]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/user_resolver.cpp
d0270f7d8d1bf3ec234f8c2719e16c49e3451594
[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 #ifdef IPV6
38                                 if (this->bound_user->client_sa.sa.sa_family == AF_INET6)
39                                 {
40                                         /* IPV6 forward lookup */
41                                         res_forward = new UserResolver(this->ServerInstance, this->bound_user, result, DNS_QUERY_AAAA, lcached);
42                                 }
43                                 else
44                                         /* IPV4 lookup (mixed protocol mode) */
45 #endif
46                                 {
47                                         /* IPV4 lookup (ipv4 only mode) */
48                                         res_forward = new UserResolver(this->ServerInstance, this->bound_user, result, DNS_QUERY_A, lcached);
49                                 }
50                                 this->ServerInstance->AddResolver(res_forward, lcached);
51                         }
52                 }
53                 catch (CoreException& e)
54                 {
55                         ServerInstance->Logs->Log("RESOLVER", 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
62                 irc::sockets::sockaddrs* user_ip = &this->bound_user->client_sa;
63                 bool rev_match = false;
64 #ifdef IPV6
65                 if (user_ip->sa.sa_family == AF_INET6)
66                 {
67                         struct in6_addr res_bin;
68                         if (inet_pton(AF_INET6, result.c_str(), &res_bin))
69                         {
70                                 rev_match = !memcmp(&user_ip->in6.sin6_addr, &res_bin, sizeof(res_bin));
71                         }
72                 }
73                 else
74 #endif
75                 {
76                         struct in_addr res_bin;
77                         if (inet_pton(AF_INET, result.c_str(), &res_bin))
78                         {
79                                 rev_match = !memcmp(&user_ip->in4.sin_addr, &res_bin, sizeof(res_bin));
80                         }
81                 }
82                 
83                 if (rev_match)
84                 {
85                         std::string hostname = this->bound_user->stored_host;
86                         if (hostname.length() < 65)
87                         {
88                                 /* Check we didnt time out */
89                                 if ((this->bound_user->registered != REG_ALL) && (!this->bound_user->dns_done))
90                                 {
91                                         /* Hostnames starting with : are not a good thing (tm) */
92                                         if (hostname[0] == ':')
93                                                 hostname.insert(0, "0");
94
95                                         this->bound_user->WriteServ("NOTICE Auth :*** Found your hostname (%s)%s", hostname.c_str(), (cached ? " -- cached" : ""));
96                                         this->bound_user->dns_done = true;
97                                         this->bound_user->dhost.assign(hostname, 0, 64);
98                                         this->bound_user->host.assign(hostname, 0, 64);
99                                         /* Invalidate cache */
100                                         this->bound_user->InvalidateCache();
101                                 }
102                         }
103                         else
104                         {
105                                 if (!this->bound_user->dns_done)
106                                 {
107                                         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());
108                                         this->bound_user->dns_done = true;
109                                 }
110                         }
111                 }
112                 else
113                 {
114                         if (!this->bound_user->dns_done)
115                         {
116                                 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());
117                                 this->bound_user->dns_done = true;
118                         }
119                 }
120
121                 // Save some memory by freeing this up; it's never used again in the user's lifetime.
122                 this->bound_user->stored_host.resize(0);
123         }
124 }
125
126 void UserResolver::OnError(ResolverError e, const std::string &errormessage)
127 {
128         if (ServerInstance->SE->GetRef(this->bound_fd) == this->bound_user)
129         {
130                 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());
131                 this->bound_user->dns_done = true;
132                 this->bound_user->stored_host.resize(0);
133                 ServerInstance->stats->statsDnsBad++;
134         }
135 }