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
36 static const char rcsid[] =
41 * This is a specially hacked-up version of getnetgrent.c used to parse
42 * data from the stored hash table of netgroup info rather than from a
43 * file. It's used mainly for the parse_netgroup() function. All the YP
44 * stuff and file support has been stripped out since it isn't needed.
55 * Static Variables and functions used by setnetgrent(), getnetgrent() and
57 * There are two linked lists:
58 * - linelist is just used by setnetgrent() to parse the net group file via.
60 * - netgrp is the list of entries for the current netgroup
63 struct linelist *l_next; /* Chain ptr. */
64 int l_parsed; /* Flag for cycles */
65 char *l_groupname; /* Name of netgroup */
66 char *l_line; /* Netgroup entrie(s) to be parsed */
70 struct netgrp *ng_next; /* Chain ptr */
71 char *ng_str[3]; /* Field pointers, see below */
73 #define NG_HOST 0 /* Host name */
74 #define NG_USER 1 /* User name */
75 #define NG_DOM 2 /* and Domain name */
77 static struct linelist *linehead = (struct linelist *)0;
78 static struct netgrp *nextgrp = (struct netgrp *)0;
86 static int parse_netgrp(char *group);
87 static struct linelist *read_for_group(char *group);
88 extern struct group_entry *gtable[];
92 * Parse the netgroup file looking for the netgroup and build the list
93 * of netgrp structures. Let parse_netgrp() and read_for_group() do
97 __setnetgrent(char *group)
101 if (group == NULL || !strlen(group))
104 if (grouphead.gr == (struct netgrp *)0 ||
105 strcmp(group, grouphead.grname)) {
107 if (parse_netgrp(group))
110 grouphead.grname = (char *)
111 malloc(strlen(group) + 1);
112 strcpy(grouphead.grname, group);
115 nextgrp = grouphead.gr;
119 * Get the next netgroup off the list.
122 __getnetgrent(char **hostp, char **userp, char **domp)
125 *hostp = nextgrp->ng_str[NG_HOST];
126 *userp = nextgrp->ng_str[NG_USER];
127 *domp = nextgrp->ng_str[NG_DOM];
128 nextgrp = nextgrp->ng_next;
135 * __endnetgrent() - cleanup
140 struct linelist *lp, *olp;
141 struct netgrp *gp, *ogp;
147 free(olp->l_groupname);
151 linehead = (struct linelist *)0;
152 if (grouphead.grname) {
153 free(grouphead.grname);
154 grouphead.grname = (char *)0;
160 if (ogp->ng_str[NG_HOST])
161 free(ogp->ng_str[NG_HOST]);
162 if (ogp->ng_str[NG_USER])
163 free(ogp->ng_str[NG_USER]);
164 if (ogp->ng_str[NG_DOM])
165 free(ogp->ng_str[NG_DOM]);
168 grouphead.gr = (struct netgrp *)0;
172 * Parse the netgroup file setting up the linked lists.
175 parse_netgrp(char *group)
184 struct linelist *lp = linehead;
187 * First, see if the line has already been read in.
190 if (!strcmp(group, lp->l_groupname))
194 if (lp == (struct linelist *)0 &&
195 (lp = read_for_group(group)) == (struct linelist *)0)
200 * This error message is largely superfluous since the
201 * code handles the error condition successfully, and
202 * spewing it out from inside libc can actually hose
205 warnx("cycle in netgroup %s", lp->l_groupname);
211 /* Watch for null pointer dereferences, dammit! */
212 while (pos != NULL && *pos != '\0') {
214 grp = (struct netgrp *)malloc(sizeof (struct netgrp));
215 bzero((char *)grp, sizeof (struct netgrp));
216 grp->ng_next = grouphead.gr;
219 gpos = strsep(&pos, ")");
223 for (strpos = 0; strpos < 3; strpos++) {
224 if ((spos = strsep(&gpos, ","))) {
228 while (*spos == ' ' || *spos == '\t')
230 if ((epos = strpbrk(spos, " \t"))) {
236 grp->ng_str[strpos] = (char *)
238 bcopy(spos, grp->ng_str[strpos],
243 * All other systems I've tested
244 * return NULL for empty netgroup
245 * fields. It's up to user programs
246 * to handle the NULLs appropriately.
248 grp->ng_str[strpos] = NULL;
253 * Note: on other platforms, malformed netgroup
254 * entries are not normally flagged. While we
255 * can catch bad entries and report them, we should
256 * stay silent by default for compatibility's sake.
259 warnx("bad entry (%s%s%s%s%s) in netgroup \"%s\"",
260 grp->ng_str[NG_HOST] == NULL ? "" : grp->ng_str[NG_HOST],
261 grp->ng_str[NG_USER] == NULL ? "" : ",",
262 grp->ng_str[NG_USER] == NULL ? "" : grp->ng_str[NG_USER],
263 grp->ng_str[NG_DOM] == NULL ? "" : ",",
264 grp->ng_str[NG_DOM] == NULL ? "" : grp->ng_str[NG_DOM],
268 spos = strsep(&pos, ", \t");
269 if (parse_netgrp(spos))
272 /* Watch for null pointer dereferences, dammit! */
274 while (*pos == ' ' || *pos == ',' || *pos == '\t')
281 * Read the netgroup file and save lines until the line for the netgroup
282 * is found. Return 1 if eof is encountered.
284 static struct linelist *
285 read_for_group(char *group)
287 char *pos, *spos, *linep = NULL, *olinep = NULL;
291 char line[LINSIZ + 1];
294 data = lookup (gtable, group);
295 sprintf(line, "%s %s", group, data);
301 while (*pos == ' ' || *pos == '\t')
304 while (*pos != ' ' && *pos != '\t' && *pos != '\n' &&
308 while (*pos == ' ' || *pos == '\t')
310 if (*pos != '\n' && *pos != '\0') {
311 lp = (struct linelist *)malloc(sizeof (*lp));
313 lp->l_groupname = (char *)malloc(len + 1);
314 bcopy(spos, lp->l_groupname, len);
315 *(lp->l_groupname + len) = '\0';
319 * Loop around handling line continuations.
322 if (*(pos + len - 1) == '\n')
324 if (*(pos + len - 1) == '\\') {
330 linep = (char *)malloc(olen + len + 1);
332 bcopy(olinep, linep, olen);
335 bcopy(pos, linep + olen, len);
337 *(linep + olen) = '\0';
342 if (fgets(line, LINSIZ, netf)) {
351 lp->l_next = linehead;
355 * If this is the one we wanted, we are done.
357 if (!strcmp(lp->l_groupname, group))
361 return ((struct linelist *)0);