Hacked By AnonymousFox

Current Path : /opt/cloudlinux/venv/lib/python3.11/site-packages/ssa/__pycache__/
Upload File :
Current File : //opt/cloudlinux/venv/lib/python3.11/site-packages/ssa/__pycache__/manager.cpython-311.pyc

�

qu�f�5����dZddlZddlZddlZddlZddlZddlZddlmZddl	m
Z
ddlmZddl
mZddlmZmZdd	lmZdd
lmZddlmZddlmZdd
lmZGd�d��Zdd�ZdS)zA
This module contains classes implementing SSA Manager behaviour
�N)�contextmanager)�iglob)�
disable_quota)�Tuple�)�load_validated_parser�load_configuration)�	flag_file)�SSAManagerError)�ssa_version)�
AutoTracer)�
DecisionMakerc���eZdZdZd�Zedefd���Zede	fd���Z
edefd���Zedefd���Z
dedefd	�Z	d'dedejfd�Zd
edefd�Zdefd�Zdefd�Zdefd�Zdefd�Zdefd�Zdedefd�Zdeeeefeffd�Zed���Zdedededdfd�Z d(d�Z!deeeefeffd�Z"d(d�Z#d(d �Z$d(d!�Z%defd"�Z&d(d#�Z'd(d$�Z(defd%�Z)d(d&�Z*dS))�Managerz
    SSA Manager class.
    c���tjd��|_d|_d|_d|_t
dd����f|_tttjf|_dS)N�managerzclos_ssa.ini)�php44�php51�php52�php53zphp\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]c�\�tj|�d��d��S)N�/�)�pwd�getpwnam�split)�paths �B/opt/cloudlinux/venv/lib64/python3.11/site-packages/ssa/manager.py�<lambda>z"Manager.__init__.<locals>.<lambda>5s��3�<��
�
�3����0B�#C�#C��)r�user)
�logging�	getLogger�logger�
ini_file_name�substrings_to_exclude_dir_paths�wildcard_ini_locations�dict�wildcard_ini_user_locations�OSError�
ValueError�
subprocess�SubprocessError�subprocess_errors��selfs r�__init__zManager.__init__$sv���'�	�2�2���+���0
��,�'
��#�
�F�C�C�
E�
E�
E�,
��(�

�Z��!;�"
����r �returnc��ddi}|�d�|���D����tj|��S)z@
        Form a success json response with given kwargs
        �result�successc��i|]\}}||��	S�r7)�.0�k�vs   r�
<dictcomp>z$Manager.response.<locals>.<dictcomp>As��=�=�=�d�a��Q��=�=�=r )�update�items�json�dumps)�args�kwargs�raw_responses   r�responsezManager.response;sI��
!�)�,�����=�=�f�l�l�n�n�=�=�=�>�>�>��z�,�'�'�'r c�J�tj�t��S)z 
        Is SSA enabled
        )�osr�isfiler
r/s r�_enabledzManager._enabledDs��
�w�~�~�i�(�(�(r c�
�ddhS)zK
        Configuration settings required Request Processor restart
        �requests_duration�ignore_listr7r/s r�_restart_required_settingsz"Manager._restart_required_settingsKs��
$�]�3�3r c�
�hd�S)N>�time�correlation�domains_number�request_number�correlation_coefficientr7r/s r�solo_filtered_settingszManager.solo_filtered_settingsRs��*�*�*�	*r �settingsc�6�|j�|��S)z�
        SSA Agent requires restart in case of changing these configuration:
            - requests_duration
            - ignore_list
        )rK�intersection)r0rSs  r�_restart_requiredzManager._restart_requiredWs���.�;�;�H�E�E�Er F�commandc��	tjdd|gdd|���}|j�d|�d����n#tj$r�}|j�dt
|j��t
|j��t
|j	��|j|j|j	|j
d��	��td
|j�d|j�d|j	p|j
�����d
}~w|j$rS}|j�dt
|��dt
|��i�	��td|�d|�����d
}~wwxYw|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)�capture_output�text�checkz
ssa-agent z
 succeededz$SSA Agent %s failed with code %s: %s)�cmd�retcode�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$�info�CalledProcessError�error�strr\�
returncoder^r_rr.)r0rW�
check_retcoder4�es     r�run_service_utilityzManager.run_service_utility_s���	?��^�_�%0�%,�%.�48�d�*7�	9�9�9�F�

