
    -_gK?                     f    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 Z
 G d d      Zy)    N)ProcessPoolExecutor)file_is_binaryc                    t        j                  |       5 }t        j                  j	                  |d      }t        t         dd       |_        |j                         D ]  }t        j                  j	                  ||j                        }t        j                  j                  |      }t        j                  j                  |      }t        j                  j                  ||g      }||k7  rt        d| d|       |j                  ||        t        j                  j	                  ||j                  j                  d      d   j                  d      d	         cd d d        S # 1 sw Y   y xY w)
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ww||FI.
 %,G5K-H%K!
 ((* 	*F'',,tV[[9KGGOOD1M5JWW))=**EFF&"D#)($}o!? @ @OOFD)	* ww||D',,"4"4S"9""="C"CF"KA"NO)P P Ps   EE++E4c                   
   e Zd ZdZg ZdZdZdZdZdZ	i Z
d Zed        Zed        Zed        Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zed        Zd Zd Zd Zd$dZd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z&d Z'd  Z(d! Z)d" Z*y#)%SoSObfuscationArchivea  A 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                   | _        || _        | j                   j                  d      d   j                  d      d   | _        | j                  | _        t        j                  d      | _        t        j                  d      | _	        | j                         | _        d| _        | j                          d| _        | j                  d	| j                    d
| j                           y )Nr   r   r   r   sossos_uiF zLoaded 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__zSoSObfuscationArchive.__init__A   s    ("&"3"3 --33C8<BB6J1M((''.''1--/!d''(	$2B2B1CD	
r   c                     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_typeQ   s
     "!r   c                 N    d| j                   j                  j                         v S )Nr,   )	__class____name__lowerr=   s    r   is_soszSoSObfuscationArchive.is_sosV   s     //55777r   c                     d| j                   v S )Ninsights)	type_namerH   s    r   is_insightsz!SoSObfuscationArchive.is_insightsZ   s    T^^++r   c                 f    | j                   r%t        j                  | j                        | _        y y r	   )
is_tarfiler   r   r    tarobjrH   s    r   r9   z SoSObfuscationArchive._load_self^   s$    ??!,,t'8'89DK r   c                     g S )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
   rH   s    r   get_nested_archivesz)SoSObfuscationArchive.get_nested_archivesc   s	     	r   c                 F   | j                   rm| j                  j                  }|j                         r|j                  S t
        j                  j                  |j                        xs t
        j                  S t
        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.
        )rO   rP   firstmemberisdirr   r   r   dirnamesepr   r    )r=   toplevels     r   get_archive_rootz&SoSObfuscationArchive.get_archive_rootl   sg     ??{{..H~~}}$77??8==1;RVV;wwt0011r   c                 `    | j                   j                  | j                  dz   d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_msgw   s+    DLL4/4AcU;<r   c                 &    d| j                    d| S )Nz	[cleaner:z] )r0   r]   s     r   _fmt_log_msgz"SoSObfuscationArchive._fmt_log_msg{   s    4,,-Ru55r   c                 X    | j                   j                  | j                  |             y r	   )r4   debugra   r]   s     r   	log_debugzSoSObfuscationArchive.log_debug~   s    $++C01r   c                 X    | j                   j                  | j                  |             y r	   )r4   r\   ra   r]   s     r   r;   zSoSObfuscationArchive.log_info   s    **3/0r   c                 
    g d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/firmwarezsys/fszsys/kernel/debugz
sys/moduler
   rH   s    r   r6   z%SoSObfuscationArchive._load_skip_list   s    

 	
r   c                 `    	 t        j                  | j                        S # t        $ r Y yw xY w)NF)r   rO   r    r   rH   s    r   rO   z SoSObfuscationArchive.is_tarfile   s0    	%%d&7&788 		s   ! 	--c                     | j                  |      }|r@| j                  d| d       t        j                  |       | xj                  dz  c_        yy)zRemove a file from the archive. This is used when cleaner encounters
        a binary file, which we cannot reliably obfuscate.
        zRemoving 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     ''.
