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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
#!/usr/bin/env python
from __future__ import with_statement
import sys, os, time, re
import threading, mutex
import SocketServer, StringIO
import ldap, ldif
debug = 0
OPERATIONS_ERROR = 0x01
PROTOCOL_ERROR = 0x02
TIMELIMIT_EXCEEDED = 0x03
SIZELIMIT_EXCEEDED = 0x04
COMPARE_FALSE = 0x05
COMPARE_TRUE = 0x06
AUTH_METHOD_NOT_SUPPORTED = 0x07
STRONG_AUTH_NOT_SUPPORTED = 0x07
STRONG_AUTH_REQUIRED = 0x08
STRONGER_AUTH_REQUIRED = 0x08
PARTIAL_RESULTS = 0x09
ADMINLIMIT_EXCEEDED = 0x0b
CONFIDENTIALITY_REQUIRED = 0x0d
SASL_BIND_IN_PROGRESS = 0x0e
NO_SUCH_ATTRIBUTE = 0x10
UNDEFINED_TYPE = 0x11
INAPPROPRIATE_MATCHING = 0x12
CONSTRAINT_VIOLATION = 0x13
TYPE_OR_VALUE_EXISTS = 0x14
INVALID_SYNTAX = 0x15
NO_SUCH_OBJECT = 0x20
ALIAS_PROBLEM = 0x21
INVALID_DN_SYNTAX = 0x22
IS_LEAF = 0x23
ALIAS_DEREF_PROBLEM = 0x24
X_PROXY_AUTHZ_FAILURE = 0x2F
INAPPROPRIATE_AUTH = 0x30
INVALID_CREDENTIALS = 0x31
INSUFFICIENT_ACCESS = 0x32
BUSY = 0x33
UNAVAILABLE = 0x34
UNWILLING_TO_PERFORM = 0x35
LOOP_DETECT = 0x36
NAMING_VIOLATION = 0x40
OBJECT_CLASS_VIOLATION = 0x41
NOT_ALLOWED_ON_NONLEAF = 0x42
NOT_ALLOWED_ON_RDN = 0x43
ALREADY_EXISTS = 0x44
NO_OBJECT_CLASS_MODS = 0x45
RESULTS_TOO_LARGE = 0x46
AFFECTS_MULTIPLE_DSAS = 0x47
OTHER_ERROR = 0x50
def split_argument(line):
parts = line.strip().split(':', 2)
name = parts[0].strip()
value = ""
if len(parts) == 2:
value = parts[1].strip()
return (name, value)
class Parser(ldif.LDIFParser):
def __init__(self, input):
ldif.LDIFParser.__init__(self, input)
self.dn = None
self.entry = None
def handle(self, dn, entry):
if self.entry is None:
self.dn = dn
self.entry = entry
class Database:
def __init__(self, suffix):
self.suffix = suffix
self.binddn = None
self.remote = None
self.mutex = threading.Lock()
# Overridable to handle specific command
def add(self, dn, entry):
raise VirtualError, (UNWILLING_TO_PERFORM, "Add not implemented")
def bind(self, dn, args):
raise VirtualError, (UNWILLING_TO_PERFORM, "Bind not implemented")
def compare(self, dn, entry):
raise VirtualError, (UNWILLING_TO_PERFORM, "Compare not implemented")
def delete(self, dn, args):
raise VirtualError, (UNWILLING_TO_PERFORM, "Delete not implemented")
def modify(self, dn, modlist):
raise VirtualError, (UNWILLING_TO_PERFORM, "Modify not implemented")
def modrdn(self, dn, args):
raise VirtualError, (UNWILLING_TO_PERFORM, "ModRDN not implemented")
def search(self, dn, args):
raise VirtualError, (UNWILLING_TO_PERFORM, "Search not implemented")
# Overridable to handle all processing
def process(self, command, dn, block):
try:
# This we handle specially
if command == "MODIFY":
return self.process_modify_internal(dn, block)
# This we handle specially
elif command == "ADD":
return self.process_add_internal(dn, block)
# All the rest we split up, multiple args go into arrays
args = Arguments()
while True:
line = block.readline()
if len(line) == 0:
break
(name, value) = split_argument(line)
args.add(name, value)
if command == "BIND":
self.bind(dn, args)
elif command == "COMPARE":
result = self.compare(dn, args)
return (result, "", None)
elif command == "DELETE":
self.delete(dn, args)
elif command == "MODIFY":
self.modify(dn, args)
elif command == "MODRDN":
self.modrdn(dn, args)
elif command == "SEARCH":
return self.process_search_internal(dn, args)
elif command == "UNBIND":
assert False # should have been handled in caller
else:
return UNWILLING_TO_PERFORM, "Unsupported operation %s" % command
return (0, "", [])
except Error, ex:
return (ex.code, ex.info, None)
def process_locked(self, command, dn, block, binddn, remote):
with self.mutex:
self.binddn = binddn
self.remote = remote
result = self.process(command, dn, block)
return result
def process_add_internal(self, dn, block):
parser = Parser(block)
parser.parse()
self.add(dn, parser.entry)
return (0, "", [])
def process_compare_intersal(self, dn, block):
parser = Parser(block)
parser.parse()
result = self.compare(dn, parser.entry)
return (result, "", [])
def process_search_internal(self, dn, args):
results = []
data = self.search(args["base"] or dn, args)
for (dn, entry) in data:
result = StringIO.StringIO()
writer = ldif.LDIFWriter(result)
writer.unparse(dn, entry)
results.append(result.getvalue())
return (0, "", results)
def process_modify_internal(self, dn, block):
op = None
attr = None
batch = 0
mods = []
while True:
line = block.readline()
if len(line) == 0:
break
line = line.strip()
# A break between different mods
if line == "-":
# Latch batch was empty, delete/replace all
if batch == 0:
if op == ldap.MOD_DELETE:
mods.append((op, attr, None))
elif op == ldap.MOD_REPLACE:
mods.append((op, attr, None))
batch = 0
op = None
attr = None
continue
# The current line
(name, value) = split_argument(line)
# Don't have a mod type yet
if op is None:
attr = value
if name == "add":
op = ldap.MOD_ADD
elif name == "replace":
op = ldap.MOD_REPLACE
elif name == "delete":
op = ldap.MOD_DELETE
else:
op = None
# Have a op, add values
elif name == attr:
assert attr is not None
mods.append((op, attr, value))
batch += 1
print mods
self.modify(dn, mods)
return (0, "", [])
class Error(Exception):
"""Exception to be returned to server"""
def __init__(self, code = OPERATIONS_ERROR, info = ""):
self.code = code
self.info = info
def __str__(self):
return "%d: %s" % (self.code, self.info)
class Arguments:
def __init__(self):
self.dict = {}
def add(self, name, value):
if self.dict.has_key(name):
if type(self.dict[name]) != type([]):
self.dict[name] = [self.dict[name]]
self.dict[name].append(value)
else:
self.dict[name] = value
self.dict[name] = value
def __getitem__(self, name):
if self.dict.has_key(name):
return self.dict[name]
return None
def __len__(self):
return len(self.dict)
class Connection(SocketServer.BaseRequestHandler):
def __init__(self, request, client_address, server):
server.unique += 1
self.identifier = server.unique
self.block_regex = re.compile("\r?\n[ \t]*\r?\n")
SocketServer.BaseRequestHandler.__init__(self, request, client_address, server)
def trace(self, message):
global debug
if debug:
prefix = "%04d " % self.identifier
lines = message.split("\n")
print >> sys.stderr, prefix + lines[0]
print >> sys.stderr, "\n".join([prefix + "*** " + line for line in lines[1:]])
def handle(self):
self.trace("CONNECTED")
req = self.request
req.setblocking(1)
req.settimeout(None)
extra = ""
block = None
while True:
data = extra + req.recv(1024)
# End of connection
if len(data) == 0:
break
parts = self.block_regex.split(data)
if len(parts) > 1:
block = unicode(parts[0], "utf-8", "strict")
break
extra = parts[0]
if block:
self.trace("REQUEST\n%s\n" % block)
self.handle_block(req, StringIO.StringIO(block))
self.trace("DISCONNECTING")
self.request.close()
def handle_block(self, req, block):
line = block.readline()
command = line.strip()
# Disconnect immediately on certain occasions
if not command:
return False
elif command == "UNBIND":
return False
suffixes = []
binddn = None
remote = None
ssf = None
msgid = None
dn = None
while True:
off = block.tell()
line = block.readline()
(name, value) = split_argument(line)
if name == "suffix":
suffixes.append(value)
elif name == "msgid" and msgid is None:
msgid = value
elif name == "dn" and dn is None:
dn = value.lower()
elif name == "binddn" and binddn is None:
binddn = value.lower()
elif name == "peername" and remote is None:
remote = value
elif name == "ssf" and ssf is None:
ssf = value
else: # Return this line and continue
block.seek(off)
break
code = 0
info = ""
data = None
if len(suffixes) == 0:
code = OPERATIONS_ERROR
info = "No suffix specified"
elif len(suffixes) > 1:
code = OPERATIONS_ERROR
info = "Multiple suffixes not supported"
else:
database = self.server.find_database(suffixes[0])
(code, info, data) = database.process_locked(command, dn, block, binddn, remote)
if data:
for dat in data:
self.trace("DATA\n%s\n" % dat)
req.sendall(dat.strip("\n") + "\n\n")
result = "RESULT"
if info:
result += "\ninfo: %s" % info
# BUG: Current OpenLDAP always wants code last
result += "\ncode: %d" % code
self.trace("RESPONSE\n%s" % result.strip("\n"))
req.sendall(result)
return True
class Server(SocketServer.ThreadingMixIn, SocketServer.UnixStreamServer):
daemon_threads = True
allow_reuse_address = True
def __init__(self, address, DatabaseClass, debug = False):
if (os.path.exists(address)):
os.unlink(address)
SocketServer.UnixStreamServer.__init__(self, address, Connection)
self.DatabaseClass = DatabaseClass
self.__databases = {}
self.unique = 0
def find_database(self, suffix):
suffix = suffix.lower()
if self.__databases.has_key(suffix):
database = self.__databases[suffix]
else:
database = self.DatabaseClass(suffix)
self.__databases[suffix] = database
return database
|