]> 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
118         /** Creating the class initialises it to the defaults
119          * as in 1.1's ./configure script. Reading other values
120          * from the config will change these values.
121          */
122         ServerLimits() : NickMax(31), ChanMax(64), MaxModes(20), IdentMax(12),
123                 MaxQuit(255), MaxTopic(307), MaxKick(255), MaxGecos(128), MaxAway(200),
124                 MaxLine(512) { }
125 };
126
127 struct CommandLineConf
128 {
129         /** If this value is true, the owner of the
130          * server specified -nofork on the command
131          * line, causing the daemon to stay in the
132          * foreground.
133          */
134         bool nofork;
135
136         /** If this value if true then all log
137          * messages will be output, regardless of
138          * the level given in the config file.
139          * This is set with the -debug commandline
140          * option.
141          */
142         bool forcedebug;
143
144         /** If this is true then log output will be
145          * written to the logfile. This is the default.
146          * If you put -nolog on the commandline then
147          * the logfile will not be written.
148          * This is meant to be used in conjunction with
149          * -debug for debugging without filling up the
150          * hard disk.
151          */
152         bool writelog;
153
154         /** True if we have been told to run the testsuite from the commandline,
155          * rather than entering the mainloop.
156          */
157         bool TestSuite;
158
159         /** Saved argc from startup
160          */
161         int argc;
162
163         /** Saved argv from startup
164          */
165         char** argv;
166 };
167
168 class CoreExport OperInfo : public refcountbase
169 {
170  public:
171         std::set<std::string> AllowedOperCommands;
172         std::set<std::string> AllowedPrivs;
173
174         /** Allowed user modes from oper classes. */
175         std::bitset<64> AllowedUserModes;
176
177         /** Allowed channel modes from oper classes. */
178         std::bitset<64> AllowedChanModes;
179
180         /** \<oper> block used for this oper-up. May be NULL. */
181         reference<ConfigTag> oper_block;
182         /** \<type> block used for this oper-up. Valid for local users, may be NULL on remote */
183         reference<ConfigTag> type_block;
184         /** \<class> blocks referenced from the \<type> block. These define individual permissions */
185         std::vector<reference<ConfigTag> > class_blocks;
186         /** Name of the oper type; i.e. the one shown in WHOIS */
187         std::string name;
188
189         /** Get a configuration item, searching in the oper, type, and class blocks (in that order) */
190         std::string getConfig(const std::string& key);
191         void init();
192 };
193
194 /** This class holds the bulk of the runtime configuration for the ircd.
195  * It allows for reading new config values, accessing configuration files,
196  * and storage of the configuration data needed to run the ircd, such as
197  * the servername, connect classes, /ADMIN data, MOTDs and filenames etc.
198  */
199 class CoreExport ServerConfig
200 {
201   private:
202         void CrossCheckOperClassType();
203         void CrossCheckConnectBlocks(ServerConfig* current);
204
205  public:
206         class ServerPaths
207         {
208          public:
209                 /** Config path */
210                 std::string Config;
211
212                 /** Data path */
213                 std::string Data;
214
215                 /** Log path */
216                 std::string Log;
217
218                 /** Module path */
219                 std::string Module;
220
221                 ServerPaths()
222                         : Config(CONFIG_PATH)
223                         , Data(DATA_PATH)
224                         , Log(LOG_PATH)
225                         , Module(MOD_PATH) { }
226
227                 std::string PrependConfig(const std::string& fn) const { return FileSystem::ExpandPath(Config, fn); }
228                 std::string PrependData(const std::string& fn) const { return FileSystem::ExpandPath(Data, fn); }
229                 std::string PrependLog(const std::string& fn) const { return FileSystem::ExpandPath(Log, fn); }
230                 std::string PrependModule(const std::string& fn) const { return FileSystem::ExpandPath(Module, fn); }
231         };
232
233         /** Get a configuration tag
234          * @param tag The name of the tag to get
235          */
236         ConfigTag* ConfValue(const std::string& tag);
237
238         ConfigTagList ConfTags(const std::string& tag);
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         /** Used to indicate who we announce invites to on a channel */
251         enum InviteAnnounceState { INVITE_ANNOUNCE_NONE, INVITE_ANNOUNCE_ALL, INVITE_ANNOUNCE_OPS, INVITE_ANNOUNCE_DYNAMIC };
252         enum OperSpyWhoisState { SPYWHOIS_NONE, SPYWHOIS_SINGLEMSG, SPYWHOIS_SPLITMSG };
253
254         /** This holds all the information in the config file,
255          * it's indexed by tag name to a vector of key/values.
256          */
257         ConfigDataHash config_data;
258
259         /** This holds all extra files that have been read in the configuration
260          * (for example, MOTD and RULES files are stored here)
261          */
262         ConfigFileCache Files;
263
264         /** Length limits, see definition of ServerLimits class
265          */
266         ServerLimits Limits;
267
268         /** Locations of various types of file (config, module, etc). */
269         ServerPaths Paths;
270
271         /** Configuration parsed from the command line.
272          */
273         CommandLineConf cmdline;
274
275         /** Clones CIDR range for ipv4 (0-32)
276          * Defaults to 32 (checks clones on all IPs seperately)
277          */
278         int c_ipv4_range;
279
280         /** Clones CIDR range for ipv6 (0-128)
281          * Defaults to 128 (checks on all IPs seperately)
282          */
283         int c_ipv6_range;
284
285         /** Holds the server name of the local server
286          * as defined by the administrator.
287          */
288         std::string ServerName;
289
290         /** Notice to give to users when they are banned by an XLine
291          */
292         std::string XLineMessage;
293
294         /* Holds the network name the local server
295          * belongs to. This is an arbitary field defined
296          * by the administrator.
297          */
298         std::string Network;
299
300         /** Holds the description of the local server
301          * as defined by the administrator.
302          */
303         std::string ServerDesc;
304
305         /** Holds the admin's name, for output in
306          * the /ADMIN command.
307          */
308         std::string AdminName;
309
310         /** Holds the email address of the admin,
311          * for output in the /ADMIN command.
312          */
313         std::string AdminEmail;
314
315         /** Holds the admin's nickname, for output
316          * in the /ADMIN command
317          */
318         std::string AdminNick;
319
320         /** The admin-configured /DIE password
321          */
322         std::string diepass;
323
324         /** The admin-configured /RESTART password
325          */
326         std::string restartpass;
327
328         /** The hash method for *BOTH* the die and restart passwords.
329          */
330         std::string powerhash;
331
332         /** The quit prefix in use, or an empty string
333          */
334         std::string PrefixQuit;
335
336         /** The quit suffix in use, or an empty string
337          */
338         std::string SuffixQuit;
339
340         /** The fixed quit message in use, or an empty string
341          */
342         std::string FixedQuit;
343
344         /** The part prefix in use, or an empty string
345          */
346         std::string PrefixPart;
347
348         /** The part suffix in use, or an empty string
349          */
350         std::string SuffixPart;
351
352         /** The fixed part message in use, or an empty string
353          */
354         std::string FixedPart;
355
356         /** Pretend disabled commands don't exist.
357          */
358         bool DisabledDontExist;
359
360         /** This variable contains a space-seperated list
361          * of commands which are disabled by the
362          * administrator of the server for non-opers.
363          */
364         std::string DisabledCommands;
365
366         /** This variable identifies which usermodes have been diabled.
367          */
368         char DisabledUModes[64];
369
370         /** This variable identifies which chanmodes have been disabled.
371          */
372         char DisabledCModes[64];
373
374         /** If set to true, then all opers on this server are
375          * shown with a generic 'is an IRC operator' line rather
376          * than the oper type. Oper types are still used internally.
377          */
378         bool GenericOper;
379
380         /** If this value is true, banned users (+b, not extbans) will not be able to change nick
381          * if banned on any channel, nor to message them.
382          */
383         bool RestrictBannedUsers;
384
385         /** If this is set to true, then mode lists (e.g
386          * MODE \#chan b) are hidden from unprivileged
387          * users.
388          */
389         bool HideModeLists[256];
390
391         /** The number of seconds the DNS subsystem
392          * will wait before timing out any request.
393          */
394         int dns_timeout;
395
396         /** The size of the read() buffer in the user
397          * handling code, used to read data into a user's
398          * recvQ.
399          */
400         int NetBufferSize;
401
402         /** The value to be used for listen() backlogs
403          * as default.
404          */
405         int MaxConn;
406
407         /** If we should check for clones during CheckClass() in AddUser()
408          * Setting this to false allows to not trigger on maxclones for users
409          * that may belong to another class after DNS-lookup is complete.
410          * It does, however, make the server spend more time on users we may potentially not want.
411          */
412         bool CCOnConnect;
413
414         /** The soft limit value assigned to the irc server.
415          * The IRC server will not allow more than this
416          * number of local users.
417          */
418         unsigned int SoftLimit;
419
420         /** Maximum number of targets for a multi target command
421          * such as PRIVMSG or KICK
422          */
423         unsigned int MaxTargets;
424
425         /** True if we're going to hide netsplits as *.net *.split for non-opers
426          */
427         bool HideSplits;
428
429         /** True if we're going to hide ban reasons for non-opers (e.g. G-Lines,
430          * K-Lines, Z-Lines)
431          */
432         bool HideBans;
433
434         /** Announce invites to the channel with a server notice
435          */
436         InviteAnnounceState AnnounceInvites;
437
438         /** If this is enabled then operators will
439          * see invisible (+i) channels in /whois.
440          */
441         OperSpyWhoisState OperSpyWhois;
442
443         /** True if raw I/O is being logged */
444         bool RawLog;
445
446         /** Set to a non-empty string to obfuscate the server name of users in WHOIS
447          */
448         std::string HideWhoisServer;
449
450         /** Set to a non empty string to obfuscate nicknames prepended to a KILL.
451          */
452         std::string HideKillsServer;
453
454         /** The full pathname and filename of the PID
455          * file as defined in the configuration.
456          */
457         std::string PID;
458
459         /** The connect classes in use by the IRC server.
460          */
461         ClassVector Classes;
462
463         /** STATS characters in this list are available
464          * only to operators.
465          */
466         std::string UserStats;
467
468         /** Default channel modes
469          */
470         std::string DefaultModes;
471
472         /** Custom version string, which if defined can replace the system info in VERSION.
473          */
474         std::string CustomVersion;
475
476         /** If set to true, provide syntax hints for unknown commands
477          */
478         bool SyntaxHints;
479
480         /** If set to true, the CycleHosts mode change will be sourced from the user,
481          * rather than the server
482          */
483         bool CycleHostsFromUser;
484
485         /** If set to true, prefixed channel NOTICEs and PRIVMSGs will have the prefix
486          *  added to the outgoing text for undernet style msg prefixing.
487          */
488         bool UndernetMsgPrefix;
489
490         /** If set to true, the full nick!user\@host will be shown in the TOPIC command
491          * for who set the topic last. If false, only the nick is shown.
492          */
493         bool FullHostInTopic;
494
495         /** Oper blocks keyed by their name
496          */
497         OperIndex oper_blocks;
498
499         /** Oper types keyed by their name
500          */
501         OperIndex OperTypes;
502
503         /** Max channels per user
504          */
505         unsigned int MaxChans;
506
507         /** Oper max channels per user
508          */
509         unsigned int OperMaxChans;
510
511         /** TS6-like server ID.
512          * NOTE: 000...999 are usable for InspIRCd servers. This
513          * makes code simpler. 0AA, 1BB etc with letters are reserved
514          * for services use.
515          */
516         std::string sid;
517
518         /** Construct a new ServerConfig
519          */
520         ServerConfig();
521
522         /** Get server ID as string with required leading zeroes
523          */
524         const std::string& GetSID() const { return sid; }
525
526         /** Read the entire configuration into memory
527          * and initialize this class. All other methods
528          * should be used only by the core.
529          */
530         void Read();
531
532         /** Apply configuration changes from the old configuration.
533          */
534         void Apply(ServerConfig* old, const std::string &useruid);
535         void ApplyModules(User* user);
536
537         void Fill();
538
539         bool ApplyDisabledCommands(const std::string& data);
540
541         /** Escapes a value for storage in a configuration key.
542          * @param str The string to escape.
543          * @param xml Are we using the XML config format?
544          */
545         static std::string Escape(const std::string& str, bool xml = true);
546
547         /** If this value is true, invites will bypass more than just +i
548          */
549         bool InvBypassModes;
550
551         /** If this value is true, snotices will not stack when repeats are sent
552          */
553         bool NoSnoticeStack;
554 };
555
556 /** The background thread for config reading, so that reading from executable includes
557  * does not block.
558  */
559 class CoreExport ConfigReaderThread : public Thread
560 {
561         ServerConfig* Config;
562         volatile bool done;
563  public:
564         const std::string TheUserUID;
565         ConfigReaderThread(const std::string &useruid)
566                 : Config(new ServerConfig), done(false), TheUserUID(useruid)
567         {
568         }
569
570         virtual ~ConfigReaderThread()
571         {
572                 delete Config;
573         }
574
575         void Run();
576         /** Run in the main thread to apply the configuration */
577         void Finish();
578         bool IsDone() { return done; }
579 };
580
581 class CoreExport ConfigStatus
582 {
583  public:
584         User* const srcuser;
585
586         ConfigStatus(User* user = NULL)
587                 : srcuser(user)
588         {
589         }
590 };