Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

############################################################################## 

# 

# Copyright (c) 2000-2012 Jens Vagelpohl and Contributors. All Rights Reserved. 

# 

# This software is subject to the provisions of the Zope Public License, 

# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. 

# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED 

# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 

# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS 

# FOR A PARTICULAR PURPOSE. 

# 

############################################################################## 

""" A fake LDAP module for unit tests 

""" 

 

from copy import deepcopy 

import ldap 

 

from dataflake.fakeldap.db import DataStore 

from dataflake.fakeldap.queryparser import Parser 

from dataflake.fakeldap.utils import explode_dn 

from dataflake.fakeldap.utils import hash_pwd 

from dataflake.fakeldap.utils import utf8_string 

 

PARSER = Parser() 

ANY = PARSER.parse_query(b'(objectClass=*)') 

TREE = DataStore() 

 

 

class FakeLDAPConnection: 

 

hash_password = True 

maintain_memberof = False 

member_attr = b'member' 

memberof_attr = b'memberOf' 

 

def __init__(self, *args, **kw): 

self.args = args 

self.kwargs = kw 

self.options = {} 

self._last_bind = None 

self.start_tls_called = False 

self.parser = Parser() 

 

def set_option(self, option, value): 

self.options[option] = value 

 

@utf8_string('binduid') 

def simple_bind_s(self, binduid, bindpwd): 

self._last_bind = (self.simple_bind_s, (binduid, bindpwd), {}) 

 

if b'Manager' in binduid: 

return 1 

 

if bindpwd in (b'', ''): 

# Emulate LDAP mis-behavior 

return 1 

 

if self.hash_password: 

bindpwd = hash_pwd(bindpwd) 

 

rec = self.search_s(binduid, scope=ldap.SCOPE_BASE, 

query=b'(objectClass=*)', attrs=(b'userPassword',)) 

 

rec_pwd = rec[0][1].get(b'userPassword') 

 

if not rec_pwd: 

raise ldap.INVALID_CREDENTIALS 

 

if bindpwd == rec_pwd[0]: 

return 1 

else: 

raise ldap.INVALID_CREDENTIALS 

 

def _filter_attrs(self, entry, attrs): 

if not attrs: 

return entry 

return dict((k, v) for k, v in entry.items() if k in attrs) 

 

@utf8_string('base', 'query') 

def search_s(self, base, scope=ldap.SCOPE_SUBTREE, 

query=b'(objectClass=*)', attrs=()): 

 

parsed_query = self.parser.parse_query(query) 

tree_pos = TREE.getElementByDN(base) 

 

if self.parser.cmp_query(parsed_query, ANY, strict=True): 

# Return all objects, no matter what class 

if scope == ldap.SCOPE_BASE: 

# Only if dn matches 'base' 

return [(base, deepcopy(self._filter_attrs(tree_pos, attrs)))] 

else: 

return [(k, deepcopy(self._filter_attrs(v, attrs))) 

for k, v in tree_pos.items()] 

 

if scope == ldap.SCOPE_BASE: 

# At this stage tree_pos will be a leaf record. We need to 

# "re-wrap" it. 

rdn = explode_dn(base)[0] 

tree_pos = {rdn: tree_pos} 

 

by_level = {} 

enumerated = enumerate(self.parser.explode_query(parsed_query)) 

for idx, (operation, filters) in enumerated: 

lvl = by_level[idx] = [] 

by_filter = {} 

for fltr in filters: 

sub = fltr(tree_pos, base) 

by_filter[fltr] = sub 

# Optimization: If it's an AND query bail out on 

# the first empty value, but still set the empty 

# value on by_filter so it gets caught in the 

# operations below. 

if not sub and operation.op in (b'&',): 

break 

 

if filters: 

values = by_filter.values() 

else: 

# If there are no filters, it's an operation on 

# all the previous levels. 

values = by_level.values() 

 

if operation.op in (b'|',): 

# Do an union 

lvl_vals = dict(lvl) 

lvl_keys = set(lvl_vals.keys()) 

for sub in values: 

sub_vals = dict(sub) 

sub_keys = set(sub_vals.keys()) 

for k in sub_keys - lvl_keys: 

lvl.append((k, sub_vals[k])) 

lvl_keys = sub_keys | lvl_keys 

elif operation.op in (b'&',): 

# Do an intersection 

for sub in values: 

# Optimization: If it's an AND query bail out on 

# the first empty value. 

if not sub: 

lvl[:] = [] 

break 

if not lvl: 

lvl[:] = sub 

else: 

new_lvl = [] 

lvl_vals = dict(lvl) 

sub_vals = dict(sub) 

lvl_keys = set(lvl_vals.keys()) 

sub_keys = set(sub_vals.keys()) 

for k in sub_keys & lvl_keys: 

new_lvl.append((k, lvl_vals[k])) 

lvl[:] = new_lvl 

if by_level: 

# Return the last one. 

return [(k, deepcopy(self._filter_attrs(v, attrs))) 

for k, v in by_level[idx]] 

 

return [] 

 

@utf8_string('dn') 

def add_s(self, dn, attr_list): 

elems = explode_dn(dn) 

rdn = elems[0] 

