我有一个用 C 编写的程序,运行在嵌入式 Linux 上,有时它想检查系统用户的密码。
- 如果我能得到地穴盐/etc/密码,我可以使用 crypt() 检查用户密码的正确性。
- 有没有shell脚本可以帮助我检查密码?比如check_passwd用户名密码,那么它返回的值是正确还是不正确?
谢谢!
我最近一直在解决同样的任务。这是 C 函数的示例(与 -lcrypt 链接)。请注意,您需要具有文件 /etc/passwd 和 /etc/shadow 的读取权限。
#include <sys/types.h>
#include <pwd.h>
#include <shadow.h>
#include <crypt.h>
#include <string.h>
#include <stdio.h>
/// @return 0 - password is correct, otherwise no
int CheckPassword( const char* user, const char* password )
{
struct passwd* passwdEntry = getpwnam( user );
if ( !passwdEntry )
{
printf( "User '%s' doesn't exist\n", user );
return 1;
}
if ( 0 != strcmp( passwdEntry->pw_passwd, "x" ) )
{
return strcmp( passwdEntry->pw_passwd, crypt( password, passwdEntry->pw_passwd ) );
}
else
{
// password is in shadow file
struct spwd* shadowEntry = getspnam( user );
if ( !shadowEntry )
{
printf( "Failed to read shadow entry for user '%s'\n", user );
return 1;
}
return strcmp( shadowEntry->sp_pwdp, crypt( password, shadowEntry->sp_pwdp ) );
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)