Zf&%edZddlZddlZddlmZmZddlmZddlm Z ddl m Z ddl Z ddl mZddlmZdd lmZdd lmZd ZGd d ZGddeZGddeZGddeeZdS)u  This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program.  If not, see . Copyright © 2019 Cloud Linux Software Inc. This software is also available under ImunifyAV commercial license, see N)ABCabstractmethod)gather)Path)Type)Message)logger)MalwareScanComplete)MalwareScanResourceTypei,c"eZdZdZdZdZdZdZdS) DetachedStatefinishedabortedrunningno_dir no_processN)__name__ __module__ __qualname__FINISHEDABORTEDRUNNINGNO_DIR NO_PROCESSR/opt/imunify360/venv/lib/python3.11/site-packages/imav/malwarelib/scan/detached.pyr r 's'HGG FJJJrr czeZdZUeed<eed<eed<eed<eed<eed<dddddd Zd Zd Zd Z d Z dZ dS) DetachedDir DETACHED_DIR done_fileerr_filelog_filepid_file progress_filedonepid)r!r"r#r$r%ct|j||_|jD]\}}t |||j|z dS)zB NOTE: Initialization should not create any files N)rr pathFILESitemssetattr)self detached_idattr file_names r__init__zDetachedDir.__init__@s_*K88 #z//11 7 7OD) D$ I 5 6 6 6 6 7 7rc|jdd|j|j|S)NT)parentsexist_ok)r)mkdirr#touchr"r-s r __enter__zDetachedDir.__enter__HsG t444   rcdSNr)r-exc_typeexc_valexc_tbs r__exit__zDetachedDir.__exit__Ns rc*t|jSr:)strr)r7s r__str__zDetachedDir.__str__Qs49~~rc,t|j|Sr:)getattrr))r-names r __getattr__zDetachedDir.__getattr__Tsty$'''rN) rrrr@__annotations__rr*r1r8r>rArErrrrr/sOOONNNNNNNNN(   E777    (((((rrceZdZUdZeeed<edZdZ dZ dZ e ddZ efdefd Zd Zee defd Zd Zee defd Ze defdZdS)DetachedOperationNDETACHED_DIR_CLSczK|jj}g}tj|r|tj|5}|D]L}|r6|||j |M dddn #1swxYwYt|d{VdSr:) rIr osr)existsscandiris_dirappendrDcheck_detached_operationr)clssink restore_dir check_corositentrys rcheck_detached_operation_dirz.DetachedOperation.check_detached_operation_dir]s *7  7>>+ & & K(( BE||~~#**C OODDTJJ               k""""""""""sAB!!B%(B%cR||_||j|_dSr:)r.rI detached_dir)r-r.s rr1zDetachedOperation.__init__ls)& 11$2BCCrc"|j|jkSr:)r.)r-others r__eq__zDetachedOperation.__eq__ps5#444rc*t|jSr:)hashr.r7s r__hash__zDetachedOperation.__hash__ssD$%%%rreturnc KdSr:r)r-___s rhandle_aborted_processz(DetachedOperation.handle_aborted_processx  rcH|js&tjd|jtjS|jjr tjS | }n#ttf$r}|j j }tj|z |kr1tjd|jj|tjcYd}~StjcYd}~Sd}~wwxYw t'j|}||r tjStjd|n9#tt&jf$r }tjd||Yd}~nd}~wwxYwtjS)NzNo such scan directory: %sz;Pid file wasn't created or pid file is empty: %s, error: %szBThe running process (pid=%s) does not match the detached operationz=Can't get the process for the detached operation (pid=%s): %s)rYrNr debugr rr!is_filerget_pidFileNotFoundError ValueErrorstatst_mtimetimewarningr$rrpsutilProcessprocess_is_suitablerError)r- start_timer'errdetached_dir_mtimeprocs rget_detached_process_statez,DetachedOperation.get_detached_process_state|s '')) ( L5t7H I I I ' '   & . . 0 0 * ) ) ,,,..CC!:. , , ,!%!2!7!7!9!9!B y{{//*<<!%.  %,,,,,,, + + + + + + + , >#&&D''-- $,,- "6<0    N           $$sI+BDA#D 4D: D D D4E EF5FFcXt|jjSr:)intrYr$ read_textr7s rrizDetachedOperation.get_pids"4$-7799:::rcdSr:rr7s ron_complete_messagez%DetachedOperation.on_complete_message  rcK|}|tjkr"||jd{VdS|tjkr||d{VdSdS)ahUpdates operation to a consistent state. Algorithm: 1) If scan_dir doesn't exist: - scan_dir doesn't already create - scan ended and MalwareScanComplete is in process 2) pid file doesn't exist: - since scan_dir mtime has passed more than 5 minutes -> something went wrong & handle_aborted_process - ... less than 5 minutes -> waiting for ai-bolit 3) done file exists -> scan completed 4) done file doesn't exist: - process executes -> scan running - process dead -> scan aborted N)rR)rxr rprocess_messager}rrd)r-rRoperation_states rrPz*DetachedOperation.check_detached_operations 99;; m4 4 4&&t'?@@ @ @ @ @ @ @ @ @ @  5 5 5--4-88 8 8 8 8 8 8 8 8 86 5rcdSr:r)rQrws rrrz%DetachedOperation.process_is_suitabler~rc KdSr:rr7s rcompletezDetachedOperation.completerer)r`N)rrrNAMErrrF classmethodrWr1r\r_rrdPROCESS_START_TIMEr@rxripropertyrr}rPboolrrrrrrrHrHYsc D;'''' # #[ #DDD555&&&    ^ 5G(%(%3(%(%(%(%T;;; W   ^X 999, $   ^[     ^   rrHceZdZdZeedefdZeedefdZ eede fdZ eedefdZ ede fdZdS) DetachedScanscanr`cdSr:rr7s r RESOURCE_TYPEzDetachedScan.RESOURCE_TYPEr~rcdSr:rr7s rprogresszDetachedScan.progressr~rcdSr:rr7s rphasezDetachedScan.phaser~rcdSr:rr7s rtotal_resourceszDetachedScan.total_resourcesr~rc8t|j|jS)N)scan_id resource_type)r r.rr7s rr}z DetachedScan.on_complete_messages$"$D4F    rN)rrrrrrr rrzrr@rrrr}rrrrrs D  6   ^X  #   ^X  s   ^X     ^X  W   X   rr)__doc__rKrnabcrrasynciorpathlibrtypingrrp"defence360agent.contracts.messagesrdefence360agent.defence360r imav.contracts.messagesr imav.malwarelib.configr rr rrHrrrrrs* ######## 666666------777777::::::'('('('('(#'('('(Tr r r r r r r r j     $c     r