#  This software was derived from SATAN 1.1.1 by Dan Farmer and Wietse Venema
#  (http://www.porcupine.org/satan).
#
#!/bin/sh -- need to mention perl here to avoid recursion
require "config/sara.cf";
'true' || eval 'exec perl -S $0 $argv:q';
eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
& eval 'exec /usr/local/bin/perl -S $0 $argv:q'
	if 0;

#
# version 1, Sun Mar 26 18:31:28 1995, last mod by zen
#

#  Usage: [perl] reconfig [file]
#
#   This replaces the program paths (e.g. /bin/awk) in SARA with an
# alternate path that is found in the file "file.paths".  It also finds
# perl5 (or at least tries!) and changes the path in all the stand-alone
# perl programs.
#

require 5.005;
require 5.8.1;
use lib 'perl/Modules';


# all the HTML browsers we know about, __IN ORDER OF PREFERENCE__!!!


@all_www= ("firefox", "seamonkey", "mozilla", "konqueror", "galeon", "epiphany", "opera", "netscape", "amaya", "dillo", "elinks", "links2", "links", "lynx", "w3m", "w3mmee", "netrik");
#  Potential directories to find commands; first, find the user's path...
$PATH = $ENV{"PATH"};
#
#  See if we can find /bin/csh or /usr/bin/csh (as perl needs it)
#
if (! -f "/bin/csh" && ! -f "/usr/bin/csh") {
  die "Perl needs the csh shell to execute this program\n";
}
#
#  Zero out optional programs; if found then will be set and enabled
$SMBCLIENT="";
$NMBCLIENT="";
$RPCCLIENT="";
$NMAP="";
$MKNOD="";
$MKFIFO="";
$SQLANLZ="";
$MOSAIC="";
$OPENSSL="";

# Kludge to bypass Norton faulting msadc.pl and t2r.pl as containing virus
if (-d "encoded") {
  $msadc_en = "encoded/msadc.pl.en";
  $msadc    = "perl/contrib/msadc.pl";
  $t2r_en   = "encoded/t2r.pl.en";
  $t2r      = "perl/contrib/t2r.pl";
  $tmp = `uudecode $msadc_en`;
  $tmp = `mv msadc.pl $msadc`;
  `chmod 700 $msadc`;
  $tmp = `uudecode $t2r_en`;
  $tmp = `mv t2r.pl $t2r`;
  `chmod 700 $t2r`;
}
  $sqlix  = "perl/contrib/SQLiX/sqlix";
  mkdir (".dump");

# additional dirs; *COLON* separated!
$other_dirs="cobin:/usr/local/bin:/usr/local/samba/bin:/usr/dt/bin:/usr/ccs/bin:/bin:/usr/bin:/usr/ucb:/usr/bsd:/usr/ucb/bin:/sbin:/usr/sbin:/usr/etc:/usr/bin/X11:/usr/X11/bin:/usr/openwin/bin:perl/contrib:/usr/local/ssl/bin:perl/contrib/SQLiX";

#
# split into a more reasonable format. Personal aliases come last.
@all_dirs = split(/:/, $other_dirs . ":" . $PATH);

