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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char rcsid[] =
39 * This is a specially hacked-up version of getnetgrent.c used to parse
40 * data from the stored hash table of netgroup info rather than from a
41 * file. It's used mainly for the parse_netgroup() function. All the YP
42 * stuff and file support has been stripped out since it isn't needed.
53 * Static Variables and functions used by setnetgrent(), getnetgrent() and
55 * There are two linked lists:
56 * - linelist is just used by setnetgrent() to parse the net group file via.
58 * - netgrp is the list of entries for the current netgroup
61 struct linelist *l_next; /* Chain ptr. */
62 int l_parsed; /* Flag for cycles */
63 char *l_groupname; /* Name of netgroup */
64 char *l_line; /* Netgroup entrie(s) to be parsed */
68 struct netgrp *ng_next; /* Chain ptr */
69 char *ng_str[3]; /* Field pointers, see below */
71 #define NG_HOST 0 /* Host name */
72 #define NG_USER 1 /* User name */
73 #define NG_DOM 2 /* and Domain name */
75 static struct linelist *linehead = (struct linelist *)0;
76 static struct netgrp *nextgrp = (struct netgrp *)0;
84 static int parse_netgrp(char *group);
85 static struct linelist *read_for_group(char *group);
86 extern struct group_entry *gtable[];
90 * Parse the netgroup file looking for the netgroup and build the list
91 * of netgrp structures. Let parse_netgrp() and read_for_group() do
95 __setnetgrent(char *group)
99 if (group == NULL || !strlen(group))
102 if (grouphead.gr == (struct netgrp *)0 ||
103 strcmp(group, grouphead.grname)) {
105 if (parse_netgrp(group))
108 grouphead.grname = (char *)
109 malloc(strlen(group) + 1);
110 strcpy(grouphead.grname, group);
113 nextgrp = grouphead.gr;
117 * Get the next netgroup off the list.
120 __getnetgrent(char **hostp, char **userp, char **domp)
123 *hostp = nextgrp->ng_str[NG_HOST];
124 *userp = nextgrp->ng_str[NG_USER];
125 *domp = nextgrp->ng_str[NG_DOM];
126 nextgrp = nextgrp->ng_next;
133 * __endnetgrent() - cleanup
138 struct linelist *lp, *olp;
139 struct netgrp *gp, *ogp;
145 free(olp->l_groupname);
149 linehead = (struct linelist *)0;
150 if (grouphead.grname) {
151 free(grouphead.grname);
152 grouphead.grname = (char *)0;
158 if (ogp->ng_str[NG_HOST])
159 free(ogp->ng_str[NG_HOST]);
160 if (ogp->ng_str[NG_USER])
161 free(ogp->ng_str[NG_USER]);
162 if (ogp->ng_str[NG_DOM])
163 free(ogp->ng_str[NG_DOM]);
166 grouphead.gr = (struct netgrp *)0;
170 * Parse the netgroup file setting up the linked lists.
173 parse_netgrp(char *group)
182 struct linelist *lp = linehead;
185 * First, see if the line has already been read in.
188 if (!strcmp(group, lp->l_groupname))
192 if (lp == (struct linelist *)0 &&
193 (lp = read_for_group(group)) == (struct linelist *)0)
198 * This error message is largely superfluous since the
199 * code handles the error condition successfully, and
200 * spewing it out from inside libc can actually hose
203 warnx("cycle in netgroup %s", lp->l_groupname);
209 /* Watch for null pointer dereferences, dammit! */
210 while (pos != NULL && *pos != '\0') {
212 grp = (struct netgrp *)malloc(sizeof (struct netgrp));
213 bzero((char *)grp, sizeof (struct netgrp));
214 grp->ng_next = grouphead.gr;
217 gpos = strsep(&pos, ")");
221 for (strpos = 0; strpos < 3; strpos++) {
222 if ((spos = strsep(&gpos, ","))) {
226 while (*spos == ' ' || *spos == '\t')
228 if ((epos = strpbrk(spos, " \t"))) {
234 grp->ng_str[strpos] = (char *)
236 bcopy(spos, grp->ng_str[strpos],
241 * All other systems I've tested
242 * return NULL for empty netgroup
243 * fields. It's up to user programs
244 * to handle the NULLs appropriately.
246 grp->ng_str[strpos] = NULL;
251 * Note: on other platforms, malformed netgroup
252 * entries are not normally flagged. While we
253 * can catch bad entries and report them, we should
254 * stay silent by default for compatibility's sake.
257 warnx("bad entry (%s%s%s%s%s) in netgroup \"%s\"",
258 grp->ng_str[NG_HOST] == NULL ? "" : grp->ng_str[NG_HOST],
259 grp->ng_str[NG_USER] == NULL ? "" : ",",
260 grp->ng_str[NG_USER] == NULL ? "" : grp->ng_str[NG_USER],
261 grp->ng_str[NG_DOM] == NULL ? "" : ",",
262 grp->ng_str[NG_DOM] == NULL ? "" : grp->ng_str[NG_DOM],
266 spos = strsep(&pos, ", \t");
267 if (parse_netgrp(spos))
270 /* Watch for null pointer dereferences, dammit! */
272 while (*pos == ' ' || *pos == ',' || *pos == '\t')
279 * Read the netgroup file and save lines until the line for the netgroup
280 * is found. Return 1 if eof is encountered.
282 static struct linelist *
283 read_for_group(char *group)
285 char *pos, *spos, *linep = NULL, *olinep = NULL;
289 char line[LINSIZ + 1];
292 data = lookup (gtable, group);
293 sprintf(line, "%s %s", group, data);
299 while (*pos == ' ' || *pos == '\t')
302 while (*pos != ' ' && *pos != '\t' && *pos != '\n' &&
306 while (*pos == ' ' || *pos == '\t')
308 if (*pos != '\n' && *pos != '\0') {
309 lp = (struct linelist *)malloc(sizeof (*lp));
311 lp->l_groupname = (char *)malloc(len + 1);
312 bcopy(spos, lp->l_groupname, len);
313 *(lp->l_groupname + len) = '\0';
317 * Loop around handling line continuations.
320 if (*(pos + len - 1) == '\n')
322 if (*(pos + len - 1) == '\\') {
328 linep = (char *)malloc(olen + len + 1);
330 bcopy(olinep, linep, olen);
333 bcopy(pos, linep + olen, len);
335 *(linep + olen) = '\0';
340 if (fgets(line, LINSIZ, netf)) {
349 lp->l_next = linehead;
353 * If this is the one we wanted, we are done.
355 if (!strcmp(lp->l_groupname, group))
359 return ((struct linelist *)0);