HEX
Server: Apache
System: Linux pdx1-shared-a1-38 6.6.104-grsec-jammy+ #3 SMP Tue Sep 16 00:28:11 UTC 2025 x86_64
User: mmickelson (3396398)
PHP: 8.1.31
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/sos/cleaner/archives/__pycache__/__init__.cpython-310.pyc
o

([QhK?�@sbddlZddlZddlZddlZddlZddlZddlmZddlm	Z	dd�Z
Gdd�d�ZdS)�N)�ProcessPoolExecutor)�file_is_binaryc	Cs�t�|��a}tj�|d�}ttddd��|_|��D]2}tj�||j�}tj�	|�}tj�	|�}tj�
||g�}||krFtd|�d|����|�||�qtj�||j�
d�d�
d	�d
�Wd�S1siwYdS)N�cleaner�fully_trusted_filtercSs|S�N�)�member�pathrr�?/usr/lib/python3/dist-packages/sos/cleaner/archives/__init__.py�<lambda>!sz!extract_archive.<locals>.<lambda>z"Attempted path traversal in tarflez != �/����.tarr)�tarfile�open�osr	�join�getattr�extraction_filter�
getmembers�name�abspath�commonprefix�	Exception�extract�split)	�archive_path�tmpdir�archiver	r�member_path�
abs_directory�
abs_target�prefixrrr
�extract_archives&���"$�r#c@s.eZdZdZgZdZdZdZdZdZ	iZ
dd�Zedd��Z
ed	d
��Zedd��Zd
d�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zedd��Zdd �Zd!d"�Zd#d$�ZdBd%d&�Zd'd(�Zd)d*�Zd+d,�Z d-d.�Z!d/d0�Z"d1d2�Z#d3d4�Z$d5d6�Z%d7d8�Z&d9d:�Z'd;d<�Z(d=d>�Z)d?d@�Z*dAS)C�SoSObfuscationArchiveaA representation of an extracted archive or an sos archive build
    directory which is used by SoSCleaner.

    Each archive that needs to be obfuscated is loaded into an instance of this
    class. All report-level operations should be contained within this class.
    r�undeterminedFcCs�||_|j|_||_|j�d�d�d�d|_|j|_t�d�|_t�d�|_	|�
�|_d|_|�
�d|_|�d	|j�d
|j���dS)Nrr
rr�sos�sos_uiF�zLoaded z	 as type )r�final_archive_pathrr�archive_name�ui_name�logging�	getLogger�soslog�ui_log�_load_skip_list�	skip_list�is_extracted�
_load_self�archive_root�log_info�description)�selfrrrrr
�__init__As
�zSoSObfuscationArchive.__init__cCst�)z=Check if the archive is a well-known type we directly support)�NotImplementedError)�cls�arc_pathrrr
�
check_is_typeQsz#SoSObfuscationArchive.check_is_typecCsd|jj��vS)Nr&)�	__class__�__name__�lower�r7rrr
�is_sosVszSoSObfuscationArchive.is_soscCs
d|jvS)N�insights)�	type_namer@rrr
�is_insightsZs
z!SoSObfuscationArchive.is_insightscCs|jrt�|j�|_dSdSr)�
is_tarfilerrr�tarobjr@rrr
r3^s�z SoSObfuscationArchive._load_selfcCsgS)a/Return a list of ObfuscationArchives that represent additional
        archives found within the target archive. For example, an archive from
        `sos collect` will return a list of ``SoSReportArchive`` objects.

        This should be overridden by individual types of ObfuscationArchive's
        rr@rrr
�get_nested_archivescsz)SoSObfuscationArchive.get_nested_archivescCs>|jr|jj}|��r|jStj�|j�ptjStj�	|j
�S)z|Set the root path for the archive that should be prepended to any
        filenames given to methods in this class.
        )rErF�firstmember�isdirrrr	�dirname�seprr)r7�toplevelrrr
�get_archive_rootlsz&SoSObfuscationArchive.get_archive_rootcCs"|j�|jdd�d|���dS)z9Helper to easily format ui messages on a per-report basisz :z<50� N)r/�infor+�r7�msgrrr
�
report_msgws"z SoSObfuscationArchive.report_msgcCsd|j�d|��S)Nz	[cleaner:z] )r*rPrrr
�_fmt_log_msg{sz"SoSObfuscationArchive._fmt_log_msgcC�|j�|�|��dSr)r.�debugrSrPrrr
�	log_debug~�zSoSObfuscationArchive.log_debugcCrTr)r.rOrSrPrrr
r5�rWzSoSObfuscationArchive.log_infocCsgd�S)zyProvide a list of files and file regexes to skip obfuscation on

        Returns: list of files and file regexes
        )z
