HEX
Server: Apache
System: Linux zacp120.webway.host 4.18.0-553.50.1.lve.el8.x86_64 #1 SMP Thu Apr 17 19:10:24 UTC 2025 x86_64
User: govancoz (1003)
PHP: 8.3.26
Disabled: exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Upload Files
File: //opt/alt/python37/lib/python3.7/site-packages/ssa/__pycache__/manager.cpython-37.pyc
B

V�d�7�@s�dZddlZddlZddlZddlZddlZddlZddlmZddl	m
Z
ddlmZddl
mZddlmZdd	lmZmZdd
lmZddlmZddlmZdd
lmZddlmZGdd�d�Zdd�dd�ZdS)zA
This module contains classes implementing SSA Manager behaviour
�N)�contextmanager)�iglob)�is_cl_solo_edition)�
disable_quota)�Tuple�)�load_validated_parser�load_configuration)�	flag_file)�SSAManagerError)�ssa_version)�
AutoTracer)�
DecisionMakerc@s�eZdZdZdd�Zedd�dd��Zeed�dd	��Z	ee
d�d
d��Zee
d�dd
��Ze
e
d�dd�ZdAeejd�dd�Ze
dd�dd�Zdd�dd�Zdd�dd�Zdd�dd�Zdd�dd�Zdd�d d!�Zeed"�d#d$�Zeeeefefd�d%d&�Zed'd(��Zeeed)d*�d+d,�Z d)d�d-d.�Z!eeeefefd�d/d0�Z"d)d�d1d2�Z#d)d�d3d4�Z$d)d�d5d6�Z%ed�d7d8�Z&d)d�d9d:�Z'd)d�d;d<�Z(dd�d=d>�Z)d)d�d?d@�Z*d)S)B�Managerz
    SSA Manager class.
    cCsDt�d�|_d|_d|_d|_tddd�d�f|_tt	t
jf|_dS)	NZmanagerzclos_ssa.ini)Zphp44Zphp51Zphp52Zphp53zphp\d+-imunifyzphp-internal)z /opt/alt/php[0-9][0-9]/link/confz+/opt/cpanel/ea-php[0-9][0-9]/root/etc/php.dz$/opt/plesk/php/[0-9].[0-9]/etc/php.dz'/usr/local/php[0-9][0-9]/lib/php.conf.dzM/usr/share/cagefs/.cpanel.multiphp/opt/cpanel/ea-php[0-9][0-9]/root/etc/php.dzA/usr/share/cagefs-skeleton/usr/local/php[0-9][0-9]/lib/php.conf.dz./var/cagefs/*/*/etc/cl.php.d/alt-php[0-9][0-9]cSst�|�d�d�S)N�/�)�pwd�getpwnam�split)�path�r�</opt/alt/python37/lib/python3.7/site-packages/ssa/manager.py�<lambda>6�z"Manager.__init__.<locals>.<lambda>)r�user)
�loggingZ	getLogger�logger�
ini_file_name�substrings_to_exclude_dir_paths�wildcard_ini_locations�dict�wildcard_ini_user_locations�OSError�
ValueError�
subprocessZSubprocessError�subprocess_errors)�selfrrr�__init__%s	zManager.__init__zjson str)�returncOs*ddi}|�dd�|��D��t�|�S)z@
        Form a success json response with given kwargs
        �result�successcSsi|]\}}||�qSrr)�.0�k�vrrr�
<dictcomp>Bsz$Manager.response.<locals>.<dictcomp>)�update�items�json�dumps)�args�kwargsZraw_responserrr�response<szManager.responsecCstj�t�S)z 
        Is SSA enabled
        )�osr�isfiler
)r&rrr�_enabledEszManager._enabledcCsddhS)zK
        Configuration settings required Request Processor restart
        Zrequests_durationZignore_listr)r&rrr�_restart_required_settingsLsz"Manager._restart_required_settingscCsdddddhS)NZcorrelationZcorrelation_coefficientZrequest_numberZtimeZdomains_numberr)r&rrr�solo_filtered_settingsSszManager.solo_filtered_settings)�settingsr(cCs|j�|�S)z�
        SSA Agent requires restart in case of changing these configuration:
            - requests_duration
            - ignore_list
        )r9�intersection)r&r;rrr�_restart_requiredXszManager._restart_requiredF)�commandr(c
Csy0tjdd|gdd|d�}|j�d|�d��Wn�tjk
r�}zd|jjdt|j�t|j�t|j	�|j|j|j	|j
d�d	�td
|j�d|j�d|j	p�|j
����Wd
d
}~XYnX|jk
�r
}z6|jjdt|�dt|�id	�td|�d|����Wd
d
}~XYnX|S)z�
        Run /sbin/service utility to make given operation with SSA Agent service
        :command: command to invoke
        :check_retcode: whether to run with check or not
        :return: subprocess info about completed process
        z
