blob: c660cda03398e695ef5e3cde434572091d1f8f06 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001#!/bin/sh
2# Wrapper for acme.sh to work on openwrt.
3#
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
7# version.
8#
9# Author: Toke Høiland-Jørgensen <toke@toke.dk>
10
11CHECK_CRON=$1
12ACME=/usr/lib/acme/acme.sh
13export CURL_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt
14export NO_TIMESTAMP=1
15
16UHTTPD_LISTEN_HTTP=
17STATE_DIR='/etc/acme'
18ACCOUNT_EMAIL=
19DEBUG=0
20NGINX_WEBSERVER=0
21UPDATE_NGINX=0
22UPDATE_UHTTPD=0
23USER_CLEANUP=
24
25. /lib/functions.sh
26
27check_cron()
28{
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
32}
33
34log()
35{
36 logger -t acme -s -p daemon.info -- "$@"
37}
38
39err()
40{
41 logger -t acme -s -p daemon.err -- "$@"
42}
43
44debug()
45{
46 [ "$DEBUG" -eq "1" ] && logger -t acme -s -p daemon.debug -- "$@"
47}
48
49get_listeners() {
50 local proto rq sq listen remote state program
51 netstat -nptl 2>/dev/null | while read proto rq sq listen remote state program; do
52 case "$proto#$listen#$program" in
53 tcp#*:80#[0-9]*/*) echo -n "${program%% *} " ;;
54 esac
55 done
56}
57
58run_acme()
59{
60 debug "Running acme.sh as '$ACME $@'"
61 $ACME "$@"
62}
63
64pre_checks()
65{
66 main_domain="$1"
67
68 log "Running pre checks for $main_domain."
69
70 listeners="$(get_listeners)"
71
72 debug "port80 listens: $listeners"
73
74 for listener in $(get_listeners); do
75 pid="${listener%/*}"
76 cmd="${listener#*/}"
77
78 case "$cmd" in
79 uhttpd)
80 if [ -n "$UHTTPD_LISTEN_HTTP" ]; then
81 debug "Already handled uhttpd; skipping"
82 continue
83 fi
84
85 debug "Found uhttpd listening on port 80; trying to disable."
86
87 UHTTPD_LISTEN_HTTP=$(uci get uhttpd.main.listen_http)
88
89 if [ -z "$UHTTPD_LISTEN_HTTP" ]; then
90 err "$main_domain: Unable to find uhttpd listen config."
91 err "Manually disable uhttpd or set webroot to continue."
92 return 1
93 fi
94
95 uci set uhttpd.main.listen_http=''
96 uci commit uhttpd || return 1
97 if ! /etc/init.d/uhttpd reload ; then
98 uci set uhttpd.main.listen_http="$UHTTPD_LISTEN_HTTP"
99 uci commit uhttpd
100 return 1
101 fi
102 ;;
103 nginx*)
104 if [ "$NGINX_WEBSERVER" -eq "1" ]; then
105 debug "Already handled nginx; skipping"
106 continue
107 fi
108
109 debug "Found nginx listening on port 80; trying to disable."
110 NGINX_WEBSERVER=1
111 local tries=0
112 while grep -sq "$cmd" "/proc/$pid/cmdline" && kill -0 "$pid"; do
113 /etc/init.d/nginx stop
114 if [ $tries -gt 10 ]; then
115 debug "Can't stop nginx. Terminating script."
116 return 1
117 fi
118 debug "Waiting for nginx to stop..."
119 tries=$((tries + 1))
120 sleep 1
121 done
122 ;;
123 "")
124 debug "Nothing listening on port 80."
125 ;;
126 *)
127 err "$main_domain: Cannot run in standalone mode; another daemon is listening on port 80."
128 err "Disable other daemon or set webroot to continue."
129 return 1
130 ;;
131 esac
132 done
133
134 iptables -I input_rule -p tcp --dport 80 -j ACCEPT -m comment --comment "ACME" || return 1
135 ip6tables -I input_rule -p tcp --dport 80 -j ACCEPT -m comment --comment "ACME" || return 1
136 debug "v4 input_rule: $(iptables -nvL input_rule)"
137 debug "v6 input_rule: $(ip6tables -nvL input_rule)"
138 return 0
139}
140
141post_checks()
142{
143 log "Running post checks (cleanup)."
144 # The comment ensures we only touch our own rules. If no rules exist, that
145 # is fine, so hide any errors
146 iptables -D input_rule -p tcp --dport 80 -j ACCEPT -m comment --comment "ACME" 2>/dev/null
147 ip6tables -D input_rule -p tcp --dport 80 -j ACCEPT -m comment --comment "ACME" 2>/dev/null
148
149 if [ -e /etc/init.d/uhttpd ] && ( [ -n "$UHTTPD_LISTEN_HTTP" ] || [ "$UPDATE_UHTTPD" -eq 1 ] ); then
150 if [ -n "$UHTTPD_LISTEN_HTTP" ]; then
151 uci set uhttpd.main.listen_http="$UHTTPD_LISTEN_HTTP"
152 UHTTPD_LISTEN_HTTP=
153 fi
154 uci commit uhttpd
155 /etc/init.d/uhttpd restart
156 fi
157
158 if [ -e /etc/init.d/nginx ] && ( [ "$NGINX_WEBSERVER" -eq 1 ] || [ "$UPDATE_NGINX" -eq 1 ] ); then
159 NGINX_WEBSERVER=0
160 /etc/init.d/nginx restart
161 fi
162
163 if [ -n "$USER_CLEANUP" ] && [ -f "$USER_CLEANUP" ]; then
164 log "Running user-provided cleanup script from $USER_CLEANUP."
165 "$USER_CLEANUP" || return 1
166 fi
167}
168
169err_out()
170{
171 post_checks
172 exit 1
173}
174
175int_out()
176{
177 post_checks
178 trap - INT
179 kill -INT $$
180}
181
182is_staging()
183{
184 local main_domain
185 local domain_dir
186 main_domain="$1"
187 domain_dir="$2"
188
189 grep -q "acme-staging" "${domain_dir}/${main_domain}.conf"
190 return $?
191}
192
193issue_cert()
194{
195 local section="$1"
196 local acme_args=
197 local enabled
198 local use_staging
199 local update_uhttpd
200 local update_nginx
201 local keylength
202 local keylength_ecc=0
203 local domains
204 local main_domain
205 local moved_staging=0
206 local failed_dir
207 local webroot
208 local dns
209 local user_setup
210 local user_cleanup
211 local ret
212 local domain_dir
213 local acme_server
214 local days
215
216 config_get_bool enabled "$section" enabled 0
217 config_get_bool use_staging "$section" use_staging
218 config_get_bool update_uhttpd "$section" update_uhttpd
219 config_get_bool update_nginx "$section" update_nginx
220 config_get calias "$section" calias
221 config_get dalias "$section" dalias
222 config_get domains "$section" domains
223 config_get keylength "$section" keylength
224 config_get webroot "$section" webroot
225 config_get dns "$section" dns
226 config_get user_setup "$section" user_setup
227 config_get user_cleanup "$section" user_cleanup
228 config_get acme_server "$section" acme_server
229 config_get days "$section" days
230
231 UPDATE_NGINX=$update_nginx
232 UPDATE_UHTTPD=$update_uhttpd
233 USER_CLEANUP=$user_cleanup
234
235 [ "$enabled" -eq "1" ] || return
236
237 [ "$DEBUG" -eq "1" ] && acme_args="$acme_args --debug"
238
239 set -- $domains
240 main_domain=$1
241
242 if [ -n "$user_setup" ] && [ -f "$user_setup" ]; then
243 log "Running user-provided setup script from $user_setup."
244 "$user_setup" "$main_domain" || return 1
245 else
246 [ -n "$webroot" ] || [ -n "$dns" ] || pre_checks "$main_domain" || return 1
247 fi
248
249 if echo $keylength | grep -q "^ec-"; then
250 domain_dir="$STATE_DIR/${main_domain}_ecc"
251 keylength_ecc=1
252 else
253 domain_dir="$STATE_DIR/${main_domain}"
254 fi
255
256 log "Running ACME for $main_domain"
257
258 handle_credentials() {
259 local credential="$1"
260 eval export $credential
261 }
262 config_list_foreach "$section" credentials handle_credentials
263
264 if [ -e "$domain_dir" ]; then
265 if [ "$use_staging" -eq "0" ] && is_staging "$main_domain" "$domain_dir"; then
266 log "Found previous cert issued using staging server. Moving it out of the way."
267 mv "$domain_dir" "${domain_dir}.staging"
268 moved_staging=1
269 else
270 log "Found previous cert config. Issuing renew."
271 [ "$keylength_ecc" -eq "1" ] && acme_args="$acme_args --ecc"
272 run_acme --home "$STATE_DIR" --renew -d "$main_domain" $acme_args && ret=0 || ret=1
273 post_checks
274 return $ret
275 fi
276 fi
277
278
279 acme_args="$acme_args $(for d in $domains; do echo -n "-d $d "; done)"
280 acme_args="$acme_args --keylength $keylength"
281 [ -n "$ACCOUNT_EMAIL" ] && acme_args="$acme_args --accountemail $ACCOUNT_EMAIL"
282
283 if [ -n "$acme_server" ]; then
284 log "Using custom ACME server URL"
285 acme_args="$acme_args --server $acme_server"
286 else
287 # default to letsencrypt because the upstream default may change
288 if [ "$use_staging" -eq "1" ]; then
289 acme_args="$acme_args --server letsencrypt_test"
290 else
291 acme_args="$acme_args --server letsencrypt"
292 fi
293 fi
294
295 if [ -n "$days" ]; then
296 log "Renewing after $days days"
297 acme_args="$acme_args --days $days"
298 fi
299
300 if [ -n "$dns" ]; then
301 log "Using dns mode"
302 acme_args="$acme_args --dns $dns"
303 if [ -n "$dalias" ]; then
304 log "Using domain alias for dns mode"
305 acme_args="$acme_args --domain-alias $dalias"
306 if [ -n "$calias" ]; then
307 err "Both domain and challenge aliases are defined. Ignoring the challenge alias."
308 fi
309 elif [ -n "$calias" ]; then
310 log "Using challenge alias for dns mode"
311 acme_args="$acme_args --challenge-alias $calias"
312 fi
313 elif [ -z "$webroot" ]; then
314 log "Using standalone mode"
315 acme_args="$acme_args --standalone --listen-v6"
316 else
317 if [ ! -d "$webroot" ]; then
318 err "$main_domain: Webroot dir '$webroot' does not exist!"
319 post_checks
320 return 1
321 fi
322 log "Using webroot dir: $webroot"
323 acme_args="$acme_args --webroot $webroot"
324 fi
325
326 if ! run_acme --home "$STATE_DIR" --issue $acme_args; then
327 failed_dir="${domain_dir}.failed-$(date +%s)"
328 err "Issuing cert for $main_domain failed. Moving state to $failed_dir"
329 [ -d "$domain_dir" ] && mv "$domain_dir" "$failed_dir"
330 if [ "$moved_staging" -eq "1" ]; then
331 err "Restoring staging certificate"
332 mv "${domain_dir}.staging" "${domain_dir}"
333 fi
334 post_checks
335 return 1
336 fi
337
338 if [ -e /etc/init.d/uhttpd ] && [ "$update_uhttpd" -eq "1" ]; then
339 uci set uhttpd.main.key="${domain_dir}/${main_domain}.key"
340 uci set uhttpd.main.cert="${domain_dir}/fullchain.cer"
341 # commit and reload is in post_checks
342 fi
343
344 local nginx_updated
345 nginx_updated=0
346 if command -v nginx-util 2>/dev/null && [ "$update_nginx" -eq "1" ]; then
347 nginx_updated=1
348 for domain in $domains; do
349 nginx-util add_ssl "${domain}" acme "${domain_dir}/fullchain.cer" \
350 "${domain_dir}/${main_domain}.key" || nginx_updated=0
351 done
352 # reload is in post_checks
353 fi
354
355 if [ "$nginx_updated" -eq "0" ] && [ -w /etc/nginx/nginx.conf ] && [ "$update_nginx" -eq "1" ]; then
356 sed -i "s#ssl_certificate\ .*#ssl_certificate ${domain_dir}/fullchain.cer;#g" /etc/nginx/nginx.conf
357 sed -i "s#ssl_certificate_key\ .*#ssl_certificate_key ${domain_dir}/${main_domain}.key;#g" /etc/nginx/nginx.conf
358 # commit and reload is in post_checks
359 fi
360
361 post_checks
362}
363
364load_vars()
365{
366 local section="$1"
367
368 STATE_DIR=$(config_get "$section" state_dir)
369 ACCOUNT_EMAIL=$(config_get "$section" account_email)
370 DEBUG=$(config_get "$section" debug)
371}
372
373check_cron
374[ -n "$CHECK_CRON" ] && exit 0
375[ -e "/var/run/acme_boot" ] && rm -f "/var/run/acme_boot" && exit 0
376
377config_load acme
378config_foreach load_vars acme
379
380if [ -z "$STATE_DIR" ] || [ -z "$ACCOUNT_EMAIL" ]; then
381 err "state_dir and account_email must be set"
382 exit 1
383fi
384
385[ -d "$STATE_DIR" ] || mkdir -p "$STATE_DIR"
386
387trap err_out HUP TERM
388trap int_out INT
389
390config_foreach issue_cert cert
391
392exit 0