Adding the authentication token stuff.

This commit is contained in:
Juan Carlos Luciani
2005-12-14 17:18:24 +00:00
parent 638e26c3cb
commit 458462e755
29 changed files with 4532 additions and 0 deletions

20
auth_token/linux/Makefile Normal file
View File

@@ -0,0 +1,20 @@
#
# configure environment
#
TARGET = libcasa_auth_token
include global.mak
include defaults.$(PLAT)
include rules.mak
BIN_NAME = $(TARGET)$(xtra).$(BIN)
LIB_NAME = $(TARGET)$(xtra).$(LIB)
#
# target object and source files
#
include objs.$(PLAT)
#
# targets
#
include target.cl

View File

@@ -0,0 +1,546 @@
/***********************************************************************
* File: auth_token.c
* Author: Juan Carlos Luciani (jluciani@novell.com)
*
* Abstract: Implements the CASA Authentication Token credentials API.
*
* Copyright (C) 2005 Novell, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* To contact Novell about this file by physical or electronic mail,
* you may find current contact information at www.novell.com.
***********************************************************************/
//===[ Include files ]=====================================================
#include "internal.h"
//===[ Type definitions ]==================================================
//
// AuthModule definition
//
typedef struct _AuthModule
{
LIST_ENTRY listEntry;
char *pAuthTypeName;
int authTypeNameLen;
void *libHandle;
AuthTokenIf *pAuthTokenIf;
} AuthModule, *PAuthModule;
//===[ Function prototypes ]===============================================
//===[ Global variables ]==================================================
// Debug Level
int DebugLevel = 1;
// AuthModule List and syncronizing mutex
static
LIST_ENTRY g_authModuleListHead = {&g_authModuleListHead, &g_authModuleListHead};
static
pthread_mutex_t g_authModuleMutex = PTHREAD_MUTEX_INITIALIZER;
//++=======================================================================
static
CasaStatus
GetAuthTokenInterface(
IN const char *pAuthTypeName,
INOUT AuthTokenIf **ppAuthTokenIf)
//
// Arguments:
//
// Returns:
//
// Abstract:
//
// Notes:
//
// Environment:
//
// L2
//=======================================================================--
{
CasaStatus retStatus;
ConfigIf *pModuleConfigIf;
DbgTrace(2, "auth_token -GetAuthTokenInterface- Start\n", 0);
// Get the configuration for the module
retStatus = GetConfigInterface("/etc/opt/novell/CASA/authtoken.d/modules.d",
pAuthTypeName,
&pModuleConfigIf);
if (CASA_SUCCESS(retStatus)
&& CasaStatusCode(retStatus) != CASA_STATUS_OBJECT_NOT_FOUND)
{
LIST_ENTRY *pListEntry;
AuthModule *pAuthModule = NULL;
int32_t authTypeNameLen = strlen(pAuthTypeName);
// Gain exclusive access to our mutex
pthread_mutex_lock(&g_authModuleMutex);
// Look if we already have the module in our list
pListEntry = g_authModuleListHead.Flink;
while (pListEntry != &g_authModuleListHead)
{
// Get pointer to the current entry
pAuthModule = CONTAINING_RECORD(pListEntry, AuthModule, listEntry);
// Check if this is the module that we need
if (pAuthModule->authTypeNameLen == authTypeNameLen
&& memcmp(pAuthTypeName, pAuthModule->pAuthTypeName, authTypeNameLen) == 0)
{
// This is the module that we need, stop looking.
break;
}
else
{
// This is not the module that we are looking for
pAuthModule = NULL;
}
// Advance to the next entry
pListEntry = pListEntry->Flink;
}
// Proceed based on whether or not a module was found
if (pAuthModule)
{
// Module found in our list, provide the caller with its AuthTokenIf
// instance after we have incremented its reference count.
pAuthModule->pAuthTokenIf->addReference(pAuthModule->pAuthTokenIf);
*ppAuthTokenIf = pAuthModule->pAuthTokenIf;
// Success
retStatus = CASA_STATUS_SUCCESS;
}
else
{
// Needed module not found in our list, create an entry.
pAuthModule = malloc(sizeof(*pAuthModule));
if (pAuthModule)
{
// Allocate buffer to contain the authentication type name within the module entry
pAuthModule->pAuthTypeName = malloc(authTypeNameLen + 1);
if (pAuthModule->pAuthTypeName)
{
char *pLibraryName;
// Initialize the library handle field
pAuthModule->libHandle = NULL;
// Save the auth type name within the entry
strcpy(pAuthModule->pAuthTypeName, pAuthTypeName);
pAuthModule->authTypeNameLen = authTypeNameLen;
// Obtain the name of the library that we must load
pLibraryName = pModuleConfigIf->getEntryValue(pModuleConfigIf, "LibraryName");
if (pLibraryName)
{
// Load the library
pAuthModule->libHandle = dlopen(pLibraryName, RTLD_LAZY);
if (pAuthModule->libHandle)
{
PFN_GetAuthTokenIfRtn pGetAuthTokenIfRtn;
// Library has been loaded, now get a pointer to its GetAuthTokenInterface routine
pGetAuthTokenIfRtn = dlsym(pAuthModule->libHandle, GET_AUTH_TOKEN_INTERFACE_RTN_SYMBOL);
if (pGetAuthTokenIfRtn)
{
// Now, obtain the modules AuthTokenIf.
retStatus = (pGetAuthTokenIfRtn)(pModuleConfigIf, &pAuthModule->pAuthTokenIf);
}
else
{
DbgTrace(0, "auth_token -GetAuthTokenInterface- dlsym error = %s\n", dlerror());
retStatus = CasaStatusBuild(CASA_SEVERITY_ERROR,
CASA_FACILITY_AUTHTOKEN,
CASA_STATUS_LIBRARY_LOAD_FAILURE);
}
}
else
{
DbgTrace(0, "auth_token -GetAuthTokenInterface- dlopen error = %s\n", dlerror());
retStatus = CasaStatusBuild(CASA_SEVERITY_ERROR,
CASA_FACILITY_AUTHTOKEN,
CASA_STATUS_UNSUCCESSFUL);
}
// Free the buffer holding the library name
free(pLibraryName);
}
else
{
DbgTrace(0, "auth_token -GetAuthTokenInterface- Library name not configured\n", 0);
retStatus = CasaStatusBuild(CASA_SEVERITY_ERROR,
CASA_FACILITY_AUTHTOKEN,
CASA_STATUS_CONFIGURATION_ERROR);
}
// Check if we were successful at obtaining the AuthTokenIf instance for the
// module.
if (CASA_SUCCESS(retStatus))
{
// Insert the entry in the list, provide the caller with its AuthTokenIf
// instance after we have incremented its reference count.
InsertTailList(&g_authModuleListHead, &pAuthModule->listEntry);
pAuthModule->pAuthTokenIf->addReference(pAuthModule->pAuthTokenIf);
*ppAuthTokenIf = pAuthModule->pAuthTokenIf;
}
else
{
// Failed, free resources.
free(pAuthModule->pAuthTypeName);
if (pAuthModule->libHandle)
dlclose(pAuthModule->libHandle);
free(pAuthModule);
}
}
else
{
DbgTrace(0, "auth_token -GetAuthTokenInterface- Unable to allocate buffer\n", 0);
// Free buffer allocated for entry
free(pAuthModule);
retStatus = CasaStatusBuild(CASA_SEVERITY_ERROR,
CASA_FACILITY_AUTHTOKEN,
CASA_STATUS_INSUFFICIENT_RESOURCES);
}
}
else
{
DbgTrace(0, "auth_token -GetAuthTokenInterface- Unable to allocate buffer\n", 0);
retStatus = CasaStatusBuild(CASA_SEVERITY_ERROR,
CASA_FACILITY_AUTHTOKEN,
CASA_STATUS_INSUFFICIENT_RESOURCES);
}
}
// Release exclusive access to our mutex
pthread_mutex_unlock(&g_authModuleMutex);
// Release config interface instance
pModuleConfigIf->releaseReference(pModuleConfigIf);
}
else
{
DbgTrace(0, "auth_token -GetAuthTokenInterface- Unable to obtain config interface\n", 0);
retStatus = CasaStatusBuild(CASA_SEVERITY_ERROR,
CASA_FACILITY_AUTHTOKEN,
CASA_STATUS_CONFIGURATION_ERROR);
}
DbgTrace(2, "auth_token -GetAuthTokenInterface- End, retStatus = %08X\n", retStatus);
return retStatus;
}
//++=======================================================================
CasaStatus SSCS_CALL
GetAuthTokenCredentials(
IN const char *pServiceName,
INOUT const char *pUserNameBuf,
INOUT int *pUserNameBufLen,
INOUT const char *pTokenBuf,
INOUT int *pTokenBufLen)
//
// Arguments:
// pServiceName -
// Pointer to NULL terminated string that contains the
// name of the service to which the client is trying to
// authenticate.
//
// pUserNameBuf -
// Pointer to buffer that will receive a string with the
// username that should used when authenticating to the
// service. The length of this buffer is specified by the
// pUserNameBufLen parameter. Note that the string
// returned will be NULL terminated.
//
// pUserNameBufLen -
// Pointer to integer that contains the length of the
// buffer pointed at by pUserNameBuf. Upon return of the
// function, the integer will contain the actual length
// of the username string (including the NULL terminator)
// if the function successfully completes or the buffer
// length required if the function fails because the buffer
// pointed at by either pUserNameBuf or pTokenBuf is not
// large enough.
//
// pTokenBuf -
// Pointer to buffer that will receive the authentication
// token. The length of this buffer is specified by the
// pTokenBufLen parameter. Note that the the authentication
// token will be in the form of a NULL terminated string.
//
// pTokenBufLen -
// Pointer to integer that contains the length of the
// buffer pointed at by pTokenBuf. Upon return of the
// function, the integer will contain the actual length
// of the authentication token if the function successfully
// completes or the buffer length required if the function
// fails because the buffer pointed at by either pUserNameBuf
// or pTokenBuf is not large enough.
//
// Returns:
// Casa Status
//
// Description:
// Get authentication token credentials to authenticate user to specified
// service.
//
// L2
//=======================================================================--
{
CasaStatus retStatus;
ConfigIf *pServiceConfigIf;
AuthTokenIf *pAuthTokenIf;
DbgTrace(1, "auth_token -GetAuthTokenCredentials- Start\n", 0);
// Validate input parameters
if (pServiceName == NULL
|| pUserNameBufLen == NULL
|| (pUserNameBuf == NULL && *pUserNameBufLen != 0)
|| pTokenBufLen == NULL
|| (pTokenBuf == NULL && *pTokenBufLen != 0))
{
DbgTrace(0, "auth_token -GetAuthTokenCredentials- Invalid input parameter\n", 0);
retStatus = CasaStatusBuild(CASA_SEVERITY_ERROR,
CASA_FACILITY_AUTHTOKEN,
CASA_STATUS_INVALID_PARAMETER);
goto exit;
}
// Check if we have a configuration entry for the service
retStatus = GetConfigInterface("/etc/opt/novell/CASA/authtoken.d/services.d",
pServiceName,
&pServiceConfigIf);
if (CASA_SUCCESS(retStatus)
&& CasaStatusCode(retStatus) != CASA_STATUS_OBJECT_NOT_FOUND)
{
char *pAuthType;
// Obtain the configured authentication type for the service
pAuthType = pServiceConfigIf->getEntryValue(pServiceConfigIf, "AuthType");
if (pAuthType)
{
// Obtain the appropriate token interface for the authentication type
retStatus = GetAuthTokenInterface(pAuthType,
&pAuthTokenIf);
if (CASA_SUCCESS(retStatus))
{
// We found a provider for the service, query it for credentials.
retStatus = pAuthTokenIf->getAuthTokenCredentials(pAuthTokenIf,
pServiceConfigIf,
pUserNameBuf,
pUserNameBufLen,
pTokenBuf,
pTokenBufLen);
// Release token interface
pAuthTokenIf->releaseReference(pAuthTokenIf);
}
else
{
// No authentication token interface available for authentication type
DbgTrace(0, "auth_token -GetAuthTokenCredentials- Failed to obtain authentication token interface\n", 0);
}
// Free the buffer holding the authentication type string
free(pAuthType);
}
else
{
DbgTrace(0, "auth_token -GetAuthTokenCredentials- Authentication type not configured\n", 0);
retStatus = CasaStatusBuild(CASA_SEVERITY_ERROR,
CASA_FACILITY_AUTHTOKEN,
CASA_STATUS_CONFIGURATION_ERROR);
}
// Release service config interface
pServiceConfigIf->releaseReference(pServiceConfigIf);
}
else
{
// We are not providing authentication services for the service
DbgTrace(1, "auth_token -GetAuthTokenCredentials- Service not configured\n", 0);
}
exit:
DbgTrace(1, "auth_token -GetAuthTokenCredentials- End, retStatus = %08X\n", retStatus);
return retStatus;
}
//++=======================================================================
CasaStatus SSCS_CALL
ValidateAuthTokenCredentials(
IN const char *pServiceName,
IN const char *pUserName,
IN const int userNameLen,
IN const char *pTokenBuf,
IN const int tokenBufLen)
//
// Arguments:
// pServiceName -
// Pointer to NULL terminated string that contains the
// name of the service to which the client is trying to
// authenticate.
//
// pUserName -
// Pointer to string with the username that is being
// authenticated to the service. The length of the name
// is specified by the pUserNameLen parameter. Note that
// the string does not need to be NULL terminated.
//
// userNameLen -
// Length of the user name contained within the buffer
// pointed at by pUserNameBuf (Does not include the NULL
// terminator). If this parameter is set to -1 then the
// function assumes that the username string is NULL
// terminated.
//
// pTokenBuf -
// Pointer to buffer that will receive the authentication
// token. The length of this buffer is specified by the
// pTokenBufLen parameter. Note that the the authentication
// token will be in the form of a NULL terminated string.
//
// tokenBufLen -
// Length of the data contained within the buffer pointed
// at by pTokenBuf.
//
// Returns:
// Casa status.
//
// Description:
// Validates authentication token credentials.
//
// L2
//=======================================================================--
{
CasaStatus retStatus;
ConfigIf *pServiceConfigIf;
AuthTokenIf *pAuthTokenIf;
DbgTrace(1, "auth_token -ValidateAuthTokenCredentials- Start\n", 0);
// Validate input parameters
if (pServiceName == NULL
|| pUserName == NULL
|| userNameLen == 0
|| pTokenBuf == NULL
|| tokenBufLen == 0)
{
DbgTrace(0, "auth_token -ValidateAuthTokenCredentials- Invalid input parameter\n", 0);
retStatus = CasaStatusBuild(CASA_SEVERITY_ERROR,
CASA_FACILITY_AUTHTOKEN,
CASA_STATUS_INVALID_PARAMETER);
goto exit;
}
// Check if we have a configuration entry for the service
retStatus = GetConfigInterface("/etc/opt/novell/CASA/authtoken.d/services.d",
pServiceName,
&pServiceConfigIf);
if (CASA_SUCCESS(retStatus))
{
// Check if the configuration entry was not found
if (CasaStatusCode(retStatus) != CASA_STATUS_OBJECT_NOT_FOUND)
{
char *pAuthType;
// Obtain the configured authentication type for the service
pAuthType = pServiceConfigIf->getEntryValue(pServiceConfigIf, "AuthType");
if (pAuthType)
{
// Obtain the appropriate token interface for the authentication type
retStatus = GetAuthTokenInterface(pAuthType,
&pAuthTokenIf);
if (CASA_SUCCESS(retStatus))
{
// We found a provider for the service, validate the credentials.
retStatus = pAuthTokenIf->validateAuthTokenCredentials(pAuthTokenIf,
pServiceConfigIf,
pUserName,
userNameLen,
pTokenBuf,
tokenBufLen);
// Release token interface
pAuthTokenIf->releaseReference(pAuthTokenIf);
}
else
{
// No authentication token interface available for authentication type
DbgTrace(0, "auth_token -ValidateAuthTokenCredentials- Failed to obtain authentication token interface\n", 0);
}
// Free the buffer holding the authentication type string
free(pAuthType);
}
else
{
DbgTrace(0, "auth_token -ValidateAuthTokenCredentials- Authentication type not configured\n", 0);
retStatus = CasaStatusBuild(CASA_SEVERITY_ERROR,
CASA_FACILITY_AUTHTOKEN,
CASA_STATUS_CONFIGURATION_ERROR);
}
// Release service config interface
pServiceConfigIf->releaseReference(pServiceConfigIf);
}
else
{
// We need to return an error
DbgTrace(0, "auth_token -ValidateAuthTokenCredentials- Service not configured\n", 0);
retStatus = CasaStatusBuild(CASA_SEVERITY_ERROR,
CASA_FACILITY_AUTHTOKEN,
CASA_STATUS_CONFIGURATION_ERROR);
}
}
else
{
DbgTrace(0, "auth_token -ValidateAuthTokenCredentials- Error obtaining service configuration\n", 0);
}
exit:
DbgTrace(1, "auth_token -ValidateAuthTokenCredentials- End, retStatus = %08X\n", retStatus);
return retStatus;
}
//++=======================================================================
//++=======================================================================
//++=======================================================================

