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