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.

1354 lines
33 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
  1. #!/usr/bin/env bash
  2. VER=1.1.8
  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. STAGE_CA="https://acme-staging.api.letsencrypt.org"
  7. VTYPE_HTTP="http-01"
  8. VTYPE_DNS="dns-01"
  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. return 1
  36. }
  37. _h2b() {
  38. hex=$(cat)
  39. i=1
  40. j=2
  41. while [ '1' ] ; do
  42. h=$(printf $hex | cut -c $i-$j)
  43. if [ -z "$h" ] ; then
  44. break;
  45. fi
  46. printf "\x$h"
  47. let "i+=2"
  48. let "j+=2"
  49. done
  50. }
  51. _base64() {
  52. openssl base64 -e | tr -d '\n'
  53. }
  54. #domain [2048]
  55. createAccountKey() {
  56. _info "Creating account key"
  57. if [ -z "$1" ] ; then
  58. echo Usage: createAccountKey account-domain [2048]
  59. return
  60. fi
  61. account=$1
  62. length=$2
  63. if [[ "$length" == "ec-"* ]] ; then
  64. length=2048
  65. fi
  66. if [ -z "$2" ] ; then
  67. _info "Use default length 2048"
  68. length=2048
  69. fi
  70. _initpath
  71. if [ -f "$ACCOUNT_KEY_PATH" ] ; then
  72. _info "Account key exists, skip"
  73. return
  74. else
  75. #generate account key
  76. openssl genrsa $length 2>/dev/null > "$ACCOUNT_KEY_PATH"
  77. fi
  78. }
  79. #domain length
  80. createDomainKey() {
  81. _info "Creating domain key"
  82. if [ -z "$1" ] ; then
  83. echo Usage: createDomainKey domain [2048]
  84. return
  85. fi
  86. domain=$1
  87. length=$2
  88. isec=""
  89. if [[ "$length" == "ec-"* ]] ; then
  90. isec="1"
  91. length=$(printf $length | cut -d '-' -f 2-100)
  92. eccname="$length"
  93. fi
  94. if [ -z "$length" ] ; then
  95. if [ "$isec" ] ; then
  96. length=256
  97. else
  98. length=2048
  99. fi
  100. fi
  101. _info "Use length $length"
  102. if [ "$isec" ] ; then
  103. if [ "$length" == "256" ] ; then
  104. eccname="prime256v1"
  105. fi
  106. if [ "$length" == "384" ] ; then
  107. eccname="secp384r1"
  108. fi
  109. if [ "$length" == "521" ] ; then
  110. eccname="secp521r1"
  111. fi
  112. _info "Using ec name: $eccname"
  113. fi
  114. _initpath $domain
  115. if [ ! -f "$CERT_KEY_PATH" ] || ( [ "$FORCE" ] && ! [ "$IS_RENEW" ] ); then
  116. #generate account key
  117. if [ "$isec" ] ; then
  118. openssl ecparam -name $eccname -genkey 2>/dev/null > "$CERT_KEY_PATH"
  119. else
  120. openssl genrsa $length 2>/dev/null > "$CERT_KEY_PATH"
  121. fi
  122. else
  123. if [ "$IS_RENEW" ] ; then
  124. _info "Domain key exists, skip"
  125. return 0
  126. else
  127. _err "Domain key exists, do you want to overwrite the key?"
  128. _err "Set FORCE=1, and try again."
  129. return 1
  130. fi
  131. fi
  132. }
  133. # domain domainlist
  134. createCSR() {
  135. _info "Creating csr"
  136. if [ -z "$1" ] ; then
  137. echo Usage: $0 domain [domainlist]
  138. return
  139. fi
  140. domain=$1
  141. _initpath $domain
  142. domainlist=$2
  143. if [ -f "$CSR_PATH" ] && [ "$IS_RENEW" ] && ! [ "$FORCE" ]; then
  144. _info "CSR exists, skip"
  145. return
  146. fi
  147. if [ -z "$domainlist" ] ; then
  148. #single domain
  149. _info "Single domain" $domain
  150. printf "[ req_distinguished_name ]\n[ req ]\ndistinguished_name = req_distinguished_name\n" > "$DOMAIN_SSL_CONF"
  151. openssl req -new -sha256 -key "$CERT_KEY_PATH" -subj "/CN=$domain" -config "$DOMAIN_SSL_CONF" -out "$CSR_PATH"
  152. else
  153. alt="DNS:$(echo $domainlist | sed "s/,/,DNS:/g")"
  154. #multi
  155. _info "Multi domain" "$alt"
  156. printf "[ req_distinguished_name ]\n[ req ]\ndistinguished_name = req_distinguished_name\n[SAN]\nsubjectAltName=$alt" > "$DOMAIN_SSL_CONF"
  157. openssl req -new -sha256 -key "$CERT_KEY_PATH" -subj "/CN=$domain" -reqexts SAN -config "$DOMAIN_SSL_CONF" -out "$CSR_PATH"
  158. fi
  159. }
  160. _b64() {
  161. __n=$(cat)
  162. echo $__n | tr '/+' '_-' | tr -d '= '
  163. }
  164. _time2str() {
  165. #BSD
  166. if date -u -d@$1 2>/dev/null ; then
  167. return
  168. fi
  169. #Linux
  170. if date -u -r $1 2>/dev/null ; then
  171. return
  172. fi
  173. }
  174. _stat() {
  175. #Linux
  176. if stat -c '%U:%G' "$1" 2>/dev/null ; then
  177. return
  178. fi
  179. #BSD
  180. if stat -f '%Su:%Sg' "$1" 2>/dev/null ; then
  181. return
  182. fi
  183. }
  184. _send_signed_request() {
  185. url=$1
  186. payload=$2
  187. needbase64=$3
  188. _debug url $url
  189. _debug payload "$payload"
  190. CURL_HEADER="$LE_WORKING_DIR/curl.header"
  191. dp="$LE_WORKING_DIR/curl.dump"
  192. CURL="curl --silent --dump-header $CURL_HEADER "
  193. if [ "$DEBUG" ] ; then
  194. CURL="$CURL --trace-ascii $dp "
  195. fi
  196. payload64=$(echo -n $payload | _base64 | _b64)
  197. _debug payload64 $payload64
  198. nonceurl="$API/directory"
  199. nonce="$($CURL -I $nonceurl | grep -o "^Replay-Nonce:.*$" | tr -d "\r\n" | cut -d ' ' -f 2)"
  200. _debug nonce "$nonce"
  201. protected="$(printf "$HEADERPLACE" | sed "s/NONCE/$nonce/" )"
  202. _debug protected "$protected"
  203. protected64="$(printf "$protected" | _base64 | _b64)"
  204. _debug protected64 "$protected64"
  205. sig=$(echo -n "$protected64.$payload64" | openssl dgst -sha256 -sign $ACCOUNT_KEY_PATH | _base64 | _b64)
  206. _debug sig "$sig"
  207. body="{\"header\": $HEADER, \"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}"
  208. _debug body "$body"
  209. if [ "$needbase64" ] ; then
  210. response="$($CURL -X POST --data "$body" $url | _base64)"
  211. else
  212. response="$($CURL -X POST --data "$body" $url)"
  213. fi
  214. responseHeaders="$(cat $CURL_HEADER)"
  215. _debug responseHeaders "$responseHeaders"
  216. _debug response "$response"
  217. code="$(grep ^HTTP $CURL_HEADER | tail -1 | cut -d " " -f 2 | tr -d "\r\n" )"
  218. _debug code $code
  219. }
  220. _get() {
  221. url="$1"
  222. _debug url $url
  223. response="$(curl --silent $url)"
  224. ret=$?
  225. _debug response "$response"
  226. code="$(echo $response | grep -o '"status":[0-9]\+' | cut -d : -f 2)"
  227. _debug code $code
  228. return $ret
  229. }
  230. #setopt "file" "opt" "=" "value" [";"]
  231. _setopt() {
  232. __conf="$1"
  233. __opt="$2"
  234. __sep="$3"
  235. __val="$4"
  236. __end="$5"
  237. if [ -z "$__opt" ] ; then
  238. echo usage: _setopt '"file" "opt" "=" "value" [";"]'
  239. return
  240. fi
  241. if [ ! -f "$__conf" ] ; then
  242. touch "$__conf"
  243. fi
  244. if grep -H -n "^$__opt$__sep" "$__conf" > /dev/null ; then
  245. _debug OK
  246. if [[ "$__val" == *"&"* ]] ; then
  247. __val="$(echo $__val | sed 's/&/\\&/g')"
  248. fi
  249. text="$(cat $__conf)"
  250. echo "$text" | sed "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" > "$__conf"
  251. elif grep -H -n "^#$__opt$__sep" "$__conf" > /dev/null ; then
  252. if [[ "$__val" == *"&"* ]] ; then
  253. __val="$(echo $__val | sed 's/&/\\&/g')"
  254. fi
  255. text="$(cat $__conf)"
  256. echo "$text" | sed "s|^#$__opt$__sep.*$|$__opt$__sep$__val$__end|" > "$__conf"
  257. else
  258. _debug APP
  259. echo "$__opt$__sep$__val$__end" >> "$__conf"
  260. fi
  261. _debug "$(grep -H -n "^$__opt$__sep" $__conf)"
  262. }
  263. #_savedomainconf key value
  264. #save to domain.conf
  265. _savedomainconf() {
  266. key="$1"
  267. value="$2"
  268. if [ "$DOMAIN_CONF" ] ; then
  269. _setopt $DOMAIN_CONF "$key" "=" "$value"
  270. else
  271. _err "DOMAIN_CONF is empty, can not save $key=$value"
  272. fi
  273. }
  274. #_saveaccountconf key value
  275. _saveaccountconf() {
  276. key="$1"
  277. value="$2"
  278. if [ "$ACCOUNT_CONF_PATH" ] ; then
  279. _setopt $ACCOUNT_CONF_PATH "$key" "=" "$value"
  280. else
  281. _err "ACCOUNT_CONF_PATH is empty, can not save $key=$value"
  282. fi
  283. }
  284. _startserver() {
  285. content="$1"
  286. _NC="nc -q 1 -l"
  287. nchelp="$(nc -h 2>&1)"
  288. #centos
  289. if echo "$nchelp" | grep "nmap.org/ncat" >/dev/null ; then
  290. _NC="nc -l"
  291. fi
  292. #debian
  293. ncver="$(nc --version 2>&1)"
  294. if echo "$ncver" | grep "http://www.deepspace6.net" > /dev/null ; then
  295. _NC="$_NC -p"
  296. fi
  297. _debug "$_NC $Le_HTTPPort"
  298. # while true ; do
  299. if [ "$DEBUG" ] ; then
  300. echo -e -n "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort -vv
  301. else
  302. echo -e -n "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort > /dev/null
  303. fi
  304. if [ "$?" != "0" ] ; then
  305. _err "nc listen error."
  306. return 1
  307. fi
  308. # done
  309. }
  310. _stopserver() {
  311. pid="$1"
  312. }
  313. _initpath() {
  314. if [ -z "$LE_WORKING_DIR" ]; then
  315. LE_WORKING_DIR=$HOME/.le
  316. fi
  317. if [ -z "$ACCOUNT_CONF_PATH" ] ; then
  318. ACCOUNT_CONF_PATH="$LE_WORKING_DIR/account.conf"
  319. fi
  320. if [ -f "$ACCOUNT_CONF_PATH" ] ; then
  321. source "$ACCOUNT_CONF_PATH"
  322. fi
  323. if [ -z "$API" ] ; then
  324. if [ -z "$STAGE" ] ; then
  325. API="$DEFAULT_CA"
  326. else
  327. API="$STAGE_CA"
  328. _info "Using stage api:$API"
  329. fi
  330. fi
  331. if [ -z "$ACME_DIR" ] ; then
  332. ACME_DIR="/home/.acme"
  333. fi
  334. if [ -z "$APACHE_CONF_BACKUP_DIR" ] ; then
  335. APACHE_CONF_BACKUP_DIR="$LE_WORKING_DIR/"
  336. fi
  337. domain="$1"
  338. if ! mkdir -p "$LE_WORKING_DIR" ; then
  339. _err "Can not craete working dir: $LE_WORKING_DIR"
  340. return 1
  341. fi
  342. if [ -z "$ACCOUNT_KEY_PATH" ] ; then
  343. ACCOUNT_KEY_PATH="$LE_WORKING_DIR/account.key"
  344. fi
  345. if [ -z "$domain" ] ; then
  346. return 0
  347. fi
  348. domainhome="$LE_WORKING_DIR/$domain"
  349. mkdir -p "$domainhome"
  350. if [ -z "$DOMAIN_PATH" ] ; then
  351. DOMAIN_PATH="$domainhome"
  352. fi
  353. if [ -z "$DOMAIN_CONF" ] ; then
  354. DOMAIN_CONF="$domainhome/$domain.conf"
  355. fi
  356. if [ -z "$DOMAIN_SSL_CONF" ] ; then
  357. DOMAIN_SSL_CONF="$domainhome/$domain.ssl.conf"
  358. fi
  359. if [ -z "$CSR_PATH" ] ; then
  360. CSR_PATH="$domainhome/$domain.csr"
  361. fi
  362. if [ -z "$CERT_KEY_PATH" ] ; then
  363. CERT_KEY_PATH="$domainhome/$domain.key"
  364. fi
  365. if [ -z "$CERT_PATH" ] ; then
  366. CERT_PATH="$domainhome/$domain.cer"
  367. fi
  368. if [ -z "$CA_CERT_PATH" ] ; then
  369. CA_CERT_PATH="$domainhome/ca.cer"
  370. fi
  371. if [ -z "$CERT_FULLCHAIN_PATH" ] ; then
  372. CERT_FULLCHAIN_PATH="$domainhome/fullchain.cer"
  373. fi
  374. }
  375. _apachePath() {
  376. httpdroot="$(apachectl -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"' )"
  377. httpdconfname="$(apachectl -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"' )"
  378. httpdconf="$httpdroot/$httpdconfname"
  379. if [ ! -f $httpdconf ] ; then
  380. _err "Apache Config file not found" $httpdconf
  381. return 1
  382. fi
  383. return 0
  384. }
  385. _restoreApache() {
  386. if [ -z "$usingApache" ] ; then
  387. return 0
  388. fi
  389. _initpath
  390. if ! _apachePath ; then
  391. return 1
  392. fi
  393. if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ] ; then
  394. _debug "No config file to restore."
  395. return 0
  396. fi
  397. cp -p "$APACHE_CONF_BACKUP_DIR/$httpdconfname" "$httpdconf"
  398. if ! apachectl -t ; then
  399. _err "Sorry, restore apache config error, please contact me."
  400. return 1;
  401. fi
  402. rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname"
  403. return 0
  404. }
  405. _setApache() {
  406. _initpath
  407. if ! _apachePath ; then
  408. return 1
  409. fi
  410. #backup the conf
  411. _debug "Backup apache config file" $httpdconf
  412. cp -p $httpdconf $APACHE_CONF_BACKUP_DIR/
  413. _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname"
  414. _info "In case there is an error that can not be restored automatically, you may try restore it yourself."
  415. _info "The backup file will be deleted on sucess, just forget it."
  416. #add alias
  417. echo "
  418. Alias /.well-known/acme-challenge $ACME_DIR
  419. <Directory $ACME_DIR >
  420. Require all granted
  421. </Directory>
  422. " >> $httpdconf
  423. if ! apachectl -t ; then
  424. _err "Sorry, apache config error, please contact me."
  425. _restoreApache
  426. return 1;
  427. fi
  428. if [ ! -d "$ACME_DIR" ] ; then
  429. mkdir -p "$ACME_DIR"
  430. chmod 755 "$ACME_DIR"
  431. fi
  432. if ! apachectl graceful ; then
  433. _err "Sorry, apachectl graceful error, please contact me."
  434. _restoreApache
  435. return 1;
  436. fi
  437. usingApache="1"
  438. return 0
  439. }
  440. _clearup () {
  441. _stopserver $serverproc
  442. serverproc=""
  443. _restoreApache
  444. }
  445. # webroot removelevel tokenfile
  446. _clearupwebbroot() {
  447. __webroot="$1"
  448. if [ -z "$__webroot" ] ; then
  449. _debug "no webroot specified, skip"
  450. return 0
  451. fi
  452. if [ "$2" == '1' ] ; then
  453. _debug "remove $__webroot/.well-known"
  454. rm -rf "$__webroot/.well-known"
  455. elif [ "$2" == '2' ] ; then
  456. _debug "remove $__webroot/.well-known/acme-challenge"
  457. rm -rf "$__webroot/.well-known/acme-challenge"
  458. elif [ "$2" == '3' ] ; then
  459. _debug "remove $__webroot/.well-known/acme-challenge/$3"
  460. rm -rf "$__webroot/.well-known/acme-challenge/$3"
  461. else
  462. _info "Skip for removelevel:$2"
  463. fi
  464. return 0
  465. }
  466. issue() {
  467. if [ -z "$2" ] ; then
  468. _err "Usage: le issue webroot|no|apache|dns a.com [www.a.com,b.com,c.com]|no [key-length]|no"
  469. return 1
  470. fi
  471. Le_Webroot="$1"
  472. Le_Domain="$2"
  473. Le_Alt="$3"
  474. Le_Keylength="$4"
  475. Le_RealCertPath="$5"
  476. Le_RealKeyPath="$6"
  477. Le_RealCACertPath="$7"
  478. Le_ReloadCmd="$8"
  479. _initpath $Le_Domain
  480. if [ -f "$DOMAIN_CONF" ] ; then
  481. Le_NextRenewTime=$(grep "^Le_NextRenewTime=" "$DOMAIN_CONF" | cut -d '=' -f 2)
  482. if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(date -u "+%s" )" -lt "$Le_NextRenewTime" ] ; then
  483. _info "Skip, Next renewal time is: $(grep "^Le_NextRenewTimeStr" "$DOMAIN_CONF" | cut -d '=' -f 2)"
  484. return 2
  485. fi
  486. fi
  487. if [ "$Le_Alt" == "no" ] ; then
  488. Le_Alt=""
  489. fi
  490. if [ "$Le_Keylength" == "no" ] ; then
  491. Le_Keylength=""
  492. fi
  493. if [ "$Le_RealCertPath" == "no" ] ; then
  494. Le_RealCertPath=""
  495. fi
  496. if [ "$Le_RealKeyPath" == "no" ] ; then
  497. Le_RealKeyPath=""
  498. fi
  499. if [ "$Le_RealCACertPath" == "no" ] ; then
  500. Le_RealCACertPath=""
  501. fi
  502. if [ "$Le_ReloadCmd" == "no" ] ; then
  503. Le_ReloadCmd=""
  504. fi
  505. _setopt "$DOMAIN_CONF" "Le_Domain" "=" "$Le_Domain"
  506. _setopt "$DOMAIN_CONF" "Le_Alt" "=" "$Le_Alt"
  507. _setopt "$DOMAIN_CONF" "Le_Webroot" "=" "$Le_Webroot"
  508. _setopt "$DOMAIN_CONF" "Le_Keylength" "=" "$Le_Keylength"
  509. _setopt "$DOMAIN_CONF" "Le_RealCertPath" "=" "\"$Le_RealCertPath\""
  510. _setopt "$DOMAIN_CONF" "Le_RealCACertPath" "=" "\"$Le_RealCACertPath\""
  511. _setopt "$DOMAIN_CONF" "Le_RealKeyPath" "=" "\"$Le_RealKeyPath\""
  512. _setopt "$DOMAIN_CONF" "Le_ReloadCmd" "=" "\"$Le_ReloadCmd\""
  513. if [ "$Le_Webroot" == "no" ] ; then
  514. _info "Standalone mode."
  515. if ! command -v "nc" > /dev/null ; then
  516. _err "Please install netcat(nc) tools first."
  517. return 1
  518. fi
  519. if [ -z "$Le_HTTPPort" ] ; then
  520. Le_HTTPPort=80
  521. fi
  522. _setopt "$DOMAIN_CONF" "Le_HTTPPort" "=" "$Le_HTTPPort"
  523. netprc="$(ss -ntpl | grep :$Le_HTTPPort" ")"
  524. if [ "$netprc" ] ; then
  525. _err "$netprc"
  526. _err "tcp port $Le_HTTPPort is already used by $(echo "$netprc" | cut -d : -f 4)"
  527. _err "Please stop it first"
  528. return 1
  529. fi
  530. fi
  531. if [ "$Le_Webroot" == "apache" ] ; then
  532. if ! _setApache ; then
  533. _err "set up apache error. Report error to me."
  534. return 1
  535. fi
  536. wellknown_path="$ACME_DIR"
  537. else
  538. usingApache=""
  539. fi
  540. createAccountKey $Le_Domain $Le_Keylength
  541. if ! createDomainKey $Le_Domain $Le_Keylength ; then
  542. _err "Create domain key error."
  543. return 1
  544. fi
  545. if ! createCSR $Le_Domain $Le_Alt ; then
  546. _err "Create CSR error."
  547. return 1
  548. fi
  549. 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)
  550. if [ "${#pub_exp}" == "5" ] ; then
  551. pub_exp=0$pub_exp
  552. fi
  553. _debug pub_exp "$pub_exp"
  554. e=$(echo $pub_exp | _h2b | _base64)
  555. _debug e "$e"
  556. modulus=$(openssl rsa -in $ACCOUNT_KEY_PATH -modulus -noout | cut -d '=' -f 2 )
  557. n=$(echo $modulus| _h2b | _base64 | _b64 )
  558. jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}'
  559. HEADER='{"alg": "RS256", "jwk": '$jwk'}'
  560. HEADERPLACE='{"nonce": "NONCE", "alg": "RS256", "jwk": '$jwk'}'
  561. _debug HEADER "$HEADER"
  562. accountkey_json=$(echo -n "$jwk" | tr -d ' ' )
  563. thumbprint=$(echo -n "$accountkey_json" | openssl dgst -sha256 -binary | _base64 | _b64)
  564. _info "Registering account"
  565. regjson='{"resource": "new-reg", "agreement": "'$AGREEMENT'"}'
  566. if [ "$ACCOUNT_EMAIL" ] ; then
  567. regjson='{"resource": "new-reg", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "agreement": "'$AGREEMENT'"}'
  568. fi
  569. _send_signed_request "$API/acme/new-reg" "$regjson"
  570. if [ "$code" == "" ] || [ "$code" == '201' ] ; then
  571. _info "Registered"
  572. echo $response > $LE_WORKING_DIR/account.json
  573. elif [ "$code" == '409' ] ; then
  574. _info "Already registered"
  575. else
  576. _err "Register account Error."
  577. _clearup
  578. return 1
  579. fi
  580. vtype="$VTYPE_HTTP"
  581. if [[ "$Le_Webroot" == "dns"* ]] ; then
  582. vtype="$VTYPE_DNS"
  583. fi
  584. vlist="$Le_Vlist"
  585. # verify each domain
  586. _info "Verify each domain"
  587. sep='#'
  588. if [ -z "$vlist" ] ; then
  589. alldomains=$(echo "$Le_Domain,$Le_Alt" | tr ',' ' ' )
  590. for d in $alldomains
  591. do
  592. _info "Getting token for domain" $d
  593. _send_signed_request "$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$d\"}}"
  594. if [ ! -z "$code" ] && [ ! "$code" == '201' ] ; then
  595. _err "new-authz error: $response"
  596. _clearup
  597. return 1
  598. fi
  599. entry="$(printf $response | egrep -o '\{[^{]*"type":"'$vtype'"[^}]*')"
  600. _debug entry "$entry"
  601. token="$(printf "$entry" | egrep -o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
  602. _debug token $token
  603. uri="$(printf "$entry" | egrep -o '"uri":"[^"]*'| cut -d : -f 2,3 | tr -d '"' )"
  604. _debug uri $uri
  605. keyauthorization="$token.$thumbprint"
  606. _debug keyauthorization "$keyauthorization"
  607. dvlist="$d$sep$keyauthorization$sep$uri"
  608. _debug dvlist "$dvlist"
  609. vlist="$vlist$dvlist,"
  610. done
  611. #add entry
  612. dnsadded=""
  613. ventries=$(echo "$vlist" | tr ',' ' ' )
  614. for ventry in $ventries
  615. do
  616. d=$(echo $ventry | cut -d $sep -f 1)
  617. keyauthorization=$(echo $ventry | cut -d $sep -f 2)
  618. if [ "$vtype" == "$VTYPE_DNS" ] ; then
  619. dnsadded='0'
  620. txtdomain="_acme-challenge.$d"
  621. _debug txtdomain "$txtdomain"
  622. txt="$(echo -e -n $keyauthorization | openssl dgst -sha256 -binary | _base64 | _b64)"
  623. _debug txt "$txt"
  624. #dns
  625. #1. check use api
  626. d_api=""
  627. if [ -f "$LE_WORKING_DIR/$d/$Le_Webroot" ] ; then
  628. d_api="$LE_WORKING_DIR/$d/$Le_Webroot"
  629. elif [ -f "$LE_WORKING_DIR/$d/$Le_Webroot.sh" ] ; then
  630. d_api="$LE_WORKING_DIR/$d/$Le_Webroot.sh"
  631. elif [ -f "$LE_WORKING_DIR/$Le_Webroot" ] ; then
  632. d_api="$LE_WORKING_DIR/$Le_Webroot"
  633. elif [ -f "$LE_WORKING_DIR/$Le_Webroot.sh" ] ; then
  634. d_api="$LE_WORKING_DIR/$Le_Webroot.sh"
  635. elif [ -f "$LE_WORKING_DIR/dnsapi/$Le_Webroot" ] ; then
  636. d_api="$LE_WORKING_DIR/dnsapi/$Le_Webroot"
  637. elif [ -f "$LE_WORKING_DIR/dnsapi/$Le_Webroot.sh" ] ; then
  638. d_api="$LE_WORKING_DIR/dnsapi/$Le_Webroot.sh"
  639. fi
  640. _debug d_api "$d_api"
  641. if [ "$d_api" ]; then
  642. _info "Found domain api file: $d_api"
  643. else
  644. _err "Add the following TXT record:"
  645. _err "Domain: $txtdomain"
  646. _err "TXT value: $txt"
  647. _err "Please be aware that you prepend _acme-challenge. before your domain"
  648. _err "so the resulting subdomain will be: $txtdomain"
  649. continue
  650. fi
  651. if ! source $d_api ; then
  652. _err "Load file $d_api error. Please check your api file and try again."
  653. return 1
  654. fi
  655. addcommand="$Le_Webroot-add"
  656. if ! command -v $addcommand ; then
  657. _err "It seems that your api file is not correct, it must have a function named: $addcommand"
  658. return 1
  659. fi
  660. if ! $addcommand $txtdomain $txt ; then
  661. _err "Error add txt for domain:$txtdomain"
  662. return 1
  663. fi
  664. dnsadded='1'
  665. fi
  666. done
  667. if [ "$dnsadded" == '0' ] ; then
  668. _setopt "$DOMAIN_CONF" "Le_Vlist" "=" "\"$vlist\""
  669. _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit."
  670. _err "Please add the TXT records to the domains, and retry again."
  671. return 1
  672. fi
  673. fi
  674. if [ "$dnsadded" == '1' ] ; then
  675. _info "Sleep 60 seconds for the txt records to take effect"
  676. sleep 60
  677. fi
  678. _debug "ok, let's start to verify"
  679. ventries=$(echo "$vlist" | tr ',' ' ' )
  680. for ventry in $ventries
  681. do
  682. d=$(echo $ventry | cut -d $sep -f 1)
  683. keyauthorization=$(echo $ventry | cut -d $sep -f 2)
  684. uri=$(echo $ventry | cut -d $sep -f 3)
  685. _info "Verifying:$d"
  686. _debug "d" "$d"
  687. _debug "keyauthorization" "$keyauthorization"
  688. _debug "uri" "$uri"
  689. removelevel=""
  690. token=""
  691. if [ "$vtype" == "$VTYPE_HTTP" ] ; then
  692. if [ "$Le_Webroot" == "no" ] ; then
  693. _info "Standalone mode server"
  694. _startserver "$keyauthorization" &
  695. serverproc="$!"
  696. sleep 2
  697. _debug serverproc $serverproc
  698. else
  699. if [ -z "$wellknown_path" ] ; then
  700. wellknown_path="$Le_Webroot/.well-known/acme-challenge"
  701. fi
  702. _debug wellknown_path "$wellknown_path"
  703. if [ ! -d "$Le_Webroot/.well-known" ] ; then
  704. removelevel='1'
  705. elif [ ! -d "$Le_Webroot/.well-known/acme-challenge" ] ; then
  706. removelevel='2'
  707. else
  708. removelevel='3'
  709. fi
  710. token="$(echo -e -n "$keyauthorization" | cut -d '.' -f 1)"
  711. _debug "writing token:$token to $wellknown_path/$token"
  712. mkdir -p "$wellknown_path"
  713. echo -n "$keyauthorization" > "$wellknown_path/$token"
  714. webroot_owner=$(_stat $Le_Webroot)
  715. _debug "Changing owner/group of .well-known to $webroot_owner"
  716. chown -R $webroot_owner "$Le_Webroot/.well-known"
  717. fi
  718. fi
  719. _send_signed_request $uri "{\"resource\": \"challenge\", \"keyAuthorization\": \"$keyauthorization\"}"
  720. if [ ! -z "$code" ] && [ ! "$code" == '202' ] ; then
  721. _err "$d:Challenge error: $resource"
  722. _clearupwebbroot "$Le_Webroot" "$removelevel" "$token"
  723. _clearup
  724. return 1
  725. fi
  726. while [ "1" ] ; do
  727. _debug "sleep 5 secs to verify"
  728. sleep 5
  729. _debug "checking"
  730. if ! _get $uri ; then
  731. _err "$d:Verify error:$resource"
  732. _clearupwebbroot "$Le_Webroot" "$removelevel" "$token"
  733. _clearup
  734. return 1
  735. fi
  736. status=$(echo $response | egrep -o '"status":"[^"]+"' | cut -d : -f 2 | tr -d '"')
  737. if [ "$status" == "valid" ] ; then
  738. _info "Success"
  739. _stopserver $serverproc
  740. serverproc=""
  741. _clearupwebbroot "$Le_Webroot" "$removelevel" "$token"
  742. break;
  743. fi
  744. if [ "$status" == "invalid" ] ; then
  745. error=$(echo $response | egrep -o '"error":{[^}]*}' | grep -o '"detail":"[^"]*"' | cut -d '"' -f 4)
  746. _err "$d:Verify error:$error"
  747. _clearupwebbroot "$Le_Webroot" "$removelevel" "$token"
  748. _clearup
  749. return 1;
  750. fi
  751. if [ "$status" == "pending" ] ; then
  752. _info "Pending"
  753. else
  754. _err "$d:Verify error:$response"
  755. _clearupwebbroot "$Le_Webroot" "$removelevel" "$token"
  756. _clearup
  757. return 1
  758. fi
  759. done
  760. done
  761. _clearup
  762. _info "Verify finished, start to sign."
  763. der="$(openssl req -in $CSR_PATH -outform DER | _base64 | _b64)"
  764. _send_signed_request "$API/acme/new-cert" "{\"resource\": \"new-cert\", \"csr\": \"$der\"}" "needbase64"
  765. Le_LinkCert="$(grep -i -o '^Location.*$' $CURL_HEADER | tr -d "\r\n" | cut -d " " -f 2)"
  766. _setopt "$DOMAIN_CONF" "Le_LinkCert" "=" "$Le_LinkCert"
  767. if [ "$Le_LinkCert" ] ; then
  768. echo -----BEGIN CERTIFICATE----- > "$CERT_PATH"
  769. curl --silent "$Le_LinkCert" | openssl base64 -e >> "$CERT_PATH"
  770. echo -----END CERTIFICATE----- >> "$CERT_PATH"
  771. _info "Cert success."
  772. cat "$CERT_PATH"
  773. _info "Your cert is in $CERT_PATH"
  774. cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH"
  775. fi
  776. if [ -z "$Le_LinkCert" ] ; then
  777. response="$(echo $response | openssl base64 -d -A)"
  778. _err "Sign failed: $(echo "$response" | grep -o '"detail":"[^"]*"')"
  779. return 1
  780. fi
  781. _setopt "$DOMAIN_CONF" 'Le_Vlist' '=' "\"\""
  782. Le_LinkIssuer=$(grep -i '^Link' $CURL_HEADER | cut -d " " -f 2| cut -d ';' -f 1 | tr -d '<>' )
  783. _setopt "$DOMAIN_CONF" "Le_LinkIssuer" "=" "$Le_LinkIssuer"
  784. if [ "$Le_LinkIssuer" ] ; then
  785. echo -----BEGIN CERTIFICATE----- > "$CA_CERT_PATH"
  786. curl --silent "$Le_LinkIssuer" | openssl base64 -e >> "$CA_CERT_PATH"
  787. echo -----END CERTIFICATE----- >> "$CA_CERT_PATH"
  788. _info "The intermediate CA cert is in $CA_CERT_PATH"
  789. cat "$CA_CERT_PATH" >> "$CERT_FULLCHAIN_PATH"
  790. _info "And the full chain certs is there: $CERT_FULLCHAIN_PATH"
  791. fi
  792. Le_CertCreateTime=$(date -u "+%s")
  793. _setopt "$DOMAIN_CONF" "Le_CertCreateTime" "=" "$Le_CertCreateTime"
  794. Le_CertCreateTimeStr=$(date -u )
  795. _setopt "$DOMAIN_CONF" "Le_CertCreateTimeStr" "=" "\"$Le_CertCreateTimeStr\""
  796. if [ ! "$Le_RenewalDays" ] ; then
  797. Le_RenewalDays=80
  798. fi
  799. _setopt "$DOMAIN_CONF" "Le_RenewalDays" "=" "$Le_RenewalDays"
  800. let "Le_NextRenewTime=Le_CertCreateTime+Le_RenewalDays*24*60*60"
  801. _setopt "$DOMAIN_CONF" "Le_NextRenewTime" "=" "$Le_NextRenewTime"
  802. Le_NextRenewTimeStr=$( _time2str $Le_NextRenewTime )
  803. _setopt "$DOMAIN_CONF" "Le_NextRenewTimeStr" "=" "\"$Le_NextRenewTimeStr\""
  804. installcert $Le_Domain "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd"
  805. }
  806. renew() {
  807. Le_Domain="$1"
  808. if [ -z "$Le_Domain" ] ; then
  809. _err "Usage: $0 domain.com"
  810. return 1
  811. fi
  812. _initpath $Le_Domain
  813. if [ ! -f "$DOMAIN_CONF" ] ; then
  814. _info "$Le_Domain is not a issued domain, skip."
  815. return 0;
  816. fi
  817. source "$DOMAIN_CONF"
  818. if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(date -u "+%s" )" -lt "$Le_NextRenewTime" ] ; then
  819. _info "Skip, Next renewal time is: $Le_NextRenewTimeStr"
  820. return 2
  821. fi
  822. IS_RENEW="1"
  823. issue "$Le_Webroot" "$Le_Domain" "$Le_Alt" "$Le_Keylength" "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd"
  824. local res=$?
  825. IS_RENEW=""
  826. return $res
  827. }
  828. renewAll() {
  829. _initpath
  830. _info "renewAll"
  831. for d in $(ls -F $LE_WORKING_DIR | grep [^.].*[.].*/$ ) ; do
  832. d=$(echo $d | cut -d '/' -f 1)
  833. _info "renew $d"
  834. Le_LinkCert=""
  835. Le_Domain=""
  836. Le_Alt=""
  837. Le_Webroot=""
  838. Le_Keylength=""
  839. Le_LinkIssuer=""
  840. Le_CertCreateTime=""
  841. Le_CertCreateTimeStr=""
  842. Le_RenewalDays=""
  843. Le_NextRenewTime=""
  844. Le_NextRenewTimeStr=""
  845. Le_RealCertPath=""
  846. Le_RealKeyPath=""
  847. Le_RealCACertPath=""
  848. Le_ReloadCmd=""
  849. DOMAIN_PATH=""
  850. DOMAIN_CONF=""
  851. DOMAIN_SSL_CONF=""
  852. CSR_PATH=""
  853. CERT_KEY_PATH=""
  854. CERT_PATH=""
  855. CA_CERT_PATH=""
  856. CERT_FULLCHAIN_PATH=""
  857. ACCOUNT_KEY_PATH=""
  858. wellknown_path=""
  859. renew "$d"
  860. done
  861. }
  862. installcert() {
  863. Le_Domain="$1"
  864. if [ -z "$Le_Domain" ] ; then
  865. _err "Usage: $0 domain.com [cert-file-path]|no [key-file-path]|no [ca-cert-file-path]|no [reloadCmd]|no"
  866. return 1
  867. fi
  868. Le_RealCertPath="$2"
  869. Le_RealKeyPath="$3"
  870. Le_RealCACertPath="$4"
  871. Le_ReloadCmd="$5"
  872. _initpath $Le_Domain
  873. _setopt "$DOMAIN_CONF" "Le_RealCertPath" "=" "\"$Le_RealCertPath\""
  874. _setopt "$DOMAIN_CONF" "Le_RealCACertPath" "=" "\"$Le_RealCACertPath\""
  875. _setopt "$DOMAIN_CONF" "Le_RealKeyPath" "=" "\"$Le_RealKeyPath\""
  876. _setopt "$DOMAIN_CONF" "Le_ReloadCmd" "=" "\"$Le_ReloadCmd\""
  877. if [ "$Le_RealCertPath" ] ; then
  878. if [ -f "$Le_RealCertPath" ] ; then
  879. cp -p "$Le_RealCertPath" "$Le_RealCertPath".bak
  880. fi
  881. cat "$CERT_PATH" > "$Le_RealCertPath"
  882. fi
  883. if [ "$Le_RealCACertPath" ] ; then
  884. if [ -f "$Le_RealCACertPath" ] ; then
  885. cp -p "$Le_RealCACertPath" "$Le_RealCACertPath".bak
  886. fi
  887. if [ "$Le_RealCACertPath" == "$Le_RealCertPath" ] ; then
  888. echo "" >> "$Le_RealCACertPath"
  889. cat "$CA_CERT_PATH" >> "$Le_RealCACertPath"
  890. else
  891. cat "$CA_CERT_PATH" > "$Le_RealCACertPath"
  892. fi
  893. fi
  894. if [ "$Le_RealKeyPath" ] ; then
  895. if [ -f "$Le_RealKeyPath" ] ; then
  896. cp -p "$Le_RealKeyPath" "$Le_RealKeyPath".bak
  897. fi
  898. cat "$CERT_KEY_PATH" > "$Le_RealKeyPath"
  899. fi
  900. if [ "$Le_ReloadCmd" ] ; then
  901. _info "Run Le_ReloadCmd: $Le_ReloadCmd"
  902. (cd "$DOMAIN_PATH" && eval "$Le_ReloadCmd")
  903. fi
  904. }
  905. installcronjob() {
  906. _initpath
  907. _info "Installing cron job"
  908. if ! crontab -l | grep 'le.sh cron' ; then
  909. if [ -f "$LE_WORKING_DIR/le.sh" ] ; then
  910. lesh="\"$LE_WORKING_DIR\"/le.sh"
  911. else
  912. _err "Can not install cronjob, le.sh not found."
  913. return 1
  914. fi
  915. crontab -l | { cat; echo "0 0 * * * LE_WORKING_DIR=\"$LE_WORKING_DIR\" $lesh cron > /dev/null"; } | crontab -
  916. fi
  917. if [ "$?" != "0" ] ; then
  918. _err "Install cron job failed. You need to manually renew your certs."
  919. _err "Or you can add cronjob by yourself:"
  920. _err "LE_WORKING_DIR=\"$LE_WORKING_DIR\" $lesh cron > /dev/null"
  921. return 1
  922. fi
  923. }
  924. uninstallcronjob() {
  925. _info "Removing cron job"
  926. cr="$(crontab -l | grep 'le.sh cron')"
  927. if [ "$cr" ] ; then
  928. crontab -l | sed "/le.sh cron/d" | crontab -
  929. LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 6 | cut -d '=' -f 2 | tr -d '"')"
  930. _info LE_WORKING_DIR "$LE_WORKING_DIR"
  931. fi
  932. _initpath
  933. }
  934. # Detect profile file if not specified as environment variable
  935. _detect_profile() {
  936. if [ -n "$PROFILE" -a -f "$PROFILE" ]; then
  937. echo "$PROFILE"
  938. return
  939. fi
  940. local DETECTED_PROFILE
  941. DETECTED_PROFILE=''
  942. local SHELLTYPE
  943. SHELLTYPE="$(basename "/$SHELL")"
  944. if [ "$SHELLTYPE" = "bash" ]; then
  945. if [ -f "$HOME/.bashrc" ]; then
  946. DETECTED_PROFILE="$HOME/.bashrc"
  947. elif [ -f "$HOME/.bash_profile" ]; then
  948. DETECTED_PROFILE="$HOME/.bash_profile"
  949. fi
  950. elif [ "$SHELLTYPE" = "zsh" ]; then
  951. DETECTED_PROFILE="$HOME/.zshrc"
  952. fi
  953. if [ -z "$DETECTED_PROFILE" ]; then
  954. if [ -f "$HOME/.profile" ]; then
  955. DETECTED_PROFILE="$HOME/.profile"
  956. elif [ -f "$HOME/.bashrc" ]; then
  957. DETECTED_PROFILE="$HOME/.bashrc"
  958. elif [ -f "$HOME/.bash_profile" ]; then
  959. DETECTED_PROFILE="$HOME/.bash_profile"
  960. elif [ -f "$HOME/.zshrc" ]; then
  961. DETECTED_PROFILE="$HOME/.zshrc"
  962. fi
  963. fi
  964. if [ ! -z "$DETECTED_PROFILE" ]; then
  965. echo "$DETECTED_PROFILE"
  966. fi
  967. }
  968. _initconf() {
  969. _initpath
  970. if [ ! -f "$ACCOUNT_CONF_PATH" ] ; then
  971. echo "#Account configurations:
  972. #Here are the supported macros, uncomment them to make them take effect.
  973. #ACCOUNT_EMAIL=aaa@aaa.com # the account email used to register account.
  974. #ACCOUNT_KEY_PATH=\"/path/to/account.key\"
  975. #STAGE=1 # Use the staging api
  976. #FORCE=1 # Force to issue cert
  977. #DEBUG=1 # Debug mode
  978. #dns api
  979. #######################
  980. #Cloudflare:
  981. #api key
  982. #CF_Key=\"sdfsdfsdfljlbjkljlkjsdfoiwje\"
  983. #account email
  984. #CF_Email=\"xxxx@sss.com\"
  985. #######################
  986. #Dnspod.cn:
  987. #api key id
  988. #DP_Id=\"1234\"
  989. #api key
  990. #DP_Key=\"sADDsdasdgdsf\"
  991. #######################
  992. #Cloudxns.com:
  993. #CX_Key=\"1234\"
  994. #
  995. #CX_Secret=\"sADDsdasdgdsf\"
  996. " > $ACCOUNT_CONF_PATH
  997. fi
  998. }
  999. install() {
  1000. if ! _initpath ; then
  1001. _err "Install failed."
  1002. return 1
  1003. fi
  1004. #check if there is sudo installed, AND if the current user is a sudoer.
  1005. if command -v sudo > /dev/null ; then
  1006. if [ "$(sudo -n uptime 2>&1|grep "load"|wc -l)" != "0" ] ; then
  1007. SUDO=sudo
  1008. fi
  1009. fi
  1010. if command -v yum > /dev/null ; then
  1011. YUM="1"
  1012. INSTALL="$SUDO yum install -y "
  1013. elif command -v apt-get > /dev/null ; then
  1014. INSTALL="$SUDO apt-get install -y "
  1015. fi
  1016. if ! command -v "curl" > /dev/null ; then
  1017. _err "Please install curl first."
  1018. _err "$INSTALL curl"
  1019. return 1
  1020. fi
  1021. if ! command -v "crontab" > /dev/null ; then
  1022. _err "Please install crontab first."
  1023. if [ "$YUM" ] ; then
  1024. _err "$INSTALL crontabs"
  1025. else
  1026. _err "$INSTALL crontab"
  1027. fi
  1028. return 1
  1029. fi
  1030. if ! command -v "openssl" > /dev/null ; then
  1031. _err "Please install openssl first."
  1032. _err "$INSTALL openssl"
  1033. return 1
  1034. fi
  1035. _info "Installing to $LE_WORKING_DIR"
  1036. cp le.sh "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/le.sh"
  1037. if [ "$?" != "0" ] ; then
  1038. _err "Install failed, can not copy le.sh"
  1039. return 1
  1040. fi
  1041. _info "Installed to $LE_WORKING_DIR/le.sh"
  1042. _profile="$(_detect_profile)"
  1043. if [ "$_profile" ] ; then
  1044. _debug "Found profile: $_profile"
  1045. echo "LE_WORKING_DIR=$LE_WORKING_DIR
  1046. alias le=\"$LE_WORKING_DIR/le.sh\"
  1047. alias le.sh=\"$LE_WORKING_DIR/le.sh\"
  1048. " > "$LE_WORKING_DIR/le.env"
  1049. echo "" >> "$_profile"
  1050. _setopt "$_profile" "source \"$LE_WORKING_DIR/le.env\""
  1051. _info "OK, Close and reopen your terminal to start using le"
  1052. else
  1053. _info "No profile is found, you will need to go into $LE_WORKING_DIR to use le.sh"
  1054. fi
  1055. mkdir -p $LE_WORKING_DIR/dnsapi
  1056. cp dnsapi/* $LE_WORKING_DIR/dnsapi/
  1057. #to keep compatible mv the .acc file to .key file
  1058. if [ -f "$LE_WORKING_DIR/account.acc" ] ; then
  1059. mv "$LE_WORKING_DIR/account.acc" "$LE_WORKING_DIR/account.key"
  1060. fi
  1061. installcronjob
  1062. if [ ! -f "$ACCOUNT_CONF_PATH" ] ; then
  1063. _initconf
  1064. fi
  1065. _info OK
  1066. }
  1067. uninstall() {
  1068. uninstallcronjob
  1069. _initpath
  1070. _profile="$(_detect_profile)"
  1071. if [ "$_profile" ] ; then
  1072. text="$(cat $_profile)"
  1073. echo "$text" | sed "s|^source.*le.env.*$||" > "$_profile"
  1074. fi
  1075. rm -f $LE_WORKING_DIR/le.sh
  1076. _info "The keys and certs are in $LE_WORKING_DIR, you can remove them by yourself."
  1077. }
  1078. cron() {
  1079. renewAll
  1080. }
  1081. version() {
  1082. _info "$PROJECT"
  1083. _info "v$VER"
  1084. }
  1085. showhelp() {
  1086. version
  1087. echo "Usage: le.sh [command] ...[args]....
  1088. Avalible commands:
  1089. install:
  1090. Install le.sh to your system.
  1091. issue:
  1092. Issue a cert.
  1093. installcert:
  1094. Install the issued cert to apache/nginx or any other server.
  1095. renew:
  1096. Renew a cert.
  1097. renewAll:
  1098. Renew all the certs.
  1099. uninstall:
  1100. Uninstall le.sh, and uninstall the cron job.
  1101. version:
  1102. Show version info.
  1103. installcronjob:
  1104. Install the cron job to renew certs, you don't need to call this. The 'install' command can automatically install the cron job.
  1105. uninstallcronjob:
  1106. Uninstall the cron job. The 'uninstall' command can do this automatically.
  1107. createAccountKey:
  1108. Create an account private key, professional use.
  1109. createDomainKey:
  1110. Create an domain private key, professional use.
  1111. createCSR:
  1112. Create CSR , professional use.
  1113. "
  1114. }
  1115. if [ -z "$1" ] ; then
  1116. showhelp
  1117. else
  1118. "$@"
  1119. fi