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