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/libexec/revnetgroup/parse_netgroup.c,v 1.7 1999/08/28 00:09:48 peter Exp $
37 * $DragonFly: src/libexec/revnetgroup/parse_netgroup.c,v 1.3 2004/08/25 01:53:39 dillon Exp $
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.
54 * Static Variables and functions used by setnetgrent(), getnetgrent() and
56 * There are two linked lists:
57 * - linelist is just used by setnetgrent() to parse the net group file via.
59 * - netgrp is the list of entries for the current netgroup
62 struct linelist *l_next; /* Chain ptr. */
63 int l_parsed; /* Flag for cycles */
64 char *l_groupname; /* Name of netgroup */
65 char *l_line; /* Netgroup entrie(s) to be parsed */
69 struct netgrp *ng_next; /* Chain ptr */
70 char *ng_str[3]; /* Field pointers, see below */
72 #define NG_HOST 0 /* Host name */
73 #define NG_USER 1 /* User name */
74 #define NG_DOM 2 /* and Domain name */
76 static struct linelist *linehead = (struct linelist *)0;
77 static struct netgrp *nextgrp = (struct netgrp *)0;
85 static int parse_netgrp();
86 static struct linelist *read_for_group();
87 void __setnetgrent(), __endnetgrent();
89 extern struct group_entry *gtable[];
93 * Parse the netgroup file looking for the netgroup and build the list
94 * of netgrp structures. Let parse_netgrp() and read_for_group() do
103 if (group == NULL || !strlen(group))
106 if (grouphead.gr == (struct netgrp *)0 ||
107 strcmp(group, grouphead.grname)) {
109 if (parse_netgrp(group))
112 grouphead.grname = (char *)
113 malloc(strlen(group) + 1);
114 strcpy(grouphead.grname, group);
117 nextgrp = grouphead.gr;
121 * Get the next netgroup off the list.
124 __getnetgrent(hostp, userp, domp)
125 char **hostp, **userp, **domp;
128 *hostp = nextgrp->ng_str[NG_HOST];
129 *userp = nextgrp->ng_str[NG_USER];
130 *domp = nextgrp->ng_str[NG_DOM];
131 nextgrp = nextgrp->ng_next;
138 * __endnetgrent() - cleanup
143 register struct linelist *lp, *olp;
144 register struct netgrp *gp, *ogp;
150 free(olp->l_groupname);
154 linehead = (struct linelist *)0;
155 if (grouphead.grname) {
156 free(grouphead.grname);
157 grouphead.grname = (char *)0;
163 if (ogp->ng_str[NG_HOST])
164 free(ogp->ng_str[NG_HOST]);
165 if (ogp->ng_str[NG_USER])
166 free(ogp->ng_str[NG_USER]);
167 if (ogp->ng_str[NG_DOM])
168 free(ogp->ng_str[NG_DOM]);
171 grouphead.gr = (struct netgrp *)0;
175 * Parse the netgroup file setting up the linked lists.
181 register char *spos, *epos;
182 register int len, strpos;
188 struct linelist *lp = linehead;
191 * First, see if the line has already been read in.
194 if (!strcmp(group, lp->l_groupname))
198 if (lp == (struct linelist *)0 &&
199 (lp = read_for_group(group)) == (struct linelist *)0)
204 * This error message is largely superfluous since the
205 * code handles the error condition successfully, and
206 * spewing it out from inside libc can actually hose
209 warnx("cycle in netgroup %s", lp->l_groupname);
215 /* Watch for null pointer dereferences, dammit! */
216 while (pos != NULL && *pos != '\0') {
218 grp = (struct netgrp *)malloc(sizeof (struct netgrp));
219 bzero((char *)grp, sizeof (struct netgrp));
220 grp->ng_next = grouphead.gr;
223 gpos = strsep(&pos, ")");
227 for (strpos = 0; strpos < 3; strpos++) {
228 if ((spos = strsep(&gpos, ","))) {
232 while (*spos == ' ' || *spos == '\t')
234 if ((epos = strpbrk(spos, " \t"))) {
240 grp->ng_str[strpos] = (char *)
242 bcopy(spos, grp->ng_str[strpos],
247 * All other systems I've tested
248 * return NULL for empty netgroup
249 * fields. It's up to user programs
250 * to handle the NULLs appropriately.
252 grp->ng_str[strpos] = NULL;
257 * Note: on other platforms, malformed netgroup
258 * entries are not normally flagged. While we
259 * can catch bad entries and report them, we should
260 * stay silent by default for compatibility's sake.
263 warnx("bad entry (%s%s%s%s%s) in netgroup \"%s\"",
264 grp->ng_str[NG_HOST] == NULL ? "" : grp->ng_str[NG_HOST],
265 grp->ng_str[NG_USER] == NULL ? "" : ",",
266 grp->ng_str[NG_USER] == NULL ? "" : grp->ng_str[NG_USER],
267 grp->ng_str[NG_DOM] == NULL ? "" : ",",
268 grp->ng_str[NG_DOM] == NULL ? "" : grp->ng_str[NG_DOM],
272 spos = strsep(&pos, ", \t");
273 if (parse_netgrp(spos))
276 /* Watch for null pointer dereferences, dammit! */
278 while (*pos == ' ' || *pos == ',' || *pos == '\t')
285 * Read the netgroup file and save lines until the line for the netgroup
286 * is found. Return 1 if eof is encountered.
288 static struct linelist *
289 read_for_group(group)
292 register char *pos, *spos, *linep = NULL, *olinep = NULL;
293 register int len, olen;
296 char line[LINSIZ + 1];
299 data = lookup (gtable, group);
300 sprintf(line, "%s %s", group, data);
306 while (*pos == ' ' || *pos == '\t')
309 while (*pos != ' ' && *pos != '\t' && *pos != '\n' &&
313 while (*pos == ' ' || *pos == '\t')
315 if (*pos != '\n' && *pos != '\0') {
316 lp = (struct linelist *)malloc(sizeof (*lp));
318 lp->l_groupname = (char *)malloc(len + 1);
319 bcopy(spos, lp->l_groupname, len);
320 *(lp->l_groupname + len) = '\0';
324 * Loop around handling line continuations.
327 if (*(pos + len - 1) == '\n')
329 if (*(pos + len - 1) == '\\') {
335 linep = (char *)malloc(olen + len + 1);
337 bcopy(olinep, linep, olen);
340 bcopy(pos, linep + olen, len);
342 *(linep + olen) = '\0';
347 if (fgets(line, LINSIZ, netf)) {
356 lp->l_next = linehead;
360 * If this is the one we wanted, we are done.
362 if (!strcmp(lp->l_groupname, group))
366 return ((struct linelist *)0);