#!/bin/ksh
###################################################################
#             (C) COPYRIGHT Candle Corporation 2000               #
#                      All Rights Reserved                        #
#                                                                 #
#       Licensed Materials - Property of Candle Corporation       #
#         Duplication or disclosure prohibited without the        #
#        expressed written consent of the Candle Corporation.     #
#                                                                 #
###################################################################

# @(#) CheckForRoot - checks for root-ownership of certain files.

# Note the meaning of the following exit codes:
#   0 = program ran successfully, and there are no root-owned files.
#   1 = program ran successfully, and there are root-owned files.
#   2 = program failure.

Err_report()
{
  case $1 in
    #$NLS-E$ "$PROGNAME warning:  $2, continuing ..."
    warn) print -u2  "$PROGNAME `nls_replace 'KCI0535I'`:  $2, `nls_replace 'KCI0536I'` ..."
          sleep 2
          return
          ;;
	#$NLS-E$ "$PROGNAME failure:  $2."	  
    fail) print -u2 "$PROGNAME `nls_replace 'KCI0537I'`:  $2."
          exit 2
          ;;
    info) print -u2 "$PROGNAME        :  $2."
          return
          ;;
  esac
}

Use_report()
{
  print -u2 "$PROGNAME [ -h CANDLEHOME ]"
  exit 0
}

# Get the names of all installed products requiring special permissions.

getVFNames()
{
  typeset -i i=0
  typeset arch name pc verName
  
  ls $CANDLEHOME/registry/*.ver > /dev/null 2>&1
  [ $? -eq 0 ] || return  # Nothing to process.

  for name in $(ls $CANDLEHOME/registry/*.ver)
  do
    verName=${name##*/}  # Remove the pathname.
    pc=$(print $verName | cut -c1-2 2> /dev/null)   # Get the product code.
    [[ $pc = @(cz|ms|or|ox|oy|ul|um|ux) ]] || continue   # Only a few products need special permissions.
  
    arch=$(print $verName | cut -c3- 2> /dev/null)  # Determine the bin arch.
    [[ $arch = @(aix|hp|sol)* ]] || continue  # Skip CMS data pieces.

    vfNames[i]=$verName
    i=i+1
  done
}

# Get the owner and group names for a file.

