ok

Mini Shell

Direktori : /lib/python3.6/site-packages/certbot/compat/__pycache__/
Upload File :
Current File : //lib/python3.6/site-packages/certbot/compat/__pycache__/filesystem.cpython-36.pyc

3

گau�@s�dZddlmZddlZddlZddlZddlZddlmZddlm	Z	ddlm
Z
ddlmZy<ddlZddl
Z
ddlZddlZddlZddlZddlZWnek
r�dZYnXd	ZGd
d�d�Ze�Zeedd�d
d�Zeed�dd�Zeeeeedd�dd�ZdVeeeedd�dd�Zeeed�dd�Zeed�dd�Zeeed�dd�Z dWeeeed �d!d"�Z!dXeedd�d#d$�Z"dYeedd�d%d&�Z#eedd'�d(d)�Z$eed�d*d+�Z%eed,�d-d.�Z&eed/�d0d1�Z'eed/�d2d3�Z(eeed4�d5d6�Z)eeed7�d8d9�Z*eeed:�d;d<�Z+eed/�d=d>�Z,eedd�d?d@�Z-dZeeeeedA�dBdC�Z.ee	ee	eeffdD�dEdF�Z/eedd'�dGdH�Z0eedd'�dIdJ�Z1e	eefedK�dLdM�Z2eeed�dNdO�Z3eeedP�dQdR�Z4edS�dTdU�Z5dS)[z;Compat module to handle files security on Windows and Linux�)�absolute_importN)�Any)�Dict)�List)�OptionalTFc@seZdZdZdd�dd�ZdS)�
_WindowsUmaskz+Store the current umask to apply on WindowsN)�returncCs
d|_dS)N�)�mask)�self�r� /usr/lib/python3.6/filesystem.py�__init__"sz_WindowsUmask.__init__)�__name__�
__module__�__qualname__�__doc__rrrrr
r sr)�	file_path�modercCs trtj||�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)rrrrr
r)sr)r
rcCstrtj|�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
)r
Zprevious_umaskrrr
r>s
	
r)�src�dstr�	copy_user�
copy_grouprcCsVtr:tj|�}|r|jnd}|r&|jnd}tj|||�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���r )rr�stat�st_uid�st_gid�chown�_copy_win_ownershipr)rrrrr�stats�user_id�group_idrrr
�copy_ownership_and_apply_modeXs


r))rrrrrcCsbtrFtj|�}|r|jnd}|r&|jnd}tj|||�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)
    rNr r )
rrr!r"r#r$r�st_moder%�_copy_win_mode)rrrrr&r'r(rrr
�copy_ownership_and_modeys

r,cCs$trtjtj|�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_mode)rrrrr
�
check_mode�sr/)rrcCs8trtj|�jtj�kStj|tj�}|j�}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.
    )
rrr!r"�getuid�
win32security�GetFileSecurity�OWNER_SECURITY_INFORMATION�GetSecurityDescriptorOwner�_get_current_user)r�security�userrrr
�check_owner�s
r8cCst|�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.
    )r8r/)rrrrr
�check_permissions�s	r9�)r�flagsrrc
Cs>trtj|||�S|tj@�r$|tj@r.tjntj}tj	�}|j
}t�}t||t
j�}|j|d�|jd|d�d}z�y$tj|tjtjtj@||dd�}Wn\tjk
r�}	z>|	jtjkr�ttj|	j��|	jtjkr�ttj|	j��|	�WYdd}	~	XnXWd|�r
|j �Xtj||tjAtjA�Stj||�}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.
    rrN)"rr�open�O_CREAT�O_EXCL�win32conZ
CREATE_NEWZ
CREATE_ALWAYSr1�SECURITY_ATTRIBUTES�SECURITY_DESCRIPTORr5�_generate_daclrr
�SetSecurityDescriptorOwner�SetSecurityDescriptorDacl�	win32fileZ
CreateFileZGENERIC_READZFILE_SHARE_READZFILE_SHARE_WRITE�
pywintypes�error�winerrorZERROR_FILE_EXISTS�OSError�errno�EEXIST�strerrorZERROR_SHARING_VIOLATIONZEACCESZCloser)
rr;rZdisposition�
attributesr6r7�daclZhandle�errrrr
r<�s6



r<cCs^td�}zFt|d|AB�tr*tj||�Stj}ztt_tj||�S|t_XWdt|�XdS)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``
    ri�N)rrr�makedirs�mkdir)rrZ
current_umaskZ
orig_mkdir_fnrrr
rPs	rPcCs�trtj||�Stj�}|j}t�}t||tj	�}|j
|d�|jd|d�ytj
||�WnHtjk
r�}z*|jtjkr�ttj|j||j��|�WYdd}~XnXdS)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``
    FrrN)rrrQr1r@rAr5rBrr
