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.

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