2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996-1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "port_before.h"
22 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <arpa/nameser.h>
33 #include <isc/memcluster.h>
35 #include "port_after.h"
42 #define NG_HOST 0 /*%< Host name */
43 #define NG_USER 1 /*%< User name */
44 #define NG_DOM 2 /*%< and Domain name */
45 #define LINSIZ 1024 /*%< Length of netgroup file line */
48 * This code is a hack-and-slash special. It realy needs to be
49 * rewritten with things like strdup, and realloc in mind.
50 * More reasonable data structures would not be a bad thing.
54 * Static Variables and functions used by setnetgrent(), getnetgrent() and
57 * There are two linked lists:
58 * \li linelist is just used by setnetgrent() to parse the net group file via.
60 * \li netgrp is the list of entries for the current netgroup
63 struct linelist
*l_next
; /*%< Chain ptr. */
64 int l_parsed
; /*%< Flag for cycles */
65 char * l_groupname
; /*%< Name of netgroup */
66 char * l_line
; /*%< Netgroup entrie(s) to be parsed */
69 struct ng_old_struct
{
70 struct ng_old_struct
*ng_next
; /*%< Chain ptr */
71 char * ng_str
[3]; /*%< Field pointers, see below */
76 struct linelist
*linehead
;
77 struct ng_old_struct
*nextgrp
;
79 struct ng_old_struct
*gr
;
86 static void ng_rewind(struct irs_ng
*, const char*);
87 static void ng_close(struct irs_ng
*);
88 static int ng_next(struct irs_ng
*, const char **,
89 const char **, const char **);
90 static int ng_test(struct irs_ng
*, const char *,
91 const char *, const char *,
93 static void ng_minimize(struct irs_ng
*);
95 static int parse_netgrp(struct irs_ng
*, const char*);
96 static struct linelist
*read_for_group(struct irs_ng
*, const char *);
97 static void freelists(struct irs_ng
*);
102 irs_lcl_ng(struct irs_acc
*this) {
108 if (!(ng
= memget(sizeof *ng
))) {
112 memset(ng
, 0x5e, sizeof *ng
);
113 if (!(pvt
= memget(sizeof *pvt
))) {
114 memput(ng
, sizeof *ng
);
118 memset(pvt
, 0, sizeof *pvt
);
120 ng
->close
= ng_close
;
123 ng
->rewind
= ng_rewind
;
124 ng
->minimize
= ng_minimize
;
131 ng_close(struct irs_ng
*this) {
132 struct pvt
*pvt
= (struct pvt
*)this->private;
137 memput(pvt
, sizeof *pvt
);
138 memput(this, sizeof *this);
142 * Parse the netgroup file looking for the netgroup and build the list
143 * of netgrp structures. Let parse_netgrp() and read_for_group() do
147 ng_rewind(struct irs_ng
*this, const char *group
) {
148 struct pvt
*pvt
= (struct pvt
*)this->private;
150 if (pvt
->fp
!= NULL
&& fseek(pvt
->fp
, SEEK_CUR
, 0L) == -1) {
155 if (pvt
->fp
== NULL
|| pvt
->grouphead
.gr
== NULL
||
156 strcmp(group
, pvt
->grouphead
.grname
)) {
160 pvt
->fp
= fopen(_PATH_NETGROUP
, "r");
161 if (pvt
->fp
!= NULL
) {
162 if (parse_netgrp(this, group
))
164 if (!(pvt
->grouphead
.grname
= strdup(group
)))
170 pvt
->nextgrp
= pvt
->grouphead
.gr
;
174 * Get the next netgroup off the list.
177 ng_next(struct irs_ng
*this, const char **host
, const char **user
,
180 struct pvt
*pvt
= (struct pvt
*)this->private;
183 *host
= pvt
->nextgrp
->ng_str
[NG_HOST
];
184 *user
= pvt
->nextgrp
->ng_str
[NG_USER
];
185 *domain
= pvt
->nextgrp
->ng_str
[NG_DOM
];
186 pvt
->nextgrp
= pvt
->nextgrp
->ng_next
;
193 * Search for a match in a netgroup.
196 ng_test(struct irs_ng
*this, const char *name
,
197 const char *host
, const char *user
, const char *domain
)
199 const char *ng_host
, *ng_user
, *ng_domain
;
201 ng_rewind(this, name
);
202 while (ng_next(this, &ng_host
, &ng_user
, &ng_domain
))
203 if ((host
== NULL
|| ng_host
== NULL
||
204 !strcmp(host
, ng_host
)) &&
205 (user
== NULL
|| ng_user
== NULL
||
206 !strcmp(user
, ng_user
)) &&
207 (domain
== NULL
|| ng_domain
== NULL
||
208 !strcmp(domain
, ng_domain
))) {
217 ng_minimize(struct irs_ng
*this) {
218 struct pvt
*pvt
= (struct pvt
*)this->private;
220 if (pvt
->fp
!= NULL
) {
221 (void)fclose(pvt
->fp
);
229 * endnetgrent() - cleanup
232 freelists(struct irs_ng
*this) {
233 struct pvt
*pvt
= (struct pvt
*)this->private;
234 struct linelist
*lp
, *olp
;
235 struct ng_old_struct
*gp
, *ogp
;
241 free(olp
->l_groupname
);
245 pvt
->linehead
= NULL
;
246 if (pvt
->grouphead
.grname
) {
247 free(pvt
->grouphead
.grname
);
248 pvt
->grouphead
.grname
= NULL
;
250 gp
= pvt
->grouphead
.gr
;
254 free(ogp
->ng_str
[NG_HOST
]);
255 free(ogp
->ng_str
[NG_USER
]);
256 free(ogp
->ng_str
[NG_DOM
]);
259 pvt
->grouphead
.gr
= NULL
;
263 * Parse the netgroup file setting up the linked lists.
266 parse_netgrp(struct irs_ng
*this, const char *group
) {
267 struct pvt
*pvt
= (struct pvt
*)this->private;
271 struct ng_old_struct
*grp
;
272 struct linelist
*lp
= pvt
->linehead
;
275 * First, see if the line has already been read in.
278 if (!strcmp(group
, lp
->l_groupname
))
283 (lp
= read_for_group(this, group
)) == NULL
)
286 /*fprintf(stderr, "Cycle in netgroup %s\n", lp->l_groupname);*/
291 while (*pos
!= '\0') {
293 if (!(grp
= malloc(sizeof (struct ng_old_struct
)))) {
298 memset(grp
, 0, sizeof (struct ng_old_struct
));
299 grp
->ng_next
= pvt
->grouphead
.gr
;
300 pvt
->grouphead
.gr
= grp
;
302 gpos
= strsep(&pos
, ")");
303 for (strpos
= 0; strpos
< 3; strpos
++) {
304 if ((spos
= strsep(&gpos
, ","))) {
305 while (*spos
== ' ' || *spos
== '\t')
307 if ((epos
= strpbrk(spos
, " \t"))) {
313 if(!(grp
->ng_str
[strpos
]
319 memcpy(grp
->ng_str
[strpos
],
327 spos
= strsep(&pos
, ", \t");
328 if (spos
!= NULL
&& parse_netgrp(this, spos
)) {
335 while (*pos
== ' ' || *pos
== ',' || *pos
== '\t')
340 /*fprintf(stderr, "Bad netgroup %s at ..%s\n", lp->l_groupname,
346 * Read the netgroup file and save lines until the line for the netgroup
347 * is found. Return 1 if eof is encountered.
349 static struct linelist
*
350 read_for_group(struct irs_ng
*this, const char *group
) {
351 struct pvt
*pvt
= (struct pvt
*)this->private;
352 char *pos
, *spos
, *linep
= NULL
, *olinep
;
355 char line
[LINSIZ
+ 1];
357 while (fgets(line
, LINSIZ
, pvt
->fp
) != NULL
) {
361 while (*pos
== ' ' || *pos
== '\t')
364 while (*pos
!= ' ' && *pos
!= '\t' && *pos
!= '\n' &&
368 while (*pos
== ' ' || *pos
== '\t')
370 if (*pos
!= '\n' && *pos
!= '\0') {
371 if (!(lp
= malloc(sizeof (*lp
)))) {
376 if (!(lp
->l_groupname
= malloc(len
+ 1))) {
381 memcpy(lp
->l_groupname
, spos
, len
);
382 *(lp
->l_groupname
+ len
) = '\0';
388 * Loop around handling line continuations.
391 if (*(pos
+ len
- 1) == '\n')
393 if (*(pos
+ len
- 1) == '\\') {
399 if (!(linep
= malloc(olen
+ len
+ 1))){
402 free(lp
->l_groupname
);
409 memcpy(linep
, olinep
, olen
);
412 memcpy(linep
+ olen
, pos
, len
);
414 *(linep
+ olen
) = '\0';
418 if (fgets(line
, LINSIZ
, pvt
->fp
)) {
426 lp
->l_next
= pvt
->linehead
;
430 * If this is the one we wanted, we are done.
432 if (!strcmp(lp
->l_groupname
, group
))