getspnam, getspnam_r, getspent, getspent_r, setspent, endspent, fgetspent, fgetspent_r, sgetspent, sgetspent_r, putspent, lckpwdf, ulckpwdf − get shadow password file entry


/* 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 *fp);

struct spwd *sgetspent(const char *s);

int putspent(const struct spwd *p, FILE *fp);

int lckpwdf(void);

int ulckpwdf(void);

/* GNU extension */
#include <shadow.h>

int getspent_r(struct spwd *spbuf,
char *
buf, size_t buflen, struct spwd **spbufp);

int getspnam_r(const char *name, struct spwd *spbuf,
char *
buf, size_t buflen, struct spwd **spbufp);

int fgetspent_r(FILE *fp, struct spwd *spbuf,
char *
buf, size_t buflen, struct spwd **spbufp);

int sgetspent_r(const char *s, struct spwd *spbuf,
char *
buf, size_t buflen, struct spwd **spbufp);

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

getspent_r(), getspnam_r(), fgetspent_r(), sgetspent_r():



Long ago it was considered safe to have encrypted passwords openly visible in the password file. When computers got faster and people got more security-conscious, this was no longer acceptable. Julianne Frances Haugh implemented the shadow password suite that keeps the encrypted passwords in the shadow password database (e.g., the local shadow password file /etc/shadow, NIS, and LDAP), readable only by root.

The functions described below resemble those for the traditional password database (e.g., see getpwnam(3) and getpwent(3)).

The getspnam() function returns a pointer to a structure containing the broken-out fields of the record in the shadow password database that matches the username name.

The getspent() function returns a pointer to the next entry in the shadow password database. The position in the input stream is initialized by setspent(). When done reading, the program may call endspent() so that resources can be deallocated.

The fgetspent() function is similar to getspent() but uses the supplied stream instead of the one implicitly opened by setspent().

The sgetspent() function parses the supplied string s into a struct spwd.

The putspent() function writes the contents of the supplied struct spwd *p as a text line in the shadow password file format to the stream fp. String entries with value NULL and numerical entries with value −1 are written as an empty string.

The lckpwdf() function 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 (lock not obtained within 15 seconds). The ulckpwdf() function 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.

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. The getspnam_r() function 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 buflen. 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.

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 */


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 cause of 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.



The caller does not have permission to access the shadow password file.


Supplied buffer is too small.



local shadow password database file


lock file

The include file <paths.h> defines the constant _PATH_SHADOW to the pathname of the shadow password file.


The shadow password database and its associated API are not specified in POSIX.1-2001. However, many other systems provide a similar API.


getgrnam(3), getpwnam(3), getpwnam_r(3), shadow(5)


This page is part of release 3.69 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at−pages/.

More Linux Commands

ldap_delete_s(3) - Perform an LDAP delete operation.........
The ldap_delete_s() routine is used to perform an LDAP delete operation synchronously. It takes dn, the DN of the entry to be deleted. It returns an LDAP error

traptoemail(1) - snmptrapd handler script to convert snmp tr
converts snmp traps into email messages. OPTIONS -f FROM sender address, defaults to root -s SMTPSERVER SMTP server, defaults to localhost ADDRESSES recipient a

pam_ftp(8) - PAM module for anonymous access module.........
pam_ftp is a PAM module which provides a pluggable anonymous ftp mode of access. This module intercepts the users name and password. If the name is ftp or anony

TIFFquery(3tiff) - query routines - Linux manual page.......
The following routines return status information about an open TIFF file. TIFFCurrentDirectory returns the index of the current directory (directories are numbe

lrint(3) - round to nearest integer - Linux manual page.....
These functions round their argument to the nearest integer value, using the current rounding direction (see fesetround(3)). Note that unlike the rint(3) family

mkdosfs(8) - create an MS-DOS file system under Linux.......
mkfs.fat is used to create an MS-DOS filesystem under Linux on a device (usually a disk partition). device is the special file corresponding to the device (e.g

mdu(1) - display the amount of space occupied by an MSDOS di
Mdu is used to list the space occupied by a directory, its subdirectories and its files. It is similar to the du command on Unix. The unit used are clusters. Us

FcMatrixScale(3) - Scale a matrix - Linux manual page.......
FcMatrixScale multiplies matrix x values by sx and y values by sy. This is done by multiplying by the matrix: sx 0 0 sy VERSION Fontconfig version 2.8.0 FcMatri

recursive_key_scan(3) - Apply a function to all keys in a ke
recursive_key_scan() performs a depth-first recursive scan of the specified keyring tree and applies func to every link found in the accessible keyrings in that

ausearch_add_timestamp_item(3) - build up search rule.......
ausearch_add_timestamp_item adds an event time condition to the current audit search expression. The search conditions can then be used to scan logs, files, or

XSetOCValues(3) - create output contexts - Linux man page...
The XCreateOC function creates an output context within the specified output method. The base font names argument is mandatory at creation time, and the output

sort(3pm) - perl pragma to control sort() behaviour.........
With the sort pragma you can control the behaviour of the builtin sort() function. In Perl versions 5.6 and earlier the quicksort algorithm was used to implemen

We can't live, work or learn in freedom unless the software we use is free.