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