Edit File: smtp_blocking.cpython-311.pyc
� ������\h@B��������������� ��������&����d�Z�ddlZddlZddlZddlZddlmZ�ddlmZm Z m Z mZ�ddlm c�mZ�ddlmZmZ�ddlmZ�ddlmZ�ddlmZ�dd lmZ�dd lmZmZmZmZ�ddlmZm Z �ddl!m"Z"�g�d �Z#ddl$m%Z%��edde&fde&fde&fg������������Z'�edde&fde(fde)fde)fde&fde&fg������������Z*dZ+dZ,�ej-��������e.������������Z/dZ0dZ1de e2���������fd�Z3d ��Z4d!��Z5�G�d"��d#e�$������������Zd%e fd&�Z6d'��Z7d(��Z8�ed)�*������������d+����������������Z9d,e*fd-�Z:d,e(fd.�Z;d,e&fd/�Z<dS�)0zs This module contains utilities to work with iptables to block SMTP traffic on the server akin to how CSF does it. �����N)�suppress)� Generator�List�Tuple� NamedTuple)� Singleton�async_lru_cache)� Protector)�SMTPBlocking)�UnifiedAccessLogger)�ip_versions)� FirewallRules�firewall_logging_enabled�get_firewall�is_nat_available)� FirewallError�FirewallBatchCommandError)� hosting_panel)�sync_rules_for_all_versions�reset_rules_for_all_versions�is_SMTP_blocking_supported�read_SMTP_settings�get_active_settings_list�conflicts_exist)� IPVersion� TableState�chain_exists�chain_referenced�rules_ok�SMTPSettings�enabled�ports�allow_users�allow_groups�allow_local�redirectTi,���filter�nat�commandsc�������������������l���K����|sdS� �|�����������������������|��������������d{V����dS�#�t����������$�r�Y�dS�w�xY�w)z� Use a non-zero return code of iptables as an indication of a failed check. An empty check command list is treated as a failure. Note: Should be called as close to the public functions as possible, since it has side effects. FNT)�commitr���)�firewallr)���s��� �O/opt/imunify360/venv/lib/python3.11/site-packages/im360/subsys/smtp_blocking.py�_true_on_successr.���F���si���������������u���o�o�h�'�'�'�'�'�'�'�'�'���t�����������u�u����s����%�� 3�3c��������������#�������K����|�D�]I} �t����������j��������|������������j��������V����#�t����������$�r�t�������������������������������d|�������������Y��Fw�xY�wdS�)z:Obtain UIDs of specified users skipping non-existing ones.zUNIX user %s does not existN)�pwd�getpwnam�pw_uid�KeyError�logger�warning)� usernames�users��� r-���� _get_uidsr8���Z���su���������������� ��,�t�$�$�+�+�+�+�+���� �� �� ��N�N�8�$�?�?�?��H� �����������$�%A�Ac��������������#�������K����|�D�]I} �t����������j��������|������������j��������V����#�t����������$�r�t�������������������������������d|�������������Y��Fw�xY�wdS�)z;Obtain GIDs of specified groups skipping non-existing ones.zUNIX group %s does not existN)�grp�getgrnam�gr_gidr3���r4���r5���)�groups�groups��� r-���� _get_gidsr@���d���su���������������� ��,�u�%�%�,�,�,�,�,���� �� �� ��N�N�9�5�A�A�A��H� ������r9���c��������������������"����e�Zd�ZdZdZdefZdefd�Zdee e df������������������fd�Zdee e df������������������fd �Zd e de fd�Zd��Zd ��Zdee���������fd�Zdeee���������ddf���������fd�Zdedefd�Zd e defd�Zd e fd�Zd e fd�Zddd�Zdd�ZdS�)r���z� This class is used to synchronise iptables rules related to outgoing SMTP traffic blocking with SMTP_BLOCKING section of imunify config. �OUTPUT_imunify360_SMTP�-j� ip_versionc������������������n�����||�_���������d�|�_��������d�|�_��������d|�_��������t ����������j����������������������|�_��������d�S�)NF)rD����_candidate_settings�active_settings�rules_were_resetr����HostingPanel�_hosting_panel)�selfrD���s��� r-����__init__zSMTPBlocking.__init__w���s8������$���#'�� �#��� %���+�8�:�:����������return.c����������� �������R�����|�j���������j��������sg�S�g�}dddddd���������������������d��|�j���������j��������D���������������������������f�|�j���������j��������r*|���������������������g���d�d �d �t ����������j���������R��������������|���������������������g���d�d�d�d �d �t ����������j���������R��������������|����������������������fd�t����������t����������j ��������|�j���������j��������|�j��������j ��������������������������������D����������������������������|����������������������fd�t����������|�j���������j��������������������D����������������������������t!������������������������rb|���������������������g���t����������j��������t����������j��������t����������j��������|�j��������������������t*����������j�����������������������������������R��������������|�j���������j��������r |�j���������j��������rt1����������|�j��������������������sY|���������������������g���d �t ����������j���������d�d���������������������|�j��������t6����������j��������j��������k����rdnd�������������R��������������|S�)z� Return a list of rules that should be used in OUTPUT_imunify360_SMTP chain. These can either be installed using append_rule / insert_rule or checked using has_rule methods of the firewall interface. �-p�tcp�-m� multiport�--dports�,c��������������3����4���K����|�]}t����������|������������V����d�S��N��str��.0�ps��� r-���� <genexpr>z6SMTPBlocking._get_filter_smtp_rules.<locals>.<genexpr>�����(����������D�D��S��V�V�D�D�D�D�D�DrM����-o�lorC����owner�--uid-owner�0c��������������3����h����K����|�],}g���d��d�d�t����������|�������������d�t����������j���������R�V����-dS��rR���ra���rb���rC���N�rY���r����ACCEPT�r[����uid�common_argss��� �r-���r]���z6SMTPBlocking._get_filter_smtp_rules.<locals>.<genexpr>����������������� �� ��� �� �� ��� ��� � ��C��� ��� ���$� �� � �� �� �� �� �� rM���c��������������3����h����K����|�],}g���d��d�d�t����������|�������������d�t����������j���������R�V����-dS��rR���ra���z--gid-ownerrC���Nrf����r[����gidrj���s��� �r-���r]���z6SMTPBlocking._get_filter_smtp_rules.<locals>.<genexpr>����������������� �� ��� �� �� ��� ��� � ��C��� ��� ���$� �� � �� �� �� �� �� rM���)r?����prefix)�actionz --reject-withzicmp{}-port-unreachable�6��)rF���r"����joinr%����appendr���rg����extendr8���� itertools�chainr#���rJ����smtp_allow_usersr@���r$���r����compose_rule�nflog_action�nflog_grouprD���r����SMTPr&���r����REJECT�formatr ����IP�V6�rK����rulesrj���s��� @r-����_get_filter_smtp_rulesz#SMTPBlocking._get_filter_smtp_rules~���s���������'�-�� ��I���������H�H�D�D�T�%=�%C�D�D�D�D�D� ����#�/�� ��L�L�F�+�F�t�F�T�F�4�F��1E�F�F� �� �� � � ��� �� �� ��� ��� � �� ��� ���$� �� � �� �� �� ���� �� �� �� ��!����,�8��'�8������� �� �� �� �� �� �$� ���� �� �� �� ��!��!9�!F�G�G� �� �� �� �� �� ��$�%�%�� ��L�L�� ��"�/�,�9�"/�";�D�O�"L�"L�#6�#;� �� �� ��������� �� �� �� �$�-� ��(�4� ��!���1�1� � � �L�L�� �����"�(���$� � �.�4�4�#��+�.�2C�C�C���������� �� �� ���rM���c����������� �������������|�j���������j��������r|�j���������j��������r|�j���������j��������sg�S�g�}dddddd���������������������d��|�j���������j��������D���������������������������f�|���������������������g���d�d �d �t����������j���������R��������������|���������������������g���d�d�d�d �d �t����������j���������R��������������|����������������������fd�t����������t����������j��������|�j���������j��������|�j ��������j��������������������������������D����������������������������|����������������������fd�t����������|�j���������j��������������������D����������������������������|���������������������g���d �t����������j���������R��������������|S�)z� Return a list of rules that should be used in OUTPUT_imunify360_SMTP chain in nat table. These can either be installed using append_rule / insert_rule or checked using has_rule methods of the firewall interface. rP���rQ���rR���rS���rT���rU���c��������������3����4���K����|�]}t����������|������������V����d�S�rW���rX���rZ���s��� r-���r]���z3SMTPBlocking._get_nat_smtp_rules.<locals>.<genexpr>����r^���rM���r_���r`���rC���ra���rb���rc���c��������������3����h����K����|�],}g���d��d�d�t����������|�������������d�t����������j���������R�V����-dS�re����rY���r����RETURNrh���s��� �r-���r]���z3SMTPBlocking._get_nat_smtp_rules.<locals>.<genexpr>��rk���rM���c��������������3����h����K����|�],}g���d��d�d�t����������|�������������d�t����������j���������R�V����-dS�rm���r����rn���s��� �r-���r]���z3SMTPBlocking._get_nat_smtp_rules.<locals>.<genexpr> ��rp���rM���)rF���r"���r%���r&���ru���rv���r���r����rw���r8���rx���ry���r#���rJ���rz���r@���r$����REDIRECTr����s��� @r-����_get_nat_smtp_rulesz SMTPBlocking._get_nat_smtp_rules����s�������� �$�*� ��(�4� ���(�1� � ��I���������H�H�D�D�T�%=�%C�D�D�D�D�D� ��� ���K�{�K�D�K�$�K��K�m�6J�K�K�L�L�L�� ��� �� �� ��� ��� � �� ��� ���$� �� � �� �� �� ���� �� �� �� ��!����,�8��'�8������� �� �� �� �� �� �$� ���� �� �� �� ��!��!9�!F�G�G� �� �� �� �� �� �� ���A�{�A�D�A�-�*@�A�A�B�B�B��rM����tablec�����������������������|t�����������k����r|������������������������������������S�|t����������k����r|������������������������������������S�g�S�rW���)�FILTERr�����NATr����)rK���r����s��� r-����_get_smtp_rules_forz SMTPBlocking._get_smtp_rules_for1��s=�������F�?�?��.�.�0�0�0��C�<�<��+�+�-�-�-�� rM���c������������������<�����|����������������������||�j���������������������gS�)N�r����ry���)� has_chain�IM360_SMTP_CHAIN�rK���r����r,���s��� r-����_im360_chain_existsz SMTPBlocking._im360_chain_exists8��s!�������"�"��d�6K�"�L�L�M�MrM���c������������������>�����|����������������������|d|�j���������������������gS�)N�OUTPUT�r����ry����rule)�has_rule�IM360_SMTP_TARGET_RULEr����s��� r-����_im360_chain_referencedz$SMTPBlocking._im360_chain_referenced;��s.����������8�$�2M�� �� �� � �� rM���c������������������V���������g�����fd��������������������������������������D����������������}|S�)zW Check if SMTP rules in Imunify chain are in accord with new settings. c��������������3����R����K����|�]!}�������������������������j��������|��������������V����"dS��r����N)r����r�����r[���r����r,���rK���r����s��� ���r-���r]���z/SMTPBlocking._im360_rules_ok.<locals>.<genexpr>G��sX�������������������!�!��t�'<�4��"���������������rM���)r����)rK���r����r,����check_commandss���``` r-����_im360_rules_okzSMTPBlocking._im360_rules_okB��sY��������� �������������!�4�4�U�;�;� ����� ����rM���Nc��������������#�������K����|����������������������|d|�j���������������������gV���|���������������������||�j���������������������|���������������������||�j���������������������gV���dS�)z� Return commands that will ensure no OUTPUT blocking on Imunify part. Since the possible errors need to be suppressed we yield commands in batches, each of which can only contain one error-prone command. r����r����r����N)�delete_ruler�����flush_chainr�����delete_chainr����s��� r-����_reset_commandszSMTPBlocking._reset_commandsP��s������������ � � ��8�$�2M�� !�� �� � �� �� �� �� � � �u�D�4I� �J�J��!�!��T�5J�!�K�K� �� �� �� �� �� rM����table_statec������������������������|�j���������}|�j��������}|�dS�|j���������p^|j���������pV|j��������o|j���������pG|j��������|j��������k����p7t ����������|j��������|j��������z ��������������pt ����������|j��������|j��������z ��������������S�)z)Check whether rules need to be recreated.NT) rG���rF���r���r���r%���r&����boolr#���r$���)rK���r�����active�news��� r-����_should_create_rulesz!SMTPBlocking._should_create_rulesd��s���������%���&���>��4���(�(�� <��'�'� <��"�:�3�?�':� <����3�<�/� <���F�&���8�9�9� <� ��F�'�#�*:�:�;�;� rM���c������������������V��������g�}|j���������s/|���������������������������������������������j�����������������������������������|j��������s0|��������������������������������������������d��j���������������������������������������������������������|������������rf|���������������������������������������������j�����������������������������������|� ������������������������fd���� ���������������������������������D����������������������������n;��j��������j��������s/|���������������������������������������������j�����������������������������������|S�)zf Return commands that will ensure firewall rules are in accord with settings. )r����r����r����c��������������3����R����K����|�]!}�������������������������j��������|��������������V����"dS�r����)�append_ruler����r����s��� ���r-���r]���z.SMTPBlocking._sync_commands.<locals>.<genexpr>���sX�������������������$�$��t�'<�4��%���������������rM���) r���rv����create_chainr����r����insert_ruler����r����r����rw���r����rF���r"���)rK���r����r����r,���r)���s���`` ` r-����_sync_commandszSMTPBlocking._sync_commandsv��sw�������� ����'�� ��O�O��%�%�d�&;�5�%�I�I� �� �� ���+�� ��O�O��$�$��"��4��%����� �� �� ���$�$�[�1�1�� ��O�O��$�$�T�%:�%�$�H�H� �� �� �� �O�O��������������!�4�4�U�;�;� ������ �� �� �� ���+�1�� �����(�(��)>�e�(�L�L��������rM���c����������������������K����|�����������������������||������������D�]I}t����������t����������������������5��|���������������������|��������������d�{V����d�d�d��������������n#�1�swxY�w�Y����Jt�������������������������������d|�������������d�S�)Nz(SMTP Rules in table '%s' have been reset)r����r���r���r+���r4����info)rK���r����r,����batchs��� r-����_reset_rules_in_tablez"SMTPBlocking._reset_rules_in_table���s������������)�)�%��:�:�� -�� -�E��3�4�4�� -�� -��o�o�e�,�,�,�,�,�,�,�,�,� -�� -�� -�� -�� -�� -�� -�� -�� -�� -�� -����� -�� -�� -�� -�����>��F�F�F�F�Fs����A�A �A c���������������������K����t����������||����������������������||��������������������������d�{V���}|�j��������j��������s |r|����������������������||��������������d�{V����d�S�t����������|t����������||����������������������||��������������������������d�{V���t����������||����������������������||��������������������������d�{V����������������}|����������������������|||������������}|r8|� ��������������������|��������������d�{V����t�������������������������������d|�������������d�S�d�S�)N)r���r���r���z;SMTP settings have been synced with the rules in table '%s')r.���r����rF���r!���r����r���r����r����r����r+���r4���r����)rK���r����r,���r���r����r)���s��� r-����_sync_rules_in_tablez!SMTPBlocking._sync_rules_in_table���s����������-��d�.�.�u�h�?�?� �� �� �� �� �� �� �� ����'�/�� ��� B��0�0���A�A�A�A�A�A�A�A�A��F� �%�#3��$�6�6�u�h�G�G�$��$��������������,��$�.�.�u�h�?�?���������������� �� �� ����&�&�u�k�8�D�D���� ��/�/�(�+�+�+�+�+�+�+�+�+��K�K�M�� �� �� �� �� � �� rM���c����������������������K����|pt����������|�j����������������������d{V���}|����������������������t����������|��������������d{V����t ����������|�j��������������������r!|����������������������t ����������|��������������d{V����d|�_��������dS�)z*Ensure no OUTPUT blocking on Imunify part.NT)r���rD���r����r����r���r����rH���)rK���r,���s��� r-����reset_ruleszSMTPBlocking.reset_rules���s������������B�\�$�/�%B�%B�B�B�B�B�B�B���(�(���:�:�:�:�:�:�:�:�:��D�O�,�,�� <��,�,�S�(�;�;�;�;�;�;�;�;�;� $����rM���c���������������������K����t����������|�j����������������������d{V���}||�_��������|����������������������t����������|��������������d{V����t����������|�j��������������������r!|����������������������t����������|��������������d{V����|�j��������|�_��������d|�_��������dS�)z2Ensure iptables rules are in accord with settings.NF) r���rD���rF���r����r����r���r����rG���rH���)rK����new_settingsr,���s��� r-���� sync_ruleszSMTPBlocking.sync_rules���s�����������%�d�o�6�6�6�6�6�6�6�6��#/�� ��'�'���9�9�9�9�9�9�9�9�9��D�O�,�,�� ;��+�+�C��:�:�:�:�:�:�:�:�:�� $�7��� %����rM���rW���)rN���N)�__name__� __module__�__qualname__�__doc__r����r����r���rL���r���r���rY���r����r�����listr����r����r�����dictr����r���r����r���r����r����r����r����r����r����r������rM���r-���r���r���n���s����������������� �0��"�$4�5��;�9��;��;��;��;�e��U�3��8�_�(=��e��e��e��e�NJ�T�%��S��/�%:��J��J��J��J�X�������������N��N��N� �� �� ��$�t�*��������� � �4��:�t�T�)� *� �� �� �� �( � �� �t�� �� �� �� �$#�C��#�j��#��#��#��#�JG���G��G��G��G�����������6%��%��%��%��%� &�� &�� &�� &�� &�� &rM���r���)� metaclass�ip_versions_to_resetc����������������������K����|�D�]=}t����������|������������j��������s't����������|�������������������������������������������������d�{V�����>d�S�rW���)r���rH���r����)r�����versions��� r-����_reset_rules_for_ip_versionsr�������s_����������'��6��6���G�$�$�5�� 6��w�'�'�3�3�5�5�5�5�5�5�5�5�5��6��6rM���c��������������������v���K����d��t����������j����������������������D���������������}�|�rt����������|���������������d{V����dS�dS�)z8 Mainly used for `SMTPBlocker` plugin shutdown. c������������������:�����g�|�]}t����������|������������j���������|��S�r����)r���rH���)r[���r����s��� r-���� <listcomp>z0reset_rules_for_all_versions.<locals>.<listcomp>���s9��������������G�$�$�5�������rM���N)r ����allr����)r����s��� r-���r���r������sk�������������"��(�(������� ���A�*�+?�@�@�@�@�@�@�@�@�@�@�@�A��ArM���c����������������������K����t�����������j��������4��d{V����t����������j����������������������D�]*}t ����������|���������������������������������|���������������d{V�����+ �ddd�������������d{V����dS�#�1��d{V���swxY�w�Y���dS�)z� Used whenever there is a need to check compatibility between Imunify config and currently used SMTP blocking iptables rules. N)r ����RULE_EDIT_LOCKr ���r!���r���r����)r����r����s��� r-���r���r������sF��������� ��'��A��A��A��A��A��A��A��A�"�*�,�,�� A�� A�G��w�'�'�2�2�<�@�@�@�@�@�@�@�@�@�@� A�A��A��A��A��A��A��A��A��A��A��A��A��A��A��A��A��A��A��A��A��A��A��A��A�����A��A��A��A��A��As����?A(�( A2�5A2����)�maxsizec��������������������v��K����t�����������j��������4��d{V����t����������t����������j��������j����������������������d{V���}� �|����������������������|����������������������dt����������j �����������������������������������g��������������d{V����n"#�t����������$�r�Y�ddd�������������d{V����dS�w�xY�w �|����������������������|����������������������dt����������j �����������������������������������g��������������d{V����n2#�t����������$�r%}t����������� ��������������������d|�������������Y�d}~nd}~ww�xY�wddd�������������d{V����n#�1��d{V���swxY�w�Y���dS�)z&Check if iptables has xt_owner module.Nr����)ry���r����FzASomething went wrong during the removal of the SMTP test rule: %sT)r ���r����r���r ���r�����V4r+���r����r����smtp_test_ruler���r����r4���r5���)r,����errs��� r-���r���r������s|�����������'�����������������%�k�n�&7�8�8�8�8�8�8�8�8�� ��/�/��(�(�&�]�-I�-K�-K��)���������� �� �� �� �� �� �� �� ���)�� �� �� ����������������������������� ���� ��/�/��(�(�&�]�-I�-K�-K��)���������� �� �� �� �� �� �� �� ���)�� �� �� ��N�N�@�� �� �� �� �� �� �� �� ����� ����-��������������������������������������������������������8��4s`����%D(�AA>�=D(�> B�D(�B�D(�!AC$�#D(�$ D�.D� D(�D�D(�( D2�5D2rN���c�������������������������t����������t����������j��������t����������j��������t ����������t����������j��������������������t ����������t����������j��������������������t����������j��������t����������j���������������������S�)z,Return current settings from Imunify config.)r!���r"���r#���r$���r%���r&���) r ���� SMTPConfig�ENABLED�PORTS�set�ALLOW_USERS�ALLOW_GROUPS�ALLOW_LOCALr����r����rM���r-���r���r�����sM��������"���� �.�/�/���0�1�1��*��$� ������rM���c������������������������g�}�t����������j����������������������D�])}|����������������������t����������|������������j����������������������*|�S�)zk Return the latest applied SMTP settings. Used to compare with the settings from config file. )r ���r!���rv���r���rG���)�active_settings_listr����s��� r-���r���r�����sL����������&�(�(��K��K���#�#�L��$9�$9�$I�J�J�J�J��rM���c�����������������������K����t����������j���������������������������������������������������������}�t����������t ����������j������������������������d{V���|�f������������S�)zC Return True if any other SMTP blocking features is active N)r���rI����get_SMTP_conflict_status�any�csf�is_SMTP_block_enabled)�panel_SMTP_conflicts��� r-���r���r���(��sV����������� �"�$�$�=�=�?�?�����c�/�1�1�1�1�1�1�1�1�3F�G�H�H�HrM���)=r����r;���rx����loggingr0���� contextlibr����typingr���r���r���r����im360.subsys.csf�subsysr�����defence360agent.utilsr���r ����im360.contracts.configr ���r���r����r����im360.internals.corer ����im360.internals.core.firewallr���r���r���r����"im360.internals.core.firewall.baser���r����im360.subsys.panelsr����__all__�defence360agent.utils.validater���r����r���r����r����r ����CAPTURE_CSF_LOCK�CSF_LOCK_TIMEOUT� getLoggerr����r4���r����r����r����r.���r8���r@���r����r���r���r���r���r���r���r����rM���r-����<module>r�������s{������� �� � � ��������������� � � � ��������������5��5��5��5��5��5��5��5��5��5��5��5�����������������������������������-��,��,��,��,��,��=��=��=��=��=��=��6��6��6��6��6��6��,��,��,��,��,��,����������������������������������������.��-��-��-��-��-��������5��4��4��4��4��4� �Z���d��0�$�7�*�d�9K�L���� ���z�� �D�� �$�� ��� ��� ��� �T�� � �� ��������� �� �8� $� $�� �����t�D�z���������(����������`&��`&��`&��`&��`&�Y��`&��`&��`&��`&�F6�T��6��6��6��6� A�� A�� A�A��A��A���������������B �L�� �� �� �� � �$�� �� �� �� �I�t��I��I��I��I��I��IrM���