VOID VmDnsReadString( PCSTR szPrompt, PSTR szString, int len, BOOLEAN bHideString ) { sigset_t sig, osig; struct termios ts, ots; PSTR pszNl = NULL; if (bHideString) { sigemptyset(&sig); sigaddset(&sig, SIGINT); sigaddset(&sig, SIGTSTP); sigprocmask(SIG_BLOCK, &sig, &osig); tcgetattr(fileno(stdin), &ts); ots = ts; ts.c_lflag &= ~(ECHO); tcsetattr(fileno(stdin), TCSANOW, &ts); } if (szPrompt) { fputs(szPrompt, stderr); fflush(stderr); } if (fgets(szString, len, stdin) == NULL) { szString[0] = '\0'; } else { pszNl = VmDnsStringChrA(szString, '\n'); if (pszNl) { *pszNl = '\0'; } } if (bHideString) { fputs("\n", stderr); tcsetattr(fileno(stdin), TCSANOW, &ots); sigprocmask(SIG_SETMASK, &osig, NULL); } fflush(stderr); }
VOID VmDnsReadString( PCSTR szPrompt, PSTR szString, int len, BOOLEAN bHideString ) { DWORD oldMode = 0; HANDLE hConIn = INVALID_HANDLE_VALUE; PSTR pszNl = NULL; if (bHideString) { hConIn = GetStdHandle(STD_INPUT_HANDLE); if (hConIn != INVALID_HANDLE_VALUE) { if (GetConsoleMode(hConIn, &oldMode)) { SetConsoleMode(hConIn, oldMode & ~ENABLE_ECHO_INPUT); } } } if (szPrompt) { fputs(szPrompt, stderr); fflush(stderr); } if (fgets(szString, len, stdin) == NULL) { szString[0] = '\0'; } else { pszNl = VmDnsStringChrA(szString, '\n'); if (pszNl) { *pszNl = '\0'; } } if (bHideString) { fputs("\n", stderr); SetConsoleMode(hConIn, oldMode); } fflush(stderr); }
static BOOLEAN VmDnsValidateForwarder( PCSTR pszForwarder ) { if (!pszForwarder || pszForwarder[0] == '\0' || VmDnsStringChrA(pszForwarder, '.') == NULL ) { return FALSE; } return TRUE; }
static DWORD VmDnsCliCreatePrimaryZone( PVM_DNS_CLI_CONTEXT pContext ) { DWORD dwError = 0; PSTR pszMboxDomain = NULL; VMDNS_ZONE_INFO zoneInfo = { 0 }; VMDNS_RECORD nsRecord = {0}; VMDNS_RECORD addrRecord = {0}; PSTR pszTargetFQDN = NULL; int ret = 0; int af = AF_INET; unsigned char buf[sizeof(struct in6_addr)]; if (IsNullOrEmptyString(pContext->pszZone)) { fprintf(stderr, "Error: DNS Zone is not specified\n"); dwError = ERROR_INVALID_PARAMETER; BAIL_ON_VMDNS_ERROR(dwError); } if (IsNullOrEmptyString(pContext->pszNSHost) || VmDnsCheckIfIPV4AddressA(pContext->pszNSHost) || VmDnsCheckIfIPV6AddressA(pContext->pszNSHost)) { fprintf(stderr, "Error: Primary Nameserver host is not specified or the format is invalid\n"); dwError = ERROR_INVALID_PARAMETER; BAIL_ON_VMDNS_ERROR(dwError); } if (pContext->dwZoneType == VMDNS_ZONE_TYPE_FORWARD && IsNullOrEmptyString(pContext->pszNSIp)) { fprintf(stderr, "Error: Primary Nameserver IP Address is not specified\n"); dwError = ERROR_INVALID_PARAMETER; BAIL_ON_VMDNS_ERROR(dwError); } if (IsNullOrEmptyString(pContext->pszMboxDomain)) { dwError = VmDnsAllocateStringA("hostmaster", &pszMboxDomain); BAIL_ON_VMDNS_ERROR(dwError); } dwError = VmDnsMakeFQDN(pContext->pszNSHost, pContext->pszZone, &pszTargetFQDN); BAIL_ON_VMDNS_ERROR(dwError); zoneInfo.pszName = pContext->pszZone; zoneInfo.pszPrimaryDnsSrvName = pszTargetFQDN; zoneInfo.pszRName = pszMboxDomain ? pszMboxDomain : pContext->pszMboxDomain; zoneInfo.serial = 1; zoneInfo.refreshInterval = VMDNS_DEFAULT_REFRESH_INTERVAL; zoneInfo.retryInterval = VMDNS_DEFAULT_RETRY_INTERVAL; zoneInfo.expire = VMDNS_DEFAULT_EXPIRE; zoneInfo.minimum = VMDNS_DEFAULT_TTL; zoneInfo.dwFlags = 0; zoneInfo.dwZoneType = pContext->dwZoneType; dwError = VmDnsCreateZoneA(pContext->pServerContext, &zoneInfo); BAIL_ON_VMDNS_ERROR(dwError); if (!IsNullOrEmptyString(pContext->pszNSHost)) { nsRecord.pszName = pContext->pszZone; nsRecord.Data.NS.pNameHost = pszTargetFQDN; nsRecord.iClass = VMDNS_CLASS_IN; nsRecord.pszName = pContext->pszZone; nsRecord.dwType = VMDNS_RR_TYPE_NS; nsRecord.dwTtl = pContext->record.dwTtl; dwError = VmDnsAddRecordA( pContext->pServerContext, pContext->pszZone, &nsRecord); BAIL_ON_VMDNS_ERROR(dwError); } if (!IsNullOrEmptyString(pContext->pszNSHost) && !IsNullOrEmptyString(pContext->pszNSIp) && pContext->dwZoneType == VMDNS_ZONE_TYPE_FORWARD) { addrRecord.iClass = VMDNS_CLASS_IN; addrRecord.pszName = pszTargetFQDN; addrRecord.dwType = VMDNS_RR_TYPE_A; addrRecord.dwTtl = pContext->record.dwTtl; if (VmDnsStringChrA(pContext->pszNSIp, ':')) { af = AF_INET6; } ret = inet_pton(af, pContext->pszNSIp, buf); if (ret <= 0) { dwError = ERROR_INVALID_PARAMETER; BAIL_ON_VMDNS_ERROR(dwError); } if (af == AF_INET) { addrRecord.Data.A.IpAddress = (VMDNS_IP4_ADDRESS)ntohl(((struct in_addr *)buf)->s_addr); } else if (af == AF_INET6) { addrRecord.dwType = VMDNS_RR_TYPE_AAAA; dwError = VmDnsCopyMemory( addrRecord.Data.AAAA.Ip6Address.IP6Byte, sizeof(addrRecord.Data.AAAA.Ip6Address.IP6Byte), #ifdef _WIN32 ((struct in6_addr*)buf)->u.Byte, #else ((struct in6_addr*)buf)->s6_addr, #endif sizeof(addrRecord.Data.AAAA.Ip6Address.IP6Byte)); BAIL_ON_VMDNS_ERROR(dwError); } else { dwError = ERROR_INVALID_PARAMETER; BAIL_ON_VMDNS_ERROR(dwError); } dwError = VmDnsAddRecordA( pContext->pServerContext, pContext->pszZone, &addrRecord); BAIL_ON_VMDNS_ERROR(dwError); } if (!IsNullOrEmptyString(pContext->pszNSHost) && !IsNullOrEmptyString(pContext->pszNSIp) && pContext->dwZoneType == VMDNS_ZONE_TYPE_REVERSE) { addrRecord.iClass = VMDNS_CLASS_IN; addrRecord.dwType = VMDNS_RR_TYPE_PTR; addrRecord.dwTtl = VMDNS_DEFAULT_TTL; addrRecord.Data.PTR.pNameHost = pszTargetFQDN; dwError = VmDnsGeneratePtrNameFromIp(pContext->pszNSIp,&addrRecord.pszName); BAIL_ON_VMDNS_ERROR(dwError); dwError = VmDnsAddRecordA( pContext->pServerContext, pContext->pszZone, &addrRecord ); BAIL_ON_VMDNS_ERROR(dwError); } cleanup: if (pszMboxDomain) { VmDnsFreeStringA(pszMboxDomain); } return dwError; error: goto cleanup; }
DWORD VmDnsGenerateReversZoneNameFromNetworkId( PCSTR pszNetworkId, PSTR* ppszZone ) { DWORD dwError = 0; PSTR pszPtrName = NULL; PSTR pszZone = NULL; PCHAR pLength = NULL; int length = 0; int family = AF_INET; BAIL_ON_VMDNS_INVALID_POINTER(pszNetworkId, dwError); BAIL_ON_VMDNS_INVALID_POINTER(ppszZone, dwError); pLength = VmDnsStringChrA(pszNetworkId, '/'); if (!pLength || !*(pLength + 1)) { dwError = ERROR_INVALID_PARAMETER; BAIL_ON_VMDNS_ERROR(dwError); } *pLength = '\0'; ++pLength; length = atoi(pLength); dwError = VmDnsGenerateRtrNameFromIp(pszNetworkId, &family, &pszPtrName); BAIL_ON_VMDNS_ERROR(dwError); if (family != AF_INET && family != AF_INET6) { dwError = ERROR_INVALID_PARAMETER; BAIL_ON_VMDNS_ERROR(dwError); } if (family == AF_INET) { if (length != 8 && length != 16 && length != 24) { dwError = ERROR_INVALID_PARAMETER; BAIL_ON_VMDNS_ERROR(dwError); } pszZone = pszPtrName; while (length < 32 && *pszZone) { pszZone = VmDnsStringChrA(pszZone, '.'); if (!pszZone) { dwError = ERROR_INVALID_PARAMETER; BAIL_ON_VMDNS_ERROR(dwError); } ++pszZone; length += 8; } if (!*pszZone) { dwError = ERROR_INVALID_PARAMETER; BAIL_ON_VMDNS_ERROR(dwError); } } else { if (length >= 128 || length <= 0) { dwError = ERROR_INVALID_PARAMETER; BAIL_ON_VMDNS_ERROR(dwError); } pszZone = pszPtrName + 64 - length/2; } dwError = VmDnsAllocateStringA(pszZone, ppszZone); BAIL_ON_VMDNS_ERROR(dwError); cleanup: return dwError; error: goto cleanup; }