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