#
#  Target shell scripts in question:
@shell_scripts=("config/paths.pl", "config/paths.sh");
@perl5_src = <bin/get_targets bin/faux_fping sara add_user bin/*.sara* bin/*.sara.ext perl/html.pl perl/contrib/*.pl perl/contrib/mysql  bin/fwping bin/telnet bin/fw bin/pop3 perl/contrib/SQLiX/sqlix>;

#
#  Target shell commands in question
@all_commands=("arp", "awk", "cat", "chmod", "cmp", "comm", "cp", "date",
    "diff", "egrep", "expr", "find", "grep", "ls", "mkdir", "mv", "rm", 
    "sed", "sh", "sort", "tftp", "touch", "uniq", "uudecode", "uuencode","ypcat",
    "strings", "finger", "ftp", "rpcinfo", "rusers", "showmount", "ping",
    "ypwhich", "nslookup", "dig", "xhost", "su", "test", "whoami", 
    "basename", "echo", "file", "rlogin", "rsh", "stty", "smbclient", 
    "nmblookup", "rpcclient", "nmap", "telnet", "nohup", "mknod", 
    "mkfifo", "openssl", "sqlix");

print "checking to make sure all the target(s) are here...\n";

for (@shell_scripts) {
	die "ERROR -- $_ not found!\n" unless -f $_;
	}

# find perl5!
print "Ok, trying to find perl5 now... hang on a bit...\n";
for $dir (@all_dirs) {
	# first, find where it might be; oftentimes you'll see perl,
	# perl4, perl5, etc. in the same dir
	next if (! -d $dir);
	while (<$dir/perl5* $dir/perl*>) {
		if (-x $_) {
			$perl_version=`($_ -v 2> /dev/null) |
				awk '/This is perl, version 5/ { print $NF }'`;
                        if (!$perl_version) {
                            $perl_version=`($_ -v 2> /dev/null) |
                                awk '/This is perl, v5/ { print $NF }'`;
                        }
			if ($perl_version) {
				$PERL=$_;
				$pflag="1";
				last;
				}
			}
			last if $pflag;
		}
	last if $pflag;
	}

die "\nCan't find perl5!  Bailing out...\n" unless $PERL;
print "\nPerl5 is in $PERL\n";

for (@perl5_src) { $perl5_src .= "$_ "; }
#print "\nchanging the source in: $perl5_src\n";
system "$PERL -pi -e \"s@^#!.*/perl.*@#!$PERL@;\" $perl5_src";

# make sure things are executable...
system("chmod u+x $perl5_src");

# find the most preferred www viewer first.
$uname = `uname -a`;
if ($uname =~ /Macintosh/ || $uname =~ /Darwin/ || $uname =~ /Jaguar/) {
   $MOSAIC="bin/ie_mac.sara";
   print "\nHTML/WWW Browser is $MOSAIC\n";
   print "Internet Explorer must be installed on system\n";
   $upper{"MOSAIC"} = $MOSAIC;
} else {
  for $www (@all_www) {
	for $dir (@all_dirs) {
		if (!$MOSAIC) {
			if (-x "$dir/$www") {
				$MOSAIC="$dir/$www";
				next;
			}
		}
	}
}
  if ($MOSAIC) {
	print "\nHTML/WWW Browser is $MOSAIC\n";
	$upper{"MOSAIC"} = $MOSAIC;
  } else {
	print "\nHTML/WWW Browser not found\n";
	$upper{"MOSAIC"} = "";
  }
}
if (! -e "bin/sssh") {
print "SSH was not built due to option deselection or missing dependencies\n";
}
if (! -e "bin/sqlanlz") {
print "SQLAT was not built due to option deselection or missing dependencies\n";
}
print "\nSo far so good...\nLooking for all the commands now...\n";
for $command (@all_commands) {
	$found="";
	for $dir (@all_dirs) {
		# special case rsh/remsh; if we can find remsh, ignore rsh
		if ($command eq "rsh") {
			# print "looking for rsh/remsh ($dir/$command)\n";
			if (-f "$dir/remsh") {
				# this converts to upper case
				($upper = $command) =~ y/[a-z]/[A-Z]/;
				$found="true";
				$upper{$upper} = "$dir/remsh";
				print "found $dir/remsh; using this instead of rsh\n";
				last;
			}
		}

		# if find the command in one of the directories, print string
		if (-f "$dir/$command") {
			# this converts to upper case
			($upper = $command) =~ y/[a-z]/[A-Z]/;
			$found="true";
                        if ($command eq "nmap" && $concurrent_processes != 1) {
                           print "Warning:  Can't run nmap with \$concurrent_processes \> 1\n\n";
                        }
                        if ($command eq "rpcclient") {
                           $tng="Warning:  Deprecated rpcclient.  Upgrade to SAMBA Version 3 or above\n\n";
                           open (TEST,"$dir/$command -V 2>/dev/null|");
                           while (<TEST>) {
                             if (/Version 3\.[0-9]\.[0-9]/) {
                              $tng="";
                             }
                           }
                           close (TEST);
                           print $tng;
                        }
			$sqlix="Warning:  SQLiX not built correctly.  No SQL Injection possiblei\n\n";
			if ($command eq "sqlix") {
                           open (TEST,"$dir/$command -h 2>/dev/null|");
                           while (<TEST>) {
                             if (/-- SQLiX --/) {
                              $sqlix = "";
                             }
			   }
			   print $sqlix;
			}

			$upper{$upper} = "$dir/$command";
			# print "found ($upper) $dir/$command\n";

			# if it's rsh we're examining, keep looking; else quit
			last unless $command eq "rsh";
                  }


		}
	if ($command ne "nmap" && $command ne "dig") {
           if ($command eq "smbclient" || $command eq "nmblookup" || $command eq "rpcclient") {
             print "Samba ($command) not found, SMB tests will be disabled\n" unless $found;
         } else {
  	     print "\nAEEEIIII...!!!  can't find $command\n\n" unless $found;
           }
         }
	}

