2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 UserResolver::UserResolver(LocalUser* user, std::string to_resolve, QueryType qt, bool &cache) :
23 Resolver(to_resolve, qt, cache, NULL), uuid(user->uuid)
25 this->fwd = (qt == DNS_QUERY_A || qt == DNS_QUERY_AAAA);
28 void UserResolver::OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
30 UserResolver *res_forward; // for forward-resolution
31 LocalUser* bound_user = (LocalUser*)ServerInstance->FindUUID(uuid);
34 ServerInstance->Logs->Log("RESOLVER", DEBUG, "Resolution finished for user '%s' who is gone", uuid.c_str());
38 ServerInstance->Logs->Log("RESOLVER", DEBUG, "DNS result for %s: '%s' -> '%s'", uuid.c_str(), input.c_str(), result.c_str());
42 // first half of resolution is done. We now need to verify that the host matches.
43 bound_user->stored_host = result;
46 /* Check we didnt time out */
47 if (bound_user->registered != REG_ALL)
50 if (bound_user->client_sa.sa.sa_family == AF_INET6)
52 /* IPV6 forward lookup */
53 res_forward = new UserResolver(bound_user, result, DNS_QUERY_AAAA, lcached);
58 res_forward = new UserResolver(bound_user, result, DNS_QUERY_A, lcached);
60 ServerInstance->AddResolver(res_forward, lcached);
63 catch (CoreException& e)
65 ServerInstance->Logs->Log("RESOLVER", DEBUG,"Error in resolver: %s",e.GetReason());
70 /* Both lookups completed */
72 irc::sockets::sockaddrs* user_ip = &bound_user->client_sa;
73 bool rev_match = false;
74 if (user_ip->sa.sa_family == AF_INET6)
76 struct in6_addr res_bin;
77 if (inet_pton(AF_INET6, result.c_str(), &res_bin))
79 rev_match = !memcmp(&user_ip->in6.sin6_addr, &res_bin, sizeof(res_bin));
84 struct in_addr res_bin;
85 if (inet_pton(AF_INET, result.c_str(), &res_bin))
87 rev_match = !memcmp(&user_ip->in4.sin_addr, &res_bin, sizeof(res_bin));
93 std::string hostname = bound_user->stored_host;
94 if (hostname.length() < 65)
96 /* Check we didnt time out */
97 if ((bound_user->registered != REG_ALL) && (!bound_user->dns_done))
99 /* Hostnames starting with : are not a good thing (tm) */
100 if (hostname[0] == ':')
101 hostname.insert(0, "0");
103 bound_user->WriteServ("NOTICE Auth :*** Found your hostname (%s)%s", hostname.c_str(), (cached ? " -- cached" : ""));
104 bound_user->dns_done = true;
105 bound_user->dhost.assign(hostname, 0, 64);
106 bound_user->host.assign(hostname, 0, 64);
107 /* Invalidate cache */
108 bound_user->InvalidateCache();
113 if (!bound_user->dns_done)
115 bound_user->WriteServ("NOTICE Auth :*** Your hostname is longer than the maximum of 64 characters, using your IP address (%s) instead.", bound_user->GetIPString());
116 bound_user->dns_done = true;
122 if (!bound_user->dns_done)
124 bound_user->WriteServ("NOTICE Auth :*** Your hostname does not match up with your IP address. Sorry, using your IP address (%s) instead.", bound_user->GetIPString());
125 bound_user->dns_done = true;
129 // Save some memory by freeing this up; it's never used again in the user's lifetime.
130 bound_user->stored_host.resize(0);
134 void UserResolver::OnError(ResolverError e, const std::string &errormessage)
136 LocalUser* bound_user = (LocalUser*)ServerInstance->FindUUID(uuid);
139 bound_user->WriteServ("NOTICE Auth :*** Could not resolve your hostname: %s; using your IP address (%s) instead.", errormessage.c_str(), bound_user->GetIPString());
140 bound_user->dns_done = true;
141 bound_user->stored_host.resize(0);
142 ServerInstance->stats->statsDnsBad++;