]> Git — Sourcephile - sourcephile-nix.git/blob - install/logical/friot/dovecot.nix
dovecot: handle perms to auto create Maildirs.
[sourcephile-nix.git] / install / logical / friot / dovecot.nix
1 {pkgs, lib, config, system, ...}:
2 let inherit (builtins) toString toFile;
3 inherit (lib) types;
4 inherit (pkgs.lib) unlines unlinesAttrs unlinesValues;
5 inherit (config.services) dovecot2 postfix x509 openldap;
6 when = x: y: if x == null then "" else y;
7 extSep = postfix.recipientDelimiter;
8 dirSep = extSep;
9 stateDir = "/var/lib/dovecot";
10 # NOTE: nixpkgs' dovecot2.stateDir is currently not exported
11 mailDir = "${stateDir}/mail";
12 sieveDir = "${stateDir}/sieve";
13 authDir = "${stateDir}/auth";
14 authUser = dovecot2.mailUser; # TODO: php_roundcube
15 authGroup = dovecot2.mailGroup; # TODO: php_roundcube
16 escapeGroup = lib.stringAsChars (c: if "a"<=c && c<="z"
17 || "0"<=c && c<="9"
18 || c=="-"
19 then c else "_");
20 domainGroup = escapeGroup "${config.networking.baseName}";
21 etc_dovecot = [
22 { target = "dovecot/${config.networking.domain}/dovecot-ldap.conf";
23 source = pkgs.writeText "dovecot-ldap.conf" ''
24 ${lib.optionalString dovecot2.debug ''
25 debug_level = 1
26 ''}
27
28 # LDAP database
29 uris = ldapi://
30 base = ou=posix,${openldap.domainSuffix}
31 scope = subtree
32 deref = never
33 blocking = no
34 # NOTE: sufficient for small systems and uses less resources.
35
36 # LDAP auth
37 sasl_bind = yes
38 sasl_mech = EXTERNAL
39 #dn = cn=admin,${openldap.domainSuffix}
40 #dnpass = useless with sasl_mech=EXTERNAL
41 auth_bind = no
42 #auth_bind_userdn = cn=%n,ou=accounts,ou=posix,dc=${openldap.domainSuffix}
43
44 # dovecot passdb query
45 # DOC: http://wiki2.dovecot.org/PasswordDatabase/ExtraFields
46 pass_filter = (&(objectClass=posixAccount)(uid=%n)(mailEnabled=TRUE))
47 pass_attrs = userPassword=password,\
48 uidNumber=userdb_uid,\
49 gidNumber=userdb_gid,\
50 mailGroupMember=userdb_mail_access_groups=${domainGroup},\
51 quotaBytes=userdb_quota_rule=*:bytes=%{ldap:quotaBytes},\
52 =user=%n@%d
53 #homeDirectory=userdb_home
54 # TODO: userdb_quota_rule=*:storage=
55 default_pass_scheme = CRYPT
56
57 # dovecot userdb query
58 # DOC: http://wiki2.dovecot.org/UserDatabase/ExtraFields
59 user_filter = (&(objectClass=posixAccount)(uid=%n)(mailEnabled=TRUE))
60 #user_filter = (&(objectClass=inetOrgPerson)(uid=%n))
61 #user_attrs = homeDirectory=home,uidNumber=uid,gidNumber=gid
62 #user_attrs = mailHomeDirectory=home,\
63 # mailStorageDirectory=mail,\
64 # mailUidNumber=uid,\
65 # mailGidNumber=gid,\
66 # mailQuota=quota_rule=*:bytes=%$
67
68 # doveadm user query
69 iterate_attrs = =user=%{ldap:uid}@${config.networking.domain}
70 iterate_filter = (&(objectClass=posixAccount)(mailEnabled=TRUE))
71 '';
72 }
73 ];
74 dovecot-virtual = pkgs.writeTextFile {
75 name = "dovecot-virtual";
76 destination = "/pop3/INBOX/dovecot-virtual";
77 text = ''
78 all
79 all+*
80 all
81 '';
82 };
83 in
84 {
85 imports = [
86 dovecot/autoconfig.nix
87 ];
88 options = {
89 services.dovecot2.sieves = {
90 global = lib.mkOption {
91 description = "global scripts.";
92 type = types.attrsOf types.str;
93 default = {};
94 };
95 before = lib.mkOption {
96 description = "before scripts.";
97 type = types.attrsOf types.str;
98 default = {};
99 };
100 after = lib.mkOption {
101 description = "after scripts.";
102 type = types.attrsOf types.str;
103 default = {};
104 };
105 };
106 };
107 config = {
108 environment.etc = etc_dovecot;
109 #services.postfix.mapFiles."transport-dovecot" =
110 # toFile "transport-dovecot"
111 # (unlines
112 # (lib.mapAttrsToList
113 # (dom: {...}: "${transportSubDomain}.${dom} lmtp:unix:private/dovecot-lmtp")
114 # dovecot2.domains));
115 systemd.services.dovecot2 = {
116 after = [
117 "postfix.service"
118 "openldap.service"
119 ];
120 restartTriggers =
121 map (f: f.source) etc_dovecot;
122 environment = {
123 #LD_LIBRARY_PATH = config.system.nssModules.path;
124 };
125 preStart = unlinesValues {
126 installMailDir = ''
127 # SEE: http://wiki2.dovecot.org/SharedMailboxes/Permissions
128 install -D -d -m 0771 \
129 -o ${dovecot2.mailUser} \
130 -g ${dovecot2.mailGroup} \
131 ${mailDir}
132 '';
133
134 installSieve = ''
135 rm -rf "${sieveDir}"
136 '' + unlinesAttrs (dir: sieves: ''
137 install -D -d -m 0755 -o root -g root \
138 ${sieveDir} ${sieveDir}/${dir}.d
139 '' + unlinesAttrs (name: text: ''
140 src=${pkgs.writeText "${name}.sieve" text}
141 dst="${sieveDir}/${dir}.d/${name}.sieve"
142 ln -fns "$src" "$dst"
143 ${pkgs.dovecot_pigeonhole}/bin/sievec "$dst"
144 '') sieves
145 ) dovecot2.sieves;
146
147 installDomains = ''
148 # NOTE: make sure nslcd cache is in sync with the LDAP data
149 systemctl restart nslcd
150 install -D -d -m 1770 \
151 -o ${dovecot2.mailUser} \
152 -g ${domainGroup} \
153 ${mailDir}/${config.networking.domain} \
154 ${stateDir}/control/${config.networking.domain} \
155 ${stateDir}/index/${config.networking.domain}
156 # NOTE: do not set the sticky bit (+t)
157 # on acl/<domain>/, to let dovecot
158 # rename acl.db.lock (own by new user)
159 # to acl.db (own by old user)
160 install -D -d -m 0770 \
161 -o ${dovecot2.mailUser} \
162 -g ${domainGroup} \
163 ${stateDir}/acl/${config.networking.domain}
164 '';
165 };
166 };
167 services.dovecot2 = {
168 enable = true;
169 debug = true;
170 mailUser = "dovemail";
171 mailGroup = "dovemail";
172 modules = [
173 #pkgs.dovecot_antispam
174 pkgs.dovecot_pigeonhole
175 ];
176 sieves = {
177 global = {
178 list = ''
179 require
180 [ "date"
181 , "fileinto"
182 , "mailbox"
183 , "variables"
184 ];
185
186 if currentdate :matches "year" "*" { set "year" "''${1}"; }
187 if currentdate :matches "month" "*" { set "month" "''${1}"; }
188
189 if exists "List-ID" {
190 if header :matches "List-ID" "*<*.*.*.*>*" {
191 set "list" "''${2}";
192 set "domain" "''${4}";
193 }
194 elsif header :matches "List-ID" "*<*.*.*>*" {
195 set "list" "''${2}";
196 set "domain" "''${3}";
197 }
198 fileinto :create "Listes+''${domain}+''${list}+''${year}+''${month}";
199 stop;
200 }
201 '';
202 spam = ''
203 require
204 [ "imap4flags"
205 ];
206
207 if header :contains "X-Spam-Level" "***" {
208 addflag "Junk";
209 }
210 '';
211 extension = ''
212 require
213 [ "envelope"
214 , "fileinto"
215 , "mailbox"
216 , "subaddress"
217 , "variables"
218 ];
219 if envelope :matches :detail "TO" "*" {
220 set "extension" "''${1}";
221 }
222 if not string :is "''${extension}" "" {
223 fileinto :create "Plus+''${extension}";
224 stop;
225 }
226 '';
227 };
228 };
229 configFile = toString (pkgs.writeText "dovecot.conf" ''
230 passdb {
231 driver = ldap
232 args = /etc/dovecot/${config.networking.domain}/dovecot-ldap.conf
233 default_fields = userdb_mail_access_groups=${domainGroup}
234 override_fields =
235 }
236 userdb {
237 driver = prefetch
238 }
239 userdb {
240 # NOTE: this userdb is only used by lda.
241 driver = ldap
242 args = /etc/dovecot/${config.networking.domain}/dovecot-ldap.conf
243 default_fields = mail_access_groups=${domainGroup}
244 override_fields =
245 skip = found
246 }
247 #passdb {
248 # driver = passwd-file
249 # args = scheme=crypt username_format=%n ${authDir}/%d/passwd
250 #}
251 #userdb {
252 # # NOTE: this userdb is only used by lda.
253 # driver = passwd-file
254 # args = username_format=%n ${authDir}/%d/passwd
255 # #default_fields = home=${mailDir}/%d/%n
256 # skip = found
257 #}
258 mail_home = ${mailDir}/%d/%n
259 # NOTE: if needed, may be overrided by userdb_mail
260 mail_location = maildir:${mailDir}/%d/%n/Maildir:LAYOUT=fs:INDEX=${stateDir}/index/%d/%n:CONTROL=${stateDir}/control/%d/%n
261 # NOTE: if needed, may be overrided by userdb_mail
262 # NOTE: INDEX and CONTROL are on a partition without quota, as explain in the doc.
263 # SEE: http://wiki2.dovecot.org/Quota/FS
264 auth_mechanisms = plain login
265 auth_ssl_require_client_cert = no
266 # NOTE: postfix does not supply a client cert.
267 auth_ssl_username_from_cert = yes
268 auth_verbose = yes
269 auth_username_format = %Lu
270 # NOTE: lowercase the username, help with LDAP?
271 ${lib.optionalString dovecot2.debug ''
272 auth_debug = yes
273 mail_debug = yes
274 verbose_ssl = yes
275 ''}
276 default_internal_user = ${dovecot2.user}
277 default_internal_group = ${dovecot2.group}
278 disable_plaintext_auth = yes
279 first_valid_uid = 10000
280 # NOTE: sync with LDAP's data.
281 lda_mailbox_autocreate = yes
282 lda_mailbox_autosubscribe = yes
283 listen = *
284 log_timestamp = "%Y-%m-%d %H:%M:%S "
285 #maildir_copy_with_hardlinks = yes
286 namespace inbox {
287 # NOTE: here because protocol sieve {namespace inbox{}} does not seem to work.
288 inbox = yes
289 location =
290 list = yes
291 prefix =
292 separator = ${dirSep}
293 }
294 namespace {
295 #list = children
296 list = yes
297 location = maildir:${mailDir}/%d/%n/Maildir:LAYOUT=fs:INDEX=${stateDir}/index/%d/%n/Shared/%n:CONTROL=${stateDir}/control/%d/%n/Shared/%n
298 # FIXME: %d not working
299 prefix = Partages+%%n+
300 separator = ${dirSep}
301 subscriptions = yes
302 type = shared
303 }
304 mail_plugins = $mail_plugins acl quota virtual
305 #mail_uid = ${dovecot2.mailUser}
306 #mail_gid = ${dovecot2.mailGroup}
307 # NOTE: each user has a dedicated (uid,gid) pair
308 #mail_privileged_group = mail
309 #mail_access_groups =
310 plugin {
311 acl = vfile:/etc/dovecot/acl/global.d
312 acl_anyone = allow
313 acl_shared_dict = file:${stateDir}/acl/%d/acl.db
314 # NOTE: to let users LIST mailboxes shared by other users,
315 # Dovecot needs a shared mailbox dictionary.
316 # FIXME: %d not working with userdb ldap
317 ##antispam_allow_append_to_spam = yes
318 # # NOTE: pour offlineimap
319 #antispam_backend = pipe
320 ##antispam_crm_args = -u;${mailDir}/%d/.crm114;/usr/share/crm114/mailfilter.crm
321 #antispam_crm_args = -u;${mailDir}/crm114;/usr/share/crm114/mailfilter.crm
322 #antispam_crm_binary = /usr/bin/crm
323 #antispam_debug_target = syslog
324 ##antispam_crm_env = HOME=%h;USER=%u
325 #antispam_ham_keywords = NonJunk
326 #antispam_pipe_program = /usr/bin/crm
327 #antispam_pipe_program_args = -u;${mailDir}/crm114;/usr/share/crm114/mailfilter.crm;--stats_only;--force
328 #antispam_pipe_program_notspam_arg = --learnnonspam
329 #antispam_pipe_program_spam_arg = --learnspam
330 #antispam_pipe_program_unlearn_spam_args = --unlearn;--learnspam
331 #antispam_pipe_program_unlearn_notspam_args = --unlearn;--learnnonspam
332 #antispam_pipe_tmpdir = ${mailDir}/crm114/tmp
333 #antispam_signature = X-CRM114-CacheID
334 #antispam_signature_missing = move
335 #antispam_spam = Junk
336 #antispam_spam_keywords = Junk
337 #antispam_trash = Trash
338 #antispam_unsure = Unsure
339 #antispam_verbose_debug = 0
340 quota = maildir:User quota
341 quota_rule = *:storage=256M
342 quota_rule2 = Trash:storage=+64M
343 quota_max_mail_size = 20M
344 #quota_exceeded_message = </path/to/quota_exceeded_message.txt
345 quota_warning = storage=95%% quota-warning 95 %u
346 quota_warning2 = storage=80%% quota-warning 80 %u
347 quota_warning3 = -storage=100%% quota-warning below %u
348 # NOTE: user is no longer over quota
349 recipient_delimiter = ${extSep}
350 sieve = file:${mailDir}/%d/%n/sieve;active=${mailDir}/%d/%n/sieve/main.sieve
351 #sieve_default = file:${mailDir}/%u/default.sieve
352 #sieve_default_name = default
353 sieve_after = ${sieveDir}/after.d/
354 sieve_before = ${sieveDir}/before.d/
355 sieve_dir = ${mailDir}/%d/%n/sieve/
356 #sieve_extensions = +spamtest +spamtestplus
357 sieve_global_dir = ${sieveDir}/global.d/
358 sieve_max_script_size = 1M
359 sieve_quota_max_scripts = 0
360 sieve_quota_max_storage = 10M
361 sieve_spamtest_max_value = 10
362 sieve_spamtest_status_header = X-Spam-Score
363 sieve_spamtest_status_type = strlen
364 sieve_user_log = /var/log/dovecot/%d/sieve.%n.log
365 }
366 service quota-warning {
367 executable = script ${
368 pkgs.writeScript "quota-warning" ''
369 #!/bin/sh -eu
370 PERCENT=$1
371 USER=$2
372 cat << EOF | ${pkgs.dovecot}/libexec/dovecot/dovecot-lda -d $USER -o
373 "plugin/quota=maildir:User quota:noenforcing"
374 From: postmaster@${config.networking.domain}
375 Subject: [WARNING] your mailbox is now $PERCENT% full.
376
377 Please remove some mails to make room for new ones.
378 EOF
379 ''
380 }
381 # use some unprivileged user for executing the quota warnings
382 user = ${dovecot2.user}
383 unix_listener quota-warning {
384 }
385 }
386 protocol imap {
387 #mail_max_userip_connections = 10
388 mail_plugins = $mail_plugins imap_acl imap_quota # antispam
389 namespace inbox {
390 inbox = yes
391 location =
392 list = yes
393 mailbox Drafts {
394 special_use = \Drafts
395 }
396 mailbox Junk {
397 special_use = \Junk
398 }
399 mailbox Sent {
400 special_use = \Sent
401 }
402 mailbox "Sent Messages" {
403 special_use = \Sent
404 }
405 mailbox Trash {
406 special_use = \Trash
407 }
408 prefix =
409 separator = ${dirSep}
410 }
411 }
412 protocol lda {
413 auth_socket_path = /var/run/dovecot/auth-userdb
414 hostname = ${config.networking.domain}
415 info_log_path =
416 log_path =
417 mail_plugins = $mail_plugins sieve
418 namespace inbox {
419 inbox = yes
420 location =
421 list = yes
422 prefix =
423 separator = ${dirSep}
424 }
425 postmaster_address = postmaster${extSep}dovecot${extSep}lda@${config.networking.domain}
426 syslog_facility = mail
427 }
428 protocol lmtp {
429 #info_log_path = /tmp/dovecot-lmtp.log
430 mail_plugins = $mail_plugins sieve
431 namespace inbox {
432 inbox = yes
433 location =
434 list = yes
435 prefix =
436 separator = ${dirSep}
437 }
438 postmaster_address = postmaster${extSep}dovecot${extSep}lmtp@${config.networking.domain}
439 }
440 protocol pop3 {
441 #mail_max_userip_connections = 10
442 namespace all {
443 # NOTE: used by ${dovecot-virtual}/pop3/INBOX/dovecot-virtual
444 hidden = yes
445 list = no
446 location =
447 prefix = all+
448 separator = ${dirSep}
449 }
450 # Virtual namespace for the virtual INBOX.
451 # Use a global directory for dovecot-virtual files.
452 namespace inbox {
453 inbox = yes
454 hidden = yes
455 list = no
456 location = virtual:${dovecot-virtual}/pop3:INDEX=${stateDir}/index/%d/%n/POP3:LAYOUT=fs
457 prefix = pop3+
458 separator = ${dirSep}
459 }
460 pop3_client_workarounds =
461 pop3_fast_size_lookups = yes
462 pop3_lock_session = yes
463 pop3_no_flag_updates = yes
464 # Use GUIDs to avoid accidental POP3 UIDL changes instead of IMAP UIDs.
465 pop3_uidl_format = %g
466 }
467 protocol sieve {
468 #mail_max_userip_connections = 10
469 #managesieve_implementation_string = Dovecot Pigeonhole
470 managesieve_max_compile_errors = 5
471 #managesieve_max_line_length = 65536
472 #managesieve_notify_capability = mailto
473 #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
474 }
475 protocols = imap lmtp pop3 sieve
476 service lmtp {
477 #executable = lmtp -L
478 process_min_avail = 2
479 unix_listener /var/lib/postfix/queue/private/dovecot-lmtp {
480 user = ${postfix.user}
481 group = ${postfix.group}
482 mode = 0600
483 }
484 #user = mail
485 }
486 service auth {
487 user = root
488 # FIXME: may be user=dovecot-auth with LDAP?
489 unix_listener auth-userdb {
490 user = ${dovecot2.user}
491 group = ${dovecot2.group}
492 mode = 0660
493 }
494 unix_listener /var/lib/postfix/queue/private/auth {
495 user = ${postfix.user}
496 group = ${postfix.group}
497 mode = 0660
498 }
499 }
500 service imap {
501 # Most of the memory goes to mmap()ing files.
502 # You may need to increase this limit if you have huge mailboxes.
503 #vsz_limit =
504 process_limit = 1024
505 }
506 service imap-login {
507 #inet_listener imap {
508 # address = 127.0.0.1
509 # port = 143
510 # ssl = no
511 #}
512 inet_listener imaps {
513 port = 993
514 ssl = yes
515 }
516 }
517 service pop3 {
518 process_limit = 1024
519 }
520 service pop3-login {
521 inet_listener pop3s {
522 port = 995
523 ssl = yes
524 }
525 }
526 ssl = required
527 ssl_dh = <${x509.dir}/dh.pem
528 ssl_cipher_list = HIGH:!LOW:!SSLv2:!EXP:!aNULL
529 ssl_cert = <${x509.cert}
530 ssl_key = <${x509.key}
531 #ssl_ca = <''${caPath}
532 #ssl_verify_client_cert = yes
533 '');
534 #${lib.concatMapStringsSep "\n"
535 # (dom: ''
536 # local_name mail.${dom} {
537 # #ssl_ca = <''${caPath}
538 # ssl_cert = <${x509.cert dom}
539 # ssl_key = <${x509.key dom}
540 # }
541 # '')
542 # dovecot2.domains
543 #}
544 };
545 };
546 }