GETPWENT(3)

๐Ÿ“ฆ
version 15.1 ยท 2 versions
, in freebsd 15.1

๐Ÿ’… GETPWENT(3) โ€” password database operations ๐Ÿ’…

okay so this is the Library Functions Manual page for getpwent(3) and its whole friend group, and we're gonna make it make sense. no cap, it's giving โ€œsystem authentication loreโ€ and honestly? iconic. ๐Ÿซถ


๐ŸŽ€ NAME ๐ŸŽ€

getpwent, getpwent_r, getpwnam, getpwnam_r, getpwuid, getpwuid_r, setpassent, setpwent, endpwent โ€” password database operations, bestie. that's the whole squad. ๐Ÿ’–


๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C Library (libc, -lc). slay. โœจ


๐Ÿงš SYNOPSIS ๐Ÿงš

#include <pwd.h>

struct passwd *
getpwent(void);

int
getpwent_r(struct passwd *pwd, char *buffer, size_t bufsize,
    struct passwd **result);

struct passwd *
getpwnam(const char *login);

int
getpwnam_r(const char *name, struct passwd *pwd, char *buffer,
    size_t bufsize, struct passwd **result);

struct passwd *
getpwuid(uid_t uid);

int
getpwuid_r(uid_t uid, struct passwd *pwd, char *buffer, size_t bufsize,
    struct passwd **result);

int
setpassent(int stayopen);

void
setpwent(void);

void
endpwent(void);

๐ŸŒธ DESCRIPTION ๐ŸŒธ

These functions operate on the password database file which is described in passwd(5). each entry in the database is defined by the structure passwd found in the include file <pwd.h>:

struct passwd {
        char    *pw_name;       /* user name */
        char    *pw_passwd;     /* encrypted password */
        uid_t   pw_uid;         /* user uid */
        gid_t   pw_gid;         /* user gid */
        time_t  pw_change;      /* password change time */
        char    *pw_class;      /* user access class */
        char    *pw_gecos;      /* Honeywell login info */
        char    *pw_dir;        /* home directory */
        char    *pw_shell;      /* default shell */
        time_t  pw_expire;      /* account expiration */
        int     pw_fields;      /* internal: fields filled in */
};

The functions getpwnam() and getpwuid() search the password database for the given login name or user uid, respectively, always returning the first one encountered. first one wins, no debating. ๐Ÿ’…

The getpwent() function sequentially reads the password database and is intended for programs that wish to process the complete list of users. it's the โ€œgimme everybodyโ€ option. โœจ

The functions getpwent_r(), getpwnam_r(), and getpwuid_r() are thread-safe versions of getpwent(), getpwnam(), and getpwuid(), respectively. the caller must provide storage for the results of the search in the pwd, buffer, bufsize, and result arguments. when these functions are successful, the pwd argument will be filled-in, and a pointer to that argument will be stored in result. if an entry is not found or an error occurs, result will be set to NULL. noted, bestie. ๐Ÿซก

The setpassent() function accomplishes two purposes. first, it causes getpwent() to โ€œrewindโ€ to the beginning of the database. additionally, if stayopen is non-zero, file descriptors are left open, significantly speeding up subsequent accesses for all of the routines. (this latter functionality is unnecessary for getpwent() as it does not close its file descriptors by default.) lowkey efficient, highkey a little risky. ๐Ÿ‘€

โš ๏ธ danger tea: it is dangerous for long-running programs to keep the file descriptors open as the database will become out of date if it is updated while the program is running. fr fr, don't leave that stale vibe lying around. ๐Ÿ’€

The setpwent() function is identical to setpassent() with an argument of zero. same energy, simpler outfit. ๐ŸŽ€

The endpwent() function closes any open files. bye files, love u. ๐Ÿ‘‹

These routines have been written to โ€œshadowโ€ the password file, e.g. allow only certain programs to have access to the encrypted password. if the process which calls them has an effective uid of 0, the encrypted password will be returned, otherwise, the password field of the returned structure will point to the string โ€˜*โ€™. so yeah, root gets the real tea and everyone else gets the asterisk. periodt. โญ


