#!/bin/sh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lpp/bosinst/samples/AE/AS/validateParams.sh 1.1 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2008,2009 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 

# @(#)46        1.1  src/bos/usr/lpp/bosinst/samples/AE/AS/validateParams.sh, bosinst, bos720 3/29/09 15:20:36

#
# A utility for config WAS.
#
# usage: sh validateParams  <parameters>

# source include for debug output
. /opt/IBM/AE/AS/debugWAS
echodebug "Validating the WebSphere configuration key value pairs"

WAS=/opt/IBM/WebSphere
WAS_HOME=$WAS/AppServer
WAS_PROFILES=$WAS/Profiles


#FULL_HOSTNAME=`cat /etc/HOSTNAME`
#WAS_USERNAME=`cat $WAS_PROFILES/.ibm/username`
#WAS_PASSWORD=`cat $WAS_PROFILES/.ibm/password`

BADPARM=false

check_type(){

  case $type in 
    default | custom | dmgr | jmgr | adminagent | cell | none) 
      echodebug "Valid value $type for key -type."
      ;;
    *) 
      echodebug "ERROR: Invalid value $type for key -type."
      echodebug "Valid values are default, custom, dmgr, jmgr, adminagent, cell, or none."
      BADPARM=true;
      ;;
  esac

} 
check_cell_name(){
  #names must start and end with alpha, number, period, dash or underscore
  #names must not contain spaces or `/\*,:;=+?|<>_%'\''"[]#$^{}()
  #
  # one way to do it using sed 
  # sed '/[]\|/\|\\|*\|,\|:\|;\|=\|+\|?\||\|<\|>\|_\|%\|[\|#\|$\|^\|{\|}\|(\|)\| \|"\|'\'']/d'
  #
  # better way using sed
  # don't print if string contains anything other than alpha, numeric, ., _, -
  if [ -z `$ECHOE $cell_name | sed -n '/[^[:alnum:]\|.\|_\|-]/!p'` ]; then 
    echodebug "ERROR: Invalid value $cell_name for key -cell_name. Value contains invalid character!"
    #echodebug "Value must not contain spaces or `/\*,:;=+?|<>_%'\''"[]#$^{}()"
    BADPARM=true;
  else 
    echodebug "Valid value $cell_name for key -cell_name."
  fi
 
}

check_node_name(){
  #names must start and end with alpha, number, period, dash or underscore
  #
  # one way to do it using sed 
  # sed '/[]\|/\|\\|*\|,\|:\|;\|=\|+\|?\||\|<\|>\|_\|%\|[\|#\|$\|^\|{\|}\|(\|)\| \|"\|'\'']/d'
  #
  # better way using sed but lets the \ thru
  # don't print if string contains anything other than alpha, numeric, ., _, -
  if [ -z `$ECHOE $node_name | sed -n '/[^[:alnum:]\|.\|_\|-]/!p'` ]; then 
    echodebug "ERROR: Invalid value $node_name for key -node_name. Value contains invalid character!"
    #echodebug "Value must not contain spaces or `/\*,:;=+?|<>_%'\''"[]#$^{}()"
    BADPARM=true;
  else 
    echodebug "Valid value $node_name for key -node_name."
  fi
 
}

check_dmgr_federate(){
  case $dmgr_federate in 
    true | false) 
      echodebug "Valid value $dmgr_federate for key -dmgr_federate."
      ;;
    *) 
      echodebug "ERROR: Invalid value $dmgr_federate for key -dmgr_federate."
      echodebug "Valid values are true or false"
      BADPARM=true;
      ;;
  esac

}

check_dmgr_host(){
  #basic check according to RFC 952 [a-zA-Z0-9.-[]:]
  # this check is far from exhaustive
  if [ -z `$ECHOE $dmgr_host | sed -n '/ /!p'` ]; then 
    echodebug "ERROR: value $dmgr_host contains a white space character!"
    BADPARM=true
  elif [ -z `$ECHOE $dmgr_host | sed -n '/[^][A-Za-z0-9:./-]/!p'` ]; then 
    echodebug "ERROR: value $dmgr_host for key -dmgr_host contains an invalid character."
    BADPARM=true
  else 
    echodebug "Valid value $dmgr_host for key -dmgr_host." 
  fi

}

