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