#
# ident "@(#)postinstall.src	1.4 04/11/01 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
#

# This script enables sunray usage for CDE (dtlogin). It modifies a
# CDE X property that tells dtlogin (and dtsession) to register
# with PAM using the "-SunRay" extension to the PAM client name.
#
# This script also edits /etc/pam.conf to add pam_sunray.so actions.
#
# Properties for dtsession are controlled via the Xsession.d/0100.SUNWut
# script.
#
# This script requires that the following CDE patches be installed:
#
#	108919 (minimum -14)
#	109354 (minimum -13)

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

if [ -z "$PKG_INSTALL_ROOT" ]; then PKG_INSTALL_ROOT=""; fi
if [ -z "$CLIENT_BASEDIR" ]; then CLIENT_BASEDIR="/opt"; fi
if [ -z "$BASEDIR" ]; then BASEDIR="$PKG_INSTALL_ROOT$CLIENT_BASEDIR"; fi

set -u

MOD="`basename $0`"

# override this variable for debugging
PAM_CONF="$PKG_INSTALL_ROOT/etc/pam.conf"

# 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"

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


# PAM modules info
#
SUNWUTLIB="SUNWut/lib"

SRLIB="$CLIENT_BASEDIR/$SUNWUTLIB/pam_sunray.so"
SRLIB_GU="$CLIENT_BASEDIR/$SUNWUTLIB/sunray_get_user.so"

SRTAG="SunRay Server Software"

# Check for some files and directories that we must have
# to make this all work.
#
if test ! -f $PAM_CONF; then
  echo "$MOD: $PAM_CONF does not exist"
  exit 1
fi

# enable sunray authentication for CDE in $PAM_CONF
# Do it  irrespective of whether it exists or not, 
# to make sure we fix the dtlogin stacks to follow the new model.
#
OS_REL="`uname -r`"

NEWSTACK=true

if [ "$OS_REL" = "5.8" ]; then
  NEWSTACK=false
fi

# We assume that we can execute utpamcfg from the installed location on
# this machine.  In general this might not be true because we could have
# just installed, say, a SPARC binary package into an alternative root
# while executing the packaging tools (and therefore this script) on a
# a box with some other architecture.  However, we happen to know that
# utpamcfg is a shell script so we can get away with running it now.

UTPAMCFG="$BASEDIR/$SUNWUTLIB/utpamcfg"

$UTPAMCFG -c $DTLOGIN -t dtlogin -n $NEWSTACK \
	-T "$SRTAG -- $DTLOGIN" -P "$PAM_CONF"
$UTPAMCFG -a $DTLOGIN -m $SRLIB \
	-T "$SRTAG -- $DTLOGIN" -P "$PAM_CONF"
$UTPAMCFG -a $DTLOGIN -m $SRLIB_GU -M property=username -f requisite \
	-T "$SRTAG -- $DTLOGIN" -P "$PAM_CONF"

$UTPAMCFG -c $DTSESSION -t dtsession -n $NEWSTACK -m $SRLIB -M syncondisplay \
	-T "$SRTAG -- $DTSESSION" -P "$PAM_CONF"

$UTPAMCFG -c $XSCREENSAVER -t other -n $NEWSTACK -m $SRLIB -M syncondisplay \
	-T "$SRTAG -- $XSCREENSAVER" -P "$PAM_CONF"

# 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 Note that this will only look for the valid class property
# XXX which may or may not contain $SUNRAY_XSERVER_CLASS_TYPE.
# XXX It is possible that other classes are in that property as
# XXX well and we should preserve those.
# XXX This is a bug which should be fixed.
#
# 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_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

exit 0
