#!/bin/ksh -p
#
# ident "@(#)utgenpam.ksh	1.48	12/06/11 Oracle"
#
# Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
#

#
# This module configures/unconfigures the PAM stack for all of SRSS
# and replaces the preremove/postinstall actions previously performed by
# SUNWuto and SUNWutps
#
# Arg #1 DEF_OPTION specifies the option to use the entries for the "other"
#	service as the default if the specified OLDAPP is not defined. 
#	See -o option for utpamcfg.  Valid values are DEF_YES and DEF_NO.
# Arg #2 TAG_OPTION specifies if tag is desired.  Valid values are TAG_YES
#	and TAG_NO.
# Arg #3 APPNAME is the application name with the optional module type.  It
#	should be in the "<appname>[:<mod-type>]".  See utpamcfg for more info.
# Arg #4 OLDAPP is the template to be used to create the stack.
# Arg #5 STACK defines if this is an old style pam.conf (i.e. < S8) or newer
#	ones (i.e. > S9).
# Arg #6 MODULE specifies the module to be added on top of the new stack.
# Arg #7 MODOPTS specifies the options to be specified with the module.
#

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

create_stack() {
    typeset DEF_OPTION="$1"
    typeset TAG_OPTION="$2"
    typeset APPNAME="$3"
    typeset OLDAPP="$4"
    typeset MODULE="${5-}"
    typeset MODOPTS="${6-}"

    typeset FLAGSTR=""
    if [ "$DEF_OPTION" == "DEF_NO" ]; then
	# specify the -o option to utpamcfg
   	FLAGSTR="-o"
    fi

    if [ "$TAG_OPTION" == "TAG_YES" ]; then
	# add tag
	typeset PAMCL=`print $APPNAME | sed -n -e 's/\([^:]*\).*$/\1/p'`
	FLAGSTR="$FLAGSTR -T \"$SRTAG -- $PAMCL\""
    fi

    typeset MODSTR=
    if [ -n "$MODULE" ]; then
	MODSTR="-m $MODULE"
    fi
    if [ -n "$MODOPTS" ]; then
	MODSTR="$MODSTR -M $MODOPTS"
    fi

    eval "$UTPAMCFG -c $APPNAME -t $OLDAPP $FLAGSTR $MODSTR"
}


#
# This module adds one entry to the top of the stack for the APPNAME specified.
#
# Arg #1 TAG_OPTION specifies if tag is desired.  Valid values are TAG_YES
#	and TAG_NO.
# Arg #2 APPNAME is the application name with the optional module type.  It
#	should be in the "<appname>[:<mod-type>]".  See utpamcfg for more info.
# Arg #3 MODULE specifies the module to be added to the top of the stack.
# Arg #4 MODOPTS specifies the options to be specified with the module.
# Arg #5 FLAG specifies the control flag to be added with the module.
#
add_module() {
    typeset TAG_OPTION="$1"
    typeset APPNAME="$2"
    typeset MODULE="$3"
    typeset MODOPTS="${4-}"
    typeset FLAG="${5-}"

    add_module_ext -a "$TAG_OPTION" "$APPNAME" "$MODULE" "$MODOPTS" "$FLAG"
}


#
# This module adds one entry to the bottom of the stack for the APPNAME specified.
#
# Arg #1 TAG_OPTION specifies if tag is desired.  Valid values are TAG_YES
#	and TAG_NO.
# Arg #2 APPNAME is the application name with the optional module type.  It
#	should be in the "<appname>[:<mod-type>]".  See utpamcfg for more info.
# Arg #3 MODULE specifies the module to be added to the bottom of the stack.
# Arg #4 MODOPTS specifies the options to be specified with the module.
# Arg #5 FLAG specifies the control flag to be added with the module.
#
append_module() {
    typeset TAG_OPTION="$1"
    typeset APPNAME="$2"
    typeset MODULE="$3"
    typeset MODOPTS="${4-}"
    typeset FLAG="${5-}"

    add_module_ext -A "$TAG_OPTION" "$APPNAME" "$MODULE" "$MODOPTS" "$FLAG"
}

