#ifndef __CLDAPNode_h__
#define __CLDAPNode_h__ 1
#include <stdio.h>
#include <string.h> //used for strcpy, etc.
#include <stdlib.h> //used for malloc
#include <map> //STL map class
#include <string> //STL string class
#include <vector> //STL vector class
#include <time.h> //time_t usage
#include "DirServices.h"
#include "DirServicesUtils.h"
#include "DirServicesConst.h"
#include "PrivateTypes.h"
#include "DSCThread.h"
#include "DSMutexSemaphore.h"
#include "CLDAPv3Configs.h"
#include <lber.h>
#include <ldap.h>
using namespace std;
typedef struct sLDAPNodeStruct sLDAPNodeStruct;
typedef struct sLDAPContextData {
LDAP *fHost; DSMutexSemaphore
*fLDAPSessionMutex; uInt32 fConfigTableIndex; char *fName; int fPort; int fType; bool authCallActive; uInt32 offset; uInt32 index;
char *fOpenRecordType; char *fOpenRecordName; char *fOpenRecordDN; char *fUserName; void *fAuthCredential; char *fAuthType;
tDirReference fPWSRef;
tDirNodeReference fPWSNodeRef;
uid_t fUID;
uid_t fEffectiveUID;
sLDAPNodeStruct *fLDAPNodeStruct;
} sLDAPContextData;
struct sLDAPNodeStruct {
LDAP *fHost; DSMutexSemaphore
*fLDAPSessionMutex; uInt32 fRefCount; uInt32 fOperationsCount; uInt32 fLDAPConfigTableIndex; char *fServerName; int fDirectLDAPPort; char *fUserName; void *fAuthCredential; char *fAuthType; int fConnectionStatus; time_t fDelayedBindTime; uInt32 fConnectionActiveCount; int fIdleTOCount; int fIdleTO; int fDelayRebindTry; };
typedef map<string, sLDAPNodeStruct*> LDAPNodeMap;
typedef LDAPNodeMap::iterator LDAPNodeMapI;
typedef vector<sLDAPNodeStruct*> LDAPNodeVector;
typedef LDAPNodeVector::iterator LDAPNodeVectorI;
enum {
kConnectionSafe = 0,
kConnectionUnsafe,
kConnectionUnknown
};
class CLDAPNode
{
public:
static bool fCheckThreadActive;
public:
CLDAPNode ( void );
virtual ~CLDAPNode ( void );
sInt32 SafeOpen ( char *inNodeName,
LDAP **outLDAPHost,
uInt32 *outLDAPConfigTableIndex,
CLDAPv3Configs *inConfigFromXML );
sInt32 AuthOpen ( char *inNodeName,
LDAP *inHost,
char *inUserName,
void *inAuthCredential,
char *inAuthType,
LDAP **outLDAPHost,
uInt32 *inOutLDAPConfigTableIndex,
bool shouldCloseOld );
sInt32 RebindSession ( char *inNodeName,
LDAP *inHost,
CLDAPv3Configs *inConfigFromXML,
LDAP **outLDAPHost );
sInt32 SimpleAuth ( char *inNodeName,
char *inUserName,
void *inAuthCredential );
sInt32 RebindAuthSession( char *inNodeName,
LDAP *inHost,
char *inUserName,
void *inAuthCredential,
char *inAuthType,
uInt32 inLDAPConfigTableIndex,
LDAP **outLDAPHost );
sInt32 SafeClose ( char *inNodeName,
LDAP *inHost);
sInt32 ForcedSafeClose ( char *inNodeName);
void GetSchema ( sLDAPContextData *inContext );
LDAP* LockSession ( sLDAPContextData *inContext );
void UnLockSession ( sLDAPContextData *inContext, bool inHasFailed = false, bool inNewMutex = false );
void CheckIdles ( void );
void CheckFailed ( void );
void EnsureCheckFailedConnectionsThreadIsRunning ( void );
void NetTransition ( void );
void ActiveConnection( char *inNodeName );
void IdleConnection ( char *inNodeName );
LDAP* InitLDAPConnection
( sLDAPNodeStruct *inLDAPNodeStruct,
sLDAPConfigData *inConfig,
CLDAPv3Configs *inConfigFromXML = nil,
bool bInNeedWriteable = false );
static struct addrinfo*
ResolveHostName ( CFStringRef inServerNameRef,
int inPortNumber );
static LDAP* EstablishConnection
( sReplicaInfo *inList,
int inPort,
int inOpenTimeout,
bool bInNeedWriteable = false );
protected:
sInt32 CleanLDAPNodeStruct ( sLDAPNodeStruct *inLDAPNodeStruct );
sInt32 BindProc ( sLDAPNodeStruct *inLDAPNodeStruct,
CLDAPv3Configs *inConfigFromXML = nil,
bool bSessionBased = false, bool bForceBind = false );
sInt32 ParseLDAPNodeName ( char *inNodeName,
char **outLDAPName,
int *outLDAPPort );
sInt32 GetSchemaMessage ( LDAP *inHost,
int inSearchTO,
LDAPMessage **outResultMsg );
sInt32 GetReplicaListMessage
( LDAP *inHost,
int inSearchTO,
char *inConfigServerString,
CFMutableArrayRef outList,
CFMutableArrayRef outWriteableList );
char** GetNamingContexts ( LDAP *inHost,
int inSearchTO,
uInt32 *outCount );
sInt32 ExtractReplicaListMessage
( LDAP *inHost,
int inSearchTO,
sLDAPNodeStruct *inLDAPNodeStruct,
CLDAPv3Configs *inConfigFromXML,
CFMutableArrayRef outList,
CFMutableArrayRef outWriteableList );
sInt32 RetrieveDefinedReplicas
( sLDAPNodeStruct *inLDAPNodeStruct,
CLDAPv3Configs *inConfigFromXML,
char *inConfigServerString,
CFMutableArrayRef &inOutRepList,
CFMutableArrayRef &inOutWriteableList,
int inPort,
sReplicaInfo **inOutList );
bool IsTokenNotATag ( char *inToken );
void RetrieveServerMappingsIfRequired
( sLDAPNodeStruct *inLDAPNodeStruct,
CLDAPv3Configs *inConfigFromXML);
void FreeReplicaList ( sReplicaInfo *inList );
static char * LDAPWithBlockingSocket
( struct addrinfo *addrInfo, int seconds );
static char * ConvertToIPAddress ( struct addrinfo *addrInfo );
static bool IsLocalAddress ( struct addrinfo *addrInfo );
static bool ReachableAddress ( struct addrinfo *addrInfo );
void CheckSASLMethods ( sLDAPNodeStruct *inLDAPNodeStruct,
CLDAPv3Configs *inConfigFromXML );
bool LocalServerIsLDAPReplica ( void );
private:
LDAPNodeMap fLDAPNodeMap;
LDAPNodeVector fDeadPoolLDAPNodeVector;
DSMutexSemaphore fLDAPNodeOpenMutex;
};
#endif // __CLDAPNode_h__