HEX
Server: LiteSpeed
System: Linux node612.namehero.net 4.18.0-553.121.1.lve.el8.x86_64 #1 SMP Thu Apr 30 16:40:41 UTC 2026 x86_64
User: dfwparty (1186)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: //lib/python3.6/site-packages/sos/cleaner/archives/__pycache__/__init__.cpython-36.pyc
3

��'j__�@srddlZddlZddlZddlZddlZddlZddlZddlmZddl	m
Z
mZmZdd�Z
Gdd�d�ZdS)�N)�ProcessPoolExecutor)�file_is_binary�sos_get_command_output�file_is_certificatec	
Cs�tj|���}tjj|d�}ttddd��|_xp|j�D]d}tjj||j�}tjj	|�}tjj	|�}tjj
||g�}||kr�td|�d|����|j||�q6Wtjj||jj
d�dj
d	�d
�SQRXdS)NZcleanerZfully_trusted_filtercSs|S)N�)�member�pathrr�/usr/lib/python3.6/__init__.py�<lambda>#sz!extract_archive.<locals>.<lambda>z"Attempted path traversal in tarflez != �/�z.tarr���)�tarfile�open�osr�join�getattrZextraction_filterZ
getmembers�name�abspath�commonprefix�	Exception�extract�split)	�archive_path�tmpdir�archiverrZmember_pathZ
abs_directoryZ
abs_target�prefixrrr	�extract_archivesrc@sxeZdZdZdZdZdZdZdZdZ	iZ
dd�Zdd�Zd	d
�Z
dd�Zd
d�ZdRdd�Zdd�Zedd��Zedd��Zedd��Zdd�Zdd�Zdd�Zd d!�ZdSd"d#�ZdTd$d%�ZdUd&d'�ZdVd(d)�Zd*d+�Zed,d-��Z d.d/�Z!d0d1�Z"d2d3�Z#d4d5�Z$dWd6d7�Z%d8d9�Z&d:d;�Z'd<d=�Z(d>d?�Z)d@dA�Z*dBdC�Z+dDdE�Z,dFdG�Z-dHdI�Z.dJdK�Z/dLdM�Z0dNdO�Z1dPdQ�Z2dS)X�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.
    rZundeterminedFcCs�||_|j|_||_|jjd�djd�d|_|j|_tjd�|_tjd�|_	|j
�|_d|_|j
�d|_||_||_f|_|jd	|j�d
|j���dS)Nrrz.tarr�sos�sos_uiF�zLoaded z	 as type r
)r�final_archive_pathrr�archive_name�ui_name�logging�	getLogger�soslog�ui_log�_load_skip_list�	skip_list�is_extracted�
_load_self�archive_root�keep_binary_files�treat_certificates�parsers�log_info�description)�selfrrr.r/rrr	�__init__Cs 
zSoSObfuscationArchive.__init__cCsVxP|jD]F}y|j|�}Wqtk
rL}z|jd|���WYdd}~XqXqW|S)NzError obfuscating string data: )r0Zparse_string_for_keysrr1)r3Zstring_data�parser�errrrr	�obfuscate_stringWs&z&SoSObfuscationArchive.obfuscate_stringcCs�|j|jd�d�}|j|jd�d|�}||kr�|j|�d}tjj||�}tjj|�shtj||�n&|jtj|��}tj	|�tj
||�dS)Nrrrr
r
)r7r�replacerrr�islink�rename�readlink�remove�symlink)r3Z
short_name�filenameZ_ob_short_nameZ_ob_filename�arc_pathZ_ob_pathZ
_target_obrrr	�obfuscate_filenameas
z(SoSObfuscationArchive.obfuscate_filenamecCs
||_dS)N)r0)r3r0rrr	�set_parsersxsz!SoSObfuscationArchive.set_parserscCs4tjd�|_tjd�|_x|jD]}|j�q WdS)Nrr )r%r&r'r(r0Zload_map_entries)r3r5rrr	�load_parser_entries{sz)SoSObfuscationArchive.load_parser_entriesNcCs�d}|j�s||fS|dkr"|j}x^|D]V}y|j|�\}}||7}Wq(tk
r|}z|jd|��|j�WYdd}~Xq(Xq(W||fS)a�Run a line through each of the obfuscation parsers, keeping a
        cumulative total of substitutions done on that particular line.

        Positional arguments:

            :param line str:        The raw line as read from the file being
                                    processed
            :param parsers:         A list of parser objects to obfuscate
                                    with. If None, use all.

        Returns the fully obfuscated line and the number of substitutions made
        rNzfailed to parse line: )�stripr0Z
