#!/bin/sh
# @(#) $Id: //bas/722_STACK/src/include/saproot.sh#2 $ SAP
#*********************************************************************
#                                                                    *
#     SAP AG Walldorf                                                *
#     Systeme, Anwendungen und Produkte in der Datenverarbeitung     *
#                                                                    *
#     (C) Copyright SAP AG,  1994                                    *
#                                                                    *
#     saproot.sh                                                     *
#                                                                    *
#     run some commands that have to be done as root                 *
#                                                                    *
#                                                                    *
#     Usage:                                                         *
#       saproot.sh <SID>                                             *
#       saproot.sh C11                                               *
#                                                                    *
#====================================================================*
# History:                                                           *
#          24.08.94 Created for saposcol, hjr                        *
#          02.12.94 Changed for INFORMIX  nes                        *
#          01.02.96 DB2/AIX Changed for DB2 for AIX                  *
#          02.04.96 DB2/AIX Changed for usage of CAR;                *
#                   and bind all packes to database                  *
#          07.05.96 DB2/AIX find CAR in /usr/sap/put/exe;            *
#          06.08.96 DB2/AIX erase aclput procedure tro               *
#          02.09.96 DB2/AIX adapt to changes from Heidi/Frank        *
#          08.10.96 DB2/AIX call -a option with -mg (Message path)   *
#          12.11.96 DB2/AIX change bind call                         *
#          04.12.96 DB2/AIX saproot.sh runnable from .../exe/run     *
#                           to support simple Kernel exchanges       *
#                           without upgrade                          *
#          07.02.97 Changed for INFORMIX FILELST2 and added if block *
#                   to ensure block only executes on Informix system *
#          25.06.97 DB2 common server (>=3.1H):                      *
#                   exatract DBATOOLS (until 31G done by R3INST)     *
#                   (FHW)                                            *
#          10.07.97 undo changes of 25.6., since DIINST(=R3INST)     *
#                   can do the job of DBATOOLS-extraction :-)        *
#          16.07.97 stored procedures (extraction/bind) not          *
#                   neccessary from 31H on -> removed (FHW)          *
#          20.01.98 DB6: remove backint (FHW)                        *
#          23.01.98 DB6: cp, chown & chmod of sddb6das & db2uext2    *
#                   (FHW)                                            *
#          06.07.99 DB6: 46A adaptions (new groups) FHW              *
#                        remove db2lib.a, chgrp/mod global ...       *
#          08.07.99 Korections for upgrade-directory other than      *
#                   /usr/sap/put                                     *
#          14.07.99 DB6: su db2<sapsid> -> update dbm cfg            *
#          02.08.99 DB6 only: PATH=$PATH:/usr/ucb (FHW)              *
#                   /bin/sh -> /bin/ksh                              *
#          01.12.99 DB6: 46B adaptions (FHW)                         *
#          25.01.00 DB2: adapated for OS/390 (no root user) (>=4.6C) *
#          05.09.00 DB6: fixes for SUN/Solaris (FHW)                 *
#          01.08.01 DB6: adaption of 610 changes (FHW)               *
#          23.07.02 DB6: fix for AIX5 (update dbmcfg)                *
#          04.10.02 DB6: fix for last fix (specify shell for update) *
#          17.01.03 DB6: chmod 4755 db6util -> chmod 755 db6util     *
#          06.06.03 OL: added chown/chmod for icmbnd                 *
#          24.11.03 DB6: modifications                               *
#          02.03.04 ORA: remove sapdba                               *
#          06.04.05 DB6: add 'update dbmcfg -> SYSMAINT_GROUP'       *
#          08.04.05 DB6: add 'revoke <5 priveleges> from PUBLIC'     *
#          04.04.11 SYB: add chown+chmod for sybctrl for Sybase ASE  *
#          15.11.11 ---: add chown+chmod for sapuxuserchk            *
#          30.01.12 ---: improvement for sapuxuserchk                *
#          07.11.13 ---: remove "other" from sapuxuserchk's ACL      *
#*********************************************************************

# AIX wants this for tr
LANG=C
EXE="`pwd`/saposcol"
ICMBND_NEW="`pwd`/icmbnd.new"
ICMBND="`pwd`/icmbnd"

if [ $# -ne 1 -a $# -ne 2 ];then
    echo
    echo "Bad number of arguments!"
    echo
    echo "Call $0 <SID> [<DBSID>]"
    echo "  e.g $0 C11"
    echo "  <DBSID> must be specified only on MCOD systems, if it differs from <SID>"
    echo
    exit 1
fi

SID=`echo $1 | tr "[a-z]" "[A-Z]"`
SIDL=`echo $1 | tr "[A-Z]" "[a-z]"`

DBSID=$SID
if [ -n "$2" ]; then
    DBSID=`echo $2 | tr "[a-z]" "[A-Z]"`
fi
DBSIDL=`echo $DBSID | tr "[A-Z]" "[a-z]"`


case `uname` in
  OS/390* | z/OS*)
#       there is no user 'root' on OS/390
	if [ `id -u` != 0 ];then
	    echo
	    echo "You have to run this script with uid 0!"
	    echo
	    exit 1
	fi
    ;;
   *)
	if [ `whoami` != root ];then
	    echo
	    echo "You have to be root to run this script !"
	    echo
	    exit 1
	fi
    ;;
