]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/dnsqueue.cpp
Fix dns socket leak found in stable
[user/henk/code/inspircd.git] / src / dnsqueue.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #include "inspircd_config.h"
18 #include "inspircd.h"
19 #include "configreader.h"
20 #include <unistd.h>
21 #include <sys/errno.h>
22 #include <sys/ioctl.h>
23 #include <sys/utsname.h>
24 #include <string>
25 #include "users.h"
26 #include "globals.h"
27 #include "inspstring.h"
28 #include "dnsqueue.h"
29 #include "dns.h"
30 #include "helperfuncs.h"
31 #include "hashcomp.h"
32 #include "socketengine.h"
33 #include "socket.h"
34
35 extern ServerConfig* Config;
36 extern InspIRCd* ServerInstance;
37
38 class Lookup;
39
40 Lookup* dnslist[MAX_DESCRIPTORS];
41
42 //enum LookupState { reverse, forward };
43
44 class Lookup {
45 private:
46         DNS resolver1;
47         DNS resolver2;
48         char u[NICKMAX];
49         std::string hostname;
50 public:
51         Lookup()
52         {
53                 *u = 0;
54                 hostname = "";
55         }
56
57         void Reset()
58         {
59                 *u = 0;
60                 hostname = "";
61         }
62
63         ~Lookup()
64         {
65         }
66
67         bool DoLookup(std::string nick)
68         {
69                 hostname = "";
70                 userrec* usr = Find(nick);
71                 if (usr)
72                 {
73                         resolver1.SetNS(std::string(Config->DNSServer));
74                         {
75                                 return false;
76                         }
77                         strlcpy(u,nick.c_str(),NICKMAX-1);
78
79 #ifndef THREADED_DNS
80                         usr->dns_fd = resolver1.GetFD();
81 #endif
82                         /* ASSOCIATE WITH DNS LOOKUP LIST */
83                         if (resolver1.GetFD() != -1)
84                         {
85                                 dnslist[resolver1.GetFD()] = this;
86                                 return true;
87                         }
88                 }
89                 return false;
90         }
91
92         bool Done(int fdcheck)
93         {
94                 if (hostname != "")
95                 {
96                         // doing forward lookup
97                         userrec* usr = NULL;
98                         if (resolver2.HasResult(fdcheck))
99                         {
100                                 if (resolver2.GetFD() != -1)
101                                 {
102                                         dnslist[resolver2.GetFD()] = NULL;
103                                         std::string ip = resolver2.GetResultIP();
104                                         usr = Find(u);
105                                         if (usr)
106                                         {
107                                                 if (usr->registered > 3)
108                                                 {
109                                                         usr->dns_done = true;
110                                                         return true;
111                                                 }
112                                                 if ((hostname != "") && (usr->registered != 7))
113                                                 {
114                                                         if ((std::string((char*)inet_ntoa(usr->ip4)) == ip) && (hostname.length() < 65))
115                                                         {
116                                                                 strlcpy(usr->host,hostname.c_str(),64);
117                                                                 strlcpy(usr->dhost,hostname.c_str(),64);
118                                                                 /*address_cache::iterator address = addrcache.find(usr->ip4);
119                                                                 if (address == addrcache.end())
120                                                                 {
121                                                                         log(DEBUG,"Caching hostname %s -> %s",(char*)inet_ntoa(usr->ip4),hostname.c_str());
122                                                                         addrcache[usr->ip4] = new std::string(hostname);
123                                                                 }*/
124                                                                 WriteServ(usr->fd,"NOTICE Auth :*** Found your hostname");
125                                                         }
126                                                         usr->dns_done = true;
127 #ifndef THREADED_DNS
128                                                         usr->dns_fd = -1;
129 #endif
130                                                         return true;
131                                                 }
132                                         }
133                                 }
134                                 else
135                                 {
136                                         usr = Find(u);
137                                         if (usr)
138                                         {
139                                                 usr->dns_done = true;
140 #ifndef THREADED_DNS
141                                                 usr->dns_fd = -1;
142 #endif
143                                         }
144                                         return true;
145                                 }
146                         }
147                         return false;
148                 }
149                 else
150                 {
151                         // doing reverse lookup
152                         userrec* usr = NULL;
153                         if (resolver1.HasResult(fdcheck))
154                         {
155                                 usr = Find(u);
156                                 if ((usr) && (usr->dns_done))
157                                 {
158                                         if (resolver1.GetFD() != -1)
159                                                 dnslist[resolver1.GetFD()] = NULL;
160                                         return true;
161                                 }
162                                 if (resolver1.GetFD() != -1)
163                                 {
164                                         dnslist[resolver1.GetFD()] = NULL;
165                                         hostname = resolver1.GetResult();
166                                         if (usr)
167                                         {
168                                                 if ((usr->registered > 3) || (hostname == ""))
169                                                 {
170                                                         WriteServ(usr->fd,"NOTICE Auth :*** Could not resolve your hostname -- Using your IP address instead");
171                                                         usr->dns_done = true;
172                                                         return true;
173                                                 }
174                                         }
175                                         if ((hostname != "") && (usr))
176                                         {
177                                                 resolver2.ForwardLookup(hostname, true);
178                                                 if (resolver2.GetFD() != -1)
179                                                 {
180                                                         dnslist[resolver2.GetFD()] = this;
181 #ifndef THREADED_DNS
182                                                         usr->dns_fd = resolver2.GetFD();
183 #endif
184                                                 }
185                                         }
186                                 }
187                         }
188                 }
189                 return false;
190         }
191
192         int GetFD()
193         {
194                 userrec* usr = Find(u);
195                 if (!usr)
196                         return 0;
197                 if (usr->dns_done)
198                         return 0;
199                 return usr->fd;
200         }
201 };
202
203 bool lookup_dns(const std::string &nick)
204 {
205         /* First attempt to find the nickname */
206         userrec* u = Find(nick);
207         if (u)
208         {
209                 /* Check the cache */
210                 /*address_cache::iterator address = addrcache.find(u->ip4);
211                 if (address != addrcache.end())
212                 {
213                         WriteServ(u->fd,"NOTICE Auth :*** Found your hostname (cached)");
214                         log(DEBUG,"Found cached host");
215                         strlcpy(u->host,address->second->c_str(),MAXBUF);
216                         strlcpy(u->dhost,address->second->c_str(),MAXBUF);
217                         u->dns_done = true;
218                         return true;
219                 }*/
220                 /* If the user exists, create a new
221                  * lookup object, and associate it
222                  * with the user. The lookup object
223                  * will maintain the reference table
224                  * which we use for quickly finding
225                  * dns results. Please note that we
226                  * do not associate a lookup with a
227                  * userrec* pointer and we use the
228                  * nickname instead because, by the
229                  * time the DNS lookup has completed,
230                  * the nickname could have quit and
231                  * if we then try and access the
232                  * pointer we get a nice segfault.
233                  */
234                 Lookup* L = new Lookup();
235                 L->DoLookup(nick);
236                 return true;
237         }
238         return false;
239 }
240
241 void dns_poll(int fdcheck)
242 {
243         /* Check the given file descriptor is in valid range */
244         if ((fdcheck < 0) || (fdcheck > MAX_DESCRIPTORS))
245                 return;
246
247         /* Try and find the file descriptor in our list of
248          * active DNS lookups
249          */
250         Lookup *x = dnslist[fdcheck];
251         if (x)
252         {
253                 /* If it exists check if its a valid fd still */
254                 if (x->GetFD() != -1)
255                 {
256                         /* Check if its done, if it is delete it */
257                         if (x->Done(fdcheck))
258                         {
259                                 /* We don't need to delete the file descriptor
260                                  * from the socket engine, as dns.cpp tracks it
261                                  * for us if we are in single-threaded country.
262                                  */
263                                 DELETE(x);
264                         }
265                 }
266                 else
267                 {
268                         /* its fd is dodgy, the dns code probably
269                          * bashed it due to error. Free the class.
270                          */
271                         DELETE(x);
272                 }
273                 /* If we got down here, the dns lookup was valid, BUT,
274                  * its still in progress. Be patient, and wait for
275                  * more socketengine events to complete the lookups.
276                  */
277                 return;
278         }
279         /* This FD doesnt belong here, lets be rid of it,
280          * just to be safe so we dont get any more events
281          * about it.
282          */
283         if (ServerInstance && ServerInstance->SE)
284                 ServerInstance->SE->DelFd(fdcheck);
285 }
286