Guest User

Untitled

a guest
Oct 15th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. '''
  2. LDAP example for VSB-TUO
  3. You need python-ldap package: pip install python-ldap (tested on version 3.1.0)
  4. '''
  5.  
  6. import getpass
  7. import ldap
  8.  
  9. if __name__ == '__main__':
  10. ldap.set_option(ldap.OPT_REFERRALS,0)
  11. ldap.protocol_version = 3
  12.  
  13. ldap_server='ldaps://ldap.vsb.cz'
  14. username = input('username: ')
  15. password= getpass.getpass('password: ')
  16.  
  17. # VSB specific user context
  18. trailing_context = username[-1]
  19.  
  20. # the following is the user_dn format provided by the ldap server
  21. user_dn = 'cn=' + username
  22. # adjust this to your base dn for searching
  23. base_dn = 'ou=USERS,o=VSB'
  24.  
  25. connect = ldap.initialize(ldap_server)
  26. search_filter = 'cn=' + username
  27.  
  28. listing = connect.search_s(base_dn, ldap.SCOPE_SUBTREE, user_dn, [])
  29.  
  30. if len(listing) == 0:
  31. print('User not found')
  32. else:
  33. print(listing[0][0])
  34.  
  35. #connect.set_option(ldap.OPT_REFERRALS, 0)
  36. try:
  37. #if authentication successful, get the full user data
  38. connect.simple_bind_s('cn={},ou={},ou=USERS,o=VSB'.format(username, trailing_context), password)
  39. result = connect.search_s(base_dn, ldap.SCOPE_SUBTREE, search_filter)
  40.  
  41. # return all user data results
  42. connect.unbind_s()
  43. print(result)
  44. except ldap.LDAPError as e:
  45. print(e)
  46. connect.unbind_s()
  47. print('authentication error')
Add Comment
Please, Sign In to add comment