
    Vh*                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZ d dlmZmZ d dlmZ  G d	 d
e      Z	 	 	 ddZd Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona	  
module: ipa_pwpolicy
author: Adralioh (@adralioh)
short_description: Manage FreeIPA password policies
description:
  - Add, modify, or delete a password policy using the IPA API.
version_added: 2.0.0
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  group:
    description:
      - Name of the group that the policy applies to.
      - If omitted, the global policy is used.
    aliases: ["name"]
    type: str
  state:
    description: State to ensure.
    default: "present"
    choices: ["absent", "present"]
    type: str
  maxpwdlife:
    description: Maximum password lifetime (in days).
    type: str
  minpwdlife:
    description: Minimum password lifetime (in hours).
    type: str
  historylength:
    description:
      - Number of previous passwords that are remembered.
      - Users cannot reuse remembered passwords.
    type: str
  minclasses:
    description: Minimum number of character classes.
    type: str
  minlength:
    description: Minimum password length.
    type: str
  priority:
    description:
      - Priority of the policy.
      - High number means lower priority.
      - Required when C(cn) is not the global policy.
    type: str
  maxfailcount:
    description: Maximum number of consecutive failures before lockout.
    type: str
  failinterval:
    description: Period (in seconds) after which the number of failed login attempts is reset.
    type: str
  lockouttime:
    description: Period (in seconds) for which users are locked out.
    type: str
  gracelimit:
    description: Maximum number of LDAP logins after password expiration.
    type: int
    version_added: 8.2.0
  maxrepeat:
    description: Maximum number of allowed same consecutive characters in the new password.
    type: int
    version_added: 8.2.0
  maxsequence:
    description: Maximum length of monotonic character sequences in the new password. An example of a monotonic sequence of
      length 5 is V(12345).
    type: int
    version_added: 8.2.0
  dictcheck:
    description: Check whether the password (with possible modifications) matches a word in a dictionary (using cracklib).
    type: bool
    version_added: 8.2.0
  usercheck:
    description: Check whether the password (with possible modifications) contains the user name in some form (if the name
      has > 3 characters).
    type: bool
    version_added: 8.2.0
extends_documentation_fragment:
  - community.general.ipa.documentation
  - community.general.attributes
a  
- name: Modify the global password policy
  community.general.ipa_pwpolicy:
    maxpwdlife: '90'
    minpwdlife: '1'
    historylength: '8'
    minclasses: '3'
    minlength: '16'
    maxfailcount: '6'
    failinterval: '60'
    lockouttime: '600'
    ipa_host: ipa.example.com
    ipa_user: admin
    ipa_pass: topsecret

- name: Ensure the password policy for the group admins is present
  community.general.ipa_pwpolicy:
    group: admins
    state: present
    maxpwdlife: '60'
    minpwdlife: '24'
    historylength: '16'
    minclasses: '4'
    priority: '10'
    minlength: '6'
    maxfailcount: '4'
    failinterval: '600'
    lockouttime: '1200'
    gracelimit: 3
    maxrepeat: 3
    maxsequence: 3
    dictcheck: true
    usercheck: true
    ipa_host: ipa.example.com
    ipa_user: admin
    ipa_pass: topsecret

- name: Ensure that the group sysops does not have a unique password policy
  community.general.ipa_pwpolicy:
    group: sysops
    state: absent
    ipa_host: ipa.example.com
    ipa_user: admin
    ipa_pass: topsecret
a  
pwpolicy:
  description: Password policy as returned by IPA API.
  returned: always
  type: dict
  sample:
    cn: ['admins']
    cospriority: ['10']
    dn: 'cn=admins,cn=EXAMPLE.COM,cn=kerberos,dc=example,dc=com'
    krbmaxpwdlife: ['60']
    krbminpwdlife: ['24']
    krbpwdfailurecountinterval: ['600']
    krbpwdhistorylength: ['16']
    krbpwdlockoutduration: ['1200']
    krbpwdmaxfailure: ['4']
    krbpwdmindiffchars: ['4']
    objectclass: ['top', 'nscontainer', 'krbpwdpolicy']