parse_liner�	log_debugr)r3�liner0�countr5�_countr6rrr	�obfuscate_line�s
*z$SoSObfuscationArchive.obfuscate_linec's�|j��xp|D�]f}|jdtj��d|����ytjj||jd��|j��rRw|jrp|j	��rp|j
��w|jr�t|�s�|j	��r�wtjj|�r�wt
|�}|�r|dkr�|j
��w|jdkr�w|jdkr�|j
��w|jdk�r|j|�}tjj||jd���fdd	�|jD�}|�s8|jd
��p,|�d��w|jd��pF|���d
}tjd|jd���}t|dddd��x}xp|D]h}y&|j||�\}}	||	7}|j|�Wn:tk
�r�}
z|jd��d|
���WYdd}
~
XnX�q|WWdQRX|jd
�|�rtj|j|�|j|�WdQRX|j�|�Wqtk
�rv}
z&|jdtj��d|�d|
���WYdd}
~
XqXqW|j|j|j fS)Nz    pid=z: obfuscating )�startZcertificatekeyZkeepr<Z	obfuscatecs(g|] }t�fdd�|jD��s|�qS)c3s|]}|j��VqdS)N)�match)�.0�_skip)�rel_namerr	�	<genexpr>�szGSoSObfuscationArchive.obfuscate_arc_files.<locals>.<listcomp>.<genexpr>)�anyZ
skip_patterns)rKZ_p)rMrr	�
<listcomp>�sz=SoSObfuscationArchive.obfuscate_arc_files.<locals>.<listcomp>zSkipping obfuscation of z" due to matching file skip patternzObfuscating r�w)�mode�dir�rzutf-8r8)�encoding�errorszUnable to obfuscate z: z': caught exception on obfuscating file )!rBrDr�getpidr�relpath�extracted_path�should_skip_filer.�should_remove_file�remove_filerr9rr/�certificate_to_textr0�tempfileZNamedTemporaryFilerrrH�writer�seek�shutilZcopyfiler�update_sub_countr@�files_obfuscated_count�total_sub_count�removed_file_count)r3�flistr>Zis_certificateZ_parsersZsubsZtfile�fnamerEZcntr6r)rMr	�obfuscate_arc_files�st











8
6z)SoSObfuscationArchive.obfuscate_arc_filescCst�dS)z=Check if the archive is a well-known type we directly supportN)�NotImplementedError)�clsr?rrr	�
check_is_type�sz#SoSObfuscationArchive.check_is_typecCsd|jjj�kS)Nr)�	__class__�__name__�lower)r3rrr	�is_sos�szSoSObfuscationArchive.is_soscCs
d|jkS)NZinsights)�	type_name)r3rrr	�is_insights�sz!SoSObfuscationArchive.is_insightscCs|jrtj|j�|_dS)N)�
is_tarfilerrr�tarobj)r3rrr	r,�sz 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
        r)r3rrr	�get_nested_archivessz)SoSObfuscationArchive.get_nested_archivescCs>|jr0|jj}|j�r|jStjj|j�p.tjStjj	|j
�S)z|Set the root path for the archive that should be prepended to any
        filenames given to methods in this class.
        )rrrsZfirstmember�isdirrrr�dirname�seprr)r3Ztoplevelrrr	�get_archive_root
