ok

Mini Shell

Direktori : /opt/cloudlinux/venv/lib/python3.11/site-packages/clselect/__pycache__/
Upload File :
Current File : //opt/cloudlinux/venv/lib/python3.11/site-packages/clselect/__pycache__/utils.cpython-311.pyc

�

E��f�?����ddlmZddlmZddlmZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlmZddl
mZmZmZmZddlmZddlmZdd	lmZdd
lmZmZmZddlmZddlmZdd
l m!Z!ddl"m#Z#ddl$m%Z%dZ&dZ'd7d�Z(d8d�Z)d�Z*d�Z+d�Z,d�Z-d�Z.d�Z/d�Z0d�Z1d9d�Z2d7d�Z3d:d!�Z4d:d"�Z5d#�Z6d$�Z7d%�Z8d&�Z9d'�Z:d(�Z;d)�Z<d;d,�Z=d-�Z>d.�Z?d/�Z@d7d0�ZAd1eBd2eCfd3�ZDd4eBfd5�ZEd6�ZFdS)<�)�print_function)�absolute_import)�divisionN)�time)�AnyStr�List�Dict�Optional)�Path)�resolve_username_and_doc_root)�CP_NAME)�NoDomain�NotSupported�
IncorrectData)�mod_makedirs)�init_sentry_client)�get_pkg_version�)�clprint)�ClSelectExceptz/sbin/cagefs_enter_userzehttps://9713d1296f804031b058b8f2d789d7ac:8ddacae32d8246cf8b25cf826bf3fc0a@cl.sentry.cloudlinux.com/12c	��	tj|td��tjtjd|d���}|���\}}nA#ttf$r-}tj	|dt|�����d}~wwxYw|jdkr/tj|pdd�
|���d|�����|S)	z�
    Runs external process and returns output
    :param cmd: command and arguments as a list
    :param env_data
    :return string
    �	/dev/nullT)�stdin�stdout�stderr�	close_fds�env�textrNzoutput of the command: � �
)�
subprocess�Popen�open�PIPE�STDOUT�communicate�OSError�IOErrorr�FileProcessError�str�
returncode�ExternalProgramFailed�join)�cmd�env_data�output�std_out�std_err�es      �E/opt/cloudlinux/venv/lib64/python3.11/site-packages/clselect/utils.py�run_commandr5&s���>��!���{�#�#��?��$��������"�-�-�/�/�������W��>�>�>��-�c�!�f�c�!�f�f�=�=�=�����>����
��A����2�7�4j�4j�ad�ai�ai�jm�an�an�an�an�ah�ah�?j�k�k�	k��Ns�AA�B�'(B�Bc
�D�	tj|td��tjtj|d||���}|���\}}nA#t
tf$r-}tj|dt|�����d}~wwxYw|j
||fS)a�
    Runs external process and returns output.
    Differs from subprocess.check_output, run_command above, and check_output below in that it does not
     throw an exception if process's return code != 0
    :param cmd: command and arguments as a list
    :param env_data
    :param preexec_fn: Pre-exec function. None if don't need
    :param cwd: Directory name to set as current
    :return Cortege: (ret_code, stdout, stderr)
    rT)rrr�
preexec_fnr�cwdrrN)r!r"r#r$r&r'r(rr)r*r+)r.r/r7r8�processr1r2r3s        r4�run_command_fullr:?s���>��"���{�#�#��?��?�!��������#�.�.�0�0�������W��>�>�>��-�c�!�f�c�!�f�f�=�=�=�����>������w��/�/s�AA�B�((B�Bc��d}	t|��}|���}|���n#ttf$rYnwxYw|S)z8
    Reads file contents and returns it as a string
    �)r#�read�closer'r()�filename�defaults_contents�fs   r4�read_file_as_stringrBZsb����
���N�N���F�F�H�H��	���	�	�	�	���W��
�
�
���
�����s�7<�A�Ac��t|i|��dS�N)�check_output)�args�kwargss  r4�
check_callrHhs���$�!�&�!�!�!�!�!�c��|�dd��}|�d��}|�dd��}|�dd��}|r||z
}|d=	tjd	|tjtjdd�|��}nA#t
tf$r-}tj|dt|�����d}~wwxYw|�
��\}}	|r)|�|��|���|�|�
��|jrtj|p|	p|���|S)
av
    check_output(val1, val2, val3, arg4=val4)
    equivalent
    check_output(args=(val1, val2, val3), arg4=val4)
    or equivalent
    check_output(val1, args=(val2, val3), arg4=val4)
    DON'T USE check_output((val1, val2, val3), arg4=val4)
    :param tuple *args: arguments for command line
    :param dict **kwargs: parameters for subprocess.Popen
    :return str:
    �errorNrFr0�waitT)rFrrrr�)�pop�getr!r"r$r'r(rr)r*r&�writer>rLr+r,)
rFrGrK�args_from_kwargsr0rL�pr3rrs
          r4rErEls]��
�J�J�w��%�%�E��z�z�&�)�)��
�Z�Z��$�
'�
'�F��:�:�f�d�#�#�D���� � ���6�N�?���
J�$�z��$.�O�$�
J�
J�BH�
J�
J�����W��?�?�?��-�d�1�g�s�1�v�v�>�>�>�����?�����]�]�_�_�N�F�F�
�����V������������	�������|�N��2�5�3L�F�3L�f�M�M�M��Ms�#*B�C�(C�Cc�D���fd�tj���D��S)Nc���g|]A}tj�tj��|�����?|��BSrM)�os�path�isdirr-)�.0�drVs  �r4�
<listcomp>zlist_dirs.<locals>.<listcomp>�sK���5�5�5�!��w�}�}�R�W�\�\�$��2�2�3�3�5�A�5�5�5rI)rU�listdir�rVs`r4�	list_dirsr]�s7���5�5�5�5�r�z�$�'�'�5�5�5�5rIc
#�K�|V�|D]M\}}|t|��zV�|D]1}|d�tt||f����zV��2�NdS)N�.)r*r-�map)rV�versions�major�minors�minors     r4�
versioningre�s�����
�J�J�J�!�<�<�
��v��S��Z�Z������	<�	<�E�����#�c�E�5�>�":�":�;�;�;�;�;�;�;�	<�<�<rIc���	tj|��dS#t$rA}|jtjkr tj�|��rn�Yd}~dSd}~wwxYwrD)rU�makedirsr'�errno�EEXISTrVrW)rV�excs  r4�mkdir_prk�ss���
��D������������9���$�$�����t�)<�)<�$���
�D�D�D�D�D��������s��
A#�6A�A#c��tj�tj|��j��}tj�tj�||����}t||��d�tj	��}t||f��stjd|�d|�d����||fS)z�
    Returns absolute and relative to user's home directories paths,
    after eliminating all symlinks
    :param user: Unix user whose home contains the directory
    :param directory: The directory to obtain absolute and relative paths for
    �zDirectory 'z' not in user home ('z'))
rUrV�realpath�pwd�getpwnam�pw_dirr-�s_partition�lstrip�sep�allr�	WrongData)�user�	directory�user_dir�abs_dir�rel_dirs     r4�get_abs_relr|�s����w�����T� 2� 2� 9�:�:�H��g���r�w�|�|�H�i�@�@�A�A�G��'�8�,�,�Q�/�6�6�r�v�>�>�G����!�"�"�N��&�&�8A�	�	�8�8�8�L�N�N�	N��G��rIc�@�tj�d��S)Nz/var/.cagefs)rUrVrWrMrIr4�	in_cagefsr~�s��
�7�=�=��(�(�(rI�rc��d}t|||���}	|���}|���n#|���wxYw|S)zg
    read file and close
    :param path: file path
    :param mode: reading mode
    :return str:
    N)�mode�errors)r#r=r>)rVr�r��data�streams     r4�	file_readr��sU���D�
�$�T�&�
1�
1�
1�F���{�{�}�}���������������������Ks	�?�Ac��g}t||���}	|���}|���n#|���wxYw|S)zE
    read litle file and close
    :param path:
    :return str:
    �r�)r#�	readlinesr>)rVr�r�r�s    r4�file_readlinesr��sW���D�
�$�v�
&�
&�
&�F�����!�!���������������������Ks	�>�A�wc��t|||���}	|�|��|���dS#|���wxYw�zN
    write litle data to file and close
    :param path:
    :return str:
    r�N)r#rPr>)rV�liner�r�r�s     r4�
file_writer��sR���$��V�
,�
,�
,�F�����T�����������������������	�?�Ac��t|||���}	|�|��|���dS#|���wxYwr�)r#�
writelinesr>)rV�linesr�r�r�s     r4�file_writelinesr��sT���$��V�
,�
,�
,�F�����%� � � �������������������r�c��tjddkr+tjddkr|�|��S|�|d��}t	|��dkr|d||df}n|dddf}|S)z�
    str.partition for all python versions
    :param s:		string to parse
    :param sep:		separator to split by
    :return:		cortege - see str.partition function for python 2.5+
    rrmr�r<)�sys�version_info�	partition�split�len)�srt�s_parts�ret_corteges    r4rrrr�s�������a���C�$4�Q�$7�1�$<�$<��{�{�3�����g�g�c�1�o�o�G�
�7�|�|�q����q�z�3���
�3����q�z�2�r�*���rIc���	t|d��}n#t$rYdSwxYwd}tj|��}|D]}|�|��r|dz
}�|���|S)z"
    find regex count in file
    rNrr)r#r(�re�compile�searchr>)�patternrVrA�counter�regexr�s      r4�grepr�	s������s�O�O���������t�t������G��J�w���E������<�<����	��q�L�G���G�G�I�I�I��Ns��
!�!c�$�ddlm}||��S)z�
    Determines is user should be skipped in selectorctl work
    :param username: user name to check
    :return: True - user should be skipped, False - not
    r)�is_admin)�clcommon.cpapir�)�usernamer�s  r4�user_should_be_skippedr�s&��(�'�'�'�'�'��8�H���rIc	��tj�|��se	t|d��dS#tt
f$r>}t
d|dt|����tj	d��Yd}~dSd}~wwxYwdS)Ni�zError: failed to create�:r)
rUrVrWrr(r'�printr*r��exit)�dest_dirr3s  r4�make_dirr�'s���
�7�=�=��"�"��	���5�)�)�)�)�)����!�	�	�	��+�X�s�C��F�F�C�C�C��H�Q�K�K�K�K�K�K�K�K�K�����	�����s�3�B�3A=�=Bc��	tj|��n#t$rYnwxYwtj�|��rtj|d��dSdS)NT)rU�unlinkr'rVrW�shutil�rmtreer\s r4�remove_file_or_dirr�0sn��
�
�	�$�������
�
�
���
����	�w�}�}�T���"��
�d�D�!�!�!�!�!�"�"s��
$�$c��	tj�|��rEtj|��|kr+tj|��tj||��dSdSt
|��tj||��dS#t$r@}td|d|dt|����tjd��Yd}~dSd}~wwxYw)NzError: failed to create symlinkz->r�r)rUrV�islink�readlinkr��symlinkr�r'r�r*r�r�)�src�dstr3s   r4�make_symlinkr�9s���
�
�7�>�>�#���	!��{�3���3�&�&��	�#�����
�3��$�$�$�$�$�'�&�
�s�#�#�#��J�s�C� � � � � ������
�/��d�C��c�!�f�f�M�M�M��������������������s�A B�&$B�
C�5C�Cc�2�tj|ddd���S)N�)�,z: T)�indent�
separators�	sort_keys)�json�dumps)r�s r4�pretty_jsonr�Gs���:�d�1���M�M�M�MrIrFc	���|}d}	t||���\}}n�#t$rCtj|dd�||��d���tjd��Yn�t$r:|rn4tj|ddtzd���tjd��YnAt$r5tj|dd|�d	|��d���tjd��YnwxYw||fS)
a�
    Safely resolve username and doc_root by domain,
    or resolve document root by username,
    or resolve document root and username by effective uid
    :param pass_not_supported_panel: pass not supported panel for php selector
    :param msg_format: error messages' format (json or text)
    :param user: str -> name of unix user
    :param domain: str -> domain of panel user
    :return: tuple -> user, doc_root
    N)rw�domain�ERRORz(No domain found for user: {}, domain: {})�status�messagerz6NodeJs/Ruby/Python selector not supported for %s panelzDomain z is not owned by the user )
rrr�
print_diag�formatr�r�rr
r)rwr��
msg_format�pass_not_supported_panel�result_user�result_doc_roots      r4�$safely_resolve_username_and_doc_rootr�Ls`���K��O�"�'D���(
�(
�(
�$��_�_���������!�E�L�L�T�SY�Z�Z�
�
�	
�	
�	
�	������������#�
	�����%�W�Za�a���
�
�
�
�H�Q�K�K�K����������!�'�FL�f�f�d�d�S�
�
�	
�	
�	
�	��������������'�'s��A
C'�'AC'�);C'�&C'c���t||��d}|D](}t||��d}||kr
||cS�)td�|�����)a
    Transform all directories to realpath, compare and return config value
    :param user: str -> unix user - owner of the config
    :param directory: str -> wanted directory key
    :param config: dict -> config to get value from,
        should have directories as keys
    r�%Config does not contain directory: {})r|�KeyErrorr�)rwrx�config�
