Your IP : 216.73.216.249


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

�

�wlj�#�	��	�ddlmZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlZddlZddlZddlZddlZddlZddlZddlmZmZmZddlmZddlmZddlmZddl m!Z!m"Z"m#Z#m$Z$ddl%m&Z&ddl'Z'dd	lm(Z(dd
l)m*Z*m+Z+ddl,m-Z-ddl.m.Z.m/Z/m0Z0dd
lm1Z1m2Z2m3Z3m4Z4m5Z5ddl6Z6ddl7m8Z8m9Z9ddl:m;Z;ddl<m=Z=m>Z>ddl?m@Z@ddlAmBZBmCZCddlDmEZEmFZFddlGmHZHmIZIddlJmKZKmLZLddlMmNZNddlOmPZPmQZQmRZRmSZSmTZTmUZUddlVmWZWmXZXmYZYddlZmZm[Z[ddl\m]Z]m^Z^m_Z_m`Z`maZambZbddlcmdZdddlemfZfmgZgddlhmiZiddlOmjZjddlkmlZld d!lmmnZndd"lompZpmqZqmrZrmsZsmtZtmuZumvZvmwZwmxZxmyZymzZzm{Z{m|Z|m}Z}m~Z~d d#lm�Z�m�Z�d d$l�m�Z�dd%l�m�Z�ej�rdd&l�m�Z�da�d'�Z�Gd(�d)ej���Z�d�d+e�d,e�fd-�Z�d�d+e�d.e�d/e�fd0�Z�d�d+e�d2e�fd3�Z�d4e�fd5�Z�d6e�fd7�Z�d4e�fd8�Z�d9ej�fd:�Z�d;�Z�d�d<e�d4e�fd=�Z�d4e�fd>�Z�dd?�d<e�d4e�fd@�Z�dd?�d<e�fdA�Z�GdB�dC��Z�dDe�d4e3e�fdE�Z�d4e1e�fdF�Z�dGe�d4dfdH�Z�dIe�d4dfdJ�Z�d<e�fdK�Z�dIe�d4dfdL�Z�d�dM�Z�dN�Z�dOe�d4e2e�e�ffdP�Z�dQe�dRe3e�fdS�Z�d+e�fdT�Z�GdU�dVe���Z�e(d�dXe�dRe3e�fdY���Z�dZ�Z�e�d�d]���Z�d^e�d4e2e�e�ffd_�Z�d�d4e�fda�Z�db�Z�dc�Z�eGdd�de����Z�eGdf�dg����Z�e+d�h��di���Z�dj�Z�dk�Z�d4e�fdl�Z�dm�Z�dn�Z�do�Z�d4e�fdp�Z�d4e�fdq�Z�e(d4e5e�fdr���Z�d�ds�Z�dtZ�du�Z�d�dv�Z�dw�Z�dx�Z�Gdy�dz��Z�d�d{�Z�Gd|�d}��Z�e(d~e�fd���Z�d~e�d4e�fd��Z�d4e�fd��Z�d4e�fd��Z�d4e�fd��Z�d4e�fd��Z�d�e�d�e�d4dfd��Z�dIe�d�e�d�e�d4dfd��Z�d4e�fd��Z�d4e�fd��Z�d4e2e�e�e�ffd��Z�dQe�d4e�fd��Z�d�e�d�e�d4e�fd��Z�d�e�d4e�fd��Z�d�e�d4e3e�fd��Z�d�e�d�e�d4e2e�e�e�ffd��Z�d�e�d4e2e�e�e�ffd��Z�d4e3e�fd��Z�e�d4e3e�fd����Z�e�d4e3e�fd����Z�e�d4e3e�fd����Z�e�d4e3e�fd����Z�d�e�d4e1fd��Z�d��Z�d�e�d�e�d�e�fd��Z�d��Z�d��Z�e+d�h��d�d<e�fd����Z�e+d�h��d<e�fd����Z�d��Z�e+d�h��d����Z�dS)��)�absolute_importN)�	dataclass�asdict�field)�iglob)�Enum)�gettext)�	urlencode�urlparse�	parse_qsl�
urlunparse)�Version)�contextmanager)�wraps�	lru_cache)�Path)�socket�AF_UNIX�SOCK_STREAM)�List�Tuple�Optional�Set�ContextManager)�write_file_via_tempfile�
disable_quota)�NoDomain)�ClPwd�drop_privileges)�mkdir��is_cl_shared_pro_edition�CLEditionDetectionError)�read_jwt�
decode_jwt)�CLN_JWT_TOKEN_PATH�DEFAULT_JWT_ES_TOKEN_PATH)�
PyJWTError�
exceptions)�
check_license)�docroot�get_domain_login�
get_server_ip�cpinfo�is_admin�get_main_username_by_uid)�exec_utility�run_command�demote)r	�	wp_config)�	WposError�WposDaemonUnreachableError�WPOSLicenseMissing�WpCliUnsupportedException�WpNotExists�WpConfigWriteFailed)�UIConfig)�	in_cagefs�_is_cagefs_enabled)�Feature)�is_panel_feature_supported)�get_locale_from_envars�)�
setup_logging)�
USER_WPOS_DIR�WPOS_DAEMON_SOCKET_FILE�CLCONFIG_UTILITY�RedisRequiredConstants�CAGEFS_ENTER_USER_BIN�CAGEFS_ENTER_UTIL�CLWPOS_OPT_DIR�ALT_PHP_PREFIX�
EA_PHP_PREFIX�PLESK_PHP_PREFIX�USER_CLWPOS_CONFIG�PUBLIC_OPTIONS�SUITES_MARKERS�XRAY_MANAGER_UTILITY�XRAY_USER_SOCKET)�pack_data_for_socket�'read_unpack_response_from_socket_client)�RollbackException)�cached_in_scope)�PHPc����fd�}|S)z'
    Decorator for catching errors
    c���t�tt��a	�|g|�Ri|��S#t$r3}t	|j|j|j|jd���Yd}~dSd}~wt$r�}t|t��rt�|��nt�
