#!/usr/bin/perl
# author: C. Edward Chow
# date:   11/9/2000
# copyright © 2000
# free distribution for research and education
# but please retain the above author, date and copyright lines.
#
use CGI qw(:standard);

use Authen::PAM;
#

$assignment="cs691hw3";
$dbm=$assignment. "\.dbm";
$webpage=$assignment . "reserve.htm";

#  print "your login:";
#  $username = <STDIN>;
#  chomp($username);
#  print "username=$username\npassword:";
#  $password = <STDIN>;
#  chomp($password);
#  print "passwd=$password...\n";
#  if (auth($username, $password)) {
#     print "login correct";
#  } else {
#     print "login incorrect";
#  }
#  exit(0);


print header('text/html');
print "<html><head><title>CS691 HW3 Lab Reservation Result</title></head><body>";
print "<h1>CS691 HW3 Lab Reservation Result</h1>";
print hr;
@names = param;
foreach $name (@names) {
	$value = param($name);
        if ($name ne "passwd") { print "$name=$value<br>";}
        if ($value eq "Reserve") { 
           $reserveSlot=$name; 
           $command = $value;
        }
        $_ = $value;
        if (/Cancel reservation by (\w+)/) {
            $command = "Cancel";
            $reservedBy = $1;
            $reserveSlot=$name; 
            print "cancel the reservation by $1\n<BR>";
        }
}
print "reserveSlot=$reserveSlot\n<BR>";
$username = param('login');
$password =param('passwd');
#print "verify login=$username passwd=$password\n<BR>";
print "verify login=$login\n<BR>";
if ($username eq "") {
      print "You need to enter login for reservation.\n<hr>\n";
      open(WEBPAGE, "reserve/$webpage");
      while(<WEBPAGE>) {
         print $_;
      }
      close(WEBPAGE); 
      exit(1);
} else {
	if (auth($username, $password)) { 
	   $search = 1; #pass! 
	} else { 
	   $search=0; #fail
      print "password not correct! <hr>\n";
      open(WEBPAGE, "reserve/$webpage");
      while(<WEBPAGE>) {
         print $_;
      }
      close(WEBPAGE); 
      exit(1);
   }
}
dbmopen(%reserve, "reserve/$dbm", 0666);
$slot = $reserve{$reserveSlot};
#print "slot=$slot<br>/n";
if ($command eq "Reserve") {
   if ($slot eq "") {
      print "$reserveSlot not reserved; reserving...";
      $reserve{$reserveSlot} = $username;
      print "<BR>$fullname: the slot $reserveSlot is reserved for you!\n<BR>";
      # move up the above print line for better web page look.
      # logically it should be after the web page is updated.
      # update the web page
      open(WEBPAGE, "reserve/$webpage");
      open(UPDATE, ">reserve/updatecs691reserve.htm");
      while(<WEBPAGE>) {
         if (/$reserveSlot/) {
            s/Reserve/Cancel reservation by $username/;
            
         } 
         print UPDATE $_;
         print $_;
      }
      close(WEBPAGE); close(UPDATE);
      system("mv reserve/updatecs691reserve.htm reserve/$webpage");
   } else {
      print "$reserveSlot reserved by $slot! Try different time slot\n<BR>";
   }
} else { # Cancel
   if ($username eq $slot) {
      print "prepare to cancel...";
      $reserve{$reserveSlot} = ""; # reset
      print "<BR>$username: the reservation on slot $reserveSlot is canceled!\n<BR>";
      # move up the above print line for better web page look.
      # logically it should be after the web page is updated.
      # update web page
      open(WEBPAGE, "reserve/$webpage");
      open(UPDATE, ">reserve/updatecs691reserve.htm");
      while(<WEBPAGE>) {
         if (/$reserveSlot/) {
            s/Cancel reservation by $username/Reserve/;
         } 
         print UPDATE $_;
         print $_;
      }
      close(WEBPAGE); close(UPDATE);
      system("mv reserve/updatecs691reserve.htm reserve/$webpage");
   } else { # login and reserveby not matched
      print "The slot is reserved by others.  Please select the timeslot
reserved by you.<BR>";
   }
}
print "</body></html>";

sub auth {
  my ($username, $password) = @_;
  $service = "login";
  
  ref($pamh = new Authen::PAM($service, $username, \&my_conv_func)) ||
         die "Error code $pamh during PAM init!";
  $res = $pamh->pam_set_item(PAM_TTY(), $tty_name);
  $res = $pamh->pam_authenticate;
  #print $pamh->pam_strerror($res),"\n" unless $res == PAM_SUCCESS();
  if ($res==PAM_SUCCESS()) {
     return 1;
  } else {
     return 0;
  }
}

sub my_conv_func {
    my @res;
    while ( @_ ) {
        my $code = shift;
        my $msg = shift;
        my $ans = "";
        $ans = $username if ($code == PAM_PROMPT_ECHO_ON() );
        $ans = $password if ($code == PAM_PROMPT_ECHO_OFF() );
        push @res, (PAM_SUCCESS(),$ans);
    }
    push @res, PAM_SUCCESS();
    return @res;
  }
