| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2020, Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
module: ipa_pwpolicy |
| 10 |
author: Adralioh (@adralioh) |
| 11 |
short_description: Manage FreeIPA password policies |
| 12 |
description: |
| 13 |
- Add, modify, or delete a password policy using the IPA API. |
| 14 |
version_added: 2.0.0 |
| 15 |
attributes: |
| 16 |
check_mode: |
| 17 |
support: full |
| 18 |
diff_mode: |
| 19 |
support: none |
| 20 |
options: |
| 21 |
group: |
| 22 |
description: |
| 23 |
- Name of the group that the policy applies to. |
| 24 |
- If omitted, the global policy is used. |
| 25 |
aliases: ["name"] |
| 26 |
type: str |
| 27 |
state: |
| 28 |
description: State to ensure. |
| 29 |
default: "present" |
| 30 |
choices: ["absent", "present"] |
| 31 |
type: str |
| 32 |
maxpwdlife: |
| 33 |
description: Maximum password lifetime (in days). |
| 34 |
type: str |
| 35 |
minpwdlife: |
| 36 |
description: Minimum password lifetime (in hours). |
| 37 |
type: str |
| 38 |
historylength: |
| 39 |
description: |
| 40 |
- Number of previous passwords that are remembered. |
| 41 |
- Users cannot reuse remembered passwords. |
| 42 |
type: str |
| 43 |
minclasses: |
| 44 |
description: Minimum number of character classes. |
| 45 |
type: str |
| 46 |
minlength: |
| 47 |
description: Minimum password length. |
| 48 |
type: str |
| 49 |
priority: |
| 50 |
description: |
| 51 |
- Priority of the policy. |
| 52 |
- High number means lower priority. |
| 53 |
- Required when C(cn) is not the global policy. |
| 54 |
type: str |
| 55 |
maxfailcount: |
| 56 |
description: Maximum number of consecutive failures before lockout. |
| 57 |
type: str |
| 58 |
failinterval: |
| 59 |
description: Period (in seconds) after which the number of failed login attempts is reset. |
| 60 |
type: str |
| 61 |
lockouttime: |
| 62 |
description: Period (in seconds) for which users are locked out. |
| 63 |
type: str |
| 64 |
gracelimit: |
| 65 |
description: Maximum number of LDAP logins after password expiration. |
| 66 |
type: int |
| 67 |
version_added: 8.2.0 |
| 68 |
maxrepeat: |
| 69 |
description: Maximum number of allowed same consecutive characters in the new password. |
| 70 |
type: int |
| 71 |
version_added: 8.2.0 |
| 72 |
maxsequence: |
| 73 |
description: Maximum length of monotonic character sequences in the new password. An example of a monotonic sequence of |
| 74 |
length 5 is V(12345). |
| 75 |
type: int |
| 76 |
version_added: 8.2.0 |
| 77 |
dictcheck: |
| 78 |
description: Check whether the password (with possible modifications) matches a word in a dictionary (using cracklib). |
| 79 |
type: bool |
| 80 |
version_added: 8.2.0 |
| 81 |
usercheck: |
| 82 |
description: Check whether the password (with possible modifications) contains the user name in some form (if the name |
| 83 |
has > 3 characters). |
| 84 |
type: bool |
| 85 |
version_added: 8.2.0 |
| 86 |
extends_documentation_fragment: |
| 87 |
- community.general._ipa.documentation |
| 88 |
- community.general._ipa.connection_notes |
| 89 |
- community.general._attributes |
| 90 |
""" |
| 91 |
|
| 92 |
EXAMPLES = r""" |
| 93 |
- name: Modify the global password policy |
| 94 |
community.general.ipa_pwpolicy: |
| 95 |
maxpwdlife: '90' |
| 96 |
minpwdlife: '1' |
| 97 |
historylength: '8' |
| 98 |
minclasses: '3' |
| 99 |
minlength: '16' |
| 100 |
maxfailcount: '6' |
| 101 |
failinterval: '60' |
| 102 |
lockouttime: '600' |
| 103 |
ipa_host: ipa.example.com |
| 104 |
ipa_user: admin |
| 105 |
ipa_pass: topsecret |
| 106 |
|
| 107 |
- name: Ensure the password policy for the group admins is present |
| 108 |
community.general.ipa_pwpolicy: |
| 109 |
group: admins |
| 110 |
state: present |
| 111 |
maxpwdlife: '60' |
| 112 |
minpwdlife: '24' |
| 113 |
historylength: '16' |
| 114 |
minclasses: '4' |
| 115 |
priority: '10' |
| 116 |
minlength: '6' |
| 117 |
maxfailcount: '4' |
| 118 |
failinterval: '600' |
| 119 |
lockouttime: '1200' |
| 120 |
gracelimit: 3 |
| 121 |
maxrepeat: 3 |
| 122 |
maxsequence: 3 |
| 123 |
dictcheck: true |
| 124 |
usercheck: true |
| 125 |
ipa_host: ipa.example.com |
| 126 |
ipa_user: admin |
| 127 |
ipa_pass: topsecret |
| 128 |
|
| 129 |
- name: Ensure that the group sysops does not have a unique password policy |
| 130 |
community.general.ipa_pwpolicy: |
| 131 |
group: sysops |
| 132 |
state: absent |
| 133 |
ipa_host: ipa.example.com |
| 134 |
ipa_user: admin |
| 135 |
ipa_pass: topsecret |
| 136 |
""" |
| 137 |
|
| 138 |
RETURN = r""" |
| 139 |
pwpolicy: |
| 140 |
description: Password policy as returned by IPA API. |
| 141 |
returned: always |
| 142 |
type: dict |
| 143 |
sample: |
| 144 |
cn: ['admins'] |
| 145 |
cospriority: ['10'] |
| 146 |
dn: 'cn=admins,cn=EXAMPLE.COM,cn=kerberos,dc=example,dc=com' |
| 147 |
krbmaxpwdlife: ['60'] |
| 148 |
krbminpwdlife: ['24'] |
| 149 |
krbpwdfailurecountinterval: ['600'] |
| 150 |
krbpwdhistorylength: ['16'] |
| 151 |
krbpwdlockoutduration: ['1200'] |
| 152 |
krbpwdmaxfailure: ['4'] |
| 153 |
krbpwdmindiffchars: ['4'] |
| 154 |
objectclass: ['top', 'nscontainer', 'krbpwdpolicy'] |
| 155 |
""" |
| 156 |
|
| 157 |
import traceback |
| 158 |
|
| 159 |
from ansible.module_utils.basic import AnsibleModule |
| 160 |
|
| 161 |
from ansible_collections.community.general.plugins.module_utils._ipa import IPAClient, ipa_argument_spec |
| 162 |
|
| 163 |
|
| 164 |
class PwPolicyIPAClient(IPAClient): |
| 165 |
"""The global policy will be selected when `name` is `None`""" |
| 166 |
|
| 167 |
def __init__(self, module, host, port, protocol): |
| 168 |
super().__init__(module, host, port, protocol) |
| 169 |
|
| 170 |
def pwpolicy_find(self, name): |
| 171 |
if name is None: |
| 172 |
# Manually set the cn to the global policy because pwpolicy_find will return a random |
| 173 |
# different policy if cn is `None` |
| 174 |
name = "global_policy" |
| 175 |
return self._post_json(method="pwpolicy_find", name=None, item={"all": True, "cn": name}) |
| 176 |
|
| 177 |
def pwpolicy_add(self, name, item): |
| 178 |
return self._post_json(method="pwpolicy_add", name=name, item=item) |
| 179 |
|
| 180 |
def pwpolicy_mod(self, name, item): |
| 181 |
return self._post_json(method="pwpolicy_mod", name=name, item=item) |
| 182 |
|
| 183 |
def pwpolicy_del(self, name): |
| 184 |
return self._post_json(method="pwpolicy_del", name=name) |
| 185 |
|
| 186 |
|
| 187 |
def get_pwpolicy_dict( |
| 188 |
maxpwdlife=None, |
| 189 |
minpwdlife=None, |
| 190 |
historylength=None, |
| 191 |
minclasses=None, |
| 192 |
minlength=None, |
| 193 |
priority=None, |
| 194 |
maxfailcount=None, |
| 195 |
failinterval=None, |
| 196 |
lockouttime=None, |
| 197 |
gracelimit=None, |
| 198 |
maxrepeat=None, |
| 199 |
maxsequence=None, |
| 200 |
dictcheck=None, |
| 201 |
usercheck=None, |
| 202 |
): |
| 203 |
pwpolicy = {} |
| 204 |
pwpolicy_options = { |
| 205 |
"krbmaxpwdlife": maxpwdlife, |
| 206 |
"krbminpwdlife": minpwdlife, |
| 207 |
"krbpwdhistorylength": historylength, |
| 208 |
"krbpwdmindiffchars": minclasses, |
| 209 |
"krbpwdminlength": minlength, |
| 210 |
"cospriority": priority, |
| 211 |
"krbpwdmaxfailure": maxfailcount, |
| 212 |
"krbpwdfailurecountinterval": failinterval, |
| 213 |
"krbpwdlockoutduration": lockouttime, |
| 214 |
"passwordgracelimit": gracelimit, |
| 215 |
"ipapwdmaxrepeat": maxrepeat, |
| 216 |
"ipapwdmaxsequence": maxsequence, |
| 217 |
} |
| 218 |
|
| 219 |
pwpolicy_boolean_options = { |
| 220 |
"ipapwddictcheck": dictcheck, |
| 221 |
"ipapwdusercheck": usercheck, |
| 222 |
} |
| 223 |
|
| 224 |
for option, value in pwpolicy_options.items(): |
| 225 |
if value is not None: |
| 226 |
pwpolicy[option] = str(value) |
| 227 |
|
| 228 |
for option, value in pwpolicy_boolean_options.items(): |
| 229 |
if value is not None: |
| 230 |
pwpolicy[option] = bool(value) |
| 231 |
|
| 232 |
return pwpolicy |
| 233 |
|
| 234 |
|
| 235 |
def get_pwpolicy_diff(client, ipa_pwpolicy, module_pwpolicy): |
| 236 |
return client.get_diff(ipa_data=ipa_pwpolicy, module_data=module_pwpolicy) |
| 237 |
|
| 238 |
|
| 239 |
def ensure(module, client): |
| 240 |
state = module.params["state"] |
| 241 |
name = module.params["group"] |
| 242 |
|
| 243 |
module_pwpolicy = get_pwpolicy_dict( |
| 244 |
maxpwdlife=module.params.get("maxpwdlife"), |
| 245 |
minpwdlife=module.params.get("minpwdlife"), |
| 246 |
historylength=module.params.get("historylength"), |
| 247 |
minclasses=module.params.get("minclasses"), |
| 248 |
minlength=module.params.get("minlength"), |
| 249 |
priority=module.params.get("priority"), |
| 250 |
maxfailcount=module.params.get("maxfailcount"), |
| 251 |
failinterval=module.params.get("failinterval"), |
| 252 |
lockouttime=module.params.get("lockouttime"), |
| 253 |
gracelimit=module.params.get("gracelimit"), |
| 254 |
maxrepeat=module.params.get("maxrepeat"), |
| 255 |
maxsequence=module.params.get("maxsequence"), |
| 256 |
dictcheck=module.params.get("dictcheck"), |
| 257 |
usercheck=module.params.get("usercheck"), |
| 258 |
) |
| 259 |
|
| 260 |
ipa_pwpolicy = client.pwpolicy_find(name=name) |
| 261 |
|
| 262 |
changed = False |
| 263 |
if state == "present": |
| 264 |
if not ipa_pwpolicy: |
| 265 |
changed = True |
| 266 |
if not module.check_mode: |
| 267 |
ipa_pwpolicy = client.pwpolicy_add(name=name, item=module_pwpolicy) |
| 268 |
else: |
| 269 |
diff = get_pwpolicy_diff(client, ipa_pwpolicy, module_pwpolicy) |
| 270 |
if len(diff) > 0: |
| 271 |
changed = True |
| 272 |
if not module.check_mode: |
| 273 |
ipa_pwpolicy = client.pwpolicy_mod(name=name, item=module_pwpolicy) |
| 274 |
else: |
| 275 |
if ipa_pwpolicy: |
| 276 |
changed = True |
| 277 |
if not module.check_mode: |
| 278 |
client.pwpolicy_del(name=name) |
| 279 |
|
| 280 |
return changed, ipa_pwpolicy |
| 281 |
|
| 282 |
|
| 283 |
def main(): |
| 284 |
argument_spec = ipa_argument_spec() |
| 285 |
argument_spec.update( |
| 286 |
group=dict(type="str", aliases=["name"]), |
| 287 |
state=dict(type="str", default="present", choices=["present", "absent"]), |
| 288 |
maxpwdlife=dict(type="str"), |
| 289 |
minpwdlife=dict(type="str"), |
| 290 |
historylength=dict(type="str"), |
| 291 |
minclasses=dict(type="str"), |
| 292 |
minlength=dict(type="str"), |
| 293 |
priority=dict(type="str"), |
| 294 |
maxfailcount=dict(type="str"), |
| 295 |
failinterval=dict(type="str"), |
| 296 |
lockouttime=dict(type="str"), |
| 297 |
gracelimit=dict(type="int"), |
| 298 |
maxrepeat=dict(type="int"), |
| 299 |
maxsequence=dict(type="int"), |
| 300 |
dictcheck=dict(type="bool"), |
| 301 |
usercheck=dict(type="bool"), |
| 302 |
) |
| 303 |
|
| 304 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 305 |
|
| 306 |
client = PwPolicyIPAClient( |
| 307 |
module=module, |
| 308 |
host=module.params["ipa_host"], |
| 309 |
port=module.params["ipa_port"], |
| 310 |
protocol=module.params["ipa_prot"], |
| 311 |
) |
| 312 |
|
| 313 |
try: |
| 314 |
client.login(username=module.params["ipa_user"], password=module.params["ipa_pass"]) |
| 315 |
changed, pwpolicy = ensure(module, client) |
| 316 |
except Exception as e: |
| 317 |
module.fail_json(msg=f"{e}", exception=traceback.format_exc()) |
| 318 |
|
| 319 |
module.exit_json(changed=changed, pwpolicy=pwpolicy) |
| 320 |
|
| 321 |
|
| 322 |
if __name__ == "__main__": |
| 323 |
main() |