]>
git.saurik.com Git - apple/network_cmds.git/blob - revnetgroup.tproj/parse_netgroup.c
b3a2a2eda5e9df9a2c8d937520f25a43b492259c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* $OpenBSD: parse_netgroup.c,v 1.2 1997/08/18 03:11:35 millert Exp $ */
25 * Copyright (c) 1992, 1993
26 * The Regents of the University of California. All rights reserved.
28 * This code is derived from software contributed to Berkeley by
29 * Rick Macklem at The University of Guelph.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * $FreeBSD: parse_netgroup.c,v 1.5 1997/02/22 14:22:02 peter Exp $
63 * This is a specially hacked-up version of getnetgrent.c used to parse
64 * data from the stored hash table of netgroup info rather than from a
65 * file. It's used mainly for the parse_netgroup() function. All the YP
66 * stuff and file support has been stripped out since it isn't needed.
76 static const char rcsid
[] = "$OpenBSD: parse_netgroup.c,v 1.2 1997/08/18 03:11:35 millert Exp $";
80 * Static Variables and functions used by setnetgrent(), getnetgrent() and
82 * There are two linked lists:
83 * - linelist is just used by setnetgrent() to parse the net group file via.
85 * - netgrp is the list of entries for the current netgroup
88 struct linelist
*l_next
; /* Chain ptr. */
89 int l_parsed
; /* Flag for cycles */
90 char *l_groupname
; /* Name of netgroup */
91 char *l_line
; /* Netgroup entrie(s) to be parsed */
95 struct netgrp
*ng_next
; /* Chain ptr */
96 char *ng_str
[3]; /* Field pointers, see below */
98 #define NG_HOST 0 /* Host name */
99 #define NG_USER 1 /* User name */
100 #define NG_DOM 2 /* and Domain name */
102 static struct linelist
*linehead
= (struct linelist
*)0;
103 static struct netgrp
*nextgrp
= (struct netgrp
*)0;
111 static int parse_netgrp();
112 static struct linelist
*read_for_group();
113 void __setnetgrent(), __endnetgrent();
115 extern struct group_entry
*gtable
[];
119 * Parse the netgroup file looking for the netgroup and build the list
120 * of netgrp structures. Let parse_netgrp() and read_for_group() do
129 if (group
== NULL
|| !strlen(group
))
132 if (grouphead
.gr
== (struct netgrp
*)0 ||
133 strcmp(group
, grouphead
.grname
)) {
135 if (parse_netgrp(group
))
138 grouphead
.grname
= (char *)
139 malloc(strlen(group
) + 1);
140 strcpy(grouphead
.grname
, group
);
143 nextgrp
= grouphead
.gr
;
147 * Get the next netgroup off the list.
150 __getnetgrent(hostp
, userp
, domp
)
151 char **hostp
, **userp
, **domp
;
154 *hostp
= nextgrp
->ng_str
[NG_HOST
];
155 *userp
= nextgrp
->ng_str
[NG_USER
];
156 *domp
= nextgrp
->ng_str
[NG_DOM
];
157 nextgrp
= nextgrp
->ng_next
;
164 * __endnetgrent() - cleanup
169 register struct linelist
*lp
, *olp
;
170 register struct netgrp
*gp
, *ogp
;
176 free(olp
->l_groupname
);
180 linehead
= (struct linelist
*)0;
181 if (grouphead
.grname
) {
182 free(grouphead
.grname
);
183 grouphead
.grname
= (char *)0;
189 if (ogp
->ng_str
[NG_HOST
])
190 free(ogp
->ng_str
[NG_HOST
]);
191 if (ogp
->ng_str
[NG_USER
])
192 free(ogp
->ng_str
[NG_USER
]);
193 if (ogp
->ng_str
[NG_DOM
])
194 free(ogp
->ng_str
[NG_DOM
]);
197 grouphead
.gr
= (struct netgrp
*)0;
201 * Parse the netgroup file setting up the linked lists.
207 register char *spos
, *epos
;
208 register int len
, strpos
;
214 struct linelist
*lp
= linehead
;
217 * First, see if the line has already been read in.
220 if (!strcmp(group
, lp
->l_groupname
))
224 if (lp
== (struct linelist
*)0 &&
225 (lp
= read_for_group(group
)) == (struct linelist
*)0)
230 * This error message is largely superflous since the
231 * code handles the error condition sucessfully, and
232 * spewing it out from inside libc can actually hose
235 fprintf(stderr
, "Cycle in netgroup %s\n", lp
->l_groupname
);
241 /* Watch for null pointer dereferences, dammit! */
242 while (pos
!= NULL
&& *pos
!= '\0') {
244 grp
= (struct netgrp
*)malloc(sizeof (struct netgrp
));
245 bzero((char *)grp
, sizeof (struct netgrp
));
246 grp
->ng_next
= grouphead
.gr
;
249 gpos
= strsep(&pos
, ")");
253 for (strpos
= 0; strpos
< 3; strpos
++) {
254 if ((spos
= strsep(&gpos
, ","))) {
258 while (*spos
== ' ' || *spos
== '\t')
260 if ((epos
= strpbrk(spos
, " \t"))) {
266 grp
->ng_str
[strpos
] = (char *)
268 bcopy(spos
, grp
->ng_str
[strpos
],
273 * All other systems I've tested
274 * return NULL for empty netgroup
275 * fields. It's up to user programs
276 * to handle the NULLs appropriately.
278 grp
->ng_str
[strpos
] = NULL
;
283 * Note: on other platforms, malformed netgroup
284 * entries are not normally flagged. While we
285 * can catch bad entries and report them, we should
286 * stay silent by default for compatibility's sake.
289 fprintf(stderr
, "Bad entry (%s%s%s%s%s) in netgroup \"%s\"\n",
290 grp
->ng_str
[NG_HOST
] == NULL
? "" : grp
->ng_str
[NG_HOST
],
291 grp
->ng_str
[NG_USER
] == NULL
? "" : ",",
292 grp
->ng_str
[NG_USER
] == NULL
? "" : grp
->ng_str
[NG_USER
],
293 grp
->ng_str
[NG_DOM
] == NULL
? "" : ",",
294 grp
->ng_str
[NG_DOM
] == NULL
? "" : grp
->ng_str
[NG_DOM
],
298 spos
= strsep(&pos
, ", \t");
299 if (parse_netgrp(spos
))
302 /* Watch for null pointer dereferences, dammit! */
304 while (*pos
== ' ' || *pos
== ',' || *pos
== '\t')
311 * Read the netgroup file and save lines until the line for the netgroup
312 * is found. Return 1 if eof is encountered.
314 static struct linelist
*
315 read_for_group(group
)
318 register char *pos
, *spos
, *linep
= NULL
, *olinep
= NULL
;
319 register int len
, olen
;
322 char line
[LINSIZ
+ 1];
325 data
= lookup (gtable
, group
);
326 sprintf(line
, "%s %s", group
, data
);
332 while (*pos
== ' ' || *pos
== '\t')
335 while (*pos
!= ' ' && *pos
!= '\t' && *pos
!= '\n' &&
339 while (*pos
== ' ' || *pos
== '\t')
341 if (*pos
!= '\n' && *pos
!= '\0') {
342 lp
= (struct linelist
*)malloc(sizeof (*lp
));
344 lp
->l_groupname
= (char *)malloc(len
+ 1);
345 bcopy(spos
, lp
->l_groupname
, len
);
346 *(lp
->l_groupname
+ len
) = '\0';
350 * Loop around handling line continuations.
353 if (*(pos
+ len
- 1) == '\n')
355 if (*(pos
+ len
- 1) == '\\') {
361 linep
= (char *)malloc(olen
+ len
+ 1);
363 bcopy(olinep
, linep
, olen
);
366 bcopy(pos
, linep
+ olen
, len
);
368 *(linep
+ olen
) = '\0';
373 if (fgets(line
, LINSIZ
, netf
)) {
382 lp
->l_next
= linehead
;
386 * If this is the one we wanted, we are done.
388 if (!strcmp(lp
->l_groupname
, group
))
392 return ((struct linelist
*)0);