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