Merge from vendor branch GDB:
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / bind / irs / gen.c
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.c,v 1.3.2.2 2004/03/17 00:40:12 marka Exp $";
20 #endif
21
22 /*
23  * this is the top level dispatcher
24  *
25  * The dispatcher is implemented as an accessor class; it is an
26  * accessor class that calls other accessor classes, as controlled by a
27  * configuration file.
28  * 
29  * A big difference between this accessor class and others is that the
30  * map class initializers are NULL, and the map classes are already
31  * filled in with method functions that will do the right thing.
32  */
33
34 /* Imports */
35
36 #include "port_before.h"
37
38 #include <isc/assertions.h>
39 #include <ctype.h>
40 #include <errno.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44
45 #include <sys/types.h>
46 #include <netinet/in.h> 
47 #include <arpa/nameser.h>
48 #include <resolv.h>
49
50 #include <isc/memcluster.h>
51 #include <irs.h>
52
53 #include "port_after.h"
54
55 #include "irs_p.h"
56 #include "gen_p.h"
57
58 /* Definitions */
59
60 struct nameval {
61         const char *    name;
62         int             val;
63 };
64
65 static const struct nameval acc_names[irs_nacc+1] = {
66         { "local", irs_lcl },
67         { "dns", irs_dns },
68         { "nis", irs_nis },
69         { "irp", irs_irp },
70         { NULL, irs_nacc }
71 };
72
73 typedef struct irs_acc *(*accinit) __P((const char *options));
74
75 static const accinit accs[irs_nacc+1] = {
76         irs_lcl_acc,
77         irs_dns_acc,
78 #ifdef WANT_IRS_NIS
79         irs_nis_acc,
80 #else
81         NULL,
82 #endif
83         irs_irp_acc,
84         NULL
85 };
86
87 static const struct nameval map_names[irs_nmap+1] = {
88         { "group", irs_gr },
89         { "passwd", irs_pw },
90         { "services", irs_sv },
91         { "protocols", irs_pr },
92         { "hosts", irs_ho },
93         { "networks", irs_nw },
94         { "netgroup", irs_ng },
95         { NULL, irs_nmap }
96 };
97
98 static const struct nameval option_names[] = {
99         { "merge", IRS_MERGE },
100         { "continue", IRS_CONTINUE },
101         { NULL, 0 }
102 };
103
104 /* Forward */
105
106 static void             gen_close(struct irs_acc *);
107 static struct __res_state * gen_res_get(struct irs_acc *);
108 static void             gen_res_set(struct irs_acc *, struct __res_state *,
109                                     void (*)(void *));
110 static int              find_name(const char *, const struct nameval nv[]);
111 static void             init_map_rules(struct gen_p *, const char *conf_file);
112 static struct irs_rule *release_rule(struct irs_rule *);
113 static int              add_rule(struct gen_p *,
114                                  enum irs_map_id, enum irs_acc_id,
115                                  const char *);
116
117 /* Public */
118
119 struct irs_acc *
120 irs_gen_acc(const char *options, const char *conf_file) {
121         struct irs_acc *acc;
122         struct gen_p *irs;
123                 
124         if (!(acc = memget(sizeof *acc))) {
125                 errno = ENOMEM;
126                 return (NULL);
127         }
128         memset(acc, 0x5e, sizeof *acc);
129         if (!(irs = memget(sizeof *irs))) {
130                 errno = ENOMEM;
131                 memput(acc, sizeof *acc);
132                 return (NULL);
133         }
134         memset(irs, 0x5e, sizeof *irs);
135         irs->options = strdup(options);
136         irs->res = NULL;
137         irs->free_res = NULL;
138         memset(irs->accessors, 0, sizeof irs->accessors);
139         memset(irs->map_rules, 0, sizeof irs->map_rules);
140         init_map_rules(irs, conf_file);
141         acc->private = irs;
142 #ifdef WANT_IRS_GR
143         acc->gr_map = irs_gen_gr;
144 #else
145         acc->gr_map = NULL;
146 #endif
147 #ifdef WANT_IRS_PW
148         acc->pw_map = irs_gen_pw;
149 #else
150         acc->pw_map = NULL;
151 #endif
152         acc->sv_map = irs_gen_sv;
153         acc->pr_map = irs_gen_pr;
154         acc->ho_map = irs_gen_ho;
155         acc->nw_map = irs_gen_nw;
156         acc->ng_map = irs_gen_ng;
157         acc->res_get = gen_res_get;
158         acc->res_set = gen_res_set;
159         acc->close = gen_close;
160         return (acc);
161 }
162
163 /* Methods */
164
165 static struct __res_state *
166 gen_res_get(struct irs_acc *this) {
167         struct gen_p *irs = (struct gen_p *)this->private;
168
169         if (irs->res == NULL) {
170                 struct __res_state *res;
171                 res = (struct __res_state *)malloc(sizeof *res);
172                 if (res == NULL)
173                         return (NULL);
174                 memset(res, 0, sizeof *res);
175                 gen_res_set(this, res, free);
176         }
177
178         if (((irs->res->options & RES_INIT) == 0U) && res_ninit(irs->res) < 0)
179                 return (NULL);
180
181         return (irs->res);
182 }
183
184 static void
185 gen_res_set(struct irs_acc *this, struct __res_state *res,
186             void (*free_res)(void *)) {
187         struct gen_p *irs = (struct gen_p *)this->private;
188 #if 0
189         struct irs_rule *rule;
190         struct irs_ho *ho;
191         struct irs_nw *nw;
192 #endif
193
194         if (irs->res && irs->free_res) {
195                 res_nclose(irs->res);
196                 (*irs->free_res)(irs->res);
197         }
198
199         irs->res = res;
200         irs->free_res = free_res;
201
202 #if 0
203         for (rule = irs->map_rules[irs_ho]; rule; rule = rule->next) {
204                 ho = rule->inst->ho;
205
206                 (*ho->res_set)(ho, res, NULL);
207         }
208         for (rule = irs->map_rules[irs_nw]; rule; rule = rule->next) {
209                 nw = rule->inst->nw;
210
211                 (*nw->res_set)(nw, res, NULL);
212         }
213 #endif
214 }
215
216 static void
217 gen_close(struct irs_acc *this) {
218         struct gen_p *irs = (struct gen_p *)this->private;
219         int n;
220         
221         /* Search rules. */
222         for (n = 0; n < irs_nmap; n++)
223                 while (irs->map_rules[n] != NULL)
224                         irs->map_rules[n] = release_rule(irs->map_rules[n]);
225
226         /* Access methods. */
227         for (n = 0; n < irs_nacc; n++) {
228                 /* Map objects. */
229                 if (irs->accessors[n].gr != NULL)
230                         (*irs->accessors[n].gr->close)(irs->accessors[n].gr);
231                 if (irs->accessors[n].pw != NULL)
232                         (*irs->accessors[n].pw->close)(irs->accessors[n].pw);
233                 if (irs->accessors[n].sv != NULL)
234                         (*irs->accessors[n].sv->close)(irs->accessors[n].sv);
235                 if (irs->accessors[n].pr != NULL)
236                         (*irs->accessors[n].pr->close)(irs->accessors[n].pr);
237                 if (irs->accessors[n].ho != NULL)
238                         (*irs->accessors[n].ho->close)(irs->accessors[n].ho);
239                 if (irs->accessors[n].nw != NULL)
240                         (*irs->accessors[n].nw->close)(irs->accessors[n].nw);
241                 if (irs->accessors[n].ng != NULL)
242                         (*irs->accessors[n].ng->close)(irs->accessors[n].ng);
243                 /* Enclosing accessor. */
244                 if (irs->accessors[n].acc != NULL)
245                         (*irs->accessors[n].acc->close)(irs->accessors[n].acc);
246         }
247
248         /* The options string was strdup'd. */
249         free((void*)irs->options);
250
251         if (irs->res && irs->free_res)
252                 (*irs->free_res)(irs->res);
253
254         /* The private data container. */
255         memput(irs, sizeof *irs);
256
257         /* The object. */
258         memput(this, sizeof *this);
259 }
260
261 /* Private */
262
263 static int
264 find_name(const char *name, const struct nameval names[]) {
265         int n;
266
267         for (n = 0; names[n].name != NULL; n++)
268                 if (strcmp(name, names[n].name) == 0)
269                         return (names[n].val);
270         return (-1);
271 }
272
273 static struct irs_rule *
274 release_rule(struct irs_rule *rule) {
275         struct irs_rule *next = rule->next;
276
277         memput(rule, sizeof *rule);
278         return (next);
279 }
280
281 static int
282 add_rule(struct gen_p *irs,
283          enum irs_map_id map, enum irs_acc_id acc,
284          const char *options)
285 {
286         struct irs_rule **rules, *last, *tmp, *new;
287         struct irs_inst *inst;
288         const char *cp;
289         int n;
290
291 #ifndef WANT_IRS_GR
292         if (map == irs_gr)
293                 return (-1);
294 #endif
295 #ifndef WANT_IRS_PW
296         if (map == irs_pw)
297                 return (-1);
298 #endif
299 #ifndef WANT_IRS_NIS
300         if (acc == irs_nis)
301                 return (-1);
302 #endif
303         new = memget(sizeof *new);
304         if (new == NULL)
305                 return (-1);
306         memset(new, 0x5e, sizeof *new);
307         new->next = NULL;
308
309         new->inst = &irs->accessors[acc];
310
311         new->flags = 0;
312         cp = options;
313         while (cp && *cp) {
314                 char option[50], *next;
315
316                 next = strchr(cp, ',');
317                 if (next)
318                         n = next++ - cp;
319                 else
320                         n = strlen(cp);
321                 if ((size_t)n > sizeof option - 1)
322                         n = sizeof option - 1;
323                 strncpy(option, cp, n);
324                 option[n] = '\0';
325
326                 n = find_name(option, option_names);
327                 if (n >= 0)
328                         new->flags |= n;
329
330                 cp = next;
331         }
332
333         rules = &irs->map_rules[map];
334         for (last = NULL, tmp = *rules;
335              tmp != NULL;
336              last = tmp, tmp = tmp->next)
337                 (void)NULL;
338         if (last == NULL)
339                 *rules = new;
340         else
341                 last->next = new;
342
343         /* Try to instantiate map accessors for this if necessary & approp. */
344         inst = &irs->accessors[acc];
345         if (inst->acc == NULL && accs[acc] != NULL)
346                 inst->acc = (*accs[acc])(irs->options);
347         if (inst->acc != NULL) {
348                 if (inst->gr == NULL && inst->acc->gr_map != NULL)
349                         inst->gr = (*inst->acc->gr_map)(inst->acc);
350                 if (inst->pw == NULL && inst->acc->pw_map != NULL)
351                         inst->pw = (*inst->acc->pw_map)(inst->acc);
352                 if (inst->sv == NULL && inst->acc->sv_map != NULL)
353                         inst->sv = (*inst->acc->sv_map)(inst->acc);
354                 if (inst->pr == NULL && inst->acc->pr_map != NULL)
355                         inst->pr = (*inst->acc->pr_map)(inst->acc);
356                 if (inst->ho == NULL && inst->acc->ho_map != NULL)
357                         inst->ho = (*inst->acc->ho_map)(inst->acc);
358                 if (inst->nw == NULL && inst->acc->nw_map != NULL)
359                         inst->nw = (*inst->acc->nw_map)(inst->acc);
360                 if (inst->ng == NULL && inst->acc->ng_map != NULL)
361                         inst->ng = (*inst->acc->ng_map)(inst->acc);
362         }
363
364         return (0);
365 }
366
367 static void
368 default_map_rules(struct gen_p *irs) {
369         /* Install time honoured and proved BSD style rules as default. */
370         add_rule(irs, irs_gr, irs_lcl, "");
371         add_rule(irs, irs_pw, irs_lcl, "");
372         add_rule(irs, irs_sv, irs_lcl, "");
373         add_rule(irs, irs_pr, irs_lcl, "");
374         add_rule(irs, irs_ho, irs_dns, "continue");
375         add_rule(irs, irs_ho, irs_lcl, "");
376         add_rule(irs, irs_nw, irs_dns, "continue");
377         add_rule(irs, irs_nw, irs_lcl, "");
378         add_rule(irs, irs_ng, irs_lcl, "");
379 }
380
381 static void
382 init_map_rules(struct gen_p *irs, const char *conf_file) {
383         char line[1024], pattern[40], mapname[20], accname[20], options[100];
384         FILE *conf;
385
386         if (conf_file == NULL) 
387                 conf_file = _PATH_IRS_CONF ;
388
389         /* A conf file of "" means compiled in defaults. Irpd wants this */
390         if (conf_file[0] == '\0' || (conf = fopen(conf_file, "r")) == NULL) {
391                 default_map_rules(irs);
392                 return;
393         }
394         (void) sprintf(pattern, "%%%ds %%%ds %%%ds\n",
395                        sizeof mapname, sizeof accname, sizeof options);
396         while (fgets(line, sizeof line, conf)) {
397                 enum irs_map_id map;
398                 enum irs_acc_id acc;
399                 char *tmp;
400                 int n;
401
402                 for (tmp = line;
403                      isascii((unsigned char)*tmp) &&
404                      isspace((unsigned char)*tmp);
405                      tmp++)
406                         (void)NULL;
407                 if (*tmp == '#' || *tmp == '\n' || *tmp == '\0')
408                         continue;
409                 n = sscanf(tmp, pattern, mapname, accname, options);
410                 if (n < 2)
411                         continue;
412                 if (n < 3)
413                         options[0] = '\0';
414
415                 n = find_name(mapname, map_names);
416                 INSIST(n < irs_nmap);
417                 if (n < 0)
418                         continue;
419                 map = (enum irs_map_id) n;
420
421                 n = find_name(accname, acc_names);
422                 INSIST(n < irs_nacc);
423                 if (n < 0)
424                         continue;
425                 acc = (enum irs_acc_id) n;
426
427                 add_rule(irs, map, acc, options);
428         }
429         fclose(conf);
430 }