#!/usr/local/bin/perl
# 
# $Header: dbInitParamFilesPerm.pl 12-jan-2007.12:11:46 manosing Exp $
#
# dbInitParamFilesPerm.pl
# 
# Copyright (c) 2004, 2006, Oracle. All rights reserved.  
#
#    NAME
#      dbInitParamFilesPerm.pl - <one-line expansion of the name>
#
#    DESCRIPTION
#      <short description of component this file declares/defines>
#
#    NOTES
#      <other useful comments, qualifications, etc.>
#
#    MODIFIED   (MM/DD/YY)
#    manosing    01/12/07 - XbranchMerge manosing_lastpols from main
#    manosing    12/28/06 - flood control for windows policy
#    manosing    12/21/06 - adding check for  "0()" 
#    dsukhwal    12/08/05 - add windows default init.ora location 
#    dsukhwal    05/04/05 - handle win32 cases 
#    dsukhwal    02/15/05 - exceptional cases in file_perm usage 
#    dkjain      12/28/04 - Removed check_permission 
#    dkjain      12/10/04 - Fixed Bug-4033921 
#    dkjain      10/31/04 - Fixed unable to connect
#    dkjain      10/08/04 - dkjain_esa_impl_init
#    dkjain      10/08/04 - Creation
# 
  require "emd_common.pl";
  require "semd_common.pl";
  require "$ENV{EMDROOT}/sysman/admin/scripts/db/esaDbUtils.pl";
  require "$ENV{EMDROOT}/sysman/admin/scripts/db/esaUtils.pl";

  my %stdinArgs = get_stdinvars();
  my $username = $stdinArgs{"EM_TARGET_USERNAME"};
  my $password = $stdinArgs{"EM_TARGET_PASSWORD"};
  my $oracleHome = $ENV{EM_TARGET_ORACLE_HOME};
  my $dbSid = $ENV{DB_SID};
  my $address = $ENV{EM_TARGET_ADDRESS};
  my $role = $ENV{EM_TARGET_ROLE};
  my $mode = 0;
  my $spMax;
  my $inMax;

  if($^O eq "MSWin32"){
    $spMax = $ENV{'NSPFILEMAX'};
    $inMax = $ENV{'NINITORAMAX'};
  }else{
    $spMax = $ENV{'SPFILEMAX'};
    $inMax = $ENV{'INITORAMAX'};
  }
  if($role =~ /SYSDBA/i)
  {
    $mode = 2;
  }
  elsif($role =~ /SYSOPER/i)
  {
    $mode = 4;
  }
  my $dsn = "dbi:Oracle:" ;
  my $property1 ="oh_initora_perm"; #the properties are nt_$property for mswin32.
  my $property2 ="oh_spfileora_perm";

  my $table = "v\$parameter";  
  my $value = "value";
  my $name = "name"; 
  my $fp1 ; 
  my $dbh = open_db_connection("dbi:Oracle:", "$username@".$address,$password,$mode); 
  #my @fp1 = getValue($dbh,$value,$name, "pfile",$table);
  #collect the perm for init from the default dir $OH/dbs

  my @fp2 = getValue($dbh,$value,$name, "spfile",$table);

  
  if($^O =~ "MSWin32") {
    $table = "v\$version" ;
    my $sth = $dbh->prepare_cached("SELECT banner FROM $table where banner like '%Oracle%' ")
          or die print "em_error=Couldn't prepare statement: $dbh->errstr" ;
    $sth->execute()
          or die print "em_error=Couldn't execute statement: $sth->errstr" ;
    my @edition = $sth->fetchrow_array();
    if( ($edition[0] =~ m/10g/) && !(($edition[0] =~ m/10\.1/)) ){#should match 10gR2 and above
                                                     #will need modification after rdbms version 11
        $fp1 = "$oracleHome\\database\\init$dbSid.ora";
    }
    else{
        my @db_name = getValue($dbh, "value","name", "db_name", 'v$parameter');
        $fp1 = "$oracleHome\\admin\\".$db_name[0]."\\pfile\\init$dbSid.ora";
    } 
  }
  else {
   $fp1 = "$oracleHome/dbs/init$dbSid.ora"; #default location and default name for unixes
  }
  #test();
  close_db_connection($dbh);
  
  collect_init_files_perm($fp1,$property1,$inMax); #Output string for P1 init.ora
  collect_init_files_perm($fp2[0],$property2, $spMax); #Output string for P2 spfile.ora

  sub test{
    $table = "v\$version" ;
    my $sth = $dbh->prepare_cached("SELECT banner FROM $table where banner like '%Oracle%' ")
          or die print "em_error=Couldn't prepare statement: $dbh->errstr" ;
    $sth->execute()
          or die print "em_error=Couldn't execute statement: $sth->errstr" ;
    my @edition = $sth->fetchrow_array();
    my @db_name = getValue($dbh, "value","name", "db_name", 'v$parameter');
    my $fp1 = "$oracleHome\\admin\\".$db_name[0]."\\pfile\\init$dbSid.ora";
    print "em_result=10.1 and below|$fp1\n";
    $fp1 = "$oracleHome\\database\\init$dbSid.ora";
    print "em_result=10.2 and above|$fp1\n";
  }
    

  sub collect_init_files_perm{
    my ($fpath,$property,$maxCount) = @_;
    if($maxCount == 0){#only one row each is to be printed for init.ora and spfile
        return;
    }
	if($^O eq "MSWin32"){
        my $users = win32_file_perm($fpath);
        if($users == -1 || $users eq "0()"){
            return;#failed to get the file permissions
        }
        print "em_result=nt_$property|$users|$fpath\n";
    }
    else{
        my $retMode =  file_perm($fpath);
        if($retMode < 0)
        { return ; } 
    
        $retMode = $retMode & 0777 ; 
        if(($retMode & 006)){
          #$permString = rwx_string($retMode);  
          $fpath = check_512char($fpath);
          printf "em_result=$property|%03o|$fpath\n",$retMode ;  
        }
    }
  }
