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