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