CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
My small script is not working. Can you help me ?

 



marco
Novice

Jan 25, 2003, 10:40 AM

Post #1 of 15 (2571 views)
My small script is not working. Can you help me ? Can't Post

Hello,

Here is what I need:

When I click on this link:

http://www.mydomain.com/cgi-bin/noswemanagem.pl?anny74@hotmail.com

I want this ( only this one) username ( Wich is the same as the email adress) and password to be delete from my password file.

Here is how the passcodes, wich I want to delete, looks like in the .htpasswd file:

anny74@hotmail.com:yaGO2D9I2Zw2U



Here is the script I'm using but it doesn't work, I get this message: User not found.
But I'm sure the user is in my password file.



#!/usr/bin/perl


$passfile="/usr/home/marco/www.mydomain.com/cgi-bin/.htpasswd";

$flock="y";



#________________________________________________________________




read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
if ($INPUT{$name}) { $INPUT{$name} = $INPUT{$name}.",".$value; }
else { $INPUT{$name} = $value; }
}

unless ($INPUT{'username'}) {
print "Content-type: text/html \n\n";

}

$temp=0;
$temp=$ENV{'QUERY_STRING'};
if ($temp) {
$INPUT{'username'} = $temp;
&remove;
}


exit;

#______________________________________________________________________



sub remove {

open (data, "<$passfile") or &error("Unable to open the password file");
if ($flock eq "y") {
flock data, 2;
}
@data=<data>;
close(data);
$count=0;
foreach $data(@data) {
$count++;
@datax=split(/:/, $data);
if ($input{'username'} eq "@datax[0]" and $input{'username'}== "@datax[0]") {
$count--;
splice (@data, $count, 1);
open (wdata, ">$passfile") or &error("Unable to write to the data file");
if ($flock eq "y") {
flock wdata, 2;
}
print wdata @data;
close(wdata);

print "User \"$input{'username'}\" is deleted form the data base.\n";
exit;
}
}
print "User not found\n";
exit;
}


#####error
sub error {
print "An error has occured. <br> The error is $_[0]<br>\n";
print "$!\n";
exit;
}



Can someone tell me what is wrong ?



Thank you,

Marco


thodi
stranger

Jan 25, 2003, 11:46 AM

Post #2 of 15 (2560 views)
Re: [marco] My small script is not working. Can you help me ? [In reply to] Can't Post

Don't use your own parameter parsing routine. Use the one that comes with the CGI module. Please use both strict and warnings, let perl help you find problems. And please use this forum's code-tag to make your code appear more beautiful.

Another ugly thing about your script is that you are comparing an array slice (@array[0]) rather than one element ($array[0]).

One quick hack to solve your problem:

Code
use strict; 
use warnings;
use CGI;

my $passfile = "bla";
my $cgi = CGI->new();
my $username = $cgi->param('username');
my @newfile = ();

# open and lock the passwordfile ...

while (<PASSFILE>) {
next if /^$username:/; # ignore this line, thus "deleting" it
push @newfile, $_;
}

# close passwordfile, then open it for reading and lock

print PASSFILE @newfile;

# close passwordfile



Paul
Enthusiast

Jan 25, 2003, 1:11 PM

Post #3 of 15 (2556 views)
Re: [thodi] My small script is not working. Can you help me ? [In reply to] Can't Post

Depending on the size of the pass file you may want to write to a new file inside the while loop instead of pushing into an array and then unlinking the old file and renaming the new one.


marco
Novice

Jan 25, 2003, 1:19 PM

Post #4 of 15 (2555 views)
Re: [thodi] My small script is not working. Can you help me ? [In reply to] Can't Post

Hello,

Thank you for your reply. Smile

There must be something I'm doing wrong because I tried your script and it doesn't work. Unsure I'm very new to PERL....


thodi
stranger

Jan 25, 2003, 1:24 PM

Post #5 of 15 (2553 views)
Re: [marco] My small script is not working. Can you help me ? [In reply to] Can't Post

The first thing you're doing wrong is that you're not telling us the error messages or "wrong" effects when running my program. Also, you did fill the parts where I only wrote comments about what to do, right?


marco
Novice

Jan 25, 2003, 4:00 PM

Post #6 of 15 (2549 views)
Re: [thodi] My small script is not working. Can you help me ? [In reply to] Can't Post

Ok. The message I get is: internal server error:



Here is the script I tried following what I understood of your advices.
( I don't speak english much + I don't know PERL much )



#!/usr/bin/perl




use strict;
use warnings;
use CGI;




my $passfile="/usr/home/marco/www.domain.com/cgi-bin/.htpasswd";
my $cgi = CGI->new();
my $username = $cgi->param('username');
my @newfile = ();



# open and lock the passwordfile ...

open (PASSFILE, "<$passfile") {&lock} or die ("Unable to open the password file");


while (<PASSFILE>) { next if /^$username:/; # ignore this line, thus "deleting" it

push @newfile, $_; }


# close passwordfile, then open it for reading and lock

close (PASSFILE);

open (PASSFILE, "$passfile") {&lock} or die ("Unable to open the password file2");

print PASSFILE @newfile;

# close passwordfile

close (PASSFILE);
&userdel;


###################################

sub userdel {
print "Content-type: text/html\n\n";
print "User deleted !<BR>\n";
exit;
}

########## FILE LOCKING SUB ##########
sub lock {

my $file = $_[0];
my $etime = time + 5;

if (-e "$file.lock") {
open (LOCK,"$file.lock");
my $temp = <LOCK>;
close (LOCK);
chomp ($temp);

if ($temp < (time - 10)) {
unlink ("$file.lock");
}
}

while (-e "$file.lock" && time < $etime) {
sleep(1);
}

if (-e "$file.lock") {
print "file lock still here $file.lock";
exit;
}
else {
open (LOCKFILE, ">$file.lock");
print LOCKFILE time;
close (LOCKFILE);
}
}

