own mailserver, lots of improvements
[distro-setup] / mail-setup
index b43bde401ff2d1227741475b02a4cf2bea82e041..da91b1cf9a013846f7ee15a4aed51b1bee16c9e3 100755 (executable)
 # 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:
 #exim4 -bt ian@localhost
 
 
-set -eE -o pipefail
-trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
-
 type=$1
 postfix() { [[ $type == postfix ]]; }
 exim() { [[ $type == exim4 ]]; }
@@ -56,15 +143,19 @@ fi
 
 
 local_mx=mail.iankelling.org
-if [[ $HOSTNAME == $MAIL_HOST ]]; then
-    host=mail.messagingengine.com
-    relayhost="[$host]:587" # postfix
-    smarthost="$host::587" # exim
-else
-    host=$local_mx
-    relayhost="[$host]:25" # postfix
-    smarthost="$host::25" # exim
-fi
+
+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
 
@@ -144,9 +235,7 @@ EOF
     s service postfix reload
 
 else # exim. has debian specific stuff for now
-    # debconf settings will not work if packages are already installed,
-    # such as on vps images.
-    pu exim4-daemon-light exim4-daemon-heavy exim4-config exim4-base exim4
+
 
     # wording of question from dpkg-reconfigure exim4-config
     # 1. internet site; mail is sent and received directly using SMTP
@@ -155,30 +244,256 @@ else # exim. has debian specific stuff for now
     # 4. local delivery only; not on a network
     # 5. no configuration at this time
     #
-    # only the one receiving host needs option 2, the rest can do option
-    # 1, but that host might change, so we pick option 2 and later don't
-    # set it up to receive anything, because it has nothing in it's
-    # receiving password file and we make that mandatory. Also, only
-    # receiving host needs dc_other_hostnames and beyond, but no harm.
-
-    # note, another related setting is /etc/mailname, which
-    # is set to be $HOSTNAME.lan on stretch. this may need to be
-    # setup on other distros.
-
-    # setting local_interfaces to empty listens on all interfaces.
-    # default is 127.0.0.1 ; ::1, so only listen to lo interface.
+    # 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 <<EOF
-exim4-config exim4/dc_eximconfig_configtype select mail sent by smarthost; received via SMTP or fetchmail
-exim4-config exim4/dc_smarthost string $smarthost
 exim4-config exim4/use_split_config boolean true
-exim4-config exim4/dc_other_hostnames string mail.iankelling.org
+EOF
+
+    source /a/bin/bash_unpublished/source-semi-priv
+    exim_main_dir=/etc/exim4/conf.d/main
+    s mkdir -p exim_main_dir
+    if [[ $HOSTNAME == $MAIL_HOST ]]; then
+        # afaik, these will get ignored, routing to my own machine, but rm
+        # them to make me feel better.
+        s rm -f ~/.forward /root/.forward
+
+        s debconf-set-selections <<EOF
+# Mail Server configuration
+# -------------------------
+
+# Please select the mail server configuration type that best meets your needs.
+
+# Systems with dynamic IP addresses, including dialup systems, should generally be
+# configured to send outgoing mail to another machine, called a 'smarthost' for
+# delivery because many receiving systems on the Internet block incoming mail from
+# dynamic IP addresses as spam protection.
+
+# A system with a dynamic IP address can receive its own mail, or local delivery can be
+# disabled entirely (except mail for root and postmaster).
+
+#   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
+
+# General type of mail configuration: 1
+exim4-config exim4/dc_eximconfig_configtype select internet site; mail is sent and received directly using SMTP
+
+
+
+# The 'mail name' is the domain name used to 'qualify' mail addresses without a domain
+# name.
+
+# This name will also be used by other programs. It should be the single, fully
+# qualified domain name (FQDN).
+
+# Thus, if a mail address on the local host is foo@example.org, the correct value for
+# this option would be example.org.
+
+# This name won't appear on From: lines of outgoing messages if rewriting is enabled.
+
+# System mail name:
+exim4-config exim4/mailname string li.iankelling.org
+
+
+
+
+# Please enter a semicolon-separated list of recipient domains for which this machine
+# should consider itself the final destination. These domains are commonly called
+# 'local domains'. The local hostname (treetowl.lan) and 'localhost' are always added
+# to the list given here.
+
+# By default all local domains will be treated identically. If both a.example and
+# b.example are local domains, acc@a.example and acc@b.example will be delivered to the
+# same final destination. If different domain names should be treated differently, it
+# is necessary to edit the config files afterwards.
+
+# Other destinations for which mail is accepted:
+# iank.bid is for testing
+# mail.iankelling.org is for machines i own
+exim4-config exim4/dc_other_hostnames string mail.iankelling.org;iank.bid;iankelling.org;zroe.org
+
+
+
+
+# Please enter a semicolon-separated list of IP addresses. The Exim SMTP listener
+# daemon will listen on all IP addresses listed here.
+
+# An empty value will cause Exim to listen for connections on all available network
+# interfaces.
+
+# If this system only receives mail directly from local services (and not from other
+# hosts), it is suggested to prohibit external connections to the local Exim daemon.
+# Such services include e-mail programs (MUAs) which talk to localhost only as well as
+# fetchmail. External connections are impossible when 127.0.0.1 is entered here, as
+# this will disable listening on public network interfaces.
+
+# IP-addresses to listen on for incoming SMTP connections:
 exim4-config exim4/dc_local_interfaces string
