Home | History | Annotate | Line # | Download | only in net
nsdispatch.c revision 1.12
      1 /*	$NetBSD: nsdispatch.c,v 1.12 1999/09/16 11:45:16 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 #if defined(LIBC_SCCS) && !defined(lint)
     41 __RCSID("$NetBSD: nsdispatch.c,v 1.12 1999/09/16 11:45:16 lukem 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 #ifdef __weak_alias
     67 __weak_alias(nsdispatch,_nsdispatch);
     68 #endif
     69 
     70 
     71 /*
     72  * default sourcelist: `files'
     73  */
     74 const ns_src __nsdefaultsrc[] = {
     75 	{ NSSRC_FILES, NS_SUCCESS },
     76 	{ 0 },
     77 };
     78 
     79 
     80 static	int			 _nsmapsize = 0;
     81 static	ns_dbt			*_nsmap = NULL;
     82 
     83 /*
     84  * size of dynamic array chunk for _nsmap and _nsmap[x].srclist
     85  */
     86 #define NSELEMSPERCHUNK		8
     87 
     88 
     89 int	_nscmp __P((const void *, const void *));
     90 
     91 
     92 int
     93 _nscmp(a, b)
     94 	const void *a;
     95 	const void *b;
     96 {
     97 	return (strcasecmp(((const ns_dbt *)a)->name,
     98 	    ((const ns_dbt *)b)->name));
     99 }
    100 
    101 
    102 void
    103 _nsdbtaddsrc(dbt, src)
    104 	ns_dbt		*dbt;
    105 	const ns_src	*src;
    106 {
    107 
    108 	_DIAGASSERT(dbt != NULL);
    109 	_DIAGASSERT(src != NULL);
    110 #ifdef _DIAGNOSTIC
    111 	if (dbt == NULL || src == NULL)
    112 		return;
    113 #endif
    114 
    115 	if ((dbt->srclistsize % NSELEMSPERCHUNK) == 0) {
    116 		dbt->srclist = (ns_src *)realloc(dbt->srclist,
    117 		    (dbt->srclistsize + NSELEMSPERCHUNK) * sizeof(ns_src));
    118 		if (dbt->srclist == NULL)
    119 			err(1, "nsdispatch: memory allocation failure");
    120 	}
    121 	memmove(&dbt->srclist[dbt->srclistsize++], src, sizeof(ns_src));
    122 }
    123 
    124 
    125 void
    126 _nsdbtdump(dbt)
    127 	const ns_dbt *dbt;
    128 {
    129 	int i;
    130 
    131 	_DIAGASSERT(dbt != NULL);
    132 #ifdef _DIAGNOSTIC
    133 	if (dbt == NULL)
    134 		return;
    135 #endif
    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 	extern	FILE 	*_nsyyin;
    170 	extern	int	 _nsyyparse __P((void));
    171 
    172 	_DIAGASSERT(name != NULL);
    173 #ifdef _DIAGNOSTIC
    174 	if (name == NULL)
    175 		return (NULL);
    176 #endif
    177 
    178 	dbt.name = name;
    179 
    180 	if (confmod) {
    181 		if (stat(_PATH_NS_CONF, &statbuf) == -1)
    182 			return (NULL);
    183 		if (confmod < statbuf.st_mtime) {
    184 			int i, j;
    185 
    186 			for (i = 0; i < _nsmapsize; i++) {
    187 				for (j = 0; j < _nsmap[i].srclistsize; j++) {
    188 					if (_nsmap[i].srclist[j].name != NULL) {
    189 						/*LINTED const cast*/
    190 						free((void *)
    191 						    _nsmap[i].srclist[j].name);
    192 					}
    193 				}
    194 				if (_nsmap[i].srclist)
    195 					free(_nsmap[i].srclist);
    196 				if (_nsmap[i].name) {
    197 					/*LINTED const cast*/
    198 					free((void *)_nsmap[i].name);
    199 				}
    200 			}
    201 			if (_nsmap)
    202 				free(_nsmap);
    203 			_nsmap = NULL;
    204 			_nsmapsize = 0;
    205 			confmod = 0;
    206 		}
    207 	}
    208 	if (!confmod) {
    209 		if (stat(_PATH_NS_CONF, &statbuf) == -1)
    210 			return (NULL);
    211 		_nsyyin = fopen(_PATH_NS_CONF, "r");
    212 		if (_nsyyin == NULL)
    213 			return (NULL);
    214 		_nsyyparse();
    215 		(void)fclose(_nsyyin);
    216 		qsort(_nsmap, (size_t)_nsmapsize, sizeof(ns_dbt), _nscmp);
    217 		confmod = statbuf.st_mtime;
    218 	}
    219 	return (bsearch(&dbt, _nsmap, (size_t)_nsmapsize, sizeof(ns_dbt),
    220 	    _nscmp));
    221 }
    222 
    223 
    224 void
    225 _nsdbtput(dbt)
    226 	const ns_dbt *dbt;
    227 {
    228 	int	i;
    229 
    230 	_DIAGASSERT(dbt != NULL);
    231 #ifdef _DIAGNOSTIC
    232 	if (dbt == NULL)
    233 		return;
    234 #endif
    235 
    236 	for (i = 0; i < _nsmapsize; i++) {
    237 		if (_nscmp(dbt, &_nsmap[i]) == 0) {
    238 					/* overwrite existing entry */
    239 			if (_nsmap[i].srclist != NULL)
    240 				free(_nsmap[i].srclist);
    241 			memmove(&_nsmap[i], dbt, sizeof(ns_dbt));
    242 			return;
    243 		}
    244 	}
    245 
    246 	if ((_nsmapsize % NSELEMSPERCHUNK) == 0) {
    247 		_nsmap = (ns_dbt *)realloc(_nsmap,
    248 		    (_nsmapsize + NSELEMSPERCHUNK) * sizeof(ns_dbt));
    249 		if (_nsmap == NULL)
    250 			err(1, "nsdispatch: memory allocation failure");
    251 	}
    252 	memmove(&_nsmap[_nsmapsize++], dbt, sizeof(ns_dbt));
    253 }
    254 
    255 
    256 int
    257 /*ARGSUSED*/
    258 #if __STDC__
    259 nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
    260 	    const char *method, const ns_src defaults[], ...)
    261 #else
    262 nsdispatch(retval, disp_tab, database, method, defaults, va_alist)
    263 	void		*retval;
    264 	const ns_dtab	 disp_tab[];
    265 	const char	*database;
    266 	const char	*method;
    267 	const ns_src	 defaults[];
    268 	va_dcl
    269 #endif
    270 {
    271 	va_list		 ap;
    272 	int		 i, curdisp, result;
    273 	const ns_dbt	*dbt;
    274 	const ns_src	*srclist;
    275 	int		 srclistsize;
    276 
    277 	_DIAGASSERT(database != NULL);
    278 	_DIAGASSERT(method != NULL);
    279 #ifdef _DIAGNOSTIC
    280 	if (database == NULL || method == NULL)
    281 		return (NS_UNAVAIL);
    282 #endif
    283 
    284 	dbt = _nsdbtget(database);
    285 	if (dbt != NULL) {
    286 		srclist = dbt->srclist;
    287 		srclistsize = dbt->srclistsize;
    288 	} else {
    289 		srclist = defaults;
    290 		srclistsize = 0;
    291 		while (srclist[srclistsize].name != NULL)
    292 			srclistsize++;
    293 	}
    294 	result = 0;
    295 
    296 	for (i = 0; i < srclistsize; i++) {
    297 		for (curdisp = 0; disp_tab[curdisp].src != NULL; curdisp++)
    298 			if (strcasecmp(disp_tab[curdisp].src,
    299 			    srclist[i].name) == 0)
    300 				break;
    301 		result = 0;
    302 		if (disp_tab[curdisp].callback) {
    303 #if __STDC__
    304 			va_start(ap, defaults);
    305 #else
    306 			va_start(ap);
    307 #endif
    308 			result = disp_tab[curdisp].callback(retval,
    309 			    disp_tab[curdisp].cb_data, ap);
    310 			va_end(ap);
    311 			if (result & srclist[i].flags) {
    312 				break;
    313 			}
    314 		}
    315 	}
    316 	return (result ? result : NS_NOTFOUND);
    317 }
    318