o
    x[hF)  ã                
   @   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 )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-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ž|ržt ||¡\}}t |¡\}}|r™d||f g}nt d¡ g }|s¥|�rôt|dƒ}dd„ |D ƒ}t|dƒ}dd„ |D ƒ}g }t|dƒD ]\}}tƒ }| |¡ | ||f¡ | |› d|› �¡ qÄt 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 |�r”zt 	d"|¡ |j|dd � W n t�y“ } z| |¡ t td#|¡ W Y d }~nd }~ww t|ƒ�rªd$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 }~�q»d }~ww |�rôt 	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 	