check_dmgr_conntype(){
  case $dmgr_conntype in 
    SOAP | RMI | IPC) 
      echodebug "Valid value $dmgr_conntype for key -dmgr_conntype."
      ;;
    *) 
      echodebug "ERROR: Invalid value $dmgr_conntype for key -dmgr_conntype."
      echodebug "Valid values are SOAP, RMI, or IPC."
      BADPARM=true;
      ;;
  esac

}

check_dmgr_jmxport(){
  if [ -z `$ECHOE $dmgr_jmxport | sed -n '/[^0123456789]/!p'` ]; then 
    echodebug "ERROR: Invalid value $dmgr_jmxport for key -dmgr_jmxport. Value contains non-numeric character!"
    BADPARM=true;
  else
    if [[ $dmgr_jmxport -ge 0 && $dmgr_jmxport -le 65536  ]]; then 
      echodebug "Valid value $dmgr_jmxport for key -dmgr_jmxport."
    else
      echodebug "ERROR: Invalid value $dmgr_jmxport for key -dmgr_jmxport. Port range 0-65535."
      BADPARM=true;
    fi
  fi

  
}

check_dmgr_userid(){
  # on linux the valid character set for usernames is defined in /etc/login.defs
  # CHARACTER_SET = [A-Za-z_][A-Za-z0-9_-.]*[A-Za-z0-9_-.$]
  #
  # not sure about other operating systems so just warn if username contains values other than CHARACTER_SET

  if [ -z `$ECHOE $dmgr_userid | sed -n '/ /!p'` ]; then 
    echodebug "ERROR: value $dmgr_userid contains a white space character!"
    BADPARM=true
  elif [ -z `$ECHOE $dmgr_userid | sed -n '/[[a-zA-Z_][A-Za-z0-9_-.]*]/!p'` ]; then 
    echodebug "ERROR: value $dmgr_userid for key -dmgr_userid starts with non-alpha character"
    BADPARM=true
  elif [ -z `$ECHOE $dmgr_userid | sed -n '/[^[:alnum:]\|'.'\|'_'\|'-']/!p'` ]; then 
    echodebug "WARNING: value $dmgr_userid for key -dmgr_userid may contain invalid character."
  else 
    echodebug "Valid value $dmgr_userid for key -dmgr_userid."
  fi
}

check_dmgr_password(){
  echodebug "Value $dmgr_password is not checked for key -dmgr_password."

}

check_jmgr_host(){
  #basic check according to RFC 952 [a-zA-Z0-9.-[]:]
  # this check is far from exhaustive
  if [ -z `$ECHOE $jmgr_host | sed -n '/ /!p'` ]; then 
    echodebug "ERROR: value $jmgr_host contains a white space character!"
    BADPARM=true
  elif [ -z `$ECHOE $jmgr_host | sed -n '/[^][A-Za-z0-9:./-]/!p'` ]; then 
    echodebug "ERROR: value $jmgr_host for key -jmgr_host contains an invalid character."
    BADPARM=true
  else 
    echodebug "Valid value $jmgr_host for key -jmgr_host." 
  fi

}

check_jmgr_register(){
  case $jmgr_register in 
    true | false) 
      echodebug "Valid value $jmgr_register for key -jmgr_register."
      ;;
    *) 
      echodebug "ERROR: Invalid value $jmgr_register for key -jmgr_register."
      echodebug "Valid values are true or false"
      BADPARM=true;
      ;;
  esac
}

check_jmgr_httpport(){
  if [ -z `$ECHOE $jmgr_httpport | sed -n '/[^0123456789]/!p'` ]; then 
    echodebug "ERROR: Invalid value $jmgr_httpport for key -jmgr_httpport. Value contains non-numeric character!"
    BADPARM=true;
  else
    if [[ $jmgr_httpport -ge 0 && $jmgr_httpport -le 65536  ]]; then 
      echodebug "Valid value $jmgr_httpport for key -jmgr_httpport."
    else
      echodebug "ERROR: Invalid value $jmgr_httpport for key -jmgr_httpport. Port range 0-65535."
      BADPARM=true;
    fi
  fi

}

