################################################################################
#
# File:         opcvmsprocs
# Description:  OpenVMS ITO Software Maintenance procedures 
# Language:     Bourne Shell
# Package:      HP OpenView IT/Operations
#
# (c) Copyright Hewlett-Packard Co. 1993 - 2004
#
################################################################################

VMS_FTP_FILE=${OPC_TOPC}/vms_ftp_file
VMS_FTP_OUTPUT=${OPC_TOPC}/vms_ftp_output
VMS_GREP_OUTPUT=${OPC_TOPC}/vms_grep_output

#==============================================================================
#
#  DESCRIPTION
#    This file should be read by all OpenVMS shell scripts which
#    are part of ITO software maintenance for Managed Nodes and execute
#    on Management Server.
#
#  DEBUGGING
#    There are environment variables, which control remote debugging:
#
#      OPC_RDEBUG  .. Additional flags, passed to remote /bin/sh -- x, vx, ...
#                     Do not add initial `-'!
#      OPC_ROUTPUT .. File name where standard error output of remote commands
#                     will be redirected.
#                     Remote command's stderr will be merged on stdout
#                     when OPC_ROUTPUT is empty.
#                     
#    For details, see lines marked with #DEBUG comment.
#

#==============================================================================
# DESCRIPTION: This tests if ftp is kerborized and the remote ftpd understands
#              Kerberos, too. The -P option is needed if
#              the other agent does not understand Kerberos.
#
# PARAMETERS: None
#
# EXIT CODE
#    0 .. not kerborized
#    1 .. kerborized

opcvmsftp_kerborized()
{
  #Check for kerborized ftp
  rm -f ${OPC_TOPC}/ftp.out
  ftp -n <<-EOF >>/dev/null 2>${OPC_TOPC}/ftp.out
    open ${OPC_NODE}
    bye
EOF
  THIS_OS=`uname`
  if [ "${THIS_OS}" = "SunOS" ]
     then
       FTP_OUT=`/usr/xpg4/bin/grep -i -E "kerberos|kcmd" ${OPC_TOPC}/ftp.out`
     else
       FTP_OUT=`grep -i -E "kerberos|kcmd" ${OPC_TOPC}/ftp.out`
     fi
  if [ -n "${FTP_OUT}" ]
  then
    # Error in ftp output. Since 'kerberos' is in the output, bypass checks.
    oi_trace opc 648 ${OPC_IP_ADDRESS}
    KERBEROS_FLAG="-P"
    export KERBEROS_FLAG
    return 1
  else
    KERBEROS_FLAG=""
    export KERBEROS_FLAG
    return 0
  fi
}     

#==============================================================================
#
#  DESCRIPTION: This tests that a file can be put to, and removed from 
#               the OpenVMS machine, which tests that the password is ok
#
#  PARAMETERS: None
#
#  ENVIRONMENT
#
#  EXIT CODE
#      0 .. size of VMS_FTP_OUTPUT file is zero
#      1 .. size of VMS_FTP_OUTPUT file is bigger than zero==> OK
#      2 .. VMS_FTP_OUTPUT file does not exist
#      3 .. error
#

