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