2007-01-27 18:41:51 -05:00
|
|
|
"""
|
2007-12-13 04:20:25 -05:00
|
|
|
LDAP Utilities
|
2007-01-27 18:41:51 -05:00
|
|
|
|
|
|
|
This module makes use of python-ldap, a Python module with bindings
|
|
|
|
to libldap, OpenLDAP's native C client library.
|
|
|
|
"""
|
2007-12-16 18:06:09 -05:00
|
|
|
import ldap.modlist, os, pwd
|
|
|
|
from subprocess import Popen, PIPE
|
2007-01-27 18:41:51 -05:00
|
|
|
|
2007-12-12 00:39:44 -05:00
|
|
|
|
2007-12-16 18:06:09 -05:00
|
|
|
def connect_sasl(uri, mech, realm, password):
|
2007-01-27 18:41:51 -05:00
|
|
|
|
2008-12-20 18:49:02 -05:00
|
|
|
try:
|
|
|
|
# open the connection
|
|
|
|
ld = ldap.initialize(uri)
|
|
|
|
|
|
|
|
# authenticate
|
|
|
|
sasl = Sasl(mech, realm, password)
|
|
|
|
ld.sasl_interactive_bind_s('', sasl)
|
|
|
|
|
|
|
|
except ldap.LOCAL_ERROR, e:
|
|
|
|
raise e
|
|
|
|
|
|
|
|
except:
|
|
|
|
print "Shit, something went wrong!"
|
2007-01-27 18:41:51 -05:00
|
|
|
|
2007-12-13 04:20:25 -05:00
|
|
|
return ld
|
2007-01-27 18:41:51 -05:00
|
|
|
|
|
|
|
|
2007-12-13 04:20:25 -05:00
|
|
|
def abslookup(ld, dn, objectclass=None):
|
2007-01-27 18:41:51 -05:00
|
|
|
|
2007-12-13 04:20:25 -05:00
|
|
|
# search for the specified dn
|
|
|
|
try:
|
|
|
|
if objectclass:
|
|
|
|
search_filter = '(objectclass=%s)' % escape(objectclass)
|
|
|
|
matches = ld.search_s(dn, ldap.SCOPE_BASE, search_filter)
|
|
|
|
else:
|
|
|
|
matches = ld.search_s(dn, ldap.SCOPE_BASE)
|
|
|
|
except ldap.NO_SUCH_OBJECT:
|
|
|
|
return None
|
2007-12-18 01:22:09 -05:00
|
|
|
|
2007-12-13 04:20:25 -05:00
|
|
|
# dn was found, but didn't match the objectclass filter
|
|
|
|
if len(matches) < 1:
|
|
|
|
return None
|
2007-01-27 18:41:51 -05:00
|
|
|
|
2007-12-13 04:20:25 -05:00
|
|
|
# return the attributes of the single successful match
|
|
|
|
match = matches[0]
|
|
|
|
match_dn, match_attributes = match
|
|
|
|
return match_attributes
|
2007-01-27 18:41:51 -05:00
|
|
|
|
|
|
|
|
2007-12-13 04:20:25 -05:00
|
|
|
def lookup(ld, rdntype, rdnval, base, objectclass=None):
|
|
|
|
dn = '%s=%s,%s' % (rdntype, escape(rdnval), base)
|
|
|
|
return abslookup(ld, dn, objectclass)
|
2007-01-27 18:41:51 -05:00
|
|
|
|
2007-02-15 01:44:56 -05:00
|
|
|
|
2008-01-13 21:12:13 -05:00
|
|
|
def search(ld, base, search_filter, params=[], scope=ldap.SCOPE_SUBTREE, attrlist=None, attrsonly=0):
|
2007-02-18 19:46:15 -05:00
|
|
|
|
2007-12-13 04:20:25 -05:00
|
|
|
real_filter = search_filter % tuple(escape(x) for x in params)
|
2007-02-18 19:46:15 -05:00
|
|
|
|
2007-12-13 04:20:25 -05:00
|
|
|
# search for entries that match the filter
|
|
|
|
matches = ld.search_s(base, scope, real_filter, attrlist, attrsonly)
|
|
|
|
return matches
|
2007-01-27 18:41:51 -05:00
|
|
|
|
2007-02-18 19:46:15 -05:00
|
|
|
|
2007-12-13 04:20:25 -05:00
|
|
|
def modify(ld, rdntype, rdnval, base, mlist):
|
|
|
|
dn = '%s=%s,%s' % (rdntype, escape(rdnval), base)
|
|
|
|
ld.modify_s(dn, mlist)
|
2007-02-18 19:46:15 -05:00
|
|
|
|
|
|
|
|
2007-12-13 04:20:25 -05:00
|
|
|
def modify_attrs(ld, rdntype, rdnval, base, old, attrs):
|
|
|
|
dn = '%s=%s,%s' % (rdntype, escape(rdnval), base)
|
2007-02-18 19:46:15 -05:00
|
|
|
|
2007-12-13 04:20:25 -05:00
|
|
|
# build list of modifications to make
|
|
|
|
changes = ldap.modlist.modifyModlist(old, attrs)
|
2007-02-18 19:46:15 -05:00
|
|
|
|
2007-12-13 04:20:25 -05:00
|
|
|
# apply changes
|
|
|
|
ld.modify_s(dn, changes)
|
2007-02-18 19:46:15 -05:00
|
|
|
|
|
|
|
|
2007-12-13 04:20:25 -05:00
|
|
|
def modify_diff(ld, rdntype, rdnval, base, old, new):
|
|
|
|
dn = '%s=%s,%s' % (rdntype, escape(rdnval), base)
|
2007-02-18 19:46:15 -05:00
|
|
|
|
2007-12-13 04:20:25 -05:00
|
|
|
# build list of modifications to make
|
|
|
|
changes = make_modlist(old, new)
|
2007-02-18 19:46:15 -05:00
|
|
|
|
2007-12-13 04:20:25 -05:00
|
|
|
# apply changes
|
|
|
|
ld.modify_s(dn, changes)
|
2007-02-18 19:46:15 -05:00
|
|
|
|
|
|
|
|
2007-12-13 04:20:25 -05:00
|
|
|
def escape(value):
|
|
|
|
"""
|
|
|
|
Escapes special characters in a value so that it may be safely inserted
|
|
|
|
into an LDAP search filter.
|
|
|
|
"""
|
2007-02-18 19:46:15 -05:00
|
|
|
|
2007-12-13 04:20:25 -05:00
|
|
|
value = str(value)
|
|
|
|
value = value.replace('\\', '\\5c').replace('*', '\\2a')
|
|
|
|
value = value.replace('(', '\\28').replace(')', '\\29')
|
|
|
|
value = value.replace('\x00', '\\00')
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
def make_modlist(old, new):
|
|
|
|
keys = set(old.keys()).union(set(new))
|
|
|
|
mlist = []
|
|
|
|
for key in keys:
|
|
|
|
if key in old and not key in new:
|
|
|
|
mlist.append((ldap.MOD_DELETE, key, list(set(old[key]))))
|
|
|
|
elif key in new and not key in old:
|
|
|
|
mlist.append((ldap.MOD_ADD, key, list(set(new[key]))))
|
|
|
|
else:
|
|
|
|
to_add = list(set(new[key]) - set(old[key]))
|
|
|
|
if len(to_add) > 0:
|
|
|
|
mlist.append((ldap.MOD_ADD, key, to_add))
|
|
|
|
to_del = list(set(old[key]) - set(new[key]))
|
|
|
|
if len(to_del) > 0:
|
|
|
|
mlist.append((ldap.MOD_DELETE, key, to_del))
|
|
|
|
return mlist
|
2007-11-15 05:28:58 -05:00
|
|
|
|
2007-01-27 18:41:51 -05:00
|
|
|
|
2007-12-13 23:53:15 -05:00
|
|
|
def format_ldaperror(ex):
|
|
|
|
desc = ex[0].get('desc', '')
|
|
|
|
info = ex[0].get('info', '')
|
|
|
|
if desc and info:
|
|
|
|
return "%s: %s" % (desc, info)
|
|
|
|
elif desc:
|
|
|
|
return desc
|
|
|
|
else:
|
|
|
|
return str(ex)
|
|
|
|
|
|
|
|
|
2007-11-27 19:20:37 -05:00
|
|
|
class Sasl:
|
|
|
|
|
2007-12-16 18:06:09 -05:00
|
|
|
def __init__(self, mech, realm, password):
|
2007-11-27 19:20:37 -05:00
|
|
|
self.mech = mech
|
2007-12-12 02:13:03 -05:00
|
|
|
self.realm = realm
|
2007-11-27 19:20:37 -05:00
|
|
|
|
2007-12-16 18:06:09 -05:00
|
|
|
if mech == 'GSSAPI' and password is not None:
|
|
|
|
userid = pwd.getpwuid(os.getuid()).pw_name
|
|
|
|
kinit = '/usr/bin/kinit'
|
|
|
|
kinit_args = [ kinit, '%s@%s' % (userid, realm) ]
|
|
|
|
kinit = Popen(kinit_args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
|
|
|
|
kinit.stdin.write('%s\n' % password)
|
|
|
|
kinit.wait()
|
|
|
|
|
2007-11-27 19:20:37 -05:00
|
|
|
def callback(self, id, challenge, prompt, defresult):
|
2007-12-06 02:04:43 -05:00
|
|
|
return ''
|