]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/user_resolver.cpp
820d7ef6551e7f6c6271da4049f82a78ce5d7dea
[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://www.inspircd.org/wiki/index.php/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, int resultnum)
25 {
26         /* We are only interested in the first matching result */
27         if (resultnum)
28                 return;
29
30         UserResolver *res_forward; // for forward-resolution
31
32         if ((!this->fwd) && (ServerInstance->SE->GetRef(this->bound_fd) == this->bound_user))
33         {
34                 this->bound_user->stored_host = result;
35                 try
36                 {
37                         /* Check we didnt time out */
38                         if (this->bound_user->registered != REG_ALL)
39                         {
40                                 bool lcached = false;
41 #ifdef IPV6
42                                 if (this->bound_user->GetProtocolFamily() == AF_INET6)
43                                 {
44                                         /* IPV6 forward lookup (with possibility of 4in6) */
45                                         const char* ip = this->bound_user->GetIPString();
46                                         res_forward = new UserResolver(this->ServerInstance, this->bound_user, result, (!strncmp(ip, "0::ffff:", 8) ? DNS_QUERY_A : DNS_QUERY_AAAA), lcached);
47                                 }
48                                 else
49                                         /* IPV4 lookup (mixed protocol mode) */
50 #endif
51                                 {
52                                         /* IPV4 lookup (ipv4 only mode) */
53                                         res_forward = new UserResolver(this->ServerInstance, this->bound_user, result, DNS_QUERY_A, lcached);
54                                 }
55                                 this->ServerInstance->AddResolver(res_forward, lcached);
56                         }
57                 }
58                 catch (CoreException& e)
59                 {
60                         ServerInstance->Logs->Log("RESOLVER", DEBUG,"Error in resolver: %s",e.GetReason());
61                 }
62         }
63         else if ((this->fwd) && (ServerInstance->SE->GetRef(this->bound_fd) == this->bound_user))
64         {
65                 /* Both lookups completed */
66                 std::string result2("0::ffff:");
67                 result2.append(result);
68                 if (this->bound_user->GetIPString() == result || this->bound_user->GetIPString() == result2)
69                 {
70                         std::string hostname = this->bound_user->stored_host;
71                         if (hostname.length() < 65)
72                         {
73                                 /* Check we didnt time out */
74                                 if ((this->bound_user->registered != REG_ALL) && (!this->bound_user->dns_done))
75                                 {
76                                         /* Hostnames starting with : are not a good thing (tm) */
77                                         if (hostname[0] == ':')
78                                                 hostname.insert(0, "0");
79
80                                         this->bound_user->WriteServ("NOTICE Auth :*** Found your hostname (%s)%s", hostname.c_str(), (cached ? " -- cached" : ""));
81                                         this->bound_user->dns_done = true;
82                                         this->bound_user->dhost.assign(hostname, 0, 64);
83                                         this->bound_user->host.assign(hostname, 0, 64);
84                                         /* Invalidate cache */
85                                         this->bound_user->InvalidateCache();
86                                 }
87                         }
88                         else
89                         {
90                                 if (!this->bound_user->dns_done)
91                                 {
92                                         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());
93                                         this->bound_user->dns_done = true;
94                                 }
95                         }
96                 }
97                 else
98                 {
99                         if (!this->bound_user->dns_done)
100                         {
101                                 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());
102                                 this->bound_user->dns_done = true;
103                         }
104                 }
105                 
106                 // Save some memory by freeing this up; it's never used again in the user's lifetime.
107                 this->bound_user->stored_host.resize(0);
108         }
109 }
110
111 void UserResolver::OnError(ResolverError e, const std::string &errormessage)
112 {
113         if (ServerInstance->SE->GetRef(this->bound_fd) == this->bound_user)
114         {
115                 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());
116                 this->bound_user->dns_done = true;
117                 this->bound_user->stored_host.resize(0);
118                 ServerInstance->stats->statsDnsBad++;
119         }
120 }