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