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