#!c:\perl\bin\perl
# History :Version       :DATE      :Coder       : Patch No
#         :50-02-00/00-01:2004.09.07:T.Kishimoto :U5002223441 For Service Pack 2
#         :50-03-07/00-03:2004.11.24:H.Yasukawa  :USSE2223911 improved Security
#         :50-04-04/00-00:2005.07.04:H.Yasukawa  :USE22225681 Apache2.0
#         :50-04-24/00-03:2005.08.05:T.Kishimoto :USE22225851 Apache2.0 Bug for SSL service
#         :50-04-28/00-01:2005.09.09:H.Yasukawa  :USE22225809 PORT80 Block
#         :50-04-40/00-02:2005.09.19  : T.Kishimoto : USE22226024 PORT80 Block for bug
#         :50-04-40/00-0x:2005.11.19  : K.Usami  : UZEN2226418 For R500 JA Security
#         :50-07-63/XX-XX:2006.07.11  :M.Kondo   : U5002227273 For LOGOutput
#         :50-08-04/00-03:2006.09.28  :M.Kondo   : USUB2227421 For Apache observation
#         :50-09-00/00-05:2006.10.30  :T.harda   : USSO2227458 For SSO
# RAID600---------------------------------------------------------------------------------
#         :60-01-23/00-05:2007.05.08  :M.Kondo   : V61W2222782 For improved Security
sub check_httpdconf(){
my $flg;
my $ssl_work;
my $webserver_type, $env_http ;                                  # USE22225809

$apache_home = "C:/Program Files/Apache Group/Apache";
$httpdconffile="$apache_home/conf/httpd.conf";
#USE22225681 Apache2.0{
$apache_pgm[1] = "C:/Program Files/Apache Group/Apache";
$apache_pgm[2] = "C:/Program Files/Apache Group/Apache/bin";
$apache_name[1]="Apache";
$apache_name[2]="Apache2";
$httpdconf_in[1]="httpd.conf";
$httpdconf_in[2]="httpd2.conf";
$httpdconf_ssl_in[1]="httpd.conf_ssl";
$httpdconf_ssl_in[2]="httpd2.conf_ssl";
#UZEN2226418
#$current_conf_version="1.1";
#$current_conf_version="2.0"; #USUB2227421 For Apache observation
#$current_conf_version="3.0";#USUB2227421 For Apache observation
#$current_conf_version="4.0";#USSO2227458 For SSO 2006.10.30
$current_conf_version="5.0";#V61W2222782 For improved Security
#} USE22225681 Apache2.0
unless(open(FILE , "< $httpdconffile")){
    sleep 1;
    unless(open(FILE , "< $httpdconffile")){
	sleep 1;
	unless(open(FILE , "< $httpdconffile")){
	    {`echo httpd.conf File Open Error >log/httpdconf.log`; }
	    exit;
	}
    }
}

$flg=0;
$ssl_work=0;
$webserver_type = "";                                            # USE22225809
$env_http = "1" ;                                                # USE22225809

while(<FILE>){
# USE22225681 Apache2.0
 if($. ==1 && /# httpd.conf RSD Version (\d+\.\d+) for ssl(.*)/){ # USE22225809
   $ssl_work=1;                                                  #USE22226024
   $webserver_type = $2 ;                                        # USE22225809
   if($1 >= $current_conf_version){ 
       $flg=1;
#USE22225851
       last;
   }
#USE22225851
#   last;
 }
# USE22225681 Apache2.0
 if($. ==1 && /# httpd.conf RSD Version (\d+\.\d+) for normal/){
   if($1 >= $current_conf_version){ 
       $flg=2;
#USE22225851
       last;
   }
#USE22225851
#   last;
 }
 if(/^[ \t]*LoadModule[ \t]+ssl_module[ \t]+modules\/mod_ssl.so/){
     $ssl_work=1;
 }
}
close(FILE);

$webserver_type =~ s/^ // ;                                      # USE22225809
if ($webserver_type ne "") {                                     # USE22225809
    $webserver_ext  = "_".$webserver_type ;                      # USE22225809
}                                                                # USE22225809

# USE22225681 Apache2.0{
$_= `sc query apache2`;
if(/STATE/i){ #USUB2227421 Apache observation
  $apache_v=2;
}else{
  $apache_v=1;
}
# USE22226024
# Status No
# 0: Non SSL /Non Version up /Port 80 
# 1: 
# 2: Non SSL /Version up     /Port 80 
# 3: 
# 4:     SSL /Non Version up /Port 80+443 
# 5:     SSL /Non Version up /Port 443 
# 6:     SSL /Version up     /Port 80+443 
# 7:     SSL /Version up     /Port 443
$svphtmldir     = "C:/dkc200/svp_html" ;
$svphtmlextdir  = "C:/dkc200/svp_html/ext" ;

$html_name = "index.htm" ;
$html_nrml = "index.htm_nrml" ;
$html_ssl  = "index.htm_ssl" ;
use File::Copy;
$status_no ="-1";
# move USE22226024 
$env_http = "1" ; # normal                                       # USE22225809
if (($ssl_work == 1) || ($flg == 1)) {                           # USE22225809
     # SSL                                                       # USE22225809
     if ($webserver_type eq "p80b") {                            # USE22225809
         # PORT 80 Block                                         # USE22225809
         $env_http = "3" ;                                       # USE22225809
	 if($flg == 0){                                          # USE22226024
           $status_no ="7";                                      # USE22226024
         }else{                                                  # USE22226024
           $status_no ="5";                                      # USE22226024
         }                                                       # USE22226024
     } else {                                                    # USE22225809
         # No PORT 80 Block                                      # USE22225809
         $env_http = "2" ;                                       # USE22225809
	 if($flg == 0){                                          # USE22226024
           $status_no ="6";                                      # USE22226024
         }else{                                                  # USE22226024
           $status_no ="4";                                      # USE22226024
         }                                                       # USE22226024
     }                                                           # USE22225809
}                                                                # USE22225809
else{                                                            # USE22226024
    if($flg == 0){                                               # USE22226024
	$status_no=2;                                            # USE22226024
    }else{                                                       # USE22226024
	$status_no=0;                                            # USE22226024
    }
}
# USE22226024
require "sjsgetenv.pl";
$itemno = 7 ; 
$pre_env_http=&getenv($itemno);
#$config_change[ status_no ][ pre_env_http ]
#$index_change[ status_no ][ pre_env_http ]
#$env_change[ status_no ][ pre_env_http ]
$config_change[0][1]="";     $config_change[0][2]="";     $config_change[0][3]="";
#$config_change[1][1]="";    $config_change[1][2]="";     $config_change[1][3]="";
$config_change[2][1]=1;      $config_change[2][2]="";     $config_change[2][3]="";
#$config_change[3][1]=""     $config_change[3][2]="";     $config_change[3][3]="";
$config_change[4][1]="";     $config_change[4][2]="";     $config_change[4][3]=3;
$config_change[5][1]="";     $config_change[5][2]=2;      $config_change[5][3]="";
$config_change[6][1]="";     $config_change[6][2]=2;      $config_change[6][3]=3;
$config_change[7][1]="";     $config_change[7][2]=2;      $config_change[7][3]=3;

$index_change[0][1]="";      $index_change[0][2]=1;      $index_change[0][3]=1;
#$index_change[1][1]="";     $index_change[1][2]="";      $index_change[1][3]="";
$index_change[2][1]="";      $index_change[2][2]=1;      $index_change[2][3]=1;
#$index_change[3][1]="";     $index_change[3][2]="";      $index_change[3][3]="";
$index_change[4][1]="";      $index_change[4][2]="";      $index_change[4][3]=3;
$index_change[5][1]=3;       $index_change[5][2]="";      $index_change[5][3]=3;
$index_change[6][1]="";      $index_change[6][2]="";      $index_change[6][3]=3;
$index_change[7][1]=3;       $index_change[7][2]="";      $index_change[7][3]=3;

$env_change[0][1]="";        $env_change[0][2]=1;         $env_change[0][3]=1;
#$env_change[1][1]="";       $env_change[1][2]="";        $env_change[1][3]="";
$env_change[2][1]="";        $env_change[2][2]=1;         $env_change[2][3]=1;
#$env_change[3][1]="";       $env_change[3][2]="";        $env_change[3][3]="";
$env_change[4][1]=2;         $env_change[4][2]="";        $env_change[4][3]="";
$env_change[5][1]=3;         $env_change[5][2]="";        $env_change[5][3]="";
$env_change[6][1]=2;         $env_change[6][2]="";        $env_change[6][3]="";
$env_change[7][1]=3;         $env_change[7][2]="";        $env_change[7][3]="";

if($debug !=0){
print "status_no     = $status_no\n";
print "pre_env_http  = $pre_env_http\n";
print "env_http      = $env_http\n";
print "config_change = $config_change[$status_no][$pre_env_http]\n";
print "index_change  = $index_change[$status_no][$pre_env_http]\n";
print "env_change    = $env_change[$status_no][$pre_env_http]\n";
}
if($config_change[$status_no][$pre_env_http] == 3){
   $webserver_ext="_p80b";
}elsif($config_change[$status_no][$pre_env_http] == 2){
   $webserver_ext="";
}
	
#} USE22225681 Apache2.0
# if httpd.conf is new version                                   # USSE2223911
$rslt="The Apache service has restarted";                         # USE22226024
#if($flg==0){                                                     # USE22226024
    if($ssl_work == 1){
	#copy ssl config
# USE22225681 Apache2.0
#	$cp=`copy httpd.conf_ssl "$httpdconffile"`;
#	$rslt=`"$apache_home/Apache.exe" -w -k restart -n "Apache"` ;
	if($config_change[$status_no][$pre_env_http] ne ""){           # USE22226024
          $cp=`copy $httpdconf_ssl_in[$apache_v]$webserver_ext "$httpdconffile"`; # USE22225809
	  $rslt=`"$apache_pgm[$apache_v]/Apache.exe" -w -k restart -n "$apache_name[$apache_v]"` ;
        }                                                         # USE22226024
    }
    if($ssl_work ==0){
	#copy normalconfig
# USE22225681 Apache2.0
#	$cp=`copy httpd.conf "$httpdconffile"`;
#	$rslt=`"$apache_home/Apache.exe" -w -k restart -n "Apache"` ;
	if($config_change[$status_no][$pre_env_http] ne ""){           # USE22226024
	  $cp=`copy $httpdconf_in[$apache_v] "$httpdconffile"`;
	  $rslt=`"$apache_pgm[$apache_v]/Apache.exe" -w -k restart -n "$apache_name[$apache_v]"` ;
        }                                                        # USE22226024
    }
#}                                                               # USE22226024


require "sjschgenv.pl";                                          # USE22225809
$itemno = 7 ;                                                    # USE22225809
# USE22226024{
if ($env_change[$status_no][$pre_env_http] ne "" ){
  &chgenv($itemno,$env_change[$status_no][$pre_env_http]);
}
if ($index_change[$status_no][$pre_env_http] == 3 ){
    copy("$svphtmlextdir/$html_ssl", "$svphtmldir/$html_name");
}elsif ($index_change[$status_no][$pre_env_http] == 2 || $index_change[$status_no][$pre_env_http] == 1){
    copy("$svphtmlextdir/$html_nrml", "$svphtmldir/$html_name");
}
#}USE22226024
#if($debug != 0 || ($flg ==0)){
#    open ( LOGFILE, "> log/httpdconf.log");
#    print LOGFILE "File change=$flg:SSL=$ssl_work\nApache Status=\n$rslt\nCopy Status=\n$cp"; }
#close(LOGFILE);

#USUB2227421 For Apache observation START
$apacheState = "";
$returnValue1 = "";
$returnValue2 = "";

$returnValue2 = `sc query apache2`;
if($returnValue2 =~ /STATE/i){
	if ($returnValue2 =~ /RUNNING/i){
		$apacheState = "Apache2 RUNNING";
	}else{
		$apacheState = "Apache2 STOPPING  & restart Apache2";
		$rslt=`"$apache_pgm[$apache_v]/Apache.exe" -w -k restart -n "$apache_name[$apache_v]"` ;
	}
}else{
	$returnValue1 = `sc query apache`;
	if($returnValue1 =~ /STATE/i){
		if($returnValue1 =~ /RUNNING/i){
			$apacheState = "Apache1 RUNNING";
		}else{
			$apacheState = "Apache1 STOPPING & restart Apache1";
			$rslt=`"$apache_pgm[$apache_v]/Apache.exe" -w -k restart -n "$apache_name[$apache_v]"` ;
		}
	}else{
		$apacheState = "Apache1&2 not Installed";
	}
}
#USUB2227421 For Apache observation END
#U5002227273 For LOGOutput START
require 'logoutput.cgi';
$filename = "log/httpdconf.log";
$outputStrHeader = "*** httpdconfLog ***";
$outputStr = "File change=$flg,SSL=$ssl_work,Apache Status=$rslt,Copy Status=$cp,SSL_work=$ssl_work,Status_no=$status_no,Pre_env_http=$pre_env_http,ApacheStatus=$apacheState";#USUB2227421 For Apache observation

logoutput($filename,$outputStrHeader,$outputStr,$Lapline,$maxlength);
#U5002227273 For LOGOutput END

return $rslt;                                                    # USE22226024
}1;
