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