#! /srvtools/http/bin/perl
#############################################################################
#
#   Copyright 1995 by the Northeast Parallel Architectures Center at 
#   Syracuse University and Global Business Network, Inc..
#  
#   File: CheckPasswd.pl
#
#   Authors:  Jungmin Kim and Paul Coddington (paulc@npac.syr.edu)
#
#   Created:  03/05/95
#   Version: 
#
#   Output: user information or 0
#
#
#   Description: Check out whether the given password or name is in passwd 
#                file or not.
#                Save user information into checkfile.
#          
#   Usage:         "http://.../WeBBoard/check"
#
#   Updates:
#   July 10 1995 by PDC
#   - Changed so that reads/writes info from/to check file for each WeBBoard, 
#     i.e. from $subdir/$checkfile. Used to open $fcheckfile (whatever that is).
#
#############################################################################

sub CheckPasswd{
   local($subdir, $uid, $passwd) = @_;
   local($userid, $upass, $name, $email);

   if($uid =~ /^guest/i) {return "guest";}

   if ($subdir) {
      $thischeckfile = "$webboardhome/$subdir/$checkfile";
   }
   else {
      $thischeckfile = "$webboardhome/$checkfile";
   }

   # Check that this is the right password file
   $thischeckfile = CheckFile($thischeckfile);

   open(INFILE, "$thischeckfile") ||
      print "Content-type\: text/html\n\n Cannot open $thischeckfile\: $!";
   
   while(<INFILE>) {
	($userid, $upass, $name, $email) = split(/\:/, $_);
	if(($userid eq $uid) && ($upass eq $passwd)) {
	    $_ = $name.":".$email;
	    s/ /+/g;
	    return $_;
	}
   }
   close(INFILE);
   "0";
}
1;

sub CheckName{
   local($subdir, $uid) = @_;
   local($userid, $upass, $name, $email);
   
   if ($subdir) {
      $thischeckfile = "$webboardhome/$subdir/$checkfile";
   }
   else {
      $thischeckfile = "$webboardhome/$checkfile";
   }

   # Check that this is the right password file
   $thischeckfile = CheckFile($thischeckfile);

      
# Debug("\n User $uid Subdir $subdir File $thischeckfille\: $!");
      
   open(INFILE, "$thischeckfile") ||
      print "Content-type\: text/html\n\n Cannot open $thischeckfile\: $!";

   while(<INFILE>) {
	($userid, $upass, $name, $email) = split(/\:/, $_);
	if($userid eq $uid) {
	    $_ = $name.":".$email;
	    s/ /+/g;
	    return $_;
	}
   }
   close(INFILE);
   "0";
}
1;

sub SaveName{
    local($subdir, $uid, $passwd, $name, $email) = @_;

   if ($subdir) {
      $thischeckfile = "$webboardhome/$subdir/$checkfile";
   }
   else {
      $thischeckfile = "$webboardhome/$checkfile";
   }

   # Check that this is the right password file
   $thischeckfile = CheckFile($thischeckfile);

   system("cp $thischeckfile $thischeckfile.bak");
   open(OUTFILE, ">>$thischeckfile") ||
      print "Content-type\: text/html\n\n Cannot open $thischeckfile\: $!";
   print OUTFILE "$uid\:$passwd\:$name\:$email\n";
   close(OUTFILE);
   # chmod 400, "$thischeckfile";
   $_ = $name.":".$email;
   s/ /+/g;
   $_;
}
1;

sub GetPasswd{
    local($subdir, $uid) = @_;
    local($userid, $upass, $name, $email, $line);
   
   if ($subdir) {
      $thischeckfile = "$webboardhome/$subdir/$checkfile";
   }
   else {
      $thischeckfile = "$webboardhome/$checkfile";
   }

   # Check that this is the right password file
   $thischeckfile = CheckFile($thischeckfile);

   open(INFILE, "$thischeckfile") ||
      print "Content-type\: text/html\n\n Cannot open $thischeckfile\: $!";

   while(<INFILE>) {
	($userid, $upass, $name, $email) = split(/\:/, $_);
	$line = $_;
	if($userid eq $uid) {
	    return $_;
	}
   }
   close(INFILE);
   "0";
}
1;

sub GetAddress{
    local($subdir, $uname) = @_;
    local($userid, $upass, $name, $email, $line);

   if ($subdir) {
      $thischeckfile = "$webboardhome/$subdir/$checkfile";
   }
   else {
      $thischeckfile = "$webboardhome/$checkfile";
   }

   # Check that this is the right password file
   $thischeckfile = CheckFile($thischeckfile);

   open(INFILE, "$thischeckfile") ||
      print "Content-type\: text/html\n\n Cannot open $thischeckfile\: $!";
   
   while(<INFILE>) {
	($userid, $upass, $name, $email) = split(/\:/, $_);
	$line = $_;
	if($name eq $uname) {
	    chop($email);   ## Remove end-of-line character
	    return $email;
	}
   }
   close(INFILE);
   "";
}
1;



# Check to see if a password file exists at this level.
# If not, figure out which one to use.
# (Need to make this recursive)
sub CheckFile{
    local($thischeckfile) = @_;
    local($dir1, $dir2);

   # See if there is a passwd file at this level.
   # If not, use the one at a higher level.
   if (-e $thischeckfile) {
     $thischeckfile;
   }
   else {
	($dir1, $dir2) = split(/\//, $subdir);
      $thischeckfile = "$webboardhome/$dir1/$checkfile";
   }
   $thischeckfile;
}
1;
