]>
git.saurik.com Git - apple/network_cmds.git/blob - revnetgroup.tproj/parse_netgroup.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
24 /* $OpenBSD: parse_netgroup.c,v 1.2 1997/08/18 03:11:35 millert Exp $ */
26 * Copyright (c) 1992, 1993
27 * The Regents of the University of California. All rights reserved.
29 * This code is derived from software contributed to Berkeley by
30 * Rick Macklem at The University of Guelph.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
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.
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
60 * $FreeBSD: parse_netgroup.c,v 1.5 1997/02/22 14:22:02 peter Exp $
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.
77 static const char rcsid
[] = "$OpenBSD: parse_netgroup.c,v 1.2 1997/08/18 03:11:35 millert Exp $";
81 * Static Variables and functions used by setnetgrent(), getnetgrent() and
83 * There are two linked lists:
84 * - linelist is just used by setnetgrent() to parse the net group file via.
86 * - netgrp is the list of entries for the current netgroup
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 */
96 struct netgrp
*ng_next
; /* Chain ptr */
97 char *ng_str
[3]; /* Field pointers, see below */
99 #define NG_HOST 0 /* Host name */
100 #define NG_USER 1 /* User name */
101 #define NG_DOM 2 /* and Domain name */
103 static struct linelist
*linehead
= (struct linelist
*)0;
104 static struct netgrp
*nextgrp
= (struct netgrp
*)0;
112 static int parse_netgrp();
113 static struct linelist
*read_for_group();
114 void __setnetgrent(), __endnetgrent();
116 extern struct group_entry
*gtable
[];
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
130 if (group
== NULL
|| !strlen(group
))
133 if (grouphead
.gr
== (struct netgrp
*)0 ||
134 strcmp(group
, grouphead
.grname
)) {
136 if (parse_netgrp(group
))
139 grouphead
.grname
= (char *)
140 malloc(strlen(group
) + 1);
141 strcpy(grouphead
.grname
, group
);
144 nextgrp
= grouphead
.gr
;
148 * Get the next netgroup off the list.
151 __getnetgrent(hostp
, userp
, domp
)
152 char **hostp
, **userp
, **domp
;
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
;
165 * __endnetgrent() - cleanup
170 register struct linelist
*lp
, *olp
;
171 register struct netgrp
*gp
, *ogp
;
177 free(olp
->l_groupname
);
181 linehead
= (struct linelist
*)0;
182 if (grouphead
.grname
) {
183 free(grouphead
.grname
);
184 grouphead
.grname
= (char *)0;
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
]);
198 grouphead
.gr
= (struct netgrp
*)0;
202 * Parse the netgroup file setting up the linked lists.
208 register char *spos
, *epos
;
209 register int len
, strpos
;
215 struct linelist
*lp
= linehead
;
218 * First, see if the line has already been read in.
221 if (!strcmp(group
, lp
->l_groupname
))
225 if (lp
== (struct linelist
*)0 &&
226 (lp
= read_for_group(group
)) == (struct linelist
*)0)
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
236 fprintf(stderr
, "Cycle in netgroup %s\n", lp
->l_groupname
);
242 /* Watch for null pointer dereferences, dammit! */
243 while (pos
!= NULL
&& *pos
!= '\0') {
245 grp
= (struct netgrp
*)malloc(sizeof (struct netgrp
));
246 bzero((char *)grp
, sizeof (struct netgrp
));
247 grp
->ng_next
= grouphead
.gr
;
250 gpos
= strsep(&pos
, ")");
254 for (strpos
= 0; strpos
< 3; strpos
++) {
255 if ((spos
= strsep(&gpos
, ","))) {
259 while (*spos
== ' ' || *spos
== '\t')
261 if ((epos
= strpbrk(spos
, " \t"))) {
267 grp
->ng_str
[strpos
] = (char *)
269 bcopy(spos
, grp
->ng_str
[strpos
],
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.
279 grp
->ng_str
[strpos
] = NULL
;
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.
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
],
299 spos
= strsep(&pos
, ", \t");
300 if (parse_netgrp(spos
))
303 /* Watch for null pointer dereferences, dammit! */
305 while (*pos
== ' ' || *pos
== ',' || *pos
== '\t')
312 * Read the netgroup file and save lines until the line for the netgroup
313 * is found. Return 1 if eof is encountered.
315 static struct linelist
*
316 read_for_group(group
)
319 register char *pos
, *spos
, *linep
= NULL
, *olinep
= NULL
;
320 register int len
, olen
;
323 char line
[LINSIZ
+ 1];
326 data
= lookup (gtable
, group
);
327 sprintf(line
, "%s %s", group
, data
);
333 while (*pos
== ' ' || *pos
== '\t')
336 while (*pos
!= ' ' && *pos
!= '\t' && *pos
!= '\n' &&
340 while (*pos
== ' ' || *pos
== '\t')
342 if (*pos
!= '\n' && *pos
!= '\0') {
343 lp
= (struct linelist
*)malloc(sizeof (*lp
));
345 lp
->l_groupname
= (char *)malloc(len
+ 1);
346 bcopy(spos
, lp
->l_groupname
, len
);
347 *(lp
->l_groupname
+ len
) = '\0';
351 * Loop around handling line continuations.
354 if (*(pos
+ len
- 1) == '\n')
356 if (*(pos
+ len
- 1) == '\\') {
362 linep
= (char *)malloc(olen
+ len
+ 1);
364 bcopy(olinep
, linep
, olen
);
367 bcopy(pos
, linep
+ olen
, len
);
369 *(linep
+ olen
) = '\0';
374 if (fgets(line
, LINSIZ
, netf
)) {
383 lp
->l_next
= linehead
;
387 * If this is the one we wanted, we are done.
389 if (!strcmp(lp
->l_groupname
, group
))
393 return ((struct linelist
*)0);