1 /*	$NetBSD: nsdispatch.c,v 1.30 2005/11/29 03:11:59 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997, 1998, 1999, 2004 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Luke Mewburn; and by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*-
40  * Copyright (c) 2003 Networks Associates Technology, Inc.
41  * All rights reserved.
42  *
43  * Portions of this software were developed for the FreeBSD Project by
44  * Jacques A. Vidrine, Safeport Network Services, and Network
45  * Associates Laboratories, the Security Research Division of Network
46  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
47  * ("CBOSS"), as part of the DARPA CHATS research program.
48  *
49  * Redistribution and use in source and binary forms, with or without
50  * modification, are permitted provided that the following conditions
51  * are met:
52  * 1. Redistributions of source code must retain the above copyright
53  *    notice, this list of conditions and the following disclaimer.
54  * 2. Redistributions in binary form must reproduce the above copyright
55  *    notice, this list of conditions and the following disclaimer in the
56  *    documentation and/or other materials provided with the distribution.
57  *
58  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68  * SUCH DAMAGE.
69  */
70 
71 #include <sys/cdefs.h>
72 
73 #include <assert.h>
74 #include <errno.h>
75 #include <nsswitch.h>
76 #include <stdarg.h>
77 #include <strings.h>
78 
79 static nss_method
_nsmethod(const char * source,const char * database __unused,const char * method __unused,const ns_dtab disp_tab[],void ** cb_data)80 _nsmethod(const char *source, const char *database __unused, const char *method __unused,
81     const ns_dtab disp_tab[], void **cb_data)
82 {
83 	int	curdisp;
84 
85 	if (disp_tab != NULL) {
86 		for (curdisp = 0; disp_tab[curdisp].src != NULL; curdisp++) {
87 			if (strcasecmp(source, disp_tab[curdisp].src) == 0) {
88 				*cb_data = disp_tab[curdisp].cb_data;
89 				return (disp_tab[curdisp].callback);
90 			}
91 		}
92 	}
93 
94 	*cb_data = NULL;
95 	return (NULL);
96 }
97 
98 int
99 /*ARGSUSED*/
nsdispatch(void * retval,const ns_dtab disp_tab[],const char * database,const char * method,const ns_src defaults[],...)100 nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
101 	    const char *method, const ns_src defaults[], ...)
102 {
103 	va_list		 ap;
104 	int		 i, result;
105 	const ns_src	*srclist;
106 	int		 srclistsize;
107 	nss_method	 cb;
108 	void		*cb_data;
109 
110 	/* retval may be NULL */
111 	/* disp_tab may be NULL */
112 	assert(database != NULL);
113 	assert(method != NULL);
114 	assert(defaults != NULL);
115 	if (database == NULL || method == NULL || defaults == NULL)
116 		return (NS_UNAVAIL);
117 
118         srclist = defaults;
119         srclistsize = 0;
120         while (srclist[srclistsize].name != NULL)
121                 srclistsize++;
122 
123 	result = 0;
124 
125 	for (i = 0; i < srclistsize; i++) {
126 		cb = _nsmethod(srclist[i].name, database, method,
127 		    disp_tab, &cb_data);
128 		result = 0;
129 		if (cb != NULL) {
130 			va_start(ap, defaults);
131 			result = (*cb)(retval, cb_data, ap);
132 			va_end(ap);
133 			if (defaults[0].flags & NS_FORCEALL)
134 				continue;
135 			if (result & srclist[i].flags)
136 				break;
137 			/* Stop trying next resolver when there is a memory space fatal error. */
138 			if ((result & NS_UNAVAIL) != 0 && errno == ENOSPC) {
139 			  break;
140 			}
141 		}
142 	}
143 	result &= NS_STATUSMASK;	/* clear private flags in result */
144 
145 	return (result ? result : NS_NOTFOUND);
146 }
147