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.

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