proc/kallsymsz
sosreport-zsys/firmwarezsys/fszsys/kernel/debugz
sys/modulerr@rrr
r0�sz%SoSObfuscationArchive._load_skip_listcCs$zt�|j�WStyYdSw)NF)rrErrr@rrr
rE�s
�z SoSObfuscationArchive.is_tarfilecCs@|�|�}|r|�d|�d��t�|�|jd7_dSdS)z�Remove a file from the archive. This is used when cleaner encounters
        a binary file, which we cannot reliably obfuscate.
        zRemoving binary file 'z' from archive�N)�
get_file_pathr5r�remove�removed_file_count)r7�fname�
full_fnamerrr
�remove_file�s

�z!SoSObfuscationArchive.remove_filecCs6|js|js|��|_tj�|j|�Stj�|j|�S)z�Based on the type of archive we're dealing with, do whatever that
        archive requires to a provided **relative** filepath to be able to
        access it within the archive
        )r2r4rMrr	r�extracted_path)r7r\rrr
�format_file_name�s

z&SoSObfuscationArchive.format_file_namec
Cs�|jdur-|jr-|�|�}z|j�|����d�WSty,|�d|�d��YdSwz"t	|�|�ddd��
}|��Wd�WS1sHwYWdSt
ym}z|�d	|�d
|���WYd}~dSd}~ww)z�Return the content from the specified fname. Particularly useful for
        tarball-type archives so we can retrieve prep file contents prior to
        extracting the entire archive
        Fzutf-8zUnable to retrieve z: no such file in archiver(�r)�encodingNzFailed to get contents of z: )r2rEr`rF�extractfile�read�decode�KeyErrorrVrr)r7r\�filename�to_read�errrrr
�get_file_content�s,

���(���z&SoSObfuscationArchive.get_file_contentcCs^|jr|s
|�d�|��|_d|_n|j|_t��dkr�|�d�t�	|j�D]{\}}}z\|D]}tj
�||�}t�|�j
}t�||tjB�q0|D]<}tj
�||�}	tj
�|	�r`tj
�|	�raqKt�|	tj�rot�|	tj�s�|�d|	�|j�d���t�|	tjtjB�qKWq(ty�}
z|�d|
���WYd}
~
q(d}
~
ww|�d|j���dS)	Nz
Extracting...Trz)Verifying permissions of archive contentszAdding owner rw permissions to r
z!Error while trying to set perms: zExtracted path is )rErR�extract_selfr_r2rr�getuidrV�walkr	r�stat�st_mode�chmod�S_IRWXU�exists�islink�access�R_OK�W_OKr�S_IRUSR�S_IWUSRr)r7�quietrJ�dirs�files�_dir�_dirname�
_dir_permsrgr\rirrr
r�sD


�������zSoSObfuscationArchive.extractcCs.|j�|j|�}||_t�|j|�||_dS)z�Rename the top-level directory to new_name, which should be an
        obfuscated string that scrubs the hostname from the top-level dir
        which would be named after the unobfuscated sos report
        N)r_�replacer*r�rename)r7�new_name�_pathrrr
�rename_top_dir�s
z$SoSObfuscationArchive.rename_top_dircCs|jr
|j�d�rdSdSdS)z�Return the compression type used by the archive, if any. This is
        then used by SoSCleaner to generate a policy-derived compression
        command to repack the archive
        �xz�gzN)rEr�endswithr@rrr
�get_compression�s
z%SoSObfuscationArchive.get_compressioncCs�d}|jd}i}|r&|d|��7}|d|��7}|dkr"ddi}ndd	i}|�d
|���tj|fd|i|���}|j|jtj�|j�dd
�Wd�|S1sTwY|S)zIPack the extracted archive as a tarfile to then be re-compressed
        �wz-obfuscated.tar�:�.r��preset��
compresslevel�zBuilding tar file �moderX)�arcnameN)	r_rVrr�addrr	rr*)r7�methodr��tarpath�
compr_args�tarrrr
�build_tar_files$

