CLDAPv3Configs.cpp [plain text]
#include <string.h> //used for strcpy, etc.
#include <stdlib.h> //used for malloc
#include <sys/types.h>
#include <sys/stat.h> //used for mkdir and stat
#include <syslog.h> //error logging
#include "CLDAPv3Configs.h"
#include "CLog.h"
#include "DSLDAPUtils.h"
#include "DirServices.h"
#include "DirServicesUtils.h"
#include "DirServicesConst.h"
CLDAPv3Configs::CLDAPv3Configs ( void )
{
pConfigTable = nil;
fConfigTableLen = 0;
fXMLData = nil;
pXMLConfigLock = new DSMutexSemaphore();
}
CLDAPv3Configs::~CLDAPv3Configs ( void )
{
uInt32 iTableIndex = 0;
sLDAPConfigData *pConfig = nil;
for (iTableIndex=0; iTableIndex<fConfigTableLen; iTableIndex++)
{
pConfig = (sLDAPConfigData *)pConfigTable->GetItemData( iTableIndex );
if (pConfig != nil)
{
CleanLDAPConfigData( pConfig );
delete( pConfig );
pConfig = nil;
pConfigTable->RemoveItem( iTableIndex );
}
}
fConfigTableLen = 0;
if ( pConfigTable != nil)
{
delete ( pConfigTable );
pConfigTable = nil;
}
if (pXMLConfigLock != nil)
{
delete(pXMLConfigLock);
pXMLConfigLock = nil;
}
if (fXMLData != nil)
{
CFRelease(fXMLData);
fXMLData = nil;
}
}
sInt32 CLDAPv3Configs::Init ( CPlugInRef *inConfigTable, uInt32 &inConfigTableLen )
{
sInt32 siResult = eDSNoErr;
sLDAPConfigData *pConfig = nil;
uInt32 sIndex = 0;
uInt32 iTableIndex = 0;
if ( inConfigTable == nil )
{
inConfigTable = new CPlugInRef( nil );
}
pConfigTable = inConfigTable;
if (!CheckForConfig((char *)"unknown", sIndex))
{
pConfig = MakeLDAPConfigData((char *)"Generic",(char *)"unknown",15,2,120,120,389,false, 0, 0, false, false, false, nil, true, nil );
pConfigTable->AddItem( fConfigTableLen, pConfig );
fConfigTableLen++;
}
XMLConfigLock();
if (fXMLData != nil)
{
CFRelease(fXMLData);
fXMLData = nil;
}
siResult = ReadXMLConfig();
XMLConfigUnlock();
if (siResult == eDSNoErr)
{
for (iTableIndex=0; iTableIndex<fConfigTableLen; iTableIndex++)
{
pConfig = (sLDAPConfigData *)pConfigTable->GetItemData( iTableIndex );
if (pConfig != nil)
{
pConfig->bUpdated = false;
}
}
XMLConfigLock();
siResult = ConfigLDAPServers();
XMLConfigUnlock();
}
inConfigTableLen = fConfigTableLen;
return( siResult );
}
sInt32 CLDAPv3Configs::ReadXMLConfig ( void )
{
sInt32 siResult = eDSNoErr;
CFURLRef configFileURL = NULL;
CFURLRef configFileCorruptedURL = NULL;
CFDataRef xmlData = NULL;
struct stat statResult;
bool bReadFile = false;
bool bCorruptedFile = false;
bool bWroteFile = false;
CFMutableDictionaryRef configDict = NULL;
sInt32 errorCode = 0;
CFStringRef sCorruptedPath = NULL;
char *filenameString = "/Library/Preferences/DirectoryService/DSLDAPv3PlugInConfig.plist";
while ( !bReadFile )
{
siResult = ::stat( filenameString, &statResult );
CFStringRef sPath = CFStringCreateWithCString( kCFAllocatorDefault, filenameString, kCFStringEncodingUTF8 );
configFileURL = ::CFURLCreateWithFileSystemPath( kCFAllocatorDefault, sPath, kCFURLPOSIXPathStyle, false );
CFRelease( sPath );
if (siResult != eDSNoErr)
{
CreatePrefDirectory();
configDict = CFDictionaryCreateMutable( kCFAllocatorDefault,
0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks );
CFDictionarySetValue( configDict, CFSTR( kXMLLDAPVersionKey ), CFSTR( "DSLDAPv3PlugIn Version 1.5" ) );
DBGLOG( kLogPlugin, "Created a new LDAP XML config file since it did not exist" );
xmlData = CFPropertyListCreateXMLData( kCFAllocatorDefault, configDict);
siResult = CFURLWriteDataAndPropertiesToResource( configFileURL,
xmlData,
NULL,
&errorCode);
::chmod( filenameString, 0600 );
CFRelease(configDict);
configDict = nil;
CFRelease(xmlData);
xmlData = nil;
} chmod( filenameString, S_IRUSR | S_IWUSR );
bReadFile = CFURLCreateDataAndPropertiesFromResource(
kCFAllocatorDefault,
configFileURL,
&xmlData, NULL,
NULL,
&siResult);
}
if (bReadFile)
{
fXMLData = xmlData;
if (!VerifyXML())
{
char *corruptPath = "/Library/Preferences/DirectoryService/DSLDAPv3PlugInConfigCorrupted.plist";
DBGLOG( kLogPlugin, "LDAP XML config file is corrupted" );
bCorruptedFile = true;
sCorruptedPath = ::CFStringCreateWithCString( kCFAllocatorDefault, corruptPath, kCFStringEncodingUTF8 );
configFileCorruptedURL = ::CFURLCreateWithFileSystemPath( kCFAllocatorDefault, sCorruptedPath, kCFURLPOSIXPathStyle, false );
CFRelease( sCorruptedPath ); sCorruptedPath = nil;
bWroteFile = CFURLWriteDataAndPropertiesToResource( configFileCorruptedURL,
xmlData,
NULL,
&errorCode);
::chmod( corruptPath, 0600 );
CFRelease(xmlData);
xmlData = nil;
}
}
else {
DBGLOG( kLogPlugin, "LDAP XML config file is unreadable" );
bCorruptedFile = true;
}
if (bCorruptedFile)
{
configDict = CFDictionaryCreateMutable( kCFAllocatorDefault,
0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks );
CFDictionarySetValue( configDict, CFSTR( kXMLLDAPVersionKey ), CFSTR( "DSLDAPv3PlugIn Version 1.5" ) );
DBGLOG( kLogPlugin, "Writing a new LDAP XML config file" );
xmlData = CFPropertyListCreateXMLData( kCFAllocatorDefault, configDict);
fXMLData = xmlData;
siResult = CFURLWriteDataAndPropertiesToResource( configFileURL,
xmlData,
NULL,
&errorCode);
if (filenameString != nil)
{
::chmod( filenameString, 0600 );
}
CFRelease(configDict);
configDict = nil;
}
if( fXMLData )
{
if( ConvertLDAPv2Config() )
{
siResult = CFURLWriteDataAndPropertiesToResource( configFileURL,
fXMLData,
NULL,
&errorCode);
if (filenameString != nil)
{
::chmod( filenameString, 0600 );
}
}
}
if (configFileURL != nil)
{
CFRelease(configFileURL); configFileURL = nil;
}
if (configFileCorruptedURL != nil)
{
CFRelease(configFileCorruptedURL); configFileCorruptedURL = nil;
}
return( siResult );
}
sInt32 CLDAPv3Configs::WriteXMLConfig ( void )
{
sInt32 siResult = eDSNoErr;
CFURLRef configFileURL = NULL;
bool bWroteFile = false;
char string[ PATH_MAX ];
struct stat statResult;
sInt32 errorCode = 0;
char *filenameString = "/Library/Preferences/DirectoryService/DSLDAPv3PlugInConfig.plist";
while (!bWroteFile)
{
siResult = ::stat( filenameString, &statResult );
if (siResult != eDSNoErr)
{
CreatePrefDirectory();
}
CFStringRef sPath = CFStringCreateWithCString( kCFAllocatorDefault, filenameString, kCFStringEncodingUTF8 );
configFileURL = ::CFURLCreateWithFileSystemPath( kCFAllocatorDefault, sPath, kCFURLPOSIXPathStyle, false );
CFRelease( sPath );
if (fXMLData != nil)
{
bWroteFile = CFURLWriteDataAndPropertiesToResource( configFileURL,
fXMLData,
NULL,
&errorCode);
::chmod( filenameString, 0600 );
}
CFRelease(configFileURL); configFileURL = nil;
}
if (bWroteFile)
{
DBGLOG( kLogPlugin, "Have written the LDAP XML config file:" );
DBGLOG1( kLogPlugin, "%s", string );
siResult = eDSNoErr;
}
else
{
DBGLOG( kLogPlugin, "LDAP XML config file has NOT been written" );
DBGLOG( kLogPlugin, "Update to LDAP Config File Failed" );
siResult = eDSPlugInConfigFileError;
}
return( siResult );
}
sInt32 CLDAPv3Configs::AddToConfig ( CFDataRef inXMLData )
{
sInt32 siResult = eDSCorruptBuffer;
CFStringRef errorString = NULL;
CFPropertyListRef configPropertyList = NULL;
CFMutableDictionaryRef configDict = NULL;
char *tmpBuff = nil;
CFIndex cfBuffSize = 1024;
CFStringRef cfStringRef = NULL;
CFBooleanRef cfBool = false;
unsigned char cfNumBool = false;
CFNumberRef cfNumber = 0;
char *server = nil;
char *mapSearchBase = nil;
int portNumber = 389;
bool bIsSSL = false;
bool bServerMappings = false;
bool bUseConfig = false;
bool bReferrals = true; int opencloseTO = 15;
int idleTO = 2;
int delayRebindTry = 120;
int searchTO = 120;
CFPropertyListRef xConfigPropertyList = NULL;
CFMutableDictionaryRef xConfigDict = NULL;
CFMutableArrayRef cfMutableArrayRef = NULL;
CFDataRef xmlBlob = NULL;
if (inXMLData != nil)
{
configPropertyList = CFPropertyListCreateFromXMLData( kCFAllocatorDefault,
inXMLData,
kCFPropertyListMutableContainers, &errorString);
if (configPropertyList != nil )
{
if ( CFDictionaryGetTypeID() == CFGetTypeID( configPropertyList ) )
{
configDict = (CFMutableDictionaryRef) configPropertyList;
}
if (configDict != nil)
{
CFDictionarySetValue(configDict, CFSTR( kXMLMakeDefLDAPFlagKey ), kCFBooleanTrue);
if (fXMLData != nil)
{
xConfigPropertyList = CFPropertyListCreateFromXMLData( kCFAllocatorDefault,
fXMLData,
kCFPropertyListMutableContainers, &errorString);
if (xConfigPropertyList != nil )
{
if ( CFDictionaryGetTypeID() == CFGetTypeID( xConfigPropertyList ) )
{
xConfigDict = (CFMutableDictionaryRef) xConfigPropertyList;
if (xConfigDict != nil)
{
if ( CFDictionaryContainsKey( xConfigDict, CFSTR( kXMLConfigArrayKey ) ) )
{
cfMutableArrayRef = (CFMutableArrayRef)CFDictionaryGetValue( xConfigDict, CFSTR( kXMLConfigArrayKey ) );
CFArrayAppendValue(cfMutableArrayRef, configDict);
}
else {
cfMutableArrayRef = CFArrayCreateMutable( kCFAllocatorDefault, NULL, &kCFTypeArrayCallBacks);
CFArrayAppendValue(cfMutableArrayRef, configDict);
CFDictionarySetValue( xConfigDict, CFSTR( kXMLConfigArrayKey ), cfMutableArrayRef );
CFRelease(cfMutableArrayRef);
}
xmlBlob = CFPropertyListCreateXMLData( kCFAllocatorDefault, xConfigDict);
SetXMLConfig(xmlBlob);
CFRelease(xmlBlob);
xmlBlob = nil;
}
}
CFRelease(xConfigPropertyList);
}
}
if ( CFDictionaryContainsKey( configDict, CFSTR( kXMLEnableUseFlagKey ) ) )
{
tmpBuff = (char *)::calloc(1, 1024);
cfBool = (CFBooleanRef)CFDictionaryGetValue( configDict, CFSTR( kXMLEnableUseFlagKey ) );
if (cfBool != nil)
{
bUseConfig = CFBooleanGetValue( cfBool );
}
if ( bUseConfig )
{
if ( CFDictionaryContainsKey( configDict, CFSTR( kXMLServerKey ) ) )
{
cfStringRef = (CFStringRef)CFDictionaryGetValue( configDict, CFSTR( kXMLServerKey ) );
if ( cfStringRef != nil )
{
if ( CFGetTypeID( cfStringRef ) == CFStringGetTypeID() )
{
::memset(tmpBuff,0,1024);
if (CFStringGetCString(cfStringRef, tmpBuff, cfBuffSize, kCFStringEncodingUTF8))
{
server = (char *)::calloc(1+strlen(tmpBuff),1);
::strcpy(server, tmpBuff);
}
}
}
}
if ( CFDictionaryContainsKey( configDict, CFSTR( kXMLIsSSLFlagKey ) ) )
{
cfBool= (CFBooleanRef)CFDictionaryGetValue( configDict, CFSTR( kXMLIsSSLFlagKey ) );
if (cfBool != nil)
{
bIsSSL = CFBooleanGetValue( cfBool );
if (bIsSSL)
{
portNumber = 636; }
}
}
if ( CFDictionaryContainsKey( configDict, CFSTR( kXMLOpenCloseTimeoutSecsKey ) ) )
{
cfNumber = (CFNumberRef)CFDictionaryGetValue( configDict, CFSTR( kXMLOpenCloseTimeoutSecsKey ) );
if ( cfNumber != nil )
{
cfNumBool = CFNumberGetValue(cfNumber, kCFNumberIntType, &opencloseTO);
}
}
if ( CFDictionaryContainsKey( configDict, CFSTR( kXMLIdleTimeoutMinsKey ) ) )
{
cfNumber = (CFNumberRef)CFDictionaryGetValue( configDict, CFSTR( kXMLIdleTimeoutMinsKey ) );
if ( cfNumber != nil )
{
cfNumBool = CFNumberGetValue(cfNumber, kCFNumberIntType, &idleTO);
}
}
if ( CFDictionaryContainsKey( configDict, CFSTR( kXMLDelayedRebindTrySecsKey ) ) )
{
cfNumber = (CFNumberRef)CFDictionaryGetValue( configDict, CFSTR( kXMLDelayedRebindTrySecsKey ) );
if ( cfNumber != nil )
{
cfNumBool = CFNumberGetValue(cfNumber, kCFNumberIntType, &delayRebindTry);
}
}
if ( CFDictionaryContainsKey( configDict, CFSTR( kXMLSearchTimeoutSecsKey ) ) )
{
cfNumber = (CFNumberRef)CFDictionaryGetValue( configDict, CFSTR( kXMLSearchTimeoutSecsKey ) );
if ( cfNumber != nil )
{
cfNumBool = CFNumberGetValue(cfNumber, kCFNumberIntType, &searchTO);
}
}
if ( CFDictionaryContainsKey( configDict, CFSTR( kXMLPortNumberKey ) ) )
{
cfNumber = (CFNumberRef)CFDictionaryGetValue( configDict, CFSTR( kXMLPortNumberKey ) );
if ( cfNumber != nil )
{
cfNumBool = CFNumberGetValue(cfNumber, kCFNumberIntType, &portNumber);
}
}
if ( CFDictionaryContainsKey( configDict, CFSTR( kXMLServerMappingsFlagKey ) ) )
{
cfBool = (CFBooleanRef)CFDictionaryGetValue( configDict, CFSTR( kXMLServerMappingsFlagKey ) );
if (cfBool != nil)
{
bServerMappings = CFBooleanGetValue( cfBool );
}
}
if ( CFDictionaryContainsKey( configDict, CFSTR( kXMLMapSearchBase ) ) )
{
cfStringRef = (CFStringRef)CFDictionaryGetValue( configDict, CFSTR( kXMLMapSearchBase ) );
if ( cfStringRef != nil )
{
if ( CFGetTypeID( cfStringRef ) == CFStringGetTypeID() )
{
::memset(tmpBuff,0,1024);
if (CFStringGetCString(cfStringRef, tmpBuff, cfBuffSize, kCFStringEncodingUTF8))
{
mapSearchBase = (char *)::calloc(1+strlen(tmpBuff),1);
::strcpy(mapSearchBase, tmpBuff);
}
}
}
}
if( cfBool = (CFBooleanRef) CFDictionaryGetValue( configDict, CFSTR(kXMLReferralFlagKey) ) )
{
if( CFGetTypeID(cfBool) == CFBooleanGetTypeID() )
{
bReferrals = CFBooleanGetValue( cfBool );
}
}
siResult = MakeServerBasedMappingsLDAPConfig( server, mapSearchBase, opencloseTO, idleTO, delayRebindTry, searchTO, portNumber, bIsSSL, true, bReferrals );
if ( server != nil )
{
free( server );
server = nil;
}
if ( mapSearchBase != nil )
{
free( mapSearchBase );
mapSearchBase = nil;
}
}
delete( tmpBuff );
} }
CFRelease(configPropertyList);
}
}
return(siResult);
}
sInt32 CLDAPv3Configs::SetXMLConfig ( CFDataRef xmlData )
{
CFDataRef currentXMLData = fXMLData;
fXMLData = xmlData;
if (VerifyXML())
{
if (currentXMLData != nil)
{
CFRelease(currentXMLData);
currentXMLData = nil;
}
CFRetain(fXMLData);
return eDSNoErr;
}
else
{
fXMLData = currentXMLData;
return eDSInvalidPlugInConfigData;
}
}
CFDataRef CLDAPv3Configs::CopyXMLConfig ( void )
{
CFDataRef combinedConfigDataRef = NULL;
CFMutableDictionaryRef configDict = NULL;
CFStringRef errorString = NULL;
CFArrayRef configArray = NULL;
CFIndex configArrayCount = 0;
CFMutableArrayRef dhcpConfigArray = NULL;
uInt32 index = 0;
sLDAPConfigData* pConfig = nil;
for (index=0; index<fConfigTableLen; index++)
{
pConfig = (sLDAPConfigData *)pConfigTable->GetItemData( index );
if (pConfig != nil)
{
if (pConfig->bUseAsDefaultLDAP) {
bool isCurrentConfInXMLData = false;
CFStringRef curConfigServerName = CFStringCreateWithCString( NULL, pConfig->fServerName, kCFStringEncodingUTF8 );
if ( configDict == NULL )
{
configDict = (CFMutableDictionaryRef)CFPropertyListCreateFromXMLData( kCFAllocatorDefault,
fXMLData,
kCFPropertyListMutableContainers, &errorString);
if ( configDict == NULL )
{
char errBuf[1024];
CFStringGetCString( errorString, errBuf, sizeof(errBuf), kCFStringEncodingUTF8 );
syslog(LOG_ERR,"DSLDAPv3PlugIn: [%s] LDAP server config could not be read.", errBuf);
CFRelease( curConfigServerName );
curConfigServerName = NULL;
break;
}
if ( CFDictionaryGetTypeID() != CFGetTypeID( configDict ) )
{
syslog(LOG_ERR,"DSLDAPv3PlugIn: LDAP server config could not be read as it was not in the correct format!");
CFRelease( configDict );
configDict = NULL;
CFRelease( curConfigServerName );
curConfigServerName = NULL;
break;
}
configArray = (CFArrayRef)CFDictionaryGetValue( configDict, CFSTR(kXMLConfigArrayKey) );
if ( configArray != NULL )
configArrayCount = CFArrayGetCount( configArray );
}
for ( CFIndex i=0; i<configArrayCount; i++ )
{
CFStringRef indexedServerName = (CFStringRef)CFDictionaryGetValue( (CFDictionaryRef)CFArrayGetValueAtIndex( configArray, i ), CFSTR(kXMLServerKey) );
if ( CFStringCompare( curConfigServerName, indexedServerName, 0 ) == kCFCompareEqualTo )
{
isCurrentConfInXMLData = true;
break;
}
}
if ( !isCurrentConfInXMLData )
{
if ( dhcpConfigArray == NULL )
dhcpConfigArray = CFArrayCreateMutable( NULL, 0, &kCFTypeArrayCallBacks );
CFMutableDictionaryRef curConfigDict = CFDictionaryCreateMutable( NULL, 0, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks );
CFNumberRef curConfigPort = CFNumberCreate( NULL, kCFNumberIntType, &(pConfig->fServerPort) );
CFStringRef curConfigUIName = CFStringCreateWithCString( NULL, pConfig->fName, kCFStringEncodingUTF8 );
CFNumberRef curConfigOpenCloseTimeOut = CFNumberCreate( NULL, kCFNumberIntType, &(pConfig->fOpenCloseTimeout) );
CFNumberRef curConfigSearchTimeOut = CFNumberCreate( NULL, kCFNumberIntType, &(pConfig->fSearchTimeout) );
CFDictionaryAddValue( curConfigDict, CFSTR(kXMLUserDefinedNameKey), curConfigUIName );
CFDictionaryAddValue( curConfigDict, CFSTR(kXMLServerKey), curConfigServerName );
if ( pConfig->fRecordTypeMapCFArray != NULL )
CFDictionaryAddValue( curConfigDict, CFSTR(kXMLRecordTypeMapArrayKey), pConfig->fRecordTypeMapCFArray );
if ( pConfig->fAttrTypeMapCFArray != NULL )
CFDictionaryAddValue( curConfigDict, CFSTR(kXMLAttrTypeMapArrayKey), pConfig->fAttrTypeMapCFArray );
if ( pConfig->fReplicaHostnames != NULL)
CFDictionaryAddValue( curConfigDict, CFSTR(kXMLReplicaHostnameListArrayKey), pConfig->fReplicaHostnames );
if ( pConfig->fWriteableHostnames != NULL)
CFDictionaryAddValue( curConfigDict, CFSTR(kXMLWriteableHostnameListArrayKey), pConfig->fWriteableHostnames );
CFDictionaryAddValue( curConfigDict, CFSTR(kXMLPortNumberKey), curConfigPort );
CFDictionaryAddValue( curConfigDict, CFSTR(kXMLOpenCloseTimeoutSecsKey), curConfigOpenCloseTimeOut );
CFDictionaryAddValue( curConfigDict, CFSTR(kXMLSearchTimeoutSecsKey), curConfigSearchTimeOut );
CFDictionaryAddValue( curConfigDict, CFSTR(kXMLIsSSLFlagKey), (pConfig->bIsSSL)?kCFBooleanTrue:kCFBooleanFalse );
CFDictionaryAddValue( curConfigDict, CFSTR(kXMLMakeDefLDAPFlagKey), kCFBooleanTrue );
CFDictionaryAddValue( curConfigDict, CFSTR(kXMLEnableUseFlagKey), kCFBooleanTrue );
CFDictionaryAddValue( curConfigDict, CFSTR(kXMLServerMappingsFlagKey), (pConfig->bServerMappings)?kCFBooleanTrue:kCFBooleanFalse );
CFDictionaryAddValue( curConfigDict, CFSTR(kXMLReferralFlagKey), (pConfig->bReferrals ? kCFBooleanTrue : kCFBooleanFalse) );
CFArrayAppendValue( dhcpConfigArray, curConfigDict );
CFRelease( curConfigSearchTimeOut );
CFRelease( curConfigOpenCloseTimeOut );
CFRelease( curConfigUIName );
CFRelease( curConfigPort );
CFRelease( curConfigDict );
}
CFRelease( curConfigServerName );
curConfigServerName = NULL;
}
}
}
if ( dhcpConfigArray == NULL )
{
combinedConfigDataRef = fXMLData;
CFRetain( combinedConfigDataRef );
}
else
{
CFDictionaryAddValue( configDict, CFSTR(kXMLDHCPConfigArrayKey), dhcpConfigArray );
combinedConfigDataRef = CFPropertyListCreateXMLData( NULL, configDict );
}
if ( dhcpConfigArray )
CFRelease( dhcpConfigArray );
if ( configDict )
CFRelease( configDict );
return combinedConfigDataRef;
}
bool CLDAPv3Configs::VerifyXML ( void )
{
bool verified = false;
CFStringRef errorString;
CFPropertyListRef configPropertyList;
if (fXMLData != nil)
{
configPropertyList = CFPropertyListCreateFromXMLData( kCFAllocatorDefault,
fXMLData,
kCFPropertyListImmutable,
&errorString);
if (configPropertyList != nil )
{
if ( CFDictionaryGetTypeID() == CFGetTypeID( configPropertyList ) )
{
verified = true;
}
CFRelease(configPropertyList);
configPropertyList = nil;
}
}
return( verified);
}
sInt32 CLDAPv3Configs::UpdateLDAPConfigWithServerMappings ( char *inServer, char *inMapSearchBase, int inPortNumber, bool inIsSSL, bool inMakeDefLDAP, bool inReferrals, LDAP *inServerHost)
{
sInt32 siResult = eDSNoErr;
CFDataRef ourXMLData = nil;
CFDataRef newXMLData = nil;
ourXMLData = RetrieveServerMappings( inServer, inMapSearchBase, inPortNumber, inIsSSL, inReferrals, inServerHost );
if (ourXMLData != nil)
{
newXMLData = VerifyAndUpdateServerLocation(inServer, inPortNumber, inIsSSL, inMakeDefLDAP, ourXMLData);
if (newXMLData != nil)
{
CFRelease(ourXMLData);
ourXMLData = newXMLData;
newXMLData = nil;
}
siResult = AddLDAPServer(ourXMLData);
CFRelease(ourXMLData);
if (siResult != eDSNoErr)
{
syslog(LOG_INFO,"DSLDAPv3PlugIn: [%s] LDAP server config not updated with server mappings due to server mappings format error.", inServer);
}
}
else
{
syslog(LOG_INFO,"DSLDAPv3PlugIn: [%s] LDAP server config not updated with server mappings due to server mappings error.", inServer);
siResult = eDSCannotAccessSession;
}
return(siResult);
}
sInt32 CLDAPv3Configs::ConfigLDAPServers ( void )
{
sInt32 siResult = eDSNoErr;
CFStringRef errorString = NULL;
CFPropertyListRef configPropertyList = NULL;
CFMutableDictionaryRef configDict = NULL;
CFArrayRef cfArrayRef = NULL;
CFIndex cfConfigCount = 0;
CFDataRef xmlData = NULL;
char *configVersion = nil;
try
{
if (fXMLData != nil)
{
configPropertyList = CFPropertyListCreateFromXMLData( kCFAllocatorDefault,
fXMLData,
kCFPropertyListMutableContainers, &errorString);
if (configPropertyList != nil )
{
if ( CFDictionaryGetTypeID() == CFGetTypeID( configPropertyList ) )
{
configDict = (CFMutableDictionaryRef) configPropertyList;
}
if (configDict != nil)
{
configVersion = GetVersion(configDict);
if ( configVersion == nil ) throw( (sInt32)eDSVersionMismatch ); if (configVersion != nil)
{
DBGLOG( kLogPlugin, "Have successfully read the LDAP XML config file" );
if (strcmp(configVersion,"DSLDAPv3PlugIn Version 1.5") == 0)
{
}
else
{
CFDictionaryRemoveValue( configDict, CFSTR( kXMLLDAPVersionKey ) );
CFDictionarySetValue( configDict, CFSTR( kXMLLDAPVersionKey ), CFSTR( "DSLDAPv3PlugIn Version 1.5" ) );
xmlData = CFPropertyListCreateXMLData( kCFAllocatorDefault, configDict);
siResult = SetXMLConfig(xmlData);
CFRelease(xmlData);
xmlData = nil;
if (siResult == eDSNoErr)
{
WriteXMLConfig();
}
}
cfArrayRef = nil;
cfArrayRef = GetConfigArray(configDict);
if (cfArrayRef != nil)
{
cfConfigCount = ::CFArrayGetCount( cfArrayRef );
for (sInt32 iConfigIndex = 0; iConfigIndex < cfConfigCount; iConfigIndex++)
{
CFDictionaryRef serverConfigDict = nil;
serverConfigDict = (CFDictionaryRef)::CFArrayGetValueAtIndex( cfArrayRef, iConfigIndex );
if ( serverConfigDict != nil )
{
{
siResult = MakeLDAPConfig(serverConfigDict, fConfigTableLen);
}
}
}
} delete(configVersion);
}
}
CFRelease(configPropertyList); configPropertyList = nil;
} }
} catch ( sInt32 err )
{
siResult = err;
if (configPropertyList != nil)
{
CFRelease(configPropertyList); configPropertyList = nil;
}
}
return( siResult );
}
CFDataRef CLDAPv3Configs::RetrieveServerMappings ( char *inServer, char *inMapSearchBase, int inPortNumber, bool inIsSSL, bool inReferrals, LDAP *inServerHost )
{
sInt32 siResult = eDSNoErr;
bool bResultFound = false;
int ldapMsgId = -1;
LDAPMessage *result = nil;
int ldapReturnCode = 0;
char *attrs[2] = {"description",NULL};
BerElement *ber;
struct berval **bValues;
char *pAttr = nil;
LDAP *serverHost = nil;
CFDataRef ourMappings = nil;
bool bCleanHost = false;
if (inServerHost == nil)
{
if ( (inServer != nil) && (inPortNumber != 0) )
{
serverHost = ldap_init( inServer, inPortNumber );
bCleanHost = true;
} }
else
{
serverHost = inServerHost;
}
if (serverHost != nil)
{
if (inIsSSL)
{
int ldapOptVal = LDAP_OPT_X_TLS_HARD;
ldap_set_option(serverHost, LDAP_OPT_X_TLS, &ldapOptVal);
}
ldap_set_option( serverHost, LDAP_OPT_REFERRALS, (inReferrals ? LDAP_OPT_ON : LDAP_OPT_OFF) );
if (inMapSearchBase == nil)
{
ldapMsgId = ldap_search( serverHost, "", LDAP_SCOPE_SUBTREE, "(&(objectclass=*)(ou=macosxodconfig))", attrs, 0);
}
else
{
ldapMsgId = ldap_search( serverHost, inMapSearchBase, LDAP_SCOPE_SUBTREE, "(&(objectclass=*)(ou=macosxodconfig))", attrs, 0);
}
if ( ldapMsgId == -1 )
{
bResultFound = false;
}
else
{
bResultFound = true;
struct timeval tv;
tv.tv_sec = 60;
tv.tv_usec = 0;
ldapReturnCode = ldap_result(serverHost, ldapMsgId, 0, &tv, &result);
}
if ( ( bResultFound ) && ( ldapReturnCode == LDAP_RES_SEARCH_ENTRY ) )
{
for ( pAttr = ldap_first_attribute (serverHost, result, &ber );
pAttr != NULL; pAttr = ldap_next_attribute(serverHost, result, ber ) )
{
if (( bValues = ldap_get_values_len (serverHost, result, pAttr )) != NULL)
{
if ( bValues[0] != NULL )
{
ourMappings = CFDataCreate(NULL,(UInt8 *)(bValues[0]->bv_val), bValues[0]->bv_len);
}
ldap_value_free_len(bValues);
}
if (pAttr != nil)
{
ldap_memfree( pAttr );
}
}
if (ber != nil)
{
ber_free( ber, 0 );
}
ldap_msgfree( result );
result = nil;
siResult = eDSNoErr;
}
else if (ldapReturnCode == LDAP_TIMEOUT)
{
siResult = eDSServerTimeout;
syslog(LOG_INFO,"DSLDAPv3PlugIn: Retrieval of Server Mappings for [%s] LDAP server has timed out.", inServer);
if ( result != nil )
{
ldap_msgfree( result );
result = nil;
}
}
else
{
siResult = eDSRecordNotFound;
syslog(LOG_INFO,"DSLDAPv3PlugIn: Server Mappings for [%s] LDAP server not found.", inServer);
if ( result != nil )
{
ldap_msgfree( result );
result = nil;
}
}
DSSearchCleanUp(serverHost, ldapMsgId);
if (bCleanHost)
{
ldap_unbind( serverHost );
}
}
return( ourMappings );
}
sInt32 CLDAPv3Configs::WriteServerMappings ( char* userName, char* password, CFDataRef inMappings )
{
sInt32 siResult = eDSNoErr;
LDAP *serverHost = nil;
CFStringRef errorString = NULL;
CFPropertyListRef configPropertyList = nil;
CFDictionaryRef serverConfigDict = nil;
char *server = nil;
int portNumber = 389;
int openCloseTO = kLDAPDefaultOpenCloseTimeoutInSeconds;
char *tmpBuff = nil;
CFIndex cfBuffSize = 1024;
CFStringRef cfStringRef = nil;
CFBooleanRef cfBool = nil;
CFNumberRef cfNumber = nil;
bool cfNumBool = false;
char *mapSearchBase = nil;
bool bIsSSL = false;
int ldapReturnCode = 0;
int version = -1;
int bindMsgId = 0;
LDAPMessage *result = nil;
char *ldapDNString = nil;
uInt32 ldapDNLength = 0;
char *ourXMLBlob = nil;
char *ouvals[2];
char *mapvals[2];
char *ocvals[3];
LDAPMod oumod;
LDAPMod mapmod;
LDAPMod ocmod;
LDAPMod *mods[4];
try
{
if (inMappings != nil)
{
configPropertyList = CFPropertyListCreateFromXMLData( kCFAllocatorDefault,
inMappings,
kCFPropertyListImmutable,
&errorString);
if (configPropertyList != nil )
{
if ( CFDictionaryGetTypeID() == CFGetTypeID( configPropertyList ) )
{
serverConfigDict = (CFDictionaryRef) configPropertyList;
}
if (serverConfigDict != nil)
{
tmpBuff = (char *)::calloc(1, 1024);
if ( CFDictionaryContainsKey( serverConfigDict, CFSTR( kXMLServerKey ) ) )
{
cfStringRef = (CFStringRef)CFDictionaryGetValue( serverConfigDict, CFSTR( kXMLServerKey ) );
if ( cfStringRef != nil )
{
if ( CFGetTypeID( cfStringRef ) == CFStringGetTypeID() )
{
::memset(tmpBuff,0,1024);
if (CFStringGetCString(cfStringRef, tmpBuff, cfBuffSize, kCFStringEncodingUTF8))
{
server = (char *)::calloc(1+strlen(tmpBuff),1);
::strcpy(server, tmpBuff);
}
}
}
}
if ( CFDictionaryContainsKey( serverConfigDict, CFSTR( kXMLOpenCloseTimeoutSecsKey ) ) )
{
cfNumber = (CFNumberRef)CFDictionaryGetValue( serverConfigDict, CFSTR( kXMLOpenCloseTimeoutSecsKey ) );
if ( cfNumber != nil )
{
cfNumBool = CFNumberGetValue(cfNumber, kCFNumberIntType, &openCloseTO);
}
}
if ( CFDictionaryContainsKey( serverConfigDict, CFSTR( kXMLIsSSLFlagKey ) ) )
{
cfBool= (CFBooleanRef)CFDictionaryGetValue( serverConfigDict, CFSTR( kXMLIsSSLFlagKey ) );
if (cfBool != nil)
{
bIsSSL = CFBooleanGetValue( cfBool );
if (bIsSSL)
{
portNumber = 636;
}
}
}
if ( CFDictionaryContainsKey( serverConfigDict, CFSTR( kXMLPortNumberKey ) ) )
{
cfNumber = (CFNumberRef)CFDictionaryGetValue( serverConfigDict, CFSTR( kXMLPortNumberKey ) );
if ( cfNumber != nil )
{
cfNumBool = CFNumberGetValue(cfNumber, kCFNumberIntType, &portNumber);
}
}
if ( CFDictionaryContainsKey( serverConfigDict, CFSTR( kXMLMapSearchBase ) ) )
{
cfStringRef = (CFStringRef)CFDictionaryGetValue( serverConfigDict, CFSTR( kXMLMapSearchBase ) );
if ( cfStringRef != nil )
{
if ( CFGetTypeID( cfStringRef ) == CFStringGetTypeID() )
{
::memset(tmpBuff,0,1024);
if (CFStringGetCString(cfStringRef, tmpBuff, cfBuffSize, kCFStringEncodingUTF8))
{
mapSearchBase = (char *)::calloc(1+strlen(tmpBuff),1);
::strcpy(mapSearchBase, tmpBuff);
}
}
}
}
free( tmpBuff );
tmpBuff = nil;
}
CFRelease(configPropertyList); configPropertyList = nil;
}
serverHost = ldap_init( server, portNumber );
if ( serverHost == nil ) throw( (sInt32)eDSCannotAccessSession );
if ( bIsSSL )
{
int ldapOptVal = LDAP_OPT_X_TLS_HARD;
ldap_set_option(serverHost, LDAP_OPT_X_TLS, &ldapOptVal);
}
version = LDAP_VERSION3;
ldap_set_option( serverHost, LDAP_OPT_PROTOCOL_VERSION, &version );
bindMsgId = ldap_bind( serverHost, userName, password, LDAP_AUTH_SIMPLE );
if (openCloseTO == 0)
{
ldapReturnCode = ldap_result(serverHost, bindMsgId, 0, NULL, &result);
}
else
{
struct timeval tv;
tv.tv_sec = openCloseTO;
tv.tv_usec = 0;
ldapReturnCode = ldap_result(serverHost, bindMsgId, 0, &tv, &result);
}
if ( ldapReturnCode == -1 )
{
throw( (sInt32)eDSCannotAccessSession );
}
else if ( ldapReturnCode == 0 )
{
ldap_unbind( serverHost );
serverHost = NULL;
throw( (sInt32)eDSCannotAccessSession );
}
else if ( ldap_result2error(serverHost, result, 1) != LDAP_SUCCESS )
{
throw( (sInt32)eDSCannotAccessSession );
}
if ( (serverHost != nil) && (mapSearchBase != nil) )
{
ldapDNLength = 21 + strlen(mapSearchBase);
ldapDNString = (char *)calloc(1, ldapDNLength + 1);
strcpy(ldapDNString,"ou = macosxodconfig, ");
strcat(ldapDNString,mapSearchBase);
ldapReturnCode = ldap_delete_s( serverHost, ldapDNString);
if ( ( ldapReturnCode == LDAP_INSUFFICIENT_ACCESS ) || ( ldapReturnCode == LDAP_INVALID_CREDENTIALS ) )
{
siResult = eDSPermissionError;
}
else if ( ldapReturnCode == LDAP_NO_SUCH_OBJECT )
{
siResult = eDSRecordNotFound;
}
else if ( ldapReturnCode != LDAP_SUCCESS )
{
siResult = eDSBogusServer;
}
if ( (siResult == eDSRecordNotFound) || (siResult == eDSNoErr) )
{
CFRange aRange;
aRange.location = 0;
aRange.length = CFDataGetLength(inMappings);
ourXMLBlob = (char *) calloc(1, aRange.length + 1);
CFDataGetBytes( inMappings, aRange, (UInt8*)ourXMLBlob );
ouvals[0] = "macosxodconfig";
ouvals[1] = NULL;
oumod.mod_op = 0;
oumod.mod_type = "ou";
oumod.mod_values = ouvals;
mapvals[0] = ourXMLBlob;
mapvals[1] = NULL;
mapmod.mod_op = 0;
mapmod.mod_type = "description";
mapmod.mod_values = mapvals;
ocvals[0] = "top";
ocvals[1] = "organizationalUnit";
ocvals[2] = NULL;
ocmod.mod_op = 0;
ocmod.mod_type = "objectclass";
ocmod.mod_values = ocvals;
mods[0] = &oumod;
mods[1] = &mapmod;
mods[2] = &ocmod;
mods[3] = NULL;
ldapReturnCode = 0;
siResult = eDSNoErr;
ldapReturnCode = ldap_add_s( serverHost, ldapDNString, mods);
if ( ( ldapReturnCode == LDAP_INSUFFICIENT_ACCESS ) || ( ldapReturnCode == LDAP_INVALID_CREDENTIALS ) )
{
siResult = eDSPermissionError;
}
else if ( ldapReturnCode == LDAP_ALREADY_EXISTS )
{
siResult = eDSRecordAlreadyExists;
}
else if ( ldapReturnCode == LDAP_NO_SUCH_OBJECT )
{
siResult = eDSRecordNotFound;
}
else if ( ldapReturnCode != LDAP_SUCCESS )
{
siResult = eDSBogusServer;
}
} } }
} catch ( sInt32 err )
{
siResult = err;
if (configPropertyList != nil)
{
CFRelease(configPropertyList); configPropertyList = nil;
}
}
if ( serverHost != nil )
{
ldap_unbind( serverHost );
serverHost = nil;
}
if ( mapSearchBase != nil )
{
free( mapSearchBase );
mapSearchBase = nil;
}
if ( ourXMLBlob != nil )
{
free( ourXMLBlob );
ourXMLBlob = nil;
}
if ( ldapDNString != nil )
{
free( ldapDNString );
ldapDNString = nil;
}
return( siResult );
}
CFDataRef CLDAPv3Configs::ReadServerMappings ( LDAP *serverHost, CFDataRef inMappings )
{
sInt32 siResult = eDSNoErr;
CFStringRef errorString;
CFPropertyListRef configPropertyList = nil;
CFMutableDictionaryRef serverConfigDict = nil;
char *configVersion = nil;
char *tmpBuff = nil;
CFIndex cfBuffSize = 1024;
CFStringRef cfStringRef = nil;
CFBooleanRef cfBool = false;
char *mapSearchBase = nil;
bool bIsSSL = false;
bool bServerMappings = false;
bool bUseConfig = false;
unsigned char cfNumBool = false;
CFNumberRef cfNumber = 0;
char *server = nil;
int portNumber = 389;
CFDataRef outMappings = nil;
try
{
if (inMappings != nil)
{
configPropertyList = CFPropertyListCreateFromXMLData( kCFAllocatorDefault,
inMappings,
kCFPropertyListMutableContainers,
&errorString);
if (configPropertyList != nil )
{
if ( CFDictionaryGetTypeID() == CFGetTypeID( configPropertyList ) )
{
serverConfigDict = (CFMutableDictionaryRef) configPropertyList; }
if (serverConfigDict != nil)
{
configVersion = GetVersion(serverConfigDict);
if ( configVersion == nil ) throw( (sInt32)eDSVersionMismatch ); if (configVersion != nil)
{
if (strcmp(configVersion,"DSLDAPv3PlugIn Version 1.5") == 0)
{
if ( CFDictionaryContainsKey( serverConfigDict, CFSTR( kXMLEnableUseFlagKey ) ) )
{
tmpBuff = (char *)::calloc(1, 1024);
cfBool = (CFBooleanRef)CFDictionaryGetValue( serverConfigDict, CFSTR( kXMLEnableUseFlagKey ) );
if (cfBool != nil)
{
bUseConfig = CFBooleanGetValue( cfBool );
}
if ( bUseConfig )
{
if ( CFDictionaryContainsKey( serverConfigDict, CFSTR( kXMLServerMappingsFlagKey ) ) )
{
cfBool = (CFBooleanRef)CFDictionaryGetValue( serverConfigDict, CFSTR( kXMLServerMappingsFlagKey ) );
if (cfBool != nil)
{
bServerMappings = CFBooleanGetValue( cfBool );
}
}
if (bServerMappings)
{
if ( CFDictionaryContainsKey( serverConfigDict, CFSTR( kXMLServerKey ) ) )
{
cfStringRef = (CFStringRef)CFDictionaryGetValue( serverConfigDict, CFSTR( kXMLServerKey ) );
if ( cfStringRef != nil )
{
if ( CFGetTypeID( cfStringRef ) == CFStringGetTypeID() )
{
::memset(tmpBuff,0,1024);
if (CFStringGetCString(cfStringRef, tmpBuff, cfBuffSize, kCFStringEncodingUTF8))
{
server = (char *)::calloc(1+strlen(tmpBuff),1);
::strcpy(server, tmpBuff);
}
}
}
}
if ( CFDictionaryContainsKey( serverConfigDict, CFSTR( kXMLIsSSLFlagKey ) ) )
{
cfBool= (CFBooleanRef)CFDictionaryGetValue( serverConfigDict, CFSTR( kXMLIsSSLFlagKey ) );
if (cfBool != nil)
{
bIsSSL = CFBooleanGetValue( cfBool );
if (bIsSSL)
{
portNumber = 636; }
}
}
if ( CFDictionaryContainsKey( serverConfigDict, CFSTR( kXMLPortNumberKey ) ) )
{
cfNumber = (CFNumberRef)CFDictionaryGetValue( serverConfigDict, CFSTR( kXMLPortNumberKey ) );
if ( cfNumber != nil )
{
cfNumBool = CFNumberGetValue(cfNumber, kCFNumberIntType, &portNumber);
}
}
if ( CFDictionaryContainsKey( serverConfigDict, CFSTR( kXMLMapSearchBase ) ) )
{
cfStringRef = (CFStringRef)CFDictionaryGetValue( serverConfigDict, CFSTR( kXMLMapSearchBase ) );
if ( cfStringRef != nil )
{
if ( CFGetTypeID( cfStringRef ) == CFStringGetTypeID() )
{
::memset(tmpBuff,0,1024);
if (CFStringGetCString(cfStringRef, tmpBuff, cfBuffSize, kCFStringEncodingUTF8))
{
mapSearchBase = (char *)::calloc(1+strlen(tmpBuff),1);
::strcpy(mapSearchBase, tmpBuff);
}
}
}
}
}
}
free( tmpBuff );
tmpBuff = nil;
} }
free( configVersion );
configVersion = nil;
}
}
CFRelease(configPropertyList); configPropertyList = nil;
}
outMappings = RetrieveServerMappings( server, mapSearchBase, portNumber, bIsSSL, true );
}
} catch ( sInt32 err )
{
siResult = err;
if (configPropertyList != nil)
{
CFRelease(configPropertyList); configPropertyList = nil;
}
}
if ( server != nil )
{
free( server );
server = nil;
}
if ( mapSearchBase != nil )
{
free( mapSearchBase );
mapSearchBase = nil;
}
return( outMappings );
}
CFDataRef CLDAPv3Configs::VerifyAndUpdateServerLocation( char *inServer, int inPortNumber, bool inIsSSL, bool inMakeDefLDAP, CFDataRef inXMLData )
{
CFStringRef errorString = nil;
CFPropertyListRef configPropertyList = nil;
CFMutableDictionaryRef serverConfigDict = nil;
char *configVersion = nil;
char *server = nil;
int portNumber = 389;
bool bIsSSL = false;
char *tmpBuff = nil;
CFStringRef cfStringRef = nil;
bool bUpdate = false;
CFBooleanRef cfBool = false;
CFNumberRef cfNumber = 0;
CFIndex cfBuffSize = 1024;
unsigned char cfNumBool = false;
CFDataRef outXMLData = nil;
bool bIsSrvrMappings = false;
bool bIsDefLDAP = false;
if (inXMLData != nil)
{
configPropertyList = CFPropertyListCreateFromXMLData( kCFAllocatorDefault,
inXMLData,
kCFPropertyListMutableContainers,
&errorString);
if (configPropertyList != nil )
{
if ( CFDictionaryGetTypeID() == CFGetTypeID( configPropertyList ) )
{
serverConfigDict = (CFMutableDictionaryRef) configPropertyList; }
if (serverConfigDict != nil)
{
configVersion = GetVersion(serverConfigDict);
if ( configVersion == nil )
{
CFRelease(configPropertyList); configPropertyList = nil;
return nil;
}
else
{
DBGLOG( kLogPlugin, "Have successfully read the LDAP XML config data" );
if (strcmp(configVersion,"DSLDAPv3PlugIn Version 1.5") == 0)
{
if ( CFDictionaryContainsKey( serverConfigDict, CFSTR( kXMLServerKey ) ) )
{
cfStringRef = (CFStringRef)CFDictionaryGetValue( serverConfigDict, CFSTR( kXMLServerKey ) );
if ( cfStringRef != nil )
{
if ( CFGetTypeID( cfStringRef ) == CFStringGetTypeID() )
{
tmpBuff = (char *)::calloc(1, 1024);
if (CFStringGetCString(cfStringRef, tmpBuff, cfBuffSize, kCFStringEncodingUTF8))
{
server = (char *)::calloc(1+strlen(tmpBuff),1);
::strcpy(server, tmpBuff);
if (strcmp(server,inServer) != 0)
{
bUpdate = true;
cfStringRef = CFStringCreateWithCString(kCFAllocatorDefault, inServer, kCFStringEncodingUTF8);
CFDictionaryReplaceValue(serverConfigDict, CFSTR( kXMLServerKey ), cfStringRef);
CFRelease(cfStringRef);
cfStringRef = nil;
}
free(server);
server = nil;
}
free(tmpBuff);
tmpBuff = nil;
}
}
}
if ( CFDictionaryContainsKey( serverConfigDict, CFSTR( kXMLPortNumberKey ) ) )
{
cfNumber = (CFNumberRef)CFDictionaryGetValue( serverConfigDict, CFSTR( kXMLPortNumberKey ) );
if ( cfNumber != nil )
{
cfNumBool = CFNumberGetValue(cfNumber, kCFNumberIntType, &portNumber);
if (portNumber != inPortNumber)
{
bUpdate = true;
cfNumber = CFNumberCreate(NULL,kCFNumberIntType,&inPortNumber);
CFDictionaryReplaceValue(serverConfigDict, CFSTR( kXMLPortNumberKey ), cfNumber);
CFRelease(cfNumber);
cfNumber = 0;
}
}
}
if ( CFDictionaryContainsKey( serverConfigDict, CFSTR( kXMLIsSSLFlagKey ) ) )
{
cfBool= (CFBooleanRef)CFDictionaryGetValue( serverConfigDict, CFSTR( kXMLIsSSLFlagKey ) );
if (cfBool != nil)
{
bIsSSL = CFBooleanGetValue( cfBool );
if (bIsSSL != inIsSSL)
{
bUpdate = true;
if (inIsSSL)
{
cfBool = kCFBooleanTrue;
}
else
{
cfBool = kCFBooleanFalse;
}
CFDictionaryReplaceValue(serverConfigDict, CFSTR( kXMLIsSSLFlagKey ), cfBool);
}
}
}
if ( CFDictionaryContainsKey( serverConfigDict, CFSTR( kXMLServerMappingsFlagKey ) ) )
{
cfBool= (CFBooleanRef)CFDictionaryGetValue( serverConfigDict, CFSTR( kXMLServerMappingsFlagKey ) );
if (cfBool != nil)
{
bIsSrvrMappings = CFBooleanGetValue( cfBool );
if (!bIsSrvrMappings)
{
bUpdate = true;
CFDictionaryReplaceValue(serverConfigDict, CFSTR( kXMLServerMappingsFlagKey ), kCFBooleanTrue);
}
}
}
else
{
bUpdate = true;
CFDictionarySetValue(serverConfigDict, CFSTR( kXMLServerMappingsFlagKey ), kCFBooleanTrue);
}
if ( CFDictionaryContainsKey( serverConfigDict, CFSTR( kXMLMakeDefLDAPFlagKey ) ) )
{
cfBool = (CFBooleanRef)CFDictionaryGetValue( serverConfigDict, CFSTR( kXMLMakeDefLDAPFlagKey ) );
if (cfBool != nil)
{
bIsDefLDAP = CFBooleanGetValue( cfBool );
if (!bIsDefLDAP && inMakeDefLDAP)
{
bUpdate = true;
CFDictionaryReplaceValue(serverConfigDict, CFSTR( kXMLMakeDefLDAPFlagKey ), kCFBooleanTrue);
}
else if (bIsDefLDAP && !inMakeDefLDAP)
{
bUpdate = true;
CFDictionaryReplaceValue(serverConfigDict, CFSTR( kXMLMakeDefLDAPFlagKey ), kCFBooleanFalse);
}
}
}
else
{
bUpdate = true;
if (inMakeDefLDAP)
{
CFDictionarySetValue(serverConfigDict, CFSTR( kXMLMakeDefLDAPFlagKey ), kCFBooleanTrue);
}
else
{
CFDictionarySetValue(serverConfigDict, CFSTR( kXMLMakeDefLDAPFlagKey ), kCFBooleanFalse);
}
}
if (bUpdate)
{
outXMLData = CFPropertyListCreateXMLData( kCFAllocatorDefault, serverConfigDict);
}
}
delete(configVersion);
} }
CFRelease(configPropertyList); configPropertyList = nil;
} }
return( outXMLData );
}
sInt32 CLDAPv3Configs::AddLDAPServer( CFDataRef inXMLData )
{
sInt32 siResult = eDSNoErr;
CFStringRef errorString = nil;
CFPropertyListRef configPropertyList = nil;
CFMutableDictionaryRef serverConfigDict = nil;
char *configVersion = nil;
try
{
if (inXMLData != nil)
{
configPropertyList = CFPropertyListCreateFromXMLData( kCFAllocatorDefault,
inXMLData,
kCFPropertyListMutableContainers,
&errorString);
if (configPropertyList != nil )
{
if ( CFDictionaryGetTypeID() == CFGetTypeID( configPropertyList ) )
{
serverConfigDict = (CFMutableDictionaryRef) configPropertyList; }
if (serverConfigDict != nil)
{
configVersion = GetVersion(serverConfigDict);
if ( configVersion == nil )
{
syslog(LOG_INFO,"DSLDAPv3PlugIn: Obtained LDAP server mappings is missing the version string.");
throw( (sInt32)eDSVersionMismatch ); }
if (configVersion != nil)
{
DBGLOG( kLogPlugin, "Have successfully read the LDAP XML config data" );
if (strcmp(configVersion,"DSLDAPv3PlugIn Version 1.5") == 0)
{
siResult = MakeLDAPConfig(serverConfigDict, fConfigTableLen, true);
}
else
{
syslog(LOG_INFO,"DSLDAPv3PlugIn: Obtained LDAP server mappings contain incorrect version string [%s] instead of [DSLDAPv3PlugIn Version 1.5].", configVersion);
}
delete(configVersion);
}
}
CFRelease(configPropertyList); configPropertyList = nil;
} }
} catch ( sInt32 err )
{
siResult = err;
if (configPropertyList != nil)
{
CFRelease(configPropertyList); configPropertyList = nil;
}
}
return( siResult );
}
CFDictionaryRef CLDAPv3Configs::CheckForServerMappings ( CFDictionaryRef ldapDict )
{
char *tmpBuff = nil;
CFIndex cfBuffSize = 1024;
CFStringRef cfStringRef = nil;
CFBooleanRef cfBool = false;
unsigned char cfNumBool = false;
CFNumberRef cfNumber = 0;
char *server = nil;
char *mapSearchBase = nil;
int portNumber = 389;
bool bIsSSL = false;
bool bServerMappings = false;
bool bUseConfig = false;
bool bReferrals = true; CFDictionaryRef outDict = nil;
CFStringRef errorString;
if ( CFDictionaryContainsKey( ldapDict, CFSTR( kXMLEnableUseFlagKey ) ) )
{
tmpBuff = (char *)::calloc(1, 1024);
cfBool = (CFBooleanRef)CFDictionaryGetValue( ldapDict, CFSTR( kXMLEnableUseFlagKey ) );
if (cfBool != nil)
{
bUseConfig = CFBooleanGetValue( cfBool );
}
if ( bUseConfig )
{
if ( CFDictionaryContainsKey( ldapDict, CFSTR( kXMLServerMappingsFlagKey ) ) )
{
cfBool = (CFBooleanRef)CFDictionaryGetValue( ldapDict, CFSTR( kXMLServerMappingsFlagKey ) );
if (cfBool != nil)
{
bServerMappings = CFBooleanGetValue( cfBool );
}
}
if (bServerMappings)
{
if ( CFDictionaryContainsKey( ldapDict, CFSTR( kXMLServerKey ) ) )
{
cfStringRef = (CFStringRef)CFDictionaryGetValue( ldapDict, CFSTR( kXMLServerKey ) );
if ( cfStringRef != nil )
{
if ( CFGetTypeID( cfStringRef ) == CFStringGetTypeID() )
{
::memset(tmpBuff,0,1024);
if (CFStringGetCString(cfStringRef, tmpBuff, cfBuffSize, kCFStringEncodingUTF8))
{
server = (char *)::calloc(1+strlen(tmpBuff),1);
::strcpy(server, tmpBuff);
}
}
}
}
if ( CFDictionaryContainsKey( ldapDict, CFSTR( kXMLIsSSLFlagKey ) ) )
{
cfBool= (CFBooleanRef)CFDictionaryGetValue( ldapDict, CFSTR( kXMLIsSSLFlagKey ) );
if (cfBool != nil)
{
bIsSSL = CFBooleanGetValue( cfBool );
if (bIsSSL)
{
portNumber = 636; }
}
}
if ( CFDictionaryContainsKey( ldapDict, CFSTR( kXMLPortNumberKey ) ) )
{
cfNumber = (CFNumberRef)CFDictionaryGetValue( ldapDict, CFSTR( kXMLPortNumberKey ) );
if ( cfNumber != nil )
{
cfNumBool = CFNumberGetValue(cfNumber, kCFNumberIntType, &portNumber);
}
}
if ( CFDictionaryContainsKey( ldapDict, CFSTR( kXMLMapSearchBase ) ) )
{
cfStringRef = (CFStringRef)CFDictionaryGetValue( ldapDict, CFSTR( kXMLMapSearchBase ) );
if ( cfStringRef != nil )
{
if ( CFGetTypeID( cfStringRef ) == CFStringGetTypeID() )
{
::memset(tmpBuff,0,1024);
if (CFStringGetCString(cfStringRef, tmpBuff, cfBuffSize, kCFStringEncodingUTF8))
{
mapSearchBase = (char *)::calloc(1+strlen(tmpBuff),1);
::strcpy(mapSearchBase, tmpBuff);
}
}
}
}
if( cfBool = (CFBooleanRef)CFDictionaryGetValue( ldapDict, CFSTR(kXMLReferralFlagKey) ) )
{
if( CFGetTypeID( cfBool ) == CFBooleanGetTypeID() )
{
bReferrals = CFBooleanGetValue( cfBool );
}
}
CFDataRef ourXMLData = nil;
ourXMLData = RetrieveServerMappings( server, mapSearchBase, portNumber, bIsSSL, bReferrals );
if (ourXMLData != nil)
{
CFPropertyListRef configPropertyList = nil;
configPropertyList = CFPropertyListCreateFromXMLData( kCFAllocatorDefault,
ourXMLData,
kCFPropertyListImmutable,
&errorString);
if (configPropertyList != nil )
{
if ( CFDictionaryGetTypeID() == CFGetTypeID( configPropertyList ) )
{
outDict = (CFDictionaryRef) configPropertyList;
}
}
CFRelease(ourXMLData);
ourXMLData = nil;
}
}
if ( server != nil )
{
free( server );
server = nil;
}
if ( mapSearchBase != nil )
{
free( mapSearchBase );
mapSearchBase = nil;
}
}
delete( tmpBuff );
}
return( outDict );
}
sInt32 CLDAPv3Configs::MakeLDAPConfig( CFDictionaryRef ldapDict, sInt32 inIndex, bool inEnsureServerMappings )
{
sInt32 siResult = eDSNoErr;
char *tmpBuff = nil;
CFIndex cfBuffSize = 1024;
CFStringRef cfStringRef = nil;
CFDataRef cfDataRef = nil;
CFBooleanRef cfBool = false;
unsigned char cfNumBool = false;
CFNumberRef cfNumber = 0;
char *uiName = nil;
char *server = nil;
char *account = nil;
char *mapSearchBase = nil;
char *password = nil;
int passwordLen = 0;
int opencloseTO = 15;
int idleTO = 2;
int delayRebindTry = 120;
int searchTO = 120;
int portNumber = 389;
bool bIsSSL = false;
bool bServerMappings = false;
bool bMakeDefLDAP= false;
bool bUseSecure = false;
bool bUseConfig = false;
bool bReferrals = true; sLDAPConfigData *pConfig = nil;
sLDAPConfigData *xConfig = nil;
uInt32 serverIndex = 0;
bool reuseEntry = false;
if ( CFDictionaryContainsKey( ldapDict, CFSTR( kXMLEnableUseFlagKey ) ) )
{
tmpBuff = (char *)::calloc(1, 1024);
cfBool = (CFBooleanRef)CFDictionaryGetValue( ldapDict, CFSTR( kXMLEnableUseFlagKey ) );
if (cfBool != nil)
{
bUseConfig = CFBooleanGetValue( cfBool );
}
if ( bUseConfig )
{
if ( CFDictionaryContainsKey( ldapDict, CFSTR( kXMLServerKey ) ) )
{
cfStringRef = (CFStringRef)CFDictionaryGetValue( ldapDict, CFSTR( kXMLServerKey ) );
if ( cfStringRef != nil )
{
if ( CFGetTypeID( cfStringRef ) == CFStringGetTypeID() )
{
::memset(tmpBuff,0,1024);
if (CFStringGetCString(cfStringRef, tmpBuff, cfBuffSize, kCFStringEncodingUTF8))
{
server = (char *)::calloc(1+strlen(tmpBuff),1);
::strcpy(server, tmpBuff);
}
}
}
}
if (CheckForConfig(server, serverIndex))
{
reuseEntry = true;
xConfig = (sLDAPConfigData *)pConfigTable->GetItemData( serverIndex );
}
if ( CFDictionaryContainsKey( ldapDict, CFSTR( kXMLIsSSLFlagKey ) ) )
{
cfBool= (CFBooleanRef)CFDictionaryGetValue( ldapDict, CFSTR( kXMLIsSSLFlagKey ) );
if (cfBool != nil)
{
bIsSSL = CFBooleanGetValue( cfBool );
if (bIsSSL)
{
portNumber = 636; }
}
}
if ( CFDictionaryContainsKey( ldapDict, CFSTR( kXMLPortNumberKey ) ) )
{
cfNumber = (CFNumberRef)CFDictionaryGetValue( ldapDict, CFSTR( kXMLPortNumberKey ) );
if ( cfNumber != nil )
{
cfNumBool = CFNumberGetValue(cfNumber, kCFNumberIntType, &portNumber);
}
}
if (inEnsureServerMappings)
{
bServerMappings = true;
}
else
{
if ( CFDictionaryContainsKey( ldapDict, CFSTR( kXMLServerMappingsFlagKey ) ) )
{
cfBool = (CFBooleanRef)CFDictionaryGetValue( ldapDict, CFSTR( kXMLServerMappingsFlagKey ) );
if (cfBool != nil)
{
bServerMappings = CFBooleanGetValue( cfBool );
}
}
}
if ( CFDictionaryContainsKey( ldapDict, CFSTR( kXMLOpenCloseTimeoutSecsKey ) ) )
{
cfNumber = (CFNumberRef)CFDictionaryGetValue( ldapDict, CFSTR( kXMLOpenCloseTimeoutSecsKey ) );
if ( cfNumber != nil )
{
cfNumBool = CFNumberGetValue(cfNumber, kCFNumberIntType, &opencloseTO);
}
}
if ( CFDictionaryContainsKey( ldapDict, CFSTR( kXMLIdleTimeoutMinsKey ) ) )
{
cfNumber = (CFNumberRef)CFDictionaryGetValue( ldapDict, CFSTR( kXMLIdleTimeoutMinsKey ) );
if ( cfNumber != nil )
{
cfNumBool = CFNumberGetValue(cfNumber, kCFNumberIntType, &idleTO);
}
}
if ( CFDictionaryContainsKey( ldapDict, CFSTR( kXMLDelayedRebindTrySecsKey ) ) )
{
cfNumber = (CFNumberRef)CFDictionaryGetValue( ldapDict, CFSTR( kXMLDelayedRebindTrySecsKey ) );
if ( cfNumber != nil )
{
cfNumBool = CFNumberGetValue(cfNumber, kCFNumberIntType, &delayRebindTry);
}
}
if ( CFDictionaryContainsKey( ldapDict, CFSTR( kXMLSearchTimeoutSecsKey ) ) )
{
cfNumber = (CFNumberRef)CFDictionaryGetValue( ldapDict, CFSTR( kXMLSearchTimeoutSecsKey ) );
if ( cfNumber != nil )
{
cfNumBool = CFNumberGetValue(cfNumber, kCFNumberIntType, &searchTO);
}
}
if ( CFDictionaryContainsKey( ldapDict, CFSTR( kXMLSecureUseFlagKey ) ) )
{
cfBool= (CFBooleanRef)CFDictionaryGetValue( ldapDict, CFSTR( kXMLSecureUseFlagKey ) );
if (cfBool != nil)
{
bUseSecure = CFBooleanGetValue( cfBool );
}
}
if ( CFDictionaryContainsKey( ldapDict, CFSTR( kXMLUserDefinedNameKey ) ) )
{
cfStringRef = (CFStringRef)CFDictionaryGetValue( ldapDict, CFSTR( kXMLUserDefinedNameKey ) );
if ( cfStringRef != nil )
{
if ( CFGetTypeID( cfStringRef ) == CFStringGetTypeID() )
{
::memset(tmpBuff,0,1024);
if (CFStringGetCString(cfStringRef, tmpBuff, cfBuffSize, kCFStringEncodingUTF8))
{
uiName = (char *)::calloc(1+strlen(tmpBuff),1);
::strcpy(uiName, tmpBuff);
}
}
}
}
if ( CFDictionaryContainsKey( ldapDict, CFSTR( kXMLServerAccountKey ) ) )
{
cfStringRef = (CFStringRef)CFDictionaryGetValue( ldapDict, CFSTR( kXMLServerAccountKey ) );
if ( cfStringRef != nil )
{
if ( CFGetTypeID( cfStringRef ) == CFStringGetTypeID() )
{
::memset(tmpBuff,0,1024);
if (CFStringGetCString(cfStringRef, tmpBuff, cfBuffSize, kCFStringEncodingUTF8))
{
account = (char *)::calloc(1+strlen(tmpBuff),1);
::strcpy(account, tmpBuff);
}
}
}
}
if ( CFDictionaryContainsKey( ldapDict, CFSTR( kXMLServerPasswordKey ) ) )
{
cfStringRef = (CFStringRef)CFDictionaryGetValue( ldapDict, CFSTR( kXMLServerPasswordKey ) );
if ( cfStringRef != nil )
{
if ( CFGetTypeID( cfStringRef ) == CFDataGetTypeID() )
{
cfDataRef = (CFDataRef)cfStringRef;
passwordLen = CFDataGetLength(cfDataRef);
password = (char*)::calloc(1+passwordLen,1);
CFDataGetBytes(cfDataRef, CFRangeMake(0,passwordLen), (UInt8*)password);
}
else if ( CFGetTypeID( cfStringRef ) == CFStringGetTypeID() )
{
::memset(tmpBuff,0,1024);
if (CFStringGetCString(cfStringRef, tmpBuff, cfBuffSize, kCFStringEncodingUTF8))
{
password = (char *)::calloc(1+strlen(tmpBuff),1);
::strcpy(password, tmpBuff);
}
}
}
}
if ( CFDictionaryContainsKey( ldapDict, CFSTR( kXMLMakeDefLDAPFlagKey ) ) )
{
cfBool = (CFBooleanRef)CFDictionaryGetValue( ldapDict, CFSTR( kXMLMakeDefLDAPFlagKey ) );
if (cfBool != nil)
{
bMakeDefLDAP = CFBooleanGetValue( cfBool );
}
}
if ( CFDictionaryContainsKey( ldapDict, CFSTR( kXMLMapSearchBase ) ) )
{
cfStringRef = (CFStringRef)CFDictionaryGetValue( ldapDict, CFSTR( kXMLMapSearchBase ) );
if ( cfStringRef != nil )
{
if ( CFGetTypeID( cfStringRef ) == CFStringGetTypeID() )
{
::memset(tmpBuff,0,1024);
if (CFStringGetCString(cfStringRef, tmpBuff, cfBuffSize, kCFStringEncodingUTF8))
{
mapSearchBase = (char *)::calloc(1+strlen(tmpBuff),1);
::strcpy(mapSearchBase, tmpBuff);
}
}
}
}
if( cfBool = (CFBooleanRef) CFDictionaryGetValue( ldapDict, CFSTR(kXMLReferralFlagKey) ) )
{
if( CFGetTypeID(cfBool) == CFBooleanGetTypeID() )
{
bReferrals = CFBooleanGetValue( cfBool );
}
}
if (reuseEntry)
{
pConfig = MakeLDAPConfigData( uiName, server, opencloseTO, idleTO, delayRebindTry, searchTO, portNumber, bUseSecure, account, password, bMakeDefLDAP, bServerMappings, bIsSSL, mapSearchBase, bReferrals, xConfig );
}
else
{
pConfig = MakeLDAPConfigData( uiName, server, opencloseTO, idleTO, delayRebindTry, searchTO, portNumber, bUseSecure, account, password, bMakeDefLDAP, bServerMappings, bIsSSL, mapSearchBase, bReferrals, nil );
}
BuildLDAPMap( pConfig, ldapDict, bServerMappings );
if ( uiName != nil )
{
free( uiName );
uiName = nil;
}
if ( server != nil )
{
free( server );
server = nil;
}
if ( account != nil )
{
free( account );
account = nil;
}
if ( password != nil )
{
free( password );
password = nil;
}
if ( mapSearchBase != nil )
{
free( mapSearchBase );
mapSearchBase = nil;
}
if (reuseEntry)
{
}
else
{
pConfigTable->AddItem( inIndex, pConfig );
fConfigTableLen++;
}
}
delete( tmpBuff );
}
return( siResult );
}
sInt32 CLDAPv3Configs::MakeServerBasedMappingsLDAPConfig ( char *inServer, char *inMapSearchBase, int inOpenCloseTO, int inIdleTO, int inDelayRebindTry, int inSearchTO, int inPortNumber, bool inIsSSL, bool inMakeDefLDAP, bool inReferrals )
{
sInt32 siResult = eDSNoErr;
uInt32 serverIndex = 0;
bool reuseEntry = false;
sLDAPConfigData *xConfig = nil;
sLDAPConfigData *pConfig = nil;
if (CheckForConfig(inServer, serverIndex))
{
reuseEntry = true;
xConfig = (sLDAPConfigData *)pConfigTable->GetItemData( serverIndex );
}
if (reuseEntry)
{
pConfig = MakeLDAPConfigData( inServer, inServer, inOpenCloseTO, inIdleTO, inDelayRebindTry, inSearchTO, inPortNumber, false, nil, nil, inMakeDefLDAP, true, inIsSSL, inMapSearchBase, inReferrals, xConfig );
}
else
{
pConfig = MakeLDAPConfigData( inServer, inServer, inOpenCloseTO, inIdleTO, inDelayRebindTry, inSearchTO, inPortNumber, false, nil, nil, inMakeDefLDAP, true, inIsSSL, inMapSearchBase, inReferrals, nil );
pConfigTable->AddItem( fConfigTableLen, pConfig );
fConfigTableLen++;
}
return( siResult );
}
bool CLDAPv3Configs::CheckForConfig ( char *inServerName, uInt32 &inConfigTableIndex )
{
bool result = false;
uInt32 iTableIndex = 0;
sLDAPConfigData *pConfig = nil;
if (inServerName != nil)
{
for (iTableIndex=0; iTableIndex<fConfigTableLen; iTableIndex++)
{
pConfig = (sLDAPConfigData *)pConfigTable->GetItemData( iTableIndex );
if (pConfig != nil)
{
if (pConfig->fServerName != nil)
{
if (::strcmp(pConfig->fServerName, inServerName) == 0 )
{
result = true;
inConfigTableIndex = iTableIndex;
break;
}
}
}
}
}
return(result);
}
sInt32 CLDAPv3Configs::BuildLDAPMap ( sLDAPConfigData *inConfig, CFDictionaryRef ldapDict, bool inServerMapppings )
{
sInt32 siResult = eDSNoErr; CFArrayRef cfArrayRef = nil;
if (inServerMapppings)
{
cfArrayRef = nil;
cfArrayRef = GetRecordTypeMapArray(ldapDict);
if ( (cfArrayRef != nil) && (CFGetTypeID( cfArrayRef ) == CFArrayGetTypeID()) && (CFArrayGetCount(cfArrayRef) > 0) )
{
if (inConfig->fRecordTypeMapCFArray != nil)
{
CFRelease(inConfig->fRecordTypeMapCFArray);
inConfig->fRecordTypeMapCFArray = 0;
}
inConfig->fRecordTypeMapCFArray = CFArrayCreateCopy(kCFAllocatorDefault, cfArrayRef);
}
cfArrayRef = nil;
cfArrayRef = GetAttributeTypeMapArray(ldapDict);
if ( (cfArrayRef != nil) && (CFGetTypeID( cfArrayRef ) == CFArrayGetTypeID()) && (CFArrayGetCount(cfArrayRef) > 0) )
{
if (inConfig->fAttrTypeMapCFArray != nil)
{
CFRelease(inConfig->fAttrTypeMapCFArray);
inConfig->fAttrTypeMapCFArray = 0;
}
inConfig->fAttrTypeMapCFArray = CFArrayCreateCopy(kCFAllocatorDefault, cfArrayRef);
}
}
else
{
cfArrayRef = nil;
cfArrayRef = GetRecordTypeMapArray(ldapDict);
if ( (cfArrayRef != nil) && (CFGetTypeID( cfArrayRef ) == CFArrayGetTypeID()) && (CFArrayGetCount(cfArrayRef) > 0) )
{
if( inConfig->fRecordTypeMapCFArray )
{
CFRelease( inConfig->fRecordTypeMapCFArray );
inConfig->fRecordTypeMapCFArray = NULL;
}
inConfig->fRecordTypeMapCFArray = CFArrayCreateCopy(kCFAllocatorDefault, cfArrayRef);
}
cfArrayRef = nil;
cfArrayRef = GetAttributeTypeMapArray(ldapDict);
if ( (cfArrayRef != nil) && (CFGetTypeID( cfArrayRef ) == CFArrayGetTypeID()) && (CFArrayGetCount(cfArrayRef) > 0) )
{
if( inConfig->fAttrTypeMapCFArray )
{
CFRelease( inConfig->fAttrTypeMapCFArray );
inConfig->fAttrTypeMapCFArray = NULL;
}
inConfig->fAttrTypeMapCFArray = CFArrayCreateCopy(kCFAllocatorDefault, cfArrayRef);
}
}
cfArrayRef = nil;
cfArrayRef = GetReplicaHostnameListArray(ldapDict);
if ( (cfArrayRef != nil) && (CFGetTypeID( cfArrayRef ) == CFArrayGetTypeID()) && (CFArrayGetCount(cfArrayRef) > 0) )
{
if (inConfig->fReplicaHostnames != nil)
{
CFRelease(inConfig->fReplicaHostnames);
inConfig->fReplicaHostnames = NULL;
}
inConfig->fReplicaHostnames = CFArrayCreateMutableCopy(kCFAllocatorDefault, NULL, cfArrayRef);
}
cfArrayRef = nil;
cfArrayRef = GetWriteableHostnameListArray(ldapDict);
if ( (cfArrayRef != nil) && (CFGetTypeID( cfArrayRef ) == CFArrayGetTypeID()) && (CFArrayGetCount(cfArrayRef) > 0) )
{
if (inConfig->fWriteableHostnames != nil)
{
CFRelease(inConfig->fWriteableHostnames);
inConfig->fWriteableHostnames = NULL;
}
inConfig->fWriteableHostnames = CFArrayCreateMutableCopy(kCFAllocatorDefault, NULL, cfArrayRef);
}
return( siResult );
}
char *CLDAPv3Configs::GetVersion ( CFDictionaryRef configDict )
{
char *outVersion = nil;
CFStringRef cfStringRef = nil;
char *tmpBuff = nil;
CFIndex cfBuffSize = 1024;
if ( CFDictionaryContainsKey( configDict, CFSTR( kXMLLDAPVersionKey ) ) )
{
cfStringRef = (CFStringRef)CFDictionaryGetValue( configDict, CFSTR( kXMLLDAPVersionKey ) );
if ( cfStringRef != nil )
{
if ( CFGetTypeID( cfStringRef ) == CFStringGetTypeID() )
{
tmpBuff = new char[1024];
::memset(tmpBuff,0,1024);
if (CFStringGetCString(cfStringRef, tmpBuff, cfBuffSize, kCFStringEncodingUTF8))
{
outVersion = new char[1+strlen(tmpBuff)];
::strcpy(outVersion, tmpBuff);
}
delete( tmpBuff );
}
}
}
return( outVersion );
}
CFArrayRef CLDAPv3Configs::GetConfigArray ( CFDictionaryRef configDict )
{
CFArrayRef cfArrayRef = nil;
if ( CFDictionaryContainsKey( configDict, CFSTR( kXMLConfigArrayKey ) ) )
{
cfArrayRef = (CFArrayRef)CFDictionaryGetValue( configDict, CFSTR( kXMLConfigArrayKey ) );
}
return( cfArrayRef );
}
CFArrayRef CLDAPv3Configs::GetDefaultRecordTypeMapArray ( CFDictionaryRef configDict )
{
CFArrayRef cfArrayRef = nil;
if ( CFDictionaryContainsKey( configDict, CFSTR( kXMLDefaultRecordTypeMapArrayKey ) ) )
{
cfArrayRef = (CFArrayRef)CFDictionaryGetValue( configDict, CFSTR( kXMLDefaultRecordTypeMapArrayKey ) );
}
return( cfArrayRef );
}
CFArrayRef CLDAPv3Configs::GetDefaultAttrTypeMapArray ( CFDictionaryRef configDict )
{
CFArrayRef cfArrayRef = nil;
if ( CFDictionaryContainsKey( configDict, CFSTR( kXMLDefaultAttrTypeMapArrayKey ) ) )
{
cfArrayRef = (CFArrayRef)CFDictionaryGetValue( configDict, CFSTR( kXMLDefaultAttrTypeMapArrayKey ) );
}
return( cfArrayRef );
}
CFArrayRef CLDAPv3Configs::GetReplicaHostnameListArray ( CFDictionaryRef configDict )
{
CFArrayRef cfArrayRef = nil;
if ( CFDictionaryContainsKey( configDict, CFSTR( kXMLReplicaHostnameListArrayKey ) ) )
{
cfArrayRef = (CFArrayRef)CFDictionaryGetValue( configDict, CFSTR( kXMLReplicaHostnameListArrayKey ) );
}
return( cfArrayRef );
}
CFArrayRef CLDAPv3Configs::GetWriteableHostnameListArray ( CFDictionaryRef configDict )
{
CFArrayRef cfArrayRef = nil;
if ( CFDictionaryContainsKey( configDict, CFSTR( kXMLWriteableHostnameListArrayKey ) ) )
{
cfArrayRef = (CFArrayRef)CFDictionaryGetValue( configDict, CFSTR( kXMLWriteableHostnameListArrayKey ) );
}
return( cfArrayRef );
}
CFArrayRef CLDAPv3Configs::GetRecordTypeMapArray ( CFDictionaryRef configDict )
{
CFArrayRef cfArrayRef = nil;
if ( CFDictionaryContainsKey( configDict, CFSTR( kXMLRecordTypeMapArrayKey ) ) )
{
cfArrayRef = (CFArrayRef)CFDictionaryGetValue( configDict, CFSTR( kXMLRecordTypeMapArrayKey ) );
}
return( cfArrayRef );
}
CFArrayRef CLDAPv3Configs::GetAttributeTypeMapArray ( CFDictionaryRef configDict )
{
CFArrayRef cfArrayRef = nil;
if ( CFDictionaryContainsKey( configDict, CFSTR( kXMLAttrTypeMapArrayKey ) ) )
{
cfArrayRef = (CFArrayRef)CFDictionaryGetValue( configDict, CFSTR( kXMLAttrTypeMapArrayKey ) );
}
return( cfArrayRef );
}
CFArrayRef CLDAPv3Configs::GetNativeTypeMapArray ( CFDictionaryRef configDict )
{
CFArrayRef cfArrayRef = nil;
if ( CFDictionaryContainsKey( configDict, CFSTR( kXMLNativeMapArrayKey ) ) )
{
cfArrayRef = (CFArrayRef)CFDictionaryGetValue( configDict, CFSTR( kXMLNativeMapArrayKey ) );
}
return( cfArrayRef );
}
sLDAPConfigData *CLDAPv3Configs::MakeLDAPConfigData ( char *inName, char *inServerName,
int inOpenCloseTO, int inIdleTO, int inDelayRebindTry,
int inSearchTO, int inPortNum,
bool inUseSecure,
char *inAccount, char *inPassword,
bool inMakeDefLDAP,
bool inServerMappings,
bool inIsSSL,
char *inMapSearchBase,
bool inReferrals,
sLDAPConfigData *inLDAPConfigData )
{
sInt32 siResult = eDSNoErr;
sLDAPConfigData *configOut = nil;
sReplicaInfo *replicaHosts = nil;
CFMutableArrayRef replicaHostNames = nil;
CFMutableArrayRef writeableHosts = nil;
CFMutableArrayRef saslMethods = nil;
if (inServerName != nil)
{
if (inLDAPConfigData != nil)
{
configOut = inLDAPConfigData;
replicaHosts = inLDAPConfigData->fReplicaHosts;
inLDAPConfigData->fReplicaHosts = nil;
replicaHostNames = inLDAPConfigData->fReplicaHostnames;
inLDAPConfigData->fReplicaHostnames = nil;
writeableHosts = inLDAPConfigData->fWriteableHostnames;
inLDAPConfigData->fWriteableHostnames = nil;
saslMethods = inLDAPConfigData->fSASLmethods;
inLDAPConfigData->fSASLmethods = nil;
}
else
{
configOut = (sLDAPConfigData *) calloc(1, sizeof(sLDAPConfigData));
}
if ( configOut != nil )
{
siResult = CleanLDAPConfigData(configOut, inServerMappings);
if (inName != nil)
{
configOut->fName = new char[1+::strlen( inName )];
::strcpy(configOut->fName, inName);
}
configOut->fServerName = new char[1+::strlen( inServerName )];
::strcpy(configOut->fServerName, inServerName);
configOut->fReplicaHosts = replicaHosts;
configOut->fReplicaHostnames = replicaHostNames;
configOut->fWriteableHostnames = writeableHosts;
configOut->fOpenCloseTimeout = inOpenCloseTO;
configOut->fIdleTimeout = inIdleTO;
configOut->fDelayRebindTry = inDelayRebindTry;
configOut->fSearchTimeout = inSearchTO;
configOut->fServerPort = inPortNum;
configOut->bSecureUse = inUseSecure;
configOut->bUpdated = true;
configOut->bUseAsDefaultLDAP = inMakeDefLDAP;
configOut->bServerMappings = inServerMappings;
configOut->bIsSSL = inIsSSL;
configOut->fSASLmethods = saslMethods;
configOut->bReferrals = inReferrals;
if (inAccount != nil)
{
configOut->fServerAccount = new char[1+::strlen( inAccount )];
::strcpy(configOut->fServerAccount, inAccount);
}
if (inPassword != nil)
{
configOut->fServerPassword = new char[1+::strlen( inPassword )];
::strcpy(configOut->fServerPassword, inPassword);
}
if (inMapSearchBase != nil)
{
configOut->fMapSearchBase = strdup(inMapSearchBase);
if ( inServerMappings )
{
configOut->bGetServerMappings = true;
}
}
configOut->bBuildReplicaList = true;
}
}
return( configOut );
}
sInt32 CLDAPv3Configs::CleanLDAPConfigData ( sLDAPConfigData *inConfig, bool inServerMappings )
{
sInt32 siResult = eDSNoErr;
sReplicaInfo *repIter = nil;
if ( inConfig == nil )
{
siResult = eDSBadContextData; }
else
{
if (inConfig->fName != nil)
{
free( inConfig->fName );
}
if (inConfig->fServerName != nil)
{
free( inConfig->fServerName );
}
if (inConfig->fServerAccount != nil)
{
free( inConfig->fServerAccount );
}
if (inConfig->fMapSearchBase != nil)
{
free( inConfig->fMapSearchBase );
}
if (inConfig->fServerPassword != nil)
{
free( inConfig->fServerPassword );
}
inConfig->fName = nil;
inConfig->fServerName = nil;
inConfig->fServerAccount = nil;
inConfig->fServerPassword = nil;
inConfig->fMapSearchBase = nil;
if (!inServerMappings) {
if (inConfig->fRecordTypeMapCFArray != 0)
{
CFRelease(inConfig->fRecordTypeMapCFArray);
inConfig->fRecordTypeMapCFArray = 0;
}
if (inConfig->fAttrTypeMapCFArray != 0)
{
CFRelease(inConfig->fAttrTypeMapCFArray);
inConfig->fAttrTypeMapCFArray = 0;
}
}
if (inConfig->fReplicaHostnames != 0)
{
CFRelease(inConfig->fReplicaHostnames);
inConfig->fReplicaHostnames = 0;
}
if (inConfig->fWriteableHostnames != 0)
{
CFRelease(inConfig->fWriteableHostnames);
inConfig->fWriteableHostnames = 0;
}
if (inConfig->fReplicaHosts != nil)
{
repIter = inConfig->fReplicaHosts;
while( repIter != nil)
{
inConfig->fReplicaHosts = repIter->fNext;
freeaddrinfo( repIter->fAddrInfo );
if (repIter->hostname != NULL)
{
CFRelease(repIter->hostname);
}
free(repIter);
repIter = inConfig->fReplicaHosts;
}
}
inConfig->fOpenCloseTimeout = 15;
inConfig->fIdleTimeout = 2;
inConfig->fDelayRebindTry = 120;
inConfig->fSearchTimeout = 120;
inConfig->fServerPort = 389;
inConfig->bSecureUse = false;
inConfig->bAvail = false;
inConfig->bUpdated = false;
inConfig->bUseAsDefaultLDAP = false;
inConfig->bServerMappings = false;
inConfig->bIsSSL = false;
inConfig->bOCBuilt = false;
inConfig->bGetServerMappings = false;
inConfig->bBuildReplicaList = false;
inConfig->bReferrals = true;
if( inConfig->fSASLmethods )
{
CFRelease( inConfig->fSASLmethods );
inConfig->fSASLmethods = NULL;
}
if (inConfig->fObjectClassSchema != nil)
{
for (ObjectClassMapCI iter = inConfig->fObjectClassSchema->begin(); iter != inConfig->fObjectClassSchema->end(); ++iter)
{
iter->second->fParentOCs.clear();
iter->second->fOtherNames.clear();
iter->second->fRequiredAttrs.clear();
iter->second->fAllowedAttrs.clear();
delete(iter->second);
inConfig->fObjectClassSchema->erase(iter->first);
}
inConfig->fObjectClassSchema->clear();
delete(inConfig->fObjectClassSchema);
inConfig->fObjectClassSchema = nil;
}
}
return( siResult );
}
char* CLDAPv3Configs::ExtractRecMap( const char *inRecType, CFArrayRef inRecordTypeMapCFArray, int inIndex, bool *outOCGroup, CFArrayRef *outOCListCFArray, ber_int_t* outScope )
{
char *outResult = nil;
CFIndex cfMapCount = 0;
CFIndex cfNativeMapCount = 0;
sInt32 iMapIndex = 0;
CFStringRef cfStringRef = nil;
CFStringRef cfRecTypeRef = nil;
CFBooleanRef cfBoolRef = nil;
char *tmpBuff = nil;
CFIndex cfBuffSize = 1024;
CFArrayRef cfNativeArrayRef = nil;
if ( (inRecordTypeMapCFArray != nil) && (inRecType != nil) )
{
cfRecTypeRef = CFStringCreateWithCString(kCFAllocatorDefault, inRecType, kCFStringEncodingUTF8);
cfMapCount = ::CFArrayGetCount( inRecordTypeMapCFArray );
if (cfMapCount != 0)
{
for (iMapIndex = 0; iMapIndex < cfMapCount; iMapIndex++)
{
CFDictionaryRef typeMapDict;
typeMapDict = (CFDictionaryRef)::CFArrayGetValueAtIndex( inRecordTypeMapCFArray, iMapIndex );
if ( typeMapDict != nil )
{
if ( CFDictionaryContainsKey( typeMapDict, CFSTR( kXMLStdNameKey ) ) )
{
cfStringRef = (CFStringRef)CFDictionaryGetValue( typeMapDict, CFSTR( kXMLStdNameKey ) );
if ( cfStringRef != nil )
{
if ( CFGetTypeID( cfStringRef ) == CFStringGetTypeID() )
{
if (CFStringCompare(cfStringRef, cfRecTypeRef, 0) == kCFCompareEqualTo)
{
cfNativeArrayRef = GetNativeTypeMapArray(typeMapDict);
if (cfNativeArrayRef != nil)
{
cfNativeMapCount = ::CFArrayGetCount( cfNativeArrayRef );
if (cfNativeMapCount != 0)
{
if ( (inIndex >= 1) && (inIndex <= cfNativeMapCount) )
{
tmpBuff = (char *) calloc(1, 1024);
if (CFGetTypeID(CFArrayGetValueAtIndex( cfNativeArrayRef, inIndex-1 )) == CFStringGetTypeID())
{
CFStringRef nativeMapString;
nativeMapString = (CFStringRef)::CFArrayGetValueAtIndex( cfNativeArrayRef, inIndex-1 );
if ( nativeMapString != nil )
{
if (CFStringGetCString(nativeMapString, tmpBuff, cfBuffSize, kCFStringEncodingUTF8))
{
outResult = (char *) calloc(1, 1+strlen(tmpBuff));
::strcpy(outResult, tmpBuff);
}
}
if ( outScope != nil )
{
*outScope = LDAP_SCOPE_SUBTREE;
}
} else {
CFDictionaryRef subNativeDict;
subNativeDict = (CFDictionaryRef)CFArrayGetValueAtIndex( cfNativeArrayRef, inIndex-1 );
if (subNativeDict != nil)
{
if ( CFGetTypeID( subNativeDict ) == CFDictionaryGetTypeID() )
{
CFStringRef searchBase;
searchBase = (CFStringRef)CFDictionaryGetValue( subNativeDict, CFSTR( kXMLSearchBase ) );
if (searchBase != nil)
{
if ( CFGetTypeID( searchBase ) == CFStringGetTypeID() )
{
::memset(tmpBuff,0,1024);
if (CFStringGetCString(searchBase, tmpBuff, cfBuffSize, kCFStringEncodingUTF8))
{
outResult = (char *) calloc(1, 1+strlen(tmpBuff));
::strcpy(outResult, tmpBuff);
CFArrayRef objectClasses;
objectClasses = (CFArrayRef)CFDictionaryGetValue( subNativeDict, CFSTR( kXMLObjectClasses ) );
if ( (objectClasses != nil) && (outOCListCFArray != nil) && (outOCGroup != nil) )
{
if ( CFGetTypeID( objectClasses ) == CFArrayGetTypeID() )
{
*outOCGroup = 0;
CFStringRef groupOCString = nil;
groupOCString = (CFStringRef)CFDictionaryGetValue( subNativeDict, CFSTR( kXMLGroupObjectClasses ) );
if ( groupOCString != nil )
{
if ( CFGetTypeID( groupOCString ) == CFStringGetTypeID() )
{
if (CFStringCompare( groupOCString, CFSTR("AND"), 0 ) == kCFCompareEqualTo)
{
*outOCGroup = 1;
}
}
}
*outOCListCFArray = CFArrayCreateCopy(kCFAllocatorDefault, objectClasses);
} } } } }
if (outScope != nil)
{
cfBoolRef = (CFBooleanRef)CFDictionaryGetValue( subNativeDict, CFSTR( kXMLOneLevelSearchScope ) );
if (cfBoolRef != nil)
{
if (CFBooleanGetValue(cfBoolRef))
{
*outScope = LDAP_SCOPE_ONELEVEL;
}
else
{
*outScope = LDAP_SCOPE_SUBTREE;
}
}
else
{
*outScope = LDAP_SCOPE_SUBTREE;
}
}
}
}
}
free(tmpBuff);
}
} } break;
}
}
}
}
}
}
}
CFRelease(cfRecTypeRef);
}
return( outResult );
}
char* CLDAPv3Configs::ExtractAttrMap( const char *inRecType, const char *inAttrType, CFArrayRef inRecordTypeMapCFArray, CFArrayRef inAttrTypeMapCFArray, int inIndex )
{
char *outResult = nil;
CFIndex cfMapCount = 0;
sInt32 iMapIndex = 0;
CFStringRef cfStringRef = nil;
CFStringRef cfRecTypeRef = nil;
CFStringRef cfAttrTypeRef = nil;
CFArrayRef cfAttrMapArrayRef = nil;
bool bNoRecSpecificAttrMap = true;
if ( (inRecordTypeMapCFArray != nil) && (inRecType != nil) && (inAttrType != nil) )
{
cfRecTypeRef = CFStringCreateWithCString(kCFAllocatorDefault, inRecType, kCFStringEncodingUTF8);
cfAttrTypeRef = CFStringCreateWithCString(kCFAllocatorDefault, inAttrType, kCFStringEncodingUTF8);
cfMapCount = ::CFArrayGetCount( inRecordTypeMapCFArray );
if (cfMapCount != 0)
{
for (iMapIndex = 0; iMapIndex < cfMapCount; iMapIndex++)
{
CFDictionaryRef typeMapDict;
typeMapDict = (CFDictionaryRef)::CFArrayGetValueAtIndex( inRecordTypeMapCFArray, iMapIndex );
if ( typeMapDict != nil )
{
if ( CFDictionaryContainsKey( typeMapDict, CFSTR( kXMLStdNameKey ) ) )
{
cfStringRef = (CFStringRef)CFDictionaryGetValue( typeMapDict, CFSTR( kXMLStdNameKey ) );
if ( cfStringRef != nil )
{
if ( CFGetTypeID( cfStringRef ) == CFStringGetTypeID() )
{
if (CFStringCompare(cfStringRef, cfRecTypeRef, 0) == kCFCompareEqualTo)
{
cfAttrMapArrayRef = GetAttributeTypeMapArray(typeMapDict);
outResult = ExtractAttrMapFromArray( cfAttrTypeRef, cfAttrMapArrayRef, inIndex, &bNoRecSpecificAttrMap );
if (bNoRecSpecificAttrMap)
{
outResult = ExtractAttrMapFromArray( cfAttrTypeRef, inAttrTypeMapCFArray, inIndex, &bNoRecSpecificAttrMap ); }
break;
}
}
}
}
}
}
}
CFRelease(cfRecTypeRef);
CFRelease(cfAttrTypeRef);
}
return( outResult );
}
char* CLDAPv3Configs::ExtractAttrMapFromArray( CFStringRef inAttrTypeRef, CFArrayRef inAttrTypeMapCFArray, int inIndex, bool *bNoRecSpecificAttrMap )
{
char *outResult = nil;
CFIndex cfAttrMapCount = 0;
CFIndex cfNativeMapCount = 0;
sInt32 iAttrMapIndex = 0;
CFStringRef cfAttrStringRef = nil;
char *tmpBuff = nil;
CFIndex cfBuffSize = 1024;
CFArrayRef cfNativeMapArrayRef = nil;
if ( (inAttrTypeRef != nil) && (inAttrTypeMapCFArray != nil) )
{
cfAttrMapCount = ::CFArrayGetCount( inAttrTypeMapCFArray );
if (cfAttrMapCount != 0)
{
for (iAttrMapIndex = 0; iAttrMapIndex < cfAttrMapCount; iAttrMapIndex++)
{
CFDictionaryRef typeAttrMapDict;
typeAttrMapDict = (CFDictionaryRef)::CFArrayGetValueAtIndex( inAttrTypeMapCFArray, iAttrMapIndex );
if ( typeAttrMapDict != nil )
{
if ( CFDictionaryContainsKey( typeAttrMapDict, CFSTR( kXMLStdNameKey ) ) )
{
cfAttrStringRef = (CFStringRef)CFDictionaryGetValue( typeAttrMapDict, CFSTR( kXMLStdNameKey ) );
if ( cfAttrStringRef != nil )
{
if ( CFGetTypeID( cfAttrStringRef ) == CFStringGetTypeID() )
{
if (CFStringCompare(cfAttrStringRef, inAttrTypeRef, 0) == kCFCompareEqualTo)
{
*bNoRecSpecificAttrMap = false;
cfNativeMapArrayRef = GetNativeTypeMapArray(typeAttrMapDict);
if (cfNativeMapArrayRef != nil)
{
cfNativeMapCount = ::CFArrayGetCount( cfNativeMapArrayRef );
if (cfNativeMapCount != 0)
{
if ( (inIndex >= 1) && (inIndex <= cfNativeMapCount) )
{
tmpBuff = (char *) calloc(1, 1024);
if (CFGetTypeID(CFArrayGetValueAtIndex( cfNativeMapArrayRef, inIndex-1 )) == CFStringGetTypeID())
{
CFStringRef nativeMapString;
nativeMapString = (CFStringRef)::CFArrayGetValueAtIndex( cfNativeMapArrayRef, inIndex-1 );
if ( nativeMapString != nil )
{
if (CFStringGetCString(nativeMapString, tmpBuff, cfBuffSize, kCFStringEncodingUTF8))
{
outResult = (char *) calloc(1, 1+strlen(tmpBuff));
::strcpy(outResult, tmpBuff);
}
} }
free(tmpBuff);
} } } break;
}
}
}
}
} } } }
return(outResult);
}
char* CLDAPv3Configs::ExtractStdAttr( char *inRecType, CFArrayRef inRecordTypeMapCFArray, CFArrayRef inAttrTypeMapCFArray, int &inputIndex )
{
char *outResult = nil;
CFIndex cfMapCount = 0;
CFIndex cfAttrMapCount = 0;
CFIndex cfAttrMapCount2 = 0;
sInt32 iMapIndex = 0;
CFStringRef cfStringRef = nil;
CFStringRef cfRecTypeRef = nil;
CFArrayRef cfAttrMapArrayRef = nil;
bool bUsedIndex = false;
char *tmpBuff = nil;
CFIndex cfBuffSize = 1024;
int inIndex = inputIndex;
if ( (inRecordTypeMapCFArray != nil) && (inRecType != nil) )
{
cfRecTypeRef = CFStringCreateWithCString(kCFAllocatorDefault, inRecType, kCFStringEncodingUTF8);
cfMapCount = ::CFArrayGetCount( inRecordTypeMapCFArray );
if (cfMapCount != 0)
{
for (iMapIndex = 0; iMapIndex < cfMapCount; iMapIndex++)
{
CFDictionaryRef typeMapDict;
typeMapDict = (CFDictionaryRef)::CFArrayGetValueAtIndex( inRecordTypeMapCFArray, iMapIndex );
if ( typeMapDict != nil )
{
if ( CFDictionaryContainsKey( typeMapDict, CFSTR( kXMLStdNameKey ) ) )
{
cfStringRef = (CFStringRef)CFDictionaryGetValue( typeMapDict, CFSTR( kXMLStdNameKey ) );
if ( cfStringRef != nil )
{
if ( CFGetTypeID( cfStringRef ) == CFStringGetTypeID() )
{
if (CFStringCompare(cfStringRef, cfRecTypeRef, 0) == kCFCompareEqualTo)
{
cfAttrMapArrayRef = GetAttributeTypeMapArray(typeMapDict);
if (cfAttrMapArrayRef != nil)
{
cfAttrMapCount = ::CFArrayGetCount( cfAttrMapArrayRef );
if (cfAttrMapCount != 0)
{
if ( (inIndex >= 1) && (inIndex <= cfAttrMapCount) )
{
bUsedIndex = true;
tmpBuff = (char *) calloc(1, 1024);
if (CFGetTypeID(CFArrayGetValueAtIndex( cfAttrMapArrayRef, inIndex-1 )) == CFDictionaryGetTypeID())
{
CFDictionaryRef stdAttrTypeDict;
stdAttrTypeDict = (CFDictionaryRef)CFArrayGetValueAtIndex( cfAttrMapArrayRef, inIndex-1 );
if ( stdAttrTypeDict != nil )
{
if ( CFDictionaryContainsKey( stdAttrTypeDict, CFSTR( kXMLStdNameKey ) ) )
{
CFStringRef attrMapString;
attrMapString = (CFStringRef)CFDictionaryGetValue( stdAttrTypeDict, CFSTR( kXMLStdNameKey ) );
if ( attrMapString != nil )
{
if (CFStringGetCString(attrMapString, tmpBuff, cfBuffSize, kCFStringEncodingUTF8))
{
outResult = (char *) calloc(1, 1+strlen(tmpBuff));
::strcpy(outResult, tmpBuff);
}
} } } }
free(tmpBuff);
} } }
while (!bUsedIndex)
{
bUsedIndex = true;
if (inAttrTypeMapCFArray != nil)
{
CFIndex commonIndex = inIndex - cfAttrMapCount;
cfAttrMapCount2 = ::CFArrayGetCount( inAttrTypeMapCFArray );
if (cfAttrMapCount2 != 0)
{
if ( (commonIndex >= 1) && (commonIndex <= cfAttrMapCount2) )
{
tmpBuff = (char *) calloc(1, 1024);
if (CFGetTypeID(CFArrayGetValueAtIndex( inAttrTypeMapCFArray, commonIndex-1 )) == CFDictionaryGetTypeID())
{
CFDictionaryRef stdAttrTypeDict;
stdAttrTypeDict = (CFDictionaryRef)CFArrayGetValueAtIndex( inAttrTypeMapCFArray, commonIndex-1 );
if ( stdAttrTypeDict != nil )
{
if ( CFDictionaryContainsKey( stdAttrTypeDict, CFSTR( kXMLStdNameKey ) ) )
{
CFStringRef attrMapString;
attrMapString = (CFStringRef)CFDictionaryGetValue( stdAttrTypeDict, CFSTR( kXMLStdNameKey ) );
if ( attrMapString != nil )
{
bool bNoDuplicate = true;
if ( (cfAttrMapArrayRef != NULL) && (cfAttrMapCount != 0) )
{
for (sInt32 aIndex = 0; aIndex < cfAttrMapCount; aIndex++)
{
if (CFGetTypeID(CFArrayGetValueAtIndex( cfAttrMapArrayRef, aIndex )) == CFDictionaryGetTypeID())
{
CFDictionaryRef stdAttrTypeDict;
stdAttrTypeDict = (CFDictionaryRef)CFArrayGetValueAtIndex( cfAttrMapArrayRef, aIndex );
if ( stdAttrTypeDict != nil )
{
if ( CFDictionaryContainsKey( stdAttrTypeDict, CFSTR( kXMLStdNameKey ) ) )
{
CFStringRef attrMapStringOld;
attrMapStringOld = (CFStringRef)CFDictionaryGetValue( stdAttrTypeDict, CFSTR( kXMLStdNameKey ) );
if ( attrMapStringOld != nil )
{
if (CFStringCompare(attrMapStringOld, attrMapString, 0) == kCFCompareEqualTo)
{
bNoDuplicate = false;
bUsedIndex = false;
inIndex++;
break;
}
} } } }
} }
if (bNoDuplicate)
{
if (CFStringGetCString(attrMapString, tmpBuff, cfBuffSize, kCFStringEncodingUTF8))
{
outResult = (char *) calloc(1, 1+strlen(tmpBuff));
::strcpy(outResult, tmpBuff);
}
}
} } } }
free(tmpBuff);
} } } } break;
}
}
}
}
}
}
}
CFRelease(cfRecTypeRef);
}
if (inIndex != inputIndex)
{
inputIndex = inIndex;
}
return( outResult );
}
int CLDAPv3Configs::AttrMapsCount( const char *inRecType, const char *inAttrType, CFArrayRef inRecordTypeMapCFArray, CFArrayRef inAttrTypeMapCFArray )
{
int outCount = 0;
CFIndex cfMapCount = 0;
sInt32 iMapIndex = 0;
CFStringRef cfStringRef = nil;
CFStringRef cfRecTypeRef = nil;
CFStringRef cfAttrTypeRef = nil;
CFArrayRef cfAttrMapArrayRef = nil;
bool bNoRecSpecificAttrMap = true;
if ( (inRecordTypeMapCFArray != nil) && (inRecType != nil) && (inAttrType != nil) )
{
cfRecTypeRef = CFStringCreateWithCString(kCFAllocatorDefault, inRecType, kCFStringEncodingUTF8);
cfAttrTypeRef = CFStringCreateWithCString(kCFAllocatorDefault, inAttrType, kCFStringEncodingUTF8);
cfMapCount = ::CFArrayGetCount( inRecordTypeMapCFArray );
if (cfMapCount != 0)
{
for (iMapIndex = 0; iMapIndex < cfMapCount; iMapIndex++)
{
CFDictionaryRef typeMapDict;
typeMapDict = (CFDictionaryRef)::CFArrayGetValueAtIndex( inRecordTypeMapCFArray, iMapIndex );
if ( typeMapDict != nil )
{
if ( CFDictionaryContainsKey( typeMapDict, CFSTR( kXMLStdNameKey ) ) )
{
cfStringRef = (CFStringRef)CFDictionaryGetValue( typeMapDict, CFSTR( kXMLStdNameKey ) );
if ( cfStringRef != nil )
{
if ( CFGetTypeID( cfStringRef ) == CFStringGetTypeID() )
{
if (CFStringCompare(cfStringRef, cfRecTypeRef, 0) == kCFCompareEqualTo)
{
cfAttrMapArrayRef = GetAttributeTypeMapArray(typeMapDict);
outCount = AttrMapFromArrayCount( cfAttrTypeRef, cfAttrMapArrayRef, &bNoRecSpecificAttrMap );
if (bNoRecSpecificAttrMap)
{
outCount = AttrMapFromArrayCount( cfAttrTypeRef, inAttrTypeMapCFArray, &bNoRecSpecificAttrMap ); }
break;
}
}
}
}
}
}
}
CFRelease(cfRecTypeRef);
CFRelease(cfAttrTypeRef);
}
return( outCount );
}
int CLDAPv3Configs::AttrMapFromArrayCount( CFStringRef inAttrTypeRef, CFArrayRef inAttrTypeMapCFArray, bool *bNoRecSpecificAttrMap )
{
int outCount = 0;
CFIndex cfAttrMapCount = 0;
sInt32 iAttrMapIndex = 0;
CFStringRef cfAttrStringRef = nil;
CFArrayRef cfNativeMapArrayRef = nil;
if ( (inAttrTypeRef != nil) && (inAttrTypeMapCFArray != nil) )
{
cfAttrMapCount = ::CFArrayGetCount( inAttrTypeMapCFArray );
if (cfAttrMapCount != 0)
{
for (iAttrMapIndex = 0; iAttrMapIndex < cfAttrMapCount; iAttrMapIndex++)
{
CFDictionaryRef typeAttrMapDict;
typeAttrMapDict = (CFDictionaryRef)::CFArrayGetValueAtIndex( inAttrTypeMapCFArray, iAttrMapIndex );
if ( typeAttrMapDict != nil )
{
if ( CFDictionaryContainsKey( typeAttrMapDict, CFSTR( kXMLStdNameKey ) ) )
{
cfAttrStringRef = (CFStringRef)CFDictionaryGetValue( typeAttrMapDict, CFSTR( kXMLStdNameKey ) );
if ( cfAttrStringRef != nil )
{
if ( CFGetTypeID( cfAttrStringRef ) == CFStringGetTypeID() )
{
if (CFStringCompare(cfAttrStringRef, inAttrTypeRef, 0) == kCFCompareEqualTo)
{
*bNoRecSpecificAttrMap = false;
cfNativeMapArrayRef = GetNativeTypeMapArray(typeAttrMapDict);
if (cfNativeMapArrayRef != nil)
{
outCount = ::CFArrayGetCount( cfNativeMapArrayRef );
} break;
}
}
}
}
} } } }
return(outCount);
}
void CLDAPv3Configs::XMLConfigLock( void )
{
if (pXMLConfigLock != nil)
{
pXMLConfigLock->Wait();
}
}
void CLDAPv3Configs::XMLConfigUnlock( void )
{
if (pXMLConfigLock != nil)
{
pXMLConfigLock->Signal();
}
}
sInt32 CLDAPv3Configs::UpdateReplicaList(char *inServerName, CFMutableArrayRef inReplicaHostnames, CFMutableArrayRef inWriteableHostnames)
{
sInt32 siResult = eDSNoErr;
CFStringRef errorString = NULL;
CFPropertyListRef configPropertyList = NULL;
CFMutableDictionaryRef configDict = NULL;
CFArrayRef cfArrayRef = NULL;
CFIndex cfConfigCount = 0;
CFDataRef xmlData = NULL;
bool bDoWrite = false;
if (fXMLData != nil)
{
configPropertyList = CFPropertyListCreateFromXMLData( kCFAllocatorDefault,
fXMLData,
kCFPropertyListMutableContainersAndLeaves, &errorString);
if (configPropertyList != nil )
{
if ( CFDictionaryGetTypeID() == CFGetTypeID( configPropertyList ) )
{
configDict = (CFMutableDictionaryRef) configPropertyList;
}
if (configDict != nil)
{
cfArrayRef = nil;
cfArrayRef = GetConfigArray(configDict);
if (cfArrayRef != nil)
{
cfConfigCount = ::CFArrayGetCount( cfArrayRef );
for (sInt32 iConfigIndex = 0; iConfigIndex < cfConfigCount; iConfigIndex++)
{
CFMutableDictionaryRef serverDict = nil;
serverDict = (CFMutableDictionaryRef)::CFArrayGetValueAtIndex( cfArrayRef, iConfigIndex );
if ( serverDict != nil )
{
CFStringRef aString = NULL;
if ( CFDictionaryContainsKey( serverDict, CFSTR( kXMLServerKey ) ) )
{
aString = (CFStringRef)CFDictionaryGetValue( serverDict, CFSTR( kXMLServerKey ) );
if ( aString != nil )
{
if ( CFGetTypeID( aString ) == CFStringGetTypeID() )
{
CFStringRef aServerName = CFStringCreateWithCString( NULL, inServerName, kCFStringEncodingUTF8 );
if (CFStringCompare(aString, aServerName, 0) == kCFCompareEqualTo)
{
CFArrayRef cfRepArrayRef = NULL;
cfRepArrayRef = GetReplicaHostnameListArray(serverDict);
if (cfRepArrayRef != NULL)
{
CFDictionaryRemoveValue( serverDict, CFSTR( kXMLReplicaHostnameListArrayKey ) );
}
if( inReplicaHostnames )
{
CFDictionarySetValue( serverDict, CFSTR( kXMLReplicaHostnameListArrayKey ), (CFArrayRef)inReplicaHostnames );
}
bDoWrite = true;
cfRepArrayRef = NULL;
cfRepArrayRef = GetWriteableHostnameListArray(serverDict);
if (cfRepArrayRef != NULL)
{
CFDictionaryRemoveValue( serverDict, CFSTR( kXMLWriteableHostnameListArrayKey ) );
}
if( inWriteableHostnames )
{
CFDictionarySetValue( serverDict, CFSTR( kXMLWriteableHostnameListArrayKey ), (CFArrayRef)inWriteableHostnames );
}
bDoWrite = true;
if (bDoWrite)
{
xmlData = CFPropertyListCreateXMLData( kCFAllocatorDefault, configDict);
siResult = SetXMLConfig(xmlData);
CFRelease(xmlData);
xmlData = nil;
if (siResult == eDSNoErr)
{
WriteXMLConfig();
}
}
CFRelease(aServerName);
break; }
CFRelease(aServerName);
}
}
}
}
}
}
}
CFRelease(configPropertyList); configPropertyList = nil;
} }
return( siResult );
}
bool CLDAPv3Configs::CreatePrefDirectory( void )
{
char *filenameString = "/Library/Preferences/DirectoryService/DSLDAPv3PlugInConfig.plist";
int siResult = eDSNoErr;
struct stat statResult;
DBGLOG( kLogPlugin, "Checking for LDAP XML config file:" );
DBGLOG1( kLogPlugin, "%s", filenameString );
siResult = ::stat( filenameString, &statResult );
if (siResult != eDSNoErr)
{
char *tempPath = "/Library/Preferences";
siResult = ::stat( tempPath, &statResult );
if (siResult != eDSNoErr)
{
::mkdir( tempPath, 0775 );
::chmod( tempPath, 0775 ); }
tempPath = "/Library/Preferences/DirectoryService";
siResult = ::stat( tempPath, &statResult );
if (siResult != eDSNoErr)
{
::mkdir( tempPath, 0775 );
::chmod( tempPath, 0775 ); }
}
return (siResult == eDSNoErr);
}
bool CLDAPv3Configs::ConvertLDAPv2Config( void )
{
struct stat statResult;
const char *prefPath = "/Library/Preferences/DirectoryService/DSLDAPPlugInConfig.clpi";
bool bReturn = false;
CFDataRef sV2ConfigData = NULL;
CFMutableDictionaryRef sV2Config = NULL;
CFMutableDictionaryRef sV3Config = NULL;
if( stat("/System/Library/Frameworks/DirectoryService.framework/Resources/Plugins/LDAPv2.dsplug", &statResult) != 0 )
{
char newName[PATH_MAX] = { 0 };
CFStringRef sPath = ::CFStringCreateWithCString( kCFAllocatorDefault, prefPath, kCFStringEncodingUTF8 );
strcpy( newName, prefPath );
strcat( newName, ".v3converted" );
if( ::stat( prefPath, &statResult ) == 0 ) {
CFURLRef sConfigFileURL = ::CFURLCreateWithFileSystemPath( kCFAllocatorDefault, sPath, kCFURLPOSIXPathStyle, false );
CFURLCreateDataAndPropertiesFromResource( kCFAllocatorDefault, sConfigFileURL, &sV2ConfigData, NULL, NULL, NULL );
CFRelease( sConfigFileURL );
sConfigFileURL = NULL;
if( sV2ConfigData )
{
sV2Config = (CFMutableDictionaryRef) CFPropertyListCreateFromXMLData( kCFAllocatorDefault, sV2ConfigData, kCFPropertyListMutableContainers, NULL );
CFRelease( sV2ConfigData );
sV2ConfigData = NULL;
}
sV3Config = (CFMutableDictionaryRef) CFPropertyListCreateFromXMLData( kCFAllocatorDefault, fXMLData, kCFPropertyListMutableContainers, NULL );
if( sV2Config && sV3Config )
{
CFStringRef tConfigKey = CFSTR( kXMLConfigArrayKey );
CFMutableArrayRef tV3ConfigEntries = (CFMutableArrayRef) CFDictionaryGetValue( sV3Config, tConfigKey );
CFArrayRef tV2ConfigEntries = (CFArrayRef) CFDictionaryGetValue( sV2Config, tConfigKey );
CFMutableDictionaryRef tV2ConfigEntry = NULL;
if( tV2ConfigEntries )
{
CFIndex v2ConfigCount = CFArrayGetCount(tV2ConfigEntries);
CFIndex v2ConfigIndex;
for( v2ConfigIndex = 0; v2ConfigIndex < v2ConfigCount; v2ConfigIndex++ )
{
tV2ConfigEntry = (CFMutableDictionaryRef) CFArrayGetValueAtIndex( tV2ConfigEntries, v2ConfigIndex );
if( tV2ConfigEntry )
{
CFTypeRef tObjectValue = CFDictionaryGetValue( tV2ConfigEntry, CFSTR(kXMLServerKey) );
if( tObjectValue )
{
if( tV3ConfigEntries )
{
CFIndex count = CFArrayGetCount( tV3ConfigEntries );
CFIndex index;
for( index = 0; index < count; index++ )
{
CFDictionaryRef tServerConfig = (CFDictionaryRef) CFArrayGetValueAtIndex( tV3ConfigEntries, index );
CFStringRef tServer = (CFStringRef) CFDictionaryGetValue( tServerConfig, CFSTR(kXMLServerKey) );
if( tServer && CFStringCompare(tServer, (CFStringRef) tObjectValue, kCFCompareCaseInsensitive) == kCFCompareEqualTo )
{
CFDictionarySetValue( tV2ConfigEntry, CFSTR(kXMLEnableUseFlagKey), kCFBooleanFalse );
}
}
}
}
CFDictionarySetValue( tV2ConfigEntry, CFSTR(kXMLServerMappingsFlagKey), kCFBooleanFalse );
CFDictionarySetValue( tV2ConfigEntry, CFSTR(kXMLMakeDefLDAPFlagKey), kCFBooleanFalse );
CFStringRef tKeyValue = CFSTR( kXMLUserDefinedNameKey );
tObjectValue = CFDictionaryGetValue( tV2ConfigEntry, tKeyValue );
if( tObjectValue )
{
CFStringRef sNewName = CFStringCreateWithFormat( kCFAllocatorDefault, NULL, CFSTR("%@ (from LDAPv2)"), tObjectValue );
CFDictionarySetValue( tV2ConfigEntry, tKeyValue, sNewName );
CFRelease( sNewName );
}
CFArrayRef tRecMap = (CFArrayRef) CFDictionaryGetValue( tV2ConfigEntry, CFSTR(kXMLRecordTypeMapArrayKey) );
if( tRecMap )
{
CFIndex index;
CFIndex count = CFArrayGetCount( tRecMap );
for( index = 0; index < count; index++ )
{
CFMutableDictionaryRef tRecordMapDict = (CFMutableDictionaryRef) CFArrayGetValueAtIndex( tRecMap, index );
CFMutableArrayRef tNativeArray = (CFMutableArrayRef) CFDictionaryGetValue( tRecordMapDict, CFSTR(kXMLNativeMapArrayKey) );
CFArrayRef sBlankArray = CFArrayCreate( NULL, NULL, 0, &kCFTypeArrayCallBacks );
CFDictionarySetValue( tRecordMapDict, CFSTR(kXMLAttrTypeMapArrayKey), sBlankArray );
CFRelease( sBlankArray );
if( tNativeArray == NULL )
{
tNativeArray = CFArrayCreateMutable( NULL, 0, &kCFTypeArrayCallBacks );
CFDictionarySetValue( tRecordMapDict, CFSTR(kXMLNativeMapArrayKey), tNativeArray );
CFRelease( tNativeArray );
}
CFMutableDictionaryRef sNewNativeDict = CFDictionaryCreateMutable( NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks );
CFDictionarySetValue( sNewNativeDict, CFSTR(kXMLGroupObjectClasses), CFSTR("OR") );
if( CFArrayGetCount( tNativeArray ) > 0 )
{
CFDictionarySetValue( sNewNativeDict, CFSTR(kXMLSearchBase), CFArrayGetValueAtIndex(tNativeArray, 0) );
}
CFArrayRemoveAllValues( tNativeArray );
CFArrayAppendValue( tNativeArray, sNewNativeDict );
CFRelease( sNewNativeDict );
}
}
if( tV3ConfigEntries == NULL )
{
tV3ConfigEntries = CFArrayCreateMutable( kCFAllocatorDefault, NULL, &kCFTypeArrayCallBacks);
CFDictionarySetValue( sV3Config, tConfigKey, tV3ConfigEntries );
CFRelease( tV3ConfigEntries );
}
CFArrayAppendValue( tV3ConfigEntries, tV2ConfigEntry );
if( fXMLData )
{
CFRelease( fXMLData );
fXMLData = NULL;
}
fXMLData = (CFDataRef) CFPropertyListCreateXMLData( kCFAllocatorDefault, sV3Config );
bReturn = true;
}
}
}
}
rename( prefPath, newName );
}
CFRelease( sPath );
sPath = NULL;
}
if( sV2Config )
{
CFRelease( sV2Config );
sV2Config = NULL;
}
if( sV3Config )
{
CFRelease( sV3Config );
sV3Config = NULL;
}
return bReturn;
}