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.

1301 lines
32 KiB

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