U
    i©¹dÚl  ã                   @   sÖ   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
 d dlmZ d dlmZ zd dlZW n ek
r   Y nX dZdZdZd	ZG d
d„ deƒZG dd„ deƒZG dd„ deƒZdS )é    N)Údatetime)ÚLock)Ú	find_spec)Úsos_get_command_outputÚfileÚlinkZnodeÚdirc                   @   sÀ   e Zd ZdZedd„ ƒZe d¡ZdZ	dZ
eƒ Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Zd+dd„Zd,dd„Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ ZdS )-ÚArchivez!Abstract base class for archives.c                 C   s   | j S )z6Returns the archive class's name as a string.
        )Ú__name__)Úcls© r   ú-/usr/lib/python3/dist-packages/sos/archive.pyÚarchive_type(   s    zArchive.archive_typeZsosZunsetFc                 C   s   d|   ¡ |f S )Nz[archive:%s] %s)r   ©ÚselfÚmsgr   r   r   Ú_format_msg5   s    zArchive._format_msgc                 C   s
   || _ d S ©N)Ú_debug)r   Údebugr   r   r   Ú	set_debug8   s    zArchive.set_debugc                 C   s   | j  |  |¡¡ d S r   )ÚlogÚerrorr   r   r   r   r   Ú	log_error;   s    zArchive.log_errorc                 C   s   | j  |  |¡¡ d S r   )r   Zwarningr   r   r   r   r   Úlog_warn>   s    zArchive.log_warnc                 C   s   | j  |  |¡¡ d S r   )r   Úinfor   r   r   r   r   Úlog_infoA   s    zArchive.log_infoc                 C   s    | j s
d S | j |  |¡¡ d S r   )r   r   r   r   r   r   r   r   Ú	log_debugD   s    zArchive.log_debugNc                 C   s   t ‚d S r   ©ÚNotImplementedError)r   ÚsrcÚdestÚforcer   r   r   Úadd_fileL   s    zArchive.add_fileÚwc                 C   s   t ‚d S r   r   )r   Úcontentr!   Úmoder   r   r   Ú
add_stringO   s    zArchive.add_stringc                 C   s   t ‚d S r   r   )r   r%   r!   r   r   r   Ú
add_binaryR   s    zArchive.add_binaryc                 C   s   t ‚d S r   r   )r   ÚsourceÚ	link_namer   r   r   Úadd_linkU   s    zArchive.add_linkc                 C   s   t ‚d S r   r   ©r   Úpathr   r   r   Úadd_dirX   s    zArchive.add_dirc                 C   s   t ‚d S r   r   )r   r-   r&   Údevicer   r   r   Úadd_node[   s    zArchive.add_nodec                 C   s   t ‚dS )z´Return a temporary directory that clients of the archive may
        use to write content to. The content of the path is guaranteed
        to be included in the generated archive.Nr   ©r   r   r   r   Úget_tmp_dir^   s    zArchive.get_tmp_dirc                 C   s   t ‚dS )zºReturn the maximum file name length this archive can support.
        This is the lesser of the name length limit of the archive
        format and any temporary file system based cache.Nr   r1   r   r   r   Úname_maxd   s    zArchive.name_maxc                 C   s   dS )aT  Return a string representing the path to the temporary
        archive. For archive classes that implement in-line handling
        this will be the archive file itself. Archives that use a
        directory based cache prior to packaging should return the
        path to the temporary directory where the report content is
        locatedNr   r1   r   r   r   Úget_archive_pathj   s    zArchive.get_archive_pathc                 C   s   dS )z:Clean up any temporary resources used by an Archive class.Nr   r1   r   r   r   Úcleanups   s    zArchive.cleanupc                 C   s   |   ¡  dS )a  Finalize an archive object via method. This may involve creating
        An archive that is subsequently compressed or simply closing an
        archive that supports in-line handling. If method is automatic then
        the following methods are tried in order: xz, gzipN)Úclose)r   Úmethodr   r   r   Úfinalizew   s    zArchive.finalize)NF)r$   )r
   Ú
