Debian Codegraph @

ansible_collections/community/general/plugins/modules/ldap_passwd.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2017-2018, Keller Fuchs <[email protected]>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: ldap_passwd
11 short_description: Set passwords in LDAP
12 description:
13 - Set a password for an LDAP entry. This module only asserts that a given password is valid for a given entry. To assert
14 the existence of an entry, see M(community.general.ldap_entry).
15 author:
16 - Keller Fuchs (@KellerFuchs)
17 requirements:
18 - python-ldap
19 attributes:
20 check_mode:
21 support: full
22 diff_mode:
23 support: none
24 options:
25 passwd:
26 description:
27 - The (plaintext) password to be set for O(dn).
28 type: str
29 extends_documentation_fragment:
30 - community.general._ldap.documentation
31 - community.general._attributes
32 """
33
34 EXAMPLES = r"""
35 - name: Set a password for the admin user
36 community.general.ldap_passwd:
37 dn: cn=admin,dc=example,dc=com
38 passwd: "{{ vault_secret }}"
39
40 - name: Setting passwords in bulk
41 community.general.ldap_passwd:
42 dn: "{{ item.key }}"
43 passwd: "{{ item.value }}"
44 with_dict:
45 alice: alice123123
46 bob: "|30b!"
47 admin: "{{ vault_secret }}"
48 """
49
50 RETURN = r"""
51 modlist:
52 description: List of modified parameters.
53 returned: success
54 type: list
55 sample:
56 - [2, "olcRootDN", ["cn=root,dc=example,dc=com"]]
57 """
58
59 import traceback
60
61 from ansible.module_utils.basic import AnsibleModule, missing_required_lib
62
63 from ansible_collections.community.general.plugins.module_utils._ldap import (
64 LdapGeneric,
65 gen_specs,
66 ldap_required_together,
67 )
68
69 LDAP_IMP_ERR = None
70 try:
71 import ldap
72
73 HAS_LDAP = True
74 except ImportError:
75 LDAP_IMP_ERR = traceback.format_exc()
76 HAS_LDAP = False
77
78
79 class LdapPasswd(LdapGeneric):
80 def __init__(self, module):
81 LdapGeneric.__init__(self, module)
82
83 # Shortcuts
84 self.passwd = self.module.params["passwd"]
85
86 def passwd_check(self):
87 try:
88 tmp_con = ldap.initialize(self.server_uri)
89 except ldap.LDAPError as e:
90 self.fail("Cannot initialize LDAP connection", e)
91
92 if self.start_tls:
93 try:
94 tmp_con.start_tls_s()
95 except ldap.LDAPError as e:
96 self.fail("Cannot start TLS.", e)
97
98 try:
99 tmp_con.simple_bind_s(self.dn, self.passwd)
100 except ldap.INVALID_CREDENTIALS:
101 return True
102 except ldap.LDAPError as e:
103 self.fail("Cannot bind to the server.", e)
104 else:
105 return False
106 finally:
107 tmp_con.unbind()
108
109 def passwd_set(self):
110 # Exit early if the password is already valid
111 if not self.passwd_check():
112 return False
113
114 # Change the password (or throw an exception)
115 try:
116 self.connection.passwd_s(self.dn, None, self.passwd)
117 except ldap.LDAPError as e:
118 self.fail("Unable to set password", e)
119
120 # Password successfully changed
121 return True
122
123
124 def main():
125 module = AnsibleModule(
126 argument_spec=gen_specs(passwd=dict(no_log=True)),
127 supports_check_mode=True,
128 required_together=ldap_required_together(),
129 )
130
131 if not HAS_LDAP:
132 module.fail_json(msg=missing_required_lib("python-ldap"), exception=LDAP_IMP_ERR)
133
134 ldap = LdapPasswd(module)
135
136 if module.check_mode:
137 module.exit_json(changed=ldap.passwd_check())
138
139 module.exit_json(changed=ldap.passwd_set())
140
141
142 if __name__ == "__main__":
143 main()