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