summaryrefslogtreecommitdiff
path: root/usr/src/cmd/nscd/nscd_nswconfig.c
blob: cbe016af95cac26df189cd754958964a8023ffe9 (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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <nss_common.h>
#include <dlfcn.h>
#include <alloca.h>

#include <stdlib.h>
#include <libscf_priv.h>
#include <string.h>
#include <assert.h>
#include "nscd_switch.h"
#include "nscd_log.h"
#include "nscd_db.h"

/*
 * _nscd_nss_finders is used to replace the nss_default_finders in libc
 * to allow nscd to have more control over the dl handles when using
 * dlsym to get the address of the nss backend instance constructors
 */
static nss_backend_constr_t _nscd_per_src_lookup(void *,
	const char *, const char *, void **);
static void _nscd_per_src_delete(void *, nss_backend_constr_t);

static nss_backend_finder_t _nscd_per_src = {
	_nscd_per_src_lookup,
	_nscd_per_src_delete,
	0,
	0 };

nss_backend_finder_t *_nscd_nss_finders = &_nscd_per_src;

/*
 * nscd database for each source. It contains backend
 * info (nscd_be_info_t) for each naming database.
 * Protected by nscd_src_backend_db_lock.
 */
nscd_db_t	***nscd_src_backend_db;
int		*nscd_src_backend_db_loaded;
static		rwlock_t nscd_src_backend_db_lock = DEFAULTRWLOCK;

/*
 * nsswitch config monitored by nscd. Protected by
 * readers/writer lock nscd_nsw_config_lock
 */
nscd_nsw_config_t ***nscd_nsw_config;
static rwlock_t nscd_nsw_config_lock = DEFAULTRWLOCK;

/*
 * nsswitch source index/name array
 * (allow 32 foreign nsswitch sources/backends)
 */
#define		NSCD_NUM_SRC_FOREIGN 32
nscd_cfg_id_t	*_nscd_cfg_nsw_src_all;
int		_nscd_cfg_num_nsw_src_all;

static void
free_nscd_nsw_config(
	nscd_acc_data_t		*data)
{

	nscd_nsw_config_t	*nsw_cfg = *(nscd_nsw_config_t **)data;
	char			*me = "free_nscd_nsw_config";

	_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
	(me, "freeing nscd nsw config %p \n", nsw_cfg);
	if (nsw_cfg == NULL)
		return;

	if (nsw_cfg->db_name != NULL)
		free(nsw_cfg->db_name);
	if (nsw_cfg->nsw_cfg_str != NULL)
		free(nsw_cfg->nsw_cfg_str);
	if (nsw_cfg->nsw_config != NULL)
		(void) __nsw_freeconfig_v1(nsw_cfg->nsw_config);
	if (nsw_cfg->src_idx != NULL)
		free(nsw_cfg->src_idx);

	free(nsw_cfg);
}


void
_nscd_free_nsw_config(
	nscd_nsw_config_t *nswcfg)
{
	free_nscd_nsw_config((nscd_acc_data_t *)&nswcfg);
}

void
_nscd_free_all_nsw_config()
{

	nscd_nsw_config_t	**nsw_cfg;
	int			i;
	char			*me = "_nscd_free_all_nsw_config";

	_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
	(me, "freeing all nscd nsw config \n");

	(void) rw_wrlock(&nscd_nsw_config_lock);
	for (i = 0; i < NSCD_NUM_DB; i++) {

		if ((nsw_cfg = nscd_nsw_config[i]) == NULL)
			continue;

		nscd_nsw_config[i] = (nscd_nsw_config_t **)_nscd_set(
		    (nscd_acc_data_t *)nsw_cfg, NULL);
	}
	(void) rw_unlock(&nscd_nsw_config_lock);
}


static void
free_nsw_backend_info_db(nscd_acc_data_t *data)
{

	nscd_db_t	*db = *(nscd_db_t **)data;
	char		*me = "free_nsw_backend_info_db";

	_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
	(me, "freeing nsw backend info db %p\n", db);

	if (db == NULL)
		return;

	_nscd_free_db(db);

}

void
_nscd_free_all_nsw_backend_info_db()
{

	nscd_db_t	**db;
	int		i;
	char		*me = " _nscd_free_all_nsw_backend_info_db";

	_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
	(me, "freeing all nsw backend info db\n");

	(void) rw_wrlock(&nscd_src_backend_db_lock);
	for (i = 0; i < NSCD_NUM_SRC; i++) {

		if ((db = nscd_src_backend_db[i]) == NULL)
			continue;

		nscd_src_backend_db[i] = (nscd_db_t **)_nscd_set(
		    (nscd_acc_data_t *)db, NULL);
		nscd_src_backend_db_loaded[i] = 0;
	}
	(void) rw_unlock(&nscd_src_backend_db_lock);
}

/*
 * Populate the backend info db for the 'NSCD_NSW_SRC_NAME(srci)'
 * source.  Create one entry for each source/database pair
 * (e.g., ldap:passwd, nis:hosts, etc).
 */
static nscd_rc_t
_nscd_populate_nsw_backend_info_db(int srci)
{
	nscd_be_info_t		be_info, *bi;
	nss_backend_finder_t	*bf;
	nscd_nsw_config_t	*nsw_cfg;
	int			i, size;
	nscd_db_entry_t		*db_entry;
	char			*src = NSCD_NSW_SRC_NAME(srci);
	const char		*dbn;
	char			*me = "_nscd_populate_nsw_backend_info_db";
	void			*handle = NULL;
	nss_backend_constr_t	c;
	void			*be_version = &_nscd_be_version;

	/* get the version number of the backend (if available) */
	if (srci >= _nscd_cfg_num_nsw_src) { /* a foreign backend */
		c = _nscd_per_src_lookup(handle, NULL, src, &handle);
		if (c == NULL)
			be_version = NULL;
		else
			be_version = (void *)c;

		_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
		(me, "foreign backend: _nss_%s_version = %p ", src, be_version);
	}

	for (i = 0; i < NSCD_NUM_DB; i++) {

		/* no nsswitch configuration, no backend info db population */
		if (nscd_nsw_config[i] == NULL || *nscd_nsw_config[i] == NULL)
			continue;

		nsw_cfg = *nscd_nsw_config[i];
		dbn = NSCD_NSW_DB_NAME(i);
		_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
		(me, "adding backend info for <%s : %s>\n", src, dbn);

		(void) memset(&be_info, 0, sizeof (be_info));

		for (bf = nsw_cfg->fe_params.finders;  bf != 0; bf = bf->next) {

			c = (*bf->lookup)(handle, dbn, src, &handle);

			if (c != 0) {
				be_info.be_constr = c;
				be_info.finder = bf;
				be_info.finder_priv = handle;
				be_info.be_version = be_version;
				break;
			}
		}
		if (be_info.be_constr == NULL) {
			/*
			 * Couldn't find the backend anywhere.
			 * This is fine, some backend just don't
			 * support certain databases.
			 */
			_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_ERROR)
			(me, "unable to find backend info "
			    "for <%s : %s>\n", src, dbn);
		}

		size = sizeof (nscd_be_info_t);

		db_entry = _nscd_alloc_db_entry(NSCD_DATA_BACKEND_INFO,
		    dbn, size, 1, 1);

		if (db_entry == NULL) {
			_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_ERROR)
			(me, "unable to allocate db entry for "
			    "<%s : %s>\n", src, dbn);
			return (NSCD_NO_MEMORY);
		}

		_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
		(me, "adding be db entry %p for <%s : %s> to db %p: "
		    "constr = %p\n", db_entry, src, dbn,
		    *nscd_src_backend_db[srci], be_info.be_constr);

		bi = (nscd_be_info_t *)*(db_entry->data_array);
		*bi = be_info;

		(void) _nscd_wrlock((nscd_acc_data_t *)
		    nscd_src_backend_db[srci]);
		nscd_src_backend_db_loaded[srci] = 1;
		(void) _nscd_add_db_entry(*nscd_src_backend_db[srci],
		    dbn, db_entry, NSCD_ADD_DB_ENTRY_LAST);
		(void) _nscd_rw_unlock((nscd_acc_data_t *)
		    nscd_src_backend_db[srci]);
	}

	return (NSCD_SUCCESS);
}

