Home | History | Annotate | Line # | Download | only in net
nsdispatch.c revision 1.1.2.1
      1  1.1.2.1  lukem /*-
      2  1.1.2.1  lukem  * Copyright 1995,1996 Luke Mewburn <lm (at) werj.com.au>.  All rights reserved.
      3  1.1.2.1  lukem  *
      4  1.1.2.1  lukem  * Redistribution and use in source and binary forms, with or without
      5  1.1.2.1  lukem  * modification, are permitted provided that the following conditions
      6  1.1.2.1  lukem  * are met:
      7  1.1.2.1  lukem  * 1. Redistributions of source code must retain the above copyright
      8  1.1.2.1  lukem  *    notice, this list of conditions and the following disclaimer.
      9  1.1.2.1  lukem  * 2. Redistributions in binary form must reproduce the above copyright
     10  1.1.2.1  lukem  *    notice, this list of conditions and the following disclaimer in the
     11  1.1.2.1  lukem  *    documentation and/or other materials provided with the distribution.
     12  1.1.2.1  lukem  * 3. All advertising materials mentioning features or use of this software
     13  1.1.2.1  lukem  *    must display the following acknowledgement:
     14  1.1.2.1  lukem  * 	This product includes software developed by Luke Mewburn.
     15  1.1.2.1  lukem  * 4. The name of the author may not be used to endorse or promote products
     16  1.1.2.1  lukem  *    derived from this software without specific prior written permission.
     17  1.1.2.1  lukem  *
     18  1.1.2.1  lukem  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.1.2.1  lukem  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.1.2.1  lukem  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.1.2.1  lukem  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.1.2.1  lukem  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  1.1.2.1  lukem  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     24  1.1.2.1  lukem  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     25  1.1.2.1  lukem  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
     26  1.1.2.1  lukem  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
     27  1.1.2.1  lukem  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1.2.1  lukem  */
     29  1.1.2.1  lukem 
     30  1.1.2.1  lukem #include <sys/types.h>
     31  1.1.2.1  lukem #include <sys/param.h>
     32  1.1.2.1  lukem #include <sys/stat.h>
     33  1.1.2.1  lukem 
     34  1.1.2.1  lukem #include <fcntl.h>
     35  1.1.2.1  lukem #include <stdio.h>
     36  1.1.2.1  lukem #include <stdlib.h>
     37  1.1.2.1  lukem #include <string.h>
     38  1.1.2.1  lukem #include <unistd.h>
     39  1.1.2.1  lukem 
     40  1.1.2.1  lukem #include "nsswitch.h"
     41  1.1.2.1  lukem 
     42  1.1.2.1  lukem static int	  _nsmapsize = 0;
     43  1.1.2.1  lukem static ns_DBT	 *_nsmap = NULL;
     44  1.1.2.1  lukem /*
     45  1.1.2.1  lukem  * number of ns_DBT per chunk in _nsmap. calculated along the lines of
     46  1.1.2.1  lukem  *	NSELEMSPERCHUNK * sizeof(ns_DBT) < (1024 - some slop)
     47  1.1.2.1  lukem  * this is to help power of 2 mallocs which are really wasteful if the
     48  1.1.2.1  lukem  * amount just overflows a power of 2 boundary.
     49  1.1.2.1  lukem  */
     50  1.1.2.1  lukem #define NSELEMSPERCHUNK		17
     51  1.1.2.1  lukem 
     52  1.1.2.1  lukem int
     53  1.1.2.1  lukem _nscmp(const void *a, const void *b)
     54  1.1.2.1  lukem {
     55  1.1.2.1  lukem     return strncmp(((ns_DBT*)a)->name, ((ns_DBT *)b)->name, NS_MAXDBLEN);
     56  1.1.2.1  lukem } /* _nscmp */
     57  1.1.2.1  lukem 
     58  1.1.2.1  lukem void
     59  1.1.2.1  lukem __nsdbput(dbt)
     60  1.1.2.1  lukem 	const ns_DBT *dbt;
     61  1.1.2.1  lukem {
     62  1.1.2.1  lukem 	int	i;
     63  1.1.2.1  lukem 
     64  1.1.2.1  lukem 	for (i = 0; i < _nsmapsize; i++) {
     65  1.1.2.1  lukem 		if (_nscmp(dbt, &_nsmap[i]) == 0) {
     66  1.1.2.1  lukem 			memmove((void *)&_nsmap[i], (void *)dbt,
     67  1.1.2.1  lukem 				sizeof(ns_DBT));
     68  1.1.2.1  lukem 			return;
     69  1.1.2.1  lukem 		}
     70  1.1.2.1  lukem 	}
     71  1.1.2.1  lukem 
     72  1.1.2.1  lukem 	if ((_nsmapsize % NSELEMSPERCHUNK) == 0) {
     73  1.1.2.1  lukem 		_nsmap = realloc(_nsmap,
     74  1.1.2.1  lukem 			    (_nsmapsize + NSELEMSPERCHUNK) * sizeof(ns_DBT));
     75  1.1.2.1  lukem 		if (_nsmap == NULL)
     76  1.1.2.1  lukem 			_err(1, "nsdispatch: %m");
     77  1.1.2.1  lukem 
     78  1.1.2.1  lukem 	}
     79  1.1.2.1  lukem 	memmove((void *)&_nsmap[_nsmapsize++], (void *)dbt, sizeof(ns_DBT));
     80  1.1.2.1  lukem } /* __nsdbput */
     81  1.1.2.1  lukem 
     82  1.1.2.1  lukem 
     83  1.1.2.1  lukem /*
     84  1.1.2.1  lukem  * __nsdbget --
     85  1.1.2.1  lukem  *	args: ns_DBT *dbt
     86  1.1.2.1  lukem  *	modifies: dbt
     87  1.1.2.1  lukem  *	looks for the element in dbt->name and finds from there
     88  1.1.2.1  lukem  *
     89  1.1.2.1  lukem  */
     90  1.1.2.1  lukem void
     91  1.1.2.1  lukem __nsdbget(dbt)
     92  1.1.2.1  lukem 	ns_DBT *dbt;
     93  1.1.2.1  lukem {
     94  1.1.2.1  lukem #ifdef NSLINEAR		/* XXX: just to compare linear vs bsearch */
     95  1.1.2.1  lukem 	int	i;
     96  1.1.2.1  lukem 	for (i = 0; i < _nsmapsize; i++) {
     97  1.1.2.1  lukem 		if (_nscmp(dbt, &_nsmap[i]) == 0) {
     98  1.1.2.1  lukem 			memmove((void *)dbt, (void *)&_nsmap[i],
     99  1.1.2.1  lukem 				sizeof(ns_DBT));
    100  1.1.2.1  lukem 			return;
    101  1.1.2.1  lukem 		}
    102  1.1.2.1  lukem 	}
    103  1.1.2.1  lukem #else
    104  1.1.2.1  lukem 	ns_DBT *e;
    105  1.1.2.1  lukem 	e = bsearch(dbt, _nsmap, _nsmapsize, sizeof(ns_DBT), _nscmp);
    106  1.1.2.1  lukem 	if (e != NULL)
    107  1.1.2.1  lukem 		memmove((void *)dbt, (void *)e, sizeof(ns_DBT));
    108  1.1.2.1  lukem #endif
    109  1.1.2.1  lukem } /* __nsdbget */
    110  1.1.2.1  lukem 
    111  1.1.2.1  lukem 
    112  1.1.2.1  lukem void
    113  1.1.2.1  lukem _nsdumpdbt(dbt)
    114  1.1.2.1  lukem 	const ns_DBT *dbt;
    115  1.1.2.1  lukem {
    116  1.1.2.1  lukem 	int i;
    117  1.1.2.1  lukem 	printf("%s:\n", dbt->name);
    118  1.1.2.1  lukem 	for (i = 0; i < dbt->size; i++) {
    119  1.1.2.1  lukem 		int source = (dbt->map[i] & NS_SOURCEMASK);
    120  1.1.2.1  lukem 		int status = (dbt->map[i] & NS_STATUSMASK);
    121  1.1.2.1  lukem 
    122  1.1.2.1  lukem 		printf("  %2d: [%x] ", i, dbt->map[i]);
    123  1.1.2.1  lukem 		if (!dbt->map[i]) {
    124  1.1.2.1  lukem 		    printf(" (empty)\n");
    125  1.1.2.1  lukem 		    continue;
    126  1.1.2.1  lukem 		}
    127  1.1.2.1  lukem 		switch(source) {
    128  1.1.2.1  lukem 		case NS_FILES:		printf(" files"); break;
    129  1.1.2.1  lukem 		case NS_DNS:		printf(" dns"); break;
    130  1.1.2.1  lukem 		case NS_NIS:		printf(" nis"); break;
    131  1.1.2.1  lukem 		case NS_NISPLUS:	printf(" nisplus"); break;
    132  1.1.2.1  lukem 		case NS_COMPAT:		printf(" compat"); break;
    133  1.1.2.1  lukem 		default:		printf(" BADSOURCE(%x)", source); break;
    134  1.1.2.1  lukem 		}
    135  1.1.2.1  lukem 
    136  1.1.2.1  lukem 		if (!(status & NS_SUCCESS))
    137  1.1.2.1  lukem 			printf(" SUCCESS=continue");
    138  1.1.2.1  lukem 		if (status & NS_UNAVAIL)
    139  1.1.2.1  lukem 			printf(" UNAVAIL=return");
    140  1.1.2.1  lukem 		if (status & NS_NOTFOUND)
    141  1.1.2.1  lukem 			printf(" NOTFOUND=return");
    142  1.1.2.1  lukem 		if (status & NS_TRYAGAIN)
    143  1.1.2.1  lukem 			printf(" TRYAGAIN=return");
    144  1.1.2.1  lukem 		printf("\n");
    145  1.1.2.1  lukem 	}
    146  1.1.2.1  lukem } /* nsdumpdbt */
    147  1.1.2.1  lukem 
    148  1.1.2.1  lukem 
    149  1.1.2.1  lukem void
    150  1.1.2.1  lukem _nsgetdbt(src, dbt)
    151  1.1.2.1  lukem 	const char	*src;
    152  1.1.2.1  lukem 	ns_DBT		*dbt;
    153  1.1.2.1  lukem {
    154  1.1.2.1  lukem 	static time_t	 confmod;
    155  1.1.2.1  lukem 	struct stat	 statbuf;
    156  1.1.2.1  lukem 
    157  1.1.2.1  lukem 	extern FILE 	*_nsyyin;
    158  1.1.2.1  lukem 	extern int	_nsyyparse();
    159  1.1.2.1  lukem 
    160  1.1.2.1  lukem 	strncpy(dbt->name, src, NS_MAXDBLEN);
    161  1.1.2.1  lukem 	dbt->name[NS_MAXDBLEN - 1] = '\0';
    162  1.1.2.1  lukem 	dbt->size = 1;
    163  1.1.2.1  lukem 	dbt->map[0] = NS_DEFAULTMAP;		/* default to 'files' */
    164  1.1.2.1  lukem 
    165  1.1.2.1  lukem 	if (confmod) {
    166  1.1.2.1  lukem 		if (stat(_PATH_NS_CONF, &statbuf) == -1)
    167  1.1.2.1  lukem 			return;
    168  1.1.2.1  lukem 		if (confmod < statbuf.st_mtime) {
    169  1.1.2.1  lukem 			free(_nsmap);
    170  1.1.2.1  lukem 			_nsmap = NULL;
    171  1.1.2.1  lukem 			_nsmapsize = 0;
    172  1.1.2.1  lukem 			confmod = 0;
    173  1.1.2.1  lukem 		}
    174  1.1.2.1  lukem 	}
    175  1.1.2.1  lukem 	if (!confmod) {
    176  1.1.2.1  lukem 		if (stat(_PATH_NS_CONF, &statbuf) == -1)
    177  1.1.2.1  lukem 			return;
    178  1.1.2.1  lukem 		_nsyyin = fopen(_PATH_NS_CONF, "r");
    179  1.1.2.1  lukem 		if (_nsyyin == NULL)
    180  1.1.2.1  lukem 			return;
    181  1.1.2.1  lukem 		_nsyyparse();
    182  1.1.2.1  lukem #ifndef NSLINEAR
    183  1.1.2.1  lukem 		qsort(_nsmap, _nsmapsize, sizeof(ns_DBT), _nscmp);
    184  1.1.2.1  lukem #endif
    185  1.1.2.1  lukem 		(void)fclose(_nsyyin);
    186  1.1.2.1  lukem 		confmod = statbuf.st_mtime;
    187  1.1.2.1  lukem 	}
    188  1.1.2.1  lukem 	__nsdbget(dbt);
    189  1.1.2.1  lukem } /* nsgetdbt */
    190  1.1.2.1  lukem 
    191  1.1.2.1  lukem 
    192  1.1.2.1  lukem int
    193  1.1.2.1  lukem #if __STDC__
    194  1.1.2.1  lukem nsdispatch(void *retval, ns_dtab disp_tab, const char *database, ...)
    195  1.1.2.1  lukem #else
    196  1.1.2.1  lukem nsdispatch(retval, disp_tab, database, va_alist)
    197  1.1.2.1  lukem 	void		*retval;
    198  1.1.2.1  lukem 	ns_dtab		disp_tab;
    199  1.1.2.1  lukem 	const char	*database;
    200  1.1.2.1  lukem 	va_dcl
    201  1.1.2.1  lukem #endif
    202  1.1.2.1  lukem {
    203  1.1.2.1  lukem 	va_list	ap;
    204  1.1.2.1  lukem 	int	curdisp, result = 0;
    205  1.1.2.1  lukem 	ns_DBT	dbt;
    206  1.1.2.1  lukem 
    207  1.1.2.1  lukem 	_nsgetdbt(database, &dbt);
    208  1.1.2.1  lukem 
    209  1.1.2.1  lukem #if NSDEBUG
    210  1.1.2.1  lukem 	_nsdumpdbt(&dbt);
    211  1.1.2.1  lukem 	fprintf(stderr, "nsdispatch: %s\n", database);
    212  1.1.2.1  lukem #endif
    213  1.1.2.1  lukem 	for (curdisp = 0; curdisp < dbt.size; curdisp++) {
    214  1.1.2.1  lukem 		int source = (dbt.map[curdisp] & NS_SOURCEMASK);
    215  1.1.2.1  lukem #if NSDEBUG
    216  1.1.2.1  lukem 		fprintf(stderr, "    %2d: source=%d", curdisp, source);
    217  1.1.2.1  lukem #endif
    218  1.1.2.1  lukem 		if (source < 0 || source >= NS_MAXSOURCE)
    219  1.1.2.1  lukem 			continue;
    220  1.1.2.1  lukem 
    221  1.1.2.1  lukem 		result = 0;
    222  1.1.2.1  lukem 		if (disp_tab[source].cb) {
    223  1.1.2.1  lukem #if __STDC__
    224  1.1.2.1  lukem 			va_start(ap, database);
    225  1.1.2.1  lukem #else
    226  1.1.2.1  lukem 			va_start(ap);
    227  1.1.2.1  lukem #endif
    228  1.1.2.1  lukem 			result = disp_tab[source].cb(retval,
    229  1.1.2.1  lukem 						disp_tab[source].cb_data, ap);
    230  1.1.2.1  lukem 			va_end(ap);
    231  1.1.2.1  lukem #if NSDEBUG
    232  1.1.2.1  lukem 			fprintf(stderr, " result=%d (%d)", result,
    233  1.1.2.1  lukem 			    (result & (dbt.map[curdisp] & NS_STATUSMASK)));
    234  1.1.2.1  lukem #endif
    235  1.1.2.1  lukem 			if (result & (dbt.map[curdisp] & NS_STATUSMASK)) {
    236  1.1.2.1  lukem #if NSDEBUG
    237  1.1.2.1  lukem 				fprintf(stderr, " MATCH!\n");
    238  1.1.2.1  lukem #endif
    239  1.1.2.1  lukem 				break;
    240  1.1.2.1  lukem 			}
    241  1.1.2.1  lukem 		}
    242  1.1.2.1  lukem #if NSDEBUG
    243  1.1.2.1  lukem 		fprintf(stderr, "\n");
    244  1.1.2.1  lukem #endif
    245  1.1.2.1  lukem 	}
    246  1.1.2.1  lukem 	return (result ? result : NS_NOTFOUND);
    247  1.1.2.1  lukem } /* nsdispatch */
    248