Fixed issues in rspamd script

This commit is contained in:
Antoine Nguyen
2023-09-14 10:10:07 +02:00
parent 0b85e2c7ef
commit 92864aa288
3 changed files with 34 additions and 18 deletions

View File

@@ -231,6 +231,10 @@ ConfigDictTemplate = [
"option": "enabled", "option": "enabled",
"default": "true", "default": "true",
}, },
{
"option": "user",
"default": "_rspamd",
},
{ {
"option": "password", "option": "password",
"default": make_password, "default": make_password,

View File

@@ -1,17 +1,18 @@
"""Amavis related functions.""" """Amavis related functions."""
import os import os
import pwd
import stat
from .. import package from .. import package
from .. import utils from .. import utils
from .. import system from .. import system
from . import base from . import base
from . import backup, install from . import install
class Rspamd(base.Installer): class Rspamd(base.Installer):
"""Rspamd installer.""" """Rspamd installer."""
appname = "rspamd" appname = "rspamd"
@@ -36,11 +37,8 @@ class Rspamd(base.Installer):
return "/etc/rspamd" return "/etc/rspamd"
def install_packages(self): def install_packages(self):
status, codename = utils.exec_cmd("lsb_release -c -s") debian_based_dist, codename = utils.is_dist_debian_based()
if debian_based_dist:
if codename.lower() in ["bionic", "bookworm", "bullseye", "buster",
"focal", "jammy", "jessie", "sid", "stretch",
"trusty", "wheezy", "xenial"]:
utils.mkdir_safe("/etc/apt/keyrings") utils.mkdir_safe("/etc/apt/keyrings")
if codename.lower() == "bionic": if codename.lower() == "bionic":
@@ -60,7 +58,8 @@ class Rspamd(base.Installer):
def install_config_files(self): def install_config_files(self):
"""Make sure config directory exists.""" """Make sure config directory exists."""
pw = pwd.getpwnam("_rspamd") user = self.config.get(self.appname, "user")
pw = pwd.getpwnam(user)
targets = [ targets = [
[self.app_config["dkim_keys_storage_dir"], pw[2], pw[3]] [self.app_config["dkim_keys_storage_dir"], pw[2], pw[3]]
] ]
@@ -94,7 +93,7 @@ class Rspamd(base.Installer):
"Please make sure it is not 'q1' or 'q2'." "Please make sure it is not 'q1' or 'q2'."
"Storing the password in plain. See" "Storing the password in plain. See"
"https://rspamd.com/doc/quickstart.html#setting-the-controller-password") "https://rspamd.com/doc/quickstart.html#setting-the-controller-password")
_context["controller_password"] = password _context["controller_password"] = self.app_config["password"]
else: else:
_context["controller_password"] = controller_password _context["controller_password"] = controller_password
_context["greylisting_disabled"] = "" if not self.app_config["greylisting"].lower() == "true" else "#" _context["greylisting_disabled"] = "" if not self.app_config["greylisting"].lower() == "true" else "#"
@@ -103,9 +102,10 @@ class Rspamd(base.Installer):
def post_run(self): def post_run(self):
"""Additional tasks.""" """Additional tasks."""
user = self.config.get(self.appname, "user")
system.add_user_to_group( system.add_user_to_group(
self.config.get("modoboa", "user"), self.config.get("modoboa", "user"),
"_rspamd" user
) )
if self.config("clamav", "enabled"): if self.config("clamav", "enabled"):
install("clamav", self.config, self.upgrade, self.archive_path) install("clamav", self.config, self.upgrade, self.archive_path)
@@ -127,10 +127,11 @@ class Rspamd(base.Installer):
"""Restore custom config files.""" """Restore custom config files."""
custom_config_dir = os.path.join(self.config_dir, custom_config_dir = os.path.join(self.config_dir,
"/local.d/") "/local.d/")
custom_backup_dir = os.path.join(path, "/rspamd/") custom_backup_dir = os.path.join(self.archive_path, "/rspamd/")
backed_up_files = [f for f in os.listdir(custom_backup_dir) backed_up_files = [
f for f in os.listdir(custom_backup_dir)
if os.path.isfile(custom_backup_dir, f) if os.path.isfile(custom_backup_dir, f)
] ]
for file in backed_up_files: for f in backed_up_files:
utils.copy_file(file, custom_config_dir) utils.copy_file(f, custom_config_dir)
utils.success("Custom Rspamd configuration restored.") utils.success("Custom Rspamd configuration restored.")

View File

@@ -103,6 +103,17 @@ def dist_name():
return dist_info()[0].lower() return dist_info()[0].lower()
def is_dist_debian_based() -> (bool, str):
"""Check if current OS is Debian based or not."""
status, codename = exec_cmd("lsb_release -c -s")
codename = codename.lower()
return codename in [
"bionic", "bookworm", "bullseye", "buster",
"focal", "jammy", "jessie", "sid", "stretch",
"trusty", "wheezy", "xenial"
], codename
def mkdir(path, mode, uid, gid): def mkdir(path, mode, uid, gid):
"""Create a directory.""" """Create a directory."""
if not os.path.exists(path): if not os.path.exists(path):