17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
6*9525b14bSRao Shoaib 
77c478bd9Sstevel@tonic-gate /*
8*9525b14bSRao Shoaib  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
97c478bd9Sstevel@tonic-gate  * Copyright (c) 1996-1999 by Internet Software Consortium.
107c478bd9Sstevel@tonic-gate  *
117c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
127c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
137c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
147c478bd9Sstevel@tonic-gate  *
15*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
16*9525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17*9525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
18*9525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19*9525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20*9525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21*9525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
227c478bd9Sstevel@tonic-gate  */
237c478bd9Sstevel@tonic-gate 
24*9525b14bSRao Shoaib /*! \file
25*9525b14bSRao Shoaib  * \brief
267c478bd9Sstevel@tonic-gate  * this is the top level dispatcher
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  * The dispatcher is implemented as an accessor class; it is an
297c478bd9Sstevel@tonic-gate  * accessor class that calls other accessor classes, as controlled by a
307c478bd9Sstevel@tonic-gate  * configuration file.
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * A big difference between this accessor class and others is that the
337c478bd9Sstevel@tonic-gate  * map class initializers are NULL, and the map classes are already
347c478bd9Sstevel@tonic-gate  * filled in with method functions that will do the right thing.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /* Imports */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include "port_before.h"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <isc/assertions.h>
427c478bd9Sstevel@tonic-gate #include <ctype.h>
437c478bd9Sstevel@tonic-gate #include <errno.h>
447c478bd9Sstevel@tonic-gate #include <stdio.h>
457c478bd9Sstevel@tonic-gate #include <stdlib.h>
467c478bd9Sstevel@tonic-gate #include <string.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include <sys/types.h>
497c478bd9Sstevel@tonic-gate #include <netinet/in.h>
507c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
517c478bd9Sstevel@tonic-gate #include <resolv.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
547c478bd9Sstevel@tonic-gate #include <irs.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #include "port_after.h"
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #include "irs_p.h"
597c478bd9Sstevel@tonic-gate #include "gen_p.h"
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #ifdef SUNW_HOSTS_FALLBACK
627c478bd9Sstevel@tonic-gate extern int __res_no_hosts_fallback(void);
637c478bd9Sstevel@tonic-gate #endif /* SUNW_HOSTS_FALLBACK */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /* Definitions */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate struct nameval {
687c478bd9Sstevel@tonic-gate 	const char *	name;
697c478bd9Sstevel@tonic-gate 	int		val;
707c478bd9Sstevel@tonic-gate };
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static const struct nameval acc_names[irs_nacc+1] = {
737c478bd9Sstevel@tonic-gate 	{ "local", irs_lcl },
747c478bd9Sstevel@tonic-gate 	{ "dns", irs_dns },
757c478bd9Sstevel@tonic-gate 	{ "nis", irs_nis },
767c478bd9Sstevel@tonic-gate 	{ "irp", irs_irp },
777c478bd9Sstevel@tonic-gate 	{ NULL, irs_nacc }
787c478bd9Sstevel@tonic-gate };
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate typedef struct irs_acc *(*accinit) __P((const char *options));
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static const accinit accs[irs_nacc+1] = {
837c478bd9Sstevel@tonic-gate 	irs_lcl_acc,
847c478bd9Sstevel@tonic-gate 	irs_dns_acc,
857c478bd9Sstevel@tonic-gate #ifdef WANT_IRS_NIS
867c478bd9Sstevel@tonic-gate 	irs_nis_acc,
877c478bd9Sstevel@tonic-gate #else
887c478bd9Sstevel@tonic-gate 	NULL,
897c478bd9Sstevel@tonic-gate #endif
907c478bd9Sstevel@tonic-gate 	irs_irp_acc,
917c478bd9Sstevel@tonic-gate 	NULL
927c478bd9Sstevel@tonic-gate };
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate static const struct nameval map_names[irs_nmap+1] = {
957c478bd9Sstevel@tonic-gate 	{ "group", irs_gr },
967c478bd9Sstevel@tonic-gate 	{ "passwd", irs_pw },
977c478bd9Sstevel@tonic-gate 	{ "services", irs_sv },
987c478bd9Sstevel@tonic-gate 	{ "protocols", irs_pr },
997c478bd9Sstevel@tonic-gate 	{ "hosts", irs_ho },
1007c478bd9Sstevel@tonic-gate 	{ "networks", irs_nw },
1017c478bd9Sstevel@tonic-gate 	{ "netgroup", irs_ng },
1027c478bd9Sstevel@tonic-gate 	{ NULL, irs_nmap }
1037c478bd9Sstevel@tonic-gate };
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static const struct nameval option_names[] = {
1067c478bd9Sstevel@tonic-gate 	{ "merge", IRS_MERGE },
1077c478bd9Sstevel@tonic-gate 	{ "continue", IRS_CONTINUE },
1087c478bd9Sstevel@tonic-gate 	{ NULL, 0 }
1097c478bd9Sstevel@tonic-gate };
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /* Forward */
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate static void		gen_close(struct irs_acc *);
1147c478bd9Sstevel@tonic-gate static struct __res_state * gen_res_get(struct irs_acc *);
1157c478bd9Sstevel@tonic-gate static void		gen_res_set(struct irs_acc *, struct __res_state *,
1167c478bd9Sstevel@tonic-gate 				    void (*)(void *));
1177c478bd9Sstevel@tonic-gate static int		find_name(const char *, const struct nameval nv[]);
1187c478bd9Sstevel@tonic-gate static void 		init_map_rules(struct gen_p *, const char *conf_file);
1197c478bd9Sstevel@tonic-gate static struct irs_rule *release_rule(struct irs_rule *);
1207c478bd9Sstevel@tonic-gate static int		add_rule(struct gen_p *,
1217c478bd9Sstevel@tonic-gate 				 enum irs_map_id, enum irs_acc_id,
1227c478bd9Sstevel@tonic-gate 				 const char *);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /* Public */
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate struct irs_acc *
irs_gen_acc(const char * options,const char * conf_file)1277c478bd9Sstevel@tonic-gate irs_gen_acc(const char *options, const char *conf_file) {
1287c478bd9Sstevel@tonic-gate 	struct irs_acc *acc;
1297c478bd9Sstevel@tonic-gate 	struct gen_p *irs;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if (!(acc = memget(sizeof *acc))) {
1327c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
1337c478bd9Sstevel@tonic-gate 		return (NULL);
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 	memset(acc, 0x5e, sizeof *acc);
1367c478bd9Sstevel@tonic-gate 	if (!(irs = memget(sizeof *irs))) {
1377c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
1387c478bd9Sstevel@tonic-gate 		memput(acc, sizeof *acc);
1397c478bd9Sstevel@tonic-gate 		return (NULL);
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 	memset(irs, 0x5e, sizeof *irs);
1427c478bd9Sstevel@tonic-gate 	irs->options = strdup(options);
1437c478bd9Sstevel@tonic-gate 	irs->res = NULL;
1447c478bd9Sstevel@tonic-gate 	irs->free_res = NULL;
1457c478bd9Sstevel@tonic-gate 	memset(irs->accessors, 0, sizeof irs->accessors);
1467c478bd9Sstevel@tonic-gate 	memset(irs->map_rules, 0, sizeof irs->map_rules);
1477c478bd9Sstevel@tonic-gate 	init_map_rules(irs, conf_file);
1487c478bd9Sstevel@tonic-gate 	acc->private = irs;
1497c478bd9Sstevel@tonic-gate #ifdef WANT_IRS_GR
1507c478bd9Sstevel@tonic-gate 	acc->gr_map = irs_gen_gr;
1517c478bd9Sstevel@tonic-gate #else
1527c478bd9Sstevel@tonic-gate 	acc->gr_map = NULL;
1537c478bd9Sstevel@tonic-gate #endif
1547c478bd9Sstevel@tonic-gate #ifdef WANT_IRS_PW
1557c478bd9Sstevel@tonic-gate 	acc->pw_map = irs_gen_pw;
1567c478bd9Sstevel@tonic-gate #else
1577c478bd9Sstevel@tonic-gate 	acc->pw_map = NULL;
1587c478bd9Sstevel@tonic-gate #endif
1597c478bd9Sstevel@tonic-gate 	acc->sv_map = irs_gen_sv;
1607c478bd9Sstevel@tonic-gate 	acc->pr_map = irs_gen_pr;
1617c478bd9Sstevel@tonic-gate 	acc->ho_map = irs_gen_ho;
1627c478bd9Sstevel@tonic-gate 	acc->nw_map = irs_gen_nw;
1637c478bd9Sstevel@tonic-gate 	acc->ng_map = irs_gen_ng;
1647c478bd9Sstevel@tonic-gate 	acc->res_get = gen_res_get;
1657c478bd9Sstevel@tonic-gate 	acc->res_set = gen_res_set;
1667c478bd9Sstevel@tonic-gate 	acc->close = gen_close;
1677c478bd9Sstevel@tonic-gate 	return (acc);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /* Methods */
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate static struct __res_state *
gen_res_get(struct irs_acc * this)1737c478bd9Sstevel@tonic-gate gen_res_get(struct irs_acc *this) {
1747c478bd9Sstevel@tonic-gate 	struct gen_p *irs = (struct gen_p *)this->private;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	if (irs->res == NULL) {
1777c478bd9Sstevel@tonic-gate 		struct __res_state *res;
1787c478bd9Sstevel@tonic-gate 		res = (struct __res_state *)malloc(sizeof *res);
1797c478bd9Sstevel@tonic-gate 		if (res == NULL)
1807c478bd9Sstevel@tonic-gate 			return (NULL);
1817c478bd9Sstevel@tonic-gate 		memset(res, 0, sizeof *res);
1827c478bd9Sstevel@tonic-gate 		gen_res_set(this, res, free);
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 
185*9525b14bSRao Shoaib 	if (((irs->res->options & RES_INIT) == 0U) && res_ninit(irs->res) < 0)
1867c478bd9Sstevel@tonic-gate 		return (NULL);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	return (irs->res);
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate static void
gen_res_set(struct irs_acc * this,struct __res_state * res,void (* free_res)(void *))1927c478bd9Sstevel@tonic-gate gen_res_set(struct irs_acc *this, struct __res_state *res,
1937c478bd9Sstevel@tonic-gate 	    void (*free_res)(void *)) {
1947c478bd9Sstevel@tonic-gate 	struct gen_p *irs = (struct gen_p *)this->private;
1957c478bd9Sstevel@tonic-gate #if 0
1967c478bd9Sstevel@tonic-gate 	struct irs_rule *rule;
1977c478bd9Sstevel@tonic-gate 	struct irs_ho *ho;
1987c478bd9Sstevel@tonic-gate 	struct irs_nw *nw;
1997c478bd9Sstevel@tonic-gate #endif
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (irs->res && irs->free_res) {
2027c478bd9Sstevel@tonic-gate 		res_nclose(irs->res);
2037c478bd9Sstevel@tonic-gate 		(*irs->free_res)(irs->res);
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	irs->res = res;
2077c478bd9Sstevel@tonic-gate 	irs->free_res = free_res;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate #if 0
2107c478bd9Sstevel@tonic-gate 	for (rule = irs->map_rules[irs_ho]; rule; rule = rule->next) {
2117c478bd9Sstevel@tonic-gate                 ho = rule->inst->ho;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate                 (*ho->res_set)(ho, res, NULL);
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 	for (rule = irs->map_rules[irs_nw]; rule; rule = rule->next) {
2167c478bd9Sstevel@tonic-gate                 nw = rule->inst->nw;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate                 (*nw->res_set)(nw, res, NULL);
2197c478bd9Sstevel@tonic-gate 	}
2207c478bd9Sstevel@tonic-gate #endif
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate static void
gen_close(struct irs_acc * this)2247c478bd9Sstevel@tonic-gate gen_close(struct irs_acc *this) {
2257c478bd9Sstevel@tonic-gate 	struct gen_p *irs = (struct gen_p *)this->private;
2267c478bd9Sstevel@tonic-gate 	int n;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	/* Search rules. */
2297c478bd9Sstevel@tonic-gate 	for (n = 0; n < irs_nmap; n++)
2307c478bd9Sstevel@tonic-gate 		while (irs->map_rules[n] != NULL)
2317c478bd9Sstevel@tonic-gate 			irs->map_rules[n] = release_rule(irs->map_rules[n]);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	/* Access methods. */
2347c478bd9Sstevel@tonic-gate 	for (n = 0; n < irs_nacc; n++) {
2357c478bd9Sstevel@tonic-gate 		/* Map objects. */
2367c478bd9Sstevel@tonic-gate 		if (irs->accessors[n].gr != NULL)
2377c478bd9Sstevel@tonic-gate 			(*irs->accessors[n].gr->close)(irs->accessors[n].gr);
2387c478bd9Sstevel@tonic-gate 		if (irs->accessors[n].pw != NULL)
2397c478bd9Sstevel@tonic-gate 			(*irs->accessors[n].pw->close)(irs->accessors[n].pw);
2407c478bd9Sstevel@tonic-gate 		if (irs->accessors[n].sv != NULL)
2417c478bd9Sstevel@tonic-gate 			(*irs->accessors[n].sv->close)(irs->accessors[n].sv);
2427c478bd9Sstevel@tonic-gate 		if (irs->accessors[n].pr != NULL)
2437c478bd9Sstevel@tonic-gate 			(*irs->accessors[n].pr->close)(irs->accessors[n].pr);
2447c478bd9Sstevel@tonic-gate 		if (irs->accessors[n].ho != NULL)
2457c478bd9Sstevel@tonic-gate 			(*irs->accessors[n].ho->close)(irs->accessors[n].ho);
2467c478bd9Sstevel@tonic-gate 		if (irs->accessors[n].nw != NULL)
2477c478bd9Sstevel@tonic-gate 			(*irs->accessors[n].nw->close)(irs->accessors[n].nw);
2487c478bd9Sstevel@tonic-gate 		if (irs->accessors[n].ng != NULL)
2497c478bd9Sstevel@tonic-gate 			(*irs->accessors[n].ng->close)(irs->accessors[n].ng);
2507c478bd9Sstevel@tonic-gate 		/* Enclosing accessor. */
2517c478bd9Sstevel@tonic-gate 		if (irs->accessors[n].acc != NULL)
2527c478bd9Sstevel@tonic-gate 			(*irs->accessors[n].acc->close)(irs->accessors[n].acc);
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	/* The options string was strdup'd. */
2567c478bd9Sstevel@tonic-gate 	free((void*)irs->options);
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	if (irs->res && irs->free_res)
2597c478bd9Sstevel@tonic-gate 		(*irs->free_res)(irs->res);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	/* The private data container. */
2627c478bd9Sstevel@tonic-gate 	memput(irs, sizeof *irs);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	/* The object. */
2657c478bd9Sstevel@tonic-gate 	memput(this, sizeof *this);
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate /* Private */
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate static int
find_name(const char * name,const struct nameval names[])2717c478bd9Sstevel@tonic-gate find_name(const char *name, const struct nameval names[]) {
2727c478bd9Sstevel@tonic-gate 	int n;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	for (n = 0; names[n].name != NULL; n++)
2757c478bd9Sstevel@tonic-gate 		if (strcmp(name, names[n].name) == 0)
2767c478bd9Sstevel@tonic-gate 			return (names[n].val);
2777c478bd9Sstevel@tonic-gate 	return (-1);
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate static struct irs_rule *
release_rule(struct irs_rule * rule)2817c478bd9Sstevel@tonic-gate release_rule(struct irs_rule *rule) {
2827c478bd9Sstevel@tonic-gate 	struct irs_rule *next = rule->next;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	memput(rule, sizeof *rule);
2857c478bd9Sstevel@tonic-gate 	return (next);
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate static int
add_rule(struct gen_p * irs,enum irs_map_id map,enum irs_acc_id acc,const char * options)2897c478bd9Sstevel@tonic-gate add_rule(struct gen_p *irs,
2907c478bd9Sstevel@tonic-gate 	 enum irs_map_id map, enum irs_acc_id acc,
2917c478bd9Sstevel@tonic-gate 	 const char *options)
2927c478bd9Sstevel@tonic-gate {
2937c478bd9Sstevel@tonic-gate 	struct irs_rule **rules, *last, *tmp, *new;
2947c478bd9Sstevel@tonic-gate 	struct irs_inst *inst;
2957c478bd9Sstevel@tonic-gate 	const char *cp;
2967c478bd9Sstevel@tonic-gate 	int n;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate #ifndef WANT_IRS_GR
2997c478bd9Sstevel@tonic-gate 	if (map == irs_gr)
3007c478bd9Sstevel@tonic-gate 		return (-1);
3017c478bd9Sstevel@tonic-gate #endif
3027c478bd9Sstevel@tonic-gate #ifndef WANT_IRS_PW
3037c478bd9Sstevel@tonic-gate 	if (map == irs_pw)
3047c478bd9Sstevel@tonic-gate 		return (-1);
3057c478bd9Sstevel@tonic-gate #endif
3067c478bd9Sstevel@tonic-gate #ifndef WANT_IRS_NIS
3077c478bd9Sstevel@tonic-gate 	if (acc == irs_nis)
3087c478bd9Sstevel@tonic-gate 		return (-1);
3097c478bd9Sstevel@tonic-gate #endif
3107c478bd9Sstevel@tonic-gate 	new = memget(sizeof *new);
3117c478bd9Sstevel@tonic-gate 	if (new == NULL)
3127c478bd9Sstevel@tonic-gate 		return (-1);
3137c478bd9Sstevel@tonic-gate 	memset(new, 0x5e, sizeof *new);
3147c478bd9Sstevel@tonic-gate 	new->next = NULL;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	new->inst = &irs->accessors[acc];
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	new->flags = 0;
3197c478bd9Sstevel@tonic-gate 	cp = options;
3207c478bd9Sstevel@tonic-gate 	while (cp && *cp) {
3217c478bd9Sstevel@tonic-gate 		char option[50], *next;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 		next = strchr(cp, ',');
3247c478bd9Sstevel@tonic-gate 		if (next)
3257c478bd9Sstevel@tonic-gate 			n = next++ - cp;
3267c478bd9Sstevel@tonic-gate 		else
3277c478bd9Sstevel@tonic-gate 			n = strlen(cp);
3287c478bd9Sstevel@tonic-gate 		if ((size_t)n > sizeof option - 1)
3297c478bd9Sstevel@tonic-gate 			n = sizeof option - 1;
3307c478bd9Sstevel@tonic-gate 		strncpy(option, cp, n);
3317c478bd9Sstevel@tonic-gate 		option[n] = '\0';
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 		n = find_name(option, option_names);
3347c478bd9Sstevel@tonic-gate 		if (n >= 0)
3357c478bd9Sstevel@tonic-gate 			new->flags |= n;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 		cp = next;
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	rules = &irs->map_rules[map];
3417c478bd9Sstevel@tonic-gate 	for (last = NULL, tmp = *rules;
3427c478bd9Sstevel@tonic-gate 	     tmp != NULL;
3437c478bd9Sstevel@tonic-gate 	     last = tmp, tmp = tmp->next)
3447c478bd9Sstevel@tonic-gate 		(void)NULL;
3457c478bd9Sstevel@tonic-gate 	if (last == NULL)
3467c478bd9Sstevel@tonic-gate 		*rules = new;
3477c478bd9Sstevel@tonic-gate 	else
3487c478bd9Sstevel@tonic-gate 		last->next = new;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	/* Try to instantiate map accessors for this if necessary & approp. */
3517c478bd9Sstevel@tonic-gate 	inst = &irs->accessors[acc];
3527c478bd9Sstevel@tonic-gate 	if (inst->acc == NULL && accs[acc] != NULL)
3537c478bd9Sstevel@tonic-gate 		inst->acc = (*accs[acc])(irs->options);
3547c478bd9Sstevel@tonic-gate 	if (inst->acc != NULL) {
3557c478bd9Sstevel@tonic-gate 		if (inst->gr == NULL && inst->acc->gr_map != NULL)
3567c478bd9Sstevel@tonic-gate 			inst->gr = (*inst->acc->gr_map)(inst->acc);
3577c478bd9Sstevel@tonic-gate 		if (inst->pw == NULL && inst->acc->pw_map != NULL)
3587c478bd9Sstevel@tonic-gate 			inst->pw = (*inst->acc->pw_map)(inst->acc);
3597c478bd9Sstevel@tonic-gate 		if (inst->sv == NULL && inst->acc->sv_map != NULL)
3607c478bd9Sstevel@tonic-gate 			inst->sv = (*inst->acc->sv_map)(inst->acc);
3617c478bd9Sstevel@tonic-gate 		if (inst->pr == NULL && inst->acc->pr_map != NULL)
3627c478bd9Sstevel@tonic-gate 			inst->pr = (*inst->acc->pr_map)(inst->acc);
3637c478bd9Sstevel@tonic-gate 		if (inst->ho == NULL && inst->acc->ho_map != NULL)
3647c478bd9Sstevel@tonic-gate 			inst->ho = (*inst->acc->ho_map)(inst->acc);
3657c478bd9Sstevel@tonic-gate 		if (inst->nw == NULL && inst->acc->nw_map != NULL)
3667c478bd9Sstevel@tonic-gate 			inst->nw = (*inst->acc->nw_map)(inst->acc);
3677c478bd9Sstevel@tonic-gate 		if (inst->ng == NULL && inst->acc->ng_map != NULL)
3687c478bd9Sstevel@tonic-gate 			inst->ng = (*inst->acc->ng_map)(inst->acc);
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	return (0);
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate static void
default_map_rules(struct gen_p * irs)3757c478bd9Sstevel@tonic-gate default_map_rules(struct gen_p *irs) {
3767c478bd9Sstevel@tonic-gate 	/* Install time honoured and proved BSD style rules as default. */
3777c478bd9Sstevel@tonic-gate 	add_rule(irs, irs_gr, irs_lcl, "");
3787c478bd9Sstevel@tonic-gate 	add_rule(irs, irs_pw, irs_lcl, "");
3797c478bd9Sstevel@tonic-gate 	add_rule(irs, irs_sv, irs_lcl, "");
3807c478bd9Sstevel@tonic-gate 	add_rule(irs, irs_pr, irs_lcl, "");
3817c478bd9Sstevel@tonic-gate #ifdef SUNW_HOSTS_FALLBACK
3827c478bd9Sstevel@tonic-gate 	if (__res_no_hosts_fallback())
3837c478bd9Sstevel@tonic-gate 		add_rule(irs, irs_ho, irs_dns, "");
3847c478bd9Sstevel@tonic-gate 	else {
3857c478bd9Sstevel@tonic-gate 		add_rule(irs, irs_ho, irs_dns, "continue");
3867c478bd9Sstevel@tonic-gate 		add_rule(irs, irs_ho, irs_lcl, "");
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate #else  /* SUNW_HOSTS_FALLBACK */
3897c478bd9Sstevel@tonic-gate 	add_rule(irs, irs_ho, irs_dns, "continue");
3907c478bd9Sstevel@tonic-gate 	add_rule(irs, irs_ho, irs_lcl, "");
3917c478bd9Sstevel@tonic-gate #endif /* SUNW_HOSTS_FALLBACK */
3927c478bd9Sstevel@tonic-gate 	add_rule(irs, irs_nw, irs_dns, "continue");
3937c478bd9Sstevel@tonic-gate 	add_rule(irs, irs_nw, irs_lcl, "");
3947c478bd9Sstevel@tonic-gate 	add_rule(irs, irs_ng, irs_lcl, "");
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate static void
init_map_rules(struct gen_p * irs,const char * conf_file)3987c478bd9Sstevel@tonic-gate init_map_rules(struct gen_p *irs, const char *conf_file) {
3997c478bd9Sstevel@tonic-gate 	char line[1024], pattern[40], mapname[20], accname[20], options[100];
4007c478bd9Sstevel@tonic-gate 	FILE *conf;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate #ifdef SUNW_HOSTS_FALLBACK
4037c478bd9Sstevel@tonic-gate 	if (__res_no_hosts_fallback()) {
4047c478bd9Sstevel@tonic-gate 		default_map_rules(irs);
4057c478bd9Sstevel@tonic-gate 		return;
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate #endif /* SUNW_HOSTS_FALLBACK */
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	if (conf_file == NULL)
4107c478bd9Sstevel@tonic-gate 		conf_file = _PATH_IRS_CONF ;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	/* A conf file of "" means compiled in defaults. Irpd wants this */
4137c478bd9Sstevel@tonic-gate 	if (conf_file[0] == '\0' || (conf = fopen(conf_file, "r")) == NULL) {
4147c478bd9Sstevel@tonic-gate 		default_map_rules(irs);
4157c478bd9Sstevel@tonic-gate 		return;
4167c478bd9Sstevel@tonic-gate 	}
417*9525b14bSRao Shoaib 	(void) sprintf(pattern, "%%%lus %%%lus %%%lus\n",
418*9525b14bSRao Shoaib 		       (unsigned long)sizeof mapname,
419*9525b14bSRao Shoaib 		       (unsigned long)sizeof accname,
420*9525b14bSRao Shoaib 		       (unsigned long)sizeof options);
4217c478bd9Sstevel@tonic-gate 	while (fgets(line, sizeof line, conf)) {
4227c478bd9Sstevel@tonic-gate 		enum irs_map_id map;
4237c478bd9Sstevel@tonic-gate 		enum irs_acc_id acc;
4247c478bd9Sstevel@tonic-gate 		char *tmp;
4257c478bd9Sstevel@tonic-gate 		int n;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 		for (tmp = line;
4287c478bd9Sstevel@tonic-gate 		     isascii((unsigned char)*tmp) &&
4297c478bd9Sstevel@tonic-gate 		     isspace((unsigned char)*tmp);
4307c478bd9Sstevel@tonic-gate 		     tmp++)
4317c478bd9Sstevel@tonic-gate 			(void)NULL;
4327c478bd9Sstevel@tonic-gate 		if (*tmp == '#' || *tmp == '\n' || *tmp == '\0')
4337c478bd9Sstevel@tonic-gate 			continue;
4347c478bd9Sstevel@tonic-gate 		n = sscanf(tmp, pattern, mapname, accname, options);
4357c478bd9Sstevel@tonic-gate 		if (n < 2)
4367c478bd9Sstevel@tonic-gate 			continue;
4377c478bd9Sstevel@tonic-gate 		if (n < 3)
4387c478bd9Sstevel@tonic-gate 			options[0] = '\0';
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 		n = find_name(mapname, map_names);
4417c478bd9Sstevel@tonic-gate 		INSIST(n < irs_nmap);
4427c478bd9Sstevel@tonic-gate 		if (n < 0)
4437c478bd9Sstevel@tonic-gate 			continue;
4447c478bd9Sstevel@tonic-gate 		map = (enum irs_map_id) n;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 		n = find_name(accname, acc_names);
4477c478bd9Sstevel@tonic-gate 		INSIST(n < irs_nacc);
4487c478bd9Sstevel@tonic-gate 		if (n < 0)
4497c478bd9Sstevel@tonic-gate 			continue;
4507c478bd9Sstevel@tonic-gate 		acc = (enum irs_acc_id) n;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 		add_rule(irs, map, acc, options);
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate 	fclose(conf);
4557c478bd9Sstevel@tonic-gate }
456