summaryrefslogtreecommitdiffstats
path: root/lib/libc/net/nsdispatch.c
blob: 926107711cd06b95a62ae6fedd688027f55441bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/*	$NetBSD: nsdispatch.c,v 1.9 1999/01/25 00:16:17 lukem Exp $	*/

/*-
 * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Luke Mewburn.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *        This product includes software developed by the NetBSD
 *        Foundation, Inc. and its contributors.
 * 4. Neither the name of The NetBSD Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
/*-
 * Copyright (c) 2003 Networks Associates Technology, Inc.
 * All rights reserved.
 *
 * Portions of this software were developed for the FreeBSD Project by
 * Jacques A. Vidrine, Safeport Network Services, and Network
 * Associates Laboratories, the Security Research Division of Network
 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
 * ("CBOSS"), as part of the DARPA CHATS research program.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include "namespace.h"
#include <sys/param.h>
#include <sys/stat.h>

#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#define _NS_PRIVATE
#include <nsswitch.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include "un-namespace.h"

enum _nss_constants {
	/* Number of elements allocated when we grow a vector */
	ELEMSPERCHUNK =	8
};

/*
 * Global NSS data structures are mostly read-only, but we update
 * them when we read or re-read the nsswitch.conf.
 */
static	pthread_rwlock_t	nss_lock = PTHREAD_RWLOCK_INITIALIZER;

/*
 * Runtime determination of whether we are dynamically linked or not.
 */
extern	int		_DYNAMIC __attribute__ ((weak));
#define	is_dynamic()	(&_DYNAMIC != NULL)

/*
 * default sourcelist: `files'
 */
const ns_src __nsdefaultsrc[] = {
	{ NSSRC_FILES, NS_SUCCESS },
	{ 0 },
};

/* Database, source mappings. */
static	unsigned int		 _nsmapsize;
static	ns_dbt			*_nsmap = NULL;

/* NSS modules. */
static	unsigned int		 _nsmodsize;
static	ns_mod			*_nsmod;

/* Placeholder for builtin modules' dlopen `handle'. */
static	int			 __nss_builtin_handle;
static	void			*nss_builtin_handle = &__nss_builtin_handle;

/*
 * Attempt to spew relatively uniform messages to syslog.
 */
#define nss_log(level, fmt, ...) \
	syslog((level), "NSSWITCH(%s): " fmt, __func__, __VA_ARGS__)
#define nss_log_simple(level, s) \
	syslog((level), "NSSWITCH(%s): " s, __func__)

/*
 * Dynamically growable arrays are used for lists of databases, sources,
 * and modules.  The following `vector' interface is used to isolate the
 * common operations.
 */
typedef	int	(*vector_comparison)(const void *, const void *);
typedef	void	(*vector_free_elem)(void *);
static	void	  vector_sort(void *, unsigned int, size_t,
		    vector_comparison);
static	void	  vector_free(void **, unsigned int *, size_t,
		    vector_free_elem);
static	void	 *vector_ref(unsigned int, void *, unsigned int, size_t);
static	void	 *vector_search(const void *, void *, unsigned int, size_t,
		    vector_comparison);
static	int	  vector_append(const void *, void **, unsigned int *, size_t);


/*
 * Internal interfaces.
 */
static	int	 string_compare(const void *, const void *);
static	int	 mtab_compare(const void *, const void *);
static	int	 nss_configure(void);
static	void	 ns_dbt_free(ns_dbt *);
static	void	 ns_mod_free(ns_mod *);
static	void	 ns_src_free(ns_src **, int);
static	void	 nss_load_builtin_modules(void);
static	void	 nss_load_module(const char *, nss_module_register_fn);
static	void	 nss_atexit(void);
/* nsparser */
extern	FILE	*_nsyyin;


/*
 * The vector operations
 */
static void
vector_sort(void *vec, unsigned int count, size_t esize,
    vector_comparison comparison)
{
	qsort(vec, count, esize, comparison);
}


static void *
vector_search(const void *key, void *vec, unsigned int count, size_t esize,
    vector_comparison comparison)
{
	return (bsearch(key, vec, count, esize, comparison));
}


