
    Vh!                        d dl mZmZmZ eZdZdZd dlZd dl	Z	d dl
mZ d dlmZ d dlmZ  ej                   d      5  d d	lmZmZ d d
lmZ ddd       g dZd Zd Zd Zd Zd Zedk(  r e        yy# 1 sw Y   *xY w)    )absolute_importdivisionprint_functiona	  
module: htpasswd
short_description: Manage user files for basic authentication
description:
  - Add and remove username/password entries in a password file using htpasswd.
  - This is used by web servers such as Apache and Nginx for basic authentication.
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  path:
    type: path
    required: true
    aliases: [dest, destfile]
    description:
      - Path to the file that contains the usernames and passwords.
  name:
    type: str
    required: true
    aliases: [username]
    description:
      - User name to add or remove.
  password:
    type: str
    required: false
    description:
      - Password associated with user.
      - Must be specified if user does not exist yet.
  hash_scheme:
    type: str
    required: false
    default: "apr_md5_crypt"
    description:
      - Hashing scheme to be used. As well as the four choices listed here, you can also use any other hash supported by passlib,
        such as V(portable_apache22) and V(host_apache24); or V(md5_crypt) and V(sha256_crypt), which are Linux passwd hashes.
        Only some schemes in addition to the four choices below will be compatible with Apache or Nginx, and supported schemes
        depend on passlib version and its dependencies.
      - See U(https://passlib.readthedocs.io/en/stable/lib/passlib.apache.html#passlib.apache.HtpasswdFile) parameter C(default_scheme).
      - 'Some of the available choices might be: V(apr_md5_crypt), V(des_crypt), V(ldap_sha1), V(plaintext).'
    aliases: [crypt_scheme]
  state:
    type: str
    required: false
    choices: [present, absent]
    default: "present"
    description:
      - Whether the user entry should be present or not.
  create:
    required: false
    type: bool
    default: true
    description:
      - Used with O(state=present). If V(true), the file will be created if it does not exist. Conversely, if set to V(false)
        and the file does not exist it will fail.
notes:
  - This module depends on the C(passlib) Python library, which needs to be installed on all target systems.
  - 'On Debian < 11, Ubuntu <= 20.04, or Fedora: install C(python-passlib).'
  - 'On Debian, Ubuntu: install C(python3-passlib).'
  - 'On RHEL or CentOS: Enable EPEL, then install C(python-passlib).'
requirements: [passlib>=1.6]
author: "Ansible Core Team"
extends_documentation_fragment:
  - files
  - community.general.attributes
a<  
- name: Add a user to a password file and ensure permissions are set
  community.general.htpasswd:
    path: /etc/nginx/passwdfile
    name: janedoe
    password: '9s36?;fyNp'
    owner: root
    group: www-data
    mode: '0640'

- name: Remove a user from a password file
  community.general.htpasswd:
    path: /etc/apache2/passwdfile
    name: foobar
    state: absent

- name: Add a user to a password file suitable for use by libpam-pwdfile
  community.general.htpasswd:
    path: /etc/mail/passwords
    name: alex
    password: oedu2eGh
    hash_scheme: md5_crypt
N)AnsibleModule)deps)	to_nativepasslib)HtpasswdFilehtpasswd_context)CryptContext)apr_md5_crypt	des_crypt	ldap_sha1	plaintextc                     t         j                  j                  |       }t         j                  j                  |      st        j                  |       y y N)ospathdirnameexistsmakedirs)destdestpaths     n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/htpasswd.pycreate_missing_directoriesr   y   s5    wwt$H77>>(#
H $    c                    |t         v rt        }nt        |gt         z         }t        j                  j                  |       s_|st        d| z        |rd| z  dfS t        |        t        | d||      }|j                  ||       |j                          d| d|dfS t        | d||      }|j                  ||      }|rd	|z  dfS |s"|j                  ||       |j                          d
|z  dfS )z5 Ensures user is present

    Returns (msg, changed) )schemeszDestination %s does not existz	Create %sT)newdefault_schemecontextzCreated z and added Fz%s already presentzAdd/update %s)apache_hashesr   r   r   r   r   
