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