1 /* $NetBSD: nsdispatch.c,v 1.20 2004/05/24 16:16:26 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.20 2004/05/24 16:16:26 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 return (-1); 249 _nsmap = new; 250 } 251 memmove(&_nsmap[_nsmapsize++], dbt, sizeof(ns_dbt)); 252 return (0); 253 } 254 255 256 int 257 /*ARGSUSED*/ 258 nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database, 259 const char *method, const ns_src defaults[], ...) 260 { 261 va_list ap; 262 int i, curdisp, result; 263 const ns_dbt *dbt; 264 const ns_src *srclist; 265 int srclistsize; 266 267 _DIAGASSERT(database != NULL); 268 _DIAGASSERT(method != NULL); 269 if (database == NULL || method == NULL) 270 return (NS_UNAVAIL); 271 272 dbt = _nsdbtget(database); 273 if (dbt != NULL) { 274 srclist = dbt->srclist; 275 srclistsize = dbt->srclistsize; 276 } else { 277 srclist = defaults; 278 srclistsize = 0; 279 while (srclist[srclistsize].name != NULL) 280 srclistsize++; 281 } 282 result = 0; 283 284 for (i = 0; i < srclistsize; i++) { 285 for (curdisp = 0; disp_tab[curdisp].src != NULL; curdisp++) 286 if (strcasecmp(disp_tab[curdisp].src, 287 srclist[i].name) == 0) 288 break; 289 result = 0; 290 if (disp_tab[curdisp].callback) { 291 va_start(ap, defaults); 292 result = disp_tab[curdisp].callback(retval, 293 disp_tab[curdisp].cb_data, ap); 294 va_end(ap); 295 if (result & srclist[i].flags) { 296 break; 297 } 298 } 299 } 300 return (result ? result : NS_NOTFOUND); 301 } 302