,ruA% dZddlZddlZddlZddlmZddlmZmZm Z ddl m Z m Z ddl mZddlmZmZddlZejZejejzejzZdZed!d Zed efd Zd"d eeefd eefdZd ed efdZed ed edede fdZ!dededeeefdeefdZ"e ddefdZ#dede fdZ$ddddedefd Z%dS)#zF Mostly copy-pasting from `safe_fileops.py` of the Imunify360 project N)ProcessPoolExecutor) ExitStackcontextmanagersuppress) lru_cachepartial)Path)OptionalUnionctjdkrtjgtj|tj|||S)Nr)osgetuid setgroupssetgidsetuid)uidgidfunargss R/opt/imunify360/venv/lib/python3.11/site-packages/restore_infected/safe_fileops.pydroprsI y{{a RIcNNNIcNNN 3:c#NKtj|||} |Vtt5tj|ddddS#1swxYwYdS#tt5tj|dddw#1swxYwYwxYw)zR Context manager which wraps os.open and close file descriptor at the end dir_fdN)r openrOSErrorclose)pathflagsrfds ropen_fdr"s# uV , , ,B g     HRLLL                  Xg     HRLLL                s@A"AAA"B$7B B$B B$B B$namec#Kt|tj5}tjd|}||krt d|VddddS#1swxYwYdS)z Context manager to get a directory file descriptor It also checks if a directory doesn't contain a symlink in the path :param name: full directory name z/proc/self/fd/{}z%Operations on symlinks are prohibitedN)r"r O_DIRECTORYreadlinkformatPermissionError)r#rreals r open_dir_fdr*)s r~ & &&{-44V<<== 4<<!"IJJ J sAA++A/2A/frc|t|trtj|||}t||S)a Context manager to open file object from file name or from file descriptor File object extended with 'st' attribute that contains os.stat_result of the opened file :param f: file name or file descriptor to open :param mode: mode for built-in open :param flags: flags for os.open, ignored if 'f' is a file descriptor :param dir_fd: directory descriptor, ignored if 'f' is a file descriptor )r rmode) isinstancestrr r)r+r.r rs r open_fobjr18s?!S3 GAU6 2 2 2    rctj||}tj||jtjz||S)Nr)r.r)r statchmodst_modeS_IRUSR)r#rsts r make_readabler8Js; f % % %BHT T\1&AAAA Irr is_safec#K|r3t|||5}|dfVddddS#1swxYwYdS||fVdS)z If is_safe flag is True, open file descriptor using name and dir_fd If is_safe is False, return name and dir_fd as is )rr N)r")r#rr r9r!s r safe_tupler;Rs  T& 6 6 6 "d(NNN                  Fls +//src_fdsrc_modedst_f dst_dir_fdcrt|d5}t|dt|5}|r |tj||t |t r(t j||dddn #1swxYwYddddS#1swxYwYdS)Nrbwbr-) r1W_FLAGSshutil copyfileobjr/r0r r4fileno)r<r=r>r? _race_dstsrc_fodst_fos r_moverJ_s5 64 5FI tWj-- 5    IKKK66*** eS ! ! 5 HV]]__8 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5s5B,AB B,B B,B B,,B03B0returncdtjvrtdStdS)Ndebianz/var/spool/cron/crontabsz/var/spool/cron)distrolikesplitr rr _crontab_pathrSss=6;==&&((((./// ! " ""rrcJt|jtkSN)r parentrS)rs r is_crontabrWzs ::   //r) _race_srcrGsrcdstc tj|\}}tj|\}}t5}|t |} t || } |t|t| } |t |} t|} | rhtj | j tj zrBtt5tj|| dddn #1swxYwY|t!|| t"| \}} t%t&| j| jt,| | j || | }t/d5}||}|dddn #1swxYwY|r |tj|| | r>tj|| j| j| tj|| j | ddddS#1swxYwYdS)NrrK) max_workers)r rrQr enter_contextr*r8r"R_FLAGSrWr3r5S_ISVTXrFileNotFoundErrorunlinkr;rCrrst_uidst_gidrJrsubmitresultchownr4)rYrZrXrGsrc_dirsrc_namedst_dirdst_namestack src_dir_fdsrc_str<r?safe_dstr>fnexecutorfutures r safe_moverr~s c**GX c**GX )?((W)=)=>> x44$$WXw %K%KLL((W)=)=>> c??  7 ++3dlB 7+,, 7 7 (:6666 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7"// xWh ? ?  z  M M   N      !Q / / / 8__R((F MMOOO                  IKKK (:....  ? HUFM6= L L L L HUFN: > > > >S)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?s]C I'D?3 I'?E I'E A0I'7*G-! I'-G1 1I'4G1 5A%I''I+.I+rU)rN)&__doc__r rDr3concurrent.futuresr contextlibrrr functoolsrrpathlibr typingr r rOO_RDONLYr^O_TRUNCO_CREATO_WRONLYrCrr"r0r*intr1r8boolr;rJrSrWrrrRrrrs| 222222::::::::::(((((((("""""""" + *rz !BK /     c    sCx#$S S # c D    5 55 c?5 5555( 1#t#### 0S0T000004t-?-?-?3-?S-?-?-?-?-?-?r