Solved

Apache::htpasswd

Posted on 2000-04-05
11
340 Views
Last Modified: 2013-12-25
Hi,
 
 Has anyone used the apache::Htpasswd module?
 
 I'm having trouble getting useful error messages.  Here is what the man page says about errors:
 
           # If something fails, check error
           $foo->error;
 
       error;
           If a method returns an error, or a method fails, the error can be retrived by calling error()
 
 I've tried this in various forms, and all I get back is something like this:
 Apache::Htpasswd=HASH(0x80c724c)
 
 When you set a new htpasswd the module is supposed to return 1 or undef, I get a string like
 this: Apache::Htpasswd=HASH(0x818a084)
 
 Those really don't help me put any kind of error testing or
 troubleshooting into my script.  Any help would be appreciated.
 
 Here is a link to the module if anyone would like to try and help me out:
 
 http://www.cpan.org/modules/by-module/Apache/Apache-Htpassword-1.2.readm
 e
 http://www.cpan.org/modules/by-module/Apache/Apache-Htpassword-1.2.tar.g
 z
 
My question:
1.  Is this the best solution, or is there a better way?
2.  Can you give a working example so I can see how it works?
 
 Thanks in advance,
 
 --John
 
0
Comment
Question by:jeacopgh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
11 Comments
 

Author Comment

by:jeacopgh
ID: 2687525
Adjusted points from 30 to 50
0
 

Author Comment

by:jeacopgh
ID: 2687990
Forgot to mention I'm using RedHat Linux 6.1 and Apache/1.3.9.

Thanks.

--John
0
 
LVL 1

Expert Comment

by:mafweb
ID: 2689439
Hi,

to me (not really knowing this module), it looks like $foo->error returns a hash, not a single variable.
You can check this by using:

$err=$foo->error;
foreach $key ($err) {
  print $key.":".$err{$key}.\n;
}

hope this works (haven't used perl for a while ;-()

maf
0
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 

Author Comment

by:jeacopgh
ID: 2689782
Hi,

Ok, I tried what maf said, this is the error I got:

Software error:

Can't call method "error" on an undefined value at
/home/httpd/cgi-bin/pwdprot/test2.cgi line 56.


This is line 56:
               $err=$foo->error();


Do I need to add anything else to the program?

Thanks,


--John

0
 
LVL 1

Expert Comment

by:mafweb
ID: 2689838
I'm not sure, but from the docs, it looks like you should use

            $err=$foo->error;

maybe this helps,

maf
0
 

Author Comment

by:jeacopgh
ID: 2689863
Hey,

Yeah, I tried that, too, got the same error:

Software error:

Can't call method "error" on an undefined value at
/home/httpd/cgi-bin/pwdprot/test2.cgi line 56.

Line 56:
                $err=$foo->error;



Thanks,

--John
0
 
LVL 1

Expert Comment

by:mafweb
ID: 2690102
could you please send me the entire program???

maf
0
 

Author Comment

by:jeacopgh
ID: 2690158
Ok, a couple of notes first:

1.  I know it's nasty, I'm not the greatest programmer.

2.  The goal is to create htaccess and htpasswd files the input form on the web.

Thanks maf!

#!/usr/bin/perl
use CGI;
CGI::ReadParse(*in);
use CGI::Carp qw(fatalsToBrowser);
sub BEGIN {
        unshift (@INC, "Htpasswd.pm");
                }
use Apache::Htpasswd;

# get input
$protectdir = $in{'protectdir'};
$authname = $in{'inputtext'};
$username = $in{'username'};
$pwdir = $in{'pwdir'};
$password = $in{'password'};
$password2 = $in{'password2'};

$protectfile = $pwdir . "\.htpasswd";

# make sure password was typed correctly both times
if ($password ne $password2) {
        print "Content-type: text/html\n\n";
        print "<html><body>";
        print "Error: Invalid New Password.";
        print "</body></html>";
        print "\n";
        die "Invalid New password";
   }  
   
open (OUTPUT, ">$protectdir/.htaccess") || die "can't open $protectdir/.htaccess in write mode";
<!-- Out to htaccess file -->
close (OUTPUT);


# touch the htpasswd so it exists
        system("touch $protectfile");

        $newpw = new Apache::Htpasswd("$protectfile");
        $newpw->htpasswd("$username", "$password");

                $err=$foo->error;

if ($err eq 1) {
        print "Content-type: text/html\n\n";
        print "<html><body>";
        print "Success\n<br>";
                foreach $key ($err) {
                print $key.":".$err{$key}.\n;
                }
        print "\n<br>";
        print $newpw;
        print "\n</body></html>\n";
} else {
        print "Content-type: text/html\n\n";
        print "<html><body>";
        print "Error\n<br>";
                foreach $key ($err) {
                print $key.":".$err{$key}.\n;
                }
        print "\n<br>";
        print $newpw;
        print "\n</body></html>\n";
}
0
 
LVL 1

Accepted Solution

by:
mafweb earned 75 total points
ID: 2690268
oops, I think there was kind of a misunderstanding here...
$foo was meant to be replaced by the name of your Htpasswd object.
So the line should read:

        $err=$newpw->error();

or maybe

        $err=$newpw->error;

(as mentioned, I'm not really sure about the () here, but I think the first is right)
What you do here is create a new object
(newpw=new Apache::Htpasswd(..)), tell it to add a new entry
(next line) and finally "ask" it for any errors that might have occured.

hope this helps,

maf
0
 

Author Comment

by:jeacopgh
ID: 2690495
Adjusted points from 50 to 75
0
 

Author Comment

by:jeacopgh
ID: 2690496
maf,

Thanks, that did it.  About 20 more minutes on my part cleaning up the script and fixing the error checking and it's done!

Thanks for the help, for some reason I just couldn't figure that one out.

--John
0

Featured Post

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question