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