1GETSPNAM(3) Linux Programmer's Manual GETSPNAM(3)
2
3
4
6 getspnam, getspnam_r, getspent, getspent_r, setspent, endspent, fget‐
7 spent, fgetspent_r, sgetspent, sgetspent_r, putspent, lckpwdf, ulckpwdf
8 - get shadow password file entry
9
11 /* General shadow password file API */
12 #include <shadow.h>
13
14 struct spwd *getspnam(const char *name);
15
16 struct spwd *getspent(void);
17
18 void setspent(void);
19
20 void endspent(void);
21
22 struct spwd *fgetspent(FILE *fp);
23
24 struct spwd *sgetspent(const char *s);
25
26 int putspent(struct spwd *p, FILE *fp);
27
28 int lckpwdf(void);
29
30 int ulckpwdf(void);
31
32 /* GNU extension */
33 #include <shadow.h>
34
35 int getspent_r(struct spwd *spbuf,
36 char *buf, size_t buflen, struct spwd **spbufp);
37
38 int getspnam_r(const char *name, struct spwd *spbuf,
39 char *buf, size_t buflen, struct spwd **spbufp);
40
41 int fgetspent_r(FILE *fp, struct spwd *spbuf,
42 char *buf, size_t buflen, struct spwd **spbufp);
43
44 int sgetspent_r(const char *s, struct spwd *spbuf,
45 char *buf, size_t buflen, struct spwd **spbufp);
46
47 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
48
49 getspent_r(), getspnam_r(), fgetspent_r(), sgetspent_r(): _BSD_SOURCE
50 || _SVID_SOURCE
51
53 Long ago it was considered safe to have encrypted passwords openly vis‐
54 ible in the password file. When computers got faster and people got
55 more security-conscious, this was no longer acceptable. Julianne
56 Frances Haugh implemented the shadow password suite that keeps the
57 encrypted passwords in the shadow password database (e.g., the local
58 shadow password file /etc/shadow, NIS, and LDAP), readable only by
59 root.
60
61 The functions described below resemble those for the traditional pass‐
62 word database (e.g., see getpwnam(3) and getpwent(3)).
63
64 The getspnam() function returns a pointer to a structure containing the
65 broken-out fields of the record in the shadow password database that
66 matches the username name.
67
68 The getspent() function returns a pointer to the next entry in the
69 shadow password database. The position in the input stream is initial‐
70 ized by setspent(). When done reading, the program may call endspent()
71 so that resources can be deallocated.
72
73 The fgetspent() function is similar to getspent() but uses the supplied
74 stream instead of the one implicitly opened by setspent().
75
76 The sgetspent() function parses the supplied string s into a struct
77 spwd.
78
79 The putspent() function writes the contents of the supplied struct spwd
80 *p as a text line in the shadow password file format to the stream fp.
81 String entries with value NULL and numerical entries with value -1 are
82 written as an empty string.
83
84 The lckpwdf() function is intended to protect against multiple simulta‐
85 neous accesses of the shadow password database. It tries to acquire a
86 lock, and returns 0 on success, or -1 on failure (lock not obtained
87 within 15 seconds). The ulckpwdf() function releases the lock again.
88 Note that there is no protection against direct access of the shadow
89 password file. Only programs that use lckpwdf() will notice the lock.
90
91 These were the functions that formed the original shadow API. They are
92 widely available.
93
94 Reentrant versions
95 Analogous to the reentrant functions for the password database, glibc
96 also has reentrant functions for the shadow password database. The
97 getspnam_r() function is like getspnam() but stores the retrieved
98 shadow password structure in the space pointed to by spbuf. This
99 shadow password structure contains pointers to strings, and these
100 strings are stored in the buffer buf of size buflen. A pointer to the
101 result (in case of success) or NULL (in case no entry was found or an
102 error occurred) is stored in *spbufp.
103
104 The functions getspent_r(), fgetspent_r(), and sgetspent_r() are simi‐
105 larly analogous to their non-reentrant counterparts.
106
107 Some non-glibc systems also have functions with these names, often with
108 different prototypes.
109
110 Structure
111 The shadow password structure is defined in <shadow.h> as follows:
112
113 struct spwd {
114 char *sp_namp; /* Login name */
115 char *sp_pwdp; /* Encrypted password */
116 long sp_lstchg; /* Date of last change (measured
117 in days since 1 Jan 1970) */
118 long sp_min; /* Min # of days between changes */
119 long sp_max; /* Max # of days between changes */
120 long sp_warn; /* # of days before password expires
121 to warn user to change it */
122 long sp_inact; /* # of days after password expires
123 until account is disabled */
124 long sp_expire; /* Date when account expires (measured
125 in days since 1 Jan 1970) */
126 unsigned long sp_flag; /* Reserved */
127 };
128
130 The functions that return a pointer return NULL if no more entries are
131 available or if an error occurs during processing. The functions which
132 have int as the return value return 0 for success and -1 for failure.
133
134 For the non-reentrant functions, the return value may point to static
135 area, and may be overwritten by subsequent calls to these functions.
136
137 The reentrant functions return zero on success. In case of error, an
138 error number is returned.
139
141 ERANGE Supplied buffer is too small.
142
144 /etc/shadow
145 local shadow password database file
146
147 /etc/.pwd.lock
148 lock file
149
150 The include file <paths.h> defines the constant _PATH_SHADOW to the
151 pathname of the shadow password file.
152
154 The shadow password database and its associated API are not specified
155 in POSIX.1-2001. However, many other systems provide a similar API.
156
158 getgrnam(3), getpwnam(3), getpwnam_r(3), shadow(5)
159
161 This page is part of release 3.22 of the Linux man-pages project. A
162 description of the project, and information about reporting bugs, can
163 be found at http://www.kernel.org/doc/man-pages/.
164
165
166
167GNU 2008-07-09 GETSPNAM(3)