decode.pl

#!/usr/bin/perl
# Author: Mike Moxcey, USDA APHIS VS beginning with work by Rick Fryar

if (!(defined($ARGV[0]))) {
&NoAddress; # exits with error msg
}
else {
$mailto = $ARGV[0]; # this is who the message is sent to
}
$origdestaddr = $mailto;
&CheckAddr;

 $temp = "../temp/temp.mail"; # this is a file in a public directory
open (TEMP, "> $temp");

$subj='Subject';
$Datarray{$subj}='No Subject Supplied from Form';

&MailInput;
&Envirovars;
close (TEMP);
system ("mail $mailto -s \'$Datarray{$subj}\' < $temp");
&SendReply;

############################ FUNCTIONS ###########

######################################
# error if this form is called wrong #
######################################

sub NoAddress {

print "Content-type: text/html\n\n";
print < <HEAD><TITLE>Calling Form Error</TITLE></HEAD> <BODY> <H1>Calling Form Error</H1>

The form you filled in didn't say to whom the results should be mailed.

If you know who created the form, please inform them of this error. If you don't know who is responsible for the form, contact the server administrator, <tt>webmaster\@$ENV{'SERVER_NAME'}</tt> and inform them of the time the error occured, the URL of the form, and anything else you can think of that might be relevant.

<address>webmaster\@$ENV{'SERVER_NAME'}</address>

</BODY>
EOE
exit 1;
}

##################################
# Verify no metachars in address #
##################################

sub CheckAddr {

$mailto =~ s/(\@[^.]*)\..*$/$1/;
$mailto .= '.usda.gov';

if ($mailto =~ /[^A-Za-z0-9.%:@-]/) {
# make sure no metacharacters in mail address
print <<EOM;
$ENV{'SERVER_PROTOCOL'} 500 Server Error
Date: $date
Server: $ENV{'SERVER_SOFTWARE'}
MIME-version: 1.0
Content-type: text/html

<HEAD><TITLE>500 Server Error</TITLE></HEAD>

<BODY>

<H1>500 Server Error</H1>
Sorry, the person who created the form you just filled out specified a bad address to mail the results to. It was $origdestaddr, which contains a character that is not usually found in addresses. For security reasons, I'm not going to attempt to mail to this address.

If you know who created the form, please inform them of this error. If you don't know who is responsible for the form, contact the server administrator, <tt>webmaster\@$ENV{'SERVER_NAME'}</tt> and inform them of the time the error occured, the URL of the form, and anything else you can think of that might be relevant.<P>

<address>webmaster\@$ENV{'SERVER_NAME'}</address>

</BODY>
EOM
exit 1;
}
}

#############################################################
# this function will break apart the HTML input into fields #
#############################################################

sub MailInput {

*in = @_ if @_;
local ($i, $key, $val);
read(STDIN, $in, $ENV{'CONTENT_LENGTH'});
@in = split(/[&;]/, $in);
foreach $i (0..$#in){
($key, $val) = split(/=/,$in[$i],2);
$key =~ s/%(..)/pack("c",hex($1))/ge;
$val =~ s/%(..)/pack("c",hex($1))/ge;
$val =~ s/\+/ /g;
print (TEMP "$key: $val\n\n");
$Datarray{$key} = $val; #make assoc array
}
}

#############################################################
# This function inserts environment variables for more info #
#############################################################

sub Envirovars {

chop($date = `date '+%d-%h-%y %T'`);
print (TEMP "Date: $date \n");
print (TEMP "IP:$ENV{'REMOTE_ADDR'}\n");
print (TEMP "\n");
}

# This function sends successful reply

sub SendReply {

print "Content-type: text/html\n\n";
print <<EOM;
<HEAD> <Title>Form mailing</Title> </HEAD><BODY BGCOLOR="FFFFFF"> Thank you for the input.

 EOM if (defined($ENV{'HTTP_REFERER'}))
{
print "<A HREF=\"$ENV{'HTTP_REFERER'}\">Back</a>";
}
print "</BODY></HTML>";

 } 


Back to Scripts Intro
Mike Moxcey Feb 1997

Aug 1997