1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
15 UserResolver::UserResolver(LocalUser* user, std::string to_resolve, QueryType qt, bool &cache) :
16 Resolver(to_resolve, qt, cache, NULL), uuid(user->uuid)
18 this->fwd = (qt == DNS_QUERY_A || qt == DNS_QUERY_AAAA);
21 void UserResolver::OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
23 UserResolver *res_forward; // for forward-resolution
24 LocalUser* bound_user = (LocalUser*)ServerInstance->FindUUID(uuid);
27 ServerInstance->Logs->Log("RESOLVER", DEBUG, "Resolution finished for user '%s' who is gone", uuid.c_str());
31 ServerInstance->Logs->Log("RESOLVER", DEBUG, "DNS result for %s: '%s' -> '%s'", uuid.c_str(), input.c_str(), result.c_str());
35 // first half of resolution is done. We now need to verify that the host matches.
36 bound_user->stored_host = result;
39 /* Check we didnt time out */
40 if (bound_user->registered != REG_ALL)
43 if (bound_user->client_sa.sa.sa_family == AF_INET6)
45 /* IPV6 forward lookup */
46 res_forward = new UserResolver(bound_user, result, DNS_QUERY_AAAA, lcached);
51 res_forward = new UserResolver(bound_user, result, DNS_QUERY_A, lcached);
53 ServerInstance->AddResolver(res_forward, lcached);
56 catch (CoreException& e)
58 ServerInstance->Logs->Log("RESOLVER", DEBUG,"Error in resolver: %s",e.GetReason());
63 /* Both lookups completed */
65 irc::sockets::sockaddrs* user_ip = &bound_user->client_sa;
66 bool rev_match = false;
67 if (user_ip->sa.sa_family == AF_INET6)
69 struct in6_addr res_bin;
70 if (inet_pton(AF_INET6, result.c_str(), &res_bin))
72 rev_match = !memcmp(&user_ip->in6.sin6_addr, &res_bin, sizeof(res_bin));
77 struct in_addr res_bin;
78 if (inet_pton(AF_INET, result.c_str(), &res_bin))
80 rev_match = !memcmp(&user_ip->in4.sin_addr, &res_bin, sizeof(res_bin));
86 std::string hostname = bound_user->stored_host;
87 if (hostname.length() < 65)
89 /* Check we didnt time out */
90 if ((bound_user->registered != REG_ALL) && (!bound_user->dns_done))
92 /* Hostnames starting with : are not a good thing (tm) */
93 if (hostname[0] == ':')
94 hostname.insert(0, "0");
96 bound_user->WriteServ("NOTICE Auth :*** Found your hostname (%s)%s", hostname.c_str(), (cached ? " -- cached" : ""));
97 bound_user->dns_done = true;
98 bound_user->dhost.assign(hostname, 0, 64);
99 bound_user->host.assign(hostname, 0, 64);
100 /* Invalidate cache */
101 bound_user->InvalidateCache();
106 if (!bound_user->dns_done)
108 bound_user->WriteServ("NOTICE Auth :*** Your hostname is longer than the maximum of 64 characters, using your IP address (%s) instead.", bound_user->GetIPString());
109 bound_user->dns_done = true;
115 if (!bound_user->dns_done)
117 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());
118 bound_user->dns_done = true;
122 // Save some memory by freeing this up; it's never used again in the user's lifetime.
123 bound_user->stored_host.resize(0);
127 void UserResolver::OnError(ResolverError e, const std::string &errormessage)
129 LocalUser* bound_user = (LocalUser*)ServerInstance->FindUUID(uuid);
132 bound_user->WriteServ("NOTICE Auth :*** Could not resolve your hostname: %s; using your IP address (%s) instead.", errormessage.c_str(), bound_user->GetIPString());
133 bound_user->dns_done = true;
134 bound_user->stored_host.resize(0);
135 ServerInstance->stats->statsDnsBad++;