17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5cb5caa98Sdjl  * Common Development and Distribution License (the "License").
6cb5caa98Sdjl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
237257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * Default backend-finder(s) for the name-service-switch routines.
297c478bd9Sstevel@tonic-gate  * At present there is a single finder that uses dlopen() to do its thing.
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * === Could also do a finder that includes db-name in filename
327c478bd9Sstevel@tonic-gate  * === and one that does dlopen(0) to check in the executable
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate 	/* Allow our finder(s) to be overridden by user-supplied ones */
367c478bd9Sstevel@tonic-gate 
377257d1b4Sraf #pragma weak _nss_default_finders = nss_default_finders
387c478bd9Sstevel@tonic-gate 
397257d1b4Sraf #include "lint.h"
407c478bd9Sstevel@tonic-gate #include "mtlib.h"
417c478bd9Sstevel@tonic-gate #include <nss_common.h>
427c478bd9Sstevel@tonic-gate #include <dlfcn.h>
437c478bd9Sstevel@tonic-gate #include <stdio.h>
447c478bd9Sstevel@tonic-gate #include <stdlib.h>
457c478bd9Sstevel@tonic-gate #include <string.h>
467c478bd9Sstevel@tonic-gate #include <alloca.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /* === ? move these constants to a public header file ? */
497c478bd9Sstevel@tonic-gate static const int  dlopen_version  = 1;
50cb5caa98Sdjl #ifndef NSS_DLOPEN_FORMAT
51cb5caa98Sdjl #define	NSS_DLOPEN_FORMAT "nss_%s.so.%d"
52cb5caa98Sdjl #endif
53cb5caa98Sdjl #ifndef NSS_DLSYM_FORMAT
54cb5caa98Sdjl #define	NSS_DLSYM_FORMAT "_nss_%s_%s_constr"
55cb5caa98Sdjl #endif
56cb5caa98Sdjl static const char dlopen_format[] = NSS_DLOPEN_FORMAT;
57cb5caa98Sdjl static const char dlsym_format [] = NSS_DLSYM_FORMAT;
587c478bd9Sstevel@tonic-gate static const size_t  format_maxlen   = sizeof (dlsym_format) - 4;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static nss_backend_constr_t
SO_per_src_lookup(void * dummy __unused,const char * db_name,const char * src_name,void ** delete_privp)61*4a38094cSToomas Soome SO_per_src_lookup(void *dummy __unused, const char *db_name,
62*4a38094cSToomas Soome     const char *src_name, void **delete_privp)
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate 	char			*name;
657c478bd9Sstevel@tonic-gate 	void			*dlhandle;
667c478bd9Sstevel@tonic-gate 	void			*sym;
677c478bd9Sstevel@tonic-gate 	size_t			len;
687c478bd9Sstevel@tonic-gate 	nss_backend_constr_t	res = 0;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	len = format_maxlen + strlen(db_name) + strlen(src_name);
717c478bd9Sstevel@tonic-gate 	name = alloca(len);
727c478bd9Sstevel@tonic-gate 	(void) sprintf(name, dlopen_format, src_name, dlopen_version);
737c478bd9Sstevel@tonic-gate 	if ((dlhandle = dlopen(name, RTLD_LAZY)) != 0) {
747c478bd9Sstevel@tonic-gate 		(void) sprintf(name, dlsym_format, src_name, db_name);
757c478bd9Sstevel@tonic-gate 		if ((sym = dlsym(dlhandle, name)) == 0) {
767c478bd9Sstevel@tonic-gate 			(void) dlclose(dlhandle);
777c478bd9Sstevel@tonic-gate 		} else {
787c478bd9Sstevel@tonic-gate 			*delete_privp = dlhandle;
797c478bd9Sstevel@tonic-gate 			res = (nss_backend_constr_t)sym;
807c478bd9Sstevel@tonic-gate 		}
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate 	return (res);
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate static void
SO_per_src_delete(void * delete_priv,nss_backend_constr_t dummy __unused)86*4a38094cSToomas Soome SO_per_src_delete(void *delete_priv, nss_backend_constr_t dummy __unused)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	(void) dlclose(delete_priv);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static nss_backend_finder_t SO_per_src = {
927c478bd9Sstevel@tonic-gate 	SO_per_src_lookup,
937c478bd9Sstevel@tonic-gate 	SO_per_src_delete,
947c478bd9Sstevel@tonic-gate 	0,
957c478bd9Sstevel@tonic-gate 	0
967c478bd9Sstevel@tonic-gate };
977c478bd9Sstevel@tonic-gate 
987257d1b4Sraf nss_backend_finder_t *nss_default_finders = &SO_per_src;
99