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