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.

2137 lines
53 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. #!/usr/bin/env bash
  2. VER=2.0.0
  3. PROJECT="https://github.com/Neilpang/le"
  4. DEFAULT_CA="https://acme-v01.api.letsencrypt.org"
  5. DEFAULT_AGREEMENT="https://letsencrypt.org/documents/LE-SA-v1.0.1-July-27-2015.pdf"
  6. DEFAULT_USER_AGENT="le.sh client: $PROJECT"
  7. STAGE_CA="https://acme-staging.api.letsencrypt.org"
  8. VTYPE_HTTP="http-01"
  9. VTYPE_DNS="dns-01"
  10. BEGIN_CSR="-----BEGIN CERTIFICATE REQUEST-----"
  11. END_CSR="-----END CERTIFICATE REQUEST-----"
  12. BEGIN_CERT="-----BEGIN CERTIFICATE-----"
  13. END_CERT="-----END CERTIFICATE-----"
  14. if [[ -z "$AGREEMENT" ]] ; then
  15. AGREEMENT="$DEFAULT_AGREEMENT"
  16. fi
  17. _info() {
  18. if [[ -z "$2" ]] ; then
  19. echo "[$(date)] $1"
  20. else
  21. echo "[$(date)] $1"="'$2'"
  22. fi
  23. }
  24. _err() {
  25. _info "$@" >&2
  26. return 1
  27. }
  28. _debug() {
  29. if [[ -z "$DEBUG" ]] ; then
  30. return
  31. fi
  32. _err "$@"
  33. return 0
  34. }
  35. _debug2() {
  36. if [[ "$DEBUG" -ge "2" ]] ; then
  37. _debug "$@"
  38. fi
  39. return
  40. }
  41. _exists() {
  42. cmd="$1"
  43. if [[ -z "$cmd" ]] ; then
  44. _err "Usage: _exists cmd"
  45. return 1
  46. fi
  47. command -v $cmd >/dev/null 2>&1
  48. ret="$?"
  49. _debug2 "$cmd exists=$ret"
  50. return $ret
  51. }
  52. _h2b() {
  53. hex=$(cat)
  54. i=1
  55. j=2
  56. while [ '1' ] ; do
  57. h=$(printf $hex | cut -c $i-$j)
  58. if [[ -z "$h" ]] ; then
  59. break;
  60. fi
  61. printf "\x$h"
  62. let "i+=2"
  63. let "j+=2"
  64. done
  65. }
  66. #options file
  67. _sed_i() {
  68. options="$1"
  69. filename="$2"
  70. if [[ -z "$filename" ]] ; then
  71. _err "Usage:_sed_i options filename"
  72. return 1
  73. fi
  74. if sed -h 2>&1 | grep "\-i[SUFFIX]" ; then
  75. _debug "Using sed -i"
  76. sed -i ""
  77. else
  78. _debug "No -i support in sed"
  79. text="$(cat $filename)"
  80. echo "$text" | sed "$options" > "$filename"
  81. fi
  82. }
  83. #Usage: file startline endline
  84. _getfile() {
  85. filename="$1"
  86. startline="$2"
  87. endline="$3"
  88. if [[ -z "$endline" ]] ; then
  89. _err "Usage: file startline endline"
  90. return 1
  91. fi
  92. i="$(grep -n -- "$startline" $filename | cut -d : -f 1)"
  93. if [[ -z "$i" ]] ; then
  94. _err "Can not find start line: $startline"
  95. return 1
  96. fi
  97. let "i+=1"
  98. _debug i $i
  99. j="$(grep -n -- "$endline" $filename | cut -d : -f 1)"
  100. if [[ -z "$j" ]] ; then
  101. _err "Can not find end line: $endline"
  102. return 1
  103. fi
  104. let "j-=1"
  105. _debug j $j
  106. sed -n $i,${j}p "$filename"
  107. }
  108. #Usage: multiline
  109. _base64() {
  110. if [[ "$1" ]] ; then
  111. openssl base64 -e
  112. else
  113. openssl base64 -e | tr -d '\r\n'
  114. fi
  115. }
  116. #Usage: multiline
  117. _dbase64() {
  118. if [[ "$1" ]] ; then
  119. openssl base64 -d -A
  120. else
  121. openssl base64 -d
  122. fi
  123. }
  124. #Usage: hashalg
  125. #Output Base64-encoded digest
  126. _digest() {
  127. alg="$1"
  128. if [[ -z "$alg" ]] ; then
  129. _err "Usage: _digest hashalg"
  130. return 1
  131. fi
  132. if [[ "$alg" == "sha256" ]] ; then
  133. openssl dgst -sha256 -binary | _base64
  134. else
  135. _err "$alg is not supported yet"
  136. return 1
  137. fi
  138. }
  139. #Usage: keyfile hashalg
  140. #Output: Base64-encoded signature value
  141. _sign() {
  142. keyfile="$1"
  143. alg="$2"
  144. if [[ -z "$alg" ]] ; then
  145. _err "Usage: _sign keyfile hashalg"
  146. return 1
  147. fi
  148. if [[ "$alg" == "sha256" ]] ; then
  149. openssl dgst -sha256 -sign "$keyfile" | _base64
  150. else
  151. _err "$alg is not supported yet"
  152. return 1
  153. fi
  154. }
  155. _ss() {
  156. _port="$1"
  157. if _exists "ss" ; then
  158. _debug "Using: ss"
  159. ss -ntpl | grep :$_port" "
  160. return 0
  161. fi
  162. if _exists "netstat" ; then
  163. _debug "Using: netstat"
  164. if netstat -h 2>&1 | grep "\-p proto" >/dev/null ; then
  165. #for windows version netstat tool
  166. netstat -anb -p tcp | grep "LISTENING" | grep :$_port" "
  167. else
  168. if netstat -help 2>&1 | grep "\-p protocol" >/dev/null ; then
  169. netstat -an -p tcp | grep LISTEN | grep :$_port" "
  170. else
  171. netstat -ntpl | grep :$_port" "
  172. fi
  173. fi
  174. return 0
  175. fi
  176. return 1
  177. }
  178. toPkcs() {
  179. domain="$1"
  180. pfxPassword="$2"
  181. if [[ -z "$domain" ]] ; then
  182. echo "Usage: le.sh --toPkcs -d domain [--password pfx-password]"
  183. return 1
  184. fi
  185. _initpath "$domain"
  186. if [[ "$pfxPassword" ]] ; then
  187. openssl pkcs12 -export -out "$CERT_PFX_PATH" -inkey "$CERT_KEY_PATH" -in "$CERT_PATH" -certfile "$CA_CERT_PATH" -password "pass:$pfxPassword"
  188. else
  189. openssl pkcs12 -export -out "$CERT_PFX_PATH" -inkey "$CERT_KEY_PATH" -in "$CERT_PATH" -certfile "$CA_CERT_PATH"
  190. fi
  191. if [[ "$?" == "0" ]] ; then
  192. _info "Success, Pfx is exported to: $CERT_PFX_PATH"
  193. fi
  194. }
  195. #domain [2048]
  196. createAccountKey() {
  197. _info "Creating account key"
  198. if [[ -z "$1" ]] ; then
  199. echo Usage: le.sh --createAccountKey -d domain.com [--accountkeylength 2048]
  200. return
  201. fi
  202. account=$1
  203. length=$2
  204. if [[ "$length" == "ec-"* ]] ; then
  205. length=2048
  206. fi
  207. if [[ -z "$2" ]] ; then
  208. _info "Use default length 2048"
  209. length=2048
  210. fi
  211. _initpath
  212. if [[ -f "$ACCOUNT_KEY_PATH" ]] ; then
  213. _info "Account key exists, skip"
  214. return
  215. else
  216. #generate account key
  217. openssl genrsa $length 2>/dev/null > "$ACCOUNT_KEY_PATH"
  218. fi
  219. }
  220. #domain length
  221. createDomainKey() {
  222. _info "Creating domain key"
  223. if [[ -z "$1" ]] ; then
  224. echo Usage: le.sh --createDomainKey -d domain.com [ --keylength 2048 ]
  225. return
  226. fi
  227. domain=$1
  228. length=$2
  229. isec=""
  230. if [[ "$length" == "ec-"* ]] ; then
  231. isec="1"
  232. length=$(printf $length | cut -d '-' -f 2-100)
  233. eccname="$length"
  234. fi
  235. if [[ -z "$length" ]] ; then
  236. if [[ "$isec" ]] ; then
  237. length=256
  238. else
  239. length=2048
  240. fi
  241. fi
  242. _info "Use length $length"
  243. if [[ "$isec" ]] ; then
  244. if [[ "$length" == "256" ]] ; then
  245. eccname="prime256v1"
  246. fi
  247. if [[ "$length" == "384" ]] ; then
  248. eccname="secp384r1"
  249. fi
  250. if [[ "$length" == "521" ]] ; then
  251. eccname="secp521r1"
  252. fi
  253. _info "Using ec name: $eccname"
  254. fi
  255. _initpath $domain
  256. if [[ ! -f "$CERT_KEY_PATH" ]] || ( [[ "$FORCE" ]] && ! [[ "$IS_RENEW" ]] ); then
  257. #generate account key
  258. if [[ "$isec" ]] ; then
  259. openssl ecparam -name $eccname -genkey 2>/dev/null > "$CERT_KEY_PATH"
  260. else
  261. openssl genrsa $length 2>/dev/null > "$CERT_KEY_PATH"
  262. fi
  263. else
  264. if [[ "$IS_RENEW" ]] ; then
  265. _info "Domain key exists, skip"
  266. return 0
  267. else
  268. _err "Domain key exists, do you want to overwrite the key?"
  269. _err "Set FORCE=1, and try again."
  270. return 1
  271. fi
  272. fi
  273. }
  274. # domain domainlist
  275. createCSR() {
  276. _info "Creating csr"
  277. if [[ -z "$1" ]] ; then
  278. echo Usage: le.sh --createCSR -d domain1.com [-d domain2.com -d domain3.com ... ]
  279. return
  280. fi
  281. domain=$1
  282. _initpath $domain
  283. domainlist=$2
  284. if [[ -f "$CSR_PATH" ]] && [[ "$IS_RENEW" ]] && [[ -z "$FORCE" ]]; then
  285. _info "CSR exists, skip"
  286. return
  287. fi
  288. if [[ -z "$domainlist" ]] || [[ "$domainlist" == "no" ]]; then
  289. #single domain
  290. _info "Single domain" $domain
  291. printf "[ req_distinguished_name ]\n[ req ]\ndistinguished_name = req_distinguished_name\n" > "$DOMAIN_SSL_CONF"
  292. openssl req -new -sha256 -key "$CERT_KEY_PATH" -subj "/CN=$domain" -config "$DOMAIN_SSL_CONF" -out "$CSR_PATH"
  293. else
  294. alt="DNS:$(echo $domainlist | sed "s/,/,DNS:/g")"
  295. #multi
  296. _info "Multi domain" "$alt"
  297. printf "[ req_distinguished_name ]\n[ req ]\ndistinguished_name = req_distinguished_name\n[SAN]\nsubjectAltName=$alt" > "$DOMAIN_SSL_CONF"
  298. openssl req -new -sha256 -key "$CERT_KEY_PATH" -subj "/CN=$domain" -reqexts SAN -config "$DOMAIN_SSL_CONF" -out "$CSR_PATH"
  299. fi
  300. }
  301. _urlencode() {
  302. __n=$(cat)
  303. echo $__n | tr '/+' '_-' | tr -d '= '
  304. }
  305. _time2str() {
  306. #BSD
  307. if date -u -d@$1 2>/dev/null ; then
  308. return
  309. fi
  310. #Linux
  311. if date -u -r $1 2>/dev/null ; then
  312. return
  313. fi
  314. }
  315. _stat() {
  316. #Linux
  317. if stat -c '%U:%G' "$1" 2>/dev/null ; then
  318. return
  319. fi
  320. #BSD
  321. if stat -f '%Su:%Sg' "$1" 2>/dev/null ; then
  322. return
  323. fi
  324. }
  325. #keyfile
  326. _calcjwk() {
  327. keyfile="$1"
  328. if [[ -z "$keyfile" ]] ; then
  329. _err "Usage: _calcjwk keyfile"
  330. return 1
  331. fi
  332. EC_SIGN=""
  333. if grep "BEGIN RSA PRIVATE KEY" "$keyfile" > /dev/null 2>&1 ; then
  334. _debug "RSA key"
  335. pub_exp=$(openssl rsa -in $keyfile -noout -text | grep "^publicExponent:"| cut -d '(' -f 2 | cut -d 'x' -f 2 | cut -d ')' -f 1)
  336. if [[ "${#pub_exp}" == "5" ]] ; then
  337. pub_exp=0$pub_exp
  338. fi
  339. _debug2 pub_exp "$pub_exp"
  340. e=$(echo $pub_exp | _h2b | _base64)
  341. _debug2 e "$e"
  342. modulus=$(openssl rsa -in $keyfile -modulus -noout | cut -d '=' -f 2 )
  343. n=$(echo $modulus| _h2b | _base64 | _urlencode )
  344. jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}'
  345. _debug2 jwk "$jwk"
  346. HEADER='{"alg": "RS256", "jwk": '$jwk'}'
  347. HEADERPLACE='{"nonce": "NONCE", "alg": "RS256", "jwk": '$jwk'}'
  348. elif grep "BEGIN EC PRIVATE KEY" "$keyfile" > /dev/null 2>&1 ; then
  349. _debug "EC key"
  350. EC_SIGN="1"
  351. crv="$(openssl ec -in $keyfile -noout -text 2>/dev/null | grep "^NIST CURVE:" | cut -d ":" -f 2 | tr -d " \r\n")"
  352. _debug2 crv $crv
  353. pubi="$(openssl ec -in $keyfile -noout -text 2>/dev/null | grep -n pub: | cut -d : -f 1)"
  354. _debug2 pubi $pubi
  355. let "pubi=pubi+1"
  356. pubj="$(openssl ec -in $keyfile -noout -text 2>/dev/null | grep -n "ASN1 OID:" | cut -d : -f 1)"
  357. _debug2 pubj $pubj
  358. let "pubj=pubj-1"
  359. pubtext="$(openssl ec -in $keyfile -noout -text 2>/dev/null | sed -n "$pubi,${pubj}p" | tr -d " \n\r")"
  360. _debug2 pubtext "$pubtext"
  361. xlen="$(printf "$pubtext" | tr -d ':' | wc -c)"
  362. let "xlen=xlen/4"
  363. _debug2 xlen $xlen
  364. let "xend=xlen+1"
  365. x="$(printf $pubtext | cut -d : -f 2-$xend)"
  366. _debug2 x $x
  367. x64="$(printf $x | tr -d : | _h2b | _base64 | _urlencode)"
  368. _debug2 x64 $x64
  369. let "xend+=1"
  370. y="$(printf $pubtext | cut -d : -f $xend-10000)"
  371. _debug2 y $y
  372. y64="$(printf $y | tr -d : | _h2b | _base64 | _urlencode)"
  373. _debug2 y64 $y64
  374. jwk='{"kty": "EC", "crv": "'$crv'", "x": "'$x64'", "y": "'$y64'"}'
  375. _debug2 jwk "$jwk"
  376. HEADER='{"alg": "ES256", "jwk": '$jwk'}'
  377. HEADERPLACE='{"nonce": "NONCE", "alg": "ES256", "jwk": '$jwk'}'
  378. else
  379. _err "Only RSA or EC key is supported."
  380. return 1
  381. fi
  382. _debug2 HEADER "$HEADER"
  383. }
  384. # body url [needbase64]
  385. _post() {
  386. body="$1"
  387. url="$2"
  388. needbase64="$3"
  389. if _exists "curl" ; then
  390. CURL="$CURL --dump-header $HTTP_HEADER "
  391. if [[ "$needbase64" ]] ; then
  392. response="$($CURL -A "User-Agent: $USER_AGENT" -X POST --data "$body" $url | _base64)"
  393. else
  394. response="$($CURL -A "User-Agent: $USER_AGENT" -X POST --data "$body" $url)"
  395. fi
  396. else
  397. if [[ "$needbase64" ]] ; then
  398. response="$($WGET -S -O - --user-agent="$USER_AGENT" --post-data="$body" $url 2>"$HTTP_HEADER" | _base64)"
  399. else
  400. response="$($WGET -S -O - --user-agent="$USER_AGENT" --post-data="$body" $url 2>"$HTTP_HEADER")"
  401. fi
  402. _sed_i "s/^ *//g" "$HTTP_HEADER"
  403. fi
  404. echo -n "$response"
  405. }
  406. # url getheader
  407. _get() {
  408. url="$1"
  409. onlyheader="$2"
  410. _debug url $url
  411. if _exists "curl" ; then
  412. if [[ "$onlyheader" ]] ; then
  413. $CURL -I -A "User-Agent: $USER_AGENT" $url
  414. else
  415. $CURL -A "User-Agent: $USER_AGENT" $url
  416. fi
  417. else
  418. _debug "WGET" "$WGET"
  419. if [[ "$onlyheader" ]] ; then
  420. eval $WGET --user-agent=\"$USER_AGENT\" -S -O /dev/null $url 2>&1 | sed 's/^[ ]*//g'
  421. else
  422. eval $WGET --user-agent=\"$USER_AGENT\" -O - $url
  423. fi
  424. fi
  425. ret=$?
  426. return $ret
  427. }
  428. # url payload needbase64 keyfile
  429. _send_signed_request() {
  430. url=$1
  431. payload=$2
  432. needbase64=$3
  433. keyfile=$4
  434. if [[ -z "$keyfile" ]] ; then
  435. keyfile="$ACCOUNT_KEY_PATH"
  436. fi
  437. _debug url $url
  438. _debug payload "$payload"
  439. if ! _calcjwk "$keyfile" ; then
  440. return 1
  441. fi
  442. payload64=$(echo -n $payload | _base64 | _urlencode)
  443. _debug2 payload64 $payload64
  444. nonceurl="$API/directory"
  445. nonce="$(_get $nonceurl "onlyheader" | grep -o "Replay-Nonce:.*$" | head -1 | tr -d "\r\n" | cut -d ' ' -f 2)"
  446. _debug nonce "$nonce"
  447. protected="$(printf "$HEADERPLACE" | sed "s/NONCE/$nonce/" )"
  448. _debug2 protected "$protected"
  449. protected64="$(printf "$protected" | _base64 | _urlencode)"
  450. _debug2 protected64 "$protected64"
  451. sig=$(echo -n "$protected64.$payload64" | _sign "$keyfile" "sha256" | _urlencode)
  452. _debug2 sig "$sig"
  453. body="{\"header\": $HEADER, \"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}"
  454. _debug2 body "$body"
  455. response="$(_post "$body" $url "$needbase64" )"
  456. responseHeaders="$(cat $HTTP_HEADER)"
  457. _debug2 responseHeaders "$responseHeaders"
  458. _debug2 response "$response"
  459. code="$(grep "^HTTP" $HTTP_HEADER | tail -1 | cut -d " " -f 2 | tr -d "\r\n" )"
  460. _debug code $code
  461. }
  462. #setopt "file" "opt" "=" "value" [";"]
  463. _setopt() {
  464. __conf="$1"
  465. __opt="$2"
  466. __sep="$3"
  467. __val="$4"
  468. __end="$5"
  469. if [[ -z "$__opt" ]] ; then
  470. echo usage: _setopt '"file" "opt" "=" "value" [";"]'
  471. return
  472. fi
  473. if [[ ! -f "$__conf" ]] ; then
  474. touch "$__conf"
  475. fi
  476. if grep -H -n "^$__opt$__sep" "$__conf" > /dev/null ; then
  477. _debug2 OK
  478. if [[ "$__val" == *"&"* ]] ; then
  479. __val="$(echo $__val | sed 's/&/\\&/g')"
  480. fi
  481. text="$(cat $__conf)"
  482. echo "$text" | sed "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" > "$__conf"
  483. elif grep -H -n "^#$__opt$__sep" "$__conf" > /dev/null ; then
  484. if [[ "$__val" == *"&"* ]] ; then
  485. __val="$(echo $__val | sed 's/&/\\&/g')"
  486. fi
  487. text="$(cat $__conf)"
  488. echo "$text" | sed "s|^#$__opt$__sep.*$|$__opt$__sep$__val$__end|" > "$__conf"
  489. else
  490. _debug2 APP
  491. echo "$__opt$__sep$__val$__end" >> "$__conf"
  492. fi
  493. _debug "$(grep -H -n "^$__opt$__sep" $__conf)"
  494. }
  495. #_savedomainconf key value
  496. #save to domain.conf
  497. _savedomainconf() {
  498. key="$1"
  499. value="$2"
  500. if [[ "$DOMAIN_CONF" ]] ; then
  501. _setopt $DOMAIN_CONF "$key" "=" "$value"
  502. else
  503. _err "DOMAIN_CONF is empty, can not save $key=$value"
  504. fi
  505. }
  506. #_saveaccountconf key value
  507. _saveaccountconf() {
  508. key="$1"
  509. value="$2"
  510. if [[ "$ACCOUNT_CONF_PATH" ]] ; then
  511. _setopt $ACCOUNT_CONF_PATH "$key" "=" "\"$value\""
  512. else
  513. _err "ACCOUNT_CONF_PATH is empty, can not save $key=$value"
  514. fi
  515. }
  516. _startserver() {
  517. content="$1"
  518. _debug "startserver: $$"
  519. nchelp="$(nc -h 2>&1)"
  520. if echo "$nchelp" | grep "\-q[ ,]" >/dev/null ; then
  521. _NC="nc -q 1 -l"
  522. else
  523. if echo "$nchelp" | grep "GNU netcat" >/dev/null && echo "$nchelp" | grep "\-c, \-\-close" >/dev/null ; then
  524. _NC="nc -c -l"
  525. elif echo "$nchelp" | grep "\-N" |grep "Shutdown the network socket after EOF on stdin" >/dev/null ; then
  526. _NC="nc -N -l"
  527. else
  528. _NC="nc -l"
  529. fi
  530. fi
  531. _debug "_NC" "$_NC"
  532. # while true ; do
  533. if [[ "$DEBUG" ]] ; then
  534. if ! echo -e -n "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort -vv ; then
  535. echo -e -n "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort -vv ;
  536. fi
  537. else
  538. if ! echo -e -n "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort > /dev/null 2>&1; then
  539. echo -e -n "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort > /dev/null 2>&1
  540. fi
  541. fi
  542. if [[ "$?" != "0" ]] ; then
  543. _err "nc listen error."
  544. exit 1
  545. fi
  546. # done
  547. }
  548. _stopserver(){
  549. pid="$1"
  550. _debug "pid" "$pid"
  551. if [[ -z "$pid" ]] ; then
  552. return
  553. fi
  554. if [[ "$(ps | grep "$pid")" ]] ; then
  555. _debug "Found proc process, kill it."
  556. kill -s 9 $pid > /dev/null
  557. fi
  558. for ncid in $(echo $(ps | grep nc) | cut -d " " -f 1) ; do
  559. _debug "kill $ncid"
  560. kill -s 9 $ncid > /dev/null
  561. done
  562. _get "http://localhost:$Le_HTTPPort" >/dev/null 2>$1
  563. }
  564. _initpath() {
  565. if [[ -z "$LE_WORKING_DIR" ]] ; then
  566. LE_WORKING_DIR=$HOME/.le
  567. fi
  568. _DEFAULT_ACCOUNT_CONF_PATH="$LE_WORKING_DIR/account.conf"
  569. if [[ -f "$_DEFAULT_ACCOUNT_CONF_PATH" ]] ; then
  570. source "$_DEFAULT_ACCOUNT_CONF_PATH"
  571. fi
  572. if [[ -z "$ACCOUNT_CONF_PATH" ]] ; then
  573. ACCOUNT_CONF_PATH="$_DEFAULT_ACCOUNT_CONF_PATH"
  574. fi
  575. if [[ -f "$ACCOUNT_CONF_PATH" ]] ; then
  576. source "$ACCOUNT_CONF_PATH"
  577. fi
  578. if [[ "$IN_CRON" ]] ; then
  579. if [[ ! "$_USER_PATH_EXPORTED" ]] ; then
  580. _USER_PATH_EXPORTED=1
  581. export PATH="$USER_PATH:$PATH"
  582. fi
  583. fi
  584. if [[ -z "$API" ]] ; then
  585. if [[ -z "$STAGE" ]] ; then
  586. API="$DEFAULT_CA"
  587. else
  588. API="$STAGE_CA"
  589. _info "Using stage api:$API"
  590. fi
  591. fi
  592. if [[ -z "$ACME_DIR" ]] ; then
  593. ACME_DIR="/home/.acme"
  594. fi
  595. if [[ -z "$APACHE_CONF_BACKUP_DIR" ]] ; then
  596. APACHE_CONF_BACKUP_DIR="$LE_WORKING_DIR"
  597. fi
  598. if [[ -z "$USER_AGENT" ]] ; then
  599. USER_AGENT="$DEFAULT_USER_AGENT"
  600. fi
  601. HTTP_HEADER="$LE_WORKING_DIR/http.header"
  602. WGET="wget -q"
  603. if [[ "$DEBUG" -ge "2" ]] ; then
  604. WGET="$WGET -d "
  605. fi
  606. dp="$LE_WORKING_DIR/curl.dump"
  607. CURL="curl -L --silent"
  608. if [[ "$DEBUG" -ge "2" ]] ; then
  609. CURL="$CURL -L --trace-ascii $dp "
  610. fi
  611. domain="$1"
  612. if [[ -z "$ACCOUNT_KEY_PATH" ]] ; then
  613. ACCOUNT_KEY_PATH="$LE_WORKING_DIR/account.key"
  614. fi
  615. if [[ -z "$domain" ]] ; then
  616. return 0
  617. fi
  618. domainhome="$LE_WORKING_DIR/$domain"
  619. mkdir -p "$domainhome"
  620. if [[ -z "$DOMAIN_PATH" ]] ; then
  621. DOMAIN_PATH="$domainhome"
  622. fi
  623. if [[ -z "$DOMAIN_CONF" ]] ; then
  624. DOMAIN_CONF="$domainhome/$domain.conf"
  625. fi
  626. if [[ -z "$DOMAIN_SSL_CONF" ]] ; then
  627. DOMAIN_SSL_CONF="$domainhome/$domain.ssl.conf"
  628. fi
  629. if [[ -z "$CSR_PATH" ]] ; then
  630. CSR_PATH="$domainhome/$domain.csr"
  631. fi
  632. if [[ -z "$CERT_KEY_PATH" ]] ; then
  633. CERT_KEY_PATH="$domainhome/$domain.key"
  634. fi
  635. if [[ -z "$CERT_PATH" ]] ; then
  636. CERT_PATH="$domainhome/$domain.cer"
  637. fi
  638. if [[ -z "$CA_CERT_PATH" ]] ; then
  639. CA_CERT_PATH="$domainhome/ca.cer"
  640. fi
  641. if [[ -z "$CERT_FULLCHAIN_PATH" ]] ; then
  642. CERT_FULLCHAIN_PATH="$domainhome/fullchain.cer"
  643. fi
  644. if [[ -z "$CERT_PFX_PATH" ]] ; then
  645. CERT_PFX_PATH="$domainhome/$domain.pfx"
  646. fi
  647. }
  648. _apachePath() {
  649. httpdconfname="$(apachectl -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"' )"
  650. if [[ "$httpdconfname" == '/'* ]] ; then
  651. httpdconf="$httpdconfname"
  652. httpdconfname="$(basename $httpdconfname)"
  653. else
  654. httpdroot="$(apachectl -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"' )"
  655. httpdconf="$httpdroot/$httpdconfname"
  656. fi
  657. if [[ ! -f $httpdconf ]] ; then
  658. _err "Apache Config file not found" $httpdconf
  659. return 1
  660. fi
  661. return 0
  662. }
  663. _restoreApache() {
  664. if [[ -z "$usingApache" ]] ; then
  665. return 0
  666. fi
  667. _initpath
  668. if ! _apachePath ; then
  669. return 1
  670. fi
  671. if [[ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ]] ; then
  672. _debug "No config file to restore."
  673. return 0
  674. fi
  675. cp -p "$APACHE_CONF_BACKUP_DIR/$httpdconfname" "$httpdconf"
  676. if ! apachectl -t ; then
  677. _err "Sorry, restore apache config error, please contact me."
  678. return 1;
  679. fi
  680. rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname"
  681. return 0
  682. }
  683. _setApache() {
  684. _initpath
  685. if ! _apachePath ; then
  686. return 1
  687. fi
  688. #backup the conf
  689. _debug "Backup apache config file" $httpdconf
  690. cp -p $httpdconf $APACHE_CONF_BACKUP_DIR/
  691. _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname"
  692. _info "In case there is an error that can not be restored automatically, you may try restore it yourself."
  693. _info "The backup file will be deleted on sucess, just forget it."
  694. #add alias
  695. apacheVer="$(apachectl -V | grep "Server version:" | cut -d : -f 2 | cut -d " " -f 2 | cut -d '/' -f 2 )"
  696. _debug "apacheVer" "$apacheVer"
  697. apacheMajer="$(echo "$apacheVer" | cut -d . -f 1)"
  698. apacheMinor="$(echo "$apacheVer" | cut -d . -f 2)"
  699. if [[ "$apacheVer" ]] && [[ "$apacheMajer" -ge "2" ]] && [[ "$apacheMinor" -ge "4" ]] ; then
  700. echo "
  701. Alias /.well-known/acme-challenge $ACME_DIR
  702. <Directory $ACME_DIR >
  703. Require all granted
  704. </Directory>
  705. " >> $httpdconf
  706. else
  707. echo "
  708. Alias /.well-known/acme-challenge $ACME_DIR
  709. <Directory $ACME_DIR >
  710. Order allow,deny
  711. Allow from all
  712. </Directory>
  713. " >> $httpdconf
  714. fi
  715. if ! apachectl -t ; then
  716. _err "Sorry, apache config error, please contact me."
  717. _restoreApache
  718. return 1;
  719. fi
  720. if [[ ! -d "$ACME_DIR" ]] ; then
  721. mkdir -p "$ACME_DIR"
  722. chmod 755 "$ACME_DIR"
  723. fi
  724. if ! apachectl graceful ; then
  725. _err "Sorry, apachectl graceful error, please contact me."
  726. _restoreApache
  727. return 1;
  728. fi
  729. usingApache="1"
  730. return 0
  731. }
  732. _clearup () {
  733. _stopserver $serverproc
  734. serverproc=""
  735. _restoreApache
  736. }
  737. # webroot removelevel tokenfile
  738. _clearupwebbroot() {
  739. __webroot="$1"
  740. if [[ -z "$__webroot" ]] ; then
  741. _debug "no webroot specified, skip"
  742. return 0
  743. fi
  744. if [[ "$2" == '1' ]] ; then
  745. _debug "remove $__webroot/.well-known"
  746. rm -rf "$__webroot/.well-known"
  747. elif [[ "$2" == '2' ]] ; then
  748. _debug "remove $__webroot/.well-known/acme-challenge"
  749. rm -rf "$__webroot/.well-known/acme-challenge"
  750. elif [[ "$2" == '3' ]] ; then
  751. _debug "remove $__webroot/.well-known/acme-challenge/$3"
  752. rm -rf "$__webroot/.well-known/acme-challenge/$3"
  753. else
  754. _info "Skip for removelevel:$2"
  755. fi
  756. return 0
  757. }
  758. issue() {
  759. if [[ -z "$2" ]] ; then
  760. echo "Usage: le --issue -d a.com -w /path/to/webroot/a.com/ "
  761. return 1
  762. fi
  763. Le_Webroot="$1"
  764. Le_Domain="$2"
  765. Le_Alt="$3"
  766. Le_Keylength="$4"
  767. Le_RealCertPath="$5"
  768. Le_RealKeyPath="$6"
  769. Le_RealCACertPath="$7"
  770. Le_ReloadCmd="$8"
  771. Le_RealFullChainPath="$9"
  772. _initpath $Le_Domain
  773. if [[ -f "$DOMAIN_CONF" ]] ; then
  774. Le_NextRenewTime=$(grep "^Le_NextRenewTime=" "$DOMAIN_CONF" | cut -d '=' -f 2)
  775. if [[ -z "$FORCE" ]] && [[ "$Le_NextRenewTime" ]] && [[ "$(date -u "+%s" )" -lt "$Le_NextRenewTime" ]] ; then
  776. _info "Skip, Next renewal time is: $(grep "^Le_NextRenewTimeStr" "$DOMAIN_CONF" | cut -d '=' -f 2)"
  777. return 2
  778. fi
  779. fi
  780. _setopt "$DOMAIN_CONF" "Le_Domain" "=" "$Le_Domain"
  781. _setopt "$DOMAIN_CONF" "Le_Alt" "=" "$Le_Alt"
  782. _setopt "$DOMAIN_CONF" "Le_Webroot" "=" "$Le_Webroot"
  783. _setopt "$DOMAIN_CONF" "Le_Keylength" "=" "$Le_Keylength"
  784. _setopt "$DOMAIN_CONF" "Le_RealCertPath" "=" "\"$Le_RealCertPath\""
  785. _setopt "$DOMAIN_CONF" "Le_RealCACertPath" "=" "\"$Le_RealCACertPath\""
  786. _setopt "$DOMAIN_CONF" "Le_RealKeyPath" "=" "\"$Le_RealKeyPath\""
  787. _setopt "$DOMAIN_CONF" "Le_ReloadCmd" "=" "\"$Le_ReloadCmd\""
  788. _setopt "$DOMAIN_CONF" "Le_RealFullChainPath" "=" "\"$Le_RealFullChainPath\""
  789. if [[ "$Le_Alt" == "no" ]] ; then
  790. Le_Alt=""
  791. fi
  792. if [[ "$Le_Keylength" == "no" ]] ; then
  793. Le_Keylength=""
  794. fi
  795. if [[ "$Le_RealCertPath" == "no" ]] ; then
  796. Le_RealCertPath=""
  797. fi
  798. if [[ "$Le_RealKeyPath" == "no" ]] ; then
  799. Le_RealKeyPath=""
  800. fi
  801. if [[ "$Le_RealCACertPath" == "no" ]] ; then
  802. Le_RealCACertPath=""
  803. fi
  804. if [[ "$Le_ReloadCmd" == "no" ]] ; then
  805. Le_ReloadCmd=""
  806. fi
  807. if [[ "$Le_RealFullChainPath" == "no" ]] ; then
  808. Le_RealFullChainPath=""
  809. fi
  810. if [[ "$Le_Webroot" == *"no"* ]] ; then
  811. _info "Standalone mode."
  812. if ! command -v "nc" > /dev/null ; then
  813. _err "Please install netcat(nc) tools first."
  814. return 1
  815. fi
  816. if [[ -z "$Le_HTTPPort" ]] ; then
  817. Le_HTTPPort=80
  818. fi
  819. _setopt "$DOMAIN_CONF" "Le_HTTPPort" "=" "$Le_HTTPPort"
  820. netprc="$(_ss "$Le_HTTPPort" | grep "$Le_HTTPPort")"
  821. if [[ "$netprc" ]] ; then
  822. _err "$netprc"
  823. _err "tcp port $Le_HTTPPort is already used by $(echo "$netprc" | cut -d : -f 4)"
  824. _err "Please stop it first"
  825. return 1
  826. fi
  827. fi
  828. if [[ "$Le_Webroot" == *"apache"* ]] ; then
  829. if ! _setApache ; then
  830. _err "set up apache error. Report error to me."
  831. return 1
  832. fi
  833. wellknown_path="$ACME_DIR"
  834. else
  835. usingApache=""
  836. fi
  837. createAccountKey $Le_Domain $Le_Keylength
  838. if ! _calcjwk "$ACCOUNT_KEY_PATH" ; then
  839. return 1
  840. fi
  841. accountkey_json=$(echo -n "$jwk" | tr -d ' ' )
  842. thumbprint=$(echo -n "$accountkey_json" | _digest "sha256" | _urlencode)
  843. accountkeyhash="$(cat "$ACCOUNT_KEY_PATH" | _digest "sha256" )"
  844. accountkeyhash="$(echo $accountkeyhash$API | _digest "sha256" )"
  845. if [[ "$accountkeyhash" != "$ACCOUNT_KEY_HASH" ]] ; then
  846. _info "Registering account"
  847. regjson='{"resource": "new-reg", "agreement": "'$AGREEMENT'"}'
  848. if [[ "$ACCOUNT_EMAIL" ]] ; then
  849. regjson='{"resource": "new-reg", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "agreement": "'$AGREEMENT'"}'
  850. fi
  851. _send_signed_request "$API/acme/new-reg" "$regjson"
  852. if [[ "$code" == "" ]] || [[ "$code" == '201' ]] ; then
  853. _info "Registered"
  854. echo $response > $LE_WORKING_DIR/account.json
  855. elif [[ "$code" == '409' ]] ; then
  856. _info "Already registered"
  857. else
  858. _err "Register account Error: $response"
  859. _clearup
  860. return 1
  861. fi
  862. ACCOUNT_KEY_HASH="$accountkeyhash"
  863. _saveaccountconf "ACCOUNT_KEY_HASH" "$ACCOUNT_KEY_HASH"
  864. else
  865. _info "Skip register account key"
  866. fi
  867. if ! createDomainKey $Le_Domain $Le_Keylength ; then
  868. _err "Create domain key error."
  869. return 1
  870. fi
  871. if ! createCSR $Le_Domain $Le_Alt ; then
  872. _err "Create CSR error."
  873. return 1
  874. fi
  875. vlist="$Le_Vlist"
  876. # verify each domain
  877. _info "Verify each domain"
  878. sep='#'
  879. if [[ -z "$vlist" ]] ; then
  880. alldomains=$(echo "$Le_Domain,$Le_Alt" | tr ',' ' ' )
  881. _index=1
  882. _currentRoot=""
  883. for d in $alldomains
  884. do
  885. _info "Getting webroot for domain" $d
  886. _w="$(echo $Le_Webroot | cut -d , -f $_index)"
  887. _debug _w "$_w"
  888. if [[ "$_w" ]] ; then
  889. _currentRoot="$_w"
  890. fi
  891. _debug "_currentRoot" "$_currentRoot"
  892. let "_index+=1"
  893. vtype="$VTYPE_HTTP"
  894. if [[ "$_currentRoot" == "dns"* ]] ; then
  895. vtype="$VTYPE_DNS"
  896. fi
  897. _info "Getting token for domain" $d
  898. _send_signed_request "$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$d\"}}"
  899. if [[ ! -z "$code" ]] && [[ ! "$code" == '201' ]] ; then
  900. _err "new-authz error: $response"
  901. _clearup
  902. return 1
  903. fi
  904. entry="$(printf $response | egrep -o '\{[^{]*"type":"'$vtype'"[^}]*')"
  905. _debug entry "$entry"
  906. token="$(printf "$entry" | egrep -o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
  907. _debug token $token
  908. uri="$(printf "$entry" | egrep -o '"uri":"[^"]*'| cut -d : -f 2,3 | tr -d '"' )"
  909. _debug uri $uri
  910. keyauthorization="$token.$thumbprint"
  911. _debug keyauthorization "$keyauthorization"
  912. dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot"
  913. _debug dvlist "$dvlist"
  914. vlist="$vlist$dvlist,"
  915. done
  916. #add entry
  917. dnsadded=""
  918. ventries=$(echo "$vlist" | tr ',' ' ' )
  919. for ventry in $ventries
  920. do
  921. d=$(echo $ventry | cut -d $sep -f 1)
  922. keyauthorization=$(echo $ventry | cut -d $sep -f 2)
  923. vtype=$(echo $ventry | cut -d $sep -f 4)
  924. _currentRoot=$(echo $ventry | cut -d $sep -f 5)
  925. if [[ "$vtype" == "$VTYPE_DNS" ]] ; then
  926. dnsadded='0'
  927. txtdomain="_acme-challenge.$d"
  928. _debug txtdomain "$txtdomain"
  929. txt="$(echo -e -n $keyauthorization | _digest "sha256" | _urlencode)"
  930. _debug txt "$txt"
  931. #dns
  932. #1. check use api
  933. d_api=""
  934. if [[ -f "$LE_WORKING_DIR/$d/$_currentRoot" ]] ; then
  935. d_api="$LE_WORKING_DIR/$d/$_currentRoot"
  936. elif [[ -f "$LE_WORKING_DIR/$d/$_currentRoot.sh" ]] ; then
  937. d_api="$LE_WORKING_DIR/$d/$_currentRoot.sh"
  938. elif [[ -f "$LE_WORKING_DIR/$_currentRoot" ]] ; then
  939. d_api="$LE_WORKING_DIR/$_currentRoot"
  940. elif [[ -f "$LE_WORKING_DIR/$_currentRoot.sh" ]] ; then
  941. d_api="$LE_WORKING_DIR/$_currentRoot.sh"
  942. elif [[ -f "$LE_WORKING_DIR/dnsapi/$_currentRoot" ]] ; then
  943. d_api="$LE_WORKING_DIR/dnsapi/$_currentRoot"
  944. elif [[ -f "$LE_WORKING_DIR/dnsapi/$_currentRoot.sh" ]] ; then
  945. d_api="$LE_WORKING_DIR/dnsapi/$_currentRoot.sh"
  946. fi
  947. _debug d_api "$d_api"
  948. if [[ "$d_api" ]] ; then
  949. _info "Found domain api file: $d_api"
  950. else
  951. _err "Add the following TXT record:"
  952. _err "Domain: $txtdomain"
  953. _err "TXT value: $txt"
  954. _err "Please be aware that you prepend _acme-challenge. before your domain"
  955. _err "so the resulting subdomain will be: $txtdomain"
  956. continue
  957. fi
  958. (
  959. if ! source $d_api ; then
  960. _err "Load file $d_api error. Please check your api file and try again."
  961. return 1
  962. fi
  963. addcommand="$_currentRoot-add"
  964. if ! _exists $addcommand ; then
  965. _err "It seems that your api file is not correct, it must have a function named: $addcommand"
  966. return 1
  967. fi
  968. if ! $addcommand $txtdomain $txt ; then
  969. _err "Error add txt for domain:$txtdomain"
  970. return 1
  971. fi
  972. )
  973. if [[ "$?" != "0" ]] ; then
  974. return 1
  975. fi
  976. dnsadded='1'
  977. fi
  978. done
  979. if [[ "$dnsadded" == '0' ]] ; then
  980. _setopt "$DOMAIN_CONF" "Le_Vlist" "=" "\"$vlist\""
  981. _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit."
  982. _err "Please add the TXT records to the domains, and retry again."
  983. return 1
  984. fi
  985. fi
  986. if [[ "$dnsadded" == '1' ]] ; then
  987. _info "Sleep 60 seconds for the txt records to take effect"
  988. sleep 60
  989. fi
  990. _debug "ok, let's start to verify"
  991. ventries=$(echo "$vlist" | tr ',' ' ' )
  992. for ventry in $ventries
  993. do
  994. d=$(echo $ventry | cut -d $sep -f 1)
  995. keyauthorization=$(echo $ventry | cut -d $sep -f 2)
  996. uri=$(echo $ventry | cut -d $sep -f 3)
  997. vtype=$(echo $ventry | cut -d $sep -f 4)
  998. _currentRoot=$(echo $ventry | cut -d $sep -f 5)
  999. _info "Verifying:$d"
  1000. _debug "d" "$d"
  1001. _debug "keyauthorization" "$keyauthorization"
  1002. _debug "uri" "$uri"
  1003. removelevel=""
  1004. token=""
  1005. _debug "_currentRoot" "$_currentRoot"
  1006. if [[ "$vtype" == "$VTYPE_HTTP" ]] ; then
  1007. if [[ "$_currentRoot" == "no" ]] ; then
  1008. _info "Standalone mode server"
  1009. _startserver "$keyauthorization" &
  1010. if [[ "$?" != "0" ]] ; then
  1011. return 1
  1012. fi
  1013. serverproc="$!"
  1014. sleep 2
  1015. _debug serverproc $serverproc
  1016. else
  1017. if [[ -z "$wellknown_path" ]] ; then
  1018. wellknown_path="$_currentRoot/.well-known/acme-challenge"
  1019. fi
  1020. _debug wellknown_path "$wellknown_path"
  1021. if [[ ! -d "$_currentRoot/.well-known" ]] ; then
  1022. removelevel='1'
  1023. elif [[ ! -d "$_currentRoot/.well-known/acme-challenge" ]] ; then
  1024. removelevel='2'
  1025. else
  1026. removelevel='3'
  1027. fi
  1028. token="$(echo -e -n "$keyauthorization" | cut -d '.' -f 1)"
  1029. _debug "writing token:$token to $wellknown_path/$token"
  1030. mkdir -p "$wellknown_path"
  1031. echo -n "$keyauthorization" > "$wellknown_path/$token"
  1032. if [[ ! "$usingApache" ]] ; then
  1033. webroot_owner=$(_stat $_currentRoot)
  1034. _debug "Changing owner/group of .well-known to $webroot_owner"
  1035. chown -R $webroot_owner "$_currentRoot/.well-known"
  1036. fi
  1037. fi
  1038. fi
  1039. _send_signed_request $uri "{\"resource\": \"challenge\", \"keyAuthorization\": \"$keyauthorization\"}"
  1040. if [[ ! -z "$code" ]] && [[ ! "$code" == '202' ]] ; then
  1041. _err "$d:Challenge error: $response"
  1042. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  1043. _clearup
  1044. return 1
  1045. fi
  1046. waittimes=0
  1047. if [[ -z "$MAX_RETRY_TIMES" ]] ; then
  1048. MAX_RETRY_TIMES=30
  1049. fi
  1050. while [[ "1" ]] ; do
  1051. let "waittimes+=1"
  1052. if [[ "$waittimes" -ge "$MAX_RETRY_TIMES" ]] ; then
  1053. _err "$d:Timeout"
  1054. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  1055. _clearup
  1056. return 1
  1057. fi
  1058. _debug "sleep 5 secs to verify"
  1059. sleep 5
  1060. _debug "checking"
  1061. response="$(_get $uri)"
  1062. if [[ "$?" != "0" ]] ; then
  1063. _err "$d:Verify error:$response"
  1064. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  1065. _clearup
  1066. return 1
  1067. fi
  1068. status=$(echo $response | egrep -o '"status":"[^"]*' | cut -d : -f 2 | tr -d '"')
  1069. if [[ "$status" == "valid" ]] ; then
  1070. _info "Success"
  1071. _stopserver $serverproc
  1072. serverproc=""
  1073. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  1074. break;
  1075. fi
  1076. if [[ "$status" == "invalid" ]] ; then
  1077. error=$(echo $response | egrep -o '"error":{[^}]*}' | grep -o '"detail":"[^"]*"' | cut -d '"' -f 4)
  1078. _err "$d:Verify error:$error"
  1079. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  1080. _clearup
  1081. return 1;
  1082. fi
  1083. if [[ "$status" == "pending" ]] ; then
  1084. _info "Pending"
  1085. else
  1086. _err "$d:Verify error:$response"
  1087. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  1088. _clearup
  1089. return 1
  1090. fi
  1091. done
  1092. done
  1093. _clearup
  1094. _info "Verify finished, start to sign."
  1095. der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _urlencode)"
  1096. _send_signed_request "$API/acme/new-cert" "{\"resource\": \"new-cert\", \"csr\": \"$der\"}" "needbase64"
  1097. Le_LinkCert="$(grep -i -o '^Location.*$' $HTTP_HEADER | head -1 | tr -d "\r\n" | cut -d " " -f 2)"
  1098. _setopt "$DOMAIN_CONF" "Le_LinkCert" "=" "$Le_LinkCert"
  1099. if [[ "$Le_LinkCert" ]] ; then
  1100. echo "$BEGIN_CERT" > "$CERT_PATH"
  1101. _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH"
  1102. echo "$END_CERT" >> "$CERT_PATH"
  1103. _info "Cert success."
  1104. cat "$CERT_PATH"
  1105. _info "Your cert is in $CERT_PATH"
  1106. cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH"
  1107. if [[ ! "$USER_PATH" ]] || [[ ! "$IN_CRON" ]] ; then
  1108. USER_PATH="$PATH"
  1109. _saveaccountconf "USER_PATH" "$USER_PATH"
  1110. fi
  1111. fi
  1112. if [[ -z "$Le_LinkCert" ]] ; then
  1113. response="$(echo $response | _dbase64 "multiline" )"
  1114. _err "Sign failed: $(echo "$response" | grep -o '"detail":"[^"]*"')"
  1115. return 1
  1116. fi
  1117. _setopt "$DOMAIN_CONF" 'Le_Vlist' '=' "\"\""
  1118. Le_LinkIssuer=$(grep -i '^Link' $HTTP_HEADER | head -1 | cut -d " " -f 2| cut -d ';' -f 1 | tr -d '<>' )
  1119. _setopt "$DOMAIN_CONF" "Le_LinkIssuer" "=" "$Le_LinkIssuer"
  1120. if [[ "$Le_LinkIssuer" ]] ; then
  1121. echo "$BEGIN_CERT" > "$CA_CERT_PATH"
  1122. _get "$Le_LinkIssuer" | _base64 "multiline" >> "$CA_CERT_PATH"
  1123. echo "$END_CERT" >> "$CA_CERT_PATH"
  1124. _info "The intermediate CA cert is in $CA_CERT_PATH"
  1125. cat "$CA_CERT_PATH" >> "$CERT_FULLCHAIN_PATH"
  1126. _info "And the full chain certs is there: $CERT_FULLCHAIN_PATH"
  1127. fi
  1128. Le_CertCreateTime=$(date -u "+%s")
  1129. _setopt "$DOMAIN_CONF" "Le_CertCreateTime" "=" "$Le_CertCreateTime"
  1130. Le_CertCreateTimeStr=$(date -u )
  1131. _setopt "$DOMAIN_CONF" "Le_CertCreateTimeStr" "=" "\"$Le_CertCreateTimeStr\""
  1132. if [[ ! "$Le_RenewalDays" ]] ; then
  1133. Le_RenewalDays=80
  1134. fi
  1135. _setopt "$DOMAIN_CONF" "Le_RenewalDays" "=" "$Le_RenewalDays"
  1136. let "Le_NextRenewTime=Le_CertCreateTime+Le_RenewalDays*24*60*60"
  1137. _setopt "$DOMAIN_CONF" "Le_NextRenewTime" "=" "$Le_NextRenewTime"
  1138. Le_NextRenewTimeStr=$( _time2str $Le_NextRenewTime )
  1139. _setopt "$DOMAIN_CONF" "Le_NextRenewTimeStr" "=" "\"$Le_NextRenewTimeStr\""
  1140. installcert $Le_Domain "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd" "$Le_RealFullChainPath"
  1141. }
  1142. renew() {
  1143. Le_Domain="$1"
  1144. if [[ -z "$Le_Domain" ]] ; then
  1145. _err "Usage: le.sh --renew -d domain.com"
  1146. return 1
  1147. fi
  1148. _initpath $Le_Domain
  1149. if [[ ! -f "$DOMAIN_CONF" ]] ; then
  1150. _info "$Le_Domain is not a issued domain, skip."
  1151. return 0;
  1152. fi
  1153. source "$DOMAIN_CONF"
  1154. if [[ -z "$FORCE" ]] && [[ "$Le_NextRenewTime" ]] && [[ "$(date -u "+%s" )" -lt "$Le_NextRenewTime" ]] ; then
  1155. _info "Skip, Next renewal time is: $Le_NextRenewTimeStr"
  1156. return 2
  1157. fi
  1158. IS_RENEW="1"
  1159. issue "$Le_Webroot" "$Le_Domain" "$Le_Alt" "$Le_Keylength" "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd" "$Le_RealFullChainPath"
  1160. local res=$?
  1161. IS_RENEW=""
  1162. return $res
  1163. }
  1164. renewAll() {
  1165. _initpath
  1166. _info "renewAll"
  1167. for d in $(ls -F ${LE_WORKING_DIR}/ | grep [^.].*[.].*/$ ) ; do
  1168. d=$(echo $d | cut -d '/' -f 1)
  1169. _info "renew $d"
  1170. Le_LinkCert=""
  1171. Le_Domain=""
  1172. Le_Alt="no"
  1173. Le_Webroot=""
  1174. Le_Keylength=""
  1175. Le_LinkIssuer=""
  1176. Le_CertCreateTime=""
  1177. Le_CertCreateTimeStr=""
  1178. Le_RenewalDays=""
  1179. Le_NextRenewTime=""
  1180. Le_NextRenewTimeStr=""
  1181. Le_RealCertPath=""
  1182. Le_RealKeyPath=""
  1183. Le_RealCACertPath=""
  1184. Le_ReloadCmd=""
  1185. Le_RealFullChainPath=""
  1186. DOMAIN_PATH=""
  1187. DOMAIN_CONF=""
  1188. DOMAIN_SSL_CONF=""
  1189. CSR_PATH=""
  1190. CERT_KEY_PATH=""
  1191. CERT_PATH=""
  1192. CA_CERT_PATH=""
  1193. CERT_PFX_PATH=""
  1194. CERT_FULLCHAIN_PATH=""
  1195. ACCOUNT_KEY_PATH=""
  1196. wellknown_path=""
  1197. renew "$d"
  1198. done
  1199. }
  1200. installcert() {
  1201. Le_Domain="$1"
  1202. if [[ -z "$Le_Domain" ]] ; then
  1203. echo "Usage: le.sh --installcert -d domain.com [--certpath cert-file-path] [--keypath key-file-path] [--capath ca-cert-file-path] [ --reloadCmd reloadCmd] [--fullchainpath fullchain-path]"
  1204. return 1
  1205. fi
  1206. Le_RealCertPath="$2"
  1207. Le_RealKeyPath="$3"
  1208. Le_RealCACertPath="$4"
  1209. Le_ReloadCmd="$5"
  1210. Le_RealFullChainPath="$6"
  1211. _initpath $Le_Domain
  1212. _setopt "$DOMAIN_CONF" "Le_RealCertPath" "=" "\"$Le_RealCertPath\""
  1213. _setopt "$DOMAIN_CONF" "Le_RealCACertPath" "=" "\"$Le_RealCACertPath\""
  1214. _setopt "$DOMAIN_CONF" "Le_RealKeyPath" "=" "\"$Le_RealKeyPath\""
  1215. _setopt "$DOMAIN_CONF" "Le_ReloadCmd" "=" "\"$Le_ReloadCmd\""
  1216. _setopt "$DOMAIN_CONF" "Le_RealFullChainPath" "=" "\"$Le_RealFullChainPath\""
  1217. if [[ "$Le_RealCertPath" ]] ; then
  1218. if [[ -f "$Le_RealCertPath" ]] ; then
  1219. cp -p "$Le_RealCertPath" "$Le_RealCertPath".bak
  1220. fi
  1221. cat "$CERT_PATH" > "$Le_RealCertPath"
  1222. fi
  1223. if [[ "$Le_RealCACertPath" ]] ; then
  1224. if [[ "$Le_RealCACertPath" == "$Le_RealCertPath" ]] ; then
  1225. echo "" >> "$Le_RealCACertPath"
  1226. cat "$CA_CERT_PATH" >> "$Le_RealCACertPath"
  1227. else
  1228. if [[ -f "$Le_RealCACertPath" ]] ; then
  1229. cp -p "$Le_RealCACertPath" "$Le_RealCACertPath".bak
  1230. fi
  1231. cat "$CA_CERT_PATH" > "$Le_RealCACertPath"
  1232. fi
  1233. fi
  1234. if [[ "$Le_RealKeyPath" ]] ; then
  1235. if [[ -f "$Le_RealKeyPath" ]] ; then
  1236. cp -p "$Le_RealKeyPath" "$Le_RealKeyPath".bak
  1237. fi
  1238. cat "$CERT_KEY_PATH" > "$Le_RealKeyPath"
  1239. fi
  1240. if [[ "$Le_RealFullChainPath" ]] ; then
  1241. if [[ -f "$Le_RealFullChainPath" ]] ; then
  1242. cp -p "$Le_RealFullChainPath" "$Le_RealFullChainPath".bak
  1243. fi
  1244. cat "$CERT_FULLCHAIN_PATH" > "$Le_RealFullChainPath"
  1245. fi
  1246. if [[ "$Le_ReloadCmd" ]] ; then
  1247. _info "Run Le_ReloadCmd: $Le_ReloadCmd"
  1248. (cd "$DOMAIN_PATH" && eval "$Le_ReloadCmd")
  1249. fi
  1250. }
  1251. installcronjob() {
  1252. _initpath
  1253. if ! _exists "crontab" ; then
  1254. _err "crontab doesn't exist, so, we can not install cron jobs."
  1255. _err "All your certs will not be renewed automatically."
  1256. _err "You must add your own cron job to call 'le.sh cron' everyday."
  1257. return 1
  1258. fi
  1259. _info "Installing cron job"
  1260. if ! crontab -l | grep 'le.sh cron' ; then
  1261. if [[ -f "$LE_WORKING_DIR/le.sh" ]] ; then
  1262. lesh="\"$LE_WORKING_DIR\"/le.sh"
  1263. else
  1264. _err "Can not install cronjob, le.sh not found."
  1265. return 1
  1266. fi
  1267. crontab -l | { cat; echo "0 0 * * * LE_WORKING_DIR=\"$LE_WORKING_DIR\" $lesh cron > /dev/null"; } | crontab -
  1268. fi
  1269. if [[ "$?" != "0" ]] ; then
  1270. _err "Install cron job failed. You need to manually renew your certs."
  1271. _err "Or you can add cronjob by yourself:"
  1272. _err "LE_WORKING_DIR=\"$LE_WORKING_DIR\" $lesh cron > /dev/null"
  1273. return 1
  1274. fi
  1275. }
  1276. uninstallcronjob() {
  1277. if ! _exists "crontab" ; then
  1278. return
  1279. fi
  1280. _info "Removing cron job"
  1281. cr="$(crontab -l | grep 'le.sh cron')"
  1282. if [[ "$cr" ]] ; then
  1283. crontab -l | sed "/le.sh cron/d" | crontab -
  1284. LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 6 | cut -d '=' -f 2 | tr -d '"')"
  1285. _info LE_WORKING_DIR "$LE_WORKING_DIR"
  1286. fi
  1287. _initpath
  1288. }
  1289. revoke() {
  1290. Le_Domain="$1"
  1291. if [[ -z "$Le_Domain" ]] ; then
  1292. echo "Usage: le.sh --revoke -d domain.com"
  1293. return 1
  1294. fi
  1295. _initpath $Le_Domain
  1296. if [[ ! -f "$DOMAIN_CONF" ]] ; then
  1297. _err "$Le_Domain is not a issued domain, skip."
  1298. return 1;
  1299. fi
  1300. if [[ ! -f "$CERT_PATH" ]] ; then
  1301. _err "Cert for $Le_Domain $CERT_PATH is not found, skip."
  1302. return 1
  1303. fi
  1304. cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}"| tr -d "\r\n" | _urlencode)"
  1305. if [[ -z "$cert" ]] ; then
  1306. _err "Cert for $Le_Domain is empty found, skip."
  1307. return 1
  1308. fi
  1309. data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}"
  1310. uri="$API/acme/revoke-cert"
  1311. _info "Try domain key first."
  1312. if _send_signed_request $uri "$data" "" "$CERT_KEY_PATH"; then
  1313. if [[ -z "$response" ]] ; then
  1314. _info "Revoke success."
  1315. rm -f $CERT_PATH
  1316. return 0
  1317. else
  1318. _err "Revoke error by domain key."
  1319. _err "$resource"
  1320. fi
  1321. fi
  1322. _info "Then try account key."
  1323. if _send_signed_request $uri "$data" "" "$ACCOUNT_KEY_PATH" ; then
  1324. if [[ -z "$response" ]] ; then
  1325. _info "Revoke success."
  1326. rm -f $CERT_PATH
  1327. return 0
  1328. else
  1329. _err "Revoke error."
  1330. _debug "$resource"
  1331. fi
  1332. fi
  1333. return 1
  1334. }
  1335. # Detect profile file if not specified as environment variable
  1336. _detect_profile() {
  1337. if [ -n "$PROFILE" -a -f "$PROFILE" ] ; then
  1338. echo "$PROFILE"
  1339. return
  1340. fi
  1341. local DETECTED_PROFILE
  1342. DETECTED_PROFILE=''
  1343. local SHELLTYPE
  1344. SHELLTYPE="$(basename "/$SHELL")"
  1345. if [[ "$SHELLTYPE" = "bash" ]] ; then
  1346. if [[ -f "$HOME/.bashrc" ]] ; then
  1347. DETECTED_PROFILE="$HOME/.bashrc"
  1348. elif [[ -f "$HOME/.bash_profile" ]] ; then
  1349. DETECTED_PROFILE="$HOME/.bash_profile"
  1350. fi
  1351. elif [[ "$SHELLTYPE" = "zsh" ]] ; then
  1352. DETECTED_PROFILE="$HOME/.zshrc"
  1353. fi
  1354. if [[ -z "$DETECTED_PROFILE" ]] ; then
  1355. if [[ -f "$HOME/.profile" ]] ; then
  1356. DETECTED_PROFILE="$HOME/.profile"
  1357. elif [[ -f "$HOME/.bashrc" ]] ; then
  1358. DETECTED_PROFILE="$HOME/.bashrc"
  1359. elif [[ -f "$HOME/.bash_profile" ]] ; then
  1360. DETECTED_PROFILE="$HOME/.bash_profile"
  1361. elif [[ -f "$HOME/.zshrc" ]] ; then
  1362. DETECTED_PROFILE="$HOME/.zshrc"
  1363. fi
  1364. fi
  1365. if [[ ! -z "$DETECTED_PROFILE" ]] ; then
  1366. echo "$DETECTED_PROFILE"
  1367. fi
  1368. }
  1369. _initconf() {
  1370. _initpath
  1371. if [[ ! -f "$ACCOUNT_CONF_PATH" ]] ; then
  1372. echo "#ACCOUNT_CONF_PATH=xxxx
  1373. #Account configurations:
  1374. #Here are the supported macros, uncomment them to make them take effect.
  1375. #ACCOUNT_EMAIL=aaa@aaa.com # the account email used to register account.
  1376. #ACCOUNT_KEY_PATH=\"/path/to/account.key\"
  1377. #STAGE=1 # Use the staging api
  1378. #FORCE=1 # Force to issue cert
  1379. #DEBUG=1 # Debug mode
  1380. #ACCOUNT_KEY_HASH=account key hash
  1381. USER_AGENT=\"le.sh client: $PROJECT\"
  1382. #USER_PATH=""
  1383. #dns api
  1384. #######################
  1385. #Cloudflare:
  1386. #api key
  1387. #CF_Key=\"sdfsdfsdfljlbjkljlkjsdfoiwje\"
  1388. #account email
  1389. #CF_Email=\"xxxx@sss.com\"
  1390. #######################
  1391. #Dnspod.cn:
  1392. #api key id
  1393. #DP_Id=\"1234\"
  1394. #api key
  1395. #DP_Key=\"sADDsdasdgdsf\"
  1396. #######################
  1397. #Cloudxns.com:
  1398. #CX_Key=\"1234\"
  1399. #
  1400. #CX_Secret=\"sADDsdasdgdsf\"
  1401. " > $ACCOUNT_CONF_PATH
  1402. fi
  1403. }
  1404. _precheck() {
  1405. if ! _exists "curl" && ! _exists "wget"; then
  1406. _err "Please install curl or wget first, we need to access http resources."
  1407. return 1
  1408. fi
  1409. if ! _exists "crontab" ; then
  1410. _err "It is recommended to install crontab first. try to install 'cron, crontab, crontabs or vixie-cron'."
  1411. _err "We need to set cron job to renew the certs automatically."
  1412. _err "Otherwise, your certs will not be able to be renewed automatically."
  1413. if [[ -z "$FORCE" ]] ; then
  1414. _err "Please define 'FORCE=1' and try install again to go without crontab."
  1415. _err "FORCE=1 ./le.sh install"
  1416. return 1
  1417. fi
  1418. fi
  1419. if ! _exists "openssl" ; then
  1420. _err "Please install openssl first."
  1421. _err "We need openssl to generate keys."
  1422. return 1
  1423. fi
  1424. if ! _exists "nc" ; then
  1425. _err "It is recommended to install nc first, try to install 'nc' or 'netcat'."
  1426. _err "We use nc for standalone server if you use standalone mode."
  1427. _err "If you don't use standalone mode, just ignore this warning."
  1428. fi
  1429. return 0
  1430. }
  1431. install() {
  1432. if ! _initpath ; then
  1433. _err "Install failed."
  1434. return 1
  1435. fi
  1436. if ! _precheck ; then
  1437. _err "Pre-check failed, can not install."
  1438. return 1
  1439. fi
  1440. _info "Installing to $LE_WORKING_DIR"
  1441. if ! mkdir -p "$LE_WORKING_DIR" ; then
  1442. _err "Can not craete working dir: $LE_WORKING_DIR"
  1443. return 1
  1444. fi
  1445. cp le.sh "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/le.sh"
  1446. if [[ "$?" != "0" ]] ; then
  1447. _err "Install failed, can not copy le.sh"
  1448. return 1
  1449. fi
  1450. _info "Installed to $LE_WORKING_DIR/le.sh"
  1451. _profile="$(_detect_profile)"
  1452. if [[ "$_profile" ]] ; then
  1453. _debug "Found profile: $_profile"
  1454. echo "LE_WORKING_DIR=$LE_WORKING_DIR
  1455. alias le=\"$LE_WORKING_DIR/le.sh\"
  1456. alias le.sh=\"$LE_WORKING_DIR/le.sh\"
  1457. " > "$LE_WORKING_DIR/le.env"
  1458. echo "" >> "$_profile"
  1459. _setopt "$_profile" "source \"$LE_WORKING_DIR/le.env\""
  1460. _info "OK, Close and reopen your terminal to start using le"
  1461. else
  1462. _info "No profile is found, you will need to go into $LE_WORKING_DIR to use le.sh"
  1463. fi
  1464. mkdir -p $LE_WORKING_DIR/dnsapi
  1465. cp dnsapi/* $LE_WORKING_DIR/dnsapi/
  1466. #to keep compatible mv the .acc file to .key file
  1467. if [[ -f "$LE_WORKING_DIR/account.acc" ]] ; then
  1468. mv "$LE_WORKING_DIR/account.acc" "$LE_WORKING_DIR/account.key"
  1469. fi
  1470. if [[ ! -f "$ACCOUNT_CONF_PATH" ]] ; then
  1471. _initconf
  1472. fi
  1473. _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\""
  1474. _setopt "$ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\""
  1475. installcronjob
  1476. _info OK
  1477. }
  1478. uninstall() {
  1479. uninstallcronjob
  1480. _initpath
  1481. _profile="$(_detect_profile)"
  1482. if [[ "$_profile" ]] ; then
  1483. text="$(cat $_profile)"
  1484. echo "$text" | sed "s|^source.*le.env.*$||" > "$_profile"
  1485. fi
  1486. rm -f $LE_WORKING_DIR/le.sh
  1487. _info "The keys and certs are in $LE_WORKING_DIR, you can remove them by yourself."
  1488. }
  1489. cron() {
  1490. IN_CRON=1
  1491. renewAll
  1492. IN_CRON=""
  1493. }
  1494. version() {
  1495. echo "$PROJECT"
  1496. echo "v$VER"
  1497. }
  1498. showhelp() {
  1499. version
  1500. echo "Usage: le.sh command ...[parameters]....
  1501. Commands:
  1502. --help, -h Show this help message.
  1503. --version, -v Show version info.
  1504. --install Install le.sh to your system.
  1505. --uninstall Uninstall le.sh, and uninstall the cron job.
  1506. --issue Issue a cert.
  1507. --installcert Install the issued cert to apache/nginx or any other server.
  1508. --renew, -r Renew a cert.
  1509. --renewAll Renew all the certs
  1510. --revoke Revoke a cert.
  1511. --installcronjob Install the cron job to renew certs, you don't need to call this. The 'install' command can automatically install the cron job.
  1512. --uninstallcronjob Uninstall the cron job. The 'uninstall' command can do this automatically.
  1513. --cron Run cron job to renew all the certs.
  1514. --toPkcs Export the certificate and key to a pfx file.
  1515. --createAccountKey, -cak Create an account private key, professional use.
  1516. --createDomainKey, -cdk Create an domain private key, professional use.
  1517. --createCSR, -ccsr Create CSR , professional use.
  1518. Parameters:
  1519. --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc.
  1520. --force, -f Used to force to install or force to renew a cert immediately.
  1521. --staging, --test Use staging server, just for test.
  1522. --debug Output debug info.
  1523. --webroot, -w /path/to/webroot Specifies the web root folder for web root mode.
  1524. --standalone Use standalone mode.
  1525. --apache Use apache mode.
  1526. --dns [dns-cf|dns-dp|dns-cx|/path/to/api/file] Use dns mode or dns api.
  1527. --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384.
  1528. --accountkeylength, -ak [2048] Specifies the account key length.
  1529. These parameters are to install the cert to nginx/apache or anyother server after issue/renew a cert:
  1530. --certpath /path/to/real/cert/file After issue/renew, the cert will be copied to this path.
  1531. --keypath /path/to/real/key/file After issue/renew, the key will be copied to this path.
  1532. --capath /path/to/real/ca/file After issue/renew, the intermediate cert will be copied to this path.
  1533. --fullchainpath /path/to/fullchain/file After issue/renew, the fullchain cert will be copied to this path.
  1534. --reloadcmd \"service nginx reload\" After issue/renew, it's used to reload the server.
  1535. --accountconf Specifies a customized account config file.
  1536. --leworkingdir Specifies the home dir for le.sh
  1537. "
  1538. }
  1539. _installOnline() {
  1540. _info "Installing from online archive."
  1541. if [[ ! "$BRANCH" ]] ; then
  1542. BRANCH="master"
  1543. fi
  1544. _initpath
  1545. target="$PROJECT/archive/$BRANCH.tar.gz"
  1546. _info "Downloading $target"
  1547. localname="$BRANCH.tar.gz"
  1548. if ! _get "$target" > $localname ; then
  1549. _debug "Download error."
  1550. return 1
  1551. fi
  1552. _info "Extracting $localname"
  1553. tar xzf $localname
  1554. cd "le-$BRANCH"
  1555. chmod +x le.sh
  1556. if ./le.sh install ; then
  1557. _info "Install success!"
  1558. fi
  1559. cd ..
  1560. rm -rf "le-$BRANCH"
  1561. rm -f "$localname"
  1562. }
  1563. _process() {
  1564. _CMD=""
  1565. _domain=""
  1566. _altdomains="no"
  1567. _webroot=""
  1568. _keylength="no"
  1569. _accountkeylength="no"
  1570. _certpath="no"
  1571. _keypath="no"
  1572. _capath="no"
  1573. _fullchainpath="no"
  1574. _reloadcmd="no"
  1575. _password=""
  1576. while (( ${#} )); do
  1577. case "${1}" in
  1578. --help|-h)
  1579. showhelp
  1580. return
  1581. ;;
  1582. --version|-v)
  1583. version
  1584. return
  1585. ;;
  1586. --install)
  1587. _CMD="install"
  1588. ;;
  1589. --uninstall)
  1590. _CMD="uninstall"
  1591. ;;
  1592. --issue)
  1593. _CMD="issue"
  1594. ;;
  1595. --installcert|-i)
  1596. _CMD="installcert"
  1597. ;;
  1598. --renew|-r)
  1599. _CMD="renew"
  1600. ;;
  1601. --renewAll|-renewall)
  1602. _CMD="renewAll"
  1603. ;;
  1604. --revoke)
  1605. _CMD="revoke"
  1606. ;;
  1607. --installcronjob)
  1608. _CMD="installcronjob"
  1609. ;;
  1610. --uninstallcronjob)
  1611. _CMD="uninstallcronjob"
  1612. ;;
  1613. --cron)
  1614. _CMD="cron"
  1615. ;;
  1616. --toPkcs)
  1617. _CMD="toPkcs"
  1618. ;;
  1619. --createAccountKey|--createaccountkey|-cak)
  1620. _CMD="createAccountKey"
  1621. ;;
  1622. --createDomainKey|--createdomainkey|-cdk)
  1623. _CMD="createDomainKey"
  1624. ;;
  1625. --createCSR|--createcsr|-ccr)
  1626. _CMD="createCSR"
  1627. ;;
  1628. --domain|-d)
  1629. _dvalue="$2"
  1630. if [[ -z "$_dvalue" ]] || [[ "$_dvalue" == "-"* ]] ; then
  1631. _err "'$_dvalue' is not a valid domain for parameter '$1'"
  1632. return 1
  1633. fi
  1634. if [[ -z "$_domain" ]] ; then
  1635. _domain="$_dvalue"
  1636. else
  1637. if [[ "$_altdomains" == "no" ]] ; then
  1638. _altdomains="$_dvalue"
  1639. else
  1640. _altdomains="$_altdomains,$_dvalue"
  1641. fi
  1642. fi
  1643. shift
  1644. ;;
  1645. --force|-f)
  1646. FORCE="1"
  1647. ;;
  1648. --staging|--test)
  1649. STAGE="1"
  1650. ;;
  1651. --debug)
  1652. if [[ "$2" == "-"* ]] || [[ -z "$2" ]]; then
  1653. DEBUG="1"
  1654. else
  1655. DEBUG="$2"
  1656. shift
  1657. fi
  1658. ;;
  1659. --webroot|-w)
  1660. wvalue="$2"
  1661. if [[ -z "$_webroot" ]] ; then
  1662. _webroot="$wvalue"
  1663. else
  1664. _webroot="$_webroot,$wvalue"
  1665. fi
  1666. shift
  1667. ;;
  1668. --standalone)
  1669. wvalue="no"
  1670. if [[ -z "$_webroot" ]] ; then
  1671. _webroot="$wvalue"
  1672. else
  1673. _webroot="$_webroot,$wvalue"
  1674. fi
  1675. ;;
  1676. --apache)
  1677. wvalue="apache"
  1678. if [[ -z "$_webroot" ]] ; then
  1679. _webroot="$wvalue"
  1680. else
  1681. _webroot="$_webroot,$wvalue"
  1682. fi
  1683. ;;
  1684. --dns)
  1685. wvalue="dns"
  1686. if [[ "$2" != "-"* ]] ; then
  1687. wvalue="$2"
  1688. shift
  1689. fi
  1690. if [[ -z "$_webroot" ]] ; then
  1691. _webroot="$wvalue"
  1692. else
  1693. _webroot="$_webroot,$wvalue"
  1694. fi
  1695. ;;
  1696. --keylength|-k)
  1697. _keylength="$2"
  1698. accountkeylength="$2"
  1699. shift
  1700. ;;
  1701. --accountkeylength|-ak)
  1702. accountkeylength="$2"
  1703. shift
  1704. ;;
  1705. --certpath)
  1706. _certpath="$2"
  1707. shift
  1708. ;;
  1709. --keypath)
  1710. _keypath="$2"
  1711. shift
  1712. ;;
  1713. --capath)
  1714. _capath="$2"
  1715. shift
  1716. ;;
  1717. --fullchainpath)
  1718. _fullchainpath="$2"
  1719. shift
  1720. ;;
  1721. --reloadcmd)
  1722. _reloadcmd="$2"
  1723. shift
  1724. ;;
  1725. --password)
  1726. _password="$2"
  1727. shift
  1728. ;;
  1729. --accountconf)
  1730. ACCOUNT_CONF_PATH="$2"
  1731. ;;
  1732. --leworkingdir)
  1733. LE_WORKING_DIR="$2"
  1734. ;;
  1735. *)
  1736. _err "Unknown parameter : $1"
  1737. return 1
  1738. ;;
  1739. esac
  1740. shift 1
  1741. done
  1742. case "${_CMD}" in
  1743. install) install ;;
  1744. uninstall) uninstall ;;
  1745. issue)
  1746. issue "$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_certpath" "$_keylength" "$_capath" "$_reloadcmd" "$_fullchainpath"
  1747. ;;
  1748. installcert)
  1749. installcert "$_domain" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath"
  1750. ;;
  1751. renew)
  1752. renew "$_domain"
  1753. ;;
  1754. renewAll)
  1755. renewAll
  1756. ;;
  1757. revoke)
  1758. revoke "$_domain"
  1759. ;;
  1760. installcronjob) installcronjob ;;
  1761. uninstallcronjob) uninstallcronjob ;;
  1762. cron) cron ;;
  1763. toPkcs)
  1764. toPkcs "$_domain" "$_password"
  1765. ;;
  1766. createAccountKey)
  1767. createAccountKey "$_domain" "$_accountkeylength"
  1768. ;;
  1769. createDomainKey)
  1770. createDomainKey "$_domain" "$_keylength"
  1771. ;;
  1772. createCSR)
  1773. createCSR "$_domain" "$_altdomains"
  1774. ;;
  1775. *)
  1776. _err "Invalid command: $_CMD"
  1777. showhelp;
  1778. return 1
  1779. ;;
  1780. esac
  1781. }
  1782. if [[ "$INSTALLONLINE" ]] ; then
  1783. INSTALLONLINE=""
  1784. _installOnline $BRANCH
  1785. exit
  1786. fi
  1787. if [[ -z "$1" ]] ; then
  1788. showhelp
  1789. else
  1790. if [[ "$1" == "-"* ]] ; then
  1791. _process "$@"
  1792. else
  1793. "$@"
  1794. fi
  1795. fi