|��|j|j|jd�}|jr
|j|d<t	|j|��Yd}~dSd}~wt$rJ}t�
|��t	|jit!|��d���Yd}~dSd}~wwxYw)N)�context�result�issues)rYrZ�warning�details)rYrZ)�loggerrB�__name__rT�error_and_exit�_is_jsonrY�message�errorsr5�
isinstancer7r\�	exceptionr]�	Exception�str)�self�args�kwargs�e�response�funcs     ��C/opt/cloudlinux/venv/lib64/python3.11/site-packages/clwpos/utils.py�func_wrapperz!catch_error.<locals>.func_wrappervs�����>�"�8�,�,�F�	M��4��.�t�.�.�.�v�.�.�.�� �	k�	k�	k��4�=�a�i�1�9�`a�`h�*i�*i�j�j�j�j�j�j�j�j�j������	4�	4�	4��!�/�0�0�
$����q�!�!�!�!�� � ��#�#�#�#$�9��	�a�i�X�X�H��y�
0�&'�i���#��4�=�(�3�3�3�3�3�3�3�3�3������	M�	M�	M����Q�����4�=�b�C��F�F�*K�*K�L�L�L�L�L�L�L�L�L�����	M���s-�*�
E�(A"�"
E�/BC:�:
E�?E�E�)rmros` rn�catch_errorrqqs)���
M�M�M�M�M�*��c�(��eZdZdZ�fd�Zd�Z�xZS)�ExtendedJSONEncoderzz
    Makes it easier to use ENUMs and DATACLASSes in program,
    automatically converting them when json is printed.
    c�D��d|d<t��jdi|��dS)NF�ensure_asciirp)�super�__init__)rhrj�	__class__s  �rnrxzExtendedJSONEncoder.__init__�s0���!&��~�������"�"�6�"�"�"�"�"rrc��t|t��r|jSt|tjtjf��r|���St|t��rt|��Stj	|��rtj
|��Stj�
||��S�N)rdr�value�datetime�date�	isoformatrrg�dataclasses�is_dataclassr�json�JSONEncoder�default)rh�objs  rnr�zExtendedJSONEncoder.default�s����c�4� � �	+��9��
��h�m�X�->�?�
@�
@�	+��=�=�?�?�"�
��W�
%�
%�	+��s�8�8�O�
�
%�c�
*�
*�	+��%�c�*�*�*���'�'��c�2�2�2rr)r_�
__module__�__qualname__�__doc__rxr��
__classcell__)rys@rnrtrt�sQ���������
#�#�#�#�#�	3�	3�	3�	3�	3�	3�	3rrrtF�is_json�	is_prettyc���|rY|r,ttj|ddt�����dSttj|dt�����dSt|��dS)z�
    Print specified dictionary
    :param data_dict: data dictionary to print
    :param is_json: True - print in JSON, False - in text
    :param is_pretty: True - pretty json print, False - none (default)
    :return: None
    �T)�indent�	sort_keys�cls)r�r�N)�printr��dumpsrt)�	data_dictr�r�s   rn�_print_dictionaryr��sy�����	R��$�*�Y�q�D�FY�Z�Z�Z�[�[�[�[�[��$�*�Y�$�<O�P�P�P�Q�Q�Q�Q�Q�	�i�����rrrb�
error_codec��d|���vr*|�d��s|�d��|r;|�dt	j��i��t||d���nu	t
t|d��|�di��z��n:#t$r-}t
dt|���d|�d	���Yd
}~nd
}~wwxYwtj
|��d
S)z�
    Print error and exit
    :param is_json:
    :param message: Dictionary with keys "result" as string and optional "context" as dict
    :param error_code: Utility return code on error
    r\�	timestampT�r�rZrYzError: z [�]N)�keys�get�pop�update�timer�r�rg�KeyError�sys�exit)r�rbr�rks    rnr`r`�s���G�L�L�N�N�"�"�7�;�;�y�+A�+A�"����I�����8�����T�Y�[�[�1�2�2�2��'�7�d�;�;�;�;�;�	8��#�g�h�'�(�(�7�;�;�y�"�+E�+E�E�F�F�F�F���	8�	8�	8��E�c�!�f�f�f�f�g�g�g�6�7�7�7�7�7�7�7�7�����	8�����H�Z�����s�?9B9�9
C0�#C+�+C0�success�datac��t|t��r)|�|tj��d���t	||d���dS)zo
    Output data wrapper
    :param is_json:
    :param data: data for output to stdout
    :param result:
    )rZr�Tr�N)rd�dictr�r�r�)r�r�rZs   rn�
print_datar��sR���$����B����v�D�I�K�K�@�@�A�A�A��d�G�t�4�4�4�4�4�4rr�returnc�0�tj��dkS)zQ
    Detects is we running under root
    :return: True - user, False - root
    r)�os�geteuidrprrrn�is_run_under_userr��s��
�:�<�<�1��rr�safelyc�H�	t��S#t$r|rYdS�wxYw)z�
    Detecting of shared_pro edition depends on jwt token
    There are some cases when we do not fail if there are
    cases with decoding (e.g summary collection)
    Fr!�r�s rn�is_shared_pro_safelyr��s@���'�)�)�)��"�����	��5�5��	���s�
�!�!c�4�ttj��S)u�
    Сheck if system environment is supported by WPOS
    :return:
        True - CPanel/Plesk on Solo/ CL Shared Pro/ CL Admin
        False - else
    )r?r>�WPOSrprrrn�is_wpos_supportedr��s��&�g�l�3�3�3rr�user_pwc��tj�|jt��}tj�|��st
|d���dSdS)z@
    Creates {homedir}/.clwpos directory if it's not exists
    i�)�modeN)r��path�join�pw_dirrC�isdirr )r��
clwpos_dirs  rn�create_clwpos_dir_if_not_existsr��sT������g�n�m�<�<�J�
�7�=�=��$�$�&�
�j�u�%�%�%�%�%�%�&�&rrc�*�t|��d}t|���d���}t|���d���}	tj�t
|��t
|��g��t
|��k}n#t$rd}YnwxYw|�|��r|std|�d|�d|�����|t|��d��d��S)NrF)�strictzdocroot z for domain z should start with �/)r+r�resolver�r��
commonpathrg�
ValueError�
startswithr5�len�lstrip)�domain�homedir�dr�
resolved_home�resolved_dr�	containeds      rn�get_relative_docrootr�s��	�����	�B���M�M�)�)��)�7�7�M��r�(�(�"�"�%�"�0�0�K���G�&�&��M�(:�(:�C��<L�<L�'M�N�N�RU�Vc�Rd�Rd�d�	�	�������	�	�	�����
�M�M�'�"�"�Y�y�Y��W�2�W�W�6�W�W�g�W�W�X�X�X�
�c�'�l�l�n�n��$�$�S�)�)�)s�AB*�*B9�8B9�usernamec�0�t|���}|jS�N�r�)�get_pwr�)r��pws  rn�home_dirr�s��	��	"�	"�	"�B�
�9�rrc�(�t��jSr{)r��pw_namerprrrn�	user_namer�s���8�8��rrr�c�,�t|���jSr�)r��pw_uidr�s rn�user_uidr�s���8�$�$�$�+�+rrc��|rtj|��Sttj����}tj|��Sr{)�pwd�getpwnam�get_username_by_uidr�r�r�s rnr�r�#s=���&��|�H�%�%�%�&�r�z�|�|�4�4���|�H�%�%�%rrc�4�eZdZdZd	dededdfd�Zd�Zd�ZdS)
�WposUserzQ
    Helper class to construct paths to user's WPOS dir and files inside it.
    Nr�r�r�c��||_|�t|��n||_tj�|jt
��|_tj�|jt��|_tj�|jd��|_	tj�|jd��|_
tj�|jd��|_dS)Nz
redis.conf�
redis.sockz.php_info-{file_id})�namer�r�r�r�rC�wpos_dirrM�wpos_config�
redis_conf�redis_socket�php_info)rhr�r�s   rnrxzWposUser.__init__0s�����	�.5�o���*�*�*�7��
�����T�]�M�B�B��
��7�<�<��
�7I�J�J����'�,�,�t�}�l�C�C����G�L�L����E�E�������T�]�4I�J�J��
�
�
rrc�"�|j|jkSr{�r�)rh�others  rn�__eq__zWposUser.__eq__9s���y�E�J�&�&rrc�*�t|j��Sr{)�hashr��rhs rn�__hash__zWposUser.__hash__<s���D�I���rrr{)r_r�r�r�rgrxr�r�rprrrnr�r�+sr��������K�K��K�s�K�d�K�K�K�K�'�'�'�����rrr��cmd_dictc���t|��}ttt��5}	|�t
��|�|��|�d��t|��}|�t|t��s,ttd��t|��i����|ddkr8ttd��|d|�d������|cddd��S#t$r%t!td	��d
t
i���t"t$t&f$r0}t!td��t|������d}~wt(t*jt.f$r0}ttd��t|������d}~wwxYw#1swxYwYdS)
z�
    Send command to CLWPOS daemon via socket
    :param cmd_dict: Command dictionary
    :return: Daemon response as dictionary, None - daemon data/socket error
    �xNzPUnexpected response from daemon. Report this issue to your system administrator.�rbr]rYrZr�z3Daemon was unable to execute the requested command.rYzbCloudLinux AccelerateWP daemon socket (%(filename)s) not found. Contact your system administrator.�filenamez&Unexpected daemon communication error.)r])rRrrr�connectrD�sendall�
settimeoutrSrdr�r5r	rgr��FileNotFoundErrorr6�ConnectionError�OSError�IOError�AttributeError�struct�errorr�)r��
bytes_to_send�s�
response_dictrks     rn�daemon_communicater@sS��)��2�2�M�	���	%�	%� _��	_�
�I�I�-�.�.�.�
�I�I�m�$�$�$�
�L�L�����C�A�F�F�M��$�J�}�d�,K�,K�$��#�$v�w�w��
�.�.������
�X�&�)�3�3��#�$Y�Z�Z�)�(�3�)�-�-�i�8�8�����
!�' _� _� _� _� _� _� _� _��(!�	�	�	�,��x����4�5�	��
� ��'�2�	p�	p�	p�,�W�5]�-^�-^�hk�lm�hn�hn�o�o�o�o��������h�7�	_�	_�	_��G�$L�M�M�WZ�[\�W]�W]�^�^�^�^�����	_����= _� _� _� _���� _� _� _� _� _� _s<�G�CD�AG�+F�G�"+G
�
G�G�G�Gc�T�d}t��jrd}tj�t��td��}tj��}tj
}tj}tj}tj
}tj}tj}dd|j�d|j�d�dtjj�d|�d�d|j�d|j�d�d|j�d|j�d�d|j�d|j�|�d�d|j�d|j�d	�d|j�d|�d	�d
dg
S)ak
    Construct list of lines (configuration settings)
    that should be in Wordpress config file to enable redis.
    Please note that deleting of the plugin would flush all keys related to the plugin (site) from redis.
    REDIS_PREFIX and SELECTIVE_FLUSH in wp-config.php would guarantee that plugin will not flush keys unrelated
    to this plugin (site)
    �false�truer�z)// Start of CloudLinux generated section
�define('z', 'z');
z', z);
z�if (defined('WP_CLI') && WP_CLI && !defined('WP_REDIS_IGNORED_GROUPS')) { define('WP_REDIS_IGNORED_GROUPS', ['user-queries']); }
z'// End of CloudLinux generated section
)�get_server_wide_options�disable_object_cache_bannersr�r�r�r�rC�uuid�uuid4rF�WP_REDIS_PREFIX�WP_REDIS_SCHEME�WP_REDIS_CLIENT�WP_REDIS_SELECTIVE_FLUSH�WP_REDIS_GRACEFUL�WP_REDIS_DISABLE_BANNERSr��val�
WP_REDIS_PATH)	�disable_banners_value�socket_path�prefix_uuid�redis_prefix�redis_schema�redis_client�redis_flush�redis_graceful�disable_bannerss	         rn�redis_cache_config_sectionrjsV��$��� � �=�'� &���'�,�,�x�z�z�=�,�G�G�K��*�,�,�K�)�9�L�)�9�L�)�9�L�(�A�K�+�=�N�,�E�O�	5�A�<�$�A�A�,�*:�A�A�A�T�)�7�<�T�T�+�T�T�T�A�<�$�A�A�,�*:�A�A�A�E�>�&�E�E�N�,>�E�E�E�N�<�$�N�N�,�*:�N�K�N�N�N�=�;�#�=�=���=�=�=�G�?�'�G�G�,A�G�G�G�	M�2��rr�wp_config_pathc��tj�|��}tj�|��st	|���tj�|��s t
td��d|i����dS)z�
    Check that wp-config.php exists inside Wordpress directory.
    :param wp_config_path: absolute path to Wordpress config file
    :raises: WposError
    z)Wordpress config file %(file)s is missing�file�rbrYN)r�r��dirname�existsr9�isfiler5r	)r�wp_paths  rn�check_wp_config_existancer$�s����g�o�o�n�-�-�G�
�7�>�>�'�"�"�#��'�"�"�"�
�7�>�>�.�)�)�x���(S� T� T�_e�gu�^v�w�w�w�w�x�xrr�abs_wp_pathc���ttj|����}t|��t	���dtdt
f�fd�}	tj|��}tt||����}td�
|��|d��dS#ttf$r}t||���d}~wwxYw)z�
    Clear cloudlinux section with redis object cach config from docroot's wp-config.php
    :param abs_wp_path: Absolute path to WordPress
    :raises: WposError
    �liner�c���|�vod|vS)zn
        Filter function that should delete CL config options from the `redis_cache_config_section()`
        r
rp)r'�lines_to_filters �rn�__config_filterz1clear_redis_cache_config.<locals>.__config_filter�s����?�*�L�/@��/L�Lrr��N)rgr4r�r$r�bool�read�list�filterrr�r�r�r:)r%rr*�wp_config_lines�cleared_wp_configrkr)s      @rn�clear_redis_cache_configr3�s��������4�4�5�5�N��n�-�-�-�0�2�2�O�M�c�M�d�M�M�M�M�M�M�5�#�.��5�5�� ����!I�!I�J�J������(9� :� :�N�E�R�R�R�R�R���W��5�5�5�!�.�!�4�4�4�����5���s�AB,�,C�=C
�
Cc�b�t��st|��ntj��S)z�
    Run code under user's privileges if we are root; otherwise do nothing.
    Used by cleanup/migration routines which should not perform filesystem writes as root.
    )r�r�
contextlib�nullcontextr�s rn�
_drop_to_userr7�s+��
->�,?�,?�]�?�8�$�$�$�Z�E[�E]�E]�]rrc���ttj|����}t|��	d�tD��}tj|��}t
d�|D����}g}|D]^��fd�|���D��}|s|sdSt
�fd�tD����s|�����_|dgt���|dd��}td�|��|d	��dS#ttf$r}t||���d}~wwxYw)
z�
    Create config for redis-cache.
    Updates wp-config.php by inserting CloudLinux redis-cache section when needed.
    :param abs_wp_path: absolute path to WordPress
    :raises: WposError
    c�(�i|]}|j|j��Srp)r�r)�.0�constants  rn�
<dictcomp>z-create_redis_cache_config.<locals>.<dictcomp>�s��_�_�_�H�H�M�8�<�_�_�_rrc3�K�|]}d|vV��	dS)�WP_REDIS_IGNORED_GROUPSNrp)r:r's  rn�	<genexpr>z,create_redis_cache_config.<locals>.<genexpr>�s(����#b�#b�$�$=��$E�#b�#b�#b�#b�#b�#brrc�,��i|]\}}d|�d��v�
||��S)r�'rp)r:�k�vr's   �rnr<z-create_redis_cache_config.<locals>.<dictcomp>�s3���i�i�i���A�_�XY�_�_�_�dh�Mh�Mh��1�Mh�Mh�MhrrNc3�2�K�|]}d|j�d��vV��dS)rrANr�)r:�redis_constantr's  �rnr?z,create_redis_cache_config.<locals>.<genexpr>�s7�����n�n�^�8�.�"5�8�8�8�D�@�n�n�n�n�n�nrrrrAr+r,)rgr4r�r$rFr.�any�items�appendrrr�r�r�r:)	r%r�absent_constantsr1�add_cli_directive�
cleaned_lines�updated_configrkr's	        @rn�create_redis_cache_configrM�s��������4�4�5�5�N��n�-�-�-�5�_�_�H^�_�_�_��#�.��5�5�� #�#b�#b�Ra�#b�#b�#b� b� b�b���
�#�	+�	+�D�i�i�i�i�1A�1G�1G�1I�1I�i�i�i��#�
�,=�
�����n�n�n�n�Wm�n�n�n�n�n�
+��$�$�T�*�*�*��
�!��
�
'�
)�
)�
��1�2�2�
�
��
	 ����� 7� 7���O�O�O�O�O���W��5�5�5�!�.�!�4�4�4�����5���s�A*D�A;D�E�-D=�=Ec	�
�tptjt��}dx}}ddlm}m}ddlm}|��D�]�}	d�||��D��}n.#t$r!}|�
d||��Yd}~�@d}~wwxYwt|��}	|D�]�}
	t|��5t||
|	�����}ddd��n#1swxYwYn/#t$r"}|�
d|
||��Yd}~�md}~wwxYw|D�]}|d	vrd
n|}t|t��s|dz
}�&tj�|��s2tj�|���d��r|dz
}|�d
|||
����tj�|
|���d��}
tt/j|
����}|�d�}tj�|��s��	t|��5tj�|��r\tj�|���tj�|
��tjz��st5d|�����tj|��ddd��n#1swxYwY|dz
}���#t$r'}|dz
}|�d||��Yd}~��d}~wwxYw���	t9j|��j}n'#t>$r|�
d|��Y��#wxYw|r%|dkstj�|��s|�d||����gtj�|t@dd��}	t|��5tj�|��s	ddd�����tj!|��}tEj#|j$��rtKj&|��ntj|��ddd��n#1swxYwY��;#tN$r8}|j(tPj)kr|�d|||��Yd}~��xd}~wt$r#}|�d|||��Yd}~���d}~wwxYw|�*d||��dS)z�
    Cleanup for old customers:
    - Remove legacy backups created next to wp-config.php (docroot):
        <wp-path>/wp-config.php.backup
    - Remove old per-user backup directory:
        <home>/.clwpos/backups/wp-config
    r)�cpusers�userdomains)�
find_wp_pathsc��g|]\}}|��Srprp)r:�_r�s   rn�
<listcomp>z4cleanup_legacy_wp_config_backups.<locals>.<listcomp>�s��>�>�>�u�q�"��>�>�>rrz>legacy wp-config backup cleanup: can't get docroots for %s: %sN)�excludeszAlegacy wp-config backup cleanup: can't scan docroot %s for %s: %s)r+�.z./r+rAz..zLlegacy wp-config backup cleanup: suspicious wp_rel=%r for user=%s docroot=%sr�z.backupz"refuse to delete outside docroot: z.legacy wp-config backup cleanup failed: %s: %sz:legacy wp-config backup cleanup: can't resolve home for %sz8legacy wp-config backup cleanup: invalid home for %s: %r�backupsz	wp-configz;legacy wp-config backup cleanup: can't remove %s for %s: %sz;legacy wp-config backup cleanup done: removed=%d skipped=%d)+r^�logging�	getLoggerr_�clcommon.cpapirOrP�clcommon.clwpos_librQrf�debugr/r7rdrgr�r��isabs�normpathr�r\r��rstripr4�lexists�sepr��unlinkr�r�r�r�rC�lstat�stat�S_ISDIR�st_mode�shutil�rmtreer��errno�ENOENT�info)�_log�removed�skippedrOrPrQr��docrootsrkrU�docroot_path�wp_paths�wp_relr%r�
legacy_backup�home�target�sts                   rn� cleanup_legacy_wp_config_backupsrw�s����0�W�&�x�0�0�D���G�g�3�3�3�3�3�3�3�3�1�1�1�1�1�1��G�I�I�Lm�Lm��	�>�>���H�(=�(=�>�>�>�H�H���	�	�	��J�J�W�Ya�cd�e�e�e��H�H�H�H�����	������>�>��$�*	e�*	e�L�
�"�8�,�,�T�T�#�M�M�,��$R�$R�$R�S�S�H�T�T�T�T�T�T�T�T�T�T�T����T�T�T�T����
�
�
��
�
�W�Ye�go�qr�������������	
����#� 
e� 
e��%��8�8���f��!�&�#�.�.���q�L�G���7�=�=��(�(��B�G�,<�,<�V�,D�,D�,O�,O�PT�,U�,U���q�L�G��L�L�f�� �$�	���� �g�l�l�<��@�@�G�G��L�L��!$�Y�^�K�%@�%@�!A�!A��#1� :� :� :�
��w���}�5�5���
e�&�x�0�0�1�1�!�w�}�}�]�;�;�e�2�7�CS�CS�Ta�Cb�Cb�Cm�Cm��G�,�,�\�:�:�R�V�C�D�D�e�#-�-c�R_�-c�-c�"d�"d�d��	�-�0�0�0�1�1�1�1�1�1�1�1�1�1�1����1�1�1�1��q�L�G�G�� �e�e�e��q�L�G��L�L�!Q�S`�bc�d�d�d�d�d�d�d�d�����e����= 
e�D	��<��)�)�0�D�D���	�	�	��J�J�S�U]�^�^�^��H�	�����	�t�s�{�{�"�'�-�-��*=�*=�{��L�L�S�U]�_c�d�d�d������d�M�9�k�J�J��
	m��x�(�(�
&�
&��w���v�.�.���
&�
&�
&�
&�
&�
&�
&��X�f�%�%���<��
�+�+�&��M�&�)�)�)�)��I�f�%�%�%�
&�
&�
&�
&�
&�
&�
&�
&�
&�
&�
&����
&�
&�
&�
&����	q�	q�	q��w�%�,�&�&����Z�\b�dl�no�p�p�p�����������	m�	m�	m��L�L�V�X^�`h�jk�l�l�l�l�l�l�l�l�����	m����	�I�I�K�W�V]�^�^�^�^�^s��A�
B�!A=�=B�C�)C�C�C�C�C�C�
D�&D�D�K,�,B"K	�K,�K
�K,�!K
�"K,�,
L	�6L	�L	�#L=�= M!� M!�Q8�!Q+�<Q8�	AQ+�Q8�+Q/	�/Q8�2Q/	�3Q8�8
S%�-R5�5
S%�S � S%c�<��t����fd���}|S)z'Decorator to check for license validityc�L��t��st����|i|��S)zLicense check wrapper)r*r7)rirjrms  �rn�wrapperz(check_license_decorator.<locals>.wrapperCs2������	'�$�&�&�&��t�T�$�V�$�$�$rr)r)rmrzs` rn�check_license_decoratorr{@s5����4�[�[�%�%�%�%��[�%��Nrrr�c��	t|��\}}||fS#t$r!ttd��d|i����wxYw)z�
    Validates domain, determines it's owner and docroot or exit with error
    :param domain: Domain name to check
    :return: Tuple (username, docroot)
    zNo such domain: %(domain)s.r�r)r+rr5r	)r��
document_root�owners   rn�check_domainrMse��d�&�v����
�u��m�#�#���d�d�d���(E� F� F�QY�[a�Pb�c�c�c�c�d���s	��+Ar��attemptsc���tj}|�|tjz}	tj|tjtjztjztjzd��}tj
tj|��j��dzr�tj
|��	tj|��n#t$rYnwxYwtj|tjtjztjztjztjzd��}tj|d��}t%|pd��D]O}	tj|���|��n@#t*$rt-jd��Y�LwxYwt1t3d�����n*#t4$rt1t3d�����wxYw|S)	zE
    Try to take lock on file with specified number of attempts.
    Nr,�?za+rAg333333�?zwAnother utility instance is already running. Try again later or contact system administrator in case if issue persists.z%IO error happened while getting lock.)�fcntl�LOCK_EX�LOCK_NBr��open�O_RDWR�O_CREAT�
O_NOFOLLOW�	O_CLOEXECrd�S_IMODE�fstatrf�closerbr��O_EXCL�fdopen�range�flock�filenor�r��sleep�LockFailedExceptionr	r�)r�r��	lock_type�fd�lock_fdrSs      rn�	lock_filer�[s����
�I����U�]�"�	�&T��W�T�2�9�r�z�1�B�M�A�B�L�P�RW�
X�
X���<������,�-�-��5�	�
�H�R�L�L�L�
��	�$������$�
�
�
���
��������	�B�J�&���2�R�]�B�R�\�Q����B�
�)�B��%�%���x�}�1�%�%�
	�
	�A�
 ���G�N�N�,�,�i�8�8�8�����
 �
 �
 ��
�3������
 ����&��1�����
�	���T�T�T�!�'�*Q�"R�"R�S�S�S�T�����NsU�B	F5�)B>�=F5�>
C�F5�
C�A?F5�'E4�2F5�4F�F5�F�F5�5'Gc��d}d}tj�|��}tj�|��}|s!|st|dt	d��i��d}|rW	tjddgddd	�
��}|jpd|jpdz}d|vrd}n"#tt
j
tf$rYnwxYwd}|rtj�d
��}|s#|s#t|dt	d��i��dSdSdS)zb
    Validate that MAx Cache is installed and its module is loaded
    (Apache and/or nginx).
    z!/opt/cloudlinux/maxcache/.versionz'/opt/cloudlinux/maxcache/.nginx-versionrZzMAx Cache is not installed.F�	apachectlz-MT�)�capture_output�text�timeoutr+�maxcache_modulez0/etc/nginx/conf.d/modules/ea-nginx-maxcache.confz;MAx Cache module is not enabled (neither Apache nor nginx).N)r�r�r"r`rS�
subprocess�run�stdout�stderrr��SubprocessErrorr�)	r��apache_version_path�nginx_version_path�apache_installed�nginx_installed�apache_module_ok�proc�output�nginx_module_oks	         rn�ensure_maxcache_availabler��so��
>��B���w�~�~�&9�:�:���g�n�n�%7�8�8�O��N�O�N��w��1�-J�+K�+K� L�M�M�M�����	��>��d�#�#���	���D��k�'�R�D�K�,=�2�>�F� �F�*�*�#'� ���!�:�#=�w�G�	�	�	��D�	�����O��]��'�.�.�)[�\�\���
�O�
���
�q�V�W�W�X�	
�	
�	
�	
�	
�
�
�
�
s�+3B�B>�=B>c��eZdZdZdS)r�z,
    Exception when failed to take lock
    N)r_r�r�r�rprrrnr�r��s��������	�Drrr��
�
resource_pathc#�VK�t|dz|��}dV�t|��dS)z�
    Lock a file, than do something.
    Make specified number of attempts to acquire the lock,
    if attempts is None, wait until the lock is released.
    Usage:
    with acquire_lock(path, attempts=1):
       ... do something with files ...
    z.lockN)r��release_lock)r�r�r�s   rn�acquire_lockr��s9�����
��/��:�:�G�	�E�E�E�������rrc��	tj|���tj��n#t$rYnwxYw|���dS)z
    Releases lock file
    N)r�r�r��LOCK_UNr�r�)�
descriptors rnr�r��sd��
�
��J�%�%�'�'���7�7�7�7���
�
�
���
�����������s�14�
A�A�php_versionrVc��d}d|jkr5|�|��r"ttd��d|i����dSdS)zX
    Ensures wp-cli is compatible, e.g some
    php modules may prevent stable work
    �
snuffleupaguszea-php74z�Seems like ea-php74 %(module)s module is enabled. It may cause instabilities while managing Object Caching. Disable it and try again�modulerN)�
identifier�is_extension_loadedr8r	)r��dangerous_modules  rn�wp_cli_compatibility_checkr��sn��'���[�+�+�+��0O�0O�P`�0a�0a�+�'��;���
�/�0�

�
�
�	
�,�+�+�+rr�hidec�v�ddtjddd|iii��dg}tt|��\}}||fS)zf
    Call cloudlinux-config utility
    to hide/show WPOS icon in user's control panel interface.
    �setz--data�options�
uiSettings�hideAccelerateWPAppz--json)r�r�r1rE)r��params�
returncoder�s    rn�set_wpos_icon_visibilityr��sS��	���
�I��/D�d�.K�L�M�N�N��	�F�&�&6��?�?��J���v��rrr�c�F�t���|d��S)zP
    Check the current state of WPOS icon in user's control panel interface
    r�)r;�	get_param��	icon_names rn�is_ui_icon_hiddenr��s���:�:���	�<�8�8�8rrc�h�ttj�t��|g��S)z[
    1. xray utility exists = alt-php-xray package installed
    2. feature is visible
    )�allr�r�r!rP)�feature_visibles rn�should_xray_user_agent_enabledr�s&��
