KGRKJGETMRETU895U-589TY5MIGM5JGB5SDFESFREWTGR54TY
Server : Apache/2.4.41 (Ubuntu)
System : Linux vmi1525618.contaboserver.net 5.4.0-105-generic #119-Ubuntu SMP Mon Mar 7 18:49:24 UTC 2022 x86_64
User : www-data ( 33)
PHP Version : 8.2.12
Disable Function : NONE
Directory :  /lib/python3/dist-packages/certbot/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //lib/python3/dist-packages/certbot/__pycache__/reverter.cpython-38.pyc
U

�]�_�@s�dZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Zddlm
Z
ddlmZddlmZddlmZddlmZddlmZe�e�ZGd	d
�d
e�ZdS)zGReverter class saves configuration checkpoints and allows for recovery.�N)�	constants)�errors)�
interfaces)�util)�os)�
filesystemc@s�eZdZdZdd�Zdd�Zd*dd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)S)+�ReverterafReverter Class - save and revert configuration checkpoints.

    This class can be used by the plugins, especially Installers, to
    undo changes made to the user's system. Modifications to files and
    commands to do undo actions taken by the plugin should be registered
    with this class before the action is taken.

    Once a change has been registered with this class, there are three
    states the change can be in. First, the change can be a temporary
    change. This should be used for changes that will soon be reverted,
    such as config changes for the purpose of solving a challenge.
    Changes are added to this state through calls to
    :func:`~add_to_temp_checkpoint` and reverted when
    :func:`~revert_temporary_config` or :func:`~recovery_routine` is
    called.

    The second state a change can be in is in progress. These changes
    are not temporary, however, they also have not been finalized in a
    checkpoint. A change must become in progress before it can be
    finalized. Changes are added to this state through calls to
    :func:`~add_to_checkpoint` and reverted when
    :func:`~recovery_routine` is called.

    The last state a change can be in is finalized in a checkpoint. A
    change is put into this state by first becoming an in progress
    change and then calling :func:`~finalize_checkpoint`. Changes
    in this state can be reverted through calls to
    :func:`~rollback_checkpoints`.

    As a final note, creating new files and registering undo commands
    are handled specially and use the methods
    :func:`~register_file_creation` and :func:`~register_undo_command`
    respectively. Both of these methods can be used to create either
    temporary or in progress changes.

    .. note:: Consider moving everything over to CSV format.

    :param config: Configuration.
    :type config: :class:`certbot.interfaces.IConfig`

    cCs ||_t�|jtj|jj�dS)N)�configr�make_or_verify_dir�
backup_dirr�CONFIG_DIRS_MODE�strict_permissions)�selfr	�r�2/usr/lib/python3/dist-packages/certbot/reverter.py�__init__Bs�zReverter.__init__cCsXtj�|jj�rTz|�|jj�Wn0tjk
rRt�	d|jj�t�d��YnXdS)aReload users original configuration files after a temporary save.

        This function should reinstall the users original configuration files
        for all saves with temporary=True

        :raises .ReverterError: when unable to revert config

        z$Incomplete or failed recovery for %sz!Unable to revert temporary configN)
r�path�isdirr	�temp_checkpoint_dir�_recover_checkpointr�
ReverterError�logger�critical�rrrr�revert_temporary_configHs	�z Reverter.revert_temporary_config�cCszt|�}Wn(tk
r4t�d�t�d��YnX|dkrRt�d�t�d��t�|jj	�}|�
�|sxt�d�nt|�|kr�t�d|t|��|dkr�|r�tj
�|jj	|���}z|�|�Wn*tjk
r�t�d�t�d��YnX|d8}q�d	S)
ayRevert 'rollback' number of configuration checkpoints.

        :param int rollback: Number of checkpoints to reverse. A str num will be
           cast to an integer. So "2" is also acceptable.

        :raises .ReverterError:
            if there is a problem with the input or if the function is
            unable to correctly revert the configuration checkpoints

        z,Rollback argument must be a positive integerz
Invalid InputrzHCertbot hasn't modified your configuration, so rollback isn't available.z0Unable to rollback %d checkpoints, only %d existz)Failed to load checkpoint during rollbackz)Unable to load checkpoint during rollbackrN)�int�
ValueErrorr�errorrrr�listdirr	r�sort�warning�lenr�join�poprr)rZrollback�backups�cp_dirrrr�rollback_checkpoints\s:


