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.

811 lines
19 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
  1. #!/bin/bash
  2. VER=1.0.4
  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. if [ -z "$API" ] ; then
  7. API="$DEFAULT_CA"
  8. fi
  9. if [ -z "$AGREEMENT" ] ; then
  10. AGREEMENT="$DEFAULT_AGREEMENT"
  11. fi
  12. _debug() {
  13. if [ -z "$DEBUG" ] ; then
  14. return
  15. fi
  16. if [ -z "$2" ] ; then
  17. echo $1
  18. else
  19. echo $1:$2
  20. fi
  21. }
  22. _info() {
  23. if [ -z "$2" ] ; then
  24. echo $1
  25. else
  26. echo $1:$2
  27. fi
  28. }
  29. _err() {
  30. if [ -z "$2" ] ; then
  31. echo "$1" >&2
  32. else
  33. echo "$1:$2" >&2
  34. fi
  35. }
  36. #domain [2048]
  37. createAccountKey() {
  38. if [ -z "$1" ] ; then
  39. echo Usage: $0 account-domain [2048]
  40. return
  41. fi
  42. account=$1
  43. length=$2
  44. if [ -z "$2" ] ; then
  45. _info "Use default length 2048"
  46. length=2048
  47. fi
  48. _initpath
  49. if [ -f "$ACCOUNT_KEY_PATH" ] ; then
  50. _info "Account key exists, skip"
  51. return
  52. else
  53. #generate account key
  54. openssl genrsa $length > "$ACCOUNT_KEY_PATH"
  55. fi
  56. }
  57. #domain length
  58. createDomainKey() {
  59. if [ -z "$1" ] ; then
  60. echo Usage: $0 domain [2048]
  61. return
  62. fi
  63. domain=$1
  64. length=$2
  65. if [ -z "$2" ] ; then
  66. _info "Use default length 2048"
  67. length=2048
  68. fi
  69. _initpath $domain
  70. if [ -f "$CERT_KEY_PATH" ] ; then
  71. _info "Domain key exists, skip"
  72. else
  73. #generate account key
  74. openssl genrsa $length > "$CERT_KEY_PATH"
  75. fi
  76. }
  77. # domain domainlist
  78. createCSR() {
  79. if [ -z "$1" ] ; then
  80. echo Usage: $0 domain [domainlist]
  81. return
  82. fi
  83. domain=$1
  84. _initpath $domain
  85. domainlist=$2
  86. if [ -f "$CSR_PATH" ] ; then
  87. _info "CSR exists, skip"
  88. return
  89. fi
  90. if [ -z "$domainlist" ] ; then
  91. #single domain
  92. _info "Single domain" $domain
  93. openssl req -new -sha256 -key "$CERT_KEY_PATH" -subj "/CN=$domain" > "$CSR_PATH"
  94. else
  95. alt="DNS:$(echo $domainlist | sed "s/,/,DNS:/g")"
  96. #multi
  97. _info "Multi domain" "$alt"
  98. openssl req -new -sha256 -key "$CERT_KEY_PATH" -subj "/CN=$domain" -reqexts SAN -config <(printf "[ req_distinguished_name ]\n[ req ]\ndistinguished_name = req_distinguished_name\n[SAN]\nsubjectAltName=$alt") -out "$CSR_PATH"
  99. fi
  100. }
  101. _b64() {
  102. __n=$(cat)
  103. echo $__n | tr '/+' '_-' | tr -d '= '
  104. }
  105. _send_signed_request() {
  106. url=$1
  107. payload=$2
  108. needbase64=$3
  109. _debug url $url
  110. _debug payload "$payload"
  111. CURL_HEADER="$WORKING_DIR/curl.header"
  112. dp="$WORKING_DIR/curl.dump"
  113. CURL="curl --silent --dump-header $CURL_HEADER "
  114. if [ "$DEBUG" ] ; then
  115. CURL="$CURL --trace-ascii $dp "
  116. fi
  117. payload64=$(echo -n $payload | base64 -w 0 | _b64)
  118. _debug payload64 $payload64
  119. nonceurl="$API/directory"
  120. nonce=$($CURL -I $nonceurl | grep "^Replay-Nonce:" | sed s/\\r//|sed s/\\n//| cut -d ' ' -f 2)
  121. _debug nonce $nonce
  122. protected=$(echo -n "$HEADERPLACE" | sed "s/NONCE/$nonce/" )
  123. _debug protected "$protected"
  124. protected64=$( echo -n $protected | base64 -w 0 | _b64)
  125. _debug protected64 "$protected64"
  126. sig=$(echo -n "$protected64.$payload64" | openssl dgst -sha256 -sign $ACCOUNT_KEY_PATH | base64 -w 0 | _b64)
  127. _debug sig "$sig"
  128. body="{\"header\": $HEADER, \"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}"
  129. _debug body "$body"
  130. if [ "$needbase64" ] ; then
  131. response="$($CURL -X POST --data "$body" $url | base64 -w 0)"
  132. else
  133. response="$($CURL -X POST --data "$body" $url)"
  134. fi
  135. responseHeaders="$(sed 's/\r//g' $CURL_HEADER)"
  136. _debug responseHeaders "$responseHeaders"
  137. _debug response "$response"
  138. code="$(grep ^HTTP $CURL_HEADER | tail -1 | cut -d " " -f 2)"
  139. _debug code $code
  140. }
  141. _get() {
  142. url="$1"
  143. _debug url $url
  144. response="$(curl --silent $url)"
  145. ret=$?
  146. _debug response "$response"
  147. code="$(echo $response | grep -o '"status":[0-9]\+' | cut -d : -f 2)"
  148. _debug code $code
  149. return $ret
  150. }
  151. #setopt "file" "opt" "=" "value" [";"]
  152. _setopt() {
  153. __conf="$1"
  154. __opt="$2"
  155. __sep="$3"
  156. __val="$4"
  157. __end="$5"
  158. if [ -z "$__opt" ] ; then
  159. echo usage: $0 '"file" "opt" "=" "value" [";"]'
  160. return
  161. fi
  162. if [ ! -f "$__conf" ] ; then
  163. touch "$__conf"
  164. fi
  165. if grep -H -n "^$__opt$__sep" "$__conf" > /dev/null ; then
  166. _debug OK
  167. if [[ "$__val" == *"&"* ]] ; then
  168. __val="$(echo $__val | sed 's/&/\\&/g')"
  169. fi
  170. sed -i "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" "$__conf"
  171. else
  172. _debug APP
  173. echo "$__opt$__sep$__val$__end" >> "$__conf"
  174. fi
  175. _debug "$(grep -H -n "^$__opt$__sep" $__conf)"
  176. }
  177. _startserver() {
  178. content="$1"
  179. while true ; do
  180. if [ "$DEBUG" ] ; then
  181. echo -e -n "HTTP/1.1 200 OK\r\n\r\n$content" | nc -q 1 -l -p 80
  182. else
  183. echo -e -n "HTTP/1.1 200 OK\r\n\r\n$content" | nc -q 1 -l -p 80 > /dev/null
  184. fi
  185. done
  186. }
  187. _stopserver() {
  188. pid="$1"
  189. if [ "$pid" ] ; then
  190. if [ "$DEBUG" ] ; then
  191. kill -s 9 $pid
  192. killall -s 9 nc
  193. else
  194. kill -s 9 $pid > /dev/null
  195. wait $pid 2>/dev/null
  196. killall -s 9 nc > /dev/null
  197. fi
  198. fi
  199. }
  200. _initpath() {
  201. if [ -z "$WORKING_DIR" ]; then
  202. WORKING_DIR=$HOME/.le
  203. fi
  204. if [ -z "$ACME_DIR" ] ; then
  205. ACME_DIR="/home/.acme"
  206. fi
  207. if [ -z "$APACHE_CONF_BACKUP_DIR" ] ; then
  208. APACHE_CONF_BACKUP_DIR="$WORKING_DIR/"
  209. fi
  210. domain="$1"
  211. mkdir -p "$WORKING_DIR"
  212. if [ -z "$ACCOUNT_KEY_PATH" ] ; then
  213. ACCOUNT_KEY_PATH="$WORKING_DIR/account.acc"
  214. fi
  215. if [ -z "$domain" ] ; then
  216. return 0
  217. fi
  218. mkdir -p "$WORKING_DIR/$domain"
  219. if [ -z "$DOMAIN_CONF" ] ; then
  220. DOMAIN_CONF="$WORKING_DIR/$domain/$Le_Domain.conf"
  221. fi
  222. if [ -z "$CSR_PATH" ] ; then
  223. CSR_PATH="$WORKING_DIR/$domain/$domain.csr"
  224. fi
  225. if [ -z "$CERT_KEY_PATH" ] ; then
  226. CERT_KEY_PATH="$WORKING_DIR/$domain/$domain.key"
  227. fi
  228. if [ -z "$CERT_PATH" ] ; then
  229. CERT_PATH="$WORKING_DIR/$domain/$domain.cer"
  230. fi
  231. if [ -z "$CA_CERT_PATH" ] ; then
  232. CA_CERT_PATH="$WORKING_DIR/$domain/ca.cer"
  233. fi
  234. }
  235. _apachePath() {
  236. httpdroot="$(apachectl -V | grep HTTPD_ROOT= | cut -d = -f 2 | sed s/\"//g)"
  237. httpdconfname="$(apachectl -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | sed s/\"//g)"
  238. httpdconf="$httpdroot/$httpdconfname"
  239. if [ ! -f $httpdconf ] ; then
  240. _err "Apache Config file not found" $httpdconf
  241. return 1
  242. fi
  243. return 0
  244. }
  245. _restoreApache() {
  246. _initpath
  247. if ! _apachePath ; then
  248. return 1
  249. fi
  250. if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ] ; then
  251. _debug "No config file to restore."
  252. return 0
  253. fi
  254. cp -p "$APACHE_CONF_BACKUP_DIR/$httpdconfname" "$httpdconf"
  255. if ! apachectl -t ; then
  256. _err "Sorry, restore apache config error, please contact me."
  257. return 1;
  258. fi
  259. rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname"
  260. return 0
  261. }
  262. _setApache() {
  263. _initpath
  264. if ! _apachePath ; then
  265. return 1
  266. fi
  267. #backup the conf
  268. _debug "Backup apache config file" $httpdconf
  269. cp -p $httpdconf $APACHE_CONF_BACKUP_DIR/
  270. _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname"
  271. _info "In case there is an error that can not be restored automatically, you may try restore it yourself."
  272. _info "The backup file will be deleted on sucess, just forget it."
  273. #add alias
  274. echo "
  275. Alias /.well-known/acme-challenge $ACME_DIR
  276. <Directory $ACME_DIR >
  277. Require all granted
  278. </Directory>
  279. " >> $httpdconf
  280. if ! apachectl -t ; then
  281. _err "Sorry, apache config error, please contact me."
  282. _restoreApache
  283. return 1;
  284. fi
  285. if [ ! -d "$ACME_DIR" ] ; then
  286. mkdir -p "$ACME_DIR"
  287. chmod 755 "$ACME_DIR"
  288. fi
  289. if ! apachectl graceful ; then
  290. _err "Sorry, apachectl graceful error, please contact me."
  291. _restoreApache
  292. return 1;
  293. fi
  294. return 0
  295. }
  296. _clearup () {
  297. _stopserver $serverproc
  298. _restoreApache
  299. }
  300. issue() {
  301. if [ -z "$1" ] ; then
  302. echo "Usage: le issue webroot|no|apache a.com [www.a.com,b.com,c.com]|no [key-length]|no [cert-file-path]|no [key-file-path]|no [ca-cert-file-path]|no [reloadCmd]|no"
  303. return 1
  304. fi
  305. Le_Webroot="$1"
  306. Le_Domain="$2"
  307. Le_Alt="$3"
  308. Le_Keylength="$4"
  309. Le_RealCertPath="$5"
  310. Le_RealKeyPath="$6"
  311. Le_RealCACertPath="$7"
  312. Le_ReloadCmd="$8"
  313. if [ -z "$Le_Domain" ] ; then
  314. Le_Domain="$1"
  315. fi
  316. _initpath $Le_Domain
  317. if [ -f "$DOMAIN_CONF" ] ; then
  318. source "$DOMAIN_CONF"
  319. if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(date -u "+%s" )" -lt "$Le_NextRenewTime" ] ; then
  320. _info "Skip, Next renwal time is: $Le_NextRenewTimeStr"
  321. return 2
  322. fi
  323. fi
  324. if [ "$Le_Alt" == "no" ] ; then
  325. Le_Alt=""
  326. fi
  327. if [ "$Le_Keylength" == "no" ] ; then
  328. Le_Keylength=""
  329. fi
  330. if [ "$Le_RealCertPath" == "no" ] ; then
  331. Le_RealCertPath=""
  332. fi
  333. if [ "$Le_RealKeyPath" == "no" ] ; then
  334. Le_RealKeyPath=""
  335. fi
  336. if [ "$Le_RealCACertPath" == "no" ] ; then
  337. Le_RealCACertPath=""
  338. fi
  339. if [ "$Le_ReloadCmd" == "no" ] ; then
  340. Le_ReloadCmd=""
  341. fi
  342. _setopt "$DOMAIN_CONF" "Le_Domain" "=" "$Le_Domain"
  343. _setopt "$DOMAIN_CONF" "Le_Alt" "=" "$Le_Alt"
  344. _setopt "$DOMAIN_CONF" "Le_Webroot" "=" "$Le_Webroot"
  345. _setopt "$DOMAIN_CONF" "Le_Keylength" "=" "$Le_Keylength"
  346. _setopt "$DOMAIN_CONF" "Le_RealCertPath" "=" "\"$Le_RealCertPath\""
  347. _setopt "$DOMAIN_CONF" "Le_RealCACertPath" "=" "\"$Le_RealCACertPath\""
  348. _setopt "$DOMAIN_CONF" "Le_RealKeyPath" "=" "\"$Le_RealKeyPath\""
  349. _setopt "$DOMAIN_CONF" "Le_ReloadCmd" "=" "\"$Le_ReloadCmd\""
  350. if [ "$Le_Webroot" == "no" ] ; then
  351. _info "Standalone mode."
  352. if ! command -v "nc" > /dev/null ; then
  353. _err "Please install netcat(nc) tools first."
  354. return 1
  355. fi
  356. netprc="$(ss -ntpl | grep ':80 ')"
  357. if [ "$netprc" ] ; then
  358. _err "$netprc"
  359. _err "tcp port 80 is already used by $(echo "$netprc" | cut -d : -f 4)"
  360. _err "Please stop it first"
  361. return 1
  362. fi
  363. fi
  364. if [ "$Le_Webroot" == "apache" ] ; then
  365. if ! _setApache ; then
  366. _err "set up apache error. Report error to me."
  367. return 1
  368. fi
  369. wellknown_path="$ACME_DIR"
  370. fi
  371. createAccountKey $Le_Domain $Le_Keylength
  372. createDomainKey $Le_Domain $Le_Keylength
  373. createCSR $Le_Domain $Le_Alt
  374. pub_exp=$(openssl rsa -in $ACCOUNT_KEY_PATH -noout -text | grep "^publicExponent:"| cut -d '(' -f 2 | cut -d 'x' -f 2 | cut -d ')' -f 1)
  375. if [ "${#pub_exp}" == "5" ] ; then
  376. pub_exp=0$pub_exp
  377. fi
  378. _debug pub_exp "$pub_exp"
  379. e=$(echo $pub_exp | xxd -r -p | base64)
  380. _debug e "$e"
  381. modulus=$(openssl rsa -in $ACCOUNT_KEY_PATH -modulus -noout | cut -d '=' -f 2 )
  382. n=$(echo $modulus| xxd -r -p | base64 -w 0 | _b64 )
  383. jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}'
  384. HEADER='{"alg": "RS256", "jwk": '$jwk'}'
  385. HEADERPLACE='{"nonce": "NONCE", "alg": "RS256", "jwk": '$jwk'}'
  386. _debug HEADER "$HEADER"
  387. accountkey_json=$(echo -n "$jwk" | sed "s/ //g")
  388. thumbprint=$(echo -n "$accountkey_json" | sha256sum | xxd -r -p | base64 -w 0 | _b64)
  389. _info "Registering account"
  390. regjson='{"resource": "new-reg", "agreement": "'$AGREEMENT'"}'
  391. if [ "$ACCOUNT_EMAIL" ] ; then
  392. regjson='{"resource": "new-reg", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "agreement": "'$AGREEMENT'"}'
  393. fi
  394. _send_signed_request "$API/acme/new-reg" "$regjson"
  395. if [ "$code" == "" ] || [ "$code" == '201' ] ; then
  396. _info "Registered"
  397. echo $response > $WORKING_DIR/account.json
  398. elif [ "$code" == '409' ] ; then
  399. _info "Already registered"
  400. else
  401. _err "Register account Error."
  402. _clearup
  403. return 1
  404. fi
  405. # verify each domain
  406. _info "Verify each domain"
  407. alldomains=$(echo "$Le_Domain,$Le_Alt" | sed "s/,/ /g")
  408. for d in $alldomains
  409. do
  410. _info "Verifing domain" $d
  411. _send_signed_request "$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$d\"}}"
  412. if [ ! -z "$code" ] && [ ! "$code" == '201' ] ; then
  413. _err "new-authz error: $response"
  414. _clearup
  415. return 1
  416. fi
  417. http01=$(echo $response | egrep -o '{[^{]*"type":"http-01"[^}]*')
  418. _debug http01 "$http01"
  419. token=$(echo "$http01" | sed 's/,/\n'/g| grep '"token":'| cut -d : -f 2|sed 's/"//g')
  420. _debug token $token
  421. uri=$(echo "$http01" | sed 's/,/\n'/g| grep '"uri":'| cut -d : -f 2,3|sed 's/"//g')
  422. _debug uri $uri
  423. keyauthorization="$token.$thumbprint"
  424. _debug keyauthorization "$keyauthorization"
  425. if [ "$Le_Webroot" == "no" ] ; then
  426. _info "Standalone mode server"
  427. _startserver "$keyauthorization" &
  428. serverproc="$!"
  429. sleep 2
  430. _debug serverproc $serverproc
  431. else
  432. if [ -z "$wellknown_path" ] ; then
  433. wellknown_path="$Le_Webroot/.well-known/acme-challenge"
  434. fi
  435. _debug wellknown_path "$wellknown_path"
  436. mkdir -p "$wellknown_path"
  437. echo -n "$keyauthorization" > "$wellknown_path/$token"
  438. fi
  439. wellknown_url="http://$d/.well-known/acme-challenge/$token"
  440. _debug wellknown_url "$wellknown_url"
  441. _debug challenge "$challenge"
  442. _send_signed_request $uri "{\"resource\": \"challenge\", \"keyAuthorization\": \"$keyauthorization\"}"
  443. if [ ! -z "$code" ] && [ ! "$code" == '202' ] ; then
  444. _err "$d:Challenge error: $resource"
  445. _clearup
  446. return 1
  447. fi
  448. while [ "1" ] ; do
  449. _debug "sleep 5 secs to verify"
  450. sleep 5
  451. _debug "checking"
  452. if ! _get $uri ; then
  453. _err "$d:Verify error:$resource"
  454. _clearup
  455. return 1
  456. fi
  457. status=$(echo $response | egrep -o '"status":"[^"]+"' | cut -d : -f 2 | sed 's/"//g')
  458. if [ "$status" == "valid" ] ; then
  459. _info "Success"
  460. break;
  461. fi
  462. if [ "$status" == "invalid" ] ; then
  463. error=$(echo $response | egrep -o '"error":{[^}]*}' | grep -o '"detail":"[^"]*"' | cut -d '"' -f 4)
  464. _err "$d:Verify error:$error"
  465. _clearup
  466. return 1;
  467. fi
  468. if [ "$status" == "pending" ] ; then
  469. _info "Pending"
  470. else
  471. _err "$d:Verify error:$response"
  472. _clearup
  473. return 1
  474. fi
  475. done
  476. _stopserver $serverproc
  477. done
  478. _clearup
  479. _info "Verify finished, start to sign."
  480. der="$(openssl req -in $CSR_PATH -outform DER | base64 -w 0 | _b64)"
  481. _send_signed_request "$API/acme/new-cert" "{\"resource\": \"new-cert\", \"csr\": \"$der\"}" "needbase64"
  482. Le_LinkCert="$(grep -i -o '^Location.*' $CURL_HEADER |sed 's/\r//g'| cut -d " " -f 2)"
  483. _setopt "$DOMAIN_CONF" "Le_LinkCert" "=" "$Le_LinkCert"
  484. if [ "$Le_LinkCert" ] ; then
  485. echo -----BEGIN CERTIFICATE----- > "$CERT_PATH"
  486. curl --silent "$Le_LinkCert" | base64 >> "$CERT_PATH"
  487. echo -----END CERTIFICATE----- >> "$CERT_PATH"
  488. _info "Cert success."
  489. cat "$CERT_PATH"
  490. _info "Your cert is in $CERT_PATH"
  491. fi
  492. if [ -z "$Le_LinkCert" ] ; then
  493. response="$(echo $response | base64 -d)"
  494. _err "Sign failed: $(echo "$response" | grep -o '"detail":"[^"]*"')"
  495. return 1
  496. fi
  497. Le_LinkIssuer=$(grep -i '^Link' $CURL_HEADER | cut -d " " -f 2| cut -d ';' -f 1 | sed 's/<//g' | sed 's/>//g')
  498. _setopt "$DOMAIN_CONF" "Le_LinkIssuer" "=" "$Le_LinkIssuer"
  499. if [ "$Le_LinkIssuer" ] ; then
  500. echo -----BEGIN CERTIFICATE----- > "$CA_CERT_PATH"
  501. curl --silent "$Le_LinkIssuer" | base64 >> "$CA_CERT_PATH"
  502. echo -----END CERTIFICATE----- >> "$CA_CERT_PATH"
  503. _info "The intermediate CA cert is in $CA_CERT_PATH"
  504. fi
  505. Le_CertCreateTime=$(date -u "+%s")
  506. _setopt "$DOMAIN_CONF" "Le_CertCreateTime" "=" "$Le_CertCreateTime"
  507. Le_CertCreateTimeStr=$(date -u "+%Y-%m-%d %H:%M:%S UTC")
  508. _setopt "$DOMAIN_CONF" "Le_CertCreateTimeStr" "=" "\"$Le_CertCreateTimeStr\""
  509. if [ ! "$Le_RenewalDays" ] ; then
  510. Le_RenewalDays=50
  511. fi
  512. _setopt "$DOMAIN_CONF" "Le_RenewalDays" "=" "$Le_RenewalDays"
  513. Le_NextRenewTime=$(date -u -d "+$Le_RenewalDays day" "+%s")
  514. _setopt "$DOMAIN_CONF" "Le_NextRenewTime" "=" "$Le_NextRenewTime"
  515. Le_NextRenewTimeStr=$(date -u -d "+$Le_RenewalDays day" "+%Y-%m-%d %H:%M:%S UTC")
  516. _setopt "$DOMAIN_CONF" "Le_NextRenewTimeStr" "=" "\"$Le_NextRenewTimeStr\""
  517. if [ "$Le_RealCertPath" ] ; then
  518. if [ -f "$Le_RealCertPath" ] ; then
  519. rm -f "$Le_RealCertPath"
  520. fi
  521. ln -s "$CERT_PATH" "$Le_RealCertPath"
  522. fi
  523. if [ "$Le_RealCACertPath" ] ; then
  524. if [ -f "$Le_RealCACertPath" ] ; then
  525. rm -f "$Le_RealCACertPath"
  526. fi
  527. ln -s "$CA_CERT_PATH" "$Le_RealCACertPath"
  528. fi
  529. if [ "$Le_RealKeyPath" ] ; then
  530. if [ -f "$Le_RealKeyPath" ] ; then
  531. rm -f "$Le_RealKeyPath"
  532. fi
  533. ln -s "$CERT_KEY_PATH" "$Le_RealKeyPath"
  534. fi
  535. if [ "$Le_ReloadCmd" ] ; then
  536. _info "Run Le_ReloadCmd: $Le_ReloadCmd"
  537. "$Le_ReloadCmd"
  538. fi
  539. }
  540. renew() {
  541. Le_Domain="$1"
  542. if [ -z "$Le_Domain" ] ; then
  543. echo Usage: $0 domain.com
  544. return 1
  545. fi
  546. issue $Le_Domain
  547. }
  548. renewAll() {
  549. _initpath
  550. _info "renewAll"
  551. for d in $(ls -F $WORKING_DIR | grep '/$') ; do
  552. d=$(echo $d | cut -d '/' -f 1)
  553. _info "renew $d"
  554. Le_LinkCert=""
  555. Le_Domain=""
  556. Le_Alt=""
  557. Le_Webroot=""
  558. Le_Keylength=""
  559. Le_LinkIssuer=""
  560. Le_CertCreateTime=""
  561. Le_CertCreateTimeStr=""
  562. Le_RenewalDays=""
  563. Le_NextRenewTime=""
  564. Le_NextRenewTimeStr=""
  565. Le_RealCertPath=""
  566. Le_RealKeyPath=""
  567. Le_RealCACertPath=""
  568. Le_ReloadCmd=""
  569. DOMAIN_CONF=""
  570. CSR_PATH=""
  571. CERT_KEY_PATH=""
  572. CERT_PATH=""
  573. CA_CERT_PATH=""
  574. ACCOUNT_KEY_PATH=""
  575. wellknown_path=""
  576. renew "$d"
  577. done
  578. }
  579. install() {
  580. _initpath
  581. if ! command -v "curl" > /dev/null ; then
  582. _err "Please install curl first."
  583. _err "Ubuntu: sudo apt-get install curl"
  584. _err "CentOS: yum install curl"
  585. return 1
  586. fi
  587. if ! command -v "crontab" > /dev/null ; then
  588. _err "Please install crontab first."
  589. _err "CentOs: yum -y install crontabs"
  590. return 1
  591. fi
  592. if ! command -v "openssl" > /dev/null ; then
  593. _err "Please install openssl first."
  594. _err "CentOs: yum -y install openssl"
  595. return 1
  596. fi
  597. if ! command -v "xxd" > /dev/null ; then
  598. _err "Please install xxd first."
  599. _err "CentOs: yum install vim-common"
  600. return 1
  601. fi
  602. _info "Installing to $WORKING_DIR"
  603. if [ ! -f /bin/le.sh ] ; then
  604. cp le.sh "/bin/"
  605. chmod +x "/bin/le.sh"
  606. ln -s "/bin/le.sh" /bin/le
  607. fi
  608. _info "Installing cron job"
  609. if command -v sudo > /dev/null ; then
  610. if [ "$(sudo -n uptime 2>&1|grep "load"|wc -l)" != "0" ] ; then
  611. SUDO=sudo
  612. fi
  613. fi
  614. if ! crontab -l | grep 'le renewAll' ; then
  615. crontab -l | { cat; echo "0 0 * * * $SUDO le renewAll > /dev/null"; } | crontab -
  616. if command -v crond > /dev/null ; then
  617. service crond reload >/dev/null
  618. else
  619. service cron reload >/dev/null
  620. fi
  621. fi
  622. _info OK
  623. }
  624. uninstall() {
  625. _initpath
  626. _info "Removing cron job"
  627. if crontab -l | grep 'le.*renewAll' ; then
  628. crontab -l | sed "/le.*renewAll/d" | crontab -
  629. if command -v crond > /dev/null ; then
  630. service crond reload >/dev/null
  631. else
  632. service cron reload >/dev/null
  633. fi
  634. fi
  635. _info "Removing /bin/le.sh"
  636. rm -f /bin/le
  637. rm -f /bin/le.sh
  638. _info "The keys and certs are in $WORKING_DIR, you can remove them by yourself."
  639. }
  640. version() {
  641. _info "$PROJECT"
  642. _info "v$VER"
  643. }
  644. showhelp() {
  645. version
  646. echo "Usage: issue|renew|renewAll|createAccountKey|createDomainKey|createCSR|install|uninstall|version"
  647. }
  648. if [ -z "$1" ] ; then
  649. showhelp
  650. else
  651. "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
  652. fi