parse_netgroup.c revision 1.2 1 1.2 lukem /* $NetBSD: parse_netgroup.c,v 1.2 1997/10/06 06:54:13 lukem 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.2 lukem __RCSID("$NetBSD: parse_netgroup.c,v 1.2 1997/10/06 06:54:13 lukem 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.2 lukem static int parse_netgrp __P((const char *));
96 1.2 lukem static struct linelist *read_for_group __P((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.2 lukem rng_setnetgrent(group)
107 1.2 lukem const char *group;
108 1.1 lukem {
109 1.1 lukem /* Sanity check */
110 1.1 lukem
111 1.1 lukem if (group == NULL || !strlen(group))
112 1.1 lukem return;
113 1.1 lukem
114 1.1 lukem if (grouphead.gr == (struct netgrp *)0 ||
115 1.1 lukem strcmp(group, grouphead.grname)) {
116 1.2 lukem rng_endnetgrent();
117 1.1 lukem if (parse_netgrp(group))
118 1.2 lukem rng_endnetgrent();
119 1.1 lukem else {
120 1.1 lukem grouphead.grname = (char *)
121 1.1 lukem malloc(strlen(group) + 1);
122 1.1 lukem strcpy(grouphead.grname, group);
123 1.1 lukem }
124 1.1 lukem }
125 1.1 lukem nextgrp = grouphead.gr;
126 1.1 lukem }
127 1.1 lukem
128 1.1 lukem /*
129 1.1 lukem * Get the next netgroup off the list.
130 1.1 lukem */
131 1.1 lukem int
132 1.2 lukem rng_getnetgrent(hostp, userp, domp)
133 1.1 lukem char **hostp, **userp, **domp;
134 1.1 lukem {
135 1.1 lukem if (nextgrp) {
136 1.1 lukem *hostp = nextgrp->ng_str[NG_HOST];
137 1.1 lukem *userp = nextgrp->ng_str[NG_USER];
138 1.1 lukem *domp = nextgrp->ng_str[NG_DOM];
139 1.1 lukem nextgrp = nextgrp->ng_next;
140 1.1 lukem return (1);
141 1.1 lukem }
142 1.1 lukem return (0);
143 1.1 lukem }
144 1.1 lukem
145 1.1 lukem /*
146 1.2 lukem * rng_endnetgrent() - cleanup
147 1.1 lukem */
148 1.1 lukem void
149 1.2 lukem rng_endnetgrent()
150 1.1 lukem {
151 1.2 lukem struct linelist *lp, *olp;
152 1.2 lukem struct netgrp *gp, *ogp;
153 1.1 lukem
154 1.1 lukem lp = linehead;
155 1.1 lukem while (lp) {
156 1.1 lukem olp = lp;
157 1.1 lukem lp = lp->l_next;
158 1.1 lukem free(olp->l_groupname);
159 1.1 lukem free(olp->l_line);
160 1.1 lukem free((char *)olp);
161 1.1 lukem }
162 1.1 lukem linehead = (struct linelist *)0;
163 1.1 lukem if (grouphead.grname) {
164 1.1 lukem free(grouphead.grname);
165 1.1 lukem grouphead.grname = (char *)0;
166 1.1 lukem }
167 1.1 lukem gp = grouphead.gr;
168 1.1 lukem while (gp) {
169 1.1 lukem ogp = gp;
170 1.1 lukem gp = gp->ng_next;
171 1.1 lukem if (ogp->ng_str[NG_HOST])
172 1.1 lukem free(ogp->ng_str[NG_HOST]);
173 1.1 lukem if (ogp->ng_str[NG_USER])
174 1.1 lukem free(ogp->ng_str[NG_USER]);
175 1.1 lukem if (ogp->ng_str[NG_DOM])
176 1.1 lukem free(ogp->ng_str[NG_DOM]);
177 1.1 lukem free((char *)ogp);
178 1.1 lukem }
179 1.1 lukem grouphead.gr = (struct netgrp *)0;
180 1.1 lukem }
181 1.1 lukem
182 1.1 lukem /*
183 1.1 lukem * Parse the netgroup file setting up the linked lists.
184 1.1 lukem */
185 1.1 lukem static int
186 1.1 lukem parse_netgrp(group)
187 1.2 lukem const char *group;
188 1.1 lukem {
189 1.2 lukem char *spos, *epos;
190 1.2 lukem int len, strpos;
191 1.1 lukem #ifdef DEBUG
192 1.2 lukem int fields;
193 1.1 lukem #endif
194 1.1 lukem char *pos, *gpos;
195 1.1 lukem struct netgrp *grp;
196 1.1 lukem struct linelist *lp = linehead;
197 1.1 lukem
198 1.1 lukem /*
199 1.1 lukem * First, see if the line has already been read in.
200 1.1 lukem */
201 1.1 lukem while (lp) {
202 1.1 lukem if (!strcmp(group, lp->l_groupname))
203 1.1 lukem break;
204 1.1 lukem lp = lp->l_next;
205 1.1 lukem }
206 1.1 lukem if (lp == (struct linelist *)0 &&
207 1.1 lukem (lp = read_for_group(group)) == (struct linelist *)0)
208 1.1 lukem return (1);
209 1.1 lukem if (lp->l_parsed) {
210 1.1 lukem #ifdef DEBUG
211 1.1 lukem /*
212 1.1 lukem * This error message is largely superflous since the
213 1.1 lukem * code handles the error condition sucessfully, and
214 1.1 lukem * spewing it out from inside libc can actually hose
215 1.1 lukem * certain programs.
216 1.1 lukem */
217 1.2 lukem warnx("Cycle in netgroup %s", lp->l_groupname);
218 1.1 lukem #endif
219 1.1 lukem return (1);
220 1.1 lukem } else
221 1.1 lukem lp->l_parsed = 1;
222 1.1 lukem pos = lp->l_line;
223 1.1 lukem /* Watch for null pointer dereferences, dammit! */
224 1.1 lukem while (pos != NULL && *pos != '\0') {
225 1.1 lukem if (*pos == '(') {
226 1.1 lukem grp = (struct netgrp *)malloc(sizeof (struct netgrp));
227 1.2 lukem memset((char *)grp, 0, sizeof (struct netgrp));
228 1.1 lukem grp->ng_next = grouphead.gr;
229 1.1 lukem grouphead.gr = grp;
230 1.1 lukem pos++;
231 1.1 lukem gpos = strsep(&pos, ")");
232 1.1 lukem #ifdef DEBUG
233 1.1 lukem fields = 0;
234 1.1 lukem #endif
235 1.1 lukem for (strpos = 0; strpos < 3; strpos++) {
236 1.1 lukem if ((spos = strsep(&gpos, ","))) {
237 1.1 lukem #ifdef DEBUG
238 1.1 lukem fields++;
239 1.1 lukem #endif
240 1.1 lukem while (*spos == ' ' || *spos == '\t')
241 1.1 lukem spos++;
242 1.1 lukem if ((epos = strpbrk(spos, " \t"))) {
243 1.1 lukem *epos = '\0';
244 1.1 lukem len = epos - spos;
245 1.1 lukem } else
246 1.1 lukem len = strlen(spos);
247 1.1 lukem if (len > 0) {
248 1.1 lukem grp->ng_str[strpos] = (char *)
249 1.1 lukem malloc(len + 1);
250 1.2 lukem memmove(grp->ng_str[strpos],
251 1.2 lukem spos, len + 1);
252 1.1 lukem }
253 1.1 lukem } else {
254 1.1 lukem /*
255 1.1 lukem * All other systems I've tested
256 1.1 lukem * return NULL for empty netgroup
257 1.1 lukem * fields. It's up to user programs
258 1.1 lukem * to handle the NULLs appropriately.
259 1.1 lukem */
260 1.1 lukem grp->ng_str[strpos] = NULL;
261 1.1 lukem }
262 1.1 lukem }
263 1.1 lukem #ifdef DEBUG
264 1.1 lukem /*
265 1.1 lukem * Note: on other platforms, malformed netgroup
266 1.1 lukem * entries are not normally flagged. While we
267 1.1 lukem * can catch bad entries and report them, we should
268 1.1 lukem * stay silent by default for compatibility's sake.
269 1.1 lukem */
270 1.1 lukem if (fields < 3)
271 1.2 lukem warnx(
272 1.2 lukem "Bad entry (%s%s%s%s%s) in netgroup \"%s\"",
273 1.2 lukem grp->ng_str[NG_HOST] == NULL ? "" :
274 1.2 lukem grp->ng_str[NG_HOST],
275 1.2 lukem grp->ng_str[NG_USER] == NULL ? "" : ",",
276 1.2 lukem grp->ng_str[NG_USER] == NULL ? "" :
277 1.2 lukem grp->ng_str[NG_USER],
278 1.2 lukem grp->ng_str[NG_DOM] == NULL ? "" : ",",
279 1.2 lukem grp->ng_str[NG_DOM] == NULL ? "" :
280 1.2 lukem grp->ng_str[NG_DOM],
281 1.2 lukem lp->l_groupname);
282 1.1 lukem #endif
283 1.1 lukem } else {
284 1.1 lukem spos = strsep(&pos, ", \t");
285 1.1 lukem if (parse_netgrp(spos))
286 1.1 lukem continue;
287 1.1 lukem }
288 1.1 lukem /* Watch for null pointer dereferences, dammit! */
289 1.1 lukem if (pos != NULL)
290 1.1 lukem while (*pos == ' ' || *pos == ',' || *pos == '\t')
291 1.1 lukem pos++;
292 1.1 lukem }
293 1.1 lukem return (0);
294 1.1 lukem }
295 1.1 lukem
296 1.1 lukem /*
297 1.1 lukem * Read the netgroup file and save lines until the line for the netgroup
298 1.1 lukem * is found. Return 1 if eof is encountered.
299 1.1 lukem */
300 1.1 lukem static struct linelist *
301 1.1 lukem read_for_group(group)
302 1.2 lukem const char *group;
303 1.1 lukem {
304 1.2 lukem char *pos, *spos, *linep = NULL, *olinep = NULL;
305 1.2 lukem int len, olen;
306 1.1 lukem int cont;
307 1.1 lukem struct linelist *lp;
308 1.1 lukem char line[LINSIZ + 1];
309 1.1 lukem char *data = NULL;
310 1.1 lukem
311 1.1 lukem data = lookup (gtable, group);
312 1.1 lukem sprintf(line, "%s %s", group, data);
313 1.1 lukem pos = (char *)&line;
314 1.1 lukem #ifdef CANT_HAPPEN
315 1.1 lukem if (*pos == '#')
316 1.1 lukem continue;
317 1.1 lukem #endif
318 1.1 lukem while (*pos == ' ' || *pos == '\t')
319 1.1 lukem pos++;
320 1.1 lukem spos = pos;
321 1.1 lukem while (*pos != ' ' && *pos != '\t' && *pos != '\n' &&
322 1.1 lukem *pos != '\0')
323 1.1 lukem pos++;
324 1.1 lukem len = pos - spos;
325 1.1 lukem while (*pos == ' ' || *pos == '\t')
326 1.1 lukem pos++;
327 1.1 lukem if (*pos != '\n' && *pos != '\0') {
328 1.1 lukem lp = (struct linelist *)malloc(sizeof (*lp));
329 1.1 lukem lp->l_parsed = 0;
330 1.1 lukem lp->l_groupname = (char *)malloc(len + 1);
331 1.2 lukem memmove(lp->l_groupname, spos, len);
332 1.1 lukem *(lp->l_groupname + len) = '\0';
333 1.1 lukem len = strlen(pos);
334 1.1 lukem olen = 0;
335 1.1 lukem /*
336 1.1 lukem * Loop around handling line continuations.
337 1.1 lukem */
338 1.1 lukem do {
339 1.1 lukem if (*(pos + len - 1) == '\n')
340 1.1 lukem len--;
341 1.1 lukem if (*(pos + len - 1) == '\\') {
342 1.1 lukem len--;
343 1.1 lukem cont = 1;
344 1.1 lukem } else
345 1.1 lukem cont = 0;
346 1.1 lukem if (len > 0) {
347 1.1 lukem linep = (char *)malloc(olen + len + 1);
348 1.1 lukem if (olen > 0) {
349 1.2 lukem memmove(linep, olinep, olen);
350 1.1 lukem free(olinep);
351 1.1 lukem }
352 1.2 lukem memmove(linep + olen, pos, len);
353 1.1 lukem olen += len;
354 1.1 lukem *(linep + olen) = '\0';
355 1.1 lukem olinep = linep;
356 1.1 lukem }
357 1.1 lukem #ifdef CANT_HAPPEN
358 1.1 lukem if (cont) {
359 1.1 lukem if (fgets(line, LINSIZ, netf)) {
360 1.1 lukem pos = line;
361 1.1 lukem len = strlen(pos);
362 1.1 lukem } else
363 1.1 lukem cont = 0;
364 1.1 lukem }
365 1.1 lukem #endif
366 1.1 lukem } while (cont);
367 1.1 lukem lp->l_line = linep;
368 1.1 lukem lp->l_next = linehead;
369 1.1 lukem linehead = lp;
370 1.1 lukem #ifdef CANT_HAPPEN
371 1.1 lukem /*
372 1.1 lukem * If this is the one we wanted, we are done.
373 1.1 lukem */
374 1.1 lukem if (!strcmp(lp->l_groupname, group))
375 1.1 lukem #endif
376 1.1 lukem return (lp);
377 1.1 lukem }
378 1.1 lukem return ((struct linelist *)0);
379 1.1 lukem }
380