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