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