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