}g'dZddlZddlZddlmZmZddlmZddlm Z ddl m Z ddl Z ddl mZddlmZmZdd lmZd ZGd d ZGd deZGddeZGddeZdS)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)MalwareScanResourceType)Message MessageType)loggeri,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__GsG t444   rcdSNr)r-exc_typeexc_valexc_tbs r__exit__zDetachedDir.__exit__Ms rc*t|jSr:)strr)r7s r__str__zDetachedDir.__str__Ps49~~rc,t|j|Sr:)getattrr))r-names r __getattr__zDetachedDir.__getattr__Ssty$'''rN) rrrr@__annotations__rr*r1r8r>rArErrrrr.sOOONNNNNNNNN(   E777    (((((rrceZdZdZeedeefdZe dZ dZ dZ dZ eddZefdefd Zd Zeed Zd Ze edefd ZedefdZdS)DetachedOperationNreturncdSr:rr7s rDETACHED_DIR_CLSz"DetachedOperation.DETACHED_DIR_CLS[  rczK|jj}g}tj|r|tj|5}|D]L}|r6|||j |M dddn #1swxYwYt|d{VdSr:) rKr 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.rK detached_dir)r-r.s rr1zDetachedOperation.__init__os)& 11$2BCCrc"|j|jkSr:)r.)r-others r__eq__zDetachedOperation.__eq__ss5#444rc*t|jSr:)hashr.r7s r__hash__zDetachedOperation.__hash__vsD$%%%rc KdSr:r)r-___s rhandle_aborted_processz(DetachedOperation.handle_aborted_process{  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)r\rQr debugr rr!is_filerget_pidFileNotFoundError ValueErrorstatst_mtimetimewarningr$rrpsutilProcessprocess_is_suitablerError)r- start_timer'errdetached_dir_mtimeprocs rget_detached_process_statez,DetachedOperation.get_detached_process_states '')) ( 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:)intr\r$ read_textr7s rrkzDetachedOperation.get_pids"4$-7799:::rcdSr:rr7s ron_complete_messagez%DetachedOperation.on_complete_messagerLrcK|}|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)rU)rzr rprocess_messagerrrf)r-rUoperation_states rrSz*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)rTrys rrtz%DetachedOperation.process_is_suitablerLrc KdSr:rr7s rcompletezDetachedOperation.completergr)rIN)rrrNAMEpropertyrrrrK classmethodrZr1r_rbrfPROCESS_START_TIMEr@rzrkrrSboolrtr rrrrrHrHXsz D  ${"3   ^X  # #[ #DDD555&&&    ^ 5G(%(%3(%(%(%(%T;;;  ^X 999, $   ^[     ^   rrHceZdZdZeedefdZeedefdZ eede fdZ eedefdZ ede fdZdS) DetachedScanscanrIcdSr:rr7s r RESOURCE_TYPEzDetachedScan.RESOURCE_TYPErLrcdSr:rr7s rprogresszDetachedScan.progressrLrcdSr:rr7s rphasezDetachedScan.phaserLrcdSr:rr7s rtotal_resourceszDetachedScan.total_resourcesrLrcBtj|j|jS)N)scan_id resource_type)r MalwareScanCompleter.rr7s rrz DetachedScan.on_complete_messages'.$D4F    rN)rrrrrrrrr|rr@rrr rrrrrrs D  6   ^X  #   ^X  s   ^X     ^X  W   X   rr)__doc__rNrpabcrrasynciorpathlibrtypingrrrimav.malwarelib.configr"defence360agent.contracts.messagesr r defence360agent.defence360r rr rrHrrrrrsw* ######## ::::::CCCCCCCC------'('('('('(#'('('(Tv v v v v v v v r     $     r