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