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