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