将Linux用户的当前密码与Perl中输入的密码进行比较

时间:2014-06-11 07:02:12

标签: linux unix encryption passwords perl

我想知道用户名在表单中提供的密码是否是用户在给定主机系统中拥有的密码...我需要这个以更改密码或返回错误消息给要求它的用户。特别希望它能在Perl中完成。

我有以下测试,看看用户是否存在,如果成功,则密码匹配,如果成功,则更改为我使用JSON Rpc Dispatcher为客户端提供服务的请求所提供的新密码。

if (getpwnam("$username")) { 
#  if ($currentpass == $sytempass)  {}              
return "User $username password is $newpass success!";
#   else { return current password is incorrect}       
} 
else {
return "user $username never existed here";
} 

1 个答案:

答案 0 :(得分:4)

标准的Linux身份验证接口是PAM,例如Authen::PAMAuthen::Simple::PAM。例如:

use Authen::Simple::PAM;

my $pam = Authen::Simple::PAM->new(service => "login");

if ($pam->authenticate($username, $password)) {
    # ...
}

仅对于本地帐户,密码哈希值通常存储在" shadow"文件。调用getpwent()时,Perl会自动检索它们:

my @pwent = getpwnam($username);
my $known_good_hash = $pwent[1];
my $checking_hash = crypt($password, $known_good_hash);

if ($checking_hash eq $known_good_hash) {
    # ...
}

crypt()函数将" salt"作为第二个参数。当与哈希进行比较时,您应该给它完整的原始哈希值,并且它将提取盐从它。)

请注意,您必须具有root访问权限才能使用这两种功能。