style fixes
[oweals/busybox.git] / libpwdgrp / pwd_grp.c
1 /* vi: set sw=4 ts=4: */
2 /*  Copyright (C) 2003     Manuel Novoa III
3  *
4  *  Licensed under GPL v2, or later.  See file LICENSE in this tarball.
5  */
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:
15  *    Move to dynamic allocation of (currently statically allocated)
16  *      buffers; especially for the group-related functions since
17  *      large group member lists will cause error returns.
18  *
19  */
20
21 #include "libbb.h"
22 #include <features.h>
23 #include <assert.h>
24
25 #ifndef _PATH_SHADOW
26 #define _PATH_SHADOW    "/etc/shadow"
27 #endif
28 #ifndef _PATH_PASSWD
29 #define _PATH_PASSWD    "/etc/passwd"
30 #endif
31 #ifndef _PATH_GROUP
32 #define _PATH_GROUP     "/etc/group"
33 #endif
34
35 /**********************************************************************/
36 /* Sizes for statically allocated buffers. */
37
38 /* If you change these values, also change _SC_GETPW_R_SIZE_MAX and
39  * _SC_GETGR_R_SIZE_MAX in libc/unistd/sysconf.c to match */
40 #define PWD_BUFFER_SIZE 256
41 #define GRP_BUFFER_SIZE 256
42
43 /**********************************************************************/
44 /* Prototypes for internal functions. */
45
46 static int bb__pgsreader(int (*parserfunc)(void *d, char *line), void *data,
47                 char *__restrict line_buff, size_t buflen, FILE *f);
48
49 static int bb__parsepwent(void *pw, char *line);
50 static int bb__parsegrent(void *gr, char *line);
51 #if ENABLE_USE_BB_SHADOW
52 static int bb__parsespent(void *sp, char *line);
53 #endif
54
55 /**********************************************************************/
56 /* For the various fget??ent_r funcs, return
57  *
58  *  0: success
59  *  ENOENT: end-of-file encountered
60  *  ERANGE: buflen too small
61  *  other error values possible. See bb__pgsreader.
62  *
63  * Also, *result == resultbuf on success and NULL on failure.
64  *
65  * NOTE: glibc difference - For the ENOENT case, glibc also sets errno.
66  *   We do not, as it really isn't an error if we reach the end-of-file.
67  *   Doing so is analogous to having fgetc() set errno on EOF.
68  */
69 /**********************************************************************/
70
71 int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf,
72                                 char *__restrict buffer, size_t buflen,
73                                 struct passwd **__restrict result)
74 {
75         int rv;
76
77         *result = NULL;
78
79         rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, stream);
80         if (!rv) {
81                 *result = resultbuf;
82         }
83
84         return rv;
85 }
86
87 int fgetgrent_r(FILE *__restrict stream, struct group *__restrict resultbuf,
88                                 char *__restrict buffer, size_t buflen,
89                                 struct group **__restrict result)
90 {
91         int rv;
92
93         *result = NULL;
94
95         rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, stream);
96         if (!rv) {
97                 *result = resultbuf;
98         }
99
100         return rv;
101 }
102
103 #if ENABLE_USE_BB_SHADOW
104 int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
105                                 char *__restrict buffer, size_t buflen,
106                                 struct spwd **__restrict result)
107 {
108         int rv;
109
110         *result = NULL;
111
112         rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, stream);
113         if (!rv) {
114                 *result = resultbuf;
115         }
116
117         return rv;
118 }
119 #endif
120
121 /**********************************************************************/
122 /* For the various fget??ent funcs, return NULL on failure and a
123  * pointer to the appropriate struct (statically allocated) on success.
124  * TODO: audit & stop using these in bbox, they pull in static buffers */
125 /**********************************************************************/
126
127 #if 0
128 struct passwd *fgetpwent(FILE *stream)
129 {
130         static char buffer[PWD_BUFFER_SIZE];
131         static struct passwd resultbuf;
132         struct passwd *result;
133
134         fgetpwent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
135         return result;
136 }
137
138 struct group *fgetgrent(FILE *stream)
139 {
140         static char buffer[GRP_BUFFER_SIZE];
141         static struct group resultbuf;
142         struct group *result;
143
144         fgetgrent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
145         return result;
146 }
147 #endif
148
149 #if ENABLE_USE_BB_SHADOW
150 #if 0
151 struct spwd *fgetspent(FILE *stream)
152 {
153         static char buffer[PWD_BUFFER_SIZE];
154         static struct spwd resultbuf;
155         struct spwd *result;
156
157         fgetspent_r(stream, &resultbuf, buffer, sizeof(buffer), &result);
158         return result;
159 }
160 #endif
161
162 int sgetspent_r(const char *string, struct spwd *result_buf,
163                                 char *buffer, size_t buflen, struct spwd **result)
164 {
165         int rv = ERANGE;
166
167         *result = NULL;
168
169         if (buflen < PWD_BUFFER_SIZE) {
170         DO_ERANGE:
171                 errno=rv;
172                 goto DONE;
173         }
174
175         if (string != buffer) {
176                 if (strlen(string) >= buflen) {
177                         goto DO_ERANGE;
178                 }
179                 strcpy(buffer, string);
180         }
181
182         rv = bb__parsespent(result_buf, buffer);
183         if (!rv) {
184                 *result = result_buf;
185         }
186
187  DONE:
188         return rv;
189 }
190 #endif
191
192 /**********************************************************************/
193
194 #define GETXXKEY_R_FUNC         getpwnam_r
195 #define GETXXKEY_R_PARSER       bb__parsepwent
196 #define GETXXKEY_R_ENTTYPE      struct passwd
197 #define GETXXKEY_R_TEST(ENT)    (!strcmp((ENT)->pw_name, key))
198 #define GETXXKEY_R_KEYTYPE      const char *__restrict
199 #define GETXXKEY_R_PATHNAME     _PATH_PASSWD
200 #include "pwd_grp_internal.c"
201
202 #define GETXXKEY_R_FUNC         getgrnam_r
203 #define GETXXKEY_R_PARSER       bb__parsegrent
204 #define GETXXKEY_R_ENTTYPE      struct group
205 #define GETXXKEY_R_TEST(ENT)    (!strcmp((ENT)->gr_name, key))
206 #define GETXXKEY_R_KEYTYPE      const char *__restrict
207 #define GETXXKEY_R_PATHNAME     _PATH_GROUP
208 #include "pwd_grp_internal.c"
209
210 #if ENABLE_USE_BB_SHADOW
211 #define GETXXKEY_R_FUNC         getspnam_r
212 #define GETXXKEY_R_PARSER       bb__parsespent
213 #define GETXXKEY_R_ENTTYPE      struct spwd
214 #define GETXXKEY_R_TEST(ENT)    (!strcmp((ENT)->sp_namp, key))
215 #define GETXXKEY_R_KEYTYPE      const char *__restrict
216 #define GETXXKEY_R_PATHNAME     _PATH_SHADOW
217 #include "pwd_grp_internal.c"
218 #endif
219
220 #define GETXXKEY_R_FUNC         getpwuid_r
221 #define GETXXKEY_R_PARSER       bb__parsepwent
222 #define GETXXKEY_R_ENTTYPE      struct passwd
223 #define GETXXKEY_R_TEST(ENT)    ((ENT)->pw_uid == key)
224 #define GETXXKEY_R_KEYTYPE      uid_t
225 #define GETXXKEY_R_PATHNAME     _PATH_PASSWD
226 #include "pwd_grp_internal.c"
227
228 #define GETXXKEY_R_FUNC         getgrgid_r
229 #define GETXXKEY_R_PARSER       bb__parsegrent
230 #define GETXXKEY_R_ENTTYPE      struct group
231 #define GETXXKEY_R_TEST(ENT)    ((ENT)->gr_gid == key)
232 #define GETXXKEY_R_KEYTYPE      gid_t
233 #define GETXXKEY_R_PATHNAME     _PATH_GROUP
234 #include "pwd_grp_internal.c"
235
236 /**********************************************************************/
237 /* TODO: audit & stop using these in bbox, they pull in static buffers */
238
239 /* This one has many users */
240 struct passwd *getpwuid(uid_t uid)
241 {
242         static char buffer[PWD_BUFFER_SIZE];
243         static struct passwd resultbuf;
244         struct passwd *result;
245
246         getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result);
247         return result;
248 }
249
250 /* This one has many users */
251 struct group *getgrgid(gid_t gid)
252 {
253         static char buffer[GRP_BUFFER_SIZE];
254         static struct group resultbuf;
255         struct group *result;
256
257         getgrgid_r(gid, &resultbuf, buffer, sizeof(buffer), &result);
258         return result;
259 }
260
261 #if 0 //ENABLE_USE_BB_SHADOW
262 /* This function is non-standard and is currently not built.  It seems
263  * to have been created as a reentrant version of the non-standard
264  * functions getspuid.  Why getspuid was added, I do not know. */
265 int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf,
266                        char *__restrict buffer, size_t buflen,
267                        struct spwd **__restrict result)
268 {
269         int rv;
270         struct passwd *pp;
271         struct passwd password;
272         char pwd_buff[PWD_BUFFER_SIZE];
273
274         *result = NULL;
275         rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp);
276         if (!rv) {
277                 rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result);
278         }
279
280         return rv;
281 }
282
283 /* This function is non-standard and is currently not built.
284  * Why it was added, I do not know. */
285 struct spwd *getspuid(uid_t uid)
286 {
287         static char buffer[PWD_BUFFER_SIZE];
288         static struct spwd resultbuf;
289         struct spwd *result;
290
291         getspuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result);
292         return result;
293 }
294 #endif
295
296 /* This one has many users */
297 struct passwd *getpwnam(const char *name)
298 {
299         static char buffer[PWD_BUFFER_SIZE];
300         static struct passwd resultbuf;
301         struct passwd *result;
302
303         getpwnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
304         return result;
305 }
306
307 /* This one has many users */
308 struct group *getgrnam(const char *name)
309 {
310         static char buffer[GRP_BUFFER_SIZE];
311         static struct group resultbuf;
312         struct group *result;
313
314         getgrnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
315         return result;
316 }
317
318 #if 0 //ENABLE_USE_BB_SHADOW
319 struct spwd *getspnam(const char *name)
320 {
321         static char buffer[PWD_BUFFER_SIZE];
322         static struct spwd resultbuf;
323         struct spwd *result;
324
325         getspnam_r(name, &resultbuf, buffer, sizeof(buffer), &result);
326         return result;
327 }
328 #endif
329
330 /* This one doesn't use static buffers */
331 int getpw(uid_t uid, char *buf)
332 {
333         struct passwd resultbuf;
334         struct passwd *result;
335         char buffer[PWD_BUFFER_SIZE];
336
337         if (!buf) {
338                 errno = EINVAL;
339         } else if (!getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result)) {
340                 if (sprintf(buf, "%s:%s:%lu:%lu:%s:%s:%s\n",
341                                         resultbuf.pw_name, resultbuf.pw_passwd,
342                                         (unsigned long)(resultbuf.pw_uid),
343                                         (unsigned long)(resultbuf.pw_gid),
344                                         resultbuf.pw_gecos, resultbuf.pw_dir,
345                                         resultbuf.pw_shell) >= 0
346                         ) {
347                         return 0;
348                 }
349         }
350
351         return -1;
352 }
353
354 /**********************************************************************/
355
356 /* FIXME: we don't have such CONFIG_xx - ?! */
357
358 #if defined CONFIG_USE_BB_THREADSAFE_SHADOW && defined PTHREAD_MUTEX_INITIALIZER
359 static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
360 # define LOCK           pthread_mutex_lock(&mylock)
361 # define UNLOCK         pthread_mutex_unlock(&mylock);
362 #else
363 # define LOCK           ((void) 0)
364 # define UNLOCK         ((void) 0)
365 #endif
366
367 static FILE *pwf /*= NULL*/;
368 void setpwent(void)
369 {
370         LOCK;
371         if (pwf) {
372                 rewind(pwf);
373         }
374         UNLOCK;
375 }
376
377 void endpwent(void)
378 {
379         LOCK;
380         if (pwf) {
381                 fclose(pwf);
382                 pwf = NULL;
383         }
384         UNLOCK;
385 }
386
387
388 int getpwent_r(struct passwd *__restrict resultbuf,
389                            char *__restrict buffer, size_t buflen,
390                            struct passwd **__restrict result)
391 {
392         int rv;
393
394         LOCK;
395         *result = NULL;                         /* In case of error... */
396
397         if (!pwf) {
398                 pwf = fopen(_PATH_PASSWD, "r");
399                 if (!pwf) {
400                         rv = errno;
401                         goto ERR;
402                 }
403         }
404
405         rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, pwf);
406         if (!rv) {
407                 *result = resultbuf;
408         }
409
410  ERR:
411         UNLOCK;
412         return rv;
413 }
414
415 static FILE *grf /*= NULL*/;
416 void setgrent(void)
417 {
418         LOCK;
419         if (grf) {
420                 rewind(grf);
421         }
422         UNLOCK;
423 }
424
425 void endgrent(void)
426 {
427         LOCK;
428         if (grf) {
429                 fclose(grf);
430                 grf = NULL;
431         }
432         UNLOCK;
433 }
434
435 int getgrent_r(struct group *__restrict resultbuf,
436                            char *__restrict buffer, size_t buflen,
437                            struct group **__restrict result)
438 {
439         int rv;
440
441         LOCK;
442         *result = NULL;                         /* In case of error... */
443
444         if (!grf) {
445                 grf = fopen(_PATH_GROUP, "r");
446                 if (!grf) {
447                         rv = errno;
448                         goto ERR;
449                 }
450         }
451
452         rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, grf);
453         if (!rv) {
454                 *result = resultbuf;
455         }
456
457  ERR:
458         UNLOCK;
459         return rv;
460 }
461
462 #if ENABLE_USE_BB_SHADOW
463 static FILE *spf /*= NULL*/;
464 void setspent(void)
465 {
466         LOCK;
467         if (spf) {
468                 rewind(spf);
469         }
470         UNLOCK;
471 }
472
473 void endspent(void)
474 {
475         LOCK;
476         if (spf) {
477                 fclose(spf);
478                 spf = NULL;
479         }
480         UNLOCK;
481 }
482
483 int getspent_r(struct spwd *resultbuf, char *buffer,
484                            size_t buflen, struct spwd **result)
485 {
486         int rv;
487
488         LOCK;
489         *result = NULL;                         /* In case of error... */
490
491         if (!spf) {
492                 spf = fopen(_PATH_SHADOW, "r");
493                 if (!spf) {
494                         rv = errno;
495                         goto ERR;
496                 }
497         }
498
499         rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, spf);
500         if (!rv) {
501                 *result = resultbuf;
502         }
503
504  ERR:
505         UNLOCK;
506         return rv;
507 }
508 #endif
509
510 #if 0
511 struct passwd *getpwent(void)
512 {
513         static char line_buff[PWD_BUFFER_SIZE];
514         static struct passwd pwd;
515         struct passwd *result;
516
517         getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
518         return result;
519 }
520
521 struct group *getgrent(void)
522 {
523         static char line_buff[GRP_BUFFER_SIZE];
524         static struct group gr;
525         struct group *result;
526
527         getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
528         return result;
529 }
530 #endif
531
532 #if 0 //ENABLE_USE_BB_SHADOW
533 struct spwd *getspent(void)
534 {
535         static char line_buff[PWD_BUFFER_SIZE];
536         static struct spwd spwd;
537         struct spwd *result;
538
539         getspent_r(&spwd, line_buff, sizeof(line_buff), &result);
540         return result;
541 }
542
543 struct spwd *sgetspent(const char *string)
544 {
545         static char line_buff[PWD_BUFFER_SIZE];
546         static struct spwd spwd;
547         struct spwd *result;
548
549         sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result);
550         return result;
551 }
552 #endif
553
554 int initgroups(const char *user, gid_t gid)
555 {
556         FILE *grfile;
557         gid_t *group_list;
558         int num_groups, rv;
559         char **m;
560         struct group group;
561         char buff[PWD_BUFFER_SIZE];
562
563         rv = -1;
564
565         /* We alloc space for 8 gids at a time. */
566         group_list = (gid_t *) malloc(8*sizeof(gid_t *));
567         if (group_list
568          && ((grfile = fopen(_PATH_GROUP, "r")) != NULL)
569         ) {
570                 *group_list = gid;
571                 num_groups = 1;
572
573                 while (!bb__pgsreader(bb__parsegrent, &group, buff, sizeof(buff), grfile)) {
574                         assert(group.gr_mem); /* Must have at least a NULL terminator. */
575                         if (group.gr_gid != gid) {
576                                 for (m=group.gr_mem ; *m ; m++) {
577                                         if (!strcmp(*m, user)) {
578                                                 if (!(num_groups & 7)) {
579                                                         gid_t *tmp = (gid_t *)
580                                                                 realloc(group_list,
581                                                                                 (num_groups+8) * sizeof(gid_t *));
582                                                         if (!tmp) {
583                                                                 rv = -1;
584                                                                 goto DO_CLOSE;
585                                                         }
586                                                         group_list = tmp;
587                                                 }
588                                                 group_list[num_groups++] = group.gr_gid;
589                                                 break;
590                                         }
591                                 }
592                         }
593                 }
594
595                 rv = setgroups(num_groups, group_list);
596         DO_CLOSE:
597                 fclose(grfile);
598         }
599
600         /* group_list will be NULL if initial malloc failed, which may trigger
601          * warnings from various malloc debuggers. */
602         free(group_list);
603         return rv;
604 }
605
606 int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
607 {
608         int rv = -1;
609
610         if (!p || !f) {
611                 errno=EINVAL;
612         } else {
613                 /* No extra thread locking is needed above what fprintf does. */
614                 if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n",
615                                         p->pw_name, p->pw_passwd,
616                                         (unsigned long)(p->pw_uid),
617                                         (unsigned long)(p->pw_gid),
618                                         p->pw_gecos, p->pw_dir, p->pw_shell) >= 0
619                         ) {
620                         rv = 0;
621                 }
622         }
623
624         return rv;
625 }
626
627 int putgrent(const struct group *__restrict p, FILE *__restrict f)
628 {
629         static const char format[] = ",%s";
630         char **m;
631         const char *fmt;
632         int rv = -1;
633
634         if (!p || !f) {                         /* Sigh... glibc checks. */
635                 errno=EINVAL;
636         } else {
637                 if (fprintf(f, "%s:%s:%lu:",
638                                         p->gr_name, p->gr_passwd,
639                                         (unsigned long)(p->gr_gid)) >= 0
640                         ) {
641
642                         fmt = format + 1;
643
644                         assert(p->gr_mem);
645                         m = p->gr_mem;
646
647                         do {
648                                 if (!*m) {
649                                         if (fputc('\n', f) >= 0) {
650                                                 rv = 0;
651                                         }
652                                         break;
653                                 }
654                                 if (fprintf(f, fmt, *m) < 0) {
655                                         break;
656                                 }
657                                 ++m;
658                                 fmt = format;
659                         } while (1);
660
661                 }
662
663         }
664
665         return rv;
666 }
667
668 #if ENABLE_USE_BB_SHADOW
669 static const unsigned char _sp_off[] = {
670         offsetof(struct spwd, sp_lstchg),       /* 2 - not a char ptr */
671         offsetof(struct spwd, sp_min),          /* 3 - not a char ptr */
672         offsetof(struct spwd, sp_max),          /* 4 - not a char ptr */
673         offsetof(struct spwd, sp_warn),         /* 5 - not a char ptr */
674         offsetof(struct spwd, sp_inact),        /* 6 - not a char ptr */
675         offsetof(struct spwd, sp_expire),       /* 7 - not a char ptr */
676 };
677
678 int putspent(const struct spwd *p, FILE *stream)
679 {
680         static const char ld_format[] = "%ld:";
681         const char *f;
682         long x;
683         int i;
684         int rv = -1;
685
686         /* Unlike putpwent and putgrent, glibc does not check the args. */
687         if (fprintf(stream, "%s:%s:", p->sp_namp,
688                                 (p->sp_pwdp ? p->sp_pwdp : "")) < 0
689         ) {
690                 goto DO_UNLOCK;
691         }
692
693         for (i=0 ; i < sizeof(_sp_off) ; i++) {
694                 f = ld_format;
695                 x = *(const long *)(((const char *) p) + _sp_off[i]);
696                 if (x == -1) {
697                         f += 3;
698                 }
699                 if (fprintf(stream, f, x) < 0) {
700                         goto DO_UNLOCK;
701                 }
702         }
703
704         if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) {
705                 goto DO_UNLOCK;
706         }
707
708         if (fputc('\n', stream) > 0) {
709                 rv = 0;
710         }
711
712 DO_UNLOCK:
713         return rv;
714 }
715 #endif
716
717 /**********************************************************************/
718 /* Internal uClibc functions.                                         */
719 /**********************************************************************/
720
721 static const unsigned char pw_off[] = {
722         offsetof(struct passwd, pw_name),       /* 0 */
723         offsetof(struct passwd, pw_passwd),     /* 1 */
724         offsetof(struct passwd, pw_uid),        /* 2 - not a char ptr */
725         offsetof(struct passwd, pw_gid),        /* 3 - not a char ptr */
726         offsetof(struct passwd, pw_gecos),      /* 4 */
727         offsetof(struct passwd, pw_dir),        /* 5 */
728         offsetof(struct passwd, pw_shell)       /* 6 */
729 };
730
731 static int bb__parsepwent(void *data, char *line)
732 {
733         char *endptr;
734         char *p;
735         int i;
736
737         i = 0;
738         do {
739                 p = ((char *) ((struct passwd *) data)) + pw_off[i];
740
741                 if ((i & 6) ^ 2) {      /* i!=2 and i!=3 */
742                         *((char **) p) = line;
743                         if (i==6) {
744                                 return 0;
745                         }
746                         /* NOTE: glibc difference - glibc allows omission of
747                          * ':' seperators after the gid field if all remaining
748                          * entries are empty.  We require all separators. */
749                         line = strchr(line, ':');
750                         if (!line) {
751                                 break;
752                         }
753                 } else {
754                         unsigned long t = strtoul(line, &endptr, 10);
755                         /* Make sure we had at least one digit, and that the
756                          * failing char is the next field seperator ':'.  See
757                          * glibc difference note above. */
758                         /* TODO: Also check for leading whitespace? */
759                         if ((endptr == line) || (*endptr != ':')) {
760                                 break;
761                         }
762                         line = endptr;
763                         if (i & 1) {            /* i == 3 -- gid */
764                                 *((gid_t *) p) = t;
765                         } else {                        /* i == 2 -- uid */
766                                 *((uid_t *) p) = t;
767                         }
768                 }
769
770                 *line++ = 0;
771                 ++i;
772         } while (1);
773
774         return -1;
775 }
776
777 /**********************************************************************/
778
779 static const unsigned char gr_off[] = {
780         offsetof(struct group, gr_name),        /* 0 */
781         offsetof(struct group, gr_passwd),      /* 1 */
782         offsetof(struct group, gr_gid)          /* 2 - not a char ptr */
783 };
784
785 static int bb__parsegrent(void *data, char *line)
786 {
787         char *endptr;
788         char *p;
789         int i;
790         char **members;
791         char *end_of_buf;
792
793         end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */
794         i = 0;
795         do {
796                 p = ((char *) ((struct group *) data)) + gr_off[i];
797
798                 if (i < 2) {
799                         *((char **) p) = line;
800                         line = strchr(line, ':');
801                         if (!line) {
802                                 break;
803                         }
804                         *line++ = 0;
805                         ++i;
806                 } else {
807                         *((gid_t *) p) = strtoul(line, &endptr, 10);
808
809                         /* NOTE: glibc difference - glibc allows omission of the
810                          * trailing colon when there is no member list.  We treat
811                          * this as an error. */
812
813                         /* Make sure we had at least one digit, and that the
814                          * failing char is the next field seperator ':'.  See
815                          * glibc difference note above. */
816                         if ((endptr == line) || (*endptr != ':')) {
817                                 break;
818                         }
819
820                         i = 1;                          /* Count terminating NULL ptr. */
821                         p = endptr;
822
823                         if (p[1]) { /* We have a member list to process. */
824                                 /* Overwrite the last ':' with a ',' before counting.
825                                  * This allows us to test for initial ',' and adds
826                                  * one ',' so that the ',' count equals the member
827                                  * count. */
828                                 *p = ',';
829                                 do {
830                                         /* NOTE: glibc difference - glibc allows and trims leading
831                                          * (but not trailing) space.  We treat this as an error. */
832                                         /* NOTE: glibc difference - glibc allows consecutive and
833                                          * trailing commas, and ignores "empty string" users.  We
834                                          * treat this as an error. */
835                                         if (*p == ',') {
836                                                 ++i;
837                                                 *p = 0; /* nul-terminate each member string. */
838                                                 if (!*++p || (*p == ',') || isspace(*p)) {
839                                                         goto ERR;
840                                                 }
841                                         }
842                                 } while (*++p);
843                         }
844
845                         /* Now align (p+1), rounding up. */
846                         /* Assumes sizeof(char **) is a power of 2. */
847                         members = (char **)( (((intptr_t) p) + sizeof(char **))
848                                                                  & ~((intptr_t)(sizeof(char **) - 1)) );
849
850                         if (((char *)(members + i)) > end_of_buf) {     /* No space. */
851                                 break;
852                         }
853
854                         ((struct group *) data)->gr_mem = members;
855
856                         if (--i) {
857                                 p = endptr;     /* Pointing to char prior to first member. */
858                                 do {
859                                         *members++ = ++p;
860                                         if (!--i) break;
861                                         while (*++p) {}
862                                 } while (1);
863                         }
864                         *members = NULL;
865
866                         return 0;
867                 }
868         } while (1);
869
870  ERR:
871         return -1;
872 }
873
874 /**********************************************************************/
875
876 #if ENABLE_USE_BB_SHADOW
877 static const unsigned char sp_off[] = {
878         offsetof(struct spwd, sp_namp),         /* 0 */
879         offsetof(struct spwd, sp_pwdp),         /* 1 */
880         offsetof(struct spwd, sp_lstchg),       /* 2 - not a char ptr */
881         offsetof(struct spwd, sp_min),          /* 3 - not a char ptr */
882         offsetof(struct spwd, sp_max),          /* 4 - not a char ptr */
883         offsetof(struct spwd, sp_warn),         /* 5 - not a char ptr */
884         offsetof(struct spwd, sp_inact),        /* 6 - not a char ptr */
885         offsetof(struct spwd, sp_expire),       /* 7 - not a char ptr */
886         offsetof(struct spwd, sp_flag)          /* 8 - not a char ptr */
887 };
888
889 static int bb__parsespent(void *data, char * line)
890 {
891         char *endptr;
892         char *p;
893         int i;
894
895         i = 0;
896         do {
897                 p = ((char *) ((struct spwd *) data)) + sp_off[i];
898                 if (i < 2) {
899                         *((char **) p) = line;
900                         line = strchr(line, ':');
901                         if (!line) {
902                                 break;
903                         }
904                 } else {
905                         *((long *) p) = (long) strtoul(line, &endptr, 10);
906
907                         if (endptr == line) {
908                                 *((long *) p) = ((i != 8) ? -1L : ((long)(~0UL)));
909                         }
910
911                         line = endptr;
912
913                         if (i == 8) {
914                                 if (!*endptr) {
915                                         return 0;
916                                 }
917                                 break;
918                         }
919
920                         if (*endptr != ':') {
921                                 break;
922                         }
923
924                 }
925
926                 *line++ = 0;
927                 ++i;
928         } while (1);
929
930         return EINVAL;
931 }
932 #endif
933
934 /**********************************************************************/
935
936 /* Reads until if EOF, or until if finds a line which fits in the buffer
937  * and for which the parser function succeeds.
938  *
939  * Returns 0 on success and ENOENT for end-of-file (glibc concession).
940  */
941
942 static int bb__pgsreader(int (*parserfunc)(void *d, char *line), void *data,
943                                 char *__restrict line_buff, size_t buflen, FILE *f)
944 {
945         int line_len;
946         int skip;
947         int rv = ERANGE;
948
949         if (buflen < PWD_BUFFER_SIZE) {
950                 errno = rv;
951         } else {
952                 skip = 0;
953                 do {
954                         if (!fgets(line_buff, buflen, f)) {
955                                 if (feof(f)) {
956                                         rv = ENOENT;
957                                 }
958                                 break;
959                         }
960
961                         line_len = strlen(line_buff) - 1; /* strlen() must be > 0. */
962                         if (line_buff[line_len] == '\n') {
963                                 line_buff[line_len] = 0;
964                         } else if (line_len + 2 == buflen) { /* line too long */
965                                 ++skip;
966                                 continue;
967                         }
968
969                         if (skip) {
970                                 --skip;
971                                 continue;
972                         }
973
974                         /* NOTE: glibc difference - glibc strips leading whitespace from
975                          * records.  We do not allow leading whitespace. */
976
977                         /* Skip empty lines, comment lines, and lines with leading
978                          * whitespace. */
979                         if (*line_buff && (*line_buff != '#') && !isspace(*line_buff)) {
980                                 if (parserfunc == bb__parsegrent) {     /* Do evil group hack. */
981                                         /* The group entry parsing function needs to know where
982                                          * the end of the buffer is so that it can construct the
983                                          * group member ptr table. */
984                                         ((struct group *) data)->gr_name = line_buff + buflen;
985                                 }
986
987                                 if (!parserfunc(data, line_buff)) {
988                                         rv = 0;
989                                         break;
990                                 }
991                         }
992                 } while (1);
993
994         }
995
996         return rv;
997 }