| Current Path : /lib/python3/dist-packages/certbot/compat/__pycache__/ |
| Current File : //lib/python3/dist-packages/certbot/compat/__pycache__/filesystem.cpython-38.pyc |
U
�]9_ � @ s` d Z ddlmZ ddlZddlZddlZz<ddlZddlZddlZddl Z ddl
Z
ddlZddlZW n e
k
r| dZY nX dZddlmZmZmZ dd� Zd d
� Zdd� Zd
d� Zdd� Zd8dd�Zd9dd�Zd:dd�Zdd� Zdd� Zdd� 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'd2d3� Z(d4d5� Z)d6d7� Z*dS );z;Compat module to handle files security on Windows and Linux� )�absolute_importNTF)�List�Union�Tuplec C s t rt�| |� 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�mode� r
�;/usr/lib/python3/dist-packages/certbot/compat/filesystem.pyr s r c C sV t r: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)r r �stat�st_uid�st_gid�chown�_copy_win_ownershipr )�src�dstr Z copy_userZ
copy_groupZstatsZuser_idZgroup_idr
r
r �copy_ownership_and_apply_mode8 s
r c C s$ t rt�t�| �j�|kS t| |�S )a`
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
)r r �S_IMODEr �st_mode�_check_win_moder
r
r
r �
check_modeP s r c C s8 t rt�| �jt�� kS t�| 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.
)
r r r r �getuid�
win32security�GetFileSecurity�OWNER_SECURITY_INFORMATION�GetSecurityDescriptorOwner�_get_current_user)r �security�userr
r
r �check_ownera s
r$ c C s t | �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.
)r$ r r
r
r
r �check_permissionst s r% � c
C s: t rt�| ||�S |tj@ �r |tj@ r.tjntj}t� � }|j
}t� }t||�}|�
|d� |�d|d� d}z�z$t�| tjtjtj@ ||dd�}W n\ tjk
r� } z<| jtjkr�ttj| j��| jtjkr�ttj| j��| �W 5 d} ~ X Y nX W 5 |�r|�� X t�| |tjA tjA �S t�| |�}t| |� |S )az
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) r r �open�O_CREAT�O_EXCL�win32conZ
CREATE_NEWZ
CREATE_ALWAYSr �SECURITY_ATTRIBUTES�SECURITY_DESCRIPTORr! �_generate_dacl�SetSecurityDescriptorOwner�SetSecurityDescriptorDaclZClose� win32fileZ
CreateFileZGENERIC_READZFILE_SHARE_READZFILE_SHARE_WRITE�
pywintypes�error�winerrorZERROR_FILE_EXISTS�OSError�errno�EEXIST�strerrorZERROR_SHARING_VIOLATIONZEACCESr )
r �flagsr Zdisposition�
attributesr"