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.

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