BYPASS SHELL BY ./RAZORGANZ
Server: nginx/1.20.1
System: Linux iZdzfnv9mwfppeZ 5.10.134-19.2.al8.x86_64 #1 SMP Wed Oct 29 22:47:09 CST 2025 x86_64
User: apache (48)
PHP: 8.2.30
Disabled: NONE
Upload Files
File: //lib/python3.6/site-packages/cloudinit/__pycache__/ssh_util.cpython-36.pyc
3

�q�hMY�@slddlZddlZddlmZddlmZmZmZddlm	Z
ddlmZmZe
j
e�ZdZdMZdZdee�dZGd d!�d!�ZGd"d#�d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zefd0d1�ZdNd2d3�ZGd4d5�d5�Zeed6�d7d8�Z eed6�d9d:�Z!d;d<�Z"ee#d=�d>d?�Z$d@dA�Z%efdBdC�Z&dDdE�Z'efeeeefdF�dGdH�Z(dIdJ�Z)dKdL�Z*dS)O�N)�suppress)�List�Sequence�Tuple)�log)�subp�utilz/etc/ssh/sshd_config�dsa�rsa�ecdsa�ed25519�(ecdsa-sha2-nistp256-cert-v01@openssh.com�ecdsa-sha2-nistp256�(ecdsa-sha2-nistp384-cert-v01@openssh.com�ecdsa-sha2-nistp384�(ecdsa-sha2-nistp521-cert-v01@openssh.com�ecdsa-sha2-nistp521�+sk-ecdsa-sha2-nistp256-cert-v01@openssh.com�"sk-ecdsa-sha2-nistp256@openssh.com�#sk-ssh-ed25519-cert-v01@openssh.com�sk-ssh-ed25519@openssh.com�ssh-dss-cert-v01@openssh.com�ssh-dss� ssh-ed25519-cert-v01@openssh.com�ssh-ed25519�ssh-rsa-cert-v01@openssh.com�ssh-rsa�ssh-xmss-cert-v01@openssh.com�ssh-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)�selfr&r%r"r#r$�r(�/usr/lib/python3.6/ssh_util.py�__init__Hs
zAuthKeyLine.__init__cCs|jo
|jS)N)r"r%)r'r(r(r)�validQszAuthKeyLine.validcCsdg}|jr|j|j�|jr(|j|j�|jr:|j|j�|jrL|j|j�|sV|jSdj|�SdS)N� )r$�appendr%r"r#r&�join)r'�toksr(r(r)�__str__TszAuthKeyLine.__str__)NNNN)�__name__�
__module__�__qualname__r*r+r0r(r(r(r)r!Gs
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}x~|t|�kr�|s&||d	kr�||}|dt|�krH|d}P||d}|dkrn|dkrn|d}n|dkr||}|d}q
W|d|�}||d�j�}||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.
        Frr,�	��\r N)r,r5)�len�lstrip)r'�entZquoted�iZcurcZnextcr$�remainr(r(r)�_extract_optionsxs 
