summaryrefslogtreecommitdiffstats
path: root/nixos/tests/postfix.nix
blob: 4a20a99b28f66c54c4cedf0d13fdc36edde056a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
let
  certs = import ./common/acme/server/snakeoil-certs.nix;
  domain = certs.domain;
in
import ./make-test-python.nix {
  name = "postfix";

  nodes.machine =
    { pkgs, ... }:
    {
      imports = [ common/user-account.nix ];
      services.postfix = {
        enable = true;
        enableSubmission = true;
        enableSubmissions = true;
        settings.main = {
          smtp_tls_CAfile = "${certs.ca.cert}";
          smtpd_tls_chain_files = [
            certs.${domain}.key
            certs.${domain}.cert
          ];
          smtpd_sasl_auth_enable = "yes";
          cyrus_sasl_config_path =
            let
              smtpdConf = pkgs.writeTextFile {
                name = "smtpd.conf";
                destination = "/etc/sasl2/smtpd.conf";
                text = ''
                  pwcheck_method: saslauthd
                  mech_list: PLAIN LOGIN
                '';
              };
            in
            "${smtpdConf}/etc/sasl2";
        };
        submissionsOptions = {
          smtpd_sasl_auth_enable = "yes";
          smtpd_client_restrictions = "permit";
          milter_macro_daemon_name = "ORIGINATING";
        };
      };
      services.saslauthd.enable = true;

      security.pki.certificateFiles = [
        certs.ca.cert
      ];
      security.pam.services = {
        # note: no 'd' on the end!
        smtp = {
          name = "smtp";
        };
      };

      networking.extraHosts = ''
        127.0.0.1 ${domain}
      '';

      environment.systemPackages =
        let
          sendTestMail = pkgs.writers.writePython3Bin "send-testmail" { } ''
            import smtplib

            with smtplib.SMTP('${domain}') as smtp:
                smtp.sendmail('root@localhost', 'alice@localhost',
                              'Subject: Test\n\nTest data.')
                smtp.quit()
          '';

          sendTestMailStarttls = pkgs.writers.writePython3Bin "send-testmail-starttls" { } ''
            import smtplib
            import ssl

            ctx = ssl.create_default_context()

            with smtplib.SMTP('${domain}') as smtp:
                smtp.ehlo()
                smtp.starttls(context=ctx)
                smtp.ehlo()
                smtp.sendmail('root@localhost', 'alice@localhost',
                              'Subject: Test STARTTLS\n\nTest data.')
                smtp.quit()
          '';

          sendTestMailSmtps = pkgs.writers.writePython3Bin "send-testmail-smtps" { } ''
            import smtplib
            import ssl

            ctx = ssl.create_default_context()

            with smtplib.SMTP_SSL(host='${domain}', context=ctx) as smtp:
                smtp.sendmail('root@localhost', 'alice@localhost',
                              'Subject: Test SMTPS\n\nTest data.')
                smtp.quit()
          '';

          auth = pkgs.writers.writePython3Bin "auth" { } ''
            import smtplib

            with smtplib.SMTP('${domain}') as smtp:
                smtp.ehlo()
                smtp.login("alice", "foobar")
                smtp.quit()
          '';

          authStarttls = pkgs.writers.writePython3Bin "authStarttls" { } ''
            import smtplib
            import ssl

            ctx = ssl.create_default_context()

            with smtplib.SMTP('${domain}') as smtp:
                smtp.ehlo()
                smtp.starttls(context=ctx)
                smtp.ehlo()
                smtp.login("alice", "foobar")
                smtp.quit()
          '';

          authSmtps = pkgs.writers.writePython3Bin "authSmtps" { } ''
            import smtplib
            import ssl

            ctx = ssl.create_default_context()

            with smtplib.SMTP_SSL('${domain}', context=ctx) as smtp:
                smtp.ehlo()
                smtp.login("alice", "foobar")
                smtp.quit()
          '';
        in
        [
          sendTestMail
          sendTestMailStarttls
          sendTestMailSmtps
          auth
          authStarttls
          authSmtps
        ];
    };

  testScript = ''
    machine.wait_for_unit("postfix.service")
    machine.succeed("send-testmail")
    machine.succeed("send-testmail-starttls")
    machine.succeed("send-testmail-smtps")
    machine.succeed("auth")
    machine.succeed("authStarttls")
    machine.succeed("authSmtps")
  '';
}