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.

4468 lines
110 KiB

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