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";
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.