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;
74 extern InspIRCd* ServerInstance;
75 extern ServerConfig *Config;
76 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
79 extern user_hash clientlist;
80 extern chan_hash chanlist;
82 void ProcessUser(userrec* cu)
85 if (cu->fd == FD_MAGIC_NUMBER)
87 log(DEBUG,"Processing user with fd %d",cu->fd);
88 if (Config->GetIOHook(cu->port))
94 MOD_RESULT = Config->GetIOHook(cu->port)->OnRawSocketRead(cu->fd,data,65535,result2);
95 log(DEBUG,"Data result returned by module: %d",MOD_RESULT);
97 catch (ModuleException& modexcept)
99 log(DEBUG,"Module exception cought: %s",modexcept.GetReason()); \
112 result = cu->ReadData(data, 65535);
114 log(DEBUG,"Read result: %d",result);
115 if ((result) && (result != -EAGAIN))
117 ServerInstance->stats->statsRecv += result;
118 // perform a check on the raw buffer as an array (not a string!) to remove
119 // characters 0 and 7 which are illegal in the RFC - replace them with spaces.
120 // hopefully this should stop even more people whining about "Unknown command: *"
121 for (int checker = 0; checker < result; checker++)
123 if ((data[checker] == 0) || (data[checker] == 7))
128 userrec* current = cu;
129 int currfd = current->fd;
131 // add the data to the users buffer
134 if (!current->AddBuffer(data))
136 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
137 if (current->registered == 7)
139 // Make sure they arn't flooding long lines.
140 if (TIME > current->reset_due)
142 current->reset_due = TIME + current->threshold;
143 current->lines_in = 0;
146 if (current->lines_in > current->flood)
148 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
149 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
150 kill_link(current,"Excess flood");
153 WriteServ(currfd, "NOTICE %s :Your previous line was too long and was not delivered (Over 512chars) Please shorten it.", current->nick);
159 WriteOpers("*** Excess flood from %s",(char*)inet_ntoa(current->ip4));
160 log(DEFAULT,"Excess flood from: %s",(char*)inet_ntoa(current->ip4));
161 add_zline(120,Config->ServerName,"Flood from unregistered connection",(char*)inet_ntoa(current->ip4));
162 apply_lines(APPLY_ZLINES);
166 if (current->recvq.length() > (unsigned)Config->NetBufferSize)
168 if (current->registered == 7)
170 kill_link(current,"RecvQ exceeded");
174 WriteOpers("*** Excess flood from %s",(char*)inet_ntoa(current->ip4));
175 log(DEFAULT,"Excess flood from: %s",(char*)inet_ntoa(current->ip4));
176 add_zline(120,Config->ServerName,"Flood from unregistered connection",(char*)inet_ntoa(current->ip4));
177 apply_lines(APPLY_ZLINES);
181 // while there are complete lines to process...
182 while (current->BufferIsReady())
185 if (TIME > current->reset_due)
187 current->reset_due = TIME + current->threshold;
188 current->lines_in = 0;
191 if (current->lines_in > current->flood)
193 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
194 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
195 kill_link(current,"Excess flood");
198 if ((floodlines > current->flood) && (current->flood != 0))
200 if (current->registered == 7)
202 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
203 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
204 kill_link(current,"Excess flood");
208 add_zline(120,Config->ServerName,"Flood from unregistered connection",(char*)inet_ntoa(current->ip4));
209 apply_lines(APPLY_ZLINES);
213 char sanitized[MAXBUF];
214 // use GetBuffer to copy single lines into the sanitized string
215 std::string single_line = current->GetBuffer();
216 current->bytes_in += single_line.length();
218 if (single_line.length()>512)
220 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
221 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
222 kill_link(current,"Excess flood");
225 strlcpy(sanitized,single_line.c_str(),MAXBUF);
228 userrec* old_comp = fd_ref_table[currfd];
229 // we're gonna re-scan to check if the nick is gone, after every
230 // command - if it has, we're gonna bail
231 ServerInstance->Parser->ProcessBuffer(sanitized,current);
232 // look for the user's record in case it's changed... if theyve quit,
233 // we cant do anything more with their buffer, so bail.
234 // there used to be an ugly, slow loop here. Now we have a reference
235 // table, life is much easier (and FASTER)
236 userrec* new_comp = fd_ref_table[currfd];
237 if ((currfd < 0) || (!fd_ref_table[currfd]) || (old_comp != new_comp))
243 /* The user is still here, flush their buffer */
244 current->FlushWriteBuf();
250 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
252 log(DEBUG,"killing: %s",cu->nick);
253 kill_link(cu,strerror(errno));
257 // result EAGAIN means nothing read
258 else if ((result == EAGAIN) || (result == -EAGAIN))
261 else if (result == 0)
263 log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
264 kill_link(cu,"Client exited");
265 log(DEBUG,"Bailing from client exit");
270 void DoSocketTimeouts(time_t TIME)
272 unsigned int numsockets = module_sockets.size();
273 SocketEngine* SE = ServerInstance->SE;
274 for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
276 InspSocket* s = (InspSocket*)*a;
277 if (s->Timeout(TIME))
279 log(DEBUG,"Socket poll returned false, close and bail");
280 SE->DelFd(s->GetFd());
282 module_sockets.erase(a);
286 if (module_sockets.size() != numsockets) break;
291 * This function is called once a second from the mainloop.
292 * It is intended to do background checking on all the user structs, e.g.
293 * stuff like ping checks, registration timeouts, etc. This function is
294 * also responsible for checking if InspSocket derived classes are timed out.
296 void DoBackgroundUserStuff(time_t TIME)
298 CullList* GlobalGoners = new CullList();
299 for (std::vector<userrec*>::iterator count2 = local_users.begin(); count2 != local_users.end(); count2++)
301 /* Sanity checks for corrupted iterators (yes, really) */
302 userrec* curr = NULL;
304 curr = (userrec*)(*count2);
305 if ((long)curr == -1)
310 // registration timeout -- didnt send USER/NICK/HOST in the time specified in
311 // their connection class.
312 if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != 7))
314 log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
315 GlobalGoners->AddItem(curr,"Registration timeout");
318 // user has signed on with USER/NICK/PASS, and dns has completed, all the modules
319 // say this user is ok to proceed, fully connect them.
320 if ((TIME > curr->signon) && (curr->registered == 3) && (AllModulesReportReady(curr)))
322 curr->dns_done = true;
323 ServerInstance->stats->statsDnsBad++;
324 FullConnectUser(curr,GlobalGoners);
327 if ((curr->dns_done) && (curr->registered == 3) && (AllModulesReportReady(curr)))
329 log(DEBUG,"dns done, registered=3, and modules ready, OK");
330 FullConnectUser(curr,GlobalGoners);
333 // It's time to PING this user. Send them a ping.
334 if ((TIME > curr->nping) && (curr->registered == 7))
336 // This user didn't answer the last ping, remove them
339 GlobalGoners->AddItem(curr,"Ping timeout");
342 Write(curr->fd,"PING :%s",Config->ServerName);
344 curr->nping = TIME+curr->pingmax;
346 // We can flush the write buffer as the last thing we do, because if they
347 // match any of the above conditions its no use flushing their buffer anyway.
348 curr->FlushWriteBuf();
349 if (curr->GetWriteError() != "")
351 GlobalGoners->AddItem(curr,curr->GetWriteError());
356 /** Remove all the queued users who are due to be quit
358 GlobalGoners->Apply();
359 /** Free to memory used
365 void OpenLog(char** argv, int argc)
367 if (Config->logpath == "")
369 Config->logpath = GetFullProgDir(argv,argc) + "/ircd.log";
371 Config->log_file = fopen(Config->logpath.c_str(),"a+");
372 if (!Config->log_file)
374 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
384 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
385 log(DEFAULT,"InspIRCd: startup: not starting with UID 0!");
393 if (*Config->DieValue)
395 printf("WARNING: %s\n\n",Config->DieValue);
396 log(DEFAULT,"Ut-Oh, somebody didn't read their config file: '%s'",Config->DieValue);
401 void LoadAllModules(InspIRCd* ServerInstance)
403 /* We must load the modules AFTER initializing the socket engine, now */
405 char configToken[MAXBUF];
406 for (int count = 0; count < Config->ConfValueEnum("module",&Config->config_f); count++)
408 Config->ConfValue("module","name",count,configToken,&Config->config_f);
409 printf("Loading module... \033[1;32m%s\033[0m\n",configToken);
410 if (!ServerInstance->LoadModule(configToken))
412 log(DEFAULT,"Exiting due to a module loader error.");
413 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());
417 log(DEFAULT,"Total loaded modules: %lu",(unsigned long)MODCOUNT+1);