xref: /illumos-gate/usr/src/lib/libc/port/gen/nss_deffinder.c (revision 9a016c63ca347047a236dff12f0da83aac8981d1)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 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 "synonyms.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 static const char dlopen_format[] = "nss_%s.so.%d";
53 static const char dlsym_format [] = "_nss_%s_%s_constr";
54 static const size_t  format_maxlen   = sizeof (dlsym_format) - 4;
55 
56 /*ARGSUSED*/
57 static nss_backend_constr_t
58 SO_per_src_lookup(void *dummy, const char *db_name, const char *src_name,
59 	void **delete_privp)
60 {
61 	char			*name;
62 	void			*dlhandle;
63 	void			*sym;
64 	size_t			len;
65 	nss_backend_constr_t	res = 0;
66 
67 	len = format_maxlen + strlen(db_name) + strlen(src_name);
68 	name = alloca(len);
69 	(void) sprintf(name, dlopen_format, src_name, dlopen_version);
70 	if ((dlhandle = dlopen(name, RTLD_LAZY)) != 0) {
71 		(void) sprintf(name, dlsym_format, src_name, db_name);
72 		if ((sym = dlsym(dlhandle, name)) == 0) {
73 			(void) dlclose(dlhandle);
74 		} else {
75 			*delete_privp = dlhandle;
76 			res = (nss_backend_constr_t)sym;
77 		}
78 	}
79 	return (res);
80 }
81 
82 /*ARGSUSED*/
83 static void
84 SO_per_src_delete(void *delete_priv, nss_backend_constr_t dummy)
85 {
86 	(void) dlclose(delete_priv);
87 }
88 
89 static nss_backend_finder_t SO_per_src = {
90 	SO_per_src_lookup,
91 	SO_per_src_delete,
92 	0,
93 	0
94 };
95 
96 nss_backend_finder_t *_nss_default_finders = &SO_per_src;
97