2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013 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)
61 LocalUser* bound_user = (LocalUser*)ServerInstance->FindUUID(uuid);
64 ServerInstance->Logs->Log("RESOLVER", LOG_DEBUG, "Resolution finished for user '%s' who is gone", uuid.c_str());
68 const DNS::ResourceRecord& ans_record = r->answers[0];
70 ServerInstance->Logs->Log("RESOLVER", LOG_DEBUG, "DNS result for %s: '%s' -> '%s'", uuid.c_str(), ans_record.name.c_str(), ans_record.rdata.c_str());
74 // first half of resolution is done. We now need to verify that the host matches.
75 ph->set(bound_user, ans_record.rdata);
77 UserResolver* res_forward;
78 if (bound_user->client_sa.sa.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("RESOLVER", LOG_DEBUG, "Error in resolver: %s",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);
103 /* Both lookups completed */
105 irc::sockets::sockaddrs* user_ip = &bound_user->client_sa;
106 bool rev_match = false;
107 if (user_ip->sa.sa_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 std::string* hostname = ph->get(bound_user);
130 if (hostname == NULL)
132 ServerInstance->Logs->Log("RESOLVER", LOG_DEFAULT, "ERROR: User has no hostname attached when doing a forward lookup");
133 bound_user->WriteNotice("*** There was an internal error resolving your host, using your IP address (" + bound_user->GetIPString() + ") instead.");
136 else if (hostname->length() < 65)
138 /* Hostnames starting with : are not a good thing (tm) */
139 if ((*hostname)[0] == ':')
140 hostname->insert(0, "0");
142 bound_user->WriteNotice("*** Found your hostname (" + *hostname + (r->cached ? ") -- cached" : ")"));
143 bound_user->host.assign(*hostname, 0, 64);
144 bound_user->dhost = bound_user->host;
146 /* Invalidate cache */
147 bound_user->InvalidateCache();
151 bound_user->WriteNotice("*** Your hostname is longer than the maximum of 64 characters, using your IP address (" + bound_user->GetIPString() + ") instead.");
154 ph->unset(bound_user);
158 bound_user->WriteNotice("*** Your hostname does not match up with your IP address. Sorry, using your IP address (" + bound_user->GetIPString() + ") instead.");
163 /** Called on failed lookup
164 * @param query The errored query
166 void OnError(const DNS::Query* query)
168 LocalUser* bound_user = (LocalUser*)ServerInstance->FindUUID(uuid);
171 bound_user->WriteNotice("*** Could not resolve your hostname: " + this->manager->GetErrorStr(query->error) + "; using your IP address (" + bound_user->GetIPString() + ") instead.");
172 dl->set(bound_user, 0);
173 ServerInstance->stats->statsDnsBad++;
178 class ModuleHostnameLookup : public Module
180 LocalIntExt dnsLookup;
181 LocalStringExt ptrHosts;
182 dynamic_reference<DNS::Manager> DNS;
185 ModuleHostnameLookup()
186 : dnsLookup("dnsLookup", this)
187 , ptrHosts("ptrHosts", this)
197 ServerInstance->Modules->AddService(this->dnsLookup);
198 ServerInstance->Modules->AddService(this->ptrHosts);
200 Implementation i[] = { I_OnUserInit, I_OnCheckReady };
201 ServerInstance->Modules->Attach(i, this, sizeof(i) / sizeof(Implementation));
204 void OnUserInit(LocalUser *user)
206 if (!DNS || user->MyClass->nouserdns)
208 user->WriteNotice("*** Skipping host resolution (disabled by server administrator)");
212 user->WriteNotice("*** Looking up your hostname...");
214 UserResolver* res_reverse = new UserResolver(*this->DNS, this, user, user->GetIPString(), DNS::QUERY_PTR);
217 /* If both the reverse and forward queries are cached, the user will be able to pass DNS completely
218 * before Process() completes, which is why dnsLookup.set() is here, before Process()
220 this->dnsLookup.set(user, 1);
221 this->DNS->Process(res_reverse);
223 catch (DNS::Exception& e)
225 this->dnsLookup.set(user, 0);
227 ServerInstance->Logs->Log("USERS", LOG_DEBUG, "Error in resolver: %s", e.GetReason());
228 ServerInstance->stats->statsDnsBad++;
232 ModResult OnCheckReady(LocalUser* user)
234 return this->dnsLookup.get(user) ? MOD_RES_DENY : MOD_RES_PASSTHRU;
239 return Version("Provides support for DNS lookups on connecting clients", VF_CORE|VF_VENDOR);
243 MODULE_INIT(ModuleHostnameLookup)