h RdZddlZddlZddlZddlZddlZddlmZddlm Z ddl m Z ddl m Z ddlmZddlmZdd lmZdd lmZdd lmZmZmZdd lmZdd lmZddlmZddl m!Z!dZ"dZ#dZ$ej%e&Z'Gdde(Z)e*Z+de*de+fdZ,ede*de+fdZ-e dde*fdZ.de*de fdZ/de+fdZ0de de+dzfdZ1de de+ddfd Z2ed!d"#de*fd$Z3e$fd%e4e*efd&e5de4e*e4e*e5fffd'Z6dS)(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) defaultdict) lru_cache)islice)Path)Any) urlencode)IndependentAgentIDAPI) HostingPanel)async_lru_cachelog_error_and_ignore safe_fileops)IMUNIFY_PATCH_PURCHASE_URL)VulnerabilityAPI)VulnerabilityHitStatus)VulnerabilityHitz.imunify_patch_id ceZdZdS)ImunifyPatchIdErrorN)__name__ __module__ __qualname__T/opt/imunify360/venv/lib/python3.11/site-packages/imav/contracts/imunify_patch_id.pyrr5sDrrusernamereturncKt|d{V}t|x}r|St}t||d{V|S)aEnsure the Imunify.Patch ID file exists for the given user. This function checks if the Imunify.Patch ID file exists in the user's home directory. If it does not exist, it generates a new ID, writes it to the file, and returns the ID. If the file already exists, it reads and returns the existing ID. Args: username (str): The username for which to ensure the ID file. Returns: ImunifyPatchUserId: The Imunify.Patch user ID. N) _get_id_file _read_id_file _generate_id_write_id_file)rid_file_ids rensure_id_filer%<sq!********GG$$$s ..C # & &&&&&&&& JrcKt|4d{Vt|d{Vcdddd{VS#1d{VswxYwYdSN)get_lockr%rs rget_imunify_patch_idr*Rs!!........#H--------..............................sA A  A )maxsizec(tjSr')asyncioLockr)s rr(r(Xs <>>rcK tj|}t|jtz }|s|js9td|jtd|j tj t|d{Vn#ttf$r.}tdt|t|d}~wwxYwnB#t$r5}td|td||d}~wwxYw|S)zEGet a file with Imunify.Patch user id and create it if does not existzNo such user homedir: Nz$Unable to put %s in user home dir %szNo such user: z No such user )pwdgetpwnamrpw_dirIMUNIFY_PATCH_ID_FILEexistsparentloggererrorrr touchstrPermissionErrorOSErrorKeyError)ruser_pwdr#es rrr]sx1<)) x''*??~~ 1>((**  FgnFFGGG)=W^== 1"(W6666666666#W- 1 1 1 :) *q0  1 1 EEE 0h00111!"<("<"<==1DE( Ns/D'CD)C==D E0EEc2tjjS)zGenerate Imunify.Patch id)uuiduuid4hexrrrr!r!ys :<< rr#c`|d5}t|D]?}|r;|ds&|x}r|ccdddS@ dddn #1swxYwYt d|ddS)zQRead Imunify.Patch id from `id_file`. If id is not found, return `None`. r#Nz Cannot parse z, file is corrupted or empty)openreversed readlines startswithstripr6warning)r#flineimunify_patch_ids rr r ~s c  ,aQ[[]]++ , ,D ,DOOC00 ,'+zz||3#,+++ ,,,,,,,, ,,,,,,,,,,,,,,,,  NNH7HHHIII 4sAB7BB  B r$cKd|d} tjt||d{VdS#ttf$r.}t dt|t|d}~wwxYw)z$Write Imunify.Patch id to `id_file`.zI# DO NOT EDIT # This file contains Imunify.Patch id unique to this user  Nz'Unable to write %s in user home dir: %s) r write_textr9r;r:r6r7r3r)r#r$textr>s rr"r"s      )%c'llD99999999999 _ %))) 57La   "q( )s(4A3)A..A3d<)r+ttlc  Ktj}t|d{V}t}|}|d{V|g t } fd|d{V D tj tj |ktjtjkz} fd|D}t#t%jtjd|Dd{V}t+||pd||d dt.|t|t1j|dd} t4d | S) Nc$i|] \}}|v || Srr).0domainpaths user_domainss r z2get_imunify_patch_purchase_url..s4 FE \ ! !  ! ! !rcg|]=}D]&\}}|D]}|j||'>Sr)items orig_filerI)rXhitrY doc_rootspathuser_domain_pathss r z2get_imunify_patch_purchase_url..s} !2!8!8!:!: FI   = # #D ) ) rc6g|]}|Sr)as_dict)rXr`s rrdz2get_imunify_patch_purchase_url..s ///3///r,T) sort_keys)iaidimunify_patch_user_id server_iprwebsitestotal_websitesvulnerable_domainsvulnerabilities?)r get_iaidr*r get_server_ipget_domains_per_usergetlenget_domain_pathsr^rselectwhereuserstatusr VULNERABLEgroup_by_severityr get_detailsget_vulnerabilities_idsrjoinPURCHASE_URL_MAX_WEBSITESjsondumpsr) rrjrk panel_managerrlrnhitsrorpurl_argsrcr[s @@rget_imunify_patch_purchase_urlrs3 ! ) + +D"6x"@"@@@@@@@ NNM++--I'<<>>>>>>>>CC"L&&N$1$B$B$D$DDDDDDDKKMM  " $ $ * *  ( *  "&<&G G I  D (*  4//$///          O%:%@b" .H/H.H!IJJ,"%&8"9"9#z/TJJJ   H) 5 58 5 55rrplimitctd}t||D]:}|d}|dd}|dvr|||xxdz cc<;t |S)Nc*ttSr')rintrrrz#group_by_severity..sS!1!1rappseverityUNKNOWN)HIGHMEDIUMLOWr)rrvaluesrudict)rprresultitemapp_namers rr}r}s11 2 2F--//77,,;88J 22 ; ; ; 8 X & & &! + & & & <<r)7__doc__r-rloggingr0r@ collectionsr functoolsr itertoolsrpathlibrtypingr urllib.parserdefence360agent.internals.iaidr +defence360agent.subsys.panels.hosting_panelr defence360agent.utilsr r r imav.contracts.configr!imav.malwarelib.api.vulnerabilityrimav.malwarelib.configrimav.malwarelib.modelrr3rMAX_SEVERITY_COUNT getLoggerrr6 Exceptionrr9ImunifyPatchUserIdr%r*r(rr!r r"rrrr}rrrrsU*  ######""""""@@@@@@DDDDDD =<<<<<>>>>>>999999222222+  8 $ $     )   3+=,..1C....  4s8( 4 $6$=    )$)-?)D))))""%%%76c767676&%76x$  #s(^    #tCH~       r