Your IP : 216.73.217.13


Current Path : /usr/lib/python3/dist-packages/cloudinit/config/__pycache__/
Upload File :
Current File : //usr/lib/python3/dist-packages/cloudinit/config/__pycache__/cc_set_passwords.cpython-38.pyc

U

�Ad3,�	@sfUdZddlZddlmZddlmZmZddlmZddl	m
Z
ddlmZddlm
Zdd	lmZmZdd
lmZddlmZddlmZmZdd
lmZmZmZddlmZddlmZdZdddeegeed�ed�ggd�Z ee!d<ee �Ze�"e#�Z$d�%dd�eeD��Z&e'e(e'd�dd�Z)dd�Z*ed �d!d"�Z+e(eeee'dd#�d$d%�Z,d)d'd(�Z-dS)*zFSet Passwords: Set user passwords and enable/disable SSH password auth�N)�Logger)�
ascii_letters�digits)�dedent)�List)�features)�log)�subp�util)�Cloud)�Config)�
MetaSchema�get_meta_doc)�ALL_DISTROS�Distro�ug_util)�PER_INSTANCE)�update_ssh_configa�This module consumes three top-level config keys: ``ssh_pwauth``, ``chpasswd``
and ``password``.

The ``ssh_pwauth`` config key determines whether or not sshd will be configured
to accept password authentication.

The ``chpasswd`` config key accepts a dictionary containing either or both of
``users`` and ``expire``. The ``users`` key is used to assign a password to a
corresponding pre-existing user. The ``expire`` key is used to set
whether to expire all user passwords specified by this module,
such that a password will need to be reset on the user's next login.

.. note::
    Prior to cloud-init 22.3, the ``expire`` key only applies to plain text
    (including ``RANDOM``) passwords. Post 22.3, the ``expire`` key applies to
    both plain text and hashed passwords.

``password`` config key is used to set the default user's password. It is
ignored if the ``chpasswd`` ``users`` is used. Note: the ``list`` keyword is
deprecated in favor of ``users``.
Zcc_set_passwordsz
Set Passwordsz7Set user passwords and enable/disable SSH password authz�            # Set a default password that would need to be changed
            # at first login
            ssh_pwauth: true
            password: password1
            aU            # Disable ssh password authentication
            # Don't require users to change their passwords on next login
            # Set the password for user1 to be 'password1' (OS does hashing)
            # Set the password for user2 to a pre-hashed password
            # Set the password for user3 to be a randomly generated password,
            #   which will be written to the system console
            ssh_pwauth: false
            chpasswd:
              expire: false
              users:
                - name: user1
                  password: password1
                  type: text
                - name: user2
                  password: $6$rounds=4096$5DJ8a9WMTEzIo5J4$Yms6imfeBvf3Yfu84mQBerh18l7OR1Wm1BJXZqFSpJ6BVas0AYJqIjP7czkOaAZHZi1kxQ5Y1IhgWN8K9NgxR1
                - name: user3
                  type: RANDOM
            )�id�name�titleZdescriptionZdistrosZ	frequencyZexamplesZactivate_by_schema_keys�meta�cCsg|]}|dkr|�qS)ZloLOI01�)�.0�xrr�C/usr/lib/python3/dist-packages/cloudinit/config/cc_set_passwords.py�
<listcomp>_sr)�
users_list�pw_type�returncs|sgS�fdd�|D�S)zDeither password or type: RANDOM is required, user is always requiredcs0g|](}|�dd��kr|d|�dd�f�qS)�type�hashr�password�RANDOM)�get)r�item�rrrrgs�z%get_users_by_type.<locals>.<listcomp>r)rrrr'r�get_users_by_typebs�
��r(c
CsPz|�d|�t�d�Wn0tjk
rJ}zt�d|�W5d}~XYnXdS)NZrestartzRestarted the SSH daemon.zm'ssh_pwauth' configuration may not be applied. Cloud-init was unable to restart SSH daemon due to error: '%s')Zmanage_service�LOG�debugr	ZProcessExecutionError�warning)�distro�service�errr�_restart_ssh_daemonos�r/)r,cCs�|�dd�}d}t|t�r$t�d�t�|�r4d}nRt�|�rDd}nBd|�d�}|d	ksd|��d
krtt�	d||�nt�d||�d	St
||i�}|s�t�	d
|�d	S|��r�t�ddddd|g�j
��}|��dkr�t||�n
t||�d	S)z�Apply sshd PasswordAuthentication changes.

    @param pw_auth: config setting from 'pw_auth'.
                    Best given as True, False, or "unchanged".
    @param distro: an instance of the distro class for the target distribution

    @return: NoneZssh_svcnameZsshZPasswordAuthenticationz�DEPRECATION: The 'ssh_pwauth' config key should be set to a boolean value. The string format is deprecated and will be removed in a future version of cloud-init.ZyesZnozLeaving SSH config 'z' unchanged.NZ	unchangedz%s ssh_pwauth=%sz$%s Unrecognized value: ssh_pwauth=%sz/No need to restart SSH service, %s not updated.Z	systemctlZshowz
