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
32 /** UUID we are looking up */
33 const std::string uuid;
35 /** True if the lookup is forward, false if is a reverse lookup
40 /** Create a resolver.
41 * @param mgr DNS Manager
42 * @param me this module
43 * @param user The user to begin lookup on
44 * @param to_resolve The IP or host to resolve
45 * @param qt The query type
47 UserResolver(DNS::Manager* mgr, Module* me, LocalUser* user, const std::string& to_resolve, DNS::QueryType qt)
48 : DNS::Request(mgr, me, to_resolve, qt)
50 , fwd(qt == DNS::QUERY_A || qt == DNS::QUERY_AAAA)
54 /** Called on successful lookup
55 * if a previous result has already come back.
56 * @param r The finished query
58 void OnLookupComplete(const DNS::Query* r) CXX11_OVERRIDE
60 LocalUser* bound_user = IS_LOCAL(ServerInstance->FindUUID(uuid));
63 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Resolution finished for user '%s' who is gone", uuid.c_str());
67 const DNS::ResourceRecord* ans_record = r->FindAnswerOfType(this->question.type);
68 if (ans_record == NULL)
74 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());
78 UserResolver* res_forward;
79 if (bound_user->client_sa.family() == AF_INET6)
81 /* IPV6 forward lookup */
82 res_forward = new UserResolver(this->manager, this->creator, bound_user, ans_record->rdata, DNS::QUERY_AAAA);
87 res_forward = new UserResolver(this->manager, this->creator, bound_user, ans_record->rdata, DNS::QUERY_A);
91 this->manager->Process(res_forward);
93 catch (DNS::Exception& e)
96 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Error in resolver: " + e.GetReason());
98 bound_user->WriteNotice("*** There was an internal error resolving your host, using your IP address (" + bound_user->GetIPString() + ") instead.");
99 dl->set(bound_user, 0);
104 /* Both lookups completed */
106 irc::sockets::sockaddrs* user_ip = &bound_user->client_sa;
107 bool rev_match = false;
108 if (user_ip->family() == AF_INET6)
110 struct in6_addr res_bin;
111 if (inet_pton(AF_INET6, ans_record->rdata.c_str(), &res_bin))
113 rev_match = !memcmp(&user_ip->in6.sin6_addr, &res_bin, sizeof(res_bin));
118 struct in_addr res_bin;
119 if (inet_pton(AF_INET, ans_record->rdata.c_str(), &res_bin))
121 rev_match = !memcmp(&user_ip->in4.sin_addr, &res_bin, sizeof(res_bin));
125 dl->set(bound_user, 0);
129 std::string* hostname = &(this->question.name);
131 if (hostname == NULL)
133 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: User has no hostname attached when doing a forward lookup");
134 bound_user->WriteNotice("*** There was an internal error resolving your host, using your IP address (" + bound_user->GetIPString() + ") instead.");
137 else if (hostname->length() <= ServerInstance->Config->Limits.MaxHost)
139 /* Hostnames starting with : are not a good thing (tm) */
140 if ((*hostname)[0] == ':')
141 hostname->insert(0, "0");
143 bound_user->WriteNotice("*** Found your hostname (" + *hostname + (r->cached ? ") -- cached" : ")"));
144 bound_user->ChangeRealHost(hostname->substr(0, ServerInstance->Config->Limits.MaxHost), true);
148 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.");
153 bound_user->WriteNotice("*** Your hostname does not match up with your IP address. Sorry, using your IP address (" + bound_user->GetIPString() + ") instead.");
158 /** Called on failed lookup
159 * @param query The errored query
161 void OnError(const DNS::Query* query) CXX11_OVERRIDE
163 LocalUser* bound_user = IS_LOCAL(ServerInstance->FindUUID(uuid));
166 bound_user->WriteNotice("*** Could not resolve your hostname: " + this->manager->GetErrorStr(query->error) + "; using your IP address (" + bound_user->GetIPString() + ") instead.");
167 dl->set(bound_user, 0);
172 class ModuleHostnameLookup : public Module
175 LocalIntExt dnsLookup;
176 dynamic_reference<DNS::Manager> DNS;
179 ModuleHostnameLookup()
180 : dnsLookup("dnsLookup", ExtensionItem::EXT_USER, this)
186 void OnSetUserIP(LocalUser* user) CXX11_OVERRIDE
188 // If core_dns is not loaded or hostname resolution is disabled for the user's
189 // connect class then the logic in this function does not apply.
190 if (!DNS || !user->MyClass->resolvehostnames)
193 // Clients can't have a DNS hostname if they aren't connected via IPv4 or IPv6.
194 if (user->client_sa.family() != AF_INET && user->client_sa.family() != AF_INET6)
197 user->WriteNotice("*** Looking up your hostname...");
199 UserResolver* res_reverse = new UserResolver(*this->DNS, this, user, user->GetIPString(), DNS::QUERY_PTR);
202 /* If both the reverse and forward queries are cached, the user will be able to pass DNS completely
203 * before Process() completes, which is why dnsLookup.set() is here, before Process()
205 this->dnsLookup.set(user, 1);
206 this->DNS->Process(res_reverse);
208 catch (DNS::Exception& e)
210 this->dnsLookup.set(user, 0);
212 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Error in resolver: " + e.GetReason());
216 ModResult OnCheckReady(LocalUser* user) CXX11_OVERRIDE
218 return this->dnsLookup.get(user) ? MOD_RES_DENY : MOD_RES_PASSTHRU;
221 Version GetVersion() CXX11_OVERRIDE
223 return Version("Provides support for DNS lookups on connecting clients", VF_CORE|VF_VENDOR);
227 MODULE_INIT(ModuleHostnameLookup)