1 /* #ident "@(#)g_initialize.c 1.2 96/02/06 SMI" */
4 * Copyright 1996 by Sun Microsystems, Inc.
6 * Permission to use, copy, modify, distribute, and sell this software
7 * and its documentation for any purpose is hereby granted without fee,
8 * provided that the above copyright notice appears in all copies and
9 * that both that copyright notice and this permission notice appear in
10 * supporting documentation, and that the name of Sun Microsystems not be used
11 * in advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. Sun Microsystems makes no
13 * representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
16 * SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
20 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
21 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
26 * This function will initialize the gssapi mechglue library
40 #ifdef USE_SOLARIS_SHARED_LIBRARIES
43 #define MECH_CONF "/etc/mech.conf"
44 #define MECH_SYM "gss_mech_initialize"
46 static void solaris_initialize (void);
47 #endif /* USE_SOLARIS_SHARED_LIBRARIES */
50 #define USE_LINUX_SHARED_LIBRARIES
53 #ifdef USE_LINUX_SHARED_LIBRARIES
55 #define MECH_CONF "/etc/gssapi_mech.conf"
56 #define MECH_SYM "gss_mech_initialize"
57 static void linux_initialize (void);
58 #endif /* USE_LINUX_SHARED_LIBRARIES */
60 #define g_OID_equal(o1,o2) \
61 (((o1)->length == (o2)->length) && \
62 (memcmp((o1)->elements,(o2)->elements,(int) (o1)->length) == 0))
64 extern gss_mechanism krb5_gss_initialize();
66 static int _gss_initialized = 0;
68 static struct gss_config null_mech = {
71 gss_mechanism *__gss_mechs_array = NULL;
74 * This function will add a new mechanism to the mechs_array
78 add_mechanism (mech, replace)
82 gss_mechanism * temp_array;
83 gss_OID_set mech_names;
84 OM_uint32 minor_status, major_status;
88 return GSS_S_COMPLETE;
90 /* initialize the mechs_array if it hasn't already been initialized */
91 if (__gss_mechs_array == NULL) {
92 __gss_mechs_array = (gss_mechanism *) malloc (sizeof(gss_mechanism));
94 if (__gss_mechs_array == NULL)
97 __gss_mechs_array[0] = &null_mech;
101 * Find the length of __gss_mechs_array, and look for an existing
104 for (i=0; __gss_mechs_array[i]->mech_type.length != 0; i++) {
105 if (!g_OID_equal(&__gss_mechs_array[i]->mech_type,
109 /* We found a match. Replace it? */
111 return GSS_S_FAILURE;
113 __gss_mechs_array[i] = mech;
114 return GSS_S_COMPLETE;
117 /* we didn't find it -- add it to the end of the __gss_mechs_array */
118 temp_array = (gss_mechanism *) realloc(__gss_mechs_array,
119 (i+2)*sizeof(gss_mechanism));
121 if (temp_array == NULL)
124 temp_array[i++] = mech;
125 temp_array[i] = &null_mech;
127 __gss_mechs_array = temp_array;
130 * OK, now let's register all of the name types this mechanism
131 * knows how to deal with.
133 major_status = gss_inquire_names_for_mech(&minor_status, &mech->mech_type,
135 if (major_status != GSS_S_COMPLETE)
136 return (GSS_S_COMPLETE);
137 for (i=0; i < mech_names->count; i++) {
138 gss_add_mech_name_type(&minor_status, &mech_names->elements[i],
141 (void) gss_release_oid_set(&minor_status, &mech_names);
143 return GSS_S_COMPLETE;
146 void gss_initialize ()
148 /* Make sure we've not run already */
149 if (_gss_initialized)
151 _gss_initialized = 1;
153 #ifdef USE_SOLARIS_SHARED_LIBRARIES
154 solaris_initialize();
156 #elif defined(USE_LINUX_SHARED_LIBRARIES)
164 * Use hard-coded in mechanisms... I need to know what mechanisms
165 * are supported... As more mechanisms become supported, they
166 * should be added here, unless shared libraries are used.
169 /* Initialize the krb5 mechanism */
170 mech = (gss_mechanism)krb5_gss_initialize();
172 add_mechanism (mech, 1);
175 #endif /* USE_SOLARIS_SHARED_LIBRARIES */
177 #if !defined(macintosh)
178 if (__gss_mechs_array == NULL) { /* this is very bad! */
179 fprintf(stderr,"gss_initialize fatal error: no mechanisms loaded!\n");
184 * Nothing for now, since this should never happen using static
192 #ifdef USE_SOLARIS_SHARED_LIBRARIES
194 * read the configuration file to find out what mechanisms to
195 * load, load them, and then load the mechanism defitions in
196 * and add the mechanisms
198 static void solaris_initialize ()
200 char buffer[BUFSIZ], *filename, *symname, *endp;
203 gss_mechanism (*sym)(void), mech;
205 if ((filename = getenv("GSSAPI_MECH_CONF")) == NULL)
206 filename = MECH_CONF;
208 if ((conffile = fopen(filename, "r")) == NULL) {
209 fprintf(stderr,"fatal error: unable to open %s:"
210 " errno %d (%s)\n", filename, errno, strerror(errno));
214 while (fgets (buffer, BUFSIZ, conffile) != NULL) {
215 /* ignore lines beginning with # */
219 /* find the first white-space character after the filename */
220 for (symname = buffer; *symname && !isspace(*symname); symname++);
222 /* Now find the first non-white-space character */
226 while (*symname && isspace(*symname))
233 /* Find the end of the symname and make sure it is NULL-terminated */
234 for (endp = symname; *endp && !isspace(*endp); endp++);
239 if ((dl = dlopen(buffer, RTLD_NOW)) == NULL) {
240 /* for debugging only */
241 fprintf(stderr,"can't open %s: %s\n",buffer, dlerror());
245 if ((sym = (gss_mechanism (*)(void))dlsym(dl, symname)) == NULL) {
250 /* Call the symbol to get the mechanism table */
253 /* And add the mechanism (or close the shared library) */
255 add_mechanism (mech, 1);
263 #endif /* USE_SOLARIS_SHARED_LIBRARIES */
265 #ifdef USE_LINUX_SHARED_LIBRARIES
266 extern gss_mechanism internal_krb5_gss_initialize(void *dl);
269 * read the configuration file to find out what mechanisms to
270 * load, load them, and then load the mechanism defitions in
271 * and add the mechanisms
273 static void linux_initialize ()
275 char buffer[BUFSIZ], *filename, *symname, *endp, *err_string;
278 gss_mechanism (*sym)(void), mech;
280 if ((filename = getenv("GSSAPI_MECH_CONF")) == NULL)
281 filename = MECH_CONF;
283 if ((conffile = fopen(filename, "r")) == NULL) {
284 fprintf(stderr,"fatal error: unable to open %s:"
285 " errno %d (%s)\n", filename, errno, strerror(errno));
289 while (fgets (buffer, BUFSIZ, conffile) != NULL) {
290 /* ignore lines beginning with # */
294 /* find the first white-space character after the filename */
295 for (symname = buffer; *symname && !isspace(*symname); symname++);
297 /* Now find the first non-white-space character */
301 while (*symname && isspace(*symname))
308 /* Find the end of the symname and make sure it is
310 for (endp = symname; *endp && !isspace(*endp); endp++);
315 if ((dl = dlopen(buffer, RTLD_NOW)) == NULL) {
316 /* for debugging only */
317 fprintf(stderr,"can't open %s: %s\n",buffer, dlerror());
321 #if defined(HAVE_KRB5) && defined(HAVE_HEIMDAL)
322 #error Should not have both HAVE_KRB5 and HAVE_HEIMDAL defined!!
326 /* Special case for dealing with MIT krb5 mechanism */
327 if (strcmp(symname, "mechglue_internal_krb5_init") == 0) {
329 fprintf(stderr, "Using special MIT initialization\n");
331 mech = internal_krb5_gss_initialize(dl);
337 /* Special case for dealing with heimdal krb5 mechanism */
338 if (strcmp(symname, "mechglue_internal_heimdal_init") == 0) {
340 fprintf(stderr, "Using special Heimdal initialization\n");
342 mech = internal_heimdal_gss_initialize(dl);
347 if ((sym = (gss_mechanism (*)(void))dlsym(dl, symname)) == NULL) {
348 if ((err_string = dlerror()) != NULL) {
349 fprintf(stderr, "%s: searching for symbol '%s' in '%s'\n",
350 err_string, symname, buffer);
356 /* Call the symbol to get the mechanism table */
360 /* And add the mechanism (or close the shared library) */
363 fprintf(stderr, "Adding mechanism for library '%s'\n", buffer);
365 add_mechanism (mech, 1);
370 "Failed to initialize mechanism for library '%s'\n",
380 #endif /* USE_LINUX_SHARED_LIBRARIES */