libpwdgrp: make it coexist with <pwd.h>, <grp.h> and <shadow.h>.
[platform/upstream/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 /* We avoid having big global data. */
57
58 struct statics {
59         /* Smaller things first */
60         struct passwd getpwuid_resultbuf;
61         struct group getgrgid_resultbuf;
62         struct passwd getpwnam_resultbuf;
63         struct group getgrnam_resultbuf;
64
65         char getpwuid_buffer[PWD_BUFFER_SIZE];
66         char getgrgid_buffer[GRP_BUFFER_SIZE];
67         char getpwnam_buffer[PWD_BUFFER_SIZE];
68         char getgrnam_buffer[GRP_BUFFER_SIZE];
69 #if 0
70         struct passwd fgetpwent_resultbuf;
71         struct group fgetgrent_resultbuf;
72         struct spwd fgetspent_resultbuf;
73         char fgetpwent_buffer[PWD_BUFFER_SIZE];
74         char fgetgrent_buffer[GRP_BUFFER_SIZE];
75         char fgetspent_buffer[PWD_BUFFER_SIZE];
76 #endif
77 #if 0 //ENABLE_USE_BB_SHADOW
78         struct spwd getspuid_resultbuf;
79         struct spwd getspnam_resultbuf;
80         char getspuid_buffer[PWD_BUFFER_SIZE];
81         char getspnam_buffer[PWD_BUFFER_SIZE];
82 #endif
83 // Not converted - too small to bother
84 //pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
85 //FILE *pwf /*= NULL*/;
86 //FILE *grf /*= NULL*/;
87 //FILE *spf /*= NULL*/;
88 #if 0
89         struct passwd getpwent_pwd;
90         struct group getgrent_gr;
91         char getpwent_line_buff[PWD_BUFFER_SIZE];
92         char getgrent_line_buff[GRP_BUFFER_SIZE];
93 #endif
94 #if 0 //ENABLE_USE_BB_SHADOW
95         struct spwd getspent_spwd;
96         struct spwd sgetspent_spwd;
97         char getspent_line_buff[PWD_BUFFER_SIZE];
98         char sgetspent_line_buff[PWD_BUFFER_SIZE];
99 #endif
100 };
101
102 static struct statics *ptr_to_statics;
103
104 static struct statics *get_S(void)
105 {
106         if (!ptr_to_statics)
107                 ptr_to_statics = xzalloc(sizeof(*ptr_to_statics));
108         return ptr_to_statics;
109 }
110
111 /* Always use in this order, get_S() must be called first */
112 #define RESULTBUF(name) &((S = get_S())->name##_resultbuf)
113 #define BUFFER(name)    (S->name##_buffer)
114
115 /**********************************************************************/
116 /* For the various fget??ent_r funcs, return
117  *
118  *  0: success
119  *  ENOENT: end-of-file encountered
120  *  ERANGE: buflen too small
121  *  other error values possible. See bb__pgsreader.
122  *
123  * Also, *result == resultbuf on success and NULL on failure.
124  *
125  * NOTE: glibc difference - For the ENOENT case, glibc also sets errno.
126  *   We do not, as it really isn't an error if we reach the end-of-file.
127  *   Doing so is analogous to having fgetc() set errno on EOF.
128  */
129 /**********************************************************************/
130
131 int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf,
132                                 char *__restrict buffer, size_t buflen,
133                                 struct passwd **__restrict result)
134 {
135         int rv;
136
137         *result = NULL;
138
139         rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, stream);
140         if (!rv) {
141                 *result = resultbuf;
142         }
143
144         return rv;
145 }
146
147 int fgetgrent_r(FILE *__restrict stream, struct group *__restrict resultbuf,
148                                 char *__restrict buffer, size_t buflen,
149                                 struct group **__restrict result)
150 {
151         int rv;
152
153         *result = NULL;
154
155         rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, stream);
156         if (!rv) {
157                 *result = resultbuf;
158         }
159
160         return rv;
161 }
162
163 #if ENABLE_USE_BB_SHADOW
164 int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
165                                 char *__restrict buffer, size_t buflen,
166                                 struct spwd **__restrict result)
167 {
168         int rv;
169
170         *result = NULL;
171
172         rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, stream);
173         if (!rv) {
174                 *result = resultbuf;
175         }
176
177         return rv;
178 }
179 #endif
180
181 /**********************************************************************/
182 /* For the various fget??ent funcs, return NULL on failure and a
183  * pointer to the appropriate struct (statically allocated) on success.
184  * TODO: audit & stop using these in bbox, they pull in static buffers */
185 /**********************************************************************/
186
187 #if 0
188 struct passwd *fgetpwent(FILE *stream)
189 {
190         struct statics *S;
191         struct passwd *resultbuf = RESULTBUF(fgetpwent);
192         char *buffer = BUFFER(fgetpwent);
193         struct passwd *result;
194
195         fgetpwent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetpwent)), &result);
196         return result;
197 }
198
199 struct group *fgetgrent(FILE *stream)
200 {
201         struct statics *S;
202         struct group *resultbuf = RESULTBUF(fgetgrent);
203         char *buffer = BUFFER(fgetgrent);
204         struct group *result;
205
206         fgetgrent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetgrent)), &result);
207         return result;
208 }
209 #endif
210
211 #if ENABLE_USE_BB_SHADOW
212 #if 0
213 struct spwd *fgetspent(FILE *stream)
214 {
215         struct statics *S;
216         struct spwd *resultbuf = RESULTBUF(fgetspent);
217         char *buffer = BUFFER(fgetspent);
218         struct spwd *result;
219
220         fgetspent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetspent)), &result);
221         return result;
222 }
223 #endif
224
225 int sgetspent_r(const char *string, struct spwd *result_buf,
226                                 char *buffer, size_t buflen, struct spwd **result)
227 {
228         int rv = ERANGE;
229
230         *result = NULL;
231
232         if (buflen < PWD_BUFFER_SIZE) {
233         DO_ERANGE:
234                 errno=rv;
235                 goto DONE;
236         }
237
238         if (string != buffer) {
239                 if (strlen(string) >= buflen) {
240                         goto DO_ERANGE;
241                 }
242                 strcpy(buffer, string);
243         }
244
245         rv = bb__parsespent(result_buf, buffer);
246         if (!rv) {
247                 *result = result_buf;
248         }
249
250  DONE:
251         return rv;
252 }
253 #endif
254
255 /**********************************************************************/
256
257 #define GETXXKEY_R_FUNC         getpwnam_r
258 #define GETXXKEY_R_PARSER       bb__parsepwent
259 #define GETXXKEY_R_ENTTYPE      struct passwd
260 #define GETXXKEY_R_TEST(ENT)    (!strcmp((ENT)->pw_name, key))
261 #define GETXXKEY_R_KEYTYPE      const char *__restrict
262 #define GETXXKEY_R_PATHNAME     _PATH_PASSWD
263 #include "pwd_grp_internal.c"
264
265 #define GETXXKEY_R_FUNC         getgrnam_r
266 #define GETXXKEY_R_PARSER       bb__parsegrent
267 #define GETXXKEY_R_ENTTYPE      struct group
268 #define GETXXKEY_R_TEST(ENT)    (!strcmp((ENT)->gr_name, key))
269 #define GETXXKEY_R_KEYTYPE      const char *__restrict
270 #define GETXXKEY_R_PATHNAME     _PATH_GROUP
271 #include "pwd_grp_internal.c"
272
273 #if ENABLE_USE_BB_SHADOW
274 #define GETXXKEY_R_FUNC         getspnam_r
275 #define GETXXKEY_R_PARSER       bb__parsespent
276 #define GETXXKEY_R_ENTTYPE      struct spwd
277 #define GETXXKEY_R_TEST(ENT)    (!strcmp((ENT)->sp_namp, key))
278 #define GETXXKEY_R_KEYTYPE      const char *__restrict
279 #define GETXXKEY_R_PATHNAME     _PATH_SHADOW
280 #include "pwd_grp_internal.c"
281 #endif
282
283 #define GETXXKEY_R_FUNC         getpwuid_r
284 #define GETXXKEY_R_PARSER       bb__parsepwent
285 #define GETXXKEY_R_ENTTYPE      struct passwd
286 #define GETXXKEY_R_TEST(ENT)    ((ENT)->pw_uid == key)
287 #define GETXXKEY_R_KEYTYPE      uid_t
288 #define GETXXKEY_R_PATHNAME     _PATH_PASSWD
289 #include "pwd_grp_internal.c"
290
291 #define GETXXKEY_R_FUNC         getgrgid_r
292 #define GETXXKEY_R_PARSER       bb__parsegrent
293 #define GETXXKEY_R_ENTTYPE      struct group
294 #define GETXXKEY_R_TEST(ENT)    ((ENT)->gr_gid == key)
295 #define GETXXKEY_R_KEYTYPE      gid_t
296 #define GETXXKEY_R_PATHNAME     _PATH_GROUP
297 #include "pwd_grp_internal.c"
298
299 /**********************************************************************/
300 /* TODO: audit & stop using these in bbox, they pull in static buffers */
301
302 /* This one has many users */
303 struct passwd *getpwuid(uid_t uid)
304 {
305         struct statics *S;
306         struct passwd *resultbuf = RESULTBUF(getpwuid);
307         char *buffer = BUFFER(getpwuid);
308         struct passwd *result;
309
310         getpwuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getpwuid)), &result);
311         return result;
312 }
313
314 /* This one has many users */
315 struct group *getgrgid(gid_t gid)
316 {
317         struct statics *S;
318         struct group *resultbuf = RESULTBUF(getgrgid);
319         char *buffer = BUFFER(getgrgid);
320         struct group *result;
321
322         getgrgid_r(gid, resultbuf, buffer, sizeof(BUFFER(getgrgid)), &result);
323         return result;
324 }
325
326 #if 0 //ENABLE_USE_BB_SHADOW
327 /* This function is non-standard and is currently not built.  It seems
328  * to have been created as a reentrant version of the non-standard
329  * functions getspuid.  Why getspuid was added, I do not know. */
330 int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf,
331                        char *__restrict buffer, size_t buflen,
332                        struct spwd **__restrict result)
333 {
334         int rv;
335         struct passwd *pp;
336         struct passwd password;
337         char pwd_buff[PWD_BUFFER_SIZE];
338
339         *result = NULL;
340         rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp);
341         if (!rv) {
342                 rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result);
343         }
344
345         return rv;
346 }
347
348 /* This function is non-standard and is currently not built.
349  * Why it was added, I do not know. */
350 struct spwd *getspuid(uid_t uid)
351 {
352         struct statics *S;
353         struct spwd *resultbuf = RESULTBUF(getspuid);
354         char *buffer = BUFFER(getspuid);
355         struct spwd *result;
356
357         getspuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getspuid)), &result);
358         return result;
359 }
360 #endif
361
362 /* This one has many users */
363 struct passwd *getpwnam(const char *name)
364 {
365         struct statics *S;
366         struct passwd *resultbuf = RESULTBUF(getpwnam);
367         char *buffer = BUFFER(getpwnam);
368         struct passwd *result;
369
370         getpwnam_r(name, resultbuf, buffer, sizeof(BUFFER(getpwnam)), &result);
371         return result;
372 }
373
374 /* This one has many users */
375 struct group *getgrnam(const char *name)
376 {
377         struct statics *S;
378         struct group *resultbuf = RESULTBUF(getgrnam);
379         char *buffer = BUFFER(getgrnam);
380         struct group *result;
381
382         getgrnam_r(name, resultbuf, buffer, sizeof(BUFFER(getgrnam)), &result);
383         return result;
384 }
385
386 #if 0 //ENABLE_USE_BB_SHADOW
387 struct spwd *getspnam(const char *name)
388 {
389         struct statics *S;
390         struct spwd *resultbuf = RESULTBUF(getspnam);
391         char *buffer = BUFFER(getspnam);
392         struct spwd *result;
393
394         getspnam_r(name, resultbuf, buffer, sizeof(BUFFER(getspnam)), &result);
395         return result;
396 }
397 #endif
398
399 #ifdef THIS_ONE_IS_UNUSED
400 /* This one doesn't use static buffers */
401 int getpw(uid_t uid, char *buf)
402 {
403         struct passwd resultbuf;
404         struct passwd *result;
405         char buffer[PWD_BUFFER_SIZE];
406
407         if (!buf) {
408                 errno = EINVAL;
409         } else if (!getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result)) {
410                 if (sprintf(buf, "%s:%s:%lu:%lu:%s:%s:%s\n",
411                                         resultbuf.pw_name, resultbuf.pw_passwd,
412                                         (unsigned long)(resultbuf.pw_uid),
413                                         (unsigned long)(resultbuf.pw_gid),
414                                         resultbuf.pw_gecos, resultbuf.pw_dir,
415                                         resultbuf.pw_shell) >= 0
416                         ) {
417                         return 0;
418                 }
419         }
420
421         return -1;
422 }
423 #endif
424
425 /**********************************************************************/
426
427 /* FIXME: we don't have such CONFIG_xx - ?! */
428
429 #if defined CONFIG_USE_BB_THREADSAFE_SHADOW && defined PTHREAD_MUTEX_INITIALIZER
430 static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
431 # define LOCK           pthread_mutex_lock(&mylock)
432 # define UNLOCK         pthread_mutex_unlock(&mylock);
433 #else
434 # define LOCK           ((void) 0)
435 # define UNLOCK         ((void) 0)
436 #endif
437
438 static FILE *pwf /*= NULL*/;
439 void setpwent(void)
440 {
441         LOCK;
442         if (pwf) {
443                 rewind(pwf);
444         }
445         UNLOCK;
446 }
447
448 void endpwent(void)
449 {
450         LOCK;
451         if (pwf) {
452                 fclose(pwf);
453                 pwf = NULL;
454         }
455         UNLOCK;
456 }
457
458
459 int getpwent_r(struct passwd *__restrict resultbuf,
460                            char *__restrict buffer, size_t buflen,
461                            struct passwd **__restrict result)
462 {
463         int rv;
464
465         LOCK;
466         *result = NULL;                         /* In case of error... */
467
468         if (!pwf) {
469                 pwf = fopen_for_read(_PATH_PASSWD);
470                 if (!pwf) {
471                         rv = errno;
472                         goto ERR;
473                 }
474         }
475
476         rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, pwf);
477         if (!rv) {
478                 *result = resultbuf;
479         }
480
481  ERR:
482         UNLOCK;
483         return rv;
484 }
485
486 static FILE *grf /*= NULL*/;
487 void setgrent(void)
488 {
489         LOCK;
490         if (grf) {
491                 rewind(grf);
492         }
493         UNLOCK;
494 }
495
496 void endgrent(void)
497 {
498         LOCK;
499         if (grf) {
500                 fclose(grf);
501                 grf = NULL;
502         }
503         UNLOCK;
504 }
505
506 int getgrent_r(struct group *__restrict resultbuf,
507                            char *__restrict buffer, size_t buflen,
508                            struct group **__restrict result)
509 {
510         int rv;
511
512         LOCK;
513         *result = NULL;                         /* In case of error... */
514
515         if (!grf) {
516                 grf = fopen_for_read(_PATH_GROUP);
517                 if (!grf) {
518                         rv = errno;
519                         goto ERR;
520                 }
521         }
522
523         rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, grf);
524         if (!rv) {
525                 *result = resultbuf;
526         }
527
528  ERR:
529         UNLOCK;
530         return rv;
531 }
532
533 #if ENABLE_USE_BB_SHADOW
534 static FILE *spf /*= NULL*/;
535 void setspent(void)
536 {
537         LOCK;
538         if (spf) {
539                 rewind(spf);
540         }
541         UNLOCK;
542 }
543
544 void endspent(void)
545 {
546         LOCK;
547         if (spf) {
548                 fclose(spf);
549                 spf = NULL;
550         }
551         UNLOCK;
552 }
553
554 int getspent_r(struct spwd *resultbuf, char *buffer,
555                            size_t buflen, struct spwd **result)
556 {
557         int rv;
558
559         LOCK;
560         *result = NULL;                         /* In case of error... */
561
562         if (!spf) {
563                 spf = fopen_for_read(_PATH_SHADOW);
564                 if (!spf) {
565                         rv = errno;
566                         goto ERR;
567                 }
568         }
569
570         rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, spf);
571         if (!rv) {
572                 *result = resultbuf;
573         }
574
575  ERR:
576         UNLOCK;
577         return rv;
578 }
579 #endif
580
581 #if 0
582 struct passwd *getpwent(void)
583 {
584         static char line_buff[PWD_BUFFER_SIZE];
585         static struct passwd pwd;
586         struct passwd *result;
587
588         getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
589         return result;
590 }
591
592 struct group *getgrent(void)
593 {
594         static char line_buff[GRP_BUFFER_SIZE];
595         static struct group gr;
596         struct group *result;
597
598         getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
599         return result;
600 }
601 #endif
602
603 #if 0 //ENABLE_USE_BB_SHADOW
604 struct spwd *getspent(void)
605 {
606         static char line_buff[PWD_BUFFER_SIZE];
607         static struct spwd spwd;
608         struct spwd *result;
609
610         getspent_r(&spwd, line_buff, sizeof(line_buff), &result);
611         return result;
612 }
613
614 struct spwd *sgetspent(const char *string)
615 {
616         static char line_buff[PWD_BUFFER_SIZE];
617         static struct spwd spwd;
618         struct spwd *result;
619
620         sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result);
621         return result;
622 }
623 #endif
624
625 static gid_t *getgrouplist_internal(int *ngroups_ptr, const char *user, gid_t gid)
626 {
627         FILE *grfile;
628         gid_t *group_list;
629         int ngroups;
630         struct group group;
631         char buff[PWD_BUFFER_SIZE];
632
633         /* We alloc space for 8 gids at a time. */
634         group_list = xmalloc(8 * sizeof(group_list[0]));
635         group_list[0] = gid;
636         ngroups = 1;
637
638         grfile = fopen_for_read(_PATH_GROUP);
639         if (grfile) {
640                 while (!bb__pgsreader(bb__parsegrent, &group, buff, sizeof(buff), grfile)) {
641                         char **m;
642                         assert(group.gr_mem); /* Must have at least a NULL terminator. */
643                         if (group.gr_gid == gid)
644                                 continue;
645                         for (m = group.gr_mem; *m; m++) {
646                                 if (strcmp(*m, user) != 0)
647                                         continue;
648                                 group_list = xrealloc_vector(group_list, 3, ngroups);
649                                 group_list[ngroups++] = group.gr_gid;
650                                 break;
651                         }
652                 }
653                 fclose(grfile);
654         }
655         *ngroups_ptr = ngroups;
656         return group_list;
657 }
658
659 int initgroups(const char *user, gid_t gid)
660 {
661         int ngroups;
662         gid_t *group_list = getgrouplist_internal(&ngroups, user, gid);
663
664         if (!group_list)
665                 return -1;
666
667         ngroups = setgroups(ngroups, group_list);
668         free(group_list);
669         return ngroups;
670 }
671
672 /* TODO: uclibc needs this ported to it! */
673 int getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups)
674 {
675         int ngroups_old = *ngroups;
676         gid_t *group_list = getgrouplist_internal(ngroups, user, gid);
677
678         if (*ngroups <= ngroups_old) {
679                 ngroups_old = *ngroups;
680                 memcpy(groups, group_list, ngroups_old * sizeof(groups[0]));
681         } else {
682                 ngroups_old = -1;
683         }
684         free(group_list);
685         return ngroups_old;
686 }
687
688 int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
689 {
690         int rv = -1;
691
692         if (!p || !f) {
693                 errno = EINVAL;
694         } else {
695                 /* No extra thread locking is needed above what fprintf does. */
696                 if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n",
697                                         p->pw_name, p->pw_passwd,
698                                         (unsigned long)(p->pw_uid),
699                                         (unsigned long)(p->pw_gid),
700                                         p->pw_gecos, p->pw_dir, p->pw_shell) >= 0
701                         ) {
702                         rv = 0;
703                 }
704         }
705
706         return rv;
707 }
708
709 int putgrent(const struct group *__restrict p, FILE *__restrict f)
710 {
711         static const char format[] ALIGN1 = ",%s";
712
713         char **m;
714         const char *fmt;
715         int rv = -1;
716
717         if (!p || !f) {                         /* Sigh... glibc checks. */
718                 errno = EINVAL;
719         } else {
720                 if (fprintf(f, "%s:%s:%lu:",
721                                         p->gr_name, p->gr_passwd,
722                                         (unsigned long)(p->gr_gid)) >= 0
723                         ) {
724
725                         fmt = format + 1;
726
727                         assert(p->gr_mem);
728                         m = p->gr_mem;
729
730                         do {
731                                 if (!*m) {
732                                         if (fputc('\n', f) >= 0) {
733                                                 rv = 0;
734                                         }
735                                         break;
736                                 }
737                                 if (fprintf(f, fmt, *m) < 0) {
738                                         break;
739                                 }
740                                 ++m;
741                                 fmt = format;
742                         } while (1);
743
744                 }
745
746         }
747
748         return rv;
749 }
750
751 #if ENABLE_USE_BB_SHADOW
752 static const unsigned char _sp_off[] ALIGN1 = {
753         offsetof(struct spwd, sp_lstchg),       /* 2 - not a char ptr */
754         offsetof(struct spwd, sp_min),          /* 3 - not a char ptr */
755         offsetof(struct spwd, sp_max),          /* 4 - not a char ptr */
756         offsetof(struct spwd, sp_warn),         /* 5 - not a char ptr */
757         offsetof(struct spwd, sp_inact),        /* 6 - not a char ptr */
758         offsetof(struct spwd, sp_expire)        /* 7 - not a char ptr */
759 };
760
761 int putspent(const struct spwd *p, FILE *stream)
762 {
763         static const char ld_format[] ALIGN1 = "%ld:";
764
765         const char *f;
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(_sp_off); i++) {
778                 f = ld_format;
779                 x = *(const long *)(((const char *) p) + _sp_off[i]);
780                 if (x == -1) {
781                         f += 3;
782                 }
783                 if (fprintf(stream, f, 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 bb__parsepwent(void *data, char *line)
816 {
817         char *endptr;
818         char *p;
819         int i;
820
821         i = 0;
822         do {
823                 p = ((char *) ((struct passwd *) data)) + pw_off[i];
824
825                 if ((i & 6) ^ 2) {      /* i!=2 and 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 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         do {
880                 p = ((char *) ((struct group *) 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                                 do {
943                                         *members++ = ++p;
944                                         if (!--i) break;
945                                         while (*++p) {}
946                                 } while (1);
947                         }
948                         *members = NULL;
949
950                         return 0;
951                 }
952         } while (1);
953
954  ERR:
955         return -1;
956 }
957
958 /**********************************************************************/
959
960 #if ENABLE_USE_BB_SHADOW
961 static const unsigned char sp_off[] ALIGN1 = {
962         offsetof(struct spwd, sp_namp),         /* 0 */
963         offsetof(struct spwd, sp_pwdp),         /* 1 */
964         offsetof(struct spwd, sp_lstchg),       /* 2 - not a char ptr */
965         offsetof(struct spwd, sp_min),          /* 3 - not a char ptr */
966         offsetof(struct spwd, sp_max),          /* 4 - not a char ptr */
967         offsetof(struct spwd, sp_warn),         /* 5 - not a char ptr */
968         offsetof(struct spwd, sp_inact),        /* 6 - not a char ptr */
969         offsetof(struct spwd, sp_expire),       /* 7 - not a char ptr */
970         offsetof(struct spwd, sp_flag)          /* 8 - not a char ptr */
971 };
972
973 static int bb__parsespent(void *data, char * line)
974 {
975         char *endptr;
976         char *p;
977         int i;
978
979         i = 0;
980         do {
981                 p = ((char *) ((struct spwd *) data)) + sp_off[i];
982                 if (i < 2) {
983                         *((char **) p) = line;
984                         line = strchr(line, ':');
985                         if (!line) {
986                                 break;
987                         }
988                 } else {
989                         *((long *) p) = (long) strtoul(line, &endptr, 10);
990
991                         if (endptr == line) {
992                                 *((long *) p) = ((i != 8) ? -1L : ((long)(~0UL)));
993                         }
994
995                         line = endptr;
996
997                         if (i == 8) {
998                                 if (!*endptr) {
999                                         return 0;
1000                                 }
1001                                 break;
1002                         }
1003
1004                         if (*endptr != ':') {
1005                                 break;
1006                         }
1007
1008                 }
1009
1010                 *line++ = 0;
1011                 ++i;
1012         } while (1);
1013
1014         return EINVAL;
1015 }
1016 #endif
1017
1018 /**********************************************************************/
1019
1020 /* Reads until if EOF, or until if finds a line which fits in the buffer
1021  * and for which the parser function succeeds.
1022  *
1023  * Returns 0 on success and ENOENT for end-of-file (glibc concession).
1024  */
1025
1026 static int bb__pgsreader(int (*parserfunc)(void *d, char *line), void *data,
1027                                 char *__restrict line_buff, size_t buflen, FILE *f)
1028 {
1029         int line_len;
1030         int skip;
1031         int rv = ERANGE;
1032
1033         if (buflen < PWD_BUFFER_SIZE) {
1034                 errno = rv;
1035         } else {
1036                 skip = 0;
1037                 do {
1038                         if (!fgets(line_buff, buflen, f)) {
1039                                 if (feof(f)) {
1040                                         rv = ENOENT;
1041                                 }
1042                                 break;
1043                         }
1044
1045                         line_len = strlen(line_buff) - 1; /* strlen() must be > 0. */
1046                         if (line_buff[line_len] == '\n') {
1047                                 line_buff[line_len] = 0;
1048                         } else if (line_len + 2 == buflen) { /* line too long */
1049                                 ++skip;
1050                                 continue;
1051                         }
1052
1053                         if (skip) {
1054                                 --skip;
1055                                 continue;
1056                         }
1057
1058                         /* NOTE: glibc difference - glibc strips leading whitespace from
1059                          * records.  We do not allow leading whitespace. */
1060
1061                         /* Skip empty lines, comment lines, and lines with leading
1062                          * whitespace. */
1063                         if (*line_buff && (*line_buff != '#') && !isspace(*line_buff)) {
1064                                 if (parserfunc == bb__parsegrent) {     /* Do evil group hack. */
1065                                         /* The group entry parsing function needs to know where
1066                                          * the end of the buffer is so that it can construct the
1067                                          * group member ptr table. */
1068                                         ((struct group *) data)->gr_name = line_buff + buflen;
1069                                 }
1070
1071                                 if (!parserfunc(data, line_buff)) {
1072                                         rv = 0;
1073                                         break;
1074                                 }
1075                         }
1076                 } while (1);
1077
1078         }
1079
1080         return rv;
1081 }