Browsing TagPerl

Authenticate Users

This snippet could authenticate users using their /etc/passwd or /etc/shadow entry. May have to run this with higher than normal privilege:


#!/usr/bin/env perl

print "Username: ";
chomp($uname = <stdin>);

$pwd = (getpwnam($uname))[1]; # get the user's pwd
die "invalid usern" unless defined $pwd and length $pwd;
$salt = substr($pwd, 0, 2);

system "stty -echo";
print "Password: ";
chomp($word = <stdin>);
print "n";
system "stty echo";

if (crypt($word, $salt) ne $pwd) {
    die "Sorry...n";
} else {
    print "okn";
}

Quick Command Line Arg parsing

To parse command line arguments passed to a script without using a module, parse @ARGV with the following:


foreach my $arg (@ARGV) {
    $a = 1, next if $arg eq '-a';
    $b = 1, next if $arg eq '-b';
    $c = 1, next if $arg eq '-c';
}

Note: This is good only for boolean (on/off) command line switches.

Debugging CGI Using ptkdb

ptkdb is a graphical Perl debugger. To use it when debugging command line scripts is very straight forward. Just type in the command line:


perl -d:ptkdb script.pl

and you’re good to go.

However, using it to debug CGIs needs some tweaking to your CGI source. Replace the usual

#!/usr/bin/perl

line at the top of the CGI script with this one:


#!/usr/bin/perl -d:ptkdb
BEGIN {$ENV{DISPLAY} = "$ENV{REMOTE_ADDR}:0.0";}

Go to your browser and invoke your CGI and a debugger window should pop up.

CGI To Display Module Documentation

Here’s a CGI to display a module’s POD. The module has to be in the $INC. This is great for providing documentation to internal modules.


# save this as showdoc.cgi
#!/usr/local/bin/perl
use strict;

use Pod::Html;
use CGI;
use CGI::Carp qw(fatalsToBrowser);

# Send out the header
print "Content-type: text/html", "nn";

my $q = new CGI;

my $module = $q->param('module');
require $module;

$| = 1;

chdir ("/tmp");

my $fullpath = $INC{$module}
    or die "$module not found";

pod2html("--infile=$fullpath", "--flush");

# Clean up the junk left by pod2html
END {
        unlink("pod2html-dircache");
        unlink("pod2html-itemcache");
}

To use this, say you want to view POD for Data::Dumper:


http://localhost/cgi-bin/showdoc.cgi?module=Data/Dumper.pm