esac


if [ ! -d /usr/sap/${SID} ]; then
    echo
    echo "There's no SAP-System $SID installed ! "
    echo
    exit 1
    fi

#-------------------------------------------------------------------
# ORACLE spezific: If the files are there, chown chmod of the files

# process all platforms supported by SAP on Oracle
PLATLST1="hp_64 hpia64 linuxia64 linuxintel linuxppc64 linuxx86_64 rs6000_64 sun_64 sunx86_64"
for p in $PLATLST1
do
# If file brbackup exists, system must be running Oracle. Execute block.
  if [ -f /usr/sap/${SID}/SYS/exe/nuc/$p/brbackup ]; then
    FILELST1="brbackup brarchive brconnect"
    for f in $FILELST1
    do
       if [ -f /usr/sap/${SID}/SYS/exe/nuc/$p/$f ]; then
	  echo "Preparing /usr/sap/${SID}/SYS/exe/nuc/$p/$f ..."
	  chown ora${DBSIDL} /usr/sap/${SID}/SYS/exe/nuc/$p/$f
	  if [ $? -ne 0 ];then
	     echo "   Error occurred by chown file /usr/sap/${SID}/SYS/exe/nuc/$p/$f!"
	     echo "   Please check what's the matter"
	  fi
	  chmod 4774 /usr/sap/${SID}/SYS/exe/nuc/$p/$f
	  if [ $? -ne 0 ];then
	     echo "   Error occurred by chmod file /usr/sap/${SID}/SYS/exe/nuc/$p/$f!"
	     echo "   Please check what's the matter"
	  fi
       fi
    done
  fi
  if [ -f /usr/sap/${SID}/SYS/exe/uc/$p/brbackup ]; then
    FILELST1="brbackup brarchive brconnect"
    for f in $FILELST1
    do
       if [ -f /usr/sap/${SID}/SYS/exe/uc/$p/$f ]; then
	  echo "Preparing /usr/sap/${SID}/SYS/exe/uc/$p/$f ..."
	  chown ora${DBSIDL} /usr/sap/${SID}/SYS/exe/uc/$p/$f
	  if [ $? -ne 0 ];then
	     echo "   Error occurred by chown file /usr/sap/${SID}/SYS/exe/uc/$p/$f!"
	     echo "   Please check what's the matter"
	  fi
	  chmod 4774 /usr/sap/${SID}/SYS/exe/uc/$p/$f
	  if [ $? -ne 0 ];then
	     echo "   Error occurred by chmod file /usr/sap/${SID}/SYS/exe/uc/$p/$f!"
	     echo "   Please check what's the matter"
	  fi
       fi
    done
  fi
done
if [ -f /usr/sap/${SID}/SYS/exe/run/brbackup ]; then
  FILELST1="brbackup brarchive brconnect"
  for f in $FILELST1
  do
     if [ -f /usr/sap/${SID}/SYS/exe/run/$f ]; then
	echo "Preparing /usr/sap/${SID}/SYS/exe/run/$f ..."
	chown ora${DBSIDL} /usr/sap/${SID}/SYS/exe/run/$f
	if [ $? -ne 0 ]; then
	   echo "   Error occurred by chown file /usr/sap/${SID}/SYS/exe/run/$f!"
	   echo "   Please check what's the matter"
	fi
	chmod 4774 /usr/sap/${SID}/SYS/exe/run/$f
	if [ $? -ne 0 ]; then
	   echo "   Error occurred by chmod file /usr/sap/${SID}/SYS/exe/run/$f!"
	   echo "   Please check what's the matter"
	fi
     fi
  done
fi
echo

# ORACLE End
#-------------------------------------------------------------------


#-------------------------------------------------------------------
#
# ICMBND START

echo "Preparing icmbnd ..."
echo

if [ -f $ICMBND_NEW ]
then
    cp -f $ICMBND_NEW $ICMBND

    case `uname` in
    OS/390* | z/OS*)
#       there is no user 'root' on OS/390
	chown `whoami`:sapsys $ICMBND
	if [ $? -ne 0 ];then
	    echo "Error occurred !"
	    exit 1
	fi
	;;
    *)
	chown root:sapsys $ICMBND
	if [ $? -ne 0 ];then
	    echo "Error occurred !"
	    exit 1
	fi
	;;
    esac
    chmod 4750 $ICMBND
    if [ $? -ne 0 ];then
	echo "Error occurred !"
	exit 1
    fi
else
    echo "icmbnd.new does not exist - skipped"
fi

#
# ICMBND END
#-------------------------------------------------------------------

#-------------------------------------------------------------------
#
# use find because don't follow symlinks.
# 
# SAPUXUSERCHK START
for file in `find /usr/sap/$SID -name "sapuxuserchk" 2> /dev/null`
do 
    echo "Set user ID bit on $file"
    chown root:sapsys $file
    if [ $? -ne 0 ];then
	echo "Unable to change owner of $file"
    else
        chmod u+s,o-rwx $file
        if [ $? -ne 0 ];then
            echo "Unable to change mode of $file"
        fi
    fi
done

#-------------------------------------------------------------------
#
# SAPUXUSERCHK END

echo "done"
exit 0

