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.

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