#
# This is the extended add_module routine which adds one entry either to the top or
# bottom of the stack for the APPNAME specified depending the first option passed in.
# -a adds to the top and -A adds to the bottom.  These options are chosen based on
# the options from utpamcfg.
#
# Arg #1 ADD_OPTION -a adds to the top and -A adds to the bottom.
# Arg #2 TAG_OPTION specifies if tag is desired.  Valid values are TAG_YES
#	and TAG_NO.
# Arg #3 APPNAME is the application name with the optional module type.  It
#	should be in the "<appname>[:<mod-type>]".  See utpamcfg for more info.
# Arg #4 MODULE specifies the module to be added to the top or to the bottom of
#	the stack depending on the ADD_OPTION flag.
# Arg #5 MODOPTS specifies the options to be specified with the module.
# Arg #6 FLAG specifies the control flag to be added with the module.
#
add_module_ext() {
    typeset ADD_OPTION="$1"
    typeset TAG_OPTION="$2"
    typeset APPNAME="$3"
    typeset MODULE="$4"
    typeset MODOPTS="${5-}"
    typeset FLAG="${6-}"
    typeset MODSTR=""

    if [ -n "$MODOPTS" ]; then
	MODSTR="-M '$MODOPTS'"
    fi

    typeset FLAGSTR=""
    if [ "$TAG_OPTION" == "TAG_YES" ]; then
	# add tag
	typeset PAMCL=`print $APPNAME | sed -n -e 's/\([^:]*\).*$/\1/p'`
	FLAGSTR="-T \"$SRTAG -- $PAMCL\""
    fi
    if [ -n "$FLAG" ]; then
	FLAGSTR="$FLAGSTR -f $FLAG"
    fi
    eval "$UTPAMCFG $ADD_OPTION $APPNAME -m $MODULE $MODSTR $FLAGSTR"
}

#
# This module delete the PAM stack for the APPNAME specified.
#
# Arg #1 APPNAME is the application name with the optional module type.  It
#	should be in the "<appname>[:<mod-type>]".  See utpamcfg for more info.
# Arg #2 TAG_OPTION specifies if tag is desired.  Valid values are TAG_YES
#	and TAG_NO.
# Arg #3 is optional and if set indicates that the specified stack is
# 	specific to SRS (and thus should be removed entirely)

delete_stack() {
    typeset APPNAME="$1"
    typeset TAG_OPTION="$2"
    typeset SRSONLY="$3"
    typeset FLAGSTR=""

    if [ "$TAG_OPTION" == "TAG_YES" ]; then
	# add tag
	typeset PAMCL=`print $APPNAME | sed -n -e 's/\([^:]*\).*$/\1/p'`
	FLAGSTR="-T \"$SRTAG -- $PAMCL\""
    fi
    eval "$UTPAMCFG ${SRSONLY:+-x} -u $APPNAME $FLAGSTR"
}

add_Xserver_Class() {
    # Tell dtlogin that SunRay is a valid X server class so that
    # it can register with PAM as the client name "dtlogin-SunRay".
    # This will also take care of dtsession, which will register
    # with PAM as the client name "dtsession-SunRay".
    #
    # This requires the following bugfix to be installed:
    #	4452627 dtlogin should register with PAM using a different
    #		client name when on SunRay
    #
    # Xconfig changes that need to be preserved over reboots need
    # to be made to a prototype Xconfig file. If that file does
    # not exist, we create it.
    #
    # XXX Even more XXX, this shouldn't happen here, it should happen in the
    # XXX utacleanup init script where we do the rest of the Xconfig fixups.  

    if test -f $XCONFIG_USR_DIR/$GENERIC_XCONFIG; then
	if test ! -f $XCONFIG_ETC_DIR/$PROTO_XCONFIG ; then
	    mkdir -p $XCONFIG_ETC_DIR
	    cp $XCONFIG_USR_DIR/$GENERIC_XCONFIG $XCONFIG_ETC_DIR/$PROTO_XCONFIG
	fi

	egrep -is "$VALID_PAM_CLASS_TAG" $XCONFIG_ETC_DIR/$PROTO_XCONFIG
	if [ $? -eq 1 ]; then
	    echo "$VALID_PAM_CLASS_TAG: $SUNRAY_XSERVER_CLASS_TYPE" \
		>>$XCONFIG_ETC_DIR/$PROTO_XCONFIG
	fi
    fi
}

