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.

394 lines
11 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. . /lib/functions.sh
  36. check_cron()
  37. {
  38. [ -f "/etc/crontabs/root" ] && grep -q '/etc/init.d/acme' /etc/crontabs/root && return
  39. echo "0 0 * * * /etc/init.d/acme start" >> /etc/crontabs/root
  40. /etc/init.d/cron start
  41. }
  42. log()
  43. {
  44. logger -t $APP -s -p daemon.info "$@"
  45. }
  46. err()
  47. {
  48. logger -t $APP -s -p daemon.err "$@"
  49. }
  50. debug()
  51. {
  52. [ "$DEBUG" -eq "1" ] && logger -t $APP -s -p daemon.debug "$@"
  53. }
  54. get_listeners() {
  55. local proto rq sq listen remote state program
  56. netstat -nptl 2>/dev/null | while read proto listen program; do
  57. case "$proto#$listen#$program" in
  58. tcp#*:80#[0-9]*/*) echo -n "${program%% *} " ;;
  59. esac
  60. done
  61. }
  62. pre_checks()
  63. {
  64. main_domain="$1"
  65. log "Running pre checks for $main_domain."
  66. listeners="$(get_listeners)"
  67. debug "port80 listens: $listeners"
  68. for listener in $(get_listeners); do
  69. pid="${listener%/*}"
  70. cmd="${listener#*/}"
  71. case "$cmd" in
  72. uhttpd)
  73. debug "Found uhttpd listening on port 80"
  74. if [ "$APP" = "acme" ]; then
  75. UHTTPD_LISTEN_HTTP=$(uci get uhttpd.main.listen_http)
  76. if [ -z "$UHTTPD_LISTEN_HTTP" ]; then
  77. err "$main_domain: Unable to find uhttpd listen config."
  78. err "Manually disable uhttpd or set webroot to continue."
  79. return 1
  80. fi
  81. uci set uhttpd.main.listen_http=''
  82. uci commit uhttpd || return 1
  83. if ! /etc/init.d/uhttpd reload ; then
  84. uci set uhttpd.main.listen_http="$UHTTPD_LISTEN_HTTP"
  85. uci commit uhttpd
  86. return 1
  87. fi
  88. fi
  89. ;;
  90. nginx*)
  91. debug "Found nginx listening on port 80"
  92. NGINX_WEBSERVER=1
  93. if [ "$APP" = "acme" ]; then
  94. local tries=0
  95. while grep -sq "$cmd" "/proc/$pid/cmdline" && kill -0 "$pid"; do
  96. /etc/init.d/nginx stop
  97. if [ $tries -gt 10 ]; then
  98. debug "Can't stop nginx. Terminating script."
  99. return 1
  100. fi
  101. debug "Waiting for nginx to stop..."
  102. tries=$((tries + 1))
  103. sleep 1
  104. done
  105. fi
  106. ;;
  107. "")
  108. err "Nothing listening on port 80."
  109. err "Standalone mode not supported, setup uhttpd or nginx"
  110. return 1
  111. ;;
  112. *)
  113. err "$main_domain: unsupported (apache/haproxy?) daemon is listening on port 80."
  114. err "if webroot is set on your current webserver comment line 132 (return 1) from this script."
  115. return 1
  116. ;;
  117. esac
  118. done
  119. iptables -I input_rule -p tcp --dport 80 -j ACCEPT -m comment --comment "ACME" || return 1
  120. debug "v4 input_rule: $(iptables -nvL input_rule)"
  121. if [ -e "/usr/sbin/ip6tables" ]; then
  122. ip6tables -I input_rule -p tcp --dport 80 -j ACCEPT -m comment --comment "ACME" || return 1
  123. debug "v6 input_rule: $(ip6tables -nvL input_rule)"
  124. fi
  125. return 0
  126. }
  127. post_checks()
  128. {
  129. log "Running post checks (cleanup)."
  130. # The comment ensures we only touch our own rules. If no rules exist, that
  131. # is fine, so hide any errors
  132. iptables -D input_rule -p tcp --dport 80 -j ACCEPT -m comment --comment "ACME" 2>/dev/null
  133. if [ -e "/usr/sbin/ip6tables" ]; then
  134. ip6tables -D input_rule -p tcp --dport 80 -j ACCEPT -m comment --comment "ACME" 2>/dev/null
  135. fi
  136. if [ -e /etc/init.d/uhttpd ] && [ "$UPDATE_UHTTPD" -eq 1 ]; then
  137. uci commit uhttpd
  138. /etc/init.d/uhttpd reload
  139. log "Restarting uhttpd..."
  140. fi
  141. if [ -e /etc/init.d/nginx ] && ( [ "$NGINX_WEBSERVER" -eq 1 ] || [ "$UPDATE_NGINX" -eq 1 ]; ); then
  142. NGINX_WEBSERVER=0
  143. /etc/init.d/nginx restart
  144. log "Restarting nginx..."
  145. fi
  146. if [ -e /etc/init.d/haproxy ] && [ "$UPDATE_HAPROXY" -eq 1 ]; then
  147. /etc/init.d/haproxy restart
  148. log "Restarting haproxy..."
  149. fi
  150. }
  151. err_out()
  152. {
  153. post_checks
  154. exit 1
  155. }
  156. int_out()
  157. {
  158. post_checks
  159. trap - INT
  160. kill -INT $$
  161. }
  162. is_staging()
  163. {
  164. local main_domain="$1"
  165. grep -q "acme-staging" "$STATE_DIR/$main_domain/${main_domain}.conf"
  166. return $?
  167. }
  168. issue_cert()
  169. {
  170. local section="$1"
  171. local acme_args=
  172. local debug=
  173. local enabled
  174. local use_staging
  175. local update_uhttpd
  176. local update_nginx
  177. local update_haproxy
  178. local keylength
  179. local domains
  180. local main_domain
  181. local failed_dir
  182. local webroot
  183. local dns
  184. local ret
  185. local staging=
  186. local HOOK=
  187. config_get_bool enabled "$section" enabled 0
  188. config_get_bool use_staging "$section" use_staging
  189. config_get_bool update_uhttpd "$section" update_uhttpd
  190. config_get_bool update_nginx "$section" update_nginx
  191. config_get_bool update_haproxy "$section" update_haproxy
  192. config_get domains "$section" domains
  193. config_get keylength "$section" keylength
  194. config_get webroot "$section" webroot
  195. config_get dns "$section" dns
  196. UPDATE_NGINX=$update_nginx
  197. UPDATE_UHTTPD=$update_uhttpd
  198. UPDATE_HAPROXY=$update_haproxy
  199. [ "$enabled" -eq "1" ] || return
  200. if [ "$APP" = "uacme" ]; then
  201. [ "$DEBUG" -eq "1" ] && debug="--verbose --verbose"
  202. elif [ "$APP" = "acme" ]; then
  203. [ "$DEBUG" -eq "1" ] && acme_args="$acme_args --debug"
  204. fi
  205. [ "$use_staging" -eq "1" ] && STATE_DIR="$STAGING_STATE_DIR" && staging="--staging"
  206. set -- $domains
  207. main_domain=$1
  208. [ -n "$webroot" ] || [ -n "$dns" ] || pre_checks "$main_domain" || return 1
  209. log "Running $APP for $main_domain"
  210. if [ "$APP" = "uacme" ]; then
  211. if [ ! -f "$STATE_DIR/private/key.pem" ]; then
  212. log "Create a new ACME account with email $ACCOUNT_EMAIL use staging=$use_staging"
  213. $ACME $debug --confdir "$STATE_DIR" $staging --yes new $ACCOUNT_EMAIL
  214. fi
  215. if [ -f "$STATE_DIR/$main_domain/cert.pem" ]; then
  216. log "Found previous cert config, use staging=$use_staging. Issuing renew."
  217. export CHALLENGE_PATH="$webroot"
  218. $ACME $debug --confdir "$STATE_DIR" $staging --never-create issue $domains --hook=$HPROGRAM && ret=0 || ret=1
  219. post_checks
  220. return $ret
  221. fi
  222. fi
  223. if [ "$APP" = "acme" ]; then
  224. handle_credentials() {
  225. local credential="$1"
  226. eval export "$credential"
  227. }
  228. config_list_foreach "$section" credentials handle_credentials
  229. if [ -e "$STATE_DIR/$main_domain" ]; then
  230. if [ "$use_staging" -eq "0" ] && is_staging "$main_domain"; then
  231. log "Found previous cert issued using staging server. Moving it out of the way."
  232. mv "$STATE_DIR/$main_domain" "$STATE_DIR/$main_domain.staging"
  233. else
  234. log "Found previous cert config. Issuing renew."
  235. $ACME --home "$STATE_DIR" --renew -d "$main_domain" "$acme_args" && ret=0 || ret=1
  236. post_checks
  237. return $ret
  238. fi
  239. fi
  240. fi
  241. acme_args="$acme_args --bits $keylength"
  242. acme_args="$acme_args $(for d in $domains; do echo -n " $d "; done)"
  243. if [ "$APP" = "acme" ]; then
  244. [ -n "$ACCOUNT_EMAIL" ] && acme_args="$acme_args --accountemail $ACCOUNT_EMAIL"
  245. [ "$use_staging" -eq "1" ] && acme_args="$acme_args --staging"
  246. fi
  247. if [ -n "$dns" ]; then
  248. #TO-DO
  249. if [ "$APP" = "acme" ]; then
  250. log "Using dns mode"
  251. acme_args="$acme_args --dns $dns"
  252. else
  253. log "Using dns mode, dns-01 is not wrapped yet"
  254. return 1
  255. # uacme_args="$uacme_args --dns $dns"
  256. fi
  257. elif [ -z "$webroot" ]; then
  258. if [ "$APP" = "acme" ]; then
  259. log "Using standalone mode"
  260. acme_args="$acme_args --standalone --listen-v6"
  261. else
  262. log "Standalone not supported by $APP"
  263. return 1
  264. fi
  265. else
  266. if [ ! -d "$webroot" ]; then
  267. err "$main_domain: Webroot dir '$webroot' does not exist!"
  268. post_checks
  269. return 1
  270. fi
  271. log "Using webroot dir: $webroot"
  272. if [ "$APP" = "uacme" ]; then
  273. export CHALLENGE_PATH="$webroot"
  274. else
  275. acme_args="$acme_args --webroot $webroot"
  276. fi
  277. fi
  278. if [ "$APP" = "uacme" ]; then
  279. workdir="--confdir"
  280. HOOK="--hook=$HPROGRAM"
  281. else
  282. workdir="--home"
  283. fi
  284. if ! $ACME $debug $workdir "$STATE_DIR" $staging issue $acme_args $HOOK; then
  285. failed_dir="$STATE_DIR/${main_domain}.failed-$(date +%s)"
  286. err "Issuing cert for $main_domain failed. Moving state to $failed_dir"
  287. [ -d "$STATE_DIR/$main_domain" ] && mv "$STATE_DIR/$main_domain" "$failed_dir"
  288. [ -d "$STATE_DIR/private/$main_domain" ] && mv "$STATE_DIR/private/$main_domain" "$failed_dir"
  289. post_checks
  290. return 1
  291. fi
  292. if [ -e /etc/init.d/uhttpd ] && [ "$update_uhttpd" -eq "1" ]; then
  293. if [ "$APP" = "uacme" ]; then
  294. uci set uhttpd.main.key="$STATE_DIR/private/${main_domain}/key.pem"
  295. uci set uhttpd.main.cert="$STATE_DIR/${main_domain}/cert.pem"
  296. else
  297. uci set uhttpd.main.key="$STATE_DIR/${main_domain}/${main_domain}.key"
  298. uci set uhttpd.main.cert="$STATE_DIR/${main_domain}/fullchain.cer"
  299. fi
  300. # commit and reload is in post_checks
  301. fi
  302. if [ -e /etc/init.d/nginx ] && [ "$update_nginx" -eq "1" ]; then
  303. if [ "$APP" = "uacme" ]; then
  304. sed -i "s#ssl_certificate\ .*#ssl_certificate $STATE_DIR/${main_domain}/cert.pem;#g" /etc/nginx/nginx.conf
  305. sed -i "s#ssl_certificate_key\ .*#ssl_certificate_key $STATE_DIR/private/${main_domain}/key.pem;#g" /etc/nginx/nginx.conf
  306. else
  307. sed -i "s#ssl_certificate\ .*#ssl_certificate $STATE_DIR/${main_domain}/fullchain.cer;#g" /etc/nginx/nginx.conf
  308. sed -i "s#ssl_certificate_key\ .*#ssl_certificate_key $STATE_DIR/${main_domain}/${main_domain}.key;#g" /etc/nginx/nginx.conf
  309. fi
  310. # commit and reload is in post_checks
  311. fi
  312. if [ -e /etc/init.d/haproxy ] && [ "$update_haproxy" -eq 1 ]; then
  313. if [ "$APP" = "uacme" ]; then
  314. cat $STATE_DIR/${main_domain}/cert.pem $STATE_DIR/private/${main_domain}/key.pem > $STATE_DIR/${main_domain}/full_haproxy.pem
  315. else
  316. cat $STATE_DIR/${main_domain}/fullchain.cer $STATE_DIR/${main_domain}/${main_domain}.key > $STATE_DIR/${main_domain}/full_haproxy.pem
  317. fi
  318. fi
  319. post_checks
  320. }
  321. load_vars()
  322. {
  323. local section="$1"
  324. STATE_DIR=$(config_get "$section" state_dir)
  325. STAGING_STATE_DIR=$STATE_DIR/staging
  326. ACCOUNT_EMAIL=$(config_get "$section" account_email)
  327. DEBUG=$(config_get "$section" debug)
  328. }
  329. check_cron
  330. [ -n "$CHECK_CRON" ] && exit 0
  331. [ -e "/var/run/acme_boot" ] && rm -f "/var/run/acme_boot" && exit 0
  332. config_load acme
  333. config_foreach load_vars acme
  334. if [ -z "$STATE_DIR" ] || [ -z "$ACCOUNT_EMAIL" ]; then
  335. err "state_dir and account_email must be set"
  336. exit 1
  337. fi
  338. [ -d "$STATE_DIR" ] || mkdir -p "$STATE_DIR"
  339. [ -d "$STAGING_STATE_DIR" ] || mkdir -p "$STAGING_STATE_DIR"
  340. trap err_out HUP TERM
  341. trap int_out INT
  342. config_foreach issue_cert cert
  343. exit 0