��
�
zReverter.rollback_checkpointsc

Cs�tjdtdd�t�|jj�}|jdd�|s<t�	d�dSz|D]}t
|�qBWn(tk
rzt�
d�|jj���YnXg}|D�]}|�t�t
|���tj�|jj|�}ttj�|d	���}|�|���W5QRX|�d
�ttj�|d���.}|����}|D]}|�d�|���q
W5QRXtj�tj�|d
���r�ttj�|d
���8}	|�d�|	����}|D]}|�d�|���qrW5QRX|�d�q�tj�tj�jd�|�ddd�dS)a Displays all saved checkpoints.

        All checkpoints are printed by
        :meth:`certbot.interfaces.IDisplay.notification`.

        .. todo:: Decide on a policy for error handling, OSError IOError...

        :raises .errors.ReverterError: If invalid directory structure.

        aThe view_config_changes method has been deprecated and will be removed in a future release. If you were using this method to implement the view_config_changes method of IInstaller, know that that method has been removed from the plugin interface and is no longer used by Certbot.�)�
stacklevelT)�reversez3Certbot has not saved backups of your configurationNzInvalid directories in {0}�
CHANGES_SINCEzAffected files:�	FILEPATHSz  {0}�	NEW_FILESzNew Configuration Files:�
F)Zforce_interactive�pause)�warnings�warn�DeprecationWarningrrr	rr r�info�floatrrr�format�append�time�ctimerr#�open�read�
splitlines�isfile�zopeZ	componentZ
getUtilityrZIDisplayZnotification)
rr%Zbkup�outputZcur_dirZ
changes_fd�paths_fd�	filepathsr�new_fdrrr�view_config_changes�sR�
�



�zReverter.view_config_changescCs|�|jj||�dS)z�Add files to temporary checkpoint.

        :param set save_files: set of filepaths to save
        :param str save_notes: notes about changes during the save

        N)�_add_to_checkpoint_dirr	r�r�
save_files�
save_notesrrr�add_to_temp_checkpoint�s
�zReverter.add_to_temp_checkpointcCs |�|�|�|jj||�dS)z�Add files to a permanent checkpoint.

        :param set save_files: set of filepaths to save
        :param str save_notes: notes about changes during the save

        N)�_check_tempfile_savesrCr	�in_progress_dirrDrrr�add_to_checkpoint�s
�zReverter.add_to_checkpointc	
Cst�|tj|jj�|�tj�	|d��\}}t
|�}|D]�}||kr8t�d|�z>t
�|tj�	|tj�|�dt|���|�d�|��Wn<tk
r�|��t�d||�t�d�||���YnX|d7}q8|��ttj�	|d�d	��}|�|�W5QRXd
S)agAdd save files to checkpoint directory.

        :param str cp_dir: Checkpoint directory filepath
        :param set save_files: set of files to save
        :param str save_notes: notes about changes made during the save

        :raises IOError: if unable to open cp_dir + FILEPATHS file
        :raises .ReverterError: if unable to add checkpoint

        r,zCreating backup of %s�_�{0}
z&Unable to add file %s to checkpoint %sz(Unable to add file {0} to checkpoint {1}rr+�aN)rr
rrr	r
�_read_and_appendrrr#r"r�debug�shutil�copy2�basename�str�writer5�IOError�closerrrr9)	rr&rErF�op_fdZexisting_filepaths�idx�filenameZnotes_fdrrrrC�sF������

zReverter._add_to_checkpoint_dircCs:tj�|�r$t|d�}|����}ng}t|d�}||fS)z�Reads the file lines and returns a file obj.

        Read the file returning the lines, and a pointer to the end of the file.

        zr+�w)rrr<r9r:r;)r�filepathrW�linesrrrrNs

