xref: /freebsd/lib/libc/gen/getpwent.3 (revision 7660b554bc59a07be0431c17e0e33815818baa69)
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.In 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.
198(Thus, if
199.Fa result
200is
201.Dv NULL
202and the return value is 0, no matching entry exists.)
203.Pp
204The
205.Fn setpassent
206function returns 0 on failure and 1 on success.
207The
208.Fn endpwent
209and
210.Fn setpwent
211functions
212have no return value.
213.Sh ERRORS
214These routines may fail for any of the errors specified in
215.Xr open 2 ,
216.Xr dbopen 3 ,
217.Xr socket 2 ,
218and
219.Xr connect 2 ,
220in addition to the following:
221.Bl -tag -width Er
222.It Bq Er ERANGE
223The buffer specified by the
224.Fa buffer
225and
226.Fa bufsize
227arguments was insufficiently sized to store the result.
228The caller should retry with a larger buffer.
229.El
230.Sh FILES
231.Bl -tag -width /etc/master.passwd -compact
232.It Pa /etc/pwd.db
233The insecure password database file
234.It Pa /etc/spwd.db
235The secure password database file
236.It Pa /etc/master.passwd
237The current password file
238.It Pa /etc/passwd
239A Version 7 format password file
240.El
241.Sh SEE ALSO
242.Xr getlogin 2 ,
243.Xr getgrent 3 ,
244.Xr nsswitch.conf 5 ,
245.Xr passwd 5 ,
246.Xr pwd_mkdb 8 ,
247.Xr vipw 8 ,
248.Xr yp 8
249.Sh HISTORY
250The
251.Fn getpwent ,
252.Fn getpwnam ,
253.Fn getpwuid ,
254.Fn setpwent ,
255and
256.Fn endpwent
257functions appeared in
258.At v7 .
259The
260.Fn setpassent
261function appeared in
262.Bx 4.3 Reno .
263The
264.Fn getpwent_r ,
265.Fn getpwnam_r ,
266and
267.Fn getpwuid_r
268functions appeared in
269.Fx 5.1 .
270.Sh STANDARDS
271The
272.Fn getpwent ,
273.Fn getpwnam ,
274.Fn getpwnam_r ,
275.Fn getpwuid ,
276.Fn getpwuid_r ,
277.Fn setpwent ,
278and
279.Fn endpwent
280functions conform to
281.St -p1003.1-96 .
282.Sh COMPATIBILITY
283The historic function
284.Xr setpwfile 3 ,
285which allowed the specification of alternate password databases,
286has been deprecated and is no longer available.
287.Sh BUGS
288The functions
289.Fn getpwent ,
290.Fn getpwnam ,
291and
292.Fn getpwuid ,
293leave their results in an internal static object and return
294a pointer to that object.
295Subsequent calls to
296the same function
297will modify the same object.
298.Pp
299The functions
300.Fn getpwent ,
301.Fn getpwent_r ,
302.Fn endpwent ,
303.Fn setpassent ,
304and
305.Fn setpwent
306are fairly useless in a networked environment and should be
307avoided, if possible.
308The
309.Fn getpwent
310and
311.Fn getpwent_r
312functions
313make no attempt to suppress duplicate information if multiple
314sources are specified in
315.Xr nsswitch.conf 5 .
316