#!/bin/ksh

#--------------------------------------------------------------------------
# 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
#call secureLock to lock down the permissions for the installation
# -or-
#call secureUnlock to unlock the permissions for the installation
#--------------------------------------------------------------------------

PROGNAME="$(basename $0)"
PROGPATH="$(cd $(dirname $0); pwd)"

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

Use_report() {
    print -u2 "\n$PROGNAME [ -h install_dir ] [ -g common_group ] [-t type_code ] lock|unlock\n"
    exit 2
}

# get the list of groups for a specific user.
get_groups() {
  case $OSTYPE in
    AIX|Linux)
      print "$(print $(groups $1 | cut -d: -f2-))"
      ;;
    *)
      print "$(print $(groups $1))"
      ;;
  esac
return
}

###################################################################
#                         Main Routine                            #
###################################################################
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

OSTYPE="$(uname)"
export OSTYPE

GROUP=""
TYPE_LIST=""

while getopts :h:g:t: opt
do
  case $opt in
    h) CANDLEHOME=$OPTARG
       CANDLEHOME=$(eval print $CANDLEHOME)  # Process ~ if it was entered.
       ;;
    g) GROUP=$OPTARG
       ;;
    t) TYPE=$OPTARG
       TYPE_LIST="$TYPE_LIST $TYPE"
       ;;
    ?) Use_report
       ;;
  esac
done
shift $((OPTIND-1))

if [[ $# -lt 1 ]]
then
   Use_report
fi

action="$1"

if [[ "$action" != @(lock|unlock) ]]
then
  Use_report
fi

#set and validate the installation directory
[[ -n "$CANDLEHOME" ]] || CANDLEHOME=$(cd $(dirname $0) ; dirname $(pwd) )

if [[ ! -d "$CANDLEHOME" ]]
then
   #$NLS-E$ "Cannot set CANDLEHOME environment variable"
   Err_report fail "`nls_replace 'KCI0523E' CANDLEHOME`"
fi

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

if [[ ! (-d "$CANDLEHOME/bin" && -d "$CANDLEHOME/config" && 
      -d "$CANDLEHOME/logs" && -d "$CANDLEHOME/registry") ]]
then
   #$NLS-E$ "$CANDLEHOME is not a valid installation"	  
   Err_report fail "`nls_replace 'KCI0525E' $CANDLEHOME`"
fi

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

#define the file containing the security mode setting
SecureFile=$CANDLEHOME/registry/SecureMode
export SecureFile

#set and validate the user ID of the installation owner
owner=$(ls -do logs registry tables config | awk '{print $3}' | sort -u)
if [[ $(print $(print $owner | wc -w)) != 1 ]]
then
   #$NLS-E$ "Cannot determine Installation Owner ID \n Multiple User IDs found on key directories \n User IDs: $(print $(print $owner))"
   Err_report fail "`nls_replace 'KCI0526E'` \n `nls_replace 'KCI0527I'` \n `nls_replace 'KCI0528I'` $(print $(print $owner))"
fi

#export the groups for the owner id
Groups=$(get_groups $owner)
export Groups

#validate the common group, if specified,
# against the user ID of the installation owner
if [[ -n "$GROUP" ]]
then
   if [[ "$GROUP" = @(lock|unlock) ]]
   then
      #$NLS-E$ "Group specified can not be \"lock\" or \"unlock\""
      Err_report fail "`nls_replace 'KCI2520E' lock unlock`"
   fi
   foundGroup=$(print $Groups | grep -w $GROUP 2> /dev/null)
   if [[ -n "$foundGroup" ]]
   then
      COMMONGROUP="$GROUP"
   else
      myID=$(id | cut -d"(" -f2 | cut -d")" -f1)
      if [[ "$myID" = root ]]
      then
         testFile=/tmp/secureTest.$$
         touch "$testFile" > /dev/null 2>&1
         chgrp "$GROUP" "$testFile" > /dev/null 2>&1
         rcGroup=$?
         rm -f "$testFile" > /dev/null 2>&1
         if [[ "$rcGroup" = 0 ]]
         then
            COMMONGROUP="$GROUP"
         else
            #$NLS-E$ "The specified group $GROUP is not a valid group"
            Err_report fail "`nls_replace 'KCI2521E' $GROUP`"
         fi
      else
         #$NLS-E$ "Installation Owner ID $owner \n must be a member of the specified group $GROUP"
         Err_report fail "`nls_replace 'KCI2522E' $owner` \n `nls_replace 'KCI2523E' $GROUP`"
      fi
   fi
else
   COMMONGROUP="@none@"
fi

# capture the applications from the product description table
all_apps=$(cat $CANDLEHOME/registry/proddsc.tbl | cut -d'|' -f1 |
           grep -v '#'| grep -v '*' | grep -v '...+' | grep -v '^ *$' | sort -u)

# capture the installed applications from the registry
installed_apps=$(print $(cd $CANDLEHOME/registry; ls *.ver |
                 grep -v 'tp[s|d|w]' | grep -v tms | grep -v env |
                 cut -c1-2 | sort -u))
export installed_apps

#validate the application code(s), if specified,
# against the product description table
# and against the installed products
if [[ -n "$TYPE_LIST" ]]
then
   APPCODES="@"
   for APPCODE in $TYPE_LIST   
   do
      if [[ "$APPCODE" = @(lock|unlock) ]]
      then
         #$NLS-E$ "Application type specified can not be \"lock\" or \"unlock\""
         Err_report fail "`nls_replace 'KCI2524E' lock unlock`"
      fi
      if [[ $(print -n "$APPCODE" | wc -c) -ne 2 ]]
      then
         #$NLS-E$ "Application type $APPCODE must be only two characreters"
         Err_report fail "`nls_replace 'KCI2525E' $APPCODE`"
      fi
      foundApp=$(print "$all_apps" | grep "$APPCODE" 2> /dev/null)
      if [[ -z "$foundApp" ]]
      then
         #$NLS-E$ "Application type $APPCODE is not valid"
         Err_report fail "`nls_replace 'KCI2526E' $APPCODE`"
      else
         APPCODES="$APPCODES$APPCODE@"
      fi
      foundApp=$(print "$installed_apps" | grep "$APPCODE" 2> /dev/null)
      if [[ -z "$foundApp" ]]
      then
         #$NLS-E$ "Application type $APPCODE is not installed"
         Err_report fail "`nls_replace 'KCI2527E' $APPCODE`"
      fi
   done
else
   APPCODES="@none@"
fi

case "$action" in
   lock) 
      #lock down the global permissions for the installation directory tree
      #$NLS-E$ "Enter the root password if prompted"
      nls_replace 'KCI0529I'
      su root -c "$PROGPATH/secureLock $owner $COMMONGROUP $APPCODES" 
   ;;
   unlock) 
      #unlock the permissions for the installation directory tree
      #$NLS-E$ "Enter the root password if propmted"
      nls_replace 'KCI0529I'      
      su root -c "$PROGPATH/secureUnlock $owner $COMMONGROUP $APPCODES" 
   ;;
esac