MM25'HIIIj!##q(# r   c                    | j                   sK| j                  s| j                         | _        t        j                  j                  | j                  |      S t        j                  j                  | j                  |      S )zBased 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
        )r8   r:   rY   r   r   r   extracted_path)r=   rm   s     r   format_file_namez&SoSObfuscationArchive.format_file_name   s]    
   $$$($9$9$;!77<< 1 1599ww||D//77r   c                    | j                   du rV| j                  rJ| j                  |      }	 | j                  j	                  |      j                         j                  d      S 	 t        | j                  |      dd      5 }|j                         cddd       S # t        $ r | j                  d| d       Y yw xY w# 1 sw Y   yxY w# t        $ r!}| j                  d	| d
|        Y d}~yd}~ww xY w)zReturn 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
        Fzutf-8zUnable to retrieve z: no such file in archiver.   r)encodingNzFailed to get contents of z: )r8   rO   rr   rP   extractfilereaddecodeKeyErrorrd   r   r   )r=   rm   filenameto_readerrs        r   get_file_contentz&SoSObfuscationArchive.get_file_content   s    
 %$//,,U3H{{..x8==?FFwOO$//6#*, */6"<<>* *  )%0IJ 	* *  !;E7"SEJKsG   7B &C C	C B>=B>C
C 
C 	C7C22C7c                    | j                   r0|s| j                  d       | j                         | _        d| _        n| j
                  | _        t        j                         dk7  r| j                  d       t        j                  | j                        D ]  \  }}}	 |D ]h  }t        j                  j                  ||      }t        j                  |      j                  }t        j                  ||t        j                  z         j |D ]  }t        j                  j                  ||      }	t        j                  j!                  |	      rt        j                  j#                  |	      rct        j$                  |	t        j&                        r%t        j$                  |	t        j(                        r| j                  d|	j+                  | j
                        d           t        j                  |	t        j,                  t        j.                  z           | j                  d| j                          y # t0        $ r}
| j                  d|
        Y d }
~
d }
~
ww xY w)	NzExtracting...Tr   z)Verifying permissions of archive contentszAdding owner rw permissions to r   z!Error while trying to set perms: zExtracted path is )rO   r_   extract_selfrq   r8   r    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_permsrz   rm   r|   s              r   r   zSoSObfuscationArchive.extract   s   ??0"&"3"3"5D $D"&"3"3D 99;!NNFG(*0C0C(D N$uN $ F#%77<<#>%'WWX%6%>%>
:+DEF %* I "Wh ?!ww~~e4u8M$ "		% 9 "		% 9 NN A#(;;t/@/@#A"#E"F!H HHUDLL4<<,GHIN( 	+D,?,?+@AB ! NNN%Fse#LMMNs    DI:A(I	I,I''I,c                     | j                   j                  | j                  |      }|| _        t        j                  | j                   |       || _         y)zRename 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   replacer0   r   rename)r=   new_name_paths      r   rename_top_dirz$SoSObfuscationArchive.rename_top_dir   sF    
 ##++D,=,=xH$
		$%%u-#r   c                 V    | j                   r| j                  j                  d      ryyy)zReturn 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)rO   r    endswithrH   s    r   get_compressionz%SoSObfuscationArchive.get_compression   s(    
 ??  ))$/r   c                 ~   d}| j                   dz   }i }|r|d| z  }|d| z  }|dk(  rddi}ndd	i}| j                  d
|        t        j                  |fd|i|5 }|j	                  | j                   t
        j                  j                  | j                        d          ddd       |S # 1 sw Y   |S xY w)zIPack the extracted archive as a tarfile to then be re-compressed
        wz-obfuscated.tar:.r   preset   compresslevel   zBuilding tar file moderi   )arcnameN)	rq   rd   r   r   addr   r   r   r0   )r=   methodr   tarpath
