#!c:\perl\bin\perl
###################################################
#  RAID utility CGI Program                       # 
#       SAN Project                               # 
#                 Copy right. 2006  Hitachi.Lmt.  # 
###################################################
#
# History  :Version          :DATE        : Coder       :Patch No.
#          :50-09-00/00-05   :2006.09.14  :T.Harada     :USSO2227412
#          :50-09-00/00-05   :2006.10.03  :T.Harada     :USSO2227433
#          :50-09-00/00-05   :2006.10.03  :T.Harada     :USSO2227435
#          :50-09-00/00-05   :2006.10.03  :T.Harada     :USSO2227436
#          :50-09-00/00-05   :2006.10.05  :T.Harada     :USSO2227440
#          :50-09-00/00-05   :2006.11.08  :T.Harada     :USSO2227467
#          :50-09-00/00-05   :2006.11.10  :T.Harada     :USSO2227469
#          :50-09-00/00-08   :2006.11.17  :T.Harada     :USSO2227481
#          :50-09-01/00-00   :2007.04.27  :M.Kondoh     :USSO2227549 For HP requirement
#
# subroutine name  :VerifyCretificate()
# filename         :SSO
# 
# Augument ()
#
# Document
# 
#
#

#requirepɃJgfBNgړ #
chdir("..");

require 'cgi-lib.cgi';
require 'sjserror.cgi';
require 'logoutput.cgi';
require 'message.cgi';
require 'SSOSubroutine.cgi';
require 'sceDefine.cgi';

&ReadParse(*in); 
VerifyCretificate($in{'TKN'}, $in{'KEY'}, $in{'XE'}, $in{'UN'}, $in{'UA'}, $in{'URL'});
exit(0);


