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
36 #if defined(LIBC_SCCS) && !defined(lint)
37 static char sccsid[] = "@(#)getgrent.c 8.2 (Berkeley) 3/21/94";
39 "$FreeBSD: src/lib/libc/gen/getgrent.c,v 1.17.6.1 2001/03/05 08:56:02 obrien Exp $";
40 #endif /* LIBC_SCCS and not lint */
44 #include <sys/types.h>
52 static struct group _gr_group;
53 static int _gr_stayopen;
54 static int grscan(), start_gr();
57 #include <rpcsvc/yp_prot.h>
58 #include <rpcsvc/ypclnt.h>
59 static int _gr_stepping_yp;
60 static int _gr_yp_enabled;
61 static int _getypgroup(struct group *, const char *, char *);
62 static int _nextypgroup(struct group *);
65 /* initial size for malloc and increase steps for realloc */
67 #define MAXLINELENGTH 256
69 static char **members; /* list of group members */
70 static int maxgrp; /* current length of **mebers */
71 static char *line; /* temp buffer for group line */
72 static int maxlinelength; /* current length of *line */
75 * Lines longer than MAXLINELENGTHLIMIT will be counted as an error.
76 * <= 0 disable check for maximum line length
77 * 256K is enough for 64,000 uids
79 #define MAXLINELENGTHLIMIT (256 * 1024)
80 #define GROUP_IGNORE_COMMENTS 1 /* allow comments in /etc/group */
85 if (!_gr_fp && !start_gr()) {
90 if (_gr_stepping_yp) {
91 if (_nextypgroup(&_gr_group))
97 if (!grscan(0, 0, NULL))
100 if(_gr_group.gr_name[0] == '+' && _gr_group.gr_name[1]) {
101 _getypgroup(&_gr_group, &_gr_group.gr_name[1],
103 } else if(_gr_group.gr_name[0] == '+') {
104 if (!_nextypgroup(&_gr_group))
124 rval = grscan(1, 0, name);
126 if(rval == -1 && (_gr_yp_enabled < 0 || (_gr_yp_enabled &&
127 _gr_group.gr_name[0] == '+'))) {
128 if (!(rval = _getypgroup(&_gr_group, name, "group.byname")))
134 return (rval) ? &_gr_group : NULL;
148 rval = grscan(1, gid, NULL);
150 if(rval == -1 && _gr_yp_enabled) {
152 snprintf(buf, sizeof buf, "%d", (unsigned)gid);
153 if (!(rval = _getypgroup(&_gr_group, buf, "group.bygid")))
159 return (rval) ? &_gr_group : NULL;
169 _gr_fp = fopen(_PATH_GROUP, "r");
170 if(!_gr_fp) return 0;
173 * This is a disgusting hack, used to determine when YP is enabled.
174 * This would be easier if we had a group database to go along with
175 * the password database.
181 while((line = fgetln(_gr_fp, &linelen)) != NULL) {
183 if(line[1] && line[1] != ':' && !_gr_yp_enabled) {
195 if (maxlinelength == 0) {
196 if ((line = (char *)malloc(MAXLINELENGTH)) == NULL)
198 maxlinelength += MAXLINELENGTH;
202 if ((members = (char **) malloc(sizeof(char **) *
214 return setgroupent(0);
218 setgroupent(stayopen)
223 _gr_stayopen = stayopen;
237 (void)fclose(_gr_fp);
243 grscan(search, gid, name)
244 register int search, gid;
247 register char *cp, **m;
258 if (fgets(line, maxlinelength, _gr_fp) == NULL)
261 if (!index(line, '\n')) {
266 /* don't allocate infinite memory */
267 if (MAXLINELENGTHLIMIT > 0 &&
268 maxlinelength >= MAXLINELENGTHLIMIT)
271 if ((line = (char *)reallocf(line,
273 (maxlinelength + MAXLINELENGTH))) == NULL)
276 if (fgets(line + maxlinelength - 1,
277 MAXLINELENGTH + 1, _gr_fp) == NULL)
280 maxlinelength += MAXLINELENGTH;
281 } while (!index(line + maxlinelength -
282 MAXLINELENGTH - 1, '\n'));
285 #ifdef GROUP_IGNORE_COMMENTS
287 * Ignore comments: ^[ \t]*#
289 for (cp = line; *cp != '\0'; cp++)
290 if (*cp != ' ' && *cp != '\t')
292 if (*cp == '#' || *cp == '\0')
298 if ((_gr_group.gr_name = strsep(&bp, ":\n")) == NULL)
302 * XXX We need to be careful to avoid proceeding
303 * past this point under certain circumstances or
304 * we risk dereferencing null pointers down below.
306 if (_gr_group.gr_name[0] == '+') {
307 if (strlen(_gr_group.gr_name) == 1) {
317 cp = &_gr_group.gr_name[1];
318 if (search && name != NULL)
319 if (strcmp(cp, name))
321 if (!_getypgroup(&_gr_group, cp,
324 if (search && name == NULL)
325 if (gid != _gr_group.gr_gid)
327 /* We're going to override -- tell the world. */
332 if (_gr_group.gr_name[0] == '+')
335 if (search && name) {
336 if(strcmp(_gr_group.gr_name, name)) {
341 if ((cp = strsep(&bp, ":\n")) == NULL)
346 if (strlen(cp) || !_ypfound)
347 _gr_group.gr_passwd = cp;
349 if ((_gr_group.gr_passwd = strsep(&bp, ":\n")) == NULL)
352 if (!(cp = strsep(&bp, ":\n")))
361 * Hurm. Should we be doing this? We allow UIDs to
362 * be overridden -- what about GIDs?
366 _gr_group.gr_gid = atoi(cp);
367 if (search && name == NULL && _gr_group.gr_gid != gid)
370 if (bp == NULL) /* !!! Must check for this! */
373 if ((cp = strsep(&bp, ":\n")) == NULL)
376 if (!strlen(cp) && _ypfound)
383 for (m = members; ; bp++) {
384 if (m == (members + maxgrp - 1)) {
385 if ((members = (char **)
388 (maxgrp + MAXGRP))) == NULL)
390 m = members + maxgrp - 1;
399 } else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
405 } else if (cp == NULL)
409 _gr_group.gr_mem = members;
420 _gr_breakout_yp(struct group *gr, char *result)
426 * XXX If 's' ends up being a NULL pointer, punt on this group.
427 * It means the NIS group entry is badly formatted and should
430 if ((s = strsep(&result, ":")) == NULL) return 0; /* name */
433 if ((s = strsep(&result, ":")) == NULL) return 0; /* password */
436 if ((s = strsep(&result, ":")) == NULL) return 0; /* gid */
437 gr->gr_gid = atoi(s);
439 if ((s = result) == NULL) return 0;
442 for (m = members; ; s++) {
443 if (m == members + maxgrp - 1) {
444 if ((members = (char **)reallocf(members,
445 sizeof(char **) * (maxgrp + MAXGRP))) == NULL)
447 m = members + maxgrp - 1;
456 } else if (*s == '\0' || *s == '\n' || *s == ' ') {
462 } else if (cp == NULL) {
466 _gr_group.gr_mem = members;
472 static char *_gr_yp_domain;
475 _getypgroup(struct group *gr, const char *name, char *map)
478 static char resultbuf[YPMAXRECORD + 2];
482 if(yp_get_default_domain(&_gr_yp_domain))
486 if(yp_match(_gr_yp_domain, map, name, strlen(name),
487 &result, &resultlen))
490 s = strchr(result, '\n');
493 if(resultlen >= sizeof resultbuf) return 0;
494 strncpy(resultbuf, result, resultlen);
495 resultbuf[resultlen] = '\0';
497 return(_gr_breakout_yp(gr, resultbuf));
503 _nextypgroup(struct group *gr)
507 char *lastkey, *result;
508 static char resultbuf[YPMAXRECORD + 2];
513 if(yp_get_default_domain(&_gr_yp_domain))
517 if(!_gr_stepping_yp) {
519 rv = yp_first(_gr_yp_domain, "group.byname",
520 &key, &keylen, &result, &resultlen);
529 rv = yp_next(_gr_yp_domain, "group.byname", key, keylen,
530 &key, &keylen, &result, &resultlen);
538 if(resultlen > sizeof(resultbuf)) {
543 strncpy(resultbuf, result, resultlen);
544 resultbuf[resultlen] = '\0';
546 if((result = strchr(resultbuf, '\n')) != NULL)
548 if (_gr_breakout_yp(gr, resultbuf))