libc: Fix some more -Wmissing-prototype.
[dragonfly.git] / lib / libc / gen / getgrent.c
CommitLineData
ed5d5720
PA
1/*-
2 * Copyright (c) 2003 Networks Associates Technology, Inc.
3 * All rights reserved.
4 *
5 * This software was developed for the FreeBSD Project by
6 * Jacques A. Vidrine, Safeport Network Services, and Network
7 * Associates Laboratories, the Security Research Division of Network
8 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
9 * ("CBOSS"), as part of the DARPA CHATS research program.
984263bc
MD
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
984263bc 19 *
ed5d5720 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
984263bc
MD
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ed5d5720 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
984263bc
MD
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
1de703da 31 *
ed5d5720 32 * $FreeBSD: src/lib/libc/gen/getgrent.c,v 1.37 2007/12/12 10:08:02 bushman Exp $
984263bc
MD
33 */
34
ed5d5720
PA
35#include "namespace.h"
36#include <sys/param.h>
37#ifdef YP
38#include <rpc/rpc.h>
39#include <rpcsvc/yp_prot.h>
40#include <rpcsvc/ypclnt.h>
41#endif
42#include <assert.h>
43#include <ctype.h>
984263bc 44#include <errno.h>
ed5d5720
PA
45#ifdef HESIOD
46#include <hesiod.h>
47#endif
48#include <grp.h>
49#include <nsswitch.h>
50#include <pthread.h>
51#include <pthread_np.h>
984263bc
MD
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
984263bc 55#include <syslog.h>
ed5d5720
PA
56#include <unistd.h>
57#include "un-namespace.h"
58#include "libc_private.h"
59#include "nss_tls.h"
60#ifdef NS_CACHING
61#include "nscache.h"
62#endif
63
64enum constants {
65 GRP_STORAGE_INITIAL = 1 << 10, /* 1 KByte */
66 GRP_STORAGE_MAX = 1 << 20, /* 1 MByte */
67 SETGRENT = 1,
68 ENDGRENT = 2,
69 HESIOD_NAME_MAX = 256,
70};
71
72static const ns_src defaultsrc[] = {
73 { NSSRC_COMPAT, NS_SUCCESS },
74 { NULL, 0 }
75};
984263bc 76
f45ae2bf 77int __getgroupmembership(const char *, gid_t, gid_t *, int, int *);
ed5d5720
PA
78int __gr_match_entry(const char *, size_t, enum nss_lookup_type,
79 const char *, gid_t);
80int __gr_parse_entry(char *, size_t, struct group *, char *, size_t,
81 int *);
82
83static int is_comment_line(const char *, size_t);
84
85union key {
86 const char *name;
87 gid_t gid;
88};
89static struct group *getgr(int (*)(union key, struct group *, char *, size_t,
90 struct group **), union key);
91static int wrap_getgrnam_r(union key, struct group *, char *, size_t,
92 struct group **);
93static int wrap_getgrgid_r(union key, struct group *, char *, size_t,
94 struct group **);
95static int wrap_getgrent_r(union key, struct group *, char *, size_t,
96 struct group **);
97
98struct files_state {
99 FILE *fp;
100 int stayopen;
101};
102static void files_endstate(void *);
103NSS_TLS_HANDLING(files);
104static int files_setgrent(void *, void *, va_list);
105static int files_group(void *, void *, va_list);
106
107
108#ifdef HESIOD
109struct dns_state {
110 long counter;
111};
112static void dns_endstate(void *);
113NSS_TLS_HANDLING(dns);
114static int dns_setgrent(void *, void *, va_list);
115static int dns_group(void *, void *, va_list);
116#endif
76f89ad8 117
76f89ad8 118
984263bc 119#ifdef YP
ed5d5720
PA
120struct nis_state {
121 char domain[MAXHOSTNAMELEN];
122 int done;
123 char *key;
124 int keylen;
125};
126static void nis_endstate(void *);
127NSS_TLS_HANDLING(nis);
128static int nis_setgrent(void *, void *, va_list);
129static int nis_group(void *, void *, va_list);
984263bc
MD
130#endif
131
ed5d5720
PA
132struct compat_state {
133 FILE *fp;
134 int stayopen;
135 char *name;
136 enum _compat {
137 COMPAT_MODE_OFF = 0,
138 COMPAT_MODE_ALL,
139 COMPAT_MODE_NAME
140 } compat;
141};
142static void compat_endstate(void *);
143NSS_TLS_HANDLING(compat);
144static int compat_setgrent(void *, void *, va_list);
145static int compat_group(void *, void *, va_list);
984263bc 146
ed5d5720
PA
147static int gr_addgid(gid_t, gid_t *, int, int *);
148static int getgroupmembership_fallback(void *, void *, va_list);
984263bc 149
ed5d5720
PA
150#ifdef NS_CACHING
151static int grp_id_func(char *, size_t *, va_list, void *);
152static int grp_marshal_func(char *, size_t *, void *, va_list, void *);
153static int grp_unmarshal_func(char *, size_t, void *, va_list, void *);
984263bc 154
ed5d5720
PA
155static int
156grp_id_func(char *buffer, size_t *buffer_size, va_list ap, void *cache_mdata)
984263bc 157{
ed5d5720
PA
158 char *name;
159 gid_t gid;
160
161 size_t desired_size, size;
162 int res = NS_UNAVAIL;
163 enum nss_lookup_type lookup_type;
164
165
166 lookup_type = (enum nss_lookup_type)cache_mdata;
167 switch (lookup_type) {
168 case nss_lt_name:
169 name = va_arg(ap, char *);
170 size = strlen(name);
171 desired_size = sizeof(enum nss_lookup_type) + size + 1;
172 if (desired_size > *buffer_size) {
173 res = NS_RETURN;
174 goto fin;
175 }
176
177 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
178 memcpy(buffer + sizeof(enum nss_lookup_type), name, size + 1);
179
180 res = NS_SUCCESS;
181 break;
182 case nss_lt_id:
183 gid = va_arg(ap, gid_t);
184 desired_size = sizeof(enum nss_lookup_type) + sizeof(gid_t);
185 if (desired_size > *buffer_size) {
186 res = NS_RETURN;
187 goto fin;
188 }
189
190 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
191 memcpy(buffer + sizeof(enum nss_lookup_type), &gid,
192 sizeof(gid_t));
193
194 res = NS_SUCCESS;
195 break;
196 default:
197 /* should be unreachable */
198 return (NS_UNAVAIL);
984263bc
MD
199 }
200
ed5d5720
PA
201fin:
202 *buffer_size = desired_size;
203 return (res);
204}
205
206static int
6d7019e6
SW
207grp_marshal_func(char *buffer, size_t *buffer_size, void *retval __unused,
208 va_list ap, void *cache_mdata)
ed5d5720 209{
cf515c3a
JM
210 char *name __unused;
211 gid_t gid __unused;
ed5d5720 212 struct group *grp;
cf515c3a
JM
213 char *orig_buf __unused;
214 size_t orig_buf_size __unused;
ed5d5720
PA
215
216 struct group new_grp;
217 size_t desired_size, size, mem_size;
218 char *p, **mem;
219
220 switch ((enum nss_lookup_type)cache_mdata) {
221 case nss_lt_name:
222 name = va_arg(ap, char *);
223 break;
224 case nss_lt_id:
225 gid = va_arg(ap, gid_t);
226 break;
227 case nss_lt_all:
228 break;
229 default:
230 /* should be unreachable */
231 return (NS_UNAVAIL);
984263bc 232 }
984263bc 233
ed5d5720
PA
234 grp = va_arg(ap, struct group *);
235 orig_buf = va_arg(ap, char *);
236 orig_buf_size = va_arg(ap, size_t);
237
238 desired_size = _ALIGNBYTES + sizeof(struct group) + sizeof(char *);
239
240 if (grp->gr_name != NULL)
241 desired_size += strlen(grp->gr_name) + 1;
242 if (grp->gr_passwd != NULL)
243 desired_size += strlen(grp->gr_passwd) + 1;
244
245 if (grp->gr_mem != NULL) {
246 mem_size = 0;
247 for (mem = grp->gr_mem; *mem; ++mem) {
248 desired_size += strlen(*mem) + 1;
249 ++mem_size;
250 }
251
252 desired_size += _ALIGNBYTES + (mem_size + 1) * sizeof(char *);
984263bc 253 }
ed5d5720
PA
254
255 if (desired_size > *buffer_size) {
256 /* this assignment is here for future use */
257 *buffer_size = desired_size;
258 return (NS_RETURN);
259 }
260
261 memcpy(&new_grp, grp, sizeof(struct group));
262 memset(buffer, 0, desired_size);
263
264 *buffer_size = desired_size;
265 p = buffer + sizeof(struct group) + sizeof(char *);
266 memcpy(buffer + sizeof(struct group), &p, sizeof(char *));
267 p = (char *)_ALIGN(p);
268
269 if (new_grp.gr_name != NULL) {
270 size = strlen(new_grp.gr_name);
271 memcpy(p, new_grp.gr_name, size);
272 new_grp.gr_name = p;
273 p += size + 1;
274 }
275
276 if (new_grp.gr_passwd != NULL) {
277 size = strlen(new_grp.gr_passwd);
278 memcpy(p, new_grp.gr_passwd, size);
279 new_grp.gr_passwd = p;
280 p += size + 1;
281 }
282
283 if (new_grp.gr_mem != NULL) {
284 p = (char *)_ALIGN(p);
285 memcpy(p, new_grp.gr_mem, sizeof(char *) * mem_size);
286 new_grp.gr_mem = (char **)p;
287 p += sizeof(char *) * (mem_size + 1);
288
289 for (mem = new_grp.gr_mem; *mem; ++mem) {
290 size = strlen(*mem);
291 memcpy(p, *mem, size);
292 *mem = p;
293 p += size + 1;
294 }
295 }
296
297 memcpy(buffer, &new_grp, sizeof(struct group));
298 return (NS_SUCCESS);
984263bc
MD
299}
300
ed5d5720
PA
301static int
302grp_unmarshal_func(char *buffer, size_t buffer_size, void *retval, va_list ap,
303 void *cache_mdata)
984263bc 304{
cf515c3a
JM
305 char *name __unused;
306 gid_t gid __unused;
ed5d5720
PA
307 struct group *grp;
308 char *orig_buf;
309 size_t orig_buf_size;
310 int *ret_errno;
984263bc 311
ed5d5720
PA
312 char *p;
313 char **mem;
314
315 switch ((enum nss_lookup_type)cache_mdata) {
316 case nss_lt_name:
317 name = va_arg(ap, char *);
318 break;
319 case nss_lt_id:
320 gid = va_arg(ap, gid_t);
321 break;
322 case nss_lt_all:
323 break;
324 default:
325 /* should be unreachable */
326 return (NS_UNAVAIL);
327 }
328
329 grp = va_arg(ap, struct group *);
330 orig_buf = va_arg(ap, char *);
331 orig_buf_size = va_arg(ap, size_t);
332 ret_errno = va_arg(ap, int *);
333
334 if (orig_buf_size <
335 buffer_size - sizeof(struct group) - sizeof(char *)) {
336 *ret_errno = ERANGE;
337 return (NS_RETURN);
338 }
339
340 memcpy(grp, buffer, sizeof(struct group));
341 memcpy(&p, buffer + sizeof(struct group), sizeof(char *));
342
343 orig_buf = (char *)_ALIGN(orig_buf);
344 memcpy(orig_buf, buffer + sizeof(struct group) + sizeof(char *) +
345 _ALIGN(p) - (size_t)p,
346 buffer_size - sizeof(struct group) - sizeof(char *) -
347 _ALIGN(p) + (size_t)p);
348 p = (char *)_ALIGN(p);
349
350 NS_APPLY_OFFSET(grp->gr_name, orig_buf, p, char *);
351 NS_APPLY_OFFSET(grp->gr_passwd, orig_buf, p, char *);
352 if (grp->gr_mem != NULL) {
353 NS_APPLY_OFFSET(grp->gr_mem, orig_buf, p, char **);
354
355 for (mem = grp->gr_mem; *mem; ++mem)
356 NS_APPLY_OFFSET(*mem, orig_buf, p, char *);
357 }
358
359 if (retval != NULL)
360 *((struct group **)retval) = grp;
361
362 return (NS_SUCCESS);
363}
364
365NSS_MP_CACHE_HANDLING(group);
366#endif /* NS_CACHING */
367
368#ifdef NS_CACHING
369static const nss_cache_info setgrent_cache_info = NS_MP_CACHE_INFO_INITIALIZER(
370 group, (void *)nss_lt_all,
371 NULL, NULL);
372#endif
373
374static const ns_dtab setgrent_dtab[] = {
375 { NSSRC_FILES, files_setgrent, (void *)SETGRENT },
376#ifdef HESIOD
377 { NSSRC_DNS, dns_setgrent, (void *)SETGRENT },
984263bc 378#endif
984263bc 379#ifdef YP
ed5d5720 380 { NSSRC_NIS, nis_setgrent, (void *)SETGRENT },
984263bc 381#endif
ed5d5720
PA
382 { NSSRC_COMPAT, compat_setgrent, (void *)SETGRENT },
383#ifdef NS_CACHING
384 NS_CACHE_CB(&setgrent_cache_info)
385#endif
386 { NULL, NULL, NULL }
387};
984263bc 388
ed5d5720
PA
389#ifdef NS_CACHING
390static const nss_cache_info endgrent_cache_info = NS_MP_CACHE_INFO_INITIALIZER(
391 group, (void *)nss_lt_all,
392 NULL, NULL);
393#endif
984263bc 394
ed5d5720
PA
395static const ns_dtab endgrent_dtab[] = {
396 { NSSRC_FILES, files_setgrent, (void *)ENDGRENT },
397#ifdef HESIOD
398 { NSSRC_DNS, dns_setgrent, (void *)ENDGRENT },
399#endif
984263bc 400#ifdef YP
ed5d5720
PA
401 { NSSRC_NIS, nis_setgrent, (void *)ENDGRENT },
402#endif
403 { NSSRC_COMPAT, compat_setgrent, (void *)ENDGRENT },
404#ifdef NS_CACHING
405 NS_CACHE_CB(&endgrent_cache_info)
406#endif
407 { NULL, NULL, NULL }
408};
409
410#ifdef NS_CACHING
411static const nss_cache_info getgrent_r_cache_info = NS_MP_CACHE_INFO_INITIALIZER(
412 group, (void *)nss_lt_all,
413 grp_marshal_func, grp_unmarshal_func);
414#endif
415
416static const ns_dtab getgrent_r_dtab[] = {
417 { NSSRC_FILES, files_group, (void *)nss_lt_all },
418#ifdef HESIOD
419 { NSSRC_DNS, dns_group, (void *)nss_lt_all },
984263bc 420#endif
984263bc 421#ifdef YP
ed5d5720 422 { NSSRC_NIS, nis_group, (void *)nss_lt_all },
984263bc 423#endif
ed5d5720
PA
424 { NSSRC_COMPAT, compat_group, (void *)nss_lt_all },
425#ifdef NS_CACHING
426 NS_CACHE_CB(&getgrent_r_cache_info)
427#endif
428 { NULL, NULL, NULL }
429};
984263bc
MD
430
431static int
ed5d5720 432gr_addgid(gid_t gid, gid_t *groups, int maxgrp, int *grpcnt)
984263bc 433{
ed5d5720
PA
434 int ret, dupc;
435
436 for (dupc = 0; dupc < MIN(maxgrp, *grpcnt); dupc++) {
437 if (groups[dupc] == gid)
438 return 1;
984263bc 439 }
ed5d5720
PA
440
441 ret = 1;
442 if (*grpcnt < maxgrp)
443 groups[*grpcnt] = gid;
444 else
445 ret = 0;
446
447 (*grpcnt)++;
448
449 return ret;
450}
451
452static int
6d7019e6 453getgroupmembership_fallback(void *retval __unused, void *mdata, va_list ap)
ed5d5720
PA
454{
455 const ns_src src[] = {
456 { mdata, NS_SUCCESS },
457 { NULL, 0}
458 };
459 struct group grp;
460 struct group *grp_p;
461 char *buf;
462 size_t bufsize;
463 const char *uname;
464 gid_t *groups;
465 gid_t agroup;
466 int maxgrp, *grpcnt;
467 int i, rv, ret_errno;
468
984263bc 469 /*
ed5d5720
PA
470 * As this is a fallback method, only provided src
471 * list will be respected during methods search.
984263bc 472 */
ed5d5720
PA
473 assert(src[0].name != NULL);
474
475 uname = va_arg(ap, const char *);
476 agroup = va_arg(ap, gid_t);
477 groups = va_arg(ap, gid_t *);
478 maxgrp = va_arg(ap, int);
479 grpcnt = va_arg(ap, int *);
480
481 rv = NS_UNAVAIL;
482
483 buf = malloc(GRP_STORAGE_INITIAL);
484 if (buf == NULL)
485 goto out;
486
487 bufsize = GRP_STORAGE_INITIAL;
488
489 gr_addgid(agroup, groups, maxgrp, grpcnt);
490
491 _nsdispatch(NULL, setgrent_dtab, NSDB_GROUP, "setgrent", src, 0);
492 for (;;) {
493 do {
494 ret_errno = 0;
495 grp_p = NULL;
496 rv = _nsdispatch(&grp_p, getgrent_r_dtab, NSDB_GROUP,
497 "getgrent_r", src, &grp, buf, bufsize, &ret_errno);
498
499 if (grp_p == NULL && ret_errno == ERANGE) {
500 free(buf);
501 if ((bufsize << 1) > GRP_STORAGE_MAX) {
502 buf = NULL;
503 errno = ERANGE;
504 goto out;
505 }
506
507 bufsize <<= 1;
508 buf = malloc(bufsize);
509 if (buf == NULL) {
510 goto out;
984263bc
MD
511 }
512 }
ed5d5720
PA
513 } while (grp_p == NULL && ret_errno == ERANGE);
514
515 if (ret_errno != 0) {
516 errno = ret_errno;
517 goto out;
984263bc 518 }
984263bc 519
ed5d5720
PA
520 if (grp_p == NULL)
521 break;
984263bc 522
ed5d5720
PA
523 for (i = 0; grp.gr_mem[i]; i++) {
524 if (strcmp(grp.gr_mem[i], uname) == 0)
525 gr_addgid(grp.gr_gid, groups, maxgrp, grpcnt);
526 }
984263bc
MD
527 }
528
ed5d5720
PA
529 _nsdispatch(NULL, endgrent_dtab, NSDB_GROUP, "endgrent", src);
530out:
531 free(buf);
532 return (rv);
984263bc
MD
533}
534
61ec5fb1 535void
984263bc
MD
536setgrent(void)
537{
ed5d5720 538 _nsdispatch(NULL, setgrent_dtab, NSDB_GROUP, "setgrent", defaultsrc, 0);
984263bc
MD
539}
540
ed5d5720 541
984263bc 542int
76f89ad8 543setgroupent(int stayopen)
984263bc 544{
ed5d5720
PA
545 _nsdispatch(NULL, setgrent_dtab, NSDB_GROUP, "setgrent", defaultsrc,
546 stayopen);
547 return (1);
984263bc
MD
548}
549
ed5d5720 550
984263bc 551void
76f89ad8 552endgrent(void)
984263bc 553{
ed5d5720 554 _nsdispatch(NULL, endgrent_dtab, NSDB_GROUP, "endgrent", defaultsrc);
984263bc
MD
555}
556
ed5d5720
PA
557
558int
559getgrent_r(struct group *grp, char *buffer, size_t bufsize,
560 struct group **result)
984263bc 561{
ed5d5720
PA
562 int rv, ret_errno;
563
564 ret_errno = 0;
565 *result = NULL;
566 rv = _nsdispatch(result, getgrent_r_dtab, NSDB_GROUP, "getgrent_r", defaultsrc,
567 grp, buffer, bufsize, &ret_errno);
568 if (rv == NS_SUCCESS)
569 return (0);
570 else
571 return (ret_errno);
572}
984263bc
MD
573
574
ed5d5720
PA
575int
576getgrnam_r(const char *name, struct group *grp, char *buffer, size_t bufsize,
577 struct group **result)
578{
579#ifdef NS_CACHING
580 static const nss_cache_info cache_info =
581 NS_COMMON_CACHE_INFO_INITIALIZER(
582 group, (void *)nss_lt_name,
583 grp_id_func, grp_marshal_func, grp_unmarshal_func);
584#endif
585
586 static const ns_dtab dtab[] = {
587 { NSSRC_FILES, files_group, (void *)nss_lt_name },
588#ifdef HESIOD
589 { NSSRC_DNS, dns_group, (void *)nss_lt_name },
984263bc 590#endif
984263bc 591#ifdef YP
ed5d5720 592 { NSSRC_NIS, nis_group, (void *)nss_lt_name },
984263bc 593#endif
ed5d5720
PA
594 { NSSRC_COMPAT, compat_group, (void *)nss_lt_name },
595#ifdef NS_CACHING
596 NS_CACHE_CB(&cache_info)
984263bc 597#endif
ed5d5720
PA
598 { NULL, NULL, NULL }
599 };
600 int rv, ret_errno;
984263bc 601
ed5d5720
PA
602 ret_errno = 0;
603 *result = NULL;
604 rv = _nsdispatch(result, dtab, NSDB_GROUP, "getgrnam_r", defaultsrc,
605 name, grp, buffer, bufsize, &ret_errno);
606 if (rv == NS_SUCCESS)
607 return (0);
608 else
609 return (ret_errno);
610}
984263bc 611
ed5d5720
PA
612
613int
614getgrgid_r(gid_t gid, struct group *grp, char *buffer, size_t bufsize,
615 struct group **result)
616{
617#ifdef NS_CACHING
618 static const nss_cache_info cache_info =
619 NS_COMMON_CACHE_INFO_INITIALIZER(
620 group, (void *)nss_lt_id,
621 grp_id_func, grp_marshal_func, grp_unmarshal_func);
984263bc 622#endif
ed5d5720
PA
623
624 static const ns_dtab dtab[] = {
625 { NSSRC_FILES, files_group, (void *)nss_lt_id },
626#ifdef HESIOD
627 { NSSRC_DNS, dns_group, (void *)nss_lt_id },
984263bc 628#endif
984263bc 629#ifdef YP
ed5d5720 630 { NSSRC_NIS, nis_group, (void *)nss_lt_id },
984263bc 631#endif
ed5d5720
PA
632 { NSSRC_COMPAT, compat_group, (void *)nss_lt_id },
633#ifdef NS_CACHING
634 NS_CACHE_CB(&cache_info)
984263bc 635#endif
ed5d5720
PA
636 { NULL, NULL, NULL }
637 };
638 int rv, ret_errno;
639
640 ret_errno = 0;
641 *result = NULL;
642 rv = _nsdispatch(result, dtab, NSDB_GROUP, "getgrgid_r", defaultsrc,
643 gid, grp, buffer, bufsize, &ret_errno);
644 if (rv == NS_SUCCESS)
645 return (0);
646 else
647 return (ret_errno);
648}
649
650
651
652int
653__getgroupmembership(const char *uname, gid_t agroup, gid_t *groups,
654 int maxgrp, int *grpcnt)
655{
656 static const ns_dtab dtab[] = {
657 NS_FALLBACK_CB(getgroupmembership_fallback)
658 { NULL, NULL, NULL }
659 };
cf515c3a 660 int rv __unused;
ed5d5720
PA
661
662 assert(uname != NULL);
663 /* groups may be NULL if just sizing when invoked with maxgrp = 0 */
664 assert(grpcnt != NULL);
665
666 *grpcnt = 0;
667 rv = _nsdispatch(NULL, dtab, NSDB_GROUP, "getgroupmembership",
668 defaultsrc, uname, agroup, groups, maxgrp, grpcnt);
669
670 /* too many groups found? */
671 return (*grpcnt > maxgrp ? -1 : 0);
672}
673
674
675static struct group grp;
676static char *grp_storage;
677static size_t grp_storage_size;
678
679static struct group *
680getgr(int (*fn)(union key, struct group *, char *, size_t, struct group **),
681 union key key)
682{
683 int rv;
684 struct group *res;
685
686 if (grp_storage == NULL) {
687 grp_storage = malloc(GRP_STORAGE_INITIAL);
688 if (grp_storage == NULL)
689 return (NULL);
690 grp_storage_size = GRP_STORAGE_INITIAL;
691 }
692 do {
693 rv = fn(key, &grp, grp_storage, grp_storage_size, &res);
694 if (res == NULL && rv == ERANGE) {
695 free(grp_storage);
696 if ((grp_storage_size << 1) > GRP_STORAGE_MAX) {
697 grp_storage = NULL;
698 errno = ERANGE;
699 return (NULL);
984263bc 700 }
ed5d5720
PA
701 grp_storage_size <<= 1;
702 grp_storage = malloc(grp_storage_size);
703 if (grp_storage == NULL)
704 return (NULL);
984263bc 705 }
ed5d5720
PA
706 } while (res == NULL && rv == ERANGE);
707 if (rv != 0)
708 errno = rv;
709 return (res);
984263bc
MD
710}
711
984263bc
MD
712
713static int
ed5d5720
PA
714wrap_getgrnam_r(union key key, struct group *grp, char *buffer, size_t bufsize,
715 struct group **res)
984263bc 716{
ed5d5720
PA
717 return (getgrnam_r(key.name, grp, buffer, bufsize, res));
718}
719
720
721static int
722wrap_getgrgid_r(union key key, struct group *grp, char *buffer, size_t bufsize,
723 struct group **res)
724{
725 return (getgrgid_r(key.gid, grp, buffer, bufsize, res));
726}
727
728
729static int
730wrap_getgrent_r(union key key __unused, struct group *grp, char *buffer,
731 size_t bufsize, struct group **res)
732{
733 return (getgrent_r(grp, buffer, bufsize, res));
734}
735
736
737struct group *
738getgrnam(const char *name)
739{
740 union key key;
741
742 key.name = name;
743 return (getgr(wrap_getgrnam_r, key));
744}
745
746
747struct group *
748getgrgid(gid_t gid)
749{
750 union key key;
751
752 key.gid = gid;
753 return (getgr(wrap_getgrgid_r, key));
754}
755
756
757struct group *
758getgrent(void)
759{
760 union key key;
761
762 key.gid = 0; /* not used */
763 return (getgr(wrap_getgrent_r, key));
764}
765
766
767static int
768is_comment_line(const char *s, size_t n)
769{
770 const char *eom;
771
772 eom = &s[n];
773
774 for (; s < eom; s++)
775 if (*s == '#' || !isspace((unsigned char)*s))
776 break;
777 return (*s == '#' || s == eom);
778}
984263bc 779
984263bc 780
ed5d5720
PA
781/*
782 * files backend
783 */
784static void
785files_endstate(void *p)
786{
984263bc 787
ed5d5720
PA
788 if (p == NULL)
789 return;
790 if (((struct files_state *)p)->fp != NULL)
791 fclose(((struct files_state *)p)->fp);
792 free(p);
793}
984263bc 794
984263bc 795
ed5d5720 796static int
6d7019e6 797files_setgrent(void *retval __unused, void *mdata, va_list ap)
ed5d5720
PA
798{
799 struct files_state *st;
800 int rv, stayopen;
801
802 rv = files_getstate(&st);
803 if (rv != 0)
804 return (NS_UNAVAIL);
805 switch ((enum constants)mdata) {
806 case SETGRENT:
807 stayopen = va_arg(ap, int);
808 if (st->fp != NULL)
809 rewind(st->fp);
810 else if (stayopen)
811 st->fp = fopen(_PATH_GROUP, "r");
812 break;
813 case ENDGRENT:
814 if (st->fp != NULL) {
815 fclose(st->fp);
816 st->fp = NULL;
984263bc 817 }
ed5d5720
PA
818 break;
819 default:
820 break;
821 }
822 return (NS_UNAVAIL);
823}
824
825
826static int
827files_group(void *retval, void *mdata, va_list ap)
828{
829 struct files_state *st;
830 enum nss_lookup_type how;
831 const char *name, *line;
832 struct group *grp;
833 gid_t gid;
834 char *buffer;
835 size_t bufsize, linesize;
836 off_t pos;
837 int rv, stayopen, *errnop;
838
839 name = NULL;
840 gid = (gid_t)-1;
841 how = (enum nss_lookup_type)mdata;
842 switch (how) {
843 case nss_lt_name:
844 name = va_arg(ap, const char *);
845 break;
846 case nss_lt_id:
847 gid = va_arg(ap, gid_t);
848 break;
849 case nss_lt_all:
850 break;
851 default:
852 return (NS_NOTFOUND);
853 }
854 grp = va_arg(ap, struct group *);
855 buffer = va_arg(ap, char *);
856 bufsize = va_arg(ap, size_t);
857 errnop = va_arg(ap, int *);
858 *errnop = files_getstate(&st);
859 if (*errnop != 0)
860 return (NS_UNAVAIL);
861 if (st->fp == NULL &&
862 ((st->fp = fopen(_PATH_GROUP, "r")) == NULL)) {
863 *errnop = errno;
864 return (NS_UNAVAIL);
865 }
866 if (how == nss_lt_all)
867 stayopen = 1;
868 else {
869 rewind(st->fp);
870 stayopen = st->stayopen;
871 }
872 rv = NS_NOTFOUND;
873 pos = ftello(st->fp);
874 while ((line = fgetln(st->fp, &linesize)) != NULL) {
875 if (line[linesize-1] == '\n')
876 linesize--;
877 rv = __gr_match_entry(line, linesize, how, name, gid);
878 if (rv != NS_SUCCESS)
879 continue;
880 /* We need room at least for the line, a string NUL
881 * terminator, alignment padding, and one (char *)
882 * pointer for the member list terminator.
883 */
884 if (bufsize <= linesize + _ALIGNBYTES + sizeof(char *)) {
885 *errnop = ERANGE;
886 rv = NS_RETURN;
984263bc 887 break;
984263bc 888 }
ed5d5720
PA
889 memcpy(buffer, line, linesize);
890 buffer[linesize] = '\0';
891 rv = __gr_parse_entry(buffer, linesize, grp,
892 &buffer[linesize + 1], bufsize - linesize - 1, errnop);
893 if (rv & NS_TERMINATE)
894 break;
895 pos = ftello(st->fp);
984263bc 896 }
ed5d5720
PA
897 if (!stayopen && st->fp != NULL) {
898 fclose(st->fp);
899 st->fp = NULL;
900 }
901 if (rv == NS_SUCCESS && retval != NULL)
902 *(struct group **)retval = grp;
903 else if (rv == NS_RETURN && *errnop == ERANGE && st->fp != NULL)
904 fseeko(st->fp, pos, SEEK_SET);
905 return (rv);
906}
907
984263bc 908
ed5d5720
PA
909#ifdef HESIOD
910/*
911 * dns backend
912 */
913static void
914dns_endstate(void *p)
915{
916
917 free(p);
918}
919
920
921static int
5dcdf778 922dns_setgrent(void *retval __unused, void *cb_data __unused, va_list ap __unused)
ed5d5720
PA
923{
924 struct dns_state *st;
925 int rv;
926
927 rv = dns_getstate(&st);
928 if (rv != 0)
929 return (NS_UNAVAIL);
930 st->counter = 0;
931 return (NS_UNAVAIL);
984263bc
MD
932}
933
984263bc
MD
934
935static int
ed5d5720 936dns_group(void *retval, void *mdata, va_list ap)
984263bc 937{
ed5d5720
PA
938 char buf[HESIOD_NAME_MAX];
939 struct dns_state *st;
940 struct group *grp;
941 const char *name, *label;
942 void *ctx;
943 char *buffer, **hes;
944 size_t bufsize, adjsize, linesize;
945 gid_t gid;
946 enum nss_lookup_type how;
947 int rv, *errnop;
984263bc 948
ed5d5720
PA
949 ctx = NULL;
950 hes = NULL;
951 name = NULL;
952 gid = (gid_t)-1;
953 how = (enum nss_lookup_type)mdata;
954 switch (how) {
955 case nss_lt_name:
956 name = va_arg(ap, const char *);
957 break;
958 case nss_lt_id:
959 gid = va_arg(ap, gid_t);
960 break;
961 case nss_lt_all:
962 break;
963 }
964 grp = va_arg(ap, struct group *);
965 buffer = va_arg(ap, char *);
966 bufsize = va_arg(ap, size_t);
967 errnop = va_arg(ap, int *);
968 *errnop = dns_getstate(&st);
969 if (*errnop != 0)
970 return (NS_UNAVAIL);
971 if (hesiod_init(&ctx) != 0) {
972 *errnop = errno;
973 rv = NS_UNAVAIL;
974 goto fin;
984263bc 975 }
ed5d5720
PA
976 do {
977 rv = NS_NOTFOUND;
978 switch (how) {
979 case nss_lt_name:
980 label = name;
981 break;
982 case nss_lt_id:
983 if (snprintf(buf, sizeof(buf), "%lu",
984 (unsigned long)gid) >= sizeof(buf))
985 goto fin;
986 label = buf;
987 break;
988 case nss_lt_all:
989 if (st->counter < 0)
990 goto fin;
991 if (snprintf(buf, sizeof(buf), "group-%ld",
992 st->counter++) >= sizeof(buf))
993 goto fin;
994 label = buf;
995 break;
996 }
997 hes = hesiod_resolve(ctx, label,
998 how == nss_lt_id ? "gid" : "group");
999 if ((how == nss_lt_id && hes == NULL &&
1000 (hes = hesiod_resolve(ctx, buf, "group")) == NULL) ||
1001 hes == NULL) {
1002 if (how == nss_lt_all)
1003 st->counter = -1;
1004 if (errno != ENOENT)
1005 *errnop = errno;
1006 goto fin;
1007 }
1008 rv = __gr_match_entry(hes[0], strlen(hes[0]), how, name, gid);
1009 if (rv != NS_SUCCESS) {
1010 hesiod_free_list(ctx, hes);
1011 hes = NULL;
1012 continue;
1013 }
1014 /* We need room at least for the line, a string NUL
1015 * terminator, alignment padding, and one (char *)
1016 * pointer for the member list terminator.
1017 */
1018 adjsize = bufsize - _ALIGNBYTES - sizeof(char *);
1019 linesize = strlcpy(buffer, hes[0], adjsize);
1020 if (linesize >= adjsize) {
1021 *errnop = ERANGE;
1022 rv = NS_RETURN;
1023 goto fin;
1024 }
1025 hesiod_free_list(ctx, hes);
1026 hes = NULL;
1027 rv = __gr_parse_entry(buffer, linesize, grp,
1028 &buffer[linesize + 1], bufsize - linesize - 1, errnop);
1029 } while (how == nss_lt_all && !(rv & NS_TERMINATE));
1030fin:
1031 if (hes != NULL)
1032 hesiod_free_list(ctx, hes);
1033 if (ctx != NULL)
1034 hesiod_end(ctx);
1035 if (rv == NS_SUCCESS && retval != NULL)
1036 *(struct group **)retval = grp;
1037 return (rv);
1038}
1039#endif /* HESIOD */
984263bc 1040
984263bc 1041
ed5d5720
PA
1042#ifdef YP
1043/*
1044 * nis backend
1045 */
1046static void
1047nis_endstate(void *p)
1048{
1049
1050 if (p == NULL)
1051 return;
1052 free(((struct nis_state *)p)->key);
1053 free(p);
1054}
984263bc 1055
984263bc 1056
ed5d5720 1057static int
6d7019e6 1058nis_setgrent(void *retval __unused, void *cb_data __unused, va_list ap __unused)
ed5d5720
PA
1059{
1060 struct nis_state *st;
1061 int rv;
1062
1063 rv = nis_getstate(&st);
1064 if (rv != 0)
1065 return (NS_UNAVAIL);
1066 st->done = 0;
1067 free(st->key);
1068 st->key = NULL;
1069 return (NS_UNAVAIL);
984263bc
MD
1070}
1071
1072
1073static int
ed5d5720 1074nis_group(void *retval, void *mdata, va_list ap)
984263bc 1075{
ed5d5720
PA
1076 char *map;
1077 struct nis_state *st;
1078 struct group *grp;
1079 const char *name;
1080 char *buffer, *key, *result;
1081 size_t bufsize;
1082 gid_t gid;
1083 enum nss_lookup_type how;
1084 int *errnop, keylen, resultlen, rv;
984263bc 1085
ed5d5720
PA
1086 name = NULL;
1087 gid = (gid_t)-1;
1088 how = (enum nss_lookup_type)mdata;
1089 switch (how) {
1090 case nss_lt_name:
1091 name = va_arg(ap, const char *);
1092 map = "group.byname";
1093 break;
1094 case nss_lt_id:
1095 gid = va_arg(ap, gid_t);
1096 map = "group.bygid";
1097 break;
1098 case nss_lt_all:
1099 map = "group.byname";
1100 break;
984263bc 1101 }
ed5d5720
PA
1102 grp = va_arg(ap, struct group *);
1103 buffer = va_arg(ap, char *);
1104 bufsize = va_arg(ap, size_t);
1105 errnop = va_arg(ap, int *);
1106 *errnop = nis_getstate(&st);
1107 if (*errnop != 0)
1108 return (NS_UNAVAIL);
1109 if (st->domain[0] == '\0') {
1110 if (getdomainname(st->domain, sizeof(st->domain)) != 0) {
1111 *errnop = errno;
1112 return (NS_UNAVAIL);
984263bc 1113 }
ed5d5720
PA
1114 }
1115 result = NULL;
1116 do {
1117 rv = NS_NOTFOUND;
1118 switch (how) {
1119 case nss_lt_name:
1120 if (strlcpy(buffer, name, bufsize) >= bufsize)
1121 goto erange;
1122 break;
1123 case nss_lt_id:
1124 if (snprintf(buffer, bufsize, "%lu",
1125 (unsigned long)gid) >= bufsize)
1126 goto erange;
1127 break;
1128 case nss_lt_all:
1129 if (st->done)
1130 goto fin;
1131 break;
984263bc 1132 }
ed5d5720
PA
1133 result = NULL;
1134 if (how == nss_lt_all) {
1135 if (st->key == NULL)
1136 rv = yp_first(st->domain, map, &st->key,
1137 &st->keylen, &result, &resultlen);
1138 else {
1139 key = st->key;
1140 keylen = st->keylen;
1141 st->key = NULL;
1142 rv = yp_next(st->domain, map, key, keylen,
1143 &st->key, &st->keylen, &result,
1144 &resultlen);
1145 free(key);
1146 }
1147 if (rv != 0) {
1148 free(result);
1149 free(st->key);
1150 st->key = NULL;
1151 if (rv == YPERR_NOMORE) {
1152 st->done = 1;
1153 rv = NS_NOTFOUND;
1154 } else
1155 rv = NS_UNAVAIL;
1156 goto fin;
1157 }
1158 } else {
1159 rv = yp_match(st->domain, map, buffer, strlen(buffer),
1160 &result, &resultlen);
1161 if (rv == YPERR_KEY) {
1162 rv = NS_NOTFOUND;
1163 continue;
1164 } else if (rv != 0) {
1165 free(result);
1166 rv = NS_UNAVAIL;
1167 continue;
1168 }
1169 }
1170 /* We need room at least for the line, a string NUL
1171 * terminator, alignment padding, and one (char *)
1172 * pointer for the member list terminator.
1173 */
1174 if (resultlen >= bufsize - _ALIGNBYTES - sizeof(char *))
1175 goto erange;
1176 memcpy(buffer, result, resultlen);
1177 buffer[resultlen] = '\0';
1178 free(result);
1179 rv = __gr_match_entry(buffer, resultlen, how, name, gid);
1180 if (rv == NS_SUCCESS)
1181 rv = __gr_parse_entry(buffer, resultlen, grp,
1182 &buffer[resultlen+1], bufsize - resultlen - 1,
1183 errnop);
1184 } while (how == nss_lt_all && !(rv & NS_TERMINATE));
1185fin:
1186 if (rv == NS_SUCCESS && retval != NULL)
1187 *(struct group **)retval = grp;
1188 return (rv);
1189erange:
1190 *errnop = ERANGE;
1191 return (NS_RETURN);
1192}
1193#endif /* YP */
984263bc 1194
ed5d5720
PA
1195
1196
1197/*
1198 * compat backend
1199 */
1200static void
1201compat_endstate(void *p)
1202{
1203 struct compat_state *st;
1204
1205 if (p == NULL)
1206 return;
1207 st = (struct compat_state *)p;
1208 free(st->name);
1209 if (st->fp != NULL)
1210 fclose(st->fp);
1211 free(p);
1212}
1213
1214
1215static int
6d7019e6 1216compat_setgrent(void *retval __unused, void *mdata, va_list ap)
ed5d5720
PA
1217{
1218 static const ns_src compatsrc[] = {
1219#ifdef YP
1220 { NSSRC_NIS, NS_SUCCESS },
1221#endif
1222 { NULL, 0 }
1223 };
1224 ns_dtab dtab[] = {
1225#ifdef HESIOD
1226 { NSSRC_DNS, dns_setgrent, NULL },
1227#endif
1228#ifdef YP
1229 { NSSRC_NIS, nis_setgrent, NULL },
1230#endif
1231 { NULL, NULL, NULL }
1232 };
1233 struct compat_state *st;
1234 int rv, stayopen;
1235
1236#define set_setent(x, y) do { \
1237 int i; \
1238 \
1239 for (i = 0; i < (sizeof(x)/sizeof(x[0])) - 1; i++) \
1240 x[i].mdata = (void *)y; \
1241} while (0)
1242
1243 rv = compat_getstate(&st);
1244 if (rv != 0)
1245 return (NS_UNAVAIL);
1246 switch ((enum constants)mdata) {
1247 case SETGRENT:
1248 stayopen = va_arg(ap, int);
1249 if (st->fp != NULL)
1250 rewind(st->fp);
1251 else if (stayopen)
1252 st->fp = fopen(_PATH_GROUP, "r");
1253 set_setent(dtab, mdata);
1254 _nsdispatch(NULL, dtab, NSDB_GROUP_COMPAT, "setgrent",
1255 compatsrc, 0);
1256 break;
1257 case ENDGRENT:
1258 if (st->fp != NULL) {
1259 fclose(st->fp);
1260 st->fp = NULL;
984263bc 1261 }
ed5d5720
PA
1262 set_setent(dtab, mdata);
1263 _nsdispatch(NULL, dtab, NSDB_GROUP_COMPAT, "endgrent",
1264 compatsrc, 0);
1265 break;
1266 default:
1267 break;
1268 }
1269 st->compat = COMPAT_MODE_OFF;
1270 free(st->name);
1271 st->name = NULL;
1272 return (NS_UNAVAIL);
1273#undef set_setent
1274}
984263bc 1275
ed5d5720
PA
1276
1277static int
1278compat_group(void *retval, void *mdata, va_list ap)
1279{
1280 static const ns_src compatsrc[] = {
1281#ifdef YP
1282 { NSSRC_NIS, NS_SUCCESS },
1283#endif
1284 { NULL, 0 }
1285 };
1286 ns_dtab dtab[] = {
1287#ifdef YP
1288 { NSSRC_NIS, nis_group, NULL },
1289#endif
1290#ifdef HESIOD
1291 { NSSRC_DNS, dns_group, NULL },
1292#endif
1293 { NULL, NULL, NULL }
1294 };
1295 struct compat_state *st;
1296 enum nss_lookup_type how;
1297 const char *name, *line;
1298 struct group *grp;
1299 gid_t gid;
1300 char *buffer, *p;
1301 void *discard;
1302 size_t bufsize, linesize;
1303 off_t pos;
1304 int rv, stayopen, *errnop;
1305
1306#define set_lookup_type(x, y) do { \
1307 int i; \
1308 \
1309 for (i = 0; i < (sizeof(x)/sizeof(x[0])) - 1; i++) \
1310 x[i].mdata = (void *)y; \
1311} while (0)
1312
1313 name = NULL;
1314 gid = (gid_t)-1;
1315 how = (enum nss_lookup_type)mdata;
1316 switch (how) {
1317 case nss_lt_name:
1318 name = va_arg(ap, const char *);
1319 break;
1320 case nss_lt_id:
1321 gid = va_arg(ap, gid_t);
1322 break;
1323 case nss_lt_all:
1324 break;
1325 default:
1326 return (NS_NOTFOUND);
1327 }
1328 grp = va_arg(ap, struct group *);
1329 buffer = va_arg(ap, char *);
1330 bufsize = va_arg(ap, size_t);
1331 errnop = va_arg(ap, int *);
1332 *errnop = compat_getstate(&st);
1333 if (*errnop != 0)
1334 return (NS_UNAVAIL);
1335 if (st->fp == NULL &&
1336 ((st->fp = fopen(_PATH_GROUP, "r")) == NULL)) {
1337 *errnop = errno;
1338 rv = NS_UNAVAIL;
1339 goto fin;
1340 }
1341 if (how == nss_lt_all)
1342 stayopen = 1;
1343 else {
1344 rewind(st->fp);
1345 stayopen = st->stayopen;
1346 }
1347docompat:
1348 switch (st->compat) {
1349 case COMPAT_MODE_ALL:
1350 set_lookup_type(dtab, how);
1351 switch (how) {
1352 case nss_lt_all:
1353 rv = _nsdispatch(&discard, dtab, NSDB_GROUP_COMPAT,
1354 "getgrent_r", compatsrc, grp, buffer, bufsize,
1355 errnop);
1356 break;
1357 case nss_lt_id:
1358 rv = _nsdispatch(&discard, dtab, NSDB_GROUP_COMPAT,
1359 "getgrgid_r", compatsrc, gid, grp, buffer, bufsize,
1360 errnop);
1361 break;
1362 case nss_lt_name:
1363 rv = _nsdispatch(&discard, dtab, NSDB_GROUP_COMPAT,
1364 "getgrnam_r", compatsrc, name, grp, buffer,
1365 bufsize, errnop);
1366 break;
1367 }
1368 if (rv & NS_TERMINATE)
1369 goto fin;
1370 st->compat = COMPAT_MODE_OFF;
1371 break;
1372 case COMPAT_MODE_NAME:
1373 set_lookup_type(dtab, nss_lt_name);
1374 rv = _nsdispatch(&discard, dtab, NSDB_GROUP_COMPAT,
1375 "getgrnam_r", compatsrc, st->name, grp, buffer, bufsize,
1376 errnop);
1377 switch (rv) {
1378 case NS_SUCCESS:
1379 switch (how) {
1380 case nss_lt_name:
1381 if (strcmp(name, grp->gr_name) != 0)
1382 rv = NS_NOTFOUND;
1383 break;
1384 case nss_lt_id:
1385 if (gid != grp->gr_gid)
1386 rv = NS_NOTFOUND;
1387 break;
1388 default:
1389 break;
1390 }
1391 break;
1392 case NS_RETURN:
1393 goto fin;
1394 default:
1395 break;
1396 }
1397 free(st->name);
1398 st->name = NULL;
1399 st->compat = COMPAT_MODE_OFF;
1400 if (rv == NS_SUCCESS)
1401 goto fin;
1402 break;
1403 default:
1404 break;
1405 }
1406 rv = NS_NOTFOUND;
1407 pos = ftello(st->fp);
1408 while ((line = fgetln(st->fp, &linesize)) != NULL) {
1409 if (line[linesize-1] == '\n')
1410 linesize--;
1411 if (linesize > 2 && line[0] == '+') {
1412 p = memchr(&line[1], ':', linesize);
1413 if (p == NULL || p == &line[1])
1414 st->compat = COMPAT_MODE_ALL;
1415 else {
1416 st->name = malloc(p - line);
1417 if (st->name == NULL) {
1418 syslog(LOG_ERR,
1419 "getgrent memory allocation failure");
1420 *errnop = ENOMEM;
1421 rv = NS_UNAVAIL;
1422 break;
1423 }
1424 memcpy(st->name, &line[1], p - line - 1);
1425 st->name[p - line - 1] = '\0';
1426 st->compat = COMPAT_MODE_NAME;
1427 }
1428 goto docompat;
1429 }
1430 rv = __gr_match_entry(line, linesize, how, name, gid);
1431 if (rv != NS_SUCCESS)
1432 continue;
1433 /* We need room at least for the line, a string NUL
1434 * terminator, alignment padding, and one (char *)
1435 * pointer for the member list terminator.
1436 */
1437 if (bufsize <= linesize + _ALIGNBYTES + sizeof(char *)) {
1438 *errnop = ERANGE;
1439 rv = NS_RETURN;
1440 break;
1441 }
1442 memcpy(buffer, line, linesize);
1443 buffer[linesize] = '\0';
1444 rv = __gr_parse_entry(buffer, linesize, grp,
1445 &buffer[linesize + 1], bufsize - linesize - 1, errnop);
1446 if (rv & NS_TERMINATE)
1447 break;
1448 pos = ftello(st->fp);
1449 }
1450fin:
1451 if (!stayopen && st->fp != NULL) {
1452 fclose(st->fp);
1453 st->fp = NULL;
984263bc 1454 }
ed5d5720
PA
1455 if (rv == NS_SUCCESS && retval != NULL)
1456 *(struct group **)retval = grp;
1457 else if (rv == NS_RETURN && *errnop == ERANGE && st->fp != NULL)
1458 fseeko(st->fp, pos, SEEK_SET);
1459 return (rv);
1460#undef set_lookup_type
984263bc
MD
1461}
1462
ed5d5720
PA
1463
1464/*
1465 * common group line matching and parsing
1466 */
1467int
1468__gr_match_entry(const char *line, size_t linesize, enum nss_lookup_type how,
1469 const char *name, gid_t gid)
1470{
1471 size_t namesize;
1472 const char *p, *eol;
1473 char *q;
1474 unsigned long n;
1475 int i, needed;
1476
1477 if (linesize == 0 || is_comment_line(line, linesize))
1478 return (NS_NOTFOUND);
1479 switch (how) {
1480 case nss_lt_name: needed = 1; break;
1481 case nss_lt_id: needed = 2; break;
1482 default: needed = 2; break;
1483 }
1484 eol = &line[linesize];
1485 for (p = line, i = 0; i < needed && p < eol; p++)
1486 if (*p == ':')
1487 i++;
1488 if (i < needed)
1489 return (NS_NOTFOUND);
1490 switch (how) {
1491 case nss_lt_name:
1492 namesize = strlen(name);
1493 if (namesize + 1 == (size_t)(p - line) &&
1494 memcmp(line, name, namesize) == 0)
1495 return (NS_SUCCESS);
1496 break;
1497 case nss_lt_id:
1498 n = strtoul(p, &q, 10);
1499 if (q < eol && *q == ':' && gid == (gid_t)n)
1500 return (NS_SUCCESS);
1501 break;
1502 case nss_lt_all:
1503 return (NS_SUCCESS);
1504 default:
1505 break;
1506 }
1507 return (NS_NOTFOUND);
1508}
1509
1510
1511int
6d7019e6
SW
1512__gr_parse_entry(char *line, size_t linesize __unused, struct group *grp,
1513 char *membuf, size_t membufsize, int *errnop)
ed5d5720
PA
1514{
1515 char *s_gid, *s_mem, *p, **members;
1516 unsigned long n;
1517 int maxmembers;
1518
1519 memset(grp, 0, sizeof(*grp));
1520 members = (char **)_ALIGN(membuf);
1521 membufsize -= (char *)members - membuf;
1522 maxmembers = membufsize / sizeof(*members);
1523 if (maxmembers <= 0 ||
1524 (grp->gr_name = strsep(&line, ":")) == NULL ||
1525 grp->gr_name[0] == '\0' ||
1526 (grp->gr_passwd = strsep(&line, ":")) == NULL ||
1527 (s_gid = strsep(&line, ":")) == NULL ||
1528 s_gid[0] == '\0')
1529 return (NS_NOTFOUND);
1530 s_mem = line;
1531 n = strtoul(s_gid, &s_gid, 10);
1532 if (s_gid[0] != '\0')
1533 return (NS_NOTFOUND);
1534 grp->gr_gid = (gid_t)n;
1535 grp->gr_mem = members;
1536 while (maxmembers > 1 && s_mem != NULL) {
1537 p = strsep(&s_mem, ",");
1538 if (p != NULL && *p != '\0') {
1539 *members++ = p;
1540 maxmembers--;
1541 }
1542 }
1543 *members = NULL;
1544 if (s_mem == NULL)
1545 return (NS_SUCCESS);
1546 else {
1547 *errnop = ERANGE;
1548 return (NS_RETURN);
1549 }
1550}