1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 Matt Schatz <genius3000@g3k.solutions>
5  *   Copyright (C) 2013-2016 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2013-2014, 2016-2021 Sadie Powell <sadie@witchery.services>
7  *   Copyright (C) 2013 Daniel Vassdal <shutter@canternet.org>
8  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
9  *   Copyright (C) 2012 Justin Crawford <Justasic@Gmail.com>
10  *   Copyright (C) 2012 DjSlash <djslash@djslash.org>
11  *   Copyright (C) 2012 ChrisTX <xpipe@hotmail.de>
12  *   Copyright (C) 2009-2011 Daniel De Graaf <danieldg@inspircd.org>
13  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
14  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
15  *   Copyright (C) 2007-2010 Robin Burchell <robin+git@viroteck.net>
16  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
17  *   Copyright (C) 2006-2008 Craig Edwards <brain@inspircd.org>
18  *   Copyright (C) 2006 Oliver Lupton <om@inspircd.org>
19  *
20  * This file is part of InspIRCd.  InspIRCd is free software: you can
21  * redistribute it and/or modify it under the terms of the GNU General Public
22  * License as published by the Free Software Foundation, version 2.
23  *
24  * This program is distributed in the hope that it will be useful, but WITHOUT
25  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
26  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
27  * details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31  */
32 
33 
34 #include "inspircd.h"
35 #include "xline.h"
36 #include "listmode.h"
37 #include "exitcodes.h"
38 #include "configparser.h"
39 #include <iostream>
40 
ServerLimits(ConfigTag * tag)41 ServerLimits::ServerLimits(ConfigTag* tag)
42 	: MaxLine(tag->getUInt("maxline", 512, 512))
43 	, NickMax(tag->getUInt("maxnick", 30, 1, MaxLine))
44 	, ChanMax(tag->getUInt("maxchan", 64, 1, MaxLine))
45 	, MaxModes(tag->getUInt("maxmodes", 20, 1))
46 	, IdentMax(tag->getUInt("maxident", 10, 1))
47 	, MaxQuit(tag->getUInt("maxquit", 255, 0, MaxLine))
48 	, MaxTopic(tag->getUInt("maxtopic", 307, 1, MaxLine))
49 	, MaxKick(tag->getUInt("maxkick", 255, 1, MaxLine))
50 	, MaxReal(tag->getUInt("maxreal", tag->getUInt("maxgecos", 128), 1, MaxLine))
51 	, MaxAway(tag->getUInt("maxaway", 200, 1, MaxLine))
52 	, MaxHost(tag->getUInt("maxhost", 64, 1, MaxLine))
53 {
54 }
55 
ServerPaths(ConfigTag * tag)56 ServerConfig::ServerPaths::ServerPaths(ConfigTag* tag)
57 	: Config(tag->getString("configdir", INSPIRCD_CONFIG_PATH, 1))
58 	, Data(tag->getString("datadir", INSPIRCD_DATA_PATH, 1))
59 	, Log(tag->getString("logdir", INSPIRCD_LOG_PATH, 1))
60 	, Module(tag->getString("moduledir", INSPIRCD_MODULE_PATH, 1))
61 	, Runtime(tag->getString("runtimedir", INSPIRCD_RUNTIME_PATH, 1))
62 {
63 }
64 
CreateEmptyTag()65 static ConfigTag* CreateEmptyTag()
66 {
67 	ConfigItems* items;
68 	return ConfigTag::create("empty", "<auto>", 0, items);
69 }
70 
ServerConfig()71 ServerConfig::ServerConfig()
72 	: EmptyTag(CreateEmptyTag())
73 	, Limits(EmptyTag)
74 	, Paths(EmptyTag)
75 	, RawLog(false)
76 	, NoSnoticeStack(false)
77 {
78 }
79 
~ServerConfig()80 ServerConfig::~ServerConfig()
81 {
82 	delete EmptyTag;
83 }
84 
ReadXLine(ServerConfig * conf,const std::string & tag,const std::string & key,XLineFactory * make)85 static void ReadXLine(ServerConfig* conf, const std::string& tag, const std::string& key, XLineFactory* make)
86 {
87 	insp::flat_set<std::string> configlines;
88 
89 	ConfigTagList tags = conf->ConfTags(tag);
90 	for(ConfigIter i = tags.first; i != tags.second; ++i)
91 	{
92 		ConfigTag* ctag = i->second;
93 
94 		const std::string mask = ctag->getString(key);
95 		if (mask.empty())
96 			throw CoreException("<" + tag + ":" + key + "> missing at " + ctag->getTagLocation());
97 
98 		const std::string reason = ctag->getString("reason");
99 		if (reason.empty())
100 			throw CoreException("<" + tag + ":reason> missing at " + ctag->getTagLocation());
101 
102 		XLine* xl = make->Generate(ServerInstance->Time(), 0, ServerInstance->Config->ServerName, reason, mask);
103 		xl->from_config = true;
104 		configlines.insert(xl->Displayable());
105 		if (!ServerInstance->XLines->AddLine(xl, NULL))
106 			delete xl;
107 	}
108 
109 	ServerInstance->XLines->ExpireRemovedConfigLines(make->GetType(), configlines);
110 }
111 
112 typedef std::map<std::string, ConfigTag*> LocalIndex;
CrossCheckOperClassType()113 void ServerConfig::CrossCheckOperClassType()
114 {
115 	LocalIndex operclass;
116 	ConfigTagList tags = ConfTags("class");
117 	for(ConfigIter i = tags.first; i != tags.second; ++i)
118 	{
119 		ConfigTag* tag = i->second;
120 		std::string name = tag->getString("name");
121 		if (name.empty())
122 			throw CoreException("<class:name> missing from tag at " + tag->getTagLocation());
123 		if (operclass.find(name) != operclass.end())
124 			throw CoreException("Duplicate class block with name " + name + " at " + tag->getTagLocation());
125 		operclass[name] = tag;
126 	}
127 	tags = ConfTags("type");
128 	for(ConfigIter i = tags.first; i != tags.second; ++i)
129 	{
130 		ConfigTag* tag = i->second;
131 		std::string name = tag->getString("name");
132 		if (name.empty())
133 			throw CoreException("<type:name> is missing from tag at " + tag->getTagLocation());
134 		if (OperTypes.find(name) != OperTypes.end())
135 			throw CoreException("Duplicate type block with name " + name + " at " + tag->getTagLocation());
136 
137 		OperInfo* ifo = new OperInfo(name);
138 		OperTypes[name] = ifo;
139 		ifo->type_block = tag;
140 
141 		std::string classname;
142 		irc::spacesepstream str(tag->getString("classes"));
143 		while (str.GetToken(classname))
144 		{
145 			LocalIndex::iterator cls = operclass.find(classname);
146 			if (cls == operclass.end())
147 				throw CoreException("Oper type " + name + " has missing class " + classname);
148 			ifo->class_blocks.push_back(cls->second);
149 		}
150 	}
151 
152 	tags = ConfTags("oper");
153 	for(ConfigIter i = tags.first; i != tags.second; ++i)
154 	{
155 		ConfigTag* tag = i->second;
156 
157 		std::string name = tag->getString("name");
158 		if (name.empty())
159 			throw CoreException("<oper:name> missing from tag at " + tag->getTagLocation());
160 
161 		std::string type = tag->getString("type");
162 		OperIndex::iterator tblk = OperTypes.find(type);
163 		if (tblk == OperTypes.end())
164 			throw CoreException("Oper block " + name + " has missing type " + type);
165 		if (oper_blocks.find(name) != oper_blocks.end())
166 			throw CoreException("Duplicate oper block with name " + name + " at " + tag->getTagLocation());
167 
168 		OperInfo* ifo = new OperInfo(type);
169 		ifo->oper_block = tag;
170 		ifo->type_block = tblk->second->type_block;
171 		ifo->class_blocks.assign(tblk->second->class_blocks.begin(), tblk->second->class_blocks.end());
172 		oper_blocks[name] = ifo;
173 	}
174 }
175 
CrossCheckConnectBlocks(ServerConfig * current)176 void ServerConfig::CrossCheckConnectBlocks(ServerConfig* current)
177 {
178 	typedef std::map<std::pair<std::string, char>, ConnectClass*> ClassMap;
179 	ClassMap oldBlocksByMask;
180 	if (current)
181 	{
182 		for(ClassVector::iterator i = current->Classes.begin(); i != current->Classes.end(); ++i)
183 		{
184 			ConnectClass* c = *i;
185 			switch (c->type)
186 			{
187 				case CC_ALLOW:
188 				case CC_DENY:
189 					oldBlocksByMask[std::make_pair(c->host, c->type)] = c;
190 					break;
191 
192 				case CC_NAMED:
193 					oldBlocksByMask[std::make_pair(c->name, c->type)] = c;
194 					break;
195 			}
196 		}
197 	}
198 
199 	size_t blk_count = config_data.count("connect");
200 	if (blk_count == 0)
201 	{
202 		// No connect blocks found; make a trivial default block
203 		ConfigItems* items;
204 		ConfigTag* tag = ConfigTag::create("connect", "<auto>", 0, items);
205 		(*items)["allow"] = "*";
206 		config_data.insert(std::make_pair("connect", tag));
207 		blk_count = 1;
208 	}
209 
210 	Classes.resize(blk_count);
211 	std::map<std::string, size_t> names;
212 
213 	bool try_again = true;
214 	for(size_t tries = 0; try_again; tries++)
215 	{
216 		try_again = false;
217 		ConfigTagList tags = ConfTags("connect");
218 		size_t i = 0;
219 		for(ConfigIter it = tags.first; it != tags.second; ++it, ++i)
220 		{
221 			ConfigTag* tag = it->second;
222 			if (Classes[i])
223 				continue;
224 
225 			ConnectClass* parent = NULL;
226 			std::string parentName = tag->getString("parent");
227 			if (!parentName.empty())
228 			{
229 				std::map<std::string, size_t>::const_iterator parentIter = names.find(parentName);
230 				if (parentIter == names.end())
231 				{
232 					try_again = true;
233 					// couldn't find parent this time. If it's the last time, we'll never find it.
234 					if (tries >= blk_count)
235 						throw CoreException("Could not find parent connect class \"" + parentName + "\" for connect block at " + tag->getTagLocation());
236 					continue;
237 				}
238 				parent = Classes[parentIter->second];
239 			}
240 
241 			std::string name = tag->getString("name");
242 			std::string mask;
243 			char type;
244 
245 			if (tag->readString("allow", mask, false) && !mask.empty())
246 				type = CC_ALLOW;
247 			else if (tag->readString("deny", mask, false) && !mask.empty())
248 				type = CC_DENY;
249 			else if (!name.empty())
250 			{
251 				type = CC_NAMED;
252 				mask = name;
253 			}
254 			else
255 			{
256 				throw CoreException("Connect class must have allow, deny, or name specified at " + tag->getTagLocation());
257 			}
258 
259 			if (name.empty())
260 				name = "unnamed-" + ConvToStr(i);
261 
262 			if (names.find(name) != names.end())
263 				throw CoreException("Two connect classes with name \"" + name + "\" defined!");
264 			names[name] = i;
265 
266 			ConnectClass* me = parent ?
267 				new ConnectClass(tag, type, mask, *parent) :
268 				new ConnectClass(tag, type, mask);
269 
270 			me->name = name;
271 
272 			me->registration_timeout = tag->getDuration("timeout", me->registration_timeout);
273 			me->pingtime = tag->getDuration("pingfreq", me->pingtime);
274 			std::string sendq;
275 			if (tag->readString("sendq", sendq))
276 			{
277 				// attempt to guess a good hard/soft sendq from a single value
278 				unsigned long value = strtoul(sendq.c_str(), NULL, 10);
279 				if (value > 16384)
280 					me->softsendqmax = value / 16;
281 				else
282 					me->softsendqmax = value;
283 				me->hardsendqmax = value * 8;
284 			}
285 			me->softsendqmax = tag->getUInt("softsendq", me->softsendqmax);
286 			me->hardsendqmax = tag->getUInt("hardsendq", me->hardsendqmax);
287 			me->recvqmax = tag->getUInt("recvq", me->recvqmax);
288 			me->penaltythreshold = tag->getUInt("threshold", me->penaltythreshold);
289 			me->commandrate = tag->getUInt("commandrate", me->commandrate);
290 			me->fakelag = tag->getBool("fakelag", me->fakelag);
291 			me->maxlocal = tag->getUInt("localmax", me->maxlocal);
292 			me->maxglobal = tag->getUInt("globalmax", me->maxglobal);
293 			me->maxchans = tag->getUInt("maxchans", me->maxchans);
294 			me->maxconnwarn = tag->getBool("maxconnwarn", me->maxconnwarn);
295 			me->limit = tag->getUInt("limit", me->limit);
296 			me->resolvehostnames = tag->getBool("resolvehostnames", me->resolvehostnames);
297 			me->password = tag->getString("password", me->password);
298 
299 			me->passwordhash = tag->getString("hash", me->passwordhash);
300 			if (!me->password.empty() && (me->passwordhash.empty() || stdalgo::string::equalsci(me->passwordhash, "plaintext")))
301 			{
302 				ServerInstance->Logs->Log("CONNECTCLASS", LOG_DEFAULT, "<connect> tag '%s' at %s contains an plain text password, this is insecure!",
303 					name.c_str(), tag->getTagLocation().c_str());
304 			}
305 
306 			std::string ports = tag->getString("port");
307 			if (!ports.empty())
308 			{
309 				irc::portparser portrange(ports, false);
310 				while (int port = portrange.GetToken())
311 					me->ports.insert(port);
312 			}
313 
314 			ClassMap::iterator oldMask = oldBlocksByMask.find(std::make_pair(me->name, me->type));
315 			if (oldMask != oldBlocksByMask.end())
316 			{
317 				ConnectClass* old = oldMask->second;
318 				oldBlocksByMask.erase(oldMask);
319 				old->Update(me);
320 				delete me;
321 				me = old;
322 			}
323 			Classes[i] = me;
324 		}
325 	}
326 }
327 
GetServerHost()328 static std::string GetServerHost()
329 {
330 #ifndef _WIN32
331 	char hostname[256];
332 	if (gethostname(hostname, sizeof(hostname)) == 0)
333 	{
334 		std::string name(hostname);
335 		if (name.find('.') == std::string::npos)
336 			name.push_back('.');
337 
338 		if (name.length() <= ServerInstance->Config->Limits.MaxHost && InspIRCd::IsHost(name))
339 			return name;
340 	}
341 #endif
342 	return "irc.example.com";
343 }
344 
Fill()345 void ServerConfig::Fill()
346 {
347 	ConfigTag* options = ConfValue("options");
348 	ConfigTag* security = ConfValue("security");
349 	ConfigTag* server = ConfValue("server");
350 	if (sid.empty())
351 	{
352 		ServerName = server->getString("name", GetServerHost(), InspIRCd::IsHost);
353 
354 		sid = server->getString("id");
355 		if (!sid.empty() && !InspIRCd::IsSID(sid))
356 			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.");
357 
358 		CaseMapping = options->getString("casemapping", "rfc1459", 1);
359 		if (CaseMapping == "ascii")
360 			national_case_insensitive_map = ascii_case_insensitive_map;
361 		else if (CaseMapping == "rfc1459")
362 			national_case_insensitive_map = rfc_case_insensitive_map;
363 		else
364 			throw CoreException("<options:casemapping> must be set to 'ascii', or 'rfc1459'");
365 	}
366 	else
367 	{
368 		std::string name = server->getString("name");
369 		if (!name.empty() && name != ServerName)
370 			throw CoreException("You must restart to change the server name");
371 
372 		std::string nsid = server->getString("id");
373 		if (!nsid.empty() && nsid != sid)
374 			throw CoreException("You must restart to change the server id");
375 
376 		std::string casemapping = options->getString("casemapping");
377 		// Ignore this value if CaseMapping is set to something the core doesn't provide (i.e., m_nationalchars).
378 		if (!casemapping.empty() && casemapping != CaseMapping && (CaseMapping == "ascii" || CaseMapping == "rfc1459"))
379 			throw CoreException("You must restart to change the server casemapping");
380 
381 	}
382 	SoftLimit = ConfValue("performance")->getUInt("softlimit", (SocketEngine::GetMaxFds() > 0 ? SocketEngine::GetMaxFds() : LONG_MAX), 10);
383 	CCOnConnect = ConfValue("performance")->getBool("clonesonconnect", true);
384 	MaxConn = ConfValue("performance")->getUInt("somaxconn", SOMAXCONN);
385 	TimeSkipWarn = ConfValue("performance")->getDuration("timeskipwarn", 2, 0, 30);
386 	XLineMessage = options->getString("xlinemessage", options->getString("moronbanner", "You're banned!"));
387 	ServerDesc = server->getString("description", "Configure Me", 1);
388 	Network = server->getString("network", "Network", 1);
389 	NetBufferSize = ConfValue("performance")->getInt("netbuffersize", 10240, 1024, 65534);
390 	CustomVersion = security->getString("customversion");
391 	HideBans = security->getBool("hidebans");
392 	HideServer = security->getString("hideserver", security->getString("hidewhois"));
393 	SyntaxHints = options->getBool("syntaxhints");
394 	FullHostInTopic = options->getBool("hostintopic");
395 	MaxTargets = security->getUInt("maxtargets", 20, 1, 31);
396 	DefaultModes = options->getString("defaultmodes", "not");
397 	PID = ConfValue("pid")->getString("file");
398 	MaxChans = ConfValue("channels")->getUInt("users", 20);
399 	OperMaxChans = ConfValue("channels")->getUInt("opers", 0);
400 	c_ipv4_range = ConfValue("cidr")->getUInt("ipv4clone", 32, 1, 32);
401 	c_ipv6_range = ConfValue("cidr")->getUInt("ipv6clone", 128, 1, 128);
402 	Limits = ServerLimits(ConfValue("limits"));
403 	Paths = ServerPaths(ConfValue("path"));
404 	NoSnoticeStack = options->getBool("nosnoticestack", false);
405 
406 	std::string defbind = options->getString("defaultbind");
407 	if (stdalgo::string::equalsci(defbind, "ipv4"))
408 	{
409 		WildcardIPv6 = false;
410 	}
411 	else if (stdalgo::string::equalsci(defbind, "ipv6"))
412 	{
413 		WildcardIPv6 = true;
414 	}
415 	else
416 	{
417 		WildcardIPv6 = true;
418 		int socktest = socket(AF_INET6, SOCK_STREAM, 0);
419 		if (socktest < 0)
420 			WildcardIPv6 = false;
421 		else
422 			SocketEngine::Close(socktest);
423 	}
424 
425 	ReadXLine(this, "badip", "ipmask", ServerInstance->XLines->GetFactory("Z"));
426 	ReadXLine(this, "badnick", "nick", ServerInstance->XLines->GetFactory("Q"));
427 	ReadXLine(this, "badhost", "host", ServerInstance->XLines->GetFactory("K"));
428 	ReadXLine(this, "exception", "host", ServerInstance->XLines->GetFactory("E"));
429 
430 	const std::string restrictbannedusers = options->getString("restrictbannedusers", "yes", 1);
431 	if (stdalgo::string::equalsci(restrictbannedusers, "no"))
432 		RestrictBannedUsers = ServerConfig::BUT_NORMAL;
433 	else if (stdalgo::string::equalsci(restrictbannedusers, "silent"))
434 		RestrictBannedUsers = ServerConfig::BUT_RESTRICT_SILENT;
435 	else if (stdalgo::string::equalsci(restrictbannedusers, "yes"))
436 		RestrictBannedUsers =  ServerConfig::BUT_RESTRICT_NOTIFY;
437 	else
438 		throw CoreException(restrictbannedusers + " is an invalid <options:restrictbannedusers> value, at " + options->getTagLocation());
439 }
440 
441 // WARNING: it is not safe to use most of the codebase in this function, as it
442 // will run in the config reader thread
Read()443 void ServerConfig::Read()
444 {
445 	/* Load and parse the config file, if there are any errors then explode */
446 
447 	ParseStack stack(this);
448 	try
449 	{
450 		valid = stack.ParseFile(ServerInstance->ConfigFileName, 0);
451 	}
452 	catch (CoreException& err)
453 	{
454 		valid = false;
455 		errstr << err.GetReason() << std::endl;
456 	}
457 }
458 
Apply(ServerConfig * old,const std::string & useruid)459 void ServerConfig::Apply(ServerConfig* old, const std::string &useruid)
460 {
461 	valid = true;
462 	if (old)
463 	{
464 		/*
465 		 * These values can only be set on boot. Keep their old values. Do it before we send messages so we actually have a servername.
466 		 */
467 		this->CaseMapping = old->CaseMapping;
468 		this->ServerName = old->ServerName;
469 		this->sid = old->sid;
470 		this->cmdline = old->cmdline;
471 	}
472 
473 	/* The stuff in here may throw CoreException, be sure we're in a position to catch it. */
474 	try
475 	{
476 		// Ensure the user has actually edited ther config.
477 		ConfigTagList dietags = ConfTags("die");
478 		if (dietags.first != dietags.second)
479 		{
480 			errstr << "Your configuration has not been edited correctly!" << std::endl;
481 			for (ConfigIter iter = dietags.first; iter != dietags.second; ++iter)
482 			{
483 				ConfigTag* tag = iter->second;
484 				const std::string reason = tag->getString("reason", "You left a <die> tag in your config", 1);
485 				errstr << reason <<  " (at " << tag->getTagLocation() << ")" << std::endl;
486 			}
487 		}
488 
489 		Fill();
490 
491 		// Handle special items
492 		CrossCheckOperClassType();
493 		CrossCheckConnectBlocks(old);
494 	}
495 	catch (CoreException &ce)
496 	{
497 		errstr << ce.GetReason() << std::endl;
498 	}
499 
500 	// Check errors before dealing with failed binds, since continuing on failed bind is wanted in some circumstances.
501 	valid = errstr.str().empty();
502 
503 	// write once here, to try it out and make sure its ok
504 	if (valid)
505 		ServerInstance->WritePID(this->PID, !old);
506 
507 	ConfigTagList binds = ConfTags("bind");
508 	if (binds.first == binds.second)
509 		errstr << "Possible configuration error: you have not defined any <bind> blocks." << std::endl
510 			<< "You will need to do this if you want clients to be able to connect!" << std::endl;
511 
512 	if (old && valid)
513 	{
514 		// On first run, ports are bound later on
515 		FailedPortList pl;
516 		ServerInstance->BindPorts(pl);
517 		if (!pl.empty())
518 		{
519 			errstr << "Warning! Some of your listener" << (pl.size() == 1 ? "s" : "") << " failed to bind:" << std::endl;
520 			for (FailedPortList::const_iterator iter = pl.begin(); iter != pl.end(); ++iter)
521 			{
522 				const FailedPort& fp = *iter;
523 				errstr << "  " << fp.sa.str() << ": " << strerror(fp.error) << std::endl
524 					<< "  " << "Created from <bind> tag at " << fp.tag->getTagLocation() << std::endl;
525 			}
526 		}
527 	}
528 
529 	User* user = useruid.empty() ? NULL : ServerInstance->FindUUID(useruid);
530 
531 	if (!valid)
532 	{
533 		ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "There were errors in your configuration file:");
534 		Classes.clear();
535 	}
536 
537 	while (errstr.good())
538 	{
539 		std::string line;
540 		getline(errstr, line, '\n');
541 		if (line.empty())
542 			continue;
543 		// On startup, print out to console (still attached at this point)
544 		if (!old)
545 			std::cout << line << std::endl;
546 		// If a user is rehashing, tell them directly
547 		if (user)
548 			user->WriteRemoteNotice(InspIRCd::Format("*** %s", line.c_str()));
549 		// Also tell opers
550 		ServerInstance->SNO->WriteGlobalSno('a', line);
551 	}
552 
553 	errstr.clear();
554 	errstr.str(std::string());
555 
556 	/* No old configuration -> initial boot, nothing more to do here */
557 	if (!old)
558 	{
559 		if (!valid)
560 		{
561 			ServerInstance->Exit(EXIT_STATUS_CONFIG);
562 		}
563 
564 		return;
565 	}
566 
567 
568 	// If there were errors processing configuration, don't touch modules.
569 	if (!valid)
570 		return;
571 
572 	ApplyModules(user);
573 
574 	if (user)
575 		user->WriteRemoteNotice("*** Successfully rehashed server.");
576 	ServerInstance->SNO->WriteGlobalSno('a', "*** Successfully rehashed server.");
577 }
578 
ApplyModules(User * user)579 void ServerConfig::ApplyModules(User* user)
580 {
581 	std::vector<std::string> added_modules;
582 	ModuleManager::ModuleMap removed_modules = ServerInstance->Modules->GetModules();
583 
584 	ConfigTagList tags = ConfTags("module");
585 	for(ConfigIter i = tags.first; i != tags.second; ++i)
586 	{
587 		ConfigTag* tag = i->second;
588 		std::string name;
589 		if (tag->readString("name", name))
590 		{
591 			name = ModuleManager::ExpandModName(name);
592 			// if this module is already loaded, the erase will succeed, so we need do nothing
593 			// otherwise, we need to add the module (which will be done later)
594 			if (removed_modules.erase(name) == 0)
595 				added_modules.push_back(name);
596 		}
597 	}
598 
599 	for (ModuleManager::ModuleMap::iterator i = removed_modules.begin(); i != removed_modules.end(); ++i)
600 	{
601 		const std::string& modname = i->first;
602 		// Don't remove core_*.so, just remove m_*.so
603 		if (InspIRCd::Match(modname, "core_*.so", ascii_case_insensitive_map))
604 			continue;
605 		if (ServerInstance->Modules->Unload(i->second))
606 		{
607 			ServerInstance->SNO->WriteGlobalSno('a', "*** REHASH UNLOADED MODULE: %s", modname.c_str());
608 
609 			if (user)
610 				user->WriteNumeric(RPL_UNLOADEDMODULE, modname, InspIRCd::Format("The %s module was unloaded.", modname.c_str()));
611 			else
612 				ServerInstance->SNO->WriteGlobalSno('a', "The %s module was unloaded.", modname.c_str());
613 		}
614 		else
615 		{
616 			if (user)
617 				user->WriteNumeric(ERR_CANTUNLOADMODULE, modname, InspIRCd::Format("Failed to unload the %s module: %s", modname.c_str(), ServerInstance->Modules->LastError().c_str()));
618 			else
619 				ServerInstance->SNO->WriteGlobalSno('a', "Failed to unload the %s module: %s", modname.c_str(), ServerInstance->Modules->LastError().c_str());
620 		}
621 	}
622 
623 	for (std::vector<std::string>::iterator adding = added_modules.begin(); adding != added_modules.end(); adding++)
624 	{
625 		// Skip modules which are already loaded.
626 		if (ServerInstance->Modules->Find(*adding))
627 			continue;
628 
629 		if (ServerInstance->Modules->Load(*adding))
630 		{
631 			ServerInstance->SNO->WriteGlobalSno('a', "*** REHASH LOADED MODULE: %s",adding->c_str());
632 			if (user)
633 				user->WriteNumeric(RPL_LOADEDMODULE, *adding, InspIRCd::Format("The %s module was loaded.", adding->c_str()));
634 			else
635 				ServerInstance->SNO->WriteGlobalSno('a', "The %s module was loaded.", adding->c_str());
636 		}
637 		else
638 		{
639 			if (user)
640 				user->WriteNumeric(ERR_CANTLOADMODULE, *adding, InspIRCd::Format("Failed to load the %s module: %s", adding->c_str(), ServerInstance->Modules->LastError().c_str()));
641 			else
642 				ServerInstance->SNO->WriteGlobalSno('a', "Failed to load the %s module: %s", adding->c_str(), ServerInstance->Modules->LastError().c_str());
643 		}
644 	}
645 }
646 
ConfValue(const std::string & tag)647 ConfigTag* ServerConfig::ConfValue(const std::string &tag)
648 {
649 	ConfigTagList found = config_data.equal_range(tag);
650 	if (found.first == found.second)
651 		return EmptyTag;
652 	ConfigTag* rv = found.first->second;
653 	found.first++;
654 	if (found.first != found.second)
655 		ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "Multiple <" + tag + "> tags found; only first will be used "
656 			"(first at " + rv->getTagLocation() + "; second at " + found.first->second->getTagLocation() + ")");
657 	return rv;
658 }
659 
ConfTags(const std::string & tag)660 ConfigTagList ServerConfig::ConfTags(const std::string& tag)
661 {
662 	return config_data.equal_range(tag);
663 }
664 
Escape(const std::string & str,bool xml)665 std::string ServerConfig::Escape(const std::string& str, bool xml)
666 {
667 	std::string escaped;
668 	for (std::string::const_iterator it = str.begin(); it != str.end(); ++it)
669 	{
670 		switch (*it)
671 		{
672 			case '"':
673 				escaped += xml ? "&quot;" : "\"";
674 				break;
675 			case '&':
676 				escaped += xml ? "&amp;" : "&";
677 				break;
678 			case '\\':
679 				escaped += xml ? "\\" : "\\\\";
680 				break;
681 			default:
682 				escaped += *it;
683 				break;
684 		}
685 	}
686 	return escaped;
687 }
688 
Run()689 void ConfigReaderThread::Run()
690 {
691 	Config->Read();
692 	done = true;
693 }
694 
Finish()695 void ConfigReaderThread::Finish()
696 {
697 	ServerConfig* old = ServerInstance->Config;
698 	ServerInstance->Logs->Log("CONFIG", LOG_DEBUG, "Switching to new configuration...");
699 	ServerInstance->Config = this->Config;
700 	Config->Apply(old, TheUserUID);
701 
702 	if (Config->valid)
703 	{
704 		/*
705 		 * Apply the changed configuration from the rehash.
706 		 *
707 		 * XXX: The order of these is IMPORTANT, do not reorder them without testing
708 		 * thoroughly!!!
709 		 */
710 		ServerInstance->Users.RehashCloneCounts();
711 		ServerInstance->XLines->CheckELines();
712 		ServerInstance->XLines->ApplyLines();
713 		User* user = ServerInstance->FindUUID(TheUserUID);
714 
715 		ConfigStatus status(user);
716 		const ModuleManager::ModuleMap& mods = ServerInstance->Modules->GetModules();
717 		for (ModuleManager::ModuleMap::const_iterator i = mods.begin(); i != mods.end(); ++i)
718 		{
719 			try
720 			{
721 				ServerInstance->Logs->Log("MODULE", LOG_DEBUG, "Rehashing " + i->first);
722 				i->second->ReadConfig(status);
723 			}
724 			catch (CoreException& modex)
725 			{
726 				ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "Exception caught: " + modex.GetReason());
727 				if (user)
728 					user->WriteNotice(i->first + ": " + modex.GetReason());
729 			}
730 		}
731 
732 		// The description of this server may have changed - update it for WHOIS etc.
733 		ServerInstance->FakeClient->server->description = Config->ServerDesc;
734 
735 		ServerInstance->ISupport.Build();
736 
737 		ServerInstance->Logs->CloseLogs();
738 		ServerInstance->Logs->OpenFileLogs();
739 
740 		if (Config->RawLog && !old->RawLog)
741 			ServerInstance->Users->ServerNoticeAll("*** Raw I/O logging is enabled on this server. All messages, passwords, and commands are being recorded.");
742 
743 		Config = old;
744 	}
745 	else
746 	{
747 		// whoops, abort!
748 		ServerInstance->Config = old;
749 	}
750 }
751