�
��z$SoSObfuscationArchive.build_tar_filec
Cs�z|�|�|_Wnty}z	|�d|����d}~ww|�d|j���z|��WdStyN}z|�d|���|�d�WYd}~dSd}~ww)z�Execute the compression command, and set the appropriate final
        archive path for later reference by SoSCleaner on a per-archive basis
        z(Exception while re-compressing archive: NzCompressed to z'Failed to remove extraction directory: z/Failed to remove temporary extraction directory)r�r)rrV�remove_extracted_pathrR)r7r�rirrr
�compresss����zSoSObfuscationArchive.compresscCsvz|�d|j���t�|j�WdSty:t�|jtj�tj	�
|j�r1t�|j�YdSt�|j�YdSw)z�After the tarball has been re-compressed, remove the extracted path
        so that we don't take up that duplicate space any longer during
        execution
        z	Removing N)rVr_�shutil�rmtree�OSErrorrrprnrxr	�isfilerZr@rrr
r�#s�z+SoSObfuscationArchive.remove_extracted_pathcCsJtd��}|�t|j|j�}|��}|Wd�S1swYdS)z}Extract an archive into our tmpdir so that we may inspect it or
        iterate through its contents for obfuscation
        rXN)r�submitr#rr�result)r7�_pool�_path_futurer	rrr
rk2s
�$�z"SoSObfuscationArchive.extract_selfccsr�t�|j�D]/\}}}|D]}tj�||�}tj�|�r |Vq|D]}tj�||�}tj�|�r5|Vq#qdS)z.Iterator for a list of symlinks in the archiveN)rrmr_r	rrs)r7rJrzr{r|�_dirpathrg�_fnamerrr
�get_symlinks=s�����z"SoSObfuscationArchive.get_symlinksccsN�t�|j�D]\}}}|D]}tj�||�d��}tj�|�s#|VqqdS)z�Iterator for a list of files in the archive, to allow clean to
        iterate over.

        Will not include symlinks, as those are handled separately
        rN)rrmr_r	r�lstriprs)r7rJ�_r{rgr�rrr
�
get_file_listIs����z#SoSObfuscationArchive.get_file_listcCs*g}t�|j�D]
\}}}|�|�q|S)z3Return a list of all directories within the archive)rrmr_�append)r7�dir_listrJr�rrr
�get_directory_listUsz(SoSObfuscationArchive.get_directory_listcCs|j�|�|j|7_dS)z�Called when a file has finished being parsed and used to track
        total substitutions made and number of files that had changes made
        N)�
file_sub_listr��total_sub_count)r7r\�countrrr
�update_sub_count\sz&SoSObfuscationArchive.update_sub_countcCs*tj�|j|�d��}tj�|�r|SdS)zReturn the filepath of a specific file within the archive so that
        it may be selectively inspected if it exists
        rr()rr	rr_r�rr)r7r\r�rrr
rYcsz#SoSObfuscationArchive.get_file_pathcCsTtj�|�|��stj�|�|��sdS|jD]}|�|�s$t�||�r'dSqdS)a%Checks the provided filename against a list of filepaths to not
        perform obfuscation on, as defined in self.skip_list

        Positional arguments:

            :param filename str:        Filename relative to the extracted
                                        archive root
        TF)	rr	r�rYrsr1�
startswith�re�match)r7rg�_skiprrr
�should_skip_filejs
�
�z&SoSObfuscationArchive.should_skip_filecCsFgd�}|D]}t�||�rdSq|�|�}tj�|�r!t|�SdS)aaDetermine if the file should be removed or not, due to an inability
        to reliably obfuscate that file based on the filename.

        :param fname:       Filename relative to the extracted archive root
        :type fname:        ``str``

        :returns:   ``True`` if the file cannot be reliably obfuscated
        :rtype:     ``bool``
        )	z.*\.gz$z.*\.xz$z
.*\.bzip2$z.*\.tar\..*z.*\.txz$z.*\.tgz$z.*\.bin$z.*\.journal$z.*\~$TF)r�r�rYrr	r�r)r7r\�obvious_removes�_arc_reg�
_full_pathrrr
�should_remove_file}s
�
z(SoSObfuscationArchive.should_remove_fileN)F)+r>�
__module__�__qualname__�__doc__r�r�r[rCr6�	is_nested�
prep_filesr8�classmethodr<�propertyrArDr3rGrMrRrSrVr5r0rEr^r`rjrr�r�r�r�r�rkr�r�r�r�rYr�r�rrrr
r$1sV


	

%
r$)r,rr�rnrr��concurrent.futuresr�
sos.utilitiesrr#r$rrrr
�<module>s