10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
30Sstevel@tonic-gate * Use is subject to license terms.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate
6*11038SRao.Shoaib@Sun.COM
70Sstevel@tonic-gate /*
8*11038SRao.Shoaib@Sun.COM * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
90Sstevel@tonic-gate * Copyright (c) 1996-1999 by Internet Software Consortium.
100Sstevel@tonic-gate *
110Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
120Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
130Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
140Sstevel@tonic-gate *
15*11038SRao.Shoaib@Sun.COM * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
16*11038SRao.Shoaib@Sun.COM * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17*11038SRao.Shoaib@Sun.COM * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
18*11038SRao.Shoaib@Sun.COM * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19*11038SRao.Shoaib@Sun.COM * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20*11038SRao.Shoaib@Sun.COM * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21*11038SRao.Shoaib@Sun.COM * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
220Sstevel@tonic-gate */
230Sstevel@tonic-gate
240Sstevel@tonic-gate #if !defined(LINT) && !defined(CODECENTER)
25*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: gen.c,v 1.7 2005/04/27 04:56:23 sra Exp $";
260Sstevel@tonic-gate #endif
270Sstevel@tonic-gate
28*11038SRao.Shoaib@Sun.COM /*! \file
29*11038SRao.Shoaib@Sun.COM * \brief
300Sstevel@tonic-gate * this is the top level dispatcher
310Sstevel@tonic-gate *
320Sstevel@tonic-gate * The dispatcher is implemented as an accessor class; it is an
330Sstevel@tonic-gate * accessor class that calls other accessor classes, as controlled by a
340Sstevel@tonic-gate * configuration file.
350Sstevel@tonic-gate *
360Sstevel@tonic-gate * A big difference between this accessor class and others is that the
370Sstevel@tonic-gate * map class initializers are NULL, and the map classes are already
380Sstevel@tonic-gate * filled in with method functions that will do the right thing.
390Sstevel@tonic-gate */
400Sstevel@tonic-gate
410Sstevel@tonic-gate /* Imports */
420Sstevel@tonic-gate
430Sstevel@tonic-gate #include "port_before.h"
440Sstevel@tonic-gate
450Sstevel@tonic-gate #include <isc/assertions.h>
460Sstevel@tonic-gate #include <ctype.h>
470Sstevel@tonic-gate #include <errno.h>
480Sstevel@tonic-gate #include <stdio.h>
490Sstevel@tonic-gate #include <stdlib.h>
500Sstevel@tonic-gate #include <string.h>
510Sstevel@tonic-gate
520Sstevel@tonic-gate #include <sys/types.h>
530Sstevel@tonic-gate #include <netinet/in.h>
540Sstevel@tonic-gate #include <arpa/nameser.h>
550Sstevel@tonic-gate #include <resolv.h>
560Sstevel@tonic-gate
570Sstevel@tonic-gate #include <isc/memcluster.h>
580Sstevel@tonic-gate #include <irs.h>
590Sstevel@tonic-gate
600Sstevel@tonic-gate #include "port_after.h"
610Sstevel@tonic-gate
620Sstevel@tonic-gate #include "irs_p.h"
630Sstevel@tonic-gate #include "gen_p.h"
640Sstevel@tonic-gate
650Sstevel@tonic-gate #ifdef SUNW_HOSTS_FALLBACK
660Sstevel@tonic-gate extern int __res_no_hosts_fallback(void);
670Sstevel@tonic-gate #endif /* SUNW_HOSTS_FALLBACK */
680Sstevel@tonic-gate
690Sstevel@tonic-gate /* Definitions */
700Sstevel@tonic-gate
710Sstevel@tonic-gate struct nameval {
720Sstevel@tonic-gate const char * name;
730Sstevel@tonic-gate int val;
740Sstevel@tonic-gate };
750Sstevel@tonic-gate
760Sstevel@tonic-gate static const struct nameval acc_names[irs_nacc+1] = {
770Sstevel@tonic-gate { "local", irs_lcl },
780Sstevel@tonic-gate { "dns", irs_dns },
790Sstevel@tonic-gate { "nis", irs_nis },
800Sstevel@tonic-gate { "irp", irs_irp },
810Sstevel@tonic-gate { NULL, irs_nacc }
820Sstevel@tonic-gate };
830Sstevel@tonic-gate
840Sstevel@tonic-gate typedef struct irs_acc *(*accinit) __P((const char *options));
850Sstevel@tonic-gate
860Sstevel@tonic-gate static const accinit accs[irs_nacc+1] = {
870Sstevel@tonic-gate irs_lcl_acc,
880Sstevel@tonic-gate irs_dns_acc,
890Sstevel@tonic-gate #ifdef WANT_IRS_NIS
900Sstevel@tonic-gate irs_nis_acc,
910Sstevel@tonic-gate #else
920Sstevel@tonic-gate NULL,
930Sstevel@tonic-gate #endif
940Sstevel@tonic-gate irs_irp_acc,
950Sstevel@tonic-gate NULL
960Sstevel@tonic-gate };
970Sstevel@tonic-gate
980Sstevel@tonic-gate static const struct nameval map_names[irs_nmap+1] = {
990Sstevel@tonic-gate { "group", irs_gr },
1000Sstevel@tonic-gate { "passwd", irs_pw },
1010Sstevel@tonic-gate { "services", irs_sv },
1020Sstevel@tonic-gate { "protocols", irs_pr },
1030Sstevel@tonic-gate { "hosts", irs_ho },
1040Sstevel@tonic-gate { "networks", irs_nw },
1050Sstevel@tonic-gate { "netgroup", irs_ng },
1060Sstevel@tonic-gate { NULL, irs_nmap }
1070Sstevel@tonic-gate };
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate static const struct nameval option_names[] = {
1100Sstevel@tonic-gate { "merge", IRS_MERGE },
1110Sstevel@tonic-gate { "continue", IRS_CONTINUE },
1120Sstevel@tonic-gate { NULL, 0 }
1130Sstevel@tonic-gate };
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate /* Forward */
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate static void gen_close(struct irs_acc *);
1180Sstevel@tonic-gate static struct __res_state * gen_res_get(struct irs_acc *);
1190Sstevel@tonic-gate static void gen_res_set(struct irs_acc *, struct __res_state *,
1200Sstevel@tonic-gate void (*)(void *));
1210Sstevel@tonic-gate static int find_name(const char *, const struct nameval nv[]);
1220Sstevel@tonic-gate static void init_map_rules(struct gen_p *, const char *conf_file);
1230Sstevel@tonic-gate static struct irs_rule *release_rule(struct irs_rule *);
1240Sstevel@tonic-gate static int add_rule(struct gen_p *,
1250Sstevel@tonic-gate enum irs_map_id, enum irs_acc_id,
1260Sstevel@tonic-gate const char *);
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate /* Public */
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate struct irs_acc *
irs_gen_acc(const char * options,const char * conf_file)1310Sstevel@tonic-gate irs_gen_acc(const char *options, const char *conf_file) {
1320Sstevel@tonic-gate struct irs_acc *acc;
1330Sstevel@tonic-gate struct gen_p *irs;
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate if (!(acc = memget(sizeof *acc))) {
1360Sstevel@tonic-gate errno = ENOMEM;
1370Sstevel@tonic-gate return (NULL);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate memset(acc, 0x5e, sizeof *acc);
1400Sstevel@tonic-gate if (!(irs = memget(sizeof *irs))) {
1410Sstevel@tonic-gate errno = ENOMEM;
1420Sstevel@tonic-gate memput(acc, sizeof *acc);
1430Sstevel@tonic-gate return (NULL);
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate memset(irs, 0x5e, sizeof *irs);
1460Sstevel@tonic-gate irs->options = strdup(options);
1470Sstevel@tonic-gate irs->res = NULL;
1480Sstevel@tonic-gate irs->free_res = NULL;
1490Sstevel@tonic-gate memset(irs->accessors, 0, sizeof irs->accessors);
1500Sstevel@tonic-gate memset(irs->map_rules, 0, sizeof irs->map_rules);
1510Sstevel@tonic-gate init_map_rules(irs, conf_file);
1520Sstevel@tonic-gate acc->private = irs;
1530Sstevel@tonic-gate #ifdef WANT_IRS_GR
1540Sstevel@tonic-gate acc->gr_map = irs_gen_gr;
1550Sstevel@tonic-gate #else
1560Sstevel@tonic-gate acc->gr_map = NULL;
1570Sstevel@tonic-gate #endif
1580Sstevel@tonic-gate #ifdef WANT_IRS_PW
1590Sstevel@tonic-gate acc->pw_map = irs_gen_pw;
1600Sstevel@tonic-gate #else
1610Sstevel@tonic-gate acc->pw_map = NULL;
1620Sstevel@tonic-gate #endif
1630Sstevel@tonic-gate acc->sv_map = irs_gen_sv;
1640Sstevel@tonic-gate acc->pr_map = irs_gen_pr;
1650Sstevel@tonic-gate acc->ho_map = irs_gen_ho;
1660Sstevel@tonic-gate acc->nw_map = irs_gen_nw;
1670Sstevel@tonic-gate acc->ng_map = irs_gen_ng;
1680Sstevel@tonic-gate acc->res_get = gen_res_get;
1690Sstevel@tonic-gate acc->res_set = gen_res_set;
1700Sstevel@tonic-gate acc->close = gen_close;
1710Sstevel@tonic-gate return (acc);
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate /* Methods */
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate static struct __res_state *
gen_res_get(struct irs_acc * this)1770Sstevel@tonic-gate gen_res_get(struct irs_acc *this) {
1780Sstevel@tonic-gate struct gen_p *irs = (struct gen_p *)this->private;
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate if (irs->res == NULL) {
1810Sstevel@tonic-gate struct __res_state *res;
1820Sstevel@tonic-gate res = (struct __res_state *)malloc(sizeof *res);
1830Sstevel@tonic-gate if (res == NULL)
1840Sstevel@tonic-gate return (NULL);
1850Sstevel@tonic-gate memset(res, 0, sizeof *res);
1860Sstevel@tonic-gate gen_res_set(this, res, free);
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate
189*11038SRao.Shoaib@Sun.COM if (((irs->res->options & RES_INIT) == 0U) && res_ninit(irs->res) < 0)
1900Sstevel@tonic-gate return (NULL);
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate return (irs->res);
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate static void
gen_res_set(struct irs_acc * this,struct __res_state * res,void (* free_res)(void *))1960Sstevel@tonic-gate gen_res_set(struct irs_acc *this, struct __res_state *res,
1970Sstevel@tonic-gate void (*free_res)(void *)) {
1980Sstevel@tonic-gate struct gen_p *irs = (struct gen_p *)this->private;
1990Sstevel@tonic-gate #if 0
2000Sstevel@tonic-gate struct irs_rule *rule;
2010Sstevel@tonic-gate struct irs_ho *ho;
2020Sstevel@tonic-gate struct irs_nw *nw;
2030Sstevel@tonic-gate #endif
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate if (irs->res && irs->free_res) {
2060Sstevel@tonic-gate res_nclose(irs->res);
2070Sstevel@tonic-gate (*irs->free_res)(irs->res);
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate irs->res = res;
2110Sstevel@tonic-gate irs->free_res = free_res;
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate #if 0
2140Sstevel@tonic-gate for (rule = irs->map_rules[irs_ho]; rule; rule = rule->next) {
2150Sstevel@tonic-gate ho = rule->inst->ho;
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate (*ho->res_set)(ho, res, NULL);
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate for (rule = irs->map_rules[irs_nw]; rule; rule = rule->next) {
2200Sstevel@tonic-gate nw = rule->inst->nw;
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate (*nw->res_set)(nw, res, NULL);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate #endif
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate static void
gen_close(struct irs_acc * this)2280Sstevel@tonic-gate gen_close(struct irs_acc *this) {
2290Sstevel@tonic-gate struct gen_p *irs = (struct gen_p *)this->private;
2300Sstevel@tonic-gate int n;
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate /* Search rules. */
2330Sstevel@tonic-gate for (n = 0; n < irs_nmap; n++)
2340Sstevel@tonic-gate while (irs->map_rules[n] != NULL)
2350Sstevel@tonic-gate irs->map_rules[n] = release_rule(irs->map_rules[n]);
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate /* Access methods. */
2380Sstevel@tonic-gate for (n = 0; n < irs_nacc; n++) {
2390Sstevel@tonic-gate /* Map objects. */
2400Sstevel@tonic-gate if (irs->accessors[n].gr != NULL)
2410Sstevel@tonic-gate (*irs->accessors[n].gr->close)(irs->accessors[n].gr);
2420Sstevel@tonic-gate if (irs->accessors[n].pw != NULL)
2430Sstevel@tonic-gate (*irs->accessors[n].pw->close)(irs->accessors[n].pw);
2440Sstevel@tonic-gate if (irs->accessors[n].sv != NULL)
2450Sstevel@tonic-gate (*irs->accessors[n].sv->close)(irs->accessors[n].sv);
2460Sstevel@tonic-gate if (irs->accessors[n].pr != NULL)
2470Sstevel@tonic-gate (*irs->accessors[n].pr->close)(irs->accessors[n].pr);
2480Sstevel@tonic-gate if (irs->accessors[n].ho != NULL)
2490Sstevel@tonic-gate (*irs->accessors[n].ho->close)(irs->accessors[n].ho);
2500Sstevel@tonic-gate if (irs->accessors[n].nw != NULL)
2510Sstevel@tonic-gate (*irs->accessors[n].nw->close)(irs->accessors[n].nw);
2520Sstevel@tonic-gate if (irs->accessors[n].ng != NULL)
2530Sstevel@tonic-gate (*irs->accessors[n].ng->close)(irs->accessors[n].ng);
2540Sstevel@tonic-gate /* Enclosing accessor. */
2550Sstevel@tonic-gate if (irs->accessors[n].acc != NULL)
2560Sstevel@tonic-gate (*irs->accessors[n].acc->close)(irs->accessors[n].acc);
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate /* The options string was strdup'd. */
2600Sstevel@tonic-gate free((void*)irs->options);
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate if (irs->res && irs->free_res)
2630Sstevel@tonic-gate (*irs->free_res)(irs->res);
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate /* The private data container. */
2660Sstevel@tonic-gate memput(irs, sizeof *irs);
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate /* The object. */
2690Sstevel@tonic-gate memput(this, sizeof *this);
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate /* Private */
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate static int
find_name(const char * name,const struct nameval names[])2750Sstevel@tonic-gate find_name(const char *name, const struct nameval names[]) {
2760Sstevel@tonic-gate int n;
2770Sstevel@tonic-gate
2780Sstevel@tonic-gate for (n = 0; names[n].name != NULL; n++)
2790Sstevel@tonic-gate if (strcmp(name, names[n].name) == 0)
2800Sstevel@tonic-gate return (names[n].val);
2810Sstevel@tonic-gate return (-1);
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate static struct irs_rule *
release_rule(struct irs_rule * rule)2850Sstevel@tonic-gate release_rule(struct irs_rule *rule) {
2860Sstevel@tonic-gate struct irs_rule *next = rule->next;
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate memput(rule, sizeof *rule);
2890Sstevel@tonic-gate return (next);
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate static int
add_rule(struct gen_p * irs,enum irs_map_id map,enum irs_acc_id acc,const char * options)2930Sstevel@tonic-gate add_rule(struct gen_p *irs,
2940Sstevel@tonic-gate enum irs_map_id map, enum irs_acc_id acc,
2950Sstevel@tonic-gate const char *options)
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate struct irs_rule **rules, *last, *tmp, *new;
2980Sstevel@tonic-gate struct irs_inst *inst;
2990Sstevel@tonic-gate const char *cp;
3000Sstevel@tonic-gate int n;
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate #ifndef WANT_IRS_GR
3030Sstevel@tonic-gate if (map == irs_gr)
3040Sstevel@tonic-gate return (-1);
3050Sstevel@tonic-gate #endif
3060Sstevel@tonic-gate #ifndef WANT_IRS_PW
3070Sstevel@tonic-gate if (map == irs_pw)
3080Sstevel@tonic-gate return (-1);
3090Sstevel@tonic-gate #endif
3100Sstevel@tonic-gate #ifndef WANT_IRS_NIS
3110Sstevel@tonic-gate if (acc == irs_nis)
3120Sstevel@tonic-gate return (-1);
3130Sstevel@tonic-gate #endif
3140Sstevel@tonic-gate new = memget(sizeof *new);
3150Sstevel@tonic-gate if (new == NULL)
3160Sstevel@tonic-gate return (-1);
3170Sstevel@tonic-gate memset(new, 0x5e, sizeof *new);
3180Sstevel@tonic-gate new->next = NULL;
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate new->inst = &irs->accessors[acc];
3210Sstevel@tonic-gate
3220Sstevel@tonic-gate new->flags = 0;
3230Sstevel@tonic-gate cp = options;
3240Sstevel@tonic-gate while (cp && *cp) {
3250Sstevel@tonic-gate char option[50], *next;
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate next = strchr(cp, ',');
3280Sstevel@tonic-gate if (next)
3290Sstevel@tonic-gate n = next++ - cp;
3300Sstevel@tonic-gate else
3310Sstevel@tonic-gate n = strlen(cp);
3320Sstevel@tonic-gate if ((size_t)n > sizeof option - 1)
3330Sstevel@tonic-gate n = sizeof option - 1;
3340Sstevel@tonic-gate strncpy(option, cp, n);
3350Sstevel@tonic-gate option[n] = '\0';
3360Sstevel@tonic-gate
3370Sstevel@tonic-gate n = find_name(option, option_names);
3380Sstevel@tonic-gate if (n >= 0)
3390Sstevel@tonic-gate new->flags |= n;
3400Sstevel@tonic-gate
3410Sstevel@tonic-gate cp = next;
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate
3440Sstevel@tonic-gate rules = &irs->map_rules[map];
3450Sstevel@tonic-gate for (last = NULL, tmp = *rules;
3460Sstevel@tonic-gate tmp != NULL;
3470Sstevel@tonic-gate last = tmp, tmp = tmp->next)
3480Sstevel@tonic-gate (void)NULL;
3490Sstevel@tonic-gate if (last == NULL)
3500Sstevel@tonic-gate *rules = new;
3510Sstevel@tonic-gate else
3520Sstevel@tonic-gate last->next = new;
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate /* Try to instantiate map accessors for this if necessary & approp. */
3550Sstevel@tonic-gate inst = &irs->accessors[acc];
3560Sstevel@tonic-gate if (inst->acc == NULL && accs[acc] != NULL)
3570Sstevel@tonic-gate inst->acc = (*accs[acc])(irs->options);
3580Sstevel@tonic-gate if (inst->acc != NULL) {
3590Sstevel@tonic-gate if (inst->gr == NULL && inst->acc->gr_map != NULL)
3600Sstevel@tonic-gate inst->gr = (*inst->acc->gr_map)(inst->acc);
3610Sstevel@tonic-gate if (inst->pw == NULL && inst->acc->pw_map != NULL)
3620Sstevel@tonic-gate inst->pw = (*inst->acc->pw_map)(inst->acc);
3630Sstevel@tonic-gate if (inst->sv == NULL && inst->acc->sv_map != NULL)
3640Sstevel@tonic-gate inst->sv = (*inst->acc->sv_map)(inst->acc);
3650Sstevel@tonic-gate if (inst->pr == NULL && inst->acc->pr_map != NULL)
3660Sstevel@tonic-gate inst->pr = (*inst->acc->pr_map)(inst->acc);
3670Sstevel@tonic-gate if (inst->ho == NULL && inst->acc->ho_map != NULL)
3680Sstevel@tonic-gate inst->ho = (*inst->acc->ho_map)(inst->acc);
3690Sstevel@tonic-gate if (inst->nw == NULL && inst->acc->nw_map != NULL)
3700Sstevel@tonic-gate inst->nw = (*inst->acc->nw_map)(inst->acc);
3710Sstevel@tonic-gate if (inst->ng == NULL && inst->acc->ng_map != NULL)
3720Sstevel@tonic-gate inst->ng = (*inst->acc->ng_map)(inst->acc);
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate
3750Sstevel@tonic-gate return (0);
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate static void
default_map_rules(struct gen_p * irs)3790Sstevel@tonic-gate default_map_rules(struct gen_p *irs) {
3800Sstevel@tonic-gate /* Install time honoured and proved BSD style rules as default. */
3810Sstevel@tonic-gate add_rule(irs, irs_gr, irs_lcl, "");
3820Sstevel@tonic-gate add_rule(irs, irs_pw, irs_lcl, "");
3830Sstevel@tonic-gate add_rule(irs, irs_sv, irs_lcl, "");
3840Sstevel@tonic-gate add_rule(irs, irs_pr, irs_lcl, "");
3850Sstevel@tonic-gate #ifdef SUNW_HOSTS_FALLBACK
3860Sstevel@tonic-gate if (__res_no_hosts_fallback())
3870Sstevel@tonic-gate add_rule(irs, irs_ho, irs_dns, "");
3880Sstevel@tonic-gate else {
3890Sstevel@tonic-gate add_rule(irs, irs_ho, irs_dns, "continue");
3900Sstevel@tonic-gate add_rule(irs, irs_ho, irs_lcl, "");
3910Sstevel@tonic-gate }
3920Sstevel@tonic-gate #else /* SUNW_HOSTS_FALLBACK */
3930Sstevel@tonic-gate add_rule(irs, irs_ho, irs_dns, "continue");
3940Sstevel@tonic-gate add_rule(irs, irs_ho, irs_lcl, "");
3950Sstevel@tonic-gate #endif /* SUNW_HOSTS_FALLBACK */
3960Sstevel@tonic-gate add_rule(irs, irs_nw, irs_dns, "continue");
3970Sstevel@tonic-gate add_rule(irs, irs_nw, irs_lcl, "");
3980Sstevel@tonic-gate add_rule(irs, irs_ng, irs_lcl, "");
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate
4010Sstevel@tonic-gate static void
init_map_rules(struct gen_p * irs,const char * conf_file)4020Sstevel@tonic-gate init_map_rules(struct gen_p *irs, const char *conf_file) {
4030Sstevel@tonic-gate char line[1024], pattern[40], mapname[20], accname[20], options[100];
4040Sstevel@tonic-gate FILE *conf;
4050Sstevel@tonic-gate
4060Sstevel@tonic-gate #ifdef SUNW_HOSTS_FALLBACK
4070Sstevel@tonic-gate if (__res_no_hosts_fallback()) {
4080Sstevel@tonic-gate default_map_rules(irs);
4090Sstevel@tonic-gate return;
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate #endif /* SUNW_HOSTS_FALLBACK */
4120Sstevel@tonic-gate
4130Sstevel@tonic-gate if (conf_file == NULL)
4140Sstevel@tonic-gate conf_file = _PATH_IRS_CONF ;
4150Sstevel@tonic-gate
4160Sstevel@tonic-gate /* A conf file of "" means compiled in defaults. Irpd wants this */
4170Sstevel@tonic-gate if (conf_file[0] == '\0' || (conf = fopen(conf_file, "r")) == NULL) {
4180Sstevel@tonic-gate default_map_rules(irs);
4190Sstevel@tonic-gate return;
4200Sstevel@tonic-gate }
421*11038SRao.Shoaib@Sun.COM (void) sprintf(pattern, "%%%lus %%%lus %%%lus\n",
422*11038SRao.Shoaib@Sun.COM (unsigned long)sizeof mapname,
423*11038SRao.Shoaib@Sun.COM (unsigned long)sizeof accname,
424*11038SRao.Shoaib@Sun.COM (unsigned long)sizeof options);
4250Sstevel@tonic-gate while (fgets(line, sizeof line, conf)) {
4260Sstevel@tonic-gate enum irs_map_id map;
4270Sstevel@tonic-gate enum irs_acc_id acc;
4280Sstevel@tonic-gate char *tmp;
4290Sstevel@tonic-gate int n;
4300Sstevel@tonic-gate
4310Sstevel@tonic-gate for (tmp = line;
4320Sstevel@tonic-gate isascii((unsigned char)*tmp) &&
4330Sstevel@tonic-gate isspace((unsigned char)*tmp);
4340Sstevel@tonic-gate tmp++)
4350Sstevel@tonic-gate (void)NULL;
4360Sstevel@tonic-gate if (*tmp == '#' || *tmp == '\n' || *tmp == '\0')
4370Sstevel@tonic-gate continue;
4380Sstevel@tonic-gate n = sscanf(tmp, pattern, mapname, accname, options);
4390Sstevel@tonic-gate if (n < 2)
4400Sstevel@tonic-gate continue;
4410Sstevel@tonic-gate if (n < 3)
4420Sstevel@tonic-gate options[0] = '\0';
4430Sstevel@tonic-gate
4440Sstevel@tonic-gate n = find_name(mapname, map_names);
4450Sstevel@tonic-gate INSIST(n < irs_nmap);
4460Sstevel@tonic-gate if (n < 0)
4470Sstevel@tonic-gate continue;
4480Sstevel@tonic-gate map = (enum irs_map_id) n;
4490Sstevel@tonic-gate
4500Sstevel@tonic-gate n = find_name(accname, acc_names);
4510Sstevel@tonic-gate INSIST(n < irs_nacc);
4520Sstevel@tonic-gate if (n < 0)
4530Sstevel@tonic-gate continue;
4540Sstevel@tonic-gate acc = (enum irs_acc_id) n;
4550Sstevel@tonic-gate
4560Sstevel@tonic-gate add_rule(irs, map, acc, options);
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate fclose(conf);
4590Sstevel@tonic-gate }
460