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