]> Git — Sourcephile - sourcephile-nix.git/blob - servers/mermet/postfix.nix
nix: fix NIX_PATH
[sourcephile-nix.git] / servers / mermet / postfix.nix
1 { pkgs, lib, config, ... }:
2 let
3 inherit (builtins) attrNames readFile toPath;
4 inherit (builtins.extraBuiltins) pass;
5 inherit (lib) types;
6 inherit (pkgs.lib) loadFile unlines unwords unlinesAttrs;
7 inherit (config) networking;
8 inherit (config.services) postfix dovecot2 openldap;
9
10 submissionHeaderCleanupRules = pkgs.writeText "submission_header_cleanup_rules" ''
11 # Removes sensitive headers from mails handed in via the submission or smtps port.
12 # See https://thomas-leister.de/mailserver-debian-stretch/
13 # Uses "pcre" style regex.
14
15 /^Received:/ IGNORE
16 /^User-Agent:/ IGNORE
17 /^X-Enigmail:/ IGNORE
18 /^X-Mailer:/ IGNORE
19 /^X-Originating-IP:/ IGNORE
20 '';
21 in
22 {
23 systemd.services.postfix.after = [
24 "openldap.service"
25 "${networking.domainBase}.key.pem-key.service"
26 #"nginx.service" # letsencrypt
27 ];
28 deployment.keys = {
29 "postfix.${networking.domainBase}.key.pem" = {
30 text = pass "x509/${networking.domainBase}/key.pem";
31 user = postfix.user;
32 group = "root";
33 destDir = "/run/keys/";
34 permissions = "0400"; # WARNING: not enforced when deployment.storeKeysOnMachine = true
35 };
36 };
37 services.postfix = {
38 enable = true;
39 #hostname = networking.domain;
40 #domain = "localdomain";
41 networksStyle = "host";
42 #mapFiles."valias" = toFile "valias" (unlines (all_valiases_postfix ++ catchAllPostfix));
43 # See https://blog.grimneko.de/2011/12/24/a-bunch-of-tips-for-improving-your-postfix-setup/
44 # for details on how this file looks. By using the same file as valias,
45 # every alias is uniquely owned by its user.
46 # The user's own address is already in all_valiases_postfix.
47 #mapFiles."vaccounts" = toFile "vaccounts" (unlines all_valiases_postfix);
48 mapFiles."virtual_alias_maps" = pkgs.writeText "virtual_alias_maps"
49 (unlinesAttrs
50 (from: to: "${from} ${unwords to}")
51 postfix.aliases);
52 mapFiles."ldap-virtual_alias_maps.cf" = pkgs.writeText "ldap-virtual_alias_maps.cf" ''
53 version = 3
54 debuglevel = 0
55 server_host = ldapi://
56 bind = sasl
57 sasl_mechs = EXTERNAL
58 search_base = ou=posix,${openldap.domainSuffix}
59 scope = sub
60 dereference = 0
61 query_filter = (&(mailAlias=%s)(mailEnabled=TRUE))
62 result_format = %s
63 result_attribute = mail
64 '';
65 mapFiles."ldap-forward.cf" = pkgs.writeText "ldap-forward.cf" ''
66 version = 3
67 debuglevel = 0
68 server_host = ldapi://
69 bind = sasl
70 sasl_mechs = EXTERNAL
71 search_base = ou=posix,${openldap.domainSuffix}
72 scope = sub
73 dereference = 0
74 query_filter = (&(mail=%s)(mailEnabled=TRUE))
75 result_format = %s
76 result_attribute = mailForwardingAddress
77 '';
78 sslCert = loadFile (../../../sec/openssl + "/${networking.domainBase}/cert.self-signed.pem");
79 sslKey = "/run/keys/postfix.${networking.domainBase}.key.pem";
80 #enableSubmission = true;
81 #enableSmtp = true;
82 destination = [
83 "localhost"
84 "localhost.localdomain"
85 networking.hostName
86 "${networking.hostName}.localdomain"
87 ];
88 networks = [
89 "127.0.0.0/8"
90 "[::1]/128"
91 ];
92 recipientDelimiter = "+";
93 config = {
94 # Appending .domain is the MUA's job
95 append_dot_mydomain = false;
96 # No console bell on new mail
97 biff = false;
98 body_checks = "";
99 #content_filter = "amavisfeed:[127.0.0.1]:10024";
100 #debug_peer_level = 4;
101 #debug_peer_list = ".$myhostname";
102 default_extra_recipient_limit = "5000";
103 # Uncomment the next line to generate "delayed mail" warnings
104 #delay_warning_time = "4h";
105 # Stops some techniques used to harvest email addresses
106 disable_vrfy_command = true;
107 duplicate_filter_limit = "5000";
108 enable_long_queue_ids = false;
109 # Pass unexisting $mydestination recipients to dovecot
110 fallback_transport = "lmtp:unix:private/dovecot-lmtp";
111 forward_path = [
112 ''$home/.forward''${recipient_delimiter}''${extension}''
113 "$home/.forward"
114 ];
115 #header_checks = "regexp:/var/lib/postfix/conf/header_checks";
116 #inet_interfaces = "all";
117 line_length_limit = "2048";
118
119 # Let $fallback_transport check existence of recipients
120 local_recipient_maps = "";
121 #mail_spool_directory = "/var/spool/mail";
122 # NOTE: nixpkgs's default
123 #local_header_rewrite_clients = "";
124 #home_mailbox = "Maildir/";
125 #mailbox_command = ''
126 # ${pkgs.procmail}/bin/procmail -t -a "$SENDER" -a "$RECIPIENT" -a "$USER" -a "$EXTENSION" -a "$DOMAIN" -a "$ORIGINAL_RECIPIENT" "$HOME/.procmailrc"
127 #'';
128 mailbox_size_limit = "204800000";
129
130 masquerade_classes = [ "envelope_sender" "header_sender" "header_recipient" ];
131 masquerade_domains = "";
132 masquerade_exceptions = "root";
133 maximal_queue_lifetime = "5d";
134 message_size_limit = "20480000";
135 mime_header_checks = "";
136 milter_header_checks = "";
137 nested_header_checks = "";
138 #non_smtpd_milters = "";
139 parent_domain_matches_subdomains = [
140 #"debug_peer_list"
141 #"fast_flush_domains"
142 #"mynetworks"
143 #"permit_mx_backup_networks"
144 #"qmqpd_authorized_clients"
145 #"smtpd_access_maps"
146 ];
147 permit_mx_backup_networks = "";
148 #policy-spf_time_limit = "3600s";
149 propagate_unmatched_extensions = [ "canonical" "virtual" "alias" ];
150 queue_minfree = "0";
151 #receive_override_options = "no_address_mappings";
152 # no_unknown_recipient_checks
153 # Do not try to reject unknown recipients (SMTP server only).
154 # This is typically specified AFTER an external content filter.
155 # no_address_mappings
156 # Disable canonical address mapping, virtual alias map expansion,
157 # address masquerading, and automatic BCC (blind carbon-copy) recipients.
158 # This is typically specified BEFORE an external content filter (eg. amavis).
159 # no_header_body_checks
160 # Disable header/body_checks. This is typically specified AFTER
161 # an external content filter.
162 # no_milters
163 # Disable Milter (mail filter) applications.
164 # This is typically specified AFTER an external content filter.
165 # Parse the extension in email address, eg. contact+extension@
166 relayhost = "";
167 #relay_clientcerts = hash:/var/lib/postfix/conf/relay_clientcerts
168 # This is where to put backup MX domains
169 relay_domains = "$mydestination";
170 relay_recipient_maps = "";
171 smtp_body_checks = "";
172 #smtp_cname_overrides_servername = false;
173 smtp_connect_timeout = "60s";
174 #smtp_header_checks = "regexp:/var/lib/postfix/smtp_header_checks";
175 smtp_mime_header_checks = "";
176 smtp_nested_header_checks = "";
177 smtp_tls_exclude_ciphers = [ "ADH" "MD5" "CAMELLIA" "SEED" "3DES" "DES" "RC4" "eNULL" "aNULL" ];
178 #smtp_tls_fingerprint_digest = "sha1";
179 smtp_tls_loglevel = "1";
180 #smtp_tls_note_starttls_offer = true;
181 #smtp_tls_policy_maps = "hash:/var/lib/postfix/conf/tls_policy";
182 # Only allow TLSv* protocols
183 smtp_tls_protocols = [ "!SSLv2" "!SSLv3" ];
184 smtp_tls_scert_verifydepth = "5";
185 #smtp_tls_secure_cert_match = [ "nexthop" "dot-nexthop" ];
186 smtp_tls_security_level = "may";
187 smtp_tls_session_cache_database = "btree:$data_directory/smtp_tls_session_cache";
188 #smtp_tls_session_cache_timeout = "3600s";
189 #smtp_tls_verify_cert_match = "hostname";
190 # Useful to test restrictions
191 smtpd_authorized_xclient_hosts = "127.0.0.1";
192 smtpd_banner = "${networking.hostName} ESMTP $mail_name (NixOS)";
193 smtpd_client_connection_count_limit = "50";
194 smtpd_client_connection_rate_limit = "0";
195 smtpd_client_event_limit_exceptions = "$mynetworks";
196 smtpd_client_message_rate_limit = "0";
197 smtpd_client_new_tls_session_rate_limit = "0";
198 smtpd_client_port_logging = false;
199 smtpd_client_recipient_rate_limit = "0";
200 smtpd_client_restrictions = [
201 #"check_client_access hash:/var/lib/postfix/conf/client_blacklist"
202 ];
203 smtpd_data_restrictions = [
204 "reject_unauth_pipelining"
205 # Force the smtp client to wait OK before sending
206 "permit"
207 ];
208 # Disable opportunistic encryption
209 smtpd_discard_ehlo_keywords = "starttls";
210 #smtpd_end_of_data_restrictions = "";
211 # Ban 5 sec on error
212 smtpd_error_sleep_time = "5";
213 smtpd_helo_required = true;
214 smtpd_helo_restrictions = [
215 "reject_invalid_helo_hostname"
216 "reject_non_fqdn_helo_hostname"
217 #"reject_unknown_helo_hostname"
218 # May be useful to fight spam
219 "permit"
220 ];
221 #smtpd_milters = "";
222 smtpd_peername_lookup = true;
223 smtpd_recipient_limit = "5000";
224 smtpd_recipient_overshoot_limit = "5000";
225 smtpd_recipient_restrictions = [
226 "reject_non_fqdn_recipient"
227 #"reject_invalid_hostname"
228 "reject_unknown_recipient_domain"
229 #"reject_non_fqdn_sender"
230 "reject_unauth_pipelining"
231 #"check_policy_service inet:localhost:12340"
232 # check quota
233 "permit_mynetworks"
234 #"permit_tls_clientcerts"
235 "permit_sasl_authenticated"
236 "reject_unverified_recipient"
237 # $fallback_transport is responsible of checking the existence of the recipient
238 # WARNING: verify(8) has a cache, dumpable if verify(8) is stopped, with:
239 # postmap -s btree:/var/lib/postfix/data/verify_cache
240 # Bypass SPF check and postgrey if the recipient is not for us or someone in backup_mx
241 "reject_unauth_destination"
242 # Check SPF
243 #"check_policy_service unix:private/spfcheck"
244 # Greylisting using postgrey
245 #"check_policy_service unix:${postgrey.socket.path}"
246 "permit_auth_destination"
247 "reject"
248 #"reject_unknown_sender_domain"
249 # Maybe better in smtpd_sender_restrictions
250 #"reject_rbl_client bl.spamcop.net"
251 #"reject_rbl_client list.dsbl.org"
252 #"reject_rbl_client zen.spamhaus.org"
253 #"reject_rbl_client dnsbl.sorbs.net"
254 ];
255 smtpd_relay_restrictions = [
256 "permit_mynetworks"
257 "permit_sasl_authenticated"
258 # NOTE: permit auth through dovecot's SASL
259 "reject_unauth_destination"
260 ];
261 #smtpd_restriction_classes = "";
262 broken_sasl_auth_clients = false;
263 #smtpd_sasl_auth_enable = true;
264 #smtpd_sasl_path = "private/auth";
265 #smtpd_sasl_security_options = "noanonymous";
266 #smtpd_sasl_type = "dovecot";
267 smtpd_sender_restrictions = [
268 "permit_mynetworks"
269 #"permit_tls_clientcerts"
270 "permit_sasl_authenticated"
271 # NOTE: permit auth through dovecot's SASL
272 #"check_sender_access hash:/var/lib/postfix/conf/sender_access"
273 "reject_unauth_pipelining"
274 "reject_non_fqdn_sender"
275 #"reject_sender_login_mismatch"
276 #"reject_unknown_sender_domain"
277 "permit"
278 ];
279 smtpd_starttls_timeout = "300s";
280 #smtpd_tls_always_issue_session_ids = true;
281 # No SASL AUTH without TLS
282 smtpd_tls_auth_only = true;
283 #smtpd_tls_CApath = "/etc/postfix/x509/ca/";
284 smtpd_tls_ask_ccert = false;
285 #smtpd_tls_ccert_verifydepth = "5";
286 smtpd_tls_ciphers = "high";
287 smtpd_tls_eecdh_grade = "ultra";
288 # Disable weak ciphers as reported by https://ssl-tools.net
289 # https://serverfault.com/questions/744168/how-to-disable-rc4-on-postfix
290 smtpd_tls_exclude_ciphers = [ "RC4" "aNULL" ];
291 smtpd_tls_fingerprint_digest = "sha512";
292 # Log only a summary message on TLS handshake completion
293 smtpd_tls_loglevel = "1";
294 smtpd_tls_mandatory_ciphers = "high";
295 smtpd_tls_mandatory_protocols = "TLSv1"; # FIXME: TLSv1.3
296 # Only allow TLSv*
297 smtpd_tls_protocols = [ "!SSLv2" "!SSLv3" ];
298 #smtpd_tls_received_header = false;
299 smtpd_tls_req_ccert = false;
300 # Postfix 2.3 and later
301 # encrypt
302 # Mandatory TLS encryption: announce STARTTLS support to SMTP clients, and require that clients use TLS
303 # encryption. According to [1720]RFC 2487 this MUST NOT be applied in case of a publicly-referenced
304 # SMTP server. Instead, this option should be used only on dedicated servers.
305 smtpd_tls_security_level = "may";
306 smtpd_tls_session_cache_database = "btree:$data_directory/smtpd_tls_session_cache";
307 #smtpd_tls_session_cache_timeout = "3600s";
308 # Stops mail from poorly written software
309 strict_rfc821_envelopes = true;
310 #sympa_destination_recipient_limit = "1";
311 #sympabounce_destination_recipient_limit = "1";
312 # postconf(5) discourages to change this
313 #tls_high_cipherlist = "AES256-SHA";
314 #tls_random_bytes = "32";
315 # Must not be in a chroot
316 #tls_random_exchange_name = "$data_directory/prng_exch";
317 #tls_random_prng_update_period = "3600s";
318 #tls_random_reseed_period = "3600s";
319 # Use a non blocking source of randomness
320 tls_random_source = "dev:/dev/urandom";
321 transport_maps = [
322 #"ldap:transport"
323 #"hash:/etc/postfix/transport-dovecot"
324 #"hash:/etc/postfix/$mydomain/transport"
325 #"hash:/etc/dovecot/transport"
326 #"regexp:/etc/sympa/transport"
327 ];
328 # Rejects immediately what $fallback_transport rejects
329 unverified_recipient_reject_code = "550";
330 # Do not specify virtual alias domain names in mydestination
331 # or relay_domains configuration parameters
332 #
333 # With a virtual alias domain, the Postfix SMTP server
334 # accepts mail for known-user@virtual-alias.domain, and
335 # rejects mail for unknown-user@virtual-alias.domain as
336 # undeliverable.
337 virtual_alias_domains = [];
338 virtual_alias_maps = [
339 #"hash:/etc/postfix/virtual_alias_maps"
340 #"hash:/etc/postfix/virtual_domain_alias_maps"
341 "ldap:/etc/postfix/ldap-forward.cf"
342 "ldap:/etc/postfix/ldap-virtual_alias_maps.cf"
343 #"hash:/etc/postfix/virtual_alias-dovecot"
344 #"hash:/var/lib/postfix/conf/valias"
345 #"regexp:/etc/sympa/virtual_alias"
346 ];
347 #virtual_uid_maps = "static:5000";
348 #virtual_gid_maps = "static:5000";
349 #virtual_mailbox_base = dovecot2.mailDir;
350 virtual_mailbox_domains = [ networking.domain ] ++ networking.domainAliases;
351 #virtual_mailbox_maps = "hash:/etc/postfix/virtual_mailbox_maps";
352 virtual_transport = "lmtp:unix:private/dovecot-lmtp";
353 };
354 #submissionOptions = {
355 # smtpd_tls_security_level = "encrypt";
356 # smtpd_sasl_auth_enable = "yes";
357 # smtpd_sasl_type = "dovecot";
358 # smtpd_sasl_path = "private/auth";
359 # smtpd_sasl_security_options = "noanonymous";
360 # smtpd_sasl_local_domain = "$myhostname";
361 # smtpd_client_restrictions = "permit_sasl_authenticated,reject";
362 # smtpd_sender_login_maps = "hash:/etc/postfix/vaccounts";
363 # smtpd_sender_restrictions = "reject_sender_login_mismatch";
364 # smtpd_recipient_restrictions = "reject_non_fqdn_recipient,reject_unknown_recipient_domain,permit_sasl_authenticated,reject";
365 # cleanup_service_name = "submission-header-cleanup";
366 #};
367 extraMasterConf = ''
368 #spfcheck unix - n n - 0 spawn
369 # user=policyd-spf argv=/usr/sbin/postfix-policyd-spf-perl
370 465 inet n - - - - smtpd
371 -o milter_macro_daemon_name=ORIGINATING
372 -o smtpd_client_restrictions=permit_sasl_authenticated,reject
373 -o smtpd_recipient_restrictions=reject_non_fqdn_recipient,reject_unknown_recipient_domain,permit_sasl_authenticated,reject
374 -o smtpd_sasl_auth_enable=yes
375 -o smtpd_sasl_local_domain=$myhostname
376 -o smtpd_sasl_path=private/auth
377 -o smtpd_sasl_security_options=noanonymous
378 -o smtpd_sasl_type=dovecot
379 -o smtpd_tls_ask_ccert=no
380 -o smtpd_tls_auth_only=yes
381 -o smtpd_tls_ccert_verifydepth=0
382 -o smtpd_tls_loglevel=1
383 -o smtpd_tls_req_ccert=no
384 -o smtpd_tls_security_level=encrypt
385 -o smtpd_tls_wrappermode=yes
386 # -o smtpd_sender_restrictions=reject_sender_login_mismatch
387 # -o smtpd_sender_login_maps=hash:/etc/postfix/vaccounts
388 # -o cleanup_service_name=submission-header-cleanup
389 submission-header-cleanup unix n - n - 0 cleanup
390 -o header_checks=pcre:${submissionHeaderCleanupRules}
391 #spfcheck unix - n n - 0 spawn
392 # user=policyd-spf argv=/usr/bin/postfix-policyd-spf-perl
393 #uucp unix - n n - - pipe
394 # flags=Fqhu user=uucp argv=uux -r -n -z -a$sender - $nexthop!rmail ($recipient)
395 #smtp inet n - - - - smtpd
396 # -o cleanup_service_name=pre-cleanup
397 # -o content_filter=amavis:[127.0.0.1]:10024
398 # -o smtpd_sender_restrictions=reject_unauth_pipelining,reject_non_fqdn_sender,permit
399 # -o receive_override_options=no_address_mappings
400 #amavis unix - - n - 2 lmtp
401 # -o lmtp_data_done_timeout=1200
402 # -o lmtp_send_xforward_command=yes
403 # -o lmtp_tls_note_starttls_offer=no
404 #127.0.0.1:10025 inet n - n - - smtpd
405 # -o content_filter=
406 # -o local_header_rewrite_clients=
407 # -o local_recipient_maps=
408 # -o mynetworks=127.0.0.0/8
409 # -o receive_override_options=no_header_body_checks,no_milters,no_unknown_recipient_checks
410 # -o relay_recipient_maps=
411 # -o smtpd_client_connection_count_limit=0
412 # -o smtpd_client_connection_rate_limit=0
413 # -o smtpd_client_restrictions=permit_mynetworks,reject
414 # -o smtpd_data_restrictions=reject_unauth_pipelining
415 # -o smtpd_delay_reject=no
416 # -o smtpd_end_of_data_restrictions=
417 # -o smtpd_error_sleep_time=0
418 # -o smtpd_hard_error_limit=1000
419 # -o smtpd_helo_restrictions=
420 # -o smtpd_milters=
421 # -o smtpd_recipient_restrictions=permit_mynetworks,reject
422 # -o smtpd_restriction_classes=
423 # -o smtpd_sender_restrictions=
424 # -o smtpd_soft_error_limit=1001
425 # -o strict_rfc821_envelopes=yes
426 #submission inet n - - - - smtpd
427 # -o cleanup_service_name=pre-cleanup
428 # -o content_filter=amavis:[127.0.0.1]:10024
429 # -o milter_macro_daemon_name=ORIGINATING
430 # -o receive_override_options=no_address_mappings
431 # -o smtpd_sender_restrictions=permit_tls_clientcerts,reject
432 # -o smtpd_tls_ask_ccert=yes
433 # -o smtpd_tls_auth_only=yes
434 # -o smtpd_tls_ccert_verifydepth=2
435 # -o smtpd_tls_loglevel=1
436 # -o smtpd_tls_req_ccert=yes
437 # -o smtpd_tls_security_level=encrypt
438 #smtps inet n - - - - smtpd
439 # -o milter_macro_daemon_name=ORIGINATING
440 # -o smtpd_client_restrictions=permit_sasl_authenticated,reject
441 # -o smtpd_sasl_auth_enable=yes
442 # -o smtpd_tls_ask_ccert=yes
443 # -o smtpd_tls_auth_only=yes
444 # -o smtpd_tls_ccert_verifydepth=0
445 # -o smtpd_tls_loglevel=1
446 # -o smtpd_tls_req_ccert=no
447 # -o smtpd_tls_security_level=encrypt
448 # -o smtpd_tls_wrappermode=yes
449 #pickup fifo n - - 60 1 pickup
450 # -o cleanup_service_name=pre-cleanup
451 # -o content_filter=amavis:[127.0.0.1]:10024
452 #pre-cleanup unix n - - - 0 cleanup
453 # -o virtual_alias_maps=
454 #cleanup unix n - - - 0 cleanup
455 # -o mime_header_checks=
456 # -o nested_header_checks=
457 # -o body_checks=
458 # -o header_checks=
459 #-- SYMPA begin
460 #sympa unix - n n - - pipe
461 # flags=R user=sympa argv=/usr/lib/sympa/bin/queue ''${recipient}
462 #sympabounce unix - n n - - pipe
463 # flags=R user=sympa argv=/usr/lib/sympa/bin/bouncequeue ''${recipient}
464 #-- SYMPA end
465 '';
466 #noclue unix - n n - - pipe
467 # flags=q user=noclue argv=/usr/local/bin/noclue-delivery ${recipient} ${sender}
468 };
469 }