delete_Xserver_Class() {
    # Remove $SUNRAY_XSERVER_CLASS_TYPE from Xconfig.
    #
    # This requires the following bugfix to be installed:
    #	4452627 dtlogin should register with PAM using a different
    #		client name when on SunRay
    #
    if test -f $XCONFIG_USR_DIR/$GENERIC_XCONFIG; then
	if test -f $XCONFIG_ETC_DIR/$PROTO_XCONFIG; then
	    SCTMP="/var/run"
	    TMP="$SCTMP/$MOD.$$"
	    TMP_SR="$TMP.PAMsr"

	    egrep -is $VALID_PAM_CLASS_TAG $XCONFIG_ETC_DIR/$PROTO_XCONFIG
	    if [ $? -ne 1 ]; then
		egrep -vi "$VALID_PAM_CLASS_TAG" $XCONFIG_ETC_DIR/$PROTO_XCONFIG >$TMP_SR
		mv $TMP_SR $XCONFIG_ETC_DIR/$PROTO_XCONFIG
	    fi
	fi
    fi
}

add_gulogin_stack() {
    create_stack "DEF_NO" "TAG_YES" $GULOGIN "NONE"
    add_module "TAG_NO" $GULOGIN:auth $AMGHLIB "" required
    add_module "TAG_NO" $GULOGIN:auth $SRLIB_GU prompt requisite
    add_module "TAG_NO" $GULOGIN:auth $AMGHLIB "" required
    add_module "TAG_NO" $GULOGIN:auth $SRLIB_GU token=auth,JavaBadge requisite
    add_module "TAG_NO" $GULOGIN:auth $SRLIB_GU property=username requisite
}

add_nscm_stack() {
    DISPMGR=$1
    
    # need to create the specific "auth" type service for utnsclogin.
    # This will be based on Display Manager service stack if it
    # exists.  Otherwise, it should fall back to the "other" service
    # stack.
    create_stack "DEF_YES" "TAG_YES" $NSCLOGIN $DISPMGR
    create_stack "DEF_YES" "TAG_NO" $NSCLOGIN:auth $DISPMGR
    add_module "TAG_NO" $NSCLOGIN:auth $AMGHLIB "" required
    add_module "TAG_NO" $NSCLOGIN:auth $SRLIB_GU property=username requisite
}

make_dm_stack() {
    DISPMGR=$1
    OLDSERVICE=${2:-}

    NEWTAGS="TAG_YES"

    if [ -n "$OLDSERVICE" ]; then
	create_stack "DEF_YES" "TAG_YES" $DISPMGR $OLDSERVICE
	create_stack "DEF_YES" "TAG_NO" $DISPMGR:auth $OLDSERVICE
	create_stack "DEF_YES" "TAG_NO" $DISPMGR:account $OLDSERVICE
	create_stack "DEF_YES" "TAG_NO" $DISPMGR:session $OLDSERVICE
	NEWTAGS="TAG_NO"
    fi
	
    # auth stack
    add_module "$NEWTAGS" $DISPMGR:auth $AMGHLIB clearuser required
    add_module "TAG_NO" $DISPMGR:auth $SRLIB_GU prompt requisite
    add_module "TAG_NO" $DISPMGR:auth $SRLIB "" sufficient
    if [ -d $LIB_SUNWKIO ] ; then
	add_module "TAG_NO" $DISPMGR:auth $KIOSKLIB "log=user" requisite
	add_module "TAG_NO" $DISPMGR:auth $KIOSKLIB "log=user ignoreuser" sufficient
    fi
    add_module "TAG_NO" $DISPMGR:auth $AMGHLIB "" required
    add_module "TAG_NO" $DISPMGR:auth $SRLIB_GU property=username requisite
    add_module "TAG_NO" $DISPMGR:auth $HOTDESKLIB "" requisite

    # account stack
    add_module "$NEWTAGS" $DISPMGR:account $SRLIB "" sufficient
    [ -d $LIB_SUNWKIO ] \
        && add_module "TAG_NO" $DISPMGR:account $KIOSKLIB "log=user" sufficient

    # session stack
    [ -d $LIB_SUNWKIO ] \
        && add_module "$NEWTAGS" $DISPMGR:session $KIOSKLIB "log=user" required \
        && NEWTAGS="TAG_NO"
    add_module "$NEWTAGS" $DISPMGR:session $HOTDESKLIB "" requisite

    # Use negative logic, so we return with a sane return code
    [[ "$DISPMGR" != gdm ]] \
    	|| make_gdm_autologin_stack
}

