parse_netgroup.c revision 1.3 1 1.3 wiz /* $NetBSD: parse_netgroup.c,v 1.3 2002/07/06 01:00:15 wiz Exp $ */
2 1.2 lukem
3 1.1 lukem /*
4 1.1 lukem * Copyright (c) 1992, 1993
5 1.1 lukem * The Regents of the University of California. All rights reserved.
6 1.1 lukem *
7 1.1 lukem * This code is derived from software contributed to Berkeley by
8 1.1 lukem * Rick Macklem at The University of Guelph.
9 1.1 lukem *
10 1.1 lukem * Redistribution and use in source and binary forms, with or without
11 1.1 lukem * modification, are permitted provided that the following conditions
12 1.1 lukem * are met:
13 1.1 lukem * 1. Redistributions of source code must retain the above copyright
14 1.1 lukem * notice, this list of conditions and the following disclaimer.
15 1.1 lukem * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 lukem * notice, this list of conditions and the following disclaimer in the
17 1.1 lukem * documentation and/or other materials provided with the distribution.
18 1.1 lukem * 3. All advertising materials mentioning features or use of this software
19 1.1 lukem * must display the following acknowledgement:
20 1.1 lukem * This product includes software developed by the University of
21 1.1 lukem * California, Berkeley and its contributors.
22 1.1 lukem * 4. Neither the name of the University nor the names of its contributors
23 1.1 lukem * may be used to endorse or promote products derived from this software
24 1.1 lukem * without specific prior written permission.
25 1.1 lukem *
26 1.1 lukem * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 lukem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 lukem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 lukem * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 lukem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 lukem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 lukem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 lukem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 lukem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 lukem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 lukem * SUCH DAMAGE.
37 1.1 lukem *
38 1.1 lukem */
39 1.1 lukem
40 1.2 lukem #include <sys/cdefs.h>
41 1.2 lukem #ifndef lint
42 1.3 wiz __RCSID("$NetBSD: parse_netgroup.c,v 1.3 2002/07/06 01:00:15 wiz Exp $");
43 1.2 lukem #endif
44 1.2 lukem
45 1.1 lukem /*
46 1.1 lukem * This is a specially hacked-up version of getnetgrent.c used to parse
47 1.1 lukem * data from the stored hash table of netgroup info rather than from a
48 1.1 lukem * file. It's used mainly for the parse_netgroup() function. All the YP
49 1.1 lukem * stuff and file support has been stripped out since it isn't needed.
50 1.1 lukem */
51 1.1 lukem
52 1.1 lukem #include <stdio.h>
53 1.2 lukem #include <stdlib.h>
54 1.1 lukem #include <string.h>
55 1.1 lukem #include <unistd.h>
56 1.2 lukem
57 1.1 lukem #include "hash.h"
58 1.1 lukem
59 1.1 lukem /*
60 1.2 lukem * Static Variables and functions used by rng_setnetgrent(), rng_getnetgrent()
61 1.2 lukem * and rng_endnetgrent().
62 1.2 lukem *
63 1.1 lukem * There are two linked lists:
64 1.1 lukem * - linelist is just used by setnetgrent() to parse the net group file via.
65 1.1 lukem * parse_netgrp()
66 1.1 lukem * - netgrp is the list of entries for the current netgroup
67 1.1 lukem */
68 1.1 lukem struct linelist {
69 1.1 lukem struct linelist *l_next; /* Chain ptr. */
70 1.1 lukem int l_parsed; /* Flag for cycles */
71 1.1 lukem char *l_groupname; /* Name of netgroup */
72 1.1 lukem char *l_line; /* Netgroup entrie(s) to be parsed */
73 1.1 lukem };
74 1.1 lukem
75 1.1 lukem struct netgrp {
76 1.1 lukem struct netgrp *ng_next; /* Chain ptr */
77 1.1 lukem char *ng_str[3]; /* Field pointers, see below */
78 1.1 lukem };
79 1.1 lukem #define NG_HOST 0 /* Host name */
80 1.1 lukem #define NG_USER 1 /* User name */
81 1.1 lukem #define NG_DOM 2 /* and Domain name */
82 1.1 lukem
83 1.1 lukem static struct linelist *linehead = (struct linelist *)0;
84 1.1 lukem static struct netgrp *nextgrp = (struct netgrp *)0;
85 1.1 lukem static struct {
86 1.1 lukem struct netgrp *gr;
87 1.1 lukem char *grname;
88 1.1 lukem } grouphead = {
89 1.1 lukem (struct netgrp *)0,
90 1.1 lukem (char *)0,
91 1.1 lukem };
92 1.2 lukem
93 1.1 lukem extern struct group_entry *gtable[];
94 1.1 lukem
95 1.3 wiz static int parse_netgrp(const char *);
96 1.3 wiz static struct linelist *read_for_group(const char *);
97 1.2 lukem
98 1.2 lukem
99 1.1 lukem /*
100 1.2 lukem * rng_setnetgrent()
101 1.1 lukem * Parse the netgroup file looking for the netgroup and build the list
102 1.1 lukem * of netgrp structures. Let parse_netgrp() and read_for_group() do
103 1.1 lukem * most of the work.
104 1.1 lukem */
105 1.1 lukem void
106 1.3 wiz rng_setnetgrent(const char *group)
107 1.1 lukem {
108 1.1 lukem /* Sanity check */
109 1.1 lukem
110 1.1 lukem if (group == NULL || !strlen(group))
111 1.1 lukem return;
112 1.1 lukem
113 1.1 lukem if (grouphead.gr == (struct netgrp *)0 ||
114 1.1 lukem strcmp(group, grouphead.grname)) {
115 1.2 lukem rng_endnetgrent();
116 1.1 lukem if (parse_netgrp(group))
117 1.2 lukem rng_endnetgrent();
118 1.1 lukem else {
119 1.1 lukem grouphead.grname = (char *)
120 1.1 lukem malloc(strlen(group) + 1);
121 1.1 lukem strcpy(grouphead.grname, group);
122 1.1 lukem }
123 1.1 lukem }
124 1.1 lukem nextgrp = grouphead.gr;
125 1.1 lukem }
126 1.1 lukem
127 1.1 lukem /*
128 1.1 lukem * Get the next netgroup off the list.
129 1.1 lukem */
130 1.1 lukem int
131 1.3 wiz rng_getnetgrent(char **hostp, char **userp, char **domp)
132 1.1 lukem {
133 1.1 lukem if (nextgrp) {
134 1.1 lukem *hostp = nextgrp->ng_str[NG_HOST];
135 1.1 lukem *userp = nextgrp->ng_str[NG_USER];
136 1.1 lukem *domp = nextgrp->ng_str[NG_DOM];
137 1.1 lukem nextgrp = nextgrp->ng_next;
138 1.1 lukem return (1);
139 1.1 lukem }
140 1.1 lukem return (0);
141 1.1 lukem }
142 1.1 lukem
143 1.1 lukem /*
144 1.2 lukem * rng_endnetgrent() - cleanup
145 1.1 lukem */
146 1.1 lukem void
147 1.3 wiz rng_endnetgrent(void)
148 1.1 lukem {
149 1.2 lukem struct linelist *lp, *olp;
150 1.2 lukem struct netgrp *gp, *ogp;
151 1.1 lukem
152 1.1 lukem lp = linehead;
153 1.1 lukem while (lp) {
154 1.1 lukem olp = lp;
155 1.1 lukem lp = lp->l_next;
156 1.1 lukem free(olp->l_groupname);
157 1.1 lukem free(olp->l_line);
158 1.1 lukem free((char *)olp);
159 1.1 lukem }
160 1.1 lukem linehead = (struct linelist *)0;
161 1.1 lukem if (grouphead.grname) {
162 1.1 lukem free(grouphead.grname);
163 1.1 lukem grouphead.grname = (char *)0;
164 1.1 lukem }
165 1.1 lukem gp = grouphead.gr;
166 1.1 lukem while (gp) {
167 1.1 lukem ogp = gp;
168 1.1 lukem gp = gp->ng_next;
169 1.1 lukem if (ogp->ng_str[NG_HOST])
170 1.1 lukem free(ogp->ng_str[NG_HOST]);
171 1.1 lukem if (ogp->ng_str[NG_USER])
172 1.1 lukem free(ogp->ng_str[NG_USER]);
173 1.1 lukem if (ogp->ng_str[NG_DOM])
174 1.1 lukem free(ogp->ng_str[NG_DOM]);
175 1.1 lukem free((char *)ogp);
176 1.1 lukem }
177 1.1 lukem grouphead.gr = (struct netgrp *)0;
178 1.1 lukem }
179 1.1 lukem
180 1.1 lukem /*
181 1.1 lukem * Parse the netgroup file setting up the linked lists.
182 1.1 lukem */
183 1.1 lukem static int
184 1.3 wiz parse_netgrp(const char *group)
185 1.1 lukem {
186 1.2 lukem char *spos, *epos;
187 1.2 lukem int len, strpos;
188 1.1 lukem #ifdef DEBUG
189 1.2 lukem int fields;
190 1.1 lukem #endif
191 1.1 lukem char *pos, *gpos;
192 1.1 lukem struct netgrp *grp;
193 1.1 lukem struct linelist *lp = linehead;
194 1.1 lukem
195 1.1 lukem /*
196 1.1 lukem * First, see if the line has already been read in.
197 1.1 lukem */
198 1.1 lukem while (lp) {
199 1.1 lukem if (!strcmp(group, lp->l_groupname))
200 1.1 lukem break;
201 1.1 lukem lp = lp->l_next;
202 1.1 lukem }
203 1.1 lukem if (lp == (struct linelist *)0 &&
204 1.1 lukem (lp = read_for_group(group)) == (struct linelist *)0)
205 1.1 lukem return (1);
206 1.1 lukem if (lp->l_parsed) {
207 1.1 lukem #ifdef DEBUG
208 1.1 lukem /*
209 1.1 lukem * This error message is largely superflous since the
210 1.1 lukem * code handles the error condition sucessfully, and
211 1.1 lukem * spewing it out from inside libc can actually hose
212 1.1 lukem * certain programs.
213 1.1 lukem */
214 1.2 lukem warnx("Cycle in netgroup %s", lp->l_groupname);
215 1.1 lukem #endif
216 1.1 lukem return (1);
217 1.1 lukem } else
218 1.1 lukem lp->l_parsed = 1;
219 1.1 lukem pos = lp->l_line;
220 1.1 lukem /* Watch for null pointer dereferences, dammit! */
221 1.1 lukem while (pos != NULL && *pos != '\0') {
222 1.1 lukem if (*pos == '(') {
223 1.1 lukem grp = (struct netgrp *)malloc(sizeof (struct netgrp));
224 1.2 lukem memset((char *)grp, 0, sizeof (struct netgrp));
225 1.1 lukem grp->ng_next = grouphead.gr;
226 1.1 lukem grouphead.gr = grp;
227 1.1 lukem pos++;
228 1.1 lukem gpos = strsep(&pos, ")");
229 1.1 lukem #ifdef DEBUG
230 1.1 lukem fields = 0;
231 1.1 lukem #endif
232 1.1 lukem for (strpos = 0; strpos < 3; strpos++) {
233 1.1 lukem if ((spos = strsep(&gpos, ","))) {
234 1.1 lukem #ifdef DEBUG
235 1.1 lukem fields++;
236 1.1 lukem #endif
237 1.1 lukem while (*spos == ' ' || *spos == '\t')
238 1.1 lukem spos++;
239 1.1 lukem if ((epos = strpbrk(spos, " \t"))) {
240 1.1 lukem *epos = '\0';
241 1.1 lukem len = epos - spos;
242 1.1 lukem } else
243 1.1 lukem len = strlen(spos);
244 1.1 lukem if (len > 0) {
245 1.1 lukem grp->ng_str[strpos] = (char *)
246 1.1 lukem malloc(len + 1);
247 1.2 lukem memmove(grp->ng_str[strpos],
248 1.2 lukem spos, len + 1);
249 1.1 lukem }
250 1.1 lukem } else {
251 1.1 lukem /*
252 1.1 lukem * All other systems I've tested
253 1.1 lukem * return NULL for empty netgroup
254 1.1 lukem * fields. It's up to user programs
255 1.1 lukem * to handle the NULLs appropriately.
256 1.1 lukem */
257 1.1 lukem grp->ng_str[strpos] = NULL;
258 1.1 lukem }
259 1.1 lukem }
260 1.1 lukem #ifdef DEBUG
261 1.1 lukem /*
262 1.1 lukem * Note: on other platforms, malformed netgroup
263 1.1 lukem * entries are not normally flagged. While we
264 1.1 lukem * can catch bad entries and report them, we should
265 1.1 lukem * stay silent by default for compatibility's sake.
266 1.1 lukem */
267 1.1 lukem if (fields < 3)
268 1.2 lukem warnx(
269 1.2 lukem "Bad entry (%s%s%s%s%s) in netgroup \"%s\"",
270 1.2 lukem grp->ng_str[NG_HOST] == NULL ? "" :
271 1.2 lukem grp->ng_str[NG_HOST],
272 1.2 lukem grp->ng_str[NG_USER] == NULL ? "" : ",",
273 1.2 lukem grp->ng_str[NG_USER] == NULL ? "" :
274 1.2 lukem grp->ng_str[NG_USER],
275 1.2 lukem grp->ng_str[NG_DOM] == NULL ? "" : ",",
276 1.2 lukem grp->ng_str[NG_DOM] == NULL ? "" :
277 1.2 lukem grp->ng_str[NG_DOM],
278 1.2 lukem lp->l_groupname);
279 1.1 lukem #endif
280 1.1 lukem } else {
281 1.1 lukem spos = strsep(&pos, ", \t");
282 1.1 lukem if (parse_netgrp(spos))
283 1.1 lukem continue;
284 1.1 lukem }
285 1.1 lukem /* Watch for null pointer dereferences, dammit! */
286 1.1 lukem if (pos != NULL)
287 1.1 lukem while (*pos == ' ' || *pos == ',' || *pos == '\t')
288 1.1 lukem pos++;
289 1.1 lukem }
290 1.1 lukem return (0);
291 1.1 lukem }
292 1.1 lukem
293 1.1 lukem /*
294 1.1 lukem * Read the netgroup file and save lines until the line for the netgroup
295 1.1 lukem * is found. Return 1 if eof is encountered.
296 1.1 lukem */
297 1.1 lukem static struct linelist *
298 1.3 wiz read_for_group(const char *group)
299 1.1 lukem {
300 1.2 lukem char *pos, *spos, *linep = NULL, *olinep = NULL;
301 1.2 lukem int len, olen;
302 1.1 lukem int cont;
303 1.1 lukem struct linelist *lp;
304 1.1 lukem char line[LINSIZ + 1];
305 1.1 lukem char *data = NULL;
306 1.1 lukem
307 1.1 lukem data = lookup (gtable, group);
308 1.1 lukem sprintf(line, "%s %s", group, data);
309 1.1 lukem pos = (char *)&line;
310 1.1 lukem #ifdef CANT_HAPPEN
311 1.1 lukem if (*pos == '#')
312 1.1 lukem continue;
313 1.1 lukem #endif
314 1.1 lukem while (*pos == ' ' || *pos == '\t')
315 1.1 lukem pos++;
316 1.1 lukem spos = pos;
317 1.1 lukem while (*pos != ' ' && *pos != '\t' && *pos != '\n' &&
318 1.1 lukem *pos != '\0')
319 1.1 lukem pos++;
320 1.1 lukem len = pos - spos;
321 1.1 lukem while (*pos == ' ' || *pos == '\t')
322 1.1 lukem pos++;
323 1.1 lukem if (*pos != '\n' && *pos != '\0') {
324 1.1 lukem lp = (struct linelist *)malloc(sizeof (*lp));
325 1.1 lukem lp->l_parsed = 0;
326 1.1 lukem lp->l_groupname = (char *)malloc(len + 1);
327 1.2 lukem memmove(lp->l_groupname, spos, len);
328 1.1 lukem *(lp->l_groupname + len) = '\0';
329 1.1 lukem len = strlen(pos);
330 1.1 lukem olen = 0;
331 1.1 lukem /*
332 1.1 lukem * Loop around handling line continuations.
333 1.1 lukem */
334 1.1 lukem do {
335 1.1 lukem if (*(pos + len - 1) == '\n')
336 1.1 lukem len--;
337 1.1 lukem if (*(pos + len - 1) == '\\') {
338 1.1 lukem len--;
339 1.1 lukem cont = 1;
340 1.1 lukem } else
341 1.1 lukem cont = 0;
342 1.1 lukem if (len > 0) {
343 1.1 lukem linep = (char *)malloc(olen + len + 1);
344 1.1 lukem if (olen > 0) {
345 1.2 lukem memmove(linep, olinep, olen);
346 1.1 lukem free(olinep);
347 1.1 lukem }
348 1.2 lukem memmove(linep + olen, pos, len);
349 1.1 lukem olen += len;
350 1.1 lukem *(linep + olen) = '\0';
351 1.1 lukem olinep = linep;
352 1.1 lukem }
353 1.1 lukem #ifdef CANT_HAPPEN
354 1.1 lukem if (cont) {
355 1.1 lukem if (fgets(line, LINSIZ, netf)) {
356 1.1 lukem pos = line;
357 1.1 lukem len = strlen(pos);
358 1.1 lukem } else
359 1.1 lukem cont = 0;
360 1.1 lukem }
361 1.1 lukem #endif
362 1.1 lukem } while (cont);
363 1.1 lukem lp->l_line = linep;
364 1.1 lukem lp->l_next = linehead;
365 1.1 lukem linehead = lp;
366 1.1 lukem #ifdef CANT_HAPPEN
367 1.1 lukem /*
368 1.1 lukem * If this is the one we wanted, we are done.
369 1.1 lukem */
370 1.1 lukem if (!strcmp(lp->l_groupname, group))
371 1.1 lukem #endif
372 1.1 lukem return (lp);
373 1.1 lukem }
374 1.1 lukem return ((struct linelist *)0);
375 1.1 lukem }
376