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 */
53 DT_BOOTONLY = 256 /* Can only be set on startup, not on rehash */
56 /** Holds a config value, either string, integer or boolean.
57 * Callback functions receive one or more of these, either on
58 * their own as a reference, or in a reference to a deque of them.
59 * The callback function can then alter the values of the ValueItem
60 * classes to validate the settings.
67 /** Initialize with an int */
69 /** Initialize with a bool */
70 ValueItem(bool value);
71 /** Initialize with a char pointer */
72 ValueItem(const char* value);
73 /** Change value to a char pointer */
74 void Set(const char* val);
75 /** Change value to an int */
77 /** Get value as an int */
79 /** Get value as a string */
81 /** Get value as a bool */
85 /** The base class of the container 'ValueContainer'
86 * used internally by the core to hold core values.
88 class ValueContainerBase
92 ValueContainerBase() { }
94 virtual ~ValueContainerBase() { }
97 /** ValueContainer is used to contain pointers to different
98 * core values such as the server name, maximum number of
100 * It is specialized to hold a data type, then pointed at
101 * a value in the ServerConfig class. When the value has been
102 * read and validated, the Set method is called to write the
103 * value safely in a type-safe manner.
105 template<typename T> class ValueContainer : public ValueContainerBase
107 /** Contained item */
111 /** Initialize with nothing */
117 /** Initialize with a value of type T */
118 ValueContainer(T Val)
123 /** Change value to type T of size s */
124 void Set(T newval, size_t s)
126 memcpy(val, newval, s);
130 /** A specialization of ValueContainer to hold a pointer to a bool
132 typedef ValueContainer<bool*> ValueContainerBool;
134 /** A specialization of ValueContainer to hold a pointer to
137 typedef ValueContainer<unsigned int*> ValueContainerUInt;
139 /** A specialization of ValueContainer to hold a pointer to
142 typedef ValueContainer<char*> ValueContainerChar;
144 /** A specialization of ValueContainer to hold a pointer to
147 typedef ValueContainer<int*> ValueContainerInt;
149 /** A set of ValueItems used by multi-value validator functions
151 typedef std::deque<ValueItem> ValueList;
153 /** A callback for validating a single value
155 typedef bool (*Validator)(ServerConfig* conf, const char*, const char*, ValueItem&);
156 /** A callback for validating multiple value entries
158 typedef bool (*MultiValidator)(ServerConfig* conf, const char*, char**, ValueList&, int*);
159 /** A callback indicating the end of a group of entries
161 typedef bool (*MultiNotify)(ServerConfig* conf, const char*);
163 /** Holds a core configuration item and its callbacks
171 /** Default, if not defined */
172 const char* default_value;
173 /** Value containers */
174 ValueContainerBase* val;
177 /** Validation function */
178 Validator validation_function;
181 /** Holds a core configuration item and its callbacks
182 * where there may be more than one item
188 /** One or more items within tag */
189 const char* items[18];
190 /** One or more defaults for items within tags */
191 const char* items_default[18];
192 /** One or more data types */
194 /** Initialization function */
195 MultiNotify init_function;
196 /** Validation function */
197 MultiValidator validation_function;
198 /** Completion function */
199 MultiNotify finish_function;
202 /** A set of oper types
204 typedef std::map<irc::string,char*> opertype_t;
206 /** A Set of oper classes
208 typedef std::map<irc::string,char*> operclass_t;
211 /** This class holds the bulk of the runtime configuration for the ircd.
212 * It allows for reading new config values, accessing configuration files,
213 * and storage of the configuration data needed to run the ircd, such as
214 * the servername, connect classes, /ADMIN data, MOTDs and filenames etc.
216 class CoreExport ServerConfig : public Extensible
219 /** Creator/owner pointer
221 InspIRCd* ServerInstance;
223 /** This variable holds the names of all
224 * files included from the main one. This
225 * is used to make sure that no files are
226 * recursively included.
228 std::vector<std::string> include_stack;
230 /** This private method processes one line of
231 * configutation, appending errors to errorstream
232 * and setting error if an error has occured.
234 bool ParseLine(ConfigDataHash &target, std::string &line, long &linenumber, std::ostringstream &errorstream);
236 /** Check that there is only one of each configuration item
238 bool CheckOnce(const char* tag);
242 /** Process an include executable directive
244 bool DoPipe(ConfigDataHash &target, const std::string &file, std::ostringstream &errorstream);
246 /** Process an include file directive
248 bool DoInclude(ConfigDataHash &target, const std::string &file, std::ostringstream &errorstream);
252 std::string RehashParameter;
254 std::ostringstream errstr;
256 ConfigDataHash newconfig;
258 std::map<std::string, std::istream*> IncludedFiles;
260 std::map<std::string, bool> CompletedFiles;
262 size_t TotalDownloaded;
265 /** Used to indicate who we announce invites to on a channel */
266 enum InviteAnnounceState { INVITE_ANNOUNCE_NONE, INVITE_ANNOUNCE_ALL, INVITE_ANNOUNCE_OPS, INVITE_ANNOUNCE_DYNAMIC };
268 /** Pointer to function that validates dns server addresses (can be changed depending on platform) */
269 Validator DNSServerValidator;
271 InspIRCd* GetInstance();
275 /** This holds all the information in the config file,
276 * it's indexed by tag name to a vector of key/values.
278 ConfigDataHash config_data;
280 /** Max number of WhoWas entries per user.
284 /** Max number of cumulative user-entries in WhoWas.
285 * When max reached and added to, push out oldest entry FIFO style.
289 /** Max seconds a user is kept in WhoWas before being pruned.
293 /** Holds the server name of the local server
294 * as defined by the administrator.
296 char ServerName[MAXBUF];
298 /** Notice to give to users when they are Xlined
300 char MoronBanner[MAXBUF];
302 /* Holds the network name the local server
303 * belongs to. This is an arbitary field defined
304 * by the administrator.
306 char Network[MAXBUF];
308 /** Holds the description of the local server
309 * as defined by the administrator.
311 char ServerDesc[MAXBUF];
313 /** Holds the admin's name, for output in
314 * the /ADMIN command.
316 char AdminName[MAXBUF];
318 /** Holds the email address of the admin,
319 * for output in the /ADMIN command.
321 char AdminEmail[MAXBUF];
323 /** Holds the admin's nickname, for output
324 * in the /ADMIN command
326 char AdminNick[MAXBUF];
328 /** The admin-configured /DIE password
330 char diepass[MAXBUF];
332 /** The admin-configured /RESTART password
334 char restartpass[MAXBUF];
336 /** The hash method for *BOTH* the die and restart passwords.
338 char powerhash[MAXBUF];
340 /** The pathname and filename of the message of the
341 * day file, as defined by the administrator.
345 /** The pathname and filename of the rules file,
346 * as defined by the administrator.
350 /** The quit prefix in use, or an empty string
352 char PrefixQuit[MAXBUF];
354 /** The quit suffix in use, or an empty string
356 char SuffixQuit[MAXBUF];
358 /** The fixed quit message in use, or an empty string
360 char FixedQuit[MAXBUF];
362 /** The part prefix in use, or an empty string
364 char PrefixPart[MAXBUF];
366 /** The part suffix in use, or an empty string
368 char SuffixPart[MAXBUF];
370 /** The fixed part message in use, or an empty string
372 char FixedPart[MAXBUF];
374 /** The last string found within a <die> tag, or
377 char DieValue[MAXBUF];
379 /** The DNS server to use for DNS queries
381 char DNSServer[MAXBUF];
383 /** This variable contains a space-seperated list
384 * of commands which are disabled by the
385 * administrator of the server for non-opers.
387 char DisabledCommands[MAXBUF];
389 /** The full path to the modules directory.
390 * This is either set at compile time, or
391 * overridden in the configuration file via
396 /** The full pathname to the executable, as
397 * given in argv[0] when the program starts.
399 char MyExecutable[1024];
401 /** The file handle of the logfile. If this
402 * value is NULL, the log file is not open,
403 * probably due to a permissions error on
404 * startup (this should not happen in normal
409 /** If this value is true, the owner of the
410 * server specified -nofork on the command
411 * line, causing the daemon to stay in the
416 /** If this value if true then all log
417 * messages will be output, regardless of
418 * the level given in the config file.
419 * This is set with the -debug commandline
424 /** If this is true then log output will be
425 * written to the logfile. This is the default.
426 * If you put -nolog on the commandline then
427 * the logfile will not be written.
428 * This is meant to be used in conjunction with
429 * -debug for debugging without filling up the
434 /** If this value is true, halfops have been
435 * enabled in the configuration file.
439 /** If this is set to true, then mode lists (e.g
440 * MODE #chan b) are hidden from unprivileged
443 bool HideModeLists[256];
445 /** If this is set to true, then channel operators
446 * are exempt from this channel mode. Used for +Sc etc.
448 bool ExemptChanOps[256];
450 /** The number of seconds the DNS subsystem
451 * will wait before timing out any request.
455 /** The size of the read() buffer in the user
456 * handling code, used to read data into a user's
461 /** The value to be used for listen() backlogs
466 /** The soft limit value assigned to the irc server.
467 * The IRC server will not allow more than this
468 * number of local users.
470 unsigned int SoftLimit;
472 /** Maximum number of targets for a multi target command
473 * such as PRIVMSG or KICK
475 unsigned int MaxTargets;
477 /** The maximum number of /WHO results allowed
478 * in any single /WHO command.
482 /** True if the DEBUG loglevel is selected.
486 /** The loglevel in use by the IRC server
490 /** How many seconds to wait before exiting
491 * the program when /DIE is correctly issued.
495 /** True if we're going to hide netsplits as *.net *.split for non-opers
499 /** True if we're going to hide ban reasons for non-opers (e.g. G-Lines,
504 /** Announce invites to the channel with a server notice
506 InviteAnnounceState AnnounceInvites;
508 /** If this is enabled then operators will
509 * see invisible (+i) channels in /whois.
513 /** Set to a non-empty string to obfuscate the server name of users in WHOIS
515 char HideWhoisServer[MAXBUF];
517 /** Set to a non empty string to obfuscate nicknames prepended to a KILL.
519 char HideKillsServer[MAXBUF];
521 /** The MOTD file, cached in a file_cache type.
525 /** The RULES file, cached in a file_cache type.
529 /** The full pathname and filename of the PID
530 * file as defined in the configuration.
534 /** The connect classes in use by the IRC server.
538 /** A list of the classes for listening client ports
540 std::vector<ListenSocket*> ports;
542 /** A list of ports claimed by IO Modules
544 std::map<int,Module*> IOHookModule;
546 std::map<BufferedSocket*, Module*> SocketIOHookModule;
548 /** The 005 tokens of this server (ISUPPORT)
549 * populated/repopulated upon loading or unloading
556 std::vector<std::string> isupport;
558 /** STATS characters in this list are available
561 char UserStats[MAXBUF];
563 /** The path and filename of the ircd.log file
567 /** Default channel modes
569 char DefaultModes[MAXBUF];
571 /** Custom version string, which if defined can replace the system info in VERSION.
573 char CustomVersion[MAXBUF];
575 /** List of u-lined servers
577 std::map<irc::string, bool> ulines;
579 /** Max banlist sizes for channels (the std::string is a glob)
581 std::map<std::string, int> maxbans;
583 /** Directory where the inspircd binary resides
587 /** If set to true, no user DNS lookups are to be performed
591 /** If set to true, provide syntax hints for unknown commands
595 /** If set to true, users appear to quit then rejoin when their hosts change.
596 * This keeps clients synchronized properly.
600 /** If set to true, prefixed channel NOTICEs and PRIVMSGs will have the prefix
601 * added to the outgoing text for undernet style msg prefixing.
603 bool UndernetMsgPrefix;
605 /** If set to true, the full nick!user@host will be shown in the TOPIC command
606 * for who set the topic last. If false, only the nick is shown.
608 bool FullHostInTopic;
610 /** All oper type definitions from the config file
612 opertype_t opertypes;
614 /** All oper class definitions from the config file
616 operclass_t operclass;
618 /** Saved argv from startup
622 /** Saved argc from startup
626 /** Max channels per user
628 unsigned int MaxChans;
630 /** Oper max channels per user
632 unsigned int OperMaxChans;
634 /** TS6-like server ID.
635 * NOTE: 000...999 are usable for InspIRCd servers. This
636 * makes code simpler. 0AA, 1BB etc with letters are reserved
641 /** True if we have been told to run the testsuite from the commandline,
642 * rather than entering the mainloop.
646 /** Construct a new ServerConfig
648 ServerConfig(InspIRCd* Instance);
650 /** Clears the include stack in preperation for a Read() call.
654 /** Get server ID as string with required leading zeroes
656 std::string GetSID();
658 /** Update the 005 vector
662 /** Send the 005 numerics (ISUPPORT) to a user
664 void Send005(User* user);
666 /** Read the entire configuration into memory
667 * and initialize this class. All other methods
668 * should be used only by the core.
670 void Read(bool bail, User* user);
672 /** Read a file into a file_cache object
674 bool ReadFile(file_cache &F, const char* fname);
676 /** Report a configuration error given in errormessage.
677 * @param bail If this is set to true, the error is sent to the console, and the program exits
678 * @param user If this is set to a non-null value, and bail is false, the errors are spooled to
679 * this user as SNOTICEs.
680 * If the parameter is NULL, the messages are spooled to all opers.
682 void ReportConfigError(const std::string &errormessage, bool bail, User* user);
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, FILE* &conf, const char* filename, std::ostringstream &errorstream);
689 /** Load 'filename' into 'target', with the new config parser everything is parsed into
690 * tag/key/value at load-time rather than at read-value time.
692 bool LoadConf(ConfigDataHash &target, FILE* &conf, const std::string &filename, std::ostringstream &errorstream);
694 /* Both these return true if the value existed or false otherwise */
696 /** Writes 'length' chars into 'result' as a string
698 bool ConfValue(ConfigDataHash &target, const char* tag, const char* var, int index, char* result, int length, bool allow_linefeeds = false);
699 /** Writes 'length' chars into 'result' as a string
701 bool ConfValue(ConfigDataHash &target, const char* tag, const char* var, const char* default_value, int index, char* result, int length, bool allow_linefeeds = false);
703 /** Writes 'length' chars into 'result' as a string
705 bool ConfValue(ConfigDataHash &target, const std::string &tag, const std::string &var, int index, std::string &result, bool allow_linefeeds = false);
706 /** Writes 'length' chars into 'result' as a string
708 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);
710 /** Tries to convert the value to an integer and write it to 'result'
712 bool ConfValueInteger(ConfigDataHash &target, const char* tag, const char* var, int index, int &result);
713 /** Tries to convert the value to an integer and write it to 'result'
715 bool ConfValueInteger(ConfigDataHash &target, const char* tag, const char* var, const char* default_value, int index, int &result);
716 /** Tries to convert the value to an integer and write it to 'result'
718 bool ConfValueInteger(ConfigDataHash &target, const std::string &tag, const std::string &var, int index, int &result);
719 /** Tries to convert the value to an integer and write it to 'result'
721 bool ConfValueInteger(ConfigDataHash &target, const std::string &tag, const std::string &var, const std::string &default_value, int index, int &result);
723 /** Returns true if the value exists and has a true value, false otherwise
725 bool ConfValueBool(ConfigDataHash &target, const char* tag, const char* var, int index);
726 /** Returns true if the value exists and has a true value, false otherwise
728 bool ConfValueBool(ConfigDataHash &target, const char* tag, const char* var, const char* default_value, int index);
729 /** Returns true if the value exists and has a true value, false otherwise
731 bool ConfValueBool(ConfigDataHash &target, const std::string &tag, const std::string &var, int index);
732 /** Returns true if the value exists and has a true value, false otherwise
734 bool ConfValueBool(ConfigDataHash &target, const std::string &tag, const std::string &var, const std::string &default_value, int index);
736 /** Returns the number of occurences of tag in the config file
738 int ConfValueEnum(ConfigDataHash &target, const char* tag);
739 /** Returns the number of occurences of tag in the config file
741 int ConfValueEnum(ConfigDataHash &target, const std::string &tag);
743 /** Returns the numbers of vars inside the index'th 'tag in the config file
745 int ConfVarEnum(ConfigDataHash &target, const char* tag, int index);
746 /** Returns the numbers of vars inside the index'th 'tag in the config file
748 int ConfVarEnum(ConfigDataHash &target, const std::string &tag, int index);
750 void ValidateHostname(const char* p, const std::string &tag, const std::string &val);
752 void ValidateIP(const char* p, const std::string &tag, const std::string &val, bool wild);
754 void ValidateNoSpaces(const char* p, const std::string &tag, const std::string &val);
756 /** Get a pointer to the module which has hooked the given port.
757 * @parameter port Port number
758 * @return Returns a pointer to the hooking module, or NULL
760 Module* GetIOHook(int port);
762 /** Hook a module to a client port, so that it can receive notifications
763 * of low-level port activity.
764 * @param port The port number
765 * @param Module the module to hook to the port
766 * @return True if the hook was successful.
768 bool AddIOHook(int port, Module* iomod);
770 /** Delete a module hook from a client port.
771 * @param port The port to detatch from
772 * @return True if successful
774 bool DelIOHook(int port);
776 /** Get a pointer to the module which has hooked the given BufferedSocket class.
777 * @parameter port Port number
778 * @return Returns a pointer to the hooking module, or NULL
780 Module* GetIOHook(BufferedSocket* is);
782 /** Hook a module to an BufferedSocket class, so that it can receive notifications
783 * of low-level socket activity.
784 * @param iomod The module to hook to the socket
785 * @param is The BufferedSocket to attach to
786 * @return True if the hook was successful.
788 bool AddIOHook(Module* iomod, BufferedSocket* is);
790 /** Delete a module hook from an BufferedSocket.
791 * @param is The BufferedSocket to detatch from.
792 * @return True if the unhook was successful
794 bool DelIOHook(BufferedSocket* is);
796 /** Returns the fully qualified path to the inspircd directory
797 * @return The full program directory
799 std::string GetFullProgDir();
801 /** Returns true if a directory is valid (within the modules directory).
802 * @param dirandfile The directory and filename to check
803 * @return True if the directory is valid
805 static bool DirValid(const char* dirandfile);
807 /** Clean a filename, stripping the directories (and drives) from string.
808 * @param name Directory to tidy
809 * @return The cleaned filename
811 static char* CleanFilename(char* name);
813 /** Check if a file exists.
814 * @param file The full path to a file
815 * @return True if the file exists and is readable.
817 static bool FileExists(const char* file);
821 /** Initialize the disabled commands list
823 CoreExport bool InitializeDisabledCommands(const char* data, InspIRCd* ServerInstance);
825 /** Initialize the oper types
827 bool InitTypes(ServerConfig* conf, const char* tag);
829 /** Initialize the oper classes
831 bool InitClasses(ServerConfig* conf, const char* tag);
833 /** Initialize an oper type
835 bool DoType(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
837 /** Initialize an oper class
839 bool DoClass(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
841 /** Finish initializing the oper types and classes
843 bool DoneClassesAndTypes(ServerConfig* conf, const char* tag);
847 /** Initialize x line
849 bool InitXLine(ServerConfig* conf, const char* tag);
851 /** Add a config-defined zline
853 bool DoZLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
855 /** Add a config-defined qline
857 bool DoQLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
859 /** Add a config-defined kline
861 bool DoKLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
863 /** Add a config-defined eline
865 bool DoELine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);