]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/configreader.h
Move <security:announceinvites> to core_channel.
[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 = 0, 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 = 0);
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 = 0, 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         /** Check for an out of range value. If the value falls outside the boundaries a warning is
73          * logged and the value is corrected (set to def).
74          * @param key The key name, used in the warning message
75          * @param res The value to verify and modify if needed
76          * @param def The default value, res will be set to this if (min <= res <= max) doesn't hold true
77          * @param min Minimum accepted value for res
78          * @param max Maximum accepted value for res
79          */
80         void CheckRange(const std::string& key, long& res, long def, long min, long max);
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 GECOS (real name) length */
113         size_t MaxGecos;
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         /** Saved argc from startup
158          */
159         int argc;
160
161         /** Saved argv from startup
162          */
163         char** argv;
164 };
165
166 class CoreExport OperInfo : public refcountbase
167 {
168  public:
169         TokenList AllowedOperCommands;
170         TokenList AllowedPrivs;
171
172         /** Allowed user modes from oper classes. */
173         std::bitset<64> AllowedUserModes;
174
175         /** Allowed channel modes from oper classes. */
176         std::bitset<64> AllowedChanModes;
177
178         /** \<oper> block used for this oper-up. May be NULL. */
179         reference<ConfigTag> oper_block;
180         /** \<type> block used for this oper-up. Valid for local users, may be NULL on remote */
181         reference<ConfigTag> type_block;
182         /** \<class> blocks referenced from the \<type> block. These define individual permissions */
183         std::vector<reference<ConfigTag> > class_blocks;
184         /** Name of the oper type; i.e. the one shown in WHOIS */
185         std::string name;
186
187         /** Creates a new OperInfo with the specified oper type name.
188          * @param Name The name of the oper type.
189          */
190         OperInfo(const std::string& Name);
191
192         /** Get a configuration item, searching in the oper, type, and class blocks (in that order) */
193         std::string getConfig(const std::string& key);
194         void init();
195 };
196
197 /** This class holds the bulk of the runtime configuration for the ircd.
198  * It allows for reading new config values, accessing configuration files,
199  * and storage of the configuration data needed to run the ircd, such as
200  * the servername, connect classes, /ADMIN data, MOTDs and filenames etc.
201  */
202 class CoreExport ServerConfig
203 {
204   private:
205         void CrossCheckOperClassType();
206         void CrossCheckConnectBlocks(ServerConfig* current);
207
208  public:
209         class ServerPaths
210         {
211          public:
212                 /** Config path */
213                 std::string Config;
214
215                 /** Data path */
216                 std::string Data;
217
218                 /** Log path */
219                 std::string Log;
220
221                 /** Module path */
222                 std::string Module;
223
224                 ServerPaths(ConfigTag* tag);
225
226                 std::string PrependConfig(const std::string& fn) const { return FileSystem::ExpandPath(Config, fn); }
227                 std::string PrependData(const std::string& fn) const { return FileSystem::ExpandPath(Data, fn); }
228                 std::string PrependLog(const std::string& fn) const { return FileSystem::ExpandPath(Log, fn); }
229                 std::string PrependModule(const std::string& fn) const { return FileSystem::ExpandPath(Module, fn); }
230         };
231
232         /** Holds a complete list of all connect blocks
233          */
234         typedef std::vector<reference<ConnectClass> > ClassVector;
235
236         /** Index of valid oper blocks and types
237          */
238         typedef insp::flat_map<std::string, reference<OperInfo> > OperIndex;
239
240         /** Get a configuration tag
241          * @param tag The name of the tag to get
242          */
243         ConfigTag* ConfValue(const std::string& tag);
244
245         ConfigTagList ConfTags(const std::string& tag);
246
247         /** An empty configuration tag. */
248         ConfigTag* EmptyTag;
249
250         /** Error stream, contains error output from any failed configuration parsing.
251          */
252         std::stringstream errstr;
253
254         /** True if this configuration is valid enough to run with */
255         bool valid;
256
257         /** Bind to IPv6 by default */
258         bool WildcardIPv6;
259
260         /** This holds all the information in the config file,
261          * it's indexed by tag name to a vector of key/values.
262          */
263         ConfigDataHash config_data;
264
265         /** This holds all extra files that have been read in the configuration
266          * (for example, MOTD and RULES files are stored here)
267          */
268         ConfigFileCache Files;
269
270         /** Length limits, see definition of ServerLimits class
271          */
272         ServerLimits Limits;
273
274         /** Locations of various types of file (config, module, etc). */
275         ServerPaths Paths;
276
277         /** Configuration parsed from the command line.
278          */
279         CommandLineConf cmdline;
280
281         /** Clones CIDR range for ipv4 (0-32)
282          * Defaults to 32 (checks clones on all IPs seperately)
283          */
284         unsigned char c_ipv4_range;
285
286         /** Clones CIDR range for ipv6 (0-128)
287          * Defaults to 128 (checks on all IPs seperately)
288          */
289         unsigned char c_ipv6_range;
290
291         /** Holds the server name of the local server
292          * as defined by the administrator.
293          */
294         std::string ServerName;
295
296         /** Notice to give to users when they are banned by an XLine
297          */
298         std::string XLineMessage;
299
300         /* Holds the network name the local server
301          * belongs to. This is an arbitary field defined
302          * by the administrator.
303          */
304         std::string Network;
305
306         /** Holds the description of the local server
307          * as defined by the administrator.
308          */
309         std::string ServerDesc;
310
311         /** Pretend disabled commands don't exist.
312          */
313         bool DisabledDontExist;
314
315         /** This variable identifies which usermodes have been diabled.
316          */
317         std::bitset<64> DisabledUModes;
318
319         /** This variable identifies which chanmodes have been disabled.
320          */
321         std::bitset<64> DisabledCModes;
322
323         /** If set to true, then all opers on this server are
324          * shown with a generic 'is an IRC operator' line rather
325          * than the oper type. Oper types are still used internally.
326          */
327         bool GenericOper;
328
329         /** If this value is true, banned users (+b, not extbans) will not be able to change nick
330          * if banned on any channel, nor to message them.
331          */
332         bool RestrictBannedUsers;
333
334         /** The size of the read() buffer in the user
335          * handling code, used to read data into a user's
336          * recvQ.
337          */
338         int NetBufferSize;
339
340         /** The value to be used for listen() backlogs
341          * as default.
342          */
343         int MaxConn;
344
345         /** If we should check for clones during CheckClass() in AddUser()
346          * Setting this to false allows to not trigger on maxclones for users
347          * that may belong to another class after DNS-lookup is complete.
348          * It does, however, make the server spend more time on users we may potentially not want.
349          */
350         bool CCOnConnect;
351
352         /** The soft limit value assigned to the irc server.
353          * The IRC server will not allow more than this
354          * number of local users.
355          */
356         unsigned int SoftLimit;
357
358         /** Maximum number of targets for a multi target command
359          * such as PRIVMSG or KICK
360          */
361         unsigned int MaxTargets;
362
363         /** True if we're going to hide netsplits as *.net *.split for non-opers
364          */
365         bool HideSplits;
366
367         /** True if we're going to hide ban reasons for non-opers (e.g. G-Lines,
368          * K-Lines, Z-Lines)
369          */
370         bool HideBans;
371
372         /** True if raw I/O is being logged */
373         bool RawLog;
374
375         /** Set to a non-empty string to obfuscate server names. */
376         std::string HideServer;
377
378         /** Set to a non empty string to obfuscate nicknames prepended to a KILL.
379          */
380         std::string HideKillsServer;
381
382         /** Set to hide kills from clients of ulined servers in snotices.
383          */
384         bool HideULineKills;
385
386         /** The full pathname and filename of the PID
387          * file as defined in the configuration.
388          */
389         std::string PID;
390
391         /** The connect classes in use by the IRC server.
392          */
393         ClassVector Classes;
394
395         /** STATS characters in this list are available
396          * only to operators.
397          */
398         std::string UserStats;
399
400         /** Default channel modes
401          */
402         std::string DefaultModes;
403
404         /** Custom version string, which if defined can replace the system info in VERSION.
405          */
406         std::string CustomVersion;
407
408         /** If set to true, provide syntax hints for unknown commands
409          */
410         bool SyntaxHints;
411
412         /** The name of the casemapping method used by this server.
413          */
414         std::string CaseMapping;
415
416         /** If set to true, the CycleHosts mode change will be sourced from the user,
417          * rather than the server
418          */
419         bool CycleHostsFromUser;
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         /** TS6-like 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         /** Read the entire configuration into memory
460          * and initialize this class. All other methods
461          * should be used only by the core.
462          */
463         void Read();
464
465         /** Apply configuration changes from the old configuration.
466          */
467         void Apply(ServerConfig* old, const std::string &useruid);
468         void ApplyModules(User* user);
469
470         void Fill();
471
472         /** Disables the commands specified in <disabled:commands>. */
473         bool ApplyDisabledCommands();
474
475         /** Escapes a value for storage in a configuration key.
476          * @param str The string to escape.
477          * @param xml Are we using the XML config format?
478          */
479         static std::string Escape(const std::string& str, bool xml = true);
480
481         /** If this value is true, snotices will not stack when repeats are sent
482          */
483         bool NoSnoticeStack;
484 };
485
486 /** The background thread for config reading, so that reading from executable includes
487  * does not block.
488  */
489 class CoreExport ConfigReaderThread : public Thread
490 {
491         ServerConfig* Config;
492         volatile bool done;
493  public:
494         const std::string TheUserUID;
495         ConfigReaderThread(const std::string &useruid)
496                 : Config(new ServerConfig), done(false), TheUserUID(useruid)
497         {
498         }
499
500         virtual ~ConfigReaderThread()
501         {
502                 delete Config;
503         }
504
505         void Run() CXX11_OVERRIDE;
506         /** Run in the main thread to apply the configuration */
507         void Finish();
508         bool IsDone() { return done; }
509 };
510
511 class CoreExport ConfigStatus
512 {
513  public:
514         User* const srcuser;
515
516         ConfigStatus(User* user = NULL)
517                 : srcuser(user)
518         {
519         }
520 };