xref: /illumos-gate/usr/src/lib/gss_mechs/mech_dh/dh_common/dh_common.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
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 #include <stdlib.h>
28 #include <stdio.h>
29 #include <dlfcn.h>
30 
31 #include "dh_gssapi.h"
32 #include "dh_common.h"
33 
34 #define	MECH_LIB_PREFIX1	"/usr/lib/"
35 
36 /*
37  * This #ifdef mess figures out if we are to be compiled into an
38  * lp64 binary for the purposes of figuring the absolute location
39  * of gss-api mechanism modules.
40  */
41 #ifdef  _LP64
42 
43 #ifdef __sparc
44 
45 #define	MECH_LIB_PREFIX2	"sparcv9/"
46 
47 #elif defined(__amd64)
48 
49 #define	MECH_LIB_PREFIX2	"amd64/"
50 
51 #else   /* __sparc */
52 
53 you need to define where under /usr the LP64 libraries live for this platform
54 
55 #endif  /* __sparc */
56 
57 #else   /* _LP64 */
58 
59 #define	MECH_LIB_PREFIX2	""
60 
61 #endif  /* _LP64 */
62 
63 #define	MECH_LIB_DIR		"gss/"
64 
65 #define	MECH_LIB_PREFIX MECH_LIB_PREFIX1 MECH_LIB_PREFIX2 MECH_LIB_DIR
66 
67 #define	DH_MECH_BACKEND		"mech_dh.so.1"
68 
69 #define	DH_MECH_BACKEND_PATH MECH_LIB_PREFIX DH_MECH_BACKEND
70 
71 static char *DHLIB = DH_MECH_BACKEND_PATH;
72 
73 #ifndef DH_MECH_SYM
74 #define	DH_MECH_SYM		"__dh_gss_initialize"
75 #endif
76 
77 /*
78  * __dh_generic_initialize: This routine is called from the mechanism
79  * specific gss_mech_initialize routine, which in turn is called from
80  * libgss to initialize a mechanism. This routine takes a pointer to
81  * a struct gss_config, the OID for the calling mechanism and that mechanisms
82  * keyopts. It returns the same gss_mechanism back, but with all fields
83  * correctly initialized. This routine in turn opens the common wire
84  * protocol moduel mech_dh.so.1 to fill in the common parts of the
85  * gss_mechanism. It then associatates the OID and the keyopts with this
86  * gss_mechanism. If there is any failure NULL is return instead.
87  */
88 gss_mechanism
__dh_generic_initialize(gss_mechanism dhmech,gss_OID_desc mech_type,dh_keyopts_t keyopts)89 __dh_generic_initialize(gss_mechanism dhmech, /* The mechanism to initialize */
90 			gss_OID_desc mech_type, /* OID of mechanism */
91 			dh_keyopts_t keyopts /* Key mechanism entry points  */)
92 {
93 	gss_mechanism (*mech_init)(gss_mechanism mech);
94 	gss_mechanism mech;
95 	void *dlhandle;
96 	dh_context_t context;
97 
98 	/* Open the common backend */
99 	if ((dlhandle = dlopen(DHLIB, RTLD_NOW)) == NULL) {
100 		return (NULL);
101 	}
102 
103 	/* Fetch the common backend initialization routine */
104 	mech_init = (gss_mechanism (*)(gss_mechanism))
105 		dlsym(dlhandle, DH_MECH_SYM);
106 
107 	/* Oops this should not happen */
108 	if (mech_init == NULL) {
109 		return (NULL);
110 
111 	}
112 
113 	/* Initialize the common parts of the gss_mechanism */
114 	if ((mech = mech_init(dhmech)) == NULL) {
115 		return (NULL);
116 	}
117 
118 	/* Set the mechanism OID */
119 	mech->mech_type = mech_type;
120 
121 	/* Grab the mechanism context */
122 	context = (dh_context_t)mech->context;
123 
124 	/* Set the keyopts */
125 	context->keyopts = keyopts;
126 
127 	/* Set a handle to the mechanism OID in the per mechanism context */
128 	context->mech = &mech->mech_type;
129 
130 	return (mech);
131 }
132