--propertyZActiveStatez--value)ZactiveZ
activatingZ	reloading)Z
get_option�
isinstance�strr)r+r
Zis_trueZis_false�lowerr*rZuses_systemdr	�stdout�stripr/)Zpw_authr,r-Zcfg_nameZcfg_valZbmsg�updated�staterrr�handle_ssh_pwauth{s@
�

��

r7)r�cfg�cloudr�argsr cCs|j}|r2|d}d|kr@d|dkr@|dd=nt�|dd�}d}g}g}	d|kr�|d}
tj|
dgd�}	d|
kr�|
dr�|�d�t|
dt�r�|�d	�t�|
d|�}n,|�d
�|�d�t�|
d�}|r�|��}t�	|
d|�}|	�s8|�s8|�r8t
�||�\}}
t
�|�\}}|�r.d
||fg}n
|�d�g}|�sH|	�r�t
|	d�}dd�|D�}t
|	d�}dd�|D�}g}t
|	d�D]<\}}t�}|�|�|�||f�|�|�d|����q�t�d�}|D]�}|�dd�\}}|�|�dk	�rd|k�r|�||f�|�|�nD|dk�s.|dk�rFt�}|�d
||f�|�||f�|�|��q�|�r�z|�d|�|j|dd�Wn<tk
�r�}z|�|�t�|d|�W5d}~XYnX|�r&z|�d|�|j|dd�Wn<tk
�r$}z|�|�t�|d|�W5d}~XYnXt|��rRdd �|�f}tjd!|ddd"�|�r�|}tj�rl||7}g}|D]\}z|�|�|�|�Wn<tk
�r�}z|�|�t�|d#|�W5d}~XYnX�qt|�r�|�d$|�t|�d%�|�t|��r|�d&t|��|d'�dS)(Nr�chpasswd�listr#T�users)�defaultz8DEPRECATION: key 'lists' is now deprecated. Use 'users'.z$Handling input for chpasswd as list.z�DEPRECATION: The chpasswd multiline string format is deprecated and will be removed from a future version of cloud-init. Use the list format instead.z0Handling input for chpasswd as multiline string.�expirez%s:%sz2No default or defined user to change password for.�textcSsg|]\}}|�qSrr�r�user�_rrrr�szhandle.<locals>.<listcomp>r"cSsg|]\}}|�qSrrrArrrr�sr$�:z\$(1|2a|2y|5|6)(\$.+){2}��RzChanging password for %s:F)Zhashedz,Failed to set passwords with chpasswd for %szSetting hashed password for %s:z3Failed to set hashed passwords with chpasswd for %sz%Set the following 'random' passwords
�
z%s
%s
)�stderrZfallback_to_stdoutzFailed to set 'expire' for %szExpired passwords for: %s usersZ
ssh_pwauthz+%s errors occurred, re-raising the last one���)r,r
Zget_cfg_option_strZget_cfg_option_listr+r0r<r*�
splitlinesZget_cfg_option_boolrZnormalize_users_groupsZextract_defaultr(�rand_user_password�append�re�compile�split�matchr;�	ExceptionZlogexc�len�joinZ	multi_logrZEXPIRE_APPLIES_TO_HASHED_USERSZ
expire_passwdr7r%)rr8r9rr:r,r#r?ZplistrZchfgZ	multiliner=Z_groupsrBZ_user_config�errorsZplist_inZhashed_plist_inZhashed_usersZrandlistrC�prog�line�u�pr.ZblurbZusers_to_expireZ
expired_usersrrr�handle�s��
�






�
�
��

$
rY�cCstj|td�S)N)Zselect_from)r
Zrand_str�PW_SET)ZpwlenrrrrK7srK)rZ).�__doc__rMZloggingr�stringrr�textwrapr�typingrZ	cloudinitrrr	r
Zcloudinit.cloudrZcloudinit.configrZcloudinit.config.schemar
rZcloudinit.distrosrrrZcloudinit.settingsrZcloudinit.ssh_utilrZMODULE_DESCRIPTIONr�__annotations__Z	getLogger�__name__r)rSr[r<r1r(r/r7rYrKrrrr�<module>sZ����)

5