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