]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/userprocess.cpp
Apparently to catch descendent classes we need to catch a reference to the parent...
[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 "inspircd_io.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 int MODCOUNT;
65 extern struct sockaddr_in client,server;
66 extern socklen_t length;
67 extern std::vector<Module*> modules;
68 extern std::vector<ircd_module*> factory;
69 extern std::vector<InspSocket*> module_sockets;
70 extern time_t TIME;
71 extern time_t OLDTIME;
72 extern std::vector<userrec*> local_users;
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         if (cu->fd == FD_MAGIC_NUMBER)
86                 return;
87         log(DEBUG,"Processing user with fd %d",cu->fd);
88         if (Config->GetIOHook(cu->port))
89         {
90                 int result2 = 0;
91                 int MOD_RESULT = 0;
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 cought: %s",modexcept.GetReason()); \
100                 }
101                 if (MOD_RESULT < 0)
102                 {
103                         result = -EAGAIN;
104                 }
105                 else
106                 {
107                         result = result2;
108                 }
109         }
110         else
111         {
112                 result = cu->ReadData(data, 65535);
113         }
114         log(DEBUG,"Read result: %d",result);
115         if ((result) && (result != -EAGAIN))
116         {
117                 ServerInstance->stats->statsRecv += result;
118                 // perform a check on the raw buffer as an array (not a string!) to remove
119                 // characters 0 and 7 which are illegal in the RFC - replace them with spaces.
120                 // hopefully this should stop even more people whining about "Unknown command: *"
121                 for (int checker = 0; checker < result; checker++)
122                 {
123                         if ((data[checker] == 0) || (data[checker] == 7))
124                                 data[checker] = ' ';
125                 }
126                 if (result > 0)
127                         data[result] = '\0';
128                 userrec* current = cu;
129                 int currfd = current->fd;
130                 int floodlines = 0;
131                 // add the data to the users buffer
132                 if (result > 0)
133                 {
134                         if (!current->AddBuffer(data))
135                         {
136                                 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
137                                 if (current->registered == 7)
138                                 {
139                                         // Make sure they arn't flooding long lines.
140                                         if (TIME > current->reset_due)
141                                         {
142                                                 current->reset_due = TIME + current->threshold;
143                                                 current->lines_in = 0;
144                                         }
145                                         current->lines_in++;
146                                         if (current->lines_in > current->flood)
147                                         {
148                                                 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
149                                                 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
150                                                 kill_link(current,"Excess flood");
151                                                 return;
152                                         } else {
153                                                 WriteServ(currfd, "NOTICE %s :Your previous line was too long and was not delivered (Over 512chars) Please shorten it.", current->nick);
154                                                 current->recvq = "";
155                                         }
156                                 }
157                                 else
158                                 {
159                                         WriteOpers("*** Excess flood from %s",(char*)inet_ntoa(current->ip4));
160                                         log(DEFAULT,"Excess flood from: %s",(char*)inet_ntoa(current->ip4));
161                                         add_zline(120,Config->ServerName,"Flood from unregistered connection",(char*)inet_ntoa(current->ip4));
162                                         apply_lines(APPLY_ZLINES);
163                                 }
164                                 return;
165                         }
166                         if (current->recvq.length() > (unsigned)Config->NetBufferSize)
167                         {
168                                 if (current->registered == 7)
169                                 {
170                                         kill_link(current,"RecvQ exceeded");
171                                 }
172                                 else
173                                 {
174                                         WriteOpers("*** Excess flood from %s",(char*)inet_ntoa(current->ip4));
175                                         log(DEFAULT,"Excess flood from: %s",(char*)inet_ntoa(current->ip4));
176                                         add_zline(120,Config->ServerName,"Flood from unregistered connection",(char*)inet_ntoa(current->ip4));
177                                         apply_lines(APPLY_ZLINES);
178                                 }
179                                 return;
180                         }
181                         // while there are complete lines to process...
182                         while (current->BufferIsReady())
183                         {
184                                 floodlines++;
185                                 if (TIME > current->reset_due)
186                                 {
187                                         current->reset_due = TIME + current->threshold;
188                                         current->lines_in = 0;
189                                 }
190                                 current->lines_in++;
191                                 if (current->lines_in > current->flood)
192                                 {
193                                         log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
194                                         WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
195                                         kill_link(current,"Excess flood");
196                                         return;
197                                 }
198                                 if ((floodlines > current->flood) && (current->flood != 0))
199                                 {
200                                         if (current->registered == 7)
201                                         {
202                                                 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
203                                                 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
204                                                 kill_link(current,"Excess flood");
205                                         }
206                                         else
207                                         {
208                                                 add_zline(120,Config->ServerName,"Flood from unregistered connection",(char*)inet_ntoa(current->ip4));
209                                                 apply_lines(APPLY_ZLINES);
210                                         }
211                                         return;
212                                 }
213                                 char sanitized[MAXBUF];
214                                 // use GetBuffer to copy single lines into the sanitized string
215                                 std::string single_line = current->GetBuffer();
216                                 current->bytes_in += single_line.length();
217                                 current->cmds_in++;
218                                 if (single_line.length()>512)
219                                 {
220                                         log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
221                                         WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
222                                         kill_link(current,"Excess flood");
223                                         return;
224                                 }
225                                 strlcpy(sanitized,single_line.c_str(),MAXBUF);
226                                 if (*sanitized)
227                                 {
228                                         userrec* old_comp = fd_ref_table[currfd];
229                                         // we're gonna re-scan to check if the nick is gone, after every
230                                         // command - if it has, we're gonna bail
231                                         ServerInstance->Parser->ProcessBuffer(sanitized,current);
232                                         // look for the user's record in case it's changed... if theyve quit,
233                                         // we cant do anything more with their buffer, so bail.
234                                         // there used to be an ugly, slow loop here. Now we have a reference
235                                         // table, life is much easier (and FASTER)
236                                         userrec* new_comp = fd_ref_table[currfd];
237                                         if ((currfd < 0) || (!fd_ref_table[currfd]) || (old_comp != new_comp))
238                                         {
239                                                 return;
240                                         }
241                                         else
242                                         {
243                                                 /* The user is still here, flush their buffer */
244                                                 current->FlushWriteBuf();
245                                         }
246                                 }
247                         }
248                         return;
249                 }
250                 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
251                 {
252                         log(DEBUG,"killing: %s",cu->nick);
253                         kill_link(cu,strerror(errno));
254                         return;
255                 }
256         }
257         // result EAGAIN means nothing read
258         else if ((result == EAGAIN) || (result == -EAGAIN))
259         {
260         }
261         else if (result == 0)
262         {
263                 log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
264                 kill_link(cu,"Client exited");
265                 log(DEBUG,"Bailing from client exit");
266                 return;
267         }
268 }
269
270 void DoSocketTimeouts(time_t TIME)
271 {
272         unsigned int numsockets = module_sockets.size();
273         SocketEngine* SE = ServerInstance->SE;
274         for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
275         {
276                 InspSocket* s = (InspSocket*)*a;
277                 if (s->Timeout(TIME))
278                 {
279                         log(DEBUG,"Socket poll returned false, close and bail");
280                         SE->DelFd(s->GetFd());
281                         s->Close();
282                         module_sockets.erase(a);
283                         delete s;
284                         break;
285                 }
286                 if (module_sockets.size() != numsockets) break;
287         }
288 }
289
290 /**
291  * This function is called once a second from the mainloop.
292  * It is intended to do background checking on all the user structs, e.g.
293  * stuff like ping checks, registration timeouts, etc. This function is
294  * also responsible for checking if InspSocket derived classes are timed out.
295  */
296 void DoBackgroundUserStuff(time_t TIME)
297 {
298         CullList* GlobalGoners = new CullList();
299         for (std::vector<userrec*>::iterator count2 = local_users.begin(); count2 != local_users.end(); count2++)
300         {
301                 /* Sanity checks for corrupted iterators (yes, really) */
302                 userrec* curr = NULL;
303                 if (*count2)
304                         curr = (userrec*)(*count2);
305                 if ((long)curr == -1)
306                         return;
307
308                 if (curr)
309                 {
310                         // registration timeout -- didnt send USER/NICK/HOST in the time specified in
311                         // their connection class.
312                         if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != 7))
313                         {
314                                 log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
315                                 GlobalGoners->AddItem(curr,"Registration timeout");
316                                 continue;
317                         }
318                         // user has signed on with USER/NICK/PASS, and dns has completed, all the modules
319                         // say this user is ok to proceed, fully connect them.
320                         if ((TIME > curr->signon) && (curr->registered == 3) && (AllModulesReportReady(curr)))
321                         {
322                                 curr->dns_done = true;
323                                 ServerInstance->stats->statsDnsBad++;
324                                 FullConnectUser(curr,GlobalGoners);
325                                 continue;
326                         }
327                         if ((curr->dns_done) && (curr->registered == 3) && (AllModulesReportReady(curr)))
328                         {
329                                 log(DEBUG,"dns done, registered=3, and modules ready, OK");
330                                 FullConnectUser(curr,GlobalGoners);
331                                 continue;
332                         }
333                         // It's time to PING this user. Send them a ping.
334                         if ((TIME > curr->nping) && (curr->registered == 7))
335                         {
336                                 // This user didn't answer the last ping, remove them
337                                 if (!curr->lastping)
338                                 {
339                                         GlobalGoners->AddItem(curr,"Ping timeout");
340                                         continue;
341                                 }
342                                 Write(curr->fd,"PING :%s",Config->ServerName);
343                                 curr->lastping = 0;
344                                 curr->nping = TIME+curr->pingmax;
345                         }
346                         // We can flush the write buffer as the last thing we do, because if they
347                         // match any of the above conditions its no use flushing their buffer anyway.
348                         curr->FlushWriteBuf();
349                         if (curr->GetWriteError() != "")
350                         {
351                                 GlobalGoners->AddItem(curr,curr->GetWriteError());
352                                 continue;
353                         }
354                 }
355         }
356         /** Remove all the queued users who are due to be quit
357          */
358         GlobalGoners->Apply();
359         /** Free to memory used
360          */
361         delete GlobalGoners;
362         return;
363 }
364
365 void OpenLog(char** argv, int argc)
366 {
367         if (Config->logpath == "")
368         {
369                 Config->logpath = GetFullProgDir(argv,argc) + "/ircd.log";
370         }
371         Config->log_file = fopen(Config->logpath.c_str(),"a+");
372         if (!Config->log_file)
373         {
374                 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
375                 Exit(ERROR);
376         }
377 }
378
379
380 void CheckRoot()
381 {
382         if (geteuid() == 0)
383         {
384                 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
385                 log(DEFAULT,"InspIRCd: startup: not starting with UID 0!");
386                 Exit(ERROR);
387         }
388 }
389
390
391 void CheckDie()
392 {
393         if (*Config->DieValue)
394         {
395                 printf("WARNING: %s\n\n",Config->DieValue);
396                 log(DEFAULT,"Ut-Oh, somebody didn't read their config file: '%s'",Config->DieValue);
397                 exit(0);
398         }
399 }
400
401 void LoadAllModules(InspIRCd* ServerInstance)
402 {
403         /* We must load the modules AFTER initializing the socket engine, now */
404         MODCOUNT = -1;
405         char configToken[MAXBUF];
406         for (int count = 0; count < Config->ConfValueEnum("module",&Config->config_f); count++)
407         {
408                 Config->ConfValue("module","name",count,configToken,&Config->config_f);
409                 printf("Loading module... \033[1;32m%s\033[0m\n",configToken);
410                 if (!ServerInstance->LoadModule(configToken))
411                 {
412                         log(DEFAULT,"Exiting due to a module loader error.");
413                         printf("\nThere was an error loading a module: %s\n\nYou might want to do './inspircd start' instead of 'bin/inspircd'\n\n",ServerInstance->ModuleError());
414                         Exit(0);
415                 }
416         }
417         log(DEFAULT,"Total loaded modules: %lu",(unsigned long)MODCOUNT+1);
418 }
419
420