#
# Check users .rhosts files for + entries
#

&pcheck("Rhosts 1: User Rhosts");

$fail = 0;
$warn = 0;

if (open(NIS, "/etc/config/yp")) {
	$status = <NIS>;
	chop ($status);
} else {
	$status = "no";
}

if ($status eq "yes") {
	$passwd = `$ypcat passwd`;
	@passwd = split(/\n/,$passwd);
} else {
	if (open(PASSWD, $passwd)) {
		while (<PASSWD>) {
			unshift(@passwd,$_);
		}
		close (PASSWD);
	} else {
		&report("ERROR:  Cannot open the $passwd file.\n");
		&failed;
		exit;
	}
}

foreach $pwent (@passwd) {
	($name,$pass,$uid,$gid,$gcod,$dir,$shell)=split(/:/,$pwent);
	
	if (-f "$dir/.rhosts") {
		$plus = 0;
		@pluslines = ();
		if (open(RHOSTS, "$dir/.rhosts")) {
			while (<RHOSTS>) {
				if (/\+/) {
					$plus = 1;
					unshift(@pluslines,$_);
				}
			}
			close (RHOSTS);
		} else {
			&report("WARNING: Cannot open $name's rhosts ($dir/.rhosts)\n");
			$warn = 1;
		}

		if ($plus) {
			&report("FAILURE: $name has a + entry in $dir/.rhosts file.\n");
			foreach $plusline (@pluslines) {
				&report("\t$plusline");
			}
			$fail = 1;
		}
	}
}

if ($fail) {
	&failed;
} elsif ($warn) {
	&warn;
} else {
	&passed;
}

##################################################
#
# Part 2 - Check hostnames and hosts.equiv
#

$fail = 0;
$warn = 0;

&pcheck("Rhosts 2: System/Root Rhosts");

if (open(ROOTR, "/.rhosts")) {
	while (<ROOTR>) {
		chop;
		($host,$etc) = split(/\s+/);
		if ($host =~ /^[0-9\.]+/) {
			$ip = pack("C4", split(/\./, $host));
			($host2) = gethostbyaddr($ip,2);
		} else {
			($host2) = gethostbyname($host);
		}
		if ($host2 eq "") {
			&report("DANGER:  I cannot find a valid address for the hostname\n",
			"\t\t$host\n");
			$fail = 1;
		} else {
			&report("INFO: $host is in root's .rhosts.\n");
		}
	}
}


if (open(EQUIV, $equiv)) {
	while (<EQUIV>) {

		if (/\+/) {
			&report("FAILURE: The following + line is in the $equiv file.\n\t$_");
			$fail = 1;
		}

		($host,$etc) = split(/\s+/);

		if ($host =~ /^[0-9\.]+/) {
			$ip = pack("C4", split(/\./, $host));
			($host2) = gethostbyaddr($ip,2);
		} else {
			($host2) = gethostbyname($host);
		}
		if ($host2 eq "") {
			&report("DANGER:  I cannot find a valid address for the hostname\n",
			"\t\t$host\n");
			$fail = 1;
		} else {
			&report("INFO: $host is in root's .rhosts.\n");
		}
	}
	close (EQUIV);
}


if ($fail) {
	&failed;
} elsif ($warn) {
	&warn;
} else {
	&passed;
}

exit 0;
