Your IP : 216.73.217.30


Current Path : /lib/python3/dist-packages/cloudinit/__pycache__/
Upload File :
Current File : //lib/python3/dist-packages/cloudinit/__pycache__/ssh_util.cpython-38.pyc

U

�Ad�R�@sLddlZddlZddlmZmZmZddlmZddlm	Z	e�
e�ZdZ
dZdZdee�d	ZGd
d�d�ZGdd
�d
�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Ze
fdd�Zd3dd�ZGdd�d�Zeed �d!d"�Zeed �d#d$�Zd%d&�Zee d'�d(d)�Z!d*d+�Z"e
fd,d-�Z#d.d/�Z$e
feeeefd0�d1d2�Z%dS)4�N)�List�Sequence�Tuple)�log)�utilz/etc/ssh/sshd_config)ZdsaZrsaZecdsaZed25519z(ecdsa-sha2-nistp256-cert-v01@openssh.comzecdsa-sha2-nistp256z(ecdsa-sha2-nistp384-cert-v01@openssh.comzecdsa-sha2-nistp384z(ecdsa-sha2-nistp521-cert-v01@openssh.comzecdsa-sha2-nistp521z+sk-ecdsa-sha2-nistp256-cert-v01@openssh.comz"sk-ecdsa-sha2-nistp256@openssh.comz#sk-ssh-ed25519-cert-v01@openssh.comzsk-ssh-ed25519@openssh.comzssh-dss-cert-v01@openssh.comzssh-dssz ssh-ed25519-cert-v01@openssh.comzssh-ed25519zssh-rsa-cert-v01@openssh.comzssh-rsazssh-xmss-cert-v01@openssh.comzssh-xmss@openssh.com�z�no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command="echo 'Please login as the user \"$USER\" rather than the user \"$DISABLE_USER\".';echo;sleep 10;exit �"c@s&eZdZddd�Zdd�Zdd�ZdS)	�AuthKeyLineNcCs"||_||_||_||_||_dS�N)�base64�comment�options�keytype�source)�selfrrrrr
�r�4/usr/lib/python3/dist-packages/cloudinit/ssh_util.py�__init__Gs
zAuthKeyLine.__init__cCs|jo
|jSr
)rr�rrrr�validPszAuthKeyLine.validcCsdg}|jr|�|j�|jr(|�|j�|jr:|�|j�|jrL|�|j�|sV|jSd�|�SdS�N� )r
�appendrrrr�join)r�toksrrr�__str__SszAuthKeyLine.__str__)NNNN)�__name__�
__module__�__qualname__rrrrrrrr	Fs�
	r	c@s"eZdZdZdd�Zddd�ZdS)�AuthKeyLineParsera�
    AUTHORIZED_KEYS FILE FORMAT
     AuthorizedKeysFile specifies the file containing public keys for public
     key authentication; if none is specified, the default is
     ~/.ssh/authorized_keys.  Each line of the file contains one key (empty
     (because of the size of the public key encoding) up to a limit of 8 kilo-
     bytes, which permits DSA keys up to 8 kilobits and RSA keys up to 16
     kilobits.  You don't want to type them in; instead, copy the
     identity.pub, id_dsa.pub, or the id_rsa.pub file and edit it.

     sshd enforces a minimum RSA key modulus size for protocol 1 and protocol
     2 keys of 768 bits.

     The options (if present) consist of comma-separated option specifica-
     tions.  No spaces are permitted, except within double quotes.  The fol-
     lowing option specifications are supported (note that option keywords are
     case-insensitive):
    cCs�d}d}|t|�kr�|s$||dkr�||}|dt|�krF|d}q�||d}|dkrl|dkrl|d}n|dkrz|}|d}q|d|�}||d���}||fS)z�
        The options (if present) consist of comma-separated option specifica-
         tions.  No spaces are permitted, except within double quotes.
         Note that option keywords are case-insensitive.
        Fr)r�	��\rN)�len�lstrip)r�entZquoted�iZcurcZnextcr
�remainrrr�_extract_optionsws 