static int
vector_append(const void *elem, void **vec, unsigned int *count, size_t esize)
{
	void	*p;

	if ((*count % ELEMSPERCHUNK) == 0) {
		p = realloc(*vec, (*count + ELEMSPERCHUNK) * esize);
		if (p == NULL) {
			nss_log_simple(LOG_ERR, "memory allocation failure");
			return (0);
		} else
			*vec = p;
	}
	memmove((void *)(((uintptr_t)*vec) + (*count * esize)),
	    elem, esize);
	(*count)++;
	return (1);
}


static void *
vector_ref(unsigned int i, void *vec, unsigned int count, size_t esize)
{
	if (i < count)
		return (void *)((uintptr_t)vec + (i * esize));
	else
		return (NULL);
}


static void
vector_free(void **vec, unsigned int *count, size_t esize,
    vector_free_elem free_elem)
{
	unsigned int	 i;
	void		*elem;

	for (i = 0; i < *count; i++) {
		elem = vector_ref(i, *vec, *count, esize);
		if (elem != NULL)
			free_elem(elem);
	}
	free(*vec);
	*vec = NULL;
	*count = 0;
}



/*
 * Comparison functions for vector_search.
 */
static int
string_compare(const void *a, const void *b)
{
      return (strcasecmp(*(const char * const *)a, *(const char * const *)b));
}


static int
mtab_compare(const void *a, const void *b)
{
      int     cmp;
      
      cmp = strcmp(((const ns_mtab *)a)->name, ((const ns_mtab *)b)->name);
      if (cmp != 0)
	      return (cmp);
      else
	      return (strcmp(((const ns_mtab *)a)->database,
		  ((const ns_mtab *)b)->database));
}



/*
 * NSS nsmap management.
 */
void
_nsdbtaddsrc(ns_dbt *dbt, const ns_src *src)
{
	const ns_mod	*modp;

	vector_append(src, (void **)&dbt->srclist, &dbt->srclistsize,
	    sizeof(*src));
	modp = vector_search(&src->name, _nsmod, _nsmodsize, sizeof(*_nsmod),
	    string_compare);
	if (modp == NULL)
		nss_load_module(src->name, NULL);
}


#ifdef _NSS_DEBUG
void
_nsdbtdump(const ns_dbt *dbt)
{
	int i;

	printf("%s (%d source%s):", dbt->name, dbt->srclistsize,
	    dbt->srclistsize == 1 ? "" : "s");
	for (i = 0; i < (int)dbt->srclistsize; i++) {
		printf(" %s", dbt->srclist[i].name);
		if (!(dbt->srclist[i].flags &
		    (NS_UNAVAIL|NS_NOTFOUND|NS_TRYAGAIN)) &&
		    (dbt->srclist[i].flags & NS_SUCCESS))
			continue;
		printf(" [");
		if (!(dbt->srclist[i].flags & NS_SUCCESS))
			printf(" SUCCESS=continue");
		if (dbt->srclist[i].flags & NS_UNAVAIL)
			printf(" UNAVAIL=return");
		if (dbt->srclist[i].flags & NS_NOTFOUND)
			printf(" NOTFOUND=return");
		if (dbt->srclist[i].flags & NS_TRYAGAIN)
			printf(" TRYAGAIN=return");
		printf(" ]");
	}
	printf("\n");
}
#endif


/* 
 * The first time nsdispatch is called (during a process's lifetime,
 * or after nsswitch.conf has been updated), nss_configure will
 * prepare global data needed by NSS.
 */
static int
nss_configure(void)
{
	static pthread_mutex_t conf_lock = PTHREAD_MUTEX_INITIALIZER;
	static time_t	 confmod;
	struct stat	 statbuf;
	int		 result;
	const char	*path;

#if defined(_NSS_DEBUG) && defined(_NSS_SHOOT_FOOT)
	/* NOTE WELL:  THIS IS A SECURITY HOLE. This must only be built
	 * for debugging purposes and MUST NEVER be used in production.
	 */
	path = getenv("NSSWITCH_CONF");
	if (path == NULL)
#endif
	path = _PATH_NS_CONF;
	if (stat(path, &statbuf) != 0)
		return (0);
	if (statbuf.st_mtime <= confmod)
		return (0);
	result = _pthread_mutex_trylock(&conf_lock);
	if (result != 0)
		return (0);
	(void)_pthread_rwlock_unlock(&nss_lock);
	result = _pthread_rwlock_wrlock(&nss_lock);
	if (result != 0)
		goto fin2;
	_nsyyin = fopen(path, "r");
	if (_nsyyin == NULL)
		goto fin;
	vector_free((void **)&_nsmap, &_nsmapsize, sizeof(*_nsmap),
	    (vector_free_elem)ns_dbt_free);
	vector_free((void **)&_nsmod, &_nsmodsize, sizeof(*_nsmod),
	    (vector_free_elem)ns_mod_free);
	nss_load_builtin_modules();
	_nsyyparse();
	(void)fclose(_nsyyin);
	vector_sort(_nsmap, _nsmapsize, sizeof(*_nsmap), string_compare);
	if (confmod == 0)
		(void)atexit(nss_atexit);
	confmod = statbuf.st_mtime;
fin:
	(void)_pthread_rwlock_unlock(&nss_lock);
	result = _pthread_rwlock_rdlock(&nss_lock);
fin2:
	(void)_pthread_mutex_unlock(&conf_lock);
	return (result);
}


