登录名
用户在登录UNIX系统时,先键入登录名,然后键入口令。系统在其口令文件(通常是/etc/passwd文件)中查看登录名。口令文件中的登录项由7个以冒号分隔的字段组成,依次是:登录名、加密口令、数字用户ID (205)、数字组ID (105)、注释字段、起始目录(/home/sar) 以及shell程序( /bin/ksh)。
目前,所有的系统已将加密口令移到另-一个文件中。
口令文件
unix系统口令文件,也有称其为用户数据库的,一般有以下各字段,这些字段包含在<pwd.h>中的passwd结构体中。
#include <sys/types.h>
#include <pwd.h>
struct passwd
{
char *pw_name; /* 用户登录名 */
char *pw_passwd; /* 加密口令 */
__uid_t pw_uid; /* 用户ID */
__gid_t pw_gid; /* 组ID */
char *pw_gecos; /* 详细用户名 */
char *pw_dir; /* 用户目录 */
char *pw_shell; /* Shell程序名 */
};
以下是两个获取口令文件项的函数。在给出用户登录名或数值用户ID后,可以查看相关项。
#include <sys/types.h>
#include <pwd.h>
struct passwd *getpwnam(const char *name);
struct passwd *getpwuid(uid_t uid);
//两个函数返回值,若成功,返回指针,出错,返回NULL
阴影口令
阴影口令还可以叫做加密口令。该文件至少包含用户名和加密口令。
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 */
};
与访问口令文件一样,也有一组函数可用于访问阴影口令文件。
#include <shadow.h>
struct spwd *getspnam(const char *name);
struct spwd *getspent(void);
//成功返回指针,出错返回NULL