a }|gxt@sdZddlmZddlmZddlZddlZddlZddlm Z ddlm Z ddlm Z ddlm Z zd?d@Z-eed3dAdBZ.eedd dCdDZ/d^e ee ee dEdFdGZ0ee ee eeffdHdIdJZ1eedd+dKdLZ2eedd+dMdNZ3e eefedOdPdQZ4eeed dRdSZ5e e edTdUdVZ6e dWdXdYZ7dS)_z;Compat module to handle files security on Windows and Linux)absolute_import)contextmanagerN)Any)Dict) Generator)OptionalTFc@seZdZdZddddZdS) _WindowsUmaskz+Store the current umask to apply on WindowsNreturncCs d|_dS)N)mask)selfr=/usr/lib/python3.9/site-packages/certbot/compat/filesystem.py__init__"sz_WindowsUmask.__init__)__name__ __module__ __qualname____doc__rrrrrr sr) file_pathmoder cCs 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_MODEoschmod_apply_win_moderrrrrr)sr)r r cCstrt|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. )rrumask_WINDOWS_UMASKr )r Zprevious_umaskrrrr>s  r)NNNccs>d}z"t|}dVW|dur:t|n|dur8t|0dS)z Apply a umask temporarily, meant to be used in a `with` block. Uses the Certbot implementation of umask. :param int mask: The user file-creation mode mask to apply temporarily N)r)r Z old_umaskrrr temp_umaskOs r)srcdstr copy_user copy_groupr cCsVtr: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)rrstatst_uidst_gidchown_copy_win_ownershipr)rr rr!r"statsuser_idgroup_idrrrcopy_ownership_and_apply_modeis   r,)rr r!r"r cCsbtrFt|}|r|jnd}|r&|jnd}t|||t||jn|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) r#N) rrr$r%r&r'rst_moder(_copy_win_mode)rr r!r"r)r*r+rrrcopy_ownership_and_modes   r/cCs$trtt|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_modes r2)rr cCs8trt|jtkSt|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. ) rrr$r%getuid win32securityGetFileSecurityOWNER_SECURITY_INFORMATIONGetSecurityDescriptorOwner_get_current_user)rsecurityuserrrr check_owners r;cCst|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;r2rrrrcheck_permissionss r<)rflagsrr c CsNtrt|||S|tj@r4|tj@r.tjntj}t }|j }t }t ||t j}||d|d|dd}zz$t|tjtjtj@||dd}Wn^tjy} zD| jtjkrttj| j| jtjkrttj| j| WYd} ~ n d} ~ 00W|r| n|r| 0t||tjAtjASt||} 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. rN)"rropenO_CREATO_EXCLwin32conZ CREATE_NEWZ CREATE_ALWAYSr4SECURITY_ATTRIBUTESSECURITY_DESCRIPTORr8_generate_daclrr SetSecurityDescriptorOwnerSetSecurityDescriptorDacl win32fileZ CreateFileZ GENERIC_READZFILE_SHARE_READZFILE_SHARE_WRITE pywintypeserrorwinerrorZERROR_FILE_EXISTSOSErrorerrnoEEXISTstrerrorZERROR_SHARING_VIOLATIONZEACCESZCloser) rr>rZ disposition attributesr9r:daclZhandleerrfdrrrr@s<          r@cCstd}zjt|d|ABtr4t||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`` rr=N)rrrmakedirsmkdir)rrZ current_umaskZ orig_mkdir_fnrrrrUs"    rUc Cstrt||St}|j}t}t||tj }| |d| d|dzt ||WnJtjy}z0|jtjkrttj|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`` Fr?rN)rrrVr4rDrEr8rFrr rGrHrIZCreateDirectoryrJrKrLZERROR_ALREADY_EXISTSrMrNrOrP)rrrQr9r:rRrSrrrrV8s    rV)rr r cCs,ttdrttd||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)hasattrrgetattrrename)rr rrrrWWs rWcCs.|}tj|}tj|r*td||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!)rpathrealpathislink RuntimeErrorformat)rZ original_pathr[rrrr\hs   r\) link_pathr cCs6t|}tr|St|dkr*|ddStddS)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 iNz3Long paths are not supported by Certbot on Windows.)rreadlinkrlen ValueError)r`r[rrrrb}s   rb)r[r cCs&trtj|ot|tjSt|S)z Is path an executable file? :param str path: path to test :return: True if path is an executable file :rtype: bool )rrr[isfileaccessX_OK_win_is_executable)r[rrr is_executablesricCsVtr ttt|jtj@St|tj }| }t| tj tj tddS)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-0Z TrusteeFormZ TrusteeTypeZ Identifier)rboolr$r0rr-S_IRWXOr4r5DACL_SECURITY_INFORMATIONGetSecurityDescriptorDaclGetEffectiveRightsFromAclTRUSTEE_IS_SIDTRUSTEE_IS_USERConvertStringSidToSid)r[r9rRrrrhas_world_permissionssrt)old_key base_moder cCs:tr6tt|jtjtjBtjBtjB@}||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$r0rr-S_IRGRPS_IWGRPS_IXGRPS_IROTH)rurvZold_moderrrcompute_private_key_modes r{)path1path2r 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 ) rrr$r%r&r4r5r6r7)r|r}Zstats1Zstats2Z security1Zuser1Z security2Zuser2rrrhas_same_ownerships   r~)r[min_moder c Cstrt|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 r?rkFT)rrr$r-r\r4r5r6rnr7rorFrange GetAceCountGetAcerprqrr) r[rr-r9r:rRZmin_daclindexZmin_acer Zeffective_maskrrrhas_min_permissionss,       rcCsNtj|sdSt|tj}|}|tjtj t d}|t j @t j kS)NFrk) rr[rer4r5rnrorprqrrr8 ntsecurityconFILE_GENERIC_EXECUTE)r[r9rRrrrrrhs rhcCsJt|}t|tj}|}t||}|d|dt|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. r?rN) r\r4r5r6r7rFrHSetFileSecurityrn)rrr9r:rRrrrr-s  r)user_sidrr r c Cs|r|d|@}t|}td}td}td}t}|||fvrjt|d}|rj|tj||t|d} | r|tj| |tdddd} |tj| ||tj| ||S) Nr=zS-1-5-18z S-1-5-32-544rjr:allTreadwriteexecute) _analyze_moder4rsZACL_generate_windows_flagsZAddAccessAllowedAceZ ACL_REVISION) rrr ZanalysissystemZadminsZeveryonerRZ user_flagsZeverybody_flagsZfull_permissionsrrrrF@s$       rF)rr cCs>|tj@|tj@|tj@d|tj@|tj@|tj@ddS)Nr)r:r)r$S_IRUSRS_IWUSRS_IXUSRrzS_IWOTHS_IXOTH)rrrrrdsrcCsLt|}t|tj}|}t|tj}||dt|tj|dSNF)r\r4r5r6r7rGr)rr security_srcZuser_src security_dstrrrr(ss  r(cCsNt|}t|tj}|}t|tj}|d|dt|tj|dS)Nr?r)r\r4r5rnrorHr)rr rrRrrrrr.s r.) rights_descr cCsJd}|dr|tjB}|dr4|tjtjAtjAB}|drF|tjB}|S)Nrrrr)rZFILE_GENERIC_READZFILE_ALL_ACCESSr)rflagrrrrs  rcCsHt|}t|tjtjB}|}|}|s4dSt||}t||Sr) r\r4r5r6rnror7rF_compare_dacls)rrr9rRr:Zref_daclrrrr1s  r1)dacl1dacl2r cs4fddtDfddtDkS)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|]}|qSrr.0r)rrr z"_compare_dacls..csg|]}|qSrrr)rrrrr)rrrrrrrrsrr cCs$dtt}td|dS)z= Return the pySID corresponding to the current user. z{0}\{1}Nr)r_win32apiZ GetDomainNameZ GetUserNamer4ZLookupAccountName)Z account_namerrrr8sr8)TT)r=)r=)r=)N)8rZ __future__r contextlibrrNrr$typingrrrrrrJrrCrIr4rL ImportErrorrrrstrintrrrrlr,r/r2r;r<r@rUrVrWr\rbrirtr{r~rrhrrFrr(r.rr1rr8rrrrsr          "  E "-$