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