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"
28 /** Derived from Resolver, and performs user forward/reverse lookups.
30 class UserResolver : public DNS::Request
33 /** UUID we are looking up */
34 const std::string uuid;
37 /** Create a resolver.
38 * @param mgr DNS Manager
39 * @param me this module
40 * @param user The user to begin lookup on
41 * @param to_resolve The IP or host to resolve
42 * @param qt The query type
44 UserResolver(DNS::Manager* mgr, Module* me, LocalUser* user, const std::string& to_resolve, DNS::QueryType qt)
45 : DNS::Request(mgr, me, to_resolve, qt)
50 /** Called on successful lookup
51 * if a previous result has already come back.
52 * @param r The finished query
54 void OnLookupComplete(const DNS::Query* r) CXX11_OVERRIDE
56 LocalUser* bound_user = IS_LOCAL(ServerInstance->FindUUID(uuid));
59 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Resolution finished for user '%s' who is gone", uuid.c_str());
63 const DNS::ResourceRecord* ans_record = r->FindAnswerOfType(this->question.type);
64 if (ans_record == NULL)
70 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "DNS %s result for %s: '%s' -> '%s'%s",
71 this->manager->GetTypeStr(question.type).c_str(), uuid.c_str(),
72 ans_record->name.c_str(), ans_record->rdata.c_str(),
73 r->cached ? " (cached)" : "");
75 if (this->question.type == DNS::QUERY_PTR)
77 UserResolver* res_forward;
78 if (bound_user->client_sa.family() == AF_INET6)
80 /* IPV6 forward lookup */
81 res_forward = new UserResolver(this->manager, this->creator, bound_user, ans_record->rdata, DNS::QUERY_AAAA);
86 res_forward = new UserResolver(this->manager, this->creator, bound_user, ans_record->rdata, DNS::QUERY_A);
90 this->manager->Process(res_forward);
92 catch (DNS::Exception& e)
95 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Error in resolver: " + e.GetReason());
97 bound_user->WriteNotice("*** There was an internal error resolving your host, using your IP address (" + bound_user->GetIPString() + ") instead.");
98 dl->set(bound_user, 0);
101 else if (this->question.type == DNS::QUERY_A || this->question.type == DNS::QUERY_AAAA)
103 /* Both lookups completed */
105 irc::sockets::sockaddrs* user_ip = &bound_user->client_sa;
106 bool rev_match = false;
107 if (user_ip->family() == AF_INET6)
109 struct in6_addr res_bin;
110 if (inet_pton(AF_INET6, ans_record->rdata.c_str(), &res_bin))
112 rev_match = !memcmp(&user_ip->in6.sin6_addr, &res_bin, sizeof(res_bin));
117 struct in_addr res_bin;
118 if (inet_pton(AF_INET, ans_record->rdata.c_str(), &res_bin))
120 rev_match = !memcmp(&user_ip->in4.sin_addr, &res_bin, sizeof(res_bin));
124 dl->set(bound_user, 0);
128 bound_user->WriteNotice("*** Found your hostname (" + this->question.name + (r->cached ? ") -- cached" : ")"));
129 bound_user->ChangeRealHost(this->question.name, true);
133 bool display_is_real = irc::equals(bound_user->GetDisplayedHost(), bound_user->GetRealHost());
134 bound_user->WriteNotice("*** Your hostname does not match up with your IP address. Sorry, using your IP address (" + bound_user->GetIPString() + ") instead.");
135 bound_user->ChangeRealHost(bound_user->GetIPString(), display_is_real);
140 /** Called on failed lookup
141 * @param query The errored query
143 void OnError(const DNS::Query* query) CXX11_OVERRIDE
145 LocalUser* bound_user = IS_LOCAL(ServerInstance->FindUUID(uuid));
148 bound_user->WriteNotice("*** Could not resolve your hostname: " + this->manager->GetErrorStr(query->error) + "; using your IP address (" + bound_user->GetIPString() + ") instead.");
149 dl->set(bound_user, 0);
154 class ModuleHostnameLookup : public Module
157 LocalIntExt dnsLookup;
158 dynamic_reference<DNS::Manager> DNS;
161 ModuleHostnameLookup()
162 : dnsLookup("dnsLookup", ExtensionItem::EXT_USER, this)
168 void OnSetUserIP(LocalUser* user) CXX11_OVERRIDE
170 // If core_dns is not loaded or hostname resolution is disabled for the user's
171 // connect class then the logic in this function does not apply.
172 if (!DNS || !user->MyClass->resolvehostnames)
175 // Clients can't have a DNS hostname if they aren't connected via IPv4 or IPv6.
176 if (user->client_sa.family() != AF_INET && user->client_sa.family() != AF_INET6)
179 user->WriteNotice("*** Looking up your hostname...");
181 UserResolver* res_reverse = new UserResolver(*this->DNS, this, user, user->GetIPString(), DNS::QUERY_PTR);
184 /* If both the reverse and forward queries are cached, the user will be able to pass DNS completely
185 * before Process() completes, which is why dnsLookup.set() is here, before Process()
187 this->dnsLookup.set(user, 1);
188 this->DNS->Process(res_reverse);
190 catch (DNS::Exception& e)
192 this->dnsLookup.set(user, 0);
194 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Error in resolver: " + e.GetReason());
198 ModResult OnCheckReady(LocalUser* user) CXX11_OVERRIDE
200 return this->dnsLookup.get(user) ? MOD_RES_DENY : MOD_RES_PASSTHRU;
203 Version GetVersion() CXX11_OVERRIDE
205 return Version("Provides support for DNS lookups on connecting clients", VF_CORE|VF_VENDOR);
209 MODULE_INIT(ModuleHostnameLookup)