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