/*
* InspIRCd -- Internet Relay Chat Daemon
*
- * Copyright (C) 2013 Adam <Adam@anope.org>
- * Copyright (C) 2003-2013 Anope Team <team@anope.org>
+ * Copyright (C) 2019 Robby <robby@chatbelgie.be>
+ * Copyright (C) 2015, 2017-2020 Sadie Powell <sadie@witchery.services>
+ * Copyright (C) 2013-2016 Attila Molnar <attilamolnar@hush.com>
+ * Copyright (C) 2013, 2015-2016 Adam <Adam@anope.org>
*
* This file is part of InspIRCd. InspIRCd is free software: you can
* redistribute it and/or modify it under the terms of the GNU General Public
using namespace DNS;
-/** A full packet sent or recieved to/from the nameserver
+/** A full packet sent or received to/from the nameserver
*/
class Packet : public Query
{
- static bool IsValidName(const std::string& name)
- {
- return (name.find_first_not_of("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-") == std::string::npos);
- }
-
void PackName(unsigned char* output, unsigned short output_size, unsigned short& pos, const std::string& name)
{
if (pos + name.length() + 2 > output_size)
record.ttl = (input[pos] << 24) | (input[pos + 1] << 16) | (input[pos + 2] << 8) | input[pos + 3];
pos += 4;
- //record.rdlength = input[pos] << 8 | input[pos + 1];
+ uint16_t rdlength = input[pos] << 8 | input[pos + 1];
pos += 2;
switch (record.type)
case QUERY_PTR:
{
record.rdata = this->UnpackName(input, input_size, pos);
- if (!IsValidName(record.rdata))
+ if (!InspIRCd::IsHost(record.rdata))
throw Exception("Invalid name"); // XXX: Causes the request to time out
break;
}
+ case QUERY_TXT:
+ {
+ if (pos + rdlength > input_size)
+ throw Exception("Unable to unpack txt resource record");
+
+ record.rdata = std::string(reinterpret_cast<const char *>(input + pos), rdlength);
+ pos += rdlength;
+
+ if (record.rdata.find_first_of("\r\n\0", 0, 3) != std::string::npos)
+ throw Exception("Invalid character in txt record");
+
+ break;
+ }
default:
break;
}
cache_map cache;
irc::sockets::sockaddrs myserver;
+ bool unloading;
/** Maximum number of entries in cache
*/
DNS::Request* requests[MAX_REQUEST_ID+1];
MyManager(Module* c) : Manager(c), Timer(5*60, true)
+ , unloading(false)
{
for (unsigned int i = 0; i <= MAX_REQUEST_ID; ++i)
requests[i] = NULL;
~MyManager()
{
+ // Ensure Process() will fail for new requests
+ unloading = true;
+
for (unsigned int i = 0; i <= MAX_REQUEST_ID; ++i)
{
DNS::Request* request = requests[i];
if (!request)
continue;
- Query rr(*request);
+ Query rr(request->question);
rr.error = ERROR_UNKNOWN;
request->OnError(&rr);
}
}
- void Process(DNS::Request* req)
+ void Process(DNS::Request* req) CXX11_OVERRIDE
{
- ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Processing request to lookup " + req->name + " of type " + ConvToStr(req->type) + " to " + this->myserver.addr());
+ if ((unloading) || (req->creator->dying))
+ throw Exception("Module is being unloaded");
+
+ ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Processing request to lookup " + req->question.name + " of type " + ConvToStr(req->question.type) + " to " + this->myserver.addr());
/* Create an id */
unsigned int tries = 0;
Packet p;
p.flags = QUERYFLAGS_RD;
p.id = req->id;
- p.question = *req;
+ p.question = req->question;
unsigned char buffer[524];
unsigned short len = p.Pack(buffer, sizeof(buffer));
}
// Update name in the original request so question checking works for PTR queries
- req->name = p.question.name;
+ req->question.name = p.question.name;
- if (SocketEngine::SendTo(this, buffer, len, 0, &this->myserver.sa, this->myserver.sa_size()) != len)
+ if (SocketEngine::SendTo(this, buffer, len, 0, this->myserver) != len)
throw Exception("DNS: Unable to send query");
// Add timer for timeout
ServerInstance->Timers.AddTimer(req);
}
- void RemoveRequest(DNS::Request* req)
+ void RemoveRequest(DNS::Request* req) CXX11_OVERRIDE
{
if (requests[req->id] == req)
requests[req->id] = NULL;
}
- std::string GetErrorStr(Error e)
+ std::string GetErrorStr(Error e) CXX11_OVERRIDE
{
switch (e)
{
}
}
+ std::string GetTypeStr(QueryType qt) CXX11_OVERRIDE
+ {
+ switch (qt)
+ {
+ case QUERY_A:
+ return "A";
+ case QUERY_AAAA:
+ return "AAAA";
+ case QUERY_CNAME:
+ return "CNAME";
+ case QUERY_PTR:
+ return "PTR";
+ case QUERY_TXT:
+ return "TXT";
+ default:
+ return "UNKNOWN";
+ }
+ }
+
void OnEventHandlerError(int errcode) CXX11_OVERRIDE
{
ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "UDP socket got an error event");
return;
}
- if (static_cast<Question&>(*request) != recv_packet.question)
+ if (request->question != recv_packet.question)
{
// This can happen under high latency, drop it silently, do not fail the request
ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Received an answer that isn't for a question we asked");
}
else
{
- ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Lookup complete for " + request->name);
+ ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Lookup complete for " + request->question.name);
ServerInstance->stats.DnsGood++;
request->OnLookupComplete(&recv_packet);
this->AddCache(recv_packet);
delete request;
}
- bool Tick(time_t now)
+ bool Tick(time_t now) CXX11_OVERRIDE
{
- ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "cache: purging DNS cache");
-
+ unsigned long expired = 0;
for (cache_map::iterator it = this->cache.begin(); it != this->cache.end(); )
{
const Query& query = it->second;
if (IsExpired(query, now))
+ {
+ expired++;
this->cache.erase(it++);
+ }
else
++it;
}
+
+ if (expired)
+ ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "cache: purged %lu expired DNS entries", expired);
+
return true;
}
void Rehash(const std::string& dnsserver, std::string sourceaddr, unsigned int sourceport)
{
- if (this->GetFd() > -1)
+ if (this->HasFd())
{
SocketEngine::Shutdown(this, 2);
SocketEngine::Close(this);
- /* Remove expired entries from the cache */
- this->Tick(ServerInstance->Time());
+ // Remove all entries from the cache.
+ cache.clear();
}
irc::sockets::aptosa(dnsserver, DNS::PORT, myserver);
/* Initialize mastersocket */
- int s = socket(myserver.sa.sa_family, SOCK_DGRAM, 0);
+ int s = socket(myserver.family(), SOCK_DGRAM, 0);
this->SetFd(s);
/* Have we got a socket? */
if (sourceaddr.empty())
{
// set a sourceaddr for irc::sockets::aptosa() based on the servers af type
- if (myserver.sa.sa_family == AF_INET)
+ if (myserver.family() == AF_INET)
sourceaddr = "0.0.0.0";
- else if (myserver.sa.sa_family == AF_INET6)
+ else if (myserver.family() == AF_INET6)
sourceaddr = "::";
}
irc::sockets::aptosa(sourceaddr, sourceport, bindto);
this->SetFd(-1);
}
- if (bindto.sa.sa_family != myserver.sa.sa_family)
+ if (bindto.family() != myserver.family())
ServerInstance->Logs->Log(MODNAME, LOG_SPARSE, "Nameserver address family differs from source address family - hostnames might not resolve");
}
else
{
#ifdef _WIN32
// attempt to look up their nameserver from the system
- ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "WARNING: <dns:server> not defined, attempting to find a working server in the system settings...");
+ ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WARNING: <dns:server> not defined, attempting to find a working server in the system settings...");
PFIXED_INFO pFixedInfo;
DWORD dwBufferSize = sizeof(FIXED_INFO);
if (!DNSServer.empty())
{
- ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "<dns:server> set to '%s' as first active resolver in the system settings.", DNSServer.c_str());
+ ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "<dns:server> set to '%s' as first active resolver in the system settings.", DNSServer.c_str());
return;
}
}
- ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "No viable nameserver found! Defaulting to nameserver '127.0.0.1'!");
+ ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "No viable nameserver found! Defaulting to nameserver '127.0.0.1'!");
#else
// attempt to look up their nameserver from /etc/resolv.conf
- ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "WARNING: <dns:server> not defined, attempting to find working server in /etc/resolv.conf...");
+ ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WARNING: <dns:server> not defined, attempting to find working server in /etc/resolv.conf...");
std::ifstream resolv("/etc/resolv.conf");
resolv >> DNSServer;
if (DNSServer.find_first_not_of("0123456789.") == std::string::npos || DNSServer.find_first_not_of("0123456789ABCDEFabcdef:") == std::string::npos)
{
- ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "<dns:server> set to '%s' as first resolver in /etc/resolv.conf.",DNSServer.c_str());
+ ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "<dns:server> set to '%s' as first resolver in /etc/resolv.conf.",DNSServer.c_str());
return;
}
}
}
- ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "/etc/resolv.conf contains no viable nameserver entries! Defaulting to nameserver '127.0.0.1'!");
+ ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "/etc/resolv.conf contains no viable nameserver entries! Defaulting to nameserver '127.0.0.1'!");
#endif
DNSServer = "127.0.0.1";
}
ConfigTag* tag = ServerInstance->Config->ConfValue("dns");
DNSServer = tag->getString("server");
SourceIP = tag->getString("sourceip");
- SourcePort = tag->getInt("sourceport", 0, 0, 65535);
+ SourcePort = tag->getUInt("sourceport", 0, 0, UINT16_MAX);
if (DNSServer.empty())
FindDNSServer();
this->manager.Rehash(DNSServer, SourceIP, SourcePort);
}
- void OnUnloadModule(Module* mod)
+ void OnUnloadModule(Module* mod) CXX11_OVERRIDE
{
for (unsigned int i = 0; i <= MAX_REQUEST_ID; ++i)
{
if (req->creator == mod)
{
- Query rr(*req);
+ Query rr(req->question);
rr.error = ERROR_UNLOADED;
req->OnError(&rr);
}
}
- Version GetVersion()
+ Version GetVersion() CXX11_OVERRIDE
{
- return Version("DNS support", VF_CORE|VF_VENDOR);
+ return Version("Provides support for DNS lookups", VF_CORE|VF_VENDOR);
}
};