1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 1997-2002 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate /*
7*0Sstevel@tonic-gate  * Copyright (c) 1996,1999 by Internet Software Consortium.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
10*0Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
11*0Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
12*0Sstevel@tonic-gate  *
13*0Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
14*0Sstevel@tonic-gate  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15*0Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
16*0Sstevel@tonic-gate  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
17*0Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
18*0Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19*0Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20*0Sstevel@tonic-gate  * SOFTWARE.
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate 
23*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate #if !defined(LINT) && !defined(CODECENTER)
26*0Sstevel@tonic-gate static const char rcsid[] = "$Id: gen_ng.c,v 1.15 2001/05/29 05:48:38 marka Exp $";
27*0Sstevel@tonic-gate #endif
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /* Imports */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #include "port_before.h"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <sys/types.h>
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include <netinet/in.h>
36*0Sstevel@tonic-gate #include <arpa/nameser.h>
37*0Sstevel@tonic-gate #include <resolv.h>
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #include <errno.h>
40*0Sstevel@tonic-gate #include <stdlib.h>
41*0Sstevel@tonic-gate #include <string.h>
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #include <isc/memcluster.h>
44*0Sstevel@tonic-gate #include <irs.h>
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate #include "port_after.h"
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #include "irs_p.h"
49*0Sstevel@tonic-gate #include "gen_p.h"
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate /* Types */
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate struct pvt {
54*0Sstevel@tonic-gate 	struct irs_rule *	rules;
55*0Sstevel@tonic-gate 	struct irs_rule *	rule;
56*0Sstevel@tonic-gate 	char *			curgroup;
57*0Sstevel@tonic-gate };
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate /* Forward */
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate static void		ng_close(struct irs_ng *);
62*0Sstevel@tonic-gate static int		ng_next(struct irs_ng *, const char **,
63*0Sstevel@tonic-gate 				const char **, const char **);
64*0Sstevel@tonic-gate static int 		ng_test(struct irs_ng *, const char *,
65*0Sstevel@tonic-gate 				const char *, const char *,
66*0Sstevel@tonic-gate 				const char *);
67*0Sstevel@tonic-gate static void 		ng_rewind(struct irs_ng *, const char *);
68*0Sstevel@tonic-gate static void		ng_minimize(struct irs_ng *);
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate /* Public */
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate struct irs_ng *
73*0Sstevel@tonic-gate irs_gen_ng(struct irs_acc *this) {
74*0Sstevel@tonic-gate 	struct gen_p *accpvt = (struct gen_p *)this->private;
75*0Sstevel@tonic-gate 	struct irs_ng *ng;
76*0Sstevel@tonic-gate 	struct pvt *pvt;
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 	if (!(ng = memget(sizeof *ng))) {
79*0Sstevel@tonic-gate 		errno = ENOMEM;
80*0Sstevel@tonic-gate 		return (NULL);
81*0Sstevel@tonic-gate 	}
82*0Sstevel@tonic-gate 	memset(ng, 0x5e, sizeof *ng);
83*0Sstevel@tonic-gate 	if (!(pvt = memget(sizeof *pvt))) {
84*0Sstevel@tonic-gate 		memput(ng, sizeof *ng);
85*0Sstevel@tonic-gate 		errno = ENOMEM;
86*0Sstevel@tonic-gate 		return (NULL);
87*0Sstevel@tonic-gate 	}
88*0Sstevel@tonic-gate 	memset(pvt, 0, sizeof *pvt);
89*0Sstevel@tonic-gate 	pvt->rules = accpvt->map_rules[irs_ng];
90*0Sstevel@tonic-gate 	pvt->rule = pvt->rules;
91*0Sstevel@tonic-gate 	ng->private = pvt;
92*0Sstevel@tonic-gate 	ng->close = ng_close;
93*0Sstevel@tonic-gate 	ng->next = ng_next;
94*0Sstevel@tonic-gate 	ng->test = ng_test;
95*0Sstevel@tonic-gate 	ng->rewind = ng_rewind;
96*0Sstevel@tonic-gate 	ng->minimize = ng_minimize;
97*0Sstevel@tonic-gate 	return (ng);
98*0Sstevel@tonic-gate }
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate /* Methods */
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate static void
103*0Sstevel@tonic-gate ng_close(struct irs_ng *this) {
104*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 	ng_minimize(this);
107*0Sstevel@tonic-gate 	if (pvt->curgroup)
108*0Sstevel@tonic-gate 		free(pvt->curgroup);
109*0Sstevel@tonic-gate 	memput(pvt, sizeof *pvt);
110*0Sstevel@tonic-gate 	memput(this, sizeof *this);
111*0Sstevel@tonic-gate }
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate static int
114*0Sstevel@tonic-gate ng_next(struct irs_ng *this, const char **host, const char **user,
115*0Sstevel@tonic-gate 	const char **domain)
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
118*0Sstevel@tonic-gate 	struct irs_ng *ng;
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	while (pvt->rule) {
121*0Sstevel@tonic-gate 		ng = pvt->rule->inst->ng;
122*0Sstevel@tonic-gate 		if ((*ng->next)(ng, host, user, domain) == 1)
123*0Sstevel@tonic-gate 			return (1);
124*0Sstevel@tonic-gate 		if (!(pvt->rule->flags & IRS_CONTINUE))
125*0Sstevel@tonic-gate 			break;
126*0Sstevel@tonic-gate 		pvt->rule = pvt->rule->next;
127*0Sstevel@tonic-gate 		if (pvt->rule) {
128*0Sstevel@tonic-gate 			ng = pvt->rule->inst->ng;
129*0Sstevel@tonic-gate 			(*ng->rewind)(ng, pvt->curgroup);
130*0Sstevel@tonic-gate 		}
131*0Sstevel@tonic-gate 	}
132*0Sstevel@tonic-gate 	return (0);
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate static int
136*0Sstevel@tonic-gate ng_test(struct irs_ng *this, const char *name,
137*0Sstevel@tonic-gate 	const char *user, const char *host, const char *domain)
138*0Sstevel@tonic-gate {
139*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
140*0Sstevel@tonic-gate 	struct irs_rule *rule;
141*0Sstevel@tonic-gate 	struct irs_ng *ng;
142*0Sstevel@tonic-gate 	int rval;
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	rval = 0;
145*0Sstevel@tonic-gate 	for (rule = pvt->rules; rule; rule = rule->next) {
146*0Sstevel@tonic-gate 		ng = rule->inst->ng;
147*0Sstevel@tonic-gate 		rval = (*ng->test)(ng, name, user, host, domain);
148*0Sstevel@tonic-gate 		if (rval || !(rule->flags & IRS_CONTINUE))
149*0Sstevel@tonic-gate 			break;
150*0Sstevel@tonic-gate 	}
151*0Sstevel@tonic-gate 	return (rval);
152*0Sstevel@tonic-gate }
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate static void
155*0Sstevel@tonic-gate ng_rewind(struct irs_ng *this, const char *group) {
156*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
157*0Sstevel@tonic-gate 	struct irs_ng *ng;
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	pvt->rule = pvt->rules;
160*0Sstevel@tonic-gate 	if (pvt->rule) {
161*0Sstevel@tonic-gate 		if (pvt->curgroup)
162*0Sstevel@tonic-gate 			free(pvt->curgroup);
163*0Sstevel@tonic-gate 		pvt->curgroup = strdup(group);
164*0Sstevel@tonic-gate 		ng = pvt->rule->inst->ng;
165*0Sstevel@tonic-gate 		(*ng->rewind)(ng, pvt->curgroup);
166*0Sstevel@tonic-gate 	}
167*0Sstevel@tonic-gate }
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate static void
170*0Sstevel@tonic-gate ng_minimize(struct irs_ng *this) {
171*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
172*0Sstevel@tonic-gate 	struct irs_rule *rule;
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	for (rule = pvt->rules; rule != NULL; rule = rule->next) {
175*0Sstevel@tonic-gate 		struct irs_ng *ng = rule->inst->ng;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 		(*ng->minimize)(ng);
178*0Sstevel@tonic-gate 	}
179*0Sstevel@tonic-gate }
180