2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $FreeBSD: src/libexec/revnetgroup/parse_netgroup.c,v 1.7 1999/08/28 00:09:48 peter Exp $
33 * $DragonFly: src/libexec/revnetgroup/parse_netgroup.c,v 1.4 2008/11/19 17:46:55 swildner Exp $
37 * This is a specially hacked-up version of getnetgrent.c used to parse
38 * data from the stored hash table of netgroup info rather than from a
39 * file. It's used mainly for the parse_netgroup() function. All the YP
40 * stuff and file support has been stripped out since it isn't needed.
50 * Static Variables and functions used by setnetgrent(), getnetgrent() and
52 * There are two linked lists:
53 * - linelist is just used by setnetgrent() to parse the net group file via.
55 * - netgrp is the list of entries for the current netgroup
58 struct linelist
*l_next
; /* Chain ptr. */
59 int l_parsed
; /* Flag for cycles */
60 char *l_groupname
; /* Name of netgroup */
61 char *l_line
; /* Netgroup entrie(s) to be parsed */
65 struct netgrp
*ng_next
; /* Chain ptr */
66 char *ng_str
[3]; /* Field pointers, see below */
68 #define NG_HOST 0 /* Host name */
69 #define NG_USER 1 /* User name */
70 #define NG_DOM 2 /* and Domain name */
72 static struct linelist
*linehead
= NULL
;
73 static struct netgrp
*nextgrp
= NULL
;
81 static int parse_netgrp(char *);
82 static struct linelist
*read_for_group(char *);
83 extern struct group_entry
*gtable
[];
87 * Parse the netgroup file looking for the netgroup and build the list
88 * of netgrp structures. Let parse_netgrp() and read_for_group() do
92 __setnetgrent(char *group
)
96 if (group
== NULL
|| !strlen(group
))
99 if (grouphead
.gr
== NULL
||
100 strcmp(group
, grouphead
.grname
)) {
102 if (parse_netgrp(group
))
105 grouphead
.grname
= (char *)
106 malloc(strlen(group
) + 1);
107 strcpy(grouphead
.grname
, group
);
110 nextgrp
= grouphead
.gr
;
114 * Get the next netgroup off the list.
117 __getnetgrent(char **hostp
, char **userp
, char **domp
)
120 *hostp
= nextgrp
->ng_str
[NG_HOST
];
121 *userp
= nextgrp
->ng_str
[NG_USER
];
122 *domp
= nextgrp
->ng_str
[NG_DOM
];
123 nextgrp
= nextgrp
->ng_next
;
130 * __endnetgrent() - cleanup
135 struct linelist
*lp
, *olp
;
136 struct netgrp
*gp
, *ogp
;
142 free(olp
->l_groupname
);
147 if (grouphead
.grname
) {
148 free(grouphead
.grname
);
149 grouphead
.grname
= NULL
;
155 if (ogp
->ng_str
[NG_HOST
])
156 free(ogp
->ng_str
[NG_HOST
]);
157 if (ogp
->ng_str
[NG_USER
])
158 free(ogp
->ng_str
[NG_USER
]);
159 if (ogp
->ng_str
[NG_DOM
])
160 free(ogp
->ng_str
[NG_DOM
]);
167 * Parse the netgroup file setting up the linked lists.
170 parse_netgrp(char *group
)
179 struct linelist
*lp
= linehead
;
182 * First, see if the line has already been read in.
185 if (!strcmp(group
, lp
->l_groupname
))
190 (lp
= read_for_group(group
)) == NULL
)
195 * This error message is largely superfluous since the
196 * code handles the error condition successfully, and
197 * spewing it out from inside libc can actually hose
200 warnx("cycle in netgroup %s", lp
->l_groupname
);
206 /* Watch for null pointer dereferences, dammit! */
207 while (pos
!= NULL
&& *pos
!= '\0') {
209 grp
= (struct netgrp
*)malloc(sizeof (struct netgrp
));
210 bzero((char *)grp
, sizeof (struct netgrp
));
211 grp
->ng_next
= grouphead
.gr
;
214 gpos
= strsep(&pos
, ")");
218 for (strpos
= 0; strpos
< 3; strpos
++) {
219 if ((spos
= strsep(&gpos
, ","))) {
223 while (*spos
== ' ' || *spos
== '\t')
225 if ((epos
= strpbrk(spos
, " \t"))) {
231 grp
->ng_str
[strpos
] = (char *)
233 bcopy(spos
, grp
->ng_str
[strpos
],
238 * All other systems I've tested
239 * return NULL for empty netgroup
240 * fields. It's up to user programs
241 * to handle the NULLs appropriately.
243 grp
->ng_str
[strpos
] = NULL
;
248 * Note: on other platforms, malformed netgroup
249 * entries are not normally flagged. While we
250 * can catch bad entries and report them, we should
251 * stay silent by default for compatibility's sake.
254 warnx("bad entry (%s%s%s%s%s) in netgroup \"%s\"",
255 grp
->ng_str
[NG_HOST
] == NULL
? "" : grp
->ng_str
[NG_HOST
],
256 grp
->ng_str
[NG_USER
] == NULL
? "" : ",",
257 grp
->ng_str
[NG_USER
] == NULL
? "" : grp
->ng_str
[NG_USER
],
258 grp
->ng_str
[NG_DOM
] == NULL
? "" : ",",
259 grp
->ng_str
[NG_DOM
] == NULL
? "" : grp
->ng_str
[NG_DOM
],
263 spos
= strsep(&pos
, ", \t");
264 if (parse_netgrp(spos
))
267 /* Watch for null pointer dereferences, dammit! */
269 while (*pos
== ' ' || *pos
== ',' || *pos
== '\t')
276 * Read the netgroup file and save lines until the line for the netgroup
277 * is found. Return 1 if eof is encountered.
279 static struct linelist
*
280 read_for_group(char *group
)
282 char *pos
, *spos
, *linep
= NULL
, *olinep
= NULL
;
286 char line
[LINSIZ
+ 1];
289 data
= lookup (gtable
, group
);
290 sprintf(line
, "%s %s", group
, data
);
296 while (*pos
== ' ' || *pos
== '\t')
299 while (*pos
!= ' ' && *pos
!= '\t' && *pos
!= '\n' &&
303 while (*pos
== ' ' || *pos
== '\t')
305 if (*pos
!= '\n' && *pos
!= '\0') {
306 lp
= (struct linelist
*)malloc(sizeof (*lp
));
308 lp
->l_groupname
= (char *)malloc(len
+ 1);
309 bcopy(spos
, lp
->l_groupname
, len
);
310 *(lp
->l_groupname
+ len
) = '\0';
314 * Loop around handling line continuations.
317 if (*(pos
+ len
- 1) == '\n')
319 if (*(pos
+ len
- 1) == '\\') {
325 linep
= (char *)malloc(olen
+ len
+ 1);
327 bcopy(olinep
, linep
, olen
);
330 bcopy(pos
, linep
+ olen
, len
);
332 *(linep
+ olen
) = '\0';
337 if (fgets(line
, LINSIZ
, netf
)) {
346 lp
->l_next
= linehead
;
350 * If this is the one we wanted, we are done.
352 if (!strcmp(lp
->l_groupname
, group
))