Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import ldap
  4. from getpass import getpass
  5.  
  6. # Set constants
  7. HOST = '<HOST>' # eg. ldaps://my-ldap-server.com
  8. BASE_DN = '<BASE_DN>' # eg. 'dc=example,dc=com'
  9.  
  10. # Set LDAP options
  11. ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
  12.  
  13. # Get username and password
  14. username = input("Enter your Linux username: ")
  15. password = getpass("Enter your Linux password: ")
  16.  
  17. # Set bind DN based on base DN
  18. bind_DN = f'uid={username},{BASE_DN}'
  19.  
  20. # Initialize LDAP
  21. l = ldap.initialize(HOST)
  22.  
  23. # Try to bind to the given username and password
  24. try:
  25. l.simple_bind_s(bind_DN, password)
  26. except ldap.NO_SUCH_OBJECT:
  27. print(f'Linux username {username} not found...')
  28. exit(1)
  29. except ldap.UNWILLING_TO_PERFORM as e:
  30. if e.args[0]['info'] == 'Unauthenticated binds are not allowed':
  31. print('A password is required...')
  32. else:
  33. print('Something went wrong, please try again...')
  34. exit(1)
  35. except ldap.INVALID_CREDENTIALS:
  36. print('Password incorrect...')
  37. exit(1)
  38.  
  39. # Perform a search for the given username's info
  40. result = l.search_s(BASE_DN, ldap.SCOPE_SUBTREE, f'(uid={username})')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement