summaryrefslogtreecommitdiff
path: root/Pivot.py
blob: 1a2782635c6096cc403b5be1d4681ee97ae05128 (plain)
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#!/usr/bin/env python

import sys, os, sets
import ldap, ldap.dn, ldap.filter, ldif
import Backend, Config

SCOPE_BASE = "0"
SCOPE_ONE = "1"
SCOPE_SUB = "2"

class Lookups:
	def __init__(self, url, binddn, password):
		self.url = url
		self.ldap = None
		self.binddn = binddn
		self.password = password


	def __connect(self, force = False):
		if self.ldap:
			if not force:
				return
			self.ldap.unbind()
		self.ldap = ldap.initialize(self.url)
		try:
			self.ldap.simple_bind_s(self.binddn, self.password)
		except ldap.LDAPError, ex:
			raise Backend.Error(Backend.OPERATIONS_ERROR,
			                    "Couldn't do internal authenticate: %s" % ex.args[0]["desc"])

	def read(self, dn, filtstr = "(objectClass=*)", attrs = None, retries = 1):
		try:
			self.__connect()
			results = self.ldap.search_s(dn, ldap.SCOPE_BASE, filtstr, attrs, 0)

			if not results:
				return None
			(dn, entry) = results[0]
			return entry

		except ldap.SERVER_DOWN, ex:
			if retries <= 0:
				raise sys.exc_type, sys.exc_value, sys.exc_traceback
			self.__connect(True)
			return self.read(dn, filtstr, attrs, retries - 1)

	def search(self, base, filtstr, attrs = [], retries = 1):
		try:
			self.__connect()
			return self.ldap.search_s(base, ldap.SCOPE_SUBTREE, filtstr, attrs, 0)

		except ldap.SERVER_DOWN, ex:
			if retries <= 0:
				raise sys.exc_type, sys.exc_value, sys.exc_traceback
			self.__connect(True)
			return self.search(base, filtstr, attrs, retries - 1)

	def modify(self, dn, mods, retries = 1):
		try:
			self.__connect()
			self.ldap.modify_s(dn, mods)

		except ldap.SERVER_DOWN, ex:
			if retries <= 0:
				raise sys.exc_type, sys.exc_value, sys.exc_traceback
			self.__connect(True)
			self.modify(dn, mods, retries - 1)

class Storage:

	def __init__(self, filename = None):
		self.filename = filename
		self.entries = { }
		self.load()

	def load(self):
		if not self.filename:
			return
		if not os.path.exists(self.filename):
			return
		input = open(self.filename, 'r')
		reader = ldif.LDIFRecordList(input)
		reader.parse()
		input.close()
		self.entries = { }
		for (dn, entry) in reader.all_records:
			self.entries[dn] = entry

	def save(self):
		if not self.filename:
			return
		output = open(self.filename, 'w')
		print >> output, "# Overwritten automatically, do not edit\n"

		writer = ldif.LDIFWriter(output)
		for (dn, entry) in self.entries.items():
			if (entry):
				writer.unparse(dn, entry)
		output.close()

	def __entry_for_dn(self, dn):
		if not self.entries.has_key(dn):
			self.entries[dn] = { }
		return self.entries[dn]

	def store(self, dn, attribute, value):
		if value is None:
			return
		entry = self.__entry_for_dn(dn)
		if not entry.has_key(attribute):
			entry[attribute] = [ ]
		if value not in entry[attribute]:
			entry[attribute].append(value)

	def remove(self, dn, attribute, value = None):
		entry = self.__entry_for_dn(dn)
		if entry.has_key(attribute):
			if value is None:
				del entry[attribute]
			elif value in entry[attribute]:
				entry[attribute] = [val for val in entry[attribute] if val != value]

	def has(self, dn, attribute, value = None):
		entry = self.__entry_for_dn(dn)
		if not entry.has_key(attribute):
			return False
		if value is None:
			return True
		return value in entry[attribute]

	def retrieve(self, dn, attribute):
		entry = self.__entry_for_dn(dn)
		if not entry.has_key(attribute):
			return [ ]
		return entry[attribute][:] # copy

	def list_attributes(self, dn):
		entry = self.__entry_for_dn(dn)
		return entry.keys()[:] # copy

	def list_dns(self):
		return self.entries.keys()[:] # copy

	def exists(self, dn):
		if not self.entries.has_key("dn"):
			return False
		return len(self.entries["dn"]) > 0

	def delete(self, dn):
		if self.entries.has_key(dn):
			del self.entries[dn]