##=======================================================================================#
## Name    : VerifyCretificate()                                                         #
## Func    : ؖ̔F؏sB                                                    #
## Param   : String : Token      : I : Token                                         #
##           String : OneTimeKey : I : SVPsOneTimeKey                           #
##           String : HostName   : I : T[o[ or IPAhX                          #
##           String : SSOUser    : I : [U                                          #
##           String : Authority  : I : [UAJEg                              #
##           String : URL        : I : Storage Navigator̋NURL                        #
## Return  : Ȃ                                                                        #
## Remarks : Ȃ                                                                        #
##---------------------------------------------------------------------------------------#
## Date    : rev.00 : 2006.09.14 : T.Harada   : USSO2227412 : VK쐬                   #
## Date    : rev.01 : 2006.10.03 : T.Harada   : USSO2227433 : dlύX(TKNmF@)      #
## Date    : rev.02 : 2006.10.03 : T.Harada   : USSO2227435 : dlύX(KEYLmF)  #
## Date    : rev.03 : 2006.10.03 : T.Harada   : USSO2227436 : dlύX(_CNg@) #
## Date    : rev.04 : 2006.11.08 : T.Harada   : USSO2227467 : čOΉ               #
## Date    : rev.05 : 2006.11.10 : T.Harada   : USSO2227469 : čOύX           #
## Date    : rev.06 : 2006.11.17 : T.Harada   : USSO2227481 : čOsΉ       #
##=======================================================================================#
sub VerifyCretificate{

	#ϐݒ #
	my $Token = $_[0];
	my $OneTimeKey = $_[1];
	my $HostName = $_[2];
	my $SSOUser = $_[3];
	my $Authority = $_[4];
	my $URL = $_[5];
	my $Rtn=1;
	my $HashOneTimeKey = "";
	my @Sec;
	my $Time = "";

	#Jart@C̃pX萔擾 #
	my $JarFileName = &JARFILENAME;

	#OneTimeKeyL(b)萔擾 #
	my $LimitTime = &LIMITTIME;


	#G[O #
	my $FileName = "log/SSOCGIError.log";
	my $OutputStrHeader = "***SSOCGIErrorLOG***";
	my $FunctionName = "VerifyCretificate";
	my $OutputStr = "";
	my $LapLine = "";
	my $HaxLength = "";
	my $error_id1 = "";
	my $error_id2 = "";

	# čO USSO2227467 #
	my $CategoryID=4;
	my $SubjectID=1;
	my $UserName=$SSOUser;
	my $Operation="SSO Authentication";
	my $ResultID="";
	my $Result="Normal end";

	# čOp[U[ݒ USSO2227467 #
	if( $Authority == 4 ){
		$UserAuthority = "Administrator";
	}elsif( $Authority == 3 ){
		$UserAuthority = "Operator";
	}elsif( $Authority == 2 ){
		$UserAuthority = "User";
	}
#	my $Detail="from=$HostName;SSO UA=$UserAuthority"; 	# USSO2227469 #
#	my $Detail="User=$UserName;from=$HostName;SSO UA=$UserAuthority"; 	# USSO2227469 # # USSO2227481 #
	my $Detail="+User=$UserName,from=$HostName,SSO UA=$UserAuthority"; 	# USSO2227481 #


	&message();

	#Token̊mF #
	# USSO2227433 CheckKToken()NULLłȂƂmF֕ύX #
	if($Token eq ""){
		#Token͕sȒl #
		$error_id1=290;
		$error_id2=55001;

		#čO SSOF؎s USSO2227467 #
		$ResultID=3;
		$Result="$error_id1-$error_id2";
		`cgilogex.exe ":$CategoryID:$SubjectID:$UserName:$Operation:$ResultID:$Result:$Detail:"`;

		#G[bZ[W #
	    $OutputStr = "FunctionName=$FunctionName,Error_No=$error_id1-$error_id2,Message=$message[$error_id1][$error_id2],TKN=NULL";
		logoutput($FileName,$OutputStrHeader,$OutputStr,$LapLine,$HaxLength);
	  	&sjserror($error_id1,$error_id2,$message[$error_id1][$error_id2]);
		return;
	}

	#OneTimeKeẙmF #
	$Rtn = &CheckKEY( $OneTimeKey );
	if($Rtn == 1){
		#OneTimeKey͕sȒl #
		$error_id1=290;
		$error_id2=55001;
		#čO SSOF؎s USSO2227467 #
		$ResultID=3;
		$Result="$error_id1-$error_id2";
		`cgilogex.exe ":$CategoryID:$SubjectID:$UserName:$Operation:$ResultID:$Result:$Detail:"`;

		#G[bZ[W #
    	$OutputStr = "FunctionName=$FunctionName,Error_No=$error_id1-$error_id2,Message=$message[$error_id1][$error_id2],KEY=$OneTimeKey";
		logoutput($FileName,$OutputStrHeader,$OutputStr,$LapLine,$HaxLength);
  		&sjserror($error_id1,$error_id2,$message[$error_id1][$error_id2]);
		return;
	}

	#OneTimeKey̗LmF  USSO2227435#
	@Sec = split(/_/,$OneTimeKey);
	$Time = substr($Sec[0],0,10);
	$Rtn = &CheckLimit($Time, $LimitTime);
	if($Rtn == 1){
		#OneTimeKey͖ #
		$error_id1=290;
		$error_id2=55000;

		#čO SSOF؎s USSO2227467 #
		$ResultID=3;
		$Result="$error_id1-$error_id2";
		`cgilogex.exe ":$CategoryID:$SubjectID:$UserName:$Operation:$ResultID:$Result:$Detail:"`;

		#G[bZ[W #
    	$OutputStr = "FunctionName=$FunctionName,Error_No=$error_id1-$error_id2,Message=$message[$error_id1][$error_id2],CheckLimit error,OneTimeKey=$OneTimeKey";
		logoutput($FileName,$OutputStrHeader,$OutputStr,$LapLine,$HaxLength);
  		&sjserror($error_id1,$error_id2,$message[$error_id1][$error_id2]);
		return;
	}

	#HostName̊mF #
	$Rtn = &CheckCharacter( $HostName );
	if($Rtn == 1){
		#HostName͕sȒl #
		$error_id1=290;
		$error_id2=55001;

		#čO SSOF؎s USSO2227467 #
		$ResultID=3;
		$Result="$error_id1-$error_id2";
		`cgilogex.exe ":$CategoryID:$SubjectID:$UserName:$Operation:$ResultID:$Result:$Detail:"`;

		#G[bZ[W #
    	$OutputStr = "FunctionName=$FunctionName,Error_No=$error_id1-$error_id2,Message=$message[$error_id1][$error_id2],XE=$HostName";
		logoutput($FileName,$OutputStrHeader,$OutputStr,$LapLine,$HaxLength);
  		&sjserror($error_id1,$error_id2,$message[$error_id1][$error_id2]);
		return;
	}

	#SSOUser̊mF #
	$Rtn = &CheckCharacter( $SSOUser );
	if($Rtn == 1){
		#SSOUser͕sȒl #
		$error_id1=290;
		$error_id2=55001;

		#čO SSOF؎s USSO2227467 #
		$ResultID=3;
		$Result="$error_id1-$error_id2";
		`cgilogex.exe ":$CategoryID:$SubjectID:$UserName:$Operation:$ResultID:$Result:$Detail:"`;

		#G[bZ[W #
    	$OutputStr = "FunctionName=$FunctionName,Error_No=$error_id1-$error_id2,Message=$message[$error_id1][$error_id2],UN=$SSOUser";
		logoutput($FileName,$OutputStrHeader,$OutputStr,$LapLine,$HaxLength);
  		&sjserror($error_id1,$error_id2,$message[$error_id1][$error_id2]);
		return;
	}

	#AuthoritẙmF #
	$Rtn = &CheckUA( $Authority );
	if($Rtn == 1){
		#Token͕sȒl #
		$error_id1=290;
		$error_id2=55001;

		#čO SSOF؎s USSO2227467 #
		$ResultID=3;
		$Result="$error_id1-$error_id2";
		`cgilogex.exe ":$CategoryID:$SubjectID:$UserName:$Operation:$ResultID:$Result:$Detail:"`;

		#G[bZ[W #
    	$OutputStr = "FunctionName=$FunctionName,Error_No=$error_id1-$error_id2,Message=$message[$error_id1][$error_id2],UA=$Authority";
		logoutput($FileName,$OutputStrHeader,$OutputStr,$LapLine,$HaxLength);
  		&sjserror($error_id1,$error_id2,$message[$error_id1][$error_id2]);
		return;
	}

	#URL̊mF #
# USSO2227549 For HP requirement 2007.4.27
#	$Rtn = &CheckURL( $URL );
	$Rtn = &CheckURL( $URL );
	if($Rtn == 1){
		#Token͕sȒl #
		$error_id1=290;
		$error_id2=55001;

		#čO SSOF؎s USSO2227467 #
		$ResultID=3;
		$Result="$error_id1-$error_id2";
#		`cgilogex.exe "$CategoryID:$SubjectID:$UserName:$Operation:$ResultID:$Result:$Detail"`; # USSO2227481 #
		`cgilogex.exe ":$CategoryID:$SubjectID:$UserName:$Operation:$ResultID:$Result:$Detail:"`; # USSO2227481 #


		#G[bZ[W #
    	$OutputStr = "FunctionName=$FunctionName,Error_No=$error_id1-$error_id2,Message=$message[$error_id1][$error_id2],URL=$URL";
		logoutput($FileName,$OutputStrHeader,$OutputStr,$LapLine,$HaxLength);
  		&sjserror($error_id1,$error_id2,$message[$error_id1][$error_id2]);
		return;
	}


	#OneTimeKey̗LmF #
	$Rtn = `java -classpath $JarFileName;. sanproject.serverex.data.CallIsKeyAlive $OneTimeKey`;
	#OneTimeKey̗LmF̌ #
	if($Rtn =~ /^NG/ || $Rtn =~ /false/){
		#OneTimeKey #
		$error_id1=290;
		$error_id2=55000;

		#čO SSOF؎s USSO2227467 #
		$ResultID=3;
		$Result="$error_id1-$error_id2";
		`cgilogex.exe ":$CategoryID:$SubjectID:$UserName:$Operation:$ResultID:$Result:$Detail:"`;

		#G[bZ[W #
    	$OutputStr = "FunctionName=$FunctionName,Error_No=$error_id1-$error_id2,Message=$message[$error_id1][$error_id2],Rtn=$Rtn";
    	logoutput($FileName,$OutputStrHeader,$OutputStr,$LapLine,$HaxLength);
  		&sjserror($error_id1,$error_id2,$message[$error_id1][$error_id2]);
		return;
	}

	#SSOF #
	$Rtn = `java -classpath $JarFileName;. sanproject.serverex.data.CallVerifyToken $Token $OneTimeKey $HostName $SSOUser $Authority $URL`;
	if($Rtn =~ /^NG/ || $Rtn =~ /false/){
		#SSOF؎s #
		$error_id1=290;
		$error_id2=57100;

		#čO SSOF؎s USSO2227467 #
		$ResultID=3;
		$Result="$error_id1-$error_id2";
		`cgilogex.exe ":$CategoryID:$SubjectID:$UserName:$Operation:$ResultID:$Result:$Detail:"`;

		#G[bZ[W
    	$OutputStr = "FunctionName=$FunctionName,Error_No=$error_id1-$error_id2,Message=$message[$error_id1][$error_id2],Rtn=$Rtn";
    	logoutput($FileName,$OutputStrHeader,$OutputStr,$LapLine,$HaxLength);
  		&sjserror($error_id1,$error_id2,$message[$error_id1][$error_id2]);
		return;
	}

	#čO SSOFؐ USSO2227467 #
	$ResultID=2;
	`cgilogex.exe ":$CategoryID:$SubjectID:$UserName:$Operation:$ResultID:$Result:$Detail:"`;
	
	#OneTimeKeynbVB #
	$HashOneTimeKey = `java MD "SSODIGEST" $OneTimeKey`;
	
	#############################
    #                           #
	#OCʂփ_CNg #
    #                           #
    #############################

	#SSO[UGR[h USSO2227440 #
	$SSOUser = &EncodeCharacter($SSOUser);

# USSO2227436 #
# USSO2227549 For HP requirement 2007.4.27
$URL="https://".$ENV{"HTTP_HOST"}."/Proxy/RWCXP";
print <<END_OF_REDIRECT;
Content-type: text/html

<HTML>

  <body style="background-image: url(/icon/splash.gif); background-position: center center; background-repeat: no-repeat;" onLoad=window.open("$URL?SSOuserid=$SSOUser&UA=$Authority&onetimekey_s=$HashOneTimeKey","SSORWCXP","width=1024,height=740,resizable=yes,scrollbars=yes");>
  </Body>
</HTML>


END_OF_REDIRECT

return;
}