-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcert-puller
executable file
·312 lines (267 loc) · 7.9 KB
/
cert-puller
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/bin/sh
# the following variables can be overridden by the config file
CERT_DST="/usr/local/etc/ssl"
CERT_SERVER="https://certs.example.org/certs"
# a space separated list of domains for which certs will be downloads
MYCERTS="example.com"
# a space separated list of services to restart/reload
SERVICES=""
SERVICES_RELOAD=""
SERVICES_RESTART=""
DOWNLOAD_DIR="/var/db/anvil"
# be sure to specify the agument & have no spaces in between the single quotes
USER_AGENT="anvil-cert-puller"
#
# --mirror avoids replacement when identical
# --quiet avoids noise
#
FETCH="/usr/bin/fetch --mirror --quiet --user-agent='${USER_AGENT}'"
CURL="/usr/local/bin/curl --silent --user-agent '${USER_AGENT}' --remote-time"
WGET="/usr/local/bin/wget --quiet --user-agent='${USER_AGENT}'"
# items above can be overridden via the configuration file
# items below here are not usually altered
CONFIG="/usr/local/etc/anvil/cert-puller.conf"
if [ -f ${CONFIG} ]; then
. ${CONFIG}
fi
# initialize variables used below
SUDO_EXAMPLES=0
NEW_CERTS_FOUND=0
# various commands used by this script
BASENAME="/usr/bin/basename"
CP="/bin/cp"
DIFF="/usr/bin/diff"
# These are the downlaoded certs which we will consider for installation
FIND_CERT_FILES="/usr/bin/find ${DOWNLOAD_DIR} -type f"
# if you want to disable logging, not recommend, put a hash before /usr
LOGGER="/usr/bin/logger -t cert-puller"
MV="/bin/mv"
SERVICE="/usr/sbin/service"
SUDO="/usr/local/bin/sudo"
TOUCH="/usr/bin/touch"
usage(){
echo "Usage: $0 [-s] [-h]"
exit 1
}
sudo_examples(){
# this function prints out commands which you can use with visudo to copy/paste
#
# NOTE: the code here must closely match that within install_new_certs() & restart_services() & restart_services_user()
#
for cert in ${MYCERTS}
do
FILES_FETCHING="ca.cer ${cert}.cer ${cert}.fullchain.cer"
for file in ${FILES_FETCHING}
do
echo "anvil ALL=(ALL) NOPASSWD:${CP} -a ${DOWNLOAD_DIR}/${file} ${CERT_DST}/${file}.tmp"
echo "anvil ALL=(ALL) NOPASSWD:${MV} ${CERT_DST}/${file}.tmp ${CERT_DST}/${file}"
done
done
for service in ${SERVICES}
do
case ${service} in
"apache22" | "apache24")
echo "anvil ALL=(ALL) NOPASSWD:${SERVICE} ${service} graceful"
;;
"dovecot" | "mosquitto" | "nginx" | "postfix")
echo "anvil ALL=(ALL) NOPASSWD:${SERVICE} ${service} restart"
;;
"postgresql")
echo "anvil ALL=(ALL) NOPASSWD:${SERVICE} ${service} reload"
;;
esac
done
for service in ${SERVICES_RELOAD}
do
echo "anvil ALL=(ALL) NOPASSWD:${SERVICE} ${service} reload"
done
for service in ${SERVICES_RESTART}
do
echo "anvil ALL=(ALL) NOPASSWD:${SERVICE} ${service} restart"
done
}
sanity_checks(){
if [ ! -r "${CERT_DST}" -o ! -d "${CERT_DST}" ]; then
${LOGGER} "${CERT_DST}" is NOT readable and a directory
${LOGGER} $0 exits
exit 2
fi
}
fetch_new_certs(){
# first, we fetch the certs are looking for.
${LOGGER} fetching into ${DOWNLOAD_DIR}
for cert in ${MYCERTS}
do
${LOGGER} checking certs for ${cert}
FILES_FETCHING="ca.cer ${cert}.cer ${cert}.fullchain.cer"
for file in ${FILES_FETCHING}
do
${LOGGER} "${cert} :: ${file}"
case ${FETCH_TOOL} in
"wget")
${LOGGER} running: ${WGET} --output-document=${DOWNLOAD_DIR}/${file} ${CERT_SERVER}/${cert}/${file}
${WGET} ${WGET_OPTIONS} --output-document=${DOWNLOAD_DIR}/${file} ${CERT_SERVER}/${cert}/${file}
;;
"curl")
${LOGGER} running: ${CURL} -o ${DOWNLOAD_DIR}/${file} ${CERT_SERVER}/${cert}/${file}
${CURL} ${CURL_OPTIONS} -o ${DOWNLOAD_DIR}/${file} ${CERT_SERVER}/${cert}/${file}
;;
*)
${LOGGER} running: ${FETCH} -o ${DOWNLOAD_DIR} ${CERT_SERVER}/${cert}/${file}
${FETCH} ${FETCH_OPTIONS} -o ${DOWNLOAD_DIR} ${CERT_SERVER}/${cert}/${file}
;;
esac
RESULT=$?
if [ "${RESULT}" != "0" ]; then
${LOGGER} "error '${RESULT}' on fetch - perhaps the remote file does not exist."
fi
done
done
}
install_new_certs(){
${LOGGER} looking for cert files: ${FIND_CERT_FILES}
# for each new thing we find, move it over to the right place
NEW_FILES=`${FIND_CERT_FILES}`
for new_file in ${NEW_FILES}
do
filename=`${BASENAME} ${new_file}`
${LOGGER} validating ${filename}
# that cert may not be installed
if [ -f ${CERT_DST}/${filename} ]; then
diff=`${DIFF} ${new_file} ${CERT_DST}/${filename}`
else
diff="x";
${LOGGER} ${filename} does not exist and will be installed
fi
# only install if the certs are different.
if [ "${diff}X" != "X" ]; then
${LOGGER} installing ${filename}
#
# NOTE: the code here must closely match that within sudo_examples() & restart_services()
#
${SUDO} ${CP} -a ${new_file} ${CERT_DST}/${filename}.tmp
if [ $? != 0 ]; then
${LOGGER} FATAL: could not install ${new_file} to ${CERT_DST}/${filename}.tmp - are the sudo permissions up to date? see cert-puller -s
fi
${SUDO} ${MV} ${CERT_DST}/${filename}.tmp ${CERT_DST}/${filename}
if [ $? != 0 ]; then
${LOGGER} FATAL: could not install ${CERT_DST}/${filename}.tmp to ${CERT_DST}/${filename} - are the sudo permissions up to date? see cert-puller -s
fi
NEW_CERTS_FOUND=1
fi
done
}
restart_services(){
#
# NOTE: the code here must closely match that within sudo_examples()
#
for service in ${SERVICES}
do
case ${service} in
"apache22" | "apache24")
${LOGGER} doing a graceful on ${service}
ERRORS=`${SUDO} ${SERVICE} ${service} graceful 2>&1`
if [ $? != 0 ]; then
${LOGGER} ERROR on graceful
${LOGGER} "${ERRORS}"
echo "${ERRORS}"
else
${LOGGER} no errors on graceful
fi
;;
# it might be better if we do a reload.
# will that be sufficient?
"dovecot" | "mosquitto" | "nginx" | "postfix" )
${LOGGER} restarting ${service}
ERRORS=`${SUDO} ${SERVICE} ${service} restart 2>&1`
if [ $? != 0 ]; then
${LOGGER} ERROR on restart
${LOGGER} "${ERRORS}"
echo "${ERRORS}"
else
${LOGGER} no errors on restart
fi
;;
# For Postgresql, we only do a reload
"postgresql" )
${LOGGER} reloading ${service}
ERRORS=`${SUDO} ${SERVICE} ${service} reload 2>&1`
if [ $? != 0 ]; then
${LOGGER} ERROR on reload
${LOGGER} "${ERRORS}"
echo "${ERRORS}"
else
${LOGGER} no errors on reload
fi
;;
*)
${LOGGER} "Unknown service requested in $0: ${service}"
;;
esac
done
}
restart_services_user(){
#
# NOTE: the code here must closely match that within sudo_examples()
#
for service in ${SERVICES_RELOAD}
do
${LOGGER} doing a reload on ${service}
ERRORS=`${SUDO} ${SERVICE} ${service} reload 2>&1`
if [ $? != 0 ]; then
${LOGGER} ERROR on reload
${LOGGER} "${ERRORS}"
echo "${ERRORS}"
else
${LOGGER} no errors on reload
fi
done
for service in ${SERVICES_RESTART}
do
${LOGGER} doing a restart on ${service}
ERRORS=`${SUDO} ${SERVICE} ${service} restart 2>&1`
if [ $? != 0 ]; then
${LOGGER} ERROR on restart
${LOGGER} "${ERRORS}"
echo "${ERRORS}"
else
${LOGGER} no errors on restart
fi
done
}
#
# main code starts here
#
while getopts "hs" opt; do
case $opt in
s)
SUDO_EXAMPLES=1
shift
;;
h)
usage
shift
;;
* )
usage
;;
esac
done
#
# give them samples for visudo
#
if [ ${SUDO_EXAMPLES} == "1" ]; then
sudo_examples
exit 0
fi
${LOGGER} starting $0
sanity_checks
fetch_new_certs
install_new_certs
if [ ${NEW_CERTS_FOUND} == "1" ]; then
restart_services
restart_services_user
else
${LOGGER} no new certs found
fi
${LOGGER} stopping $0