1import ./make-test-python.nix {
2 name = "opensmtpd-rspamd";
3
4 nodes = {
5 smtp1 = { pkgs, ... }: {
6 imports = [ common/user-account.nix ];
7 networking = {
8 firewall.allowedTCPPorts = [ 25 143 ];
9 useDHCP = false;
10 interfaces.eth1.ipv4.addresses = pkgs.lib.mkOverride 0 [
11 { address = "192.168.1.1"; prefixLength = 24; }
12 ];
13 };
14 environment.systemPackages = [ pkgs.opensmtpd ];
15 services.opensmtpd = {
16 enable = true;
17 extraServerArgs = [ "-v" ];
18 serverConfiguration = ''
19 listen on 0.0.0.0
20 action dovecot_deliver mda \
21 "${pkgs.dovecot}/libexec/dovecot/deliver -d %{user.username}"
22 match from any for local action dovecot_deliver
23
24 action do_relay relay
25 # DO NOT DO THIS IN PRODUCTION!
26 # Setting up authentication requires a certificate which is painful in
27 # a test environment, but THIS WOULD BE DANGEROUS OUTSIDE OF A
28 # WELL-CONTROLLED ENVIRONMENT!
29 match from any for any action do_relay
30 '';
31 };
32 services.dovecot2 = {
33 enable = true;
34 enableImap = true;
35 mailLocation = "maildir:~/mail";
36 protocols = [ "imap" ];
37 };
38 };
39
40 smtp2 = { pkgs, ... }: {
41 imports = [ common/user-account.nix ];
42 networking = {
43 firewall.allowedTCPPorts = [ 25 143 ];
44 useDHCP = false;
45 interfaces.eth1.ipv4.addresses = pkgs.lib.mkOverride 0 [
46 { address = "192.168.1.2"; prefixLength = 24; }
47 ];
48 };
49 environment.systemPackages = [ pkgs.opensmtpd ];
50 services.rspamd = {
51 enable = true;
52 locals."worker-normal.inc".text = ''
53 bind_socket = "127.0.0.1:11333";
54 '';
55 };
56 services.opensmtpd = {
57 enable = true;
58 extraServerArgs = [ "-v" ];
59 serverConfiguration = ''
60 filter rspamd proc-exec "${pkgs.opensmtpd-filter-rspamd}/bin/filter-rspamd"
61 listen on 0.0.0.0 filter rspamd
62 action dovecot_deliver mda \
63 "${pkgs.dovecot}/libexec/dovecot/deliver -d %{user.username}"
64 match from any for local action dovecot_deliver
65 '';
66 };
67 services.dovecot2 = {
68 enable = true;
69 enableImap = true;
70 mailLocation = "maildir:~/mail";
71 protocols = [ "imap" ];
72 };
73 };
74
75 client = { pkgs, ... }: {
76 networking = {
77 useDHCP = false;
78 interfaces.eth1.ipv4.addresses = pkgs.lib.mkOverride 0 [
79 { address = "192.168.1.3"; prefixLength = 24; }
80 ];
81 };
82 environment.systemPackages = let
83 sendTestMail = pkgs.writeScriptBin "send-a-test-mail" ''
84 #!${pkgs.python3.interpreter}
85 import smtplib, sys
86
87 with smtplib.SMTP('192.168.1.1') as smtp:
88 smtp.sendmail('alice@[192.168.1.1]', 'bob@[192.168.1.2]', """
89 From: alice@smtp1
90 To: bob@smtp2
91 Subject: Test
92
93 Hello World
94 Here goes the spam test
95 XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X
96 """)
97 '';
98
99 checkMailBounced = pkgs.writeScriptBin "check-mail-bounced" ''
100 #!${pkgs.python3.interpreter}
101 import imaplib
102
103 with imaplib.IMAP4('192.168.1.1', 143) as imap:
104 imap.login('alice', 'foobar')
105 imap.select()
106 status, refs = imap.search(None, 'ALL')
107 assert status == 'OK'
108 assert len(refs) == 1
109 status, msg = imap.fetch(refs[0], 'BODY[TEXT]')
110 assert status == 'OK'
111 content = msg[0][1]
112 print("===> content:", content)
113 assert b"An error has occurred while attempting to deliver a message" in content
114 '';
115 in [ sendTestMail checkMailBounced ];
116 };
117 };
118
119 testScript = ''
120 start_all()
121
122 client.wait_for_unit("network-online.target")
123 smtp1.wait_for_unit("opensmtpd")
124 smtp2.wait_for_unit("opensmtpd")
125 smtp2.wait_for_unit("rspamd")
126 smtp2.wait_for_unit("dovecot2")
127
128 # To prevent sporadic failures during daemon startup, make sure
129 # services are listening on their ports before sending requests
130 smtp1.wait_for_open_port(25)
131 smtp2.wait_for_open_port(25)
132 smtp2.wait_for_open_port(143)
133 smtp2.wait_for_open_port(11333)
134
135 client.succeed("send-a-test-mail")
136 smtp1.wait_until_fails("smtpctl show queue | egrep .")
137 client.succeed("check-mail-bounced >&2")
138 '';
139
140 meta.timeout = 1800;
141}