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 65These functions 66operate on the password database file 67which is described 68in 69.Xr passwd 5 . 70Each entry in the database is defined by the structure 71.Ar passwd 72found in the include 73file 74.Aq Pa pwd.h : 75.Bd -literal -offset indent 76struct 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 91The functions 92.Fn getpwnam 93and 94.Fn getpwuid 95search the password database for the given login name or user uid, 96respectively, always returning the first one encountered. 97.Pp 98The 99.Fn getpwent 100function 101sequentially reads the password database and is intended for programs 102that wish to process the complete list of users. 103.Pp 104The 105.Fn setpassent 106function 107accomplishes two purposes. 108First, it causes 109.Fn getpwent 110to ``rewind'' to the beginning of the database. 111Additionally, if 112.Fa stayopen 113is non-zero, file descriptors are left open, significantly speeding 114up subsequent accesses for all of the routines. 115(This latter functionality is unnecessary for 116.Fn getpwent 117as it doesn't close its file descriptors by default.) 118.Pp 119It is dangerous for long-running programs to keep the file descriptors 120open as the database will become out of date if it is updated while the 121program is running. 122.Pp 123The 124.Fn setpwent 125function 126is identical to 127.Fn setpassent 128with an argument of zero. 129.Pp 130The 131.Fn endpwent 132function 133closes any open files. 134.Pp 135These routines have been written to ``shadow'' the password file, e.g.\& 136allow only certain programs to have access to the encrypted password. 137If the process which calls them has an effective uid of 0, the encrypted 138password will be returned, otherwise, the password field of the returned 139structure will point to the string 140.Ql * . 141.Sh YP/NIS INTERACTION 142When the 143.Xr yp 8 144password database is enabled, the 145.Fn getpwnam 146and 147.Fn getpwuid 148functions use the YP maps 149.Dq Li passwd.byname 150and 151.Dq Li passwd.byuid , 152respectively, if the requested password entry is not found in the 153local database. The 154.Fn getpwent 155function will step through the YP map 156.Dq Li passwd.byname 157if the entire map is enabled as described in 158.Xr passwd 5 . 159.Sh RETURN VALUES 160The functions 161.Fn getpwent , 162.Fn getpwnam , 163and 164.Fn getpwuid , 165return a valid pointer to a passwd structure on success 166and a null pointer if end-of-file is reached or an error occurs. 167The 168.Fn setpassent 169function returns 0 on failure and 1 on success. 170The 171.Fn endpwent 172and 173.Fn setpwent 174functions 175have no return value. 176.Sh FILES 177.Bl -tag -width /etc/master.passwd -compact 178.It Pa /etc/pwd.db 179The insecure password database file 180.It Pa /etc/spwd.db 181The secure password database file 182.It Pa /etc/master.passwd 183The current password file 184.It Pa /etc/passwd 185A 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 195The 196.Fn getpwent , 197.Fn getpwnam , 198.Fn getpwuid , 199.Fn setpwent , 200and 201.Fn endpwent 202functions appeared in 203.At v7 . 204The 205.Fn setpassent 206function appeared in 207.Bx 4.3 Reno . 208.Sh COMPATIBILITY 209The historic function 210.Xr setpwfile 3 , 211which allowed the specification of alternate password databases, 212has been deprecated and is no longer available. 213.Sh BUGS 214The functions 215.Fn getpwent , 216.Fn getpwnam , 217and 218.Fn getpwuid , 219leave their results in an internal static object and return 220a pointer to that object. 221Subsequent calls to 222the same function 223will modify the same object. 224