Deleted Added
full compact
nsdispatch.c (127727) nsdispatch.c (158115)
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.

--- 54 unchanged lines hidden (view full) ---

63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * SUCH DAMAGE.
68 *
69 */
70#include <sys/cdefs.h>
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.

--- 54 unchanged lines hidden (view full) ---

63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * SUCH DAMAGE.
68 *
69 */
70#include <sys/cdefs.h>
71__FBSDID("$FreeBSD: head/lib/libc/net/nsdispatch.c 127727 2004-04-01 19:12:45Z nectar $");
71__FBSDID("$FreeBSD: head/lib/libc/net/nsdispatch.c 158115 2006-04-28 12:03:38Z ume $");
72
73#include "namespace.h"
74#include <sys/param.h>
75#include <sys/stat.h>
76
77#include <dlfcn.h>
78#include <errno.h>
79#include <fcntl.h>
80#define _NS_PRIVATE
81#include <nsswitch.h>
82#include <pthread.h>
83#include <stdio.h>
84#include <stdlib.h>
85#include <string.h>
86#include <syslog.h>
87#include <unistd.h>
88#include "un-namespace.h"
72
73#include "namespace.h"
74#include <sys/param.h>
75#include <sys/stat.h>
76
77#include <dlfcn.h>
78#include <errno.h>
79#include <fcntl.h>
80#define _NS_PRIVATE
81#include <nsswitch.h>
82#include <pthread.h>
83#include <stdio.h>
84#include <stdlib.h>
85#include <string.h>
86#include <syslog.h>
87#include <unistd.h>
88#include "un-namespace.h"
89#ifdef NS_CACHING
90#include "nscache.h"
91#endif
89
90enum _nss_constants {
91 /* Number of elements allocated when we grow a vector */
92 ELEMSPERCHUNK = 8
93};
94
95/*
96 * Global NSS data structures are mostly read-only, but we update

--- 22 unchanged lines hidden (view full) ---

119/* NSS modules. */
120static unsigned int _nsmodsize;
121static ns_mod *_nsmod;
122
123/* Placeholder for builtin modules' dlopen `handle'. */
124static int __nss_builtin_handle;
125static void *nss_builtin_handle = &__nss_builtin_handle;
126
92
93enum _nss_constants {
94 /* Number of elements allocated when we grow a vector */
95 ELEMSPERCHUNK = 8
96};
97
98/*
99 * Global NSS data structures are mostly read-only, but we update

--- 22 unchanged lines hidden (view full) ---

122/* NSS modules. */
123static unsigned int _nsmodsize;
124static ns_mod *_nsmod;
125
126/* Placeholder for builtin modules' dlopen `handle'. */
127static int __nss_builtin_handle;
128static void *nss_builtin_handle = &__nss_builtin_handle;
129
130#ifdef NS_CACHING
127/*
131/*
132 * Cache lookup cycle prevention function - if !NULL then no cache lookups
133 * will be made
134 */
135static void *nss_cache_cycle_prevention_func = NULL;
136#endif
137
138/*
128 * Attempt to spew relatively uniform messages to syslog.
129 */
130#define nss_log(level, fmt, ...) \
131 syslog((level), "NSSWITCH(%s): " fmt, __func__, __VA_ARGS__)
132#define nss_log_simple(level, s) \
133 syslog((level), "NSSWITCH(%s): " s, __func__)
134
135/*

--- 90 unchanged lines hidden (view full) ---

226 elem = vector_ref(i, vec, *count, esize);
227 if (elem != NULL)
228 free_elem(elem);
229 }
230 free(vec);
231 *count = 0;
232}
233
139 * Attempt to spew relatively uniform messages to syslog.
140 */
141#define nss_log(level, fmt, ...) \
142 syslog((level), "NSSWITCH(%s): " fmt, __func__, __VA_ARGS__)
143#define nss_log_simple(level, s) \
144 syslog((level), "NSSWITCH(%s): " s, __func__)
145
146/*

--- 90 unchanged lines hidden (view full) ---

237 elem = vector_ref(i, vec, *count, esize);
238 if (elem != NULL)
239 free_elem(elem);
240 }
241 free(vec);
242 *count = 0;
243}
244
234
235
236/*
237 * Comparison functions for vector_search.
238 */
239static int
240string_compare(const void *a, const void *b)
241{
242 return (strcasecmp(*(const char * const *)a, *(const char * const *)b));
243}

--- 7 unchanged lines hidden (view full) ---

