]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/configreader.h
Allow modules to prevent a message from updating the idle time.
[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-2019 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>
15  *
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.
19  *
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
23  * details.
24  *
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/>.
27  */
28
29
30 #pragma once
31
32 #include <sstream>
33 #include <string>
34 #include <vector>
35 #include <map>
36 #include "inspircd.h"
37 #include "modules.h"
38 #include "socketengine.h"
39 #include "socket.h"
40 #include "token_list.h"
41
42 /** Structure representing a single \<tag> in config */
43 class CoreExport ConfigTag : public refcountbase
44 {
45         ConfigItems items;
46  public:
47         const std::string tag;
48         const std::string src_name;
49         const int src_line;
50
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);
63
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
71          */
72         unsigned long getDuration(const std::string& key, unsigned long def, unsigned long min = 0, unsigned long max = ULONG_MAX);
73
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
79          */
80         bool readString(const std::string& key, std::string& value, bool allow_newline = false);
81
82         std::string getTagLocation();
83
84         inline const ConfigItems& getItems() const { return items; }
85
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);
88  private:
89         ConfigTag(const std::string& Tag, const std::string& file, int line);
90 };
91
92 /** Defines the server's length limits on various length-limited
93  * items such as topics, nicknames, channel names etc.
94  */
95 class ServerLimits
96 {
97  public:
98         /** Maximum nickname length */
99         size_t NickMax;
100         /** Maximum channel length */
101         size_t ChanMax;
102         /** Maximum number of modes per line */
103         size_t MaxModes;
104         /** Maximum length of ident, not including ~ etc */
105         size_t IdentMax;
106         /** Maximum length of a quit message */
107         size_t MaxQuit;
108         /** Maximum topic length */
109         size_t MaxTopic;
110         /** Maximum kick message length */
111         size_t MaxKick;
112         /** Maximum real name length */
113         size_t MaxReal;
114         /** Maximum away message length */
115         size_t MaxAway;
116         /** Maximum line length */
117         size_t MaxLine;
118         /** Maximum hostname length */
119         size_t MaxHost;
120
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
123          */
124         ServerLimits(ConfigTag* tag);
125
126         /** Maximum length of a n!u\@h mask */
127         size_t GetMaxMask() const { return NickMax + 1 + IdentMax + 1 + MaxHost; }
128 };
129
130 struct CommandLineConf
131 {
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
135          * foreground.
136          */
137         bool nofork;
138
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
143          * option.
144          */
145         bool forcedebug;
146
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
153          * hard disk.
154          */
155         bool writelog;
156
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
160          * the default.
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.
166          */
167         bool writepid;
168
169         /* Whether the --runasroot option was specified at boot. */
170         bool runasroot;
171
172         /** Saved argc from startup. */
173         int argc;
174
175         /** Saved argv from startup. */
176         char** argv;
177 };
178
179 class CoreExport OperInfo : public refcountbase
180 {
181  public:
182         TokenList AllowedOperCommands;
183         TokenList AllowedPrivs;
184
185         /** Allowed user modes from oper classes. */
186         std::bitset<64> AllowedUserModes;
187
188         /** Allowed channel modes from oper classes. */
189         std::bitset<64> AllowedChanModes;
190
191         /** \<oper> block used for this oper-up. May be NULL. */
192         reference<ConfigTag> oper_block;
193         /** \<type> block used for this oper-up. Valid for local users, may be NULL on remote */
194         reference<ConfigTag> type_block;
195         /** \<class> blocks referenced from the \<type> block. These define individual permissions */
196         std::vector<reference<ConfigTag> > class_blocks;
197         /** Name of the oper type; i.e. the one shown in WHOIS */
198         std::string name;
199
200         /** Creates a new OperInfo with the specified oper type name.
201          * @param Name The name of the oper type.
202          */
203         OperInfo(const std::string& Name);
204
205         /** Get a configuration item, searching in the oper, type, and class blocks (in that order) */
206         std::string getConfig(const std::string& key);
207         void init();
208 };
209
210 /** This class holds the bulk of the runtime configuration for the ircd.
211  * It allows for reading new config values, accessing configuration files,
212  * and storage of the configuration data needed to run the ircd, such as
213  * the servername, connect classes, /ADMIN data, MOTDs and filenames etc.
214  */
215 class CoreExport ServerConfig
216 {
217   private:
218         void CrossCheckOperClassType();
219         void CrossCheckConnectBlocks(ServerConfig* current);
220
221  public:
222         /** How to treat a user in a channel who is banned. */
223         enum BannedUserTreatment
224         {
225                 /** Don't treat a banned user any different to normal. */
226                 BUT_NORMAL,
227
228                 /** Restrict the actions of a banned user. */
229                 BUT_RESTRICT_SILENT,
230
231                 /** Restrict the actions of a banned user and notify them of their treatment. */
232                 BUT_RESTRICT_NOTIFY
233         };
234
235         class ServerPaths
236         {
237          public:
238                 /** Config path */
239                 std::string Config;
240
241                 /** Data path */
242                 std::string Data;
243
244                 /** Log path */
245                 std::string Log;
246
247                 /** Module path */
248                 std::string Module;
249
250                 ServerPaths(ConfigTag* tag);
251
252                 std::string PrependConfig(const std::string& fn) const { return FileSystem::ExpandPath(Config, fn); }
253                 std::string PrependData(const std::string& fn) const { return FileSystem::ExpandPath(Data, fn); }
254                 std::string PrependLog(const std::string& fn) const { return FileSystem::ExpandPath(Log, fn); }
255                 std::string PrependModule(const std::string& fn) const { return FileSystem::ExpandPath(Module, fn); }
256         };
257
258         /** Holds a complete list of all connect blocks
259          */
260         typedef std::vector<reference<ConnectClass> > ClassVector;
261
262         /** Index of valid oper blocks and types
263          */
264         typedef insp::flat_map<std::string, reference<OperInfo> > OperIndex;
265
266         /** Get a configuration tag by name. If one or more tags are present then the first is returned.
267          * @param tag The name of the tag to get.
268          * @returns Either a tag from the config or EmptyTag.
269          */
270         ConfigTag* ConfValue(const std::string& tag);
271
272         /** Get a list of configuration tags by name.
273          * @param tag The name of the tags to get.
274          * @returns Either a list of tags from the config or an empty ConfigTagList.
275          */
276         ConfigTagList ConfTags(const std::string& tag);
277
278         /** An empty configuration tag. */
279         ConfigTag* EmptyTag;
280
281         /** Error stream, contains error output from any failed configuration parsing.
282          */
283         std::stringstream errstr;
284
285         /** True if this configuration is valid enough to run with */
286         bool valid;
287
288         /** Bind to IPv6 by default */
289         bool WildcardIPv6;
290
291         /** This holds all the information in the config file,
292          * it's indexed by tag name to a vector of key/values.
293          */
294         ConfigDataHash config_data;
295
296         /** This holds all extra files that have been read in the configuration
297          * (for example, MOTD and RULES files are stored here)
298          */
299         ConfigFileCache Files;
300
301         /** Length limits, see definition of ServerLimits class
302          */
303         ServerLimits Limits;
304
305         /** Locations of various types of file (config, module, etc). */
306         ServerPaths Paths;
307
308         /** Configuration parsed from the command line.
309          */
310         CommandLineConf cmdline;
311
312         /** Clones CIDR range for ipv4 (0-32)
313          * Defaults to 32 (checks clones on all IPs seperately)
314          */
315         unsigned char c_ipv4_range;
316
317         /** Clones CIDR range for ipv6 (0-128)
318          * Defaults to 128 (checks on all IPs seperately)
319          */
320         unsigned char c_ipv6_range;
321
322         /** Holds the server name of the local server
323          * as defined by the administrator.
324          */
325         std::string ServerName;
326
327         /** Notice to give to users when they are banned by an XLine
328          */
329         std::string XLineMessage;
330
331         /* Holds the network name the local server
332          * belongs to. This is an arbitary field defined
333          * by the administrator.
334          */
335         std::string Network;
336
337         /** Holds the description of the local server
338          * as defined by the administrator.
339          */
340         std::string ServerDesc;
341
342         /** How to treat a user in a channel who is banned. */
343         BannedUserTreatment RestrictBannedUsers;
344
345         /** The size of the read() buffer in the user
346          * handling code, used to read data into a user's
347          * recvQ.
348          */
349         int NetBufferSize;
350
351         /** The value to be used for listen() backlogs
352          * as default.
353          */
354         int MaxConn;
355
356         /** If we should check for clones during CheckClass() in AddUser()
357          * Setting this to false allows to not trigger on maxclones for users
358          * that may belong to another class after DNS-lookup is complete.
359          * It does, however, make the server spend more time on users we may potentially not want.
360          */
361         bool CCOnConnect;
362
363         /** The soft limit value assigned to the irc server.
364          * The IRC server will not allow more than this
365          * number of local users.
366          */
367         unsigned int SoftLimit;
368
369         /** Maximum number of targets for a multi target command
370          * such as PRIVMSG or KICK
371          */
372         unsigned int MaxTargets;
373
374         /** The number of seconds that the server clock can skip by before server operators are warned. */
375         time_t TimeSkipWarn;
376
377         /** True if we're going to hide ban reasons for non-opers (e.g. G-lines,
378          * K-lines, Z-lines)
379          */
380         bool HideBans;
381
382         /** True if raw I/O is being logged */
383         bool RawLog;
384
385         /** Set to a non-empty string to obfuscate server names. */
386         std::string HideServer;
387
388         /** The full pathname and filename of the PID
389          * file as defined in the configuration.
390          */
391         std::string PID;
392
393         /** The connect classes in use by the IRC server.
394          */
395         ClassVector Classes;
396
397         /** Default channel modes
398          */
399         std::string DefaultModes;
400
401         /** Custom version string, which if defined can replace the system info in VERSION.
402          */
403         std::string CustomVersion;
404
405         /** If set to true, provide syntax hints for unknown commands
406          */
407         bool SyntaxHints;
408
409         /** The name of the casemapping method used by this server.
410          */
411         std::string CaseMapping;
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         /** Escapes a value for storage in a configuration key.
465          * @param str The string to escape.
466          * @param xml Are we using the XML config format?
467          */
468         static std::string Escape(const std::string& str, bool xml = true);
469
470         /** If this value is true, snotices will not stack when repeats are sent
471          */
472         bool NoSnoticeStack;
473 };
474
475 /** The background thread for config reading, so that reading from executable includes
476  * does not block.
477  */
478 class CoreExport ConfigReaderThread : public Thread
479 {
480         ServerConfig* Config;
481         volatile bool done;
482  public:
483         const std::string TheUserUID;
484         ConfigReaderThread(const std::string &useruid)
485                 : Config(new ServerConfig), done(false), TheUserUID(useruid)
486         {
487         }
488
489         virtual ~ConfigReaderThread()
490         {
491                 delete Config;
492         }
493
494         void Run() CXX11_OVERRIDE;
495         /** Run in the main thread to apply the configuration */
496         void Finish();
497         bool IsDone() { return done; }
498 };
499
500 /** Represents the status of a config load. */
501 class CoreExport ConfigStatus
502 {
503  public:
504         /** Whether this is the initial config load. */
505         bool const initial;
506
507         /** The user who initiated the config load or NULL if not initiated by a user. */
508         User* const srcuser;
509
510         /** Initializes a new instance of the ConfigStatus class.
511          * @param user The user who initiated the config load or NULL if not initiated by a user.
512          * @param isinitial Whether this is the initial config load.
513          */
514         ConfigStatus(User* user = NULL, bool isinitial = false)
515                 : initial(isinitial)
516                 , srcuser(user)
517         {
518         }
519 };