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.

1368 lines
34 KiB

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