700
auth_token/linux/config.c Normal file
View File

@@ -0,0 +1,700 @@
/***********************************************************************
* File: config.c
* Author: Juan Carlos Luciani (jluciani@novell.com)
*
* Abstract: Implements the Configuration Interface.
*
* Copyright (C) 2005 Novell, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* To contact Novell about this file by physical or electronic mail,
* you may find current contact information at www.novell.com.
***********************************************************************/
//===[ Include files ]=====================================================
#include "internal.h"
//===[ Type definitions ]==================================================
//
// Config Key object
//
typedef struct _ConfigKey
{
LIST_ENTRY listEntry;
char *pKeyName;
int keyNameLen;
char *pValue;
int valueLen;
} ConfigKey, *pConfigKey;
//
// Config Interface instance data
//
typedef struct _ConfigIfInstance
{
LIST_ENTRY listEntry;
int refCount;
char *pConfigFolder;
int configFolderLen;
char *pConfigName;
int configNameLen;
LIST_ENTRY configKeyListHead;
ConfigIf configIf;
} ConfigIfInstance, *PConfigIfInstance;
//===[ Function prototypes ]===============================================
//===[ Global variables ]==================================================
// ConfigIf synchronization mutex and variables
static
pthread_mutex_t g_configIfMutex = PTHREAD_MUTEX_INITIALIZER;
static
LIST_ENTRY g_configIfListHead = {&g_configIfListHead, &g_configIfListHead};
static
int g_numConfigIfObjs = 0;
//++=======================================================================
static void
RemoveWhiteSpaceFromTheEnd(
IN const char *pInString)
//
// Arguments:
//
// Returns:
//
// Abstract:
//
// Notes:
//
// L2
//=======================================================================--
{
char *pLineEnd = (char*) pInString + strlen(pInString) - 1;
DbgTrace(3, "auth_token -RemoveWhiteSpaceFromTheEnd- Start\n", 0);
while (pLineEnd != pInString)
{
if (*pLineEnd == '\n'
|| *pLineEnd == ' '
|| *pLineEnd == '\t')
{
// Strike this character
*pLineEnd = '\0';
pLineEnd --;
}
else
{
// Found a non-white character
break;
}
}
DbgTrace(3, "auth_token -RemoveWhiteSpaceFromTheEnd- End\n", 0);
}
//++=======================================================================
static char*
SkipWhiteSpace(
IN const char *pInString)
//
// Arguments:
//
// Returns:
//
// Abstract:
//
// Notes:
//
// L2
//=======================================================================--
{
char *pOutString = (char*) pInString;
DbgTrace(3, "auth_token -SkipWhiteSpace- Start\n", 0);
while (*pOutString != '\0')
{
if (*pOutString == '\n'
|| *pOutString == ' '
|| *pOutString == '\t')
{
// Skip this character
pOutString ++;
}
else
{
// Found a non-white character
break;
}
}
DbgTrace(3, "auth_token -SkipWhiteSpace- End\n", 0);
return pOutString;
}
//++=======================================================================
static char*
SkipNonWhiteSpace(
IN const char *pInString)
//
// Arguments:
//
// Returns:
//
// Abstract:
//
// Notes:
//
// L2
//=======================================================================--
{
char *pOutString;
DbgTrace(3, "auth_token -SkipNonWhiteSpace- Start\n", 0);
while (*pOutString != '\0')
{
if (*pOutString == '\n'
|| *pOutString == ' '
|| *pOutString == '\t')
{
// Found a white character
break;
}
else
{
// Skip this character
pOutString ++;
}
}
DbgTrace(3, "auth_token -SkipNonWhiteSpace- End\n", 0);
return pOutString;
}
//++=======================================================================
static void
LowerCaseString(
IN char *pDestString,
IN const char *pSrcString)
//
// Arguments:
//
// Returns:
//
// Abstract:
//
// Notes:
//
// L2
//=======================================================================--
{
int i;
DbgTrace(3, "auth_token -LowerCaseString- Start\n", 0);
// Copy the string as lower case
for (i = 0; pSrcString[i] != '\0'; i++)
{
if (isalpha(pSrcString[i]))
pDestString[i] = tolower(pSrcString[i]);
else
pDestString[i] = pSrcString[i];
}
// Null terminate the destination string
pDestString[i] = '\0';
DbgTrace(3, "auth_token -LowerCaseString- End\n", 0);
}
//++=======================================================================
int SSCS_CALL
ConfigIf_AddReference(
IN const void *pIfInstance)
//
// Arguments:
// pIfInstance -
// Pointer to interface object.
//
// Returns:
// Interface reference count.
//
// Description:
// Increases interface reference count.
//
// L2
//=======================================================================--
{
int refCount;
ConfigIfInstance *pConfigIfInstance = CONTAINING_RECORD(pIfInstance, ConfigIfInstance, configIf);
DbgTrace(2, "auth_token -ConfigIf_AddReference- Start\n", 0);
// Increment the reference count on the object
pthread_mutex_lock(&g_configIfMutex);
pConfigIfInstance->refCount ++;
refCount = pConfigIfInstance->refCount;
pthread_mutex_unlock(&g_configIfMutex);
DbgTrace(2, "auth_token -ConfigIf_AddReference- End, refCount = %08X\n", refCount);
return refCount;
}
//++=======================================================================
void SSCS_CALL
ConfigIf_ReleaseReference(
IN const void *pIfInstance)
//
// Arguments:
// pIfInstance -
// Pointer to interface object.
//
// Returns:
// Nothing.
//
// Description:
// Decreases interface reference count. The interface is deallocated if
// the reference count becomes zero.
//
// L2
//=======================================================================--
{
bool freeObj = false;
ConfigIfInstance *pConfigIfInstance = CONTAINING_RECORD(pIfInstance, ConfigIfInstance, configIf);
DbgTrace(2, "auth_token -ConfigIf_ReleaseReference- Start\n", 0);
// Decrement the reference count on the object and determine if it needs to
// be released.
pthread_mutex_lock(&g_configIfMutex);
pConfigIfInstance->refCount --;
if (pConfigIfInstance->refCount == 0)
{
// The object needs to be released, forget about it.
freeObj = true;
g_numConfigIfObjs --;
RemoveEntryList(&pConfigIfInstance->listEntry);
}
pthread_mutex_unlock(&g_configIfMutex);
// Free object if necessary
if (freeObj)
{
// Free all of the config key objects associated with this configuration
// interface instance.
while (!IsListEmpty(&pConfigIfInstance->configKeyListHead))
{
LIST_ENTRY *pListEntry;
ConfigKey *pConfigKey;
// Get reference to entry at the head of the list
pListEntry = pConfigIfInstance->configKeyListHead.Flink;
pConfigKey = CONTAINING_RECORD(pListEntry, ConfigKey, listEntry);
// Free the buffers associated with the ConfigKey
free(pConfigKey->pKeyName);
free(pConfigKey->pValue);
// Remove the entry from the list
RemoveEntryList(&pConfigKey->listEntry);
// Finish freeing the ConfigKey
free(pConfigKey);
}
// Free the rest of the buffers associated with the interface instance data
free(pConfigIfInstance->pConfigFolder);
free(pConfigIfInstance->pConfigName);
free(pConfigIfInstance);
}
DbgTrace(2, "auth_token -ConfigIf_ReleaseReference- End\n", 0);
}
//++=======================================================================
char* SSCS_CALL
ConfigIf__GetEntryValue(
IN const void *pIfInstance,
IN const char *pKeyName)
//
// Arguments:
// pIfInstance -
// Pointer to interface object.
//
// pKeyName -
// Pointer to NULL terminated string that contains the
// name of the key whose value is being requested.
//
// Returns:
// Pointer to NULL terminated string with value being requested or NULL.
//
// Description:
// Gets value associated with a key for the configuration object.
//
// L2
//=======================================================================--
{
ConfigIfInstance *pConfigIfInstance = CONTAINING_RECORD(pIfInstance, ConfigIfInstance, configIf);
char *pValue = NULL;
LIST_ENTRY *pListEntry;
ConfigKey *pConfigKey;
int keyNameLen = strlen(pKeyName);
char *pKeyNameLowercase;
DbgTrace(2, "auth_token -ConfigIf_GetEntryValue- Start\n", 0);
// Allocate enough space to hold lower case version of the key name
pKeyNameLowercase = malloc(keyNameLen);
if (pKeyNameLowercase)
{
// Lower case the key name
LowerCaseString(pKeyNameLowercase, pKeyName);
// Try to find matching ConfigKey
pListEntry = pConfigIfInstance->configKeyListHead.Flink;
while (pListEntry != &pConfigIfInstance->configKeyListHead)
{
// Get pointer to the current entry
pConfigKey = CONTAINING_RECORD(pListEntry, ConfigKey, listEntry);
// Check if we have a match
if (pConfigKey->keyNameLen == keyNameLen
&& memcmp(pKeyNameLowercase, pConfigKey->pKeyName, keyNameLen) == 0)
{
// We found it, return its value.
pValue = malloc(pConfigKey->valueLen + 1);
if (pValue)
{
strcpy(pValue, pConfigKey->pValue);
}
else
{
DbgTrace(0, "auth_token -ConfigIf_GetEntryValue- Buffer allocation failure\n", 0);
}
break;
}
// Advance to the next entry
pListEntry = pListEntry->Flink;
}
// Free the lower case version of the key name
free(pKeyNameLowercase);
}
else
{
DbgTrace(0, "auth_token -ConfigIf_GetEntryValue- Buffer allocation failure\n", 0);
}
DbgTrace(2, "auth_token -ConfigIf_GetEntryValue- End, pValue = %08X\n", pValue);
return pValue;
}
//++=======================================================================
CasaStatus SSCS_CALL
GetConfigInterface(
IN const char *pConfigFolder,
IN const char *pConfigName,
INOUT ConfigIf **ppConfigIf)
//
// Arguments:
// pConfigFolder -
// Pointer to NULL terminated string that contains the name of
// the folder containing the configuration file.
//
// pConfigName -
// Pointer to NULL terminated string containing the name of the
// configuration entry.
//
// ppConfigIf -
// Pointer to variable that will receive pointer to ConfigIf
// instance.
//
// Returns:
// Casa Status
//
// Description:
// Get configuration interface to specified configuration entry.
//
// L2
//=======================================================================--
{
int configFolderLen = strlen(pConfigFolder);
int configNameLen = strlen(pConfigName);
ConfigIfInstance *pConfigIfInstance;
LIST_ENTRY *pListEntry;
CasaStatus retStatus = CasaStatusBuild(CASA_SEVERITY_INFORMATIONAL,
CASA_FACILITY_AUTHTOKEN,
CASA_STATUS_OBJECT_NOT_FOUND);
DbgTrace(2, "auth_token -GetConfigInterface- Start\n", 0);
// Obtain exclusive access to our mutex
pthread_mutex_lock(&g_configIfMutex);
// Check if we already have an entry in our list for the configuration
pListEntry = g_configIfListHead.Flink;
while (pListEntry != &g_configIfListHead)
{
// Get pointer to the current entry
pConfigIfInstance = CONTAINING_RECORD(pListEntry, ConfigIfInstance, listEntry);
// Check if we have a match
if (pConfigIfInstance->configFolderLen == configFolderLen
&& pConfigIfInstance->configNameLen == configNameLen
&& memcmp(pConfigFolder, pConfigIfInstance->pConfigFolder, configFolderLen) == 0
&& memcmp(pConfigName, pConfigIfInstance->pConfigName, configNameLen) == 0)
{
// We found it, return the ConfigIf associated with the instance data
// after incrementing its reference count.
pConfigIfInstance->refCount ++;
*ppConfigIf = &pConfigIfInstance->configIf;
// Success
retStatus = CASA_STATUS_SUCCESS;
break;
}
// Advance to the next entry
pListEntry = pListEntry->Flink;
}
// Proceed to create interface instance data for the configuration if none was found
if (retStatus != CASA_STATUS_SUCCESS)
{
char *pFilePath;
// Build a string containing the configuration file path
pFilePath = malloc(configFolderLen + 1 + configNameLen + sizeof(".conf"));
if (pFilePath)
{
FILE *pConfigFile;
strcpy(pFilePath, pConfigFolder);
strcat(pFilePath, "/");
strcat(pFilePath, pConfigName);
strcat(pFilePath, ".conf");
// Open the configuration file for reading
pConfigFile = fopen(pFilePath, "r");
if (pConfigFile)
{
// Opened the file, create a ConfigIfInstance object for it.
pConfigIfInstance = malloc(sizeof(*pConfigIfInstance));
if (pConfigIfInstance)
{
// Initialize the list head within the instance data
InitializeListHead(&pConfigIfInstance->configKeyListHead);
// Initialize the ConfigIf within the instance data
pConfigIfInstance->configIf.addReference = ConfigIf_AddReference;
pConfigIfInstance->configIf.releaseReference = ConfigIf_ReleaseReference;
pConfigIfInstance->configIf.getEntryValue = ConfigIf__GetEntryValue;
// Save the ConfigFolder and ConfigName information within the instance data
pConfigIfInstance->pConfigFolder = malloc(configFolderLen + 1);
if (pConfigIfInstance->pConfigFolder)
{
strcpy(pConfigIfInstance->pConfigFolder, pConfigFolder);
pConfigIfInstance->configFolderLen = configFolderLen;
pConfigIfInstance->pConfigName = malloc(configNameLen + 1);
if (pConfigIfInstance->pConfigName)
{
strcpy(pConfigIfInstance->pConfigName, pConfigName);
pConfigIfInstance->configNameLen = configNameLen;
// Add the instance data into our list and bump up its reference count
// since we did that.
InsertTailList(&g_configIfListHead, &pConfigIfInstance->listEntry);
pConfigIfInstance->refCount = 1;
// At this point we want to return success to the caller even if we
// experience a read error.
retStatus = CASA_STATUS_SUCCESS;
// Return the ConfigIf associated with the instance data after
// incrementing its reference count.
pConfigIfInstance->refCount ++;
*ppConfigIf = &pConfigIfInstance->configIf;
// Now update the instance data with the information present in the file
if (fseek(pConfigFile, 0, SEEK_SET) == 0)
{
char line[512];
while (fgets(line, sizeof(line), pConfigFile) != NULL)
{
int lineLength;
RemoveWhiteSpaceFromTheEnd(line);
lineLength = strlen(line);
if (lineLength != 0)
{
char *pKey;
char *pKeyEnd;
char *pValue;
int keyLen, valueLen;
ConfigKey *pConfigKey;
// Attempt to find the key
pKey = SkipWhiteSpace(line);
// Make sure that we are not dealing with an empty line or a comment
if (*pKey == '\0' || *pKey == '#')
continue;
// Go past the key
pKeyEnd = SkipNonWhiteSpace(pKey);
// Protect against a malformed line
if (*pKeyEnd == '\0')
{
DbgTrace(0, "auth_token -GetConfigInterface- Key found without value\n", 0);
continue;
}
// Attempt to find the value
pValue = SkipWhiteSpace(pKeyEnd);
// Protect against a malformed line
if (*pValue == '\0')
{
DbgTrace(0, "auth_token -GetConfigInterface- Key found without value\n", 0);
continue;
}
// Delineate the key
*pKeyEnd = '\0';
// Create a ConfigKey object for this key/value pair
pConfigKey = malloc(sizeof(*pConfigKey));
if (pConfigKey)
{
pConfigKey->keyNameLen = strlen(pKey);
pConfigKey->pKeyName = malloc(pConfigKey->keyNameLen + 1);
if (pConfigKey->pKeyName)
{
// Save the key name in lower case
LowerCaseString(pConfigKey->pKeyName, pKey);
pConfigKey->valueLen = strlen(pValue);
pConfigKey->pValue = malloc(pConfigKey->valueLen + 1);
if (pConfigKey->pValue)
{
strcpy(pConfigKey->pValue, pValue);
// The entry is ready, now associate it with the instance data.
InsertTailList(&pConfigIfInstance->configKeyListHead, &pConfigKey->listEntry);
}
else
{
DbgTrace(0, "auth_token -GetConfigInterface- Buffer allocation failure\n", 0);
free(pConfigKey->pKeyName);
free(pConfigKey);
}
}
else
{
DbgTrace(0, "auth_token -GetConfigInterface- Buffer allocation failure\n", 0);
free(pConfigKey);
}
}
else
{
DbgTrace(0, "auth_token -GetConfigInterface- Buffer allocation failure\n", 0);
}
}
}
}
else
{
DbgTrace(0, "auth_token -GetConfigInterface- File seek error, errno = %d\n", errno);
}
}
else
{
DbgTrace(0, "auth_token -GetConfigInterface- Buffer allocation failure\n", 0);
// Free the buffers associated with the instance data
free(pConfigIfInstance->pConfigFolder);
free(pConfigIfInstance);
}
}
else
{
DbgTrace(0, "auth_token -GetConfigInterface- Buffer allocation failure\n", 0);
// Free the buffer allocated for the instance data
free(pConfigIfInstance);
}
}
else
{
DbgTrace(0, "auth_token -GetConfigInterface- Buffer allocation failure\n", 0);
}
// Close the file
fclose(pConfigFile);
}
else
{
DbgTrace(1, "auth_token -GetConfigInterface- Unable to open config file, errno = %d\n", errno);
}
}
else
{
DbgTrace(0, "auth_token -GetConfigInterface- Buffer allocation error\n", 0);
}
}
// Release exclusive access to our mutex
pthread_mutex_unlock(&g_configIfMutex);
DbgTrace(2, "auth_token -GetConfigInterface- End, retStatus = %08X\n", retStatus);
return retStatus;
}
//++=======================================================================
//++=======================================================================
//++=======================================================================

