2 # Wrapper for acme.sh to work on openwrt.
4 # This program is free software; you can redistribute it and/or modify it under
5 # the terms of the GNU General Public License as published by the Free Software
6 # Foundation; either version 3 of the License, or (at your option) any later
9 # Author: Toke Høiland-Jørgensen <toke@toke.dk>
12 ACME
=/usr
/lib
/acme
/acme.sh
13 # We export both ca variables in an attempts to keep backwards
14 # compatibility with older versions of curl that was linked against
16 export SSL_CERT_DIR
=/etc
/ssl
/certs
17 export CURL_CA_BUNDLE
=/etc
/ssl
/certs
/ca-certificates.crt
29 [ -f "/etc/crontabs/root" ] && grep -q '/etc/init.d/acme' /etc
/crontabs
/root
&& return
30 echo "0 0 * * * /etc/init.d/acme start" >> /etc
/crontabs
/root
31 /etc
/init.d
/cron start
36 [ "$DEBUG" -eq "1" ] && echo "$@" >&2
41 echo "Running pre checks."
44 [ -d "$STATE_DIR" ] || mkdir
-p "$STATE_DIR"
46 if [ -e /etc
/init.d
/uhttpd
]; then
48 UHTTPD_LISTEN_HTTP
=$
(uci get uhttpd.main.listen_http
)
50 uci
set uhttpd.main.listen_http
=''
52 /etc
/init.d
/uhttpd reload ||
return 1
55 iptables
-I input_rule
-p tcp
--dport 80 -j ACCEPT ||
return 1
56 ip6tables
-I input_rule
-p tcp
--dport 80 -j ACCEPT ||
return 1
57 debug
"v4 input_rule: $(iptables -nvL input_rule)"
58 debug
"v6 input_rule: $(ip6tables -nvL input_rule)"
59 debug
"port80 listens: $(netstat -ntpl | grep :80)"
65 echo "Running post checks (cleanup)."
66 iptables
-D input_rule
-p tcp
--dport 80 -j ACCEPT
67 ip6tables
-D input_rule
-p tcp
--dport 80 -j ACCEPT
69 if [ -e /etc
/init.d
/uhttpd
]; then
70 uci
set uhttpd.main.listen_http
="$UHTTPD_LISTEN_HTTP"
72 /etc
/init.d
/uhttpd reload
91 local main_domain
="$1"
93 grep -q "acme-staging" "$STATE_DIR/$main_domain/${main_domain}.conf"
107 local moved_staging
=0
110 config_get_bool enabled
"$section" enabled
0
111 config_get_bool use_staging
"$section" use_staging
112 config_get_bool update_uhttpd
"$section" update_uhttpd
113 config_get domains
"$section" domains
114 config_get keylength
"$section" keylength
116 [ "$enabled" -eq "1" ] ||
return
118 [ "$DEBUG" -eq "1" ] && acme_args
="$acme_args --debug"
123 if [ -e "$STATE_DIR/$main_domain" ]; then
124 if [ "$use_staging" -eq "0" ] && is_staging
"$main_domain"; then
125 echo "Found previous cert issued using staging server. Moving it out of the way."
126 mv "$STATE_DIR/$main_domain" "$STATE_DIR/$main_domain.staging"
129 echo "Found previous cert config. Issuing renew."
130 $ACME --home "$STATE_DIR" --renew -d "$main_domain" $acme_args ||
return 1
136 acme_args
="$acme_args $(for d in $domains; do echo -n "-d $d "; done)"
137 acme_args
="$acme_args --standalone"
138 acme_args
="$acme_args --keylength $keylength"
139 [ -n "$ACCOUNT_EMAIL" ] && acme_args
="$acme_args --accountemail $ACCOUNT_EMAIL"
140 [ "$use_staging" -eq "1" ] && acme_args
="$acme_args --staging"
142 if ! $ACME --home "$STATE_DIR" --issue $acme_args; then
143 failed_dir
="$STATE_DIR/${main_domain}.failed-$(date +%s)"
144 echo "Issuing cert for $main_domain failed. Moving state to $failed_dir" >&2
145 [ -d "$STATE_DIR/$main_domain" ] && mv "$STATE_DIR/$main_domain" "$failed_dir"
146 if [ "$moved_staging" -eq "1" ]; then
147 echo "Restoring staging certificate" >&2
148 mv "$STATE_DIR/${main_domain}.staging" "$STATE_DIR/${main_domain}"
153 if [ "$update_uhttpd" -eq "1" ]; then
154 uci
set uhttpd.main.key
="$STATE_DIR/${main_domain}/${main_domain}.key"
155 uci
set uhttpd.main.cert
="$STATE_DIR/${main_domain}/fullchain.cer"
156 # commit and reload is in post_checks
165 STATE_DIR
=$
(config_get
"$section" state_dir
)
166 ACCOUNT_EMAIL
=$
(config_get
"$section" account_email
)
167 DEBUG
=$
(config_get
"$section" debug
)
170 if [ -n "$CHECK_CRON" ]; then
176 config_foreach load_vars acme
179 trap err_out HUP TERM
182 config_foreach issue_cert cert