2d187fe47c3af33f251ee96efe7bbcb07f9a77a3
[distro-setup] / postfix-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 set -eE -o pipefail
17 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
18
19 # dunno why debian installed postfix with builddep emacs
20 # but I will just explicitly install it here since
21 # I use it for sending mail in emacs.
22 if private-host; then
23 relayhost="[mail.messagingengine.com]:587"
24 else
25 # ses initially suggests port 25, but I had problems connecting to that.
26 relayhost="[email-smtp.us-west-2.amazonaws.com]:587"
27 fi
28 if isdeb; then
29 s debconf-set-selections <<EOF
30 postfix postfix/main_mailer_type select Satellite system
31 postfix postfix/mailname string $host
32 postfix postfix/relayhost string $relayhost
33 EOF
34
35 pi postfix
36 else
37 pi postfix
38 # Settings from reading the output when installing on debian,
39 # then seeing which were different in a default install on arch.
40 # I assume the same works for fedora.
41 postconfin <<EOF
42 mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
43 mailbox_size_limit = 0
44 relayhost = $relayhost
45 inet_interfaces = loopback-only
46 EOF
47
48 s systemctl enable postfix
49 s systemctl start postfix
50 fi
51
52
53 # note, previously, the rest of setup was done separately.
54
55
56 # based on,http://www.postfix.org/qmgr.8.html and my notes in gnus
57 # originally tried moving specific directories under /var/spool/postfix,
58 # but postfix didn't like that
59 if [[ ! -L /var/spool/postfix ]]; then
60 ser stop postfix
61 n=/q/postfix-`distro-name``debian-archive`
62 if [[ -e $n ]]; then
63 echo "$0: warning: $n already exists before we do the link, removing it"
64 s rm -rf $n
65 fi
66 s mv /var/spool/postfix $n
67 s lnf -T $n /var/spool/postfix
68 ser start postfix
69 s journalctl -n 20 | cat # sudo as we may not have journal reading rights yet
70 fi
71
72
73 # This also works instead of ~/.forward
74 # s sed -i --follow-symlinks '/^root/d' /etc/aliases ||:
75 #echo "root: $HOSTNAME@$SOME_DOMAIN" | s tee -a /etc/aliases
76 # this can't be a symlink and has permission restrictions
77 # it might work in /etc/aliases, but this seems more proper.
78
79 if s grep amazonaws /etc/postfix/sasl_passwd &>/dev/null; then
80 forward=$HOSTNAME@$IMPERSONAL_DOMAIN
81 else
82 forward=$HOSTNAME@$PERSONAL_DOMAIN
83 fi
84 e $forward > ~/.forward
85 e $forward | s tee /root/.forward
86
87 # linode image has a root alias. completely useless, remove it.
88 sudo sed -i '/^root:/d' /etc/aliases
89
90
91 s newaliases
92
93 # if I wanted the from address to be renamed and sent to a different address,
94 # echo "sdx@localhost development@localhost" | sudo dd of=/etc/postfix/recipient_canonical
95 # sudo postmap hash:/etc/postfix/recipient_canonical
96 # sudo service postfix reload
97
98
99 # i'm assuming mail just won't work on systems without the sasl_passwd.
100 postconfin <<'EOF'
101 smtp_sasl_auth_enable = yes
102 smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
103 smtp_sasl_security_options = noanonymous
104 smtp_tls_security_level = secure
105 message_size_limit = 20480000
106 smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
107 inet_protocols = ipv4
108 EOF
109 # msg_size_limit: I ran into a log file not sending cuz of size. double from 10 to 20 meg limit
110 # inet_protocols: without this, postfix tries an ipv6 lookup then gives
111 # up and fails. snippet from syslog: type=AAAA: Host not found, try again
112
113
114 s postmap hash:/etc/postfix/sasl_passwd
115 # offlineimap uses this too, it is much easier to use one location than to
116 # condition it's config and postfix's config
117 case $distro in
118 fedora) s lnf -T ca-certificates.crt /etc/ssl/ca-bundle.trust.crt ;;
119 *) :
120 esac
121
122 s service postfix reload
123 sgo postfix