#!/bin/sh
#* ----------------------------------------------------------------------
#*               Proprietary and Confidential Information
#*
#* This material contains, and is part of a computer software program
#* which is, proprietary and confidential information owned by COMPUTER
#* ASSOCIATES INTERNATIONAL, INC.  The program, including this material,
#* may not be duplicated, disclosed or reproduced in whole or in part for
#* any purpose without the express written authorization of COMPUTER
#* ASSOCIATES. All authorized reproductions must be marked with this
#* legend.
#*
#*                          Copyright (c) 2003
#*             Computer Associates International, Incorporated
#*                          All rights reserved
#*
#* ----------------------------------------------------------------------
#

# camchown changes the ownership of the cam and/or caft daemon processes
# and adjusts the ownership of various files accordingly
#
# camchown [[-x|o|f] -l] [-u user ] [-g group]
#
# where -x or -o remove or add 'other' access to the CLI
#       -f restricts ownership change to the CAFT daemon and associated files
#       -l extends -x|o changes to the API libraries
#       -u specifies the user the daemon[s] should run under
#       -g specifies the group the daemon[s] should run under
#

# Take UserId as single argument and verify it appropriately
chkUser()
{
    # save user name locally
    user=$1

    # remove any existing temporary file before beginning
    rm -f /tmp/camChkUser

    # create temporary file against which to validate user by attempting to
    # set the owner of this file to be the user whose existence want to test
    touch /tmp/camChkUser
    chown $user /tmp/camChkUser 2>&1
    errStat=$?

    # tidy up
    rm -f /tmp/camChkUser

    # return the validation status
    return $errStat
}

# Take GroupId as single argument and verify it appropriately
chkGroup()
{
    # save group name locally
    group=$1

    # remove any existing temporary file before beginning
    rm -f /tmp/camChkGroup

    # create temporary file against which to validate user by attempting to
    # set the owner of this file to be the user whose existence want to test
    touch /tmp/camChkGroup
    chgrp $group /tmp/camChkGroup 2>&1
    errStat=$?

    # tidy up
    rm -f /tmp/camChkGroup

    # return the validation status
    return $errStat
}

id | grep '^uid=0(root)' > /dev/null

if [ $? -ne 0 ]
then
    echo "$0 must be run by root."
    exit 1
fi

if [ -z "$CAI_MSQ" ]
then
    if [ -f /etc/profile.CA ]
    then
        . /etc/profile.CA
    fi
    if [ -z "$CAI_MSQ" ]
    then
        # The CCS environment is broken - let's see if our marker file is present
        CAI_MSQ="`cat /etc/catngcampath`"
        export CAI_MSQ
    fi
fi

HAVEOTHER=""
HAVENOOTHER=""
CAFTONLY=""
CHANGELIB=""

CAMUSER=""
CAMGROUP=""

while getopts xoflu:g: c
do
	case $c in
		x)	HAVENOOTHER=y;;
		o)	HAVEOTHER=y;;
		f)	CAFTONLY=y;;
		l)	CHANGELIB=y;;
		u)	CAMUSER=$OPTARG;;
		g)	CAMGROUP=$OPTARG;;
		?)	echo usage: "$0 [-x|o|f] [-l] [-u user ] [-g group]"
			exit 1
	esac
done

if [ -z "$HAVEOTHER" -a -z "$HAVENOOTHER" -a -z "$CAFTONLY" -a -z "$CHANGELIB" -a -z "$CAMUSER" -a -z "$CAMGROUP" ]
then
    # No parameters
    echo usage: "$0 [-x|o|f] [-l] [-u user ] [-g group]"
	exit 1
fi

if [ -n "$CAMUSER" -o -n "$CAMGROUP" -o -n "$CAFTONLY" ]
then
    [ -n "$CAMUSER" ]  || CAMUSER=`ls -dlL "$CAI_MSQ/bin/camf" | awk '{ print $3 }'`
    [ -n "$CAMGROUP" ] || CAMGROUP=`ls -dlL "$CAI_MSQ/bin/camf" | awk '{ print $4 }'`
