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