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
19 /** Determines if a channel op is exempt from given mode m,
20 * in config of server instance s.
22 #define CHANOPS_EXEMPT(s, m) (s->Config->ExemptChanOps[(unsigned char)m])
30 #include "socketengine.h"
33 /* Required forward definitions */
38 /** Types of data in the core config
42 DT_NOTHING = 0, /* No data */
43 DT_INTEGER = 1, /* Integer */
44 DT_CHARPTR = 2, /* Char pointer */
45 DT_BOOLEAN = 3, /* Boolean */
46 DT_HOSTNAME = 4, /* Hostname syntax */
47 DT_NOSPACES = 5, /* No spaces */
48 DT_IPADDRESS = 6, /* IP address (v4, v6) */
49 DT_CHANNEL = 7, /* Channel name */
50 DT_ALLOW_WILD = 64, /* Allow wildcards/CIDR in DT_IPADDRESS */
51 DT_ALLOW_NEWLINE = 128 /* New line characters allowed in DT_CHARPTR */
54 /** The maximum number of values in a core configuration tag. Can be increased if needed.
56 #define MAX_VALUES_PER_TAG 18
58 /** Holds a config value, either string, integer or boolean.
59 * Callback functions receive one or more of these, either on
60 * their own as a reference, or in a reference to a deque of them.
61 * The callback function can then alter the values of the ValueItem
62 * classes to validate the settings.
69 /** Initialize with an int */
71 /** Initialize with a bool */
72 ValueItem(bool value);
73 /** Initialize with a char pointer */
74 ValueItem(const char* value);
75 /** Change value to a char pointer */
76 void Set(const char* val);
77 /** Change value to an int */
79 /** Get value as an int */
81 /** Get value as a string */
83 /** Get value as a bool */
87 /** The base class of the container 'ValueContainer'
88 * used internally by the core to hold core values.
90 class ValueContainerBase
94 ValueContainerBase() { }
96 virtual ~ValueContainerBase() { }
99 /** ValueContainer is used to contain pointers to different
100 * core values such as the server name, maximum number of
102 * It is specialized to hold a data type, then pointed at
103 * a value in the ServerConfig class. When the value has been
104 * read and validated, the Set method is called to write the
105 * value safely in a type-safe manner.
107 template<typename T> class ValueContainer : public ValueContainerBase
109 /** Contained item */
113 /** Initialize with nothing */
119 /** Initialize with a value of type T */
120 ValueContainer(T Val)
125 /** Change value to type T of size s */
126 void Set(T newval, size_t s)
128 memcpy(val, newval, s);
132 /** A specialization of ValueContainer to hold a pointer to a bool
134 typedef ValueContainer<bool*> ValueContainerBool;
136 /** A specialization of ValueContainer to hold a pointer to
139 typedef ValueContainer<unsigned int*> ValueContainerUInt;
141 /** A specialization of ValueContainer to hold a pointer to
144 typedef ValueContainer<char*> ValueContainerChar;
146 /** A specialization of ValueContainer to hold a pointer to
149 typedef ValueContainer<int*> ValueContainerInt;
151 /** A specialization of ValueContainer to hold a pointer to
154 typedef ValueContainer<size_t*> ValueContainerST;
156 /** A set of ValueItems used by multi-value validator functions
158 typedef std::deque<ValueItem> ValueList;
160 /** A callback for validating a single value
162 typedef bool (*Validator)(ServerConfig* conf, const char*, const char*, ValueItem&);
163 /** A callback for validating multiple value entries
165 typedef bool (*MultiValidator)(ServerConfig* conf, const char*, char**, ValueList&, int*);
166 /** A callback indicating the end of a group of entries
168 typedef bool (*MultiNotify)(ServerConfig* conf, const char*);
170 /** Holds a core configuration item and its callbacks
178 /** Default, if not defined */
179 const char* default_value;
180 /** Value containers */
181 ValueContainerBase* val;
184 /** Validation function */
185 Validator validation_function;
188 /** Represents a deprecated configuration tag.
198 /** Reason for deprecation
203 /** Holds a core configuration item and its callbacks
204 * where there may be more than one item
210 /** One or more items within tag */
211 const char* items[MAX_VALUES_PER_TAG];
212 /** One or more defaults for items within tags */
213 const char* items_default[MAX_VALUES_PER_TAG];
214 /** One or more data types */
215 int datatype[MAX_VALUES_PER_TAG];
216 /** Initialization function */
217 MultiNotify init_function;
218 /** Validation function */
219 MultiValidator validation_function;
220 /** Completion function */
221 MultiNotify finish_function;
224 /** A set of oper types
226 typedef std::map<irc::string,std::string> opertype_t;
228 /** Holds an oper class.
230 struct operclass_data : public Extensible
232 /** Command list for the class
236 /** Channel mode list for the class
240 /** User mode list for the class
244 /** Priviledges given by this class
249 /** A Set of oper classes
251 typedef std::map<irc::string, operclass_data> operclass_t;
253 /** Defines the server's length limits on various length-limited
254 * items such as topics, nicknames, channel names etc.
256 class ServerLimits : public Extensible
259 /** Maximum nickname length */
261 /** Maximum channel length */
263 /** Maximum number of modes per line */
265 /** Maximum length of ident, not including ~ etc */
267 /** Maximum length of a quit message */
269 /** Maximum topic length */
271 /** Maximum kick message length */
273 /** Maximum GECOS (real name) length */
275 /** Maximum away message length */
278 /** Creating the class initialises it to the defaults
279 * as in 1.1's ./configure script. Reading other values
280 * from the config will change these values.
282 ServerLimits() : NickMax(31), ChanMax(64), MaxModes(20), IdentMax(12), MaxQuit(255), MaxTopic(307), MaxKick(255), MaxGecos(128), MaxAway(200)
286 /** Finalises the settings by adding one. This allows for them to be used as-is
287 * without a 'value+1' when using the std::string assignment methods etc.
302 /** This class holds the bulk of the runtime configuration for the ircd.
303 * It allows for reading new config values, accessing configuration files,
304 * and storage of the configuration data needed to run the ircd, such as
305 * the servername, connect classes, /ADMIN data, MOTDs and filenames etc.
307 class CoreExport ServerConfig : public Extensible
310 /** Creator/owner pointer
312 InspIRCd* ServerInstance;
314 /** This variable holds the names of all
315 * files included from the main one. This
316 * is used to make sure that no files are
317 * recursively included.
319 std::vector<std::string> include_stack;
321 /** This private method processes one line of
322 * configutation, appending errors to errorstream
323 * and setting error if an error has occured.
325 bool ParseLine(const std::string &filename, std::string &line, long &linenumber);
327 /** Check that there is only one of each configuration item
329 bool CheckOnce(const char* tag);
332 /** Process an include executable directive
334 bool DoPipe(const std::string &file);
336 /** Process an include file directive
338 bool DoInclude(const std::string &file);
340 /** Error stream, contains error output from any failed configuration parsing.
342 std::stringstream errstr;
344 /** True if this configuration is valid enough to run with */
347 /** Set of included files. Do we use this any more?
349 std::map<std::string, std::istream*> IncludedFiles;
351 /** Used to indicate who we announce invites to on a channel */
352 enum InviteAnnounceState { INVITE_ANNOUNCE_NONE, INVITE_ANNOUNCE_ALL, INVITE_ANNOUNCE_OPS, INVITE_ANNOUNCE_DYNAMIC };
354 /** Pointer to function that validates dns server addresses (can be changed depending on platform) */
355 Validator DNSServerValidator;
357 /** Returns the creator InspIRCd pointer
359 InspIRCd* GetInstance();
361 /** Not used any more as it is named, can probably be removed or renamed.
365 /** This holds all the information in the config file,
366 * it's indexed by tag name to a vector of key/values.
368 ConfigDataHash config_data;
370 /** Length limits, see definition of ServerLimits class
374 /** Clones CIDR range for ipv4 (0-32)
375 * Defaults to 32 (checks clones on all IPs seperately)
379 /** Clones CIDR range for ipv6 (0-128)
380 * Defaults to 128 (checks on all IPs seperately)
384 /** Max number of WhoWas entries per user.
388 /** Max number of cumulative user-entries in WhoWas.
389 * When max reached and added to, push out oldest entry FIFO style.
393 /** Max seconds a user is kept in WhoWas before being pruned.
397 /** Both for set(g|u)id.
399 char SetUser[MAXBUF];
400 char SetGroup[MAXBUF];
402 /** Holds the server name of the local server
403 * as defined by the administrator.
405 char ServerName[MAXBUF];
407 /** Notice to give to users when they are Xlined
409 char MoronBanner[MAXBUF];
411 /* Holds the network name the local server
412 * belongs to. This is an arbitary field defined
413 * by the administrator.
415 char Network[MAXBUF];
417 /** Holds the description of the local server
418 * as defined by the administrator.
420 char ServerDesc[MAXBUF];
422 /** Holds the admin's name, for output in
423 * the /ADMIN command.
425 char AdminName[MAXBUF];
427 /** Holds the email address of the admin,
428 * for output in the /ADMIN command.
430 char AdminEmail[MAXBUF];
432 /** Holds the admin's nickname, for output
433 * in the /ADMIN command
435 char AdminNick[MAXBUF];
437 /** The admin-configured /DIE password
439 char diepass[MAXBUF];
441 /** The admin-configured /RESTART password
443 char restartpass[MAXBUF];
445 /** The hash method for *BOTH* the die and restart passwords.
447 char powerhash[MAXBUF];
449 /** The pathname and filename of the message of the
450 * day file, as defined by the administrator.
454 /** The pathname and filename of the rules file,
455 * as defined by the administrator.
459 /** The quit prefix in use, or an empty string
461 char PrefixQuit[MAXBUF];
463 /** The quit suffix in use, or an empty string
465 char SuffixQuit[MAXBUF];
467 /** The fixed quit message in use, or an empty string
469 char FixedQuit[MAXBUF];
471 /** The part prefix in use, or an empty string
473 char PrefixPart[MAXBUF];
475 /** The part suffix in use, or an empty string
477 char SuffixPart[MAXBUF];
479 /** The fixed part message in use, or an empty string
481 char FixedPart[MAXBUF];
483 /** The last string found within a <die> tag, or
486 char DieValue[MAXBUF];
488 /** The DNS server to use for DNS queries
490 char DNSServer[MAXBUF];
492 /** Pretend disabled commands don't exist.
494 bool DisabledDontExist;
496 /** This variable contains a space-seperated list
497 * of commands which are disabled by the
498 * administrator of the server for non-opers.
500 char DisabledCommands[MAXBUF];
502 /** This variable identifies which usermodes have been diabled.
505 char DisabledUModes[64];
507 /** This variable identifies which chanmodes have been disabled.
509 char DisabledCModes[64];
511 /** The full path to the modules directory.
512 * This is either set at compile time, or
513 * overridden in the configuration file via
518 /** The full pathname to the executable, as
519 * given in argv[0] when the program starts.
521 char MyExecutable[1024];
523 /** The file handle of the logfile. If this
524 * value is NULL, the log file is not open,
525 * probably due to a permissions error on
526 * startup (this should not happen in normal
531 /** If this value is true, the owner of the
532 * server specified -nofork on the command
533 * line, causing the daemon to stay in the
538 /** If this value if true then all log
539 * messages will be output, regardless of
540 * the level given in the config file.
541 * This is set with the -debug commandline
546 /** If this is true then log output will be
547 * written to the logfile. This is the default.
548 * If you put -nolog on the commandline then
549 * the logfile will not be written.
550 * This is meant to be used in conjunction with
551 * -debug for debugging without filling up the
556 /** If set to true, then all opers on this server are
557 * shown with a generic 'is an IRC operator' line rather
558 * than the oper type. Oper types are still used internally.
562 /** If this value is true, banned users (+b, not extbans) will not be able to change nick
563 * if banned on any channel, nor to message them.
565 bool RestrictBannedUsers;
567 /** If this value is true, halfops have been
568 * enabled in the configuration file.
572 /** If this is set to true, then mode lists (e.g
573 * MODE #chan b) are hidden from unprivileged
576 bool HideModeLists[256];
578 /** If this is set to true, then channel operators
579 * are exempt from this channel mode. Used for +Sc etc.
581 bool ExemptChanOps[256];
583 /** The number of seconds the DNS subsystem
584 * will wait before timing out any request.
588 /** The size of the read() buffer in the user
589 * handling code, used to read data into a user's
594 /** The value to be used for listen() backlogs
599 /** The soft limit value assigned to the irc server.
600 * The IRC server will not allow more than this
601 * number of local users.
603 unsigned int SoftLimit;
605 /** Maximum number of targets for a multi target command
606 * such as PRIVMSG or KICK
608 unsigned int MaxTargets;
610 /** The maximum number of /WHO results allowed
611 * in any single /WHO command.
615 /** True if the DEBUG loglevel is selected.
619 /** How many seconds to wait before exiting
620 * the program when /DIE is correctly issued.
624 /** True if we're going to hide netsplits as *.net *.split for non-opers
628 /** True if we're going to hide ban reasons for non-opers (e.g. G-Lines,
633 /** Announce invites to the channel with a server notice
635 InviteAnnounceState AnnounceInvites;
637 /** If this is enabled then operators will
638 * see invisible (+i) channels in /whois.
642 /** Set to a non-empty string to obfuscate the server name of users in WHOIS
644 char HideWhoisServer[MAXBUF];
646 /** Set to a non empty string to obfuscate nicknames prepended to a KILL.
648 char HideKillsServer[MAXBUF];
650 /** The MOTD file, cached in a file_cache type.
654 /** The RULES file, cached in a file_cache type.
658 /** The full pathname and filename of the PID
659 * file as defined in the configuration.
663 /** The connect classes in use by the IRC server.
667 /** The 005 tokens of this server (ISUPPORT)
668 * populated/repopulated upon loading or unloading
675 std::vector<std::string> isupport;
677 /** STATS characters in this list are available
680 char UserStats[MAXBUF];
682 /** The path and filename of the ircd.log file
686 /** Default channel modes
688 char DefaultModes[MAXBUF];
690 /** Custom version string, which if defined can replace the system info in VERSION.
692 char CustomVersion[MAXBUF];
694 /** List of u-lined servers
696 std::map<irc::string, bool> ulines;
698 /** Max banlist sizes for channels (the std::string is a glob)
700 std::map<std::string, int> maxbans;
702 /** Directory where the inspircd binary resides
706 /** If set to true, no user DNS lookups are to be performed
710 /** If set to true, provide syntax hints for unknown commands
714 /** If set to true, users appear to quit then rejoin when their hosts change.
715 * This keeps clients synchronized properly.
719 /** If set to true, prefixed channel NOTICEs and PRIVMSGs will have the prefix
720 * added to the outgoing text for undernet style msg prefixing.
722 bool UndernetMsgPrefix;
724 /** If set to true, the full nick!user@host will be shown in the TOPIC command
725 * for who set the topic last. If false, only the nick is shown.
727 bool FullHostInTopic;
729 /** All oper type definitions from the config file
731 opertype_t opertypes;
733 /** All oper class definitions from the config file
735 operclass_t operclass;
737 /** Saved argv from startup
741 /** Saved argc from startup
745 /** Max channels per user
747 unsigned int MaxChans;
749 /** Oper max channels per user
751 unsigned int OperMaxChans;
753 /** TS6-like server ID.
754 * NOTE: 000...999 are usable for InspIRCd servers. This
755 * makes code simpler. 0AA, 1BB etc with letters are reserved
760 /** True if we have been told to run the testsuite from the commandline,
761 * rather than entering the mainloop.
765 /** Construct a new ServerConfig
767 ServerConfig(InspIRCd* Instance);
769 /** Clears the include stack in preperation for a Read() call.
773 /** Get server ID as string with required leading zeroes
775 std::string GetSID();
777 /** Update the 005 vector
781 /** Send the 005 numerics (ISUPPORT) to a user
783 void Send005(User* user);
785 /** Read the entire configuration into memory
786 * and initialize this class. All other methods
787 * should be used only by the core.
791 /** Apply configuration changes from the old configuration.
793 void Apply(ServerConfig* old, const std::string &useruid);
795 /** Read a file into a file_cache object
797 bool ReadFile(file_cache &F, const char* fname);
799 /* Returns true if the given string starts with a windows drive letter
801 bool StartsWithWindowsDriveLetter(const std::string &path);
803 /** Load 'filename' into 'target', with the new config parser everything is parsed into
804 * tag/key/value at load-time rather than at read-value time.
806 bool LoadConf(FILE* &conf, const char* filename);
808 /** Load 'filename' into 'target', with the new config parser everything is parsed into
809 * tag/key/value at load-time rather than at read-value time.
811 bool LoadConf(FILE* &conf, const std::string &filename);
813 /** Writes 'length' chars into 'result' as a string
815 bool ConfValue(const char* tag, const char* var, int index, char* result, int length, bool allow_linefeeds = false);
817 /** Writes 'length' chars into 'result' as a string
819 bool ConfValue(const char* tag, const char* var, const char* default_value, int index, char* result, int length, bool allow_linefeeds = false);
821 /** Writes 'length' chars into 'result' as a string
823 bool ConfValue(const std::string &tag, const std::string &var, int index, std::string &result, bool allow_linefeeds = false);
825 /** Writes 'length' chars into 'result' as a string
827 bool ConfValue(const std::string &tag, const std::string &var, const std::string &default_value, int index, std::string &result, bool allow_linefeeds = false);
829 /** Tries to convert the value to an integer and write it to 'result'
831 bool ConfValueInteger(const char* tag, const char* var, int index, int &result);
833 /** Tries to convert the value to an integer and write it to 'result'
835 bool ConfValueInteger(const char* tag, const char* var, const char* default_value, int index, int &result);
837 /** Tries to convert the value to an integer and write it to 'result'
839 bool ConfValueInteger(const std::string &tag, const std::string &var, int index, int &result);
841 /** Tries to convert the value to an integer and write it to 'result'
843 bool ConfValueInteger(const std::string &tag, const std::string &var, const std::string &default_value, int index, int &result);
845 /** Returns true if the value exists and has a true value, false otherwise
847 bool ConfValueBool(const char* tag, const char* var, int index);
849 /** Returns true if the value exists and has a true value, false otherwise
851 bool ConfValueBool(const char* tag, const char* var, const char* default_value, int index);
853 /** Returns true if the value exists and has a true value, false otherwise
855 bool ConfValueBool(const std::string &tag, const std::string &var, int index);
857 /** Returns true if the value exists and has a true value, false otherwise
859 bool ConfValueBool(const std::string &tag, const std::string &var, const std::string &default_value, int index);
861 /** Returns the number of occurences of tag in the config file
863 int ConfValueEnum(const char* tag);
864 /** Returns the number of occurences of tag in the config file
866 int ConfValueEnum(const std::string &tag);
868 /** Returns the numbers of vars inside the index'th 'tag in the config file
870 int ConfVarEnum(const char* tag, int index);
871 /** Returns the numbers of vars inside the index'th 'tag in the config file
873 int ConfVarEnum(const std::string &tag, int index);
875 /** Validates a hostname value, throwing ConfigException if it is not valid
877 void ValidateHostname(const char* p, const std::string &tag, const std::string &val);
879 /** Validates an IP address value, throwing ConfigException if it is not valid
881 void ValidateIP(const char* p, const std::string &tag, const std::string &val, bool wild);
883 /** Validates a value that should not contain spaces, throwing ConfigException of it is not valid
885 void ValidateNoSpaces(const char* p, const std::string &tag, const std::string &val);
887 /** Returns the fully qualified path to the inspircd directory
888 * @return The full program directory
890 std::string GetFullProgDir();
892 /** Clean a filename, stripping the directories (and drives) from string.
893 * @param name Directory to tidy
894 * @return The cleaned filename
896 static char* CleanFilename(char* name);
898 /** Check if a file exists.
899 * @param file The full path to a file
900 * @return True if the file exists and is readable.
902 static bool FileExists(const char* file);
904 /** If this value is true, invites will bypass more than just +i
910 /** Initialize the disabled commands list
912 CoreExport bool InitializeDisabledCommands(const char* data, InspIRCd* ServerInstance);
914 /** Initialize the oper types
916 bool InitTypes(ServerConfig* conf, const char* tag);
918 /** Initialize the oper classes
920 bool InitClasses(ServerConfig* conf, const char* tag);
922 /** Initialize an oper type
924 bool DoType(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
926 /** Initialize an oper class
928 bool DoClass(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
930 /** Finish initializing the oper types and classes
932 bool DoneClassesAndTypes(ServerConfig* conf, const char* tag);
936 /** Initialize x line
938 bool InitXLine(ServerConfig* conf, const char* tag);
940 /** Add a config-defined zline
942 bool DoZLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
944 /** Add a config-defined qline
946 bool DoQLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
948 /** Add a config-defined kline
950 bool DoKLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
952 /** Add a config-defined eline
954 bool DoELine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);