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