blob: 5b0d6b2453da541e40137d1a20a665e4b98645e7 [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 <assert.h>
Bernhard Reutner-Fischerfa939aa2006-04-05 16:21:37 +000023
Eric Andersen9615a082004-07-15 12:53:49 +000024#ifndef _PATH_SHADOW
25#define _PATH_SHADOW "/etc/shadow"
26#endif
27#ifndef _PATH_PASSWD
28#define _PATH_PASSWD "/etc/passwd"
29#endif
30#ifndef _PATH_GROUP
31#define _PATH_GROUP "/etc/group"
32#endif
33
34/**********************************************************************/
Rob Landley06ec8cf2006-03-03 19:02:50 +000035/* Sizes for statically allocated buffers. */
Eric Andersen9615a082004-07-15 12:53:49 +000036
37/* If you change these values, also change _SC_GETPW_R_SIZE_MAX and
38 * _SC_GETGR_R_SIZE_MAX in libc/unistd/sysconf.c to match */
39#define PWD_BUFFER_SIZE 256
40#define GRP_BUFFER_SIZE 256
41
42/**********************************************************************/
43/* Prototypes for internal functions. */
44
Denis Vlasenkocb04ff52006-12-30 21:11:57 +000045static int bb__pgsreader(int (*parserfunc)(void *d, char *line), void *data,
Denis Vlasenko7d219aa2006-10-05 10:17:08 +000046 char *__restrict line_buff, size_t buflen, FILE *f);
Eric Andersen9615a082004-07-15 12:53:49 +000047
Denis Vlasenkocb04ff52006-12-30 21:11:57 +000048static int bb__parsepwent(void *pw, char *line);
49static int bb__parsegrent(void *gr, char *line);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +000050#if ENABLE_USE_BB_SHADOW
Denis Vlasenkocb04ff52006-12-30 21:11:57 +000051static int bb__parsespent(void *sp, char *line);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +000052#endif
53
Eric Andersen9615a082004-07-15 12:53:49 +000054/**********************************************************************/
Denis Vlasenko2c91efb2007-06-18 10:08:27 +000055/* We avoid having big global data. */
56
57struct statics {
58 /* Smaller things first */
59 struct passwd getpwuid_resultbuf;
60 struct group getgrgid_resultbuf;
61 struct passwd getpwnam_resultbuf;
62 struct group getgrnam_resultbuf;
63
64 char getpwuid_buffer[PWD_BUFFER_SIZE];
65 char getgrgid_buffer[GRP_BUFFER_SIZE];
66 char getpwnam_buffer[PWD_BUFFER_SIZE];
67 char getgrnam_buffer[GRP_BUFFER_SIZE];
68#if 0
69 struct passwd fgetpwent_resultbuf;
70 struct group fgetgrent_resultbuf;
71 struct spwd fgetspent_resultbuf;
72 char fgetpwent_buffer[PWD_BUFFER_SIZE];
73 char fgetgrent_buffer[GRP_BUFFER_SIZE];
74 char fgetspent_buffer[PWD_BUFFER_SIZE];
75#endif
76#if 0 //ENABLE_USE_BB_SHADOW
77 struct spwd getspuid_resultbuf;
78 struct spwd getspnam_resultbuf;
79 char getspuid_buffer[PWD_BUFFER_SIZE];
80 char getspnam_buffer[PWD_BUFFER_SIZE];
81#endif
82// Not converted - too small to bother
83//pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
84//FILE *pwf /*= NULL*/;
85//FILE *grf /*= NULL*/;
86//FILE *spf /*= NULL*/;
87#if 0
88 struct passwd getpwent_pwd;
89 struct group getgrent_gr;
90 char getpwent_line_buff[PWD_BUFFER_SIZE];
91 char getgrent_line_buff[GRP_BUFFER_SIZE];
92#endif
93#if 0 //ENABLE_USE_BB_SHADOW
94 struct spwd getspent_spwd;
95 struct spwd sgetspent_spwd;
96 char getspent_line_buff[PWD_BUFFER_SIZE];
97 char sgetspent_line_buff[PWD_BUFFER_SIZE];
98#endif
99};
100
101static struct statics *ptr_to_statics;
102
103static struct statics *get_S(void)
104{
105 if (!ptr_to_statics)
106 ptr_to_statics = xzalloc(sizeof(*ptr_to_statics));
107 return ptr_to_statics;
108}
109
110/* Always use in this order, get_S() must be called first */
111#define RESULTBUF(name) &((S = get_S())->name##_resultbuf)
112#define BUFFER(name) (S->name##_buffer)
113
114/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000115/* For the various fget??ent_r funcs, return
116 *
117 * 0: success
118 * ENOENT: end-of-file encountered
119 * ERANGE: buflen too small
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000120 * other error values possible. See bb__pgsreader.
Eric Andersen9615a082004-07-15 12:53:49 +0000121 *
122 * Also, *result == resultbuf on success and NULL on failure.
123 *
124 * NOTE: glibc difference - For the ENOENT case, glibc also sets errno.
125 * We do not, as it really isn't an error if we reach the end-of-file.
126 * Doing so is analogous to having fgetc() set errno on EOF.
127 */
128/**********************************************************************/
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000129
Eric Andersen9615a082004-07-15 12:53:49 +0000130int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf,
131 char *__restrict buffer, size_t buflen,
132 struct passwd **__restrict result)
133{
134 int rv;
135
136 *result = NULL;
137
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000138 rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, stream);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000139 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000140 *result = resultbuf;
141 }
142
143 return rv;
144}
145
Eric Andersen9615a082004-07-15 12:53:49 +0000146int fgetgrent_r(FILE *__restrict stream, struct group *__restrict resultbuf,
147 char *__restrict buffer, size_t buflen,
148 struct group **__restrict result)
149{
150 int rv;
151
152 *result = NULL;
153
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000154 rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, stream);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000155 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000156 *result = resultbuf;
157 }
158
159 return rv;
160}
161
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000162#if ENABLE_USE_BB_SHADOW
Eric Andersen9615a082004-07-15 12:53:49 +0000163int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
164 char *__restrict buffer, size_t buflen,
165 struct spwd **__restrict result)
166{
167 int rv;
168
169 *result = NULL;
170
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000171 rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, stream);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000172 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000173 *result = resultbuf;
174 }
175
176 return rv;
177}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000178#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000179
Eric Andersen9615a082004-07-15 12:53:49 +0000180/**********************************************************************/
181/* For the various fget??ent funcs, return NULL on failure and a
Rob Landley06ec8cf2006-03-03 19:02:50 +0000182 * pointer to the appropriate struct (statically allocated) on success.
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000183 * TODO: audit & stop using these in bbox, they pull in static buffers */
Eric Andersen9615a082004-07-15 12:53:49 +0000184/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000185
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000186#if 0
Eric Andersen9615a082004-07-15 12:53:49 +0000187struct passwd *fgetpwent(FILE *stream)
188{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000189 struct statics *S;
190 struct passwd *resultbuf = RESULTBUF(fgetpwent);
191 char *buffer = BUFFER(fgetpwent);
Eric Andersen9615a082004-07-15 12:53:49 +0000192 struct passwd *result;
193
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000194 fgetpwent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetpwent)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000195 return result;
196}
197
Eric Andersen9615a082004-07-15 12:53:49 +0000198struct group *fgetgrent(FILE *stream)
199{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000200 struct statics *S;
201 struct group *resultbuf = RESULTBUF(fgetgrent);
202 char *buffer = BUFFER(fgetgrent);
Eric Andersen9615a082004-07-15 12:53:49 +0000203 struct group *result;
204
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000205 fgetgrent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetgrent)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000206 return result;
207}
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000208#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000209
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000210#if ENABLE_USE_BB_SHADOW
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000211#if 0
Eric Andersen9615a082004-07-15 12:53:49 +0000212struct spwd *fgetspent(FILE *stream)
213{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000214 struct statics *S;
215 struct spwd *resultbuf = RESULTBUF(fgetspent);
216 char *buffer = BUFFER(fgetspent);
Eric Andersen9615a082004-07-15 12:53:49 +0000217 struct spwd *result;
218
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000219 fgetspent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetspent)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000220 return result;
221}
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000222#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000223
Eric Andersen9615a082004-07-15 12:53:49 +0000224int sgetspent_r(const char *string, struct spwd *result_buf,
225 char *buffer, size_t buflen, struct spwd **result)
226{
227 int rv = ERANGE;
228
229 *result = NULL;
230
231 if (buflen < PWD_BUFFER_SIZE) {
232 DO_ERANGE:
233 errno=rv;
234 goto DONE;
235 }
236
237 if (string != buffer) {
238 if (strlen(string) >= buflen) {
239 goto DO_ERANGE;
240 }
241 strcpy(buffer, string);
242 }
243
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000244 rv = bb__parsespent(result_buf, buffer);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000245 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000246 *result = result_buf;
247 }
248
249 DONE:
250 return rv;
251}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000252#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000253
Eric Andersen9615a082004-07-15 12:53:49 +0000254/**********************************************************************/
255
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000256#define GETXXKEY_R_FUNC getpwnam_r
257#define GETXXKEY_R_PARSER bb__parsepwent
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000258#define GETXXKEY_R_ENTTYPE struct passwd
259#define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key))
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000260#define GETXXKEY_R_KEYTYPE const char *__restrict
261#define GETXXKEY_R_PATHNAME _PATH_PASSWD
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000262#include "pwd_grp_internal.c"
Eric Andersen9615a082004-07-15 12:53:49 +0000263
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000264#define GETXXKEY_R_FUNC getgrnam_r
265#define GETXXKEY_R_PARSER bb__parsegrent
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000266#define GETXXKEY_R_ENTTYPE struct group
267#define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key))
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000268#define GETXXKEY_R_KEYTYPE const char *__restrict
269#define GETXXKEY_R_PATHNAME _PATH_GROUP
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000270#include "pwd_grp_internal.c"
Eric Andersen9615a082004-07-15 12:53:49 +0000271
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000272#if ENABLE_USE_BB_SHADOW
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000273#define GETXXKEY_R_FUNC getspnam_r
274#define GETXXKEY_R_PARSER bb__parsespent
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000275#define GETXXKEY_R_ENTTYPE struct spwd
276#define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key))
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000277#define GETXXKEY_R_KEYTYPE const char *__restrict
278#define GETXXKEY_R_PATHNAME _PATH_SHADOW
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000279#include "pwd_grp_internal.c"
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000280#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000281
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000282#define GETXXKEY_R_FUNC getpwuid_r
283#define GETXXKEY_R_PARSER bb__parsepwent
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000284#define GETXXKEY_R_ENTTYPE struct passwd
285#define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key)
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000286#define GETXXKEY_R_KEYTYPE uid_t
287#define GETXXKEY_R_PATHNAME _PATH_PASSWD
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000288#include "pwd_grp_internal.c"
Eric Andersen9615a082004-07-15 12:53:49 +0000289
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000290#define GETXXKEY_R_FUNC getgrgid_r
291#define GETXXKEY_R_PARSER bb__parsegrent
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000292#define GETXXKEY_R_ENTTYPE struct group
293#define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key)
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000294#define GETXXKEY_R_KEYTYPE gid_t
295#define GETXXKEY_R_PATHNAME _PATH_GROUP
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000296#include "pwd_grp_internal.c"
Eric Andersen9615a082004-07-15 12:53:49 +0000297
298/**********************************************************************/
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000299/* TODO: audit & stop using these in bbox, they pull in static buffers */
Eric Andersen9615a082004-07-15 12:53:49 +0000300
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000301/* This one has many users */
Eric Andersen9615a082004-07-15 12:53:49 +0000302struct passwd *getpwuid(uid_t uid)
303{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000304 struct statics *S;
305 struct passwd *resultbuf = RESULTBUF(getpwuid);
306 char *buffer = BUFFER(getpwuid);
Eric Andersen9615a082004-07-15 12:53:49 +0000307 struct passwd *result;
308
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000309 getpwuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getpwuid)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000310 return result;
311}
312
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000313/* This one has many users */
Eric Andersen9615a082004-07-15 12:53:49 +0000314struct group *getgrgid(gid_t gid)
315{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000316 struct statics *S;
317 struct group *resultbuf = RESULTBUF(getgrgid);
318 char *buffer = BUFFER(getgrgid);
Eric Andersen9615a082004-07-15 12:53:49 +0000319 struct group *result;
320
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000321 getgrgid_r(gid, resultbuf, buffer, sizeof(BUFFER(getgrgid)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000322 return result;
323}
324
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000325#if 0 //ENABLE_USE_BB_SHADOW
Eric Andersen9615a082004-07-15 12:53:49 +0000326/* This function is non-standard and is currently not built. It seems
327 * to have been created as a reentrant version of the non-standard
328 * functions getspuid. Why getspuid was added, I do not know. */
Eric Andersen9615a082004-07-15 12:53:49 +0000329int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf,
330 char *__restrict buffer, size_t buflen,
331 struct spwd **__restrict result)
332{
333 int rv;
334 struct passwd *pp;
335 struct passwd password;
336 char pwd_buff[PWD_BUFFER_SIZE];
337
338 *result = NULL;
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000339 rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp);
340 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000341 rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result);
342 }
343
344 return rv;
345}
346
Eric Andersen9615a082004-07-15 12:53:49 +0000347/* This function is non-standard and is currently not built.
348 * Why it was added, I do not know. */
Eric Andersen9615a082004-07-15 12:53:49 +0000349struct spwd *getspuid(uid_t uid)
350{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000351 struct statics *S;
352 struct spwd *resultbuf = RESULTBUF(getspuid);
353 char *buffer = BUFFER(getspuid);
Eric Andersen9615a082004-07-15 12:53:49 +0000354 struct spwd *result;
355
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000356 getspuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getspuid)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000357 return result;
358}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000359#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000360
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000361/* This one has many users */
Eric Andersen9615a082004-07-15 12:53:49 +0000362struct passwd *getpwnam(const char *name)
363{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000364 struct statics *S;
365 struct passwd *resultbuf = RESULTBUF(getpwnam);
366 char *buffer = BUFFER(getpwnam);
Eric Andersen9615a082004-07-15 12:53:49 +0000367 struct passwd *result;
368
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000369 getpwnam_r(name, resultbuf, buffer, sizeof(BUFFER(getpwnam)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000370 return result;
371}
372
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000373/* This one has many users */
Eric Andersen9615a082004-07-15 12:53:49 +0000374struct group *getgrnam(const char *name)
375{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000376 struct statics *S;
377 struct group *resultbuf = RESULTBUF(getgrnam);
378 char *buffer = BUFFER(getgrnam);
Eric Andersen9615a082004-07-15 12:53:49 +0000379 struct group *result;
380
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000381 getgrnam_r(name, resultbuf, buffer, sizeof(BUFFER(getgrnam)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000382 return result;
383}
384
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000385#if 0 //ENABLE_USE_BB_SHADOW
Eric Andersen9615a082004-07-15 12:53:49 +0000386struct spwd *getspnam(const char *name)
387{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000388 struct statics *S;
389 struct spwd *resultbuf = RESULTBUF(getspnam);
390 char *buffer = BUFFER(getspnam);
Eric Andersen9615a082004-07-15 12:53:49 +0000391 struct spwd *result;
392
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000393 getspnam_r(name, resultbuf, buffer, sizeof(BUFFER(getspnam)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000394 return result;
395}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000396#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000397
Denis Vlasenkoee5dce32008-09-26 10:35:55 +0000398#ifdef THIS_ONE_IS_UNUSED
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}
Denis Vlasenkoee5dce32008-09-26 10:35:55 +0000422#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000423
Eric Andersen9615a082004-07-15 12:53:49 +0000424/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000425
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000426/* FIXME: we don't have such CONFIG_xx - ?! */
427
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000428#if defined CONFIG_USE_BB_THREADSAFE_SHADOW && defined PTHREAD_MUTEX_INITIALIZER
429static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
430# define LOCK pthread_mutex_lock(&mylock)
431# define UNLOCK pthread_mutex_unlock(&mylock);
432#else
433# define LOCK ((void) 0)
434# define UNLOCK ((void) 0)
435#endif
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000436
Eric Andersen9615a082004-07-15 12:53:49 +0000437static FILE *pwf /*= NULL*/;
438void setpwent(void)
439{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000440 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000441 if (pwf) {
442 rewind(pwf);
443 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000444 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000445}
446
447void endpwent(void)
448{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000449 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000450 if (pwf) {
451 fclose(pwf);
452 pwf = NULL;
453 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000454 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000455}
456
457
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000458int getpwent_r(struct passwd *__restrict resultbuf,
Eric Andersen9615a082004-07-15 12:53:49 +0000459 char *__restrict buffer, size_t buflen,
460 struct passwd **__restrict result)
461{
462 int rv;
463
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000464 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000465 *result = NULL; /* In case of error... */
466
467 if (!pwf) {
Denis Vlasenko5415c852008-07-21 23:05:26 +0000468 pwf = fopen_for_read(_PATH_PASSWD);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000469 if (!pwf) {
Eric Andersen9615a082004-07-15 12:53:49 +0000470 rv = errno;
471 goto ERR;
472 }
473 }
474
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000475 rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, pwf);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000476 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000477 *result = resultbuf;
478 }
479
480 ERR:
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000481 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000482 return rv;
483}
484
Eric Andersen9615a082004-07-15 12:53:49 +0000485static FILE *grf /*= NULL*/;
486void setgrent(void)
487{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000488 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000489 if (grf) {
490 rewind(grf);
491 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000492 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000493}
494
495void endgrent(void)
496{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000497 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000498 if (grf) {
499 fclose(grf);
500 grf = NULL;
501 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000502 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000503}
504
505int getgrent_r(struct group *__restrict resultbuf,
506 char *__restrict buffer, size_t buflen,
507 struct group **__restrict result)
508{
509 int rv;
510
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000511 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000512 *result = NULL; /* In case of error... */
513
514 if (!grf) {
Denis Vlasenko5415c852008-07-21 23:05:26 +0000515 grf = fopen_for_read(_PATH_GROUP);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000516 if (!grf) {
Eric Andersen9615a082004-07-15 12:53:49 +0000517 rv = errno;
518 goto ERR;
519 }
520 }
521
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000522 rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, grf);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000523 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000524 *result = resultbuf;
525 }
526
527 ERR:
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000528 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000529 return rv;
530}
531
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000532#if ENABLE_USE_BB_SHADOW
Eric Andersen9615a082004-07-15 12:53:49 +0000533static FILE *spf /*= NULL*/;
534void setspent(void)
535{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000536 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000537 if (spf) {
538 rewind(spf);
539 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000540 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000541}
542
543void endspent(void)
544{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000545 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000546 if (spf) {
547 fclose(spf);
548 spf = NULL;
549 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000550 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000551}
552
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000553int getspent_r(struct spwd *resultbuf, char *buffer,
Eric Andersen9615a082004-07-15 12:53:49 +0000554 size_t buflen, struct spwd **result)
555{
556 int rv;
557
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000558 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000559 *result = NULL; /* In case of error... */
560
561 if (!spf) {
Denis Vlasenko5415c852008-07-21 23:05:26 +0000562 spf = fopen_for_read(_PATH_SHADOW);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000563 if (!spf) {
Eric Andersen9615a082004-07-15 12:53:49 +0000564 rv = errno;
565 goto ERR;
566 }
567 }
568
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000569 rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, spf);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000570 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000571 *result = resultbuf;
572 }
573
574 ERR:
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000575 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000576 return rv;
577}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000578#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000579
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000580#if 0
Eric Andersen9615a082004-07-15 12:53:49 +0000581struct passwd *getpwent(void)
582{
583 static char line_buff[PWD_BUFFER_SIZE];
584 static struct passwd pwd;
585 struct passwd *result;
586
587 getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
588 return result;
589}
590
Eric Andersen9615a082004-07-15 12:53:49 +0000591struct group *getgrent(void)
592{
593 static char line_buff[GRP_BUFFER_SIZE];
594 static struct group gr;
595 struct group *result;
596
597 getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
598 return result;
599}
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000600#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000601
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000602#if 0 //ENABLE_USE_BB_SHADOW
Eric Andersen9615a082004-07-15 12:53:49 +0000603struct spwd *getspent(void)
604{
605 static char line_buff[PWD_BUFFER_SIZE];
606 static struct spwd spwd;
607 struct spwd *result;
608
609 getspent_r(&spwd, line_buff, sizeof(line_buff), &result);
610 return result;
611}
612
Eric Andersen9615a082004-07-15 12:53:49 +0000613struct spwd *sgetspent(const char *string)
614{
615 static char line_buff[PWD_BUFFER_SIZE];
616 static struct spwd spwd;
617 struct spwd *result;
618
619 sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result);
620 return result;
621}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000622#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000623
Denis Vlasenko22284262008-09-18 00:56:24 +0000624static gid_t *getgrouplist_internal(int *ngroups_ptr, const char *user, gid_t gid)
Eric Andersen9615a082004-07-15 12:53:49 +0000625{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000626 FILE *grfile;
Eric Andersen9615a082004-07-15 12:53:49 +0000627 gid_t *group_list;
Denis Vlasenko22284262008-09-18 00:56:24 +0000628 int ngroups;
Eric Andersen9615a082004-07-15 12:53:49 +0000629 struct group group;
630 char buff[PWD_BUFFER_SIZE];
631
Denis Vlasenko22284262008-09-18 00:56:24 +0000632 /* We alloc space for 8 gids at a time. */
633 group_list = xmalloc(8 * sizeof(group_list[0]));
634 group_list[0] = gid;
635 ngroups = 1;
636
Denis Vlasenko5415c852008-07-21 23:05:26 +0000637 grfile = fopen_for_read(_PATH_GROUP);
Denis Vlasenko22284262008-09-18 00:56:24 +0000638 if (grfile) {
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000639 while (!bb__pgsreader(bb__parsegrent, &group, buff, sizeof(buff), grfile)) {
Denis Vlasenko22284262008-09-18 00:56:24 +0000640 char **m;
Eric Andersen9615a082004-07-15 12:53:49 +0000641 assert(group.gr_mem); /* Must have at least a NULL terminator. */
Denis Vlasenko22284262008-09-18 00:56:24 +0000642 if (group.gr_gid == gid)
643 continue;
644 for (m = group.gr_mem; *m; m++) {
645 if (strcmp(*m, user) != 0)
646 continue;
647 group_list = xrealloc_vector(group_list, 3, ngroups);
648 group_list[ngroups++] = group.gr_gid;
649 break;
Eric Andersen9615a082004-07-15 12:53:49 +0000650 }
651 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000652 fclose(grfile);
Eric Andersen9615a082004-07-15 12:53:49 +0000653 }
Denis Vlasenko22284262008-09-18 00:56:24 +0000654 *ngroups_ptr = ngroups;
655 return group_list;
656}
Eric Andersen9615a082004-07-15 12:53:49 +0000657
Denis Vlasenko22284262008-09-18 00:56:24 +0000658int initgroups(const char *user, gid_t gid)
659{
660 int ngroups;
661 gid_t *group_list = getgrouplist_internal(&ngroups, user, gid);
662
Denis Vlasenko22284262008-09-18 00:56:24 +0000663 ngroups = setgroups(ngroups, group_list);
664 free(group_list);
665 return ngroups;
666}
667
Denis Vlasenko22284262008-09-18 00:56:24 +0000668int getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups)
669{
670 int ngroups_old = *ngroups;
671 gid_t *group_list = getgrouplist_internal(ngroups, user, gid);
672
673 if (*ngroups <= ngroups_old) {
674 ngroups_old = *ngroups;
675 memcpy(groups, group_list, ngroups_old * sizeof(groups[0]));
676 } else {
677 ngroups_old = -1;
678 }
679 free(group_list);
680 return ngroups_old;
Eric Andersen9615a082004-07-15 12:53:49 +0000681}
682
Eric Andersen9615a082004-07-15 12:53:49 +0000683int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
684{
685 int rv = -1;
686
687 if (!p || !f) {
Denis Vlasenko92305822008-03-20 15:12:58 +0000688 errno = EINVAL;
Eric Andersen9615a082004-07-15 12:53:49 +0000689 } else {
690 /* No extra thread locking is needed above what fprintf does. */
691 if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n",
692 p->pw_name, p->pw_passwd,
693 (unsigned long)(p->pw_uid),
694 (unsigned long)(p->pw_gid),
695 p->pw_gecos, p->pw_dir, p->pw_shell) >= 0
696 ) {
697 rv = 0;
698 }
699 }
700
701 return rv;
702}
703
Eric Andersen9615a082004-07-15 12:53:49 +0000704int putgrent(const struct group *__restrict p, FILE *__restrict f)
705{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000706 static const char format[] ALIGN1 = ",%s";
707
Eric Andersen9615a082004-07-15 12:53:49 +0000708 char **m;
709 const char *fmt;
710 int rv = -1;
711
712 if (!p || !f) { /* Sigh... glibc checks. */
Denis Vlasenko92305822008-03-20 15:12:58 +0000713 errno = EINVAL;
Eric Andersen9615a082004-07-15 12:53:49 +0000714 } else {
715 if (fprintf(f, "%s:%s:%lu:",
716 p->gr_name, p->gr_passwd,
717 (unsigned long)(p->gr_gid)) >= 0
718 ) {
719
720 fmt = format + 1;
721
722 assert(p->gr_mem);
723 m = p->gr_mem;
724
725 do {
726 if (!*m) {
Rob Landley25413bf2005-10-08 02:23:22 +0000727 if (fputc('\n', f) >= 0) {
Eric Andersen9615a082004-07-15 12:53:49 +0000728 rv = 0;
729 }
730 break;
731 }
732 if (fprintf(f, fmt, *m) < 0) {
733 break;
734 }
735 ++m;
736 fmt = format;
737 } while (1);
738
739 }
740
741 }
742
743 return rv;
744}
745
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000746#if ENABLE_USE_BB_SHADOW
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000747static const unsigned char _sp_off[] ALIGN1 = {
748 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
749 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
750 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
751 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
752 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
753 offsetof(struct spwd, sp_expire) /* 7 - not a char ptr */
Eric Andersen9615a082004-07-15 12:53:49 +0000754};
755
756int putspent(const struct spwd *p, FILE *stream)
757{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000758 static const char ld_format[] ALIGN1 = "%ld:";
759
Eric Andersen9615a082004-07-15 12:53:49 +0000760 const char *f;
Denis Vlasenko87468852007-04-13 23:22:00 +0000761 long x;
Eric Andersen9615a082004-07-15 12:53:49 +0000762 int i;
763 int rv = -1;
764
765 /* Unlike putpwent and putgrent, glibc does not check the args. */
766 if (fprintf(stream, "%s:%s:", p->sp_namp,
767 (p->sp_pwdp ? p->sp_pwdp : "")) < 0
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000768 ) {
Eric Andersen9615a082004-07-15 12:53:49 +0000769 goto DO_UNLOCK;
770 }
771
Denis Vlasenkob71c6682007-07-21 15:08:09 +0000772 for (i = 0; i < sizeof(_sp_off); i++) {
Eric Andersen9615a082004-07-15 12:53:49 +0000773 f = ld_format;
Denis Vlasenko87468852007-04-13 23:22:00 +0000774 x = *(const long *)(((const char *) p) + _sp_off[i]);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000775 if (x == -1) {
Eric Andersen9615a082004-07-15 12:53:49 +0000776 f += 3;
777 }
778 if (fprintf(stream, f, x) < 0) {
779 goto DO_UNLOCK;
780 }
781 }
782
783 if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) {
784 goto DO_UNLOCK;
785 }
786
Rob Landley25413bf2005-10-08 02:23:22 +0000787 if (fputc('\n', stream) > 0) {
Eric Andersen9615a082004-07-15 12:53:49 +0000788 rv = 0;
789 }
790
791DO_UNLOCK:
792 return rv;
793}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000794#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000795
Eric Andersen9615a082004-07-15 12:53:49 +0000796/**********************************************************************/
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000797/* Internal uClibc functions. */
Eric Andersen9615a082004-07-15 12:53:49 +0000798/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000799
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000800static const unsigned char pw_off[] ALIGN1 = {
801 offsetof(struct passwd, pw_name), /* 0 */
802 offsetof(struct passwd, pw_passwd), /* 1 */
803 offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */
804 offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */
805 offsetof(struct passwd, pw_gecos), /* 4 */
806 offsetof(struct passwd, pw_dir), /* 5 */
807 offsetof(struct passwd, pw_shell) /* 6 */
Eric Andersen9615a082004-07-15 12:53:49 +0000808};
809
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000810static int bb__parsepwent(void *data, char *line)
Eric Andersen9615a082004-07-15 12:53:49 +0000811{
812 char *endptr;
813 char *p;
814 int i;
815
816 i = 0;
817 do {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200818 p = (char *) data + pw_off[i];
Eric Andersen9615a082004-07-15 12:53:49 +0000819
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000820 if ((i & 6) ^ 2) { /* i!=2 and i!=3 */
Eric Andersen9615a082004-07-15 12:53:49 +0000821 *((char **) p) = line;
822 if (i==6) {
823 return 0;
824 }
825 /* NOTE: glibc difference - glibc allows omission of
826 * ':' seperators after the gid field if all remaining
827 * entries are empty. We require all separators. */
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000828 line = strchr(line, ':');
829 if (!line) {
Eric Andersen9615a082004-07-15 12:53:49 +0000830 break;
831 }
832 } else {
833 unsigned long t = strtoul(line, &endptr, 10);
834 /* Make sure we had at least one digit, and that the
835 * failing char is the next field seperator ':'. See
836 * glibc difference note above. */
837 /* TODO: Also check for leading whitespace? */
838 if ((endptr == line) || (*endptr != ':')) {
839 break;
840 }
841 line = endptr;
842 if (i & 1) { /* i == 3 -- gid */
843 *((gid_t *) p) = t;
844 } else { /* i == 2 -- uid */
845 *((uid_t *) p) = t;
846 }
847 }
848
849 *line++ = 0;
850 ++i;
851 } while (1);
852
853 return -1;
854}
855
Eric Andersen9615a082004-07-15 12:53:49 +0000856/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000857
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000858static const unsigned char gr_off[] ALIGN1 = {
859 offsetof(struct group, gr_name), /* 0 */
860 offsetof(struct group, gr_passwd), /* 1 */
861 offsetof(struct group, gr_gid) /* 2 - not a char ptr */
Eric Andersen9615a082004-07-15 12:53:49 +0000862};
863
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000864static int bb__parsegrent(void *data, char *line)
Eric Andersen9615a082004-07-15 12:53:49 +0000865{
866 char *endptr;
867 char *p;
868 int i;
869 char **members;
870 char *end_of_buf;
871
872 end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */
873 i = 0;
874 do {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200875 p = (char *) data + gr_off[i];
Eric Andersen9615a082004-07-15 12:53:49 +0000876
877 if (i < 2) {
878 *((char **) p) = line;
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000879 line = strchr(line, ':');
880 if (!line) {
Eric Andersen9615a082004-07-15 12:53:49 +0000881 break;
882 }
883 *line++ = 0;
884 ++i;
885 } else {
886 *((gid_t *) p) = strtoul(line, &endptr, 10);
887
888 /* NOTE: glibc difference - glibc allows omission of the
889 * trailing colon when there is no member list. We treat
890 * this as an error. */
891
892 /* Make sure we had at least one digit, and that the
893 * failing char is the next field seperator ':'. See
894 * glibc difference note above. */
895 if ((endptr == line) || (*endptr != ':')) {
896 break;
897 }
898
899 i = 1; /* Count terminating NULL ptr. */
900 p = endptr;
901
902 if (p[1]) { /* We have a member list to process. */
903 /* Overwrite the last ':' with a ',' before counting.
904 * This allows us to test for initial ',' and adds
905 * one ',' so that the ',' count equals the member
906 * count. */
907 *p = ',';
908 do {
909 /* NOTE: glibc difference - glibc allows and trims leading
910 * (but not trailing) space. We treat this as an error. */
911 /* NOTE: glibc difference - glibc allows consecutive and
912 * trailing commas, and ignores "empty string" users. We
913 * treat this as an error. */
914 if (*p == ',') {
915 ++i;
916 *p = 0; /* nul-terminate each member string. */
917 if (!*++p || (*p == ',') || isspace(*p)) {
918 goto ERR;
919 }
920 }
921 } while (*++p);
922 }
923
924 /* Now align (p+1), rounding up. */
925 /* Assumes sizeof(char **) is a power of 2. */
926 members = (char **)( (((intptr_t) p) + sizeof(char **))
927 & ~((intptr_t)(sizeof(char **) - 1)) );
928
929 if (((char *)(members + i)) > end_of_buf) { /* No space. */
930 break;
931 }
932
933 ((struct group *) data)->gr_mem = members;
934
935 if (--i) {
936 p = endptr; /* Pointing to char prior to first member. */
937 do {
938 *members++ = ++p;
939 if (!--i) break;
940 while (*++p) {}
941 } while (1);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000942 }
Eric Andersen9615a082004-07-15 12:53:49 +0000943 *members = NULL;
944
945 return 0;
946 }
947 } while (1);
948
949 ERR:
950 return -1;
951}
952
Eric Andersen9615a082004-07-15 12:53:49 +0000953/**********************************************************************/
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000954
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000955#if ENABLE_USE_BB_SHADOW
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000956static const unsigned char sp_off[] ALIGN1 = {
957 offsetof(struct spwd, sp_namp), /* 0 */
958 offsetof(struct spwd, sp_pwdp), /* 1 */
959 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
960 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
961 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
962 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
963 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
964 offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */
965 offsetof(struct spwd, sp_flag) /* 8 - not a char ptr */
Eric Andersen9615a082004-07-15 12:53:49 +0000966};
967
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200968static int bb__parsespent(void *data, char *line)
Eric Andersen9615a082004-07-15 12:53:49 +0000969{
970 char *endptr;
971 char *p;
972 int i;
973
974 i = 0;
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200975 while (1) {
976 p = (char *) data + sp_off[i];
Eric Andersen9615a082004-07-15 12:53:49 +0000977 if (i < 2) {
978 *((char **) p) = line;
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000979 line = strchr(line, ':');
980 if (!line) {
Eric Andersen9615a082004-07-15 12:53:49 +0000981 break;
982 }
983 } else {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200984 *((long *) p) = strtoul(line, &endptr, 10);
Eric Andersen9615a082004-07-15 12:53:49 +0000985
986 if (endptr == line) {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200987 *((long *) p) = (i != 8) ? -1L : (long)(~0UL);
Eric Andersen9615a082004-07-15 12:53:49 +0000988 }
989
990 line = endptr;
991
992 if (i == 8) {
993 if (!*endptr) {
994 return 0;
995 }
996 break;
997 }
998
999 if (*endptr != ':') {
1000 break;
1001 }
Eric Andersen9615a082004-07-15 12:53:49 +00001002 }
1003
Denys Vlasenko1f27ab02009-09-23 17:17:53 +02001004 *line++ = '\0';
Eric Andersen9615a082004-07-15 12:53:49 +00001005 ++i;
Denys Vlasenko1f27ab02009-09-23 17:17:53 +02001006 }
Eric Andersen9615a082004-07-15 12:53:49 +00001007
1008 return EINVAL;
1009}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +00001010#endif
Eric Andersen9615a082004-07-15 12:53:49 +00001011
Eric Andersen9615a082004-07-15 12:53:49 +00001012/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +00001013
1014/* Reads until if EOF, or until if finds a line which fits in the buffer
1015 * and for which the parser function succeeds.
1016 *
1017 * Returns 0 on success and ENOENT for end-of-file (glibc concession).
1018 */
1019
Denis Vlasenkocb04ff52006-12-30 21:11:57 +00001020static int bb__pgsreader(int (*parserfunc)(void *d, char *line), void *data,
Eric Andersen9615a082004-07-15 12:53:49 +00001021 char *__restrict line_buff, size_t buflen, FILE *f)
1022{
Eric Andersen9615a082004-07-15 12:53:49 +00001023 int skip;
1024 int rv = ERANGE;
1025
1026 if (buflen < PWD_BUFFER_SIZE) {
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +00001027 errno = rv;
Denys Vlasenko57dc3c72010-03-31 10:24:37 +02001028 return rv;
1029 }
1030
1031 skip = 0;
1032 while (1) {
1033 if (!fgets(line_buff, buflen, f)) {
1034 if (feof(f)) {
1035 rv = ENOENT;
1036 }
1037 break;
1038 }
1039
1040 {
1041 int line_len = strlen(line_buff) - 1;
1042 if (line_len >= 0 && line_buff[line_len] == '\n') {
1043 line_buff[line_len] = '\0';
1044 } else
1045 if (line_len + 2 == buflen) {
1046 /* A start (or continuation) of overlong line */
1047 skip = 1;
1048 continue;
1049 } /* else: a last line in the file, and it has no '\n' */
1050 }
1051
1052 if (skip) {
1053 /* This "line" is a remainder of overlong line, ignore */
1054 skip = 0;
1055 continue;
1056 }
1057
1058 /* NOTE: glibc difference - glibc strips leading whitespace from
1059 * records. We do not allow leading whitespace. */
1060
1061 /* Skip empty lines, comment lines, and lines with leading
1062 * whitespace. */
1063 if (line_buff[0] != '\0' && line_buff[0] != '#' && !isspace(line_buff[0])) {
1064 if (parserfunc == bb__parsegrent) {
1065 /* Do evil group hack:
1066 * The group entry parsing function needs to know where
1067 * the end of the buffer is so that it can construct the
1068 * group member ptr table. */
1069 ((struct group *) data)->gr_name = line_buff + buflen;
1070 }
1071 if (parserfunc(data, line_buff) == 0) {
1072 rv = 0;
Eric Andersen9615a082004-07-15 12:53:49 +00001073 break;
1074 }
Denys Vlasenko57dc3c72010-03-31 10:24:37 +02001075 }
1076 } /* while (1) */
Eric Andersen9615a082004-07-15 12:53:49 +00001077
1078 return rv;
1079}