Home | History | Annotate | Line # | Download | only in net
nsdispatch.c revision 1.19
      1 /*	$NetBSD: nsdispatch.c,v 1.19 2004/05/23 16:53:22 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.19 2004/05/23 16:53:22 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 <stdarg.h>
     56 #include <stdio.h>
     57 #include <stdlib.h>
     58 #include <string.h>
     59 #include <unistd.h>
     60 #include <threadlib.h>
     61 
     62 extern	FILE 	*_nsyyin;
     63 extern	int	 _nsyyparse __P((void));
     64 
     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 #ifdef _REENTRANT
     83 static 	mutex_t			 _nsmutex = MUTEX_INITIALIZER;
     84 #define NSLOCK()		mutex_lock(&_nsmutex)
     85 #define NSUNLOCK()		mutex_unlock(&_nsmutex)
     86 #else
     87 #define NSLOCK()
     88 #define NSUNLOCK()
     89 #endif
     90 
     91 
     92 /*
     93  * size of dynamic array chunk for _nsmap and _nsmap[x].srclist
     94  */
     95 #define NSELEMSPERCHUNK		8
     96 
     97 
     98 int	_nscmp __P((const void *, const void *));
     99 
    100 
    101 int
    102 _nscmp(a, b)
    103 	const void *a;
    104 	const void *b;
    105 {
    106 	return (strcasecmp(((const ns_dbt *)a)->name,
    107 	    ((const ns_dbt *)b)->name));
    108 }
    109 
    110 
    111 int
    112 _nsdbtaddsrc(dbt, src)
    113 	ns_dbt		*dbt;
    114 	const ns_src	*src;
    115 {
    116 
    117 	_DIAGASSERT(dbt != NULL);
    118 	_DIAGASSERT(src != NULL);
    119 
    120 	if ((dbt->srclistsize % NSELEMSPERCHUNK) == 0) {
    121 		ns_src *new;
    122 
    123 		new = (ns_src *)realloc(dbt->srclist,
    124 		    (dbt->srclistsize + NSELEMSPERCHUNK) * sizeof(ns_src));
    125 		if (new == NULL)
    126 			return (-1);
    127 		dbt->srclist = new;
    128 	}
    129 	memmove(&dbt->srclist[dbt->srclistsize++], src, sizeof(ns_src));
    130 	return (0);
    131 }
    132 
    133 
    134 void
    135 _nsdbtdump(dbt)
    136 	const ns_dbt *dbt;
    137 {
    138 	int i;
    139 
    140 	_DIAGASSERT(dbt != NULL);
    141 
    142 	printf("%s (%d source%s):", dbt->name, dbt->srclistsize,
    143 	    dbt->srclistsize == 1 ? "" : "s");
    144 	for (i = 0; i < dbt->srclistsize; i++) {
    145 		printf(" %s", dbt->srclist[i].name);
    146 		if (!(dbt->srclist[i].flags &
    147 		    (NS_UNAVAIL|NS_NOTFOUND|NS_TRYAGAIN)) &&
    148 		    (dbt->srclist[i].flags & NS_SUCCESS))
    149 			continue;
    150 		printf(" [");
    151 		if (!(dbt->srclist[i].flags & NS_SUCCESS))
    152 			printf(" SUCCESS=continue");
    153 		if (dbt->srclist[i].flags & NS_UNAVAIL)
    154 			printf(" UNAVAIL=return");
    155 		if (dbt->srclist[i].flags & NS_NOTFOUND)
    156 			printf(" NOTFOUND=return");
    157 		if (dbt->srclist[i].flags & NS_TRYAGAIN)
    158 			printf(" TRYAGAIN=return");
    159 		printf(" ]");
    160 	}
    161 	printf("\n");
    162 }
    163 
    164 
    165 const ns_dbt *
    166 _nsdbtget(name)
    167 	const char	*name;
    168 {
    169 	static	time_t	 confmod;
    170 
    171 	struct stat	 statbuf;
    172 	ns_dbt		 dbt;
    173 
    174 	_DIAGASSERT(name != NULL);
    175 
    176 	dbt.name = name;
    177 
    178 	NSLOCK();
    179 	if (stat(_PATH_NS_CONF, &statbuf) == -1)
    180 		return (NULL);
    181 	if (confmod) {
    182 		if (confmod < statbuf.st_mtime) {
    183 			int i, j;
    184 
    185 			for (i = 0; i < _nsmapsize; i++) {
    186 				for (j = 0; j < _nsmap[i].srclistsize; j++) {
    187 					if (_nsmap[i].srclist[j].name != NULL) {
    188 						/*LINTED const cast*/
    189 						free((void *)
    190 						    _nsmap[i].srclist[j].name);
    191 					}
    192 				}
    193 				if (_nsmap[i].srclist)
    194 					free(_nsmap[i].srclist);
    195 				if (_nsmap[i].name) {
    196 					/*LINTED const cast*/
    197 					free((void *)_nsmap[i].name);
    198 				}
    199 			}
    200 			if (_nsmap)
    201 				free(_nsmap);
    202 			_nsmap = NULL;
    203 			_nsmapsize = 0;
    204 			confmod = 0;
    205 		}
    206 	}
    207 	if (!confmod) {
    208 		_nsyyin = fopen(_PATH_NS_CONF, "r");
    209 		if (_nsyyin == NULL) {
    210 			NSUNLOCK();
    211 			return (NULL);
    212 		}
    213 		_nsyyparse();
    214 		(void)fclose(_nsyyin);
    215 		qsort(_nsmap, (size_t)_nsmapsize, sizeof(ns_dbt), _nscmp);
    216 		confmod = statbuf.st_mtime;
    217 	}
    218 	NSUNLOCK();
    219 	return (bsearch(&dbt, _nsmap, (size_t)_nsmapsize, sizeof(ns_dbt),
    220 	    _nscmp));
    221 }
    222 
    223 
    224 int
    225 _nsdbtput(dbt)
    226 	const ns_dbt *dbt;
    227 {
    228 	int	i;
    229 
    230 	_DIAGASSERT(dbt != NULL);
    231 
    232 	for (i = 0; i < _nsmapsize; i++) {
    233 		if (_nscmp(dbt, &_nsmap[i]) == 0) {
    234 					/* overwrite existing entry */
    235 			if (_nsmap[i].srclist != NULL)
    236 				free(_nsmap[i].srclist);
    237 			memmove(&_nsmap[i], dbt, sizeof(ns_dbt));
    238 			return (0);
    239 		}
    240 	}
    241 
    242 	if ((_nsmapsize % NSELEMSPERCHUNK) == 0) {
    243 		ns_dbt *new;
    244 
    245 		new = (ns_dbt *)realloc(_nsmap,
    246 		    (_nsmapsize + NSELEMSPERCHUNK) * sizeof(ns_dbt));
    247 		if (new == NULL) {
    248 			NSUNLOCK();
    249 			return (-1);
    250 		}
    251 		_nsmap = new;
    252 	}
    253 	memmove(&_nsmap[_nsmapsize++], dbt, sizeof(ns_dbt));
    254 	return (0);
    255 }
    256 
    257 
    258 int
    259 /*ARGSUSED*/
    260 nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
    261 	    const char *method, const ns_src defaults[], ...)
    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 			va_start(ap, defaults);
    294 			result = disp_tab[curdisp].callback(retval,
    295 			    disp_tab[curdisp].cb_data, ap);
    296 			va_end(ap);
    297 			if (result & srclist[i].flags) {
    298 				break;
    299 			}
    300 		}
    301 	}
    302 	return (result ? result : NS_NOTFOUND);
    303 }
    304