#!/bin/sh

#
# Copyright 2004 Berbee Information Networks
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2,
# as published by the Free Software Foundation
#

CVSid='$Id$'

PDIRS=${PDIRS:-"${ROOT}/pub/tools/nessus-support:${ROOT}/usr/local/share/nessus-support:${ROOT}/usr/share/nessus-support"}
TODAY=$(date +'%Y-%m-%d')

for OPTION
do
  case "${OPTION}" in
    -*nou*) NOUPDATE=1
    ;;
    -*non*) NONAMES=1
    ;;
    -*noc*) NOCLASS=1
    ;;
  esac
done

if [ "${NOUPDATE}" ]
then
  echo "Not updating Nessus plugins."
else
  # kill the cached user password
  sudo -k
  echo -e "Running sudo. Enter your own \c"
  sudo nessus-update-plugins
  sleep 25
fi

oIFS="${IFS}"
IFS=:
set - ${PDIRS}
IFS="${oIFS}"
unset oIFS

for PDIR
do

  # don't bother if it's not there
  [ -d "${PDIR}" ] || continue

  if [ "${NONAMES}" ]
  then
    echo "Not updating Nessus Names files."
  else

    # make the plugin files
    nessus -q -p localhost 1241 klann 'ksi#829' | tee ${PDIR}/nessus-plugins-${TODAY} |
      cut -d'|' -f 1,2,3,4 |
      sort -t'|' -k 1n,1 > ${PDIR}/nessus-plugin-names-${TODAY}

    rm -f ${PDIR}/nessus-plugins &&
      {
        ln -s ${PDIR}/nessus-plugins-${TODAY} ${PDIR}/nessus-plugins ;
	chmod g+rw ${PDIR}/nessus-plugins-${TODAY} ;
      }
    rm -f ${PDIR}/nessus-plugin-names &&
      {
	ln -s ${PDIR}/nessus-plugin-names-${TODAY} ${PDIR}/nessus-plugin-names ;
	chmod g+rw ${PDIR}/nessus-plugin-names-${TODAY} ;
      }

  fi


  if [ "${NOCLASS}" ]
  then
    echo "Not updating Nessus Class Name files."
  else

    # make the class name mapping file
    for class in $(cut -d'|' -f 4 ${PDIR}/nessus-plugins | sort | uniq)
    do
      unset LINE
      [ -e ${PDIR}/nessus-class-names ] && LINE=$(grep "^${class}|" ${PDIR}/nessus-class-names 2>/dev/null)
  
      if [ -z "${LINE}" ]
      then
        echo -e 1>&2 "Seems there is a new class (${class}). Enter a descriptive name: \c"
        read NAME
        echo "${class}|${NAME}"
      else
        echo "${LINE}"
      fi
    done > ${PDIR}/nessus-class-names-TEMP

    mv ${PDIR}/nessus-class-names-TEMP ${PDIR}/nessus-class-names-${TODAY}
    rm -f ${PDIR}/nessus-class-names &&
      {
	ln -s ${PDIR}/nessus-class-names-${TODAY} ${PDIR}/nessus-class-names ;
	chmod g+rw ${PDIR}/nessus-class-names-${TODAY} ;
      }

  fi

done

exit