rCrDrEZCreateDirectoryrFrGrHZERROR_ALREADY_EXISTSrIrJrKrL)rrrMr6r7rNrOrrr
rQ's	rQ)rrrcCs,ttd�rttd�||�ntj||�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)rrrrr
rRFs
rRcCs�|}tstjdkr<tjj|�}tjj|�r8tdj|���|Sg}x`tjj|�r�|}tj	|�}tjj
|�s~tjjtjj|�|�}||kr�tdj|���|j
|�qBWtjj|�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!)rVrW)r�sys�version_infor�path�realpath�islink�RuntimeError�format�readlink�isabs�join�dirname�append�abspath)rZ
original_pathrZZinspected_paths�	link_pathrrr
r[Ws 	
r[)rercCsBtj|�}ts|jd�r|St|�dkr6|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)rerZrrr
r_ys	
	r_)rZrcCs&trtjj|�otj|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
    )rrrZ�isfile�access�X_OK�_win_is_executable)rZrrr
�
is_executable�srncCsVtr ttjtj|�j�tj@�Stj|tj	�}|j
�}t|jtjtj
tjd�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
    zS-1-1-0)�TrusteeForm�TrusteeType�
Identifier)r�boolr!r-rr*�S_IRWXOr1r2�DACL_SECURITY_INFORMATION�GetSecurityDescriptorDacl�GetEffectiveRightsFromAcl�TRUSTEE_IS_SID�TRUSTEE_IS_USER�ConvertStringSidToSid)rZr6rNrrr
�has_world_permissions�srz)�old_key�	base_modercCs:tr6tjtj|�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
    )	rr!r-rr*�S_IRGRP�S_IWGRP�S_IXGRP�S_IROTH)r{r|Zold_moderrr
�compute_private_key_mode�s
	r�)�path1�path2rcCsdtr0tj|�}tj|�}|j|jf|j|jfkStj|tj�}|j�}tj|tj�}|j�}||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

    )	rrr!r"r#r1r2r3r4)r�r�Zstats1Zstats2Z	security1Zuser1Z	security2Zuser2rrr
�has_same_ownership�s

r�)rZ�min_modercCs�trtj|�j}|||BkSt|�}tj|tjtjB�}|j	�}|j
�}t||�}xTt|j
��D]D}|j|�}|d}	|d}|jtjtj|d��}
|
|
|	Bkr`dSq`WdS)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
    r�)rorprqFT)rrr!r*r[r1r2r3rtr4rurB�range�GetAceCount�GetAcervrwrx)rZr�r*r6r7rNZmin_dacl�indexZmin_acer
Zeffective_maskrrr
�has_min_permissions�s(



r�cCsNtjj|�sdStj|tj�}|j�}|jtjtj	t
�d��}|tj@tjkS)NF)rorprq)
rrZrjr1r2rtrurvrwrxr5�
ntsecuritycon�FILE_GENERIC_EXECUTE)rZr6rNrrrr
rmsrmcCsJt|�}tj|tj�}|j�}t||�}|jd|d�tj|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.
    rrN)	r[r1r2r3r4rBrD�SetFileSecurityrt)rrr6r7rNrrr
r)s
r)�user_sidrr
rcCs�|r|d|@}t|�}tjd�}tjd�}tjd�}tj�}|||gkrjt|d�}|rj|jtj||�t|d�}	|	r�|jtj|	|�tdddd��}
|jtj|
|�|jtj|
|�|S)	Ni�zS-1-5-18zS-1-5-32-544zS-1-1-0r7�allT)�read�write�execute)�
_analyze_moder1ryZACL�_generate_windows_flagsZAddAccessAllowedAceZACL_REVISION)r�rr
Zanalysis�systemZadminsZeveryonerNZ
user_flagsZeverybody_flagsZfull_permissionsrrr
rB<s$


rB)rrcCs>|tj@|tj@|tj@d�|tj@|tj@|tj@d�d�S)N)r�r�r�)r7r�)r!�S_IRUSR�S_IWUSR�S_IXUSRr��S_IWOTH�S_IXOTH)rrrr
r�`sr�cCsLt|�}tj|tj�}|j�}tj|tj�}|j|d�tj|tj|�dS)NF)r[r1r2r3r4rCr�)rr�security_srcZuser_src�security_dstrrr
r%osr%cCsNt|�}tj|tj�}|j�}tj|tj�}|jd|d�tj|tj|�dS)Nrr)r[r1r2rtrurDr�)rrr�rNr�rrr
r+~sr+)�rights_descrcCsJd}|dr|tjB}|dr4|tjtjAtjAB}|drF|tjB}|S)Nrr�r�r�)r�ZFILE_GENERIC_READZFILE_ALL_ACCESSr�)r��flagrrr
r��s

r�cCsHt|�}tj|tjtjB�}|j�}|j�}|s4dSt||�}t||�S)NF)	r[r1r2r3rtrur4rB�_compare_dacls)rrr6rNr7Zref_daclrrr
r.�s


r.)�dacl1�dacl2rcs4�fdd�t�j��D��fdd�t�j��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|]}�j|��qSr)r�)�.0r�)r�rr
�
<listcomp>�sz"_compare_dacls.<locals>.<listcomp>csg|]}�j|��qSr)r�)r�r�)r�rr
r��s)r�r�)r�r�r)r�r�r
r��sr�)rcCs$djtj�tj��}tjd|�dS)z=
    Return the pySID corresponding to the current user.
    z{0}\{1}Nr)r^�win32apiZ
GetDomainNameZGetUserNamer1ZLookupAccountName)Zaccount_namerrr
r5�sr5)TT)r:)r:)r:)N)6rZ
__future__rrJrr!rXZtypingrrrrr�rFr�r?rEr1rH�ImportErrorrrr�str�intrrrrr)r,r/r8r9r<rPrQrRr[r_rnrzr�r�r�rmrrBr�r%r+r�r.r�r5rrrr
�<module>sf
!E ""-$ 
	

Zerion Mini Shell 1.0