HEX
Server: Apache/2.4.65 (Debian)
System: Linux kubikelcreative 5.10.0-35-amd64 #1 SMP Debian 5.10.237-1 (2025-05-19) x86_64
User: www-data (33)
PHP: 8.4.13
Disabled: NONE
Upload Files
File: //usr/lib/python3/dist-packages/certbot/compat/__pycache__/filesystem.cpython-39.pyc
a

�`�u�@s�dZddlmZddlZddlZddlZddlZddlmZz<ddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZWney�dZYn0dZGdd�d�Ze�Zd	d
�Zdd�Zd
d�ZdBdd�Zdd�Zdd�Zdd�ZdCdd�ZdDdd�ZdEdd�Zdd�Zd d!�Zd"d#�Z d$d%�Z!d&d'�Z"d(d)�Z#d*d+�Z$d,d-�Z%d.d/�Z&d0d1�Z'dFd2d3�Z(d4d5�Z)d6d7�Z*d8d9�Z+d:d;�Z,d<d=�Z-d>d?�Z.d@dA�Z/dS)Gz;Compat module to handle files security on Windows and Linux�)�absolute_importN)�ListTFc@seZdZdZdd�ZdS)�
_WindowsUmaskz+Store the current umask to apply on WindowscCs
d|_dS)N�)�mask)�self�r�;/usr/lib/python3/dist-packages/certbot/compat/filesystem.py�__init__ sz_WindowsUmask.__init__N)�__name__�
__module__�__qualname__�__doc__r
rrrr	rsrcCs trt�||�n
t||�dS)a[
    Apply a POSIX mode on given file_path:

      - for Linux, the POSIX mode will be directly applied using chmod,
      - for Windows, the POSIX mode will be translated into a Windows DACL that make sense for
        Certbot context, and applied to the file using kernel calls.

    The definition of the Windows DACL that correspond to a POSIX mode, in the context of Certbot,
    is explained at https://github.com/certbot/certbot/issues/6356 and is implemented by the
    method `_generate_windows_flags()`.

    :param str file_path: Path of the file
    :param int mode: POSIX mode to apply
    N)�
POSIX_MODE�os�chmod�_apply_win_mode��	file_path�moderrr	r'srcCstrt�|�Stj}|t_|S)a$
    Set the current numeric umask and return the previous umask. On Linux, the built-in umask
    method is used. On Windows, our Certbot-side implementation is used.

    :param int mask: The user file-creation mode mask to apply.
    :rtype: int
    :return: The previous umask value.
    )rr�umask�_WINDOWS_UMASKr)rZprevious_umaskrrr	r=s


rcCsVtr:t�|�}|r|jnd}|r&|jnd}t�|||�n|rHt||�t||�dS)a�
    Copy ownership (user and optionally group on Linux) from the source to the
    destination, then apply given mode in compatible way for Linux and Windows.
    This replaces the os.chown command.

    :param str src: Path of the source file
    :param str dst: Path of the destination file
    :param int mode: Permission mode to apply on the destination file
    :param bool copy_user: Copy user if `True`
    :param bool copy_group: Copy group if `True` on Linux (has no effect on Windows)
    ���N)rr�stat�st_uid�st_gid�chown�_copy_win_ownershipr)�src�dstr�	copy_user�
copy_group�stats�user_id�group_idrrr	�copy_ownership_and_apply_modeXs


r%cCsbtrFt�|�}|r|jnd}|r&|jnd}t�|||�t||j�n|rTt||�t	||�dS)aU
    Copy ownership (user and optionally group on Linux) and mode/DACL
    from the source to the destination.

    :param str src: Path of the source file
    :param str dst: Path of the destination file
    :param bool copy_user: Copy user if `True`
    :param bool copy_group: Copy group if `True` on Linux (has no effect on Windows)
    rN)
rrrrrrr�st_moder�_copy_win_mode)rrr r!r"r#r$rrr	�copy_ownership_and_modeys

r(cCs$trt�t�|�j�|kSt||�S)aa
    Check if the given mode matches the permissions of the given file.
    On Linux, will make a direct comparison, on Windows, mode will be compared against
    the security model.

    :param str file_path: Path of the file
    :param int mode: POSIX mode to test
    :rtype: bool
    :return: True if the POSIX mode matches the file permissions
    )rr�S_IMODErr&�_check_win_moderrrr	�
check_mode�sr+cCs8trt�|�jt��kSt�|tj�}|��}t	�|kS)z�
    Check if given file is owned by current user.

    :param str file_path: File path to check
    :rtype: bool
    :return: True if given file is owned by current user, False otherwise.
    )
rrrr�getuid�
win32security�GetFileSecurity�OWNER_SECURITY_INFORMATION�GetSecurityDescriptorOwner�_get_current_user)r�security�userrrr	�check_owner�s
	r4cCst|�ot||�S)z�
    Check if given file has the given mode and is owned by current user.

    :param str file_path: File path to check
    :param int mode: POSIX mode to check
    :rtype: bool
    :return: True if file has correct mode and owner, False otherwise.
    )r4r+rrrr	�check_permissions�s
r5�c

CsNtrt�|||�S|tj@�r4|tj@r.tjntj}t�	�}|j
}t�}t||t
j�}|�|d�|�d|d�d}z�z$t�|tjtjtj@||dd�}Wn^tjy�}	zD|	jtjkr�ttj|	j��|	jtjkr�ttj|	j��|	�WYd}	~	n
d}	~	00W|�r|� �n|�r|� �0t�||tjAtjA�St�||�}t!||�|S)aw
    Wrapper of original os.open function, that will ensure on Windows that given mode
    is correctly applied.

    :param str file_path: The file path to open
    :param int flags: Flags to apply on file while opened
    :param int mode: POSIX mode to apply on file when opened,
        Python defaults will be applied if ``None``
    :returns: the file descriptor to the opened file
    :rtype: int
    :raise: OSError(errno.EEXIST) if the file already exists and os.O_CREAT & os.O_EXCL are set,
            OSError(errno.EACCES) on Windows if the file already exists and is a directory, and
            os.O_CREAT is set.
    r�N)"rr�open�O_CREAT�O_EXCL�win32conZ
CREATE_NEWZ
CREATE_ALWAYSr-�SECURITY_ATTRIBUTES�SECURITY_DESCRIPTORr1�_generate_daclrr�SetSecurityDescriptorOwner�SetSecurityDescriptorDacl�	win32fileZ
CreateFileZGENERIC_READZFILE_SHARE_READZFILE_SHARE_WRITE�
pywintypes�error�winerrorZERROR_FILE_EXISTS�OSError�errno�EEXIST�strerrorZERROR_SHARING_VIOLATIONZEACCESZCloser)
r�flagsrZdisposition�
attributesr2r3�daclZhandle�errrrr	r8�s<

�
�

r8cCs�td�}zjt|d|AB�tr4t�||�Wt|�Stj}z$tt_t�||�W|t_Wt|�S|t_0Wt|�n
t|�0dS)a4
    Rewrite of original os.makedirs function, that will ensure on Windows that given mode
    is correctly applied.

    :param str file_path: The file path to open
    :param int mode: POSIX mode to apply on leaf directory when created, Python defaults
                     will be applied if ``None``
    rr6N)rrr�makedirs�mkdir)rrZ
current_umaskZ
orig_mkdir_fnrrr	rMs"
���
rMc
Cs�trt�||�St��}|j}t�}t||tj	�}|�
|d�|�d|d�zt�
||�WnJtjy�}z0|jtjkr�ttj|j||j��|�WYd}~n
d}~00dS)a,
    Rewrite of original os.mkdir function, that will ensure on Windows that given mode
    is correctly applied.

    :param str file_path: The file path to open
    :param int mode: POSIX mode to apply on directory when created, Python defaults
                     will be applied if ``None``
    Fr7rN)rrrNr-r<r=r1r>rrr?r@rAZCreateDirectoryrBrCrDZERROR_ALREADY_EXISTSrErFrGrH)rrrJr2r3rKrLrrr	rN/s
rNcCs,ttd�rttd�||�nt�||�dS)z�
    Rename a file to a destination path and handles situations where the destination exists.

    :param str src: The current file path.
    :param str dst: The new file path.
    �replaceN)�hasattrr�getattr�rename)rrrrr	rOOs
rOcCs�|}tstjdkr<tj�|�}tj�|�r8td�|���|Sg}tj�|�r�|}t�	|�}tj�
|�s|tj�tj�|�|�}||vr�td�|���|�
|�q@tj�|�S)a 
    Find the real path for the given path. This method resolves symlinks, including
    recursive symlinks, and is protected against symlinks that creates an infinite loop.

    :param str file_path: The path to resolve
    :returns: The real path for the given path
    :rtype: str
    )��zError, link {0} is a loop!)r�sys�version_infor�path�realpath�islink�RuntimeError�format�readlink�isabs�join�dirname�append�abspath)rZ
original_pathrWZinspected_paths�	link_pathrrr	rXas 

rXcCs@t�|�}ts|�d�s|St|�dkr4|dd�Std��dS)a
    Return a string representing the path to which the symbolic link points.

    :param str link_path: The symlink path to resolve
    :return: The path the symlink points to
    :returns: str
    :raise: ValueError if a long path (260> characters) is encountered on Windows
    z\\?\i�Nz3Long paths are not supported by Certbot on Windows.)rr\r�
startswith�len�
ValueError)rbrWrrr	r\�s

	r\cCs&trtj�|�ot�|tj�St|�S)z�
    Is path an executable file?

    :param str path: path to test
    :return: True if path is an executable file
    :rtype: bool
    )rrrW�isfile�access�X_OK�_win_is_executable)rWrrr	�
is_executable�s	rkcCsVtr tt�t�|�j�tj@�St�|tj	�}|�
�}t|�tjtj
t�d�d���S)z�
    Check if everybody/world has any right (read/write/execute) on a file given its path.

    :param str path: path to test
    :return: True if everybody/world has any right to the file
    :rtype: bool
    �S-1-1-0�ZTrusteeFormZTrusteeTypeZ
Identifier)r�boolrr)rr&�S_IRWXOr-r.�DACL_SECURITY_INFORMATION�GetSecurityDescriptorDacl�GetEffectiveRightsFromAcl�TRUSTEE_IS_SID�TRUSTEE_IS_USER�ConvertStringSidToSid)rWr2rKrrr	�has_world_permissions�s	�rvcCs:tr6t�t�|�j�tjtjBtjBtjB@}||BS|S)a
    Calculate the POSIX mode to apply to a private key given the previous private key.

    :param str old_key: path to the previous private key
    :param int base_mode: the minimum modes to apply to a private key
    :return: the POSIX mode to apply
    :rtype: int
    )	rrr)rr&�S_IRGRP�S_IWGRP�S_IXGRP�S_IROTH)Zold_keyZ	base_modeZold_moderrr	�compute_private_key_mode�s
�r{cCsdtr0t�|�}t�|�}|j|jf|j|jfkSt�|tj�}|��}t�|tj�}|��}||kS)as
    Return True if the ownership of two files given their respective path is the same.
    On Windows, ownership is checked against owner only, since files do not have a group owner.

    :param str path1: path to the first file
    :param str path2: path to the second file
    :return: True if both files have the same ownership, False otherwise
    :rtype: bool

    )	rrrrrr-r.r/r0)Zpath1Zpath2Zstats1Zstats2Z	security1Zuser1Z	security2Zuser2rrr	�has_same_ownership�s

r|cCs�trt�|�j}|||BkSt|�}t�|tjtjB�}|�	�}|�
�}t||�}t|�
��D]F}|�|�}|d}	|d}|�tjtj|d��}
|
|
|	Bkr^dSq^dS)a�
    Check if a file given its path has at least the permissions defined by the given minimal mode.
    On Windows, group permissions are ignored since files do not have a group owner.

    :param str path: path to the file to check
    :param int min_mode: the minimal permissions expected
    :return: True if the file matches the minimal permissions expectations, False otherwise
    :rtype: bool
    r7�rmFT)rrrr&rXr-r.r/rpr0rqr>�range�GetAceCount�GetAcerrrsrt)rWZmin_moder&r2r3rKZmin_dacl�indexZmin_acerZeffective_maskrrr	�has_min_permissions�s,�

�r�cCsNtj�|�sdSt�|tj�}|��}|�tjtj	t
�d��}|tj@tjkS)NFrm)
rrWrgr-r.rprqrrrsrtr1�
ntsecuritycon�FILE_GENERIC_EXECUTE)rWr2rKrrrr	rj*s�rjcCsJt|�}t�|tj�}|��}t||�}|�d|d�t�|tj|�dS)z�
    This function converts the given POSIX mode into a Windows ACL list, and applies it to the
    file given its path. If the given path is a symbolic link, it will resolved to apply the
    mode on the targeted file.
    r7rN)	rXr-r.r/r0r>r@�SetFileSecurityrp)rrr2r3rKrrr	r:s
rcCs�|r|d|@}t|�}t�d�}t�d�}t�d�}t��}|||fvrjt|d�}|rj|�tj||�t|d�}	|	r�|�tj|	|�tdddd��}
|�tj|
|�|�tj|
|�|S)	Nr6zS-1-5-18zS-1-5-32-544rlr3�allT��read�write�execute)�
_analyze_moder-ruZACL�_generate_windows_flagsZAddAccessAllowedAceZACL_REVISION)Zuser_sidrrZanalysis�systemZadminsZeveryonerKZ
user_flagsZeverybody_flagsZfull_permissionsrrr	r>Ms$


r>cCs>|tj@|tj@|tj@d�|tj@|tj@|tj@d�d�S)Nr�)r3r�)r�S_IRUSR�S_IWUSR�S_IXUSRrz�S_IWOTH�S_IXOTH)rrrr	r�qs���r�cCsLt|�}t�|tj�}|��}t�|tj�}|�|d�t�|tj|�dS�NF)rXr-r.r/r0r?r�)rr�security_srcZuser_src�security_dstrrr	r�srcCsNt|�}t�|tj�}|��}t�|tj�}|�d|d�t�|tj|�dS)Nr7r)rXr-r.rprqr@r�)rrr�rKr�rrr	r'�sr'cCsJd}|dr|tjB}|dr4|tjtjAtjAB}|drF|tjB}|S)Nrr�r�r�)r�ZFILE_GENERIC_READZFILE_ALL_ACCESSr�)Zrights_desc�flagrrr	r��s
��
r�cCsHt|�}t�|tjtjB�}|��}|��}|s4dSt||�}t||�Sr�)	rXr-r.r/rprqr0r>�_compare_dacls)rrr2rKr3Zref_daclrrr	r*�s
�
r*cs4�fdd�t����D��fdd�t����D�kS)z�
    This method compare the two given DACLs to check if they are identical.
    Identical means here that they contains the same set of ACEs in the same order.
    csg|]}��|��qSr�r���.0r�)�dacl1rr	�
<listcomp>��z"_compare_dacls.<locals>.<listcomp>csg|]}��|��qSrr�r�)�dacl2rr	r��r�)r~r�r�r�rr�r	r��s�r�cCs$d�t��t���}t�d|�dS)z=
    Return the pySID corresponding to the current user.
    z{0}\{1}Nr)r[�win32apiZ
GetDomainNameZGetUserNamer-ZLookupAccountName)Zaccount_namerrr	r1�sr1)TT)r6)r6)r6)N)0rZ
__future__rrFrrrUZacme.magic_typingrr�r-r;r�rArBrD�ImportErrorrrrrrr%r(r+r4r5r8rMrNrOrXr\rkrvr{r|r�rjrr>r�rr'r�r*r�r1rrrr	�<module>s^
!

F
!
 ##.
$