check_jmgr_userid(){
  # on linux the valid character set for usernames is defined in /etc/login.defs
  # CHARACTER_SET = [A-Za-z_][A-Za-z0-9_-.]*[A-Za-z0-9_-.$]
  #
  # not sure about other operating systems so just warn if username contains values other than CHARACTER_SET

  if [ -z `$ECHOE $jmgr_userid | sed -n '/ /!p'` ]; then 
    echodebug "ERROR: value $jmgr_userid contains a white space character!"
    BADPARM=true
  elif [ -z `$ECHOE $jmgr_userid | sed -n '/[[a-zA-Z_][A-Za-z0-9_-.]*]/!p'` ]; then 
    echodebug "ERROR: value $jmgr_userid for key -jmgr_userid starts with non-alpha character"
    BADPARM=true
  elif [ -z `$ECHOE $jmgr_userid | sed -n '/[^[:alnum:]\|'.'\|'_'\|'-']/!p'` ]; then 
    echodebug "WARNING: value $jmgr_userid for key -jmgr_userid may contain invalid character."
  else 
    echodebug "Valid value $jmgr_userid for key -jmgr_userid."
  fi
}

check_jmgr_password(){
  echodebug "Value $jmgr_password is not checked for key -jmgr_password."

}

check_profile_num(){
  case $profile_num in 
    1 | 2 | 3 | 4) 
      echodebug "Valid value $profile_num for key -profile_num."
      ;;
    *) 
      echodebug "ERROR: Invalid value $profile_num for key -profile_num."
      echodebug "Valid values are 1, 2, 3, or 4."
      BADPARM=true;
      ;;
  esac

}

check_autostart(){
  case $autostart in 
    true | false) 
      echodebug "Valid value $autostart for key -autostart"
      ;;
    *) 
      echodebug "ERROR: Invalid value $autostart for key -autostart."
      echodebug "Valid values are true or false"
      BADPARM=true;
      ;;
  esac

}

