]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/userprocess.cpp
Add <options:cyclehosts> which allows a user to appear to have quit when their host...
[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 void InspIRCd::DoSocketTimeouts(time_t TIME)
264 {
265         unsigned int numsockets = this->module_sockets.size();
266
267         for (std::vector<InspSocket*>::iterator a = this->module_sockets.begin(); a < this->module_sockets.end(); a++)
268         {
269                 InspSocket* s = *a;
270                 int fd = s->GetFd();
271                 if ((s) && (fd >= 0) && (fd < MAX_DESCRIPTORS) && (this->SE->GetRef(fd) == s) && (s->Timeout(TIME)))
272                 {
273                         this->Log(DEBUG,"userprocess.cpp: Socket poll returned false, close and bail");
274                         SE->DelFd(s);
275                         this->module_sockets.erase(a);
276                         s->Close();
277                         DELETE(s);
278                         break;
279                 }
280
281                 if (this->module_sockets.size() != numsockets)
282                         break;
283         }
284 }
285
286 /**
287  * This function is called once a second from the mainloop.
288  * It is intended to do background checking on all the user structs, e.g.
289  * stuff like ping checks, registration timeouts, etc. This function is
290  * also responsible for checking if InspSocket derived classes are timed out.
291  */
292 void InspIRCd::DoBackgroundUserStuff(time_t TIME)
293 {
294         CullList GlobalGoners(this);
295
296         /* XXX: IT IS NOT SAFE TO USE AN ITERATOR HERE. DON'T EVEN THINK ABOUT IT. */
297         for (unsigned long count2 = 0; count2 != this->local_users.size(); count2++)
298         {
299                 if (count2 >= this->local_users.size())
300                         break;
301
302                 userrec* curr = this->local_users[count2];
303
304                 if (curr)
305                 {
306                         /*
307                          * registration timeout -- didnt send USER/NICK/HOST
308                          * in the time specified in their connection class.
309                          */
310                         if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != REG_ALL))
311                         {
312                                 this->Log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
313                                 //ZapThisDns(curr->fd);
314                                 GlobalGoners.AddItem(curr,"Registration timeout");
315                                 continue;
316                         }
317                         /*
318                          * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
319                          * say this user is ok to proceed, fully connect them.
320                          */
321                         if ((TIME > curr->signon) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
322                         {
323                                 curr->dns_done = true;
324                                 //ZapThisDns(curr->fd);
325                                 this->stats->statsDnsBad++;
326                                 curr->FullConnect(&GlobalGoners);
327                                 continue;
328                         }
329                         if ((curr->dns_done) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
330                         {
331                                 this->Log(DEBUG,"dns done, registered=3, and modules ready, OK");
332                                 curr->FullConnect(&GlobalGoners);
333                                 //ZapThisDns(curr->fd);
334                                 continue;
335                         }
336                         // It's time to PING this user. Send them a ping.
337                         if ((TIME > curr->nping) && (curr->registered == REG_ALL))
338                         {
339                                 // This user didn't answer the last ping, remove them
340                                 if (!curr->lastping)
341                                 {
342                                         GlobalGoners.AddItem(curr,"Ping timeout");
343                                         curr->lastping = 1;
344                                         curr->nping = TIME+curr->pingmax;
345                                         continue;
346                                 }
347                                 curr->Write("PING :%s",this->Config->ServerName);
348                                 curr->lastping = 0;
349                                 curr->nping = TIME+curr->pingmax;
350                         }
351
352                         /*
353                          * We can flush the write buffer as the last thing we do, because if they
354                          * match any of the above conditions its no use flushing their buffer anyway.
355                          */
356         
357                         curr->FlushWriteBuf();
358                         if (*curr->GetWriteError())
359                         {
360                                 GlobalGoners.AddItem(curr,curr->GetWriteError());
361                                 continue;
362                         }
363                 }
364
365         }
366
367
368         /* Remove all the queued users who are due to be quit, free memory used. */
369         GlobalGoners.Apply();
370 }