From 52cf727a53ff1805da0da9ef86ecc27e20c3d335 Mon Sep 17 00:00:00 2001 From: Symphorien Gibol Date: Sun, 5 Jan 2020 12:00:00 +0000 Subject: [PATCH 1/3] nixos/roundcube: do not write passwords to the store nor run php as root If the database is local, use postgres peer authentication. Otherwise, use a password file. Leave database initialisation to postgresql.ensure*. Leave /var/lib/roundcube creation to systemd. Run php upgrade script as unpriviledged user. --- nixos/modules/services/mail/roundcube.nix | 68 ++++++++++++++++------- 1 file changed, 48 insertions(+), 20 deletions(-) diff --git a/nixos/modules/services/mail/roundcube.nix b/nixos/modules/services/mail/roundcube.nix index 36dda619ad0..b064c717958 100644 --- a/nixos/modules/services/mail/roundcube.nix +++ b/nixos/modules/services/mail/roundcube.nix @@ -5,6 +5,8 @@ with lib; let cfg = config.services.roundcube; fpm = config.services.phpfpm.pools.roundcube; + localDB = cfg.database.host == "localhost"; + user = cfg.database.username; in { options.services.roundcube = { @@ -44,7 +46,10 @@ in username = mkOption { type = types.str; default = "roundcube"; - description = "Username for the postgresql connection"; + description = '' + Username for the postgresql connection. + If database.host is set to localhost, a unix user and group of the same name will be created as well. + ''; }; host = mkOption { type = types.str; @@ -58,7 +63,12 @@ in }; password = mkOption { type = types.str; - description = "Password for the postgresql connection"; + description = "Password for the postgresql connection. Do not use: the password will be stored world readable in the store; use passwordFile instead."; + default = ""; + }; + passwordFile = mkOption { + type = types.str; + description = "Password file for the postgresql connection. Must be readable by user nginx. Ignored if database.host is set to localhost, as peer authentication will be used."; }; dbname = mkOption { type = types.str; @@ -83,11 +93,17 @@ in }; config = mkIf cfg.enable { + # backward compatibility: if password is set but not passwordFile, make one. + services.roundcube.database.passwordFile = mkIf (!localDB && cfg.database.password != "") (mkDefault ("${pkgs.writeText "roundcube-password" cfg.database.password}")); + warnings = lib.optional (!localDB && cfg.database.password != "") "services.roundcube.database.password is deprecated and insecure; use services.roundcube.database.passwordFile instead"; + environment.etc."roundcube/config.inc.php".text = '' Date: Sun, 5 Jan 2020 12:00:00 +0000 Subject: [PATCH 2/3] nixos/roundcube: don't use the default and insecure des_key The php installer creates a random one, but we bypass it, so we have to create one ourselves. This should be backward compatible as encryption is used for session cookies only: users at the time of the upgrade will be logged out but nothing more. https://github.com/roundcube/roundcubemail/blob/259b7fa0650fea9320b38cb17c4e80497acae7a3/config/config.inc.php.sample#L73 --- nixos/modules/services/mail/roundcube.nix | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/nixos/modules/services/mail/roundcube.nix b/nixos/modules/services/mail/roundcube.nix index b064c717958..22c14666a3a 100644 --- a/nixos/modules/services/mail/roundcube.nix +++ b/nixos/modules/services/mail/roundcube.nix @@ -107,6 +107,7 @@ in $config['log_driver'] = 'syslog'; $config['max_message_size'] = '25M'; $config['plugins'] = [${concatMapStringsSep "," (p: "'${p}'") cfg.plugins}]; + $config['des_key'] = file_get_contents('/var/lib/roundcube/des_key'); ${cfg.extraConfig} ''; @@ -190,12 +191,21 @@ in ${psql} -f ${cfg.package}/SQL/postgres.initial.sql fi + if [ ! -f /var/lib/roundcube/des_key ]; then + base64 /dev/urandom | head -c 24 > /var/lib/roundcube/des_key; + # we need to log out everyone in case change the des_key + # from the default when upgrading from nixos 19.09 + ${psql} <<< 'TRUNCATE TABLE session;' + fi + ${pkgs.php}/bin/php ${cfg.package}/bin/update.sh ''; serviceConfig = { Type = "oneshot"; StateDirectory = "roundcube"; User = if localDB then user else "nginx"; + # so that the des_key is not world readable + StateDirectoryMode = "0700"; }; } ]; From b5d692e12336b594c12c6ee1a21e7c3181c6ed98 Mon Sep 17 00:00:00 2001 From: Symphorien Gibol Date: Sun, 5 Jan 2020 12:00:00 +0000 Subject: [PATCH 3/3] nixos/roundcube: provide path to mime.types file fixes this warning: WARNING: Mimetype to file extension mapping doesn't work properly! --- nixos/modules/services/mail/roundcube.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/nixos/modules/services/mail/roundcube.nix b/nixos/modules/services/mail/roundcube.nix index 22c14666a3a..0bb0eaedad5 100644 --- a/nixos/modules/services/mail/roundcube.nix +++ b/nixos/modules/services/mail/roundcube.nix @@ -108,6 +108,7 @@ in $config['max_message_size'] = '25M'; $config['plugins'] = [${concatMapStringsSep "," (p: "'${p}'") cfg.plugins}]; $config['des_key'] = file_get_contents('/var/lib/roundcube/des_key'); + $config['mime_types'] = '${pkgs.nginx}/conf/mime.types'; ${cfg.extraConfig} '';