#! /bin/sh

# file: repairlinux
# 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: repairlinux <snmpread> <snmptrap> <trustcert> <asmpswd>
#        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
#        <asmpswd> is the adminstrative password for legacy
#                    System Management Home Page agents
#        Note that each of the arguments can be a null string ("") if
#        its corresponding setup is not needed
#

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

RHSNMPDCONF=/etc/snmp/snmpd.conf
SUSNMPDCONF=/usr/share/snmp/snmpd.conf

HPASMINI=/var/spool/compaq/wbem/homepage/cpqhmmdx.ini
HPSMHXML=/opt/hp/hpsmh/conf/smhpd.xml
HPASMCERT=/var/spool/compaq/wbem/certs
HPSMHCERT=/opt/hp/hpsmh/certs
#
# function to log messages
# usage: msg message
#        where message is the message to display
#
msg ()
{
  echo $1
}


# make sure we have 4 arguments, if not display usage message
# and terminate
if [ $# -ne 4 ]
then
  echo "><usage: repairlinux <snmpread> <snmptrap> <trustcert> <asmpswd>"
  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 "><       <asmpswd> is the adminstrative password for legacy"
  echo "><                   System Management Home Page agents"
  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
ASMPSWD=$4

# 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

    # determine location of snmp conf file for the running linux
    SNMPDCONF=$RHSNMPDCONF
    if [ -f $SUSNMPDCONF ]
    then
      SNMPDCONF=$SUSNMPDCONF
    fi
    
    # check to see if snmp is installed by checking to see if the snmpd.conf
    # file exist
    if [ -f $SNMPDCONF ]
    then
    
      # determine whether snmpd is running or not
      SNMPRESTART=`ps -e | grep snmpd | grep -v grep | wc -l`
  
      # we need to do some work, but first we need to shutdown snmpd
      # if it is running
      if [ $SNMPRESTART -ne 0 ]
      then
        msg "><Stopping snmpd..."
        /etc/init.d/snmpd stop
      else
        msg "><snmpd 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 ^rocommunity $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 rocommunity $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 ^trapsink $SNMPDCONF | grep $SNMPTRAP | wc -l`
        if [ $CK -eq 0 ]
        then
          # need to add trap destination address
          msg "><Adding trap destination address $SNMPTRAP"
          echo trapsink $SNMPTRAP >> $SNMPDCONF
        else
          msg "><trap destination address $SNMPTRAP already in $SNMPDCONF"
        fi
      fi
    
      # restart snmpd agent if it was running
      if [ $SNMPRESTART -ne 0 ]
      then
        msg "><Restarting snmpd..."
        /etc/init.d/snmpd start
      fi
    else
      msg "><SNMP product is not installed"
    fi
  fi



# set admin password and trust for legacy agents
if [ "X$ASMPSWD" != "X" -o "X$TRUSTCERT" != "X" ]
then

  # Is HP Advanced Server Management installed?
  if [ -f $HPASMINI ]
  then
    # determine if hp server management drivers and agents daemon is
    # running or not
    HPASMRESTART=`/etc/init.d/hpasm status | grep running | grep -v grep | wc -l`

    # if hp server management drivers and agents daemon is running
    # shutdown it down
    if [ $HPASMRESTART -ne 0 ]
    then
      msg "><Stopping HP Server Management Drivers and Agents"
      /etc/init.d/hpasm stop
    else
      msg "><HP Server Management Drivers and Agents is not active"
    fi

    # setup trust certificate for HP Advanced Server Management 
    # Drivers and Agents?
    if [ "X$TRUSTCERT" != "X" ]
    then
      # copy new certificate to HP Legacy SMH cert folder
      msg "><Copying $TRUSTCERT to $HPASMCERT"
      cp -f $TRUSTCERT $HPASMCERT

	  # change the trustmode in the cpqhmmdx.ini file to be TrustByCert
      if [ -f $HPASMINI ]; then
         l_c=$(wc -l $HPASMINI | awk '{print $1}')
         for rline in $(seq 0 $l_c)
         do
             read rline
             uline=$(echo $rline | tr '[:lower:]' '[:upper:]')
             elmvar=$(echo $uline | cut -f1 -d '=')
             elmval=$(echo $uline | cut -f2 -d '=')
             if [[ $elmvar == "TRUSTMODE"  ]]; then
               tempvar=$(echo $rline | cut -f2 -d '=')
               etrustmode=$(echo $tempvar | tr -d '[:cntrl:]')
             fi
         done <$HPASMINI
      fi
      # No TrustMode case
      if [[ $etrustmode == "" ]]; then
        echo "TrustMode=TrustByCert" | cat >> $HPASMINI
      fi
      # TrustByName case
      if [[ $etrustmode == "TrustByName" ]]; then
        cat $HPASMINI | sed 's/TrustByName/TrustByCert/g' > $HPASMINI
      fi
      # TrustByAll case
      if [[ $etrustmode == "TrustByAll" ]]; then
        cat $HPASMINI | sed 's/TrustByAll/TrustByCert/g' > $HPASMINI
      fi
    fi

    # set administrator password of HP Advanced Server Management agents
    if [ "X$ASMPSWD" != "X" ]
    then
      msg "><Setting administrator password for HP Server Management Drivers and Agents"
      /var/opt/mx/tmp/hpwebsetup -pA:$ASMPSWD -f
    fi

    # restart hp server management drivers and agents if necessary
    if [ $HPASMRESTART -ne 0 ]
    then
      msg "><Restarting HP Server Management Drivers and Agents"
      /etc/init.d/hpasm start
    fi

  else
    msg "><HP Server Management Drivers and Agents, is not installed."
  fi
fi


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

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

    # determine if System Management Home Page is active
    SMHRESTART=`/etc/init.d/hpsmhd status |grep running | grep -v grep | wc -l`

    # shutdown smh if necessary
    if [ $SMHRESTART -ne 0 ]
    then
      msg "><Stopping System Management Home Page"
      /etc/init.d/hpsmhd 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 $HPSMHCERT"
    cp -f $TRUSTCERT $HPSMHCERT

    # change the trustmode in the smhpd.xml file to be TrustByCert
    # SMH - Just hammer it...
    if [ -f $HPSMHXML ]; then
      cat $HPSMHXML | sed 's/TrustByName/TrustByCert/g' > $HPSMHXML
      cat $HPSMHXML | sed 's/TrustByAll/TrustByCert/g' > $HPSMHXML
      chown 79:79 $HPSMHXML
      chmod 660 $HPSMHXML
    fi
    
    # restart smh if necessary
    if [ $SMHRESTART -ne 0 ]
    then
      msg "><Restarting System Management Home Page"
      /etc/init.d/hpsmhd start
    fi
  else
    msg "><System Management Home Page is not installed"
  fi
fi