make_gdm_autologin_stack() {
    # Prepare a gdm-autologin stack, to be able to use autologin for
    # kiosk (and eventually NSCM)

    # We assume that a basic autologin stack exists, so we just add our stuff
    # (because we have no reasonable template)

    # This option can be used to feed an external user to kiosk:
    typeset SET_KIOSK_EXT_USER="userdata=PAM_KIOSK_EXTERNAL_USER"

    # auth stack
    #  Q: Do we need AMGH clearuser here ?
    #  A: No! userdata=... does not set authd user
    # add_module "TAG_YES" $GDM_AUTOLOGIN:auth $AMGHLIB clearuser required
    add_module "TAG_YES" $GDM_AUTOLOGIN:auth $SRLIB "" sufficient
    if [ -d $LIB_SUNWKIO ] ; then
	add_module "TAG_NO" $GDM_AUTOLOGIN:auth $KIOSKLIB "log=user" requisite
	# TODO: add option to grab name from $SRLIB_GU via PAM item
	add_module "TAG_NO" $GDM_AUTOLOGIN:auth $KIOSKLIB "log=user use_user" sufficient
    fi
    add_module "TAG_NO" $GDM_AUTOLOGIN:auth $AMGHLIB "$SET_KIOSK_EXT_USER" required
    add_module "TAG_NO" $GDM_AUTOLOGIN:auth $SRLIB_GU "property=username $SET_KIOSK_EXT_USER" requisite
    add_module "TAG_NO" $GDM_AUTOLOGIN:auth $HOTDESKLIB "" requisite

    # account stack
    add_module "TAG_YES" $GDM_AUTOLOGIN:account $SRLIB "" sufficient
    [ -d $LIB_SUNWKIO ] \
        && add_module "TAG_NO" $GDM_AUTOLOGIN:account $KIOSKLIB "log=user" sufficient

    # session stack
    NEW_STACK_TAG="TAG_YES"
    [ -d $LIB_SUNWKIO ] \
        && add_module "$NEW_STACK_TAG" $GDM_AUTOLOGIN:session $KIOSKLIB "log=user" required \
        && NEW_STACK_TAG="TAG_NO"
    add_module "$NEW_STACK_TAG" $GDM_AUTOLOGIN:session $HOTDESKLIB "" requisite
}