�K���=�'�=�=�=�>�>�>�>���,�		^�		^�		^��K���6��A�E�
�
��A�L�!�!��A�H�
�
��e���!"��Q�X�?�?�
�
@�
@�
@�"�\�Q�U�\�\�a�l�\�\�a�h�FZ�RS�RZ�\�\�^�^�
^������%�	?�	?�	?��K���F��G���%*�C��F�F�O�
�
5�
5�
5�!�=��=�=�!�=�=�?�?�
?�����	?����
�
s#�9=�E�BC(�(
E�5AE�Er@c��t��}|�|��	|���nO#t$rB}|j�ddt
|��i���td|�����d}~wwxYw|�|��r|�	dd���|�
��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 filerbr`z"Failed to update SSA config file: N�restartT�ri)r�override�write_ssa_confr*r$rfrgrrVrkrC)r0r@�configrjs    r�
set_configzManager.set_configs���'�(�(���������	L��!�!�#�#�#�#���	L�	L�	L��K���@�%*�C��F�F�O�
�
5�
5�
5�!�"J�q�"J�"J�K�K�K�����	L����
�!�!�$�'�'�	D��$�$�Y�d�$�C�C�C��}�}���s�:�
B�=B�Bc�J�t��}|�|���S)zV
        Get current SSA config.
        :return: JSON encoded current config
        )rq)r	rC)r0�full_configs  r�
get_configzManager.get_config�s#��
)�*�*���}�}�K�}�0�0�0r c�D�|jrdnd}|�|���S)zY
        Get current status of SSA.
        :return: JSON encoded current status
        �enabled�disabled)�
ssa_status)rGrC)r0�statuss  r�get_ssa_statuszManager.get_ssa_status�s(��
#�m�;������}�}��}�/�/�/r c��|js<|���|���|���|���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
        )rG�
generate_inis�start_ssa_agent�create_flagr{r/s r�
enable_ssazManager.enable_ssa�sV���}�	���� � � �� � �"�"�"��������"�"�$�$�$r c��|jr<|���|���|���|���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
        )rG�remove_clos_inis�stop_ssa_agent�remove_flagr{r/s r�disable_ssazManager.disable_ssa�sV���=�	��!�!�#�#�#����!�!�!��������"�"�$�$�$r c��d�t�����D��}|�|t��|jrdnd|���t
��������S)z�
        Get SSA statistics.
        Includes:
        - config values
        - version
        - SSA status (enabled|disabled)
        - SSA Agent status (active|inactive)
        :return: JSON encoded current statistics
        c�X�i|]'\}}|t|�������(Sr7)rg�lower)r8�key�values   rr;z%Manager.get_stats.<locals>.<dictcomp>�s<��1�1�1�z�s�E�3��E�
�
�(�(�*�*�1�1�1r rwrx)rq�versionrz�agent_status�autotracing)r	r=rCrrG�status_ssa_agentr
�	get_stats)r0�_configs  rr�zManager.get_stats�s���1�1�%�'�'�-�-�/�/�1�1�1���}�}���M�M� $�
�=�9�9�:��.�.�0�0�"���.�.�0�0��
�
�	
r �dir_pathc�.���fd�|jD��}|S)z6
        Checking for substrings in a string.
        c�>��g|]}tj|����|��Sr7)�re�search)r8�	substringr�s  �r�
<listcomp>z+Manager.unused_dir_path.<locals>.<listcomp>�s9���2�2�2�Y��)�I�x�0�0�2�y�2�2�2r )r&)r0r��ress ` r�unused_dir_pathzManager.unused_dir_path�s2���2�2�2�2�$�*N�2�2�2���
r c#�vK�|jD]0}t|��D]}|�|��r�d|fV���1|jD]v}t|d��D]^}|�|��r�	|d|��}|j|jf|fV��=#|j�d|��Y�\xYw�wdS)z�
        Generator of existing paths (matching known wildcard locations)
        for additional ini files
        Returns tuple of (uid, gid) and path.
        )rrrr!zhUnable to get information about user owning %s directory (maybe he`s already terminated?), skip updatingN)r'rr�r)�pw_uid�pw_gidr$rd)r0�locationr��	pw_records    r�existing_pathszManager.existing_paths�s0�����3�	'�	'�H�!�(�O�O�
'�
'���'�'��1�1����h�&�&�&�&�&�
'�
�8�	I�	I�H�!�(�6�"2�3�3�
I�
I���'�'��1�1���I� 0��� 0�� :� :�I�%�+�Y�-=�>��H�H�H�H�H��
��K�$�$�&5�6>�@�@�@��H����
I�	I�	Is�2B�B5c#�K�	tj|��tj|��dV�tjd��tjd��dS#tjd��tjd��wxYw)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)rE�setegid�seteuid)r0�uid�gids   r�
_user_contextzManager._user_context�sn����	��J�s�O�O�O��J�s�O�O�O��E�E�E��J�q�M�M�M��J�q�M�M�M�M�M��
�J�q�M�M�M��J�q�M�M�M�M���s�,A�*Br�r��ini_pathNc��tj�||j��}|�||��5t��5t
|d��5}|j�d|��|�	d��ddd��n#1swxYwYddd��n#1swxYwYddd��dS#1swxYwYdS)zB
        Enable SSA extension for single ini_path (given)
        �wzGenerating %s file...zextension=clos_ssa.soN)
rEr�joinr%r�r�openr$rd�write)r0r�r�r�r�inis      r�generate_single_inizManager.generate_single_inis����w�|�|�H�d�&8�9�9��
�
�
��S�
)�
)�	/�	/����	/�	/��T�3���	/�#&��K���4�d�;�;�;��I�I�-�.�.�.�		/�	/�	/�	/�	/�	/�	/�	/�	/�	/�	/����	/�	/�	/�	/�	/�	/�	/�	/�	/�	/�	/�	/�	/�	/�	/����	/�	/�	/�	/�	/�	/�	/�	/�	/�	/�	/�	/�	/�	/�	/�	/����	/�	/�	/�	/�	/�	/sY�C�B0�1B�
B0�B�B0� B�!B0�$C�0B4	�4C�7B4	�8C�C�Cc	��|j�d��|���D]�\\}}}	|�|||���!#t$r|j�d|��Y�Ht
$r3}|j�d|t|����Yd}~�d}~wwxYw|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"N�	Finished!)r$rdr�r��PermissionError�	Exceptionrfrg)r0r�r�r�rjs     rr}zManager.generate_iniss
��
	
����;�<�<�<�$(�$7�$7�$9�$9�		�		� �J�S�#��
��(�(��c�8�<�<�<�<��"�
�
�
��� � �"H�IQ�S�S�S����
�
�
���!�!�"[�]e�gj�kl�gm�gm�n�n�n����������
����	
�����%�%�%�%�%s�A�%B3�7	B3�)B.�.B3c#��K�|���D]O\\}}}tj|��D]2}|j|vr�||ftj�||��fV��3�PdS)z�
        Generator function searching for clos_ssa.ini files
        in all existing Additional ini paths
        Returns tuple of (uid, gid) and path.
        N)r�rE�listdirr%rr�)r0r�r�r��names     r�find_clos_iniszManager.find_clos_inis)s�����%)�$7�$7�$9�$9�	?�	?� �J�S�#���
�8�,�,�
?�
?���%�T�1�1���C�j�"�'�,�,�x��">�">�>�>�>�>�>�
?�	?�	?r c	��|j�d��|���D]�\\}}}	|�||��5t	j|��ddd��n#1swxYwY�L#t$r3}|j�d|t|����Yd}~��d}~wwxYw|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"r�)	r$rdr�r�rE�unlinkr��	exceptionrg)r0r�r��clos_inirjs     rr�zManager.remove_clos_inis5s.��	
����9�:�:�:�$(�$7�$7�$9�$9�	�	� �J�S�#��
��'�'��S�1�1�(�(��I�h�'�'�'�(�(�(�(�(�(�(�(�(�(�(����(�(�(�(����
�
�
���%�%�&]�_g�il�mn�io�io�p�p�p����������
����	
�����%�%�%�%�%s;�A;�A/�#A;�/A3	�3A;�6A3	�7A;�;
B8�)B3�3B8c��|�d��}|jr|�dd���dS|�dd���dS)ze
        Start SSA Agent service
        or restart it if it is accidentally already running
        rz�startTrnrmN�rkrh�r0r�s  rr~zManager.start_ssa_agentEsa��
�/�/��9�9���"�	D��$�$�W�D�$�A�A�A�A�A��$�$�Y�d�$�C�C�C�C�Cr c�p�|�d��}|js|�dd���dSdS)z`
        Stop SSA Agent service
        or do nothing if it is accidentally not running
        rz�stopTrnNr�r�s  rr�zManager.stop_ssa_agentPsN��
�/�/��9�9���&�	A��$�$�V�4�$�@�@�@�@�@�	A�	Ar c�Z�	|�dd���n#t$rYdSwxYwdS)z:
        Get SSA Agent status: active or inactive
        rzTrn�inactive�active)rkrr/s rr�zManager.status_ssa_agentYsJ��	��$�$�X�T�$�B�B�B�B���	�	�	��:�:�	�����xs��
