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)
87 if (cu->fd == FD_MAGIC_NUMBER)
90 log(DEBUG,"Processing user with fd %d",cu->fd);
92 if (Config->GetIOHook(cu->port))
99 MOD_RESULT = Config->GetIOHook(cu->port)->OnRawSocketRead(cu->fd,data,65535,result2);
100 log(DEBUG,"Data result returned by module: %d",MOD_RESULT);
102 catch (ModuleException& modexcept)
104 log(DEBUG,"Module exception cought: %s",modexcept.GetReason()); \
118 result = cu->ReadData(data, 65535);
121 log(DEBUG,"Read result: %d",result);
123 if ((result) && (result != -EAGAIN))
129 ServerInstance->stats->statsRecv += result;
131 * perform a check on the raw buffer as an array (not a string!) to remove
132 * characters 0 and 7 which are illegal in the RFC - replace them with spaces.
133 * hopefully this should stop even more people whining about "Unknown command: *"
137 * XXX - potential replacement for the below using my beloved pointers. --w00t
139 * for (char *c = data; data && *data; data++)
141 * if (*data == 0 || *data == 7)
146 for (int checker = 0; checker < result; checker++)
148 if ((data[checker] == 0) || (data[checker] == 7))
156 currfd = current->fd;
159 // add the data to the users buffer
162 if (!current->AddBuffer(data))
164 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
165 if (current->registered == 7)
167 // Make sure they arn't flooding long lines.
168 if (TIME > current->reset_due)
170 current->reset_due = TIME + current->threshold;
171 current->lines_in = 0;
176 if (current->lines_in > current->flood)
178 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
179 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
180 kill_link(current,"Excess flood");
185 WriteServ(currfd, "NOTICE %s :Your previous line was too long and was not delivered (Over 512chars) Please shorten it.", current->nick);
191 WriteOpers("*** Excess flood from %s",(char*)inet_ntoa(current->ip4));
192 log(DEFAULT,"Excess flood from: %s",(char*)inet_ntoa(current->ip4));
193 add_zline(120,Config->ServerName,"Flood from unregistered connection",(char*)inet_ntoa(current->ip4));
194 apply_lines(APPLY_ZLINES);
200 if (current->recvq.length() > (unsigned)Config->NetBufferSize)
202 if (current->registered == 7)
204 kill_link(current,"RecvQ exceeded");
208 WriteOpers("*** Excess flood from %s",(char*)inet_ntoa(current->ip4));
209 log(DEFAULT,"Excess flood from: %s",(char*)inet_ntoa(current->ip4));
210 add_zline(120,Config->ServerName,"Flood from unregistered connection",(char*)inet_ntoa(current->ip4));
211 apply_lines(APPLY_ZLINES);
217 // while there are complete lines to process...
218 while (current->BufferIsReady())
220 char sanitized[MAXBUF];
223 if (TIME > current->reset_due)
225 current->reset_due = TIME + current->threshold;
226 current->lines_in = 0;
231 if (current->lines_in > current->flood)
233 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
234 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
235 kill_link(current,"Excess flood");
239 if ((floodlines > current->flood) && (current->flood != 0))
241 if (current->registered == 7)
243 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
244 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
245 kill_link(current,"Excess flood");
249 add_zline(120,Config->ServerName,"Flood from unregistered connection",(char*)inet_ntoa(current->ip4));
250 apply_lines(APPLY_ZLINES);
256 // use GetBuffer to copy single lines into the sanitized string
257 std::string single_line = current->GetBuffer();
258 current->bytes_in += single_line.length();
260 strlcpy(sanitized,single_line.c_str(),511);
264 userrec* old_comp = fd_ref_table[currfd];
266 ServerInstance->Parser->ProcessBuffer(sanitized,current);
268 * look for the user's record in case it's changed... if theyve quit,
269 * we cant do anything more with their buffer, so bail.
270 * there used to be an ugly, slow loop here. Now we have a reference
271 * table, life is much easier (and FASTER)
273 userrec* new_comp = fd_ref_table[currfd];
274 if ((currfd < 0) || (!fd_ref_table[currfd]) || (old_comp != new_comp))
280 /* The user is still here, flush their buffer */
281 current->FlushWriteBuf();
289 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
291 log(DEBUG,"killing: %s",cu->nick);
292 kill_link(cu,strerror(errno));
297 // result EAGAIN means nothing read
298 else if ((result == EAGAIN) || (result == -EAGAIN))
302 else if (result == 0)
304 log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
305 kill_link(cu,"Client exited");
306 log(DEBUG,"Bailing from client exit");
311 void DoSocketTimeouts(time_t TIME)
313 unsigned int numsockets = module_sockets.size();
314 SocketEngine* SE = ServerInstance->SE;
316 for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
318 InspSocket* s = (InspSocket*)*a;
319 if (s->Timeout(TIME))
321 log(DEBUG,"Socket poll returned false, close and bail");
322 SE->DelFd(s->GetFd());
324 module_sockets.erase(a);
329 if (module_sockets.size() != numsockets)
335 * This function is called once a second from the mainloop.
336 * It is intended to do background checking on all the user structs, e.g.
337 * stuff like ping checks, registration timeouts, etc. This function is
338 * also responsible for checking if InspSocket derived classes are timed out.
340 void DoBackgroundUserStuff(time_t TIME)
342 CullList* GlobalGoners = new CullList();
344 for (std::vector<userrec*>::iterator count2 = local_users.begin(); count2 != local_users.end(); count2++)
346 /* Sanity checks for corrupted iterators (yes, really) */
347 userrec* curr = NULL;
350 curr = (userrec*)(*count2);
351 if ((long)curr == -1)
357 * registration timeout -- didnt send USER/NICK/HOST
358 * in the time specified in their connection class.
360 if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != 7))
362 log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
363 GlobalGoners->AddItem(curr,"Registration timeout");
368 * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
369 * say this user is ok to proceed, fully connect them.
371 if ((TIME > curr->signon) && (curr->registered == 3) && (AllModulesReportReady(curr)))
373 curr->dns_done = true;
374 ServerInstance->stats->statsDnsBad++;
375 FullConnectUser(curr,GlobalGoners);
379 if ((curr->dns_done) && (curr->registered == 3) && (AllModulesReportReady(curr)))
381 log(DEBUG,"dns done, registered=3, and modules ready, OK");
382 FullConnectUser(curr,GlobalGoners);
386 // It's time to PING this user. Send them a ping.
387 if ((TIME > curr->nping) && (curr->registered == 7))
389 // This user didn't answer the last ping, remove them
392 GlobalGoners->AddItem(curr,"Ping timeout");
396 Write(curr->fd,"PING :%s",Config->ServerName);
398 curr->nping = TIME+curr->pingmax;
402 * We can flush the write buffer as the last thing we do, because if they
403 * match any of the above conditions its no use flushing their buffer anyway.
405 curr->FlushWriteBuf();
407 if (curr->GetWriteError() != "")
409 GlobalGoners->AddItem(curr,curr->GetWriteError());
415 /* Remove all the queued users who are due to be quit, free memory used. */
416 GlobalGoners->Apply();
421 void OpenLog(char** argv, int argc)
425 if (Config->logpath == "")
427 Config->logpath = GetFullProgDir(argv,argc) + "/ircd.log";
432 Config->log_file = fopen(LOG_FILE,"a+");
434 if (!Config->log_file)
436 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
443 Config->log_file = fopen(Config->logpath.c_str(),"a+");
445 if (!Config->log_file)
447 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
457 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
458 log(DEFAULT,"InspIRCd: startup: not starting with UID 0!");
466 if (*Config->DieValue)
468 printf("WARNING: %s\n\n",Config->DieValue);
469 log(DEFAULT,"Uh-Oh, somebody didn't read their config file: '%s'",Config->DieValue);
474 /* We must load the modules AFTER initializing the socket engine, now */
475 void LoadAllModules(InspIRCd* ServerInstance)
477 char configToken[MAXBUF];
481 for (int count = 0; count < Config->ConfValueEnum("module",&Config->config_f); count++)
483 Config->ConfValue("module","name",count,configToken,&Config->config_f);
484 printf("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
486 if (!ServerInstance->LoadModule(configToken))
488 log(DEFAULT,"Exiting due to a module loader error.");
489 printf("\nThere was an error loading a module: %s\n\n",ServerInstance->ModuleError());
494 log(DEFAULT,"Total loaded modules: %lu",(unsigned long)MODCOUNT+1);