N)AnsibleModule)	IPAClientipa_argument_spec)	to_nativec                   :     e Zd ZdZ fdZd Zd Zd Zd Z xZ	S )PwPolicyIPAClientz8The global policy will be selected when `name` is `None`c                 2    t         t        |   ||||       y )N)superr   __init__)selfmodulehostportprotocol	__class__s        r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/ipa_pwpolicy.pyr   zPwPolicyIPAClient.__init__   s    /dHM    c                 8    |d}| j                  dd d|d      S )Nglobal_policypwpolicy_findT)allcnmethodnameitem
_post_jsonr   r   s     r   r   zPwPolicyIPAClient.pwpolicy_find   s)    < #DoDt[_G`aar   c                 *    | j                  d||      S )Npwpolicy_addr   r    r   r   r   s      r   r$   zPwPolicyIPAClient.pwpolicy_add       n4dKKr   c                 *    | j                  d||      S )Npwpolicy_modr   r    r%   s      r   r(   zPwPolicyIPAClient.pwpolicy_mod   r&   r   c                 (    | j                  d|      S )Npwpolicy_del)r   r   r    r"   s     r   r*   zPwPolicyIPAClient.pwpolicy_del   s    n4@@r   )
__name__
__module____qualname____doc__r   r   r$   r(   r*   __classcell__)r   s   @r   r   r      s$    BNbLLAr   r   c                     i }| |||||||||	|
|d}||d}|j                         D ]  \  }}|	t        |      ||<    |j                         D ]  \  }}|	t        |      ||<    |S )N)krbmaxpwdlifekrbminpwdlifekrbpwdhistorylengthkrbpwdmindiffcharskrbpwdminlengthcosprioritykrbpwdmaxfailurekrbpwdfailurecountintervalkrbpwdlockoutdurationpasswordgracelimitipapwdmaxrepeatipapwdmaxsequence)ipapwddictcheckipapwdusercheck)itemsr	   bool)
maxpwdlife
minpwdlifehistorylength
minclasses	minlengthprioritymaxfailcountfailintervallockouttime
gracelimit	maxrepeatmaxsequence	dictcheck	usercheckpwpolicypwpolicy_optionspwpolicy_boolean_optionsoptionvalues                      r   get_pwpolicy_dictrT      s     H##,($(&2!,($(  %$ 
 *//1 0(/HV0 2779 +#E{HV+ Or   c                 (    | j                  ||      S )N)ipa_datamodule_data)get_diff)clientipa_pwpolicymodule_pwpolicys      r   get_pwpolicy_diffr\      s    ??Lo?NNr   c                 x   | j                   d   }| j                   d   }t        | j                   j                  d      | j                   j                  d      | j                   j                  d      | j                   j                  d      | j                   j                  d      | j                   j                  d      | j                   j                  d	      | j                   j                  d
      | j                   j                  d      | j                   j                  d      | j                   j                  d      | j                   j                  d      | j                   j                  d      | j                   j                  d            }|j                  |      }d}|dk(  rg|s%d}| j                  sS|j                  ||      }||fS t        |||      }t        |      dkD  r!d}| j                  s|j                  ||      }||fS |r d}| j                  s|j                  |       ||fS )NstategrouprA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   )rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   )r   FpresentT)r   r   r   )
paramsrT   getr   
check_moder$   r\   lenr(   r*   )r   rY   r^   r   r[   rZ   changeddiffs           r   ensurerg      s   MM'"E==!D'6==3D3D\3R39==3D3D\3R6<mm6G6G6X39==3D3D\3R28--2C2CK2P171B1B:1N5;]]5F5F~5V5;]]5F5F~5V4:MM4E4Em4T39==3D3D\3R28--2C2CK2P4:MM4E4Em4T28--2C2CK2P28--2C2CK2P*O  ''T'2LG	G$$%22?2S L   %V\?KD4y1}((#)#6#6D#6#WL L   G$$###.L  r   c                  T   t               } | j                  t        ddg      t        ddddg      t        d      t        d      t        d      t        d      t        d      t        d      t        d      t        d      t        d      t        d      t        d      t        d      t        d	      t        d	      
       t        | d      }t	        ||j
                  d   |j
                  d   |j
                  d         }	 |j                  |j
                  d   |j
                  d          t        ||      \  }}|j                         y # t        $ r8}|j                  t        |      t        j                                Y d }~Pd }~ww xY w)Nstrr   )typealiasesr`   absent)rj   defaultchoices)rj   intr@   )r_   r^   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   T)argument_specsupports_check_modeipa_hostipa_portipa_prot)r   r   r   r   ipa_useripa_pass)usernamepassword)msg	exception)re   rO   )r   updatedictr   r   ra   loginrg   	Exception	fail_jsonr	   	traceback
format_exc	exit_json)rp   r   rY   re   rO   es         r   mainr     sy   %'MtA#	IW_K`a$(e$4$(e$4'+'7$(e$4#'U#3"&E"2&*&6&*&6%)u%5$(e$4#'U#3%)u%5#'V#4#'V#4  $ /35F f$*MM*$=$*MM*$=(.j(ACF
MfmmJ7$mmJ7 	 	9"662 Wx8  MYq\Y5I5I5KLLMs   <E& &	F'/.F""F'__main__)NNNNNNNNNNNNNN)
__future__r   r   r   rj   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   ansible.module_utils.basicr   >ansible_collections.community.general.plugins.module_utils.ipar   r   +ansible.module_utils.common.text.convertersr	   r   rT   r\   rg   r   r+    r   r   <module>r      s    A @Qf,\
&  4 g AA	 A, X\UYuy FO(!V#9L zF r   