z"AuthKeyLineParser._extract_optionsNcCs�|jd�}|jd�s |j�dkr(t|�Sdd�}|j�}y||�\}}}WnZtk
r�|j|�\}	}
|dkrt|	}y||
�\}}}Wntk
r�t|�SXYnXt|||||d�S)Nz
�#�cSs^|jdd�}t|�dkr(tdt|���|dtkrDtd|d��t|�dkrZ|jd�|S)N�zTo few fields: %srzInvalid keytype %sr?)�splitr8�	TypeError�VALID_KEY_TYPESr-)r:r/r(r(r)�
parse_ssh_key�s
z.AuthKeyLineParser.parse.<locals>.parse_ssh_key)r%r"r#r$)�rstrip�
startswith�stripr!rBr=)r'Zsrc_liner$�linerDr:r%r"r#Zkeyoptsr<r(r(r)�parse�s*
zAuthKeyLineParser.parse)N)r1r2r3�__doc__r=rIr(r(r(r)r4dsr4c
Cs�g}t�}g}xp|D]h}y<tjj|�rRtj|�j�}x|D]}|j|j|��q:WWqt	t
fk
rztjtd|�YqXqW|S)NzError reading lines from %s)
r4�os�path�isfiler�	load_file�
splitlinesr-rI�IOError�OSError�logexc�LOG)�fnames�lines�parser�contents�fnamerHr(r(r)�parse_authorized_keys�s

rYcCs�tdd�|D��}x`tdt|��D]N}||}|j�s8q"x.|D]&}|j|jkr>|}||kr>|j|�q>W|||<q"Wx|D]}|j|�qzWdd�|D�}|jd�dj|�S)NcSsg|]}|j�r|�qSr()r+)�.0�kr(r(r)�
<listcomp>�sz*update_authorized_keys.<locals>.<listcomp>rcSsg|]}t|��qSr()�str)rZ�br(r(r)r\�sr?�
)�list�ranger8r+r"�remover-r.)Zold_entries�keysZto_addr;r:r[�keyrUr(r(r)�update_authorized_keys�s 


recCs8tj|�}|s|jr$td|��tjj|jd�|fS)Nz"Unable to get SSH info for user %rz.ssh)�pwd�getpwnam�pw_dir�RuntimeErrorrKrLr.)�username�pw_entr(r(r)�users_ssh_info�s
rlc	Csxd|fd|fdf}|sd}|j�}g}xL|D]D}x|D]\}}|j||�}q6W|jd�sftjj||�}|j|�q,W|S)Nz%hz%u�%%�%z%h/.ssh/authorized_keys�/)rmrn)rA�replacerFrKrLr.r-)	�valueZhomedirrjZmacros�pathsZrenderedrLZmacroZfieldr(r(r)�render_authorizedkeysfile_paths�s

rsc
Cs�d}|rd}tj|�}|r@||kr@|dkr@tjd||||�dStj|�}||kr\|dM}n.tj|�}tj|�}	||	kr�|dM}n|dM}||@d	kr�tjd
|||�dS|r�|d@d	kr�tjd||�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.Fi��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_ownerrS�debugZget_permissionsZ	get_groupZget_user_groups)
rjZcurrent_path�	full_path�is_file�strictmodesZminimal_permissions�ownerZparent_permissionZgroup_ownerZuser_groupsr(r(r)�check_permissionssD





r}cCs�t|�d}td�d}�y�|jd�dd�}d}tjj|j�}x�|D]�}|d|7}tjj|�rrtjd|�dStjj	|�r�tjd|�dS|j
|�sF||jkr�qFtjj|��stj
|��Pd}	|j}
|j}|j
|j�r�d	}	|j}
|j}tj||	d
d�tj||
|�WdQRXt|||d|�}|sFdSqFWtjj|��sJtjj|��rZtjd|�dStjj|��s�tj|dd
d
d�tj||j|j�t|||d
|�}|�s�dSWn6ttfk
�r�}
ztjtt|
��dSd}
~
XnXd
S)Nr6rtror?z-Invalid directory. Symlink exists in path: %sFz*Invalid directory. File exists in path: %si�i�T)�mode�exist_okz%s is not a file!i�)r~Zensure_dir_exists���)rlrArKrL�dirnamerh�islinkrSrxrMrF�existsr�SeLinuxGuardZpw_uidZpw_gid�makedirsZ	chownbyidr}�isdir�
write_filerPrQrRr])rj�filenamer{Z
user_pwentZ
root_pwentZdirectoriesZ
parent_folderZhome_folderZ	directoryr~Zuid�gidZpermissions�er(r(r)�check_create_pathJsb


r�c
Cs"t|�\}}tjj|d�}|}g}tj|dd��ny2t|�}|jdd�}|jdd�}	t||j	|�}Wn4t
tfk
r�||d<tjt
d	t|d�YnXWdQRXxXt|j�|�D]F\}
}td
|
kd|
k|jdj|j	��g�r�t|||	dk�}|r�|}Pq�W||k�rt
jd
|�|t|g�fS)NZauthorized_keysT)�	recursiveZauthorizedkeysfilez%h/.ssh/authorized_keysr{�yesrzhFailed extracting 'AuthorizedKeysFile' in SSH config from %r, using 'AuthorizedKeysFile' file %r insteadz%uz%hz{}/zAAuthorizedKeysFile has an user-specific authorized_keys, using %s)rlrKrLr.rr��parse_ssh_config_map�getrsrhrPrQrRrS�DEF_SSHD_CFG�ziprA�anyrF�formatr�rxrY)
rjZ
sshd_cfg_file�ssh_dirrkZdefault_authorizedkeys_fileZuser_authorizedkeys_fileZauth_key_fnsZssh_cfgZ	key_pathsr{Zkey_path�auth_key_fnZpermissions_okr(r(r)�extract_authorized_keys�sF
r�c
Cs�t�}g}x$|D]}|j|jt|�|d��qWt|�\}}tjj|�}tj	|dd�� t
||�}	tj||	dd�WdQRXdS)N)r$T)r�)�
preserve_mode)r4r-rIr]r�rKrLr�rr�rer�)
rcrjr$rVZkey_entriesr[r�Zauth_key_entriesr��contentr(r(r)�setup_user_keys�s

r�c@s*eZdZddd�Zedd��Zdd�ZdS)	�SshdConfigLineNcCs||_||_||_dS)N)rH�_keyrq)r'rHr[�vr(r(r)r*�szSshdConfigLine.__init__cCs|jdkrdS|jj�S)N)r��lower)r'r(r(r)rd�s
zSshdConfigLine.keycCs>|jdkrt|j�St|j�}|jr6|dt|j�7}|SdS)Nr,)r�r]rHrq)r'r�r(r(r)r0�s


zSshdConfigLine.__str__)NN)r1r2r3r*�propertyrdr0r(r(r(r)r��s
r�)�returncCs"tjj|�sgSttj|�j��S)N)rKrLrM�parse_ssh_config_linesrrNrO)rXr(r(r)�parse_ssh_config�sr�cCs�g}x�|D]�}|j�}|s&|jd�r6|jt|��q
y|jdd�\}}WnLtk
r�y|jdd�\}}Wn"tk
r�tjd|�w
YnXYnX|jt|||��q
W|S)Nr>r6�=z;sshd_config: option "%s" has no key/value pair, skipping it)rGrFr-r�rA�
ValueErrorrSrx)rU�retrHrd�valr(r(r)r��s$
r�cCs:t|�}|siSi}x |D]}|js&q|j||j<qW|S)N)r�rdrq)rXrUr�rHr(r(r)r�s
r�)rXr�cCsdS)NFr()rXr(r(r)�_includes_dconf%sr�cCs^t|�rZtjj|�d��s.tj|�d�dd�tjj|�d�d�}tjj|�sZtj|d�|S)Nz.di�)r~z50-cloud-init.confi�)	r�rKrLr�rZ
ensure_dirr.rMZensure_file)rXr(r(r)�"_ensure_cloud_init_ssh_config_file/sr�cCsPt|�}t|�}t||d�}|rDtj|djdd�|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.)rU�updatesr_cSsg|]}t|��qSr()r])rZrHr(r(r)r\Esz%update_ssh_config.<locals>.<listcomp>T)r�r)r�r��update_ssh_config_linesrr�r.r8)r�rXrU�changedr(r(r)�update_ssh_config:sr�c	Cst�}g}tdd�|j�D��}x�t|dd�D]v\}}|js>q.|j|kr.||j}||}|j|�|j|kr�tjd|||�q.|j	|�tjd|||j|�||_q.Wt
|�t
|�k�r
xN|j�D]B\}}||kr�q�|j	|�|j	td||��tjdt
|�||�q�W|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|]}|j�|f�qSr()r�)rZr[r(r(r)r\Usz+update_ssh_config_lines.<locals>.<listcomp>r6)�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�dictrc�	enumeraterd�addrqrSrxr-r8�itemsr�)	rUr��foundr�Zcasemapr;rHrdrqr(r(r)r�Ks<






r�)rUcCs>|sdSt|�}dd�|D�}tj|dj|�dddd�dS)Ncss |]\}}|�d|��VqdS)r,Nr()rZr[r�r(r(r)�	<genexpr>}sz$append_ssh_config.<locals>.<genexpr>r_ZabT)Zomoder�)r�rr�r.)rUrXr�r(r(r)�append_ssh_configysr�cCspd}ttj�� tjddgddgd�\}}WdQRXd}x2|jd	�D]$}|j|�rD|t|�|jd
��SqDWdS)z�Get the full version of the OpenSSH sshd daemon on the system.

    On an ubuntu system, this would look something like:
    1.2p1 Ubuntu-1ubuntu0.1

    If we can't find `sshd` or parse the version number, return None.
    r?Zsshdz-Vrr6)�rcsNZOpenSSH_r_�,)rrZProcessExecutionErrorrArFr8�find)�err�_�prefixrHr(r(r)�get_opensshd_version�s
$
r�cCs�d}t�}|dkrtjj|�Sd|kr:|d|jd��}n d|krV|d|jd��}n|}ytjj|�}|Sttfk
r�tjd|�YnXdS)z�Get the upstream version of the OpenSSH sshd dameon on the system.

    This will NOT include the portable number, so if the Ubuntu version looks
    like `1.2p1 Ubuntu-1ubuntu0.1`, then this function would return
    `1.2`
    z9.0N�pr,z Could not parse sshd version: %s)	r�rZVersionZfrom_strr�r�rBrSZwarning)Zupstream_versionZfull_versionr(r(r)�get_opensshd_upstream_version�sr�)r	r
rrr
rrrrrrrrrrrrrrrrr)N)+rKrf�
contextlibrZtypingrrrZ	cloudinitrZloggingrrZ	getLoggerr1rSr�rCZ_DISABLE_USER_SSH_EXITr]ZDISABLE_USER_OPTSr!r4rYrerlrsr}r�r�r�r�r�r�r��boolr�r�r�r�r�r�r�r(r(r(r)�<module>	sh
YEO9

.