晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。   林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。   见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝)   既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。   南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。 sh-3ll

HOME


sh-3ll 1.0
DIR:/opt/imunify360/venv/share/imunify360/scripts/migrate_csf/
Upload File :
Current File : //opt/imunify360/venv/share/imunify360/scripts/migrate_csf/smtp.py
"""
SMTP blocking migration from CSF to Imunify360.

Mapping:
SMTP_BLOCK = "1" -> SMTP_BLOCKING.enable = true
SMTP_PORTS = "25,465,587" -> SMTP_BLOCKING.ports = "25,587,465"
SMTP_ALLOWUSER = "user1" -> SMTP_BLOCKING.allow_users = ["user1"]
SMTP_ALLOWGROUP = "group1" -> SMTP_BLOCKING.allow_groups = ["group1"]
SMTP_ALLOWLOCAL = "1" -> SMTP_BLOCKING.allow_local = true
"""

import pathlib
import yaml
from csf_conf import get_csf_config_value
from logger_config import get_logger, capture_exception

CONFIG_DIR = pathlib.Path("/etc/sysconfig/imunify360/imunify360.config.d/")


def migrate_smtp() -> None:
    """Migrate SMTP blocking configuration from CSF to Imunify360."""
    logger = get_logger()
    try:
        logger.info("Migrating SMTP blocking configuration...")

        smtp_block = get_csf_config_value("SMTP_BLOCK")
        smtp_ports = get_csf_config_value("SMTP_PORTS")
        smtp_allowuser = get_csf_config_value("SMTP_ALLOWUSER")
        smtp_allowgroup = get_csf_config_value("SMTP_ALLOWGROUP")
        smtp_allowlocal = get_csf_config_value("SMTP_ALLOWLOCAL")

        if smtp_block != "1":
            logger.info(
                "SMTP blocking is disabled in CSF, skipping migration."
            )
            return

        imunify_config = {"SMTP_BLOCKING": {}}

        imunify_config["SMTP_BLOCKING"]["enable"] = smtp_block == "1"

        if smtp_ports:
            imunify_config["SMTP_BLOCKING"]["ports"] = smtp_ports.split(",")

        if smtp_allowuser:
            users = [
                user.strip()
                for user in smtp_allowuser.split(",")
                if user.strip()
            ]
            if users:
                imunify_config["SMTP_BLOCKING"]["allow_users"] = users

        if smtp_allowgroup:
            groups = [
                group.strip()
                for group in smtp_allowgroup.split(",")
                if group.strip()
            ]
            if groups:
                imunify_config["SMTP_BLOCKING"]["allow_groups"] = groups

        imunify_config["SMTP_BLOCKING"]["allow_local"] = smtp_allowlocal == "1"

        CONFIG_DIR.mkdir(parents=True, exist_ok=True)

        config_file = CONFIG_DIR / "70-csf-smtp.conf"
        with open(config_file, "w") as f:
            yaml.dump(imunify_config, f)

        logger.info(f"SMTP blocking configuration written to {config_file}")
        logger.info("SMTP migration completed successfully.")
    except Exception as e:
        logger.error(f"Error during SMTP migration: {e}")
        capture_exception(e, {"migration_type": "smtp"})