]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/user_resolver.cpp
Use the remote channel's capitalization on a losing TS merge
[user/henk/code/inspircd.git] / src / user_resolver.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 UserResolver::UserResolver(LocalUser* user, std::string to_resolve, QueryType qt, bool &cache) :
16         Resolver(to_resolve, qt, cache, NULL), uuid(user->uuid)
17 {
18         this->fwd = (qt == DNS_QUERY_A || qt == DNS_QUERY_AAAA);
19 }
20
21 void UserResolver::OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
22 {
23         UserResolver *res_forward; // for forward-resolution
24         LocalUser* bound_user = (LocalUser*)ServerInstance->FindUUID(uuid);
25         if (!bound_user)
26         {
27                 ServerInstance->Logs->Log("RESOLVER", DEBUG, "Resolution finished for user '%s' who is gone", uuid.c_str());
28                 return;
29         }
30
31         ServerInstance->Logs->Log("RESOLVER", DEBUG, "DNS result for %s: '%s' -> '%s'", uuid.c_str(), input.c_str(), result.c_str());
32
33         if (!fwd)
34         {
35                 // first half of resolution is done. We now need to verify that the host matches.
36                 bound_user->stored_host = result;
37                 try
38                 {
39                         /* Check we didnt time out */
40                         if (bound_user->registered != REG_ALL)
41                         {
42                                 bool lcached = false;
43                                 if (bound_user->client_sa.sa.sa_family == AF_INET6)
44                                 {
45                                         /* IPV6 forward lookup */
46                                         res_forward = new UserResolver(bound_user, result, DNS_QUERY_AAAA, lcached);
47                                 }
48                                 else
49                                 {
50                                         /* IPV4 lookup */
51                                         res_forward = new UserResolver(bound_user, result, DNS_QUERY_A, lcached);
52                                 }
53                                 ServerInstance->AddResolver(res_forward, lcached);
54                         }
55                 }
56                 catch (CoreException& e)
57                 {
58                         ServerInstance->Logs->Log("RESOLVER", DEBUG,"Error in resolver: %s",e.GetReason());
59                 }
60         }
61         else
62         {
63                 /* Both lookups completed */
64
65                 irc::sockets::sockaddrs* user_ip = &bound_user->client_sa;
66                 bool rev_match = false;
67                 if (user_ip->sa.sa_family == AF_INET6)
68                 {
69                         struct in6_addr res_bin;
70                         if (inet_pton(AF_INET6, result.c_str(), &res_bin))
71                         {
72                                 rev_match = !memcmp(&user_ip->in6.sin6_addr, &res_bin, sizeof(res_bin));
73                         }
74                 }
75                 else
76                 {
77                         struct in_addr res_bin;
78                         if (inet_pton(AF_INET, result.c_str(), &res_bin))
79                         {
80                                 rev_match = !memcmp(&user_ip->in4.sin_addr, &res_bin, sizeof(res_bin));
81                         }
82                 }
83                 
84                 if (rev_match)
85                 {
86                         std::string hostname = bound_user->stored_host;
87                         if (hostname.length() < 65)
88                         {
89                                 /* Check we didnt time out */
90                                 if ((bound_user->registered != REG_ALL) && (!bound_user->dns_done))
91                                 {
92                                         /* Hostnames starting with : are not a good thing (tm) */
93                                         if (hostname[0] == ':')
94                                                 hostname.insert(0, "0");
95
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();
102                                 }
103                         }
104                         else
105                         {
106                                 if (!bound_user->dns_done)
107                                 {
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;
110                                 }
111                         }
112                 }
113                 else
114                 {
115                         if (!bound_user->dns_done)
116                         {
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;
119                         }
120                 }
121
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);
124         }
125 }
126
127 void UserResolver::OnError(ResolverError e, const std::string &errormessage)
128 {
129         LocalUser* bound_user = (LocalUser*)ServerInstance->FindUUID(uuid);
130         if (bound_user)
131         {
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++;
136         }
137 }