]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/userprocess.cpp
GROK!
[user/henk/code/inspircd.git] / src / userprocess.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2005 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 "typedefs.h"
60 #include "command_parse.h"
61
62 extern int MODCOUNT;
63 extern struct sockaddr_in client,server;
64 extern socklen_t length;
65 extern std::vector<Module*> modules;
66 extern std::vector<ircd_module*> factory;
67 extern std::vector<InspSocket*> module_sockets;
68 extern time_t TIME;
69 extern time_t OLDTIME;
70 extern std::vector<userrec*> local_users;
71
72 extern InspIRCd* ServerInstance;
73 extern SocketEngine* SE;
74 extern serverstats* stats;
75 extern ServerConfig *Config;
76 extern CommandParser *Parser;
77
78 extern userrec* fd_ref_table[65536];
79 char data[65536];
80
81 extern user_hash clientlist;
82 extern chan_hash chanlist;
83
84 extern Module* IOHookModule;
85
86 void ProcessUser(userrec* cu)
87 {
88         int result = EAGAIN;
89         log(DEBUG,"Processing user with fd %d",cu->fd);
90         if (IOHookModule)
91         {
92                 int MOD_RESULT = 0;
93                 int result2 = 0;
94                 IOHookModule->OnRawSocketRead(cu->fd,data,65535,result2);
95                 if (!MOD_RESULT)
96                 {
97                         result = cu->ReadData(data, 65535);
98                 }
99                 else
100                 {
101                         log(DEBUG,"Data result returned by module: %d",MOD_RESULT);
102                         result = result2;
103                 }
104         }
105         else
106         {
107                 result = cu->ReadData(data, 65535);
108         }
109         log(DEBUG,"Read result: %d",result);
110         if (result)
111         {
112                 stats->statsRecv += result;
113                 // perform a check on the raw buffer as an array (not a string!) to remove
114                 // characters 0 and 7 which are illegal in the RFC - replace them with spaces.
115                 // hopefully this should stop even more people whining about "Unknown command: *"
116                 for (int checker = 0; checker < result; checker++)
117                 {
118                         if ((data[checker] == 0) || (data[checker] == 7))
119                                 data[checker] = ' ';
120                 }
121                 if (result > 0)
122                         data[result] = '\0';
123                 userrec* current = cu;
124                 int currfd = current->fd;
125                 int floodlines = 0;
126                 // add the data to the users buffer
127                 if (result > 0)
128                 {
129                         if (!current->AddBuffer(data))
130                         {
131                                 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
132                                 if (current->registered == 7)
133                                 {
134                                         kill_link(current,"RecvQ exceeded");
135                                 }
136                                 else
137                                 {
138                                         WriteOpers("*** Excess flood from %s",current->ip);
139                                         log(DEFAULT,"Excess flood from: %s",current->ip);
140                                         add_zline(120,Config->ServerName,"Flood from unregistered connection",current->ip);
141                                         apply_lines(APPLY_ZLINES);
142                                 }
143                                 return;
144                         }
145                         if (current->recvq.length() > (unsigned)Config->NetBufferSize)
146                         {
147                                 if (current->registered == 7)
148                                 {
149                                         kill_link(current,"RecvQ exceeded");
150                                 }
151                                 else
152                                 {
153                                         WriteOpers("*** Excess flood from %s",current->ip);
154                                         log(DEFAULT,"Excess flood from: %s",current->ip);
155                                         add_zline(120,Config->ServerName,"Flood from unregistered connection",current->ip);
156                                         apply_lines(APPLY_ZLINES);
157                                 }
158                                 return;
159                         }
160                         // while there are complete lines to process...
161                         while (current->BufferIsReady())
162                         {
163                                 floodlines++;
164                                 if (TIME > current->reset_due)
165                                 {
166                                         current->reset_due = TIME + current->threshold;
167                                         current->lines_in = 0;
168                                 }
169                                 current->lines_in++;
170                                 if (current->lines_in > current->flood)
171                                 {
172                                         log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
173                                         WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
174                                         kill_link(current,"Excess flood");
175                                         return;
176                                 }
177                                 if ((floodlines > current->flood) && (current->flood != 0))
178                                 {
179                                         if (current->registered == 7)
180                                         {
181                                                 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
182                                                 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
183                                                 kill_link(current,"Excess flood");
184                                         }
185                                         else
186                                         {
187                                                 add_zline(120,Config->ServerName,"Flood from unregistered connection",current->ip);
188                                                 apply_lines(APPLY_ZLINES);
189                                         }
190                                         return;
191                                 }
192                                 char sanitized[MAXBUF];
193                                 // use GetBuffer to copy single lines into the sanitized string
194                                 std::string single_line = current->GetBuffer();
195                                 current->bytes_in += single_line.length();
196                                 current->cmds_in++;
197                                 if (single_line.length()>512)
198                                 {
199                                         log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
200                                         WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
201                                         kill_link(current,"Excess flood");
202                                         return;
203                                 }
204                                 strlcpy(sanitized,single_line.c_str(),MAXBUF);
205                                 if (*sanitized)
206                                 {
207                                         userrec* old_comp = fd_ref_table[currfd];
208                                         // we're gonna re-scan to check if the nick is gone, after every
209                                         // command - if it has, we're gonna bail
210                                         Parser->ProcessBuffer(sanitized,current);
211                                         // look for the user's record in case it's changed... if theyve quit,
212                                         // we cant do anything more with their buffer, so bail.
213                                         // there used to be an ugly, slow loop here. Now we have a reference
214                                         // table, life is much easier (and FASTER)
215                                         userrec* new_comp = fd_ref_table[currfd];
216                                         if ((currfd < 0) || (!fd_ref_table[currfd]) || (old_comp != new_comp))
217                                         {
218                                                 return;
219                                         }
220                                         else
221                                         {
222                                                 /* The user is still here, flush their buffer */
223                                                 current->FlushWriteBuf();
224                                         }
225                                 }
226                         }
227                         return;
228                 }
229                 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
230                 {
231                         log(DEBUG,"killing: %s",cu->nick);
232                         kill_link(cu,strerror(errno));
233                         return;
234                 }
235         }
236         // result EAGAIN means nothing read
237         else if (result == EAGAIN)
238         {
239         }
240         else if (result == 0)
241         {
242                 log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
243                 kill_link(cu,"Client exited");
244                 log(DEBUG,"Bailing from client exit");
245                 return;
246         }
247 }
248
249
250 /**
251  * This function is called once a second from the mainloop.
252  * It is intended to do background checking on all the user structs, e.g.
253  * stuff like ping checks, registration timeouts, etc.
254  * The function returns false when it is finished, and true if
255  * it needs to be run again (e.g. it has processed one of a batch of
256  * QUIT messages, but couldnt continue iterating because the iterator
257  * became invalid). This function is also responsible for checking
258  * if InspSocket derived classes are timed out.
259  */
260 bool DoBackgroundUserStuff(time_t TIME)
261 {
262         unsigned int numsockets = module_sockets.size();
263         for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
264         {
265                 InspSocket* s = (InspSocket*)*a;
266                 if (s->Timeout(TIME))
267                 {
268                         log(DEBUG,"Socket poll returned false, close and bail");
269                         SE->DelFd(s->GetFd());
270                         s->Close();
271                         module_sockets.erase(a);
272                         delete s;
273                         break;
274                 }
275                 if (module_sockets.size() != numsockets) break;
276         }
277         /* TODO: We need a seperate hash containing only local users for this
278          */
279         for (std::vector<userrec*>::iterator count2 = local_users.begin(); count2 != local_users.end(); count2++)
280         {
281                 /* Sanity checks for corrupted iterators (yes, really) */
282                 userrec* curr = NULL;
283                 if (*count2)
284                         curr = (userrec*)(*count2);
285                 if ((long)curr == -1)
286                         return false;
287
288                 if ((curr) && (curr->fd != 0)) /* XXX - why are we checking fd != 0? --w00t */
289                 {
290                         int currfd = curr->fd;
291                         // we don't check the state of remote users.
292                         if (IS_LOCAL(curr))
293                         {
294                                 curr->FlushWriteBuf();
295                                 if (curr->GetWriteError() != "")
296                                 {
297                                         log(DEBUG,"InspIRCd: write error: %s",curr->GetWriteError().c_str());
298                                         kill_link(curr,curr->GetWriteError().c_str());
299                                         return true;
300                                 }
301                                 // registration timeout -- didnt send USER/NICK/HOST in the time specified in
302                                 // their connection class.
303                                 if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != 7))
304                                 {
305                                         log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
306                                         kill_link(curr,"Registration timeout");
307                                         return true;
308                                 }
309                                 if ((TIME > curr->signon) && (curr->registered == 3) && (AllModulesReportReady(curr)))
310                                 {
311                                         log(DEBUG,"signon exceed, registered=3, and modules ready, OK: %d %d",TIME,curr->signon);
312                                         curr->dns_done = true;
313                                         stats->statsDnsBad++;
314                                         FullConnectUser(curr);
315                                         if (fd_ref_table[currfd] != curr) // something changed, bail pronto
316                                                 return true;
317                                  }
318                                  if ((curr->dns_done) && (curr->registered == 3) && (AllModulesReportReady(curr)))
319                                  {
320                                        log(DEBUG,"dns done, registered=3, and modules ready, OK");
321                                        FullConnectUser(curr);
322                                        if (fd_ref_table[currfd] != curr) // something changed, bail pronto
323                                                 return true;
324                                  }
325                                  if ((TIME > curr->nping) && (isnick(curr->nick)) && (curr->registered == 7))
326                                  {
327                                        if ((!curr->lastping) && (curr->registered == 7))
328                                        {
329                                                log(DEBUG,"InspIRCd: ping timeout: %s",curr->nick);
330                                                kill_link(curr,"Ping timeout");
331                                                return true;
332                                        }
333                                        Write(curr->fd,"PING :%s",Config->ServerName);
334                                        log(DEBUG,"InspIRCd: pinging: %s",curr->nick);
335                                        curr->lastping = 0;
336                                        curr->nping = TIME+curr->pingmax;       // was hard coded to 120
337                                 }
338                         }
339                 }
340         }
341         return false;
342 }
343
344 void OpenLog(char** argv, int argc)
345 {
346         std::string logpath = GetFullProgDir(argv,argc) + "/ircd.log";
347         Config->log_file = fopen(logpath.c_str(),"a+");
348         if (!Config->log_file)
349         {
350                 printf("ERROR: Could not write to logfile %s, bailing!\n\n",logpath.c_str());
351                 Exit(ERROR);
352         }
353 #ifdef IS_CYGWIN
354         printf("Logging to ircd.log...\n");
355 #else
356         printf("Logging to %s...\n",logpath.c_str());
357 #endif
358 }
359
360
361 void CheckRoot()
362 {
363         if (geteuid() == 0)
364         {
365                 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
366                 log(DEFAULT,"InspIRCd: startup: not starting with UID 0!");
367                 Exit(ERROR);
368         }
369 }
370
371
372 void CheckDie()
373 {
374         if (*Config->DieValue)
375         {
376                 printf("WARNING: %s\n\n",Config->DieValue);
377                 log(DEFAULT,"Ut-Oh, somebody didn't read their config file: '%s'",Config->DieValue);
378                 exit(0);
379         }
380 }
381
382 void LoadAllModules()
383 {
384         /* We must load the modules AFTER initializing the socket engine, now */
385         MODCOUNT = -1;
386         char configToken[MAXBUF];
387         for (int count = 0; count < Config->ConfValueEnum("module",&Config->config_f); count++)
388         {
389                 Config->ConfValue("module","name",count,configToken,&Config->config_f);
390                 printf("Loading module... \033[1;32m%s\033[0m\n",configToken);
391                 if (!ServerInstance->LoadModule(configToken))
392                 {
393                         log(DEFAULT,"Exiting due to a module loader error.");
394                         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());
395                         Exit(0);
396                 }
397         }
398         log(DEFAULT,"Total loaded modules: %lu",(unsigned long)MODCOUNT+1);
399 }
400
401