我正在使用 cgi 实现登录页面。我能够正确检索登录名和密码,但我不知道如何更改为该用户。我正在使用 httpd apache。
在研究了可能的方法之后,我发现可以使用 suexec 来解决这个问题,但我不太清楚如何使用它。
这是我的 cgi 代码:
#!/bin/bash
echo "Content-type: text/html"
echo ""
echo '<html>'
echo '<head>'
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
echo '<title>Form Example</title>'
echo '</head>'
echo '<body>'
echo "<form method=GET action=\"${SCRIPT}\">"\
'<table nowrap>'\
'<tr><td>Username</TD><TD><input type="text" name="val_x" size=12></td></tr>'\
'<tr><td>Password</td><td><input type="password" name="val_y" size=12 value=""></td>'\
'</tr></table>'
echo '<br><input type="submit" value="Login">'\
'</form>'
# Make sure we have been invoked properly.
if [ "$REQUEST_METHOD" != "GET" ]; then
echo "<hr>Script Error:"\
"<br>Usage error, cannot complete request, REQUEST_METHOD!=GET."\
"<br>Check your FORM declaration and be sure to use METHOD=\"GET\".<hr>"
exit 1
fi
# If no search arguments, exit gracefully now.
if [ -z "$QUERY_STRING" ]; then
exit 0
else
# No looping this time, just extract the data you are looking for with sed:
XX=`echo "$QUERY_STRING" | sed -n 's/^.*val_x=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
YY=`echo "$QUERY_STRING" | sed -n 's/^.*val_y=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
echo "val_x: " $XX
echo '<br>'
echo "val_y: " $YY
su $XX -p $YY #I know this is not the best way to login but I'm using it for testing purposes
echo ""
echo `whoami`
echo `pwd`
fi
echo '</body>'
echo '</html>'
exit 0
每当我尝试“whoami”时总是返回 apache。
这是我获得部分代码的链接: http://www.yolinux.com/TUTORIALS/BashShellCgi.html