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.

816 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
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. if [ -z "$usingApache" ] ; then
  247. return 0
  248. fi
  249. _initpath
  250. if ! _apachePath ; then
  251. return 1
  252. fi
  253. if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ] ; then
  254. _debug "No config file to restore."
  255. return 0
  256. fi
  257. cp -p "$APACHE_CONF_BACKUP_DIR/$httpdconfname" "$httpdconf"
  258. if ! apachectl -t ; then
  259. _err "Sorry, restore apache config error, please contact me."
  260. return 1;
  261. fi
  262. rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname"
  263. return 0
  264. }
  265. _setApache() {
  266. _initpath
  267. if ! _apachePath ; then
  268. return 1
  269. fi
  270. #backup the conf
  271. _debug "Backup apache config file" $httpdconf
  272. cp -p $httpdconf $APACHE_CONF_BACKUP_DIR/
  273. _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname"
  274. _info "In case there is an error that can not be restored automatically, you may try restore it yourself."
  275. _info "The backup file will be deleted on sucess, just forget it."
  276. #add alias
  277. echo "
  278. Alias /.well-known/acme-challenge $ACME_DIR
  279. <Directory $ACME_DIR >
  280. Require all granted
  281. </Directory>
  282. " >> $httpdconf
  283. if ! apachectl -t ; then
  284. _err "Sorry, apache config error, please contact me."
  285. _restoreApache
  286. return 1;
  287. fi
  288. if [ ! -d "$ACME_DIR" ] ; then
  289. mkdir -p "$ACME_DIR"
  290. chmod 755 "$ACME_DIR"
  291. fi
  292. if ! apachectl graceful ; then
  293. _err "Sorry, apachectl graceful error, please contact me."
  294. _restoreApache
  295. return 1;
  296. fi
  297. usingApache="1"
  298. return 0
  299. }
  300. _clearup () {
  301. _stopserver $serverproc
  302. _restoreApache
  303. }
  304. issue() {
  305. if [ -z "$1" ] ; then
  306. 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"
  307. return 1
  308. fi
  309. Le_Webroot="$1"
  310. Le_Domain="$2"
  311. Le_Alt="$3"
  312. Le_Keylength="$4"
  313. Le_RealCertPath="$5"
  314. Le_RealKeyPath="$6"
  315. Le_RealCACertPath="$7"
  316. Le_ReloadCmd="$8"
  317. if [ -z "$Le_Domain" ] ; then
  318. Le_Domain="$1"
  319. fi
  320. _initpath $Le_Domain
  321. if [ -f "$DOMAIN_CONF" ] ; then
  322. source "$DOMAIN_CONF"
  323. if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(date -u "+%s" )" -lt "$Le_NextRenewTime" ] ; then
  324. _info "Skip, Next renwal time is: $Le_NextRenewTimeStr"
  325. return 2
  326. fi
  327. fi
  328. if [ "$Le_Alt" == "no" ] ; then
  329. Le_Alt=""
  330. fi
  331. if [ "$Le_Keylength" == "no" ] ; then
  332. Le_Keylength=""
  333. fi
  334. if [ "$Le_RealCertPath" == "no" ] ; then
  335. Le_RealCertPath=""
  336. fi
  337. if [ "$Le_RealKeyPath" == "no" ] ; then
  338. Le_RealKeyPath=""
  339. fi
  340. if [ "$Le_RealCACertPath" == "no" ] ; then
  341. Le_RealCACertPath=""
  342. fi
  343. if [ "$Le_ReloadCmd" == "no" ] ; then
  344. Le_ReloadCmd=""
  345. fi
  346. _setopt "$DOMAIN_CONF" "Le_Domain" "=" "$Le_Domain"
  347. _setopt "$DOMAIN_CONF" "Le_Alt" "=" "$Le_Alt"
  348. _setopt "$DOMAIN_CONF" "Le_Webroot" "=" "$Le_Webroot"
  349. _setopt "$DOMAIN_CONF" "Le_Keylength" "=" "$Le_Keylength"
  350. _setopt "$DOMAIN_CONF" "Le_RealCertPath" "=" "\"$Le_RealCertPath\""
  351. _setopt "$DOMAIN_CONF" "Le_RealCACertPath" "=" "\"$Le_RealCACertPath\""
  352. _setopt "$DOMAIN_CONF" "Le_RealKeyPath" "=" "\"$Le_RealKeyPath\""
  353. _setopt "$DOMAIN_CONF" "Le_ReloadCmd" "=" "\"$Le_ReloadCmd\""
  354. if [ "$Le_Webroot" == "no" ] ; then
  355. _info "Standalone mode."
  356. if ! command -v "nc" > /dev/null ; then
  357. _err "Please install netcat(nc) tools first."
  358. return 1
  359. fi
  360. netprc="$(ss -ntpl | grep ':80 ')"
  361. if [ "$netprc" ] ; then
  362. _err "$netprc"
  363. _err "tcp port 80 is already used by $(echo "$netprc" | cut -d : -f 4)"
  364. _err "Please stop it first"
  365. return 1
  366. fi
  367. fi
  368. if [ "$Le_Webroot" == "apache" ] ; then
  369. if ! _setApache ; then
  370. _err "set up apache error. Report error to me."
  371. return 1
  372. fi
  373. wellknown_path="$ACME_DIR"
  374. else
  375. usingApache=""
  376. fi
  377. createAccountKey $Le_Domain $Le_Keylength
  378. createDomainKey $Le_Domain $Le_Keylength
  379. createCSR $Le_Domain $Le_Alt
  380. 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)
  381. if [ "${#pub_exp}" == "5" ] ; then
  382. pub_exp=0$pub_exp
  383. fi
  384. _debug pub_exp "$pub_exp"
  385. e=$(echo $pub_exp | xxd -r -p | base64)
  386. _debug e "$e"
  387. modulus=$(openssl rsa -in $ACCOUNT_KEY_PATH -modulus -noout | cut -d '=' -f 2 )
  388. n=$(echo $modulus| xxd -r -p | base64 -w 0 | _b64 )
  389. jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}'
  390. HEADER='{"alg": "RS256", "jwk": '$jwk'}'
  391. HEADERPLACE='{"nonce": "NONCE", "alg": "RS256", "jwk": '$jwk'}'
  392. _debug HEADER "$HEADER"
  393. accountkey_json=$(echo -n "$jwk" | sed "s/ //g")
  394. thumbprint=$(echo -n "$accountkey_json" | sha256sum | xxd -r -p | base64 -w 0 | _b64)
  395. _info "Registering account"
  396. regjson='{"resource": "new-reg", "agreement": "'$AGREEMENT'"}'
  397. if [ "$ACCOUNT_EMAIL" ] ; then
  398. regjson='{"resource": "new-reg", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "agreement": "'$AGREEMENT'"}'
  399. fi
  400. _send_signed_request "$API/acme/new-reg" "$regjson"
  401. if [ "$code" == "" ] || [ "$code" == '201' ] ; then
  402. _info "Registered"
  403. echo $response > $WORKING_DIR/account.json
  404. elif [ "$code" == '409' ] ; then
  405. _info "Already registered"
  406. else
  407. _err "Register account Error."
  408. _clearup
  409. return 1
  410. fi
  411. # verify each domain
  412. _info "Verify each domain"
  413. alldomains=$(echo "$Le_Domain,$Le_Alt" | sed "s/,/ /g")
  414. for d in $alldomains
  415. do
  416. _info "Verifing domain" $d
  417. _send_signed_request "$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$d\"}}"
  418. if [ ! -z "$code" ] && [ ! "$code" == '201' ] ; then
  419. _err "new-authz error: $response"
  420. _clearup
  421. return 1
  422. fi
  423. http01=$(echo $response | egrep -o '{[^{]*"type":"http-01"[^}]*')
  424. _debug http01 "$http01"
  425. token=$(echo "$http01" | sed 's/,/\n'/g| grep '"token":'| cut -d : -f 2|sed 's/"//g')
  426. _debug token $token
  427. uri=$(echo "$http01" | sed 's/,/\n'/g| grep '"uri":'| cut -d : -f 2,3|sed 's/"//g')
  428. _debug uri $uri
  429. keyauthorization="$token.$thumbprint"
  430. _debug keyauthorization "$keyauthorization"
  431. if [ "$Le_Webroot" == "no" ] ; then
  432. _info "Standalone mode server"
  433. _startserver "$keyauthorization" &
  434. serverproc="$!"
  435. sleep 2
  436. _debug serverproc $serverproc
  437. else
  438. if [ -z "$wellknown_path" ] ; then
  439. wellknown_path="$Le_Webroot/.well-known/acme-challenge"
  440. fi
  441. _debug wellknown_path "$wellknown_path"
  442. mkdir -p "$wellknown_path"
  443. echo -n "$keyauthorization" > "$wellknown_path/$token"
  444. fi
  445. wellknown_url="http://$d/.well-known/acme-challenge/$token"
  446. _debug wellknown_url "$wellknown_url"
  447. _debug challenge "$challenge"
  448. _send_signed_request $uri "{\"resource\": \"challenge\", \"keyAuthorization\": \"$keyauthorization\"}"
  449. if [ ! -z "$code" ] && [ ! "$code" == '202' ] ; then
  450. _err "$d:Challenge error: $resource"
  451. _clearup
  452. return 1
  453. fi
  454. while [ "1" ] ; do
  455. _debug "sleep 5 secs to verify"
  456. sleep 5
  457. _debug "checking"
  458. if ! _get $uri ; then
  459. _err "$d:Verify error:$resource"
  460. _clearup
  461. return 1
  462. fi
  463. status=$(echo $response | egrep -o '"status":"[^"]+"' | cut -d : -f 2 | sed 's/"//g')
  464. if [ "$status" == "valid" ] ; then
  465. _info "Success"
  466. break;
  467. fi
  468. if [ "$status" == "invalid" ] ; then
  469. error=$(echo $response | egrep -o '"error":{[^}]*}' | grep -o '"detail":"[^"]*"' | cut -d '"' -f 4)
  470. _err "$d:Verify error:$error"
  471. _clearup
  472. return 1;
  473. fi
  474. if [ "$status" == "pending" ] ; then
  475. _info "Pending"
  476. else
  477. _err "$d:Verify error:$response"
  478. _clearup
  479. return 1
  480. fi
  481. done
  482. _stopserver $serverproc
  483. done
  484. _clearup
  485. _info "Verify finished, start to sign."
  486. der="$(openssl req -in $CSR_PATH -outform DER | base64 -w 0 | _b64)"
  487. _send_signed_request "$API/acme/new-cert" "{\"resource\": \"new-cert\", \"csr\": \"$der\"}" "needbase64"
  488. Le_LinkCert="$(grep -i -o '^Location.*' $CURL_HEADER |sed 's/\r//g'| cut -d " " -f 2)"
  489. _setopt "$DOMAIN_CONF" "Le_LinkCert" "=" "$Le_LinkCert"
  490. if [ "$Le_LinkCert" ] ; then
  491. echo -----BEGIN CERTIFICATE----- > "$CERT_PATH"
  492. curl --silent "$Le_LinkCert" | base64 >> "$CERT_PATH"
  493. echo -----END CERTIFICATE----- >> "$CERT_PATH"
  494. _info "Cert success."
  495. cat "$CERT_PATH"
  496. _info "Your cert is in $CERT_PATH"
  497. fi
  498. if [ -z "$Le_LinkCert" ] ; then
  499. response="$(echo $response | base64 -d)"
  500. _err "Sign failed: $(echo "$response" | grep -o '"detail":"[^"]*"')"
  501. return 1
  502. fi
  503. Le_LinkIssuer=$(grep -i '^Link' $CURL_HEADER | cut -d " " -f 2| cut -d ';' -f 1 | sed 's/<//g' | sed 's/>//g')
  504. _setopt "$DOMAIN_CONF" "Le_LinkIssuer" "=" "$Le_LinkIssuer"
  505. if [ "$Le_LinkIssuer" ] ; then
  506. echo -----BEGIN CERTIFICATE----- > "$CA_CERT_PATH"
  507. curl --silent "$Le_LinkIssuer" | base64 >> "$CA_CERT_PATH"
  508. echo -----END CERTIFICATE----- >> "$CA_CERT_PATH"
  509. _info "The intermediate CA cert is in $CA_CERT_PATH"
  510. fi
  511. Le_CertCreateTime=$(date -u "+%s")
  512. _setopt "$DOMAIN_CONF" "Le_CertCreateTime" "=" "$Le_CertCreateTime"
  513. Le_CertCreateTimeStr=$(date -u "+%Y-%m-%d %H:%M:%S UTC")
  514. _setopt "$DOMAIN_CONF" "Le_CertCreateTimeStr" "=" "\"$Le_CertCreateTimeStr\""
  515. if [ ! "$Le_RenewalDays" ] ; then
  516. Le_RenewalDays=50
  517. fi
  518. _setopt "$DOMAIN_CONF" "Le_RenewalDays" "=" "$Le_RenewalDays"
  519. Le_NextRenewTime=$(date -u -d "+$Le_RenewalDays day" "+%s")
  520. _setopt "$DOMAIN_CONF" "Le_NextRenewTime" "=" "$Le_NextRenewTime"
  521. Le_NextRenewTimeStr=$(date -u -d "+$Le_RenewalDays day" "+%Y-%m-%d %H:%M:%S UTC")
  522. _setopt "$DOMAIN_CONF" "Le_NextRenewTimeStr" "=" "\"$Le_NextRenewTimeStr\""
  523. if [ "$Le_RealCertPath" ] ; then
  524. if [ -f "$Le_RealCertPath" ] ; then
  525. rm -f "$Le_RealCertPath"
  526. fi
  527. ln -s "$CERT_PATH" "$Le_RealCertPath"
  528. fi
  529. if [ "$Le_RealCACertPath" ] ; then
  530. if [ -f "$Le_RealCACertPath" ] ; then
  531. rm -f "$Le_RealCACertPath"
  532. fi
  533. ln -s "$CA_CERT_PATH" "$Le_RealCACertPath"
  534. fi
  535. if [ "$Le_RealKeyPath" ] ; then
  536. if [ -f "$Le_RealKeyPath" ] ; then
  537. rm -f "$Le_RealKeyPath"
  538. fi
  539. ln -s "$CERT_KEY_PATH" "$Le_RealKeyPath"
  540. fi
  541. if [ "$Le_ReloadCmd" ] ; then
  542. _info "Run Le_ReloadCmd: $Le_ReloadCmd"
  543. "$Le_ReloadCmd"
  544. fi
  545. }
  546. renew() {
  547. Le_Domain="$1"
  548. if [ -z "$Le_Domain" ] ; then
  549. echo Usage: $0 domain.com
  550. return 1
  551. fi
  552. issue $Le_Domain
  553. }
  554. renewAll() {
  555. _initpath
  556. _info "renewAll"
  557. for d in $(ls -F $WORKING_DIR | grep '/$') ; do
  558. d=$(echo $d | cut -d '/' -f 1)
  559. _info "renew $d"
  560. Le_LinkCert=""
  561. Le_Domain=""
  562. Le_Alt=""
  563. Le_Webroot=""
  564. Le_Keylength=""
  565. Le_LinkIssuer=""
  566. Le_CertCreateTime=""
  567. Le_CertCreateTimeStr=""
  568. Le_RenewalDays=""
  569. Le_NextRenewTime=""
  570. Le_NextRenewTimeStr=""
  571. Le_RealCertPath=""
  572. Le_RealKeyPath=""
  573. Le_RealCACertPath=""
  574. Le_ReloadCmd=""
  575. DOMAIN_CONF=""
  576. CSR_PATH=""
  577. CERT_KEY_PATH=""
  578. CERT_PATH=""
  579. CA_CERT_PATH=""
  580. ACCOUNT_KEY_PATH=""
  581. wellknown_path=""
  582. renew "$d"
  583. done
  584. }
  585. install() {
  586. _initpath
  587. if ! command -v "curl" > /dev/null ; then
  588. _err "Please install curl first."
  589. _err "Ubuntu: sudo apt-get install curl"
  590. _err "CentOS: yum install curl"
  591. return 1
  592. fi
  593. if ! command -v "crontab" > /dev/null ; then
  594. _err "Please install crontab first."
  595. _err "CentOs: yum -y install crontabs"
  596. return 1
  597. fi
  598. if ! command -v "openssl" > /dev/null ; then
  599. _err "Please install openssl first."
  600. _err "CentOs: yum -y install openssl"
  601. return 1
  602. fi
  603. if ! command -v "xxd" > /dev/null ; then
  604. _err "Please install xxd first."
  605. _err "CentOs: yum install vim-common"
  606. return 1
  607. fi
  608. _info "Installing to $WORKING_DIR"
  609. if [ ! -f /bin/le.sh ] ; then
  610. cp le.sh "/bin/"
  611. chmod +x "/bin/le.sh"
  612. ln -s "/bin/le.sh" /bin/le
  613. fi
  614. _info "Installing cron job"
  615. if command -v sudo > /dev/null ; then
  616. if [ "$(sudo -n uptime 2>&1|grep "load"|wc -l)" != "0" ] ; then
  617. SUDO=sudo
  618. fi
  619. fi
  620. if ! crontab -l | grep 'le renewAll' ; then
  621. crontab -l | { cat; echo "0 0 * * * $SUDO le renewAll > /dev/null"; } | crontab -
  622. if command -v crond > /dev/null ; then
  623. service crond reload >/dev/null
  624. else
  625. service cron reload >/dev/null
  626. fi
  627. fi
  628. _info OK
  629. }
  630. uninstall() {
  631. _initpath
  632. _info "Removing cron job"
  633. if crontab -l | grep 'le.*renewAll' ; then
  634. crontab -l | sed "/le.*renewAll/d" | crontab -
  635. if command -v crond > /dev/null ; then
  636. service crond reload >/dev/null
  637. else
  638. service cron reload >/dev/null
  639. fi
  640. fi
  641. _info "Removing /bin/le.sh"
  642. rm -f /bin/le
  643. rm -f /bin/le.sh
  644. _info "The keys and certs are in $WORKING_DIR, you can remove them by yourself."
  645. }
  646. version() {
  647. _info "$PROJECT"
  648. _info "v$VER"
  649. }
  650. showhelp() {
  651. version
  652. echo "Usage: issue|renew|renewAll|createAccountKey|createDomainKey|createCSR|install|uninstall|version"
  653. }
  654. if [ -z "$1" ] ; then
  655. showhelp
  656. else
  657. "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
  658. fi