__module__Ú__qualname__Ú__doc__Úclassmethodr   ÚloggingZ	getLoggerr   Ú_namer   r   Ú
_path_lockr   r   r   r   r   r   r#   r'   r(   r+   r.   r0   r2   r3   r4   r5   r8   r   r   r   r   r	   %   s0   



	r	   c                   @   sØ   e Zd ZdZdZdZdZd3dd„Zdd„ Zdd	„ Z	d4dd„Z
d5dd„Zdd„ Zd6dd„Zd7dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd8d%d&„Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ ZdS )9ÚFileCacheArchivezd Abstract superclass for archive types that use a temporary cache
    directory in the file system. Ú Nc              	   C   sš   || _ tj | j |g¡|kr,tj ||¡| _ || _|| _|| _|| _|pJd| _	|| _
tj ||¡| _| j t | jd¡ W 5 Q R X |  d| jf ¡ d S )Nú/éÀ  z*initialised empty FileCacheArchive at '%s')r>   Úosr-   ÚcommonprefixÚrelpathÚ_tmp_dirZ_policyZ_threadsÚenc_optsÚsysrootÚmanifestÚjoinÚ_archive_rootr?   Úmakedirsr   ©r   ÚnameZtmpdirZpolicyZthreadsrH   rI   rJ   r   r   r   Ú__init__ˆ   s    
ÿzFileCacheArchive.__init__c                 C   s(   t j |¡r| t j¡}t j | j|¡S r   )rD   r-   ÚisabsÚlstripÚseprK   rL   )r   rO   r   r   r   Ú	dest_pathš   s    zFileCacheArchive.dest_pathc                 C   s@   | j r| | j ¡r|S |d tjkr0|dd … }tj | j |¡S )Nr   é   )rI   Ú
startswithrD   rS   r-   rK   r,   r   r   r   Újoin_sysrootŸ   s
    zFileCacheArchive.join_sysrootrC   c                    s¨  |   d| ¡ | j‰ |}‡ fdd„}| d¡s4|}n&tj |  |¡¡rJ|ntj |¡d }|}g }|dkr’|dkr’tj |¡\}}	| |	¡ |}qb| 	¡  ˆ }
d}|D ]ü}tj 
|
|¡}
||
ƒsÂq¦tj 
||¡}tj |
¡s¦|   d|
 ¡ tj |¡r„tj |¡r„t |¡}tj |¡d }tj 
||¡}| j||d}tj |¡}tj |¡rdtj ||¡}|   d	|
|f ¡ t ||
¡ q¦|   d
|
 ¡ t |
|¡ |}q¦|S )aØ  Create leading path components

            The standard python `os.makedirs` is insufficient for our
            needs: it will only create directories, and ignores the fact
            that some path components may be symbolic links.

            :param src: The source path in the host file system for which
                        leading components should be created, or the path
                        to an sos_* virtual directory inside the archive.

                        Host paths must be absolute (initial '/'), and
                        sos_* directory paths must be a path relative to
                        the root of the archive.

            :param mode: An optional mode to be used when creating path
                         components.
            :returns: A rewritten destination path in the case that one
                      or more symbolic links in intermediate components
                      of the path have altered the path destination.
        zMaking leading paths for %sc                    s   |   tj ˆ d¡¡S )z>Test whether path ``path`` is inside the archive.
            rA   )rV   rD   r-   rK   )r-   ©Úrootr   r   Ú
in_archive¿   s    z8FileCacheArchive._make_leading_paths.<locals>.in_archiverB   r   rA   zMaking path %s©r&   zMaking symlink '%s' -> '%s'zMaking directory %s)r   rL   rV   rD   r-   ÚisdirrW   ÚsplitÚappendÚreverserK   ÚexistsÚislinkÚreadlinkÚ_make_leading_pathsÚnormpathrQ   rF   ÚsymlinkÚmkdir)r   r    r&   r!   rZ   Úsrc_dirr-   Z
path_compsÚheadÚtailZabs_pathZsrc_pathÚcompÚtargetZ
target_dirZ
target_srcr   rX   r   rc   ¦   sR    
ÿ

ÿz$FileCacheArchive._make_leading_pathsFc           
      C   sZ  |p|   |¡}|tkr|}ntj |¡d }|s4|S tj |¡rZtj |¡sZtd| ƒ‚n2tj |¡sŒ|tkrr|ntj |¡d }|  |¡ dd„ }|rœ|S tj |¡rVt 	|¡}d}	|t