/sbin/servicez	ssa-agentT)Zcapture_output�textZcheckz
ssa-agent z
 succeededz$SSA Agent %s failed with code %s: %s)�cmdZretcode�stdout�stderr)�extraz
SSA Agent z failed with code z: Nz&Failed to run %s command for SSA Agent�errzFailed to run z for SSA Agent: )
r$�runr�infoZCalledProcessError�error�strr@�
returncoderArBrr%)r&r>�
check_retcoder)�errr�run_service_utility`s.4$zManager.run_service_utility)r3r(c
Cs�t�}|�|�y|��WnHtk
rd}z*|jjddt|�id�td|����Wdd}~XYnX|�|�r~|j	ddd�|�
�S)	z�
        Change SSA config and restart it.
        :args: dict to override current option values
        :return: JSON encoded result of the action
        z Failed to update SSA config filerD)rCz"Failed to update SSA config file: N�restartT)rJ)r�overrideZwrite_ssa_confr"rrGrHrr=rLr5)r&r3�configrKrrr�
set_config�s
 
zManager.set_configcs>t�}tdd�r2�fdd�|��D�}�j|d�S�j|d�S)zV
        Get current SSA config.
        :return: JSON encoded current config
        T)Zskip_jwt_checkcs i|]\}}|�jkr||�qSr)r:)r+�key�value)r&rrr.�sz&Manager.get_config.<locals>.<dictcomp>)rO)r	rr0r5)r&Zfull_configZfiltered_configr)r&r�
get_config�s

zManager.get_configcCs|jr
dnd}|j|d�S)zY
        Get current status of SSA.
        :return: JSON encoded current status
        �enabled�disabled)Z
ssa_status)r8r5)r&�statusrrr�get_ssa_status�szManager.get_ssa_statuscCs2|jrtddd��|��|��|��|��S)a�
        Enable SSA:
            - add clos_ssa extension for each PHP version on server
            - add clos_ssa extension into cagefs for each user and each ver
            - start SSA Agent (if it is not already started)
            - restart Apache (etc.) and FPM, reset CRIU images
            - create flag_file indicating that SSA is enabled successfully
        :return: JSON encoded current status
        zSSA is already enabled�warning)�flag)r8r�
generate_inis�start_ssa_agent�create_flagr5)r&rrr�
enable_ssa�s
zManager.enable_ssacCs2|jstddd��|��|��|��|��S)a{
        Disable SSA:
            - remove clos_ssa extension for each PHP version on server
            - remove clos_ssa extension from cagefs for each user and each ver
            - stop SSA Agent
            - restart Apache (etc.) and FPM, reset CRIU images
            - remove flag_file indicating that SSA is enabled
        :return: JSON encoded current status
        zSSA is already disabledrX)rY)r8r�remove_clos_inis�stop_ssa_agent�remove_flagr5)r&rrr�disable_ssa�s
zManager.disable_ssacCs>dd�t���D�}|j|t�|jr(dnd|��t���d�S)z�
        Get SSA statistics.
        Includes:
        - config values
        - version
        - SSA status (enabled|disabled)
        - SSA Agent status (active|inactive)
        :return: JSON encoded current statistics
        cSsi|]\}}t|���|�qSr)rH�lower)r+rQrRrrrr.�sz%Manager.get_stats.<locals>.<dictcomp>rTrU)rO�versionrV�agent_statusZautotracing)r	r0r5rr8�status_ssa_agentr
�	get_stats)r&Z_configrrrrf�s
zManager.get_stats)�dir_pathr(cs�fdd�|jD�}|S)z6
        Checking for substrings in a string.
        csg|]}t�|��r|�qSr)�re�search)r+Z	substring)rgrr�
<listcomp>�sz+Manager.unused_dir_path.<locals>.<listcomp>)r)r&rg�resr)rgr�unused_dir_path�szManager.unused_dir_pathc	cs�x6|jD],}x&t|�D]}|�|�r&qd|fVqWqWxp|jD]f}x`t|d�D]P}|�|�rbqRy|d|�}Wn|j�d|�wRYqRX|j|jf|fVqRWq@WdS)z�
        Generator of existing paths (matching known wildcard locations)
        for additional ini files
        Returns tuple of (uid, gid) and path.
        )rrrrzhUnable to get information about user owning %s directory (maybe he`s already terminated?), skip updatingN)rrrlr!rrFZpw_uidZpw_gid)r&�locationrgZ	pw_recordrrr�existing_paths�s 

zManager.existing_pathsc	cs:zt�|�t�|�dVWdt�d�t�d�XdS)z�
        Dive into user context by dropping permissions
        to avoid most of the security issues.

        Does not cover cagefs case because it also requires nsenter,
        which is only available with execve() call in our system
        Nr)r6�setegid�seteuid)r&�uid�gidrrr�
_user_contexts	



zManager._user_contextN)rqrr�ini_pathr(cCsltj�||j�}|�||��Ft��4t|d��}|j�d|�|�	d�WdQRXWdQRXWdQRXdS)zB
        Enable SSA extension for single ini_path (given)
        �wzGenerating %s file...zextension=clos_ssa.soN)
r6r�joinrrsr�openrrF�write)r&rqrrrtrZinirrr�generate_single_iniszManager.generate_single_inicCs�|j�d�x�|��D]z\\}}}y|�|||�Wqtk
rX|j�d|�wYqtk
r�}z|j�d|t|��wWdd}~XYqXqW|j�d�dS)zj
        Place clos_ssa.ini into each existing Additional ini path,
        including cagefs ones
        z Generating clos_ssa.ini files...z>Unable to update file %s, possible permission misconfigurationz7Exception on generating clos_ssa.ini: "%s", error: "%s"Nz	Finished!)rrFrnry�PermissionError�	ExceptionrGrH)r&rqrrrtrKrrrrZszManager.generate_inisccsVxP|��D]D\\}}}x6t�|�D](}|j|kr2q"||ftj�||�fVq"Wq
WdS)z�
        Generator function searching for clos_ssa.ini files
        in all existing Additional ini paths
        Returns tuple of (uid, gid) and path.
        N)rnr6�listdirrrrv)r&rqrrrt�namerrr�find_clos_inis/s

zManager.find_clos_iniscCs�|j�d�xx|��D]l\\}}}y&|�||��t�|�WdQRXWqtk
r�}z|j�d|t|��wWdd}~XYqXqW|j�d�dS)z8
        Remove all gathered clos_ssa.ini files
        zRemoving clos_ssa.ini files...Nz5Exception on removing clos_ssa.ini: "%s", error: "%s"z	Finished!)	rrFr~rsr6�unlinkr{Z	exceptionrH)r&rqrrZclos_inirKrrrr^;szManager.remove_clos_iniscCs2|�d�}|jr |jddd�n|jddd�dS)ze
        Start SSA Agent service
        or restart it if it is accidentally already running
        rV�startT)rJrMN)rLrI)r&rdrrrr[Ks
zManager.start_ssa_agentcCs"|�d�}|js|jddd�dS)z`
        Stop SSA Agent service
        or do nothing if it is accidentally not running
        rV�stopT)rJN)rLrI)r&rdrrrr_Vs
