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