+
+
+
+
+# Mail for the 'postmaster', 'root', and other system accounts needs to be redirected
+# to the user account of the actual system administrator.
+
+# If this value is left empty, such mail will be saved in /var/mail/mail, which is not
+# recommended.
+
+# Note that postmaster's mail should be read on the system to which it is directed,
+# rather than being forwarded elsewhere, so (at least one of) the users listed here
+# should not redirect their mail off this machine. A 'real-' prefix can be used to
+# force local delivery.
+
+# Multiple user names need to be separated by spaces.
+
+# Root and postmaster mail recipient:
 exim4-config exim4/dc_postmaster string ian
+
+
+
+# Exim is able to store locally delivered email in different formats. The most commonly
+# used ones are mbox and Maildir. mbox uses a single file for the complete mail folder
+# stored in /var/mail/. With Maildir format every single message is stored in a
+# separate file in ~/Maildir/.
+
+# Please note that most mail tools in Debian expect the local delivery method to be
+# mbox in their default.
+
+#   1. mbox format in /var/mail/  2. Maildir format in home directory
+
+# Delivery method for local mail: 2
 exim4-config exim4/dc_localdelivery select Maildir format in home directory
 EOF
+        # MAIN_HARDCODE_PRIMARY_HOSTNAME might mess up the
+        # smarthost config type, not sure. all other settings
+        # would be unused in that config type.
+        s dd of=$exim_main_dir/000_localmacros 2>/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
+
+# disabled, didn't finished configuring
+#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]
+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 <<EOF
+exim4-config exim4/dc_eximconfig_configtype select mail sent by smarthost; no local mail
+exim4-config exim4/dc_smarthost string $smarthost
+EOF
+    fi
+
+    # if we already have it installed, need to reconfigure, without being prompted
+    if ! dpkg -s exim4-config &>/dev/null; then
+        s dpkg-reconfigure -u -pcritical exim4-config
+    fi
     # light version does not have sasl auth support.
-    pi exim4-daemon-heavy
+    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 <<<CRON=1
+    # just noticed this in the config file, seems like a good idea.
+    s sed -i '/^\s*NICE\s*=/d' /etc/default/spamassassin
+    s tee -a /etc/default/spamassassin <<<'NICE="--nicelevel 15"'
+
+    s dd of=/etc/systemd/system/spamddnsfix.service <<'EOF'
+[Unit]
+Description=spamd dns bug fix cronjob
+
+[Service]
+Type=oneshot
+ExecStart=/a/bin/distro-setup/spamd-dns-fix
+EOF
+    s dd of=/etc/systemd/system/spamddnsfix.timer <<'EOF'
+[Unit]
+Description=run spamd bug fix script every 10 minutes
+
+[Timer]
+# the script looks back 9 minutes into the journal,
+# it takes a second to run,
+# so lets run every 9 minutes and 10 seconds.
+OnActiveSec=550
 
+[Install]
+WantedBy=timers.target
+EOF
+    ser daemon-reload
+    sgo spamddnsfix.timer
+    #
+    #####   end spamassassin config
+
+    gitslink # needed to install the execstart files below
     s dd of=/etc/systemd/system/mailcert.service <<'EOF'
 [Unit]
 Description=Mail cert rsync
@@ -186,7 +501,7 @@ After=multi-user.target
 
 [Service]
 Type=oneshot
-ExecStart=/a/bin/log-quiet/sysd-mail-once mailcert /a/bin/distro-setup/mail-cert-cron
+ExecStart=/usr/local/bin/sysd-mail-once mailcert /usr/local/bin/mail-cert-cron
 EOF
 
     s dd of=/etc/systemd/system/mailcert.timer <<'EOF'
@@ -199,32 +514,46 @@ OnCalendar=daily
 [Install]
 WantedBy=timers.target
 EOF
+    ser daemon-reload
     ser start mailcert
     sgo mailcert.timer
 
+
+    # based on a little google and package search, just the dovecot
+    # packages we need instead of dovecot-common.
+    #
+    # dovecot-lmtpd is for exim to deliver to dovecot instead of maildir
+    # directly.  The reason to do this is to use dovecot's sieve, which
+    # has extensions that allow it to be almost equivalent to exim's
+    # filter capabilities, some ways probably better, some worse, and
+    # sieve has the benefit of being supported in postfix and
+    # proprietary/weird environments, so there is more examples on the
+    # internet. I was torn about whether to do this or not, meh.
+    pi dovecot-core dovecot-imapd dovecot-sieve dovecot-lmtpd
+
+    # if we changed 90-sieve.conf and removed the active part of the
+    # sieve option, we wouldn't need this, but I'd rather not modify a
+    # default config if not needed. This won't work as a symlink in /a/c
+    # unfortunately.
+    lnf -T sieve/main.sieve ~/.dovecot.sieve
+
+    # we have a few config files which installing exim/dovecot overwrites,
+    # and might as well have this before reading /etc/mailpass,
+    # which this sets up too.
+    conflink
+
+    # begin setup passwd.client
     f=/etc/exim4/passwd.client
     s rm -f $f
     s touch $f
     s chmod 640 $f
     s chown root:Debian-exim $f
-    # generating secure pass, and storing for server too:
-    # user=USUALLY_SAME_AS_HOSTNAME
-    # f=$(mktemp)
-    # apg -m 50 -x 70 -n 1 -a 1 -M CLN >$f
-    # 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
-    #
-    # for ad-hoc testing of some random new host:
-    # host=testhost # client host username & hostname
-    # f=$(mktemp)
-    # apg -m 50 -x 70 -n 1 -a 1 -M CLN >$f
-    # s sed "/^host:/d" /etc/exim4/passwd
-    # echo "$host:$(mkpasswd -m sha-512 -s <$f)" | s tee -a /etc/exim4/passwd
-    # echo "mail.iankelling.org:$host:$(<$f)" | ssh root@$host dd of=/etc/exim4/passwd.client
+    # note: this will go away
     s cat /etc/mailpass| while read -r domain port pass; do
         # reference: exim4_passwd_client(5)
-        printf "%s:%s" "$domain" "$pass" | s tee -a $f >/dev/null
+        printf "%s:%s\n" "$domain" "$pass" | s tee -a $f >/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.
@@ -237,33 +566,36 @@ EOF
     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
-        ser restart exim4
     fi
-fi
 
 
+    ser restart exim4
+
+    fi
 
-# linode image has a root alias. completely useless, remove it.
-sudo sed -i '/^root:/d' /etc/aliases
 
-s newaliases
 
+    # linode image has a root alias. completely useless, remove it.
+    sudo sed -i '/^root:/d' /etc/aliases
 
-# 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
+    s newaliases
+
+
+    # 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
-    s lnf -T $dir $sdir
-fi
 
-sgo $type
+    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
+    # 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