zManager.stop_ssa_agentcCs,y|jddd�Wntk
r&dSXdS)z:
        Get SSA Agent status: active or inactive
        rVT)rJZinactiveZactive)rLr)r&rrrre_s
zManager.status_ssa_agentc	Cs.ttd��WdQRX|j�dt�d��dS)zE
        Create a flag file indicating successful enablement
        ruNz
Flag file z created)rwr
rrF)r&rrrr\is
zManager.create_flagc
Csfy"t�t�|j�dt�d��Wn>tk
r`}z |j�dt�dt|����Wdd}~XYnXdS)z:
        Remove a flag file indicating enablement
        z
Flag file z removedz removal failed: N)r6rr
rrFr"rXrH)r&rKrrrr`qs
zManager.remove_flagcCst���}|jf|�S)zG
        Get last report.
        :return: JSON encoded report
        )rZget_json_reportr5)r&Zreportrrr�
get_report|s
zManager.get_reportcCs|jr|��dS)z@
        Regenerates clos_ssa inis while SSA is enabled
        N)r8rZ)r&rrr�regenerate_inis�szManager.regenerate_inis)F)+�__name__�
__module__�__qualname__�__doc__r'�staticmethodr5�property�boolr8�setr9r:r r=rHr$ZCompletedProcessrLrPrSrWr]rarf�listrlr�intrnrrsryrZr~r^r[r_rer\r`r�r�rrrrr s@		
rzManager instance)r(cCst�S)zk
    Factory function for appropriate manager initialization
    :return: appropriate manager instance
    )rrrrr�initialize_manager�sr�) r�r1rr6rrhr$Z
contextlibrZglobrZclcommon.lib.cleditionrZsecureior�typingrZ
configurationrr	Zinternal.constantsr
Zinternal.exceptionsrZinternal.utilsrZmodules.autotracerr
Zmodules.decision_makerrrr�rrrr�<module>
s*n