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