print "\nOk, now doing substitutions on the shell scripts...\n";
for $shell (@shell_scripts) {
 	print "Changing paths in $shell...\n";
	die "Can't open $shell\n" unless open(SCRIPT, $shell);
	rename($shell, $shell . '.old');
	die "Can't open $shell\n" unless open(OUT, ">$shell");

	#
	#  Open up the script, search for lines beginning with
	# stuff like "TEST", "AWK", etc.  If the file ends in "pl",
	# assume it's a perl script and change it accordingly
	while (<SCRIPT>) {
		$found = 0;
		for $command (keys %upper) {
			if(/^\$?$command=/) {
				# shell script
				if ($shell !~ /.pl$/) {
					print OUT "$command=$upper{$command}\n";
					}
				# perl script
				else {
					print OUT "\$" . "$command=\"$upper{$command}\";\n";
					}
				$found = 1;
				}
			}
		print OUT $_ if !$found;
		}
	close(SCRIPT);
	close(OUT);
	}
do "html/build_sss.pl";
print "\nNow building CVE database\n";
do "html/cve/sara_cve_tutorial.pl";
do "html/cve/cve_build.pl";
unlink <fact.*>;
unlink <tmp.*>;
unlink <tmp1.*>;


print "\nNow building FIFOs for SSS\n";
require 'config/sara.cf';
require 'config/paths.pl';
unlink("$sara_in");
unlink("$sara_out");
$psargs="-ef";
$pstmp=`ps -auxc 2>/dev/null`;
$psargs="-auxc" if $pstmp;
$_web_owner=`ps $psargs 2>/dev/null|grep "[ /]httpd[2 ]"|grep -v root|grep -v grep`;
if (! $_web_owner) {
  $_web_owner=`ps $psargs 2>/dev/null|grep "[ /]httpd\$"|grep -v root|grep -v grep`;
}
if (! $_web_owner) {
  $_web_owner=`ps $psargs 2>/dev/null|grep "[ /]apache\$"|grep -v root|grep -v grep`;
}
if (! $_web_owner) {
  $_web_owner=`ps $psargs 2>/dev/null|grep "[ /]apache[2 ]"|grep -v root|grep -v grep`;
}
$_web_owner =~ s/^\s+//;
if ($_web_owner) {
 ($web_owner, $pid, $ppid) = split(' ', $_web_owner);
# $_web_owner = `ps -ef | grep http | grep $pid | grep -v grep |grep -v root`;
# ($web_owner, $dummy) = split(' ',$_web_owner);
 if ($web_owner) {
   if (-f $sara_in || -p $sara_in) {
    print "   SARA Self Scan (SSS) option not installed; problem building FIFOs\n";
    exit;
   } elsif (! -d $sara_in) {
    system(`mkdir $sara_in`);
   }
   system(`chown $web_owner $sara_in`);
#   system(`chgrp $web_owner $sara_in`);
   system(`chmod 700 $sara_in`);
 } else {
  print "   SARA Self Scan (SSS) option not installed; web owner could not be determined\n\n";

 }
} else {
 print "   SARA Self Scan (SSS) option not installed; no web server found running\n";
}


# done...

