#!/usr/bin/perl5.8.4
#  This software was derived from SATAN 1.1.1 by Dan Farmer and Wietse Venema
#  (http://www.porcupine.org/satan).
#
#
# Find out some stuff about NFS using showmount: world-wide exports,
# boot clients.  Should not bother to do this when rpc.rip fails.
#

# require these packages:
$running_under_sara = 1;
require "config/paths.pl";
require "perl/misc.pl";
require "perl/fix_hostname.pl";
 
if ($#ARGV != 0) {
        print "Usage $0 target\n";
        exit(1);
        }

$target = $ARGV[0];
($service = $0) =~ s@^.*/([^\/\.]*)\..*$@$1@;

#
# we don't make value judgements here
#

$severity = "";
$service_output = "";

# Showmount -e tells us who can mount what from this server.

open(SM, "$SHOWMOUNT -e $target|");

while (<SM>) {
	chop;
	next unless /^(\S+)\s+(\S+)\s*$/;
	$files = $1;
	@hosts = split(",", $2);
	for $host (@hosts) {
		$Host = $host;
		$host =~ tr/A-Z/a-z/;
		if ($host eq "\(everyone\)" || $Host eq "Everyone") {
			next if $untrusted_host;
			$sid = 'NFS-004';
			($severity,$service_output,$text, $cve) = &get_sid($sid);
			$text =~ s/\;/$files/;
			$status="a";
			$trustee="$files\@$target";
			$trusted="root\@ANY";
			}
		else {
			$fqdn = &fix_hostname($host ,$target);
			# if the host doesn't really exist, it could
			# be a netgroup or something... try to complete
			# hostname if not FQDN, etc., then try to resolve.
			# If everything fails, just output what we can:
			if ($fqdn eq "") {
				$status="u";
				$severity=""; 
				$trustee="$files\@$target"; 
				$trusted="root\@$host"; 
				$service_output="$target $host"; 
				$text="Exports $files to $host, but we can't verify that $host exists"; 
				} 
			else { 
				$host = $fqdn; 
				$status="a"; 
				$severity="";
				$trustee="$files\@$target"; 
				$trusted="root\@$host"; 
				$service_output="$target $host"; 
				$text="exports $files to $host"; 
				} 
			} 
		&sara_print(); 
		$print_flag = 1; 
		} 
	} 
# Showmount -a tells what systems actually mount from this server.  open(SM, "$SHOWMOUNT -a $target|");

while (<SM>) {
	chop;
	next unless /(\S+):(\S+)/;
	($host = $1) =~ tr/A-Z/a-z/;
	$path = $2;
	$fqdn = &fix_hostname($host ,$target);
	# If everything fails, just output what we can:
	if ($fqdn eq "") {
		$status="u";
		$severity="";
		$trustee="$path\@$target";
		$trusted="root\@$host";
		$service_output="$target $host";
		$text="$host mounts $path from $target, but we can't verify that $host exists";
	} else {
		$host = $fqdn;
		$status="a";
		$severity="";
		$trustee="$path\@$target";
		$trusted="root\@$host";
		$service_output="$target $host";
		$text="$host mounts $path from $target";
	}
	&sara_print();
	$print_flag = 1;
}

# print something out if nothing has happened so far...
if (!$print_flag) {
	$status="a";
	$severity="";
	$text="Not running showmount or other error";
	&sara_print();
}
