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