###############################################################################
#                       AUTH-LIB.PL
#
# This script was written by Selena Sol & Gunther Birznieks. 
# Date Created: 5-10-96
# Date Last Modified: 11-19-98
# Last modifications by Michael E. Schechter (mschechter@earthlink.net)
#
#   You may copy this under the terms of the GNU General Public
#   License or the Artistic License which is distributed with
#   copies of Perl v5.x for UNIX.
# 
# Selena Sol may be contacted at selena@extropia.com
#
# Purpose: Provides a set of library routines to provide
# a standard authentication front_end to CGI Programs
#
# Procedures:
#   GetSessionInfo - Returns the session information.  If the session
#					 file has not been created, bring in the
#					 auth-extra-lib.pl file to do user authentication.
#	auth_lib_fail_html - This subroutine provides an error message if
#						 a session file is passed in but can not be
#						 opened for some reason.
#
# Special Notes: 
#
# The script is written to be compact for returning session
# information since that is what the script does 99% of the time
# When the script needs to do more, it does a require on
# a huge auth-extra-lib.pl file that has all sorts of routines
# and options.
###############################################################################

###############################################################################
# Subroutine:  GetSessionInfo()
#
#   Parameters:
#         $session = Session ID.  Null if it is not defined yet.
#     $main_script = the script you are calling
#                    &GetSessionInfo From
#     		   *in = A reference to the form data that was read
#           		 in with &ReadParse from cgi-lib.pl.
#
#   Usage:
#		($session, $username, $group, @extra_fields)
#			= &GetSessionInfo($session, "script name", *in);
#
#   Output:
#     	 $session = session id
#     	$username = user name
#     	   $group = group information
#   @extra_fields = an array of fields usually consisting of the following:
#				$name = users real name
#			   $email = email address
###############################################################################

sub GetSessionInfo {
	local($session, $main_script, *in) = @_;
	local($session_file, @fields);
	
# Check to see if the session id is null.  If it is, load in the
# authentication library files and then call &VerifyUser, which
# is found in the newly required file, to log the user in.
#
# Else, if the session id is defined, we untaint it, open up the session
# file, read it in, and return the values contained therein after a split
# command.  We unshift the session id onto the array that we return.

	if ($session eq "") {
		require "$auth_lib/auth-extra-lib.pl";
		@fields = &VerifyUser($main_script, *in);
	} else {
		$session =~ /(\w*)/;
    	$session = $1;
    	$session_file = "$session.dat";
		open (SESSIONFILE, "$auth_session_dir/$session_file") || &auth_lib_fail_html;
		while (<SESSIONFILE>) {
			chomp($_);
			@fields = split(/\|/, $_);
		}
		close (SESSIONFILE);
		unshift(@fields, $session);
	}
	return (@fields);
}

###############################################################################
# Subroutine:  auth_lib_fail_html()
#
#   Parameters:
#		None
#
#   Usage:
#		&auth_lib_fail_html;
#
#   Output:
#		An HTML page informing the user that there was an error while
#		trying to open their session file.
###############################################################################

sub auth_lib_fail_html {

# If the session file was defined, but could not be opened for whatever reason,
# we print out an HTML page to let the user know that there was a failure in
# retrieving their information.  We also provide very basic instructions to
# the user on how to fix this (login again, basically).

	print "Content-type: text/html\n\n";
	print "<html><head><title>Error Occured</title></head>";
	print "<body$bodytag>\n$basefont_tag\n";
	print "<center><h3>Session File Error</h3></center><hr><blockquote>\n";
	print "<b>Sorry, there appears to be a problem accessing a session file.\n";
	print "<p>Please reload the URL, and sign in again.<b>\n";
	print "</blockquote>\n</body></html>";
	exit;
}

1;