# test valid and invalid names
unitTest() {

  #valid type values default | custom | dmgr | jmgr | adminagent | cell | none
  type=default
  check_type
  type=custom
  check_type
  type=dmgr
  check_type
  type=jmgr
  check_type
  type=adminagent
  check_type
  type=cell
  check_type
  type=none
  check_type
  #invalid type values
  type=bogustype
  check_type
  type=
  check_type
  
  #valid cell_name values
  cell_name="prisonblock"
  check_cell_name
  cell_name="prison.block"
  check_cell_name
  cell_name="prison_block"
  check_cell_name
  cell_name="prison-block"
  check_cell_name

  #invalid cell_name values
  #invalid cell_name values
  cell_name=""
  check_cell_name
  cell_name="prison block"
  check_cell_name
  cell_name="prison!block"
  check_cell_name
  cell_name="prison@block"
  check_cell_name
  cell_name="prison#block"
  check_cell_name
  cell_name='prison$block'
  check_cell_name
  cell_name="prison%block"
  check_cell_name
  cell_name="prison^block"
  check_cell_name
  cell_name='prison&block'
  check_cell_name
  cell_name="prison*block"
  check_cell_name
  cell_name="prison(block"
  check_cell_name
  cell_name="prison)block"
  check_cell_name
  cell_name="prison{block"
  check_cell_name
  cell_name="prison}block"
  check_cell_name
  cell_name="prison[block"
  check_cell_name
  cell_name="prison]block"
  check_cell_name
  cell_name="prison:block"
  check_cell_name
  cell_name="prison;block"
  check_cell_name
  cell_name="prison;block"
  check_cell_name
  cell_name='prison/block'
  check_cell_name
  cell_name='prison\block'
  check_cell_name
  cell_name="prison+block"
  check_cell_name
  cell_name="prison=,block"
  check_cell_name

#valid node_name values
  node_name="prisonblock"
  check_node_name
  node_name="prison.block"
  check_node_name
  node_name="prison_block"
  check_node_name
  node_name="prison-block"
  check_node_name

  #invalid node_name values
  node_name=""
  check_node_name
  node_name="prison block"
  check_node_name
  node_name="prison!block"
  check_node_name
  node_name="prison@block"
  check_node_name
  node_name="prison#block"
  check_node_name
  node_name='prison$block'
  check_node_name
  node_name="prison%block"
  check_node_name
  node_name="prison^block"
  check_node_name
  node_name="prison&block"
  check_node_name
  node_name="prison*block"
  check_node_name
  node_name="prison(block"
  check_node_name
  node_name="prison)block"
  check_node_name
  node_name="prison{block"
  check_node_name
  node_name="prison}block"
  check_node_name
  node_name="prison[block"
  check_node_name
  node_name="prison]block"
  check_node_name
  node_name="prison:block"
  check_node_name
  node_name="prison;block"
  check_node_name
  node_name="prison;block"
  check_node_name
  node_name='prison/block'
  check_node_name
  node_name='prison\block'
  check_node_name
  node_name="prison+block"
  check_node_name
  node_name="prison=,block"
  check_node_name

  #valid dmgr_federate values
  dmgr_federate=true
  check_dmgr_federate
  dmgr_federate=false
  check_dmgr_federate
  #Invalid dmgr_federate values
  dmgr_federate=
  check_dmgr_federate
  dmgr_federate=tr
  check_dmgr_federate
  dmgr_federate=fish
  check_dmgr_federate

  #valid dmgr_host values
  dmgr_host="10.1.1.1"
  check_dmgr_host
  dmgr_host="domain.name.com"
  check_dmgr_host  
  dmgr_host="[::/128]"
  check_dmgr_host
  dmgr_host="[fe80::/10]"
  check_dmgr_host
  dmgr_host="[2001:0db8::1428:57ab]"
  check_dmgr_host
  dmgr_host="hostname"
  check_dmgr_host
  #invalid dmgr_host values
  dmgr_host=""
  check_dmgr_host
  dmgr_host="10.1.1.#"
  check_dmgr_host
  dmgr_host="host%name"
  check_dmgr_host
  dmgr_host="fe80<>::80"
  check_dmgr_host
  dmgr_host="_hostname"
  check_dmgr_host
  dmgr_host="{hostname}"
  check_dmgr_host

  #valid dmgr_conntype values SOAP | RMI | IPC
  dmgr_conntype=SOAP
  check_dmgr_conntype
  dmgr_conntype=RMI
  check_dmgr_conntype
  dmgr_conntype=IPC
  check_dmgr_conntype
  #invalid dmgr_conntype values
  dmgr_conntype=
  check_dmgr_conntype
  dmgr_conntype=soap
  check_dmgr_conntype

  #valid dmgr_jmxport values 0-65535
  dmgr_jmxport=0
  check_dmgr_jmxport
  dmgr_jmxport=65535
  check_dmgr_jmxport
  dmgr_jmxport=1800
  check_dmgr_jmxport
  dmgr_jmxport=21
  check_dmgr_jmxport
  #invalid dmgr_jmxport values
  dmgr_jmxport=
  check_dmgr_jmxport
  dmgr_jmxport=-1
  check_dmgr_jmxport
  dmgr_jmxport=70000
  check_dmgr_jmxport

  #valid dmgr_userid values
  dmgr_userid="username"
  check_dmgr_userid
  dmgr_userid="user_name"
  check_dmgr_userid
  dmgr_userid="user-name"
  check_dmgr_userid
  dmgr_userid="user.name"
  check_dmgr_userid
  dmgr_userid="user9name"
  check_dmgr_userid
  #invalid dmgr_userid values
  dmgr_userid=""
  check_dmgr_userid
  dmgr_userid="user name"
  check_dmgr_userid
  dmgr_userid="user%name"
  check_dmgr_userid
  dmgr_userid=")user-name"
  check_dmgr_userid

  #dmgr_password is not checked
  dmgr_password="password"
  check_dmgr_password

  #valid jmgr_host values
  jmgr_host="10.1.1.1"
  check_jmgr_host
  jmgr_host="domain.name.com"
  check_jmgr_host  
  jmgr_host="[::/128]"
  check_jmgr_host
  jmgr_host="[fe80::/10]"
  check_jmgr_host
  jmgr_host="[2001:0db8::1428:57ab]"
  check_jmgr_host
  jmgr_host="hostname"
  check_jmgr_host
  #invalid jmgr_host values
  jmgr_host=""
  check_jmgr_host
  jmgr_host="10.1.1.#"
  check_jmgr_host
  jmgr_host="host%name"
  check_jmgr_host
  jmgr_host="fe80<>::80"
  check_jmgr_host
  jmgr_host="_hostname"
  check_jmgr_host
  jmgr_host="{hostname}"
  check_jmgr_host

  #valid jmgr_register values
  jmgr_register=true
  check_jmgr_register
  jmgr_register=false
  check_jmgr_register
  #Invalid jmgr_register values
  jmgr_register=
  check_jmgr_register
  jmgr_register=tr
  check_jmgr_register
  jmgr_register=fish
  check_jmgr_register

  #valid jmgr_httpport values 0-65535
  jmgr_httpport=0
  check_jmgr_httpport
  jmgr_httpport=65535
  check_jmgr_httpport
  jmgr_httpport=1800
  check_jmgr_httpport
  jmgr_httpport=21
  check_jmgr_httpport
  #invalid jmgr_httpport values
  jmgr_httpport=
  check_jmgr_httpport
  jmgr_httpport=-1
  check_jmgr_httpport
  jmgr_httpport=70000
  check_jmgr_httpport

  #valid jmgr_userid values
  jmgr_userid="username"
  check_jmgr_userid
  jmgr_userid="user_name"
  check_jmgr_userid
  jmgr_userid="user-name"
  check_jmgr_userid
  jmgr_userid="user.name"
  check_jmgr_userid
  jmgr_userid="user9name"
  check_jmgr_userid
  #invalid jmgr_userid values
  jmgr_userid=""
  check_jmgr_userid
  jmgr_userid="user name"
  check_jmgr_userid
  jmgr_userid="user%name"
  check_jmgr_userid
  jmgr_userid=")user-name"
  check_jmgr_userid

  #jmgr_password is not checked
  jmgr_password="password"
  check_jmgr_password

  #valid profile_num values 1,2,3,4
  profile_num=1
  check_profile_num
  profile_num=2
  check_profile_num
  profile_num=3
  check_profile_num
  profile_num=4
  check_profile_num
  #invalid profile_num values
  profile_num=
  check_profile_num
  profile_num=5
  check_profile_num
  profile_num=a
  check_profile_num

  #valid autostart values true | false
  autostart=true
  check_autostart
  autostart=false
  check_autostart
  #invalid autostart values
  autostart=
  check_autostart
  autostart=dog
  check_autostart
  
}

