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.

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