blob: 3fe70f40c4a1c0b031ebb96dbc1fc96975b8b724 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Eric Andersen9615a082004-07-15 12:53:49 +00002/* Copyright (C) 2003 Manuel Novoa III
3 *
Rob Landley25413bf2005-10-08 02:23:22 +00004 * Licensed under GPL v2, or later. See file LICENSE in this tarball.
Eric Andersen9615a082004-07-15 12:53:49 +00005 */
6
7/* Nov 6, 2003 Initial version.
8 *
9 * NOTE: This implementation is quite strict about requiring all
10 * field seperators. It also does not allow leading whitespace
11 * except when processing the numeric fields. glibc is more
12 * lenient. See the various glibc difference comments below.
13 *
14 * TODO:
Rob Landley06ec8cf2006-03-03 19:02:50 +000015 * Move to dynamic allocation of (currently statically allocated)
Eric Andersen9615a082004-07-15 12:53:49 +000016 * buffers; especially for the group-related functions since
17 * large group member lists will cause error returns.
18 *
19 */
20
Rob Landleyea224be2006-06-18 20:20:07 +000021#include "libbb.h"
Eric Andersen9615a082004-07-15 12:53:49 +000022#include <features.h>
Eric Andersen9615a082004-07-15 12:53:49 +000023#include <assert.h>
Bernhard Reutner-Fischerfa939aa2006-04-05 16:21:37 +000024
Eric Andersen9615a082004-07-15 12:53:49 +000025#ifndef _PATH_SHADOW
26#define _PATH_SHADOW "/etc/shadow"
27#endif
28#ifndef _PATH_PASSWD
29#define _PATH_PASSWD "/etc/passwd"
30#endif
31#ifndef _PATH_GROUP
32#define _PATH_GROUP "/etc/group"
33#endif
34
35/**********************************************************************/
Rob Landley06ec8cf2006-03-03 19:02:50 +000036/* Sizes for statically allocated buffers. */
Eric Andersen9615a082004-07-15 12:53:49 +000037
38/* If you change these values, also change _SC_GETPW_R_SIZE_MAX and
39 * _SC_GETGR_R_SIZE_MAX in libc/unistd/sysconf.c to match */
40#define PWD_BUFFER_SIZE 256
41#define GRP_BUFFER_SIZE 256
42
43/**********************************************************************/
44/* Prototypes for internal functions. */
45
Denis Vlasenkocb04ff52006-12-30 21:11:57 +000046static int bb__pgsreader(int (*parserfunc)(void *d, char *line), void *data,
Denis Vlasenko7d219aa2006-10-05 10:17:08 +000047 char *__restrict line_buff, size_t buflen, FILE *f);
Eric Andersen9615a082004-07-15 12:53:49 +000048
Denis Vlasenkocb04ff52006-12-30 21:11:57 +000049static int bb__parsepwent(void *pw, char *line);
50static int bb__parsegrent(void *gr, char *line);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +000051#if ENABLE_USE_BB_SHADOW
Denis Vlasenkocb04ff52006-12-30 21:11:57 +000052static int bb__parsespent(void *sp, char *line);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +000053#endif
54
Eric Andersen9615a082004-07-15 12:53:49 +000055/**********************************************************************/
Denis Vlasenko2c91efb2007-06-18 10:08:27 +000056/* We avoid having big global data. */
57
58struct statics {
59 /* Smaller things first */
60 struct passwd getpwuid_resultbuf;
61 struct group getgrgid_resultbuf;
62 struct passwd getpwnam_resultbuf;
63 struct group getgrnam_resultbuf;
64
65 char getpwuid_buffer[PWD_BUFFER_SIZE];
66 char getgrgid_buffer[GRP_BUFFER_SIZE];
67 char getpwnam_buffer[PWD_BUFFER_SIZE];
68 char getgrnam_buffer[GRP_BUFFER_SIZE];
69#if 0
70 struct passwd fgetpwent_resultbuf;
71 struct group fgetgrent_resultbuf;
72 struct spwd fgetspent_resultbuf;
73 char fgetpwent_buffer[PWD_BUFFER_SIZE];
74 char fgetgrent_buffer[GRP_BUFFER_SIZE];
75 char fgetspent_buffer[PWD_BUFFER_SIZE];
76#endif
77#if 0 //ENABLE_USE_BB_SHADOW
78 struct spwd getspuid_resultbuf;
79 struct spwd getspnam_resultbuf;
80 char getspuid_buffer[PWD_BUFFER_SIZE];
81 char getspnam_buffer[PWD_BUFFER_SIZE];
82#endif
83// Not converted - too small to bother
84//pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
85//FILE *pwf /*= NULL*/;
86//FILE *grf /*= NULL*/;
87//FILE *spf /*= NULL*/;
88#if 0
89 struct passwd getpwent_pwd;
90 struct group getgrent_gr;
91 char getpwent_line_buff[PWD_BUFFER_SIZE];
92 char getgrent_line_buff[GRP_BUFFER_SIZE];
93#endif
94#if 0 //ENABLE_USE_BB_SHADOW
95 struct spwd getspent_spwd;
96 struct spwd sgetspent_spwd;
97 char getspent_line_buff[PWD_BUFFER_SIZE];
98 char sgetspent_line_buff[PWD_BUFFER_SIZE];
99#endif
100};
101
102static struct statics *ptr_to_statics;
103
104static struct statics *get_S(void)
105{
106 if (!ptr_to_statics)
107 ptr_to_statics = xzalloc(sizeof(*ptr_to_statics));
108 return ptr_to_statics;
109}
110
111/* Always use in this order, get_S() must be called first */
112#define RESULTBUF(name) &((S = get_S())->name##_resultbuf)
113#define BUFFER(name) (S->name##_buffer)
114
115/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000116/* For the various fget??ent_r funcs, return
117 *
118 * 0: success
119 * ENOENT: end-of-file encountered
120 * ERANGE: buflen too small
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000121 * other error values possible. See bb__pgsreader.
Eric Andersen9615a082004-07-15 12:53:49 +0000122 *
123 * Also, *result == resultbuf on success and NULL on failure.
124 *
125 * NOTE: glibc difference - For the ENOENT case, glibc also sets errno.
126 * We do not, as it really isn't an error if we reach the end-of-file.
127 * Doing so is analogous to having fgetc() set errno on EOF.
128 */
129/**********************************************************************/
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000130
Eric Andersen9615a082004-07-15 12:53:49 +0000131int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf,
132 char *__restrict buffer, size_t buflen,
133 struct passwd **__restrict result)
134{
135 int rv;
136
137 *result = NULL;
138
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000139 rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, stream);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000140 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000141 *result = resultbuf;
142 }
143
144 return rv;
145}
146
Eric Andersen9615a082004-07-15 12:53:49 +0000147int fgetgrent_r(FILE *__restrict stream, struct group *__restrict resultbuf,
148 char *__restrict buffer, size_t buflen,
149 struct group **__restrict result)
150{
151 int rv;
152
153 *result = NULL;
154
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000155 rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, stream);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000156 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000157 *result = resultbuf;
158 }
159
160 return rv;
161}
162
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000163#if ENABLE_USE_BB_SHADOW
Eric Andersen9615a082004-07-15 12:53:49 +0000164int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
165 char *__restrict buffer, size_t buflen,
166 struct spwd **__restrict result)
167{
168 int rv;
169
170 *result = NULL;
171
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000172 rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, stream);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000173 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000174 *result = resultbuf;
175 }
176
177 return rv;
178}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000179#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000180
Eric Andersen9615a082004-07-15 12:53:49 +0000181/**********************************************************************/
182/* For the various fget??ent funcs, return NULL on failure and a
Rob Landley06ec8cf2006-03-03 19:02:50 +0000183 * pointer to the appropriate struct (statically allocated) on success.
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000184 * TODO: audit & stop using these in bbox, they pull in static buffers */
Eric Andersen9615a082004-07-15 12:53:49 +0000185/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000186
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000187#if 0
Eric Andersen9615a082004-07-15 12:53:49 +0000188struct passwd *fgetpwent(FILE *stream)
189{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000190 struct statics *S;
191 struct passwd *resultbuf = RESULTBUF(fgetpwent);
192 char *buffer = BUFFER(fgetpwent);
Eric Andersen9615a082004-07-15 12:53:49 +0000193 struct passwd *result;
194
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000195 fgetpwent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetpwent)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000196 return result;
197}
198
Eric Andersen9615a082004-07-15 12:53:49 +0000199struct group *fgetgrent(FILE *stream)
200{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000201 struct statics *S;
202 struct group *resultbuf = RESULTBUF(fgetgrent);
203 char *buffer = BUFFER(fgetgrent);
Eric Andersen9615a082004-07-15 12:53:49 +0000204 struct group *result;
205
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000206 fgetgrent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetgrent)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000207 return result;
208}
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000209#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000210
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000211#if ENABLE_USE_BB_SHADOW
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000212#if 0
Eric Andersen9615a082004-07-15 12:53:49 +0000213struct spwd *fgetspent(FILE *stream)
214{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000215 struct statics *S;
216 struct spwd *resultbuf = RESULTBUF(fgetspent);
217 char *buffer = BUFFER(fgetspent);
Eric Andersen9615a082004-07-15 12:53:49 +0000218 struct spwd *result;
219
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000220 fgetspent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetspent)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000221 return result;
222}
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000223#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000224
Eric Andersen9615a082004-07-15 12:53:49 +0000225int sgetspent_r(const char *string, struct spwd *result_buf,
226 char *buffer, size_t buflen, struct spwd **result)
227{
228 int rv = ERANGE;
229
230 *result = NULL;
231
232 if (buflen < PWD_BUFFER_SIZE) {
233 DO_ERANGE:
234 errno=rv;
235 goto DONE;
236 }
237
238 if (string != buffer) {
239 if (strlen(string) >= buflen) {
240 goto DO_ERANGE;
241 }
242 strcpy(buffer, string);
243 }
244
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000245 rv = bb__parsespent(result_buf, buffer);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000246 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000247 *result = result_buf;
248 }
249
250 DONE:
251 return rv;
252}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000253#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000254
Eric Andersen9615a082004-07-15 12:53:49 +0000255/**********************************************************************/
256
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000257#define GETXXKEY_R_FUNC getpwnam_r
258#define GETXXKEY_R_PARSER bb__parsepwent
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000259#define GETXXKEY_R_ENTTYPE struct passwd
260#define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key))
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000261#define GETXXKEY_R_KEYTYPE const char *__restrict
262#define GETXXKEY_R_PATHNAME _PATH_PASSWD
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000263#include "pwd_grp_internal.c"
Eric Andersen9615a082004-07-15 12:53:49 +0000264
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000265#define GETXXKEY_R_FUNC getgrnam_r
266#define GETXXKEY_R_PARSER bb__parsegrent
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000267#define GETXXKEY_R_ENTTYPE struct group
268#define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key))
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000269#define GETXXKEY_R_KEYTYPE const char *__restrict
270#define GETXXKEY_R_PATHNAME _PATH_GROUP
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000271#include "pwd_grp_internal.c"
Eric Andersen9615a082004-07-15 12:53:49 +0000272
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000273#if ENABLE_USE_BB_SHADOW
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000274#define GETXXKEY_R_FUNC getspnam_r
275#define GETXXKEY_R_PARSER bb__parsespent
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000276#define GETXXKEY_R_ENTTYPE struct spwd
277#define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key))
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000278#define GETXXKEY_R_KEYTYPE const char *__restrict
279#define GETXXKEY_R_PATHNAME _PATH_SHADOW
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000280#include "pwd_grp_internal.c"
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000281#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000282
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000283#define GETXXKEY_R_FUNC getpwuid_r
284#define GETXXKEY_R_PARSER bb__parsepwent
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000285#define GETXXKEY_R_ENTTYPE struct passwd
286#define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key)
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000287#define GETXXKEY_R_KEYTYPE uid_t
288#define GETXXKEY_R_PATHNAME _PATH_PASSWD
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000289#include "pwd_grp_internal.c"
Eric Andersen9615a082004-07-15 12:53:49 +0000290
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000291#define GETXXKEY_R_FUNC getgrgid_r
292#define GETXXKEY_R_PARSER bb__parsegrent
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000293#define GETXXKEY_R_ENTTYPE struct group
294#define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key)
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000295#define GETXXKEY_R_KEYTYPE gid_t
296#define GETXXKEY_R_PATHNAME _PATH_GROUP
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000297#include "pwd_grp_internal.c"
Eric Andersen9615a082004-07-15 12:53:49 +0000298
299/**********************************************************************/
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000300/* TODO: audit & stop using these in bbox, they pull in static buffers */
Eric Andersen9615a082004-07-15 12:53:49 +0000301
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000302/* This one has many users */
Eric Andersen9615a082004-07-15 12:53:49 +0000303struct passwd *getpwuid(uid_t uid)
304{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000305 struct statics *S;
306 struct passwd *resultbuf = RESULTBUF(getpwuid);
307 char *buffer = BUFFER(getpwuid);
Eric Andersen9615a082004-07-15 12:53:49 +0000308 struct passwd *result;
309
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000310 getpwuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getpwuid)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000311 return result;
312}
313
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000314/* This one has many users */
Eric Andersen9615a082004-07-15 12:53:49 +0000315struct group *getgrgid(gid_t gid)
316{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000317 struct statics *S;
318 struct group *resultbuf = RESULTBUF(getgrgid);
319 char *buffer = BUFFER(getgrgid);
Eric Andersen9615a082004-07-15 12:53:49 +0000320 struct group *result;
321
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000322 getgrgid_r(gid, resultbuf, buffer, sizeof(BUFFER(getgrgid)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000323 return result;
324}
325
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000326#if 0 //ENABLE_USE_BB_SHADOW
Eric Andersen9615a082004-07-15 12:53:49 +0000327/* This function is non-standard and is currently not built. It seems
328 * to have been created as a reentrant version of the non-standard
329 * functions getspuid. Why getspuid was added, I do not know. */
Eric Andersen9615a082004-07-15 12:53:49 +0000330int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf,
331 char *__restrict buffer, size_t buflen,
332 struct spwd **__restrict result)
333{
334 int rv;
335 struct passwd *pp;
336 struct passwd password;
337 char pwd_buff[PWD_BUFFER_SIZE];
338
339 *result = NULL;
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000340 rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp);
341 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000342 rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result);
343 }
344
345 return rv;
346}
347
Eric Andersen9615a082004-07-15 12:53:49 +0000348/* This function is non-standard and is currently not built.
349 * Why it was added, I do not know. */
Eric Andersen9615a082004-07-15 12:53:49 +0000350struct spwd *getspuid(uid_t uid)
351{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000352 struct statics *S;
353 struct spwd *resultbuf = RESULTBUF(getspuid);
354 char *buffer = BUFFER(getspuid);
Eric Andersen9615a082004-07-15 12:53:49 +0000355 struct spwd *result;
356
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000357 getspuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getspuid)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000358 return result;
359}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000360#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000361
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000362/* This one has many users */
Eric Andersen9615a082004-07-15 12:53:49 +0000363struct passwd *getpwnam(const char *name)
364{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000365 struct statics *S;
366 struct passwd *resultbuf = RESULTBUF(getpwnam);
367 char *buffer = BUFFER(getpwnam);
Eric Andersen9615a082004-07-15 12:53:49 +0000368 struct passwd *result;
369
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000370 getpwnam_r(name, resultbuf, buffer, sizeof(BUFFER(getpwnam)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000371 return result;
372}
373
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000374/* This one has many users */
Eric Andersen9615a082004-07-15 12:53:49 +0000375struct group *getgrnam(const char *name)
376{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000377 struct statics *S;
378 struct group *resultbuf = RESULTBUF(getgrnam);
379 char *buffer = BUFFER(getgrnam);
Eric Andersen9615a082004-07-15 12:53:49 +0000380 struct group *result;
381
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000382 getgrnam_r(name, resultbuf, buffer, sizeof(BUFFER(getgrnam)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000383 return result;
384}
385
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000386#if 0 //ENABLE_USE_BB_SHADOW
Eric Andersen9615a082004-07-15 12:53:49 +0000387struct spwd *getspnam(const char *name)
388{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000389 struct statics *S;
390 struct spwd *resultbuf = RESULTBUF(getspnam);
391 char *buffer = BUFFER(getspnam);
Eric Andersen9615a082004-07-15 12:53:49 +0000392 struct spwd *result;
393
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000394 getspnam_r(name, resultbuf, buffer, sizeof(BUFFER(getspnam)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000395 return result;
396}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000397#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000398
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000399/* This one doesn't use static buffers */
Eric Andersen9615a082004-07-15 12:53:49 +0000400int getpw(uid_t uid, char *buf)
401{
402 struct passwd resultbuf;
403 struct passwd *result;
404 char buffer[PWD_BUFFER_SIZE];
405
406 if (!buf) {
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000407 errno = EINVAL;
Eric Andersen9615a082004-07-15 12:53:49 +0000408 } else if (!getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result)) {
409 if (sprintf(buf, "%s:%s:%lu:%lu:%s:%s:%s\n",
410 resultbuf.pw_name, resultbuf.pw_passwd,
411 (unsigned long)(resultbuf.pw_uid),
412 (unsigned long)(resultbuf.pw_gid),
413 resultbuf.pw_gecos, resultbuf.pw_dir,
414 resultbuf.pw_shell) >= 0
415 ) {
416 return 0;
417 }
418 }
419
420 return -1;
421}
422
Eric Andersen9615a082004-07-15 12:53:49 +0000423/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000424
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000425/* FIXME: we don't have such CONFIG_xx - ?! */
426
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000427#if defined CONFIG_USE_BB_THREADSAFE_SHADOW && defined PTHREAD_MUTEX_INITIALIZER
428static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
429# define LOCK pthread_mutex_lock(&mylock)
430# define UNLOCK pthread_mutex_unlock(&mylock);
431#else
432# define LOCK ((void) 0)
433# define UNLOCK ((void) 0)
434#endif
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000435
Eric Andersen9615a082004-07-15 12:53:49 +0000436static FILE *pwf /*= NULL*/;
437void setpwent(void)
438{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000439 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000440 if (pwf) {
441 rewind(pwf);
442 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000443 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000444}
445
446void endpwent(void)
447{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000448 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000449 if (pwf) {
450 fclose(pwf);
451 pwf = NULL;
452 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000453 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000454}
455
456
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000457int getpwent_r(struct passwd *__restrict resultbuf,
Eric Andersen9615a082004-07-15 12:53:49 +0000458 char *__restrict buffer, size_t buflen,
459 struct passwd **__restrict result)
460{
461 int rv;
462
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000463 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000464 *result = NULL; /* In case of error... */
465
466 if (!pwf) {
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000467 pwf = fopen(_PATH_PASSWD, "r");
468 if (!pwf) {
Eric Andersen9615a082004-07-15 12:53:49 +0000469 rv = errno;
470 goto ERR;
471 }
472 }
473
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000474 rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, pwf);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000475 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000476 *result = resultbuf;
477 }
478
479 ERR:
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000480 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000481 return rv;
482}
483
Eric Andersen9615a082004-07-15 12:53:49 +0000484static FILE *grf /*= NULL*/;
485void setgrent(void)
486{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000487 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000488 if (grf) {
489 rewind(grf);
490 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000491 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000492}
493
494void endgrent(void)
495{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000496 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000497 if (grf) {
498 fclose(grf);
499 grf = NULL;
500 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000501 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000502}
503
504int getgrent_r(struct group *__restrict resultbuf,
505 char *__restrict buffer, size_t buflen,
506 struct group **__restrict result)
507{
508 int rv;
509
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000510 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000511 *result = NULL; /* In case of error... */
512
513 if (!grf) {
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000514 grf = fopen(_PATH_GROUP, "r");
515 if (!grf) {
Eric Andersen9615a082004-07-15 12:53:49 +0000516 rv = errno;
517 goto ERR;
518 }
519 }
520
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000521 rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, grf);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000522 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000523 *result = resultbuf;
524 }
525
526 ERR:
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000527 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000528 return rv;
529}
530
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000531#if ENABLE_USE_BB_SHADOW
Eric Andersen9615a082004-07-15 12:53:49 +0000532static FILE *spf /*= NULL*/;
533void setspent(void)
534{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000535 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000536 if (spf) {
537 rewind(spf);
538 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000539 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000540}
541
542void endspent(void)
543{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000544 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000545 if (spf) {
546 fclose(spf);
547 spf = NULL;
548 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000549 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000550}
551
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000552int getspent_r(struct spwd *resultbuf, char *buffer,
Eric Andersen9615a082004-07-15 12:53:49 +0000553 size_t buflen, struct spwd **result)
554{
555 int rv;
556
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000557 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000558 *result = NULL; /* In case of error... */
559
560 if (!spf) {
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000561 spf = fopen(_PATH_SHADOW, "r");
562 if (!spf) {
Eric Andersen9615a082004-07-15 12:53:49 +0000563 rv = errno;
564 goto ERR;
565 }
566 }
567
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000568 rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, spf);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000569 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000570 *result = resultbuf;
571 }
572
573 ERR:
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000574 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000575 return rv;
576}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000577#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000578
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000579#if 0
Eric Andersen9615a082004-07-15 12:53:49 +0000580struct passwd *getpwent(void)
581{
582 static char line_buff[PWD_BUFFER_SIZE];
583 static struct passwd pwd;
584 struct passwd *result;
585
586 getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
587 return result;
588}
589
Eric Andersen9615a082004-07-15 12:53:49 +0000590struct group *getgrent(void)
591{
592 static char line_buff[GRP_BUFFER_SIZE];
593 static struct group gr;
594 struct group *result;
595
596 getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
597 return result;
598}
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000599#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000600
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000601#if 0 //ENABLE_USE_BB_SHADOW
Eric Andersen9615a082004-07-15 12:53:49 +0000602struct spwd *getspent(void)
603{
604 static char line_buff[PWD_BUFFER_SIZE];
605 static struct spwd spwd;
606 struct spwd *result;
607
608 getspent_r(&spwd, line_buff, sizeof(line_buff), &result);
609 return result;
610}
611
Eric Andersen9615a082004-07-15 12:53:49 +0000612struct spwd *sgetspent(const char *string)
613{
614 static char line_buff[PWD_BUFFER_SIZE];
615 static struct spwd spwd;
616 struct spwd *result;
617
618 sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result);
619 return result;
620}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000621#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000622
Eric Andersen9615a082004-07-15 12:53:49 +0000623int initgroups(const char *user, gid_t gid)
624{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000625 FILE *grfile;
Eric Andersen9615a082004-07-15 12:53:49 +0000626 gid_t *group_list;
627 int num_groups, rv;
628 char **m;
629 struct group group;
630 char buff[PWD_BUFFER_SIZE];
631
632 rv = -1;
Denis Vlasenko92305822008-03-20 15:12:58 +0000633 grfile = fopen(_PATH_GROUP, "r");
634 if (grfile != NULL) {
Eric Andersen9615a082004-07-15 12:53:49 +0000635
Denis Vlasenko92305822008-03-20 15:12:58 +0000636 /* We alloc space for 8 gids at a time. */
637 group_list = xmalloc(8 * sizeof(gid_t *));
Eric Andersen9615a082004-07-15 12:53:49 +0000638 *group_list = gid;
639 num_groups = 1;
640
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000641 while (!bb__pgsreader(bb__parsegrent, &group, buff, sizeof(buff), grfile)) {
Eric Andersen9615a082004-07-15 12:53:49 +0000642 assert(group.gr_mem); /* Must have at least a NULL terminator. */
643 if (group.gr_gid != gid) {
Denis Vlasenkob71c6682007-07-21 15:08:09 +0000644 for (m = group.gr_mem; *m; m++) {
Eric Andersen9615a082004-07-15 12:53:49 +0000645 if (!strcmp(*m, user)) {
646 if (!(num_groups & 7)) {
Denis Vlasenko92305822008-03-20 15:12:58 +0000647 gid_t *tmp = xrealloc(group_list,
648 (num_groups+8) * sizeof(gid_t *));
Eric Andersen9615a082004-07-15 12:53:49 +0000649 group_list = tmp;
650 }
651 group_list[num_groups++] = group.gr_gid;
652 break;
653 }
654 }
655 }
656 }
657
658 rv = setgroups(num_groups, group_list);
Denis Vlasenko92305822008-03-20 15:12:58 +0000659 free(group_list);
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000660 fclose(grfile);
Eric Andersen9615a082004-07-15 12:53:49 +0000661 }
662
Eric Andersen9615a082004-07-15 12:53:49 +0000663 return rv;
664}
665
Eric Andersen9615a082004-07-15 12:53:49 +0000666int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
667{
668 int rv = -1;
669
670 if (!p || !f) {
Denis Vlasenko92305822008-03-20 15:12:58 +0000671 errno = EINVAL;
Eric Andersen9615a082004-07-15 12:53:49 +0000672 } else {
673 /* No extra thread locking is needed above what fprintf does. */
674 if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n",
675 p->pw_name, p->pw_passwd,
676 (unsigned long)(p->pw_uid),
677 (unsigned long)(p->pw_gid),
678 p->pw_gecos, p->pw_dir, p->pw_shell) >= 0
679 ) {
680 rv = 0;
681 }
682 }
683
684 return rv;
685}
686
Eric Andersen9615a082004-07-15 12:53:49 +0000687int putgrent(const struct group *__restrict p, FILE *__restrict f)
688{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000689 static const char format[] ALIGN1 = ",%s";
690
Eric Andersen9615a082004-07-15 12:53:49 +0000691 char **m;
692 const char *fmt;
693 int rv = -1;
694
695 if (!p || !f) { /* Sigh... glibc checks. */
Denis Vlasenko92305822008-03-20 15:12:58 +0000696 errno = EINVAL;
Eric Andersen9615a082004-07-15 12:53:49 +0000697 } else {
698 if (fprintf(f, "%s:%s:%lu:",
699 p->gr_name, p->gr_passwd,
700 (unsigned long)(p->gr_gid)) >= 0
701 ) {
702
703 fmt = format + 1;
704
705 assert(p->gr_mem);
706 m = p->gr_mem;
707
708 do {
709 if (!*m) {
Rob Landley25413bf2005-10-08 02:23:22 +0000710 if (fputc('\n', f) >= 0) {
Eric Andersen9615a082004-07-15 12:53:49 +0000711 rv = 0;
712 }
713 break;
714 }
715 if (fprintf(f, fmt, *m) < 0) {
716 break;
717 }
718 ++m;
719 fmt = format;
720 } while (1);
721
722 }
723
724 }
725
726 return rv;
727}
728
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000729#if ENABLE_USE_BB_SHADOW
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000730static const unsigned char _sp_off[] ALIGN1 = {
731 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
732 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
733 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
734 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
735 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
736 offsetof(struct spwd, sp_expire) /* 7 - not a char ptr */
Eric Andersen9615a082004-07-15 12:53:49 +0000737};
738
739int putspent(const struct spwd *p, FILE *stream)
740{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000741 static const char ld_format[] ALIGN1 = "%ld:";
742
Eric Andersen9615a082004-07-15 12:53:49 +0000743 const char *f;
Denis Vlasenko87468852007-04-13 23:22:00 +0000744 long x;
Eric Andersen9615a082004-07-15 12:53:49 +0000745 int i;
746 int rv = -1;
747
748 /* Unlike putpwent and putgrent, glibc does not check the args. */
749 if (fprintf(stream, "%s:%s:", p->sp_namp,
750 (p->sp_pwdp ? p->sp_pwdp : "")) < 0
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000751 ) {
Eric Andersen9615a082004-07-15 12:53:49 +0000752 goto DO_UNLOCK;
753 }
754
Denis Vlasenkob71c6682007-07-21 15:08:09 +0000755 for (i = 0; i < sizeof(_sp_off); i++) {
Eric Andersen9615a082004-07-15 12:53:49 +0000756 f = ld_format;
Denis Vlasenko87468852007-04-13 23:22:00 +0000757 x = *(const long *)(((const char *) p) + _sp_off[i]);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000758 if (x == -1) {
Eric Andersen9615a082004-07-15 12:53:49 +0000759 f += 3;
760 }
761 if (fprintf(stream, f, x) < 0) {
762 goto DO_UNLOCK;
763 }
764 }
765
766 if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) {
767 goto DO_UNLOCK;
768 }
769
Rob Landley25413bf2005-10-08 02:23:22 +0000770 if (fputc('\n', stream) > 0) {
Eric Andersen9615a082004-07-15 12:53:49 +0000771 rv = 0;
772 }
773
774DO_UNLOCK:
775 return rv;
776}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000777#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000778
Eric Andersen9615a082004-07-15 12:53:49 +0000779/**********************************************************************/
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000780/* Internal uClibc functions. */
Eric Andersen9615a082004-07-15 12:53:49 +0000781/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000782
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000783static const unsigned char pw_off[] ALIGN1 = {
784 offsetof(struct passwd, pw_name), /* 0 */
785 offsetof(struct passwd, pw_passwd), /* 1 */
786 offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */
787 offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */
788 offsetof(struct passwd, pw_gecos), /* 4 */
789 offsetof(struct passwd, pw_dir), /* 5 */
790 offsetof(struct passwd, pw_shell) /* 6 */
Eric Andersen9615a082004-07-15 12:53:49 +0000791};
792
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000793static int bb__parsepwent(void *data, char *line)
Eric Andersen9615a082004-07-15 12:53:49 +0000794{
795 char *endptr;
796 char *p;
797 int i;
798
799 i = 0;
800 do {
801 p = ((char *) ((struct passwd *) data)) + pw_off[i];
802
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000803 if ((i & 6) ^ 2) { /* i!=2 and i!=3 */
Eric Andersen9615a082004-07-15 12:53:49 +0000804 *((char **) p) = line;
805 if (i==6) {
806 return 0;
807 }
808 /* NOTE: glibc difference - glibc allows omission of
809 * ':' seperators after the gid field if all remaining
810 * entries are empty. We require all separators. */
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000811 line = strchr(line, ':');
812 if (!line) {
Eric Andersen9615a082004-07-15 12:53:49 +0000813 break;
814 }
815 } else {
816 unsigned long t = strtoul(line, &endptr, 10);
817 /* Make sure we had at least one digit, and that the
818 * failing char is the next field seperator ':'. See
819 * glibc difference note above. */
820 /* TODO: Also check for leading whitespace? */
821 if ((endptr == line) || (*endptr != ':')) {
822 break;
823 }
824 line = endptr;
825 if (i & 1) { /* i == 3 -- gid */
826 *((gid_t *) p) = t;
827 } else { /* i == 2 -- uid */
828 *((uid_t *) p) = t;
829 }
830 }
831
832 *line++ = 0;
833 ++i;
834 } while (1);
835
836 return -1;
837}
838
Eric Andersen9615a082004-07-15 12:53:49 +0000839/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000840
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000841static const unsigned char gr_off[] ALIGN1 = {
842 offsetof(struct group, gr_name), /* 0 */
843 offsetof(struct group, gr_passwd), /* 1 */
844 offsetof(struct group, gr_gid) /* 2 - not a char ptr */
Eric Andersen9615a082004-07-15 12:53:49 +0000845};
846
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000847static int bb__parsegrent(void *data, char *line)
Eric Andersen9615a082004-07-15 12:53:49 +0000848{
849 char *endptr;
850 char *p;
851 int i;
852 char **members;
853 char *end_of_buf;
854
855 end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */
856 i = 0;
857 do {
858 p = ((char *) ((struct group *) data)) + gr_off[i];
859
860 if (i < 2) {
861 *((char **) p) = line;
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000862 line = strchr(line, ':');
863 if (!line) {
Eric Andersen9615a082004-07-15 12:53:49 +0000864 break;
865 }
866 *line++ = 0;
867 ++i;
868 } else {
869 *((gid_t *) p) = strtoul(line, &endptr, 10);
870
871 /* NOTE: glibc difference - glibc allows omission of the
872 * trailing colon when there is no member list. We treat
873 * this as an error. */
874
875 /* Make sure we had at least one digit, and that the
876 * failing char is the next field seperator ':'. See
877 * glibc difference note above. */
878 if ((endptr == line) || (*endptr != ':')) {
879 break;
880 }
881
882 i = 1; /* Count terminating NULL ptr. */
883 p = endptr;
884
885 if (p[1]) { /* We have a member list to process. */
886 /* Overwrite the last ':' with a ',' before counting.
887 * This allows us to test for initial ',' and adds
888 * one ',' so that the ',' count equals the member
889 * count. */
890 *p = ',';
891 do {
892 /* NOTE: glibc difference - glibc allows and trims leading
893 * (but not trailing) space. We treat this as an error. */
894 /* NOTE: glibc difference - glibc allows consecutive and
895 * trailing commas, and ignores "empty string" users. We
896 * treat this as an error. */
897 if (*p == ',') {
898 ++i;
899 *p = 0; /* nul-terminate each member string. */
900 if (!*++p || (*p == ',') || isspace(*p)) {
901 goto ERR;
902 }
903 }
904 } while (*++p);
905 }
906
907 /* Now align (p+1), rounding up. */
908 /* Assumes sizeof(char **) is a power of 2. */
909 members = (char **)( (((intptr_t) p) + sizeof(char **))
910 & ~((intptr_t)(sizeof(char **) - 1)) );
911
912 if (((char *)(members + i)) > end_of_buf) { /* No space. */
913 break;
914 }
915
916 ((struct group *) data)->gr_mem = members;
917
918 if (--i) {
919 p = endptr; /* Pointing to char prior to first member. */
920 do {
921 *members++ = ++p;
922 if (!--i) break;
923 while (*++p) {}
924 } while (1);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000925 }
Eric Andersen9615a082004-07-15 12:53:49 +0000926 *members = NULL;
927
928 return 0;
929 }
930 } while (1);
931
932 ERR:
933 return -1;
934}
935
Eric Andersen9615a082004-07-15 12:53:49 +0000936/**********************************************************************/
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000937
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000938#if ENABLE_USE_BB_SHADOW
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000939static const unsigned char sp_off[] ALIGN1 = {
940 offsetof(struct spwd, sp_namp), /* 0 */
941 offsetof(struct spwd, sp_pwdp), /* 1 */
942 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
943 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
944 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
945 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
946 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
947 offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */
948 offsetof(struct spwd, sp_flag) /* 8 - not a char ptr */
Eric Andersen9615a082004-07-15 12:53:49 +0000949};
950
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000951static int bb__parsespent(void *data, char * line)
Eric Andersen9615a082004-07-15 12:53:49 +0000952{
953 char *endptr;
954 char *p;
955 int i;
956
957 i = 0;
958 do {
959 p = ((char *) ((struct spwd *) data)) + sp_off[i];
960 if (i < 2) {
961 *((char **) p) = line;
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000962 line = strchr(line, ':');
963 if (!line) {
Eric Andersen9615a082004-07-15 12:53:49 +0000964 break;
965 }
966 } else {
Eric Andersen9615a082004-07-15 12:53:49 +0000967 *((long *) p) = (long) strtoul(line, &endptr, 10);
968
969 if (endptr == line) {
970 *((long *) p) = ((i != 8) ? -1L : ((long)(~0UL)));
971 }
972
973 line = endptr;
974
975 if (i == 8) {
976 if (!*endptr) {
977 return 0;
978 }
979 break;
980 }
981
982 if (*endptr != ':') {
983 break;
984 }
985
986 }
987
988 *line++ = 0;
989 ++i;
990 } while (1);
991
992 return EINVAL;
993}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000994#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000995
Eric Andersen9615a082004-07-15 12:53:49 +0000996/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000997
998/* Reads until if EOF, or until if finds a line which fits in the buffer
999 * and for which the parser function succeeds.
1000 *
1001 * Returns 0 on success and ENOENT for end-of-file (glibc concession).
1002 */
1003
Denis Vlasenkocb04ff52006-12-30 21:11:57 +00001004static int bb__pgsreader(int (*parserfunc)(void *d, char *line), void *data,
Eric Andersen9615a082004-07-15 12:53:49 +00001005 char *__restrict line_buff, size_t buflen, FILE *f)
1006{
1007 int line_len;
1008 int skip;
1009 int rv = ERANGE;
1010
1011 if (buflen < PWD_BUFFER_SIZE) {
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +00001012 errno = rv;
Eric Andersen9615a082004-07-15 12:53:49 +00001013 } else {
1014 skip = 0;
1015 do {
Rob Landley25413bf2005-10-08 02:23:22 +00001016 if (!fgets(line_buff, buflen, f)) {
1017 if (feof(f)) {
Eric Andersen9615a082004-07-15 12:53:49 +00001018 rv = ENOENT;
1019 }
1020 break;
1021 }
1022
1023 line_len = strlen(line_buff) - 1; /* strlen() must be > 0. */
1024 if (line_buff[line_len] == '\n') {
1025 line_buff[line_len] = 0;
1026 } else if (line_len + 2 == buflen) { /* line too long */
1027 ++skip;
1028 continue;
1029 }
1030
1031 if (skip) {
1032 --skip;
1033 continue;
1034 }
1035
1036 /* NOTE: glibc difference - glibc strips leading whitespace from
1037 * records. We do not allow leading whitespace. */
1038
1039 /* Skip empty lines, comment lines, and lines with leading
1040 * whitespace. */
1041 if (*line_buff && (*line_buff != '#') && !isspace(*line_buff)) {
Denis Vlasenkocb04ff52006-12-30 21:11:57 +00001042 if (parserfunc == bb__parsegrent) { /* Do evil group hack. */
Eric Andersen9615a082004-07-15 12:53:49 +00001043 /* The group entry parsing function needs to know where
1044 * the end of the buffer is so that it can construct the
1045 * group member ptr table. */
1046 ((struct group *) data)->gr_name = line_buff + buflen;
1047 }
1048
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +00001049 if (!parserfunc(data, line_buff)) {
Eric Andersen9615a082004-07-15 12:53:49 +00001050 rv = 0;
1051 break;
1052 }
1053 }
1054 } while (1);
1055
1056 }
1057
1058 return rv;
1059}