HEX
Server: Apache
System: Linux zacp120.webway.host 4.18.0-553.50.1.lve.el8.x86_64 #1 SMP Thu Apr 17 19:10:24 UTC 2025 x86_64
User: govancoz (1003)
PHP: 8.3.26
Disabled: exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Upload Files
File: //lib/python3.6/site-packages/sos/cleaner/archives/__pycache__/__init__.cpython-36.pyc
3

mk�hY�@sjddlZddlZddlZddlZddlZddlZddlZddlmZddl	m
Z
dd�ZGdd�d�ZdS)�N)�ProcessPoolExecutor)�file_is_binaryc	
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@speZdZdZdZdZdZdZdZdZ	iZ
dd�Zdd�Zd	d
�Z
dd�Zd
d�ZdPdd�Zdd�Zedd��Zedd��Zedd��Zdd�Zdd�Zdd�Zd d!�ZdQd"d#�ZdRd$d%�ZdSd&d'�ZdTd(d)�Zd*d+�Zed,d-��Z d.d/�Z!d0d1�Z"d2d3�Z#dUd4d5�Z$d6d7�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�Z1dS)V�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)Nr	r
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�parsers�log_info�description)�selfrrr,rrr�__init__Bs
zSoSObfuscationArchive.__init__cCsVxP|jD]F}y|j|�}Wqtk
rL}z|jd|���WYdd}~XqXqW|S)NzError obfuscating string data: )r-Zparse_string_for_keysrr.)r0Zstring_data�parser�errrrr�obfuscate_stringTs&z&SoSObfuscationArchive.obfuscate_stringcCs�|j|jd�d�}|j|jd�d|�}||kr�|j|�d}tjj||�}tjj|�shtj||�n&|jtj|��}tj	|�tj
||�dS)Nr	r
rrr)r4r�replacerrr�islink�rename�readlink�remove�symlink)r0�
short_name�filenameZ_ob_short_nameZ_ob_filename�arc_pathZ_ob_pathZ
_target_obrrr�obfuscate_filename^s
z(SoSObfuscationArchive.obfuscate_filenamecCs
||_dS)N)r-)r0r-rrr�set_parsersusz!SoSObfuscationArchive.set_parserscCs4tjd�|_tjd�|_x|jD]}|j�q WdS)Nrr)r#r$r%r&r-Zload_map_entries)r0r2rrr�load_parser_entriesxs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: )�stripr-Z
parse_liner�	log_debugr)r0�liner-�countr2�_countr3rrr�obfuscate_line~s
*z$SoSObfuscationArchive.obfuscate_linec
's|j��x|D�]�}|jdtj��d|����y�|j|jd�d�|j��rTw|jrr|j��rr|j	��w|jr�t
|�s�|j��r�wtjj|�r�w�fdd�|j
D�}|s�|jd�p�|�d��w|jd	�p�|���d
}tjd|jd���}t|d
ddd��x}xp|D]h}y&|j||�\}}||7}|j|�Wn:tk
�rp}	z|jd��d|	���WYdd}	~	XnX�qWWdQRX|jd
�|�r�tj|j|�|j|�WdQRX|j�|�Wqtk
�r}	z&|jdtj��d|�d|	���WYdd}	~	XqXqW|j|j|jfS)Nz    pid=z: obfuscating r	r
cs(g|] }t�fdd�|jD��s|�qS)c3s|]}|j��VqdS)N)�match)�.0�_skip)r;rr�	<genexpr>�szGSoSObfuscationArchive.obfuscate_arc_files.<locals>.<listcomp>.<genexpr>)�anyZ
skip_patterns)rHZ_p)r;rr�
<listcomp>�sz=SoSObfuscationArchive.obfuscate_arc_files.<locals>.<listcomp>zSkipping obfuscation of z" due to matching file skip patternzObfuscating r�w)�mode�dir�rzutf-8r5)�encoding�errorszUnable to obfuscate z: z': caught exception on obfuscating file )r@rBr�getpidrr!�should_skip_filer,�should_remove_file�remove_filerrr6r-�tempfileZNamedTemporaryFilerr
rF�writer�seek�shutilZcopyfiler�update_sub_countr>�files_obfuscated_count�total_sub_count�removed_file_count)
r0�flistr<Z_parsersZsubsZtfile�fnamerCZcntr3r)r;r�obfuscate_arc_files�sX





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)r0rrr�is_sos�szSoSObfuscationArchive.is_soscCs
d|jkS)NZinsights)�	type_name)r0rrr�is_insights�sz!SoSObfuscationArchive.is_insightscCs|jrtj|j�|_dS)N)�
is_tarfilerr
r�tarobj)r0rrrr*�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)r0rrr�get_nested_archives�sz)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.
        )rkrlZfirstmember�isdirrrr�dirname�seprr)r0Ztoplevelrrr�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")r0�msgrrr�
report_msgsz SoSObfuscationArchive.report_msgcCs&d|rd|��nd�d|j�d|��S)Nz[cleaner�:r�[z]] )r!)r0rt�callerrrr�_fmt_log_msgsz"SoSObfuscationArchive._fmt_log_msgcCs|jj|j||��dS)N)r%�debugry)r0rtrxrrrrB	szSoSObfuscationArchive.log_debugcCs|jj|j||��dS)N)r%rsry)r0rtrxrrrr.szSoSObfuscationArchive.log_infocCs|jj|j||��dS)N)r%�errorry)r0rtrxrrr�	log_errorszSoSObfuscationArchive.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)r0rrrr'sz%SoSObfuscationArchive._load_skip_listcCs&ytj|j�Stk
r dSXdS)NF)rrkrr)r0rrrrk sz SoSObfuscationArchive.is_tarfilecCs<|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_pathr.rr9r^)r0r`Z
full_fnamerrrrV's


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+rqrrr�extracted_path)r0r`rrr�format_file_name2s

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 archiverrP)rQNzFailed to get contents of z: )r)rkrrlZextractfile�read�decode�KeyErrorrBr
r)r0r`r<Zto_readr3rrr�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)rkru�extract_selfr~r)rlrr�getuidrB�walkrr�stat�st_mode�chmod�S_IRWXU�existsr6�access�R_OK�W_OKr�S_IRUSR�S_IWUSRr)r0�quietro�dirs�files�_dirZ_dirnameZ
_dir_permsr<r`r3rrrrTs6




&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)r~r5r!rr7)r0�new_name�_pathrrr�rename_top_dirzsz$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)rkr�endswith)r0rrr�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
        rMz-obfuscated.tarrv�.r�Zpreset�Z
compresslevel�zBuilding tar file rNr
)ZarcnameN)	r~rBrr
�addrrrr!)r0�methodrNZtarpathZ
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 rrB�remove_extracted_pathru)r0r�r3rrr�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)rBr~rZZrmtree�OSErrorrr�r�r�r�isfiler9)r0rrrr��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)r0Z_poolZ_path_futurerrrrr��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�r~rrr6)r0ror�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
        r	N)rr�r~rr�lstripr6)r0ro�_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�r~�append)r0Zdir_listror�rrr�get_directory_list�sz(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)r\r])r0rDrrrr[�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
        r	r)rrrr~r�r�)r0r`r�rrrr}�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}r6r(�
startswith�rerG)r0r<rIrrrrT�s
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�rGr}rrr�r)r0r`Zobvious_removesZ_arc_regZ
_full_pathrrrrUs 

z(SoSObfuscationArchive.should_remove_file)N)N)N)N)N)F)2rf�
__module__�__qualname__�__doc__r\r]r^rir/Z	is_nestedZ
prep_filesr1r4r>r?r@rFra�classmethodrd�propertyrhrjr*rmrqruryrBr.r|r'rkrVrr�rr�r�r�r�r�r�r�r�r�r[r}rTrUrrrrr2sZ

A	




&
r)
r#rrZr�rrWr��concurrent.futuresrZ
sos.utilitiesrrrrrrr�<module>s