-
Notifications
You must be signed in to change notification settings - Fork 4
/
setup-opendkim-postfix.sh
executable file
·285 lines (251 loc) · 10.9 KB
/
setup-opendkim-postfix.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/bin/bash
# Asuming postfix is already setup and running.
# Tested on Debian Stretch
# Version 0.3. d.d 2018-05-01
# Changes, corrected typos
### CONFIGURATION
# The Primary email domain, ( Note: all other settings are defaults and are ok to use.)
# Changing the domain to your primary email domain is a must.
OPENDKIM_DOMAIN=""
# This folder is used to set/configure the KeyTable, SigningTable and TrustedHosts files.
OPENDKIM_DIR="/etc/postfix/dkim"
# The selector; this results in mail20180325 (mailYearMonthDay format)
OPENDKIM_SELECTOR="mail"
# Use 1024 or 2048, Note that 4096 wont work, its to big for DNS entrie
OPENDKIM_BITS="2048"
# The "default" opendkim port is used.
OPENDKIM_PORT="8892"
# ! Note.
# some settings are set in /etc/defaults/opendkim.
# These settings are use to generate the correct opendkim.service file.
# see the link, differences between opendkim 2.9.2 2.10.x 2.11.x
### PROGRAM ####################################################################
set -e
DATE_NOW="$(date +%Y%m%d)"
### Sources to read.
GoReadThis(){
echo "# Very Usefull info to read."
echo "# My used sources to make this script:"
echo "# https://wiki.debian.org/opendkim and/or (yes its a different page) https://wiki.debian.org/OpenDKIM"
echo "# https://terminal28.com/how-to-install-and-configure-opendkim-postfix-dns-debian-9-stretch/"
echo "# https://jschumacher.info/2017/06/upgrading-to-debian-stretch-with-dovecot-postfix-opendkim/ ! know the difference between opendkim 2.9.2 2.10.x and 2.11.x"
echo "# https://linode.com/docs/email/postfix/configure-spf-and-dkim-in-postfix-on-debian-8/ ! some usefull info for stretch also."
echo "# https://blog.thisispedro.com/index.php/2016/08/21/opendkim-and-restricted-dns/"
echo "# http://www.postfix.org/MILTER_README.html what is a milter?"
echo "#"
echo "# ! These three are very good to read to understand SPF DKIM DMARC setups."
echo "# http://www.tech-savvy.nl/2017/02/17/antispam-counter-measures-explained-part-1-how-the-sender-policy-framework-really-works/"
echo "# http://www.tech-savvy.nl/2017/03/09/antispam-counter-measures-explained-part-2-advanced-spf-records-best-practice-and-biggest-mistakes/"
echo "# http://www.tech-savvy.nl/2017/04/11/antispam-counter-measures-explained-part-3-dkim/"
echo "# https://elasticemail.com/dmarc/"
}
CheckPackageIsInstalled() {
if [ "$(dpkg-query -W --showformat='${db:Status-Status}\n' "${1}")" == "installed" ]; then
echo "Package was already installed : ${1}."
else
apt-get -qy install "${1}"
fi
}
GenerateKeyOpenDKIM(){
echo "Generating new key, please wait."
# ! Note, by most show -h rsa-sha256, this is incorrect.
opendkim-genkey -r -D /etc/dkimkeys/ -d ${OPENDKIM_DOMAIN} -s ${OPENDKIM_SELECTOR}${DATE_NOW} -b ${OPENDKIM_BITS} -h sha256
}
FixRightsOpenDKIM() {
echo "Setting rights again on dkim files/folders."
chgrp opendkim ${OPENDKIM_DIR}/*
chmod u=+rw,g=+r,o=-rwx ${OPENDKIM_DIR}/*
chown opendkim /etc/dkimkeys/*
chmod u=+rw,g=-rwx,o=-rwx /etc/dkimkeys/*
}
CheckOpenDKIMdnsKey(){
echo "# If the DNS is setup you can run the following to check the key."
echo "opendkim-testkey -d ${OPENDKIM_DOMAIN} -s ${OPENDKIM_SELECTOR}${DATE_NOW} -vvv"
echo
echo "The expected result should look like this :"
echo "# opendkim-testkey: using default configfile /etc/opendkim.conf"
echo "# opendkim-testkey: checking key '${OPENDKIM_SELECTOR}${DATE_NOW}._domainkey.${OPENDKIM_DOMAIN}'"
echo "# opendkim-testkey: key secure ( or opendkim-testkey: key not secure)"
echo "# opendkim-testkey: key OK"
echo
echo "# If you are using OpenDKIM behind a restrictive network that doesn’t allow all outgoing UDP connections,"
echo "# you may find some issues when checking authoritative responses for public keys:"
echo "# ... query timed out ... or unexpected reply class/type (-1/-1)"
echo "# Check firewall setting also. ( allow port 53 (tcp+udp)"
echo
echo "# The workaround is to set static DNS servers (whatever is allowed in your network/firewall)"
echo "# in /etc/opendkim.conf using the directive Nameservers"
echo "# example, add : Nameservers 208.67.222.222,208.67.220.220 for openDNS"
echo "# example, add : Nameservers 8.8.8.8,8.8.4.4 for Google"
echo "# If you use bind or any other caching dns server with forwarding, add the forwarder DNS servers!"
}
echo
#
CheckPackageIsInstalled opendkim
CheckPackageIsInstalled opendkim-tools
echo
if [ ! -d ${OPENDKIM_DIR} ]; then
install -d ${OPENDKIM_DIR} -o root -g opendkim -m 750
# -D Save2Folder -d domainnaam.tld -s selecter
# !Note, update dkim, then change the selector
GenerateKeyOpenDKIM
touch ${OPENDKIM_DIR}/KeyTable
touch ${OPENDKIM_DIR}/SigningTable
touch ${OPENDKIM_DIR}/TrustedHosts
# change group to opendkim and set the rights to 750
FixRightsOpenDKIM
#
# Commonly-used options; the commented-out versions show the defaults.
#Canonicalization simple
#Mode sv
#SubDomains no
# sed -i 's/#Mode/Mode/g' /etc/opendkim.conf
# Not needed its the default.
else
echo "The opendkim key is generated and placed already in /etc/dkimkeys (debian default)"
echo "Please note, keep you rights correct here. "
echo "Use: "
echo "chgrp opendkim ${OPENDKIM_DIR}/* "
echo "chown opendkim /etc/dkimkeys/* "
echo "chmod u=+rw,g=+r,o=-rwx ${OPENDKIM_DIR}/*"
echo "chmod u=+rw,g=-rwx,o=-rwx /etc/dkimkeys/*"
echo "To correct it."
echo
fi
if [ $(grep ${OPENDKIM_PORT} < /etc/default/opendkim | wc -l) -eq 0 ]; then
# Disable unix socket
sed -i 's/SOCKET=local:\$RUNDIR\/opendkim.sock/#SOCKET=local:\$RUNDIR\/opendkim.sock/g' /etc/default/opendkim
# Enable TCP socket. ( defaults in this script to port 8892, its used in opendkim defaults )
sed -i 's/#SOCKET=inet:12345\@localhost/SOCKET=inet:${OPENDKIM_PORT}\@localhost/g' /etc/default/opendkim
sed -i 's/# listen on loopback on port 12345/# listen on loopback on port ${OPENDKIM_PORT}/g' /etc/default/opendkim
# or via Postfix SOCKET below sed lines.
#sed -i 's/#RUNDIR=\/var\/spool\/postfix/RUNDIR=\/var\/spool\/postfix/g' /etc/default/opendkim
#sed -i 's/RUNDIR=\/var\/run\/opendkim/#RUNDIR=\/var\/run\/opendkim/g' /etc/default/opendkim
# ! Note, not everything is setup for unix sockets, that needs more work.
# Generate the correct service file.
/lib/opendkim/opendkim.service.generate
systemctl daemon-reload
systemctl restart opendkim
else
echo "File /etc/default/opendkim was already modified."
echo "The port was already modified to ${OPENDKIM_PORT}@localhost"
fi
if [ $(grep SigningTable < /etc/opendkim.conf | wc -l) -eq 0 ]; then
cat << EOF >> /etc/opendkim.conf
###### Added for OpenDKIM by script
# Specify the list of keys
KeyTable file:${OPENDKIM_DIR}/KeyTable
# Match keys and domains
SigningTable file:${OPENDKIM_DIR}/SigningTable
# Host/IP trusts.
ExternalIgnoreList file:${OPENDKIM_DIR}/TrustedHosts
InternalHosts file:${OPENDKIM_DIR}/TrustedHosts
EOF
else
echo
echo "Keytable was already done, asumming SigningTable and InternalHosts ExternalIgnoreList also done."
echo "This modification was already done in : /etc/opendkim.conf."
fi
# KeyTable
if [ $(grep ${OPENDKIM_DOMAIN} < ${OPENDKIM_DIR}/KeyTable | wc -l) -eq 0 ]; then
echo "Detected clean ${OPENDKIM_DIR}/KeyTable"
cat << EOF >> ${OPENDKIM_DIR}/KeyTable
###### Added for OpenDKIM by script
${OPENDKIM_SELECTOR}${DATE_NOW}._domainkey.${OPENDKIM_DOMAIN} ${OPENDKIM_DOMAIN}:${OPENDKIM_SELECTOR}${DATE_NOW}:/etc/dkimkeys/${OPENDKIM_SELECTOR}${DATE_NOW}.private
EOF
echo
echo "Added some defaults, please review this yourself also"
echo
elif [ $(grep "${OPENDKIM_SELECTOR}${DATE_NOW}" < ${OPENDKIM_DIR}/KeyTable | wc -l) -eq 0 ]; then
echo "Adding a new key to KeyTable file".
echo "${OPENDKIM_SELECTOR}${DATE_NOW}._domainkey.${OPENDKIM_DOMAIN} ${OPENDKIM_DOMAIN}:${OPENDKIM_SELECTOR}${DATE_NOW}:/etc/dkimkeys/${OPENDKIM_SELECTOR}${DATE_NOW}.private" >> ${OPENDKIM_DIR}/KeyTable
KEY_UPDATED="yes"
else
echo "A Keytable key was already added today, review it or remove it to generate a new one."
fi
# SigningTable
if [ $(grep ${OPENDKIM_DOMAIN} < ${OPENDKIM_DIR}/SigningTable | wc -l) -eq 0 ]; then
echo "Detected clean ${OPENDKIM_DIR}/SigningTable"
cat << EOF >> ${OPENDKIM_DIR}/SigningTable
###### Added for OpenDKIM by script
# You can specify multiple domains
# Example.net SelectorDate._domainkey.example.net
#
${OPENDKIM_DOMAIN} ${OPENDKIM_SELECTOR}${DATE_NOW}._domainkey.${OPENDKIM_DOMAIN}
EOF
echo
echo "Added some defaults, please review this yourself also"
echo
elif [ $(grep "${OPENDKIM_SELECTOR}${DATE_NOW}" < ${OPENDKIM_DIR}/SigningTable | wc -l) -eq 0 ]; then
echo "Adding new key to SigningTable file".
echo "${OPENDKIM_SELECTOR}${DATE_NOW}._domainkey.${OPENDKIM_DOMAIN} ${OPENDKIM_DOMAIN}:${OPENDKIM_SELECTOR}${DATE_NOW}:/etc/dkimkeys/${OPENDKIM_SELECTOR}${DATE_NOW}.private" >> ${OPENDKIM_DIR}/SigningTable
KEY_UPDATED="yes"
else
echo "A SigningTable key was already added today, review it or remove it to generate a new one."
fi
if [ "${KEY_UPDATE}" = "yes" ]; then
systemctl restart opendkim postfix
if [ "$?" -ne 0 ]; then
echo "ERROR, Something is wrong, unable to restart opendkim and/or postfix correctly."
echo "ERROR, Please check you config NOW !!!! "
echo "use : systemctl restart opendkim postfix when your done."
echo "Exiting now....."
exit 1
fi
fi
# TrustedHosts
if [ $(grep $(hostname -f) < ${OPENDKIM_DIR}/TrustedHosts | wc -l) -eq 0 ]; then
echo "Detected clean ${OPENDKIM_DIR}/TrustedHosts"
cat << EOF >> ${OPENDKIM_DIR}/TrustedHosts
###### Added for OpenDKIM by script
127.0.0.1
::1
localhost
$(hostname -s)
$(hostname -d)
${OPENDKIM_DOMAIN}
EOF
for y in $(hostname -A); do
echo $y >> /etc/postfix/dkim/TrustedHosts
done
for x in $(hostname -I); do
echo $x >> /etc/postfix/dkim/TrustedHosts
done
echo "###### Manual added below here." >> ${OPENDKIM_DIR}/TrustedHosts
echo
echo "Added some defaults, please review this yourself also, look in ${OPENDKIM_DIR}/"
echo
fi
unset x
unset y
if [ $(grep "inet:localhost:${OPENDKIM_PORT}" < /etc/postfix/main.cf | wc -l) -eq 0 ]; then
cat << EOF >> /etc/postfix/main.cf
###### Added for OpenDKIM by script
milter_default_action = accept
milter_protocol = 6
smtpd_milters = inet:localhost:${OPENDKIM_PORT}
non_smtpd_milters = inet:localhost:${OPENDKIM_PORT}
EOF
else
echo "Postfix was already setup for dkim on localhost:${OPENDKIM_PORT}"
echo "The modification is done in : /etc/postfix/main.cf"
echo
fi
# for the dns.
echo
echo "Folder /etc/dkimkeys/ is the private key folder."
cat /etc/dkimkeys/README.PrivateKeys
echo
echo
echo "Use this for the DNS TXT record: "
if [ ! -f /etc/dkimkeys/${OPENDKIM_SELECTOR}${DATE_NOW}.txt ]; then
GenerateKeyOpenDKIM
else
echo "Already generated the dkimkey with selector : ${OPENDKIM_SELECTOR}${DATE_NOW}"
fi
FixRightsOpenDKIM
cat /etc/dkimkeys/${OPENDKIM_SELECTOR}${DATE_NOW}.txt
echo
GoReadThis
echo
CheckOpenDKIMdnsKey