opcvmsftp_chk_access()
{
  if [ $# -ne 1 ]
  then
    #  
    #
    a=""
  fi

OPC_INST_USER="SYSTEM"
export OPC_INST_USER
  # First time check sets KERBEROS_FLAG
  opcvmsftp_kerborized    

#create test file
  OPC_TEST_FILENAME="opc_test_access_file.txt"
  rm -f ${OPC_TOPC}/${OPC_TEST_FILENAME}
  echo This is file is only testing and can be deleted. >> ${OPC_TOPC}/${OPC_TEST_FILENAME} 
  rm -f ${OPC_TOPC}/ftp.out
  ftp ${KERBEROS_FLAG} -n <<-EOF >${OPC_TOPC}/ftp.out
    open ${OPC_NODE}
    user "${OPC_INST_USER}" ${OPC_PASSWD}
    binary
    put ${OPC_TOPC}/${OPC_TEST_FILENAME} ${OPC_TEST_FILENAME}
    del ${OPC_TEST_FILENAME};
    close
    bye
EOF
  # after this ftp session there must NOT be something in the output
  # but because the -n option pushes the stderr output into the 
  # file. If the file is greater than zero bytes assume that an error 
  # has occurred. This is caused by the client ftp so other versions of ftp
  # servers on the OpenVMS side will not cause problems.
  #
  rm -f ${OPC_TOPC}/${OPC_TEST_FILENAME}
  if [ -f ${OPC_TOPC}/ftp.out ]
  then
    #   VMS_FTP_OUTPUT file exists - its size has to be detected
    #
    if [ -s ${OPC_TOPC}/ftp.out ]
    then
# DDTS 09965 Brian
# no need to show the ftp output in this case.
#      oi_show_file ${OPC_TOPC}/ftp.out
      return 3
    else
      #   It really seems to be ok
      #
      return 1
    fi
  else
    # ftp output file does not exist.
    return 2
  fi
}

#==============================================================================
#
#  DESCRIPTION
#
#  PARAMETERS
#
#  ENVIRONMENT
#
#  EXIT CODE
#      0 .. size of VMS_FTP_OUTPUT file is zero
#      1 .. size of VMS_FTP_OUTPUT file is bigger than zero
#      2 .. VMS_FTP_OUTPUT file does not exist
#      3 .. error
#      4 .. wrong number of parameters
#
opcvmsftp()
{
  #  input parameters
  #
  if [ $# -lt 1 -o $# -gt 12 ]
  then
    #   usage error
    #
    oi_error "opc" 511 "opcvmsftp"
    exit 4
  fi 

  # sets KERBEROS_FLAG if necessary
  opcvmsftp_kerborized

OPC_INST_USER="SYSTEM"
export OPC_INST_USER
  rm -f ${OPC_TOPC}/ftp.out
  ftp ${KERBEROS_FLAG} -n <<-EOF >${OPC_TOPC}/ftp.out
    open ${OPC_NODE} 
    user "${OPC_INST_USER}" ${OPC_PASSWD}
    binary 
    $1 
    $2
    $3
    $4
    $5
    $6
    $7
    $8
    $9
    ${10}
    ${11}
    ${12}
    close 
    quit
EOF

  FTP_OUT=`cat ${OPC_TOPC}/ftp.out`
  if [ -n "${FTP_OUT}" ]
 
  then
    #   Problems transferring package via ftp...
    #
    oi_show_file ${OPC_TOPC}/ftp.out
    rm -f ${OPC_TOPC}/ftp.out
    return 3
  else
    #   It really seems to be ok
    #
    return 1
  fi
}

#==============================================================================
#
#  DESCRIPTION
#
#  PARAMETERS
#
#  ENVIRONMENT
#
#  EXIT CODE
#      0 .. task successfully done
#      1 .. problems with opc_rexec utility; user give up.
#
opcvmspwget()
{
    #   try with remsh(1) first by using REXEC_SCRIPT callout script,
    #   set environment variables before script is called
    #
    OPC_PASSWD_REQ=0; export OPC_PASSWD_REQ

    #   acquire password and set environment variables before REXEC_SCRIPT
    #   is called
    #   do this in endless loop until REXEC_SCRIPT works or user gives up
    #
    ASK_USER=`echo "$OPC_INST_USER" | sed -e 's/ /_/g'`
    ASK_MSG_NUM="374 \"${ASK_USER}\" ${OPC_NODE}"

    while true
    do
        if [ -z "$OPC_PASSWD" ]
        then
          stty -echo
          oi_prompt_and_ask "opc" 381 "${ASK_USER}" "${OPC_NODE}" ${ASK_MSG_NUM}
          stty echo
          echo
          if [ "${OPC_ANSWER}" = "${OPC_OIANSWER_DEF}" ]
          then
              #   user did not answer
              #
              OPC_PASSWD=""; export OPC_PASSWD
              RETURN_VAL=1
              break
          fi
          OPC_PASSWD="${OPC_ANSWER}"
        fi
        opcvmsftp_chk_access ""
        if [ $? -eq 1 ]
        then
            #   the return code is ok - that means that the ftp connection 
            #   works and the password is ok.
            #
            RETURN_VAL=0
            break
        fi

        #   password is wrong
        #   ask, if root password should be re-entered once more
        #
        oi_verbose opc 034 ${OPC_NODE}
        echo
        oi_prompt_and_ask "opc" 191 "${OPC_PLATFORM}" "${OPC_NODE}" \
                                313 ${OPC_NODE}
        case "${OPC_ANSWER}" in
            [${OPC_YES_LIT}]*)
            #   try again with new password
            #
		ASK_USER=`echo "$OPC_INST_USER" | sed -e 's/ /_/g'`
                ASK_MSG_NUM="375 \"${ASK_USER}\""
		OPC_PASSWD=""
            ;;
            *)
            #   user give up
            #
                OPC_PASSWD=""; export OPC_PASSWD
                RETURN_VAL=1
                break
            ;;
        esac

    done

    rm -f VMS_FTP_FILE VMS_FTP_OUTPUT
    unset ASK_MSG_NUM REM_CMD
    if [ ${RETURN_VAL} -eq 0 ]
    then
        unset RETURN_VAL
        return 0
    else
        unset RETURN_VAL
        return 1
    fi

}

