2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2007, 2009 Dennis Friis <peavey@inspircd.org>
6 * Copyright (C) 2006-2008 Craig Edwards <craigedwards@brainbox.cc>
7 * Copyright (C) 2006-2008 Robin Burchell <robin+git@viroteck.net>
8 * Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
10 * This file is part of InspIRCd. InspIRCd is free software: you can
11 * redistribute it and/or modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation, version 2.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "socketengine.h"
34 #include "token_list.h"
36 /** Structure representing a single \<tag> in config */
37 class CoreExport ConfigTag : public refcountbase
41 const std::string tag;
42 const std::string src_name;
45 /** Get the value of an option, using def if it does not exist */
46 std::string getString(const std::string& key, const std::string& def, const TR1NS::function<bool(const std::string&)>& validator);
47 /** Get the value of an option, using def if it does not exist */
48 std::string getString(const std::string& key, const std::string& def = "", size_t minlen = 0, size_t maxlen = UINT32_MAX);
49 /** Get the value of an option, using def if it does not exist */
50 long getInt(const std::string& key, long def, long min = LONG_MIN, long max = LONG_MAX);
51 /** Get the value of an option, using def if it does not exist */
52 unsigned long getUInt(const std::string& key, unsigned long def, unsigned long min = 0, unsigned long max = ULONG_MAX);
53 /** Get the value of an option, using def if it does not exist */
54 double getFloat(const std::string& key, double def, double min = DBL_MIN, double max = DBL_MAX);
55 /** Get the value of an option, using def if it does not exist */
56 bool getBool(const std::string& key, bool def = false);
58 /** Get the value in seconds of a duration that is in the user-friendly "1h2m3s" format,
59 * using a default value if it does not exist or is out of bounds.
60 * @param key The config key name
61 * @param def Default value (optional)
62 * @param min Minimum acceptable value (optional)
63 * @param max Maximum acceptable value (optional)
64 * @return The duration in seconds
66 unsigned long getDuration(const std::string& key, unsigned long def, unsigned long min = 0, unsigned long max = ULONG_MAX);
68 /** Get the value of an option
69 * @param key The option to get
70 * @param value The location to store the value (unmodified if does not exist)
71 * @param allow_newline Allow newlines in the option (normally replaced with spaces)
72 * @return true if the option exists
74 bool readString(const std::string& key, std::string& value, bool allow_newline = false);
76 std::string getTagLocation();
78 inline const ConfigItems& getItems() const { return items; }
80 /** Create a new ConfigTag, giving access to the private ConfigItems item list */
81 static ConfigTag* create(const std::string& Tag, const std::string& file, int line, ConfigItems*& Items);
83 ConfigTag(const std::string& Tag, const std::string& file, int line);
86 /** Defines the server's length limits on various length-limited
87 * items such as topics, nicknames, channel names etc.
92 /** Maximum nickname length */
94 /** Maximum channel length */
96 /** Maximum number of modes per line */
98 /** Maximum length of ident, not including ~ etc */
100 /** Maximum length of a quit message */
102 /** Maximum topic length */
104 /** Maximum kick message length */
106 /** Maximum real name length */
108 /** Maximum away message length */
110 /** Maximum line length */
112 /** Maximum hostname length */
115 /** Read all limits from a config tag. Limits which aren't specified in the tag are set to a default value.
116 * @param tag Configuration tag to read the limits from
118 ServerLimits(ConfigTag* tag);
120 /** Maximum length of a n!u\@h mask */
121 size_t GetMaxMask() const { return NickMax + 1 + IdentMax + 1 + MaxHost; }
124 struct CommandLineConf
126 /** If this value is true, the owner of the
127 * server specified -nofork on the command
128 * line, causing the daemon to stay in the
133 /** If this value if true then all log
134 * messages will be output, regardless of
135 * the level given in the config file.
136 * This is set with the -debug commandline
141 /** If this is true then log output will be
142 * written to the logfile. This is the default.
143 * If you put -nolog on the commandline then
144 * the logfile will not be written.
145 * This is meant to be used in conjunction with
146 * -debug for debugging without filling up the
151 /** If this is true, a PID file will be written
152 * to the file given in the "file" variable of
153 * the \<pid> tag in the config file. This is
155 * Passing --nopid as a command line argument
156 * sets this to false; in this case, a PID file
157 * will not be written, even the default PID
158 * file that is usually written when the \<pid>
159 * tag is not defined in the config file.
163 /* Whether the --runasroot option was specified at boot. */
166 /** Saved argc from startup. */
169 /** Saved argv from startup. */
173 class CoreExport OperInfo : public refcountbase
176 TokenList AllowedOperCommands;
177 TokenList AllowedPrivs;
179 /** Allowed user modes from oper classes. */
180 std::bitset<64> AllowedUserModes;
182 /** Allowed channel modes from oper classes. */
183 std::bitset<64> AllowedChanModes;
185 /** \<oper> block used for this oper-up. May be NULL. */
186 reference<ConfigTag> oper_block;
187 /** \<type> block used for this oper-up. Valid for local users, may be NULL on remote */
188 reference<ConfigTag> type_block;
189 /** \<class> blocks referenced from the \<type> block. These define individual permissions */
190 std::vector<reference<ConfigTag> > class_blocks;
191 /** Name of the oper type; i.e. the one shown in WHOIS */
194 /** Creates a new OperInfo with the specified oper type name.
195 * @param Name The name of the oper type.
197 OperInfo(const std::string& Name);
199 /** Get a configuration item, searching in the oper, type, and class blocks (in that order) */
200 std::string getConfig(const std::string& key);
204 /** This class holds the bulk of the runtime configuration for the ircd.
205 * It allows for reading new config values, accessing configuration files,
206 * and storage of the configuration data needed to run the ircd, such as
207 * the servername, connect classes, /ADMIN data, MOTDs and filenames etc.
209 class CoreExport ServerConfig
212 void CrossCheckOperClassType();
213 void CrossCheckConnectBlocks(ServerConfig* current);
216 /** How to treat a user in a channel who is banned. */
217 enum BannedUserTreatment
219 /** Don't treat a banned user any different to normal. */
222 /** Restrict the actions of a banned user. */
225 /** Restrict the actions of a banned user and notify them of their treatment. */
244 ServerPaths(ConfigTag* tag);
246 std::string PrependConfig(const std::string& fn) const { return FileSystem::ExpandPath(Config, fn); }
247 std::string PrependData(const std::string& fn) const { return FileSystem::ExpandPath(Data, fn); }
248 std::string PrependLog(const std::string& fn) const { return FileSystem::ExpandPath(Log, fn); }
249 std::string PrependModule(const std::string& fn) const { return FileSystem::ExpandPath(Module, fn); }
252 /** Holds a complete list of all connect blocks
254 typedef std::vector<reference<ConnectClass> > ClassVector;
256 /** Index of valid oper blocks and types
258 typedef insp::flat_map<std::string, reference<OperInfo> > OperIndex;
260 /** Get a configuration tag by name. If one or more tags are present then the first is returned.
261 * @param tag The name of the tag to get.
262 * @returns Either a tag from the config or EmptyTag.
264 ConfigTag* ConfValue(const std::string& tag);
266 /** Get a list of configuration tags by name.
267 * @param tag The name of the tags to get.
268 * @returns Either a list of tags from the config or an empty ConfigTagList.
270 ConfigTagList ConfTags(const std::string& tag);
272 /** An empty configuration tag. */
275 /** Error stream, contains error output from any failed configuration parsing.
277 std::stringstream errstr;
279 /** True if this configuration is valid enough to run with */
282 /** Bind to IPv6 by default */
285 /** This holds all the information in the config file,
286 * it's indexed by tag name to a vector of key/values.
288 ConfigDataHash config_data;
290 /** This holds all extra files that have been read in the configuration
291 * (for example, MOTD and RULES files are stored here)
293 ConfigFileCache Files;
295 /** Length limits, see definition of ServerLimits class
299 /** Locations of various types of file (config, module, etc). */
302 /** Configuration parsed from the command line.
304 CommandLineConf cmdline;
306 /** Clones CIDR range for ipv4 (0-32)
307 * Defaults to 32 (checks clones on all IPs seperately)
309 unsigned char c_ipv4_range;
311 /** Clones CIDR range for ipv6 (0-128)
312 * Defaults to 128 (checks on all IPs seperately)
314 unsigned char c_ipv6_range;
316 /** Holds the server name of the local server
317 * as defined by the administrator.
319 std::string ServerName;
321 /** Notice to give to users when they are banned by an XLine
323 std::string XLineMessage;
325 /* Holds the network name the local server
326 * belongs to. This is an arbitary field defined
327 * by the administrator.
331 /** Holds the description of the local server
332 * as defined by the administrator.
334 std::string ServerDesc;
336 /** How to treat a user in a channel who is banned. */
337 BannedUserTreatment RestrictBannedUsers;
339 /** The size of the read() buffer in the user
340 * handling code, used to read data into a user's
345 /** The value to be used for listen() backlogs
350 /** If we should check for clones during CheckClass() in AddUser()
351 * Setting this to false allows to not trigger on maxclones for users
352 * that may belong to another class after DNS-lookup is complete.
353 * It does, however, make the server spend more time on users we may potentially not want.
357 /** The soft limit value assigned to the irc server.
358 * The IRC server will not allow more than this
359 * number of local users.
361 unsigned int SoftLimit;
363 /** Maximum number of targets for a multi target command
364 * such as PRIVMSG or KICK
366 unsigned int MaxTargets;
368 /** The number of seconds that the server clock can skip by before server operators are warned. */
371 /** True if we're going to hide ban reasons for non-opers (e.g. G-lines,
376 /** True if raw I/O is being logged */
379 /** Set to a non-empty string to obfuscate server names. */
380 std::string HideServer;
382 /** The full pathname and filename of the PID
383 * file as defined in the configuration.
387 /** The connect classes in use by the IRC server.
391 /** Default channel modes
393 std::string DefaultModes;
395 /** Custom version string, which if defined can replace the system info in VERSION.
397 std::string CustomVersion;
399 /** If set to true, provide syntax hints for unknown commands
403 /** The name of the casemapping method used by this server.
405 std::string CaseMapping;
407 /** If set to true, the full nick!user\@host will be shown in the TOPIC command
408 * for who set the topic last. If false, only the nick is shown.
410 bool FullHostInTopic;
412 /** Oper blocks keyed by their name
414 OperIndex oper_blocks;
416 /** Oper types keyed by their name
420 /** Default value for <connect:maxchans>, deprecated in 3.0
422 unsigned int MaxChans;
424 /** Default value for <oper:maxchans>, deprecated in 3.0
426 unsigned int OperMaxChans;
428 /** TS6-like server ID.
429 * NOTE: 000...999 are usable for InspIRCd servers. This
430 * makes code simpler. 0AA, 1BB etc with letters are reserved
435 /** Construct a new ServerConfig
441 /** Get server ID as string with required leading zeroes
443 const std::string& GetSID() const { return sid; }
445 /** Read the entire configuration into memory
446 * and initialize this class. All other methods
447 * should be used only by the core.
451 /** Apply configuration changes from the old configuration.
453 void Apply(ServerConfig* old, const std::string &useruid);
454 void ApplyModules(User* user);
458 /** Escapes a value for storage in a configuration key.
459 * @param str The string to escape.
460 * @param xml Are we using the XML config format?
462 static std::string Escape(const std::string& str, bool xml = true);
464 /** If this value is true, snotices will not stack when repeats are sent
469 /** The background thread for config reading, so that reading from executable includes
472 class CoreExport ConfigReaderThread : public Thread
474 ServerConfig* Config;
477 const std::string TheUserUID;
478 ConfigReaderThread(const std::string &useruid)
479 : Config(new ServerConfig), done(false), TheUserUID(useruid)
483 virtual ~ConfigReaderThread()
488 void Run() CXX11_OVERRIDE;
489 /** Run in the main thread to apply the configuration */
491 bool IsDone() { return done; }
494 /** Represents the status of a config load. */
495 class CoreExport ConfigStatus
498 /** Whether this is the initial config load. */
501 /** The user who initiated the config load or NULL if not initiated by a user. */
504 /** Initializes a new instance of the ConfigStatus class.
505 * @param user The user who initiated the config load or NULL if not initiated by a user.
506 * @param isinitial Whether this is the initial config load.
508 ConfigStatus(User* user = NULL, bool isinitial = false)