­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ """An RPC interface to SMTP blocking subsystem. It is needed only for manual testing purposes. """ import logging from defence360agent.rpc_tools import lookup, ValidationError from defence360agent.utils import Scope, check_disabled_firewall from im360.contracts.messages import SmtpReset, SmtpSync from im360.internals.core.firewall import is_nftables_backend from im360.simple_rpc.lists import raise_acquired_validation_error from im360.simple_rpc.resident_socket import send_to_socket from im360.subsys.smtp_blocking import ( sync_rules_for_all_versions, reset_rules_for_all_versions, is_SMTP_blocking_supported, read_SMTP_settings, conflicts_exist, ) __all__ = ["SMTPEndpoints"] logger = logging.getLogger(__name__) class SMTPEndpoints(lookup.RootEndpoints): SCOPE = Scope.IM360 @lookup.bind("smtp-blocking", "sync") @check_disabled_firewall async def sync_rules(self): """Create SMTP blocking iptables rules according to settings.""" if await is_SMTP_blocking_supported(): if await conflicts_exist(): raise ValidationError( "SMTP blocking conflicts with third party features" ) if is_nftables_backend(): await self._send_msg_to_socket( {"method": SmtpSync.DEFAULT_METHOD} ) else: smtp_settings = read_SMTP_settings() await sync_rules_for_all_versions(smtp_settings) else: raise ValidationError( "SMTP blocking is not supported by current kernel" ) @lookup.bind("smtp-blocking", "reset") @check_disabled_firewall async def reset_rules(self): """Remove all rules installed by this plugin.""" if await is_SMTP_blocking_supported(): if await conflicts_exist(): raise ValidationError( "SMTP blocking conflicts with third party features" ) if is_nftables_backend(): await self._send_msg_to_socket( {"method": SmtpReset.DEFAULT_METHOD} ) else: await reset_rules_for_all_versions() else: raise ValidationError( "SMTP blocking is not supported by current kernel" ) RESIDENT_REPLY_TIMEOUT = 60 @raise_acquired_validation_error async def _send_msg_to_socket(self, msg): return await send_to_socket( msg=msg, timeout=self.RESIDENT_REPLY_TIMEOUT )