2022-08-12 21:54:49 +05: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 13:08:50 +05:00
|
|
|
services.openssh = {
|
|
|
|
enable = true;
|
|
|
|
permitRootLogin = "yes";
|
|
|
|
};
|
|
|
|
|
2022-08-12 21:54:49 +05:00
|
|
|
environment.systemPackages = [
|
|
|
|
(pkgs.writeScriptBin "mail-check" ''
|
|
|
|
${pkgs.python3}/bin/python ${../scripts/mail-check.py} $@
|
|
|
|
'')];
|
|
|
|
|
2023-05-20 03:12:02 +05:00
|
|
|
environment.etc.bind-password.text = bindPassword;
|
|
|
|
|
2022-08-12 21:54:49 +05:00
|
|
|
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";
|
2023-05-20 03:12:02 +05:00
|
|
|
olcDbDirectory = "/var/lib/openldap/example";
|
2022-08-12 21:54:49 +05:00
|
|
|
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";
|
2023-05-20 03:12:02 +05:00
|
|
|
passwordFile = "/etc/bind-password";
|
2022-08-12 21:54:49 +05:00
|
|
|
};
|
|
|
|
searchBase = "ou=users,dc=example";
|
|
|
|
searchScope = "sub";
|
|
|
|
};
|
|
|
|
|
|
|
|
vmailGroupName = "vmail";
|
|
|
|
vmailUID = 5000;
|
|
|
|
|
|
|
|
enableImap = false;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
testScript = ''
|
|
|
|
import sys
|
2023-05-19 13:08:50 +05:00
|
|
|
import re
|
2022-08-12 21:54:49 +05:00
|
|
|
|
|
|
|
machine.start()
|
|
|
|
machine.wait_for_unit("multi-user.target")
|
|
|
|
|
2023-05-19 13:08:50 +05: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 21:54:49 +05:00
|
|
|
try:
|
|
|
|
assert value == expected
|
|
|
|
except AssertionError:
|
2023-05-19 13:08:50 +05:00
|
|
|
print(f"Expected {conf} lookup for key '{key}' to return '{expected}, but got '{value}'", file=sys.stderr)
|
2022-08-12 21:54:49 +05:00
|
|
|
raise
|
|
|
|
|
|
|
|
with subtest("Test postmap lookups"):
|
2023-05-19 13:08:50 +05: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 21:54:49 +05:00
|
|
|
|
2023-05-19 13:08:50 +05: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 21:54:49 +05:00
|
|
|
|
|
|
|
with subtest("Test doveadm lookups"):
|
2023-05-19 13:08:50 +05:00
|
|
|
machine.succeed("doveadm user -u alice@example.com")
|
|
|
|
machine.succeed("doveadm user -u bob@example.com")
|
2022-08-12 21:54:49 +05:00
|
|
|
|
2023-05-20 03:12:02 +05:00
|
|
|
with subtest("Files containing secrets are only readable by root"):
|
|
|
|
machine.succeed("ls -l /run/postfix/*.cf | grep -e '-rw------- 1 root root'")
|
|
|
|
machine.succeed("ls -l /run/dovecot2/dovecot-ldap.conf.ext | grep -e '-rw------- 1 root root'")
|
|
|
|
|
2022-08-12 21:54:49 +05: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 13:08:50 +05:00
|
|
|
"--smtp-username alice@example.com",
|
2022-08-12 21:54:49 +05:00
|
|
|
"--imap-host localhost",
|
2023-05-19 13:08:50 +05:00
|
|
|
"--imap-username bob@example.com",
|
2022-08-12 21:54:49 +05: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 13:08:50 +05:00
|
|
|
machine.succeed("journalctl -u postfix | grep -q 'Sender address rejected: not owned by user alice@example.com'")
|
2022-08-12 21:54:49 +05: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 13:08:50 +05:00
|
|
|
"--smtp-username alice@example.com",
|
2022-08-12 21:54:49 +05:00
|
|
|
"--imap-host localhost",
|
2023-05-19 13:08:50 +05:00
|
|
|
"--imap-username bob@example.com",
|
2022-08-12 21:54:49 +05:00
|
|
|
"--from-addr alice@example.com",
|
|
|
|
"--to-addr bob@example.com",
|
|
|
|
"--src-password-file <(echo '${alicePassword}')",
|
|
|
|
"--dst-password-file <(echo '${bobPassword}')",
|
|
|
|
"--ignore-dkim-spf"
|
|
|
|
]))
|
|
|
|
'';
|
|
|
|
}
|