xref: /freebsd/lib/libc/gen/getpwent.3 (revision 77b7cdf1999ee965ad494fddd184b18f532ac91a)
1.\" Copyright (c) 1988, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     From: @(#)getpwent.3	8.2 (Berkeley) 12/11/93
33.\" $FreeBSD$
34.\"
35.Dd April 16, 2003
36.Dt GETPWENT 3
37.Os
38.Sh NAME
39.Nm getpwent ,
40.Nm getpwent_r ,
41.Nm getpwnam ,
42.Nm getpwnam_r ,
43.Nm getpwuid ,
44.Nm getpwuid_r ,
45.Nm setpassent ,
46.Nm setpwent ,
47.Nm endpwent
48.Nd password database operations
49.Sh LIBRARY
50.Lb libc
51.Sh SYNOPSIS
52.In sys/types.h
53.In pwd.h
54.Ft struct passwd *
55.Fn getpwent void
56.Ft int
57.Fn getpwent_r "struct passwd *pwd" "char *buffer" "size_t bufsize" "struct passwd **result"
58.Ft struct passwd *
59.Fn getpwnam "const char *login"
60.Ft int
61.Fn getpwnam_r "const char *name" "struct passwd *pwd" "char *buffer" "size_t bufsize" "struct passwd **result"
62.Ft struct passwd *
63.Fn getpwuid "uid_t uid"
64.Ft int
65.Fn getpwuid_r "uid_t uid" "struct passwd *pwd" "char *buffer" "size_t bufsize" "struct passwd **result"
66.Ft int
67.Fn setpassent "int  stayopen"
68.Ft void
69.Fn setpwent void
70.Ft void
71.Fn endpwent void
72.Sh DESCRIPTION
73These functions
74operate on the password database file
75which is described
76in
77.Xr passwd 5 .
78Each entry in the database is defined by the structure
79.Vt passwd
80found in the include
81file
82.Aq Pa pwd.h :
83.Bd -literal -offset indent
84struct passwd {
85	char	*pw_name;	/* user name */
86	char	*pw_passwd;	/* encrypted password */
87	uid_t	pw_uid;		/* user uid */
88	gid_t	pw_gid;		/* user gid */
89	time_t	pw_change;	/* password change time */
90	char	*pw_class;	/* user access class */
91	char	*pw_gecos;	/* Honeywell login info */
92	char	*pw_dir;	/* home directory */
93	char	*pw_shell;	/* default shell */
94	time_t	pw_expire;	/* account expiration */
95	int	pw_fields;	/* internal: fields filled in */
96};
97.Ed
98.Pp
99The functions
100.Fn getpwnam
101and
102.Fn getpwuid
103search the password database for the given login name or user uid,
104respectively, always returning the first one encountered.
105.Pp
106The
107.Fn getpwent
108function
109sequentially reads the password database and is intended for programs
110that wish to process the complete list of users.
111.Pp
112The functions
113.Fn getpwent_r ,
114.Fn getpwnam_r ,
115and
116.Fn getpwuid_r
117are thread-safe versions of
118.Fn getpwent ,
119.Fn getpwnam ,
120and
121.Fn getpwuid ,
122respectively.
123The caller must provide storage for the results of the search in
124the
125.Fa pwd ,
126.Fa buffer ,
127.Fa bufsize ,
128and
129.Fa result
130arguments.
131When these functions are successful, the
132.Fa pwd
133argument will be filled-in, and a pointer to that argument will be
134stored in
135.Fa result .
136If an entry is not found or an error occurs,
137.Fa result
138will be set to
139.Dv NULL .
140.Pp
141The
142.Fn setpassent
143function
144accomplishes two purposes.
145First, it causes
146.Fn getpwent
147to ``rewind'' to the beginning of the database.
148Additionally, if
149.Fa stayopen
150is non-zero, file descriptors are left open, significantly speeding
151up subsequent accesses for all of the routines.
152(This latter functionality is unnecessary for
153.Fn getpwent
154as it doesn't close its file descriptors by default.)
155.Pp
156It is dangerous for long-running programs to keep the file descriptors
157open as the database will become out of date if it is updated while the
158program is running.
159.Pp
160The
161.Fn setpwent
162function
163is identical to
164.Fn setpassent
165with an argument of zero.
166.Pp
167The
168.Fn endpwent
169function
170closes any open files.
171.Pp
172These routines have been written to ``shadow'' the password file, e.g.\&
173allow only certain programs to have access to the encrypted password.
174If the process which calls them has an effective uid of 0, the encrypted
175password will be returned, otherwise, the password field of the returned
176structure will point to the string
177.Ql * .
178.Sh RETURN VALUES
179The functions
180.Fn getpwent ,
181.Fn getpwnam ,
182and
183.Fn getpwuid
184return a valid pointer to a passwd structure on success
185or
186.Dv NULL
187if the entry is not found or if an error occurs.
188In the latter case,
189.Va errno
190will be set.
191The functions
192.Fn getpwent_r ,
193.Fn getpwnam_r ,
194and
195.Fn getpwuid_r
196return 0 if no error occurred, or an error number to indicate failure.
197It is not an error if a matching entry is not found.  (Thus, if
198.Fa result
199is
200.Dv NULL
201and the return value is 0, no matching entry exists.)
202.Pp
203The
204.Fn setpassent
205function returns 0 on failure and 1 on success.
206The
207.Fn endpwent
208and
209.Fn setpwent
210functions
211have no return value.
212.Sh ERRORS
213These routines may fail for any of the errors specified in
214.Xr open 2 ,
215.Xr dbopen 2 ,
216.Xr socket 2 ,
217and
218.Xr connect 2 ,
219in addition to the following:
220.Bl -tag -width Er
221.It Bq Er ERANGE
222The buffer specified by the
223.Fa buffer
224and
225.Fa bufsize
226arguments was insufficiently sized to store the result.
227The caller should retry with a larger buffer.
228.El
229.Sh FILES
230.Bl -tag -width /etc/master.passwd -compact
231.It Pa /etc/pwd.db
232The insecure password database file
233.It Pa /etc/spwd.db
234The secure password database file
235.It Pa /etc/master.passwd
236The current password file
237.It Pa /etc/passwd
238A Version 7 format password file
239.El
240.Sh SEE ALSO
241.Xr getlogin 2 ,
242.Xr getgrent 3 ,
243.Xr nsswitch.conf 5 ,
244.Xr passwd 5 ,
245.Xr pwd_mkdb 8 ,
246.Xr vipw 8 ,
247.Xr yp 8
248.Sh HISTORY
249The
250.Fn getpwent ,
251.Fn getpwnam ,
252.Fn getpwuid ,
253.Fn setpwent ,
254and
255.Fn endpwent
256functions appeared in
257.At v7 .
258The
259.Fn setpassent
260function appeared in
261.Bx 4.3 Reno .
262The
263.Fn getpwent_r ,
264.Fn getpwnam_r ,
265and
266.Fn getpwuid_r
267functions appeared in
268.Fx 5.1 .
269.Sh STANDARDS
270The
271.Fn getpwent ,
272.Fn getpwnam ,
273.Fn getpwnam_r ,
274.Fn getpwuid ,
275.Fn getpwuid_r ,
276.Fn setpwent ,
277and
278.Fn endpwent
279functions conform to
280.St -p1003.1-96 .
281.Sh COMPATIBILITY
282The historic function
283.Xr setpwfile 3 ,
284which allowed the specification of alternate password databases,
285has been deprecated and is no longer available.
286.Sh BUGS
287The functions
288.Fn getpwent ,
289.Fn getpwnam ,
290and
291.Fn getpwuid ,
292leave their results in an internal static object and return
293a pointer to that object.
294Subsequent calls to
295the same function
296will modify the same object.
297.Pp
298The functions
299.Fn getpwent ,
300.Fn getpwent_r ,
301.Fn endpwent ,
302.Fn setpassent ,
303and
304.Fn setpwent
305are fairly useless in a networked environment and should be
306avoided, if possible.
307The
308.Fn getpwent
309and
310.Fn getpwent_r
311functions
312make no attempt to suppress duplicate information if multiple
313sources are specified in
314.Xr nsswitch.conf 5 .
315