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