Merge independent repo conflink into this one
[distro-setup] / mail-setup
1 #!/bin/bash -l
2 # Copyright (C) 2016 Ian Kelling
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 set -eE -o pipefail
17 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
18
19 ####### begin perstent password instructions ######
20 # # exim passwords:
21 # # for hosts which have all private files I just use the same user
22 # # for other hosts, each one get\'s their own password.
23 # # for generating secure pass, and storing for server too:
24 # # user=USUALLY_SAME_AS_HOSTNAME
25 # user=li
26 # f=$(mktemp)
27 # apg -m 50 -x 70 -n 1 -a 1 -M CLN >$f
28 # s sed -i "/^$user:/d" /p/c/filesystem/etc/exim4/passwd
29 # echo "$user:$(mkpasswd -m sha-512 -s <$f)" >>/p/c/filesystem/etc/exim4/passwd
30 # echo "mail.iankelling.org:$user:$(<$f)" >> /p/c/machine_specific/$user/filesystem/etc/mailpass
31 # # then run this script, or part of it which uses /etc/mailpass
32
33 # # dovecot password, i just need 1 as I\'m the only user
34 # mkdir /p/c/filesystem/etc/dovecot
35 # echo "ian:$(doveadm pw -s ssha256)::::::" >/p/c/filesystem/etc/dovecot/users
36 # conflink
37
38
39
40 # # for ad-hoc testing of some random new host sending mail:
41 # user=li # client host username & hostname
42 # f=$(mktemp)
43 # apg -m 50 -x 70 -n 1 -a 1 -M CLN >$f
44 # s sed -i "/^$user:/d" /etc/exim4/passwd
45 # echo "$user:$(mkpasswd -m sha-512 -s <$f)" | s tee -a /etc/exim4/passwd
46 # echo "mail.iankelling.org:$user:$(<$f)" | ssh root@$user dd of=/etc/exim4/passwd.client
47 ####### end perstent password instructions ######
48
49
50 ####### begin persistent dkim/dns instructions #########
51 # # Remove 1 level of comments in this section, set the domain var
52 # # for the domain you are setting up, then run this and copy dns settings
53 # # into dns.
54 # domain=iankelling.org
55 # c /p/c/filesystem/etc/exim4
56 # # this has several bugs addressed in comments, but it was helpful
57 # # https://debian-administration.org/article/718/DKIM-signing_outgoing_mail_with_exim4
58
59 # openssl genrsa -out $domain-private.pem 2048 -outform PEM
60 # openssl rsa -in $domain-private.pem -out $domain.pem -pubout -outform PEM
61 # # selector is needed for having multiple keys for one domain.
62 # # I dun do that, so just use a static one: li
63 # echo "txt record name: li._domainkey.$domain"
64 # # Debadmin page does not have v=, fastmail does, and this
65 # # says it\'s recommended in 3.6.1, default is DKIM1 anyways.
66 # # https://www.ietf.org/rfc/rfc6376.txt
67 # # Join and print all but first and last line.
68 # # last line: swap hold & pattern, remove newlines, print.
69 # # lines 2+: append to hold space
70 # echo "txt record contents:"
71 # echo "v=DKIM1; k=rsa; p=$(sed -n '${x;s/\n//gp};2,$H' $domain.pem)"
72 # chmod 644 $domain.pem
73 # chmod 640 $domain-private.pem
74 # # in conflink, we chown these to group debian
75 # conflink
76 # # selector was also put into /etc/exim4/conf.d/main/000_localmacros,
77 # # via the mail-setup scripts
78
79 # # 2017-02 dmarc policies:
80 # # yahoo: p=reject, hotmail: p=none, gmail: p=none, fastmail none for legacy reasons
81 # # gmail will be changing to p=reject, which is expected to cause problems
82 # # with a few old mailing lists, copying theirs for now.
83 # echo "dmarc dns, name: _dmarc value: v=DMARC1; p=none; rua=mailto:mailauth-reports@$domain"
84
85 # # 2017-02 spf policies:
86 # # google ~all, hotmail -all, yahoo: ?all, fastmail ?all
87 # # i include fastmail\'s settings, per their instructions,
88 # # and follow their policy. In mail in a box, or similar instructions,
89 # # I\'ve seen recommended to not use a restrictive policy.
90 # echo "spf dns: name is empty, value: v=spf1 a include:spf.messagingengine.com ?all"
91
92 # # to check if dns has updated, you do
93 # host -a mesmtp._domainkey.$domain
94
95 # # mx records,
96 # # setting it to iankelling.org would work the same, but this
97 # # is more flexible, I could change where mail.iankelling.org pointed.
98 # cat <<'EOF'
99 # mx records, 2 records each, for * and empty domain
100 # pri 10 mail.iankelling.org
101 # pri 20 in1-smtp.messagingengine.com
102 # pri 30 in2-smtp.messagingengine.com
103 # EOF
104 ####### end persistent dkim instructions #########
105
106
107 # misc exim notes:
108 # useful exim docs:
109 # /usr/share/doc/exim4-base/README.Debian.gz
110 # /usr/share/doc/exim4-base/spec.txt.gz
111
112 # routers, transports, and authenticators are sections, and you define
113 # driver instances in those sections, and the manual calls them driver
114 # types but there is also a more specific "type" of driver, which is specified
115 # with the driver = some_module setting in the driver.
116
117 # the driver option must precede and private options (options that are
118 # specific to that driver), so follow example of putting it at beginning.
119
120 # The full list of option settings for any particular driver instance,
121 # including all the defaulted values, can be extracted by making use of
122 # the -bP command line option.
123
124 # exim clear out message queue. as root:
125 # adapted from somewhere on stackoverflow.
126 # ser stop exim4; sleep 1; exim -bp | exiqgrep -i | xargs exim -Mrm; ser start exim4
127
128 # fastmail has changed their smtp server, but the old one still works,
129 # I see no reason to bother changing.
130 # New one is smtp.fastmail.com
131
132 # test delivery & rewrite settings:
133 #exim4 -bt ian@localhost
134
135
136 type=$1
137 postfix() { [[ $type == postfix ]]; }
138 exim() { [[ $type == exim4 ]]; }
139 if ! exim && ! postfix; then
140 echo "$1: error: expected exim4 or postfix as first arg"
141 exit 1
142 fi
143
144
145 local_mx=mail.iankelling.org
146
147 host=$local_mx
148 relayhost="[$host]:25" # postfix
149 smarthost="$host::25" # exim
150
151
152 # this was for when I used the exim config type
153 # "mail sent by smarthost; received via SMTP or fetchmail"
154 # if [[ $HOSTNAME == $MAIL_HOST ]]; then
155 # host=mail.messagingengine.com
156 # relayhost="[$host]:587" # postfix
157 # smarthost="$host::587" # exim
158 # fi
159
160 forward=ian@$local_mx
161
162
163 if [[ $HOSTNAME == $MAIL_HOST ]]; then
164 # if we are MAIL_HOST, exim config sets up an /etc/alias from
165 # root to the postmaster, which i config to ian, as long as there
166 # exists an entry for root, or there was no preexisting aliases file.
167 # based on the postinst file.
168 s rm -f /etc/aliases
169 else
170 # linode image has a root alias, I think it might override our .forward
171 sudo sed -i '/^root:/d' /etc/aliases
172 s newaliases
173
174 # background: This also works instead of ~/.forward
175 # s sed -i --follow-symlinks '/^root/d' /etc/aliases ||:
176 #echo "root: $HOSTNAME@$SOME_DOMAIN" | s tee -a /etc/aliases
177 # this can\'t be a symlink and has permission restrictions
178 # it might work in /etc/aliases, but this seems more proper.
179 e $forward > ~/.forward
180 e $forward | s tee /root/.forward
181 # 644 is required. shouldn\'t need changing, but set it just in case.
182 s chmod 644 ~/.forward /root/.forward
183 fi
184
185 # offlineimap uses this too, it is much easier to use one location than to
186 # condition it\'s config and postfix\'s config
187 case $distro in
188 fedora) s lnf -T ca-certificates.crt /etc/ssl/ca-bundle.trust.crt ;;
189 *) :
190 esac
191
192 if postfix; then
193 # dunno why, but debian installed postfix with builddep emacs
194 # but I will just explicitly install it here since
195 # I use it for sending mail in emacs.
196 if isdeb; then
197 s debconf-set-selections <<EOF
198 postfix postfix/main_mailer_type select Satellite system
199 postfix postfix/mailname string $HOSTNAME
200 postfix postfix/relayhost string $relayhost
201 EOF
202
203 pi postfix
204 else
205 pi postfix
206 # Settings from reading the output when installing on debian,
207 # then seeing which were different in a default install on arch.
208 # I assume the same works for fedora.
209 postconfin <<EOF
210 mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
211 mailbox_size_limit = 0
212 relayhost = $relayhost
213 inet_interfaces = loopback-only
214 EOF
215
216 s systemctl enable postfix
217 s systemctl start postfix
218 fi
219 # i\'m assuming mail just won\'t work on systems without the sasl_passwd.
220 postconfin <<'EOF'
221 smtp_sasl_auth_enable = yes
222 smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
223 smtp_sasl_security_options = noanonymous
224 smtp_tls_security_level = secure
225 message_size_limit = 20480000
226 smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
227 inet_protocols = ipv4
228 EOF
229 # msg_size_limit: I ran into a log file not sending cuz of size. double from 10 to 20 meg limit
230 # inet_protocols: without this, postfix tries an ipv6 lookup then gives
231 # up and fails. snippet from syslog: type=AAAA: Host not found, try again
232
233
234 f=/etc/postfix/sasl_passwd
235 s rm -f $f
236 s touch $f
237 s chmod 600 $f
238 s cat /etc/mailpass| while read -r domain port pass; do
239 # format: domain port user:pass
240 # mailpass is just a name i made up, since postfix and
241 # exim both use a slightly crazy format to translate to
242 # each other, it\'s easier to use my own format.
243 printf "[%s]:%s %s" "$domain" "$port" "${pass/@/#}" | s tee -a $f >/dev/null
244 done
245 s postmap hash:/etc/postfix/sasl_passwd
246 s service postfix reload
247
248 else # exim. has debian specific stuff for now
249
250
251 # wording of question from dpkg-reconfigure exim4-config
252 # 1. internet site; mail is sent and received directly using SMTP
253 # 2. mail sent by smarthost; received via SMTP or fetchmail
254 # 3. mail sent by smarthost; no local mail
255 # 4. local delivery only; not on a network
256 # 5. no configuration at this time
257 #
258 # Note, I have used option 2 in the past for receiving mail
259 # from lan hosts, sending external mail via another smtp server.
260 #
261 # Note, other than configtype, we could set all the options in
262 # both types of configs without harm, they would either be
263 # ignored or be disabled by other settings, but the default
264 # local_interfaces definitely makes things more secure.
265
266 # most of these settings get translated into settings
267 # in /etc/exim4/update-exim4.conf.conf
268 # mailname setting sets /etc/mailname
269
270 s debconf-set-selections <<EOF
271 exim4-config exim4/use_split_config boolean true
272 EOF
273
274 source /a/bin/bash_unpublished/source-semi-priv
275 exim_main_dir=/etc/exim4/conf.d/main
276 s mkdir -p exim_main_dir
277 if [[ $HOSTNAME == $MAIL_HOST ]]; then
278 # afaik, these will get ignored, routing to my own machine, but rm
279 # them to make me feel better.
280 s rm -f ~/.forward /root/.forward
281
282 s debconf-set-selections <<EOF
283 # Mail Server configuration
284 # -------------------------
285
286 # Please select the mail server configuration type that best meets your needs.
287
288 # Systems with dynamic IP addresses, including dialup systems, should generally be
289 # configured to send outgoing mail to another machine, called a 'smarthost' for
290 # delivery because many receiving systems on the Internet block incoming mail from
291 # dynamic IP addresses as spam protection.
292
293 # A system with a dynamic IP address can receive its own mail, or local delivery can be
294 # disabled entirely (except mail for root and postmaster).
295
296 # 1. internet site; mail is sent and received directly using SMTP
297 # 2. mail sent by smarthost; received via SMTP or fetchmail
298 # 3. mail sent by smarthost; no local mail
299 # 4. local delivery only; not on a network
300 # 5. no configuration at this time
301
302 # General type of mail configuration: 1
303 exim4-config exim4/dc_eximconfig_configtype select internet site; mail is sent and received directly using SMTP
304
305
306
307 # The 'mail name' is the domain name used to 'qualify' mail addresses without a domain
308 # name.
309
310 # This name will also be used by other programs. It should be the single, fully
311 # qualified domain name (FQDN).
312
313 # Thus, if a mail address on the local host is foo@example.org, the correct value for
314 # this option would be example.org.
315
316 # This name won\'t appear on From: lines of outgoing messages if rewriting is enabled.
317
318 # System mail name:
319 exim4-config exim4/mailname string li.iankelling.org
320
321
322
323
324 # Please enter a semicolon-separated list of recipient domains for which this machine
325 # should consider itself the final destination. These domains are commonly called
326 # 'local domains'. The local hostname (treetowl.lan) and 'localhost' are always added
327 # to the list given here.
328
329 # By default all local domains will be treated identically. If both a.example and
330 # b.example are local domains, acc@a.example and acc@b.example will be delivered to the
331 # same final destination. If different domain names should be treated differently, it
332 # is necessary to edit the config files afterwards.
333
334 # Other destinations for which mail is accepted:
335 # iank.bid is for testing
336 # mail.iankelling.org is for machines i own
337 exim4-config exim4/dc_other_hostnames string *.iankelling.org;iankelling.org;*iank.bid;iank.bid;*zroe.org;zroe.org
338
339
340
341
342 # Please enter a semicolon-separated list of IP addresses. The Exim SMTP listener
343 # daemon will listen on all IP addresses listed here.
344
345 # An empty value will cause Exim to listen for connections on all available network
346 # interfaces.
347
348 # If this system only receives mail directly from local services (and not from other
349 # hosts), it is suggested to prohibit external connections to the local Exim daemon.
350 # Such services include e-mail programs (MUAs) which talk to localhost only as well as
351 # fetchmail. External connections are impossible when 127.0.0.1 is entered here, as
352 # this will disable listening on public network interfaces.
353
354 # IP-addresses to listen on for incoming SMTP connections:
355 exim4-config exim4/dc_local_interfaces string
356
357
358
359
360 # Mail for the 'postmaster', 'root', and other system accounts needs to be redirected
361 # to the user account of the actual system administrator.
362
363 # If this value is left empty, such mail will be saved in /var/mail/mail, which is not
364 # recommended.
365
366 # Note that postmaster\'s mail should be read on the system to which it is directed,
367 # rather than being forwarded elsewhere, so (at least one of) the users listed here
368 # should not redirect their mail off this machine. A 'real-' prefix can be used to
369 # force local delivery.
370
371 # Multiple user names need to be separated by spaces.
372
373 # Root and postmaster mail recipient:
374 exim4-config exim4/dc_postmaster string ian
375
376
377
378 # Exim is able to store locally delivered email in different formats. The most commonly
379 # used ones are mbox and Maildir. mbox uses a single file for the complete mail folder
380 # stored in /var/mail/. With Maildir format every single message is stored in a
381 # separate file in ~/Maildir/.
382
383 # Please note that most mail tools in Debian expect the local delivery method to be
384 # mbox in their default.
385
386 # 1. mbox format in /var/mail/ 2. Maildir format in home directory
387
388 # Delivery method for local mail: 2
389 exim4-config exim4/dc_localdelivery select Maildir format in home directory
390 EOF
391 # MAIN_HARDCODE_PRIMARY_HOSTNAME might mess up the
392 # smarthost config type, not sure. all other settings
393 # would be unused in that config type.
394 s dd of=$exim_main_dir/000_localmacros 2>/dev/null <<'EOF'
395 MAIN_TLS_ENABLE = true
396
397 DKIM_CANON = relaxed
398 DKIM_SELECTOR = li
399
400 # from comments in
401 # https://debian-administration.org/article/718/DKIM-signing_outgoing_mail_with_exim4
402
403 # The file is based on the outgoing domain-name in the from-header.
404 DKIM_DOMAIN = ${lc:${domain:$h_from:}}
405 # sign if key exists
406 DKIM_PRIVATE_KEY= ${if exists{/etc/exim4/${dkim_domain}-private.pem} {/etc/exim4/${dkim_domain}-private.pem}}
407
408
409 # failing message on mail-tester.com:
410 # We check if there is a server (A Record) behind your hostname treetowl.
411 # You may want to publish a DNS record (A type) for the hostname treetowl or use a different hostname in your mail software
412 # https://serverfault.com/questions/46545/how-do-i-change-exim4s-primary-hostname-on-a-debian-box
413 # and this one seemed appropriate from grepping config
414 MAIN_HARDCODE_PRIMARY_HOSTNAME = li.iankelling.org
415
416 # normally empty, I set this so I can set the envelope address
417 # when doing mail redelivery to invoke filters
418 MAIN_TRUSTED_GROUPS = ian
419
420 LOCAL_DELIVERY = dovecot_lmtp
421
422 CHECK_RCPT_LOCAL_ACL_FILE = /etc/exim4/rcpt_local_acl
423 EOF
424
425
426 s dd of=/etc/systemd/system/offlineimapsync.timer <<'EOF'
427 [Unit]
428 Description=Run offlineimap-sync once every 5 mins
429
430 [Timer]
431 OnCalendar=*:0/5
432
433 [Install]
434 WantedBy=timers.target
435 EOF
436
437 s dd of=/etc/systemd/system/offlineimapsync.service <<'EOF'
438 [Unit]
439 Description=Offlineimap sync
440 After=multi-user.target
441
442 [Service]
443 User=ian
444 Type=oneshot
445 ExecStart=/a/bin/log-quiet/sysd-mail-once offlineimap-sync /a/bin/distro-setup/offlineimap-sync
446 EOF
447 s systemctl daemon-reload
448 s systemctl enable offlineimapsync.timer
449 s systemctl start offlineimapsync.timer
450
451 else # $HOSTNAME != $MAIL_HOST
452 s systemctl disable offlineimapsync.timer &>/dev/null ||:
453 s systemctl stop offlineimapsync.timer &>/dev/null ||:
454 #
455 #
456 # would only exist because I wrote it i the previous condition,
457 # it\'s not part of exim
458 s rm -f $exim_main_dir/000_localmacros
459 s debconf-set-selections <<EOF
460 exim4-config exim4/dc_eximconfig_configtype select mail sent by smarthost; no local mail
461 exim4-config exim4/dc_smarthost string $smarthost
462 EOF
463 fi
464
465 # if we already have it installed, need to reconfigure, without being prompted
466 if dpkg -s exim4-config &>/dev/null; then
467 # gotta remove this, otherwise the set-selections are completely
468 # ignored. It woulda been nice if this was documented somewhere!
469 s rm -f /etc/exim4/update-exim4.conf.conf
470 s dpkg-reconfigure -u -fnoninteractive exim4-config
471 fi
472 # light version does not have sasl auth support.
473 pi exim4-daemon-heavy spamassassin
474
475 ##### begin spamassassin config
476 ser enable spamassassin
477 # per readme.debian
478 s sed -i '/^\s*CRON\s*=/d' /etc/default/spamassassin
479 s tee -a /etc/default/spamassassin <<<CRON=1
480 # just noticed this in the config file, seems like a good idea.
481 s sed -i '/^\s*NICE\s*=/d' /etc/default/spamassassin
482 s tee -a /etc/default/spamassassin <<<'NICE="--nicelevel 15"'
483 ser reload spamassassin
484
485 s dd of=/etc/systemd/system/spamddnsfix.service <<'EOF'
486 [Unit]
487 Description=spamd dns bug fix cronjob
488
489 [Service]
490 Type=oneshot
491 ExecStart=/a/bin/distro-setup/spamd-dns-fix
492 EOF
493 s dd of=/etc/systemd/system/spamddnsfix.timer <<'EOF'
494 [Unit]
495 Description=run spamd bug fix script every 10 minutes
496
497 [Timer]
498 OnActiveSec=60
499 # the script looks back 9 minutes into the journal,
500 # it takes a second to run,
501 # so lets run every 9 minutes and 10 seconds.
502 OnUnitActiveSec=550
503
504 [Install]
505 WantedBy=timers.target
506 EOF
507 ser daemon-reload
508 sgo spamddnsfix.timer
509 #
510 ##### end spamassassin config
511
512 gitslink # needed to install the execstart files below
513 s dd of=/etc/systemd/system/mailcert.service <<'EOF'
514 [Unit]
515 Description=Mail cert rsync
516 After=multi-user.target
517
518 [Service]
519 Type=oneshot
520 ExecStart=/usr/local/bin/sysd-mail-once mailcert /usr/local/bin/mail-cert-cron
521 EOF
522
523 s dd of=/etc/systemd/system/mailcert.timer <<'EOF'
524 [Unit]
525 Description=Run mail-cert once a day
526
527 [Timer]
528 OnCalendar=daily
529
530 [Install]
531 WantedBy=timers.target
532 EOF
533 ser daemon-reload
534 ser start mailcert
535 sgo mailcert.timer
536
537
538 # based on a little google and package search, just the dovecot
539 # packages we need instead of dovecot-common.
540 #
541 # dovecot-lmtpd is for exim to deliver to dovecot instead of maildir
542 # directly. The reason to do this is to use dovecot\'s sieve, which
543 # has extensions that allow it to be almost equivalent to exim\'s
544 # filter capabilities, some ways probably better, some worse, and
545 # sieve has the benefit of being supported in postfix and
546 # proprietary/weird environments, so there is more examples on the
547 # internet. I was torn about whether to do this or not, meh.
548 pi dovecot-core dovecot-imapd dovecot-sieve dovecot-lmtpd
549
550 # if we changed 90-sieve.conf and removed the active part of the
551 # sieve option, we wouldn\'t need this, but I\'d rather not modify a
552 # default config if not needed. This won\'t work as a symlink in /a/c
553 # unfortunately.
554 lnf -T sieve/main.sieve ~/.dovecot.sieve
555
556 # we have a few config files which installing exim/dovecot overwrites,
557 # and might as well have this before reading /etc/mailpass,
558 # which this sets up too.
559 conflink
560
561 # begin setup passwd.client
562 f=/etc/exim4/passwd.client
563 s rm -f $f
564 s touch $f
565 s chmod 640 $f
566 s chown root:Debian-exim $f
567 # note: this will go away
568 s cat /etc/mailpass| while read -r domain port pass; do
569 # reference: exim4_passwd_client(5)
570 printf "%s:%s\n" "$domain" "$pass" | s tee -a $f >/dev/null
571 done
572 # end setup passwd.client
573
574 # https://blog.dhampir.no/content/make-exim4-on-debian-respect-forward-and-etcaliases-when-using-a-smarthost
575 # i only need .forwards, so just doing that one.
576 cd /etc/exim4/conf.d/router
577 a=userforward
578 b=${a}_higher_priority
579 tmp=$(mktemp)
580 of=175_$b
581 # sed to make the router name unique
582 sed -r s/^\\S+:/$b:/ 600_exim4-config_$a | s dd of=$tmp 2>/dev/null
583 if ! diff -q $tmp $of &>/dev/null; then
584 s dd if=$tmp of=$of >/dev/null
585 fi
586
587
588 ser restart exim4
589
590 fi
591
592
593
594
595
596 # based on http://www.postfix.org/qmgr.8.html and my notes in gnus
597 dir=/nocow/$type
598 sdir=/var/spool/$type
599 if [[ $(readlink -f $sdir) != $dir ]]; then
600 ser stop $type
601 if [[ ! -e $dir && -d $sdir ]]; then
602 s mv $sdir $dir
603 fi
604 s lnf -T $dir $sdir
605 fi
606
607 sgo $type
608
609
610 # if I wanted the from address to be renamed and sent to a different address,
611 # echo "sdx@localhost development@localhost" | sudo dd of=/etc/postfix/recipient_canonical
612 # sudo postmap hash:/etc/postfix/recipient_canonical
613 # sudo service postfix reload