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