2006-04-03 22:20:37 +02:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Novell, Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* 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 Lesser 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, Novell, Inc.
|
|
|
|
*
|
|
|
|
* To contact Novell about this file by physical or electronic mail,
|
|
|
|
* you may find current contact information at www.novell.com.
|
|
|
|
*
|
|
|
|
* Author: Juan Carlos Luciani <jluciani@novell.com>
|
|
|
|
*
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
//===[ Include files ]=====================================================
|
|
|
|
|
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
//===[ Type definitions ]==================================================
|
|
|
|
|
|
|
|
#define DEFAULT_RETRY_LIFETIME 5 // seconds
|
|
|
|
|
|
|
|
//===[ Function prototypes ]===============================================
|
|
|
|
|
|
|
|
//===[ Global variables ]==================================================
|
|
|
|
|
|
|
|
//
|
|
|
|
// Debug tracing level
|
|
|
|
//
|
2006-06-21 01:01:51 +02:00
|
|
|
int DebugLevel = 0;
|
2006-04-03 22:20:37 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Operating parameter
|
|
|
|
//
|
2006-06-21 01:01:51 +02:00
|
|
|
bool secureRpcSetting = false;
|
2006-06-20 22:33:11 +02:00
|
|
|
bool g_bInitialized = FALSE;
|
2006-04-03 22:20:37 +02:00
|
|
|
|
|
|
|
//++=======================================================================
|
|
|
|
static
|
|
|
|
CasaStatus
|
|
|
|
ObtainSessionToken(
|
|
|
|
IN RpcSession *pRpcSession,
|
|
|
|
IN AuthPolicy *pAuthPolicy,
|
|
|
|
INOUT char **ppSessionToken)
|
|
|
|
//
|
|
|
|
// Arguments:
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
//
|
|
|
|
// Abstract:
|
|
|
|
//
|
|
|
|
// Notes:
|
|
|
|
//
|
2006-04-28 20:58:25 +02:00
|
|
|
// L2
|
2006-04-03 22:20:37 +02:00
|
|
|
//=======================================================================--
|
|
|
|
{
|
2006-04-28 20:58:25 +02:00
|
|
|
CasaStatus retStatus = CasaStatusBuild(CASA_SEVERITY_ERROR,
|
|
|
|
CASA_FACILITY_AUTHTOKEN,
|
|
|
|
CASA_STATUS_UNSUCCESSFUL);
|
|
|
|
LIST_ENTRY *pListEntry;
|
|
|
|
AuthCacheEntry *pCacheEntry = NULL;
|
2006-04-03 22:20:37 +02:00
|
|
|
|
|
|
|
DbgTrace(1, "-ObtainSessionToken- Start\n", 0);
|
|
|
|
|
|
|
|
// Initialize output parameter
|
|
|
|
*ppSessionToken = NULL;
|
|
|
|
|
|
|
|
// Look in our cache for an entry that matches one of the auth
|
|
|
|
// contexts specified in the AuthPolicy object.
|
|
|
|
pListEntry = pAuthPolicy->authContextListHead.Flink;
|
|
|
|
while (pListEntry != &pAuthPolicy->authContextListHead)
|
|
|
|
{
|
|
|
|
AuthContext *pAuthContext;
|
|
|
|
|
|
|
|
// Get pointer to AuthContext structure
|
|
|
|
pAuthContext = CONTAINING_RECORD(pListEntry, AuthContext, listEntry);
|
|
|
|
|
|
|
|
// Try to find a cache entry for the auth context
|
2006-06-02 18:58:27 +02:00
|
|
|
pCacheEntry = FindSessionTokenEntryInCache(pAuthContext->pContext);
|
2006-04-03 22:20:37 +02:00
|
|
|
if (pCacheEntry != NULL)
|
|
|
|
{
|
2006-04-28 20:58:25 +02:00
|
|
|
// Cache entry found, check if it is of use to us.
|
|
|
|
if (CASA_SUCCESS(pCacheEntry->status))
|
|
|
|
{
|
|
|
|
// This entry can be used, stop looking.
|
|
|
|
retStatus = pCacheEntry->status;
|
|
|
|
break;
|
|
|
|
}
|
2006-05-19 22:10:29 +02:00
|
|
|
else
|
|
|
|
{
|
2006-06-20 22:33:11 +02:00
|
|
|
// Free the entry
|
|
|
|
FreeAuthCacheEntry(pCacheEntry);
|
2006-05-19 22:10:29 +02:00
|
|
|
}
|
2006-04-03 22:20:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Advance to the next entry
|
|
|
|
pListEntry = pListEntry->Flink;
|
|
|
|
}
|
|
|
|
|
2006-04-28 20:58:25 +02:00
|
|
|
// If we did not find a cache entry that we can use, then try to create one.
|
2006-04-03 22:20:37 +02:00
|
|
|
pListEntry = pAuthPolicy->authContextListHead.Flink;
|
|
|
|
while (!CASA_SUCCESS(retStatus)
|
|
|
|
&& pListEntry != &pAuthPolicy->authContextListHead)
|
|
|
|
{
|
|
|
|
AuthContext *pAuthContext;
|
|
|
|
char *pAuthMechToken;
|
|
|
|
|
|
|
|
// Get pointer to AuthContext structure
|
|
|
|
pAuthContext = CONTAINING_RECORD(pListEntry, AuthContext, listEntry);
|
|
|
|
|
2006-04-28 20:58:25 +02:00
|
|
|
// Only try to create cache entry for the auth context if there is not
|
|
|
|
// one already.
|
2006-06-02 18:58:27 +02:00
|
|
|
pCacheEntry = FindSessionTokenEntryInCache(pAuthContext->pContext);
|
2006-04-28 20:58:25 +02:00
|
|
|
if (pCacheEntry == NULL)
|
2006-04-03 22:20:37 +02:00
|
|
|
{
|
2006-06-20 22:33:11 +02:00
|
|
|
char *pReqMsg = NULL;
|
|
|
|
char *pRespMsg = NULL;
|
|
|
|
int respLen;
|
|
|
|
|
2006-04-28 20:58:25 +02:00
|
|
|
// Get authentication mechanism token
|
|
|
|
retStatus = GetAuthMechToken(pAuthContext, &pAuthMechToken);
|
|
|
|
if (!CASA_SUCCESS(retStatus))
|
|
|
|
{
|
|
|
|
// We were not able to obtain an authentication mechanism token
|
|
|
|
// for the context.
|
|
|
|
//
|
|
|
|
// Advance to the next entry
|
|
|
|
pListEntry = pListEntry->Flink;
|
|
|
|
continue;
|
|
|
|
}
|
2006-04-03 22:20:37 +02:00
|
|
|
|
2006-06-20 22:33:11 +02:00
|
|
|
// Authenticate to the ATS
|
|
|
|
pReqMsg = BuildAuthenticateMsg(pAuthContext, pAuthMechToken);
|
|
|
|
if (pReqMsg)
|
|
|
|
{
|
|
|
|
// Issue rpc
|
|
|
|
retStatus = Rpc(pRpcSession,
|
|
|
|
"Authenticate",
|
|
|
|
secureRpcSetting,
|
|
|
|
pReqMsg,
|
|
|
|
&pRespMsg,
|
|
|
|
&respLen);
|
|
|
|
if (CASA_SUCCESS(retStatus))
|
|
|
|
{
|
|
|
|
AuthenticateResp *pAuthenticateResp;
|
|
|
|
|
|
|
|
// Create Authenticate response object
|
|
|
|
retStatus = CreateAuthenticateResp(pRespMsg, respLen, &pAuthenticateResp);
|
|
|
|
if (CASA_SUCCESS(retStatus))
|
|
|
|
{
|
|
|
|
// Return the auth token to the caller
|
|
|
|
pCacheEntry = CreateSessionTokenCacheEntry(
|
|
|
|
pAuthContext->pContext,
|
|
|
|
retStatus,
|
|
|
|
pAuthenticateResp->pToken,
|
|
|
|
pAuthenticateResp->tokenLifetime);
|
|
|
|
|
|
|
|
pAuthenticateResp->pToken = NULL; // To keep us from freeing the buffer
|
|
|
|
|
|
|
|
// Free the Authenticate response object
|
|
|
|
RelAuthenticateResp(pAuthenticateResp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DbgTrace(0, "-ObtainSessionToken- Authenticate Rpc failure, error = %08X\n", retStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free resources that may be hanging around
|
|
|
|
if (pRespMsg)
|
|
|
|
free(pRespMsg);
|
|
|
|
|
|
|
|
free(pReqMsg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DbgTrace(0, "-ObtainSessionToken- Error building Authenticate msg\n", 0);
|
|
|
|
retStatus = CasaStatusBuild(CASA_SEVERITY_ERROR,
|
|
|
|
CASA_FACILITY_AUTHTOKEN,
|
|
|
|
CASA_STATUS_INSUFFICIENT_RESOURCES);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the entry to the cache if successful or if the reason that we failed
|
|
|
|
// was because the server was unavailable.
|
|
|
|
if (CasaStatusCode(retStatus) == CASA_STATUS_AUTH_SERVER_UNAVAILABLE)
|
|
|
|
{
|
|
|
|
pCacheEntry = CreateSessionTokenCacheEntry(
|
|
|
|
pAuthContext->pContext,
|
|
|
|
retStatus,
|
|
|
|
NULL,
|
|
|
|
DEFAULT_RETRY_LIFETIME);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release the cache entry if the resulting status is not successful
|
|
|
|
if (!CASA_SUCCESS(retStatus))
|
|
|
|
{
|
|
|
|
FreeAuthCacheEntry(pCacheEntry);
|
|
|
|
}
|
2006-04-03 22:20:37 +02:00
|
|
|
|
2006-04-28 20:58:25 +02:00
|
|
|
// Free up the buffer associated with the authentication mechanism token
|
2006-04-03 22:20:37 +02:00
|
|
|
free(pAuthMechToken);
|
|
|
|
}
|
2006-05-19 22:10:29 +02:00
|
|
|
else
|
|
|
|
{
|
2006-06-20 22:33:11 +02:00
|
|
|
// Free the entry
|
|
|
|
FreeAuthCacheEntry(pCacheEntry);
|
2006-05-19 22:10:29 +02:00
|
|
|
}
|
2006-04-03 22:20:37 +02:00
|
|
|
|
|
|
|
// Advance to the next entry
|
|
|
|
pListEntry = pListEntry->Flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return session token if successful
|
|
|
|
if (CASA_SUCCESS(retStatus))
|
|
|
|
{
|
|
|
|
// Allocate a buffer for the return token
|
2006-06-20 22:33:11 +02:00
|
|
|
*ppSessionToken = (char*) malloc(strlen(pCacheEntry->token) + 1);
|
2006-04-03 22:20:37 +02:00
|
|
|
if (*ppSessionToken)
|
|
|
|
{
|
|
|
|
// Copy the token onto the allocated buffer
|
2006-06-20 22:33:11 +02:00
|
|
|
strcpy(*ppSessionToken, pCacheEntry->token);
|
2006-04-03 22:20:37 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DbgTrace(0, "-ObtainSessionToken- Buffer allocation failure\n", 0);
|
|
|
|
retStatus = CasaStatusBuild(CASA_SEVERITY_ERROR,
|
|
|
|
CASA_FACILITY_AUTHTOKEN,
|
|
|
|
CASA_STATUS_INSUFFICIENT_RESOURCES);
|
|
|
|
}
|
2006-05-19 22:10:29 +02:00
|
|
|
|
2006-06-20 22:33:11 +02:00
|
|
|
FreeAuthCacheEntry(pCacheEntry);
|
2006-04-03 22:20:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DbgTrace(1, "-ObtainSessionToken- End, retStatus = %08X\n", retStatus);
|
|
|
|
|
|
|
|
return retStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//++=======================================================================
|
|
|
|
static
|
|
|
|
CasaStatus
|
|
|
|
ObtainAuthTokenFromServer(
|
2006-05-19 22:10:29 +02:00
|
|
|
IN const char *pServiceName,
|
|
|
|
IN const char *pHostName,
|
2006-04-03 22:20:37 +02:00
|
|
|
INOUT char **ppAuthToken,
|
|
|
|
INOUT int *pTokenLifetime)
|
|
|
|
//
|
|
|
|
// Arguments:
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
//
|
|
|
|
// Abstract:
|
|
|
|
//
|
|
|
|
// Notes:
|
|
|
|
//
|
2006-04-28 20:58:25 +02:00
|
|
|
// L2
|
2006-04-03 22:20:37 +02:00
|
|
|
//=======================================================================--
|
|
|
|
{
|
|
|
|
CasaStatus retStatus = CASA_STATUS_SUCCESS;
|
|
|
|
RpcSession *pRpcSession;
|
|
|
|
|
|
|
|
DbgTrace(1, "-ObtainAuthTokenFromServer- Start\n", 0);
|
|
|
|
|
|
|
|
// Initialize output parameter
|
|
|
|
*ppAuthToken = NULL;
|
|
|
|
|
|
|
|
// Open Rpc Session to the auth service at the specified host
|
|
|
|
pRpcSession = OpenRpcSession(pHostName);
|
|
|
|
if (pRpcSession)
|
|
|
|
{
|
|
|
|
char *pReqMsg = NULL;
|
|
|
|
char *pRespMsg = NULL;
|
|
|
|
int respLen;
|
|
|
|
AuthPolicy *pAuthPolicy = NULL;
|
|
|
|
GetAuthPolicyResp *pGetAuthPolicyResp = NULL;
|
|
|
|
GetAuthTokenResp *pGetAuthTokenResp = NULL;
|
|
|
|
char *pSessionToken = NULL;
|
|
|
|
|
|
|
|
// Request the auth parameters associated with this service
|
|
|
|
pReqMsg = BuildGetAuthPolicyMsg(pServiceName, pHostName);
|
|
|
|
if (pReqMsg)
|
|
|
|
{
|
|
|
|
// Issue rpc
|
|
|
|
retStatus = Rpc(pRpcSession,
|
|
|
|
"GetAuthPolicy",
|
|
|
|
secureRpcSetting,
|
|
|
|
pReqMsg,
|
|
|
|
&pRespMsg,
|
|
|
|
&respLen);
|
|
|
|
if (CASA_SUCCESS(retStatus))
|
|
|
|
{
|
|
|
|
// Create GetAuthPolicy response object
|
|
|
|
retStatus = CreateGetAuthPolicyResp(pRespMsg, respLen, &pGetAuthPolicyResp);
|
|
|
|
if (CASA_SUCCESS(retStatus))
|
|
|
|
{
|
|
|
|
// Create the AuthPolicy object
|
|
|
|
retStatus = CreateAuthPolicy(pGetAuthPolicyResp->pPolicy,
|
|
|
|
pGetAuthPolicyResp->policyLen,
|
|
|
|
&pAuthPolicy);
|
|
|
|
if (CASA_SUCCESS(retStatus))
|
|
|
|
{
|
|
|
|
// Now try to obtain a session token
|
2006-04-28 20:58:25 +02:00
|
|
|
retStatus = ObtainSessionToken(pRpcSession, pAuthPolicy, &pSessionToken);
|
2006-04-03 22:20:37 +02:00
|
|
|
if (CASA_SUCCESS(retStatus))
|
|
|
|
{
|
|
|
|
// Request auth token for the service
|
|
|
|
free(pReqMsg);
|
|
|
|
pReqMsg = BuildGetAuthTokenMsg(pServiceName, pHostName, pSessionToken);
|
|
|
|
if (pReqMsg)
|
|
|
|
{
|
|
|
|
// Free the previous response msg buffer
|
|
|
|
free(pRespMsg);
|
|
|
|
pRespMsg = NULL;
|
|
|
|
|
|
|
|
// Issue rpc
|
|
|
|
retStatus = Rpc(pRpcSession,
|
|
|
|
"GetAuthToken",
|
|
|
|
secureRpcSetting,
|
|
|
|
pReqMsg,
|
|
|
|
&pRespMsg,
|
|
|
|
&respLen);
|
|
|
|
if (CASA_SUCCESS(retStatus))
|
|
|
|
{
|
|
|
|
// Create GetAuthPolicy response object
|
|
|
|
retStatus = CreateGetAuthTokenResp(pRespMsg, respLen, &pGetAuthTokenResp);
|
|
|
|
if (CASA_SUCCESS(retStatus))
|
|
|
|
{
|
|
|
|
// Return the auth token to the caller
|
|
|
|
*ppAuthToken = pGetAuthTokenResp->pToken;
|
|
|
|
pGetAuthTokenResp->pToken = NULL; // To keep us from freeing the buffer
|
|
|
|
*pTokenLifetime = pGetAuthTokenResp->tokenLifetime;
|
|
|
|
}
|
2006-04-28 20:58:25 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
DbgTrace(0, "-ObtainAuthTokenFromServer- Failed to create GetAuthTokenResp object, error = %08X\n", retStatus);
|
|
|
|
}
|
2006-04-03 22:20:37 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DbgTrace(0, "-ObtainAuthTokenFromServer- GetAuthToken Rpc failure, error = %08X\n", retStatus);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DbgTrace(0, "-ObtainAuthTokenFromServer- Error building GetAuthToken msg\n", 0);
|
|
|
|
retStatus = CasaStatusBuild(CASA_SEVERITY_ERROR,
|
|
|
|
CASA_FACILITY_AUTHTOKEN,
|
|
|
|
CASA_STATUS_INSUFFICIENT_RESOURCES);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-04-28 20:58:25 +02:00
|
|
|
DbgTrace(0, "-ObtainAuthTokenFromServer- Failed to obtain session token, error = %08X\n", retStatus);
|
2006-04-03 22:20:37 +02:00
|
|
|
}
|
|
|
|
}
|
2006-04-28 20:58:25 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
DbgTrace(0, "-ObtainAuthTokenFromServer- Failed to create AuthPolicy object, error = %08X\n", retStatus);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DbgTrace(0, "-ObtainAuthTokenFromServer- Failed to create GetAuthPolicyResp object, error = %08X\n", retStatus);
|
2006-04-03 22:20:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DbgTrace(0, "-ObtainAuthTokenFromServer- GetAuthPolicy Rpc failure, error = %08X\n", retStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free resources that may be hanging around
|
|
|
|
if (pReqMsg)
|
|
|
|
free(pReqMsg);
|
|
|
|
|
|
|
|
if (pRespMsg)
|
|
|
|
free(pRespMsg);
|
|
|
|
|
|
|
|
if (pSessionToken)
|
|
|
|
free(pSessionToken);
|
|
|
|
|
|
|
|
if (pGetAuthTokenResp)
|
|
|
|
RelGetAuthTokenResp(pGetAuthTokenResp);
|
|
|
|
|
|
|
|
if (pGetAuthPolicyResp)
|
|
|
|
RelGetAuthPolicyResp(pGetAuthPolicyResp);
|
|
|
|
|
|
|
|
if (pAuthPolicy)
|
|
|
|
RelAuthPolicy(pAuthPolicy);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DbgTrace(0, "-ObtainAuthTokenFromServer- Error building GetAuthPolicy msg\n", 0);
|
|
|
|
retStatus = CasaStatusBuild(CASA_SEVERITY_ERROR,
|
|
|
|
CASA_FACILITY_AUTHTOKEN,
|
|
|
|
CASA_STATUS_INSUFFICIENT_RESOURCES);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the Rpc Session
|
|
|
|
CloseRpcSession(pRpcSession);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DbgTrace(0, "-ObtainAuthTokenFromServer- Error opening Rpc session\n", 0);
|
|
|
|
retStatus = CasaStatusBuild(CASA_SEVERITY_ERROR,
|
|
|
|
CASA_FACILITY_AUTHTOKEN,
|
|
|
|
CASA_STATUS_INSUFFICIENT_RESOURCES);
|
|
|
|
}
|
|
|
|
|
|
|
|
DbgTrace(1, "-ObtainAuthTokenFromServer- End, retStatus = %08X\n", retStatus);
|
|
|
|
|
|
|
|
return retStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//++=======================================================================
|
|
|
|
CasaStatus SSCS_CALL
|
|
|
|
ObtainAuthToken(
|
2006-05-16 17:24:21 +02:00
|
|
|
IN const char *pServiceName,
|
|
|
|
IN const char *pHostName,
|
2006-04-03 22:20:37 +02:00
|
|
|
INOUT char *pAuthTokenBuf,
|
|
|
|
INOUT int *pAuthTokenBufLen)
|
|
|
|
//
|
|
|
|
// Arguments:
|
2006-05-16 17:24:21 +02:00
|
|
|
// pServiceName -
|
2006-04-28 20:58:25 +02:00
|
|
|
// Pointer to NULL terminated string that contains the
|
2006-05-16 17:24:21 +02:00
|
|
|
// name of the service to which the client is trying to
|
|
|
|
// authenticate.
|
|
|
|
//
|
|
|
|
// pHostName -
|
|
|
|
// Pointer to NULL terminated string that contains the
|
|
|
|
// name of the host where resides the service to which the
|
|
|
|
// client is trying to authenticate. Note that the name
|
2006-04-28 20:58:25 +02:00
|
|
|
// can either be a DNS name or a dotted IP address.
|
|
|
|
//
|
|
|
|
// pAuthTokenBuf -
|
|
|
|
// Pointer to buffer that will receive the authentication
|
|
|
|
// token. The length of this buffer is specified by the
|
|
|
|
// pAuthTokenBufLen parameter. Note that the the authentication
|
|
|
|
// token will be in the form of a NULL terminated string.
|
2006-04-03 22:20:37 +02:00
|
|
|
//
|
2006-04-28 20:58:25 +02:00
|
|
|
// pAuthTokenBufLen -
|
|
|
|
// Pointer to integer that contains the length of the
|
|
|
|
// buffer pointed at by pAuthTokenBuf. 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 pAuthTokenBuf is
|
|
|
|
// not large enough.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// Casa Status
|
|
|
|
//
|
|
|
|
// Description:
|
|
|
|
// Get authentication token to authenticate user to specified
|
|
|
|
// service at host.
|
2006-04-03 22:20:37 +02:00
|
|
|
//
|
2006-04-28 20:58:25 +02:00
|
|
|
// L2
|
2006-04-03 22:20:37 +02:00
|
|
|
//=======================================================================--
|
|
|
|
{
|
2006-05-16 17:24:21 +02:00
|
|
|
CasaStatus retStatus = CASA_STATUS_SUCCESS;
|
|
|
|
AuthCacheEntry *pCacheEntry;
|
|
|
|
char *pNormalizedHostName;
|
2006-06-20 22:33:11 +02:00
|
|
|
unsigned char *pToken;
|
2006-06-21 01:01:51 +02:00
|
|
|
HANDLE hUserMutex = NULL;
|
|
|
|
|
2006-04-03 22:20:37 +02:00
|
|
|
|
|
|
|
DbgTrace(1, "-ObtainAuthToken- Start\n", 0);
|
|
|
|
|
|
|
|
// Verify the input parameters
|
2006-05-16 17:24:21 +02:00
|
|
|
if (pServiceName == NULL
|
|
|
|
|| pHostName == NULL
|
2006-04-03 22:20:37 +02:00
|
|
|
|| pAuthTokenBufLen == NULL
|
|
|
|
|| (*pAuthTokenBufLen != 0 && pAuthTokenBuf == NULL))
|
|
|
|
{
|
|
|
|
DbgTrace(0, "-ObtainAuthToken- Invalid parameter\n", 0);
|
|
|
|
retStatus = CasaStatusBuild(CASA_SEVERITY_ERROR,
|
|
|
|
CASA_FACILITY_AUTHTOKEN,
|
|
|
|
CASA_STATUS_INVALID_PARAMETER);
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2006-06-20 22:33:11 +02:00
|
|
|
// Make sure we are initialized
|
|
|
|
// Obtain our synchronization mutex
|
2006-06-21 01:01:51 +02:00
|
|
|
AcquireInitializationMutex();
|
2006-06-20 22:33:11 +02:00
|
|
|
|
|
|
|
// Create user synchronization mutex
|
2006-06-21 01:01:51 +02:00
|
|
|
retStatus = CreateUserMutex(&hUserMutex);
|
2006-06-20 22:33:11 +02:00
|
|
|
|
|
|
|
if (retStatus != CASA_STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
DbgTrace(0, "-ObtainAuthToken- Error creating mutex for the user\n", 0);
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_bInitialized == FALSE)
|
|
|
|
{
|
|
|
|
retStatus = InitializeLibrary();
|
|
|
|
|
|
|
|
if (retStatus == CASA_STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
g_bInitialized = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release our synchronization mutex
|
2006-06-21 01:01:51 +02:00
|
|
|
ReleaseInitializationMutex();
|
2006-06-20 22:33:11 +02:00
|
|
|
|
2006-05-16 17:24:21 +02:00
|
|
|
// Normalize the host name
|
|
|
|
pNormalizedHostName = NormalizeHostName(pHostName);
|
|
|
|
if (pNormalizedHostName)
|
2006-04-03 22:20:37 +02:00
|
|
|
{
|
2006-05-16 17:24:21 +02:00
|
|
|
// Start user process synchronization
|
2006-06-21 01:01:51 +02:00
|
|
|
AcquireUserMutex(hUserMutex);
|
2006-04-03 22:20:37 +02:00
|
|
|
|
2006-05-16 17:24:21 +02:00
|
|
|
// Try to find a cache entry for the service
|
2006-06-02 18:58:27 +02:00
|
|
|
pCacheEntry = FindAuthTokenEntryInCache(pServiceName, pNormalizedHostName);
|
2006-05-16 17:24:21 +02:00
|
|
|
if (pCacheEntry == NULL)
|
|
|
|
{
|
2006-06-20 22:33:11 +02:00
|
|
|
// Initialize to retry in case of failure
|
|
|
|
int cacheEntryLifetime = DEFAULT_RETRY_LIFETIME;
|
|
|
|
|
|
|
|
// Cache entry created, now try to obtain auth token from the CASA Server
|
|
|
|
retStatus = ObtainAuthTokenFromServer(pServiceName,
|
|
|
|
pNormalizedHostName,
|
|
|
|
&pToken,
|
|
|
|
&cacheEntryLifetime);
|
|
|
|
|
|
|
|
// Add the entry to the cache if successful or if the reason that we failed
|
|
|
|
// was because the server was un-available.
|
|
|
|
if (CASA_SUCCESS(retStatus)
|
|
|
|
|| CasaStatusCode(retStatus) == CASA_STATUS_AUTH_SERVER_UNAVAILABLE)
|
|
|
|
{
|
|
|
|
pCacheEntry = CreateAuthTokenCacheEntry(
|
|
|
|
pServiceName,
|
|
|
|
pNormalizedHostName,
|
|
|
|
retStatus,
|
|
|
|
pToken,
|
|
|
|
cacheEntryLifetime);
|
|
|
|
|
|
|
|
if (pCacheEntry)
|
|
|
|
{
|
|
|
|
// Release the cache entry if the resulting status is not successful
|
|
|
|
if (!CASA_SUCCESS(retStatus))
|
|
|
|
{
|
|
|
|
FreeAuthCacheEntry(pCacheEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-05-19 22:10:29 +02:00
|
|
|
|
2006-05-16 17:24:21 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-05-19 22:10:29 +02:00
|
|
|
// Cache entry found, update the return status with the information saved in it
|
|
|
|
// and release it if its status is not successful.
|
|
|
|
if (!CASA_SUCCESS(retStatus = pCacheEntry->status))
|
|
|
|
{
|
2006-06-20 22:33:11 +02:00
|
|
|
FreeAuthCacheEntry(pCacheEntry);
|
2006-05-19 22:10:29 +02:00
|
|
|
}
|
2006-05-16 17:24:21 +02:00
|
|
|
}
|
2006-04-03 22:20:37 +02:00
|
|
|
|
2006-06-20 22:33:11 +02:00
|
|
|
|
2006-05-16 17:24:21 +02:00
|
|
|
// Try to return auth token if we have one to return
|
|
|
|
if (CASA_SUCCESS(retStatus))
|
|
|
|
{
|
2006-06-20 22:33:11 +02:00
|
|
|
int tokenLen = (int) strlen(pCacheEntry->token) + 1;
|
2006-04-03 22:20:37 +02:00
|
|
|
|
2006-05-16 17:24:21 +02:00
|
|
|
// We have an authentication token, try to return it to the caller
|
|
|
|
// after verifying that the supplied buffer is big enough.
|
|
|
|
if (*pAuthTokenBufLen >= tokenLen)
|
|
|
|
{
|
|
|
|
// Return the auth token to the caller
|
2006-06-20 22:33:11 +02:00
|
|
|
strcpy(pAuthTokenBuf, pCacheEntry->token);
|
2006-04-03 22:20:37 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-05-16 17:24:21 +02:00
|
|
|
DbgTrace(0, "-ObtainAuthToken- The supplied buffer is not large enough", 0);
|
2006-04-03 22:20:37 +02:00
|
|
|
retStatus = CasaStatusBuild(CASA_SEVERITY_ERROR,
|
|
|
|
CASA_FACILITY_AUTHTOKEN,
|
2006-05-16 17:24:21 +02:00
|
|
|
CASA_STATUS_BUFFER_OVERFLOW);
|
2006-04-03 22:20:37 +02:00
|
|
|
}
|
2006-05-16 17:24:21 +02:00
|
|
|
|
|
|
|
// Return the token length to the caller
|
|
|
|
*pAuthTokenBufLen = tokenLen;
|
2006-05-19 22:10:29 +02:00
|
|
|
|
2006-06-20 22:33:11 +02:00
|
|
|
FreeAuthCacheEntry(pCacheEntry);
|
2006-04-03 22:20:37 +02:00
|
|
|
}
|
|
|
|
|
2006-05-16 17:24:21 +02:00
|
|
|
// Stop user process synchronization
|
2006-06-21 01:01:51 +02:00
|
|
|
ReleaseUserMutex(hUserMutex);
|
2006-05-16 17:24:21 +02:00
|
|
|
|
|
|
|
// Free the space allocated for the normalized host name
|
|
|
|
free(pNormalizedHostName);
|
2006-04-03 22:20:37 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-05-16 17:24:21 +02:00
|
|
|
DbgTrace(0, "-ObtainAuthToken- Host name normalization failed\n", 0);
|
|
|
|
retStatus = CasaStatusBuild(CASA_SEVERITY_ERROR,
|
|
|
|
CASA_FACILITY_AUTHTOKEN,
|
|
|
|
CASA_STATUS_UNSUCCESSFUL);
|
2006-04-03 22:20:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
|
2006-06-21 01:01:51 +02:00
|
|
|
if (hUserMutex != NULL)
|
|
|
|
{
|
|
|
|
DestroyUserMutex(hUserMutex);
|
|
|
|
}
|
|
|
|
|
2006-04-03 22:20:37 +02:00
|
|
|
DbgTrace(1, "-ObtainAuthToken- End, retStatus = %08X\n", retStatus);
|
|
|
|
|
|
|
|
return retStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//++=======================================================================
|
|
|
|
int
|
2006-06-20 22:33:11 +02:00
|
|
|
Initialize(void)
|
2006-04-03 22:20:37 +02:00
|
|
|
//
|
|
|
|
// Arguments:
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
//
|
|
|
|
// Abstract:
|
|
|
|
//
|
|
|
|
// Notes:
|
|
|
|
//
|
2006-04-28 20:58:25 +02:00
|
|
|
// L2
|
2006-04-03 22:20:37 +02:00
|
|
|
//=======================================================================--
|
|
|
|
{
|
2006-06-21 01:01:51 +02:00
|
|
|
int retStatus;
|
2006-04-03 22:20:37 +02:00
|
|
|
|
|
|
|
DbgTrace(1, "-InitializeLibrary- Start\n", 0);
|
|
|
|
|
2006-06-21 01:01:51 +02:00
|
|
|
retStatus = CreateInitializationMutex();
|
2006-06-20 22:33:11 +02:00
|
|
|
|
|
|
|
DbgTrace(1, "-InitializeLibrary- End, retStatus = %08X\n", retStatus);
|
|
|
|
|
|
|
|
return retStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//++=======================================================================
|
|
|
|
int
|
|
|
|
InitializeLibrary(void)
|
|
|
|
//
|
|
|
|
// Arguments:
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
//
|
|
|
|
// Abstract:
|
|
|
|
//
|
|
|
|
// Notes:
|
|
|
|
//
|
|
|
|
// L2
|
|
|
|
//=======================================================================--
|
|
|
|
{
|
|
|
|
int retStatus = -1;
|
|
|
|
|
|
|
|
DbgTrace(1, "-InitializeLibrary- Start\n", 0);
|
|
|
|
|
|
|
|
// Initialize the host name normalization
|
|
|
|
retStatus = InitializeHostNameNormalization();
|
|
|
|
|
|
|
|
|
|
|
|
if (CASA_SUCCESS(retStatus))
|
2006-04-03 22:20:37 +02:00
|
|
|
{
|
2006-06-20 22:33:11 +02:00
|
|
|
retStatus = InitializeAuthCache();
|
2006-04-03 22:20:37 +02:00
|
|
|
}
|
|
|
|
|
2006-06-20 22:33:11 +02:00
|
|
|
|
2006-04-03 22:20:37 +02:00
|
|
|
DbgTrace(1, "-InitializeLibrary- End, retStatus = %08X\n", retStatus);
|
|
|
|
|
|
|
|
return retStatus;
|
|
|
|
}
|
|
|
|
|
2006-04-28 20:58:25 +02:00
|
|
|
//++=======================================================================
|
|
|
|
//++=======================================================================
|
|
|
|
//++=======================================================================
|
|
|
|
|