-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathfixdnattrs.py
74 lines (65 loc) · 2.08 KB
/
fixdnattrs.py
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
import os
import sys
import ldap
import ldif
import pprint
from dsadmin import DSAdmin, Entry
host = "localhost.localdomain"
port = 1100
binddn = "cn=directory manager"
bindpw = "password"
ldiffile = "/share/internal/tetframework/testcases/DS/6.0/import/airius10k.ldif"
basedn = "o=airius.com"
class ReadLdif(ldif.LDIFParser):
def __init__(
self,
input_file,
ignored_attr_types=None,max_entries=0,process_url_schemes=None
):
"""
See LDIFParser.__init__()
Additional Parameters:
all_records
List instance for storing parsed records
"""
self.dndict = {} # maps dn to Entry
self.cndict = {} # maps cn to Entry
self.dnlist = [] # contains entries in order read
self.input_file = input_file
myfile = open(input_file, "r")
ldif.LDIFParser.__init__(self,myfile,ignored_attr_types,max_entries,process_url_schemes)
self.parse()
myfile.close()
def fixattr(self,ent,attr):
val = ent.getValue(attr)
if val:
if val.startswith("cn="): return # already a DN
othent = self.cndict.get(val, None)
if not othent:
# print "Error: could not find %s under %s" % (val, basedn)
# just make something up - it's bogus anyway
val = "cn=%s,ou=imaginary,%s" % (val, basedn)
else:
val = othent.dn
ent.setValue(attr, val)
def fixdnattrs(self,attrlist):
for ent in self.dnlist:
for attr in attrlist:
self.fixattr(ent,attr)
def printit(self):
for ent in self.dnlist:
sys.stdout.write(str(ent))
def handle(self,dn,entry):
"""
Append single record to dictionary of all records.
"""
ent = Entry((dn, entry))
normdn = DSAdmin.normalizeDN(dn)
self.dndict[normdn] = ent
cn = ent.cn
if cn:
self.cndict[cn] = ent
self.dnlist.append(ent);
rdr = ReadLdif(ldiffile)
#rdr.fixdnattrs(['manager', 'secretary'])
rdr.printit()