#!/bin/sh
#
# nessus-update-plugins
#
# This script will retrieve all the newest plugins from 
# www.nessus.org.
#
# NOTE: the use of this script is dangerous as the authenticity of 
#       the scripts is not checked for. USE THIS SCRIPT WITH CAUTION
# 
# 
# Author  : Renaud Deraison <deraison@cvs.nessus.org>
# License : GPL (but for two lines of script, does it matter ?)
# 
#
# usage : nessus-update-plugins [-v]
# 
#
# -l        : list the plugins but do not install them
# -r <name> : read a plugin name
# -v        : be verbose
# -vv       : be more verbose (debug)
#



# Proxy users
#
# If you are behind a proxy, you can set this options here
# or in ~/.nessus-update-pluginsrc
#
# If you edit THIS file, then the proxy (and proxy username/password)
# will be system-wide. 

proxy_user=
proxy_passwd=
proxy=

# Examples :
# proxy_user="renaud"
# proxy_passwd="topsecr3t"
# proxy="proxy.fr.nessus.org:8080"
#
# You can copy the lines above and put them
# in your ~/.nessus-update-pluginsrc




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

fetch_cmd="/usr/bin/lynx -source"
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=${prefix}/info
mandir=${prefix}/man

#
# Read the configuration file, if any
#
test -f ~/.nessus-update-pluginsrc && . ~/.nessus-update-pluginsrc

help_screen()
{
 echo "nessus-update-plugins 1.0.3, by Renaud Deraison <deraison@cvs.nessus.org>"
 echo
 echo
 echo "Usage : nessus-update-plugins [-v[v]] [-l] [-r name] [-h]"
 echo
 echo "-v              : be verbose"
 echo "-l              : list the plugins available on the server"
 echo "-r <pluginname> : view the content of a plugin"
 echo "-i <pluginname> : only install <pluginname>"
 echo "-h              : this help screen"
 echo
 echo "Default action  : update the nessusd plugins"
 exit 0
}


list_plugins()
{
 if [ -n "$fetch_cmd" ];
 then
  echo "List of plugins available on www.nessus.org : "
  echo
  $fetch_cmd $proxyopts http://www.nessus.org/nasl/index.txt
  echo
  echo
 fi
 exit 0
}


view_plugin()
{
 if [ -n "$fetch_cmd" ];
 then
  $fetch_cmd $proxyopts "http://www.nessus.org/nasl/$1"
 fi
 exit 0
}

install_plugin()
{
if [ -n "$fetch_cmd" ];
then
test -f "$libdir/nessus/plugins/$1" && mv "$libdir/nessus/plugins/$1" "$libdir/nessus/plugins/$1.bak"
$fetch_cmd $proxyopts "http://www.nessus.org/nasl/$1" > "$libdir/nessus/plugins/$1"
grep "404 Not Found" "$libdir/nessus/plugins/$1" 2>&1 > /dev/null && {
	echo "Error - file not found"
	rm -f "$libdir/nessus/plugins/$1"
	test -f "$libdir/nessus/plugins/$1.bak" && mv "$libdir/nessus/plugins/$1.bak" "$libdir/nessus/plugins/$1"
	}
	
# HUP nessusd
test -f /var/nessus/nessusd.pid && (
    pid=`cat /var/nessus/nessusd.pid`
    kill -1 $pid 2>/dev/null
)
 exit 0

else
echo "Error \$fetch_cmd or \$gzip are not set - abort"
exit 1
fi
}


proxyopts=""

echo "$fetch_cmd" | grep "lynx" 2>&1 > /dev/null &&
{
 test -n "$proxy" && http_proxy="http://$proxy/"
 test -n "$proxy_user" && proxyopts="-pauth=\"$proxy_user:$proxy_passwd\""
} 

echo "$fetch_cmd" | grep "wget" 2>&1 > /dev/null &&
{
 test -n "$proxy" && http_proxy="http://$proxy/"
 test -n "$proxy_user" && proxyopts="--proxy=on --proxy-user=$proxy_user --proxy-passwd=$proxy_passwd"
}


export http_proxy

opts=`getopt "vlr:hi:" $*`

for i in $opts
do
 case $i in
 -h )
   help_screen
   ;;
   
  -v)
   if [ -z "$verbose" ];
    then
      verbose="y"
     else
       set -x
   fi
   ;;
   
 -l)
   get_plugin_list="y"
   ;;
 
 -r)
   expect_r="y"
   ;;
   
  -i)
   expect_i="y"
   ;;
    
  *)
   test -n "$expect_r" &&
     {
      plug_name="$i"
      unset expect_r
     }
     
   test -n "$expect_i" &&
     {
      install_plug="$i"
      unset expect_i
     }  
   ;;
 esac
done   



test -n "$get_plugin_list" && list_plugins
test -n "$plug_name" && view_plugin "$plug_name"
test -n "$install_plug" && install_plugin "$install_plug"

tar="-xf"	
test -z "$verbose" || tar="-xvf"





case `id` in uid=0*) ;; *)
    echo "only root should use nessus-update-plugins"
        exit 1
	esac
	
	
if [ -n "$fetch_cmd" -a -n "$gzip" ] ;
then
cwd=`pwd`
tmpdir=$TEMPDIR
test -z "$tmpdir" &&
{
 tmpdir=$TMPDIR
 test -z "$tmpdir" && tmpdir=/tmp
}
mkdir "$tmpdir/nessus-update-plugins-$$" || {
	echo "Could not create temporary directory ($tmpdir/nessus-update-plugins-$$)"
	exit 1
}
cd "$tmpdir/nessus-update-plugins-$$"
$fetch_cmd $proxyopts http://www.nessus.org/nasl/all-1.2.tar.gz | $gzip -cd | tar  $tar - 
cp *.nasl $libdir/nessus/plugins/
cp *.inc $libdir/nessus/plugins/
cd "$cwd"
rm -rf "$tmpdir/nessus-update-plugins-$$"
chown 0 $libdir/nessus/plugins/*.nasl
chgrp 0 $libdir/nessus/plugins/*.nasl

# HUP nessusd
test -f /var/nessus/nessusd.pid && (
    pid=`cat /var/nessus/nessusd.pid`
    kill -1 $pid 2>/dev/null
)
else
 echo "Error \$fetch_cmd or \$gzip are not set - abort"
exit 1
fi
