1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2005 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"
24 #include "inspircd_util.h"
27 #include <sys/errno.h>
28 #include <sys/ioctl.h>
29 #include <sys/utsname.h>
33 #include <ext/hash_map>
55 #include "inspstring.h"
57 #include "helperfuncs.h"
59 #include "socketengine.h"
62 extern int openSockfd[MAXSOCKS];
64 extern bool unlimitcore;
65 extern struct sockaddr_in client,server;
66 extern char addrs[MAXBUF][255];
67 extern socklen_t length;
68 extern char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
70 extern char DisabledCommands[MAXBUF];
72 extern bool AllowHalfop;
73 extern bool AllowProtect;
74 extern bool AllowFounder;
76 extern std::vector<Module*> modules;
77 extern std::vector<std::string> module_names;
78 extern std::vector<ircd_module*> factory;
80 extern std::vector<InspSocket*> module_sockets;
82 extern SocketEngine* SE;
85 extern time_t OLDTIME;
88 extern time_t startup_time;
89 extern int NetBufferSize;
91 extern unsigned int SoftLimit;
92 extern int MaxWhoResults;
93 extern time_t nb_start;
94 extern int dns_timeout;
96 extern int statsAccept, statsRefused, statsUnknown, statsCollisions, statsDns, statsDnsGood, statsDnsBad, statsConnects, statsSent, statsRecv;
98 extern userrec* fd_ref_table[65536];
101 extern char ServerName[MAXBUF];
102 extern char Network[MAXBUF];
103 extern char ServerDesc[MAXBUF];
104 extern char AdminName[MAXBUF];
105 extern char AdminEmail[MAXBUF];
106 extern char AdminNick[MAXBUF];
107 extern char diepass[MAXBUF];
108 extern char restartpass[MAXBUF];
109 extern char motd[MAXBUF];
110 extern char rules[MAXBUF];
111 extern char list[MAXBUF];
112 extern char PrefixQuit[MAXBUF];
113 extern char DieValue[MAXBUF];
114 extern char DNSServer[MAXBUF];
115 extern char data[65536];
118 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, irc::StrHashComp> user_hash;
119 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, irc::StrHashComp> chan_hash;
120 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, irc::InAddr_HashComp> address_cache;
121 typedef nspace::hash_map<std::string, WhoWasUser*, nspace::hash<string>, irc::StrHashComp> whowas_hash;
123 extern user_hash clientlist;
124 extern chan_hash chanlist;
125 extern whowas_hash whowas;
127 extern FILE *log_file;
128 extern std::stringstream config_f;
130 void ProcessUser(userrec* cu)
133 log(DEBUG,"Processing user with fd %d",cu->fd);
136 FOREACH_RESULT(OnRawSocketRead(cu->fd,data,65535,result2));
139 result = cu->ReadData(data, 65535);
143 log(DEBUG,"Data result returned by module: %d",MOD_RESULT);
146 log(DEBUG,"Read result: %d",result);
150 // perform a check on the raw buffer as an array (not a string!) to remove
151 // characters 0 and 7 which are illegal in the RFC - replace them with spaces.
152 // hopefully this should stop even more people whining about "Unknown command: *"
153 for (int checker = 0; checker < result; checker++)
155 if ((data[checker] == 0) || (data[checker] == 7))
160 userrec* current = cu;
161 int currfd = current->fd;
163 // add the data to the users buffer
166 if (!current->AddBuffer(data))
168 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
169 if (current->registered == 7)
171 kill_link(current,"RecvQ exceeded");
175 WriteOpers("*** Excess flood from %s",current->ip);
176 log(DEFAULT,"Excess flood from: %s",current->ip);
177 add_zline(120,ServerName,"Flood from unregistered connection",current->ip);
182 if (current->recvq.length() > (unsigned)NetBufferSize)
184 if (current->registered == 7)
186 kill_link(current,"RecvQ exceeded");
190 WriteOpers("*** Excess flood from %s",current->ip);
191 log(DEFAULT,"Excess flood from: %s",current->ip);
192 add_zline(120,ServerName,"Flood from unregistered connection",current->ip);
197 // while there are complete lines to process...
198 while (current->BufferIsReady())
201 if (TIME > current->reset_due)
203 current->reset_due = TIME + current->threshold;
204 current->lines_in = 0;
207 if (current->lines_in > current->flood)
209 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
210 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
211 kill_link(current,"Excess flood");
214 if ((floodlines > current->flood) && (current->flood != 0))
216 if (current->registered == 7)
218 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
219 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
220 kill_link(current,"Excess flood");
224 add_zline(120,ServerName,"Flood from unregistered connection",current->ip);
229 char sanitized[MAXBUF];
230 // use GetBuffer to copy single lines into the sanitized string
231 std::string single_line = current->GetBuffer();
232 current->bytes_in += single_line.length();
234 if (single_line.length()>512)
236 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
237 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
238 kill_link(current,"Excess flood");
241 strlcpy(sanitized,single_line.c_str(),MAXBUF);
244 userrec* old_comp = fd_ref_table[currfd];
245 // we're gonna re-scan to check if the nick is gone, after every
246 // command - if it has, we're gonna bail
247 process_buffer(sanitized,current);
248 // look for the user's record in case it's changed... if theyve quit,
249 // we cant do anything more with their buffer, so bail.
250 // there used to be an ugly, slow loop here. Now we have a reference
251 // table, life is much easier (and FASTER)
252 userrec* new_comp = fd_ref_table[currfd];
253 if ((currfd < 0) || (!fd_ref_table[currfd]) || (old_comp != new_comp))
259 /* The user is still here, flush their buffer */
260 current->FlushWriteBuf();
266 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
268 log(DEBUG,"killing: %s",cu->nick);
269 kill_link(cu,strerror(errno));
273 // result EAGAIN means nothing read
274 else if (result == EAGAIN)
277 else if (result == 0)
279 log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
280 kill_link(cu,"Client exited");
281 log(DEBUG,"Bailing from client exit");
288 * This function is called once a second from the mainloop.
289 * It is intended to do background checking on all the user structs, e.g.
290 * stuff like ping checks, registration timeouts, etc.
291 * The function returns false when it is finished, and true if
292 * it needs to be run again (e.g. it has processed one of a batch of
293 * QUIT messages, but couldnt continue iterating because the iterator
294 * became invalid). This function is also responsible for checking
295 * if InspSocket derived classes are timed out.
297 bool DoBackgroundUserStuff(time_t TIME)
299 unsigned int numsockets = module_sockets.size();
300 for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
302 InspSocket* s = (InspSocket*)*a;
303 if (s->Timeout(TIME))
305 log(DEBUG,"Socket poll returned false, close and bail");
306 SE->DelFd(s->GetFd());
308 module_sockets.erase(a);
312 if (module_sockets.size() != numsockets) break;
314 /* TODO: We need a seperate hash containing only local users for this
316 for (user_hash::iterator count2 = clientlist.begin(); count2 != clientlist.end(); count2++)
318 /* Sanity checks for corrupted iterators (yes, really) */
319 userrec* curr = NULL;
321 curr = count2->second;
322 if ((long)curr == -1)
325 if ((curr) && (curr->fd != 0))
327 int currfd = curr->fd;
328 // we don't check the state of remote users.
329 if ((currfd != -1) && (currfd != FD_MAGIC_NUMBER))
331 curr->FlushWriteBuf();
332 if (curr->GetWriteError() != "")
334 log(DEBUG,"InspIRCd: write error: %s",curr->GetWriteError().c_str());
335 kill_link(curr,curr->GetWriteError().c_str());
338 // registration timeout -- didnt send USER/NICK/HOST in the time specified in
339 // their connection class.
340 if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != 7))
342 log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
343 kill_link(curr,"Registration timeout");
346 if ((TIME > curr->signon) && (curr->registered == 3) && (AllModulesReportReady(curr)))
348 log(DEBUG,"signon exceed, registered=3, and modules ready, OK: %d %d",TIME,curr->signon);
349 curr->dns_done = true;
351 FullConnectUser(curr);
352 if (fd_ref_table[currfd] != curr) // something changed, bail pronto
355 if ((curr->dns_done) && (curr->registered == 3) && (AllModulesReportReady(curr)))
357 log(DEBUG,"dns done, registered=3, and modules ready, OK");
358 FullConnectUser(curr);
359 if (fd_ref_table[currfd] != curr) // something changed, bail pronto
362 if ((TIME > curr->nping) && (isnick(curr->nick)) && (curr->registered == 7))
364 if ((!curr->lastping) && (curr->registered == 7))
366 log(DEBUG,"InspIRCd: ping timeout: %s",curr->nick);
367 kill_link(curr,"Ping timeout");
370 Write(curr->fd,"PING :%s",ServerName);
371 log(DEBUG,"InspIRCd: pinging: %s",curr->nick);
373 curr->nping = TIME+curr->pingmax; // was hard coded to 120
381 void OpenLog(char** argv, int argc)
383 std::string logpath = GetFullProgDir(argv,argc) + "/ircd.log";
384 log_file = fopen(logpath.c_str(),"a+");
387 printf("ERROR: Could not write to logfile %s, bailing!\n\n",logpath.c_str());
391 printf("Logging to ircd.log...\n");
393 printf("Logging to %s...\n",logpath.c_str());
402 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
403 log(DEFAULT,"InspIRCd: startup: not starting with UID 0!");
413 printf("WARNING: %s\n\n",DieValue);
414 log(DEFAULT,"Ut-Oh, somebody didn't read their config file: '%s'",DieValue);
419 void LoadAllModules()
421 /* We must load the modules AFTER initializing the socket engine, now */
423 for (int count = 0; count < ConfValueEnum("module",&config_f); count++)
425 ConfValue("module","name",count,configToken,&config_f);
426 printf("Loading module... \033[1;32m%s\033[0m\n",configToken);
427 if (!LoadModule(configToken))
429 log(DEFAULT,"Exiting due to a module loader error.");
430 printf("\nThere was an error loading a module: %s\n\nYou might want to do './inspircd start' instead of 'bin/inspircd'\n\n",ModuleError());
434 log(DEFAULT,"Total loaded modules: %lu",(unsigned long)MODCOUNT+1);