1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 #ifndef INSPIRCD_CONFIGREADER
15 #define INSPIRCD_CONFIGREADER
23 #include "socketengine.h"
26 /* Required forward definitions */
32 /** A set of oper types
34 typedef std::map<irc::string,std::string> opertype_t;
36 /** Holds an oper class.
38 struct operclass_data : public classbase
40 /** Command list for the class
42 std::string commandlist;
44 /** Channel mode list for the class
46 std::string cmodelist;
48 /** User mode list for the class
50 std::string umodelist;
52 /** Priviledges given by this class
57 /** A Set of oper classes
59 typedef std::map<irc::string, operclass_data> operclass_t;
61 /** Defines the server's length limits on various length-limited
62 * items such as topics, nicknames, channel names etc.
67 /** Maximum nickname length */
69 /** Maximum channel length */
71 /** Maximum number of modes per line */
73 /** Maximum length of ident, not including ~ etc */
75 /** Maximum length of a quit message */
77 /** Maximum topic length */
79 /** Maximum kick message length */
81 /** Maximum GECOS (real name) length */
83 /** Maximum away message length */
86 /** Creating the class initialises it to the defaults
87 * as in 1.1's ./configure script. Reading other values
88 * from the config will change these values.
90 ServerLimits() : NickMax(31), ChanMax(64), MaxModes(20), IdentMax(12), MaxQuit(255), MaxTopic(307), MaxKick(255), MaxGecos(128), MaxAway(200)
94 /** Finalises the settings by adding one. This allows for them to be used as-is
95 * without a 'value+1' when using the std::string assignment methods etc.
110 /** This class holds the bulk of the runtime configuration for the ircd.
111 * It allows for reading new config values, accessing configuration files,
112 * and storage of the configuration data needed to run the ircd, such as
113 * the servername, connect classes, /ADMIN data, MOTDs and filenames etc.
115 class CoreExport ServerConfig : public classbase
118 /** This variable holds the names of all
119 * files included from the main one. This
120 * is used to make sure that no files are
121 * recursively included.
123 std::vector<std::string> include_stack;
125 /* classes removed by this rehash */
126 std::vector<ConnectClass*> removed_classes;
128 /** This private method processes one line of
129 * configutation, appending errors to errorstream
130 * and setting error if an error has occured.
132 bool ParseLine(const std::string &filename, std::string &line, long &linenumber, bool allowexeinc);
134 /** Check that there is only one of each configuration item
136 bool CheckOnce(const char* tag);
138 void CrossCheckOperClassType();
139 void CrossCheckConnectBlocks(ServerConfig* current);
142 /** Process an include executable directive
144 bool DoPipe(const std::string &file);
146 /** Process an include file directive
148 bool DoInclude(const std::string &file, bool allowexeinc);
150 /** Error stream, contains error output from any failed configuration parsing.
152 std::stringstream errstr;
154 /** True if this configuration is valid enough to run with */
157 /** Set of included files. Do we use this any more?
159 std::map<std::string, std::istream*> IncludedFiles;
161 /** Used to indicate who we announce invites to on a channel */
162 enum InviteAnnounceState { INVITE_ANNOUNCE_NONE, INVITE_ANNOUNCE_ALL, INVITE_ANNOUNCE_OPS, INVITE_ANNOUNCE_DYNAMIC };
164 /** Not used any more as it is named, can probably be removed or renamed.
168 /** This holds all the information in the config file,
169 * it's indexed by tag name to a vector of key/values.
171 ConfigDataHash config_data;
173 /** Length limits, see definition of ServerLimits class
177 /** Clones CIDR range for ipv4 (0-32)
178 * Defaults to 32 (checks clones on all IPs seperately)
182 /** Clones CIDR range for ipv6 (0-128)
183 * Defaults to 128 (checks on all IPs seperately)
187 /** Max number of WhoWas entries per user.
191 /** Max number of cumulative user-entries in WhoWas.
192 * When max reached and added to, push out oldest entry FIFO style.
196 /** Max seconds a user is kept in WhoWas before being pruned.
200 /** Both for set(g|u)id.
203 std::string SetGroup;
205 /** Holds the server name of the local server
206 * as defined by the administrator.
208 std::string ServerName;
210 /** Notice to give to users when they are Xlined
212 std::string MoronBanner;
214 /* Holds the network name the local server
215 * belongs to. This is an arbitary field defined
216 * by the administrator.
220 /** Holds the description of the local server
221 * as defined by the administrator.
223 std::string ServerDesc;
225 /** Holds the admin's name, for output in
226 * the /ADMIN command.
228 std::string AdminName;
230 /** Holds the email address of the admin,
231 * for output in the /ADMIN command.
233 std::string AdminEmail;
235 /** Holds the admin's nickname, for output
236 * in the /ADMIN command
238 std::string AdminNick;
240 /** The admin-configured /DIE password
244 /** The admin-configured /RESTART password
246 std::string restartpass;
248 /** The hash method for *BOTH* the die and restart passwords.
250 std::string powerhash;
252 /** The pathname and filename of the message of the
253 * day file, as defined by the administrator.
257 /** The pathname and filename of the rules file,
258 * as defined by the administrator.
262 /** The quit prefix in use, or an empty string
264 std::string PrefixQuit;
266 /** The quit suffix in use, or an empty string
268 std::string SuffixQuit;
270 /** The fixed quit message in use, or an empty string
272 std::string FixedQuit;
274 /** The part prefix in use, or an empty string
276 std::string PrefixPart;
278 /** The part suffix in use, or an empty string
280 std::string SuffixPart;
282 /** The fixed part message in use, or an empty string
284 std::string FixedPart;
286 /** The last string found within a <die> tag, or
289 std::string DieValue;
291 /** The DNS server to use for DNS queries
293 std::string DNSServer;
295 /** Pretend disabled commands don't exist.
297 bool DisabledDontExist;
299 /** This variable contains a space-seperated list
300 * of commands which are disabled by the
301 * administrator of the server for non-opers.
303 std::string DisabledCommands;
305 /** This variable identifies which usermodes have been diabled.
308 char DisabledUModes[64];
310 /** This variable identifies which chanmodes have been disabled.
312 char DisabledCModes[64];
314 /** The full path to the modules directory.
315 * This is either set at compile time, or
316 * overridden in the configuration file via
321 /** The file handle of the logfile. If this
322 * value is NULL, the log file is not open,
323 * probably due to a permissions error on
324 * startup (this should not happen in normal
329 /** If this value is true, the owner of the
330 * server specified -nofork on the command
331 * line, causing the daemon to stay in the
336 /** If this value if true then all log
337 * messages will be output, regardless of
338 * the level given in the config file.
339 * This is set with the -debug commandline
344 /** If this is true then log output will be
345 * written to the logfile. This is the default.
346 * If you put -nolog on the commandline then
347 * the logfile will not be written.
348 * This is meant to be used in conjunction with
349 * -debug for debugging without filling up the
354 /** If set to true, then all opers on this server are
355 * shown with a generic 'is an IRC operator' line rather
356 * than the oper type. Oper types are still used internally.
360 /** If this value is true, banned users (+b, not extbans) will not be able to change nick
361 * if banned on any channel, nor to message them.
363 bool RestrictBannedUsers;
365 /** If this value is true, halfops have been
366 * enabled in the configuration file.
370 /** If this is set to true, then mode lists (e.g
371 * MODE #chan b) are hidden from unprivileged
374 bool HideModeLists[256];
376 /** The number of seconds the DNS subsystem
377 * will wait before timing out any request.
381 /** The size of the read() buffer in the user
382 * handling code, used to read data into a user's
387 /** The value to be used for listen() backlogs
392 /** The soft limit value assigned to the irc server.
393 * The IRC server will not allow more than this
394 * number of local users.
396 unsigned int SoftLimit;
398 /** Maximum number of targets for a multi target command
399 * such as PRIVMSG or KICK
401 unsigned int MaxTargets;
403 /** The maximum number of /WHO results allowed
404 * in any single /WHO command.
408 /** True if the DEBUG loglevel is selected.
412 /** How many seconds to wait before exiting
413 * the program when /DIE is correctly issued.
417 /** True if we're going to hide netsplits as *.net *.split for non-opers
421 /** True if we're going to hide ban reasons for non-opers (e.g. G-Lines,
426 /** Announce invites to the channel with a server notice
428 InviteAnnounceState AnnounceInvites;
430 /** If this is enabled then operators will
431 * see invisible (+i) channels in /whois.
435 /** Set to a non-empty string to obfuscate the server name of users in WHOIS
437 std::string HideWhoisServer;
439 /** Set to a non empty string to obfuscate nicknames prepended to a KILL.
441 std::string HideKillsServer;
443 /** The MOTD file, cached in a file_cache type.
447 /** The RULES file, cached in a file_cache type.
451 /** The full pathname and filename of the PID
452 * file as defined in the configuration.
456 /** The connect classes in use by the IRC server.
460 /** The 005 tokens of this server (ISUPPORT)
461 * populated/repopulated upon loading or unloading
468 std::vector<std::string> isupport;
470 /** STATS characters in this list are available
473 std::string UserStats;
475 /** The path and filename of the ircd.log file
479 /** Default channel modes
481 std::string DefaultModes;
483 /** Custom version string, which if defined can replace the system info in VERSION.
485 std::string CustomVersion;
487 /** List of u-lined servers
489 std::map<irc::string, bool> ulines;
491 /** Max banlist sizes for channels (the std::string is a glob)
493 std::map<std::string, int> maxbans;
495 /** Directory where the inspircd binary resides
499 /** If set to true, no user DNS lookups are to be performed
503 /** If set to true, provide syntax hints for unknown commands
507 /** If set to true, users appear to quit then rejoin when their hosts change.
508 * This keeps clients synchronized properly.
512 /** If set to true, prefixed channel NOTICEs and PRIVMSGs will have the prefix
513 * added to the outgoing text for undernet style msg prefixing.
515 bool UndernetMsgPrefix;
517 /** If set to true, the full nick!user@host will be shown in the TOPIC command
518 * for who set the topic last. If false, only the nick is shown.
520 bool FullHostInTopic;
522 /** All oper type definitions from the config file
524 opertype_t opertypes;
526 /** All oper class definitions from the config file
528 operclass_t operclass;
530 /** Saved argv from startup
534 /** Saved argc from startup
538 /** Max channels per user
540 unsigned int MaxChans;
542 /** Oper max channels per user
544 unsigned int OperMaxChans;
546 /** TS6-like server ID.
547 * NOTE: 000...999 are usable for InspIRCd servers. This
548 * makes code simpler. 0AA, 1BB etc with letters are reserved
553 /** True if we have been told to run the testsuite from the commandline,
554 * rather than entering the mainloop.
558 /** Construct a new ServerConfig
562 /** Get server ID as string with required leading zeroes
564 std::string GetSID();
566 /** Update the 005 vector
570 /** Send the 005 numerics (ISUPPORT) to a user
572 void Send005(User* user);
574 /** Read the entire configuration into memory
575 * and initialize this class. All other methods
576 * should be used only by the core.
580 /** Apply configuration changes from the old configuration.
582 void Apply(ServerConfig* old, const std::string &useruid);
583 void ApplyModules(User* user);
585 /** Read a file into a file_cache object
587 bool ReadFile(file_cache &F, const char* fname);
589 /* Returns true if the given string starts with a windows drive letter
591 bool StartsWithWindowsDriveLetter(const std::string &path);
593 /** Load 'filename' into 'target', with the new config parser everything is parsed into
594 * tag/key/value at load-time rather than at read-value time.
596 bool LoadConf(FILE* &conf, const char* filename, bool allowexeinc);
598 /** Load 'filename' into 'target', with the new config parser everything is parsed into
599 * tag/key/value at load-time rather than at read-value time.
601 bool LoadConf(FILE* &conf, const std::string &filename, bool allowexeinc);
603 /** Writes 'length' chars into 'result' as a string
605 bool ConfValue(const char* tag, const char* var, int index, char* result, int length, bool allow_linefeeds = false);
607 /** Writes 'length' chars into 'result' as a string
609 bool ConfValue(const char* tag, const char* var, const char* default_value, int index, char* result, int length, bool allow_linefeeds = false);
611 /** Writes 'length' chars into 'result' as a string
613 bool ConfValue(const std::string &tag, const std::string &var, int index, std::string &result, bool allow_linefeeds = false);
615 /** Writes 'length' chars into 'result' as a string
617 bool ConfValue(const std::string &tag, const std::string &var, const std::string &default_value, int index, std::string &result, bool allow_linefeeds = false);
619 /** Tries to convert the value to an integer and write it to 'result'
621 bool ConfValueInteger(const char* tag, const char* var, int index, int &result);
623 /** Tries to convert the value to an integer and write it to 'result'
625 bool ConfValueInteger(const char* tag, const char* var, const char* default_value, int index, int &result);
627 /** Tries to convert the value to an integer and write it to 'result'
629 bool ConfValueInteger(const std::string &tag, const std::string &var, int index, int &result);
631 /** Tries to convert the value to an integer and write it to 'result'
633 bool ConfValueInteger(const std::string &tag, const std::string &var, const std::string &default_value, int index, int &result);
635 /** Returns true if the value exists and has a true value, false otherwise
637 bool ConfValueBool(const char* tag, const char* var, int index);
639 /** Returns true if the value exists and has a true value, false otherwise
641 bool ConfValueBool(const char* tag, const char* var, const char* default_value, int index);
643 /** Returns true if the value exists and has a true value, false otherwise
645 bool ConfValueBool(const std::string &tag, const std::string &var, int index);
647 /** Returns true if the value exists and has a true value, false otherwise
649 bool ConfValueBool(const std::string &tag, const std::string &var, const std::string &default_value, int index);
651 /** Returns the number of occurences of tag in the config file
653 int ConfValueEnum(const char* tag);
654 /** Returns the number of occurences of tag in the config file
656 int ConfValueEnum(const std::string &tag);
658 /** Returns the numbers of vars inside the index'th 'tag in the config file
660 int ConfVarEnum(const char* tag, int index);
661 /** Returns the numbers of vars inside the index'th 'tag in the config file
663 int ConfVarEnum(const std::string &tag, int index);
665 bool ApplyDisabledCommands(const std::string& data);
667 /** Clean a filename, stripping the directories (and drives) from string.
668 * @param name Directory to tidy
669 * @return The cleaned filename
671 static const char* CleanFilename(const char* name);
673 /** Check if a file exists.
674 * @param file The full path to a file
675 * @return True if the file exists and is readable.
677 static bool FileExists(const char* file);
679 /** If this value is true, invites will bypass more than just +i
686 /** Types of data in the core config
690 DT_NOTHING = 0, /* No data */
691 DT_INTEGER = 1, /* Integer */
692 DT_CHARPTR = 2, /* Char pointer */
693 DT_BOOLEAN = 3, /* Boolean */
694 DT_HOSTNAME = 4, /* Hostname syntax */
695 DT_NOSPACES = 5, /* No spaces */
696 DT_IPADDRESS = 6, /* IP address (v4, v6) */
697 DT_CHANNEL = 7, /* Channel name */
698 DT_LIMIT = 8, /* size_t */
699 DT_ALLOW_WILD = 64, /* Allow wildcards/CIDR in DT_IPADDRESS */
700 DT_ALLOW_NEWLINE = 128 /* New line characters allowed in DT_CHARPTR */
703 /** The maximum number of values in a core configuration tag. Can be increased if needed.
705 #define MAX_VALUES_PER_TAG 18
707 /** Holds a config value, either string, integer or boolean.
708 * Callback functions receive one or more of these, either on
709 * their own as a reference, or in a reference to a deque of them.
710 * The callback function can then alter the values of the ValueItem
711 * classes to validate the settings.
718 /** Initialize with an int */
719 ValueItem(int value);
720 /** Initialize with a bool */
721 ValueItem(bool value);
722 /** Initialize with a string */
723 ValueItem(const char* value) : v(value) { }
724 /** Change value to a string */
725 void Set(const std::string &val);
726 /** Change value to an int */
728 /** Get value as an int */
730 /** Get value as a string */
731 const char* GetString() const;
732 /** Get value as a string */
733 inline const std::string& GetValue() const { return v; }
734 /** Get value as a bool */
738 /** The base class of the container 'ValueContainer'
739 * used internally by the core to hold core values.
741 class ValueContainerBase
745 ValueContainerBase() { }
747 virtual ~ValueContainerBase() { }
750 /** ValueContainer is used to contain pointers to different
751 * core values such as the server name, maximum number of
753 * It is specialized to hold a data type, then pointed at
754 * a value in the ServerConfig class. When the value has been
755 * read and validated, the Set method is called to write the
756 * value safely in a type-safe manner.
758 template<typename T> class ValueContainer : public ValueContainerBase
760 T ServerConfig::* const vptr;
762 /** Initialize with a value of type T */
763 ValueContainer(T ServerConfig::* const offset) : vptr(offset)
767 /** Change value to type T of size s */
768 void Set(ServerConfig* conf, const T& value)
773 void Set(ServerConfig* conf, const ValueItem& item);
776 class ValueContainerLimit : public ValueContainerBase
778 size_t ServerLimits::* const vptr;
780 /** Initialize with a value of type T */
781 ValueContainerLimit(size_t ServerLimits::* const offset) : vptr(offset)
785 /** Change value to type T of size s */
786 void Set(ServerConfig* conf, const size_t& value)
788 conf->Limits.*vptr = value;
792 /** A specialization of ValueContainer to hold a pointer to a bool
794 typedef ValueContainer<bool> ValueContainerBool;
796 /** A specialization of ValueContainer to hold a pointer to
799 typedef ValueContainer<unsigned int> ValueContainerUInt;
801 /** A specialization of ValueContainer to hold a pointer to
804 typedef ValueContainer<std::string> ValueContainerString;
806 /** A specialization of ValueContainer to hold a pointer to
809 typedef ValueContainer<int> ValueContainerInt;
811 /** A set of ValueItems used by multi-value validator functions
813 typedef std::deque<ValueItem> ValueList;
815 /** A callback for validating a single value
817 typedef bool (*Validator)(ServerConfig* conf, const char*, const char*, ValueItem&);
818 /** A callback for validating multiple value entries
820 typedef bool (*MultiValidator)(ServerConfig* conf, const char*, const char**, ValueList&, int*);
821 /** A callback indicating the end of a group of entries
823 typedef bool (*MultiNotify)(ServerConfig* conf, const char*);
825 /** Holds a core configuration item and its callbacks
833 /** Default, if not defined */
834 const char* default_value;
835 /** Value containers */
836 ValueContainerBase* val;
839 /** Validation function */
840 Validator validation_function;
843 /** Represents a deprecated configuration tag.
853 /** Reason for deprecation
858 /** Holds a core configuration item and its callbacks
859 * where there may be more than one item
865 /** One or more items within tag */
866 const char* items[MAX_VALUES_PER_TAG];
867 /** One or more defaults for items within tags */
868 const char* items_default[MAX_VALUES_PER_TAG];
869 /** One or more data types */
870 int datatype[MAX_VALUES_PER_TAG];
871 /** Validation function */
872 MultiValidator validation_function;