#!/usr/bin/perl -w

# $Id: query_tw.pl,v 1.7 1996/02/12 18:48:13 hoagland Exp $

# query_tw.pl, by Jim Hoagland (hoagland@cs) 1/96

# this program sends a request to a tripwire agent on a given host, and displays the result to the user's browser  for the ARPA IDS demo GUI
# Input is CGI, 3 field in a form (host,files,action)
# outputs text to STDOUT

require 5.000;

require '/pkg/www/arpa/secret/cgi-bin/gui-helper.pl';
require '/pkg/www/arpa/secret/cgi-bin/network-help.pl';
require '/pkg/arpa/demo.0/require.pl';

umask 000;

require CGI;

$input= new CGI;

foreach (@ARGV) {  # simulate field input
  $input->param(split('=',$_,2));
}

#$state= State->new(&get_statefile);

# send the request to tripwire and return the TCP port to listen on for
# a responce
sub send_tw_request {
  my ($host,$request,$check_mask,$port,@files)= @_;
  my $hostname= $myhostname; # from require.pl
#print "Content-type: text/plain\n\n$check_mask\n";
  my (%mess)= ('dtyp' => 'to_trip_stdin', 'request' => $request,'file' => join(' ',@files),'respond_to_host' => $hostname,'respond_to_port' => $port, 'time' => time);
  $mess{'check_mask'}= $check_mask if defined($check_mask);
  &send_scl_message($host,%mess);
  return $port;
}
#-------------

&start_multipart_output;
print "Content-type: text/plain\n\n";

$host= $input->param('host');

(@infiles)= $input->param('files');

@files= ();
# we send an empty list to request all protected files
foreach (@infiles) {
  if (!$_) {
    next;
  } elsif ($_ eq 'all') {
    @files= ();
    last;
  } else {
    push(@files,$_);
  }
}

$request= $input->param('request');

if ($request eq 'changed') {
  $check_mask= 0;
  @mask= $input->param('check_mask');
  foreach (@mask) {
    $check_mask+= $_;
  } 
} else {
  $check_mask= undef;
}


my $port= &get_responce_port;
&setup_rcv_tcp_connection($port);
$port= &send_tw_request($host,$request,$check_mask,$port,@files);
#print "going to accept\n";
&accept_tcp_connection;
#print "\ngot accepted\n";
#sleep 10;

&end_part;



print <<">>";
Content-type:  text/html

<HTML>
<HEAD>
<TITLE>Results of querying tripwire on $host</TITLE>
</HEAD>
<body bgcolor="#EEFFEE" link="#ee0000" alink="#FF3300" vlink="#0000cc">
<H1>Results of querying tripwire on $host</H1>
This is what the agent reported:
<PRE>
>>


&print_tcp_input;

print <<">>";
</PRE><P>
<HR>
<A HREF="http://seclab.cs.ucdavis.edu/arpa/secret/cgi-bin/main.pl">Return to main page</A><BR>
<A HREF="http://seclab.cs.ucdavis.edu/arpa/secret/cgi-bin/component_request.pl">Return to component request page</A>
</BODY>
</HTML>
>>
&close_tcp_connection;

1;
