]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/configreader.cpp
b0f2d68722bc5ccfecd466f20d55b86c84b15b3c
[user/henk/code/inspircd.git] / src / configreader.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 #include "configreader.h"
18 #include <sstream>
19 #include <fstream>
20 #include "inspircd.h"
21 #include "xline.h"
22
23 std::vector<std::string> old_module_names, new_module_names, added_modules, removed_modules;
24
25 ServerConfig::ServerConfig(InspIRCd* Instance) : ServerInstance(Instance)
26 {
27         this->ClearStack();
28         *TempDir = *ServerName = *Network = *ServerDesc = *AdminName = '\0';
29         *HideWhoisServer = *AdminEmail = *AdminNick = *diepass = *restartpass = '\0';
30         *CustomVersion = *motd = *rules = *PrefixQuit = *DieValue = *DNSServer = '\0';
31         *UserStats = *ModPath = *MyExecutable = *DisabledCommands = *PID = '\0';
32         log_file = NULL;
33         NoUserDns = forcedebug = OperSpyWhois = nofork = HideBans = HideSplits = false;
34         CycleHosts = writelog = AllowHalfop = true;
35         dns_timeout = DieDelay = 5;
36         MaxTargets = 20;
37         NetBufferSize = 10240;
38         SoftLimit = MAXCLIENTS;
39         MaxConn = SOMAXCONN;
40         MaxWhoResults = 100;
41         debugging = 0;
42         LogLevel = DEFAULT;
43         maxbans.clear();
44 }
45
46 void ServerConfig::ClearStack()
47 {
48         include_stack.clear();
49 }
50
51 Module* ServerConfig::GetIOHook(int port)
52 {
53         std::map<int,Module*>::iterator x = IOHookModule.find(port);
54         return (x != IOHookModule.end() ? x->second : NULL);
55 }
56
57 bool ServerConfig::AddIOHook(int port, Module* iomod)
58 {
59         if (!GetIOHook(port))
60         {
61                 IOHookModule[port] = iomod;
62                 return true;
63         }
64         else
65         {
66                 ModuleException err("Port already hooked by another module");
67                 throw(err);
68                 return false;
69         }
70 }
71
72 bool ServerConfig::DelIOHook(int port)
73 {
74         std::map<int,Module*>::iterator x = IOHookModule.find(port);
75         if (x != IOHookModule.end())
76         {
77                 IOHookModule.erase(x);
78                 return true;
79         }
80         return false;
81 }
82
83 bool ServerConfig::CheckOnce(char* tag, bool bail, userrec* user)
84 {
85         int count = ConfValueEnum(this->config_data, tag);
86         
87         if (count > 1)
88         {
89                 if (bail)
90                 {
91                         printf("There were errors in your configuration:\nYou have more than one <%s> tag, this is not permitted.\n",tag);
92                         InspIRCd::Exit(ERROR);
93                 }
94                 else
95                 {
96                         if (user)
97                         {
98                                 user->WriteServ("There were errors in your configuration:");
99                                 user->WriteServ("You have more than one <%s> tag, this is not permitted.\n",tag);
100                         }
101                         else
102                         {
103                                 ServerInstance->WriteOpers("There were errors in the configuration file:");
104                                 ServerInstance->WriteOpers("You have more than one <%s> tag, this is not permitted.\n",tag);
105                         }
106                 }
107                 return false;
108         }
109         if (count < 1)
110         {
111                 if (bail)
112                 {
113                         printf("There were errors in your configuration:\nYou have not defined a <%s> tag, this is required.\n",tag);
114                         InspIRCd::Exit(ERROR);
115                 }
116                 else
117                 {
118                         if (user)
119                         {
120                                 user->WriteServ("There were errors in your configuration:");
121                                 user->WriteServ("You have not defined a <%s> tag, this is required.",tag);
122                         }
123                         else
124                         {
125                                 ServerInstance->WriteOpers("There were errors in the configuration file:");
126                                 ServerInstance->WriteOpers("You have not defined a <%s> tag, this is required.",tag);
127                         }
128                 }
129                 return false;
130         }
131         return true;
132 }
133
134 bool NoValidation(ServerConfig* conf, const char* tag, const char* value, void* data)
135 {
136         conf->GetInstance()->Log(DEBUG,"No validation for <%s:%s>",tag,value);
137         return true;
138 }
139
140 bool ValidateTempDir(ServerConfig* conf, const char* tag, const char* value, void* data)
141 {
142         char* x = (char*)data;
143         if (!*x)
144                 strlcpy(x,"/tmp",1024);
145         return true;
146 }
147  
148 bool ValidateMaxTargets(ServerConfig* conf, const char* tag, const char* value, void* data)
149 {
150         int* x = (int*)data;
151         if ((*x < 0) || (*x > 31))
152         {
153                 conf->GetInstance()->Log(DEFAULT,"WARNING: <options:maxtargets> value is greater than 31 or less than 0, set to 20.");
154                 *x = 20;
155         }
156         return true;
157 }
158
159 bool ValidateSoftLimit(ServerConfig* conf, const char* tag, const char* value, void* data)
160 {
161         int* x = (int*)data;    
162         if ((*x < 1) || (*x > MAXCLIENTS))
163         {
164                 conf->GetInstance()->Log(DEFAULT,"WARNING: <options:softlimit> value is greater than %d or less than 0, set to %d.",MAXCLIENTS,MAXCLIENTS);
165                 *x = MAXCLIENTS;
166         }
167         return true;
168 }
169
170 bool ValidateMaxConn(ServerConfig* conf, const char* tag, const char* value, void* data)
171 {
172         int* x = (int*)data;    
173         if (*x > SOMAXCONN)
174                 conf->GetInstance()->Log(DEFAULT,"WARNING: <options:somaxconn> value may be higher than the system-defined SOMAXCONN value!");
175         if (!*x)
176                 *x = SOMAXCONN;
177         return true;
178 }
179
180 bool ValidateDnsTimeout(ServerConfig* conf, const char* tag, const char* value, void* data)
181 {
182         int* x = (int*)data;
183         if (!*x)
184                 *x = 5;
185         return true;
186 }
187
188 bool InitializeDisabledCommands(const char* data, InspIRCd* ServerInstance)
189 {
190         std::stringstream dcmds(data);
191         std::string thiscmd;
192
193         /* Enable everything first */
194         for (nspace::hash_map<std::string,command_t*>::iterator x = ServerInstance->Parser->cmdlist.begin(); x != ServerInstance->Parser->cmdlist.end(); x++)
195                 x->second->Disable(false);
196
197         /* Now disable all the ones which the user wants disabled */
198         while (dcmds >> thiscmd)
199         {
200                 nspace::hash_map<std::string,command_t*>::iterator cm = ServerInstance->Parser->cmdlist.find(thiscmd);
201                 if (cm != ServerInstance->Parser->cmdlist.end())
202                 {
203                         ServerInstance->Log(DEBUG,"Disabling command '%s'",cm->second->command.c_str());
204                         cm->second->Disable(true);
205                 }
206         }
207         return true;
208 }
209
210 bool ValidateDnsServer(ServerConfig* conf, const char* tag, const char* value, void* data)
211 {
212         char* x = (char*)data;
213         if (!*x)
214         {
215                 // attempt to look up their nameserver from /etc/resolv.conf
216                 conf->GetInstance()->Log(DEFAULT,"WARNING: <dns:server> not defined, attempting to find working server in /etc/resolv.conf...");
217                 ifstream resolv("/etc/resolv.conf");
218                 std::string nameserver;
219                 bool found_server = false;
220
221                 if (resolv.is_open())
222                 {
223                         while (resolv >> nameserver)
224                         {
225                                 if ((nameserver == "nameserver") && (!found_server))
226                                 {
227                                         resolv >> nameserver;
228                                         strlcpy(x,nameserver.c_str(),MAXBUF);
229                                         found_server = true;
230                                         conf->GetInstance()->Log(DEFAULT,"<dns:server> set to '%s' as first resolver in /etc/resolv.conf.",nameserver.c_str());
231                                 }
232                         }
233
234                         if (!found_server)
235                         {
236                                 conf->GetInstance()->Log(DEFAULT,"/etc/resolv.conf contains no viable nameserver entries! Defaulting to nameserver '127.0.0.1'!");
237                                 strlcpy(x,"127.0.0.1",MAXBUF);
238                         }
239                 }
240                 else
241                 {
242                         conf->GetInstance()->Log(DEFAULT,"/etc/resolv.conf can't be opened! Defaulting to nameserver '127.0.0.1'!");
243                         strlcpy(x,"127.0.0.1",MAXBUF);
244                 }
245         }
246         return true;
247 }
248
249 bool ValidateModPath(ServerConfig* conf, const char* tag, const char* value, void* data)
250 {
251         char* x = (char*)data;  
252         if (!*x)
253                 strlcpy(x,MOD_PATH,MAXBUF);
254         return true;
255 }
256
257
258 bool ValidateServerName(ServerConfig* conf, const char* tag, const char* value, void* data)
259 {
260         char* x = (char*)data;
261         if (!strchr(x,'.'))
262         {
263                 conf->GetInstance()->Log(DEFAULT,"WARNING: <server:name> '%s' is not a fully-qualified domain name. Changed to '%s%c'",x,x,'.');
264                 charlcat(x,'.',MAXBUF);
265         }
266         //strlower(x);
267         return true;
268 }
269
270 bool ValidateNetBufferSize(ServerConfig* conf, const char* tag, const char* value, void* data)
271 {
272         if ((!conf->NetBufferSize) || (conf->NetBufferSize > 65535) || (conf->NetBufferSize < 1024))
273         {
274                 conf->GetInstance()->Log(DEFAULT,"No NetBufferSize specified or size out of range, setting to default of 10240.");
275                 conf->NetBufferSize = 10240;
276         }
277         return true;
278 }
279
280 bool ValidateMaxWho(ServerConfig* conf, const char* tag, const char* value, void* data)
281 {
282         if ((!conf->MaxWhoResults) || (conf->MaxWhoResults > 65535) || (conf->MaxWhoResults < 1))
283         {
284                 conf->GetInstance()->Log(DEFAULT,"No MaxWhoResults specified or size out of range, setting to default of 128.");
285                 conf->MaxWhoResults = 128;
286         }
287         return true;
288 }
289
290 bool ValidateLogLevel(ServerConfig* conf, const char* tag, const char* value, void* data)
291 {
292         const char* dbg = (const char*)data;
293         conf->LogLevel = DEFAULT;
294         if (!strcmp(dbg,"debug"))
295         {
296                 conf->LogLevel = DEBUG;
297                 conf->debugging = 1;
298         }
299         else if (!strcmp(dbg,"verbose"))
300                 conf->LogLevel = VERBOSE;
301         else if (!strcmp(dbg,"default"))
302                 conf->LogLevel = DEFAULT;
303         else if (!strcmp(dbg,"sparse"))
304                 conf->LogLevel = SPARSE;
305         else if (!strcmp(dbg,"none"))
306                 conf->LogLevel = NONE;
307         return true;
308 }
309
310 bool ValidateMotd(ServerConfig* conf, const char* tag, const char* value, void* data)
311 {
312         conf->ReadFile(conf->MOTD,conf->motd);
313         return true;
314 }
315
316 bool ValidateRules(ServerConfig* conf, const char* tag, const char* value, void* data)
317 {
318         conf->ReadFile(conf->RULES,conf->rules);
319         return true;
320 }
321
322 /* Callback called before processing the first <connect> tag
323  */
324 bool InitConnect(ServerConfig* conf, const char* tag)
325 {
326         conf->GetInstance()->Log(DEFAULT,"Reading connect classes...");
327         conf->Classes.clear();
328         return true;
329 }
330
331 /* Callback called to process a single <connect> tag
332  */
333 bool DoConnect(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
334 {
335         ConnectClass c;
336         char* allow = (char*)values[0]; /* Yeah, there are a lot of values. Live with it. */
337         char* deny = (char*)values[1];
338         char* password = (char*)values[2];
339         int* timeout = (int*)values[3];
340         int* pingfreq = (int*)values[4];
341         int* flood = (int*)values[5];
342         int* threshold = (int*)values[6];
343         int* sendq = (int*)values[7];
344         int* recvq = (int*)values[8];
345         int* localmax = (int*)values[9];
346         int* globalmax = (int*)values[10];
347
348         if (*allow)
349         {
350                 c.host = allow;
351                 c.type = CC_ALLOW;
352                 c.pass = password;
353                 c.registration_timeout = *timeout;
354                 c.pingtime = *pingfreq;
355                 c.flood = *flood;
356                 c.threshold = *threshold;
357                 c.sendqmax = *sendq;
358                 c.recvqmax = *recvq;
359                 c.maxlocal = *localmax;
360                 c.maxglobal = *globalmax;
361
362
363                 if (c.maxlocal == 0)
364                         c.maxlocal = 3;
365                 if (c.maxglobal == 0)
366                         c.maxglobal = 3;
367                 if (c.threshold == 0)
368                 {
369                         c.threshold = 1;
370                         c.flood = 999;
371                         conf->GetInstance()->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());
372                 }
373                 if (c.sendqmax == 0)
374                         c.sendqmax = 262114;
375                 if (c.recvqmax == 0)
376                         c.recvqmax = 4096;
377                 if (c.registration_timeout == 0)
378                         c.registration_timeout = 90;
379                 if (c.pingtime == 0)
380                         c.pingtime = 120;
381                 conf->Classes.push_back(c);
382         }
383         else
384         {
385                 c.host = deny;
386                 c.type = CC_DENY;
387                 conf->Classes.push_back(c);
388                 conf->GetInstance()->Log(DEBUG,"Read connect class type DENY, host=%s",deny);
389         }
390
391         return true;
392 }
393
394 /* Callback called when there are no more <connect> tags
395  */
396 bool DoneConnect(ServerConfig* conf, const char* tag)
397 {
398         conf->GetInstance()->Log(DEBUG,"DoneConnect called for tag: %s",tag);
399         return true;
400 }
401
402 /* Callback called before processing the first <uline> tag
403  */
404 bool InitULine(ServerConfig* conf, const char* tag)
405 {
406         conf->ulines.clear();
407         return true;
408 }
409
410 /* Callback called to process a single <uline> tag
411  */
412 bool DoULine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
413 {
414         char* server = (char*)values[0];
415         conf->GetInstance()->Log(DEBUG,"Read ULINE '%s'",server);
416         conf->ulines.push_back(server);
417         return true;
418 }
419
420 /* Callback called when there are no more <uline> tags
421  */
422 bool DoneULine(ServerConfig* conf, const char* tag)
423 {
424         return true;
425 }
426
427 /* Callback called before processing the first <module> tag
428  */
429 bool InitModule(ServerConfig* conf, const char* tag)
430 {
431         old_module_names.clear();
432         new_module_names.clear();
433         added_modules.clear();
434         removed_modules.clear();
435         for (std::vector<std::string>::iterator t = conf->module_names.begin(); t != conf->module_names.end(); t++)
436         {
437                 old_module_names.push_back(*t);
438         }
439         return true;
440 }
441
442 /* Callback called to process a single <module> tag
443  */
444 bool DoModule(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
445 {
446         char* modname = (char*)values[0];
447         new_module_names.push_back(modname);
448         return true;
449 }
450
451 /* Callback called when there are no more <module> tags
452  */
453 bool DoneModule(ServerConfig* conf, const char* tag)
454 {
455         // now create a list of new modules that are due to be loaded
456         // and a seperate list of modules which are due to be unloaded
457         for (std::vector<std::string>::iterator _new = new_module_names.begin(); _new != new_module_names.end(); _new++)
458         {
459                 bool added = true;
460
461                 for (std::vector<std::string>::iterator old = old_module_names.begin(); old != old_module_names.end(); old++)
462                 {
463                         if (*old == *_new)
464                                 added = false;
465                 }
466
467                 if (added)
468                         added_modules.push_back(*_new);
469         }
470
471         for (std::vector<std::string>::iterator oldm = old_module_names.begin(); oldm != old_module_names.end(); oldm++)
472         {
473                 bool removed = true;
474                 for (std::vector<std::string>::iterator newm = new_module_names.begin(); newm != new_module_names.end(); newm++)
475                 {
476                         if (*newm == *oldm)
477                                 removed = false;
478                 }
479
480                 if (removed)
481                         removed_modules.push_back(*oldm);
482         }
483         return true;
484 }
485
486 /* Callback called before processing the first <banlist> tag
487  */
488 bool InitMaxBans(ServerConfig* conf, const char* tag)
489 {
490         conf->maxbans.clear();
491         return true;
492 }
493
494 /* Callback called to process a single <banlist> tag
495  */
496 bool DoMaxBans(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
497 {
498         char* channel = (char*)values[0];
499         int* limit = (int*)values[1];
500         conf->maxbans[channel] = *limit;
501         return true;
502 }
503
504 /* Callback called when there are no more <banlist> tags.
505  */
506 bool DoneMaxBans(ServerConfig* conf, const char* tag)
507 {
508         return true;
509 }
510
511 void ServerConfig::Read(bool bail, userrec* user)
512 {
513         char debug[MAXBUF];             /* Temporary buffer for debugging value */
514         char* data[12];                 /* Temporary buffers for reading multiple occurance tags into */
515         void* ptr[12];                  /* Temporary pointers for passing to callbacks */
516         int r_i[12];                    /* Temporary array for casting */
517         int rem = 0, add = 0;           /* Number of modules added, number of modules removed */
518         std::ostringstream errstr;      /* String stream containing the error output */
519
520         /* These tags MUST occur and must ONLY occur once in the config file */
521         static char* Once[] = { "server", "admin", "files", "power", "options", NULL };
522
523         /* These tags can occur ONCE or not at all */
524         static InitialConfig Values[] = {
525                 {"options",             "softlimit",                    &this->SoftLimit,               DT_INTEGER, ValidateSoftLimit},
526                 {"options",             "somaxconn",                    &this->MaxConn,                 DT_INTEGER, ValidateMaxConn},
527                 {"server",              "name",                         &this->ServerName,              DT_CHARPTR, ValidateServerName},
528                 {"server",              "description",                  &this->ServerDesc,              DT_CHARPTR, NoValidation},
529                 {"server",              "network",                      &this->Network,                 DT_CHARPTR, NoValidation},
530                 {"admin",               "name",                         &this->AdminName,               DT_CHARPTR, NoValidation},
531                 {"admin",               "email",                        &this->AdminEmail,              DT_CHARPTR, NoValidation},
532                 {"admin",               "nick",                         &this->AdminNick,               DT_CHARPTR, NoValidation},
533                 {"files",               "motd",                         &this->motd,                    DT_CHARPTR, ValidateMotd},
534                 {"files",               "rules",                        &this->rules,                   DT_CHARPTR, ValidateRules},
535                 {"power",               "diepass",                      &this->diepass,                 DT_CHARPTR, NoValidation},      
536                 {"power",               "pause",                        &this->DieDelay,                DT_INTEGER, NoValidation},
537                 {"power",               "restartpass",                  &this->restartpass,             DT_CHARPTR, NoValidation},
538                 {"options",             "prefixquit",                   &this->PrefixQuit,              DT_CHARPTR, NoValidation},
539                 {"die",                 "value",                        &this->DieValue,                DT_CHARPTR, NoValidation},
540                 {"options",             "loglevel",                     &debug,                         DT_CHARPTR, ValidateLogLevel},
541                 {"options",             "netbuffersize",                &this->NetBufferSize,           DT_INTEGER, ValidateNetBufferSize},
542                 {"options",             "maxwho",                       &this->MaxWhoResults,           DT_INTEGER, ValidateMaxWho},
543                 {"options",             "allowhalfop",                  &this->AllowHalfop,             DT_BOOLEAN, NoValidation},
544                 {"dns",                 "server",                       &this->DNSServer,               DT_CHARPTR, ValidateDnsServer},
545                 {"dns",                 "timeout",                      &this->dns_timeout,             DT_INTEGER, ValidateDnsTimeout},
546                 {"options",             "moduledir",                    &this->ModPath,                 DT_CHARPTR, ValidateModPath},
547                 {"disabled",            "commands",                     &this->DisabledCommands,        DT_CHARPTR, NoValidation},
548                 {"options",             "userstats",            &this->UserStats,               DT_CHARPTR, NoValidation},
549                 {"options",             "customversion",                &this->CustomVersion,           DT_CHARPTR, NoValidation},
550                 {"options",             "hidesplits",                   &this->HideSplits,              DT_BOOLEAN, NoValidation},
551                 {"options",             "hidebans",                     &this->HideBans,                DT_BOOLEAN, NoValidation},
552                 {"options",             "hidewhois",                    &this->HideWhoisServer,         DT_CHARPTR, NoValidation},
553                 {"options",             "operspywhois",                 &this->OperSpyWhois,            DT_BOOLEAN, NoValidation},
554                 {"options",             "tempdir",                      &this->TempDir,                 DT_CHARPTR, ValidateTempDir},
555                 {"options",             "nouserdns",                    &this->NoUserDns,               DT_BOOLEAN, NoValidation},
556                 {"options",             "syntaxhints",                  &this->SyntaxHints,             DT_BOOLEAN, NoValidation},
557                 {"options",             "cyclehosts",                   &this->CycleHosts,              DT_BOOLEAN, NoValidation},
558                 {"pid",                 "file",                         &this->PID,                     DT_CHARPTR, NoValidation},
559                 {NULL}
560         };
561
562         /* These tags can occur multiple times, and therefore they have special code to read them
563          * which is different to the code for reading the singular tags listed above.
564          */
565         static MultiConfig MultiValues[] = {
566
567                 {"connect",
568                                 {"allow",       "deny",         "password",     "timeout",      "pingfreq",     "flood",
569                                 "threshold",    "sendq",        "recvq",        "localmax",     "globalmax",    NULL},
570                                 {DT_CHARPTR,    DT_CHARPTR,     DT_CHARPTR,     DT_INTEGER,     DT_INTEGER,     DT_INTEGER,
571                                  DT_INTEGER,    DT_INTEGER,     DT_INTEGER,     DT_INTEGER,     DT_INTEGER},
572                                 InitConnect, DoConnect, DoneConnect},
573
574                 {"uline",
575                                 {"server",      NULL},
576                                 {DT_CHARPTR},
577                                 InitULine,DoULine,DoneULine},
578
579                 {"banlist",
580                                 {"chan",        "limit",        NULL},
581                                 {DT_CHARPTR,    DT_INTEGER},
582                                 InitMaxBans, DoMaxBans, DoneMaxBans},
583
584                 {"module",
585                                 {"name",        NULL},
586                                 {DT_CHARPTR},
587                                 InitModule, DoModule, DoneModule},
588
589                 {"badip",
590                                 {"reason",      "ipmask",       NULL},
591                                 {DT_CHARPTR,    DT_CHARPTR},
592                                 InitXLine, DoZLine, DoneXLine},
593
594                 {"badnick",
595                                 {"reason",      "nick",         NULL},
596                                 {DT_CHARPTR,    DT_CHARPTR},
597                                 InitXLine, DoQLine, DoneXLine},
598
599                 {"badhost",
600                                 {"reason",      "host",         NULL},
601                                 {DT_CHARPTR,    DT_CHARPTR},
602                                 InitXLine, DoKLine, DoneXLine},
603
604                 {"exception",
605                                 {"reason",      "host",         NULL},
606                                 {DT_CHARPTR,    DT_CHARPTR},
607                                 InitXLine, DoELine, DoneXLine},
608
609                 {"type",
610                                 {"name",        "classes",      NULL},
611                                 {DT_CHARPTR,    DT_CHARPTR},
612                                 InitTypes, DoType, DoneClassesAndTypes},
613
614                 {"class",
615                                 {"name",        "commands",     NULL},
616                                 {DT_CHARPTR,    DT_CHARPTR},
617                                 InitClasses, DoClass, DoneClassesAndTypes},
618
619                 {NULL}
620         };
621
622         include_stack.clear();
623
624         /* Load and parse the config file, if there are any errors then explode */
625         
626         /* Make a copy here so if it fails then we can carry on running with an unaffected config */
627         ConfigDataHash newconfig;
628         
629         if (this->LoadConf(newconfig, CONFIG_FILE, errstr))
630         {
631                 /* If we succeeded, set the ircd config to the new one */
632                 this->config_data = newconfig;  
633         }
634         else
635         {
636                 ServerInstance->Log(DEFAULT, "There were errors in your configuration:\n%s", errstr.str().c_str());
637
638                 if (bail)
639                 {
640                         /* Unneeded because of the ServerInstance->Log() aboive? */
641                         printf("There were errors in your configuration:\n%s",errstr.str().c_str());
642                         InspIRCd::Exit(ERROR);
643                 }
644                 else
645                 {
646                         std::string errors = errstr.str();
647                         std::string::size_type start;
648                         unsigned int prefixlen;
649                         
650                         start = 0;
651                         /* ":ServerInstance->Config->ServerName NOTICE user->nick :" */
652                         prefixlen = strlen(this->ServerName) + strlen(user->nick) + 11;
653         
654                         if (user)
655                         {
656                                 user->WriteServ("NOTICE %s :There were errors in the configuration file:",user->nick);
657                                 
658                                 while(start < errors.length())
659                                 {
660                                         user->WriteServ("NOTICE %s :%s",user->nick, errors.substr(start, 510 - prefixlen).c_str());
661                                         start += 510 - prefixlen;
662                                 }
663                         }
664                         else
665                         {
666                                 ServerInstance->WriteOpers("There were errors in the configuration file:");
667                                 
668                                 while(start < errors.length())
669                                 {
670                                         ServerInstance->WriteOpers(errors.substr(start, 360).c_str());
671                                         start += 360;
672                                 }
673                         }
674
675                         return;
676                 }
677         }
678
679         /* Check we dont have more than one of singular tags, or any of them missing
680          */
681         for (int Index = 0; Once[Index]; Index++)
682                 if (!CheckOnce(Once[Index],bail,user))
683                         return;
684
685         /* Read the values of all the tags which occur once or not at all, and call their callbacks.
686          */
687         for (int Index = 0; Values[Index].tag; Index++)
688         {
689                 int* val_i = (int*) Values[Index].val;
690                 char* val_c = (char*) Values[Index].val;
691
692                 switch (Values[Index].datatype)
693                 {
694                         case DT_CHARPTR:
695                                 /* Assuming MAXBUF here, potentially unsafe */
696                                 ConfValue(this->config_data, Values[Index].tag, Values[Index].value, 0, val_c, MAXBUF);
697                         break;
698
699                         case DT_INTEGER:
700                                 ConfValueInteger(this->config_data, Values[Index].tag, Values[Index].value, 0, *val_i);
701                         break;
702
703                         case DT_BOOLEAN:
704                                 *val_i = ConfValueBool(this->config_data, Values[Index].tag, Values[Index].value, 0);
705                         break;
706
707                         case DT_NOTHING:
708                         break;
709                 }
710
711                 Values[Index].validation_function(this, Values[Index].tag, Values[Index].value, Values[Index].val);
712         }
713
714         /* Claim memory for use when reading multiple tags
715          */
716         for (int n = 0; n < 12; n++)
717                 data[n] = new char[MAXBUF];
718
719         /* Read the multiple-tag items (class tags, connect tags, etc)
720          * and call the callbacks associated with them. We have three
721          * callbacks for these, a 'start', 'item' and 'end' callback.
722          */
723         
724         /* XXX - Make this use ConfValueInteger and so on */
725         for (int Index = 0; MultiValues[Index].tag; Index++)
726         {
727                 MultiValues[Index].init_function(this, MultiValues[Index].tag);
728
729                 int number_of_tags = ConfValueEnum(this->config_data, MultiValues[Index].tag);
730
731                 for (int tagnum = 0; tagnum < number_of_tags; tagnum++)
732                 {
733                         for (int valuenum = 0; MultiValues[Index].items[valuenum]; valuenum++)
734                         {
735                                 ConfValue(this->config_data, MultiValues[Index].tag, MultiValues[Index].items[valuenum], tagnum, data[valuenum], MAXBUF);
736
737                                 switch (MultiValues[Index].datatype[valuenum])
738                                 {
739                                         case DT_CHARPTR:
740                                                 ptr[valuenum] = data[valuenum];
741                                         break;
742                                         case DT_INTEGER:
743                                                 r_i[valuenum] = atoi(data[valuenum]);
744                                                 ptr[valuenum] = &r_i[valuenum];
745                                         break;
746                                         case DT_BOOLEAN:
747                                                 r_i[valuenum] = ((*data[valuenum] == tolower('y')) || (*data[valuenum] == tolower('t')) || (*data[valuenum] == '1'));
748                                                 ptr[valuenum] = &r_i[valuenum];
749                                         break;
750                                         default:
751                                         break;
752                                 }
753                         }
754                         MultiValues[Index].validation_function(this, MultiValues[Index].tag, (char**)MultiValues[Index].items, ptr, MultiValues[Index].datatype);
755                 }
756
757                 MultiValues[Index].finish_function(this, MultiValues[Index].tag);
758         }
759
760         /* Free any memory we claimed
761          */
762         for (int n = 0; n < 12; n++)
763                 delete[] data[n];
764
765         // write once here, to try it out and make sure its ok
766         ServerInstance->WritePID(this->PID);
767
768         ServerInstance->Log(DEFAULT,"Done reading configuration file, InspIRCd is now starting.");
769
770         /* If we're rehashing, let's load any new modules, and unload old ones
771          */
772         if (!bail)
773         {
774                 int found_ports;
775                 FailedPortList pl;
776                 ServerInstance->stats->BoundPortCount = ServerInstance->BindPorts(false, found_ports, pl);
777
778                 if (!removed_modules.empty())
779                         for (std::vector<std::string>::iterator removing = removed_modules.begin(); removing != removed_modules.end(); removing++)
780                         {
781                                 if (ServerInstance->UnloadModule(removing->c_str()))
782                                 {
783                                         ServerInstance->WriteOpers("*** REHASH UNLOADED MODULE: %s",removing->c_str());
784
785                                         if (user)
786                                                 user->WriteServ("973 %s %s :Module %s successfully unloaded.",user->nick, removing->c_str(), removing->c_str());
787
788                                         rem++;
789                                 }
790                                 else
791                                 {
792                                         if (user)
793                                                 user->WriteServ("972 %s %s :Failed to unload module %s: %s",user->nick, removing->c_str(), removing->c_str(), ServerInstance->ModuleError());
794                                 }
795                         }
796
797                 if (!added_modules.empty())
798                 for (std::vector<std::string>::iterator adding = added_modules.begin(); adding != added_modules.end(); adding++)
799                 {
800                         if (ServerInstance->LoadModule(adding->c_str()))
801                         {
802                                 ServerInstance->WriteOpers("*** REHASH LOADED MODULE: %s",adding->c_str());
803
804                                 if (user)
805                                         user->WriteServ("975 %s %s :Module %s successfully loaded.",user->nick, adding->c_str(), adding->c_str());
806
807                                 add++;
808                         }
809                         else
810                         {
811                                 if (user)
812                                         user->WriteServ("974 %s %s :Failed to load module %s: %s",user->nick, adding->c_str(), adding->c_str(), ServerInstance->ModuleError());
813                         }
814                 }
815
816                 ServerInstance->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());
817         }
818 }
819
820 bool ServerConfig::LoadConf(ConfigDataHash &target, const char* filename, std::ostringstream &errorstream)
821 {
822         std::ifstream conf(filename);
823         std::string line;
824         char ch;
825         long linenumber;
826         bool in_tag;
827         bool in_quote;
828         bool in_comment;
829         
830         linenumber = 1;
831         in_tag = false;
832         in_quote = false;
833         in_comment = false;
834         
835         /* Check if the file open failed first */
836         if (!conf)
837         {
838                 errorstream << "LoadConf: Couldn't open config file: " << filename << std::endl;
839                 return false;
840         }
841         
842         /* Fix the chmod of the file to restrict it to the current user and group */
843         chmod(filename,0600);
844         
845         for (unsigned int t = 0; t < include_stack.size(); t++)
846         {
847                 if (std::string(filename) == include_stack[t])
848                 {
849                         errorstream << "File " << filename << " is included recursively (looped inclusion)." << std::endl;
850                         return false;
851                 }
852         }
853         
854         /* It's not already included, add it to the list of files we've loaded */
855         include_stack.push_back(filename);
856         
857         /* Start reading characters... */       
858         while(conf.get(ch))
859         {
860                 /*
861                  * Here we try and get individual tags on separate lines,
862                  * this would be so easy if we just made people format
863                  * their config files like that, but they don't so...
864                  * We check for a '<' and then know the line is over when
865                  * we get a '>' not inside quotes. If we find two '<' and
866                  * no '>' then die with an error.
867                  */
868                 
869                 if((ch == '#') && !in_quote)
870                         in_comment = true;
871                 
872                 if(((ch == '\n') || (ch == '\r')) && in_quote)
873                 {
874                         errorstream << "Got a newline within a quoted section, this is probably a typo: " << filename << ":" << linenumber << std::endl;
875                         return false;
876                 }
877                 
878                 switch(ch)
879                 {
880                         case '\n':
881                                 linenumber++;
882                         case '\r':
883                                 in_comment = false;
884                         case '\0':
885                                 continue;
886                         case '\t':
887                                 ch = ' ';
888                 }
889                 
890                 if(in_comment)
891                         continue;
892
893                 /* XXX: Added by Brain, May 1st 2006 - Escaping of characters.
894                  * Note that this WILL NOT usually allow insertion of newlines,
895                  * because a newline is two characters long. Use it primarily to
896                  * insert the " symbol.
897                  *
898                  * Note that this also involves a further check when parsing the line,
899                  * which can be found below.
900                  */
901                 if ((ch == '\\') && (in_quote) && (in_tag))
902                 {
903                         line += ch;
904                         ServerInstance->Log(DEBUG,"Escape sequence in config line.");
905                         char real_character;
906                         if (conf.get(real_character))
907                         {
908                                 ServerInstance->Log(DEBUG,"Escaping %c", real_character);
909                                 if (real_character == 'n')
910                                         real_character = '\n';
911                                 line += real_character;
912                                 continue;
913                         }
914                         else
915                         {
916                                 errorstream << "End of file after a \\, what did you want to escape?: " << filename << ":" << linenumber << std::endl;
917                                 return false;
918                         }
919                 }
920
921                 line += ch;
922                 
923                 if(ch == '<')
924                 {
925                         if(in_tag)
926                         {
927                                 if(!in_quote)
928                                 {
929                                         errorstream << "Got another opening < when the first one wasn't closed: " << filename << ":" << linenumber << std::endl;
930                                         return false;
931                                 }
932                         }
933                         else
934                         {
935                                 if(in_quote)
936                                 {
937                                         errorstream << "We're in a quote but outside a tag, interesting. " << filename << ":" << linenumber << std::endl;
938                                         return false;
939                                 }
940                                 else
941                                 {
942                                         // errorstream << "Opening new config tag on line " << linenumber << std::endl;
943                                         in_tag = true;
944                                 }
945                         }
946                 }
947                 else if(ch == '"')
948                 {
949                         if(in_tag)
950                         {
951                                 if(in_quote)
952                                 {
953                                         // errorstream << "Closing quote in config tag on line " << linenumber << std::endl;
954                                         in_quote = false;
955                                 }
956                                 else
957                                 {
958                                         // errorstream << "Opening quote in config tag on line " << linenumber << std::endl;
959                                         in_quote = true;
960                                 }
961                         }
962                         else
963                         {
964                                 if(in_quote)
965                                 {
966                                         errorstream << "Found a (closing) \" outside a tag: " << filename << ":" << linenumber << std::endl;
967                                 }
968                                 else
969                                 {
970                                         errorstream << "Found a (opening) \" outside a tag: " << filename << ":" << linenumber << std::endl;
971                                 }
972                         }
973                 }
974                 else if(ch == '>')
975                 {
976                         if(!in_quote)
977                         {
978                                 if(in_tag)
979                                 {
980                                         // errorstream << "Closing config tag on line " << linenumber << std::endl;
981                                         in_tag = false;
982
983                                         /*
984                                          * If this finds an <include> then ParseLine can simply call
985                                          * LoadConf() and load the included config into the same ConfigDataHash
986                                          */
987                                         
988                                         if(!this->ParseLine(target, line, linenumber, errorstream))
989                                                 return false;
990                                         
991                                         line.clear();
992                                 }
993                                 else
994                                 {
995                                         errorstream << "Got a closing > when we weren't inside a tag: " << filename << ":" << linenumber << std::endl;
996                                         return false;
997                                 }
998                         }
999                 }
1000         }
1001         
1002         return true;
1003 }
1004
1005 bool ServerConfig::LoadConf(ConfigDataHash &target, const std::string &filename, std::ostringstream &errorstream)
1006 {
1007         return this->LoadConf(target, filename.c_str(), errorstream);
1008 }
1009
1010 bool ServerConfig::ParseLine(ConfigDataHash &target, std::string &line, long linenumber, std::ostringstream &errorstream)
1011 {
1012         std::string tagname;
1013         std::string current_key;
1014         std::string current_value;
1015         KeyValList results;
1016         bool got_name;
1017         bool got_key;
1018         bool in_quote;
1019         
1020         got_name = got_key = in_quote = false;
1021         
1022         // std::cout << "ParseLine(data, '" << line << "', " << linenumber << ", stream)" << std::endl;
1023         
1024         for(std::string::iterator c = line.begin(); c != line.end(); c++)
1025         {
1026                 if(!got_name)
1027                 {
1028                         /* We don't know the tag name yet. */
1029                         
1030                         if(*c != ' ')
1031                         {
1032                                 if(*c != '<')
1033                                 {
1034                                         tagname += *c;
1035                                 }
1036                         }
1037                         else
1038                         {
1039                                 /* We got to a space, we should have the tagname now. */
1040                                 if(tagname.length())
1041                                 {
1042                                         got_name = true;
1043                                 }
1044                         }
1045                 }
1046                 else
1047                 {
1048                         /* We have the tag name */
1049                         if (!got_key)
1050                         {
1051                                 /* We're still reading the key name */
1052                                 if (*c != '=')
1053                                 {
1054                                         if (*c != ' ')
1055                                         {
1056                                                 current_key += *c;
1057                                         }
1058                                 }
1059                                 else
1060                                 {
1061                                         /* We got an '=', end of the key name. */
1062                                         got_key = true;
1063                                 }
1064                         }
1065                         else
1066                         {
1067                                 /* We have the key name, now we're looking for quotes and the value */
1068
1069                                 /* Correctly handle escaped characters here.
1070                                  * See the XXX'ed section above.
1071                                  */
1072                                 if ((*c == '\\') && (in_quote))
1073                                 {
1074                                         c++;
1075                                         if (*c == 'n')
1076                                                 current_value += '\n';
1077                                         else
1078                                                 current_value += *c;
1079                                         continue;
1080                                 }
1081                                 if (*c == '"')
1082                                 {
1083                                         if (!in_quote)
1084                                         {
1085                                                 /* We're not already in a quote. */
1086                                                 in_quote = true;
1087                                         }
1088                                         else
1089                                         {
1090                                                 /* Leaving quotes, we have the value */
1091                                                 results.push_back(KeyVal(current_key, current_value));
1092                                                 
1093                                                 // std::cout << "<" << tagname << ":" << current_key << "> " << current_value << std::endl;
1094                                                 
1095                                                 in_quote = false;
1096                                                 got_key = false;
1097                                                 
1098                                                 if((tagname == "include") && (current_key == "file"))
1099                                                 {
1100                                                         if(!this->DoInclude(target, current_value, errorstream))
1101                                                                 return false;
1102                                                 }
1103                                                 
1104                                                 current_key.clear();
1105                                                 current_value.clear();
1106                                         }
1107                                 }
1108                                 else
1109                                 {
1110                                         if(in_quote)
1111                                         {
1112                                                 current_value += *c;
1113                                         }
1114                                 }
1115                         }
1116                 }
1117         }
1118         
1119         /* Finished parsing the tag, add it to the config hash */
1120         target.insert(std::pair<std::string, KeyValList > (tagname, results));
1121         
1122         return true;
1123 }
1124
1125 bool ServerConfig::DoInclude(ConfigDataHash &target, const std::string &file, std::ostringstream &errorstream)
1126 {
1127         std::string confpath;
1128         std::string newfile;
1129         std::string::size_type pos;
1130         
1131         confpath = CONFIG_FILE;
1132         newfile = file;
1133         
1134         for (std::string::iterator c = newfile.begin(); c != newfile.end(); c++)
1135         {
1136                 if (*c == '\\')
1137                 {
1138                         *c = '/';
1139                 }
1140         }
1141
1142         if (file[0] != '/')
1143         {
1144                 if((pos = confpath.find("/inspircd.conf")) != std::string::npos)
1145                 {
1146                         /* Leaves us with just the path */
1147                         newfile = confpath.substr(0, pos) + std::string("/") + newfile;
1148                 }
1149                 else
1150                 {
1151                         errorstream << "Couldn't get config path from: " << confpath << std::endl;
1152                         return false;
1153                 }
1154         }
1155         
1156         return LoadConf(target, newfile, errorstream);
1157 }
1158
1159 bool ServerConfig::ConfValue(ConfigDataHash &target, const char* tag, const char* var, int index, char* result, int length)
1160 {
1161         std::string value;
1162         bool r = ConfValue(target, std::string(tag), std::string(var), index, value);
1163         strlcpy(result, value.c_str(), length);
1164         return r;
1165 }
1166
1167 bool ServerConfig::ConfValue(ConfigDataHash &target, const std::string &tag, const std::string &var, int index, std::string &result)
1168 {
1169         ConfigDataHash::size_type pos = index;
1170         if((pos >= 0) && (pos < target.count(tag)))
1171         {
1172                 ConfigDataHash::const_iterator iter = target.find(tag);
1173                 
1174                 for(int i = 0; i < index; i++)
1175                         iter++;
1176                 
1177                 for(KeyValList::const_iterator j = iter->second.begin(); j != iter->second.end(); j++)
1178                 {
1179                         if(j->first == var)
1180                         {
1181                                 result = j->second;
1182                                 return true;
1183                         }
1184                 }
1185         }
1186         else if(pos == 0)
1187         {
1188                 ServerInstance->Log(DEBUG, "No <%s> tags in config file.", tag.c_str());
1189         }
1190         else
1191         {
1192                 ServerInstance->Log(DEBUG, "ConfValue got an out-of-range index %d, there are only %d occurences of %s", pos, target.count(tag), tag.c_str());
1193         }
1194         
1195         return false;
1196 }
1197         
1198 bool ServerConfig::ConfValueInteger(ConfigDataHash &target, const char* tag, const char* var, int index, int &result)
1199 {
1200         return ConfValueInteger(target, std::string(tag), std::string(var), index, result);
1201 }
1202
1203 bool ServerConfig::ConfValueInteger(ConfigDataHash &target, const std::string &tag, const std::string &var, int index, int &result)
1204 {
1205         std::string value;
1206         std::istringstream stream;
1207         bool r = ConfValue(target, tag, var, index, value);
1208         stream.str(value);
1209         if(!(stream >> result))
1210                 return false;
1211         return r;
1212 }
1213         
1214 bool ServerConfig::ConfValueBool(ConfigDataHash &target, const char* tag, const char* var, int index)
1215 {
1216         return ConfValueBool(target, std::string(tag), std::string(var), index);
1217 }
1218
1219 bool ServerConfig::ConfValueBool(ConfigDataHash &target, const std::string &tag, const std::string &var, int index)
1220 {
1221         std::string result;
1222         if(!ConfValue(target, tag, var, index, result))
1223                 return false;
1224         
1225         return ((result == "yes") || (result == "true") || (result == "1"));
1226 }
1227         
1228 int ServerConfig::ConfValueEnum(ConfigDataHash &target, const char* tag)
1229 {
1230         return target.count(tag);
1231 }
1232
1233 int ServerConfig::ConfValueEnum(ConfigDataHash &target, const std::string &tag)
1234 {
1235         return target.count(tag);
1236 }
1237         
1238 int ServerConfig::ConfVarEnum(ConfigDataHash &target, const char* tag, int index)
1239 {
1240         return ConfVarEnum(target, std::string(tag), index);
1241 }
1242
1243 int ServerConfig::ConfVarEnum(ConfigDataHash &target, const std::string &tag, int index)
1244 {
1245         ConfigDataHash::size_type pos = index;
1246         
1247         if((pos >= 0) && (pos < target.count(tag)))
1248         {
1249                 ConfigDataHash::const_iterator iter = target.find(tag);
1250                 
1251                 for(int i = 0; i < index; i++)
1252                         iter++;
1253                 
1254                 return iter->second.size();
1255         }
1256         else if(pos == 0)
1257         {
1258                 ServerInstance->Log(DEBUG, "No <%s> tags in config file.", tag.c_str());
1259         }
1260         else
1261         {
1262                 ServerInstance->Log(DEBUG, "ConfVarEnum got an out-of-range index %d, there are only %d occurences of %s", pos, target.count(tag), tag.c_str());
1263         }
1264         
1265         return 0;
1266 }
1267
1268 /** Read the contents of a file located by `fname' into a file_cache pointed at by `F'.
1269  */
1270 bool ServerConfig::ReadFile(file_cache &F, const char* fname)
1271 {
1272         FILE* file;
1273         char linebuf[MAXBUF];
1274
1275         F.clear();
1276         
1277         if (*fname != '/')
1278         {
1279                 std::string::size_type pos;
1280                 std::string confpath = CONFIG_FILE;
1281                 if((pos = confpath.find("/inspircd.conf")) != std::string::npos)
1282                 {
1283                         /* Leaves us with just the path */
1284                         std::string newfile = confpath.substr(0, pos) + std::string("/") + fname;
1285                         file =  fopen(newfile.c_str(), "r");
1286                         
1287                 }
1288         }
1289         else
1290                 file =  fopen(fname, "r");
1291
1292         if (file)
1293         {
1294                 while (!feof(file))
1295                 {
1296                         fgets(linebuf, sizeof(linebuf), file);
1297                         linebuf[strlen(linebuf)-1] = 0;
1298
1299                         if (!feof(file))
1300                         {
1301                                 F.push_back(*linebuf ? linebuf : " ");
1302                         }
1303                 }
1304
1305                 fclose(file);
1306         }
1307         else
1308                 return false;
1309
1310         return true;
1311 }
1312
1313 bool ServerConfig::FileExists(const char* file)
1314 {
1315         FILE *input;
1316         if ((input = fopen (file, "r")) == NULL)
1317         {
1318                 return false;
1319         }
1320         else
1321         {
1322                 fclose(input);
1323                 return true;
1324         }
1325 }
1326
1327 char* ServerConfig::CleanFilename(char* name)
1328 {
1329         char* p = name + strlen(name);
1330         while ((p != name) && (*p != '/')) p--;
1331         return (p != name ? ++p : p);
1332 }
1333
1334
1335 bool ServerConfig::DirValid(const char* dirandfile)
1336 {
1337         char work[MAXBUF];
1338         char buffer[MAXBUF];
1339         char otherdir[MAXBUF];
1340         int p;
1341
1342         strlcpy(work, dirandfile, MAXBUF);
1343         p = strlen(work);
1344
1345         // we just want the dir
1346         while (*work)
1347         {
1348                 if (work[p] == '/')
1349                 {
1350                         work[p] = '\0';
1351                         break;
1352                 }
1353
1354                 work[p--] = '\0';
1355         }
1356
1357         // Get the current working directory
1358         if (getcwd(buffer, MAXBUF ) == NULL )
1359                 return false;
1360
1361         chdir(work);
1362
1363         if (getcwd(otherdir, MAXBUF ) == NULL )
1364                 return false;
1365
1366         chdir(buffer);
1367
1368         size_t t = strlen(work);
1369
1370         if (strlen(otherdir) >= t)
1371         {
1372                 otherdir[t] = '\0';
1373
1374                 if (!strcmp(otherdir,work))
1375                 {
1376                         return true;
1377                 }
1378
1379                 return false;
1380         }
1381         else
1382         {
1383                 return false;
1384         }
1385 }
1386
1387 std::string ServerConfig::GetFullProgDir(char** argv, int argc)
1388 {
1389         char work[MAXBUF];
1390         char buffer[MAXBUF];
1391         char otherdir[MAXBUF];
1392         int p;
1393
1394         strlcpy(work,argv[0],MAXBUF);
1395         p = strlen(work);
1396
1397         // we just want the dir
1398         while (*work)
1399         {
1400                 if (work[p] == '/')
1401                 {
1402                         work[p] = '\0';
1403                         break;
1404                 }
1405
1406                 work[p--] = '\0';
1407         }
1408
1409         // Get the current working directory
1410         if (getcwd(buffer, MAXBUF) == NULL)
1411                 return "";
1412
1413         chdir(work);
1414
1415         if (getcwd(otherdir, MAXBUF) == NULL)
1416                 return "";
1417
1418         chdir(buffer);
1419         return otherdir;
1420 }
1421
1422 InspIRCd* ServerConfig::GetInstance()
1423 {
1424         return ServerInstance;
1425 }
1426