sz&SoSObfuscationArchive.get_archive_rootcCs"|jj|jdd�d|���dS)z9Helper to easily format ui messages on a per-report basisz :z<50� N)r(�infor$)r3�msgrrr	�
report_msgsz SoSObfuscationArchive.report_msgcCs&d|rd|��nd�d|j�d|��S)Nz[cleaner�:r!�[z]] )r#)r3r{�callerrrr	�_fmt_log_msgsz"SoSObfuscationArchive._fmt_log_msgcCs|jj|j||��dS)N)r'�debugr�)r3r{rrrr	rDszSoSObfuscationArchive.log_debugcCs|jj|j||��dS)N)r'rzr�)r3r{rrrr	r1 szSoSObfuscationArchive.log_infocCs|jj|j||��dS)N)r'�errorr�)r3r{rrrr	�	log_error#szSoSObfuscationArchive.log_errorcCsddddddgS)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/moduler)r3rrr	r)&sz%SoSObfuscationArchive._load_skip_listcCs&ytj|j�Stk
r dSXdS)NF)rrrrr)r3rrr	rr4sz SoSObfuscationArchive.is_tarfilecCsB|�d�}|jd|�d��td|�dt|����tj|�|S)z�Convert a certificate to text. This is used when cleaner encounters
        a certificate file and the option 'treat_certificates' is 'obfuscate'.
        z.textzConverting certificate file 'z	' to textz openssl storeutl -r -noout -out z -text -certs )r1r�strrr<)r3rgZout_fnrrr	r];s

z)SoSObfuscationArchive.certificate_to_textcCs<|j|�}|r8|jd|�d��tj|�|jd7_dS)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 archiverN)�
get_file_pathr1rr<re)r3rgZ
full_fnamerrr	r\Hs


z!SoSObfuscationArchive.remove_filecCs6|js&|js|j�|_tjj|j|�Stjj|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
        )r+r-rxrrrrY)r3rgrrr	�format_file_nameSs

