5c61e56386ae49254c65d18b454a2ea1c7e26e05
[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 <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 #ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
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 /**********************************************************************/
403
404 /* FIXME: we don't have such CONFIG_xx - ?! */
405
406 #if defined CONFIG_USE_BB_THREADSAFE_SHADOW && defined PTHREAD_MUTEX_INITIALIZER
407 static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
408 # define LOCK           pthread_mutex_lock(&mylock)
409 # define UNLOCK         pthread_mutex_unlock(&mylock);
410 #else
411 # define LOCK           ((void) 0)
412 # define UNLOCK         ((void) 0)
413 #endif
414
415 static FILE *pwf /*= NULL*/;
416 void setpwent(void)
417 {
418         LOCK;
419         if (pwf) {
420                 rewind(pwf);
421         }
422         UNLOCK;
423 }
424
425 void endpwent(void)
426 {
427         LOCK;
428         if (pwf) {
429                 fclose(pwf);
430                 pwf = NULL;
431         }
432         UNLOCK;
433 }
434
435
436 int getpwent_r(struct passwd *__restrict resultbuf,
437                            char *__restrict buffer, size_t buflen,
438                            struct passwd **__restrict result)
439 {
440         int rv;
441
442         LOCK;
443         *result = NULL;                         /* In case of error... */
444
445         if (!pwf) {
446                 pwf = fopen_for_read(_PATH_PASSWD);
447                 if (!pwf) {
448                         rv = errno;
449                         goto ERR;
450                 }
451         }
452
453         rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, pwf);
454         if (!rv) {
455                 *result = resultbuf;
456         }
457
458  ERR:
459         UNLOCK;
460         return rv;
461 }
462
463 static FILE *grf /*= NULL*/;
464 void setgrent(void)
465 {
466         LOCK;
467         if (grf) {
468                 rewind(grf);
469         }
470         UNLOCK;
471 }
472
473 void endgrent(void)
474 {
475         LOCK;
476         if (grf) {
477                 fclose(grf);
478                 grf = NULL;
479         }
480         UNLOCK;
481 }
482
483 int getgrent_r(struct group *__restrict resultbuf,
484                            char *__restrict buffer, size_t buflen,
485                            struct group **__restrict result)
486 {
487         int rv;
488
489         LOCK;
490         *result = NULL;                         /* In case of error... */
491
492         if (!grf) {
493                 grf = fopen_for_read(_PATH_GROUP);
494                 if (!grf) {
495                         rv = errno;
496                         goto ERR;
497                 }
498         }
499
500         rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, grf);
501         if (!rv) {
502                 *result = resultbuf;
503         }
504
505  ERR:
506         UNLOCK;
507         return rv;
508 }
509
510 #if ENABLE_USE_BB_SHADOW
511 static FILE *spf /*= NULL*/;
512 void setspent(void)
513 {
514         LOCK;
515         if (spf) {
516                 rewind(spf);
517         }
518         UNLOCK;
519 }
520
521 void endspent(void)
522 {
523         LOCK;
524         if (spf) {
525                 fclose(spf);
526                 spf = NULL;
527         }
528         UNLOCK;
529 }
530
531 int getspent_r(struct spwd *resultbuf, char *buffer,
532                            size_t buflen, struct spwd **result)
533 {
534         int rv;
535
536         LOCK;
537         *result = NULL;                         /* In case of error... */
538
539         if (!spf) {
540                 spf = fopen_for_read(_PATH_SHADOW);
541                 if (!spf) {
542                         rv = errno;
543                         goto ERR;
544                 }
545         }
546
547         rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, spf);
548         if (!rv) {
549                 *result = resultbuf;
550         }
551
552  ERR:
553         UNLOCK;
554         return rv;
555 }
556 #endif
557
558 #ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
559 struct passwd *getpwent(void)
560 {
561         static char line_buff[PWD_BUFFER_SIZE];
562         static struct passwd pwd;
563         struct passwd *result;
564
565         getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
566         return result;
567 }
568
569 struct group *getgrent(void)
570 {
571         static char line_buff[GRP_BUFFER_SIZE];
572         static struct group gr;
573         struct group *result;
574
575         getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
576         return result;
577 }
578 #endif
579
580 #if 0 //ENABLE_USE_BB_SHADOW
581 struct spwd *getspent(void)
582 {
583         static char line_buff[PWD_BUFFER_SIZE];
584         static struct spwd spwd;
585         struct spwd *result;
586
587         getspent_r(&spwd, line_buff, sizeof(line_buff), &result);
588         return result;
589 }
590
591 struct spwd *sgetspent(const char *string)
592 {
593         static char line_buff[PWD_BUFFER_SIZE];
594         static struct spwd spwd;
595         struct spwd *result;
596
597         sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result);
598         return result;
599 }
600 #endif
601
602 static gid_t *getgrouplist_internal(int *ngroups_ptr, const char *user, gid_t gid)
603 {
604         FILE *grfile;
605         gid_t *group_list;
606         int ngroups;
607         struct group group;
608         char buff[PWD_BUFFER_SIZE];
609
610         /* We alloc space for 8 gids at a time. */
611         group_list = xmalloc(8 * sizeof(group_list[0]));
612         group_list[0] = gid;
613         ngroups = 1;
614
615         grfile = fopen_for_read(_PATH_GROUP);
616         if (grfile) {
617                 while (!bb__pgsreader(bb__parsegrent, &group, buff, sizeof(buff), grfile)) {
618                         char **m;
619                         assert(group.gr_mem); /* Must have at least a NULL terminator. */
620                         if (group.gr_gid == gid)
621                                 continue;
622                         for (m = group.gr_mem; *m; m++) {
623                                 if (strcmp(*m, user) != 0)
624                                         continue;
625                                 group_list = xrealloc_vector(group_list, /*8=2^3:*/ 3, ngroups);
626                                 group_list[ngroups++] = group.gr_gid;
627                                 break;
628                         }
629                 }
630                 fclose(grfile);
631         }
632         *ngroups_ptr = ngroups;
633         return group_list;
634 }
635
636 int initgroups(const char *user, gid_t gid)
637 {
638         int ngroups;
639         gid_t *group_list = getgrouplist_internal(&ngroups, user, gid);
640
641         ngroups = setgroups(ngroups, group_list);
642         free(group_list);
643         return ngroups;
644 }
645
646 int getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups)
647 {
648         int ngroups_old = *ngroups;
649         gid_t *group_list = getgrouplist_internal(ngroups, user, gid);
650
651         if (*ngroups <= ngroups_old) {
652                 ngroups_old = *ngroups;
653                 memcpy(groups, group_list, ngroups_old * sizeof(groups[0]));
654         } else {
655                 ngroups_old = -1;
656         }
657         free(group_list);
658         return ngroups_old;
659 }
660
661 #ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
662 int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
663 {
664         int rv = -1;
665
666         if (!p || !f) {
667                 errno = EINVAL;
668                 return rv;
669         }
670
671         /* No extra thread locking is needed above what fprintf does. */
672         if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n",
673                                 p->pw_name, p->pw_passwd,
674                                 (unsigned long)(p->pw_uid),
675                                 (unsigned long)(p->pw_gid),
676                                 p->pw_gecos, p->pw_dir, p->pw_shell) >= 0
677                 ) {
678                 rv = 0;
679         }
680
681         return rv;
682 }
683
684 int putgrent(const struct group *__restrict p, FILE *__restrict f)
685 {
686         int rv = -1;
687
688         if (!p || !f) {                         /* Sigh... glibc checks. */
689                 errno = EINVAL;
690                 return rv;
691         }
692
693         if (fprintf(f, "%s:%s:%lu:",
694                                 p->gr_name, p->gr_passwd,
695                                 (unsigned long)(p->gr_gid)) >= 0
696         ) {
697                 static const char format[] ALIGN1 = ",%s";
698
699                 char **m;
700                 const char *fmt;
701
702                 fmt = format + 1;
703
704                 assert(p->gr_mem);
705                 m = p->gr_mem;
706
707                 while (1) {
708                         if (!*m) {
709                                 if (fputc('\n', f) >= 0) {
710                                         rv = 0;
711                                 }
712                                 break;
713                         }
714                         if (fprintf(f, fmt, *m) < 0) {
715                                 break;
716                         }
717                         m++;
718                         fmt = format;
719                 }
720         }
721
722         return rv;
723 }
724 #endif
725
726 #if ENABLE_USE_BB_SHADOW
727 static const unsigned char put_sp_off[] ALIGN1 = {
728         offsetof(struct spwd, sp_lstchg),       /* 2 - not a char ptr */
729         offsetof(struct spwd, sp_min),          /* 3 - not a char ptr */
730         offsetof(struct spwd, sp_max),          /* 4 - not a char ptr */
731         offsetof(struct spwd, sp_warn),         /* 5 - not a char ptr */
732         offsetof(struct spwd, sp_inact),        /* 6 - not a char ptr */
733         offsetof(struct spwd, sp_expire)        /* 7 - not a char ptr */
734 };
735
736 int putspent(const struct spwd *p, FILE *stream)
737 {
738         const char *fmt;
739         long x;
740         int i;
741         int rv = -1;
742
743         /* Unlike putpwent and putgrent, glibc does not check the args. */
744         if (fprintf(stream, "%s:%s:", p->sp_namp,
745                                 (p->sp_pwdp ? p->sp_pwdp : "")) < 0
746         ) {
747                 goto DO_UNLOCK;
748         }
749
750         for (i = 0; i < sizeof(put_sp_off); i++) {
751                 fmt = "%ld:";
752                 x = *(long *)((char *)p + put_sp_off[i]);
753                 if (x == -1) {
754                         fmt += 3;
755                 }
756                 if (fprintf(stream, fmt, x) < 0) {
757                         goto DO_UNLOCK;
758                 }
759         }
760
761         if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) {
762                 goto DO_UNLOCK;
763         }
764
765         if (fputc('\n', stream) > 0) {
766                 rv = 0;
767         }
768
769  DO_UNLOCK:
770         return rv;
771 }
772 #endif
773
774 /**********************************************************************/
775 /* Internal uClibc functions.                                         */
776 /**********************************************************************/
777
778 static const unsigned char pw_off[] ALIGN1 = {
779         offsetof(struct passwd, pw_name),       /* 0 */
780         offsetof(struct passwd, pw_passwd),     /* 1 */
781         offsetof(struct passwd, pw_uid),        /* 2 - not a char ptr */
782         offsetof(struct passwd, pw_gid),        /* 3 - not a char ptr */
783         offsetof(struct passwd, pw_gecos),      /* 4 */
784         offsetof(struct passwd, pw_dir),        /* 5 */
785         offsetof(struct passwd, pw_shell)       /* 6 */
786 };
787
788 static int FAST_FUNC bb__parsepwent(void *data, char *line)
789 {
790         char *endptr;
791         char *p;
792         int i;
793
794         i = 0;
795         while (1) {
796                 p = (char *) data + pw_off[i];
797
798                 if (i < 2 || i > 3) {
799                         *((char **) p) = line;
800                         if (i == 6) {
801                                 return 0;
802                         }
803                         /* NOTE: glibc difference - glibc allows omission of
804                          * ':' seperators after the gid field if all remaining
805                          * entries are empty.  We require all separators. */
806                         line = strchr(line, ':');
807                         if (!line) {
808                                 break;
809                         }
810                 } else {
811                         unsigned long t = strtoul(line, &endptr, 10);
812                         /* Make sure we had at least one digit, and that the
813                          * failing char is the next field seperator ':'.  See
814                          * glibc difference note above. */
815                         /* TODO: Also check for leading whitespace? */
816                         if ((endptr == line) || (*endptr != ':')) {
817                                 break;
818                         }
819                         line = endptr;
820                         if (i & 1) {            /* i == 3 -- gid */
821                                 *((gid_t *) p) = t;
822                         } else {                        /* i == 2 -- uid */
823                                 *((uid_t *) p) = t;
824                         }
825                 }
826
827                 *line++ = '\0';
828                 i++;
829         } /* while (1) */
830
831         return -1;
832 }
833
834 /**********************************************************************/
835
836 static const unsigned char gr_off[] ALIGN1 = {
837         offsetof(struct group, gr_name),        /* 0 */
838         offsetof(struct group, gr_passwd),      /* 1 */
839         offsetof(struct group, gr_gid)          /* 2 - not a char ptr */
840 };
841
842 static int FAST_FUNC bb__parsegrent(void *data, char *line)
843 {
844         char *endptr;
845         char *p;
846         int i;
847         char **members;
848         char *end_of_buf;
849
850         end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */
851         i = 0;
852         while (1) {
853                 p = (char *) data + gr_off[i];
854
855                 if (i < 2) {
856                         *((char **) p) = line;
857                         line = strchr(line, ':');
858                         if (!line) {
859                                 break;
860                         }
861                         *line++ = '\0';
862                         i++;
863                 } else {
864                         *((gid_t *) p) = strtoul(line, &endptr, 10);
865
866                         /* NOTE: glibc difference - glibc allows omission of the
867                          * trailing colon when there is no member list.  We treat
868                          * this as an error. */
869
870                         /* Make sure we had at least one digit, and that the
871                          * failing char is the next field seperator ':'.  See
872                          * glibc difference note above. */
873                         if ((endptr == line) || (*endptr != ':')) {
874                                 break;
875                         }
876
877                         i = 1;                          /* Count terminating NULL ptr. */
878                         p = endptr;
879
880                         if (p[1]) { /* We have a member list to process. */
881                                 /* Overwrite the last ':' with a ',' before counting.
882                                  * This allows us to test for initial ',' and adds
883                                  * one ',' so that the ',' count equals the member
884                                  * count. */
885                                 *p = ',';
886                                 do {
887                                         /* NOTE: glibc difference - glibc allows and trims leading
888                                          * (but not trailing) space.  We treat this as an error. */
889                                         /* NOTE: glibc difference - glibc allows consecutive and
890                                          * trailing commas, and ignores "empty string" users.  We
891                                          * treat this as an error. */
892                                         if (*p == ',') {
893                                                 ++i;
894                                                 *p = 0; /* nul-terminate each member string. */
895                                                 if (!*++p || (*p == ',') || isspace(*p)) {
896                                                         goto ERR;
897                                                 }
898                                         }
899                                 } while (*++p);
900                         }
901
902                         /* Now align (p+1), rounding up. */
903                         /* Assumes sizeof(char **) is a power of 2. */
904                         members = (char **)( (((intptr_t) p) + sizeof(char **))
905                                                                  & ~((intptr_t)(sizeof(char **) - 1)) );
906
907                         if (((char *)(members + i)) > end_of_buf) {     /* No space. */
908                                 break;
909                         }
910
911                         ((struct group *) data)->gr_mem = members;
912
913                         if (--i) {
914                                 p = endptr;     /* Pointing to char prior to first member. */
915                                 while (1) {
916                                         *members++ = ++p;
917                                         if (!--i)
918                                                 break;
919                                         while (*++p)
920                                                 continue;
921                                 }
922                         }
923                         *members = NULL;
924
925                         return 0;
926                 }
927         } /* while (1) */
928
929  ERR:
930         return -1;
931 }
932
933 /**********************************************************************/
934
935 #if ENABLE_USE_BB_SHADOW
936 static const unsigned char sp_off[] ALIGN1 = {
937         offsetof(struct spwd, sp_namp),         /* 0: char* */
938         offsetof(struct spwd, sp_pwdp),         /* 1: char* */
939         offsetof(struct spwd, sp_lstchg),       /* 2: long */
940         offsetof(struct spwd, sp_min),          /* 3: long */
941         offsetof(struct spwd, sp_max),          /* 4: long */
942         offsetof(struct spwd, sp_warn),         /* 5: long */
943         offsetof(struct spwd, sp_inact),        /* 6: long */
944         offsetof(struct spwd, sp_expire),       /* 7: long */
945         offsetof(struct spwd, sp_flag)          /* 8: unsigned long */
946 };
947
948 static int FAST_FUNC bb__parsespent(void *data, char *line)
949 {
950         char *endptr;
951         char *p;
952         int i;
953
954         i = 0;
955         while (1) {
956                 p = (char *) data + sp_off[i];
957                 if (i < 2) {
958                         *((char **) p) = line;
959                         line = strchr(line, ':');
960                         if (!line) {
961                                 break; /* error */
962                         }
963                 } else {
964                         *((long *) p) = strtoul(line, &endptr, 10);
965                         if (endptr == line) {
966                                 *((long *) p) = -1L;
967                         }
968                         line = endptr;
969                         if (i == 8) {
970                                 if (*line != '\0') {
971                                         break; /* error */
972                                 }
973                                 return 0; /* all ok */
974                         }
975                         if (*line != ':') {
976                                 break; /* error */
977                         }
978                 }
979                 *line++ = '\0';
980                 i++;
981         }
982
983         return EINVAL;
984 }
985 #endif
986
987 /**********************************************************************/
988
989 /* Reads until if EOF, or until if finds a line which fits in the buffer
990  * and for which the parser function succeeds.
991  *
992  * Returns 0 on success and ENOENT for end-of-file (glibc concession).
993  */
994 static int bb__pgsreader(
995                 int FAST_FUNC (*parserfunc)(void *d, char *line),
996                 void *data,
997                 char *__restrict line_buff,
998                 size_t buflen,
999                 FILE *f)
1000 {
1001         int skip;
1002         int rv = ERANGE;
1003
1004         if (buflen < PWD_BUFFER_SIZE) {
1005                 errno = rv;
1006                 return rv;
1007         }
1008
1009         skip = 0;
1010         while (1) {
1011                 if (!fgets(line_buff, buflen, f)) {
1012                         if (feof(f)) {
1013                                 rv = ENOENT;
1014                         }
1015                         break;
1016                 }
1017
1018                 {
1019                         int line_len = strlen(line_buff) - 1;
1020                         if (line_len >= 0 && line_buff[line_len] == '\n') {
1021                                 line_buff[line_len] = '\0';
1022                         } else
1023                         if (line_len + 2 == buflen) {
1024                                 /* A start (or continuation) of overlong line */
1025                                 skip = 1;
1026                                 continue;
1027                         } /* else: a last line in the file, and it has no '\n' */
1028                 }
1029
1030                 if (skip) {
1031                         /* This "line" is a remainder of overlong line, ignore */
1032                         skip = 0;
1033                         continue;
1034                 }
1035
1036                 /* NOTE: glibc difference - glibc strips leading whitespace from
1037                  * records.  We do not allow leading whitespace. */
1038
1039                 /* Skip empty lines, comment lines, and lines with leading
1040                  * whitespace. */
1041                 if (line_buff[0] != '\0' && line_buff[0] != '#' && !isspace(line_buff[0])) {
1042                         if (parserfunc == bb__parsegrent) {
1043                                 /* Do evil group hack:
1044                                  * The group entry parsing function needs to know where
1045                                  * the end of the buffer is so that it can construct the
1046                                  * group member ptr table. */
1047                                 ((struct group *) data)->gr_name = line_buff + buflen;
1048                         }
1049                         if (parserfunc(data, line_buff) == 0) {
1050                                 rv = 0;
1051                                 break;
1052                         }
1053                 }
1054         } /* while (1) */
1055
1056         return rv;
1057 }