#!perl
# Fig. 26.17: password.pl
# Program to search a database for usernames and passwords.

use CGI qw(:standard);

$testUsername = param( "USERNAME" );
$testPassword = param( "PASSWORD" );

open ( FILE, "password.txt" ) || 
   die "The database could not be opened";

while ( $line = <FILE> )
{
   chomp $line;
   ( $username, $password ) = split( ",", $line );
   
   if ( $testUsername eq $username )
   {
      $userVerified = 1;
      if ( $testPassword eq $password )
      {
         $passwordVerified = 1;
         last;
      }
   } 
}

close( FILE );
print header;

if ( $userVerified && $passwordVerified )
{
   accessGranted();
}
elsif ( $userVerified && !$passwordVerified )
{
   wrongPassword();
}
else
{
   accessDenied();
}

sub accessGranted
{
   print "<TITLE>Thank You</TITLE>";
   print "<FONT FACE = \"ARIAL\" SIZE = 2 COLOR = \"BLUE\">";
   print "<STRONG>Permission has been granted, $username.";
   print "<BR>Enjoy the site.</STRONG></FONT>";
}

sub wrongPassword
{
   print "<TITLE>Access Denied</TITLE>";
   print "<FONT FACE = \"ARIAL\" SIZE = 2 COLOR = \"RED\"><STRONG>";
   print "You entered an invalid password.<BR>";
   print "Access has been denied.</STRONG></FONT>";
}

sub accessDenied
{
   print "<TITLE>Access Denied</TITLE>";
   print "<FONT FACE = \"ARIAL\" SIZE = 3 COLOR = \"RED\"><STRONG>";
   print "You were denied access to this server.";
   print "</STRONG></FONT>";
}

########################################################################## 
# (C) Copyright 2001 by Deitel & Associates, Inc. and Prentice Hall.     #
# All Rights Reserved.                                                   #
#                                                                        #
# DISCLAIMER: The authors and publisher of this book have used their     #
# best efforts in preparing the book. These efforts include the          #
# development, research, and testing of the theories and programs        #
# to determine their effectiveness. The authors and publisher make       #
# no warranty of any kind, expressed or implied, with regard to these    #
# programs or to the documentation contained in these books. The authors #
# and publisher shall not be liable in any event for incidental or       #
# consequential damages in connection with, or arising out of, the       #
# furnishing, performance, or use of these programs.                     #
##########################################################################
