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