�����3�4�4�o�F�G�G�Grrc���ttj�t��tj�t
��t
d���g��S)z�
    1. xray utility exists = alt-php-xray installed
    2. xray socket exists
    3. end-user plugin was not enabled by admin = hidden in UI
    �hideXrayAppr�)r�r�r�r!rPrQr�rprrrn�should_xray_user_agent_disabledr�	sM����G�N�N�/�0�0��G�N�N�+�,�,��
�6�6�6�	
���rrc�l�eZdZUee���Zeeed<ee���Z	eeed<dS)�WHMCSServerWideOptions��default_factory�allowed_suites�visible_suitesN)
r_r�r�rr/r�rr�__annotations__r�rprrrnr�r�sT�������%*�U�4�%@�%@�%@�N�H�T�N�@�@�@�%*�U�4�%@�%@�%@�N�H�T�N�@�@�@�@�@rrr�c�n�eZdZUdZeed<eed<eed<eed<eed<eed<ee���Z	eed	<d
Z
eeed<d
Zeeed<d
Z
eeed
<d
Zeeed<d
Zeeed<d
Zeeed<dd�Zed���Zed���Zed���Zed���Zd
S)�ServerWideOptionsz�
    Options holder representing server-wide option
    available for reading for any user on server.

    Only can be changed by root.
    �	show_iconr�r��supported_suites�supported_features�hidden_featuresr��
whmcs_optionsNr�"disable_smart_advice_notifications�%disable_smart_advice_wordpress_plugin�disable_smart_advice_reminders�upgrade_url�upgrade_url_cdn�object_cachec	���	�ddlm�	m�i�	fd��	jD����fd��jD���}||vrdSd}|�	jvr|j�dS|j}|�jvr|j�dS|j}|�dSt
t|����}tt|d����}|�
t||��|t��dd||d	���t|��|d<t|��S)
z�
        Append some needed arguments to upgrade url to make it specific for user.
        Please pay attention that we add *customer_name* instead of system user,
        that may be different on plesk.
        r)�PremiumSuite�CDNSuiteProc� ��i|]
}|�j��Srpr�)r:�featurer�s  �rnr<z>ServerWideOptions.get_upgrade_url_for_user.<locals>.<dictcomp>Cs���W�W�W�g�w��)�W�W�Wrrc� ��i|]
}|�j��Srpr�)r:r�r�s  �rnr<z>ServerWideOptions.get_upgrade_url_for_user.<locals>.<dictcomp>Ds���U�U�U�W�w��(�U�U�UrrNr��cloudlinux_advantage�provisioning)r�r��	server_ip�m�action�suite)�clwpos.feature_suitesr�r��primary_featuresr�r�r/rr�rr�r,r-r
r
)
rhr�r�r��feature_to_suite�
target_url�	url_parts�queryr�r�s
        @@rn�get_upgrade_url_for_userz*ServerWideOptions.get_upgrade_url_for_user7sd����	D�C�C�C�C�C�C�C�

�W�W�W�W��9V�W�W�W�
�U�U�U�U��8T�U�U�U�
��
�*�*�*��4��
��l�3�3�3���'��t��)�J��k�2�2�2��#�+��t��-�J����4���*�-�-�.�.�	��Y�y��|�,�,�-�-��
���,�X�v�>�>� �*�_�_�+�(�)�'�2�

�
�		
�		
�		
�!��'�'�	�!���)�$�$�$rrc��ttt|j��t|jj��z����Sr{)r/r�r�r�r�s rn�allowed_suites_listz%ServerWideOptions.allowed_suites_listi�6���C��T�0�1�1�D��9K�9Z�4[�4[�[�\�\�]�]�]rrc��ttt|j��t|jj��z����Sr{)r/r�r�r�r�s rn�visible_suites_listz%ServerWideOptions.visible_suites_listmr�rrc���ddlm}t��}�jD].}|��fd�||jD�����/|S)NrA��
ALL_SUITESc�&��h|]
}|�jv�|��Srp�r��r:r�rhs  �rn�	<setcomp>z5ServerWideOptions.allowed_features.<locals>.<setcomp>y�'���l�l�l�W��TX�Tk�Ik�Ik��Ik�Ik�Ikrr)�feature_suitesrr�r�r��feature_set)rhr�_allowed_featuresr�s`   rn�allowed_featuresz"ServerWideOptions.allowed_featuresqsw���	/�.�.�.�.�.��E�E���-�	�	�E��$�$�l�l�l�l�
�5�(9�(E�l�l�l�
�
�
�
�!� rrc���ddlm}t��}�jD].}|��fd�||jD�����/|S)NrArc�&��h|]
}|�jv�|��Srprrs  �rnrz5ServerWideOptions.visible_features.<locals>.<setcomp>�rrr)rrr�r�r�r	)rhr�_visible_featuresr�s`   rn�visible_featuresz"ServerWideOptions.visible_features}su���.�.�.�.�.�.��E�E���-�	�	�E��$�$�l�l�l�l�
�5�(9�(E�l�l�l�
�
�
�
�!� rr)r�)r_r�r�r�r-r�rrr�r�rrr�r�r�r�rgr�r��propertyr�r�rrrprrrnr�r�s�����������O�O�O���������������������,1�E�BX�,Y�,Y�,Y�M�)�Y�Y�Y�37� �(�4�.�7�7�7�9=�&����=�=�=�<@�)�8�D�>�@�@�@�59�"�H�T�N�9�9�9�!%�K��#��%�%�%�%)�O�X�c�]�)�)�)�0%�0%�0%�0%�d�^�^��X�^��^�^��X�^��	!�	!��X�	!��!�!��X�!�!�!rrr�)�maxsizec��ddlm}	td|ji��}|d|d}}n#t$rd\}}YnwxYw||fS)Nr��
WposDaemon�command�accelerate_wp_premium�accelerate_wp_cdn�FF)�
clwpos.daemonrr�&DAEMON_GET_SUPPORTED_SUITES_BY_LICENSEr5)r�suites_statuses_by_license�awp_premium_status�awp_cdn_statuss    rn�.get_suites_status_from_license_daemon_fallbackr�s���(�(�(�(�(�(�:�%7��:�L�
�&
�&
�"�
'�'>�?�&�':�;�+�����:�:�:�-9�*��N�N�N�:�����~�-�-s�&/�A�Ac���ddlm}m}m}t	j��st
��\}}nt��\}}|j||j||j|i�	|d��S)Nr)r�r��CDNSuiteT)
r�r�r�r r�r��get_suites_status_from_licenserr�r�)r�r�r�r rrs      rn�is_feature_supported_by_licenser"�s���I�I�I�I�I�I�I�I�I�I�
�:�<�<�^�-K�-M�-M�*��N�N�-[�-]�-]�*��N���-��
�~���.��
�c�%����	rrc���ddlm}t��}|���D]:\}}|jD]-}|jrt
|��r|�|���.�;|S)NrAr)rrr�rG�features�IS_BILLABLEr"�add)rr��
suite_name�
suite_item�
suite_features     rn�get_supported_featuresr*�s���*�*�*�*�*�*�����",�"2�"2�"4�"4�6�6��
�J�'�0�	6�	6�M� �,�
6�0O�PZ�0[�0[�
6�"�&�&�}�5�5�5��	6��rrc
���ddlm}m}m}m}m}t
���dd��}g}g}|j|j|j|jfD]W}tj
�t|��s�-|�
|��|�
|���Xt|||t|��tt!����g���S)zw
    Return default content of /opt/clwpos/public_config.json.
    This file is accessible by all users on server.
    rA)�AWPSuiter�r r��SUPPORTED_SUITESr�r�)r�r�r�r�r�r�)rr,r�r r�r-r;r�r�r�r�r"rOrHr�r/r*)	r,r�r r�r-�is_icon_hiddenr�r�r�s	         rn�get_default_server_wide_optionsr/�s��`�_�_�_�_�_�_�_�_�_�_�_�_�_��Z�Z�)�)�*?��N�N�N��N��N�
�#�X�]�H�M�;�CS�T�%�%���w�~�~�n�U�3�4�4�	�����e�$�$�$����e�$�$�$�$��$�$�%�%��.�/�/�� 6� 8� 8�9�9��
���rrc��ddlm}m}m}m}t��\}}t
j||||gdd||g��}d�|D��S)zY
    Get list of supported suites taking
    into account license and status on CLN.
    rA)r,r�r r�Tc��g|]	}|j��
Srpr�)r:r�s  rnrTz(get_supported_suites.<locals>.<listcomp>�s��+�+�+�5�E�J�+�+�+rr)rr,r�r r�r!�	itertools�compress)r,r�r r��is_awp_premium_allowed�is_awp_cdn_allowed�suitess       rn�get_supported_suitesr7�s|��
N�M�M�M�M�M�M�M�M�M�M�M�1O�1Q�1Q�.��.�
�
�	�<��;�7�$��FX�Zl�9m���F�,�+�F�+�+�+�+rrc��td���x}}tj�t��rM	t��}n#t$rYdSwxYw|�d|��}|�d|��}||fS)NTr�rr4r5)r�r�r�r!r&�_get_jwt_payloadr#r�)r4r5�jwts   rnr!r!�s���2F�d�2S�2S�2S�S��/�	�w�~�~�(�)�)�O�	 �"�$�$�C�C��&�	 �	 �	 ��<�<�	 ����"%���)A�CY�!Z�!Z�� �W�W�%9�;M�N�N��!�#5�5�5s�A�
A�Ac��tt��}	t|d���}n,#t$r}t	dt�d|�����d}~wwxYw|S)z1
    Read jwt, verify it and return payload.
    T)�
verify_expz)Unable to detect edition from jwt token: z-. Please, make sure it is not broken, error: N)r$r&r%r(r#)�tokenr:rks   rnr9r9�s���
�'�(�(�E�
���4�0�0�0�����
�
�
�%�
>�8J�
>�
>�:;�
>�
>�
�
�	
�����
����
�Js�(�
A�A�Ac�f�t|jt��r|jStdi|j��S)Nrp)rdr�r�)�server_wide_optionss rn�get_whcms_server_wide_optionsr@s:���%�3�5K�L�L�1�"�0�0�!�F�F�$7�$E�F�F�Frrc�b��ddlm}t��}tj�t��s|Sdh�ttd��5}|���}	tj
|��}dD]�}||vr�|�|��}t||��}ttt|tt|���t|������z������}t#|||����|j��fd�|���D����t+|��|_	t/��}	|jdd�D] }
|
|	vr|j�|
���!n#t4$r
d|_YnwxYw|cddd��S#tjj$r8}t;t=d��t?|��d	ti�
���d}~wwxYw#1swxYwYdS)zL
    Gets server wide options which apply
    as defaults for all users
    rArr��r)r�r�r�c�$��i|]\}}|�v�	||��
