/***********************************************************************
 * 
 *  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>
 *
 ***********************************************************************/

#ifndef _INTERNAL_H_
#define _INTERNAL_H_

//===[ Include files ]=====================================================

#include "platform.h"
#include <expat.h>
#include <micasa_types.h>
#include <casa_status.h>
#include <casa_c_authtoken.h>
#include "list_entry.h"
#include "config_if.h"
#include "mech_if.h"
#include "proto.h"

//===[ Type definitions ]==================================================

#define MAX_RPC_REPLY_SZ (256 * 1024)

//
// ATS Host Entry structure
// 
typedef struct _ATSHostEntry
{
   LIST_ENTRY  listEntry;
   char        *pNameAndPort;
   char        *pName;
   uint16_t    port;


} ATSHostEntry, *PATSHostEntry;

//
// Authentication Context structure
// 
typedef struct _AuthContext
{
   LIST_ENTRY  listEntry;
   char        *pContext;
   size_t      contextLen;
   char        *pMechanism;
   size_t      mechanismLen;
   char        *pMechInfo;
   size_t      mechInfoLen;

} AuthContext, *PAuthContext;

//
// Authentication Policy structure
// 
typedef struct _AuthPolicy
{
   LIST_ENTRY  authContextListHead;

} AuthPolicy, *PAuthPolicy;

//
// Get Authentication Policy Response structure
// 
typedef struct _GetAuthPolicyResp
{
   char     *pPolicy;
   size_t   policyLen;

} GetAuthPolicyResp, *PGetAuthPolicyResp;

//
// Get Authentication Token Response structure
// 
typedef struct _GetAuthTokenResp
{
   char     *pToken;
   size_t   tokenLen;
   int      tokenLifetime;

} GetAuthTokenResp, *PGetAuthTokenResp;

//
// Authenticate Response structure
//
typedef struct _AuthenticateResp
{
   char     *pToken;
   size_t   tokenLen;
   int      tokenLifetime;

} AuthenticateResp, *PAuthenticateResp;

//
// Auth Cache Entry definition
//
// IMPORTANT NOTE - If changes are made to this structure then you
// will need to deal with compatibility issues with cached tokens
// since the entries are stored binarily in the miCASA cache.
// 
typedef struct _AuthCacheEntry
{
   int         status;
   DWORD       creationTime;
   DWORD       expirationTime;
   bool        doesNotExpire;
   char        token[1];
    
} AuthCacheEntry, *PAuthCacheEntry;


//===[ Inlines functions   ]===============================================

//===[ Function prototypes ]===============================================

//===[ Global variables ]==================================================

//===[ Global externals ]==================================================

extern int  DebugLevel;
extern char *g_pDebugLogFilePath;

extern char clientConfigFolder[];

extern char mechConfigFolder[];

extern char pathCharString[];


//===[ External prototypes ]===============================================

//
// Functions exported by engine.c
// 

extern
CasaStatus
ObtainAuthTokenInt(
   IN    const char *pServiceName,
   IN    const char *pHostName,
   IN    const void *pCredStoreScope,
   INOUT char *pAuthTokenBuf,
   INOUT int *pAuthTokenBufLen);

//
// Functions exported by authmech.c
// 

extern
CasaStatus
GetAuthMechToken(
   IN    AuthContext *pAuthContext,
   IN    const char *pHostName,
   IN    void *pCredStoreScope,
   INOUT char **ppAuthMechToken);

//
// Functions exported by getpolicymsg.c
// 

extern
char*
BuildGetAuthPolicyMsg(
   IN    const char *pServiceName,
   IN    const char *pHostName);

extern
CasaStatus
CreateGetAuthPolicyResp(
   IN    char *pRespMsg,
   IN    size_t respLen,
   INOUT GetAuthPolicyResp **ppGetAuthPolicyResp);

extern
void
RelGetAuthPolicyResp(
   IN    GetAuthPolicyResp *pGetAuthPolicyResp);

extern
int
InitializeLibrary(void);

extern
void
UnInitializeLibrary(void);


//
// Functions exported by authpolicy.c
// 

extern
CasaStatus
CreateAuthPolicy(
   IN    char *pEncodedData,
   IN    size_t encodedDataLen,
   INOUT AuthPolicy **ppAuthPolicy);

extern
void
RelAuthPolicy(
   IN    AuthPolicy *pAuthPolicy);

//
// Functions exported by authmsg.c
// 

extern
char*
BuildAuthenticateMsg(
   IN    AuthContext *pAuthContext,
   IN    char *pAuthMechToken);

extern
CasaStatus
CreateAuthenticateResp(
   IN    char *pRespMsg,
   IN    size_t respLen,
   INOUT AuthenticateResp **ppAuthenticateResp);

extern
void
RelAuthenticateResp(
   IN    AuthenticateResp *pAuthenticateResp);

//
// Functions exported by gettokenmsg.c
// 

extern
char*
BuildGetAuthTokenMsg(
   IN    const char *pServiceName,
   IN    const char *pHostName,
   IN    char *pSessionToken);

