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.

787 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.1
  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. wellknown_path="$wellknown_path/$token"
  423. echo -n "$keyauthorization" > $wellknown_path
  424. fi
  425. wellknown_url="http://$d/.well-known/acme-challenge/$token"
  426. _debug wellknown_url "$wellknown_url"
  427. _debug challenge "$challenge"
  428. _send_signed_request $uri "{\"resource\": \"challenge\", \"keyAuthorization\": \"$keyauthorization\"}"
  429. if [ ! -z "$code" ] && [ ! "$code" == '202' ] ; then
  430. _err "$d:Challenge error: $resource"
  431. _clearup
  432. return 1
  433. fi
  434. while [ "1" ] ; do
  435. _debug "sleep 5 secs to verify"
  436. sleep 5
  437. _debug "checking"
  438. if ! _get $uri ; then
  439. _err "$d:Verify error:$resource"
  440. _clearup
  441. return 1
  442. fi
  443. status=$(echo $response | egrep -o '"status":"[^"]+"' | cut -d : -f 2 | sed 's/"//g')
  444. if [ "$status" == "valid" ] ; then
  445. _info "Success"
  446. break;
  447. fi
  448. if [ "$status" == "invalid" ] ; then
  449. error=$(echo $response | egrep -o '"error":{[^}]*}' | grep -o '"detail":"[^"]*"' | cut -d '"' -f 4)
  450. _err "$d:Verify error:$error"
  451. _clearup
  452. return 1;
  453. fi
  454. if [ "$status" == "pending" ] ; then
  455. _info "Pending"
  456. else
  457. _err "$d:Verify error:$response"
  458. _clearup
  459. return 1
  460. fi
  461. done
  462. _stopserver $serverproc
  463. done
  464. _clearup
  465. _info "Verify finished, start to sign."
  466. der="$(openssl req -in $CSR_PATH -outform DER | base64 -w 0 | _b64)"
  467. _send_signed_request "$API/acme/new-cert" "{\"resource\": \"new-cert\", \"csr\": \"$der\"}" "needbase64"
  468. Le_LinkCert="$(grep -i -o '^Location.*' $CURL_HEADER |sed 's/\r//g'| cut -d " " -f 2)"
  469. _setopt "$DOMAIN_CONF" "Le_LinkCert" "=" "$Le_LinkCert"
  470. if [ "$Le_LinkCert" ] ; then
  471. echo -----BEGIN CERTIFICATE----- > "$CERT_PATH"
  472. curl --silent "$Le_LinkCert" | base64 >> "$CERT_PATH"
  473. echo -----END CERTIFICATE----- >> "$CERT_PATH"
  474. _info "Cert success."
  475. cat "$CERT_PATH"
  476. _info "Your cert is in $CERT_PATH"
  477. fi
  478. if [ -z "$Le_LinkCert" ] ; then
  479. response="$(echo $response | base64 -d)"
  480. _err "Sign failed: $(echo "$response" | grep -o '"detail":"[^"]*"')"
  481. return 1
  482. fi
  483. Le_LinkIssuer=$(grep -i '^Link' $CURL_HEADER | cut -d " " -f 2| cut -d ';' -f 1 | sed 's/<//g' | sed 's/>//g')
  484. _setopt "$DOMAIN_CONF" "Le_LinkIssuer" "=" "$Le_LinkIssuer"
  485. if [ "$Le_LinkIssuer" ] ; then
  486. echo -----BEGIN CERTIFICATE----- > "$CA_CERT_PATH"
  487. curl --silent "$Le_LinkIssuer" | base64 >> "$CA_CERT_PATH"
  488. echo -----END CERTIFICATE----- >> "$CA_CERT_PATH"
  489. _info "The intermediate CA cert is in $CA_CERT_PATH"
  490. fi
  491. Le_CertCreateTime=$(date -u "+%s")
  492. _setopt "$DOMAIN_CONF" "Le_CertCreateTime" "=" "$Le_CertCreateTime"
  493. Le_CertCreateTimeStr=$(date -u "+%Y-%m-%d %H:%M:%S UTC")
  494. _setopt "$DOMAIN_CONF" "Le_CertCreateTimeStr" "=" "\"$Le_CertCreateTimeStr\""
  495. if [ ! "$Le_RenewalDays" ] ; then
  496. Le_RenewalDays=50
  497. fi
  498. _setopt "$DOMAIN_CONF" "Le_RenewalDays" "=" "$Le_RenewalDays"
  499. Le_NextRenewTime=$(date -u -d "+$Le_RenewalDays day" "+%s")
  500. _setopt "$DOMAIN_CONF" "Le_NextRenewTime" "=" "$Le_NextRenewTime"
  501. Le_NextRenewTimeStr=$(date -u -d "+$Le_RenewalDays day" "+%Y-%m-%d %H:%M:%S UTC")
  502. _setopt "$DOMAIN_CONF" "Le_NextRenewTimeStr" "=" "\"$Le_NextRenewTimeStr\""
  503. if [ "$Le_RealCertPath" ] ; then
  504. if [ -f "$Le_RealCertPath" ] ; then
  505. rm -f "$Le_RealCertPath"
  506. fi
  507. ln -s "$CERT_PATH" "$Le_RealCertPath"
  508. fi
  509. if [ "$Le_RealCACertPath" ] ; then
  510. if [ -f "$Le_RealCACertPath" ] ; then
  511. rm -f "$Le_RealCACertPath"
  512. fi
  513. ln -s "$CA_CERT_PATH" "$Le_RealCACertPath"
  514. fi
  515. if [ "$Le_RealKeyPath" ] ; then
  516. if [ -f "$Le_RealKeyPath" ] ; then
  517. rm -f "$Le_RealKeyPath"
  518. fi
  519. ln -s "$CERT_KEY_PATH" "$Le_RealKeyPath"
  520. fi
  521. if [ "$Le_ReloadCmd" ] ; then
  522. _info "Run Le_ReloadCmd: $Le_ReloadCmd"
  523. "$Le_ReloadCmd"
  524. fi
  525. }
  526. renew() {
  527. Le_Domain="$1"
  528. if [ -z "$Le_Domain" ] ; then
  529. echo Usage: $0 domain.com
  530. return 1
  531. fi
  532. issue $Le_Domain
  533. }
  534. renewAll() {
  535. _initpath
  536. _info "renewAll"
  537. for d in $(ls -F $WORKING_DIR | grep '/$') ; do
  538. d=$(echo $d | cut -d '/' -f 1)
  539. _info "renew $d"
  540. Le_LinkCert=""
  541. Le_Domain=""
  542. Le_Alt=""
  543. Le_Webroot=""
  544. Le_Keylength=""
  545. Le_LinkIssuer=""
  546. Le_CertCreateTime=""
  547. Le_CertCreateTimeStr=""
  548. Le_RenewalDays=""
  549. Le_NextRenewTime=""
  550. Le_NextRenewTimeStr=""
  551. Le_RealCertPath=""
  552. Le_RealKeyPath=""
  553. Le_RealCACertPath=""
  554. Le_ReloadCmd=""
  555. renew "$d"
  556. done
  557. }
  558. install() {
  559. _initpath
  560. if ! command -v "curl" > /dev/null ; then
  561. _err "Please install curl first."
  562. _err "Ubuntu: sudo apt-get install curl"
  563. _err "CentOS: yum install curl"
  564. return 1
  565. fi
  566. if ! command -v "crontab" > /dev/null ; then
  567. _err "Please install crontab first."
  568. _err "CentOs: yum -y install crontabs"
  569. return 1
  570. fi
  571. if ! command -v "openssl" > /dev/null ; then
  572. _err "Please install openssl first."
  573. _err "CentOs: yum -y install openssl"
  574. return 1
  575. fi
  576. if ! command -v "xxd" > /dev/null ; then
  577. _err "Please install xxd first."
  578. _err "CentOs: yum install vim-common"
  579. return 1
  580. fi
  581. _info "Installing to $WORKING_DIR"
  582. if [ ! -f /bin/le.sh ] ; then
  583. cp le.sh "/bin/"
  584. chmod +x "/bin/le.sh"
  585. ln -s "/bin/le.sh" /bin/le
  586. fi
  587. _info "Installing cron job"
  588. if command -v sudo > /dev/null ; then
  589. if [ "$(sudo -n uptime 2>&1|grep "load"|wc -l)" != "0" ] ; then
  590. SUDO=sudo
  591. fi
  592. fi
  593. if ! crontab -l | grep 'le renewAll' ; then
  594. crontab -l | { cat; echo "0 0 * * * $SUDO le renewAll > /dev/null"; } | crontab -
  595. if command -v crond > /dev/null ; then
  596. service crond reload >/dev/null
  597. else
  598. service cron reload >/dev/null
  599. fi
  600. fi
  601. _info OK
  602. }
  603. uninstall() {
  604. _initpath
  605. _info "Removing cron job"
  606. if crontab -l | grep 'le.*renewAll' ; then
  607. crontab -l | sed "/le.*renewAll/d" | crontab -
  608. if command -v crond > /dev/null ; then
  609. service crond reload >/dev/null
  610. else
  611. service cron reload >/dev/null
  612. fi
  613. fi
  614. _info "Removing /bin/le.sh"
  615. rm -f /bin/le
  616. rm -f /bin/le.sh
  617. _info "The keys and certs are in $WORKING_DIR, you can remove them by yourself."
  618. }
  619. showhelp() {
  620. echo "Usage: issue|renew|renewAll|createAccountKey|createDomainKey|createCSR|install|uninstall"
  621. }
  622. if [ -z "$1" ] ; then
  623. showhelp
  624. else
  625. "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
  626. fi