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