compr_argstars         r   build_tar_filez$SoSObfuscationArchive.build_tar_file   s     %%(99
axL D6(|#G~&]
-q1
+G956\\';;
; 	AsGGD''GGMM$*;*;<Q?  A	A 	A s    AB22B<c                 R   	 | j                  |      | _        | j                  d| j                          	 | j	                          y# t        $ r}| j                  d|         d}~ww xY w# t        $ r/}| j                  d|        | j                  d       Y d}~yd}~ww xY w)zExecute 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: NzCompressed to z'Failed to remove extraction directory: z/Failed to remove temporary extraction directory)r   r/   r   rd   remove_extracted_pathr_   )r=   r   r|   s      r   compresszSoSObfuscationArchive.compress  s    	&*&9&9&&AD# 	(?(?'@AB	O&&(  	NNEcUKL	  	ONNDSEJKOOMNN	Os.   A A. 	A+A&&A+.	B&7%B!!B&c                    	 | j                  d| j                          t        j                  | j                         y# t        $ r t        j                  | j                  t        j                         t
        j                  j                  | j                        r!t        j                  | j                         Y yt        j                  | j                         Y yw xY w)zAfter 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)rd   rq   shutilrmtreeOSErrorr   r   r   r   r   isfilerk   rH   s    r   r   z+SoSObfuscationArchive.remove_extracted_path#  s    
	3NNYt':':&;<=MM$--. 	3HHT(($,,7ww~~d112		$--.d112	3s   =A   A?C#C#"C#c                     t        d      5 }|j                  t        | j                  | j                        }|j                         }|cddd       S # 1 sw Y   yxY w)z}Extract an archive into our tmpdir so that we may inspect it or
        iterate through its contents for obfuscation
        ri   N)r   submitr'   r    r!   result)r=   _pool_path_futurer   s       r   r   z"SoSObfuscationArchive.extract_self2  sT    
 !# 	u <<(,(9(94;;HL&&(D		 	 	s   =AAc              #     K   t        j                  | j                        D ]  \  }}}|D ]F  }t         j                  j	                  ||      }t         j                  j                  |      sC| H |D ]F  }t         j                  j	                  ||      }t         j                  j                  |      sC| H  yw)z.Iterator for a list of symlinks in the archiveN)r   r   rq   r   r   r   )r=   rV   r   r   r   _dirpathrz   _fnames           r   get_symlinksz"SoSObfuscationArchive.get_symlinks=  s     $&GGD,?,?$@ 	! GT5 #77<<677>>(+"N# " !gx877>>&) L!	!s   A+C.A
C9	Cc              #     K   t        j                  | j                        D ]`  \  }}}|D ]U  }t         j                  j	                  ||j                  d            }t         j                  j                  |      rR| W b yw)zIterator 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)r   r   rq   r   r   lstripr   )r=   rV   _r   rz   r   s         r   get_file_listz#SoSObfuscationArchive.get_file_listI  sn      "$)<)<!= 	!GQ! !gxs/CDww~~f- L!	!s   A:B=	Bc                 |    g }t        j                  | j                        D ]  \  }}}|j                  |        |S )z3Return a list of all directories within the archive)r   r   rq   append)r=   dir_listrV   r   s       r   get_directory_listz(SoSObfuscationArchive.get_directory_listU  s<    WWT%8%89 	%MGQOOG$	%r   c                 d    | j                   j                  |       | xj                  |z  c_        y)zCalled 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)     	!!%(%r   c                     t         j                  j                  | j                  |j	                  d            }t         j                  j                  |      r|S dS )zReturn the filepath of a specific file within the archive so that
        it may be selectively inspected if it exists
        r   r.   )r   r   r   rq   r   r   )r=   rm   r   s      r   rj   z#SoSObfuscationArchive.get_file_pathc  sA     T00%,,s2CDu-u525r   c                 2   t         j                  j                  | j                  |            s/t         j                  j	                  | j                  |            sy| j
                  D ]+  }|j                  |      st        j                  ||      s+ y y)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)	r   r   r   rj   r   r7   
startswithrematch)r=   rz   _skips      r   should_skip_filez&SoSObfuscationArchive.should_skip_filej  ss     t11(;<t11(;<^^ 	E""5)RXXeX-F	 r   c                     g d}|D ]  }t        j                  ||      s y | j                  |      }t        j                  j                  |      rt        |      S y)aa  Determine 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   rj   r   r   r   r   )r=   rm   obvious_removes_arc_reg
_full_paths        r   should_remove_filez(SoSObfuscationArchive.should_remove_file}  s[    

 ( 	Hxx%(	 ''.
77>>*%!*--r   N)F)+rF   
__module____qualname____doc__r   r   rl   rL   r<   	is_nested
prep_filesr>   classmethodrC   propertyrI   rM   r9   rR   rY   r_   ra   rd   r;   r6   rO   ro   rr   r}   r   r   r   r   r   r   r   r   r   r   r   rj   r   r   r
   r   r   r)   r)   1   s    MOI KIJ
  " " 8 8 , ,:
	2=621
  	)	8.#CJ$	&O 3	
!
!&6& r   r)   )r2   r   r   r   r   r   concurrent.futuresr   sos.utilitiesr   r'   r)   r
   r   r   <module>r      s1     	    	 2 (P0l lr   