#!/usr/bin/perl

use CGI;

# cgi-bin for traceroute - v1.4b - new links added Apr 17th 1996
# July,Dec 1995 by Laurent Demailly - dl@hplyot.obspm.fr
# infos,... on http://hplyot.obspm.fr/~dl/wwwtools.html
# Free software - GNU GPL - AS IS, NO WARRANTY...
# please keep author's (my) reference...

# Redone in PERL 5.x by Matti Aarnio <matti.aarnio@zmailer.org>

# note: using ncsa server, it is recommended to name the script
# "nph-something", like nph-traceroute so outputs come as it is
# done and not at the end.

# configure the following according to your place :
# place name:

$PLACENAME="ZMailer.ORG server at SONERA network in Finland";

# traceroute full path (unix path):
$TRACEROUTE = '/home/httpd/zmailer/cgi-bin/traceroute.exe';

# this script relative url (absolute would work too)
$SCRIPTURL = '/cgi-bin/nph-traceroute';

select STDOUT; $| = 1;

#printf("Content-Type: TEXT/HTML\nPragma: no-cache\n\n");

printf('<HTML><HEAD><TITLE>Traceroute</TITLE>
<link rev="made" href="mailto:matti.aarnio@zmailer.org">
</HEAD><BODY>
<H1>Traceroute from %s</H1>
', $PLACENAME);

die('"traceroute" program not executable at this system!')
 unless ( -x $TRACEROUTE );


$query = new CGI;
$domain = $query->param('DOMAIN');

if (!defined $domain || $domain eq '') {
    $domain = $ENV{'REMOTE_ADDR'};

    printf("No domain given!<P>\n");
    printf("Will try trace back to caller, or their web-proxy...<P>\n");
    
}

$MODE = ""; #-AIO";
$MODESET = "modeset: "; #AS-query + ICMP ECHO + Owner-query";
$MODESEP = '{';

if ($query->param('ASQ') eq 'on') {
    $MODE .= "A";
    $MODESET .= $MODESEP . "AS-Query ";
    $MODESEP = ',';
}
if ($query->param('ICMP') eq 'on') {
    $MODE .= "I";
    $MODESET .= $MODESEP . "ICMP-Query ";
    $MODESEP = ',';
}
if ($query->param('OWN') eq 'on') {
    $MODE .= "O";
    $MODESET .= $MODESEP . "SOA-Owner-Query ";
    $MODESEP = ',';
}
if ($MODESEP eq '{') {
    $MODESET .= 'none';
} else {
    $MODESET .= '}';
}

if ($MODE ne '') {
    $MODE = '-' . $MODE;
}

#
# printf("<PRE>\n");
# foreach $env (keys %ENV) {
#     printf("%-20s :  %s\n", $env, $ENV{$env});
# }
# printf("</PRE>\n");
#

if (defined $domain) {

    $domain =~ tr/A-Za-z0-9.-//cd;

    printf("Result for <B>%s</B>;&nbsp;&nbsp; %s:\n<PRE>\n",$domain, $MODESET);
#printf "%s\n","/bin/nice $TRACEROUTE $MODE -w 2 -- $domain 2>&1";
    system("/bin/nice $TRACEROUTE $MODE -w 2 -- $domain 2>&1");
    printf("</PRE>\n");

} else {

# printf("<PRE>\n");
# foreach $env (keys %ENV) {
#     printf("%-20s :  %s\n", $env, $ENV{$env});
# }
# printf("</PRE>\n");

}

exit 0;
