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