#include <stdio.h>
#include <pwd.h>
#include <unistd.h>
#include <sys/types.h>
int main ()
{
struct passwd* tmp = NULL;
tmp = getpwuid(geteuid());
printf("username:%s\n",tmp->pw_name);
printf("user password:%s\n",tmp->pw_passwd);
printf("user ID:%d\n",tmp->pw_uid);
printf("group ID:%d\n",tmp->pw_gid);
printf("real name:%s\n",tmp->pw_gecos);
printf("home directory:%s\n",tmp->pw_dir);
printf("shell program:%s\n",tmp->pw_shell);
}
gcc -o password password.c
./password
username:root
user password:x
user ID:0
group ID:0
real name:
home directory:/root
shell program:/bin/bash
getpwuid () 返回user ID 对应的passwd结构体指针
#include <sys/types.h>
#include <pwd.h>
struct passwd *getpwuid(uid_t uid);
geteuid 返回有效的user ID
#include <unistd.h>
#include <sys/types.h>
uid_t geteuid(void);
passwd 结构体定义在<pwd.h>中,结构如下:
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; /* real name */
char *pw_dir; /* home directory */
char *pw_shell; /* shell program */
};
读取 /etc/passwd 的 API 为
struct passwd *getpwnam(const char *name); # 通过用户名
struct passwd *getpwuid(uid_t uid); # 通过 uid
struct passwd
{
char *pw_name; //用户名
char *pw_passwd; //用户密码
uid_t pw_uid; //用户id
uid_t pw_gid; //用户组id
char *pw_gecos; //用户描述
char *pw_dir; //用户家目录
char *pw_shell; //用户登录shell
};
读取 /etc/shadow 的 API 为
struct spwd *getspnam(const char *name); # 通过用户名
struct spwd {
char *sp_namp; /* 登录名 */
char *sp_pwdp; /* 加密口令 */
long sp_lstchg; /* 最后一次修改时间 */
long sp_min; /* 最小时间间隔 */
long sp_max; /* 最大时间间隔 */
long sp_warn; /* 警告时间 */
long sp_inact; /* 不活动时间 */
long sp_expire; /* 失效时间 */
unsigned long sp_flag; /* 保留 */
};
标签:tmp,sp,uid,passwd,linux,char,pw,结构 From: https://blog.51cto.com/lenglingx/6390784