o
    iF)                  
   @   s.  U d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	m
Z
mZ ddl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 degeg dZeed< eeZ de!de"de!fddZ#dede"de"fddZ$defddZ%de"dedede!ddf
dd Z&d$d"d#Z'dS )%zFSet Passwords: Set user passwords and enable/disable SSH password auth    N)List)features	lifecyclesubputil)Cloud)Config)
MetaSchema)ALL_DISTROSDistroug_util)log_util)PER_INSTANCE)update_ssh_configcc_set_passwords)iddistros	frequencyactivate_by_schema_keysmeta
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 | ]}| d d kr|d | ddfqS )typehashnamepasswordRANDOM)get).0itemr    C/usr/lib/python3/dist-packages/cloudinit/config/cc_set_passwords.py
<listcomp>(   s
    z%get_users_by_type.<locals>.<listcomp>r"   )r   r   r"   r!   r#   get_users_by_type#   s   
r%   distroservice
extra_argsc              
   G   sZ   z| j d|g|R   td W d S  tjy, } ztd| W Y d }~d S d }~ww )N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')manage_serviceLOGdebugr   ProcessExecutionErrorwarning)r&   r'   r(   er"   r"   r#   _restart_ssh_daemon0   s   r0   c                 C   s   | dd}d}t| trtjdddd t| rd}n*t| r%d	}n"d
| d}| du s5|  dkr>t	
d||  dS t	d||  dS t||i}|sWt	
d| dS | rytddddd|gj }| dv rwt||d dS dS 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: Nonessh_svcnamesshPasswordAuthenticationz-Using a string value for the 'ssh_pwauth' key22.2z&Use a boolean value with 'ssh_pwauth'.
deprecateddeprecated_versionextra_messageyesnozLeaving 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	reloadingz--job-mode=ignore-dependencies)
get_option
isinstancestrr   	deprecater   is_trueis_falselowerr+   r,   r.   r   uses_systemdr   stdoutstripr0   )pw_authr&   r'   cfg_namecfg_valbmsgupdatedstater"   r"   r#   handle_ssh_pwauth<   sP   


	
rR   r   cfgcloudargsc                 C   s  |j }|r|d }d|v rd|d v r|d d= nt|dd }d}g }g }d|v rz|d }	tj|	dg d}d|	v rs|	d rstjdd	d
d t|	d trZt	d t|	d|}ntjdddd t	d t|	d}
|
rs|

 }t|	d|}|s|s|rt||\}}t|\}}|rd||f g}ntd g }|s|rt|d}dd |D }t|d}dd |D }g }t|dD ]\}}t }|| |||f || d|  qtd}|D ]F}|dd\}}||d urd|vr|||f || q|dks|dkr#t }|d||f  |||f || q|rbzt	d| |j|dd  W n tya } z|| ttd!| W Y d }~nd }~ww |rzt	d"| |j|dd  W n ty } z|| ttd#| W Y d }~nd }~ww t|rd$d%|f}tjd&| ddd' |r|}tjr||7 }g }|D ]/}z| | || W q ty } z|| ttd(| W Y d }~qd }~ww |rt	d)| t!|"d*| t|rt	d+t| |d, d S )-Nr   chpasswdlistr   Tusers)defaultzConfig key 'lists'z22.3zUse 'users' instead.r5   z$Handling input for chpasswd as list.zThe chpasswd multiline stringr4   zUse string type instead.z0Handling input for chpasswd as multiline string.expirez%s:%sz2No default or defined user to change password for.textc                 S      g | ]\}}|qS r"   r"   r   user_r"   r"   r#   r$          zhandle.<locals>.<listcomp>r   c                 S   r\   r"   r"   r]   r"   r"   r#   r$      r`   r   :z\$(1|2a|2y|5|6)(\$.+){2}   RzChanging password for %s:F)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
)stderrfallback_to_stdoutzFailed to set 'expire' for %szExpired passwords for: %s users
ssh_pwauthz+%s errors occurred, re-raising the last one)#r&   r   get_cfg_option_strget_cfg_option_listr   rE   rC   rW   r+   r,   
splitlinesget_cfg_option_boolr   normalize_users_groupsextract_defaultr.   r%   rand_user_passwordappendrecompilesplitmatchrV   	Exceptionlogexclenjoinr   	multi_logr   EXPIRE_APPLIES_TO_HASHED_USERSexpire_passwdrR   r   )r   rS   rT   rU   r&   r   rZ   plistr   chfg	multilinerX   _groupsr^   _user_configerrorsplist_inhashed_plist_inhashed_usersrandlistr_   proglineupr/   blurbusers_to_expireexpired_usersr"   r"   r#   handle~   s   















r      c                 C   s   | dk rt dttjttjttjttjg}|t	t
j| t| tjtj tj tj d t| d|S )N   z.Password length must be at least 4 characters.)select_from )
ValueErrorrandomchoicestringdigitsascii_lowercaseascii_uppercasepunctuationextendrW   r   rand_strrx   shufflery   )pwlenres_rand_listr"   r"   r#   rp     s.   






rp   )r   )(__doc__loggingr   rr   r   typingr   	cloudinitr   r   r   r   cloudinit.cloudr   cloudinit.configr   cloudinit.config.schemar	   cloudinit.distrosr
   r   r   cloudinit.logr   cloudinit.settingsr   cloudinit.ssh_utilr   r   __annotations__	getLogger__name__r+   rW   rD   r%   r0   rR   r   rp   r"   r"   r"   r#   <module>   s4   
B 	