#!/usr/local/bin/perl
################################################################################
#                                                                              #
# CORE.PL                                                                      #
#                                                                              #
# Just the shell of a CGI script, with no guts yet.                            #
#                                                                              #
################################################################################
### SERVER SPECIFIC PARAMETERS #################################################
################################################################################

# debug switch - 1/0 is on/off
  $debugger = 0;
  $debug = "";

# authentication code 
  $code = "lsakjdfh78";

################################################################################
### PARSE USER INPUT ###########################################################
################################################################################

if($ENV{'CONTENT_LENGTH'}){
  read(STDIN, $in, $ENV{'CONTENT_LENGTH'});
  $debug .= "<P>METHOD: POST</P>";
  }elsif($ENV{'QUERY_STRING'}){
  &scriptError(5);
  }else{
  $debug .= "<P>NO INPUT RECEIVED</P>";
  &startUp();
  }

$debug .= "\n\n<TABLE BORDER>";
@in = split(/[&;]/,$in);
foreach $i (0 .. $#in) {
   $in[$i] =~ s/\+/ /g;
   ($key, $val) = split(/=/,$in[$i],2);
   $key =~ s/%([A-Fa-f0-9]{2})/pack("c",hex($1))/ge;
   $val =~ s/%([A-Fa-f0-9]{2})/pack("c",hex($1))/ge;
   $in{$key} .= "\0" if (defined($in{$key}));
   $in{$key} .= $val; 
   $debug .= "<TR><TD>$key</TD><TD>$val</TD></TR>";
   }
$debug .= "</TABLE>\n\n";

################################################################################
### ABUSE PREVENTION ###########################################################
################################################################################

&scriptError(2) unless ($in{'c'} eq $code); 

################################################################################
### DO THE DO ##################################################################
################################################################################

unless ($in{'u'} && $in{'p'}){
 $o = "<H1>Say What?</H1>\n\n";
 $o .= "<P>A username and password must be provided to create the ";
 $o .= "line for <TT>htpasswd</TT>.</P>";
 $o .= &deForm;
 &display("$o");
 }


srand;
@saltchars = ('a'..'z','A'..'Z',0..9);
$salt = $saltchars[int(rand($#saltchars+1))];
$salt .= $saltchars[int(rand($#saltchars+1))];
$crypted = crypt($in{'p'},$salt);
$deline = "$in{'u'}:$crypted";


# $debug .= "<P>YO:<BR>@saltchars<BR>$salt<BR>$crypted</P>";


$o = "<H1>Password Encryption</H1>\n\n";
$o .= "<P>Here's the line to add to <TT>htpasswd</TT> for the username ";
$o .= "and password you provided:</P>\n";
$o .= "<PRE>$deline\n</PRE><P>&nbsp;</P>\n\n";
$o .= "<P>If you need to set up another username/password pair, or are just ";
$o .= "easily amused, you can do it again:</P>";
$o .= &deForm;
&display("$o");


################################################################################
### INITIAL FUNCTION ###########################################################
################################################################################

sub startUp{
 $o = "<H1>Password Encryption</H1>\n\n";
 $o .= "<P>Enter a username and password, and the script will provide ";
 $o .= "a line for an <TT>htpasswd</TT> file.</P>";
 $o .= &deForm;
 &display("$o");
 }

################################################################################
### RENDER FORM ################################################################
################################################################################

sub deForm{
  $d = "<FORM METHOD=\"POST\" ACTION=\"password.jsp\">\n";
  $d .= "<INPUT TYPE=\"HIDDEN\" NAME=\"c\" VALUE=\"$code\"><TABLE>\n";
  $d .= "<TR><TD>Username:</TD>";
  $d .= "<TD><INPUT TYPE=\"TEXT\" NAME=\"u\" SIZE=\"20\"></TD></TR>\n";
  $d .= "<TR><TD>Password:</TD>";
  $d .= "<TD><INPUT TYPE=\"TEXT\" NAME=\"p\" SIZE=\"20\"></TD></TR>\n";
  $d .= "<TR><TD></TD><TD><INPUT TYPE=\"SUBMIT\" VLAUE=\"Encrypt\"</TD></TR>\n";
  $d .= "</TABLE></FORM>\n\n";
  return $d;
  }


################################################################################
### SUBROUTINE: FATAL ERROR MESSAGES ###########################################
################################################################################

sub scriptError{
  $o .= "<H1>Error Type $_[0]</H1>\n\n<P>";
  $o .= "EOF error - no data returned." if ($_[0] eq '1');
  $o .= "Invalid authentication paramter." if ($_[0] eq '2');
  $o .= "Missing action paramter" if ($_[0] eq '3');
  $o .= "Invalid paramter $in{'a'}" if ($_[0] eq '4');
  $o .= "Invalid Method" if ($_[0] eq '5');
  $o .= "" if ($_[0] eq '6');
  $o .= "" if ($_[0] eq '7');
  $o .= "" if ($_[0] eq '8');
  $o .= "" if ($_[0] eq '9');
  $o .= "" if ($_[0] eq '10');
  $o .= "</P>\n\n";
  &display($o);
  }

################################################################################
### SUBROUTINE: PRINT TO SCREEN ################################################
################################################################################

sub display{
  print "Content-type: text/html\n\n";
  $switch = 0;
  open (TMP,"password.tmp");
  while (<TMP>){
    chomp ($_);
    if ($_ eq '<!-- HERE -->'){
        print $_[0];
        }else{
        print "$_\n";
        }
     }
  close(TMP);
  print "<HR>$debug" if ($debugger); 
  exit;
  }

################################################################################
######################################################################### EOF ##
&scriptError(1); ###############################################################





























