2018-10-23 10:58:36 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import os
|
2021-12-17 16:54:05 +02:00
|
|
|
import logging
|
2019-01-08 00:38:06 +02:00
|
|
|
import sys
|
2019-07-25 10:33:57 +02:00
|
|
|
from socrate import conf
|
2019-11-21 14:46:38 +02:00
|
|
|
import subprocess
|
2021-12-17 16:54:05 +02:00
|
|
|
import hmac
|
2018-12-07 13:37:40 +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"))
|
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))
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2021-12-17 16:54:05 +02:00
|
|
|
# derive roundcube secret key
|
|
|
|
secret_key = env.get("SECRET_KEY")
|
|
|
|
if not secret_key:
|
|
|
|
try:
|
|
|
|
secret_key = open(env.get("SECRET_KEY_FILE"), "r").read().strip()
|
|
|
|
except Exception as exc:
|
|
|
|
print(f"Can't read SECRET_KEY from file: {exc}", file=sys.stderr)
|
|
|
|
exit(2)
|
|
|
|
|
|
|
|
context['SECRET_KEY'] = hmac.new(bytearray(secret_key, 'utf-8'), bytearray('ROUNDCUBE_KEY', 'utf-8'), 'sha256').hexdigest()
|
|
|
|
|
|
|
|
# roundcube plugins
|
|
|
|
# (using "dict" because it is ordered and "set" is not)
|
2021-12-17 16:55:16 +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/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
|
|
|
|
context["INCLUDES"] = sorted(inc for inc in os.listdir("/overrides") if inc.endswith(".inc")) if os.path.isdir("/overrides") else []
|
|
|
|
|
2021-12-17 16:54:05 +02:00
|
|
|
# create config files
|
|
|
|
conf.jinja("/php.ini", context, "/usr/local/etc/php/conf.d/roundcube.ini")
|
|
|
|
conf.jinja("/config.inc.php", context, "/var/www/html/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
|
|
|
|
2021-12-17 16:54:05 +02:00
|
|
|
print("Initializing database")
|
2019-11-21 22:05:15 +02:00
|
|
|
try:
|
2021-06-18 23:21:24 +02:00
|
|
|
result = subprocess.check_output(["/var/www/html/bin/initdb.sh", "--dir", "/var/www/html/SQL"],
|
|
|
|
stderr=subprocess.STDOUT)
|
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:
|
2019-11-21 22:05:15 +02:00
|
|
|
print("Already initialzed")
|
|
|
|
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:
|
2021-06-18 23:21:24 +02:00
|
|
|
subprocess.check_call(["/var/www/html/bin/update.sh", "--version=?", "-y"], stderr=subprocess.STDOUT)
|
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:
|
|
|
|
subprocess.check_call(["/var/www/html/bin/cleandb.sh"], stderr=subprocess.STDOUT)
|
2021-12-17 16:54:05 +02:00
|
|
|
except subprocess.CalledProcessError as exc:
|
|
|
|
exit(5)
|
2019-11-21 22:05:15 +02:00
|
|
|
|
2021-12-17 16:54:05 +02:00
|
|
|
# setup permissions
|
2020-03-13 22:17:21 +02:00
|
|
|
os.system("chown -R www-data:www-data /data")
|
|
|
|
|
2021-12-17 16:54:05 +02:00
|
|
|
# clean env
|
|
|
|
[env.pop(key, None) for key in env.keys() if key == "SECRET_KEY" or key.startswith("ROUNDCUBE_")]
|
|
|
|
|
|
|
|
# run apache
|
|
|
|
os.execve("/usr/local/bin/apache2-foreground", ["apache2-foreground"], env)
|
|
|
|
|