Current File : //usr/lib/python3/dist-packages/sos/cleaner/archives/__pycache__/__init__.cpython-312.pyc
�

-�_gK?��f�ddlZddlZddlZddlZddlZddlZddlmZddlm	Z	d�Z
Gd�d�Zy)�N)�ProcessPoolExecutor)�file_is_binaryc���tj|�5}tjj	|d�}ttdd��|_|j�D]�}tjj	||j�}tjj|�}tjj|�}tjj||g�}||k7rtd|�d|����|j||���tjj	||jjd�djd�d	�cddd�S#1swYyxYw)
N�cleaner�fully_trusted_filterc��|S�N�)�member�paths  �?/usr/lib/python3/dist-packages/sos/cleaner/archives/__init__.py�<lambda>z!extract_archive.<locals>.<lambda>!s��&��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�prefixs	         r
�extract_archiver's4��	���l�	#�P�w��w�w�|�|�F�I�.��
%,�G�5K�-H�%K��!�
�(�(�*�	*�F��'�'�,�,�t�V�[�[�9�K��G�G�O�O�D�1�M�������5�J��W�W�)�)�=�*�*E�F�F���&��"D�#)�(�$�}�o�!?�@�@��O�O�F�D�)�	*��w�w�|�|�D�'�,�,�"4�"4�S�"9�"�"=�"C�"C�F�"K�A�"N�O�)P�P�P�s�EE+�+E4c�
�eZdZdZgZdZdZdZdZdZ	iZ
d�Zed��Z
ed��Zed��Zd	�Zd
�Zd�Zd�Zd
�Zd�Zd�Zd�Zed��Zd�Zd�Zd�Zd$d�Zd�Zd�Zd�Z d�Z!d�Z"d�Z#d�Z$d�Z%d�Z&d�Z'd �Z(d!�Z)d"�Z*y#)%�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�undeterminedFc���||_|j|_||_|jjd�djd�d|_|j|_t
jd�|_t
jd�|_	|j�|_d|_|j�d|_|jd	|j�d
|j ���y)Nrrrr�sos�sos_uiF�zLoaded z	 as type )r �final_archive_pathr!r�archive_name�ui_name�logging�	getLogger�soslog�ui_log�_load_skip_list�	skip_list�is_extracted�
_load_self�archive_root�log_info�description)�selfr r!s   r
�__init__zSoSObfuscationArchive.__init__As���(���"&�"3�"3������ �-�-�3�3�C�8��<�B�B�6�J�1�M����(�(����'�'��.����'�'��1����-�-�/���!�����������
�
��d�'�'�(�	�$�2B�2B�1C�D�	
rc��t�)z=Check if the archive is a well-known type we directly support)�NotImplementedError)�cls�arc_paths  r
�
check_is_typez#SoSObfuscationArchive.check_is_typeQs
��"�!rc�N�d|jjj�vS)Nr,)�	__class__�__name__�lower�r=s r
�is_soszSoSObfuscationArchive.is_sosVs ������/�/�5�5�7�7�7rc��d|jvS)N�insights)�	type_namerHs r
�is_insightsz!SoSObfuscationArchive.is_insightsZs���T�^�^�+�+rc�f�|jr%tj|j�|_yyr	)�
is_tarfilerrr �tarobjrHs r
r9z SoSObfuscationArchive._load_self^s$���?�?�!�,�,�t�'8�'8�9�D�K�rc��gS)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
rHs r
�get_nested_archivesz)SoSObfuscationArchive.get_nested_archivescs	���	rc�F�|jrm|jj}|j�r|jSt
jj|j�xst
jSt
jj|j�S)z|Set the root path for the archive that should be prepended to any
        filenames given to methods in this class.
        )rOrP�firstmember�isdirrrr�dirname�seprr )r=�toplevels  r
�get_archive_rootz&SoSObfuscationArchive.get_archive_rootlsg���?�?��{�{�.�.�H��~�~���}�}�$��7�7�?�?�8�=�=�1�;�R�V�V�;��w�w���t�0�0�1�1rc�`�|jj|jdzd�d|���y)z9Helper to easily format ui messages on a per-report basisz :z<50� N)r5�infor1�r=�msgs  r
�
report_msgz SoSObfuscationArchive.report_msgws+�������D�L�L�4�/��4�A�c�U�;�<rc�&�d|j�d|��S)Nz	[cleaner:z] )r0r]s  r
�_fmt_log_msgz"SoSObfuscationArchive._fmt_log_msg{s���4�,�,�-�R��u�5�5rc�X�|jj|j|��yr	)r4�debugrar]s  r
�	log_debugzSoSObfuscationArchive.log_debug~s�������$�+�+�C�0�1rc�X�|jj|j|��yr	)r4r\rar]s  r
r;zSoSObfuscationArchive.log_info�s��������*�*�3�/�0rc�
�gd�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/moduler
rHs r
r6z%SoSObfuscationArchive._load_skip_list�s��

�	
rc�`�	tj|j�S#t$rYywxYw)NF)rrOr rrHs r
rOz SoSObfuscationArchive.is_tarfile�s0��	��%�%�d�&7�&7�8�8���	��	�s�!�	-�-c��|j|�}|r@|jd|�d��tj|�|xjdz
c_yy)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_pathr;r�remove�removed_file_count)r=�fname�
full_fnames   r
�remove_filez!SoSObfuscationArchive.remove_file�sO���'�'��.�
���M�M�2�5�'��H�I��I�I�j�!��#�#�q�(�#�rc��|jsK|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
        )r8r:rYrrr�extracted_path)r=rms  r