/*
 * create data structures (used by the switch engine) based
 * on the input switch policy configuration and database
 * name and indexes
 */
nscd_rc_t
_nscd_create_sw_struct(
	int				dbi,
	int				compat_basei,
	const char			*dbn,
	const char			*cfgstr,
	void				*swcfgv1,
	nscd_nsw_params_t		*params)
{
	char				*me = "_nscd_create_sw_struct";
	nscd_rc_t			rc = NSCD_SUCCESS;
	nscd_nsw_config_t		*nsw_cfg = NULL;
	nscd_nsw_config_t		**nsw_cfg_p = NULL;
	struct __nsw_switchconfig_v1	*swcfg = NULL;
	struct __nsw_lookup_v1		*lkp;
	enum __nsw_parse_err		err;
	int				maxsrc;
	int				*src_idx_a = NULL;
	int				j, k;

	/*
	 * if the nsw config string has been parsed into
	 * a struct __nsw_switchconfig_v1, use it. If not,
	 * create the struct.
	 */
	if (swcfgv1 != NULL)
		swcfg = (struct __nsw_switchconfig_v1 *)swcfgv1;
	else {
		char	*cstr;

		cstr = strdup(cfgstr);
		if (cstr == NULL)
			return (NSCD_NO_MEMORY);

		/*
		 * parse the nsw config string and create
		 * a struct __nsw_switchconfig_v1
		 */
		swcfg = _nsw_getoneconfig_v1(dbn, cstr, &err);
		free(cstr);
		if (swcfg == NULL) {
			rc = NSCD_CFG_SYNTAX_ERROR;
			_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_ERROR)
			(me, "error: unable to process nsw config string\n");
			goto error_exit;
		}
	}

	/* allocate the space for a nscd_nsw_config_t */
	nsw_cfg = calloc(1, sizeof (nscd_nsw_config_t));
	if (nsw_cfg == NULL) {
		rc = NSCD_NO_MEMORY;
		_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_ERROR)
		(me, "error: unable to allocate an nscd_nsw_config_t\n");
		goto error_exit;
	}

	/* need to know how many backends (sources) */
	maxsrc = swcfg->num_lookups;
	nsw_cfg->max_src = maxsrc;

	/*
	 * allocate an array to store the index for each
	 * backend (source)
	 */
	src_idx_a = calloc(1, maxsrc * sizeof (int));
	if (src_idx_a == NULL) {
		rc = NSCD_NO_MEMORY;
		_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_ERROR)
		(me, "error: unable to allocate an array for source index\n");
		goto error_exit;
	}

	/*
	 * set the index for each backend (source)
	 */
	lkp = swcfg->lookups;
	for (j = 0; j < maxsrc; j++) {
		char *usrc;

		for (k = 0; k < NSCD_NUM_SRC && NSCD_NSW_SRC_NAME(k) != NULL &&
		    strcmp(lkp->service_name, NSCD_NSW_SRC_NAME(k)) != 0;
		    k++) {
			/* empty */
		}

		if (k < NSCD_NUM_SRC && nscd_src_backend_db_loaded[k] == 0) {
			_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
			(me, "unknown nsw source name %s\n", lkp->service_name);
			usrc = strdup(lkp->service_name);
			if (usrc == NULL) {
				rc = NSCD_NO_MEMORY;
				_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_ERROR)
				(me, "unable to strdup() source name\n");
				goto error_exit;
			}
			NSCD_NSW_SRC_NAME(k) = usrc;

			rc = _nscd_populate_nsw_backend_info_db(k);
			if (rc != NSCD_SUCCESS) {
				free(usrc);
				NSCD_NSW_SRC_NAME(k) = NULL;
				goto error_exit;
			}
		} else if (NSCD_NSW_SRC_NAME(k) == NULL) {
			/*
			 * number of user-defined source exceeded
			 */
			rc = NSCD_CFG_SYNTAX_ERROR;
			_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_ERROR)
			(me, "error: number of user_defined source exceeded\n");

			goto error_exit;
		}

		_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
		(me, "setting source index array [%d] = %d (%s)\n",
		    j, k, lkp->service_name);

		src_idx_a[j] = k;

		lkp = lkp->next;
		if (lkp == NULL) break;

		_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
		(me, "number of nsw sources = %d\n", nsw_cfg->max_src);
		_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
		(me, "next nsw source is %s\n", lkp->service_name);
	}

	/* set it up to reference count the switch policy config */
	nsw_cfg_p = (nscd_nsw_config_t **)_nscd_alloc(NSCD_DATA_NSW_CONFIG,
	    sizeof (nscd_nsw_config_t **), free_nscd_nsw_config,
	    NSCD_ALLOC_RWLOCK);

	if (nsw_cfg_p == NULL) {
		rc = NSCD_NO_MEMORY;
		_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_ERROR)
		(me, "unable to allocate a new nsw config DB\n");
		goto error_exit;
	}
	*nsw_cfg_p = nsw_cfg;

	_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
	(me, "new nsw config DB %p allocated\n", nsw_cfg_p);

	/* save all the data in the new nscd_nsw_config_t */
	nsw_cfg->db_name = strdup(dbn);
	nsw_cfg->nsw_cfg_str = strdup(cfgstr);
	if (nsw_cfg->db_name == NULL || nsw_cfg->nsw_cfg_str == NULL) {
		rc = NSCD_NO_MEMORY;
		goto error_exit;
	}

	_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
	(me, "switch policy \"%s\" for database is \"%s\"\n",
	    nsw_cfg->db_name, nsw_cfg->nsw_cfg_str);

	nsw_cfg->nsw_config = swcfg;
	nsw_cfg->src_idx = src_idx_a;

	/*
	 * set default frontend params and if necessary call initf()
	 * to initialize or override
	 */
	nsw_cfg->fe_params.max_active_per_src = 10;
	nsw_cfg->fe_params.max_dormant_per_src = 1;
	nsw_cfg->fe_params.finders = _nscd_nss_finders;
	if (params != NULL) {
		nsw_cfg->fe_params = params->p;

		if (params->p.flags & NSS_USE_DEFAULT_CONFIG) {
			params->nswcfg = nsw_cfg_p;
			/*
			 * this nsw_cfg is not meant to last long, no need
			 * to set up the nsw state and getent bases, just
			 * exit with NSCD_SUCCESS
			 */
			nsw_cfg->nobase = 1;
			goto error_exit;
		}
	} else
		(void) (nscd_nss_db_initf[dbi])(&nsw_cfg->fe_params);

	/*
	 * also create a new nsw state base
	 */
	if ((rc = _nscd_init_nsw_state_base(dbi, compat_basei, 1)) !=
	    NSCD_SUCCESS) {
		_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_ERROR)
		(me, "unable to initialize a nsw state base(%d)\n", dbi);
		goto error_exit;
	}

	_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
	(me, "new nsw state base(%d) %p created\n", dbi,
	    nscd_nsw_state_base[dbi]);

	/*
	 * also create a new getent context base
	 */
	if ((rc = _nscd_init_getent_ctx_base(dbi, 1)) != NSCD_SUCCESS) {
		_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_ERROR)
		(me, "unable to initialize a getent context base(%d)\n", dbi);
		goto error_exit;
	}

	_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
	(me, "new getent context base(%d) %p created\n", dbi,
	    nscd_getent_ctx_base[dbi]);

	_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
	(me, "new nsw config created (database = %s, "
	"config = %s)\n", dbn, cfgstr);

	/*
	 * Activate the new nscd_nsw_config_t and make it the
	 * current nsswitch config. The old one pointed to by
	 * nscd_nsw_config[dbi] will either be destroyed
	 * immediately or left on the side line (and be
	 * destroyed eventually). __nscd_set() will set the
	 * associated flags to make it happen.
	 */
	nscd_nsw_config[dbi] = (nscd_nsw_config_t **)_nscd_set(
	    (nscd_acc_data_t *)nscd_nsw_config[dbi],
	    (nscd_acc_data_t *)nsw_cfg_p);

	error_exit:

	if (rc != NSCD_SUCCESS) {

		if (swcfgv1 == NULL && swcfg != NULL)
			(void) __nsw_freeconfig_v1(swcfg);
		if (src_idx_a != NULL)
			free(src_idx_a);
		if (nsw_cfg_p)
			free(nsw_cfg_p);
		if (nsw_cfg != NULL) {
			if (nsw_cfg->db_name != NULL)
				free(nsw_cfg->db_name);
			if (nsw_cfg->nsw_cfg_str != NULL)
				free(nsw_cfg->nsw_cfg_str);
			free(nsw_cfg);
		}

		return (rc);
	} else
		return (NSCD_SUCCESS);
}

