Advertisement
Guest User

Untitled

a guest
Jul 4th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import ldap
  4. import base64
  5. import hashlib
  6. import binascii
  7. import ldap.modlist as modlist
  8. import subprocess
  9.  
  10.  
  11. LDAP_HOST = "ldap.corp.nosa.com"
  12. LDAP_DN = "ou=People,dc=nosa,dc=com"
  13. LDAP_USER = "cn=root,dc=nosa,dc=com"
  14. LDAP_PASS = ""
  15.  
  16.  
  17. class WDJLdap(object):
  18.  
  19. def __init__(self, ldap_host=LDAP_HOST, ldap_dn=LDAP_DN, ldap_user=LDAP_USER, ldap_pass=LDAP_PASS):
  20. self.ldapconn = ldap.initialize('ldap://%s' % ldap_host)
  21. self.ldapconn.simple_bind_s("cn=root,dc=nosa,dc=com", ldap_pass)
  22.  
  23. def list_user(self):
  24. retval = self.ldapconn.search_s(
  25. 'ou=People,dc=nosa,dc=com', ldap.SCOPE_SUBTREE, '(uid=*)', ['*'])
  26. return retval
  27.  
  28. def search_user(self, uid):
  29. retval = self.ldapconn.search_s(
  30. 'ou=People,dc=nosa,dc=com', ldap.SCOPE_SUBTREE, '(uid=%s)' % uid, ['*'])
  31. return retval
  32.  
  33. def get_email(self, uid):
  34. retval = self.ldapconn.search_s(
  35. 'ou=People,dc=nosa,dc=com', ldap.SCOPE_SUBTREE, '(uid=%s)' % uid, ['mail'])
  36. return retval[0][1]['mail'][0]
  37.  
  38. def list_staff_user(self):
  39. retval = self.ldapconn.search_s(
  40. 'ou=Group,dc=nosa,dc=com', ldap.SCOPE_SUBTREE, '(cn=staff)', ['*'])
  41. return retval
  42.  
  43. def add_user(self, firstname, lastname, email, password):
  44. name = firstname + " " + lastname
  45. uid = email[:-14]
  46. shatmp = hashlib.sha1()
  47. shatmp.update(password)
  48. shatmp1 = shatmp.hexdigest()
  49. shatmp2 = binascii.unhexlify(shatmp1)
  50. shatmp3 = base64.encodestring(shatmp2)
  51. shatmp4 = shatmp3.strip()
  52. shapasswd = "{SHA}" + shatmp4
  53. info = {'cn': [name,],
  54. 'displayName': [name,],
  55. 'givenName': [firstname,],
  56. 'sn': [lastname,],
  57. 'mail': [email,],
  58. 'uid': [uid,],
  59. 'userPassword': [shapasswd,],
  60. 'objectclass': ['top', 'person', 'organizationalPerson', 'inetOrgPerson']
  61. }
  62. dn = 'uid=%s,ou=People,dc=nosa,dc=com' % uid
  63. attr = [(k, v) for (k, v) in info.items()]
  64. self.ldapconn.add_s(dn, attr)
  65. group_dn = "cn=staff,ou=Group,dc=nosa,dc=com"
  66. mod_attr = [ (ldap.MOD_ADD,'member',dn )]
  67. self.ldapconn.modify_s(group_dn, mod_attr)
  68.  
  69. def change_passwd(self, username, newpass):
  70. dn = 'uid=%s,ou=People,dc=nosa,dc=com' % username
  71. shatmp = hashlib.sha1()
  72. shatmp.update(newpass)
  73. shatmp1 = shatmp.hexdigest()
  74. shatmp2 = binascii.unhexlify(shatmp1)
  75. shatmp3 = base64.encodestring(shatmp2)
  76. shatmp4 = shatmp3.strip()
  77. shapasswd = "{SHA}" + shatmp4
  78. mod_attr = [ (ldap.MOD_REPLACE,'userPassword',shapasswd )]
  79. self.ldapconn.modify_s(dn,mod_attr)
  80.  
  81. def delete_user_in_group(self, uid):
  82. dn = 'uid=%s,ou=People,dc=nosa,dc=com' % uid
  83. group_dn = "cn=staff,ou=Group,dc=nosa,dc=com"
  84. mod_attr = [ (ldap.MOD_DELETE,'member',dn )]
  85. self.ldapconn.modify_s(group_dn, mod_attr)
  86.  
  87. def del_user(self, uid):
  88. dn = 'uid=%s,ou=People,dc=nosa,dc=com' % uid
  89. email = self.get_email(uid)
  90. self.ldapconn.delete_s(dn)
  91. self.delete_user_in_group(uid)
  92. cmd = "bash -x delete_gerrit_user.sh %s" % email
  93. ret = subprocess.call(cmd,shell=True)
  94. if ret != 0:
  95. return False
  96.  
  97. def has_user(self, uid):
  98. retval = self.ldapconn.search_s(
  99. 'ou=People,dc=nosa,dc=com', ldap.SCOPE_SUBTREE, '(uid=%s)' % uid, ['*'])
  100. print retval
  101. if len(retval):
  102. return True
  103. else:
  104. return False
  105.  
  106. def unbind_link(self):
  107. """release the connection to ldap server"""
  108. self.ldapconn.unbind()
  109.  
  110.  
  111. def main():
  112. demo = WDJLdap()
  113. print demo.search_user("username")
  114.  
  115.  
  116. if __name__ == '__main__':
  117. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement