2022-11-10 17:51:22 +02:00
|
|
|
#!/usr/bin/env python3
|
2018-10-23 10:58:36 +02:00
|
|
|
|
|
|
|
import os
|
2021-12-17 16:54:05 +02:00
|
|
|
import logging
|
2019-01-08 00:38:06 +02:00
|
|
|
import sys
|
2019-11-21 14:46:38 +02:00
|
|
|
import subprocess
|
2022-11-12 12:34:58 +02:00
|
|
|
import shutil
|
2021-12-17 16:54:05 +02:00
|
|
|
import hmac
|
2018-12-07 13:37:40 +02:00
|
|
|
|
2022-11-12 12:34:58 +02:00
|
|
|
from socrate import conf, system
|
2022-11-10 17:51:22 +02:00
|
|
|
|
2021-12-17 16:54:05 +02:00
|
|
|
env = os.environ
|
2019-01-08 00:38:06 +02:00
|
|
|
|
2021-12-17 16:54:05 +02:00
|
|
|
logging.basicConfig(stream=sys.stderr, level=env.get("LOG_LEVEL", "WARNING"))
|
2022-12-08 13:46:31 +02:00
|
|
|
system.set_env(['ROUNDCUBE','SNUFFLEUPAGUS'])
|
2018-12-07 13:37:40 +02:00
|
|
|
|
2021-12-17 16:54:05 +02:00
|
|
|
# jinja context
|
|
|
|
context = {}
|
|
|
|
context.update(env)
|
|
|
|
|
|
|
|
context["MAX_FILESIZE"] = str(int(int(env.get("MESSAGE_SIZE_LIMIT", "50000000")) * 0.66 / 1048576))
|
|
|
|
|
2023-01-28 19:30:33 +02:00
|
|
|
# Get the first DNS server
|
|
|
|
with open("/etc/resolv.conf") as handle:
|
|
|
|
content = handle.read().split()
|
|
|
|
resolver = content[content.index("nameserver") + 1]
|
|
|
|
context["RESOLVER"] = f"[{resolver}]" if ":" in resolver else resolver
|
|
|
|
|
2021-12-17 16:54:05 +02:00
|
|
|
db_flavor = env.get("ROUNDCUBE_DB_FLAVOR", "sqlite")
|
2021-06-18 23:21:24 +02:00
|
|
|
if db_flavor == "sqlite":
|
2021-12-17 16:54:05 +02:00
|
|
|
context["DB_DSNW"] = "sqlite:////data/roundcube.db"
|
2021-06-18 23:21:24 +02:00
|
|
|
elif db_flavor == "mysql":
|
2021-12-17 16:54:05 +02:00
|
|
|
context["DB_DSNW"] = "mysql://%s:%s@%s/%s" % (
|
|
|
|
env.get("ROUNDCUBE_DB_USER", "roundcube"),
|
|
|
|
env.get("ROUNDCUBE_DB_PW"),
|
|
|
|
env.get("ROUNDCUBE_DB_HOST", "database"),
|
|
|
|
env.get("ROUNDCUBE_DB_NAME", "roundcube")
|
2021-06-18 23:21:24 +02:00
|
|
|
)
|
|
|
|
elif db_flavor == "postgresql":
|
2021-12-17 16:54:05 +02:00
|
|
|
context["DB_DSNW"] = "pgsql://%s:%s@%s/%s" % (
|
|
|
|
env.get("ROUNDCUBE_DB_USER", "roundcube"),
|
|
|
|
env.get("ROUNDCUBE_DB_PW"),
|
|
|
|
env.get("ROUNDCUBE_DB_HOST", "database"),
|
|
|
|
env.get("ROUNDCUBE_DB_NAME", "roundcube")
|
2021-06-18 23:21:24 +02:00
|
|
|
)
|
2019-11-19 13:22:09 +02:00
|
|
|
else:
|
2021-12-17 16:54:05 +02:00
|
|
|
print(f"Unknown ROUNDCUBE_DB_FLAVOR: {db_flavor}", file=sys.stderr)
|
2019-11-19 13:22:09 +02:00
|
|
|
exit(1)
|
|
|
|
|
2022-11-18 14:25:02 +02:00
|
|
|
conf.jinja("/etc/snuffleupagus.rules.tpl", context, "/etc/snuffleupagus.rules")
|
2021-12-17 16:54:05 +02:00
|
|
|
|
|
|
|
# roundcube plugins
|
|
|
|
# (using "dict" because it is ordered and "set" is not)
|
2022-11-12 12:34:58 +02:00
|
|
|
plugins = dict((p, None) for p in env.get("ROUNDCUBE_PLUGINS", "").replace(" ", "").split(",") if p and os.path.isdir(os.path.join("/var/www/roundcube/plugins", p)))
|
2021-12-17 16:54:05 +02:00
|
|
|
if plugins:
|
|
|
|
plugins["mailu"] = None
|
|
|
|
else:
|
|
|
|
plugins = dict((k, None) for k in ["archive", "zipdownload", "markasjunk", "managesieve", "enigma", "carddav", "mailu"])
|
|
|
|
|
|
|
|
context["PLUGINS"] = ",".join(f"'{p}'" for p in plugins)
|
|
|
|
|
2021-12-18 18:43:21 +02:00
|
|
|
# add overrides
|
2022-11-10 21:03:26 +02:00
|
|
|
context["INCLUDES"] = sorted(inc for inc in os.listdir("/overrides") if inc.endswith((".inc", ".inc.php"))) if os.path.isdir("/overrides") else []
|
2021-12-18 18:43:21 +02:00
|
|
|
|
2021-12-21 00:13:18 +02:00
|
|
|
# calculate variables for config file
|
2021-12-21 17:06:34 +02:00
|
|
|
context["SESSION_TIMEOUT_MINUTES"] = max(int(env.get("SESSION_TIMEOUT", "3600")) // 60, 1)
|
2021-12-21 00:13:18 +02:00
|
|
|
|
2021-12-17 16:54:05 +02:00
|
|
|
# create config files
|
2022-11-12 12:34:58 +02:00
|
|
|
conf.jinja("/conf/config.inc.php", context, "/var/www/roundcube/config/config.inc.php")
|
2018-10-23 10:58:36 +02:00
|
|
|
|
2021-12-17 16:54:05 +02:00
|
|
|
# create dirs
|
2021-09-24 18:15:00 +02:00
|
|
|
os.system("mkdir -p /data/gpg")
|
2019-11-21 14:46:38 +02:00
|
|
|
|
2022-11-24 20:08:30 +02:00
|
|
|
base = "/data/_data_/_default_/"
|
|
|
|
shutil.rmtree(base + "domains/", ignore_errors=True)
|
|
|
|
os.makedirs(base + "domains", exist_ok=True)
|
|
|
|
os.makedirs(base + "configs", exist_ok=True)
|
|
|
|
|
|
|
|
conf.jinja("/defaults/default.json", context, "/data/_data_/_default_/domains/default.json")
|
|
|
|
conf.jinja("/defaults/application.ini", context, "/data/_data_/_default_/configs/application.ini")
|
|
|
|
conf.jinja("/defaults/php.ini", context, "/etc/php81/php.ini")
|
|
|
|
|
|
|
|
# setup permissions
|
|
|
|
os.system("chown -R mailu:mailu /data")
|
|
|
|
|
2022-12-23 11:58:06 +02:00
|
|
|
def demote(username='mailu'):
|
2022-11-24 19:40:56 +02:00
|
|
|
def result():
|
2022-12-23 11:58:06 +02:00
|
|
|
system.drop_privs_to(username)
|
2022-11-24 19:40:56 +02:00
|
|
|
return result
|
|
|
|
|
2021-12-17 16:54:05 +02:00
|
|
|
print("Initializing database")
|
2019-11-21 22:05:15 +02:00
|
|
|
try:
|
2022-11-12 12:34:58 +02:00
|
|
|
result = subprocess.check_output(["/var/www/roundcube/bin/initdb.sh", "--dir", "/var/www/roundcube/SQL"],
|
2022-12-23 11:58:06 +02:00
|
|
|
stderr=subprocess.STDOUT, preexec_fn=demote())
|
2019-11-21 22:05:15 +02:00
|
|
|
print(result.decode())
|
2021-12-17 16:54:05 +02:00
|
|
|
except subprocess.CalledProcessError as exc:
|
|
|
|
err = exc.stdout.decode()
|
|
|
|
if "already exists" in err:
|
2022-11-10 21:03:26 +02:00
|
|
|
print("Already initialized")
|
2019-11-21 22:05:15 +02:00
|
|
|
else:
|
2021-12-17 16:54:05 +02:00
|
|
|
print(err)
|
|
|
|
exit(3)
|
2019-11-21 22:05:15 +02:00
|
|
|
|
2021-12-17 16:54:05 +02:00
|
|
|
print("Upgrading database")
|
2019-11-21 22:05:15 +02:00
|
|
|
try:
|
2022-12-23 11:58:06 +02:00
|
|
|
subprocess.check_call(["/var/www/roundcube/bin/update.sh", "--version=?", "-y"], stderr=subprocess.STDOUT, preexec_fn=demote())
|
2021-12-17 16:54:05 +02:00
|
|
|
except subprocess.CalledProcessError as exc:
|
|
|
|
exit(4)
|
2021-11-02 13:21:40 +02:00
|
|
|
else:
|
2021-12-17 16:54:05 +02:00
|
|
|
print("Cleaning database")
|
2021-11-02 13:21:40 +02:00
|
|
|
try:
|
2022-12-23 11:58:06 +02:00
|
|
|
subprocess.check_call(["/var/www/roundcube/bin/cleandb.sh"], stderr=subprocess.STDOUT, preexec_fn=demote())
|
2021-12-17 16:54:05 +02:00
|
|
|
except subprocess.CalledProcessError as exc:
|
|
|
|
exit(5)
|
2019-11-21 22:05:15 +02:00
|
|
|
|
2022-11-10 17:51:22 +02:00
|
|
|
# Configure nginx
|
2022-11-12 12:34:58 +02:00
|
|
|
conf.jinja("/conf/nginx-webmail.conf", context, "/etc/nginx/http.d/webmail.conf")
|
2022-11-10 17:51:22 +02:00
|
|
|
if os.path.exists("/var/run/nginx.pid"):
|
|
|
|
os.system("nginx -s reload")
|
2020-03-13 22:17:21 +02:00
|
|
|
|
2022-12-08 13:46:31 +02:00
|
|
|
system.clean_env()
|
2021-12-17 16:54:05 +02:00
|
|
|
|
2022-11-10 17:51:22 +02:00
|
|
|
# run nginx
|
2022-11-11 14:08:27 +02:00
|
|
|
os.system("php-fpm81")
|
2022-11-10 17:51:22 +02:00
|
|
|
os.execv("/usr/sbin/nginx", ["nginx", "-g", "daemon off;"])
|
2021-12-17 16:54:05 +02:00
|
|
|
|