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