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