]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/configreader.h
Add range checking to ConfigTag::getFloat.
[user/henk/code/inspircd.git] / include / configreader.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
9  *
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.
13  *
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
17  * details.
18  *
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/>.
21  */
22
23
24 #pragma once
25
26 #include <sstream>
27 #include <string>
28 #include <vector>
29 #include <map>
30 #include "inspircd.h"
31 #include "modules.h"
32 #include "socketengine.h"
33 #include "socket.h"
34 #include "token_list.h"
35
36 /** Structure representing a single \<tag> in config */
37 class CoreExport ConfigTag : public refcountbase
38 {
39         ConfigItems items;
40  public:
41         const std::string tag;
42         const std::string src_name;
43         const int src_line;
44
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 = "", size_t minlen = 0, size_t maxlen = UINT32_MAX);
47         /** Get the value of an option, using def if it does not exist */
48         long getInt(const std::string& key, long def, long min = LONG_MIN, long max = LONG_MAX);
49         /** Get the value of an option, using def if it does not exist */
50         unsigned long getUInt(const std::string& key, unsigned long def, unsigned long min = 0, unsigned long max = ULONG_MAX);
51         /** Get the value of an option, using def if it does not exist */
52         double getFloat(const std::string& key, double def, double min = DBL_MIN, double max = DBL_MAX);
53         /** Get the value of an option, using def if it does not exist */
54         bool getBool(const std::string& key, bool def = false);
55
56         /** Get the value in seconds of a duration that is in the user-friendly "1h2m3s" format,
57          * using a default value if it does not exist or is out of bounds.
58          * @param key The config key name
59          * @param def Default value (optional)
60          * @param min Minimum acceptable value (optional)
61          * @param max Maximum acceptable value (optional)
62          * @return The duration in seconds
63          */
64         unsigned long getDuration(const std::string& key, unsigned long def, unsigned long min = 0, unsigned long max = ULONG_MAX);
65
66         /** Get the value of an option
67          * @param key The option to get
68          * @param value The location to store the value (unmodified if does not exist)
69          * @param allow_newline Allow newlines in the option (normally replaced with spaces)
70          * @return true if the option exists
71          */
72         bool readString(const std::string& key, std::string& value, bool allow_newline = false);
73
74         std::string getTagLocation();
75
76         inline const ConfigItems& getItems() const { return items; }
77
78         /** Create a new ConfigTag, giving access to the private ConfigItems item list */
79         static ConfigTag* create(const std::string& Tag, const std::string& file, int line, ConfigItems*& Items);
80  private:
81         ConfigTag(const std::string& Tag, const std::string& file, int line);
82 };
83
84 /** Defines the server's length limits on various length-limited
85  * items such as topics, nicknames, channel names etc.
86  */
87 class ServerLimits
88 {
89  public:
90         /** Maximum nickname length */
91         size_t NickMax;
92         /** Maximum channel length */
93         size_t ChanMax;
94         /** Maximum number of modes per line */
95         size_t MaxModes;
96         /** Maximum length of ident, not including ~ etc */
97         size_t IdentMax;
98         /** Maximum length of a quit message */
99         size_t MaxQuit;
100         /** Maximum topic length */
101         size_t MaxTopic;
102         /** Maximum kick message length */
103         size_t MaxKick;
104         /** Maximum GECOS (real name) length */
105         size_t MaxGecos;
106         /** Maximum away message length */
107         size_t MaxAway;
108         /** Maximum line length */
109         size_t MaxLine;
110         /** Maximum hostname length */
111         size_t MaxHost;
112
113         /** Read all limits from a config tag. Limits which aren't specified in the tag are set to a default value.
114          * @param tag Configuration tag to read the limits from
115          */
116         ServerLimits(ConfigTag* tag);
117
118         /** Maximum length of a n!u\@h mask */
119         size_t GetMaxMask() const { return NickMax + 1 + IdentMax + 1 + MaxHost; }
120 };
121
122 struct CommandLineConf
123 {
124         /** If this value is true, the owner of the
125          * server specified -nofork on the command
126          * line, causing the daemon to stay in the
127          * foreground.
128          */
129         bool nofork;
130
131         /** If this value if true then all log
132          * messages will be output, regardless of
133          * the level given in the config file.
134          * This is set with the -debug commandline
135          * option.
136          */
137         bool forcedebug;
138
139         /** If this is true then log output will be
140          * written to the logfile. This is the default.
141          * If you put -nolog on the commandline then
142          * the logfile will not be written.
143          * This is meant to be used in conjunction with
144          * -debug for debugging without filling up the
145          * hard disk.
146          */
147         bool writelog;
148
149         /** Saved argc from startup
150          */
151         int argc;
152
153         /** Saved argv from startup
154          */
155         char** argv;
156 };
157
158 class CoreExport OperInfo : public refcountbase
159 {
160  public:
161         TokenList AllowedOperCommands;
162         TokenList AllowedPrivs;
163
164         /** Allowed user modes from oper classes. */
165         std::bitset<64> AllowedUserModes;
166
167         /** Allowed channel modes from oper classes. */
168         std::bitset<64> AllowedChanModes;
169
170         /** \<oper> block used for this oper-up. May be NULL. */
171         reference<ConfigTag> oper_block;
172         /** \<type> block used for this oper-up. Valid for local users, may be NULL on remote */
173         reference<ConfigTag> type_block;
174         /** \<class> blocks referenced from the \<type> block. These define individual permissions */
175         std::vector<reference<ConfigTag> > class_blocks;
176         /** Name of the oper type; i.e. the one shown in WHOIS */
177         std::string name;
178
179         /** Creates a new OperInfo with the specified oper type name.
180          * @param Name The name of the oper type.
181          */
182         OperInfo(const std::string& Name);
183
184         /** Get a configuration item, searching in the oper, type, and class blocks (in that order) */
185         std::string getConfig(const std::string& key);
186         void init();
187 };
188
189 /** This class holds the bulk of the runtime configuration for the ircd.
190  * It allows for reading new config values, accessing configuration files,
191  * and storage of the configuration data needed to run the ircd, such as
192  * the servername, connect classes, /ADMIN data, MOTDs and filenames etc.
193  */
194 class CoreExport ServerConfig
195 {
196   private:
197         void CrossCheckOperClassType();
198         void CrossCheckConnectBlocks(ServerConfig* current);
199
200  public:
201         class ServerPaths
202         {
203          public:
204                 /** Config path */
205                 std::string Config;
206
207                 /** Data path */
208                 std::string Data;
209
210                 /** Log path */
211                 std::string Log;
212
213                 /** Module path */
214                 std::string Module;
215
216                 ServerPaths(ConfigTag* tag);
217
218                 std::string PrependConfig(const std::string& fn) const { return FileSystem::ExpandPath(Config, fn); }
219                 std::string PrependData(const std::string& fn) const { return FileSystem::ExpandPath(Data, fn); }
220                 std::string PrependLog(const std::string& fn) const { return FileSystem::ExpandPath(Log, fn); }
221                 std::string PrependModule(const std::string& fn) const { return FileSystem::ExpandPath(Module, fn); }
222         };
223
224         /** Holds a complete list of all connect blocks
225          */
226         typedef std::vector<reference<ConnectClass> > ClassVector;
227
228         /** Index of valid oper blocks and types
229          */
230         typedef insp::flat_map<std::string, reference<OperInfo> > OperIndex;
231
232         /** Get a configuration tag
233          * @param tag The name of the tag to get
234          */
235         ConfigTag* ConfValue(const std::string& tag);
236
237         ConfigTagList ConfTags(const std::string& tag);
238
239         /** An empty configuration tag. */
240         ConfigTag* EmptyTag;
241
242         /** Error stream, contains error output from any failed configuration parsing.
243          */
244         std::stringstream errstr;
245
246         /** True if this configuration is valid enough to run with */
247         bool valid;
248
249         /** Bind to IPv6 by default */
250         bool WildcardIPv6;
251
252         /** This holds all the information in the config file,
253          * it's indexed by tag name to a vector of key/values.
254          */
255         ConfigDataHash config_data;
256
257         /** This holds all extra files that have been read in the configuration
258          * (for example, MOTD and RULES files are stored here)
259          */
260         ConfigFileCache Files;
261
262         /** Length limits, see definition of ServerLimits class
263          */
264         ServerLimits Limits;
265
266         /** Locations of various types of file (config, module, etc). */
267         ServerPaths Paths;
268
269         /** Configuration parsed from the command line.
270          */
271         CommandLineConf cmdline;
272
273         /** Clones CIDR range for ipv4 (0-32)
274          * Defaults to 32 (checks clones on all IPs seperately)
275          */
276         unsigned char c_ipv4_range;
277
278         /** Clones CIDR range for ipv6 (0-128)
279          * Defaults to 128 (checks on all IPs seperately)
280          */
281         unsigned char c_ipv6_range;
282
283         /** Holds the server name of the local server
284          * as defined by the administrator.
285          */
286         std::string ServerName;
287
288         /** Notice to give to users when they are banned by an XLine
289          */
290         std::string XLineMessage;
291
292         /* Holds the network name the local server
293          * belongs to. This is an arbitary field defined
294          * by the administrator.
295          */
296         std::string Network;
297
298         /** Holds the description of the local server
299          * as defined by the administrator.
300          */
301         std::string ServerDesc;
302
303         /** Pretend disabled commands don't exist.
304          */
305         bool DisabledDontExist;
306
307         /** This variable identifies which usermodes have been diabled.
308          */
309         std::bitset<64> DisabledUModes;
310
311         /** This variable identifies which chanmodes have been disabled.
312          */
313         std::bitset<64> DisabledCModes;
314
315         /** If set to true, then all opers on this server are
316          * shown with a generic 'is an IRC operator' line rather
317          * than the oper type. Oper types are still used internally.
318          */
319         bool GenericOper;
320
321         /** If this value is true, banned users (+b, not extbans) will not be able to change nick
322          * if banned on any channel, nor to message them.
323          */
324         bool RestrictBannedUsers;
325
326         /** The size of the read() buffer in the user
327          * handling code, used to read data into a user's
328          * recvQ.
329          */
330         int NetBufferSize;
331
332         /** The value to be used for listen() backlogs
333          * as default.
334          */
335         int MaxConn;
336
337         /** If we should check for clones during CheckClass() in AddUser()
338          * Setting this to false allows to not trigger on maxclones for users
339          * that may belong to another class after DNS-lookup is complete.
340          * It does, however, make the server spend more time on users we may potentially not want.
341          */
342         bool CCOnConnect;
343
344         /** The soft limit value assigned to the irc server.
345          * The IRC server will not allow more than this
346          * number of local users.
347          */
348         unsigned int SoftLimit;
349
350         /** Maximum number of targets for a multi target command
351          * such as PRIVMSG or KICK
352          */
353         unsigned int MaxTargets;
354
355         /** True if we're going to hide netsplits as *.net *.split for non-opers
356          */
357         bool HideSplits;
358
359         /** True if we're going to hide ban reasons for non-opers (e.g. G-Lines,
360          * K-Lines, Z-Lines)
361          */
362         bool HideBans;
363
364         /** True if raw I/O is being logged */
365         bool RawLog;
366
367         /** Set to a non-empty string to obfuscate server names. */
368         std::string HideServer;
369
370         /** Set to a non empty string to obfuscate nicknames prepended to a KILL.
371          */
372         std::string HideKillsServer;
373
374         /** Set to hide kills from clients of ulined servers in snotices.
375          */
376         bool HideULineKills;
377
378         /** The full pathname and filename of the PID
379          * file as defined in the configuration.
380          */
381         std::string PID;
382
383         /** The connect classes in use by the IRC server.
384          */
385         ClassVector Classes;
386
387         /** STATS characters in this list are available
388          * only to operators.
389          */
390         std::string UserStats;
391
392         /** Default channel modes
393          */
394         std::string DefaultModes;
395
396         /** Custom version string, which if defined can replace the system info in VERSION.
397          */
398         std::string CustomVersion;
399
400         /** If set to true, provide syntax hints for unknown commands
401          */
402         bool SyntaxHints;
403
404         /** The name of the casemapping method used by this server.
405          */
406         std::string CaseMapping;
407
408         /** If set to true, the CycleHosts mode change will be sourced from the user,
409          * rather than the server
410          */
411         bool CycleHostsFromUser;
412
413         /** If set to true, the full nick!user\@host will be shown in the TOPIC command
414          * for who set the topic last. If false, only the nick is shown.
415          */
416         bool FullHostInTopic;
417
418         /** Oper blocks keyed by their name
419          */
420         OperIndex oper_blocks;
421
422         /** Oper types keyed by their name
423          */
424         OperIndex OperTypes;
425
426         /** Default value for <connect:maxchans>, deprecated in 3.0
427          */
428         unsigned int MaxChans;
429
430         /** Default value for <oper:maxchans>, deprecated in 3.0
431          */
432         unsigned int OperMaxChans;
433
434         /** TS6-like server ID.
435          * NOTE: 000...999 are usable for InspIRCd servers. This
436          * makes code simpler. 0AA, 1BB etc with letters are reserved
437          * for services use.
438          */
439         std::string sid;
440
441         /** Construct a new ServerConfig
442          */
443         ServerConfig();
444
445         ~ServerConfig();
446
447         /** Get server ID as string with required leading zeroes
448          */
449         const std::string& GetSID() const { return sid; }
450
451         /** Read the entire configuration into memory
452          * and initialize this class. All other methods
453          * should be used only by the core.
454          */
455         void Read();
456
457         /** Apply configuration changes from the old configuration.
458          */
459         void Apply(ServerConfig* old, const std::string &useruid);
460         void ApplyModules(User* user);
461
462         void Fill();
463
464         /** Disables the commands specified in <disabled:commands>. */
465         bool ApplyDisabledCommands();
466
467         /** Escapes a value for storage in a configuration key.
468          * @param str The string to escape.
469          * @param xml Are we using the XML config format?
470          */
471         static std::string Escape(const std::string& str, bool xml = true);
472
473         /** If this value is true, snotices will not stack when repeats are sent
474          */
475         bool NoSnoticeStack;
476 };
477
478 /** The background thread for config reading, so that reading from executable includes
479  * does not block.
480  */
481 class CoreExport ConfigReaderThread : public Thread
482 {
483         ServerConfig* Config;
484         volatile bool done;
485  public:
486         const std::string TheUserUID;
487         ConfigReaderThread(const std::string &useruid)
488                 : Config(new ServerConfig), done(false), TheUserUID(useruid)
489         {
490         }
491
492         virtual ~ConfigReaderThread()
493         {
494                 delete Config;
495         }
496
497         void Run() CXX11_OVERRIDE;
498         /** Run in the main thread to apply the configuration */
499         void Finish();
500         bool IsDone() { return done; }
501 };
502
503 class CoreExport ConfigStatus
504 {
505  public:
506         User* const srcuser;
507
508         ConfigStatus(User* user = NULL)
509                 : srcuser(user)
510         {
511         }
512 };