#!/bin/sh
#This script is created by ssparser automatically. The parser first created by MaoShouyan
printf "Content-type: text/html;charset=gb2312
Cache-Control: no-cache

"
echo -n "";
if [ -f ${RAMDISK}/PG.conf ]; then
        . ${RAMDISK}/PG.conf
        PGPATH="${RAMDISK}"
else
        . /etc/PG.conf
fi
LDAPAUTH=${PGPATH}/bin/ldapauth
RADAUTH=${PGPATH}/bin/radauth
FLOWEYE=${PGPATH}/bin/floweye
mkdir -p /usr/ramdisk/tmp
${FLOWEYE} webauth stat > /usr/ramdisk/tmp/webauth.stat
. /usr/ramdisk/tmp/webauth.stat
[ "${webauth_afterlogin}" = "NULL" ] && webauth_afterlogin=""
[ "${webauth_type}" = "2" ] && disabled="disabled "
if [ "${REQUEST_METHOD}" = "GET" ]; then
	CGI_userurl=`echo ${QUERY_STRING} | cut -d '=' -f4-`
fi

echo -n "
<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\">
<head><title>û¼</title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">
<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">
<META HTTP-EQUIV=\"Cache-Control\" content=\"no-cache\">
<META name=\"viewport\" content=\"width=device-width,user-scalable=no\"/>  
<style type=\"text/css\">
body {background:#ffffff;font-family:΢ź arial;}
.login {position:absolute;width:350px;height:200px;top:45%;left:48%;margin-top:-100px;margin-left:-150px;}
</style></head>
<script language=\"javascript\">
function beforeCommit(frmObj)
{
	var passwd = document.getElementById(\"password\");
	var usrname = document.getElementById(\"username\");
	if (usrname.value == \"\") {
		alert(\"û!\");
		usrname.select();
		return false;
	}
	if (passwd.value == \"\") {
		alert(\"!\");
		passwd.select();
		return false;
	}
	return true;
}
function onModifyPwd()
{
        window.location.href = \"/cgi-bin/modifypwd\";
}
</script>
<body>
";
if [ "${REQUEST_METHOD}" = "POST" ]; then
	[ "${CGI_clientip}" = "" ] && CGI_clientip="${REMOTE_ADDR}"
	echo "<br><br><br><br><br><center><h2>Ժ֤ʺš</h2><b</center>" 
	isok="ok"
	if [ "${webauth_type}" = "1" ]; then 
		if [ "${webauth_adserver}" = "0.0.0.0" -o "${webauth_adport}" = "0" -o "${webauth_adpath}" = "" ]; then
			echo "<script language=javascript>alert(\"½ʧܣ½򲻴ڣϵԱ\");</script>"
			isok=""
		else    
			errmsg=`${LDAPAUTH} -h ${webauth_adserver} -p ${webauth_adport} -D ${CGI_username} -w ${CGI_password} -n 2>/dev/null`
			if [ "$?" = "0" ]; then
				args="name=ad:${CGI_username}"
			else
				echo "<script language=javascript>alert(\"¼ʧܣʺŻ!\");</script>"
				isok=""
			fi
		fi
	elif [ "${webauth_type}" = "2" ]; then
    		if [ "${webauth_rdsecret}" = "" ]; then
			echo "<script language=javascript>alert(\"½ʧܣ½ϵԱ\");</script>"
			isok=""
		else
			OPTIONS="User-Name=${CGI_username},User-Password=${CGI_password},Message-Authenticator=0x00"                 
			msg=`echo "${OPTIONS}" | ${RADAUTH} -d ${PGPATH}/etc/radius ${webauth_rdserver}:${webauth_rdport} auth ${webauth_rdsecret}`
			if [ "$?" = "1" ]; then
				echo "<script language=javascript>alert(\"¼ʧܣʺŻ!\");</script>"
				isok=""
			fi   
			args="name=rd:${CGI_username}" 
		fi
	else
		args="name=${CGI_username} passwd=${CGI_password}"
	fi 
	if [ "${isok}" = "ok" ]; then      	
    		errmsg=`${FLOWEYE} usrinfo login ${args} loginip=${CGI_clientip}`
		if [ "$?" != "0" ]; then
    			if [ "${webauth_type}" = "1" ]; then
      				echo "<script language=javascript>"
      				echo "alert(\"${errmsg}\");"
      				echo "</script>"
    			else
      				if [ "${errmsg}" = "DISABLED" ]; then
      					echo "<script language=javascript>"
      					echo "alert(\"˺Ѿֹʹ!\");"
      					echo "</script>"
      				elif [ "${errmsg}" = "MAXLOGINED" ]; then
      					echo "<script language=javascript>"
      					echo "alert(\"˺Ѿһַ¼!\");"
      					echo "</script>"
      				elif [ "${errmsg}" = "LOGINED" ]; then
      					echo "<script language=javascript>"
      					echo "alert(\"˺Ѿ¼!\");"
      					echo "</script>"
				elif [ "${errmsg}" = "IPFULL" ]; then
      					echo "<script language=javascript>"
      					echo "alert(\"Ȩ󳬹ϵͳ趨ֵ!\");"
      					echo "</script>"
				elif [ "${errmsg}" = "IPMISMATCH" ]; then
      					echo "<script language=javascript>"
      					echo "alert(\"½IPͰ󶨵IPһ!\");"
      					echo "</script>"
      				else
      					echo "<script language=javascript>"
              				if [ "${webauth_type}" != "0" ]; then
						echo "alert(\"½ʧܣԺ!${errmsg}\");"
					else
						echo "alert(\"û벻(${errmsg})!\");"
					fi
					echo "</script>"
      				fi
    			fi
		else
			echo "<br><center><h2>֤ɹڼϴηҳ</h2></center>" 
     			if [ "${webauth_afterlogin}" != "" ]; then
          			echo "<META HTTP-EQUIV=REFRESH CONTENT=\"0;URL=http://${webauth_afterlogin}\" />"
    			else
          			echo "<META HTTP-EQUIV=REFRESH CONTENT=\"0;URL=http://${CGI_userurl}\" />"
			fi
			echo "</body></html>"
			exit 0;
		fi
	fi
fi

echo -n "
<div class=\"login\" id=\"login\">
<form action=\"/cgi-bin/login\" method=\"POST\" onsubmit=\"return beforeCommit(this)\" enctype=\"multipart/form-data\">
<table>
	<tr><td colspan=\"2\" align=\"center\"><img src=\"/login_logo.gif\" /></td></tr>
	<tr><td style=\"width:70px;font-size:14px;\" align=\"center\"><b>û</b></td>
	<td><input type=\"text\" value=\"${CGI_username}\" id=\"username\" name=\"username\" style=\"width:168px;\" /></td></tr>
	<tr><td align=\"center\" style=\"font-size:14px;\"><b>&nbsp;&nbsp;</b></td>
	<td><input type=\"password\" value=\"\" id=\"password\" name=\"password\" style=\"width:168px\" /></td></tr>
	<tr><td colspan=\"2\"></td></tr>
	<tr><td></td>
	<td><input type=\"submit\" value=\"ȷ&nbsp;&nbsp;\" style=\"width:80px;height:22px;\"/>
            <input type=button value=\"޸...\" ${disabled}style=\"width:80px;height:22px\" onclick=\"onModifyPwd()\" />
	    <input type=hidden name=clientip value=\"${CGI_clientip}\" />
	    <input type=hidden name=userurl value=\"${CGI_userurl}\"/>
	</td>
	</tr>
</table>
</form>
</div>
</body>
</html>
";