Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- '''
- Read password from ldap and add or change it in opensmtp passwd file
- '''
- import ldap
- OPENSMTPD_PASSWD_FILE = "/etc/mail/passwd"
- SERVER_LDAP = "ldap.reve.space"
- DN = "ou=utilisateurs,dc=reve,dc=space"
- def read_ldap_pass():
- connection = ldap.open(SERVER_LDAP)
- connection.protocol_version = ldap.VERSION3
- searchScope = ldap.SCOPE_SUBTREE
- retrieveAttributes = ['userPassword','uid']
- searchFilter = "(objectClass=inetOrgPerson)"
- ldap_result_id = connection.search(
- DN,
- searchScope,
- searchFilter,
- retrieveAttributes)
- result_set = list()
- while True:
- result_type, result_data = connection.result(ldap_result_id, 0)
- if (result_data == []):
- break
- else:
- if result_type == ldap.RES_SEARCH_ENTRY:
- result_set.append(result_data)
- openldap_pass = dict()
- for result in result_set:
- user = result[0][1]['uid'][0]
- password = result[0][1]['userPassword'][0].split('}').[1]
- openldap_pass.update({user: password})
- return(openldap_pass)
- def read_opensmtpd_pass(pass_list):
- opensmtp_pass = dict()
- user_dico = dict ()
- with open(OPENSMTPD_PASSWD_FILE, 'r') as file:
- for line in file.readlines():
- user = line.split(':')[0]
- user_dico['password'] = line.split(':')[1]
- user_dico['options'] = line.split(':')[-1]
- opensmtp_pass.update({user: user_dico})
- return(opensmtp_pass)
- if __name__ == '__main__':
- password_dico = read_opensmtpd_pass(read_ldap_pass())
- with open(OPENSMTPD_PASSWD_FILE, 'w') as file:
- for key in password_dico.keys():
- file.write(key + ":" + password_dico[key]['password'] + "::::::" + password_dico[key]['options'] +"\n" )
Add Comment
Please, Sign In to add comment