]> Git — Sourcephile - sourcephile-nix.git/blob - servers/mermet/postfix.nix
nix: fix nixpkgs-overlays=
[sourcephile-nix.git] / servers / mermet / postfix.nix
1 { pkgs, lib, config, ... }:
2 let
3 inherit (builtins) attrNames concatStringsSep readFile toPath;
4 inherit (builtins.extraBuiltins) pass;
5 inherit (lib) types;
6 inherit (pkgs.lib) loadFile unlines unwords unlinesAttrs;
7 inherit (config) networking users;
8 inherit (config.services) postfix dovecot2 openldap;
9 in
10 {
11 imports = [
12 postfix/sourcephile.fr.nix
13 postfix/autogeree.net.nix
14 ];
15 options = {
16 services.postfix = {
17 tls_server_sni_maps = lib.mkOption {
18 type = types.attrsOf (types.listOf types.path);
19 default = {};
20 apply = m: pkgs.writeText "sni" (lib.concatStringsSep "\n" (lib.mapAttrsToList (domain: x509: ''
21 ${domain} ${unwords x509}
22 '') m));
23 };
24 };
25 };
26 config = {
27 users.groups.acme.members = [ postfix.user ];
28 systemd.services.postfix = {
29 after = ["openldap.service"];
30 preStart = ''
31 install -m 400 -o root -g root ${postfix.tls_server_sni_maps} /run/keys/postfix-sni
32 ${pkgs.postfix}/bin/postmap -F hash:/run/keys/postfix-sni
33 '';
34 };
35 services.postfix = {
36 enable = true;
37 networksStyle = "host";
38 hostname ="${networking.hostName}.${networking.domain}";
39 domain = networking.domain;
40 origin = "$myhostname";
41 destination = [
42 "localhost"
43 "localhost.localdomain"
44 "$myhostname"
45 ];
46 postmasterAlias = "root";
47 rootAlias = "root@${networking.domain}";
48 sslCert = loadFile (../../../sec/openssl + "/${networking.domain}/cert.self-signed.pem");
49 sslKey = "/run/keys/${networking.domain}.key.pem";
50 networks = [
51 "127.0.0.0/8"
52 "[::1]/128"
53 ];
54 setSendmail = true;
55 # Parse the extension in email address, eg. contact+extension@
56 recipientDelimiter = "+";
57 config = {
58 debug_peer_level = "4";
59 debug_peer_list = [
60 #"chomsky.autogeree.net"
61 #"localhost"
62 #"mail.sourcephile.fr"
63 ];
64
65 #
66 # Sending to the world
67 #
68 # Appending .domain is the MUA's job
69 append_dot_mydomain = false;
70 smtp_body_checks = "";
71 #smtp_cname_overrides_servername = false;
72 smtp_connect_timeout = "60s";
73 #smtp_header_checks = "regexp:/var/lib/postfix/smtp_header_checks";
74 smtp_mime_header_checks = "";
75 smtp_nested_header_checks = "";
76 smtp_tls_exclude_ciphers = [ "ADH" "MD5" "CAMELLIA" "SEED" "3DES" "DES" "RC4" "eNULL" "aNULL" ];
77 #smtp_tls_fingerprint_digest = "sha1";
78 smtp_tls_loglevel = "1";
79 #smtp_tls_note_starttls_offer = true;
80 #smtp_tls_policy_maps = "hash:/var/lib/postfix/conf/tls_policy";
81 # Only allow TLSv* protocols
82 smtp_tls_protocols = [ "!SSLv2" "!SSLv3" ];
83 #smtp_tls_scert_verifydepth = "5";
84 #smtp_tls_secure_cert_match = [ "nexthop" "dot-nexthop" ];
85 smtp_tls_security_level = "may";
86 smtp_tls_session_cache_database = "btree:$data_directory/smtp_tls_session_cache";
87 #smtp_tls_session_cache_timeout = "3600s";
88 #smtp_tls_verify_cert_match = "hostname";
89
90 #
91 # Receiving from the world
92 #
93 message_size_limit = "20480000";
94 maximal_queue_lifetime = "5d";
95 default_extra_recipient_limit = "5000";
96 line_length_limit = "2048";
97 duplicate_filter_limit = "5000";
98 # Stops mail from poorly written software
99 strict_rfc821_envelopes = true;
100 mime_header_checks = [];
101 milter_header_checks = [];
102 nested_header_checks = [];
103 body_checks = [];
104 content_filter = "";
105 permit_mx_backup_networks = [];
106 propagate_unmatched_extensions = [ "canonical" "virtual" "alias" ];
107 #masquerade_classes = [ "envelope_sender" "header_sender" "header_recipient" ];
108 #masquerade_domains = "";
109 #masquerade_exceptions = "root";
110 queue_minfree = "0";
111 # Stops some techniques used to harvest email addresses
112 disable_vrfy_command = true;
113 enable_long_queue_ids = false;
114 # Useful to test restrictions
115 smtpd_authorized_xclient_hosts = "127.0.0.1";
116 smtpd_banner = "$myhostname ESMTP $mail_name (NixOS)";
117 smtpd_client_connection_count_limit = "50";
118 smtpd_client_connection_rate_limit = "0";
119 smtpd_client_event_limit_exceptions = "$mynetworks";
120 smtpd_client_message_rate_limit = "0";
121 smtpd_client_new_tls_session_rate_limit = "0";
122 smtpd_client_port_logging = false;
123 smtpd_client_recipient_rate_limit = "0";
124 # Ban 5 sec on error
125 smtpd_error_sleep_time = "5";
126 # Needed to enforce reject_unknown_helo_hostname
127 smtpd_helo_required = true;
128 smtpd_helo_restrictions = [
129 "reject_invalid_helo_hostname"
130 "reject_non_fqdn_helo_hostname"
131 # Don't talk to mail systems that don't know their own hostname.
132 "reject_unknown_helo_hostname"
133 "permit"
134 ];
135 smtpd_client_restrictions = [
136 ];
137 # Set in postfix/*.nix and used in submissions/smptd
138 # with reject_sender_login_mismatch
139 smtpd_sender_login_maps = [];
140 smtpd_sender_restrictions = [
141 "reject_non_fqdn_sender"
142 "permit"
143 ];
144 smtpd_reject_unlisted_recipient = true;
145 # Check the RCPT TO, before smtpd_recipient_restrictions
146 # Restrictions based on what is allowed or not,
147 # these are applied before smtpd_recipient_restrictions
148 smtpd_relay_restrictions = [
149 "permit_mynetworks"
150 # Check the recipient's address in virtual_mailbox_domains and virtual_mailbox_maps
151 "permit_auth_destination"
152 # The world is only authorized to use our relay for the above destinations.
153 "reject"
154 ];
155 # Restrictions based on what is working or not
156 smtpd_recipient_restrictions = [
157 # Reject if the domain is not fully qualified
158 "reject_non_fqdn_recipient"
159 # Reject if the domain is not working, even before bothering to check the address
160 "reject_unknown_recipient_domain"
161 # Reject if the address is not working
162 # WARNING: this does not work if the recipient is greylisting.
163 # WARNING: verify(8) has a cache, dumpable if verify(8) is stopped, with:
164 # postmap -s btree:/var/lib/postfix/data/verify_cache
165 #"reject_unverified_recipient"
166 "permit"
167 ];
168 # Trust the verify database
169 #unverified_recipient_reject_code = "550";
170 smtpd_data_restrictions = [
171 # Force the smtpd's client to wait OK before sending
172 "reject_unauth_pipelining"
173 "permit"
174 ];
175 smtpd_end_of_data_restrictions = [
176 # Enforce mail volume quota via policy service callouts.
177 #check_policy_service unix:private/policy
178 ];
179 #smtpd_milters = "";
180 smtpd_peername_lookup = true;
181 smtpd_recipient_limit = "5000";
182 smtpd_recipient_overshoot_limit = "5000";
183 #smtpd_restriction_classes = "";
184 #smtpd_sasl_auth_enable = true;
185 #smtpd_sasl_path = "private/auth";
186 #smtpd_sasl_security_options = "noanonymous";
187 #smtpd_sasl_type = "dovecot";
188 smtpd_starttls_timeout = "300s";
189 #smtpd_tls_always_issue_session_ids = true;
190 #smtpd_tls_CApath = "/etc/postfix/x509/ca/";
191 smtpd_tls_ask_ccert = false;
192 #smtpd_tls_ccert_verifydepth = "5";
193 smtpd_tls_ciphers = "high";
194 smtpd_tls_eecdh_grade = "auto";
195 # Disable weak ciphers as reported by https://ssl-tools.net
196 # https://serverfault.com/questions/744168/how-to-disable-rc4-on-postfix
197 smtpd_tls_exclude_ciphers = [ "ADH" "MD5" "CAMELLIA" "SEED" "3DES" "DES" "RC4" "eNULL" "aNULL" ];
198 smtpd_tls_fingerprint_digest = "sha512";
199 # Log only a summary message on TLS handshake completion
200 smtpd_tls_loglevel = "1";
201 smtpd_tls_mandatory_ciphers = "high";
202 smtpd_tls_mandatory_protocols = [ "!SSLv2" "!SSLv3" ];
203 # Only allow TLSv*
204 smtpd_tls_protocols = [ "!SSLv2" "!SSLv3" ];
205 #smtpd_tls_received_header = false;
206 smtpd_tls_req_ccert = false;
207 # Postfix 2.3 and later
208 # encrypt
209 # Mandatory TLS encryption: announce STARTTLS support to SMTP clients, and require that clients use TLS
210 # encryption. According to [1720]RFC 2487 this MUST NOT be applied in case of a publicly-referenced
211 # SMTP server. Instead, this option should be used only on dedicated servers.
212 smtpd_tls_security_level = "may";
213 smtpd_tls_session_cache_database = "btree:$data_directory/smtpd_tls_session_cache";
214 #smtpd_tls_session_cache_timeout = "3600s";
215 #smtpd_tls_chain_files =
216
217 relayhost = [];
218 #relay_clientcerts = hash:/var/lib/postfix/conf/relay_clientcerts
219 # This is where to put backup MX domains
220 relay_domains = [];
221 relay_recipient_maps = [];
222
223 # Use a non blocking source of randomness
224 tls_random_source = "dev:/dev/urandom";
225 # Map each domain to a specific X.509 certificate
226 tls_server_sni_maps = "hash:/run/keys/postfix-sni";
227
228 # Only explicitely aliased accounts have a mail, not all the passwd
229 local_recipient_maps = "$alias_maps";
230 # Note that the local transport rewrites the envelope recipient
231 # according to the alias_maps, and thus the aliasing is transparent
232 # to the nexthop (eg. dovecot)
233 #local_transport = local:$myhostname
234 # No console bell on new mail
235 biff = false;
236 forward_path = [
237 /*
238 "$home/.forward''${recipient_delimiter}''${extension}"
239 "$home/.forward"
240 */
241 ];
242
243 # Filled by the postfix/*.nix
244 virtual_mailbox_domains = [];
245 # Completed by the postfix/*.nix
246 virtual_mailbox_maps = [
247 "hash:/etc/postfix/virtual"
248 ];
249 virtual_transport = "lmtp:unix:private/dovecot-lmtp";
250 /*
251 dovecot_destination_recipient_limit = "1";
252 virtual_transport = "dovecot";
253 */
254
255 # There is no fallback
256 fallback_transport = "";
257 };
258 virtualMapType = "hash";
259 masterConfig =
260 let
261 mkVal = value:
262 if lib.isList value
263 then concatStringsSep "," value
264 else
265 if value == true then "yes"
266 else if value == false then "no"
267 else toString value;
268 mkKeyVal = opt: val: [ "-o" (opt + "=" + mkVal val) ];
269 mkArgs = args: lib.concatLists (lib.mapAttrsToList mkKeyVal args);
270 in {
271 pickup = {
272 args = mkArgs {
273 cleanup_service_name = "submissions-header-cleanup";
274 };
275 };
276 # Implicit TLS on port 465
277 # https://tools.ietf.org/html/rfc8314#section-3.3
278 submissions = {
279 type = "inet";
280 private = false;
281 command = "smtpd";
282 args = mkArgs {
283 syslog_name = "postfix/submissions";
284 # Implicit TLS, not STARTTLS
285 smtpd_tls_wrappermode = true;
286 smtpd_tls_mandatory_protocols = [
287 "TLSv1.3"
288 # K-9 Mail 5.600 still requires this..
289 "TLSv1.2"
290 ];
291 milter_macro_daemon_name = "ORIGINATING";
292 smtpd_helo_restrictions = [
293 "permit_sasl_authenticated"
294 ] ++ postfix.config.smtpd_helo_restrictions;
295 smtpd_relay_restrictions = [
296 # SASL authorizes to send to the world
297 "permit_sasl_authenticated"
298 "reject"
299 ];
300 smtpd_sasl_auth_enable = true;
301 smtpd_sasl_type = "dovecot";
302 smtpd_sasl_path = "private/auth";
303 smtpd_sasl_local_domain = "";
304 # Offer SASL authentication only after a TLS-encrypted session has been established
305 smtpd_tls_auth_only = true;
306 smtpd_sasl_tls_security_options = [ "noanonymous" ];
307 # Do not put SASL logins in mail headers
308 smtpd_sasl_authenticated_header = false;
309 # Who cares about (old) Outlook
310 broken_sasl_auth_clients = false;
311 smtpd_sender_restrictions = [
312 "reject_non_fqdn_sender"
313 # Check that the SASL user is using only its own
314 # mail addresses on the envelope, as indicated in smtpd_sender_login_maps
315 "reject_sender_login_mismatch"
316 "permit"
317 ];
318 # No X.509 certificates for users, for now
319 smtpd_tls_ask_ccert = false;
320 smtpd_tls_ccert_verifydepth = 0;
321 smtpd_tls_loglevel = 1;
322 smtpd_tls_req_ccert = false;
323 cleanup_service_name = "submissions-header-cleanup";
324 };
325 };
326 submissions-header-cleanup = {
327 type = "unix";
328 private = false;
329 maxproc = 0;
330 command = "cleanup";
331 args = mkArgs {
332 header_checks = "pcre:" + pkgs.writeText "submission_header_cleanup_rules" ''
333 # Removes sensitive headers from mails handed in via the submission or smtps port.
334 # See https://thomas-leister.de/mailserver-debian-stretch/
335 # Uses "pcre" style regex.
336
337 /^Received:/ IGNORE
338 /^User-Agent:/ IGNORE
339 /^X-Enigmail:/ IGNORE
340 /^X-Mailer:/ IGNORE
341 /^X-Originating-IP:/ IGNORE
342 '';
343 };
344 };
345 /*
346 dovecot = {
347 type = "unix";
348 privileged = true;
349 chroot = false;
350 command = "pipe";
351 args = let
352 # rspamd could be used as a milter, but then it cannot apply
353 # its checks "per user" (milter is not yet dispatched to
354 # users), so we wrap dovecot-lda inside rspamc per recipient
355 # here.
356 rspamc_dovecot = pkgs.writeScriptBin "rspamc_dovecot" ''
357 #!${pkgs.stdenv.shell}
358 sender="$1"
359 original_recipient="$2"
360 user="$3"
361 ${pkgs.coreutils}/bin/cat - | \
362 (${pkgs.rspamd}/bin/rspamc -h ${config.myServices.mail.rspamd.sockets.worker-controller} -c bayes -d "$user" --mime || true) | \
363 ${pkgs.dovecot}/libexec/dovecot/dovecot-lda -f "$sender" -a "$original_recipient" -d "$user"
364 '';
365 in [
366 "flags=DRhu" "user=vhost:vhost"
367 "argv=${rspamc_dovecot}/bin/rspamc_dovecot \${sender} \${original_recipient} \${user}@\${nexthop}"
368 ];
369 };
370 */
371 };
372 extraMasterConf = ''
373 #spfcheck unix - n n - 0 spawn
374 # user=policyd-spf argv=/usr/sbin/postfix-policyd-spf-perl
375 # -o smtpd_sender_restrictions=reject_sender_login_mismatch
376 # -o smtpd_sender_login_maps=hash:/etc/postfix/vaccounts
377 # -o cleanup_service_name=submissions-header-cleanup
378 #spfcheck unix - n n - 0 spawn
379 # user=policyd-spf argv=/usr/bin/postfix-policyd-spf-perl
380 #uucp unix - n n - - pipe
381 # flags=Fqhu user=uucp argv=uux -r -n -z -a$sender - $nexthop!rmail ($recipient)
382 #smtp inet n - - - - smtpd
383 # -o cleanup_service_name=pre-cleanup
384 # -o content_filter=amavis:[127.0.0.1]:10024
385 # -o smtpd_sender_restrictions=reject_unauth_pipelining,reject_non_fqdn_sender,permit
386 # -o receive_override_options=no_address_mappings
387 #amavis unix - - n - 2 lmtp
388 # -o lmtp_data_done_timeout=1200
389 # -o lmtp_send_xforward_command=yes
390 # -o lmtp_tls_note_starttls_offer=no
391 #127.0.0.1:10025 inet n - n - - smtpd
392 # -o content_filter=
393 # -o local_header_rewrite_clients=
394 # -o local_recipient_maps=
395 # -o mynetworks=127.0.0.0/8
396 # -o receive_override_options=no_header_body_checks,no_milters,no_unknown_recipient_checks
397 # -o relay_recipient_maps=
398 # -o smtpd_client_connection_count_limit=0
399 # -o smtpd_client_connection_rate_limit=0
400 # -o smtpd_client_restrictions=permit_mynetworks,reject
401 # -o smtpd_data_restrictions=reject_unauth_pipelining
402 # -o smtpd_delay_reject=no
403 # -o smtpd_end_of_data_restrictions=
404 # -o smtpd_error_sleep_time=0
405 # -o smtpd_hard_error_limit=1000
406 # -o smtpd_helo_restrictions=
407 # -o smtpd_milters=
408 # -o smtpd_recipient_restrictions=permit_mynetworks,reject
409 # -o smtpd_restriction_classes=
410 # -o smtpd_sender_restrictions=
411 # -o smtpd_soft_error_limit=1001
412 # -o strict_rfc821_envelopes=yes
413 #submission inet n - - - - smtpd
414 # -o cleanup_service_name=pre-cleanup
415 # -o content_filter=amavis:[127.0.0.1]:10024
416 # -o milter_macro_daemon_name=ORIGINATING
417 # -o receive_override_options=no_address_mappings
418 # -o smtpd_sender_restrictions=permit_tls_clientcerts,reject
419 # -o smtpd_tls_ask_ccert=yes
420 # -o smtpd_tls_auth_only=yes
421 # -o smtpd_tls_ccert_verifydepth=2
422 # -o smtpd_tls_loglevel=1
423 # -o smtpd_tls_req_ccert=yes
424 # -o smtpd_tls_security_level=encrypt
425 #smtps inet n - - - - smtpd
426 # -o milter_macro_daemon_name=ORIGINATING
427 # -o smtpd_client_restrictions=permit_sasl_authenticated,reject
428 # -o smtpd_sasl_auth_enable=yes
429 # -o smtpd_tls_ask_ccert=yes
430 # -o smtpd_tls_auth_only=yes
431 # -o smtpd_tls_ccert_verifydepth=0
432 # -o smtpd_tls_loglevel=1
433 # -o smtpd_tls_req_ccert=no
434 # -o smtpd_tls_security_level=encrypt
435 # -o smtpd_tls_wrappermode=yes
436 #pickup fifo n - - 60 1 pickup
437 # -o cleanup_service_name=pre-cleanup
438 # -o content_filter=amavis:[127.0.0.1]:10024
439 #pre-cleanup unix n - - - 0 cleanup
440 # -o virtual_alias_maps=
441 #cleanup unix n - - - 0 cleanup
442 # -o mime_header_checks=
443 # -o nested_header_checks=
444 # -o body_checks=
445 # -o header_checks=
446 #-- SYMPA begin
447 #sympa unix - n n - - pipe
448 # flags=R user=sympa argv=/usr/lib/sympa/bin/queue ''${recipient}
449 #sympabounce unix - n n - - pipe
450 # flags=R user=sympa argv=/usr/lib/sympa/bin/bouncequeue ''${recipient}
451 #-- SYMPA end
452 '';
453 #noclue unix - n n - - pipe
454 # flags=q user=noclue argv=/usr/local/bin/noclue-delivery ${recipient} ${sender}
455 };
456 };
457 }