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