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