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