#!/bin/ksh -p
#
# ident "@(#)utamghadm.ksh	1.7 05/06/21 SMI"
#
# Copyright 2004,2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

PATH=/usr/xpg4/bin:/bin:/usr/bin
PROG=$(basename $0)

Usage() {
    cat <<EOF
Usage:	$PROG
	$PROG -d
	$PROG -s executable
	$PROG -l library
	$PROG -x
Opts:
   -d		   Disable AMGH
   -l library	   Path to mapping library to use
   -s executable   Path to mapping executable to use
   -x		   Machine-readable output (key/value)

With no arguments, print configuration
EOF
}

IsEuid0() {
    if [ "$(id -u)" = 0 ]; then
	return 0
    else
	return 1
    fi
}

ErrorMsg() {
    print -u2 "$PROG: $1"
}

GetProp() {
    $UTPROP $1 2>/dev/null | sed "s/^${1}=//"
}

PrintRestartMsg() {

     print -u2 "
Please restart services for the change to take effect. Use :

           /opt/SUNWut/sbin/utrestart -c

Note that this will terminate all sessions."

}


PrintConfig() {
    typeset lib script
    lib=$1
    script=$2

    if [ -n "$xflag" ]; then
	print "LIB=$lib"
	print "SCRIPT=$script"
	return
    fi
    
    if [ -z "${lib}${script}" ]; then
	print "AMGH is not configured"
    else
	if [ -n "$script" ]; then
	    if [ "$lib" != $UTAMGHMAPLIB ]; then
		ErrorMsg "System Error: Wrong map lib for scripts '$lib'"
	    fi
	    printf "AMGH Mapping Script: %s\n" $script
	else
	    printf "AMGH Mapping Library: %s\n" $lib
	fi
    fi
}

DeleteConfig() {
    $UTPROP -d AmghMappingExecutablePath 2>/dev/null
    $UTPROP -s -d AmghMappingLibraryPath 2>/dev/null
}

MakeConfig() {
    if [ -n "$sflag" ]; then
	$UTPROP -a AmghMappingExecutablePath "$script"
	lib=$UTAMGHMAPLIB
    fi

    if [ -z "$lib" ]; then	# should never happen
	ErrorMsg "System Error: no lib defined"
	exit 2
    fi

    $UTPROP -s -a AmghMappingLibraryPath "$lib"
}

AbsolutePath() {
    print $1 | grep "^/" >/dev/null
}

# PermsTight target
# target must be a file owned by root, and not writable by group or other
PermsTight() {
    TARG=$1

    # Make sure it's a regular file
    if [ ! -f "$TARG" ] ; then
	return 1
    fi

    # Get the file mode and owner
    PERMS="$(ls -nlL "$TARG" | awk '{print $1, $3}')"

    # Is the owner anything but uid 0?
    if [ $(echo $PERMS | cut -d " " -f 2) != 0 ]; then
	return 1
    fi

    # Is it group or other writable?
    if [ $(echo $PERMS | cut -c 6,9) != "--" ]; then
	return 1
    fi

    return 0
}

UTPRODINFO=/etc/opt/SUNWut/basedir/lib/utprodinfo
UTLIBDIR=$(${UTPRODINFO} -r SUNWutgsm)/SUNWut/lib
UTAMGHMAPLIB=${UTLIBDIR}/libutamghmapwrapper.so.1
UTPROP=${UTLIBDIR}/utprop
UTADMIN_CONF=/etc/opt/SUNWut/utadmin.conf

if [[ ! -f $UTADMIN_CONF ]]
then
	ErrorMsg "Please run utconfig first before using this utility"
	exit 1

fi

cur_lib=$(GetProp AmghMappingLibraryPath)
cur_script=$(GetProp AmghMappingExecutablePath)

if [ $# = 0 ]; then
    PrintConfig "$cur_lib" "$cur_script"
    exit 0
fi

sflag=
lflag=
dflag=
xflag=
script=
lib=

# while getopts xhdus:l: flag; do
while getopts xhds:l: flag; do
    case $flag in
	d)	dflag=1
		;;
	s)	sflag=1
		script="$OPTARG"
		;;
	l)	lflag=1
		lib="$OPTARG"
		;;
	h)	Usage
		exit 0
		;;
	x)	xflag=1
		;;
	?)	Usage 1>&2
		exit 1
		;;
    esac
done

shift `expr $OPTIND - 1`

# Check args

if [ $# != 0 ]; then
    Usage 1>&2
    exit 1
fi

# Check for sanity of the user specification
if [ -n "$xflag" -a "${sflag}${lflag}${dflag}" ]; then
    ErrorMsg "-x flag cannot be specified with any other flags"
    Usage 1>&2
    exit 1
fi

if [ -n "$dflag" -a "${sflag}${lflag}${xflag}" ]; then
    ErrorMsg "-d flag cannot be specified with any other flags"
    Usage 1>&2
    exit 1
fi

if [ -n "$sflag" -a -n "$lflag" ]; then
    ErrorMsg "-s and -l flags cannot be specified together"
    Usage 1>&2
    exit 1
fi

# If $lib isn't set, but $script is, use mapping library for comparison
if [ -z "$xflag" ] && [ "${cur_lib}${cur_script}" = "${lib:-${script:+${UTAMGHMAPLIB}}}${script}" ]; then
    ErrorMsg "No configuration change requested"
    PrintConfig "$cur_lib" "$cur_script"
    exit 1
fi

# Check that absolute pathnames are entered
if [ -n "$lib" ] && ! $(AbsolutePath $lib); then
    ErrorMsg "Please enter an absolute pathname for the library"
    Usage 1>&2
    exit 1
fi

if [ -n "$script" ] && ! $(AbsolutePath $script); then
    ErrorMsg "Please enter an absolute pathname for the executable"
    Usage 1>&2
    exit 1
fi

# Check for existance of libs and scripts
if [ -n "${sflag}${lflag}" -a ! -f "${lib}${script}" ]; then
    ErrorMsg "Library or Script does not exist"
    Usage 1>&2
    exit 1
fi

# Check for correct perms on libs and scripts
if [ -n "$sflag" ] && ( [ ! -x "$script" ] || ! $(PermsTight "$script") ); then
    ErrorMsg "Executables must be owned by root, executable, and writeable only by its owner"
    Usage 1>&2
    exit 1
fi

if [ -n "$lflag" ] && ! $(PermsTight "$lib"); then
    ErrorMsg "Libraries must be owned by root and writable only by its owner"
    Usage 1>&2
    exit 1
fi

# OK, enough nonsense, let's do something

if [ -n "$xflag" ]; then
    PrintConfig "$cur_lib" "$cur_script"
    exit 0
fi

if ! IsEuid0; then
    echo "Error: root permissions required" 1>&2
    exit 1
fi

if [ -n "$dflag" ]; then
    DeleteConfig
else
    DeleteConfig
    MakeConfig
fi

lib=$(GetProp AmghMappingLibraryPath)
script=$(GetProp AmghMappingExecutablePath)

PrintConfig "$lib" "$script"
PrintRestartMsg

exit 0