๐Ÿ”ฅ RETURN VALUES ๐Ÿ”ฅ

The functions getpwent(), getpwnam(), and getpwuid() return a valid pointer to a passwd structure on success or NULL if the entry is not found or if an error occurs. if an error does occur, errno will be set. ๐Ÿ“Œ

note that programs must explicitly set errno to zero before calling any of these functions if they need to distinguish between a non-existent entry and an error. yes, manually. do not skip this. ๐Ÿ™ˆ

The functions getpwent_r(), getpwnam_r(), and getpwuid_r() return 0 if no error occurred, or an error number to indicate failure. it is not an error if a matching entry is not found. (thus, if result is NULL and the return value is 0, no matching entry exists.) so 0 + NULL = โ€œnobody home,โ€ not โ€œsomething exploded.โ€ ๐Ÿ’–

The setpassent() function returns 0 on failure and 1 on success. The endpwent() and setpwent() functions have no return value. okay so those two just do their thing and don't report back. main character energy, honestly. ๐Ÿ’…


๐Ÿ“ FILES ๐Ÿ“

  • /etc/pwd.db โ€” The insecure password database file
  • /etc/spwd.db โ€” The secure password database file
  • /etc/master.passwd โ€” The current password file
  • /etc/passwd โ€” A Version 7 format password file

๐Ÿฆ‹ COMPATIBILITY ๐Ÿฆ‹

The historic function setpwfile(3), which allowed the specification of alternate password databases, has been deprecated and is no longer available. rip to that one, but we move. ๐Ÿ’€โœจ


๐ŸŒˆ ERRORS ๐ŸŒˆ

These routines may fail for any of the errors specified in open(2), dbopen(3), socket(2), and connect(2), in addition to the following:

  • [ERANGE] โ€” The buffer specified by the buffer and bufsize arguments was insufficiently sized to store the result. The caller should retry with a larger buffer. so yeah, your buffer was too small, bestie โ€” size up and try again. ๐Ÿ“๐Ÿ’–

๐Ÿ‘‘ SEE ALSO ๐Ÿ‘‘

getlogin(2), getgrent(3), nsswitch.conf(5), passwd(5), pwd_mkdb(8), vipw(8), yp(8)

the lore is interconnected, iykyk. ๐Ÿงš


โญ STANDARDS โญ

The getpwent(), getpwnam(), getpwnam_r(), getpwuid(), getpwuid_r(), setpwent(), and endpwent() functions conform to ISO/IEC 9945-1:1996 (โ€œPOSIX.1โ€). certified and standardized, no cap. ๐ŸŽ€


๐Ÿ’œ HISTORY ๐Ÿ’œ

  • The getpwent(), getpwnam(), getpwuid(), setpwent(), and endpwent() functions appeared in Version 7 AT&T UNIX.
  • The setpassent() function appeared in 4.3BSD-Reno.
  • The getpwent_r(), getpwnam_r(), and getpwuid_r() functions appeared in FreeBSD 5.1.

honestly? a whole lineage. ancestors ate. ๐Ÿ‘‘๐Ÿ”ฅ


๐Ÿ’€ BUGS ๐Ÿ’€

The functions getpwent(), getpwnam(), and getpwuid(), leave their results in an internal static object and return a pointer to that object. subsequent calls to the same function will modify the same object. so don't get attached to that pointer like it's your permanent emotional support result โ€” it can change under you. ๐Ÿ˜ญ

The functions getpwent(), getpwent_r(), endpwent(), setpassent(), and setpwent() are fairly useless in a networked environment and should be avoided, if possible. The getpwent() and getpwent_r() functions make no attempt to suppress duplicate information if multiple sources are specified in nsswitch.conf(5). so yeah, duplicates may absolutely show up and the manual is not sorry. ๐Ÿ™ˆ๐Ÿ’…


okay that's a wrap on this Debian GETPWENT(3) page dated November 28, 2022 โ€” go forth and query responsibly, bestie! ๐Ÿ’–โœจ

๐Ÿ“ฆ where this page comes from
  • freebsd 15.1: base 15.1
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms