#!/bin/bash -l # Copyright (C) 2016 Ian Kelling # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # http://www.apache.org/licenses/LICENSE-2.0 # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. set -eE -o pipefail trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR ####### begin perstent password instructions ###### # # exim passwords: # # for hosts which have all private files I just use the same user # # for other hosts, each one get\'s their own password. # # for generating secure pass, and storing for server too: # # user=USUALLY_SAME_AS_HOSTNAME # user=li # f=$(mktemp) # apg -m 50 -x 70 -n 1 -a 1 -M CLN >$f # s sed -i "/^$user:/d" /p/c/filesystem/etc/exim4/passwd # echo "$user:$(mkpasswd -m sha-512 -s <$f)" >>/p/c/filesystem/etc/exim4/passwd # echo "mail.iankelling.org:$user:$(<$f)" >> /p/c/machine_specific/$user/filesystem/etc/mailpass # # then run this script, or part of it which uses /etc/mailpass # # dovecot password, i just need 1 as I\'m the only user # mkdir /p/c/filesystem/etc/dovecot # echo "ian:$(doveadm pw -s ssha256)::::::" >/p/c/filesystem/etc/dovecot/users # conflink # # for ad-hoc testing of some random new host sending mail: # user=li # client host username & hostname # f=$(mktemp) # apg -m 50 -x 70 -n 1 -a 1 -M CLN >$f # s sed -i "/^$user:/d" /etc/exim4/passwd # echo "$user:$(mkpasswd -m sha-512 -s <$f)" | s tee -a /etc/exim4/passwd # echo "mail.iankelling.org:$user:$(<$f)" | ssh root@$user dd of=/etc/exim4/passwd.client ####### end perstent password instructions ###### ####### begin persistent dkim/dns instructions ######### # # Remove 1 level of comments in this section, set the domain var # # for the domain you are setting up, then run this and copy dns settings # # into dns. # domain=iankelling.org # c /p/c/filesystem/etc/exim4 # # this has several bugs addressed in comments, but it was helpful # # https://debian-administration.org/article/718/DKIM-signing_outgoing_mail_with_exim4 # openssl genrsa -out $domain-private.pem 2048 -outform PEM # openssl rsa -in $domain-private.pem -out $domain.pem -pubout -outform PEM # # selector is needed for having multiple keys for one domain. # # I dun do that, so just use a static one: li # echo "txt record name: li._domainkey.$domain" # # Debadmin page does not have v=, fastmail does, and this # # says it\'s recommended in 3.6.1, default is DKIM1 anyways. # # https://www.ietf.org/rfc/rfc6376.txt # # Join and print all but first and last line. # # last line: swap hold & pattern, remove newlines, print. # # lines 2+: append to hold space # echo "txt record contents:" # echo "v=DKIM1; k=rsa; p=$(sed -n '${x;s/\n//gp};2,$H' $domain.pem)" # chmod 644 $domain.pem # chmod 640 $domain-private.pem # # in conflink, we chown these to group debian # conflink # # selector was also put into /etc/exim4/conf.d/main/000_localmacros, # # via the mail-setup scripts # # 2017-02 dmarc policies: # # yahoo: p=reject, hotmail: p=none, gmail: p=none, fastmail none for legacy reasons # # gmail will be changing to p=reject, which is expected to cause problems # # with a few old mailing lists, copying theirs for now. # echo "dmarc dns, name: _dmarc value: v=DMARC1; p=none; rua=mailto:mailauth-reports@$domain" # # 2017-02 spf policies: # # google ~all, hotmail -all, yahoo: ?all, fastmail ?all # # i include fastmail\'s settings, per their instructions, # # and follow their policy. In mail in a box, or similar instructions, # # I\'ve seen recommended to not use a restrictive policy. # echo "spf dns: name is empty, value: v=spf1 a include:spf.messagingengine.com ?all" # # to check if dns has updated, you do # host -a mesmtp._domainkey.$domain # # mx records, # # setting it to iankelling.org would work the same, but this # # is more flexible, I could change where mail.iankelling.org pointed. # cat <<'EOF' # mx records, 2 records each, for * and empty domain # pri 10 mail.iankelling.org # pri 20 in1-smtp.messagingengine.com # pri 30 in2-smtp.messagingengine.com # EOF ####### end persistent dkim instructions ######### # misc exim notes: # useful exim docs: # /usr/share/doc/exim4-base/README.Debian.gz # /usr/share/doc/exim4-base/spec.txt.gz # routers, transports, and authenticators are sections, and you define # driver instances in those sections, and the manual calls them driver # types but there is also a more specific "type" of driver, which is specified # with the driver = some_module setting in the driver. # the driver option must precede and private options (options that are # specific to that driver), so follow example of putting it at beginning. # The full list of option settings for any particular driver instance, # including all the defaulted values, can be extracted by making use of # the -bP command line option. # exim clear out message queue. as root: # adapted from somewhere on stackoverflow. # ser stop exim4; sleep 1; exim -bp | exiqgrep -i | xargs exim -Mrm; ser start exim4 # fastmail has changed their smtp server, but the old one still works, # I see no reason to bother changing. # New one is smtp.fastmail.com # test delivery & rewrite settings: #exim4 -bt ian@localhost type=$1 postfix() { [[ $type == postfix ]]; } exim() { [[ $type == exim4 ]]; } if ! exim && ! postfix; then echo "$1: error: expected exim4 or postfix as first arg" exit 1 fi local_mx=mail.iankelling.org host=$local_mx relayhost="[$host]:25" # postfix smarthost="$host::25" # exim # this was for when I used the exim config type # "mail sent by smarthost; received via SMTP or fetchmail" # if [[ $HOSTNAME == $MAIL_HOST ]]; then # host=mail.messagingengine.com # relayhost="[$host]:587" # postfix # smarthost="$host::587" # exim # fi forward=ian@$local_mx if [[ $HOSTNAME == $MAIL_HOST ]]; then # if we are MAIL_HOST, exim config sets up an /etc/alias from # root to the postmaster, which i config to ian, as long as there # exists an entry for root, or there was no preexisting aliases file. # based on the postinst file. s rm -f /etc/aliases else # linode image has a root alias, I think it might override our .forward sudo sed -i '/^root:/d' /etc/aliases s newaliases # background: This also works instead of ~/.forward # s sed -i --follow-symlinks '/^root/d' /etc/aliases ||: #echo "root: $HOSTNAME@$SOME_DOMAIN" | s tee -a /etc/aliases # this can\'t be a symlink and has permission restrictions # it might work in /etc/aliases, but this seems more proper. e $forward > ~/.forward e $forward | s tee /root/.forward # 644 is required. shouldn\'t need changing, but set it just in case. s chmod 644 ~/.forward /root/.forward fi # offlineimap uses this too, it is much easier to use one location than to # condition it\'s config and postfix\'s config case $distro in fedora) s lnf -T ca-certificates.crt /etc/ssl/ca-bundle.trust.crt ;; *) : esac if postfix; then # dunno why, but debian installed postfix with builddep emacs # but I will just explicitly install it here since # I use it for sending mail in emacs. if isdeb; then s debconf-set-selections </dev/null done s postmap hash:/etc/postfix/sasl_passwd s service postfix reload else # exim. has debian specific stuff for now # wording of question from dpkg-reconfigure exim4-config # 1. internet site; mail is sent and received directly using SMTP # 2. mail sent by smarthost; received via SMTP or fetchmail # 3. mail sent by smarthost; no local mail # 4. local delivery only; not on a network # 5. no configuration at this time # # Note, I have used option 2 in the past for receiving mail # from lan hosts, sending external mail via another smtp server. # # Note, other than configtype, we could set all the options in # both types of configs without harm, they would either be # ignored or be disabled by other settings, but the default # local_interfaces definitely makes things more secure. # most of these settings get translated into settings # in /etc/exim4/update-exim4.conf.conf # mailname setting sets /etc/mailname s debconf-set-selections </dev/null <<'EOF' MAIN_TLS_ENABLE = true DKIM_CANON = relaxed DKIM_SELECTOR = li # from comments in # https://debian-administration.org/article/718/DKIM-signing_outgoing_mail_with_exim4 # The file is based on the outgoing domain-name in the from-header. DKIM_DOMAIN = ${lc:${domain:$h_from:}} # sign if key exists DKIM_PRIVATE_KEY= ${if exists{/etc/exim4/${dkim_domain}-private.pem} {/etc/exim4/${dkim_domain}-private.pem}} # failing message on mail-tester.com: # We check if there is a server (A Record) behind your hostname treetowl. # You may want to publish a DNS record (A type) for the hostname treetowl or use a different hostname in your mail software # https://serverfault.com/questions/46545/how-do-i-change-exim4s-primary-hostname-on-a-debian-box # and this one seemed appropriate from grepping config MAIN_HARDCODE_PRIMARY_HOSTNAME = li.iankelling.org # normally empty, I set this so I can set the envelope address # when doing mail redelivery to invoke filters MAIN_TRUSTED_GROUPS = ian LOCAL_DELIVERY = dovecot_lmtp CHECK_RCPT_LOCAL_ACL_FILE = /etc/exim4/rcpt_local_acl EOF s dd of=/etc/systemd/system/offlineimapsync.timer <<'EOF' [Unit] Description=Run offlineimap-sync once every 5 mins [Timer] OnCalendar=*:0/5 [Install] WantedBy=timers.target EOF s dd of=/etc/systemd/system/offlineimapsync.service <<'EOF' [Unit] Description=Offlineimap sync After=multi-user.target [Service] User=ian Type=oneshot ExecStart=/a/bin/log-quiet/sysd-mail-once offlineimap-sync /a/bin/distro-setup/offlineimap-sync EOF s systemctl daemon-reload s systemctl enable offlineimapsync.timer s systemctl start offlineimapsync.timer else # $HOSTNAME != $MAIL_HOST s systemctl disable offlineimapsync.timer &>/dev/null ||: s systemctl stop offlineimapsync.timer &>/dev/null ||: # # # would only exist because I wrote it i the previous condition, # it\'s not part of exim s rm -f $exim_main_dir/000_localmacros s debconf-set-selections </dev/null; then # gotta remove this, otherwise the set-selections are completely # ignored. It woulda been nice if this was documented somewhere! s rm -f /etc/exim4/update-exim4.conf.conf s dpkg-reconfigure -u -fnoninteractive exim4-config fi # light version does not have sasl auth support. pi exim4-daemon-heavy spamassassin ##### begin spamassassin config ser enable spamassassin # per readme.debian s sed -i '/^\s*CRON\s*=/d' /etc/default/spamassassin s tee -a /etc/default/spamassassin <</dev/null done # end setup passwd.client # https://blog.dhampir.no/content/make-exim4-on-debian-respect-forward-and-etcaliases-when-using-a-smarthost # i only need .forwards, so just doing that one. cd /etc/exim4/conf.d/router a=userforward b=${a}_higher_priority tmp=$(mktemp) of=175_$b # sed to make the router name unique sed -r s/^\\S+:/$b:/ 600_exim4-config_$a | s dd of=$tmp 2>/dev/null if ! diff -q $tmp $of &>/dev/null; then s dd if=$tmp of=$of >/dev/null fi ser restart exim4 fi # based on http://www.postfix.org/qmgr.8.html and my notes in gnus dir=/nocow/$type sdir=/var/spool/$type if [[ $(readlink -f $sdir) != $dir ]]; then ser stop $type if [[ ! -e $dir && -d $sdir ]]; then s mv $sdir $dir fi s lnf -T $dir $sdir fi sgo $type # if I wanted the from address to be renamed and sent to a different address, # echo "sdx@localhost development@localhost" | sudo dd of=/etc/postfix/recipient_canonical # sudo postmap hash:/etc/postfix/recipient_canonical # sudo service postfix reload