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