#!/bin/sh
#  $Id: sso_status.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_status ]; then
  . $TTAMSG/common
  . $TTAMSG/sso_status
else
  echo "Unable to open message file: $TTAMSG/sso_status"
  exit 1
fi

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
STATUS=1

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

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

# Check TPLA is active
echo $N "${lf_verifyla}" $C
TPLA=`${TTA} config list --tarantella-config-components-thirdpartyloginauthority | awk '{print $2}'`
SSO=`${TTA} config list --tarantella-config-sso-enabled | awk '{print $2}'`
if [ "${TPLA}" -eq 1 -a "${SSO}" -eq 1 ]; then
  echo "${lf_active}"
else
  STATUS=0
  echo "${lf_inactive}"
fi

# Check the client overrides
echo $N "${lf_setpriv}" $C
OVERRIDES=`${TTA} config list --tarantella-config-applaunch-allowclientoverrides| grep "sso_pw"`
if [ -n "${OVERRIDES}" ]; then
   echo "${lf_active}"
else
  STATUS=0
  echo "${lf_inactive}"
fi

# Check SSO URL is set
echo $N "${lf_checkssourl}" $C
SSOURL=`${TTA} config list --tarantella-config-sso-url | grep "http"`
if [ -n "${SSOURL}" ]; then
  echo "${lf_available}"
else
  STATUS=0
  echo "${lf_unavailable}"
fi

# Check the WebGate binaries exist
echo $N "${lf_webgate_binaries}" $C
WEBGATE_INSTALL=`GetWebGateConfig webgateinstall`
if [ ! -s "${WEBGATE_INSTALL}/lib/webgate.so" -o ! -s "${WEBGATE_INSTALL}/lib/webgatessl.so" ]; then
   echo "${lf_unavailable}"
   STATUS=0
else
   echo "${lf_available}"
fi 

# Check webgate.conf is available
echo $N "${lf_webgate_conf}" $C
A_VER=`cat ${TTADIR}/var/info/apacheversion`
if [ ! -s "${TTADIR}/webserver/apache/${A_VER}/conf/webgate.conf" ]; then
   echo "${lf_unavailable}"
   STATUS=0
else
   echo "${lf_available}"
fi
 
# Check webgate configuration files are available
echo $N "${lf_webgate_instance}" $C
WEBGATE_INSTANCE=`GetWebGateConfig webgateinstance`
if [ -z "${WEBGATE_INSTANCE}" ]; then
   echo "${lf_unavailable}"
   STATUS=0
else
   echo "${lf_available}"

   echo "${lf_webgate_reg}"
   echo $N "${lf_obaccessclient}" $C
   OAMCFGDIR="${WEBGATE_INSTANCE}/webgate/config" 
   if [ ! -s "${OAMCFGDIR}/ObAccessClient.xml" ]; then
      echo "${lf_unavailable}"
      STATUS=0
   else
      echo "${lf_available}"
   fi
 
   echo $N "${lf_cwallet}" $C
   if [ ! -s "${OAMCFGDIR}/cwallet.sso" ]; then
      echo "${lf_unavailable}"
      STATUS=0
   else
      echo "${lf_available}"
   fi

   SECURITY_LEVEL=`GetSecurity ${OAMCFGDIR}/ObAccessClient.xml`
   case "${SECURITY_LEVEL}" in
      "simple")
          echo $N "${lf_password}" $C
          if [ ! -s "${OAMCFGDIR}/password.xml" ]; then
             echo "${lf_unavailable}"
             STATUS=0
          else
             echo "${lf_available}"
          fi
          echo $N "${lf_aaa_key}" $C
          if [ ! -s "${OAMCFGDIR}/simple/aaa_key.pem" ]; then
             echo "${lf_unavailable}"
             STATUS=0
          else
             echo "${lf_available}"
          fi
          echo $N "${lf_aaa_cert}" $C
          if [ ! -s "${OAMCFGDIR}/simple/aaa_cert.pem" ]; then
             echo "${lf_unavailable}"
             STATUS=0
          else
             echo "${lf_available}"
          fi  
          ;;             
      "cert")
          echo $N "${lf_password}" $C
          if [ ! -s "${OAMCFGDIR}/password.xml" ]; then
             echo "${lf_unavailable}"
             STATUS=0
          else
             echo "${lf_available}"
          fi
          echo $N "${lf_aaa_key}" $C
          if [ ! -s "${OAMCFGDIR}/simple/aaa_key.pem" ]; then
             echo "${lf_unavailable}"
             STATUS=0
          else
             echo "${lf_available}"
          fi 
          echo $N "${lf_aaa_cert}" $C
          if [ ! -s "${OAMCFGDIR}/simple/aaa_cert.pem" ]; then
             echo "${lf_unavailable}"
             STATUS=0
          else
             echo "${lf_available}"
          fi  
          echo $N "${lf_aaa_chain}" $C
          if [ ! -s "${OAMCFGDIR}/simple/aaa_chain.pem" ]; then
             echo "${lf_unavailable}"
             STATUS=0
          else
             echo "${lf_available}"
          fi  
          ;;
   esac
fi

# Report status
if [ "${STATUS}" -eq 0 ]; then
  echo "${lf_disabled}"
  echo "${lf_enable}"
else
  # Check for sso status file which indicates apache has been restarted
  if [ -s "${TTADIR}/var/info/sso.status" ]; then
     echo "${lf_enabled}"
  else
     echo "${lf_configured}"
  fi
fi