251 cmp = strcmp(((const ns_mtab *)a)->name, ((const ns_mtab *)b)->name);
252 if (cmp != 0)
253 return (cmp);
254 else
255 return (strcmp(((const ns_mtab *)a)->database,
256 ((const ns_mtab *)b)->database));
257}
258
245/*
246 * Comparison functions for vector_search.
247 */
248static int
249string_compare(const void *a, const void *b)
250{
251 return (strcasecmp(*(const char * const *)a, *(const char * const *)b));
252}

--- 7 unchanged lines hidden (view full) ---

260 cmp = strcmp(((const ns_mtab *)a)->name, ((const ns_mtab *)b)->name);
261 if (cmp != 0)
262 return (cmp);
263 else
264 return (strcmp(((const ns_mtab *)a)->database,
265 ((const ns_mtab *)b)->database));
266}
267
259
260
261/*
262 * NSS nsmap management.
263 */
264void
265_nsdbtaddsrc(ns_dbt *dbt, const ns_src *src)
266{
267 const ns_mod *modp;
268

--- 44 unchanged lines hidden (view full) ---

313static int
314nss_configure(void)
315{
316 static pthread_mutex_t conf_lock = PTHREAD_MUTEX_INITIALIZER;
317 static time_t confmod;
318 struct stat statbuf;
319 int result, isthreaded;
320 const char *path;
268/*
269 * NSS nsmap management.
270 */
271void
272_nsdbtaddsrc(ns_dbt *dbt, const ns_src *src)
273{
274 const ns_mod *modp;
275

--- 44 unchanged lines hidden (view full) ---

320static int
321nss_configure(void)
322{
323 static pthread_mutex_t conf_lock = PTHREAD_MUTEX_INITIALIZER;
324 static time_t confmod;
325 struct stat statbuf;
326 int result, isthreaded;
327 const char *path;
328#ifdef NS_CACHING
329 void *handle;
330#endif
321
322 result = 0;
323 isthreaded = __isthreaded;
324#if defined(_NSS_DEBUG) && defined(_NSS_SHOOT_FOOT)
325 /* NOTE WELL: THIS IS A SECURITY HOLE. This must only be built
326 * for debugging purposes and MUST NEVER be used in production.
327 */
328 path = getenv("NSSWITCH_CONF");

--- 22 unchanged lines hidden (view full) ---

351 (vector_free_elem)ns_mod_free);
352 nss_load_builtin_modules();
353 _nsyyparse();
354 (void)fclose(_nsyyin);
355 vector_sort(_nsmap, _nsmapsize, sizeof(*_nsmap), string_compare);
356 if (confmod == 0)
357 (void)atexit(nss_atexit);
358 confmod = statbuf.st_mtime;
331
332 result = 0;
333 isthreaded = __isthreaded;
334#if defined(_NSS_DEBUG) && defined(_NSS_SHOOT_FOOT)
335 /* NOTE WELL: THIS IS A SECURITY HOLE. This must only be built
336 * for debugging purposes and MUST NEVER be used in production.
337 */
338 path = getenv("NSSWITCH_CONF");

--- 22 unchanged lines hidden (view full) ---

361 (vector_free_elem)ns_mod_free);
362 nss_load_builtin_modules();
363 _nsyyparse();
364 (void)fclose(_nsyyin);
365 vector_sort(_nsmap, _nsmapsize, sizeof(*_nsmap), string_compare);
366 if (confmod == 0)
367 (void)atexit(nss_atexit);
368 confmod = statbuf.st_mtime;
369
370#ifdef NS_CACHING
371 handle = dlopen(NULL, RTLD_LAZY | RTLD_GLOBAL);
372 if (handle != NULL) {
373 nss_cache_cycle_prevention_func = dlsym(handle,
374 "_nss_cache_cycle_prevention_function");
375 dlclose(handle);
376 }
377#endif
359fin:
360 if (isthreaded) {
361 (void)_pthread_rwlock_unlock(&nss_lock);
362 if (result == 0)
363 result = _pthread_rwlock_rdlock(&nss_lock);
364 }
365fin2:
366 if (isthreaded)

--- 211 unchanged lines hidden (view full) ---

578{
579 va_list ap;
580 const ns_dbt *dbt;
581 const ns_src *srclist;
582 nss_method method;
583 void *mdata;
584 int isthreaded, serrno, i, result, srclistsize;
585
378fin:
379 if (isthreaded) {
380 (void)_pthread_rwlock_unlock(&nss_lock);
381 if (result == 0)
382 result = _pthread_rwlock_rdlock(&nss_lock);
383 }
384fin2:
385 if (isthreaded)

--- 211 unchanged lines hidden (view full) ---

597{
598 va_list ap;
599 const ns_dbt *dbt;
600 const ns_src *srclist;
601 nss_method method;
602 void *mdata;
603 int isthreaded, serrno, i, result, srclistsize;
604
605#ifdef NS_CACHING
606 nss_cache_data cache_data;
607 nss_cache_data *cache_data_p;
608 int cache_flag;
609#endif
610
586 isthreaded = __isthreaded;
587 serrno = errno;
588 if (isthreaded) {
589 result = _pthread_rwlock_rdlock(&nss_lock);
590 if (result != 0) {
591 result = NS_UNAVAIL;
592 goto fin;
593 }

--- 9 unchanged lines hidden (view full) ---

603 srclist = dbt->srclist;
604 srclistsize = dbt->srclistsize;
605 } else {
606 srclist = defaults;
607 srclistsize = 0;
608 while (srclist[srclistsize].name != NULL)
609 srclistsize++;
610 }
611 isthreaded = __isthreaded;
612 serrno = errno;
613 if (isthreaded) {
614 result = _pthread_rwlock_rdlock(&nss_lock);
615 if (result != 0) {
616 result = NS_UNAVAIL;
617 goto fin;
618 }

--- 9 unchanged lines hidden (view full) ---

628 srclist = dbt->srclist;
629 srclistsize = dbt->srclistsize;
630 } else {
631 srclist = defaults;
632 srclistsize = 0;
633 while (srclist[srclistsize].name != NULL)
634 srclistsize++;
635 }
636
637#ifdef NS_CACHING
638 cache_data_p = NULL;
639 cache_flag = 0;
640#endif
611 for (i = 0; i < srclistsize; i++) {
612 result = NS_NOTFOUND;
613 method = nss_method_lookup(srclist[i].name, database,
614 method_name, disp_tab, &mdata);
641 for (i = 0; i < srclistsize; i++) {
642 result = NS_NOTFOUND;
643 method = nss_method_lookup(srclist[i].name, database,
644 method_name, disp_tab, &mdata);
645
615 if (method != NULL) {
646 if (method != NULL) {
647#ifdef NS_CACHING
648 if (strcmp(srclist[i].name, NSSRC_CACHE) == 0 &&
649 nss_cache_cycle_prevention_func == NULL) {
650#ifdef NS_STRICT_LIBC_EID_CHECKING
651 if (issetugid() != 0)
652 continue;
653#endif
654 cache_flag = 1;
655
656 memset(&cache_data, 0, sizeof(nss_cache_data));
657 cache_data.info = (nss_cache_info const *)mdata;
658 cache_data_p = &cache_data;
659
660 va_start(ap, defaults);
661 if (cache_data.info->id_func != NULL)
662 result = __nss_common_cache_read(retval,
663 cache_data_p, ap);
664 else if (cache_data.info->marshal_func != NULL)
665 result = __nss_mp_cache_read(retval,
666 cache_data_p, ap);
667 else
668 result = __nss_mp_cache_end(retval,
669 cache_data_p, ap);
670 va_end(ap);
671 } else {
672 cache_flag = 0;
673 va_start(ap, defaults);
674 result = method(retval, mdata, ap);
675 va_end(ap);
676 }
677#else /* NS_CACHING */
616 va_start(ap, defaults);
617 result = method(retval, mdata, ap);
618 va_end(ap);
678 va_start(ap, defaults);
679 result = method(retval, mdata, ap);
680 va_end(ap);
681#endif /* NS_CACHING */
682
619 if (result & (srclist[i].flags))
620 break;
621 }
622 }
683 if (result & (srclist[i].flags))
684 break;
685 }
686 }
687
688#ifdef NS_CACHING
689 if (cache_data_p != NULL &&
690 (result & (NS_NOTFOUND | NS_SUCCESS)) && cache_flag == 0) {
691 va_start(ap, defaults);
692 if (result == NS_SUCCESS) {
693 if (cache_data.info->id_func != NULL)
694 __nss_common_cache_write(retval, cache_data_p,
695 ap);
696 else if (cache_data.info->marshal_func != NULL)
697 __nss_mp_cache_write(retval, cache_data_p, ap);
698 } else if (result == NS_NOTFOUND) {
699 if (cache_data.info->id_func == NULL) {
700 if (cache_data.info->marshal_func != NULL)
701 __nss_mp_cache_write_submit(retval,
702 cache_data_p, ap);
703 } else
704 __nss_common_cache_write_negative(cache_data_p);
705 }
706 va_end(ap);
707 }
708#endif /* NS_CACHING */
709
623 if (isthreaded)
624 (void)_pthread_rwlock_unlock(&nss_lock);
625fin:
626 errno = serrno;
627 return (result);
628}
710 if (isthreaded)
711 (void)_pthread_rwlock_unlock(&nss_lock);
712fin:
713 errno = serrno;
714 return (result);
715}