]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/dnsqueue.cpp
Added forward lookup sanity checks to single threaded dns
[user/henk/code/inspircd.git] / src / dnsqueue.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 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 /* Now with added unF! ;) */
18
19 using namespace std;
20
21 #include "inspircd_config.h"
22 #include "inspircd.h"
23 #include "inspircd_io.h"
24 #include "inspircd_util.h"
25 #include <unistd.h>
26 #include <sys/errno.h>
27 #include <sys/ioctl.h>
28 #include <sys/utsname.h>
29 #include <cstdio>
30 #include <time.h>
31 #include <string>
32 #ifdef GCC3
33 #include <ext/hash_map>
34 #else
35 #include <hash_map>
36 #endif
37 #include <map>
38 #include <sstream>
39 #include <vector>
40 #include <deque>
41 #include "users.h"
42 #include "ctables.h"
43 #include "globals.h"
44 #include "modules.h"
45 #include "dynamic.h"
46 #include "wildcard.h"
47 #include "message.h"
48 #include "mode.h"
49 #include "commands.h"
50 #include "xline.h"
51 #include "inspstring.h"
52 #include "dnsqueue.h"
53 #include <time.h>
54 #include <sys/types.h>
55 #include <sys/socket.h>
56 #include <sys/poll.h>
57 #include <sys/time.h>
58 #include <netinet/in.h>
59 #include <string.h>
60 #include "dns.h"
61 #include "helperfuncs.h"
62 #include "hashcomp.h"
63
64 extern int MaxWhoResults;
65
66 extern std::vector<Module*> modules;
67 extern std::vector<std::string> module_names;
68 extern std::vector<ircd_module*> factory;
69
70 extern int MODCOUNT;
71
72 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, irc::StrHashComp> user_hash;
73 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, irc::StrHashComp> chan_hash;
74 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, irc::InAddr_HashComp> address_cache;
75 typedef nspace::hash_map<std::string, WhoWasUser*, nspace::hash<string>, irc::StrHashComp> whowas_hash;
76 typedef std::deque<command_t> command_table;
77
78 extern user_hash clientlist;
79 extern chan_hash chanlist;
80 extern whowas_hash whowas;
81 extern command_table cmdlist;
82
83 extern ClassVector Classes;
84
85 extern char DNSServer[MAXBUF];
86 long max_fd_alloc = 0;
87
88 extern time_t TIME;
89
90 //enum LookupState { reverse, forward };
91
92 class Lookup {
93 private:
94         DNS resolver1;
95         DNS resolver2;
96         char u[NICKMAX];
97         std::string hostname;
98 public:
99         Lookup()
100         {
101                 strcpy(u,"");
102         }
103
104         void Reset()
105         {
106                 strcpy(u,"");
107         }
108
109         ~Lookup()
110         {
111         }
112
113         bool DoLookup(std::string nick)
114         {
115                 hostname = "";
116                 userrec* usr = Find(nick);
117                 if (usr)
118                 {
119                         log(DEBUG,"New Lookup class for %s with DNSServer set to '%s'",nick.c_str(),DNSServer);
120                         resolver1.SetNS(std::string(DNSServer));
121                         if (!resolver1.ReverseLookup(std::string(usr->host)))
122                                 return false;
123                         strlcpy(u,nick.c_str(),NICKMAX);
124                         return true;
125                 }
126                 return false;
127         }
128
129         bool Done()
130         {
131                 if (hostname != "")
132                 {
133                         // doing forward lookup
134                         userrec* usr = NULL;
135                         if (resolver2.HasResult())
136                         {
137                                 if (resolver2.GetFD() != 0)
138                                 {
139                                         std::string ip = resolver2.GetResultIP();
140                                         log(DEBUG,"FORWARD RESULT! %s",ip.c_str());
141
142                                         usr = Find(u);
143                                         if (usr)
144                                         {
145                                                 if (usr->registered > 3)
146                                                 {
147                                                         usr->dns_done = true;
148                                                         return true;
149                                                 }
150                                                 if ((hostname != "") && (usr->registered != 7))
151                                                 {
152                                                         if (std::string(usr->ip) == ip)
153                                                         {
154                                                                 strlcpy(usr->host,hostname.c_str(),MAXBUF);
155                                                                 strlcpy(usr->dhost,hostname.c_str(),MAXBUF);
156                                                                 log(DEBUG,"Forward and reverse match, assigning hostname");
157                                                         }
158                                                         else
159                                                         {
160                                                                 log(DEBUG,"AWOOGA! Forward lookup doesn't match reverse: R='%s',F='%s',IP='%s'",hostname.c_str(),ip.c_str(),usr->ip);
161                                                         }
162                                                         usr->dns_done = true;
163                                                         return true;
164                                                 }
165                                         }
166                                 }
167                                 else
168                                 {
169                                         usr = Find(u);
170                                         if (usr)
171                                                 usr->dns_done = true;
172                                         return true;
173                                 }
174                         }
175                 }
176                 else
177                 {
178                         // doing reverse lookup
179                         userrec* usr = NULL;
180                         if (resolver1.HasResult())
181                         {
182                                 if (resolver1.GetFD() != 0)
183                                 {
184                                         hostname = resolver1.GetResult();
185                                         log(DEBUG,"REVERSE RESULT! %s",hostname.c_str());
186                                         usr = Find(u);
187                                         if (usr)
188                                         {
189                                                 if (usr->registered > 3)
190                                                 {
191                                                         usr->dns_done = true;
192                                                         return true;
193                                                 }
194                                         }
195                                         resolver2.ForwardLookup(hostname);
196                                 }
197                         }
198                 }
199                 return false;
200         }
201
202         int GetFD()
203         {
204                 userrec* usr = Find(u);
205                 if (!usr)
206                         return 0;
207                 if (usr->dns_done)
208                         return 0;
209                 return usr->fd;
210         }
211 };
212
213 Lookup dnsq[255];
214
215 bool lookup_dns(std::string nick)
216 {
217         userrec* u = Find(nick);
218         if (u)
219         {
220                 // place a new user into the queue...
221                 log(DEBUG,"Queueing DNS lookup for %s",u->nick);
222                 WriteServ(u->fd,"NOTICE Auth :Looking up your hostname...");
223                 Lookup L;
224                 if (L.DoLookup(nick))
225                 {
226                         for (int j = 0; j < 255; j++)
227                         {
228                                 if (!dnsq[j].GetFD())
229                                 {
230                                         dnsq[j] = L;
231                                         return true;
232                                 }
233                         }
234                         // calculate the maximum value, this saves cpu time later
235                         for (int p = 0; p < 255; p++)
236                                 if (dnsq[p].GetFD())
237                                         max_fd_alloc = p;
238                 }
239                 else
240                 {
241                         return false;
242                 }
243         }
244         return false;
245 }
246
247 void dns_poll()
248 {
249         // do we have items in the queue?
250         for (int j = 0; j <= max_fd_alloc; j++)
251         {
252                 // are any ready, or stale?
253                 if (dnsq[j].GetFD())
254                 {
255                         if (dnsq[j].Done())
256                         {
257                                 dnsq[j].Reset();
258                         }
259                 }
260         }
261         // looks like someones freed an item, recalculate end of list.
262         if ((!dnsq[max_fd_alloc].GetFD()) && (max_fd_alloc != 0))
263                 for (int p = 0; p < 255; p++)
264                         if (dnsq[p].GetFD())
265                                 max_fd_alloc = p;
266
267 }