utpam_configure() {
    # Note: Kiosk will not necessarily be installed ( e.g. on a TX system )
    #       so we configure it only if it's available.

    if [ $OS = "Linux" ]; then
	NEWTAG="TAG_YES"
	DISPMGR=gdm
	DM_STACK_NAME=gdm
	OLDSERVICE=

	if [ -f /etc/pam.d/$SYS_AUTH ]; then
	    if [ -f /lib/security/$STACKLIB ]; then
		# RHEL 3 and 4 import the system-auth stack by default
		add_module "TAG_YES" $SELFREG $STACKLIB service=$SYS_AUTH required
	    else
		add_module "TAG_YES" $SELFREG $SYS_AUTH "" substack
	    fi
	else
	    add_module "TAG_YES" $SELFREG $UNIX2LIB "" required
	fi

	# Template for hotdesk stack 
	# XXX: maybe we should search for an existing stack here?
	hdtmpl=gnome-screensaver
	# XXX: DEF_YES makes little sense, because the 'other' stack is usually 
	#      pam_deny on Linux, but the hardcoded fallback in utpamcfg is 
	#      worse
	# Copy all modules (we need auth, account and passwd)
	create_stack "DEF_YES" "TAG_YES" $HOTDESK $hdtmpl
    else
	NEWTAG="TAG_NO"
	if [ -x /usr/dt/bin/dtlogin ]; then
	    DISPMGR=dtlogin
	    DM_STACK_NAME=$DTLOGIN
	    OLDSERVICE=dtlogin
	    create_stack "DEF_YES" "TAG_YES" $DTSESSION dtsession
	    create_stack "DEF_YES" "TAG_NO" $DTSESSION:auth dtsession $SRLIB syncondisplay
	    # Add pam_sunray.so line to dtsession-SunRay account stack to work with LDAP
	    add_module "TAG_NO" $DTSESSION:account $SRLIB "" sufficient
	else
	    DISPMGR=$GDM
	    DM_STACK_NAME=$GDM
	    OLDSERVICE=other
	fi
	create_stack "DEF_YES" "TAG_YES" $XSCREENSAVER:auth other $SRLIB syncondisplay
	
	# Add xscreensaver account stack to work with LDAP. 
        create_stack "DEF_YES" "TAG_NO" $XSCREENSAVER:account xscreensaver $SRLIB ""

	add_Xserver_Class
	hdtmpl=dtsession
	# XXX: DEF_YES makes little sense, because the 'other' account stack on TX
	#      contains pam_tsol_account so doesn't work, but the
	#      hardcoded fallback in utpamcfg is worse
	# Copy all modules (we need auth, account and passwd)
	create_stack "DEF_YES" "TAG_YES" $HOTDESK:account $hdtmpl
	create_stack "DEF_NO" "TAG_YES" $ADMINGUI "NONE"
    fi

    # add the admingui entry module first so it shows up immediately after its
    # tag entry on Solaris
    add_module "$NEWTAG" $ADMINGUI $ADMINGUILIB "" sufficient
    add_gulogin_stack
    add_nscm_stack $DISPMGR
    make_dm_stack $DM_STACK_NAME $OLDSERVICE
}

utpam_unconfigure() {
    delete_stack $ADMINGUI "TAG_NO" true
    delete_stack $SELFREG "TAG_NO" true
    delete_stack $HOTDESK "TAG_NO" true
    delete_stack $NSCLOGIN "TAG_NO" true
    delete_stack $GULOGIN "TAG_NO" true

    delete_stack ${XSCREENSAVER} "TAG_NO"
    if [ -x /usr/dt/bin/dtlogin ] ; then
        delete_stack $DTLOGIN "TAG_NO"
        delete_stack $DTSESSION "TAG_NO"
    fi
    if [ -x /usr/sbin/gdm ] ; then
        delete_stack $GDM "TAG_NO"
        delete_stack $GDM_AUTOLOGIN "TAG_NO"
    fi

    delete_Xserver_Class
}

Usage() {
    printf "Usage: %s: [-r alternate_root] disable|enable\n" $0
}

# main

PKG_INSTALL_ROOT=""
CLIENT_BASEDIR="/opt"
DEBUG_ROOT=""

disable=false
while getopts d:hr: opt; do
    case $opt in
	d)	DEBUG_ROOT="$OPTARG";;
	r)	PKG_INSTALL_ROOT="$OPTARG";;
	h)	Usage
		exit 0;;
	?)	Usage
		exit 1;;
    esac
done
shift $(($OPTIND - 1))

action=
if [ "$1" == "disable" ]; then
    action=disable
    shift
