summaryrefslogtreecommitdiffstats
path: root/support/gssapi/g_initialize.c
blob: 9523d40af76d0cb2a80393e95ae4b1c58f9d5b6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/* #ident  "@(#)g_initialize.c 1.2     96/02/06 SMI" */

/*
 * Copyright 1996 by Sun Microsystems, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appears in all copies and
 * that both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of Sun Microsystems not be used
 * in advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission. Sun Microsystems makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 * SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * This function will initialize the gssapi mechglue library
 */

#include "config.h"
#include "mglueP.h"
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

#ifdef USE_SOLARIS_SHARED_LIBRARIES
#include <dlfcn.h>

#define MECH_CONF "/etc/mech.conf"
#define MECH_SYM "gss_mech_initialize"

static void solaris_initialize (void);
#endif /* USE_SOLARIS_SHARED_LIBRARIES */

#ifdef __linux__
#define USE_LINUX_SHARED_LIBRARIES
#endif

#ifdef USE_LINUX_SHARED_LIBRARIES
#include <dlfcn.h>
#define MECH_CONF "/etc/gssapi_mech.conf"
#define MECH_SYM "gss_mech_initialize"
static void linux_initialize (void);
#endif /* USE_LINUX_SHARED_LIBRARIES */

#define g_OID_equal(o1,o2) \
   (((o1)->length == (o2)->length) && \
    (memcmp((o1)->elements,(o2)->elements,(int) (o1)->length) == 0))

extern gss_mechanism krb5_gss_initialize();

static int _gss_initialized = 0;

static struct gss_config null_mech = {
  {0,NULL}};

gss_mechanism *__gss_mechs_array = NULL;

/*
 * This function will add a new mechanism to the mechs_array
 */

static OM_uint32
add_mechanism (mech, replace)
     gss_mechanism mech;
     int replace;
{
    gss_mechanism *	temp_array;
    gss_OID_set		mech_names;
    OM_uint32		minor_status, major_status;
    unsigned int 	i;

    if (mech == NULL)
	return GSS_S_COMPLETE;

    /* initialize the mechs_array if it hasn't already been initialized */
    if (__gss_mechs_array == NULL) {
	__gss_mechs_array = (gss_mechanism *) malloc (sizeof(gss_mechanism));

	if (__gss_mechs_array == NULL)
	    return ENOMEM;

	__gss_mechs_array[0] = &null_mech;
    }

    /*
     * Find the length of __gss_mechs_array, and look for an existing
     * entry for this OID
     */
    for (i=0; __gss_mechs_array[i]->mech_type.length != 0; i++) {
	if (!g_OID_equal(&__gss_mechs_array[i]->mech_type,
			 &mech->mech_type))
	    continue;

	/* We found a match.  Replace it? */
	if (!replace)
	    return GSS_S_FAILURE;

	__gss_mechs_array[i] = mech;
	return GSS_S_COMPLETE;
    }

    /* we didn't find it -- add it to the end of the __gss_mechs_array */
    temp_array = (gss_mechanism *) realloc(__gss_mechs_array,
					   (i+2)*sizeof(gss_mechanism));

    if (temp_array == NULL)
	return ENOMEM;

    temp_array[i++] = mech;
    temp_array[i] = &null_mech;

    __gss_mechs_array = temp_array;

    /*
     * OK, now let's register all of the name types this mechanism
     * knows how to deal with.
     */
    major_status = gss_inquire_names_for_mech(&minor_status, &mech->mech_type,
					      &mech_names);
    if (major_status != GSS_S_COMPLETE)
	return (GSS_S_COMPLETE);
    for (i=0; i < mech_names->count; i++) {
	gss_add_mech_name_type(&minor_status, &mech_names->elements[i],
			       &mech->mech_type);
    }
    (void) gss_release_oid_set(&minor_status, &mech_names);

    return GSS_S_COMPLETE;
}

void gss_initialize ()
{
    /* Make sure we've not run already */
    if (_gss_initialized)
	return;
    _gss_initialized = 1;

#ifdef USE_SOLARIS_SHARED_LIBRARIES
    solaris_initialize();

#elif defined(USE_LINUX_SHARED_LIBRARIES)
    linux_initialize();

#else
    {
	gss_mechanism mech;

	/*
	 * Use hard-coded in mechanisms...  I need to know what mechanisms
	 * are supported...  As more mechanisms become supported, they
	 * should be added here, unless shared libraries are used.
	 */

	/* Initialize the krb5 mechanism */
	mech = (gss_mechanism)krb5_gss_initialize();
	if (mech)
	    add_mechanism (mech, 1);
    }

#endif /* USE_SOLARIS_SHARED_LIBRARIES */

#if !defined(macintosh)
    if (__gss_mechs_array == NULL) { /* this is very bad! */
      fprintf(stderr,"gss_initialize fatal error: no mechanisms loaded!\n");
      exit(-1);
    }
#else
    /*
     * Nothing for now, since this should never happen using static
     * mechanism loading.
     */
#endif

    return;
}

#ifdef USE_SOLARIS_SHARED_LIBRARIES
/*
 * read the configuration file to find out what mechanisms to
 * load, load them, and then load the mechanism defitions in
 * and add the mechanisms
 */
static void solaris_initialize ()
{
    char buffer[BUFSIZ], *filename, *symname, *endp;
    FILE *conffile;
    void *dl;
    gss_mechanism (*sym)(void), mech;

    if ((filename = getenv("GSSAPI_MECH_CONF")) == NULL)
	filename = MECH_CONF;

    if ((conffile = fopen(filename, "r")) == NULL) {
		fprintf(stderr,"fatal error: unable to open %s:"
			" errno %d (%s)\n", filename, errno, strerror(errno));
		return;
    }

    while (fgets (buffer, BUFSIZ, conffile) != NULL) {
	/* ignore lines beginning with # */
	if (*buffer == '#')
	    continue;

	/* find the first white-space character after the filename */
	for (symname = buffer; *symname && !isspace(*symname); symname++);

	/* Now find the first non-white-space character */
	if (*symname) {
	    *symname = '\0';
	    symname++;
	    while (*symname && isspace(*symname))
		symname++;
	}

	if (! *symname)
	    symname = MECH_SYM;
	else {
	  /* Find the end of the symname and make sure it is NULL-terminated */
	  for (endp = symname; *endp && !isspace(*endp); endp++);
	  if (*endp)
	    *endp = '\0';
	}

	if ((dl = dlopen(buffer, RTLD_NOW)) == NULL) {
		/* for debugging only */
		fprintf(stderr,"can't open %s: %s\n",buffer, dlerror());
		continue;
	}

	if ((sym = (gss_mechanism (*)(void))dlsym(dl, symname)) == NULL) {
	    dlclose(dl);
	    continue;
	}

	/* Call the symbol to get the mechanism table */
	mech = sym();

	/* And add the mechanism (or close the shared library) */
	if (mech)
	    add_mechanism (mech, 1);
	else
	    dlclose(dl);

    } /* while */

    return;
}
#endif /* USE_SOLARIS_SHARED_LIBRARIES */

#ifdef USE_LINUX_SHARED_LIBRARIES
extern gss_mechanism internal_krb5_gss_initialize(void *dl);

/*
 * read the configuration file to find out what mechanisms to
 * load, load them, and then load the mechanism defitions in
 * and add the mechanisms
 */
static void linux_initialize ()
{
    char buffer[BUFSIZ], *filename, *symname, *endp, *err_string;
    FILE *conffile;
    void *dl;
    gss_mechanism (*sym)(void), mech;

    if ((filename = getenv("GSSAPI_MECH_CONF")) == NULL)
	filename = MECH_CONF;

    if ((conffile = fopen(filename, "r")) == NULL) {
		fprintf(stderr,"fatal error: unable to open %s:"
			" errno %d (%s)\n", filename, errno, strerror(errno));
		return;
    }

    while (fgets (buffer, BUFSIZ, conffile) != NULL) {
	/* ignore lines beginning with # */
	if (*buffer == '#')
	    continue;

	/* find the first white-space character after the filename */
	for (symname = buffer; *symname && !isspace(*symname); symname++);

	/* Now find the first non-white-space character */
	if (*symname) {
	    *symname = '\0';
	    symname++;
	    while (*symname && isspace(*symname))
		symname++;
	}

	if (! *symname)
	    symname = MECH_SYM;
	else {
	    /* Find the end of the symname and make sure it is
	     * NULL-terminated */
	    for (endp = symname; *endp && !isspace(*endp); endp++);
	    if (*endp)
		*endp = '\0';
	}

	if ((dl = dlopen(buffer, RTLD_NOW)) == NULL) {
	    /* for debugging only */
	    fprintf(stderr,"can't open %s: %s\n",buffer, dlerror());
	    continue;
	}

#if defined(HAVE_KRB5) && defined(HAVE_HEIMDAL)
#error Should not have both HAVE_KRB5 and HAVE_HEIMDAL defined!!
#endif

#ifdef HAVE_KRB5
	/* Special case for dealing with MIT krb5 mechanism */
	if (strcmp(symname, "mechglue_internal_krb5_init") == 0) {
#ifdef DEBUG
	    fprintf(stderr, "Using special MIT initialization\n");
#endif
	    mech = internal_krb5_gss_initialize(dl);
	}
	else
#endif

#ifdef HAVE_HEIMDAL
	/* Special case for dealing with heimdal krb5 mechanism */
	if (strcmp(symname, "mechglue_internal_heimdal_init") == 0) {
#ifdef DEBUG
	    fprintf(stderr, "Using special Heimdal initialization\n");
#endif
	    mech = internal_heimdal_gss_initialize(dl);
	}
	else
#endif
	{
	    if ((sym = (gss_mechanism (*)(void))dlsym(dl, symname)) == NULL) {
		if ((err_string = dlerror()) != NULL) {
		    fprintf(stderr, "%s: searching for symbol '%s' in '%s'\n",
			    err_string, symname, buffer);
		    dlclose(dl);
		}
		continue;
	    }

	    /* Call the symbol to get the mechanism table */
	    mech = sym();
	}

	/* And add the mechanism (or close the shared library) */
	if (mech) {
#ifdef DEBUG
	    fprintf(stderr, "Adding mechanism for library '%s'\n", buffer);
#endif
	    add_mechanism (mech, 1);
	}
	else {
#ifdef DEBUG
	    fprintf(stderr,
		    "Failed to initialize mechanism for library '%s'\n",
		     buffer);
#endif
	    dlclose(dl);
	}

    } /* while */

    return;
}
#endif /* USE_LINUX_SHARED_LIBRARIES */