Home | History | Annotate | Line # | Download | only in net
nsdispatch.c revision 1.15
      1 /*	$NetBSD: nsdispatch.c,v 1.15 1999/11/28 05:46:15 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.15 1999/11/28 05:46:15 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 int
    103 _nsdbtaddsrc(dbt, src)
    104 	ns_dbt		*dbt;
    105 	const ns_src	*src;
    106 {
    107 
    108 	_DIAGASSERT(dbt != NULL);
    109 	_DIAGASSERT(src != NULL);
    110 
    111 	if ((dbt->srclistsize % NSELEMSPERCHUNK) == 0) {
    112 		ns_src *new;
    113 
    114 		new = (ns_src *)realloc(dbt->srclist,
    115 		    (dbt->srclistsize + NSELEMSPERCHUNK) * sizeof(ns_src));
    116 		if (new == NULL)
    117 			return (-1);
    118 		dbt->srclist = new;
    119 	}
    120 	memmove(&dbt->srclist[dbt->srclistsize++], src, sizeof(ns_src));
    121 	return (0);
    122 }
    123 
    124 
    125 void
    126 _nsdbtdump(dbt)
    127 	const ns_dbt *dbt;
    128 {
    129 	int i;
    130 
    131 	_DIAGASSERT(dbt != NULL);
    132 
    133 	printf("%s (%d source%s):", dbt->name, dbt->srclistsize,
    134 	    dbt->srclistsize == 1 ? "" : "s");
    135 	for (i = 0; i < dbt->srclistsize; i++) {
    136 		printf(" %s", dbt->srclist[i].name);
    137 		if (!(dbt->srclist[i].flags &
    138 		    (NS_UNAVAIL|NS_NOTFOUND|NS_TRYAGAIN)) &&
    139 		    (dbt->srclist[i].flags & NS_SUCCESS))
    140 			continue;
    141 		printf(" [");
    142 		if (!(dbt->srclist[i].flags & NS_SUCCESS))
    143 			printf(" SUCCESS=continue");
    144 		if (dbt->srclist[i].flags & NS_UNAVAIL)
    145 			printf(" UNAVAIL=return");
    146 		if (dbt->srclist[i].flags & NS_NOTFOUND)
    147 			printf(" NOTFOUND=return");
    148 		if (dbt->srclist[i].flags & NS_TRYAGAIN)
    149 			printf(" TRYAGAIN=return");
    150 		printf(" ]");
    151 	}
    152 	printf("\n");
    153 }
    154 
    155 
    156 const ns_dbt *
    157 _nsdbtget(name)
    158 	const char	*name;
    159 {
    160 	static	time_t	 confmod;
    161 
    162 	struct stat	 statbuf;
    163 	ns_dbt		 dbt;
    164 
    165 	extern	FILE 	*_nsyyin;
    166 	extern	int	 _nsyyparse __P((void));
    167 
    168 	_DIAGASSERT(name != NULL);
    169 
    170 	dbt.name = name;
    171 
    172 	if (confmod) {
    173 		if (stat(_PATH_NS_CONF, &statbuf) == -1)
    174 			return (NULL);
    175 		if (confmod < statbuf.st_mtime) {
    176 			int i, j;
    177 
    178 			for (i = 0; i < _nsmapsize; i++) {
    179 				for (j = 0; j < _nsmap[i].srclistsize; j++) {
    180 					if (_nsmap[i].srclist[j].name != NULL) {
    181 						/*LINTED const cast*/
    182 						free((void *)
    183 						    _nsmap[i].srclist[j].name);
    184 					}
    185 				}
    186 				if (_nsmap[i].srclist)
    187 					free(_nsmap[i].srclist);
    188 				if (_nsmap[i].name) {
    189 					/*LINTED const cast*/
    190 					free((void *)_nsmap[i].name);
    191 				}
    192 			}
    193 			if (_nsmap)
    194 				free(_nsmap);
    195 			_nsmap = NULL;
    196 			_nsmapsize = 0;
    197 			confmod = 0;
    198 		}
    199 	}
    200 	if (!confmod) {
    201 		if (stat(_PATH_NS_CONF, &statbuf) == -1)
    202 			return (NULL);
    203 		_nsyyin = fopen(_PATH_NS_CONF, "r");
    204 		if (_nsyyin == NULL)
    205 			return (NULL);
    206 		_nsyyparse();
    207 		(void)fclose(_nsyyin);
    208 		qsort(_nsmap, (size_t)_nsmapsize, sizeof(ns_dbt), _nscmp);
    209 		confmod = statbuf.st_mtime;
    210 	}
    211 	return (bsearch(&dbt, _nsmap, (size_t)_nsmapsize, sizeof(ns_dbt),
    212 	    _nscmp));
    213 }
    214 
    215 
    216 int
    217 _nsdbtput(dbt)
    218 	const ns_dbt *dbt;
    219 {
    220 	int	i;
    221 
    222 	_DIAGASSERT(dbt != NULL);
    223 
    224 	for (i = 0; i < _nsmapsize; i++) {
    225 		if (_nscmp(dbt, &_nsmap[i]) == 0) {
    226 					/* overwrite existing entry */
    227 			if (_nsmap[i].srclist != NULL)
    228 				free(_nsmap[i].srclist);
    229 			memmove(&_nsmap[i], dbt, sizeof(ns_dbt));
    230 			return (0);
    231 		}
    232 	}
    233 
    234 	if ((_nsmapsize % NSELEMSPERCHUNK) == 0) {
    235 		ns_dbt *new;
    236 
    237 		new = (ns_dbt *)realloc(_nsmap,
    238 		    (_nsmapsize + NSELEMSPERCHUNK) * sizeof(ns_dbt));
    239 		if (new == NULL)
    240 			return (-1);
    241 		_nsmap = new;
    242 	}
    243 	memmove(&_nsmap[_nsmapsize++], dbt, sizeof(ns_dbt));
    244 	return (0);
    245 }
    246 
    247 
    248 int
    249 /*ARGSUSED*/
    250 #if __STDC__
    251 nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
    252 	    const char *method, const ns_src defaults[], ...)
    253 #else
    254 nsdispatch(retval, disp_tab, database, method, defaults, va_alist)
    255 	void		*retval;
    256 	const ns_dtab	 disp_tab[];
    257 	const char	*database;
    258 	const char	*method;
    259 	const ns_src	 defaults[];
    260 	va_dcl
    261 #endif
    262 {
    263 	va_list		 ap;
    264 	int		 i, curdisp, result;
    265 	const ns_dbt	*dbt;
    266 	const ns_src	*srclist;
    267 	int		 srclistsize;
    268 
    269 	_DIAGASSERT(database != NULL);
    270 	_DIAGASSERT(method != NULL);
    271 	if (database == NULL || method == NULL)
    272 		return (NS_UNAVAIL);
    273 
    274 	dbt = _nsdbtget(database);
    275 	if (dbt != NULL) {
    276 		srclist = dbt->srclist;
    277 		srclistsize = dbt->srclistsize;
    278 	} else {
    279 		srclist = defaults;
    280 		srclistsize = 0;
    281 		while (srclist[srclistsize].name != NULL)
    282 			srclistsize++;
    283 	}
    284 	result = 0;
    285 
    286 	for (i = 0; i < srclistsize; i++) {
    287 		for (curdisp = 0; disp_tab[curdisp].src != NULL; curdisp++)
    288 			if (strcasecmp(disp_tab[curdisp].src,
    289 			    srclist[i].name) == 0)
    290 				break;
    291 		result = 0;
    292 		if (disp_tab[curdisp].callback) {
    293 #if __STDC__
    294 			va_start(ap, defaults);
    295 #else
    296 			va_start(ap);
    297 #endif
    298 			result = disp_tab[curdisp].callback(retval,
    299 			    disp_tab[curdisp].cb_data, ap);
    300 			va_end(ap);
    301 			if (result & srclist[i].flags) {
    302 				break;
    303 			}
    304 		}
    305 	}
    306 	return (result ? result : NS_NOTFOUND);
    307 }
    308