DWORD LWNetDnsBuildSRVRecordList( IN PDNS_RESPONSE_HEADER pHeader, IN PDLINKEDLIST pAnswersList, IN PDLINKEDLIST pAdditionalsList, OUT PDLINKEDLIST* ppSRVRecordList ) { DWORD dwError = 0; PDNS_SRV_INFO_RECORD pSRVRecord = NULL; PDLINKEDLIST pListMember = NULL; PDLINKEDLIST pSRVRecordList = NULL; pListMember = pAnswersList; while (pListMember) { dwError = LWNetDnsBuildSRVRecord(pHeader, (PDNS_RECORD)pListMember->pItem, pAdditionalsList, &pSRVRecord); if (dwError) { // Already logged on ERROR_NOT_FOUND if (dwError != ERROR_NOT_FOUND) { LWNET_LOG_ERROR("Failed to build SRV record information"); } // Skip dwError = 0; } else { dwError = LWNetDLinkedListAppend(&pSRVRecordList, pSRVRecord); BAIL_ON_LWNET_ERROR(dwError); pSRVRecord = NULL; } pListMember = pListMember->pNext; } error: if (pSRVRecord) { LWNetDnsFreeSRVInfoRecord(pSRVRecord); } if (dwError) { LWNET_SAFE_FREE_SRV_INFO_LINKED_LIST(pSRVRecordList); } *ppSRVRecordList = pSRVRecordList; return dwError; }
DWORD LWNetSrvStartNetBios( VOID) { DWORD dwError = 0; if (LWNetConfigIsNetBiosEnabled()) { dwError = LWNetSrvNetBiosInit(); BAIL_ON_LWNET_ERROR(dwError); dwError = LWNetSrvStartNetBiosThread(); BAIL_ON_LWNET_ERROR(dwError); } cleanup: return dwError; error: LWNetSrvNetBiosCleanup(); LWNET_LOG_ERROR("Failed initializing NetBIOS listener thread %s", LwErrnoToName(dwError)); goto cleanup; }
NTSTATUS LWNetSvcmRefresh( PLW_SVCM_INSTANCE pInstance ) { DWORD dwError = 0; HANDLE hServer = NULL; LWNET_LOG_INFO("Refreshing configuration"); dwError = LWNetSrvOpenServer( getuid(), getgid(), &hServer); BAIL_ON_LWNET_ERROR(dwError); dwError = LWNetSrvRefreshConfiguration(hServer); BAIL_ON_LWNET_ERROR(dwError); LWNET_LOG_INFO("Refreshed configuration successfully"); cleanup: if (hServer != NULL) { LWNetSrvCloseServer(hServer); } return LwWin32ErrorToNtStatus(dwError); error: LWNET_LOG_ERROR("Failed to refresh configuration. [Error code:%u]", dwError); goto cleanup; }
static DWORD LWNetSrvPingCLdapProcess( IN PLWNET_CLDAP_CONNECTION_CONTEXT pContext, IN DWORD dwDsFlags, IN LWNET_UNIX_MS_TIME_T StopTime, OUT PLWNET_DC_INFO* ppDcInfo, OUT PBOOLEAN pbFailedFindWritable ) { DWORD dwError = 0; DWORD dwResultType = 0; LDAPMessage* pMessage = NULL; PBYTE pNetlogonAttributeValue = NULL; DWORD dwNetlogonAttributeSize = 0; PLWNET_DC_INFO pDcInfo = NULL; BOOLEAN bFailedFindWritable = FALSE; struct timeval timeout = {0}; LDAP *ld = NULL; ld = LwLdapGetSession(pContext->hDirectory); dwResultType = ldap_result( ld, pContext->msgid, 0, &timeout, &pMessage); if (dwResultType == 0) { // timed out goto error; } else if (dwResultType == -1) { // -1 = problem dwError = LDAP_NO_SUCH_OBJECT; LWNET_LOG_VERBOSE("Caught LDAP_NO_SUCH_OBJECT Error on ldap search"); } else { // returns result type if (dwResultType != LDAP_RES_SEARCH_ENTRY) { dwError = LDAP_NO_SUCH_OBJECT; LWNET_LOG_DEBUG("Caught incorrect result type on ldap search: %d", dwError); } } dwError = LwMapLdapErrorToLwError(dwError); BAIL_ON_LWNET_ERROR(dwError); dwError = LwLdapGetBytes( pContext->hDirectory, pMessage, NETLOGON_LDAP_ATTRIBUTE_NAME, &pNetlogonAttributeValue, &dwNetlogonAttributeSize); BAIL_ON_LWNET_ERROR(dwError); dwError = LWNetBuildDCInfo(pNetlogonAttributeValue, dwNetlogonAttributeSize, &pDcInfo); BAIL_ON_LWNET_ERROR(dwError); dwError = LWNetAllocateString(pContext->pServerInfo->pszAddress, &pDcInfo->pszDomainControllerAddress); BAIL_ON_LWNET_ERROR(dwError); pDcInfo->dwPingTime = (DWORD)(StopTime - pContext->StartTime); if (StopTime < pContext->StartTime) { LWNET_LOG_ERROR("Stop time is earlier than start time"); } if (!LWNetSrvIsMatchingDcInfo(pDcInfo, dwDsFlags)) { dwError = LW_ERROR_NO_SUCH_OBJECT; if (LWNetSrvIsMatchingDcInfo(pDcInfo, dwDsFlags & ~DS_WRITABLE_REQUIRED)) { // We found something, but it failed only because it did // not satisfy writability. bFailedFindWritable = TRUE; } } error: if (pMessage) { ldap_msgfree(pMessage); } LWNET_SAFE_FREE_MEMORY(pNetlogonAttributeValue); if (dwError) { LWNET_SAFE_FREE_DC_INFO(pDcInfo); LWNetSrvPingCLdapEnd(pContext); } *ppDcInfo = pDcInfo; *pbFailedFindWritable = bFailedFindWritable; return dwError; }
DWORD ParseArgs( int argc, char* argv[], PSTR* ppszTargetFQDN, PSTR* ppszSiteName, PSTR* ppszPrimaryDomain, PDWORD pdwFlags ) { typedef enum { PARSE_MODE_OPEN = 0, PARSE_MODE_SITENAME, PARSE_MODE_PREFERRED_DOMAIN, PARSE_MODE_OPTIONS } ParseMode; DWORD dwError = 0; int iArg = 1; PSTR pszArg = NULL; ParseMode parseMode = PARSE_MODE_OPEN; PSTR pszTargetFQDN = NULL; PSTR pszSiteName = NULL; PSTR pszPrimaryDomain = NULL; DWORD dwFlags = 0; do { pszArg = argv[iArg++]; if (IsNullOrEmptyString(pszArg)) { break; } switch (parseMode) { case PARSE_MODE_OPEN: if ((strcmp(pszArg, "--help") == 0) || (strcmp(pszArg, "-h") == 0)) { ShowUsage(); exit(0); } else { dwError = LWNetAllocateString(pszArg, &pszTargetFQDN); BAIL_ON_LWNET_ERROR(dwError); parseMode = PARSE_MODE_OPTIONS; } break; case PARSE_MODE_OPTIONS: if(strcmp(pszArg, "--site") == 0) { parseMode = PARSE_MODE_SITENAME; } else if(strcmp(pszArg, "--preferred-domain") == 0) { parseMode = PARSE_MODE_PREFERRED_DOMAIN; } else if(strcmp(pszArg, "--force") == 0) { dwError = AddFlag(DS_FORCE_REDISCOVERY, &dwFlags); BAIL_ON_LWNET_ERROR(dwError); } else if(strcmp(pszArg, "--ds-required") == 0) { dwError = AddFlag(DS_DIRECTORY_SERVICE_REQUIRED, &dwFlags); BAIL_ON_LWNET_ERROR(dwError); } else if(strcmp(pszArg, "--gc-required") == 0) { dwError = AddFlag(DS_GC_SERVER_REQUIRED, &dwFlags); BAIL_ON_LWNET_ERROR(dwError); } else if(strcmp(pszArg, "--pdc-required") == 0) { dwError = AddFlag(DS_PDC_REQUIRED, &dwFlags); BAIL_ON_LWNET_ERROR(dwError); } else if(strcmp(pszArg, "--background-only") == 0) { dwError = AddFlag(DS_BACKGROUND_ONLY, &dwFlags); BAIL_ON_LWNET_ERROR(dwError); } else if(strcmp(pszArg, "--kdc-required") == 0) { dwError = AddFlag(DS_KDC_REQUIRED, &dwFlags); BAIL_ON_LWNET_ERROR(dwError); } else if(strcmp(pszArg, "--timeserv-required") == 0) { dwError = AddFlag(DS_TIMESERV_REQUIRED, &dwFlags); BAIL_ON_LWNET_ERROR(dwError); } else if(strcmp(pszArg, "--writeable-required") == 0) { dwError = AddFlag(DS_WRITABLE_REQUIRED, &dwFlags); BAIL_ON_LWNET_ERROR(dwError); } else if(strcmp(pszArg, "--good-timeserv-required") == 0) { dwError = AddFlag(DS_GOOD_TIMESERV_REQUIRED, &dwFlags); BAIL_ON_LWNET_ERROR(dwError); } else if(strcmp(pszArg, "--avoid-self") == 0) { dwError = AddFlag(DS_AVOID_SELF, &dwFlags); BAIL_ON_LWNET_ERROR(dwError); } else { LWNET_LOG_ERROR("Invalid argument: %s", pszArg); dwError = ERROR_INVALID_PARAMETER; BAIL_ON_LWNET_ERROR(dwError); } break; case PARSE_MODE_SITENAME: if(!IsNullOrEmptyString(pszSiteName)) { LWNET_LOG_ERROR("Invalid argument: %s", pszArg); dwError = ERROR_INVALID_PARAMETER; BAIL_ON_LWNET_ERROR(dwError); } dwError = LWNetAllocateString(pszArg, &pszSiteName); BAIL_ON_LWNET_ERROR(dwError); parseMode = PARSE_MODE_OPTIONS; break; case PARSE_MODE_PREFERRED_DOMAIN: if(!IsNullOrEmptyString(pszPrimaryDomain)) { LWNET_LOG_ERROR("Invalid argument: %s", pszArg); dwError = ERROR_INVALID_PARAMETER; BAIL_ON_LWNET_ERROR(dwError); } dwError = LWNetAllocateString(pszArg, &pszPrimaryDomain); BAIL_ON_LWNET_ERROR(dwError); parseMode = PARSE_MODE_OPTIONS; break; } } while (iArg < argc); if(IsNullOrEmptyString(pszTargetFQDN)) { ShowUsage(); exit(0); } error: if (dwError) { LWNET_SAFE_FREE_STRING(pszTargetFQDN); LWNET_SAFE_FREE_STRING(pszSiteName); LWNET_SAFE_FREE_STRING(pszPrimaryDomain); dwFlags = 0; } *ppszTargetFQDN = pszTargetFQDN; *ppszSiteName = pszSiteName; *ppszPrimaryDomain = pszPrimaryDomain; *pdwFlags = dwFlags; return dwError; }
DWORD LWNetDnsBuildServerArray( IN PDLINKEDLIST pSrvRecordList, OUT PDNS_SERVER_INFO* ppServerArray, OUT PDWORD pdwServerCount ) { DWORD dwError = 0; DWORD dwServerCount = 0; PDNS_SERVER_INFO pServerArray = NULL; PDLINKEDLIST pListMember = NULL; PDNS_SRV_INFO_RECORD pSrvRecord = NULL; DWORD dwServerIndex = 0; DWORD dwStringSize = 0; DWORD dwRequiredSize = 0; PSTR pStringLocation = NULL; for (pListMember = pSrvRecordList; pListMember; pListMember = pListMember->pNext) { pSrvRecord = (PDNS_SRV_INFO_RECORD)pListMember->pItem; dwStringSize += strlen(pSrvRecord->pszAddress) + 1; dwStringSize += strlen(pSrvRecord->pszTarget) + 1; dwServerCount++; } if (dwServerCount < 1) { // nothing to do, so we are done dwError = 0; goto error; } dwRequiredSize = dwServerCount * sizeof(DNS_SERVER_INFO) + dwStringSize; dwError = LWNetAllocateMemory(dwRequiredSize, (PVOID*)&pServerArray); BAIL_ON_LWNET_ERROR(dwError); pStringLocation = CT_PTR_ADD(pServerArray, dwServerCount * sizeof(DNS_SERVER_INFO)); dwServerIndex = 0; for (pListMember = pSrvRecordList; pListMember; pListMember = pListMember->pNext) { PSTR source; pSrvRecord = (PDNS_SRV_INFO_RECORD)pListMember->pItem; // Copy the strings into the buffer pServerArray[dwServerIndex].pszAddress = pStringLocation; for (source = pSrvRecord->pszAddress; source[0]; source++) { pStringLocation[0] = source[0]; pStringLocation++; } pStringLocation[0] = source[0]; pStringLocation++; pServerArray[dwServerIndex].pszName = pStringLocation; for (source = pSrvRecord->pszTarget; source[0]; source++) { pStringLocation[0] = source[0]; pStringLocation++; } pStringLocation[0] = source[0]; pStringLocation++; dwServerIndex++; } // TODO: Turns this into ASSERT if (CT_PTR_OFFSET(pServerArray, pStringLocation) != dwRequiredSize) { LWNET_LOG_ERROR("ASSERT - potential buffer overflow"); } error: if (dwError) { LWNET_SAFE_FREE_MEMORY(pServerArray); dwServerCount = 0; } *ppServerArray = pServerArray; *pdwServerCount = dwServerCount; return dwError; }
int main( int argc, char* argv[] ) { DWORD dwError = 0; PSTR pszHostName = NULL; PWSTR pwszHostName = NULL; PWSTR pwszCanonName = NULL; PSTR pszCanonName = NULL; PLWNET_RESOLVE_ADDR *ppAddressList = NULL; DWORD dwAddressListLen = 0; DWORD i = 0; CHAR ipAddressBuf[INET_ADDRSTRLEN]; PCSTR pszAddress = NULL; DWORD ipAddressLen = 0; DWORD ipAddrFamily = 0; PBYTE pIpAddr = NULL; lwnet_init_logging_to_file(LWNET_LOG_LEVEL_VERBOSE, TRUE, ""); ParseArgs(argc, argv); if (argc == 1) { printf("usage: %s hostname\n", argv[0]); return 0; } pszHostName = argv[1]; dwError = LwRtlWC16StringAllocateFromCString(&pwszHostName, pszHostName); BAIL_ON_LWNET_ERROR(dwError); dwError = LWNetResolveName( (PCWSTR) pwszHostName, &pwszCanonName, &ppAddressList, &dwAddressListLen); BAIL_ON_LWNET_ERROR(dwError); dwError = LwRtlCStringAllocateFromWC16String(&pszCanonName, pwszCanonName); BAIL_ON_LWNET_ERROR(dwError); for (i=0; i<dwAddressListLen; i++) { if (ppAddressList[i]->AddressType == LWNET_IP_ADDR_V4) { ipAddressLen = 4; ipAddrFamily = PF_INET; pIpAddr = ppAddressList[i]->Address.Ip4Addr; } else if (ppAddressList[i]->AddressType == LWNET_IP_ADDR_V6) { ipAddressLen = 16; ipAddrFamily = PF_INET6; pIpAddr = ppAddressList[i]->Address.Ip4Addr; } pszAddress = inet_ntop(ipAddrFamily, pIpAddr, ipAddressBuf, sizeof(ipAddressBuf)); if (pszAddress) { printf("IP Address = %s\n", pszAddress); } } printf("Responses = %d Host: '%s'\n", dwAddressListLen, pszCanonName); cleanup: LWNetResolveNameFree(pwszCanonName, ppAddressList, dwAddressListLen); LWNET_SAFE_FREE_MEMORY(pwszHostName); LWNET_SAFE_FREE_STRING(pszCanonName); return (dwError); error: if (dwError == ERROR_BAD_NET_NAME) { printf("LWNetResolveName() failed DNS/NetBIOS name resolution\n"); } else { LWNET_LOG_ERROR("Failed communication with likewise-netlogond. " "Error code [%d]\n", dwError); } goto cleanup; }