1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/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])
31 #include "socketengine.h"
34 /* Required forward definitions */
39 /** Types of data in the core config
43 DT_NOTHING = 0, /* No data */
44 DT_INTEGER = 1, /* Integer */
45 DT_CHARPTR = 2, /* Char pointer */
46 DT_BOOLEAN = 3, /* Boolean */
47 DT_HOSTNAME = 4, /* Hostname syntax */
48 DT_NOSPACES = 5, /* No spaces */
49 DT_IPADDRESS = 6, /* IP address (v4, v6) */
50 DT_CHANNEL = 7, /* Channel name */
51 DT_ALLOW_WILD = 64, /* Allow wildcards/CIDR in DT_IPADDRESS */
52 DT_ALLOW_NEWLINE = 128 /* New line characters allowed in DT_CHARPTR */
55 /** Holds a config value, either string, integer or boolean.
56 * Callback functions receive one or more of these, either on
57 * their own as a reference, or in a reference to a deque of them.
58 * The callback function can then alter the values of the ValueItem
59 * classes to validate the settings.
66 /** Initialize with an int */
68 /** Initialize with a bool */
69 ValueItem(bool value);
70 /** Initialize with a char pointer */
71 ValueItem(const char* value);
72 /** Change value to a char pointer */
73 void Set(const char* val);
74 /** Change value to an int */
76 /** Get value as an int */
78 /** Get value as a string */
80 /** Get value as a bool */
84 /** The base class of the container 'ValueContainer'
85 * used internally by the core to hold core values.
87 class ValueContainerBase
91 ValueContainerBase() { }
93 virtual ~ValueContainerBase() { }
96 /** ValueContainer is used to contain pointers to different
97 * core values such as the server name, maximum number of
99 * It is specialized to hold a data type, then pointed at
100 * a value in the ServerConfig class. When the value has been
101 * read and validated, the Set method is called to write the
102 * value safely in a type-safe manner.
104 template<typename T> class ValueContainer : public ValueContainerBase
106 /** Contained item */
110 /** Initialize with nothing */
116 /** Initialize with a value of type T */
117 ValueContainer(T Val)
122 /** Change value to type T of size s */
123 void Set(T newval, size_t s)
125 memcpy(val, newval, s);
129 /** A specialization of ValueContainer to hold a pointer to a bool
131 typedef ValueContainer<bool*> ValueContainerBool;
133 /** A specialization of ValueContainer to hold a pointer to
136 typedef ValueContainer<unsigned int*> ValueContainerUInt;
138 /** A specialization of ValueContainer to hold a pointer to
141 typedef ValueContainer<char*> ValueContainerChar;
143 /** A specialization of ValueContainer to hold a pointer to
146 typedef ValueContainer<int*> ValueContainerInt;
148 /** A set of ValueItems used by multi-value validator functions
150 typedef std::deque<ValueItem> ValueList;
152 /** A callback for validating a single value
154 typedef bool (*Validator)(ServerConfig* conf, const char*, const char*, ValueItem&);
155 /** A callback for validating multiple value entries
157 typedef bool (*MultiValidator)(ServerConfig* conf, const char*, char**, ValueList&, int*);
158 /** A callback indicating the end of a group of entries
160 typedef bool (*MultiNotify)(ServerConfig* conf, const char*);
162 /** Holds a core configuration item and its callbacks
170 /** Default, if not defined */
171 const char* default_value;
172 /** Value containers */
173 ValueContainerBase* val;
175 ConfigDataType datatype;
176 /** Validation function */
177 Validator validation_function;
180 /** Holds a core configuration item and its callbacks
181 * where there may be more than one item
187 /** One or more items within tag */
188 const char* items[18];
189 /** One or more defaults for items within tags */
190 const char* items_default[18];
191 /** One or more data types */
193 /** Initialization function */
194 MultiNotify init_function;
195 /** Validation function */
196 MultiValidator validation_function;
197 /** Completion function */
198 MultiNotify finish_function;
201 /** A set of oper types
203 typedef std::map<irc::string,char*> opertype_t;
205 /** A Set of oper classes
207 typedef std::map<irc::string,char*> operclass_t;
210 /** This class holds the bulk of the runtime configuration for the ircd.
211 * It allows for reading new config values, accessing configuration files,
212 * and storage of the configuration data needed to run the ircd, such as
213 * the servername, connect classes, /ADMIN data, MOTDs and filenames etc.
215 class CoreExport ServerConfig : public Extensible
218 /** Creator/owner pointer
220 InspIRCd* ServerInstance;
222 /** This variable holds the names of all
223 * files included from the main one. This
224 * is used to make sure that no files are
225 * recursively included.
227 std::vector<std::string> include_stack;
229 /** This private method processes one line of
230 * configutation, appending errors to errorstream
231 * and setting error if an error has occured.
233 bool ParseLine(ConfigDataHash &target, std::string &line, long &linenumber, std::ostringstream &errorstream);
235 /** Process an include directive
237 bool DoInclude(ConfigDataHash &target, const std::string &file, std::ostringstream &errorstream);
239 /** Check that there is only one of each configuration item
241 bool CheckOnce(const char* tag);
247 std::string RehashParameter;
249 std::ostringstream errstr;
251 ConfigDataHash newconfig;
253 std::map<std::string, std::istream*> IncludedFiles;
255 std::map<std::string, bool> CompletedFiles;
257 size_t TotalDownloaded;
260 /** Used to indicate who we announce invites to on a channel */
261 enum InviteAnnounceState { INVITE_ANNOUNCE_NONE, INVITE_ANNOUNCE_ALL, INVITE_ANNOUNCE_OPS, INVITE_ANNOUNCE_DYNAMIC };
263 /** Pointer to function that validates dns server addresses (can be changed depending on platform) */
264 Validator DNSServerValidator;
266 InspIRCd* GetInstance();
270 /** This holds all the information in the config file,
271 * it's indexed by tag name to a vector of key/values.
273 ConfigDataHash config_data;
275 /** Max number of WhoWas entries per user.
279 /** Max number of cumulative user-entries in WhoWas.
280 * When max reached and added to, push out oldest entry FIFO style.
284 /** Max seconds a user is kept in WhoWas before being pruned.
288 /** Holds the server name of the local server
289 * as defined by the administrator.
291 char ServerName[MAXBUF];
293 /** Notice to give to users when they are Xlined
295 char MoronBanner[MAXBUF];
297 /* Holds the network name the local server
298 * belongs to. This is an arbitary field defined
299 * by the administrator.
301 char Network[MAXBUF];
303 /** Holds the description of the local server
304 * as defined by the administrator.
306 char ServerDesc[MAXBUF];
308 /** Holds the admin's name, for output in
309 * the /ADMIN command.
311 char AdminName[MAXBUF];
313 /** Holds the email address of the admin,
314 * for output in the /ADMIN command.
316 char AdminEmail[MAXBUF];
318 /** Holds the admin's nickname, for output
319 * in the /ADMIN command
321 char AdminNick[MAXBUF];
323 /** The admin-configured /DIE password
325 char diepass[MAXBUF];
327 /** The admin-configured /RESTART password
329 char restartpass[MAXBUF];
331 /** The hash method for *BOTH* the die and restart passwords.
333 char powerhash[MAXBUF];
335 /** The pathname and filename of the message of the
336 * day file, as defined by the administrator.
340 /** The pathname and filename of the rules file,
341 * as defined by the administrator.
345 /** The quit prefix in use, or an empty string
347 char PrefixQuit[MAXBUF];
349 /** The quit suffix in use, or an empty string
351 char SuffixQuit[MAXBUF];
353 /** The fixed quit message in use, or an empty string
355 char FixedQuit[MAXBUF];
357 /** The part prefix in use, or an empty string
359 char PrefixPart[MAXBUF];
361 /** The part suffix in use, or an empty string
363 char SuffixPart[MAXBUF];
365 /** The fixed part message in use, or an empty string
367 char FixedPart[MAXBUF];
369 /** The last string found within a <die> tag, or
372 char DieValue[MAXBUF];
374 /** The DNS server to use for DNS queries
376 char DNSServer[MAXBUF];
378 /** This variable contains a space-seperated list
379 * of commands which are disabled by the
380 * administrator of the server for non-opers.
382 char DisabledCommands[MAXBUF];
384 /** The full path to the modules directory.
385 * This is either set at compile time, or
386 * overridden in the configuration file via
391 /** The full pathname to the executable, as
392 * given in argv[0] when the program starts.
394 char MyExecutable[1024];
396 /** The file handle of the logfile. If this
397 * value is NULL, the log file is not open,
398 * probably due to a permissions error on
399 * startup (this should not happen in normal
404 /** If this value is true, the owner of the
405 * server specified -nofork on the command
406 * line, causing the daemon to stay in the
411 /** If this value if true then all log
412 * messages will be output, regardless of
413 * the level given in the config file.
414 * This is set with the -debug commandline
419 /** If this is true then log output will be
420 * written to the logfile. This is the default.
421 * If you put -nolog on the commandline then
422 * the logfile will not be written.
423 * This is meant to be used in conjunction with
424 * -debug for debugging without filling up the
429 /** If this value is true, halfops have been
430 * enabled in the configuration file.
434 /** If this is set to true, then mode lists (e.g
435 * MODE #chan b) are hidden from unprivileged
438 bool HideModeLists[256];
440 /** If this is set to true, then channel operators
441 * are exempt from this channel mode. Used for +Sc etc.
443 bool ExemptChanOps[256];
445 /** The number of seconds the DNS subsystem
446 * will wait before timing out any request.
450 /** The size of the read() buffer in the user
451 * handling code, used to read data into a user's
456 /** The value to be used for listen() backlogs
461 /** The soft limit value assigned to the irc server.
462 * The IRC server will not allow more than this
463 * number of local users.
465 unsigned int SoftLimit;
467 /** Maximum number of targets for a multi target command
468 * such as PRIVMSG or KICK
470 unsigned int MaxTargets;
472 /** The maximum number of /WHO results allowed
473 * in any single /WHO command.
477 /** True if the DEBUG loglevel is selected.
481 /** The loglevel in use by the IRC server
485 /** How many seconds to wait before exiting
486 * the program when /DIE is correctly issued.
490 /** True if we're going to hide netsplits as *.net *.split for non-opers
494 /** True if we're going to hide ban reasons for non-opers (e.g. G-Lines,
499 /** Announce invites to the channel with a server notice
501 InviteAnnounceState AnnounceInvites;
503 /** If this is enabled then operators will
504 * see invisible (+i) channels in /whois.
508 /** Set to a non-empty string to obfuscate the server name of users in WHOIS
510 char HideWhoisServer[MAXBUF];
512 /** Set to a non empty string to obfuscate nicknames prepended to a KILL.
514 char HideKillsServer[MAXBUF];
516 /** The MOTD file, cached in a file_cache type.
520 /** The RULES file, cached in a file_cache type.
524 /** The full pathname and filename of the PID
525 * file as defined in the configuration.
529 /** The connect classes in use by the IRC server.
533 /** A list of the classes for listening client ports
535 std::vector<ListenSocket*> ports;
537 /** A list of ports claimed by IO Modules
539 std::map<int,Module*> IOHookModule;
541 std::map<BufferedSocket*, Module*> SocketIOHookModule;
543 /** The 005 tokens of this server (ISUPPORT)
544 * populated/repopulated upon loading or unloading
551 std::vector<std::string> isupport;
553 /** STATS characters in this list are available
556 char UserStats[MAXBUF];
558 /** The path and filename of the ircd.log file
562 /** Default channel modes
564 char DefaultModes[MAXBUF];
566 /** Custom version string, which if defined can replace the system info in VERSION.
568 char CustomVersion[MAXBUF];
570 /** List of u-lined servers
572 std::map<irc::string, bool> ulines;
574 /** Max banlist sizes for channels (the std::string is a glob)
576 std::map<std::string, int> maxbans;
578 /** Directory where the inspircd binary resides
582 /** If set to true, no user DNS lookups are to be performed
586 /** If set to true, provide syntax hints for unknown commands
590 /** If set to true, users appear to quit then rejoin when their hosts change.
591 * This keeps clients synchronized properly.
595 /** If set to true, prefixed channel NOTICEs and PRIVMSGs will have the prefix
596 * added to the outgoing text for undernet style msg prefixing.
598 bool UndernetMsgPrefix;
600 /** If set to true, the full nick!user@host will be shown in the TOPIC command
601 * for who set the topic last. If false, only the nick is shown.
603 bool FullHostInTopic;
605 /** All oper type definitions from the config file
607 opertype_t opertypes;
609 /** All oper class definitions from the config file
611 operclass_t operclass;
613 /** Saved argv from startup
617 /** Saved argc from startup
621 /** Max channels per user
623 unsigned int MaxChans;
625 /** Oper max channels per user
627 unsigned int OperMaxChans;
629 /** TS6-like server ID.
630 * NOTE: 000...999 are usable for InspIRCd servers. This
631 * makes code simpler. 0AA, 1BB etc with letters are reserved
636 /** True if we have been told to run the testsuite from the commandline,
637 * rather than entering the mainloop.
641 /** Construct a new ServerConfig
643 ServerConfig(InspIRCd* Instance);
645 /** Clears the include stack in preperation for a Read() call.
649 /** Get server ID as string with required leading zeroes
651 std::string GetSID();
653 /** Update the 005 vector
657 /** Send the 005 numerics (ISUPPORT) to a user
659 void Send005(User* user);
661 /** Read the entire configuration into memory
662 * and initialize this class. All other methods
663 * should be used only by the core.
665 void Read(bool bail, User* user);
667 /** Read a file into a file_cache object
669 bool ReadFile(file_cache &F, const char* fname);
671 /** Report a configuration error given in errormessage.
672 * @param bail If this is set to true, the error is sent to the console, and the program exits
673 * @param user If this is set to a non-null value, and bail is false, the errors are spooled to
674 * this user as SNOTICEs.
675 * If the parameter is NULL, the messages are spooled to all opers.
677 void ReportConfigError(const std::string &errormessage, bool bail, User* user);
679 /** Load 'filename' into 'target', with the new config parser everything is parsed into
680 * tag/key/value at load-time rather than at read-value time.
682 bool LoadConf(ConfigDataHash &target, const char* filename, std::ostringstream &errorstream);
684 /** Load 'filename' into 'target', with the new config parser everything is parsed into
685 * tag/key/value at load-time rather than at read-value time.
687 bool LoadConf(ConfigDataHash &target, const std::string &filename, std::ostringstream &errorstream);
689 /* Both these return true if the value existed or false otherwise */
691 /** Writes 'length' chars into 'result' as a string
693 bool ConfValue(ConfigDataHash &target, const char* tag, const char* var, int index, char* result, int length, bool allow_linefeeds = false);
694 /** Writes 'length' chars into 'result' as a string
696 bool ConfValue(ConfigDataHash &target, const char* tag, const char* var, const char* default_value, int index, char* result, int length, bool allow_linefeeds = false);
698 /** Writes 'length' chars into 'result' as a string
700 bool ConfValue(ConfigDataHash &target, const std::string &tag, const std::string &var, int index, std::string &result, bool allow_linefeeds = false);
701 /** Writes 'length' chars into 'result' as a string
703 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);
705 /** Tries to convert the value to an integer and write it to 'result'
707 bool ConfValueInteger(ConfigDataHash &target, const char* tag, const char* var, int index, int &result);
708 /** Tries to convert the value to an integer and write it to 'result'
710 bool ConfValueInteger(ConfigDataHash &target, const char* tag, const char* var, const char* default_value, int index, int &result);
711 /** Tries to convert the value to an integer and write it to 'result'
713 bool ConfValueInteger(ConfigDataHash &target, const std::string &tag, const std::string &var, int index, int &result);
714 /** Tries to convert the value to an integer and write it to 'result'
716 bool ConfValueInteger(ConfigDataHash &target, const std::string &tag, const std::string &var, const std::string &default_value, int index, int &result);
718 /** Returns true if the value exists and has a true value, false otherwise
720 bool ConfValueBool(ConfigDataHash &target, const char* tag, const char* var, int index);
721 /** Returns true if the value exists and has a true value, false otherwise
723 bool ConfValueBool(ConfigDataHash &target, const char* tag, const char* var, const char* default_value, int index);
724 /** Returns true if the value exists and has a true value, false otherwise
726 bool ConfValueBool(ConfigDataHash &target, const std::string &tag, const std::string &var, int index);
727 /** Returns true if the value exists and has a true value, false otherwise
729 bool ConfValueBool(ConfigDataHash &target, const std::string &tag, const std::string &var, const std::string &default_value, int index);
731 /** Returns the number of occurences of tag in the config file
733 int ConfValueEnum(ConfigDataHash &target, const char* tag);
734 /** Returns the number of occurences of tag in the config file
736 int ConfValueEnum(ConfigDataHash &target, const std::string &tag);
738 /** Returns the numbers of vars inside the index'th 'tag in the config file
740 int ConfVarEnum(ConfigDataHash &target, const char* tag, int index);
741 /** Returns the numbers of vars inside the index'th 'tag in the config file
743 int ConfVarEnum(ConfigDataHash &target, const std::string &tag, int index);
745 void ValidateHostname(const char* p, const std::string &tag, const std::string &val);
747 void ValidateIP(const char* p, const std::string &tag, const std::string &val, bool wild);
749 void ValidateNoSpaces(const char* p, const std::string &tag, const std::string &val);
751 /** Get a pointer to the module which has hooked the given port.
752 * @parameter port Port number
753 * @return Returns a pointer to the hooking module, or NULL
755 Module* GetIOHook(int port);
757 /** Hook a module to a client port, so that it can receive notifications
758 * of low-level port activity.
759 * @param port The port number
760 * @param Module the module to hook to the port
761 * @return True if the hook was successful.
763 bool AddIOHook(int port, Module* iomod);
765 /** Delete a module hook from a client port.
766 * @param port The port to detatch from
767 * @return True if successful
769 bool DelIOHook(int port);
771 /** Get a pointer to the module which has hooked the given BufferedSocket class.
772 * @parameter port Port number
773 * @return Returns a pointer to the hooking module, or NULL
775 Module* GetIOHook(BufferedSocket* is);
777 /** Hook a module to an BufferedSocket class, so that it can receive notifications
778 * of low-level socket activity.
779 * @param iomod The module to hook to the socket
780 * @param is The BufferedSocket to attach to
781 * @return True if the hook was successful.
783 bool AddIOHook(Module* iomod, BufferedSocket* is);
785 /** Delete a module hook from an BufferedSocket.
786 * @param is The BufferedSocket to detatch from.
787 * @return True if the unhook was successful
789 bool DelIOHook(BufferedSocket* is);
791 /** Returns the fully qualified path to the inspircd directory
792 * @return The full program directory
794 std::string GetFullProgDir();
796 /** Returns true if a directory is valid (within the modules directory).
797 * @param dirandfile The directory and filename to check
798 * @return True if the directory is valid
800 static bool DirValid(const char* dirandfile);
802 /** Clean a filename, stripping the directories (and drives) from string.
803 * @param name Directory to tidy
804 * @return The cleaned filename
806 static char* CleanFilename(char* name);
808 /** Check if a file exists.
809 * @param file The full path to a file
810 * @return True if the file exists and is readable.
812 static bool FileExists(const char* file);
816 /** Initialize the disabled commands list
818 CoreExport bool InitializeDisabledCommands(const char* data, InspIRCd* ServerInstance);
820 /** Initialize the oper types
822 bool InitTypes(ServerConfig* conf, const char* tag);
824 /** Initialize the oper classes
826 bool InitClasses(ServerConfig* conf, const char* tag);
828 /** Initialize an oper type
830 bool DoType(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
832 /** Initialize an oper class
834 bool DoClass(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
836 /** Finish initializing the oper types and classes
838 bool DoneClassesAndTypes(ServerConfig* conf, const char* tag);
842 /** Initialize x line
844 bool InitXLine(ServerConfig* conf, const char* tag);
846 /** Add a config-defined zline
848 bool DoZLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
850 /** Add a config-defined qline
852 bool DoQLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
854 /** Add a config-defined kline
856 bool DoKLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
858 /** Add a config-defined eline
860 bool DoELine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);