1getspnam(3) Library Functions 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 Standard C library (libc, -lc)
12
14 /* General shadow password file API */
15 #include <shadow.h>
16
17 struct spwd *getspnam(const char *name);
18 struct spwd *getspent(void);
19
20 void setspent(void);
21 void endspent(void);
22
23 struct spwd *fgetspent(FILE *stream);
24 struct spwd *sgetspent(const char *s);
25
26 int putspent(const struct spwd *p, FILE *stream);
27
28 int lckpwdf(void);
29 int ulckpwdf(void);
30
31 /* GNU extension */
32 #include <shadow.h>
33
34 int getspent_r(struct spwd *spbuf,
35 char buf[.buflen], size_t buflen, struct spwd **spbufp);
36 int getspnam_r(const char *name, struct spwd *spbuf,
37 char buf[.buflen], size_t buflen, struct spwd **spbufp);
38
39 int fgetspent_r(FILE *stream, struct spwd *spbuf,
40 char buf[.buflen], size_t buflen, struct spwd **spbufp);
41 int sgetspent_r(const char *s, struct spwd *spbuf,
42 char buf[.buflen], size_t buflen, struct spwd **spbufp);
43
44 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
45
46 getspent_r(), getspnam_r(), fgetspent_r(), sgetspent_r():
47 Since glibc 2.19:
48 _DEFAULT_SOURCE
49 glibc 2.19 and earlier:
50 _BSD_SOURCE || _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 en‐
57 crypted 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 stream. String
81 entries with value NULL and numerical entries with value -1 are written
82 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 nonreentrant 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
117 (measured in days since
118 1970-01-01 00:00:00 +0000 (UTC)) */
119 long sp_min; /* Min # of days between changes */
120 long sp_max; /* Max # of days between changes */
121 long sp_warn; /* # of days before password expires
122 to warn user to change it */
123 long sp_inact; /* # of days after password expires
124 until account is disabled */
125 long sp_expire; /* Date when account expires
126 (measured in days since
127 1970-01-01 00:00:00 +0000 (UTC)) */
128 unsigned long sp_flag; /* Reserved */
129 };
130
132 The functions that return a pointer return NULL if no more entries are
133 available or if an error occurs during processing. The functions which
134 have int as the return value return 0 for success and -1 for failure,
135 with errno set to indicate the error.
136
137 For the nonreentrant functions, the return value may point to static
138 area, and may be overwritten by subsequent calls to these functions.
139
140 The reentrant functions return zero on success. In case of error, an
141 error number is returned.
142
144 EACCES The caller does not have permission to access the shadow pass‐
145 word file.
146
147 ERANGE Supplied buffer is too small.
148
150 /etc/shadow
151 local shadow password database file
152
153 /etc/.pwd.lock
154 lock file
155
156 The include file <paths.h> defines the constant _PATH_SHADOW to the
157 pathname of the shadow password file.
158
160 For an explanation of the terms used in this section, see at‐
161 tributes(7).
162
163 ┌──────────────┬───────────────┬───────────────────────────────────────┐
164 │Interface │ Attribute │ Value │
165 ├──────────────┼───────────────┼───────────────────────────────────────┤
166 │getspnam() │ Thread safety │ MT-Unsafe race:getspnam locale │
167 ├──────────────┼───────────────┼───────────────────────────────────────┤
168 │getspent() │ Thread safety │ MT-Unsafe race:getspent race:spentbuf │
169 │ │ │ locale │
170 ├──────────────┼───────────────┼───────────────────────────────────────┤
171 │setspent(), │ Thread safety │ MT-Unsafe race:getspent locale │
172 │endspent(), │ │ │
173 │getspent_r() │ │ │
174 ├──────────────┼───────────────┼───────────────────────────────────────┤
175 │fgetspent() │ Thread safety │ MT-Unsafe race:fgetspent │
176 ├──────────────┼───────────────┼───────────────────────────────────────┤
177 │sgetspent() │ Thread safety │ MT-Unsafe race:sgetspent │
178 ├──────────────┼───────────────┼───────────────────────────────────────┤
179 │putspent(), │ Thread safety │ MT-Safe locale │
180 │getspnam_r(), │ │ │
181 │sgetspent_r() │ │ │
182 ├──────────────┼───────────────┼───────────────────────────────────────┤
183 │lckpwdf(), │ Thread safety │ MT-Safe │
184 │ulckpwdf(), │ │ │
185 │fgetspent_r() │ │ │
186 └──────────────┴───────────────┴───────────────────────────────────────┘
187 In the above table, getspent in race:getspent signifies that if any of
188 the functions setspent(), getspent(), getspent_r(), or endspent() are
189 used in parallel in different threads of a program, then data races
190 could occur.
191
193 Many other systems provide a similar API.
194
196 None.
197
199 getgrnam(3), getpwnam(3), getpwnam_r(3), shadow(5)
200
201
202
203Linux man-pages 6.05 2023-07-20 getspnam(3)