1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Default backend-finder(s) for the name-service-switch routines.
30  * At present there is a single finder that uses dlopen() to do its thing.
31  *
32  * === Could also do a finder that includes db-name in filename
33  * === and one that does dlopen(0) to check in the executable
34  */
35 
36 	/* Allow our finder(s) to be overridden by user-supplied ones */
37 
38 #pragma weak nss_default_finders = _nss_default_finders
39 
40 #include "synonyms.h"
41 #include "mtlib.h"
42 #include <nss_common.h>
43 #include <dlfcn.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <alloca.h>
48 
49 /* === ? move these constants to a public header file ? */
50 static const int  dlopen_version  = 1;
51 #ifndef NSS_DLOPEN_FORMAT
52 #define	NSS_DLOPEN_FORMAT "nss_%s.so.%d"
53 #endif
54 #ifndef NSS_DLSYM_FORMAT
55 #define	NSS_DLSYM_FORMAT "_nss_%s_%s_constr"
56 #endif
57 static const char dlopen_format[] = NSS_DLOPEN_FORMAT;
58 static const char dlsym_format [] = NSS_DLSYM_FORMAT;
59 static const size_t  format_maxlen   = sizeof (dlsym_format) - 4;
60 
61 /*ARGSUSED*/
62 static nss_backend_constr_t
63 SO_per_src_lookup(void *dummy, const char *db_name, const char *src_name,
64 	void **delete_privp)
65 {
66 	char			*name;
67 	void			*dlhandle;
68 	void			*sym;
69 	size_t			len;
70 	nss_backend_constr_t	res = 0;
71 
72 	len = format_maxlen + strlen(db_name) + strlen(src_name);
73 	name = alloca(len);
74 	(void) sprintf(name, dlopen_format, src_name, dlopen_version);
75 	if ((dlhandle = dlopen(name, RTLD_LAZY)) != 0) {
76 		(void) sprintf(name, dlsym_format, src_name, db_name);
77 		if ((sym = dlsym(dlhandle, name)) == 0) {
78 			(void) dlclose(dlhandle);
79 		} else {
80 			*delete_privp = dlhandle;
81 			res = (nss_backend_constr_t)sym;
82 		}
83 	}
84 	return (res);
85 }
86 
87 /*ARGSUSED*/
88 static void
89 SO_per_src_delete(void *delete_priv, nss_backend_constr_t dummy)
90 {
91 	(void) dlclose(delete_priv);
92 }
93 
94 static nss_backend_finder_t SO_per_src = {
95 	SO_per_src_lookup,
96 	SO_per_src_delete,
97 	0,
98 	0
99 };
100 
101 nss_backend_finder_t *_nss_default_finders = &SO_per_src;
102