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