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.

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