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.

838 lines
20 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
  1. #!/bin/bash
  2. VER=1.0.5
  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" ] && ! [ "$FORCE" ] ; then
  71. if [ "$IS_RENEW" ] ; then
  72. _info "Domain key exists, skip"
  73. return 0
  74. else
  75. _err "Domain key exists, do you want to overwrite the key?"
  76. _err "Set FORCE=1, and try again."
  77. return 1
  78. fi
  79. else
  80. #generate account key
  81. openssl genrsa $length > "$CERT_KEY_PATH"
  82. fi
  83. }
  84. # domain domainlist
  85. createCSR() {
  86. if [ -z "$1" ] ; then
  87. echo Usage: $0 domain [domainlist]
  88. return
  89. fi
  90. domain=$1
  91. _initpath $domain
  92. domainlist=$2
  93. if [ -f "$CSR_PATH" ] && [ "$IS_RENEW" ]; then
  94. _info "CSR exists, skip"
  95. return
  96. fi
  97. if [ -z "$domainlist" ] ; then
  98. #single domain
  99. _info "Single domain" $domain
  100. openssl req -new -sha256 -key "$CERT_KEY_PATH" -subj "/CN=$domain" > "$CSR_PATH"
  101. else
  102. alt="DNS:$(echo $domainlist | sed "s/,/,DNS:/g")"
  103. #multi
  104. _info "Multi domain" "$alt"
  105. 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"
  106. fi
  107. }
  108. _b64() {
  109. __n=$(cat)
  110. echo $__n | tr '/+' '_-' | tr -d '= '
  111. }
  112. _send_signed_request() {
  113. url=$1
  114. payload=$2
  115. needbase64=$3
  116. _debug url $url
  117. _debug payload "$payload"
  118. CURL_HEADER="$WORKING_DIR/curl.header"
  119. dp="$WORKING_DIR/curl.dump"
  120. CURL="curl --silent --dump-header $CURL_HEADER "
  121. if [ "$DEBUG" ] ; then
  122. CURL="$CURL --trace-ascii $dp "
  123. fi
  124. payload64=$(echo -n $payload | base64 -w 0 | _b64)
  125. _debug payload64 $payload64
  126. nonceurl="$API/directory"
  127. nonce=$($CURL -I $nonceurl | grep "^Replay-Nonce:" | sed s/\\r//|sed s/\\n//| cut -d ' ' -f 2)
  128. _debug nonce $nonce
  129. protected=$(echo -n "$HEADERPLACE" | sed "s/NONCE/$nonce/" )
  130. _debug protected "$protected"
  131. protected64=$( echo -n $protected | base64 -w 0 | _b64)
  132. _debug protected64 "$protected64"
  133. sig=$(echo -n "$protected64.$payload64" | openssl dgst -sha256 -sign $ACCOUNT_KEY_PATH | base64 -w 0 | _b64)
  134. _debug sig "$sig"
  135. body="{\"header\": $HEADER, \"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}"
  136. _debug body "$body"
  137. if [ "$needbase64" ] ; then
  138. response="$($CURL -X POST --data "$body" $url | base64 -w 0)"
  139. else
  140. response="$($CURL -X POST --data "$body" $url)"
  141. fi
  142. responseHeaders="$(sed 's/\r//g' $CURL_HEADER)"
  143. _debug responseHeaders "$responseHeaders"
  144. _debug response "$response"
  145. code="$(grep ^HTTP $CURL_HEADER | tail -1 | cut -d " " -f 2)"
  146. _debug code $code
  147. }
  148. _get() {
  149. url="$1"
  150. _debug url $url
  151. response="$(curl --silent $url)"
  152. ret=$?
  153. _debug response "$response"
  154. code="$(echo $response | grep -o '"status":[0-9]\+' | cut -d : -f 2)"
  155. _debug code $code
  156. return $ret
  157. }
  158. #setopt "file" "opt" "=" "value" [";"]
  159. _setopt() {
  160. __conf="$1"
  161. __opt="$2"
  162. __sep="$3"
  163. __val="$4"
  164. __end="$5"
  165. if [ -z "$__opt" ] ; then
  166. echo usage: $0 '"file" "opt" "=" "value" [";"]'
  167. return
  168. fi
  169. if [ ! -f "$__conf" ] ; then
  170. touch "$__conf"
  171. fi
  172. if grep -H -n "^$__opt$__sep" "$__conf" > /dev/null ; then
  173. _debug OK
  174. if [[ "$__val" == *"&"* ]] ; then
  175. __val="$(echo $__val | sed 's/&/\\&/g')"
  176. fi
  177. sed -i "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" "$__conf"
  178. else
  179. _debug APP
  180. echo "$__opt$__sep$__val$__end" >> "$__conf"
  181. fi
  182. _debug "$(grep -H -n "^$__opt$__sep" $__conf)"
  183. }
  184. _startserver() {
  185. content="$1"
  186. # while true ; do
  187. if [ "$DEBUG" ] ; then
  188. echo -e -n "HTTP/1.1 200 OK\r\n\r\n$content" | nc -q 1 -l -p 80
  189. else
  190. echo -e -n "HTTP/1.1 200 OK\r\n\r\n$content" | nc -q 1 -l -p 80 > /dev/null
  191. fi
  192. # done
  193. }
  194. _stopserver() {
  195. pid="$1"
  196. if [ "$pid" ] ; then
  197. if [ "$DEBUG" ] ; then
  198. # kill -s 9 $pid
  199. # killall -s 9 nc
  200. else
  201. # kill -s 9 $pid > /dev/null
  202. # wait $pid 2>/dev/null
  203. # killall -s 9 nc > /dev/null
  204. fi
  205. fi
  206. }
  207. _initpath() {
  208. if [ -z "$WORKING_DIR" ]; then
  209. WORKING_DIR=$HOME/.le
  210. fi
  211. if [ -z "$ACME_DIR" ] ; then
  212. ACME_DIR="/home/.acme"
  213. fi
  214. if [ -z "$APACHE_CONF_BACKUP_DIR" ] ; then
  215. APACHE_CONF_BACKUP_DIR="$WORKING_DIR/"
  216. fi
  217. domain="$1"
  218. mkdir -p "$WORKING_DIR"
  219. if [ -z "$ACCOUNT_KEY_PATH" ] ; then
  220. ACCOUNT_KEY_PATH="$WORKING_DIR/account.acc"
  221. fi
  222. if [ -z "$domain" ] ; then
  223. return 0
  224. fi
  225. mkdir -p "$WORKING_DIR/$domain"
  226. if [ -z "$DOMAIN_CONF" ] ; then
  227. DOMAIN_CONF="$WORKING_DIR/$domain/$Le_Domain.conf"
  228. fi
  229. if [ -z "$CSR_PATH" ] ; then
  230. CSR_PATH="$WORKING_DIR/$domain/$domain.csr"
  231. fi
  232. if [ -z "$CERT_KEY_PATH" ] ; then
  233. CERT_KEY_PATH="$WORKING_DIR/$domain/$domain.key"
  234. fi
  235. if [ -z "$CERT_PATH" ] ; then
  236. CERT_PATH="$WORKING_DIR/$domain/$domain.cer"
  237. fi
  238. if [ -z "$CA_CERT_PATH" ] ; then
  239. CA_CERT_PATH="$WORKING_DIR/$domain/ca.cer"
  240. fi
  241. }
  242. _apachePath() {
  243. httpdroot="$(apachectl -V | grep HTTPD_ROOT= | cut -d = -f 2 | sed s/\"//g)"
  244. httpdconfname="$(apachectl -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | sed s/\"//g)"
  245. httpdconf="$httpdroot/$httpdconfname"
  246. if [ ! -f $httpdconf ] ; then
  247. _err "Apache Config file not found" $httpdconf
  248. return 1
  249. fi
  250. return 0
  251. }
  252. _restoreApache() {
  253. if [ -z "$usingApache" ] ; then
  254. return 0
  255. fi
  256. _initpath
  257. if ! _apachePath ; then
  258. return 1
  259. fi
  260. if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ] ; then
  261. _debug "No config file to restore."
  262. return 0
  263. fi
  264. cp -p "$APACHE_CONF_BACKUP_DIR/$httpdconfname" "$httpdconf"
  265. if ! apachectl -t ; then
  266. _err "Sorry, restore apache config error, please contact me."
  267. return 1;
  268. fi
  269. rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname"
  270. return 0
  271. }
  272. _setApache() {
  273. _initpath
  274. if ! _apachePath ; then
  275. return 1
  276. fi
  277. #backup the conf
  278. _debug "Backup apache config file" $httpdconf
  279. cp -p $httpdconf $APACHE_CONF_BACKUP_DIR/
  280. _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname"
  281. _info "In case there is an error that can not be restored automatically, you may try restore it yourself."
  282. _info "The backup file will be deleted on sucess, just forget it."
  283. #add alias
  284. echo "
  285. Alias /.well-known/acme-challenge $ACME_DIR
  286. <Directory $ACME_DIR >
  287. Require all granted
  288. </Directory>
  289. " >> $httpdconf
  290. if ! apachectl -t ; then
  291. _err "Sorry, apache config error, please contact me."
  292. _restoreApache
  293. return 1;
  294. fi
  295. if [ ! -d "$ACME_DIR" ] ; then
  296. mkdir -p "$ACME_DIR"
  297. chmod 755 "$ACME_DIR"
  298. fi
  299. if ! apachectl graceful ; then
  300. _err "Sorry, apachectl graceful error, please contact me."
  301. _restoreApache
  302. return 1;
  303. fi
  304. usingApache="1"
  305. return 0
  306. }
  307. _clearup () {
  308. _stopserver $serverproc
  309. serverproc=""
  310. _restoreApache
  311. }
  312. issue() {
  313. if [ -z "$2" ] ; then
  314. _err "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"
  315. return 1
  316. fi
  317. Le_Webroot="$1"
  318. Le_Domain="$2"
  319. Le_Alt="$3"
  320. Le_Keylength="$4"
  321. Le_RealCertPath="$5"
  322. Le_RealKeyPath="$6"
  323. Le_RealCACertPath="$7"
  324. Le_ReloadCmd="$8"
  325. _initpath $Le_Domain
  326. if [ -f "$DOMAIN_CONF" ] ; then
  327. Le_NextRenewTime=$(grep "^Le_NextRenewTime=" "$DOMAIN_CONF" | cut -d '=' -f 2)
  328. if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(date -u "+%s" )" -lt "$Le_NextRenewTime" ] ; then
  329. _info "Skip, Next renewal time is: $(grep "^Le_NextRenewTimeStr" "$DOMAIN_CONF" | cut -d '=' -f 2)"
  330. return 2
  331. fi
  332. fi
  333. if [ "$Le_Alt" == "no" ] ; then
  334. Le_Alt=""
  335. fi
  336. if [ "$Le_Keylength" == "no" ] ; then
  337. Le_Keylength=""
  338. fi
  339. if [ "$Le_RealCertPath" == "no" ] ; then
  340. Le_RealCertPath=""
  341. fi
  342. if [ "$Le_RealKeyPath" == "no" ] ; then
  343. Le_RealKeyPath=""
  344. fi
  345. if [ "$Le_RealCACertPath" == "no" ] ; then
  346. Le_RealCACertPath=""
  347. fi
  348. if [ "$Le_ReloadCmd" == "no" ] ; then
  349. Le_ReloadCmd=""
  350. fi
  351. _setopt "$DOMAIN_CONF" "Le_Domain" "=" "$Le_Domain"
  352. _setopt "$DOMAIN_CONF" "Le_Alt" "=" "$Le_Alt"
  353. _setopt "$DOMAIN_CONF" "Le_Webroot" "=" "$Le_Webroot"
  354. _setopt "$DOMAIN_CONF" "Le_Keylength" "=" "$Le_Keylength"
  355. _setopt "$DOMAIN_CONF" "Le_RealCertPath" "=" "\"$Le_RealCertPath\""
  356. _setopt "$DOMAIN_CONF" "Le_RealCACertPath" "=" "\"$Le_RealCACertPath\""
  357. _setopt "$DOMAIN_CONF" "Le_RealKeyPath" "=" "\"$Le_RealKeyPath\""
  358. _setopt "$DOMAIN_CONF" "Le_ReloadCmd" "=" "\"$Le_ReloadCmd\""
  359. if [ "$Le_Webroot" == "no" ] ; then
  360. _info "Standalone mode."
  361. if ! command -v "nc" > /dev/null ; then
  362. _err "Please install netcat(nc) tools first."
  363. return 1
  364. fi
  365. netprc="$(ss -ntpl | grep ':80 ')"
  366. if [ "$netprc" ] ; then
  367. _err "$netprc"
  368. _err "tcp port 80 is already used by $(echo "$netprc" | cut -d : -f 4)"
  369. _err "Please stop it first"
  370. return 1
  371. fi
  372. fi
  373. if [ "$Le_Webroot" == "apache" ] ; then
  374. if ! _setApache ; then
  375. _err "set up apache error. Report error to me."
  376. return 1
  377. fi
  378. wellknown_path="$ACME_DIR"
  379. else
  380. usingApache=""
  381. fi
  382. createAccountKey $Le_Domain $Le_Keylength
  383. if ! createDomainKey $Le_Domain $Le_Keylength ; then
  384. _err "Create domain key error."
  385. return 1
  386. fi
  387. if ! createCSR $Le_Domain $Le_Alt ; then
  388. _err "Create CSR error."
  389. return 1
  390. fi
  391. 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)
  392. if [ "${#pub_exp}" == "5" ] ; then
  393. pub_exp=0$pub_exp
  394. fi
  395. _debug pub_exp "$pub_exp"
  396. e=$(echo $pub_exp | xxd -r -p | base64)
  397. _debug e "$e"
  398. modulus=$(openssl rsa -in $ACCOUNT_KEY_PATH -modulus -noout | cut -d '=' -f 2 )
  399. n=$(echo $modulus| xxd -r -p | base64 -w 0 | _b64 )
  400. jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}'
  401. HEADER='{"alg": "RS256", "jwk": '$jwk'}'
  402. HEADERPLACE='{"nonce": "NONCE", "alg": "RS256", "jwk": '$jwk'}'
  403. _debug HEADER "$HEADER"
  404. accountkey_json=$(echo -n "$jwk" | sed "s/ //g")
  405. thumbprint=$(echo -n "$accountkey_json" | sha256sum | xxd -r -p | base64 -w 0 | _b64)
  406. _info "Registering account"
  407. regjson='{"resource": "new-reg", "agreement": "'$AGREEMENT'"}'
  408. if [ "$ACCOUNT_EMAIL" ] ; then
  409. regjson='{"resource": "new-reg", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "agreement": "'$AGREEMENT'"}'
  410. fi
  411. _send_signed_request "$API/acme/new-reg" "$regjson"
  412. if [ "$code" == "" ] || [ "$code" == '201' ] ; then
  413. _info "Registered"
  414. echo $response > $WORKING_DIR/account.json
  415. elif [ "$code" == '409' ] ; then
  416. _info "Already registered"
  417. else
  418. _err "Register account Error."
  419. _clearup
  420. return 1
  421. fi
  422. # verify each domain
  423. _info "Verify each domain"
  424. alldomains=$(echo "$Le_Domain,$Le_Alt" | sed "s/,/ /g")
  425. for d in $alldomains
  426. do
  427. _info "Verifing domain" $d
  428. _send_signed_request "$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$d\"}}"
  429. if [ ! -z "$code" ] && [ ! "$code" == '201' ] ; then
  430. _err "new-authz error: $response"
  431. _clearup
  432. return 1
  433. fi
  434. http01=$(echo $response | egrep -o '{[^{]*"type":"http-01"[^}]*')
  435. _debug http01 "$http01"
  436. token=$(echo "$http01" | sed 's/,/\n'/g| grep '"token":'| cut -d : -f 2|sed 's/"//g')
  437. _debug token $token
  438. uri=$(echo "$http01" | sed 's/,/\n'/g| grep '"uri":'| cut -d : -f 2,3|sed 's/"//g')
  439. _debug uri $uri
  440. keyauthorization="$token.$thumbprint"
  441. _debug keyauthorization "$keyauthorization"
  442. if [ "$Le_Webroot" == "no" ] ; then
  443. _info "Standalone mode server"
  444. _startserver "$keyauthorization" &
  445. serverproc="$!"
  446. sleep 2
  447. _debug serverproc $serverproc
  448. else
  449. if [ -z "$wellknown_path" ] ; then
  450. wellknown_path="$Le_Webroot/.well-known/acme-challenge"
  451. fi
  452. _debug wellknown_path "$wellknown_path"
  453. mkdir -p "$wellknown_path"
  454. echo -n "$keyauthorization" > "$wellknown_path/$token"
  455. fi
  456. wellknown_url="http://$d/.well-known/acme-challenge/$token"
  457. _debug wellknown_url "$wellknown_url"
  458. _debug challenge "$challenge"
  459. _send_signed_request $uri "{\"resource\": \"challenge\", \"keyAuthorization\": \"$keyauthorization\"}"
  460. if [ ! -z "$code" ] && [ ! "$code" == '202' ] ; then
  461. _err "$d:Challenge error: $resource"
  462. _clearup
  463. return 1
  464. fi
  465. while [ "1" ] ; do
  466. _debug "sleep 5 secs to verify"
  467. sleep 5
  468. _debug "checking"
  469. if ! _get $uri ; then
  470. _err "$d:Verify error:$resource"
  471. _clearup
  472. return 1
  473. fi
  474. status=$(echo $response | egrep -o '"status":"[^"]+"' | cut -d : -f 2 | sed 's/"//g')
  475. if [ "$status" == "valid" ] ; then
  476. _info "Success"
  477. break;
  478. fi
  479. if [ "$status" == "invalid" ] ; then
  480. error=$(echo $response | egrep -o '"error":{[^}]*}' | grep -o '"detail":"[^"]*"' | cut -d '"' -f 4)
  481. _err "$d:Verify error:$error"
  482. _clearup
  483. return 1;
  484. fi
  485. if [ "$status" == "pending" ] ; then
  486. _info "Pending"
  487. else
  488. _err "$d:Verify error:$response"
  489. _clearup
  490. return 1
  491. fi
  492. done
  493. _stopserver $serverproc
  494. serverproc=""
  495. done
  496. _clearup
  497. _info "Verify finished, start to sign."
  498. der="$(openssl req -in $CSR_PATH -outform DER | base64 -w 0 | _b64)"
  499. _send_signed_request "$API/acme/new-cert" "{\"resource\": \"new-cert\", \"csr\": \"$der\"}" "needbase64"
  500. Le_LinkCert="$(grep -i -o '^Location.*' $CURL_HEADER |sed 's/\r//g'| cut -d " " -f 2)"
  501. _setopt "$DOMAIN_CONF" "Le_LinkCert" "=" "$Le_LinkCert"
  502. if [ "$Le_LinkCert" ] ; then
  503. echo -----BEGIN CERTIFICATE----- > "$CERT_PATH"
  504. curl --silent "$Le_LinkCert" | base64 >> "$CERT_PATH"
  505. echo -----END CERTIFICATE----- >> "$CERT_PATH"
  506. _info "Cert success."
  507. cat "$CERT_PATH"
  508. _info "Your cert is in $CERT_PATH"
  509. fi
  510. if [ -z "$Le_LinkCert" ] ; then
  511. response="$(echo $response | base64 -d)"
  512. _err "Sign failed: $(echo "$response" | grep -o '"detail":"[^"]*"')"
  513. return 1
  514. fi
  515. Le_LinkIssuer=$(grep -i '^Link' $CURL_HEADER | cut -d " " -f 2| cut -d ';' -f 1 | sed 's/<//g' | sed 's/>//g')
  516. _setopt "$DOMAIN_CONF" "Le_LinkIssuer" "=" "$Le_LinkIssuer"
  517. if [ "$Le_LinkIssuer" ] ; then
  518. echo -----BEGIN CERTIFICATE----- > "$CA_CERT_PATH"
  519. curl --silent "$Le_LinkIssuer" | base64 >> "$CA_CERT_PATH"
  520. echo -----END CERTIFICATE----- >> "$CA_CERT_PATH"
  521. _info "The intermediate CA cert is in $CA_CERT_PATH"
  522. fi
  523. Le_CertCreateTime=$(date -u "+%s")
  524. _setopt "$DOMAIN_CONF" "Le_CertCreateTime" "=" "$Le_CertCreateTime"
  525. Le_CertCreateTimeStr=$(date -u "+%Y-%m-%d %H:%M:%S UTC")
  526. _setopt "$DOMAIN_CONF" "Le_CertCreateTimeStr" "=" "\"$Le_CertCreateTimeStr\""
  527. if [ ! "$Le_RenewalDays" ] ; then
  528. Le_RenewalDays=80
  529. fi
  530. _setopt "$DOMAIN_CONF" "Le_RenewalDays" "=" "$Le_RenewalDays"
  531. Le_NextRenewTime=$(date -u -d "+$Le_RenewalDays day" "+%s")
  532. _setopt "$DOMAIN_CONF" "Le_NextRenewTime" "=" "$Le_NextRenewTime"
  533. Le_NextRenewTimeStr=$(date -u -d "+$Le_RenewalDays day" "+%Y-%m-%d %H:%M:%S UTC")
  534. _setopt "$DOMAIN_CONF" "Le_NextRenewTimeStr" "=" "\"$Le_NextRenewTimeStr\""
  535. if [ "$Le_RealCertPath" ] ; then
  536. if [ -f "$Le_RealCertPath" ] ; then
  537. cp -p "$Le_RealCertPath" "$Le_RealCertPath".bak
  538. fi
  539. cat "$CERT_PATH" > "$Le_RealCertPath"
  540. fi
  541. if [ "$Le_RealCACertPath" ] ; then
  542. if [ -f "$Le_RealCACertPath" ] ; then
  543. cp -p "$Le_RealCACertPath" "$Le_RealCACertPath".bak
  544. fi
  545. cat "$CA_CERT_PATH" > "$Le_RealCACertPath"
  546. fi
  547. if [ "$Le_RealKeyPath" ] ; then
  548. if [ -f "$Le_RealKeyPath" ] ; then
  549. cp -p "$Le_RealKeyPath" "$Le_RealKeyPath".bak
  550. fi
  551. cat "$CERT_KEY_PATH" > "$Le_RealKeyPath"
  552. fi
  553. if [ "$Le_ReloadCmd" ] ; then
  554. _info "Run Le_ReloadCmd: $Le_ReloadCmd"
  555. $Le_ReloadCmd
  556. fi
  557. }
  558. renew() {
  559. Le_Domain="$1"
  560. if [ -z "$Le_Domain" ] ; then
  561. _err "Usage: $0 domain.com"
  562. return 1
  563. fi
  564. _initpath $Le_Domain
  565. if [ -f "$DOMAIN_CONF" ] ; then
  566. source "$DOMAIN_CONF"
  567. if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(date -u "+%s" )" -lt "$Le_NextRenewTime" ] ; then
  568. _info "Skip, Next renewal time is: $Le_NextRenewTimeStr"
  569. return 2
  570. fi
  571. fi
  572. IS_RENEW="1"
  573. issue "$Le_Webroot" "$Le_Domain" "$Le_Alt" "$Le_Keylength" "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd"
  574. IS_RENEW=""
  575. }
  576. renewAll() {
  577. _initpath
  578. _info "renewAll"
  579. for d in $(ls -F $WORKING_DIR | grep '/$') ; do
  580. d=$(echo $d | cut -d '/' -f 1)
  581. _info "renew $d"
  582. Le_LinkCert=""
  583. Le_Domain=""
  584. Le_Alt=""
  585. Le_Webroot=""
  586. Le_Keylength=""
  587. Le_LinkIssuer=""
  588. Le_CertCreateTime=""
  589. Le_CertCreateTimeStr=""
  590. Le_RenewalDays=""
  591. Le_NextRenewTime=""
  592. Le_NextRenewTimeStr=""
  593. Le_RealCertPath=""
  594. Le_RealKeyPath=""
  595. Le_RealCACertPath=""
  596. Le_ReloadCmd=""
  597. DOMAIN_CONF=""
  598. CSR_PATH=""
  599. CERT_KEY_PATH=""
  600. CERT_PATH=""
  601. CA_CERT_PATH=""
  602. ACCOUNT_KEY_PATH=""
  603. wellknown_path=""
  604. renew "$d"
  605. done
  606. }
  607. install() {
  608. _initpath
  609. if ! command -v "curl" > /dev/null ; then
  610. _err "Please install curl first."
  611. _err "Ubuntu: sudo apt-get install curl"
  612. _err "CentOS: yum install curl"
  613. return 1
  614. fi
  615. if ! command -v "crontab" > /dev/null ; then
  616. _err "Please install crontab first."
  617. _err "CentOs: yum -y install crontabs"
  618. return 1
  619. fi
  620. if ! command -v "openssl" > /dev/null ; then
  621. _err "Please install openssl first."
  622. _err "CentOs: yum -y install openssl"
  623. return 1
  624. fi
  625. if ! command -v "xxd" > /dev/null ; then
  626. _err "Please install xxd first."
  627. _err "CentOs: yum install vim-common"
  628. return 1
  629. fi
  630. _info "Installing to $WORKING_DIR"
  631. if [ ! -f /bin/le.sh ] ; then
  632. cp le.sh "/bin/"
  633. chmod +x "/bin/le.sh"
  634. ln -s "/bin/le.sh" /bin/le
  635. fi
  636. _info "Installing cron job"
  637. if command -v sudo > /dev/null ; then
  638. if [ "$(sudo -n uptime 2>&1|grep "load"|wc -l)" != "0" ] ; then
  639. SUDO=sudo
  640. fi
  641. fi
  642. if ! crontab -l | grep 'le renewAll' ; then
  643. crontab -l | { cat; echo "0 0 * * * $SUDO le renewAll > /dev/null"; } | crontab -
  644. if command -v crond > /dev/null ; then
  645. service crond reload >/dev/null
  646. else
  647. service cron reload >/dev/null
  648. fi
  649. fi
  650. _info OK
  651. }
  652. uninstall() {
  653. _initpath
  654. _info "Removing cron job"
  655. if crontab -l | grep 'le.*renewAll' ; then
  656. crontab -l | sed "/le.*renewAll/d" | crontab -
  657. if command -v crond > /dev/null ; then
  658. service crond reload >/dev/null
  659. else
  660. service cron reload >/dev/null
  661. fi
  662. fi
  663. _info "Removing /bin/le.sh"
  664. rm -f /bin/le
  665. rm -f /bin/le.sh
  666. _info "The keys and certs are in $WORKING_DIR, you can remove them by yourself."
  667. }
  668. version() {
  669. _info "$PROJECT"
  670. _info "v$VER"
  671. }
  672. showhelp() {
  673. version
  674. echo "Usage: issue|renew|renewAll|createAccountKey|createDomainKey|createCSR|install|uninstall|version"
  675. }
  676. if [ -z "$1" ] ; then
  677. showhelp
  678. else
  679. "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
  680. fi