Advertisement
Guest User

Untitled

a guest
Nov 6th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import ldap
  4. import ldap.modlist as modlist
  5. import sys
  6. import getpass
  7.  
  8. def login():
  9.  
  10. pprompt = lambda: (getpass.getpass('User password: '), getpass.getpass('Confirm user password: '))
  11.  
  12. p1, p2 = pprompt()
  13. while p1 != p2:
  14. print('Passwords do not match. Try again')
  15. p1, p2 = pprompt()
  16.  
  17. return p1
  18.  
  19. AD_LDAP_URL='ldaps://<ldapserver>:636'
  20. ADMIN_USER='<bind admin user>'
  21. # User must be authorized to create accounts, naturally.
  22. BASE_DN='<base dn - where the user will be created>'
  23. REALM='<Kerberos REALM - ALL CAPITALS'
  24.  
  25. if len(sys.argv) == 1:
  26. print("Sintaxe: ldapuser.py <username> <host>")
  27. print("\t\t\t <username>: (required) username to be created")
  28. print("\t\t\t <host>: (optional) host to be used on service principal")
  29.  
  30. else:
  31. ADMIN_PASSWORD=getpass.getpass('Enter Bind Password: ')
  32. username=sys.argv[1]
  33. if len(sys.argv) > 2:
  34. host='/'+str(sys.argv[2])
  35. service=str(username)+str(host)+str(REALM)
  36. username=str(username)+str(host)
  37. else:
  38. service=''
  39. password=login()
  40. # The value of password still needs to adhere to the domain's password policy.
  41. unicode_pass = unicode('\"' + password + '\"', 'iso-8859-1')
  42. password_value = unicode_pass.encode('utf-16-le')
  43. principal=str(username)+str(REALM)
  44. l = ldap.initialize(AD_LDAP_URL)
  45. l.simple_bind_s(ADMIN_USER, ADMIN_PASSWORD)
  46.  
  47. dn='CN='+str(username)+','+str(BASE_DN)
  48. attrs = {}
  49.  
  50. attrs['objectclass'] = ['top','person','organizationalPerson','user']
  51. attrs['cn'] = str(username)
  52. attrs['unicodePwd'] = str(password_value)
  53. attrs['userPrincipalName'] = str(principal)
  54. attrs['userAccountControl'] = str(66048)
  55. if service != '':
  56. attrs['servicePrincipalName'] = str(service)
  57. else:
  58. attrs['samaccountname'] = str(username)
  59. attrs['lockoutTime'] = str(0)
  60. attrs['accountExpires'] = str(0)
  61.  
  62. ldif = modlist.addModlist(attrs)
  63. l.add_s(dn,ldif)
  64. print "User "+str(username)+" added\n Principal="+str(principal)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement