/etc/passwd
NAME
getpwnam, getpwnam_r, getpwuid, getpwuid_r - get password file entry
SYNOPSIS
#include <sys/types.h>
#include <pwd.h>
struct passwd *getpwnam(const char *name);
//传入一个文件名,获得一个struct passwd * 指针
struct passwd *getpwuid(uid_t uid);
struct passwd {
char *pw_name; /* username */
char *pw_passwd; /* user password */
uid_t pw_uid; /* user ID */
gid_t pw_gid; /* group ID */
char *pw_gecos; /* user information */
char *pw_dir; /* home directory */
char *pw_shell; /* shell program */
};
getpwuid()的实现
//getpwuid 实现
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<pwd.h>
int main(int argc,char ** argv)
{
struct passwd *pwdline;
if(argc < 2)
{
fprintf(stderr,"Usage...\n");
exit(1);
}
pwdline = getpwuid(atoi(argv[1]));
puts(pwdline->pw_name);
exit(0);
}
/etc/group
getgrgid() getgrnam()
NAME
getgrnam, getgrnam_r, getgrgid, getgrgid_r - get group file entry
SYNOPSIS
#include <sys/types.h>
#include <grp.h>
struct group *getgrnam(const char *name);
struct group *getgrgid(gid_t gid);
struct group {
char *gr_name; /* group name */
char *gr_passwd; /* group password */
gid_t gr_gid; /* group ID */
char **gr_mem; /* NULL-terminated array of pointers to names of group members */
};
/etc/shadow
由于/etc/passwd文件是所有用户都可读的,如果用户的密码太简单或规律比较明显的话,一台普通的计算机就能够很容易地将它破解,因此对安全性要求较高的Linux系统都把加密后的口令字分离出来,单独存放在一个文件中,这个文件是/etc/shadow文件。 有超级用户才拥有该文件读权限,这就保证了用户密码的安全性。
详细信息见此链接
getspnam() 、 crypt() //用来加密
NAME
getspnam, getspnam_r, getspent, getspent_r, setspent, endspent, fgetspent, fgetspent_r, sgetspent,
sgetspent_r, putspent, lckpwdf, ulckpwdf - get shadow password file entry
SYNOPSIS
/* General shadow password file API */
#include <shadow.h>
struct spwd *getspnam(const char *name);
struct spwd *getspent(void);
struct spwd {
char *sp_namp; /* Login name */
char *sp_pwdp; /* Encrypted password */
long sp_lstchg; /* Date of last change(measured in days since 1970-01-01 00:00:00 +0000 (UTC)) */
long sp_min; /* Min # of days between changes */
long sp_max; /* Max # of days between changes */
long sp_warn; /* # of days before password expires to warn user to change it */
long sp_inact; /* # of days after password expires until account is disabled */
long sp_expire; /* Date when account expires(measured in days since 1970-01-01 00:00:00 +0000 (UTC)) */
unsigned long sp_flag; /* Reserved */
};
NAME
crypt, crypt_r - password and data encryption
SYNOPSIS
#define _XOPEN_SOURCE /* See feature_test_macros(7) */ 宏定义,所以还需要往makefile 中加入一个宏 -D__XOPEN_SOURCE
#include <unistd.h>
char *crypt(const char *key, const char *salt);
//第一个参数:原串 第二个参数:掺杂的串
//通过指定方式返回char * 指针
//一个简单的确认用户名的程序
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<shadow.h>
#include<string.h>
int main(int argc,char** argv)
{
char *input_pass;
struct spwd *shdowline;
char *crypted_pass;
if(argc < 2)
{
fprintf(stderr,"Usage...\n");
exit(1);
} //getpass - get a password
input_pass = getpass("PassWord:"); //获得原文 char *getpass(const char *prompt);
shdowline = getspnam(argv[1]); //获得格式 struct spwd *getspnam(const char *name);
//char *crypt(const char *key, const char *salt);
crypted_pass = crypt(input_pass,shdowline->sp_pwdp); //进行加密
//验证口令是否一样
if(strcmp(shdowline->sp_pwdp,crypted_pass)==0)
{
puts("OK");
}
else{
puts("wrong.");
}
exit(0);
}