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.

4532 lines
111 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
8 years ago
9 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
8 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
8 years ago
9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
8 years ago
9 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
8 years ago
  1. #!/usr/bin/env sh
  2. VER=2.6.4
  3. PROJECT_NAME="acme.sh"
  4. PROJECT_ENTRY="acme.sh"
  5. PROJECT="https://github.com/Neilpang/$PROJECT_NAME"
  6. DEFAULT_INSTALL_HOME="$HOME/.$PROJECT_NAME"
  7. _SCRIPT_="$0"
  8. _SUB_FOLDERS="dnsapi deploy"
  9. DEFAULT_CA="https://acme-v01.api.letsencrypt.org"
  10. DEFAULT_AGREEMENT="https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf"
  11. DEFAULT_USER_AGENT="$PROJECT_ENTRY client v$VER : $PROJECT"
  12. DEFAULT_ACCOUNT_EMAIL=""
  13. DEFAULT_ACCOUNT_KEY_LENGTH=2048
  14. DEFAULT_DOMAIN_KEY_LENGTH=2048
  15. STAGE_CA="https://acme-staging.api.letsencrypt.org"
  16. VTYPE_HTTP="http-01"
  17. VTYPE_DNS="dns-01"
  18. VTYPE_TLS="tls-sni-01"
  19. VTYPE_TLS2="tls-sni-02"
  20. LOCAL_ANY_ADDRESS="0.0.0.0"
  21. MAX_RENEW=60
  22. DEFAULT_DNS_SLEEP=120
  23. NO_VALUE="no"
  24. W_TLS="tls"
  25. STATE_VERIFIED="verified_ok"
  26. BEGIN_CSR="-----BEGIN CERTIFICATE REQUEST-----"
  27. END_CSR="-----END CERTIFICATE REQUEST-----"
  28. BEGIN_CERT="-----BEGIN CERTIFICATE-----"
  29. END_CERT="-----END CERTIFICATE-----"
  30. RENEW_SKIP=2
  31. ECC_SEP="_"
  32. ECC_SUFFIX="${ECC_SEP}ecc"
  33. LOG_LEVEL_1=1
  34. LOG_LEVEL_2=2
  35. LOG_LEVEL_3=3
  36. DEFAULT_LOG_LEVEL="$LOG_LEVEL_1"
  37. _DEBUG_WIKI="https://github.com/Neilpang/acme.sh/wiki/How-to-debug-acme.sh"
  38. __INTERACTIVE=""
  39. if [ -t 1 ]; then
  40. __INTERACTIVE="1"
  41. fi
  42. __green() {
  43. if [ "$__INTERACTIVE" ]; then
  44. printf '\033[1;31;32m'
  45. fi
  46. printf -- "$1"
  47. if [ "$__INTERACTIVE" ]; then
  48. printf '\033[0m'
  49. fi
  50. }
  51. __red() {
  52. if [ "$__INTERACTIVE" ]; then
  53. printf '\033[1;31;40m'
  54. fi
  55. printf -- "$1"
  56. if [ "$__INTERACTIVE" ]; then
  57. printf '\033[0m'
  58. fi
  59. }
  60. _printargs() {
  61. if [ -z "$2" ]; then
  62. printf -- "[$(date)] $1"
  63. else
  64. printf -- "[$(date)] $1='$2'"
  65. fi
  66. printf "\n"
  67. }
  68. _dlg_versions() {
  69. echo "Diagnosis versions: "
  70. echo "openssl:"
  71. if _exists openssl; then
  72. openssl version 2>&1
  73. else
  74. echo "openssl doesn't exists."
  75. fi
  76. echo "apache:"
  77. if [ "$_APACHECTL" ] && _exists "$_APACHECTL"; then
  78. _APACHECTL -V 2>&1
  79. else
  80. echo "apache doesn't exists."
  81. fi
  82. echo "nc:"
  83. if _exists "nc"; then
  84. nc -h 2>&1
  85. else
  86. _debug "nc doesn't exists."
  87. fi
  88. }
  89. _log() {
  90. [ -z "$LOG_FILE" ] && return
  91. _printargs "$@" >>"$LOG_FILE"
  92. }
  93. _info() {
  94. _log "$@"
  95. _printargs "$@"
  96. }
  97. _err() {
  98. _log "$@"
  99. printf -- "[$(date)] " >&2
  100. if [ -z "$2" ]; then
  101. __red "$1" >&2
  102. else
  103. __red "$1='$2'" >&2
  104. fi
  105. printf "\n" >&2
  106. return 1
  107. }
  108. _usage() {
  109. __red "$@" >&2
  110. printf "\n" >&2
  111. }
  112. _debug() {
  113. if [ -z "$LOG_LEVEL" ] || [ "$LOG_LEVEL" -ge "$LOG_LEVEL_1" ]; then
  114. _log "$@"
  115. fi
  116. if [ -z "$DEBUG" ]; then
  117. return
  118. fi
  119. _printargs "$@" >&2
  120. }
  121. _debug2() {
  122. if [ "$LOG_LEVEL" ] && [ "$LOG_LEVEL" -ge "$LOG_LEVEL_2" ]; then
  123. _log "$@"
  124. fi
  125. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
  126. _debug "$@"
  127. fi
  128. }
  129. _debug3() {
  130. if [ "$LOG_LEVEL" ] && [ "$LOG_LEVEL" -ge "$LOG_LEVEL_3" ]; then
  131. _log "$@"
  132. fi
  133. if [ "$DEBUG" ] && [ "$DEBUG" -ge "3" ]; then
  134. _debug "$@"
  135. fi
  136. }
  137. _startswith() {
  138. _str="$1"
  139. _sub="$2"
  140. echo "$_str" | grep "^$_sub" >/dev/null 2>&1
  141. }
  142. _endswith() {
  143. _str="$1"
  144. _sub="$2"
  145. echo "$_str" | grep -- "$_sub\$" >/dev/null 2>&1
  146. }
  147. _contains() {
  148. _str="$1"
  149. _sub="$2"
  150. echo "$_str" | grep -- "$_sub" >/dev/null 2>&1
  151. }
  152. _hasfield() {
  153. _str="$1"
  154. _field="$2"
  155. _sep="$3"
  156. if [ -z "$_field" ]; then
  157. _usage "Usage: str field [sep]"
  158. return 1
  159. fi
  160. if [ -z "$_sep" ]; then
  161. _sep=","
  162. fi
  163. for f in $(echo "$_str" | tr ',' ' '); do
  164. if [ "$f" = "$_field" ]; then
  165. _debug2 "'$_str' contains '$_field'"
  166. return 0 #contains ok
  167. fi
  168. done
  169. _debug2 "'$_str' does not contain '$_field'"
  170. return 1 #not contains
  171. }
  172. _getfield() {
  173. _str="$1"
  174. _findex="$2"
  175. _sep="$3"
  176. if [ -z "$_findex" ]; then
  177. _usage "Usage: str field [sep]"
  178. return 1
  179. fi
  180. if [ -z "$_sep" ]; then
  181. _sep=","
  182. fi
  183. _ffi="$_findex"
  184. while [ "$_ffi" -gt "0" ]; do
  185. _fv="$(echo "$_str" | cut -d "$_sep" -f "$_ffi")"
  186. if [ "$_fv" ]; then
  187. printf -- "%s" "$_fv"
  188. return 0
  189. fi
  190. _ffi="$(_math "$_ffi" - 1)"
  191. done
  192. printf -- "%s" "$_str"
  193. }
  194. _exists() {
  195. cmd="$1"
  196. if [ -z "$cmd" ]; then
  197. _usage "Usage: _exists cmd"
  198. return 1
  199. fi
  200. if type command >/dev/null 2>&1; then
  201. command -v "$cmd" >/dev/null 2>&1
  202. else
  203. type "$cmd" >/dev/null 2>&1
  204. fi
  205. ret="$?"
  206. _debug3 "$cmd exists=$ret"
  207. return $ret
  208. }
  209. #a + b
  210. _math() {
  211. expr "$@"
  212. }
  213. _h_char_2_dec() {
  214. _ch=$1
  215. case "${_ch}" in
  216. a | A)
  217. printf "10"
  218. ;;
  219. b | B)
  220. printf "11"
  221. ;;
  222. c | C)
  223. printf "12"
  224. ;;
  225. d | D)
  226. printf "13"
  227. ;;
  228. e | E)
  229. printf "14"
  230. ;;
  231. f | F)
  232. printf "15"
  233. ;;
  234. *)
  235. printf "%s" "$_ch"
  236. ;;
  237. esac
  238. }
  239. _URGLY_PRINTF=""
  240. if [ "$(printf '\x41')" != 'A' ]; then
  241. _URGLY_PRINTF=1
  242. fi
  243. _h2b() {
  244. hex=$(cat)
  245. i=1
  246. j=2
  247. if _exists let; then
  248. uselet="1"
  249. fi
  250. _debug3 uselet "$uselet"
  251. _debug3 _URGLY_PRINTF "$_URGLY_PRINTF"
  252. while true; do
  253. if [ -z "$_URGLY_PRINTF" ]; then
  254. h="$(printf "%s" "$hex" | cut -c $i-$j)"
  255. if [ -z "$h" ]; then
  256. break
  257. fi
  258. printf "\x$h"
  259. else
  260. ic="$(printf "%s" "$hex" | cut -c $i)"
  261. jc="$(printf "%s" "$hex" | cut -c $j)"
  262. if [ -z "$ic$jc" ]; then
  263. break
  264. fi
  265. ic="$(_h_char_2_dec "$ic")"
  266. jc="$(_h_char_2_dec "$jc")"
  267. printf '\'"$(printf "%o" "$(_math "$ic" \* 16 + $jc)")"
  268. fi
  269. if [ "$uselet" ]; then
  270. let "i+=2" >/dev/null
  271. let "j+=2" >/dev/null
  272. else
  273. i="$(_math "$i" + 2)"
  274. j="$(_math "$j" + 2)"
  275. fi
  276. done
  277. }
  278. #options file
  279. _sed_i() {
  280. options="$1"
  281. filename="$2"
  282. if [ -z "$filename" ]; then
  283. _usage "Usage:_sed_i options filename"
  284. return 1
  285. fi
  286. _debug2 options "$options"
  287. if sed -h 2>&1 | grep "\-i\[SUFFIX]" >/dev/null 2>&1; then
  288. _debug "Using sed -i"
  289. sed -i "$options" "$filename"
  290. else
  291. _debug "No -i support in sed"
  292. text="$(cat "$filename")"
  293. echo "$text" | sed "$options" >"$filename"
  294. fi
  295. }
  296. _egrep_o() {
  297. if _contains "$(egrep -o 2>&1)" "egrep: illegal option -- o"; then
  298. sed -n 's/.*\('"$1"'\).*/\1/p'
  299. else
  300. egrep -o "$1"
  301. fi
  302. }
  303. #Usage: file startline endline
  304. _getfile() {
  305. filename="$1"
  306. startline="$2"
  307. endline="$3"
  308. if [ -z "$endline" ]; then
  309. _usage "Usage: file startline endline"
  310. return 1
  311. fi
  312. i="$(grep -n -- "$startline" "$filename" | cut -d : -f 1)"
  313. if [ -z "$i" ]; then
  314. _err "Can not find start line: $startline"
  315. return 1
  316. fi
  317. i="$(_math "$i" + 1)"
  318. _debug i "$i"
  319. j="$(grep -n -- "$endline" "$filename" | cut -d : -f 1)"
  320. if [ -z "$j" ]; then
  321. _err "Can not find end line: $endline"
  322. return 1
  323. fi
  324. j="$(_math "$j" - 1)"
  325. _debug j "$j"
  326. sed -n "$i,${j}p" "$filename"
  327. }
  328. #Usage: multiline
  329. _base64() {
  330. if [ "$1" ]; then
  331. openssl base64 -e
  332. else
  333. openssl base64 -e | tr -d '\r\n'
  334. fi
  335. }
  336. #Usage: multiline
  337. _dbase64() {
  338. if [ "$1" ]; then
  339. openssl base64 -d -A
  340. else
  341. openssl base64 -d
  342. fi
  343. }
  344. #Usage: hashalg [outputhex]
  345. #Output Base64-encoded digest
  346. _digest() {
  347. alg="$1"
  348. if [ -z "$alg" ]; then
  349. _usage "Usage: _digest hashalg"
  350. return 1
  351. fi
  352. outputhex="$2"
  353. if [ "$alg" = "sha256" ] || [ "$alg" = "sha1" ]; then
  354. if [ "$outputhex" ]; then
  355. openssl dgst -"$alg" -hex | cut -d = -f 2 | tr -d ' '
  356. else
  357. openssl dgst -"$alg" -binary | _base64
  358. fi
  359. else
  360. _err "$alg is not supported yet"
  361. return 1
  362. fi
  363. }
  364. #Usage: hashalg secret [outputhex]
  365. #Output Base64-encoded hmac
  366. _hmac() {
  367. alg="$1"
  368. hmac_sec="$2"
  369. outputhex="$3"
  370. if [ -z "$hmac_sec" ]; then
  371. _usage "Usage: _hmac hashalg secret [outputhex]"
  372. return 1
  373. fi
  374. if [ "$alg" = "sha256" ] || [ "$alg" = "sha1" ]; then
  375. if [ "$outputhex" ]; then
  376. openssl dgst -"$alg" -hmac "$hmac_sec" | cut -d = -f 2 | tr -d ' '
  377. else
  378. openssl dgst -"$alg" -hmac "$hmac_sec" -binary | _base64
  379. fi
  380. else
  381. _err "$alg is not supported yet"
  382. return 1
  383. fi
  384. }
  385. #Usage: keyfile hashalg
  386. #Output: Base64-encoded signature value
  387. _sign() {
  388. keyfile="$1"
  389. alg="$2"
  390. if [ -z "$alg" ]; then
  391. _usage "Usage: _sign keyfile hashalg"
  392. return 1
  393. fi
  394. _sign_openssl="openssl dgst -sign $keyfile "
  395. if [ "$alg" = "sha256" ]; then
  396. _sign_openssl="$_sign_openssl -$alg"
  397. else
  398. _err "$alg is not supported yet"
  399. return 1
  400. fi
  401. if grep "BEGIN RSA PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then
  402. $_sign_openssl | _base64
  403. elif grep "BEGIN EC PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then
  404. if ! _signedECText="$($_sign_openssl | openssl asn1parse -inform DER)"; then
  405. _err "Sign failed: $_sign_openssl"
  406. _err "Key file: $keyfile"
  407. _err "Key content:$(wc -l <"$keyfile") lises"
  408. return 1
  409. fi
  410. _debug3 "_signedECText" "$_signedECText"
  411. _ec_r="$(echo "$_signedECText" | _head_n 2 | _tail_n 1 | cut -d : -f 4 | tr -d "\r\n")"
  412. _debug3 "_ec_r" "$_ec_r"
  413. _ec_s="$(echo "$_signedECText" | _head_n 3 | _tail_n 1 | cut -d : -f 4 | tr -d "\r\n")"
  414. _debug3 "_ec_s" "$_ec_s"
  415. printf "%s" "$_ec_r$_ec_s" | _h2b | _base64
  416. else
  417. _err "Unknown key file format."
  418. return 1
  419. fi
  420. }
  421. #keylength
  422. _isEccKey() {
  423. _length="$1"
  424. if [ -z "$_length" ]; then
  425. return 1
  426. fi
  427. [ "$_length" != "1024" ] \
  428. && [ "$_length" != "2048" ] \
  429. && [ "$_length" != "3072" ] \
  430. && [ "$_length" != "4096" ] \
  431. && [ "$_length" != "8192" ]
  432. }
  433. # _createkey 2048|ec-256 file
  434. _createkey() {
  435. length="$1"
  436. f="$2"
  437. eccname="$length"
  438. if _startswith "$length" "ec-"; then
  439. length=$(printf "%s" "$length" | cut -d '-' -f 2-100)
  440. if [ "$length" = "256" ]; then
  441. eccname="prime256v1"
  442. fi
  443. if [ "$length" = "384" ]; then
  444. eccname="secp384r1"
  445. fi
  446. if [ "$length" = "521" ]; then
  447. eccname="secp521r1"
  448. fi
  449. fi
  450. if [ -z "$length" ]; then
  451. length=2048
  452. fi
  453. _debug "Use length $length"
  454. if _isEccKey "$length"; then
  455. _debug "Using ec name: $eccname"
  456. openssl ecparam -name $eccname -genkey 2>/dev/null >"$f"
  457. else
  458. _debug "Using RSA: $length"
  459. openssl genrsa $length 2>/dev/null >"$f"
  460. fi
  461. if [ "$?" != "0" ]; then
  462. _err "Create key error."
  463. return 1
  464. fi
  465. }
  466. #domain
  467. _is_idn() {
  468. _is_idn_d="$1"
  469. _debug2 _is_idn_d "$_is_idn_d"
  470. _idn_temp=$(printf "%s" "$_is_idn_d" | tr -d '[0-9]' | tr -d '[a-z]' | tr -d 'A-Z' | tr -d '.,-')
  471. _debug2 _idn_temp "$_idn_temp"
  472. [ "$_idn_temp" ]
  473. }
  474. #aa.com
  475. #aa.com,bb.com,cc.com
  476. _idn() {
  477. __idn_d="$1"
  478. if ! _is_idn "$__idn_d"; then
  479. printf "%s" "$__idn_d"
  480. return 0
  481. fi
  482. if _exists idn; then
  483. if _contains "$__idn_d" ','; then
  484. _i_first="1"
  485. for f in $(echo "$__idn_d" | tr ',' ' '); do
  486. [ -z "$f" ] && continue
  487. if [ -z "$_i_first" ]; then
  488. printf "%s" ","
  489. else
  490. _i_first=""
  491. fi
  492. idn --quiet "$f" | tr -d "\r\n"
  493. done
  494. else
  495. idn "$__idn_d" | tr -d "\r\n"
  496. fi
  497. else
  498. _err "Please install idn to process IDN names."
  499. fi
  500. }
  501. #_createcsr cn san_list keyfile csrfile conf
  502. _createcsr() {
  503. _debug _createcsr
  504. domain="$1"
  505. domainlist="$2"
  506. csrkey="$3"
  507. csr="$4"
  508. csrconf="$5"
  509. _debug2 domain "$domain"
  510. _debug2 domainlist "$domainlist"
  511. _debug2 csrkey "$csrkey"
  512. _debug2 csr "$csr"
  513. _debug2 csrconf "$csrconf"
  514. printf "[ req_distinguished_name ]\n[ req ]\ndistinguished_name = req_distinguished_name\nreq_extensions = v3_req\n[ v3_req ]\n\nkeyUsage = nonRepudiation, digitalSignature, keyEncipherment" >"$csrconf"
  515. if [ -z "$domainlist" ] || [ "$domainlist" = "$NO_VALUE" ]; then
  516. #single domain
  517. _info "Single domain" "$domain"
  518. else
  519. domainlist="$(_idn "$domainlist")"
  520. _debug2 domainlist "$domainlist"
  521. if _contains "$domainlist" ","; then
  522. alt="DNS:$(echo "$domainlist" | sed "s/,/,DNS:/g")"
  523. else
  524. alt="DNS:$domainlist"
  525. fi
  526. #multi
  527. _info "Multi domain" "$alt"
  528. printf -- "\nsubjectAltName=$alt" >>"$csrconf"
  529. fi
  530. if [ "$Le_OCSP_Stable" ]; then
  531. _savedomainconf Le_OCSP_Stable "$Le_OCSP_Stable"
  532. printf -- "\nbasicConstraints = CA:FALSE\n1.3.6.1.5.5.7.1.24=DER:30:03:02:01:05" >>"$csrconf"
  533. fi
  534. _csr_cn="$(_idn "$domain")"
  535. _debug2 _csr_cn "$_csr_cn"
  536. openssl req -new -sha256 -key "$csrkey" -subj "/CN=$_csr_cn" -config "$csrconf" -out "$csr"
  537. }
  538. #_signcsr key csr conf cert
  539. _signcsr() {
  540. key="$1"
  541. csr="$2"
  542. conf="$3"
  543. cert="$4"
  544. _debug "_signcsr"
  545. _msg="$(openssl x509 -req -days 365 -in "$csr" -signkey "$key" -extensions v3_req -extfile "$conf" -out "$cert" 2>&1)"
  546. _ret="$?"
  547. _debug "$_msg"
  548. return $_ret
  549. }
  550. #_csrfile
  551. _readSubjectFromCSR() {
  552. _csrfile="$1"
  553. if [ -z "$_csrfile" ]; then
  554. _usage "_readSubjectFromCSR mycsr.csr"
  555. return 1
  556. fi
  557. openssl req -noout -in "$_csrfile" -subject | _egrep_o "CN=.*" | cut -d = -f 2 | cut -d / -f 1 | tr -d '\n'
  558. }
  559. #_csrfile
  560. #echo comma separated domain list
  561. _readSubjectAltNamesFromCSR() {
  562. _csrfile="$1"
  563. if [ -z "$_csrfile" ]; then
  564. _usage "_readSubjectAltNamesFromCSR mycsr.csr"
  565. return 1
  566. fi
  567. _csrsubj="$(_readSubjectFromCSR "$_csrfile")"
  568. _debug _csrsubj "$_csrsubj"
  569. _dnsAltnames="$(openssl req -noout -text -in "$_csrfile" | grep "^ *DNS:.*" | tr -d ' \n')"
  570. _debug _dnsAltnames "$_dnsAltnames"
  571. if _contains "$_dnsAltnames," "DNS:$_csrsubj,"; then
  572. _debug "AltNames contains subject"
  573. _dnsAltnames="$(printf "%s" "$_dnsAltnames," | sed "s/DNS:$_csrsubj,//g")"
  574. else
  575. _debug "AltNames doesn't contain subject"
  576. fi
  577. printf "%s" "$_dnsAltnames" | sed "s/DNS://g"
  578. }
  579. #_csrfile
  580. _readKeyLengthFromCSR() {
  581. _csrfile="$1"
  582. if [ -z "$_csrfile" ]; then
  583. _usage "_readKeyLengthFromCSR mycsr.csr"
  584. return 1
  585. fi
  586. _outcsr="$(openssl req -noout -text -in "$_csrfile")"
  587. if _contains "$_outcsr" "Public Key Algorithm: id-ecPublicKey"; then
  588. _debug "ECC CSR"
  589. echo "$_outcsr" | _egrep_o "^ *ASN1 OID:.*" | cut -d ':' -f 2 | tr -d ' '
  590. else
  591. _debug "RSA CSR"
  592. echo "$_outcsr" | _egrep_o "^ *Public-Key:.*" | cut -d '(' -f 2 | cut -d ' ' -f 1
  593. fi
  594. }
  595. _ss() {
  596. _port="$1"
  597. if _exists "ss"; then
  598. _debug "Using: ss"
  599. ss -ntpl | grep ":$_port "
  600. return 0
  601. fi
  602. if _exists "netstat"; then
  603. _debug "Using: netstat"
  604. if netstat -h 2>&1 | grep "\-p proto" >/dev/null; then
  605. #for windows version netstat tool
  606. netstat -an -p tcp | grep "LISTENING" | grep ":$_port "
  607. else
  608. if netstat -help 2>&1 | grep "\-p protocol" >/dev/null; then
  609. netstat -an -p tcp | grep LISTEN | grep ":$_port "
  610. elif netstat -help 2>&1 | grep -- '-P protocol' >/dev/null; then
  611. #for solaris
  612. netstat -an -P tcp | grep "\.$_port " | grep "LISTEN"
  613. else
  614. netstat -ntpl | grep ":$_port "
  615. fi
  616. fi
  617. return 0
  618. fi
  619. return 1
  620. }
  621. #domain [password] [isEcc]
  622. toPkcs() {
  623. domain="$1"
  624. pfxPassword="$2"
  625. if [ -z "$domain" ]; then
  626. _usage "Usage: $PROJECT_ENTRY --toPkcs -d domain [--password pfx-password]"
  627. return 1
  628. fi
  629. _isEcc="$3"
  630. _initpath "$domain" "$_isEcc"
  631. if [ "$pfxPassword" ]; then
  632. openssl pkcs12 -export -out "$CERT_PFX_PATH" -inkey "$CERT_KEY_PATH" -in "$CERT_PATH" -certfile "$CA_CERT_PATH" -password "pass:$pfxPassword"
  633. else
  634. openssl pkcs12 -export -out "$CERT_PFX_PATH" -inkey "$CERT_KEY_PATH" -in "$CERT_PATH" -certfile "$CA_CERT_PATH"
  635. fi
  636. if [ "$?" = "0" ]; then
  637. _info "Success, Pfx is exported to: $CERT_PFX_PATH"
  638. fi
  639. }
  640. #[2048]
  641. createAccountKey() {
  642. _info "Creating account key"
  643. if [ -z "$1" ]; then
  644. _usage "Usage: $PROJECT_ENTRY --createAccountKey --accountkeylength 2048"
  645. return
  646. fi
  647. length=$1
  648. _create_account_key "$length"
  649. }
  650. _create_account_key() {
  651. length=$1
  652. if [ -z "$length" ] || [ "$length" = "$NO_VALUE" ]; then
  653. _debug "Use default length $DEFAULT_ACCOUNT_KEY_LENGTH"
  654. length="$DEFAULT_ACCOUNT_KEY_LENGTH"
  655. fi
  656. _debug length "$length"
  657. _initpath
  658. mkdir -p "$CA_DIR"
  659. if [ -f "$ACCOUNT_KEY_PATH" ]; then
  660. _info "Account key exists, skip"
  661. return
  662. else
  663. #generate account key
  664. _createkey "$length" "$ACCOUNT_KEY_PATH"
  665. fi
  666. }
  667. #domain [length]
  668. createDomainKey() {
  669. _info "Creating domain key"
  670. if [ -z "$1" ]; then
  671. _usage "Usage: $PROJECT_ENTRY --createDomainKey -d domain.com [ --keylength 2048 ]"
  672. return
  673. fi
  674. domain=$1
  675. length=$2
  676. if [ -z "$length" ]; then
  677. _debug "Use DEFAULT_DOMAIN_KEY_LENGTH=$DEFAULT_DOMAIN_KEY_LENGTH"
  678. length="$DEFAULT_DOMAIN_KEY_LENGTH"
  679. fi
  680. _initpath "$domain" "$length"
  681. if [ ! -f "$CERT_KEY_PATH" ] || ([ "$FORCE" ] && ! [ "$IS_RENEW" ]); then
  682. _createkey "$length" "$CERT_KEY_PATH"
  683. else
  684. if [ "$IS_RENEW" ]; then
  685. _info "Domain key exists, skip"
  686. return 0
  687. else
  688. _err "Domain key exists, do you want to overwrite the key?"
  689. _err "Add '--force', and try again."
  690. return 1
  691. fi
  692. fi
  693. }
  694. # domain domainlist isEcc
  695. createCSR() {
  696. _info "Creating csr"
  697. if [ -z "$1" ]; then
  698. _usage "Usage: $PROJECT_ENTRY --createCSR -d domain1.com [-d domain2.com -d domain3.com ... ]"
  699. return
  700. fi
  701. domain="$1"
  702. domainlist="$2"
  703. _isEcc="$3"
  704. _initpath "$domain" "$_isEcc"
  705. if [ -f "$CSR_PATH" ] && [ "$IS_RENEW" ] && [ -z "$FORCE" ]; then
  706. _info "CSR exists, skip"
  707. return
  708. fi
  709. if [ ! -f "$CERT_KEY_PATH" ]; then
  710. _err "The key file is not found: $CERT_KEY_PATH"
  711. _err "Please create the key file first."
  712. return 1
  713. fi
  714. _createcsr "$domain" "$domainlist" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"
  715. }
  716. _urlencode() {
  717. tr '/+' '_-' | tr -d '= '
  718. }
  719. _time2str() {
  720. #BSD
  721. if date -u -d@"$1" 2>/dev/null; then
  722. return
  723. fi
  724. #Linux
  725. if date -u -r "$1" 2>/dev/null; then
  726. return
  727. fi
  728. #Soaris
  729. if _exists adb; then
  730. echo $(echo "0t${1}=Y" | adb)
  731. fi
  732. }
  733. _normalizeJson() {
  734. sed "s/\" *: *\([\"{\[]\)/\":\1/g" | sed "s/^ *\([^ ]\)/\1/" | tr -d "\r\n"
  735. }
  736. _stat() {
  737. #Linux
  738. if stat -c '%U:%G' "$1" 2>/dev/null; then
  739. return
  740. fi
  741. #BSD
  742. if stat -f '%Su:%Sg' "$1" 2>/dev/null; then
  743. return
  744. fi
  745. return 1 #error, 'stat' not found
  746. }
  747. #keyfile
  748. _calcjwk() {
  749. keyfile="$1"
  750. if [ -z "$keyfile" ]; then
  751. _usage "Usage: _calcjwk keyfile"
  752. return 1
  753. fi
  754. if [ "$JWK_HEADER" ] && [ "$__CACHED_JWK_KEY_FILE" = "$keyfile" ]; then
  755. _debug2 "Use cached jwk for file: $__CACHED_JWK_KEY_FILE"
  756. return 0
  757. fi
  758. if grep "BEGIN RSA PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then
  759. _debug "RSA key"
  760. pub_exp=$(openssl rsa -in "$keyfile" -noout -text | grep "^publicExponent:" | cut -d '(' -f 2 | cut -d 'x' -f 2 | cut -d ')' -f 1)
  761. if [ "${#pub_exp}" = "5" ]; then
  762. pub_exp=0$pub_exp
  763. fi
  764. _debug3 pub_exp "$pub_exp"
  765. e=$(echo "$pub_exp" | _h2b | _base64)
  766. _debug3 e "$e"
  767. modulus=$(openssl rsa -in "$keyfile" -modulus -noout | cut -d '=' -f 2)
  768. _debug3 modulus "$modulus"
  769. n="$(printf "%s" "$modulus" | _h2b | _base64 | _urlencode)"
  770. jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}'
  771. _debug3 jwk "$jwk"
  772. JWK_HEADER='{"alg": "RS256", "jwk": '$jwk'}'
  773. JWK_HEADERPLACE_PART1='{"nonce": "'
  774. JWK_HEADERPLACE_PART2='", "alg": "RS256", "jwk": '$jwk'}'
  775. elif grep "BEGIN EC PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then
  776. _debug "EC key"
  777. crv="$(openssl ec -in "$keyfile" -noout -text 2>/dev/null | grep "^NIST CURVE:" | cut -d ":" -f 2 | tr -d " \r\n")"
  778. _debug3 crv "$crv"
  779. if [ -z "$crv" ]; then
  780. _debug "Let's try ASN1 OID"
  781. crv_oid="$(openssl ec -in "$keyfile" -noout -text 2>/dev/null | grep "^ASN1 OID:" | cut -d ":" -f 2 | tr -d " \r\n")"
  782. _debug3 crv_oid "$crv_oid"
  783. case "${crv_oid}" in
  784. "prime256v1")
  785. crv="P-256"
  786. ;;
  787. "secp384r1")
  788. crv="P-384"
  789. ;;
  790. "secp521r1")
  791. crv="P-521"
  792. ;;
  793. *)
  794. _err "ECC oid : $crv_oid"
  795. return 1
  796. ;;
  797. esac
  798. _debug3 crv "$crv"
  799. fi
  800. pubi="$(openssl ec -in "$keyfile" -noout -text 2>/dev/null | grep -n pub: | cut -d : -f 1)"
  801. pubi=$(_math "$pubi" + 1)
  802. _debug3 pubi "$pubi"
  803. pubj="$(openssl ec -in "$keyfile" -noout -text 2>/dev/null | grep -n "ASN1 OID:" | cut -d : -f 1)"
  804. pubj=$(_math "$pubj" - 1)
  805. _debug3 pubj "$pubj"
  806. pubtext="$(openssl ec -in "$keyfile" -noout -text 2>/dev/null | sed -n "$pubi,${pubj}p" | tr -d " \n\r")"
  807. _debug3 pubtext "$pubtext"
  808. xlen="$(printf "%s" "$pubtext" | tr -d ':' | wc -c)"
  809. xlen=$(_math "$xlen" / 4)
  810. _debug3 xlen "$xlen"
  811. xend=$(_math "$xlen" + 1)
  812. x="$(printf "%s" "$pubtext" | cut -d : -f 2-"$xend")"
  813. _debug3 x "$x"
  814. x64="$(printf "%s" "$x" | tr -d : | _h2b | _base64 | _urlencode)"
  815. _debug3 x64 "$x64"
  816. xend=$(_math "$xend" + 1)
  817. y="$(printf "%s" "$pubtext" | cut -d : -f "$xend"-10000)"
  818. _debug3 y "$y"
  819. y64="$(printf "%s" "$y" | tr -d : | _h2b | _base64 | _urlencode)"
  820. _debug3 y64 "$y64"
  821. jwk='{"crv": "'$crv'", "kty": "EC", "x": "'$x64'", "y": "'$y64'"}'
  822. _debug3 jwk "$jwk"
  823. JWK_HEADER='{"alg": "ES256", "jwk": '$jwk'}'
  824. JWK_HEADERPLACE_PART1='{"nonce": "'
  825. JWK_HEADERPLACE_PART2='", "alg": "ES256", "jwk": '$jwk'}'
  826. else
  827. _err "Only RSA or EC key is supported."
  828. return 1
  829. fi
  830. _debug3 JWK_HEADER "$JWK_HEADER"
  831. __CACHED_JWK_KEY_FILE="$keyfile"
  832. }
  833. _time() {
  834. date -u "+%s"
  835. }
  836. _mktemp() {
  837. if _exists mktemp; then
  838. if mktemp 2>/dev/null; then
  839. return 0
  840. elif _contains "$(mktemp 2>&1)" "-t prefix" && mktemp -t "$PROJECT_NAME" 2>/dev/null; then
  841. #for Mac osx
  842. return 0
  843. fi
  844. fi
  845. if [ -d "/tmp" ]; then
  846. echo "/tmp/${PROJECT_NAME}wefADf24sf.$(_time).tmp"
  847. return 0
  848. elif [ "$LE_TEMP_DIR" ] && mkdir -p "$LE_TEMP_DIR"; then
  849. echo "/$LE_TEMP_DIR/wefADf24sf.$(_time).tmp"
  850. return 0
  851. fi
  852. _err "Can not create temp file."
  853. }
  854. _inithttp() {
  855. if [ -z "$HTTP_HEADER" ] || ! touch "$HTTP_HEADER"; then
  856. HTTP_HEADER="$(_mktemp)"
  857. _debug2 HTTP_HEADER "$HTTP_HEADER"
  858. fi
  859. if [ "$__HTTP_INITIALIZED" ]; then
  860. if [ "$_ACME_CURL$_ACME_WGET" ]; then
  861. _debug2 "Http already initialized."
  862. return 0
  863. fi
  864. fi
  865. if [ -z "$_ACME_CURL" ] && _exists "curl"; then
  866. _ACME_CURL="curl -L --silent --dump-header $HTTP_HEADER "
  867. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
  868. _CURL_DUMP="$(_mktemp)"
  869. _ACME_CURL="$_ACME_CURL --trace-ascii $_CURL_DUMP "
  870. fi
  871. if [ "$CA_BUNDLE" ]; then
  872. _ACME_CURL="$_ACME_CURL --cacert $CA_BUNDLE "
  873. fi
  874. if [ "$HTTPS_INSECURE" ]; then
  875. _ACME_CURL="$_ACME_CURL --insecure "
  876. fi
  877. fi
  878. if [ -z "$_ACME_WGET" ] && _exists "wget"; then
  879. _ACME_WGET="wget -q"
  880. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
  881. _ACME_WGET="$_ACME_WGET -d "
  882. fi
  883. if [ "$CA_BUNDLE" ]; then
  884. _ACME_WGET="$_ACME_WGET --ca-certificate $CA_BUNDLE "
  885. fi
  886. if [ "$HTTPS_INSECURE" ]; then
  887. _ACME_WGET="$_ACME_WGET --no-check-certificate "
  888. fi
  889. fi
  890. __HTTP_INITIALIZED=1
  891. }
  892. # body url [needbase64] [POST|PUT]
  893. _post() {
  894. body="$1"
  895. url="$2"
  896. needbase64="$3"
  897. httpmethod="$4"
  898. if [ -z "$httpmethod" ]; then
  899. httpmethod="POST"
  900. fi
  901. _debug $httpmethod
  902. _debug "url" "$url"
  903. _debug2 "body" "$body"
  904. _inithttp
  905. if [ "$_ACME_CURL" ]; then
  906. _CURL="$_ACME_CURL"
  907. _debug "_CURL" "$_CURL"
  908. if [ "$needbase64" ]; then
  909. response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$url" | _base64)"
  910. else
  911. response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$url")"
  912. fi
  913. _ret="$?"
  914. if [ "$_ret" != "0" ]; then
  915. _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $_ret"
  916. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
  917. _err "Here is the curl dump log:"
  918. _err "$(cat "$_CURL_DUMP")"
  919. fi
  920. fi
  921. elif [ "$_ACME_WGET" ]; then
  922. _debug "_ACME_WGET" "$_ACME_WGET"
  923. if [ "$needbase64" ]; then
  924. if [ "$httpmethod" = "POST" ]; then
  925. response="$($_ACME_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$url" 2>"$HTTP_HEADER" | _base64)"
  926. else
  927. response="$($_ACME_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$url" 2>"$HTTP_HEADER" | _base64)"
  928. fi
  929. else
  930. if [ "$httpmethod" = "POST" ]; then
  931. response="$($_ACME_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$url" 2>"$HTTP_HEADER")"
  932. else
  933. response="$($_ACME_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$url" 2>"$HTTP_HEADER")"
  934. fi
  935. fi
  936. _ret="$?"
  937. if [ "$_ret" = "8" ]; then
  938. _ret=0
  939. _debug "wget returns 8, the server returns a 'Bad request' respons, lets process the response later."
  940. fi
  941. if [ "$_ret" != "0" ]; then
  942. _err "Please refer to https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html for error code: $_ret"
  943. fi
  944. _sed_i "s/^ *//g" "$HTTP_HEADER"
  945. else
  946. _ret="$?"
  947. _err "Neither curl nor wget is found, can not do $httpmethod."
  948. fi
  949. _debug "_ret" "$_ret"
  950. printf "%s" "$response"
  951. return $_ret
  952. }
  953. # url getheader timeout
  954. _get() {
  955. _debug GET
  956. url="$1"
  957. onlyheader="$2"
  958. t="$3"
  959. _debug url "$url"
  960. _debug "timeout" "$t"
  961. _inithttp
  962. if [ "$_ACME_CURL" ]; then
  963. _CURL="$_ACME_CURL"
  964. if [ "$t" ]; then
  965. _CURL="$_CURL --connect-timeout $t"
  966. fi
  967. _debug "_CURL" "$_CURL"
  968. if [ "$onlyheader" ]; then
  969. $_CURL -I --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url"
  970. else
  971. $_CURL --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url"
  972. fi
  973. ret=$?
  974. if [ "$ret" != "0" ]; then
  975. _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $ret"
  976. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
  977. _err "Here is the curl dump log:"
  978. _err "$(cat "$_CURL_DUMP")"
  979. fi
  980. fi
  981. elif [ "$_ACME_WGET" ]; then
  982. _WGET="$_ACME_WGET"
  983. if [ "$t" ]; then
  984. _WGET="$_WGET --timeout=$t"
  985. fi
  986. _debug "_WGET" "$_WGET"
  987. if [ "$onlyheader" ]; then
  988. $_WGET --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -S -O /dev/null "$url" 2>&1 | sed 's/^[ ]*//g'
  989. else
  990. $_WGET --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -O - "$url"
  991. fi
  992. ret=$?
  993. if [ "$_ret" = "8" ]; then
  994. _ret=0
  995. _debug "wget returns 8, the server returns a 'Bad request' respons, lets process the response later."
  996. fi
  997. if [ "$ret" != "0" ]; then
  998. _err "Please refer to https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html for error code: $ret"
  999. fi
  1000. else
  1001. ret=$?
  1002. _err "Neither curl nor wget is found, can not do GET."
  1003. fi
  1004. _debug "ret" "$ret"
  1005. return $ret
  1006. }
  1007. _head_n() {
  1008. head -n "$1"
  1009. }
  1010. _tail_n() {
  1011. if ! tail -n "$1" 2>/dev/null; then
  1012. #fix for solaris
  1013. tail -"$1"
  1014. fi
  1015. }
  1016. # url payload needbase64 keyfile
  1017. _send_signed_request() {
  1018. url=$1
  1019. payload=$2
  1020. needbase64=$3
  1021. keyfile=$4
  1022. if [ -z "$keyfile" ]; then
  1023. keyfile="$ACCOUNT_KEY_PATH"
  1024. fi
  1025. _debug url "$url"
  1026. _debug payload "$payload"
  1027. if ! _calcjwk "$keyfile"; then
  1028. return 1
  1029. fi
  1030. payload64=$(printf "%s" "$payload" | _base64 | _urlencode)
  1031. _debug3 payload64 "$payload64"
  1032. if [ -z "$_CACHED_NONCE" ]; then
  1033. _debug2 "Get nonce."
  1034. nonceurl="$API/directory"
  1035. _headers="$(_get "$nonceurl" "onlyheader")"
  1036. if [ "$?" != "0" ]; then
  1037. _err "Can not connect to $nonceurl to get nonce."
  1038. return 1
  1039. fi
  1040. _debug2 _headers "$_headers"
  1041. _CACHED_NONCE="$(echo "$_headers" | grep "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2)"
  1042. _debug2 _CACHED_NONCE "$_CACHED_NONCE"
  1043. else
  1044. _debug2 "Use _CACHED_NONCE" "$_CACHED_NONCE"
  1045. fi
  1046. nonce="$_CACHED_NONCE"
  1047. _debug2 nonce "$nonce"
  1048. protected="$JWK_HEADERPLACE_PART1$nonce$JWK_HEADERPLACE_PART2"
  1049. _debug3 protected "$protected"
  1050. protected64="$(printf "%s" "$protected" | _base64 | _urlencode)"
  1051. _debug3 protected64 "$protected64"
  1052. if ! _sig_t="$(printf "%s" "$protected64.$payload64" | _sign "$keyfile" "sha256")"; then
  1053. _err "Sign request failed."
  1054. return 1
  1055. fi
  1056. _debug3 _sig_t "$_sig_t"
  1057. sig="$(printf "%s" "$_sig_t" | _urlencode)"
  1058. _debug3 sig "$sig"
  1059. body="{\"header\": $JWK_HEADER, \"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}"
  1060. _debug3 body "$body"
  1061. response="$(_post "$body" "$url" "$needbase64")"
  1062. _CACHED_NONCE=""
  1063. if [ "$?" != "0" ]; then
  1064. _err "Can not post to $url"
  1065. return 1
  1066. fi
  1067. _debug2 original "$response"
  1068. response="$(echo "$response" | _normalizeJson)"
  1069. responseHeaders="$(cat "$HTTP_HEADER")"
  1070. _debug2 responseHeaders "$responseHeaders"
  1071. _debug2 response "$response"
  1072. code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\r\n")"
  1073. _debug code "$code"
  1074. _CACHED_NONCE="$(echo "$responseHeaders" | grep "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2)"
  1075. }
  1076. #setopt "file" "opt" "=" "value" [";"]
  1077. _setopt() {
  1078. __conf="$1"
  1079. __opt="$2"
  1080. __sep="$3"
  1081. __val="$4"
  1082. __end="$5"
  1083. if [ -z "$__opt" ]; then
  1084. _usage usage: _setopt '"file" "opt" "=" "value" [";"]'
  1085. return
  1086. fi
  1087. if [ ! -f "$__conf" ]; then
  1088. touch "$__conf"
  1089. fi
  1090. if grep -n "^$__opt$__sep" "$__conf" >/dev/null; then
  1091. _debug3 OK
  1092. if _contains "$__val" "&"; then
  1093. __val="$(echo "$__val" | sed 's/&/\\&/g')"
  1094. fi
  1095. text="$(cat "$__conf")"
  1096. echo "$text" | sed "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf"
  1097. elif grep -n "^#$__opt$__sep" "$__conf" >/dev/null; then
  1098. if _contains "$__val" "&"; then
  1099. __val="$(echo "$__val" | sed 's/&/\\&/g')"
  1100. fi
  1101. text="$(cat "$__conf")"
  1102. echo "$text" | sed "s|^#$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf"
  1103. else
  1104. _debug3 APP
  1105. echo "$__opt$__sep$__val$__end" >>"$__conf"
  1106. fi
  1107. _debug2 "$(grep -n "^$__opt$__sep" "$__conf")"
  1108. }
  1109. #_save_conf file key value
  1110. #save to conf
  1111. _save_conf() {
  1112. _s_c_f="$1"
  1113. _sdkey="$2"
  1114. _sdvalue="$3"
  1115. if [ "$_s_c_f" ]; then
  1116. _setopt "$_s_c_f" "$_sdkey" "=" "'$_sdvalue'"
  1117. else
  1118. _err "config file is empty, can not save $_sdkey=$_sdvalue"
  1119. fi
  1120. }
  1121. #_clear_conf file key
  1122. _clear_conf() {
  1123. _c_c_f="$1"
  1124. _sdkey="$2"
  1125. if [ "$_c_c_f" ]; then
  1126. _sed_i "s/^$_sdkey.*$//" "$_c_c_f"
  1127. else
  1128. _err "config file is empty, can not clear"
  1129. fi
  1130. }
  1131. #_read_conf file key
  1132. _read_conf() {
  1133. _r_c_f="$1"
  1134. _sdkey="$2"
  1135. if [ -f "$_r_c_f" ]; then
  1136. (
  1137. eval "$(grep "^$_sdkey *=" "$_r_c_f")"
  1138. eval "printf \"%s\" \"\$$_sdkey\""
  1139. )
  1140. else
  1141. _debug "config file is empty, can not read $_sdkey"
  1142. fi
  1143. }
  1144. #_savedomainconf key value
  1145. #save to domain.conf
  1146. _savedomainconf() {
  1147. _save_conf "$DOMAIN_CONF" "$1" "$2"
  1148. }
  1149. #_cleardomainconf key
  1150. _cleardomainconf() {
  1151. _clear_conf "$DOMAIN_CONF" "$1"
  1152. }
  1153. #_readdomainconf key
  1154. _readdomainconf() {
  1155. _read_conf "$DOMAIN_CONF" "$1"
  1156. }
  1157. #_saveaccountconf key value
  1158. _saveaccountconf() {
  1159. _save_conf "$ACCOUNT_CONF_PATH" "$1" "$2"
  1160. }
  1161. #_clearaccountconf key
  1162. _clearaccountconf() {
  1163. _clear_conf "$ACCOUNT_CONF_PATH" "$1"
  1164. }
  1165. #_savecaconf key value
  1166. _savecaconf() {
  1167. _save_conf "$CA_CONF" "$1" "$2"
  1168. }
  1169. #_readcaconf key
  1170. _readcaconf() {
  1171. _read_conf "$CA_CONF" "$1"
  1172. }
  1173. #_clearaccountconf key
  1174. _clearcaconf() {
  1175. _clear_conf "$CA_CONF" "$1"
  1176. }
  1177. # content localaddress
  1178. _startserver() {
  1179. content="$1"
  1180. ncaddr="$2"
  1181. _debug "ncaddr" "$ncaddr"
  1182. _debug "startserver: $$"
  1183. nchelp="$(nc -h 2>&1)"
  1184. _debug Le_HTTPPort "$Le_HTTPPort"
  1185. _debug Le_Listen_V4 "$Le_Listen_V4"
  1186. _debug Le_Listen_V6 "$Le_Listen_V6"
  1187. _NC="nc"
  1188. if [ "$Le_Listen_V4" ]; then
  1189. _NC="$_NC -4"
  1190. elif [ "$Le_Listen_V6" ]; then
  1191. _NC="$_NC -6"
  1192. fi
  1193. if echo "$nchelp" | grep "\-q[ ,]" >/dev/null; then
  1194. _NC="$_NC -q 1 -l $ncaddr"
  1195. else
  1196. if echo "$nchelp" | grep "GNU netcat" >/dev/null && echo "$nchelp" | grep "\-c, \-\-close" >/dev/null; then
  1197. _NC="$_NC -c -l $ncaddr"
  1198. elif echo "$nchelp" | grep "\-N" | grep "Shutdown the network socket after EOF on stdin" >/dev/null; then
  1199. _NC="$_NC -N -l $ncaddr"
  1200. else
  1201. _NC="$_NC -l $ncaddr"
  1202. fi
  1203. fi
  1204. _debug "_NC" "$_NC"
  1205. #for centos ncat
  1206. if _contains "$nchelp" "nmap.org"; then
  1207. _debug "Using ncat: nmap.org"
  1208. if [ "$DEBUG" ]; then
  1209. if printf "%s\r\n\r\n%s" "HTTP/1.1 200 OK" "$content" | $_NC "$Le_HTTPPort"; then
  1210. return
  1211. fi
  1212. else
  1213. if printf "%s\r\n\r\n%s" "HTTP/1.1 200 OK" "$content" | $_NC "$Le_HTTPPort" >/dev/null 2>&1; then
  1214. return
  1215. fi
  1216. fi
  1217. _err "ncat listen error."
  1218. fi
  1219. # while true ; do
  1220. if [ "$DEBUG" ]; then
  1221. if ! printf "%s\r\n\r\n%s" "HTTP/1.1 200 OK" "$content" | $_NC -p "$Le_HTTPPort"; then
  1222. printf "%s\r\n\r\n%s" "HTTP/1.1 200 OK" "$content" | $_NC "$Le_HTTPPort"
  1223. fi
  1224. else
  1225. if ! printf "%s\r\n\r\n%s" "HTTP/1.1 200 OK" "$content" | $_NC -p "$Le_HTTPPort" >/dev/null 2>&1; then
  1226. printf "%s\r\n\r\n%s" "HTTP/1.1 200 OK" "$content" | $_NC "$Le_HTTPPort" >/dev/null 2>&1
  1227. fi
  1228. fi
  1229. if [ "$?" != "0" ]; then
  1230. _err "nc listen error."
  1231. exit 1
  1232. fi
  1233. # done
  1234. }
  1235. _stopserver() {
  1236. pid="$1"
  1237. _debug "pid" "$pid"
  1238. if [ -z "$pid" ]; then
  1239. return
  1240. fi
  1241. _debug2 "Le_HTTPPort" "$Le_HTTPPort"
  1242. if [ "$Le_HTTPPort" ]; then
  1243. if [ "$DEBUG" ] && [ "$DEBUG" -gt "3" ]; then
  1244. _get "http://localhost:$Le_HTTPPort" "" 1
  1245. else
  1246. _get "http://localhost:$Le_HTTPPort" "" 1 >/dev/null 2>&1
  1247. fi
  1248. fi
  1249. _debug2 "Le_TLSPort" "$Le_TLSPort"
  1250. if [ "$Le_TLSPort" ]; then
  1251. if [ "$DEBUG" ] && [ "$DEBUG" -gt "3" ]; then
  1252. _get "https://localhost:$Le_TLSPort" "" 1
  1253. _get "https://localhost:$Le_TLSPort" "" 1
  1254. else
  1255. _get "https://localhost:$Le_TLSPort" "" 1 >/dev/null 2>&1
  1256. _get "https://localhost:$Le_TLSPort" "" 1 >/dev/null 2>&1
  1257. fi
  1258. fi
  1259. }
  1260. # sleep sec
  1261. _sleep() {
  1262. _sleep_sec="$1"
  1263. if [ "$__INTERACTIVE" ]; then
  1264. _sleep_c="$_sleep_sec"
  1265. while [ "$_sleep_c" -ge "0" ]; do
  1266. printf "\r \r"
  1267. __green "$_sleep_c"
  1268. _sleep_c="$(_math "$_sleep_c" - 1)"
  1269. sleep 1
  1270. done
  1271. printf "\r"
  1272. else
  1273. sleep "$_sleep_sec"
  1274. fi
  1275. }
  1276. # _starttlsserver san_a san_b port content _ncaddr
  1277. _starttlsserver() {
  1278. _info "Starting tls server."
  1279. san_a="$1"
  1280. san_b="$2"
  1281. port="$3"
  1282. content="$4"
  1283. opaddr="$5"
  1284. _debug san_a "$san_a"
  1285. _debug san_b "$san_b"
  1286. _debug port "$port"
  1287. #create key TLS_KEY
  1288. if ! _createkey "2048" "$TLS_KEY"; then
  1289. _err "Create tls validation key error."
  1290. return 1
  1291. fi
  1292. #create csr
  1293. alt="$san_a"
  1294. if [ "$san_b" ]; then
  1295. alt="$alt,$san_b"
  1296. fi
  1297. if ! _createcsr "tls.acme.sh" "$alt" "$TLS_KEY" "$TLS_CSR" "$TLS_CONF"; then
  1298. _err "Create tls validation csr error."
  1299. return 1
  1300. fi
  1301. #self signed
  1302. if ! _signcsr "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" "$TLS_CERT"; then
  1303. _err "Create tls validation cert error."
  1304. return 1
  1305. fi
  1306. __S_OPENSSL="openssl s_server -cert $TLS_CERT -key $TLS_KEY "
  1307. if [ "$opaddr" ]; then
  1308. __S_OPENSSL="$__S_OPENSSL -accept $opaddr:$port"
  1309. else
  1310. __S_OPENSSL="$__S_OPENSSL -accept $port"
  1311. fi
  1312. _debug Le_Listen_V4 "$Le_Listen_V4"
  1313. _debug Le_Listen_V6 "$Le_Listen_V6"
  1314. if [ "$Le_Listen_V4" ]; then
  1315. __S_OPENSSL="$__S_OPENSSL -4"
  1316. elif [ "$Le_Listen_V6" ]; then
  1317. __S_OPENSSL="$__S_OPENSSL -6"
  1318. fi
  1319. #start openssl
  1320. _debug "$__S_OPENSSL"
  1321. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
  1322. (printf "%s\r\n\r\n%s" "HTTP/1.1 200 OK" "$content" | $__S_OPENSSL -tlsextdebug) &
  1323. else
  1324. (printf "%s\r\n\r\n%s" "HTTP/1.1 200 OK" "$content" | $__S_OPENSSL >/dev/null 2>&1) &
  1325. fi
  1326. serverproc="$!"
  1327. sleep 1
  1328. _debug serverproc "$serverproc"
  1329. }
  1330. #file
  1331. _readlink() {
  1332. _rf="$1"
  1333. if ! readlink -f "$_rf" 2>/dev/null; then
  1334. if _startswith "$_rf" "\./$PROJECT_ENTRY"; then
  1335. printf -- "%s" "$(pwd)/$PROJECT_ENTRY"
  1336. return 0
  1337. fi
  1338. readlink "$_rf"
  1339. fi
  1340. }
  1341. __initHome() {
  1342. if [ -z "$_SCRIPT_HOME" ]; then
  1343. if _exists readlink && _exists dirname; then
  1344. _debug "Lets find script dir."
  1345. _debug "_SCRIPT_" "$_SCRIPT_"
  1346. _script="$(_readlink "$_SCRIPT_")"
  1347. _debug "_script" "$_script"
  1348. _script_home="$(dirname "$_script")"
  1349. _debug "_script_home" "$_script_home"
  1350. if [ -d "$_script_home" ]; then
  1351. _SCRIPT_HOME="$_script_home"
  1352. else
  1353. _err "It seems the script home is not correct:$_script_home"
  1354. fi
  1355. fi
  1356. fi
  1357. if [ -z "$LE_WORKING_DIR" ]; then
  1358. if [ -f "$DEFAULT_INSTALL_HOME/account.conf" ]; then
  1359. _debug "It seems that $PROJECT_NAME is already installed in $DEFAULT_INSTALL_HOME"
  1360. LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
  1361. else
  1362. LE_WORKING_DIR="$_SCRIPT_HOME"
  1363. fi
  1364. fi
  1365. if [ -z "$LE_WORKING_DIR" ]; then
  1366. _debug "Using default home:$DEFAULT_INSTALL_HOME"
  1367. LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
  1368. fi
  1369. export LE_WORKING_DIR
  1370. _DEFAULT_ACCOUNT_CONF_PATH="$LE_WORKING_DIR/account.conf"
  1371. if [ -z "$ACCOUNT_CONF_PATH" ]; then
  1372. if [ -f "$_DEFAULT_ACCOUNT_CONF_PATH" ]; then
  1373. . "$_DEFAULT_ACCOUNT_CONF_PATH"
  1374. fi
  1375. fi
  1376. if [ -z "$ACCOUNT_CONF_PATH" ]; then
  1377. ACCOUNT_CONF_PATH="$_DEFAULT_ACCOUNT_CONF_PATH"
  1378. fi
  1379. DEFAULT_LOG_FILE="$LE_WORKING_DIR/$PROJECT_NAME.log"
  1380. DEFAULT_CA_HOME="$LE_WORKING_DIR/ca"
  1381. if [ -z "$LE_TEMP_DIR" ]; then
  1382. LE_TEMP_DIR="$LE_WORKING_DIR/tmp"
  1383. fi
  1384. }
  1385. #[domain] [keylength]
  1386. _initpath() {
  1387. __initHome
  1388. if [ -f "$ACCOUNT_CONF_PATH" ]; then
  1389. . "$ACCOUNT_CONF_PATH"
  1390. fi
  1391. if [ "$IN_CRON" ]; then
  1392. if [ ! "$_USER_PATH_EXPORTED" ]; then
  1393. _USER_PATH_EXPORTED=1
  1394. export PATH="$USER_PATH:$PATH"
  1395. fi
  1396. fi
  1397. if [ -z "$CA_HOME" ]; then
  1398. CA_HOME="$DEFAULT_CA_HOME"
  1399. fi
  1400. if [ -z "$API" ]; then
  1401. if [ -z "$STAGE" ]; then
  1402. API="$DEFAULT_CA"
  1403. else
  1404. API="$STAGE_CA"
  1405. _info "Using stage api:$API"
  1406. fi
  1407. fi
  1408. _API_HOST="$(echo "$API" | cut -d : -f 2 | tr -d '/')"
  1409. CA_DIR="$CA_HOME/$_API_HOST"
  1410. _DEFAULT_CA_CONF="$CA_DIR/ca.conf"
  1411. if [ -z "$CA_CONF" ]; then
  1412. CA_CONF="$_DEFAULT_CA_CONF"
  1413. fi
  1414. if [ -f "$CA_CONF" ]; then
  1415. . "$CA_CONF"
  1416. fi
  1417. if [ -z "$ACME_DIR" ]; then
  1418. ACME_DIR="/home/.acme"
  1419. fi
  1420. if [ -z "$APACHE_CONF_BACKUP_DIR" ]; then
  1421. APACHE_CONF_BACKUP_DIR="$LE_WORKING_DIR"
  1422. fi
  1423. if [ -z "$USER_AGENT" ]; then
  1424. USER_AGENT="$DEFAULT_USER_AGENT"
  1425. fi
  1426. if [ -z "$HTTP_HEADER" ]; then
  1427. HTTP_HEADER="$LE_WORKING_DIR/http.header"
  1428. fi
  1429. _OLD_ACCOUNT_KEY="$LE_WORKING_DIR/account.key"
  1430. _OLD_ACCOUNT_JSON="$LE_WORKING_DIR/account.json"
  1431. _DEFAULT_ACCOUNT_KEY_PATH="$CA_DIR/account.key"
  1432. _DEFAULT_ACCOUNT_JSON_PATH="$CA_DIR/account.json"
  1433. if [ -z "$ACCOUNT_KEY_PATH" ]; then
  1434. ACCOUNT_KEY_PATH="$_DEFAULT_ACCOUNT_KEY_PATH"
  1435. fi
  1436. if [ -z "$ACCOUNT_JSON_PATH" ]; then
  1437. ACCOUNT_JSON_PATH="$_DEFAULT_ACCOUNT_JSON_PATH"
  1438. fi
  1439. _DEFAULT_CERT_HOME="$LE_WORKING_DIR"
  1440. if [ -z "$CERT_HOME" ]; then
  1441. CERT_HOME="$_DEFAULT_CERT_HOME"
  1442. fi
  1443. if [ -z "$1" ]; then
  1444. return 0
  1445. fi
  1446. mkdir -p "$CA_DIR"
  1447. domain="$1"
  1448. _ilength="$2"
  1449. if [ -z "$DOMAIN_PATH" ]; then
  1450. domainhome="$CERT_HOME/$domain"
  1451. domainhomeecc="$CERT_HOME/$domain$ECC_SUFFIX"
  1452. DOMAIN_PATH="$domainhome"
  1453. if _isEccKey "$_ilength"; then
  1454. DOMAIN_PATH="$domainhomeecc"
  1455. else
  1456. if [ ! -d "$domainhome" ] && [ -d "$domainhomeecc" ]; then
  1457. _info "The domain '$domain' seems to have a ECC cert already, please add '$(__red "--ecc")' parameter if you want to use that cert."
  1458. fi
  1459. fi
  1460. _debug DOMAIN_PATH "$DOMAIN_PATH"
  1461. fi
  1462. if [ ! -d "$DOMAIN_PATH" ]; then
  1463. if ! mkdir -p "$DOMAIN_PATH"; then
  1464. _err "Can not create domain path: $DOMAIN_PATH"
  1465. return 1
  1466. fi
  1467. fi
  1468. if [ -z "$DOMAIN_CONF" ]; then
  1469. DOMAIN_CONF="$DOMAIN_PATH/$domain.conf"
  1470. fi
  1471. if [ -z "$DOMAIN_SSL_CONF" ]; then
  1472. DOMAIN_SSL_CONF="$DOMAIN_PATH/$domain.csr.conf"
  1473. fi
  1474. if [ -z "$CSR_PATH" ]; then
  1475. CSR_PATH="$DOMAIN_PATH/$domain.csr"
  1476. fi
  1477. if [ -z "$CERT_KEY_PATH" ]; then
  1478. CERT_KEY_PATH="$DOMAIN_PATH/$domain.key"
  1479. fi
  1480. if [ -z "$CERT_PATH" ]; then
  1481. CERT_PATH="$DOMAIN_PATH/$domain.cer"
  1482. fi
  1483. if [ -z "$CA_CERT_PATH" ]; then
  1484. CA_CERT_PATH="$DOMAIN_PATH/ca.cer"
  1485. fi
  1486. if [ -z "$CERT_FULLCHAIN_PATH" ]; then
  1487. CERT_FULLCHAIN_PATH="$DOMAIN_PATH/fullchain.cer"
  1488. fi
  1489. if [ -z "$CERT_PFX_PATH" ]; then
  1490. CERT_PFX_PATH="$DOMAIN_PATH/$domain.pfx"
  1491. fi
  1492. if [ -z "$TLS_CONF" ]; then
  1493. TLS_CONF="$DOMAIN_PATH/tls.valdation.conf"
  1494. fi
  1495. if [ -z "$TLS_CERT" ]; then
  1496. TLS_CERT="$DOMAIN_PATH/tls.valdation.cert"
  1497. fi
  1498. if [ -z "$TLS_KEY" ]; then
  1499. TLS_KEY="$DOMAIN_PATH/tls.valdation.key"
  1500. fi
  1501. if [ -z "$TLS_CSR" ]; then
  1502. TLS_CSR="$DOMAIN_PATH/tls.valdation.csr"
  1503. fi
  1504. }
  1505. _exec() {
  1506. if [ -z "$_EXEC_TEMP_ERR" ]; then
  1507. _EXEC_TEMP_ERR="$(_mktemp)"
  1508. fi
  1509. if [ "$_EXEC_TEMP_ERR" ]; then
  1510. "$@" 2>"$_EXEC_TEMP_ERR"
  1511. else
  1512. "$@"
  1513. fi
  1514. }
  1515. _exec_err() {
  1516. [ "$_EXEC_TEMP_ERR" ] && _err "$(cat "$_EXEC_TEMP_ERR")"
  1517. }
  1518. _apachePath() {
  1519. _APACHECTL="apachectl"
  1520. if ! _exists apachectl; then
  1521. if _exists apache2ctl; then
  1522. _APACHECTL="apache2ctl"
  1523. else
  1524. _err "'apachectl not found. It seems that apache is not installed, or you are not root user.'"
  1525. _err "Please use webroot mode to try again."
  1526. return 1
  1527. fi
  1528. fi
  1529. if ! _exec $_APACHECTL -V >/dev/null; then
  1530. _exec_err
  1531. return 1
  1532. fi
  1533. if [ "$APACHE_HTTPD_CONF" ]; then
  1534. _saveaccountconf APACHE_HTTPD_CONF "$APACHE_HTTPD_CONF"
  1535. httpdconf="$APACHE_HTTPD_CONF"
  1536. httpdconfname="$(basename "$httpdconfname")"
  1537. else
  1538. httpdconfname="$($_APACHECTL -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"')"
  1539. _debug httpdconfname "$httpdconfname"
  1540. if [ -z "$httpdconfname" ]; then
  1541. _err "Can not read apache config file."
  1542. return 1
  1543. fi
  1544. if _startswith "$httpdconfname" '/'; then
  1545. httpdconf="$httpdconfname"
  1546. httpdconfname="$(basename "$httpdconfname")"
  1547. else
  1548. httpdroot="$($_APACHECTL -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"')"
  1549. _debug httpdroot "$httpdroot"
  1550. httpdconf="$httpdroot/$httpdconfname"
  1551. httpdconfname="$(basename "$httpdconfname")"
  1552. fi
  1553. fi
  1554. _debug httpdconf "$httpdconf"
  1555. _debug httpdconfname "$httpdconfname"
  1556. if [ ! -f "$httpdconf" ]; then
  1557. _err "Apache Config file not found" "$httpdconf"
  1558. return 1
  1559. fi
  1560. return 0
  1561. }
  1562. _restoreApache() {
  1563. if [ -z "$usingApache" ]; then
  1564. return 0
  1565. fi
  1566. _initpath
  1567. if ! _apachePath; then
  1568. return 1
  1569. fi
  1570. if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ]; then
  1571. _debug "No config file to restore."
  1572. return 0
  1573. fi
  1574. cat "$APACHE_CONF_BACKUP_DIR/$httpdconfname" >"$httpdconf"
  1575. _debug "Restored: $httpdconf."
  1576. if ! _exec $_APACHECTL -t; then
  1577. _exec_err
  1578. _err "Sorry, restore apache config error, please contact me."
  1579. return 1
  1580. fi
  1581. _debug "Restored successfully."
  1582. rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname"
  1583. return 0
  1584. }
  1585. _setApache() {
  1586. _initpath
  1587. if ! _apachePath; then
  1588. return 1
  1589. fi
  1590. #test the conf first
  1591. _info "Checking if there is an error in the apache config file before starting."
  1592. if ! _exec "$_APACHECTL" -t >/dev/null; then
  1593. _exec_err
  1594. _err "The apache config file has error, please fix it first, then try again."
  1595. _err "Don't worry, there is nothing changed to your system."
  1596. return 1
  1597. else
  1598. _info "OK"
  1599. fi
  1600. #backup the conf
  1601. _debug "Backup apache config file" "$httpdconf"
  1602. if ! cp "$httpdconf" "$APACHE_CONF_BACKUP_DIR/"; then
  1603. _err "Can not backup apache config file, so abort. Don't worry, the apache config is not changed."
  1604. _err "This might be a bug of $PROJECT_NAME , pleae report issue: $PROJECT"
  1605. return 1
  1606. fi
  1607. _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname"
  1608. _info "In case there is an error that can not be restored automatically, you may try restore it yourself."
  1609. _info "The backup file will be deleted on sucess, just forget it."
  1610. #add alias
  1611. apacheVer="$($_APACHECTL -V | grep "Server version:" | cut -d : -f 2 | cut -d " " -f 2 | cut -d '/' -f 2)"
  1612. _debug "apacheVer" "$apacheVer"
  1613. apacheMajer="$(echo "$apacheVer" | cut -d . -f 1)"
  1614. apacheMinor="$(echo "$apacheVer" | cut -d . -f 2)"
  1615. if [ "$apacheVer" ] && [ "$apacheMajer$apacheMinor" -ge "24" ]; then
  1616. echo "
  1617. Alias /.well-known/acme-challenge $ACME_DIR
  1618. <Directory $ACME_DIR >
  1619. Require all granted
  1620. </Directory>
  1621. " >>"$httpdconf"
  1622. else
  1623. echo "
  1624. Alias /.well-known/acme-challenge $ACME_DIR
  1625. <Directory $ACME_DIR >
  1626. Order allow,deny
  1627. Allow from all
  1628. </Directory>
  1629. " >>"$httpdconf"
  1630. fi
  1631. _msg="$($_APACHECTL -t 2>&1)"
  1632. if [ "$?" != "0" ]; then
  1633. _err "Sorry, apache config error"
  1634. if _restoreApache; then
  1635. _err "The apache config file is restored."
  1636. else
  1637. _err "Sorry, The apache config file can not be restored, please report bug."
  1638. fi
  1639. return 1
  1640. fi
  1641. if [ ! -d "$ACME_DIR" ]; then
  1642. mkdir -p "$ACME_DIR"
  1643. chmod 755 "$ACME_DIR"
  1644. fi
  1645. if ! _exec "$_APACHECTL" graceful; then
  1646. _exec_err
  1647. _err "$_APACHECTL graceful error, please contact me."
  1648. _restoreApache
  1649. return 1
  1650. fi
  1651. usingApache="1"
  1652. return 0
  1653. }
  1654. _clearup() {
  1655. _stopserver "$serverproc"
  1656. serverproc=""
  1657. _restoreApache
  1658. _clearupdns
  1659. if [ -z "$DEBUG" ]; then
  1660. rm -f "$TLS_CONF"
  1661. rm -f "$TLS_CERT"
  1662. rm -f "$TLS_KEY"
  1663. rm -f "$TLS_CSR"
  1664. fi
  1665. }
  1666. _clearupdns() {
  1667. _debug "_clearupdns"
  1668. if [ "$dnsadded" != 1 ] || [ -z "$vlist" ]; then
  1669. _debug "Dns not added, skip."
  1670. return
  1671. fi
  1672. ventries=$(echo "$vlist" | tr ',' ' ')
  1673. for ventry in $ventries; do
  1674. d=$(echo "$ventry" | cut -d "$sep" -f 1)
  1675. keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
  1676. vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
  1677. _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
  1678. if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
  1679. _info "$d is already verified, skip $vtype."
  1680. continue
  1681. fi
  1682. if [ "$vtype" != "$VTYPE_DNS" ]; then
  1683. _info "Skip $d for $vtype"
  1684. continue
  1685. fi
  1686. d_api="$(_findHook "$d" dnsapi "$_currentRoot")"
  1687. _debug d_api "$d_api"
  1688. if [ -z "$d_api" ]; then
  1689. _info "Not Found domain api file: $d_api"
  1690. continue
  1691. fi
  1692. (
  1693. if ! . "$d_api"; then
  1694. _err "Load file $d_api error. Please check your api file and try again."
  1695. return 1
  1696. fi
  1697. rmcommand="${_currentRoot}_rm"
  1698. if ! _exists "$rmcommand"; then
  1699. _err "It seems that your api file doesn't define $rmcommand"
  1700. return 1
  1701. fi
  1702. txtdomain="_acme-challenge.$d"
  1703. if ! $rmcommand "$txtdomain"; then
  1704. _err "Error removing txt for domain:$txtdomain"
  1705. return 1
  1706. fi
  1707. )
  1708. done
  1709. }
  1710. # webroot removelevel tokenfile
  1711. _clearupwebbroot() {
  1712. __webroot="$1"
  1713. if [ -z "$__webroot" ]; then
  1714. _debug "no webroot specified, skip"
  1715. return 0
  1716. fi
  1717. _rmpath=""
  1718. if [ "$2" = '1' ]; then
  1719. _rmpath="$__webroot/.well-known"
  1720. elif [ "$2" = '2' ]; then
  1721. _rmpath="$__webroot/.well-known/acme-challenge"
  1722. elif [ "$2" = '3' ]; then
  1723. _rmpath="$__webroot/.well-known/acme-challenge/$3"
  1724. else
  1725. _debug "Skip for removelevel:$2"
  1726. fi
  1727. if [ "$_rmpath" ]; then
  1728. if [ "$DEBUG" ]; then
  1729. _debug "Debugging, skip removing: $_rmpath"
  1730. else
  1731. rm -rf "$_rmpath"
  1732. fi
  1733. fi
  1734. return 0
  1735. }
  1736. _on_before_issue() {
  1737. _debug _on_before_issue
  1738. if _hasfield "$Le_Webroot" "$NO_VALUE"; then
  1739. if ! _exists "nc"; then
  1740. _err "Please install netcat(nc) tools first."
  1741. return 1
  1742. fi
  1743. fi
  1744. _debug Le_LocalAddress "$Le_LocalAddress"
  1745. alldomains=$(echo "$Le_Domain,$Le_Alt" | tr ',' ' ')
  1746. _index=1
  1747. _currentRoot=""
  1748. _addrIndex=1
  1749. for d in $alldomains; do
  1750. _debug "Check for domain" "$d"
  1751. _currentRoot="$(_getfield "$Le_Webroot" $_index)"
  1752. _debug "_currentRoot" "$_currentRoot"
  1753. _index=$(_math $_index + 1)
  1754. _checkport=""
  1755. if [ "$_currentRoot" = "$NO_VALUE" ]; then
  1756. _info "Standalone mode."
  1757. if [ -z "$Le_HTTPPort" ]; then
  1758. Le_HTTPPort=80
  1759. else
  1760. _savedomainconf "Le_HTTPPort" "$Le_HTTPPort"
  1761. fi
  1762. _checkport="$Le_HTTPPort"
  1763. elif [ "$_currentRoot" = "$W_TLS" ]; then
  1764. _info "Standalone tls mode."
  1765. if [ -z "$Le_TLSPort" ]; then
  1766. Le_TLSPort=443
  1767. else
  1768. _savedomainconf "Le_TLSPort" "$Le_TLSPort"
  1769. fi
  1770. _checkport="$Le_TLSPort"
  1771. fi
  1772. if [ "$_checkport" ]; then
  1773. _debug _checkport "$_checkport"
  1774. _checkaddr="$(_getfield "$Le_LocalAddress" $_addrIndex)"
  1775. _debug _checkaddr "$_checkaddr"
  1776. _addrIndex="$(_math $_addrIndex + 1)"
  1777. _netprc="$(_ss "$_checkport" | grep "$_checkport")"
  1778. netprc="$(echo "$_netprc" | grep "$_checkaddr")"
  1779. if [ -z "$netprc" ]; then
  1780. netprc="$(echo "$_netprc" | grep "$LOCAL_ANY_ADDRESS")"
  1781. fi
  1782. if [ "$netprc" ]; then
  1783. _err "$netprc"
  1784. _err "tcp port $_checkport is already used by $(echo "$netprc" | cut -d : -f 4)"
  1785. _err "Please stop it first"
  1786. return 1
  1787. fi
  1788. fi
  1789. done
  1790. if _hasfield "$Le_Webroot" "apache"; then
  1791. if ! _setApache; then
  1792. _err "set up apache error. Report error to me."
  1793. return 1
  1794. fi
  1795. else
  1796. usingApache=""
  1797. fi
  1798. #run pre hook
  1799. if [ "$Le_PreHook" ]; then
  1800. _info "Run pre hook:'$Le_PreHook'"
  1801. if ! (
  1802. cd "$DOMAIN_PATH" && eval "$Le_PreHook"
  1803. ); then
  1804. _err "Error when run pre hook."
  1805. return 1
  1806. fi
  1807. fi
  1808. }
  1809. _on_issue_err() {
  1810. _debug _on_issue_err
  1811. if [ "$LOG_FILE" ]; then
  1812. _err "Please check log file for more details: $LOG_FILE"
  1813. else
  1814. _err "Please use add '--debug' or '--log' to check more details."
  1815. _err "See: $_DEBUG_WIKI"
  1816. fi
  1817. if [ "$DEBUG" ] && [ "$DEBUG" -gt "0" ]; then
  1818. _debug "$(_dlg_versions)"
  1819. fi
  1820. #run the post hook
  1821. if [ "$Le_PostHook" ]; then
  1822. _info "Run post hook:'$Le_PostHook'"
  1823. if ! (
  1824. cd "$DOMAIN_PATH" && eval "$Le_PostHook"
  1825. ); then
  1826. _err "Error when run post hook."
  1827. return 1
  1828. fi
  1829. fi
  1830. }
  1831. _on_issue_success() {
  1832. _debug _on_issue_success
  1833. #run the post hook
  1834. if [ "$Le_PostHook" ]; then
  1835. _info "Run post hook:'$Le_PostHook'"
  1836. if ! (
  1837. cd "$DOMAIN_PATH" && eval "$Le_PostHook"
  1838. ); then
  1839. _err "Error when run post hook."
  1840. return 1
  1841. fi
  1842. fi
  1843. #run renew hook
  1844. if [ "$IS_RENEW" ] && [ "$Le_RenewHook" ]; then
  1845. _info "Run renew hook:'$Le_RenewHook'"
  1846. if ! (
  1847. cd "$DOMAIN_PATH" && eval "$Le_RenewHook"
  1848. ); then
  1849. _err "Error when run renew hook."
  1850. return 1
  1851. fi
  1852. fi
  1853. }
  1854. updateaccount() {
  1855. _initpath
  1856. _regAccount
  1857. }
  1858. registeraccount() {
  1859. _reg_length="$1"
  1860. _initpath
  1861. _regAccount "$_reg_length"
  1862. }
  1863. __calcAccountKeyHash() {
  1864. [ -f "$ACCOUNT_KEY_PATH" ] && _digest sha256 <"$ACCOUNT_KEY_PATH"
  1865. }
  1866. #keylength
  1867. _regAccount() {
  1868. _initpath
  1869. _reg_length="$1"
  1870. if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then
  1871. _info "mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH"
  1872. mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH"
  1873. fi
  1874. if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then
  1875. _info "mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH"
  1876. mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH"
  1877. fi
  1878. if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
  1879. if ! _create_account_key "$_reg_length"; then
  1880. _err "Create account key error."
  1881. return 1
  1882. fi
  1883. fi
  1884. if ! _calcjwk "$ACCOUNT_KEY_PATH"; then
  1885. return 1
  1886. fi
  1887. _updateTos=""
  1888. _reg_res="new-reg"
  1889. while true; do
  1890. _debug AGREEMENT "$AGREEMENT"
  1891. regjson='{"resource": "'$_reg_res'", "agreement": "'$AGREEMENT'"}'
  1892. if [ "$ACCOUNT_EMAIL" ]; then
  1893. regjson='{"resource": "'$_reg_res'", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "agreement": "'$AGREEMENT'"}'
  1894. fi
  1895. if [ -z "$_updateTos" ]; then
  1896. _info "Registering account"
  1897. if ! _send_signed_request "$API/acme/new-reg" "$regjson"; then
  1898. _err "Register account Error: $response"
  1899. return 1
  1900. fi
  1901. if [ "$code" = "" ] || [ "$code" = '201' ]; then
  1902. echo "$response" >"$ACCOUNT_JSON_PATH"
  1903. _info "Registered"
  1904. elif [ "$code" = '409' ]; then
  1905. _info "Already registered"
  1906. else
  1907. _err "Register account Error: $response"
  1908. return 1
  1909. fi
  1910. _accUri="$(echo "$responseHeaders" | grep "^Location:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
  1911. _debug "_accUri" "$_accUri"
  1912. _tos="$(echo "$responseHeaders" | grep "^Link:.*rel=\"terms-of-service\"" | _head_n 1 | _egrep_o "<.*>" | tr -d '<>')"
  1913. _debug "_tos" "$_tos"
  1914. if [ -z "$_tos" ]; then
  1915. _debug "Use default tos: $DEFAULT_AGREEMENT"
  1916. _tos="$DEFAULT_AGREEMENT"
  1917. fi
  1918. if [ "$_tos" != "$AGREEMENT" ]; then
  1919. _updateTos=1
  1920. AGREEMENT="$_tos"
  1921. _reg_res="reg"
  1922. continue
  1923. fi
  1924. else
  1925. _debug "Update tos: $_tos"
  1926. if ! _send_signed_request "$_accUri" "$regjson"; then
  1927. _err "Update tos error."
  1928. return 1
  1929. fi
  1930. if [ "$code" = '202' ]; then
  1931. _info "Update success."
  1932. CA_KEY_HASH="$(__calcAccountKeyHash)"
  1933. _debug "Calc CA_KEY_HASH" "$CA_KEY_HASH"
  1934. _savecaconf CA_KEY_HASH "$CA_KEY_HASH"
  1935. else
  1936. _err "Update account error."
  1937. return 1
  1938. fi
  1939. fi
  1940. return 0
  1941. done
  1942. }
  1943. # domain folder file
  1944. _findHook() {
  1945. _hookdomain="$1"
  1946. _hookcat="$2"
  1947. _hookname="$3"
  1948. if [ -f "$_SCRIPT_HOME/$_hookdomain/$_hookname" ]; then
  1949. d_api="$_SCRIPT_HOME/$_hookdomain/$_hookname"
  1950. elif [ -f "$_SCRIPT_HOME/$_hookdomain/$_hookname.sh" ]; then
  1951. d_api="$_SCRIPT_HOME/$_hookdomain/$_hookname.sh"
  1952. elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname" ]; then
  1953. d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname"
  1954. elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname.sh" ]; then
  1955. d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname.sh"
  1956. elif [ -f "$LE_WORKING_DIR/$_hookname" ]; then
  1957. d_api="$LE_WORKING_DIR/$_hookname"
  1958. elif [ -f "$LE_WORKING_DIR/$_hookname.sh" ]; then
  1959. d_api="$LE_WORKING_DIR/$_hookname.sh"
  1960. elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname" ]; then
  1961. d_api="$LE_WORKING_DIR/$_hookcat/$_hookname"
  1962. elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname.sh" ]; then
  1963. d_api="$LE_WORKING_DIR/$_hookcat/$_hookname.sh"
  1964. fi
  1965. printf "%s" "$d_api"
  1966. }
  1967. #domain
  1968. __get_domain_new_authz() {
  1969. _gdnd="$1"
  1970. _info "Getting new-authz for domain" "$_gdnd"
  1971. _Max_new_authz_retry_times=5
  1972. _authz_i=0
  1973. while [ "$_authz_i" -lt "$_Max_new_authz_retry_times" ]; do
  1974. _info "Try new-authz for the $_authz_i time."
  1975. if ! _send_signed_request "$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$(_idn "$_gdnd")\"}}"; then
  1976. _err "Can not get domain new authz."
  1977. return 1
  1978. fi
  1979. if ! _contains "$response" "An error occurred while processing your request"; then
  1980. _info "The new-authz request is ok."
  1981. break
  1982. fi
  1983. _authz_i="$(_math "$_authz_i" + 1)"
  1984. _info "The server is busy, Sleep $_authz_i to retry."
  1985. _sleep "$_authz_i"
  1986. done
  1987. if [ "$_authz_i" = "$_Max_new_authz_retry_times" ]; then
  1988. _debug "new-authz retry reach the max $_Max_new_authz_retry_times times."
  1989. fi
  1990. if [ ! -z "$code" ] && [ ! "$code" = '201' ]; then
  1991. _err "new-authz error: $response"
  1992. return 1
  1993. fi
  1994. }
  1995. #webroot, domain domainlist keylength
  1996. issue() {
  1997. if [ -z "$2" ]; then
  1998. _usage "Usage: $PROJECT_ENTRY --issue -d a.com -w /path/to/webroot/a.com/ "
  1999. return 1
  2000. fi
  2001. Le_Webroot="$1"
  2002. Le_Domain="$2"
  2003. Le_Alt="$3"
  2004. Le_Keylength="$4"
  2005. Le_RealCertPath="$5"
  2006. Le_RealKeyPath="$6"
  2007. Le_RealCACertPath="$7"
  2008. Le_ReloadCmd="$8"
  2009. Le_RealFullChainPath="$9"
  2010. Le_PreHook="${10}"
  2011. Le_PostHook="${11}"
  2012. Le_RenewHook="${12}"
  2013. Le_LocalAddress="${13}"
  2014. #remove these later.
  2015. if [ "$Le_Webroot" = "dns-cf" ]; then
  2016. Le_Webroot="dns_cf"
  2017. fi
  2018. if [ "$Le_Webroot" = "dns-dp" ]; then
  2019. Le_Webroot="dns_dp"
  2020. fi
  2021. if [ "$Le_Webroot" = "dns-cx" ]; then
  2022. Le_Webroot="dns_cx"
  2023. fi
  2024. _debug "Using api: $API"
  2025. if [ ! "$IS_RENEW" ]; then
  2026. _initpath "$Le_Domain" "$Le_Keylength"
  2027. mkdir -p "$DOMAIN_PATH"
  2028. fi
  2029. if [ -f "$DOMAIN_CONF" ]; then
  2030. Le_NextRenewTime=$(_readdomainconf Le_NextRenewTime)
  2031. _debug Le_NextRenewTime "$Le_NextRenewTime"
  2032. if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
  2033. _saved_domain=$(_readdomainconf Le_Domain)
  2034. _debug _saved_domain "$_saved_domain"
  2035. _saved_alt=$(_readdomainconf Le_Alt)
  2036. _debug _saved_alt "$_saved_alt"
  2037. if [ "$_saved_domain,$_saved_alt" = "$Le_Domain,$Le_Alt" ]; then
  2038. _info "Domains not changed."
  2039. _info "Skip, Next renewal time is: $(__green "$(_readdomainconf Le_NextRenewTimeStr)")"
  2040. _info "Add '$(__red '--force')' to force to renew."
  2041. return $RENEW_SKIP
  2042. else
  2043. _info "Domains have changed."
  2044. fi
  2045. fi
  2046. fi
  2047. _savedomainconf "Le_Domain" "$Le_Domain"
  2048. _savedomainconf "Le_Alt" "$Le_Alt"
  2049. _savedomainconf "Le_Webroot" "$Le_Webroot"
  2050. _savedomainconf "Le_PreHook" "$Le_PreHook"
  2051. _savedomainconf "Le_PostHook" "$Le_PostHook"
  2052. _savedomainconf "Le_RenewHook" "$Le_RenewHook"
  2053. if [ "$Le_LocalAddress" ]; then
  2054. _savedomainconf "Le_LocalAddress" "$Le_LocalAddress"
  2055. else
  2056. _cleardomainconf "Le_LocalAddress"
  2057. fi
  2058. Le_API="$API"
  2059. _savedomainconf "Le_API" "$Le_API"
  2060. if [ "$Le_Alt" = "$NO_VALUE" ]; then
  2061. Le_Alt=""
  2062. fi
  2063. if [ "$Le_Keylength" = "$NO_VALUE" ]; then
  2064. Le_Keylength=""
  2065. fi
  2066. if ! _on_before_issue; then
  2067. _err "_on_before_issue."
  2068. return 1
  2069. fi
  2070. _saved_account_key_hash="$(_readcaconf "CA_KEY_HASH")"
  2071. _debug2 _saved_account_key_hash "$_saved_account_key_hash"
  2072. if [ -z "$_saved_account_key_hash" ] || [ "$_saved_account_key_hash" != "$(__calcAccountKeyHash)" ]; then
  2073. if ! _regAccount "$_accountkeylength"; then
  2074. _on_issue_err
  2075. return 1
  2076. fi
  2077. else
  2078. _debug "_saved_account_key_hash is not changed, skip register account."
  2079. fi
  2080. if [ -f "$CSR_PATH" ] && [ ! -f "$CERT_KEY_PATH" ]; then
  2081. _info "Signing from existing CSR."
  2082. else
  2083. _key=$(_readdomainconf Le_Keylength)
  2084. _debug "Read key length:$_key"
  2085. if [ ! -f "$CERT_KEY_PATH" ] || [ "$Le_Keylength" != "$_key" ]; then
  2086. if ! createDomainKey "$Le_Domain" "$Le_Keylength"; then
  2087. _err "Create domain key error."
  2088. _clearup
  2089. _on_issue_err
  2090. return 1
  2091. fi
  2092. fi
  2093. if ! _createcsr "$Le_Domain" "$Le_Alt" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"; then
  2094. _err "Create CSR error."
  2095. _clearup
  2096. _on_issue_err
  2097. return 1
  2098. fi
  2099. fi
  2100. _savedomainconf "Le_Keylength" "$Le_Keylength"
  2101. vlist="$Le_Vlist"
  2102. _info "Getting domain auth token for each domain"
  2103. sep='#'
  2104. if [ -z "$vlist" ]; then
  2105. alldomains=$(echo "$Le_Domain,$Le_Alt" | tr ',' ' ')
  2106. _index=1
  2107. _currentRoot=""
  2108. for d in $alldomains; do
  2109. _info "Getting webroot for domain" "$d"
  2110. _w="$(echo $Le_Webroot | cut -d , -f $_index)"
  2111. _info _w "$_w"
  2112. if [ "$_w" ]; then
  2113. _currentRoot="$_w"
  2114. fi
  2115. _debug "_currentRoot" "$_currentRoot"
  2116. _index=$(_math $_index + 1)
  2117. vtype="$VTYPE_HTTP"
  2118. if _startswith "$_currentRoot" "dns"; then
  2119. vtype="$VTYPE_DNS"
  2120. fi
  2121. if [ "$_currentRoot" = "$W_TLS" ]; then
  2122. vtype="$VTYPE_TLS"
  2123. fi
  2124. if ! __get_domain_new_authz "$d"; then
  2125. _clearup
  2126. _on_issue_err
  2127. return 1
  2128. fi
  2129. if [ -z "$thumbprint" ]; then
  2130. accountkey_json=$(printf "%s" "$jwk" | tr -d ' ')
  2131. thumbprint=$(printf "%s" "$accountkey_json" | _digest "sha256" | _urlencode)
  2132. fi
  2133. entry="$(printf "%s\n" "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')"
  2134. _debug entry "$entry"
  2135. if [ -z "$entry" ]; then
  2136. _err "Error, can not get domain token $d"
  2137. _clearup
  2138. _on_issue_err
  2139. return 1
  2140. fi
  2141. token="$(printf "%s\n" "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
  2142. _debug token "$token"
  2143. uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*' | cut -d : -f 2,3 | tr -d '"')"
  2144. _debug uri "$uri"
  2145. keyauthorization="$token.$thumbprint"
  2146. _debug keyauthorization "$keyauthorization"
  2147. if printf "%s" "$response" | grep '"status":"valid"' >/dev/null 2>&1; then
  2148. _info "$d is already verified, skip."
  2149. keyauthorization="$STATE_VERIFIED"
  2150. _debug keyauthorization "$keyauthorization"
  2151. fi
  2152. dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot"
  2153. _debug dvlist "$dvlist"
  2154. vlist="$vlist$dvlist,"
  2155. done
  2156. #add entry
  2157. dnsadded=""
  2158. ventries=$(echo "$vlist" | tr ',' ' ')
  2159. for ventry in $ventries; do
  2160. d=$(echo "$ventry" | cut -d "$sep" -f 1)
  2161. keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
  2162. vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
  2163. _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
  2164. if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
  2165. _info "$d is already verified, skip $vtype."
  2166. continue
  2167. fi
  2168. if [ "$vtype" = "$VTYPE_DNS" ]; then
  2169. dnsadded='0'
  2170. txtdomain="_acme-challenge.$d"
  2171. _debug txtdomain "$txtdomain"
  2172. txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _urlencode)"
  2173. _debug txt "$txt"
  2174. d_api="$(_findHook "$d" dnsapi "$_currentRoot")"
  2175. _debug d_api "$d_api"
  2176. if [ "$d_api" ]; then
  2177. _info "Found domain api file: $d_api"
  2178. else
  2179. _err "Add the following TXT record:"
  2180. _err "Domain: '$(__green "$txtdomain")'"
  2181. _err "TXT value: '$(__green "$txt")'"
  2182. _err "Please be aware that you prepend _acme-challenge. before your domain"
  2183. _err "so the resulting subdomain will be: $txtdomain"
  2184. continue
  2185. fi
  2186. (
  2187. if ! . "$d_api"; then
  2188. _err "Load file $d_api error. Please check your api file and try again."
  2189. return 1
  2190. fi
  2191. addcommand="${_currentRoot}_add"
  2192. if ! _exists "$addcommand"; then
  2193. _err "It seems that your api file is not correct, it must have a function named: $addcommand"
  2194. return 1
  2195. fi
  2196. if ! $addcommand "$txtdomain" "$txt"; then
  2197. _err "Error add txt for domain:$txtdomain"
  2198. return 1
  2199. fi
  2200. )
  2201. if [ "$?" != "0" ]; then
  2202. _clearup
  2203. _on_issue_err
  2204. return 1
  2205. fi
  2206. dnsadded='1'
  2207. fi
  2208. done
  2209. if [ "$dnsadded" = '0' ]; then
  2210. _savedomainconf "Le_Vlist" "$vlist"
  2211. _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit."
  2212. _err "Please add the TXT records to the domains, and retry again."
  2213. _clearup
  2214. _on_issue_err
  2215. return 1
  2216. fi
  2217. fi
  2218. if [ "$dnsadded" = '1' ]; then
  2219. if [ -z "$Le_DNSSleep" ]; then
  2220. Le_DNSSleep="$DEFAULT_DNS_SLEEP"
  2221. else
  2222. _savedomainconf "Le_DNSSleep" "$Le_DNSSleep"
  2223. fi
  2224. _info "Sleep $(__green $Le_DNSSleep) seconds for the txt records to take effect"
  2225. _sleep "$Le_DNSSleep"
  2226. fi
  2227. _debug "ok, let's start to verify"
  2228. _ncIndex=1
  2229. ventries=$(echo "$vlist" | tr ',' ' ')
  2230. for ventry in $ventries; do
  2231. d=$(echo "$ventry" | cut -d "$sep" -f 1)
  2232. keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
  2233. uri=$(echo "$ventry" | cut -d "$sep" -f 3)
  2234. vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
  2235. _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
  2236. if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
  2237. _info "$d is already verified, skip $vtype."
  2238. continue
  2239. fi
  2240. _info "Verifying:$d"
  2241. _debug "d" "$d"
  2242. _debug "keyauthorization" "$keyauthorization"
  2243. _debug "uri" "$uri"
  2244. removelevel=""
  2245. token="$(printf "%s" "$keyauthorization" | cut -d '.' -f 1)"
  2246. _debug "_currentRoot" "$_currentRoot"
  2247. if [ "$vtype" = "$VTYPE_HTTP" ]; then
  2248. if [ "$_currentRoot" = "$NO_VALUE" ]; then
  2249. _info "Standalone mode server"
  2250. _ncaddr="$(_getfield "$Le_LocalAddress" "$_ncIndex")"
  2251. _ncIndex="$(_math $_ncIndex + 1)"
  2252. _startserver "$keyauthorization" "$_ncaddr" &
  2253. if [ "$?" != "0" ]; then
  2254. _clearup
  2255. _on_issue_err
  2256. return 1
  2257. fi
  2258. serverproc="$!"
  2259. sleep 1
  2260. _debug serverproc "$serverproc"
  2261. else
  2262. if [ "$_currentRoot" = "apache" ]; then
  2263. wellknown_path="$ACME_DIR"
  2264. else
  2265. wellknown_path="$_currentRoot/.well-known/acme-challenge"
  2266. if [ ! -d "$_currentRoot/.well-known" ]; then
  2267. removelevel='1'
  2268. elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ]; then
  2269. removelevel='2'
  2270. else
  2271. removelevel='3'
  2272. fi
  2273. fi
  2274. _debug wellknown_path "$wellknown_path"
  2275. _debug "writing token:$token to $wellknown_path/$token"
  2276. mkdir -p "$wellknown_path"
  2277. if ! printf "%s" "$keyauthorization" >"$wellknown_path/$token"; then
  2278. _err "$d:Can not write token to file : $wellknown_path/$token"
  2279. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  2280. _clearup
  2281. _on_issue_err
  2282. return 1
  2283. fi
  2284. if [ ! "$usingApache" ]; then
  2285. if webroot_owner=$(_stat "$_currentRoot"); then
  2286. _debug "Changing owner/group of .well-known to $webroot_owner"
  2287. chown -R "$webroot_owner" "$_currentRoot/.well-known"
  2288. else
  2289. _debug "not chaning owner/group of webroot"
  2290. fi
  2291. fi
  2292. fi
  2293. elif [ "$vtype" = "$VTYPE_TLS" ]; then
  2294. #create A
  2295. #_hash_A="$(printf "%s" $token | _digest "sha256" "hex" )"
  2296. #_debug2 _hash_A "$_hash_A"
  2297. #_x="$(echo $_hash_A | cut -c 1-32)"
  2298. #_debug2 _x "$_x"
  2299. #_y="$(echo $_hash_A | cut -c 33-64)"
  2300. #_debug2 _y "$_y"
  2301. #_SAN_A="$_x.$_y.token.acme.invalid"
  2302. #_debug2 _SAN_A "$_SAN_A"
  2303. #create B
  2304. _hash_B="$(printf "%s" "$keyauthorization" | _digest "sha256" "hex")"
  2305. _debug2 _hash_B "$_hash_B"
  2306. _x="$(echo "$_hash_B" | cut -c 1-32)"
  2307. _debug2 _x "$_x"
  2308. _y="$(echo "$_hash_B" | cut -c 33-64)"
  2309. _debug2 _y "$_y"
  2310. #_SAN_B="$_x.$_y.ka.acme.invalid"
  2311. _SAN_B="$_x.$_y.acme.invalid"
  2312. _debug2 _SAN_B "$_SAN_B"
  2313. _ncaddr="$(_getfield "$Le_LocalAddress" "$_ncIndex")"
  2314. _ncIndex="$(_math "$_ncIndex" + 1)"
  2315. if ! _starttlsserver "$_SAN_B" "$_SAN_A" "$Le_TLSPort" "$keyauthorization" "$_ncaddr"; then
  2316. _err "Start tls server error."
  2317. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  2318. _clearup
  2319. _on_issue_err
  2320. return 1
  2321. fi
  2322. fi
  2323. if ! _send_signed_request "$uri" "{\"resource\": \"challenge\", \"keyAuthorization\": \"$keyauthorization\"}"; then
  2324. _err "$d:Can not get challenge: $response"
  2325. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  2326. _clearup
  2327. _on_issue_err
  2328. return 1
  2329. fi
  2330. if [ ! -z "$code" ] && [ ! "$code" = '202' ]; then
  2331. _err "$d:Challenge error: $response"
  2332. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  2333. _clearup
  2334. _on_issue_err
  2335. return 1
  2336. fi
  2337. waittimes=0
  2338. if [ -z "$MAX_RETRY_TIMES" ]; then
  2339. MAX_RETRY_TIMES=30
  2340. fi
  2341. while true; do
  2342. waittimes=$(_math "$waittimes" + 1)
  2343. if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ]; then
  2344. _err "$d:Timeout"
  2345. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  2346. _clearup
  2347. _on_issue_err
  2348. return 1
  2349. fi
  2350. _debug "sleep 2 secs to verify"
  2351. sleep 2
  2352. _debug "checking"
  2353. response="$(_get "$uri")"
  2354. if [ "$?" != "0" ]; then
  2355. _err "$d:Verify error:$response"
  2356. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  2357. _clearup
  2358. _on_issue_err
  2359. return 1
  2360. fi
  2361. _debug2 original "$response"
  2362. response="$(echo "$response" | _normalizeJson)"
  2363. _debug2 response "$response"
  2364. status=$(echo "$response" | _egrep_o '"status":"[^"]*' | cut -d : -f 2 | tr -d '"')
  2365. if [ "$status" = "valid" ]; then
  2366. _info "Success"
  2367. _stopserver "$serverproc"
  2368. serverproc=""
  2369. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  2370. break
  2371. fi
  2372. if [ "$status" = "invalid" ]; then
  2373. error="$(echo "$response" | tr -d "\r\n" | _egrep_o '"error":\{[^\}]*')"
  2374. _debug2 error "$error"
  2375. errordetail="$(echo "$error" | _egrep_o '"detail": *"[^"]*' | cut -d '"' -f 4)"
  2376. _debug2 errordetail "$errordetail"
  2377. if [ "$errordetail" ]; then
  2378. _err "$d:Verify error:$errordetail"
  2379. else
  2380. _err "$d:Verify error:$error"
  2381. fi
  2382. if [ "$DEBUG" ]; then
  2383. if [ "$vtype" = "$VTYPE_HTTP" ]; then
  2384. _debug "Debug: get token url."
  2385. _get "http://$d/.well-known/acme-challenge/$token" "" 1
  2386. fi
  2387. fi
  2388. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  2389. _clearup
  2390. _on_issue_err
  2391. return 1
  2392. fi
  2393. if [ "$status" = "pending" ]; then
  2394. _info "Pending"
  2395. else
  2396. _err "$d:Verify error:$response"
  2397. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  2398. _clearup
  2399. _on_issue_err
  2400. return 1
  2401. fi
  2402. done
  2403. done
  2404. _clearup
  2405. _info "Verify finished, start to sign."
  2406. der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _urlencode)"
  2407. if ! _send_signed_request "$API/acme/new-cert" "{\"resource\": \"new-cert\", \"csr\": \"$der\"}" "needbase64"; then
  2408. _err "Sign failed."
  2409. _on_issue_err
  2410. return 1
  2411. fi
  2412. _rcert="$response"
  2413. Le_LinkCert="$(grep -i '^Location.*$' "$HTTP_HEADER" | _head_n 1 | tr -d "\r\n" | cut -d " " -f 2)"
  2414. _savedomainconf "Le_LinkCert" "$Le_LinkCert"
  2415. if [ "$Le_LinkCert" ]; then
  2416. echo "$BEGIN_CERT" >"$CERT_PATH"
  2417. #if ! _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH" ; then
  2418. # _debug "Get cert failed. Let's try last response."
  2419. # printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >> "$CERT_PATH"
  2420. #fi
  2421. if ! printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >>"$CERT_PATH"; then
  2422. _debug "Try cert link."
  2423. _get "$Le_LinkCert" | _base64 "multiline" >>"$CERT_PATH"
  2424. fi
  2425. echo "$END_CERT" >>"$CERT_PATH"
  2426. _info "$(__green "Cert success.")"
  2427. cat "$CERT_PATH"
  2428. _info "Your cert is in $(__green " $CERT_PATH ")"
  2429. if [ -f "$CERT_KEY_PATH" ]; then
  2430. _info "Your cert key is in $(__green " $CERT_KEY_PATH ")"
  2431. fi
  2432. cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH"
  2433. if [ ! "$USER_PATH" ] || [ ! "$IN_CRON" ]; then
  2434. USER_PATH="$PATH"
  2435. _saveaccountconf "USER_PATH" "$USER_PATH"
  2436. fi
  2437. fi
  2438. if [ -z "$Le_LinkCert" ]; then
  2439. response="$(echo "$response" | _dbase64 "multiline" | _normalizeJson)"
  2440. _err "Sign failed: $(echo "$response" | _egrep_o '"detail":"[^"]*"')"
  2441. _on_issue_err
  2442. return 1
  2443. fi
  2444. _cleardomainconf "Le_Vlist"
  2445. Le_LinkIssuer=$(grep -i '^Link' "$HTTP_HEADER" | _head_n 1 | cut -d " " -f 2 | cut -d ';' -f 1 | tr -d '<>')
  2446. if ! _contains "$Le_LinkIssuer" ":"; then
  2447. Le_LinkIssuer="$API$Le_LinkIssuer"
  2448. fi
  2449. _savedomainconf "Le_LinkIssuer" "$Le_LinkIssuer"
  2450. if [ "$Le_LinkIssuer" ]; then
  2451. echo "$BEGIN_CERT" >"$CA_CERT_PATH"
  2452. _get "$Le_LinkIssuer" | _base64 "multiline" >>"$CA_CERT_PATH"
  2453. echo "$END_CERT" >>"$CA_CERT_PATH"
  2454. _info "The intermediate CA cert is in $(__green " $CA_CERT_PATH ")"
  2455. cat "$CA_CERT_PATH" >>"$CERT_FULLCHAIN_PATH"
  2456. _info "And the full chain certs is there: $(__green " $CERT_FULLCHAIN_PATH ")"
  2457. fi
  2458. Le_CertCreateTime=$(_time)
  2459. _savedomainconf "Le_CertCreateTime" "$Le_CertCreateTime"
  2460. Le_CertCreateTimeStr=$(date -u)
  2461. _savedomainconf "Le_CertCreateTimeStr" "$Le_CertCreateTimeStr"
  2462. if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ] || [ "$Le_RenewalDays" -gt "$MAX_RENEW" ]; then
  2463. Le_RenewalDays="$MAX_RENEW"
  2464. else
  2465. _savedomainconf "Le_RenewalDays" "$Le_RenewalDays"
  2466. fi
  2467. if [ "$CA_BUNDLE" ]; then
  2468. _saveaccountconf CA_BUNDLE "$CA_BUNDLE"
  2469. else
  2470. _clearaccountconf "CA_BUNDLE"
  2471. fi
  2472. if [ "$HTTPS_INSECURE" ]; then
  2473. _saveaccountconf HTTPS_INSECURE "$HTTPS_INSECURE"
  2474. else
  2475. _clearaccountconf "HTTPS_INSECURE"
  2476. fi
  2477. if [ "$Le_Listen_V4" ]; then
  2478. _savedomainconf "Le_Listen_V4" "$Le_Listen_V4"
  2479. _cleardomainconf Le_Listen_V6
  2480. elif [ "$Le_Listen_V6" ]; then
  2481. _savedomainconf "Le_Listen_V6" "$Le_Listen_V6"
  2482. _cleardomainconf Le_Listen_V4
  2483. fi
  2484. Le_NextRenewTime=$(_math "$Le_CertCreateTime" + "$Le_RenewalDays" \* 24 \* 60 \* 60)
  2485. Le_NextRenewTimeStr=$(_time2str "$Le_NextRenewTime")
  2486. _savedomainconf "Le_NextRenewTimeStr" "$Le_NextRenewTimeStr"
  2487. Le_NextRenewTime=$(_math "$Le_NextRenewTime" - 86400)
  2488. _savedomainconf "Le_NextRenewTime" "$Le_NextRenewTime"
  2489. _on_issue_success
  2490. if [ "$Le_RealCertPath$Le_RealKeyPath$Le_RealCACertPath$Le_ReloadCmd$Le_RealFullChainPath" ]; then
  2491. _installcert
  2492. fi
  2493. }
  2494. #domain [isEcc]
  2495. renew() {
  2496. Le_Domain="$1"
  2497. if [ -z "$Le_Domain" ]; then
  2498. _usage "Usage: $PROJECT_ENTRY --renew -d domain.com [--ecc]"
  2499. return 1
  2500. fi
  2501. _isEcc="$2"
  2502. _initpath "$Le_Domain" "$_isEcc"
  2503. _info "$(__green "Renew: '$Le_Domain'")"
  2504. if [ ! -f "$DOMAIN_CONF" ]; then
  2505. _info "'$Le_Domain' is not a issued domain, skip."
  2506. return 0
  2507. fi
  2508. if [ "$Le_RenewalDays" ]; then
  2509. _savedomainconf Le_RenewalDays "$Le_RenewalDays"
  2510. fi
  2511. . "$DOMAIN_CONF"
  2512. if [ "$Le_API" ]; then
  2513. API="$Le_API"
  2514. fi
  2515. if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
  2516. _info "Skip, Next renewal time is: $(__green "$Le_NextRenewTimeStr")"
  2517. _info "Add '$(__red '--force')' to force to renew."
  2518. return "$RENEW_SKIP"
  2519. fi
  2520. IS_RENEW="1"
  2521. issue "$Le_Webroot" "$Le_Domain" "$Le_Alt" "$Le_Keylength" "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd" "$Le_RealFullChainPath" "$Le_PreHook" "$Le_PostHook" "$Le_RenewHook" "$Le_LocalAddress"
  2522. res="$?"
  2523. if [ "$res" != "0" ]; then
  2524. return "$res"
  2525. fi
  2526. if [ "$Le_DeployHook" ]; then
  2527. deploy "$Le_Domain" "$Le_DeployHook" "$Le_Keylength"
  2528. res="$?"
  2529. fi
  2530. IS_RENEW=""
  2531. return "$res"
  2532. }
  2533. #renewAll [stopRenewOnError]
  2534. renewAll() {
  2535. _initpath
  2536. _stopRenewOnError="$1"
  2537. _debug "_stopRenewOnError" "$_stopRenewOnError"
  2538. _ret="0"
  2539. for d in "${CERT_HOME}"/*.*/; do
  2540. _debug d "$d"
  2541. d=$(basename "$d")
  2542. _debug d "$d"
  2543. (
  2544. if _endswith "$d" "$ECC_SUFFIX"; then
  2545. _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
  2546. d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
  2547. fi
  2548. renew "$d" "$_isEcc"
  2549. )
  2550. rc="$?"
  2551. _debug "Return code: $rc"
  2552. if [ "$rc" != "0" ]; then
  2553. if [ "$rc" = "$RENEW_SKIP" ]; then
  2554. _info "Skipped $d"
  2555. elif [ "$_stopRenewOnError" ]; then
  2556. _err "Error renew $d, stop now."
  2557. return "$rc"
  2558. else
  2559. _ret="$rc"
  2560. _err "Error renew $d, Go ahead to next one."
  2561. fi
  2562. fi
  2563. done
  2564. return "$_ret"
  2565. }
  2566. #csr webroot
  2567. signcsr() {
  2568. _csrfile="$1"
  2569. _csrW="$2"
  2570. if [ -z "$_csrfile" ] || [ -z "$_csrW" ]; then
  2571. _usage "Usage: $PROJECT_ENTRY --signcsr --csr mycsr.csr -w /path/to/webroot/a.com/ "
  2572. return 1
  2573. fi
  2574. _initpath
  2575. _csrsubj=$(_readSubjectFromCSR "$_csrfile")
  2576. if [ "$?" != "0" ]; then
  2577. _err "Can not read subject from csr: $_csrfile"
  2578. return 1
  2579. fi
  2580. _debug _csrsubj "$_csrsubj"
  2581. _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
  2582. if [ "$?" != "0" ]; then
  2583. _err "Can not read domain list from csr: $_csrfile"
  2584. return 1
  2585. fi
  2586. _debug "_csrdomainlist" "$_csrdomainlist"
  2587. if [ -z "$_csrsubj" ]; then
  2588. _csrsubj="$(_getfield "$_csrdomainlist" 1)"
  2589. _debug _csrsubj "$_csrsubj"
  2590. _csrdomainlist="$(echo "$_csrdomainlist" | cut -d , -f 2-)"
  2591. _debug "_csrdomainlist" "$_csrdomainlist"
  2592. fi
  2593. if [ -z "$_csrsubj" ]; then
  2594. _err "Can not read subject from csr: $_csrfile"
  2595. return 1
  2596. fi
  2597. _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
  2598. if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
  2599. _err "Can not read key length from csr: $_csrfile"
  2600. return 1
  2601. fi
  2602. _initpath "$_csrsubj" "$_csrkeylength"
  2603. mkdir -p "$DOMAIN_PATH"
  2604. _info "Copy csr to: $CSR_PATH"
  2605. cp "$_csrfile" "$CSR_PATH"
  2606. issue "$_csrW" "$_csrsubj" "$_csrdomainlist" "$_csrkeylength"
  2607. }
  2608. showcsr() {
  2609. _csrfile="$1"
  2610. _csrd="$2"
  2611. if [ -z "$_csrfile" ] && [ -z "$_csrd" ]; then
  2612. _usage "Usage: $PROJECT_ENTRY --showcsr --csr mycsr.csr"
  2613. return 1
  2614. fi
  2615. _initpath
  2616. _csrsubj=$(_readSubjectFromCSR "$_csrfile")
  2617. if [ "$?" != "0" ] || [ -z "$_csrsubj" ]; then
  2618. _err "Can not read subject from csr: $_csrfile"
  2619. return 1
  2620. fi
  2621. _info "Subject=$_csrsubj"
  2622. _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
  2623. if [ "$?" != "0" ]; then
  2624. _err "Can not read domain list from csr: $_csrfile"
  2625. return 1
  2626. fi
  2627. _debug "_csrdomainlist" "$_csrdomainlist"
  2628. _info "SubjectAltNames=$_csrdomainlist"
  2629. _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
  2630. if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
  2631. _err "Can not read key length from csr: $_csrfile"
  2632. return 1
  2633. fi
  2634. _info "KeyLength=$_csrkeylength"
  2635. }
  2636. list() {
  2637. _raw="$1"
  2638. _initpath
  2639. _sep="|"
  2640. if [ "$_raw" ]; then
  2641. printf "%s\n" "Main_Domain${_sep}KeyLength${_sep}SAN_Domains${_sep}Created${_sep}Renew"
  2642. for d in "${CERT_HOME}"/*.*/; do
  2643. d=$(basename "$d")
  2644. _debug d "$d"
  2645. (
  2646. if _endswith "$d" "$ECC_SUFFIX"; then
  2647. _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
  2648. d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
  2649. fi
  2650. _initpath $d "$_isEcc"
  2651. if [ -f "$DOMAIN_CONF" ]; then
  2652. . "$DOMAIN_CONF"
  2653. printf "%s\n" "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr"
  2654. fi
  2655. )
  2656. done
  2657. else
  2658. if _exists column; then
  2659. list "raw" | column -t -s "$_sep"
  2660. else
  2661. list "raw" | tr "$_sep" '\t'
  2662. fi
  2663. fi
  2664. }
  2665. deploy() {
  2666. Le_Domain="$1"
  2667. Le_DeployHook="$2"
  2668. _isEcc="$3"
  2669. if [ -z "$Le_DeployHook" ]; then
  2670. _usage "Usage: $PROJECT_ENTRY --deploy -d domain.com --deploy-hook cpanel [--ecc] "
  2671. return 1
  2672. fi
  2673. _initpath $Le_Domain "$_isEcc"
  2674. if [ ! -d "$DOMAIN_PATH" ]; then
  2675. _err "Domain is not valid:'$Le_Domain'"
  2676. return 1
  2677. fi
  2678. _deployApi="$(_findHook $Le_Domain deploy $Le_DeployHook)"
  2679. if [ -z "$_deployApi" ]; then
  2680. _err "The deploy hook $Le_DeployHook is not found."
  2681. return 1
  2682. fi
  2683. _debug _deployApi "$_deployApi"
  2684. _savedomainconf Le_DeployHook "$Le_DeployHook"
  2685. if ! (
  2686. if ! . $_deployApi; then
  2687. _err "Load file $_deployApi error. Please check your api file and try again."
  2688. return 1
  2689. fi
  2690. d_command="${Le_DeployHook}_deploy"
  2691. if ! _exists $d_command; then
  2692. _err "It seems that your api file is not correct, it must have a function named: $d_command"
  2693. return 1
  2694. fi
  2695. if ! $d_command $Le_Domain "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$CERT_FULLCHAIN_PATH"; then
  2696. _err "Error deploy for domain:$Le_Domain"
  2697. _on_issue_err
  2698. return 1
  2699. fi
  2700. ); then
  2701. _err "Deploy error."
  2702. return 1
  2703. else
  2704. _info "$(__green Success)"
  2705. fi
  2706. }
  2707. installcert() {
  2708. Le_Domain="$1"
  2709. if [ -z "$Le_Domain" ]; then
  2710. _usage "Usage: $PROJECT_ENTRY --installcert -d domain.com [--ecc] [--certpath cert-file-path] [--keypath key-file-path] [--capath ca-cert-file-path] [ --reloadCmd reloadCmd] [--fullchainpath fullchain-path]"
  2711. return 1
  2712. fi
  2713. Le_RealCertPath="$2"
  2714. Le_RealKeyPath="$3"
  2715. Le_RealCACertPath="$4"
  2716. Le_ReloadCmd="$5"
  2717. Le_RealFullChainPath="$6"
  2718. _isEcc="$7"
  2719. _initpath $Le_Domain "$_isEcc"
  2720. if [ ! -d "$DOMAIN_PATH" ]; then
  2721. _err "Domain is not valid:'$Le_Domain'"
  2722. return 1
  2723. fi
  2724. _installcert
  2725. }
  2726. _installcert() {
  2727. _savedomainconf "Le_RealCertPath" "$Le_RealCertPath"
  2728. _savedomainconf "Le_RealCACertPath" "$Le_RealCACertPath"
  2729. _savedomainconf "Le_RealKeyPath" "$Le_RealKeyPath"
  2730. _savedomainconf "Le_ReloadCmd" "$Le_ReloadCmd"
  2731. _savedomainconf "Le_RealFullChainPath" "$Le_RealFullChainPath"
  2732. if [ "$Le_RealCertPath" = "$NO_VALUE" ]; then
  2733. Le_RealCertPath=""
  2734. fi
  2735. if [ "$Le_RealKeyPath" = "$NO_VALUE" ]; then
  2736. Le_RealKeyPath=""
  2737. fi
  2738. if [ "$Le_RealCACertPath" = "$NO_VALUE" ]; then
  2739. Le_RealCACertPath=""
  2740. fi
  2741. if [ "$Le_ReloadCmd" = "$NO_VALUE" ]; then
  2742. Le_ReloadCmd=""
  2743. fi
  2744. if [ "$Le_RealFullChainPath" = "$NO_VALUE" ]; then
  2745. Le_RealFullChainPath=""
  2746. fi
  2747. _installed="0"
  2748. if [ "$Le_RealCertPath" ]; then
  2749. _installed=1
  2750. _info "Installing cert to:$Le_RealCertPath"
  2751. if [ -f "$Le_RealCertPath" ] && [ ! "$IS_RENEW" ]; then
  2752. cp "$Le_RealCertPath" "$Le_RealCertPath".bak
  2753. fi
  2754. cat "$CERT_PATH" >"$Le_RealCertPath"
  2755. fi
  2756. if [ "$Le_RealCACertPath" ]; then
  2757. _installed=1
  2758. _info "Installing CA to:$Le_RealCACertPath"
  2759. if [ "$Le_RealCACertPath" = "$Le_RealCertPath" ]; then
  2760. echo "" >>"$Le_RealCACertPath"
  2761. cat "$CA_CERT_PATH" >>"$Le_RealCACertPath"
  2762. else
  2763. if [ -f "$Le_RealCACertPath" ] && [ ! "$IS_RENEW" ]; then
  2764. cp "$Le_RealCACertPath" "$Le_RealCACertPath".bak
  2765. fi
  2766. cat "$CA_CERT_PATH" >"$Le_RealCACertPath"
  2767. fi
  2768. fi
  2769. if [ "$Le_RealKeyPath" ]; then
  2770. _installed=1
  2771. _info "Installing key to:$Le_RealKeyPath"
  2772. if [ -f "$Le_RealKeyPath" ] && [ ! "$IS_RENEW" ]; then
  2773. cp "$Le_RealKeyPath" "$Le_RealKeyPath".bak
  2774. fi
  2775. cat "$CERT_KEY_PATH" >"$Le_RealKeyPath"
  2776. fi
  2777. if [ "$Le_RealFullChainPath" ]; then
  2778. _installed=1
  2779. _info "Installing full chain to:$Le_RealFullChainPath"
  2780. if [ -f "$Le_RealFullChainPath" ] && [ ! "$IS_RENEW" ]; then
  2781. cp "$Le_RealFullChainPath" "$Le_RealFullChainPath".bak
  2782. fi
  2783. cat "$CERT_FULLCHAIN_PATH" >"$Le_RealFullChainPath"
  2784. fi
  2785. if [ "$Le_ReloadCmd" ]; then
  2786. _installed=1
  2787. _info "Run Le_ReloadCmd: $Le_ReloadCmd"
  2788. if (cd "$DOMAIN_PATH" && eval "$Le_ReloadCmd"); then
  2789. _info "$(__green "Reload success")"
  2790. else
  2791. _err "Reload error for :$Le_Domain"
  2792. fi
  2793. fi
  2794. }
  2795. installcronjob() {
  2796. _initpath
  2797. if ! _exists "crontab"; then
  2798. _err "crontab doesn't exist, so, we can not install cron jobs."
  2799. _err "All your certs will not be renewed automatically."
  2800. _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday."
  2801. return 1
  2802. fi
  2803. _info "Installing cron job"
  2804. if ! crontab -l | grep "$PROJECT_ENTRY --cron"; then
  2805. if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ]; then
  2806. lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY"
  2807. else
  2808. _err "Can not install cronjob, $PROJECT_ENTRY not found."
  2809. return 1
  2810. fi
  2811. if _exists uname && uname -a | grep solaris >/dev/null; then
  2812. crontab -l | {
  2813. cat
  2814. echo "0 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
  2815. } | crontab --
  2816. else
  2817. crontab -l | {
  2818. cat
  2819. echo "0 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
  2820. } | crontab -
  2821. fi
  2822. fi
  2823. if [ "$?" != "0" ]; then
  2824. _err "Install cron job failed. You need to manually renew your certs."
  2825. _err "Or you can add cronjob by yourself:"
  2826. _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
  2827. return 1
  2828. fi
  2829. }
  2830. uninstallcronjob() {
  2831. if ! _exists "crontab"; then
  2832. return
  2833. fi
  2834. _info "Removing cron job"
  2835. cr="$(crontab -l | grep "$PROJECT_ENTRY --cron")"
  2836. if [ "$cr" ]; then
  2837. if _exists uname && uname -a | grep solaris >/dev/null; then
  2838. crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab --
  2839. else
  2840. crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab -
  2841. fi
  2842. LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')"
  2843. _info LE_WORKING_DIR "$LE_WORKING_DIR"
  2844. fi
  2845. _initpath
  2846. }
  2847. revoke() {
  2848. Le_Domain="$1"
  2849. if [ -z "$Le_Domain" ]; then
  2850. _usage "Usage: $PROJECT_ENTRY --revoke -d domain.com"
  2851. return 1
  2852. fi
  2853. _isEcc="$2"
  2854. _initpath $Le_Domain "$_isEcc"
  2855. if [ ! -f "$DOMAIN_CONF" ]; then
  2856. _err "$Le_Domain is not a issued domain, skip."
  2857. return 1
  2858. fi
  2859. if [ ! -f "$CERT_PATH" ]; then
  2860. _err "Cert for $Le_Domain $CERT_PATH is not found, skip."
  2861. return 1
  2862. fi
  2863. cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}" | tr -d "\r\n" | _urlencode)"
  2864. if [ -z "$cert" ]; then
  2865. _err "Cert for $Le_Domain is empty found, skip."
  2866. return 1
  2867. fi
  2868. data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}"
  2869. uri="$API/acme/revoke-cert"
  2870. if [ -f "$CERT_KEY_PATH" ]; then
  2871. _info "Try domain key first."
  2872. if _send_signed_request $uri "$data" "" "$CERT_KEY_PATH"; then
  2873. if [ -z "$response" ]; then
  2874. _info "Revoke success."
  2875. rm -f $CERT_PATH
  2876. return 0
  2877. else
  2878. _err "Revoke error by domain key."
  2879. _err "$response"
  2880. fi
  2881. fi
  2882. else
  2883. _info "Domain key file doesn't exists."
  2884. fi
  2885. _info "Try account key."
  2886. if _send_signed_request $uri "$data" "" "$ACCOUNT_KEY_PATH"; then
  2887. if [ -z "$response" ]; then
  2888. _info "Revoke success."
  2889. rm -f $CERT_PATH
  2890. return 0
  2891. else
  2892. _err "Revoke error."
  2893. _debug "$response"
  2894. fi
  2895. fi
  2896. return 1
  2897. }
  2898. #domain vtype
  2899. _deactivate() {
  2900. _d_domain="$1"
  2901. _d_type="$2"
  2902. _initpath
  2903. _d_i=0
  2904. _d_max_retry=9
  2905. while [ "$_d_i" -lt "$_d_max_retry" ]; do
  2906. _info "Deactivate: $_d_domain"
  2907. _d_i="$(_math $_d_i + 1)"
  2908. if ! __get_domain_new_authz "$_d_domain"; then
  2909. _err "Can not get domain new authz token."
  2910. return 1
  2911. fi
  2912. authzUri="$(echo "$responseHeaders" | grep "^Location:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
  2913. _debug "authzUri" "$authzUri"
  2914. if [ ! -z "$code" ] && [ ! "$code" = '201' ]; then
  2915. _err "new-authz error: $response"
  2916. return 1
  2917. fi
  2918. entry="$(printf "%s\n" "$response" | _egrep_o '[^\{]*"status":"valid","uri"[^\}]*')"
  2919. _debug entry "$entry"
  2920. if [ -z "$entry" ]; then
  2921. _info "No more valid entry found."
  2922. break
  2923. fi
  2924. _vtype="$(printf "%s\n" "$entry" | _egrep_o '"type": *"[^"]*"' | cut -d : -f 2 | tr -d '"')"
  2925. _debug _vtype $_vtype
  2926. _info "Found $_vtype"
  2927. uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*' | cut -d : -f 2,3 | tr -d '"')"
  2928. _debug uri $uri
  2929. if [ "$_d_type" ] && [ "$_d_type" != "$_vtype" ]; then
  2930. _info "Skip $_vtype"
  2931. continue
  2932. fi
  2933. _info "Deactivate: $_vtype"
  2934. if ! _send_signed_request "$authzUri" "{\"resource\": \"authz\", \"status\":\"deactivated\"}"; then
  2935. _err "Can not deactivate $_vtype."
  2936. return 1
  2937. fi
  2938. _info "Deactivate: $_vtype success."
  2939. done
  2940. _debug "$_d_i"
  2941. if [ "$_d_i" -lt "$_d_max_retry" ]; then
  2942. _info "Deactivated success!"
  2943. else
  2944. _err "Deactivate failed."
  2945. fi
  2946. }
  2947. deactivate() {
  2948. _d_domain_list="$1"
  2949. _d_type="$2"
  2950. _initpath
  2951. _debug _d_domain_list "$_d_domain_list"
  2952. if [ -z "$(echo $_d_domain_list | cut -d , -f 1)" ]; then
  2953. _usage "Usage: $PROJECT_ENTRY --deactivate -d domain.com [-d domain.com]"
  2954. return 1
  2955. fi
  2956. for _d_dm in $(echo "$_d_domain_list" | tr ',' ' '); do
  2957. if [ -z "$_d_dm" ] || [ "$_d_dm" = "$NO_VALUE" ]; then
  2958. continue
  2959. fi
  2960. if ! _deactivate "$_d_dm" $_d_type; then
  2961. return 1
  2962. fi
  2963. done
  2964. }
  2965. # Detect profile file if not specified as environment variable
  2966. _detect_profile() {
  2967. if [ -n "$PROFILE" -a -f "$PROFILE" ]; then
  2968. echo "$PROFILE"
  2969. return
  2970. fi
  2971. DETECTED_PROFILE=''
  2972. SHELLTYPE="$(basename "/$SHELL")"
  2973. if [ "$SHELLTYPE" = "bash" ]; then
  2974. if [ -f "$HOME/.bashrc" ]; then
  2975. DETECTED_PROFILE="$HOME/.bashrc"
  2976. elif [ -f "$HOME/.bash_profile" ]; then
  2977. DETECTED_PROFILE="$HOME/.bash_profile"
  2978. fi
  2979. elif [ "$SHELLTYPE" = "zsh" ]; then
  2980. DETECTED_PROFILE="$HOME/.zshrc"
  2981. fi
  2982. if [ -z "$DETECTED_PROFILE" ]; then
  2983. if [ -f "$HOME/.profile" ]; then
  2984. DETECTED_PROFILE="$HOME/.profile"
  2985. elif [ -f "$HOME/.bashrc" ]; then
  2986. DETECTED_PROFILE="$HOME/.bashrc"
  2987. elif [ -f "$HOME/.bash_profile" ]; then
  2988. DETECTED_PROFILE="$HOME/.bash_profile"
  2989. elif [ -f "$HOME/.zshrc" ]; then
  2990. DETECTED_PROFILE="$HOME/.zshrc"
  2991. fi
  2992. fi
  2993. if [ ! -z "$DETECTED_PROFILE" ]; then
  2994. echo "$DETECTED_PROFILE"
  2995. fi
  2996. }
  2997. _initconf() {
  2998. _initpath
  2999. if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
  3000. echo "#ACCOUNT_CONF_PATH=xxxx
  3001. #Account configurations:
  3002. #Here are the supported macros, uncomment them to make them take effect.
  3003. #ACCOUNT_EMAIL=aaa@example.com # the account email used to register account.
  3004. #ACCOUNT_KEY_PATH=\"/path/to/account.key\"
  3005. #CERT_HOME=\"/path/to/cert/home\"
  3006. #LOG_FILE=\"$DEFAULT_LOG_FILE\"
  3007. #LOG_LEVEL=1
  3008. #AUTO_UPGRADE=\"1\"
  3009. #STAGE=1 # Use the staging api
  3010. #FORCE=1 # Force to issue cert
  3011. #DEBUG=1 # Debug mode
  3012. #USER_AGENT=\"$USER_AGENT\"
  3013. #USER_PATH=""
  3014. #dns api
  3015. #######################
  3016. #Cloudflare:
  3017. #api key
  3018. #CF_Key=\"sdfsdfsdfljlbjkljlkjsdfoiwje\"
  3019. #account email
  3020. #CF_Email=\"xxxx@sss.com\"
  3021. #######################
  3022. #Dnspod.cn:
  3023. #api key id
  3024. #DP_Id=\"1234\"
  3025. #api key
  3026. #DP_Key=\"sADDsdasdgdsf\"
  3027. #######################
  3028. #Cloudxns.com:
  3029. #CX_Key=\"1234\"
  3030. #
  3031. #CX_Secret=\"sADDsdasdgdsf\"
  3032. #######################
  3033. #Godaddy.com:
  3034. #GD_Key=\"sdfdsgdgdfdasfds\"
  3035. #
  3036. #GD_Secret=\"sADDsdasdfsdfdssdgdsf\"
  3037. #######################
  3038. #PowerDNS:
  3039. #PDNS_Url=\"http://ns.example.com:8081\"
  3040. #PDNS_ServerId=\"localhost\"
  3041. #PDNS_Token=\"0123456789ABCDEF\"
  3042. #PDNS_Ttl=60
  3043. " >"$ACCOUNT_CONF_PATH"
  3044. fi
  3045. }
  3046. # nocron
  3047. _precheck() {
  3048. _nocron="$1"
  3049. if ! _exists "curl" && ! _exists "wget"; then
  3050. _err "Please install curl or wget first, we need to access http resources."
  3051. return 1
  3052. fi
  3053. if [ -z "$_nocron" ]; then
  3054. if ! _exists "crontab"; then
  3055. _err "It is recommended to install crontab first. try to install 'cron, crontab, crontabs or vixie-cron'."
  3056. _err "We need to set cron job to renew the certs automatically."
  3057. _err "Otherwise, your certs will not be able to be renewed automatically."
  3058. if [ -z "$FORCE" ]; then
  3059. _err "Please add '--force' and try install again to go without crontab."
  3060. _err "./$PROJECT_ENTRY --install --force"
  3061. return 1
  3062. fi
  3063. fi
  3064. fi
  3065. if ! _exists "openssl"; then
  3066. _err "Please install openssl first."
  3067. _err "We need openssl to generate keys."
  3068. return 1
  3069. fi
  3070. if ! _exists "nc"; then
  3071. _err "It is recommended to install nc first, try to install 'nc' or 'netcat'."
  3072. _err "We use nc for standalone server if you use standalone mode."
  3073. _err "If you don't use standalone mode, just ignore this warning."
  3074. fi
  3075. return 0
  3076. }
  3077. _setShebang() {
  3078. _file="$1"
  3079. _shebang="$2"
  3080. if [ -z "$_shebang" ]; then
  3081. _usage "Usage: file shebang"
  3082. return 1
  3083. fi
  3084. cp "$_file" "$_file.tmp"
  3085. echo "$_shebang" >"$_file"
  3086. sed -n 2,99999p "$_file.tmp" >>"$_file"
  3087. rm -f "$_file.tmp"
  3088. }
  3089. _installalias() {
  3090. _initpath
  3091. _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env"
  3092. if [ "$_upgrading" ] && [ "$_upgrading" = "1" ]; then
  3093. echo "$(cat "$_envfile")" | sed "s|^LE_WORKING_DIR.*$||" >"$_envfile"
  3094. echo "$(cat "$_envfile")" | sed "s|^alias le.*$||" >"$_envfile"
  3095. echo "$(cat "$_envfile")" | sed "s|^alias le.sh.*$||" >"$_envfile"
  3096. fi
  3097. _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\""
  3098. _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY\""
  3099. _profile="$(_detect_profile)"
  3100. if [ "$_profile" ]; then
  3101. _debug "Found profile: $_profile"
  3102. _info "Installing alias to '$_profile'"
  3103. _setopt "$_profile" ". \"$_envfile\""
  3104. _info "OK, Close and reopen your terminal to start using $PROJECT_NAME"
  3105. else
  3106. _info "No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME"
  3107. fi
  3108. #for csh
  3109. _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh"
  3110. _csh_profile="$HOME/.cshrc"
  3111. if [ -f "$_csh_profile" ]; then
  3112. _info "Installing alias to '$_csh_profile'"
  3113. _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
  3114. _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY\""
  3115. _setopt "$_csh_profile" "source \"$_cshfile\""
  3116. fi
  3117. #for tcsh
  3118. _tcsh_profile="$HOME/.tcshrc"
  3119. if [ -f "$_tcsh_profile" ]; then
  3120. _info "Installing alias to '$_tcsh_profile'"
  3121. _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
  3122. _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY\""
  3123. _setopt "$_tcsh_profile" "source \"$_cshfile\""
  3124. fi
  3125. }
  3126. # nocron
  3127. install() {
  3128. if [ -z "$LE_WORKING_DIR" ]; then
  3129. LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
  3130. fi
  3131. _nocron="$1"
  3132. if ! _initpath; then
  3133. _err "Install failed."
  3134. return 1
  3135. fi
  3136. if [ "$_nocron" ]; then
  3137. _debug "Skip install cron job"
  3138. fi
  3139. if ! _precheck "$_nocron"; then
  3140. _err "Pre-check failed, can not install."
  3141. return 1
  3142. fi
  3143. #convert from le
  3144. if [ -d "$HOME/.le" ]; then
  3145. for envfile in "le.env" "le.sh.env"; do
  3146. if [ -f "$HOME/.le/$envfile" ]; then
  3147. if grep "le.sh" "$HOME/.le/$envfile" >/dev/null; then
  3148. _upgrading="1"
  3149. _info "You are upgrading from le.sh"
  3150. _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR"
  3151. mv "$HOME/.le" "$LE_WORKING_DIR"
  3152. mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env"
  3153. break
  3154. fi
  3155. fi
  3156. done
  3157. fi
  3158. _info "Installing to $LE_WORKING_DIR"
  3159. if ! mkdir -p "$LE_WORKING_DIR"; then
  3160. _err "Can not create working dir: $LE_WORKING_DIR"
  3161. return 1
  3162. fi
  3163. chmod 700 "$LE_WORKING_DIR"
  3164. cp "$PROJECT_ENTRY" "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY"
  3165. if [ "$?" != "0" ]; then
  3166. _err "Install failed, can not copy $PROJECT_ENTRY"
  3167. return 1
  3168. fi
  3169. _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY"
  3170. _installalias
  3171. for subf in $_SUB_FOLDERS; do
  3172. if [ -d "$subf" ]; then
  3173. mkdir -p "$LE_WORKING_DIR/$subf"
  3174. cp "$subf"/* "$LE_WORKING_DIR"/"$subf"/
  3175. fi
  3176. done
  3177. if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
  3178. _initconf
  3179. fi
  3180. if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ]; then
  3181. _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\""
  3182. fi
  3183. if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ]; then
  3184. _saveaccountconf "CERT_HOME" "$CERT_HOME"
  3185. fi
  3186. if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ]; then
  3187. _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH"
  3188. fi
  3189. if [ -z "$_nocron" ]; then
  3190. installcronjob
  3191. fi
  3192. if [ -z "$NO_DETECT_SH" ]; then
  3193. #Modify shebang
  3194. if _exists bash; then
  3195. _info "Good, bash is found, so change the shebang to use bash as prefered."
  3196. _shebang='#!/usr/bin/env bash'
  3197. _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang"
  3198. for subf in $_SUB_FOLDERS; do
  3199. if [ -d "$LE_WORKING_DIR/$subf" ]; then
  3200. for _apifile in "$LE_WORKING_DIR/$subf/"*.sh; do
  3201. _setShebang "$_apifile" "$_shebang"
  3202. done
  3203. fi
  3204. done
  3205. fi
  3206. fi
  3207. _info OK
  3208. }
  3209. # nocron
  3210. uninstall() {
  3211. _nocron="$1"
  3212. if [ -z "$_nocron" ]; then
  3213. uninstallcronjob
  3214. fi
  3215. _initpath
  3216. _uninstallalias
  3217. rm -f "$LE_WORKING_DIR/$PROJECT_ENTRY"
  3218. _info "The keys and certs are in $LE_WORKING_DIR, you can remove them by yourself."
  3219. }
  3220. _uninstallalias() {
  3221. _initpath
  3222. _profile="$(_detect_profile)"
  3223. if [ "$_profile" ]; then
  3224. _info "Uninstalling alias from: '$_profile'"
  3225. text="$(cat "$_profile")"
  3226. echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" >"$_profile"
  3227. fi
  3228. _csh_profile="$HOME/.cshrc"
  3229. if [ -f "$_csh_profile" ]; then
  3230. _info "Uninstalling alias from: '$_csh_profile'"
  3231. text="$(cat "$_csh_profile")"
  3232. echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_csh_profile"
  3233. fi
  3234. _tcsh_profile="$HOME/.tcshrc"
  3235. if [ -f "$_tcsh_profile" ]; then
  3236. _info "Uninstalling alias from: '$_csh_profile'"
  3237. text="$(cat "$_tcsh_profile")"
  3238. echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_tcsh_profile"
  3239. fi
  3240. }
  3241. cron() {
  3242. IN_CRON=1
  3243. _initpath
  3244. if [ "$AUTO_UPGRADE" = "1" ]; then
  3245. export LE_WORKING_DIR
  3246. (
  3247. if ! upgrade; then
  3248. _err "Cron:Upgrade failed!"
  3249. return 1
  3250. fi
  3251. )
  3252. . "$LE_WORKING_DIR/$PROJECT_ENTRY" >/dev/null
  3253. if [ -t 1 ]; then
  3254. __INTERACTIVE="1"
  3255. fi
  3256. _info "Auto upgraded to: $VER"
  3257. fi
  3258. renewAll
  3259. _ret="$?"
  3260. IN_CRON=""
  3261. exit $_ret
  3262. }
  3263. version() {
  3264. echo "$PROJECT"
  3265. echo "v$VER"
  3266. }
  3267. showhelp() {
  3268. _initpath
  3269. version
  3270. echo "Usage: $PROJECT_ENTRY command ...[parameters]....
  3271. Commands:
  3272. --help, -h Show this help message.
  3273. --version, -v Show version info.
  3274. --install Install $PROJECT_NAME to your system.
  3275. --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job.
  3276. --upgrade Upgrade $PROJECT_NAME to the latest code from $PROJECT .
  3277. --issue Issue a cert.
  3278. --signcsr Issue a cert from an existing csr.
  3279. --deploy Deploy the cert to your server.
  3280. --installcert Install the issued cert to apache/nginx or any other server.
  3281. --renew, -r Renew a cert.
  3282. --renewAll Renew all the certs.
  3283. --revoke Revoke a cert.
  3284. --list List all the certs.
  3285. --showcsr Show the content of a csr.
  3286. --installcronjob Install the cron job to renew certs, you don't need to call this. The 'install' command can automatically install the cron job.
  3287. --uninstallcronjob Uninstall the cron job. The 'uninstall' command can do this automatically.
  3288. --cron Run cron job to renew all the certs.
  3289. --toPkcs Export the certificate and key to a pfx file.
  3290. --updateaccount Update account info.
  3291. --registeraccount Register account key.
  3292. --createAccountKey, -cak Create an account private key, professional use.
  3293. --createDomainKey, -cdk Create an domain private key, professional use.
  3294. --createCSR, -ccsr Create CSR , professional use.
  3295. --deactivate Deactivate the domain authz, professional use.
  3296. Parameters:
  3297. --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc.
  3298. --force, -f Used to force to install or force to renew a cert immediately.
  3299. --staging, --test Use staging server, just for test.
  3300. --debug Output debug info.
  3301. --webroot, -w /path/to/webroot Specifies the web root folder for web root mode.
  3302. --standalone Use standalone mode.
  3303. --tls Use standalone tls mode.
  3304. --apache Use apache mode.
  3305. --dns [dns_cf|dns_dp|dns_cx|/path/to/api/file] Use dns mode or dns api.
  3306. --dnssleep [$DEFAULT_DNS_SLEEP] The time in seconds to wait for all the txt records to take effect in dns api mode. Default $DEFAULT_DNS_SLEEP seconds.
  3307. --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384.
  3308. --accountkeylength, -ak [2048] Specifies the account key length.
  3309. --log [/path/to/logfile] Specifies the log file. The default is: \"$DEFAULT_LOG_FILE\" if you don't give a file path here.
  3310. --log-level 1|2 Specifies the log level, default is 1.
  3311. These parameters are to install the cert to nginx/apache or anyother server after issue/renew a cert:
  3312. --certpath /path/to/real/cert/file After issue/renew, the cert will be copied to this path.
  3313. --keypath /path/to/real/key/file After issue/renew, the key will be copied to this path.
  3314. --capath /path/to/real/ca/file After issue/renew, the intermediate cert will be copied to this path.
  3315. --fullchainpath /path/to/fullchain/file After issue/renew, the fullchain cert will be copied to this path.
  3316. --reloadcmd \"service nginx reload\" After issue/renew, it's used to reload the server.
  3317. --accountconf Specifies a customized account config file.
  3318. --home Specifies the home dir for $PROJECT_NAME .
  3319. --certhome Specifies the home dir to save all the certs, only valid for '--install' command.
  3320. --useragent Specifies the user agent string. it will be saved for future use too.
  3321. --accountemail Specifies the account email for registering, Only valid for the '--install' command.
  3322. --accountkey Specifies the account key path, Only valid for the '--install' command.
  3323. --days Specifies the days to renew the cert when using '--issue' command. The max value is $MAX_RENEW days.
  3324. --httpport Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer.
  3325. --tlsport Specifies the standalone tls listening port. Only valid if the server is behind a reverse proxy or load balancer.
  3326. --local-address Specifies the standalone/tls server listening address, in case you have multiple ip addresses.
  3327. --listraw Only used for '--list' command, list the certs in raw format.
  3328. --stopRenewOnError, -se Only valid for '--renewall' command. Stop if one cert has error in renewal.
  3329. --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted.
  3330. --ca-bundle Specifices the path to the CA certificate bundle to verify api server's certificate.
  3331. --nocron Only valid for '--install' command, which means: do not install the default cron job. In this case, the certs will not be renewed automatically.
  3332. --ecc Specifies to use the ECC cert. Valid for '--installcert', '--renew', '--revoke', '--toPkcs' and '--createCSR'
  3333. --csr Specifies the input csr.
  3334. --pre-hook Command to be run before obtaining any certificates.
  3335. --post-hook Command to be run after attempting to obtain/renew certificates. No matter the obain/renew is success or failed.
  3336. --renew-hook Command to be run once for each successfully renewed certificate.
  3337. --deploy-hook The hook file to deploy cert
  3338. --ocsp-must-staple, --ocsp Generate ocsp must Staple extension.
  3339. --auto-upgrade [0|1] Valid for '--upgrade' command, indicating whether to upgrade automatically in future.
  3340. --listen-v4 Force standalone/tls server to listen at ipv4.
  3341. --listen-v6 Force standalone/tls server to listen at ipv6.
  3342. "
  3343. }
  3344. # nocron
  3345. _installOnline() {
  3346. _info "Installing from online archive."
  3347. _nocron="$1"
  3348. if [ ! "$BRANCH" ]; then
  3349. BRANCH="master"
  3350. fi
  3351. target="$PROJECT/archive/$BRANCH.tar.gz"
  3352. _info "Downloading $target"
  3353. localname="$BRANCH.tar.gz"
  3354. if ! _get "$target" >$localname; then
  3355. _err "Download error."
  3356. return 1
  3357. fi
  3358. (
  3359. _info "Extracting $localname"
  3360. tar xzf $localname
  3361. cd "$PROJECT_NAME-$BRANCH"
  3362. chmod +x $PROJECT_ENTRY
  3363. if ./$PROJECT_ENTRY install "$_nocron"; then
  3364. _info "Install success!"
  3365. fi
  3366. cd ..
  3367. rm -rf "$PROJECT_NAME-$BRANCH"
  3368. rm -f "$localname"
  3369. )
  3370. }
  3371. upgrade() {
  3372. if (
  3373. _initpath
  3374. export LE_WORKING_DIR
  3375. cd "$LE_WORKING_DIR"
  3376. _installOnline "nocron"
  3377. ); then
  3378. _info "Upgrade success!"
  3379. exit 0
  3380. else
  3381. _err "Upgrade failed!"
  3382. exit 1
  3383. fi
  3384. }
  3385. _processAccountConf() {
  3386. if [ "$_useragent" ]; then
  3387. _saveaccountconf "USER_AGENT" "$_useragent"
  3388. elif [ "$USER_AGENT" ] && [ "$USER_AGENT" != "$DEFAULT_USER_AGENT" ]; then
  3389. _saveaccountconf "USER_AGENT" "$USER_AGENT"
  3390. fi
  3391. if [ "$_accountemail" ]; then
  3392. _saveaccountconf "ACCOUNT_EMAIL" "$_accountemail"
  3393. elif [ "$ACCOUNT_EMAIL" ] && [ "$ACCOUNT_EMAIL" != "$DEFAULT_ACCOUNT_EMAIL" ]; then
  3394. _saveaccountconf "ACCOUNT_EMAIL" "$ACCOUNT_EMAIL"
  3395. fi
  3396. if [ "$_auto_upgrade" ]; then
  3397. _saveaccountconf "AUTO_UPGRADE" "$_auto_upgrade"
  3398. elif [ "$AUTO_UPGRADE" ]; then
  3399. _saveaccountconf "AUTO_UPGRADE" "$AUTO_UPGRADE"
  3400. fi
  3401. }
  3402. _process() {
  3403. _CMD=""
  3404. _domain=""
  3405. _altdomains="$NO_VALUE"
  3406. _webroot=""
  3407. _keylength=""
  3408. _accountkeylength=""
  3409. _certpath=""
  3410. _keypath=""
  3411. _capath=""
  3412. _fullchainpath=""
  3413. _reloadcmd=""
  3414. _password=""
  3415. _accountconf=""
  3416. _useragent=""
  3417. _accountemail=""
  3418. _accountkey=""
  3419. _certhome=""
  3420. _httpport=""
  3421. _tlsport=""
  3422. _dnssleep=""
  3423. _listraw=""
  3424. _stopRenewOnError=""
  3425. _insecure=""
  3426. _ca_bundle=""
  3427. _nocron=""
  3428. _ecc=""
  3429. _csr=""
  3430. _pre_hook=""
  3431. _post_hook=""
  3432. _renew_hook=""
  3433. _deploy_hook=""
  3434. _logfile=""
  3435. _log=""
  3436. _local_address=""
  3437. _log_level=""
  3438. _auto_upgrade=""
  3439. _listen_v4=""
  3440. _listen_v6=""
  3441. while [ ${#} -gt 0 ]; do
  3442. case "${1}" in
  3443. --help | -h)
  3444. showhelp
  3445. return
  3446. ;;
  3447. --version | -v)
  3448. version
  3449. return
  3450. ;;
  3451. --install)
  3452. _CMD="install"
  3453. ;;
  3454. --uninstall)
  3455. _CMD="uninstall"
  3456. ;;
  3457. --upgrade)
  3458. _CMD="upgrade"
  3459. ;;
  3460. --issue)
  3461. _CMD="issue"
  3462. ;;
  3463. --deploy)
  3464. _CMD="deploy"
  3465. ;;
  3466. --signcsr)
  3467. _CMD="signcsr"
  3468. ;;
  3469. --showcsr)
  3470. _CMD="showcsr"
  3471. ;;
  3472. --installcert | -i)
  3473. _CMD="installcert"
  3474. ;;
  3475. --renew | -r)
  3476. _CMD="renew"
  3477. ;;
  3478. --renewAll | --renewall)
  3479. _CMD="renewAll"
  3480. ;;
  3481. --revoke)
  3482. _CMD="revoke"
  3483. ;;
  3484. --list)
  3485. _CMD="list"
  3486. ;;
  3487. --installcronjob)
  3488. _CMD="installcronjob"
  3489. ;;
  3490. --uninstallcronjob)
  3491. _CMD="uninstallcronjob"
  3492. ;;
  3493. --cron)
  3494. _CMD="cron"
  3495. ;;
  3496. --toPkcs)
  3497. _CMD="toPkcs"
  3498. ;;
  3499. --createAccountKey | --createaccountkey | -cak)
  3500. _CMD="createAccountKey"
  3501. ;;
  3502. --createDomainKey | --createdomainkey | -cdk)
  3503. _CMD="createDomainKey"
  3504. ;;
  3505. --createCSR | --createcsr | -ccr)
  3506. _CMD="createCSR"
  3507. ;;
  3508. --deactivate)
  3509. _CMD="deactivate"
  3510. ;;
  3511. --updateaccount)
  3512. _CMD="updateaccount"
  3513. ;;
  3514. --registeraccount)
  3515. _CMD="registeraccount"
  3516. ;;
  3517. --domain | -d)
  3518. _dvalue="$2"
  3519. if [ "$_dvalue" ]; then
  3520. if _startswith "$_dvalue" "-"; then
  3521. _err "'$_dvalue' is not a valid domain for parameter '$1'"
  3522. return 1
  3523. fi
  3524. if _is_idn "$_dvalue" && ! _exists idn; then
  3525. _err "It seems that $_dvalue is an IDN( Internationalized Domain Names), please install 'idn' command first."
  3526. return 1
  3527. fi
  3528. if [ -z "$_domain" ]; then
  3529. _domain="$_dvalue"
  3530. else
  3531. if [ "$_altdomains" = "$NO_VALUE" ]; then
  3532. _altdomains="$_dvalue"
  3533. else
  3534. _altdomains="$_altdomains,$_dvalue"
  3535. fi
  3536. fi
  3537. fi
  3538. shift
  3539. ;;
  3540. --force | -f)
  3541. FORCE="1"
  3542. ;;
  3543. --staging | --test)
  3544. STAGE="1"
  3545. ;;
  3546. --debug)
  3547. if [ -z "$2" ] || _startswith "$2" "-"; then
  3548. DEBUG="1"
  3549. else
  3550. DEBUG="$2"
  3551. shift
  3552. fi
  3553. ;;
  3554. --webroot | -w)
  3555. wvalue="$2"
  3556. if [ -z "$_webroot" ]; then
  3557. _webroot="$wvalue"
  3558. else
  3559. _webroot="$_webroot,$wvalue"
  3560. fi
  3561. shift
  3562. ;;
  3563. --standalone)
  3564. wvalue="$NO_VALUE"
  3565. if [ -z "$_webroot" ]; then
  3566. _webroot="$wvalue"
  3567. else
  3568. _webroot="$_webroot,$wvalue"
  3569. fi
  3570. ;;
  3571. --local-address)
  3572. lvalue="$2"
  3573. _local_address="$_local_address$lvalue,"
  3574. shift
  3575. ;;
  3576. --apache)
  3577. wvalue="apache"
  3578. if [ -z "$_webroot" ]; then
  3579. _webroot="$wvalue"
  3580. else
  3581. _webroot="$_webroot,$wvalue"
  3582. fi
  3583. ;;
  3584. --tls)
  3585. wvalue="$W_TLS"
  3586. if [ -z "$_webroot" ]; then
  3587. _webroot="$wvalue"
  3588. else
  3589. _webroot="$_webroot,$wvalue"
  3590. fi
  3591. ;;
  3592. --dns)
  3593. wvalue="dns"
  3594. if ! _startswith "$2" "-"; then
  3595. wvalue="$2"
  3596. shift
  3597. fi
  3598. if [ -z "$_webroot" ]; then
  3599. _webroot="$wvalue"
  3600. else
  3601. _webroot="$_webroot,$wvalue"
  3602. fi
  3603. ;;
  3604. --dnssleep)
  3605. _dnssleep="$2"
  3606. Le_DNSSleep="$_dnssleep"
  3607. shift
  3608. ;;
  3609. --keylength | -k)
  3610. _keylength="$2"
  3611. shift
  3612. ;;
  3613. --accountkeylength | -ak)
  3614. _accountkeylength="$2"
  3615. shift
  3616. ;;
  3617. --certpath)
  3618. _certpath="$2"
  3619. shift
  3620. ;;
  3621. --keypath)
  3622. _keypath="$2"
  3623. shift
  3624. ;;
  3625. --capath)
  3626. _capath="$2"
  3627. shift
  3628. ;;
  3629. --fullchainpath)
  3630. _fullchainpath="$2"
  3631. shift
  3632. ;;
  3633. --reloadcmd | --reloadCmd)
  3634. _reloadcmd="$2"
  3635. shift
  3636. ;;
  3637. --password)
  3638. _password="$2"
  3639. shift
  3640. ;;
  3641. --accountconf)
  3642. _accountconf="$2"
  3643. ACCOUNT_CONF_PATH="$_accountconf"
  3644. shift
  3645. ;;
  3646. --home)
  3647. LE_WORKING_DIR="$2"
  3648. shift
  3649. ;;
  3650. --certhome)
  3651. _certhome="$2"
  3652. CERT_HOME="$_certhome"
  3653. shift
  3654. ;;
  3655. --useragent)
  3656. _useragent="$2"
  3657. USER_AGENT="$_useragent"
  3658. shift
  3659. ;;
  3660. --accountemail)
  3661. _accountemail="$2"
  3662. ACCOUNT_EMAIL="$_accountemail"
  3663. shift
  3664. ;;
  3665. --accountkey)
  3666. _accountkey="$2"
  3667. ACCOUNT_KEY_PATH="$_accountkey"
  3668. shift
  3669. ;;
  3670. --days)
  3671. _days="$2"
  3672. Le_RenewalDays="$_days"
  3673. shift
  3674. ;;
  3675. --httpport)
  3676. _httpport="$2"
  3677. Le_HTTPPort="$_httpport"
  3678. shift
  3679. ;;
  3680. --tlsport)
  3681. _tlsport="$2"
  3682. Le_TLSPort="$_tlsport"
  3683. shift
  3684. ;;
  3685. --listraw)
  3686. _listraw="raw"
  3687. ;;
  3688. --stopRenewOnError | --stoprenewonerror | -se)
  3689. _stopRenewOnError="1"
  3690. ;;
  3691. --insecure)
  3692. _insecure="1"
  3693. HTTPS_INSECURE="1"
  3694. ;;
  3695. --ca-bundle)
  3696. _ca_bundle="$(readlink -f "$2")"
  3697. CA_BUNDLE="$_ca_bundle"
  3698. shift
  3699. ;;
  3700. --nocron)
  3701. _nocron="1"
  3702. ;;
  3703. --ecc)
  3704. _ecc="isEcc"
  3705. ;;
  3706. --csr)
  3707. _csr="$2"
  3708. shift
  3709. ;;
  3710. --pre-hook)
  3711. _pre_hook="$2"
  3712. shift
  3713. ;;
  3714. --post-hook)
  3715. _post_hook="$2"
  3716. shift
  3717. ;;
  3718. --renew-hook)
  3719. _renew_hook="$2"
  3720. shift
  3721. ;;
  3722. --deploy-hook)
  3723. _deploy_hook="$2"
  3724. shift
  3725. ;;
  3726. --ocsp-must-staple | --ocsp)
  3727. Le_OCSP_Stable="1"
  3728. ;;
  3729. --log | --logfile)
  3730. _log="1"
  3731. _logfile="$2"
  3732. if _startswith "$_logfile" '-'; then
  3733. _logfile=""
  3734. else
  3735. shift
  3736. fi
  3737. LOG_FILE="$_logfile"
  3738. if [ -z "$LOG_LEVEL" ]; then
  3739. LOG_LEVEL="$DEFAULT_LOG_LEVEL"
  3740. fi
  3741. ;;
  3742. --log-level)
  3743. _log_level="$2"
  3744. LOG_LEVEL="$_log_level"
  3745. shift
  3746. ;;
  3747. --auto-upgrade)
  3748. _auto_upgrade="$2"
  3749. if [ -z "$_auto_upgrade" ] || _startswith "$_auto_upgrade" '-'; then
  3750. _auto_upgrade="1"
  3751. else
  3752. shift
  3753. fi
  3754. AUTO_UPGRADE="$_auto_upgrade"
  3755. ;;
  3756. --listen-v4)
  3757. _listen_v4="1"
  3758. Le_Listen_V4="$_listen_v4"
  3759. ;;
  3760. --listen-v6)
  3761. _listen_v6="1"
  3762. Le_Listen_V6="$_listen_v6"
  3763. ;;
  3764. *)
  3765. _err "Unknown parameter : $1"
  3766. return 1
  3767. ;;
  3768. esac
  3769. shift 1
  3770. done
  3771. if [ "${_CMD}" != "install" ]; then
  3772. __initHome
  3773. if [ "$_log" ]; then
  3774. if [ -z "$_logfile" ]; then
  3775. _logfile="$DEFAULT_LOG_FILE"
  3776. fi
  3777. fi
  3778. if [ "$_logfile" ]; then
  3779. _saveaccountconf "LOG_FILE" "$_logfile"
  3780. LOG_FILE="$_logfile"
  3781. fi
  3782. if [ "$_log_level" ]; then
  3783. _saveaccountconf "LOG_LEVEL" "$_log_level"
  3784. LOG_LEVEL="$_log_level"
  3785. fi
  3786. _processAccountConf
  3787. fi
  3788. _debug2 LE_WORKING_DIR "$LE_WORKING_DIR"
  3789. if [ "$DEBUG" ]; then
  3790. version
  3791. fi
  3792. case "${_CMD}" in
  3793. install) install "$_nocron" ;;
  3794. uninstall) uninstall "$_nocron" ;;
  3795. upgrade) upgrade ;;
  3796. issue)
  3797. issue "$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address"
  3798. ;;
  3799. deploy)
  3800. deploy "$_domain" "$_deploy_hook" "$_ecc"
  3801. ;;
  3802. signcsr)
  3803. signcsr "$_csr" "$_webroot"
  3804. ;;
  3805. showcsr)
  3806. showcsr "$_csr" "$_domain"
  3807. ;;
  3808. installcert)
  3809. installcert "$_domain" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath" "$_ecc"
  3810. ;;
  3811. renew)
  3812. renew "$_domain" "$_ecc"
  3813. ;;
  3814. renewAll)
  3815. renewAll "$_stopRenewOnError"
  3816. ;;
  3817. revoke)
  3818. revoke "$_domain" "$_ecc"
  3819. ;;
  3820. deactivate)
  3821. deactivate "$_domain,$_altdomains"
  3822. ;;
  3823. registeraccount)
  3824. registeraccount "$_accountkeylength"
  3825. ;;
  3826. updateaccount)
  3827. updateaccount
  3828. ;;
  3829. list)
  3830. list "$_listraw"
  3831. ;;
  3832. installcronjob) installcronjob ;;
  3833. uninstallcronjob) uninstallcronjob ;;
  3834. cron) cron ;;
  3835. toPkcs)
  3836. toPkcs "$_domain" "$_password" "$_ecc"
  3837. ;;
  3838. createAccountKey)
  3839. createAccountKey "$_accountkeylength"
  3840. ;;
  3841. createDomainKey)
  3842. createDomainKey "$_domain" "$_keylength"
  3843. ;;
  3844. createCSR)
  3845. createCSR "$_domain" "$_altdomains" "$_ecc"
  3846. ;;
  3847. *)
  3848. _err "Invalid command: $_CMD"
  3849. showhelp
  3850. return 1
  3851. ;;
  3852. esac
  3853. _ret="$?"
  3854. if [ "$_ret" != "0" ]; then
  3855. return $_ret
  3856. fi
  3857. if [ "${_CMD}" = "install" ]; then
  3858. if [ "$_log" ]; then
  3859. if [ -z "$LOG_FILE" ]; then
  3860. LOG_FILE="$DEFAULT_LOG_FILE"
  3861. fi
  3862. _saveaccountconf "LOG_FILE" "$LOG_FILE"
  3863. fi
  3864. if [ "$_log_level" ]; then
  3865. _saveaccountconf "LOG_LEVEL" "$_log_level"
  3866. fi
  3867. _processAccountConf
  3868. fi
  3869. }
  3870. if [ "$INSTALLONLINE" ]; then
  3871. INSTALLONLINE=""
  3872. _installOnline $BRANCH
  3873. exit
  3874. fi
  3875. main() {
  3876. [ -z "$1" ] && showhelp && return
  3877. if _startswith "$1" '-'; then _process "$@"; else "$@"; fi
  3878. }
  3879. main "$@"