krÜt |j¡sÜt|	|df ƒ‚|tkrt |j¡st|	|df ƒ‚|tkr*||jƒs*t|	|df ƒ‚|tkrRt |j¡sRt|	|d	f ƒ‚d
S |S )a4  Check a new destination path in the archive.

            Since it is possible for multiple plugins to collect the same
            paths, and since plugins can now run concurrently, it is possible
            for two threads to race in archive methods: historically the
            archive class only needed to test for the actual presence of a
            path, since it was impossible for another `Archive` client to
            enter the class while another method invocation was being
            dispatched.

            Deal with this by implementing a locking scheme for operations
            that modify the path structure of the archive, and by testing
            explicitly for conflicts with any existing content at the
            specified destination path.

            It is not an error to attempt to create a path that already
            exists in the archive so long as the type of the object to be
            added matches the type of object already found at the path.

            It is an error to attempt to re-create an existing path with
            a different path type (for example, creating a symbolic link
            at a path already occupied by a regular file).

            :param src: the source path to be copied to the archive
            :param path_type: the type of object to be copied
            :param dest: an optional destination path
            :param force: force file creation even if the path exists
            :returns: An absolute destination path if the path should be
                      copied now or `None` otherwise
        r   z'path '%s' exists and is not a directoryc                 S   s(   t t | ¡t | ¡t | ¡t | ¡gƒS r   )ÚanyÚstatÚS_ISBLKÚS_ISCHRÚS_ISFIFOÚS_ISSOCKr[   r   r   r   Ú
is_special1  s    üz/FileCacheArchive.check_path.<locals>.is_specialz path '%s' exists and is not a %szregular filezsymbolic linkzspecial fileZ	directoryN)rT   ÚP_DIRrD   r-   r]   r`   r\   Ú
ValueErrorrc   ÚlstatÚP_FILErm   ÚS_ISREGÚst_modeÚP_LINKÚS_ISLNKÚP_NODEÚS_ISDIR)
r   r    Ú	path_typer!   r"   Údest_dirrg   rr   ÚstZve_msgr   r   r   Ú
check_path  s:    ÿ

zFileCacheArchive.check_pathc              
   C   sž   zdt  |¡}| d¡s | d¡rDt ||¡ t j||j|jfd nt ||¡ t  	||j
|j¡ W n4 tk
r˜ } z|  d||f ¡ W 5 d }~X Y nX d S )Nú/sys/ú/proc/)Únsz&caught '%s' setting attributes of '%s')rD   rm   rV   ÚshutilZcopymodeÚutimeÚst_atime_nsÚst_mtime_nsZcopystatÚchownÚst_uidÚst_gidÚ	Exceptionr   )r   r    r!   rm   Úer   r   r   Ú_copy_attributesM  s    
ÿz!FileCacheArchive._copy_attributesc                 C   s
  | j ú |s|}| j|t|d}|s2W 5 Q R £ d S t|dd ƒs°zt ||¡ W nJ tk
r˜ } z,| d¡sˆ| d¡rvn|  d||f ¡ W 5 d }~X Y nX |  	||¡ d| }n8| 
d¡ t|dƒ}|D ]}| |¡ qÊW 5 Q R X d	}|  d
|| jf ¡ W 5 Q R X d S )N©r"   Úreadr   r‚   zFile %s not collected: '%s'z'%s'r   r$   z	open filez!added %s to FileCacheArchive '%s')r?   r€   rv   Úgetattrr„   ÚcopyÚOSErrorrV   r   r   ÚseekÚopenÚwriter   rL   )r   r    r!   r"   rŒ   Ú	file_nameÚfÚliner   r   r   r#   [  s.    $

ÿzFileCacheArchive.add_filer$   c              	   C   s†   | j v |}| j|tdd}tj||dd}t|tƒrB| dd¡}| |¡ t	j
 |¡rd|  ||¡ |  d|| jf ¡ W 5 Q R X d S )NTrŽ   úutf-8©ÚencodingÚutf8Úignorez-added string at '%s' to FileCacheArchive '%s')r?   r€   rv   Úcodecsr”   Ú
isinstanceÚbytesÚdecoder•   rD   r-   r`   r   r   rL   )r   r%   r!   r&   r    r—   r   r   r   r'     s    

