#include <mechglueP.h>
#include "gssapiP_generic.h"
#include <stdio.h>
#include <syslog.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <ctype.h>
#include <errno.h>
#include <synch.h>
#include <dlfcn.h>
#include <libintl.h>
#ifndef TEXT_DOMAIN
#error TEXT_DOMAIN not defined
#endif
#define MECH_CONF "/etc/gss/mech"
#define MECH_LIB_PREFIX1 "/usr/lib/"
#ifdef _LP64
#ifdef __sparc
#define MECH_LIB_PREFIX2 "sparcv9/"
#elif defined(__amd64)
#define MECH_LIB_PREFIX2 "amd64/"
#else
you need to define where under /usr the LP64 libraries live for this platform
#endif
#else
#define MECH_LIB_PREFIX2 ""
#endif
#define MECH_LIB_DIR "gss/"
#define MECH_LIB_PREFIX MECH_LIB_PREFIX1 MECH_LIB_PREFIX2 MECH_LIB_DIR
#ifndef MECH_SYM
#define MECH_SYM "gss_mech_initialize"
#endif
#define M_DEFAULT "default"
static gss_mech_info searchMechList(const gss_OID);
static void loadConfigFile(const char *);
static void updateMechList(void);
static gss_mech_info g_mechList = NULL;
static gss_mech_info g_mechListTail = NULL;
static mutex_t g_mechListLock;
static time_t g_confFileModTime = (time_t)0;
OM_uint32
gss_release_oid(minor_status, oid)
OM_uint32 *minor_status;
gss_OID *oid;
{
OM_uint32 major;
gss_mech_info aMech = g_mechList;
if (minor_status == NULL)
return (GSS_S_CALL_INACCESSIBLE_WRITE);
*minor_status = 0;
while (aMech != NULL) {
if (aMech->mech && aMech->mech->gss_internal_release_oid) {
major = aMech->mech->gss_internal_release_oid(
aMech->mech->context,
minor_status, oid);
if (major == GSS_S_COMPLETE)
return (GSS_S_COMPLETE);
map_error(minor_status, aMech->mech);
}
aMech = aMech->next;
}
return (generic_gss_release_oid(minor_status, oid));
}
static time_t g_mechSetTime = (time_t)0;
static gss_OID_set_desc g_mechSet = { 0, NULL };
static mutex_t g_mechSetLock;
OM_uint32
gss_indicate_mechs(minorStatus, mechSet)
OM_uint32 *minorStatus;
gss_OID_set *mechSet;
{
gss_mech_info mList;
char *fileName;
struct stat fileInfo;
int count, i, j;
gss_OID curItem;
if (minorStatus != NULL)
*minorStatus = 0;
if (mechSet != NULL)
*mechSet = GSS_C_NO_OID_SET;
if (minorStatus == NULL || mechSet == NULL)
return (GSS_S_CALL_INACCESSIBLE_WRITE);
fileName = MECH_CONF;
if ((stat(fileName, &fileInfo) == 0 &&
fileInfo.st_mtime > g_mechSetTime)) {
(void) mutex_lock(&g_mechListLock);
if (fileInfo.st_mtime > g_confFileModTime)
{
g_confFileModTime = fileInfo.st_mtime;
loadConfigFile(fileName);
}
(void) mutex_lock(&g_mechSetLock);
if (g_mechSet.count != 0) {
for (i = 0; i < g_mechSet.count; i++)
free(g_mechSet.elements[i].elements);
free(g_mechSet.elements);
g_mechSet.elements = NULL;
g_mechSet.count = 0;
}
mList = g_mechList;
count = 0;
while (mList != NULL) {
count++;
mList = mList->next;
}
if (count > 0) {
g_mechSet.elements =
(gss_OID) calloc(count, sizeof (gss_OID_desc));
if (g_mechSet.elements == NULL) {
(void) mutex_unlock(&g_mechSetLock);
(void) mutex_unlock(&g_mechListLock);
return (GSS_S_FAILURE);
}
(void) memset(g_mechSet.elements, 0,
count * sizeof (gss_OID_desc));
g_mechSet.count = count;
count = 0;
mList = g_mechList;
while (mList != NULL) {
curItem = &(g_mechSet.elements[count]);
curItem->elements = (void*)
malloc(mList->mech_type->length);
if (curItem->elements == NULL) {
for (i = 0; i < count; i++) {
free(g_mechSet.elements[i].
elements);
}
free(g_mechSet.elements);
g_mechSet.count = 0;
g_mechSet.elements = NULL;
(void) mutex_unlock(&g_mechSetLock);
(void) mutex_unlock(&g_mechListLock);
return (GSS_S_FAILURE);
}
g_OID_copy(curItem, mList->mech_type);
count++;
mList = mList->next;
}
}
g_mechSetTime = fileInfo.st_mtime;
(void) mutex_unlock(&g_mechSetLock);
(void) mutex_unlock(&g_mechListLock);
}
if ((*mechSet =
(gss_OID_set) malloc(sizeof (gss_OID_set_desc))) == NULL)
{
return (GSS_S_FAILURE);
}
(void) mutex_lock(&g_mechSetLock);
if (((*mechSet)->elements =
(void*) calloc(g_mechSet.count, sizeof (gss_OID_desc)))
== NULL)
{
(void) mutex_unlock(&g_mechSetLock);
free(*mechSet);
*mechSet = NULL;
return (GSS_S_FAILURE);
}
(void) memcpy((*mechSet)->elements, g_mechSet.elements,
g_mechSet.count * sizeof (gss_OID_desc));
(*mechSet)->count = g_mechSet.count;
for (i = 0; i < (*mechSet)->count; i++) {
curItem = &((*mechSet)->elements[i]);
curItem->elements =
(void *) malloc(g_mechSet.elements[i].length);
if (curItem->elements == NULL) {
(void) mutex_unlock(&g_mechSetLock);
for (j = 0; j < i; j++) {
free((*mechSet)->elements[j].elements);
}
free((*mechSet)->elements);
free(mechSet);
*mechSet = NULL;
return (GSS_S_FAILURE);
}
g_OID_copy(curItem, &g_mechSet.elements[i]);
}
(void) mutex_unlock(&g_mechSetLock);
return (GSS_S_COMPLETE);
}
char *
__gss_get_modOptions(oid)
const gss_OID oid;
{
gss_mech_info aMech;
char *modOptions = NULL;
(void) mutex_lock(&g_mechListLock);
updateMechList();
(void) mutex_unlock(&g_mechListLock);
if ((aMech = searchMechList(oid)) == NULL ||
aMech->optionStr == NULL) {
return (NULL);
}
(void) mutex_lock(&g_mechListLock);
if (aMech->optionStr)
modOptions = strdup(aMech->optionStr);
(void) mutex_unlock(&g_mechListLock);
return (modOptions);
}
char *
__gss_get_kmodName(oid)
const gss_OID oid;
{
gss_mech_info aMech;
char *kmodName = NULL;
(void) mutex_lock(&g_mechListLock);
updateMechList();
(void) mutex_unlock(&g_mechListLock);
if ((aMech = searchMechList(oid)) == NULL || aMech->kmodName == NULL) {
return (NULL);
}
(void) mutex_lock(&g_mechListLock);
if (aMech->kmodName)
kmodName = strdup(aMech->kmodName);
(void) mutex_unlock(&g_mechListLock);
return (kmodName);
}
OM_uint32
__gss_mech_to_oid(const char *mechStr, gss_OID* oid)
{
gss_mech_info aMech;
if (oid == NULL)
return (GSS_S_CALL_INACCESSIBLE_WRITE);
*oid = GSS_C_NULL_OID;
if ((mechStr == NULL) || (strlen(mechStr) == 0) ||
(strcasecmp(mechStr, M_DEFAULT) == 0))
return (GSS_S_COMPLETE);
(void) mutex_lock(&g_mechListLock);
updateMechList();
(void) mutex_unlock(&g_mechListLock);
aMech = g_mechList;
while (aMech != NULL) {
if ((aMech->mechNameStr) &&
strcmp(aMech->mechNameStr, mechStr) == 0) {
*oid = aMech->mech_type;
return (GSS_S_COMPLETE);
}
aMech = aMech->next;
}
return (GSS_S_FAILURE);
}
const char *
__gss_oid_to_mech(const gss_OID oid)
{
gss_mech_info aMech;
if (oid == GSS_C_NULL_OID)
return (M_DEFAULT);
(void) mutex_lock(&g_mechListLock);
updateMechList();
(void) mutex_unlock(&g_mechListLock);
if ((aMech = searchMechList(oid)) == NULL)
return (NULL);
return (aMech->mechNameStr);
}
OM_uint32
__gss_get_mechanisms(char *mechArray[], int arrayLen)
{
gss_mech_info aMech;
int i;
if (mechArray == NULL || arrayLen < 1)
return (GSS_S_CALL_INACCESSIBLE_WRITE);
(void) mutex_lock(&g_mechListLock);
updateMechList();
(void) mutex_unlock(&g_mechListLock);
aMech = g_mechList;
for (i = 1; i < arrayLen; i++) {
if (aMech != NULL) {
*mechArray = aMech->mechNameStr;
mechArray++;
aMech = aMech->next;
} else
break;
}
*mechArray = NULL;
return (GSS_S_COMPLETE);
}
static void
updateMechList(void)
{
char *fileName;
struct stat fileInfo;
fileName = MECH_CONF;
if (stat(fileName, &fileInfo) == 0 &&
(fileInfo.st_mtime > g_confFileModTime)) {
loadConfigFile(fileName);
g_confFileModTime = fileInfo.st_mtime;
}
}
gss_mechanism
__gss_get_mechanism(oid)
const gss_OID oid;
{
gss_mech_info aMech;
gss_mechanism (*sym)(const gss_OID);
void *dl;
if ((aMech = searchMechList(oid)) != NULL && aMech->mech) {
return (aMech->mech);
}
(void) mutex_lock(&g_mechListLock);
updateMechList();
aMech = searchMechList(oid);
if (aMech == NULL) {
(void) mutex_unlock(&g_mechListLock);
return ((gss_mechanism)NULL);
}
if (aMech->mech) {
(void) mutex_unlock(&g_mechListLock);
return (aMech->mech);
}
if ((dl = dlopen(aMech->uLibName, RTLD_NOW)) == NULL) {
(void) syslog(LOG_INFO, "libgss dlopen(%s): %s\n",
aMech->uLibName, dlerror());
(void) mutex_unlock(&g_mechListLock);
return ((gss_mechanism)NULL);
}
if ((sym = (gss_mechanism (*)(const gss_OID))dlsym(dl, MECH_SYM))
== NULL) {
(void) dlclose(dl);
(void) syslog(LOG_INFO, "unable to initialize mechanism"
" library [%s]\n", aMech->uLibName);
(void) mutex_unlock(&g_mechListLock);
return ((gss_mechanism)NULL);
}
aMech->mech = (*sym)(aMech->mech_type);
if (aMech->mech == NULL) {
(void) dlclose(dl);
(void) syslog(LOG_INFO, "unable to initialize mechanism"
" library [%s]\n", aMech->uLibName);
(void) mutex_unlock(&g_mechListLock);
return ((gss_mechanism)NULL);
}
aMech->dl_handle = dl;
(void) mutex_unlock(&g_mechListLock);
return (aMech->mech);
}
gss_mechanism_ext
__gss_get_mechanism_ext(oid)
const gss_OID oid;
{
gss_mech_info aMech;
gss_mechanism_ext mech_ext;
if ((aMech = searchMechList(oid)) != NULL && aMech->mech_ext != NULL)
return (aMech->mech_ext);
if (__gss_get_mechanism(oid) == NULL)
return (NULL);
if (aMech->dl_handle == NULL)
return (NULL);
mech_ext = (gss_mechanism_ext)malloc(sizeof (struct gss_config_ext));
if (mech_ext == NULL)
return (NULL);
mech_ext->gss_acquire_cred_with_password =
(gss_acquire_cred_with_password_sfct)dlsym(aMech->dl_handle,
"gssspi_acquire_cred_with_password");
(void) mutex_lock(&g_mechListLock);
if (aMech->mech_ext == NULL)
aMech->mech_ext = mech_ext;
else
free(mech_ext);
(void) mutex_unlock(&g_mechListLock);
return (aMech->mech_ext);
}
static gss_mech_info searchMechList(oid)
const gss_OID oid;
{
gss_mech_info aMech = g_mechList;
if (oid == GSS_C_NULL_OID)
return (aMech);
while (aMech != NULL) {
if (g_OID_equal(aMech->mech_type, oid))
return (aMech);
aMech = aMech->next;
}
return ((gss_mech_info) NULL);
}
static void loadConfigFile(fileName)
const char *fileName;
{
char buffer[BUFSIZ], *oidStr, *oid, *sharedLib, *kernMod, *endp;
char *modOptions;
char sharedPath[sizeof (MECH_LIB_PREFIX) + BUFSIZ];
char *tmpStr;
FILE *confFile;
gss_OID mechOid;
gss_mech_info aMech, tmp;
OM_uint32 minor;
gss_buffer_desc oidBuf;
if ((confFile = fopen(fileName, "rF")) == NULL) {
return;
}
(void) memset(buffer, 0, sizeof (buffer));
while (fgets(buffer, BUFSIZ, confFile) != NULL) {
if (*buffer == '#')
continue;
oidStr = buffer;
for (oid = buffer; *oid && !isspace(*oid); oid++);
if (*oid) {
*oid = '\0';
oid++;
while (*oid && isspace(*oid))
oid++;
}
if (! *oid)
continue;
for (endp = oid; *endp && !isspace(*endp); endp++)
;
if (*endp) {
*endp = '\0';
}
oidBuf.value = (void *)oid;
oidBuf.length = strlen(oid);
if (generic_gss_str_to_oid(&minor, &oidBuf, &mechOid)
!= GSS_S_COMPLETE) {
(void) syslog(LOG_INFO, "invalid mechanism oid"
" [%s] in configuration file", oid);
continue;
}
aMech = searchMechList(mechOid);
if (aMech && aMech->mech) {
free(mechOid->elements);
free(mechOid);
continue;
}
for (sharedLib = endp+1; *sharedLib && isspace(*sharedLib);
sharedLib++)
;
if (! *sharedLib) {
free(mechOid->elements);
free(mechOid);
continue;
}
for (endp = sharedLib; *endp && !isspace(*endp); endp++)
;
if (*endp) {
*endp = '\0';
}
for (kernMod = endp+1; *kernMod && isspace(*kernMod);
kernMod++)
;
if (*kernMod && *kernMod != '[') {
for (endp = kernMod; *endp && !isspace(*endp); endp++)
;
if (*endp) {
*endp = '\0';
}
} else
kernMod = NULL;
for (modOptions = endp+1; *modOptions && isspace(*modOptions);
modOptions++);
if (*modOptions == '[') {
for (modOptions = modOptions+1;
*modOptions && isspace(*modOptions);
modOptions++);
for (endp = modOptions;
*endp && *endp != ']'; endp++);
if (endp)
*endp = '\0';
} else {
modOptions = NULL;
}
(void) strcpy(sharedPath, MECH_LIB_PREFIX);
(void) strcat(sharedPath, sharedLib);
if (aMech) {
if (aMech->kmodName) {
free(aMech->kmodName);
aMech->kmodName = NULL;
}
if (aMech->optionStr) {
free(aMech->optionStr);
aMech->optionStr = NULL;
}
if ((tmpStr = strdup(sharedPath)) != NULL) {
if (aMech->uLibName)
free(aMech->uLibName);
aMech->uLibName = tmpStr;
}
if (kernMod)
aMech->kmodName = strdup(kernMod);
if (modOptions)
aMech->optionStr = strdup(modOptions);
free(mechOid->elements);
free(mechOid);
continue;
}
aMech = malloc(sizeof (struct gss_mech_config));
if (aMech == NULL) {
free(mechOid->elements);
free(mechOid);
continue;
}
(void) memset(aMech, 0, sizeof (struct gss_mech_config));
aMech->mech_type = mechOid;
aMech->uLibName = strdup(sharedPath);
aMech->mechNameStr = strdup(oidStr);
if (aMech->uLibName == NULL || aMech->mechNameStr == NULL) {
if (aMech->uLibName)
free(aMech->uLibName);
if (aMech->mechNameStr)
free(aMech->mechNameStr);
free(mechOid->elements);
free(mechOid);
free(aMech);
continue;
}
if (kernMod)
aMech->kmodName = strdup(kernMod);
if (modOptions)
aMech->optionStr = strdup(modOptions);
tmp = g_mechListTail;
g_mechListTail = aMech;
if (tmp != NULL)
tmp->next = aMech;
if (g_mechList == NULL)
g_mechList = aMech;
}
(void) fclose(confFile);
}