2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2019 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)
34 #include <sys/types.h>
46 static bool Use_Log = true, Using_MotdFile = true;
47 static CONF_SERVER New_Server;
48 static int New_Server_Idx;
50 static char Conf_MotdFile[FNAME_LEN];
51 static char Conf_HelpFile[FNAME_LEN];
52 static char Conf_IncludeDir[FNAME_LEN];
54 static void Set_Defaults PARAMS(( bool InitServers ));
55 static bool Read_Config PARAMS(( bool TestOnly, bool IsStarting ));
56 static void Read_Config_File PARAMS(( const char *File, FILE *fd ));
57 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
59 static void Handle_GLOBAL PARAMS((const char *File, int Line,
60 char *Var, char *Arg ));
61 static void Handle_LIMITS PARAMS((const char *File, int Line,
62 char *Var, char *Arg ));
63 static void Handle_OPTIONS PARAMS((const char *File, int Line,
64 char *Var, char *Arg ));
65 static void Handle_OPERATOR PARAMS((const char *File, int Line,
66 char *Var, char *Arg ));
67 static void Handle_SERVER PARAMS((const char *File, int Line,
68 char *Var, char *Arg ));
69 static void Handle_CHANNEL PARAMS((const char *File, int Line,
70 char *Var, char *Arg ));
72 static void Config_Error PARAMS((const int Level, const char *Format, ...));
74 static void Config_Error_NaN PARAMS((const char *File, const int LINE,
76 static void Config_Error_Section PARAMS((const char *File, const int Line,
77 const char *Item, const char *Section));
78 static void Config_Error_TooLong PARAMS((const char *File, const int LINE,
81 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
85 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
87 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
91 #define DEFAULT_CIPHERS "HIGH:!aNULL:@STRENGTH:!SSLv3"
94 #define DEFAULT_CIPHERS "SECURE128:-VERS-SSL3.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);
121 free(Conf_SSLOptions.CipherList);
122 Conf_SSLOptions.CipherList = NULL;
126 * Check if the current configuration uses/requires SSL.
128 * @returns true if SSL is used and should be initialized.
135 /* SSL listen ports configured? */
136 if (array_bytes(&Conf_SSLOptions.ListenPorts))
139 for (i = 0; i < MAX_SERVERS; i++) {
140 if (Conf_Server[i].port > 0
141 && Conf_Server[i].SSLConnect)
148 * Make sure that a configured file is readable.
150 * Currently, this function is only used for SSL-related options ...
152 * @param Var Configuration variable
153 * @param Filename Configured filename
156 CheckFileReadable(const char *Var, const char *Filename)
163 fp = fopen(Filename, "r");
167 Config_Error(LOG_ERR, "Can't read \"%s\" (\"%s\"): %s",
168 Filename, Var, strerror(errno));
175 * Duplicate string and warn on errors.
177 * @returns Pointer to string on success, NULL otherwise.
180 strdup_warn(const char *str)
182 char *ptr = strdup(str);
184 Config_Error(LOG_ERR,
185 "Could not allocate memory for string: %s", str);
190 * Output a comma separated list of ports (integer values).
197 len = array_length(a, sizeof(UINT16));
199 ports = (UINT16*) array_start(a);
200 printf("%u", (unsigned int) *ports);
203 printf(", %u", (unsigned int) *ports);
210 * Parse a comma separated string into an array of port numbers (integers).
213 ports_parse(array *a, const char *File, int Line, char *Arg)
221 ptr = strtok( Arg, "," );
225 if (port > 0 && port < 0xFFFF) {
226 port16 = (UINT16) port;
227 if (!array_catb(a, (char*)&port16, sizeof port16))
228 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
229 File, Line, port, strerror(errno));
231 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
235 ptr = strtok( NULL, "," );
240 * Initialize configuration module.
245 Read_Config(false, true);
246 Validate_Config(false, false);
250 * "Rehash" (reload) server configuration.
252 * @returns true if configuration has been re-read, false on errors.
257 if (!Read_Config(false, false))
259 Validate_Config(false, true);
261 /* Update CLIENT structure of local server */
262 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
267 * Output a boolean value as "yes/no" string.
270 yesno_to_str(int boolean_value)
278 * Free all IRC operator configuration structures.
283 struct Conf_Oper *op;
286 len = array_length(&Conf_Opers, sizeof(*op));
287 op = array_start(&Conf_Opers);
292 array_free(&Conf_Opers);
296 * Output all IRC operator configuration structures.
301 struct Conf_Oper *op;
304 count = array_length(&Conf_Opers, sizeof(*op));
305 op = array_start(&Conf_Opers);
306 for (i = 0; i < count; i++, op++) {
311 printf(" Name = %s\n", op->name);
312 printf(" Password = %s\n", op->pwd);
313 printf(" Mask = %s\n\n", op->mask ? op->mask : "");
318 * Read configuration, validate and output it.
320 * This function waits for a keypress of the user when stdin/stdout are valid
321 * tty's ("you can read our nice message and we can read in your keypress").
323 * @return 0 on success, 1 on failure(s); therefore the result code can
324 * directly be used by exit() when running "ngircd --configtest".
333 size_t predef_channel_count;
334 struct Conf_Channel *predef_chan;
338 if (!Read_Config(true, true))
341 config_valid = Validate_Config(true, false);
344 if(isatty(fileno(stdin)) && isatty(fileno(stdout))) {
345 puts("OK, press enter to see a dump of your server configuration ...");
348 puts("Ok, dump of your server configuration follows:\n");
351 printf(" Name = %s\n", Conf_ServerName);
352 printf(" AdminInfo1 = %s\n", Conf_ServerAdmin1);
353 printf(" AdminInfo2 = %s\n", Conf_ServerAdmin2);
354 printf(" AdminEMail = %s\n", Conf_ServerAdminMail);
355 printf(" HelpFile = %s\n", Conf_HelpFile);
356 printf(" Info = %s\n", Conf_ServerInfo);
357 printf(" Listen = %s\n", Conf_ListenAddress);
358 if (Using_MotdFile) {
359 printf(" MotdFile = %s\n", Conf_MotdFile);
360 printf(" MotdPhrase =\n");
362 printf(" MotdFile = \n");
363 printf(" MotdPhrase = %s\n", array_bytes(&Conf_Motd)
364 ? (const char*) array_start(&Conf_Motd) : "");
366 printf(" Network = %s\n", Conf_Network);
368 printf(" Password = %s\n", Conf_ServerPwd);
369 printf(" PidFile = %s\n", Conf_PidFile);
371 ports_puts(&Conf_ListenPorts);
372 grp = getgrgid(Conf_GID);
374 printf(" ServerGID = %s\n", grp->gr_name);
376 printf(" ServerGID = %ld\n", (long)Conf_GID);
377 pwd = getpwuid(Conf_UID);
379 printf(" ServerUID = %s\n", pwd->pw_name);
381 printf(" ServerUID = %ld\n", (long)Conf_UID);
385 printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
386 printf(" IdleTimeout = %d\n", Conf_IdleTimeout);
387 printf(" MaxConnections = %d\n", Conf_MaxConnections);
388 printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
389 printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
390 printf(" MaxNickLength = %u\n", Conf_MaxNickLength - 1);
391 printf(" MaxPenaltyTime = %ld\n", Conf_MaxPenaltyTime);
392 printf(" MaxListSize = %d\n", Conf_MaxListSize);
393 printf(" PingTimeout = %d\n", Conf_PingTimeout);
394 printf(" PongTimeout = %d\n", Conf_PongTimeout);
398 printf(" AllowedChannelTypes = %s\n", Conf_AllowedChannelTypes);
399 printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
400 printf(" ChrootDir = %s\n", Conf_Chroot);
401 printf(" CloakHost = %s\n", Conf_CloakHost);
402 printf(" CloakHostModeX = %s\n", Conf_CloakHostModeX);
403 printf(" CloakHostSalt = %s\n", Conf_CloakHostSalt);
404 printf(" CloakUserToNick = %s\n", yesno_to_str(Conf_CloakUserToNick));
406 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
407 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
409 printf(" DefaultUserModes = %s\n", Conf_DefaultUserModes);
410 printf(" DNS = %s\n", yesno_to_str(Conf_DNS));
412 printf(" Ident = %s\n", yesno_to_str(Conf_Ident));
414 printf(" IncludeDir = %s\n", Conf_IncludeDir);
415 printf(" MorePrivacy = %s\n", yesno_to_str(Conf_MorePrivacy));
416 printf(" NoticeBeforeRegistration = %s\n", yesno_to_str(Conf_NoticeBeforeRegistration));
417 printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
418 printf(" OperChanPAutoOp = %s\n", yesno_to_str(Conf_OperChanPAutoOp));
419 printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
421 printf(" PAM = %s\n", yesno_to_str(Conf_PAM));
422 printf(" PAMIsOptional = %s\n", yesno_to_str(Conf_PAMIsOptional));
423 printf(" PAMServiceName = %s\n", Conf_PAMServiceName);
426 printf(" RequireAuthPing = %s\n", yesno_to_str(Conf_AuthPing));
428 printf(" ScrubCTCP = %s\n", yesno_to_str(Conf_ScrubCTCP));
430 printf(" SyslogFacility = %s\n",
431 ngt_SyslogFacilityName(Conf_SyslogFacility));
433 printf(" WebircPassword = %s\n", Conf_WebircPwd);
438 printf(" CertFile = %s\n", Conf_SSLOptions.CertFile
439 ? Conf_SSLOptions.CertFile : "");
440 printf(" CipherList = %s\n", Conf_SSLOptions.CipherList ?
441 Conf_SSLOptions.CipherList : DEFAULT_CIPHERS);
442 printf(" DHFile = %s\n", Conf_SSLOptions.DHFile
443 ? Conf_SSLOptions.DHFile : "");
444 printf(" KeyFile = %s\n", Conf_SSLOptions.KeyFile
445 ? Conf_SSLOptions.KeyFile : "");
446 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
447 puts(" KeyFilePassword = <secret>");
449 puts(" KeyFilePassword = ");
450 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
452 ports_puts(&Conf_SSLOptions.ListenPorts);
458 for( i = 0; i < MAX_SERVERS; i++ ) {
459 if( ! Conf_Server[i].name[0] ) continue;
461 /* Valid "Server" section */
463 printf( " Name = %s\n", Conf_Server[i].name );
464 printf( " Host = %s\n", Conf_Server[i].host );
465 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
467 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
469 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
470 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
471 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
472 printf( " Group = %d\n", Conf_Server[i].group );
473 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
476 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
477 predef_chan = array_start(&Conf_Channels);
479 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
480 if (!predef_chan->name[0])
483 /* Valid "Channel" section */
485 printf(" Name = %s\n", predef_chan->name);
486 printf(" Modes = %s\n", predef_chan->modes);
487 printf(" Key = %s\n", predef_chan->key);
488 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
489 printf(" Topic = %s\n", predef_chan->topic);
490 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
493 return (config_valid ? 0 : 1);
497 * Remove connection information from configured server.
499 * If the server is set as "once", delete it from our configuration;
500 * otherwise set the time for the next connection attempt.
502 * Non-server connections will be silently ignored.
505 Conf_UnsetServer( CONN_ID Idx )
510 /* Check all our configured servers */
511 for( i = 0; i < MAX_SERVERS; i++ ) {
512 if( Conf_Server[i].conn_id != Idx ) continue;
514 /* Gotcha! Mark server configuration as "unused": */
515 Conf_Server[i].conn_id = NONE;
517 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
518 /* Delete configuration here */
519 Init_Server_Struct( &Conf_Server[i] );
521 /* Set time for next connect attempt */
523 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
524 /* The connection has been "long", so we don't
525 * require the next attempt to be delayed. */
526 Conf_Server[i].lasttry =
527 t - Conf_ConnectRetry + RECONNECT_DELAY;
529 /* "Short" connection, enforce "ConnectRetry"
530 * but randomize it a little bit: 15 seconds. */
531 Conf_Server[i].lasttry =
532 #ifdef HAVE_ARC4RANDOM
533 t + (arc4random() % 15);
535 t + rand() / (RAND_MAX / 15);
543 * Set connection information for specified configured server.
546 Conf_SetServer( int ConfServer, CONN_ID Idx )
548 assert( ConfServer > NONE );
549 assert( Idx > NONE );
551 if (Conf_Server[ConfServer].conn_id > NONE &&
552 Conf_Server[ConfServer].conn_id != Idx) {
554 "Connection %d: Server configuration of \"%s\" already in use by connection %d!",
555 Idx, Conf_Server[ConfServer].name,
556 Conf_Server[ConfServer].conn_id);
557 Conn_Close(Idx, NULL, "Server configuration already in use", true);
560 Conf_Server[ConfServer].conn_id = Idx;
565 * Get index of server in configuration structure.
568 Conf_GetServer( CONN_ID Idx )
572 assert( Idx > NONE );
574 for( i = 0; i < MAX_SERVERS; i++ ) {
575 if( Conf_Server[i].conn_id == Idx ) return i;
581 * Enable a server by name and adjust its port number.
583 * @returns true if a server has been enabled and now has a valid port
584 * number and host name for outgoing connections.
587 Conf_EnableServer( const char *Name, UINT16 Port )
591 assert( Name != NULL );
592 for( i = 0; i < MAX_SERVERS; i++ ) {
593 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
594 /* Gotcha! Set port and enable server: */
595 Conf_Server[i].port = Port;
596 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
597 return (Conf_Server[i].port && Conf_Server[i].host[0]);
604 * Enable a server by name.
606 * The server is only usable as outgoing server, if it has set a valid port
607 * number for outgoing connections!
608 * If not, you have to use Conf_EnableServer() function to make it available.
610 * @returns true if a server has been enabled; false otherwise.
613 Conf_EnablePassiveServer(const char *Name)
617 assert( Name != NULL );
618 for (i = 0; i < MAX_SERVERS; i++) {
619 if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
620 && (Conf_Server[i].port > 0)) {
621 /* BINGO! Enable server */
622 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
623 Conf_Server[i].lasttry = 0;
631 * Disable a server by name.
632 * An already established connection will be disconnected.
634 * @returns true if a server was found and has been disabled.
637 Conf_DisableServer( const char *Name )
641 assert( Name != NULL );
642 for( i = 0; i < MAX_SERVERS; i++ ) {
643 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
644 /* Gotcha! Disable and disconnect server: */
645 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
646 if( Conf_Server[i].conn_id > NONE )
647 Conn_Close(Conf_Server[i].conn_id, NULL,
648 "Server link terminated on operator request",
657 * Add a new remote server to our configuration.
659 * @param Name Name of the new server.
660 * @param Port Port number to connect to or 0 for incoming connections.
661 * @param Host Host name to connect to.
662 * @param MyPwd Password that will be sent to the peer.
663 * @param PeerPwd Password that must be received from the peer.
664 * @returns true if the new server has been added; false otherwise.
667 Conf_AddServer(const char *Name, UINT16 Port, const char *Host,
668 const char *MyPwd, const char *PeerPwd)
672 assert( Name != NULL );
673 assert( Host != NULL );
674 assert( MyPwd != NULL );
675 assert( PeerPwd != NULL );
677 /* Search unused item in server configuration structure */
678 for( i = 0; i < MAX_SERVERS; i++ ) {
679 /* Is this item used? */
680 if( ! Conf_Server[i].name[0] ) break;
682 if( i >= MAX_SERVERS ) return false;
684 Init_Server_Struct( &Conf_Server[i] );
685 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
686 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
687 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
688 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
689 Conf_Server[i].port = Port;
690 Conf_Server[i].flags = CONF_SFLAG_ONCE;
696 * Check if the given nickname is reserved for services on a particular server.
698 * @param ConfServer The server index to check.
699 * @param Nick The nickname to check.
700 * @returns true if the given nickname belongs to an "IRC service".
703 Conf_NickIsService(int ConfServer, const char *Nick)
705 assert (ConfServer >= 0);
706 assert (ConfServer < MAX_SERVERS);
708 return MatchCaseInsensitiveList(Conf_Server[ConfServer].svs_mask,
713 * Check if the given nickname is blocked for "normal client" use.
715 * @param Nick The nickname to check.
716 * @returns true if the given nickname belongs to an "IRC service".
719 Conf_NickIsBlocked(const char *Nick)
723 for(i = 0; i < MAX_SERVERS; i++) {
724 if (!Conf_Server[i].name[0])
726 if (Conf_NickIsService(i, Nick))
733 * Initialize configuration settings with their default values.
736 Set_Defaults(bool InitServers)
739 char random[RANDOM_SALT_LEN + 1];
742 strcpy(Conf_ServerName, "");
743 strcpy(Conf_ServerAdmin1, "");
744 strcpy(Conf_ServerAdmin2, "");
745 strcpy(Conf_ServerAdminMail, "");
746 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
747 PACKAGE_NAME, PACKAGE_VERSION);
748 strcpy(Conf_Network, "");
749 free(Conf_ListenAddress);
750 Conf_ListenAddress = NULL;
751 array_free(&Conf_ListenPorts);
752 array_free(&Conf_Motd);
753 array_free(&Conf_Helptext);
754 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
755 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
756 strlcpy(Conf_HelpFile, DOCDIR, sizeof(Conf_HelpFile));
757 strlcat(Conf_HelpFile, HELP_FILE, sizeof(Conf_HelpFile));
758 strcpy(Conf_ServerPwd, "");
759 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
760 Conf_UID = Conf_GID = 0;
763 Conf_ConnectRetry = 60;
764 Conf_IdleTimeout = 0;
765 Conf_MaxConnections = 0;
766 Conf_MaxConnectionsIP = 5;
768 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
769 Conf_MaxPenaltyTime = -1;
770 Conf_MaxListSize = 100;
771 Conf_PingTimeout = 120;
772 Conf_PongTimeout = 20;
775 strlcpy(Conf_AllowedChannelTypes, CHANTYPES,
776 sizeof(Conf_AllowedChannelTypes));
777 Conf_AllowRemoteOper = false;
779 Conf_AuthPing = false;
781 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
782 strcpy(Conf_CloakHost, "");
783 strcpy(Conf_CloakHostModeX, "");
784 strlcpy(Conf_CloakHostSalt, ngt_RandomStr(random, RANDOM_SALT_LEN),
785 sizeof(Conf_CloakHostSalt));
786 Conf_CloakUserToNick = false;
787 Conf_ConnectIPv4 = true;
789 Conf_ConnectIPv6 = true;
791 Conf_ConnectIPv6 = false;
793 strcpy(Conf_DefaultUserModes, "");
800 strcpy(Conf_IncludeDir, "");
801 Conf_MorePrivacy = false;
802 Conf_NoticeBeforeRegistration = false;
803 Conf_OperCanMode = false;
804 Conf_OperChanPAutoOp = true;
805 Conf_OperServerMode = false;
811 Conf_PAMIsOptional = false;
812 strcpy(Conf_PAMServiceName, "ngircd");
813 Conf_ScrubCTCP = false;
816 Conf_SyslogFacility = LOG_LOCAL5;
818 Conf_SyslogFacility = 0;
822 /* Initialize server configuration structures */
824 for (i = 0; i < MAX_SERVERS;
825 Init_Server_Struct(&Conf_Server[i++]));
830 * Get number of configured listening ports.
832 * @returns The number of ports (IPv4+IPv6) on which the server should listen.
837 size_t cnt = array_bytes(&Conf_ListenPorts);
839 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
845 * Read contents of a text file into an array.
847 * This function is used to read the MOTD and help text file, for example.
849 * @param Filename Name of the file to read.
850 * @return true, when the file has been read in.
853 Read_TextFile(const char *Filename, const char *Name, array *Destination)
859 if (*Filename == '\0')
862 fp = fopen(Filename, "r");
864 Config_Error(LOG_ERR, "Can't read %s file \"%s\": %s",
865 Name, Filename, strerror(errno));
869 array_free(Destination);
870 while (fgets(line, (int)sizeof line, fp)) {
871 ngt_TrimLastChr(line, '\n');
873 /* add text including \0 */
874 if (!array_catb(Destination, line, strlen(line) + 1)) {
875 Log(LOG_ERR, "Cannot read/add \"%s\", line %d: %s",
876 Filename, line_no, strerror(errno));
886 * Read ngIRCd configuration file.
888 * Please note that this function uses exit(1) on fatal errors and therefore
889 * can result in ngIRCd terminating!
891 * @param IsStarting Flag indicating if ngIRCd is starting or not.
892 * @returns true when the configuration file has been read
893 * successfully; false otherwise.
896 Read_Config(bool TestOnly, bool IsStarting)
898 const UINT16 defaultport = 6667;
899 char *ptr, file[FNAME_LEN];
900 struct dirent *entry;
905 Log(LOG_INFO, "Using configuration file \"%s\" ...", NGIRCd_ConfFile);
907 /* Open configuration file */
908 fd = fopen( NGIRCd_ConfFile, "r" );
910 /* No configuration file found! */
911 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
912 NGIRCd_ConfFile, strerror( errno ));
915 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
920 Set_Defaults(IsStarting);
923 Config_Error(LOG_INFO,
924 "Reading configuration from \"%s\" ...",
927 /* Clean up server configuration structure: mark all already
928 * configured servers as "once" so that they are deleted
929 * after the next disconnect and delete all unused servers.
930 * And delete all servers which are "duplicates" of servers
931 * that are already marked as "once" (such servers have been
932 * created by the last rehash but are now useless). */
933 for( i = 0; i < MAX_SERVERS; i++ ) {
934 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
936 /* This structure is in use ... */
937 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
938 /* Check for duplicates */
939 for( n = 0; n < MAX_SERVERS; n++ ) {
940 if( n == i ) continue;
942 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
943 Init_Server_Struct( &Conf_Server[n] );
945 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
951 /* Mark server as "once" */
952 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
953 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
958 /* Initialize variables */
959 Init_Server_Struct( &New_Server );
960 New_Server_Idx = NONE;
965 Read_Config_File(NGIRCd_ConfFile, fd);
968 if (Conf_IncludeDir[0]) {
969 dh = opendir(Conf_IncludeDir);
971 Config_Error(LOG_ALERT,
972 "Can't open include directory \"%s\": %s",
973 Conf_IncludeDir, strerror(errno));
975 strlcpy(Conf_IncludeDir, SYSCONFDIR, sizeof(Conf_IncludeDir));
976 strlcat(Conf_IncludeDir, CONFIG_DIR, sizeof(Conf_IncludeDir));
977 dh = opendir(Conf_IncludeDir);
980 /* Include further configuration files, if IncludeDir is available */
982 while ((entry = readdir(dh)) != NULL) {
983 ptr = strrchr(entry->d_name, '.');
984 if (!ptr || strcasecmp(ptr, ".conf") != 0)
986 snprintf(file, sizeof(file), "%s/%s",
987 Conf_IncludeDir, entry->d_name);
989 Config_Error(LOG_INFO,
990 "Reading configuration from \"%s\" ...",
992 fd = fopen(file, "r");
994 Read_Config_File(file, fd);
997 Config_Error(LOG_ALERT,
998 "Can't read configuration \"%s\": %s",
999 file, strerror(errno));
1004 /* Check if there is still a server to add */
1005 if( New_Server.name[0] ) {
1006 /* Copy data to "real" server structure */
1007 assert( New_Server_Idx > NONE );
1008 Conf_Server[New_Server_Idx] = New_Server;
1011 /* not a single listening port? Add default. */
1012 if (no_listenports() &&
1013 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
1015 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
1016 (unsigned int) defaultport, strerror(errno));
1021 if (!Conf_ListenAddress)
1022 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
1024 if (!Conf_ListenAddress) {
1025 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
1029 /* No MOTD phrase configured? (re)try motd file. */
1030 if (array_bytes(&Conf_Motd) == 0) {
1031 if (Read_TextFile(Conf_MotdFile, "MOTD", &Conf_Motd))
1032 Using_MotdFile = true;
1035 /* Try to read ngIRCd help text file. */
1036 (void)Read_TextFile(Conf_HelpFile, "help text", &Conf_Helptext);
1037 if (!array_bytes(&Conf_Helptext))
1038 Config_Error(LOG_WARNING,
1039 "No help text available, HELP command will be of limited use.");
1042 /* Make sure that all SSL-related files are readable */
1043 CheckFileReadable("CertFile", Conf_SSLOptions.CertFile);
1044 CheckFileReadable("DHFile", Conf_SSLOptions.DHFile);
1045 CheckFileReadable("KeyFile", Conf_SSLOptions.KeyFile);
1047 /* Set the default ciphers if none were configured */
1048 if (!Conf_SSLOptions.CipherList)
1049 Conf_SSLOptions.CipherList = strdup_warn(DEFAULT_CIPHERS);
1056 * Read in and handle a configuration file.
1058 * @param File Name of the configuration file.
1059 * @param fd File descriptor already opened for reading.
1062 Read_Config_File(const char *File, FILE *fd)
1064 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
1068 /* Read configuration file */
1071 if (!fgets(str, sizeof(str), fd))
1076 /* Skip comments and empty lines */
1077 if (str[0] == ';' || str[0] == '#' || str[0] == '\0')
1080 if (strlen(str) >= sizeof(str) - 1) {
1081 Config_Error(LOG_WARNING, "%s, line %d too long!",
1086 /* Is this the beginning of a new section? */
1087 if ((str[0] == '[') && (str[strlen(str) - 1] == ']')) {
1088 strlcpy(section, str, sizeof(section));
1089 if (strcasecmp(section, "[GLOBAL]") == 0
1090 || strcasecmp(section, "[LIMITS]") == 0
1091 || strcasecmp(section, "[OPTIONS]") == 0
1093 || strcasecmp(section, "[SSL]") == 0
1098 if (strcasecmp(section, "[SERVER]") == 0) {
1099 /* Check if there is already a server to add */
1100 if (New_Server.name[0]) {
1101 /* Copy data to "real" server structure */
1102 assert(New_Server_Idx > NONE);
1103 Conf_Server[New_Server_Idx] =
1107 /* Re-init structure for new server */
1108 Init_Server_Struct(&New_Server);
1110 /* Search unused item in server configuration structure */
1111 for (i = 0; i < MAX_SERVERS; i++) {
1112 /* Is this item used? */
1113 if (!Conf_Server[i].name[0])
1116 if (i >= MAX_SERVERS) {
1117 /* Oops, no free item found! */
1118 Config_Error(LOG_ERR,
1119 "Too many servers configured.");
1120 New_Server_Idx = NONE;
1126 if (strcasecmp(section, "[CHANNEL]") == 0) {
1127 count = array_length(&Conf_Channels,
1132 sizeof(struct Conf_Channel), count)) {
1133 Config_Error(LOG_ERR,
1134 "Could not allocate memory for new operator (line %d)",
1140 if (strcasecmp(section, "[OPERATOR]") == 0) {
1141 count = array_length(&Conf_Opers,
1142 sizeof(struct Conf_Oper));
1143 if (!array_alloc(&Conf_Opers,
1144 sizeof(struct Conf_Oper),
1146 Config_Error(LOG_ERR,
1147 "Could not allocate memory for new channel (line &d)",
1153 Config_Error(LOG_ERR,
1154 "%s, line %d: Unknown section \"%s\"!",
1155 File, line, section);
1158 if (section[0] == 0x1)
1161 /* Split line into variable name and parameters */
1162 ptr = strchr(str, '=');
1164 Config_Error(LOG_ERR, "%s, line %d: Syntax error!",
1174 if (strcasecmp(section, "[GLOBAL]") == 0)
1175 Handle_GLOBAL(File, line, var, arg);
1176 else if (strcasecmp(section, "[LIMITS]") == 0)
1177 Handle_LIMITS(File, line, var, arg);
1178 else if (strcasecmp(section, "[OPTIONS]") == 0)
1179 Handle_OPTIONS(File, line, var, arg);
1181 else if (strcasecmp(section, "[SSL]") == 0)
1182 Handle_SSL(File, line, var, arg);
1184 else if (strcasecmp(section, "[OPERATOR]") == 0)
1185 Handle_OPERATOR(File, line, var, arg);
1186 else if (strcasecmp(section, "[SERVER]") == 0)
1187 Handle_SERVER(File, line, var, arg);
1188 else if (strcasecmp(section, "[CHANNEL]") == 0)
1189 Handle_CHANNEL(File, line, var, arg);
1191 Config_Error(LOG_ERR,
1192 "%s, line %d: Variable \"%s\" outside section!",
1198 * Check whether a string argument is "true" or "false".
1200 * @param Arg Input string.
1201 * @returns true if the input string has been parsed as "yes", "true"
1202 * (case insensitive) or a non-zero integer value.
1205 Check_ArgIsTrue(const char *Arg)
1207 if (strcasecmp(Arg, "yes") == 0)
1209 if (strcasecmp(Arg, "true") == 0)
1218 * Handle setting of "MaxNickLength".
1220 * @param Line Line number in configuration file.
1221 * @raram Arg Input string.
1222 * @returns New configured maximum nickname length.
1225 Handle_MaxNickLength(const char *File, int Line, const char *Arg)
1229 new = (unsigned) atoi(Arg) + 1;
1230 if (new > CLIENT_NICK_LEN) {
1231 Config_Error(LOG_WARNING,
1232 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
1233 File, Line, CLIENT_NICK_LEN - 1);
1234 return CLIENT_NICK_LEN;
1237 Config_Error(LOG_WARNING,
1238 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
1246 * Output a warning messages if IDENT is configured but not compiled in.
1249 WarnIdent(const char UNUSED *File, int UNUSED Line)
1253 /* user has enabled ident lookups explicitly, but ... */
1254 Config_Error(LOG_WARNING,
1255 "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
1262 * Output a warning messages if IPv6 is configured but not compiled in.
1265 WarnIPv6(const char UNUSED *File, int UNUSED Line)
1268 if (Conf_ConnectIPv6) {
1269 /* user has enabled IPv6 explicitly, but ... */
1270 Config_Error(LOG_WARNING,
1271 "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
1278 * Output a warning messages if PAM is configured but not compiled in.
1281 WarnPAM(const char UNUSED *File, int UNUSED Line)
1285 Config_Error(LOG_WARNING,
1286 "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
1293 * Handle legacy "NoXXX" options in [GLOBAL] section.
1295 * TODO: This function and support for "NoXXX" could be removed starting
1296 * with ngIRCd release 19 (one release after marking it "deprecated").
1298 * @param Var Variable name.
1299 * @param Arg Argument string.
1300 * @returns true if a NoXXX option has been processed; false otherwise.
1303 CheckLegacyNoOption(const char *Var, const char *Arg)
1305 if(strcasecmp(Var, "NoDNS") == 0) {
1306 Conf_DNS = !Check_ArgIsTrue( Arg );
1309 if (strcasecmp(Var, "NoIdent") == 0) {
1310 Conf_Ident = !Check_ArgIsTrue(Arg);
1313 if(strcasecmp(Var, "NoPAM") == 0) {
1314 Conf_PAM = !Check_ArgIsTrue(Arg);
1321 * Handle deprecated legacy options in [GLOBAL] section.
1323 * TODO: This function and support for these options in the [Global] section
1324 * could be removed starting with ngIRCd release 19 (one release after
1325 * marking it "deprecated").
1327 * @param Var Variable name.
1328 * @param Arg Argument string.
1329 * @returns true if a legacy option has been processed; false otherwise.
1332 CheckLegacyGlobalOption(const char *File, int Line, char *Var, char *Arg)
1334 if (strcasecmp(Var, "AllowRemoteOper") == 0
1335 || strcasecmp(Var, "ChrootDir") == 0
1336 || strcasecmp(Var, "ConnectIPv4") == 0
1337 || strcasecmp(Var, "ConnectIPv6") == 0
1338 || strcasecmp(Var, "OperCanUseMode") == 0
1339 || strcasecmp(Var, "OperChanPAutoOp") == 0
1340 || strcasecmp(Var, "OperServerMode") == 0
1341 || strcasecmp(Var, "PredefChannelsOnly") == 0
1342 || strcasecmp(Var, "SyslogFacility") == 0
1343 || strcasecmp(Var, "WebircPassword") == 0) {
1344 Handle_OPTIONS(File, Line, Var, Arg);
1347 if (strcasecmp(Var, "ConnectRetry") == 0
1348 || strcasecmp(Var, "IdleTimeout") == 0
1349 || strcasecmp(Var, "MaxConnections") == 0
1350 || strcasecmp(Var, "MaxConnectionsIP") == 0
1351 || strcasecmp(Var, "MaxJoins") == 0
1352 || strcasecmp(Var, "MaxNickLength") == 0
1353 || strcasecmp(Var, "PingTimeout") == 0
1354 || strcasecmp(Var, "PongTimeout") == 0) {
1355 Handle_LIMITS(File, Line, Var, Arg);
1359 if (strcasecmp(Var, "SSLCertFile") == 0
1360 || strcasecmp(Var, "SSLDHFile") == 0
1361 || strcasecmp(Var, "SSLKeyFile") == 0
1362 || strcasecmp(Var, "SSLKeyFilePassword") == 0
1363 || strcasecmp(Var, "SSLPorts") == 0) {
1364 Handle_SSL(File, Line, Var + 3, Arg);
1373 * Strip "no" prefix of a string.
1375 * TODO: This function and support for "NoXXX" should be removed starting
1376 * with ngIRCd release 19! (One release after marking it "deprecated").
1378 * @param str Pointer to input string starting with "no".
1379 * @returns New pointer to string without "no" prefix.
1382 NoNo(const char *str)
1384 assert(strncasecmp("no", str, 2) == 0 && str[2]);
1389 * Invert "boolean" string.
1391 * TODO: This function and support for "NoXXX" should be removed starting
1392 * with ngIRCd release 19! (One release after marking it "deprecated").
1394 * @param arg "Boolean" input string.
1395 * @returns Pointer to inverted "boolean string".
1398 InvertArg(const char *arg)
1400 return yesno_to_str(!Check_ArgIsTrue(arg));
1404 * Handle variable in [Global] configuration section.
1406 * @param Line Line numer in configuration file.
1407 * @param Var Variable name.
1408 * @param Arg Variable argument.
1411 Handle_GLOBAL(const char *File, int Line, char *Var, char *Arg )
1416 const char *section;
1419 assert(File != NULL);
1421 assert(Var != NULL);
1422 assert(Arg != NULL);
1424 if (strcasecmp(Var, "Name") == 0) {
1425 len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
1426 if (len >= sizeof(Conf_ServerName))
1427 Config_Error_TooLong(File, Line, Var);
1430 if (strcasecmp(Var, "AdminInfo1") == 0) {
1431 len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
1432 if (len >= sizeof(Conf_ServerAdmin1))
1433 Config_Error_TooLong(File, Line, Var);
1436 if (strcasecmp(Var, "AdminInfo2") == 0) {
1437 len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
1438 if (len >= sizeof(Conf_ServerAdmin2))
1439 Config_Error_TooLong(File, Line, Var);
1442 if (strcasecmp(Var, "AdminEMail") == 0) {
1443 len = strlcpy(Conf_ServerAdminMail, Arg,
1444 sizeof(Conf_ServerAdminMail));
1445 if (len >= sizeof(Conf_ServerAdminMail))
1446 Config_Error_TooLong(File, Line, Var);
1449 if (strcasecmp(Var, "Info") == 0) {
1450 len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
1451 if (len >= sizeof(Conf_ServerInfo))
1452 Config_Error_TooLong(File, Line, Var);
1455 if (strcasecmp(Var, "HelpFile") == 0) {
1456 len = strlcpy(Conf_HelpFile, Arg, sizeof(Conf_HelpFile));
1457 if (len >= sizeof(Conf_HelpFile))
1458 Config_Error_TooLong(File, Line, Var);
1461 if (strcasecmp(Var, "Listen") == 0) {
1462 if (Conf_ListenAddress) {
1463 Config_Error(LOG_ERR,
1464 "Multiple Listen= options, ignoring: %s",
1468 Conf_ListenAddress = strdup_warn(Arg);
1469 /* If allocation fails, we're in trouble: we cannot ignore the
1470 * error -- otherwise ngircd would listen on all interfaces. */
1471 if (!Conf_ListenAddress) {
1472 Config_Error(LOG_ALERT,
1473 "%s exiting due to fatal errors!",
1479 if (strcasecmp(Var, "MotdFile") == 0) {
1480 len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
1481 if (len >= sizeof(Conf_MotdFile))
1482 Config_Error_TooLong(File, Line, Var);
1485 if (strcasecmp(Var, "MotdPhrase") == 0) {
1490 Config_Error_TooLong(File, Line, Var);
1493 if (!array_copyb(&Conf_Motd, Arg, len + 1))
1494 Config_Error(LOG_WARNING,
1495 "%s, line %d: Could not append MotdPhrase: %s",
1496 File, Line, strerror(errno));
1497 Using_MotdFile = false;
1500 if (strcasecmp(Var, "Network") == 0) {
1501 len = strlcpy(Conf_Network, Arg, sizeof(Conf_Network));
1502 if (len >= sizeof(Conf_Network))
1503 Config_Error_TooLong(File, Line, Var);
1504 ptr = strchr(Conf_Network, ' ');
1506 Config_Error(LOG_WARNING,
1507 "%s, line %d: \"Network\" can't contain spaces!",
1513 if(strcasecmp(Var, "Password") == 0) {
1514 len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
1515 if (len >= sizeof(Conf_ServerPwd))
1516 Config_Error_TooLong(File, Line, Var);
1519 if (strcasecmp(Var, "PidFile") == 0) {
1520 len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
1521 if (len >= sizeof(Conf_PidFile))
1522 Config_Error_TooLong(File, Line, Var);
1525 if (strcasecmp(Var, "Ports") == 0) {
1526 ports_parse(&Conf_ListenPorts, File, Line, Arg);
1529 if (strcasecmp(Var, "ServerGID") == 0) {
1530 grp = getgrnam(Arg);
1532 Conf_GID = grp->gr_gid;
1534 Conf_GID = (unsigned int)atoi(Arg);
1535 if (!Conf_GID && strcmp(Arg, "0"))
1536 Config_Error(LOG_WARNING,
1537 "%s, line %d: Value of \"%s\" is not a valid group name or ID!",
1542 if (strcasecmp(Var, "ServerUID") == 0) {
1543 pwd = getpwnam(Arg);
1545 Conf_UID = pwd->pw_uid;
1547 Conf_UID = (unsigned int)atoi(Arg);
1548 if (!Conf_UID && strcmp(Arg, "0"))
1549 Config_Error(LOG_WARNING,
1550 "%s, line %d: Value of \"%s\" is not a valid user name or ID!",
1556 if (CheckLegacyNoOption(Var, Arg)) {
1557 /* TODO: This function and support for "NoXXX" could be
1558 * be removed starting with ngIRCd release 19 (one release
1559 * after marking it "deprecated"). */
1560 Config_Error(LOG_WARNING,
1561 "%s, line %d (section \"Global\"): \"No\"-Prefix is deprecated, use \"%s = %s\" in [Options] section!",
1562 File, Line, NoNo(Var), InvertArg(Arg));
1563 if (strcasecmp(Var, "NoIdent") == 0)
1564 WarnIdent(File, Line);
1565 else if (strcasecmp(Var, "NoPam") == 0)
1566 WarnPAM(File, Line);
1569 if ((section = CheckLegacyGlobalOption(File, Line, Var, Arg))) {
1570 /** TODO: This function and support for these options in the
1571 * [Global] section could be removed starting with ngIRCd
1572 * release 19 (one release after marking it "deprecated"). */
1573 if (strncasecmp(Var, "SSL", 3) == 0) {
1574 Config_Error(LOG_WARNING,
1575 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s and rename to \"%s\"!",
1576 File, Line, Var, section,
1579 Config_Error(LOG_WARNING,
1580 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s!",
1581 File, Line, Var, section);
1586 Config_Error_Section(File, Line, Var, "Global");
1590 * Handle variable in [Limits] configuration section.
1592 * @param Line Line numer in configuration file.
1593 * @param Var Variable name.
1594 * @param Arg Variable argument.
1597 Handle_LIMITS(const char *File, int Line, char *Var, char *Arg)
1599 assert(File != NULL);
1601 assert(Var != NULL);
1602 assert(Arg != NULL);
1604 if (strcasecmp(Var, "ConnectRetry") == 0) {
1605 Conf_ConnectRetry = atoi(Arg);
1606 if (Conf_ConnectRetry < 5) {
1607 Config_Error(LOG_WARNING,
1608 "%s, line %d: Value of \"ConnectRetry\" too low!",
1610 Conf_ConnectRetry = 5;
1614 if (strcasecmp(Var, "IdleTimeout") == 0) {
1615 Conf_IdleTimeout = atoi(Arg);
1616 if (!Conf_IdleTimeout && strcmp(Arg, "0"))
1617 Config_Error_NaN(File, Line, Var);
1620 if (strcasecmp(Var, "MaxConnections") == 0) {
1621 Conf_MaxConnections = atoi(Arg);
1622 if (!Conf_MaxConnections && strcmp(Arg, "0"))
1623 Config_Error_NaN(File, Line, Var);
1626 if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
1627 Conf_MaxConnectionsIP = atoi(Arg);
1628 if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
1629 Config_Error_NaN(File, Line, Var);
1632 if (strcasecmp(Var, "MaxJoins") == 0) {
1633 Conf_MaxJoins = atoi(Arg);
1634 if (!Conf_MaxJoins && strcmp(Arg, "0"))
1635 Config_Error_NaN(File, Line, Var);
1638 if (strcasecmp(Var, "MaxNickLength") == 0) {
1639 Conf_MaxNickLength = Handle_MaxNickLength(File, Line, Arg);
1642 if (strcasecmp(Var, "MaxListSize") == 0) {
1643 Conf_MaxListSize = atoi(Arg);
1644 if (!Conf_MaxListSize && strcmp(Arg, "0"))
1645 Config_Error_NaN(File, Line, Var);
1648 if (strcasecmp(Var, "MaxPenaltyTime") == 0) {
1649 Conf_MaxPenaltyTime = atol(Arg);
1650 if (Conf_MaxPenaltyTime < -1)
1651 Conf_MaxPenaltyTime = -1; /* "unlimited" */
1654 if (strcasecmp(Var, "PingTimeout") == 0) {
1655 Conf_PingTimeout = atoi(Arg);
1656 if (Conf_PingTimeout < 5) {
1657 Config_Error(LOG_WARNING,
1658 "%s, line %d: Value of \"PingTimeout\" too low!",
1660 Conf_PingTimeout = 5;
1664 if (strcasecmp(Var, "PongTimeout") == 0) {
1665 Conf_PongTimeout = atoi(Arg);
1666 if (Conf_PongTimeout < 5) {
1667 Config_Error(LOG_WARNING,
1668 "%s, line %d: Value of \"PongTimeout\" too low!",
1670 Conf_PongTimeout = 5;
1675 Config_Error_Section(File, Line, Var, "Limits");
1679 * Handle variable in [Options] configuration section.
1681 * @param Line Line numer in configuration file.
1682 * @param Var Variable name.
1683 * @param Arg Variable argument.
1686 Handle_OPTIONS(const char *File, int Line, char *Var, char *Arg)
1691 assert(File != NULL);
1693 assert(Var != NULL);
1694 assert(Arg != NULL);
1696 if (strcasecmp(Var, "AllowedChannelTypes") == 0) {
1698 Conf_AllowedChannelTypes[0] = '\0';
1700 if (strchr(Conf_AllowedChannelTypes, *p)) {
1701 /* Prefix is already included; ignore it */
1706 if (strchr(CHANTYPES, *p)) {
1707 len = strlen(Conf_AllowedChannelTypes) + 1;
1708 assert(len < sizeof(Conf_AllowedChannelTypes));
1709 Conf_AllowedChannelTypes[len - 1] = *p;
1710 Conf_AllowedChannelTypes[len] = '\0';
1712 Config_Error(LOG_WARNING,
1713 "%s, line %d: Unknown channel prefix \"%c\" in \"AllowedChannelTypes\"!",
1720 if (strcasecmp(Var, "AllowRemoteOper") == 0) {
1721 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1724 if (strcasecmp(Var, "ChrootDir") == 0) {
1725 len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
1726 if (len >= sizeof(Conf_Chroot))
1727 Config_Error_TooLong(File, Line, Var);
1730 if (strcasecmp(Var, "CloakHost") == 0) {
1731 len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
1732 if (len >= sizeof(Conf_CloakHost))
1733 Config_Error_TooLong(File, Line, Var);
1736 if (strcasecmp(Var, "CloakHostModeX") == 0) {
1737 len = strlcpy(Conf_CloakHostModeX, Arg, sizeof(Conf_CloakHostModeX));
1738 if (len >= sizeof(Conf_CloakHostModeX))
1739 Config_Error_TooLong(File, Line, Var);
1742 if (strcasecmp(Var, "CloakHostSalt") == 0) {
1743 len = strlcpy(Conf_CloakHostSalt, Arg, sizeof(Conf_CloakHostSalt));
1744 if (len >= sizeof(Conf_CloakHostSalt))
1745 Config_Error_TooLong(File, Line, Var);
1748 if (strcasecmp(Var, "CloakUserToNick") == 0) {
1749 Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
1752 if (strcasecmp(Var, "ConnectIPv6") == 0) {
1753 Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
1754 WarnIPv6(File, Line);
1757 if (strcasecmp(Var, "ConnectIPv4") == 0) {
1758 Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
1761 if (strcasecmp(Var, "DefaultUserModes") == 0) {
1763 Conf_DefaultUserModes[0] = '\0';
1765 if (strchr(Conf_DefaultUserModes, *p)) {
1766 /* Mode is already included; ignore it */
1771 if (strchr(USERMODES, *p)) {
1772 len = strlen(Conf_DefaultUserModes) + 1;
1773 assert(len < sizeof(Conf_DefaultUserModes));
1774 Conf_DefaultUserModes[len - 1] = *p;
1775 Conf_DefaultUserModes[len] = '\0';
1777 Config_Error(LOG_WARNING,
1778 "%s, line %d: Unknown user mode \"%c\" in \"DefaultUserModes\"!",
1785 if (strcasecmp(Var, "DNS") == 0) {
1786 Conf_DNS = Check_ArgIsTrue(Arg);
1789 if (strcasecmp(Var, "Ident") == 0) {
1790 Conf_Ident = Check_ArgIsTrue(Arg);
1791 WarnIdent(File, Line);
1794 if (strcasecmp(Var, "IncludeDir") == 0) {
1795 if (Conf_IncludeDir[0]) {
1796 Config_Error(LOG_ERR,
1797 "%s, line %d: Can't overwrite value of \"IncludeDir\" variable!",
1801 len = strlcpy(Conf_IncludeDir, Arg, sizeof(Conf_IncludeDir));
1802 if (len >= sizeof(Conf_IncludeDir))
1803 Config_Error_TooLong(File, Line, Var);
1806 if (strcasecmp(Var, "MorePrivacy") == 0) {
1807 Conf_MorePrivacy = Check_ArgIsTrue(Arg);
1810 if (strcasecmp(Var, "NoticeAuth") == 0) {
1812 * TODO: This section and support for "NoticeAuth" variable
1813 * could be removed starting with ngIRCd release 24 (one
1814 * release after marking it "deprecated") ...
1816 Config_Error(LOG_WARNING,
1817 "%s, line %d (section \"Options\"): \"%s\" is deprecated, please use \"NoticeBeforeRegistration\"!",
1819 Conf_NoticeBeforeRegistration = Check_ArgIsTrue(Arg);
1822 if (strcasecmp(Var, "NoticeBeforeRegistration") == 0) {
1823 Conf_NoticeBeforeRegistration = Check_ArgIsTrue(Arg);
1826 if (strcasecmp(Var, "OperCanUseMode") == 0) {
1827 Conf_OperCanMode = Check_ArgIsTrue(Arg);
1830 if (strcasecmp(Var, "OperChanPAutoOp") == 0) {
1831 Conf_OperChanPAutoOp = Check_ArgIsTrue(Arg);
1834 if (strcasecmp(Var, "OperServerMode") == 0) {
1835 Conf_OperServerMode = Check_ArgIsTrue(Arg);
1838 if (strcasecmp(Var, "PAM") == 0) {
1839 Conf_PAM = Check_ArgIsTrue(Arg);
1840 WarnPAM(File, Line);
1843 if (strcasecmp(Var, "PAMIsOptional") == 0 ) {
1844 Conf_PAMIsOptional = Check_ArgIsTrue(Arg);
1847 if (strcasecmp(Var, "PAMServiceName") == 0) {
1848 len = strlcpy(Conf_PAMServiceName, Arg, sizeof(Conf_PAMServiceName));
1849 if (len >= sizeof(Conf_PAMServiceName))
1850 Config_Error_TooLong(File, Line, Var);
1853 if (strcasecmp(Var, "PredefChannelsOnly") == 0) {
1855 * TODO: This section and support for "PredefChannelsOnly"
1856 * could be removed starting with ngIRCd release 22 (one
1857 * release after marking it "deprecated") ...
1859 Config_Error(LOG_WARNING,
1860 "%s, line %d (section \"Options\"): \"%s\" is deprecated, please use \"AllowedChannelTypes\"!",
1862 if (Check_ArgIsTrue(Arg))
1863 Conf_AllowedChannelTypes[0] = '\0';
1865 strlcpy(Conf_AllowedChannelTypes, CHANTYPES,
1866 sizeof(Conf_AllowedChannelTypes));
1870 if (strcasecmp(Var, "RequireAuthPing") == 0) {
1871 Conf_AuthPing = Check_ArgIsTrue(Arg);
1875 if (strcasecmp(Var, "ScrubCTCP") == 0) {
1876 Conf_ScrubCTCP = Check_ArgIsTrue(Arg);
1880 if (strcasecmp(Var, "SyslogFacility") == 0) {
1881 Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
1882 Conf_SyslogFacility);
1886 if (strcasecmp(Var, "WebircPassword") == 0) {
1887 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
1888 if (len >= sizeof(Conf_WebircPwd))
1889 Config_Error_TooLong(File, Line, Var);
1893 Config_Error_Section(File, Line, Var, "Options");
1899 * Handle variable in [SSL] configuration section.
1901 * @param Line Line numer in configuration file.
1902 * @param Var Variable name.
1903 * @param Arg Variable argument.
1906 Handle_SSL(const char *File, int Line, char *Var, char *Arg)
1908 assert(File != NULL);
1910 assert(Var != NULL);
1911 assert(Arg != NULL);
1913 if (strcasecmp(Var, "CertFile") == 0) {
1914 assert(Conf_SSLOptions.CertFile == NULL);
1915 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1918 if (strcasecmp(Var, "DHFile") == 0) {
1919 assert(Conf_SSLOptions.DHFile == NULL);
1920 Conf_SSLOptions.DHFile = strdup_warn(Arg);
1923 if (strcasecmp(Var, "KeyFile") == 0) {
1924 assert(Conf_SSLOptions.KeyFile == NULL);
1925 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1928 if (strcasecmp(Var, "KeyFilePassword") == 0) {
1929 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1930 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1931 Config_Error(LOG_ERR,
1932 "%s, line %d (section \"SSL\"): Could not copy %s: %s!",
1933 File, Line, Var, strerror(errno));
1936 if (strcasecmp(Var, "Ports") == 0) {
1937 ports_parse(&Conf_SSLOptions.ListenPorts, File, Line, Arg);
1940 if (strcasecmp(Var, "CipherList") == 0) {
1941 assert(Conf_SSLOptions.CipherList == NULL);
1942 Conf_SSLOptions.CipherList = strdup_warn(Arg);
1946 Config_Error_Section(File, Line, Var, "SSL");
1952 * Handle variable in [Operator] configuration section.
1954 * @param Line Line numer in configuration file.
1955 * @param Var Variable name.
1956 * @param Arg Variable argument.
1959 Handle_OPERATOR(const char *File, int Line, char *Var, char *Arg )
1962 struct Conf_Oper *op;
1964 assert( File != NULL );
1966 assert( Var != NULL );
1967 assert( Arg != NULL );
1969 op = array_get(&Conf_Opers, sizeof(*op),
1970 array_length(&Conf_Opers, sizeof(*op)) - 1);
1974 if (strcasecmp(Var, "Name") == 0) {
1975 /* Name of IRC operator */
1976 len = strlcpy(op->name, Arg, sizeof(op->name));
1977 if (len >= sizeof(op->name))
1978 Config_Error_TooLong(File, Line, Var);
1981 if (strcasecmp(Var, "Password") == 0) {
1982 /* Password of IRC operator */
1983 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1984 if (len >= sizeof(op->pwd))
1985 Config_Error_TooLong(File, Line, Var);
1988 if (strcasecmp(Var, "Mask") == 0) {
1990 return; /* Hostname already configured */
1991 op->mask = strdup_warn( Arg );
1995 Config_Error_Section(File, Line, Var, "Operator");
1999 * Handle variable in [Server] configuration section.
2001 * @param Line Line numer in configuration file.
2002 * @param Var Variable name.
2003 * @param Arg Variable argument.
2006 Handle_SERVER(const char *File, int Line, char *Var, char *Arg )
2011 assert( File != NULL );
2013 assert( Var != NULL );
2014 assert( Arg != NULL );
2016 /* Ignore server block if no space is left in server configuration structure */
2017 if( New_Server_Idx <= NONE ) return;
2019 if( strcasecmp( Var, "Host" ) == 0 ) {
2020 /* Hostname of the server */
2021 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
2022 if (len >= sizeof( New_Server.host ))
2023 Config_Error_TooLong(File, Line, Var);
2026 if( strcasecmp( Var, "Name" ) == 0 ) {
2027 /* Name of the server ("Nick"/"ID") */
2028 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
2029 if (len >= sizeof( New_Server.name ))
2030 Config_Error_TooLong(File, Line, Var);
2033 if (strcasecmp(Var, "Bind") == 0) {
2034 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
2037 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
2041 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
2042 /* Password of this server which is sent to the peer */
2044 Config_Error(LOG_ERR,
2045 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
2048 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
2049 if (len >= sizeof( New_Server.pwd_in ))
2050 Config_Error_TooLong(File, Line, Var);
2053 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
2054 /* Passwort of the peer which must be received */
2055 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
2056 if (len >= sizeof( New_Server.pwd_out ))
2057 Config_Error_TooLong(File, Line, Var);
2060 if( strcasecmp( Var, "Port" ) == 0 ) {
2061 /* Port to which this server should connect */
2063 if (port >= 0 && port < 0xFFFF)
2064 New_Server.port = (UINT16)port;
2066 Config_Error(LOG_ERR,
2067 "%s, line %d (section \"Server\"): Illegal port number %ld!",
2072 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
2073 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
2077 if( strcasecmp( Var, "Group" ) == 0 ) {
2079 New_Server.group = atoi( Arg );
2080 if (!New_Server.group && strcmp(Arg, "0"))
2081 Config_Error_NaN(File, Line, Var);
2084 if( strcasecmp( Var, "Passive" ) == 0 ) {
2085 if (Check_ArgIsTrue(Arg))
2086 New_Server.flags |= CONF_SFLAG_DISABLED;
2089 if (strcasecmp(Var, "ServiceMask") == 0) {
2090 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
2091 sizeof(New_Server.svs_mask));
2092 if (len >= sizeof(New_Server.svs_mask))
2093 Config_Error_TooLong(File, Line, Var);
2097 Config_Error_Section(File, Line, Var, "Server");
2101 * Copy channel name into channel structure.
2103 * If the channel name is not valid because of a missing prefix ('#', '&'),
2104 * a default prefix of '#' will be added.
2106 * @param new_chan New already allocated channel structure.
2107 * @param name Name of the new channel.
2108 * @returns true on success, false otherwise.
2111 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
2113 size_t size = sizeof(new_chan->name);
2114 char *dest = new_chan->name;
2116 if (!Channel_IsValidName(name)) {
2118 * maybe user forgot to add a '#'.
2119 * This is only here for user convenience.
2125 return size > strlcpy(dest, name, size);
2129 * Handle variable in [Channel] configuration section.
2131 * @param Line Line numer in configuration file.
2132 * @param Var Variable name.
2133 * @param Arg Variable argument.
2136 Handle_CHANNEL(const char *File, int Line, char *Var, char *Arg)
2139 struct Conf_Channel *chan;
2141 assert( File != NULL );
2143 assert( Var != NULL );
2144 assert( Arg != NULL );
2146 chan = array_get(&Conf_Channels, sizeof(*chan),
2147 array_length(&Conf_Channels, sizeof(*chan)) - 1);
2151 if (strcasecmp(Var, "Name") == 0) {
2152 if (!Handle_Channelname(chan, Arg))
2153 Config_Error_TooLong(File, Line, Var);
2156 if (strcasecmp(Var, "Modes") == 0) {
2158 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
2159 if (len >= sizeof(chan->modes))
2160 Config_Error_TooLong(File, Line, Var);
2163 if( strcasecmp( Var, "Topic" ) == 0 ) {
2165 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
2166 if (len >= sizeof(chan->topic))
2167 Config_Error_TooLong(File, Line, Var);
2170 if( strcasecmp( Var, "Key" ) == 0 ) {
2171 /* Initial Channel Key (mode k) */
2172 len = strlcpy(chan->key, Arg, sizeof(chan->key));
2173 if (len >= sizeof(chan->key))
2174 Config_Error_TooLong(File, Line, Var);
2177 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
2178 /* maximum user limit, mode l */
2179 chan->maxusers = (unsigned long) atol(Arg);
2180 if (!chan->maxusers && strcmp(Arg, "0"))
2181 Config_Error_NaN(File, Line, Var);
2184 if (strcasecmp(Var, "KeyFile") == 0) {
2186 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
2187 if (len >= sizeof(chan->keyfile))
2188 Config_Error_TooLong(File, Line, Var);
2192 Config_Error_Section(File, Line, Var, "Channel");
2196 * Validate server configuration.
2198 * Please note that this function uses exit(1) on fatal errors and therefore
2199 * can result in ngIRCd terminating!
2201 * @param Configtest true if the daemon has been called with "--configtest".
2202 * @param Rehash true if re-reading configuration on runtime.
2203 * @returns true if configuration is valid.
2206 Validate_Config(bool Configtest, bool Rehash)
2208 /* Validate configuration settings. */
2211 int i, servers, servers_once;
2213 bool config_valid = true;
2216 /* Emit a warning when the config file is not a full path name */
2217 if (NGIRCd_ConfFile[0] && NGIRCd_ConfFile[0] != '/') {
2218 Config_Error(LOG_WARNING,
2219 "Not specifying a full path name to \"%s\" can cause problems when rehashing the server!",
2223 /* Validate configured server name, see RFC 2812 section 2.3.1 */
2224 ptr = Conf_ServerName;
2226 if (*ptr >= 'a' && *ptr <= 'z') continue;
2227 if (*ptr >= 'A' && *ptr <= 'Z') continue;
2228 if (*ptr >= '0' && *ptr <= '9') continue;
2229 if (ptr > Conf_ServerName) {
2230 if (*ptr == '.' || *ptr == '-')
2233 Conf_ServerName[0] = '\0';
2237 if (!Conf_ServerName[0] || !strchr(Conf_ServerName, '.'))
2239 /* No server name configured! */
2240 config_valid = false;
2241 Config_Error(LOG_ALERT,
2242 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
2244 if (!Configtest && !Rehash) {
2245 Config_Error(LOG_ALERT,
2246 "%s exiting due to fatal errors!",
2253 if (!Conf_ServerAdminMail[0]) {
2254 /* No administrative contact configured! */
2255 config_valid = false;
2256 Config_Error(LOG_ALERT,
2257 "No administrator email address configured in \"%s\" ('AdminEMail')!",
2260 Config_Error(LOG_ALERT,
2261 "%s exiting due to fatal errors!",
2268 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
2269 && !Conf_ServerAdminMail[0]) {
2270 /* No administrative information configured! */
2271 Config_Error(LOG_WARNING,
2272 "No administrative information configured but required by RFC!");
2276 if (Conf_PAM && Conf_ServerPwd[0])
2277 Config_Error(LOG_ERR,
2278 "This server uses PAM, \"Password\" in [Global] section will be ignored!");
2281 if (Conf_MaxPenaltyTime != -1)
2282 Config_Error(LOG_WARNING,
2283 "Maximum penalty increase ('MaxPenaltyTime') is set to %ld, this is not recommended!",
2284 Conf_MaxPenaltyTime);
2287 servers = servers_once = 0;
2288 for (i = 0; i < MAX_SERVERS; i++) {
2289 if (Conf_Server[i].name[0]) {
2291 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
2296 "Configuration: Operators=%ld, Servers=%d[%d], Channels=%ld",
2297 array_length(&Conf_Opers, sizeof(struct Conf_Oper)),
2298 servers, servers_once,
2299 array_length(&Conf_Channels, sizeof(struct Conf_Channel)));
2302 return config_valid;
2306 * Output "line too long" warning.
2308 * @param Line Line number in configuration file.
2309 * @param Item Affected variable name.
2312 Config_Error_TooLong(const char *File, const int Line, const char *Item)
2314 Config_Error(LOG_WARNING, "%s, line %d: Value of \"%s\" too long!",
2319 * Output "unknown variable" warning.
2321 * @param Line Line number in configuration file.
2322 * @param Item Affected variable name.
2323 * @param Section Section name.
2326 Config_Error_Section(const char *File, const int Line, const char *Item,
2327 const char *Section)
2329 Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
2330 File, Line, Section, Item);
2334 * Output "not a number" warning.
2336 * @param Line Line number in configuration file.
2337 * @param Item Affected variable name.
2340 Config_Error_NaN(const char *File, const int Line, const char *Item )
2342 Config_Error(LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
2347 * Output configuration error to console and/or logfile.
2349 * On runtime, the normal log functions of the daemon are used. But when
2350 * testing the configuration ("--configtest"), all messages go directly
2353 * @param Level Severity level of the message.
2354 * @param Format Format string; see printf() function.
2357 static void Config_Error( const int Level, const char *Format, ... )
2359 static void Config_Error( Level, Format, va_alist )
2365 char msg[MAX_LOG_MSG_LEN];
2368 assert( Format != NULL );
2371 va_start( ap, Format );
2375 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
2379 if (Level <= LOG_WARNING)
2380 printf(" - %s\n", msg);
2384 Log(Level, "%s", msg);
2390 * Dump internal state of the "configuration module".
2393 Conf_DebugDump(void)
2397 Log(LOG_DEBUG, "Configured servers:");
2398 for (i = 0; i < MAX_SERVERS; i++) {
2399 if (! Conf_Server[i].name[0])
2402 " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
2403 Conf_Server[i].name, Conf_Server[i].host,
2404 Conf_Server[i].port, Conf_Server[i].lasttry,
2405 Conf_Server[i].group, Conf_Server[i].flags,
2406 Conf_Server[i].conn_id);
2413 * Initialize server configuration structure to default values.
2415 * @param Server Pointer to server structure to initialize.
2418 Init_Server_Struct( CONF_SERVER *Server )
2420 assert( Server != NULL );
2422 memset( Server, 0, sizeof (CONF_SERVER) );
2424 Server->group = NONE;
2425 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
2427 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
2429 Proc_InitStruct(&Server->res_stat);
2430 Server->conn_id = NONE;
2431 memset(&Server->bind_addr, 0, sizeof(Server->bind_addr));