zReverter._read_and_appendc
Cs6tj�tj�|d��r*|�tj�|d��tj�tj�|d��r�zjttj�|d���N}|����}t|�D]2\}}t	�
tj�|tj�|�dt|��|�qjW5QRXWn2t
tfk
r�t�d|�t�d|��YnX|�tj�|d��zt	�|�Wn0tk
�r0t�d|�t�d|��YnXdS)aRecover a specific checkpoint.

        Recover a specific checkpoint provided by cp_dir
        Note: this function does not reload augeas.

        :param str cp_dir: checkpoint directory file path

        :raises errors.ReverterError: If unable to recover checkpoint

        �COMMANDSr,rKzUnable to recover files from %sr-zUnable to remove directory: %sN)rrr<r#�_run_undo_commandsr9r:r;�	enumeraterPrQrRrSrU�OSErrorrrrr�_remove_contained_filesZrmtree)rr&r?r@rXrrrrrs4
���
�zReverter._recover_checkpointcCs�tjddkrddini}t|df|��Z}t�|�}tt|��D]<}zt�|�Wq@t	j
k
rzt�dd�
|��Yq@Xq@W5QRXdS)	zRun all commands in a file.rr(�newline��rzUnable to run undo command: %s� N)�sys�version_infor9�csv�reader�reversed�listrZ
run_scriptrZSubprocessErrorrrr#)rr[�kwargsZcsvfileZ	csvreader�commandrrrr^:s
�zReverter._run_undo_commandsc	Cs�g}tj�|jjd�}tj�|�rJt|d��}|�|���	��W5QRXtj�|jjd�}tj�|�r�t|d��}|�|���	��W5QRX|D]}||kr�t
�d|��q�dS)z�Verify save isn't overwriting any temporary files.

        :param set save_files: Set of files about to be saved.

        :raises certbot.errors.ReverterError:
            when save is attempting to overwrite a temporary file.

        r,rdr-z+Attempting to overwrite challenge file - %sN)rrr#r	rr<r9�extendr:r;rr)rrEZprotected_filesZ	temp_pathZprotected_fd�new_pathrYrrrrHJs 	��zReverter._check_tempfile_savesc	Gs�|st�d��|�|�}d}zxz>|�tj�|d��\}}|D]}||kr<|�d�	|��q<Wn4t
tfk
r�t�
d|�t�d�	|���YnXW5|dk	r�|��XdS)aARegister the creation of all files during certbot execution.

        Call this method before writing to the file to make sure that the
        file will be cleaned up if the program exits unexpectedly.
        (Before a save occurs)

        :param bool temporary: If the file creation registry is for
            a temp or permanent save.
        :param \*files: file paths (str) to be registered

        :raises certbot.errors.ReverterError: If
            call does not contain necessary parameters or if the file creation
            is unable to be registered.

        z,Forgot to provide files to registration callNr-rLz(Unable to register file creation(s) - %sz)Unable to register file creation(s) - {0})rr�_get_cp_dirrVrNrrr#rTr5rUr`rr)r�	temporary�filesr&rAZex_filesrrrr�register_file_creationhs 

�zReverter.register_file_creationc	Cs�tj�|�|�d�}d}tjddkr.ddini}ztzBtj�|�rRt|df|�}nt|df|�}t	�
|�}|�|�Wn,tt
fk
r�t�d	�t�d
��YnXW5|dk	r�|��XdS)a�Register a command to be run to undo actions taken.

        .. warning:: This function does not enforce order of operations in terms
            of file modification vs. command registration.  All undo commands
            are run first before all normal files are reverted to their previous
            state.  If you need to maintain strict order, you may create
            checkpoints before and after the the command registration. This
            function may be improved in the future based on demand.

        :param bool temporary: Whether the command should be saved in the
            IN_PROGRESS or TEMPORARY checkpoints.
        :param command: Command to be run.
        :type command: list of str

        r]Nrr(rbrcrMrZzUnable to register undo commandz Unable to register undo command.)rrr#rprfrgrVr<r9rh�writerZwriterowrUr`rrrr)rrqrmZcommands_fpZcommand_filerlZ	csvwriterrrr�register_undo_command�s 

