Linux环境下利用getpwent()读取口令文件回顾

Linux系统口令文件定义在头文件中,在结构体struct passwd中定义。

struct passwd应包含以下成员:

char    *pw_name   User's login name.
uid_t    pw_uid    Numerical user ID.
gid_t    pw_gid    Numerical group ID.
char    *pw_dir    Initial working directory.
char    *pw_shell  Program to use as shell.

读口令文件应该用到的函数:

// The endpwent() function is used to close the password database after all processing has been performed.
void           endpwent(void);

// The  getpwent()  function  returns  a pointer to a structure containing the broken-out fields of a record
// from the password database (e.g., the local password file /etc/passwd, NIS, and LDAP).   The  first  time
// getpwent() is called, it returns the first entry; thereafter, it returns successive entries.
struct passwd *getpwent(void);

// The setpwent() function rewinds to the beginning of the password database.
void           setpwent(void);

示例代码:

#include <pwd.h>
#include <stdio.h>

int main(void)
{
	struct passwd *pwd;

	setpwent();

	while((pwd = getpwent()) != NULL)
	{
		printf("%s\t%d\t%d\t%s\t%s\n", pwd->pw_name, pwd->pw_uid,
				pwd->pw_gid, pwd->pw_dir, pwd->pw_shell);
	}

	endpwent();

	return 0;
}

运行结果:

[ycxie@fedora Workspace]$ gcc printpwent.c -o printpwent -Wall
[ycxie@fedora Workspace]$ ./printpwent
gluster	996	991	/run/gluster	/sbin/nologin
tcpdump	72	72	/	/sbin/nologin
nm-openconnect	988	982	/	/sbin/nologin
avahi	70	70	/var/run/avahi-daemon	/sbin/nologin
sshd	74	74	/var/empty/sshd	/sbin/nologin
rpcuser	29	29	/var/lib/nfs	/sbin/nologin
openvpn	994	989	/etc/openvpn	/sbin/nologin
bin	1	1	/bin	/sbin/nologin
games	12	100	/usr/games	/sbin/nologin
systemd-network	192	192	/	/sbin/nologin
gdm	42	42	/var/lib/gdm	/sbin/nologin
ycxie	1000	1000	/home/ycxie	/bin/bash
pipewire	993	987	/var/run/pipewire	/sbin/nologin
setroubleshoot	987	980	/var/lib/setroubleshoot	/sbin/nologin
usbmuxd	113	113	/	/sbin/nologin
daemon	2	2	/sbin	/sbin/nologin
dnsmasq	978	978	/var/lib/dnsmasq	/sbin/nologin
ftp	14	50	/var/ftp	/sbin/nologin
systemd-resolve	193	193	/	/sbin/nologin
adm	3	4	/var/adm	/sbin/nologin
saslauth	990	76	/run/saslauthd	/sbin/nologin
qemu	107	107	/	/sbin/nologin
tss	59	59	/dev/null	/sbin/nologin
gnome-initial-setup	986	979	/run/gnome-initial-setup/	/sbin/nologin
nobody	99	99	/	/sbin/nologin
geoclue	992	986	/var/lib/geoclue	/sbin/nologin
mail	8	12	/var/spool/mail	/sbin/nologin
rpc	32	32	/var/lib/rpcbind	/sbin/nologin
polkitd	997	996	/	/sbin/nologin
colord	991	984	/var/lib/colord	/sbin/nologin
pulse	171	171	/var/run/pulse	/sbin/nologin
systemd-coredump	999	998	/	/sbin/nologin
rtkit	172	172	/proc	/sbin/nologin
apache	48	48	/usr/share/httpd	/sbin/nologin
lp	4	7	/var/spool/lpd	/sbin/nologin
chrony	995	990	/var/lib/chrony	/sbin/nologin
dbus	81	81	/	/sbin/nologin
radvd	75	75	/	/sbin/nologin
systemd-timesync	998	997	/	/sbin/nologin
nm-openvpn	989	983	/	/sbin/nologin
abrt	173	173	/etc/abrt	/sbin/nologin
root	0	0	/root	/bin/bash
bin	1	1	/bin	/sbin/nologin
daemon	2	2	/sbin	/sbin/nologin
adm	3	4	/var/adm	/sbin/nologin
lp	4	7	/var/spool/lpd	/sbin/nologin
sync	5	0	/sbin	/bin/sync
shutdown	6	0	/sbin	/sbin/shutdown
halt	7	0	/sbin	/sbin/halt
mail	8	12	/var/spool/mail	/sbin/nologin
operator	11	0	/root	/sbin/nologin
games	12	100	/usr/games	/sbin/nologin
ftp	14	50	/var/ftp	/sbin/nologin
nobody	99	99	/	/sbin/nologin
apache	48	48	/usr/share/httpd	/sbin/nologin
systemd-coredump	999	998	/	/sbin/nologin
systemd-timesync	998	997	/	/sbin/nologin
systemd-network	192	192	/	/sbin/nologin
systemd-resolve	193	193	/	/sbin/nologin
dbus	81	81	/	/sbin/nologin
polkitd	997	996	/	/sbin/nologin
qemu	107	107	/	/sbin/nologin
rtkit	172	172	/proc	/sbin/nologin
pulse	171	171	/var/run/pulse	/sbin/nologin
gluster	996	991	/run/gluster	/sbin/nologin
avahi	70	70	/var/run/avahi-daemon	/sbin/nologin
chrony	995	990	/var/lib/chrony	/sbin/nologin
usbmuxd	113	113	/	/sbin/nologin
openvpn	994	989	/etc/openvpn	/sbin/nologin
radvd	75	75	/	/sbin/nologin
pipewire	993	987	/var/run/pipewire	/sbin/nologin
geoclue	992	986	/var/lib/geoclue	/sbin/nologin
rpc	32	32	/var/lib/rpcbind	/sbin/nologin
colord	991	984	/var/lib/colord	/sbin/nologin
saslauth	990	76	/run/saslauthd	/sbin/nologin
nm-openvpn	989	983	/	/sbin/nologin
nm-openconnect	988	982	/	/sbin/nologin
gdm	42	42	/var/lib/gdm	/sbin/nologin
abrt	173	173	/etc/abrt	/sbin/nologin
setroubleshoot	987	980	/var/lib/setroubleshoot	/sbin/nologin
rpcuser	29	29	/var/lib/nfs	/sbin/nologin
gnome-initial-setup	986	979	/run/gnome-initial-setup/	/sbin/nologin
sshd	74	74	/var/empty/sshd	/sbin/nologin
tss	59	59	/dev/null	/sbin/nologin
tcpdump	72	72	/	/sbin/nologin
ycxie	1000	1000	/home/ycxie	/bin/bash
dnsmasq	978	978	/var/lib/dnsmasq	/sbin/nologin

Leave a Reply

Your email address will not be published. Required fields are marked *