extern
CasaStatus
CreateGetAuthTokenResp(
   IN    char *pRespMsg,
   IN    size_t respLen,
   INOUT GetAuthTokenResp **ppGetAuthTokenResp);

extern
void
RelGetAuthTokenResp(
   IN    GetAuthTokenResp *pGetAuthTokenResp);

//
// Functions exported by cache.c
//

extern
AuthCacheEntry*
CreateSessionTokenCacheEntry(
   IN    const char *pCacheKey,
   IN	   CasaStatus status,
   IN    char *pToken,
   IN    int entryLifetime,
   IN    void *pCredStoreScope);

extern
AuthCacheEntry*
CreateAuthTokenCacheEntry(
   IN    const char *pCacheKey,
   IN    const char *pHostName,
   IN    const ATSHostEntry *pATSHost,
   IN	   CasaStatus status,
   IN    char *pToken,
   IN    int entryLifetime,
   IN    void *pCredStoreScope);

extern
void
FreeAuthCacheEntry(
   IN    AuthCacheEntry *pEntry);

extern
AuthCacheEntry*
FindSessionTokenEntryInCache(
   IN    const char *pCacheKey,
   IN    void *pCredStoreScope);

extern
AuthCacheEntry*
FindAuthTokenEntryInCache(
   IN    const char *pCacheKey,
   IN    const char *pGroupOrHostName,
   IN    const ATSHostEntry *pATSHost,
   IN    void *pCredStoreScope);

extern
void
RemoveSessionTokenEntryInCache(
   IN    const char *pCacheKey,
   IN    void *pCredStoreScope);

extern
void
DeleteAuthTokenEntriesInCache(
   IN    void *pCredStoreScope);

extern
void
DeleteSessionTokenEntriesInCache(
   IN    void *pCredStoreScope);

extern
CasaStatus
InitializeAuthCache(void);

extern
void
UnInitializeAuthCache(void);


//
// Functions exported by config.c
//

extern
CasaStatus
GetConfigInterface(
   IN       const char  *pConfigFolder,
   IN       const char  *pConfigName,
   INOUT    ConfigIf    **ppConfigIf);

//
// Functions exported by platform.c
//

extern
CasaStatus
CreateUserMutex(
   HANDLE *phMutex
   );

extern
void
AcquireUserMutex(
   HANDLE hMutex
   );

extern
void
ReleaseUserMutex(
   HANDLE hMutex
   );

extern
void
DestroyUserMutex(
   HANDLE hMutex
   );

extern
LIB_HANDLE
OpenLibrary(
   IN    char *pFileName);

extern
void
CloseLibrary(
   IN    LIB_HANDLE libHandle);

extern
void*
GetFunctionPtr(
   IN    LIB_HANDLE libHandle,
   IN    char *pFunctionName);

extern
char*
NormalizeHostName(
   IN    const char *pHostName);

extern
CasaStatus
InitializeHostNameNormalization(void);

extern
void
UnInitializeHostNameNormalization(void);

//
// Functions exported by rpc.c
//

extern
RpcSession*
OpenRpcSession(
   IN    const char *pHostName,
   IN    const uint16_t hostPort);

extern
void
CloseRpcSession(
   IN    RpcSession *pSession);

#define SECURE_RPC_FLAG                            1
#define ALLOW_INVALID_CERTS_RPC_FLAG               2
#define ALLOW_INVALID_CERTS_USER_APPROVAL_RPC_FLAG 4

extern
CasaStatus
Rpc(
   IN    RpcSession *pSession,
   IN    char *pMethod,
   IN    long flags,
   IN    char *pRequestData,
   INOUT char **ppResponseData,
   INOUT size_t *pResponseDataLen);

extern
CasaStatus
InitializeRpc(void);

extern
void
UnInitializeRpc(void);


//
// Functions exported by utils.c
//

extern
CasaStatus
EncodeData(
   IN    const void *pData,
   IN    const uint32_t dataLen,
   INOUT char **ppEncodedData,
   INOUT uint32_t *pEncodedDataLen);

extern
CasaStatus
DecodeData(
   IN    const char *pEncodedData,
   IN    const uint32_t encodedDataLen, // Does not include NULL terminator
   INOUT void **ppData,
   INOUT uint32_t *pDataLen);

extern
int
dtoul(
   IN    const char *cp,
   IN    const int len);

//
// Functions exported by invalidcert.c
//

extern
bool
InvalidCertsFromHostAllowed(
   IN    char *pHostName);

extern
void
AllowInvalidCertsFromHost(
   IN    char *pHostName);

#define INVALID_CERT_CA_FLAG     1
#define INVALID_CERT_CN_FLAG     2
#define INVALID_CERT_DATE_FLAG   4

extern
bool
UserApprovedCert(
   IN    char *pHostName,
   IN    char *pCertSubject,
   IN    char *pCertIssuer,
   IN    long invalidCertFlags);


//=========================================================================

#endif // _INTERNAL_H_