z&SoSObfuscationArchive.format_file_namecCs�|jdkrZ|jrZ|j|�}y|jj|�j�jd�Stk
rV|jd|�d��dSXn`y(t	|j|�ddd��
}|j�SQRXWn6t
k
r�}z|jd	|�d
|���dSd}~XnXdS)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!rT)rUNzFailed to get contents of z: )r+rrr�rsZextractfile�read�decode�KeyErrorrDrr)r3rgr>Zto_readr6rrr	�get_file_content^s

z&SoSObfuscationArchive.get_file_contentcCs~|jr,|s|jd�|j�|_d|_d|_n|j|_tj�dk�rh|j	d��xtj
|j�D�]\}}}y�x8|D]0}tjj||�}tj
|�j}tj||t
jB�qpWx�|D]~}tjj||�}	tjj|	�s�tjj|	�r�q�tj|	tj�s�tj|	tj�r�|j	d|	j|j�d	���tj|	t
jt
jB�q�WWq\tk
�rb}
z|j	d|
���WYdd}
~
Xq\Xq\W|j	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 r
)rrr|�extract_selfrYr+rsrr�getuidrD�walkrr�stat�st_mode�chmod�S_IRWXU�existsr9�access�R_OK�W_OKr�S_IRUSR�S_IWUSRr)r3�quietrv�dirs�files�_dirZ_dirnameZ
_dir_permsr>rgr6rrr	rus6




&zSoSObfuscationArchive.extractcCs.|jj|j|�}||_tj|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)rYr8r#rr:)r3�new_name�_pathrrr	�rename_top_dir�sz$SoSObfuscationArchive.rename_top_dircCs|jr|jjd�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
        �xzZgzN)rrr�endswith)r3rrr	�get_compression�s
z%SoSObfuscationArchive.get_compressioncCs�d}|jd}i}|rL|d|��7}|d|��7}|dkrDddi}ndd	i}|jd
|���tj|fd|i|���$}|j|jtjj|j�dd
�WdQRX|S)zIPack the extracted archive as a tarfile to then be re-compressed
        rQz-obfuscated.tarr}�.r�Zpreset�Z
compresslevel�zBuilding tar file rRr)ZarcnameN)	rYrDrr�addrrrr#)r3�methodrRZtarpathZ
compr_argsZtarrrr	�build_tar_file�s

 z$SoSObfuscationArchive.build_tar_filecCs�y|j|�|_Wn4tk
rD}z|jd|����WYdd}~XnX|jd|j���y|j�Wn<tk
r�}z |jd|���|jd�WYdd}~XnXdS)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"rrD�remove_extracted_pathr|)r3r�r6rrr	�compress�szSoSObfuscationArchive.compresscCsty"|jd|j���tj|j�WnLtk
rntj|jtj�tj	j
|j�r^tj|j�ntj|j�YnXdS)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)rDrYraZrmtree�OSErrorrr�r�r�r�isfiler<)r3rrr	r��sz+SoSObfuscationArchive.remove_extracted_pathcCs2td�� }|jt|j|j�}|j�}|SQRXdS)z}Extract an archive into our tmpdir so that we may inspect it or
        iterate through its contents for obfuscation
        rN)rZsubmitrrr�result)r3Z_poolZ_path_futurerrrr	r��s

z"SoSObfuscationArchive.extract_selfccs|xvtj|j�D]f\}}}x,|D]$}tjj||�}tjj|�r|VqWx,|D]$}tjj||�}tjj|�rL|VqLWqWdS)z.Iterator for a list of symlinks in the archiveN)rr�rYrrr9)r3rvr�r�r�Z_dirpathr>�_fnamerrr	�get_symlinks�s


z"SoSObfuscationArchive.get_symlinksccsTxNtj|j�D]>\}}}x2|D]*}tjj||jd��}tjj|�s|VqWqWdS)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
        rN)rr�rYrr�lstripr9)r3rv�_r�r>r�rrr	�	get_files�s

zSoSObfuscationArchive.get_filescCs.g}x$tj|j�D]\}}}|j|�qW|S)z3Return a list of all directories within the archive)rr�rY�append)r3Zdir_listrvr�rrr	�get_directory_listsz(SoSObfuscationArchive.get_directory_listcCs |jd7_|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
        rN)rcrd)r3rFrrr	rbsz&SoSObfuscationArchive.update_sub_countcCs*tjj|j|jd��}tjj|�r&|SdS)zReturn the filepath of a specific file within the archive so that
        it may be selectively inspected if it exists
        rr!)rrrrYr�r�)r3rgr�rrr	r�sz#SoSObfuscationArchive.get_file_pathcCsZtjj|j|��r,tjj|j|��r,dSx(|jD]}|j|�sNtj||�r4dSq4WdS)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)	rrr�r�r9r*�
startswith�rerJ)r3r>rLrrr	rZs
z&SoSObfuscationArchive.should_skip_filec	CsVddddddddd	g	}x|D]}tj||�rd
SqW|j|�}tjj|�rRt|�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�rJr�rrr�r)r3rgZobvious_removesZ_arc_regZ
_full_pathrrr	r[-s 

z(SoSObfuscationArchive.should_remove_file)N)N)N)N)N)F)3rm�
__module__�__qualname__�__doc__rcrdrerpr2Z	is_nestedZ
prep_filesr4r7r@rArBrHrh�classmethodrk�propertyrorqr,rtrxr|r�rDr1r�r)rrr]r\r�r�rr�r�r�r�r�r�r�r�r�rbr�rZr[rrrr	r3s\

R	





&
r)r%rrar�rr^r��concurrent.futuresrZ
sos.utilitiesrrrrrrrrr	�<module>s