#!/bin/sh
#
#ident "@(#)smloginmodule	1.8 04/08/12 SMI"
# Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.

################################################################################
##
## This script registers JAAS based Login Module implementations
## with the Sun Java(TM) Web Console.
##
## This interface is deprecated in favor of "smreg add|remove -m ..."
## and will redirect registration to that command.
##
################################################################################


################################################################################
#
# Globals Variables
#
################################################################################

PATH=/bin:/sbin:/usr/sbin

## This script's name
PROGNAME=`basename "$0"`

## Return codes
EXIT_SUCCESS=0
EXIT_USAGE=1		# missing/malformed arguments
EXIT_UNKNOWNOS=3	# can't determine OS we're running on
EXIT_BADOS=4		# detected OS not supported

## Useful constants
ADD_CMD="add"			# "add" subcommand
REMOVE_CMD="remove"		# "remove" subcommand

# indicates to print the version of this script
VERSION_FLAG_SHORT="-v"		
VERSION_FLAG_LONG="--version"	

# indicates one option of the Loginmodule follows next
OPTION_FLAG_SHORT="-o"		
OPTION_FLAG_LONG="--option"	

# indicates one JAAS behaviour flag of the Loginmodule follows next
BEH_FLAG_SHORT="-f"		
BEH_FLAG_LONG="--flag"	

# indicates to print the usage of this script
HELP_FLAG_SHORT="-h"		
HELP_FLAG_LONG="--help"		

# indicate to disable deprecation warning
DEPRECATE_FLAG_SHORT="-n"
DEPRECATE_FLAG_LONG="--nowarn"

# Must be able to determine target OS so can set up appropriate
# command environment.
OS=`uname -s`
if [ ! -n "${OS}" ]; then
    echo "Unable to determine operating system."
    exit $EXIT_UNKNOWNOS
fi

# Setup command environment for target OS.
if [ "${OS}" = "SunOS" ]; then
    PATH=/bin:/sbin:/usr/sbin
    MANSECTION=1M
elif [ "${OS}" = "Linux" ]; then
    PATH=/bin:/sbin:/usr/bin:/usr/sbin
    MANSECTION=1
else
    echo "Not supported on detected OS \"${OS}\"."
    exit $EXIT_BADOS
fi


################################################################################
#
# moduleUsage
#
# Prints usage of this script to the screen.
#
################################################################################

moduleUsage() {

    cat - << EOF
    Usage:
        $PROGNAME [$HELP_FLAG_SHORT | $DEPRECATE_FLAG_SHORT] add <login_module_class_name> \\
            $OPTION_FLAG_SHORT <name1>="<value1>" \\
            $OPTION_FLAG_SHORT <name2>="<value2>" \\
            $BEH_FLAG_SHORT <required | optional | requisite | sufficient>

        $PROGNAME [$HELP_FLAG_SHORT | $DEPRECATE_FLAG_SHORT] remove <login_module_class_name>

    "$PROGNAME add ..." is deprecated in favor of "smreg add -m ..."
    "$PROGNAME remove ..." is deprecated in favor of "smreg remove -m ..."
    For more information, see smreg($MANSECTION).

    Options:
        -h --help       Usage for $PROGNAME
        -o --option     Options specific to the login module. The options are
                        specified as name value pairs, each preceeded with the
                        "-o" flag.
        -f --flag       A flag value that controls the overall behavior as
                        authentication proceeds down the stack. The following
                        values are allowed:
                            required | requisite | sufficient | optional
        -n --nowarn     Disable printing of the command deprecation warning.

EOF

    exit $EXIT_USAGE

} ## end moduleUsage


################################################################################
#
# Prints the given string in user-friendly format.
#
################################################################################

printOut() {

    cat - << EOF

    $1

EOF

} ## end printOut


################################################################################
#
# Main function area for register
#
# $1 = add|remove|-h|-n
# For further details on arguments, see the smreg command
#
################################################################################

# subcommand must be provided
if [ -z "$1" ]; then
    moduleUsage
fi

if [ "$1" = "$DEPRECATE_FLAG_SHORT" -o "$1" = "$DEPRECATE_FLAG_LONG" ]; then
    shift
else
    echo "WARNING!  This command is deprecated and will be unavailable in a"
    echo "future release.  For more information, see smreg($MANSECTION)."
    echo "To disable this warning, specify $DEPRECATE_FLAG_SHORT as the first option."
    echo ""
fi
    

SUB_COMMAND=$1
case "$SUB_COMMAND" in


"$HELP_FLAG_SHORT" | "$HELP_FLAG_LONG" )
    moduleUsage
    ;;

"$VERSION_FLAG_SHORT" | "$VERSION_FLAG_LONG" )

    # We will redirect to "smreg -V
    command="sh `dirname $0`/smreg -V"
    results=`$command`
    status=$?
    if [ "$status" -eq 1 ]; then
	moduleUsage
    fi
    if [ "$status" -ne 0 ]; then
	printOut "${results}\n"
	exit $status
    fi

    # Same output from smreg, but change program name.
    printOut "$results" | sed -e "s/smreg/smloginmodule/" -e "s/        /    /"
    ;;

*)
    # ADD and REMOVE subcommands redirect to smreg.
    # All others are usage errors
    if [ "$SUB_COMMAND" = "$ADD_CMD" ]; then
	command="sh `dirname $0`/smreg add -m"
    elif [ "$SUB_COMMAND" = "$REMOVE_CMD" ]; then
	command="sh `dirname $0`/smreg remove -m"
    else
	moduleUsage
    fi

    shift
    moduleName=$1

    # Some values are in 'name=value' format, where value can contain multiple
    # words wrapped in quotes.  However, the parent shell eats the quotes, so
    # we have re-insert them in order to hand over processing of the command
    # to smreg.
    #
    shift
    while [ -n "$1" ]; do
	a=$1

	# -f maps to -b in smreg
	if [ "$1" = "-f" ]; then
	    a="-b"
	fi

	# If property value in 'name=value' format, embed value in quotes
	echo $1 | grep '=' >/dev/null 2>&1
	if [ $? -eq 0 ]; then	
	    a=`echo $1 | sed -e 's/=/=\"/' -e 's/$/\"/'`
	fi

	# Continue building up command and its arguments
	command="$command $a"
	shift
    done

    command="$command $moduleName"

    # Redirect and handle return status appropriately.
    results=`eval $command`
    status=$?

    # Usage error maps to out own usage msg.
    if [ "$status" -eq 1 ]; then
	moduleUsage
    fi

    # All other errors redirect to screen.
    if [ "$status" -ne 0 ]; then
	printOut "${results}\n"
	exit $status
    fi

    # If have results on success, redirect to screen
    if [ -n "$results" ]; then
	printOut "${results}"
    fi
    ;;

esac

exit $EXIT_SUCCESS
