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