tree_pos = TREE.getElementByDN(elems[1:]) 

 

if rdn in tree_pos: 

raise ldap.ALREADY_EXISTS(rdn) 

 

# Add rdn to attributes as well. 

rdn_key, rdn_value = rdn.split(b'=') 

tree_pos[rdn] = {rdn_key: [rdn_value]} 

record = tree_pos[rdn] 

 

for key, value in attr_list: 

record[key] = value 

 

# Maintain memberOf 

if self.maintain_memberof: 

if key == self.member_attr: 

for v in value: 

self.modify_s(v, 

[(ldap.MOD_ADD, 

self.memberof_attr, 

[dn])]) 

 

@utf8_string('dn') 

def delete_s(self, dn): 

elems = explode_dn(dn) 

rdn = elems[0] 

tree_pos = TREE.getElementByDN(elems[1:]) 

 

if rdn not in tree_pos: 

raise ldap.NO_SUCH_OBJECT(rdn) 

 

# Maintain memberOf 

if self.maintain_memberof: 

record = tree_pos[rdn] 

if self.member_attr in record: 

for value in record[self.member_attr]: 

self.modify_s(value, 

[(ldap.MOD_DELETE, 

self.memberof_attr, 

[dn])]) 

if self.memberof_attr in record: 

for value in record[self.memberof_attr]: 

self.modify_s(value, 

[(ldap.MOD_DELETE, self.member_attr, [dn])]) 

 

del tree_pos[rdn] 

 

@utf8_string('dn') 

def modify_s(self, dn, mod_list): 

elems = explode_dn(dn) 

rdn = elems[0] 

tree_pos = TREE.getElementByDN(elems[1:]) 

 

if rdn not in tree_pos: 

raise ldap.NO_SUCH_OBJECT(rdn) 

 

rec = deepcopy(tree_pos.get(rdn)) 

 

for mod in mod_list: 

if mod[0] == ldap.MOD_REPLACE: 

rec[mod[1]] = mod[2] 

elif mod[0] == ldap.MOD_ADD: 

cur_val = rec.get(mod[1], []) 

cur_val.extend(mod[2]) 

rec[mod[1]] = cur_val 

else: 

if mod[1] in rec: 

if mod[2] is None: 

del rec[mod[1]] 

else: 

cur_vals = rec[mod[1]] 

for removed in mod[2]: 

if removed in cur_vals: 

cur_vals.remove(removed) 

 

rec[mod[1]] = cur_vals 

 

tree_pos[rdn] = rec 

 

# Maintain memberOf 

if self.maintain_memberof: 

for mod in mod_list: 

if mod[1] == self.member_attr: 

if mod[0] == ldap.MOD_ADD: 

for v in mod[2]: 

self.modify_s(v, 

[(ldap.MOD_ADD, 

self.memberof_attr, 

[dn])]) 

elif mod[0] == ldap.MOD_DELETE: 

for v in mod[2]: 

self.modify_s(v, 

[(ldap.MOD_DELETE, 

self.memberof_attr, 

[dn])]) 

 

@utf8_string('dn', 'new_rdn') 

def modrdn_s(self, dn, new_rdn, *ign): 

elems = explode_dn(dn) 

rdn = elems[0] 

tree_pos = TREE.getElementByDN(elems[1:]) 

 

if rdn not in tree_pos: 

raise ldap.NO_SUCH_OBJECT(rdn) 

 

if new_rdn in tree_pos: 

raise ldap.ALREADY_EXISTS(new_rdn) 

 

rec = tree_pos.get(rdn) 

 

del tree_pos[rdn] 

tree_pos[new_rdn] = rec 

 

def start_tls_s(self): 

self.start_tls_called = True 

 

def result(self, msgid=ldap.RES_ANY, all=1, timeout=-1): 

return ('partial', [('partial result', {b'dn': b'partial result'})]) 

 

def unbind(self): 

self.unbind_s() 

 

def unbind_s(self): 

self._last_bind = None 

 

 

class RaisingFakeLDAPConnection(FakeLDAPConnection): 

 

def setExceptionAndMethod(self, raise_on, exc_class, exc_arg=None): 

if isinstance(exc_class, (list, tuple)): 

self.exception_list = list(exc_class) 

self.exception_list.reverse() 

else: 

self.exception_list = [exc_class] 

 

hideaway = '%s_old' % raise_on 

setattr(self, hideaway, getattr(self, raise_on)) 

 

def func(*args, **kw): 

if len(self.exception_list) <= 1: 

setattr(self, raise_on, getattr(self, hideaway)) 

setattr(self, 'args', args) 

setattr(self, 'kwargs', kw) 

 

exc_class = self.exception_list.pop() 

if exc_arg: 

raise exc_class(exc_arg) 

else: 

raise exc_class 

setattr(self, raise_on, func) 

 

 

class FixedResultFakeLDAPConnection(FakeLDAPConnection): 

search_results = [] 

 

def search_s(self, base, scope=ldap.SCOPE_SUBTREE, 

query=b'(objectClass=*)', attrs=()): 

return self.search_results 

 

 

class ldapobject: 

class ReconnectLDAPObject(FakeLDAPConnection): 

pass