-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldap.py
90 lines (82 loc) · 3.44 KB
/
ldap.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
''' LDAP management class
2015 (c) Alex Ivkin
v1.1
'''
from java.lang import System
from java.util import Hashtable
from javax.naming import Context
from javax.naming.directory import InitialDirContext, BasicAttributes, DirContext, SearchControls, BasicAttribute
from types import ListType
class Directory:
def __init__(self,url,user,pwd,proto):
env=Hashtable()
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory")
env.put(Context.SECURITY_AUTHENTICATION,"simple")
env.put(Context.PROVIDER_URL,url)
env.put(Context.SECURITY_PRINCIPAL,user)
env.put(Context.SECURITY_CREDENTIALS,pwd)
if proto is not None:
env.put(Context.SECURITY_PROTOCOL,proto)
ctx=InitialDirContext(env)
self.url=url
self.ctx=ctx
def __str__(self):
return self.url
def pythonize(self,results):
# convert the namingenumeration into a list of enties that are hashes of attributes that contain lists of attributes
pyresults=[]
for result in results:
pyresult={'dn':result.nameInNamespace.lower()} # force case insensitivity on the DN
for attribute in result.attributes.all:
pyresult[attribute.getID().lower()]=[value for value in attribute.all] #convert namingenumeration
pyresults.append(pyresult)
return pyresults
def add(self,loc,entry):
''' Add a new entry to the LDAP based on the provided hash that contains entry attributes and corresponding values '''
ldapentry=BasicAttributes()
for (attr,values) in entry.items(): # add hashed attributes one by one
attribute=BasicAttribute(attr)
if type(values) is ListType: # add list items one by one
for value in values:
attribute.add(value)
else:
attribute.add(values)
#print str(attribute)
ldapentry.put(attribute)
self.ctx.createSubcontext(loc,ldapentry)
def find(self,filter):
srch=SearchControls()
srch.setSearchScope(SearchControls.SUBTREE_SCOPE)
results=self.ctx.search("",filter,srch)
return self.pythonize(results)
def locate(self,dn):
''' locate a specific entry in a tree by dn
returns the located item
'''
if dn is None:
return None
try:
attributes=self.ctx.getAttributes(dn) # without this call the lookup would return a new (blank) object
pyresult={}
for attribute in attributes.all:
pyresult[attribute.getID().lower()]=[value for value in attribute.all] #convert namingenumeration
return pyresult
except:
#print "%s, %s" % (sys.exc_info()[0],sys.exc_info()[1])
return None
def poke(self,dn,attr):
''' locate a specific entry in a tree by dn
returns a specific attribute on that entry
'''
try:
return self.ctx.getAttributes(dn).get(attr).get() # a bit elaborate due to java's nastyness
except:
#print "%s, %s" % (sys.exc_info()[0],sys.exc_info()[1])
return None
def modify(self,entry,attr,values):
newAttrs=BasicAttributes(1)
newattr=BasicAttribute(attr)
for value in values:
newattr.add(value)
newAttrs.put(newattr)
self.ctx.modifyAttributes(entry,DirContext.REPLACE_ATTRIBUTE,newAttrs)