2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
16 * Configuration management (reading, parsing & validation)
35 #include <sys/types.h>
52 static bool Use_Log = true, Using_MotdFile = true;
53 static CONF_SERVER New_Server;
54 static int New_Server_Idx;
56 static char Conf_MotdFile[FNAME_LEN];
57 static char Conf_HelpFile[FNAME_LEN];
58 static char Conf_IncludeDir[FNAME_LEN];
60 static void Set_Defaults PARAMS(( bool InitServers ));
61 static bool Read_Config PARAMS(( bool TestOnly, bool IsStarting ));
62 static void Read_Config_File PARAMS(( const char *File, FILE *fd ));
63 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
65 static void Handle_GLOBAL PARAMS((const char *File, int Line,
66 char *Var, char *Arg ));
67 static void Handle_LIMITS PARAMS((const char *File, int Line,
68 char *Var, char *Arg ));
69 static void Handle_OPTIONS PARAMS((const char *File, int Line,
70 char *Var, char *Arg ));
71 static void Handle_OPERATOR PARAMS((const char *File, int Line,
72 char *Var, char *Arg ));
73 static void Handle_SERVER PARAMS((const char *File, int Line,
74 char *Var, char *Arg ));
75 static void Handle_CHANNEL PARAMS((const char *File, int Line,
76 char *Var, char *Arg ));
78 static void Config_Error PARAMS((const int Level, const char *Format, ...));
80 static void Config_Error_NaN PARAMS((const char *File, const int LINE,
82 static void Config_Error_Section PARAMS((const char *File, const int Line,
83 const char *Item, const char *Section));
84 static void Config_Error_TooLong PARAMS((const char *File, const int LINE,
87 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
91 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
93 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
99 static void Handle_SSL PARAMS((const char *File, int Line, char *Var, char *Ark));
101 struct SSLOptions Conf_SSLOptions;
104 * Initialize SSL configuration.
109 free(Conf_SSLOptions.KeyFile);
110 Conf_SSLOptions.KeyFile = NULL;
112 free(Conf_SSLOptions.CertFile);
113 Conf_SSLOptions.CertFile = NULL;
115 free(Conf_SSLOptions.DHFile);
116 Conf_SSLOptions.DHFile = NULL;
117 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
119 array_free(&Conf_SSLOptions.ListenPorts);
123 * Check if the current configuration uses/requires SSL.
125 * @returns true if SSL is used and should be initialized.
132 /* SSL listen ports configured? */
133 if (array_bytes(&Conf_SSLOptions.ListenPorts))
136 for (i = 0; i < MAX_SERVERS; i++) {
137 if (Conf_Server[i].port > 0
138 && Conf_Server[i].SSLConnect)
145 * Make sure that a configured file is readable.
147 * Currently, this function is only used for SSL-related options ...
149 * @param Var Configuration variable
150 * @param Filename Configured filename
153 CheckFileReadable(const char *Var, const char *Filename)
160 fp = fopen(Filename, "r");
164 Config_Error(LOG_ERR, "Can't read \"%s\" (\"%s\"): %s",
165 Filename, Var, strerror(errno));
172 * Duplicate string and warn on errors.
174 * @returns Pointer to string on success, NULL otherwise.
177 strdup_warn(const char *str)
179 char *ptr = strdup(str);
181 Config_Error(LOG_ERR,
182 "Could not allocate memory for string: %s", str);
187 * Output a comma separated list of ports (integer values).
194 len = array_length(a, sizeof(UINT16));
196 ports = (UINT16*) array_start(a);
197 printf("%u", (unsigned int) *ports);
200 printf(", %u", (unsigned int) *ports);
207 * Parse a comma separated string into an array of port numbers (integers).
210 ports_parse(array *a, int Line, char *Arg)
218 ptr = strtok( Arg, "," );
222 if (port > 0 && port < 0xFFFF) {
223 port16 = (UINT16) port;
224 if (!array_catb(a, (char*)&port16, sizeof port16))
225 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
226 NGIRCd_ConfFile, Line, port, strerror(errno));
228 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
229 NGIRCd_ConfFile, Line, port );
232 ptr = strtok( NULL, "," );
237 * Initialize configuration module.
242 Read_Config(false, true);
243 Validate_Config(false, false);
247 * "Rehash" (reload) server configuration.
249 * @returns true if configuration has been re-read, false on errors.
254 if (!Read_Config(false, false))
256 Validate_Config(false, true);
258 /* Update CLIENT structure of local server */
259 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
264 * Output a boolean value as "yes/no" string.
267 yesno_to_str(int boolean_value)
275 * Free all IRC operator configuration structures.
280 struct Conf_Oper *op;
283 len = array_length(&Conf_Opers, sizeof(*op));
284 op = array_start(&Conf_Opers);
289 array_free(&Conf_Opers);
293 * Output all IRC operator configuration structures.
298 struct Conf_Oper *op;
301 count = array_length(&Conf_Opers, sizeof(*op));
302 op = array_start(&Conf_Opers);
303 for (i = 0; i < count; i++, op++) {
308 printf(" Name = %s\n", op->name);
309 printf(" Password = %s\n", op->pwd);
310 printf(" Mask = %s\n\n", op->mask ? op->mask : "");
315 * Read configuration, validate and output it.
317 * This function waits for a keypress of the user when stdin/stdout are valid
318 * tty's ("you can read our nice message and we can read in your keypress").
320 * @return 0 on success, 1 on failure(s); therefore the result code can
321 * directly be used by exit() when running "ngircd --configtest".
330 size_t predef_channel_count;
331 struct Conf_Channel *predef_chan;
335 if (!Read_Config(true, true))
338 config_valid = Validate_Config(true, false);
341 if(isatty(fileno(stdin)) && isatty(fileno(stdout))) {
342 puts("OK, press enter to see a dump of your server configuration ...");
345 puts("Ok, dump of your server configuration follows:\n");
348 printf(" Name = %s\n", Conf_ServerName);
349 printf(" AdminInfo1 = %s\n", Conf_ServerAdmin1);
350 printf(" AdminInfo2 = %s\n", Conf_ServerAdmin2);
351 printf(" AdminEMail = %s\n", Conf_ServerAdminMail);
352 printf(" HelpFile = %s\n", Conf_HelpFile);
353 printf(" Info = %s\n", Conf_ServerInfo);
354 printf(" Listen = %s\n", Conf_ListenAddress);
355 if (Using_MotdFile) {
356 printf(" MotdFile = %s\n", Conf_MotdFile);
357 printf(" MotdPhrase =\n");
359 printf(" MotdFile = \n");
360 printf(" MotdPhrase = %s\n", array_bytes(&Conf_Motd)
361 ? (const char*) array_start(&Conf_Motd) : "");
364 printf(" Password = %s\n", Conf_ServerPwd);
366 printf(" PidFile = %s\n", Conf_PidFile);
368 ports_puts(&Conf_ListenPorts);
369 grp = getgrgid(Conf_GID);
371 printf(" ServerGID = %s\n", grp->gr_name);
373 printf(" ServerGID = %ld\n", (long)Conf_GID);
374 pwd = getpwuid(Conf_UID);
376 printf(" ServerUID = %s\n", pwd->pw_name);
378 printf(" ServerUID = %ld\n", (long)Conf_UID);
382 printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
383 printf(" IdleTimeout = %d\n", Conf_IdleTimeout);
384 printf(" MaxConnections = %d\n", Conf_MaxConnections);
385 printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
386 printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
387 printf(" MaxNickLength = %u\n", Conf_MaxNickLength - 1);
388 printf(" MaxListSize = %d\n", Conf_MaxListSize);
389 printf(" PingTimeout = %d\n", Conf_PingTimeout);
390 printf(" PongTimeout = %d\n", Conf_PongTimeout);
394 printf(" AllowedChannelTypes = %s\n", Conf_AllowedChannelTypes);
395 printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
396 printf(" ChrootDir = %s\n", Conf_Chroot);
397 printf(" CloakHost = %s\n", Conf_CloakHost);
398 printf(" CloakHostModeX = %s\n", Conf_CloakHostModeX);
399 printf(" CloakHostSalt = %s\n", Conf_CloakHostSalt);
400 printf(" CloakUserToNick = %s\n", yesno_to_str(Conf_CloakUserToNick));
402 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
403 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
405 printf(" DefaultUserModes = %s\n", Conf_DefaultUserModes);
406 printf(" DNS = %s\n", yesno_to_str(Conf_DNS));
408 printf(" Ident = %s\n", yesno_to_str(Conf_Ident));
410 printf(" IncludeDir = %s\n", Conf_IncludeDir);
411 printf(" MorePrivacy = %s\n", yesno_to_str(Conf_MorePrivacy));
412 printf(" NoticeAuth = %s\n", yesno_to_str(Conf_NoticeAuth));
413 printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
414 printf(" OperChanPAutoOp = %s\n", yesno_to_str(Conf_OperChanPAutoOp));
415 printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
417 printf(" PAM = %s\n", yesno_to_str(Conf_PAM));
418 printf(" PAMIsOptional = %s\n", yesno_to_str(Conf_PAMIsOptional));
421 printf(" RequireAuthPing = %s\n", yesno_to_str(Conf_AuthPing));
423 printf(" ScrubCTCP = %s\n", yesno_to_str(Conf_ScrubCTCP));
425 printf(" SyslogFacility = %s\n",
426 ngt_SyslogFacilityName(Conf_SyslogFacility));
428 printf(" WebircPassword = %s\n", Conf_WebircPwd);
433 printf(" CertFile = %s\n", Conf_SSLOptions.CertFile
434 ? Conf_SSLOptions.CertFile : "");
435 printf(" DHFile = %s\n", Conf_SSLOptions.DHFile
436 ? Conf_SSLOptions.DHFile : "");
437 printf(" KeyFile = %s\n", Conf_SSLOptions.KeyFile
438 ? Conf_SSLOptions.KeyFile : "");
439 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
440 puts(" KeyFilePassword = <secret>");
442 puts(" KeyFilePassword = ");
443 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
445 ports_puts(&Conf_SSLOptions.ListenPorts);
451 for( i = 0; i < MAX_SERVERS; i++ ) {
452 if( ! Conf_Server[i].name[0] ) continue;
454 /* Valid "Server" section */
456 printf( " Name = %s\n", Conf_Server[i].name );
457 printf( " Host = %s\n", Conf_Server[i].host );
458 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
460 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
462 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
463 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
464 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
465 printf( " Group = %d\n", Conf_Server[i].group );
466 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
469 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
470 predef_chan = array_start(&Conf_Channels);
472 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
473 if (!predef_chan->name[0])
476 /* Valid "Channel" section */
478 printf(" Name = %s\n", predef_chan->name);
479 printf(" Modes = %s\n", predef_chan->modes);
480 printf(" Key = %s\n", predef_chan->key);
481 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
482 printf(" Topic = %s\n", predef_chan->topic);
483 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
486 return (config_valid ? 0 : 1);
490 * Remove connection information from configured server.
492 * If the server is set as "once", delete it from our configuration;
493 * otherwise set the time for the next connection attempt.
495 * Non-server connections will be silently ignored.
498 Conf_UnsetServer( CONN_ID Idx )
503 /* Check all our configured servers */
504 for( i = 0; i < MAX_SERVERS; i++ ) {
505 if( Conf_Server[i].conn_id != Idx ) continue;
507 /* Gotcha! Mark server configuration as "unused": */
508 Conf_Server[i].conn_id = NONE;
510 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
511 /* Delete configuration here */
512 Init_Server_Struct( &Conf_Server[i] );
514 /* Set time for next connect attempt */
516 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
517 /* The connection has been "long", so we don't
518 * require the next attempt to be delayed. */
519 Conf_Server[i].lasttry =
520 t - Conf_ConnectRetry + RECONNECT_DELAY;
522 /* "Short" connection, enforce "ConnectRetry"
523 * but randomize it a little bit: 15 seconds. */
524 Conf_Server[i].lasttry =
525 t + rand() / (RAND_MAX / 15);
532 * Set connection information for specified configured server.
535 Conf_SetServer( int ConfServer, CONN_ID Idx )
537 assert( ConfServer > NONE );
538 assert( Idx > NONE );
540 if (Conf_Server[ConfServer].conn_id > NONE &&
541 Conf_Server[ConfServer].conn_id != Idx) {
543 "Connection %d: Server configuration of \"%s\" already in use by connection %d!",
544 Idx, Conf_Server[ConfServer].name,
545 Conf_Server[ConfServer].conn_id);
546 Conn_Close(Idx, NULL, "Server configuration already in use", true);
549 Conf_Server[ConfServer].conn_id = Idx;
554 * Get index of server in configuration structure.
557 Conf_GetServer( CONN_ID Idx )
561 assert( Idx > NONE );
563 for( i = 0; i < MAX_SERVERS; i++ ) {
564 if( Conf_Server[i].conn_id == Idx ) return i;
570 * Enable a server by name and adjust its port number.
572 * @returns true if a server has been enabled and now has a valid port
573 * number and host name for outgoing connections.
576 Conf_EnableServer( const char *Name, UINT16 Port )
580 assert( Name != NULL );
581 for( i = 0; i < MAX_SERVERS; i++ ) {
582 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
583 /* Gotcha! Set port and enable server: */
584 Conf_Server[i].port = Port;
585 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
586 return (Conf_Server[i].port && Conf_Server[i].host[0]);
593 * Enable a server by name.
595 * The server is only usable as outgoing server, if it has set a valid port
596 * number for outgoing connections!
597 * If not, you have to use Conf_EnableServer() function to make it available.
599 * @returns true if a server has been enabled; false otherwise.
602 Conf_EnablePassiveServer(const char *Name)
606 assert( Name != NULL );
607 for (i = 0; i < MAX_SERVERS; i++) {
608 if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
609 && (Conf_Server[i].port > 0)) {
610 /* BINGO! Enable server */
611 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
619 * Disable a server by name.
620 * An already established connection will be disconnected.
622 * @returns true if a server was found and has been disabled.
625 Conf_DisableServer( const char *Name )
629 assert( Name != NULL );
630 for( i = 0; i < MAX_SERVERS; i++ ) {
631 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
632 /* Gotcha! Disable and disconnect server: */
633 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
634 if( Conf_Server[i].conn_id > NONE )
635 Conn_Close(Conf_Server[i].conn_id, NULL,
636 "Server link terminated on operator request",
645 * Add a new remote server to our configuration.
647 * @param Name Name of the new server.
648 * @param Port Port number to connect to or 0 for incoming connections.
649 * @param Host Host name to connect to.
650 * @param MyPwd Password that will be sent to the peer.
651 * @param PeerPwd Password that must be received from the peer.
652 * @returns true if the new server has been added; false otherwise.
655 Conf_AddServer(const char *Name, UINT16 Port, const char *Host,
656 const char *MyPwd, const char *PeerPwd)
660 assert( Name != NULL );
661 assert( Host != NULL );
662 assert( MyPwd != NULL );
663 assert( PeerPwd != NULL );
665 /* Search unused item in server configuration structure */
666 for( i = 0; i < MAX_SERVERS; i++ ) {
667 /* Is this item used? */
668 if( ! Conf_Server[i].name[0] ) break;
670 if( i >= MAX_SERVERS ) return false;
672 Init_Server_Struct( &Conf_Server[i] );
673 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
674 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
675 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
676 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
677 Conf_Server[i].port = Port;
678 Conf_Server[i].flags = CONF_SFLAG_ONCE;
684 * Check if the given nickname is reserved for services on a particular server.
686 * @param ConfServer The server index to check.
687 * @param Nick The nickname to check.
688 * @returns true if the given nickname belongs to an "IRC service".
691 Conf_NickIsService(int ConfServer, const char *Nick)
693 assert (ConfServer >= 0);
694 assert (ConfServer < MAX_SERVERS);
696 return MatchCaseInsensitiveList(Conf_Server[ConfServer].svs_mask,
701 * Check if the given nickname is blocked for "normal client" use.
703 * @param ConfServer The server index or NONE to check all configured servers.
704 * @param Nick The nickname to check.
705 * @returns true if the given nickname belongs to an "IRC service".
708 Conf_NickIsBlocked(const char *Nick)
712 for(i = 0; i < MAX_SERVERS; i++) {
713 if (!Conf_Server[i].name[0])
715 if (Conf_NickIsService(i, Nick))
722 * Initialize configuration settings with their default values.
725 Set_Defaults(bool InitServers)
728 char random[RANDOM_SALT_LEN + 1];
731 strcpy(Conf_ServerName, "");
732 strcpy(Conf_ServerAdmin1, "");
733 strcpy(Conf_ServerAdmin2, "");
734 strcpy(Conf_ServerAdminMail, "");
735 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
736 PACKAGE_NAME, PACKAGE_VERSION);
737 free(Conf_ListenAddress);
738 Conf_ListenAddress = NULL;
739 array_free(&Conf_ListenPorts);
740 array_free(&Conf_Motd);
741 array_free(&Conf_Helptext);
742 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
743 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
744 strlcpy(Conf_HelpFile, DOCDIR, sizeof(Conf_HelpFile));
745 strlcat(Conf_HelpFile, HELP_FILE, sizeof(Conf_HelpFile));
746 strcpy(Conf_ServerPwd, "");
747 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
748 Conf_UID = Conf_GID = 0;
751 Conf_ConnectRetry = 60;
752 Conf_IdleTimeout = 0;
753 Conf_MaxConnections = 0;
754 Conf_MaxConnectionsIP = 5;
756 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
757 Conf_MaxListSize = 100;
758 Conf_PingTimeout = 120;
759 Conf_PongTimeout = 20;
762 strlcpy(Conf_AllowedChannelTypes, CHANTYPES,
763 sizeof(Conf_AllowedChannelTypes));
764 Conf_AllowRemoteOper = false;
766 Conf_AuthPing = false;
768 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
769 strcpy(Conf_CloakHost, "");
770 strcpy(Conf_CloakHostModeX, "");
771 strlcpy(Conf_CloakHostSalt, ngt_RandomStr(random, RANDOM_SALT_LEN),
772 sizeof(Conf_CloakHostSalt));
773 Conf_CloakUserToNick = false;
774 Conf_ConnectIPv4 = true;
776 Conf_ConnectIPv6 = true;
778 Conf_ConnectIPv6 = false;
780 strcpy(Conf_DefaultUserModes, "");
787 strcpy(Conf_IncludeDir, "");
788 Conf_MorePrivacy = false;
789 Conf_NoticeAuth = false;
790 Conf_OperCanMode = false;
791 Conf_OperChanPAutoOp = true;
792 Conf_OperServerMode = false;
798 Conf_PAMIsOptional = false;
800 Conf_ScrubCTCP = false;
802 Conf_SyslogFacility = LOG_LOCAL5;
804 Conf_SyslogFacility = 0;
808 /* Initialize server configuration structures */
810 for (i = 0; i < MAX_SERVERS;
811 Init_Server_Struct(&Conf_Server[i++]));
816 * Get number of configured listening ports.
818 * @returns The number of ports (IPv4+IPv6) on which the server should listen.
823 size_t cnt = array_bytes(&Conf_ListenPorts);
825 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
831 * Read contents of a text file into an array.
833 * This function is used to read the MOTD and help text file, for example.
835 * @param filename Name of the file to read.
836 * @return true, when the file has been read in.
839 Read_TextFile(const char *Filename, const char *Name, array *Destination)
845 if (*Filename == '\0')
848 fp = fopen(Filename, "r");
850 Config_Error(LOG_ERR, "Can't read %s file \"%s\": %s",
851 Name, Filename, strerror(errno));
855 array_free(Destination);
856 while (fgets(line, (int)sizeof line, fp)) {
857 ngt_TrimLastChr(line, '\n');
859 /* add text including \0 */
860 if (!array_catb(Destination, line, strlen(line) + 1)) {
861 Log(LOG_ERR, "Cannot read/add \"%s\", line %d: %s",
862 Filename, line_no, strerror(errno));
872 * Read ngIRCd configuration file.
874 * Please note that this function uses exit(1) on fatal errors and therefore
875 * can result in ngIRCd terminating!
877 * @param ngircd_starting Flag indicating if ngIRCd is starting or not.
878 * @returns true when the configuration file has been read
879 * successfully; false otherwise.
882 Read_Config(bool TestOnly, bool IsStarting)
884 const UINT16 defaultport = 6667;
885 char *ptr, file[FNAME_LEN];
886 struct dirent *entry;
891 /* Open configuration file */
892 fd = fopen( NGIRCd_ConfFile, "r" );
894 /* No configuration file found! */
895 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
896 NGIRCd_ConfFile, strerror( errno ));
899 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
904 Set_Defaults(IsStarting);
907 Config_Error(LOG_INFO,
908 "Reading configuration from \"%s\" ...",
911 /* Clean up server configuration structure: mark all already
912 * configured servers as "once" so that they are deleted
913 * after the next disconnect and delete all unused servers.
914 * And delete all servers which are "duplicates" of servers
915 * that are already marked as "once" (such servers have been
916 * created by the last rehash but are now useless). */
917 for( i = 0; i < MAX_SERVERS; i++ ) {
918 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
920 /* This structure is in use ... */
921 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
922 /* Check for duplicates */
923 for( n = 0; n < MAX_SERVERS; n++ ) {
924 if( n == i ) continue;
926 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
927 Init_Server_Struct( &Conf_Server[n] );
929 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
935 /* Mark server as "once" */
936 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
937 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
942 /* Initialize variables */
943 Init_Server_Struct( &New_Server );
944 New_Server_Idx = NONE;
949 Read_Config_File(NGIRCd_ConfFile, fd);
952 if (Conf_IncludeDir[0]) {
953 dh = opendir(Conf_IncludeDir);
955 Config_Error(LOG_ALERT,
956 "Can't open include directory \"%s\": %s",
957 Conf_IncludeDir, strerror(errno));
959 strlcpy(Conf_IncludeDir, SYSCONFDIR, sizeof(Conf_IncludeDir));
960 strlcat(Conf_IncludeDir, CONFIG_DIR, sizeof(Conf_IncludeDir));
961 dh = opendir(Conf_IncludeDir);
964 /* Include further configuration files, if IncludeDir is available */
966 while ((entry = readdir(dh)) != NULL) {
967 ptr = strrchr(entry->d_name, '.');
968 if (!ptr || strcasecmp(ptr, ".conf") != 0)
970 snprintf(file, sizeof(file), "%s/%s",
971 Conf_IncludeDir, entry->d_name);
973 Config_Error(LOG_INFO,
974 "Reading configuration from \"%s\" ...",
976 fd = fopen(file, "r");
978 Read_Config_File(file, fd);
981 Config_Error(LOG_ALERT,
982 "Can't read configuration \"%s\": %s",
983 file, strerror(errno));
988 /* Check if there is still a server to add */
989 if( New_Server.name[0] ) {
990 /* Copy data to "real" server structure */
991 assert( New_Server_Idx > NONE );
992 Conf_Server[New_Server_Idx] = New_Server;
995 /* not a single listening port? Add default. */
996 if (no_listenports() &&
997 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
999 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
1000 (unsigned int) defaultport, strerror(errno));
1005 if (!Conf_ListenAddress)
1006 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
1008 if (!Conf_ListenAddress) {
1009 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
1013 /* No MOTD phrase configured? (re)try motd file. */
1014 if (array_bytes(&Conf_Motd) == 0) {
1015 if (Read_TextFile(Conf_MotdFile, "MOTD", &Conf_Motd))
1016 Using_MotdFile = true;
1019 /* Try to read ngIRCd help text file. */
1020 (void)Read_TextFile(Conf_HelpFile, "help text", &Conf_Helptext);
1021 if (!array_bytes(&Conf_Helptext))
1022 Config_Error(LOG_WARNING,
1023 "No help text available, HELP command will be of limited use.");
1026 /* Make sure that all SSL-related files are readable */
1027 CheckFileReadable("CertFile", Conf_SSLOptions.CertFile);
1028 CheckFileReadable("DHFile", Conf_SSLOptions.DHFile);
1029 CheckFileReadable("KeyFile", Conf_SSLOptions.KeyFile);
1038 static void Read_Config_File(const char *File, FILE *fd)
1040 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
1044 /* Read configuration file */
1047 if (!fgets(str, LINE_LEN, fd))
1052 /* Skip comments and empty lines */
1053 if (str[0] == ';' || str[0] == '#' || str[0] == '\0')
1056 /* Is this the beginning of a new section? */
1057 if ((str[0] == '[') && (str[strlen(str) - 1] == ']')) {
1058 strlcpy(section, str, sizeof(section));
1059 if (strcasecmp(section, "[GLOBAL]") == 0
1060 || strcasecmp(section, "[LIMITS]") == 0
1061 || strcasecmp(section, "[OPTIONS]") == 0
1063 || strcasecmp(section, "[SSL]") == 0
1068 if (strcasecmp(section, "[SERVER]") == 0) {
1069 /* Check if there is already a server to add */
1070 if (New_Server.name[0]) {
1071 /* Copy data to "real" server structure */
1072 assert(New_Server_Idx > NONE);
1073 Conf_Server[New_Server_Idx] =
1077 /* Re-init structure for new server */
1078 Init_Server_Struct(&New_Server);
1080 /* Search unused item in server configuration structure */
1081 for (i = 0; i < MAX_SERVERS; i++) {
1082 /* Is this item used? */
1083 if (!Conf_Server[i].name[0])
1086 if (i >= MAX_SERVERS) {
1087 /* Oops, no free item found! */
1088 Config_Error(LOG_ERR,
1089 "Too many servers configured.");
1090 New_Server_Idx = NONE;
1096 if (strcasecmp(section, "[CHANNEL]") == 0) {
1097 count = array_length(&Conf_Channels,
1102 sizeof(struct Conf_Channel), count)) {
1103 Config_Error(LOG_ERR,
1104 "Could not allocate memory for new operator (line %d)",
1110 if (strcasecmp(section, "[OPERATOR]") == 0) {
1111 count = array_length(&Conf_Opers,
1112 sizeof(struct Conf_Oper));
1113 if (!array_alloc(&Conf_Opers,
1114 sizeof(struct Conf_Oper),
1116 Config_Error(LOG_ERR,
1117 "Could not allocate memory for new channel (line &d)",
1123 Config_Error(LOG_ERR,
1124 "%s, line %d: Unknown section \"%s\"!",
1125 NGIRCd_ConfFile, line, section);
1128 if (section[0] == 0x1)
1131 /* Split line into variable name and parameters */
1132 ptr = strchr(str, '=');
1134 Config_Error(LOG_ERR, "%s, line %d: Syntax error!",
1135 NGIRCd_ConfFile, line);
1144 if (strcasecmp(section, "[GLOBAL]") == 0)
1145 Handle_GLOBAL(File, line, var, arg);
1146 else if (strcasecmp(section, "[LIMITS]") == 0)
1147 Handle_LIMITS(File, line, var, arg);
1148 else if (strcasecmp(section, "[OPTIONS]") == 0)
1149 Handle_OPTIONS(File, line, var, arg);
1151 else if (strcasecmp(section, "[SSL]") == 0)
1152 Handle_SSL(File, line, var, arg);
1154 else if (strcasecmp(section, "[OPERATOR]") == 0)
1155 Handle_OPERATOR(File, line, var, arg);
1156 else if (strcasecmp(section, "[SERVER]") == 0)
1157 Handle_SERVER(File, line, var, arg);
1158 else if (strcasecmp(section, "[CHANNEL]") == 0)
1159 Handle_CHANNEL(File, line, var, arg);
1161 Config_Error(LOG_ERR,
1162 "%s, line %d: Variable \"%s\" outside section!",
1163 NGIRCd_ConfFile, line, var);
1168 * Check whether a string argument is "true" or "false".
1170 * @param Arg Input string.
1171 * @returns true if the input string has been parsed as "yes", "true"
1172 * (case insensitive) or a non-zero integer value.
1175 Check_ArgIsTrue(const char *Arg)
1177 if (strcasecmp(Arg, "yes") == 0)
1179 if (strcasecmp(Arg, "true") == 0)
1188 * Handle setting of "MaxNickLength".
1190 * @param Line Line number in configuration file.
1191 * @raram Arg Input string.
1192 * @returns New configured maximum nickname length.
1195 Handle_MaxNickLength(int Line, const char *Arg)
1199 new = (unsigned) atoi(Arg) + 1;
1200 if (new > CLIENT_NICK_LEN) {
1201 Config_Error(LOG_WARNING,
1202 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
1203 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
1204 return CLIENT_NICK_LEN;
1207 Config_Error(LOG_WARNING,
1208 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
1209 NGIRCd_ConfFile, Line);
1216 * Output a warning messages if IDENT is configured but not compiled in.
1219 WarnIdent(int UNUSED Line)
1223 /* user has enabled ident lookups explicitly, but ... */
1224 Config_Error(LOG_WARNING,
1225 "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
1226 NGIRCd_ConfFile, Line);
1232 * Output a warning messages if IPv6 is configured but not compiled in.
1235 WarnIPv6(int UNUSED Line)
1238 if (Conf_ConnectIPv6) {
1239 /* user has enabled IPv6 explicitly, but ... */
1240 Config_Error(LOG_WARNING,
1241 "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
1242 NGIRCd_ConfFile, Line);
1248 * Output a warning messages if PAM is configured but not compiled in.
1251 WarnPAM(int UNUSED Line)
1255 Config_Error(LOG_WARNING,
1256 "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
1257 NGIRCd_ConfFile, Line);
1263 * Handle legacy "NoXXX" options in [GLOBAL] section.
1265 * TODO: This function and support for "NoXXX" could be removed starting
1266 * with ngIRCd release 19 (one release after marking it "deprecated").
1268 * @param Var Variable name.
1269 * @param Arg Argument string.
1270 * @returns true if a NoXXX option has been processed; false otherwise.
1273 CheckLegacyNoOption(const char *Var, const char *Arg)
1275 if(strcasecmp(Var, "NoDNS") == 0) {
1276 Conf_DNS = !Check_ArgIsTrue( Arg );
1279 if (strcasecmp(Var, "NoIdent") == 0) {
1280 Conf_Ident = !Check_ArgIsTrue(Arg);
1283 if(strcasecmp(Var, "NoPAM") == 0) {
1284 Conf_PAM = !Check_ArgIsTrue(Arg);
1291 * Handle deprecated legacy options in [GLOBAL] section.
1293 * TODO: This function and support for these options in the [Global] section
1294 * could be removed starting with ngIRCd release 19 (one release after
1295 * marking it "deprecated").
1297 * @param Var Variable name.
1298 * @param Arg Argument string.
1299 * @returns true if a legacy option has been processed; false otherwise.
1302 CheckLegacyGlobalOption(const char *File, int Line, char *Var, char *Arg)
1304 if (strcasecmp(Var, "AllowRemoteOper") == 0
1305 || strcasecmp(Var, "ChrootDir") == 0
1306 || strcasecmp(Var, "ConnectIPv4") == 0
1307 || strcasecmp(Var, "ConnectIPv6") == 0
1308 || strcasecmp(Var, "OperCanUseMode") == 0
1309 || strcasecmp(Var, "OperChanPAutoOp") == 0
1310 || strcasecmp(Var, "OperServerMode") == 0
1311 || strcasecmp(Var, "PredefChannelsOnly") == 0
1312 || strcasecmp(Var, "SyslogFacility") == 0
1313 || strcasecmp(Var, "WebircPassword") == 0) {
1314 Handle_OPTIONS(File, Line, Var, Arg);
1317 if (strcasecmp(Var, "ConnectRetry") == 0
1318 || strcasecmp(Var, "IdleTimeout") == 0
1319 || strcasecmp(Var, "MaxConnections") == 0
1320 || strcasecmp(Var, "MaxConnectionsIP") == 0
1321 || strcasecmp(Var, "MaxJoins") == 0
1322 || strcasecmp(Var, "MaxNickLength") == 0
1323 || strcasecmp(Var, "PingTimeout") == 0
1324 || strcasecmp(Var, "PongTimeout") == 0) {
1325 Handle_LIMITS(File, Line, Var, Arg);
1329 if (strcasecmp(Var, "SSLCertFile") == 0
1330 || strcasecmp(Var, "SSLDHFile") == 0
1331 || strcasecmp(Var, "SSLKeyFile") == 0
1332 || strcasecmp(Var, "SSLKeyFilePassword") == 0
1333 || strcasecmp(Var, "SSLPorts") == 0) {
1334 Handle_SSL(File, Line, Var + 3, Arg);
1343 * Strip "no" prefix of a string.
1345 * TODO: This function and support for "NoXXX" should be removed starting
1346 * with ngIRCd release 19! (One release after marking it "deprecated").
1348 * @param str Pointer to input string starting with "no".
1349 * @returns New pointer to string without "no" prefix.
1352 NoNo(const char *str)
1354 assert(strncasecmp("no", str, 2) == 0 && str[2]);
1359 * Invert "boolean" string.
1361 * TODO: This function and support for "NoXXX" should be removed starting
1362 * with ngIRCd release 19! (One release after marking it "deprecated").
1364 * @param arg "Boolean" input string.
1365 * @returns Pointer to inverted "boolean string".
1368 InvertArg(const char *arg)
1370 return yesno_to_str(!Check_ArgIsTrue(arg));
1374 * Handle variable in [Global] configuration section.
1376 * @param Line Line numer in configuration file.
1377 * @param Var Variable name.
1378 * @param Arg Variable argument.
1381 Handle_GLOBAL(const char *File, int Line, char *Var, char *Arg )
1386 const char *section;
1388 assert(File != NULL);
1390 assert(Var != NULL);
1391 assert(Arg != NULL);
1393 if (strcasecmp(Var, "Name") == 0) {
1394 len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
1395 if (len >= sizeof(Conf_ServerName))
1396 Config_Error_TooLong(File, Line, Var);
1399 if (strcasecmp(Var, "AdminInfo1") == 0) {
1400 len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
1401 if (len >= sizeof(Conf_ServerAdmin1))
1402 Config_Error_TooLong(File, Line, Var);
1405 if (strcasecmp(Var, "AdminInfo2") == 0) {
1406 len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
1407 if (len >= sizeof(Conf_ServerAdmin2))
1408 Config_Error_TooLong(File, Line, Var);
1411 if (strcasecmp(Var, "AdminEMail") == 0) {
1412 len = strlcpy(Conf_ServerAdminMail, Arg,
1413 sizeof(Conf_ServerAdminMail));
1414 if (len >= sizeof(Conf_ServerAdminMail))
1415 Config_Error_TooLong(File, Line, Var);
1418 if (strcasecmp(Var, "Info") == 0) {
1419 len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
1420 if (len >= sizeof(Conf_ServerInfo))
1421 Config_Error_TooLong(File, Line, Var);
1424 if (strcasecmp(Var, "HelpFile") == 0) {
1425 len = strlcpy(Conf_HelpFile, Arg, sizeof(Conf_HelpFile));
1426 if (len >= sizeof(Conf_HelpFile))
1427 Config_Error_TooLong(File, Line, Var);
1430 if (strcasecmp(Var, "Listen") == 0) {
1431 if (Conf_ListenAddress) {
1432 Config_Error(LOG_ERR,
1433 "Multiple Listen= options, ignoring: %s",
1437 Conf_ListenAddress = strdup_warn(Arg);
1438 /* If allocation fails, we're in trouble: we cannot ignore the
1439 * error -- otherwise ngircd would listen on all interfaces. */
1440 if (!Conf_ListenAddress) {
1441 Config_Error(LOG_ALERT,
1442 "%s exiting due to fatal errors!",
1448 if (strcasecmp(Var, "MotdFile") == 0) {
1449 len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
1450 if (len >= sizeof(Conf_MotdFile))
1451 Config_Error_TooLong(File, Line, Var);
1454 if (strcasecmp(Var, "MotdPhrase") == 0) {
1458 if (len >= LINE_LEN) {
1459 Config_Error_TooLong(File, Line, Var);
1462 if (!array_copyb(&Conf_Motd, Arg, len + 1))
1463 Config_Error(LOG_WARNING,
1464 "%s, line %d: Could not append MotdPhrase: %s",
1465 NGIRCd_ConfFile, Line, strerror(errno));
1466 Using_MotdFile = false;
1469 if(strcasecmp(Var, "Password") == 0) {
1470 len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
1471 if (len >= sizeof(Conf_ServerPwd))
1472 Config_Error_TooLong(File, Line, Var);
1475 if (strcasecmp(Var, "PidFile") == 0) {
1476 len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
1477 if (len >= sizeof(Conf_PidFile))
1478 Config_Error_TooLong(File, Line, Var);
1481 if (strcasecmp(Var, "Ports") == 0) {
1482 ports_parse(&Conf_ListenPorts, Line, Arg);
1485 if (strcasecmp(Var, "ServerGID") == 0) {
1486 grp = getgrnam(Arg);
1488 Conf_GID = grp->gr_gid;
1490 Conf_GID = (unsigned int)atoi(Arg);
1491 if (!Conf_GID && strcmp(Arg, "0"))
1492 Config_Error(LOG_WARNING,
1493 "%s, line %d: Value of \"%s\" is not a valid group name or ID!",
1494 NGIRCd_ConfFile, Line, Var);
1498 if (strcasecmp(Var, "ServerUID") == 0) {
1499 pwd = getpwnam(Arg);
1501 Conf_UID = pwd->pw_uid;
1503 Conf_UID = (unsigned int)atoi(Arg);
1504 if (!Conf_UID && strcmp(Arg, "0"))
1505 Config_Error(LOG_WARNING,
1506 "%s, line %d: Value of \"%s\" is not a valid user name or ID!",
1507 NGIRCd_ConfFile, Line, Var);
1512 if (CheckLegacyNoOption(Var, Arg)) {
1513 /* TODO: This function and support for "NoXXX" could be
1514 * be removed starting with ngIRCd release 19 (one release
1515 * after marking it "deprecated"). */
1516 Config_Error(LOG_WARNING,
1517 "%s, line %d (section \"Global\"): \"No\"-Prefix is deprecated, use \"%s = %s\" in [Options] section!",
1518 NGIRCd_ConfFile, Line, NoNo(Var), InvertArg(Arg));
1519 if (strcasecmp(Var, "NoIdent") == 0)
1521 else if (strcasecmp(Var, "NoPam") == 0)
1525 if ((section = CheckLegacyGlobalOption(File, Line, Var, Arg))) {
1526 /** TODO: This function and support for these options in the
1527 * [Global] section could be removed starting with ngIRCd
1528 * release 19 (one release after marking it "deprecated"). */
1529 if (strncasecmp(Var, "SSL", 3) == 0) {
1530 Config_Error(LOG_WARNING,
1531 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s and rename to \"%s\"!",
1532 NGIRCd_ConfFile, Line, Var, section,
1535 Config_Error(LOG_WARNING,
1536 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s!",
1537 NGIRCd_ConfFile, Line, Var, section);
1542 Config_Error_Section(File, Line, Var, "Global");
1546 * Handle variable in [Limits] configuration section.
1548 * @param Line Line numer in configuration file.
1549 * @param Var Variable name.
1550 * @param Arg Variable argument.
1553 Handle_LIMITS(const char *File, int Line, char *Var, char *Arg)
1555 assert(File != NULL);
1557 assert(Var != NULL);
1558 assert(Arg != NULL);
1560 if (strcasecmp(Var, "ConnectRetry") == 0) {
1561 Conf_ConnectRetry = atoi(Arg);
1562 if (Conf_ConnectRetry < 5) {
1563 Config_Error(LOG_WARNING,
1564 "%s, line %d: Value of \"ConnectRetry\" too low!",
1565 NGIRCd_ConfFile, Line);
1566 Conf_ConnectRetry = 5;
1570 if (strcasecmp(Var, "IdleTimeout") == 0) {
1571 Conf_IdleTimeout = atoi(Arg);
1572 if (!Conf_IdleTimeout && strcmp(Arg, "0"))
1573 Config_Error_NaN(File, Line, Var);
1576 if (strcasecmp(Var, "MaxConnections") == 0) {
1577 Conf_MaxConnections = atoi(Arg);
1578 if (!Conf_MaxConnections && strcmp(Arg, "0"))
1579 Config_Error_NaN(File, Line, Var);
1582 if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
1583 Conf_MaxConnectionsIP = atoi(Arg);
1584 if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
1585 Config_Error_NaN(File, Line, Var);
1588 if (strcasecmp(Var, "MaxJoins") == 0) {
1589 Conf_MaxJoins = atoi(Arg);
1590 if (!Conf_MaxJoins && strcmp(Arg, "0"))
1591 Config_Error_NaN(File, Line, Var);
1594 if (strcasecmp(Var, "MaxNickLength") == 0) {
1595 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1598 if (strcasecmp(Var, "MaxListSize") == 0) {
1599 Conf_MaxListSize = atoi(Arg);
1600 if (!Conf_MaxListSize && strcmp(Arg, "0"))
1601 Config_Error_NaN(File, Line, Var);
1604 if (strcasecmp(Var, "PingTimeout") == 0) {
1605 Conf_PingTimeout = atoi(Arg);
1606 if (Conf_PingTimeout < 5) {
1607 Config_Error(LOG_WARNING,
1608 "%s, line %d: Value of \"PingTimeout\" too low!",
1609 NGIRCd_ConfFile, Line);
1610 Conf_PingTimeout = 5;
1614 if (strcasecmp(Var, "PongTimeout") == 0) {
1615 Conf_PongTimeout = atoi(Arg);
1616 if (Conf_PongTimeout < 5) {
1617 Config_Error(LOG_WARNING,
1618 "%s, line %d: Value of \"PongTimeout\" too low!",
1619 NGIRCd_ConfFile, Line);
1620 Conf_PongTimeout = 5;
1625 Config_Error_Section(File, Line, Var, "Limits");
1629 * Handle variable in [Options] configuration section.
1631 * @param Line Line numer in configuration file.
1632 * @param Var Variable name.
1633 * @param Arg Variable argument.
1636 Handle_OPTIONS(const char *File, int Line, char *Var, char *Arg)
1641 assert(File != NULL);
1643 assert(Var != NULL);
1644 assert(Arg != NULL);
1646 if (strcasecmp(Var, "AllowedChannelTypes") == 0) {
1648 Conf_AllowedChannelTypes[0] = '\0';
1650 if (strchr(Conf_AllowedChannelTypes, *p)) {
1651 /* Prefix is already included; ignore it */
1656 if (strchr(CHANTYPES, *p)) {
1657 len = strlen(Conf_AllowedChannelTypes) + 1;
1658 assert(len < sizeof(Conf_AllowedChannelTypes));
1659 Conf_AllowedChannelTypes[len - 1] = *p;
1660 Conf_AllowedChannelTypes[len] = '\0';
1662 Config_Error(LOG_WARNING,
1663 "%s, line %d: Unknown channel prefix \"%c\" in \"AllowedChannelTypes\"!",
1670 if (strcasecmp(Var, "AllowRemoteOper") == 0) {
1671 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1674 if (strcasecmp(Var, "ChrootDir") == 0) {
1675 len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
1676 if (len >= sizeof(Conf_Chroot))
1677 Config_Error_TooLong(File, Line, Var);
1680 if (strcasecmp(Var, "CloakHost") == 0) {
1681 len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
1682 if (len >= sizeof(Conf_CloakHost))
1683 Config_Error_TooLong(File, Line, Var);
1686 if (strcasecmp(Var, "CloakHostModeX") == 0) {
1687 len = strlcpy(Conf_CloakHostModeX, Arg, sizeof(Conf_CloakHostModeX));
1688 if (len >= sizeof(Conf_CloakHostModeX))
1689 Config_Error_TooLong(File, Line, Var);
1692 if (strcasecmp(Var, "CloakHostSalt") == 0) {
1693 len = strlcpy(Conf_CloakHostSalt, Arg, sizeof(Conf_CloakHostSalt));
1694 if (len >= sizeof(Conf_CloakHostSalt))
1695 Config_Error_TooLong(File, Line, Var);
1698 if (strcasecmp(Var, "CloakUserToNick") == 0) {
1699 Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
1702 if (strcasecmp(Var, "ConnectIPv6") == 0) {
1703 Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
1707 if (strcasecmp(Var, "ConnectIPv4") == 0) {
1708 Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
1711 if (strcasecmp(Var, "DefaultUserModes") == 0) {
1713 Conf_DefaultUserModes[0] = '\0';
1715 if (strchr(Conf_DefaultUserModes, *p)) {
1716 /* Mode is already included; ignore it */
1721 if (strchr(USERMODES, *p)) {
1722 len = strlen(Conf_DefaultUserModes) + 1;
1723 assert(len < sizeof(Conf_DefaultUserModes));
1724 Conf_DefaultUserModes[len - 1] = *p;
1725 Conf_DefaultUserModes[len] = '\0';
1727 Config_Error(LOG_WARNING,
1728 "%s, line %d: Unknown user mode \"%c\" in \"DefaultUserModes\"!",
1735 if (strcasecmp(Var, "DNS") == 0) {
1736 Conf_DNS = Check_ArgIsTrue(Arg);
1739 if (strcasecmp(Var, "Ident") == 0) {
1740 Conf_Ident = Check_ArgIsTrue(Arg);
1744 if (strcasecmp(Var, "IncludeDir") == 0) {
1745 if (Conf_IncludeDir[0]) {
1746 Config_Error(LOG_ERR,
1747 "%s, line %d: Can't overwrite value of \"IncludeDir\" variable!",
1751 len = strlcpy(Conf_IncludeDir, Arg, sizeof(Conf_IncludeDir));
1752 if (len >= sizeof(Conf_IncludeDir))
1753 Config_Error_TooLong(File, Line, Var);
1756 if (strcasecmp(Var, "MorePrivacy") == 0) {
1757 Conf_MorePrivacy = Check_ArgIsTrue(Arg);
1760 if (strcasecmp(Var, "NoticeAuth") == 0) {
1761 Conf_NoticeAuth = Check_ArgIsTrue(Arg);
1764 if (strcasecmp(Var, "OperCanUseMode") == 0) {
1765 Conf_OperCanMode = Check_ArgIsTrue(Arg);
1768 if (strcasecmp(Var, "OperChanPAutoOp") == 0) {
1769 Conf_OperChanPAutoOp = Check_ArgIsTrue(Arg);
1772 if (strcasecmp(Var, "OperServerMode") == 0) {
1773 Conf_OperServerMode = Check_ArgIsTrue(Arg);
1776 if (strcasecmp(Var, "PAM") == 0) {
1777 Conf_PAM = Check_ArgIsTrue(Arg);
1781 if (strcasecmp(Var, "PAMIsOptional") == 0 ) {
1782 Conf_PAMIsOptional = Check_ArgIsTrue(Arg);
1785 if (strcasecmp(Var, "PredefChannelsOnly") == 0) {
1787 * TODO: This section and support for "PredefChannelsOnly"
1788 * could be removed starting with ngIRCd release 22 (one
1789 * release after marking it "deprecated") ...
1791 Config_Error(LOG_WARNING,
1792 "%s, line %d (section \"Options\"): \"%s\" is deprecated, please use \"AllowedChannelTypes\"!",
1794 if (Check_ArgIsTrue(Arg))
1795 Conf_AllowedChannelTypes[0] = '\0';
1797 strlcpy(Conf_AllowedChannelTypes, CHANTYPES,
1798 sizeof(Conf_AllowedChannelTypes));
1802 if (strcasecmp(Var, "RequireAuthPing") == 0) {
1803 Conf_AuthPing = Check_ArgIsTrue(Arg);
1807 if (strcasecmp(Var, "ScrubCTCP") == 0) {
1808 Conf_ScrubCTCP = Check_ArgIsTrue(Arg);
1812 if (strcasecmp(Var, "SyslogFacility") == 0) {
1813 Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
1814 Conf_SyslogFacility);
1818 if (strcasecmp(Var, "WebircPassword") == 0) {
1819 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
1820 if (len >= sizeof(Conf_WebircPwd))
1821 Config_Error_TooLong(File, Line, Var);
1825 Config_Error_Section(File, Line, Var, "Options");
1831 * Handle variable in [SSL] configuration section.
1833 * @param Line Line numer in configuration file.
1834 * @param Var Variable name.
1835 * @param Arg Variable argument.
1838 Handle_SSL(const char *File, int Line, char *Var, char *Arg)
1840 assert(File != NULL);
1842 assert(Var != NULL);
1843 assert(Arg != NULL);
1845 if (strcasecmp(Var, "CertFile") == 0) {
1846 assert(Conf_SSLOptions.CertFile == NULL);
1847 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1850 if (strcasecmp(Var, "DHFile") == 0) {
1851 assert(Conf_SSLOptions.DHFile == NULL);
1852 Conf_SSLOptions.DHFile = strdup_warn(Arg);
1855 if (strcasecmp(Var, "KeyFile") == 0) {
1856 assert(Conf_SSLOptions.KeyFile == NULL);
1857 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1860 if (strcasecmp(Var, "KeyFilePassword") == 0) {
1861 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1862 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1863 Config_Error(LOG_ERR,
1864 "%s, line %d (section \"SSL\"): Could not copy %s: %s!",
1865 File, Line, Var, strerror(errno));
1868 if (strcasecmp(Var, "Ports") == 0) {
1869 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1873 Config_Error_Section(File, Line, Var, "SSL");
1879 * Handle variable in [Operator] configuration section.
1881 * @param Line Line numer in configuration file.
1882 * @param Var Variable name.
1883 * @param Arg Variable argument.
1886 Handle_OPERATOR(const char *File, int Line, char *Var, char *Arg )
1889 struct Conf_Oper *op;
1891 assert( File != NULL );
1893 assert( Var != NULL );
1894 assert( Arg != NULL );
1896 op = array_get(&Conf_Opers, sizeof(*op),
1897 array_length(&Conf_Opers, sizeof(*op)) - 1);
1901 if (strcasecmp(Var, "Name") == 0) {
1902 /* Name of IRC operator */
1903 len = strlcpy(op->name, Arg, sizeof(op->name));
1904 if (len >= sizeof(op->name))
1905 Config_Error_TooLong(File, Line, Var);
1908 if (strcasecmp(Var, "Password") == 0) {
1909 /* Password of IRC operator */
1910 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1911 if (len >= sizeof(op->pwd))
1912 Config_Error_TooLong(File, Line, Var);
1915 if (strcasecmp(Var, "Mask") == 0) {
1917 return; /* Hostname already configured */
1918 op->mask = strdup_warn( Arg );
1922 Config_Error_Section(File, Line, Var, "Operator");
1926 * Handle variable in [Server] configuration section.
1928 * @param Line Line numer in configuration file.
1929 * @param Var Variable name.
1930 * @param Arg Variable argument.
1933 Handle_SERVER(const char *File, int Line, char *Var, char *Arg )
1938 assert( File != NULL );
1940 assert( Var != NULL );
1941 assert( Arg != NULL );
1943 /* Ignore server block if no space is left in server configuration structure */
1944 if( New_Server_Idx <= NONE ) return;
1946 if( strcasecmp( Var, "Host" ) == 0 ) {
1947 /* Hostname of the server */
1948 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1949 if (len >= sizeof( New_Server.host ))
1950 Config_Error_TooLong(File, Line, Var);
1953 if( strcasecmp( Var, "Name" ) == 0 ) {
1954 /* Name of the server ("Nick"/"ID") */
1955 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1956 if (len >= sizeof( New_Server.name ))
1957 Config_Error_TooLong(File, Line, Var);
1960 if (strcasecmp(Var, "Bind") == 0) {
1961 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1964 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1965 NGIRCd_ConfFile, Line, Arg);
1968 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1969 /* Password of this server which is sent to the peer */
1971 Config_Error(LOG_ERR,
1972 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1973 NGIRCd_ConfFile, Line);
1975 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1976 if (len >= sizeof( New_Server.pwd_in ))
1977 Config_Error_TooLong(File, Line, Var);
1980 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1981 /* Passwort of the peer which must be received */
1982 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1983 if (len >= sizeof( New_Server.pwd_out ))
1984 Config_Error_TooLong(File, Line, Var);
1987 if( strcasecmp( Var, "Port" ) == 0 ) {
1988 /* Port to which this server should connect */
1990 if (port >= 0 && port < 0xFFFF)
1991 New_Server.port = (UINT16)port;
1993 Config_Error(LOG_ERR,
1994 "%s, line %d (section \"Server\"): Illegal port number %ld!",
1995 NGIRCd_ConfFile, Line, port );
1999 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
2000 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
2004 if( strcasecmp( Var, "Group" ) == 0 ) {
2006 New_Server.group = atoi( Arg );
2007 if (!New_Server.group && strcmp(Arg, "0"))
2008 Config_Error_NaN(File, Line, Var);
2011 if( strcasecmp( Var, "Passive" ) == 0 ) {
2012 if (Check_ArgIsTrue(Arg))
2013 New_Server.flags |= CONF_SFLAG_DISABLED;
2016 if (strcasecmp(Var, "ServiceMask") == 0) {
2017 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
2018 sizeof(New_Server.svs_mask));
2019 if (len >= sizeof(New_Server.svs_mask))
2020 Config_Error_TooLong(File, Line, Var);
2024 Config_Error_Section(File, Line, Var, "Server");
2028 * Copy channel name into channel structure.
2030 * If the channel name is not valid because of a missing prefix ('#', '&'),
2031 * a default prefix of '#' will be added.
2033 * @param new_chan New already allocated channel structure.
2034 * @param name Name of the new channel.
2035 * @returns true on success, false otherwise.
2038 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
2040 size_t size = sizeof(new_chan->name);
2041 char *dest = new_chan->name;
2043 if (!Channel_IsValidName(name)) {
2045 * maybe user forgot to add a '#'.
2046 * This is only here for user convenience.
2052 return size > strlcpy(dest, name, size);
2056 * Handle variable in [Channel] configuration section.
2058 * @param Line Line numer in configuration file.
2059 * @param Var Variable name.
2060 * @param Arg Variable argument.
2063 Handle_CHANNEL(const char *File, int Line, char *Var, char *Arg)
2066 struct Conf_Channel *chan;
2068 assert( File != NULL );
2070 assert( Var != NULL );
2071 assert( Arg != NULL );
2073 chan = array_get(&Conf_Channels, sizeof(*chan),
2074 array_length(&Conf_Channels, sizeof(*chan)) - 1);
2078 if (strcasecmp(Var, "Name") == 0) {
2079 if (!Handle_Channelname(chan, Arg))
2080 Config_Error_TooLong(File, Line, Var);
2083 if (strcasecmp(Var, "Modes") == 0) {
2085 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
2086 if (len >= sizeof(chan->modes))
2087 Config_Error_TooLong(File, Line, Var);
2090 if( strcasecmp( Var, "Topic" ) == 0 ) {
2092 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
2093 if (len >= sizeof(chan->topic))
2094 Config_Error_TooLong(File, Line, Var);
2097 if( strcasecmp( Var, "Key" ) == 0 ) {
2098 /* Initial Channel Key (mode k) */
2099 len = strlcpy(chan->key, Arg, sizeof(chan->key));
2100 if (len >= sizeof(chan->key))
2101 Config_Error_TooLong(File, Line, Var);
2104 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
2105 /* maximum user limit, mode l */
2106 chan->maxusers = (unsigned long) atol(Arg);
2107 if (!chan->maxusers && strcmp(Arg, "0"))
2108 Config_Error_NaN(File, Line, Var);
2111 if (strcasecmp(Var, "KeyFile") == 0) {
2113 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
2114 if (len >= sizeof(chan->keyfile))
2115 Config_Error_TooLong(File, Line, Var);
2119 Config_Error_Section(File, Line, Var, "Channel");
2123 * Validate server configuration.
2125 * Please note that this function uses exit(1) on fatal errors and therefore
2126 * can result in ngIRCd terminating!
2128 * @param Configtest true if the daemon has been called with "--configtest".
2129 * @param Rehash true if re-reading configuration on runtime.
2130 * @returns true if configuration is valid.
2133 Validate_Config(bool Configtest, bool Rehash)
2135 /* Validate configuration settings. */
2138 int i, servers, servers_once;
2140 bool config_valid = true;
2143 /* Emit a warning when the config file is not a full path name */
2144 if (NGIRCd_ConfFile[0] && NGIRCd_ConfFile[0] != '/') {
2145 Config_Error(LOG_WARNING,
2146 "Not specifying a full path name to \"%s\" can cause problems when rehashing the server!",
2150 /* Validate configured server name, see RFC 2812 section 2.3.1 */
2151 ptr = Conf_ServerName;
2153 if (*ptr >= 'a' && *ptr <= 'z') continue;
2154 if (*ptr >= 'A' && *ptr <= 'Z') continue;
2155 if (*ptr >= '0' && *ptr <= '9') continue;
2156 if (ptr > Conf_ServerName) {
2157 if (*ptr == '.' || *ptr == '-')
2160 Conf_ServerName[0] = '\0';
2164 if (!Conf_ServerName[0]) {
2165 /* No server name configured! */
2166 config_valid = false;
2167 Config_Error(LOG_ALERT,
2168 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
2170 if (!Configtest && !Rehash) {
2171 Config_Error(LOG_ALERT,
2172 "%s exiting due to fatal errors!",
2178 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
2179 /* No dot in server name! */
2180 config_valid = false;
2181 Config_Error(LOG_ALERT,
2182 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
2185 Config_Error(LOG_ALERT,
2186 "%s exiting due to fatal errors!",
2193 if (!Conf_ServerAdminMail[0]) {
2194 /* No administrative contact configured! */
2195 config_valid = false;
2196 Config_Error(LOG_ALERT,
2197 "No administrator email address configured in \"%s\" ('AdminEMail')!",
2200 Config_Error(LOG_ALERT,
2201 "%s exiting due to fatal errors!",
2208 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
2209 && !Conf_ServerAdminMail[0]) {
2210 /* No administrative information configured! */
2211 Config_Error(LOG_WARNING,
2212 "No administrative information configured but required by RFC!");
2216 if (Conf_ServerPwd[0])
2217 Config_Error(LOG_ERR,
2218 "This server uses PAM, \"Password\" in [Global] section will be ignored!");
2222 servers = servers_once = 0;
2223 for (i = 0; i < MAX_SERVERS; i++) {
2224 if (Conf_Server[i].name[0]) {
2226 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
2231 "Configuration: Operators=%ld, Servers=%d[%d], Channels=%ld",
2232 array_length(&Conf_Opers, sizeof(struct Conf_Oper)),
2233 servers, servers_once,
2234 array_length(&Conf_Channels, sizeof(struct Conf_Channel)));
2237 return config_valid;
2241 * Output "line too long" warning.
2243 * @param Line Line number in configuration file.
2244 * @param Item Affected variable name.
2247 Config_Error_TooLong(const char *File, const int Line, const char *Item)
2249 Config_Error(LOG_WARNING, "%s, line %d: Value of \"%s\" too long!",
2254 * Output "unknown variable" warning.
2256 * @param Line Line number in configuration file.
2257 * @param Item Affected variable name.
2258 * @param Section Section name.
2261 Config_Error_Section(const char *File, const int Line, const char *Item,
2262 const char *Section)
2264 Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
2265 File, Line, Section, Item);
2269 * Output "not a number" warning.
2271 * @param Line Line number in configuration file.
2272 * @param Item Affected variable name.
2275 Config_Error_NaN(const char *File, const int Line, const char *Item )
2277 Config_Error(LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
2282 * Output configuration error to console and/or logfile.
2284 * On runtime, the normal log functions of the daemon are used. But when
2285 * testing the configuration ("--configtest"), all messages go directly
2288 * @param Level Severity level of the message.
2289 * @param Format Format string; see printf() function.
2292 static void Config_Error( const int Level, const char *Format, ... )
2294 static void Config_Error( Level, Format, va_alist )
2300 char msg[MAX_LOG_MSG_LEN];
2303 assert( Format != NULL );
2306 va_start( ap, Format );
2310 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
2314 if (Level <= LOG_WARNING)
2315 printf(" - %s\n", msg);
2319 Log(Level, "%s", msg);
2325 * Dump internal state of the "configuration module".
2328 Conf_DebugDump(void)
2332 Log(LOG_DEBUG, "Configured servers:");
2333 for (i = 0; i < MAX_SERVERS; i++) {
2334 if (! Conf_Server[i].name[0])
2337 " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
2338 Conf_Server[i].name, Conf_Server[i].host,
2339 Conf_Server[i].port, Conf_Server[i].lasttry,
2340 Conf_Server[i].group, Conf_Server[i].flags,
2341 Conf_Server[i].conn_id);
2348 * Initialize server configuration structure to default values.
2350 * @param Server Pointer to server structure to initialize.
2353 Init_Server_Struct( CONF_SERVER *Server )
2355 assert( Server != NULL );
2357 memset( Server, 0, sizeof (CONF_SERVER) );
2359 Server->group = NONE;
2360 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
2362 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
2364 Proc_InitStruct(&Server->res_stat);
2365 Server->conn_id = NONE;
2366 memset(&Server->bind_addr, 0, sizeof(Server->bind_addr));