#!/bin/sh
#  $Id: sso_enable.sh 109677 2015-03-31 12:24:31Z pc161604 $
#  Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
#  This software code contains proprietary information
#  and should be treated as confidential.

N=""
C=""
if echo -n | grep n > /dev/null
then
  C="\c"
else
  N="-n"
fi
case "$MACHINEARCH" in
i3li*|ppli*) N="$N -e" ;;
esac
_ss_headlines="--------------------------------------------------------------------------"

if [ -f $TTAMSG/common -a -f $TTAMSG/sso_enable ]; then
  . $TTAMSG/common
  . $TTAMSG/sso_enable
else
  echo "Unable to open message file: $TTAMSG/sso_enable"
  exit 1
fi

u="`LANG=C LC_ALL=C id | sed 's/.*uid=\([0-9][0-9]*\).*/\1/'`"
if [ "$u" = "0" ]
then
  : # all is okay
else
  echo "You must be root to run this program"
  exit 15
fi

GetPrivUsrGrp()
{
    cat ${TTAINSTDIR}/var/serverconfig/global/array.properties 2>/dev/null | awk -F= \
	'BEGIN { u="ttasys"; wu="ttaserv"; g="ttaserv" }
	$1 == "tarantella.config.array.privilegeduser" { u = $2 }
	$1 == "tarantella.config.array.webserveruser" { wu = $2 }
	$1 == "tarantella.config.array.privilegedgroup" { g = $2 }
	END { print u " " wu " " g }'
}
_privusrgrp=`GetPrivUsrGrp`
privusr=`expr "$_privusrgrp" : '\([^ ]*\) .*'`
webservusr=`expr "$_privusrgrp" : '.* \([^ ]*\) .*'`
privgrp=`expr "$_privusrgrp" : '.* \([^ ]*\)$'`

GetSecurity()
{
   FILE=$1

   SECURITY=`tr '>' '\012' < ${FILE}  | tr '<' ' '  | grep security`
   VALUE=`echo ${SECURITY} | grep "Value"`
   if [ -z "${VALUE}" ]; then
      LINE=`grep -n security ${FILE} | awk '{print $1}' | tr ':' ' '`
      NEXT=`expr ${LINE} + 1`
      SECURITY=`head -${NEXT} ${FILE} | tail -1`
   fi

   echo ${SECURITY} | tr '"' ' ' |
   awk '{ for (i=1; i<NF; i++)
   {
       if ($i == "Value=")
       {
          n=(i+1)
          print $n
          break
       }
   } }'
}

GetWebGateConfig()
{
   CONFIG=$1

   # Try local first
   LOCAL_CONFIG="--tarantella-config-sso-server-${CONFIG}"
   CONF=`${TTAINSTDIR}/bin/tarantella config list ${LOCAL_CONFIG} | awk '{print $2}' | tr -d '\"'`
   if [ -z "${CONF}" ]; then
      GLOBAL_CONFIG="--tarantella-config-sso-${CONFIG}"
      CONF=`${TTAINSTDIR}/bin/tarantella config list ${GLOBAL_CONFIG} | awk '{print $2}' | tr -d '\"'`
   fi

   echo "${CONF}"
}


TTA=${TTADIR}/bin/tarantella

usage()
{
    echo "${enable_usage}" >&2
}

getWebGateData()
{
   PROMPT=$1
   CONFIG=$2
   LOCAL_CONFIG=$3
   DATA=""
   UNCHANGED=0 

   if [ -n "${LOCAL_CONFIG}" ]; then
      # Local value takes precedence, if set
      DATA=`${TTA} config list ${LOCAL_CONFIG} | awk '{print $2}' | tr -d '\"'`
   fi

   if [ -z "${DATA}" ]; then
      DATA=`${TTA} config list ${CONFIG} | awk '{print $2}' | tr -d '\"'`
   fi
   
   # Prompt user to confirm existing value or enter new one 
   while :; do
      echo $N "${PROMPT} [${DATA}]" $C
      read reply
      if [ -z "${reply}" ]; then
         if [ -n "${DATA}" ]; then
             reply=${DATA}
             UNCHANGED=1 
             break
         fi
      else
         break
      fi
   done

   # Data is unchanged - do nothing 
   if [ ${UNCHANGED} -eq 1 ]; then
      return
   fi

   if [ -z "${LOCAL_CONFIG}" ]; then
      # Global only
      ${TTA} config edit "${CONFIG}" "${reply}" > /dev/null
   else
      # Store locally or globally ?
      while :; do
        echo $N "${lf_prompt_local} [${lf_global}]" $C
        read location
        case "${location}" in
          ${lf_global}|"")
             # Store the global value and clear the local one
             ${TTA} config edit "${CONFIG}" "${reply}" "${LOCAL_CONFIG}" "" > /dev/null
             break;;
          ${lf_local}) 
	     # Store the local value
             ${TTA} config edit "${LOCAL_CONFIG}" "${reply}" > /dev/null
             break;;
        esac
      done
   fi
}

while [ -n "$1" ]; do
    case "$1" in
	--help) 
	    usage
	    exit 0
	    ;;
	*) 
	    usage
	    exit 0
	    ;;
    esac
    shift
done

# Warn about restart of apache
echo "${lf_warnrestart}"
while :; do
   echo $N "${lf_confirm}" $C
   read reply
   case "${reply}" in
     [yY]*) break;;
     [nN]*) echo "${lf_nochanges}"; exit 0;;
   esac
done

# Prompt for all the required OAM data
getWebGateData "${lf_prompt_url}" "--tarantella-config-sso-url" 
getWebGateData "${lf_prompt_webgate_instance}" "--tarantella-config-sso-webgateinstance" "--tarantella-config-sso-server-webgateinstance"
getWebGateData "${lf_prompt_webgate_install}" "--tarantella-config-sso-webgateinstall" "--tarantella-config-sso-server-webgateinstall"
getWebGateData "${lf_prompt_webgate_conf}" "--tarantella-config-sso-webgateconf" "--tarantella-config-sso-server-webgateconf"

echo "${lf_loginauth}"
# Store the old value of tpla so it can be reset if sso is disabled
${TTA} config list --tarantella-config-components-thirdpartyloginauthority | awk '{print $2}' > ${TTADIR}/var/info/sso_tpla.orig

# Turn on TPLA
${TTA} config edit --tarantella-config-components-thirdpartyloginauthority 1 > /dev/null
${TTA} config edit --tarantella-config-login-thirdparty-allownonens 1 > /dev/null
# Turn on SSO
${TTA} config edit --tarantella-config-sso-enabled 1 > /dev/null

# Set client overrides
echo "${lf_setpriv}"
OVERRIDES=`${TTA} config list --tarantella-config-applaunch-allowclientoverrides | awk '{print $2}' | sed -e s/\"//g`
if [ -n "${OVERRIDES}" ]; then
  FOUND=`echo ${OVERRIDES} | grep sso_pw`
  if [ -z "${FOUND}" ]; then
    OVERRIDES=${OVERRIDES},sso_pw
  fi
else
  OVERRIDES=sso_pw
fi

${TTA} config edit --tarantella-config-applaunch-allowclientoverrides "${OVERRIDES}" > /dev/null

# Trigger a restart of SSO
${TTA} sso restart