�format_file_namez&SoSObfuscationArchive.format_file_name�s]��
� � ��$�$�$(�$9�$9�$;��!��7�7�<�<�� 1� 1�5�9�9��w�w�|�|�D�/�/��7�7rc���|jdurV|jrJ|j|�}	|jj	|�j�j
d�S	t|j|�dd��5}|j�cddd�S#t$r|jd|�d��YywxYw#1swYyxYw#t$r!}|jd	|�d
|���Yd}~yd}~wwxYw)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: )r8rOrrrP�extractfile�read�decode�KeyErrorrdrr)r=rm�filename�to_read�errs     r
�get_file_contentz&SoSObfuscationArchive.get_file_content�s���
����%�$�/�/��,�,�U�3�H�
��{�{�.�.�x�8�=�=�?�F�F�w�O�O�
��$�/�/��6��#*�,�*�/6�"�<�<�>�*�*���
����)�%��0I�J���	
��*�*���
����!;�E�7�"�S�E�J�K���
�sG�7B�&C
�C�	C
�B>�=B>�C
�C
�
C
�
	C7�C2�2C7c���|jr0|s|jd�|j�|_d|_n|j
|_t
j�dk7�r�|jd�t
j|j�D�]�\}}}	|D]h}tjj||�}t
j|�j}t
j||tjz��j|D�]}tjj||�}	tjj!|	�rtjj#|	�r�ct
j$|	tj&�r%t
j$|	tj(�r��|jd|	j+|j
�d���t
j|	tj,tj.z������|jd|j���y#t0$r}
|jd|
���Yd}
~
���d}
~
wwxYw)	Nz
Extracting...Trz)Verifying permissions of archive contentszAdding owner rw permissions to rz!Error while trying to set perms: zExtracted path is )rOr_�extract_selfrqr8r r�getuidrd�walkrr�stat�st_mode�chmod�S_IRWXU�exists�islink�access�R_OK�W_OKr�S_IRUSR�S_IWUSRr)r=�quietrV�dirs�files�_dir�_dirname�
_dir_permsrzrmr|s           r
rzSoSObfuscationArchive.extract�s����?�?������0�"&�"3�"3�"5�D�� $�D��"&�"3�"3�D���9�9�;�!���N�N�F�G�(*����0C�0C�(D�
N�$���u�N� $�F��#%�7�7�<�<���#>��%'�W�W�X�%6�%>�%>�
�����:����+D�E�F�%*�I�� "�����W�h� ?��!�w�w�~�~�e�4������u�8M�$� "�	�	�%���� 9� "�	�	�%���� 9� �N�N� A�#(�;�;�t�/@�/@�#A�"�#E�"F�!H���H�H�U�D�L�L�4�<�<�,G�H�I�

N�(	
���+�D�,?�,?�+@�A�B��!�N��N�N�%F�s�e�#L�M�M��N�s �DI�:A(I�	I,�
I'�'I,c��|jj|j|�}||_tj|j|�||_y)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)rq�replacer0r�rename)r=�new_name�_paths   r
�rename_top_dirz$SoSObfuscationArchive.rename_top_dir�sF��
�#�#�+�+�D�,=�,=�x�H��$���
�	�	�$�%�%�u�-�#��rc�V�|jr|jjd�ryyy)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)rOr �endswithrHs r
�get_compressionz%SoSObfuscationArchive.get_compression�s(��
�?�?�� � �)�)�$�/���rc�~�d}|jdz}i}|r|d|��z
}|d|��z
}|dk(rddi}ndd	i}|jd
|���tj|fd|i|��5}|j	|jt
jj|j�d�
�ddd�|S#1swY|SxYw)zIPack the extracted archive as a tarfile to then be re-compressed
        �wz-obfuscated.tar�:�.r��preset��
compresslevel�zBuilding tar file �moderi)�arcnameN)	rqrdrr�addrrrr0)r=�methodr��tarpath�
compr_args�tars      r
�build_tar_filez$SoSObfuscationArchive.build_tar_files������%�%�(9�9���
���a��x�L� �D���6�(�|�#�G���~�&��]�
�-�q�1�
����+�G�9�5�6�
�\�\�'�
;��
;�
�
;�	A�s��G�G�D�'�'��G�G�M�M�$�*;�*;�<�Q�?�
�
A�	A���	A���s
� AB2�2B<c�R�	|j|�|_|jd|j���	|j	�y#t$r}|jd|����d}~wwxYw#t$r/}|jd|���|jd�Yd}~yd}~wwxYw)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_)r=r�r|s   r
�compresszSoSObfuscationArchive.compresss���	�&*�&9�&9�&�&A�D�#�	
�����(?�(?�'@�A�B�	O��&�&�(���	��N�N�E�c�U�K�L���	���	O��N�N�D�S�E�J�K��O�O�M�N�N��	O�s.�A�A.�	A+�A&�&A+�.	B&�7%B!�!B&c���	|jd|j���tj|j�y#t$r�tj|jtj�t
jj|j�r!tj|j�Yytj|j�YywxYw)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)rdrq�shutil�rmtree�OSErrorrr�r�r�r�isfilerkrHs r
r�z+SoSObfuscationArchive.remove_extracted_path#s���
	3��N�N�Y�t�':�':�&;�<�=��M�M�$�-�-�.���	3��H�H�T�(�(�$�,�,�7��w�w�~�~�d�1�1�2��	�	�$�-�-�.��
�
�d�1�1�2�	3�s�=A�A?C#�C#�"C#c��td�5}|jt|j|j�}|j�}|cddd�S#1swYyxYw)z}Extract an archive into our tmpdir so that we may inspect it or
        iterate through its contents for obfuscation
        riN)r�submitr'r r!�result)r=�_pool�_path_futurers    r
rz"SoSObfuscationArchive.extract_self2sT��
!��
#�	�u� �<�<��(,�(9�(9�4�;�;�H�L��&�&�(�D��		�	�	�s�=A�Ac#�K�tj|j�D]�\}}}|D]F}tjj	||�}tjj|�s�C|���H|D]F}tjj	||�}tjj|�s�C|���H��y�w)z.Iterator for a list of symlinks in the archiveN)rr�rqrrr�)r=rVr�r�r��_dirpathrz�_fnames        r
�get_symlinksz"SoSObfuscationArchive.get_symlinks=s�����$&�G�G�D�,?�,?�$@�	!� �G�T�5��
#���7�7�<�<���6���7�7�>�>�(�+�"�N�
#�"�
!�������g�x�8���7�7�>�>�&�)� �L�
!�	!�s�A+C�.A
C�9	Cc#�K�tj|j�D]`\}}}|D]U}tjj	||jd��}tjj
|�r�R|���W�by�w)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�rqrr�lstripr�)r=rV�_r�rzr�s      r
�
get_file_listz#SoSObfuscationArchive.get_file_listIsn����"$����)<�)<�!=�	!��G�Q��!�
!�������g�x���s�/C�D���w�w�~�~�f�-� �L�
!�	!�s�A:B�=	Bc�|�g}tj|j�D]\}}}|j|��|S)z3Return a list of all directories within the archive)rr�rq�append)r=�dir_listrVr�s    r
�get_directory_listz(SoSObfuscationArchive.get_directory_listUs<�����W�W�T�%8�%8�9�	%�M�G�Q���O�O�G�$�	%��rc�d�|jj|�|xj|z
c_y)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)r=rm�counts   r
�update_sub_countz&SoSObfuscationArchive.update_sub_count\s)��	
���!�!�%�(�����%�rc��tjj|j|j	d��}tjj|�r|SdS)zReturn the filepath of a specific file within the archive so that
        it may be selectively inspected if it exists
        rr.)rrrrqr�r�)r=rmr�s   r
rjz#SoSObfuscationArchive.get_file_pathcsA�������T�0�0�%�,�,�s�2C�D�������u�-�u�5�2�5rc�2�tjj|j|��s/tjj	|j|��sy|j
D]+}|j
|�stj||�s�+yy)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�rjr�r7�
startswith�re�match)r=rz�_skips   r
�should_skip_filez&SoSObfuscationArchive.should_skip_filejss�������t�1�1�(�;�<������t�1�1�(�;�<���^�^�	�E��"�"�5�)�R�X�X�e�X�-F��	�rc��gd�}|D]}tj||�s�y|j|�}tjj|�rt
|�Sy)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�rjrrr�r)r=rm�obvious_removes�_arc_reg�
_full_paths     r
�should_remove_filez(SoSObfuscationArchive.should_remove_file}s[��

��(�	�H��x�x��%�(��	��'�'��.�
�
�7�7�>�>�*�%�!�*�-�-�rN)F)+rF�
__module__�__qualname__�__doc__r�r�rlrLr<�	is_nested�
prep_filesr>�classmethodrC�propertyrIrMr9rRrYr_rardr;r6rOrorrr}rr�r�r�r�r�rr�r�r�r�rjr�r�r
rr
r)r)1s����M��O����I� �K��I��J�
� �"��"��8��8��,��,�:�
�	2�=�6�2�1�
�����	)�	8��.#C�J$�	��&O� 
3�	�
!�
!��&�6��& rr))r2rr�r�rr��concurrent.futuresr�
sos.utilitiesrr'r)r
rr
�<module>r�s1���	�
���	�2�(�P�0l�lr