# Misc Password Change Functions.
#
$HTML="/admin";

#Password Daemon Parameters;
$pwdport = 106;
$clientid = "ds1.dsoln.com.au";
$serverid = "ds1.dsoln.com.au"; 

###---------------------------------------------------------------------------------------###
sub print_header() {
	print "Content-type: text/html\n\n";
	print "<html>\n";
	print "<head>\n";
	print "<title>Dynamic Solutions Customer Reporting</title>\n";
	print "</head>\n";

}

###---------------------------------------------------------------------------------------###

sub print_body() {
	print "<body background=\"/images_dynamic/stone.jpg\">\n";
	print "<table width=100% border=0 cellpadding=10><tr><td>\n";    
	print "<a href=\"http://www.dynamic.net.au\"><img src=\"/images_dynamic/ds_medium.gif\" border=0></a>\n";
        print "</td><td align=\"left\" valign=\"top\"><font size=-1>\n";
        print "For all your computing and networking needs.<br>\n";
        print "414 Gilbert Road Preston, Vic 3072 Australia<br>\n";
        print "ACN 074 259 902<br>94718224 (telephone) 94711622 (fax)\n";
        print "</font></td></tr></table>\n";
	print "<h4 align=right>$_[0]<hr width=100%></h4>\n";
}

###---------------------------------------------------------------------------------------###

sub print_footer()
{
	print "</body>\n";
	print "</html>\n";
}

###---------------------------------------------------------------------------------------###

sub err_report()
{
    select(STDOUT);
    print "<Center><H2><Blink>Error:</Blink> $_[0]<BR><BR><BR>Please see your System
    Administrator</H2></Center>\n";
    if ( length( $contents{adminusername} ) == 0 )
    {
    	print "<a href=$HTML/userpasswd.html>User: Try Again?</a><br>";
    }   
    else
    {
    	print "<a href=$HTML/adminpasswd.html>Admin: Try Again?</a><br>";  
    }
    print "<a href=$HTML/index.html>Return to Customer Access Page</a>\n";  
    print_footer();
    exit(1);
}

#----------------------------------------------------------------------------------------------------

sub change_password()
{
	$adminusername = $_[0];
	$adminpasswd = $_[1];
	$username = $_[2];
	$useroldpasswd = $_[3];
	$usernewpasswd = $_[4];


	($d1, $d2, $prototype) = getprotobyname( "tcp" );
	($d1, $d2, $d3, $d4, $rawclient) = gethostbyname($clientid);
	($d1, $d2, $d3, $d4, $rawserver) = gethostbyname($serverid);
	$clientaddr = pack( "Sna4x8",2,0,$rawclient ); 
	$serveraddr = pack( "Sna4x8",2,$pwdport,$rawserver );
	socket( SOCKET,2,1,$prototype) || die( "No Socket" );
	bind( SOCKET,$clientaddr) || die( "Can't Bind Socket" );
	$cstat=connect(SOCKET, $serveraddr );
	select(SOCKET);
	$|=1;		#Arrange to flush cache

	if ( $cstat != 0 )
	{
		if ( $adminusername eq "none" )
		{
			#User change of Password All following sent to password daemon.
			&get_reply();
			print "user $username\n";
			&get_reply();
			print "pass $useroldpasswd\n";
			&get_reply();
			print "newpass $usernewpasswd\n";
			&get_reply();
			select(STDOUT);
		}
		else
		{
			#Admin Change of Password
			&get_reply();
			print "admin $adminusername $adminpasswd\n";
			&get_reply();
			print "user $username\n";
			&get_reply();
			print "pass none\n";
			&get_reply();
			print "newpass $usernewpasswd\n";
			&get_reply();
			select(STDOUT);
		}
	}
	else
	{
		&err_report( "Password Server is Not Responding" );
	}
	close(SOCKET);
}
#----------------------------------------------------------------------------------------------------

sub get_reply()
{
	$line=<SOCKET>;
	$v = substr( $line,0,3 );
	if ( $v ne "200" )
	{
		&err_report( $line );
	}
}

#----------------------------------------------------------------------------------------------------

1;
