1 import ./make-test-python.nix {
2 name = "opensmtpd-rspamd";
5 smtp1 = { pkgs, ... }: {
6 imports = [ common/user-account.nix ];
8 firewall.allowedTCPPorts = [ 25 143 ];
10 interfaces.eth1.ipv4.addresses = pkgs.lib.mkOverride 0 [
11 { address = "192.168.1.1"; prefixLength = 24; }
14 environment.systemPackages = [ pkgs.opensmtpd ];
15 services.opensmtpd = {
17 extraServerArgs = [ "-v" ];
18 serverConfiguration = ''
20 action dovecot_deliver mda \
21 "${pkgs.dovecot}/libexec/dovecot/deliver -d %{user.username}"
22 match from any for local action dovecot_deliver
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
35 mailLocation = "maildir:~/mail";
36 protocols = [ "imap" ];
40 smtp2 = { pkgs, ... }: {
41 imports = [ common/user-account.nix ];
43 firewall.allowedTCPPorts = [ 25 143 ];
45 interfaces.eth1.ipv4.addresses = pkgs.lib.mkOverride 0 [
46 { address = "192.168.1.2"; prefixLength = 24; }
49 environment.systemPackages = [ pkgs.opensmtpd ];
52 locals."worker-normal.inc".text = ''
53 bind_socket = "127.0.0.1:11333";
56 services.opensmtpd = {
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
70 mailLocation = "maildir:~/mail";
71 protocols = [ "imap" ];
75 client = { pkgs, ... }: {
78 interfaces.eth1.ipv4.addresses = pkgs.lib.mkOverride 0 [
79 { address = "192.168.1.3"; prefixLength = 24; }
82 environment.systemPackages = let
83 sendTestMail = pkgs.writeScriptBin "send-a-test-mail" ''
84 #!${pkgs.python3.interpreter}
87 with smtplib.SMTP('192.168.1.1') as smtp:
88 smtp.sendmail('alice@[192.168.1.1]', 'bob@[192.168.1.2]', """
94 Here goes the spam test
95 XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X
99 checkMailBounced = pkgs.writeScriptBin "check-mail-bounced" ''
100 #!${pkgs.python3.interpreter}
103 with imaplib.IMAP4('192.168.1.1', 143) as imap:
104 imap.login('alice', 'foobar')
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'
112 print("===> content:", content)
113 assert b"An error has occurred while attempting to deliver a message" in content
115 in [ sendTestMail checkMailBounced ];
122 client.systemctl("start network-online.target")
123 client.wait_for_unit("network-online.target")
124 smtp1.wait_for_unit("opensmtpd")
125 smtp2.wait_for_unit("opensmtpd")
126 smtp2.wait_for_unit("rspamd")
127 smtp2.wait_for_unit("dovecot2")
129 # To prevent sporadic failures during daemon startup, make sure
130 # services are listening on their ports before sending requests
131 smtp1.wait_for_open_port(25)
132 smtp2.wait_for_open_port(25)
133 smtp2.wait_for_open_port(143)
134 smtp2.wait_for_open_port(11333)
136 client.succeed("send-a-test-mail")
137 smtp1.wait_until_fails("smtpctl show queue | egrep .")
138 client.succeed("check-mail-bounced >&2")