xref: /netbsd-src/lib/libc/gen/getpwent.3 (revision d20841bb642898112fe68f0ad3f7b26dddf56f07)
1.\"	$NetBSD: getpwent.3,v 1.23 2003/08/07 16:42:50 agc Exp $
2.\"
3.\" Copyright (c) 1988, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\"     @(#)getpwent.3	8.2 (Berkeley) 12/11/93
31.\"
32.Dd April 25, 1999
33.Dt GETPWENT 3
34.Os
35.Sh NAME
36.Nm getpwent ,
37.Nm getpwnam ,
38.Nm getpwuid ,
39.Nm setpassent ,
40.Nm setpwent ,
41.Nm endpwent
42.Nd password database operations
43.Sh LIBRARY
44.Lb libc
45.Sh SYNOPSIS
46.In pwd.h
47.Ft struct passwd *
48.Fn getpwent void
49.Ft struct passwd *
50.Fn getpwnam "const char *login"
51.Ft struct passwd *
52.Fn getpwuid "uid_t uid"
53.Ft int
54.Fn setpassent "int  stayopen"
55.Ft void
56.Fn setpwent void
57.Ft void
58.Fn endpwent void
59.Sh DESCRIPTION
60These functions
61operate on the password database file
62which is described
63in
64.Xr passwd 5 .
65Each entry in the database is defined by the structure
66.Ar passwd
67found in the include
68file
69.Aq Pa pwd.h :
70.Bd -literal -offset indent
71struct passwd {
72	char	*pw_name;	/* user name */
73	char	*pw_passwd;	/* encrypted password */
74	uid_t	pw_uid;		/* user uid */
75	gid_t	pw_gid;		/* user gid */
76	time_t	pw_change;	/* password change time */
77	char	*pw_class;	/* user access class */
78	char	*pw_gecos;	/* Honeywell login info */
79	char	*pw_dir;	/* home directory */
80	char	*pw_shell;	/* default shell */
81	time_t	pw_expire;	/* account expiration */
82};
83.Ed
84.Pp
85The functions
86.Fn getpwnam
87and
88.Fn getpwuid
89search the password database for the given login name or user uid,
90respectively, always returning the first one encountered.
91.Pp
92The
93.Fn getpwent
94function
95sequentially reads the password database and is intended for programs
96that wish to process the complete list of users.
97.Pp
98The
99.Fn setpassent
100function
101accomplishes two purposes.
102First, it causes
103.Fn getpwent
104to ``rewind'' to the beginning of the database.
105Additionally, if
106.Fa stayopen
107is non-zero, file descriptors are left open, significantly speeding
108up subsequent accesses for all of the functions.
109(This latter functionality is unnecessary for
110.Fn getpwent
111as it doesn't close its file descriptors by default.)
112.Pp
113It is dangerous for long-running programs to keep the file descriptors
114open as the database will become out of date if it is updated while the
115program is running.
116.Pp
117The
118.Fn setpwent
119function
120is equivalent to
121.Fn setpassent
122with an argument of zero.
123.Pp
124The
125.Fn endpwent
126function
127closes any open files.
128.Pp
129These functions have been written to ``shadow'' the password file, e.g.
130allow only certain programs to have access to the encrypted password.
131If the process which calls them has an effective uid of 0, the encrypted
132password will be returned, otherwise, the password field of the returned
133structure will point to the string
134.Ql * .
135.Sh RETURN VALUES
136The functions
137.Fn getpwent ,
138.Fn getpwnam ,
139and
140.Fn getpwuid ,
141return a valid pointer to a passwd structure on success
142and a null pointer if end-of-file is reached or an error occurs.
143The
144.Fn setpassent
145function returns 0 on failure and 1 on success.
146The
147.Fn endpwent
148and
149.Fn setpwent
150functions
151have no return value.
152.Sh FILES
153.Bl -tag -width /etc/master.passwd -compact
154.It Pa /etc/pwd.db
155The insecure password database file
156.It Pa /etc/spwd.db
157The secure password database file
158.It Pa /etc/master.passwd
159The current password file
160.It Pa /etc/passwd
161A Version 7 format password file
162.El
163.Sh SEE ALSO
164.Xr getlogin 2 ,
165.Xr getgrent 3 ,
166.Xr nsswitch.conf 5 ,
167.Xr passwd 5 ,
168.Xr passwd.conf 5 ,
169.Xr pwd_mkdb 8 ,
170.Xr vipw 8
171.Sh STANDARDS
172The
173.Fn getpwnam
174and
175.Fn getpwuid
176functions conform to
177.St -p1003.1-90 .
178.Sh HISTORY
179The
180.Nm getpwent ,
181.Nm getpwnam ,
182.Nm getpwuid ,
183.Nm setpwent ,
184and
185.Nm endpwent
186functions appeared in
187.At v7 .
188The
189.Nm setpassent
190function appeared in
191.Bx 4.3 Reno .
192.Sh BUGS
193The functions
194.Fn getpwent ,
195.Fn getpwnam ,
196and
197.Fn getpwuid ,
198leave their results in an internal static object and return
199a pointer to that object.
200Subsequent calls to any of these functions will modify the same object.
201.Pp
202The functions
203.Fn getpwent ,
204.Fn endpwent ,
205.Fn setpassent ,
206and
207.Fn setpwent
208are fairly useless in a networked environment and should be
209avoided, if possible.
210.Fn getpwent
211makes no attempt to suppress duplicate information if multiple
212sources are specified in
213.Xr nsswitch.conf 5
214.Sh COMPATIBILITY
215The historic function
216.Fn setpwfile
217which allowed the specification of alternative password databases,
218has been deprecated and is no longer available.
219