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.

1350 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
  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 "$_NC"
  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. # done
  305. }
  306. _stopserver() {
  307. pid="$1"
  308. }
  309. _initpath() {
  310. if [ -z "$LE_WORKING_DIR" ]; then
  311. LE_WORKING_DIR=$HOME/.le
  312. fi
  313. if [ -z "$ACCOUNT_CONF_PATH" ] ; then
  314. ACCOUNT_CONF_PATH="$LE_WORKING_DIR/account.conf"
  315. fi
  316. if [ -f "$ACCOUNT_CONF_PATH" ] ; then
  317. source "$ACCOUNT_CONF_PATH"
  318. fi
  319. if [ -z "$API" ] ; then
  320. if [ -z "$STAGE" ] ; then
  321. API="$DEFAULT_CA"
  322. else
  323. API="$STAGE_CA"
  324. _info "Using stage api:$API"
  325. fi
  326. fi
  327. if [ -z "$ACME_DIR" ] ; then
  328. ACME_DIR="/home/.acme"
  329. fi
  330. if [ -z "$APACHE_CONF_BACKUP_DIR" ] ; then
  331. APACHE_CONF_BACKUP_DIR="$LE_WORKING_DIR/"
  332. fi
  333. domain="$1"
  334. if ! mkdir -p "$LE_WORKING_DIR" ; then
  335. _err "Can not craete working dir: $LE_WORKING_DIR"
  336. return 1
  337. fi
  338. if [ -z "$ACCOUNT_KEY_PATH" ] ; then
  339. ACCOUNT_KEY_PATH="$LE_WORKING_DIR/account.key"
  340. fi
  341. if [ -z "$domain" ] ; then
  342. return 0
  343. fi
  344. domainhome="$LE_WORKING_DIR/$domain"
  345. mkdir -p "$domainhome"
  346. if [ -z "$DOMAIN_PATH" ] ; then
  347. DOMAIN_PATH="$domainhome"
  348. fi
  349. if [ -z "$DOMAIN_CONF" ] ; then
  350. DOMAIN_CONF="$domainhome/$domain.conf"
  351. fi
  352. if [ -z "$DOMAIN_SSL_CONF" ] ; then
  353. DOMAIN_SSL_CONF="$domainhome/$domain.ssl.conf"
  354. fi
  355. if [ -z "$CSR_PATH" ] ; then
  356. CSR_PATH="$domainhome/$domain.csr"
  357. fi
  358. if [ -z "$CERT_KEY_PATH" ] ; then
  359. CERT_KEY_PATH="$domainhome/$domain.key"
  360. fi
  361. if [ -z "$CERT_PATH" ] ; then
  362. CERT_PATH="$domainhome/$domain.cer"
  363. fi
  364. if [ -z "$CA_CERT_PATH" ] ; then
  365. CA_CERT_PATH="$domainhome/ca.cer"
  366. fi
  367. if [ -z "$CERT_FULLCHAIN_PATH" ] ; then
  368. CERT_FULLCHAIN_PATH="$domainhome/fullchain.pem"
  369. fi
  370. }
  371. _apachePath() {
  372. httpdroot="$(apachectl -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"' )"
  373. httpdconfname="$(apachectl -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"' )"
  374. httpdconf="$httpdroot/$httpdconfname"
  375. if [ ! -f $httpdconf ] ; then
  376. _err "Apache Config file not found" $httpdconf
  377. return 1
  378. fi
  379. return 0
  380. }
  381. _restoreApache() {
  382. if [ -z "$usingApache" ] ; then
  383. return 0
  384. fi
  385. _initpath
  386. if ! _apachePath ; then
  387. return 1
  388. fi
  389. if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ] ; then
  390. _debug "No config file to restore."
  391. return 0
  392. fi
  393. cp -p "$APACHE_CONF_BACKUP_DIR/$httpdconfname" "$httpdconf"
  394. if ! apachectl -t ; then
  395. _err "Sorry, restore apache config error, please contact me."
  396. return 1;
  397. fi
  398. rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname"
  399. return 0
  400. }
  401. _setApache() {
  402. _initpath
  403. if ! _apachePath ; then
  404. return 1
  405. fi
  406. #backup the conf
  407. _debug "Backup apache config file" $httpdconf
  408. cp -p $httpdconf $APACHE_CONF_BACKUP_DIR/
  409. _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname"
  410. _info "In case there is an error that can not be restored automatically, you may try restore it yourself."
  411. _info "The backup file will be deleted on sucess, just forget it."
  412. #add alias
  413. echo "
  414. Alias /.well-known/acme-challenge $ACME_DIR
  415. <Directory $ACME_DIR >
  416. Require all granted
  417. </Directory>
  418. " >> $httpdconf
  419. if ! apachectl -t ; then
  420. _err "Sorry, apache config error, please contact me."
  421. _restoreApache
  422. return 1;
  423. fi
  424. if [ ! -d "$ACME_DIR" ] ; then
  425. mkdir -p "$ACME_DIR"
  426. chmod 755 "$ACME_DIR"
  427. fi
  428. if ! apachectl graceful ; then
  429. _err "Sorry, apachectl graceful error, please contact me."
  430. _restoreApache
  431. return 1;
  432. fi
  433. usingApache="1"
  434. return 0
  435. }
  436. _clearup () {
  437. _stopserver $serverproc
  438. serverproc=""
  439. _restoreApache
  440. }
  441. # webroot removelevel tokenfile
  442. _clearupwebbroot() {
  443. __webroot="$1"
  444. if [ -z "$__webroot" ] ; then
  445. _debug "no webroot specified, skip"
  446. return 0
  447. fi
  448. if [ "$2" == '1' ] ; then
  449. _debug "remove $__webroot/.well-known"
  450. rm -rf "$__webroot/.well-known"
  451. elif [ "$2" == '2' ] ; then
  452. _debug "remove $__webroot/.well-known/acme-challenge"
  453. rm -rf "$__webroot/.well-known/acme-challenge"
  454. elif [ "$2" == '3' ] ; then
  455. _debug "remove $__webroot/.well-known/acme-challenge/$3"
  456. rm -rf "$__webroot/.well-known/acme-challenge/$3"
  457. else
  458. _info "Skip for removelevel:$2"
  459. fi
  460. return 0
  461. }
  462. issue() {
  463. if [ -z "$2" ] ; then
  464. _err "Usage: le issue webroot|no|apache|dns a.com [www.a.com,b.com,c.com]|no [key-length]|no"
  465. return 1
  466. fi
  467. Le_Webroot="$1"
  468. Le_Domain="$2"
  469. Le_Alt="$3"
  470. Le_Keylength="$4"
  471. Le_RealCertPath="$5"
  472. Le_RealKeyPath="$6"
  473. Le_RealCACertPath="$7"
  474. Le_ReloadCmd="$8"
  475. _initpath $Le_Domain
  476. if [ -f "$DOMAIN_CONF" ] ; then
  477. Le_NextRenewTime=$(grep "^Le_NextRenewTime=" "$DOMAIN_CONF" | cut -d '=' -f 2)
  478. if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(date -u "+%s" )" -lt "$Le_NextRenewTime" ] ; then
  479. _info "Skip, Next renewal time is: $(grep "^Le_NextRenewTimeStr" "$DOMAIN_CONF" | cut -d '=' -f 2)"
  480. return 2
  481. fi
  482. fi
  483. if [ "$Le_Alt" == "no" ] ; then
  484. Le_Alt=""
  485. fi
  486. if [ "$Le_Keylength" == "no" ] ; then
  487. Le_Keylength=""
  488. fi
  489. if [ "$Le_RealCertPath" == "no" ] ; then
  490. Le_RealCertPath=""
  491. fi
  492. if [ "$Le_RealKeyPath" == "no" ] ; then
  493. Le_RealKeyPath=""
  494. fi
  495. if [ "$Le_RealCACertPath" == "no" ] ; then
  496. Le_RealCACertPath=""
  497. fi
  498. if [ "$Le_ReloadCmd" == "no" ] ; then
  499. Le_ReloadCmd=""
  500. fi
  501. _setopt "$DOMAIN_CONF" "Le_Domain" "=" "$Le_Domain"
  502. _setopt "$DOMAIN_CONF" "Le_Alt" "=" "$Le_Alt"
  503. _setopt "$DOMAIN_CONF" "Le_Webroot" "=" "$Le_Webroot"
  504. _setopt "$DOMAIN_CONF" "Le_Keylength" "=" "$Le_Keylength"
  505. _setopt "$DOMAIN_CONF" "Le_RealCertPath" "=" "\"$Le_RealCertPath\""
  506. _setopt "$DOMAIN_CONF" "Le_RealCACertPath" "=" "\"$Le_RealCACertPath\""
  507. _setopt "$DOMAIN_CONF" "Le_RealKeyPath" "=" "\"$Le_RealKeyPath\""
  508. _setopt "$DOMAIN_CONF" "Le_ReloadCmd" "=" "\"$Le_ReloadCmd\""
  509. if [ "$Le_Webroot" == "no" ] ; then
  510. _info "Standalone mode."
  511. if ! command -v "nc" > /dev/null ; then
  512. _err "Please install netcat(nc) tools first."
  513. return 1
  514. fi
  515. if [ -z "$Le_HTTPPort" ] ; then
  516. Le_HTTPPort=80
  517. fi
  518. _setopt "$DOMAIN_CONF" "Le_HTTPPort" "=" "$Le_HTTPPort"
  519. netprc="$(ss -ntpl | grep :$Le_HTTPPort" ")"
  520. if [ "$netprc" ] ; then
  521. _err "$netprc"
  522. _err "tcp port $Le_HTTPPort is already used by $(echo "$netprc" | cut -d : -f 4)"
  523. _err "Please stop it first"
  524. return 1
  525. fi
  526. fi
  527. if [ "$Le_Webroot" == "apache" ] ; then
  528. if ! _setApache ; then
  529. _err "set up apache error. Report error to me."
  530. return 1
  531. fi
  532. wellknown_path="$ACME_DIR"
  533. else
  534. usingApache=""
  535. fi
  536. createAccountKey $Le_Domain $Le_Keylength
  537. if ! createDomainKey $Le_Domain $Le_Keylength ; then
  538. _err "Create domain key error."
  539. return 1
  540. fi
  541. if ! createCSR $Le_Domain $Le_Alt ; then
  542. _err "Create CSR error."
  543. return 1
  544. fi
  545. 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)
  546. if [ "${#pub_exp}" == "5" ] ; then
  547. pub_exp=0$pub_exp
  548. fi
  549. _debug pub_exp "$pub_exp"
  550. e=$(echo $pub_exp | _h2b | _base64)
  551. _debug e "$e"
  552. modulus=$(openssl rsa -in $ACCOUNT_KEY_PATH -modulus -noout | cut -d '=' -f 2 )
  553. n=$(echo $modulus| _h2b | _base64 | _b64 )
  554. jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}'
  555. HEADER='{"alg": "RS256", "jwk": '$jwk'}'
  556. HEADERPLACE='{"nonce": "NONCE", "alg": "RS256", "jwk": '$jwk'}'
  557. _debug HEADER "$HEADER"
  558. accountkey_json=$(echo -n "$jwk" | tr -d ' ' )
  559. thumbprint=$(echo -n "$accountkey_json" | openssl dgst -sha256 -binary | _base64 | _b64)
  560. _info "Registering account"
  561. regjson='{"resource": "new-reg", "agreement": "'$AGREEMENT'"}'
  562. if [ "$ACCOUNT_EMAIL" ] ; then
  563. regjson='{"resource": "new-reg", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "agreement": "'$AGREEMENT'"}'
  564. fi
  565. _send_signed_request "$API/acme/new-reg" "$regjson"
  566. if [ "$code" == "" ] || [ "$code" == '201' ] ; then
  567. _info "Registered"
  568. echo $response > $LE_WORKING_DIR/account.json
  569. elif [ "$code" == '409' ] ; then
  570. _info "Already registered"
  571. else
  572. _err "Register account Error."
  573. _clearup
  574. return 1
  575. fi
  576. vtype="$VTYPE_HTTP"
  577. if [[ "$Le_Webroot" == "dns"* ]] ; then
  578. vtype="$VTYPE_DNS"
  579. fi
  580. vlist="$Le_Vlist"
  581. # verify each domain
  582. _info "Verify each domain"
  583. sep='#'
  584. if [ -z "$vlist" ] ; then
  585. alldomains=$(echo "$Le_Domain,$Le_Alt" | tr ',' ' ' )
  586. for d in $alldomains
  587. do
  588. _info "Getting token for domain" $d
  589. _send_signed_request "$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$d\"}}"
  590. if [ ! -z "$code" ] && [ ! "$code" == '201' ] ; then
  591. _err "new-authz error: $response"
  592. _clearup
  593. return 1
  594. fi
  595. entry="$(printf $response | egrep -o '\{[^{]*"type":"'$vtype'"[^}]*')"
  596. _debug entry "$entry"
  597. token="$(printf "$entry" | egrep -o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
  598. _debug token $token
  599. uri="$(printf "$entry" | egrep -o '"uri":"[^"]*'| cut -d : -f 2,3 | tr -d '"' )"
  600. _debug uri $uri
  601. keyauthorization="$token.$thumbprint"
  602. _debug keyauthorization "$keyauthorization"
  603. dvlist="$d$sep$keyauthorization$sep$uri"
  604. _debug dvlist "$dvlist"
  605. vlist="$vlist$dvlist,"
  606. done
  607. #add entry
  608. dnsadded=""
  609. ventries=$(echo "$vlist" | tr ',' ' ' )
  610. for ventry in $ventries
  611. do
  612. d=$(echo $ventry | cut -d $sep -f 1)
  613. keyauthorization=$(echo $ventry | cut -d $sep -f 2)
  614. if [ "$vtype" == "$VTYPE_DNS" ] ; then
  615. dnsadded='0'
  616. txtdomain="_acme-challenge.$d"
  617. _debug txtdomain "$txtdomain"
  618. txt="$(echo -e -n $keyauthorization | openssl dgst -sha256 -binary | _base64 | _b64)"
  619. _debug txt "$txt"
  620. #dns
  621. #1. check use api
  622. d_api=""
  623. if [ -f "$LE_WORKING_DIR/$d/$Le_Webroot" ] ; then
  624. d_api="$LE_WORKING_DIR/$d/$Le_Webroot"
  625. elif [ -f "$LE_WORKING_DIR/$d/$Le_Webroot.sh" ] ; then
  626. d_api="$LE_WORKING_DIR/$d/$Le_Webroot.sh"
  627. elif [ -f "$LE_WORKING_DIR/$Le_Webroot" ] ; then
  628. d_api="$LE_WORKING_DIR/$Le_Webroot"
  629. elif [ -f "$LE_WORKING_DIR/$Le_Webroot.sh" ] ; then
  630. d_api="$LE_WORKING_DIR/$Le_Webroot.sh"
  631. elif [ -f "$LE_WORKING_DIR/dnsapi/$Le_Webroot" ] ; then
  632. d_api="$LE_WORKING_DIR/dnsapi/$Le_Webroot"
  633. elif [ -f "$LE_WORKING_DIR/dnsapi/$Le_Webroot.sh" ] ; then
  634. d_api="$LE_WORKING_DIR/dnsapi/$Le_Webroot.sh"
  635. fi
  636. _debug d_api "$d_api"
  637. if [ "$d_api" ]; then
  638. _info "Found domain api file: $d_api"
  639. else
  640. _err "Add the following TXT record:"
  641. _err "Domain: $txtdomain"
  642. _err "TXT value: $txt"
  643. _err "Please be aware that you prepend _acme-challenge. before your domain"
  644. _err "so the resulting subdomain will be: $txtdomain"
  645. continue
  646. fi
  647. if ! source $d_api ; then
  648. _err "Load file $d_api error. Please check your api file and try again."
  649. return 1
  650. fi
  651. addcommand="$Le_Webroot-add"
  652. if ! command -v $addcommand ; then
  653. _err "It seems that your api file is not correct, it must have a function named: $addcommand"
  654. return 1
  655. fi
  656. if ! $addcommand $txtdomain $txt ; then
  657. _err "Error add txt for domain:$txtdomain"
  658. return 1
  659. fi
  660. dnsadded='1'
  661. fi
  662. done
  663. if [ "$dnsadded" == '0' ] ; then
  664. _setopt "$DOMAIN_CONF" "Le_Vlist" "=" "\"$vlist\""
  665. _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit."
  666. _err "Please add the TXT records to the domains, and retry again."
  667. return 1
  668. fi
  669. fi
  670. if [ "$dnsadded" == '1' ] ; then
  671. _info "Sleep 60 seconds for the txt records to take effect"
  672. sleep 60
  673. fi
  674. _debug "ok, let's start to verify"
  675. ventries=$(echo "$vlist" | tr ',' ' ' )
  676. for ventry in $ventries
  677. do
  678. d=$(echo $ventry | cut -d $sep -f 1)
  679. keyauthorization=$(echo $ventry | cut -d $sep -f 2)
  680. uri=$(echo $ventry | cut -d $sep -f 3)
  681. _info "Verifying:$d"
  682. _debug "d" "$d"
  683. _debug "keyauthorization" "$keyauthorization"
  684. _debug "uri" "$uri"
  685. removelevel=""
  686. token=""
  687. if [ "$vtype" == "$VTYPE_HTTP" ] ; then
  688. if [ "$Le_Webroot" == "no" ] ; then
  689. _info "Standalone mode server"
  690. _startserver "$keyauthorization" &
  691. serverproc="$!"
  692. sleep 2
  693. _debug serverproc $serverproc
  694. else
  695. if [ -z "$wellknown_path" ] ; then
  696. wellknown_path="$Le_Webroot/.well-known/acme-challenge"
  697. fi
  698. _debug wellknown_path "$wellknown_path"
  699. if [ ! -d "$Le_Webroot/.well-known" ] ; then
  700. removelevel='1'
  701. elif [ ! -d "$Le_Webroot/.well-known/acme-challenge" ] ; then
  702. removelevel='2'
  703. else
  704. removelevel='3'
  705. fi
  706. token="$(echo -e -n "$keyauthorization" | cut -d '.' -f 1)"
  707. _debug "writing token:$token to $wellknown_path/$token"
  708. mkdir -p "$wellknown_path"
  709. echo -n "$keyauthorization" > "$wellknown_path/$token"
  710. webroot_owner=$(_stat $Le_Webroot)
  711. _debug "Changing owner/group of .well-known to $webroot_owner"
  712. chown -R $webroot_owner "$Le_Webroot/.well-known"
  713. fi
  714. fi
  715. _send_signed_request $uri "{\"resource\": \"challenge\", \"keyAuthorization\": \"$keyauthorization\"}"
  716. if [ ! -z "$code" ] && [ ! "$code" == '202' ] ; then
  717. _err "$d:Challenge error: $resource"
  718. _clearupwebbroot "$Le_Webroot" "$removelevel" "$token"
  719. _clearup
  720. return 1
  721. fi
  722. while [ "1" ] ; do
  723. _debug "sleep 5 secs to verify"
  724. sleep 5
  725. _debug "checking"
  726. if ! _get $uri ; then
  727. _err "$d:Verify error:$resource"
  728. _clearupwebbroot "$Le_Webroot" "$removelevel" "$token"
  729. _clearup
  730. return 1
  731. fi
  732. status=$(echo $response | egrep -o '"status":"[^"]+"' | cut -d : -f 2 | tr -d '"')
  733. if [ "$status" == "valid" ] ; then
  734. _info "Success"
  735. _stopserver $serverproc
  736. serverproc=""
  737. _clearupwebbroot "$Le_Webroot" "$removelevel" "$token"
  738. break;
  739. fi
  740. if [ "$status" == "invalid" ] ; then
  741. error=$(echo $response | egrep -o '"error":{[^}]*}' | grep -o '"detail":"[^"]*"' | cut -d '"' -f 4)
  742. _err "$d:Verify error:$error"
  743. _clearupwebbroot "$Le_Webroot" "$removelevel" "$token"
  744. _clearup
  745. return 1;
  746. fi
  747. if [ "$status" == "pending" ] ; then
  748. _info "Pending"
  749. else
  750. _err "$d:Verify error:$response"
  751. _clearupwebbroot "$Le_Webroot" "$removelevel" "$token"
  752. _clearup
  753. return 1
  754. fi
  755. done
  756. done
  757. _clearup
  758. _info "Verify finished, start to sign."
  759. der="$(openssl req -in $CSR_PATH -outform DER | _base64 | _b64)"
  760. _send_signed_request "$API/acme/new-cert" "{\"resource\": \"new-cert\", \"csr\": \"$der\"}" "needbase64"
  761. Le_LinkCert="$(grep -i -o '^Location.*$' $CURL_HEADER | tr -d "\r\n" | cut -d " " -f 2)"
  762. _setopt "$DOMAIN_CONF" "Le_LinkCert" "=" "$Le_LinkCert"
  763. if [ "$Le_LinkCert" ] ; then
  764. echo -----BEGIN CERTIFICATE----- > "$CERT_PATH"
  765. curl --silent "$Le_LinkCert" | openssl base64 -e >> "$CERT_PATH"
  766. echo -----END CERTIFICATE----- >> "$CERT_PATH"
  767. _info "Cert success."
  768. cat "$CERT_PATH"
  769. _info "Your cert is in $CERT_PATH"
  770. cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH"
  771. fi
  772. if [ -z "$Le_LinkCert" ] ; then
  773. response="$(echo $response | openssl base64 -d -A)"
  774. _err "Sign failed: $(echo "$response" | grep -o '"detail":"[^"]*"')"
  775. return 1
  776. fi
  777. _setopt "$DOMAIN_CONF" 'Le_Vlist' '=' "\"\""
  778. Le_LinkIssuer=$(grep -i '^Link' $CURL_HEADER | cut -d " " -f 2| cut -d ';' -f 1 | tr -d '<>' )
  779. _setopt "$DOMAIN_CONF" "Le_LinkIssuer" "=" "$Le_LinkIssuer"
  780. if [ "$Le_LinkIssuer" ] ; then
  781. echo -----BEGIN CERTIFICATE----- > "$CA_CERT_PATH"
  782. curl --silent "$Le_LinkIssuer" | openssl base64 -e >> "$CA_CERT_PATH"
  783. echo -----END CERTIFICATE----- >> "$CA_CERT_PATH"
  784. _info "The intermediate CA cert is in $CA_CERT_PATH"
  785. cat "$CA_CERT_PATH" >> "$CERT_FULLCHAIN_PATH"
  786. _info "And the full chain certs is there: $CERT_FULLCHAIN_PATH"
  787. fi
  788. Le_CertCreateTime=$(date -u "+%s")
  789. _setopt "$DOMAIN_CONF" "Le_CertCreateTime" "=" "$Le_CertCreateTime"
  790. Le_CertCreateTimeStr=$(date -u )
  791. _setopt "$DOMAIN_CONF" "Le_CertCreateTimeStr" "=" "\"$Le_CertCreateTimeStr\""
  792. if [ ! "$Le_RenewalDays" ] ; then
  793. Le_RenewalDays=80
  794. fi
  795. _setopt "$DOMAIN_CONF" "Le_RenewalDays" "=" "$Le_RenewalDays"
  796. let "Le_NextRenewTime=Le_CertCreateTime+Le_RenewalDays*24*60*60"
  797. _setopt "$DOMAIN_CONF" "Le_NextRenewTime" "=" "$Le_NextRenewTime"
  798. Le_NextRenewTimeStr=$( _time2str $Le_NextRenewTime )
  799. _setopt "$DOMAIN_CONF" "Le_NextRenewTimeStr" "=" "\"$Le_NextRenewTimeStr\""
  800. installcert $Le_Domain "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd"
  801. }
  802. renew() {
  803. Le_Domain="$1"
  804. if [ -z "$Le_Domain" ] ; then
  805. _err "Usage: $0 domain.com"
  806. return 1
  807. fi
  808. _initpath $Le_Domain
  809. if [ ! -f "$DOMAIN_CONF" ] ; then
  810. _info "$Le_Domain is not a issued domain, skip."
  811. return 0;
  812. fi
  813. source "$DOMAIN_CONF"
  814. if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(date -u "+%s" )" -lt "$Le_NextRenewTime" ] ; then
  815. _info "Skip, Next renewal time is: $Le_NextRenewTimeStr"
  816. return 2
  817. fi
  818. IS_RENEW="1"
  819. issue "$Le_Webroot" "$Le_Domain" "$Le_Alt" "$Le_Keylength" "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd"
  820. local res=$?
  821. IS_RENEW=""
  822. return $res
  823. }
  824. renewAll() {
  825. _initpath
  826. _info "renewAll"
  827. for d in $(ls -F $LE_WORKING_DIR | grep [^.].*[.].*/$ ) ; do
  828. d=$(echo $d | cut -d '/' -f 1)
  829. _info "renew $d"
  830. Le_LinkCert=""
  831. Le_Domain=""
  832. Le_Alt=""
  833. Le_Webroot=""
  834. Le_Keylength=""
  835. Le_LinkIssuer=""
  836. Le_CertCreateTime=""
  837. Le_CertCreateTimeStr=""
  838. Le_RenewalDays=""
  839. Le_NextRenewTime=""
  840. Le_NextRenewTimeStr=""
  841. Le_RealCertPath=""
  842. Le_RealKeyPath=""
  843. Le_RealCACertPath=""
  844. Le_ReloadCmd=""
  845. DOMAIN_PATH=""
  846. DOMAIN_CONF=""
  847. DOMAIN_SSL_CONF=""
  848. CSR_PATH=""
  849. CERT_KEY_PATH=""
  850. CERT_PATH=""
  851. CA_CERT_PATH=""
  852. CERT_FULLCHAIN_PATH=""
  853. ACCOUNT_KEY_PATH=""
  854. wellknown_path=""
  855. renew "$d"
  856. done
  857. }
  858. installcert() {
  859. Le_Domain="$1"
  860. if [ -z "$Le_Domain" ] ; then
  861. _err "Usage: $0 domain.com [cert-file-path]|no [key-file-path]|no [ca-cert-file-path]|no [reloadCmd]|no"
  862. return 1
  863. fi
  864. Le_RealCertPath="$2"
  865. Le_RealKeyPath="$3"
  866. Le_RealCACertPath="$4"
  867. Le_ReloadCmd="$5"
  868. _initpath $Le_Domain
  869. _setopt "$DOMAIN_CONF" "Le_RealCertPath" "=" "\"$Le_RealCertPath\""
  870. _setopt "$DOMAIN_CONF" "Le_RealCACertPath" "=" "\"$Le_RealCACertPath\""
  871. _setopt "$DOMAIN_CONF" "Le_RealKeyPath" "=" "\"$Le_RealKeyPath\""
  872. _setopt "$DOMAIN_CONF" "Le_ReloadCmd" "=" "\"$Le_ReloadCmd\""
  873. if [ "$Le_RealCertPath" ] ; then
  874. if [ -f "$Le_RealCertPath" ] ; then
  875. cp -p "$Le_RealCertPath" "$Le_RealCertPath".bak
  876. fi
  877. cat "$CERT_PATH" > "$Le_RealCertPath"
  878. fi
  879. if [ "$Le_RealCACertPath" ] ; then
  880. if [ -f "$Le_RealCACertPath" ] ; then
  881. cp -p "$Le_RealCACertPath" "$Le_RealCACertPath".bak
  882. fi
  883. if [ "$Le_RealCACertPath" == "$Le_RealCertPath" ] ; then
  884. echo "" >> "$Le_RealCACertPath"
  885. cat "$CA_CERT_PATH" >> "$Le_RealCACertPath"
  886. else
  887. cat "$CA_CERT_PATH" > "$Le_RealCACertPath"
  888. fi
  889. fi
  890. if [ "$Le_RealKeyPath" ] ; then
  891. if [ -f "$Le_RealKeyPath" ] ; then
  892. cp -p "$Le_RealKeyPath" "$Le_RealKeyPath".bak
  893. fi
  894. cat "$CERT_KEY_PATH" > "$Le_RealKeyPath"
  895. fi
  896. if [ "$Le_ReloadCmd" ] ; then
  897. _info "Run Le_ReloadCmd: $Le_ReloadCmd"
  898. (cd "$DOMAIN_PATH" && eval "$Le_ReloadCmd")
  899. fi
  900. }
  901. installcronjob() {
  902. _initpath
  903. _info "Installing cron job"
  904. if ! crontab -l | grep 'le.sh cron' ; then
  905. if [ -f "$LE_WORKING_DIR/le.sh" ] ; then
  906. lesh="\"$LE_WORKING_DIR\"/le.sh"
  907. else
  908. _err "Can not install cronjob, le.sh not found."
  909. return 1
  910. fi
  911. crontab -l | { cat; echo "0 0 * * * LE_WORKING_DIR=\"$LE_WORKING_DIR\" $lesh cron > /dev/null"; } | crontab -
  912. fi
  913. if [ "$?" != "0" ] ; then
  914. _err "Install cron job failed. You need to manually renew your certs."
  915. _err "Or you can add cronjob by yourself:"
  916. _err "LE_WORKING_DIR=\"$LE_WORKING_DIR\" $lesh cron > /dev/null"
  917. return 1
  918. fi
  919. }
  920. uninstallcronjob() {
  921. _info "Removing cron job"
  922. cr="$(crontab -l | grep 'le.sh cron')"
  923. if [ "$cr" ] ; then
  924. crontab -l | sed "/le.sh cron/d" | crontab -
  925. LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 6 | cut -d '=' -f 2 | tr -d '"')"
  926. _info LE_WORKING_DIR "$LE_WORKING_DIR"
  927. fi
  928. _initpath
  929. }
  930. # Detect profile file if not specified as environment variable
  931. _detect_profile() {
  932. if [ -n "$PROFILE" -a -f "$PROFILE" ]; then
  933. echo "$PROFILE"
  934. return
  935. fi
  936. local DETECTED_PROFILE
  937. DETECTED_PROFILE=''
  938. local SHELLTYPE
  939. SHELLTYPE="$(basename "/$SHELL")"
  940. if [ "$SHELLTYPE" = "bash" ]; then
  941. if [ -f "$HOME/.bashrc" ]; then
  942. DETECTED_PROFILE="$HOME/.bashrc"
  943. elif [ -f "$HOME/.bash_profile" ]; then
  944. DETECTED_PROFILE="$HOME/.bash_profile"
  945. fi
  946. elif [ "$SHELLTYPE" = "zsh" ]; then
  947. DETECTED_PROFILE="$HOME/.zshrc"
  948. fi
  949. if [ -z "$DETECTED_PROFILE" ]; then
  950. if [ -f "$HOME/.profile" ]; then
  951. DETECTED_PROFILE="$HOME/.profile"
  952. elif [ -f "$HOME/.bashrc" ]; then
  953. DETECTED_PROFILE="$HOME/.bashrc"
  954. elif [ -f "$HOME/.bash_profile" ]; then
  955. DETECTED_PROFILE="$HOME/.bash_profile"
  956. elif [ -f "$HOME/.zshrc" ]; then
  957. DETECTED_PROFILE="$HOME/.zshrc"
  958. fi
  959. fi
  960. if [ ! -z "$DETECTED_PROFILE" ]; then
  961. echo "$DETECTED_PROFILE"
  962. fi
  963. }
  964. _initconf() {
  965. _initpath
  966. if [ ! -f "$ACCOUNT_CONF_PATH" ] ; then
  967. echo "#Account configurations:
  968. #Here are the supported macros, uncomment them to make them take effect.
  969. #ACCOUNT_EMAIL=aaa@aaa.com # the account email used to register account.
  970. #ACCOUNT_KEY_PATH=\"/path/to/account.key\"
  971. #STAGE=1 # Use the staging api
  972. #FORCE=1 # Force to issue cert
  973. #DEBUG=1 # Debug mode
  974. #dns api
  975. #######################
  976. #Cloudflare:
  977. #api key
  978. #CF_Key=\"sdfsdfsdfljlbjkljlkjsdfoiwje\"
  979. #account email
  980. #CF_Email=\"xxxx@sss.com\"
  981. #######################
  982. #Dnspod.cn:
  983. #api key id
  984. #DP_Id=\"1234\"
  985. #api key
  986. #DP_Key=\"sADDsdasdgdsf\"
  987. #######################
  988. #Cloudxns.com:
  989. #CX_Key=\"1234\"
  990. #
  991. #CX_Secret=\"sADDsdasdgdsf\"
  992. " > $ACCOUNT_CONF_PATH
  993. fi
  994. }
  995. install() {
  996. if ! _initpath ; then
  997. _err "Install failed."
  998. return 1
  999. fi
  1000. #check if there is sudo installed, AND if the current user is a sudoer.
  1001. if command -v sudo > /dev/null ; then
  1002. if [ "$(sudo -n uptime 2>&1|grep "load"|wc -l)" != "0" ] ; then
  1003. SUDO=sudo
  1004. fi
  1005. fi
  1006. if command -v yum > /dev/null ; then
  1007. YUM="1"
  1008. INSTALL="$SUDO yum install -y "
  1009. elif command -v apt-get > /dev/null ; then
  1010. INSTALL="$SUDO apt-get install -y "
  1011. fi
  1012. if ! command -v "curl" > /dev/null ; then
  1013. _err "Please install curl first."
  1014. _err "$INSTALL curl"
  1015. return 1
  1016. fi
  1017. if ! command -v "crontab" > /dev/null ; then
  1018. _err "Please install crontab first."
  1019. if [ "$YUM" ] ; then
  1020. _err "$INSTALL crontabs"
  1021. else
  1022. _err "$INSTALL crontab"
  1023. fi
  1024. return 1
  1025. fi
  1026. if ! command -v "openssl" > /dev/null ; then
  1027. _err "Please install openssl first."
  1028. _err "$INSTALL openssl"
  1029. return 1
  1030. fi
  1031. _info "Installing to $LE_WORKING_DIR"
  1032. cp le.sh "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/le.sh"
  1033. if [ "$?" != "0" ] ; then
  1034. _err "Install failed, can not copy le.sh"
  1035. return 1
  1036. fi
  1037. _info "Installed to $LE_WORKING_DIR/le.sh"
  1038. _profile="$(_detect_profile)"
  1039. if [ "$_profile" ] ; then
  1040. _debug "Found profile: $_profile"
  1041. echo "LE_WORKING_DIR=$LE_WORKING_DIR
  1042. alias le=\"$LE_WORKING_DIR/le.sh\"
  1043. alias le.sh=\"$LE_WORKING_DIR/le.sh\"
  1044. " > "$LE_WORKING_DIR/le.env"
  1045. echo "" >> "$_profile"
  1046. _setopt "$_profile" "source \"$LE_WORKING_DIR/le.env\""
  1047. _info "OK, Close and reopen your terminal to start using le"
  1048. else
  1049. _info "No profile is found, you will need to go into $LE_WORKING_DIR to use le.sh"
  1050. fi
  1051. mkdir -p $LE_WORKING_DIR/dnsapi
  1052. cp dnsapi/* $LE_WORKING_DIR/dnsapi/
  1053. #to keep compatible mv the .acc file to .key file
  1054. if [ -f "$LE_WORKING_DIR/account.acc" ] ; then
  1055. mv "$LE_WORKING_DIR/account.acc" "$LE_WORKING_DIR/account.key"
  1056. fi
  1057. installcronjob
  1058. if [ ! -f "$ACCOUNT_CONF_PATH" ] ; then
  1059. _initconf
  1060. fi
  1061. _info OK
  1062. }
  1063. uninstall() {
  1064. uninstallcronjob
  1065. _initpath
  1066. _profile="$(_detect_profile)"
  1067. if [ "$_profile" ] ; then
  1068. text="$(cat $_profile)"
  1069. echo "$text" | sed "s|^source.*le.env.*$||" > "$_profile"
  1070. fi
  1071. rm -f $LE_WORKING_DIR/le.sh
  1072. _info "The keys and certs are in $LE_WORKING_DIR, you can remove them by yourself."
  1073. }
  1074. cron() {
  1075. renewAll
  1076. }
  1077. version() {
  1078. _info "$PROJECT"
  1079. _info "v$VER"
  1080. }
  1081. showhelp() {
  1082. version
  1083. echo "Usage: le.sh [command] ...[args]....
  1084. Avalible commands:
  1085. install:
  1086. Install le.sh to your system.
  1087. issue:
  1088. Issue a cert.
  1089. installcert:
  1090. Install the issued cert to apache/nginx or any other server.
  1091. renew:
  1092. Renew a cert.
  1093. renewAll:
  1094. Renew all the certs.
  1095. uninstall:
  1096. Uninstall le.sh, and uninstall the cron job.
  1097. version:
  1098. Show version info.
  1099. installcronjob:
  1100. Install the cron job to renew certs, you don't need to call this. The 'install' command can automatically install the cron job.
  1101. uninstallcronjob:
  1102. Uninstall the cron job. The 'uninstall' command can do this automatically.
  1103. createAccountKey:
  1104. Create an account private key, professional use.
  1105. createDomainKey:
  1106. Create an domain private key, professional use.
  1107. createCSR:
  1108. Create CSR , professional use.
  1109. "
  1110. }
  1111. if [ -z "$1" ] ; then
  1112. showhelp
  1113. else
  1114. "$@"
  1115. fi