HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: //usr/lib/python3/dist-packages/cloudinit/config/__pycache__/cc_set_passwords.cpython-310.pyc
o

�Ad3,�@stUdZddlZddlmZddlmZmZddlmZddl	m
Z
ddlmZddlm
Zdd	lmZmZdd
lmZddlmZddlmZmZdd
lmZmZmZddlmZddlmZdZdddeegeed�ed�ggd�Z ee!d<ee �Ze�"e#�Z$d�%dd�eeD��Z&de'de(de'fdd�Z)d d!�Z*d"efd#d$�Z+d%e(d&ed'ed(ed)e'ddfd*d+�Z,d/d-d.�Z-dS)0zFSet 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``.
�cc_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�title�description�distros�	frequency�examples�activate_by_schema_keys�meta�cCsg|]}|dvr|�qS)�loLOI01�)�.0�xr r �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�r&r r#r$gs��z%get_users_by_type.<locals>.<listcomp>r )r%r&r r.r#�get_users_by_typebs��
��r/c
CsRz|�d|�t�d�WdStjy(}z
t�d|�WYd}~dSd}~ww)N�restartzRestarted the SSH daemon.zm'ssh_pwauth' configuration may not be applied. Cloud-init was unable to restart SSH daemon due to error: '%s')�manage_service�LOG�debugr	�ProcessExecutionError�warning)�distro�service�er r r#�_restart_ssh_daemonos���r9r6cCs�|�dd�}d}t|t�rt�d�t�|�rd}n*t�|�r"d}n"d|�d�}|d	us2|��d
kr;t�	d||�d	St�d||�d	St
||i�}|sTt�	d
|�d	S|��rut�ddddd|g�j
��}|��dvrst||�d	Sd	St||�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: None�ssh_svcname�ssh�PasswordAuthenticationz�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.�yes�nozLeaving SSH config 'z' unchanged.N�	unchangedz%s ssh_pwauth=%sz$%s Unrecognized value: ssh_pwauth=%sz/No need to restart SSH service, %s not updated.�	systemctl�showz
--property�ActiveStatez--value)�active�
activating�	reloading)�
get_option�
isinstance�strr2r5r
�is_true�is_false�lowerr3r�uses_systemdr	�stdout�stripr9)�pw_authr6r7�cfg_name�cfg_val�bmsg�updated�stater r r#�handle_ssh_pwauth{sH
�

���	�
�rUr�cfg�cloudr�argscCs|j}|r|d}d|vrd|dvr|dd=nt�|dd�}d}g}g}	d|vrt|d}
tj|
dgd�}	d|
vrm|
drm|�d�t|
dt�rW|�d	�t�|
d|�}n|�d
�|�d�t�|
d�}|rm|��}t�	|
d|�}|	s�|s�|r�t
�||�\}}
t
�|�\}}|r�d
||fg}n|�d�g}|s�|	�r�t
|	d�}dd�|D�}t
|	d�}dd�|D�}g}t
|	d�D]\}}t�}|�|�|�||f�|�|�d|���q�t�d�}|D]F}|�dd�\}}|�|�du�rd|v�r|�||f�|�|�q�|dk�s|dk�rt�}|�d
||f�|�||f�|�|�q�|�r\z|�d|�|j|dd�Wnt�y[}z|�|�t�|d|�WYd}~nd}~ww|�r�z|�d|�|j|dd�Wnt�y�}z|�|�t�|d|�WYd}~nd}~wwt|��r�dd �|�f}tjd!|ddd"�|�r�|}tj�r�||7}g}|D]/}z
|�|�|�|�W�q�t�y�}z|�|�t�|d#|�WYd}~�q�d}~ww|�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.�textcS�g|]\}}|�qSr r �r!�user�_r r r#r$��zhandle.<locals>.<listcomp>r)cSr_r r r`r r r#r$�rcr+�:z\$(1|2a|2y|5|6)(\$.+){2}��RzChanging password for %s:F)�hashedz,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
)�stderr�fallback_to_stdoutzFailed to set 'expire' for %szExpired passwords for: %s users�
ssh_pwauthz+%s errors occurred, re-raising the last one���)r6r
�get_cfg_option_str�get_cfg_option_listr5rGrZr3�
splitlines�get_cfg_option_boolr�normalize_users_groups�extract_defaultr/�rand_user_password�append�re�compile�split�matchrY�	Exception�logexc�len�join�	multi_logr�EXPIRE_APPLIES_TO_HASHED_USERS�
expire_passwdrUr,)rrVrWrrXr6r*r]�plistr%�chfg�	multiliner[�_groupsra�_user_config�errors�plist_in�hashed_plist_in�hashed_users�randlistrb�prog�line�u�pr8�blurb�users_to_expire�
expired_usersr r r#�handle�s�
��
�







���
���
�
�

��
�r��cCstj|td�S)N)�select_from)r
�rand_str�PW_SET)�pwlenr r r#rs7srs)r�).�__doc__ru�loggingr�stringrr�textwrapr�typingr�	cloudinitrrr	r
�cloudinit.cloudr�cloudinit.configr�cloudinit.config.schemar
r�cloudinit.distrosrrr�cloudinit.settingsr�cloudinit.ssh_utilr�MODULE_DESCRIPTIONr�__annotations__�	getLogger�__name__r2r|r�rZrHr/r9rUr�rsr r r r#�<module>sh����)

4�����
