From 4845b2185f9829a018354543f14672691abcb107 Mon Sep 17 00:00:00 2001 From: RvdH Date: Mon, 4 Oct 2021 15:01:13 +0200 Subject: [PATCH] 5.7.0-DNSServer INI Setting(s) Boolean UseDNSCache String DNSServer --- .../Common/Application/IniFileSettings.cpp | 6 ++- .../Common/Application/IniFileSettings.h | 4 ++ .../Server/Common/TCPIP/DNSResolverWinApi.cpp | 49 ++++++++++++++++++- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/hmailserver/source/Server/Common/Application/IniFileSettings.cpp b/hmailserver/source/Server/Common/Application/IniFileSettings.cpp index e4a2b1cea..92962fd91 100644 --- a/hmailserver/source/Server/Common/Application/IniFileSettings.cpp +++ b/hmailserver/source/Server/Common/Application/IniFileSettings.cpp @@ -59,7 +59,8 @@ namespace HM blocked_iphold_seconds_(0), smtpdmax_size_drop_(0), backup_messages_dbonly_(false), - add_xauth_user_ip_(false) + add_xauth_user_ip_(false), + use_dns_cache_(true) { @@ -194,7 +195,8 @@ namespace HM smtpdmax_size_drop_ = ReadIniSettingInteger_("Settings", "SMTPDMaxSizeDrop",0); backup_messages_dbonly_ = ReadIniSettingInteger_("Settings", "BackupMessagesDBOnly",0) == 1; add_xauth_user_ip_ = ReadIniSettingInteger_("Settings", "AddXAuthUserIP",1) == 1; - + use_dns_cache_ = ReadIniSettingInteger_("Settings", "UseDNSCache", 1) == 1; + dns_server_ = ReadIniSettingString_("Settings", "DNSServer", ""); rewrite_envelope_from_when_forwarding_ = ReadIniSettingInteger_("Settings", "RewriteEnvelopeFromWhenForwarding", 0) == 1; m_sDisableAUTHList = ReadIniSettingString_("Settings", "DisableAUTHList", ""); } diff --git a/hmailserver/source/Server/Common/Application/IniFileSettings.h b/hmailserver/source/Server/Common/Application/IniFileSettings.h index 00f368a74..daac8f4e1 100644 --- a/hmailserver/source/Server/Common/Application/IniFileSettings.h +++ b/hmailserver/source/Server/Common/Application/IniFileSettings.h @@ -110,6 +110,8 @@ namespace HM bool GetBackupMessagesDBOnly () const { return backup_messages_dbonly_; } bool GetAddXAuthUserIP () const { return add_xauth_user_ip_; } bool GetRewriteEnvelopeFromWhenForwarding() const { return rewrite_envelope_from_when_forwarding_; } + bool GetUseDNSCache() const { return use_dns_cache_; } + String GetDNSServer() const { return dns_server_; } std::set GetAuthDisabledOnPorts(); private: @@ -188,6 +190,8 @@ namespace HM bool backup_messages_dbonly_; bool add_xauth_user_ip_; bool rewrite_envelope_from_when_forwarding_; + bool use_dns_cache_; + String dns_server_; String database_provider_; String m_sDisableAUTHList; diff --git a/hmailserver/source/Server/Common/TCPIP/DNSResolverWinApi.cpp b/hmailserver/source/Server/Common/TCPIP/DNSResolverWinApi.cpp index 24670bfe2..57eed19c3 100644 --- a/hmailserver/source/Server/Common/TCPIP/DNSResolverWinApi.cpp +++ b/hmailserver/source/Server/Common/TCPIP/DNSResolverWinApi.cpp @@ -65,7 +65,51 @@ namespace HM { PDNS_RECORD pDnsRecord = NULL; - DNS_STATUS nDnsStatus = DnsQuery(query, resourceType, DNS_QUERY_STANDARD, NULL, &pDnsRecord, NULL); + PIP4_ARRAY pSrvList = NULL; + + DWORD fOptions; + fOptions = DNS_QUERY_STANDARD; + + if (!IniFileSettings::Instance()->GetUseDNSCache()) + { + fOptions += DNS_QUERY_BYPASS_CACHE; + } + + AnsiString sCustomDNS; + sCustomDNS = IniFileSettings::Instance()->GetDNSServer().Trim(); + if (!sCustomDNS.IsEmpty()) + { + pSrvList = (PIP4_ARRAY)malloc(sizeof(IP4_ARRAY)); + if (!pSrvList) { + + String sMessage; + sMessage.Format(_T("Unable to allocate memory for DNS server list. Query: %s, Type: %d."), query, resourceType); + ErrorManager::Instance()->ReportError(ErrorManager::Low, 4401, "DNSResolver::_Resolve", sMessage); + + return false; + } + + // Custom DNSServer IPv4 address + pSrvList->AddrCount = 1; + pSrvList->AddrArray[0] = inet_addr(sCustomDNS.c_str()); //Custom DNS server IP address + if (pSrvList->AddrArray[0] == INADDR_NONE) { + + String sMessage; + sMessage.Format(_T("Invalid DNSServer IP address. DNSServer IP: %hs."), sCustomDNS.c_str()); + ErrorManager::Instance()->ReportError(ErrorManager::Low, 4401, "DNSResolver::_Resolve", sMessage); + + // fallback to the system dns servers + pSrvList = NULL; + } + else + { + // We need this if not using system dns servers + if (fOptions != DNS_QUERY_BYPASS_CACHE) + fOptions += DNS_QUERY_BYPASS_CACHE; + } + } + + DNS_STATUS nDnsStatus = DnsQuery(query, resourceType, fOptions, pSrvList, &pDnsRecord, NULL); PDNS_RECORD pDnsRecordsToDelete = pDnsRecord; @@ -186,6 +230,9 @@ namespace HM _FreeDNSRecord(pDnsRecordsToDelete); pDnsRecordsToDelete = 0; + if (pSrvList != NULL) + free(pSrvList); + std::sort(foundRecords.begin(), foundRecords.end(), SortDnsRecordsByPreference);