�zReverter.register_undo_commandcCs.|r|jj}n|jj}t�|tj|jj�|S)z%Return the proper reverter directory.)r	rrIrr
rrr
)rrqr&rrrrp�s
�zReverter._get_cp_dircCsh|��tj�|jj�rdz|�|jj�Wn8tjk
rbt	�
d|jj�t�d|jj��YnXdS)aCRevert configuration to most recent finalized checkpoint.

        Remove all changes (temporary and permanent) that have not been
        finalized. This is useful to protect against crashes and other
        execution interruptions.

        :raises .errors.ReverterError: If unable to recover the configuration

        z=Incomplete or failed recovery for IN_PROGRESS checkpoint - %sN)rrrrr	rIrrrrrrrrr�recovery_routine�s���zReverter.recovery_routinec	Cs�tj�|�sdSzTt|d��@}|����}|D](}tj�|�rJt�|�q.t�	d|�q.W5QRXWn4t
tfk
r�t�d|�t
�d�|���YnXdS)a Erase all files contained within file_list.

        :param str file_list: file containing list of file paths to be deleted

        :returns: Success
        :rtype: bool

        :raises certbot.errors.ReverterError: If
            all files within file_list cannot be removed

        FrdzVFile: %s - Could not be found to be deleted
 - Certbot probably shut down unexpectedlyz.Unable to remove filepaths contained within %sz/Unable to remove filepaths contained within {0}T)rrr<r9r:r;�lexists�removerr!rUr`rrrr5)rZ	file_listZlist_fdr@rrrrra�s.����
z Reverter._remove_contained_filesc
Cstj�|jj�sdStj�|jjd�}tj�|jjd�}tj�|�snt�d�t	|d��}|�
d�W5QRXzXt	|d��8}|�
d|�t	|d��}|�
|���W5QRXW5QRXt�
||�Wn>ttfk
�rt�d	�t�d
t���t�d��YnX|��dS)aZFinalize the checkpoint.

        Timestamps and permanently saves all changes made through the use
        of :func:`~add_to_checkpoint` and :func:`~register_file_creation`

        :param str title: Title describing checkpoint

        :raises certbot.errors.ReverterError: when the
            checkpoint is not able to be finalized.

        Nr+zCHANGES_SINCE.tmpz/Rollback checkpoint is empty (no changes made?)rZzNo changes
z	-- %s --
rdz,Unable to finalize checkpoint - adding titlezException was:
%szUnable to add title)rrrr	rIr#�existsrr3r9rTr:rPZmoverUr`rrO�	traceback�
format_excrr�_timestamp_progress_dir)r�titleZchanges_since_pathZchanges_since_tmp_path�fZchanges_tmpZchanges_origrrr�finalize_checkpoints&

"
zReverter.finalize_checkpointcCs�tt���}t�tj�|jjd��}dd�|D�}|�|�|�	�|d|krxtt
|d�d�}t�d||�|}n<t
|�dkr�|d|kr�t�d|�tt
|d�d	�}|}|S)
zBDetermine the timestamp of the checkpoint, enforcing monotonicity.z[0-9]*cSsg|]}tj�|��qSr)rrrR)�.0�drrr�
<listcomp>0sz2Reverter._checkpoint_timestamp.<locals>.<listcomp>���rzyCurrent timestamp %s does not correspond to newest reverter checkpoint; your clock probably jumped. Time travelling to %s���z6Race condition with timestamp %s, incrementing by 0.01g{�G�z�?)rSr7�globrrr#r	rr6r r4rr!r"rO)r�	timestampZothersZ
timetravelrrr�_checkpoint_timestamp,s"
�zReverter._checkpoint_timestampc	Cs�tj�d�D]X}|��}tj�|jj|�}zt	�
|jj|�WdStk
rbt
�d|�YqXqt
�d|jj|�t�d��dS)zTimestamp the checkpoint.r(Nz1Extreme, unexpected race condition, retrying (%s)z'Unable to finalize checkpoint, %s -> %sz&Unable to finalize checkpoint renaming)�sixZmoves�ranger�rrr#r	rr�replacerIr`rr!rrr)rrKr�Z	final_dirrrrr|As ��z Reverter._timestamp_progress_dirN)r)�__name__�
__module__�__qualname__�__doc__rrr'rBrGrJrCrNrr^rHrsrurprvrarr�r|rrrrrs()
*:
-''&&)r)r�rhr�ZloggingrPrfr7rzr0r�Zzope.componentr=ZcertbotrrrrZcertbot.compatrrZ	getLoggerr�r�objectrrrrr�<module>s$


Anon7 - 2021