]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/userprocess.cpp
This should be faster, we read it like a stream now with GetToken until GetToken...
[user/henk/code/inspircd.git] / src / userprocess.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 /* Now with added unF! ;) */
18
19 using namespace std;
20
21 #include "inspircd_config.h"
22 #include "inspircd.h"
23 #include "configreader.h"
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <sys/errno.h>
27 #include <sys/ioctl.h>
28 #include <sys/utsname.h>
29 #include <time.h>
30 #include <string>
31 #include <ext/hash_map>
32 #include <map>
33 #include <sstream>
34 #include <vector>
35 #include <deque>
36 #include <sched.h>
37 #ifdef THREADED_DNS
38 #include <pthread.h>
39 #endif
40 #include "users.h"
41 #include "ctables.h"
42 #include "globals.h"
43 #include "modules.h"
44 #include "dynamic.h"
45 #include "wildcard.h"
46 #include "message.h"
47 #include "mode.h"
48 #include "commands.h"
49 #include "xline.h"
50 #include "inspstring.h"
51 #include "dnsqueue.h"
52 #include "helperfuncs.h"
53 #include "hashcomp.h"
54 #include "socketengine.h"
55 #include "userprocess.h"
56 #include "typedefs.h"
57 #include "command_parse.h"
58 #include "cull_list.h"
59
60 extern struct sockaddr_in client,server;
61 extern socklen_t length;
62 extern std::vector<Module*> modules;
63 extern std::vector<ircd_module*> factory;
64 extern std::vector<InspSocket*> module_sockets;
65 extern time_t TIME;
66 extern time_t OLDTIME;
67 extern std::vector<userrec*> local_users;
68 extern InspSocket* socket_ref[MAX_DESCRIPTORS];
69
70 extern InspIRCd* ServerInstance;
71 extern ServerConfig *Config;
72 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
73 char data[65536];
74
75 extern user_hash clientlist;
76 extern chan_hash chanlist;
77
78 void ProcessUser(userrec* cu)
79 {
80         int result = EAGAIN;
81
82         if (cu->fd == FD_MAGIC_NUMBER)
83                 return;
84
85         log(DEBUG,"Processing user with fd %d",cu->fd);
86
87         if (Config->GetIOHook(cu->port))
88         {
89                 int result2 = 0;
90                 int MOD_RESULT = 0;
91
92                 try
93                 {
94                         MOD_RESULT = Config->GetIOHook(cu->port)->OnRawSocketRead(cu->fd,data,65535,result2);
95                         log(DEBUG,"Data result returned by module: %d",MOD_RESULT);
96                 }
97                 catch (ModuleException& modexcept)
98                 {
99                         log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
100                 }
101
102                 if (MOD_RESULT < 0)
103                 {
104                         result = -EAGAIN;
105                 }
106                 else
107                 {
108                         result = result2;
109                 }
110         }
111         else
112         {
113                 result = cu->ReadData(data, 65535);
114         }
115
116         log(DEBUG,"Read result: %d",result);
117
118         if ((result) && (result != -EAGAIN))
119         {
120                 userrec *current;
121                 int currfd;
122                 int floodlines;
123
124                 ServerInstance->stats->statsRecv += result;
125                 /*
126                  * perform a check on the raw buffer as an array (not a string!) to remove
127                  * characters 0 and 7 which are illegal in the RFC - replace them with spaces.
128                  * hopefully this should stop even more people whining about "Unknown command: *"
129                  */
130
131                 /*
132                  * XXX - potential replacement for the below using my beloved pointers. --w00t
133                  * XXX - no garauntee there's not \0's in the middle of the data,
134                  *       and no reason for it to be terminated either. -- Om
135                  *
136                  * for (char *c = data; data && *data; data++)
137                  * {
138                  *      if (*data == 0 || *data == 7)
139                  *              data = ' ';
140                  * }
141                  */
142
143                 for (int checker = 0; checker < result; checker++)
144                 {
145                         if ((data[checker] == 0) || (data[checker] == 7))
146                                 data[checker] = ' ';
147                 }
148
149                 if (result > 0)
150                         data[result] = '\0';
151
152                 current = cu;
153                 currfd = current->fd;
154                 floodlines = 0;
155
156                 // add the data to the users buffer
157                 if (result > 0)
158                 {
159                         if (!current->AddBuffer(data))
160                         {
161                                 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
162                                 if (current->registered == 7)
163                                 {
164                                         // Make sure they arn't flooding long lines.
165                                         if (TIME > current->reset_due)
166                                         {
167                                                 current->reset_due = TIME + current->threshold;
168                                                 current->lines_in = 0;
169                                         }
170
171                                         current->lines_in++;
172
173                                         if (current->lines_in > current->flood)
174                                         {
175                                                 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
176                                                 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
177                                                 kill_link(current,"Excess flood");
178                                                 return;
179                                         }
180                                         else
181                                         {
182                                                 WriteServ(currfd, "NOTICE %s :Your previous line was too long and was not delivered (Over 512chars) Please shorten it.", current->nick);
183                                                 current->recvq = "";
184                                         }
185                                 }
186                                 else
187                                 {
188                                         WriteOpers("*** Excess flood from %s",(char*)inet_ntoa(current->ip4));
189                                         log(DEFAULT,"Excess flood from: %s",(char*)inet_ntoa(current->ip4));
190                                         add_zline(120,Config->ServerName,"Flood from unregistered connection",(char*)inet_ntoa(current->ip4));
191                                         apply_lines(APPLY_ZLINES);
192                                 }
193
194                                 return;
195                         }
196
197                         if (current->recvq.length() > (unsigned)Config->NetBufferSize)
198                         {
199                                 if (current->registered == 7)
200                                 {
201                                         kill_link(current,"RecvQ exceeded");
202                                 }
203                                 else
204                                 {
205                                         WriteOpers("*** Excess flood from %s",(char*)inet_ntoa(current->ip4));
206                                         log(DEFAULT,"Excess flood from: %s",(char*)inet_ntoa(current->ip4));
207                                         add_zline(120,Config->ServerName,"Flood from unregistered connection",(char*)inet_ntoa(current->ip4));
208                                         apply_lines(APPLY_ZLINES);
209                                 }
210
211                                 return;
212                         }
213
214                         // while there are complete lines to process...
215                         while (current->BufferIsReady())
216                         {
217                                 char sanitized[MAXBUF];
218
219                                 floodlines++;
220                                 if (TIME > current->reset_due)
221                                 {
222                                         current->reset_due = TIME + current->threshold;
223                                         current->lines_in = 0;
224                                 }
225
226                                 current->lines_in++;
227
228                                 if (current->lines_in > current->flood)
229                                 {
230                                         log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
231                                         WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
232                                         kill_link(current,"Excess flood");
233                                         return;
234                                 }
235
236                                 if ((floodlines > current->flood) && (current->flood != 0))
237                                 {
238                                         if (current->registered == 7)
239                                         {
240                                                 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
241                                                 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
242                                                 kill_link(current,"Excess flood");
243                                         }
244                                         else
245                                         {
246                                                 add_zline(120,Config->ServerName,"Flood from unregistered connection",(char*)inet_ntoa(current->ip4));
247                                                 apply_lines(APPLY_ZLINES);
248                                         }
249
250                                         return;
251                                 }
252
253                                 // use GetBuffer to copy single lines into the sanitized string
254                                 std::string single_line = current->GetBuffer();
255                                 current->bytes_in += single_line.length();
256                                 current->cmds_in++;
257                                 strlcpy(sanitized,single_line.c_str(),511);
258
259                                 if (*sanitized)
260                                 {
261                                         userrec* old_comp = fd_ref_table[currfd];
262
263                                         ServerInstance->Parser->ProcessBuffer(sanitized,current);
264                                         /*
265                                          * look for the user's record in case it's changed... if theyve quit,
266                                          * we cant do anything more with their buffer, so bail.
267                                          * there used to be an ugly, slow loop here. Now we have a reference
268                                          * table, life is much easier (and FASTER)
269                                          */
270                                         userrec* new_comp = fd_ref_table[currfd];
271                                         if ((currfd < 0) || (!fd_ref_table[currfd]) || (old_comp != new_comp))
272                                         {
273                                                 return;
274                                         }
275                                         else
276                                         {
277                                                 /* The user is still here, flush their buffer */
278                                                 current->FlushWriteBuf();
279                                         }
280                                 }
281                         }
282
283                         return;
284                 }
285
286                 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
287                 {
288                         log(DEBUG,"killing: %s",cu->nick);
289                         kill_link(cu,strerror(errno));
290                         return;
291                 }
292         }
293
294         // result EAGAIN means nothing read
295         else if ((result == EAGAIN) || (result == -EAGAIN))
296         {
297                 /* do nothing */
298         }
299         else if (result == 0)
300         {
301                 log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
302                 kill_link(cu,"Client exited");
303                 log(DEBUG,"Bailing from client exit");
304                 return;
305         }
306 }
307
308 void DoSocketTimeouts(time_t TIME)
309 {
310         unsigned int numsockets = module_sockets.size();
311         SocketEngine* SE = ServerInstance->SE;
312
313         for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
314         {
315                 InspSocket* s = (InspSocket*)*a;
316                 if ((s) && (s->GetFd() >= 0) && (s->GetFd() < MAX_DESCRIPTORS) && (socket_ref[s->GetFd()] != NULL) && (s->Timeout(TIME)))
317                 {
318                         log(DEBUG,"userprocess.cpp: Socket poll returned false, close and bail");
319                         socket_ref[s->GetFd()] = NULL;
320                         SE->DelFd(s->GetFd());
321                         module_sockets.erase(a);
322                         s->Close();
323                         DELETE(s);
324                         break;
325                 }
326
327                 if (module_sockets.size() != numsockets)
328                         break;
329         }
330 }
331
332 /**
333  * This function is called once a second from the mainloop.
334  * It is intended to do background checking on all the user structs, e.g.
335  * stuff like ping checks, registration timeouts, etc. This function is
336  * also responsible for checking if InspSocket derived classes are timed out.
337  */
338 void DoBackgroundUserStuff(time_t TIME)
339 {
340         CullList GlobalGoners;
341
342         for (std::vector<userrec*>::iterator count2 = local_users.begin(); count2 != local_users.end(); count2++)
343         {
344                 /* Sanity checks for corrupted iterators (yes, really) */
345                 userrec* curr = NULL;
346
347                 if (*count2)
348                         curr = (userrec*)(*count2);
349                 if ((long)curr == -1)
350                         return;
351
352                 if (curr)
353                 {
354                         /*
355                          * registration timeout -- didnt send USER/NICK/HOST
356                          * in the time specified in their connection class.
357                          */
358                         if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != 7))
359                         {
360                                 log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
361                                 GlobalGoners.AddItem(curr,"Registration timeout");
362                                 continue;
363                         }
364
365                         /*
366                          * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
367                          * say this user is ok to proceed, fully connect them.
368                          */
369                         if ((TIME > curr->signon) && (curr->registered == 3) && (AllModulesReportReady(curr)))
370                         {
371                                 curr->dns_done = true;
372                                 ServerInstance->stats->statsDnsBad++;
373                                 FullConnectUser(curr,&GlobalGoners);
374                                 continue;
375                         }
376
377                         if ((curr->dns_done) && (curr->registered == 3) && (AllModulesReportReady(curr)))
378                         {
379                                 log(DEBUG,"dns done, registered=3, and modules ready, OK");
380                                 FullConnectUser(curr,&GlobalGoners);
381                                 continue;
382                         }
383
384                         // It's time to PING this user. Send them a ping.
385                         if ((TIME > curr->nping) && (curr->registered == 7))
386                         {
387                                 // This user didn't answer the last ping, remove them
388                                 if (!curr->lastping)
389                                 {
390                                         GlobalGoners.AddItem(curr,"Ping timeout");
391                                         continue;
392                                 }
393
394                                 Write(curr->fd,"PING :%s",Config->ServerName);
395                                 curr->lastping = 0;
396                                 curr->nping = TIME+curr->pingmax;
397                         }
398
399                         /*
400                          * We can flush the write buffer as the last thing we do, because if they
401                          * match any of the above conditions its no use flushing their buffer anyway.
402                          */
403                         curr->FlushWriteBuf();
404
405                         if (*curr->GetWriteError())
406                         {
407                                 GlobalGoners.AddItem(curr,curr->GetWriteError());
408                                 continue;
409                         }
410                 }
411         }
412
413         /* Remove all the queued users who are due to be quit, free memory used. */
414         GlobalGoners.Apply();
415 }