Guest User

Untitled

a guest
May 6th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 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_ldap_pass():
  13. connection = ldap.open(SERVER_LDAP)
  14. connection.protocol_version = ldap.VERSION3
  15. searchScope = ldap.SCOPE_SUBTREE
  16. retrieveAttributes = ['userPassword','uid']
  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].split('}').[1]
  35. openldap_pass.update({user: password})
  36. return(openldap_pass)
  37.  
  38.  
  39. def read_opensmtpd_pass(pass_list):
  40. opensmtp_pass = dict()
  41. user_dico = dict ()
  42. with open(OPENSMTPD_PASSWD_FILE, 'r') as file:
  43. for line in file.readlines():
  44. user = line.split(':')[0]
  45. user_dico['password'] = line.split(':')[1]
  46. user_dico['options'] = line.split(':')[-1]
  47.  
  48. opensmtp_pass.update({user: user_dico})
  49. return(opensmtp_pass)
  50.  
  51.  
  52. if __name__ == '__main__':
  53. password_dico = read_opensmtpd_pass(read_ldap_pass())
  54. with open(OPENSMTPD_PASSWD_FILE, 'w') as file:
  55. for key in password_dico.keys():
  56. file.write(key + ":" + password_dico[key]['password'] + "::::::" + password_dico[key]['options'] +"\n" )
Add Comment
Please, Sign In to add comment