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.

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