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.

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