]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/user_resolver.cpp
f23682b1408f8c221b31a51c1b80d9c726e9d68a
[user/henk/code/inspircd.git] / src / user_resolver.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 #include "inspircd.h"
15
16 UserResolver::UserResolver(InspIRCd* Instance, User* user, std::string to_resolve, QueryType qt, bool &cache) :
17         Resolver(Instance, to_resolve, qt, cache), bound_user(user)
18 {
19         this->fwd = (qt == DNS_QUERY_A || qt == DNS_QUERY_AAAA);
20         this->bound_fd = user->GetFd();
21 }
22
23 void UserResolver::OnLookupComplete(const std::string &result, unsigned int ttl, bool cached, int resultnum)
24 {
25         /* We are only interested in the first matching result */
26         if (resultnum)
27                 return;
28
29         if ((!this->fwd) && (ServerInstance->SE->GetRef(this->bound_fd) == this->bound_user))
30         {
31                 this->bound_user->stored_host = result;
32                 try
33                 {
34                         /* Check we didnt time out */
35                         if (this->bound_user->registered != REG_ALL)
36                         {
37                                 bool cached;
38 #ifdef IPV6
39                                 if (this->bound_user->GetProtocolFamily() == AF_INET6)
40                                 {
41                                         /* IPV6 forward lookup (with possibility of 4in6) */
42                                         const char* ip = this->bound_user->GetIPString();
43                                         bound_user->res_forward = new UserResolver(this->ServerInstance, this->bound_user, result, (!strncmp(ip, "0::ffff:", 8) ? DNS_QUERY_A : DNS_QUERY_AAAA), cached);
44                                 }
45                                 else
46                                         /* IPV4 lookup (mixed protocol mode) */
47 #endif
48                                 /* IPV4 lookup (ipv4 only mode) */
49                                 bound_user->res_forward = new UserResolver(this->ServerInstance, this->bound_user, result, DNS_QUERY_A, cached);
50                                 this->ServerInstance->AddResolver(bound_user->res_forward, cached);
51                         }
52                 }
53                 catch (CoreException& e)
54                 {
55                         ServerInstance->Log(DEBUG,"Error in resolver: %s",e.GetReason());
56                 }
57         }
58         else if ((this->fwd) && (ServerInstance->SE->GetRef(this->bound_fd) == this->bound_user))
59         {
60                 /* Both lookups completed */
61                 std::string result2("0::ffff:");
62                 result2.append(result);
63                 if (this->bound_user->GetIPString() == result || this->bound_user->GetIPString() == result2)
64                 {
65                         std::string hostname = this->bound_user->stored_host;
66                         if (hostname.length() < 65)
67                         {
68                                 /* Check we didnt time out */
69                                 if ((this->bound_user->registered != REG_ALL) && (!this->bound_user->dns_done))
70                                 {
71                                         /* Hostnames starting with : are not a good thing (tm) */
72                                         if (*(hostname.c_str()) == ':')
73                                                 hostname.insert(0, "0");
74
75                                         this->bound_user->WriteServ("NOTICE Auth :*** Found your hostname (%s)%s", hostname.c_str(), (cached ? " -- cached" : ""));
76                                         this->bound_user->dns_done = true;
77                                         strlcpy(this->bound_user->dhost, hostname.c_str(),64);
78                                         strlcpy(this->bound_user->host, hostname.c_str(),64);
79                                         /* Invalidate cache */
80                                         this->bound_user->InvalidateCache();
81                                 }
82                         }
83                         else
84                         {
85                                 if (!this->bound_user->dns_done)
86                                 {
87                                         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());
88                                         this->bound_user->dns_done = true;
89                                 }
90                         }
91                 }
92                 else
93                 {
94                         if (!this->bound_user->dns_done)
95                         {
96                                 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());
97                                 this->bound_user->dns_done = true;
98                         }
99                 }
100         }
101 }
102
103 void UserResolver::OnError(ResolverError e, const std::string &errormessage)
104 {
105         if (ServerInstance->SE->GetRef(this->bound_fd) == this->bound_user)
106         {
107                 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());
108                 this->bound_user->dns_done = true;
109                 ServerInstance->stats->statsDnsBad++;
110         }
111 }
112
113