xref: /netbsd-src/lib/libc/gen/getpwent.3 (revision cda4f8f6ee55684e8d311b86c99ea59191e6b74f)
1.\" Copyright (c) 1988, 1991 The Regents of the University of California.
2.\" 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	6.8 (Berkeley) 4/19/91
33.\"	$Id: getpwent.3,v 1.2 1993/07/30 08:36:42 mycroft Exp $
34.\"
35.Dd April 19, 1991
36.Dt GETPWENT 3
37.Os
38.Sh NAME
39.Nm getpwent ,
40.Nm getpwnam ,
41.Nm getpwuid ,
42.Nm setpassent ,
43.Nm setpwent ,
44.Nm endpwent
45.Nd password database operations
46.Sh SYNOPSIS
47.Fd #include <sys/types.h>
48.Fd #include <pwd.h>
49.Ft struct passwd *
50.Fn getpwent void
51.Ft struct passwd *
52.Fn getpwnam "const char *login"
53.Ft struct passwd *
54.Fn getpwuid "uid_t uid"
55.Ft int
56.Fn setpassent "int  stayopen"
57.Ft int
58.Fn setpwent void
59.Ft void
60.Fn endpwent void
61.Sh DESCRIPTION
62These functions
63operate on the password database file
64which is described
65in
66.Xr passwd 5 .
67Each entry in the database is defined by the structure
68.Ar passwd
69found in the include
70file
71.Aq Pa pwd.h :
72.Bd -literal -offset indent
73struct passwd {
74	char	*pw_name;	/* user name */
75	char	*pw_passwd;	/* encrypted password */
76	uid_t	pw_uid;		/* user uid */
77	gid_t	pw_gid;		/* user gid */
78	time_t	pw_change;	/* password change time */
79	char	*pw_class;	/* user access class */
80	char	*pw_gecos;	/* Honeywell login info */
81	char	*pw_dir;	/* home directory */
82	char	*pw_shell;	/* default shell */
83	time_t	pw_expire;	/* account expiration */
84};
85.Ed
86.Pp
87The functions
88.Fn getpwnam
89and
90.Fn getpwuid
91search the password database for the given login name or user uid,
92respectively, always returning the first one encountered.
93.Pp
94The
95.Fn getpwent
96function
97sequentially reads the password database and is intended for programs
98that wish to process the complete list of users.
99.Pp
100The
101.Fn setpassent
102function
103accomplishes two purposes.
104First, it causes
105.Fn getpwent
106to ``rewind'' to the beginning of the database.
107Additionally, if
108.Fa stayopen
109is non-zero, file descriptors are left open, significantly speeding
110up subsequent accesses for all of the routines.
111(This latter functionality is unnecessary for
112.Fn getpwent
113as it doesn't close its file descriptors by default.)
114.Pp
115It is dangerous for long-running programs to keep the file descriptors
116open the database will become out of date if it is updated while the
117program is running.
118.Pp
119The
120.Fn setpwent
121function
122is identical to
123.Fn setpassent
124with an argument of zero.
125.Pp
126The
127.Fn endpwent
128function
129closes any open files.
130.Pp
131These routines have been written to ``shadow'' the password file, e.g.
132allow only certain programs to have access to the encrypted password.
133If the process which calls them has an effective uid of 0, the encrypted
134password will be returned, otherwise, the password field of the retuned
135structure will point to the string
136.Ql * .
137.Sh RETURN VALUES
138The functions
139.Fn getpwent ,
140.Fn getpwnam ,
141and
142.Fn getpwuid ,
143return a valid pointer to a passwd structure on success
144and a null pointer if end-of-file is reached or an error occurs.
145The functions
146.Fn setpassent
147and
148.Fn setpwent
149return 0 on failure and 1 on success.
150The
151.Fn endpwent
152function
153has no return value.
154.Sh FILES
155.Bl -tag -width /etc/master.passwd -compact
156.It Pa /var/db/pwd.db
157The insecure password database file
158.It Pa /var/db/spwd.db
159The secure password database file
160.It Pa /etc/master.passwd
161The current password file
162.It Pa /etc/passwd
163A Version 7 format password file
164.El
165.Sh SEE ALSO
166.Xr getlogin 3 ,
167.Xr getgrent 3 ,
168.Xr passwd 5 ,
169.Xr pwd_mkdb 8 ,
170.Xr vipw 8
171.Sh HISTORY
172The
173.Nm getpwent ,
174.Nm getpwnam ,
175.Nm getpwuid ,
176.Nm setpwent,
177and
178.Nm endpwent
179functions appeared in
180.At v7 .
181The
182.Nm setpassent
183function appeared in
184.Bx 4.3 Reno .
185.Sh BUGS
186The functions
187.Fn getpwent ,
188.Fn getpwnam ,
189and
190.Fn getpwuid ,
191leave their results in an internal static object and return
192a pointer to that object. Subsequent calls to
193the same function
194will modify the same object.
195.Pp
196The routines
197.Fn getpwent ,
198.Fn endpwent ,
199.Fn setpassent ,
200and
201.Fn setpwent
202are fairly useless in a networked environment and should be
203avoided, if possible.
204.Sh COMPATIBILITY
205The historic function
206.Xr setpwfile 3 ,
207which allowed the specification of alternate password databases,
208has been deprecated and is no longer available.
209