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