blob: ac65d4c5bc2386e26fb3095e18d39d44f114424c [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Eric Andersen9615a082004-07-15 12:53:49 +00002/* Copyright (C) 2003 Manuel Novoa III
3 *
Rob Landley25413bf2005-10-08 02:23:22 +00004 * Licensed under GPL v2, or later. See file LICENSE in this tarball.
Eric Andersen9615a082004-07-15 12:53:49 +00005 */
6
7/* Nov 6, 2003 Initial version.
8 *
9 * NOTE: This implementation is quite strict about requiring all
10 * field seperators. It also does not allow leading whitespace
11 * except when processing the numeric fields. glibc is more
12 * lenient. See the various glibc difference comments below.
13 *
14 * TODO:
Rob Landley06ec8cf2006-03-03 19:02:50 +000015 * Move to dynamic allocation of (currently statically allocated)
Eric Andersen9615a082004-07-15 12:53:49 +000016 * buffers; especially for the group-related functions since
17 * large group member lists will cause error returns.
18 *
19 */
20
Rob Landleyea224be2006-06-18 20:20:07 +000021#include "libbb.h"
Eric Andersen9615a082004-07-15 12:53:49 +000022#include <features.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <stdint.h>
26#include <string.h>
27#include <stddef.h>
28#include <errno.h>
29#include <assert.h>
30#include <ctype.h>
Bernhard Reutner-Fischerfa939aa2006-04-05 16:21:37 +000031
Eric Andersen9615a082004-07-15 12:53:49 +000032#ifndef _PATH_SHADOW
33#define _PATH_SHADOW "/etc/shadow"
34#endif
35#ifndef _PATH_PASSWD
36#define _PATH_PASSWD "/etc/passwd"
37#endif
38#ifndef _PATH_GROUP
39#define _PATH_GROUP "/etc/group"
40#endif
41
42/**********************************************************************/
Rob Landley06ec8cf2006-03-03 19:02:50 +000043/* Sizes for statically allocated buffers. */
Eric Andersen9615a082004-07-15 12:53:49 +000044
45/* If you change these values, also change _SC_GETPW_R_SIZE_MAX and
46 * _SC_GETGR_R_SIZE_MAX in libc/unistd/sysconf.c to match */
47#define PWD_BUFFER_SIZE 256
48#define GRP_BUFFER_SIZE 256
49
50/**********************************************************************/
51/* Prototypes for internal functions. */
52
53extern int __parsepwent(void *pw, char *line);
54extern int __parsegrent(void *gr, char *line);
55extern int __parsespent(void *sp, char *line);
56
57extern int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,
Denis Vlasenko7d219aa2006-10-05 10:17:08 +000058 char *__restrict line_buff, size_t buflen, FILE *f);
Eric Andersen9615a082004-07-15 12:53:49 +000059
60/**********************************************************************/
61/* For the various fget??ent_r funcs, return
62 *
63 * 0: success
64 * ENOENT: end-of-file encountered
65 * ERANGE: buflen too small
66 * other error values possible. See __pgsreader.
67 *
68 * Also, *result == resultbuf on success and NULL on failure.
69 *
70 * NOTE: glibc difference - For the ENOENT case, glibc also sets errno.
71 * We do not, as it really isn't an error if we reach the end-of-file.
72 * Doing so is analogous to having fgetc() set errno on EOF.
73 */
74/**********************************************************************/
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +000075
Eric Andersen9615a082004-07-15 12:53:49 +000076int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf,
77 char *__restrict buffer, size_t buflen,
78 struct passwd **__restrict result)
79{
80 int rv;
81
82 *result = NULL;
83
84 if (!(rv = __pgsreader(__parsepwent, resultbuf, buffer, buflen, stream))) {
85 *result = resultbuf;
86 }
87
88 return rv;
89}
90
Eric Andersen9615a082004-07-15 12:53:49 +000091int fgetgrent_r(FILE *__restrict stream, struct group *__restrict resultbuf,
92 char *__restrict buffer, size_t buflen,
93 struct group **__restrict result)
94{
95 int rv;
96
97 *result = NULL;
98
99 if (!(rv = __pgsreader(__parsegrent, resultbuf, buffer, buflen, stream))) {
100 *result = resultbuf;
101 }
102
103 return rv;
104}
105
Eric Andersen9615a082004-07-15 12:53:49 +0000106int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
107 char *__restrict buffer, size_t buflen,
108 struct spwd **__restrict result)
109{
110 int rv;
111
112 *result = NULL;
113
114 if (!(rv = __pgsreader(__parsespent, resultbuf, buffer, buflen, stream))) {
115 *result = resultbuf;
116 }
117
118 return rv;
119}
120
Eric Andersen9615a082004-07-15 12:53:49 +0000121/**********************************************************************/
122/* For the various fget??ent funcs, return NULL on failure and a
Rob Landley06ec8cf2006-03-03 19:02:50 +0000123 * pointer to the appropriate struct (statically allocated) on success.
Eric Andersen9615a082004-07-15 12:53:49 +0000124 */
125/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000126
127struct passwd *fgetpwent(FILE *stream)
128{
129 static char buffer[PWD_BUFFER_SIZE];
130 static struct passwd resultbuf;
131 struct passwd *result;
132
133 fgetpwent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
134 return result;
135}
136
Eric Andersen9615a082004-07-15 12:53:49 +0000137struct group *fgetgrent(FILE *stream)
138{
139 static char buffer[GRP_BUFFER_SIZE];
140 static struct group resultbuf;
141 struct group *result;
142
143 fgetgrent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
144 return result;
145}
146
Eric Andersen9615a082004-07-15 12:53:49 +0000147extern int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
148 char *__restrict buffer, size_t buflen,
149 struct spwd **__restrict result);
150struct spwd *fgetspent(FILE *stream)
151{
152 static char buffer[PWD_BUFFER_SIZE];
153 static struct spwd resultbuf;
154 struct spwd *result;
155
156 fgetspent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
157 return result;
158}
159
Eric Andersen9615a082004-07-15 12:53:49 +0000160int sgetspent_r(const char *string, struct spwd *result_buf,
161 char *buffer, size_t buflen, struct spwd **result)
162{
163 int rv = ERANGE;
164
165 *result = NULL;
166
167 if (buflen < PWD_BUFFER_SIZE) {
168 DO_ERANGE:
169 errno=rv;
170 goto DONE;
171 }
172
173 if (string != buffer) {
174 if (strlen(string) >= buflen) {
175 goto DO_ERANGE;
176 }
177 strcpy(buffer, string);
178 }
179
180 if (!(rv = __parsespent(result_buf, buffer))) {
181 *result = result_buf;
182 }
183
184 DONE:
185 return rv;
186}
187
Eric Andersen9615a082004-07-15 12:53:49 +0000188/**********************************************************************/
189
190#ifdef GETXXKEY_R_FUNC
191#error GETXXKEY_R_FUNC is already defined!
192#endif
193
Denis Vlasenko7d219aa2006-10-05 10:17:08 +0000194#define GETXXKEY_R_FUNC getpwnam_R
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000195#define GETXXKEY_R_PARSER __parsepwent
Eric Andersen9615a082004-07-15 12:53:49 +0000196#define GETXXKEY_R_ENTTYPE struct passwd
197#define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key))
198#define DO_GETXXKEY_R_KEYTYPE const char *__restrict
199#define DO_GETXXKEY_R_PATHNAME _PATH_PASSWD
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000200#include "pwd_grp_internal.c"
Eric Andersen9615a082004-07-15 12:53:49 +0000201
Denis Vlasenko7d219aa2006-10-05 10:17:08 +0000202#define GETXXKEY_R_FUNC getgrnam_R
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000203#define GETXXKEY_R_PARSER __parsegrent
Eric Andersen9615a082004-07-15 12:53:49 +0000204#define GETXXKEY_R_ENTTYPE struct group
205#define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key))
206#define DO_GETXXKEY_R_KEYTYPE const char *__restrict
207#define DO_GETXXKEY_R_PATHNAME _PATH_GROUP
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000208#include "pwd_grp_internal.c"
Eric Andersen9615a082004-07-15 12:53:49 +0000209
Denis Vlasenko7d219aa2006-10-05 10:17:08 +0000210#define GETXXKEY_R_FUNC getspnam_R
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000211#define GETXXKEY_R_PARSER __parsespent
Eric Andersen9615a082004-07-15 12:53:49 +0000212#define GETXXKEY_R_ENTTYPE struct spwd
213#define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key))
214#define DO_GETXXKEY_R_KEYTYPE const char *__restrict
215#define DO_GETXXKEY_R_PATHNAME _PATH_SHADOW
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000216#include "pwd_grp_internal.c"
Eric Andersen9615a082004-07-15 12:53:49 +0000217
Denis Vlasenko7d219aa2006-10-05 10:17:08 +0000218#define GETXXKEY_R_FUNC getpwuid_R
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000219#define GETXXKEY_R_PARSER __parsepwent
Eric Andersen9615a082004-07-15 12:53:49 +0000220#define GETXXKEY_R_ENTTYPE struct passwd
221#define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key)
222#define DO_GETXXKEY_R_KEYTYPE uid_t
223#define DO_GETXXKEY_R_PATHNAME _PATH_PASSWD
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000224#include "pwd_grp_internal.c"
Eric Andersen9615a082004-07-15 12:53:49 +0000225
Denis Vlasenko7d219aa2006-10-05 10:17:08 +0000226#define GETXXKEY_R_FUNC getgrgid_R
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000227#define GETXXKEY_R_PARSER __parsegrent
Eric Andersen9615a082004-07-15 12:53:49 +0000228#define GETXXKEY_R_ENTTYPE struct group
229#define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key)
230#define DO_GETXXKEY_R_KEYTYPE gid_t
231#define DO_GETXXKEY_R_PATHNAME _PATH_GROUP
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000232#include "pwd_grp_internal.c"
Eric Andersen9615a082004-07-15 12:53:49 +0000233
234/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000235
236struct passwd *getpwuid(uid_t uid)
237{
238 static char buffer[PWD_BUFFER_SIZE];
239 static struct passwd resultbuf;
240 struct passwd *result;
241
242 getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result);
243 return result;
244}
245
Eric Andersen9615a082004-07-15 12:53:49 +0000246struct group *getgrgid(gid_t gid)
247{
248 static char buffer[GRP_BUFFER_SIZE];
249 static struct group resultbuf;
250 struct group *result;
251
252 getgrgid_r(gid, &resultbuf, buffer, sizeof(buffer), &result);
253 return result;
254}
255
Eric Andersen9615a082004-07-15 12:53:49 +0000256/* This function is non-standard and is currently not built. It seems
257 * to have been created as a reentrant version of the non-standard
258 * functions getspuid. Why getspuid was added, I do not know. */
259
260int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf,
261 char *__restrict buffer, size_t buflen,
262 struct spwd **__restrict result)
263{
264 int rv;
265 struct passwd *pp;
266 struct passwd password;
267 char pwd_buff[PWD_BUFFER_SIZE];
268
269 *result = NULL;
270 if (!(rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp))) {
271 rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result);
272 }
273
274 return rv;
275}
276
Eric Andersen9615a082004-07-15 12:53:49 +0000277/* This function is non-standard and is currently not built.
278 * Why it was added, I do not know. */
279
280struct spwd *getspuid(uid_t uid)
281{
282 static char buffer[PWD_BUFFER_SIZE];
283 static struct spwd resultbuf;
284 struct spwd *result;
285
286 getspuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result);
287 return result;
288}
289
Eric Andersen9615a082004-07-15 12:53:49 +0000290struct passwd *getpwnam(const char *name)
291{
292 static char buffer[PWD_BUFFER_SIZE];
293 static struct passwd resultbuf;
294 struct passwd *result;
295
296 getpwnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
297 return result;
298}
299
Eric Andersen9615a082004-07-15 12:53:49 +0000300struct group *getgrnam(const char *name)
301{
302 static char buffer[GRP_BUFFER_SIZE];
303 static struct group resultbuf;
304 struct group *result;
305
306 getgrnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
307 return result;
308}
309
Eric Andersen9615a082004-07-15 12:53:49 +0000310struct spwd *getspnam(const char *name)
311{
312 static char buffer[PWD_BUFFER_SIZE];
313 static struct spwd resultbuf;
314 struct spwd *result;
315
316 getspnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
317 return result;
318}
319
Eric Andersen9615a082004-07-15 12:53:49 +0000320int getpw(uid_t uid, char *buf)
321{
322 struct passwd resultbuf;
323 struct passwd *result;
324 char buffer[PWD_BUFFER_SIZE];
325
326 if (!buf) {
327 errno=EINVAL;
328 } else if (!getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result)) {
329 if (sprintf(buf, "%s:%s:%lu:%lu:%s:%s:%s\n",
330 resultbuf.pw_name, resultbuf.pw_passwd,
331 (unsigned long)(resultbuf.pw_uid),
332 (unsigned long)(resultbuf.pw_gid),
333 resultbuf.pw_gecos, resultbuf.pw_dir,
334 resultbuf.pw_shell) >= 0
335 ) {
336 return 0;
337 }
338 }
339
340 return -1;
341}
342
Eric Andersen9615a082004-07-15 12:53:49 +0000343/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000344
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000345#if defined CONFIG_USE_BB_THREADSAFE_SHADOW && defined PTHREAD_MUTEX_INITIALIZER
346static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
347# define LOCK pthread_mutex_lock(&mylock)
348# define UNLOCK pthread_mutex_unlock(&mylock);
349#else
350# define LOCK ((void) 0)
351# define UNLOCK ((void) 0)
352#endif
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000353
Eric Andersen9615a082004-07-15 12:53:49 +0000354static FILE *pwf /*= NULL*/;
355void setpwent(void)
356{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000357 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000358 if (pwf) {
359 rewind(pwf);
360 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000361 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000362}
363
364void endpwent(void)
365{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000366 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000367 if (pwf) {
368 fclose(pwf);
369 pwf = NULL;
370 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000371 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000372}
373
374
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000375int getpwent_r(struct passwd *__restrict resultbuf,
Eric Andersen9615a082004-07-15 12:53:49 +0000376 char *__restrict buffer, size_t buflen,
377 struct passwd **__restrict result)
378{
379 int rv;
380
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000381 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000382 *result = NULL; /* In case of error... */
383
384 if (!pwf) {
385 if (!(pwf = fopen(_PATH_PASSWD, "r"))) {
386 rv = errno;
387 goto ERR;
388 }
389 }
390
391 if (!(rv = __pgsreader(__parsepwent, resultbuf,
392 buffer, buflen, pwf))) {
393 *result = resultbuf;
394 }
395
396 ERR:
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000397 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000398 return rv;
399}
400
Eric Andersen9615a082004-07-15 12:53:49 +0000401static FILE *grf /*= NULL*/;
402void setgrent(void)
403{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000404 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000405 if (grf) {
406 rewind(grf);
407 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000408 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000409}
410
411void endgrent(void)
412{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000413 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000414 if (grf) {
415 fclose(grf);
416 grf = NULL;
417 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000418 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000419}
420
421int getgrent_r(struct group *__restrict resultbuf,
422 char *__restrict buffer, size_t buflen,
423 struct group **__restrict result)
424{
425 int rv;
426
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000427 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000428 *result = NULL; /* In case of error... */
429
430 if (!grf) {
431 if (!(grf = fopen(_PATH_GROUP, "r"))) {
432 rv = errno;
433 goto ERR;
434 }
435 }
436
437 if (!(rv = __pgsreader(__parsegrent, resultbuf,
438 buffer, buflen, grf))) {
439 *result = resultbuf;
440 }
441
442 ERR:
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000443 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000444 return rv;
445}
446
Eric Andersen9615a082004-07-15 12:53:49 +0000447static FILE *spf /*= NULL*/;
448void setspent(void)
449{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000450 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000451 if (spf) {
452 rewind(spf);
453 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000454 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000455}
456
457void endspent(void)
458{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000459 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000460 if (spf) {
461 fclose(spf);
462 spf = NULL;
463 }
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000464 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000465}
466
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000467int getspent_r(struct spwd *resultbuf, char *buffer,
Eric Andersen9615a082004-07-15 12:53:49 +0000468 size_t buflen, struct spwd **result)
469{
470 int rv;
471
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000472 LOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000473 *result = NULL; /* In case of error... */
474
475 if (!spf) {
476 if (!(spf = fopen(_PATH_SHADOW, "r"))) {
477 rv = errno;
478 goto ERR;
479 }
480 }
481
482 if (!(rv = __pgsreader(__parsespent, resultbuf,
483 buffer, buflen, spf))) {
484 *result = resultbuf;
485 }
486
487 ERR:
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000488 UNLOCK;
Eric Andersen9615a082004-07-15 12:53:49 +0000489 return rv;
490}
491
Eric Andersen9615a082004-07-15 12:53:49 +0000492struct passwd *getpwent(void)
493{
494 static char line_buff[PWD_BUFFER_SIZE];
495 static struct passwd pwd;
496 struct passwd *result;
497
498 getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
499 return result;
500}
501
Eric Andersen9615a082004-07-15 12:53:49 +0000502struct group *getgrent(void)
503{
504 static char line_buff[GRP_BUFFER_SIZE];
505 static struct group gr;
506 struct group *result;
507
508 getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
509 return result;
510}
511
Eric Andersen9615a082004-07-15 12:53:49 +0000512struct spwd *getspent(void)
513{
514 static char line_buff[PWD_BUFFER_SIZE];
515 static struct spwd spwd;
516 struct spwd *result;
517
518 getspent_r(&spwd, line_buff, sizeof(line_buff), &result);
519 return result;
520}
521
Eric Andersen9615a082004-07-15 12:53:49 +0000522struct spwd *sgetspent(const char *string)
523{
524 static char line_buff[PWD_BUFFER_SIZE];
525 static struct spwd spwd;
526 struct spwd *result;
527
528 sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result);
529 return result;
530}
531
Eric Andersen9615a082004-07-15 12:53:49 +0000532int initgroups(const char *user, gid_t gid)
533{
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000534 FILE *grfile;
Eric Andersen9615a082004-07-15 12:53:49 +0000535 gid_t *group_list;
536 int num_groups, rv;
537 char **m;
538 struct group group;
539 char buff[PWD_BUFFER_SIZE];
540
541 rv = -1;
542
543 /* We alloc space for 8 gids at a time. */
544 if (((group_list = (gid_t *) malloc(8*sizeof(gid_t *))) != NULL)
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000545 && ((grfile = fopen(_PATH_GROUP, "r")) != NULL)
Eric Andersen9615a082004-07-15 12:53:49 +0000546 ) {
547
548 *group_list = gid;
549 num_groups = 1;
550
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000551 while (!__pgsreader(__parsegrent, &group, buff, sizeof(buff), grfile)) {
Eric Andersen9615a082004-07-15 12:53:49 +0000552 assert(group.gr_mem); /* Must have at least a NULL terminator. */
553 if (group.gr_gid != gid) {
554 for (m=group.gr_mem ; *m ; m++) {
555 if (!strcmp(*m, user)) {
556 if (!(num_groups & 7)) {
557 gid_t *tmp = (gid_t *)
558 realloc(group_list,
559 (num_groups+8) * sizeof(gid_t *));
560 if (!tmp) {
561 rv = -1;
562 goto DO_CLOSE;
563 }
564 group_list = tmp;
565 }
566 group_list[num_groups++] = group.gr_gid;
567 break;
568 }
569 }
570 }
571 }
572
573 rv = setgroups(num_groups, group_list);
574 DO_CLOSE:
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000575 fclose(grfile);
Eric Andersen9615a082004-07-15 12:53:49 +0000576 }
577
578 /* group_list will be NULL if initial malloc failed, which may trigger
579 * warnings from various malloc debuggers. */
580 free(group_list);
581 return rv;
582}
583
Eric Andersen9615a082004-07-15 12:53:49 +0000584int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
585{
586 int rv = -1;
587
588 if (!p || !f) {
589 errno=EINVAL;
590 } else {
591 /* No extra thread locking is needed above what fprintf does. */
592 if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n",
593 p->pw_name, p->pw_passwd,
594 (unsigned long)(p->pw_uid),
595 (unsigned long)(p->pw_gid),
596 p->pw_gecos, p->pw_dir, p->pw_shell) >= 0
597 ) {
598 rv = 0;
599 }
600 }
601
602 return rv;
603}
604
Eric Andersen9615a082004-07-15 12:53:49 +0000605int putgrent(const struct group *__restrict p, FILE *__restrict f)
606{
607 static const char format[] = ",%s";
608 char **m;
609 const char *fmt;
610 int rv = -1;
611
612 if (!p || !f) { /* Sigh... glibc checks. */
613 errno=EINVAL;
614 } else {
615 if (fprintf(f, "%s:%s:%lu:",
616 p->gr_name, p->gr_passwd,
617 (unsigned long)(p->gr_gid)) >= 0
618 ) {
619
620 fmt = format + 1;
621
622 assert(p->gr_mem);
623 m = p->gr_mem;
624
625 do {
626 if (!*m) {
Rob Landley25413bf2005-10-08 02:23:22 +0000627 if (fputc('\n', f) >= 0) {
Eric Andersen9615a082004-07-15 12:53:49 +0000628 rv = 0;
629 }
630 break;
631 }
632 if (fprintf(f, fmt, *m) < 0) {
633 break;
634 }
635 ++m;
636 fmt = format;
637 } while (1);
638
639 }
640
641 }
642
643 return rv;
644}
645
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000646static const unsigned char _sp_off[] = {
Eric Andersen9615a082004-07-15 12:53:49 +0000647 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000648 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
Eric Andersen9615a082004-07-15 12:53:49 +0000649 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000650 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
651 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
652 offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */
Eric Andersen9615a082004-07-15 12:53:49 +0000653};
654
655int putspent(const struct spwd *p, FILE *stream)
656{
657 static const char ld_format[] = "%ld:";
658 const char *f;
659 long int x;
660 int i;
661 int rv = -1;
662
663 /* Unlike putpwent and putgrent, glibc does not check the args. */
664 if (fprintf(stream, "%s:%s:", p->sp_namp,
665 (p->sp_pwdp ? p->sp_pwdp : "")) < 0
666 ) {
667 goto DO_UNLOCK;
668 }
669
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000670 for (i=0 ; i < sizeof(_sp_off) ; i++) {
Eric Andersen9615a082004-07-15 12:53:49 +0000671 f = ld_format;
Bernhard Reutner-Fischer30c7de02005-10-28 11:21:40 +0000672 if ((x = *(const long int *)(((const char *) p) + _sp_off[i])) == -1) {
Eric Andersen9615a082004-07-15 12:53:49 +0000673 f += 3;
674 }
675 if (fprintf(stream, f, x) < 0) {
676 goto DO_UNLOCK;
677 }
678 }
679
680 if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) {
681 goto DO_UNLOCK;
682 }
683
Rob Landley25413bf2005-10-08 02:23:22 +0000684 if (fputc('\n', stream) > 0) {
Eric Andersen9615a082004-07-15 12:53:49 +0000685 rv = 0;
686 }
687
688DO_UNLOCK:
689 return rv;
690}
691
Eric Andersen9615a082004-07-15 12:53:49 +0000692/**********************************************************************/
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000693/* Internal uClibc functions. */
Eric Andersen9615a082004-07-15 12:53:49 +0000694/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000695
696static const unsigned char pw_off[] = {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000697 offsetof(struct passwd, pw_name), /* 0 */
Eric Andersen9615a082004-07-15 12:53:49 +0000698 offsetof(struct passwd, pw_passwd), /* 1 */
699 offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000700 offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */
Eric Andersen9615a082004-07-15 12:53:49 +0000701 offsetof(struct passwd, pw_gecos), /* 4 */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000702 offsetof(struct passwd, pw_dir), /* 5 */
703 offsetof(struct passwd, pw_shell) /* 6 */
Eric Andersen9615a082004-07-15 12:53:49 +0000704};
705
706int __parsepwent(void *data, char *line)
707{
708 char *endptr;
709 char *p;
710 int i;
711
712 i = 0;
713 do {
714 p = ((char *) ((struct passwd *) data)) + pw_off[i];
715
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000716 if ((i & 6) ^ 2) { /* i!=2 and i!=3 */
Eric Andersen9615a082004-07-15 12:53:49 +0000717 *((char **) p) = line;
718 if (i==6) {
719 return 0;
720 }
721 /* NOTE: glibc difference - glibc allows omission of
722 * ':' seperators after the gid field if all remaining
723 * entries are empty. We require all separators. */
724 if (!(line = strchr(line, ':'))) {
725 break;
726 }
727 } else {
728 unsigned long t = strtoul(line, &endptr, 10);
729 /* Make sure we had at least one digit, and that the
730 * failing char is the next field seperator ':'. See
731 * glibc difference note above. */
732 /* TODO: Also check for leading whitespace? */
733 if ((endptr == line) || (*endptr != ':')) {
734 break;
735 }
736 line = endptr;
737 if (i & 1) { /* i == 3 -- gid */
738 *((gid_t *) p) = t;
739 } else { /* i == 2 -- uid */
740 *((uid_t *) p) = t;
741 }
742 }
743
744 *line++ = 0;
745 ++i;
746 } while (1);
747
748 return -1;
749}
750
Eric Andersen9615a082004-07-15 12:53:49 +0000751/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000752
753static const unsigned char gr_off[] = {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000754 offsetof(struct group, gr_name), /* 0 */
Eric Andersen9615a082004-07-15 12:53:49 +0000755 offsetof(struct group, gr_passwd), /* 1 */
756 offsetof(struct group, gr_gid) /* 2 - not a char ptr */
757};
758
759int __parsegrent(void *data, char *line)
760{
761 char *endptr;
762 char *p;
763 int i;
764 char **members;
765 char *end_of_buf;
766
767 end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */
768 i = 0;
769 do {
770 p = ((char *) ((struct group *) data)) + gr_off[i];
771
772 if (i < 2) {
773 *((char **) p) = line;
774 if (!(line = strchr(line, ':'))) {
775 break;
776 }
777 *line++ = 0;
778 ++i;
779 } else {
780 *((gid_t *) p) = strtoul(line, &endptr, 10);
781
782 /* NOTE: glibc difference - glibc allows omission of the
783 * trailing colon when there is no member list. We treat
784 * this as an error. */
785
786 /* Make sure we had at least one digit, and that the
787 * failing char is the next field seperator ':'. See
788 * glibc difference note above. */
789 if ((endptr == line) || (*endptr != ':')) {
790 break;
791 }
792
793 i = 1; /* Count terminating NULL ptr. */
794 p = endptr;
795
796 if (p[1]) { /* We have a member list to process. */
797 /* Overwrite the last ':' with a ',' before counting.
798 * This allows us to test for initial ',' and adds
799 * one ',' so that the ',' count equals the member
800 * count. */
801 *p = ',';
802 do {
803 /* NOTE: glibc difference - glibc allows and trims leading
804 * (but not trailing) space. We treat this as an error. */
805 /* NOTE: glibc difference - glibc allows consecutive and
806 * trailing commas, and ignores "empty string" users. We
807 * treat this as an error. */
808 if (*p == ',') {
809 ++i;
810 *p = 0; /* nul-terminate each member string. */
811 if (!*++p || (*p == ',') || isspace(*p)) {
812 goto ERR;
813 }
814 }
815 } while (*++p);
816 }
817
818 /* Now align (p+1), rounding up. */
819 /* Assumes sizeof(char **) is a power of 2. */
820 members = (char **)( (((intptr_t) p) + sizeof(char **))
821 & ~((intptr_t)(sizeof(char **) - 1)) );
822
823 if (((char *)(members + i)) > end_of_buf) { /* No space. */
824 break;
825 }
826
827 ((struct group *) data)->gr_mem = members;
828
829 if (--i) {
830 p = endptr; /* Pointing to char prior to first member. */
831 do {
832 *members++ = ++p;
833 if (!--i) break;
834 while (*++p) {}
835 } while (1);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000836 }
Eric Andersen9615a082004-07-15 12:53:49 +0000837 *members = NULL;
838
839 return 0;
840 }
841 } while (1);
842
843 ERR:
844 return -1;
845}
846
Eric Andersen9615a082004-07-15 12:53:49 +0000847/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000848
849static const unsigned char sp_off[] = {
850 offsetof(struct spwd, sp_namp), /* 0 */
851 offsetof(struct spwd, sp_pwdp), /* 1 */
852 offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000853 offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
Eric Andersen9615a082004-07-15 12:53:49 +0000854 offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000855 offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
856 offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
857 offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */
858 offsetof(struct spwd, sp_flag) /* 8 - not a char ptr */
Eric Andersen9615a082004-07-15 12:53:49 +0000859};
860
861int __parsespent(void *data, char * line)
862{
863 char *endptr;
864 char *p;
865 int i;
866
867 i = 0;
868 do {
869 p = ((char *) ((struct spwd *) data)) + sp_off[i];
870 if (i < 2) {
871 *((char **) p) = line;
872 if (!(line = strchr(line, ':'))) {
873 break;
874 }
875 } else {
Eric Andersen9615a082004-07-15 12:53:49 +0000876 *((long *) p) = (long) strtoul(line, &endptr, 10);
877
878 if (endptr == line) {
879 *((long *) p) = ((i != 8) ? -1L : ((long)(~0UL)));
880 }
881
882 line = endptr;
883
884 if (i == 8) {
885 if (!*endptr) {
886 return 0;
887 }
888 break;
889 }
890
891 if (*endptr != ':') {
892 break;
893 }
894
895 }
896
897 *line++ = 0;
898 ++i;
899 } while (1);
900
901 return EINVAL;
902}
903
Eric Andersen9615a082004-07-15 12:53:49 +0000904/**********************************************************************/
Eric Andersen9615a082004-07-15 12:53:49 +0000905
906/* Reads until if EOF, or until if finds a line which fits in the buffer
907 * and for which the parser function succeeds.
908 *
909 * Returns 0 on success and ENOENT for end-of-file (glibc concession).
910 */
911
912int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,
913 char *__restrict line_buff, size_t buflen, FILE *f)
914{
915 int line_len;
916 int skip;
917 int rv = ERANGE;
918
919 if (buflen < PWD_BUFFER_SIZE) {
920 errno=rv;
921 } else {
922 skip = 0;
923 do {
Rob Landley25413bf2005-10-08 02:23:22 +0000924 if (!fgets(line_buff, buflen, f)) {
925 if (feof(f)) {
Eric Andersen9615a082004-07-15 12:53:49 +0000926 rv = ENOENT;
927 }
928 break;
929 }
930
931 line_len = strlen(line_buff) - 1; /* strlen() must be > 0. */
932 if (line_buff[line_len] == '\n') {
933 line_buff[line_len] = 0;
934 } else if (line_len + 2 == buflen) { /* line too long */
935 ++skip;
936 continue;
937 }
938
939 if (skip) {
940 --skip;
941 continue;
942 }
943
944 /* NOTE: glibc difference - glibc strips leading whitespace from
945 * records. We do not allow leading whitespace. */
946
947 /* Skip empty lines, comment lines, and lines with leading
948 * whitespace. */
949 if (*line_buff && (*line_buff != '#') && !isspace(*line_buff)) {
950 if (__parserfunc == __parsegrent) { /* Do evil group hack. */
951 /* The group entry parsing function needs to know where
952 * the end of the buffer is so that it can construct the
953 * group member ptr table. */
954 ((struct group *) data)->gr_name = line_buff + buflen;
955 }
956
957 if (!__parserfunc(data, line_buff)) {
958 rv = 0;
959 break;
960 }
961 }
962 } while (1);
963
964 }
965
966 return rv;
967}
968
Eric Andersen9615a082004-07-15 12:53:49 +0000969/**********************************************************************/