blob: 06f3607a0d022ef55e434bc1889fc1947571119d [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02002/* Copyright (C) 2003 Manuel Novoa III
Eric Andersen9615a082004-07-15 12:53:49 +00003 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02004 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen9615a082004-07-15 12:53:49 +00005 */
6
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007/* Nov 6, 2003 Initial version.
Eric Andersen9615a082004-07-15 12:53:49 +00008 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * NOTE: This implementation is quite strict about requiring all
Eric Andersen9615a082004-07-15 12:53:49 +000010 * 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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020014 * 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.
Eric Andersen9615a082004-07-15 12:53:49 +000018 */
19
Rob Landleyea224be2006-06-18 20:20:07 +000020#include "libbb.h"
Eric Andersen9615a082004-07-15 12:53:49 +000021#include <assert.h>
Bernhard Reutner-Fischerfa939aa2006-04-05 16:21:37 +000022
Eric Andersen9615a082004-07-15 12:53:49 +000023#ifndef _PATH_SHADOW
24#define _PATH_SHADOW "/etc/shadow"
25#endif
26#ifndef _PATH_PASSWD
27#define _PATH_PASSWD "/etc/passwd"
28#endif
29#ifndef _PATH_GROUP
30#define _PATH_GROUP "/etc/group"
31#endif
32
33/**********************************************************************/
Rob Landley06ec8cf2006-03-03 19:02:50 +000034/* Sizes for statically allocated buffers. */
Eric Andersen9615a082004-07-15 12:53:49 +000035
36/* If you change these values, also change _SC_GETPW_R_SIZE_MAX and
37 * _SC_GETGR_R_SIZE_MAX in libc/unistd/sysconf.c to match */
38#define PWD_BUFFER_SIZE 256
39#define GRP_BUFFER_SIZE 256
40
41/**********************************************************************/
42/* Prototypes for internal functions. */
43
Denys Vlasenko17fcd722010-03-31 12:37:43 +020044static int bb__pgsreader(
45 int FAST_FUNC (*parserfunc)(void *d, char *line),
46 void *data,
47 char *__restrict line_buff,
48 size_t buflen,
49 FILE *f);
Eric Andersen9615a082004-07-15 12:53:49 +000050
Denys Vlasenko17fcd722010-03-31 12:37:43 +020051static int FAST_FUNC bb__parsepwent(void *pw, char *line);
52static int FAST_FUNC bb__parsegrent(void *gr, char *line);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +000053#if ENABLE_USE_BB_SHADOW
Denys Vlasenko17fcd722010-03-31 12:37:43 +020054static int FAST_FUNC bb__parsespent(void *sp, char *line);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +000055#endif
56
Eric Andersen9615a082004-07-15 12:53:49 +000057/**********************************************************************/
Denis Vlasenko2c91efb2007-06-18 10:08:27 +000058/* We avoid having big global data. */
59
60struct statics {
61 /* Smaller things first */
62 struct passwd getpwuid_resultbuf;
63 struct group getgrgid_resultbuf;
64 struct passwd getpwnam_resultbuf;
65 struct group getgrnam_resultbuf;
66
67 char getpwuid_buffer[PWD_BUFFER_SIZE];
68 char getgrgid_buffer[GRP_BUFFER_SIZE];
69 char getpwnam_buffer[PWD_BUFFER_SIZE];
70 char getgrnam_buffer[GRP_BUFFER_SIZE];
71#if 0
72 struct passwd fgetpwent_resultbuf;
73 struct group fgetgrent_resultbuf;
74 struct spwd fgetspent_resultbuf;
75 char fgetpwent_buffer[PWD_BUFFER_SIZE];
76 char fgetgrent_buffer[GRP_BUFFER_SIZE];
77 char fgetspent_buffer[PWD_BUFFER_SIZE];
78#endif
79#if 0 //ENABLE_USE_BB_SHADOW
80 struct spwd getspuid_resultbuf;
81 struct spwd getspnam_resultbuf;
82 char getspuid_buffer[PWD_BUFFER_SIZE];
83 char getspnam_buffer[PWD_BUFFER_SIZE];
84#endif
85// Not converted - too small to bother
86//pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
87//FILE *pwf /*= NULL*/;
88//FILE *grf /*= NULL*/;
89//FILE *spf /*= NULL*/;
90#if 0
91 struct passwd getpwent_pwd;
92 struct group getgrent_gr;
93 char getpwent_line_buff[PWD_BUFFER_SIZE];
94 char getgrent_line_buff[GRP_BUFFER_SIZE];
95#endif
96#if 0 //ENABLE_USE_BB_SHADOW
97 struct spwd getspent_spwd;
98 struct spwd sgetspent_spwd;
99 char getspent_line_buff[PWD_BUFFER_SIZE];
100 char sgetspent_line_buff[PWD_BUFFER_SIZE];
101#endif
102};
103
104static struct statics *ptr_to_statics;
105
106static struct statics *get_S(void)
107{
108 if (!ptr_to_statics)
109 ptr_to_statics = xzalloc(sizeof(*ptr_to_statics));
110 return ptr_to_statics;
111}
112
113/* Always use in this order, get_S() must be called first */
114#define RESULTBUF(name) &((S = get_S())->name##_resultbuf)
115#define BUFFER(name) (S->name##_buffer)
116
117/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000118/* For the various fget??ent_r funcs, return
119 *
120 * 0: success
121 * ENOENT: end-of-file encountered
122 * ERANGE: buflen too small
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000123 * other error values possible. See bb__pgsreader.
Eric Andersen9615a082004-07-15 12:53:49 +0000124 *
125 * Also, *result == resultbuf on success and NULL on failure.
126 *
127 * NOTE: glibc difference - For the ENOENT case, glibc also sets errno.
128 * We do not, as it really isn't an error if we reach the end-of-file.
129 * Doing so is analogous to having fgetc() set errno on EOF.
130 */
131/**********************************************************************/
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000132
Eric Andersen9615a082004-07-15 12:53:49 +0000133int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf,
134 char *__restrict buffer, size_t buflen,
135 struct passwd **__restrict result)
136{
137 int rv;
138
139 *result = NULL;
140
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000141 rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, stream);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000142 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000143 *result = resultbuf;
144 }
145
146 return rv;
147}
148
Eric Andersen9615a082004-07-15 12:53:49 +0000149int fgetgrent_r(FILE *__restrict stream, struct group *__restrict resultbuf,
150 char *__restrict buffer, size_t buflen,
151 struct group **__restrict result)
152{
153 int rv;
154
155 *result = NULL;
156
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000157 rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, stream);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000158 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000159 *result = resultbuf;
160 }
161
162 return rv;
163}
164
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000165#if ENABLE_USE_BB_SHADOW
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200166#ifdef UNUSED_FOR_NOW
Eric Andersen9615a082004-07-15 12:53:49 +0000167int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
168 char *__restrict buffer, size_t buflen,
169 struct spwd **__restrict result)
170{
171 int rv;
172
173 *result = NULL;
174
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000175 rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, stream);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000176 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000177 *result = resultbuf;
178 }
179
180 return rv;
181}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000182#endif
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200183#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000184
Eric Andersen9615a082004-07-15 12:53:49 +0000185/**********************************************************************/
186/* For the various fget??ent funcs, return NULL on failure and a
Rob Landley06ec8cf2006-03-03 19:02:50 +0000187 * pointer to the appropriate struct (statically allocated) on success.
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000188 * TODO: audit & stop using these in bbox, they pull in static buffers */
Eric Andersen9615a082004-07-15 12:53:49 +0000189/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000190
Denys Vlasenko55301292010-03-31 12:38:17 +0200191#ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
Eric Andersen9615a082004-07-15 12:53:49 +0000192struct passwd *fgetpwent(FILE *stream)
193{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000194 struct statics *S;
195 struct passwd *resultbuf = RESULTBUF(fgetpwent);
196 char *buffer = BUFFER(fgetpwent);
Eric Andersen9615a082004-07-15 12:53:49 +0000197 struct passwd *result;
198
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000199 fgetpwent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetpwent)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000200 return result;
201}
202
Eric Andersen9615a082004-07-15 12:53:49 +0000203struct group *fgetgrent(FILE *stream)
204{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000205 struct statics *S;
206 struct group *resultbuf = RESULTBUF(fgetgrent);
207 char *buffer = BUFFER(fgetgrent);
Eric Andersen9615a082004-07-15 12:53:49 +0000208 struct group *result;
209
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000210 fgetgrent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetgrent)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000211 return result;
212}
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000213#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000214
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000215#if ENABLE_USE_BB_SHADOW
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200216#ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
Eric Andersen9615a082004-07-15 12:53:49 +0000217struct spwd *fgetspent(FILE *stream)
218{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000219 struct statics *S;
220 struct spwd *resultbuf = RESULTBUF(fgetspent);
221 char *buffer = BUFFER(fgetspent);
Eric Andersen9615a082004-07-15 12:53:49 +0000222 struct spwd *result;
223
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000224 fgetspent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetspent)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000225 return result;
226}
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000227#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000228
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200229#ifdef UNUSED_FOR_NOW
Eric Andersen9615a082004-07-15 12:53:49 +0000230int sgetspent_r(const char *string, struct spwd *result_buf,
231 char *buffer, size_t buflen, struct spwd **result)
232{
233 int rv = ERANGE;
234
235 *result = NULL;
236
237 if (buflen < PWD_BUFFER_SIZE) {
Denys Vlasenko9e59e272010-03-31 10:31:51 +0200238 DO_ERANGE:
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200239 errno = rv;
Eric Andersen9615a082004-07-15 12:53:49 +0000240 goto DONE;
241 }
242
243 if (string != buffer) {
244 if (strlen(string) >= buflen) {
245 goto DO_ERANGE;
246 }
247 strcpy(buffer, string);
248 }
249
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000250 rv = bb__parsespent(result_buf, buffer);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000251 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000252 *result = result_buf;
253 }
254
255 DONE:
256 return rv;
257}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000258#endif
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200259#endif /* ENABLE_USE_BB_SHADOW */
Eric Andersen9615a082004-07-15 12:53:49 +0000260
Eric Andersen9615a082004-07-15 12:53:49 +0000261/**********************************************************************/
262
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000263#define GETXXKEY_R_FUNC getpwnam_r
264#define GETXXKEY_R_PARSER bb__parsepwent
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000265#define GETXXKEY_R_ENTTYPE struct passwd
266#define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key))
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000267#define GETXXKEY_R_KEYTYPE const char *__restrict
268#define GETXXKEY_R_PATHNAME _PATH_PASSWD
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000269#include "pwd_grp_internal.c"
Eric Andersen9615a082004-07-15 12:53:49 +0000270
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000271#define GETXXKEY_R_FUNC getgrnam_r
272#define GETXXKEY_R_PARSER bb__parsegrent
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000273#define GETXXKEY_R_ENTTYPE struct group
274#define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key))
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000275#define GETXXKEY_R_KEYTYPE const char *__restrict
276#define GETXXKEY_R_PATHNAME _PATH_GROUP
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000277#include "pwd_grp_internal.c"
Eric Andersen9615a082004-07-15 12:53:49 +0000278
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000279#if ENABLE_USE_BB_SHADOW
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000280#define GETXXKEY_R_FUNC getspnam_r
281#define GETXXKEY_R_PARSER bb__parsespent
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000282#define GETXXKEY_R_ENTTYPE struct spwd
283#define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key))
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000284#define GETXXKEY_R_KEYTYPE const char *__restrict
285#define GETXXKEY_R_PATHNAME _PATH_SHADOW
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000286#include "pwd_grp_internal.c"
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000287#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000288
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000289#define GETXXKEY_R_FUNC getpwuid_r
290#define GETXXKEY_R_PARSER bb__parsepwent
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000291#define GETXXKEY_R_ENTTYPE struct passwd
292#define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key)
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000293#define GETXXKEY_R_KEYTYPE uid_t
294#define GETXXKEY_R_PATHNAME _PATH_PASSWD
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000295#include "pwd_grp_internal.c"
Eric Andersen9615a082004-07-15 12:53:49 +0000296
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000297#define GETXXKEY_R_FUNC getgrgid_r
298#define GETXXKEY_R_PARSER bb__parsegrent
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000299#define GETXXKEY_R_ENTTYPE struct group
300#define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key)
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000301#define GETXXKEY_R_KEYTYPE gid_t
302#define GETXXKEY_R_PATHNAME _PATH_GROUP
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000303#include "pwd_grp_internal.c"
Eric Andersen9615a082004-07-15 12:53:49 +0000304
305/**********************************************************************/
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000306/* TODO: audit & stop using these in bbox, they pull in static buffers */
Eric Andersen9615a082004-07-15 12:53:49 +0000307
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000308/* This one has many users */
Eric Andersen9615a082004-07-15 12:53:49 +0000309struct passwd *getpwuid(uid_t uid)
310{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000311 struct statics *S;
312 struct passwd *resultbuf = RESULTBUF(getpwuid);
313 char *buffer = BUFFER(getpwuid);
Eric Andersen9615a082004-07-15 12:53:49 +0000314 struct passwd *result;
315
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000316 getpwuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getpwuid)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000317 return result;
318}
319
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000320/* This one has many users */
Eric Andersen9615a082004-07-15 12:53:49 +0000321struct group *getgrgid(gid_t gid)
322{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000323 struct statics *S;
324 struct group *resultbuf = RESULTBUF(getgrgid);
325 char *buffer = BUFFER(getgrgid);
Eric Andersen9615a082004-07-15 12:53:49 +0000326 struct group *result;
327
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000328 getgrgid_r(gid, resultbuf, buffer, sizeof(BUFFER(getgrgid)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000329 return result;
330}
331
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000332#if 0 //ENABLE_USE_BB_SHADOW
Eric Andersen9615a082004-07-15 12:53:49 +0000333/* This function is non-standard and is currently not built. It seems
334 * to have been created as a reentrant version of the non-standard
335 * functions getspuid. Why getspuid was added, I do not know. */
Eric Andersen9615a082004-07-15 12:53:49 +0000336int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf,
337 char *__restrict buffer, size_t buflen,
338 struct spwd **__restrict result)
339{
340 int rv;
341 struct passwd *pp;
342 struct passwd password;
343 char pwd_buff[PWD_BUFFER_SIZE];
344
345 *result = NULL;
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000346 rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp);
347 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000348 rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result);
349 }
350
351 return rv;
352}
353
Eric Andersen9615a082004-07-15 12:53:49 +0000354/* This function is non-standard and is currently not built.
355 * Why it was added, I do not know. */
Eric Andersen9615a082004-07-15 12:53:49 +0000356struct spwd *getspuid(uid_t uid)
357{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000358 struct statics *S;
359 struct spwd *resultbuf = RESULTBUF(getspuid);
360 char *buffer = BUFFER(getspuid);
Eric Andersen9615a082004-07-15 12:53:49 +0000361 struct spwd *result;
362
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000363 getspuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getspuid)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000364 return result;
365}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000366#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000367
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000368/* This one has many users */
Eric Andersen9615a082004-07-15 12:53:49 +0000369struct passwd *getpwnam(const char *name)
370{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000371 struct statics *S;
372 struct passwd *resultbuf = RESULTBUF(getpwnam);
373 char *buffer = BUFFER(getpwnam);
Eric Andersen9615a082004-07-15 12:53:49 +0000374 struct passwd *result;
375
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000376 getpwnam_r(name, resultbuf, buffer, sizeof(BUFFER(getpwnam)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000377 return result;
378}
379
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000380/* This one has many users */
Eric Andersen9615a082004-07-15 12:53:49 +0000381struct group *getgrnam(const char *name)
382{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000383 struct statics *S;
384 struct group *resultbuf = RESULTBUF(getgrnam);
385 char *buffer = BUFFER(getgrnam);
Eric Andersen9615a082004-07-15 12:53:49 +0000386 struct group *result;
387
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000388 getgrnam_r(name, resultbuf, buffer, sizeof(BUFFER(getgrnam)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000389 return result;
390}
391
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000392#if 0 //ENABLE_USE_BB_SHADOW
Eric Andersen9615a082004-07-15 12:53:49 +0000393struct spwd *getspnam(const char *name)
394{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000395 struct statics *S;
396 struct spwd *resultbuf = RESULTBUF(getspnam);
397 char *buffer = BUFFER(getspnam);
Eric Andersen9615a082004-07-15 12:53:49 +0000398 struct spwd *result;
399
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000400 getspnam_r(name, resultbuf, buffer, sizeof(BUFFER(getspnam)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000401 return result;
402}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000403#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000404
Eric Andersen9615a082004-07-15 12:53:49 +0000405/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000406
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000407/* FIXME: we don't have such CONFIG_xx - ?! */
408
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000409#if defined CONFIG_USE_BB_THREADSAFE_SHADOW && defined PTHREAD_MUTEX_INITIALIZER
410static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
411# define LOCK pthread_mutex_lock(&mylock)
412# define UNLOCK pthread_mutex_unlock(&mylock);
413#else
414# define LOCK ((void) 0)
415# define UNLOCK ((void) 0)
416#endif
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000417
Eric Andersen9615a082004-07-15 12:53:49 +0000418static FILE *pwf /*= NULL*/;
419void setpwent(void)
420{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000421 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000422 if (pwf) {
423 rewind(pwf);
424 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000425 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000426}
427
428void endpwent(void)
429{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000430 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000431 if (pwf) {
432 fclose(pwf);
433 pwf = NULL;
434 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000435 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000436}
437
438
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000439int getpwent_r(struct passwd *__restrict resultbuf,
Eric Andersen9615a082004-07-15 12:53:49 +0000440 char *__restrict buffer, size_t buflen,
441 struct passwd **__restrict result)
442{
443 int rv;
444
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000445 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000446 *result = NULL; /* In case of error... */
447
448 if (!pwf) {
Denis Vlasenko5415c852008-07-21 23:05:26 +0000449 pwf = fopen_for_read(_PATH_PASSWD);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000450 if (!pwf) {
Eric Andersen9615a082004-07-15 12:53:49 +0000451 rv = errno;
452 goto ERR;
453 }
454 }
455
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000456 rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, pwf);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000457 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000458 *result = resultbuf;
459 }
460
461 ERR:
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000462 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000463 return rv;
464}
465
Eric Andersen9615a082004-07-15 12:53:49 +0000466static FILE *grf /*= NULL*/;
467void setgrent(void)
468{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000469 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000470 if (grf) {
471 rewind(grf);
472 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000473 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000474}
475
476void endgrent(void)
477{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000478 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000479 if (grf) {
480 fclose(grf);
481 grf = NULL;
482 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000483 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000484}
485
486int getgrent_r(struct group *__restrict resultbuf,
487 char *__restrict buffer, size_t buflen,
488 struct group **__restrict result)
489{
490 int rv;
491
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000492 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000493 *result = NULL; /* In case of error... */
494
495 if (!grf) {
Denis Vlasenko5415c852008-07-21 23:05:26 +0000496 grf = fopen_for_read(_PATH_GROUP);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000497 if (!grf) {
Eric Andersen9615a082004-07-15 12:53:49 +0000498 rv = errno;
499 goto ERR;
500 }
501 }
502
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000503 rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, grf);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000504 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000505 *result = resultbuf;
506 }
507
508 ERR:
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000509 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000510 return rv;
511}
512
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200513#ifdef UNUSED_FOR_NOW
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000514#if ENABLE_USE_BB_SHADOW
Eric Andersen9615a082004-07-15 12:53:49 +0000515static FILE *spf /*= NULL*/;
516void setspent(void)
517{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000518 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000519 if (spf) {
520 rewind(spf);
521 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000522 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000523}
524
525void endspent(void)
526{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000527 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000528 if (spf) {
529 fclose(spf);
530 spf = NULL;
531 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000532 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000533}
534
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000535int getspent_r(struct spwd *resultbuf, char *buffer,
Eric Andersen9615a082004-07-15 12:53:49 +0000536 size_t buflen, struct spwd **result)
537{
538 int rv;
539
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000540 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000541 *result = NULL; /* In case of error... */
542
543 if (!spf) {
Denis Vlasenko5415c852008-07-21 23:05:26 +0000544 spf = fopen_for_read(_PATH_SHADOW);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000545 if (!spf) {
Eric Andersen9615a082004-07-15 12:53:49 +0000546 rv = errno;
547 goto ERR;
548 }
549 }
550
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000551 rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, spf);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000552 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000553 *result = resultbuf;
554 }
555
556 ERR:
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000557 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000558 return rv;
559}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000560#endif
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200561#endif /* UNUSED_FOR_NOW */
Eric Andersen9615a082004-07-15 12:53:49 +0000562
Denys Vlasenko55301292010-03-31 12:38:17 +0200563#ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
Eric Andersen9615a082004-07-15 12:53:49 +0000564struct passwd *getpwent(void)
565{
566 static char line_buff[PWD_BUFFER_SIZE];
567 static struct passwd pwd;
568 struct passwd *result;
569
570 getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
571 return result;
572}
573
Eric Andersen9615a082004-07-15 12:53:49 +0000574struct group *getgrent(void)
575{
576 static char line_buff[GRP_BUFFER_SIZE];
577 static struct group gr;
578 struct group *result;
579
580 getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
581 return result;
582}
583
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200584#if ENABLE_USE_BB_SHADOW
Eric Andersen9615a082004-07-15 12:53:49 +0000585struct spwd *getspent(void)
586{
587 static char line_buff[PWD_BUFFER_SIZE];
588 static struct spwd spwd;
589 struct spwd *result;
590
591 getspent_r(&spwd, line_buff, sizeof(line_buff), &result);
592 return result;
593}
594
Eric Andersen9615a082004-07-15 12:53:49 +0000595struct spwd *sgetspent(const char *string)
596{
597 static char line_buff[PWD_BUFFER_SIZE];
598 static struct spwd spwd;
599 struct spwd *result;
600
601 sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result);
602 return result;
603}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000604#endif
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200605#endif /* UNUSED_SINCE_WE_AVOID_STATIC_BUFS */
Eric Andersen9615a082004-07-15 12:53:49 +0000606
Denis Vlasenko22284262008-09-18 00:56:24 +0000607static gid_t *getgrouplist_internal(int *ngroups_ptr, const char *user, gid_t gid)
Eric Andersen9615a082004-07-15 12:53:49 +0000608{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000609 FILE *grfile;
Eric Andersen9615a082004-07-15 12:53:49 +0000610 gid_t *group_list;
Denis Vlasenko22284262008-09-18 00:56:24 +0000611 int ngroups;
Eric Andersen9615a082004-07-15 12:53:49 +0000612 struct group group;
613 char buff[PWD_BUFFER_SIZE];
614
Denis Vlasenko22284262008-09-18 00:56:24 +0000615 /* We alloc space for 8 gids at a time. */
616 group_list = xmalloc(8 * sizeof(group_list[0]));
617 group_list[0] = gid;
618 ngroups = 1;
619
Denis Vlasenko5415c852008-07-21 23:05:26 +0000620 grfile = fopen_for_read(_PATH_GROUP);
Denis Vlasenko22284262008-09-18 00:56:24 +0000621 if (grfile) {
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000622 while (!bb__pgsreader(bb__parsegrent, &group, buff, sizeof(buff), grfile)) {
Denis Vlasenko22284262008-09-18 00:56:24 +0000623 char **m;
Eric Andersen9615a082004-07-15 12:53:49 +0000624 assert(group.gr_mem); /* Must have at least a NULL terminator. */
Denis Vlasenko22284262008-09-18 00:56:24 +0000625 if (group.gr_gid == gid)
626 continue;
627 for (m = group.gr_mem; *m; m++) {
628 if (strcmp(*m, user) != 0)
629 continue;
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200630 group_list = xrealloc_vector(group_list, /*8=2^3:*/ 3, ngroups);
Denis Vlasenko22284262008-09-18 00:56:24 +0000631 group_list[ngroups++] = group.gr_gid;
632 break;
Eric Andersen9615a082004-07-15 12:53:49 +0000633 }
634 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000635 fclose(grfile);
Eric Andersen9615a082004-07-15 12:53:49 +0000636 }
Denis Vlasenko22284262008-09-18 00:56:24 +0000637 *ngroups_ptr = ngroups;
638 return group_list;
639}
Eric Andersen9615a082004-07-15 12:53:49 +0000640
Denis Vlasenko22284262008-09-18 00:56:24 +0000641int initgroups(const char *user, gid_t gid)
642{
643 int ngroups;
644 gid_t *group_list = getgrouplist_internal(&ngroups, user, gid);
645
Denis Vlasenko22284262008-09-18 00:56:24 +0000646 ngroups = setgroups(ngroups, group_list);
647 free(group_list);
648 return ngroups;
649}
650
Denis Vlasenko22284262008-09-18 00:56:24 +0000651int getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups)
652{
653 int ngroups_old = *ngroups;
654 gid_t *group_list = getgrouplist_internal(ngroups, user, gid);
655
656 if (*ngroups <= ngroups_old) {
657 ngroups_old = *ngroups;
658 memcpy(groups, group_list, ngroups_old * sizeof(groups[0]));
659 } else {
660 ngroups_old = -1;
661 }
662 free(group_list);
663 return ngroups_old;
Eric Andersen9615a082004-07-15 12:53:49 +0000664}
665
Denys Vlasenko55301292010-03-31 12:38:17 +0200666#ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
Eric Andersen9615a082004-07-15 12:53:49 +0000667int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
668{
669 int rv = -1;
670
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200671#if 0
672 /* glibc does this check */
Eric Andersen9615a082004-07-15 12:53:49 +0000673 if (!p || !f) {
Denis Vlasenko92305822008-03-20 15:12:58 +0000674 errno = EINVAL;
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200675 return rv;
676 }
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200677#endif
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200678
679 /* No extra thread locking is needed above what fprintf does. */
680 if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n",
681 p->pw_name, p->pw_passwd,
682 (unsigned long)(p->pw_uid),
683 (unsigned long)(p->pw_gid),
684 p->pw_gecos, p->pw_dir, p->pw_shell) >= 0
685 ) {
686 rv = 0;
Eric Andersen9615a082004-07-15 12:53:49 +0000687 }
688
689 return rv;
690}
691
Eric Andersen9615a082004-07-15 12:53:49 +0000692int putgrent(const struct group *__restrict p, FILE *__restrict f)
693{
Eric Andersen9615a082004-07-15 12:53:49 +0000694 int rv = -1;
695
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200696#if 0
697 /* glibc does this check */
698 if (!p || !f) {
Denis Vlasenko92305822008-03-20 15:12:58 +0000699 errno = EINVAL;
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200700 return rv;
701 }
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200702#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000703
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200704 if (fprintf(f, "%s:%s:%lu:",
705 p->gr_name, p->gr_passwd,
706 (unsigned long)(p->gr_gid)) >= 0
707 ) {
708 static const char format[] ALIGN1 = ",%s";
Eric Andersen9615a082004-07-15 12:53:49 +0000709
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200710 char **m;
711 const char *fmt;
Eric Andersen9615a082004-07-15 12:53:49 +0000712
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200713 fmt = format + 1;
714
715 assert(p->gr_mem);
716 m = p->gr_mem;
717
718 while (1) {
719 if (!*m) {
720 if (fputc('\n', f) >= 0) {
721 rv = 0;
Eric Andersen9615a082004-07-15 12:53:49 +0000722 }
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200723 break;
Denys Vlasenko9e59e272010-03-31 10:31:51 +0200724 }
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200725 if (fprintf(f, fmt, *m) < 0) {
726 break;
727 }
728 m++;
729 fmt = format;
Eric Andersen9615a082004-07-15 12:53:49 +0000730 }
Eric Andersen9615a082004-07-15 12:53:49 +0000731 }
732
733 return rv;
734}
Denys Vlasenko55301292010-03-31 12:38:17 +0200735#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000736
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000737#if ENABLE_USE_BB_SHADOW
Denys Vlasenko05d1a322010-04-02 12:12:43 +0200738#ifdef UNUSED_FOR_NOW
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200739static const unsigned char put_sp_off[] ALIGN1 = {
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000740 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
741 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
742 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
743 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
744 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
745 offsetof(struct spwd, sp_expire) /* 7 - not a char ptr */
Eric Andersen9615a082004-07-15 12:53:49 +0000746};
747
748int putspent(const struct spwd *p, FILE *stream)
749{
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200750 const char *fmt;
Denis Vlasenko87468852007-04-13 23:22:00 +0000751 long x;
Eric Andersen9615a082004-07-15 12:53:49 +0000752 int i;
753 int rv = -1;
754
755 /* Unlike putpwent and putgrent, glibc does not check the args. */
756 if (fprintf(stream, "%s:%s:", p->sp_namp,
757 (p->sp_pwdp ? p->sp_pwdp : "")) < 0
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000758 ) {
Eric Andersen9615a082004-07-15 12:53:49 +0000759 goto DO_UNLOCK;
760 }
761
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200762 for (i = 0; i < sizeof(put_sp_off); i++) {
763 fmt = "%ld:";
764 x = *(long *)((char *)p + put_sp_off[i]);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000765 if (x == -1) {
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200766 fmt += 3;
Eric Andersen9615a082004-07-15 12:53:49 +0000767 }
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200768 if (fprintf(stream, fmt, x) < 0) {
Eric Andersen9615a082004-07-15 12:53:49 +0000769 goto DO_UNLOCK;
770 }
771 }
772
773 if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) {
774 goto DO_UNLOCK;
775 }
776
Rob Landley25413bf2005-10-08 02:23:22 +0000777 if (fputc('\n', stream) > 0) {
Eric Andersen9615a082004-07-15 12:53:49 +0000778 rv = 0;
779 }
780
Denys Vlasenko9e59e272010-03-31 10:31:51 +0200781 DO_UNLOCK:
Eric Andersen9615a082004-07-15 12:53:49 +0000782 return rv;
783}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000784#endif
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200785#endif /* USE_BB_SHADOW */
Eric Andersen9615a082004-07-15 12:53:49 +0000786
Eric Andersen9615a082004-07-15 12:53:49 +0000787/**********************************************************************/
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200788/* Internal functions */
Eric Andersen9615a082004-07-15 12:53:49 +0000789/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000790
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000791static const unsigned char pw_off[] ALIGN1 = {
792 offsetof(struct passwd, pw_name), /* 0 */
793 offsetof(struct passwd, pw_passwd), /* 1 */
794 offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */
795 offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */
796 offsetof(struct passwd, pw_gecos), /* 4 */
797 offsetof(struct passwd, pw_dir), /* 5 */
798 offsetof(struct passwd, pw_shell) /* 6 */
Eric Andersen9615a082004-07-15 12:53:49 +0000799};
800
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200801static int FAST_FUNC bb__parsepwent(void *data, char *line)
Eric Andersen9615a082004-07-15 12:53:49 +0000802{
803 char *endptr;
804 char *p;
805 int i;
806
807 i = 0;
Denys Vlasenko9e59e272010-03-31 10:31:51 +0200808 while (1) {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200809 p = (char *) data + pw_off[i];
Eric Andersen9615a082004-07-15 12:53:49 +0000810
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200811 if (i < 2 || i > 3) {
Eric Andersen9615a082004-07-15 12:53:49 +0000812 *((char **) p) = line;
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200813 if (i == 6) {
Eric Andersen9615a082004-07-15 12:53:49 +0000814 return 0;
815 }
816 /* NOTE: glibc difference - glibc allows omission of
817 * ':' seperators after the gid field if all remaining
818 * entries are empty. We require all separators. */
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000819 line = strchr(line, ':');
820 if (!line) {
Eric Andersen9615a082004-07-15 12:53:49 +0000821 break;
822 }
823 } else {
824 unsigned long t = strtoul(line, &endptr, 10);
825 /* Make sure we had at least one digit, and that the
826 * failing char is the next field seperator ':'. See
827 * glibc difference note above. */
828 /* TODO: Also check for leading whitespace? */
829 if ((endptr == line) || (*endptr != ':')) {
830 break;
831 }
832 line = endptr;
833 if (i & 1) { /* i == 3 -- gid */
834 *((gid_t *) p) = t;
835 } else { /* i == 2 -- uid */
836 *((uid_t *) p) = t;
837 }
838 }
839
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200840 *line++ = '\0';
841 i++;
Denys Vlasenko9e59e272010-03-31 10:31:51 +0200842 } /* while (1) */
Eric Andersen9615a082004-07-15 12:53:49 +0000843
844 return -1;
845}
846
Eric Andersen9615a082004-07-15 12:53:49 +0000847/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000848
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000849static const unsigned char gr_off[] ALIGN1 = {
850 offsetof(struct group, gr_name), /* 0 */
851 offsetof(struct group, gr_passwd), /* 1 */
852 offsetof(struct group, gr_gid) /* 2 - not a char ptr */
Eric Andersen9615a082004-07-15 12:53:49 +0000853};
854
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200855static int FAST_FUNC bb__parsegrent(void *data, char *line)
Eric Andersen9615a082004-07-15 12:53:49 +0000856{
857 char *endptr;
858 char *p;
859 int i;
860 char **members;
861 char *end_of_buf;
862
863 end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */
864 i = 0;
Denys Vlasenko9e59e272010-03-31 10:31:51 +0200865 while (1) {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200866 p = (char *) data + gr_off[i];
Eric Andersen9615a082004-07-15 12:53:49 +0000867
868 if (i < 2) {
869 *((char **) p) = line;
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000870 line = strchr(line, ':');
871 if (!line) {
Eric Andersen9615a082004-07-15 12:53:49 +0000872 break;
873 }
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200874 *line++ = '\0';
875 i++;
Eric Andersen9615a082004-07-15 12:53:49 +0000876 } else {
877 *((gid_t *) p) = strtoul(line, &endptr, 10);
878
879 /* NOTE: glibc difference - glibc allows omission of the
880 * trailing colon when there is no member list. We treat
881 * this as an error. */
882
883 /* Make sure we had at least one digit, and that the
884 * failing char is the next field seperator ':'. See
885 * glibc difference note above. */
886 if ((endptr == line) || (*endptr != ':')) {
887 break;
888 }
889
890 i = 1; /* Count terminating NULL ptr. */
891 p = endptr;
892
893 if (p[1]) { /* We have a member list to process. */
894 /* Overwrite the last ':' with a ',' before counting.
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200895 * This allows us to (1) test for initial ','
896 * and (2) adds one ',' so that the number of commas
897 * equals the member count. */
Eric Andersen9615a082004-07-15 12:53:49 +0000898 *p = ',';
899 do {
900 /* NOTE: glibc difference - glibc allows and trims leading
901 * (but not trailing) space. We treat this as an error. */
902 /* NOTE: glibc difference - glibc allows consecutive and
903 * trailing commas, and ignores "empty string" users. We
904 * treat this as an error. */
905 if (*p == ',') {
906 ++i;
907 *p = 0; /* nul-terminate each member string. */
908 if (!*++p || (*p == ',') || isspace(*p)) {
909 goto ERR;
910 }
911 }
912 } while (*++p);
913 }
914
915 /* Now align (p+1), rounding up. */
916 /* Assumes sizeof(char **) is a power of 2. */
917 members = (char **)( (((intptr_t) p) + sizeof(char **))
918 & ~((intptr_t)(sizeof(char **) - 1)) );
919
920 if (((char *)(members + i)) > end_of_buf) { /* No space. */
921 break;
922 }
923
924 ((struct group *) data)->gr_mem = members;
925
926 if (--i) {
927 p = endptr; /* Pointing to char prior to first member. */
Denys Vlasenko9e59e272010-03-31 10:31:51 +0200928 while (1) {
Eric Andersen9615a082004-07-15 12:53:49 +0000929 *members++ = ++p;
Denys Vlasenko9e59e272010-03-31 10:31:51 +0200930 if (!--i)
931 break;
932 while (*++p)
933 continue;
934 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000935 }
Eric Andersen9615a082004-07-15 12:53:49 +0000936 *members = NULL;
937
938 return 0;
939 }
Denys Vlasenko9e59e272010-03-31 10:31:51 +0200940 } /* while (1) */
Eric Andersen9615a082004-07-15 12:53:49 +0000941
942 ERR:
943 return -1;
944}
945
Eric Andersen9615a082004-07-15 12:53:49 +0000946/**********************************************************************/
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000947
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000948#if ENABLE_USE_BB_SHADOW
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000949static const unsigned char sp_off[] ALIGN1 = {
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200950 offsetof(struct spwd, sp_namp), /* 0: char* */
951 offsetof(struct spwd, sp_pwdp), /* 1: char* */
952 offsetof(struct spwd, sp_lstchg), /* 2: long */
953 offsetof(struct spwd, sp_min), /* 3: long */
954 offsetof(struct spwd, sp_max), /* 4: long */
955 offsetof(struct spwd, sp_warn), /* 5: long */
956 offsetof(struct spwd, sp_inact), /* 6: long */
957 offsetof(struct spwd, sp_expire), /* 7: long */
958 offsetof(struct spwd, sp_flag) /* 8: unsigned long */
Eric Andersen9615a082004-07-15 12:53:49 +0000959};
960
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200961static int FAST_FUNC bb__parsespent(void *data, char *line)
Eric Andersen9615a082004-07-15 12:53:49 +0000962{
963 char *endptr;
964 char *p;
965 int i;
966
967 i = 0;
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200968 while (1) {
969 p = (char *) data + sp_off[i];
Eric Andersen9615a082004-07-15 12:53:49 +0000970 if (i < 2) {
971 *((char **) p) = line;
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000972 line = strchr(line, ':');
973 if (!line) {
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200974 break; /* error */
Eric Andersen9615a082004-07-15 12:53:49 +0000975 }
976 } else {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200977 *((long *) p) = strtoul(line, &endptr, 10);
Eric Andersen9615a082004-07-15 12:53:49 +0000978 if (endptr == line) {
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200979 *((long *) p) = -1L;
Eric Andersen9615a082004-07-15 12:53:49 +0000980 }
Eric Andersen9615a082004-07-15 12:53:49 +0000981 line = endptr;
Eric Andersen9615a082004-07-15 12:53:49 +0000982 if (i == 8) {
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200983 if (*line != '\0') {
984 break; /* error */
Eric Andersen9615a082004-07-15 12:53:49 +0000985 }
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200986 return 0; /* all ok */
Eric Andersen9615a082004-07-15 12:53:49 +0000987 }
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200988 if (*line != ':') {
989 break; /* error */
Eric Andersen9615a082004-07-15 12:53:49 +0000990 }
Eric Andersen9615a082004-07-15 12:53:49 +0000991 }
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200992 *line++ = '\0';
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200993 i++;
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200994 }
Eric Andersen9615a082004-07-15 12:53:49 +0000995
996 return EINVAL;
997}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000998#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000999
Eric Andersen9615a082004-07-15 12:53:49 +00001000/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +00001001
Denys Vlasenko05d1a322010-04-02 12:12:43 +02001002/* Reads until EOF, or until it finds a line which fits in the buffer
Eric Andersen9615a082004-07-15 12:53:49 +00001003 * and for which the parser function succeeds.
1004 *
Denys Vlasenko05d1a322010-04-02 12:12:43 +02001005 * Returns 0 on success and ENOENT for end-of-file (glibc convention).
Eric Andersen9615a082004-07-15 12:53:49 +00001006 */
Denys Vlasenko17fcd722010-03-31 12:37:43 +02001007static int bb__pgsreader(
1008 int FAST_FUNC (*parserfunc)(void *d, char *line),
1009 void *data,
1010 char *__restrict line_buff,
1011 size_t buflen,
1012 FILE *f)
Eric Andersen9615a082004-07-15 12:53:49 +00001013{
Eric Andersen9615a082004-07-15 12:53:49 +00001014 int skip;
1015 int rv = ERANGE;
1016
1017 if (buflen < PWD_BUFFER_SIZE) {
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +00001018 errno = rv;
Denys Vlasenko57dc3c72010-03-31 10:24:37 +02001019 return rv;
1020 }
1021
1022 skip = 0;
1023 while (1) {
1024 if (!fgets(line_buff, buflen, f)) {
1025 if (feof(f)) {
1026 rv = ENOENT;
1027 }
1028 break;
1029 }
1030
1031 {
1032 int line_len = strlen(line_buff) - 1;
1033 if (line_len >= 0 && line_buff[line_len] == '\n') {
1034 line_buff[line_len] = '\0';
1035 } else
1036 if (line_len + 2 == buflen) {
1037 /* A start (or continuation) of overlong line */
1038 skip = 1;
1039 continue;
1040 } /* else: a last line in the file, and it has no '\n' */
1041 }
1042
1043 if (skip) {
1044 /* This "line" is a remainder of overlong line, ignore */
1045 skip = 0;
1046 continue;
1047 }
1048
1049 /* NOTE: glibc difference - glibc strips leading whitespace from
1050 * records. We do not allow leading whitespace. */
1051
1052 /* Skip empty lines, comment lines, and lines with leading
1053 * whitespace. */
1054 if (line_buff[0] != '\0' && line_buff[0] != '#' && !isspace(line_buff[0])) {
1055 if (parserfunc == bb__parsegrent) {
1056 /* Do evil group hack:
1057 * The group entry parsing function needs to know where
1058 * the end of the buffer is so that it can construct the
1059 * group member ptr table. */
1060 ((struct group *) data)->gr_name = line_buff + buflen;
1061 }
1062 if (parserfunc(data, line_buff) == 0) {
1063 rv = 0;
Eric Andersen9615a082004-07-15 12:53:49 +00001064 break;
1065 }
Denys Vlasenko57dc3c72010-03-31 10:24:37 +02001066 }
1067 } /* while (1) */
Eric Andersen9615a082004-07-15 12:53:49 +00001068
1069 return rv;
1070}