-
Notifications
You must be signed in to change notification settings - Fork 0
/
bp-lets.zsh
executable file
·359 lines (296 loc) · 10.7 KB
/
bp-lets.zsh
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/bin/zsh
# {{{ copyright and usage
#
## Copyright (c) 2017-2019 brainpower <brainpower at mailbox dot org>
##
## Permission is hereby granted, free of charge, to any person obtaining a copy
## of this software and associated documentation files (the "Software"), to deal
## in the Software without restriction, including without limitation the rights
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
## copies of the Software, and to permit persons to whom the Software is
## furnished to do so, subject to the following conditions:
##
## The above copyright notice and this permission notice shall be included in
## all copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
## THE SOFTWARE.
if [[ $1 =~ "help" ]] || { [[ -z "$2" ]] && [[ $1 != "create-account" ]] } || [[ -z "$1" ]]; then
printf "Usage: \n" >&2
printf " %s help\n" "$0" >&2
printf " %s create-account\n" "$0" >&2
printf " %s create-cert <certname>\n" "$0" >&2
printf " %s renew <certname>\n" "$0" >&2
printf "\n" >&2
printf "Actions:\n" >&2
printf " help Show this help.\n" >&2
printf " create-account Generates the Let's Encrypt account key.\n" >&2
printf " create-cert Generates a private key and a CSR for a certificate. <certname> should be an unique identifier for the certificate.\n" >&2
printf " renew Request a new or renew an old certificate using acme-tiny.\n" >&2
exit 1
fi
action="$1"
certname="$2"
script_dir="$(dirname $(readlink -f "${0}"))"
# }}}
# set a variable if it is unset or empty
# $1: name of the variable
# $2: value to be set
function set_if_unset() {
if [[ -z "${(P)1}" ]]; then
eval "${1}=${2}"
fi
}
#################################
# BEGIN default configuration #
#################################
# DEPRECATED: use a post-renew.d/ script
# services to be reloaded after renewal
services=()
if [[ -r "${script_dir}/config.zsh" ]]; then
source "${script_dir}/config.zsh"
fi
# permissions to be set on the directory containing key and certificate
set_if_unset dirmode "750"
# directory served at http://<domain>/.well-known/acme-challenge/
set_if_unset acmedir "/srv/acme-challenges/"
# location of the acme-tiny python script
set_if_unset acmebin "${HOME}/acme-tiny/acme_tiny.py"
# directory containing the certificate subdirectories
set_if_unset xbasedir "/var/local/letse"
# absolute path of the certificate subdirectory
set_if_unset basedir "${xbasedir}/${certname}"
# absolute directory to place the new certificate in,
# $livedir will be symlinked to this directory if renewal is successful
set_if_unset newdir "${basedir}/$(date +%F)"
# path of the symlink to $newdir
set_if_unset livedir "${basedir}/live"
# path to the Let's Encrypt account key
set_if_unset acckey "${xbasedir}/account.key"
# size of the account key in bits (only used by "create-account" action)
set_if_unset acckeysize 4096
# size of the certificate's private key (only used by "create-certificate")
set_if_unset keysize 4096
# the location of the openssl.cnf used for adding SANs to the csr
set_if_unset openssl_cnf "/etc/ssl/openssl.cnf"
###############################
# END default configuration #
###############################
# check if given files are readable, error out otherwise
# $@: list of files
function check_files_readable() {
local file
for file in "$@"; do
if [[ ! -r "$file" ]]; then
printf "ERROR: Required file %s is missing or not readable!" "$file" >&2
exit 1
fi
done
}
# execute scripts in post-renew.d if any exist
# $@: a list of dirs with a post-renew.d folder with scripts in them
function exec_post_renew_d(){
local dir
for dir in "$@"; do
if [[ -d "${dir}/post-renew.d/" ]]; then
find "${dir}/post-renew.d/" \( -type f -o -type l \) -print0 | while read -d $'\0' dfile; do
if [[ -x "$dfile" ]]; then
printf "INFO: running post-renew.d/%s\n" "$(basename "$dfile")" >&2
"$dfile" "${basedir}/live/" "$certname"
else
printf "INFO: skipping post-renew.d/%s - forgot to chmod +x?\n" "$(basename "$dfile")" >&2
fi
done
fi
done
}
# split a certificate bundle into the certificate and the intermediates.
# output files will be completely overwritten!
# $1: the source file, cert + intermediates as pem
# $2: the name of the file to write the certificate to
# $3: the name of the file to write the intermediates to
function split_cert(){
printf "" > "$2"
printf "" > "$3"
awk '
BEGIN { n=0; p=0 }
/-----BEGIN CERTIFICATE-----/ { n++; p=1 }
{
if ( p > 0 ){
if( n == 1 ) {
print > "'"$2"'"
} else {
print >> "'"$3"'"
}
}
}
/-----END CERTIFICATE-----/ { p=0 }
' < "$1"
}
# ask the user to input a list of domains
# that list is stored into an array named domains
# this will overwrite the global variable/array 'domains' if it exists
# $1: message to display
gather_domains() {
domains=()
printf "%s\n" "$1"
while IFS= read -r dom; do
if [[ -z "$dom" ]]; then
break
fi
domains+=( "$dom" )
done
printf "Entered domains are:\n"
for dom in "${domains[@]}"; do
printf "* '%s'\n" $dom
done
printf "Continue? [Yn] "
read
if [[ $REPLY =~ [Nn][Oo]* ]]; then
exit 1
fi
}
# print an openssl.cnf generated from domains array to given file
generate_csr(){
local subject
subject="/CN=${domains[1]}"
cp "${openssl_cnf}" openssl.cnf
printf "[SAN]\nsubjectAltName=" >> openssl.cnf
for dom in "${domains[@]}"; do
printf "DNS:%s," "$dom" >> openssl.cnf
done
printf "\n" >> openssl.cnf
sed '/subjectAltName/s@,$@@' -i openssl.cnf
openssl req -new -sha256 -key "private.key" -subj "${subject}" -reqexts SAN -config openssl.cnf > request.csr
}
##########
# main #
##########
if [[ $action = "renew" ]]; then
if [[ ! -d "$basedir" ]]; then
printf "ERROR: %s does not exist!\n" "$basedir" >&2
printf "Unable to renew non-existent certificate!\n" >&2
exit 1
fi
check_files_readable \
"${basedir}/private.key" \
"${basedir}/request.csr"
mkdir -p "${newdir}"
chmod "${dirmode}" "${newdir}"
if ! cd "${newdir}"; then
printf "ERROR: Changing directory failed: %s\n" "${newdir}" >&2
exit 2
fi
python3 "${acmebin}" \
--quiet \
--account-key "${acckey}" \
--acme-dir "${acmedir}" \
--csr "${basedir}/request.csr" \
> "full-bundle.crt"
ret=$?
if [[ $ret == 0 ]]; then
split_cert "full-bundle.crt" \
"certificate.crt" \
"ca-bundle.crt"
cp -a "${basedir}/private.key" "private.key"
cat "private.key" "full-bundle.crt" > "key-bundle.crt"
cd "${basedir}"
ln -Tfs "${newdir}" live
exec_post_renew_d "${script_dir}" "${basedir}" "${xbasedir}"
if [[ -n "${services}" ]]; then
printf "WARNING: Using the services array is deprecated, use a post-renew.d script instead.\n" >&2
for service in "${services[@]}"; do
if sudo systemctl is-active -q "${service}"; then
sudo systemctl restart "${service}"
fi
done
fi
else
printf "WARNING: acme_tiny.py failed with code: %s\n" "$ret" >&2
fi
elif [[ $action = "create-account" ]]; then
if [[ -f $acckey ]]; then
printf "ERROR: Account Key already exists: %s\n" "$acckey" >&2
printf "If you really want to create a new one, delete it first!\n" >&2
exit 1
fi
mkdir -p "${xbasedir}"
chmod "${dirmode}" "${xbasedir}"
openssl genrsa "$acckeysize" > "$acckey"
if [[ $? = 0 ]]; then
printf "Key successfully generated.\n"
fi
elif [[ $action = "create-cert" ]]; then
if [[ -d "$basedir" ]]; then
printf "ERROR: %s already exists!\n" "$basedir" >&2
printf "If you really want a new certificate, give it another name or remove the existing one.\n" >&2
exit 1
fi
gather_domains "Please enter all domains you want
(don't forget www. !; first one used as CN; empty line submits):"
mkdir -p "$basedir"
chmod "${dirmode}" "${basedir}"
cd "${basedir}" || exit 1
mkdir -p "post-renew.d"
printf "Generating new private key...\n"
openssl genrsa "$keysize" > "private.key"
printf "Generating new CSR...\n"
generate_csr
printf "Use '%s renew %s' now to request the new certificate...\n" "$(basename "$0")" "${certname}"
printf "%s\n" "${certname}" >> "${xbasedir}/active"
elif [[ $action = "change-cert" ]]; then
if [[ ! -d "$basedir" ]]; then
printf "ERROR: %s does not exist!\n" "$basedir" >&2
printf "Use 'create-cert' action to create one.\n" >&2
exit 1
fi
cd "${basedir}" || exit 1
if [[ ! -f "private.key" ]]; then
printf "ERROR: No private.key found in: %s" "${basedir}" >&2
exit 1
fi
gather_domains "Please enter all domains you want, replacing the old ones!
(don't forget www. !; first one used as CN; empty line submits):"
printf "Generating new CSR...\n"
generate_csr
printf "Use '%s renew %s' now to request the new certificate...\n" "$(basename "$0")" "${certname}"
printf "%s\n" "${certname}" >> "${xbasedir}/active"
elif [[ $action = "recreate-csr" ]]; then
if ! [[ -d "$basedir" ]]; then
printf "ERROR: %s does not exist!\n" "$basedir" >&2
printf "Create a new certificate using 'create-cert' action.\n" >&2
exit 1
fi
cd "${basedir}" || exit 1
if [[ ! -f "private.key" ]]; then
printf "ERROR: No private.key found in: %s" "${basedir}" >&2
exit 1
fi
if ! [[ -r "openssl.cnf" ]]; then
printf "ERROR: openssl.cnf is not readable. Is this really a cert created with this script?\n" >&2
exit 1
fi
subject="$(openssl req -in request.csr -noout -subject | sed 's@^subject=.*CN = \([^ ]\+\)@\1@')"
printf "Previous subject domain (CN) was: %s\nContinue using it? [Yn] " "$subject"
read
if [[ $REPLY =~ [Nn][Oo]* ]]; then
printf "Enter new subject domain (CN): "
read subject
fi
if [[ -z ${subject} ]]; then
printf "ERROR: empty subject!\n" >&2
exit 1
fi
printf "Will open an editor now so you can edit the SANs at the end of the file.\n"
printf "Press any key to continue...\n"
read
${EDITOR:-vim} openssl.cnf
printf "Generating new CSR...\n"
openssl req -new -sha256 -key "private.key" -subj "/CN=${subject}" -reqexts SAN -config openssl.cnf > request.csr
printf "Use '%s renew %s' now to request a new certificate...\n" "$(basename "$0")" "${certname}"
fi