elif [ "$1" == "enable" ]; then
    action=enable
    shift
fi
if [ -n "$*" -o -z "$action" ]; then
    Usage
    exit 1
fi

if [ -z "$BASEDIR_DEFAULT" ]; then
    BASEDIR_DEFAULT="$PKG_INSTALL_ROOT$CLIENT_BASEDIR"
fi

OS=`uname -s`
OS_REL=`uname -r`

ETCDIR_LIB=/etc/opt/SUNWut/lib

BASEDIR_PS="$DEBUG_ROOT$BASEDIR_DEFAULT"
BASEDIR_O="$DEBUG_ROOT$BASEDIR_DEFAULT"
BASEDIR_GSM="$DEBUG_ROOT$BASEDIR_DEFAULT"
BASEDIR_KIOSK="$DEBUG_ROOT$BASEDIR_DEFAULT"


LIB_SUNWUTO="$BASEDIR_O/SUNWut/lib"
LIB_SUNWKIO="$BASEDIR_KIOSK/SUNWkio/lib"
if [ $OS = "Linux" ]; then
	LIB_SUNWUTPS="$ETCDIR_LIB/\$\PLATFORM"
	LIB_SUNWUTGSM="$ETCDIR_LIB/\$\PLATFORM"
	LIB_SUNWUTKIOSK="$ETCDIR_LIB/\$\PLATFORM"
else
	LIB_SUNWUTPS="$BASEDIR_PS/SUNWut/lib"
	LIB_SUNWUTGSM="$BASEDIR_GSM/SUNWut/lib"
	LIB_SUNWUTKIOSK="$LIB_SUNWKIO"
fi

UTPAMCFG="$LIB_SUNWUTO/utpamcfg -n true"
SRLIB_GU="$LIB_SUNWUTPS/sunray_get_user.so.1"
HOTDESKLIB="$LIB_SUNWUTPS/pam_sunray_hotdesk.so.1"
AMGHLIB="$LIB_SUNWUTGSM/pam_sunray_amgh.so.1"
SRLIB="$LIB_SUNWUTPS/pam_sunray.so.1"
ADMINGUILIB="$BASEDIR_PS/SUNWut/lib/pam_sunray_admingui.so.1"
KIOSKLIB="$LIB_SUNWUTKIOSK/pam_kiosk.so.1"
STACKLIB=pam_stack.so
UNIX2LIB=pam_unix2.so

# PAM client name X server class (dtlogin/dtsession)
# requires fix for 4452627
XCONFIG_USR_DIR="$PKG_INSTALL_ROOT/usr/dt/config"
XCONFIG_ETC_DIR="$PKG_INSTALL_ROOT/etc/dt/config"
GENERIC_XCONFIG="Xconfig"
PROTO_XCONFIG="Xconfig.SUNWut.prototype"
VALID_PAM_CLASS_TAG="Dtlogin.validPAMclasses"
SUNRAY_XSERVER_CLASS_TYPE="SunRay"

# SunRay-qualified CDE application names in pam.conf
DTLOGIN="dtlogin-$SUNRAY_XSERVER_CLASS_TYPE"
DTSESSION="dtsession-$SUNRAY_XSERVER_CLASS_TYPE"

# GDM app name in pam.conf
GDM="gdm"
GDM_AUTOLOGIN="gdm-autologin"

# GNOME application name in pam.conf
XSCREENSAVER="xscreensaver"

PAM_CONF="/etc/pam.conf"
PAM_DIR="/etc/pam.d"

NSCLOGIN=utnsclogin
GULOGIN=utgulogin
ADMINGUI=utadmingui
SELFREG=utselfreg
HOTDESK=uthotdesk
# We don't manage a uthotdesk stack on Solaris - that's for users to (de)configure

SYS_AUTH=system-auth

SRTAG="SunRay Server Software"

export PKG_INSTALL_ROOT

utpam_unconfigure
if [ $action = enable ] ; then
    utpam_configure
fi

exit 0
