/* 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():
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.
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.
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 */ };
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 include file <paths.h> defines the constant _PATH_SHADOW to the pathname of the shadow password file.