minor updates
[distro-setup] / mail-setup
1 #!/bin/bash -l
2 # Copyright (C) 2016 Ian Kelling
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16
17 # misc exim notes:
18 # useful exim docs:
19 # /usr/share/doc/exim4-base/README.Debian.gz
20 # /usr/share/doc/exim4-base/spec.txt.gz
21
22 # routers, transports, and authenticators are sections, and you define
23 # driver instances in those sections, and the manual calls them driver
24 # types but there is also a more specific "type" of driver, which is specified
25 # with the driver = some_module setting in the driver.
26
27 # the driver option must precede and private options (options that are
28 # specific to that driver), so follow example of putting it at beginning.
29
30 # The full list of option settings for any particular driver instance,
31 # including all the defaulted values, can be extracted by making use of
32 # the -bP command line option.
33
34 # exim clear out message queue. as root:
35 # adapted from somewhere on stackoverflow.
36 # ser stop exim4; sleep 1; exim -bp | exiqgrep -i | xargs exim -Mrm; ser start exim4
37
38 # fastmail has changed their smtp server, but the old one still works,
39 # I see no reason to bother changing.
40 # New one is smtp.fastmail.com
41
42 # test delivery & rewrite settings:
43 #exim4 -bt ian@localhost
44
45
46 set -eE -o pipefail
47 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
48
49 type=$1
50 postfix() { [[ $type == postfix ]]; }
51 exim() { [[ $type == exim4 ]]; }
52 if ! exim && ! postfix; then
53 echo "$1: error: expected exim4 or postfix as first arg"
54 exit 1
55 fi
56
57 if private-host; then
58 host=mail.messagingengine.com
59 forward=$HOSTNAME@$PERSONAL_DOMAIN
60 else
61 # ses initially suggests port 25, but I had problems connecting to that.
62 host=email-smtp.us-west-2.amazonaws.com
63 forward=$HOSTNAME@$IMPERSONAL_DOMAIN
64 fi
65
66 relayhost="[$host]:587" # postfix
67 smarthost="$host::587" # exim
68
69 # background: This also works instead of ~/.forward
70 # s sed -i --follow-symlinks '/^root/d' /etc/aliases ||:
71 #echo "root: $HOSTNAME@$SOME_DOMAIN" | s tee -a /etc/aliases
72 # this can't be a symlink and has permission restrictions
73 # it might work in /etc/aliases, but this seems more proper.
74 e $forward > ~/.forward
75 e $forward | s tee /root/.forward
76 # exim log complains about 664 permissions.
77 s chmod 644 ~/.forward /root/.forward
78
79
80 # offlineimap uses this too, it is much easier to use one location than to
81 # condition it's config and postfix's config
82 case $distro in
83 fedora) s lnf -T ca-certificates.crt /etc/ssl/ca-bundle.trust.crt ;;
84 *) :
85 esac
86
87 read -r domain pass < <(s cat /etc/mailpass) # format: domain user:pass
88 if postfix; then
89 # dunno why, but debian installed postfix with builddep emacs
90 # but I will just explicitly install it here since
91 # I use it for sending mail in emacs.
92 if isdeb; then
93 s debconf-set-selections <<EOF
94 postfix postfix/main_mailer_type select Satellite system
95 postfix postfix/mailname string $HOSTNAME
96 postfix postfix/relayhost string $relayhost
97 EOF
98
99 pi postfix
100 else
101 pi postfix
102 # Settings from reading the output when installing on debian,
103 # then seeing which were different in a default install on arch.
104 # I assume the same works for fedora.
105 postconfin <<EOF
106 mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
107 mailbox_size_limit = 0
108 relayhost = $relayhost
109 inet_interfaces = loopback-only
110 EOF
111
112 s systemctl enable postfix
113 s systemctl start postfix
114 fi
115 # i'm assuming mail just won't work on systems without the sasl_passwd.
116 postconfin <<'EOF'
117 smtp_sasl_auth_enable = yes
118 smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
119 smtp_sasl_security_options = noanonymous
120 smtp_tls_security_level = secure
121 message_size_limit = 20480000
122 smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
123 inet_protocols = ipv4
124 EOF
125 # msg_size_limit: I ran into a log file not sending cuz of size. double from 10 to 20 meg limit
126 # inet_protocols: without this, postfix tries an ipv6 lookup then gives
127 # up and fails. snippet from syslog: type=AAAA: Host not found, try again
128
129
130 # mailpass is just a name i made up, since postfix and
131 # exim both use a slightly crazy format to translate to
132 # each other, it's easier to use my own format.
133 f=/etc/postfix/sasl_passwd
134 s touch $f
135 s chmod 600 $f
136 printf "[%s]:587 %s" "$domain" "${pass/@/#}" | s dd of=/etc/postfix/sasl_passwd 2>/dev/null
137 s postmap hash:/etc/postfix/sasl_passwd
138 s service postfix reload
139
140 else # exim
141
142 # wording of question from dpkg-reconfigure exim4-config
143 # 1. internet site; mail is sent and received directly using SMTP
144 # 2. mail sent by smarthost; received via SMTP or fetchmail
145 # 3. mail sent by smarthost; no local mail
146 # 4. local delivery only; not on a network
147 # 5. no configuration at this time
148
149 # default mailname is $HOSTNAME.lan,
150 # mailname makes addresses like "root" be root@mailname
151 # and a qualified domain does not get forwarded per
152 # .forward. whatever, this fixes that.
153 s debconf-set-selections <<EOF
154 exim4-config exim4/dc_eximconfig_configtype select mail sent by smarthost; no local mail
155 exim4-config exim4/dc_smarthost string $smarthost
156 exim4-config exim4/use_split_config boolean true
157 exim4-config exim4/mailname string $HOSTNAME
158 EOF
159 # light version does not have sasl auth support.
160 pi exim4-daemon-heavy
161
162 f=/etc/exim4/passwd.client
163 s touch $f
164 s chmod 640 $f # before writing sensitive info
165 s chown root:Debian-exim $f
166 # reference: exim4_passwd_client(5)
167 printf "%s:%s" "$domain" "$pass" | s dd of=$f 2>/dev/null
168 # https://blog.dhampir.no/content/make-exim4-on-debian-respect-forward-and-etcaliases-when-using-a-smarthost
169 # i only need .forwards, so just doing that one.
170 cd /etc/exim4/conf.d/router
171 a=userforward
172 b=${a}_higher_priority
173 tmp=$(mktemp)
174 of=175_$b
175 # sed to make the router name unique
176 sed -r s/^\\S+:/$b:/ 600_exim4-config_$a | s dd of=$tmp 2>/dev/null
177 if ! diff -q >/dev/null $tmp $of; then
178 s dd if=$tmp of=$of >/dev/null
179 ser restart exim4
180 fi
181 fi
182
183 # linode image has a root alias. completely useless, remove it.
184 sudo sed -i '/^root:/d' /etc/aliases
185
186 s newaliases
187
188
189 # based on http://www.postfix.org/qmgr.8.html and my notes in gnus
190 dir=/nocow/$type
191 sdir=/var/spool/$type
192 if [[ $(readlink -f $sdir) != $dir ]]; then
193 ser stop $type
194 if [[ ! -e $dir && -d $sdir ]]; then
195 s mv $sdir $dir
196 fi
197 s lnf -T $dir $sdir
198 fi
199
200 sgo $type
201
202
203 # if I wanted the from address to be renamed and sent to a different address,
204 # echo "sdx@localhost development@localhost" | sudo dd of=/etc/postfix/recipient_canonical
205 # sudo postmap hash:/etc/postfix/recipient_canonical
206 # sudo service postfix reload