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
9 years ago
9 years ago
9 years ago
  1. #!/bin/bash
  2. VER=1.0.2
  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. API="$DEFAULT_CA"
  7. AGREEMENT="$DEFAULT_AGREEMENT"
  8. _debug() {
  9. if [ -z "$DEBUG" ] ; then
  10. return
  11. fi
  12. if [ -z "$2" ] ; then
  13. echo $1
  14. else
  15. echo $1:$2
  16. fi
  17. }
  18. _info() {
  19. if [ -z "$2" ] ; then
  20. echo $1
  21. else
  22. echo $1:$2
  23. fi
  24. }
  25. _err() {
  26. if [ -z "$2" ] ; then
  27. echo "$1" >&2
  28. else
  29. echo "$1:$2" >&2
  30. fi
  31. }
  32. #domain [2048]
  33. createAccountKey() {
  34. if [ -z "$1" ] ; then
  35. echo Usage: $0 account-domain [2048]
  36. return
  37. fi
  38. account=$1
  39. length=$2
  40. if [ -z "$2" ] ; then
  41. _info "Use default length 2048"
  42. length=2048
  43. fi
  44. _initpath
  45. if [ -f "$ACCOUNT_KEY_PATH" ] ; then
  46. _info "Account key exists, skip"
  47. return
  48. else
  49. #generate account key
  50. openssl genrsa $length > "$ACCOUNT_KEY_PATH"
  51. fi
  52. }
  53. #domain length
  54. createDomainKey() {
  55. if [ -z "$1" ] ; then
  56. echo Usage: $0 domain [2048]
  57. return
  58. fi
  59. domain=$1
  60. length=$2
  61. if [ -z "$2" ] ; then
  62. _info "Use default length 2048"
  63. length=2048
  64. fi
  65. _initpath $domain
  66. if [ -f "$CERT_KEY_PATH" ] ; then
  67. _info "Domain key exists, skip"
  68. else
  69. #generate account key
  70. openssl genrsa $length > "$CERT_KEY_PATH"
  71. fi
  72. }
  73. # domain domainlist
  74. createCSR() {
  75. if [ -z "$1" ] ; then
  76. echo Usage: $0 domain [domainlist]
  77. return
  78. fi
  79. domain=$1
  80. _initpath $domain
  81. domainlist=$2
  82. if [ -f "$CSR_PATH" ] ; then
  83. _info "CSR exists, skip"
  84. return
  85. fi
  86. if [ -z "$domainlist" ] ; then
  87. #single domain
  88. _info "Single domain" $domain
  89. openssl req -new -sha256 -key "$CERT_KEY_PATH" -subj "/CN=$domain" > "$CSR_PATH"
  90. else
  91. alt="DNS:$(echo $domainlist | sed "s/,/,DNS:/g")"
  92. #multi
  93. _info "Multi domain" "$alt"
  94. 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"
  95. fi
  96. }
  97. _b64() {
  98. __n=$(cat)
  99. echo $__n | tr '/+' '_-' | tr -d '= '
  100. }
  101. _send_signed_request() {
  102. url=$1
  103. payload=$2
  104. needbase64=$3
  105. _debug url $url
  106. _debug payload "$payload"
  107. CURL_HEADER="$WORKING_DIR/curl.header"
  108. dp="$WORKING_DIR/curl.dump"
  109. CURL="curl --silent --dump-header $CURL_HEADER "
  110. if [ "$DEBUG" ] ; then
  111. CURL="$CURL --trace-ascii $dp "
  112. fi
  113. payload64=$(echo -n $payload | base64 -w 0 | _b64)
  114. _debug payload64 $payload64
  115. nonceurl="$API/directory"
  116. nonce=$($CURL -I $nonceurl | grep "^Replay-Nonce:" | sed s/\\r//|sed s/\\n//| cut -d ' ' -f 2)
  117. _debug nonce $nonce
  118. protected=$(echo -n "$HEADERPLACE" | sed "s/NONCE/$nonce/" )
  119. _debug protected "$protected"
  120. protected64=$( echo -n $protected | base64 -w 0 | _b64)
  121. _debug protected64 "$protected64"
  122. sig=$(echo -n "$protected64.$payload64" | openssl dgst -sha256 -sign $ACCOUNT_KEY_PATH | base64 -w 0 | _b64)
  123. _debug sig "$sig"
  124. body="{\"header\": $HEADER, \"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}"
  125. _debug body "$body"
  126. if [ "$needbase64" ] ; then
  127. response="$($CURL -X POST --data "$body" $url | base64 -w 0)"
  128. else
  129. response="$($CURL -X POST --data "$body" $url)"
  130. fi
  131. responseHeaders="$(sed 's/\r//g' $CURL_HEADER)"
  132. _debug responseHeaders "$responseHeaders"
  133. _debug response "$response"
  134. code="$(grep ^HTTP $CURL_HEADER | tail -1 | cut -d " " -f 2)"
  135. _debug code $code
  136. }
  137. _get() {
  138. url="$1"
  139. _debug url $url
  140. response="$(curl --silent $url)"
  141. ret=$?
  142. _debug response "$response"
  143. code="$(echo $response | grep -o '"status":[0-9]\+' | cut -d : -f 2)"
  144. _debug code $code
  145. return $ret
  146. }
  147. #setopt "file" "opt" "=" "value" [";"]
  148. _setopt() {
  149. __conf="$1"
  150. __opt="$2"
  151. __sep="$3"
  152. __val="$4"
  153. __end="$5"
  154. if [ -z "$__opt" ] ; then
  155. echo usage: $0 '"file" "opt" "=" "value" [";"]'
  156. return
  157. fi
  158. if [ ! -f "$__conf" ] ; then
  159. touch "$__conf"
  160. fi
  161. if grep -H -n "^$__opt$__sep" "$__conf" > /dev/null ; then
  162. _debug OK
  163. if [[ "$__val" == *"&"* ]] ; then
  164. __val="$(echo $__val | sed 's/&/\\&/g')"
  165. fi
  166. sed -i "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" "$__conf"
  167. else
  168. _debug APP
  169. echo "$__opt$__sep$__val$__end" >> "$__conf"
  170. fi
  171. _debug "$(grep -H -n "^$__opt$__sep" $__conf)"
  172. }
  173. _startserver() {
  174. content="$1"
  175. while true ; do
  176. if [ "$DEBUG" ] ; then
  177. echo -e -n "HTTP/1.1 200 OK\r\n\r\n$content" | nc -q 1 -l -p 80
  178. else
  179. echo -e -n "HTTP/1.1 200 OK\r\n\r\n$content" | nc -q 1 -l -p 80 > /dev/null
  180. fi
  181. done
  182. }
  183. _stopserver() {
  184. pid="$1"
  185. if [ "$pid" ] ; then
  186. if [ "$DEBUG" ] ; then
  187. kill -s 9 $pid
  188. killall -s 9 nc
  189. else
  190. kill -s 9 $pid > /dev/null
  191. wait $pid 2>/dev/null
  192. killall -s 9 nc > /dev/null
  193. fi
  194. fi
  195. }
  196. _initpath() {
  197. if [ -z "$WORKING_DIR" ]; then
  198. WORKING_DIR=$HOME/.le
  199. fi
  200. if [ -z "$ACME_DIR" ] ; then
  201. ACME_DIR="/home/.acme"
  202. fi
  203. if [ -z "$APACHE_CONF_BACKUP_DIR" ] ; then
  204. APACHE_CONF_BACKUP_DIR="$WORKING_DIR/"
  205. fi
  206. domain="$1"
  207. mkdir -p "$WORKING_DIR"
  208. ACCOUNT_KEY_PATH="$WORKING_DIR/account.acc"
  209. if [ -z "$domain" ] ; then
  210. return 0
  211. fi
  212. mkdir -p "$WORKING_DIR/$domain"
  213. DOMAIN_CONF="$WORKING_DIR/$domain/$Le_Domain.conf"
  214. CSR_PATH="$WORKING_DIR/$domain/$domain.csr"
  215. CERT_KEY_PATH="$WORKING_DIR/$domain/$domain.key"
  216. CERT_PATH="$WORKING_DIR/$domain/$domain.cer"
  217. CA_CERT_PATH="$WORKING_DIR/$domain/ca.cer"
  218. }
  219. _apachePath() {
  220. httpdroot="$(apachectl -V | grep HTTPD_ROOT= | cut -d = -f 2 | sed s/\"//g)"
  221. httpdconfname="$(apachectl -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | sed s/\"//g)"
  222. httpdconf="$httpdroot/$httpdconfname"
  223. if [ ! -f $httpdconf ] ; then
  224. _err "Apache Config file not found" $httpdconf
  225. return 1
  226. fi
  227. return 0
  228. }
  229. _restoreApache() {
  230. _initpath
  231. if ! _apachePath ; then
  232. return 1
  233. fi
  234. if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ] ; then
  235. _debug "No config file to restore."
  236. return 0
  237. fi
  238. cp -p "$APACHE_CONF_BACKUP_DIR/$httpdconfname" "$httpdconf"
  239. if ! apachectl -t ; then
  240. _err "Sorry, restore apache config error, please contact me."
  241. return 1;
  242. fi
  243. rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname"
  244. return 0
  245. }
  246. _setApache() {
  247. _initpath
  248. if ! _apachePath ; then
  249. return 1
  250. fi
  251. #backup the conf
  252. _debug "Backup apache config file" $httpdconf
  253. cp -p $httpdconf $APACHE_CONF_BACKUP_DIR/
  254. _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname"
  255. _info "In case there is an error that can not be restored automatically, you may try restore it yourself."
  256. _info "The backup file will be deleted on sucess, just forget it."
  257. #add alias
  258. echo "
  259. Alias /.well-known/acme-challenge $ACME_DIR
  260. <Directory $ACME_DIR >
  261. Order allow,deny
  262. Allow from all
  263. </Directory>
  264. " >> $httpdconf
  265. if ! apachectl -t ; then
  266. _err "Sorry, apache config error, please contact me."
  267. _restoreApache
  268. return 1;
  269. fi
  270. if [ ! -d "$ACME_DIR" ] ; then
  271. mkdir -p "$ACME_DIR"
  272. chmod 755 "$ACME_DIR"
  273. fi
  274. if ! apachectl graceful ; then
  275. _err "Sorry, apachectl graceful error, please contact me."
  276. _restoreApache
  277. return 1;
  278. fi
  279. return 0
  280. }
  281. _clearup () {
  282. _stopserver $serverproc
  283. _restoreApache
  284. }
  285. issue() {
  286. if [ -z "$1" ] ; then
  287. 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"
  288. return 1
  289. fi
  290. Le_Webroot="$1"
  291. Le_Domain="$2"
  292. Le_Alt="$3"
  293. Le_Keylength="$4"
  294. Le_RealCertPath="$5"
  295. Le_RealKeyPath="$6"
  296. Le_RealCACertPath="$7"
  297. Le_ReloadCmd="$8"
  298. if [ -z "$Le_Domain" ] ; then
  299. Le_Domain="$1"
  300. fi
  301. _initpath $Le_Domain
  302. if [ -f "$DOMAIN_CONF" ] ; then
  303. source "$DOMAIN_CONF"
  304. if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(date -u "+%s" )" -lt "$Le_NextRenewTime" ] ; then
  305. _info "Skip, Next renwal time is: $Le_NextRenewTimeStr"
  306. return 2
  307. fi
  308. fi
  309. if [ "$Le_Alt" == "no" ] ; then
  310. Le_Alt=""
  311. fi
  312. if [ "$Le_Keylength" == "no" ] ; then
  313. Le_Keylength=""
  314. fi
  315. if [ "$Le_RealCertPath" == "no" ] ; then
  316. Le_RealCertPath=""
  317. fi
  318. if [ "$Le_RealKeyPath" == "no" ] ; then
  319. Le_RealKeyPath=""
  320. fi
  321. if [ "$Le_RealCACertPath" == "no" ] ; then
  322. Le_RealCACertPath=""
  323. fi
  324. if [ "$Le_ReloadCmd" == "no" ] ; then
  325. Le_ReloadCmd=""
  326. fi
  327. _setopt "$DOMAIN_CONF" "Le_Domain" "=" "$Le_Domain"
  328. _setopt "$DOMAIN_CONF" "Le_Alt" "=" "$Le_Alt"
  329. _setopt "$DOMAIN_CONF" "Le_Webroot" "=" "$Le_Webroot"
  330. _setopt "$DOMAIN_CONF" "Le_Keylength" "=" "$Le_Keylength"
  331. _setopt "$DOMAIN_CONF" "Le_RealCertPath" "=" "\"$Le_RealCertPath\""
  332. _setopt "$DOMAIN_CONF" "Le_RealCACertPath" "=" "\"$Le_RealCACertPath\""
  333. _setopt "$DOMAIN_CONF" "Le_RealKeyPath" "=" "\"$Le_RealKeyPath\""
  334. _setopt "$DOMAIN_CONF" "Le_ReloadCmd" "=" "\"$Le_ReloadCmd\""
  335. if [ "$Le_Webroot" == "no" ] ; then
  336. _info "Standalone mode."
  337. if ! command -v "nc" > /dev/null ; then
  338. _err "Please install netcat(nc) tools first."
  339. return 1
  340. fi
  341. netprc="$(ss -ntpl | grep ':80 ')"
  342. if [ "$netprc" ] ; then
  343. _err "$netprc"
  344. _err "tcp port 80 is already used by $(echo "$netprc" | cut -d : -f 4)"
  345. _err "Please stop it first"
  346. return 1
  347. fi
  348. fi
  349. if [ "$Le_Webroot" == "apache" ] ; then
  350. if ! _setApache ; then
  351. _err "set up apache error. Report error to me."
  352. return 1
  353. fi
  354. wellknown_path="$ACME_DIR"
  355. fi
  356. createAccountKey $Le_Domain $Le_Keylength
  357. createDomainKey $Le_Domain $Le_Keylength
  358. createCSR $Le_Domain $Le_Alt
  359. 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)
  360. if [ "${#pub_exp}" == "5" ] ; then
  361. pub_exp=0$pub_exp
  362. fi
  363. _debug pub_exp "$pub_exp"
  364. e=$(echo $pub_exp | xxd -r -p | base64)
  365. _debug e "$e"
  366. modulus=$(openssl rsa -in $ACCOUNT_KEY_PATH -modulus -noout | cut -d '=' -f 2 )
  367. n=$(echo $modulus| xxd -r -p | base64 -w 0 | _b64 )
  368. jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}'
  369. HEADER='{"alg": "RS256", "jwk": '$jwk'}'
  370. HEADERPLACE='{"nonce": "NONCE", "alg": "RS256", "jwk": '$jwk'}'
  371. _debug HEADER "$HEADER"
  372. accountkey_json=$(echo -n "$jwk" | sed "s/ //g")
  373. thumbprint=$(echo -n "$accountkey_json" | sha256sum | xxd -r -p | base64 -w 0 | _b64)
  374. _info "Registering account"
  375. regjson='{"resource": "new-reg", "agreement": "'$AGREEMENT'"}'
  376. if [ "$ACCOUNT_EMAIL" ] ; then
  377. regjson='{"resource": "new-reg", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "agreement": "'$AGREEMENT'"}'
  378. fi
  379. _send_signed_request "$API/acme/new-reg" "$regjson"
  380. if [ "$code" == "" ] || [ "$code" == '201' ] ; then
  381. _info "Registered"
  382. echo $response > $WORKING_DIR/account.json
  383. elif [ "$code" == '409' ] ; then
  384. _info "Already registered"
  385. else
  386. _err "Register account Error."
  387. _clearup
  388. return 1
  389. fi
  390. # verify each domain
  391. _info "Verify each domain"
  392. alldomains=$(echo "$Le_Domain,$Le_Alt" | sed "s/,/ /g")
  393. for d in $alldomains
  394. do
  395. _info "Verifing domain" $d
  396. _send_signed_request "$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$d\"}}"
  397. if [ ! -z "$code" ] && [ ! "$code" == '201' ] ; then
  398. _err "new-authz error: $response"
  399. _clearup
  400. return 1
  401. fi
  402. http01=$(echo $response | egrep -o '{[^{]*"type":"http-01"[^}]*')
  403. _debug http01 "$http01"
  404. token=$(echo "$http01" | sed 's/,/\n'/g| grep '"token":'| cut -d : -f 2|sed 's/"//g')
  405. _debug token $token
  406. uri=$(echo "$http01" | sed 's/,/\n'/g| grep '"uri":'| cut -d : -f 2,3|sed 's/"//g')
  407. _debug uri $uri
  408. keyauthorization="$token.$thumbprint"
  409. _debug keyauthorization "$keyauthorization"
  410. if [ "$Le_Webroot" == "no" ] ; then
  411. _info "Standalone mode server"
  412. _startserver "$keyauthorization" &
  413. serverproc="$!"
  414. sleep 2
  415. _debug serverproc $serverproc
  416. else
  417. if [ -z "$wellknown_path" ] ; then
  418. wellknown_path="$Le_Webroot/.well-known/acme-challenge"
  419. fi
  420. _debug wellknown_path "$wellknown_path"
  421. mkdir -p "$wellknown_path"
  422. echo -n "$keyauthorization" > "$wellknown_path/$token"
  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. _clearup
  464. _info "Verify finished, start to sign."
  465. der="$(openssl req -in $CSR_PATH -outform DER | base64 -w 0 | _b64)"
  466. _send_signed_request "$API/acme/new-cert" "{\"resource\": \"new-cert\", \"csr\": \"$der\"}" "needbase64"
  467. Le_LinkCert="$(grep -i -o '^Location.*' $CURL_HEADER |sed 's/\r//g'| cut -d " " -f 2)"
  468. _setopt "$DOMAIN_CONF" "Le_LinkCert" "=" "$Le_LinkCert"
  469. if [ "$Le_LinkCert" ] ; then
  470. echo -----BEGIN CERTIFICATE----- > "$CERT_PATH"
  471. curl --silent "$Le_LinkCert" | base64 >> "$CERT_PATH"
  472. echo -----END CERTIFICATE----- >> "$CERT_PATH"
  473. _info "Cert success."
  474. cat "$CERT_PATH"
  475. _info "Your cert is in $CERT_PATH"
  476. fi
  477. if [ -z "$Le_LinkCert" ] ; then
  478. response="$(echo $response | base64 -d)"
  479. _err "Sign failed: $(echo "$response" | grep -o '"detail":"[^"]*"')"
  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. version() {
  619. _info "$PROJECT"
  620. _info "v$VER"
  621. }
  622. showhelp() {
  623. version
  624. echo "Usage: issue|renew|renewAll|createAccountKey|createDomainKey|createCSR|install|uninstall|version"
  625. }
  626. if [ -z "$1" ] ; then
  627. showhelp
  628. else
  629. "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
  630. fi