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.


In reply to Unix Authentication by coec1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.