#!/usr/bin/perl
#--------------------------------------------------------------------------------------------
#	web_user_passwd.cgi - A User password change utility
#
#	Companion Web Page		:userpasswd.html & adminpasswd.html
#	Companion System Daemon		:poppassd Port 106
#	Companion Common File		:misc_passwd
#	Companion Permissions File	:/etc/popassd.table 
#
#	Written:		Chris Henry chenry@dynamic.net.au
#	Created:		4/9/97
#
#	History:
#
#	CH 4/9/97 - File first Created.
#	CH 11/9/97 - First Online Version 1.0
#--------------------------------------------------------------------------------------------

require "/etc/local/perl/misc";
require "misc_passwd";

#--- Main Line Code ---

&get_request();

&print_header();
&print_body();

$can_i_change = 0;

#Check Incoming Parameters
if ( length( $contents{adminusername} ) == 0 )
{
	if (
		( length($contents{username}) > 0 ) && 
		( length($contents{password}) > 0 ) && 
		( length($contents{password1}) > 0 ) &&
		( length($contents{password2}) > 0 )
	   )
	{
		$can_i_change = 1;
	}
}
else
{
       if (
                ( length($contents{adminusername}) > 0 ) &&
                ( length($contents{adminpassword}) > 0 ) && 
                ( length($contents{username}) > 0 ) &&
                ( length($contents{password1}) > 0 ) &&
                ( length($contents{password2}) > 0 )   
           )
        {   
                $can_i_change = 1;
        }       
}

if ( $can_i_change == 0 )
{
	print"<center><h2>You Must complete all fields on the password change form.";
        print" Use your browser back button to Try again or Consult your local Administrator.</h2></center>\n";
	print"<br><br>\n";
}
else
{
	if ( ( $contents{password1} ne $contents{password2} ) )
	{
		print"<center><h2>Your New Password is not the same as your Repeated New Password.";
        	print" These must agree. Try again or Consult your local Administrator.</h2></center>\n";
		print"<br><br>\n";
		if ( length( $contents{adminusername} ) == 0 )
		{
        		print "<a href=$HTML/userpasswd.html>User: Try Again?</a>";  
		}
		else
		{
        		print "<a href=$HTML/adminpasswd.html>Admin: Try Again?</a>";  
		}
		$can_i_change = 0;
	}
} 


#Change Password Section
if ( $can_i_change == 1 )
{
	if ( length( $contents{adminusername} ) == 0 )
	{
		&change_password("none","none",$contents{username}, $contents{password}, $contents{password1});
		print "<center><h2>Your Password has been changed Sucessfully";
		print "</h2></center>\n";
		print "<br><br>\n";
       	 	print "<a href=$HTML/index.html>Return to Customer Access Page</a>";
	}
	else
	{
		&change_password($contents{adminusername},$contents{adminpassword},$contents{username}, $contents{password}, $contents{password1});
		print "<center><h2>Your Password has been changed Sucessfully";
		print "</h2></center>\n";
		print "<br><br>\n";
       	 	print "<a href=$HTML/index.html>Return to Customer Access Page</a>";

	}
	
}

&print_footer();