class Static:
    def __init__(self, func):
        self.__call__ = func

class Tags:
	def __init__(self, database, tags):
		self.database = database
		self.tags = tags

	def __refresh(self, force = False):
		if not force and self.tags is not None:
			return
		try:
			results = self.database.lookups.search(self.database.search_base,
			                                       "(%s=*)" % self.database.tag_attribute,
			                                       [self.database.tag_attribute])

			tags = { }
			for (dn, entry) in results:
				for attr in entry.values():
					for value in attr:
						tags[value] = self.database.dn_attribute
			self.tags = tags

		except ldap.LDAPError, ex:
			raise Backend.Error(Backend.OPERATIONS_ERROR,
			                    "Couldn't search ldap for keys: %s" % ex.args[0]["desc"])

	def __len__(self):
		self.__refresh()
		return len(self.tags)
	def __getitem__(self, k):
		self.__refresh()
		return self.tags[k]
	def __setitem__(self, k):
		assert False
	def __delitem__(self, k):
		assert False
	def __contains__(self, k):
		self.__refresh()
		return k in self.tags
	def __iter__(self):
		self.__refresh()
		return iter(self.tags)
	def items(self):
		self.__refresh()
		return self.tags.items()


	def from_dn(dn):
		try:
			parsed = ldap.dn.str2dn(dn)
		except ValueError:
			raise Backend.Error(Backend.Error.PROTOCOL_ERROR, "invalid dn: %s" % dn)
		return Tags.from_parsed_dn(parsed)
	from_dn = Static(from_dn)

	def from_parsed_dn(parsed):
		tags = { }
		for (typ, val, num) in parsed[0]:
			tags[val] = typ
		return Tags(None, tags)
	from_parsed_dn = Static(from_parsed_dn)

	def from_database(database):
		return Tags(database, None)
	from_database = Static(from_database)


def parse_dn(dn):
	try:
		return ldap.dn.str2dn(dn)
	except:
		raise Backend.Error(Backend.PROTOCOL_ERROR, "Invalid dn: %s" % dn)

def is_parsed_dn_parent(dn, parent):
	if len(dn) != len(parent) + 1:
		return False
	# Go backwards and validate each parent
	for i in range(-1, -1 - len(parent)):
		if dn[i] != parent[i]:
			return False
	return True

