2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2019 Matt Schatz <genius3000@g3k.solutions>
5 * Copyright (C) 2018 Chris Novakovic <chrisnovakovic@users.noreply.github.com>
6 * Copyright (C) 2013-2014, 2016 Attila Molnar <attilamolnar@hush.com>
7 * Copyright (C) 2013 Daniel Vassdal <shutter@canternet.org>
8 * Copyright (C) 2012-2014, 2016-2020 Sadie Powell <sadie@witchery.services>
9 * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
10 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
11 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
12 * Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
13 * Copyright (C) 2007-2008 Craig Edwards <brain@inspircd.org>
14 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
16 * This file is part of InspIRCd. InspIRCd is free software: you can
17 * redistribute it and/or modify it under the terms of the GNU General Public
18 * License as published by the Free Software Foundation, version 2.
20 * This program is distributed in the hope that it will be useful, but WITHOUT
21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 #include "socketengine.h"
40 #include "token_list.h"
42 /** Structure representing a single \<tag> in config */
43 class CoreExport ConfigTag : public refcountbase
47 const std::string tag;
48 const std::string src_name;
51 /** Get the value of an option, using def if it does not exist */
52 std::string getString(const std::string& key, const std::string& def, const TR1NS::function<bool(const std::string&)>& validator);
53 /** Get the value of an option, using def if it does not exist */
54 std::string getString(const std::string& key, const std::string& def = "", size_t minlen = 0, size_t maxlen = UINT32_MAX);
55 /** Get the value of an option, using def if it does not exist */
56 long getInt(const std::string& key, long def, long min = LONG_MIN, long max = LONG_MAX);
57 /** Get the value of an option, using def if it does not exist */
58 unsigned long getUInt(const std::string& key, unsigned long def, unsigned long min = 0, unsigned long max = ULONG_MAX);
59 /** Get the value of an option, using def if it does not exist */
60 double getFloat(const std::string& key, double def, double min = DBL_MIN, double max = DBL_MAX);
61 /** Get the value of an option, using def if it does not exist */
62 bool getBool(const std::string& key, bool def = false);
64 /** Get the value in seconds of a duration that is in the user-friendly "1h2m3s" format,
65 * using a default value if it does not exist or is out of bounds.
66 * @param key The config key name
67 * @param def Default value (optional)
68 * @param min Minimum acceptable value (optional)
69 * @param max Maximum acceptable value (optional)
70 * @return The duration in seconds
72 unsigned long getDuration(const std::string& key, unsigned long def, unsigned long min = 0, unsigned long max = ULONG_MAX);
74 /** Get the value of an option
75 * @param key The option to get
76 * @param value The location to store the value (unmodified if does not exist)
77 * @param allow_newline Allow newlines in the option (normally replaced with spaces)
78 * @return true if the option exists
80 bool readString(const std::string& key, std::string& value, bool allow_newline = false);
82 std::string getTagLocation();
84 inline const ConfigItems& getItems() const { return items; }
86 /** Create a new ConfigTag, giving access to the private ConfigItems item list */
87 static ConfigTag* create(const std::string& Tag, const std::string& file, int line, ConfigItems*& Items);
89 ConfigTag(const std::string& Tag, const std::string& file, int line);
92 /** Defines the server's length limits on various length-limited
93 * items such as topics, nicknames, channel names etc.
98 /** Maximum line length */
100 /** Maximum nickname length */
102 /** Maximum channel length */
104 /** Maximum number of modes per line */
106 /** Maximum length of ident, not including ~ etc */
108 /** Maximum length of a quit message */
110 /** Maximum topic length */
112 /** Maximum kick message length */
114 /** Maximum real name length */
116 /** Maximum away message length */
118 /** Maximum hostname length */
121 /** Read all limits from a config tag. Limits which aren't specified in the tag are set to a default value.
122 * @param tag Configuration tag to read the limits from
124 ServerLimits(ConfigTag* tag);
126 /** Maximum length of a n!u\@h mask */
127 size_t GetMaxMask() const { return NickMax + 1 + IdentMax + 1 + MaxHost; }
130 struct CommandLineConf
132 /** If this value is true, the owner of the
133 * server specified -nofork on the command
134 * line, causing the daemon to stay in the
139 /** If this value if true then all log
140 * messages will be output, regardless of
141 * the level given in the config file.
142 * This is set with the -debug commandline
147 /** If this is true then log output will be
148 * written to the logfile. This is the default.
149 * If you put -nolog on the commandline then
150 * the logfile will not be written.
151 * This is meant to be used in conjunction with
152 * -debug for debugging without filling up the
157 /** If this is true, a PID file will be written
158 * to the file given in the "file" variable of
159 * the \<pid> tag in the config file. This is
161 * Passing --nopid as a command line argument
162 * sets this to false; in this case, a PID file
163 * will not be written, even the default PID
164 * file that is usually written when the \<pid>
165 * tag is not defined in the config file.
169 /* Whether the --runasroot option was specified at boot. */
172 /** Saved argc from startup. */
175 /** Saved argv from startup. */
179 class CoreExport OperInfo : public refcountbase
182 TokenList AllowedOperCommands;
183 TokenList AllowedPrivs;
185 /** Allowed user modes from oper classes. */
186 std::bitset<64> AllowedUserModes;
188 /** Allowed channel modes from oper classes. */
189 std::bitset<64> AllowedChanModes;
191 /** Allowed snomasks from oper classes. */
192 std::bitset<64> AllowedSnomasks;
194 /** \<oper> block used for this oper-up. May be NULL. */
195 reference<ConfigTag> oper_block;
196 /** \<type> block used for this oper-up. Valid for local users, may be NULL on remote */
197 reference<ConfigTag> type_block;
198 /** \<class> blocks referenced from the \<type> block. These define individual permissions */
199 std::vector<reference<ConfigTag> > class_blocks;
200 /** Name of the oper type; i.e. the one shown in WHOIS */
203 /** Creates a new OperInfo with the specified oper type name.
204 * @param Name The name of the oper type.
206 OperInfo(const std::string& Name);
208 /** Get a configuration item, searching in the oper, type, and class blocks (in that order) */
209 std::string getConfig(const std::string& key);
213 /** This class holds the bulk of the runtime configuration for the ircd.
214 * It allows for reading new config values, accessing configuration files,
215 * and storage of the configuration data needed to run the ircd, such as
216 * the servername, connect classes, /ADMIN data, MOTDs and filenames etc.
218 class CoreExport ServerConfig
221 void CrossCheckOperClassType();
222 void CrossCheckConnectBlocks(ServerConfig* current);
225 /** How to treat a user in a channel who is banned. */
226 enum BannedUserTreatment
228 /** Don't treat a banned user any different to normal. */
231 /** Restrict the actions of a banned user. */
234 /** Restrict the actions of a banned user and notify them of their treatment. */
253 ServerPaths(ConfigTag* tag);
255 std::string PrependConfig(const std::string& fn) const { return FileSystem::ExpandPath(Config, fn); }
256 std::string PrependData(const std::string& fn) const { return FileSystem::ExpandPath(Data, fn); }
257 std::string PrependLog(const std::string& fn) const { return FileSystem::ExpandPath(Log, fn); }
258 std::string PrependModule(const std::string& fn) const { return FileSystem::ExpandPath(Module, fn); }
261 /** Holds a complete list of all connect blocks
263 typedef std::vector<reference<ConnectClass> > ClassVector;
265 /** Index of valid oper blocks and types
267 typedef insp::flat_map<std::string, reference<OperInfo> > OperIndex;
269 /** Get a configuration tag by name. If one or more tags are present then the first is returned.
270 * @param tag The name of the tag to get.
271 * @returns Either a tag from the config or EmptyTag.
273 ConfigTag* ConfValue(const std::string& tag);
275 /** Get a list of configuration tags by name.
276 * @param tag The name of the tags to get.
277 * @returns Either a list of tags from the config or an empty ConfigTagList.
279 ConfigTagList ConfTags(const std::string& tag);
281 /** An empty configuration tag. */
284 /** Error stream, contains error output from any failed configuration parsing.
286 std::stringstream errstr;
288 /** True if this configuration is valid enough to run with */
291 /** Bind to IPv6 by default */
294 /** This holds all the information in the config file,
295 * it's indexed by tag name to a vector of key/values.
297 ConfigDataHash config_data;
299 /** This holds all extra files that have been read in the configuration
300 * (for example, MOTD and RULES files are stored here)
302 ConfigFileCache Files;
304 /** Length limits, see definition of ServerLimits class
308 /** Locations of various types of file (config, module, etc). */
311 /** Configuration parsed from the command line.
313 CommandLineConf cmdline;
315 /** Clones CIDR range for ipv4 (0-32)
316 * Defaults to 32 (checks clones on all IPs separately)
318 unsigned char c_ipv4_range;
320 /** Clones CIDR range for ipv6 (0-128)
321 * Defaults to 128 (checks on all IPs separately)
323 unsigned char c_ipv6_range;
325 /** Holds the server name of the local server
326 * as defined by the administrator.
328 std::string ServerName;
330 /** Notice to give to users when they are banned by an XLine
332 std::string XLineMessage;
334 /* Holds the network name the local server
335 * belongs to. This is an arbitrary field defined
336 * by the administrator.
340 /** Holds the description of the local server
341 * as defined by the administrator.
343 std::string ServerDesc;
345 /** How to treat a user in a channel who is banned. */
346 BannedUserTreatment RestrictBannedUsers;
348 /** The size of the read() buffer in the user
349 * handling code, used to read data into a user's
354 /** The value to be used for listen() backlogs
359 /** If we should check for clones during CheckClass() in AddUser()
360 * Setting this to false allows to not trigger on maxclones for users
361 * that may belong to another class after DNS-lookup is complete.
362 * It does, however, make the server spend more time on users we may potentially not want.
366 /** The soft limit value assigned to the irc server.
367 * The IRC server will not allow more than this
368 * number of local users.
370 unsigned int SoftLimit;
372 /** Maximum number of targets for a multi target command
373 * such as PRIVMSG or KICK
375 unsigned int MaxTargets;
377 /** The number of seconds that the server clock can skip by before server operators are warned. */
380 /** True if we're going to hide ban reasons for non-opers (e.g. G-lines,
385 /** True if raw I/O is being logged */
388 /** Set to a non-empty string to obfuscate server names. */
389 std::string HideServer;
391 /** The full pathname and filename of the PID
392 * file as defined in the configuration.
396 /** The connect classes in use by the IRC server.
400 /** Default channel modes
402 std::string DefaultModes;
404 /** Custom version string, which if defined can replace the system info in VERSION.
406 std::string CustomVersion;
408 /** If set to true, provide syntax hints for unknown commands
412 /** The name of the casemapping method used by this server.
414 std::string CaseMapping;
416 /** If set to true, the full nick!user\@host will be shown in the TOPIC command
417 * for who set the topic last. If false, only the nick is shown.
419 bool FullHostInTopic;
421 /** Oper blocks keyed by their name
423 OperIndex oper_blocks;
425 /** Oper types keyed by their name
429 /** Default value for <connect:maxchans>, deprecated in 3.0
431 unsigned int MaxChans;
433 /** Default value for <oper:maxchans>, deprecated in 3.0
435 unsigned int OperMaxChans;
437 /** TS6-like server ID.
438 * NOTE: 000...999 are usable for InspIRCd servers. This
439 * makes code simpler. 0AA, 1BB etc with letters are reserved
444 /** Construct a new ServerConfig
450 /** Get server ID as string with required leading zeroes
452 const std::string& GetSID() const { return sid; }
454 /** Retrieves the server name which should be shown to users. */
455 const std::string& GetServerName() const { return HideServer.empty() ? ServerName : HideServer; }
457 /** Retrieves the server description which should be shown to users. */
458 const std::string& GetServerDesc() const { return HideServer.empty() ? ServerDesc : HideServer; }
460 /** Read the entire configuration into memory
461 * and initialize this class. All other methods
462 * should be used only by the core.
466 /** Apply configuration changes from the old configuration.
468 void Apply(ServerConfig* old, const std::string &useruid);
469 void ApplyModules(User* user);
473 /** Escapes a value for storage in a configuration key.
474 * @param str The string to escape.
475 * @param xml Are we using the XML config format?
477 static std::string Escape(const std::string& str, bool xml = true);
479 /** If this value is true, snotices will not stack when repeats are sent
484 /** The background thread for config reading, so that reading from executable includes
487 class CoreExport ConfigReaderThread : public Thread
489 ServerConfig* Config;
492 const std::string TheUserUID;
493 ConfigReaderThread(const std::string &useruid)
494 : Config(new ServerConfig), done(false), TheUserUID(useruid)
498 virtual ~ConfigReaderThread()
503 void Run() CXX11_OVERRIDE;
504 /** Run in the main thread to apply the configuration */
506 bool IsDone() { return done; }
509 /** Represents the status of a config load. */
510 class CoreExport ConfigStatus
513 /** Whether this is the initial config load. */
516 /** The user who initiated the config load or NULL if not initiated by a user. */
519 /** Initializes a new instance of the ConfigStatus class.
520 * @param user The user who initiated the config load or NULL if not initiated by a user.
521 * @param isinitial Whether this is the initial config load.
523 ConfigStatus(User* user = NULL, bool isinitial = false)