1 /* $NetBSD: getgrent.c,v 1.34.2.1 1999/04/27 14:10:58 perry Exp $ */
3 * Copyright (c) 1989, 1993
4 * The Regents of the University of California. All rights reserved.
5 * Portions Copyright (c) 1994, Jason Downs. All Rights Reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#)getgrent.c 8.2 (Berkeley) 3/21/94
36 * $FreeBSD: src/lib/libc/gen/getgrent.c,v 1.17.6.1 2001/03/05 08:56:02 obrien Exp $
37 * $DragonFly: src/lib/libc/gen/getgrent.c,v 1.3 2004/06/06 15:05:55 hmp Exp $
42 #include <sys/types.h>
50 static struct group _gr_group;
51 static int _gr_stayopen;
52 static int grscan(), start_gr();
55 #include <rpcsvc/yp_prot.h>
56 #include <rpcsvc/ypclnt.h>
57 static int _gr_stepping_yp;
58 static int _gr_yp_enabled;
59 static int _getypgroup(struct group *, const char *, char *);
60 static int _nextypgroup(struct group *);
63 /* initial size for malloc and increase steps for realloc */
65 #define MAXLINELENGTH 256
67 static char **members; /* list of group members */
68 static int maxgrp; /* current length of **mebers */
69 static char *line; /* temp buffer for group line */
70 static int maxlinelength; /* current length of *line */
73 * Lines longer than MAXLINELENGTHLIMIT will be counted as an error.
74 * <= 0 disable check for maximum line length
75 * 256K is enough for 64,000 uids
77 #define MAXLINELENGTHLIMIT (256 * 1024)
78 #define GROUP_IGNORE_COMMENTS 1 /* allow comments in /etc/group */
83 if (!_gr_fp && !start_gr()) {
88 if (_gr_stepping_yp) {
89 if (_nextypgroup(&_gr_group))
95 if (!grscan(0, 0, NULL))
98 if(_gr_group.gr_name[0] == '+' && _gr_group.gr_name[1]) {
99 _getypgroup(&_gr_group, &_gr_group.gr_name[1],
101 } else if(_gr_group.gr_name[0] == '+') {
102 if (!_nextypgroup(&_gr_group))
122 rval = grscan(1, 0, name);
124 if(rval == -1 && (_gr_yp_enabled < 0 || (_gr_yp_enabled &&
125 _gr_group.gr_name[0] == '+'))) {
126 if (!(rval = _getypgroup(&_gr_group, name, "group.byname")))
132 return (rval) ? &_gr_group : NULL;
146 rval = grscan(1, gid, NULL);
148 if(rval == -1 && _gr_yp_enabled) {
150 snprintf(buf, sizeof buf, "%d", (unsigned)gid);
151 if (!(rval = _getypgroup(&_gr_group, buf, "group.bygid")))
157 return (rval) ? &_gr_group : NULL;
167 _gr_fp = fopen(_PATH_GROUP, "r");
168 if(!_gr_fp) return 0;
171 * This is a disgusting hack, used to determine when YP is enabled.
172 * This would be easier if we had a group database to go along with
173 * the password database.
179 while((line = fgetln(_gr_fp, &linelen)) != NULL) {
181 if(line[1] && line[1] != ':' && !_gr_yp_enabled) {
193 if (maxlinelength == 0) {
194 if ((line = (char *)malloc(MAXLINELENGTH)) == NULL)
196 maxlinelength += MAXLINELENGTH;
200 if ((members = (char **) malloc(sizeof(char **) *
212 return setgroupent(0);
216 setgroupent(stayopen)
221 _gr_stayopen = stayopen;
235 (void)fclose(_gr_fp);
241 grscan(search, gid, name)
256 if (fgets(line, maxlinelength, _gr_fp) == NULL)
259 if (!index(line, '\n')) {
264 /* don't allocate infinite memory */
265 if (MAXLINELENGTHLIMIT > 0 &&
266 maxlinelength >= MAXLINELENGTHLIMIT)
269 if ((line = (char *)reallocf(line,
271 (maxlinelength + MAXLINELENGTH))) == NULL)
274 if (fgets(line + maxlinelength - 1,
275 MAXLINELENGTH + 1, _gr_fp) == NULL)
278 maxlinelength += MAXLINELENGTH;
279 } while (!index(line + maxlinelength -
280 MAXLINELENGTH - 1, '\n'));
283 #ifdef GROUP_IGNORE_COMMENTS
285 * Ignore comments: ^[ \t]*#
287 for (cp = line; *cp != '\0'; cp++)
288 if (*cp != ' ' && *cp != '\t')
290 if (*cp == '#' || *cp == '\0')
296 if ((_gr_group.gr_name = strsep(&bp, ":\n")) == NULL)
300 * XXX We need to be careful to avoid proceeding
301 * past this point under certain circumstances or
302 * we risk dereferencing null pointers down below.
304 if (_gr_group.gr_name[0] == '+') {
305 if (strlen(_gr_group.gr_name) == 1) {
315 cp = &_gr_group.gr_name[1];
316 if (search && name != NULL)
317 if (strcmp(cp, name))
319 if (!_getypgroup(&_gr_group, cp,
322 if (search && name == NULL)
323 if (gid != _gr_group.gr_gid)
325 /* We're going to override -- tell the world. */
330 if (_gr_group.gr_name[0] == '+')
333 if (search && name) {
334 if(strcmp(_gr_group.gr_name, name)) {
339 if ((cp = strsep(&bp, ":\n")) == NULL)
344 if (strlen(cp) || !_ypfound)
345 _gr_group.gr_passwd = cp;
347 if ((_gr_group.gr_passwd = strsep(&bp, ":\n")) == NULL)
350 if (!(cp = strsep(&bp, ":\n")))
359 * Hurm. Should we be doing this? We allow UIDs to
360 * be overridden -- what about GIDs?
364 _gr_group.gr_gid = atoi(cp);
365 if (search && name == NULL && _gr_group.gr_gid != gid)
368 if (bp == NULL) /* !!! Must check for this! */
371 if ((cp = strsep(&bp, ":\n")) == NULL)
374 if (!strlen(cp) && _ypfound)
381 for (m = members; ; bp++) {
382 if (m == (members + maxgrp - 1)) {
383 if ((members = (char **)
386 (maxgrp + MAXGRP))) == NULL)
388 m = members + maxgrp - 1;
397 } else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
403 } else if (cp == NULL)
407 _gr_group.gr_mem = members;
418 _gr_breakout_yp(struct group *gr, char *result)
424 * XXX If 's' ends up being a NULL pointer, punt on this group.
425 * It means the NIS group entry is badly formatted and should
428 if ((s = strsep(&result, ":")) == NULL) return 0; /* name */
431 if ((s = strsep(&result, ":")) == NULL) return 0; /* password */
434 if ((s = strsep(&result, ":")) == NULL) return 0; /* gid */
435 gr->gr_gid = atoi(s);
437 if ((s = result) == NULL) return 0;
440 for (m = members; ; s++) {
441 if (m == members + maxgrp - 1) {
442 if ((members = (char **)reallocf(members,
443 sizeof(char **) * (maxgrp + MAXGRP))) == NULL)
445 m = members + maxgrp - 1;
454 } else if (*s == '\0' || *s == '\n' || *s == ' ') {
460 } else if (cp == NULL) {
464 _gr_group.gr_mem = members;
470 static char *_gr_yp_domain;
473 _getypgroup(struct group *gr, const char *name, char *map)
476 static char resultbuf[YPMAXRECORD + 2];
480 if(yp_get_default_domain(&_gr_yp_domain))
484 if(yp_match(_gr_yp_domain, map, name, strlen(name),
485 &result, &resultlen))
488 s = strchr(result, '\n');
491 if(resultlen >= sizeof resultbuf) return 0;
492 strncpy(resultbuf, result, resultlen);
493 resultbuf[resultlen] = '\0';
495 return(_gr_breakout_yp(gr, resultbuf));
501 _nextypgroup(struct group *gr)
505 char *lastkey, *result;
506 static char resultbuf[YPMAXRECORD + 2];
511 if(yp_get_default_domain(&_gr_yp_domain))
515 if(!_gr_stepping_yp) {
517 rv = yp_first(_gr_yp_domain, "group.byname",
518 &key, &keylen, &result, &resultlen);
527 rv = yp_next(_gr_yp_domain, "group.byname", key, keylen,
528 &key, &keylen, &result, &resultlen);
536 if(resultlen > sizeof(resultbuf)) {
541 strncpy(resultbuf, result, resultlen);
542 resultbuf[resultlen] = '\0';
544 if((result = strchr(resultbuf, '\n')) != NULL)
546 if (_gr_breakout_yp(gr, resultbuf))