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.

4529 lines
111 KiB

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