#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
#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;
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;
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;
}
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;
if (!replace)
return GSS_S_FAILURE;
__gss_mechs_array[i] = mech;
return GSS_S_COMPLETE;
}
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;
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 ()
{
gss_mechanism mech;
if (_gss_initialized)
return;
_gss_initialized = 1;
#ifdef USE_SOLARIS_SHARED_LIBRARIES
solaris_initialize();
#else
mech = (gss_mechanism)krb5_gss_initialize();
if (mech)
add_mechanism (mech, 1);
#endif
#if !defined(macintosh)
if (__gss_mechs_array == NULL) {
fprintf(stderr,"gss_initialize fatal error: no mechanisms loaded!\n");
exit(-1);
}
#else
#endif
return;
}
#ifdef USE_SOLARIS_SHARED_LIBRARIES
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)
return;
while (fgets (buffer, BUFSIZ, conffile) != NULL) {
if (*buffer == '#')
continue;
for (symname = buffer; *symname && !isspace(*symname); symname++);
if (*symname) {
*symname = '\0';
symname++;
while (*symname && isspace(*symname))
symname++;
}
if (! *symname)
symname = MECH_SYM;
else {
for (endp = symname; *endp && !isspace(*endp); endp++);
if (*endp)
*endp = '\0';
}
if ((dl = dlopen(buffer, RTLD_NOW)) == NULL) {
fprintf(stderr,"can't open %s: %s\n",buffer, dlerror());
continue;
}
if ((sym = (gss_mechanism (*)(void))dlsym(dl, symname)) == NULL) {
dlclose(dl);
continue;
}
mech = sym();
if (mech)
add_mechanism (mech, 1);
else
dlclose(dl);
}
return;
}
#endif