]>
git.saurik.com Git - apple/network_cmds.git/blob - revnetgroup.tproj/parse_netgroup.c
c9dab0f35e248299d35f18599263016198d51faf
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* $OpenBSD: parse_netgroup.c,v 1.2 1997/08/18 03:11:35 millert Exp $ */
27 * Copyright (c) 1992, 1993
28 * The Regents of the University of California. All rights reserved.
30 * This code is derived from software contributed to Berkeley by
31 * Rick Macklem at The University of Guelph.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * $FreeBSD: parse_netgroup.c,v 1.5 1997/02/22 14:22:02 peter Exp $
65 * This is a specially hacked-up version of getnetgrent.c used to parse
66 * data from the stored hash table of netgroup info rather than from a
67 * file. It's used mainly for the parse_netgroup() function. All the YP
68 * stuff and file support has been stripped out since it isn't needed.
78 static const char rcsid
[] = "$OpenBSD: parse_netgroup.c,v 1.2 1997/08/18 03:11:35 millert Exp $";
82 * Static Variables and functions used by setnetgrent(), getnetgrent() and
84 * There are two linked lists:
85 * - linelist is just used by setnetgrent() to parse the net group file via.
87 * - netgrp is the list of entries for the current netgroup
90 struct linelist
*l_next
; /* Chain ptr. */
91 int l_parsed
; /* Flag for cycles */
92 char *l_groupname
; /* Name of netgroup */
93 char *l_line
; /* Netgroup entrie(s) to be parsed */
97 struct netgrp
*ng_next
; /* Chain ptr */
98 char *ng_str
[3]; /* Field pointers, see below */
100 #define NG_HOST 0 /* Host name */
101 #define NG_USER 1 /* User name */
102 #define NG_DOM 2 /* and Domain name */
104 static struct linelist
*linehead
= (struct linelist
*)0;
105 static struct netgrp
*nextgrp
= (struct netgrp
*)0;
113 static int parse_netgrp();
114 static struct linelist
*read_for_group();
115 void __setnetgrent(), __endnetgrent();
117 extern struct group_entry
*gtable
[];
121 * Parse the netgroup file looking for the netgroup and build the list
122 * of netgrp structures. Let parse_netgrp() and read_for_group() do
131 if (group
== NULL
|| !strlen(group
))
134 if (grouphead
.gr
== (struct netgrp
*)0 ||
135 strcmp(group
, grouphead
.grname
)) {
137 if (parse_netgrp(group
))
140 grouphead
.grname
= (char *)
141 malloc(strlen(group
) + 1);
142 strcpy(grouphead
.grname
, group
);
145 nextgrp
= grouphead
.gr
;
149 * Get the next netgroup off the list.
152 __getnetgrent(hostp
, userp
, domp
)
153 char **hostp
, **userp
, **domp
;
156 *hostp
= nextgrp
->ng_str
[NG_HOST
];
157 *userp
= nextgrp
->ng_str
[NG_USER
];
158 *domp
= nextgrp
->ng_str
[NG_DOM
];
159 nextgrp
= nextgrp
->ng_next
;
166 * __endnetgrent() - cleanup
171 register struct linelist
*lp
, *olp
;
172 register struct netgrp
*gp
, *ogp
;
178 free(olp
->l_groupname
);
182 linehead
= (struct linelist
*)0;
183 if (grouphead
.grname
) {
184 free(grouphead
.grname
);
185 grouphead
.grname
= (char *)0;
191 if (ogp
->ng_str
[NG_HOST
])
192 free(ogp
->ng_str
[NG_HOST
]);
193 if (ogp
->ng_str
[NG_USER
])
194 free(ogp
->ng_str
[NG_USER
]);
195 if (ogp
->ng_str
[NG_DOM
])
196 free(ogp
->ng_str
[NG_DOM
]);
199 grouphead
.gr
= (struct netgrp
*)0;
203 * Parse the netgroup file setting up the linked lists.
209 register char *spos
, *epos
;
210 register int len
, strpos
;
216 struct linelist
*lp
= linehead
;
219 * First, see if the line has already been read in.
222 if (!strcmp(group
, lp
->l_groupname
))
226 if (lp
== (struct linelist
*)0 &&
227 (lp
= read_for_group(group
)) == (struct linelist
*)0)
232 * This error message is largely superflous since the
233 * code handles the error condition sucessfully, and
234 * spewing it out from inside libc can actually hose
237 fprintf(stderr
, "Cycle in netgroup %s\n", lp
->l_groupname
);
243 /* Watch for null pointer dereferences, dammit! */
244 while (pos
!= NULL
&& *pos
!= '\0') {
246 grp
= (struct netgrp
*)malloc(sizeof (struct netgrp
));
247 bzero((char *)grp
, sizeof (struct netgrp
));
248 grp
->ng_next
= grouphead
.gr
;
251 gpos
= strsep(&pos
, ")");
255 for (strpos
= 0; strpos
< 3; strpos
++) {
256 if ((spos
= strsep(&gpos
, ","))) {
260 while (*spos
== ' ' || *spos
== '\t')
262 if ((epos
= strpbrk(spos
, " \t"))) {
268 grp
->ng_str
[strpos
] = (char *)
270 bcopy(spos
, grp
->ng_str
[strpos
],
275 * All other systems I've tested
276 * return NULL for empty netgroup
277 * fields. It's up to user programs
278 * to handle the NULLs appropriately.
280 grp
->ng_str
[strpos
] = NULL
;
285 * Note: on other platforms, malformed netgroup
286 * entries are not normally flagged. While we
287 * can catch bad entries and report them, we should
288 * stay silent by default for compatibility's sake.
291 fprintf(stderr
, "Bad entry (%s%s%s%s%s) in netgroup \"%s\"\n",
292 grp
->ng_str
[NG_HOST
] == NULL
? "" : grp
->ng_str
[NG_HOST
],
293 grp
->ng_str
[NG_USER
] == NULL
? "" : ",",
294 grp
->ng_str
[NG_USER
] == NULL
? "" : grp
->ng_str
[NG_USER
],
295 grp
->ng_str
[NG_DOM
] == NULL
? "" : ",",
296 grp
->ng_str
[NG_DOM
] == NULL
? "" : grp
->ng_str
[NG_DOM
],
300 spos
= strsep(&pos
, ", \t");
301 if (parse_netgrp(spos
))
304 /* Watch for null pointer dereferences, dammit! */
306 while (*pos
== ' ' || *pos
== ',' || *pos
== '\t')
313 * Read the netgroup file and save lines until the line for the netgroup
314 * is found. Return 1 if eof is encountered.
316 static struct linelist
*
317 read_for_group(group
)
320 register char *pos
, *spos
, *linep
= NULL
, *olinep
= NULL
;
321 register int len
, olen
;
324 char line
[LINSIZ
+ 1];
327 data
= lookup (gtable
, group
);
328 sprintf(line
, "%s %s", group
, data
);
334 while (*pos
== ' ' || *pos
== '\t')
337 while (*pos
!= ' ' && *pos
!= '\t' && *pos
!= '\n' &&
341 while (*pos
== ' ' || *pos
== '\t')
343 if (*pos
!= '\n' && *pos
!= '\0') {
344 lp
= (struct linelist
*)malloc(sizeof (*lp
));
346 lp
->l_groupname
= (char *)malloc(len
+ 1);
347 bcopy(spos
, lp
->l_groupname
, len
);
348 *(lp
->l_groupname
+ len
) = '\0';
352 * Loop around handling line continuations.
355 if (*(pos
+ len
- 1) == '\n')
357 if (*(pos
+ len
- 1) == '\\') {
363 linep
= (char *)malloc(olen
+ len
+ 1);
365 bcopy(olinep
, linep
, olen
);
368 bcopy(pos
, linep
+ olen
, len
);
370 *(linep
+ olen
) = '\0';
375 if (fgets(line
, LINSIZ
, netf
)) {
384 lp
->l_next
= linehead
;
388 * If this is the one we wanted, we are done.
390 if (!strcmp(lp
->l_groupname
, group
))
394 return ((struct linelist
*)0);