2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013-2016 Adam <Adam@anope.org>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "modules/dns.h"
29 /** Derived from Resolver, and performs user forward/reverse lookups.
31 class UserResolver : public DNS::Request
33 /** UUID we are looking up */
34 const std::string uuid;
36 /** True if the lookup is forward, false if is a reverse lookup
41 /** Create a resolver.
42 * @param mgr DNS Manager
43 * @param me this module
44 * @param user The user to begin lookup on
45 * @param to_resolve The IP or host to resolve
46 * @param qt The query type
48 UserResolver(DNS::Manager* mgr, Module* me, LocalUser* user, const std::string& to_resolve, DNS::QueryType qt)
49 : DNS::Request(mgr, me, to_resolve, qt)
51 , fwd(qt == DNS::QUERY_A || qt == DNS::QUERY_AAAA)
55 /** Called on successful lookup
56 * if a previous result has already come back.
57 * @param r The finished query
59 void OnLookupComplete(const DNS::Query* r) CXX11_OVERRIDE
61 LocalUser* bound_user = (LocalUser*)ServerInstance->FindUUID(uuid);
64 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Resolution finished for user '%s' who is gone", uuid.c_str());
68 const DNS::ResourceRecord* ans_record = r->FindAnswerOfType(this->question.type);
69 if (ans_record == NULL)
75 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "DNS result for %s: '%s' -> '%s'", uuid.c_str(), ans_record->name.c_str(), ans_record->rdata.c_str());
79 // first half of resolution is done. We now need to verify that the host matches.
80 ph->set(bound_user, ans_record->rdata);
82 UserResolver* res_forward;
83 if (bound_user->client_sa.family() == AF_INET6)
85 /* IPV6 forward lookup */
86 res_forward = new UserResolver(this->manager, this->creator, bound_user, ans_record->rdata, DNS::QUERY_AAAA);
91 res_forward = new UserResolver(this->manager, this->creator, bound_user, ans_record->rdata, DNS::QUERY_A);
95 this->manager->Process(res_forward);
97 catch (DNS::Exception& e)
100 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Error in resolver: " + e.GetReason());
102 bound_user->WriteNotice("*** There was an internal error resolving your host, using your IP address (" + bound_user->GetIPString() + ") instead.");
103 dl->set(bound_user, 0);
108 /* Both lookups completed */
110 irc::sockets::sockaddrs* user_ip = &bound_user->client_sa;
111 bool rev_match = false;
112 if (user_ip->family() == AF_INET6)
114 struct in6_addr res_bin;
115 if (inet_pton(AF_INET6, ans_record->rdata.c_str(), &res_bin))
117 rev_match = !memcmp(&user_ip->in6.sin6_addr, &res_bin, sizeof(res_bin));
122 struct in_addr res_bin;
123 if (inet_pton(AF_INET, ans_record->rdata.c_str(), &res_bin))
125 rev_match = !memcmp(&user_ip->in4.sin_addr, &res_bin, sizeof(res_bin));
129 dl->set(bound_user, 0);
133 std::string* hostname = ph->get(bound_user);
135 if (hostname == NULL)
137 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: User has no hostname attached when doing a forward lookup");
138 bound_user->WriteNotice("*** There was an internal error resolving your host, using your IP address (" + bound_user->GetIPString() + ") instead.");
141 else if (hostname->length() <= ServerInstance->Config->Limits.MaxHost)
143 /* Hostnames starting with : are not a good thing (tm) */
144 if ((*hostname)[0] == ':')
145 hostname->insert(0, "0");
147 bound_user->WriteNotice("*** Found your hostname (" + *hostname + (r->cached ? ") -- cached" : ")"));
148 bound_user->ChangeRealHost(hostname->substr(0, ServerInstance->Config->Limits.MaxHost), true);
152 bound_user->WriteNotice("*** Your hostname is longer than the maximum of " + ConvToStr(ServerInstance->Config->Limits.MaxHost) + " characters, using your IP address (" + bound_user->GetIPString() + ") instead.");
155 ph->unset(bound_user);
159 bound_user->WriteNotice("*** Your hostname does not match up with your IP address. Sorry, using your IP address (" + bound_user->GetIPString() + ") instead.");
164 /** Called on failed lookup
165 * @param query The errored query
167 void OnError(const DNS::Query* query) CXX11_OVERRIDE
169 LocalUser* bound_user = (LocalUser*)ServerInstance->FindUUID(uuid);
172 bound_user->WriteNotice("*** Could not resolve your hostname: " + this->manager->GetErrorStr(query->error) + "; using your IP address (" + bound_user->GetIPString() + ") instead.");
173 dl->set(bound_user, 0);
178 class ModuleHostnameLookup : public Module
180 LocalIntExt dnsLookup;
181 LocalStringExt ptrHosts;
182 dynamic_reference<DNS::Manager> DNS;
185 ModuleHostnameLookup()
186 : dnsLookup("dnsLookup", ExtensionItem::EXT_USER, this)
187 , ptrHosts("ptrHosts", ExtensionItem::EXT_USER, this)
194 void OnSetUserIP(LocalUser* user) CXX11_OVERRIDE
196 if (!DNS || !user->MyClass->resolvehostnames)
198 user->WriteNotice("*** Skipping host resolution (disabled by server administrator)");
202 // Clients can't have a DNS hostname if they aren't connected via IPv4 or IPv6.
203 if (user->client_sa.family() != AF_INET && user->client_sa.family() != AF_INET6)
205 user->WriteNotice("*** Skipping host resolution (connected via a non-IP socket)");
209 user->WriteNotice("*** Looking up your hostname...");
211 UserResolver* res_reverse = new UserResolver(*this->DNS, this, user, user->GetIPString(), DNS::QUERY_PTR);
214 /* If both the reverse and forward queries are cached, the user will be able to pass DNS completely
215 * before Process() completes, which is why dnsLookup.set() is here, before Process()
217 this->dnsLookup.set(user, 1);
218 this->DNS->Process(res_reverse);
220 catch (DNS::Exception& e)
222 this->dnsLookup.set(user, 0);
224 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Error in resolver: " + e.GetReason());
228 ModResult OnCheckReady(LocalUser* user) CXX11_OVERRIDE
230 return this->dnsLookup.get(user) ? MOD_RES_DENY : MOD_RES_PASSTHRU;
233 Version GetVersion() CXX11_OVERRIDE
235 return Version("Provides support for DNS lookups on connecting clients", VF_CORE|VF_VENDOR);
239 MODULE_INIT(ModuleHostnameLookup)