Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import ldap
- import base64
- import hashlib
- import binascii
- import ldap.modlist as modlist
- import subprocess
- LDAP_HOST = "ldap.corp.nosa.com"
- LDAP_DN = "ou=People,dc=nosa,dc=com"
- LDAP_USER = "cn=root,dc=nosa,dc=com"
- LDAP_PASS = ""
- class WDJLdap(object):
- def __init__(self, ldap_host=LDAP_HOST, ldap_dn=LDAP_DN, ldap_user=LDAP_USER, ldap_pass=LDAP_PASS):
- self.ldapconn = ldap.initialize('ldap://%s' % ldap_host)
- self.ldapconn.simple_bind_s("cn=root,dc=nosa,dc=com", ldap_pass)
- def list_user(self):
- retval = self.ldapconn.search_s(
- 'ou=People,dc=nosa,dc=com', ldap.SCOPE_SUBTREE, '(uid=*)', ['*'])
- return retval
- def search_user(self, uid):
- retval = self.ldapconn.search_s(
- 'ou=People,dc=nosa,dc=com', ldap.SCOPE_SUBTREE, '(uid=%s)' % uid, ['*'])
- return retval
- def get_email(self, uid):
- retval = self.ldapconn.search_s(
- 'ou=People,dc=nosa,dc=com', ldap.SCOPE_SUBTREE, '(uid=%s)' % uid, ['mail'])
- return retval[0][1]['mail'][0]
- def list_staff_user(self):
- retval = self.ldapconn.search_s(
- 'ou=Group,dc=nosa,dc=com', ldap.SCOPE_SUBTREE, '(cn=staff)', ['*'])
- return retval
- def add_user(self, firstname, lastname, email, password):
- name = firstname + " " + lastname
- uid = email[:-14]
- shatmp = hashlib.sha1()
- shatmp.update(password)
- shatmp1 = shatmp.hexdigest()
- shatmp2 = binascii.unhexlify(shatmp1)
- shatmp3 = base64.encodestring(shatmp2)
- shatmp4 = shatmp3.strip()
- shapasswd = "{SHA}" + shatmp4
- info = {'cn': [name,],
- 'displayName': [name,],
- 'givenName': [firstname,],
- 'sn': [lastname,],
- 'mail': [email,],
- 'uid': [uid,],
- 'userPassword': [shapasswd,],
- 'objectclass': ['top', 'person', 'organizationalPerson', 'inetOrgPerson']
- }
- dn = 'uid=%s,ou=People,dc=nosa,dc=com' % uid
- attr = [(k, v) for (k, v) in info.items()]
- self.ldapconn.add_s(dn, attr)
- group_dn = "cn=staff,ou=Group,dc=nosa,dc=com"
- mod_attr = [ (ldap.MOD_ADD,'member',dn )]
- self.ldapconn.modify_s(group_dn, mod_attr)
- def change_passwd(self, username, newpass):
- dn = 'uid=%s,ou=People,dc=nosa,dc=com' % username
- shatmp = hashlib.sha1()
- shatmp.update(newpass)
- shatmp1 = shatmp.hexdigest()
- shatmp2 = binascii.unhexlify(shatmp1)
- shatmp3 = base64.encodestring(shatmp2)
- shatmp4 = shatmp3.strip()
- shapasswd = "{SHA}" + shatmp4
- mod_attr = [ (ldap.MOD_REPLACE,'userPassword',shapasswd )]
- self.ldapconn.modify_s(dn,mod_attr)
- def delete_user_in_group(self, uid):
- dn = 'uid=%s,ou=People,dc=nosa,dc=com' % uid
- group_dn = "cn=staff,ou=Group,dc=nosa,dc=com"
- mod_attr = [ (ldap.MOD_DELETE,'member',dn )]
- self.ldapconn.modify_s(group_dn, mod_attr)
- def del_user(self, uid):
- dn = 'uid=%s,ou=People,dc=nosa,dc=com' % uid
- email = self.get_email(uid)
- self.ldapconn.delete_s(dn)
- self.delete_user_in_group(uid)
- cmd = "bash -x delete_gerrit_user.sh %s" % email
- ret = subprocess.call(cmd,shell=True)
- if ret != 0:
- return False
- def has_user(self, uid):
- retval = self.ldapconn.search_s(
- 'ou=People,dc=nosa,dc=com', ldap.SCOPE_SUBTREE, '(uid=%s)' % uid, ['*'])
- print retval
- if len(retval):
- return True
- else:
- return False
- def unbind_link(self):
- """release the connection to ldap server"""
- self.ldapconn.unbind()
- def main():
- demo = WDJLdap()
- print demo.search_user("username")
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement