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