2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * $FreeBSD: src/lib/libc/gen/getnetgrent.c,v 1.26 1999/11/04 04:16:27 ache Exp $
39 #if defined(LIBC_SCCS) && !defined(lint)
40 static char sccsid[] = "@(#)getnetgrent.c 8.2 (Berkeley) 4/27/95";
41 #endif /* LIBC_SCCS and not lint */
51 * We want to be able to use NIS netgroups properly while retaining
52 * the ability to use a local /etc/netgroup file. Unfortunately, you
53 * can't really do both at the same time - at least, not efficiently.
54 * NetBSD deals with this problem by creating a netgroup database
55 * using Berkeley DB (just like the password database) that allows
56 * for lookups using netgroup, netgroup.byuser or netgroup.byhost
57 * searches. This is a neat idea, but I don't have time to implement
58 * something like that now. (I think ultimately it would be nice
59 * if we DB-fied the group and netgroup stuff all in one shot, but
60 * for now I'm satisfied just to have something that works well
61 * without requiring massive code changes.)
63 * Therefore, to still permit the use of the local file and maintain
64 * optimum NIS performance, we allow for the following conditions:
66 * - If /etc/netgroup does not exist and NIS is turned on, we use
69 * - If /etc/netgroup exists but is empty, we use NIS netgroups
72 * - If /etc/netgroup exists and contains _only_ a '+', we use
75 * - If /etc/netgroup exists, contains locally defined netgroups
76 * and a '+', we use a mixture of NIS and the local entries.
77 * This method should return the same NIS data as just using
78 * NIS alone, but it will be slower if the NIS netgroup database
79 * is large (innetgr() in particular will suffer since extra
80 * processing has to be done in order to determine memberships
81 * using just the raw netgroup data).
83 * - If /etc/netgroup exists and contains only locally defined
84 * netgroup entries, we use just those local entries and ignore
85 * NIS (this is the original, pre-NIS behavior).
89 #include <rpcsvc/yp_prot.h>
90 #include <rpcsvc/ypclnt.h>
91 #include <sys/types.h>
93 #include <sys/param.h>
94 #include <sys/errno.h>
95 static char *_netgr_yp_domain;
97 static int _netgr_yp_enabled;
98 static int _yp_innetgr;
101 #ifndef _PATH_NETGROUP
102 #define _PATH_NETGROUP "/etc/netgroup"
106 * Static Variables and functions used by setnetgrent(), getnetgrent() and
108 * There are two linked lists:
109 * - linelist is just used by setnetgrent() to parse the net group file via.
111 * - netgrp is the list of entries for the current netgroup
114 struct linelist *l_next; /* Chain ptr. */
115 int l_parsed; /* Flag for cycles */
116 char *l_groupname; /* Name of netgroup */
117 char *l_line; /* Netgroup entrie(s) to be parsed */
121 struct netgrp *ng_next; /* Chain ptr */
122 char *ng_str[3]; /* Field pointers, see below */
124 #define NG_HOST 0 /* Host name */
125 #define NG_USER 1 /* User name */
126 #define NG_DOM 2 /* and Domain name */
128 static struct linelist *linehead = (struct linelist *)0;
129 static struct netgrp *nextgrp = (struct netgrp *)0;
137 static FILE *netf = (FILE *)0;
138 static int parse_netgrp();
139 static struct linelist *read_for_group();
140 void setnetgrent(), endnetgrent();
141 int getnetgrent(), innetgr();
143 #define LINSIZ 1024 /* Length of netgroup file line */
147 * Parse the netgroup file looking for the netgroup and build the list
148 * of netgrp structures. Let parse_netgrp() and read_for_group() do
156 struct stat _yp_statp;
162 if (group == NULL || !strlen(group))
165 if (grouphead.gr == (struct netgrp *)0 ||
166 strcmp(group, grouphead.grname)) {
169 /* Presumed guilty until proven innocent. */
172 * If /etc/netgroup doesn't exist or is empty,
173 * use NIS exclusively.
175 if (((stat(_PATH_NETGROUP, &_yp_statp) < 0) &&
176 errno == ENOENT) || _yp_statp.st_size == 0)
177 _use_only_yp = _netgr_yp_enabled = 1;
178 if ((netf = fopen(_PATH_NETGROUP,"r")) != NULL ||_use_only_yp){
180 * Icky: grab the first character of the netgroup file
181 * and turn on NIS if it's a '+'. rewind the stream
182 * afterwards so we don't goof up read_for_group() later.
185 fscanf(netf, "%c", &_yp_plus);
188 _use_only_yp = _netgr_yp_enabled = 1;
191 * If we were called specifically for an innetgr()
192 * lookup and we're in NIS-only mode, short-circuit
193 * parse_netgroup() and cut directly to the chase.
195 if (_use_only_yp && _yp_innetgr) {
202 if (netf = fopen(_PATH_NETGROUP, "r")) {
204 if (parse_netgrp(group))
207 grouphead.grname = (char *)
208 malloc(strlen(group) + 1);
209 strcpy(grouphead.grname, group);
215 nextgrp = grouphead.gr;
219 * Get the next netgroup off the list.
222 getnetgrent(hostp, userp, domp)
223 char **hostp, **userp, **domp;
230 *hostp = nextgrp->ng_str[NG_HOST];
231 *userp = nextgrp->ng_str[NG_USER];
232 *domp = nextgrp->ng_str[NG_DOM];
233 nextgrp = nextgrp->ng_next;
240 * endnetgrent() - cleanup
245 register struct linelist *lp, *olp;
246 register struct netgrp *gp, *ogp;
252 free(olp->l_groupname);
256 linehead = (struct linelist *)0;
257 if (grouphead.grname) {
258 free(grouphead.grname);
259 grouphead.grname = (char *)0;
265 if (ogp->ng_str[NG_HOST])
266 free(ogp->ng_str[NG_HOST]);
267 if (ogp->ng_str[NG_USER])
268 free(ogp->ng_str[NG_USER]);
269 if (ogp->ng_str[NG_DOM])
270 free(ogp->ng_str[NG_DOM]);
273 grouphead.gr = (struct netgrp *)0;
275 _netgr_yp_enabled = 0;
280 static int _listmatch(list, group, len)
284 char *ptr = list, *cptr;
285 int glen = strlen(group);
287 /* skip possible leading whitespace */
288 while(isspace((unsigned char)*ptr))
291 while (ptr < list + len) {
293 while(*ptr != ',' && *ptr != '\0' && !isspace((unsigned char)*ptr))
295 if (strncmp(cptr, group, glen) == 0 && glen == (ptr - cptr))
297 while(*ptr == ',' || isspace((unsigned char)*ptr))
304 static int _buildkey(key, str, dom, rotation)
305 char *key, *str, *dom;
312 case(1): sprintf((char *)key, "%s.%s", str, dom ? dom : "*");
314 case(2): sprintf((char *)key, "%s.*", str);
316 case(3): sprintf((char *)key, "*.%s", dom ? dom : "*");
318 case(4): sprintf((char *)key, "*.*");
326 * Search for a match in a netgroup.
329 innetgr(group, host, user, dom)
330 const char *group, *host, *user, *dom;
332 char *hst, *usr, *dm;
340 if (group == NULL || !strlen(group))
350 * If we're in NIS-only mode, do the search using
351 * NIS 'reverse netgroup' lookups.
354 char _key[MAXHOSTNAMELEN];
357 if(yp_get_default_domain(&_netgr_yp_domain))
359 while(_buildkey(_key, user ? user : host, dom, &rot)) {
360 y = yp_match(_netgr_yp_domain, user? "netgroup.byuser":
361 "netgroup.byhost", _key, strlen(_key), &result,
365 * If we get an error other than 'no
366 * such key in map' then something is
367 * wrong and we should stop the search.
372 rv = _listmatch(result, group, resultlen);
381 * Couldn't match using NIS-exclusive mode. If the error
382 * was YPERR_MAP, then the failure happened because there
383 * was no netgroup.byhost or netgroup.byuser map. The odds
384 * are we are talking to an Sun NIS+ server in YP emulation
385 * mode; if this is the case, then we have to do the check
386 * the 'old-fashioned' way by grovelling through the netgroup
387 * map and resolving memberships on the fly.
396 while (getnetgrent(&hst, &usr, &dm))
397 if ((host == NULL || hst == NULL || !strcmp(host, hst)) &&
398 (user == NULL || usr == NULL || !strcmp(user, usr)) &&
399 ( dom == NULL || dm == NULL || !strcmp(dom, dm))) {
408 * Parse the netgroup file setting up the linked lists.
414 register char *spos, *epos;
415 register int len, strpos;
421 struct linelist *lp = linehead;
424 * First, see if the line has already been read in.
427 if (!strcmp(group, lp->l_groupname))
431 if (lp == (struct linelist *)0 &&
432 (lp = read_for_group(group)) == (struct linelist *)0)
437 * This error message is largely superflous since the
438 * code handles the error condition sucessfully, and
439 * spewing it out from inside libc can actually hose
442 fprintf(stderr, "Cycle in netgroup %s\n", lp->l_groupname);
448 /* Watch for null pointer dereferences, dammit! */
449 while (pos != NULL && *pos != '\0') {
451 grp = (struct netgrp *)malloc(sizeof (struct netgrp));
452 bzero((char *)grp, sizeof (struct netgrp));
453 grp->ng_next = grouphead.gr;
456 gpos = strsep(&pos, ")");
460 for (strpos = 0; strpos < 3; strpos++) {
461 if ((spos = strsep(&gpos, ","))) {
465 while (*spos == ' ' || *spos == '\t')
467 if ((epos = strpbrk(spos, " \t"))) {
473 grp->ng_str[strpos] = (char *)
475 bcopy(spos, grp->ng_str[strpos],
480 * All other systems I've tested
481 * return NULL for empty netgroup
482 * fields. It's up to user programs
483 * to handle the NULLs appropriately.
485 grp->ng_str[strpos] = NULL;
490 * Note: on other platforms, malformed netgroup
491 * entries are not normally flagged. While we
492 * can catch bad entries and report them, we should
493 * stay silent by default for compatibility's sake.
496 fprintf(stderr, "Bad entry (%s%s%s%s%s) in netgroup \"%s\"\n",
497 grp->ng_str[NG_HOST] == NULL ? "" : grp->ng_str[NG_HOST],
498 grp->ng_str[NG_USER] == NULL ? "" : ",",
499 grp->ng_str[NG_USER] == NULL ? "" : grp->ng_str[NG_USER],
500 grp->ng_str[NG_DOM] == NULL ? "" : ",",
501 grp->ng_str[NG_DOM] == NULL ? "" : grp->ng_str[NG_DOM],
505 spos = strsep(&pos, ", \t");
506 if (parse_netgrp(spos))
511 while (*pos == ' ' || *pos == ',' || *pos == '\t')
518 * Read the netgroup file and save lines until the line for the netgroup
519 * is found. Return 1 if eof is encountered.
521 static struct linelist *
522 read_for_group(group)
525 register char *pos, *spos, *linep, *olinep;
526 register int len, olen;
529 char line[LINSIZ + 2];
534 while (_netgr_yp_enabled || fgets(line, LINSIZ, netf) != NULL) {
535 if (_netgr_yp_enabled) {
536 if(!_netgr_yp_domain)
537 if(yp_get_default_domain(&_netgr_yp_domain))
539 if (yp_match(_netgr_yp_domain, "netgroup", group,
540 strlen(group), &result, &resultlen)) {
543 return ((struct linelist *)0);
545 _netgr_yp_enabled = 0;
549 snprintf(line, LINSIZ, "%s %s", group, result);
553 while (fgets(line, LINSIZ, netf) != NULL) {
558 _netgr_yp_enabled = 1;
564 while (*pos == ' ' || *pos == '\t')
567 while (*pos != ' ' && *pos != '\t' && *pos != '\n' &&
571 while (*pos == ' ' || *pos == '\t')
573 if (*pos != '\n' && *pos != '\0') {
574 lp = (struct linelist *)malloc(sizeof (*lp));
576 lp->l_groupname = (char *)malloc(len + 1);
577 bcopy(spos, lp->l_groupname, len);
578 *(lp->l_groupname + len) = '\0';
583 * Loop around handling line continuations.
586 if (*(pos + len - 1) == '\n')
588 if (*(pos + len - 1) == '\\') {
594 linep = (char *)malloc(olen + len + 1);
596 bcopy(olinep, linep, olen);
599 bcopy(pos, linep + olen, len);
601 *(linep + olen) = '\0';
605 if (fgets(line, LINSIZ, netf)) {
613 lp->l_next = linehead;
617 * If this is the one we wanted, we are done.
619 if (!strcmp(lp->l_groupname, group))
625 * Yucky. The recursive nature of this whole mess might require
626 * us to make more than one pass through the netgroup file.
627 * This might be best left outside the #ifdef YP, but YP is
628 * defined by default anyway, so I'll leave it like this
629 * until I know better.
633 return ((struct linelist *)0);