#!/usr/bin/perl -w

# $Id: query_component.pl,v 1.5 1996/02/12 18:48:30 hoagland Exp $

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

# this program sends a request to some agent on a given host, and displays the TCP result to the user's browser  for the ARPA IDS demo GUI

# Input is CGI, fields in a form
# 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));
}

# send the request to the component and return the TCP port to listen on for
# a responce
sub send_component_request {
  my ($dest_host,$port,%rec)= @_;
  $rec{'time'}= time unless $rec{'time'};
  my $hostname= $myhostname; # from require.pl
  #print "scr: sending ",join(' ',%rec)," to $dest_host with respond=<$hostname,$port>";
  &send_scl_message($dest_host, %rec, 'respond_to_host' => $hostname,'respond_to_port' => $port);
  return $port;
}
#-------------

&start_multipart_output;
$dest_host= $input->param('dest_host');
$input->delete('dest_host');
$agent= $input->param('agent_text');
$input->delete('agent_text');

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

%rec= map(($_ => $input->param($_)),$input->param);
if (($host=$rec{'hostname'}) || ($host=$rec{'host'})) {
  my $state= State->new(&get_statefile);
  $state->{'last_host'}= $host;
  $state->store;
}

# validate numeric fields
foreach ('pid','timestamp') {
  unless (!$rec{$_} || $rec{$_} =~ /^\d+$/) {
    &show_text_part("The entry in the $_ field must be numeric.");
    exit;
  }
}

  my $port= &get_responce_port;
&setup_rcv_tcp_connection($port);
$port= &send_component_request($dest_host,$port,%rec);
#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 $agent on $dest_host</TITLE>
</HEAD>
<body bgcolor="#EEFFEE" link="#ee0000" alink="#FF3300" vlink="#0000cc">
<H1>Results of querying $agent on $dest_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;
