Guest User

Untitled

a guest
Jun 24th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # getadsmtp.py
  4. # Written by Maximilian Thoma 2016
  5. # Version 1.0
  6. # The script is an translation from the orginal perl script getadsmtp.pl
  7.  
  8. # This script will pull all users' SMTP addresses from your Active Directory
  9. # (including primary and secondary email addresses) and list them in the
  10. # format "user@example.com OK" which Postfix uses with relay_recipient_maps.
  11. # Be sure to double-check the path to python above.
  12.  
  13. # This requires python-ldap to be installed. To install python-ldap on debian based systems,
  14. # at a shell type "apt-get install python-ldap" or "sudo apt-get install python-ldap"
  15.  
  16. import os, sys, ldap
  17.  
  18. # Enter the path/file for the output
  19. valid_addresses = "/etc/postfix/example_recipients"
  20.  
  21. # Enter the FQDN of your Active Directory domain controllers below
  22. dc1="dc01.example.com"
  23. dc2="dc02.example.com"
  24.  
  25. # Enter the LDAP container for your userbase.
  26. # The syntax is CN=Users,dc=example,dc=com
  27. # This can be found by installing the Windows 2000 Support Tools
  28. # then running ADSI Edit.
  29. # In ADSI Edit, expand the "Domain NC [domaincontroller1.example.com]" &
  30. # you will see, for example, DC=example,DC=com (this is your base).
  31. # The Users Container will be specified in the right pane as
  32. # CN=Users depending on your schema (this is your container).
  33. # You can double-check this by clicking "Properties" of your user
  34. # folder in ADSI Edit and examining the "Path" value, such as:
  35. # LDAP://domaincontroller1.example.com/CN=Users,DC=example,DC=com
  36. # which would be hqbase="cn=Users,dc=example,dc=com"
  37. # Note: You can also use just hqbase="dc=example,dc=com"
  38. hqbase="cn=Users,dc=example,dc=com"
  39.  
  40. # Enter the username & password for a valid user in your Active Directory
  41. # with username in the form cn=username,cn=Users,dc=example,dc=com
  42. # Make sure the user's password does not expire. Note that this user
  43. # does not require any special privileges.
  44. # You can double-check this by clicking "Properties" of your user in
  45. # ADSI Edit and examining the "Path" value, such as:
  46. # LDAP://domaincontroller1.example.com/CN=user,CN=Users,DC=example,DC=com
  47. # which would be $user="cn=user,cn=Users,dc=example,dc=com"
  48. # Note: You can also use the UPN login: "user@example.com"
  49. user="cn=user,cn=Users,dc=example,dc=com"
  50. passwd="password"
  51.  
  52. try:
  53. l = ldap.initialize("ldap://%s" %(dc1))
  54. l.set_option(ldap.OPT_REFERRALS, 0)
  55. l.protocol_version = 3
  56. l.simple_bind_s(user, passwd)
  57.  
  58. except ldap.LDAPError, e:
  59. try:
  60. l = ldap.initialize("ldap://%s" %(dc2))
  61. l.set_option(ldap.OPT_REFERRALS, 0)
  62. l.protocol_version = 3
  63. l.simple_bind_s(user, passwd)
  64.  
  65. except ldap.LDAPError, e:
  66. print "Error connecting to specified domain controllers\n"
  67. sys.exit()
  68.  
  69. # Play around with this to grab objects such as Contacts, Public Folders, etc.
  70. # A minimal filter for just users with email would be:
  71. # filter = "(&(sAMAccountName=*)(mail=*))"
  72. filter = "(& (mailnickname=*) (| (&(objectCategory=person)(objectClass=user)(!(homeMDB=*))(!(msExchHomeServerName=*)))(&(objectCategory=person)(objectClass=user)(|(homeMDB=*)(msExchHomeServerName=*)))(&(objectCategory=person)(objectClass=contact))(objectCategory=group)(objectCategory=publicFolder)(objectClass=msExchDynamicDistributionList) ))"
  73.  
  74. attrs = ["proxyAddresses"]
  75. scope = ldap.SCOPE_SUBTREE
  76.  
  77. r = l.search(hqbase, scope, filter, attrs)
  78. type,a = l.result(r)
  79. result_set = []
  80.  
  81. for x in a:
  82. name,attrs = x
  83. if hasattr(attrs, 'has_key') and attrs.has_key('proxyAddresses'):
  84. proxyAddresses = attrs['proxyAddresses']
  85. for y in proxyAddresses:
  86. result_set.append("%s OK" %(y.replace("smtp:","").replace("SMTP:","")))
  87.  
  88. # Add additional restrictions, users, etc. to the output file below.
  89. #result_set.append("user@example.com OK")
  90. #result_set.append("user1@example.com 550 User unknown.")
  91. #result_set.append("bad.example.com 550 User does not exist.")
  92.  
  93.  
  94. #######################################################################
  95. # Build file ...
  96. output = file(valid_addresses,'w')
  97.  
  98. for line in result_set:
  99. output.write("%s\n" %(line))
  100.  
  101. output.close()
Add Comment
Please, Sign In to add comment