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