From 00a506053c7672ea656640adb852cea79d78fae3 Mon Sep 17 00:00:00 2001 From: neil Date: Sun, 17 Apr 2016 17:33:08 +0800 Subject: [PATCH 01/11] compatible to dash --- acme.sh | 103 +++++++++++++++++++++++++++++++++++++---------- dnsapi/dns_cf.sh | 2 +- dnsapi/dns_cx.sh | 2 +- dnsapi/dns_dp.sh | 2 +- 4 files changed, 85 insertions(+), 24 deletions(-) diff --git a/acme.sh b/acme.sh index af4e87f..2c16799 100755 --- a/acme.sh +++ b/acme.sh @@ -28,6 +28,13 @@ if [ -z "$AGREEMENT" ] ; then fi + +_URGLY_PRINTF="" +if [ "$(printf '\x41')" = '\x41' ] ; then + _URGLY_PRINTF=1 +fi + + _info() { if [ -z "$2" ] ; then echo "[$(date)] $1" @@ -84,18 +91,71 @@ _exists(){ return $ret } +#a + b +_math(){ + expr "$@" +} + +_h_char_2_dec() { + _ch=$1 + case "${_ch}" in + a|A) + echo -n 10 + ;; + b|B) + echo -n 11 + ;; + c|C) + echo -n 12 + ;; + d|D) + echo -n 13 + ;; + e|E) + echo -n 14 + ;; + f|F) + echo -n 15 + ;; + *) + echo -n $_ch + ;; + esac + +} + _h2b() { hex=$(cat) i=1 j=2 + if _exists let ; then + uselet="1" + fi + _debug uselet "$uselet" while [ '1' ] ; do - h=$(printf $hex | cut -c $i-$j) - if [ -z "$h" ] ; then - break; + if [ -z "$_URGLY_PRINTF" ] ; then + h=$(printf $hex | cut -c $i-$j) + if [ -z "$h" ] ; then + break; + fi + printf "\x$h" + else + ic=$(printf $hex | cut -c $i) + jc=$(printf $hex | cut -c $j) + if [ -z "$ic$jc" ] ; then + break; + fi + ic="$(_h_char_2_dec $ic)" + jc="$(_h_char_2_dec $jc)" + printf '\'"$(printf %o "$(_math $ic \* 16 + $jc)")" fi - printf "\x$h" - let "i+=2" - let "j+=2" + if [ "$uselet" ] ; then + let "i+=2" + let "j+=2" + else + i="$(_math $i + 2)" + j="$(_math $j + 2)" + fi done } @@ -133,7 +193,7 @@ _getfile() { _err "Can not find start line: $startline" return 1 fi - let "i+=1" + _debug i $i j="$(grep -n -- "$endline" $filename | cut -d : -f 1)" @@ -141,10 +201,10 @@ _getfile() { _err "Can not find end line: $endline" return 1 fi - let "j-=1" + _debug j $j - sed -n $i,${j}p "$filename" + sed -n $i,${j}p "$filename" | head -n -1 | tail -n +2 } @@ -427,7 +487,8 @@ _calcjwk() { _debug2 e "$e" modulus=$(openssl rsa -in $keyfile -modulus -noout | cut -d '=' -f 2 ) - n=$(echo $modulus| _h2b | _base64 | _urlencode ) + _debug2 modulus "$modulus" + n=$(echo -n $modulus| _h2b | _base64 | _urlencode ) jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}' _debug2 jwk "$jwk" @@ -440,28 +501,28 @@ _calcjwk() { _debug2 crv $crv pubi="$(openssl ec -in $keyfile -noout -text 2>/dev/null | grep -n pub: | cut -d : -f 1)" + pubi=$(_math $pubi + 1) _debug2 pubi $pubi - let "pubi=pubi+1" pubj="$(openssl ec -in $keyfile -noout -text 2>/dev/null | grep -n "ASN1 OID:" | cut -d : -f 1)" + pubj=$(_math $pubj + 1) _debug2 pubj $pubj - let "pubj=pubj-1" pubtext="$(openssl ec -in $keyfile -noout -text 2>/dev/null | sed -n "$pubi,${pubj}p" | tr -d " \n\r")" _debug2 pubtext "$pubtext" xlen="$(printf "$pubtext" | tr -d ':' | wc -c)" - let "xlen=xlen/4" + xlen=$(_math $xlen / 4) _debug2 xlen $xlen - - let "xend=xlen+1" + + xend=$(_math $xend + 1) x="$(printf $pubtext | cut -d : -f 2-$xend)" _debug2 x $x x64="$(printf $x | tr -d : | _h2b | _base64 | _urlencode)" _debug2 x64 $x64 - - let "xend+=1" + + xend=$(_math $xend + 1) y="$(printf $pubtext | cut -d : -f $xend-10000)" _debug2 y $y @@ -1117,7 +1178,7 @@ issue() { _currentRoot="$_w" fi _debug "_currentRoot" "$_currentRoot" - let "_index+=1" + _index=$(_math $_index + 1) vtype="$VTYPE_HTTP" if _startswith "$_currentRoot" "dns" ; then @@ -1312,7 +1373,7 @@ issue() { fi while [ "1" ] ; do - let "waittimes+=1" + waittimes=$(_math $waittimes + 1) if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ] ; then _err "$d:Timeout" _clearupwebbroot "$_currentRoot" "$removelevel" "$token" @@ -1418,8 +1479,8 @@ issue() { fi _setopt "$DOMAIN_CONF" "Le_RenewalDays" "=" "$Le_RenewalDays" - - let "Le_NextRenewTime=Le_CertCreateTime+Le_RenewalDays*24*60*60" + + Le_NextRenewTime=$(_math $Le_CertCreateTime + $Le_RenewalDays \* 24 \* 60 \* 60) _setopt "$DOMAIN_CONF" "Le_NextRenewTime" "=" "$Le_NextRenewTime" Le_NextRenewTimeStr=$( _time2str $Le_NextRenewTime ) diff --git a/dnsapi/dns_cf.sh b/dnsapi/dns_cf.sh index c5eaac4..a6c1576 100755 --- a/dnsapi/dns_cf.sh +++ b/dnsapi/dns_cf.sh @@ -112,7 +112,7 @@ _get_root() { return 1 fi p=$i - let "i+=1" + i=$(expr $i + 1) done return 1 } diff --git a/dnsapi/dns_cx.sh b/dnsapi/dns_cx.sh index 3ebb7bf..1975802 100644 --- a/dnsapi/dns_cx.sh +++ b/dnsapi/dns_cx.sh @@ -159,7 +159,7 @@ _get_root() { return 1 fi p=$i - let "i+=1" + i=$(expr $i + 1) done return 1 } diff --git a/dnsapi/dns_dp.sh b/dnsapi/dns_dp.sh index f47d4bd..a504379 100644 --- a/dnsapi/dns_dp.sh +++ b/dnsapi/dns_dp.sh @@ -165,7 +165,7 @@ _get_root() { return 1 fi p=$i - let "i+=1" + i=$(expr $i + 1) done return 1 } From f4312b44286bc2073a72244a6606f9c44f1b002b Mon Sep 17 00:00:00 2001 From: neil Date: Sun, 17 Apr 2016 18:54:06 +0800 Subject: [PATCH 02/11] fix --- acme.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/acme.sh b/acme.sh index 2c16799..c569e4b 100755 --- a/acme.sh +++ b/acme.sh @@ -30,7 +30,7 @@ fi _URGLY_PRINTF="" -if [ "$(printf '\x41')" = '\x41' ] ; then +if [ "$(printf '\x41')" != 'A' ] ; then _URGLY_PRINTF=1 fi @@ -132,6 +132,7 @@ _h2b() { uselet="1" fi _debug uselet "$uselet" + _debug _URGLY_PRINTF "$_URGLY_PRINTF" while [ '1' ] ; do if [ -z "$_URGLY_PRINTF" ] ; then h=$(printf $hex | cut -c $i-$j) @@ -150,8 +151,8 @@ _h2b() { printf '\'"$(printf %o "$(_math $ic \* 16 + $jc)")" fi if [ "$uselet" ] ; then - let "i+=2" - let "j+=2" + let "i+=2" >/dev/null + let "j+=2" >/dev/null else i="$(_math $i + 2)" j="$(_math $j + 2)" From c5f2df5c9304120eb740a188e1f533a54414e925 Mon Sep 17 00:00:00 2001 From: neil Date: Sun, 17 Apr 2016 19:16:48 +0800 Subject: [PATCH 03/11] fix compatible for sh. head -n -1 is not supported on pfsense. --- acme.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/acme.sh b/acme.sh index 7d3b30c..0453240 100755 --- a/acme.sh +++ b/acme.sh @@ -194,7 +194,7 @@ _getfile() { _err "Can not find start line: $startline" return 1 fi - + i="$(_math $i + 1)" _debug i $i j="$(grep -n -- "$endline" $filename | cut -d : -f 1)" @@ -202,10 +202,10 @@ _getfile() { _err "Can not find end line: $endline" return 1 fi - + j="$(_math $j - 1)" _debug j $j - sed -n $i,${j}p "$filename" | head -n -1 | tail -n +2 + sed -n $i,${j}p "$filename" } From 0a7c9364427f925130b24233f608a55986a56761 Mon Sep 17 00:00:00 2001 From: neil Date: Sun, 17 Apr 2016 19:47:22 +0800 Subject: [PATCH 04/11] modify shebang to bash after install if bash is installed. --- acme.sh | 29 +++++++++++++++++++++++++++-- dnsapi/dns_cf.sh | 2 +- dnsapi/dns_cx.sh | 2 +- dnsapi/dns_dp.sh | 2 +- dnsapi/dns_myapi.sh | 2 +- 5 files changed, 31 insertions(+), 6 deletions(-) diff --git a/acme.sh b/acme.sh index 0453240..697dc23 100755 --- a/acme.sh +++ b/acme.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/usr/bin/env sh VER=2.2.1 PROJECT_NAME="acme.sh" @@ -1844,6 +1844,19 @@ _precheck() { return 0 } +_setShebang() { + _file="$1" + _shebang="$2" + if [ -z "$_shebang" ] ; then + _err "Usage: file shebang" + return 1 + fi + cp "$_file" "$_file.tmp" + echo "$_shebang" > "$_file" + sed -n 2,99999p "$_file.tmp" >> "$_file" + rm -f "$_file.tmp" +} + install() { if ! _initpath ; then @@ -1937,7 +1950,19 @@ install() { fi installcronjob - + + #Modify shebang + if _exists bash ; then + _info "Good, bash is installed, change the shebang to use bash as prefered." + _shebang='#!/usr/bin/env bash' + _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang" + if [ -d "$LE_WORKING_DIR/dnsapi" ] ; then + for _apifile in $(ls "$LE_WORKING_DIR/dnsapi/"*.sh) ; do + _setShebang "$_apifile" "$_shebang" + done + fi + fi + _info OK } diff --git a/dnsapi/dns_cf.sh b/dnsapi/dns_cf.sh index a6c1576..2aac340 100755 --- a/dnsapi/dns_cf.sh +++ b/dnsapi/dns_cf.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/usr/bin/env sh # diff --git a/dnsapi/dns_cx.sh b/dnsapi/dns_cx.sh index 1975802..070a2ad 100644 --- a/dnsapi/dns_cx.sh +++ b/dnsapi/dns_cx.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/usr/bin/env sh # Cloudxns.com Domain api # diff --git a/dnsapi/dns_dp.sh b/dnsapi/dns_dp.sh index a504379..510384a 100644 --- a/dnsapi/dns_dp.sh +++ b/dnsapi/dns_dp.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/usr/bin/env sh # Dnspod.cn Domain api # diff --git a/dnsapi/dns_myapi.sh b/dnsapi/dns_myapi.sh index 3c5b865..f06cc1e 100644 --- a/dnsapi/dns_myapi.sh +++ b/dnsapi/dns_myapi.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/usr/bin/env sh #Here is a sample custom api script. #This file name is "dns_myapi.sh" From fc33dbb54e5d07d086ba5a1c97c8f0ea7aa4d333 Mon Sep 17 00:00:00 2001 From: neil Date: Sun, 17 Apr 2016 20:49:28 +0800 Subject: [PATCH 05/11] fix compatible --- acme.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/acme.sh b/acme.sh index 697dc23..8882b09 100755 --- a/acme.sh +++ b/acme.sh @@ -720,12 +720,12 @@ _startserver() { _debug "_NC" "$_NC" # while true ; do if [ "$DEBUG" ] ; then - if ! echo -e -n "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort -vv ; then - echo -e -n "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort -vv ; + if ! echo -n "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort -vv ; then + echo -n "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort -vv ; fi else - if ! echo -e -n "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort > /dev/null 2>&1; then - echo -e -n "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort > /dev/null 2>&1 + if ! echo -n "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort > /dev/null 2>&1; then + echo -n "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort > /dev/null 2>&1 fi fi if [ "$?" != "0" ] ; then From a1048c48e467de9feb8f13042affda4d4194e9d3 Mon Sep 17 00:00:00 2001 From: neil Date: Sun, 17 Apr 2016 20:58:56 +0800 Subject: [PATCH 06/11] fix compatible for dash --- acme.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/acme.sh b/acme.sh index 8882b09..001ef04 100755 --- a/acme.sh +++ b/acme.sh @@ -720,12 +720,12 @@ _startserver() { _debug "_NC" "$_NC" # while true ; do if [ "$DEBUG" ] ; then - if ! echo -n "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort -vv ; then - echo -n "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort -vv ; + if ! printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort -vv ; then + printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort -vv ; fi else - if ! echo -n "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort > /dev/null 2>&1; then - echo -n "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort > /dev/null 2>&1 + if ! printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort > /dev/null 2>&1; then + printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort > /dev/null 2>&1 fi fi if [ "$?" != "0" ] ; then From a79b26af6cc3b6419e4eca3bed73147105a218a4 Mon Sep 17 00:00:00 2001 From: Richard van Dijk Date: Mon, 18 Apr 2016 02:37:35 +0200 Subject: [PATCH 07/11] fix renewAll bug $CERT_HOME is required by renewAll, but wasn't initialized. --- acme.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/acme.sh b/acme.sh index 001ef04..303da6f 100755 --- a/acme.sh +++ b/acme.sh @@ -824,17 +824,17 @@ _initpath() { ACCOUNT_KEY_PATH="$_DEFAULT_ACCOUNT_KEY_PATH" fi + _DEFAULT_CERT_HOME="$LE_WORKING_DIR" + if [ -z "$CERT_HOME" ] ; then + CERT_HOME="$_DEFAULT_CERT_HOME" + fi + domain="$1" if [ -z "$domain" ] ; then return 0 fi - _DEFAULT_CERT_HOME="$LE_WORKING_DIR" - if [ -z "$CERT_HOME" ] ; then - CERT_HOME="$_DEFAULT_CERT_HOME" - fi - domainhome="$CERT_HOME/$domain" mkdir -p "$domainhome" From 641989fdee337d108c8598762df291d61b0e9ee0 Mon Sep 17 00:00:00 2001 From: neil Date: Mon, 18 Apr 2016 22:43:33 +0800 Subject: [PATCH 08/11] NO_DETECT_SH --- acme.sh | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/acme.sh b/acme.sh index fbddc39..74536ec 100755 --- a/acme.sh +++ b/acme.sh @@ -1951,15 +1951,17 @@ install() { installcronjob - #Modify shebang - if _exists bash ; then - _info "Good, bash is installed, change the shebang to use bash as prefered." - _shebang='#!/usr/bin/env bash' - _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang" - if [ -d "$LE_WORKING_DIR/dnsapi" ] ; then - for _apifile in $(ls "$LE_WORKING_DIR/dnsapi/"*.sh) ; do - _setShebang "$_apifile" "$_shebang" - done + if [ -z "$NO_DETECT_SH" ] ; then + #Modify shebang + if _exists bash ; then + _info "Good, bash is installed, change the shebang to use bash as prefered." + _shebang='#!/usr/bin/env bash' + _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang" + if [ -d "$LE_WORKING_DIR/dnsapi" ] ; then + for _apifile in $(ls "$LE_WORKING_DIR/dnsapi/"*.sh) ; do + _setShebang "$_apifile" "$_shebang" + done + fi fi fi From 80a0a7b5c598e62507eff8b6c043bd1cbae82d17 Mon Sep 17 00:00:00 2001 From: neil Date: Tue, 19 Apr 2016 18:36:15 +0800 Subject: [PATCH 09/11] minor: fix apachectl error message (#160) --- acme.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/acme.sh b/acme.sh index 74536ec..49170ff 100755 --- a/acme.sh +++ b/acme.sh @@ -871,6 +871,11 @@ _initpath() { _apachePath() { + if ! _exists apachectl ; then + _err "'apachecrl not found. It seems that apache is not installed, or you are not root user.'" + _err "Please use webroot mode to try again." + return 1 + fi httpdconfname="$(apachectl -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"' )" if _startswith "$httpdconfname" '/' ; then httpdconf="$httpdconfname" From c8b6fe62a1637e01cf582925f299c59438fdbe8e Mon Sep 17 00:00:00 2001 From: neil Date: Tue, 19 Apr 2016 22:01:51 +0800 Subject: [PATCH 10/11] fix compatible for windows nc --- acme.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/acme.sh b/acme.sh index 49170ff..a39cdb6 100755 --- a/acme.sh +++ b/acme.sh @@ -720,8 +720,8 @@ _startserver() { _debug "_NC" "$_NC" # while true ; do if [ "$DEBUG" ] ; then - if ! printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort -vv ; then - printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort -vv ; + if ! printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort ; then + printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort ; fi else if ! printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort > /dev/null 2>&1; then From 0cca212843afa86c14fbccb6f1130817a8d9babc Mon Sep 17 00:00:00 2001 From: neil Date: Tue, 19 Apr 2016 23:17:38 +0800 Subject: [PATCH 11/11] v2.2.2 support sh --- acme.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/acme.sh b/acme.sh index a39cdb6..81f170b 100755 --- a/acme.sh +++ b/acme.sh @@ -1,5 +1,5 @@ #!/usr/bin/env sh -VER=2.2.1 +VER=2.2.2 PROJECT_NAME="acme.sh"