1let
2 certs = import ./common/acme/server/snakeoil-certs.nix;
3 domain = certs.domain;
4in
5import ./make-test-python.nix {
6 name = "postfix";
7
8 nodes.machine =
9 { pkgs, ... }:
10 {
11 imports = [ common/user-account.nix ];
12 services.postfix = {
13 enable = true;
14 enableSubmission = true;
15 enableSubmissions = true;
16 settings.main = {
17 smtp_tls_CAfile = "${certs.ca.cert}";
18 smtpd_tls_chain_files = [
19 certs.${domain}.key
20 certs.${domain}.cert
21 ];
22 smtpd_sasl_auth_enable = "yes";
23 cyrus_sasl_config_path =
24 let
25 smtpdConf = pkgs.writeTextFile {
26 name = "smtpd.conf";
27 destination = "/etc/sasl2/smtpd.conf";
28 text = ''
29 pwcheck_method: saslauthd
30 mech_list: PLAIN LOGIN
31 '';
32 };
33 in
34 "${smtpdConf}/etc/sasl2";
35 };
36 submissionsOptions = {
37 smtpd_sasl_auth_enable = "yes";
38 smtpd_client_restrictions = "permit";
39 milter_macro_daemon_name = "ORIGINATING";
40 };
41 };
42 services.saslauthd.enable = true;
43
44 security.pki.certificateFiles = [
45 certs.ca.cert
46 ];
47 security.pam.services = {
48 # note: no 'd' on the end!
49 smtp = {
50 name = "smtp";
51 };
52 };
53
54 networking.extraHosts = ''
55 127.0.0.1 ${domain}
56 '';
57
58 environment.systemPackages =
59 let
60 sendTestMail = pkgs.writers.writePython3Bin "send-testmail" { } ''
61 import smtplib
62
63 with smtplib.SMTP('${domain}') as smtp:
64 smtp.sendmail('root@localhost', 'alice@localhost',
65 'Subject: Test\n\nTest data.')
66 smtp.quit()
67 '';
68
69 sendTestMailStarttls = pkgs.writers.writePython3Bin "send-testmail-starttls" { } ''
70 import smtplib
71 import ssl
72
73 ctx = ssl.create_default_context()
74
75 with smtplib.SMTP('${domain}') as smtp:
76 smtp.ehlo()
77 smtp.starttls(context=ctx)
78 smtp.ehlo()
79 smtp.sendmail('root@localhost', 'alice@localhost',
80 'Subject: Test STARTTLS\n\nTest data.')
81 smtp.quit()
82 '';
83
84 sendTestMailSmtps = pkgs.writers.writePython3Bin "send-testmail-smtps" { } ''
85 import smtplib
86 import ssl
87
88 ctx = ssl.create_default_context()
89
90 with smtplib.SMTP_SSL(host='${domain}', context=ctx) as smtp:
91 smtp.sendmail('root@localhost', 'alice@localhost',
92 'Subject: Test SMTPS\n\nTest data.')
93 smtp.quit()
94 '';
95
96 auth = pkgs.writers.writePython3Bin "auth" { } ''
97 import smtplib
98
99 with smtplib.SMTP('${domain}') as smtp:
100 smtp.ehlo()
101 smtp.login("alice", "foobar")
102 smtp.quit()
103 '';
104
105 authStarttls = pkgs.writers.writePython3Bin "authStarttls" { } ''
106 import smtplib
107 import ssl
108
109 ctx = ssl.create_default_context()
110
111 with smtplib.SMTP('${domain}') as smtp:
112 smtp.ehlo()
113 smtp.starttls(context=ctx)
114 smtp.ehlo()
115 smtp.login("alice", "foobar")
116 smtp.quit()
117 '';
118
119 authSmtps = pkgs.writers.writePython3Bin "authSmtps" { } ''
120 import smtplib
121 import ssl
122
123 ctx = ssl.create_default_context()
124
125 with smtplib.SMTP_SSL('${domain}', context=ctx) as smtp:
126 smtp.ehlo()
127 smtp.login("alice", "foobar")
128 smtp.quit()
129 '';
130 in
131 [
132 sendTestMail
133 sendTestMailStarttls
134 sendTestMailSmtps
135 auth
136 authStarttls
137 authSmtps
138 ];
139 };
140
141 testScript = ''
142 machine.wait_for_unit("postfix.service")
143 machine.succeed("send-testmail")
144 machine.succeed("send-testmail-starttls")
145 machine.succeed("send-testmail-smtps")
146 machine.succeed("auth")
147 machine.succeed("authStarttls")
148 machine.succeed("authSmtps")
149 '';
150}