
    ~ gK?                     f    S SK r S SKrS SKrS SKrS SKrS SKrS SKJr  S SKJ	r	  S r
 " S S5      rg)    N)ProcessPoolExecutor)file_is_binaryc                    [         R                  " U 5       n[        R                  R	                  US5      n[        [         SS 5      Ul        UR                  5        H  n[        R                  R	                  X4R                  5      n[        R                  R                  U5      n[        R                  R                  U5      n[        R                  R                  Xg/5      nX:w  a  [        SU SU 35      eUR                  XC5        M     [        R                  R	                  X2R                  R                  S5      S   R                  S5      S	   5      sS S S 5        $ ! , (       d  f       g = f)
Ncleanerfully_trusted_filterc                     U $ N )memberpaths     ?/usr/lib/python3/dist-packages/sos/cleaner/archives/__init__.py<lambda>!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(      s   	l	#www||FI.
 %,G5K-H%K!
 ((*F'',,t[[9KGGOOD1M5JWW))=*EFF&"D#)($}o!? @ @OOF) + ww||D,,"4"4S"9""="C"CF"KA"NO) 
$	#	#s   E	E**
E8c                      \ rS rSrSr/ rSrSrSrSr	Sr
0 rS r\S 5       r\S 5       r\S	 5       rS
 rS rS rS rS rS rS rS r\S 5       rS rS rS rS&S jrS rS r S r!S r"S r#S r$S r%S r&S r'S  r(S! r)S" r*S# r+S$r,g%)'SoSObfuscationArchive1   zA 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                    Xl         U R                   U l        X l        U R                   R                  S5      S   R                  S5      S   U l        U R                  U l        [        R                  " S5      U l        [        R                  " S5      U l	        U R                  5       U l        SU l        U R                  5         SU l        U R                  S	U R                    S
U R                    35        g )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__SoSObfuscationArchive.__init__A   s    ("&"3"3 --33C8<BB6J1M((''.''1--/!d''(	$2B2B1CD	
r   c                     [         e)z=Check if the archive is a well-known type we directly support)NotImplementedError)clsarc_paths     r   check_is_type#SoSObfuscationArchive.check_is_typeQ   s
     "!r   c                 P    SU R                   R                  R                  5       ;   $ )Nr.   )	__class____name__lowerr?   s    r   is_sosSoSObfuscationArchive.is_sosV   s     //55777r   c                      SU R                   ;   $ )Ninsights)	type_namerL   s    r   is_insights!SoSObfuscationArchive.is_insightsZ   s    T^^++r   c                 r    U R                   (       a&  [        R                  " U R                  5      U l        g g r	   )
is_tarfiler   r   r!   tarobjrL   s    r   r;    SoSObfuscationArchive._load_self^   s$    ??!,,t'8'89DK r   c                     / $ )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
   rL   s    r   get_nested_archives)SoSObfuscationArchive.get_nested_archivesc   s	     	r   c                 d   U R                   (       aw  U R                  R                  nUR                  5       (       a  UR                  $ [
        R                  R                  UR                  5      =(       d    [
        R                  $ [
        R                  R                  U R                  5      $ )zlSet the root path for the archive that should be prepended to any
filenames given to methods in this class.
)rU   rV   firstmemberisdirr   r   r   dirnamesepr   r!   )r?   toplevels     r   get_archive_root&SoSObfuscationArchive.get_archive_rootl   sj     ??{{..H~~}}$77??8==1;RVV;wwt0011r   c                 `    U R                   R                  U R                  S-   S SU 35        g)z9Helper to easily format ui messages on a per-report basisz :z<50 N)r7   infor3   r?   msgs     r   
report_msg SoSObfuscationArchive.report_msgw   s+    DLL4/4AcU;<r   c                 &    SU R                    SU 3$ )Nz	[cleaner:z] )r2   rf   s     r   _fmt_log_msg"SoSObfuscationArchive._fmt_log_msg{   s    4,,-Ru55r   c                 X    U R                   R                  U R                  U5      5        g r	   )r6   debugrk   rf   s     r   	log_debugSoSObfuscationArchive.log_debug~   s    $++C01r   c                 X    U R                   R                  U R                  U5      5        g r	   )r6   re   rk   rf   s     r   r=   SoSObfuscationArchive.log_info   s    **3/0r   c                 
    / SQ$ )ziProvide 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
   rL   s    r   r8   %SoSObfuscationArchive._load_skip_list   s    

 	
r   c                 d     [         R                  " U R                  5      $ ! [         a     gf = f)NF)r   rU   r!   r   rL   s    r   rU    SoSObfuscationArchive.is_tarfile   s0    	%%d&7&788 		s   " 
//c                     U R                  U5      nU(       aA  U R                  SU S35        [        R                  " U5        U =R                  S-  sl        gg)zxRemove 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_file!SoSObfuscationArchive.remove_file   sO     ''.
MM25'HIIIj!##q(# r   c                    U R                   (       dP  U R                  (       d  U R                  5       U l        [        R                  R                  U R                  U5      $ [        R                  R                  U R                  U5      $ )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
)r:   r<   ra   r   r   r   extracted_path)r?   r|   s     r   format_file_name&SoSObfuscationArchive.format_file_name   s]    
   $$$($9$9$;!77<< 1 1599ww||D//77r   c                    U R                   SL a[  U R                  (       aJ  U R                  U5      n U R                  R	                  U5      R                  5       R                  S5      $  [        U R                  U5      SSS9 nUR                  5       sSSS5        $ ! [         a    U R                  SU S35         gf = f! , (       d  f       g= f! [         a!  nU R                  S	U S
U 35         SnAgSnAff = f)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 archiver0   r)encodingNzFailed to get contents of z: )r:   rU   r   rV   extractfilereaddecodeKeyErrorro   r   r   )r?   r|   filenameto_readerrs        r   get_file_content&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  CC
CC C 
D C<<Dc                 6   U R                   (       a5  U(       d  U R                  S5        U R                  5       U l        SU l        OU R
                  U l        [        R                  " 5       S:w  Ga  U R                  S5        [        R                  " U R                  5       GH  u  p#n U Hi  n[        R                  R                  X%5      n[        R                  " U5      R                  n[        R                  " Xg[        R                  -  5        Mk     U GH)  n[        R                  R                  X(5      n	[        R                  R!                  U	5      (       a$  [        R                  R#                  U	5      (       a  Mm  [        R$                  " U	[        R&                  5      (       a,  [        R$                  " U	[        R(                  5      (       a  M  U R                  SU	R+                  U R
                  5      S    35        [        R                  " U	[        R,                  [        R.                  -  5        GM,     GM     U R                  SU R                   35        g ! [0         a   n
U R                  SU
 35         S n
A
GM  S n
A
ff = f)	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 )rU   rh   extract_selfr   r:   r!   r   getuidro   walkr   r   statst_modechmodS_IRWXUexistsislinkaccessR_OKW_OKr    S_IRUSRS_IWUSRr   )r?   quietr^   dirsfiles_dir_dirname
_dir_permsr   r|   r   s              r   r   SoSObfuscationArchive.extract   s   ??0"&"3"3"5D $D"&"3"3D 99;!NNFG(*0C0C(D$uN $#%77<<#>%'WWX%6%>%>
+DE !% %* "W ?!ww~~e44u8M8M$ "		% 9 9 "		% 9 9 NN A#(;;t/@/@#A"#E"F!H HHUDLL4<<,GH %* )E( 	+D,?,?+@AB ! NNN%Fse#LMMNs    +D0I.A+I..
J8JJc                     U R                   R                  U R                  U5      nXl        [        R                  " U R                   U5        X l         g)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)r   replacer2   r   rename)r?   new_name_paths      r   rename_top_dir$SoSObfuscationArchive.rename_top_dir   sB    
 ##++D,=,=xH$
		$%%u-#r   c                 j    U R                   (       a"  U R                  R                  S5      (       a  ggg)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)rU   r!   endswithrL   s    r   get_compression%SoSObfuscationArchive.get_compression   s+    
 ??  ))$//r   c                    SnU R                   S-   n0 nU(       a  USU 3-  nUSU 3-  nUS:X  a  SS0nOSS	0nU R                  S
U 35        [        R                  " U4SU0UD6 nUR	                  U R                   [
        R                  R                  U R                  5      S   S9  SSS5        U$ ! , (       d  f       U$ = f)zIPack the extracted archive as a tarfile to then be re-compressed
        wz-obfuscated.tar:.r   preset   compresslevel   zBuilding tar file moderx   )arcnameN)	r   ro   r   r   addr   r   r    r2   )r?   methodr   tarpath
compr_argstars         r   build_tar_file$SoSObfuscationArchive.build_tar_file   s     %%(99
axL D6(|#G~&]
-q1
+G956\\';;
;sGGD''GGMM$*;*;<Q?  A <  <; s   'AB66
Cc                 V    U R                  U5      U l        U R                  SU R                   35         U R	                  5         g! [         a  nU R                  SU 35        e SnAff = f! [         a/  nU R                  SU 35        U R                  S5         SnAgSnAff = f)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   r1   r   ro   remove_extracted_pathrh   )r?   r   r   s      r   compress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(9%B##B(c                     U R                  SU R                   35        [        R                  " U R                  5        g! [         a    [
        R                  " U R                  [        R                  5        [
        R                  R                  U R                  5      (       a"  [
        R                  " U R                  5         g[        R                  " U R                  5         gf = f)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)ro   r   shutilrmtreeOSErrorr   r   r   r   r   isfilerz   rL   s    r   r   +SoSObfuscationArchive.remove_extracted_path#  s    
	3NNYt':':&;<=MM$--. 	3HHT(($,,7ww~~d1122		$--.d112	3s   >A BC-
 C-,C-c                     [        S5       nUR                  [        U R                  U R                  5      nUR                  5       nUsSSS5        $ ! , (       d  f       g= f)zmExtract an archive into our tmpdir so that we may inspect it or
iterate through its contents for obfuscation
rx   N)r   submitr(   r!   r"   result)r?   _pool_path_futurer   s       r   r   "SoSObfuscationArchive.extract_self2  sK    
 !#u <<(,(9(94;;HL&&(D	 $##s   =A
A!c              #     #    [         R                  " U R                  5       H  u  pnU HL  n[         R                  R	                  X5      n[         R                  R                  U5      (       d  MH  Uv   MN     U HL  n[         R                  R	                  X5      n[         R                  R                  U5      (       d  MH  Uv   MN     M     g7f)z.Iterator for a list of symlinks in the archiveN)r   r   r   r   r   r   )r?   r^   r   r   r   _dirpathr   _fnames           r   get_symlinks"SoSObfuscationArchive.get_symlinks=  s     $&GGD,?,?$@ G577<<677>>(++"N  "g877>>&)) L " %As   A-C3ACCc              #   "  #    [         R                  " U R                  5       Hg  u  pnU H[  n[         R                  R	                  XR                  S5      5      n[         R                  R                  U5      (       a  MW  Uv   M]     Mi     g7f)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   r   r   r   lstripr   )r?   r^   _r   r   r   s         r   get_file_list#SoSObfuscationArchive.get_file_listI  sb      "$)<)<!=G!gs/CDww~~f-- L " ">s   A<BBc                     / n[         R                  " U R                  5       H  u  n  nUR                  U5        M     U$ )z3Return a list of all directories within the archive)r   r   r   append)r?   dir_listr^   r   s       r   get_directory_list(SoSObfuscationArchive.get_directory_listU  s7    WWT%8%89MGQOOG$ :r   c                 d    U R                   R                  U5        U =R                  U-  sl        g)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?   r|   counts      r   update_sub_count&SoSObfuscationArchive.update_sub_count\  s)     	!!%(%r   c                     [         R                  R                  U R                  UR	                  S5      5      n[         R                  R                  U5      (       a  U$ S$ )zoReturn the filepath of a specific file within the archive so that
it may be selectively inspected if it exists
r   r0   )r   r   r   r   r   r   )r?   r|   r   s      r   ry   #SoSObfuscationArchive.get_file_pathc  sD     T00%,,s2CDu--u525r   c                 ^   [         R                  R                  U R                  U5      5      (       d4  [         R                  R	                  U R                  U5      5      (       d  gU R
                   H6  nUR                  U5      (       d  [        R                  " X!5      (       d  M6    g   g)zChecks 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   ry   r   r9   
startswithrematch)r?   r   _skips      r   should_skip_file&SoSObfuscationArchive.should_skip_filej  sx     t11(;<<t11(;<<^^E""5))RXXe-F-F $ r   c                     / SQnU H   n[         R                  " X15      (       d  M     g   U R                  U5      n[        R                  R                  U5      (       a  [        U5      $ g)a1  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   ry   r   r   r   r   )r?   r|   obvious_removes_arc_reg
_full_paths        r   should_remove_file(SoSObfuscationArchive.should_remove_file}  sZ    

 (Hxx(( ( ''.
77>>*%%!*--r   )r2   r!   r<   r   r1   r:   r9   r6   rV   r"   r7   r3   N)F)-rJ   
__module____qualname____firstlineno____doc__r   r   r{   rQ   r>   	is_nested
prep_filesr@   classmethodrF   propertyrM   rR   r;   rY   ra   rh   rk   ro   r=   r8   rU   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   ry   r   r   __static_attributes__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*   )r4   r   r   r   r   r   concurrent.futuresr   sos.utilitiesr   r(   r*   r
   r   r   <module>r     s1     	    	 2 (P0l lr   