2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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
35 #include <sys/cdefs.h>
36 __SCCSID("@(#)getnetgrent.c 8.2 (Berkeley) 4/27/95");
37 __FBSDID("$FreeBSD$");
39 #include "namespace.h"
46 #include <pthread_np.h>
57 * We want to be able to use NIS netgroups properly while retaining
58 * the ability to use a local /etc/netgroup file. Unfortunately, you
59 * can't really do both at the same time - at least, not efficiently.
60 * NetBSD deals with this problem by creating a netgroup database
61 * using Berkeley DB (just like the password database) that allows
62 * for lookups using netgroup, netgroup.byuser or netgroup.byhost
63 * searches. This is a neat idea, but I don't have time to implement
64 * something like that now. (I think ultimately it would be nice
65 * if we DB-fied the group and netgroup stuff all in one shot, but
66 * for now I'm satisfied just to have something that works well
67 * without requiring massive code changes.)
69 * Therefore, to still permit the use of the local file and maintain
70 * optimum NIS performance, we allow for the following conditions:
72 * - If /etc/netgroup does not exist and NIS is turned on, we use
75 * - If /etc/netgroup exists but is empty, we use NIS netgroups
78 * - If /etc/netgroup exists and contains _only_ a '+', we use
81 * - If /etc/netgroup exists, contains locally defined netgroups
82 * and a '+', we use a mixture of NIS and the local entries.
83 * This method should return the same NIS data as just using
84 * NIS alone, but it will be slower if the NIS netgroup database
85 * is large (innetgr() in particular will suffer since extra
86 * processing has to be done in order to determine memberships
87 * using just the raw netgroup data).
89 * - If /etc/netgroup exists and contains only locally defined
90 * netgroup entries, we use just those local entries and ignore
91 * NIS (this is the original, pre-NIS behavior).
95 #include <rpcsvc/yp_prot.h>
96 #include <rpcsvc/ypclnt.h>
97 #include <sys/param.h>
99 #include <sys/errno.h>
100 static char *_netgr_yp_domain;
102 static int _netgr_yp_enabled;
103 static int _yp_innetgr;
106 #ifndef _PATH_NETGROUP
107 #define _PATH_NETGROUP "/etc/netgroup"
111 NGRP_STORAGE_INITIAL = 1 << 10, /* 1 KByte */
112 NGRP_STORAGE_MAX = 1 << 20, /* 1 MByte */
115 static const ns_src defaultsrc[] = {
116 { NSSRC_COMPAT, NS_SUCCESS },
121 * Static Variables and functions used by setnetgrent(), getnetgrent() and
123 * There are two linked lists:
124 * - linelist is just used by setnetgrent() to parse the net group file via.
126 * - netgrp is the list of entries for the current netgroup
129 struct linelist *l_next; /* Chain ptr. */
130 int l_parsed; /* Flag for cycles */
131 char *l_groupname; /* Name of netgroup */
132 char *l_line; /* Netgroup entrie(s) to be parsed */
136 struct netgrp *ng_next; /* Chain ptr */
137 char *ng_str[3]; /* Field pointers, see below */
142 struct linelist *st_linehead;
143 struct netgrp *st_nextgrp;
144 struct netgrp *st_gr;
148 #define NG_HOST 0 /* Host name */
149 #define NG_USER 1 /* User name */
150 #define NG_DOM 2 /* and Domain name */
152 static void netgr_endstate(void *);
153 NSS_TLS_HANDLING(netgr);
155 static int files_endnetgrent(void *, void *, va_list);
156 static int files_getnetgrent_r(void *, void *, va_list);
157 static int files_setnetgrent(void *, void *, va_list);
159 static int compat_endnetgrent(void *, void *, va_list);
160 static int compat_innetgr(void *, void *, va_list);
161 static int compat_getnetgrent_r(void *, void *, va_list);
162 static int compat_setnetgrent(void *, void *, va_list);
164 static void _compat_clearstate(void);
165 static int _getnetgrent_r(char **, char **, char **, char *, size_t, int *,
166 struct netgr_state *);
167 static int _innetgr_fallback(void *, void *, const char *, const char *,
168 const char *, const char *);
169 static int innetgr_fallback(void *, void *, va_list);
170 static int parse_netgrp(const char *, struct netgr_state *, int);
171 static struct linelist *read_for_group(const char *, struct netgr_state *, int);
173 #define LINSIZ 1024 /* Length of netgroup file line */
175 static const ns_dtab getnetgrent_dtab[] = {
176 NS_FILES_CB(files_getnetgrent_r, NULL)
177 NS_COMPAT_CB(compat_getnetgrent_r, NULL)
178 { NULL, NULL, NULL },
181 static const ns_dtab setnetgrent_dtab[] = {
182 NS_FILES_CB(files_setnetgrent, NULL)
183 NS_COMPAT_CB(compat_setnetgrent, NULL)
184 { NULL, NULL, NULL },
187 static const ns_dtab endnetgrent_dtab[] = {
188 NS_FILES_CB(files_endnetgrent, NULL)
189 NS_COMPAT_CB(compat_endnetgrent, NULL)
190 { NULL, NULL, NULL },
193 static struct netgr_state compat_state;
196 netgr_endstate(void *arg)
198 struct linelist *lp, *olp;
199 struct netgrp *gp, *ogp;
200 struct netgr_state *st;
202 st = (struct netgr_state *)arg;
203 lp = st->st_linehead;
207 free(olp->l_groupname);
211 st->st_linehead = NULL;
212 if (st->st_grname != NULL) {
214 st->st_grname = NULL;
220 free(ogp->ng_str[NG_HOST]);
221 free(ogp->ng_str[NG_USER]);
222 free(ogp->ng_str[NG_DOM]);
226 st->st_nextgrp = NULL;
230 files_getnetgrent_r(void *retval, void *mdata, va_list ap)
232 struct netgr_state *st;
233 char **hostp, **userp, **domp, *buf;
237 hostp = va_arg(ap, char **);
238 userp = va_arg(ap, char **);
239 domp = va_arg(ap, char **);
240 buf = va_arg(ap, char *);
241 bufsize = va_arg(ap, size_t);
242 errnop = va_arg(ap, int *);
244 if (netgr_getstate(&st) != 0)
247 return (_getnetgrent_r(hostp, userp, domp, buf, bufsize, errnop, st));
251 files_setnetgrent(void *retval, void *mdata, va_list ap)
253 const ns_src src[] = {
254 { NSSRC_FILES, NS_SUCCESS },
257 struct netgr_state *st;
261 group = va_arg(ap, const char *);
263 if (group == NULL || group[0] == '\0')
266 rv = netgr_getstate(&st);
270 if (st->st_gr == NULL || strcmp(group, st->st_grname) != 0) {
271 (void)_nsdispatch(NULL, endnetgrent_dtab, NSDB_NETGROUP,
273 if ((st->st_netf = fopen(_PATH_NETGROUP, "re")) != NULL) {
274 if (parse_netgrp(group, st, 0) != 0)
275 (void)_nsdispatch(NULL, endnetgrent_dtab,
276 NSDB_NETGROUP, "endnetgrent", src);
278 st->st_grname = strdup(group);
279 (void)fclose(st->st_netf);
283 st->st_nextgrp = st->st_gr;
284 return (st->st_grname != NULL ? NS_SUCCESS : NS_NOTFOUND);
288 files_endnetgrent(void *retval, void *mdata, va_list ap)
290 struct netgr_state *st;
292 if (netgr_getstate(&st) != 0)
299 compat_getnetgrent_r(void *retval, void *mdata, va_list ap)
301 char **hostp, **userp, **domp, *buf;
308 hostp = va_arg(ap, char **);
309 userp = va_arg(ap, char **);
310 domp = va_arg(ap, char **);
311 buf = va_arg(ap, char *);
312 bufsize = va_arg(ap, size_t);
313 errnop = va_arg(ap, int *);
315 return (_getnetgrent_r(hostp, userp, domp, buf, bufsize, errnop,
320 * compat_setnetgrent()
321 * Parse the netgroup file looking for the netgroup and build the list
322 * of netgrp structures. Let parse_netgrp() and read_for_group() do
326 compat_setnetgrent(void *retval, void *mdata, va_list ap)
331 struct stat _yp_statp;
335 group = va_arg(ap, const char *);
338 if (group == NULL || !strlen(group))
341 if (compat_state.st_gr == NULL ||
342 strcmp(group, compat_state.st_grname) != 0) {
343 _compat_clearstate();
346 /* Presumed guilty until proven innocent. */
349 * If /etc/netgroup doesn't exist or is empty,
350 * use NIS exclusively.
352 if (((stat(_PATH_NETGROUP, &_yp_statp) < 0) &&
353 errno == ENOENT) || _yp_statp.st_size == 0)
354 _use_only_yp = _netgr_yp_enabled = 1;
355 if ((netf = fopen(_PATH_NETGROUP,"re")) != NULL ||_use_only_yp){
356 compat_state.st_netf = netf;
358 * Icky: grab the first character of the netgroup file
359 * and turn on NIS if it's a '+'. rewind the stream
360 * afterwards so we don't goof up read_for_group() later.
363 fscanf(netf, "%c", &_yp_plus);
366 _use_only_yp = _netgr_yp_enabled = 1;
369 * If we were called specifically for an innetgr()
370 * lookup and we're in NIS-only mode, short-circuit
371 * parse_netgroup() and cut directly to the chase.
373 if (_use_only_yp && _yp_innetgr) {
380 if ((netf = fopen(_PATH_NETGROUP, "re"))) {
381 compat_state.st_netf = netf;
383 if (parse_netgrp(group, &compat_state, 1)) {
384 _compat_clearstate();
386 compat_state.st_grname = strdup(group);
392 compat_state.st_nextgrp = compat_state.st_gr;
397 _compat_clearstate(void)
401 _netgr_yp_enabled = 0;
403 netgr_endstate(&compat_state);
407 * compat_endnetgrent() - cleanup
410 compat_endnetgrent(void *retval, void *mdata, va_list ap)
413 _compat_clearstate();
418 _getnetgrent_r(char **hostp, char **userp, char **domp, char *buf,
419 size_t bufsize, int *errnop, struct netgr_state *st)
425 #define COPY_NG_ELEM(dstp, i) do { \
426 src = st->st_nextgrp->ng_str[(i)]; \
429 len = strlcpy(p, src, bufsize); \
430 if (len >= bufsize) { \
432 return (NS_RETURN); \
436 bufsize -= len + 1; \
440 if (st->st_nextgrp != NULL) {
441 COPY_NG_ELEM(hostp, NG_HOST);
442 COPY_NG_ELEM(userp, NG_USER);
443 COPY_NG_ELEM(domp, NG_DOM);
444 st->st_nextgrp = st->st_nextgrp->ng_next;
456 _listmatch(const char *list, const char *group, int len)
458 const char *ptr = list;
460 int glen = strlen(group);
462 /* skip possible leading whitespace */
463 while (isspace((unsigned char)*ptr))
466 while (ptr < list + len) {
468 while(*ptr != ',' && *ptr != '\0' && !isspace((unsigned char)*ptr))
470 if (strncmp(cptr, group, glen) == 0 && glen == (ptr - cptr))
472 while (*ptr == ',' || isspace((unsigned char)*ptr))
480 _revnetgr_lookup(char* lookupdom, char* map, const char* str,
481 const char* dom, const char* group)
484 char key[MAXHOSTNAMELEN];
488 for (rot = 0; ; rot++) {
491 snprintf(key, MAXHOSTNAMELEN, "%s.%s", str,
492 dom ? dom : lookupdom);
495 snprintf(key, MAXHOSTNAMELEN, "%s.*", str);
498 snprintf(key, MAXHOSTNAMELEN, "*.%s",
499 dom ? dom : lookupdom);
502 snprintf(key, MAXHOSTNAMELEN, "*.*");
507 y = yp_match(lookupdom, map, key, strlen(key), &result,
510 rv = _listmatch(result, group, resultlen);
514 } else if (y != YPERR_KEY) {
516 * If we get an error other than 'no
517 * such key in map' then something is
518 * wrong and we should stop the search.
527 * Search for a match in a netgroup.
530 compat_innetgr(void *retval, void *mdata, va_list ap)
533 const ns_src src[] = {
534 { mdata, NS_SUCCESS },
538 const char *group, *host, *user, *dom;
540 group = va_arg(ap, const char *);
541 host = va_arg(ap, const char *);
542 user = va_arg(ap, const char *);
543 dom = va_arg(ap, const char *);
545 if (group == NULL || !strlen(group))
550 (void)_nsdispatch(NULL, setnetgrent_dtab, NSDB_NETGROUP, "setnetgrent",
554 * If we're in NIS-only mode, do the search using
555 * NIS 'reverse netgroup' lookups.
557 * What happens with 'reverse netgroup' lookups:
559 * 1) try 'reverse netgroup' lookup
560 * 1.a) if host is specified and user is null:
561 * look in netgroup.byhost
562 * (try host.domain, host.*, *.domain or *.*)
563 * if found, return yes
564 * 1.b) if user is specified and host is null:
565 * look in netgroup.byuser
566 * (try host.domain, host.*, *.domain or *.*)
567 * if found, return yes
568 * 1.c) if both host and user are specified,
569 * don't do 'reverse netgroup' lookup. It won't work.
570 * 1.d) if neither host ane user are specified (why?!?)
571 * don't do 'reverse netgroup' lookup either.
572 * 2) if domain is specified and 'reverse lookup' is done:
573 * 'reverse lookup' was authoritative. bye bye.
574 * 3) otherwise, too bad, try it the slow way.
576 if (_use_only_yp && (host == NULL) != (user == NULL)) {
578 if(yp_get_default_domain(&_netgr_yp_domain))
579 return (NS_NOTFOUND);
580 ret = _revnetgr_lookup(_netgr_yp_domain,
581 host?"netgroup.byhost":"netgroup.byuser",
582 host?host:user, dom, group);
586 } else if (ret == 0 && dom != NULL) {
593 return (_innetgr_fallback(retval, mdata, group, host, user, dom));
597 _innetgr_fallback(void *retval, void *mdata, const char *group, const char *host,
598 const char *user, const char *dom)
600 const ns_src src[] = {
601 { mdata, NS_SUCCESS },
609 if (group == NULL || group[0] == '\0')
612 bufsize = NGRP_STORAGE_INITIAL;
613 buf = malloc(bufsize);
619 (void)_nsdispatch(NULL, setnetgrent_dtab, NSDB_NETGROUP, "setnetgrent",
625 rv = _nsdispatch(NULL, getnetgrent_dtab, NSDB_NETGROUP,
626 "getnetgrent_r", src, &h, &u, &d, buf, bufsize,
628 if (rv != NS_SUCCESS && ret_errno == ERANGE) {
630 if (bufsize > NGRP_STORAGE_MAX ||
631 (buf = reallocf(buf, bufsize)) == NULL)
634 } while (rv != NS_SUCCESS && ret_errno == ERANGE);
636 if (rv != NS_SUCCESS) {
637 if (rv == NS_NOTFOUND && ret_errno == 0)
642 if ((host == NULL || h == NULL || strcmp(host, h) == 0) &&
643 (user == NULL || u == NULL || strcmp(user, u) == 0) &&
644 (dom == NULL || d == NULL || strcmp(dom, d) == 0)) {
652 (void)_nsdispatch(NULL, endnetgrent_dtab, NSDB_NETGROUP, "endnetgrent",
658 innetgr_fallback(void *retval, void *mdata, va_list ap)
660 const char *group, *host, *user, *dom;
662 group = va_arg(ap, const char *);
663 host = va_arg(ap, const char *);
664 user = va_arg(ap, const char *);
665 dom = va_arg(ap, const char *);
667 return (_innetgr_fallback(retval, mdata, group, host, user, dom));
671 * Parse the netgroup file setting up the linked lists.
674 parse_netgrp(const char *group, struct netgr_state *st, int niscompat)
677 struct linelist *lp = st->st_linehead;
679 char *epos, *gpos, *pos, *spos;
680 int freepos, len, strpos;
686 * First, see if the line has already been read in.
689 if (!strcmp(group, lp->l_groupname))
693 if (lp == NULL && (lp = read_for_group(group, st, niscompat)) == NULL)
698 * This error message is largely superflous since the
699 * code handles the error condition sucessfully, and
700 * spewing it out from inside libc can actually hose
703 fprintf(stderr, "Cycle in netgroup %s\n", lp->l_groupname);
709 /* Watch for null pointer dereferences, dammit! */
710 while (pos != NULL && *pos != '\0') {
712 grp = malloc(sizeof(*grp));
716 bzero(grp, sizeof(*grp));
718 gpos = strsep(&pos, ")");
722 for (strpos = 0; strpos < 3; strpos++) {
723 if ((spos = strsep(&gpos, ",")) == NULL) {
725 * All other systems I've tested
726 * return NULL for empty netgroup
727 * fields. It's up to user programs
728 * to handle the NULLs appropriately.
736 while (*spos == ' ' || *spos == '\t')
738 if ((epos = strpbrk(spos, " \t"))) {
745 ng[strpos] = malloc(len + 1);
746 if (ng[strpos] == NULL) {
747 for (freepos = 0; freepos < strpos;
753 bcopy(spos, ng[strpos], len + 1);
755 grp->ng_next = st->st_gr;
759 * Note: on other platforms, malformed netgroup
760 * entries are not normally flagged. While we
761 * can catch bad entries and report them, we should
762 * stay silent by default for compatibility's sake.
766 "Bad entry (%s%s%s%s%s) in netgroup \"%s\"\n",
767 ng[NG_HOST] == NULL ? "" : ng[NG_HOST],
768 ng[NG_USER] == NULL ? "" : ",",
769 ng[NG_USER] == NULL ? "" : ng[NG_USER],
770 ng[NG_DOM] == NULL ? "" : ",",
771 ng[NG_DOM] == NULL ? "" : ng[NG_DOM],
776 spos = strsep(&pos, ", \t");
777 if (parse_netgrp(spos, st, niscompat))
782 while (*pos == ' ' || *pos == ',' || *pos == '\t')
789 * Read the netgroup file and save lines until the line for the netgroup
790 * is found. Return 1 if eof is encountered.
792 static struct linelist *
793 read_for_group(const char *group, struct netgr_state *st, int niscompat)
795 char *linep, *olinep, *pos, *spos;
799 char line[LINSIZ + 2];
807 while ((_netgr_yp_enabled && niscompat) ||
808 fgets(line, LINSIZ, netf) != NULL) {
809 if (_netgr_yp_enabled) {
810 if(!_netgr_yp_domain)
811 if(yp_get_default_domain(&_netgr_yp_domain))
813 if (yp_match(_netgr_yp_domain, "netgroup", group,
814 strlen(group), &result, &resultlen)) {
817 return ((struct linelist *)0);
819 _netgr_yp_enabled = 0;
823 if (strlen(result) == 0) {
827 snprintf(line, LINSIZ, "%s %s", group, result);
832 while (fgets(line, LINSIZ, netf) != NULL) {
836 if (niscompat && *pos == '+') {
837 _netgr_yp_enabled = 1;
843 while (*pos == ' ' || *pos == '\t')
846 while (*pos != ' ' && *pos != '\t' && *pos != '\n' &&
850 while (*pos == ' ' || *pos == '\t')
852 if (*pos != '\n' && *pos != '\0') {
853 lp = malloc(sizeof (*lp));
857 lp->l_groupname = malloc(len + 1);
858 if (lp->l_groupname == NULL) {
862 bcopy(spos, lp->l_groupname, len);
863 *(lp->l_groupname + len) = '\0';
868 * Loop around handling line continuations.
871 if (*(pos + len - 1) == '\n')
873 if (*(pos + len - 1) == '\\') {
879 linep = malloc(olen + len + 1);
881 free(lp->l_groupname);
888 bcopy(olinep, linep, olen);
891 bcopy(pos, linep + olen, len);
893 *(linep + olen) = '\0';
897 if (fgets(line, LINSIZ, netf)) {
905 lp->l_next = st->st_linehead;
906 st->st_linehead = lp;
909 * If this is the one we wanted, we are done.
911 if (!strcmp(lp->l_groupname, group))
917 * Yucky. The recursive nature of this whole mess might require
918 * us to make more than one pass through the netgroup file.
919 * This might be best left outside the #ifdef YP, but YP is
920 * defined by default anyway, so I'll leave it like this
921 * until I know better.
929 getnetgrent_r(char **hostp, char **userp, char **domp, char *buf, size_t bufsize)
934 rv = _nsdispatch(NULL, getnetgrent_dtab, NSDB_NETGROUP, "getnetgrent_r",
935 defaultsrc, hostp, userp, domp, buf, bufsize, &ret_errno);
936 if (rv == NS_SUCCESS) {
945 getnetgrent(char **hostp, char **userp, char **domp)
947 static char *ngrp_storage;
948 static size_t ngrp_storage_size;
951 if (ngrp_storage == NULL) {
952 ngrp_storage_size = NGRP_STORAGE_INITIAL;
953 ngrp_storage = malloc(ngrp_storage_size);
954 if (ngrp_storage == NULL)
960 rv = _nsdispatch(NULL, getnetgrent_dtab, NSDB_NETGROUP,
961 "getnetgrent_r", defaultsrc, hostp, userp, domp,
962 ngrp_storage, ngrp_storage_size, &ret_errno);
963 if (rv != NS_SUCCESS && ret_errno == ERANGE) {
964 ngrp_storage_size *= 2;
965 if (ngrp_storage_size > NGRP_STORAGE_MAX) {
971 ngrp_storage = reallocf(ngrp_storage,
973 if (ngrp_storage == NULL)
976 } while (rv != NS_SUCCESS && ret_errno == ERANGE);
978 if (rv == NS_SUCCESS) {
987 setnetgrent(const char *netgroup)
990 (void)_nsdispatch(NULL, setnetgrent_dtab, NSDB_NETGROUP, "setnetgrent",
991 defaultsrc, netgroup);
998 (void)_nsdispatch(NULL, endnetgrent_dtab, NSDB_NETGROUP, "endnetgrent",
1003 innetgr(const char *netgroup, const char *host, const char *user,
1006 static const ns_dtab dtab[] = {
1007 NS_COMPAT_CB(compat_innetgr, NULL)
1008 NS_FALLBACK_CB(innetgr_fallback)
1009 { NULL, NULL, NULL },
1013 rv = _nsdispatch(&result, dtab, NSDB_NETGROUP, "innetgr", defaultsrc,
1014 netgroup, host, user, domain);
1015 return (rv == NS_SUCCESS ? result : 0);