ÿzFileCacheArchive.add_stringc              	   C   sb   | j R |  |t¡}|s&W 5 Q R £ d S tj|dd d}| |¡ |  d|| jf ¡ W 5 Q R X d S )NÚwbrš   z,added binary content at '%s' to archive '%s')r?   r€   rv   rž   r”   r•   r   rL   )r   r%   r!   r—   r   r   r   r(   ’  s    
ÿzFileCacheArchive.add_binaryc           	   	   C   s®  |   d||f ¡ | jR |  |t¡}|s8W 5 Q R £ d S tj |¡sft ||¡ |   d||| jf ¡ W 5 Q R X |   d|||f ¡ tj 	|¡}tj 
tj ||¡¡}|  |¡}dd„ }tj |¡sªtj |¡rH|}tj 	|¡}tj |t |¡¡}tj ||¡}|||ƒr(|   d||f ¡ d S |   d||f ¡ |  ||¡ nbtj |¡rp|   d| ¡ |  |¡ n:tj |¡r˜|   d	| ¡ |  |¡ n|   d
||f ¡ d S )Nzadding symlink at '%s' -> '%s'z-added symlink at '%s' to '%s' in archive '%s'z.Link follow up: source=%s link_name=%s dest=%sc              
   S   s’   t j | ¡}t j |¡s.t j t j ||¡¡}t j | ¡} | |krFdS zt  | ¡ W n8 tk
rŒ } z|jdkrzW Y ¢
dS ‚ W 5 d}~X Y nX dS )z†Return ``True`` if the symbolic link ``link_name`` is part
                of a file system loop, or ``False`` otherwise.
            Té(   NF)	rD   r-   ÚdirnamerQ   ÚrealpathrK   rm   r’   Úerrno)r*   r)   Zlink_dirrŒ   r   r   r   Úis_loop³  s    

z*FileCacheArchive.add_link.<locals>.is_loopz#Link '%s' - '%s' loops: skipping...z'Adding link %s -> %s for link follow upz Adding dir %s for link follow upz!Adding file %s for link follow upz)No link follow up: source=%s link_name=%s)r   r?   r€   ry   rD   r-   Úlexistsre   rL   r¤   r¥   rK   rT   r`   ra   rb   rF   r+   r\   r.   Úisfiler#   )	r   r)   r*   r!   Z
source_dirZhost_path_nameZdest_path_namer§   r~   r   r   r   r+     sR    
ÿÿ
ÿÿÿzFileCacheArchive.add_linkc              	   C   s"   | j  |  |t¡ W 5 Q R X dS )zmCreate a directory in the archive.

            :param path: the path in the host file system to add
        N)r?   r€   rs   r,   r   r   r   r.   ä  s    zFileCacheArchive.add_dirc              
   C   s–   |   |t¡}|sd S tj |¡s’zt |||¡ W nR tk
