From c0df22aaae62242c6335f9ac14c6dd58c9e36ca7 Mon Sep 17 00:00:00 2001 From: geistesk Date: Mon, 2 Apr 2018 15:42:58 +0200 Subject: [PATCH] Support for multiple extraVirtualAliases Should fix #104 by introducing ``` extraVirtualAliases = { "single-alias@domain.foobar" = "user1@domain.foobar"; "multi-alias@domain.foobar" = [ "user1@domain.foobar" "user2@domain.foobar" ]; }; ``` --- default.nix | 21 ++++++++++++---- mail-server/postfix.nix | 5 +++- tests/extern.nix | 56 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 6 deletions(-) diff --git a/default.nix b/default.nix index 43041a5..e5dbc3e 100644 --- a/default.nix +++ b/default.nix @@ -135,19 +135,30 @@ in }; extraVirtualAliases = mkOption { - type = types.attrsOf (types.enum (builtins.attrNames cfg.loginAccounts)); + type = types.loaOf (mkOptionType { + name = "Login Account"; + check = (ele: + let accounts = builtins.attrNames cfg.loginAccounts; + in if (builtins.isList ele) + then (builtins.all (x: builtins.elem x accounts) ele) && (builtins.length ele > 0) + else (builtins.elem ele accounts)); + }); example = { "info@example.com" = "user1@example.com"; "postmaster@example.com" = "user1@example.com"; "abuse@example.com" = "user1@example.com"; + "multi@example.com" = [ "user1@example.com" "user2@example.com" ]; }; description = '' - Virtual Aliases. A virtual alias `"info@example2.com" = "user1@example.com"` means that - all mail to `info@example2.com` is forwarded to `user1@example.com`. Note + Virtual Aliases. A virtual alias `"info@example.com" = "user1@example.com"` means that + all mail to `info@example.com` is forwarded to `user1@example.com`. Note that it is expected that `postmaster@example.com` and `abuse@example.com` is forwarded to some valid email address. (Alternatively you can create login accounts for `postmaster` and (or) `abuse`). Furthermore, it also allows - the user `user1@example.com` to send emails as `info@example2.com`. + the user `user1@example.com` to send emails as `info@example.com`. + It's also possible to create an alias for multiple accounts. In this + example all mails for `multi@example.com` will be forwarded to both + `user1@example.com` and `user2@example.com`. ''; default = {}; }; @@ -160,7 +171,7 @@ in "abuse@example.com" = "user1@example.com"; }; description = '' - Alias for extraVirtualAliases. Deprecated. + Alias for extraVirtualAliases, but only for single aliases! Deprecated. ''; default = {}; }; diff --git a/mail-server/postfix.nix b/mail-server/postfix.nix index de9b726..bf20371 100644 --- a/mail-server/postfix.nix +++ b/mail-server/postfix.nix @@ -47,7 +47,10 @@ let (map (from: let to = cfg.extraVirtualAliases.${from}; - in "${from} ${to}") + aliasList = (l: let aliasStr = builtins.foldl' (x: y: x + y + ", ") "" l; + in builtins.substring 0 (builtins.stringLength aliasStr - 2) aliasStr); + in if (builtins.isList to) then "${from} " + (aliasList to) + else "${from} ${to}") (builtins.attrNames cfg.extraVirtualAliases)); # all_valiases_postfix :: [ String ] diff --git a/tests/extern.nix b/tests/extern.nix index 32af471..9aea6a5 100644 --- a/tests/extern.nix +++ b/tests/extern.nix @@ -49,6 +49,11 @@ import { }; }; + extraVirtualAliases = { + "single-alias@example.com" = "user1@example.com"; + "multi-alias@example.com" = [ "user1@example.com" "user2@example.com" ]; + }; + enableImap = true; }; }; @@ -113,6 +118,13 @@ import { from postmaster@example.com user user1@example.com password user1 + + account test5 + host ${serverIP} + port 587 + from single-alias@example.com + user user1@example.com + password user1 ''; }; "root/email1".text = '' @@ -154,6 +166,34 @@ import { I think I may have misconfigured the mail server XOXO Postmaster ''; + "root/email4".text = '' + From: Single Alias + To: User1 + Cc: + Bcc: + Subject: This is a test Email from single-alias\@example.com to user1 + Reply-To: + + Hello User1, + + how are you doing today? + + XOXO User1 aka Single Alias + ''; + "root/email5".text = '' + From: User2 + To: Multi Alias + Cc: + Bcc: + Subject: This is a test Email from user2\@example.com to multi-alias + Reply-To: + + Hello Multi Alias, + + how are we doing today? + + XOXO User1 + ''; }; }; }; @@ -238,6 +278,22 @@ import { $client->fail("fetchmail -v"); }; + subtest "extraVirtualAliases", sub { + $client->execute("rm ~/mail/*"); + # send email from single-alias to user1 + $client->succeed("msmtp -a test5 --tls=on --tls-certcheck=off --auth=on user1\@example.com < /etc/root/email4 >&2"); + $server->waitUntilFails('[ "$(postqueue -p)" != "Mail queue is empty" ]'); + # fetchmail returns EXIT_CODE 0 when it retrieves mail + $client->succeed("fetchmail -v"); + + $client->execute("rm ~/mail/*"); + # send email from user1 to multi-alias (user{1,2}@example.com) + $client->succeed("msmtp -a test --tls=on --tls-certcheck=off --auth=on multi-alias\@example.com < /etc/root/email5 >&2"); + $server->waitUntilFails('[ "$(postqueue -p)" != "Mail queue is empty" ]'); + # fetchmail returns EXIT_CODE 0 when it retrieves mail + $client->succeed("fetchmail -v"); + }; + subtest "quota", sub { $client->execute("rm ~/mail/*"); $client->execute("mv ~/.fetchmailRcLowQuota ~/.fetchmailrc");