View File

@@ -0,0 +1,81 @@
/***********************************************************************
* File: internal.h
* Author: Juan Carlos Luciani (jluciani@novell.com)
*
* Abstract: Defines or includes the definitions necessary for the
* module.
*
* Copyright (C) 2005 Novell, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* To contact Novell about this file by physical or electronic mail,
* you may find current contact information at www.novell.com.
***********************************************************************/
//===[ Include files ]=====================================================
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <pthread.h>
#include <dlfcn.h>
#include <micasa_types.h>
#include <casa_status.h>
#include <list_entry.h>
#include <casa_auth_token.h>
#include <auth_token_int.h>
//===[ Type definitions ]==================================================
#define CONTAINING_RECORD(address, type, field) ((type *)( \
(char*)(address) - \
(char*)(&((type *)0)->field)))
//
// DbgTrace macro define
//
#define DbgTrace(LEVEL, X, Y) { \
if (LEVEL == 0) \
printf(X, Y); \
else if (DebugLevel >= LEVEL) \
printf(X, Y); \
}
//===[ Inlines functions ]===============================================
//===[ Function prototypes ]===============================================
//===[ Global variables ]==================================================
//===[ Global externals ]==================================================
extern int DebugLevel;
//===[ External prototypes ]===============================================
extern
CasaStatus CSAPI
GetConfigInterface(
IN const char *pFolder,
IN const char *pConfigName,
INOUT ConfigIf **ppConfigIf);
//=========================================================================

View File

@@ -0,0 +1,8 @@
VER_1.0
{
global:
GetAuthTokenCredentials;
ValidateAuthTokenCredentials;
local:
*;
};

10
auth_token/linux/link.lux Normal file
View File

@@ -0,0 +1,10 @@
LINK = $(CC) \
-Wl,-Bsymbolic \
-shared \
-Wl,--version-script=$(TARGET)_$(PLAT).exp \
-Wl,-rpath -Wl,/usr/$(ARCH_LlB) \
-L/usr/$(ARCH_LIB) -lpthread -lc -ldl \
-Wl,-soname -Wl,$(TARGET).so \
-o $(LIBDIR)$(XTRA)/$(TARGET).so \
-L$(LIBDIR)$(XTRA) \
$(OBJDIR)*.$(O)

View File

@@ -0,0 +1,4 @@
OBJS=\
auth_token.$(O) \
config.$(O)