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 */
52 DT_BOOTONLY = 256 /* Can only be set on startup, not on rehash */
55 /** The maximum number of values in a core configuration tag. Can be increased if needed.
57 #define MAX_VALUES_PER_TAG 18
59 /** Holds a config value, either string, integer or boolean.
60 * Callback functions receive one or more of these, either on
61 * their own as a reference, or in a reference to a deque of them.
62 * The callback function can then alter the values of the ValueItem
63 * classes to validate the settings.
70 /** Initialize with an int */
72 /** Initialize with a bool */
73 ValueItem(bool value);
74 /** Initialize with a char pointer */
75 ValueItem(const char* value);
76 /** Change value to a char pointer */
77 void Set(const char* val);
78 /** Change value to an int */
80 /** Get value as an int */
82 /** Get value as a string */
84 /** Get value as a bool */
88 /** The base class of the container 'ValueContainer'
89 * used internally by the core to hold core values.
91 class ValueContainerBase
95 ValueContainerBase() { }
97 virtual ~ValueContainerBase() { }
100 /** ValueContainer is used to contain pointers to different
101 * core values such as the server name, maximum number of
103 * It is specialized to hold a data type, then pointed at
104 * a value in the ServerConfig class. When the value has been
105 * read and validated, the Set method is called to write the
106 * value safely in a type-safe manner.
108 template<typename T> class ValueContainer : public ValueContainerBase
110 /** Contained item */
114 /** Initialize with nothing */
120 /** Initialize with a value of type T */
121 ValueContainer(T Val)
126 /** Change value to type T of size s */
127 void Set(T newval, size_t s)
129 memcpy(val, newval, s);
133 /** A specialization of ValueContainer to hold a pointer to a bool
135 typedef ValueContainer<bool*> ValueContainerBool;
137 /** A specialization of ValueContainer to hold a pointer to
140 typedef ValueContainer<unsigned int*> ValueContainerUInt;
142 /** A specialization of ValueContainer to hold a pointer to
145 typedef ValueContainer<char*> ValueContainerChar;
147 /** A specialization of ValueContainer to hold a pointer to
150 typedef ValueContainer<int*> ValueContainerInt;
152 /** A specialization of ValueContainer to hold a pointer to
155 typedef ValueContainer<size_t*> ValueContainerST;
157 /** A set of ValueItems used by multi-value validator functions
159 typedef std::deque<ValueItem> ValueList;
161 /** A callback for validating a single value
163 typedef bool (*Validator)(ServerConfig* conf, const char*, const char*, ValueItem&);
164 /** A callback for validating multiple value entries
166 typedef bool (*MultiValidator)(ServerConfig* conf, const char*, char**, ValueList&, int*);
167 /** A callback indicating the end of a group of entries
169 typedef bool (*MultiNotify)(ServerConfig* conf, const char*);
171 /** Holds a core configuration item and its callbacks
179 /** Default, if not defined */
180 const char* default_value;
181 /** Value containers */
182 ValueContainerBase* val;
185 /** Validation function */
186 Validator validation_function;
189 /** Represents a deprecated configuration tag.
199 /** Reason for deprecation
204 /** Holds a core configuration item and its callbacks
205 * where there may be more than one item
211 /** One or more items within tag */
212 const char* items[MAX_VALUES_PER_TAG];
213 /** One or more defaults for items within tags */
214 const char* items_default[MAX_VALUES_PER_TAG];
215 /** One or more data types */
216 int datatype[MAX_VALUES_PER_TAG];
217 /** Initialization function */
218 MultiNotify init_function;
219 /** Validation function */
220 MultiValidator validation_function;
221 /** Completion function */
222 MultiNotify finish_function;
225 /** A set of oper types
227 typedef std::map<irc::string,std::string> opertype_t;
229 /** Holds an oper class.
231 struct operclass_data : public Extensible
233 /** Command list for the class
237 /** Channel mode list for the class
241 /** User mode list for the class
245 /** Priviledges given by this class
250 /** A Set of oper classes
252 typedef std::map<irc::string, operclass_data> operclass_t;
254 /** Defines the server's length limits on various length-limited
255 * items such as topics, nicknames, channel names etc.
257 class ServerLimits : public Extensible
260 /** Maximum nickname length */
262 /** Maximum channel length */
264 /** Maximum number of modes per line */
266 /** Maximum length of ident, not including ~ etc */
268 /** Maximum length of a quit message */
270 /** Maximum topic length */
272 /** Maximum kick message length */
274 /** Maximum GECOS (real name) length */
276 /** Maximum away message length */
279 /** Creating the class initialises it to the defaults
280 * as in 1.1's ./configure script. Reading other values
281 * from the config will change these values.
283 ServerLimits() : NickMax(31), ChanMax(64), MaxModes(20), IdentMax(12), MaxQuit(255), MaxTopic(307), MaxKick(255), MaxGecos(128), MaxAway(200)
287 /** Finalises the settings by adding one. This allows for them to be used as-is
288 * without a 'value+1' when using the std::string assignment methods etc.
303 /** This class holds the bulk of the runtime configuration for the ircd.
304 * It allows for reading new config values, accessing configuration files,
305 * and storage of the configuration data needed to run the ircd, such as
306 * the servername, connect classes, /ADMIN data, MOTDs and filenames etc.
308 class CoreExport ServerConfig : public Extensible
311 /** Creator/owner pointer
313 InspIRCd* ServerInstance;
315 /** This variable holds the names of all
316 * files included from the main one. This
317 * is used to make sure that no files are
318 * recursively included.
320 std::vector<std::string> include_stack;
322 /** This private method processes one line of
323 * configutation, appending errors to errorstream
324 * and setting error if an error has occured.
326 bool ParseLine(ConfigDataHash &target, const std::string &filename, std::string &line, long &linenumber, std::ostringstream &errorstream);
328 /** Check that there is only one of each configuration item
330 bool CheckOnce(const char* tag, ConfigDataHash &newconf);
333 /** Process an include executable directive
335 bool DoPipe(ConfigDataHash &target, const std::string &file, std::ostringstream &errorstream);
337 /** Process an include file directive
339 bool DoInclude(ConfigDataHash &target, const std::string &file, std::ostringstream &errorstream);
341 /** User that is currently performing a rehash, needed because the
342 * rehash code is now threaded and needs to know who to give errors and feedback to.
344 std::string RehashUserUID;
346 /** Error stream, contains error output from any failed configuration parsing.
348 std::ostringstream* errstr;
350 /** Holds the new configuration when a rehash occurs so we dont overwrite the existing
351 * working config with a broken one without checking it first and swapping pointers.
353 ConfigDataHash newconfig;
355 /** Set of included files. Do we use this any more?
357 std::map<std::string, std::istream*> IncludedFiles;
359 /** Used to indicate who we announce invites to on a channel */
360 enum InviteAnnounceState { INVITE_ANNOUNCE_NONE, INVITE_ANNOUNCE_ALL, INVITE_ANNOUNCE_OPS, INVITE_ANNOUNCE_DYNAMIC };
362 /** Pointer to function that validates dns server addresses (can be changed depending on platform) */
363 Validator DNSServerValidator;
365 /** Returns the creator InspIRCd pointer
367 InspIRCd* GetInstance();
369 /** Not used any more as it is named, can probably be removed or renamed.
373 /** This holds all the information in the config file,
374 * it's indexed by tag name to a vector of key/values.
376 ConfigDataHash config_data;
378 /** Length limits, see definition of ServerLimits class
382 /** Clones CIDR range for ipv4 (0-32)
383 * Defaults to 32 (checks clones on all IPs seperately)
387 /** Clones CIDR range for ipv6 (0-128)
388 * Defaults to 128 (checks on all IPs seperately)
392 /** Max number of WhoWas entries per user.
396 /** Max number of cumulative user-entries in WhoWas.
397 * When max reached and added to, push out oldest entry FIFO style.
401 /** Max seconds a user is kept in WhoWas before being pruned.
405 /** Both for set(g|u)id.
407 char SetUser[MAXBUF];
408 char SetGroup[MAXBUF];
410 /** Holds the server name of the local server
411 * as defined by the administrator.
413 char ServerName[MAXBUF];
415 /** Notice to give to users when they are Xlined
417 char MoronBanner[MAXBUF];
419 /* Holds the network name the local server
420 * belongs to. This is an arbitary field defined
421 * by the administrator.
423 char Network[MAXBUF];
425 /** Holds the description of the local server
426 * as defined by the administrator.
428 char ServerDesc[MAXBUF];
430 /** Holds the admin's name, for output in
431 * the /ADMIN command.
433 char AdminName[MAXBUF];
435 /** Holds the email address of the admin,
436 * for output in the /ADMIN command.
438 char AdminEmail[MAXBUF];
440 /** Holds the admin's nickname, for output
441 * in the /ADMIN command
443 char AdminNick[MAXBUF];
445 /** The admin-configured /DIE password
447 char diepass[MAXBUF];
449 /** The admin-configured /RESTART password
451 char restartpass[MAXBUF];
453 /** The hash method for *BOTH* the die and restart passwords.
455 char powerhash[MAXBUF];
457 /** The pathname and filename of the message of the
458 * day file, as defined by the administrator.
462 /** The pathname and filename of the rules file,
463 * as defined by the administrator.
467 /** The quit prefix in use, or an empty string
469 char PrefixQuit[MAXBUF];
471 /** The quit suffix in use, or an empty string
473 char SuffixQuit[MAXBUF];
475 /** The fixed quit message in use, or an empty string
477 char FixedQuit[MAXBUF];
479 /** The part prefix in use, or an empty string
481 char PrefixPart[MAXBUF];
483 /** The part suffix in use, or an empty string
485 char SuffixPart[MAXBUF];
487 /** The fixed part message in use, or an empty string
489 char FixedPart[MAXBUF];
491 /** The last string found within a <die> tag, or
494 char DieValue[MAXBUF];
496 /** The DNS server to use for DNS queries
498 char DNSServer[MAXBUF];
500 /** Pretend disabled commands don't exist.
502 bool DisabledDontExist;
504 /** This variable contains a space-seperated list
505 * of commands which are disabled by the
506 * administrator of the server for non-opers.
508 char DisabledCommands[MAXBUF];
510 /** This variable identifies which usermodes have been diabled.
513 char DisabledUModes[64];
515 /** This variable identifies which chanmodes have been disabled.
517 char DisabledCModes[64];
519 /** The full path to the modules directory.
520 * This is either set at compile time, or
521 * overridden in the configuration file via
526 /** The full pathname to the executable, as
527 * given in argv[0] when the program starts.
529 char MyExecutable[1024];
531 /** The file handle of the logfile. If this
532 * value is NULL, the log file is not open,
533 * probably due to a permissions error on
534 * startup (this should not happen in normal
539 /** If this value is true, the owner of the
540 * server specified -nofork on the command
541 * line, causing the daemon to stay in the
546 /** If this value if true then all log
547 * messages will be output, regardless of
548 * the level given in the config file.
549 * This is set with the -debug commandline
554 /** If this is true then log output will be
555 * written to the logfile. This is the default.
556 * If you put -nolog on the commandline then
557 * the logfile will not be written.
558 * This is meant to be used in conjunction with
559 * -debug for debugging without filling up the
564 /** If set to true, then all opers on this server are
565 * shown with a generic 'is an IRC operator' line rather
566 * than the oper type. Oper types are still used internally.
570 /** If this value is true, banned users (+b, not extbans) will not be able to change nick
571 * if banned on any channel, nor to message them.
573 bool RestrictBannedUsers;
575 /** If this value is true, halfops have been
576 * enabled in the configuration file.
580 /** If this is set to true, then mode lists (e.g
581 * MODE #chan b) are hidden from unprivileged
584 bool HideModeLists[256];
586 /** If this is set to true, then channel operators
587 * are exempt from this channel mode. Used for +Sc etc.
589 bool ExemptChanOps[256];
591 /** The number of seconds the DNS subsystem
592 * will wait before timing out any request.
596 /** The size of the read() buffer in the user
597 * handling code, used to read data into a user's
602 /** The value to be used for listen() backlogs
607 /** The soft limit value assigned to the irc server.
608 * The IRC server will not allow more than this
609 * number of local users.
611 unsigned int SoftLimit;
613 /** Maximum number of targets for a multi target command
614 * such as PRIVMSG or KICK
616 unsigned int MaxTargets;
618 /** The maximum number of /WHO results allowed
619 * in any single /WHO command.
623 /** True if the DEBUG loglevel is selected.
627 /** How many seconds to wait before exiting
628 * the program when /DIE is correctly issued.
632 /** True if we're going to hide netsplits as *.net *.split for non-opers
636 /** True if we're going to hide ban reasons for non-opers (e.g. G-Lines,
641 /** Announce invites to the channel with a server notice
643 InviteAnnounceState AnnounceInvites;
645 /** If this is enabled then operators will
646 * see invisible (+i) channels in /whois.
650 /** Set to a non-empty string to obfuscate the server name of users in WHOIS
652 char HideWhoisServer[MAXBUF];
654 /** Set to a non empty string to obfuscate nicknames prepended to a KILL.
656 char HideKillsServer[MAXBUF];
658 /** The MOTD file, cached in a file_cache type.
662 /** The RULES file, cached in a file_cache type.
666 /** The full pathname and filename of the PID
667 * file as defined in the configuration.
671 /** The connect classes in use by the IRC server.
675 /** A list of the classes for listening ports
677 std::vector<ListenSocketBase *> ports;
679 /** The 005 tokens of this server (ISUPPORT)
680 * populated/repopulated upon loading or unloading
687 std::vector<std::string> isupport;
689 /** STATS characters in this list are available
692 char UserStats[MAXBUF];
694 /** The path and filename of the ircd.log file
698 /** Default channel modes
700 char DefaultModes[MAXBUF];
702 /** Custom version string, which if defined can replace the system info in VERSION.
704 char CustomVersion[MAXBUF];
706 /** List of u-lined servers
708 std::map<irc::string, bool> ulines;
710 /** Max banlist sizes for channels (the std::string is a glob)
712 std::map<std::string, int> maxbans;
714 /** Directory where the inspircd binary resides
718 /** If set to true, no user DNS lookups are to be performed
722 /** If set to true, provide syntax hints for unknown commands
726 /** If set to true, users appear to quit then rejoin when their hosts change.
727 * This keeps clients synchronized properly.
731 /** If set to true, prefixed channel NOTICEs and PRIVMSGs will have the prefix
732 * added to the outgoing text for undernet style msg prefixing.
734 bool UndernetMsgPrefix;
736 /** If set to true, the full nick!user@host will be shown in the TOPIC command
737 * for who set the topic last. If false, only the nick is shown.
739 bool FullHostInTopic;
741 /** All oper type definitions from the config file
743 opertype_t opertypes;
745 /** All oper class definitions from the config file
747 operclass_t operclass;
749 /** Saved argv from startup
753 /** Saved argc from startup
757 /** Max channels per user
759 unsigned int MaxChans;
761 /** Oper max channels per user
763 unsigned int OperMaxChans;
765 /** TS6-like server ID.
766 * NOTE: 000...999 are usable for InspIRCd servers. This
767 * makes code simpler. 0AA, 1BB etc with letters are reserved
772 /** True if we have been told to run the testsuite from the commandline,
773 * rather than entering the mainloop.
777 /** Construct a new ServerConfig
779 ServerConfig(InspIRCd* Instance);
781 /** Clears the include stack in preperation for a Read() call.
785 /** Get server ID as string with required leading zeroes
787 std::string GetSID();
789 /** Update the 005 vector
793 /** Send the 005 numerics (ISUPPORT) to a user
795 void Send005(User* user);
797 /** Read the entire configuration into memory
798 * and initialize this class. All other methods
799 * should be used only by the core.
801 void Read(bool bail, const std::string &useruid);
803 /** Read a file into a file_cache object
805 bool ReadFile(file_cache &F, const char* fname);
807 /* Returns true if the given string starts with a windows drive letter
809 bool StartsWithWindowsDriveLetter(const std::string &path);
811 /** Report a configuration error given in errormessage.
812 * @param bail If this is set to true, the error is sent to the console, and the program exits
813 * @param useruid If this is set to a non-empty value which is a valid UID, and bail is false,
814 * the errors are spooled to this user as SNOTICEs.
815 * If the parameter is not a valid UID, the messages are spooled to all opers.
817 void ReportConfigError(const std::string &errormessage, bool bail, const std::string &useruid);
819 /** Load 'filename' into 'target', with the new config parser everything is parsed into
820 * tag/key/value at load-time rather than at read-value time.
822 bool LoadConf(ConfigDataHash &target, FILE* &conf, const char* filename, std::ostringstream &errorstream);
824 /** Load 'filename' into 'target', with the new config parser everything is parsed into
825 * tag/key/value at load-time rather than at read-value time.
827 bool LoadConf(ConfigDataHash &target, FILE* &conf, const std::string &filename, std::ostringstream &errorstream);
829 /** Writes 'length' chars into 'result' as a string
831 bool ConfValue(ConfigDataHash &target, const char* tag, const char* var, int index, char* result, int length, bool allow_linefeeds = false);
833 /** Writes 'length' chars into 'result' as a string
835 bool ConfValue(ConfigDataHash &target, const char* tag, const char* var, const char* default_value, int index, char* result, int length, bool allow_linefeeds = false);
837 /** Writes 'length' chars into 'result' as a string
839 bool ConfValue(ConfigDataHash &target, const std::string &tag, const std::string &var, int index, std::string &result, bool allow_linefeeds = false);
841 /** Writes 'length' chars into 'result' as a string
843 bool ConfValue(ConfigDataHash &target, const std::string &tag, const std::string &var, const std::string &default_value, int index, std::string &result, bool allow_linefeeds = false);
845 /** Tries to convert the value to an integer and write it to 'result'
847 bool ConfValueInteger(ConfigDataHash &target, const char* tag, const char* var, int index, int &result);
849 /** Tries to convert the value to an integer and write it to 'result'
851 bool ConfValueInteger(ConfigDataHash &target, const char* tag, const char* var, const char* default_value, int index, int &result);
853 /** Tries to convert the value to an integer and write it to 'result'
855 bool ConfValueInteger(ConfigDataHash &target, const std::string &tag, const std::string &var, int index, int &result);
857 /** Tries to convert the value to an integer and write it to 'result'
859 bool ConfValueInteger(ConfigDataHash &target, const std::string &tag, const std::string &var, const std::string &default_value, int index, int &result);
861 /** Returns true if the value exists and has a true value, false otherwise
863 bool ConfValueBool(ConfigDataHash &target, const char* tag, const char* var, int index);
865 /** Returns true if the value exists and has a true value, false otherwise
867 bool ConfValueBool(ConfigDataHash &target, const char* tag, const char* var, const char* default_value, int index);
869 /** Returns true if the value exists and has a true value, false otherwise
871 bool ConfValueBool(ConfigDataHash &target, const std::string &tag, const std::string &var, int index);
873 /** Returns true if the value exists and has a true value, false otherwise
875 bool ConfValueBool(ConfigDataHash &target, const std::string &tag, const std::string &var, const std::string &default_value, int index);
877 /** Returns the number of occurences of tag in the config file
879 int ConfValueEnum(ConfigDataHash &target, const char* tag);
880 /** Returns the number of occurences of tag in the config file
882 int ConfValueEnum(ConfigDataHash &target, const std::string &tag);
884 /** Returns the numbers of vars inside the index'th 'tag in the config file
886 int ConfVarEnum(ConfigDataHash &target, const char* tag, int index);
887 /** Returns the numbers of vars inside the index'th 'tag in the config file
889 int ConfVarEnum(ConfigDataHash &target, const std::string &tag, int index);
891 /** Validates a hostname value, throwing ConfigException if it is not valid
893 void ValidateHostname(const char* p, const std::string &tag, const std::string &val);
895 /** Validates an IP address value, throwing ConfigException if it is not valid
897 void ValidateIP(const char* p, const std::string &tag, const std::string &val, bool wild);
899 /** Validates a value that should not contain spaces, throwing ConfigException of it is not valid
901 void ValidateNoSpaces(const char* p, const std::string &tag, const std::string &val);
903 /** Returns the fully qualified path to the inspircd directory
904 * @return The full program directory
906 std::string GetFullProgDir();
908 /** Returns true if a directory is valid (within the modules directory).
909 * @param dirandfile The directory and filename to check
910 * @return True if the directory is valid
912 static bool DirValid(const char* dirandfile);
914 /** Clean a filename, stripping the directories (and drives) from string.
915 * @param name Directory to tidy
916 * @return The cleaned filename
918 static char* CleanFilename(char* name);
920 /** Check if a file exists.
921 * @param file The full path to a file
922 * @return True if the file exists and is readable.
924 static bool FileExists(const char* file);
926 /** If this value is true, invites will bypass more than just +i
932 /** Initialize the disabled commands list
934 CoreExport bool InitializeDisabledCommands(const char* data, InspIRCd* ServerInstance);
936 /** Initialize the oper types
938 bool InitTypes(ServerConfig* conf, const char* tag);
940 /** Initialize the oper classes
942 bool InitClasses(ServerConfig* conf, const char* tag);
944 /** Initialize an oper type
946 bool DoType(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
948 /** Initialize an oper class
950 bool DoClass(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
952 /** Finish initializing the oper types and classes
954 bool DoneClassesAndTypes(ServerConfig* conf, const char* tag);
958 /** Initialize x line
960 bool InitXLine(ServerConfig* conf, const char* tag);
962 /** Add a config-defined zline
964 bool DoZLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
966 /** Add a config-defined qline
968 bool DoQLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
970 /** Add a config-defined kline
972 bool DoKLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
974 /** Add a config-defined eline
976 bool DoELine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);