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