############
sub unlock {
my $file = shift;
unlink ("$file.lock");
}

1;

#################


Paul
Enthusiast

Jan 25, 2003, 5:31 PM

Post #7 of 15 (2546 views)
Re: [marco] My small script is not working. Can you help me ? [In reply to] Can't Post


Quote
The message I get is: internal server error:


Your error log will contain better details :)

If you don't know where your log is add:

use CGI::Carp qw(fatalsToBrowser);

...near the very top of your script and it will show errors to the browser.


(This post was edited by Paul on Jan 25, 2003, 5:31 PM)


thodi
stranger

Jan 26, 2003, 1:55 AM

Post #8 of 15 (2542 views)
Re: [marco] My small script is not working. Can you help me ? [In reply to] Can't Post


In Reply To
open (PASSFILE, "<$passfile") {&lock} or die ("Unable to open the password file");

There is a syntax error. You can check your script for syntactical correctness by running "perl -c scriptname".

lock() can also be the name of a builtin Perl function, so it would be better to think of another name for that sub.

And please use the code (or the perl tag, which I only just saw now :-)) tag for posting code: http://perlguru.com/gforum.cgi?do=markup_help;


(This post was edited by thodi on Jan 26, 2003, 1:58 AM)


marco
Novice

Jan 26, 2003, 7:33 AM

Post #9 of 15 (2536 views)
Re: [thodi] My small script is not working. Can you help me ? [In reply to] Can't Post

Thanks to both of you, I finally got rid of my Error messages.

But I still have a problem:

My script SEEM to work well, when I type this:

http://www.domain.com/cgi-bin/noswemanagem.pl?nancy74@hotmail.com



I get this message: User deleted !



But when I look at my .htpasswd file, the user is NOT deleted...



Here is the code I'm using now:

[perl]

#!/usr/bin/perl


use CGI::Carp qw(fatalsToBrowser);

use strict;
use warnings;
use CGI;




my $passfile="/usr/home/marco/www.doamin.com/cgi-bin/.htpasswd";
my $cgi = CGI->new();
my $username = $cgi->param('username');
my @newfile = ();



# open and lock the passwordfile ...

open (PASSFILE, "<$passfile") or &noopen;
flock(PASSFILE,2);


while (<PASSFILE>) { next if /^$username:/; # ignore this line, thus "deleting" it

push @newfile, $_; }


# close passwordfile, then open it for reading and lock

close (PASSFILE);

open (PASSFILE, "$passfile") or &noopen;
flock(PASSFILE,2);

print PASSFILE @newfile;

# close passwordfile

close (PASSFILE);
&userdel;


##############

sub userdel {
print "Content-type: text/html\n\n";
print "User deleted !<BR>\n";
exit;
}

##############
sub noopen {
print "Content-type: text/html\n\n";
print "can't open file<BR>\n";
exit;
}

[/perl]


thodi
stranger

Jan 26, 2003, 7:52 AM

Post #10 of 15 (2535 views)
Re: [marco] My small script is not working. Can you help me ? [In reply to] Can't Post


In Reply To
I get this message: User deleted !
But when I look at my .htpasswd file, the user is NOT deleted...

Code
   
open (PASSFILE, "$passfile") or &noopen;
flock(PASSFILE,2);
print PASSFILE @newfile;


Your are only opening the file for reading here, so the print will not work.

Code
open PASSFILE, ">$passfile" or noopen();

If you print out the $! variable in your noopen() sub, you will also know why the file could not be opened, by the way.


marco
Novice

Jan 26, 2003, 9:43 AM

Post #11 of 15 (2533 views)
Re: [thodi] My small script is not working. Can you help me ? [In reply to] Can't Post

I have changed the line:

open (PASSFILE, "$passfile") or &noopen;

to

open PASSFILE, ">$passfile" or noopen();


And it still doesn't delete the passcodes in my .htpasswd file.


vall33
New User

Jan 26, 2003, 8:12 PM

Post #12 of 15 (2520 views)
Re: [marco] My small script is not working. Can you help me ? [In reply to] Can't Post

A permissions issue perhaps?


thodi
stranger

Jan 27, 2003, 1:12 AM

Post #13 of 15 (2514 views)
Re: [marco] My small script is not working. Can you help me ? [In reply to] Can't Post

Try some debug output in all he important places. Is the line with the email address really found?


davorg
Thaumaturge / Moderator

Jan 27, 2003, 1:49 AM

Post #14 of 15 (2515 views)
Re: [marco] My small script is not working. Can you help me ? [In reply to] Can't Post

Compare what you are passing to your program:

In Reply To
http://www.domain.com/cgi-bin/noswemanagem.pl?nancy74@hotmail.com

To what your program is expecting as input

In Reply To

Code
my $username = $cgi->param('username');


Your program is expecting a parameter called "username", but you're not passing it a parameter with that name (or, indeed, any named paramters at all).

Try calling it with:

http://www.domain.com/cgi-bin/noswemanagem.pl?username=nancy74@hotmail.com

and see what happens.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


marco
Novice

Jan 27, 2003, 5:46 AM

Post #15 of 15 (2510 views)
Re: [davorg] My small script is not working. Can you help me ? [In reply to] Can't Post

Exactly Dave !

That was the problem. Wink

Thank you very much !



Thanks to everybody. All your tips helped me a lot. Smile

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives