#
# Checks to see if TCP Wrapper is installed on the machine.
#
#

&pcheck("TCP Wrapper 1: Installation Check");

$fail = 1;

if (open(INETD, $inetd)) {
	while (<INETD>) {
		next if (/^#/);
		next unless (/(telnet|ftp|shell|login)/);

		@parts = split(/\s+/);
		($patch,$realdir) = &is_tcpd(@parts[5]);

		if (((@parts[5] =~ /tcpd/) && ($patch > 0)) && ($fail)) {
			&report("INFO:  TCP Wrapper is installed on this system.\n",
			"\t\t(Version $patch)\n");
			$fail = 0;
		}

		if ((!(@parts[5] =~ /tcpd/) && ($patch > 0)) && ($fail)) {
			&report("INFO:  TCP Wrapper is installed on this system by replacing the\n",
			"\tactual daemons. (Version $patch | Real Dir: $realdir)\n");
			$fail = 0;
		}
	}

} else {
	&report("ERROR:  Cannot open $inetd file.\n");
	&error;
	exit;
}
		
close (INETD);

if ($fail) {
	&report("FAILED: TCP Wrapper cannot be found on this system. It should\n",
		"\talways be installed to make sure connections to the machine\n",
		"\tare allowed and are logged.  Please install it.\n");
	&failed;
	exit;
} else {
	&passed;
}

&pcheck("TCP Wrapper 2: Services");

if (open (INETD, $inetd)) {
	while (<INETD>) {
		if (/stream\s+tcp/ && !/tcpd\s+/) {
			if (!($_ =~ /(^#|ident|internal)/)) {
			      ($service) = split(/\s+/);
			      unshift(@unwrapped,$service);
			}
		}
	}
	if (defined(@unwrapped)) {
		&report("WARNING: The following stream/tcp network services are not TCP Wrapped.\n");
		foreach $service (@unwrapped) {
			&report("\t\t$service\n");
		}
		$warn = 1;
	}
} else {
	&report("ERROR: I can't open the $inetd file.\n");
	&error;
	exit;
}

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

exit 0;


#############################################################
#
# is_tcpd procedure - thanks nate!
#
sub is_tcpd {
  local($file) = @_;
  local($istcpd, $pl, $directory) = (0, 0, 0);
  
  ## look through the strings output
  open(STRINGS, "$strings $file 2>&1 |");
  while (<STRINGS>) {
    if (/tcpd\.c/) { $istcpd = 1; }            ## tcpd.c is in there
    elsif (/patchlevel\s+(.+)$/) { $pl = $1; } ## find the patchlevel
    elsif (substr($_, 0, 1) eq '/') {          ## the directory that the
      chop;                                    ## daemons are located in.
      if (-d $_) { $directory = $_; }          ## (start with /)
      }
    }
  close(STRINGS);
  
  if ($istcpd) { ($pl, $directory); }
  else { (0, 0); }
}
