我想知道用户名在表单中提供的密码是否是用户在给定主机的系统上拥有的密码...我需要这个才能更改密码或返回错误消息给请求它的用户。特别希望它在 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";
}
最佳答案
标准的 Linux 身份验证接口(interface)是 PAM,例如Authen::PAM或 Authen::Simple::PAM .例如:
use Authen::Simple::PAM;
my $pam = Authen::Simple::PAM->new(service => "login");
if ($pam->authenticate($username, $password)) {
# ...
}
仅对于本地帐户,密码哈希通常存储在“影子”文件中。 Perl 会在调用 getpwent()
时自动检索它们:
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()
函数将“盐”作为第二个参数。当与散列进行比较时,您应该给它完整的原始散列,它会从中提取盐.)
请注意,您必须拥有 root 访问权限才能使用这两种工具。
关于linux - 将 Linux 用户的当前密码与在 Perl 中输入的密码进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24311005/