xref: /netbsd-src/lib/libc/net/nsdispatch.c (revision 1ca5c1b28139779176bd5c13ad7c5f25c0bcd5f8)
1 /*	$NetBSD: nsdispatch.c,v 1.17 2000/12/20 20:51:08 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997, 1998, 1999 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.
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 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 __RCSID("$NetBSD: nsdispatch.c,v 1.17 2000/12/20 20:51:08 christos Exp $");
42 #endif /* LIBC_SCCS and not lint */
43 
44 #include "namespace.h"
45 
46 #include <sys/types.h>
47 #include <sys/param.h>
48 #include <sys/stat.h>
49 
50 #include <assert.h>
51 #include <err.h>
52 #include <fcntl.h>
53 #define _NS_PRIVATE
54 #include <nsswitch.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 
60 #ifdef __STDC__
61 #include <stdarg.h>
62 #else
63 #include <varargs.h>
64 #endif
65 
66 extern	FILE 	*_nsyyin;
67 extern	int	 _nsyyparse __P((void));
68 
69 
70 #ifdef __weak_alias
71 __weak_alias(nsdispatch,_nsdispatch)
72 #endif
73 
74 
75 /*
76  * default sourcelist: `files'
77  */
78 const ns_src __nsdefaultsrc[] = {
79 	{ NSSRC_FILES, NS_SUCCESS },
80 	{ 0 },
81 };
82 
83 
84 static	int			 _nsmapsize = 0;
85 static	ns_dbt			*_nsmap = NULL;
86 
87 /*
88  * size of dynamic array chunk for _nsmap and _nsmap[x].srclist
89  */
90 #define NSELEMSPERCHUNK		8
91 
92 
93 int	_nscmp __P((const void *, const void *));
94 
95 
96 int
97 _nscmp(a, b)
98 	const void *a;
99 	const void *b;
100 {
101 	return (strcasecmp(((const ns_dbt *)a)->name,
102 	    ((const ns_dbt *)b)->name));
103 }
104 
105 
106 int
107 _nsdbtaddsrc(dbt, src)
108 	ns_dbt		*dbt;
109 	const ns_src	*src;
110 {
111 
112 	_DIAGASSERT(dbt != NULL);
113 	_DIAGASSERT(src != NULL);
114 
115 	if ((dbt->srclistsize % NSELEMSPERCHUNK) == 0) {
116 		ns_src *new;
117 
118 		new = (ns_src *)realloc(dbt->srclist,
119 		    (dbt->srclistsize + NSELEMSPERCHUNK) * sizeof(ns_src));
120 		if (new == NULL)
121 			return (-1);
122 		dbt->srclist = new;
123 	}
124 	memmove(&dbt->srclist[dbt->srclistsize++], src, sizeof(ns_src));
125 	return (0);
126 }
127 
128 
129 void
130 _nsdbtdump(dbt)
131 	const ns_dbt *dbt;
132 {
133 	int i;
134 
135 	_DIAGASSERT(dbt != NULL);
136 
137 	printf("%s (%d source%s):", dbt->name, dbt->srclistsize,
138 	    dbt->srclistsize == 1 ? "" : "s");
139 	for (i = 0; i < dbt->srclistsize; i++) {
140 		printf(" %s", dbt->srclist[i].name);
141 		if (!(dbt->srclist[i].flags &
142 		    (NS_UNAVAIL|NS_NOTFOUND|NS_TRYAGAIN)) &&
143 		    (dbt->srclist[i].flags & NS_SUCCESS))
144 			continue;
145 		printf(" [");
146 		if (!(dbt->srclist[i].flags & NS_SUCCESS))
147 			printf(" SUCCESS=continue");
148 		if (dbt->srclist[i].flags & NS_UNAVAIL)
149 			printf(" UNAVAIL=return");
150 		if (dbt->srclist[i].flags & NS_NOTFOUND)
151 			printf(" NOTFOUND=return");
152 		if (dbt->srclist[i].flags & NS_TRYAGAIN)
153 			printf(" TRYAGAIN=return");
154 		printf(" ]");
155 	}
156 	printf("\n");
157 }
158 
159 
160 const ns_dbt *
161 _nsdbtget(name)
162 	const char	*name;
163 {
164 	static	time_t	 confmod;
165 
166 	struct stat	 statbuf;
167 	ns_dbt		 dbt;
168 
169 	_DIAGASSERT(name != NULL);
170 
171 	dbt.name = name;
172 
173 	if (confmod) {
174 		if (stat(_PATH_NS_CONF, &statbuf) == -1)
175 			return (NULL);
176 		if (confmod < statbuf.st_mtime) {
177 			int i, j;
178 
179 			for (i = 0; i < _nsmapsize; i++) {
180 				for (j = 0; j < _nsmap[i].srclistsize; j++) {
181 					if (_nsmap[i].srclist[j].name != NULL) {
182 						/*LINTED const cast*/
183 						free((void *)
184 						    _nsmap[i].srclist[j].name);
185 					}
186 				}
187 				if (_nsmap[i].srclist)
188 					free(_nsmap[i].srclist);
189 				if (_nsmap[i].name) {
190 					/*LINTED const cast*/
191 					free((void *)_nsmap[i].name);
192 				}
193 			}
194 			if (_nsmap)
195 				free(_nsmap);
196 			_nsmap = NULL;
197 			_nsmapsize = 0;
198 			confmod = 0;
199 		}
200 	}
201 	if (!confmod) {
202 		if (stat(_PATH_NS_CONF, &statbuf) == -1)
203 			return (NULL);
204 		_nsyyin = fopen(_PATH_NS_CONF, "r");
205 		if (_nsyyin == NULL)
206 			return (NULL);
207 		_nsyyparse();
208 		(void)fclose(_nsyyin);
209 		qsort(_nsmap, (size_t)_nsmapsize, sizeof(ns_dbt), _nscmp);
210 		confmod = statbuf.st_mtime;
211 	}
212 	return (bsearch(&dbt, _nsmap, (size_t)_nsmapsize, sizeof(ns_dbt),
213 	    _nscmp));
214 }
215 
216 
217 int
218 _nsdbtput(dbt)
219 	const ns_dbt *dbt;
220 {
221 	int	i;
222 
223 	_DIAGASSERT(dbt != NULL);
224 
225 	for (i = 0; i < _nsmapsize; i++) {
226 		if (_nscmp(dbt, &_nsmap[i]) == 0) {
227 					/* overwrite existing entry */
228 			if (_nsmap[i].srclist != NULL)
229 				free(_nsmap[i].srclist);
230 			memmove(&_nsmap[i], dbt, sizeof(ns_dbt));
231 			return (0);
232 		}
233 	}
234 
235 	if ((_nsmapsize % NSELEMSPERCHUNK) == 0) {
236 		ns_dbt *new;
237 
238 		new = (ns_dbt *)realloc(_nsmap,
239 		    (_nsmapsize + NSELEMSPERCHUNK) * sizeof(ns_dbt));
240 		if (new == NULL)
241 			return (-1);
242 		_nsmap = new;
243 	}
244 	memmove(&_nsmap[_nsmapsize++], dbt, sizeof(ns_dbt));
245 	return (0);
246 }
247 
248 
249 int
250 /*ARGSUSED*/
251 #if __STDC__
252 nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
253 	    const char *method, const ns_src defaults[], ...)
254 #else
255 nsdispatch(retval, disp_tab, database, method, defaults, va_alist)
256 	void		*retval;
257 	const ns_dtab	 disp_tab[];
258 	const char	*database;
259 	const char	*method;
260 	const ns_src	 defaults[];
261 	va_dcl
262 #endif
263 {
264 	va_list		 ap;
265 	int		 i, curdisp, result;
266 	const ns_dbt	*dbt;
267 	const ns_src	*srclist;
268 	int		 srclistsize;
269 
270 	_DIAGASSERT(database != NULL);
271 	_DIAGASSERT(method != NULL);
272 	if (database == NULL || method == NULL)
273 		return (NS_UNAVAIL);
274 
275 	dbt = _nsdbtget(database);
276 	if (dbt != NULL) {
277 		srclist = dbt->srclist;
278 		srclistsize = dbt->srclistsize;
279 	} else {
280 		srclist = defaults;
281 		srclistsize = 0;
282 		while (srclist[srclistsize].name != NULL)
283 			srclistsize++;
284 	}
285 	result = 0;
286 
287 	for (i = 0; i < srclistsize; i++) {
288 		for (curdisp = 0; disp_tab[curdisp].src != NULL; curdisp++)
289 			if (strcasecmp(disp_tab[curdisp].src,
290 			    srclist[i].name) == 0)
291 				break;
292 		result = 0;
293 		if (disp_tab[curdisp].callback) {
294 #if __STDC__
295 			va_start(ap, defaults);
296 #else
297 			va_start(ap);
298 #endif
299 			result = disp_tab[curdisp].callback(retval,
300 			    disp_tab[curdisp].cb_data, ap);
301 			va_end(ap);
302 			if (result & srclist[i].flags) {
303 				break;
304 			}
305 		}
306 	}
307 	return (result ? result : NS_NOTFOUND);
308 }
309