Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. import traceback
  2. import ldap
  3. import datetime
  4.  
  5.  
  6. def get_membership_from_answer (obj) :
  7. if list == type(obj):
  8. for i in obj:
  9. ans = get_membership_from_answer(i)
  10. if None != ans:
  11. return ans;
  12. elif tuple == type(obj):
  13. for i in obj:
  14. ans = get_membership_from_answer(i)
  15. if None != ans:
  16. return ans;
  17. elif dict == type(obj):
  18. array_of_groups = obj.get('memberOf')
  19. ans = list()
  20. for grp in array_of_groups:
  21. start_name = grp.find ("CN=")
  22. end_name = grp.find(",")
  23. ans.append(grp[start_name+3: end_name])
  24. return ans
  25. else:
  26. return None
  27. return None
  28.  
  29. #testing
  30. try:
  31. l = ldap.open("192.168.1.1")
  32. l.set_option(ldap.OPT_REFERRALS, 0)
  33. l.protocol_version = ldap.VERSION3
  34.  
  35. username = "ca4@test.local.domain"
  36. password = "pass_Ca"
  37.  
  38. dn_recs = username.split('@')[1].split('.')
  39. username_bare = username.split('@')[0]
  40. for i in range(len(dn_recs)):
  41. dn_recs[i] = 'dc=%s' % dn_recs[i]
  42. dn_recs = ','.join(dn_recs)
  43.  
  44. # Any errors will throw an ldap.LDAPError exception
  45. # or related exception so you can ignore the result
  46. l.simple_bind_s(username, password)
  47.  
  48. #get groups - here we are falling. what should be here?
  49. f_filterStr = "(&(objectClass=user)(cn=%s))" % username_bare
  50. print "Filter == ", f_filterStr
  51. results = l.search_s(dn_recs, ldap.SCOPE_SUBTREE, f_filterStr)
  52. print "results == ", results
  53. print get_membership_from_answer (results)
  54.  
  55. except ldap.LDAPError, e:
  56. print "No login :-("
  57. print traceback.format_exc()
  58.  
  59. $$>test_ldap_grps_1.py
  60. Filter == (&(objectClass=user)(cn=ca4))
  61.  
  62. results == [('CN=ca4,DC=test,DC=local,DC=domain', {'primaryGroupID': ['513'], 'logonCount': ['0'], 'cn': ['ca4'], 'countryCode': ['0'], 'dSCorePropagationData': ['16010101000000.0Z'], 'objectClass': ['top', 'person', 'organizationalPerson', 'user'], 'userPrincipalName': ['ca4@test.local.domain'], 'lastLogonTimestamp': ['130606496321699064'], 'instanceType': ['4'], 'distinguishedName': ['CN=ca4,DC=test,DC=local,DC=domain'], 'sAMAccountType': ['805306368'], 'objectSid': ['x01x05x00x00x00x00x00x05x15x00x00x00xc4Jyx08Kxc94x8ex8fx1excdx96Wx04x00x00'], 'whenCreated': ['20140601213859.0Z'], 'uSNCreated': ['12788'], 'badPasswordTime': ['130606517298027248'], 'pwdLastSet': ['130591819072140892'], 'sAMAccountName': ['ca4'], 'objectCategory': ['CN=Person,CN=Schema,CN=Configuration,DC=test,DC=local,DC=domain'], 'objectGUID': ['xb4xb8.x8ahx00x84Mx84xe4xd5xa3xe2)x84x7f'], 'whenChanged': ['20141116221352.0Z'], 'badPwdCount': ['0'], 'accountExpires': ['9223372036854775807'], 'displayName': ['ca4'], 'name': ['ca4'], 'memberOf': ['CN=ca_manager,DC=test,DC=local,DC=domain', 'CN=ca_tech,DC=test,DC=local,DC=domain', 'CN=ca_change,DC=test,DC=local,DC=domain', 'CN=ca,DC=test,DC=local,DC=domain'], 'codePage': ['0'], 'userAccountControl': ['66048'], 'lastLogon': ['130606528359838513'], 'uSNChanged': ['41278'], 'givenName': ['ca4'], 'lastLogoff': ['0']}), (None, ['ldap://ForestDnsZones.test.local.domain/DC=ForestDnsZones,DC=test,DC=local,DC=domain']), (None, ['ldap://DomainDnsZones.test.local.domain/DC=DomainDnsZones,DC=test,DC=local,DC=domain']), (None, ['ldap://test.local.domain/CN=Configuration,DC=test,DC=local,DC=domain'])]
  63.  
  64. ['ca_manager', 'ca_tech', 'ca_change', 'ca']
  65.  
  66. $$>test_ldap_grps_1.py
  67. Filter == (&(objectClass=user)(cn=ca4))
  68.  
  69. results == [('CN=ca4,DC=test,DC=local,DC=domain', None)]
  70.  
  71. None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement