Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sub not returning vars

Status
Not open for further replies.

JimJx

Technical User
Joined
Feb 16, 2001
Messages
202
Location
US
Hello again all,

I wrote the little page below to check if everything was passing properly.

Everything works as it should and prints out what I expect to see.

However, when I run check_password, I get nada out of it when it hits the last statement before the sub.

Am I doing the return wrong or can you spot something else that I FUBAR'd?

As always, thanks for reading and I appreciate any suggestions.
Jim

Code:
#!/usr/bin/perl

require "WAccessLib.pl";

my($flogin, $fpass, $flevel, $fpage, $Cpw);

&ReadParse(*input);
my $mode = "guest";
my $file = $MEMBER_FILE;
my $login = $input{'login'};
my $password = $input{'pass'};
my $page = $input{'page'};

if (($input{'ac'} eq "chpass") && (! $input{'BT_Exit'})) {
	&change_password($login, $password, %input);
	exit;
} 


print "content-type:text/html\n\n";

print "Login: $login - Password: $password - Mode: $mode - File: $file<p />\n";

open(PASSWORD, "$PASSWORD_FILE") || &Error("Cannot open password file: $PASSWORD_FILE, Error: $!",1);
	@PassFile = <PASSWORD>;
close(PASSWORD);

&check_password($mode,$login,$password);

$Cpw = &CryptPassword($password);
print "<b>Cpw: $Cpw<P /></b>\n";
foreach (@PassFile) {
($Fname, $Fpw, $Flevel, $Ffile) = split(/\s+/);
print "Fname: $Fname, Fpw: $Fpw, Flevel: $Flevel, Ffile: $Ffile<br>\n";
if ($Cpw eq $Fpw) {
$match = YES;
} else {
$match = No;
}
print "<b>MATCH?: $match<P /></b>\n";
}


print "<p /><b>$flogin - $fpass - $flevel<P /></b>\n";


sub check_password {
$mode=$input{'mode'};
$login=$input{'login'};
$password=$input{'password'};
my($flogin, $fpass, $flevel);

        open(PASSWORD, $PASSWORD_FILE) or die "$!";
        @pass = <PASSWORD>;
        close password;
        foreach (@pass) {
          ($flogin, $fpass, $flevel)= split(/\s+/);
             if (($flogin eq $login) && ($fpass eq &CryptPassword($password))){
              return ($flogin, $fpass, $flevel);
            }
    return 0;
}
}
 
&check_password isn't returning any vars because you're not receiving them!

Code:
($flogin,$fpass,$flevel) = &check_password($mode,$login,$password);

Subroutines don't return variables into the global environment, you must receive them from the subroutine in this way.

-------------
Kirsle.net | Kirsle's Programs and Projects
 
Thanks for the quick reply Kirsle!

I made the change that you suggested, and the sub is still returning 0

Here is the output I am getting....

Login: TEST1 - Password: 0001 - Mode: guest - File: Member.html

Cpw: 00SXHKDIsPEFA

Fname: TEST1, Fpw: 00SXHKDIsPEFA, Flevel: guest, Ffile: member
MATCH Name?: YES MATCH Pass?: YES

Fname: TEST2, Fpw: 00ZQ0o4WPqVUQ, Flevel: guest, Ffile: member
MATCH Name?: No MATCH Pass?: No

0 - -

TEST1 00SXHKDIsPEFA guest member

TEST2 00ZQ0o4WPqVUQ guest member
 
If the sub is returning 0, it is probably because this:

Code:
             if ([COLOR=red]($flogin eq $login) && ($fpass eq &CryptPassword($password))[/color]){
              return ($flogin, $fpass, $flevel);
            }

is returning false. Try adding print statements around this area:

Code:
             print "Checking conditions...\n";
             if (($flogin eq $login) && ($fpass eq &CryptPassword($password))){
              print "Conditions met!\n";
              return ($flogin, $fpass, $flevel);
            }
            else {
              print "conditions not met\n";
            }

A few extra print's here and there are good ways to debug a program. :-)

-------------
Kirsle.net | Kirsle's Programs and Projects
 
Running your code through perltidy to make it readable:

Code:
#!/usr/bin/perl

require "WAccessLib.pl";

my ( $flogin, $fpass, $flevel, $fpage, $Cpw );

&ReadParse(*input);
my $mode     = "guest";
my $file     = $MEMBER_FILE;
my $login    = $input{'login'};
my $password = $input{'pass'};
my $page     = $input{'page'};

if ( ( $input{'ac'} eq "chpass" ) && ( !$input{'BT_Exit'} ) ) {
    &change_password( $login, $password, %input );
    exit;
}

print "content-type:text/html\n\n";

print "Login: $login - Password: $password - Mode: $mode - File: $file<p />\n";

open( PASSWORD, "$PASSWORD_FILE" )
  || &Error( "Cannot open password file: $PASSWORD_FILE, Error: $!", 1 );
@PassFile = <PASSWORD>;
close(PASSWORD);

&check_password( $mode, $login, $password );

$Cpw = &CryptPassword($password);
print "<b>Cpw: $Cpw<P /></b>\n";
foreach (@PassFile) {
    ( $Fname, $Fpw, $Flevel, $Ffile ) = split(/\s+/);
    print "Fname: $Fname, Fpw: $Fpw, Flevel: $Flevel, Ffile: $Ffile<br>\n";
    if ( $Cpw eq $Fpw ) {
        $match = YES;
    }
    else {
        $match = No;
    }
    print "<b>MATCH?: $match<P /></b>\n";
}

print "<p /><b>$flogin - $fpass - $flevel<P /></b>\n";

sub check_password {
    $mode     = $input{'mode'};
    $login    = $input{'login'};
    $password = $input{'password'};
    my ( $flogin, $fpass, $flevel );

    open( PASSWORD, $PASSWORD_FILE ) or die "$!";
    @pass = <PASSWORD>;
    close password;
    foreach (@pass) {
        ( $flogin, $fpass, $flevel ) = split(/\s+/);
        if ( ( $flogin eq $login ) && ( $fpass eq &CryptPassword($password) ) )
        {
            return ( $flogin, $fpass, $flevel );
        }
        return 0;
    }
}

The truth is ... there are just so many things wrong with the structure of this code, that I don't even know where to begin. The way that your passing and returning variables to the function check_password still seem particularly shady. Where's your @_?

Good Luck.
 
This isn't meant to be the end of this.... It started as more of a way to see what was being returned by the sub since I had problems with it.

This file was banged out in about 2 minutes to see if I could get the sub to pass the correct values back and to see why it wasn't happening in the live version of the script.....
 
You have a live script that uses this method?

&ReadParse(*input);


that is nearly 10 years out of date. Upgrade to using CGI.pm if you can.


- Kevin, perl coder unexceptional!
 
Written by someone else and I am on a deadline to get this thing done. It was one of the things that I suggested, but I was told that that doesn't really need to be changed, just work with it and *maybe* they would 'get around' to changing it later. I will be so glad to get this project over with. Hopefully a couple more days wil do it.

I have said it before, I hate working with someone else's code.......

Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top