static nscd_rc_t
create_nsw_config(int dbi)
{

	nscd_nsw_config_t	*nsw_cfg = NULL;
	nscd_nsw_config_t	**nsw_cfg_p = NULL;
	char			*me = "create_nsw_config";

	/*
	 * Allocate a pointer space for saving a pointer to a
	 * nscd_nsw_config_t. _nscd_alloc() will also create
	 * a nscd_access_t header with a rwlock_t and mutex_t
	 * for controlling later access to the data pointed
	 * to by the pointer.
	 */
	nsw_cfg_p = (nscd_nsw_config_t **)_nscd_alloc(NSCD_DATA_NSW_CONFIG,
	    sizeof (nscd_nsw_config_t **), free_nscd_nsw_config,
	    NSCD_ALLOC_RWLOCK | NSCD_ALLOC_MUTEX);

	if (nsw_cfg_p == NULL) {
		_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_ERROR)
		(me, "unable to allocate a space for a nsw config pointer\n");
		return (NSCD_NO_MEMORY);
	}
	_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
		(me, "addr for saving nsw config pointer = %p\n", nsw_cfg_p);

	/*
	 * If pseudo-databases (initf function not defined),
	 * no need to create a nscd_nsw_config_t yet,
	 * so put a NULL pointer in the pointer space.
	 */
	if (nscd_nss_db_initf[dbi] == NULL) {
		*nsw_cfg_p = NULL;
		nscd_nsw_config[dbi] = (nscd_nsw_config_t **)_nscd_set(
		    (nscd_acc_data_t *)nscd_nsw_config[dbi],
		    (nscd_acc_data_t *)nsw_cfg_p);

		_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
		(me, "pointer to nsw config has been set to NULL\n");

		return (NSCD_SUCCESS);
	}

	/* allocate the space for a nscd_nsw_config_t */
	nsw_cfg = calloc(1, sizeof (nscd_nsw_config_t));
	if (nsw_cfg == NULL) {
		_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_ERROR)
		(me, "unable to allocate a nsw config structure\n");
		return (NSCD_NO_MEMORY);
	}
	_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
	(me, "nsw config structure %pallocated\n", nsw_cfg);

	nsw_cfg->db_name = strdup(NSCD_NSW_DB_NAME(dbi));
	if (nsw_cfg->db_name == NULL) {
		_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_ERROR)
		(me, "unable to strdup the db name\n");
		return (NSCD_NO_MEMORY);
	}

	/*
	 * set default frontend params and then call initf()
	 * to initialize or override
	 */
	nsw_cfg->fe_params.max_active_per_src = 10;
	nsw_cfg->fe_params.max_dormant_per_src = 1;
	nsw_cfg->fe_params.finders = _nscd_nss_finders;
	(void) (nscd_nss_db_initf[dbi])(&nsw_cfg->fe_params);

	/*
	 * activate the new nscd_nsw_config_t
	 */
	*nsw_cfg_p = nsw_cfg;
	nscd_nsw_config[dbi] = (nscd_nsw_config_t **)_nscd_set(
	    (nscd_acc_data_t *)nscd_nsw_config[dbi],
	    (nscd_acc_data_t *)nsw_cfg_p);

	_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
	(me, "nsw config %p activated\n", nsw_cfg);

	return (NSCD_SUCCESS);
}

nscd_rc_t
_nscd_init_all_nsw_config(void)
{
	nscd_rc_t	rc;
	int		i;
	char		*me = "_nscd_init_all_nsw_config";

	_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
	(me, "initializing all nsw config\n");

	for (i = 0; i < NSCD_NUM_DB; i++) {
		if ((rc = create_nsw_config(i)) != NSCD_SUCCESS)
			return (rc);
	}

	return (NSCD_SUCCESS);
}

static nscd_rc_t
init_nsw_be_info_db(int srci)
{
	nscd_db_t	*ret, **db_p;
	char		*me = "init_nsw_be_info_db";

	ret = _nscd_alloc_db(NSCD_DB_SIZE_SMALL);

	if (ret == NULL) {
		_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_ERROR)
		(me, "unable to allocate a nsw be info database\n");
		return (NSCD_NO_MEMORY);
	}

	/* set up to reference count the backend info db */
	db_p = (nscd_db_t **)_nscd_alloc(NSCD_DATA_BACKEND_INFO_DB,
	    sizeof (nscd_db_t **), free_nsw_backend_info_db,
	    NSCD_ALLOC_RWLOCK);

	if (db_p == NULL) {
		_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_ERROR)
		(me, "unable to allocate the pointer to the nsw "
		"be info database\n");
		return (NSCD_NO_MEMORY);
	}

	*db_p = ret;
	_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
	(me, "backend database (db_p = %p, db = %p)\n", db_p, *db_p);

	nscd_src_backend_db[srci] = (nscd_db_t **)_nscd_set(
	    (nscd_acc_data_t *)nscd_src_backend_db[srci],
	    (nscd_acc_data_t *)db_p);

	return (NSCD_SUCCESS);
}