ValueErrorr   r
   set_passwordsavecheck_password)	r   usernamepasswordhash_schemecreate
check_moder!   htfounds	            r   presentr.      s     m#"}}'DE77>>$<tCDD$&--"4($DgV
(+
	-18<dCC$E+wW!!(H5(83U;;(3	#h.55r   c                     t        | d      }||j                         vrd|z  dfS |s!|j                  |       |j                          d|z  dfS )z4 Ensures user is absent

    Returns (msg, changed) F)r   %s not presentz	Remove %sT)r
   usersdeleter%   )r   r'   r+   r,   s       r   absentr3      sV     
d	&Brxxz! 8+U33IIhGGIh&--r   c                     | j                  | j                        }| j                  |d      r|r|dz  }d}|dz  }||fS )NFz and Tz,ownership, perms or SE linux context changed)load_file_common_argumentsparamsset_fs_attributes_if_different)modulechangedmessage	file_argss       r   check_file_attrsr<      sN    11&--@I,,Y>wGAAGr   c                     t        t        ddddg      t        dddg      t        ddd d	      t        ddd
dg      t        dddddg      t        dd            } t        | dd      }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  }t	        j
                  |       	 t        |d      5 }	|	j                         }
d d d        d}
D ]  }|j                         rd} n |rO|r t        j                         }|j                  }t        |d      5 }	|	j                  d |
D               d d d        	 |dk(  rt        ||||||      \  }}ne|dk(  rJt        j                   j#                  |      s|j%                  d|z  d|z  d       t'        |||      \  }}n|j)                  d|z          y t+        |||      \  }}|j%                  ||!       y # 1 sw Y   xY w# 1 sw Y   xY w# t        $ r Y w xY w# t,        $ r%}|j)                  t/        |              Y d }~y d }~ww xY w)"Nr   Tr   destfile)typerequiredaliasesstrr'   F)r?   r@   defaultno_logr   crypt_scheme)r?   r@   rC   rA   r.   r3   )r?   r@   rC   choicesbool)r?   rC   )r   namer(   r)   stater*   )argument_specadd_file_common_argssupports_check_moderH   r(   r)   rI   r*   rwc              3   B   K   | ]  }|j                         s|  y wr   )strip).0lines     r   	<genexpr>zmain.<locals>.<genexpr>   s     Ddtzz|TDs   r0   z%s does not exist)msgwarningsr9   zInvalid state: %s)rT   )rT   r9   )dictr   r6   r+   r   validateopen	readlinesrP   tempfileNamedTemporaryFilerH   
writelinesIOErrorr.   r   r   r   	exit_jsonr3   	fail_jsonr<   	Exceptionr   )arg_specr8   r   r'   r(   r)   rI   r*   r+   flinesrP   rR   temprT   r9   es                    r   mainrf      s   vvz6JKutj\B55$tLee_WeVfgy9V^J_`.H 04/35F == D}}V$H}}Z(H--.KMM'"E]]8$F""JMM&$_ 	"KKME	"  	D::<	
 224yydC EADeDDE+I$T8X{FT^_NS'h77>>$'  %5%@*=*De ! U#D(J?NS'!4u!<=)&'3?gS'2G	" 	"E E  $  +Yq\**+sg   ,H; 8H"	H; )5H; H/7H;  A=I
 >#I
 "H,'H; /H84H; ;	II
	I8I33I8__main__)
__future__r   r   r   r?   __metaclass__DOCUMENTATIONEXAMPLESr   rZ   ansible.module_utils.basicr   :ansible_collections.community.general.plugins.module_utilsr   +ansible.module_utils.common.text.convertersr   declarepasslib.apacher
   r   passlib.contextr   r"   r   r.   r3   r<   rf   __name__ r   r   <module>rt      s    A @BH2 
  4 K A T\\) -=,-
 I6@.
@+F zF [- -s   A55A>