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