]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/user_resolver.cpp
b37d112feef60824326a8c6fb31070836a218ec4
[user/henk/code/inspircd.git] / src / user_resolver.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 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 /* $Core */
15
16 #include "inspircd.h"
17 UserResolver::UserResolver(InspIRCd* Instance, User* user, std::string to_resolve, QueryType qt, bool &cache) :
18         Resolver(Instance, to_resolve, qt, cache), bound_user(user)
19 {
20         this->fwd = (qt == DNS_QUERY_A || qt == DNS_QUERY_AAAA);
21         this->bound_fd = user->GetFd();
22 }
23
24 void UserResolver::OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
25 {
26         UserResolver *res_forward; // for forward-resolution
27
28         if ((!this->fwd) && (ServerInstance->SE->GetRef(this->bound_fd) == this->bound_user))
29         {
30                 this->bound_user->stored_host = result;
31                 try
32                 {
33                         /* Check we didnt time out */
34                         if (this->bound_user->registered != REG_ALL)
35                         {
36                                 bool lcached = false;
37                                 if (this->bound_user->client_sa.sa.sa_family == AF_INET6)
38                                 {
39                                         /* IPV6 forward lookup */
40                                         res_forward = new UserResolver(this->ServerInstance, this->bound_user, result, DNS_QUERY_AAAA, lcached);
41                                 }
42                                 else
43                                 {
44                                         /* IPV4 lookup */
45                                         res_forward = new UserResolver(this->ServerInstance, this->bound_user, result, DNS_QUERY_A, lcached);
46                                 }
47                                 this->ServerInstance->AddResolver(res_forward, lcached);
48                         }
49                 }
50                 catch (CoreException& e)
51                 {
52                         ServerInstance->Logs->Log("RESOLVER", DEBUG,"Error in resolver: %s",e.GetReason());
53                 }
54         }
55         else if ((this->fwd) && (ServerInstance->SE->GetRef(this->bound_fd) == this->bound_user))
56         {
57                 /* Both lookups completed */
58
59                 irc::sockets::sockaddrs* user_ip = &this->bound_user->client_sa;
60                 bool rev_match = false;
61                 if (user_ip->sa.sa_family == AF_INET6)
62                 {
63                         struct in6_addr res_bin;
64                         if (inet_pton(AF_INET6, result.c_str(), &res_bin))
65                         {
66                                 rev_match = !memcmp(&user_ip->in6.sin6_addr, &res_bin, sizeof(res_bin));
67                         }
68                 }
69                 else
70                 {
71                         struct in_addr res_bin;
72                         if (inet_pton(AF_INET, result.c_str(), &res_bin))
73                         {
74                                 rev_match = !memcmp(&user_ip->in4.sin_addr, &res_bin, sizeof(res_bin));
75                         }
76                 }
77                 
78                 if (rev_match)
79                 {
80                         std::string hostname = this->bound_user->stored_host;
81                         if (hostname.length() < 65)
82                         {
83                                 /* Check we didnt time out */
84                                 if ((this->bound_user->registered != REG_ALL) && (!this->bound_user->dns_done))
85                                 {
86                                         /* Hostnames starting with : are not a good thing (tm) */
87                                         if (hostname[0] == ':')
88                                                 hostname.insert(0, "0");
89
90                                         this->bound_user->WriteServ("NOTICE Auth :*** Found your hostname (%s)%s", hostname.c_str(), (cached ? " -- cached" : ""));
91                                         this->bound_user->dns_done = true;
92                                         this->bound_user->dhost.assign(hostname, 0, 64);
93                                         this->bound_user->host.assign(hostname, 0, 64);
94                                         /* Invalidate cache */
95                                         this->bound_user->InvalidateCache();
96                                 }
97                         }
98                         else
99                         {
100                                 if (!this->bound_user->dns_done)
101                                 {
102                                         this->bound_user->WriteServ("NOTICE Auth :*** Your hostname is longer than the maximum of 64 characters, using your IP address (%s) instead.", this->bound_user->GetIPString());
103                                         this->bound_user->dns_done = true;
104                                 }
105                         }
106                 }
107                 else
108                 {
109                         if (!this->bound_user->dns_done)
110                         {
111                                 this->bound_user->WriteServ("NOTICE Auth :*** Your hostname does not match up with your IP address. Sorry, using your IP address (%s) instead.", this->bound_user->GetIPString());
112                                 this->bound_user->dns_done = true;
113                         }
114                 }
115
116                 // Save some memory by freeing this up; it's never used again in the user's lifetime.
117                 this->bound_user->stored_host.resize(0);
118         }
119 }
120
121 void UserResolver::OnError(ResolverError e, const std::string &errormessage)
122 {
123         if (ServerInstance->SE->GetRef(this->bound_fd) == this->bound_user)
124         {
125                 this->bound_user->WriteServ("NOTICE Auth :*** Could not resolve your hostname: %s; using your IP address (%s) instead.", errormessage.c_str(), this->bound_user->GetIPString());
126                 this->bound_user->dns_done = true;
127                 this->bound_user->stored_host.resize(0);
128                 ServerInstance->stats->statsDnsBad++;
129         }
130 }