r„ } z4|jtjkrpd}|  	d||f ¡ W Y ¢d S |‚W 5 d }~X Y nX |  
||¡ d S )NzOperation not permittedzadd_node: %s - mknod '%s')r€   r{   rD   r-   r`   Úmknodr’   r¦   ZEPERMr   r   )r   r-   r&   r/   r!   rŒ   r   r   r   r   r0   í  s    
zFileCacheArchive.add_nodec                 C   s*   dt jkr"t jd }t  | j|¡S dS d S )NÚPC_NAME_MAXéÿ   )rD   Úpathconf_namesÚpathconfrL   )r   Zpc_name_maxr   r   r   r3   ý  s    

zFileCacheArchive.name_maxc                 C   s   | j S r   ©rL   r1   r   r   r   r2     s    zFileCacheArchive.get_tmp_dirc                 C   s   | j S r   r¯   r1   r   r   r   r4     s    z!FileCacheArchive.get_archive_pathc                 C   s2   t jt j | j|¡|d |  d|| jf ¡ dS )zsCreate path, including leading components.

            Used by sos.sosreport to set up sos_* directories.
        r[   z2created directory at '%s' in FileCacheArchive '%s'N)rD   rM   r-   rK   rL   r   )r   r-   r&   r   r   r   rM   
  s    ÿzFileCacheArchive.makedirsc                 C   s   |   |¡}tj|ddddS )NÚrr™   r   )r›   Úerrors)rT   rž   r”   r,   r   r   r   Ú	open_file  s    
zFileCacheArchive.open_filec                 C   s   t j | j¡rt | j¡ d S r   )rD   r-   r\   rL   r„   Zrmtreer1   r   r   r   r5     s    zFileCacheArchive.cleanupc                 C   sf   t  ¡ }| jj}|| }| j d|¡ | j d|¡ | j d|¡ |  | jjddtj 	dd¡¡ dS )	zŒAdds component-agnostic data to the manifest so that individual
        SoSComponents do not need to redundantly add these manually
        Zend_timeÚrun_timeZcompressioné   )ÚindentÚsos_reportszmanifest.jsonN)
r   ZnowrJ   Z
start_timeZ	add_fieldr'   Zget_jsonrD   r-   rK   )r   r7   ÚendÚstartr³   r   r   r   Úadd_final_manifest_data  s    ÿz(FileCacheArchive.add_final_manifest_datac                 C   sN   |  | j¡| _tj | j| j¡}t | j|¡ || _tj | j|  ¡ ¡| _	dS )zeRename the archive to an obfuscated version using an initialized
        SoSCleaner instance
        N)
Zobfuscate_stringr>   rD   r-   rK   rG   ÚrenamerL   rO   Ú_archive_name)r   ZcleanerZ	_new_rootr   r   r   Úrename_archive_root(  s
    z$FileCacheArchive.rename_archive_rootc              
   C   sè   |   d| j|f ¡ z|  |¡}W n< tk
r^ } z|  d| ¡ |  ¡  W Y ¢S d }~X Y nX |  ¡  |   d| jt 	| j¡j
f ¡ | jd ràz|  |¡W S  tk
rÜ } z"d}|  d||f ¡ | W Y ¢S d }~X Y qäX n|S d S )Nz)finalizing archive '%s' using method '%s'z-An error occurred compressing the archive: %szbuilt archive at '%s' (size=%d)Zencryptz)An error occurred encrypting the archive:z%s %s)r   rL   Ú_build_archiver‹   r   rO   r5   r»   rD   rm   Úst_sizerH   Ú_encrypt)r   r7   ÚresÚerrrŒ   Zexp_msgr   r   r   r8   2  s,    ÿÿ
ÿ
zFileCacheArchive.finalizec                 C   sÜ   |  dd¡}|d7 }d| }d}| jd rD|d| jd  7 }||7 }| jd r„d	| jd   d
d¡ }d|i}|d7 }d| }||7 }t|d|d}|d dkr¢|S |d dkrÄ| jd r¾d}qÐd}nd|d  }t|ƒ‚dS )a÷  Encrypts the compressed archive using GPG.

        If encryption fails for any reason, it should be logged by sos but not
        cause execution to stop. The assumption is that the unencrypted archive
        would still be of use to the user, and/or that the end user has another
        means of securing the archive.

        Returns the name of the encrypted archive, or raises an exception to
        signal that encryption failed and the unencrypted archive name should
        be used.
        z
sosreport-zsecured-sosreport-z.gpgzgpg --batch -o %s NÚkeyz--trust-model always -e -r %s Zpasswordz%sz'"rA   Zsos_gpgz-c --passphrase-fd 0 z!/bin/bash -c "echo $sos_gpg | %s"r   )ZtimeoutÚenvZstatusé   zSpecified key not in keyringzCould not read passphrasezgpg exited with code %s)ÚreplacerH   r   r‹   )r   ÚarchiveZarc_nameZenc_cmdrÃ   Zpasswdr°   r   r   r   r   r¿   J  s,    


zFileCacheArchive._encrypt)N)rC   )NF)NF)r$   )rC   )r
   r9   r:   r;   rG   rL   r»   rP   rT   rW   rc   r€   r   r#   r'   r(   r+   r.   r0   r3   r2   r4   rM   r²   r5   r¹   r¼   r8   r¿   r   r   r   r   r@   €   s6    ÿ

\
K
$
G	
	
r@   c                       s`   e Zd ZdZdZdZd‡ fdd„	Zddd„Zdd	„ Zd
d„ Z	dd„ Z
‡ fdd„Zdd„ Z‡  ZS )ÚTarFileArchivez: archive class using python TarFile to create tar archivesNFc              	      s:   t t| ƒ |||||||¡ d| _tj ||  ¡ ¡| _d S )NÚtar)	ÚsuperrÇ   rP   Ú_suffixrD   r-   rK   rO   r»   rN   ©Ú	__class__r   r   rP   |  s      ÿ ÿzTarFileArchive.__init__c                 C   sP   |j |_d|j |jd< d|j |jd< |r4||_n|j|_|j|_|j	|_
d S )Nz%.9fZatimeÚctime)Úst_mtimeÚmtimeÚst_atimeÚpax_headersÚst_ctimer&   rx   r‰   ZuidrŠ   Úgid)r   Ztar_infoÚfstatr&   r   r   r   Úset_tarinfo_from_stat…  s    z$TarFileArchive.set_tarinfo_from_statc                    s¤   |j ttj | j¡d ƒd … ‰ ˆ s*| j‰ dddg}t‡ fdd„|D ƒƒrNd S zt ˆ ¡}W n tk
rt   | Y S X | j	r”|  
ˆ ¡}|r”||jd< |  ||¡ |S )Néÿÿÿÿz/version.txt$z/sos_logs(/.*)?z/sos_reports(/.*)?c                 3   s   | ]}t  |ˆ ¡V  qd S r   )ÚreÚmatch)Ú.0Úskip©Ú	orig_pathr   r   Ú	<genexpr>—  s     z9TarFileArchive.copy_permissions_filter.<locals>.<genexpr>zRHT.security.selinux)rO   ÚlenrD   r-   r]   rL   rl   rm   r’   Ú_with_selinux_contextÚget_selinux_contextrÑ   rÕ   )r   ZtarinfoZskipsrÔ   Úcontextr   rÛ   r   Úcopy_permissions_filter’  s      



