]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/userprocess.cpp
Changed two fields in ConnectClass to strings, moved constructor stuff to init-list
[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 ServerConfig *Config;
74 extern userrec* fd_ref_table[65536];
75 char data[65536];
76
77 extern user_hash clientlist;
78 extern chan_hash chanlist;
79
80 void ProcessUser(userrec* cu)
81 {
82         int result = EAGAIN;
83         log(DEBUG,"Processing user with fd %d",cu->fd);
84         if (Config->GetIOHook(cu->port))
85         {
86                 int result2 = 0;
87                 int MOD_RESULT = Config->GetIOHook(cu->port)->OnRawSocketRead(cu->fd,data,65535,result2);
88                 log(DEBUG,"Data result returned by module: %d",MOD_RESULT);
89                 if (MOD_RESULT < 0)
90                 {
91                         result = EAGAIN;
92                 }
93                 else
94                 {
95                         result = result2;
96                 }
97         }
98         else
99         {
100                 result = cu->ReadData(data, 65535);
101         }
102         log(DEBUG,"Read result: %d",result);
103         if (result)
104         {
105                 ServerInstance->stats->statsRecv += result;
106                 // perform a check on the raw buffer as an array (not a string!) to remove
107                 // characters 0 and 7 which are illegal in the RFC - replace them with spaces.
108                 // hopefully this should stop even more people whining about "Unknown command: *"
109                 for (int checker = 0; checker < result; checker++)
110                 {
111                         if ((data[checker] == 0) || (data[checker] == 7))
112                                 data[checker] = ' ';
113                 }
114                 if (result > 0)
115                         data[result] = '\0';
116                 userrec* current = cu;
117                 int currfd = current->fd;
118                 int floodlines = 0;
119                 // add the data to the users buffer
120                 if (result > 0)
121                 {
122                         if (!current->AddBuffer(data))
123                         {
124                                 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
125                                 if (current->registered == 7)
126                                 {
127                                         kill_link(current,"RecvQ exceeded");
128                                 }
129                                 else
130                                 {
131                                         WriteOpers("*** Excess flood from %s",current->ip);
132                                         log(DEFAULT,"Excess flood from: %s",current->ip);
133                                         add_zline(120,Config->ServerName,"Flood from unregistered connection",current->ip);
134                                         apply_lines(APPLY_ZLINES);
135                                 }
136                                 return;
137                         }
138                         if (current->recvq.length() > (unsigned)Config->NetBufferSize)
139                         {
140                                 if (current->registered == 7)
141                                 {
142                                         kill_link(current,"RecvQ exceeded");
143                                 }
144                                 else
145                                 {
146                                         WriteOpers("*** Excess flood from %s",current->ip);
147                                         log(DEFAULT,"Excess flood from: %s",current->ip);
148                                         add_zline(120,Config->ServerName,"Flood from unregistered connection",current->ip);
149                                         apply_lines(APPLY_ZLINES);
150                                 }
151                                 return;
152                         }
153                         // while there are complete lines to process...
154                         while (current->BufferIsReady())
155                         {
156                                 floodlines++;
157                                 if (TIME > current->reset_due)
158                                 {
159                                         current->reset_due = TIME + current->threshold;
160                                         current->lines_in = 0;
161                                 }
162                                 current->lines_in++;
163                                 if (current->lines_in > current->flood)
164                                 {
165                                         log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
166                                         WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
167                                         kill_link(current,"Excess flood");
168                                         return;
169                                 }
170                                 if ((floodlines > current->flood) && (current->flood != 0))
171                                 {
172                                         if (current->registered == 7)
173                                         {
174                                                 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
175                                                 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
176                                                 kill_link(current,"Excess flood");
177                                         }
178                                         else
179                                         {
180                                                 add_zline(120,Config->ServerName,"Flood from unregistered connection",current->ip);
181                                                 apply_lines(APPLY_ZLINES);
182                                         }
183                                         return;
184                                 }
185                                 char sanitized[MAXBUF];
186                                 // use GetBuffer to copy single lines into the sanitized string
187                                 std::string single_line = current->GetBuffer();
188                                 current->bytes_in += single_line.length();
189                                 current->cmds_in++;
190                                 if (single_line.length()>512)
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                                 strlcpy(sanitized,single_line.c_str(),MAXBUF);
198                                 if (*sanitized)
199                                 {
200                                         userrec* old_comp = fd_ref_table[currfd];
201                                         // we're gonna re-scan to check if the nick is gone, after every
202                                         // command - if it has, we're gonna bail
203                                         ServerInstance->Parser->ProcessBuffer(sanitized,current);
204                                         // look for the user's record in case it's changed... if theyve quit,
205                                         // we cant do anything more with their buffer, so bail.
206                                         // there used to be an ugly, slow loop here. Now we have a reference
207                                         // table, life is much easier (and FASTER)
208                                         userrec* new_comp = fd_ref_table[currfd];
209                                         if ((currfd < 0) || (!fd_ref_table[currfd]) || (old_comp != new_comp))
210                                         {
211                                                 return;
212                                         }
213                                         else
214                                         {
215                                                 /* The user is still here, flush their buffer */
216                                                 current->FlushWriteBuf();
217                                         }
218                                 }
219                         }
220                         return;
221                 }
222                 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
223                 {
224                         log(DEBUG,"killing: %s",cu->nick);
225                         kill_link(cu,strerror(errno));
226                         return;
227                 }
228         }
229         // result EAGAIN means nothing read
230         else if (result == EAGAIN)
231         {
232         }
233         else if (result == 0)
234         {
235                 log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
236                 kill_link(cu,"Client exited");
237                 log(DEBUG,"Bailing from client exit");
238                 return;
239         }
240 }
241
242
243 /**
244  * This function is called once a second from the mainloop.
245  * It is intended to do background checking on all the user structs, e.g.
246  * stuff like ping checks, registration timeouts, etc.
247  * The function returns false when it is finished, and true if
248  * it needs to be run again (e.g. it has processed one of a batch of
249  * QUIT messages, but couldnt continue iterating because the iterator
250  * became invalid). This function is also responsible for checking
251  * if InspSocket derived classes are timed out.
252  */
253 bool DoBackgroundUserStuff(time_t TIME)
254 {
255         unsigned int numsockets = module_sockets.size();
256         SocketEngine* SE = ServerInstance->SE;
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 (std::vector<userrec*>::iterator count2 = local_users.begin(); count2 != local_users.end(); count2++)
274         {
275                 /* Sanity checks for corrupted iterators (yes, really) */
276                 userrec* curr = NULL;
277                 if (*count2)
278                         curr = (userrec*)(*count2);
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                                         ServerInstance->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(InspIRCd* ServerInstance)
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 < Config->ConfValueEnum("module",&Config->config_f); count++)
382         {
383                 Config->ConfValue("module","name",count,configToken,&Config->config_f);
384                 printf("Loading module... \033[1;32m%s\033[0m\n",configToken);
385                 if (!ServerInstance->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",ServerInstance->ModuleError());
389                         Exit(0);
390                 }
391         }
392         log(DEFAULT,"Total loaded modules: %lu",(unsigned long)MODCOUNT+1);
393 }
394
395