#
# ident "@(#)preremove.src	1.4 04/10/22 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
#

# This script disables sunray usage for CDE (dtlogin). It removes 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 remove 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`"

SUNWUTLIB="SUNWut/lib"

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

SCTMP="/var/run"

TMP="$SCTMP/$MOD.$$"
TMP_SR="$TMP.PAMsr"

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

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

SRTAG="SunRay Server Software"

# Remove the appname from the passed file
#
#    Usage: remove_PAM_prop {filename} {appname} {tag}
#
remove_PAM_prop () {
  FNAME="$1"
  APPNAME="$2"
  TAG="$3"

  awk '
    $1 != AppName { print $0; }
  ' AppName="$APPNAME" $FNAME >$TMP_SR

  if [ "$?" = 0 ]; then
    egrep -v "$TAG" $TMP_SR >$FNAME
    rm $TMP_SR
  else
    rm $TMP_SR
    echo "$MOD: error updating $FNAME"
    exit 1
  fi

}

# Remove sunray authentication from $PAM_CONF
#
remove_sr_from_PAM () {
  remove_PAM_prop $PAM_CONF $DTLOGIN "$SRTAG -- $DTLOGIN"
  remove_PAM_prop $PAM_CONF $DTSESSION "$SRTAG -- $DTSESSION"
  remove_PAM_prop $PAM_CONF $XSCREENSAVER "$SRTAG -- $XSCREENSAVER"
}

# 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

# CDE is considered disabled for sunray authentication
# if the token $SRLIBso is not in $PAM_CONF
#
remove_sr_from_PAM

# 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
#
# 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 Should we do a diff between the config file in /etc and
# XXX the one in /usr, and if they are the same, then remove
# XXX the file in /etc?
#
if test -f $XCONFIG_ETC_DIR/$PROTO_XCONFIG; then
  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

exit 0