z&TarFileArchive.copy_permissions_filterc                 C   s0   zt  |¡\}}|W S  tk
r*   Y d S X d S r   )ÚselinuxZ
getfileconr‹   )r   r-   ZrcÚcr   r   r   rà   ¤  s
    z"TarFileArchive.get_selinux_contextc                 C   s   d| j | jf S )Nz%s.%s)rL   rÊ   r1   r   r   r   rO   «  s    zTarFileArchive.namec                    s   t t| ƒ ¡ S r   )rÉ   rÇ   r3   r1   rË   r   r   r3   ®  s    zTarFileArchive.name_maxc                 C   sî   |dkrt dƒd k	rdnd}| d¡}| jd|  | _|dkrHddi}nd	d
i}tj| jfdd| i|—Ž}dD ]D}tj tj | j	|¡¡sŽqp|j
tj | j	|¡| j› d|› d qp|j
| j	| j| jd | ¡  |  jd| 7  _|  ¡ S )NÚautoZlzmaZxzZgzipZipz.%sZcompresslevelé   Zpreseté   r&   zw:%s)zversion.txtr¶   Zsos_logsrB   )Úarcname)rè   Úfilter)r   Ústripr»   Útarfiler”   rD   r-   r`   rK   rL   Úaddr>   râ   r6   rÊ   rO   )r   r7   Z
_comp_modeÚkwargsrÈ   Z_contentr   r   r   r½   ³  s.    

ÿþÿzTarFileArchive._build_archive)N)N)r
   r9   r:   r;   r7   rß   rP   rÕ   râ   rà   rO   r3   r½   Ú__classcell__r   r   rË   r   rÇ   v  s    ÿ	
rÇ   )rD   rë   r„   r=   rž   r¦   rm   r×   r   Z	threadingr   Úimportlib.utilr   Zsos.utilitiesr   rã   ÚImportErrorrv   ry   r{   rs   Úobjectr	   r@   rÇ   r   r   r   r   Ú<module>   s2   [   y