Srprp)r:rBrC�non_overridable_fieldss   �rnr<z+get_server_wide_options.<locals>.<dictcomp>,s+���,w�,w�,w�d�a��WX�`v�Wv�Wv�Q��Wv�Wv�WvrrNzZFile is corrupted: Please, delete file %(config_file)s or fix the line provided in details�config_filer�) rrr/r�r�r"rNr�r.r��loadsr��getattrr/�sortedr��intersection�setattr�__dict__r�rGr@r�r7r��remove�PermissionError�decoder�JSONDecodeErrorr5rSrg)
r�default_options�f�content�
configuration�option_to_merge�suites_from_config�suites_from_defaults�
merged_values�server_suites_allowedr��errrDs
            @rnrr
s����
+�*�*�*�*�*�5�7�7�O�
�7�>�>�.�)�)����2�3��	
�n�c�	"�	"�*�a��&�&�(�(��'	�"&�*�W�"5�"5�M�$\�

I�

I��"�-�7�7��%2�%6�%6��%G�%G�"�'.���'P�'P�$�!%��3�3�d�3�?Q�;R�;R�;_�;_�`c�dn�`o�`o�;p�;p�6q�6q�q�r�r�s�s�!�!�
����-�H�H�H�H�
�$�+�+�,w�,w�,w�,w�m�>Q�>Q�>S�>S�,w�,w�,w�x�x�x�,I�/�,Z�,Z�O�)�	
G�(<�(>�(>�%�-�=�a�a�a�@�G�G�E��$9�9�9�'�8�?�?��F�F�F��G��#�
8�
8�
8�48��0�0�0�
8����#�I*�*�*�*�*�*�*�*��J�|�+�	�	�	���v�w�w��C���&��7����
�����	����K*�*�*�*����*�*�*�*�*�*sU�H$�*DG�1F0�?1G�0G�G�G�G�H!�)3H�H!�!H$�$H(�+H(c#�K�tj�t��st	dt�d����t��}|V�t
t��5ttd��5}tj
t|��|��ddd��n#1swxYwYddd��dS#1swxYwYdS)z1Set icon visibility in clwpos public options filez6Can't write public options as configuration directory z does not exist�wN)r�r�r!rIr�rr�rNr�r��dumpr)�public_config_datarQs  rn�write_public_optionsr^DsF�����7�>�>�.�)�)�
��d�^�d�d�d�
�
�	
�1�2�2��
����	�n�	%�	%�1�1�t�N�C�'@�'@�1�A��	�&�+�,�,�a�0�0�0�1�1�1�1�1�1�1�1�1�1�1����1�1�1�1�1�1�1�1�1�1�1�1�1�1�1�1����1�1�1�1�1�1s6�%C�;#B*�C�*B.	�.C�1B.	�2C�C�	Cc	�����tj|ftjtjdddd�|��}ddd����fd���fd�d	|jfd
|jffD��}|D]}|����|�dnt
j��|z}d}|D]U}|�dn#td
|t
j��z
��}	|�	|	��|�
��rd}�V|���	|�d���n=#tj
$r+|���|���YnwxYw|j|jfD]'}
	|
����#t $rY�$wxYw|rtj
||���tj||j�d	�d
��S)a�
    Run *command* like ``subprocess.run(capture_output=True)`` but read at most
    *max_output_bytes* characters from each of stdout/stderr into this process'
    memory, then terminate and reap the child.

    A plain ``timeout`` bounds only a *silent* stall: a child that floods a huge
    reply fast returns well under the timeout with the whole payload already
    buffered into (our, possibly root) RSS. Capping the bytes we read closes
    that memory-exhaustion vector for untrusted/tenant-controlled endpoints.

    Returns a ``subprocess.CompletedProcess``; raises
    ``subprocess.TimeoutExpired`` when the child produced no bound within
    *timeout* (the silent-stall case), matching ``subprocess.run(timeout=...)``.
    T�utf-8�replace)r�r�r��encodingrcr+)r�r�c�j��	|�����|<dS#ttf$rYdSwxYwr{)r.r�r�)r��stream�captured�max_output_bytess  ��rn�_drainz#_run_bounded_output.<locals>._drainosK���	�
$�[�[�)9�:�:�H�T�N�N�N����$�	�	�	��D�D�	���s��2�2c�H��g|]\}}tj�||fd�����S)T)ruri�daemon)�	threading�Thread)r:r�rdrgs   �rnrTz'_run_bounded_output.<locals>.<listcomp>ysC�������D�&�	���d�F�^�D�I�I�I���rrr�r�NFgrA)r�)r��Popen�PIPEr�r��startr��	monotonic�maxr��is_alive�	terminate�wait�TimeoutExpired�killr�r��CompletedProcessr�)
rrfr��popen_kwargsr��readers�thread�deadline�	timed_out�	remainingrdrgres
 `         @@rn�_run_bounded_outputr}Us@�������������
���
�����D���+�+�H�����������&���4�x���6M�N����G��������������t�t�D�N�,<�,<�w�,F�H��I�����$�,�D�D�#�c�8�d�n�FV�FV�;V�2W�2W�	����I�����?�?���	��I��	�N�N������	�	�!�	�������$�����	�	�����	�	�����������;���,����	��L�L�N�N�N�N���	�	�	��D�	�����:��'���9�9�9��&�w����(�AS�U]�^f�Ug�h�h�hs$�;D�7E�E� E5�5
F�F)z	--api_keyz--unique_idz	--api-keyz--tokenz--secretc��t|t��}|r|�d��}n.t|ttf��rt|��}n|Sg}d}|D]y}|r|�d��d}�t|t��r3tD]+}||krd}n |�|dz��rd|z}n�,|�|���z|rd�|��St|��|��S)a�
    Return a log-safe copy of a command with secret-bearing argument values
    masked. Accepts an argv list/tuple or a full command string and returns the
    same type. Only the value of a known secret flag (e.g. --api_key=<uuid>,
    --unique_id=<uuid>) is replaced with <redacted>; the flag name is kept so
    logs stay useful. The command actually passed to exec is never modified.
    � Fz
<redacted>T�=z
%s=<redacted>)
rdrg�splitr/�tuplerH�_SECRET_ARG_FLAGSr�r��type)r�	is_string�tokens�redacted�redact_nextr=�flags       rn�redact_command_secretsr��s:���7�C�(�(�I������s�#�#���	�G�d�E�]�	+�	+���g��������H��K������	��O�O�L�)�)�)��K���e�S�!�!�		�)�
�
���D�=�=�"&�K��E��#�#�D�3�J�/�/��+�d�2�E��E��	���������"��x�x��!�!�!��4��=�=��"�"�"rrc�H�d}t��}d|vr
|r||dd<tjdtt	|����t|�d������t
��sttj	��s�|�At|f||ttj
��tj����d�|��Stj|fddddttj
��tj����|d	�|��Stj
��d
kr-t#t%d��t	|��z����t'|t��rt(d
z|z}nt(dg|z}|�At|f||ttj
��tj����d�|��Stj|fttj
��tj����dddd|d�|��S)aV
    Wrapper for subprocess to enter cagefs
    do not enter cagefs if:
     - CloudLinux Solo
     - if process already started as user in cagefs

    :param timeout: optional subprocess timeout in seconds; None (default)
        keeps the previous unbounded behaviour for existing callers. Pass a
        bound when the command talks to an untrusted/tenant-controlled endpoint
        so a non-responding child cannot block the caller indefinitely.
    :param max_output_bytes: optional cap on how many characters are read from
        the child's stdout/stderr into memory; None (default) keeps the previous
        unbounded ``capture_output`` behaviour for existing callers. Pass a small
        bound when the command talks to an untrusted/tenant-controlled endpoint
        so a fast flood cannot exhaust memory even before the timeout fires.
    T�env�LANG�*Executing command: %s with environment: %sN)rfr��
preexec_fnr`ra)r�r�rbrcr�r�rz�Internal error: command %s must not be run as root. Please contact support if you have questions: https://cloudlinux.zendesk.com�rbz --no-io-and-memory-limit �--no-io-and-memory-limit)r�r�r�rbrcr�)r@rXrkrgr�r�r<r?r>�CAGEFSr}r3r�r��getegidr�r�r5r	rdrH)rr�rfrj�__traceback_hide__�locale�with_cagefs_enters       rn�run_in_cagefs_if_neededr��sf��*��
#�
%�
%�F�����6�� &��u�
�f���L�4�c�:P�QX�:Y�:Y�6Z�6Z�\_�`f�`j�`j�kp�`q�`q�\r�\r�����{�{�2
�4�W�^�D�D�2
��'�&���!1��!�"�*�,�,��
���=�=�	��
���
��~��	
������b�j�l�l�B�J�L�L�9�9��	
�	
��	
�	
�		
��:�<�<�1�����5���
)��1�1�2����
��g�s�#�#�	Z� 1�4P� P�SZ� Z���!2�4N� O�RY� Y���'�&�!��!1��!�"�*�,�,��
���=�=�	��
���
��~��	
��b�j�l�l�B�J�L�L�9�9������	
�	
��	
�	
�		
rrc�r�	t���|��S#tj$rYdSwxYw)z
    Returns uid for user
    N)r�get_uid�NoSuchUserExceptionr�s rn�uid_by_namer�sB����w�w���t�$�$�$���$�����t�t����s� #�6�6c�D�	t|��S#t$rYdSwxYwr{)r�rf)�uids rn�name_by_uidr�"s7���"�3�'�'�'�������t�t����s��
�c� �eZdZdZdd�Zd�Zd�Zdedeefd�Z	dd	�Z
dd�Zdd�Zded
e
efd�Zd�Zdedeed
efd�Zed���Zdd�Zd�Zdedeed
efd�Zdede
ed
efd�Zdede
ed
efd�ZdS)�PhpIniConfigz>
    Helper class to update extensions in php .ini files.
    Nc�4�||_tjd��|_tjd��|_tjd��|_|pt
jt��|_	td|jj��d����f|_dS)Nz-^;\s*extension\s*=\s*(?P<module_name>\w+)\.soz,^\s*extension\s*=\s*(?P<module_name>\w+)\.soz1^\s*;?\s*extension\s*=\s*(?P<module_name>\w+)\.soz/var/cagefs/*/*/etc/cl.php.d/c�8�|�d��dS)Nr�r�)r�)r�s rn�<lambda>z'PhpIniConfig.__init__.<locals>.<lambda>8s��fj�fp�fp�qt�fu�fu�vw�fx�rr)r��user)
r��re�compile�disabled_pattern�enabled_pattern�	extensionrXrYr_r^r�r��wildcard_ini_user_locations)rhr��
custom_loggers   rnrxzPhpIniConfig.__init__.s���&��� "�
�+[� \� \���!�z�*Y�Z�Z�����$X�Y�Y���#�B�w�'8��'B�'B���
�R�T�5E�5P�R�R�Yx�Yx�
�
�
�,
��(�(�(rrc���	|�d��d�d��dS#t$r4}|j�d|t	|����Yd}~dSd}~wwxYw)z-
        Parse .so extensions safely
        r�rAz.sorz4Cannot parse extension name from line: %s, error: %sN)r�rfr^r\rg)rhr'rks   rn�_parse_extension_namez"PhpIniConfig._parse_extension_name<s���	��:�:�c�?�?�1�%�+�+�E�2�2�1�5�5���	�	�	��K��� V�X\�^a�bc�^d�^d�e�e�e��4�4�4�4�4�����	���s�36�
A4�)A/�/A4c�z�tj�|jj|��}tj�|��sgSt
|��5}|���}ddd��n#1swxYwYg}|D]/}|�|��}|s�|�	|���0|Sr{)
r�r�r�r��dirr!r��	readlinesr�rH)rh�ini_path�	full_pathrQ�ini_content�modules�ext�raw_module_names        rn�get_ini_contentzPhpIniConfig.get_ini_contentFs����G�L�L��!1�!5�x�@�@�	��w�~�~�i�(�(�	��I�
�)�_�_�	(���+�+�-�-�K�	(�	(�	(�	(�	(�	(�	(�	(�	(�	(�	(����	(�	(�	(�	(����	,�	,�C�"�8�8��=�=�O�"�
���N�N�?�+�+�+�+��s�A<�<B�Br�r�c��tj�|jj|��}tj�|��s|�||d���dS|�||��dS)NF�r!)r�r�r�r�r�r!�_write_modules�enable_modules)rhr�r�r�s    rn�create_custom_inizPhpIniConfig.create_custom_iniWst���G�L�L��!1�!5�t�<�<�	��w�~�~�i�(�(�	/����	�7�5��A�A�A�A�A�
����g�.�.�.�.�.rrc�.�|rztj�|jj|��}tj�|��r1|j�d|����tj|��|�	dgd���dS)NzCustom ini to be removed: zacceleratewp.iniT)rL)
r�r�r�r�r�r!r^r\rb�update_user_ini)rhr��all_inir�s    rn�remove_custom_inizPhpIniConfig.remove_custom_inias����	%�����T�%5�%9�4�@�@�I��w�~�~�i�(�(�
%���!�!�"J�y�"J�"J�K�K�K��	�)�$�$�$����/��D��A�A�A�A�ArrFc	���|jD]`}t|d��}|D]F}	|�|||||���#t$r|j�d��Y�CwxYw�adS)Nr�z&Error updating single acceleratewp.ini)r�r�_update_single_inirfr^re)rh�ini_filenamer�rL�location�cagefs_paths�dir_paths       rnr�zPhpIniConfig.update_user_iniis����8�	�	�H� ��&�!1�2�2�L�(�
�
����+�+�H�h���W]�^�^�^�^�� �����K�)�)�*R�S�S�S��H�����
�	�	s�>�$A%�$A%c	��|d|��}tj�||��}t|��5t	��5|rOtj�|��r/|j�d|��tj|��n5|�	||tj�|�����ddd��n#1swxYwYddd��dS#1swxYwYdS)Nr�z#Custom user ini: %s will be removedr�)
r�r�r�rrr!r^r\rbr�)rhr�r�r�r�rLr�r�s        rnr�zPhpIniConfig._update_single_iniss���#�8�F�#�H�-�-���w�|�|�H�l�3�3��
�X�
&�
&�	P�	P�
���	P�	P��
P��7�>�>�$�'�'�$��K�%�%�&K�T�R�R�R��I�d�O�O�O���#�#�D�'�"�'�.�.��:N�:N�#�O�O�O�
	P�	P�	P�	P�	P�	P�	P�	P�	P�	P�	P����	P�	P�	P�	P�	P�	P�	P�	P�	P�	P�	P�	P�	P�	P�	P�	P����	P�	P�	P�	P�	P�	Ps7�C;�BC#�C;�#C'	�'C;�*C'	�+C;�;C?�C?r�c�t��t|d��5}�fd�|D��cddd��S#1swxYwYdS)zU
        Return enabled modules.
        :param path: full path to .ini file
        rBc���h|]I}�j�|����j�|���d����JS�N�module_name�r��match�group�r:r'rhs  �rnrz0PhpIniConfig._enabled_modules.<locals>.<setcomp>�sY��������'�-�-�d�3�3�?��$�*�*�4�0�0�6�6�}�E�E�?�?�?rrN�r��rhr�rQs`  rn�_enabled_moduleszPhpIniConfig._enabled_moduless����
�$��_�_�	����������	�	�	�	�	�	�	�	�	�	�	�	����	�	�	�	�	�	��-�1�1c�t��t|d��5}�fd�|D��cddd��S#1swxYwYdS)NrBc���h|]I}�j�|����j�|���d����JSr�)r�r�r�r�s  �rnrz0PhpIniConfig._extensions_list.<locals>.<setcomp>�sV������DH�QU�Q_�Qe�Qe�fj�Qk�Qk�Qw���$�$�T�*�*�0�0��?�?�Qw�Qw�Qwrrr�r�s`  rn�_extensions_listzPhpIniConfig._extensions_list�s����
�$��_�_�	������LM����	�	�	�	�	�	�	�	�	�	�	�	����	�	�	�	�	�	r�c�$�tj�|jj|��}tj�|��sdS|j�d|����t|��}|r|�	||��dS)z�
        Enable specified modules in .ini php file.
        :param path: path to .ini file related to php directory
        :param modules: list of modules that should be enabled
        FzEnable such extensions: T)
