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