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))
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);
104 result = cu->ReadData(data, 65535);
106 log(DEBUG,"Read result: %d",result);
107 if ((result) && (result != -EAGAIN))
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++)
115 if ((data[checker] == 0) || (data[checker] == 7))
120 userrec* current = cu;
121 int currfd = current->fd;
123 // add the data to the users buffer
126 if (!current->AddBuffer(data))
128 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
129 if (current->registered == 7)
131 // Make sure they arn't flooding long lines.
132 if (TIME > current->reset_due)
134 current->reset_due = TIME + current->threshold;
135 current->lines_in = 0;
138 if (current->lines_in > current->flood)
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");
145 WriteServ(currfd, "NOTICE %s :Your previous line was too long and was not delivered (Over 512chars) Please shorten it.", current->nick);
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);
158 if (current->recvq.length() > (unsigned)Config->NetBufferSize)
160 if (current->registered == 7)
162 kill_link(current,"RecvQ exceeded");
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);
173 // while there are complete lines to process...
174 while (current->BufferIsReady())
177 if (TIME > current->reset_due)
179 current->reset_due = TIME + current->threshold;
180 current->lines_in = 0;
183 if (current->lines_in > current->flood)
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");
190 if ((floodlines > current->flood) && (current->flood != 0))
192 if (current->registered == 7)
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");
200 add_zline(120,Config->ServerName,"Flood from unregistered connection",(char*)inet_ntoa(current->ip4));
201 apply_lines(APPLY_ZLINES);
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();
210 if (single_line.length()>512)
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");
217 strlcpy(sanitized,single_line.c_str(),MAXBUF);
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))
235 /* The user is still here, flush their buffer */
236 current->FlushWriteBuf();
242 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
244 log(DEBUG,"killing: %s",cu->nick);
245 kill_link(cu,strerror(errno));
249 // result EAGAIN means nothing read
250 else if ((result == EAGAIN) || (result == -EAGAIN))
253 else if (result == 0)
255 log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
256 kill_link(cu,"Client exited");
257 log(DEBUG,"Bailing from client exit");
262 void DoSocketTimeouts(time_t TIME)
264 unsigned int numsockets = module_sockets.size();
265 SocketEngine* SE = ServerInstance->SE;
266 for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
268 InspSocket* s = (InspSocket*)*a;
269 if (s->Timeout(TIME))
271 log(DEBUG,"Socket poll returned false, close and bail");
272 SE->DelFd(s->GetFd());
274 module_sockets.erase(a);
278 if (module_sockets.size() != numsockets) break;
283 * This function is called once a second from the mainloop.
284 * It is intended to do background checking on all the user structs, e.g.
285 * stuff like ping checks, registration timeouts, etc. This function is
286 * also responsible for checking if InspSocket derived classes are timed out.
288 void DoBackgroundUserStuff(time_t TIME)
290 CullList* GlobalGoners = new CullList();
291 for (std::vector<userrec*>::iterator count2 = local_users.begin(); count2 != local_users.end(); count2++)
293 /* Sanity checks for corrupted iterators (yes, really) */
294 userrec* curr = NULL;
296 curr = (userrec*)(*count2);
297 if ((long)curr == -1)
302 // registration timeout -- didnt send USER/NICK/HOST in the time specified in
303 // their connection class.
304 if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != 7))
306 log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
307 GlobalGoners->AddItem(curr,"Registration timeout");
310 // user has signed on with USER/NICK/PASS, and dns has completed, all the modules
311 // say this user is ok to proceed, fully connect them.
312 if ((TIME > curr->signon) && (curr->registered == 3) && (AllModulesReportReady(curr)))
314 curr->dns_done = true;
315 ServerInstance->stats->statsDnsBad++;
316 FullConnectUser(curr,GlobalGoners);
319 if ((curr->dns_done) && (curr->registered == 3) && (AllModulesReportReady(curr)))
321 log(DEBUG,"dns done, registered=3, and modules ready, OK");
322 FullConnectUser(curr,GlobalGoners);
325 // It's time to PING this user. Send them a ping.
326 if ((TIME > curr->nping) && (curr->registered == 7))
328 // This user didn't answer the last ping, remove them
331 GlobalGoners->AddItem(curr,"Ping timeout");
334 Write(curr->fd,"PING :%s",Config->ServerName);
336 curr->nping = TIME+curr->pingmax;
338 // We can flush the write buffer as the last thing we do, because if they
339 // match any of the above conditions its no use flushing their buffer anyway.
340 curr->FlushWriteBuf();
341 if (curr->GetWriteError() != "")
343 GlobalGoners->AddItem(curr,curr->GetWriteError());
348 /** Remove all the queued users who are due to be quit
350 GlobalGoners->Apply();
351 /** Free to memory used
357 void OpenLog(char** argv, int argc)
359 if (Config->logpath == "")
361 Config->logpath = GetFullProgDir(argv,argc) + "/ircd.log";
363 Config->log_file = fopen(Config->logpath.c_str(),"a+");
364 if (!Config->log_file)
366 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
376 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
377 log(DEFAULT,"InspIRCd: startup: not starting with UID 0!");
385 if (*Config->DieValue)
387 printf("WARNING: %s\n\n",Config->DieValue);
388 log(DEFAULT,"Ut-Oh, somebody didn't read their config file: '%s'",Config->DieValue);
393 void LoadAllModules(InspIRCd* ServerInstance)
395 /* We must load the modules AFTER initializing the socket engine, now */
397 char configToken[MAXBUF];
398 for (int count = 0; count < Config->ConfValueEnum("module",&Config->config_f); count++)
400 Config->ConfValue("module","name",count,configToken,&Config->config_f);
401 printf("Loading module... \033[1;32m%s\033[0m\n",configToken);
402 if (!ServerInstance->LoadModule(configToken))
404 log(DEFAULT,"Exiting due to a module loader error.");
405 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());
409 log(DEFAULT,"Total loaded modules: %lu",(unsigned long)MODCOUNT+1);