]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/userprocess.cpp
24bb91f94d60100bbdb8629764c20a32d335a766
[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                                 strlcpy(sanitized,single_line.c_str(),511);
220                                 if (*sanitized)
221                                 {
222                                         userrec* old_comp = fd_ref_table[currfd];
223                                         // we're gonna re-scan to check if the nick is gone, after every
224                                         // command - if it has, we're gonna bail
225                                         ServerInstance->Parser->ProcessBuffer(sanitized,current);
226                                         // look for the user's record in case it's changed... if theyve quit,
227                                         // we cant do anything more with their buffer, so bail.
228                                         // there used to be an ugly, slow loop here. Now we have a reference
229                                         // table, life is much easier (and FASTER)
230                                         userrec* new_comp = fd_ref_table[currfd];
231                                         if ((currfd < 0) || (!fd_ref_table[currfd]) || (old_comp != new_comp))
232                                         {
233                                                 return;
234                                         }
235                                         else
236                                         {
237                                                 /* The user is still here, flush their buffer */
238                                                 current->FlushWriteBuf();
239                                         }
240                                 }
241                         }
242                         return;
243                 }
244                 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
245                 {
246                         log(DEBUG,"killing: %s",cu->nick);
247                         kill_link(cu,strerror(errno));
248                         return;
249                 }
250         }
251         // result EAGAIN means nothing read
252         else if ((result == EAGAIN) || (result == -EAGAIN))
253         {
254         }
255         else if (result == 0)
256         {
257                 log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
258                 kill_link(cu,"Client exited");
259                 log(DEBUG,"Bailing from client exit");
260                 return;
261         }
262 }
263
264 void DoSocketTimeouts(time_t TIME)
265 {
266         unsigned int numsockets = module_sockets.size();
267         SocketEngine* SE = ServerInstance->SE;
268         for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
269         {
270                 InspSocket* s = (InspSocket*)*a;
271                 if (s->Timeout(TIME))
272                 {
273                         log(DEBUG,"Socket poll returned false, close and bail");
274                         SE->DelFd(s->GetFd());
275                         s->Close();
276                         module_sockets.erase(a);
277                         delete s;
278                         break;
279                 }
280                 if (module_sockets.size() != numsockets) break;
281         }
282 }
283
284 /**
285  * This function is called once a second from the mainloop.
286  * It is intended to do background checking on all the user structs, e.g.
287  * stuff like ping checks, registration timeouts, etc. This function is
288  * also responsible for checking if InspSocket derived classes are timed out.
289  */
290 void DoBackgroundUserStuff(time_t TIME)
291 {
292         CullList* GlobalGoners = new CullList();
293         for (std::vector<userrec*>::iterator count2 = local_users.begin(); count2 != local_users.end(); count2++)
294         {
295                 /* Sanity checks for corrupted iterators (yes, really) */
296                 userrec* curr = NULL;
297                 if (*count2)
298                         curr = (userrec*)(*count2);
299                 if ((long)curr == -1)
300                         return;
301
302                 if (curr)
303                 {
304                         // registration timeout -- didnt send USER/NICK/HOST in the time specified in
305                         // their connection class.
306                         if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != 7))
307                         {
308                                 log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
309                                 GlobalGoners->AddItem(curr,"Registration timeout");
310                                 continue;
311                         }
312                         // user has signed on with USER/NICK/PASS, and dns has completed, all the modules
313                         // say this user is ok to proceed, fully connect them.
314                         if ((TIME > curr->signon) && (curr->registered == 3) && (AllModulesReportReady(curr)))
315                         {
316                                 curr->dns_done = true;
317                                 ServerInstance->stats->statsDnsBad++;
318                                 FullConnectUser(curr,GlobalGoners);
319                                 continue;
320                         }
321                         if ((curr->dns_done) && (curr->registered == 3) && (AllModulesReportReady(curr)))
322                         {
323                                 log(DEBUG,"dns done, registered=3, and modules ready, OK");
324                                 FullConnectUser(curr,GlobalGoners);
325                                 continue;
326                         }
327                         // It's time to PING this user. Send them a ping.
328                         if ((TIME > curr->nping) && (curr->registered == 7))
329                         {
330                                 // This user didn't answer the last ping, remove them
331                                 if (!curr->lastping)
332                                 {
333                                         GlobalGoners->AddItem(curr,"Ping timeout");
334                                         continue;
335                                 }
336                                 Write(curr->fd,"PING :%s",Config->ServerName);
337                                 curr->lastping = 0;
338                                 curr->nping = TIME+curr->pingmax;
339                         }
340                         // We can flush the write buffer as the last thing we do, because if they
341                         // match any of the above conditions its no use flushing their buffer anyway.
342                         curr->FlushWriteBuf();
343                         if (curr->GetWriteError() != "")
344                         {
345                                 GlobalGoners->AddItem(curr,curr->GetWriteError());
346                                 continue;
347                         }
348                 }
349         }
350         /** Remove all the queued users who are due to be quit
351          */
352         GlobalGoners->Apply();
353         /** Free to memory used
354          */
355         delete GlobalGoners;
356         return;
357 }
358
359 void OpenLog(char** argv, int argc)
360 {
361         if (!*LOG_FILE)
362         {
363                 if (Config->logpath == "")
364                 {
365                         Config->logpath = GetFullProgDir(argv,argc) + "/ircd.log";
366                 }
367         }
368         else
369         {
370                 Config->log_file = fopen(LOG_FILE,"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                 return;
377         }
378         Config->log_file = fopen(Config->logpath.c_str(),"a+");
379         if (!Config->log_file)
380         {
381                 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
382                 Exit(ERROR);
383         }
384 }
385
386
387 void CheckRoot()
388 {
389         if (geteuid() == 0)
390         {
391                 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
392                 log(DEFAULT,"InspIRCd: startup: not starting with UID 0!");
393                 Exit(ERROR);
394         }
395 }
396
397
398 void CheckDie()
399 {
400         if (*Config->DieValue)
401         {
402                 printf("WARNING: %s\n\n",Config->DieValue);
403                 log(DEFAULT,"Uh-Oh, somebody didn't read their config file: '%s'",Config->DieValue);
404                 Exit(ERROR);
405         }
406 }
407
408 void LoadAllModules(InspIRCd* ServerInstance)
409 {
410         /* We must load the modules AFTER initializing the socket engine, now */
411         MODCOUNT = -1;
412         char configToken[MAXBUF];
413         for (int count = 0; count < Config->ConfValueEnum("module",&Config->config_f); count++)
414         {
415                 Config->ConfValue("module","name",count,configToken,&Config->config_f);
416                 printf("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
417                 if (!ServerInstance->LoadModule(configToken))
418                 {
419                         log(DEFAULT,"Exiting due to a module loader error.");
420                         printf("\nThere was an error loading a module: %s\n\n",ServerInstance->ModuleError());
421                         Exit(0);
422                 }
423         }
424         log(DEFAULT,"Total loaded modules: %lu",(unsigned long)MODCOUNT+1);
425 }
426
427