support sh

This commit is contained in:
neil 2016-04-19 23:23:52 +08:00
commit 073ca4bf8d
5 changed files with 123 additions and 34 deletions

143
acme.sh
View File

@ -1,5 +1,5 @@
#!/usr/bin/env bash
VER=2.2.1
#!/usr/bin/env sh
VER=2.2.2
PROJECT_NAME="acme.sh"
@ -28,6 +28,13 @@ if [ -z "$AGREEMENT" ] ; then
fi
_URGLY_PRINTF=""
if [ "$(printf '\x41')" != 'A' ] ; then
_URGLY_PRINTF=1
fi
_info() {
if [ -z "$2" ] ; then
echo "[$(date)] $1"
@ -84,18 +91,72 @@ _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"
_debug _URGLY_PRINTF "$_URGLY_PRINTF"
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" >/dev/null
let "j+=2" >/dev/null
else
i="$(_math $i + 2)"
j="$(_math $j + 2)"
fi
done
}
@ -133,7 +194,7 @@ _getfile() {
_err "Can not find start line: $startline"
return 1
fi
let "i+=1"
i="$(_math $i + 1)"
_debug i $i
j="$(grep -n -- "$endline" $filename | cut -d : -f 1)"
@ -141,7 +202,7 @@ _getfile() {
_err "Can not find end line: $endline"
return 1
fi
let "j-=1"
j="$(_math $j - 1)"
_debug j $j
sed -n $i,${j}p "$filename"
@ -427,7 +488,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 +502,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
@ -658,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 ; then
echo -e -n "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort ;
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 ! 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 ! 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
@ -1122,7 +1184,7 @@ issue() {
_currentRoot="$_w"
fi
_debug "_currentRoot" "$_currentRoot"
let "_index+=1"
_index=$(_math $_index + 1)
vtype="$VTYPE_HTTP"
if _startswith "$_currentRoot" "dns" ; then
@ -1317,7 +1379,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"
@ -1423,8 +1485,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 )
@ -1787,6 +1849,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
@ -1880,7 +1955,21 @@ install() {
fi
installcronjob
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
_info OK
}

View File

@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh
#
@ -112,7 +112,7 @@ _get_root() {
return 1
fi
p=$i
let "i+=1"
i=$(expr $i + 1)
done
return 1
}

View File

@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh
# Cloudxns.com Domain api
#
@ -159,7 +159,7 @@ _get_root() {
return 1
fi
p=$i
let "i+=1"
i=$(expr $i + 1)
done
return 1
}

View File

@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh
# Dnspod.cn Domain api
#
@ -165,7 +165,7 @@ _get_root() {
return 1
fi
p=$i
let "i+=1"
i=$(expr $i + 1)
done
return 1
}

View File

@ -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"