#! /bin/sh

# file: repairhpux
# desc: provide support functions to update or install the read community
#       string and the trap destination address for snmp and to update
#       or install the trust certificate for system management home page
#
# Copyright (c) 2005 Hewlett-Packard Development Company, L.P.
#
# usage: repairhpux <snmpread> <snmptrap> <trustcert>
#        where
#        <snmpread> is the snmp read community string
#        <snmptrap> is the trap destination address
#        <trustcert> is the file name of the certificate
#                    for System Management Home Page
#        Note that each of the arguments can be a null string ("") if
#        its corresponding setup is not needed
#

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

SNMPDCONF=/etc/SnmpAgent.d/snmpd.conf

HPSMHXML=/opt/hpsmh/conf.common/smhpd.xml
HPSMHXMLTMP=/opt/hpsmh/conf.common/smhpd.sim

#
# function to log messages
# usage: msg message
#        where message is the message to display
#
msg ()
{
  echo $1
}


# make sure we have 3 arguments, if not display usage message
# and terminate
if [ $# -ne 3 ]
then
  echo "><usage: repairhpux <snmpread> <snmptrap> <trustcert>"
  echo "><       where"
  echo "><       <snmpread> is the snmp read community string"
  echo "><       <snmptrap> is the trap destination address"
  echo "><       <trustcert> is the file name of the trust certificate"
  echo "><                   for System Management Home Page"
  echo "><       Note that each of the arguments can be a null string (\"\") if"
  echo "><       its corresponding setup is not needed."
  exit 1
fi

# get the arguments
SNMPREAD=$1
SNMPTRAP=$2
TRUSTCERT=$3

# determine exact location where this script is located
# save current working subdirectory
SAVEDIR=`pwd`

# determine absolute path to locataion of this script
PATHNAME=`dirname $0`

# if path name is just a period then the user is already in
# the subtree where the script reside
if [ $PATHNAME = "." ]
then
  LOCATION=`pwd`
else
  # now check for full path name by noting if the first character
  # is a "/" or not
  echo $PATHNAME | grep '^/' > /dev/null
  if test $? -eq 0
  then
    # the full absolute path name found
    LOCATION=$PATHNAME
  else
    # need to form the full path name
    LOCATION="`pwd`/$PATHNAME"
  fi
fi

# set the working subdirectory
cd $LOCATION


# set up the snmp read community string and the trap destination
# address

# do we need to do any work for snmp
if [ "X$SNMPREAD" != "X" -o "X$SNMPTRAP" != "X" ]
then

  # check to see if snmp is installed by checking to see if the snmpd.conf
  # file exist
  if [ -f $SNMPDCONF ]
  then

    # determine whether snmpdm is running or not
    SNMPRESTART=`ps -e | grep snmpdm | grep -v grep | wc -l`

    # we need to do some work, but first we need to shutdown snmpdm
    # if it is running
    if [ $SNMPRESTART -ne 0 ]
    then
      msg "><Stopping snmpdm..."
      /sbin/init.d/SnmpMaster stop
    else
      msg "><snmpdm is not active"
    fi

    # setup read community string if necessary
    if [ "X$SNMPREAD" != "X" ]
    then

      # if the read string is not in snmpd.conf then add it
      match=0
      for val in $(grep ^get-community-name $SNMPDCONF)
      do
        # do we have a match
        if [ "X$SNMPREAD" = "X$val" ]
        then
          match=1
        fi
      done

      if [ $match -eq 0 ]
      then
        # need to add community string
        msg "><Adding read community string $SNMPREAD"
        echo get-community-name: $SNMPREAD >> $SNMPDCONF
      else
        msg "><Read community string $SNMPREAD already in $SNMPDCONF"
      fi
    fi

    # setup snmp trap destination address if necessary
    if [ "X$SNMPTRAP" != "X" ]
    then
      # if the trap destination address  is not in snmpd.conf then add it
      CK=`grep ^trap-dest $SNMPDCONF | grep $SNMPTRAP | wc -l`
      if [ $CK -eq 0 ]
      then
        # need to add community string
        msg "><Adding trap destination address $SNMPTRAP"
        echo trap-dest: $SNMPTRAP >> $SNMPDCONF
      else
        msg "><trap destination address $SNMPTRAP already in $SNMPDCONF"
      fi
    fi

    # restart snmp master agent if it was running
    # need to restart sub agents too  /usr/sbin/snmpd
    if [ $SNMPRESTART -ne 0 ]
    then
      msg "><Restarting snmpdm..."
      # start the master agent and its subagents
      for name in $( ls /sbin/SnmpAgtStart.d/S* )
      do
        SNMPFDDIAGENT=`echo $name | grep SnmpFddi4 | wc -l`
        if [ $SNMPFDDIAGENT -eq 0 ]
        then
          # display the agent start message
          $name start_msg
          # start the agent or subagent
          $name start
        else
          # restarting the SnmpFddi4 sub agent holds SSH session open,
          # preventing SIM from properly terminating the CRA task,
          # so schedule it to start 1 minute from now.
          SNMPFDDIAGENTNAME=`$name start_msg`
          msg "Delaying $SNMPFDDIAGENTNAME"
          echo "$name start" | at now + 1 minutes
        fi
      done
    fi
    msg "><Done with snmp changes"
  else
    msg "><snmp product is not installed"
  fi
fi



# need to setup trust certificate for System Management Home Page?
if [ "X$TRUSTCERT" != "X" ]
then

  # Is the System Management Home Page installed?
  if [ -f /opt/hpsmh/conf/smhpd.conf ]
  then

    # determine if System Management Home Page is active
    SMHRESTART=`ps -x -fu root |grep httpd | grep /opt/hpsmh/conf/smhpd.conf | wc -l`

    # shutdown smh if necessary
    if [ $SMHRESTART -ne 0 ]
    then
      msg "><Stopping System Management Home Page"
      /opt/hpsmh/lbin/hpsmh stop
    else
      msg "><System Management Home Page is not active"
    fi

    # copy new certificate to System Management Home Page cert folder
    msg "><Copying $TRUSTCERT to /var/opt/hpsmh/certs"
    cp -f $TRUSTCERT /var/opt/hpsmh/certs
    # change the trustmode in the smhpd.xml file to be TrustByCert
    # SMH - Just hammer it...
    if [ -f $HPSMHXML ]; then
      msg "><Changing Trust Mode to Trust By Certificate "
      # Copy the existing config file to a temporary file
      #cp -f $HPSMHXML $HPSMHXMLTMP #this does not always work if file is in use
      cat $HPSMHXML > $HPSMHXMLTMP

      # append new-line to temp file so sed processes all lines (it may be missing)
      echo "\n" >> $HPSMHXMLTMP

      # change trust mode to TrustByCert, working on temp config file
      cat $HPSMHXMLTMP | sed 's/TrustByName/TrustByCert/g' > $HPSMHXMLTMP
      cat $HPSMHXMLTMP | sed 's/TrustByAll/TrustByCert/g' > $HPSMHXMLTMP

      # restore temp config file to the original
      mv -f $HPSMHXMLTMP $HPSMHXML

      # reset owner, group, and permissions
      chown bin $HPSMHXML
      chgrp bin $HPSMHXML
      chmod 444 $HPSMHXML
    fi

    # restart smh if necessary
    if [ $SMHRESTART -ne 0 ]
    then
      msg "><Restarting System Management Home Page"
      /opt/hpsmh/lbin/hpsmh start
    fi
  else
    msg "><System Management Home Page is not installed"
  fi
fi
