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