(�(c��ttd��5	ddd��n#1swxYwY|j�dt�d���dS)zE
        Create a flag file indicating successful enablement
        r�N�
Flag file z created)r�r
r$rdr/s rrzManager.create_flagcs����)�S�
!�
!�	�	��	�	�	�	�	�	�	�	�	�	�	����	�	�	�	�����9�i�9�9�9�:�:�:�:�:s�$�(�(c	��	tjt��|j�dt�d���dS#t
$r=}|j�dt�dt|������Yd}~dSd}~wwxYw)z:
        Remove a flag file indicating enablement
        r�z removedz removal failed: N)rEr�r
r$rdr*�warningrg)r0rjs  rr�zManager.remove_flagks���	C��I�i� � � ��K���=�)�=�=�=�>�>�>�>�>���	C�	C�	C��K���A�Y�A�A��Q���A�A�
C�
C�
C�
C�
C�
C�
C�
C�
C�����	C���s�<A�
B�
2B�Bc�\�t�����}|jdi|��S)zG
        Get last report.
        :return: JSON encoded report
        r7)r�get_json_reportrC)r0�reports  r�
get_reportzManager.get_reportvs1��
���0�0�2�2���t�}�&�&�v�&�&�&r c�@�|jr|���dSdS)z@
        Regenerates clos_ssa inis while SSA is enabled
        N)rGr}r/s r�regenerate_iniszManager.regenerate_inis~s0���=�	!���� � � � � �	!�	!r )F)r2N)+�__name__�
