xref: /freebsd/lib/libc/net/nsdispatch.c (revision b52b9d56d4e96089873a75f9e29062eec19fabba)
1 /*	$NetBSD: nsdispatch.c,v 1.9 1999/01/25 00:16:17 lukem 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 __FBSDID("$FreeBSD$");
41 
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/stat.h>
45 
46 #include <err.h>
47 #include <fcntl.h>
48 #define _NS_PRIVATE
49 #include <nsswitch.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 
55 /*
56  * default sourcelist: `files'
57  */
58 const ns_src __nsdefaultsrc[] = {
59 	{ NSSRC_FILES, NS_SUCCESS },
60 	{ 0 },
61 };
62 
63 
64 static	int			 _nsmapsize = 0;
65 static	ns_dbt			*_nsmap = NULL;
66 
67 /*
68  * size of dynamic array chunk for _nsmap and _nsmap[x].srclist
69  */
70 #define NSELEMSPERCHUNK		8
71 
72 
73 int	_nscmp(const void *, const void *);
74 
75 
76 int
77 _nscmp(a, b)
78 	const void *a;
79 	const void *b;
80 {
81 	return (strcasecmp(((const ns_dbt *)a)->name,
82 	    ((const ns_dbt *)b)->name));
83 }
84 
85 
86 void
87 _nsdbtaddsrc(dbt, src)
88 	ns_dbt		*dbt;
89 	const ns_src	*src;
90 {
91 	if ((dbt->srclistsize % NSELEMSPERCHUNK) == 0) {
92 		dbt->srclist = (ns_src *)realloc(dbt->srclist,
93 		    (dbt->srclistsize + NSELEMSPERCHUNK) * sizeof(ns_src));
94 		if (dbt->srclist == NULL)
95 			_err(1, "nsdispatch: memory allocation failure");
96 	}
97 	memmove(&dbt->srclist[dbt->srclistsize++], src, sizeof(ns_src));
98 }
99 
100 
101 void
102 _nsdbtdump(dbt)
103 	const ns_dbt *dbt;
104 {
105 	int i;
106 
107 	printf("%s (%d source%s):", dbt->name, dbt->srclistsize,
108 	    dbt->srclistsize == 1 ? "" : "s");
109 	for (i = 0; i < dbt->srclistsize; i++) {
110 		printf(" %s", dbt->srclist[i].name);
111 		if (!(dbt->srclist[i].flags &
112 		    (NS_UNAVAIL|NS_NOTFOUND|NS_TRYAGAIN)) &&
113 		    (dbt->srclist[i].flags & NS_SUCCESS))
114 			continue;
115 		printf(" [");
116 		if (!(dbt->srclist[i].flags & NS_SUCCESS))
117 			printf(" SUCCESS=continue");
118 		if (dbt->srclist[i].flags & NS_UNAVAIL)
119 			printf(" UNAVAIL=return");
120 		if (dbt->srclist[i].flags & NS_NOTFOUND)
121 			printf(" NOTFOUND=return");
122 		if (dbt->srclist[i].flags & NS_TRYAGAIN)
123 			printf(" TRYAGAIN=return");
124 		printf(" ]");
125 	}
126 	printf("\n");
127 }
128 
129 
130 const ns_dbt *
131 _nsdbtget(name)
132 	const char	*name;
133 {
134 	static	time_t	 confmod;
135 
136 	struct stat	 statbuf;
137 	ns_dbt		 dbt;
138 
139 	extern	FILE 	*_nsyyin;
140 	extern	int	 _nsyyparse(void);
141 
142 	dbt.name = name;
143 
144 	if (confmod) {
145 		if (stat(_PATH_NS_CONF, &statbuf) == -1)
146 			return (NULL);
147 		if (confmod < statbuf.st_mtime) {
148 			int i, j;
149 
150 			for (i = 0; i < _nsmapsize; i++) {
151 				for (j = 0; j < _nsmap[i].srclistsize; j++) {
152 					if (_nsmap[i].srclist[j].name != NULL) {
153 						/*LINTED const cast*/
154 						free((void *)
155 						    _nsmap[i].srclist[j].name);
156 					}
157 				}
158 				if (_nsmap[i].srclist)
159 					free(_nsmap[i].srclist);
160 				if (_nsmap[i].name) {
161 					/*LINTED const cast*/
162 					free((void *)_nsmap[i].name);
163 				}
164 			}
165 			if (_nsmap)
166 				free(_nsmap);
167 			_nsmap = NULL;
168 			_nsmapsize = 0;
169 			confmod = 0;
170 		}
171 	}
172 	if (!confmod) {
173 		if (stat(_PATH_NS_CONF, &statbuf) == -1)
174 			return (NULL);
175 		_nsyyin = fopen(_PATH_NS_CONF, "r");
176 		if (_nsyyin == NULL)
177 			return (NULL);
178 		_nsyyparse();
179 		(void)fclose(_nsyyin);
180 		qsort(_nsmap, (size_t)_nsmapsize, sizeof(ns_dbt), _nscmp);
181 		confmod = statbuf.st_mtime;
182 	}
183 	return (bsearch(&dbt, _nsmap, (size_t)_nsmapsize, sizeof(ns_dbt),
184 	    _nscmp));
185 }
186 
187 
188 void
189 _nsdbtput(dbt)
190 	const ns_dbt *dbt;
191 {
192 	int	i;
193 
194 	for (i = 0; i < _nsmapsize; i++) {
195 		if (_nscmp(dbt, &_nsmap[i]) == 0) {
196 					/* overwrite existing entry */
197 			if (_nsmap[i].srclist != NULL)
198 				free(_nsmap[i].srclist);
199 			memmove(&_nsmap[i], dbt, sizeof(ns_dbt));
200 			return;
201 		}
202 	}
203 
204 	if ((_nsmapsize % NSELEMSPERCHUNK) == 0) {
205 		_nsmap = (ns_dbt *)realloc(_nsmap,
206 		    (_nsmapsize + NSELEMSPERCHUNK) * sizeof(ns_dbt));
207 		if (_nsmap == NULL)
208 			_err(1, "nsdispatch: memory allocation failure");
209 	}
210 	memmove(&_nsmap[_nsmapsize++], dbt, sizeof(ns_dbt));
211 }
212 
213 
214 int
215 nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
216 	    const char *method, const ns_src defaults[], ...)
217 {
218 	va_list		 ap;
219 	int		 i, curdisp, result;
220 	const ns_dbt	*dbt;
221 	const ns_src	*srclist;
222 	int		 srclistsize;
223 
224 	dbt = _nsdbtget(database);
225 	if (dbt != NULL) {
226 		srclist = dbt->srclist;
227 		srclistsize = dbt->srclistsize;
228 	} else {
229 		srclist = defaults;
230 		srclistsize = 0;
231 		while (srclist[srclistsize].name != NULL)
232 			srclistsize++;
233 	}
234 	result = 0;
235 
236 	for (i = 0; i < srclistsize; i++) {
237 		for (curdisp = 0; disp_tab[curdisp].src != NULL; curdisp++)
238 			if (strcasecmp(disp_tab[curdisp].src,
239 			    srclist[i].name) == 0)
240 				break;
241 		result = 0;
242 		if (disp_tab[curdisp].callback) {
243 			va_start(ap, defaults);
244 			result = disp_tab[curdisp].callback(retval,
245 			    disp_tab[curdisp].cb_data, ap);
246 			va_end(ap);
247 			if (result & srclist[i].flags) {
248 				break;
249 			}
250 		}
251 	}
252 	return (result ? result : NS_NOTFOUND);
253 }
254