r�r�r�r�r�r!r^r\r�r�)rhr�r�r��modules_to_enables     rnr�zPhpIniConfig.enable_modules�s����G�L�L��!1�!5�t�<�<�	��w�~�~�i�(�(�	��5�����>�W�>�>�?�?�?���L�L���	>����	�+<�=�=�=��trrc��d|�d�S)z-
        redis -> extension=redis.so
        z
extension=z.so
rp)r�s rn�_format_as_ini_extzPhpIniConfig._format_as_ini_ext�s��
*�F�)�)�)�)rrTc	���	�g}�j�d|����|r�t|��}t|��5}|���D]U�	t�	�fd�|D����r"�j�d�	�d|�d����@|��	���V	ddd��n#1swxYwYt|��}d|vr|�d����|D]I}��	|��}�j�d	|����|�|���J|rE�j�d
|����td�|��|dd
���dSdS)Nz,Such extensions are required to be enabled: c3�F�K�|]}��|���vV��dSr{)r�)r:r�r'rhs  ��rnr?z.PhpIniConfig._write_modules.<locals>.<genexpr>�s6�����]�]�C�4�2�2�3�7�7�4�?�]�]�]�]�]�]rrzSkip z, z will be added further�redisc�,�|�d��S�Nr�)�endswith)�xs rnr�z-PhpIniConfig._write_modules.<locals>.<lambda>�s��a�j�j��.A�.A�rr��keyzAppending lines to be written: zPath to write: r+�T)�
copy_owner)r^r\r�r�r�rFrHrH�sortr�rr�)
rhr�r�r!�
new_ini_linesrQ�sorted_modulesr��extension_liner's
`        @rnr�zPhpIniConfig._write_modules�s�����
�����\�IZ�\�\�]�]�]��	/� #�$5� 6� 6���i���
/�A��K�K�M�M�/�/�D��]�]�]�]�]�K\�]�]�]�]�]�!���)�)�*c�$�*c�*c�:K�*c�*c�*c�d�d�d� �!�(�(��.�.�.�.�	/�
/�
/�
/�
/�
/�
/�
/�
/�
/�
/�
/����
/�
/�
/�
/� � 1�2�2���n�$�$����$A�$A��B�B�B�$�	1�	1�F�!�4�4�V�<�<�N��K���P��P�P�Q�Q�Q�� � ��0�0�0�0��	_��K���;�	�;�;�<�<�<�#�B�G�G�M�$:�$:�I�u�Y]�^�^�^�^�^�^�	_�	_s�A+B:�:B>�B>c� �tj�|jj|��}tj�|��sgSt
|�|����}|j�	d|�d|����|S)zH
        Reads <ext>.ini file and loads all required extensions
        zRequired extensions for z are: )
r�r�r�r�r�r!r/r�r^r\)rhr�r��required_moduless    rn�get_required_modulesz!PhpIniConfig.get_required_modules�s����G�L�L��!1�!5�t�<�<�	��w�~�~�i�(�(�	��I��� 5� 5�i� @� @�A�A������S�T�S�S�AQ�S�S�T�T�T��rrc�����tj��jj|��}tj�|��sdSt
|����|��z��rlt|��5}��fd�|�	��D��}ddd��n#1swxYwYtd�|��|d��dS)z�
        Disable specified modules in .ini php file.
        :param path: path to .ini file related to php directory
        :param modules: list of modules that should be disabled
        Fc�<��g|]}��|�����Srp)�_disable_module)r:r'�modules_to_disablerhs  ��rnrTz0PhpIniConfig.disable_modules.<locals>.<listcomp>�s+��� j� j� j�TX��!5�!5�d�<N�!O�!O� j� j� jrrNr+r�T)r�r�r�r�r�r!r�r�r�r�r)rhr�r�r�rQr�r�s`     @rn�disable_moduleszPhpIniConfig.disable_modules�s�����G�L�L��!1�!5�t�<�<�	��w�~�~�i�(�(�	��5� ��\�\�D�,A�,A�)�,L�,L�L���	N��i���
k�A� j� j� j� j� j�\]�\g�\g�\i�\i� j� j� j�
�
k�
k�
k�
k�
k�
k�
k�
k�
k�
k�
k����
k�
k�
k�
k�#�B�G�G�M�$:�$:�I�u�M�M�M��ts�"B2�2B6�9B6r'r�c���|j�|��}|�U|�d��}||vr<|�|��|�d�����S|S)zV
        Search for disabled module in line, uncomment line to enable module.
        Nr��;)r�r�r�rLr�)rhr'r�r�r�s     rn�_enable_modulezPhpIniConfig._enable_module�st���%�+�+�D�1�1�����+�+�m�4�4�K��/�/�/�!�(�(��5�5�5��{�{�3�'�'�.�.�0�0�0��rrr�c�z�|j�|��}|�|�d��}||vrd|��S|S)zT
        Search for enabled module in line, comment line to disable module.
        Nr�r�r�)rhr'r�r�r�s     rnr�zPhpIniConfig._disable_module�sN���$�*�*�4�0�0�����+�+�m�4�4�K��0�0�0�!�4�z�z�!��rrr{�F)T)r_r�r�r�rxr�r�rgrr�r�r�r�rr�r�r-r��staticmethodr�r�r�r�r�r�rprrrnr�r�)s���������
�
�
�
�������"/�c�/�D��I�/�/�/�/�B�B�B�B�����
P�
P�
P�
P�
�S�
�S��X�
�
�
�
����
�3�
��c��
�t�
�
�
�
��*�*��\�*�_�_�_�_�:	 �	 �	 ��C��$�s�)������� 
�3�
�3�s�8�
��
�
�
�
�	�C�	�S��X�	�#�	�	�	�	�	�	rrr�c��ttj��sdSt��sT|�t	td������td|gtjdd�ztjdd�z}nCt��s3tt�����rtdgtjz}ndSdt��i}tjd	t!|��t!|����t#j|tjtj|�
��}|���tj|j��dS)a%
    All user-related actions must run inside of cagefs for security reasons.
    If solo just return because cagefs is only for shared and shared pro
    If root executed, we enter into user cagefs if user is pointed
    If not in cagefs and cagefs is enabeled for user enter into cagefs
    Nz�Internal Error: root enters into CageFS without specifying usernamePlease contact support if you have questions: https://cloudlinux.zendesk.comr�r�rA�)r�r�r�)r��stdinr�)r?r>r�r�r5r	rGr��argvr<r=r�rHr@rXrkrgr�rlr�r��communicater�r�)r��cmdr��ps    rn�_run_clwpos_as_user_in_cagefsr��sA��&�g�n�5�5���������<���5������
�%�&@�$�G�#�(�SU�TU�SU�,�V�Y\�Ya�bc�bd�bd�Ye�e���
�[�[��/�Y�[�[�A�A�A�� �"<�=���H���	���)�+�+�
,�C��L�=�s�3�x�x��S���R�R�R����S�Z�s�y�c�J�J�J�A��M�M�O�O�O��H�Q�\�����rrc�Z�eZdZdZdeddfd�Zd
d�Zd
d�Zdefd�Z	e
defd	���ZdS)�RedisConfigurePidFilezm
    Helper class that provides methods to work with
    pid files of php redis configuration processes.
    �
php_prefixr�Nc�X�|�d�|_tt|j��|_dS)Nz-cloudlinux.pid)�_pid_file_namerrIr�)rhrs  rnrxzRedisConfigurePidFile.__init__s+��!+�<�<�<������)<�=�=��	�	�	rrc���|j�d��5}|�tt	j������ddd��dS#1swxYwYdS)Nr[)r�r��writergr��getpid�rhrQs  rn�createzRedisConfigurePidFile.create!s���
�Y�^�^�C�
 �
 �	&�A�
