3 \�me�+ � @ sH U d Z ddlZddlZddlmZmZ ddlmZ ddlm Z ddl mZmZm Z ddlmZ ddlmZ dd lmZmZ dd lmZmZmZ ddlmZ ddlmZ d Zdddeegeed�ed�gg d�Zeee�Z eje �Z!dj"dd� ee D ��Z#e$e%e$d�dd�Z&dd� Z'ed�dd�Z(e%eee$dd�d d!�Z)d%d#d$�Z*dS )&zFSet Passwords: Set user passwords and enable/disable SSH password auth� N)� ascii_letters�digits)�dedent)�List)�features�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�title�descriptionZdistrosZ frequencyZexamplesZactivate_by_schema_keys� c C s g | ]}|d kr|�qS )ZloLOI01� )�.0�xr r �&/usr/lib/python3.6/cc_set_passwords.py� <listcomp>] s r )� users_list�pw_type�returnc s | sg S � fdd�| D �S )zDeither password or type: RANDOM is required, user is always requiredc s0 g | ](}|j d d�� kr|d |j dd�f�qS )�type�hashr �password�RANDOM)�get)r �item)r r r r f s z%get_users_by_type.<locals>.<listcomp>r )r r r )r r �get_users_by_type` s r% c C sP y| j d|� tjd� W n0 tjk rJ } ztjd|� W Y d d }~X nX d S )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�er r r �_restart_ssh_daemonm s r, )r) c C s� |j dd�}d}t| t�r*tjdddd� tj| �r:d}nRtj| �rJd }nBd |� d�}| dksj| j� d krztj d|| � ntj d|| � dS t||i�}|s�tj d|� dS |j� r�t j ddddd|g�jj� }|j� dkr�t||� n t||� dS )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-Using a string value for the 'ssh_pwauth' keyz22.2z&Use a boolean value with 'ssh_pwauth'.)� deprecated�deprecated_version� extra_message�yes�nozLeaving 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�active� activating� reloading)r2 r3 r4 )Z get_option� isinstance�strr � deprecateZis_trueZis_false�lowerr&