]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/user_resolver.cpp
Merge pull request #1018 from SaberUK/insp20+hidekills
[user/henk/code/inspircd.git] / src / user_resolver.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
6  *
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.
10  *
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
14  * details.
15  *
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/>.
18  */
19
20
21 #include "inspircd.h"
22 UserResolver::UserResolver(LocalUser* user, std::string to_resolve, QueryType qt, bool &cache) :
23         Resolver(to_resolve, qt, cache, NULL), uuid(user->uuid)
24 {
25         this->fwd = (qt == DNS_QUERY_A || qt == DNS_QUERY_AAAA);
26 }
27
28 void UserResolver::OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
29 {
30         UserResolver *res_forward; // for forward-resolution
31         LocalUser* bound_user = (LocalUser*)ServerInstance->FindUUID(uuid);
32         if (!bound_user)
33         {
34                 ServerInstance->Logs->Log("RESOLVER", DEBUG, "Resolution finished for user '%s' who is gone", uuid.c_str());
35                 return;
36         }
37
38         ServerInstance->Logs->Log("RESOLVER", DEBUG, "DNS result for %s: '%s' -> '%s'", uuid.c_str(), input.c_str(), result.c_str());
39
40         if (!fwd)
41         {
42                 // first half of resolution is done. We now need to verify that the host matches.
43                 bound_user->stored_host = result;
44                 try
45                 {
46                         /* Check we didnt time out */
47                         if (bound_user->registered != REG_ALL)
48                         {
49                                 bool lcached = false;
50                                 if (bound_user->client_sa.sa.sa_family == AF_INET6)
51                                 {
52                                         /* IPV6 forward lookup */
53                                         res_forward = new UserResolver(bound_user, result, DNS_QUERY_AAAA, lcached);
54                                 }
55                                 else
56                                 {
57                                         /* IPV4 lookup */
58                                         res_forward = new UserResolver(bound_user, result, DNS_QUERY_A, lcached);
59                                 }
60                                 ServerInstance->AddResolver(res_forward, lcached);
61                         }
62                 }
63                 catch (CoreException& e)
64                 {
65                         ServerInstance->Logs->Log("RESOLVER", DEBUG,"Error in resolver: %s",e.GetReason());
66                 }
67         }
68         else
69         {
70                 /* Both lookups completed */
71
72                 irc::sockets::sockaddrs* user_ip = &bound_user->client_sa;
73                 bool rev_match = false;
74                 if (user_ip->sa.sa_family == AF_INET6)
75                 {
76                         struct in6_addr res_bin;
77                         if (inet_pton(AF_INET6, result.c_str(), &res_bin))
78                         {
79                                 rev_match = !memcmp(&user_ip->in6.sin6_addr, &res_bin, sizeof(res_bin));
80                         }
81                 }
82                 else
83                 {
84                         struct in_addr res_bin;
85                         if (inet_pton(AF_INET, result.c_str(), &res_bin))
86                         {
87                                 rev_match = !memcmp(&user_ip->in4.sin_addr, &res_bin, sizeof(res_bin));
88                         }
89                 }
90                 
91                 if (rev_match)
92                 {
93                         std::string hostname = bound_user->stored_host;
94                         if (hostname.length() < 65)
95                         {
96                                 /* Check we didnt time out */
97                                 if ((bound_user->registered != REG_ALL) && (!bound_user->dns_done))
98                                 {
99                                         /* Hostnames starting with : are not a good thing (tm) */
100                                         if (hostname[0] == ':')
101                                                 hostname.insert(0, "0");
102
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();
109                                 }
110                         }
111                         else
112                         {
113                                 if (!bound_user->dns_done)
114                                 {
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;
117                                 }
118                         }
119                 }
120                 else
121                 {
122                         if (!bound_user->dns_done)
123                         {
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;
126                         }
127                 }
128
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);
131         }
132 }
133
134 void UserResolver::OnError(ResolverError e, const std::string &errormessage)
135 {
136         LocalUser* bound_user = (LocalUser*)ServerInstance->FindUUID(uuid);
137         if (bound_user)
138         {
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++;
143         }
144 }