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