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