void
_nsdbtput(const ns_dbt *dbt)
{
	unsigned int	 i;
	ns_dbt		*p;

	for (i = 0; i < _nsmapsize; i++) {
		p = vector_ref(i, _nsmap, _nsmapsize, sizeof(*_nsmap));
		if (string_compare(&dbt->name, &p->name) == 0) {
			/* overwrite existing entry */
			if (p->srclist != NULL)
				ns_src_free(&p->srclist, p->srclistsize);
			memmove(p, dbt, sizeof(*dbt));
			return;
		}
	}
	vector_append(dbt, (void **)&_nsmap, &_nsmapsize, sizeof(*_nsmap));
}


static void
ns_dbt_free(ns_dbt *dbt)
{
	ns_src_free(&dbt->srclist, dbt->srclistsize);
}


static void
ns_src_free(ns_src **src, int srclistsize)
{
	int	i;

	for (i = 0; i < srclistsize; i++)
		if ((*src)[i].name != NULL)
			/* This one was allocated by nslexer. You'll just
			 * have to trust me.
			 */
			free((void *)((*src)[i].name));
	free(*src);
	*src = NULL;
}



/*
 * NSS module management.
 */
/* The built-in NSS modules are all loaded at once. */
#define NSS_BACKEND(name, reg) \
ns_mtab	*reg(unsigned int *, nss_module_unregister_fn *);
#include "nss_backends.h"
#undef NSS_BACKEND

static void
nss_load_builtin_modules(void)
{
#define NSS_BACKEND(name, reg) nss_load_module(#name, reg);
#include "nss_backends.h"
#undef NSS_BACKEND
}


/* Load a built-in or dynamically linked module.  If the `reg_fn'
 * argument is non-NULL, assume a built-in module and use reg_fn to
 * register it.  Otherwise, search for a dynamic NSS module.
 */
static void
nss_load_module(const char *source, nss_module_register_fn reg_fn)
{
	char		 buf[PATH_MAX];
	ns_mod		 mod;
	nss_module_register_fn fn;
	
	memset(&mod, 0, sizeof(mod));
	mod.name = strdup(source);
	if (mod.name == NULL) {
		nss_log_simple(LOG_ERR, "memory allocation failure");
		return;
	}
	if (reg_fn != NULL) {
		/* The placeholder is required, as a NULL handle 
		 * represents an invalid module.
		 */
		mod.handle = nss_builtin_handle;
		fn = reg_fn;
	} else if (!is_dynamic())
		goto fin;
	else {
		if (snprintf(buf, sizeof(buf), "nss_%s.so.%d", mod.name,
		    NSS_MODULE_INTERFACE_VERSION) >= (int)sizeof(buf))
			goto fin;
		mod.handle = dlopen(buf, RTLD_LOCAL|RTLD_LAZY);
		if (mod.handle == NULL) {
#ifdef _NSS_DEBUG
			/* This gets pretty annoying since the built-in
			 * sources aren't modules yet.
			 */
			nss_log(LOG_DEBUG, "%s, %s", mod.name, dlerror());
#endif
			goto fin;
		}
		fn = (nss_module_register_fn)dlfunc(mod.handle,
		    "nss_module_register");
		if (fn == NULL) {
			(void)dlclose(mod.handle);
			mod.handle = NULL;
			nss_log(LOG_ERR, "%s, %s", mod.name, dlerror());
			goto fin;
		}
	}
	mod.mtab = fn(mod.name, &mod.mtabsize, &mod.unregister);
	if (mod.mtab == NULL || mod.mtabsize == 0) {
		if (mod.handle != nss_builtin_handle)
			(void)dlclose(mod.handle);
		mod.handle = NULL;
		nss_log(LOG_ERR, "%s, registration failed", mod.name);
		goto fin;
	}
	if (mod.mtabsize > 1)
		qsort(mod.mtab, mod.mtabsize, sizeof(mod.mtab[0]),
		    mtab_compare);
fin:
	vector_append(&mod, (void **)&_nsmod, &_nsmodsize, sizeof(*_nsmod));
	vector_sort(_nsmod, _nsmodsize, sizeof(*_nsmod), string_compare);
}



