]> Git — Sourcephile - sourcephile-nix.git/blob - install/logical/friot/postfix.nix
postfix: prepare LDAP config, need Internet to recompile postfix withLDAP=true.
[sourcephile-nix.git] / install / logical / friot / postfix.nix
1 {pkgs, lib, config, nodes, ...}:
2 let inherit (builtins) attrNames toFile;
3 inherit (lib) types;
4 inherit (config.services) x509 postfix dovecot2 postgrey openldap;
5 unlines = lib.concatStringsSep "\n";
6 unwords = lib.concatStringsSep " ";
7 when = x: y: if x == null then "" else y;
8
9 submissionHeaderCleanupRules = pkgs.writeText "submission_header_cleanup_rules" ''
10 # Removes sensitive headers from mails handed in via the submission or smtps port.
11 # See https://thomas-leister.de/mailserver-debian-stretch/
12 # Uses "pcre" style regex.
13
14 /^Received:/ IGNORE
15 /^User-Agent:/ IGNORE
16 /^X-Enigmail:/ IGNORE
17 /^X-Mailer:/ IGNORE
18 /^X-Originating-IP:/ IGNORE
19 '';
20 in
21 {
22 options.services.postfix.aliases = lib.mkOption {
23 type = with types; attrsOf (listOf str);
24 default = {};
25 example = { "root@${config.networking.domain}" = [
26 "user1@${config.networking.domain}"
27 "user2@${config.networking.domain}"
28 ];
29 "@example.coop" = ["user1@${config.networking.domain}"];
30 };
31 };
32 config = {
33 systemd.services.postfix.after =
34 if x509.scheme == "letsencrypt"
35 then [ "nginx.service" ] # XXX: not sure if this is enough
36 else [];
37 services.postfix = {
38 enable = true;
39 #hostname = config.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" =
49 toFile "virtual_alias_maps"
50 (unlines
51 (lib.mapAttrsToList
52 (dom: {accounts, ...}:
53 unlines
54 (lib.mapAttrsToList
55 (user: acct:
56 unlines
57 (map (from: "${from} ${user}@${dom}")
58 acct.aliases))
59 accounts))
60 dovecot2.domains)
61 + unlines (lib.mapAttrsToList
62 (from: to: "${from} ${unwords to}")
63 postfix.aliases));
64 mapFiles."ldap-virtual_alias_maps.cf" =
65 toFile "ldap-virtual_alias_maps.cf" ''
66 version = 3
67 debuglevel = 3
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))
75 result_format = %s
76 result_attribute = mailAlias
77 '';
78 sslCert = x509.cert;
79 sslKey = x509.key;
80 #enableSubmission = true;
81 #enableSmtp = true;
82 destination = [
83 "localhost"
84 "localhost.localdomain"
85 config.networking.hostName
86 "${config.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 = "${config.networking.fqdn} 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 # Needed by postgrey
223 smtpd_peername_lookup = true;
224 smtpd_recipient_limit = "5000";
225 smtpd_recipient_overshoot_limit = "5000";
226 smtpd_recipient_restrictions = [
227 "reject_non_fqdn_recipient"
228 #"reject_invalid_hostname"
229 "reject_unknown_recipient_domain"
230 #"reject_non_fqdn_sender"
231 "reject_unauth_pipelining"
232 #"check_policy_service inet:localhost:12340"
233 # check quota
234 "permit_mynetworks"
235 "permit_tls_clientcerts"
236 "permit_sasl_authenticated"
237 "reject_unverified_recipient"
238 # $fallback_transport is responsible of checking the existence of the recipient
239 # WARNING: verify(8) has a cache, dumpable if verify(8) is stopped, with:
240 # postmap -s btree:/var/lib/postfix/data/verify_cache
241 # Bypass SPF check and postgrey if the recipient is not for us or someone in backup_mx
242 "reject_unauth_destination"
243 # Check SPF
244 #"check_policy_service unix:private/spfcheck"
245 # Greylisting using postgrey
246 "check_policy_service unix:${postgrey.socket.path}"
247 # Once postgrey passed, permit what is for us
248 "permit_auth_destination"
249 "reject"
250 #"reject_unknown_sender_domain"
251 # Maybe better in smtpd_sender_restrictions
252 #"reject_rbl_client bl.spamcop.net"
253 #"reject_rbl_client list.dsbl.org"
254 #"reject_rbl_client zen.spamhaus.org"
255 #"reject_rbl_client dnsbl.sorbs.net"
256 ];
257 smtpd_relay_restrictions = [
258 "permit_mynetworks"
259 "permit_sasl_authenticated"
260 # NOTE: permit auth through dovecot's SASL
261 "reject_unauth_destination"
262 ];
263 #smtpd_restriction_classes = "";
264 broken_sasl_auth_clients = false;
265 #smtpd_sasl_auth_enable = true;
266 #smtpd_sasl_path = "private/auth";
267 #smtpd_sasl_security_options = "noanonymous";
268 #smtpd_sasl_type = "dovecot";
269 smtpd_sender_restrictions = [
270 "permit_mynetworks"
271 "permit_tls_clientcerts"
272 "permit_sasl_authenticated"
273 # NOTE: permit auth through dovecot's SASL
274 #"check_sender_access hash:/var/lib/postfix/conf/sender_access"
275 "reject_unauth_pipelining"
276 "reject_non_fqdn_sender"
277 #"reject_sender_login_mismatch"
278 #"reject_unknown_sender_domain"
279 "permit"
280 ];
281 smtpd_starttls_timeout = "300s";
282 #smtpd_tls_always_issue_session_ids = true;
283 # No SASL AUTH without TLS
284 smtpd_tls_auth_only = true;
285 #smtpd_tls_CApath = "/etc/postfix/x509/ca/";
286 smtpd_tls_ask_ccert = false;
287 #smtpd_tls_ccert_verifydepth = "5";
288 smtpd_tls_ciphers = "high";
289 smtpd_tls_eecdh_grade = "ultra";
290 # Disable weak ciphers as reported by https://ssl-tools.net
291 # https://serverfault.com/questions/744168/how-to-disable-rc4-on-postfix
292 smtpd_tls_exclude_ciphers = [ "RC4" "aNULL" ];
293 smtpd_tls_fingerprint_digest = "sha512";
294 # Log only a summary message on TLS handshake completion
295 smtpd_tls_loglevel = "1";
296 smtpd_tls_mandatory_ciphers = "high";
297 smtpd_tls_mandatory_protocols = "TLSv1";
298 # Only allow TLSv*
299 smtpd_tls_protocols = [ "!SSLv2" "!SSLv3" ];
300 #smtpd_tls_received_header = false;
301 smtpd_tls_req_ccert = false;
302 # Postfix 2.3 and later
303 # encrypt
304 # Mandatory TLS encryption: announce STARTTLS support to SMTP clients, and require that clients use TLS
305 # encryption. According to [1720]RFC 2487 this MUST NOT be applied in case of a publicly-referenced
306 # SMTP server. Instead, this option should be used only on dedicated servers.
307 smtpd_tls_security_level = "may";
308 smtpd_tls_session_cache_database = "btree:$data_directory/smtpd_tls_session_cache";
309 #smtpd_tls_session_cache_timeout = "3600s";
310 # Stops mail from poorly written software
311 strict_rfc821_envelopes = true;
312 #sympa_destination_recipient_limit = "1";
313 #sympabounce_destination_recipient_limit = "1";
314 # postconf(5) discourages to change this
315 #tls_high_cipherlist = "AES256-SHA";
316 #tls_random_bytes = "32";
317 # Must not be in a chroot
318 #tls_random_exchange_name = "$data_directory/prng_exch";
319 #tls_random_prng_update_period = "3600s";
320 #tls_random_reseed_period = "3600s";
321 # Use a non blocking source of randomness
322 tls_random_source = "dev:/dev/urandom";
323 transport_maps = [
324 #"ldap:transport"
325 #"hash:/etc/postfix/transport-dovecot"
326 #"hash:/etc/postfix/$mydomain/transport"
327 #"hash:/etc/dovecot/transport"
328 #"regexp:/etc/sympa/transport"
329 ];
330 # Rejects immediately what $fallback_transport rejects
331 unverified_recipient_reject_code = "550";
332 # Do not specify virtual alias domain names in mydestination
333 # or relay_domains configuration parameters
334 #
335 # With a virtual alias domain, the Postfix SMTP server
336 # accepts mail for known-user@virtual-alias.domain, and
337 # rejects mail for unknown-user@virtual-alias.domain as
338 # undeliverable.
339 virtual_alias_domains = [];
340 virtual_alias_maps = [
341 "hash:/etc/postfix/virtual_alias_maps"
342 #TODO: "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 = toFile "virtual_mailbox_domains" (unlines (attrNames dovecot2.domains));
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 };
470 }