2022-08-12 18:54:49 +02:00
|
|
|
{ pkgs ? import <nixpkgs> {}
|
|
|
|
, ...
|
|
|
|
}:
|
|
|
|
|
|
|
|
let
|
|
|
|
bindPassword = "unsafegibberish";
|
|
|
|
alicePassword = "testalice";
|
|
|
|
bobPassword = "testbob";
|
|
|
|
in
|
|
|
|
pkgs.nixosTest {
|
|
|
|
name = "ldap";
|
|
|
|
nodes = {
|
|
|
|
machine = { config, pkgs, ... }: {
|
|
|
|
imports = [
|
|
|
|
./../default.nix
|
|
|
|
./lib/config.nix
|
|
|
|
];
|
|
|
|
|
|
|
|
virtualisation.memorySize = 1024;
|
|
|
|
|
2023-05-19 10:08:50 +02:00
|
|
|
services.openssh = {
|
|
|
|
enable = true;
|
|
|
|
permitRootLogin = "yes";
|
|
|
|
};
|
|
|
|
|
2022-08-12 18:54:49 +02:00
|
|
|
environment.systemPackages = [
|
|
|
|
(pkgs.writeScriptBin "mail-check" ''
|
|
|
|
${pkgs.python3}/bin/python ${../scripts/mail-check.py} $@
|
|
|
|
'')];
|
|
|
|
|
|
|
|
services.openldap = {
|
|
|
|
enable = true;
|
|
|
|
settings = {
|
|
|
|
children = {
|
|
|
|
"cn=schema".includes = [
|
|
|
|
"${pkgs.openldap}/etc/schema/core.ldif"
|
|
|
|
"${pkgs.openldap}/etc/schema/cosine.ldif"
|
|
|
|
"${pkgs.openldap}/etc/schema/inetorgperson.ldif"
|
|
|
|
"${pkgs.openldap}/etc/schema/nis.ldif"
|
|
|
|
];
|
|
|
|
"olcDatabase={1}mdb" = {
|
|
|
|
attrs = {
|
|
|
|
objectClass = [
|
|
|
|
"olcDatabaseConfig"
|
|
|
|
"olcMdbConfig"
|
|
|
|
];
|
|
|
|
olcDatabase = "{1}mdb";
|
|
|
|
olcDbDirectory = "/var/lib/openldap";
|
|
|
|
olcSuffix = "dc=example";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
declarativeContents."dc=example" = ''
|
|
|
|
dn: dc=example
|
|
|
|
objectClass: domain
|
|
|
|
dc: example
|
|
|
|
|
|
|
|
dn: cn=mail,dc=example
|
|
|
|
objectClass: organizationalRole
|
|
|
|
objectClass: simpleSecurityObject
|
|
|
|
objectClass: top
|
|
|
|
cn: mail
|
|
|
|
userPassword: ${bindPassword}
|
|
|
|
|
|
|
|
dn: ou=users,dc=example
|
|
|
|
objectClass: organizationalUnit
|
|
|
|
ou: users
|
|
|
|
|
|
|
|
dn: cn=alice,ou=users,dc=example
|
|
|
|
objectClass: inetOrgPerson
|
|
|
|
cn: alice
|
|
|
|
sn: Foo
|
|
|
|
mail: alice@example.com
|
|
|
|
userPassword: ${alicePassword}
|
|
|
|
|
|
|
|
dn: cn=bob,ou=users,dc=example
|
|
|
|
objectClass: inetOrgPerson
|
|
|
|
cn: bob
|
|
|
|
sn: Bar
|
|
|
|
mail: bob@example.com
|
|
|
|
userPassword: ${bobPassword}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
mailserver = {
|
|
|
|
enable = true;
|
|
|
|
fqdn = "mail.example.com";
|
|
|
|
domains = [ "example.com" ];
|
|
|
|
localDnsResolver = false;
|
|
|
|
|
|
|
|
ldap = {
|
|
|
|
enable = true;
|
|
|
|
uris = [
|
|
|
|
"ldap://"
|
|
|
|
];
|
|
|
|
bind = {
|
|
|
|
dn = "cn=mail,dc=example";
|
|
|
|
password = bindPassword;
|
|
|
|
};
|
|
|
|
searchBase = "ou=users,dc=example";
|
|
|
|
searchScope = "sub";
|
|
|
|
};
|
|
|
|
|
|
|
|
vmailGroupName = "vmail";
|
|
|
|
vmailUID = 5000;
|
|
|
|
|
|
|
|
enableImap = false;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
testScript = ''
|
|
|
|
import sys
|
2023-05-19 10:08:50 +02:00
|
|
|
import re
|
2022-08-12 18:54:49 +02:00
|
|
|
|
|
|
|
machine.start()
|
|
|
|
machine.wait_for_unit("multi-user.target")
|
|
|
|
|
2023-05-19 10:08:50 +02:00
|
|
|
# This function retrieves the ldap table file from a postconf
|
|
|
|
# command.
|
|
|
|
# A key lookup is achived and the returned value is compared
|
|
|
|
# to the expected value.
|
|
|
|
def test_lookup(postconf_cmdline, key, expected):
|
|
|
|
conf = machine.succeed(postconf_cmdline).rstrip()
|
|
|
|
ldap_table_path = re.match('.* =.*ldap:(.*)', conf).group(1)
|
|
|
|
value = machine.succeed(f"postmap -q {key} ldap:{ldap_table_path}").rstrip()
|
2022-08-12 18:54:49 +02:00
|
|
|
try:
|
|
|
|
assert value == expected
|
|
|
|
except AssertionError:
|
2023-05-19 10:08:50 +02:00
|
|
|
print(f"Expected {conf} lookup for key '{key}' to return '{expected}, but got '{value}'", file=sys.stderr)
|
2022-08-12 18:54:49 +02:00
|
|
|
raise
|
|
|
|
|
|
|
|
with subtest("Test postmap lookups"):
|
2023-05-19 10:08:50 +02:00
|
|
|
test_lookup("postconf virtual_mailbox_maps", "alice@example.com", "alice@example.com")
|
|
|
|
test_lookup("postconf -P submission/inet/smtpd_sender_login_maps", "alice@example.com", "alice@example.com")
|
2022-08-12 18:54:49 +02:00
|
|
|
|
2023-05-19 10:08:50 +02:00
|
|
|
test_lookup("postconf virtual_mailbox_maps", "bob@example.com", "bob@example.com")
|
|
|
|
test_lookup("postconf -P submission/inet/smtpd_sender_login_maps", "bob@example.com", "bob@example.com")
|
2022-08-12 18:54:49 +02:00
|
|
|
|
|
|
|
with subtest("Test doveadm lookups"):
|
2023-05-19 10:08:50 +02:00
|
|
|
machine.succeed("doveadm user -u alice@example.com")
|
|
|
|
machine.succeed("doveadm user -u bob@example.com")
|
2022-08-12 18:54:49 +02:00
|
|
|
|
|
|
|
with subtest("Test account/mail address binding"):
|
|
|
|
machine.fail(" ".join([
|
|
|
|
"mail-check send-and-read",
|
|
|
|
"--smtp-port 587",
|
|
|
|
"--smtp-starttls",
|
|
|
|
"--smtp-host localhost",
|
2023-05-19 10:08:50 +02:00
|
|
|
"--smtp-username alice@example.com",
|
2022-08-12 18:54:49 +02:00
|
|
|
"--imap-host localhost",
|
2023-05-19 10:08:50 +02:00
|
|
|
"--imap-username bob@example.com",
|
2022-08-12 18:54:49 +02:00
|
|
|
"--from-addr bob@example.com",
|
|
|
|
"--to-addr aliceb@example.com",
|
|
|
|
"--src-password-file <(echo '${alicePassword}')",
|
|
|
|
"--dst-password-file <(echo '${bobPassword}')",
|
|
|
|
"--ignore-dkim-spf"
|
|
|
|
]))
|
2023-05-19 10:08:50 +02:00
|
|
|
machine.succeed("journalctl -u postfix | grep -q 'Sender address rejected: not owned by user alice@example.com'")
|
2022-08-12 18:54:49 +02:00
|
|
|
|
|
|
|
with subtest("Test mail delivery"):
|
|
|
|
machine.succeed(" ".join([
|
|
|
|
"mail-check send-and-read",
|
|
|
|
"--smtp-port 587",
|
|
|
|
"--smtp-starttls",
|
|
|
|
"--smtp-host localhost",
|
2023-05-19 10:08:50 +02:00
|
|
|
"--smtp-username alice@example.com",
|
2022-08-12 18:54:49 +02:00
|
|
|
"--imap-host localhost",
|
2023-05-19 10:08:50 +02:00
|
|
|
"--imap-username bob@example.com",
|
2022-08-12 18:54:49 +02:00
|
|
|
"--from-addr alice@example.com",
|
|
|
|
"--to-addr bob@example.com",
|
|
|
|
"--src-password-file <(echo '${alicePassword}')",
|
|
|
|
"--dst-password-file <(echo '${bobPassword}')",
|
|
|
|
"--ignore-dkim-spf"
|
|
|
|
]))
|
|
|
|
'';
|
|
|
|
}
|