You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

476 lines
13 KiB

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