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