158f0484fSRodney W. Grimes.\" Copyright (c) 1988, 1991, 1993 258f0484fSRodney W. Grimes.\" The Regents of the University of California. All rights reserved. 358f0484fSRodney W. Grimes.\" 458f0484fSRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without 558f0484fSRodney W. Grimes.\" modification, are permitted provided that the following conditions 658f0484fSRodney W. Grimes.\" are met: 758f0484fSRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright 858f0484fSRodney W. Grimes.\" notice, this list of conditions and the following disclaimer. 958f0484fSRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright 1058f0484fSRodney W. Grimes.\" notice, this list of conditions and the following disclaimer in the 1158f0484fSRodney W. Grimes.\" documentation and/or other materials provided with the distribution. 12fbbd9655SWarner Losh.\" 3. Neither the name of the University nor the names of its contributors 1358f0484fSRodney W. Grimes.\" may be used to endorse or promote products derived from this software 1458f0484fSRodney W. Grimes.\" without specific prior written permission. 1558f0484fSRodney W. Grimes.\" 1658f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1758f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1858f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1958f0484fSRodney W. Grimes.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2058f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2158f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2258f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2358f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2458f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2558f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2658f0484fSRodney W. Grimes.\" SUCH DAMAGE. 2758f0484fSRodney W. Grimes.\" 28*a6d40b0aSGuilherme Janczak.Dd November 28, 2022 2958f0484fSRodney W. Grimes.Dt GETPWENT 3 3058f0484fSRodney W. Grimes.Os 3158f0484fSRodney W. Grimes.Sh NAME 3258f0484fSRodney W. Grimes.Nm getpwent , 3305f98035SJacques Vidrine.Nm getpwent_r , 3458f0484fSRodney W. Grimes.Nm getpwnam , 3505f98035SJacques Vidrine.Nm getpwnam_r , 3658f0484fSRodney W. Grimes.Nm getpwuid , 3705f98035SJacques Vidrine.Nm getpwuid_r , 3858f0484fSRodney W. Grimes.Nm setpassent , 3958f0484fSRodney W. Grimes.Nm setpwent , 4058f0484fSRodney W. Grimes.Nm endpwent 4158f0484fSRodney W. Grimes.Nd password database operations 4225bb73e0SAlexey Zelkin.Sh LIBRARY 4325bb73e0SAlexey Zelkin.Lb libc 4458f0484fSRodney W. Grimes.Sh SYNOPSIS 4532eef9aeSRuslan Ermilov.In pwd.h 4658f0484fSRodney W. Grimes.Ft struct passwd * 4758f0484fSRodney W. Grimes.Fn getpwent void 4805f98035SJacques Vidrine.Ft int 4905f98035SJacques Vidrine.Fn getpwent_r "struct passwd *pwd" "char *buffer" "size_t bufsize" "struct passwd **result" 5058f0484fSRodney W. Grimes.Ft struct passwd * 5158f0484fSRodney W. Grimes.Fn getpwnam "const char *login" 5205f98035SJacques Vidrine.Ft int 5305f98035SJacques Vidrine.Fn getpwnam_r "const char *name" "struct passwd *pwd" "char *buffer" "size_t bufsize" "struct passwd **result" 5458f0484fSRodney W. Grimes.Ft struct passwd * 5558f0484fSRodney W. Grimes.Fn getpwuid "uid_t uid" 5658f0484fSRodney W. Grimes.Ft int 5705f98035SJacques Vidrine.Fn getpwuid_r "uid_t uid" "struct passwd *pwd" "char *buffer" "size_t bufsize" "struct passwd **result" 5805f98035SJacques Vidrine.Ft int 5958f0484fSRodney W. Grimes.Fn setpassent "int stayopen" 60f6399947SSteve Price.Ft void 6158f0484fSRodney W. Grimes.Fn setpwent void 6258f0484fSRodney W. Grimes.Ft void 6358f0484fSRodney W. Grimes.Fn endpwent void 6458f0484fSRodney W. Grimes.Sh DESCRIPTION 6558f0484fSRodney W. GrimesThese functions 6658f0484fSRodney W. Grimesoperate on the password database file 6758f0484fSRodney W. Grimeswhich is described 6858f0484fSRodney W. Grimesin 6958f0484fSRodney W. Grimes.Xr passwd 5 . 7058f0484fSRodney W. GrimesEach entry in the database is defined by the structure 715c564baeSRuslan Ermilov.Vt passwd 7258f0484fSRodney W. Grimesfound in the include 7358f0484fSRodney W. Grimesfile 74fe08efe6SRuslan Ermilov.In pwd.h : 7558f0484fSRodney W. Grimes.Bd -literal -offset indent 7658f0484fSRodney W. Grimesstruct passwd { 7758f0484fSRodney W. Grimes char *pw_name; /* user name */ 7858f0484fSRodney W. Grimes char *pw_passwd; /* encrypted password */ 79a135bdb9SDag-Erling Smørgrav uid_t pw_uid; /* user uid */ 80a135bdb9SDag-Erling Smørgrav gid_t pw_gid; /* user gid */ 8158f0484fSRodney W. Grimes time_t pw_change; /* password change time */ 8258f0484fSRodney W. Grimes char *pw_class; /* user access class */ 8358f0484fSRodney W. Grimes char *pw_gecos; /* Honeywell login info */ 8458f0484fSRodney W. Grimes char *pw_dir; /* home directory */ 8558f0484fSRodney W. Grimes char *pw_shell; /* default shell */ 8658f0484fSRodney W. Grimes time_t pw_expire; /* account expiration */ 871ffa6450SMike Pritchard int pw_fields; /* internal: fields filled in */ 8858f0484fSRodney W. Grimes}; 8958f0484fSRodney W. Grimes.Ed 9058f0484fSRodney W. Grimes.Pp 9158f0484fSRodney W. GrimesThe functions 9258f0484fSRodney W. Grimes.Fn getpwnam 9358f0484fSRodney W. Grimesand 9458f0484fSRodney W. Grimes.Fn getpwuid 9558f0484fSRodney W. Grimessearch the password database for the given login name or user uid, 9658f0484fSRodney W. Grimesrespectively, always returning the first one encountered. 9758f0484fSRodney W. Grimes.Pp 9858f0484fSRodney W. GrimesThe 9958f0484fSRodney W. Grimes.Fn getpwent 10058f0484fSRodney W. Grimesfunction 10158f0484fSRodney W. Grimessequentially reads the password database and is intended for programs 10258f0484fSRodney W. Grimesthat wish to process the complete list of users. 10358f0484fSRodney W. Grimes.Pp 10405f98035SJacques VidrineThe functions 10505f98035SJacques Vidrine.Fn getpwent_r , 10605f98035SJacques Vidrine.Fn getpwnam_r , 10705f98035SJacques Vidrineand 10805f98035SJacques Vidrine.Fn getpwuid_r 10905f98035SJacques Vidrineare thread-safe versions of 11005f98035SJacques Vidrine.Fn getpwent , 11105f98035SJacques Vidrine.Fn getpwnam , 11205f98035SJacques Vidrineand 11305f98035SJacques Vidrine.Fn getpwuid , 11405f98035SJacques Vidrinerespectively. 11505f98035SJacques VidrineThe caller must provide storage for the results of the search in 11605f98035SJacques Vidrinethe 11705f98035SJacques Vidrine.Fa pwd , 11805f98035SJacques Vidrine.Fa buffer , 11905f98035SJacques Vidrine.Fa bufsize , 12005f98035SJacques Vidrineand 12105f98035SJacques Vidrine.Fa result 12205f98035SJacques Vidrinearguments. 12305f98035SJacques VidrineWhen these functions are successful, the 12405f98035SJacques Vidrine.Fa pwd 12505f98035SJacques Vidrineargument will be filled-in, and a pointer to that argument will be 12605f98035SJacques Vidrinestored in 12705f98035SJacques Vidrine.Fa result . 12805f98035SJacques VidrineIf an entry is not found or an error occurs, 12905f98035SJacques Vidrine.Fa result 13005f98035SJacques Vidrinewill be set to 13105f98035SJacques Vidrine.Dv NULL . 13205f98035SJacques Vidrine.Pp 13358f0484fSRodney W. GrimesThe 13458f0484fSRodney W. Grimes.Fn setpassent 13558f0484fSRodney W. Grimesfunction 13658f0484fSRodney W. Grimesaccomplishes two purposes. 13758f0484fSRodney W. GrimesFirst, it causes 13858f0484fSRodney W. Grimes.Fn getpwent 13958f0484fSRodney W. Grimesto ``rewind'' to the beginning of the database. 14058f0484fSRodney W. GrimesAdditionally, if 14158f0484fSRodney W. Grimes.Fa stayopen 14258f0484fSRodney W. Grimesis non-zero, file descriptors are left open, significantly speeding 14358f0484fSRodney W. Grimesup subsequent accesses for all of the routines. 14458f0484fSRodney W. Grimes(This latter functionality is unnecessary for 14558f0484fSRodney W. Grimes.Fn getpwent 1460227791bSRuslan Ermilovas it does not close its file descriptors by default.) 14758f0484fSRodney W. Grimes.Pp 14858f0484fSRodney W. GrimesIt is dangerous for long-running programs to keep the file descriptors 14958f0484fSRodney W. Grimesopen as the database will become out of date if it is updated while the 15058f0484fSRodney W. Grimesprogram is running. 15158f0484fSRodney W. Grimes.Pp 15258f0484fSRodney W. GrimesThe 15358f0484fSRodney W. Grimes.Fn setpwent 15458f0484fSRodney W. Grimesfunction 15558f0484fSRodney W. Grimesis identical to 15658f0484fSRodney W. Grimes.Fn setpassent 15758f0484fSRodney W. Grimeswith an argument of zero. 15858f0484fSRodney W. Grimes.Pp 15958f0484fSRodney W. GrimesThe 16058f0484fSRodney W. Grimes.Fn endpwent 16158f0484fSRodney W. Grimesfunction 16258f0484fSRodney W. Grimescloses any open files. 16358f0484fSRodney W. Grimes.Pp 16457e4378bSRuslan ErmilovThese routines have been written to ``shadow'' the password file, e.g.\& 16558f0484fSRodney W. Grimesallow only certain programs to have access to the encrypted password. 16658f0484fSRodney W. GrimesIf the process which calls them has an effective uid of 0, the encrypted 16758f0484fSRodney W. Grimespassword will be returned, otherwise, the password field of the returned 16858f0484fSRodney W. Grimesstructure will point to the string 16958f0484fSRodney W. Grimes.Ql * . 17058f0484fSRodney W. Grimes.Sh RETURN VALUES 17158f0484fSRodney W. GrimesThe functions 17258f0484fSRodney W. Grimes.Fn getpwent , 17358f0484fSRodney W. Grimes.Fn getpwnam , 17458f0484fSRodney W. Grimesand 17505f98035SJacques Vidrine.Fn getpwuid 17658f0484fSRodney W. Grimesreturn a valid pointer to a passwd structure on success 17705f98035SJacques Vidrineor 17805f98035SJacques Vidrine.Dv NULL 17905f98035SJacques Vidrineif the entry is not found or if an error occurs. 1805edb850fSTim KientzleIf an error does occur, 18105f98035SJacques Vidrine.Va errno 18205f98035SJacques Vidrinewill be set. 1835edb850fSTim KientzleNote that programs must explicitly set 1845edb850fSTim Kientzle.Va errno 1855edb850fSTim Kientzleto zero before calling any of these functions if they need to 1865edb850fSTim Kientzledistinguish between a non-existent entry and an error. 18705f98035SJacques VidrineThe functions 18805f98035SJacques Vidrine.Fn getpwent_r , 18905f98035SJacques Vidrine.Fn getpwnam_r , 19005f98035SJacques Vidrineand 19105f98035SJacques Vidrine.Fn getpwuid_r 19205f98035SJacques Vidrinereturn 0 if no error occurred, or an error number to indicate failure. 1933a5146d9SRuslan ErmilovIt is not an error if a matching entry is not found. 1943a5146d9SRuslan Ermilov(Thus, if 19505f98035SJacques Vidrine.Fa result 19605f98035SJacques Vidrineis 19705f98035SJacques Vidrine.Dv NULL 19805f98035SJacques Vidrineand the return value is 0, no matching entry exists.) 19905f98035SJacques Vidrine.Pp 200f6399947SSteve PriceThe 20158f0484fSRodney W. Grimes.Fn setpassent 202f6399947SSteve Pricefunction returns 0 on failure and 1 on success. 20358f0484fSRodney W. GrimesThe 20458f0484fSRodney W. Grimes.Fn endpwent 205f6399947SSteve Priceand 206f6399947SSteve Price.Fn setpwent 207f6399947SSteve Pricefunctions 208f6399947SSteve Pricehave no return value. 20924a0682cSRuslan Ermilov.Sh FILES 21024a0682cSRuslan Ermilov.Bl -tag -width /etc/master.passwd -compact 21124a0682cSRuslan Ermilov.It Pa /etc/pwd.db 21224a0682cSRuslan ErmilovThe insecure password database file 21324a0682cSRuslan Ermilov.It Pa /etc/spwd.db 21424a0682cSRuslan ErmilovThe secure password database file 21524a0682cSRuslan Ermilov.It Pa /etc/master.passwd 21624a0682cSRuslan ErmilovThe current password file 21724a0682cSRuslan Ermilov.It Pa /etc/passwd 21824a0682cSRuslan ErmilovA Version 7 format password file 21924a0682cSRuslan Ermilov.El 22024a0682cSRuslan Ermilov.Sh COMPATIBILITY 22124a0682cSRuslan ErmilovThe historic function 22224a0682cSRuslan Ermilov.Xr setpwfile 3 , 22324a0682cSRuslan Ermilovwhich allowed the specification of alternate password databases, 22424a0682cSRuslan Ermilovhas been deprecated and is no longer available. 22505f98035SJacques Vidrine.Sh ERRORS 22605f98035SJacques VidrineThese routines may fail for any of the errors specified in 22705f98035SJacques Vidrine.Xr open 2 , 2280d511e32SPhilippe Charnier.Xr dbopen 3 , 22905f98035SJacques Vidrine.Xr socket 2 , 23005f98035SJacques Vidrineand 23105f98035SJacques Vidrine.Xr connect 2 , 23205f98035SJacques Vidrinein addition to the following: 23305f98035SJacques Vidrine.Bl -tag -width Er 23405f98035SJacques Vidrine.It Bq Er ERANGE 23505f98035SJacques VidrineThe buffer specified by the 23605f98035SJacques Vidrine.Fa buffer 23705f98035SJacques Vidrineand 23805f98035SJacques Vidrine.Fa bufsize 23905f98035SJacques Vidrinearguments was insufficiently sized to store the result. 24005f98035SJacques VidrineThe caller should retry with a larger buffer. 24105f98035SJacques Vidrine.El 24258f0484fSRodney W. Grimes.Sh SEE ALSO 24378b0b234SMike Pritchard.Xr getlogin 2 , 24458f0484fSRodney W. Grimes.Xr getgrent 3 , 245248aee62SJacques Vidrine.Xr nsswitch.conf 5 , 24658f0484fSRodney W. Grimes.Xr passwd 5 , 24758f0484fSRodney W. Grimes.Xr pwd_mkdb 8 , 248491a8429SRuslan Ermilov.Xr vipw 8 , 249491a8429SRuslan Ermilov.Xr yp 8 25024a0682cSRuslan Ermilov.Sh STANDARDS 25124a0682cSRuslan ErmilovThe 25224a0682cSRuslan Ermilov.Fn getpwent , 25324a0682cSRuslan Ermilov.Fn getpwnam , 25424a0682cSRuslan Ermilov.Fn getpwnam_r , 25524a0682cSRuslan Ermilov.Fn getpwuid , 25624a0682cSRuslan Ermilov.Fn getpwuid_r , 25724a0682cSRuslan Ermilov.Fn setpwent , 25824a0682cSRuslan Ermilovand 25924a0682cSRuslan Ermilov.Fn endpwent 26024a0682cSRuslan Ermilovfunctions conform to 26124a0682cSRuslan Ermilov.St -p1003.1-96 . 26258f0484fSRodney W. Grimes.Sh HISTORY 26358f0484fSRodney W. GrimesThe 264bf5a138eSMike Pritchard.Fn getpwent , 265bf5a138eSMike Pritchard.Fn getpwnam , 266bf5a138eSMike Pritchard.Fn getpwuid , 267bf5a138eSMike Pritchard.Fn setpwent , 26858f0484fSRodney W. Grimesand 269bf5a138eSMike Pritchard.Fn endpwent 27058f0484fSRodney W. Grimesfunctions appeared in 27158f0484fSRodney W. Grimes.At v7 . 27258f0484fSRodney W. GrimesThe 273bf5a138eSMike Pritchard.Fn setpassent 27458f0484fSRodney W. Grimesfunction appeared in 27558f0484fSRodney W. Grimes.Bx 4.3 Reno . 27605f98035SJacques VidrineThe 27705f98035SJacques Vidrine.Fn getpwent_r , 27805f98035SJacques Vidrine.Fn getpwnam_r , 27905f98035SJacques Vidrineand 28005f98035SJacques Vidrine.Fn getpwuid_r 28105f98035SJacques Vidrinefunctions appeared in 28205f98035SJacques Vidrine.Fx 5.1 . 28358f0484fSRodney W. Grimes.Sh BUGS 28458f0484fSRodney W. GrimesThe functions 28558f0484fSRodney W. Grimes.Fn getpwent , 28658f0484fSRodney W. Grimes.Fn getpwnam , 28758f0484fSRodney W. Grimesand 28858f0484fSRodney W. Grimes.Fn getpwuid , 28958f0484fSRodney W. Grimesleave their results in an internal static object and return 290c6ff3a1bSSheldon Hearna pointer to that object. 291c6ff3a1bSSheldon HearnSubsequent calls to 29258f0484fSRodney W. Grimesthe same function 29358f0484fSRodney W. Grimeswill modify the same object. 294248aee62SJacques Vidrine.Pp 295248aee62SJacques VidrineThe functions 296248aee62SJacques Vidrine.Fn getpwent , 29705f98035SJacques Vidrine.Fn getpwent_r , 298248aee62SJacques Vidrine.Fn endpwent , 299248aee62SJacques Vidrine.Fn setpassent , 300248aee62SJacques Vidrineand 301248aee62SJacques Vidrine.Fn setpwent 302248aee62SJacques Vidrineare fairly useless in a networked environment and should be 303248aee62SJacques Vidrineavoided, if possible. 30495f4226bSRuslan ErmilovThe 305248aee62SJacques Vidrine.Fn getpwent 30605f98035SJacques Vidrineand 30705f98035SJacques Vidrine.Fn getpwent_r 30805f98035SJacques Vidrinefunctions 30905f98035SJacques Vidrinemake no attempt to suppress duplicate information if multiple 310248aee62SJacques Vidrinesources are specified in 3118e770e78SSheldon Hearn.Xr nsswitch.conf 5 . 312