#!/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).
#
# Report file systems that are exported via portmap or that can be mounted
# by unpriviliged programs. World-mountable file systems are already taken
# care of by showmount.sara.
#
# version 1, Mon Mar 20 18:48:11 1995, last mod by wietse
#

$running_under_sara = 1;
require 'config/sara.cf';
require 'config/paths.pl';
require 'perl/misc.pl';
require 'perl/hostname.pl';
require 'perl/getfqdn.pl';
require 'perllib/getopts.pl';

$usage="Usage: $0 [-t timeout -u -v] target";
$opt_u = $untrusted_host;
&Getopts("t:uv") || die $usage;
$timeout = $short_timeout;

($#ARGV == 0) || die $usage;

$target = $ARGV[0];
$flags = "-v" if defined($opt_v);
$timeout = $opt_t if defined($opt_t);
$untrusted_host = $opt_u;

$flags = "$flags -t $timeout" if defined($opt_t);

$service = &basename($0, ".sara");
$severity = "x";
$status = "a";
$service_output = "";

$| = 1;

open(NFS, "$NFS_CHK $flags $target |") || die "$0: cannot run nfs-chk";
while(<NFS>) {
	if (defined($opt_v)) {
	    print;
	}
        $WORLD = "TRUE" if /to the world/;
        
	if (/exports (\S+) via portmapper/) {
                $sid = 'NFS-001';
                ($severity,$service_output,$text, $cve) = &get_sid($sid);
                $status  = "a";
                $text =~ s/\;/$1/;
		$trusted = "ANY\@ANY";
		$trustee = "$1\@$target";
		$text = "exports $1 via portmapper";
		&sara_print();
	} elsif (/Mounted: (\S+) via mount daemon/) {
		$sid = 'NFS-002';
		($severity,$service_output,$text, $cve) = &get_sid($sid);
		$status  = "a";
		$text =~ s/\;/$1/;
		$trusted = "ANY\@ANY";
		$trustee = "$1\@$target";
		&sara_print() if ( ! $WORLD);
	} elsif (/exports (\S+) to unprivileged/) {
		$sid = 'ARC-0015';
		($severity,$service_output,$text, $cve) = &get_sid($sid);
		$status  = "a";
		$text =~ s/\;/$1/;
		$this_host = &getfqdn(&hostname());
		$trusted = "ANY\@$this_host";
		$trustee = "$1\@$target";
		&sara_print();
	}
	# world-wide exports already taken care of in showmount.sara
}
