blob: 34690a738e554f3caff88d596c4593515a37117d [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
Denys Vlasenko17fcd722010-03-31 12:37:43 +020045static int bb__pgsreader(
46 int FAST_FUNC (*parserfunc)(void *d, char *line),
47 void *data,
48 char *__restrict line_buff,
49 size_t buflen,
50 FILE *f);
Eric Andersen9615a082004-07-15 12:53:49 +000051
Denys Vlasenko17fcd722010-03-31 12:37:43 +020052static int FAST_FUNC bb__parsepwent(void *pw, char *line);
53static int FAST_FUNC bb__parsegrent(void *gr, char *line);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +000054#if ENABLE_USE_BB_SHADOW
Denys Vlasenko17fcd722010-03-31 12:37:43 +020055static int FAST_FUNC bb__parsespent(void *sp, char *line);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +000056#endif
57
Eric Andersen9615a082004-07-15 12:53:49 +000058/**********************************************************************/
Denis Vlasenko2c91efb2007-06-18 10:08:27 +000059/* We avoid having big global data. */
60
61struct statics {
62 /* Smaller things first */
63 struct passwd getpwuid_resultbuf;
64 struct group getgrgid_resultbuf;
65 struct passwd getpwnam_resultbuf;
66 struct group getgrnam_resultbuf;
67
68 char getpwuid_buffer[PWD_BUFFER_SIZE];
69 char getgrgid_buffer[GRP_BUFFER_SIZE];
70 char getpwnam_buffer[PWD_BUFFER_SIZE];
71 char getgrnam_buffer[GRP_BUFFER_SIZE];
72#if 0
73 struct passwd fgetpwent_resultbuf;
74 struct group fgetgrent_resultbuf;
75 struct spwd fgetspent_resultbuf;
76 char fgetpwent_buffer[PWD_BUFFER_SIZE];
77 char fgetgrent_buffer[GRP_BUFFER_SIZE];
78 char fgetspent_buffer[PWD_BUFFER_SIZE];
79#endif
80#if 0 //ENABLE_USE_BB_SHADOW
81 struct spwd getspuid_resultbuf;
82 struct spwd getspnam_resultbuf;
83 char getspuid_buffer[PWD_BUFFER_SIZE];
84 char getspnam_buffer[PWD_BUFFER_SIZE];
85#endif
86// Not converted - too small to bother
87//pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
88//FILE *pwf /*= NULL*/;
89//FILE *grf /*= NULL*/;
90//FILE *spf /*= NULL*/;
91#if 0
92 struct passwd getpwent_pwd;
93 struct group getgrent_gr;
94 char getpwent_line_buff[PWD_BUFFER_SIZE];
95 char getgrent_line_buff[GRP_BUFFER_SIZE];
96#endif
97#if 0 //ENABLE_USE_BB_SHADOW
98 struct spwd getspent_spwd;
99 struct spwd sgetspent_spwd;
100 char getspent_line_buff[PWD_BUFFER_SIZE];
101 char sgetspent_line_buff[PWD_BUFFER_SIZE];
102#endif
103};
104
105static struct statics *ptr_to_statics;
106
107static struct statics *get_S(void)
108{
109 if (!ptr_to_statics)
110 ptr_to_statics = xzalloc(sizeof(*ptr_to_statics));
111 return ptr_to_statics;
112}
113
114/* Always use in this order, get_S() must be called first */
115#define RESULTBUF(name) &((S = get_S())->name##_resultbuf)
116#define BUFFER(name) (S->name##_buffer)
117
118/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000119/* For the various fget??ent_r funcs, return
120 *
121 * 0: success
122 * ENOENT: end-of-file encountered
123 * ERANGE: buflen too small
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000124 * other error values possible. See bb__pgsreader.
Eric Andersen9615a082004-07-15 12:53:49 +0000125 *
126 * Also, *result == resultbuf on success and NULL on failure.
127 *
128 * NOTE: glibc difference - For the ENOENT case, glibc also sets errno.
129 * We do not, as it really isn't an error if we reach the end-of-file.
130 * Doing so is analogous to having fgetc() set errno on EOF.
131 */
132/**********************************************************************/
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000133
Eric Andersen9615a082004-07-15 12:53:49 +0000134int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf,
135 char *__restrict buffer, size_t buflen,
136 struct passwd **__restrict result)
137{
138 int rv;
139
140 *result = NULL;
141
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000142 rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, stream);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000143 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000144 *result = resultbuf;
145 }
146
147 return rv;
148}
149
Eric Andersen9615a082004-07-15 12:53:49 +0000150int fgetgrent_r(FILE *__restrict stream, struct group *__restrict resultbuf,
151 char *__restrict buffer, size_t buflen,
152 struct group **__restrict result)
153{
154 int rv;
155
156 *result = NULL;
157
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000158 rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, stream);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000159 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000160 *result = resultbuf;
161 }
162
163 return rv;
164}
165
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000166#if ENABLE_USE_BB_SHADOW
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200167#ifdef UNUSED_FOR_NOW
Eric Andersen9615a082004-07-15 12:53:49 +0000168int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
169 char *__restrict buffer, size_t buflen,
170 struct spwd **__restrict result)
171{
172 int rv;
173
174 *result = NULL;
175
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000176 rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, stream);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000177 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000178 *result = resultbuf;
179 }
180
181 return rv;
182}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000183#endif
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200184#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000185
Eric Andersen9615a082004-07-15 12:53:49 +0000186/**********************************************************************/
187/* For the various fget??ent funcs, return NULL on failure and a
Rob Landley06ec8cf2006-03-03 19:02:50 +0000188 * pointer to the appropriate struct (statically allocated) on success.
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000189 * TODO: audit & stop using these in bbox, they pull in static buffers */
Eric Andersen9615a082004-07-15 12:53:49 +0000190/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000191
Denys Vlasenko55301292010-03-31 12:38:17 +0200192#ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
Eric Andersen9615a082004-07-15 12:53:49 +0000193struct passwd *fgetpwent(FILE *stream)
194{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000195 struct statics *S;
196 struct passwd *resultbuf = RESULTBUF(fgetpwent);
197 char *buffer = BUFFER(fgetpwent);
Eric Andersen9615a082004-07-15 12:53:49 +0000198 struct passwd *result;
199
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000200 fgetpwent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetpwent)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000201 return result;
202}
203
Eric Andersen9615a082004-07-15 12:53:49 +0000204struct group *fgetgrent(FILE *stream)
205{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000206 struct statics *S;
207 struct group *resultbuf = RESULTBUF(fgetgrent);
208 char *buffer = BUFFER(fgetgrent);
Eric Andersen9615a082004-07-15 12:53:49 +0000209 struct group *result;
210
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000211 fgetgrent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetgrent)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000212 return result;
213}
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000214#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000215
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000216#if ENABLE_USE_BB_SHADOW
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200217#ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
Eric Andersen9615a082004-07-15 12:53:49 +0000218struct spwd *fgetspent(FILE *stream)
219{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000220 struct statics *S;
221 struct spwd *resultbuf = RESULTBUF(fgetspent);
222 char *buffer = BUFFER(fgetspent);
Eric Andersen9615a082004-07-15 12:53:49 +0000223 struct spwd *result;
224
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000225 fgetspent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetspent)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000226 return result;
227}
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000228#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000229
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200230#ifdef UNUSED_FOR_NOW
Eric Andersen9615a082004-07-15 12:53:49 +0000231int sgetspent_r(const char *string, struct spwd *result_buf,
232 char *buffer, size_t buflen, struct spwd **result)
233{
234 int rv = ERANGE;
235
236 *result = NULL;
237
238 if (buflen < PWD_BUFFER_SIZE) {
Denys Vlasenko9e59e272010-03-31 10:31:51 +0200239 DO_ERANGE:
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200240 errno = rv;
Eric Andersen9615a082004-07-15 12:53:49 +0000241 goto DONE;
242 }
243
244 if (string != buffer) {
245 if (strlen(string) >= buflen) {
246 goto DO_ERANGE;
247 }
248 strcpy(buffer, string);
249 }
250
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000251 rv = bb__parsespent(result_buf, buffer);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000252 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000253 *result = result_buf;
254 }
255
256 DONE:
257 return rv;
258}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000259#endif
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200260#endif /* ENABLE_USE_BB_SHADOW */
Eric Andersen9615a082004-07-15 12:53:49 +0000261
Eric Andersen9615a082004-07-15 12:53:49 +0000262/**********************************************************************/
263
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000264#define GETXXKEY_R_FUNC getpwnam_r
265#define GETXXKEY_R_PARSER bb__parsepwent
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000266#define GETXXKEY_R_ENTTYPE struct passwd
267#define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key))
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000268#define GETXXKEY_R_KEYTYPE const char *__restrict
269#define GETXXKEY_R_PATHNAME _PATH_PASSWD
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000270#include "pwd_grp_internal.c"
Eric Andersen9615a082004-07-15 12:53:49 +0000271
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000272#define GETXXKEY_R_FUNC getgrnam_r
273#define GETXXKEY_R_PARSER bb__parsegrent
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000274#define GETXXKEY_R_ENTTYPE struct group
275#define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key))
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000276#define GETXXKEY_R_KEYTYPE const char *__restrict
277#define GETXXKEY_R_PATHNAME _PATH_GROUP
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000278#include "pwd_grp_internal.c"
Eric Andersen9615a082004-07-15 12:53:49 +0000279
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000280#if ENABLE_USE_BB_SHADOW
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000281#define GETXXKEY_R_FUNC getspnam_r
282#define GETXXKEY_R_PARSER bb__parsespent
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000283#define GETXXKEY_R_ENTTYPE struct spwd
284#define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key))
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000285#define GETXXKEY_R_KEYTYPE const char *__restrict
286#define GETXXKEY_R_PATHNAME _PATH_SHADOW
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000287#include "pwd_grp_internal.c"
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000288#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000289
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000290#define GETXXKEY_R_FUNC getpwuid_r
291#define GETXXKEY_R_PARSER bb__parsepwent
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000292#define GETXXKEY_R_ENTTYPE struct passwd
293#define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key)
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000294#define GETXXKEY_R_KEYTYPE uid_t
295#define GETXXKEY_R_PATHNAME _PATH_PASSWD
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000296#include "pwd_grp_internal.c"
Eric Andersen9615a082004-07-15 12:53:49 +0000297
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000298#define GETXXKEY_R_FUNC getgrgid_r
299#define GETXXKEY_R_PARSER bb__parsegrent
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000300#define GETXXKEY_R_ENTTYPE struct group
301#define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key)
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000302#define GETXXKEY_R_KEYTYPE gid_t
303#define GETXXKEY_R_PATHNAME _PATH_GROUP
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000304#include "pwd_grp_internal.c"
Eric Andersen9615a082004-07-15 12:53:49 +0000305
306/**********************************************************************/
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000307/* TODO: audit & stop using these in bbox, they pull in static buffers */
Eric Andersen9615a082004-07-15 12:53:49 +0000308
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000309/* This one has many users */
Eric Andersen9615a082004-07-15 12:53:49 +0000310struct passwd *getpwuid(uid_t uid)
311{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000312 struct statics *S;
313 struct passwd *resultbuf = RESULTBUF(getpwuid);
314 char *buffer = BUFFER(getpwuid);
Eric Andersen9615a082004-07-15 12:53:49 +0000315 struct passwd *result;
316
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000317 getpwuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getpwuid)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000318 return result;
319}
320
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000321/* This one has many users */
Eric Andersen9615a082004-07-15 12:53:49 +0000322struct group *getgrgid(gid_t gid)
323{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000324 struct statics *S;
325 struct group *resultbuf = RESULTBUF(getgrgid);
326 char *buffer = BUFFER(getgrgid);
Eric Andersen9615a082004-07-15 12:53:49 +0000327 struct group *result;
328
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000329 getgrgid_r(gid, resultbuf, buffer, sizeof(BUFFER(getgrgid)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000330 return result;
331}
332
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000333#if 0 //ENABLE_USE_BB_SHADOW
Eric Andersen9615a082004-07-15 12:53:49 +0000334/* This function is non-standard and is currently not built. It seems
335 * to have been created as a reentrant version of the non-standard
336 * functions getspuid. Why getspuid was added, I do not know. */
Eric Andersen9615a082004-07-15 12:53:49 +0000337int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf,
338 char *__restrict buffer, size_t buflen,
339 struct spwd **__restrict result)
340{
341 int rv;
342 struct passwd *pp;
343 struct passwd password;
344 char pwd_buff[PWD_BUFFER_SIZE];
345
346 *result = NULL;
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000347 rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp);
348 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000349 rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result);
350 }
351
352 return rv;
353}
354
Eric Andersen9615a082004-07-15 12:53:49 +0000355/* This function is non-standard and is currently not built.
356 * Why it was added, I do not know. */
Eric Andersen9615a082004-07-15 12:53:49 +0000357struct spwd *getspuid(uid_t uid)
358{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000359 struct statics *S;
360 struct spwd *resultbuf = RESULTBUF(getspuid);
361 char *buffer = BUFFER(getspuid);
Eric Andersen9615a082004-07-15 12:53:49 +0000362 struct spwd *result;
363
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000364 getspuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getspuid)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000365 return result;
366}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000367#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000368
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000369/* This one has many users */
Eric Andersen9615a082004-07-15 12:53:49 +0000370struct passwd *getpwnam(const char *name)
371{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000372 struct statics *S;
373 struct passwd *resultbuf = RESULTBUF(getpwnam);
374 char *buffer = BUFFER(getpwnam);
Eric Andersen9615a082004-07-15 12:53:49 +0000375 struct passwd *result;
376
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000377 getpwnam_r(name, resultbuf, buffer, sizeof(BUFFER(getpwnam)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000378 return result;
379}
380
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000381/* This one has many users */
Eric Andersen9615a082004-07-15 12:53:49 +0000382struct group *getgrnam(const char *name)
383{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000384 struct statics *S;
385 struct group *resultbuf = RESULTBUF(getgrnam);
386 char *buffer = BUFFER(getgrnam);
Eric Andersen9615a082004-07-15 12:53:49 +0000387 struct group *result;
388
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000389 getgrnam_r(name, resultbuf, buffer, sizeof(BUFFER(getgrnam)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000390 return result;
391}
392
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000393#if 0 //ENABLE_USE_BB_SHADOW
Eric Andersen9615a082004-07-15 12:53:49 +0000394struct spwd *getspnam(const char *name)
395{
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000396 struct statics *S;
397 struct spwd *resultbuf = RESULTBUF(getspnam);
398 char *buffer = BUFFER(getspnam);
Eric Andersen9615a082004-07-15 12:53:49 +0000399 struct spwd *result;
400
Denis Vlasenko2c91efb2007-06-18 10:08:27 +0000401 getspnam_r(name, resultbuf, buffer, sizeof(BUFFER(getspnam)), &result);
Eric Andersen9615a082004-07-15 12:53:49 +0000402 return result;
403}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000404#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000405
Eric Andersen9615a082004-07-15 12:53:49 +0000406/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000407
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000408/* FIXME: we don't have such CONFIG_xx - ?! */
409
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000410#if defined CONFIG_USE_BB_THREADSAFE_SHADOW && defined PTHREAD_MUTEX_INITIALIZER
411static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
412# define LOCK pthread_mutex_lock(&mylock)
413# define UNLOCK pthread_mutex_unlock(&mylock);
414#else
415# define LOCK ((void) 0)
416# define UNLOCK ((void) 0)
417#endif
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000418
Eric Andersen9615a082004-07-15 12:53:49 +0000419static FILE *pwf /*= NULL*/;
420void setpwent(void)
421{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000422 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000423 if (pwf) {
424 rewind(pwf);
425 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000426 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000427}
428
429void endpwent(void)
430{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000431 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000432 if (pwf) {
433 fclose(pwf);
434 pwf = NULL;
435 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000436 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000437}
438
439
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000440int getpwent_r(struct passwd *__restrict resultbuf,
Eric Andersen9615a082004-07-15 12:53:49 +0000441 char *__restrict buffer, size_t buflen,
442 struct passwd **__restrict result)
443{
444 int rv;
445
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000446 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000447 *result = NULL; /* In case of error... */
448
449 if (!pwf) {
Denis Vlasenko5415c852008-07-21 23:05:26 +0000450 pwf = fopen_for_read(_PATH_PASSWD);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000451 if (!pwf) {
Eric Andersen9615a082004-07-15 12:53:49 +0000452 rv = errno;
453 goto ERR;
454 }
455 }
456
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000457 rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, pwf);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000458 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000459 *result = resultbuf;
460 }
461
462 ERR:
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000463 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000464 return rv;
465}
466
Eric Andersen9615a082004-07-15 12:53:49 +0000467static FILE *grf /*= NULL*/;
468void setgrent(void)
469{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000470 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000471 if (grf) {
472 rewind(grf);
473 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000474 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000475}
476
477void endgrent(void)
478{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000479 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000480 if (grf) {
481 fclose(grf);
482 grf = NULL;
483 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000484 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000485}
486
487int getgrent_r(struct group *__restrict resultbuf,
488 char *__restrict buffer, size_t buflen,
489 struct group **__restrict result)
490{
491 int rv;
492
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000493 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000494 *result = NULL; /* In case of error... */
495
496 if (!grf) {
Denis Vlasenko5415c852008-07-21 23:05:26 +0000497 grf = fopen_for_read(_PATH_GROUP);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000498 if (!grf) {
Eric Andersen9615a082004-07-15 12:53:49 +0000499 rv = errno;
500 goto ERR;
501 }
502 }
503
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000504 rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, grf);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000505 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000506 *result = resultbuf;
507 }
508
509 ERR:
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000510 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000511 return rv;
512}
513
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200514#ifdef UNUSED_FOR_NOW
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000515#if ENABLE_USE_BB_SHADOW
Eric Andersen9615a082004-07-15 12:53:49 +0000516static FILE *spf /*= NULL*/;
517void setspent(void)
518{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000519 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000520 if (spf) {
521 rewind(spf);
522 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000523 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000524}
525
526void endspent(void)
527{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000528 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000529 if (spf) {
530 fclose(spf);
531 spf = NULL;
532 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000533 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000534}
535
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000536int getspent_r(struct spwd *resultbuf, char *buffer,
Eric Andersen9615a082004-07-15 12:53:49 +0000537 size_t buflen, struct spwd **result)
538{
539 int rv;
540
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000541 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000542 *result = NULL; /* In case of error... */
543
544 if (!spf) {
Denis Vlasenko5415c852008-07-21 23:05:26 +0000545 spf = fopen_for_read(_PATH_SHADOW);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000546 if (!spf) {
Eric Andersen9615a082004-07-15 12:53:49 +0000547 rv = errno;
548 goto ERR;
549 }
550 }
551
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000552 rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, spf);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000553 if (!rv) {
Eric Andersen9615a082004-07-15 12:53:49 +0000554 *result = resultbuf;
555 }
556
557 ERR:
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000558 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000559 return rv;
560}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000561#endif
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200562#endif /* UNUSED_FOR_NOW */
Eric Andersen9615a082004-07-15 12:53:49 +0000563
Denys Vlasenko55301292010-03-31 12:38:17 +0200564#ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
Eric Andersen9615a082004-07-15 12:53:49 +0000565struct passwd *getpwent(void)
566{
567 static char line_buff[PWD_BUFFER_SIZE];
568 static struct passwd pwd;
569 struct passwd *result;
570
571 getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
572 return result;
573}
574
Eric Andersen9615a082004-07-15 12:53:49 +0000575struct group *getgrent(void)
576{
577 static char line_buff[GRP_BUFFER_SIZE];
578 static struct group gr;
579 struct group *result;
580
581 getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
582 return result;
583}
584
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200585#if ENABLE_USE_BB_SHADOW
Eric Andersen9615a082004-07-15 12:53:49 +0000586struct spwd *getspent(void)
587{
588 static char line_buff[PWD_BUFFER_SIZE];
589 static struct spwd spwd;
590 struct spwd *result;
591
592 getspent_r(&spwd, line_buff, sizeof(line_buff), &result);
593 return result;
594}
595
Eric Andersen9615a082004-07-15 12:53:49 +0000596struct spwd *sgetspent(const char *string)
597{
598 static char line_buff[PWD_BUFFER_SIZE];
599 static struct spwd spwd;
600 struct spwd *result;
601
602 sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result);
603 return result;
604}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000605#endif
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200606#endif /* UNUSED_SINCE_WE_AVOID_STATIC_BUFS */
Eric Andersen9615a082004-07-15 12:53:49 +0000607
Denis Vlasenko22284262008-09-18 00:56:24 +0000608static gid_t *getgrouplist_internal(int *ngroups_ptr, const char *user, gid_t gid)
Eric Andersen9615a082004-07-15 12:53:49 +0000609{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000610 FILE *grfile;
Eric Andersen9615a082004-07-15 12:53:49 +0000611 gid_t *group_list;
Denis Vlasenko22284262008-09-18 00:56:24 +0000612 int ngroups;
Eric Andersen9615a082004-07-15 12:53:49 +0000613 struct group group;
614 char buff[PWD_BUFFER_SIZE];
615
Denis Vlasenko22284262008-09-18 00:56:24 +0000616 /* We alloc space for 8 gids at a time. */
617 group_list = xmalloc(8 * sizeof(group_list[0]));
618 group_list[0] = gid;
619 ngroups = 1;
620
Denis Vlasenko5415c852008-07-21 23:05:26 +0000621 grfile = fopen_for_read(_PATH_GROUP);
Denis Vlasenko22284262008-09-18 00:56:24 +0000622 if (grfile) {
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000623 while (!bb__pgsreader(bb__parsegrent, &group, buff, sizeof(buff), grfile)) {
Denis Vlasenko22284262008-09-18 00:56:24 +0000624 char **m;
Eric Andersen9615a082004-07-15 12:53:49 +0000625 assert(group.gr_mem); /* Must have at least a NULL terminator. */
Denis Vlasenko22284262008-09-18 00:56:24 +0000626 if (group.gr_gid == gid)
627 continue;
628 for (m = group.gr_mem; *m; m++) {
629 if (strcmp(*m, user) != 0)
630 continue;
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200631 group_list = xrealloc_vector(group_list, /*8=2^3:*/ 3, ngroups);
Denis Vlasenko22284262008-09-18 00:56:24 +0000632 group_list[ngroups++] = group.gr_gid;
633 break;
Eric Andersen9615a082004-07-15 12:53:49 +0000634 }
635 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000636 fclose(grfile);
Eric Andersen9615a082004-07-15 12:53:49 +0000637 }
Denis Vlasenko22284262008-09-18 00:56:24 +0000638 *ngroups_ptr = ngroups;
639 return group_list;
640}
Eric Andersen9615a082004-07-15 12:53:49 +0000641
Denis Vlasenko22284262008-09-18 00:56:24 +0000642int initgroups(const char *user, gid_t gid)
643{
644 int ngroups;
645 gid_t *group_list = getgrouplist_internal(&ngroups, user, gid);
646
Denis Vlasenko22284262008-09-18 00:56:24 +0000647 ngroups = setgroups(ngroups, group_list);
648 free(group_list);
649 return ngroups;
650}
651
Denis Vlasenko22284262008-09-18 00:56:24 +0000652int getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups)
653{
654 int ngroups_old = *ngroups;
655 gid_t *group_list = getgrouplist_internal(ngroups, user, gid);
656
657 if (*ngroups <= ngroups_old) {
658 ngroups_old = *ngroups;
659 memcpy(groups, group_list, ngroups_old * sizeof(groups[0]));
660 } else {
661 ngroups_old = -1;
662 }
663 free(group_list);
664 return ngroups_old;
Eric Andersen9615a082004-07-15 12:53:49 +0000665}
666
Denys Vlasenko55301292010-03-31 12:38:17 +0200667#ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
Eric Andersen9615a082004-07-15 12:53:49 +0000668int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
669{
670 int rv = -1;
671
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200672#if 0
673 /* glibc does this check */
Eric Andersen9615a082004-07-15 12:53:49 +0000674 if (!p || !f) {
Denis Vlasenko92305822008-03-20 15:12:58 +0000675 errno = EINVAL;
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200676 return rv;
677 }
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200678#endif
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200679
680 /* No extra thread locking is needed above what fprintf does. */
681 if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n",
682 p->pw_name, p->pw_passwd,
683 (unsigned long)(p->pw_uid),
684 (unsigned long)(p->pw_gid),
685 p->pw_gecos, p->pw_dir, p->pw_shell) >= 0
686 ) {
687 rv = 0;
Eric Andersen9615a082004-07-15 12:53:49 +0000688 }
689
690 return rv;
691}
692
Eric Andersen9615a082004-07-15 12:53:49 +0000693int putgrent(const struct group *__restrict p, FILE *__restrict f)
694{
Eric Andersen9615a082004-07-15 12:53:49 +0000695 int rv = -1;
696
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200697#if 0
698 /* glibc does this check */
699 if (!p || !f) {
Denis Vlasenko92305822008-03-20 15:12:58 +0000700 errno = EINVAL;
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200701 return rv;
702 }
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200703#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000704
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200705 if (fprintf(f, "%s:%s:%lu:",
706 p->gr_name, p->gr_passwd,
707 (unsigned long)(p->gr_gid)) >= 0
708 ) {
709 static const char format[] ALIGN1 = ",%s";
Eric Andersen9615a082004-07-15 12:53:49 +0000710
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200711 char **m;
712 const char *fmt;
Eric Andersen9615a082004-07-15 12:53:49 +0000713
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200714 fmt = format + 1;
715
716 assert(p->gr_mem);
717 m = p->gr_mem;
718
719 while (1) {
720 if (!*m) {
721 if (fputc('\n', f) >= 0) {
722 rv = 0;
Eric Andersen9615a082004-07-15 12:53:49 +0000723 }
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200724 break;
Denys Vlasenko9e59e272010-03-31 10:31:51 +0200725 }
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200726 if (fprintf(f, fmt, *m) < 0) {
727 break;
728 }
729 m++;
730 fmt = format;
Eric Andersen9615a082004-07-15 12:53:49 +0000731 }
Eric Andersen9615a082004-07-15 12:53:49 +0000732 }
733
734 return rv;
735}
Denys Vlasenko55301292010-03-31 12:38:17 +0200736#endif
Eric Andersen9615a082004-07-15 12:53:49 +0000737
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000738#if ENABLE_USE_BB_SHADOW
Denys Vlasenko05d1a322010-04-02 12:12:43 +0200739#ifdef UNUSED_FOR_NOW
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200740static const unsigned char put_sp_off[] ALIGN1 = {
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000741 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
742 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
743 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
744 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
745 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
746 offsetof(struct spwd, sp_expire) /* 7 - not a char ptr */
Eric Andersen9615a082004-07-15 12:53:49 +0000747};
748
749int putspent(const struct spwd *p, FILE *stream)
750{
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200751 const char *fmt;
Denis Vlasenko87468852007-04-13 23:22:00 +0000752 long x;
Eric Andersen9615a082004-07-15 12:53:49 +0000753 int i;
754 int rv = -1;
755
756 /* Unlike putpwent and putgrent, glibc does not check the args. */
757 if (fprintf(stream, "%s:%s:", p->sp_namp,
758 (p->sp_pwdp ? p->sp_pwdp : "")) < 0
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000759 ) {
Eric Andersen9615a082004-07-15 12:53:49 +0000760 goto DO_UNLOCK;
761 }
762
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200763 for (i = 0; i < sizeof(put_sp_off); i++) {
764 fmt = "%ld:";
765 x = *(long *)((char *)p + put_sp_off[i]);
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000766 if (x == -1) {
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200767 fmt += 3;
Eric Andersen9615a082004-07-15 12:53:49 +0000768 }
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200769 if (fprintf(stream, fmt, x) < 0) {
Eric Andersen9615a082004-07-15 12:53:49 +0000770 goto DO_UNLOCK;
771 }
772 }
773
774 if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) {
775 goto DO_UNLOCK;
776 }
777
Rob Landley25413bf2005-10-08 02:23:22 +0000778 if (fputc('\n', stream) > 0) {
Eric Andersen9615a082004-07-15 12:53:49 +0000779 rv = 0;
780 }
781
Denys Vlasenko9e59e272010-03-31 10:31:51 +0200782 DO_UNLOCK:
Eric Andersen9615a082004-07-15 12:53:49 +0000783 return rv;
784}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000785#endif
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200786#endif /* USE_BB_SHADOW */
Eric Andersen9615a082004-07-15 12:53:49 +0000787
Eric Andersen9615a082004-07-15 12:53:49 +0000788/**********************************************************************/
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200789/* Internal functions */
Eric Andersen9615a082004-07-15 12:53:49 +0000790/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000791
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000792static const unsigned char pw_off[] ALIGN1 = {
793 offsetof(struct passwd, pw_name), /* 0 */
794 offsetof(struct passwd, pw_passwd), /* 1 */
795 offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */
796 offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */
797 offsetof(struct passwd, pw_gecos), /* 4 */
798 offsetof(struct passwd, pw_dir), /* 5 */
799 offsetof(struct passwd, pw_shell) /* 6 */
Eric Andersen9615a082004-07-15 12:53:49 +0000800};
801
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200802static int FAST_FUNC bb__parsepwent(void *data, char *line)
Eric Andersen9615a082004-07-15 12:53:49 +0000803{
804 char *endptr;
805 char *p;
806 int i;
807
808 i = 0;
Denys Vlasenko9e59e272010-03-31 10:31:51 +0200809 while (1) {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200810 p = (char *) data + pw_off[i];
Eric Andersen9615a082004-07-15 12:53:49 +0000811
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200812 if (i < 2 || i > 3) {
Eric Andersen9615a082004-07-15 12:53:49 +0000813 *((char **) p) = line;
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200814 if (i == 6) {
Eric Andersen9615a082004-07-15 12:53:49 +0000815 return 0;
816 }
817 /* NOTE: glibc difference - glibc allows omission of
818 * ':' seperators after the gid field if all remaining
819 * entries are empty. We require all separators. */
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000820 line = strchr(line, ':');
821 if (!line) {
Eric Andersen9615a082004-07-15 12:53:49 +0000822 break;
823 }
824 } else {
825 unsigned long t = strtoul(line, &endptr, 10);
826 /* Make sure we had at least one digit, and that the
827 * failing char is the next field seperator ':'. See
828 * glibc difference note above. */
829 /* TODO: Also check for leading whitespace? */
830 if ((endptr == line) || (*endptr != ':')) {
831 break;
832 }
833 line = endptr;
834 if (i & 1) { /* i == 3 -- gid */
835 *((gid_t *) p) = t;
836 } else { /* i == 2 -- uid */
837 *((uid_t *) p) = t;
838 }
839 }
840
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200841 *line++ = '\0';
842 i++;
Denys Vlasenko9e59e272010-03-31 10:31:51 +0200843 } /* while (1) */
Eric Andersen9615a082004-07-15 12:53:49 +0000844
845 return -1;
846}
847
Eric Andersen9615a082004-07-15 12:53:49 +0000848/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000849
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000850static const unsigned char gr_off[] ALIGN1 = {
851 offsetof(struct group, gr_name), /* 0 */
852 offsetof(struct group, gr_passwd), /* 1 */
853 offsetof(struct group, gr_gid) /* 2 - not a char ptr */
Eric Andersen9615a082004-07-15 12:53:49 +0000854};
855
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200856static int FAST_FUNC bb__parsegrent(void *data, char *line)
Eric Andersen9615a082004-07-15 12:53:49 +0000857{
858 char *endptr;
859 char *p;
860 int i;
861 char **members;
862 char *end_of_buf;
863
864 end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */
865 i = 0;
Denys Vlasenko9e59e272010-03-31 10:31:51 +0200866 while (1) {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200867 p = (char *) data + gr_off[i];
Eric Andersen9615a082004-07-15 12:53:49 +0000868
869 if (i < 2) {
870 *((char **) p) = line;
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000871 line = strchr(line, ':');
872 if (!line) {
Eric Andersen9615a082004-07-15 12:53:49 +0000873 break;
874 }
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200875 *line++ = '\0';
876 i++;
Eric Andersen9615a082004-07-15 12:53:49 +0000877 } else {
878 *((gid_t *) p) = strtoul(line, &endptr, 10);
879
880 /* NOTE: glibc difference - glibc allows omission of the
881 * trailing colon when there is no member list. We treat
882 * this as an error. */
883
884 /* Make sure we had at least one digit, and that the
885 * failing char is the next field seperator ':'. See
886 * glibc difference note above. */
887 if ((endptr == line) || (*endptr != ':')) {
888 break;
889 }
890
891 i = 1; /* Count terminating NULL ptr. */
892 p = endptr;
893
894 if (p[1]) { /* We have a member list to process. */
895 /* Overwrite the last ':' with a ',' before counting.
Denys Vlasenko8d22ca82010-03-31 14:43:58 +0200896 * This allows us to (1) test for initial ','
897 * and (2) adds one ',' so that the number of commas
898 * equals the member count. */
Eric Andersen9615a082004-07-15 12:53:49 +0000899 *p = ',';
900 do {
901 /* NOTE: glibc difference - glibc allows and trims leading
902 * (but not trailing) space. We treat this as an error. */
903 /* NOTE: glibc difference - glibc allows consecutive and
904 * trailing commas, and ignores "empty string" users. We
905 * treat this as an error. */
906 if (*p == ',') {
907 ++i;
908 *p = 0; /* nul-terminate each member string. */
909 if (!*++p || (*p == ',') || isspace(*p)) {
910 goto ERR;
911 }
912 }
913 } while (*++p);
914 }
915
916 /* Now align (p+1), rounding up. */
917 /* Assumes sizeof(char **) is a power of 2. */
918 members = (char **)( (((intptr_t) p) + sizeof(char **))
919 & ~((intptr_t)(sizeof(char **) - 1)) );
920
921 if (((char *)(members + i)) > end_of_buf) { /* No space. */
922 break;
923 }
924
925 ((struct group *) data)->gr_mem = members;
926
927 if (--i) {
928 p = endptr; /* Pointing to char prior to first member. */
Denys Vlasenko9e59e272010-03-31 10:31:51 +0200929 while (1) {
Eric Andersen9615a082004-07-15 12:53:49 +0000930 *members++ = ++p;
Denys Vlasenko9e59e272010-03-31 10:31:51 +0200931 if (!--i)
932 break;
933 while (*++p)
934 continue;
935 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000936 }
Eric Andersen9615a082004-07-15 12:53:49 +0000937 *members = NULL;
938
939 return 0;
940 }
Denys Vlasenko9e59e272010-03-31 10:31:51 +0200941 } /* while (1) */
Eric Andersen9615a082004-07-15 12:53:49 +0000942
943 ERR:
944 return -1;
945}
946
Eric Andersen9615a082004-07-15 12:53:49 +0000947/**********************************************************************/
Denis Vlasenkocb04ff52006-12-30 21:11:57 +0000948
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000949#if ENABLE_USE_BB_SHADOW
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000950static const unsigned char sp_off[] ALIGN1 = {
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200951 offsetof(struct spwd, sp_namp), /* 0: char* */
952 offsetof(struct spwd, sp_pwdp), /* 1: char* */
953 offsetof(struct spwd, sp_lstchg), /* 2: long */
954 offsetof(struct spwd, sp_min), /* 3: long */
955 offsetof(struct spwd, sp_max), /* 4: long */
956 offsetof(struct spwd, sp_warn), /* 5: long */
957 offsetof(struct spwd, sp_inact), /* 6: long */
958 offsetof(struct spwd, sp_expire), /* 7: long */
959 offsetof(struct spwd, sp_flag) /* 8: unsigned long */
Eric Andersen9615a082004-07-15 12:53:49 +0000960};
961
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200962static int FAST_FUNC bb__parsespent(void *data, char *line)
Eric Andersen9615a082004-07-15 12:53:49 +0000963{
964 char *endptr;
965 char *p;
966 int i;
967
968 i = 0;
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200969 while (1) {
970 p = (char *) data + sp_off[i];
Eric Andersen9615a082004-07-15 12:53:49 +0000971 if (i < 2) {
972 *((char **) p) = line;
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000973 line = strchr(line, ':');
974 if (!line) {
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200975 break; /* error */
Eric Andersen9615a082004-07-15 12:53:49 +0000976 }
977 } else {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200978 *((long *) p) = strtoul(line, &endptr, 10);
Eric Andersen9615a082004-07-15 12:53:49 +0000979 if (endptr == line) {
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200980 *((long *) p) = -1L;
Eric Andersen9615a082004-07-15 12:53:49 +0000981 }
Eric Andersen9615a082004-07-15 12:53:49 +0000982 line = endptr;
Eric Andersen9615a082004-07-15 12:53:49 +0000983 if (i == 8) {
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200984 if (*line != '\0') {
985 break; /* error */
Eric Andersen9615a082004-07-15 12:53:49 +0000986 }
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200987 return 0; /* all ok */
Eric Andersen9615a082004-07-15 12:53:49 +0000988 }
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200989 if (*line != ':') {
990 break; /* error */
Eric Andersen9615a082004-07-15 12:53:49 +0000991 }
Eric Andersen9615a082004-07-15 12:53:49 +0000992 }
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200993 *line++ = '\0';
Denys Vlasenko17fcd722010-03-31 12:37:43 +0200994 i++;
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200995 }
Eric Andersen9615a082004-07-15 12:53:49 +0000996
997 return EINVAL;
998}
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000999#endif
Eric Andersen9615a082004-07-15 12:53:49 +00001000
Eric Andersen9615a082004-07-15 12:53:49 +00001001/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +00001002
Denys Vlasenko05d1a322010-04-02 12:12:43 +02001003/* Reads until EOF, or until it finds a line which fits in the buffer
Eric Andersen9615a082004-07-15 12:53:49 +00001004 * and for which the parser function succeeds.
1005 *
Denys Vlasenko05d1a322010-04-02 12:12:43 +02001006 * Returns 0 on success and ENOENT for end-of-file (glibc convention).
Eric Andersen9615a082004-07-15 12:53:49 +00001007 */
Denys Vlasenko17fcd722010-03-31 12:37:43 +02001008static int bb__pgsreader(
1009 int FAST_FUNC (*parserfunc)(void *d, char *line),
1010 void *data,
1011 char *__restrict line_buff,
1012 size_t buflen,
1013 FILE *f)
Eric Andersen9615a082004-07-15 12:53:49 +00001014{
Eric Andersen9615a082004-07-15 12:53:49 +00001015 int skip;
1016 int rv = ERANGE;
1017
1018 if (buflen < PWD_BUFFER_SIZE) {
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +00001019 errno = rv;
Denys Vlasenko57dc3c72010-03-31 10:24:37 +02001020 return rv;
1021 }
1022
1023 skip = 0;
1024 while (1) {
1025 if (!fgets(line_buff, buflen, f)) {
1026 if (feof(f)) {
1027 rv = ENOENT;
1028 }
1029 break;
1030 }
1031
1032 {
1033 int line_len = strlen(line_buff) - 1;
1034 if (line_len >= 0 && line_buff[line_len] == '\n') {
1035 line_buff[line_len] = '\0';
1036 } else
1037 if (line_len + 2 == buflen) {
1038 /* A start (or continuation) of overlong line */
1039 skip = 1;
1040 continue;
1041 } /* else: a last line in the file, and it has no '\n' */
1042 }
1043
1044 if (skip) {
1045 /* This "line" is a remainder of overlong line, ignore */
1046 skip = 0;
1047 continue;
1048 }
1049
1050 /* NOTE: glibc difference - glibc strips leading whitespace from
1051 * records. We do not allow leading whitespace. */
1052
1053 /* Skip empty lines, comment lines, and lines with leading
1054 * whitespace. */
1055 if (line_buff[0] != '\0' && line_buff[0] != '#' && !isspace(line_buff[0])) {
1056 if (parserfunc == bb__parsegrent) {
1057 /* Do evil group hack:
1058 * The group entry parsing function needs to know where
1059 * the end of the buffer is so that it can construct the
1060 * group member ptr table. */
1061 ((struct group *) data)->gr_name = line_buff + buflen;
1062 }
1063 if (parserfunc(data, line_buff) == 0) {
1064 rv = 0;
Eric Andersen9615a082004-07-15 12:53:49 +00001065 break;
1066 }
Denys Vlasenko57dc3c72010-03-31 10:24:37 +02001067 }
1068 } /* while (1) */
Eric Andersen9615a082004-07-15 12:53:49 +00001069
1070 return rv;
1071}