nscd_rc_t
_nscd_init_all_nsw_be_info_db(void)
{

	int		i;
	nscd_rc_t	rc;
	char		*me = "_nscd_init_all_nsw_be_info_db";

	_NSCD_LOG(NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
	(me, "initializing all nsw be info databases\n");

	for (i = 0; i < NSCD_NUM_SRC; i++) {
		if ((rc = init_nsw_be_info_db(i)) != NSCD_SUCCESS)
			return (rc);
	}

	return (NSCD_SUCCESS);
}


nscd_rc_t
_nscd_alloc_nsw_config()
{
	nscd_nsw_config = calloc(NSCD_NUM_DB, sizeof (nscd_nsw_config_t **));
	if (nscd_nsw_config == NULL)
		return (NSCD_NO_MEMORY);

	return (NSCD_SUCCESS);
}

nscd_rc_t
_nscd_alloc_nsw_be_info_db()
{
	int	i;

	_nscd_cfg_num_nsw_src_all = _nscd_cfg_num_nsw_src +
	    NSCD_NUM_SRC_FOREIGN;
	nscd_src_backend_db = calloc(NSCD_NUM_SRC, sizeof (nscd_db_t **));
	if (nscd_src_backend_db == NULL)
		return (NSCD_NO_MEMORY);
	nscd_src_backend_db_loaded = calloc(NSCD_NUM_SRC, sizeof (int));
	if (nscd_src_backend_db_loaded == NULL) {
		free(nscd_src_backend_db);
		return (NSCD_NO_MEMORY);
	}

	/* also allocate/init the nsswitch source index/name array */
	_nscd_cfg_nsw_src_all = (nscd_cfg_id_t *)calloc(
	    _nscd_cfg_num_nsw_src_all + 1, sizeof (nscd_cfg_id_t));
	for (i = 0; i < _nscd_cfg_num_nsw_src_all + 1; i++)
		(_nscd_cfg_nsw_src_all + i)->index = -1;

	(void) memcpy(_nscd_cfg_nsw_src_all, _nscd_cfg_nsw_src,
	    _nscd_cfg_num_nsw_src * sizeof (nscd_cfg_id_t));
	return (NSCD_SUCCESS);
}

nscd_rc_t
_nscd_populate_nsw_backend_info()
{
	int		i;
	nscd_rc_t	rc;

	for (i = 0; i < NSCD_NUM_SRC; i++) {
		if (NSCD_NSW_SRC_NAME(i) == NULL)
			continue;
		rc = _nscd_populate_nsw_backend_info_db(i);
		if (rc != NSCD_SUCCESS)
		return (rc);
	}

	return (NSCD_SUCCESS);
}

/*
 * The following defines nscd's own lookup and delete functions
 * that are to be stored in nss_backend_finder_t which is used
 * by _nscd_populate_nsw_backend_info_db() to initialize the
 * various nss backend instances
 */

static const int  dlopen_version  = 1;
#ifndef NSS_DLOPEN_FORMAT
#define	NSS_DLOPEN_FORMAT "nss_%s.so.%d"
#endif
#ifndef NSS_DLSYM_FORMAT
#define	NSS_DLSYM_FORMAT   "_nss_%s_%s_constr"
#define	NSS_DLSYM_FORMAT_V "_nss_%s_version"
#endif
static const char dlopen_format[] = NSS_DLOPEN_FORMAT;
static const char dlsym_format [] = NSS_DLSYM_FORMAT;
static const char dlsym_format_v [] = NSS_DLSYM_FORMAT_V;
static const size_t  format_maxlen   = sizeof (dlsym_format);

/*ARGSUSED*/
static nss_backend_constr_t
_nscd_per_src_lookup(void *handle, const char *db_name, const char *src_name,
	void **delete_privp)
{
	char			*name;
	void			*dlhandle;
	void			*sym;
	size_t			len;
	nss_backend_constr_t	res = NULL;

	len = format_maxlen + strlen(src_name);
	if (db_name != NULL)
		len += strlen(db_name);
	name = alloca(len);
	dlhandle = handle;
	if ((dlhandle = handle) == NULL) {
		(void) sprintf(name, dlopen_format, src_name, dlopen_version);
		dlhandle = dlopen(name, RTLD_LAZY);
	}

	if (dlhandle != NULL) {
		if (db_name != NULL)
			(void) sprintf(name, dlsym_format, src_name, db_name);
		else
			(void) sprintf(name, dlsym_format_v, src_name);
		if ((sym = dlsym(dlhandle, name)) == 0) {
			if (handle == NULL)
				(void) dlclose(dlhandle);
		} else {
			*delete_privp = dlhandle;
			res = (nss_backend_constr_t)sym;
		}
	}
	return (res);
}

/*ARGSUSED*/
static void
_nscd_per_src_delete(void *delete_priv, nss_backend_constr_t dummy)
{
	(void) dlclose(delete_priv);
}