xref: /illumos-gate/usr/src/lib/libc/port/gen/nss_deffinder.c (revision 06ca4e396ecc93ed45a333664a51558b7e84e8f5)
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 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Default backend-finder(s) for the name-service-switch routines.
29  * At present there is a single finder that uses dlopen() to do its thing.
30  *
31  * === Could also do a finder that includes db-name in filename
32  * === and one that does dlopen(0) to check in the executable
33  */
34 
35 	/* Allow our finder(s) to be overridden by user-supplied ones */
36 
37 #pragma weak _nss_default_finders = nss_default_finders
38 
39 #include "lint.h"
40 #include "mtlib.h"
41 #include <nss_common.h>
42 #include <dlfcn.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <alloca.h>
47 
48 /* === ? move these constants to a public header file ? */
49 static const int  dlopen_version  = 1;
50 #ifndef NSS_DLOPEN_FORMAT
51 #define	NSS_DLOPEN_FORMAT "nss_%s.so.%d"
52 #endif
53 #ifndef NSS_DLSYM_FORMAT
54 #define	NSS_DLSYM_FORMAT "_nss_%s_%s_constr"
55 #endif
56 static const char dlopen_format[] = NSS_DLOPEN_FORMAT;
57 static const char dlsym_format [] = NSS_DLSYM_FORMAT;
58 static const size_t  format_maxlen   = sizeof (dlsym_format) - 4;
59 
60 static nss_backend_constr_t
61 SO_per_src_lookup(void *dummy __unused, const char *db_name,
62     const char *src_name, void **delete_privp)
63 {
64 	char			*name;
65 	void			*dlhandle;
66 	void			*sym;
67 	size_t			len;
68 	nss_backend_constr_t	res = 0;
69 
70 	len = format_maxlen + strlen(db_name) + strlen(src_name);
71 	name = alloca(len);
72 	(void) sprintf(name, dlopen_format, src_name, dlopen_version);
73 	if ((dlhandle = dlopen(name, RTLD_LAZY)) != 0) {
74 		(void) sprintf(name, dlsym_format, src_name, db_name);
75 		if ((sym = dlsym(dlhandle, name)) == 0) {
76 			(void) dlclose(dlhandle);
77 		} else {
78 			*delete_privp = dlhandle;
79 			res = (nss_backend_constr_t)sym;
80 		}
81 	}
82 	return (res);
83 }
84 
85 static void
86 SO_per_src_delete(void *delete_priv, nss_backend_constr_t dummy __unused)
87 {
88 	(void) dlclose(delete_priv);
89 }
90 
91 static nss_backend_finder_t SO_per_src = {
92 	SO_per_src_lookup,
93 	SO_per_src_delete,
94 	0,
95 	0
96 };
97 
98 nss_backend_finder_t *nss_default_finders = &SO_per_src;
99