1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
15 /* $CopyInstall: conf/inspircd.quotes.example $(CONPATH) */
16 /* $CopyInstall: conf/inspircd.rules.example $(CONPATH) */
17 /* $CopyInstall: conf/inspircd.motd.example $(CONPATH) */
18 /* $CopyInstall: conf/inspircd.helpop-full.example $(CONPATH) */
19 /* $CopyInstall: conf/inspircd.helpop.example $(CONPATH) */
20 /* $CopyInstall: conf/inspircd.censor.example $(CONPATH) */
21 /* $CopyInstall: conf/inspircd.filter.example $(CONPATH) */
22 /* $CopyInstall: conf/inspircd.conf.example $(CONPATH) */
23 /* $CopyInstall: conf/modules.conf.example $(CONPATH) */
24 /* $CopyInstall: conf/opers.conf.example $(CONPATH) */
25 /* $CopyInstall: conf/links.conf.example $(CONPATH) */
26 /* $CopyInstall: .gdbargs $(BASE) */
31 #include "exitcodes.h"
32 #include "commands/cmd_whowas.h"
33 #include "modes/cmode_h.h"
35 static void ReqRead(ServerConfig* src, const std::string& tag, const std::string& key, std::string& dest)
37 ConfigTag* t = src->ConfValue(tag);
38 if (!t || !t->readString(key, dest))
39 throw CoreException("You must specify a value for <" + tag + ":" + key + ">");
42 /** Represents a deprecated configuration tag.
52 /** Reason for deprecation
57 ServerConfig::ServerConfig()
59 WhoWasGroupSize = WhoWasMaxGroups = WhoWasMaxKeep = 0;
61 NoUserDns = forcedebug = OperSpyWhois = nofork = HideBans = HideSplits = UndernetMsgPrefix = false;
62 CycleHosts = writelog = AllowHalfop = InvBypassModes = true;
63 dns_timeout = DieDelay = 5;
65 NetBufferSize = 10240;
66 SoftLimit = ServerInstance->SE->GetMaxFds();
76 void ServerConfig::Update005()
78 std::stringstream out(data005);
81 int token_counter = 0;
85 line5 = line5 + token + " ";
87 if (token_counter >= 13)
90 snprintf(buf, MAXBUF, "%s:are supported by this server", line5.c_str());
91 isupport.push_back(buf);
99 snprintf(buf, MAXBUF, "%s:are supported by this server", line5.c_str());
100 isupport.push_back(buf);
104 void ServerConfig::Send005(User* user)
106 for (std::vector<std::string>::iterator line = ServerInstance->Config->isupport.begin(); line != ServerInstance->Config->isupport.end(); line++)
107 user->WriteNumeric(RPL_ISUPPORT, "%s %s", user->nick.c_str(), line->c_str());
110 template<typename T, typename V>
111 static void range(T& value, V min, V max, V def, const char* msg)
113 if (value >= (T)min && value <= (T)max)
115 ServerInstance->Logs->Log("CONFIG", DEFAULT,
116 "WARNING: %s value of %ld is not between %ld and %ld; set to %ld.",
117 msg, (long)value, (long)min, (long)max, (long)def);
121 bool ServerConfig::CheckOnce(const char* tag)
124 throw CoreException("You have not defined a <"+std::string(tag)+"> tag, this is required.");
125 if (ConfValue(tag, 1))
126 throw CoreException("You have more than one <"+std::string(tag)+"> tag, this is not permitted.");
130 /* NOTE: Before anyone asks why we're not using inet_pton for this, it is because inet_pton and friends do not return so much detail,
131 * even in strerror(errno). They just return 'yes' or 'no' to an address without such detail as to whats WRONG with the address.
132 * Because ircd users arent as technical as they used to be (;)) we are going to give more of a useful error message.
134 static void ValidIP(const std::string& ip, const std::string& key)
136 const char* p = ip.c_str();
139 int not_numbers = false;
145 throw CoreException("The value of "+key+" is not an IP address");
147 for (const char* ptr = p; *ptr; ++ptr)
149 if (*ptr != ':' && *ptr != '.')
151 if (*ptr < '0' || *ptr > '9')
153 if ((*ptr < '0' || *ptr > '9') && (toupper(*ptr) < 'A' || toupper(*ptr) > 'F'))
159 throw CoreException("The value of "+key+" is not an IP address");
170 throw CoreException("The value of "+key+" is an IPv4 address with too many fields!");
173 throw CoreException("The value of "+key+" is an IPv6 address with too many fields!");
175 if (num_seps == 0 && num_dots < 3)
176 throw CoreException("The value of "+key+" looks to be a malformed IPv4 address");
178 if (num_seps == 0 && num_dots == 3 && not_numbers)
179 throw CoreException("The value of "+key+" contains non-numeric characters in an IPv4 address");
181 if (num_seps != 0 && not_hex)
182 throw CoreException("The value of "+key+" contains non-hexdecimal characters in an IPv6 address");
184 if (num_seps != 0 && num_dots != 3 && num_dots != 0)
185 throw CoreException("The value of "+key+" is a malformed IPv6 4in6 address");
189 static void ValidHost(const std::string& p, const std::string& msg)
192 if (p.empty() || p[0] == '.')
193 throw CoreException("The value of "+msg+" is not a valid hostname");
194 for (unsigned int i=0;i < p.length();i++)
199 throw CoreException("The value of "+msg+" is not a valid hostname");
206 throw CoreException("The value of "+msg+" is not a valid hostname");
209 // Specialized validators
211 bool ServerConfig::ApplyDisabledCommands(const std::string& data)
213 std::stringstream dcmds(data);
216 /* Enable everything first */
217 for (Commandtable::iterator x = ServerInstance->Parser->cmdlist.begin(); x != ServerInstance->Parser->cmdlist.end(); x++)
218 x->second->Disable(false);
220 /* Now disable all the ones which the user wants disabled */
221 while (dcmds >> thiscmd)
223 Commandtable::iterator cm = ServerInstance->Parser->cmdlist.find(thiscmd);
224 if (cm != ServerInstance->Parser->cmdlist.end())
226 cm->second->Disable(true);
233 // Note: the windows validator is in win32wrapper.cpp
234 void FindDNS(std::string& server);
236 static void FindDNS(std::string& server)
241 // attempt to look up their nameserver from /etc/resolv.conf
242 ServerInstance->Logs->Log("CONFIG",DEFAULT,"WARNING: <dns:server> not defined, attempting to find working server in /etc/resolv.conf...");
244 std::ifstream resolv("/etc/resolv.conf");
246 while (resolv >> server)
248 if (server == "nameserver")
251 ServerInstance->Logs->Log("CONFIG",DEFAULT,"<dns:server> set to '%s' as first resolver in /etc/resolv.conf.",server.c_str());
256 ServerInstance->Logs->Log("CONFIG",DEFAULT,"/etc/resolv.conf contains no viable nameserver entries! Defaulting to nameserver '127.0.0.1'!");
257 server = "127.0.0.1";
261 static void ReadXLine(ServerConfig* conf, const std::string& tag, const std::string& key, XLineFactory* make)
265 ConfigTag* ctag = conf->ConfValue(tag, i);
269 if (!ctag->readString(key, mask))
270 throw CoreException("<"+tag+":"+key+"> missing");
271 std::string reason = ctag->getString("reason", "<Config>");
272 XLine* xl = make->Generate(ServerInstance->Time(), 0, "<Config>", reason, mask);
273 if (!ServerInstance->XLines->AddLine(xl, NULL))
278 void ServerConfig::CrossCheckOperClassType()
280 for (int i = 0;; ++i)
282 ConfigTag* tag = ConfValue("class", i);
285 std::string name = tag->getString("name");
287 throw CoreException("<class:name> is required for all <class> tags");
288 operclass[name] = tag;
290 for (int i = 0;; ++i)
292 ConfigTag* tag = ConfValue("type", i);
296 std::string name = tag->getString("name");
298 throw CoreException("<type:name> is required for all <type> tags");
299 opertypes[name] = tag;
301 std::string classname;
302 irc::spacesepstream str(tag->getString("classes"));
303 while (str.GetToken(classname))
305 if (operclass.find(classname) == operclass.end())
306 throw CoreException("Oper type " + name + " has missing class " + classname);
311 void ServerConfig::CrossCheckConnectBlocks(ServerConfig* current)
313 typedef std::map<std::string, ConnectClass*> ClassMap;
314 ClassMap oldBlocksByMask;
317 for(ClassVector::iterator i = current->Classes.begin(); i != current->Classes.end(); ++i)
319 ConnectClass* c = *i;
320 std::string typeMask = (c->type == CC_ALLOW) ? "a" : "d";
322 oldBlocksByMask[typeMask] = c;
326 ClassMap newBlocksByMask;
327 std::map<std::string, int> names;
329 bool try_again = true;
330 for(int tries=0; try_again; tries++)
333 for(unsigned int i=0;; i++)
335 ConfigTag* tag = ConfValue("connect", i);
338 if (Classes.size() <= i)
343 ConnectClass* parent = NULL;
344 std::string parentName = tag->getString("parent");
345 if (!parentName.empty())
347 std::map<std::string,int>::iterator parentIter = names.find(parentName);
348 if (parentIter == names.end())
351 // couldn't find parent this time. If it's the last time, we'll never find it.
353 throw CoreException("Could not find parent connect class \"" + parentName + "\" for connect block " + ConvToStr(i));
356 parent = Classes[parentIter->second];
359 std::string name = tag->getString("name");
362 if (names.find(name) != names.end())
363 throw CoreException("Two connect classes with name \"" + name + "\" defined!");
367 std::string mask, typeMask;
370 if (tag->readString("allow", mask, false))
373 typeMask = 'a' + mask;
375 else if (tag->readString("deny", mask, false))
378 typeMask = 'd' + mask;
382 throw CoreException("Connect class must have an allow or deny mask (#" + ConvToStr(i) + ")");
384 ClassMap::iterator dupMask = newBlocksByMask.find(typeMask);
385 if (dupMask != newBlocksByMask.end())
386 throw CoreException("Two connect classes cannot have the same mask (" + mask + ")");
388 ConnectClass* me = parent ?
389 new ConnectClass(tag, type, mask, *parent) :
390 new ConnectClass(tag, type, mask);
395 tag->readString("password", me->pass);
396 tag->readString("hash", me->hash);
397 me->registration_timeout = tag->getInt("timeout", me->registration_timeout);
398 me->pingtime = tag->getInt("pingfreq", me->pingtime);
400 if (tag->readString("sendq", sendq))
402 // attempt to guess a good hard/soft sendq from a single value
403 long value = atol(sendq.c_str());
405 me->softsendqmax = value / 16;
407 me->softsendqmax = value;
408 me->hardsendqmax = value * 8;
410 me->softsendqmax = tag->getInt("softsendq", me->softsendqmax);
411 me->hardsendqmax = tag->getInt("hardsendq", me->hardsendqmax);
412 me->recvqmax = tag->getInt("recvq", me->recvqmax);
413 me->maxlocal = tag->getInt("localmax", me->maxlocal);
414 me->maxglobal = tag->getInt("globalmax", me->maxglobal);
415 me->port = tag->getInt("port", me->port);
416 me->maxchans = tag->getInt("maxchans", me->maxchans);
417 me->limit = tag->getInt("limit", me->limit);
419 ClassMap::iterator oldMask = oldBlocksByMask.find(typeMask);
420 if (oldMask != oldBlocksByMask.end())
422 ConnectClass* old = oldMask->second;
423 oldBlocksByMask.erase(oldMask);
428 newBlocksByMask[typeMask] = me;
434 static const Deprecated ChangedConfig[] = {
435 {"options", "hidelinks", "has been moved to <security:hidelinks> as of 1.2a3"},
436 {"options", "hidewhois", "has been moved to <security:hidewhois> as of 1.2a3"},
437 {"options", "userstats", "has been moved to <security:userstats> as of 1.2a3"},
438 {"options", "customversion", "has been moved to <security:customversion> as of 1.2a3"},
439 {"options", "hidesplits", "has been moved to <security:hidesplits> as of 1.2a3"},
440 {"options", "hidebans", "has been moved to <security:hidebans> as of 1.2a3"},
441 {"options", "hidekills", "has been moved to <security:hidekills> as of 1.2a3"},
442 {"options", "operspywhois", "has been moved to <security:operspywhois> as of 1.2a3"},
443 {"options", "announceinvites", "has been moved to <security:announceinvites> as of 1.2a3"},
444 {"options", "hidemodes", "has been moved to <security:hidemodes> as of 1.2a3"},
445 {"options", "maxtargets", "has been moved to <security:maxtargets> as of 1.2a3"},
446 {"options", "nouserdns", "has been moved to <performance:nouserdns> as of 1.2a3"},
447 {"options", "maxwho", "has been moved to <performance:maxwho> as of 1.2a3"},
448 {"options", "softlimit", "has been moved to <performance:softlimit> as of 1.2a3"},
449 {"options", "somaxconn", "has been moved to <performance:somaxconn> as of 1.2a3"},
450 {"options", "netbuffersize", "has been moved to <performance:netbuffersize> as of 1.2a3"},
451 {"options", "maxwho", "has been moved to <performance:maxwho> as of 1.2a3"},
452 {"options", "loglevel", "1.2 does not use the loglevel value. Please define <log> tags instead."},
453 {"die", "value", "has always been deprecated"},
456 void ServerConfig::Fill()
458 ReqRead(this, "server", "name", ServerName);
459 ReqRead(this, "power", "diepass", diepass);
460 ReqRead(this, "power", "restartpass", restartpass);
462 ConfigTag* options = ConfValue("options");
463 ConfigTag* security = ConfValue("security");
464 powerhash = ConfValue("power")->getString("hash");
465 DieDelay = ConfValue("power")->getInt("pause");
466 PrefixQuit = options->getString("prefixquit");
467 SuffixQuit = options->getString("suffixquit");
468 FixedQuit = options->getString("fixedquit");
469 PrefixPart = options->getString("prefixpart");
470 SuffixPart = options->getString("suffixpart");
471 FixedPart = options->getString("fixedpart");
472 SoftLimit = ConfValue("performance")->getInt("softlimit", ServerInstance->SE->GetMaxFds());
473 MaxConn = ConfValue("performance")->getInt("somaxconn", SOMAXCONN);
474 MoronBanner = options->getString("moronbanner", "You're banned!");
475 ServerDesc = ConfValue("server")->getString("description", "Configure Me");
476 Network = ConfValue("server")->getString("network", "Network");
477 sid = ConfValue("server")->getString("id", "");
478 AdminName = ConfValue("admin")->getString("name", "");
479 AdminEmail = ConfValue("admin")->getString("email", "null@example.com");
480 AdminNick = ConfValue("admin")->getString("nick", "admin");
481 ModPath = options->getString("moduledir", MOD_PATH);
482 NetBufferSize = ConfValue("performance")->getInt("netbuffersize", 10240);
483 MaxWhoResults = ConfValue("performance")->getInt("maxwho", 1024);
484 dns_timeout = ConfValue("dns")->getInt("timeout", 5);
485 DisabledCommands = ConfValue("disabled")->getString("commands", "");
486 DisabledDontExist = ConfValue("disabled")->getBool("fakenonexistant");
487 SetUser = security->getString("runasuser");
488 SetGroup = security->getString("runasgroup");
489 UserStats = security->getString("userstats");
490 CustomVersion = security->getString("customversion");
491 HideSplits = security->getBool("hidesplits");
492 HideBans = security->getBool("hidebans");
493 HideWhoisServer = security->getString("hidewhois");
494 HideKillsServer = security->getString("hidekills");
495 OperSpyWhois = security->getBool("operspywhois");
496 RestrictBannedUsers = security->getBool("restrictbannedusers", true);
497 GenericOper = security->getBool("genericoper");
498 NoUserDns = ConfValue("performance")->getBool("nouserdns");
499 SyntaxHints = options->getBool("syntaxhints");
500 CycleHosts = options->getBool("cyclehosts");
501 UndernetMsgPrefix = options->getBool("ircumsgprefix");
502 FullHostInTopic = options->getBool("hostintopic");
503 MaxTargets = security->getInt("maxtargets", 20);
504 DefaultModes = options->getString("defaultmodes", "nt");
505 PID = ConfValue("pid")->getString("file");
506 WhoWasGroupSize = ConfValue("whowas")->getInt("groupsize");
507 WhoWasMaxGroups = ConfValue("whowas")->getInt("maxgroups");
508 WhoWasMaxKeep = ServerInstance->Duration(ConfValue("whowas")->getString("maxkeep"));
509 DieValue = ConfValue("die")->getString("value");
510 MaxChans = ConfValue("channels")->getInt("users");
511 OperMaxChans = ConfValue("channels")->getInt("opers");
512 c_ipv4_range = ConfValue("cidr")->getInt("ipv4clone");
513 c_ipv6_range = ConfValue("cidr")->getInt("ipv6clone");
514 Limits.NickMax = ConfValue("limits")->getInt("maxnick", 32);
515 Limits.ChanMax = ConfValue("limits")->getInt("maxchan", 64);
516 Limits.MaxModes = ConfValue("limits")->getInt("maxmodes", 20);
517 Limits.IdentMax = ConfValue("limits")->getInt("maxident", 11);
518 Limits.MaxQuit = ConfValue("limits")->getInt("maxquit", 255);
519 Limits.MaxTopic = ConfValue("limits")->getInt("maxtopic", 307);
520 Limits.MaxKick = ConfValue("limits")->getInt("maxkick", 255);
521 Limits.MaxGecos = ConfValue("limits")->getInt("maxgecos", 128);
522 Limits.MaxAway = ConfValue("limits")->getInt("maxaway", 200);
523 InvBypassModes = options->getBool("invitebypassmodes", true);
525 range(SoftLimit, 10, ServerInstance->SE->GetMaxFds(), ServerInstance->SE->GetMaxFds(), "<performance:softlimit>");
526 range(MaxConn, 0, SOMAXCONN, SOMAXCONN, "<performance:somaxconn>");
527 range(MaxTargets, 1, 31, 20, "<security:maxtargets>");
528 range(NetBufferSize, 1024, 65534, 10240, "<performance:netbuffersize>");
529 range(MaxWhoResults, 1, 65535, 1024, "<performace:maxwho>");
530 range(WhoWasGroupSize, 0, 10000, 10, "<whowas:groupsize>");
531 range(WhoWasMaxGroups, 0, 1000000, 10240, "<whowas:maxgroups>");
532 range(WhoWasMaxKeep, 3600, INT_MAX, 3600, "<whowas:maxkeep>");
534 ValidIP(DNSServer, "<dns:server>");
535 ValidHost(ServerName, "<server:name>");
536 if (!sid.empty() && !ServerInstance->IsSID(sid))
537 throw CoreException(sid + " is not a valid server ID. A server ID must be 3 characters long, with the first character a digit and the next two characters a digit or letter.");
539 for (int i = 0;; ++i)
541 ConfigTag* tag = ConfValue("uline", i);
545 if (!tag->readString("server", server))
546 throw CoreException("<uline> tag missing server");
547 ulines[assign(server)] = tag->getBool("silent");
552 ConfigTag* tag = ConfValue("banlist", i);
556 if (!tag->readString("chan", chan))
557 throw CoreException("<banlist> tag missing chan");
558 maxbans[chan] = tag->getInt("limit");
561 ReadXLine(this, "badip", "ipmask", ServerInstance->XLines->GetFactory("Z"));
562 ReadXLine(this, "badnick", "nick", ServerInstance->XLines->GetFactory("Q"));
563 ReadXLine(this, "badhost", "host", ServerInstance->XLines->GetFactory("K"));
564 ReadXLine(this, "exception", "host", ServerInstance->XLines->GetFactory("E"));
566 memset(DisabledUModes, 0, sizeof(DisabledUModes));
567 for (const unsigned char* p = (const unsigned char*)ConfValue("disabled")->getString("usermodes").c_str(); *p; ++p)
569 if (*p < 'A' || *p > ('A' + 64)) throw CoreException(std::string("Invalid usermode ")+(char)*p+" was found.");
570 DisabledUModes[*p - 'A'] = 1;
573 memset(DisabledCModes, 0, sizeof(DisabledCModes));
574 for (const unsigned char* p = (const unsigned char*)ConfValue("disabled")->getString("chanmodes").c_str(); *p; ++p)
576 if (*p < 'A' || *p > ('A' + 64)) throw CoreException(std::string("Invalid chanmode ")+(char)*p+" was found.");
577 DisabledCModes[*p - 'A'] = 1;
580 memset(HideModeLists, 0, sizeof(HideModeLists));
581 for (const unsigned char* p = (const unsigned char*)ConfValue("security")->getString("hidemodes").c_str(); *p; ++p)
582 HideModeLists[*p] = true;
584 std::string v = security->getString("announceinvites");
587 AnnounceInvites = ServerConfig::INVITE_ANNOUNCE_OPS;
589 AnnounceInvites = ServerConfig::INVITE_ANNOUNCE_ALL;
590 else if (v == "dynamic")
591 AnnounceInvites = ServerConfig::INVITE_ANNOUNCE_DYNAMIC;
593 AnnounceInvites = ServerConfig::INVITE_ANNOUNCE_NONE;
595 bool AllowHalfOp = options->getBool("allowhalfop");
596 ModeHandler* mh = ServerInstance->Modes->FindMode('h', MODETYPE_CHANNEL);
597 if (AllowHalfOp && !mh) {
598 ServerInstance->Logs->Log("CONFIG", DEFAULT, "Enabling halfop mode.");
599 mh = new ModeChannelHalfOp;
600 ServerInstance->Modes->AddMode(mh);
601 } else if (!AllowHalfOp && mh) {
602 ServerInstance->Logs->Log("CONFIG", DEFAULT, "Disabling halfop mode.");
603 ServerInstance->Modes->DelMode(mh);
607 Module* whowas = ServerInstance->Modules->Find("cmd_whowas.so");
609 WhowasRequest(NULL, whowas, WhowasRequest::WHOWAS_PRUNE).Send();
614 /* These tags MUST occur and must ONLY occur once in the config file */
615 static const char* const Once[] = { "server", "admin", "files", "power", "options" };
617 // WARNING: it is not safe to use most of the codebase in this function, as it
618 // will run in the config reader thread
619 void ServerConfig::Read()
621 /* Load and parse the config file, if there are any errors then explode */
623 valid = DoInclude(ServerInstance->ConfigFileName, true);
626 ReadFile(MOTD, ConfValue("files")->getString("motd"));
627 ReadFile(RULES, ConfValue("files")->getString("rules"));
628 DNSServer = ConfValue("dns")->getString("server");
633 void ServerConfig::Apply(ServerConfig* old, const std::string &useruid)
636 /* std::ostringstream::clear() does not clear the string itself, only the error flags. */
638 errstr.str().clear();
639 include_stack.clear();
641 /* The stuff in here may throw CoreException, be sure we're in a position to catch it. */
644 /* Check we dont have more than one of singular tags, or any of them missing
646 for (int Index = 0; Index * sizeof(*Once) < sizeof(Once); Index++)
647 CheckOnce(Once[Index]);
649 for (int Index = 0; Index * sizeof(Deprecated) < sizeof(ChangedConfig); Index++)
652 if (ConfValue(ChangedConfig[Index].tag)->readString(ChangedConfig[Index].value, dummy, true))
653 throw CoreException(std::string("Your configuration contains a deprecated value: <") + ChangedConfig[Index].tag + ":" + ChangedConfig[Index].value + "> - " + ChangedConfig[Index].reason);
658 // Handle special items
659 CrossCheckOperClassType();
660 CrossCheckConnectBlocks(old);
662 catch (CoreException &ce)
664 errstr << ce.GetReason();
668 // write once here, to try it out and make sure its ok
669 ServerInstance->WritePID(this->PID);
672 * These values can only be set on boot. Keep their old values. Do it before we send messages so we actually have a servername.
676 this->ServerName = old->ServerName;
677 this->sid = old->sid;
678 this->argv = old->argv;
679 this->argc = old->argc;
681 // Same for ports... they're bound later on first run.
683 ServerInstance->BindPorts(pl);
686 errstr << "Not all your client ports could be bound.\nThe following port(s) failed to bind:\n";
689 for (FailedPortList::iterator i = pl.begin(); i != pl.end(); i++, j++)
692 snprintf(buf, MAXBUF, "%d. Address: %s Reason: %s\n", j, i->first.empty() ? "<all>" : i->first.c_str(), i->second.c_str());
698 User* user = useruid.empty() ? NULL : ServerInstance->FindNick(useruid);
700 valid = errstr.str().empty();
702 ServerInstance->Logs->Log("CONFIG",DEFAULT, "There were errors in your configuration file:");
704 while (errstr.good())
707 getline(errstr, line, '\n');
711 user->WriteServ("NOTICE %s :*** %s", user->nick.c_str(), line.c_str());
713 ServerInstance->SNO->WriteGlobalSno('a', line);
718 // Starting up, so print it out so it's seen. XXX this is a bit of a hack.
719 printf("%s\n", line.c_str());
724 errstr.str(std::string());
726 /* No old configuration -> initial boot, nothing more to do here */
731 ServerInstance->Exit(EXIT_STATUS_CONFIG);
737 // If there were errors processing configuration, don't touch modules.
744 void ServerConfig::ApplyModules(User* user)
746 const std::vector<std::string> v = ServerInstance->Modules->GetAllModuleNames(0);
747 std::vector<std::string> added_modules;
748 std::set<std::string> removed_modules(v.begin(), v.end());
752 ConfigTag* tag = ConfValue("module", i);
756 if (tag->readString("name", name))
758 // if this module is already loaded, the erase will succeed, so we need do nothing
759 // otherwise, we need to add the module (which will be done later)
760 if (removed_modules.erase(name) == 0)
761 added_modules.push_back(name);
765 for (std::set<std::string>::iterator removing = removed_modules.begin(); removing != removed_modules.end(); removing++)
767 // Don't remove cmd_*.so, just remove m_*.so
768 if (removing->c_str()[0] == 'c')
770 Module* m = ServerInstance->Modules->Find(*removing);
771 if (m && ServerInstance->Modules->Unload(m))
773 ServerInstance->SNO->WriteGlobalSno('a', "*** REHASH UNLOADED MODULE: %s",removing->c_str());
776 user->WriteNumeric(RPL_UNLOADEDMODULE, "%s %s :Module %s successfully unloaded.",user->nick.c_str(), removing->c_str(), removing->c_str());
778 ServerInstance->SNO->WriteGlobalSno('a', "Module %s successfully unloaded.", removing->c_str());
783 user->WriteNumeric(ERR_CANTUNLOADMODULE, "%s %s :Failed to unload module %s: %s",user->nick.c_str(), removing->c_str(), removing->c_str(), ServerInstance->Modules->LastError().c_str());
785 ServerInstance->SNO->WriteGlobalSno('a', "Failed to unload module %s: %s", removing->c_str(), ServerInstance->Modules->LastError().c_str());
789 for (std::vector<std::string>::iterator adding = added_modules.begin(); adding != added_modules.end(); adding++)
791 if (ServerInstance->Modules->Load(adding->c_str()))
793 ServerInstance->SNO->WriteGlobalSno('a', "*** REHASH LOADED MODULE: %s",adding->c_str());
795 user->WriteNumeric(RPL_LOADEDMODULE, "%s %s :Module %s successfully loaded.",user->nick.c_str(), adding->c_str(), adding->c_str());
797 ServerInstance->SNO->WriteGlobalSno('a', "Module %s successfully loaded.", adding->c_str());
802 user->WriteNumeric(ERR_CANTLOADMODULE, "%s %s :Failed to load module %s: %s",user->nick.c_str(), adding->c_str(), adding->c_str(), ServerInstance->Modules->LastError().c_str());
804 ServerInstance->SNO->WriteGlobalSno('a', "Failed to load module %s: %s", adding->c_str(), ServerInstance->Modules->LastError().c_str());
809 user->WriteServ("NOTICE %s :*** Successfully rehashed server.", user->nick.c_str());
811 ServerInstance->SNO->WriteGlobalSno('a', "*** Successfully rehashed server.");
814 bool ServerConfig::LoadConf(FILE* &conf, const char* filename, bool allowexeinc)
819 long last_successful_parse = 1;
823 int character_count = 0;
829 ServerInstance->Logs->Log("CONFIG", DEBUG, "Reading %s", filename);
831 /* Check if the file open failed first */
834 errstr << "LoadConf: Couldn't open config file: " << filename << std::endl;
838 for (unsigned int t = 0; t < include_stack.size(); t++)
840 if (std::string(filename) == include_stack[t])
842 errstr << "File " << filename << " is included recursively (looped inclusion)." << std::endl;
847 /* It's not already included, add it to the list of files we've loaded */
848 include_stack.push_back(filename);
850 /* Start reading characters... */
851 while ((ch = fgetc(conf)) != EOF)
854 * Fix for moronic windows issue spotted by Adremelech.
855 * Some windows editors save text files as utf-16, which is
856 * a total pain in the ass to parse. Users should save in the
857 * right config format! If we ever see a file where the first
858 * byte is 0xFF or 0xFE, or the second is 0xFF or 0xFE, then
859 * this is most likely a utf-16 file. Bail out and insult user.
861 if ((character_count++ < 2) && (ch == '\xFF' || ch == '\xFE'))
863 errstr << "File " << filename << " cannot be read, as it is encoded in braindead UTF-16. Save your file as plain ASCII!" << std::endl;
868 * Here we try and get individual tags on separate lines,
869 * this would be so easy if we just made people format
870 * their config files like that, but they don't so...
871 * We check for a '<' and then know the line is over when
872 * we get a '>' not inside quotes. If we find two '<' and
873 * no '>' then die with an error.
876 if ((ch == '#') && !in_quote)
897 /* XXX: Added by Brain, May 1st 2006 - Escaping of characters.
898 * Note that this WILL NOT usually allow insertion of newlines,
899 * because a newline is two characters long. Use it primarily to
900 * insert the " symbol.
902 * Note that this also involves a further check when parsing the line,
903 * which can be found below.
905 if ((ch == '\\') && (in_quote) && (in_tag))
911 real_character = fgetc(conf);
912 if (real_character == 'n')
913 real_character = '\n';
914 line += real_character;
919 errstr << "End of file after a \\, what did you want to escape?: " << filename << ":" << linenumber << std::endl;
927 if ((ch != '<') && (!in_tag) && (!in_comment) && (ch > ' ') && (ch != 9))
929 errstr << "You have stray characters beyond the tag which starts at " << filename << ":" << last_successful_parse << std::endl;
939 errstr << "The tag at location " << filename << ":" << last_successful_parse << " was valid, but there is an error in the tag which comes after it. You are possibly missing a \" or >. Please check this." << std::endl;
947 errstr << "Parser error: Inside a quote but not within the last valid tag, which was opened at: " << filename << ":" << last_successful_parse << std::endl;
952 // errstr << "Opening new config tag on line " << linenumber << std::endl;
963 // errstr << "Closing quote in config tag on line " << linenumber << std::endl;
968 // errstr << "Opening quote in config tag on line " << linenumber << std::endl;
976 errstr << "The tag immediately after the one at " << filename << ":" << last_successful_parse << " has a missing closing \" symbol. Please check this." << std::endl;
980 errstr << "You have opened a quote (\") beyond the tag at " << filename << ":" << last_successful_parse << " without opening a new tag. Please check this." << std::endl;
990 // errstr << "Closing config tag on line " << linenumber << std::endl;
994 * If this finds an <include> then ParseLine can simply call
995 * LoadConf() and load the included config into the same ConfigDataHash
997 long bl = linenumber;
998 if (!this->ParseLine(filename, line, linenumber, allowexeinc))
1000 last_successful_parse = linenumber;
1008 errstr << "You forgot to close the tag which comes immediately after the one at " << filename << ":" << last_successful_parse << std::endl;
1015 /* Fix for bug #392 - if we reach the end of a file and we are still in a quote or comment, most likely the user fucked up */
1016 if (in_comment || in_quote)
1018 errstr << "Reached end of file whilst still inside a quoted section or tag. This is most likely an error or there \
1019 is a newline missing from the end of the file: " << filename << ":" << linenumber << std::endl;
1026 bool ServerConfig::LoadConf(FILE* &conf, const std::string &filename, bool allowexeinc)
1028 return this->LoadConf(conf, filename.c_str(), allowexeinc);
1031 bool ServerConfig::ParseLine(const std::string &filename, std::string &line, long &linenumber, bool allowexeinc)
1033 std::string tagname;
1034 std::string current_key;
1035 std::string current_value;
1036 reference<ConfigTag> result;
1041 got_key = in_quote = false;
1043 for(std::string::iterator c = line.begin(); c != line.end(); c++)
1047 /* We don't know the tag name yet. */
1053 if ((*c >= 'a' && *c <= 'z') || (*c >= 'A' && *c <='Z') || (*c >= '0' && *c <= '9') || *c == '_')
1057 errstr << "Invalid character in value name of tag: '" << *c << "' in value '" << tagname << "' in filename: " << filename << ":" << linenumber << std::endl;
1064 /* We got to a space, we should have the tagname now. */
1065 if(tagname.length())
1067 result = new ConfigTag(tagname);
1073 /* We have the tag name */
1076 /* We're still reading the key name */
1077 if ((*c != '=') && (*c != '>'))
1081 if ((*c >= 'a' && *c <= 'z') || (*c >= 'A' && *c <='Z') || (*c >= '0' && *c <= '9') || *c == '_')
1085 errstr << "Invalid character in key: '" << *c << "' in key '" << current_key << "' in filename: " << filename << ":" << linenumber << std::endl;
1092 /* We got an '=', end of the key name. */
1098 /* We have the key name, now we're looking for quotes and the value */
1100 /* Correctly handle escaped characters here.
1101 * See the XXX'ed section above.
1103 if ((*c == '\\') && (in_quote))
1107 current_value += '\n';
1109 current_value += *c;
1112 else if ((*c == '\\') && (!in_quote))
1114 errstr << "You can't have an escape sequence outside of a quoted section: " << filename << ":" << linenumber << std::endl;
1117 else if ((*c == '\n') && (in_quote))
1119 /* Got a 'real' \n, treat it as part of the value */
1120 current_value += '\n';
1123 else if ((*c == '\r') && (in_quote))
1125 /* Got a \r, drop it */
1133 /* We're not already in a quote. */
1138 /* Leaving the quotes, we have the current value */
1139 result->items.push_back(KeyVal(current_key, current_value));
1141 // std::cout << "<" << tagname << ":" << current_key << "> " << current_value << std::endl;
1146 if ((tagname == "include") && (current_key == "file"))
1148 if (!this->DoInclude(current_value, allowexeinc))
1151 else if ((tagname == "include") && (current_key == "executable"))
1155 errstr << "Executable includes are not allowed to use <include:executable>\n"
1156 "This could be an attempt to execute commands from a malicious remote include.\n"
1157 "If you need multiple levels of remote include, create a script to assemble the "
1158 "contents locally or include files using <include:file>\n";
1162 /* Pipe an executable and use its stdout as config data */
1163 if (!this->DoPipe(current_value))
1167 current_key.clear();
1168 current_value.clear();
1176 current_value += *c;
1183 /* Finished parsing the tag, add it to the config hash */
1184 config_data.insert(std::make_pair(tagname, result));
1189 bool ServerConfig::DoPipe(const std::string &file)
1191 FILE* conf = popen(file.c_str(), "r");
1196 ret = LoadConf(conf, file.c_str(), false);
1200 errstr << "Couldn't execute: " << file << std::endl;
1205 bool ServerConfig::StartsWithWindowsDriveLetter(const std::string &path)
1207 return (path.length() > 2 && isalpha(path[0]) && path[1] == ':');
1210 bool ServerConfig::DoInclude(const std::string &file, bool allowexeinc)
1212 FILE* conf = fopen(file.c_str(), "r");
1217 ret = LoadConf(conf, file, allowexeinc);
1221 errstr << "Couldn't open config file: " << file << std::endl;
1226 ConfigTag* ServerConfig::ConfValue(const std::string &tag, int offset)
1228 ConfigDataHash::size_type pos = offset;
1229 if (pos >= config_data.count(tag))
1232 ConfigDataHash::iterator iter = config_data.find(tag);
1234 for(int i = 0; i < offset; i++)
1237 return iter->second;
1240 bool ConfigTag::readString(const std::string& key, std::string& value, bool allow_lf)
1244 for(std::vector<KeyVal>::iterator j = items.begin(); j != items.end(); ++j)
1249 if (!allow_lf && (value.find('\n') != std::string::npos))
1251 ServerInstance->Logs->Log("CONFIG",DEFAULT, "Value of <" + tag + ":" + key + "> contains a linefeed, and linefeeds in this value are not permitted -- stripped to spaces.");
1252 for (std::string::iterator n = value.begin(); n != value.end(); n++)
1261 std::string ConfigTag::getString(const std::string& key, const std::string& def)
1263 std::string res = def;
1265 readString(key, res);
1269 long ConfigTag::getInt(const std::string &key, long def)
1272 if(!this || !readString(key, result))
1275 const char* res_cstr = result.c_str();
1276 char* res_tail = NULL;
1277 long res = strtol(res_cstr, &res_tail, 0);
1278 if (res_tail == res_cstr)
1280 switch (toupper(*res_tail))
1286 res= res* 1024 * 1024;
1289 res= res* 1024 * 1024 * 1024;
1295 double ConfigTag::getFloat(const std::string &key, double def)
1298 if (!readString(key, result))
1300 return strtod(result.c_str(), NULL);
1303 bool ConfigTag::getBool(const std::string &key, bool def)
1306 if(!readString(key, result))
1309 return (result == "yes" || result == "true" || result == "1" || result == "on");
1312 /** Read the contents of a file located by `fname' into a file_cache pointed at by `F'.
1314 bool ServerConfig::ReadFile(file_cache &F, const std::string& fname)
1320 char linebuf[MAXBUF];
1324 if (!FileExists(fname.c_str()))
1326 file = fopen(fname.c_str(), "r");
1332 if (fgets(linebuf, sizeof(linebuf), file))
1333 linebuf[strlen(linebuf)-1] = 0;
1337 F.push_back(*linebuf ? linebuf : " ");
1348 bool ServerConfig::FileExists(const char* file)
1351 if (stat(file, &sb) == -1)
1354 if ((sb.st_mode & S_IFDIR) > 0)
1357 FILE *input = fopen(file, "r");
1367 const char* ServerConfig::CleanFilename(const char* name)
1369 const char* p = name + strlen(name);
1370 while ((p != name) && (*p != '/') && (*p != '\\')) p--;
1371 return (p != name ? ++p : p);
1375 std::string ServerConfig::GetSID()
1380 void ConfigReaderThread::Run()
1382 Config = new ServerConfig;
1387 void ConfigReaderThread::Finish()
1389 ServerConfig* old = ServerInstance->Config;
1390 ServerInstance->Logs->Log("CONFIG",DEBUG,"Switching to new configuration...");
1391 ServerInstance->Logs->CloseLogs();
1392 ServerInstance->Config = this->Config;
1393 ServerInstance->Logs->OpenFileLogs();
1394 Config->Apply(old, TheUserUID);
1399 * Apply the changed configuration from the rehash.
1401 * XXX: The order of these is IMPORTANT, do not reorder them without testing
1404 ServerInstance->XLines->CheckELines();
1405 ServerInstance->XLines->CheckELines();
1406 ServerInstance->XLines->ApplyLines();
1407 ServerInstance->Res->Rehash();
1408 ServerInstance->ResetMaxBans();
1409 Config->ApplyDisabledCommands(Config->DisabledCommands);
1410 User* user = TheUserUID.empty() ? ServerInstance->FindNick(TheUserUID) : NULL;
1411 FOREACH_MOD(I_OnRehash, OnRehash(user));
1412 ServerInstance->BuildISupport();
1419 ServerInstance->Logs->CloseLogs();
1420 ServerInstance->Config = old;
1421 ServerInstance->Logs->OpenFileLogs();
1422 delete this->Config;