#!/bin/ksh

#--------------------------------------------------------------------------
# This script must be run from the secureMain script
# and it must run as root
#--------------------------------------------------------------------------

#--------------------------------------------------------------------------
# Licensed Materials - Property of IBM
# 5724-C04
#
# (C) Copyright IBM Corporation 2006. All Rights Reserved.
# US Government Users Restricted Rights - Use, duplication
# or disclosure restricted by GSA ADP Schedule Contract
# with IBM Corp.
#--------------------------------------------------------------------------
#
# Description:
#
#set and validate the user ID of the installation owner
#set and validate the installation directory
#if the owner is root, force all files to be owned by root
#lock down the global permissions for the installation directory tree
#processess application specific permissions due to multi-user run requirements.
#reset the special permissions required for certain products.
#--------------------------------------------------------------------------


#--------------------------------------------------------------------------
# Do NOT use chmod -R, instead use the subroutine Rchmod
# This subroutine uses the secureChmodR script to handle 
# each file separately and mess with the 'x' bit 
chmod_list() {
permission=$1
if [[ "$permission" = "-R" ]]
then
   recurse=$permission
   shift
   permission=$1
else
   recurse=""
fi
shift
dir_list="$*"
for dir in $dir_list
do
   if [[ -d "$dir" ]]
   then
      if [[ -n "$recurse" ]]
      then
         find $dir -exec $CANDLEHOME/bin/secureChmod "$permission" {} \; 
      else
         $CANDLEHOME/bin/secureChmod "$permission" $dir
      fi
   fi
   if [[ -f "$dir" ]]
   then
      $CANDLEHOME/bin/secureChmod "$permission" $dir
   fi
done
}
#--------------------------------------------------------------------------

if [[ $# != 3 ]]
then
   #$NLS-E$ "Parameters missing, exiting"
   nls_replace 'KCI0515E' 
   exit
fi

#gather the calling parms
owner=$1
commongroup=$2
appcodes=$3

#validate the user ID of the installation owner
if [[ -z "$owner" ]]
then
   #$NLS-E$ "Installation Owner ID not supplied, exiting"
   nls_replace 'KCI0510E' 
   exit
fi

#validate the common group specified
if [[ -z "$commongroup" ]]
then
   #$NLS-E$ "Common Group not supplied, exiting"
   nls_replace 'KCI0512E' 
   exit
fi

#validate the application list
if [[ -z "$appcodes" ]]
then
   #$NLS-E$ "Application list not supplied, exiting"
   nls_replace 'KCI0513E' 
   exit
fi

#set and validate the installation directory
[[ -z "$CANDLEHOME" ]] && CANDLEHOME=$(cd $(dirname $0) ; dirname $(pwd) )
if [[ ! (-d "$CANDLEHOME/bin" && -d "$CANDLEHOME/config" && 
      -d "$CANDLEHOME/logs" && -d "$CANDLEHOME/registry") ]]
then
   #$NLS-E$ "$CANDLEHOME is not a valid installation, exiting"	  
   nls_replace 'KCI0511E' $CANDLEHOME
   exit
fi

#set and validate the list of platform directories in the installation directory
platform_dirs=$(print $(ls -d aix* hp* sol* l* 2> /dev/null | grep -v licenses | grep -v logs))
if [[ -z "$platform_dirs" ]]
then
   #$NLS-E$ "No platform directories were found, exiting"
   nls_replace 'KCI0514E' 
   exit
fi   

#change directory to the installation directory
cd "$CANDLEHOME"

user=$owner

if [[ "$commongroup" = "@none@" ]]
then
   if [[ "$user" = root && "$OSTYPE" = SunOS ]]
   then
      group="other"
   else
      group=$(print "$Groups" | cut -d' ' -f1)
   fi
else
   group=$commongroup
fi

usergroup="$user:$group"

secureMode=""
if [[ -r "$SecureFile" ]]
then
   secureMode=$(cat $SecureFile)
fi
if [[ -z "$secureMode" ]]
then
   secureMode="unlock:$usergroup"
fi

oldMode=$(print "$secureMode" | cut -d: -f1)
oldUser=$(print "$secureMode" | cut -d: -f2)
oldGroup=$(print "$secureMode" | cut -d: -f3)
lockForce=""
if [[ "$oldUser" != "$user" || "$oldGroup" != "$group" ]]
then
   lockForce="yes"
fi

# Always run the baseSecureLock routine
if [[ -r "$CANDLEHOME/bin/baseSecure.shl" ]]
then
   . "$CANDLEHOME/bin/baseSecure.shl"
   print == baseSecureLock
   baseSecureLock
fi


#skip application codes that require no action because they are included
# as prereqs with other applications or the base process.
skipcodes=""
if [[ -r "$CANDLEHOME/bin/skipSecure.shl" ]]
then
   . "$CANDLEHOME/bin/skipSecure.shl"
fi

for app in $installed_apps
do
   skipApp=$(print "$skipcodes" | grep $app 2> /dev/null)
   if [[ -n "$skipApp" ]] 
   then
      print == SecureSkip ${app}
   else
      foundApp=$(print "$appcodes" | grep $app 2> /dev/null)
      if [[ "$lockForce" = yes || "$appcodes" = "@none@" || -n "$foundApp" ]] 
      then
         if [[ -r "$CANDLEHOME/bin/${app}Secure.shl" ]]
         then
            . "$CANDLEHOME/bin/${app}Secure.shl"
            print == ${app}SecureLock
            ${app}SecureLock
         elif [[ -r "$CANDLEHOME/bin/xxSecure.shl" ]]
         then
            . "$CANDLEHOME/bin/xxSecure.shl"
            print == xxSecureLock ${app}
            xxSecureLock
         fi
      fi
   fi
done

secureMode="lock:$usergroup"
print "$secureMode" > $SecureFile

