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