#!/usr/local/bin/perl
# passreg.pl
#This script will take form info log it to people.list, select a random password, and then e-mail #the password to the e-mail address in the form data.
#copyright CSNET written by Brian Maguire with the help(lots) of Rod Clark and the other #million scripts that I looked at to make this.  Use by permission only contact #maguireb@norwich.net
###################################################################

$SendMail       = "/usr/lib/sendmail";

# create this file where the Web server can write to it
# set permissions to world-writable, not world-readable (chmod 622)
$PeopleFile     = "/export/home/www/mirroreddns/secure/people.list";

$Subject        = "Mirror DNS Password";
$FromAddress    = "maguireb@norwich.net";
$ReplyToAddress = "maguireb@norwich.net";
$CC = "maguireb@norwich.net";   # administrator gets a copy 
$Message        = "Your Password:";
$MessageHref  = "Now you may go to \n
http://www.csnet.net/mirroreddns/mirroreddns.html\n
 and enter the secure\n\n 
area.  Please keep this in a safe place."; 

&LocalReadParse;
$UserName      = $in{'username'};
$FirstName     = $in{'firstname'};
$LastName      = $in{'lastname'};
$PostalAddress = $in{'address'};
$Occupation    = $in{'occupation'};
$random	= time()+$$;

&PrintThanksPage;
&SendUserMail;
&LogUser;


sub PrintThanksPage
{
  &PrintHeader;
  print <<ENDPRINT;
<HTML>
<HEAD>
<TITLE>Thanks</TITLE>
</HEAD>
<BODY bgcolor="#FFFFE4" background="http://www.aa.net/~rclark/ivory.gif">

<H2>Your request has been sent and is probably in your mainbox already.</H2>

<b>Thanks for registering!</b>

<p>
If you included your correct e-mail address, a password
will be e-mailed to you automatically.

</BODY>
</HTML>

ENDPRINT
}

sub SendUserMail
{
	
  open (SENDMAIL, "|$SendMail -t") || die "Can't open sendmail: $!\n";

  print SENDMAIL <<ENDPRINT;
To: $UserName
Cc: $CC
From: $FromAddress
Reply-To: $ReplyToAddress
Subject: $Subject

$Message
$random

ENDPRINT
  close (SENDMAIL);
	}


sub LogUser
{
  # log the username, firstname, lastname, occupation and comment from the
  # registration form, and the newly created password, to the database file
  # people.list

  open (PEOPLE, ">>$PeopleFile");
  print PEOPLE "$UserName $random $FirstName $LastName $PostalAddress $Occupation\n";
  close (PEOPLE);
}
	

#=====================================================================
# GENERAL PURPOSE (BOILERPLATE) ROUTINES
#=====================================================================

sub LocalReadParse
# from Steven Brenner's cgi-lib.pl v1.14
{
  local (*in) = @_ if @_;
  local ($i, $key, $val);

  # Read in text
  if (&LocalMethGet)
  {
    $in = $ENV{'QUERY_STRING'};
  }
  elsif (&LocalMethPost)
  {
    read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
  }

  @in = split(/[&;]/,$in);

  foreach $i (0 .. $#in)
  {
    # Convert plusses to spaces
    $in[$i] =~ s/\+/ /g;

    # Split into key and value
    # splits on the first =
    ($key, $val) = split(/=/,$in[$i],2);

    # Convert %XX from hex numbers to alphanumeric
    $key =~ s/%(..)/pack("c",hex($1))/ge;
    $val =~ s/%(..)/pack("c",hex($1))/ge;

    # Associate key and value
    # \0 is the multiple separator
    $in{$key} .= "\0" if (defined($in{$key}));
    $in{$key} .= $val;
  }
  return scalar(@in);
}

sub LocalMethGet
# from Steven Brenner's cgi-lib.pl v1.14
# true if this cgi call was using the GET request, false otherwise
{
  return ($ENV{'REQUEST_METHOD'} eq "GET");
}

sub LocalMethPost
# from Steven Brenner's cgi-lib.pl v1.14
# true if this cgi call was using the POST request, false otherwise
{
  return ($ENV{'REQUEST_METHOD'} eq "POST");
}

sub PrintHeader
{
  print "Content-type: text/html\n\n";
  return;
}