fi

if [ -n "$HAVEOTHER" -a -n "$HAVENOOTHER" ]
then
    echo usage: "$0 [-x|o|f] [-l] [-u user ] [-g group]"
    echo "either -x or -o may be specified, not both"
    exit 1
fi

if [ -n "$CHANGELIB" ]
then
    if [ -z "$HAVEOTHER" -a -z "$HAVENOOTHER" ]
    then
        echo usage: "$0 [-x|o|f] [-l] [-u user ] [-g group]"
        echo "one of -x or -o must be specified with -l"
        exit 1
    fi
fi

if [ -n "$CAFTONLY" ]
then
    if [ -n "$HAVEOTHER" -o -n "$HAVENOOTHER" ]
    then
        echo usage: "$0 [-x|o|f] [-l] [-u user ] [-g group]"
        echo "-x or -o cannot be specified with -f"
        exit 1
    fi
fi

if [ -n "$CAMUSER" ] # both must be -n
then
    chkUser $CAMUSER
    if [ $? -ne 0 ]
    then
        echo user $CAMUSER is invalid 
        exit 1
    fi

    chkGroup $CAMGROUP
    if [ $? -ne 0 ]
    then
        echo group $CAMGROUP is invalid 
        exit 1
    fi

    # Re-own the files vital to the CAFT daemon
    # NOTE: The CAFT API ownership isn't changed at this point
    for i in caftenv.cfg caftexec bin/caftf ftlogs 'ftlogs/*'
    do
        chown $CAMUSER:$CAMGROUP "$CAI_MSQ/$i"  2> /dev/null
    done

    # Make sure the cafthost config file is readable, wherever the CAFT server runs 
    # Other access may be take away by a later run with -o
    [ -f "$CAI_MSQ/cafthost.cfg" ] && chmod a+r "$CAI_MSQ/cafthost.cfg"
fi

# Now go on and deal with CAM and other permissions
if [ -z "$CAFTONLY" ]
then
    MODECHANGE=""

    if [ -n "$HAVEOTHER" ]
    then
	# add read permisions and execute permissions if 'executable'
        MODECHANGE="o=rX"
    else
        if [ -n "$HAVENOOTHER" ]
        then
            MODECHANGE="o-rx"
        fi
    fi

    if [ -n "$CAMUSER" ] # both must be -n
    then
	# NOTE: we do not change the permisions of caftexec, only the ownership (earlier)
        for i in cam.cfg camclient.cfg bin lib scripts 'bin/*' 'lib/*' 'scripts/*' logs 'logs/*' qlocal 'qlocal/*' qremote 'qremote/*'
        do
            # ignore errors as some files may not exist
            # don't try to check existance as some parameters are wildcards
            chown $CAMUSER:$CAMGROUP "$CAI_MSQ/$i" 2> /dev/null
        done

        # Make sure the starter programs have correct ownership and permissions 
        for i in cam caft
        do
            chown root "$CAI_MSQ/bin/$i"
            chmod u=srx "$CAI_MSQ/bin/$i"
        done
    fi;

    if [ -n "$MODECHANGE" ]
    then
        # Change 'other' access
        for i in logs ftlogs qlocal qremote bin scripts
        do
	    chmod $MODECHANGE "$CAI_MSQ/$i/*" 2> /dev/null
        done

        # change permissions if the files exist
        for i in cam.cfg camclient.cfg caftenv.cfg cafthost.cfg logs ftlogs qlocal qremote bin scripts
        do
	    [ -f "$CAI_MSQ/$i" ] && chmod $MODECHANGE "$CAI_MSQ/$i"
        done

        if [ -n "$CHANGELIB" ]
	then
            # The suffix is paltform dependent
            for i in libawmsq.* libcaft.* libcaften.*
            do
                chmod $MODECHANGE "$CAI_MSQ/lib/$i" 2> /dev/null
            done
        fi
    fi
fi