static void
ns_mod_free(ns_mod *mod)
{

	free(mod->name);
	if (mod->handle == NULL)
		return;
	if (mod->unregister != NULL)
		mod->unregister(mod->mtab, mod->mtabsize);
	if (mod->handle != nss_builtin_handle)
		(void)dlclose(mod->handle);
}



/*
 * Cleanup
 */
static void
nss_atexit(void)
{
	(void)_pthread_rwlock_wrlock(&nss_lock);
	vector_free((void **)&_nsmap, &_nsmapsize, sizeof(*_nsmap),
	    (vector_free_elem)ns_dbt_free);
	vector_free((void **)&_nsmod, &_nsmodsize, sizeof(*_nsmod),
	    (vector_free_elem)ns_mod_free);
	(void)_pthread_rwlock_unlock(&nss_lock);
}



/*
 * Finally, the actual implementation.
 */
static nss_method
nss_method_lookup(const char *source, const char *database,
    const char *method, const ns_dtab disp_tab[], void **mdata)
{
	ns_mod	*mod;
	ns_mtab	*match, key;
	int	 i;

	if (disp_tab != NULL)
		for (i = 0; disp_tab[i].src != NULL; i++)
			if (strcasecmp(source, disp_tab[i].src) == 0) {
				*mdata = disp_tab[i].mdata;
				return (disp_tab[i].method);
			}
	mod = vector_search(&source, _nsmod, _nsmodsize, sizeof(*_nsmod),
	    string_compare);
	if (mod != NULL && mod->handle != NULL) {
		key.database = database;
		key.name = method;
		match = bsearch(&key, mod->mtab, mod->mtabsize,
		    sizeof(mod->mtab[0]), mtab_compare);
		if (match != NULL) {
			*mdata = match->mdata;
			return (match->method);
		}
	}
	if (is_dynamic())
		nss_log(LOG_DEBUG, "%s, %s, %s, not found", source, database,
		    method);
	*mdata = NULL;
	return (NULL);
}


__weak_reference(_nsdispatch, nsdispatch);

int
_nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
	    const char *method_name, const ns_src defaults[], ...)
{
	va_list		 ap;
	const ns_dbt	*dbt;
	const ns_src	*srclist;
	nss_method	 method;
	void		*mdata;
	int		 serrno, i, result, srclistsize;

	serrno = errno;
	result = _pthread_rwlock_rdlock(&nss_lock);
	if (result != 0) {
		result = NS_UNAVAIL;
		goto fin;
	}
	result = nss_configure();
	if (result != 0) {
		result = NS_UNAVAIL;
		goto fin;
	}
	dbt = vector_search(&database, _nsmap, _nsmapsize, sizeof(*_nsmap),
	    string_compare);
	if (dbt != NULL) {
		srclist = dbt->srclist;
		srclistsize = dbt->srclistsize;
	} else {
		srclist = defaults;
		srclistsize = 0;
		while (srclist[srclistsize].name != NULL)
			srclistsize++;
	}
	for (i = 0; i < srclistsize; i++) {
		result = NS_NOTFOUND;
		method = nss_method_lookup(srclist[i].name, database,
		    method_name, disp_tab, &mdata);
		if (method != NULL) {
			va_start(ap, defaults);
			result = method(retval, mdata, ap);
			va_end(ap);
			if (result & (srclist[i].flags))
				break;
		}
	}
	(void)_pthread_rwlock_unlock(&nss_lock);
fin:
	errno = serrno;
	return (result);
}
OpenPOWER on IntegriCloud