xref: /titanic_52/usr/src/lib/libresolv2/common/irs/gen_ng.c (revision 174bc6499d233e329ecd3d98a880a7b07df16bfa)
1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 1996,1999 by Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17 
18 #if !defined(LINT) && !defined(CODECENTER)
19 static const char rcsid[] = "$Id: gen_ng.c,v 1.3 2005/04/27 04:56:23 sra Exp $";
20 #endif
21 
22 /* Imports */
23 
24 #include "port_before.h"
25 
26 #include <sys/types.h>
27 
28 #include <netinet/in.h>
29 #include <arpa/nameser.h>
30 #include <resolv.h>
31 
32 #include <errno.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #include <isc/memcluster.h>
37 #include <irs.h>
38 
39 #include "port_after.h"
40 
41 #include "irs_p.h"
42 #include "gen_p.h"
43 
44 /* Types */
45 
46 struct pvt {
47 	struct irs_rule *	rules;
48 	struct irs_rule *	rule;
49 	char *			curgroup;
50 };
51 
52 /* Forward */
53 
54 static void		ng_close(struct irs_ng *);
55 static int		ng_next(struct irs_ng *, const char **,
56 				const char **, const char **);
57 static int 		ng_test(struct irs_ng *, const char *,
58 				const char *, const char *,
59 				const char *);
60 static void 		ng_rewind(struct irs_ng *, const char *);
61 static void		ng_minimize(struct irs_ng *);
62 
63 /* Public */
64 
65 struct irs_ng *
66 irs_gen_ng(struct irs_acc *this) {
67 	struct gen_p *accpvt = (struct gen_p *)this->private;
68 	struct irs_ng *ng;
69 	struct pvt *pvt;
70 
71 	if (!(ng = memget(sizeof *ng))) {
72 		errno = ENOMEM;
73 		return (NULL);
74 	}
75 	memset(ng, 0x5e, sizeof *ng);
76 	if (!(pvt = memget(sizeof *pvt))) {
77 		memput(ng, sizeof *ng);
78 		errno = ENOMEM;
79 		return (NULL);
80 	}
81 	memset(pvt, 0, sizeof *pvt);
82 	pvt->rules = accpvt->map_rules[irs_ng];
83 	pvt->rule = pvt->rules;
84 	ng->private = pvt;
85 	ng->close = ng_close;
86 	ng->next = ng_next;
87 	ng->test = ng_test;
88 	ng->rewind = ng_rewind;
89 	ng->minimize = ng_minimize;
90 	return (ng);
91 }
92 
93 /* Methods */
94 
95 static void
96 ng_close(struct irs_ng *this) {
97 	struct pvt *pvt = (struct pvt *)this->private;
98 
99 	ng_minimize(this);
100 	if (pvt->curgroup)
101 		free(pvt->curgroup);
102 	memput(pvt, sizeof *pvt);
103 	memput(this, sizeof *this);
104 }
105 
106 static int
107 ng_next(struct irs_ng *this, const char **host, const char **user,
108 	const char **domain)
109 {
110 	struct pvt *pvt = (struct pvt *)this->private;
111 	struct irs_ng *ng;
112 
113 	while (pvt->rule) {
114 		ng = pvt->rule->inst->ng;
115 		if ((*ng->next)(ng, host, user, domain) == 1)
116 			return (1);
117 		if (!(pvt->rule->flags & IRS_CONTINUE))
118 			break;
119 		pvt->rule = pvt->rule->next;
120 		if (pvt->rule) {
121 			ng = pvt->rule->inst->ng;
122 			(*ng->rewind)(ng, pvt->curgroup);
123 		}
124 	}
125 	return (0);
126 }
127 
128 static int
129 ng_test(struct irs_ng *this, const char *name,
130 	const char *user, const char *host, const char *domain)
131 {
132 	struct pvt *pvt = (struct pvt *)this->private;
133 	struct irs_rule *rule;
134 	struct irs_ng *ng;
135 	int rval;
136 
137 	rval = 0;
138 	for (rule = pvt->rules; rule; rule = rule->next) {
139 		ng = rule->inst->ng;
140 		rval = (*ng->test)(ng, name, user, host, domain);
141 		if (rval || !(rule->flags & IRS_CONTINUE))
142 			break;
143 	}
144 	return (rval);
145 }
146 
147 static void
148 ng_rewind(struct irs_ng *this, const char *group) {
149 	struct pvt *pvt = (struct pvt *)this->private;
150 	struct irs_ng *ng;
151 
152 	pvt->rule = pvt->rules;
153 	if (pvt->rule) {
154 		if (pvt->curgroup)
155 			free(pvt->curgroup);
156 		pvt->curgroup = strdup(group);
157 		ng = pvt->rule->inst->ng;
158 		(*ng->rewind)(ng, pvt->curgroup);
159 	}
160 }
161 
162 static void
163 ng_minimize(struct irs_ng *this) {
164 	struct pvt *pvt = (struct pvt *)this->private;
165 	struct irs_rule *rule;
166 
167 	for (rule = pvt->rules; rule != NULL; rule = rule->next) {
168 		struct irs_ng *ng = rule->inst->ng;
169 
170 		(*ng->minimize)(ng);
171 	}
172 }
173 
174 /*! \file */
175