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.

2105 lines
52 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
  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. nchelp="$(nc -h 2>&1)"
  519. if echo "$nchelp" | grep "\-q[ ,]" >/dev/null ; then
  520. _NC="nc -q 1 -l"
  521. else
  522. if echo "$nchelp" | grep "GNU netcat" >/dev/null && echo "$nchelp" | grep "\-c, \-\-close" >/dev/null ; then
  523. _NC="nc -c -l"
  524. elif echo "$nchelp" | grep "\-N" |grep "Shutdown the network socket after EOF on stdin" >/dev/null ; then
  525. _NC="nc -N -l"
  526. else
  527. _NC="nc -l"
  528. fi
  529. fi
  530. _debug "_NC" "$_NC"
  531. # while true ; do
  532. if [[ "$DEBUG" ]] ; then
  533. if ! echo -e -n "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort -vv ; then
  534. echo -e -n "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort -vv ;
  535. fi
  536. else
  537. if ! echo -e -n "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort > /dev/null 2>&1; then
  538. echo -e -n "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort > /dev/null 2>&1
  539. fi
  540. fi
  541. if [[ "$?" != "0" ]] ; then
  542. _err "nc listen error."
  543. return 1
  544. fi
  545. # done
  546. }
  547. _stopserver() {
  548. pid="$1"
  549. }
  550. _initpath() {
  551. if [[ -z "$LE_WORKING_DIR" ]] ; then
  552. LE_WORKING_DIR=$HOME/.le
  553. fi
  554. _DEFAULT_ACCOUNT_CONF_PATH="$LE_WORKING_DIR/account.conf"
  555. if [[ -f "$_DEFAULT_ACCOUNT_CONF_PATH" ]] ; then
  556. source "$_DEFAULT_ACCOUNT_CONF_PATH"
  557. fi
  558. if [[ -z "$ACCOUNT_CONF_PATH" ]] ; then
  559. ACCOUNT_CONF_PATH="$_DEFAULT_ACCOUNT_CONF_PATH"
  560. fi
  561. if [[ -f "$ACCOUNT_CONF_PATH" ]] ; then
  562. source "$ACCOUNT_CONF_PATH"
  563. fi
  564. if [[ "$IN_CRON" ]] ; then
  565. if [[ ! "$_USER_PATH_EXPORTED" ]] ; then
  566. _USER_PATH_EXPORTED=1
  567. export PATH="$USER_PATH:$PATH"
  568. fi
  569. fi
  570. if [[ -z "$API" ]] ; then
  571. if [[ -z "$STAGE" ]] ; then
  572. API="$DEFAULT_CA"
  573. else
  574. API="$STAGE_CA"
  575. _info "Using stage api:$API"
  576. fi
  577. fi
  578. if [[ -z "$ACME_DIR" ]] ; then
  579. ACME_DIR="/home/.acme"
  580. fi
  581. if [[ -z "$APACHE_CONF_BACKUP_DIR" ]] ; then
  582. APACHE_CONF_BACKUP_DIR="$LE_WORKING_DIR"
  583. fi
  584. if [[ -z "$USER_AGENT" ]] ; then
  585. USER_AGENT="$DEFAULT_USER_AGENT"
  586. fi
  587. HTTP_HEADER="$LE_WORKING_DIR/http.header"
  588. WGET="wget -q"
  589. if [[ "$DEBUG" -ge "2" ]] ; then
  590. WGET="$WGET -d "
  591. fi
  592. dp="$LE_WORKING_DIR/curl.dump"
  593. CURL="curl -L --silent"
  594. if [[ "$DEBUG" -ge "2" ]] ; then
  595. CURL="$CURL -L --trace-ascii $dp "
  596. fi
  597. domain="$1"
  598. if [[ -z "$ACCOUNT_KEY_PATH" ]] ; then
  599. ACCOUNT_KEY_PATH="$LE_WORKING_DIR/account.key"
  600. fi
  601. if [[ -z "$domain" ]] ; then
  602. return 0
  603. fi
  604. domainhome="$LE_WORKING_DIR/$domain"
  605. mkdir -p "$domainhome"
  606. if [[ -z "$DOMAIN_PATH" ]] ; then
  607. DOMAIN_PATH="$domainhome"
  608. fi
  609. if [[ -z "$DOMAIN_CONF" ]] ; then
  610. DOMAIN_CONF="$domainhome/$domain.conf"
  611. fi
  612. if [[ -z "$DOMAIN_SSL_CONF" ]] ; then
  613. DOMAIN_SSL_CONF="$domainhome/$domain.ssl.conf"
  614. fi
  615. if [[ -z "$CSR_PATH" ]] ; then
  616. CSR_PATH="$domainhome/$domain.csr"
  617. fi
  618. if [[ -z "$CERT_KEY_PATH" ]] ; then
  619. CERT_KEY_PATH="$domainhome/$domain.key"
  620. fi
  621. if [[ -z "$CERT_PATH" ]] ; then
  622. CERT_PATH="$domainhome/$domain.cer"
  623. fi
  624. if [[ -z "$CA_CERT_PATH" ]] ; then
  625. CA_CERT_PATH="$domainhome/ca.cer"
  626. fi
  627. if [[ -z "$CERT_FULLCHAIN_PATH" ]] ; then
  628. CERT_FULLCHAIN_PATH="$domainhome/fullchain.cer"
  629. fi
  630. if [[ -z "$CERT_PFX_PATH" ]] ; then
  631. CERT_PFX_PATH="$domainhome/$domain.pfx"
  632. fi
  633. }
  634. _apachePath() {
  635. httpdconfname="$(apachectl -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"' )"
  636. if [[ "$httpdconfname" == '/'* ]] ; then
  637. httpdconf="$httpdconfname"
  638. httpdconfname="$(basename $httpdconfname)"
  639. else
  640. httpdroot="$(apachectl -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"' )"
  641. httpdconf="$httpdroot/$httpdconfname"
  642. fi
  643. if [[ ! -f $httpdconf ]] ; then
  644. _err "Apache Config file not found" $httpdconf
  645. return 1
  646. fi
  647. return 0
  648. }
  649. _restoreApache() {
  650. if [[ -z "$usingApache" ]] ; then
  651. return 0
  652. fi
  653. _initpath
  654. if ! _apachePath ; then
  655. return 1
  656. fi
  657. if [[ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ]] ; then
  658. _debug "No config file to restore."
  659. return 0
  660. fi
  661. cp -p "$APACHE_CONF_BACKUP_DIR/$httpdconfname" "$httpdconf"
  662. if ! apachectl -t ; then
  663. _err "Sorry, restore apache config error, please contact me."
  664. return 1;
  665. fi
  666. rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname"
  667. return 0
  668. }
  669. _setApache() {
  670. _initpath
  671. if ! _apachePath ; then
  672. return 1
  673. fi
  674. #backup the conf
  675. _debug "Backup apache config file" $httpdconf
  676. cp -p $httpdconf $APACHE_CONF_BACKUP_DIR/
  677. _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname"
  678. _info "In case there is an error that can not be restored automatically, you may try restore it yourself."
  679. _info "The backup file will be deleted on sucess, just forget it."
  680. #add alias
  681. apacheVer="$(apachectl -V | grep "Server version:" | cut -d : -f 2 | cut -d " " -f 2 | cut -d '/' -f 2 )"
  682. _debug "apacheVer" "$apacheVer"
  683. apacheMajer="$(echo "$apacheVer" | cut -d . -f 1)"
  684. apacheMinor="$(echo "$apacheVer" | cut -d . -f 2)"
  685. if [[ "$apacheVer" ]] && [[ "$apacheMajer" -ge "2" ]] && [[ "$apacheMinor" -ge "4" ]] ; then
  686. echo "
  687. Alias /.well-known/acme-challenge $ACME_DIR
  688. <Directory $ACME_DIR >
  689. Require all granted
  690. </Directory>
  691. " >> $httpdconf
  692. else
  693. echo "
  694. Alias /.well-known/acme-challenge $ACME_DIR
  695. <Directory $ACME_DIR >
  696. Order allow,deny
  697. Allow from all
  698. </Directory>
  699. " >> $httpdconf
  700. fi
  701. if ! apachectl -t ; then
  702. _err "Sorry, apache config error, please contact me."
  703. _restoreApache
  704. return 1;
  705. fi
  706. if [[ ! -d "$ACME_DIR" ]] ; then
  707. mkdir -p "$ACME_DIR"
  708. chmod 755 "$ACME_DIR"
  709. fi
  710. if ! apachectl graceful ; then
  711. _err "Sorry, apachectl graceful error, please contact me."
  712. _restoreApache
  713. return 1;
  714. fi
  715. usingApache="1"
  716. return 0
  717. }
  718. _clearup () {
  719. _stopserver $serverproc
  720. serverproc=""
  721. _restoreApache
  722. }
  723. # webroot removelevel tokenfile
  724. _clearupwebbroot() {
  725. __webroot="$1"
  726. if [[ -z "$__webroot" ]] ; then
  727. _debug "no webroot specified, skip"
  728. return 0
  729. fi
  730. if [[ "$2" == '1' ]] ; then
  731. _debug "remove $__webroot/.well-known"
  732. rm -rf "$__webroot/.well-known"
  733. elif [[ "$2" == '2' ]] ; then
  734. _debug "remove $__webroot/.well-known/acme-challenge"
  735. rm -rf "$__webroot/.well-known/acme-challenge"
  736. elif [[ "$2" == '3' ]] ; then
  737. _debug "remove $__webroot/.well-known/acme-challenge/$3"
  738. rm -rf "$__webroot/.well-known/acme-challenge/$3"
  739. else
  740. _info "Skip for removelevel:$2"
  741. fi
  742. return 0
  743. }
  744. issue() {
  745. if [[ -z "$2" ]] ; then
  746. echo "Usage: le --issue -d a.com -w /path/to/webroot/a.com/ "
  747. return 1
  748. fi
  749. Le_Webroot="$1"
  750. Le_Domain="$2"
  751. Le_Alt="$3"
  752. Le_Keylength="$4"
  753. Le_RealCertPath="$5"
  754. Le_RealKeyPath="$6"
  755. Le_RealCACertPath="$7"
  756. Le_ReloadCmd="$8"
  757. Le_RealFullChainPath="$9"
  758. _initpath $Le_Domain
  759. if [[ -f "$DOMAIN_CONF" ]] ; then
  760. Le_NextRenewTime=$(grep "^Le_NextRenewTime=" "$DOMAIN_CONF" | cut -d '=' -f 2)
  761. if [[ -z "$FORCE" ]] && [[ "$Le_NextRenewTime" ]] && [[ "$(date -u "+%s" )" -lt "$Le_NextRenewTime" ]] ; then
  762. _info "Skip, Next renewal time is: $(grep "^Le_NextRenewTimeStr" "$DOMAIN_CONF" | cut -d '=' -f 2)"
  763. return 2
  764. fi
  765. fi
  766. _setopt "$DOMAIN_CONF" "Le_Domain" "=" "$Le_Domain"
  767. _setopt "$DOMAIN_CONF" "Le_Alt" "=" "$Le_Alt"
  768. _setopt "$DOMAIN_CONF" "Le_Webroot" "=" "$Le_Webroot"
  769. _setopt "$DOMAIN_CONF" "Le_Keylength" "=" "$Le_Keylength"
  770. _setopt "$DOMAIN_CONF" "Le_RealCertPath" "=" "\"$Le_RealCertPath\""
  771. _setopt "$DOMAIN_CONF" "Le_RealCACertPath" "=" "\"$Le_RealCACertPath\""
  772. _setopt "$DOMAIN_CONF" "Le_RealKeyPath" "=" "\"$Le_RealKeyPath\""
  773. _setopt "$DOMAIN_CONF" "Le_ReloadCmd" "=" "\"$Le_ReloadCmd\""
  774. _setopt "$DOMAIN_CONF" "Le_RealFullChainPath" "=" "\"$Le_RealFullChainPath\""
  775. if [[ "$Le_Alt" == "no" ]] ; then
  776. Le_Alt=""
  777. fi
  778. if [[ "$Le_Keylength" == "no" ]] ; then
  779. Le_Keylength=""
  780. fi
  781. if [[ "$Le_RealCertPath" == "no" ]] ; then
  782. Le_RealCertPath=""
  783. fi
  784. if [[ "$Le_RealKeyPath" == "no" ]] ; then
  785. Le_RealKeyPath=""
  786. fi
  787. if [[ "$Le_RealCACertPath" == "no" ]] ; then
  788. Le_RealCACertPath=""
  789. fi
  790. if [[ "$Le_ReloadCmd" == "no" ]] ; then
  791. Le_ReloadCmd=""
  792. fi
  793. if [[ "$Le_RealFullChainPath" == "no" ]] ; then
  794. Le_RealFullChainPath=""
  795. fi
  796. if [[ "$Le_Webroot" == *"no"* ]] ; then
  797. _info "Standalone mode."
  798. if ! command -v "nc" > /dev/null ; then
  799. _err "Please install netcat(nc) tools first."
  800. return 1
  801. fi
  802. if [[ -z "$Le_HTTPPort" ]] ; then
  803. Le_HTTPPort=80
  804. fi
  805. _setopt "$DOMAIN_CONF" "Le_HTTPPort" "=" "$Le_HTTPPort"
  806. netprc="$(_ss "$Le_HTTPPort" | grep "$Le_HTTPPort")"
  807. if [[ "$netprc" ]] ; then
  808. _err "$netprc"
  809. _err "tcp port $Le_HTTPPort is already used by $(echo "$netprc" | cut -d : -f 4)"
  810. _err "Please stop it first"
  811. return 1
  812. fi
  813. fi
  814. if [[ "$Le_Webroot" == *"apache"* ]] ; then
  815. if ! _setApache ; then
  816. _err "set up apache error. Report error to me."
  817. return 1
  818. fi
  819. wellknown_path="$ACME_DIR"
  820. else
  821. usingApache=""
  822. fi
  823. createAccountKey $Le_Domain $Le_Keylength
  824. if ! _calcjwk "$ACCOUNT_KEY_PATH" ; then
  825. return 1
  826. fi
  827. accountkey_json=$(echo -n "$jwk" | tr -d ' ' )
  828. thumbprint=$(echo -n "$accountkey_json" | _digest "sha256" | _urlencode)
  829. accountkeyhash="$(cat "$ACCOUNT_KEY_PATH" | _digest "sha256" )"
  830. accountkeyhash="$(echo $accountkeyhash$API | _digest "sha256" )"
  831. if [[ "$accountkeyhash" != "$ACCOUNT_KEY_HASH" ]] ; then
  832. _info "Registering account"
  833. regjson='{"resource": "new-reg", "agreement": "'$AGREEMENT'"}'
  834. if [[ "$ACCOUNT_EMAIL" ]] ; then
  835. regjson='{"resource": "new-reg", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "agreement": "'$AGREEMENT'"}'
  836. fi
  837. _send_signed_request "$API/acme/new-reg" "$regjson"
  838. if [[ "$code" == "" ]] || [[ "$code" == '201' ]] ; then
  839. _info "Registered"
  840. echo $response > $LE_WORKING_DIR/account.json
  841. elif [[ "$code" == '409' ]] ; then
  842. _info "Already registered"
  843. else
  844. _err "Register account Error: $response"
  845. _clearup
  846. return 1
  847. fi
  848. ACCOUNT_KEY_HASH="$accountkeyhash"
  849. _saveaccountconf "ACCOUNT_KEY_HASH" "$ACCOUNT_KEY_HASH"
  850. else
  851. _info "Skip register account key"
  852. fi
  853. if ! createDomainKey $Le_Domain $Le_Keylength ; then
  854. _err "Create domain key error."
  855. return 1
  856. fi
  857. if ! createCSR $Le_Domain $Le_Alt ; then
  858. _err "Create CSR error."
  859. return 1
  860. fi
  861. vlist="$Le_Vlist"
  862. # verify each domain
  863. _info "Verify each domain"
  864. sep='#'
  865. if [[ -z "$vlist" ]] ; then
  866. alldomains=$(echo "$Le_Domain,$Le_Alt" | tr ',' ' ' )
  867. _index=1
  868. _currentRoot=""
  869. for d in $alldomains
  870. do
  871. _info "Getting webroot for domain" $d
  872. _w="$(echo $Le_Webroot | cut -d , -f $_index)"
  873. _debug _w "$_w"
  874. if [[ "$_w" ]] ; then
  875. _currentRoot="$_w"
  876. fi
  877. _debug "_currentRoot" "$_currentRoot"
  878. let "_index+=1"
  879. vtype="$VTYPE_HTTP"
  880. if [[ "$_currentRoot" == "dns"* ]] ; then
  881. vtype="$VTYPE_DNS"
  882. fi
  883. _info "Getting token for domain" $d
  884. _send_signed_request "$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$d\"}}"
  885. if [[ ! -z "$code" ]] && [[ ! "$code" == '201' ]] ; then
  886. _err "new-authz error: $response"
  887. _clearup
  888. return 1
  889. fi
  890. entry="$(printf $response | egrep -o '\{[^{]*"type":"'$vtype'"[^}]*')"
  891. _debug entry "$entry"
  892. token="$(printf "$entry" | egrep -o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
  893. _debug token $token
  894. uri="$(printf "$entry" | egrep -o '"uri":"[^"]*'| cut -d : -f 2,3 | tr -d '"' )"
  895. _debug uri $uri
  896. keyauthorization="$token.$thumbprint"
  897. _debug keyauthorization "$keyauthorization"
  898. dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot"
  899. _debug dvlist "$dvlist"
  900. vlist="$vlist$dvlist,"
  901. done
  902. #add entry
  903. dnsadded=""
  904. ventries=$(echo "$vlist" | tr ',' ' ' )
  905. for ventry in $ventries
  906. do
  907. d=$(echo $ventry | cut -d $sep -f 1)
  908. keyauthorization=$(echo $ventry | cut -d $sep -f 2)
  909. vtype=$(echo $ventry | cut -d $sep -f 4)
  910. _currentRoot=$(echo $ventry | cut -d $sep -f 5)
  911. if [[ "$vtype" == "$VTYPE_DNS" ]] ; then
  912. dnsadded='0'
  913. txtdomain="_acme-challenge.$d"
  914. _debug txtdomain "$txtdomain"
  915. txt="$(echo -e -n $keyauthorization | _digest "sha256" | _urlencode)"
  916. _debug txt "$txt"
  917. #dns
  918. #1. check use api
  919. d_api=""
  920. if [[ -f "$LE_WORKING_DIR/$d/$_currentRoot" ]] ; then
  921. d_api="$LE_WORKING_DIR/$d/$_currentRoot"
  922. elif [[ -f "$LE_WORKING_DIR/$d/$_currentRoot.sh" ]] ; then
  923. d_api="$LE_WORKING_DIR/$d/$_currentRoot.sh"
  924. elif [[ -f "$LE_WORKING_DIR/$_currentRoot" ]] ; then
  925. d_api="$LE_WORKING_DIR/$_currentRoot"
  926. elif [[ -f "$LE_WORKING_DIR/$_currentRoot.sh" ]] ; then
  927. d_api="$LE_WORKING_DIR/$_currentRoot.sh"
  928. elif [[ -f "$LE_WORKING_DIR/dnsapi/$_currentRoot" ]] ; then
  929. d_api="$LE_WORKING_DIR/dnsapi/$_currentRoot"
  930. elif [[ -f "$LE_WORKING_DIR/dnsapi/$_currentRoot.sh" ]] ; then
  931. d_api="$LE_WORKING_DIR/dnsapi/$_currentRoot.sh"
  932. fi
  933. _debug d_api "$d_api"
  934. if [[ "$d_api" ]] ; then
  935. _info "Found domain api file: $d_api"
  936. else
  937. _err "Add the following TXT record:"
  938. _err "Domain: $txtdomain"
  939. _err "TXT value: $txt"
  940. _err "Please be aware that you prepend _acme-challenge. before your domain"
  941. _err "so the resulting subdomain will be: $txtdomain"
  942. continue
  943. fi
  944. (
  945. if ! source $d_api ; then
  946. _err "Load file $d_api error. Please check your api file and try again."
  947. return 1
  948. fi
  949. addcommand="$_currentRoot-add"
  950. if ! _exists $addcommand ; then
  951. _err "It seems that your api file is not correct, it must have a function named: $addcommand"
  952. return 1
  953. fi
  954. if ! $addcommand $txtdomain $txt ; then
  955. _err "Error add txt for domain:$txtdomain"
  956. return 1
  957. fi
  958. )
  959. if [[ "$?" != "0" ]] ; then
  960. return 1
  961. fi
  962. dnsadded='1'
  963. fi
  964. done
  965. if [[ "$dnsadded" == '0' ]] ; then
  966. _setopt "$DOMAIN_CONF" "Le_Vlist" "=" "\"$vlist\""
  967. _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit."
  968. _err "Please add the TXT records to the domains, and retry again."
  969. return 1
  970. fi
  971. fi
  972. if [[ "$dnsadded" == '1' ]] ; then
  973. _info "Sleep 60 seconds for the txt records to take effect"
  974. sleep 60
  975. fi
  976. _debug "ok, let's start to verify"
  977. ventries=$(echo "$vlist" | tr ',' ' ' )
  978. for ventry in $ventries
  979. do
  980. d=$(echo $ventry | cut -d $sep -f 1)
  981. keyauthorization=$(echo $ventry | cut -d $sep -f 2)
  982. uri=$(echo $ventry | cut -d $sep -f 3)
  983. vtype=$(echo $ventry | cut -d $sep -f 4)
  984. _currentRoot=$(echo $ventry | cut -d $sep -f 5)
  985. _info "Verifying:$d"
  986. _debug "d" "$d"
  987. _debug "keyauthorization" "$keyauthorization"
  988. _debug "uri" "$uri"
  989. removelevel=""
  990. token=""
  991. _debug "_currentRoot" "$_currentRoot"
  992. if [[ "$vtype" == "$VTYPE_HTTP" ]] ; then
  993. if [[ "$_currentRoot" == "no" ]] ; then
  994. _info "Standalone mode server"
  995. _startserver "$keyauthorization" &
  996. serverproc="$!"
  997. sleep 2
  998. _debug serverproc $serverproc
  999. else
  1000. if [[ -z "$wellknown_path" ]] ; then
  1001. wellknown_path="$_currentRoot/.well-known/acme-challenge"
  1002. fi
  1003. _debug wellknown_path "$wellknown_path"
  1004. if [[ ! -d "$_currentRoot/.well-known" ]] ; then
  1005. removelevel='1'
  1006. elif [[ ! -d "$_currentRoot/.well-known/acme-challenge" ]] ; then
  1007. removelevel='2'
  1008. else
  1009. removelevel='3'
  1010. fi
  1011. token="$(echo -e -n "$keyauthorization" | cut -d '.' -f 1)"
  1012. _debug "writing token:$token to $wellknown_path/$token"
  1013. mkdir -p "$wellknown_path"
  1014. echo -n "$keyauthorization" > "$wellknown_path/$token"
  1015. if [[ ! "$usingApache" ]] ; then
  1016. webroot_owner=$(_stat $_currentRoot)
  1017. _debug "Changing owner/group of .well-known to $webroot_owner"
  1018. chown -R $webroot_owner "$_currentRoot/.well-known"
  1019. fi
  1020. fi
  1021. fi
  1022. _send_signed_request $uri "{\"resource\": \"challenge\", \"keyAuthorization\": \"$keyauthorization\"}"
  1023. if [[ ! -z "$code" ]] && [[ ! "$code" == '202' ]] ; then
  1024. _err "$d:Challenge error: $response"
  1025. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  1026. _clearup
  1027. return 1
  1028. fi
  1029. while [[ "1" ]] ; do
  1030. _debug "sleep 5 secs to verify"
  1031. sleep 5
  1032. _debug "checking"
  1033. response="$(_get $uri)"
  1034. if [[ "$?" != "0" ]] ; then
  1035. _err "$d:Verify error:$response"
  1036. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  1037. _clearup
  1038. return 1
  1039. fi
  1040. status=$(echo $response | egrep -o '"status":"[^"]*' | cut -d : -f 2 | tr -d '"')
  1041. if [[ "$status" == "valid" ]] ; then
  1042. _info "Success"
  1043. _stopserver $serverproc
  1044. serverproc=""
  1045. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  1046. break;
  1047. fi
  1048. if [[ "$status" == "invalid" ]] ; then
  1049. error=$(echo $response | egrep -o '"error":{[^}]*}' | grep -o '"detail":"[^"]*"' | cut -d '"' -f 4)
  1050. _err "$d:Verify error:$error"
  1051. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  1052. _clearup
  1053. return 1;
  1054. fi
  1055. if [[ "$status" == "pending" ]] ; then
  1056. _info "Pending"
  1057. else
  1058. _err "$d:Verify error:$response"
  1059. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  1060. _clearup
  1061. return 1
  1062. fi
  1063. done
  1064. done
  1065. _clearup
  1066. _info "Verify finished, start to sign."
  1067. der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _urlencode)"
  1068. _send_signed_request "$API/acme/new-cert" "{\"resource\": \"new-cert\", \"csr\": \"$der\"}" "needbase64"
  1069. Le_LinkCert="$(grep -i -o '^Location.*$' $HTTP_HEADER | head -1 | tr -d "\r\n" | cut -d " " -f 2)"
  1070. _setopt "$DOMAIN_CONF" "Le_LinkCert" "=" "$Le_LinkCert"
  1071. if [[ "$Le_LinkCert" ]] ; then
  1072. echo "$BEGIN_CERT" > "$CERT_PATH"
  1073. _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH"
  1074. echo "$END_CERT" >> "$CERT_PATH"
  1075. _info "Cert success."
  1076. cat "$CERT_PATH"
  1077. _info "Your cert is in $CERT_PATH"
  1078. cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH"
  1079. if [[ ! "$USER_PATH" ]] || [[ ! "$IN_CRON" ]] ; then
  1080. USER_PATH="$PATH"
  1081. _saveaccountconf "USER_PATH" "$USER_PATH"
  1082. fi
  1083. fi
  1084. if [[ -z "$Le_LinkCert" ]] ; then
  1085. response="$(echo $response | _dbase64 "multiline" )"
  1086. _err "Sign failed: $(echo "$response" | grep -o '"detail":"[^"]*"')"
  1087. return 1
  1088. fi
  1089. _setopt "$DOMAIN_CONF" 'Le_Vlist' '=' "\"\""
  1090. Le_LinkIssuer=$(grep -i '^Link' $HTTP_HEADER | head -1 | cut -d " " -f 2| cut -d ';' -f 1 | tr -d '<>' )
  1091. _setopt "$DOMAIN_CONF" "Le_LinkIssuer" "=" "$Le_LinkIssuer"
  1092. if [[ "$Le_LinkIssuer" ]] ; then
  1093. echo "$BEGIN_CERT" > "$CA_CERT_PATH"
  1094. _get "$Le_LinkIssuer" | _base64 "multiline" >> "$CA_CERT_PATH"
  1095. echo "$END_CERT" >> "$CA_CERT_PATH"
  1096. _info "The intermediate CA cert is in $CA_CERT_PATH"
  1097. cat "$CA_CERT_PATH" >> "$CERT_FULLCHAIN_PATH"
  1098. _info "And the full chain certs is there: $CERT_FULLCHAIN_PATH"
  1099. fi
  1100. Le_CertCreateTime=$(date -u "+%s")
  1101. _setopt "$DOMAIN_CONF" "Le_CertCreateTime" "=" "$Le_CertCreateTime"
  1102. Le_CertCreateTimeStr=$(date -u )
  1103. _setopt "$DOMAIN_CONF" "Le_CertCreateTimeStr" "=" "\"$Le_CertCreateTimeStr\""
  1104. if [[ ! "$Le_RenewalDays" ]] ; then
  1105. Le_RenewalDays=80
  1106. fi
  1107. _setopt "$DOMAIN_CONF" "Le_RenewalDays" "=" "$Le_RenewalDays"
  1108. let "Le_NextRenewTime=Le_CertCreateTime+Le_RenewalDays*24*60*60"
  1109. _setopt "$DOMAIN_CONF" "Le_NextRenewTime" "=" "$Le_NextRenewTime"
  1110. Le_NextRenewTimeStr=$( _time2str $Le_NextRenewTime )
  1111. _setopt "$DOMAIN_CONF" "Le_NextRenewTimeStr" "=" "\"$Le_NextRenewTimeStr\""
  1112. installcert $Le_Domain "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd" "$Le_RealFullChainPath"
  1113. }
  1114. renew() {
  1115. Le_Domain="$1"
  1116. if [[ -z "$Le_Domain" ]] ; then
  1117. _err "Usage: le.sh --renew -d domain.com"
  1118. return 1
  1119. fi
  1120. _initpath $Le_Domain
  1121. if [[ ! -f "$DOMAIN_CONF" ]] ; then
  1122. _info "$Le_Domain is not a issued domain, skip."
  1123. return 0;
  1124. fi
  1125. source "$DOMAIN_CONF"
  1126. if [[ -z "$FORCE" ]] && [[ "$Le_NextRenewTime" ]] && [[ "$(date -u "+%s" )" -lt "$Le_NextRenewTime" ]] ; then
  1127. _info "Skip, Next renewal time is: $Le_NextRenewTimeStr"
  1128. return 2
  1129. fi
  1130. IS_RENEW="1"
  1131. issue "$Le_Webroot" "$Le_Domain" "$Le_Alt" "$Le_Keylength" "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd" "$Le_RealFullChainPath"
  1132. local res=$?
  1133. IS_RENEW=""
  1134. return $res
  1135. }
  1136. renewAll() {
  1137. _initpath
  1138. _info "renewAll"
  1139. for d in $(ls -F ${LE_WORKING_DIR}/ | grep [^.].*[.].*/$ ) ; do
  1140. d=$(echo $d | cut -d '/' -f 1)
  1141. _info "renew $d"
  1142. Le_LinkCert=""
  1143. Le_Domain=""
  1144. Le_Alt="no"
  1145. Le_Webroot=""
  1146. Le_Keylength=""
  1147. Le_LinkIssuer=""
  1148. Le_CertCreateTime=""
  1149. Le_CertCreateTimeStr=""
  1150. Le_RenewalDays=""
  1151. Le_NextRenewTime=""
  1152. Le_NextRenewTimeStr=""
  1153. Le_RealCertPath=""
  1154. Le_RealKeyPath=""
  1155. Le_RealCACertPath=""
  1156. Le_ReloadCmd=""
  1157. Le_RealFullChainPath=""
  1158. DOMAIN_PATH=""
  1159. DOMAIN_CONF=""
  1160. DOMAIN_SSL_CONF=""
  1161. CSR_PATH=""
  1162. CERT_KEY_PATH=""
  1163. CERT_PATH=""
  1164. CA_CERT_PATH=""
  1165. CERT_PFX_PATH=""
  1166. CERT_FULLCHAIN_PATH=""
  1167. ACCOUNT_KEY_PATH=""
  1168. wellknown_path=""
  1169. renew "$d"
  1170. done
  1171. }
  1172. installcert() {
  1173. Le_Domain="$1"
  1174. if [[ -z "$Le_Domain" ]] ; then
  1175. 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]"
  1176. return 1
  1177. fi
  1178. Le_RealCertPath="$2"
  1179. Le_RealKeyPath="$3"
  1180. Le_RealCACertPath="$4"
  1181. Le_ReloadCmd="$5"
  1182. Le_RealFullChainPath="$6"
  1183. _initpath $Le_Domain
  1184. _setopt "$DOMAIN_CONF" "Le_RealCertPath" "=" "\"$Le_RealCertPath\""
  1185. _setopt "$DOMAIN_CONF" "Le_RealCACertPath" "=" "\"$Le_RealCACertPath\""
  1186. _setopt "$DOMAIN_CONF" "Le_RealKeyPath" "=" "\"$Le_RealKeyPath\""
  1187. _setopt "$DOMAIN_CONF" "Le_ReloadCmd" "=" "\"$Le_ReloadCmd\""
  1188. _setopt "$DOMAIN_CONF" "Le_RealFullChainPath" "=" "\"$Le_RealFullChainPath\""
  1189. if [[ "$Le_RealCertPath" ]] ; then
  1190. if [[ -f "$Le_RealCertPath" ]] ; then
  1191. cp -p "$Le_RealCertPath" "$Le_RealCertPath".bak
  1192. fi
  1193. cat "$CERT_PATH" > "$Le_RealCertPath"
  1194. fi
  1195. if [[ "$Le_RealCACertPath" ]] ; then
  1196. if [[ "$Le_RealCACertPath" == "$Le_RealCertPath" ]] ; then
  1197. echo "" >> "$Le_RealCACertPath"
  1198. cat "$CA_CERT_PATH" >> "$Le_RealCACertPath"
  1199. else
  1200. if [[ -f "$Le_RealCACertPath" ]] ; then
  1201. cp -p "$Le_RealCACertPath" "$Le_RealCACertPath".bak
  1202. fi
  1203. cat "$CA_CERT_PATH" > "$Le_RealCACertPath"
  1204. fi
  1205. fi
  1206. if [[ "$Le_RealKeyPath" ]] ; then
  1207. if [[ -f "$Le_RealKeyPath" ]] ; then
  1208. cp -p "$Le_RealKeyPath" "$Le_RealKeyPath".bak
  1209. fi
  1210. cat "$CERT_KEY_PATH" > "$Le_RealKeyPath"
  1211. fi
  1212. if [[ "$Le_RealFullChainPath" ]] ; then
  1213. if [[ -f "$Le_RealFullChainPath" ]] ; then
  1214. cp -p "$Le_RealFullChainPath" "$Le_RealFullChainPath".bak
  1215. fi
  1216. cat "$CERT_FULLCHAIN_PATH" > "$Le_RealFullChainPath"
  1217. fi
  1218. if [[ "$Le_ReloadCmd" ]] ; then
  1219. _info "Run Le_ReloadCmd: $Le_ReloadCmd"
  1220. (cd "$DOMAIN_PATH" && eval "$Le_ReloadCmd")
  1221. fi
  1222. }
  1223. installcronjob() {
  1224. _initpath
  1225. if ! _exists "crontab" ; then
  1226. _err "crontab doesn't exist, so, we can not install cron jobs."
  1227. _err "All your certs will not be renewed automatically."
  1228. _err "You must add your own cron job to call 'le.sh cron' everyday."
  1229. return 1
  1230. fi
  1231. _info "Installing cron job"
  1232. if ! crontab -l | grep 'le.sh cron' ; then
  1233. if [[ -f "$LE_WORKING_DIR/le.sh" ]] ; then
  1234. lesh="\"$LE_WORKING_DIR\"/le.sh"
  1235. else
  1236. _err "Can not install cronjob, le.sh not found."
  1237. return 1
  1238. fi
  1239. crontab -l | { cat; echo "0 0 * * * LE_WORKING_DIR=\"$LE_WORKING_DIR\" $lesh cron > /dev/null"; } | crontab -
  1240. fi
  1241. if [[ "$?" != "0" ]] ; then
  1242. _err "Install cron job failed. You need to manually renew your certs."
  1243. _err "Or you can add cronjob by yourself:"
  1244. _err "LE_WORKING_DIR=\"$LE_WORKING_DIR\" $lesh cron > /dev/null"
  1245. return 1
  1246. fi
  1247. }
  1248. uninstallcronjob() {
  1249. if ! _exists "crontab" ; then
  1250. return
  1251. fi
  1252. _info "Removing cron job"
  1253. cr="$(crontab -l | grep 'le.sh cron')"
  1254. if [[ "$cr" ]] ; then
  1255. crontab -l | sed "/le.sh cron/d" | crontab -
  1256. LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 6 | cut -d '=' -f 2 | tr -d '"')"
  1257. _info LE_WORKING_DIR "$LE_WORKING_DIR"
  1258. fi
  1259. _initpath
  1260. }
  1261. revoke() {
  1262. Le_Domain="$1"
  1263. if [[ -z "$Le_Domain" ]] ; then
  1264. echo "Usage: le.sh --revoke -d domain.com"
  1265. return 1
  1266. fi
  1267. _initpath $Le_Domain
  1268. if [[ ! -f "$DOMAIN_CONF" ]] ; then
  1269. _err "$Le_Domain is not a issued domain, skip."
  1270. return 1;
  1271. fi
  1272. if [[ ! -f "$CERT_PATH" ]] ; then
  1273. _err "Cert for $Le_Domain $CERT_PATH is not found, skip."
  1274. return 1
  1275. fi
  1276. cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}"| tr -d "\r\n" | _urlencode)"
  1277. if [[ -z "$cert" ]] ; then
  1278. _err "Cert for $Le_Domain is empty found, skip."
  1279. return 1
  1280. fi
  1281. data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}"
  1282. uri="$API/acme/revoke-cert"
  1283. _info "Try domain key first."
  1284. if _send_signed_request $uri "$data" "" "$CERT_KEY_PATH"; then
  1285. if [[ -z "$response" ]] ; then
  1286. _info "Revoke success."
  1287. rm -f $CERT_PATH
  1288. return 0
  1289. else
  1290. _err "Revoke error by domain key."
  1291. _err "$resource"
  1292. fi
  1293. fi
  1294. _info "Then try account key."
  1295. if _send_signed_request $uri "$data" "" "$ACCOUNT_KEY_PATH" ; then
  1296. if [[ -z "$response" ]] ; then
  1297. _info "Revoke success."
  1298. rm -f $CERT_PATH
  1299. return 0
  1300. else
  1301. _err "Revoke error."
  1302. _debug "$resource"
  1303. fi
  1304. fi
  1305. return 1
  1306. }
  1307. # Detect profile file if not specified as environment variable
  1308. _detect_profile() {
  1309. if [ -n "$PROFILE" -a -f "$PROFILE" ] ; then
  1310. echo "$PROFILE"
  1311. return
  1312. fi
  1313. local DETECTED_PROFILE
  1314. DETECTED_PROFILE=''
  1315. local SHELLTYPE
  1316. SHELLTYPE="$(basename "/$SHELL")"
  1317. if [[ "$SHELLTYPE" = "bash" ]] ; then
  1318. if [[ -f "$HOME/.bashrc" ]] ; then
  1319. DETECTED_PROFILE="$HOME/.bashrc"
  1320. elif [[ -f "$HOME/.bash_profile" ]] ; then
  1321. DETECTED_PROFILE="$HOME/.bash_profile"
  1322. fi
  1323. elif [[ "$SHELLTYPE" = "zsh" ]] ; then
  1324. DETECTED_PROFILE="$HOME/.zshrc"
  1325. fi
  1326. if [[ -z "$DETECTED_PROFILE" ]] ; then
  1327. if [[ -f "$HOME/.profile" ]] ; then
  1328. DETECTED_PROFILE="$HOME/.profile"
  1329. elif [[ -f "$HOME/.bashrc" ]] ; then
  1330. DETECTED_PROFILE="$HOME/.bashrc"
  1331. elif [[ -f "$HOME/.bash_profile" ]] ; then
  1332. DETECTED_PROFILE="$HOME/.bash_profile"
  1333. elif [[ -f "$HOME/.zshrc" ]] ; then
  1334. DETECTED_PROFILE="$HOME/.zshrc"
  1335. fi
  1336. fi
  1337. if [[ ! -z "$DETECTED_PROFILE" ]] ; then
  1338. echo "$DETECTED_PROFILE"
  1339. fi
  1340. }
  1341. _initconf() {
  1342. _initpath
  1343. if [[ ! -f "$ACCOUNT_CONF_PATH" ]] ; then
  1344. echo "#ACCOUNT_CONF_PATH=xxxx
  1345. #Account configurations:
  1346. #Here are the supported macros, uncomment them to make them take effect.
  1347. #ACCOUNT_EMAIL=aaa@aaa.com # the account email used to register account.
  1348. #ACCOUNT_KEY_PATH=\"/path/to/account.key\"
  1349. #STAGE=1 # Use the staging api
  1350. #FORCE=1 # Force to issue cert
  1351. #DEBUG=1 # Debug mode
  1352. #ACCOUNT_KEY_HASH=account key hash
  1353. USER_AGENT=\"le.sh client: $PROJECT\"
  1354. #USER_PATH=""
  1355. #dns api
  1356. #######################
  1357. #Cloudflare:
  1358. #api key
  1359. #CF_Key=\"sdfsdfsdfljlbjkljlkjsdfoiwje\"
  1360. #account email
  1361. #CF_Email=\"xxxx@sss.com\"
  1362. #######################
  1363. #Dnspod.cn:
  1364. #api key id
  1365. #DP_Id=\"1234\"
  1366. #api key
  1367. #DP_Key=\"sADDsdasdgdsf\"
  1368. #######################
  1369. #Cloudxns.com:
  1370. #CX_Key=\"1234\"
  1371. #
  1372. #CX_Secret=\"sADDsdasdgdsf\"
  1373. " > $ACCOUNT_CONF_PATH
  1374. fi
  1375. }
  1376. _precheck() {
  1377. if ! _exists "curl" && ! _exists "wget"; then
  1378. _err "Please install curl or wget first, we need to access http resources."
  1379. return 1
  1380. fi
  1381. if ! _exists "crontab" ; then
  1382. _err "It is recommended to install crontab first. try to install 'cron, crontab, crontabs or vixie-cron'."
  1383. _err "We need to set cron job to renew the certs automatically."
  1384. _err "Otherwise, your certs will not be able to be renewed automatically."
  1385. if [[ -z "$FORCE" ]] ; then
  1386. _err "Please define 'FORCE=1' and try install again to go without crontab."
  1387. _err "FORCE=1 ./le.sh install"
  1388. return 1
  1389. fi
  1390. fi
  1391. if ! _exists "openssl" ; then
  1392. _err "Please install openssl first."
  1393. _err "We need openssl to generate keys."
  1394. return 1
  1395. fi
  1396. if ! _exists "nc" ; then
  1397. _err "It is recommended to install nc first, try to install 'nc' or 'netcat'."
  1398. _err "We use nc for standalone server if you use standalone mode."
  1399. _err "If you don't use standalone mode, just ignore this warning."
  1400. fi
  1401. return 0
  1402. }
  1403. install() {
  1404. if ! _initpath ; then
  1405. _err "Install failed."
  1406. return 1
  1407. fi
  1408. if ! _precheck ; then
  1409. _err "Pre-check failed, can not install."
  1410. return 1
  1411. fi
  1412. _info "Installing to $LE_WORKING_DIR"
  1413. if ! mkdir -p "$LE_WORKING_DIR" ; then
  1414. _err "Can not craete working dir: $LE_WORKING_DIR"
  1415. return 1
  1416. fi
  1417. cp le.sh "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/le.sh"
  1418. if [[ "$?" != "0" ]] ; then
  1419. _err "Install failed, can not copy le.sh"
  1420. return 1
  1421. fi
  1422. _info "Installed to $LE_WORKING_DIR/le.sh"
  1423. _profile="$(_detect_profile)"
  1424. if [[ "$_profile" ]] ; then
  1425. _debug "Found profile: $_profile"
  1426. echo "LE_WORKING_DIR=$LE_WORKING_DIR
  1427. alias le=\"$LE_WORKING_DIR/le.sh\"
  1428. alias le.sh=\"$LE_WORKING_DIR/le.sh\"
  1429. " > "$LE_WORKING_DIR/le.env"
  1430. echo "" >> "$_profile"
  1431. _setopt "$_profile" "source \"$LE_WORKING_DIR/le.env\""
  1432. _info "OK, Close and reopen your terminal to start using le"
  1433. else
  1434. _info "No profile is found, you will need to go into $LE_WORKING_DIR to use le.sh"
  1435. fi
  1436. mkdir -p $LE_WORKING_DIR/dnsapi
  1437. cp dnsapi/* $LE_WORKING_DIR/dnsapi/
  1438. #to keep compatible mv the .acc file to .key file
  1439. if [[ -f "$LE_WORKING_DIR/account.acc" ]] ; then
  1440. mv "$LE_WORKING_DIR/account.acc" "$LE_WORKING_DIR/account.key"
  1441. fi
  1442. if [[ ! -f "$ACCOUNT_CONF_PATH" ]] ; then
  1443. _initconf
  1444. fi
  1445. _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\""
  1446. _setopt "$ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\""
  1447. installcronjob
  1448. _info OK
  1449. }
  1450. uninstall() {
  1451. uninstallcronjob
  1452. _initpath
  1453. _profile="$(_detect_profile)"
  1454. if [[ "$_profile" ]] ; then
  1455. text="$(cat $_profile)"
  1456. echo "$text" | sed "s|^source.*le.env.*$||" > "$_profile"
  1457. fi
  1458. rm -f $LE_WORKING_DIR/le.sh
  1459. _info "The keys and certs are in $LE_WORKING_DIR, you can remove them by yourself."
  1460. }
  1461. cron() {
  1462. IN_CRON=1
  1463. renewAll
  1464. IN_CRON=""
  1465. }
  1466. version() {
  1467. echo "$PROJECT"
  1468. echo "v$VER"
  1469. }
  1470. showhelp() {
  1471. version
  1472. echo "Usage: le.sh command ...[parameters]....
  1473. Commands:
  1474. --help, -h Show this help message.
  1475. --version, -v Show version info.
  1476. --install Install le.sh to your system.
  1477. --uninstall Uninstall le.sh, and uninstall the cron job.
  1478. --issue Issue a cert.
  1479. --installcert Install the issued cert to apache/nginx or any other server.
  1480. --renew, -r Renew a cert.
  1481. --renewAll Renew all the certs
  1482. --revoke Revoke a cert.
  1483. --installcronjob Install the cron job to renew certs, you don't need to call this. The 'install' command can automatically install the cron job.
  1484. --uninstallcronjob Uninstall the cron job. The 'uninstall' command can do this automatically.
  1485. --cron Run cron job to renew all the certs.
  1486. --toPkcs Export the certificate and key to a pfx file.
  1487. --createAccountKey, -cak Create an account private key, professional use.
  1488. --createDomainKey, -cdk Create an domain private key, professional use.
  1489. --createCSR, -ccsr Create CSR , professional use.
  1490. Parameters:
  1491. --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc.
  1492. --force, -f Used to force to install or force to renew a cert immediately.
  1493. --staging, --test Use staging server, just for test.
  1494. --debug Output debug info.
  1495. --webroot, -w /path/to/webroot Specifies the web root folder for web root mode.
  1496. --standalone Use standalone mode.
  1497. --apache Use apache mode.
  1498. --dns [dns-cf|dns-dp|dns-cx|/path/to/api/file] Use dns mode or dns api.
  1499. --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384.
  1500. --accountkeylength, -ak [2048] Specifies the account key length.
  1501. These parameters are to install the cert to nginx/apache or anyother server after issue/renew a cert:
  1502. --certpath /path/to/real/cert/file After issue/renew, the cert will be copied to this path.
  1503. --keypath /path/to/real/key/file After issue/renew, the key will be copied to this path.
  1504. --capath /path/to/real/ca/file After issue/renew, the intermediate cert will be copied to this path.
  1505. --fullchainpath /path/to/fullchain/file After issue/renew, the fullchain cert will be copied to this path.
  1506. --reloadcmd \"service nginx reload\" After issue/renew, it's used to reload the server.
  1507. --accountconf Specifies a customized account config file.
  1508. --leworkingdir Specifies the home dir for le.sh
  1509. "
  1510. }
  1511. _installOnline() {
  1512. _info "Installing from online archive."
  1513. if [[ ! "$BRANCH" ]] ; then
  1514. BRANCH="master"
  1515. fi
  1516. _initpath
  1517. target="$PROJECT/archive/$BRANCH.tar.gz"
  1518. _info "Downloading $target"
  1519. localname="$BRANCH.tar.gz"
  1520. if ! _get "$target" > $localname ; then
  1521. _debug "Download error."
  1522. return 1
  1523. fi
  1524. _info "Extracting $localname"
  1525. tar xzf $localname
  1526. cd "le-$BRANCH"
  1527. chmod +x le.sh
  1528. if ./le.sh install ; then
  1529. _info "Install success!"
  1530. fi
  1531. cd ..
  1532. rm -rf "le-$BRANCH"
  1533. rm -f "$localname"
  1534. }
  1535. _process() {
  1536. _CMD=""
  1537. _domain=""
  1538. _altdomains="no"
  1539. _webroot=""
  1540. _keylength="no"
  1541. _accountkeylength="no"
  1542. _certpath="no"
  1543. _keypath="no"
  1544. _capath="no"
  1545. _fullchainpath="no"
  1546. _reloadcmd="no"
  1547. _password=""
  1548. while (( ${#} )); do
  1549. case "${1}" in
  1550. --help|-h)
  1551. showhelp
  1552. return
  1553. ;;
  1554. --version|-v)
  1555. version
  1556. return
  1557. ;;
  1558. --install)
  1559. _CMD="install"
  1560. ;;
  1561. --uninstall)
  1562. _CMD="uninstall"
  1563. ;;
  1564. --issue)
  1565. _CMD="issue"
  1566. ;;
  1567. --installcert|-i)
  1568. _CMD="installcert"
  1569. ;;
  1570. --renew|-r)
  1571. _CMD="renew"
  1572. ;;
  1573. --renewAll|-renewall)
  1574. _CMD="renewAll"
  1575. ;;
  1576. --revoke)
  1577. _CMD="revoke"
  1578. ;;
  1579. --installcronjob)
  1580. _CMD="installcronjob"
  1581. ;;
  1582. --uninstallcronjob)
  1583. _CMD="uninstallcronjob"
  1584. ;;
  1585. --cron)
  1586. _CMD="cron"
  1587. ;;
  1588. --toPkcs)
  1589. _CMD="toPkcs"
  1590. ;;
  1591. --createAccountKey|--createaccountkey|-cak)
  1592. _CMD="createAccountKey"
  1593. ;;
  1594. --createDomainKey|--createdomainkey|-cdk)
  1595. _CMD="createDomainKey"
  1596. ;;
  1597. --createCSR|--createcsr|-ccr)
  1598. _CMD="createCSR"
  1599. ;;
  1600. --domain|-d)
  1601. _dvalue="$2"
  1602. if [[ -z "$_dvalue" ]] || [[ "$_dvalue" == "-"* ]] ; then
  1603. _err "'$_dvalue' is not a valid domain for parameter '$1'"
  1604. return 1
  1605. fi
  1606. if [[ -z "$_domain" ]] ; then
  1607. _domain="$_dvalue"
  1608. else
  1609. if [[ "$_altdomains" == "no" ]] ; then
  1610. _altdomains="$_dvalue"
  1611. else
  1612. _altdomains="$_altdomains,$_dvalue"
  1613. fi
  1614. fi
  1615. shift
  1616. ;;
  1617. --force|-f)
  1618. FORCE="1"
  1619. ;;
  1620. --staging|--test)
  1621. STAGE="1"
  1622. ;;
  1623. --debug)
  1624. if [[ "$2" == "-"* ]] ; then
  1625. DEBUG="1"
  1626. else
  1627. DEBUG="$2"
  1628. shift
  1629. fi
  1630. ;;
  1631. --webroot|-w)
  1632. wvalue="$2"
  1633. if [[ -z "$_webroot" ]] ; then
  1634. _webroot="$wvalue"
  1635. else
  1636. _webroot="$_webroot,$wvalue"
  1637. fi
  1638. shift
  1639. ;;
  1640. --standalone)
  1641. wvalue="no"
  1642. if [[ -z "$_webroot" ]] ; then
  1643. _webroot="$wvalue"
  1644. else
  1645. _webroot="$_webroot,$wvalue"
  1646. fi
  1647. ;;
  1648. --apache)
  1649. wvalue="apache"
  1650. if [[ -z "$_webroot" ]] ; then
  1651. _webroot="$wvalue"
  1652. else
  1653. _webroot="$_webroot,$wvalue"
  1654. fi
  1655. ;;
  1656. --dns)
  1657. wvalue="dns"
  1658. if [[ "$2" != "-"* ]] ; then
  1659. wvalue="$2"
  1660. shift
  1661. fi
  1662. if [[ -z "$_webroot" ]] ; then
  1663. _webroot="$wvalue"
  1664. else
  1665. _webroot="$_webroot,$wvalue"
  1666. fi
  1667. ;;
  1668. --keylength|-k)
  1669. _keylength="$2"
  1670. accountkeylength="$2"
  1671. shift
  1672. ;;
  1673. --accountkeylength|-ak)
  1674. accountkeylength="$2"
  1675. shift
  1676. ;;
  1677. --certpath)
  1678. _certpath="$2"
  1679. shift
  1680. ;;
  1681. --keypath)
  1682. _keypath="$2"
  1683. shift
  1684. ;;
  1685. --capath)
  1686. _capath="$2"
  1687. shift
  1688. ;;
  1689. --fullchainpath)
  1690. _fullchainpath="$2"
  1691. shift
  1692. ;;
  1693. --reloadcmd)
  1694. _reloadcmd="$2"
  1695. shift
  1696. ;;
  1697. --password)
  1698. _password="$2"
  1699. shift
  1700. ;;
  1701. --accountconf)
  1702. ACCOUNT_CONF_PATH="$2"
  1703. ;;
  1704. --leworkingdir)
  1705. LE_WORKING_DIR="$2"
  1706. ;;
  1707. *)
  1708. _err "Unknown parameter : $1"
  1709. return 1
  1710. ;;
  1711. esac
  1712. shift 1
  1713. done
  1714. case "${_CMD}" in
  1715. install) install ;;
  1716. uninstall) uninstall ;;
  1717. issue)
  1718. issue "$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_certpath" "$_keylength" "$_capath" "$_reloadcmd" "$_fullchainpath"
  1719. ;;
  1720. installcert)
  1721. installcert "$_domain" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath"
  1722. ;;
  1723. renew)
  1724. renew "$_domain"
  1725. ;;
  1726. renewAll)
  1727. renewAll
  1728. ;;
  1729. revoke)
  1730. revoke "$_domain"
  1731. ;;
  1732. installcronjob) installcronjob ;;
  1733. uninstallcronjob) uninstallcronjob ;;
  1734. cron) cron ;;
  1735. toPkcs)
  1736. toPkcs "$_domain" "$_password"
  1737. ;;
  1738. createAccountKey)
  1739. createAccountKey "$_domain" "$_accountkeylength"
  1740. ;;
  1741. createDomainKey)
  1742. createDomainKey "$_domain" "$_keylength"
  1743. ;;
  1744. createCSR)
  1745. createCSR "$_domain" "$_altdomains"
  1746. ;;
  1747. *)
  1748. _err "Invalid command: $_CMD"
  1749. showhelp;
  1750. return 1
  1751. ;;
  1752. esac
  1753. }
  1754. if [[ "$INSTALLONLINE" ]] ; then
  1755. INSTALLONLINE=""
  1756. _installOnline $BRANCH
  1757. exit
  1758. fi
  1759. if [[ -z "$1" ]] ; then
  1760. showhelp
  1761. else
  1762. if [[ "$1" == "-"* ]] ; then
  1763. _process "$@"
  1764. else
  1765. "$@"
  1766. fi
  1767. fi