z"AuthKeyLineParser._extract_optionsNcCs�|�d�}|�d�s |��dkr(t|�Sdd�}|��}z||�\}}}Wnbtk
r�|�|�\}	}
|dkrt|	}z||
�\}}}Wn tk
r�t|�YYSXYnXt|||||d�S)Nz
�#�cSs^|�dd�}t|�dkr(tdt|���|dtkrDtd|d��t|�dkrZ|�d�|S)N�zTo few fields: %srzInvalid keytype %sr*)�splitr#�	TypeError�VALID_KEY_TYPESr)r%rrrr�
parse_ssh_key�s
z.AuthKeyLineParser.parse.<locals>.parse_ssh_key)rrrr
)�rstrip�
startswith�stripr	r-r()rZsrc_liner
�liner/r%rrrZkeyoptsr'rrr�parse�s,
�zAuthKeyLineParser.parse)N)rrr�__doc__r(r4rrrrrcsrc
Cs|g}t�}g}|D]d}z8tj�|�rLt�|���}|D]}|�|�|��q6Wqt	t
fk
rtt�td|�YqXq|S)NzError reading lines from %s)
r�os�path�isfiler�	load_file�
splitlinesrr4�IOError�OSError�logexc�LOG)�fnames�lines�parser�contents�fnamer3rrr�parse_authorized_keys�srDcCs�tdd�|D��}tdt|��D]J}||}|��s6q |D]&}|j|jkr:|}||kr:|�|�q:|||<q |D]}|�|�qpdd�|D�}|�d�d�|�S)NcSsg|]}|��r|�qSr)r��.0�krrr�
<listcomp>�sz*update_authorized_keys.<locals>.<listcomp>rcSsg|]}t|��qSr��str)rF�brrrrH�sr*�
)�list�ranger#rr�removerr)Zold_entries�keysZto_addr&r%rG�keyr@rrr�update_authorized_keys�s 

rRcCs4t�|�}|r|js td|��tj�|jd�|fS)Nz"Unable to get SSH info for user %rz.ssh)�pwd�getpwnam�pw_dir�RuntimeErrorr6r7r)�username�pw_entrrr�users_ssh_info�s

rYc	Cspd|fd|fdf}|sd}|��}g}|D]@}|D]\}}|�||�}q2|�d�s`tj�||�}|�|�q*|S)N�%h�%u)z%%�%�%h/.ssh/authorized_keys�/)r,�replacer1r6r7rr)	�valueZhomedirrWZmacros�pathsZrenderedr7ZmacroZfieldrrr�render_authorizedkeysfile_paths�s
rbc
Cs�d}|rd}t�|�}|r@||kr@|dkr@t�d||||�dSt�|�}||kr\|dM}n.t�|�}t�|�}	||	kr�|dM}n|dM}||@d	kr�t�d
|||�dS|r�|d@d	kr�t�d||�dSd
S)aVCheck if the file/folder in @current_path has the right permissions.

    We need to check that:
    1. If StrictMode is enabled, the owner is either root or the user
    2. the user can access the file/folder, otherwise ssh won't use it
    3. If StrictMode is enabled, no write permission is given to group
       and world users (022)
    i�i��rootzXPath %s in %s must be own by user %s or by root, but instead is own by %s. Ignoring key.F��8�rzBPath %s in %s must be accessible by user %s, check its permissions�zRPath %s in %s must not give writepermission to group or world users. Ignoring key.T)rZ	get_ownerr>�debugZget_permissionsZ	get_groupZget_user_groups)
rWZcurrent_path�	full_path�is_file�strictmodesZminimal_permissions�ownerZparent_permissionZgroup_ownerZuser_groupsrrr�check_permissionssJ
�




��rmc
Cs�t|�d}td�d}�z�|�d�dd�}d}tj�|j�}|D]�}|d|7}tj�|�rtt�d|�WdStj�	|�r�t�d|�WdS|�
|�sD||jkr�qDtj�|��st�
|��Pd	}	|j}
|j}|�
|j�r�d
}	|j}
|j}tj||	dd�t�||
|�W5QRXt|||d|�}|sDWdSqDtj�|��sRtj�|��rdt�d
|�WdStj�|��s�tj|dddd�t�||j|j�t|||d|�}|�s�WdSWn>ttfk
�r�}
zt�tt|
��WY�dSd}
~
XYnXdS)Nr!rcr^���r*z-Invalid directory. Symlink exists in path: %sFz*Invalid directory. File exists in path: %s��rdT)�mode�exist_okz%s is not a file!�)rpZensure_dir_exists)rYr,r6r7�dirnamerU�islinkr>rhr8r1�existsr�SeLinuxGuard�pw_uid�pw_gid�makedirsZ	chownbyidrm�isdir�
write_filer;r<r=rJ)rW�filenamerkZ
user_pwentZ
root_pwentZdirectoriesZ
parent_folderZhome_folderZ	directoryrpZuid�gidZpermissions�errr�check_create_pathIs������
�
rc
Cs t|�\}}tj�|d�}|}g}tj|dd��nz2t|�}|�dd�}|�dd�}	t||j	|�}Wn4t
tfk
r�||d<t�t
d	t|d�YnXW5QRXt|��|�D]H\}
}td
|
kd|
k|�d�|j	��g�r�t|||	dk�}|r�|}q�q�||k�rt
�d
|�|t|g�fS)NZauthorized_keysT��	recursiveZauthorizedkeysfiler]rkZyesrzhFailed extracting 'AuthorizedKeysFile' in SSH config from %r, using 'AuthorizedKeysFile' file %r insteadr[rZz{}/zAAuthorizedKeysFile has an user-specific authorized_keys, using %s)rYr6r7rrrv�parse_ssh_config_map�getrbrUr;r<r=r>�DEF_SSHD_CFG�zipr,�anyr1�formatrrhrD)
rWZ
sshd_cfg_file�ssh_dirrXZdefault_authorizedkeys_fileZuser_authorizedkeys_fileZauth_key_fnsZssh_cfgZ	key_pathsrkZkey_path�auth_key_fnZpermissions_okrrr�extract_authorized_keys�s`���
���
��r�c
	Cs|t�}g}|D]}|�|jt|�|d��qt|�\}}tj�|�}tj	|dd�� t
