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