coec1 has asked for the wisdom of the Perl Monks concerning the following question:
Greetings most venerable monks
I am building a basic CMS in Mason. Currently, my users (under 20) all have Unix accounts and I'd prefer to keep it that way. When my users log in I'd like to run the following script from outside of Mason (suidperl?) to verify the username/password pair.
The script itself works fine from the shell, I haven't put the script into Mason as yet because I want to know if is a bad way of going about this. Essentially, the Mason site will set an environment variable containing the password and then call the script as '/path/to/script/chk_auth username', finally the Mason code will unset the environment variable.
I have looked at a couple of Perl modules but they to not support shadow passwords. I also need to extend the script to work under both Linux and AIX, but thats a lot down the track...
#!/usr/bin/perl -wT use strict; use Crypt::PasswdMD5; use Data::Dumper; use constant TRUE => '1'; use constant FALSE => '0'; my $username = shift; my $password = shift; my $found = FALSE; my ($salt, $MD5passwd); my ($name, $passwd, $uid, $gid); # Obviously we need a username/password pair if ((!defined $username || $username eq '') || (!defined $password || +$password eq '')) { # warn "Must supply a username and password pair\n" ; exit 3; } # Never guess root's password exit 1 if ($username == "root"); while (($name, $passwd, $uid, $gid) = getpwent()) { next unless ($name eq $username); # Only check real user accounts (includes root but hey, we sho +uld be paranoid) exit 1 if ($uid < 500); # We have found the user in /etc/passwd $found = TRUE; # Pass the salt please $salt = substr($passwd, 3, 8); # generate an encrypted string to test against the encrypted p +assword $MD5passwd = unix_md5_crypt($password, $salt); if ($MD5passwd eq $passwd) { exit 0; # Correct } else { exit 1; # Wrong } #last; } exit 2; # User not found __END__
Many thanks
CC
PS: the chk_auth script has permissions of 04500.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Unix Authentication
by kschwab (Vicar) on Jan 11, 2004 at 02:59 UTC |