||�}	tj||	dd�W5QRXdS)N)r
Tr���
preserve_mode)rrr4rJr�r6r7rsrrvrRr{)
rPrWr
rAZkey_entriesrGr�Zauth_key_entriesr��contentrrr�setup_user_keys�s
r�c@s*eZdZddd�Zedd��Zdd�ZdS)	�SshdConfigLineNcCs||_||_||_dSr
)r3�_keyr`)rr3rG�vrrrr�szSshdConfigLine.__init__cCs|jdkrdS|j��Sr
)r��lowerrrrrrQ�s
zSshdConfigLine.keycCs>|jdkrt|j�St|j�}|jr6|dt|j�7}|SdSr)r�rJr3r`)rr�rrrr�s


zSshdConfigLine.__str__)NN)rrrr�propertyrQrrrrrr��s

r�)�returncCs"tj�|�sgStt�|����Sr
)r6r7r8�parse_ssh_config_linesrr9r:�rCrrr�parse_ssh_config�sr�cCs�g}|D]�}|��}|r"|�d�r2|�t|��qz|�dd�\}}WnPtk
r�z|�dd�\}}Wn&tk
r�t�d|�YYqYnXYnX|�t|||��q|S)Nr)r!�=z;sshd_config: option "%s" has no key/value pair, skipping it)r2r1rr�r,�
ValueErrorr>rh)r@�retr3rQ�valrrrr��s&�r�cCs6t|�}|siSi}|D]}|js$q|j||j<q|Sr
)r�rQr`)rCr@r�r3rrrr�sr�)rCr�c	CsVtj�|�sdSt|d��2}|D]&}|�d|�d��r W5QR�dSq W5QRXdS)NF�rzInclude z	.d/*.confT)r6r7r8�openr1)rC�fr3rrr�_includes_dconf$sr�cCs^t|�rZtj�|�d��s.tj|�d�dd�tj�|�d�d�}tj�|�sZt�|d�|S)Nz.dro)rpz50-cloud-init.confrr)	r�r6r7rzrZ
ensure_dirrr8Zensure_filer�rrr�"_ensure_cloud_init_ssh_config_file.sr�cCsPt|�}t|�}t||d�}|rDtj|d�dd�|D��ddd�t|�dkS)z�Read fname, and update if changes are necessary.

    @param updates: dictionary of desired values {Option: value}
    @return: boolean indicating if an update was done.)r@�updatesrLcSsg|]}t|��qSrrI)rFr3rrrrHDsz%update_ssh_config.<locals>.<listcomp>Tr�r)r�r��update_ssh_config_linesrr{rr#)r�rCr@�changedrrr�update_ssh_config9s�r�c	Cst�}g}tdd�|��D��}t|dd�D]v\}}|js<q,|j|kr,||j}||}|�|�|j|kr~t�d|||�q,|�	|�t�d|||j|�||_q,t
|�t
|�k�r|��D]B\}}||kr�q�|�	|�|�	td||��t�dt
|�||�q�|S)	z�Update the SSH config lines per updates.

    @param lines: array of SshdConfigLine.  This array is updated in place.
    @param updates: dictionary of desired values {Option: value}
    @return: A list of keys in updates that were changed.cSsg|]}|��|f�qSr)r�rErrrrHTsz+update_ssh_config_lines.<locals>.<listcomp>r!)�startz$line %d: option %s already set to %sz#line %d: option %s updated %s -> %sr*z line %d: option %s added with %s)
�set�dictrP�	enumeraterQ�addr`r>rhrr#�itemsr�)	r@r��foundr�Zcasemapr&r3rQr`rrrr�JsN



�
�
�r�)r@cCs>|sdSt|�}dd�|D�}tj|d�|�dddd�dS)Ncss |]\}}|�d|��VqdS)rNr)rFrGr�rrr�	<genexpr>|sz$append_ssh_config.<locals>.<genexpr>rLZabT)Zomoder�)r�rr{r)r@rCr�rrr�append_ssh_configxs�r�)N)&r6rS�typingrrrZ	cloudinitrZloggingrZ	getLoggerrr>r�r.Z_DISABLE_USER_SSH_EXITrJZDISABLE_USER_OPTSr	rrDrRrYrbrmrr�r�r�r�r�r��boolr�r�r�r�r�rrrr�<module>	sB
���YEO9

.