getOwnGrp()
{ # $1 = full pathname of the file to check.

  typeset -i numEle=0
  fileOwn="" fileGrp=""
  
  set -A lsInfo $(ls -l $1 2> /dev/null | awk '{print $3" "$4}') 
  numEle=${#lsInfo[*]}
  [ numEle -eq 2 ] || exit 2  # Error. 
  
  fileOwn=${lsInfo[0]}
  fileGrp=${lsInfo[1]}
}

# Determine the arch of the common database code (dd).

setDDArch()
{
  # $1 = the name of the product's .ver file

  typeset vFile=$CANDLEHOME/registry/$1
  [ -r $vFile ] || return

  ddArch=$(grep " *preReqALL *= *dd" $vFile | cut -d "=" -f2 | cut -d "|" -f1)
  if [ -n "$ddArch" ]
  then
    ddArch=$(print $ddArch | cut -c3-)
    [ -n "$ddArch" ] && return
  fi

  # Try just plain "preReq".

  ddArch=$(grep " *preReq *= *dd" $vFile | cut -d "=" -f2 | cut -d "|" -f1)
  if [ -n "$ddArch" ]
  then
    ddArch=$(print $ddArch | cut -c3-)
    [ -n "$ddArch" ] && return
  fi

  # If no "dd" prereq could be found, use the bin arch of the product.

  ddArch=${1%.ver}  # Remove the extension.
  ddArch=$(print $ddArch | cut -c3-)  # Get the architecture.
}

# Check Command Pro (cz).

checkCZ()
{
  # $1 is the name of the product's .ver file.
  
  # Define local variables.
  typeset binArch binDir files name 

  files="kumsnetd"

  binArch=${1%.ver}  # Remove the extension.
  binArch=$(print $binArch | cut -c3-)  # Get the architecture.

  if [[ "$binArch" = "ccp" ]]
  then
   return
  fi

  binDir=$CANDLEHOME/$binArch/cz/bin
  for name in $files
  do
    [ -r $binDir/$name ] || continue
    getOwnGrp $binDir/$name
    if [ "$fileOwn" = "root" ]
    then
      hasRoot=true
      return
    fi
  done
}

# Check database agents (or, ox and oy).

checkDB()
{ # $1 is the name of the product's .ver file.
  
  # Define local variables.
  typeset kddos

  setDDArch $1

  kddos=$CANDLEHOME/$ddArch/bin/kddos
  [ -r $kddos ] || return
  getOwnGrp $kddos
  if [ "$fileOwn" = "root" ]
  then
    hasRoot=true
  fi
}

# Check the CMS.

checkMS()
{ # $1 is the name of the product's .ver file.
  
  # Define local variables.
  typeset binArch kds

  binArch=${1%.ver}  # Remove the extension.
  binArch=$(print $binArch | cut -c3-)  # Get the architecture.
  
  kds=$CANDLEHOME/$binArch/ms/bin/kdsvlunx
  [ -r $kds ] || return
  getOwnGrp $kds
  if [ "$fileOwn" = "root" ]
  then
    hasRoot=true
  fi
}

# Check UNIX Logs Monitoring Agent (ul).

checkUL()
{ # $1 is the name of the product's .ver file.
  
  # Define local variables.
  typeset binArch kul

  binArch=${1%.ver}  # Remove the extension.
  binArch=$(print $binArch | cut -c3-)  # Get the architecture.

  kul=$CANDLEHOME/$binArch/ul/bin/kulagent
  [ -r $kul ] || return
  getOwnGrp $kul
  if [ "$fileOwn" = "root" ]
  then
    hasRoot=true
  fi
}

#  Check Universal Agent (um).

checkUM()
{
  # $1 is the name of the product's .ver file.
  
  # Define local variables.
  typeset binArch binDir files name  

  files="kumsnetd kumstrap"

  binArch=${1%.ver}  # Remove the extension.
  binArch=$(print $binArch | cut -c3-)  # Get the architecture.

  binDir=$CANDLEHOME/$binArch/um/bin
  for name in $files
  do
    [ -r $binDir/$name ] || continue
    getOwnGrp $binDir/$name
    if [ "$fileOwn" = "root" ]
    then
      hasRoot=true
      return
    fi
  done
}

# Check UNIX OS Monitoring Agent (ux)

checkUX()
{
  # $1 is the name of the product's .ver file.
  
  # Define local variables.
  typeset binArch binDir name 

  binArch=${1%.ver}  # Remove the extension.
  binArch=$(print $binArch | cut -c3-)  # Get the architecture.
  binDir=$CANDLEHOME/$binArch/ux/bin

  if [[ "$binArch" = aix+([0-9]) ]]  # Retained for when we were changing the owner of stat_daemon to root.
  then
    name=$binDir/stat_daemon
    [ -r $name ] || return
    getOwnGrp $name
    if [ "$fileOwn" = "root" ]
    then
      hasRoot=true
      return
    fi
  fi
  
  if [[ "$binArch" = sol+([0-9]) ]]
  then
    name=$binDir/kuxagent
    [ -r $name ] || return
    getOwnGrp $name
    if [ "$fileOwn" = "root" ]
    then
      hasRoot=true
    fi
  fi
}


###################################################################
#                          Main                                   #
###################################################################
bindir=`pwd`/$(dirname $0)
[ "$(print $0 | grep '\./')" = "" ] && [ "$(print $0 | grep '^/.*')" = "" ] && [ `pwd` = '/' ] && bindir=`pwd`$(dirname $0)
[ "$(print $0 | grep '^/.*')" != "" ] && bindir=$(dirname $0)

export TEXTDOMAINDIR=$bindir/../config/nls/msg
export PATH=$bindir:$PATH

export PATH=/usr/bin:$PATH
PROGNAME=$(basename $0)

typeset ddArch hasRoot=false

while getopts :h: opt
do case $opt in
  h) CANDLEHOME=$OPTARG
     CANDLEHOME=$(eval print $CANDLEHOME)  # Process ~ if it was entered.
     ;;
  ?) Use_report
     ;;
  :) Use_report
     ;;
  esac
done
shift OPTIND-1
[ $# -eq 0 ] || Use_report 

#$NLS-E$ "CANDLEHOME directory not set or not specified"
[ -n "$CANDLEHOME" ] || Err_report fail "`nls_replace 'KCI0680E' CANDLEHOME`"
#$NLS-E$ "\"$CANDLEHOME\" is not a directory"
[ -d "$CANDLEHOME" ] || Err_report fail "`nls_replace 'KCI0681E' \\"$CANDLEHOME\\"`"

if [ $(print "$CANDLEHOME" | wc -w) -ne 1 ]
then
  #$NLS-E$ "Blanks are not permitted in CANDLEHOME"
  Err_report fail "`nls_replace 'KCI0682E' CANDLEHOME`"
fi

cd $CANDLEHOME > /dev/null 2>&1
#$NLS-E$ "could not cd to \"$CANDLEHOME\" directory"
[ $? -eq 0 ] || Err_report fail "`nls_replace 'KCI0683E' \\"$CANDLEHOME\\"`"
export CANDLEHOME

regDir=$CANDLEHOME/registry
#$NLS-E$ "\"$regDir\" is not a directory"
[ -d $regDir ] || Err_report fail "`nls_replace 'KCI0684E' \\"$regDir\\"`"

# Get the names of the .ver files for products that require special permissions.

set -A vfNames
getVFNames  # getVFNames() will fill the vfNames array.

# See if there are any root-owned files.

for name in ${vfNames[*]}
do
  pCode=$(print $name | cut -c1,2)  # Get the 2-character product code.
  case $pCode in
    cz)
      checkCZ $name
      ;;
    ms)
      checkMS $name
      ;;
    or|ox|oy)
      checkDB $name
      ;;
    ul)
      checkUL $name
      ;;
    um)
      checkUM $name
      ;;
    ux)
      checkUX $name
      ;;
  esac
  [ "$hasRoot" = "true" ] && exit 1  # UnSetRoot needs to be run.
done

exit 0
