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