JNIEXPORT jint JNICALL Java_com_vmware_identity_cdc_CdcAdapter_CdcEnumDCEntriesW( JNIEnv *env, jclass clazz, jobject jpServer, jobject jStringList ) { DWORD dwError = 0; PVMAFD_SERVER pServer = NULL; PWSTR* ppwszDCEntriesArray = NULL; DWORD dwCount = 0; if (jpServer == NULL) { dwError = ERROR_INVALID_PARAMETER; BAIL_ON_ERROR(dwError); } dwError = JniGetPointer(env, jpServer, (PVOID*)&pServer); BAIL_ON_ERROR(dwError); if (jStringList == NULL) { dwError = ERROR_INVALID_PARAMETER; BAIL_ON_ERROR(dwError); } dwError = CdcEnumDCEntriesW( pServer, &ppwszDCEntriesArray, &dwCount ); BAIL_ON_ERROR(dwError); dwError = JniAddStringsToList(env, jStringList, ppwszDCEntriesArray, dwCount); BAIL_ON_ERROR(dwError); cleanup: if (ppwszDCEntriesArray) { VmAfdFreeStringArrayW(ppwszDCEntriesArray, dwCount); } return dwError; error: goto cleanup; }
static DWORD CdcPurgeCache( VOID ) { DWORD dwError = 0; PWSTR *ppszEntryNames = NULL; DWORD dwDCCount = 0; DWORD dwIndex = 0; dwError = CdcDbEnumDCEntries( &ppszEntryNames, &dwDCCount ); BAIL_ON_VMAFD_ERROR(dwError); if (dwDCCount) { for (; dwIndex<dwDCCount; ++dwIndex) { dwError = CdcDbDeleteDCEntry(ppszEntryNames[dwIndex]); BAIL_ON_VMAFD_ERROR(dwError); } } cleanup: if (ppszEntryNames) { VmAfdFreeStringArrayW(ppszEntryNames, dwDCCount); } return dwError; error: goto cleanup; }
DWORD VecsSrvEnumFilteredStores ( PVM_AFD_CONNECTION_CONTEXT pConnectionContext, PWSTR **ppwszStoreNames, PDWORD pdwCount ) { DWORD dwError = 0; DWORD dwCount = 0; PBYTE pContextBlob = NULL; DWORD dwContextSize = 0; DWORD dwContextSizeRead = 0; PWSTR *pwszStoreName = NULL; if (!pConnectionContext || !pConnectionContext->pSecurityContext || !ppwszStoreNames || !pdwCount ) { dwError = ERROR_INVALID_PARAMETER; BAIL_ON_VMAFD_ERROR (dwError); } if (VmAfdIsRootSecurityContext(pConnectionContext)) { dwError = VecsSrvEnumCertStore( &pwszStoreName, &dwCount ); BAIL_ON_VMAFD_ERROR (dwError); } else { dwError = VmAfdGetSecurityContextSize ( pConnectionContext->pSecurityContext, &dwContextSize ); BAIL_ON_VMAFD_ERROR (dwError); dwError = VmAfdAllocateMemory ( dwContextSize, (PVOID *) &pContextBlob ); BAIL_ON_VMAFD_ERROR (dwError); dwError = VmAfdEncodeSecurityContext ( pConnectionContext->pSecurityContext, pContextBlob, dwContextSize, &dwContextSizeRead ); BAIL_ON_VMAFD_ERROR (dwError); dwError = VecsDbEnumFilteredStores ( pContextBlob, dwContextSizeRead, &pwszStoreName, &dwCount ); BAIL_ON_VMAFD_ERROR (dwError); } *ppwszStoreNames = pwszStoreName; *pdwCount = dwCount; cleanup: VMAFD_SAFE_FREE_MEMORY (pContextBlob); return dwError; error: if (ppwszStoreNames) { *ppwszStoreNames = NULL; } if (pwszStoreName) { VmAfdFreeStringArrayW (pwszStoreName, dwCount); } if (pdwCount) { *pdwCount = 0; } goto cleanup; }
static DWORD CdcUpdateCache( BOOL bPurgeRefresh ) { DWORD dwError = 0; PWSTR *ppszDomainControllers = NULL; DWORD dwDCCount = 0; DWORD dwIndex = 0; time_t timeTaken = 0; DWORD dwPingResult = 0; PCDC_DB_ENTRY_W pCdcEntry = NULL; if (bPurgeRefresh) { dwError = CdcGetDomainControllers( &ppszDomainControllers, &dwDCCount ); } if (!bPurgeRefresh || dwError) { dwError = CdcDbEnumDCEntries( &ppszDomainControllers, &dwDCCount ); BAIL_ON_VMAFD_ERROR(dwError); } if (dwDCCount) { dwError = VmAfdAllocateMemory( sizeof(CDC_DB_ENTRY_W)*dwDCCount, (PVOID *)&pCdcEntry ); BAIL_ON_VMAFD_ERROR(dwError); for (; dwIndex < dwDCCount; ++dwIndex) { dwError = VmAfdAllocateStringW( ppszDomainControllers[dwIndex], &pCdcEntry[dwIndex].pszDCName ); BAIL_ON_VMAFD_ERROR(dwError); dwError = CdcDCPing( ppszDomainControllers[dwIndex], NULL, //to accomodate multiple domains in future &dwPingResult, &timeTaken, &pCdcEntry[dwIndex].pszSiteName ); pCdcEntry[dwIndex].dwPingTime = (DWORD) (timeTaken); if (!dwPingResult) { pCdcEntry[dwIndex].bIsAlive = TRUE; } pCdcEntry[dwIndex].dwLastPing = (DWORD)time(NULL); pCdcEntry[dwIndex].cdcEntryStatus = bPurgeRefresh?CDC_DB_ENTRY_STATUS_NEW: CDC_DB_ENTRY_STATUS_UPDATE; } dwError = CdcRefreshCache(bPurgeRefresh, pCdcEntry, dwDCCount); BAIL_ON_VMAFD_ERROR(dwError); } cleanup: if (pCdcEntry) { VmAfdFreeCdcDbEntryArrayW(pCdcEntry,dwDCCount); } if (ppszDomainControllers) { VmAfdFreeStringArrayW( ppszDomainControllers, dwDCCount ); } return dwError; error: goto cleanup; }
static DWORD CdcRefreshCache( BOOL bPurgeRefresh, PCDC_DB_ENTRY_W pCdcEntry, DWORD dwCount ) { //TODO: Or simply always purge and repopulate? DWORD dwError = 0; PWSTR *ppszDomainControllers = NULL; DWORD dwDBDCCount = 0; DWORD dwDbIndex = 0; DWORD dwIndex = 0; PWSTR pwszDomainName = NULL; if (!pCdcEntry) { dwError = ERROR_INVALID_PARAMETER; BAIL_ON_VMAFD_ERROR(dwError); } dwError = VmAfSrvGetDomainName(&pwszDomainName); BAIL_ON_VMAFD_ERROR(dwError); if (bPurgeRefresh) { dwError = CdcDbEnumDCEntries(&ppszDomainControllers, &dwDBDCCount); BAIL_ON_VMAFD_ERROR(dwError); for (; dwDbIndex<dwDBDCCount; ++dwDbIndex) { BOOL bFoundDC = FALSE; for (dwIndex = 0;dwIndex < dwCount; ++dwIndex) { if (VmAfdStringIsEqualW( ppszDomainControllers[dwDbIndex], pCdcEntry[dwIndex].pszDCName, FALSE ) ) { pCdcEntry[dwIndex].cdcEntryStatus = CDC_DB_ENTRY_STATUS_UPDATE; bFoundDC = TRUE; break; } } if (!bFoundDC) { CdcDbDeleteDCEntry(ppszDomainControllers[dwDbIndex]); } } } for (dwIndex = 0; dwIndex < dwCount; ++dwIndex) { dwError = VmAfdAllocateStringW( pwszDomainName, &pCdcEntry[dwIndex].pszDomainName ); BAIL_ON_VMAFD_ERROR(dwError); switch (pCdcEntry[dwIndex].cdcEntryStatus) { case CDC_DB_ENTRY_STATUS_NEW: dwError = CdcDbAddDCEntry(&pCdcEntry[dwIndex]); break; case CDC_DB_ENTRY_STATUS_UPDATE: dwError = CdcDbUpdateDCEntry(&pCdcEntry[dwIndex]); break; default: dwError = ERROR_INVALID_PARAMETER; break; } BAIL_ON_VMAFD_ERROR(dwError); } cleanup: if (ppszDomainControllers) { VmAfdFreeStringArrayW(ppszDomainControllers, dwDBDCCount); } VMAFD_SAFE_FREE_MEMORY(pwszDomainName); return dwError; error: goto cleanup; }
static DWORD CdcGetDomainControllers( PWSTR** ppszDomainControllers, PDWORD pdwCount ) { DWORD dwError = 0; DWORD dwIndex = 0; PWSTR pwszDomain = NULL; PSTR pszDomain = NULL; PSTR pszDCName = NULL; PWSTR pwszAccount = NULL; PSTR pszAccount = NULL; PWSTR pwszPassword = NULL; PSTR pszPassword = NULL; PWSTR pwszAccountDN = NULL; PSTR pszAccountDN = NULL; PVMDNS_SERVER_CONTEXT pConnection = NULL; PVMDNS_RECORD_ARRAY pRecordArray = NULL; PWSTR* pszDomainControllers = NULL; PCDC_DC_INFO_W pAffinitizedDC = NULL; if (!ppszDomainControllers || !pdwCount) { dwError = ERROR_INVALID_PARAMETER; BAIL_ON_VMAFD_ERROR(dwError); } dwError = VmAfSrvGetMachineAccountInfo( &pwszAccount, &pwszPassword, &pwszAccountDN, NULL ); BAIL_ON_VMAFD_ERROR(dwError); dwError = VmAfdAllocateStringAFromW( pwszAccount, &pszAccount ); BAIL_ON_VMAFD_ERROR(dwError); dwError = VmAfdAllocateStringAFromW( pwszPassword, &pszPassword ); BAIL_ON_VMAFD_ERROR(dwError); dwError = VmAfSrvGetDomainName(&pwszDomain); BAIL_ON_VMAFD_ERROR(dwError); dwError = VmAfdAllocateStringAFromW( pwszDomain, &pszDomain ); BAIL_ON_VMAFD_ERROR(dwError); dwError = CdcSrvGetDCName(pwszDomain,&pAffinitizedDC); BAIL_ON_VMAFD_ERROR(dwError); dwError = VmAfdAllocateStringAFromW( pAffinitizedDC->pszDCName, &pszDCName ); BAIL_ON_VMAFD_ERROR(dwError); dwError = VmDnsOpenServerA( pszDCName, pszAccount, pszDomain, pszPassword, 0, NULL, &pConnection); BAIL_ON_VMAFD_ERROR(dwError); dwError = VmDnsQueryRecordsA( pConnection, pszDomain, "_ldap._tcp", VMDNS_RR_TYPE_SRV, 0, &pRecordArray); BAIL_ON_VMAFD_ERROR(dwError); if (pRecordArray->dwCount) { dwError = VmAfdAllocateMemory( sizeof(PWSTR)*pRecordArray->dwCount, (PVOID *)&pszDomainControllers ); BAIL_ON_VMAFD_ERROR(dwError); } for (; dwIndex < pRecordArray->dwCount; dwIndex++) { dwError = VmAfdAllocateStringWFromA( pRecordArray->Records[dwIndex].Data.SRV.pNameTarget, &pszDomainControllers[dwIndex] ); BAIL_ON_VMAFD_ERROR(dwError); dwError = VmAfdTrimFQDNTrailingDot(pszDomainControllers[dwIndex]); BAIL_ON_VMAFD_ERROR(dwError); } dwIndex = 0; *ppszDomainControllers = pszDomainControllers; *pdwCount = pRecordArray->dwCount; cleanup: VMAFD_SAFE_FREE_MEMORY(pwszDomain); VMAFD_SAFE_FREE_MEMORY(pszDomain); VMAFD_SAFE_FREE_MEMORY(pszDCName); VMAFD_SAFE_FREE_MEMORY(pwszAccount); VMAFD_SAFE_FREE_MEMORY(pszAccount); VMAFD_SAFE_FREE_MEMORY(pwszPassword); VMAFD_SAFE_FREE_MEMORY(pszPassword); VMAFD_SAFE_FREE_MEMORY(pwszAccountDN); VMAFD_SAFE_FREE_MEMORY(pszAccountDN); if (pAffinitizedDC) { VmAfdFreeDomainControllerInfoW(pAffinitizedDC); } if (pRecordArray) { VmDnsFreeRecordArray(pRecordArray); } if (pConnection) { VmDnsCloseServer(pConnection); } return dwError; error: if (ppszDomainControllers) { *ppszDomainControllers = NULL; } if (pszDomainControllers) { VmAfdFreeStringArrayW(pszDomainControllers,pRecordArray->dwCount); } goto cleanup; }
DWORD CdcSrvEnumDCEntries( PWSTR **pppszEntryNames, PDWORD pdwCount ) { DWORD dwError = 0; DWORD dwCount = 0; DWORD dwIndex = 0; PWSTR* ppszEntryNames = NULL; PCDC_DB_ENTRY_W pCdcEntries = NULL; UINT64 iStart = 0; UINT64 iEnd = 0; if (!pppszEntryNames || !pdwCount) { dwError = ERROR_INVALID_PARAMETER; BAIL_ON_VMAFD_ERROR(dwError); } iStart = VmAfdGetTimeInMilliSec(); dwError = CdcDbEnumDCEntries(&pCdcEntries, &dwCount); BAIL_ON_VMAFD_ERROR(dwError); if (dwCount) { dwError = VmAfdAllocateMemory( sizeof(PWSTR)*dwCount, (PVOID)&ppszEntryNames ); BAIL_ON_VMAFD_ERROR(dwError); for (; dwIndex < dwCount; ++dwIndex) { dwError = VmAfdAllocateStringW( pCdcEntries[dwIndex].pszDCName, &ppszEntryNames[dwIndex] ); BAIL_ON_VMAFD_ERROR(dwError); } } iEnd = VmAfdGetTimeInMilliSec(); *pppszEntryNames = ppszEntryNames; *pdwCount = dwCount; cleanup: if (pCdcEntries) { VmAfdFreeCdcDbEntriesW(pCdcEntries, dwCount); } return dwError; error: (DWORD)VmAfdAddDBSuperLogEntry( gVmafdGlobals.pLogger, iStart, iEnd, pCdcEntries, dwError); if (pppszEntryNames) { *pppszEntryNames = NULL; } if (ppszEntryNames) { VmAfdFreeStringArrayW(ppszEntryNames, dwCount); } goto cleanup; }