#==============================================================================
#
#  DESCRIPTION
#
#  PARAMETERS
#
#  ENVIRONMENT
#
#  EXIT CODE
#
opcvmspwget_account()
{
	if [ ! -z "$OPC_PASSWD" ]
	then
		WNT_ACCOUNT_PASSWD="${OPC_PASSWD}"; export WNT_ACCOUNT_PASSWD
		return 0
	fi
    while true
    do
        ASK_MSG_NUM="370 ${OPC_NODE}"
        echo
        stty -echo
        oi_prompt_and_ask "opc" 381 "SYSTEM" "${OPC_NODE}" ${ASK_MSG_NUM}
        stty echo
        echo

        if [ "${OPC_ANSWER}" = "${OPC_OIANSWER_DEF}" ]
        then
            #   user did not answer
            #
            WNT_ACCOUNT_PASSWD=""; export WNT_ACCOUNT_PASSWD
            RETURN_VAL=1
            break
        fi
        WNT_ACCOUNT_PASSWD="${OPC_ANSWER}"; export WNT_ACCOUNT_PASSWD

        ASK_MSG_NUM="371"
        stty -echo
        oi_prompt_and_ask "opc" 381 "SYSTEM" "${OPC_NODE}" ${ASK_MSG_NUM}
        stty echo
        echo

        if [ "${OPC_ANSWER}" = "${WNT_ACCOUNT_PASSWD}" ]
        then
            #   the password was confirmed - use it
            #
            RETURN_VAL=0
            break
        fi

        #   password is wrong
        #   ask, if root password should be re-entered once more
        #
        oi_verbose opc 372 ${OPC_NODE}
    done

    rm -f VMS_FTP_FILE VMS_FTP_OUTPUT
    unset ASK_MSG_NUM REM_CMD
    if [ ${RETURN_VAL} -eq 0 ]
    then
        unset RETURN_VAL
        return 0
    else
        unset RETURN_VAL
        return 1
    fi
}
#==============================================================================
#
#  DESCRIPTION: This determines the version of OpenVMS running on the
#               managed node.
#
#  PARAMETERS: None
#
#  ENVIRONMENT
#
#  EXIT CODE
#      0 .. version successfully obtained
#      1 .. error obtaining version
#

opcvms_version()
{
  # First time check sets KERBEROS_FLAG
  opcvmsftp_kerborized

  rm -f ${OPC_TOPC}/ftp.out
  ftp ${KERBEROS_FLAG} -n -v <<-EOF >${OPC_TOPC}/ftp.out
    open ${OPC_NODE}
    user "${OPC_INST_USER}" ${OPC_PASSWD}
    system
    close
    bye
EOF

	# Look for OS system version

FTP_OUT=`grep "OpenVMS" ${OPC_TOPC}/ftp.out`
if [ $? -eq 1 ]
then
	# Could not obtain version
	VMS_VERSION=""
	export VMS_VERSION
	return 1
fi

rm -f ${OPC_TOPC}/ftp.out

VMS_VERSION=`echo ${FTP_OUT} | cut -d " " -f 4`
export VMS_VERSION

return 0
}
