繁体   English   中英

使用CGI的Shell脚本在bash上可以正常使用,但在sh上则不能

[英]Shell script with CGI works fine with bash but not with sh

我发现以下使用bash的CGI的工作示例。 如果我将前两行更改为

#!/bin/sh
echo "Content-type: text/html\n\n"

以下脚本停止工作,并且当我在浏览器中浏览脚本时,在脚本底部声明的“ foo”,“ bar”和“ foobar”消失了。

任何想法我怎样才能使同一个示例与sh一起工作。 实际上,我需要在没有bash但sh的嵌入式设备上运行这样的示例。

#!/bin/bash
echo -e "Content-type: text/html\n\n"
echo "
<html>
<body>
<form action="http://${HTTP_HOST}:${SERVER_PORT}${SCRIPT_NAME}?foo=1234" method="POST">
<input type="text" name="bar">
<textarea name="foobar"></textarea>
<input type="submit">
</form>"


# (internal) routine to store POST data
cgi_get_POST_vars()
{
    # check content type
    # FIXME: not sure if we could handle uploads with this..
    [ "${CONTENT_TYPE}" != "application/x-www-form-urlencoded" ] && \
    echo "Warning: you should probably use MIME type "\
         "application/x-www-form-urlencoded!" 1>&2
    # save POST variables (only first time this is called)
    [ -z "$QUERY_STRING_POST" \
      -a "$REQUEST_METHOD" = "POST" -a ! -z "$CONTENT_LENGTH" ] && \
    read -n $CONTENT_LENGTH QUERY_STRING_POST
    return
}

# (internal) routine to decode urlencoded strings
cgi_decodevar()
{
    [ $# -ne 1 ] && return
    local v t h
    # replace all + with whitespace and append %%
    t="${1//+/ }%%"
    while [ ${#t} -gt 0 -a "${t}" != "%" ]; do
    v="${v}${t%%\%*}" # digest up to the first %
    t="${t#*%}"       # remove digested part
    # decode if there is anything to decode and if not at end of string
    if [ ${#t} -gt 0 -a "${t}" != "%" ]; then
        h=${t:0:2} # save first two chars
        t="${t:2}" # remove these
        v="${v}"`echo -e \\\\x${h}` # convert hex to special char
    fi
    done
    # return decoded string
    echo "${v}"
    return
}

# routine to get variables from http requests
# usage: cgi_getvars method varname1 [.. varnameN]
# method is either GET or POST or BOTH
# the magic varible name ALL gets everything
cgi_getvars()
{
    [ $# -lt 2 ] && return
    local q p k v s
    # get query
    case $1 in
    GET)
        [ ! -z "${QUERY_STRING}" ] && q="${QUERY_STRING}&"
        ;;
    POST)
        cgi_get_POST_vars
        [ ! -z "${QUERY_STRING_POST}" ] && q="${QUERY_STRING_POST}&"
        ;;
    BOTH)
        [ ! -z "${QUERY_STRING}" ] && q="${QUERY_STRING}&"
        cgi_get_POST_vars
        [ ! -z "${QUERY_STRING_POST}" ] && q="${q}${QUERY_STRING_POST}&"
        ;;
    esac
    shift
    s=" $* "
    # parse the query data
    while [ ! -z "$q" ]; do
    p="${q%%&*}"  # get first part of query string
    k="${p%%=*}"  # get the key (variable name) from it
    v="${p#*=}"   # get the value from it
    q="${q#$p&*}" # strip first part from query string
    # decode and evaluate var if requested
    [ "$1" = "ALL" -o "${s/ $k /}" != "$s" ] && \
        eval "$k=\"`cgi_decodevar \"$v\"`\""
    done
    return
}



# register all GET and POST variables
cgi_getvars BOTH ALL

echo "<pre>foo=$foo</pre>"
echo "<pre>bar=$bar</pre>"
echo "<pre>foobar=$foobar</pre>"

echo "</body>
</html>"

更新1: sh -x script返回以下内容:

+ echo Content-type: text/html\n\n
Content-type: text/html


+ echo 
<html>
<body>
<form action=http://:?foo=1234 method=POST>
<input type=text name=bar>
<textarea name=foobar></textarea>
<input type=submit>
</form>

<html>
<body>
<form action=http://:?foo=1234 method=POST>
<input type=text name=bar>
<textarea name=foobar></textarea>
<input type=submit>
</form>
+ cgi_getvars BOTH ALL
+ [ 2 -lt 2 ]
+ local q p k v s
+ [ ! -z  ]
+ cgi_get_POST_vars
+ [  != application/x-www-form-urlencoded ]
+ echo Warning: you should probably use MIME type  application/x-www-form-urlencoded!
Warning: you should probably use MIME type  application/x-www-form-urlencoded!
+ [ -z  -a  = POST -a ! -z  ]
+ return
+ [ ! -z  ]
+ shift
+ s= ALL 
+ [ ! -z  ]
+ return
+ echo <pre>foo=</pre>
<pre>foo=</pre>
+ echo <pre>bar=</pre>
<pre>bar=</pre>
+ echo <pre>foobar=</pre>
<pre>foobar=</pre>
+ echo </body>
</html>
</body>
</html>

与POSIX规范相比,Bash具有许多扩展,您的脚本正在使用其中的一些扩展。 您的/bin/sh显然不是bash(可能是ash,破折号,mksh等),并且没有这些扩展名。 您将必须遍历脚本并对照sh或POSIX规范的文档检查每个构造。

幸运地看:

  • function cgi_get_POST_vars() :不应在其中使用function关键字,并且大括号应位于同一行。
  • read -n $CONTENT_LENGTH QUERY_STRING_POST :读取(内置shell)在POSIX中没有-n选项。
  • t="${1//+/ }%%"h=${t:0:2} :Bourne不支持这两个修饰符。

但可能还会更多。

编辑:

  • echo是外壳之间最不兼容的命令。 该标准只是说\\行为是实现定义的。 您必须改用printf

使用function关键字的函数声明与Bourne脚本不兼容。 sh的正确语法是

cgi_get_POST_vars() {
    ...
}

还要小心使用此类脚本。 该参数解析器容易受到外壳注入攻击。 例如,如果传递了一个变量:

cgi-bin/myscript.cgi&foo=bar`ls`bar

我相信“ ls”命令将在参数解码期间执行。 您可以想象到比“ ls”更具破坏性的命令。 还请考虑如果敌对用户传递参数&PATH=""会发生什么。 这些东西很难弄清楚,如果您正在处理嵌入式设备,那么如果Web服务器以root特权运行,我将不会感到惊讶。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM