organize / cleanup
[distro-setup] / mail-setup
1 #!/bin/bash
2 set -x
3
4 # Copyright (C) 2016 Ian Kelling
5
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9
10 # http://www.apache.org/licenses/LICENSE-2.0
11
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 set -eE -o pipefail
19 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
20
21 [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
22
23 usage() {
24 cat <<EOF
25 Usage: ${0##*/} exim4|postfix
26 Setup exim4 / postfix / dovecot
27
28 The minimal assumption we have is that /etc/mailpass exists
29
30
31 I\'ve had problems with postfix on debian:
32 on stretch, a startup ordering issue caused all mail to fail.
33 postfix changed defaults to only use ipv6 dns, causing all my mail to fail.
34 I haven\'t gotten around to getting a non-debian exim
35 setup.
36
37 -h|--help Print help and exit.
38 EOF
39 exit $1
40 }
41
42 type=$1
43 postfix() { [[ $type == postfix ]]; }
44 exim() { [[ $type == exim4 ]]; }
45
46 if ! exim && ! postfix; then
47 usage 1
48 fi
49
50 if [[ ! $SUDO_USER ]]; then
51 echo "$0: error: requires running as nonroot or sudo"
52 fi
53 u=$SUDO_USER
54
55
56 ####### begin perstent password instructions ######
57 # # exim passwords:
58 # # for hosts which have all private files I just use the same user
59 # # for other hosts, each one get\'s their own password.
60 # # for generating secure pass, and storing for server too:
61 # # user=USUALLY_SAME_AS_HOSTNAME
62 # user=li
63 # f=$(mktemp)
64 # apg -m 50 -x 70 -n 1 -a 1 -M CLN >$f
65 # s sed -i "/^$user:/d" /p/c/filesystem/etc/exim4/passwd
66 # echo "$user:$(mkpasswd -m sha-512 -s <$f)" >>/p/c/filesystem/etc/exim4/passwd
67 # echo "mail.iankelling.org $user $(<$f)" >> /p/c/machine_specific/$user/filesystem/etc/mailpass
68 # # then run this script, or part of it which uses /etc/mailpass
69
70 # # dovecot password, i just need 1 as I\'m the only user
71 # mkdir /p/c/filesystem/etc/dovecot
72 # echo "ian:$(doveadm pw -s ssha256)::::::" >/p/c/filesystem/etc/dovecot/users
73 # conflink
74
75
76
77 # # for ad-hoc testing of some random new host sending mail:
78 # user=li # client host username & hostname
79 # f=$(mktemp)
80 # apg -m 50 -x 70 -n 1 -a 1 -M CLN >$f
81 # s sed -i "/^$user:/d" /etc/exim4/passwd
82 # echo "$user:$(mkpasswd -m sha-512 -s <$f)" | s tee -a /etc/exim4/passwd
83 # echo "mail.iankelling.org:$user:$(<$f)" | ssh root@$user dd of=/etc/exim4/passwd.client
84 ####### end perstent password instructions ######
85
86
87 ####### begin persistent dkim/dns instructions #########
88 # # Remove 1 level of comments in this section, set the domain var
89 # # for the domain you are setting up, then run this and copy dns settings
90 # # into dns.
91 # domain=iankelling.org
92 # c /p/c/filesystem/etc/exim4
93 # # this has several bugs addressed in comments, but it was helpful
94 # # https://debian-administration.org/article/718/DKIM-signing_outgoing_mail_with_exim4
95
96 # openssl genrsa -out $domain-private.pem 2048 -outform PEM
97 # openssl rsa -in $domain-private.pem -out $domain.pem -pubout -outform PEM
98 # # selector is needed for having multiple keys for one domain.
99 # # I dun do that, so just use a static one: li
100 # echo "txt record name: li._domainkey.$domain"
101 # # Debadmin page does not have v=, fastmail does, and this
102 # # says it\'s recommended in 3.6.1, default is DKIM1 anyways.
103 # # https://www.ietf.org/rfc/rfc6376.txt
104 # # Join and print all but first and last line.
105 # # last line: swap hold & pattern, remove newlines, print.
106 # # lines 2+: append to hold space
107 # echo "txt record contents:"
108 # echo "v=DKIM1; k=rsa; p=$(sed -n '${x;s/\n//gp};2,$H' $domain.pem)"
109 # chmod 644 $domain.pem
110 # chmod 640 $domain-private.pem
111 # # in conflink, we chown these to group debian
112 # conflink
113 # # selector was also put into /etc/exim4/conf.d/main/000_localmacros,
114 # # via the mail-setup scripts
115
116 # # 2017-02 dmarc policies:
117 # # host -t txt _dmarc.gmail.com
118 # # yahoo: p=reject, hotmail: p=none, gmail: p=none, fastmail none for legacy reasons
119 # # there were articles claiming gmail would be changing
120 # # to p=reject, in early 2017, which didn\'t happen. I see no sources on them. It\'s
121 # # expected to cause problems
122 # # with a few old mailing lists, copying theirs for now.
123 #
124 # echo "dmarc dns, name: _dmarc value: v=DMARC1; p=none; rua=mailto:mailauth-reports@$domain"
125
126 # # 2017-02 spf policies:
127 # # host -t txt lists.fedoraproject.org
128 # # google ~all, hotmail -all, yahoo: ?all, fastmail ?all
129 # # i include fastmail\'s settings, per their instructions,
130 # # and follow their policy. In mail in a box, or similar instructions,
131 # # I\'ve seen recommended to not use a restrictive policy.
132 # echo "spf dns: name is empty, value: v=spf1 a include:spf.messagingengine.com ?all"
133
134 # # to check if dns has updated, you do
135 # host -a mesmtp._domainkey.$domain
136
137 # # mx records,
138 # # setting it to iankelling.org would work the same, but this
139 # # is more flexible, I could change where mail.iankelling.org pointed.
140 # cat <<'EOF'
141 # mx records, 2 records each, for * and empty domain
142 # pri 10 mail.iankelling.org
143 # pri 20 in1-smtp.messagingengine.com
144 # pri 30 in2-smtp.messagingengine.com
145 # EOF
146 ####### end persistent dkim instructions #########
147
148
149 # misc exim notes:
150 # useful exim docs:
151 # /usr/share/doc/exim4-base/README.Debian.gz
152 # /usr/share/doc/exim4-base/spec.txt.gz
153
154 # routers, transports, and authenticators are sections, and you define
155 # driver instances in those sections, and the manual calls them driver
156 # types but there is also a more specific "type" of driver, which is specified
157 # with the driver = some_module setting in the driver.
158
159 # the driver option must precede and private options (options that are
160 # specific to that driver), so follow example of putting it at beginning.
161
162 # The full list of option settings for any particular driver instance,
163 # including all the defaulted values, can be extracted by making use of
164 # the -bP command line option.
165 # exim -bP config_file to see what config file it used
166 # exim -bP config to see
167
168 # exim clear out message queue. as root:
169 # adapted from somewhere on stackoverflow.
170 # ser stop exim4; sleep 1; exim -bp | exiqgrep -i | xargs exim -Mrm; ser start exim4
171
172 # fastmail has changed their smtp server, but the old one still works,
173 # I see no reason to bother changing.
174 # New one is smtp.fastmail.com
175
176 # test delivery & rewrite settings:
177 #exim4 -bt iank@localhost
178
179
180 postconfin() {
181 local MAPFILE
182 mapfile -t
183 local s
184 postconf -ev "${MAPFILE[@]}"
185 }
186 e() { printf "%s\n" "$*"; }
187
188 postmaster=$u
189 mxhost=mail.iankelling.org
190 mxport=25
191 forward=$u@$mxhost
192
193 # old setup. left as comment for example
194 # mxhost=mail.messagingengine.com
195 # mxport=587
196 # forward=ian@iankelling.org
197
198 relayhost="[$mxhost]:$mxport" # postfix
199 smarthost="$mxhost::$mxport" # exim
200
201 # trisquel 8 = openvpn, debian stretch = openvpn-client
202 vpn_ser=openvpn-client
203 if [[ ! -e /lib/systemd/system/openvpn-client@.service ]]; then
204 vpn_ser=openvpn
205 fi
206
207 if [[ $HOSTNAME == $MAIL_HOST ]]; then
208 # afaik, these will get ignored because they are routing to my own
209 # machine, but rm them is safer
210 rm -f $(eval echo ~$postmaster)/.forward /root/.forward
211 else
212 # this can\'t be a symlink and has permission restrictions
213 # it might work in /etc/aliases, but this seems more proper.
214 install -m 644 {-o,-g}$postmaster <(e $forward) $(eval echo ~$postmaster)/.forward
215 fi
216
217 # offlineimap uses this too, it is much easier to use one location than to
218 # condition it\'s config and postfix\'s config
219 if [[ -f /etc/fedora-release ]]; then
220 /a/exe/lnf -T ca-certificates.crt /etc/ssl/ca-bundle.trust.crt
221 fi
222
223 if postfix; then
224 # dunno why, but debian installed postfix with builddep emacs
225 # but I will just explicitly install it here since
226 # I use it for sending mail in emacs.
227 if command -v apt-get &> /dev/null; then
228 debconf-set-selections <<EOF
229 postfix postfix/main_mailer_type select Satellite system
230 postfix postfix/mailname string $HOSTNAME
231 postfix postfix/relayhost string $relayhost
232 postfix postfix/root_address string $postmaster
233 EOF
234 if dpkg -s postfix &>/dev/null; then
235 dpkg-reconfigure -u -fnoninteractive postfix
236 else
237 apt-get -y install --purge --auto-remove postfix
238 fi
239 else
240 source /a/bin/distro-functions/src/package-manager-abstractions
241 pi postfix
242 # Settings from reading the output when installing on debian,
243 # then seeing which were different in a default install on arch.
244 # I assume the same works for fedora.
245 postconfin <<EOF
246 mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
247 mailbox_size_limit = 0
248 relayhost = $relayhost
249 inet_interfaces = loopback-only
250 EOF
251
252 systemctl enable postfix
253 systemctl start postfix
254 fi
255 # i\'m assuming mail just won\'t work on systems without the sasl_passwd.
256 postconfin <<'EOF'
257 smtp_sasl_auth_enable = yes
258 smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
259 smtp_sasl_security_options = noanonymous
260 smtp_tls_security_level = secure
261 message_size_limit = 20480000
262 smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
263 inet_protocols = ipv4
264 EOF
265 # msg_size_limit: I ran into a log file not sending cuz of size. double from 10 to 20 meg limit
266 # inet_protocols: without this, I\'ve had postfix try an ipv6 lookup then gives
267 # up and fail forever. snippet from syslog: type=AAAA: Host not found, try again
268
269
270 f=/etc/postfix/sasl_passwd
271 install -m 600 /dev/null $f
272 cat /etc/mailpass| while read -r domain port pass; do
273 # format: domain port user:pass
274 # mailpass is just a name i made up, since postfix and
275 # exim both use a slightly crazy format to translate to
276 # each other, it\'s easier to use my own format.
277 printf "[%s]:%s %s" "$domain" "$port" "${pass/@/#}" >>$f
278 done
279 postmap hash:/etc/postfix/sasl_passwd
280 # need restart instead of reload when changing
281 # inet_protocols
282 service postfix restart
283
284 else # begin exim. has debian specific stuff for now
285
286 if ! dpkg -s openvpn &>/dev/null; then
287 apt-get -y install --purge --auto-remove openvpn
288 fi
289
290 if [[ -e /p/c/filesystem ]]; then
291 # to put the hostname in the known hosts
292 ssh -o StrictHostKeyChecking=no root@li.iankelling.org :
293 /a/exe/vpn-mk-client-cert -b mail -n mail li.iankelling.org
294 fi
295
296 cat >/etc/systemd/system/mailroute.service <<EOF
297 [Unit]
298 # this unit is configured to start and stop whenever $vpn_ser@mail.service
299 # does
300 Description=Routing for email vpn
301 After=network.target
302 BindsTo=$vpn_ser@mail.service
303 After=$vpn_ser@mail.service
304
305 [Service]
306 Type=oneshot
307 ExecStart=/a/bin/distro-setup/mail-route start
308 ExecStop=/a/bin/distro-setup/mail-route stop
309 RemainAfterExit=yes
310
311 [Install]
312 RequiredBy=$vpn_ser@mail.service
313 EOF
314
315 cat >/etc/systemd/system/offlineimapsync.timer <<'EOF'
316 [Unit]
317 Description=Run offlineimap-sync once every 5 mins
318
319 [Timer]
320 OnCalendar=*:0/5
321
322 [Install]
323 WantedBy=timers.target
324 EOF
325
326 cat >/etc/systemd/system/offlineimapsync.service <<EOF
327 [Unit]
328 Description=Offlineimap sync
329 After=multi-user.target
330
331 [Service]
332 User=$u
333 Type=oneshot
334 ExecStart=/a/bin/log-quiet/sysd-mail-once offlineimap-sync /a/bin/distro-setup/offlineimap-sync
335 EOF
336 systemctl daemon-reload
337 systemctl enable mailroute
338
339 # wording of question from dpkg-reconfigure exim4-config
340 # 1. internet site; mail is sent and received directly using SMTP
341 # 2. mail sent by smarthost; received via SMTP or fetchmail
342 # 3. mail sent by smarthost; no local mail
343 # 4. local delivery only; not on a network
344 # 5. no configuration at this time
345 #
346 # Note, I have used option 2 in the past for receiving mail
347 # from lan hosts, sending external mail via another smtp server.
348 #
349 # Note, other than configtype, we could set all the options in
350 # both types of configs without harm, they would either be
351 # ignored or be disabled by other settings, but the default
352 # local_interfaces definitely makes things more secure.
353
354 # most of these settings get translated into settings
355 # in /etc/exim4/update-exim4.conf.conf
356 # how /etc/exim4/update-exim4.conf.conf translates into actual exim settings is
357 # documented in man update-exim4.conf, which outputs to the config that
358 # exim actually reads. except the man page is not perfect, for example,
359 # it doesn't document that it sets
360 # DCconfig_${dc_eximconfig_configtype}" "1"
361 # which is a line from update-exim4.conf, which is a relatively short bash script.
362 # mailname setting sets /etc/mailname
363
364 debconf-set-selections <<EOF
365 exim4-config exim4/use_split_config boolean true
366 EOF
367
368 source /a/bin/bash_unpublished/source-semi-priv
369 exim_main_dir=/etc/exim4/conf.d/main
370 mkdir -p $exim_main_dir
371
372
373
374 #### begin mail cert setup ###
375 f=/usr/local/bin/mail-cert-cron
376 cat >$f <<'EOF'
377 set -eE -o pipefail
378 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
379
380 [[ $EUID == 0 ]] || exec sudo "$BASH_SOURCE" "$@"
381
382 f=/a/bin/bash_unpublished/source-semi-priv
383 if [[ -e $f ]]; then
384 source $f
385 fi
386 if [[ $HOSTNAME == $MAIL_HOST ]]; then
387 local_mx=mail.iankelling.org
388 rsync_common="rsync -ogtL --chown=root:Debian-exim --chmod=640 root@li:/etc/letsencrypt/live/$local_mx/"
389 ${rsync_common}fullchain.pem /etc/exim4/exim.crt
390 ${rsync_common}privkey.pem /etc/exim4/exim.key
391 fi
392 EOF
393 chmod 755 $f
394
395 cat >/etc/systemd/system/mailcert.service <<'EOF'
396 [Unit]
397 Description=Mail cert rsync
398 After=multi-user.target
399
400 [Service]
401 Type=oneshot
402 ExecStart=/a/bin/log-quiet/sysd-mail-once mailcert /usr/local/bin/mail-cert-cron
403 EOF
404
405 cat >/etc/systemd/system/mailcert.timer <<'EOF'
406 [Unit]
407 Description=Run mail-cert once a day
408
409 [Timer]
410 OnCalendar=daily
411
412 [Install]
413 WantedBy=timers.target
414 EOF
415 systemctl daemon-reload
416 systemctl start mailcert
417 systemctl restart mailcert.timer
418 systemctl enable mailcert.timer
419
420 ##### end mailcert setup #####
421
422
423
424 if [[ $HOSTNAME == $MAIL_HOST ]]; then
425
426 debconf-set-selections <<EOF
427 # Mail Server configuration
428 # -------------------------
429
430 # Please select the mail server configuration type that best meets your needs.
431
432 # Systems with dynamic IP addresses, including dialup systems, should generally be
433 # configured to send outgoing mail to another machine, called a 'smarthost' for
434 # delivery because many receiving systems on the Internet block incoming mail from
435 # dynamic IP addresses as spam protection.
436
437 # A system with a dynamic IP address can receive its own mail, or local delivery can be
438 # disabled entirely (except mail for root and postmaster).
439
440 # 1. internet site; mail is sent and received directly using SMTP
441 # 2. mail sent by smarthost; received via SMTP or fetchmail
442 # 3. mail sent by smarthost; no local mail
443 # 4. local delivery only; not on a network
444 # 5. no configuration at this time
445
446 # General type of mail configuration: 1
447 exim4-config exim4/dc_eximconfig_configtype select internet site; mail is sent and received directly using SMTP
448
449
450
451 # The 'mail name' is the domain name used to 'qualify' mail addresses without a domain
452 # name.
453
454 # This name will also be used by other programs. It should be the single, fully
455 # qualified domain name (FQDN).
456
457 # Thus, if a mail address on the local host is foo@example.org, the correct value for
458 # this option would be example.org.
459
460 # This name won\'t appear on From: lines of outgoing messages if rewriting is enabled.
461
462 # System mail name:
463 exim4-config exim4/mailname string mail.iankelling.org
464
465
466
467
468 # Please enter a semicolon-separated list of recipient domains for which this machine
469 # should consider itself the final destination. These domains are commonly called
470 # 'local domains'. The local hostname (treetowl.lan) and 'localhost' are always added
471 # to the list given here.
472
473 # By default all local domains will be treated identically. If both a.example and
474 # b.example are local domains, acc@a.example and acc@b.example will be delivered to the
475 # same final destination. If different domain names should be treated differently, it
476 # is necessary to edit the config files afterwards.
477
478 # Other destinations for which mail is accepted:
479 # iank.bid is for testing
480 # mail.iankelling.org is for machines i own
481 exim4-config exim4/dc_other_hostnames string *.iankelling.org;iankelling.org;*iank.bid;iank.bid;*zroe.org;zroe.org;*.b8.nz;b8.nz
482
483
484
485
486 # Please enter a semicolon-separated list of IP addresses. The Exim SMTP listener
487 # daemon will listen on all IP addresses listed here.
488
489 # An empty value will cause Exim to listen for connections on all available network
490 # interfaces.
491
492 # If this system only receives mail directly from local services (and not from other
493 # hosts), it is suggested to prohibit external connections to the local Exim daemon.
494 # Such services include e-mail programs (MUAs) which talk to localhost only as well as
495 # fetchmail. External connections are impossible when 127.0.0.1 is entered here, as
496 # this will disable listening on public network interfaces.
497
498 # IP-addresses to listen on for incoming SMTP connections:
499 exim4-config exim4/dc_local_interfaces string
500
501
502
503
504 # Mail for the 'postmaster', 'root', and other system accounts needs to be redirected
505 # to the user account of the actual system administrator.
506
507 # If this value is left empty, such mail will be saved in /var/mail/mail, which is not
508 # recommended.
509
510 # Note that postmaster\'s mail should be read on the system to which it is directed,
511 # rather than being forwarded elsewhere, so (at least one of) the users listed here
512 # should not redirect their mail off this machine. A 'real-' prefix can be used to
513 # force local delivery.
514
515 # Multiple user names need to be separated by spaces.
516
517 # Root and postmaster mail recipient:
518 exim4-config exim4/dc_postmaster string $postmaster
519
520
521
522 # Exim is able to store locally delivered email in different formats. The most commonly
523 # used ones are mbox and Maildir. mbox uses a single file for the complete mail folder
524 # stored in /var/mail/. With Maildir format every single message is stored in a
525 # separate file in ~/Maildir/.
526
527 # Please note that most mail tools in Debian expect the local delivery method to be
528 # mbox in their default.
529
530 # 1. mbox format in /var/mail/ 2. Maildir format in home directory
531
532 # Delivery method for local mail: 2
533 exim4-config exim4/dc_localdelivery select Maildir format in home directory
534 EOF
535 # MAIN_HARDCODE_PRIMARY_HOSTNAME might mess up the
536 # smarthost config type, not sure. all other settings
537 # would be unused in that config type.
538 cat >$exim_main_dir/000_localmacros <<EOF
539 # i don't have ipv6 setup for my tunnel yet.
540 disable_ipv6 = true
541
542 MAIN_TLS_ENABLE = true
543
544 DKIM_CANON = relaxed
545 DKIM_SELECTOR = li
546
547 # from comments in
548 # https://debian-administration.org/article/718/DKIM-signing_outgoing_mail_with_exim4
549
550 # The file is based on the outgoing domain-name in the from-header.
551 DKIM_DOMAIN = \${lc:\${domain:\$h_from:}}
552 # sign if key exists
553 DKIM_PRIVATE_KEY= \${if exists{/etc/exim4/\${dkim_domain}-private.pem} {/etc/exim4/\${dkim_domain}-private.pem}}
554
555
556 # failing message on mail-tester.com:
557 # We check if there is a server (A Record) behind your hostname treetowl.
558 # You may want to publish a DNS record (A type) for the hostname treetowl or use a different hostname in your mail software
559 # https://serverfault.com/questions/46545/how-do-i-change-exim4s-primary-hostname-on-a-debian-box
560 # and this one seemed appropriate from grepping config.
561 # I originally set this to li.iankelling.org, but then ended up with errors when li tried to send
562 # mail to treetowl, so this should basically be a name that no host has as their
563 # canonical hostname since the actual host sits behind a nat and changes.
564 # Seems logical for this to be the same as mailname.
565 MAIN_HARDCODE_PRIMARY_HOSTNAME = mail.iankelling.org
566
567 # normally empty, I set this so I can set the envelope address
568 # when doing mail redelivery to invoke filters
569 MAIN_TRUSTED_GROUPS = $u
570
571 LOCAL_DELIVERY = dovecot_lmtp
572
573 # options exim has to avoid having to alter the default config files
574 CHECK_RCPT_LOCAL_ACL_FILE = /etc/exim4/rcpt_local_acl
575 CHECK_DATA_LOCAL_ACL_FILE = /etc/exim4/data_local_acl
576
577 # debian exim config added this in 2016 or so?
578 # it's part of the smtp spec, to limit lines to 998 chars
579 # but a fair amount of legit mail does not adhere to it. I don't think
580 # this should be default, like it says in
581 # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=828801
582 # todo: the bug for introducing this was about headers, but
583 # the fix maybe is for all lines? one says gmail rejects, the
584 # other says gmail does not reject. figure out and open a new bug.
585 IGNORE_SMTP_LINE_LENGTH_LIMIT = true
586
587 # most of the ones that gmail seems to use.
588 # Exim has horrible default of signing unincluded
589 # list- headers since they got mentioned in an
590 # rfc, but this messes up mailing lists, like gnu/debian which want to
591 # keep your dkim signature intact but add list- headers.
592 DKIM_SIGN_HEADERS = mime-version:in-reply-to:references:from:date:subject:to
593
594 EOF
595
596
597 ####### begin dovecot setup ########
598 # based on a little google and package search, just the dovecot
599 # packages we need instead of dovecot-common.
600 #
601 # dovecot-lmtpd is for exim to deliver to dovecot instead of maildir
602 # directly. The reason to do this is to use dovecot\'s sieve, which
603 # has extensions that allow it to be almost equivalent to exim\'s
604 # filter capabilities, some ways probably better, some worse, and
605 # sieve has the benefit of being supported in postfix and
606 # proprietary/weird environments, so there is more examples on the
607 # internet. I was torn about whether to do this or not, meh.
608 apt-get -y install --purge --auto-remove \
609 dovecot-core dovecot-imapd dovecot-sieve dovecot-lmtpd
610
611 # if we changed 90-sieve.conf and removed the active part of the
612 # sieve option, we wouldn\'t need this, but I\'d rather not modify a
613 # default config if not needed. This won\'t work as a symlink in /a/c
614 # unfortunately.
615 sudo -u $postmaster /a/exe/lnf -T sieve/main.sieve $(eval echo ~$postmaster)/.dovecot.sieve
616
617 sed -ri -f - /etc/dovecot/conf.d/10-mail.conf <<'EOF'
618 1i mail_location = maildir:/m/md:LAYOUT=fs:INBOX=/m/md/INBOX
619 /^\s*mail_location\s*=/d
620 EOF
621
622 cat >/etc/dovecot/conf.d/20-lmtp.conf <<EOF
623 protocol lmtp {
624 #per https://wiki2.dovecot.org/Pigeonhole/Sieve/Configuration
625 mail_plugins = \$mail_plugins sieve
626 # default was
627 #mail_plugins = \$mail_plugins
628
629 # For a normal setup with exim, we need something like this, which
630 # removes the domain part
631 # auth_username_format = %Ln
632 #
633 # or else # Exim says something like
634 # "LMTP error after RCPT ... 550 ... User doesn't exist someuser@somedomain"
635 # Dovecot verbose log says something like
636 # "auth-worker(9048): passwd(someuser@somedomain): unknown user"
637 # reference: http://wiki.dovecot.org/LMTP/Exim
638 #
639 # However, I use this to direct all mail to the same inbox.
640 # A normal way to do this, which I did at first is to have
641 # a router in exim almost at the end, eg 950,
642 #local_catchall:
643 # debug_print = "R: catchall for \$local_part@\$domain"
644 # driver = redirect
645 # domains = +local_domains
646 # data = $u
647 # based on
648 # http://blog.alteholz.eu/2015/04/exim4-and-catchall-email-address/
649 # with superflous options removed.
650 # However, this causes the envelope to be rewritten,
651 # which makes filtering into mailboxes a little less robust or more complicated,
652 # so I've done it this way instead. it also requires
653 # modifying the local router in exim.
654 auth_username_format = $u
655 }
656
657 EOF
658
659
660 cat >/etc/dovecot/local.conf <<'EOF'
661 # so I can use a different login that my shell login for mail. this is
662 # worth doing solely for the reason that if this login is compromised,
663 # it won't also compromise my shell password.
664 !include conf.d/auth-passwdfile.conf.ext
665
666 # settings derived from wiki and 10-ssl.conf
667 ssl = required
668 ssl_cert = </etc/exim4/exim.crt
669 ssl_key = </etc/exim4/exim.key
670 # https://github.com/certbot/certbot/raw/master/certbot-apache/certbot_apache/options-ssl-apache.conf
671 # in my cert cronjob, I check if that has changed upstream.
672 ssl_cipher_list = ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA
673
674 # ian: added this, more secure, per google etc
675 ssl_prefer_server_ciphers = yes
676
677 # for debugging info, uncomment these.
678 # logs go to syslog and to /var/log/mail.log
679 # auth_verbose=yes
680 #mail_debug=yes
681 EOF
682 ####### end dovecot setup ########
683
684
685 systemctl enable offlineimapsync.timer
686 systemctl start offlineimapsync.timer
687 systemctl restart $vpn_ser@mail
688 systemctl enable $vpn_ser@mail
689 systemctl enable dovecot
690 systemctl restart dovecot
691
692 else # $HOSTNAME != $MAIL_HOST
693 systemctl disable offlineimapsync.timer &>/dev/null ||:
694 systemctl stop offlineimapsync.timer &>/dev/null ||:
695 systemctl disable $vpn_ser@mail
696 systemctl stop $vpn_ser@mail
697 systemctl disable dovecot ||:
698 systemctl stop dovecot ||:
699 #
700 #
701 # would only exist because I wrote it i the previous condition,
702 # it\'s not part of exim
703 rm -f $exim_main_dir/000_localmacros
704 debconf-set-selections <<EOF
705 exim4-config exim4/dc_eximconfig_configtype select mail sent by smarthost; no local mail
706 exim4-config exim4/dc_smarthost string $smarthost
707 # the default, i think is from /etc/mailname. better to set it to
708 # whatever the current fqdn is.
709 exim4-config exim4/mailname string $(hostname -f)
710 EOF
711
712 fi # end $HOSTNAME != $MAIL_HOST
713
714 # if we already have it installed, need to reconfigure, without being prompted
715 if dpkg -s exim4-config &>/dev/null; then
716 # gotta remove this, otherwise the set-selections are completely
717 # ignored. It woulda been nice if this was documented somewhere!
718 rm -f /etc/exim4/update-exim4.conf.conf
719 dpkg-reconfigure -u -fnoninteractive exim4-config
720 fi
721
722 # i have the spool directory be common to distro multi-boot, so
723 # we need the uid to be the same. 608 cuz it's kind of in the middle
724 # of the free system uids.
725 IFS=:; read _ _ uid _ < <(getent passwd Debian-exim ); unset IFS
726 IFS=:; read _ _ gid _ < <(getent group Debian-exim ); unset IFS
727 if [[ ! $uid ]]; then
728 # from /var/lib/dpkg/info/exim4-base.postinst, plus uid and gid options
729 adduser --uid 608 --gid 608 --system --group --quiet --home /var/spool/exim4 \
730 --no-create-home --disabled-login --force-badname Debian-exim
731 elif [[ $uid != 608 ]]; then
732 systemctl stop exim4 ||:
733 usermod -u 608 Debian-exim
734 groupmod -g 608 Debian-exim
735 usermod -g 608 Debian-exim
736 find / /nocow -xdev -uid $uid -exec chown -h 608 {} +
737 find / /nocow -xdev -gid $gid -exec chgrp -h 608 {} +
738 fi
739
740 # light version of exim does not have sasl auth support.
741 apt-get -y install --purge --auto-remove exim4-daemon-heavy spamassassin
742
743
744
745
746 ##### begin spamassassin config
747 systemctl enable spamassassin
748 # per readme.debian
749 sed -i '/^\s*CRON\s*=/d' /etc/default/spamassassin
750 e CRON=1 >>/etc/default/spamassassin
751 # just noticed this in the config file, seems like a good idea.
752 sed -i '/^\s*NICE\s*=/d' /etc/default/spamassassin
753 e 'NICE="--nicelevel 15"' >>/etc/default/spamassassin
754 systemctl start spamassassin
755 systemctl reload spamassassin
756
757 cat >/etc/systemd/system/spamddnsfix.service <<'EOF'
758 [Unit]
759 Description=spamd dns bug fix cronjob
760
761 [Service]
762 Type=oneshot
763 ExecStart=/a/bin/distro-setup/spamd-dns-fix
764 EOF
765 # 2017-09, debian closed the bug on this saying upstream had fixed it.
766 # remove this when i\'m using the newer package, ie, debian 10, or maybe
767 # ubuntu 18.04.
768 cat >/etc/systemd/system/spamddnsfix.timer <<'EOF'
769 [Unit]
770 Description=run spamd bug fix script every 10 minutes
771
772 [Timer]
773 OnActiveSec=60
774 # the script looks back 9 minutes into the journal,
775 # it takes a second to run,
776 # so lets run every 9 minutes and 10 seconds.
777 OnUnitActiveSec=550
778
779 [Install]
780 WantedBy=timers.target
781 EOF
782 systemctl daemon-reload
783 systemctl restart spamddnsfix.timer
784 systemctl enable spamddnsfix.timer
785 #
786 ##### end spamassassin config
787
788
789
790
791
792 cat >/etc/exim4/rcpt_local_acl <<'EOF'
793 # Only hosts we control send to mail.iankelling.org, so make sure
794 # they are all authed.
795 # Note, if we wanted authed senders for all domains,
796 # we could make this condition in acl_check_mail
797 deny
798 message = ian trusted domain recepient but no auth
799 !authenticated = *
800 domains = mail.iankelling.org
801 EOF
802 cat >/etc/exim4/data_local_acl <<'EOF'
803 # Except for the "condition =", this was
804 # a comment in the check_data acl. The comment about this not
805 # being suitable is mostly bs. The only thing related I found was to
806 # add the condition =, cuz spamassassin has problems with big
807 # messages and spammers don't bother with big messages,
808 # but I've increased the size from 10k
809 # suggested in official docs, and 100k in the wiki example because
810 # those docs are rather old and I see a 110k spam message
811 # pretty quickly looking through my spam folder.
812 warn
813 condition = ${if < {$message_size}{2000K}}
814 spam = Debian-exim:true
815 add_header = X-Spam_score: $spam_score\n\
816 X-Spam_score_int: $spam_score_int\n\
817 X-Spam_bar: $spam_bar\n\
818 X-Spam_report: $spam_report
819
820 EOF
821 cat >/etc/exim4/conf.d/auth/29_exim4-config_auth <<'EOF'
822 # from 30_exim4-config_examples
823
824 plain_server:
825 driver = plaintext
826 public_name = PLAIN
827 server_condition = "${if crypteq{$auth3}{${extract{1}{:}{${lookup{$auth2}lsearch{CONFDIR/passwd}{$value}{*:*}}}}}{1}{0}}"
828 server_set_id = $auth2
829 server_prompts = :
830 .ifndef AUTH_SERVER_ALLOW_NOTLS_PASSWORDS
831 server_advertise_condition = ${if eq{$tls_in_cipher}{}{}{*}}
832 .endif
833 EOF
834
835 cat >/etc/exim4/conf.d/router/900_exim4-config_local_user <<'EOF'
836 ### router/900_exim4-config_local_user
837 #################################
838
839 # This router matches local user mailboxes. If the router fails, the error
840 # message is "Unknown user".
841
842 local_user:
843 debug_print = "R: local_user for $local_part@$domain"
844 driver = accept
845 domains = +local_domains
846 # ian: commented this, in conjunction with a dovecot lmtp
847 # change so I get mail for all users.
848 # check_local_user
849 local_parts = ! root
850 transport = LOCAL_DELIVERY
851 cannot_route_message = Unknown user
852 EOF
853 cat >/etc/exim4/conf.d/transport/30_exim4-config_dovecot_lmtp <<'EOF'
854 dovecot_lmtp:
855 driver = lmtp
856 socket = /var/run/dovecot/lmtp
857 #maximum number of deliveries per batch, default 1
858 batch_max = 200
859 EOF
860
861 cat >/etc/exim4/conf.d/router/190_exim4-config_fsfsmarthost <<'EOF'
862 # smarthost for fsf mail
863 # ian: copied from /etc/exim4/conf.d/router/200_exim4-config_primary, and added senders = and
864 # replaced DCsmarthost with mail.fsf.org
865 fsfsmarthost:
866 debug_print = "R: smarthost for $local_part@$domain"
867 driver = manualroute
868 domains = ! +local_domains
869 senders = *@fsf.org
870 transport = remote_smtp_smarthost
871 route_list = * mail.fsf.org byname
872 host_find_failed = ignore
873 same_domain_copy_routing = yes
874 no_more
875 EOF
876
877 # https://blog.dhampir.no/content/make-exim4-on-debian-respect-forward-and-etcaliases-when-using-a-smarthost
878 # i only need .forwards, so just doing that one.
879 cd /etc/exim4/conf.d/router
880 b=userforward_higher_priority
881 # replace the router name so it is unique
882 sed -r s/^\\S+:/$b:/ 600_exim4-config_userforward >175_$b
883
884 # begin setup passwd.client
885 f=/etc/exim4/passwd.client
886 rm -f /etc/exim4/passwd.client
887 install -m 640 -g Debian-exim /dev/null $f
888 cat /etc/mailpass| while read -r domain port pass; do
889 # reference: exim4_passwd_client(5)
890 printf "%s:%s\n" "$domain" "$pass" >>$f
891 done
892 # end setup passwd.client
893
894 systemctl restart exim4
895
896 fi #### end if exim4
897
898 # /etc/alias setup is debian specific, and
899 # exim config sets up an /etc/alias from root to the postmaster, which i
900 # config to ian, as long as there exists an entry for root, or there was
901 # no preexisting aliases file. based on the postinst file. postfix
902 # won\'t set up a root to $postmaster alias if it\'s already installed.
903 # Since postfix is not the greatest, just set it ourselves.
904 if [[ $postmaster != root ]]; then
905 sed -i --follow-symlinks -f - /etc/aliases <<EOF
906 \$a root: $postmaster
907 /^root:/d
908 EOF
909 newaliases
910 fi
911
912 # put spool dir in directory that spans multiple distros.
913 # based on http://www.postfix.org/qmgr.8.html and my notes in gnus
914 #
915 # todo: I\'m suspicious of uids for Debian-exim being the same across
916 # distros. It would be good to test this.
917 dir=/nocow/$type
918 sdir=/var/spool/$type
919 # we only do this if our system has $dir
920 if [[ -e /nocow && $(readlink -f $sdir) != $dir ]]; then
921 systemctl stop $type
922 if [[ ! -e $dir && -d $sdir ]]; then
923 mv $sdir $dir
924 fi
925 /a/exe/lnf -T $dir $sdir
926 fi
927
928 systemctl restart $type
929 systemctl enable $type
930
931 # MAIL_HOST also does radicale, and easier to start and stop it here
932 # for when MAIL_HOST changes, so radicale gets the synced files and
933 # does not stop us from remounting /o.
934 if dpkg -s radicale &>/dev/null; then
935 if [[ $HOSTNAME == $MAIL_HOST ]]; then
936 systemctl restart radicale
937 systemctl enable radicale
938 if [[ -e /etc/logrotate.d/radicale.disabled ]]; then
939 mv /etc/logrotate.d/radicale{.disabled,}
940 fi
941 else
942 systemctl stop radicale
943 systemctl disable radicale
944 # weekly logrotate tries to restart radicale even if it's a disabled service in flidas.
945 if [[ -e /etc/logrotate.d/radicale ]]; then
946 mv /etc/logrotate.d/radicale{,.disabled}
947 fi
948 fi
949 fi
950 exit 0
951
952 # if I wanted the from address to be renamed and sent to a different address,
953 # echo "sdx@localhost development@localhost" | sudo dd of=/etc/postfix/recipient_canonical
954 # sudo postmap hash:/etc/postfix/recipient_canonical
955 # sudo service postfix reload