-
Notifications
You must be signed in to change notification settings - Fork 33
/
synology-letsencrypt.sh
80 lines (64 loc) · 2.07 KB
/
synology-letsencrypt.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
#!/bin/bash -e
[[ $EUID == 0 ]] || { echo >&2 "This script must be run as root"; exit 1; }
while getopts ":p:ch" opt; do
case $opt in
p) LEGO_PATH="$OPTARG" ;;
c) CREATE_HOOK=false ;;
h)
echo "Usage: $0 [options]"
echo " -p <lego_path> The path where Lego will install your certs"
echo " -c Suppress [c]reation of the hook scripts, if you have your own"
exit 0
;;
:) echo "Error: -${OPTARG} requires an argument" >&2 ;;
\?) echo "Invalid option -$OPTARG" >&2 ;;
esac
done
LEGO_PATH=${LEGO_PATH:-/usr/local/etc/synology-letsencrypt}
CREATE_HOOK=${CREATE_HOOK:-true}
source "$LEGO_PATH/env"
export LEGO_PATH
archive_path="/usr/syno/etc/certificate/_archive"
cert_path="$LEGO_PATH/certificates"
cert_domain="${DOMAINS[1]#\*.}"
hook_path="$LEGO_PATH/hook"
mkdir -p "$cert_path"
## cert_id
cert_id_path="$cert_path/$cert_domain.cert_id"
/usr/local/bin/synology-letsencrypt-make-cert-id.sh "$cert_id_path" "$archive_path"
source "$cert_id_path"
if [[ -z $cert_id ]]; then
echo >&2 "ID not found in $cert_id_path"
exit 1
fi
## install hook
archive_cert_path="$archive_path/$cert_id"
if [[ ! -d $archive_cert_path ]]; then
mkdir -p "$archive_cert_path"
fi
if [[ ${CREATE_HOOK} == true ]]; then
cat >"$hook_path" <<EOF
#!/bin/bash
cp "${cert_path}/${cert_domain}.crt" "${archive_cert_path}/cert.pem"
cp "${cert_path}/${cert_domain}.crt" "${archive_cert_path}/fullchain.pem"
cp "${cert_path}/${cert_domain}.issuer.crt" "${archive_cert_path}/chain.pem"
cp "${cert_path}/${cert_domain}.key" "${archive_cert_path}/privkey.pem"
/usr/local/bin/synology-letsencrypt-reload-services.sh "$cert_id"
EOF
chmod 700 "$hook_path"
fi
## run or renew
if [[ -s $cert_path/$cert_domain.crt ]]; then
CMD=(renew --renew-hook)
else
CMD=(run --run-hook)
fi
# https://go-acme.github.io/lego/usage/cli/
/usr/local/bin/lego \
--accept-tos \
--key-type "rsa4096" \
--email "$EMAIL" \
--dns "$DNS_PROVIDER" \
"${DOMAINS[@]}" \
"${LEGO_OPTIONS[@]}" \
"${CMD[@]}" "$hook_path"