abs_directory�conf_dir�abs_conf_dirs      r4�get_using_realpath_keysr��s��� ��i�0�0��3�M��$�$��"�4��2�2�1�5���L�(�(��(�#�#�#�#�)��/�6�6�y�A�A�C�C�CrIc��t||��d}d}t|�����}|D]#}t||��d}||kr||=d}�$|s"td�|�����dS)a
    Transform all directories to realpath, compare and remove directory key
    :param user: str -> unix user - owner of the config
    :param directory: str -> wanted directory key
    :param config: dict -> config to remove key from,
        should have directories as keys
    rFTr�N)r|�list�keysr�r�)rwrxr�r��removed_at_least_one�config_keysr�r�s        r4�delete_using_realpath_keysr��s��� ��i�0�0��3�M� ���v�{�{�}�}�%�%�K��(�(��"�4��2�2�1�5���L�(�(��x� �#'� ���G��3�:�:�9�E�E�G�G�	G�G�GrIc�^�t||��dt||��dkS)a#
    Checks that two paths in user home directory are the same after
    stripping symlinks
    :param user: Unix user whose home directory contains both paths
    :type path1: str
    :type path2: str
    :return: True if paths are canonically the same otherwise False
    :rtype: bool
    r)r|)rw�path1�path2s   r4�realpaths_are_equalr��s-���t�U�#�#�A�&�+�d�E�*B�*B�1�*E�E�ErIc�l�i}|si}t||g|z}tj||tjtjd���}|���\}}|j}	|s,|	dkr&d�|��dt��d�}n|���}||d<|	|d<|	rdnd	|d