__module__�__qualname__�__doc__r1�staticmethodrgrC�property�boolrG�setrKrRr(rVr,�CompletedProcessrkrrrur{r�r�r��listr�r�intr�rr�r�r}r�r�r~r�r�rr�r�r�r7r rrrse��������
�
�
�.�(�S�(�(�(��\�(��)�$�)�)�)��X�)��4�C�4�4�4��X�4��*��*�*�*��X�*�F�$�F�3�F�F�F�F�+0���3��4>�4O�����@�t�������&1�C�1�1�1�1�0��0�0�0�0�%�C�%�%�%�%�"%�S�%�%�%�%�"
�3�
�
�
�
�(��������I��e�C��H�o�s�&:� ;�I�I�I�I�4����^��"	/�s�	/��	/��	/��	/�	/�	/�	/�&�&�&�&�$
?��e�C��H�o�s�&:� ;�
?�
?�
?�
?�&�&�&�&� 	D�	D�	D�	D�A�A�A�A��#�����;�;�;�;�	C�	C�	C�	C�'�C�'�'�'�'�!�!�!�!�!�!r rr2�Manager instancec��t��S)zk
    Factory function for appropriate manager initialization
    :return: appropriate manager instance
    )rr7r r�initialize_managerr��s��
�9�9�r )r2r�)r�r>r"rErr�r,�
contextlibr�globr�secureior�typingr�
configurationrr	�internal.constantsr
�internal.exceptionsr�internal.utilsr�modules.autotracerr
�modules.decision_makerrrr�r7r r�<module>r�sS������������	�	�	�	�
�
�
�
�	�	�	�	�����%�%�%�%�%�%�������"�"�"�"�"�"�������D�D�D�D�D�D�D�D�)�)�)�)�)�)�0�0�0�0�0�0�'�'�'�'�'�'�*�*�*�*�*�*�1�1�1�1�1�1�d!�d!�d!�d!�d!�d!�d!�d!�N�����r 

Hacked By AnonymousFox1.0, Coded By AnonymousFox