�G�G�C��	���$�$�%�%�%�	&�	&�	&�	&�	&�	&�	&�	&�	&�	&�	&�	&����	&�	&�	&�	&�	&�	&s�4A�A �#A c�n�|j���r|j���dSdSr{)r��is_filerbr�s rnrLzRedisConfigurePidFile.remove%s<���9�����	��I��������	�	rrc�4�|j���Sr{)r�rr�s rnr!zRedisConfigurePidFile.exists)s���y� � �"�"�"rrc�6�|���sdS|j���5}	t|��������cddd��S#t$rYnwxYw	ddd��n#1swxYwYdS)N���)r!r�r��intr.�stripr�rs  rn�pidzRedisConfigurePidFile.pid,s����{�{�}�}�	��2�
�Y�^�^�
�
�	��
��1�6�6�8�8�>�>�+�+�,�,�	�	�	�	�	�	�	�	���
�
�
���
�����		�	�	�	�	�	�	�	�	�	�	����	�	�	�	�
�rs.�B�2A1�1
A>�;B�=A>�>B�B�B�r�N)
r_r�r�r�rgrxr	rLr-r!rrrrprrrnrrs���������
>�3�>�4�>�>�>�>�&�&�&�&�����#��#�#�#�#���S�����X���rrrrc#�K�t|��}	|���dV�|���dS#|���wxYw)za
    Context manager for creating pid file of current process.
    Removes pid file on exit.
    N)rr	rL)r�pid_files  rn�create_pid_filer8s_����%�Z�0�0�H��������
����������������������s�A�Ac��t|��}tj��dkr|���S	t	j|j��}d|���vS#ttj	f$rYdSwxYw)z�
    Find out if PHP redis configuration process is running.
    Based on looking for presence of pid files.
    For root also checks process existence.
    r�enable_redisF)
rr�r�r!�psutil�Processrr�r��
NoSuchProcess)rr�processs   rn�"is_php_redis_configuration_runningrFs���%�Z�0�0�H�	�z�|�|�q������ � � ���.���.�.��������/�/����,�-�����u�u����s�.A+�+B�Bc�*�tt��S)zE
    Find out if alt-PHP redis configuration process is running.
    )rrJrprrrn�&is_alt_php_redis_configuration_runningrVs��.�n�=�=�=rrc�*�tt��S�zD
    Find out if ea-PHP redis configuration process is running.
    )rrKrprrrn�%is_ea_php_redis_configuration_runningr!]s��.�m�<�<�<rrc�*�tt��Sr )rrLrprrrn�(is_plesk_php_redis_configuration_runningr#ds��.�.>�?�?�?rrc�V�t��pt��p
t��S)za
    Find out if redis configuration process
    is running for any PHP (ea-php or alt-php).
    )rr!r#rprrrn�is_redis_configuration_runningr%ks.��	/�0�0�	6�0�2�2�	6�3�5�5�rr�new_user�old_userc����t�j��5}|���}ddd��n#1swxYwY��fd�|D��}td�|���jd��dS)z;
    Replace user's wpos directory path in redis.conf.
    Nc�P��g|]"}|��j�j����#Srp)rar��r:r'r&r's  ��rnrTz%update_redis_conf.<locals>.<listcomp>~s-���e�e�e�D�T�\�\�(�"3�X�5F�G�G�e�e�errr+r,)r�r�r�rr�)r&r'rQ�redis_conf_lines�
updated_liness``   rn�update_redis_confr-ws�����
�h�!�	"�	"�)�a��;�;�=�=��)�)�)�)�)�)�)�)�)�)�)����)�)�)�)�f�e�e�e�e�Td�e�e�e�M��B�G�G�M�2�2�H�4G��O�O�O�O�Os�8�<�<c�2���	tj|��}n9#t$r,}td|�d|��tj���Yd}~dSd}~wwxYw��fd�|D��}t
d�|��tj|��d��dS)z<
    Replace user's redis socket path in wp-config.php.
    z=Error occurred during opening wp-config.php located in path "z": )rNc�f��g|]-}�j|vr |��j�j��n|��.Srp)r�rar*s  ��rnrTz$update_wp_config.<locals>.<listcomp>�sS�������GO�F[�_c�Fc�Fc����X�*�H�,A�B�B�B�im���rrr+r,)	r4r.r�r�r�r�rr�r�)r%r&r'r1rkr,s ``   rn�update_wp_configr0�s������#�.��5�5��������
�a�k�a�a�^_�a�a�hk�hr�s�s�s�s��������������������#����M��B�G�G�M�2�2�I�N�;�4O�4O�QV�W�W�W�W�Ws��
A�!A
�
Ac�t�tjtj����}|���S)z!
    Get parent process PID.
    )rrr�r�ppid)r�s rn�get_parent_pidr3�s'���>�"�)�+�+�&�&�D��9�9�;�;�rrc�D�tgd�d���\}}}|dkrdSdS)z`
    Detect CL WPOS daemon presence in system
    :return: True - daemon works / False - No
    )�
/sbin/service�clwpos_monitoring�statusT��return_full_outputrF�r2)r�rSs  rn�_is_monitoring_daemon_existsr;�s8��#�#S�#S�#S�hl�m�m�m��J��1��Q����u��4rrc��tjd|��tgd�d���\}}}|stddgd���\}}}|||fS)z5
    Update systemd unit file and reload systemd
    �//usr/share/cloudlinux/clwpos_monitoring.service)�/usr/bin/systemctl�enable�--no-reload�clwpos_monitoring.serviceTr8r>�
daemon-reload)rg�copyr2)�systemd_unit_file�retcoder�r�s    rn�$_update_clwpos_daemon_config_systemdrF�sw���K�A�CT�U�U�U�)�T�T�T�im�����G�V�V��p�"-�/C�_�.U�jn�"o�"o�"o������F�F�"�"rrc���tj��}t|d��5�t�fd�d��D]}|�|���	ddd��n#1swxYwY|���S)N�rbc�.����d��S)Ni)r.)rQs�rnr�z_sha256_file.<locals>.<lambda>�s���!�&�&��"5�"5�rrrr)�hashlib�sha256r��iterr��	hexdigest)r��h�chunkrQs   @rn�_sha256_filerP�s��������A�	
�d�D�	�	��Q��5�5�5�5�s�;�;�	�	�E�
�H�H�U�O�O�O�O�	�������������������
�;�;�=�=�s�,A�A"�%A"�path_a�path_bc���	tj|��}tj|��}|j|jkrdSt|��t|��kS#t$rYdSwxYw)z�
    Best-effort content comparison. We use it to decide whether shipped systemd unit
    needs to be copied into /etc (and thus requires daemon-reload).
    F)r�rd�st_sizerPr�)rQrR�st_a�st_bs    rn�_files_identicalrW�sr��
��w�v�����w�v�����<�4�<�'�'��5��F�#�#�|�F�';�';�;�;�������u�u����s�8A�A�
A*�)A*�service_namec�>�tdd|gd���\}}}|dkS)Nr>z
is-enabledTr8rr:)rXrErSs   rn�_systemd_service_is_enabledrZ�s-���!5�|�\� R�gk�l�l�l�M�G�Q���a�<�rr�	unit_namec�$�tdddd|gd���\}}}|dkrdS|���}|d	krdSd
|vr-|�d
d��\}}|���}|���}|d	krdS|dkS)
z�
    Returns:
    - True/False if systemd reports it explicitly
    - None if the property is unsupported/unknown (depends on systemd version)
    r>�showz-p�NeedDaemonReloadTr8rNr+r�rA�yes)r2rr��lower)r[rEr�rSr|�rhss      rn�!_systemd_unit_needs_daemon_reloadrb�s���%�	�v�t�-?��K������G�V�Q��!�|�|��t�
�L�L�N�N�E���{�{��t�
�e�|�|����S�!�$�$���3��	�	�����K�K�M�M�E���{�{��t��E�>�rrrD�is_module_allowed_on_serverc���d\}}}dtj��vrtgd�d���\}}}n|rt|��\}}}|stgd�d���\}}}|||fS)z4
    Install WPOS daemon to system and start it
    )rNN�el6)z/sbin/chkconfigz--addr6Tr8)r5r6rn)�platform�releaser2rF)rDrcrEr�r�s     rn�_install_daemon_internalrh�s���,��G�V�V��� �"�"�"�"�"-�=�=�=�RV�#
�#
�#
������'�	^�'K�K\�&]�&]�#�G�V�V��x�"-�.]�.]�.]�rv�"w�"w�"w������F�F�"�"rrc��tptjt��}d}d}tj�|���rd}t||��}t|��}t|��}|p|du}|duo|}	t��}
|�d|
||||	��d\}}}
|r)|�d��t|��\}}}
ny|r/|�d	|��td
dd|gd�
��\}}}
|s/|r-|�d��td
dgd�
��\}}}
n|	r|�d��|r|||
fSt��rdSt||��S)a�
    Install WPOS daemon to server if need:
        - if daemon already present - do nothing;
        - on CL Shared Pro install daemon if module allowed
    On solo and if /etc/systemd/system/clwpos_monitoring.service present it will be updated always
    We do not need restart installed daemon here, it's done in rpm_posttrans.sh
    :param is_module_allowed_on_server: True/False
    z-/etc/systemd/system/clwpos_monitoring.servicer=rATNz�install_monitoring_daemon: unit_exists=1 daemon_exists=%s needs_unit_update=%s needs_enable=%s need_daemon_reload=%s need_daemon_reload_unknown=%s)rr+r+zPinstall_monitoring_daemon: updating unit file from shipped and reloading systemdz2install_monitoring_daemon: enabling %s (no-reload)r>r?r@r8zQinstall_monitoring_daemon: running systemctl daemon-reload (NeedDaemonReload=yes)rBzXinstall_monitoring_daemon: skipping daemon-reload (NeedDaemonReload unsupported/unknown))r^rXrYr_r�r�r!rWrZrbr;rkrFr2rh)rcrlrD�shipped_unit_filer[�needs_unit_update�needs_enable�need_reload_flag�needs_daemon_reload�needs_daemon_reload_unknown�
daemon_existsrEr�r�s              rn�install_monitoring_daemonrqs���0�W�&�x�0�0�D�G��I��
�w�~�~�'�(�(�)+�/�	� 0�1B�DU� V� V�V��6�y�A�A�A��<�Y�G�G��/�M�4D��4L��&6�$�&>�&X�GX�CX�#�4�6�6�
��	�	�
R�����'�	
�	
�	
�#,������	v��I�I�h�i�i�i�&J�K\�&]�&]�#�G�V�V�V��
��	�	�N�PY�Z�Z�Z�*5�)�8�]�I�N�'+�+�+�+�'�����
v�2�
v��	�	�m�n�n�n�*5�)�?�;�'+�+�+�+�'�����-�
v��	�	�t�u�u�u��	+��F�F�*�*�#�%�%���y�#�$5�7R�S�S�Srrc��dd|�d�i}	t|��}n#t$rYdSt$rYdSwxYw|�d��S)a\
    Ask the AccelerateWP daemon whether the given service is running.
    Returns None when the daemon itself is unreachable, so that callers
    can distinguish "service is down" from "daemon is down" (CLPRO-3351).
    An application-level error from an alive daemon (e.g. "user has no
    redis yet") still means the service is not running.
    rzget-z-statusNFr7)rr6r5r�)�service�command_get_service_status_dict�
