xref: /illumos-gate/usr/src/lib/libgss/g_initialize.c (revision 004388ebfdfe2ed7dfd2d153a876dfcc22d2c006)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*004388ebScasper  * Common Development and Distribution License (the "License").
6*004388ebScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*004388ebScasper  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * This file contains functions to initialize the gssapi library and
307c478bd9Sstevel@tonic-gate  * load mechanism libraries.
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * It also contain functions requiring direct access to the mechanism's
337c478bd9Sstevel@tonic-gate  * list (gss_inidicate_mechs and gss_release_oid) as well as support
347c478bd9Sstevel@tonic-gate  * functions which translate the mechanism strings to oids and vise versa.
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * The mechanism libraries are loaded on demand.  This is triggered
377c478bd9Sstevel@tonic-gate  * through the get_mechanism function call.
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  * Updates to the mechList are performed with the following restrictions:
407c478bd9Sstevel@tonic-gate  *	- once a library is loaded, none of the fields are updated
417c478bd9Sstevel@tonic-gate  *	- existing entiries for non-loaded mechs, will have the
427c478bd9Sstevel@tonic-gate  *		library and kernel module names updated only
437c478bd9Sstevel@tonic-gate  *		(i.e. the mech oid and mech name will not be updated)
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include <mechglueP.h>
477c478bd9Sstevel@tonic-gate #include <stdio.h>
487c478bd9Sstevel@tonic-gate #include <syslog.h>
497c478bd9Sstevel@tonic-gate #include <stdlib.h>
507c478bd9Sstevel@tonic-gate #include <string.h>
517c478bd9Sstevel@tonic-gate #include <sys/stat.h>
527c478bd9Sstevel@tonic-gate #include <ctype.h>
537c478bd9Sstevel@tonic-gate #include <errno.h>
547c478bd9Sstevel@tonic-gate #include <synch.h>
557c478bd9Sstevel@tonic-gate #include <dlfcn.h>
567c478bd9Sstevel@tonic-gate #include <libintl.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN
607c478bd9Sstevel@tonic-gate #error TEXT_DOMAIN not defined
617c478bd9Sstevel@tonic-gate #endif
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #define	MECH_CONF "/etc/gss/mech"
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #define	MECH_LIB_PREFIX1	"/usr/lib/"
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * This #ifdef mess figures out if we are to be compiled into
697c478bd9Sstevel@tonic-gate  * a sparcv9/lp64 binary for the purposes of figuring the absolute location
707c478bd9Sstevel@tonic-gate  * of gss-api mechanism modules.
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate #ifdef	_LP64
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #ifdef	__sparc
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #define	MECH_LIB_PREFIX2	"sparcv9/"
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #elif defined(__amd64)
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #define	MECH_LIB_PREFIX2	"amd64/"
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #else	/* __sparc */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate you need to define where under /usr the LP64 libraries live for this platform
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #endif	/* __sparc */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #else	/* _LP64 */
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #define	MECH_LIB_PREFIX2	""
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate #define	MECH_LIB_DIR		"gss/"
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #define	MECH_LIB_PREFIX	MECH_LIB_PREFIX1 MECH_LIB_PREFIX2 MECH_LIB_DIR
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate #ifndef	MECH_SYM
1007c478bd9Sstevel@tonic-gate #define	MECH_SYM "gss_mech_initialize"
1017c478bd9Sstevel@tonic-gate #endif
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate #define	M_DEFAULT	"default"
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /* Local functions */
1067c478bd9Sstevel@tonic-gate static gss_mech_info searchMechList(const gss_OID);
1077c478bd9Sstevel@tonic-gate static void loadConfigFile(const char *);
1087c478bd9Sstevel@tonic-gate static void updateMechList(void);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * list of mechanism libraries and their entry points.
1137c478bd9Sstevel@tonic-gate  * the list also maintains state of the mech libraries (loaded or not).
1147c478bd9Sstevel@tonic-gate  */
1157c478bd9Sstevel@tonic-gate static gss_mech_info g_mechList = NULL;
1167c478bd9Sstevel@tonic-gate static gss_mech_info g_mechListTail = NULL;
1177c478bd9Sstevel@tonic-gate static mutex_t g_mechListLock;
1187c478bd9Sstevel@tonic-gate static time_t g_confFileModTime = (time_t)0;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate  * function used to reclaim the memory used by a gss_OID structure.
1227c478bd9Sstevel@tonic-gate  * This routine requires direct access to the mechList.
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate OM_uint32
1257c478bd9Sstevel@tonic-gate gss_release_oid(minor_status, oid)
1267c478bd9Sstevel@tonic-gate OM_uint32 *minor_status;
1277c478bd9Sstevel@tonic-gate gss_OID *oid;
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	OM_uint32 major;
1307c478bd9Sstevel@tonic-gate 	gss_mech_info aMech = g_mechList;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	if (minor_status == NULL)
1337c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	*minor_status = 0;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	while (aMech != NULL) {
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		/*
1407c478bd9Sstevel@tonic-gate 		 * look through the loaded mechanism libraries for
1417c478bd9Sstevel@tonic-gate 		 * gss_internal_release_oid until one returns success.
1427c478bd9Sstevel@tonic-gate 		 * gss_internal_release_oid will only return success when
1437c478bd9Sstevel@tonic-gate 		 * the OID was recognized as an internal mechanism OID. if no
1447c478bd9Sstevel@tonic-gate 		 * mechanisms recognize the OID, then call the generic version.
1457c478bd9Sstevel@tonic-gate 		 */
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 		/*
1487c478bd9Sstevel@tonic-gate 		 * we can walk the mechanism list without a mutex, because we
1497c478bd9Sstevel@tonic-gate 		 * are only looking at fields which once read will never change.
1507c478bd9Sstevel@tonic-gate 		 * Mechanism entries are always added to the end, and as
1517c478bd9Sstevel@tonic-gate 		 * complete entries.
1527c478bd9Sstevel@tonic-gate 		 */
1537c478bd9Sstevel@tonic-gate 		if (aMech->mech && aMech->mech->gss_internal_release_oid) {
1547c478bd9Sstevel@tonic-gate 			major = aMech->mech->gss_internal_release_oid(
1557c478bd9Sstevel@tonic-gate 					aMech->mech->context,
1567c478bd9Sstevel@tonic-gate 					minor_status, oid);
1577c478bd9Sstevel@tonic-gate 			if (major == GSS_S_COMPLETE)
1587c478bd9Sstevel@tonic-gate 				return (GSS_S_COMPLETE);
1597c478bd9Sstevel@tonic-gate 		}
1607c478bd9Sstevel@tonic-gate 		aMech = aMech->next;
1617c478bd9Sstevel@tonic-gate 	} /* while */
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	return (generic_gss_release_oid(minor_status, oid));
1647c478bd9Sstevel@tonic-gate } /* gss_release_oid */
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * this function will return an oid set indicating available mechanisms.
1697c478bd9Sstevel@tonic-gate  * The set returned is based on configuration file entries and
1707c478bd9Sstevel@tonic-gate  * NOT on the loaded mechanisms.  This function does not check if any
1717c478bd9Sstevel@tonic-gate  * of these can actually be loaded.
1727c478bd9Sstevel@tonic-gate  * This routine needs direct access to the mechanism list.
1737c478bd9Sstevel@tonic-gate  * To avoid reading the configuration file each call, we will save a
1747c478bd9Sstevel@tonic-gate  * a mech oid set, and only update it once the file has changed.
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate static time_t g_mechSetTime = (time_t)0;
1777c478bd9Sstevel@tonic-gate static gss_OID_set_desc g_mechSet = { 0, NULL };
1787c478bd9Sstevel@tonic-gate static mutex_t g_mechSetLock;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate OM_uint32
1827c478bd9Sstevel@tonic-gate gss_indicate_mechs(minorStatus, mechSet)
1837c478bd9Sstevel@tonic-gate OM_uint32 *minorStatus;
1847c478bd9Sstevel@tonic-gate gss_OID_set *mechSet;
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate 	gss_mech_info mList;
1877c478bd9Sstevel@tonic-gate 	char *fileName;
1887c478bd9Sstevel@tonic-gate 	struct stat fileInfo;
1897c478bd9Sstevel@tonic-gate 	int count, i, j;
1907c478bd9Sstevel@tonic-gate 	gss_OID curItem;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if (!minorStatus)
1937c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	*minorStatus = 0;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	/* check output parameter */
1997c478bd9Sstevel@tonic-gate 	if (mechSet == NULL)
2007c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	fileName = MECH_CONF;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	/*
2057c478bd9Sstevel@tonic-gate 	 * If we have already computed the mechanisms supported and if it
2067c478bd9Sstevel@tonic-gate 	 * is still valid; make a copy and return to caller,
2077c478bd9Sstevel@tonic-gate 	 * otherwise build it first.
2087c478bd9Sstevel@tonic-gate 	 */
2097c478bd9Sstevel@tonic-gate 	if ((stat(fileName, &fileInfo) == 0 &&
2107c478bd9Sstevel@tonic-gate 		fileInfo.st_mtime > g_mechSetTime)) {
2117c478bd9Sstevel@tonic-gate 		/*
2127c478bd9Sstevel@tonic-gate 		 * lock the mutex since we will be updating
2137c478bd9Sstevel@tonic-gate 		 * the mechList structure
2147c478bd9Sstevel@tonic-gate 		 * we need to keep the lock while we build the mechanism list
2157c478bd9Sstevel@tonic-gate 		 * since we are accessing parts of the mechList which could be
2167c478bd9Sstevel@tonic-gate 		 * modified.
2177c478bd9Sstevel@tonic-gate 		 */
2187c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&g_mechListLock);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		/*
2217c478bd9Sstevel@tonic-gate 		 * this checks for the case when we need to re-construct the
2227c478bd9Sstevel@tonic-gate 		 * g_mechSet structure, but the mechanism list is upto date
2237c478bd9Sstevel@tonic-gate 		 * (because it has been read by someone calling
2247c478bd9Sstevel@tonic-gate 		 * __gss_get_mechanism)
2257c478bd9Sstevel@tonic-gate 		 */
2267c478bd9Sstevel@tonic-gate 		if (fileInfo.st_mtime > g_confFileModTime)
2277c478bd9Sstevel@tonic-gate 		{
2287c478bd9Sstevel@tonic-gate 			g_confFileModTime = fileInfo.st_mtime;
2297c478bd9Sstevel@tonic-gate 			loadConfigFile(fileName);
2307c478bd9Sstevel@tonic-gate 		}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 		/*
2337c478bd9Sstevel@tonic-gate 		 * we need to lock the mech set so that no one else will
2347c478bd9Sstevel@tonic-gate 		 * try to read it as we are re-creating it
2357c478bd9Sstevel@tonic-gate 		 */
2367c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&g_mechSetLock);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 		/* if the oid list already exists we must free it first */
2397c478bd9Sstevel@tonic-gate 		if (g_mechSet.count != 0) {
2407c478bd9Sstevel@tonic-gate 			for (i = 0; i < g_mechSet.count; i++)
2417c478bd9Sstevel@tonic-gate 				free(g_mechSet.elements[i].elements);
2427c478bd9Sstevel@tonic-gate 			free(g_mechSet.elements);
2437c478bd9Sstevel@tonic-gate 			g_mechSet.elements = NULL;
2447c478bd9Sstevel@tonic-gate 			g_mechSet.count = 0;
2457c478bd9Sstevel@tonic-gate 		}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 		/* determine how many elements to have in the list */
2487c478bd9Sstevel@tonic-gate 		mList = g_mechList;
2497c478bd9Sstevel@tonic-gate 		count = 0;
2507c478bd9Sstevel@tonic-gate 		while (mList != NULL) {
2517c478bd9Sstevel@tonic-gate 			count++;
2527c478bd9Sstevel@tonic-gate 			mList = mList->next;
2537c478bd9Sstevel@tonic-gate 		}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 		/* this should always be true, but.... */
2567c478bd9Sstevel@tonic-gate 		if (count > 0) {
2577c478bd9Sstevel@tonic-gate 			g_mechSet.elements =
2587c478bd9Sstevel@tonic-gate 				(gss_OID) calloc(count, sizeof (gss_OID_desc));
2597c478bd9Sstevel@tonic-gate 			if (g_mechSet.elements == NULL) {
2607c478bd9Sstevel@tonic-gate 				(void) mutex_unlock(&g_mechSetLock);
2617c478bd9Sstevel@tonic-gate 				(void) mutex_unlock(&g_mechListLock);
2627c478bd9Sstevel@tonic-gate 				return (GSS_S_FAILURE);
2637c478bd9Sstevel@tonic-gate 			}
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 			(void) memset(g_mechSet.elements, 0,
2667c478bd9Sstevel@tonic-gate 				count * sizeof (gss_OID_desc));
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 			/* now copy each oid element */
2697c478bd9Sstevel@tonic-gate 			g_mechSet.count = count;
2707c478bd9Sstevel@tonic-gate 			count = 0;
2717c478bd9Sstevel@tonic-gate 			mList = g_mechList;
2727c478bd9Sstevel@tonic-gate 			while (mList != NULL) {
2737c478bd9Sstevel@tonic-gate 				curItem = &(g_mechSet.elements[count]);
2747c478bd9Sstevel@tonic-gate 				curItem->elements = (void*)
2757c478bd9Sstevel@tonic-gate 					malloc(mList->mech_type->length);
2767c478bd9Sstevel@tonic-gate 				if (curItem->elements == NULL) {
2777c478bd9Sstevel@tonic-gate 					/*
2787c478bd9Sstevel@tonic-gate 					 * this is nasty - we must delete the
2797c478bd9Sstevel@tonic-gate 					 * part of the array already copied
2807c478bd9Sstevel@tonic-gate 					 */
2817c478bd9Sstevel@tonic-gate 					for (i = 0; i < count; i++) {
2827c478bd9Sstevel@tonic-gate 						free(g_mechSet.elements[i].
2837c478bd9Sstevel@tonic-gate 							elements);
2847c478bd9Sstevel@tonic-gate 					}
2857c478bd9Sstevel@tonic-gate 					free(g_mechSet.elements);
2867c478bd9Sstevel@tonic-gate 					g_mechSet.count = 0;
2877c478bd9Sstevel@tonic-gate 					g_mechSet.elements = NULL;
2887c478bd9Sstevel@tonic-gate 					(void) mutex_unlock(&g_mechSetLock);
2897c478bd9Sstevel@tonic-gate 					(void) mutex_unlock(&g_mechListLock);
2907c478bd9Sstevel@tonic-gate 					return (GSS_S_FAILURE);
2917c478bd9Sstevel@tonic-gate 				}
2927c478bd9Sstevel@tonic-gate 				g_OID_copy(curItem, mList->mech_type);
2937c478bd9Sstevel@tonic-gate 				count++;
2947c478bd9Sstevel@tonic-gate 				mList = mList->next;
2957c478bd9Sstevel@tonic-gate 			}
2967c478bd9Sstevel@tonic-gate 		}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 		g_mechSetTime = fileInfo.st_mtime;
2997c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&g_mechSetLock);
3007c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&g_mechListLock);
3017c478bd9Sstevel@tonic-gate 	} /* if g_mechSet is out of date or not initialized */
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	/*
3047c478bd9Sstevel@tonic-gate 	 * the mech set is created and it is up to date
3057c478bd9Sstevel@tonic-gate 	 * so just copy it to caller
3067c478bd9Sstevel@tonic-gate 	 */
3077c478bd9Sstevel@tonic-gate 	if ((*mechSet =
3087c478bd9Sstevel@tonic-gate 		(gss_OID_set) malloc(sizeof (gss_OID_set_desc))) == NULL)
3097c478bd9Sstevel@tonic-gate 	{
3107c478bd9Sstevel@tonic-gate 		return (GSS_S_FAILURE);
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	/*
3147c478bd9Sstevel@tonic-gate 	 * need to lock the g_mechSet in case someone tries to update it while
3157c478bd9Sstevel@tonic-gate 	 * I'm copying it.
3167c478bd9Sstevel@tonic-gate 	 */
3177c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&g_mechSetLock);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	/* allocate space for the oid structures */
3207c478bd9Sstevel@tonic-gate 	if (((*mechSet)->elements =
3217c478bd9Sstevel@tonic-gate 		(void*) calloc(g_mechSet.count, sizeof (gss_OID_desc)))
3227c478bd9Sstevel@tonic-gate 		== NULL)
3237c478bd9Sstevel@tonic-gate 	{
3247c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&g_mechSetLock);
3257c478bd9Sstevel@tonic-gate 		free(*mechSet);
3267c478bd9Sstevel@tonic-gate 		*mechSet = NULL;
3277c478bd9Sstevel@tonic-gate 		return (GSS_S_FAILURE);
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	/* now copy the oid structures */
3317c478bd9Sstevel@tonic-gate 	(void) memcpy((*mechSet)->elements, g_mechSet.elements,
3327c478bd9Sstevel@tonic-gate 		g_mechSet.count * sizeof (gss_OID_desc));
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	(*mechSet)->count = g_mechSet.count;
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	/* still need to copy each of the oid elements arrays */
3377c478bd9Sstevel@tonic-gate 	for (i = 0; i < (*mechSet)->count; i++) {
3387c478bd9Sstevel@tonic-gate 		curItem = &((*mechSet)->elements[i]);
3397c478bd9Sstevel@tonic-gate 		curItem->elements =
3407c478bd9Sstevel@tonic-gate 			(void *) malloc(g_mechSet.elements[i].length);
3417c478bd9Sstevel@tonic-gate 		if (curItem->elements == NULL) {
3427c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&g_mechSetLock);
3437c478bd9Sstevel@tonic-gate 			/*
3447c478bd9Sstevel@tonic-gate 			 * must still free the allocated elements for
3457c478bd9Sstevel@tonic-gate 			 * each allocated gss_OID_desc
3467c478bd9Sstevel@tonic-gate 			 */
3477c478bd9Sstevel@tonic-gate 			for (j = 0; j < i; j++) {
3487c478bd9Sstevel@tonic-gate 				free((*mechSet)->elements[j].elements);
3497c478bd9Sstevel@tonic-gate 			}
3507c478bd9Sstevel@tonic-gate 			free((*mechSet)->elements);
3517c478bd9Sstevel@tonic-gate 			free(mechSet);
3527c478bd9Sstevel@tonic-gate 			*mechSet = NULL;
3537c478bd9Sstevel@tonic-gate 			return (GSS_S_FAILURE);
3547c478bd9Sstevel@tonic-gate 		}
3557c478bd9Sstevel@tonic-gate 		g_OID_copy(curItem, &g_mechSet.elements[i]);
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&g_mechSetLock);
3587c478bd9Sstevel@tonic-gate 	return (GSS_S_COMPLETE);
3597c478bd9Sstevel@tonic-gate } /* gss_indicate_mechs */
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate /*
3627c478bd9Sstevel@tonic-gate  * this function has been added for use by modules that need to
3637c478bd9Sstevel@tonic-gate  * know what (if any) optional parameters are supplied in the
3647c478bd9Sstevel@tonic-gate  * config file (MECH_CONF).
3657c478bd9Sstevel@tonic-gate  * It will return the option string for a specified mechanism.
3667c478bd9Sstevel@tonic-gate  * caller is responsible for freeing the memory
3677c478bd9Sstevel@tonic-gate  */
3687c478bd9Sstevel@tonic-gate char *
3697c478bd9Sstevel@tonic-gate __gss_get_modOptions(oid)
3707c478bd9Sstevel@tonic-gate const gss_OID oid;
3717c478bd9Sstevel@tonic-gate {
3727c478bd9Sstevel@tonic-gate 	gss_mech_info aMech;
3737c478bd9Sstevel@tonic-gate 	char *modOptions = NULL;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	/* make sure we have fresh data */
3767c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&g_mechListLock);
3777c478bd9Sstevel@tonic-gate 	updateMechList();
3787c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&g_mechListLock);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	/* searching the list does not require a lock */
3817c478bd9Sstevel@tonic-gate 	if ((aMech = searchMechList(oid)) == NULL ||
3827c478bd9Sstevel@tonic-gate 		aMech->optionStr == NULL) {
3837c478bd9Sstevel@tonic-gate 		return (NULL);
3847c478bd9Sstevel@tonic-gate 	}
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	/*
3877c478bd9Sstevel@tonic-gate 	 * need to obtain a lock on this structure in case someone else
3887c478bd9Sstevel@tonic-gate 	 * will try to update it during the copy
3897c478bd9Sstevel@tonic-gate 	 */
3907c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&g_mechListLock);
3917c478bd9Sstevel@tonic-gate 	if (aMech->optionStr)
3927c478bd9Sstevel@tonic-gate 		modOptions = strdup(aMech->optionStr);
3937c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&g_mechListLock);
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	return (modOptions);
3967c478bd9Sstevel@tonic-gate } /* __gss_get_modOptions */
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate /*
3997c478bd9Sstevel@tonic-gate  * this function has been added for use by gssd.
4007c478bd9Sstevel@tonic-gate  * It will return the kernel module name for a specified mechanism.
4017c478bd9Sstevel@tonic-gate  * caller is responsible for freeing the memory
4027c478bd9Sstevel@tonic-gate  */
4037c478bd9Sstevel@tonic-gate char *
4047c478bd9Sstevel@tonic-gate __gss_get_kmodName(oid)
4057c478bd9Sstevel@tonic-gate const gss_OID oid;
4067c478bd9Sstevel@tonic-gate {
4077c478bd9Sstevel@tonic-gate 	gss_mech_info aMech;
4087c478bd9Sstevel@tonic-gate 	char *kmodName = NULL;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	/* make sure we have fresh data */
4117c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&g_mechListLock);
4127c478bd9Sstevel@tonic-gate 	updateMechList();
4137c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&g_mechListLock);
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	/* searching the list does not require a lock */
4167c478bd9Sstevel@tonic-gate 	if ((aMech = searchMechList(oid)) == NULL || aMech->kmodName == NULL) {
4177c478bd9Sstevel@tonic-gate 		return (NULL);
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	/*
4217c478bd9Sstevel@tonic-gate 	 * need to obtain a lock on this structure in case someone else
4227c478bd9Sstevel@tonic-gate 	 * will try to update it during the copy
4237c478bd9Sstevel@tonic-gate 	 */
4247c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&g_mechListLock);
4257c478bd9Sstevel@tonic-gate 	if (aMech->kmodName)
4267c478bd9Sstevel@tonic-gate 		kmodName = strdup(aMech->kmodName);
4277c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&g_mechListLock);
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	return (kmodName);
4307c478bd9Sstevel@tonic-gate } /* __gss_get_kmodName */
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate /*
4347c478bd9Sstevel@tonic-gate  * given a mechanism string return the mechanism oid
4357c478bd9Sstevel@tonic-gate  */
4367c478bd9Sstevel@tonic-gate OM_uint32
4377c478bd9Sstevel@tonic-gate __gss_mech_to_oid(const char *mechStr, gss_OID* oid)
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate 	gss_mech_info aMech;
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	if (oid == NULL)
4427c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	*oid = GSS_C_NULL_OID;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	if ((mechStr == NULL) || (strlen(mechStr) == 0) ||
4477c478bd9Sstevel@tonic-gate 		(strcasecmp(mechStr, M_DEFAULT) == 0))
4487c478bd9Sstevel@tonic-gate 		return (GSS_S_COMPLETE);
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	/* ensure we have fresh data */
4517c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&g_mechListLock);
4527c478bd9Sstevel@tonic-gate 	updateMechList();
4537c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&g_mechListLock);
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	aMech = g_mechList;
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	/* no lock required - only looking at fields that are not updated */
4587c478bd9Sstevel@tonic-gate 	while (aMech != NULL) {
4597c478bd9Sstevel@tonic-gate 		if ((aMech->mechNameStr) &&
4607c478bd9Sstevel@tonic-gate 			strcmp(aMech->mechNameStr, mechStr) == 0) {
4617c478bd9Sstevel@tonic-gate 			*oid = aMech->mech_type;
4627c478bd9Sstevel@tonic-gate 			return (GSS_S_COMPLETE);
4637c478bd9Sstevel@tonic-gate 		}
4647c478bd9Sstevel@tonic-gate 		aMech = aMech->next;
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 	return (GSS_S_FAILURE);
4677c478bd9Sstevel@tonic-gate } /* __gss_mech_to_oid */
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate /*
4717c478bd9Sstevel@tonic-gate  * Given the mechanism oid, return the readable mechanism name
4727c478bd9Sstevel@tonic-gate  * associated with that oid from the mech config file
4737c478bd9Sstevel@tonic-gate  * (/etc/gss/mech).
4747c478bd9Sstevel@tonic-gate  */
4757c478bd9Sstevel@tonic-gate const char *
4767c478bd9Sstevel@tonic-gate __gss_oid_to_mech(const gss_OID oid)
4777c478bd9Sstevel@tonic-gate {
4787c478bd9Sstevel@tonic-gate 	gss_mech_info aMech;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	if (oid == GSS_C_NULL_OID)
4817c478bd9Sstevel@tonic-gate 		return (M_DEFAULT);
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	/* ensure we have fresh data */
4847c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&g_mechListLock);
4857c478bd9Sstevel@tonic-gate 	updateMechList();
4867c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&g_mechListLock);
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	if ((aMech = searchMechList(oid)) == NULL)
4897c478bd9Sstevel@tonic-gate 		return (NULL);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	return (aMech->mechNameStr);
4927c478bd9Sstevel@tonic-gate } /* __gss_oid_to_mech */
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate /*
4967c478bd9Sstevel@tonic-gate  * return a list of mechanism strings supported
4977c478bd9Sstevel@tonic-gate  * upon return the array is terminated with a NULL entry
4987c478bd9Sstevel@tonic-gate  */
4997c478bd9Sstevel@tonic-gate OM_uint32
5007c478bd9Sstevel@tonic-gate __gss_get_mechanisms(char *mechArray[], int arrayLen)
5017c478bd9Sstevel@tonic-gate {
5027c478bd9Sstevel@tonic-gate 	gss_mech_info aMech;
5037c478bd9Sstevel@tonic-gate 	int i;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	if (mechArray == NULL || arrayLen < 1)
5067c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	/* ensure we have fresh data */
5097c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&g_mechListLock);
5107c478bd9Sstevel@tonic-gate 	updateMechList();
5117c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&g_mechListLock);
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	aMech = g_mechList;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	/* no lock required - only looking at fields that are not updated */
5167c478bd9Sstevel@tonic-gate 	for (i = 1; i < arrayLen; i++) {
5177c478bd9Sstevel@tonic-gate 		if (aMech != NULL) {
5187c478bd9Sstevel@tonic-gate 			*mechArray = aMech->mechNameStr;
5197c478bd9Sstevel@tonic-gate 			mechArray++;
5207c478bd9Sstevel@tonic-gate 			aMech = aMech->next;
5217c478bd9Sstevel@tonic-gate 		} else
5227c478bd9Sstevel@tonic-gate 			break;
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate 	*mechArray = NULL;
5257c478bd9Sstevel@tonic-gate 	return (GSS_S_COMPLETE);
5267c478bd9Sstevel@tonic-gate } /* gss_get_mechanisms */
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate /*
5307c478bd9Sstevel@tonic-gate  * determines if the mechList needs to be updated from file
5317c478bd9Sstevel@tonic-gate  * and performs the update.
5327c478bd9Sstevel@tonic-gate  * this functions must be called with a lock of g_mechListLock
5337c478bd9Sstevel@tonic-gate  */
5347c478bd9Sstevel@tonic-gate static void
5357c478bd9Sstevel@tonic-gate updateMechList(void)
5367c478bd9Sstevel@tonic-gate {
5377c478bd9Sstevel@tonic-gate 	char *fileName;
5387c478bd9Sstevel@tonic-gate 	struct stat fileInfo;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	fileName = MECH_CONF;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	/* check if mechList needs updating */
5437c478bd9Sstevel@tonic-gate 	if (stat(fileName, &fileInfo) == 0 &&
5447c478bd9Sstevel@tonic-gate 		(fileInfo.st_mtime > g_confFileModTime)) {
5457c478bd9Sstevel@tonic-gate 		loadConfigFile(fileName);
5467c478bd9Sstevel@tonic-gate 		g_confFileModTime = fileInfo.st_mtime;
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate } /* updateMechList */
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate /*
5527c478bd9Sstevel@tonic-gate  * given the mechanism type, return the mechanism structure
5537c478bd9Sstevel@tonic-gate  * containing the mechanism library entry points.
5547c478bd9Sstevel@tonic-gate  * will return NULL if mech type is not found
5557c478bd9Sstevel@tonic-gate  * This function will also trigger the loading of the mechanism
5567c478bd9Sstevel@tonic-gate  * module if it has not been already loaded.
5577c478bd9Sstevel@tonic-gate  */
5587c478bd9Sstevel@tonic-gate gss_mechanism
5597c478bd9Sstevel@tonic-gate __gss_get_mechanism(oid)
5607c478bd9Sstevel@tonic-gate const gss_OID oid;
5617c478bd9Sstevel@tonic-gate {
5627c478bd9Sstevel@tonic-gate 	gss_mech_info aMech;
5637c478bd9Sstevel@tonic-gate 	gss_mechanism (*sym)(const gss_OID);
5647c478bd9Sstevel@tonic-gate 	void *dl;
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	/* check if the mechanism is already loaded */
5677c478bd9Sstevel@tonic-gate 	if ((aMech = searchMechList(oid)) != NULL && aMech->mech) {
5687c478bd9Sstevel@tonic-gate 		return (aMech->mech);
5697c478bd9Sstevel@tonic-gate 	}
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	/*
5727c478bd9Sstevel@tonic-gate 	 * might need to re-read the configuration file before loading
5737c478bd9Sstevel@tonic-gate 	 * the mechanism to ensure we have the latest info.
5747c478bd9Sstevel@tonic-gate 	 */
5757c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&g_mechListLock);
5767c478bd9Sstevel@tonic-gate 	updateMechList();
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	aMech = searchMechList(oid);
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	/* is the mechanism present in the list ? */
5817c478bd9Sstevel@tonic-gate 	if (aMech == NULL) {
5827c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&g_mechListLock);
5837c478bd9Sstevel@tonic-gate 		return ((gss_mechanism)NULL);
5847c478bd9Sstevel@tonic-gate 	}
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	/* has another thread loaded the mech */
5877c478bd9Sstevel@tonic-gate 	if (aMech->mech) {
5887c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&g_mechListLock);
5897c478bd9Sstevel@tonic-gate 		return (aMech->mech);
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	/* we found the mechanism, but it is not loaded */
5937c478bd9Sstevel@tonic-gate 	if ((dl = dlopen(aMech->uLibName, RTLD_NOW)) == NULL) {
5947c478bd9Sstevel@tonic-gate 		(void) syslog(LOG_INFO, "libgss dlopen(%s): %s\n",
5957c478bd9Sstevel@tonic-gate 				aMech->uLibName, dlerror());
5967c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&g_mechListLock);
5977c478bd9Sstevel@tonic-gate 		return ((gss_mechanism)NULL);
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if ((sym = (gss_mechanism (*)(const gss_OID))dlsym(dl, MECH_SYM))
6017c478bd9Sstevel@tonic-gate 			== NULL) {
6027c478bd9Sstevel@tonic-gate 		(void) dlclose(dl);
6037c478bd9Sstevel@tonic-gate 		(void) syslog(LOG_INFO, "unable to initialize mechanism"
6047c478bd9Sstevel@tonic-gate 				" library [%s]\n", aMech->uLibName);
6057c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&g_mechListLock);
6067c478bd9Sstevel@tonic-gate 		return ((gss_mechanism)NULL);
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	/* Call the symbol to get the mechanism table */
6107c478bd9Sstevel@tonic-gate 	aMech->mech = (*sym)(aMech->mech_type);
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	if (aMech->mech == NULL) {
6137c478bd9Sstevel@tonic-gate 		(void) dlclose(dl);
6147c478bd9Sstevel@tonic-gate 		(void) syslog(LOG_INFO, "unable to initialize mechanism"
6157c478bd9Sstevel@tonic-gate 				" library [%s]\n", aMech->uLibName);
6167c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&g_mechListLock);
6177c478bd9Sstevel@tonic-gate 		return ((gss_mechanism)NULL);
6187c478bd9Sstevel@tonic-gate 	}
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	aMech->dl_handle = dl;
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&g_mechListLock);
6237c478bd9Sstevel@tonic-gate 	return (aMech->mech);
6247c478bd9Sstevel@tonic-gate } /* __gss_get_mechanism */
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate gss_mechanism_ext
6277c478bd9Sstevel@tonic-gate __gss_get_mechanism_ext(oid)
6287c478bd9Sstevel@tonic-gate const gss_OID oid;
6297c478bd9Sstevel@tonic-gate {
6307c478bd9Sstevel@tonic-gate 	gss_mech_info aMech;
6317c478bd9Sstevel@tonic-gate 	gss_mechanism_ext mech_ext;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	/* check if the mechanism is already loaded */
6347c478bd9Sstevel@tonic-gate 	if ((aMech = searchMechList(oid)) != NULL && aMech->mech_ext != NULL)
6357c478bd9Sstevel@tonic-gate 		return (aMech->mech_ext);
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	if (__gss_get_mechanism(oid) == NULL)
6387c478bd9Sstevel@tonic-gate 		return (NULL);
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	if (aMech->dl_handle == NULL)
6417c478bd9Sstevel@tonic-gate 		return (NULL);
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	/* Load the gss_config_ext struct for this mech */
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	mech_ext = (gss_mechanism_ext)malloc(sizeof (struct gss_config_ext));
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	if (mech_ext == NULL)
6487c478bd9Sstevel@tonic-gate 		return (NULL);
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	/*
6517c478bd9Sstevel@tonic-gate 	 * dlsym() the mech's 'method' functions for the extended APIs
6527c478bd9Sstevel@tonic-gate 	 *
6537c478bd9Sstevel@tonic-gate 	 * NOTE:  Until the void *context argument is removed from the
6547c478bd9Sstevel@tonic-gate 	 * SPI method functions' signatures it will be necessary to have
6557c478bd9Sstevel@tonic-gate 	 * different function pointer typedefs and function names for
6567c478bd9Sstevel@tonic-gate 	 * the SPI methods than for the API.  When this argument is
6577c478bd9Sstevel@tonic-gate 	 * removed it will be possible to rename gss_*_sfct to gss_*_fct
6587c478bd9Sstevel@tonic-gate 	 * and and gssspi_* to gss_*.
6597c478bd9Sstevel@tonic-gate 	 */
6607c478bd9Sstevel@tonic-gate 	mech_ext->gss_acquire_cred_with_password =
6617c478bd9Sstevel@tonic-gate 		(gss_acquire_cred_with_password_sfct)dlsym(aMech->dl_handle,
6627c478bd9Sstevel@tonic-gate 			"gssspi_acquire_cred_with_password");
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	/* Set aMech->mech_ext */
6657c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&g_mechListLock);
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	if (aMech->mech_ext == NULL)
6687c478bd9Sstevel@tonic-gate 		aMech->mech_ext = mech_ext;
6697c478bd9Sstevel@tonic-gate 	else
6707c478bd9Sstevel@tonic-gate 		free(mech_ext);	/* we raced and lost; don't leak */
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&g_mechListLock);
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	return (aMech->mech_ext);
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate } /* __gss_get_mechanism_ext */
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate /*
6807c478bd9Sstevel@tonic-gate  * this routine is used for searching the list of mechanism data.
6817c478bd9Sstevel@tonic-gate  * it needs not be mutex protected because we only add new structures
6827c478bd9Sstevel@tonic-gate  * from the end and they are fully initialized before being added.
6837c478bd9Sstevel@tonic-gate  */
6847c478bd9Sstevel@tonic-gate static gss_mech_info searchMechList(oid)
6857c478bd9Sstevel@tonic-gate const gss_OID oid;
6867c478bd9Sstevel@tonic-gate {
6877c478bd9Sstevel@tonic-gate 	gss_mech_info aMech = g_mechList;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	/* if oid is null -> then get default which is the first in the list */
6907c478bd9Sstevel@tonic-gate 	if (oid == GSS_C_NULL_OID)
6917c478bd9Sstevel@tonic-gate 		return (aMech);
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	while (aMech != NULL) {
6947c478bd9Sstevel@tonic-gate 		if (g_OID_equal(aMech->mech_type, oid))
6957c478bd9Sstevel@tonic-gate 			return (aMech);
6967c478bd9Sstevel@tonic-gate 		aMech = aMech->next;
6977c478bd9Sstevel@tonic-gate 	}
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	/* none found */
7007c478bd9Sstevel@tonic-gate 	return ((gss_mech_info) NULL);
7017c478bd9Sstevel@tonic-gate } /* searchMechList */
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate /*
7057c478bd9Sstevel@tonic-gate  * loads the configuration file
7067c478bd9Sstevel@tonic-gate  * this is called while having a mutex lock on the mechanism list
7077c478bd9Sstevel@tonic-gate  * entries for libraries that have been loaded can't be modified
7087c478bd9Sstevel@tonic-gate  * mechNameStr and mech_type fields are not updated during updates
7097c478bd9Sstevel@tonic-gate  */
7107c478bd9Sstevel@tonic-gate static void loadConfigFile(fileName)
7117c478bd9Sstevel@tonic-gate const char *fileName;
7127c478bd9Sstevel@tonic-gate {
7137c478bd9Sstevel@tonic-gate 	char buffer[BUFSIZ], *oidStr, *oid, *sharedLib, *kernMod, *endp;
7147c478bd9Sstevel@tonic-gate 	char *modOptions;
7157c478bd9Sstevel@tonic-gate 	char sharedPath[sizeof (MECH_LIB_PREFIX) + BUFSIZ];
7167c478bd9Sstevel@tonic-gate 	char *tmpStr;
7177c478bd9Sstevel@tonic-gate 	FILE *confFile;
7187c478bd9Sstevel@tonic-gate 	gss_OID mechOid;
7197c478bd9Sstevel@tonic-gate 	gss_mech_info aMech, tmp;
7207c478bd9Sstevel@tonic-gate 	OM_uint32 minor;
7217c478bd9Sstevel@tonic-gate 	gss_buffer_desc oidBuf;
7227c478bd9Sstevel@tonic-gate 
723*004388ebScasper 	if ((confFile = fopen(fileName, "rF")) == NULL) {
7247c478bd9Sstevel@tonic-gate 		return;
7257c478bd9Sstevel@tonic-gate 	}
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	(void) memset(buffer, 0, sizeof (buffer));
7287c478bd9Sstevel@tonic-gate 	while (fgets(buffer, BUFSIZ, confFile) != NULL) {
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 		/* ignore lines beginning with # */
7317c478bd9Sstevel@tonic-gate 		if (*buffer == '#')
7327c478bd9Sstevel@tonic-gate 			continue;
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 		/*
7357c478bd9Sstevel@tonic-gate 		 * find the first white-space character after
7367c478bd9Sstevel@tonic-gate 		 * the mechanism name
7377c478bd9Sstevel@tonic-gate 		 */
7387c478bd9Sstevel@tonic-gate 		oidStr = buffer;
7397c478bd9Sstevel@tonic-gate 		for (oid = buffer; *oid && !isspace(*oid); oid++);
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 		/* Now find the first non-white-space character */
7427c478bd9Sstevel@tonic-gate 		if (*oid) {
7437c478bd9Sstevel@tonic-gate 			*oid = '\0';
7447c478bd9Sstevel@tonic-gate 			oid++;
7457c478bd9Sstevel@tonic-gate 			while (*oid && isspace(*oid))
7467c478bd9Sstevel@tonic-gate 				oid++;
7477c478bd9Sstevel@tonic-gate 		}
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 		/*
7507c478bd9Sstevel@tonic-gate 		 * If that's all, then this is a corrupt entry. Skip it.
7517c478bd9Sstevel@tonic-gate 		 */
7527c478bd9Sstevel@tonic-gate 		if (! *oid)
7537c478bd9Sstevel@tonic-gate 			continue;
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 		/* Find the end of the oid and make sure it is NULL-ended */
7567c478bd9Sstevel@tonic-gate 		for (endp = oid; *endp && !isspace(*endp); endp++)
7577c478bd9Sstevel@tonic-gate 			;
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 		if (*endp) {
7607c478bd9Sstevel@tonic-gate 			*endp = '\0';
7617c478bd9Sstevel@tonic-gate 		}
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 		/*
7647c478bd9Sstevel@tonic-gate 		 * check if an entry for this oid already exists
7657c478bd9Sstevel@tonic-gate 		 * if it does, and the library is already loaded then
7667c478bd9Sstevel@tonic-gate 		 * we can't modify it, so skip it
7677c478bd9Sstevel@tonic-gate 		 */
7687c478bd9Sstevel@tonic-gate 		oidBuf.value = (void *)oid;
7697c478bd9Sstevel@tonic-gate 		oidBuf.length = strlen(oid);
7707c478bd9Sstevel@tonic-gate 		if (generic_gss_str_to_oid(&minor, &oidBuf, &mechOid)
7717c478bd9Sstevel@tonic-gate 			!= GSS_S_COMPLETE) {
7727c478bd9Sstevel@tonic-gate 			(void) syslog(LOG_INFO, "invalid mechanism oid"
7737c478bd9Sstevel@tonic-gate 					" [%s] in configuration file", oid);
7747c478bd9Sstevel@tonic-gate 			continue;
7757c478bd9Sstevel@tonic-gate 		}
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 		aMech = searchMechList(mechOid);
7787c478bd9Sstevel@tonic-gate 		if (aMech && aMech->mech) {
7797c478bd9Sstevel@tonic-gate 			free(mechOid->elements);
7807c478bd9Sstevel@tonic-gate 			free(mechOid);
7817c478bd9Sstevel@tonic-gate 			continue;
7827c478bd9Sstevel@tonic-gate 		}
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 		/* Find the start of the shared lib name */
7857c478bd9Sstevel@tonic-gate 		for (sharedLib = endp+1; *sharedLib && isspace(*sharedLib);
7867c478bd9Sstevel@tonic-gate 			sharedLib++)
7877c478bd9Sstevel@tonic-gate 			;
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 		/*
7907c478bd9Sstevel@tonic-gate 		 * If that's all, then this is a corrupt entry. Skip it.
7917c478bd9Sstevel@tonic-gate 		 */
7927c478bd9Sstevel@tonic-gate 		if (! *sharedLib) {
7937c478bd9Sstevel@tonic-gate 			free(mechOid->elements);
7947c478bd9Sstevel@tonic-gate 			free(mechOid);
7957c478bd9Sstevel@tonic-gate 			continue;
7967c478bd9Sstevel@tonic-gate 		}
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 		/*
7997c478bd9Sstevel@tonic-gate 		 * Find the end of the shared lib name and make sure it is
8007c478bd9Sstevel@tonic-gate 		 *  NULL-terminated.
8017c478bd9Sstevel@tonic-gate 		 */
8027c478bd9Sstevel@tonic-gate 		for (endp = sharedLib; *endp && !isspace(*endp); endp++)
8037c478bd9Sstevel@tonic-gate 			;
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 		if (*endp) {
8067c478bd9Sstevel@tonic-gate 			*endp = '\0';
8077c478bd9Sstevel@tonic-gate 		}
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 		/* Find the start of the optional kernel module lib name */
8107c478bd9Sstevel@tonic-gate 		for (kernMod = endp+1; *kernMod && isspace(*kernMod);
8117c478bd9Sstevel@tonic-gate 			kernMod++)
8127c478bd9Sstevel@tonic-gate 			;
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 		/*
8157c478bd9Sstevel@tonic-gate 		 * If this item starts with a bracket "[", then
8167c478bd9Sstevel@tonic-gate 		 * it is not a kernel module, but is a list of
8177c478bd9Sstevel@tonic-gate 		 * options for the user module to parse later.
8187c478bd9Sstevel@tonic-gate 		 */
8197c478bd9Sstevel@tonic-gate 		if (*kernMod && *kernMod != '[') {
8207c478bd9Sstevel@tonic-gate 			/*
8217c478bd9Sstevel@tonic-gate 			 * Find the end of the shared lib name and make sure
8227c478bd9Sstevel@tonic-gate 			 * it is NULL-terminated.
8237c478bd9Sstevel@tonic-gate 			 */
8247c478bd9Sstevel@tonic-gate 			for (endp = kernMod; *endp && !isspace(*endp); endp++)
8257c478bd9Sstevel@tonic-gate 				;
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 			if (*endp) {
8287c478bd9Sstevel@tonic-gate 				*endp = '\0';
8297c478bd9Sstevel@tonic-gate 			}
8307c478bd9Sstevel@tonic-gate 		} else
8317c478bd9Sstevel@tonic-gate 			kernMod = NULL;
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 		/* Find the start of the optional module options list */
8347c478bd9Sstevel@tonic-gate 		for (modOptions = endp+1; *modOptions && isspace(*modOptions);
8357c478bd9Sstevel@tonic-gate 			modOptions++);
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 		if (*modOptions == '[')  {
8387c478bd9Sstevel@tonic-gate 			/* move past the opening bracket */
8397c478bd9Sstevel@tonic-gate 			for (modOptions = modOptions+1;
8407c478bd9Sstevel@tonic-gate 			    *modOptions && isspace(*modOptions);
8417c478bd9Sstevel@tonic-gate 			    modOptions++);
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 			/* Find the closing bracket */
8447c478bd9Sstevel@tonic-gate 			for (endp = modOptions;
8457c478bd9Sstevel@tonic-gate 				*endp && *endp != ']'; endp++);
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 			if (endp)
8487c478bd9Sstevel@tonic-gate 				*endp = '\0';
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 		} else {
8517c478bd9Sstevel@tonic-gate 			modOptions = NULL;
8527c478bd9Sstevel@tonic-gate 		}
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 		(void) strcpy(sharedPath, MECH_LIB_PREFIX);
8557c478bd9Sstevel@tonic-gate 		(void) strcat(sharedPath, sharedLib);
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 		/*
8587c478bd9Sstevel@tonic-gate 		 * are we creating a new mechanism entry or
8597c478bd9Sstevel@tonic-gate 		 * just modifying existing (non loaded) mechanism entry
8607c478bd9Sstevel@tonic-gate 		 */
8617c478bd9Sstevel@tonic-gate 		if (aMech) {
8627c478bd9Sstevel@tonic-gate 			/*
8637c478bd9Sstevel@tonic-gate 			 * delete any old values and set new
8647c478bd9Sstevel@tonic-gate 			 * mechNameStr and mech_type are not modified
8657c478bd9Sstevel@tonic-gate 			 */
8667c478bd9Sstevel@tonic-gate 			if (aMech->kmodName) {
8677c478bd9Sstevel@tonic-gate 				free(aMech->kmodName);
8687c478bd9Sstevel@tonic-gate 				aMech->kmodName = NULL;
8697c478bd9Sstevel@tonic-gate 			}
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 			if (aMech->optionStr) {
8727c478bd9Sstevel@tonic-gate 				free(aMech->optionStr);
8737c478bd9Sstevel@tonic-gate 				aMech->optionStr = NULL;
8747c478bd9Sstevel@tonic-gate 			}
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 			if ((tmpStr = strdup(sharedPath)) != NULL) {
8777c478bd9Sstevel@tonic-gate 				if (aMech->uLibName)
8787c478bd9Sstevel@tonic-gate 					free(aMech->uLibName);
8797c478bd9Sstevel@tonic-gate 				aMech->uLibName = tmpStr;
8807c478bd9Sstevel@tonic-gate 			}
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 			if (kernMod) /* this is an optional parameter */
8837c478bd9Sstevel@tonic-gate 				aMech->kmodName = strdup(kernMod);
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 			if (modOptions) /* optional module options */
8867c478bd9Sstevel@tonic-gate 				aMech->optionStr = strdup(modOptions);
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 			/* the oid is already set */
8897c478bd9Sstevel@tonic-gate 			free(mechOid->elements);
8907c478bd9Sstevel@tonic-gate 			free(mechOid);
8917c478bd9Sstevel@tonic-gate 			continue;
8927c478bd9Sstevel@tonic-gate 		}
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 		/* adding a new entry */
8957c478bd9Sstevel@tonic-gate 		aMech = malloc(sizeof (struct gss_mech_config));
8967c478bd9Sstevel@tonic-gate 		if (aMech == NULL) {
8977c478bd9Sstevel@tonic-gate 			free(mechOid->elements);
8987c478bd9Sstevel@tonic-gate 			free(mechOid);
8997c478bd9Sstevel@tonic-gate 			continue;
9007c478bd9Sstevel@tonic-gate 		}
9017c478bd9Sstevel@tonic-gate 		(void) memset(aMech, 0, sizeof (struct gss_mech_config));
9027c478bd9Sstevel@tonic-gate 		aMech->mech_type = mechOid;
9037c478bd9Sstevel@tonic-gate 		aMech->uLibName = strdup(sharedPath);
9047c478bd9Sstevel@tonic-gate 		aMech->mechNameStr = strdup(oidStr);
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 		/* check if any memory allocations failed - bad news */
9077c478bd9Sstevel@tonic-gate 		if (aMech->uLibName == NULL || aMech->mechNameStr == NULL) {
9087c478bd9Sstevel@tonic-gate 			if (aMech->uLibName)
9097c478bd9Sstevel@tonic-gate 				free(aMech->uLibName);
9107c478bd9Sstevel@tonic-gate 			if (aMech->mechNameStr)
9117c478bd9Sstevel@tonic-gate 				free(aMech->mechNameStr);
9127c478bd9Sstevel@tonic-gate 			free(mechOid->elements);
9137c478bd9Sstevel@tonic-gate 			free(mechOid);
9147c478bd9Sstevel@tonic-gate 			free(aMech);
9157c478bd9Sstevel@tonic-gate 			continue;
9167c478bd9Sstevel@tonic-gate 		}
9177c478bd9Sstevel@tonic-gate 		if (kernMod)	/* this is an optional parameter */
9187c478bd9Sstevel@tonic-gate 			aMech->kmodName = strdup(kernMod);
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 		if (modOptions)
9217c478bd9Sstevel@tonic-gate 			aMech->optionStr = strdup(modOptions);
9227c478bd9Sstevel@tonic-gate 		/*
9237c478bd9Sstevel@tonic-gate 		 * add the new entry to the end of the list - make sure
9247c478bd9Sstevel@tonic-gate 		 * that only complete entries are added because other
9257c478bd9Sstevel@tonic-gate 		 * threads might currently be searching the list.
9267c478bd9Sstevel@tonic-gate 		 */
9277c478bd9Sstevel@tonic-gate 		tmp = g_mechListTail;
9287c478bd9Sstevel@tonic-gate 		g_mechListTail = aMech;
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 		if (tmp != NULL)
9317c478bd9Sstevel@tonic-gate 			tmp->next = aMech;
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 		if (g_mechList == NULL)
9347c478bd9Sstevel@tonic-gate 			g_mechList = aMech;
9357c478bd9Sstevel@tonic-gate 	} /* while */
9367c478bd9Sstevel@tonic-gate 	(void) fclose(confFile);
9377c478bd9Sstevel@tonic-gate } /* loadConfigFile */
938