Gossamer Forum
Home : General : Perl Programming :

Code help...

Quote Reply
Code help...
Hi. This bit of code is really getting on my nerves. Basically, what it is going to do is check to see if the username and password the enter exists. However, for some reason the && part (in bold) is nt matching. I added loads of debugging info to it BTW earlier t try and narrow down why the login thing wasnt working. The code is;

Code:
#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);
use CGI qw(:standard);
print "Content-type: text/html \n\n";
$input = new CGI;

$action = $input->param('action');

if ($action eq "login") {

# get the params

$password = $input->param('password');
$username = $input->param('username');

if ($password eq "") { &error("You need to specify a password!"); }


if ($username eq "") { &error("You need to specify a username!"); }


open (LIST, "/home/ace-clipart.com/passwords.list") || &error("Unable to open password.list...reason: $!");
flock(LIST,2);
@input = <LIST>;
close(LIST);

# search the file for a username
foreach $line (@input) {

print "$line <BR>";

if ($line =~ /$username\:\:+/) {

($ausername, $apassword) = split(/::/, $line);

print "$username & $password <BR>";
print "$ausername & $apassword <BR>";

if ($username eq $ausername && $apassword eq $password) { print "password ok..logged in"; last; } else { &error("Unable to login. Wrong password!"); }


} # end the regex if

} # end the foreach

} else { print "no info sent"; }




# an error sub incase something goes wrong.
sub error {

my $error = shift;
print $error;
exit;

}

It is probably something really silly, but I cant for the life of me see it Frown

Anyone a bit more awake than me?

Thanks

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] Code help... In reply to
Mmm try:
Code:
#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);
use CGI qw(:standard);

$input = new CGI;
$action = $input->param('action');

if ($action eq "login") {

$password = $input->param('password');
$username = $input->param('username');

!$password and &error("You need to specify a password!");
!$username and &error("You need to specify a username!");

open (LIST, "/home/ace-clipart.com/passwords.list") or &error("Unable to open password.list...reason: $!");
flock(LIST,2);
while (<LIST>) {
chomp;
@stuff = split /::/;
if (($stuff[0] eq $username) and ($stuff[1] eq $password)) {
$found = 1;
last;
}
}
close LIST;

!$found and &error("Unable to login.");

print $input->header;
print "Found";

}

sub error { print $input->header; print $_[0]; exit; }

It isn't tested so the chances of it working are slim but try it anyway.

Last edited by:

RedRum: Nov 10, 2001, 5:06 AM
Quote Reply
Re: [RedRum] Code help... In reply to
Nope. Didnt work Frown I cant see any real reason for it to be doing this Frown I did notice however, that the last entry in the file DOES work. But anything before it won't. Any ideas on why?

thanks

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Me!] Code help... In reply to
Quote:
($username eq $ausername && $apassword eq $password)

Try this:

(($username eq $ausername) && ($apassword eq $password))


easy does it
Quote Reply
Re: [Me!] Code help... In reply to
The code itself should work fine. All it is doing is splitting at :: and matching it against the input.

What exactly does the file look like?

You must have something wrong somewhere.

Last edited by:

RedRum: Nov 10, 2001, 6:07 AM
Quote Reply
Re: [Bearwithme] Code help... In reply to
Frown Still not working. I cant see why the heck it shouldnt work Frown

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] Code help... In reply to
I need more info.
Quote Reply
Re: [RedRum] Code help... In reply to
Thanks for the help.. I have fixed it now (with some help from one of my mates). Iam now using Grep to check the file for the correct string.

I'm using;

Code:
#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);
use CGI qw(:standard);
print "Content-type: text/html \n\n";
$input = new CGI;

$action = $input->param('action');

if ($action eq "login") {

# get the params

$password = $input->param('password');
$username = $input->param('username');

if ($password eq "") {
&error("You need to specify a password!");
}


if ($username eq "") {
&error("You need to specify a username!");
}

$output = `/bin/grep -c $username::$password /home/ace-clipart.com/passwords.list`;
chomp $output;
if ($output) { print "OK"; } else { print "Nope..wrong! Not found"; }

} else { print "no info sent"; }




# an error sub incase something goes wrong.
sub error {

my $error = shift;
print $error;
exit;

}

Thanks anyway, the help was appreciated Smile

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] Code help... In reply to
Hmm you don't need to check the username and pass like that:

if ($password eq "") {
&error("You need to specify a password!");
}
if ($username eq "") {
&error("You need to specify a username!");
}

toooooo long.

!$username or !$password and &error("You need to specify a username and password!");

Last edited by:

RedRum: Nov 10, 2001, 7:55 AM
Quote Reply
Re: [RedRum] Code help... In reply to
Mmm...good point Smile

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] Code help... In reply to
I'm not totally sure about the reality of what Im about to say but using grep as a system command like you have it above may be a bit risky security wise...although I'm just guessing.
Quote Reply
Re: [RedRum] Code help... In reply to
Yeah, your right. The security is not great. If, for example the real information was testing::mypassword , then enterig testing and my would work. However, I'm changing it so that the match has to be like;

$username::$password:word

Rather than

$username::$password

That way, it makes it a bit harder to guess the password.

This script though will only be used by about 10 people anyway. It s not going to be a widely used script. It is just to make my life easier than havingto update some of thier stuff manually Cool

Thanks for pointing out the possible security problems though Wink

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [RedRum] Code help... In reply to
Quote:
!$username or !$password and &error("You need to specify a username and password!");
but then you can't have a username or password of '0' Tongue


Adrian
Quote Reply
Re: [brewt] Code help... In reply to
Yeah but it isn't likely you'd want one of just '0' Tongue