#!/usr/bin/perl
#
# bin/showmount.saint
#
# 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_saint = 1;
require "config/paths.pl";
require "perl/misc.pl";
require "perl/fix_hostname.pl";
 
print "BEGIN\n";
if ($#ARGV < 0) {
        print "Usage $0 target\n";
        exit(1);
        }

$target = $ARGV[0];
#($service = $0) =~ s@^.*/([^\/\.]*)\..*$@$1@;
# made service same as nfs-chk.saint to avoid duplicate facts, 6/00
$service = "nfs";

#
# 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") {
			# Following line is useless; config variables
			# don't carry over to these scripts (6/00)
			next if $untrusted_host;
			$status="a";
			$severity="uw";
			$trustee="$files\@$target";
			$trusted="root\@ANY";
			$service_output="unrestricted NFS export";
			$text="Exports $files to everyone";
			}
		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="x";
				$trustee="$files\@$target";
				$trusted="root\@$host";
				$service_output="$target $host";
				$text="Exports $files to $host";
				}
			}
		&saint_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="x";
		$trustee="$path\@$target";
		$trusted="root\@$host";
		$service_output="$target $host";
		$text="$host mounts $path from $target";
	}
	&saint_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";
	&saint_print();
}