class Database(Backend.Database):
	def __init__(self, suffix):
		Backend.Database.__init__(self, suffix)

		self.rootdn = Config.require("ldap-root")
		self.search_base = Config.require("ldap-base")
		self.lookups = Lookups(Config.require("ldap-host"), self.rootdn,
		                       Config.require("ldap-password"))
		self.suffix_dn = parse_dn(self.suffix)

		self.dn_attribute = Config.require("rdn-attribute")
		self.object_class = Config.require("ref-objectclass")
		self.ref_attribute = Config.require("ref-attribute")
		self.key_attribute = Config.require("key-attribute")
		self.access_attribute = Config.require("access-attribute")
		self.tag_attribute = Config.require("tag-attribute")

		filename = Config.option("storage-file")
		self.storage = Storage(filename)


	def __search_tag_keys(self, tags):

		if not len(tags):
			return []

		# Build up a filter
		filter = [ldap.filter.filter_format("(%s=%s)", (self.tag_attribute, tag)) for tag in tags]
		if len(filter) > 1:
			filter = "(&" + "".join(filter) + ")"
		else:
			filter = filter[0]

		try:
			# Search for all those guys
			results = self.lookups.search(self.search_base, filter, [ self.key_attribute ])

		except ldap.LDAPError, ex:
			raise Backend.Error(Backend.OPERATIONS_ERROR,
			                    "Couldn't search ldap for tags: %s" % ex.args[0]["desc"])

		return [entry[self.key_attribute][0] for (dn, entry) in results if entry[self.key_attribute]]


	def __search_key_dns(self, key):

		# Build up a filter
		filter = ldap.filter.filter_format("(%s=%s)", (self.key_attribute, key))

		try:
			# Do the actual search
			results = self.lookups.search(self.search_base, filter)

		except ldap.LDAPError, ex:
			raise Backend.Error(Backend.OPERATIONS_ERROR,
			                    "Couldn't search ldap for keys: %s" % ex.args[0]["desc"])

		return [dn for (dn, entry) in results if dn]


	def __build_root_entry(self, tags):
		attrs = {
			"objectClass" : [ "top" ],
			"hasSubordinates" : [  ]
		}

		for (typ, val, num) in self.suffix_dn[0]:
			if not attrs.has_key(typ):
				attrs[typ] = [ ]
			attrs[typ].append(val)

		attrs["hasSubordinates"].append(tags and "TRUE" or "FALSE")
		return (self.suffix, attrs)


	def __build_pivot_entry(self, tags, keys):
		attrs = {
			self.ref_attribute : [ ],
			"objectClass" : [ self.object_class ],
			"hasSubordinates" : [ "FALSE" ]
		}

		# Build up a DN, and relevant attrs
		rdn = []
		for tag, typ in tags.items():
			rdn.append((typ, tag, 1))
			if not attrs.has_key(typ):
				attrs[typ] = [ ]
			attrs[typ].append(tag)
		dn = [ rdn ]
		dn.extend(self.suffix_dn)
		dn = ldap.dn.dn2str(dn)

		for key in keys:
			attrs[self.ref_attribute].append(key)

		return (dn, attrs)


	def __build_storage_entry(self, parsed_dn, keys):
		attrs = {
			self.ref_attribute : [ ]
		}

		# Build up DN relevant attrs
		for (typ, val, num) in parsed_dn[0]:
			if not attrs.has_key(typ):
				attrs[typ] = [ ]
			attrs[typ].append(val)

		for key in keys:
			attrs[self.ref_attribute].append(key)

		# All other storage attributes retrieved later if necessary
		return (ldap.dn.dn2str(parsed_dn), attrs)


	def __complete_results(self, args, entries):
		# TODO: Support sizelimit
		# TODO: Support a filter

		# Only return the attribute names?
		only_names = (args["attrsonly"] == "1")
		which_attrs = args["attrs"]
		all_attrs = (which_attrs == "all" or
		             which_attrs == "*" or
		             which_attrs == "+")
		which_attrs = which_attrs.split(" ")

		# Convert results from our map to a list with (dn, entry) tuples
		results = [ ]

		for (dn, entry) in entries.items():

			# Retrieve extra value names
			extra = self.storage.list_attributes(dn)

			# Only return attribute names
			if only_names:
				for attr in extra:
					entry[attr] = [ "" ]
				for attr in entry:
					entry[attr] = [ "" ]

			# Return extra attribute names and values
			else:
				for attr in extra:
					values = self.storage.retrieve(dn, attr)
					if entry.has_key(attr):
						entry[attr].extend(values)
					else:
						entry[attr] = values
					# Remove all duplicates
					entry[attr] = list(set(entry[attr]))

			# Limit to the attributes requested
			if not all_attrs:
				for attr in entry.keys():
					if attr not in which_attrs:
						del entry[attr]

			results.append((dn, entry))

		return results


	def search(self, dn, args):
		results = { }
		parsed = parse_dn(dn)

		# Arguments sent
		scope = args["scope"] or SCOPE_BASE

		# Start at the root
		if parsed == self.suffix_dn:
			tags = Tags.from_database(self)
			if scope == SCOPE_BASE or scope == SCOPE_SUB:
				(dn, entry) = self.__build_root_entry(tags)
				results[dn] = entry
			if scope == SCOPE_ONE or scope == SCOPE_SUB:
				# Process each tag individually, by default
				for (tag, typ) in tags.items():
					ctags = { tag : typ }
					keys = self.__search_tag_keys(ctags)
					(child, entry) = self.__build_pivot_entry(ctags, keys)
					results[child] = entry
				# Process all extra storage items
				for child in self.storage.list_dns():
					if child not in results:
						cparsed = parse_dn(child)
						ctags = Tags.from_parsed_dn(cparsed)
						keys = self.__search_tag_keys(ctags)
						(child, entry) = self.__build_storage_entry(parse_dn(child), keys)
						results[child] = entry

		# Something in the database
		elif self.storage.exists(dn):
			if scope == SCOPE_BASE or scope == SCOPE_SUB:
				tags = Tags.from_parsed_dn(parsed)
				keys = self.__search_tag_keys(tags)
				(dn, entry) = self.__build_storage_entry(parsed, keys)
				results[dn] = entry

		# Start at a tag
		elif is_parsed_dn_parent(parsed, self.suffix_dn):
			if scope == SCOPE_BASE or scope == SCOPE_SUB:
				tags = Tags.from_parsed_dn(parsed)
				keys = self.__search_tag_keys(tags)
				if keys:
					(dn, entry) = self.__build_pivot_entry(tags, keys)
					results[dn] = entry


		# We don't have that base
		else:
			raise Backend.Error(Backend.NO_SUCH_OBJECT, "DN '%s' does not exist" % dn)

		return self.__complete_results(args, results)


	def __build_key_mods(self, key, tags, op, mods):
		dns = self.__search_key_dns(key)
		if not dns:
			raise Backend.Error(Backend.CONSTRAINT_VIOLATION,
			                    "Cannot find an entry for %s '%s'" % (self.key_attribute, key))
		for dn in dns:
			if not mods.has_key(dn):
				mods[dn] = (key, [])
			for tag in tags:
				mods[dn][1].append((op, self.tag_attribute, tag))


	def __check_write_access(self, dn):
		if self.binddn == self.rootdn:
			return True
		return self.storage.has(dn, self.access_attribute, self.binddn)


	def add(self, dn, entry):

		parsed = parse_dn(dn)
		tags = Tags.from_parsed_dn(parsed)

		if parsed == self.suffix_dn:
			raise Backend.Error(Backend.ALREADY_EXISTS, "This entry already exists: %s" % dn)
		if not is_parsed_dn_parent(parsed, self.suffix_dn):
			raise Backend.Error(Backend.NO_SUCH_OBJECT, "Parent of '%s' does not exist or is not a valid place for an entry" % dn)
		if self.storage.exists(dn):
			raise Backend.Error(Backend.ALREADY_EXISTS, "This entry already exists: %s" % dn)
		if len(self.__search_tag_keys(tags)):
			raise Backend.Error(Backend.ALREADY_EXISTS, "This entry already exists: %s" % dn)

		# Everyone has implicit access to create a new group

		# Convert into a modify change set
		mods = []
		for (attr, values) in entry.items():
			for value in values:
				mods.append((ldap.MOD_ADD, attr, value))

		# Add an access attribute for the creator
		if self.binddn and not self.access_attribute in entry :
			mods.append((ldap.MOD_ADD, self.access_attribute, self.binddn))

		# Make the actual changes
		self.__change(parsed, mods, tags)

		# Save extra attributes to storage
		self.storage.save()


	def delete(self, dn, args):
		parsed = parse_dn(dn)
		tags = Tags.from_parsed_dn(parsed)

		if parsed == self.suffix_dn:
			raise Backend.Error(Backend.NOT_ALLOWED_ON_NONLEAF, "Cannot delete the root entry: %s" % dn)
		if not is_parsed_dn_parent(parsed, self.suffix_dn):
			raise Backend.Error(Backend.NO_SUCH_OBJECT, "Entry does not exist: %s" % dn)

		if not self.__check_write_access(dn):
			raise Backend.Error(Backend.INSUFFICIENT_ACCESS, "Access denied to delete entry: %s" % dn)

		mods = []
		mods.append((ldap.MOD_DELETE, self.ref_attribute, None))

		# Make the actual changes
		self.__change(parsed, mods, tags)

		# Delete extra attributes from storage
		self.storage.delete(dn)
		self.storage.save()


	def modify(self, dn, mods):
		parsed = parse_dn(dn)
		tags = Tags.from_parsed_dn(parsed)

		if dn == self.suffix:
			raise Backend.Error(Backend.INSUFFICIENT_ACCESS, "Cannot modify root dn of pivot area: %s" % dn)
		if not is_parsed_dn_parent (parsed, self.suffix_dn):
			raise Backend.Error(Backend.NO_SUCH_OBJECT, "DN '%s' does not exist" % dn)

		if not self.__check_write_access(dn):
			raise Backend.Error(Backend.INSUFFICIENT_ACCESS, "Access denied to modify entry: %s" % dn)

		# Make the actual changes
		self.__change(parsed, mods, tags)

		# Save extra attributes to storage
		self.storage.save()


	def __change(self, parsed, mods, tags):

		add_keys = sets.Set()
		remove_keys = sets.Set()
		remove_all = False

		# Parse out all the adds and removes
		for (op, attr, value) in mods:

			# Process access attributes later
			if attr != self.ref_attribute:
				continue

			if op == ldap.MOD_ADD:
				if value:
					add_keys.add(value)
			elif op == ldap.MOD_REPLACE:
				remove_all = True
				if value:
					add_keys.add(value)
			elif op == ldap.MOD_DELETE:
				if value:
					remove_keys.add(value)
				else:
					remove_all = True
			else:
				continue

		# Remove all of the ref attribute
		if remove_all:
			for key in self.__search_tag_keys (tags):
				remove_keys.add(key)

		# Make them all unique, and non conflicting
		for key in add_keys.copy():
			if key in remove_keys:
				remove_keys.remove(key)
				add_keys.remove(key)

		# Change them to DNs, and build mods objects for each dn
		keys_and_mods_by_dn = { }
		for key in add_keys:
			self.__build_key_mods(key, tags, ldap.MOD_ADD, keys_and_mods_by_dn)
		for key in remove_keys:
			self.__build_key_mods(key, tags, ldap.MOD_DELETE, keys_and_mods_by_dn)


		# Now perform the actual actions, combining errors
		errors = []
		for (dn, (key, mod)) in keys_and_mods_by_dn.items():
			try:
				self.lookups.modify(dn, mod)
			except (ldap.TYPE_OR_VALUE_EXISTS, ldap.NO_SUCH_ATTRIBUTE):
				continue
			except ldap.NO_SUCH_OBJECT:
				errors.append(key)
			except ldap.LDAPError, ex:
				raise Backend.Error(Backend.OPERATIONS_ERROR,
				                    "Couldn't perform one of the modifications: %s" % ex.args[0]["desc"])

		# Send back errors
		if errors:
			raise Backend.Error(Backend.CONSTRAINT_VIOLATION,
			                    "Couldn't change %s for %s" % (self.key_attribute, ", ".join(errors)))

		# Process other attributes now
		dn = ldap.dn.dn2str(parsed)
		for (op, attr, value) in mods:
			if attr == self.ref_attribute:
				continue
			if op == ldap.MOD_ADD:
				self.storage.store(dn, attr, value)
			elif op == ldap.MOD_REPLACE:
				self.storage.remove(dn, attr)
				self.storage.add(dn, attr, value)
			elif op == ldap.MOD_DELETE:
				self.storage.remove(dn, attr, value)