Guest User

Untitled

a guest
May 4th, 2018
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. '''
  3. Read password from ldap and add or change it in opensmtp passwd file
  4. '''
  5. import ldap
  6.  
  7. OPENSMTPD_PASSWD_FILE = "/etc/mail/passwd"
  8. SERVER_LDAP = "ldap.reve.space"
  9. DN = "ou=utilisateurs,dc=reve,dc=space"
  10.  
  11.  
  12. def read_pass():
  13. connection = ldap.open(SERVER_LDAP)
  14. connection.protocol_version = ldap.VERSION3
  15. searchScope = ldap.SCOPE_SUBTREE
  16. retrieveAttributes = ['userPassword']
  17. searchFilter = "(objectClass=inetOrgPerson)"
  18. ldap_result_id = connection.search(
  19. DN,
  20. searchScope,
  21. searchFilter,
  22. retrieveAttributes)
  23. result_set = list()
  24. while True:
  25. result_type, result_data = connection.result(ldap_result_id, 0)
  26. if (result_data == []):
  27. break
  28. else:
  29. if result_type == ldap.RES_SEARCH_ENTRY:
  30. result_set.append(result_data)
  31. openldap_pass = dict()
  32. for result in result_set:
  33. user = result[0][1]['uid'][0]
  34. password = result[0][1]['userPassword'][0]
  35. openldap_pass.update({user: password})
  36. return(openldap_pass)
  37.  
  38.  
  39. def write_pass(pass_list):
  40. opensmtp_pass = dict()
  41. with open(OPENSMTPD_PASSWD_FILE, 'r') as file:
  42. for line in file.readlines():
  43. user = line.split(':')[0]
  44. password = line.split(':')[1]
  45. opensmtp_pass.update({user: password})
  46. opensmtp_pass.update(pass_list)
  47. return(opensmtp_pass)
  48.  
  49.  
  50. if __name__ == '__main__':
  51. password_dico = write_pass(read_pass())
  52. with open(OPENSMTPD_PASSWD_FILE, 'w') as file:
  53. for key in password_dico.keys():
  54. file.write(key + ":" + password_dico[key] + "::::::\n")
Add Comment
Please, Sign In to add comment