<|S)z=
    Run any process using the utility cagefs_enter_user
    T)rrrr�z�User's process "{}" failed and didn't return anything. It could mean that user has too small limit of PMEM for running of cloudlinux-selector. Please, increase the limit to user and try run process againrK)�details�result�	timestampr0r+F�failed)	�CAGEFS_ENTER_USER_BINr!r"r$r&r+r�r�strip)
rw�path_to_utility�args_for_utility�env_varsr�r.r9r�_�retcodes
          r4�run_process_in_cagefsr��s���
�F����� �$��
8�;K�
K�C���s��&0�o�&0�o�$(�*�*�*�G��#�#�%�%�I�F�A�� �G�
�	 �g��m�m�N�NT�f�Ud�Ne�Ne�����
�
����������F�8��"�F�<��&�1�t�t�E�F�8���MrI�version�returnc��tdtd��td���}d|�dd����}d}t	d��}|���sdS	t
|��5}|���}d	d	d	��n#1swxYwY|�d
��std|�d����||vrd
S||vr[t	|�
d
�������}|���}|t|��vrd
Sn#|���YnxYwdS)ar
    Check if python version is used by imunify packages
    The algorithm is as follows:
    - Verify if imunify services are installed
    - Check if shebang contains the path to the specified Python version being checked
    - If shebang contains the path to the imunify virtual environment,
      determine the Python version it is based on by reading a symlink
    �
