]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/configreader.h
f42bf67fea6c1c5adf3bbe820fc84457b1b55b10
[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         /** Finalises the settings by adding one. This allows for them to be used as-is
107          * without a 'value+1' when using the std::string assignment methods etc.
108          */
109         void Finalise()
110         {
111                 NickMax++;
112                 ChanMax++;
113                 IdentMax++;
114                 MaxQuit++;
115                 MaxTopic++;
116                 MaxKick++;
117                 MaxGecos++;
118                 MaxAway++;
119         }
120 };
121
122 struct CommandLineConf
123 {
124         /** If this value is true, the owner of the
125          * server specified -nofork on the command
126          * line, causing the daemon to stay in the
127          * foreground.
128          */
129         bool nofork;
130
131         /** If this value if true then all log
132          * messages will be output, regardless of
133          * the level given in the config file.
134          * This is set with the -debug commandline
135          * option.
136          */
137         bool forcedebug;
138
139         /** If this is true then log output will be
140          * written to the logfile. This is the default.
141          * If you put -nolog on the commandline then
142          * the logfile will not be written.
143          * This is meant to be used in conjunction with
144          * -debug for debugging without filling up the
145          * hard disk.
146          */
147         bool writelog;
148
149         /** True if we have been told to run the testsuite from the commandline,
150          * rather than entering the mainloop.
151          */
152         bool TestSuite;
153
154         /** Saved argc from startup
155          */
156         int argc;
157
158         /** Saved argv from startup
159          */
160         char** argv;
161
162         std::string startup_log;
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         inline const char* NameStr()
191         {
192                 return irc::Spacify(name.c_str());
193         }
194 };
195
196 /** This class holds the bulk of the runtime configuration for the ircd.
197  * It allows for reading new config values, accessing configuration files,
198  * and storage of the configuration data needed to run the ircd, such as
199  * the servername, connect classes, /ADMIN data, MOTDs and filenames etc.
200  */
201 class CoreExport ServerConfig
202 {
203   private:
204         void CrossCheckOperClassType();
205         void CrossCheckConnectBlocks(ServerConfig* current);
206
207  public:
208
209         /** Get a configuration tag
210          * @param tag The name of the tag to get
211          * @param offset get the Nth occurance of the tag
212          */
213         ConfigTag* ConfValue(const std::string& tag);
214
215         ConfigTagList ConfTags(const std::string& tag);
216
217         /** Error stream, contains error output from any failed configuration parsing.
218          */
219         std::stringstream errstr;
220
221         /** True if this configuration is valid enough to run with */
222         bool valid;
223
224         /** Bind to IPv6 by default */
225         bool WildcardIPv6;
226
227         /** Used to indicate who we announce invites to on a channel */
228         enum InviteAnnounceState { INVITE_ANNOUNCE_NONE, INVITE_ANNOUNCE_ALL, INVITE_ANNOUNCE_OPS, INVITE_ANNOUNCE_DYNAMIC };
229         enum OperSpyWhoisState { SPYWHOIS_NONE, SPYWHOIS_SINGLEMSG, SPYWHOIS_SPLITMSG };
230
231         /** This holds all the information in the config file,
232          * it's indexed by tag name to a vector of key/values.
233          */
234         ConfigDataHash config_data;
235
236         /** This holds all extra files that have been read in the configuration
237          * (for example, MOTD and RULES files are stored here)
238          */
239         ConfigFileCache Files;
240
241         /** Length limits, see definition of ServerLimits class
242          */
243         ServerLimits Limits;
244
245         /** Configuration parsed from the command line.
246          */
247         CommandLineConf cmdline;
248
249         /** Clones CIDR range for ipv4 (0-32)
250          * Defaults to 32 (checks clones on all IPs seperately)
251          */
252         int c_ipv4_range;
253
254         /** Clones CIDR range for ipv6 (0-128)
255          * Defaults to 128 (checks on all IPs seperately)
256          */
257         int c_ipv6_range;
258
259         /** Max number of WhoWas entries per user.
260          */
261         int WhoWasGroupSize;
262
263         /** Max number of cumulative user-entries in WhoWas.
264          *  When max reached and added to, push out oldest entry FIFO style.
265          */
266         int WhoWasMaxGroups;
267
268         /** Max seconds a user is kept in WhoWas before being pruned.
269          */
270         int WhoWasMaxKeep;
271
272         /** Holds the server name of the local server
273          * as defined by the administrator.
274          */
275         std::string ServerName;
276
277         /** Notice to give to users when they are Xlined
278          */
279         std::string MoronBanner;
280
281         /* Holds the network name the local server
282          * belongs to. This is an arbitary field defined
283          * by the administrator.
284          */
285         std::string Network;
286
287         /** Holds the description of the local server
288          * as defined by the administrator.
289          */
290         std::string ServerDesc;
291
292         /** Holds the admin's name, for output in
293          * the /ADMIN command.
294          */
295         std::string AdminName;
296
297         /** Holds the email address of the admin,
298          * for output in the /ADMIN command.
299          */
300         std::string AdminEmail;
301
302         /** Holds the admin's nickname, for output
303          * in the /ADMIN command
304          */
305         std::string AdminNick;
306
307         /** The admin-configured /DIE password
308          */
309         std::string diepass;
310
311         /** The admin-configured /RESTART password
312          */
313         std::string restartpass;
314
315         /** The hash method for *BOTH* the die and restart passwords.
316          */
317         std::string powerhash;
318
319         /** The pathname and filename of the message of the
320          * day file, as defined by the administrator.
321          */
322         std::string motd;
323
324         /** The pathname and filename of the rules file,
325          * as defined by the administrator.
326          */
327         std::string rules;
328
329         /** The quit prefix in use, or an empty string
330          */
331         std::string PrefixQuit;
332
333         /** The quit suffix in use, or an empty string
334          */
335         std::string SuffixQuit;
336
337         /** The fixed quit message in use, or an empty string
338          */
339         std::string FixedQuit;
340
341         /** The part prefix in use, or an empty string
342          */
343         std::string PrefixPart;
344
345         /** The part suffix in use, or an empty string
346          */
347         std::string SuffixPart;
348
349         /** The fixed part message in use, or an empty string
350          */
351         std::string FixedPart;
352
353         /** The DNS server to use for DNS queries
354          */
355         std::string DNSServer;
356
357         /** Pretend disabled commands don't exist.
358          */
359         bool DisabledDontExist;
360
361         /** This variable contains a space-seperated list
362          * of commands which are disabled by the
363          * administrator of the server for non-opers.
364          */
365         std::string DisabledCommands;
366
367         /** This variable identifies which usermodes have been diabled.
368          */
369         char DisabledUModes[64];
370
371         /** This variable identifies which chanmodes have been disabled.
372          */
373         char DisabledCModes[64];
374
375         /** The full path to the modules directory.
376          * This is either set at compile time, or
377          * overridden in the configuration file via
378          * the <path> tag.
379          */
380         std::string ModPath;
381
382         /** If set to true, then all opers on this server are
383          * shown with a generic 'is an IRC operator' line rather
384          * than the oper type. Oper types are still used internally.
385          */
386         bool GenericOper;
387
388         /** If this value is true, banned users (+b, not extbans) will not be able to change nick
389          * if banned on any channel, nor to message them.
390          */
391         bool RestrictBannedUsers;
392
393         /** If this is set to true, then mode lists (e.g
394          * MODE #chan b) are hidden from unprivileged
395          * users.
396          */
397         bool HideModeLists[256];
398
399         /** The number of seconds the DNS subsystem
400          * will wait before timing out any request.
401          */
402         int dns_timeout;
403
404         /** The size of the read() buffer in the user
405          * handling code, used to read data into a user's
406          * recvQ.
407          */
408         int NetBufferSize;
409
410         /** The value to be used for listen() backlogs
411          * as default.
412          */
413         int MaxConn;
414
415         /** The soft limit value assigned to the irc server.
416          * The IRC server will not allow more than this
417          * number of local users.
418          */
419         unsigned int SoftLimit;
420
421         /** Maximum number of targets for a multi target command
422          * such as PRIVMSG or KICK
423          */
424         unsigned int MaxTargets;
425
426         /** True if we're going to hide netsplits as *.net *.split for non-opers
427          */
428         bool HideSplits;
429
430         /** True if we're going to hide ban reasons for non-opers (e.g. G-Lines,
431          * K-Lines, Z-Lines)
432          */
433         bool HideBans;
434
435         /** Announce invites to the channel with a server notice
436          */
437         InviteAnnounceState AnnounceInvites;
438
439         /** If this is enabled then operators will
440          * see invisible (+i) channels in /whois.
441          */
442         OperSpyWhoisState OperSpyWhois;
443
444         /** True if raw I/O is being logged */
445         bool RawLog;
446
447         /** Set to a non-empty string to obfuscate the server name of users in WHOIS
448          */
449         std::string HideWhoisServer;
450
451         /** Set to a non empty string to obfuscate nicknames prepended to a KILL.
452          */
453         std::string HideKillsServer;
454
455         /** The full pathname and filename of the PID
456          * file as defined in the configuration.
457          */
458         std::string PID;
459
460         /** The connect classes in use by the IRC server.
461          */
462         ClassVector Classes;
463
464         /** The 005 tokens of this server (ISUPPORT)
465          * populated/repopulated upon loading or unloading
466          * modules.
467          */
468         std::string data005;
469
470         /** isupport strings
471          */
472         std::vector<std::string> isupport;
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         /** Max banlist sizes for channels (the std::string is a glob)
492          */
493         std::map<std::string, int> maxbans;
494
495         /** If set to true, no user DNS lookups are to be performed
496          */
497         bool NoUserDns;
498
499         /** If set to true, provide syntax hints for unknown commands
500          */
501         bool SyntaxHints;
502
503         /** If set to true, users appear to quit then rejoin when their hosts change.
504          * This keeps clients synchronized properly.
505          */
506         bool CycleHosts;
507
508         /** If set to true, the CycleHosts mode change will be sourced from the user,
509          * rather than the server
510          */
511         bool CycleHostsFromUser;
512
513         /** If set to true, prefixed channel NOTICEs and PRIVMSGs will have the prefix
514          *  added to the outgoing text for undernet style msg prefixing.
515          */
516         bool UndernetMsgPrefix;
517
518         /** If set to true, the full nick!user@host will be shown in the TOPIC command
519          * for who set the topic last. If false, only the nick is shown.
520          */
521         bool FullHostInTopic;
522
523         /** Oper block and type index.
524          * For anonymous oper blocks (type only), prefix with a space.
525          */
526         OperIndex oper_blocks;
527
528         /** Max channels per user
529          */
530         unsigned int MaxChans;
531
532         /** Oper max channels per user
533          */
534         unsigned int OperMaxChans;
535
536         /** TS6-like server ID.
537          * NOTE: 000...999 are usable for InspIRCd servers. This
538          * makes code simpler. 0AA, 1BB etc with letters are reserved
539          * for services use.
540          */
541         std::string sid;
542
543         /** Construct a new ServerConfig
544          */
545         ServerConfig();
546
547         /** Get server ID as string with required leading zeroes
548          */
549         std::string GetSID();
550
551         /** Update the 005 vector
552          */
553         void Update005();
554
555         /** Send the 005 numerics (ISUPPORT) to a user
556          */
557         void Send005(User* user);
558
559         /** Read the entire configuration into memory
560          * and initialize this class. All other methods
561          * should be used only by the core.
562          */
563         void Read();
564
565         /** Apply configuration changes from the old configuration.
566          */
567         void Apply(ServerConfig* old, const std::string &useruid);
568         void ApplyModules(User* user);
569
570         void Fill();
571
572         /* Returns true if the given string starts with a windows drive letter
573          */
574         bool StartsWithWindowsDriveLetter(const std::string &path);
575
576         bool ApplyDisabledCommands(const std::string& data);
577
578         /** Clean a filename, stripping the directories (and drives) from string.
579          * @param name Directory to tidy
580          * @return The cleaned filename
581          */
582         static const char* CleanFilename(const char* name);
583
584         /** Check if a file exists.
585          * @param file The full path to a file
586          * @return True if the file exists and is readable.
587          */
588         static bool FileExists(const char* file);
589
590         /** If this value is true, invites will bypass more than just +i
591          */
592         bool InvBypassModes;
593
594         /** If this value is true, snotices will not stack when repeats are sent
595          */
596         bool NoSnoticeStack;
597
598 };
599
600 /** The background thread for config reading, so that reading from executable includes
601  * does not block.
602  */
603 class CoreExport ConfigReaderThread : public Thread
604 {
605         ServerConfig* Config;
606         volatile bool done;
607  public:
608         const std::string TheUserUID;
609         ConfigReaderThread(const std::string &useruid)
610                 : Config(new ServerConfig), done(false), TheUserUID(useruid)
611         {
612         }
613
614         virtual ~ConfigReaderThread()
615         {
616                 delete Config;
617         }
618
619         void Run();
620         /** Run in the main thread to apply the configuration */
621         void Finish();
622         bool IsDone() { return done; }
623 };
624
625 #endif