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