#!/bin/sh
#
# nessus-update-plugins
#
# This script will retrieve all the newest plugins from 
# www.nessus.org using the utility 'nessus-fetch'.
# 
# 
# Author  : Renaud Deraison <deraison@cvs.nessus.org>
# License : GPL (but for two lines of script, does it matter ?)
# 
#
# usage : nessus-update-plugins [-v]
# 
#
# -d <dir>  : use <dir> as the plugin dir
# -v        : be verbose
# -vv       : be more verbose (debug)
#


#
# The command we use to retrieve the plugins
#


#-------------- DO NOT EDIT THIS FILE BEYOND THAT POINT ---------------------#



gzip=/usr/bin/gzip
prefix=/usr/local
exec_prefix=${prefix}
bindir=${exec_prefix}/bin
sbindir=${exec_prefix}/sbin
libexecdir=${exec_prefix}/libexec
datadir=${prefix}/share
sysconfdir=/etc
sharedstatedir=/var
localstatedir=/var
libdir=${exec_prefix}/lib
includedir=${prefix}/include
oldincludedir=/usr/include
infodir=/usr/local/info
mandir=/usr/local/man

pluginsdir="$libdir/nessus/plugins"



case `id` in uid=0*) ;; 
   *euid=0*) ;;
   *)
    echo "only root should use nessus-update-plugins"
        exit 1
	esac
	

if [ ! -x "$bindir/nessus-fetch" ]; then
 echo "nessus-fetch(1) (part of nessus-core) is not installed on your system"
 echo "Aborting"
 exit 1
fi

if [ ! -x "$gzip" ]; then
 echo "gzip is not installed on your system"
 echo "Aborting"
fi

if [ ! -r "$sysconfdir/nessus/nessusd.conf" ]; then
	if [ ! -e "$sysconfdir/nessus/nessusd.conf" ]; then
	echo "$sysconfdir/nessus/nessusd.conf does not exist!"
	echo "Do you have the nessus daemon installed?"
	else
	echo "I cannot read $sysconfdir/nessus/nessusd.conf."
	echo "Are you root?"
	fi
	exit 1
fi
newdir=`awk '/plugins_folder/ {print $3}' $sysconfdir/nessus/nessusd.conf`
test -n "$newdir" && pluginsdir="$newdir"




help_screen()
{
 echo "nessus-update-plugins 2.0.0, by Renaud Deraison <deraison@cvs.nessus.org>"
 echo
 echo
 echo "Usage : nessus-update-plugins [-v][-h]"
 echo
 echo "-v              : be verbose"
 echo "-h              : this help screen"
 echo
 echo "Default action  : update the nessusd plugins"
 exit 0
}



opts=`getopt "vh" $*`

for i in $opts
do
 case $i in
 -h )
   help_screen
   ;;
   
  -v)
   if [ -z "$verbose" ];
    then
      verbose="y"
     else
       set -x
   fi
   ;;
 esac
done
 

if [ -z "$verbose" ];
then
 tar="-xf"	
else
 tar="-xvf"
fi



if [ ! -d $pluginsdir ] ; then
	echo "Plugindir $pluginsdir is not a directory!"
	exit 1
fi


	
cwd=`pwd`
tmpdir=$TEMPDIR
test -z "$tmpdir" &&
{
 tmpdir=$TMPDIR
 test -z "$tmpdir" && tmpdir=/tmp
}


mkdir -m 0700 "$tmpdir/nessus-update-plugins-$$" || {
	echo "Could not create temporary directory ($tmpdir/nessus-update-plugins-$$)"
	exit 1
}
cd "$tmpdir/nessus-update-plugins-$$"
$bindir/nessus-fetch --plugins-md5 || { 
	echo "Could not retrieve the plugins MD5"
	echo "Aborting"
	exit 1
	}

test -s "$pluginsdir/MD5" && {
	if [ -x /usr/bin/diff ];
	then 
	 diff "$pluginsdir/MD5"  all-2.0.tar.gz.md5 > /dev/null && {
	 cd "$cwd"
	 rm -rf "$tmpdir/nessus-update-plugins-$$"
	 exit 0
	}
	fi
}


$bindir/nessus-fetch --plugins || {
	echo "Could not retrieve the Nessus plugins"
	echo "Aborting"
	exit 1 
	}
	

# Check the archive signature
test -x $sbindir/nessus-check-signature && {
	$sbindir/nessus-check-signature all-2.0.tar.gz all-2.0.sig || {
		echo "Aborting"
		exit 1
		}
	}

rm -f all-2.0.sig
	
cd "$pluginsdir/"
$gzip -cd "$tmpdir/nessus-update-plugins-$$/all-2.0.tar.gz" | tar $tar - 
rm -f "$pluginsdir/MD5"
mv "$tmpdir/nessus-update-plugins-$$/all-2.0.tar.gz.md5" "$pluginsdir/MD5"

cd "$cwd"
rm -rf "$tmpdir/nessus-update-plugins-$$"

chown -R 0:0 "$pluginsdir/"


# HUP nessusd
test -f /var/nessus/nessusd.pid && {
    pid=`cat /var/nessus/nessusd.pid`
    kill -1 $pid 2>/dev/null
}

exit 0
