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