1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
17 /* Now with added unF! ;) */
21 #include "inspircd_config.h"
23 #include "inspircd_io.h"
26 #include <sys/errno.h>
27 #include <sys/ioctl.h>
28 #include <sys/utsname.h>
32 #include <ext/hash_map>
54 #include "inspstring.h"
56 #include "helperfuncs.h"
58 #include "socketengine.h"
59 #include "userprocess.h"
61 #include "command_parse.h"
62 #include "cull_list.h"
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;
71 extern time_t OLDTIME;
72 extern std::vector<userrec*> local_users;
73 char LOG_FILE[MAXBUF];
75 extern InspIRCd* ServerInstance;
76 extern ServerConfig *Config;
77 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
80 extern user_hash clientlist;
81 extern chan_hash chanlist;
83 void ProcessUser(userrec* cu)
86 if (cu->fd == FD_MAGIC_NUMBER)
88 log(DEBUG,"Processing user with fd %d",cu->fd);
89 if (Config->GetIOHook(cu->port))
95 MOD_RESULT = Config->GetIOHook(cu->port)->OnRawSocketRead(cu->fd,data,65535,result2);
96 log(DEBUG,"Data result returned by module: %d",MOD_RESULT);
98 catch (ModuleException& modexcept)
100 log(DEBUG,"Module exception cought: %s",modexcept.GetReason()); \
113 result = cu->ReadData(data, 65535);
115 log(DEBUG,"Read result: %d",result);
116 if ((result) && (result != -EAGAIN))
118 ServerInstance->stats->statsRecv += result;
119 // perform a check on the raw buffer as an array (not a string!) to remove
120 // characters 0 and 7 which are illegal in the RFC - replace them with spaces.
121 // hopefully this should stop even more people whining about "Unknown command: *"
122 for (int checker = 0; checker < result; checker++)
124 if ((data[checker] == 0) || (data[checker] == 7))
129 userrec* current = cu;
130 int currfd = current->fd;
132 // add the data to the users buffer
135 if (!current->AddBuffer(data))
137 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
138 if (current->registered == 7)
140 // Make sure they arn't flooding long lines.
141 if (TIME > current->reset_due)
143 current->reset_due = TIME + current->threshold;
144 current->lines_in = 0;
147 if (current->lines_in > current->flood)
149 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
150 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
151 kill_link(current,"Excess flood");
154 WriteServ(currfd, "NOTICE %s :Your previous line was too long and was not delivered (Over 512chars) Please shorten it.", current->nick);
160 WriteOpers("*** Excess flood from %s",(char*)inet_ntoa(current->ip4));
161 log(DEFAULT,"Excess flood from: %s",(char*)inet_ntoa(current->ip4));
162 add_zline(120,Config->ServerName,"Flood from unregistered connection",(char*)inet_ntoa(current->ip4));
163 apply_lines(APPLY_ZLINES);
167 if (current->recvq.length() > (unsigned)Config->NetBufferSize)
169 if (current->registered == 7)
171 kill_link(current,"RecvQ exceeded");
175 WriteOpers("*** Excess flood from %s",(char*)inet_ntoa(current->ip4));
176 log(DEFAULT,"Excess flood from: %s",(char*)inet_ntoa(current->ip4));
177 add_zline(120,Config->ServerName,"Flood from unregistered connection",(char*)inet_ntoa(current->ip4));
178 apply_lines(APPLY_ZLINES);
182 // while there are complete lines to process...
183 while (current->BufferIsReady())
186 if (TIME > current->reset_due)
188 current->reset_due = TIME + current->threshold;
189 current->lines_in = 0;
192 if (current->lines_in > current->flood)
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");
199 if ((floodlines > current->flood) && (current->flood != 0))
201 if (current->registered == 7)
203 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
204 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
205 kill_link(current,"Excess flood");
209 add_zline(120,Config->ServerName,"Flood from unregistered connection",(char*)inet_ntoa(current->ip4));
210 apply_lines(APPLY_ZLINES);
214 char sanitized[MAXBUF];
215 // use GetBuffer to copy single lines into the sanitized string
216 std::string single_line = current->GetBuffer();
217 current->bytes_in += single_line.length();
219 if (single_line.length()>512)
221 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
222 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
223 kill_link(current,"Excess flood");
226 strlcpy(sanitized,single_line.c_str(),MAXBUF);
229 userrec* old_comp = fd_ref_table[currfd];
230 // we're gonna re-scan to check if the nick is gone, after every
231 // command - if it has, we're gonna bail
232 ServerInstance->Parser->ProcessBuffer(sanitized,current);
233 // look for the user's record in case it's changed... if theyve quit,
234 // we cant do anything more with their buffer, so bail.
235 // there used to be an ugly, slow loop here. Now we have a reference
236 // table, life is much easier (and FASTER)
237 userrec* new_comp = fd_ref_table[currfd];
238 if ((currfd < 0) || (!fd_ref_table[currfd]) || (old_comp != new_comp))
244 /* The user is still here, flush their buffer */
245 current->FlushWriteBuf();
251 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
253 log(DEBUG,"killing: %s",cu->nick);
254 kill_link(cu,strerror(errno));
258 // result EAGAIN means nothing read
259 else if ((result == EAGAIN) || (result == -EAGAIN))
262 else if (result == 0)
264 log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
265 kill_link(cu,"Client exited");
266 log(DEBUG,"Bailing from client exit");
271 void DoSocketTimeouts(time_t TIME)
273 unsigned int numsockets = module_sockets.size();
274 SocketEngine* SE = ServerInstance->SE;
275 for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
277 InspSocket* s = (InspSocket*)*a;
278 if (s->Timeout(TIME))
280 log(DEBUG,"Socket poll returned false, close and bail");
281 SE->DelFd(s->GetFd());
283 module_sockets.erase(a);
287 if (module_sockets.size() != numsockets) break;
292 * This function is called once a second from the mainloop.
293 * It is intended to do background checking on all the user structs, e.g.
294 * stuff like ping checks, registration timeouts, etc. This function is
295 * also responsible for checking if InspSocket derived classes are timed out.
297 void DoBackgroundUserStuff(time_t TIME)
299 CullList* GlobalGoners = new CullList();
300 for (std::vector<userrec*>::iterator count2 = local_users.begin(); count2 != local_users.end(); count2++)
302 /* Sanity checks for corrupted iterators (yes, really) */
303 userrec* curr = NULL;
305 curr = (userrec*)(*count2);
306 if ((long)curr == -1)
311 // registration timeout -- didnt send USER/NICK/HOST in the time specified in
312 // their connection class.
313 if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != 7))
315 log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
316 GlobalGoners->AddItem(curr,"Registration timeout");
319 // user has signed on with USER/NICK/PASS, and dns has completed, all the modules
320 // say this user is ok to proceed, fully connect them.
321 if ((TIME > curr->signon) && (curr->registered == 3) && (AllModulesReportReady(curr)))
323 curr->dns_done = true;
324 ServerInstance->stats->statsDnsBad++;
325 FullConnectUser(curr,GlobalGoners);
328 if ((curr->dns_done) && (curr->registered == 3) && (AllModulesReportReady(curr)))
330 log(DEBUG,"dns done, registered=3, and modules ready, OK");
331 FullConnectUser(curr,GlobalGoners);
334 // It's time to PING this user. Send them a ping.
335 if ((TIME > curr->nping) && (curr->registered == 7))
337 // This user didn't answer the last ping, remove them
340 GlobalGoners->AddItem(curr,"Ping timeout");
343 Write(curr->fd,"PING :%s",Config->ServerName);
345 curr->nping = TIME+curr->pingmax;
347 // We can flush the write buffer as the last thing we do, because if they
348 // match any of the above conditions its no use flushing their buffer anyway.
349 curr->FlushWriteBuf();
350 if (curr->GetWriteError() != "")
352 GlobalGoners->AddItem(curr,curr->GetWriteError());
357 /** Remove all the queued users who are due to be quit
359 GlobalGoners->Apply();
360 /** Free to memory used
366 void OpenLog(char** argv, int argc)
370 if (Config->logpath == "")
372 Config->logpath = GetFullProgDir(argv,argc) + "/ircd.log";
377 Config->log_file = fopen(LOG_FILE,"a+");
378 if (!Config->log_file)
380 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
385 Config->log_file = fopen(Config->logpath.c_str(),"a+");
386 if (!Config->log_file)
388 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
398 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
399 log(DEFAULT,"InspIRCd: startup: not starting with UID 0!");
407 if (*Config->DieValue)
409 printf("WARNING: %s\n\n",Config->DieValue);
410 log(DEFAULT,"Ut-Oh, somebody didn't read their config file: '%s'",Config->DieValue);
415 void LoadAllModules(InspIRCd* ServerInstance)
417 /* We must load the modules AFTER initializing the socket engine, now */
419 char configToken[MAXBUF];
420 for (int count = 0; count < Config->ConfValueEnum("module",&Config->config_f); count++)
422 Config->ConfValue("module","name",count,configToken,&Config->config_f);
423 printf("Loading module... \033[1;32m%s\033[0m\n",configToken);
424 if (!ServerInstance->LoadModule(configToken))
426 log(DEFAULT,"Exiting due to a module loader error.");
427 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());
431 log(DEFAULT,"Total loaded modules: %lu",(unsigned long)MODCOUNT+1);