xref: /onnv-gate/usr/src/lib/libc/port/gen/nss_deffinder.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52830Sdjl  * Common Development and Distribution License (the "License").
62830Sdjl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Default backend-finder(s) for the name-service-switch routines.
310Sstevel@tonic-gate  * At present there is a single finder that uses dlopen() to do its thing.
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  * === Could also do a finder that includes db-name in filename
340Sstevel@tonic-gate  * === and one that does dlopen(0) to check in the executable
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate 	/* Allow our finder(s) to be overridden by user-supplied ones */
380Sstevel@tonic-gate 
39*6812Sraf #pragma weak _nss_default_finders = nss_default_finders
400Sstevel@tonic-gate 
41*6812Sraf #include "lint.h"
420Sstevel@tonic-gate #include "mtlib.h"
430Sstevel@tonic-gate #include <nss_common.h>
440Sstevel@tonic-gate #include <dlfcn.h>
450Sstevel@tonic-gate #include <stdio.h>
460Sstevel@tonic-gate #include <stdlib.h>
470Sstevel@tonic-gate #include <string.h>
480Sstevel@tonic-gate #include <alloca.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /* === ? move these constants to a public header file ? */
510Sstevel@tonic-gate static const int  dlopen_version  = 1;
522830Sdjl #ifndef NSS_DLOPEN_FORMAT
532830Sdjl #define	NSS_DLOPEN_FORMAT "nss_%s.so.%d"
542830Sdjl #endif
552830Sdjl #ifndef NSS_DLSYM_FORMAT
562830Sdjl #define	NSS_DLSYM_FORMAT "_nss_%s_%s_constr"
572830Sdjl #endif
582830Sdjl static const char dlopen_format[] = NSS_DLOPEN_FORMAT;
592830Sdjl static const char dlsym_format [] = NSS_DLSYM_FORMAT;
600Sstevel@tonic-gate static const size_t  format_maxlen   = sizeof (dlsym_format) - 4;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /*ARGSUSED*/
630Sstevel@tonic-gate static nss_backend_constr_t
SO_per_src_lookup(void * dummy,const char * db_name,const char * src_name,void ** delete_privp)640Sstevel@tonic-gate SO_per_src_lookup(void *dummy, const char *db_name, const char *src_name,
650Sstevel@tonic-gate 	void **delete_privp)
660Sstevel@tonic-gate {
670Sstevel@tonic-gate 	char			*name;
680Sstevel@tonic-gate 	void			*dlhandle;
690Sstevel@tonic-gate 	void			*sym;
700Sstevel@tonic-gate 	size_t			len;
710Sstevel@tonic-gate 	nss_backend_constr_t	res = 0;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	len = format_maxlen + strlen(db_name) + strlen(src_name);
740Sstevel@tonic-gate 	name = alloca(len);
750Sstevel@tonic-gate 	(void) sprintf(name, dlopen_format, src_name, dlopen_version);
760Sstevel@tonic-gate 	if ((dlhandle = dlopen(name, RTLD_LAZY)) != 0) {
770Sstevel@tonic-gate 		(void) sprintf(name, dlsym_format, src_name, db_name);
780Sstevel@tonic-gate 		if ((sym = dlsym(dlhandle, name)) == 0) {
790Sstevel@tonic-gate 			(void) dlclose(dlhandle);
800Sstevel@tonic-gate 		} else {
810Sstevel@tonic-gate 			*delete_privp = dlhandle;
820Sstevel@tonic-gate 			res = (nss_backend_constr_t)sym;
830Sstevel@tonic-gate 		}
840Sstevel@tonic-gate 	}
850Sstevel@tonic-gate 	return (res);
860Sstevel@tonic-gate }
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /*ARGSUSED*/
890Sstevel@tonic-gate static void
SO_per_src_delete(void * delete_priv,nss_backend_constr_t dummy)900Sstevel@tonic-gate SO_per_src_delete(void *delete_priv, nss_backend_constr_t dummy)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate 	(void) dlclose(delete_priv);
930Sstevel@tonic-gate }
940Sstevel@tonic-gate 
950Sstevel@tonic-gate static nss_backend_finder_t SO_per_src = {
960Sstevel@tonic-gate 	SO_per_src_lookup,
970Sstevel@tonic-gate 	SO_per_src_delete,
980Sstevel@tonic-gate 	0,
990Sstevel@tonic-gate 	0
1000Sstevel@tonic-gate };
1010Sstevel@tonic-gate 
102*6812Sraf nss_backend_finder_t *nss_default_finders = &SO_per_src;
103