getspnam(3)
๐ธ getspnam(3) โ shadow password file entry, bestie edition ๐ธ
Okay so this is getspnam(3) from the Library Functions Manual, and itโs literally the whole shadow-password squad: getspnam, getspnam_r, getspent, getspent_r, setspent, endspent, fgetspent, fgetspent_r, sgetspent, sgetspent_r, putspent, lckpwdf, and ulckpwdf. Their whole job? Get shadow password file entries. Periodt. ๐
๐ LIBRARY ๐
Standard C library (libc, -lc).
โจ SYNOPSIS โจ
/* General shadow password file API */
#include <shadow.h>
struct spwd *getspnam(const char *name);
struct spwd *getspent(void);
void setspent(void);
void endspent(void);
struct spwd *fgetspent(FILE *stream);
struct spwd *sgetspent(const char *s);
int putspent(const struct spwd *p, FILE *stream);
int lckpwdf(void);
int ulckpwdf(void);
/* GNU extension */
#include <shadow.h>
int getspent_r(size_t size;
struct spwd *spbuf,
char buf[size], size_t size,
struct spwd **spbufp);
int getspnam_r(size_t size;
const char *name, struct spwd *spbuf,
char buf[size], size_t size,
struct spwd **spbufp);
int fgetspent_r(size_t size;
FILE *stream, struct spwd *spbuf,
char buf[size], size_t size,
struct spwd **spbufp);
int sgetspent_r(size_t size;
const char *s, struct spwd *spbuf,
char buf[size], size_t size,
struct spwd **spbufp);
๐ง Feature Test Macro Requirements for glibc ๐ง
For glibc, see feature_test_macros(7):
getspent_r(), getspnam_r(), fgetspent_r(), sgetspent_r():
Since glibc 2.19:
_DEFAULT_SOURCE
glibc 2.19 and earlier:
_BSD_SOURCE || _SVID_SOURCE
๐ DESCRIPTION ๐
So back in the day, bestie, it was somehow considered safe to have encrypted passwords just openly visible in the password file. The way that computers got faster and people got more security-conscious? Yeah, that vibe check failed hard. Julianne Frances Haugh implemented the shadow password suite, which keeps the encrypted passwords in the shadow password database โ like the local shadow password file /etc/shadow, plus NIS and LDAP โ readable only by root. Iconic and necessary. ๐
The functions below are giving very much the same energy as the traditional password database functions, like getpwnam(3) and getpwent(3).
getspnam() returns a pointer to a structure containing the broken-out fields of the record in the shadow password database that matches the username name. Slay.
getspent() returns a pointer to the next entry in the shadow password database. The position in the input stream is initialized by setspent(). When youโre done reading, your program may call endspent() so resources can be deallocated. Clean up after yourself, queen. ๐งน
fgetspent() is similar to getspent() but uses the supplied stream instead of the one implicitly opened by setspent().
sgetspent() parses the supplied string s into a struct spwd.
putspent() writes the contents of the supplied struct spwd *p as a text line in the shadow password file format to stream. String entries with value NULL and numerical entries with value -1 are written as an empty string.
lckpwdf() is intended to protect against multiple simultaneous accesses of the shadow password database. It tries to acquire a lock, and returns 0 on success, or -1 on failure โ meaning the lock was not obtained within 15 seconds. ulckpwdf() releases the lock again.
โ ๏ธ Note that there is no protection against direct access of the shadow password file. Only programs that use lckpwdf() will notice the lock. So likeโฆ the lock is cute, but itโs not a whole security aura. ๐
These were the functions that formed the original shadow API. They are widely available.
๐ Reentrant versions ๐
Analogous to the reentrant functions for the password database, glibc also has reentrant functions for the shadow password database.
getspnam_r() is like getspnam() but stores the retrieved shadow password structure in the space pointed to by spbuf. This shadow password structure contains pointers to strings, and these strings are stored in the buffer buf of size size. A pointer to the result โ in case of success โ or NULL โ in case no entry was found or an error occurred โ is stored in *spbufp.
The functions getspent_r(), fgetspent_r(), and sgetspent_r() are similarly analogous to their nonreentrant counterparts.
Some non-glibc systems also have functions with these names, often with different prototypes. Noted, bestie. Different prototype, different problems. ๐ญ
๐ฆ Structure ๐ฆ
The shadow password structure is defined in <shadow.h> as follows:
struct spwd {
char *sp_namp; /* Login name */
char *sp_pwdp; /* Encrypted password */
long sp_lstchg; /* Date of last change
(measured in days since
1970-01-01 00:00:00 +0000 (UTC)) */
long sp_min; /* Min # of days between changes */
long sp_max; /* Max # of days between changes */
long sp_warn; /* # of days before password expires
to warn user to change it */
long sp_inact; /* # of days after password expires
until account is disabled */
long sp_expire; /* Date when account expires
(measured in days since
1970-01-01 00:00:00 +0000 (UTC)) */
unsigned long sp_flag; /* Reserved */
};
๐ RETURN VALUE ๐
The functions that return a pointer return NULL if no more entries are available or if an error occurs during processing.
The functions which have int as the return value return 0 for success and -1 for failure, with errno set to indicate the error.
For the nonreentrant functions, the return value may point to static area, and may be overwritten by subsequent calls to these functions.
The reentrant functions return zero on success. In case of error, an error number is returned.
๐ ERRORS ๐
EACCESโ The caller does not have permission to access the shadow password file. Root-only energy, sorry bestie.ERANGEโ Supplied buffer is too small. The buffer said โnot today.โ ๐ซ
๐๏ธ FILES ๐๏ธ
/etc/shadowโ local shadow password database file/etc/.pwd.lockโ lock file
The include file <paths.h> defines the constant _PATH_SHADOW to the pathname of the shadow password file.
๐งต ATTRIBUTES ๐งต
For an explanation of the terms used in this section, see attributes(7).
getspnam()โ Thread safety:MT-Unsafe race:getspnam localegetspent()โ Thread safety:MT-Unsafe race:getspent race:spentbuf localesetspent(),endspent(),getspent_r()โ Thread safety:MT-Unsafe race:getspent localefgetspent()โ Thread safety:MT-Unsafe race:fgetspentsgetspent()โ Thread safety:MT-Unsafe race:sgetspentputspent(),getspnam_r(),sgetspent_r()โ Thread safety:MT-Safe localelckpwdf(),ulckpwdf(),fgetspent_r()โ Thread safety:MT-Safe
In the above table, getspent in race:getspent signifies that if any of the functions setspent(), getspent(), getspent_r(), or endspent() are used in parallel in different threads of a program, then data races could occur. So likeโฆ maybe donโt do that? ๐ญ
๐ VERSIONS ๐
Many other systems provide a similar API.
โญ STANDARDS โญ
None.
๐ SEE ALSO ๐
getgrnam(3), getpwnam(3), getpwnam_r(3), shadow(5)
๐ซถ Signed off with love by Linux man-pages 6.18, dated 2026-02-08. Stay shadowy, bestie! ๐
๐ฆ where this page comes from
- alpine 3.24: man-pages 6.18-r0 ยท GPL-2.0-or-later
- nixos 26.05: man-pages 6.18
- ๐ the page's license: GPL-1.0-or-later
- Copyright 2003, Andries E. Brouwer <aeb@cwi.nl>
- Copyright 2003, Walter Harms <walter.harms@informatik.uni-oldenburg.de>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms