1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 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(char* value);
72 /** Change value to a char pointer */
73 void Set(char* value);
74 /** Change value to a const char pointer */
75 void Set(const char* val);
76 /** Change value to an int */
78 /** Get value as an int */
80 /** Get value as a string */
82 /** Get value as a bool */
86 /** The base class of the container 'ValueContainer'
87 * used internally by the core to hold core values.
89 class ValueContainerBase
93 ValueContainerBase() { }
95 virtual ~ValueContainerBase() { }
98 /** ValueContainer is used to contain pointers to different
99 * core values such as the server name, maximum number of
101 * It is specialized to hold a data type, then pointed at
102 * a value in the ServerConfig class. When the value has been
103 * read and validated, the Set method is called to write the
104 * value safely in a type-safe manner.
106 template<typename T> class ValueContainer : public ValueContainerBase
108 /** Contained item */
112 /** Initialize with nothing */
118 /** Initialize with a value of type T */
119 ValueContainer(T Val)
124 /** Change value to type T of size s */
125 void Set(T newval, size_t s)
127 memcpy(val, newval, s);
131 /** A specialization of ValueContainer to hold a pointer to a bool
133 typedef ValueContainer<bool*> ValueContainerBool;
135 /** A specialization of ValueContainer to hold a pointer to
138 typedef ValueContainer<unsigned int*> ValueContainerUInt;
140 /** A specialization of ValueContainer to hold a pointer to
143 typedef ValueContainer<char*> ValueContainerChar;
145 /** A specialization of ValueContainer to hold a pointer to
148 typedef ValueContainer<int*> ValueContainerInt;
150 /** A set of ValueItems used by multi-value validator functions
152 typedef std::deque<ValueItem> ValueList;
154 /** A callback for validating a single value
156 typedef bool (*Validator)(ServerConfig* conf, const char*, const char*, ValueItem&);
157 /** A callback for validating multiple value entries
159 typedef bool (*MultiValidator)(ServerConfig* conf, const char*, char**, ValueList&, int*);
160 /** A callback indicating the end of a group of entries
162 typedef bool (*MultiNotify)(ServerConfig* conf, const char*);
164 /** Holds a core configuration item and its callbacks
172 /** Default, if not defined */
174 /** Value containers */
175 ValueContainerBase* val;
177 ConfigDataType datatype;
178 /** Validation function */
179 Validator validation_function;
182 /** Holds a core configuration item and its callbacks
183 * where there may be more than one item
189 /** One or more items within tag */
191 /** One or more defaults for items within tags */
192 char* items_default[17];
193 /** One or more data types */
195 /** Initialization function */
196 MultiNotify init_function;
197 /** Validation function */
198 MultiValidator validation_function;
199 /** Completion function */
200 MultiNotify finish_function;
203 /** A set of oper types
205 typedef std::map<irc::string,char*> opertype_t;
207 /** A Set of oper classes
209 typedef std::map<irc::string,char*> operclass_t;
212 /** This class holds the bulk of the runtime configuration for the ircd.
213 * It allows for reading new config values, accessing configuration files,
214 * and storage of the configuration data needed to run the ircd, such as
215 * the servername, connect classes, /ADMIN data, MOTDs and filenames etc.
217 class CoreExport ServerConfig : public Extensible
220 /** Creator/owner pointer
222 InspIRCd* ServerInstance;
224 /** This variable holds the names of all
225 * files included from the main one. This
226 * is used to make sure that no files are
227 * recursively included.
229 std::vector<std::string> include_stack;
231 /** This private method processes one line of
232 * configutation, appending errors to errorstream
233 * and setting error if an error has occured.
235 bool ParseLine(ConfigDataHash &target, std::string &line, long &linenumber, std::ostringstream &errorstream, int pass);
237 /** Process an include directive
239 bool DoInclude(ConfigDataHash &target, const std::string &file, std::ostringstream &errorstream, int pass);
241 /** Check that there is only one of each configuration item
243 bool CheckOnce(char* tag);
247 std::map<std::string, std::istream*> IncludedFiles;
249 size_t TotalDownloaded;
252 /** Used to indicate who we announce invites to on a channel */
253 enum InviteAnnounceState { INVITE_ANNOUNCE_NONE, INVITE_ANNOUNCE_ALL, INVITE_ANNOUNCE_OPS, INVITE_ANNOUNCE_DYNAMIC };
255 /** Pointer to function that validates dns server addresses (can be changed depending on platform) */
256 Validator DNSServerValidator;
258 InspIRCd* GetInstance();
262 void StartDownloads();
264 /** This holds all the information in the config file,
265 * it's indexed by tag name to a vector of key/values.
267 ConfigDataHash config_data;
269 /** Max number of WhoWas entries per user.
273 /** Max number of cumulative user-entries in WhoWas.
274 * When max reached and added to, push out oldest entry FIFO style.
278 /** Max seconds a user is kept in WhoWas before being pruned.
282 /** Holds the server name of the local server
283 * as defined by the administrator.
285 char ServerName[MAXBUF];
287 /** Notice to give to users when they are Xlined
289 char MoronBanner[MAXBUF];
291 /* Holds the network name the local server
292 * belongs to. This is an arbitary field defined
293 * by the administrator.
295 char Network[MAXBUF];
297 /** Holds the description of the local server
298 * as defined by the administrator.
300 char ServerDesc[MAXBUF];
302 /** Holds the admin's name, for output in
303 * the /ADMIN command.
305 char AdminName[MAXBUF];
307 /** Holds the email address of the admin,
308 * for output in the /ADMIN command.
310 char AdminEmail[MAXBUF];
312 /** Holds the admin's nickname, for output
313 * in the /ADMIN command
315 char AdminNick[MAXBUF];
317 /** The admin-configured /DIE password
319 char diepass[MAXBUF];
321 /** The admin-configured /RESTART password
323 char restartpass[MAXBUF];
325 /** The pathname and filename of the message of the
326 * day file, as defined by the administrator.
330 /** The pathname and filename of the rules file,
331 * as defined by the administrator.
335 /** The quit prefix in use, or an empty string
337 char PrefixQuit[MAXBUF];
339 /** The quit suffix in use, or an empty string
341 char SuffixQuit[MAXBUF];
343 /** The fixed quit message in use, or an empty string
345 char FixedQuit[MAXBUF];
347 /** The last string found within a <die> tag, or
350 char DieValue[MAXBUF];
352 /** The DNS server to use for DNS queries
354 char DNSServer[MAXBUF];
356 /** This variable contains a space-seperated list
357 * of commands which are disabled by the
358 * administrator of the server for non-opers.
360 char DisabledCommands[MAXBUF];
362 /** The full path to the modules directory.
363 * This is either set at compile time, or
364 * overridden in the configuration file via
369 /** The full pathname to the executable, as
370 * given in argv[0] when the program starts.
372 char MyExecutable[1024];
374 /** The file handle of the logfile. If this
375 * value is NULL, the log file is not open,
376 * probably due to a permissions error on
377 * startup (this should not happen in normal
382 /** If this value is true, the owner of the
383 * server specified -nofork on the command
384 * line, causing the daemon to stay in the
389 /** If this value if true then all log
390 * messages will be output, regardless of
391 * the level given in the config file.
392 * This is set with the -debug commandline
397 /** If this is true then log output will be
398 * written to the logfile. This is the default.
399 * If you put -nolog on the commandline then
400 * the logfile will not be written.
401 * This is meant to be used in conjunction with
402 * -debug for debugging without filling up the
407 /** If this value is true, halfops have been
408 * enabled in the configuration file.
412 /** If this is set to true, then mode lists (e.g
413 * MODE #chan b) are hidden from unprivileged
416 bool HideModeLists[256];
418 /** If this is set to true, then channel operators
419 * are exempt from this channel mode. Used for +Sc etc.
421 bool ExemptChanOps[256];
423 /** The number of seconds the DNS subsystem
424 * will wait before timing out any request.
428 /** The size of the read() buffer in the user
429 * handling code, used to read data into a user's
434 /** The value to be used for listen() backlogs
439 /** The soft limit value assigned to the irc server.
440 * The IRC server will not allow more than this
441 * number of local users.
443 unsigned int SoftLimit;
445 /** Maximum number of targets for a multi target command
446 * such as PRIVMSG or KICK
448 unsigned int MaxTargets;
450 /** The maximum number of /WHO results allowed
451 * in any single /WHO command.
455 /** True if the DEBUG loglevel is selected.
459 /** The loglevel in use by the IRC server
463 /** How many seconds to wait before exiting
464 * the program when /DIE is correctly issued.
468 /** True if we're going to hide netsplits as *.net *.split for non-opers
472 /** True if we're going to hide ban reasons for non-opers (e.g. G-Lines,
477 /** Announce invites to the channel with a server notice
479 InviteAnnounceState AnnounceInvites;
481 /** If this is enabled then operators will
482 * see invisible (+i) channels in /whois.
486 /** Set to a non-empty string to obfuscate the server name of users in WHOIS
488 char HideWhoisServer[MAXBUF];
490 /** Set to a non empty string to obfuscate nicknames prepended to a KILL.
492 char HideKillsServer[MAXBUF];
494 /** The MOTD file, cached in a file_cache type.
498 /** The RULES file, cached in a file_cache type.
502 /** The full pathname and filename of the PID
503 * file as defined in the configuration.
507 /** The connect classes in use by the IRC server.
511 /** A list of the classes for listening client ports
513 std::vector<ListenSocket*> ports;
515 /** A list of ports claimed by IO Modules
517 std::map<int,Module*> IOHookModule;
519 std::map<BufferedSocket*, Module*> SocketIOHookModule;
521 /** The 005 tokens of this server (ISUPPORT)
522 * populated/repopulated upon loading or unloading
529 std::vector<std::string> isupport;
531 /** STATS characters in this list are available
534 char UserStats[MAXBUF];
536 /** The path and filename of the ircd.log file
540 /** Default channel modes
542 char DefaultModes[MAXBUF];
544 /** Custom version string, which if defined can replace the system info in VERSION.
546 char CustomVersion[MAXBUF];
548 /** List of u-lined servers
550 std::map<irc::string, bool> ulines;
552 /** Max banlist sizes for channels (the std::string is a glob)
554 std::map<std::string, int> maxbans;
556 /** Directory where the inspircd binary resides
560 /** If set to true, no user DNS lookups are to be performed
564 /** If set to true, provide syntax hints for unknown commands
568 /** If set to true, users appear to quit then rejoin when their hosts change.
569 * This keeps clients synchronized properly.
573 /** If set to true, prefixed channel NOTICEs and PRIVMSGs will have the prefix
574 * added to the outgoing text for undernet style msg prefixing.
576 bool UndernetMsgPrefix;
578 /** If set to true, the full nick!user@host will be shown in the TOPIC command
579 * for who set the topic last. If false, only the nick is shown.
581 bool FullHostInTopic;
583 /** All oper type definitions from the config file
585 opertype_t opertypes;
587 /** All oper class definitions from the config file
589 operclass_t operclass;
591 /** Saved argv from startup
595 /** Saved argc from startup
599 /** Max channels per user
601 unsigned int MaxChans;
603 /** Oper max channels per user
605 unsigned int OperMaxChans;
607 /** TS6-like server ID.
608 * NOTE: 000...999 are usable for InspIRCd servers. This
609 * makes code simpler. 0AA, 1BB etc with letters are reserved
614 /** Construct a new ServerConfig
616 ServerConfig(InspIRCd* Instance);
618 /** Clears the include stack in preperation for a Read() call.
622 /** Get server ID as string with required leading zeroes
624 std::string GetSID();
626 /** Update the 005 vector
630 /** Send the 005 numerics (ISUPPORT) to a user
632 void Send005(User* user);
634 /** Read the entire configuration into memory
635 * and initialize this class. All other methods
636 * should be used only by the core.
638 void Read(bool bail, User* user, int pass);
640 /** Read a file into a file_cache object
642 bool ReadFile(file_cache &F, const char* fname);
644 /** Report a configuration error given in errormessage.
645 * @param bail If this is set to true, the error is sent to the console, and the program exits
646 * @param user If this is set to a non-null value, and bail is false, the errors are spooled to
647 * this user as SNOTICEs.
648 * If the parameter is NULL, the messages are spooled to all users via WriteOpers as SNOTICEs.
650 void ReportConfigError(const std::string &errormessage, bool bail, User* user);
652 /** Load 'filename' into 'target', with the new config parser everything is parsed into
653 * tag/key/value at load-time rather than at read-value time.
655 bool LoadConf(ConfigDataHash &target, const char* filename, std::ostringstream &errorstream, int pass);
657 /** Load 'filename' into 'target', with the new config parser everything is parsed into
658 * tag/key/value at load-time rather than at read-value time.
660 bool LoadConf(ConfigDataHash &target, const std::string &filename, std::ostringstream &errorstream, int pass);
662 /* Both these return true if the value existed or false otherwise */
664 /** Writes 'length' chars into 'result' as a string
666 bool ConfValue(ConfigDataHash &target, const char* tag, const char* var, int index, char* result, int length, bool allow_linefeeds = false);
667 /** Writes 'length' chars into 'result' as a string
669 bool ConfValue(ConfigDataHash &target, const char* tag, const char* var, const char* default_value, int index, char* result, int length, bool allow_linefeeds = false);
671 /** Writes 'length' chars into 'result' as a string
673 bool ConfValue(ConfigDataHash &target, const std::string &tag, const std::string &var, int index, std::string &result, bool allow_linefeeds = false);
674 /** Writes 'length' chars into 'result' as a string
676 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);
678 /** Tries to convert the value to an integer and write it to 'result'
680 bool ConfValueInteger(ConfigDataHash &target, const char* tag, const char* var, int index, int &result);
681 /** Tries to convert the value to an integer and write it to 'result'
683 bool ConfValueInteger(ConfigDataHash &target, const char* tag, const char* var, const char* default_value, int index, int &result);
684 /** Tries to convert the value to an integer and write it to 'result'
686 bool ConfValueInteger(ConfigDataHash &target, const std::string &tag, const std::string &var, int index, int &result);
687 /** Tries to convert the value to an integer and write it to 'result'
689 bool ConfValueInteger(ConfigDataHash &target, const std::string &tag, const std::string &var, const std::string &default_value, int index, int &result);
691 /** Returns true if the value exists and has a true value, false otherwise
693 bool ConfValueBool(ConfigDataHash &target, const char* tag, const char* var, int index);
694 /** Returns true if the value exists and has a true value, false otherwise
696 bool ConfValueBool(ConfigDataHash &target, const char* tag, const char* var, const char* default_value, int index);
697 /** Returns true if the value exists and has a true value, false otherwise
699 bool ConfValueBool(ConfigDataHash &target, const std::string &tag, const std::string &var, int index);
700 /** Returns true if the value exists and has a true value, false otherwise
702 bool ConfValueBool(ConfigDataHash &target, const std::string &tag, const std::string &var, const std::string &default_value, int index);
704 /** Returns the number of occurences of tag in the config file
706 int ConfValueEnum(ConfigDataHash &target, const char* tag);
707 /** Returns the number of occurences of tag in the config file
709 int ConfValueEnum(ConfigDataHash &target, const std::string &tag);
711 /** Returns the numbers of vars inside the index'th 'tag in the config file
713 int ConfVarEnum(ConfigDataHash &target, const char* tag, int index);
714 /** Returns the numbers of vars inside the index'th 'tag in the config file
716 int ConfVarEnum(ConfigDataHash &target, const std::string &tag, int index);
718 void ValidateHostname(const char* p, const std::string &tag, const std::string &val);
720 void ValidateIP(const char* p, const std::string &tag, const std::string &val, bool wild);
722 void ValidateNoSpaces(const char* p, const std::string &tag, const std::string &val);
724 /** Get a pointer to the module which has hooked the given port.
725 * @parameter port Port number
726 * @return Returns a pointer to the hooking module, or NULL
728 Module* GetIOHook(int port);
730 /** Hook a module to a client port, so that it can receive notifications
731 * of low-level port activity.
732 * @param port The port number
733 * @param Module the module to hook to the port
734 * @return True if the hook was successful.
736 bool AddIOHook(int port, Module* iomod);
738 /** Delete a module hook from a client port.
739 * @param port The port to detatch from
740 * @return True if successful
742 bool DelIOHook(int port);
744 /** Get a pointer to the module which has hooked the given BufferedSocket class.
745 * @parameter port Port number
746 * @return Returns a pointer to the hooking module, or NULL
748 Module* GetIOHook(BufferedSocket* is);
750 /** Hook a module to an BufferedSocket class, so that it can receive notifications
751 * of low-level socket activity.
752 * @param iomod The module to hook to the socket
753 * @param is The BufferedSocket to attach to
754 * @return True if the hook was successful.
756 bool AddIOHook(Module* iomod, BufferedSocket* is);
758 /** Delete a module hook from an BufferedSocket.
759 * @param is The BufferedSocket to detatch from.
760 * @return True if the unhook was successful
762 bool DelIOHook(BufferedSocket* is);
764 /** Returns the fully qualified path to the inspircd directory
765 * @return The full program directory
767 std::string GetFullProgDir();
769 /** Returns true if a directory is valid (within the modules directory).
770 * @param dirandfile The directory and filename to check
771 * @return True if the directory is valid
773 static bool DirValid(const char* dirandfile);
775 /** Clean a filename, stripping the directories (and drives) from string.
776 * @param name Directory to tidy
777 * @return The cleaned filename
779 static char* CleanFilename(char* name);
781 /** Check if a file exists.
782 * @param file The full path to a file
783 * @return True if the file exists and is readable.
785 static bool FileExists(const char* file);
789 /** Initialize the disabled commands list
791 CoreExport bool InitializeDisabledCommands(const char* data, InspIRCd* ServerInstance);
793 /** Initialize the oper types
795 bool InitTypes(ServerConfig* conf, const char* tag);
797 /** Initialize the oper classes
799 bool InitClasses(ServerConfig* conf, const char* tag);
801 /** Initialize an oper type
803 bool DoType(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
805 /** Initialize an oper class
807 bool DoClass(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
809 /** Finish initializing the oper types and classes
811 bool DoneClassesAndTypes(ServerConfig* conf, const char* tag);
815 /** Initialize x line
817 bool InitXLine(ServerConfig* conf, const char* tag);
819 /** Add a config-defined zline
821 bool DoZLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
823 /** Add a config-defined qline
825 bool DoQLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
827 /** Add a config-defined kline
829 bool DoKLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
831 /** Add a config-defined eline
833 bool DoELine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);