
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
\me+              	   @   sH  U d Z ddlZddlZddlmZmZ ddlmZ ddlm	Z	 ddl
mZmZmZ ddlmZ ddlmZ dd	lmZmZ dd
lmZmZmZ ddlmZ ddlmZ dZdddeegeededgg dZeeeZ eje Z!dj"dd ee D Z#e$e%e$dddZ&dd Z'edddZ(e%eee$ddd d!Z)d%d#d$Z*dS )&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``.
Z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descriptionZdistrosZ	frequencyZexamplesZactivate_by_schema_keys c             C   s   g | ]}|d kr|qS )ZloLOI01 ).0xr   r   &/usr/lib/python3.6/cc_set_passwords.py
<listcomp>]   s    r   )
users_listpw_typereturnc                s   | sg S  fdd| D S )zDeither password or type: RANDOM is required, user is always requiredc                s0   g | ](}|j d d kr|d |j ddfqS )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| tjd W n0 tjk
rJ } ztjd| W Y d d }~X nX d S )NZrestartzRestarted the SSH daemon.zm'ssh_pwauth' configuration may not be applied. Cloud-init was unable to restart SSH daemon due to error: '%s')Zmanage_serviceLOGdebugr   ZProcessExecutionErrorwarning)distroserviceer   r   r   _restart_ssh_daemonm   s    r,   )r)   c             C   s   |j dd}d}t| tr*tjdddd tj| r:d}nRtj| rJd	}nBd
| d}| dksj| j dkrztj	d||  ntj
d||  dS t||i}|stj	d| dS |j rtjddddd|gjj }|j dkrt|| n
t|| dS )zApply 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: NoneZssh_svcnameZsshZPasswordAuthenticationz-Using a string value for the 'ssh_pwauth' keyz22.2z&Use a boolean value with 'ssh_pwauth'.)
deprecateddeprecated_versionextra_messageyesnozLeaving 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	systemctlZshowz
--propertyZActiveStatez--valueactive
activating	reloading)r2   r3   r4   )Z
get_option
isinstancestrr   	deprecateZis_trueZis_falselowerr&   r'   r(   r   Zuses_systemdr   stdoutstripr,   )Zpw_authr)   r*   Zcfg_nameZcfg_valZbmsgupdatedstater   r   r   handle_ssh_pwauthy   s>    


r=   )r   cfgcloudargsr   c          -   C   s2  |j }|r2|d }d|kr@d|d kr@|d d= ntj|dd }d}g }g }d|kr|d }	tj|	dg d}d|	kr|	d rtjdd	d
d t|	d trtjd tj|	d|}n2tjdddd tjd tj|	d}
|
r|
j	 }tj
|	d|}|p| rD|rDtj||\}}tj|\}}|r:d||f g}n
tjd g }|sT|rt|d}dd |D }t|d}dd |D }g }xJt|dD ]<\}}t }|j| |j||f |j| d|  qW tjd}x|D ]}|jdd\}}|j|d k	r,d|kr,|j||f |j| nD|dks@|dkrXt }|jd||f  |j||f |j| qW |rytjd| |j|dd  W n< tk
r } z|j| tjtd!| W Y d d }~X nX |r:ytjd"| |j|dd  W n< tk
r8 } z|j| tjtd#| W Y d d }~X nX t|rfd$d%j|f}tjd&| ddd' |r|}tjr||7 }g }xd|D ]\}y|j| |j| W n< tk
r } z|j| tjtd(| W Y d d }~X nX qW |rtjd)| t|j d*| t|r.tjd+t| |d, d S )-Nr   chpasswdlistr!   Tusers)defaultzConfig key 'lists'z22.3zUse 'users' instead.)r-   r.   r/   z$Handling input for chpasswd as list.zThe chpasswd multiline stringz22.2zUse string type instead.z0Handling input for chpasswd as multiline string.expirez%s:%sz2No default or defined user to change password for.textc             S   s   g | ]\}}|qS r   r   )r   user_r   r   r   r      s    zhandle.<locals>.<listcomp>r    c             S   s   g | ]\}}|qS r   r   )r   rG   rH   r   r   r   r      s    r"   :z\$(1|2a|2y|5|6)(\$.+){2}   RzChanging password for %s:F)Zhashedz,Failed to set passwords with chpasswd for %szSetting hashed password for %s:z3Failed to set hashed passwords with chpasswd for %sz%Set the following 'random' passwords

z%s
%s
)stderrZfallback_to_stdoutzFailed to set 'expire' for %szExpired passwords for: %s usersZ
ssh_pwauthz+%s errors occurred, re-raising the last one)!r)   r   Zget_cfg_option_strZget_cfg_option_listr7   r5   rB   r&   r'   
splitlinesZget_cfg_option_boolr   Znormalize_users_groupsZextract_defaultr(   r%   rand_user_passwordappendrecompilesplitmatchrA   	ExceptionZlogexclenjoinZ	multi_logr   ZEXPIRE_APPLIES_TO_HASHED_USERSZexpire_passwdr=   r#   )r   r>   r?   r@   r)   r!   rE   Zplistr   ZchfgZ	multilinerC   Z_groupsrG   Z_user_configerrorsZplist_inZhashed_plist_inZhashed_usersZrandlistrH   proglineupr+   ZblurbZusers_to_expireZexpired_usersr   r   r   handle   s    













&
r^      c             C   s   t j| tdS )N)Zselect_from)r   Zrand_strPW_SET)Zpwlenr   r   r   rP   5  s    rP   )r_   )+__doc__ZloggingrR   stringr   r   textwrapr   Ztypingr   Z	cloudinitr   r   r   Zcloudinit.cloudr	   Zcloudinit.configr
   Zcloudinit.config.schemar   r   Zcloudinit.distrosr   r   r   Zcloudinit.settingsr   Zcloudinit.ssh_utilr   ZMODULE_DESCRIPTIONmetaZ	getLogger__name__r&   rX   r`   rB   r6   r%   r,   r=   r^   rP   r   r   r   r   <module>   sB   
4 	