Nov
10

Authenticate Users

By

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 user\n" 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 "ok\n";
}
Categories : Linux, Notes, Perl

Leave a Comment

You must be logged in to post a comment.