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

"
echo -n "";
RAMDISK="/usr/ramdisk"
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 ${RAMDISK}/tmp
${FLOWEYE} webauth stat > ${RAMDISK}/tmp/webauth.stat
. ${RAMDISK}/tmp/webauth.stat
[ "${webauth_afterlogin}" = "NULL" ] && webauth_afterlogin=""
[ "${webauth_type}" = "2" ] && disabled="disabled "
if [ "${REQUEST_METHOD}" = "GET" ]; then
    errmsg=`echo ${QUERY_STRING} | grep "^clientip=.*.iarmdst="`
    if [ "${errmsg}" != "" ]; then
        eval `echo "CGI_${QUERY_STRING}" | cut -d'&' -f1`
        CGI_httpreferer="`echo ${QUERY_STRING} | awk '{i=index($1,"&");if(i!=0)print substr($1,i+9);}'`"
    fi
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 onModifyPwd()
{
  var httpreferer = document.getElementsByName(\"httpreferer\")[0];
	window.location.href = \"/cgi-bin/modifypwd?clientip=${CGI_clientip}&httpreferer=\"+httpreferer.value;
}
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 onloadbody()
{
  var httpreferer = document.getElementsByName(\"httpreferer\")[0];
  if(typeof(httpreferer) == \"object\") {
      var lnk=\"${CGI_httpreferer}\";
      if(lnk.match('^http') == null) 
          lnk = 'http://' + lnk;
      httpreferer.value=escape(lnk);
  }
}
</script>
<body onload=\"onloadbody()\">
";
if [ "${REQUEST_METHOD}" = "POST" ]; then
  if [ "${CGI_clientip}" = "" ]; then
      CGI_clientip="${REMOTE_ADDR}"
  fi
	echo "<br><br><br><br><br><center><h2>Ժ֤ʺš</h2><br><a ID=ShowTitle></a></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    
    	webauth_adpath=`echo "${webauth_adpath}" | sed "s/{loginDN}/${CGI_username}/g"`
    	errmsg=`${LDAPAUTH} -h ${webauth_adserver} -p ${webauth_adport} -D ${webauth_adpath} -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>"
      			else
      				echo "<script language=javascript>"
              if [ "${webauth_type}" != "0" ]; then
      				  echo "alert(\"½ʧܣԺ!${errmsg}\");"
              else
      				  echo "alert(\"û벻!\");"
              fi
      				echo "</script>"
      			fi
    		fi
    	else
     		if [ "${webauth_afterlogin}" != "" ]; then
          echo "<META HTTP-EQUIV=REFRESH CONTENT=\"0;URL=http://${webauth_afterlogin}\" />"
    		else
          echo "<script language=javascript>"
          if [ "${CGI_httpreferer}" = "" -o "${CGI_httpreferer}" = "http://" ]; then
              echo "document.getElementsByTagName(\"h2\")[0].innerHTML = '¼ɹԿʼʹ!';"
              echo "alert(\"¼ɹԿʼʹ!\");"
          else
              echo "window.location=\"${CGI_httpreferer}\";"
          fi
          echo "</script></body></html>"
    		fi
        exit 0;
      fi
  fi
  echo "<script>document.body.innerHTML = '';</script>"
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:90px;font-size:14px;\" align=\"center\"><b>û</b></td><td><input type=\"text\" value=\"${CGI_username}\" id=\"username\" name=\"username\" style=\"width:170px;\" /></td></tr>
	<tr><td align=\"center\" style=\"font-size:14px;\"><b>&nbsp;&nbsp;&nbsp;</b></td>
	<td><input type=\"password\" value=\"\" id=\"password\" name=\"password\" style=\"width:170px\" /></td></tr>
	<tr><td colspan=\"2\"></td></tr>
	<tr><td></td>
	<td><input type=\"submit\" value=\"¼\" style=\"width:80px;height:22px;\"/>&nbsp;&nbsp;
	    <input type=button value=\"޸...\" ${disabled}style=\"width:80px;height:22px\" onclick=\"onModifyPwd()\" />
	    <input type=hidden name=clientip value=\"${CGI_clientip}\" />
	    <input type=hidden name=httpreferer value=\"\"/>
	</td>
	</tr>
</table>
</form>
</div>
</body>
</html>
";