daemon_results   rn�get_status_from_daemonrv?s��(1�2I��2I�2I�2I�&J�#��*�+J�K�K�
�
��%�����t�t������u�u��������X�&�&�&s��
4�	4�4c� �td��Sr��rvrprrrn�redis_is_runningryQ���!�'�*�*�*rrc� �td��S)N�	litespeedrxrprrrn�litespeed_is_runningr}Vs��!�+�.�.�.rrc� �td��S)N�nginxrxrprrrn�nginx_is_runningr�[rzrrc� �td��S)N�apache2nginxrxrprrrn�apache2nginx_is_runningr�`s��!�.�1�1�1rr�	attributec���ddlm}|t����}tj�|d��}	t
|��5}tj|��|cddd��S#1swxYwYdS#ttj
f$r6}tjd|��ttd�����d}~wt$rttd�����wxYw)z7
    Return attribute's value from info.json file.
    r)�get_admin_config_directoryz	info.jsonNz,Error during reading of "info.json" file: %sz�Failed to retrieve data about php version which is currently used. Daemon is not available and cache data is malformed, please try again and contact your administrator if the issue persists.z�Failed to retrieve data about php version which is currently used. Daemon is not available and cache data is not available. Contact your administrator if the issue persists.)r�r�r�r�r�r�r�r��loadr�rOrXrer5rSr�)r�r��admin_config_dir�	info_jsonrQrks      rn�_get_data_from_info_jsonr�es]��A�@�@�@�@�@�1�1�(�*�*�=�=������-�{�;�;�I�
�
�)�_�_�	+���9�Q�<�<�	�*�	+�	+�	+�	+�	+�	+�	+�	+�	+�	+�	+�	+����	+�	+�	+�	+�	+�	+���d�*�+�
�
�
���J�A�N�N�N��
�E�
�
�
�
�	
������
�
�
��
�D�
�
�
�
�	
�
���s;�B�A5�(B�5A9�9B�<A9�=B�C3�1C	�	*C3c�d�t}tj��rtj}||��Sr{)rr�r�r5r6)r�rYs  rn�drop_permissions_if_neededr��s/���G�	�z�|�|�)��(���7�8���rrrr�r�c��ddlm}||�|��vrdnd}	t|j|d���d}n#t
$rd}YnwxYw|rd}|S)Nrr�active�no)rr��pendingF)rrr�r�!DAEMON_GET_UPGRADE_ATTEMPT_STATUSr5)rr�r�r�subscription_status�
is_pendings      rn�get_subscription_statusr��s���(�(�(�(�(�(�&-�1A�1E�1E�e�1L�1L�&L�&L�(�(�RV���'�J�4`�mt�(u�(u�v�v��
�
�
�������
�
�
������(�'���s�A�A�Ac�\�d\}}}	tt��}n4#ttf$r dd�t��dfcYSwxYw	t|��nG#tj$r	d\}}}Yn0tj$r	d\}}}Yntj	$r	d\}}}YnwxYw|||fS)z�
    JWT token check. Mostly copied from cllib, but with some accelerate-wp tunes, including:
    - clsolo, cladmin tokens are now valid
    - no need to check for shared, because our tools just don't work on shared
    )T�OKNFzJWT file {} read errorN)FzJWT token issuer is invalidN)FzJWT token expiredN)FzJWT token format errorN)
r$r'r�r��formatr%r)�InvalidIssuerError�ExpiredSignatureErrorr()�success_flag�
error_message�token_strings   rn�jwt_token_checkr��s��1A�-�L�-��W�� 9�:�:�����W��W�W�W��.�5�5�6O�P�P�RV�V�V�V�V�W����Z��<� � � � ���(�_�_�_�4^�1��m�\�\�\��+�U�U�U�4T�1��m�\�\�\�� �Z�Z�Z�4Y�1��m�\�\�\�Z�������4�4s-��.A�
A�A"�"B&�9B&�B&�%B&c���d}tj�|��sdSt|��5}|������cddd��S#1swxYwYdS)Nz+/usr/share/cloudlinux/accelerate-wp.version)r�r�r!r�r.r)�version_filerQs  rn�get_accelerate_wp_versionr��s���@�L�
�7�>�>�,�'�'���t�	
�l�	�	� �q��v�v�x�x�~�~��� � � � � � � � � � � � ���� � � � � � s�&A&�&A*�-A*c��|stj��dkr-t|d���dd}t|��St	|��S)zE
    Returns True/False whether target user is owned by reseller
    r)�reseller)�cpuser�keyls)r�r�r.r/�)is_user_owned_by_reseller_daemon_fallback)r��
force_as_root�
user_owners   rn�is_user_owned_by_resellerr��sX��
�C��
����)�)��8�=�A�A�A�!�D�Q�G�
��J�'�'�'�'�8��B�B�Brrc�@�ddlm}|�ftj��}	t	j|��j}n#t$rd}YnwxYw||kr ttd��d|i����	td|ji��d}n#t$rYdSwxYw|S)	NrrzpRefusing to resolve reseller ownership for user %(username)s: a non-root caller may only query its own identity.r�rr�is_owned_by_resellerT)rrr�r�r�r�r�r�r5r	r� DAEMON_IS_USER_OWNED_BY_RESELLER)r�r�
caller_uid�
requested_uidr�s     rnr�r��s���(�(�(�(�(�(����Z�\�\�
�	!��L��2�2�9�M�M���	!�	!�	!� �M�M�M�	!�����J�&�&���W���$�X�.����
��1�9�j�>i�2j�k�k�"� 
���������t�t������s!�7�A�A�0B
�
B�Bc���tj��dkrt|��St���|��}i}|D]}|j||j<�t|���d����}|D]'�t�fd�|D����r
|�cS�(tj|��jS)Nrc�,�|�d��S)Nr�)�count)�ds rnr�z%get_username_by_uid.<locals>.<lambda>�s��Q�W�W�S�\�\�rrr�c3�T�K�|]"}|�k�|��dz��V��#dS)r�N)r�)r:rt�	main_homes  �rnr?z&get_username_by_uid.<locals>.<genexpr>�s>�����a�a��t�W`�O`�O`�4�?�?�9�s�?�3�3�O`�O`�O`�O`�a�arr)r�r�r0r�
get_pw_by_uidr�r�rHr�rFr��getpwuid)r��pwd_list�	home_dirs�user_pwd�sorted_dirsr�s     @rnr�r��s����	�z�|�|�q���'��,�,�,��7�7�(�(��-�-���	� �	:�	:�H�)1�)9�I�h�o�&�&��Y�^�^�-�-�3I�3I�J�J�J��$�	,�	,�I��a�a�a�a��a�a�a�a�a�
,� ��+�+�+�+�
,��|�C� � �(�(rrc��ddlm}tj��dkr
|��Sddlm}	t
d|ji��dS#t$rYdSwxYw)Nr)�is_wp2_environmentrr�is_wp2F)	rZr�r�r�rrr�DAEMON_IS_WP2_ENVIRONMENTr5)r�rs  rn�is_wp2_environment_safer�s���1�1�1�1�1�1�	�z�|�|�q���!�!�#�#�#�,�,�,�,�,�,�	�%�y�*�2V�&W�X�X�Ya�b�b���	�	�	��5�5�	���s�A�
A�Ar)rA)r�r{r)r�)r�rV)r�)NNr�)��
__future__rr5r�r}rir2rJrXr�r�rgrdr�r�rjr�r�r��typingr�rr�rrr�globr�enumrr	rS�urllib.parser
rrr
�packaging.versionrrr�	functoolsrr�pathlibrrrrrrrrrrf�secureiorr�clcommon.cpapi.cpapiexceptionsr�clcommon.clpwdrr�clcommon.clcaptainr �clcommon.lib.cleditionr"r#�clcommon.lib.jwt_tokenr$r%�clcommon.lib.constsr&r'r:r(r)�cllicenselibr*rZr+r,r-r.r/r0�clcommon.utilsr1r2r3�clwposr4�clwpos.cl_wpos_exceptionsr5r6r7r8r9r:�clcommon.ui_configr;�clcommon.clcagefsr<r=�clcommon.constr>r?r[r@�logsetuprB�clwpos.constantsrCrDrErFrGrHrIrJrKrLrMrNrOrPrQ�socket_utilsrRrS�user.website_check.errorsrT�clwpos.scoped_cacherU�
TYPE_CHECKING�clwpos.php.baserVr^rqr�rtr-r�r�rr`r�r�r�r��
struct_passwdr�r�rgr�r�r�r�r�rrr$r3r7rMrwr{rr�r�rfr�r�r�r�r�r�r�r�r�r�rr"r*r/r7r!r9r@rr^r}r�r�r�r�r�r�r�rrrrr!r#r%r-r0r3r;rFrPrWrZrbrhrqrvryr}r�r�r�r�r�r�r�r�r�r�r�rprrrn�<module>r�s]��'�&�&�&�&�&�����������������������������	�	�	�	�	�	�	�	�
�
�
�
�����
�
�
�
�
�
�
�
�������������
�
�
�
�
�
�
�
�������������0�0�0�0�0�0�0�0�0�0������������� � � � � � �C�C�C�C�C�C�C�C�C�C�C�C�%�%�%�%�%�%�
�
�
�
�%�%�%�%�%�%�&�&�&�&�&�&�&�&�������/�/�/�/�/�/�/�/�/�/�=�=�=�=�=�=�=�=�=�=�=�=�=�=�����;�;�;�;�;�;�;�;�3�3�3�3�3�3�1�1�1�1�1�1�1�1�$�$�$�$�$�$�T�T�T�T�T�T�T�T�7�7�7�7�7�7�7�7�M�M�M�M�M�M�M�M�&�&�&�&�&�&�&�&�&�&�&�&�&�&�����������������=�<�<�<�<�<�<�<�<�<�%�%�%�%�%�%�%�%�����������������(�'�'�'�'�'�;�;�;�;�;�;�;�;�"�"�"�"�"�"�5�5�5�5�5�5�6�6�6�6�6�6�#�#�#�#�#�#�����������������������������������$X�W�W�W�W�W�W�W�8�8�8�8�8�8�/�/�/�/�/�/�	��$�#�#�#�#�#�#�	
�����:3�3�3�3�3�$�*�3�3�3�,��$��4�����&��D��4��S�����*	5�	5��	5�D�	5�	5�	5�	5��4�����������4�4�4�4�4�4�&�S�->�&�&�&�&�*�*�*�&��s��c�����
�3�����!%�,�,�,�#�,��,�,�,�,�#�&�&�&��&�&�&�&���������*'_��'_�(�4�.�'_�'_�'_�'_�T �D��I� � � � �Fx�c�x�d�x�x�x�x�5�#�5�$�5�5�5�5�0^�C�^�^�^�^�#5�3�#5�4�#5�#5�#5�#5�L\_�\_�\_�\_�~
�
�
�d��d��s�C�x��d�d�d�d�/�C�/�8�C�=�/�/�/�/�d%
�t�%
�%
�%
�%
�P	�	�	�	�	�)�	�	�	������x��}�������
�
�
��
�
�
���
�"�4��E�#�s�(�O�����9�9�$�9�9�9�9�H�H�H�����A�A�A�A�A�A�A���A�
�g!�g!�g!�g!�g!�g!�g!���g!�T��4����.�.���.�&�������):�����@,�,�,�6�6�6����"G�:P�G�G�G�G�7�!2�7�7�7�7�t�
1�n�->�?�
1�
1�
1���
1� @i�@i�@i�@i�JU��%#�%#�%#�PP
�P
�P
�P
�f������K�K�K�K�K�K�K�K�\����@��������B�
��
�
�
���
�
�3�
�4�
�
�
�
� >��>�>�>�>�=�t�=�=�=�=�@�$�@�@�@�@�	��	�	�	�	�P��P�H�P��P�P�P�P�X�#�X��X�X�X�RV�X�X�X�X�"�������d�����#�u�S�#�s�]�?S�#�#�#�#��s��s������S��#��$������c��d�����
����$������6#��#�RV�#�[`�ad�fi�kn�an�[o�#�#�#�#�&;T�4�;T�E�#�s�TW�-�DX�;T�;T�;T�;T�|'�x��~�'�'�'�'�$�+�(�4�.�+�+�+���+��/�h�t�n�/�/�/���/��+�(�4�.�+�+�+���+��2��$��2�2�2���2�
��
��
�
�
�
�>����d��3�������*5�5�5�. � � ���4����C�C��C�C�C���C���4���� �� � � ��� �>)�)�)�"��4����������rr