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!

Question on grep 1

Status
Not open for further replies.

JimJx

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

I have a sub that reads a file, finds the user and checks the password. Nothing that needs to be real secure, but something that makes it a little harder to get in.

Anyway, if I login using the first user listed in the file, all works well, past that, invalid password message.....

Any ideas why this is not going past the first line of the file?

Thanks,
Jim

Code:
sub check_password {
	my($mode, $login, $password) = @_;
	my($flogin, $fpass, $flevel, $fpage);

	if (($login  eq $UNIV_LOGIN) && ($password eq $UNIV_PASSWORD)) { return 1; }
	
	if ($mode && $login && $password) {
		open(PASSWORD, $PASSWORD_FILE);
	 	($_) = grep(/^$login\t/, <PASSWORD>);
	  	close(PASSWORD);
		s/\n//;
		($flogin, $fpass, $flevel, $fpage) = split /\t/;
    if ( ($flogin eq $login) && ($fpass eq &CryptPassword($password)) ) {
	    	 return 1;
		}
	}
	return 0;
}

The password file is:
Code:
JRC1        00SXHKDIsPEFA        guest        member
JRC2        00vW/ON1hNgpM        guest        member
JRC6        00QbzQRDCkj0s        guest        member
JRC7        00TocvUpWbIok        guest        member
 
Dude ... don't assign to $_. That's laziness that is bound to lead you into trouble.

Code:
open(PASSWORD, $PASSWORD_FILE);
[COLOR=blue]my ($line)[/color] = grep(/^[COLOR=blue]\Q[/color]$login[COLOR=blue]\E[/color]\t/, <PASSWORD>);
close(PASSWORD);
chomp $line;
($flogin, $fpass, $flevel, $fpage) = split /\t/, $line;

Also, treat your logins like literal strings instead of regular expressions. Most likely that wouldn't have introduced an error, but it's so easy to avoid that you might as well.

Other than that, things like ok.
 
Code:
sub check_password {
    my($mode, $login, $password) = @_;
    my($flogin, $fpass, $flevel, $fpage);

    if (($login  eq $UNIV_LOGIN) && ($password eq $UNIV_PASSWORD)) {
        return 1;
    }
    if ($mode && $login && $password) {
        open(PASSWORD, $PASSWORD_FILE) or die "$!";
        while( my($flogin, $fpass, $flevel, $fpage)= split(/\t/, <PASSWORD>)) {
             if (($flogin eq $login) && ($fpass eq &CryptPassword($password))){
                close(PASSWORD);
                return 1;
            }
        }
    }
    close(PASSWORD);
    return 0;
}

- Kevin, perl coder unexceptional!
 
Thanks for the replies!

Everything works great Kevin.

Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top