Skip to content

Commit

Permalink
5.7.0-DNSServer INI Setting(s)
Browse files Browse the repository at this point in the history
Boolean UseDNSCache
String DNSServer
  • Loading branch information
RvdHout authored and martinknafve committed Jan 15, 2022
1 parent fe5577e commit 4845b21
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
Expand Up @@ -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)

{

Expand Down Expand Up @@ -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", "");
}
Expand Down
Expand Up @@ -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<int> GetAuthDisabledOnPorts();

private:
Expand Down Expand Up @@ -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;
Expand Down
49 changes: 48 additions & 1 deletion hmailserver/source/Server/Common/TCPIP/DNSResolverWinApi.cpp
Expand Up @@ -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;

Expand Down Expand Up @@ -186,6 +230,9 @@ namespace HM
_FreeDNSRecord(pDnsRecordsToDelete);
pDnsRecordsToDelete = 0;

if (pSrvList != NULL)
free(pSrvList);

std::sort(foundRecords.begin(), foundRecords.end(), SortDnsRecordsByPreference);


Expand Down

0 comments on commit 4845b21

Please sign in to comment.