#expand the positional parameters
PARAMETERS=$@

while [ $# -ne 0 ]
do
	case $1 in
		-type)
		type=$2
                check_`echo $1 | sed s/'-'//`
		;;
		-cell_name)
		cell_name=$2
                check_`echo $1 | sed s/'-'//`
		;;
		-node_name)
		node_name=$2
                check_`echo $1 | sed s/'-'//`
		;;
		-dmgr_federate)
		dmgr_federate=$2
                check_`echo $1 | sed s/'-'//`
		;;
		-dmgr_host)
		dmgr_host=$2
                check_`echo $1 | sed s/'-'//`
		;;
		-dmgr_jmxport)
		dmgr_jmxport=$2
                check_`echo $1 | sed s/'-'//`
		;;
		-dmgr_conntype)
		dmgr_conntype=$2
                check_`echo $1 | sed s/'-'//`
		;;
		-dmgr_user)
		dmgr_userid=$2
                check_`echo $1 | sed s/'-'//`
		;;
		-dmgr_pass)
		dmgr_password=$2
                check_`echo $1 | sed s/'-'//`
		;;
		-jmgr_host)
		jmgr_host=$2
                check_`echo $1 | sed s/'-'//`
		;;
                -jmgr_register)
		jmgr_register=$2
                check_`echo $1 | sed s/'-'//`
		;;
		-jmgr_httpport)
		jmgr_httpport=$2
                check_`echo $1 | sed s/'-'//`
		;;
		-jmgr_userid)
		jmgr_userid=$2
                check_`echo $1 | sed s/'-'//`
		;;
		-jmgr_password)
		jmgr_password=$2
                check_`echo $1 | sed s/'-'//`
		;;
		-profile_num)
		profile_num=$2
                check_`echo $1 | sed s/'-'//`
		;;
                -autostart)
		autostart=$2
                check_`echo $1 | sed s/'-'//`
		;;
                -unitTest)
                unittest=$2
                unitTest
		;;
		*)
                echodebug "ERROR: Unknown key $1"
                BADPARM=true
		;;
	esac
	shift 2
done

if [ $? -gt 0  ]; then
  echo `date` " WebSphere Virtual Appliance configuration failed." > /etc/motd
  echo `date` " Please check /opt/IBM/AE/AR/ConfigWAS.traceout" >> /etc/motd
  exit $?
elif [ $BADPARM == true ]; then  
  echo `date` " WebSphere Virtual Appliance configuration failed." > /etc/motd
  echo `date` " Please check /opt/IBM/AE/AR/ConfigWAS.traceout" >> /etc/motd
  exit 1
else 
  exit 0
fi 
