]> Git — Sourcephile - sourcephile-nix.git/blob - hosts/mermet/dovecot.nix
apcupsd: enable on losurdo
[sourcephile-nix.git] / hosts / mermet / dovecot.nix
1 { inputs, pkgs, lib, config, system, host, ... }:
2 let
3 inherit (builtins) toString toFile readFile;
4 inherit (lib) types;
5 inherit (pkgs.lib) loadFile unlines unlinesAttrs unlinesValues unwords;
6 inherit (config) networking;
7 inherit (config.services) dovecot2 postfix openldap;
8
9 # NOTE: nixpkgs' dovecot2.stateDir is currently not exported
10 stateDir = "/var/lib/dovecot";
11
12 sieve_pipe_bin_dir = pkgs.buildEnv {
13 name = "sieve_pipe_bin_dir";
14 pathsToLink = [ "/bin" ];
15 paths = [
16 learn-spam
17 learn-ham
18 ];
19 };
20 learn-spam = pkgs.writeShellScriptBin "learn-spam.sh" ''
21 exec ${pkgs.rspamd}/bin/rspamc -h /run/rspamd/learner.sock learn_spam
22 '';
23 learn-ham = pkgs.writeShellScriptBin "learn-ham.sh" ''
24 exec ${pkgs.rspamd}/bin/rspamc -h /run/rspamd/learner.sock learn_ham
25 '';
26
27 dovecot-virtual = pkgs.buildEnv {
28 name = "dovecot-virtual";
29 pathsToLink = [ "/" ];
30 paths = [
31 dovecot-virtual-all
32 dovecot-virtual-recents
33 ];
34 };
35 dovecot-virtual-all = pkgs.writeTextFile {
36 name = "dovecot-virtual-all";
37 destination = "/All/dovecot-virtual";
38 text = ''
39 *
40 all
41 '';
42 };
43 dovecot-virtual-recents = pkgs.writeTextFile {
44 name = "dovecot-virtual-recents";
45 destination = "/Recents/dovecot-virtual";
46 text = ''
47 *
48 all younger 172800
49 '';
50 };
51 dovecot-virtual-pop3 = pkgs.writeTextFile {
52 name = "dovecot-virtual-pop3";
53 destination = "/pop3/INBOX/dovecot-virtual";
54 text = ''
55 All
56 All+*
57 all
58 '';
59 };
60 in
61 {
62 imports = [
63 dovecot/sourcephile.fr.nix
64 dovecot/autogeree.net.nix
65 ];
66 users.groups.acme.members = [ dovecot2.user ];
67 systemd.services.dovecot2 = {
68 after = [
69 "postfix.service"
70 "openldap.service"
71 ];
72 /*
73 preStart = ''
74 # SEE: http://wiki2.dovecot.org/SharedMailboxes/Permissions
75 install -D -d -m 0771 \
76 -o "${dovecot2.user}" \
77 -g "${dovecot2.group}" \
78 ${stateDir}/mail
79 '';
80 */
81 serviceConfig = {
82 #ExecStart = lib.mkForce "${pkgs.utillinux}/bin/setarch x86_64 --addr-no-randomize /bin/sh -c 'LD_PRELOAD=${pkgs.gcc-unwrapped.lib}/lib/libasan.so ${pkgs.dovecot}/sbin/dovecot -F'";
83 # Dovecot2 does not work with environment.memoryAllocator.provider="scudo"
84 # https://wiki.dovecot.org/Design/Memory
85 # Scudo ERROR: CHECK failed at /build/compiler-rt-7.1.0.src/lib/scudo/../sanitizer_common/sanitizer_allocator_primary64.h:
86 # 644 ((beg)) == ((address_range.MapOrDie(beg, size))) (4398046511092, 4398046507008)
87 BindReadOnlyPaths = [ "/dev/null:/etc/ld-nix.so.preload" ];
88 };
89 };
90 #users.users."${dovecot2.mailUser}".isSystemUser = true; # Fix nixpkgs
91 networking.nftables.ruleset = ''
92 add rule inet filter net2fw tcp dport 993 counter accept comment "IMAPS"
93 #add rule inet filter net2fw tcp dport 995 counter accept comment "POP3S"
94 add rule inet filter net2fw tcp dport 4190 counter accept comment "Sieve"
95 '';
96 fileSystems."/var/lib/dovecot" = {
97 device = "rpool/var/mail";
98 fsType = "zfs";
99 };
100 services.dovecot2 = {
101 enable = true;
102 modules = [
103 pkgs.dovecot_pigeonhole
104 pkgs.dovecot_fts_xapian
105 ];
106 enablePAM = false;
107 enableImap = true;
108 enableLmtp = true;
109 enablePop3 = false;
110 protocols = [ "sieve" ];
111 # If needed, may be overrided by userdb_mail= in passdb, or mail= in userdb
112 # Here INDEX and CONTROL are separated,
113 # it's not useful since there is no quota at the filesystem level
114 # it's just to let this possibility later on.
115 mailLocation = "sdbox:${stateDir}/home/%d/%n/mail:UTF-8:CONTROL=${stateDir}/control/%d/%n:INDEX=${stateDir}/index/%d/%n";
116 createMailUser = false;
117 mailUser = "";
118 mailGroup = "";
119 sslServerCert = null;
120 sieveScripts = {
121 global = dovecot/sieve/global;
122 };
123 extraConfig = ''
124 auth_verbose = no
125 auth_debug = no
126 mail_debug = no
127 verbose_ssl = no
128 log_timestamp = "%Y-%m-%d %H:%M:%S "
129
130 ssl = required
131 ssl_dh = <${inputs.secrets + "/openssl/dh.pem"}
132 ssl_cipher_list = HIGH:!LOW:!SSLv2:!EXP:!aNULL
133 ssl_prefer_server_ciphers = yes
134 ssl_cert = </var/lib/acme/${networking.domain}/fullchain.pem
135 ssl_key = </var/lib/acme/${networking.domain}/key.pem
136 #ssl_ca = <''${caPath}
137 #ssl_verify_client_cert = yes
138 # FIXME: change to TLSv1.3 when supported by K-9 Mail
139 ssl_min_protocol = TLSv1.2
140
141 listen = *
142 mail_home = ${stateDir}/home/%d/%n
143 # Read multiple mails in parallel, improves performance
144 mail_prefetch_count = 20
145 # Default VSZ (virtual memory size) limit for service processes. This is mainly
146 # intended to catch and kill processes that leak memory before they eat up everything.
147 # Increased for fts_xapian.
148 default_vsz_limit = 1G
149
150 # DOC: https://doc.dovecot.org/configuration_manual/authentication/authentication_mechanisms/
151 # DOC: https://ldapwiki.com/wiki/DIGEST-MD5
152 # The misunderstanding is the notion "password sent over the network in plain-text" is not secure,
153 # when in fact, storing the password in the directory server using a Salted SHA-2 hash
154 # and transmitting the clear text password over a secure connection
155 # is more secure than having the directory server store the password in clear text
156 # or in a reversible encryption scheme.
157 # Therefore, SASL with DIGEST-MD5 (or CRAM-MD5) should be avoided.
158 # Use doveadm pw -s SSHA
159 # or doveadm pw -s PBKDF2 -r 10000
160 # or slappasswd -o module-load=pw-pbkdf2 -h "{PBKDF2-SHA256}"
161 auth_mechanisms = plain login
162 auth_username_format = %Ln
163 service auth {
164 user = ${dovecot2.user}
165 # auth_socket_path points to this userdb socket by default. It's typically
166 # used by dovecot-lda, doveadm, possibly imap process, etc.
167 # Users that have access to this socket are able to get a list
168 # of all usernames and get results of everyone's userdb lookups.
169 unix_listener auth-userdb {
170 user = ${dovecot2.user}
171 group = ${dovecot2.group}
172 mode = 0660
173 }
174 unix_listener /var/lib/postfix/queue/private/auth {
175 user = ${postfix.user}
176 group = ${postfix.group}
177 mode = 0660
178 }
179 }
180 service auth-worker {
181 user = ${dovecot2.user}
182 }
183 auth_cache_verify_password_with_worker = yes
184 # SEE: http://wiki2.dovecot.org/Quota/FS
185 # NOTE: postfix does not supply a client cert.
186 auth_ssl_require_client_cert = no
187 #auth_ssl_username_from_cert = yes
188 # NOTE: lowercase the username, help with LDAP?
189 auth_username_format = %Lu
190 # NOTE: sync with LDAP's data.
191 first_valid_uid = 1000
192 disable_plaintext_auth = yes
193
194 userdb {
195 driver = ldap
196 # A different path than passdb's args enables non-blocking LDAP requests
197 args = ${pkgs.symlinkJoin {name="ldap"; paths=[./dovecot];}}/ldap.conf
198 default_fields =
199 override_fields =
200 }
201
202 # No dirty syncs for maildir: locations which may be used directly by neomutt
203 maildir_very_dirty_syncs = no
204 namespace Inbox {
205 type = private
206 inbox = yes
207 hidden = no
208 list = yes
209 prefix =
210 separator = +
211 }
212 namespace Shared {
213 type = shared
214 #list = children
215 # NOTE: always listed in the LIST command.
216 list = yes
217 # NOTE: how to access the other users' mailboxes.
218 # NOTE: %var expands to the logged in user's variable, while
219 # %%var expands to the other users' variables.
220 # NOTE: INDEX and CONTROL are shared, INDEXPVT is not.
221 location = sdbox:${stateDir}/home/%%d/%%n/${stateDir}/mail:UTF-8:CONTROL=${stateDir}/control/%%d/%%n/shared:INDEX=${stateDir}/index/%%d/%%n/shared:INDEXPVT=${stateDir}/index/%d/%n/shared/%%n
222 prefix = Partages+%%n+
223 separator = +
224 subscriptions = yes
225 }
226 mail_plugins = $mail_plugins virtual
227 namespace Virtual {
228 prefix = Virtual+
229 separator = +
230 hidden = no
231 list = yes
232 subscriptions = no
233 location = virtual:${dovecot-virtual}:UTF-8:INDEX=${stateDir}/index/%d/%n/virtual
234 }
235
236 mail_plugins = $mail_plugins acl
237 plugin {
238 acl = vfile:/etc/dovecot/acl/global
239 acl_anyone = allow
240 # NOTE: to let users LIST mailboxes shared by other users,
241 # Dovecot needs a shared mailbox dictionary.
242 # FIXME: %d not working with userdb ldap
243 acl_shared_dict = file:${stateDir}/acl/%d/acl.db
244 }
245
246 mail_plugins = $mail_plugins fts fts_xapian
247 plugin {
248 # WARNING: doveadm fts rescan is buggy, it will delete the index:
249 # https://dovecot.org/pipermail/dovecot/2019-February/114709.html
250 # If need be, use rather: doveadm index -u "*@sourcephile.fr" -q '*'
251 plugin = fts fts_xapian
252 fts = xapian
253 fts_autoindex = yes
254 fts_autoindex_exclude = \Junk
255 fts_autoindex_exclude2 = \Trash
256 fts_enforced = yes
257 # 2 and 20 are the NGram values for header fields, which means the
258 # keywords created for fields (To, Cc, ...) are between is 2 and 20 chars long.
259 # Full words are also added by default.
260 fts_xapian = partial=2 full=20 attachments=1 verbose=0
261 fts_languages = en fr
262 #fts_dovecot_fs = posix:prefix=%h/fts/
263 }
264
265 mail_plugins = $mail_plugins quota
266 plugin {
267 quota = maildir:User quota
268 quota_rule = *:storage=256M
269 quota_rule2 = Trash:storage=+64M
270 quota_max_mail_size = 20M
271 #quota_exceeded_message = </path/to/quota_exceeded_message.txt
272 quota_warning = storage=95%% quota-warning 95 %u
273 quota_warning2 = storage=80%% quota-warning 80 %u
274 quota_warning3 = -storage=100%% quota-warning below %u
275 }
276
277 protocol lda {
278 hostname = ${networking.domain}
279 mail_plugins = $mail_plugins sieve
280 postmaster_address = postmaster+dovecot+lda@${networking.domain}
281 syslog_facility = mail
282 }
283 lda_mailbox_autocreate = yes
284 lda_mailbox_autosubscribe = yes
285
286 protocol lmtp {
287 mail_plugins = $mail_plugins sieve
288 postmaster_address = postmaster+dovecot+lmtp@${networking.domain}
289 }
290 service lmtp {
291 process_min_avail = ${toString host.CPUs}
292 unix_listener /var/lib/postfix/queue/private/dovecot-lmtp {
293 user = ${postfix.user}
294 group = ${postfix.group}
295 mode = 0600
296 }
297 }
298 plugin {
299 # Let extension.sieve do the handling of the detail
300 #lmtp_save_to_detail_mailbox = yes
301 recipient_delimiter = ${postfix.recipientDelimiter}
302 }
303
304 service imap {
305 # Most of the memory goes to mmap()ing files.
306 # You may need to increase this limit if you have huge mailboxes.
307 #vsz_limit =
308 process_limit = 1024
309 }
310 service imap-login {
311 #inet_listener imap {
312 # address = 127.0.0.1
313 # port = 143
314 # ssl = no
315 #}
316 inet_listener imaps {
317 port = 993
318 ssl = yes
319 }
320 }
321 # Store METADATA information within user's HOME directory
322 mail_attribute_dict = file:%h/dovecot-attributes
323 # If you have Dovecot v2.2.8+ you may get a significant performance improvement with fetch-headers:
324 imapc_features = $imapc_features fetch-headers
325 protocol imap {
326 #mail_max_userip_connections = 10
327 mail_plugins = $mail_plugins imap_acl imap_quota imap_sieve virtual
328 imap_metadata = yes
329
330 # DOC: https://wiki.dovecot.org/MailboxSettings
331 # Due to a bug in Dovecot v2.2.30+ if special-use flags are used,
332 # SPECIAL-USE needs to be added to post-login CAPABILITY response as RFC 6154 mandates.
333 imap_capability = +SPECIAL-USE
334
335 namespace Inbox {
336 mailbox "Archives" {
337 auto = subscribe
338 special_use = \Archive
339 }
340 mailbox "Drafts" {
341 auto = no
342 special_use = \Drafts
343 }
344 mailbox "Brouillons" {
345 auto = subscribe
346 special_use = \Drafts
347 }
348 mailbox "Junk" {
349 auto = no
350 #autoexpunge = 30d
351 special_use = \Junk
352 }
353 mailbox "Spams" {
354 auto = no
355 special_use = \Junk
356 }
357 mailbox "Spam" {
358 auto = no
359 special_use = \Junk
360 }
361 mailbox "Pourriels" {
362 auto = no
363 special_use = \Junk
364 }
365 mailbox "Pourriel" {
366 auto = no
367 special_use = \Junk
368 }
369 mailbox "Sent" {
370 auto = no
371 special_use = \Sent
372 }
373 mailbox "Sent Items" {
374 auto = no
375 special_use = \Sent
376 }
377 mailbox "Sent Messages" {
378 auto = no
379 special_use = \Sent
380 }
381 mailbox "Envoyés" {
382 auto = no
383 special_use = \Sent
384 }
385 mailbox "Trash" {
386 auto = no
387 special_use = \Trash
388 #autoexpunge = 30d
389 }
390 mailbox "Corbeille" {
391 auto = no
392 special_use = \Trash
393 }
394 }
395 namespace Virtual {
396 mailbox All {
397 auto = no
398 comment = All messages from all mailboxes
399 # This flag is confused with \Archives by K9-Mail
400 special_use = \All
401 }
402 mailbox Recents {
403 auto = no
404 comment = All messages from all mailboxes, arrived in the past 48h
405 }
406 }
407 }
408 plugin {
409 sieve_plugins = sieve_imapsieve sieve_extprograms
410 sieve_global_extensions = +vnd.dovecot.environment +vnd.dovecot.pipe
411 #sieve_extensions = +editheader
412 sieve = file:~/sieve;active=~/active.sieve
413 sieve_default = file:${stateDir}/sieve/global/default.sieve
414 sieve_default_name = main
415 sieve_pipe_bin_dir = ${sieve_pipe_bin_dir}/bin
416 sieve_max_script_size = 1M
417 sieve_quota_max_scripts = 0
418 sieve_quota_max_storage = 10M
419 #sieve_spamtest_max_value = 10
420 #sieve_spamtest_status_header = X-Spam-Score
421 #sieve_spamtest_status_type = strlen
422 #sieve_user_log = ~/sieve.log
423 # Enables support for user Sieve scripts in IMAP
424 #imapsieve_url = sieve://mail.${networking.domain}:4190
425
426 # When a flag changes, spam or ham according to the \Junk or \NonJunk flags
427 imapsieve_mailbox1_name = *
428 imapsieve_mailbox1_causes = FLAG
429 imapsieve_mailbox1_before = file:${stateDir}/sieve/global/spam-or-ham.sieve
430
431 # From elsewhere to Junk folder
432 imapsieve_mailbox2_name = Pourriel
433 imapsieve_mailbox2_causes = COPY APPEND
434 imapsieve_mailbox2_before = file:${stateDir}/sieve/global/report-spam.sieve
435
436 # From Junk folder to elsewhere
437 imapsieve_mailbox3_name = *
438 imapsieve_mailbox3_from = Pourriel
439 imapsieve_mailbox3_causes = COPY
440 imapsieve_mailbox3_before = file:${stateDir}/sieve/global/report-ham.sieve
441 }
442 protocol sieve {
443 # Maximum number of ManageSieve connections allowed for a user from each IP address.
444 # NOTE: The username is compared case-sensitively.
445 mail_max_userip_connections = 10
446
447 # To fool ManageSieve clients that are focused on CMU's timesieved you can specify
448 # the IMPLEMENTATION capability that the dovecot reports to clients.
449 # For example: 'Cyrus timesieved v2.2.13'
450 managesieve_implementation_string = Dovecot Pigeonhole
451
452 # The maximum number of compile errors that are returned to the client upon script
453 # upload or script verification.
454 managesieve_max_compile_errors = 5
455
456 #mail_max_userip_connections = 10
457 #managesieve_implementation_string = Dovecot Pigeonhole
458 managesieve_max_compile_errors = 5
459 #managesieve_max_line_length = 65536
460 #managesieve_notify_capability = mailto
461 #managesieve_sieve_capability = fileinto reject envelope encoded-character vacation subaddress comparator-i;ascii-numeric relational regex imap4flags copy include variables body enotify environment mailbox date ihave
462 }
463 service managesieve-login {
464 inet_listener sieve {
465 port = 4190
466 #ssl = yes
467 }
468
469 # Number of connections to handle before starting a new process. Typically
470 # the only useful values are 0 (unlimited) or 1. 1 is more secure, but 0
471 # is faster. <doc/wiki/LoginProcess.txt>
472 service_count = 1
473
474 # Number of processes to always keep waiting for more connections.
475 process_min_avail = 0
476
477 # If you set service_count=0, you probably need to grow this.
478 #vsz_limit = 64M
479 }
480
481 service quota-warning {
482 executable = script ${
483 pkgs.writeShellScript "quota-warning" ''
484 set -eu
485 PERCENT=$1
486 USER=$2
487 cat << EOF | ${pkgs.dovecot}/libexec/dovecot/dovecot-lda -d $USER -o
488 "plugin/quota=maildir:User quota:noenforcing"
489 From: postmaster@${networking.domain}
490 Subject: [WARNING] your mailbox is now $PERCENT% full.
491
492 Please remove some mails to make room for new ones.
493 EOF
494 ''
495 }
496 # use some unprivileged user for executing the quota warnings
497 user = ${dovecot2.user}
498 unix_listener quota-warning {
499 }
500 }
501
502 protocol pop3 {
503 mail_plugins = $mail_plugins virtual
504 #mail_max_userip_connections = 10
505 # Virtual namespace for the virtual INBOX.
506 # Use a global directory for dovecot-virtual files.
507 #namespace Inbox {
508 # hidden = yes
509 # list = no
510 # location = virtual:''${dovecot-virtual-pop3}/pop3:INDEX=${stateDir}/index/%d/%n/virtual/pop3:LAYOUT=fs
511 # prefix = pop3+
512 #}
513 pop3_client_workarounds =
514 pop3_fast_size_lookups = yes
515 pop3_lock_session = yes
516 pop3_no_flag_updates = yes
517 # Use GUIDs to avoid accidental POP3 UIDL changes instead of IMAP UIDs.
518 pop3_uidl_format = %g
519 }
520 service pop3 {
521 process_limit = 1024
522 }
523 service pop3-login {
524 inet_listener pop3s {
525 port = 995
526 ssl = yes
527 }
528 }
529 '';
530 };
531 }