]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/inspircd_io.cpp
Moved typedefs etc into the header where they belong
[user/henk/code/inspircd.git] / src / inspircd_io.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include "inspircd_config.h"
20 #include <sys/time.h>
21 #include <sys/resource.h>
22 #include <sys/types.h>
23 #include <string>
24 #include <unistd.h>
25 #include <sstream>
26 #include <iostream>
27 #include <fstream>
28 #include "inspircd.h"
29 #include "inspircd_io.h"
30 #include "inspstring.h"
31 #include "helperfuncs.h"
32 #include "userprocess.h"
33 #include "xline.h"
34
35 extern ServerConfig *Config;
36 extern InspIRCd* ServerInstance;
37 extern int openSockfd[MAXSOCKS];
38 extern time_t TIME;
39
40 extern int MODCOUNT;
41 extern std::vector<Module*> modules;
42 extern std::vector<ircd_module*> factory;
43
44 ServerConfig::ServerConfig()
45 {
46         this->ClearStack();
47         *TempDir = *ServerName = *Network = *ServerDesc = *AdminName = '\0';
48         *HideWhoisServer = *AdminEmail = *AdminNick = *diepass = *restartpass = '\0';
49         *CustomVersion = *motd = *rules = *PrefixQuit = *DieValue = *DNSServer = '\0';
50         *OperOnlyStats = *ModPath = *MyExecutable = *DisabledCommands = *PID = '\0';
51         log_file = NULL;
52         nofork = HideBans = HideSplits = unlimitcore = false;
53         AllowHalfop = true;
54         dns_timeout = DieDelay = 5;
55         MaxTargets = 20;
56         NetBufferSize = 10240;
57         SoftLimit = MAXCLIENTS;
58         MaxConn = SOMAXCONN;
59         MaxWhoResults = 100;
60         debugging = 0;
61         LogLevel = DEFAULT;
62         maxbans.clear();
63 }
64
65 void ServerConfig::ClearStack()
66 {
67         include_stack.clear();
68 }
69
70 Module* ServerConfig::GetIOHook(int port)
71 {
72         std::map<int,Module*>::iterator x = IOHookModule.find(port);
73         return (x != IOHookModule.end() ? x->second : NULL);
74 }
75
76 bool ServerConfig::AddIOHook(int port, Module* iomod)
77 {
78         if (!GetIOHook(port))
79         {
80                 IOHookModule[port] = iomod;
81                 return true;
82         }
83         else
84         {
85                 ModuleException err("Port already hooked by another module");
86                 throw(err);
87                 return false;
88         }
89 }
90
91 bool ServerConfig::DelIOHook(int port)
92 {
93         std::map<int,Module*>::iterator x = IOHookModule.find(port);
94         if (x != IOHookModule.end())
95         {
96                 IOHookModule.erase(x);
97                 return true;
98         }
99         return false;
100 }
101
102 bool ServerConfig::CheckOnce(char* tag, bool bail, userrec* user)
103 {
104         int count = ConfValueEnum(tag,&Config->config_f);
105         if (count > 1)
106         {
107                 if (bail)
108                 {
109                         printf("There were errors in your configuration:\nYou have more than one <%s> tag, this is not permitted.\n",tag);
110                         Exit(0);
111                 }
112                 else
113                 {
114                         if (user)
115                         {
116                                 WriteServ(user->fd,"There were errors in your configuration:");
117                                 WriteServ(user->fd,"You have more than one <%s> tag, this is not permitted.\n",tag);
118                         }
119                         else
120                         {
121                                 WriteOpers("There were errors in the configuration file:");
122                                 WriteOpers("You have more than one <%s> tag, this is not permitted.\n",tag);
123                         }
124                 }
125                 return false;
126         }
127         if (count < 1)
128         {
129                 if (bail)
130                 {
131                         printf("There were errors in your configuration:\nYou have not defined a <%s> tag, this is required.\n",tag);
132                         Exit(0);
133                 }
134                 else
135                 {
136                         if (user)
137                         {
138                                 WriteServ(user->fd,"There were errors in your configuration:");
139                                 WriteServ(user->fd,"You have not defined a <%s> tag, this is required.",tag);
140                         }
141                         else
142                         {
143                                 WriteOpers("There were errors in the configuration file:");
144                                 WriteOpers("You have not defined a <%s> tag, this is required.",tag);
145                         }
146                 }
147                 return false;
148         }
149         return true;
150 }
151
152 bool NoValidation(const char* tag, const char* value, void* data)
153 {
154         log(DEBUG,"No validation for <%s:%s>",tag,value);
155         return true;
156 }
157
158 bool ValidateTempDir(const char* tag, const char* value, void* data)
159 {
160         char* x = (char*)data;
161         if (!*x)
162                strlcpy(x,"/tmp",1024);
163         return true;
164 }
165  
166 bool ValidateMaxTargets(const char* tag, const char* value, void* data)
167 {
168         int* x = (int*)data;
169         if ((*x < 0) || (*x > 31))
170         {
171                 log(DEFAULT,"WARNING: <options:maxtargets> value is greater than 31 or less than 0, set to 20.");
172                 *x = 20;
173         }
174         return true;
175 }
176
177 bool ValidateSoftLimit(const char* tag, const char* value, void* data)
178 {
179         int* x = (int*)data;    
180         if ((*x < 1) || (*x > MAXCLIENTS))
181         {
182                 log(DEFAULT,"WARNING: <options:softlimit> value is greater than %d or less than 0, set to %d.",MAXCLIENTS,MAXCLIENTS);
183                 *x = MAXCLIENTS;
184         }
185         return true;
186 }
187
188 bool ValidateMaxConn(const char* tag, const char* value, void* data)
189 {
190         int* x = (int*)data;    
191         if (*x > SOMAXCONN)
192                 log(DEFAULT,"WARNING: <options:somaxconn> value may be higher than the system-defined SOMAXCONN value!");
193         if (!*x)
194                 *x = SOMAXCONN;
195         return true;
196 }
197
198 bool ValidateDnsTimeout(const char* tag, const char* value, void* data)
199 {
200         int* x = (int*)data;
201         if (!*x)
202                 *x = 5;
203         return true;
204 }
205
206 bool ValidateDnsServer(const char* tag, const char* value, void* data)
207 {
208         char* x = (char*)data;
209         if (!*x)
210         {
211                 // attempt to look up their nameserver from /etc/resolv.conf
212                 log(DEFAULT,"WARNING: <dns:server> not defined, attempting to find working server in /etc/resolv.conf...");
213                 ifstream resolv("/etc/resolv.conf");
214                 std::string nameserver;
215                 bool found_server = false;
216          
217                 if (resolv.is_open())
218                 {
219                         while (resolv >> nameserver)
220                         {
221                                 if ((nameserver == "nameserver") && (!found_server))
222                                 {
223                                         resolv >> nameserver;
224                                         strlcpy(x,nameserver.c_str(),MAXBUF);
225                                         found_server = true;
226                                         log(DEFAULT,"<dns:server> set to '%s' as first resolver in /etc/resolv.conf.",nameserver.c_str());
227                                 }
228                         }
229                                 
230                         if (!found_server)
231                         {
232                                 log(DEFAULT,"/etc/resolv.conf contains no viable nameserver entries! Defaulting to nameserver '127.0.0.1'!");
233                                 strlcpy(x,"127.0.0.1",MAXBUF);
234                         }
235                 }
236                 else
237                 {
238                         log(DEFAULT,"/etc/resolv.conf can't be opened! Defaulting to nameserver '127.0.0.1'!");
239                         strlcpy(x,"127.0.0.1",MAXBUF);
240                 }
241         }
242         return true;
243 }
244
245 bool ValidateModPath(const char* tag, const char* value, void* data)
246 {
247         char* x = (char*)data;  
248         if (!*x)
249                 strlcpy(x,MOD_PATH,MAXBUF);
250         return true;
251 }
252
253
254 bool ValidateServerName(const char* tag, const char* value, void* data)
255 {
256         char* x = (char*)data;
257         if (!strchr(x,'.'))
258         {
259                 log(DEFAULT,"WARNING: <server:name> '%s' is not a fully-qualified domain name. Changed to '%s%c'",x,x,'.');
260                 charlcat(x,'.',MAXBUF);
261         }
262         strlower(x);
263         return true;
264 }
265
266 bool ValidateNetBufferSize(const char* tag, const char* value, void* data)
267 {
268         if ((!Config->NetBufferSize) || (Config->NetBufferSize > 65535) || (Config->NetBufferSize < 1024))
269         {
270                 log(DEFAULT,"No NetBufferSize specified or size out of range, setting to default of 10240.");
271                 Config->NetBufferSize = 10240;
272         }
273         return true;
274 }
275
276 bool ValidateMaxWho(const char* tag, const char* value, void* data)
277 {
278         if ((!Config->MaxWhoResults) || (Config->MaxWhoResults > 65535) || (Config->MaxWhoResults < 1))
279         {
280                 log(DEFAULT,"No MaxWhoResults specified or size out of range, setting to default of 128.");
281                 Config->MaxWhoResults = 128;
282         }
283         return true;
284 }
285
286 bool ValidateLogLevel(const char* tag, const char* value, void* data)
287 {
288         const char* dbg = (const char*)data;
289         Config->LogLevel = DEFAULT;                        
290         if (!strcmp(dbg,"debug"))
291         {
292                 Config->LogLevel = DEBUG;
293                 Config->debugging = 1;
294         }
295         else if (!strcmp(dbg,"verbose"))
296                 Config->LogLevel = VERBOSE;
297         else if (!strcmp(dbg,"default"))
298                 Config->LogLevel = DEFAULT;
299         else if (!strcmp(dbg,"sparse"))
300                 Config->LogLevel = SPARSE;
301         else if (!strcmp(dbg,"none"))
302                 Config->LogLevel = NONE;
303         return true;
304 }
305
306 bool ValidateMotd(const char* tag, const char* value, void* data)
307 {
308         readfile(Config->MOTD,Config->motd);
309         return true;
310 }
311
312 bool ValidateRules(const char* tag, const char* value, void* data)
313 {
314         readfile(Config->RULES,Config->rules);
315         return true;
316 }
317
318
319 void ServerConfig::Read(bool bail, userrec* user)
320 {
321         char debug[MAXBUF];
322         char CM1[MAXBUF],CM2[MAXBUF];
323         char ServName[MAXBUF],Value[MAXBUF];
324         char dataline[1024];
325         ConnectClass c;
326         std::stringstream errstr;
327
328         static InitialConfig Values[] = {
329                 {"options",     "softlimit",            &this->SoftLimit,               DT_INTEGER, ValidateSoftLimit},
330                 {"options",     "somaxconn",            &this->MaxConn,                 DT_INTEGER, ValidateMaxConn},
331                 {"server",      "name",                 &this->ServerName,              DT_CHARPTR, ValidateServerName},
332                 {"server",      "description",          &this->ServerDesc,              DT_CHARPTR, NoValidation},
333                 {"server",      "network",              &this->Network,                 DT_CHARPTR, NoValidation},
334                 {"admin",       "name",                 &this->AdminName,               DT_CHARPTR, NoValidation},
335                 {"admin",       "email",                &this->AdminEmail,              DT_CHARPTR, NoValidation},
336                 {"admin",       "nick",                 &this->AdminNick,               DT_CHARPTR, NoValidation},
337                 {"files",       "motd",                 &this->motd,                    DT_CHARPTR, ValidateMotd},
338                 {"files",       "rules",                &this->rules,                   DT_CHARPTR, ValidateRules},
339                 {"power",       "diepass",              &this->diepass,                 DT_CHARPTR, NoValidation},      
340                 {"power",       "pauseval",             &this->DieDelay,                DT_INTEGER, NoValidation},
341                 {"power",       "restartpass",          &this->restartpass,             DT_CHARPTR, NoValidation},
342                 {"options",     "prefixquit",           &this->PrefixQuit,              DT_CHARPTR, NoValidation},
343                 {"die",         "value",                &this->DieValue,                DT_CHARPTR, NoValidation},
344                 {"options",     "loglevel",             &debug,                         DT_CHARPTR, ValidateLogLevel},
345                 {"options",     "netbuffersize",        &this->NetBufferSize,           DT_INTEGER, ValidateNetBufferSize},
346                 {"options",     "maxwho",               &this->MaxWhoResults,           DT_INTEGER, ValidateMaxWho},
347                 {"options",     "allowhalfop",          &this->AllowHalfop,             DT_BOOLEAN, NoValidation},
348                 {"dns",         "server",               &this->DNSServer,               DT_CHARPTR, ValidateDnsServer},
349                 {"dns",         "timeout",              &this->dns_timeout,             DT_INTEGER, ValidateDnsTimeout},
350                 {"options",     "moduledir",            &this->ModPath,                 DT_CHARPTR, ValidateModPath},
351                 {"disabled",    "commands",             &this->DisabledCommands,        DT_CHARPTR, NoValidation},
352                 {"options",     "operonlystats",        &this->OperOnlyStats,           DT_CHARPTR, NoValidation},
353                 {"options",     "customversion",        &this->CustomVersion,           DT_CHARPTR, NoValidation},
354                 {"options",     "hidesplits",           &this->HideSplits,              DT_BOOLEAN, NoValidation},
355                 {"options",     "hidebans",             &this->HideBans,                DT_BOOLEAN, NoValidation},
356                 {"options",     "hidewhois",            &this->HideWhoisServer,         DT_CHARPTR, NoValidation},
357                 {"options",     "tempdir",              &this->TempDir,                 DT_CHARPTR, ValidateTempDir},
358                 {NULL}
359         };
360         
361         include_stack.clear();
362
363         if (!LoadConf(CONFIG_FILE,&Config->config_f,&errstr))
364         {
365                 errstr.seekg(0);
366                 log(DEFAULT,"There were errors in your configuration:\n%s",errstr.str().c_str());
367
368                 if (bail)
369                 {
370                         printf("There were errors in your configuration:\n%s",errstr.str().c_str());
371                         Exit(0);
372                 }
373                 else
374                 {
375                         if (user)
376                         {
377                                 WriteServ(user->fd,"NOTICE %s :There were errors in the configuration file:",user->nick);
378                                 while (!errstr.eof())
379                                 {
380                                         errstr.getline(dataline,1024);
381                                         WriteServ(user->fd,"NOTICE %s :%s",user->nick,dataline);
382                                 }
383                         }
384                         else
385                         {
386                                 WriteOpers("There were errors in the configuration file:");
387                                 while (!errstr.eof())
388                                 {
389                                         errstr.getline(dataline,1024);
390                                         WriteOpers(dataline);
391                                 }
392                         }
393
394                         return;
395                 }
396         }
397
398         /* Check we dont have more than one of singular tags
399          */
400         if (!CheckOnce("server",bail,user) || !CheckOnce("admin",bail,user) || !CheckOnce("files",bail,user)
401                 || !CheckOnce("power",bail,user) || !CheckOnce("options",bail,user) || !CheckOnce("pid",bail,user))
402         {
403                 return;
404         }
405
406         char* convert;
407         for (int Index = 0; Values[Index].tag; Index++)
408         {
409                 int* val_i = (int*) Values[Index].val;
410                 char* val_c = (char*) Values[Index].val;
411
412                 switch (Values[Index].datatype)
413                 {
414                         case DT_CHARPTR:
415                                 ConfValue(Values[Index].tag, Values[Index].value, 0, val_c, &this->config_f);
416                         break;
417
418                         case DT_INTEGER:
419                                 convert = new char[MAXBUF];
420                                 ConfValue(Values[Index].tag, Values[Index].value, 0, convert, &this->config_f);
421                                 *val_i = atoi(convert);
422                                 delete[] convert;
423                         break;
424
425                         case DT_BOOLEAN:
426                                 convert = new char[MAXBUF];
427                                 ConfValue(Values[Index].tag, Values[Index].value, 0, convert, &this->config_f);
428                                 *val_i = ((*convert == tolower('y')) || (*convert == tolower('t')) || (*convert == '1'));
429                                 delete[] convert;
430                         break;
431
432                         case DT_NOTHING:
433                         break;
434                 }
435
436                 Values[Index].validation_function(Values[Index].tag, Values[Index].value, Values[Index].val);
437         }
438
439         log(DEFAULT,"Reading connect classes...");
440         Classes.clear();
441
442         for (int i = 0; i < ConfValueEnum("connect",&Config->config_f); i++)
443         {
444                 *Value = 0;
445                 ConfValue("connect","allow",i,Value,&Config->config_f);
446                 if (*Value)
447                 {
448                         c.host = Value;
449                         c.type = CC_ALLOW;
450                         *Value = 0;
451                         ConfValue("connect","password",i,Value,&Config->config_f);
452                         c.pass = Value;
453                         c.registration_timeout = ConfValueInteger("connect","timeout",i,&Config->config_f); // default is 2 minutes
454                         c.pingtime = ConfValueInteger("connect","pingfreq",i,&Config->config_f);
455                         c.flood = ConfValueInteger("connect","flood",i,&Config->config_f);
456                         c.threshold = ConfValueInteger("connect","threshold",i,&Config->config_f);
457                         c.sendqmax = ConfValueInteger("connect","sendq",i,&Config->config_f);
458                         c.recvqmax = ConfValueInteger("connect","recvq",i,&Config->config_f);
459                         c.maxlocal = ConfValueInteger("connect","localmax",i,&Config->config_f);
460                         c.maxglobal = ConfValueInteger("connect","globalmax",i,&Config->config_f);
461
462                         
463                         if (c.maxlocal == 0)
464                         {
465                                 c.maxlocal = 3;
466                         }
467
468                         if (c.maxglobal == 0)
469                         {
470                                 c.maxglobal = 3;
471                         }
472
473                         if (c.threshold == 0)
474                         {
475                                 c.threshold = 1;
476                                 c.flood = 999;
477                                 log(DEFAULT,"Warning: Connect allow line '%s' has no flood/threshold settings. Setting this tag to 999 lines in 1 second.",c.host.c_str());
478                         }
479
480                         if (c.sendqmax == 0)
481                         {
482                                 c.sendqmax = 262114;
483                         }
484                         if (c.recvqmax == 0)
485                         {
486                                 c.recvqmax = 4096;
487                         }
488                         if (c.registration_timeout == 0)
489                         {
490                                 c.registration_timeout = 90;
491                         }
492                         if (c.pingtime == 0)
493                         {
494                                 c.pingtime = 120;
495                         }
496
497                         Classes.push_back(c);
498                 }
499                 else
500                 {
501                         ConfValue("connect","deny",i,Value,&Config->config_f);
502                         c.host = Value;
503                         c.type = CC_DENY;
504                         Classes.push_back(c);
505                         log(DEBUG,"Read connect class type DENY, host=%s",c.host.c_str());
506                 }
507         }
508
509         Config->ulines.clear();
510
511         for (int i = 0; i < ConfValueEnum("uline",&Config->config_f); i++)
512         {   
513                 ConfValue("uline","server",i,ServName,&Config->config_f);
514                 {
515                         log(DEBUG,"Read ULINE '%s'",ServName);
516                         Config->ulines.push_back(ServName);
517                 }
518         }
519
520         maxbans.clear();
521
522         for (int count = 0; count < Config->ConfValueEnum("banlist",&Config->config_f); count++)
523         {
524                 Config->ConfValue("banlist","chan",count,CM1,&Config->config_f);
525                 Config->ConfValue("banlist","limit",count,CM2,&Config->config_f);
526                 maxbans[CM1] = atoi(CM2);
527         }
528
529         ReadClassesAndTypes();
530         log(DEFAULT,"Reading K lines,Q lines and Z lines from config...");
531         read_xline_defaults();
532         log(DEFAULT,"Applying K lines, Q lines and Z lines...");
533         apply_lines(APPLY_ALL);
534
535         ConfValue("pid","file",0,Config->PID,&Config->config_f);
536         // write once here, to try it out and make sure its ok
537         WritePID(Config->PID);
538
539         log(DEFAULT,"Done reading configuration file, InspIRCd is now starting.");
540         if (!bail)
541         {
542                 log(DEFAULT,"Adding and removing modules due to rehash...");
543
544                 std::vector<std::string> old_module_names, new_module_names, added_modules, removed_modules;
545
546                 // store the old module names
547                 for (std::vector<std::string>::iterator t = module_names.begin(); t != module_names.end(); t++)
548                 {
549                         old_module_names.push_back(*t);
550                 }
551
552                 // get the new module names
553                 for (int count2 = 0; count2 < ConfValueEnum("module",&Config->config_f); count2++)
554                 {
555                         ConfValue("module","name",count2,Value,&Config->config_f);
556                         new_module_names.push_back(Value);
557                 }
558
559                 // now create a list of new modules that are due to be loaded
560                 // and a seperate list of modules which are due to be unloaded
561                 for (std::vector<std::string>::iterator _new = new_module_names.begin(); _new != new_module_names.end(); _new++)
562                 {
563                         bool added = true;
564
565                         for (std::vector<std::string>::iterator old = old_module_names.begin(); old != old_module_names.end(); old++)
566                         {
567                                 if (*old == *_new)
568                                         added = false;
569                         }
570
571                         if (added)
572                                 added_modules.push_back(*_new);
573                 }
574
575                 for (std::vector<std::string>::iterator oldm = old_module_names.begin(); oldm != old_module_names.end(); oldm++)
576                 {
577                         bool removed = true;
578                         for (std::vector<std::string>::iterator newm = new_module_names.begin(); newm != new_module_names.end(); newm++)
579                         {
580                                 if (*newm == *oldm)
581                                         removed = false;
582                         }
583
584                         if (removed)
585                                 removed_modules.push_back(*oldm);
586                 }
587
588                 /*
589                  * now we have added_modules, a vector of modules to be loaded,
590                  * and removed_modules, a vector of modules
591                  * to be removed.
592                  */
593                 int rem = 0, add = 0;
594                 if (!removed_modules.empty())
595                         for (std::vector<std::string>::iterator removing = removed_modules.begin(); removing != removed_modules.end(); removing++)
596                         {
597                                 if (ServerInstance->UnloadModule(removing->c_str()))
598                                 {
599                                         WriteOpers("*** REHASH UNLOADED MODULE: %s",removing->c_str());
600
601                                         if (user)
602                                                 WriteServ(user->fd,"973 %s %s :Module %s successfully unloaded.",user->nick, removing->c_str(), removing->c_str());
603
604                                         rem++;
605                                 }
606                                 else
607                                 {
608                                         if (user)
609                                                 WriteServ(user->fd,"972 %s %s :Failed to unload module %s: %s",user->nick, removing->c_str(), removing->c_str(), ServerInstance->ModuleError());
610                                 }
611                         }
612
613                 if (!added_modules.empty())
614                 for (std::vector<std::string>::iterator adding = added_modules.begin(); adding != added_modules.end(); adding++)
615                 {
616                         if (ServerInstance->LoadModule(adding->c_str()))
617                         {
618                                 WriteOpers("*** REHASH LOADED MODULE: %s",adding->c_str());
619
620                                 if (user)
621                                         WriteServ(user->fd,"975 %s %s :Module %s successfully loaded.",user->nick, adding->c_str(), adding->c_str());
622
623                                 add++;
624                         }
625                         else
626                         {
627                                 if (user)
628                                         WriteServ(user->fd,"974 %s %s :Failed to load module %s: %s",user->nick, adding->c_str(), adding->c_str(), ServerInstance->ModuleError());
629                         }
630                 }
631
632                 log(DEFAULT,"Successfully unloaded %lu of %lu modules and loaded %lu of %lu modules.",(unsigned long)rem,(unsigned long)removed_modules.size(),(unsigned long)add,(unsigned long)added_modules.size());
633         }
634 }
635
636
637 void Exit (int status)
638 {
639         if (Config->log_file)
640                 fclose(Config->log_file);
641         send_error("Server shutdown.");
642         exit (status);
643 }
644
645 void Killed(int status)
646 {
647         if (Config->log_file)
648                 fclose(Config->log_file);
649         send_error("Server terminated.");
650         exit(status);
651 }
652
653 char* CleanFilename(char* name)
654 {
655         char* p = name + strlen(name);
656         while ((p != name) && (*p != '/')) p--;
657         return (p != name ? ++p : p);
658 }
659
660
661 void Rehash(int status)
662 {
663         WriteOpers("Rehashing config file %s due to SIGHUP",CleanFilename(CONFIG_FILE));
664         fclose(Config->log_file);
665         OpenLog(NULL,0);
666         Config->Read(false,NULL);
667         FOREACH_MOD(I_OnRehash,OnRehash(""));
668 }
669
670
671
672 void Start (void)
673 {
674         printf("\033[1;32mInspire Internet Relay Chat Server, compiled %s at %s\n",__DATE__,__TIME__);
675         printf("(C) ChatSpike Development team.\033[0m\n\n");
676         printf("Developers:\033[1;32m Brain, FrostyCoolSlug, w00t, Om\033[0m\n");
677         printf("Others:\033[1;32m See /INFO Output\033[0m\n");
678         printf("Name concept:\033[1;32m   Lord_Zathras\033[0m\n\n");
679 }
680
681 void WritePID(std::string filename)
682 {
683         ofstream outfile(filename.c_str());
684         if (outfile.is_open())
685         {
686                 outfile << getpid();
687                 outfile.close();
688         }
689         else
690         {
691                 printf("Failed to write PID-file '%s', exiting.\n",filename.c_str());
692                 log(DEFAULT,"Failed to write PID-file '%s', exiting.",filename.c_str());
693                 Exit(0);
694         }
695 }
696
697 void SetSignals()
698 {
699         signal (SIGALRM, SIG_IGN);
700         signal (SIGHUP, Rehash);
701         signal (SIGPIPE, SIG_IGN);
702         signal (SIGTERM, Exit);
703         signal (SIGSEGV, Error);
704 }
705
706
707 int DaemonSeed (void)
708 {
709         int childpid;
710         if ((childpid = fork ()) < 0)
711                 return (ERROR);
712         else if (childpid > 0)
713         {
714                 /* We wait a few seconds here, so that the shell prompt doesnt come back over the output */
715                 sleep(6);
716                 exit (0);
717         }
718         setsid ();
719         umask (007);
720         printf("InspIRCd Process ID: \033[1;32m%lu\033[0m\n",(unsigned long)getpid());
721
722         if (Config->unlimitcore)
723         {
724                 rlimit rl;
725                 if (getrlimit(RLIMIT_CORE, &rl) == -1)
726                 {
727                         log(DEFAULT,"Failed to getrlimit()!");
728                         return(FALSE);
729                 }
730                 else
731                 {
732                         rl.rlim_cur = rl.rlim_max;
733                         if (setrlimit(RLIMIT_CORE, &rl) == -1)
734                                 log(DEFAULT,"setrlimit() failed, cannot increase coredump size.");
735                 }
736         }
737   
738         return (TRUE);
739 }
740
741
742 /* Make Sure Modules Are Avaliable!
743  * (BugFix By Craig.. See? I do work! :p)
744  * Modified by brain, requires const char*
745  * to work with other API functions
746  */
747
748 bool FileExists (const char* file)
749 {
750         FILE *input;
751         if ((input = fopen (file, "r")) == NULL)
752         {
753                 return(false);
754         }
755         else
756         {
757                 fclose (input);
758                 return(true);
759         }
760 }
761
762 /* ConfProcess does the following things to a config line in the following order:
763  *
764  * Processes the line for syntax errors as shown below
765  *  (1) Line void of quotes or equals (a malformed, illegal tag format)
766  *  (2) Odd number of quotes on the line indicating a missing quote
767  *  (3) number of equals signs not equal to number of quotes / 2 (missing an equals sign)
768  *  (4) Spaces between the opening bracket (<) and the keyword
769  *  (5) Spaces between a keyword and an equals sign
770  *  (6) Spaces between an equals sign and a quote
771  * Removes trailing spaces
772  * Removes leading spaces
773  * Converts tabs to spaces
774  * Turns multiple spaces that are outside of quotes into single spaces
775  */
776
777 std::string ServerConfig::ConfProcess(char* buffer, long linenumber, std::stringstream* errorstream, bool &error, std::string filename)
778 {
779         long number_of_quotes = 0;
780         long number_of_equals = 0;
781         bool has_open_bracket = false;
782         bool in_quotes = false;
783         error = false;
784         if (!buffer)
785         {
786                 return "";
787         }
788         // firstly clean up the line by stripping spaces from the start and end and converting tabs to spaces
789         for (char* d = buffer; *d; d++)
790                 if (*d == 9)
791                         *d = ' ';
792         while (*buffer == ' ') buffer++;
793         while ((buffer[strlen(buffer)-1] == ' ') && (*buffer)) buffer[strlen(buffer)-1] = '\0';
794
795         // empty lines are syntactically valid, as are comments
796         if (!(*buffer) || buffer[0] == '#')
797                 return "";
798
799         for (unsigned int c = 0; c < strlen(buffer); c++)
800         {
801                 // convert all spaces that are OUTSIDE quotes into hardspace (0xA0) as this will make them easier to
802                 // search and replace later :)
803                 if ((!in_quotes) && (buffer[c] == ' '))
804                         buffer[c] = '\xA0';
805                 if ((buffer[c] == '<') && (!in_quotes))
806                 {
807                         has_open_bracket = true;
808                         if (strlen(buffer) == 1)
809                         {
810                                 *errorstream << "Tag without identifier at " << filename << ":" << linenumber << endl;
811                                 error = true;
812                                 return "";
813                         }
814                         else if ((tolower(buffer[c+1]) < 'a') || (tolower(buffer[c+1]) > 'z'))
815                         {
816                                 *errorstream << "Invalid characters in identifier at " << filename << ":" << linenumber << endl;
817                                 error = true;
818                                 return "";
819                         }
820                 }
821                 if (buffer[c] == '"')
822                 {
823                         number_of_quotes++;
824                         in_quotes = (!in_quotes);
825                 }
826                 if ((buffer[c] == '=') && (!in_quotes))
827                 {
828                         number_of_equals++;
829                         if (strlen(buffer) == c)
830                         {
831                                 *errorstream << "Variable without a value at " << filename << ":" << linenumber << endl;
832                                 error = true;
833                                 return "";
834                         }
835                         else if (buffer[c+1] != '"')
836                         {
837                                 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
838                                 error = true;
839                                 return "";
840                         }
841                         else if (!c)
842                         {
843                                 *errorstream << "Value without a variable (line starts with '=') at " << filename << ":" << linenumber << endl;
844                                 error = true;
845                                 return "";
846                         }
847                         else if (buffer[c-1] == '\xA0')
848                         {
849                                 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
850                                 error = true;
851                                 return "";
852                         }
853                 }
854         }
855         // no quotes, and no equals. something freaky.
856         if ((!number_of_quotes) || (!number_of_equals) && (strlen(buffer)>2) && (buffer[0]=='<'))
857         {
858                 *errorstream << "Malformed tag at " << filename << ":" << linenumber << endl;
859                 error = true;
860                 return "";
861         }
862         // odd number of quotes. thats just wrong.
863         if ((number_of_quotes % 2) != 0)
864         {
865                 *errorstream << "Missing \" at " << filename << ":" << linenumber << endl;
866                 error = true;
867                 return "";
868         }
869         if (number_of_equals < (number_of_quotes/2))
870         {
871                 *errorstream << "Missing '=' at " << filename << ":" << linenumber << endl;
872         }
873         if (number_of_equals > (number_of_quotes/2))
874         {
875                 *errorstream << "Too many '=' at " << filename << ":" << linenumber << endl;
876         }
877
878         std::string parsedata = buffer;
879         // turn multispace into single space
880         while (parsedata.find("\xA0\xA0") != std::string::npos)
881         {
882                 parsedata.erase(parsedata.find("\xA0\xA0"),1);
883         }
884
885         // turn our hardspace back into softspace
886         for (unsigned int d = 0; d < parsedata.length(); d++)
887         {
888                 if (parsedata[d] == '\xA0')
889                         parsedata[d] = ' ';
890         }
891
892         // and we're done, the line is fine!
893         return parsedata;
894 }
895
896 int ServerConfig::fgets_safe(char* buffer, size_t maxsize, FILE* &file)
897 {
898         char c_read = '\0';
899         unsigned int bufptr = 0;
900         while ((!feof(file)) && (c_read != '\n') && (c_read != '\r') && (bufptr < maxsize))
901         {
902                 c_read = fgetc(file);
903                 if ((c_read != '\n') && (c_read != '\r'))
904                         buffer[bufptr++] = c_read;
905         }
906         buffer[bufptr] = '\0';
907         return bufptr;
908 }
909
910 bool ServerConfig::LoadConf(const char* filename, std::stringstream *target, std::stringstream* errorstream)
911 {
912         target->str("");
913         errorstream->str("");
914         long linenumber = 1;
915         // first, check that the file exists before we try to do anything with it
916         if (!FileExists(filename))
917         {
918                 *errorstream << "File " << filename << " not found." << endl;
919                 return false;
920         }
921         // Fix the chmod of the file to restrict it to the current user and group
922         chmod(filename,0600);
923         for (unsigned int t = 0; t < include_stack.size(); t++)
924         {
925                 if (std::string(filename) == include_stack[t])
926                 {
927                         *errorstream << "File " << filename << " is included recursively (looped inclusion)." << endl;
928                         return false;
929                 }
930         }
931         include_stack.push_back(filename);
932         // now open it
933         FILE* conf = fopen(filename,"r");
934         char buffer[MAXBUF];
935         if (conf)
936         {
937                 while (!feof(conf))
938                 {
939                         if (fgets_safe(buffer, MAXBUF, conf))
940                         {
941                                 if ((!feof(conf)) && (buffer) && (strlen(buffer)))
942                                 {
943                                         if ((buffer[0] != '#') && (buffer[0] != '\r')  && (buffer[0] != '\n'))
944                                         {
945                                                 if (!strncmp(buffer,"<include file=\"",15))
946                                                 {
947                                                         char* buf = buffer;
948                                                         char confpath[10240],newconf[10240];
949                                                         // include file directive
950                                                         buf += 15;      // advance to filename
951                                                         for (unsigned int j = 0; j < strlen(buf); j++)
952                                                         {
953                                                                 if (buf[j] == '\\')
954                                                                         buf[j] = '/';
955                                                                 if (buf[j] == '"')
956                                                                 {
957                                                                         buf[j] = '\0';
958                                                                         break;
959                                                                 }
960                                                         }
961                                                         log(DEBUG,"Opening included file '%s'",buf);
962                                                         if (*buf != '/')
963                                                         {
964                                                                 strlcpy(confpath,CONFIG_FILE,10240);
965                                                                 if (strstr(confpath,"/inspircd.conf"))
966                                                                 {
967                                                                         // leaves us with just the path
968                                                                         *(strstr(confpath,"/inspircd.conf")) = '\0';
969                                                                 }
970                                                                 snprintf(newconf,10240,"%s/%s",confpath,buf);
971                                                         }
972                                                         else strlcpy(newconf,buf,10240);
973                                                         std::stringstream merge(stringstream::in | stringstream::out);
974                                                         // recursively call LoadConf and get the new data, use the same errorstream
975                                                         if (LoadConf(newconf, &merge, errorstream))
976                                                         {
977                                                                 // append to the end of the file
978                                                                 std::string newstuff = merge.str();
979                                                                 *target << newstuff;
980                                                         }
981                                                         else
982                                                         {
983                                                                 // the error propogates up to its parent recursively
984                                                                 // causing the config reader to bail at the top level.
985                                                                 fclose(conf);
986                                                                 return false;
987                                                         }
988                                                 }
989                                                 else
990                                                 {
991                                                         bool error = false;
992                                                         std::string data = this->ConfProcess(buffer,linenumber++,errorstream,error,filename);
993                                                         if (error)
994                                                         {
995                                                                 return false;
996                                                         }
997                                                         *target << data;
998                                                 }
999                                         }
1000                                         else linenumber++;
1001                                 }
1002                         }
1003                 }
1004                 fclose(conf);
1005         }
1006         target->seekg(0);
1007         return true;
1008 }
1009
1010 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
1011
1012 int ServerConfig::EnumConf(std::stringstream *config, const char* tag)
1013 {
1014         int ptr = 0;
1015         char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
1016         int in_token, in_quotes, tptr, idx = 0;
1017
1018         std::string x = config->str();
1019         const char* buf = x.c_str();
1020         long bptr = 0;
1021         long len = config->str().length();
1022         
1023         ptr = 0;
1024         in_token = 0;
1025         in_quotes = 0;
1026         lastc = '\0';
1027         while (bptr<len)
1028         {
1029                 lastc = c;
1030                 c = buf[bptr++];
1031                 if ((c == '#') && (lastc == '\n'))
1032                 {
1033                         while ((c != '\n') && (bptr<len))
1034                         {
1035                                 lastc = c;
1036                                 c = buf[bptr++];
1037                         }
1038                 }
1039                 if ((c == '<') && (!in_quotes))
1040                 {
1041                         tptr = 0;
1042                         in_token = 1;
1043                         do {
1044                                 c = buf[bptr++];
1045                                 if (c != ' ')
1046                                 {
1047                                         c_tag[tptr++] = c;
1048                                         c_tag[tptr] = '\0';
1049                                 }
1050                         } while (c != ' ');
1051                 }
1052                 if (c == '"')
1053                 {
1054                         in_quotes = (!in_quotes);
1055                 }
1056                 if ((c == '>') && (!in_quotes))
1057                 {
1058                         in_token = 0;
1059                         if (!strcmp(c_tag,tag))
1060                         {
1061                                 /* correct tag, but wrong index */
1062                                 idx++;
1063                         }
1064                         c_tag[0] = '\0';
1065                         buffer[0] = '\0';
1066                         ptr = 0;
1067                         tptr = 0;
1068                 }
1069                 if (c != '>')
1070                 {
1071                         if ((in_token) && (c != '\n') && (c != '\r'))
1072                         {
1073                                 buffer[ptr++] = c;
1074                                 buffer[ptr] = '\0';
1075                         }
1076                 }
1077         }
1078         return idx;
1079 }
1080
1081 /* Counts the number of values within a certain tag */
1082
1083 int ServerConfig::EnumValues(std::stringstream *config, const char* tag, int index)
1084 {
1085         int ptr = 0;
1086         char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
1087         int in_token, in_quotes, tptr, idx = 0;
1088         
1089         bool correct_tag = false;
1090         int num_items = 0;
1091
1092         const char* buf = config->str().c_str();
1093         long bptr = 0;
1094         long len = strlen(buf);
1095         
1096         ptr = 0;
1097         in_token = 0;
1098         in_quotes = 0;
1099         lastc = '\0';
1100         while (bptr<len)
1101         {
1102                 lastc = c;
1103                 c = buf[bptr++];
1104                 if ((c == '#') && (lastc == '\n'))
1105                 {
1106                         while ((c != '\n') && (bptr<len))
1107                         {
1108                                 lastc = c;
1109                                 c = buf[bptr++];
1110                         }
1111                 }
1112                 if ((c == '<') && (!in_quotes))
1113                 {
1114                         tptr = 0;
1115                         in_token = 1;
1116                         do {
1117                                 c = buf[bptr++];
1118                                 if (c != ' ')
1119                                 {
1120                                         c_tag[tptr++] = c;
1121                                         c_tag[tptr] = '\0';
1122                                         
1123                                         if ((!strcmp(c_tag,tag)) && (idx == index))
1124                                         {
1125                                                 correct_tag = true;
1126                                         }
1127                                 }
1128                         } while (c != ' ');
1129                 }
1130                 if (c == '"')
1131                 {
1132                         in_quotes = (!in_quotes);
1133                 }
1134                 
1135                 if ( (correct_tag) && (!in_quotes) && ( (c == ' ') || (c == '\n') || (c == '\r') ) )
1136                 {
1137                         num_items++;
1138                 }
1139                 if ((c == '>') && (!in_quotes))
1140                 {
1141                         in_token = 0;
1142                         if (correct_tag)
1143                                 correct_tag = false;
1144                         if (!strcmp(c_tag,tag))
1145                         {
1146                                 /* correct tag, but wrong index */
1147                                 idx++;
1148                         }
1149                         c_tag[0] = '\0';
1150                         buffer[0] = '\0';
1151                         ptr = 0;
1152                         tptr = 0;
1153                 }
1154                 if (c != '>')
1155                 {
1156                         if ((in_token) && (c != '\n') && (c != '\r'))
1157                         {
1158                                 buffer[ptr++] = c;
1159                                 buffer[ptr] = '\0';
1160                         }
1161                 }
1162         }
1163         return num_items+1;
1164 }
1165
1166
1167
1168 int ServerConfig::ConfValueEnum(char* tag, std::stringstream* config)
1169 {
1170         return EnumConf(config,tag);
1171 }
1172
1173
1174
1175 /* Retrieves a value from the config file. If there is more than one value of the specified
1176  * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
1177  *
1178  * ConfValue("oper","name",2,result);
1179  */
1180
1181 int ServerConfig::ReadConf(std::stringstream *config, const char* tag, const char* var, int index, char *result)
1182 {
1183         int ptr = 0;
1184         char buffer[65535], c_tag[MAXBUF], c, lastc;
1185         int in_token, in_quotes, tptr, idx = 0;
1186         char* key;
1187
1188         std::string x = config->str();
1189         const char* buf = x.c_str();
1190         long bptr = 0;
1191         long len = config->str().length();
1192         
1193         ptr = 0;
1194         in_token = 0;
1195         in_quotes = 0;
1196         lastc = '\0';
1197         c_tag[0] = '\0';
1198         buffer[0] = '\0';
1199         while (bptr<len)
1200         {
1201                 lastc = c;
1202                 c = buf[bptr++];
1203                 // FIX: Treat tabs as spaces
1204                 if (c == 9)
1205                         c = 32;
1206                 if ((c == '<') && (!in_quotes))
1207                 {
1208                         tptr = 0;
1209                         in_token = 1;
1210                         do {
1211                                 c = buf[bptr++];
1212                                 if (c != ' ')
1213                                 {
1214                                         c_tag[tptr++] = c;
1215                                         c_tag[tptr] = '\0';
1216                                 }
1217                         // FIX: Tab can follow a tagname as well as space.
1218                         } while ((c != ' ') && (c != 9));
1219                 }
1220                 if (c == '"')
1221                 {
1222                         in_quotes = (!in_quotes);
1223                 }
1224                 if ((c == '>') && (!in_quotes))
1225                 {
1226                         in_token = 0;
1227                         if (idx == index)
1228                         {
1229                                 if (!strcmp(c_tag,tag))
1230                                 {
1231                                         if ((buffer) && (c_tag) && (var))
1232                                         {
1233                                                 key = strstr(buffer,var);
1234                                                 if (!key)
1235                                                 {
1236                                                         /* value not found in tag */
1237                                                         *result = 0;
1238                                                         return 0;
1239                                                 }
1240                                                 else
1241                                                 {
1242                                                         key+=strlen(var);
1243                                                         while (*key !='"')
1244                                                         {
1245                                                                 if (!*key)
1246                                                                 {
1247                                                                         /* missing quote */
1248                                                                         *result = 0;
1249                                                                         return 0;
1250                                                                 }
1251                                                                 key++;
1252                                                         }
1253                                                         key++;
1254                                                         for (unsigned j = 0; j < strlen(key); j++)
1255                                                         {
1256                                                                 if (key[j] == '"')
1257                                                                 {
1258                                                                         key[j] = '\0';
1259                                                                 }
1260                                                         }
1261                                                         strlcpy(result,key,MAXBUF);
1262                                                         return 1;
1263                                                 }
1264                                         }
1265                                 }
1266                         }
1267                         if (!strcmp(c_tag,tag))
1268                         {
1269                                 /* correct tag, but wrong index */
1270                                 idx++;
1271                         }
1272                         c_tag[0] = '\0';
1273                         buffer[0] = '\0';
1274                         ptr = 0;
1275                         tptr = 0;
1276                 }
1277                 if (c != '>')
1278                 {
1279                         if ((in_token) && (c != '\n') && (c != '\r'))
1280                         {
1281                                 buffer[ptr++] = c;
1282                                 buffer[ptr] = '\0';
1283                         }
1284                 }
1285         }
1286         *result = 0; // value or its tag not found at all
1287         return 0;
1288 }
1289
1290
1291
1292 int ServerConfig::ConfValue(char* tag, char* var, int index, char *result,std::stringstream *config)
1293 {
1294         ReadConf(config, tag, var, index, result);
1295         return 0;
1296 }
1297
1298 int ServerConfig::ConfValueInteger(char* tag, char* var, int index, std::stringstream *config)
1299 {
1300         char result[MAXBUF];
1301         ReadConf(config, tag, var, index, result);
1302         return atoi(result);
1303 }
1304
1305 // This will bind a socket to a port. It works for UDP/TCP
1306 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
1307 {
1308         memset((char *)&server,0,sizeof(server));
1309         struct in_addr addy;
1310         inet_aton(addr,&addy);
1311         server.sin_family = AF_INET;
1312         if (!*addr)
1313         {
1314                 server.sin_addr.s_addr = htonl(INADDR_ANY);
1315         }
1316         else
1317         {
1318                 server.sin_addr = addy;
1319         }
1320         server.sin_port = htons(port);
1321         if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
1322         {
1323                 return(ERROR);
1324         }
1325         else
1326         {
1327                 listen(sockfd, Config->MaxConn);
1328                 return(TRUE);
1329         }
1330 }
1331
1332
1333 // Open a TCP Socket
1334 int OpenTCPSocket (void)
1335 {
1336         int sockfd;
1337         int on = 1;
1338         struct linger linger = { 0 };
1339   
1340         if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
1341                 return (ERROR);
1342         else
1343         {
1344                 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
1345                 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
1346                 linger.l_onoff = 1;
1347                 linger.l_linger = 1;
1348                 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));
1349                 return (sockfd);
1350         }
1351 }
1352
1353 int BindPorts()
1354 {
1355         char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
1356         sockaddr_in client,server;
1357         int clientportcount = 0;
1358         int BoundPortCount = 0;
1359
1360         for (int count = 0; count < Config->ConfValueEnum("bind",&Config->config_f); count++)
1361         {
1362                 Config->ConfValue("bind","port",count,configToken,&Config->config_f);
1363                 Config->ConfValue("bind","address",count,Addr,&Config->config_f);
1364                 Config->ConfValue("bind","type",count,Type,&Config->config_f);
1365
1366                 if ((!*Type) || (!strcmp(Type,"clients")))
1367                 {
1368                         // modules handle server bind types now
1369                         Config->ports[clientportcount] = atoi(configToken);
1370
1371                         // If the client put bind "*", this is an unrealism.
1372                         // We don't actually support this as documented, but
1373                         // i got fed up of people trying it, so now it converts
1374                         // it to an empty string meaning the same 'bind to all'.
1375                         if (*Addr == '*')
1376                                 *Addr = 0;
1377
1378                         strlcpy(Config->addrs[clientportcount],Addr,256);
1379                         clientportcount++;
1380                         log(DEBUG,"InspIRCd: startup: read binding %s:%s [%s] from config",Addr,configToken, Type);
1381                 }
1382         }
1383
1384         int PortCount = clientportcount;
1385
1386         for (int count = 0; count < PortCount; count++)
1387         {
1388                 if ((openSockfd[BoundPortCount] = OpenTCPSocket()) == ERROR)
1389                 {
1390                         log(DEBUG,"InspIRCd: startup: bad fd %lu binding port [%s:%d]",(unsigned long)openSockfd[BoundPortCount],Config->addrs[count],(unsigned long)Config->ports[count]);
1391                         return(ERROR);
1392                 }
1393
1394                 if (BindSocket(openSockfd[BoundPortCount],client,server,Config->ports[count],Config->addrs[count]) == ERROR)
1395                 {
1396                         log(DEFAULT,"InspIRCd: startup: failed to bind port [%s:%lu]: %s",Config->addrs[count],(unsigned long)Config->ports[count],strerror(errno));
1397                 }
1398                 else
1399                 {
1400                         /* well we at least bound to one socket so we'll continue */
1401                         BoundPortCount++;
1402                 }
1403         }
1404
1405         /* if we didn't bind to anything then abort */
1406         if (!BoundPortCount)
1407         {
1408                 log(DEFAULT,"InspIRCd: startup: no ports bound, bailing!");
1409                 printf("\nERROR: Was not able to bind any of %lu ports! Please check your configuration.\n\n", (unsigned long)PortCount);
1410                 return (ERROR);
1411         }
1412
1413         return BoundPortCount;
1414 }
1415