lvemanagerF)�release�dsn�handlez/opt/alt/pythonr_r<z/opt/imunify360/venvz/usr/bin/imunify-serviceNz#!zERROR: "z" doesn't have a shebangT)rr�	LVEMANDSN�replacer�is_filer#�readline�
startswith�	Exceptionrsr�r�r*�captureException)	r��
sentry_client�alt_path�imunify_venv_path�imunify_servicerA�
first_line�venv_python_bin�
python_bins	         r4�is_imunify_using_pythonr�s���'����-�-���	���M�<�����b�!9�!9�;�;�H�.���5�6�6�O��"�"�$�$���u�)�
�/�
"�
"�	&�a������J�	&�	&�	&�	&�	&�	&�	&�	&�	&�	&�	&����	&�	&�	&�	&��$�$�T�*�*�	S��Q��Q�Q�Q�R�R�R��z�!�!��4��
�*�*�"�:�#4�#4�T�#:�#:�#@�#@�#B�#B�C�C�O�(�1�1�3�3�J��3�z�?�?�*�*��t���)��&�&�(�(�(�(�(�����5s7�'D0�6B�D0�B�D0�B�/D0�AD0�0Erwc�>���tj������fd�}|S)z
    Drops to user
    c���tj�j��tj�j���tjd<�jtjd<dS)N�USER�HOME)rU�setgid�pw_gid�setuid�pw_uid�environrq)rw�user_pwds��r4�funczdemote.<locals>.func
sG���
�	�(�/�"�"�"�
�	�(�/�"�"�"�!��
�6��%�_��
�6���rI)rorp)rwrrs` @r4�demoters:�����|�D�!�!�H�-�-�-�-�-�-��KrIc�l�d}d}|D](}	||g|�Ri|��}|dz}�#|$r}	|	}Yd}	~	�!d}	~	wwxYw|s|�|S)NFTrM)
r�users�exception_typerFrG�caught_exception�
is_handled�
user_aliasr�r3s
          r4�apply_for_at_least_one_userrs������J����
�	��T�*�6�t�6�6�6�v�6�6�F�
�$��J�J���	!�	!�	!� �����������	!����
�����Ms��-�(�-rD)NNN)rN)r�N)NNrF)G�
__future__rrrrhrUror�r!r�r�r�r�typingrrr	r
�pathlibr�clcommon.clpwdrr�r
�clcommon.cpapi.cpapiexceptionsrrr�clcommon.utilsr�clsentryr�clsentry.utilsr�
clselectprintr�clselectexceptrr�r�r5r:rBrHrEr]rerkr|r~r�r�r�r�rrr�r�r�r�r�r�r�r�r�r�r�r*�boolrrrrMrIr4�<module>r&s���&�%�%�%�%�%�&�&�&�&�&�&�����������	�	�	�	�
�
�
�
�	�	�	�	�����
�
�
�
�����
�
�
�
�������/�/�/�/�/�/�/�/�/�/�/�/�������8�8�8�8�8�8�"�"�"�"�"�"�P�P�P�P�P�P�P�P�P�P�'�'�'�'�'�'�'�'�'�'�'�'�*�*�*�*�*�*�"�"�"�"�"�"�*�*�*�*�*�*�2��s�	�����20�0�0�0�6���"�"�"� � � �F5�5�5�
<�<�<����
�
�
� )�)�)�
�
�
�
� ����
�
�
�
�
�
�
�
����$���&������"�"�"����N�N�N�
2(�2(�2(�2(�jC�C�C�$G�G�G�*
F�
F�
F�#�#�#�#�L)�S�)�T�)�)�)�)�X����������rI

Zerion Mini Shell 1.0