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 for(j = 0; j < predef_chan->modes_num; j++)
487 printf(" Modes = %s\n", predef_chan->modes[j]);
488 printf(" Key = %s\n", predef_chan->key);
489 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
490 printf(" Topic = %s\n", predef_chan->topic);
491 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
494 return (config_valid ? 0 : 1);
498 * Remove connection information from configured server.
500 * If the server is set as "once", delete it from our configuration;
501 * otherwise set the time for the next connection attempt.
503 * Non-server connections will be silently ignored.
506 Conf_UnsetServer( CONN_ID Idx )
511 /* Check all our configured servers */
512 for( i = 0; i < MAX_SERVERS; i++ ) {
513 if( Conf_Server[i].conn_id != Idx ) continue;
515 /* Gotcha! Mark server configuration as "unused": */
516 Conf_Server[i].conn_id = NONE;
518 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
519 /* Delete configuration here */
520 Init_Server_Struct( &Conf_Server[i] );
522 /* Set time for next connect attempt */
524 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
525 /* The connection has been "long", so we don't
526 * require the next attempt to be delayed. */
527 Conf_Server[i].lasttry =
528 t - Conf_ConnectRetry + RECONNECT_DELAY;
530 /* "Short" connection, enforce "ConnectRetry"
531 * but randomize it a little bit: 15 seconds. */
532 Conf_Server[i].lasttry =
533 #ifdef HAVE_ARC4RANDOM
534 t + (arc4random() % 15);
536 t + rand() / (RAND_MAX / 15);
544 * Set connection information for specified configured server.
547 Conf_SetServer( int ConfServer, CONN_ID Idx )
549 assert( ConfServer > NONE );
550 assert( Idx > NONE );
552 if (Conf_Server[ConfServer].conn_id > NONE &&
553 Conf_Server[ConfServer].conn_id != Idx) {
555 "Connection %d: Server configuration of \"%s\" already in use by connection %d!",
556 Idx, Conf_Server[ConfServer].name,
557 Conf_Server[ConfServer].conn_id);
558 Conn_Close(Idx, NULL, "Server configuration already in use", true);
561 Conf_Server[ConfServer].conn_id = Idx;
566 * Get index of server in configuration structure.
569 Conf_GetServer( CONN_ID Idx )
573 assert( Idx > NONE );
575 for( i = 0; i < MAX_SERVERS; i++ ) {
576 if( Conf_Server[i].conn_id == Idx ) return i;
582 * Enable a server by name and adjust its port number.
584 * @returns true if a server has been enabled and now has a valid port
585 * number and host name for outgoing connections.
588 Conf_EnableServer( const char *Name, UINT16 Port )
592 assert( Name != NULL );
593 for( i = 0; i < MAX_SERVERS; i++ ) {
594 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
595 /* Gotcha! Set port and enable server: */
596 Conf_Server[i].port = Port;
597 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
598 return (Conf_Server[i].port && Conf_Server[i].host[0]);
605 * Enable a server by name.
607 * The server is only usable as outgoing server, if it has set a valid port
608 * number for outgoing connections!
609 * If not, you have to use Conf_EnableServer() function to make it available.
611 * @returns true if a server has been enabled; false otherwise.
614 Conf_EnablePassiveServer(const char *Name)
618 assert( Name != NULL );
619 for (i = 0; i < MAX_SERVERS; i++) {
620 if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
621 && (Conf_Server[i].port > 0)) {
622 /* BINGO! Enable server */
623 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
624 Conf_Server[i].lasttry = 0;
632 * Disable a server by name.
633 * An already established connection will be disconnected.
635 * @returns true if a server was found and has been disabled.
638 Conf_DisableServer( const char *Name )
642 assert( Name != NULL );
643 for( i = 0; i < MAX_SERVERS; i++ ) {
644 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
645 /* Gotcha! Disable and disconnect server: */
646 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
647 if( Conf_Server[i].conn_id > NONE )
648 Conn_Close(Conf_Server[i].conn_id, NULL,
649 "Server link terminated on operator request",
658 * Add a new remote server to our configuration.
660 * @param Name Name of the new server.
661 * @param Port Port number to connect to or 0 for incoming connections.
662 * @param Host Host name to connect to.
663 * @param MyPwd Password that will be sent to the peer.
664 * @param PeerPwd Password that must be received from the peer.
665 * @returns true if the new server has been added; false otherwise.
668 Conf_AddServer(const char *Name, UINT16 Port, const char *Host,
669 const char *MyPwd, const char *PeerPwd)
673 assert( Name != NULL );
674 assert( Host != NULL );
675 assert( MyPwd != NULL );
676 assert( PeerPwd != NULL );
678 /* Search unused item in server configuration structure */
679 for( i = 0; i < MAX_SERVERS; i++ ) {
680 /* Is this item used? */
681 if( ! Conf_Server[i].name[0] ) break;
683 if( i >= MAX_SERVERS ) return false;
685 Init_Server_Struct( &Conf_Server[i] );
686 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
687 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
688 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
689 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
690 Conf_Server[i].port = Port;
691 Conf_Server[i].flags = CONF_SFLAG_ONCE;
697 * Check if the given nickname is reserved for services on a particular server.
699 * @param ConfServer The server index to check.
700 * @param Nick The nickname to check.
701 * @returns true if the given nickname belongs to an "IRC service".
704 Conf_NickIsService(int ConfServer, const char *Nick)
706 assert (ConfServer >= 0);
707 assert (ConfServer < MAX_SERVERS);
709 return MatchCaseInsensitiveList(Conf_Server[ConfServer].svs_mask,
714 * Check if the given nickname is blocked for "normal client" use.
716 * @param Nick The nickname to check.
717 * @returns true if the given nickname belongs to an "IRC service".
720 Conf_NickIsBlocked(const char *Nick)
724 for(i = 0; i < MAX_SERVERS; i++) {
725 if (!Conf_Server[i].name[0])
727 if (Conf_NickIsService(i, Nick))
734 * Initialize configuration settings with their default values.
737 Set_Defaults(bool InitServers)
740 char random[RANDOM_SALT_LEN + 1];
743 strcpy(Conf_ServerName, "");
744 strcpy(Conf_ServerAdmin1, "");
745 strcpy(Conf_ServerAdmin2, "");
746 strcpy(Conf_ServerAdminMail, "");
747 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
748 PACKAGE_NAME, PACKAGE_VERSION);
749 strcpy(Conf_Network, "");
750 free(Conf_ListenAddress);
751 Conf_ListenAddress = NULL;
752 array_free(&Conf_ListenPorts);
753 array_free(&Conf_Motd);
754 array_free(&Conf_Helptext);
755 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
756 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
757 strlcpy(Conf_HelpFile, DOCDIR, sizeof(Conf_HelpFile));
758 strlcat(Conf_HelpFile, HELP_FILE, sizeof(Conf_HelpFile));
759 strcpy(Conf_ServerPwd, "");
760 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
761 Conf_UID = Conf_GID = 0;
764 Conf_ConnectRetry = 60;
765 Conf_IdleTimeout = 0;
766 Conf_MaxConnections = 0;
767 Conf_MaxConnectionsIP = 5;
769 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
770 Conf_MaxPenaltyTime = -1;
771 Conf_MaxListSize = 100;
772 Conf_PingTimeout = 120;
773 Conf_PongTimeout = 20;
776 strlcpy(Conf_AllowedChannelTypes, CHANTYPES,
777 sizeof(Conf_AllowedChannelTypes));
778 Conf_AllowRemoteOper = false;
780 Conf_AuthPing = false;
782 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
783 strcpy(Conf_CloakHost, "");
784 strcpy(Conf_CloakHostModeX, "");
785 strlcpy(Conf_CloakHostSalt, ngt_RandomStr(random, RANDOM_SALT_LEN),
786 sizeof(Conf_CloakHostSalt));
787 Conf_CloakUserToNick = false;
788 Conf_ConnectIPv4 = true;
790 Conf_ConnectIPv6 = true;
792 Conf_ConnectIPv6 = false;
794 strcpy(Conf_DefaultUserModes, "");
801 strcpy(Conf_IncludeDir, "");
802 Conf_MorePrivacy = false;
803 Conf_NoticeBeforeRegistration = false;
804 Conf_OperCanMode = false;
805 Conf_OperChanPAutoOp = true;
806 Conf_OperServerMode = false;
812 Conf_PAMIsOptional = false;
813 strcpy(Conf_PAMServiceName, "ngircd");
814 Conf_ScrubCTCP = false;
817 Conf_SyslogFacility = LOG_LOCAL5;
819 Conf_SyslogFacility = 0;
823 /* Initialize server configuration structures */
825 for (i = 0; i < MAX_SERVERS;
826 Init_Server_Struct(&Conf_Server[i++]));
831 * Get number of configured listening ports.
833 * @returns The number of ports (IPv4+IPv6) on which the server should listen.
838 size_t cnt = array_bytes(&Conf_ListenPorts);
840 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
846 * Read contents of a text file into an array.
848 * This function is used to read the MOTD and help text file, for example.
850 * @param Filename Name of the file to read.
851 * @return true, when the file has been read in.
854 Read_TextFile(const char *Filename, const char *Name, array *Destination)
860 if (*Filename == '\0')
863 fp = fopen(Filename, "r");
865 Config_Error(LOG_ERR, "Can't read %s file \"%s\": %s",
866 Name, Filename, strerror(errno));
870 array_free(Destination);
871 while (fgets(line, (int)sizeof line, fp)) {
872 ngt_TrimLastChr(line, '\n');
874 /* add text including \0 */
875 if (!array_catb(Destination, line, strlen(line) + 1)) {
876 Log(LOG_ERR, "Cannot read/add \"%s\", line %d: %s",
877 Filename, line_no, strerror(errno));
887 * Read ngIRCd configuration file.
889 * Please note that this function uses exit(1) on fatal errors and therefore
890 * can result in ngIRCd terminating!
892 * @param IsStarting Flag indicating if ngIRCd is starting or not.
893 * @returns true when the configuration file has been read
894 * successfully; false otherwise.
897 Read_Config(bool TestOnly, bool IsStarting)
899 const UINT16 defaultport = 6667;
900 char *ptr, file[FNAME_LEN];
901 struct dirent *entry;
906 Config_Error(LOG_INFO, "Using configuration file \"%s\" ...", NGIRCd_ConfFile);
908 /* Open configuration file */
909 fd = fopen( NGIRCd_ConfFile, "r" );
911 /* No configuration file found! */
912 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
913 NGIRCd_ConfFile, strerror( errno ));
916 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
921 Set_Defaults(IsStarting);
924 Config_Error(LOG_INFO,
925 "Reading configuration from \"%s\" ...",
928 /* Clean up server configuration structure: mark all already
929 * configured servers as "once" so that they are deleted
930 * after the next disconnect and delete all unused servers.
931 * And delete all servers which are "duplicates" of servers
932 * that are already marked as "once" (such servers have been
933 * created by the last rehash but are now useless). */
934 for( i = 0; i < MAX_SERVERS; i++ ) {
935 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
937 /* This structure is in use ... */
938 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
939 /* Check for duplicates */
940 for( n = 0; n < MAX_SERVERS; n++ ) {
941 if( n == i ) continue;
943 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
944 Init_Server_Struct( &Conf_Server[n] );
946 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
952 /* Mark server as "once" */
953 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
954 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
959 /* Initialize variables */
960 Init_Server_Struct( &New_Server );
961 New_Server_Idx = NONE;
966 Read_Config_File(NGIRCd_ConfFile, fd);
969 if (Conf_IncludeDir[0]) {
970 dh = opendir(Conf_IncludeDir);
972 Config_Error(LOG_ALERT,
973 "Can't open include directory \"%s\": %s",
974 Conf_IncludeDir, strerror(errno));
976 strlcpy(Conf_IncludeDir, SYSCONFDIR, sizeof(Conf_IncludeDir));
977 strlcat(Conf_IncludeDir, CONFIG_DIR, sizeof(Conf_IncludeDir));
978 dh = opendir(Conf_IncludeDir);
981 /* Include further configuration files, if IncludeDir is available */
983 while ((entry = readdir(dh)) != NULL) {
984 ptr = strrchr(entry->d_name, '.');
985 if (!ptr || strcasecmp(ptr, ".conf") != 0)
987 snprintf(file, sizeof(file), "%s/%s",
988 Conf_IncludeDir, entry->d_name);
990 Config_Error(LOG_INFO,
991 "Reading configuration from \"%s\" ...",
993 fd = fopen(file, "r");
995 Read_Config_File(file, fd);
998 Config_Error(LOG_ALERT,
999 "Can't read configuration \"%s\": %s",
1000 file, strerror(errno));
1005 /* Check if there is still a server to add */
1006 if( New_Server.name[0] ) {
1007 /* Copy data to "real" server structure */
1008 assert( New_Server_Idx > NONE );
1009 Conf_Server[New_Server_Idx] = New_Server;
1012 /* not a single listening port? Add default. */
1013 if (no_listenports() &&
1014 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
1016 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
1017 (unsigned int) defaultport, strerror(errno));
1022 if (!Conf_ListenAddress)
1023 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
1025 if (!Conf_ListenAddress) {
1026 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
1030 /* No MOTD phrase configured? (re)try motd file. */
1031 if (array_bytes(&Conf_Motd) == 0) {
1032 if (Read_TextFile(Conf_MotdFile, "MOTD", &Conf_Motd))
1033 Using_MotdFile = true;
1036 /* Try to read ngIRCd help text file. */
1037 (void)Read_TextFile(Conf_HelpFile, "help text", &Conf_Helptext);
1038 if (!array_bytes(&Conf_Helptext))
1039 Config_Error(LOG_WARNING,
1040 "No help text available, HELP command will be of limited use.");
1043 /* Make sure that all SSL-related files are readable */
1044 CheckFileReadable("CertFile", Conf_SSLOptions.CertFile);
1045 CheckFileReadable("DHFile", Conf_SSLOptions.DHFile);
1046 CheckFileReadable("KeyFile", Conf_SSLOptions.KeyFile);
1048 /* Set the default ciphers if none were configured */
1049 if (!Conf_SSLOptions.CipherList)
1050 Conf_SSLOptions.CipherList = strdup_warn(DEFAULT_CIPHERS);
1057 * Read in and handle a configuration file.
1059 * @param File Name of the configuration file.
1060 * @param fd File descriptor already opened for reading.
1063 Read_Config_File(const char *File, FILE *fd)
1065 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
1069 /* Read configuration file */
1072 if (!fgets(str, sizeof(str), fd))
1077 /* Skip comments and empty lines */
1078 if (str[0] == ';' || str[0] == '#' || str[0] == '\0')
1081 if (strlen(str) >= sizeof(str) - 1) {
1082 Config_Error(LOG_WARNING, "%s, line %d too long!",
1087 /* Is this the beginning of a new section? */
1088 if ((str[0] == '[') && (str[strlen(str) - 1] == ']')) {
1089 strlcpy(section, str, sizeof(section));
1090 if (strcasecmp(section, "[GLOBAL]") == 0
1091 || strcasecmp(section, "[LIMITS]") == 0
1092 || strcasecmp(section, "[OPTIONS]") == 0
1094 || strcasecmp(section, "[SSL]") == 0
1099 if (strcasecmp(section, "[SERVER]") == 0) {
1100 /* Check if there is already a server to add */
1101 if (New_Server.name[0]) {
1102 /* Copy data to "real" server structure */
1103 assert(New_Server_Idx > NONE);
1104 Conf_Server[New_Server_Idx] =
1108 /* Re-init structure for new server */
1109 Init_Server_Struct(&New_Server);
1111 /* Search unused item in server configuration structure */
1112 for (i = 0; i < MAX_SERVERS; i++) {
1113 /* Is this item used? */
1114 if (!Conf_Server[i].name[0])
1117 if (i >= MAX_SERVERS) {
1118 /* Oops, no free item found! */
1119 Config_Error(LOG_ERR,
1120 "Too many servers configured.");
1121 New_Server_Idx = NONE;
1127 if (strcasecmp(section, "[CHANNEL]") == 0) {
1128 count = array_length(&Conf_Channels,
1133 sizeof(struct Conf_Channel), count)) {
1134 Config_Error(LOG_ERR,
1135 "Could not allocate memory for new operator (line %d)",
1141 if (strcasecmp(section, "[OPERATOR]") == 0) {
1142 count = array_length(&Conf_Opers,
1143 sizeof(struct Conf_Oper));
1144 if (!array_alloc(&Conf_Opers,
1145 sizeof(struct Conf_Oper),
1147 Config_Error(LOG_ERR,
1148 "Could not allocate memory for new channel (line &d)",
1154 Config_Error(LOG_ERR,
1155 "%s, line %d: Unknown section \"%s\"!",
1156 File, line, section);
1159 if (section[0] == 0x1)
1162 /* Split line into variable name and parameters */
1163 ptr = strchr(str, '=');
1165 Config_Error(LOG_ERR, "%s, line %d: Syntax error!",
1175 if (strcasecmp(section, "[GLOBAL]") == 0)
1176 Handle_GLOBAL(File, line, var, arg);
1177 else if (strcasecmp(section, "[LIMITS]") == 0)
1178 Handle_LIMITS(File, line, var, arg);
1179 else if (strcasecmp(section, "[OPTIONS]") == 0)
1180 Handle_OPTIONS(File, line, var, arg);
1182 else if (strcasecmp(section, "[SSL]") == 0)
1183 Handle_SSL(File, line, var, arg);
1185 else if (strcasecmp(section, "[OPERATOR]") == 0)
1186 Handle_OPERATOR(File, line, var, arg);
1187 else if (strcasecmp(section, "[SERVER]") == 0)
1188 Handle_SERVER(File, line, var, arg);
1189 else if (strcasecmp(section, "[CHANNEL]") == 0)
1190 Handle_CHANNEL(File, line, var, arg);
1192 Config_Error(LOG_ERR,
1193 "%s, line %d: Variable \"%s\" outside section!",
1199 * Check whether a string argument is "true" or "false".
1201 * @param Arg Input string.
1202 * @returns true if the input string has been parsed as "yes", "true"
1203 * (case insensitive) or a non-zero integer value.
1206 Check_ArgIsTrue(const char *Arg)
1208 if (strcasecmp(Arg, "yes") == 0)
1210 if (strcasecmp(Arg, "true") == 0)
1219 * Handle setting of "MaxNickLength".
1221 * @param Line Line number in configuration file.
1222 * @raram Arg Input string.
1223 * @returns New configured maximum nickname length.
1226 Handle_MaxNickLength(const char *File, int Line, const char *Arg)
1230 new = (unsigned) atoi(Arg) + 1;
1231 if (new > CLIENT_NICK_LEN) {
1232 Config_Error(LOG_WARNING,
1233 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
1234 File, Line, CLIENT_NICK_LEN - 1);
1235 return CLIENT_NICK_LEN;
1238 Config_Error(LOG_WARNING,
1239 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
1247 * Output a warning messages if IDENT is configured but not compiled in.
1250 WarnIdent(const char UNUSED *File, int UNUSED Line)
1254 /* user has enabled ident lookups explicitly, but ... */
1255 Config_Error(LOG_WARNING,
1256 "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
1263 * Output a warning messages if IPv6 is configured but not compiled in.
1266 WarnIPv6(const char UNUSED *File, int UNUSED Line)
1269 if (Conf_ConnectIPv6) {
1270 /* user has enabled IPv6 explicitly, but ... */
1271 Config_Error(LOG_WARNING,
1272 "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
1279 * Output a warning messages if PAM is configured but not compiled in.
1282 WarnPAM(const char UNUSED *File, int UNUSED Line)
1286 Config_Error(LOG_WARNING,
1287 "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
1295 * Handle variable in [Global] configuration section.
1297 * @param Line Line numer in configuration file.
1298 * @param Var Variable name.
1299 * @param Arg Variable argument.
1302 Handle_GLOBAL(const char *File, int Line, char *Var, char *Arg )
1309 assert(File != NULL);
1311 assert(Var != NULL);
1312 assert(Arg != NULL);
1314 if (strcasecmp(Var, "Name") == 0) {
1315 len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
1316 if (len >= sizeof(Conf_ServerName))
1317 Config_Error_TooLong(File, Line, Var);
1320 if (strcasecmp(Var, "AdminInfo1") == 0) {
1321 len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
1322 if (len >= sizeof(Conf_ServerAdmin1))
1323 Config_Error_TooLong(File, Line, Var);
1326 if (strcasecmp(Var, "AdminInfo2") == 0) {
1327 len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
1328 if (len >= sizeof(Conf_ServerAdmin2))
1329 Config_Error_TooLong(File, Line, Var);
1332 if (strcasecmp(Var, "AdminEMail") == 0) {
1333 len = strlcpy(Conf_ServerAdminMail, Arg,
1334 sizeof(Conf_ServerAdminMail));
1335 if (len >= sizeof(Conf_ServerAdminMail))
1336 Config_Error_TooLong(File, Line, Var);
1339 if (strcasecmp(Var, "Info") == 0) {
1340 len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
1341 if (len >= sizeof(Conf_ServerInfo))
1342 Config_Error_TooLong(File, Line, Var);
1345 if (strcasecmp(Var, "HelpFile") == 0) {
1346 len = strlcpy(Conf_HelpFile, Arg, sizeof(Conf_HelpFile));
1347 if (len >= sizeof(Conf_HelpFile))
1348 Config_Error_TooLong(File, Line, Var);
1351 if (strcasecmp(Var, "Listen") == 0) {
1352 if (Conf_ListenAddress) {
1353 Config_Error(LOG_ERR,
1354 "Multiple Listen= options, ignoring: %s",
1358 Conf_ListenAddress = strdup_warn(Arg);
1359 /* If allocation fails, we're in trouble: we cannot ignore the
1360 * error -- otherwise ngircd would listen on all interfaces. */
1361 if (!Conf_ListenAddress) {
1362 Config_Error(LOG_ALERT,
1363 "%s exiting due to fatal errors!",
1369 if (strcasecmp(Var, "MotdFile") == 0) {
1370 len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
1371 if (len >= sizeof(Conf_MotdFile))
1372 Config_Error_TooLong(File, Line, Var);
1375 if (strcasecmp(Var, "MotdPhrase") == 0) {
1380 Config_Error_TooLong(File, Line, Var);
1383 if (!array_copyb(&Conf_Motd, Arg, len + 1))
1384 Config_Error(LOG_WARNING,
1385 "%s, line %d: Could not append MotdPhrase: %s",
1386 File, Line, strerror(errno));
1387 Using_MotdFile = false;
1390 if (strcasecmp(Var, "Network") == 0) {
1391 len = strlcpy(Conf_Network, Arg, sizeof(Conf_Network));
1392 if (len >= sizeof(Conf_Network))
1393 Config_Error_TooLong(File, Line, Var);
1394 ptr = strchr(Conf_Network, ' ');
1396 Config_Error(LOG_WARNING,
1397 "%s, line %d: \"Network\" can't contain spaces!",
1403 if(strcasecmp(Var, "Password") == 0) {
1404 len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
1405 if (len >= sizeof(Conf_ServerPwd))
1406 Config_Error_TooLong(File, Line, Var);
1409 if (strcasecmp(Var, "PidFile") == 0) {
1410 len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
1411 if (len >= sizeof(Conf_PidFile))
1412 Config_Error_TooLong(File, Line, Var);
1415 if (strcasecmp(Var, "Ports") == 0) {
1416 ports_parse(&Conf_ListenPorts, File, Line, Arg);
1419 if (strcasecmp(Var, "ServerGID") == 0) {
1420 grp = getgrnam(Arg);
1422 Conf_GID = grp->gr_gid;
1424 Conf_GID = (unsigned int)atoi(Arg);
1425 if (!Conf_GID && strcmp(Arg, "0"))
1426 Config_Error(LOG_WARNING,
1427 "%s, line %d: Value of \"%s\" is not a valid group name or ID!",
1432 if (strcasecmp(Var, "ServerUID") == 0) {
1433 pwd = getpwnam(Arg);
1435 Conf_UID = pwd->pw_uid;
1437 Conf_UID = (unsigned int)atoi(Arg);
1438 if (!Conf_UID && strcmp(Arg, "0"))
1439 Config_Error(LOG_WARNING,
1440 "%s, line %d: Value of \"%s\" is not a valid user name or ID!",
1446 Config_Error_Section(File, Line, Var, "Global");
1450 * Handle variable in [Limits] configuration section.
1452 * @param Line Line numer in configuration file.
1453 * @param Var Variable name.
1454 * @param Arg Variable argument.
1457 Handle_LIMITS(const char *File, int Line, char *Var, char *Arg)
1459 assert(File != NULL);
1461 assert(Var != NULL);
1462 assert(Arg != NULL);
1464 if (strcasecmp(Var, "ConnectRetry") == 0) {
1465 Conf_ConnectRetry = atoi(Arg);
1466 if (Conf_ConnectRetry < 5) {
1467 Config_Error(LOG_WARNING,
1468 "%s, line %d: Value of \"ConnectRetry\" too low!",
1470 Conf_ConnectRetry = 5;
1474 if (strcasecmp(Var, "IdleTimeout") == 0) {
1475 Conf_IdleTimeout = atoi(Arg);
1476 if (!Conf_IdleTimeout && strcmp(Arg, "0"))
1477 Config_Error_NaN(File, Line, Var);
1480 if (strcasecmp(Var, "MaxConnections") == 0) {
1481 Conf_MaxConnections = atoi(Arg);
1482 if (!Conf_MaxConnections && strcmp(Arg, "0"))
1483 Config_Error_NaN(File, Line, Var);
1486 if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
1487 Conf_MaxConnectionsIP = atoi(Arg);
1488 if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
1489 Config_Error_NaN(File, Line, Var);
1492 if (strcasecmp(Var, "MaxJoins") == 0) {
1493 Conf_MaxJoins = atoi(Arg);
1494 if (!Conf_MaxJoins && strcmp(Arg, "0"))
1495 Config_Error_NaN(File, Line, Var);
1498 if (strcasecmp(Var, "MaxNickLength") == 0) {
1499 Conf_MaxNickLength = Handle_MaxNickLength(File, Line, Arg);
1502 if (strcasecmp(Var, "MaxListSize") == 0) {
1503 Conf_MaxListSize = atoi(Arg);
1504 if (!Conf_MaxListSize && strcmp(Arg, "0"))
1505 Config_Error_NaN(File, Line, Var);
1508 if (strcasecmp(Var, "MaxPenaltyTime") == 0) {
1509 Conf_MaxPenaltyTime = atol(Arg);
1510 if (Conf_MaxPenaltyTime < -1)
1511 Conf_MaxPenaltyTime = -1; /* "unlimited" */
1514 if (strcasecmp(Var, "PingTimeout") == 0) {
1515 Conf_PingTimeout = atoi(Arg);
1516 if (Conf_PingTimeout < 5) {
1517 Config_Error(LOG_WARNING,
1518 "%s, line %d: Value of \"PingTimeout\" too low!",
1520 Conf_PingTimeout = 5;
1524 if (strcasecmp(Var, "PongTimeout") == 0) {
1525 Conf_PongTimeout = atoi(Arg);
1526 if (Conf_PongTimeout < 5) {
1527 Config_Error(LOG_WARNING,
1528 "%s, line %d: Value of \"PongTimeout\" too low!",
1530 Conf_PongTimeout = 5;
1535 Config_Error_Section(File, Line, Var, "Limits");
1539 * Handle variable in [Options] configuration section.
1541 * @param Line Line numer in configuration file.
1542 * @param Var Variable name.
1543 * @param Arg Variable argument.
1546 Handle_OPTIONS(const char *File, int Line, char *Var, char *Arg)
1551 assert(File != NULL);
1553 assert(Var != NULL);
1554 assert(Arg != NULL);
1556 if (strcasecmp(Var, "AllowedChannelTypes") == 0) {
1558 Conf_AllowedChannelTypes[0] = '\0';
1560 if (strchr(Conf_AllowedChannelTypes, *p)) {
1561 /* Prefix is already included; ignore it */
1566 if (strchr(CHANTYPES, *p)) {
1567 len = strlen(Conf_AllowedChannelTypes) + 1;
1568 assert(len < sizeof(Conf_AllowedChannelTypes));
1569 Conf_AllowedChannelTypes[len - 1] = *p;
1570 Conf_AllowedChannelTypes[len] = '\0';
1572 Config_Error(LOG_WARNING,
1573 "%s, line %d: Unknown channel prefix \"%c\" in \"AllowedChannelTypes\"!",
1580 if (strcasecmp(Var, "AllowRemoteOper") == 0) {
1581 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1584 if (strcasecmp(Var, "ChrootDir") == 0) {
1585 len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
1586 if (len >= sizeof(Conf_Chroot))
1587 Config_Error_TooLong(File, Line, Var);
1590 if (strcasecmp(Var, "CloakHost") == 0) {
1591 len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
1592 if (len >= sizeof(Conf_CloakHost))
1593 Config_Error_TooLong(File, Line, Var);
1596 if (strcasecmp(Var, "CloakHostModeX") == 0) {
1597 len = strlcpy(Conf_CloakHostModeX, Arg, sizeof(Conf_CloakHostModeX));
1598 if (len >= sizeof(Conf_CloakHostModeX))
1599 Config_Error_TooLong(File, Line, Var);
1602 if (strcasecmp(Var, "CloakHostSalt") == 0) {
1603 len = strlcpy(Conf_CloakHostSalt, Arg, sizeof(Conf_CloakHostSalt));
1604 if (len >= sizeof(Conf_CloakHostSalt))
1605 Config_Error_TooLong(File, Line, Var);
1608 if (strcasecmp(Var, "CloakUserToNick") == 0) {
1609 Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
1612 if (strcasecmp(Var, "ConnectIPv6") == 0) {
1613 Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
1614 WarnIPv6(File, Line);
1617 if (strcasecmp(Var, "ConnectIPv4") == 0) {
1618 Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
1621 if (strcasecmp(Var, "DefaultUserModes") == 0) {
1623 Conf_DefaultUserModes[0] = '\0';
1625 if (strchr(Conf_DefaultUserModes, *p)) {
1626 /* Mode is already included; ignore it */
1631 if (strchr(USERMODES, *p)) {
1632 len = strlen(Conf_DefaultUserModes) + 1;
1633 assert(len < sizeof(Conf_DefaultUserModes));
1634 Conf_DefaultUserModes[len - 1] = *p;
1635 Conf_DefaultUserModes[len] = '\0';
1637 Config_Error(LOG_WARNING,
1638 "%s, line %d: Unknown user mode \"%c\" in \"DefaultUserModes\"!",
1645 if (strcasecmp(Var, "DNS") == 0) {
1646 Conf_DNS = Check_ArgIsTrue(Arg);
1649 if (strcasecmp(Var, "Ident") == 0) {
1650 Conf_Ident = Check_ArgIsTrue(Arg);
1651 WarnIdent(File, Line);
1654 if (strcasecmp(Var, "IncludeDir") == 0) {
1655 if (Conf_IncludeDir[0]) {
1656 Config_Error(LOG_ERR,
1657 "%s, line %d: Can't overwrite value of \"IncludeDir\" variable!",
1661 len = strlcpy(Conf_IncludeDir, Arg, sizeof(Conf_IncludeDir));
1662 if (len >= sizeof(Conf_IncludeDir))
1663 Config_Error_TooLong(File, Line, Var);
1666 if (strcasecmp(Var, "MorePrivacy") == 0) {
1667 Conf_MorePrivacy = Check_ArgIsTrue(Arg);
1670 if (strcasecmp(Var, "NoticeBeforeRegistration") == 0) {
1671 Conf_NoticeBeforeRegistration = Check_ArgIsTrue(Arg);
1674 if (strcasecmp(Var, "OperCanUseMode") == 0) {
1675 Conf_OperCanMode = Check_ArgIsTrue(Arg);
1678 if (strcasecmp(Var, "OperChanPAutoOp") == 0) {
1679 Conf_OperChanPAutoOp = Check_ArgIsTrue(Arg);
1682 if (strcasecmp(Var, "OperServerMode") == 0) {
1683 Conf_OperServerMode = Check_ArgIsTrue(Arg);
1686 if (strcasecmp(Var, "PAM") == 0) {
1687 Conf_PAM = Check_ArgIsTrue(Arg);
1688 WarnPAM(File, Line);
1691 if (strcasecmp(Var, "PAMIsOptional") == 0 ) {
1692 Conf_PAMIsOptional = Check_ArgIsTrue(Arg);
1695 if (strcasecmp(Var, "PAMServiceName") == 0) {
1696 len = strlcpy(Conf_PAMServiceName, Arg, sizeof(Conf_PAMServiceName));
1697 if (len >= sizeof(Conf_PAMServiceName))
1698 Config_Error_TooLong(File, Line, Var);
1702 if (strcasecmp(Var, "RequireAuthPing") == 0) {
1703 Conf_AuthPing = Check_ArgIsTrue(Arg);
1707 if (strcasecmp(Var, "ScrubCTCP") == 0) {
1708 Conf_ScrubCTCP = Check_ArgIsTrue(Arg);
1712 if (strcasecmp(Var, "SyslogFacility") == 0) {
1713 Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
1714 Conf_SyslogFacility);
1718 if (strcasecmp(Var, "WebircPassword") == 0) {
1719 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
1720 if (len >= sizeof(Conf_WebircPwd))
1721 Config_Error_TooLong(File, Line, Var);
1725 Config_Error_Section(File, Line, Var, "Options");
1731 * Handle variable in [SSL] configuration section.
1733 * @param Line Line numer in configuration file.
1734 * @param Var Variable name.
1735 * @param Arg Variable argument.
1738 Handle_SSL(const char *File, int Line, char *Var, char *Arg)
1740 assert(File != NULL);
1742 assert(Var != NULL);
1743 assert(Arg != NULL);
1745 if (strcasecmp(Var, "CertFile") == 0) {
1746 assert(Conf_SSLOptions.CertFile == NULL);
1747 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1750 if (strcasecmp(Var, "DHFile") == 0) {
1751 assert(Conf_SSLOptions.DHFile == NULL);
1752 Conf_SSLOptions.DHFile = strdup_warn(Arg);
1755 if (strcasecmp(Var, "KeyFile") == 0) {
1756 assert(Conf_SSLOptions.KeyFile == NULL);
1757 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1760 if (strcasecmp(Var, "KeyFilePassword") == 0) {
1761 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1762 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1763 Config_Error(LOG_ERR,
1764 "%s, line %d (section \"SSL\"): Could not copy %s: %s!",
1765 File, Line, Var, strerror(errno));
1768 if (strcasecmp(Var, "Ports") == 0) {
1769 ports_parse(&Conf_SSLOptions.ListenPorts, File, Line, Arg);
1772 if (strcasecmp(Var, "CipherList") == 0) {
1773 assert(Conf_SSLOptions.CipherList == NULL);
1774 Conf_SSLOptions.CipherList = strdup_warn(Arg);
1778 Config_Error_Section(File, Line, Var, "SSL");
1784 * Handle variable in [Operator] configuration section.
1786 * @param Line Line numer in configuration file.
1787 * @param Var Variable name.
1788 * @param Arg Variable argument.
1791 Handle_OPERATOR(const char *File, int Line, char *Var, char *Arg )
1794 struct Conf_Oper *op;
1796 assert( File != NULL );
1798 assert( Var != NULL );
1799 assert( Arg != NULL );
1801 op = array_get(&Conf_Opers, sizeof(*op),
1802 array_length(&Conf_Opers, sizeof(*op)) - 1);
1806 if (strcasecmp(Var, "Name") == 0) {
1807 /* Name of IRC operator */
1808 len = strlcpy(op->name, Arg, sizeof(op->name));
1809 if (len >= sizeof(op->name))
1810 Config_Error_TooLong(File, Line, Var);
1813 if (strcasecmp(Var, "Password") == 0) {
1814 /* Password of IRC operator */
1815 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1816 if (len >= sizeof(op->pwd))
1817 Config_Error_TooLong(File, Line, Var);
1820 if (strcasecmp(Var, "Mask") == 0) {
1822 return; /* Hostname already configured */
1823 op->mask = strdup_warn( Arg );
1827 Config_Error_Section(File, Line, Var, "Operator");
1831 * Handle variable in [Server] configuration section.
1833 * @param Line Line numer in configuration file.
1834 * @param Var Variable name.
1835 * @param Arg Variable argument.
1838 Handle_SERVER(const char *File, int Line, char *Var, char *Arg )
1843 assert( File != NULL );
1845 assert( Var != NULL );
1846 assert( Arg != NULL );
1848 /* Ignore server block if no space is left in server configuration structure */
1849 if( New_Server_Idx <= NONE ) return;
1851 if( strcasecmp( Var, "Host" ) == 0 ) {
1852 /* Hostname of the server */
1853 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1854 if (len >= sizeof( New_Server.host ))
1855 Config_Error_TooLong(File, Line, Var);
1858 if( strcasecmp( Var, "Name" ) == 0 ) {
1859 /* Name of the server ("Nick"/"ID") */
1860 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1861 if (len >= sizeof( New_Server.name ))
1862 Config_Error_TooLong(File, Line, Var);
1865 if (strcasecmp(Var, "Bind") == 0) {
1866 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1869 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1873 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1874 /* Password of this server which is sent to the peer */
1876 Config_Error(LOG_ERR,
1877 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1880 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1881 if (len >= sizeof( New_Server.pwd_in ))
1882 Config_Error_TooLong(File, Line, Var);
1885 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1886 /* Passwort of the peer which must be received */
1887 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1888 if (len >= sizeof( New_Server.pwd_out ))
1889 Config_Error_TooLong(File, Line, Var);
1892 if( strcasecmp( Var, "Port" ) == 0 ) {
1893 /* Port to which this server should connect */
1895 if (port >= 0 && port < 0xFFFF)
1896 New_Server.port = (UINT16)port;
1898 Config_Error(LOG_ERR,
1899 "%s, line %d (section \"Server\"): Illegal port number %ld!",
1904 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1905 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1909 if( strcasecmp( Var, "Group" ) == 0 ) {
1911 New_Server.group = atoi( Arg );
1912 if (!New_Server.group && strcmp(Arg, "0"))
1913 Config_Error_NaN(File, Line, Var);
1916 if( strcasecmp( Var, "Passive" ) == 0 ) {
1917 if (Check_ArgIsTrue(Arg))
1918 New_Server.flags |= CONF_SFLAG_DISABLED;
1921 if (strcasecmp(Var, "ServiceMask") == 0) {
1922 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1923 sizeof(New_Server.svs_mask));
1924 if (len >= sizeof(New_Server.svs_mask))
1925 Config_Error_TooLong(File, Line, Var);
1929 Config_Error_Section(File, Line, Var, "Server");
1933 * Copy channel name into channel structure.
1935 * If the channel name is not valid because of a missing prefix ('#', '&'),
1936 * a default prefix of '#' will be added.
1938 * @param new_chan New already allocated channel structure.
1939 * @param name Name of the new channel.
1940 * @returns true on success, false otherwise.
1943 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1945 size_t size = sizeof(new_chan->name);
1946 char *dest = new_chan->name;
1948 if (!Channel_IsValidName(name)) {
1950 * maybe user forgot to add a '#'.
1951 * This is only here for user convenience.
1957 return size > strlcpy(dest, name, size);
1961 * Handle variable in [Channel] configuration section.
1963 * @param Line Line numer in configuration file.
1964 * @param Var Variable name.
1965 * @param Arg Variable argument.
1968 Handle_CHANNEL(const char *File, int Line, char *Var, char *Arg)
1971 struct Conf_Channel *chan;
1973 assert( File != NULL );
1975 assert( Var != NULL );
1976 assert( Arg != NULL );
1978 chan = array_get(&Conf_Channels, sizeof(*chan),
1979 array_length(&Conf_Channels, sizeof(*chan)) - 1);
1983 if (strcasecmp(Var, "Name") == 0) {
1984 if (!Handle_Channelname(chan, Arg))
1985 Config_Error_TooLong(File, Line, Var);
1988 if (strcasecmp(Var, "Modes") == 0) {
1990 if(chan->modes_num >= sizeof(chan->modes)) {
1991 Config_Error(LOG_ERR, "Too many Modes, option ignored.");
1994 chan->modes[chan->modes_num++] = strndup(Arg, COMMAND_LEN);
1995 if(strlen(Arg) >= COMMAND_LEN)
1996 Config_Error_TooLong(File, Line, Var);
1999 if( strcasecmp( Var, "Topic" ) == 0 ) {
2001 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
2002 if (len >= sizeof(chan->topic))
2003 Config_Error_TooLong(File, Line, Var);
2006 if( strcasecmp( Var, "Key" ) == 0 ) {
2007 /* Initial Channel Key (mode k) */
2008 len = strlcpy(chan->key, Arg, sizeof(chan->key));
2009 if (len >= sizeof(chan->key))
2010 Config_Error_TooLong(File, Line, Var);
2011 Config_Error(LOG_WARNING,
2012 "%s, line %d (section \"Channel\"): \"%s\" is deprecated here, use \"Modes = +k <key>\"!",
2016 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
2017 /* maximum user limit, mode l */
2018 chan->maxusers = (unsigned long) atol(Arg);
2019 if (!chan->maxusers && strcmp(Arg, "0"))
2020 Config_Error_NaN(File, Line, Var);
2021 Config_Error(LOG_WARNING,
2022 "%s, line %d (section \"Channel\"): \"%s\" is deprecated here, use \"Modes = +l <limit>\"!",
2026 if (strcasecmp(Var, "KeyFile") == 0) {
2028 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
2029 if (len >= sizeof(chan->keyfile))
2030 Config_Error_TooLong(File, Line, Var);
2034 Config_Error_Section(File, Line, Var, "Channel");
2038 * Validate server configuration.
2040 * Please note that this function uses exit(1) on fatal errors and therefore
2041 * can result in ngIRCd terminating!
2043 * @param Configtest true if the daemon has been called with "--configtest".
2044 * @param Rehash true if re-reading configuration on runtime.
2045 * @returns true if configuration is valid.
2048 Validate_Config(bool Configtest, bool Rehash)
2050 /* Validate configuration settings. */
2053 int i, servers, servers_once;
2055 bool config_valid = true;
2058 /* Emit a warning when the config file is not a full path name */
2059 if (NGIRCd_ConfFile[0] && NGIRCd_ConfFile[0] != '/') {
2060 Config_Error(LOG_WARNING,
2061 "Not specifying a full path name to \"%s\" can cause problems when rehashing the server!",
2065 /* Validate configured server name, see RFC 2812 section 2.3.1 */
2066 ptr = Conf_ServerName;
2068 if (*ptr >= 'a' && *ptr <= 'z') continue;
2069 if (*ptr >= 'A' && *ptr <= 'Z') continue;
2070 if (*ptr >= '0' && *ptr <= '9') continue;
2071 if (ptr > Conf_ServerName) {
2072 if (*ptr == '.' || *ptr == '-')
2075 Conf_ServerName[0] = '\0';
2079 if (!Conf_ServerName[0] || !strchr(Conf_ServerName, '.'))
2081 /* No server name configured! */
2082 config_valid = false;
2083 Config_Error(LOG_ALERT,
2084 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
2086 if (!Configtest && !Rehash) {
2087 Config_Error(LOG_ALERT,
2088 "%s exiting due to fatal errors!",
2095 if (!Conf_ServerAdminMail[0]) {
2096 /* No administrative contact configured! */
2097 config_valid = false;
2098 Config_Error(LOG_ALERT,
2099 "No administrator email address configured in \"%s\" ('AdminEMail')!",
2102 Config_Error(LOG_ALERT,
2103 "%s exiting due to fatal errors!",
2110 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
2111 && !Conf_ServerAdminMail[0]) {
2112 /* No administrative information configured! */
2113 Config_Error(LOG_WARNING,
2114 "No administrative information configured but required by RFC!");
2118 if (Conf_PAM && Conf_ServerPwd[0])
2119 Config_Error(LOG_ERR,
2120 "This server uses PAM, \"Password\" in [Global] section will be ignored!");
2123 if (Conf_MaxPenaltyTime != -1)
2124 Config_Error(LOG_WARNING,
2125 "Maximum penalty increase ('MaxPenaltyTime') is set to %ld, this is not recommended!",
2126 Conf_MaxPenaltyTime);
2129 servers = servers_once = 0;
2130 for (i = 0; i < MAX_SERVERS; i++) {
2131 if (Conf_Server[i].name[0]) {
2133 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
2138 "Configuration: Operators=%ld, Servers=%d[%d], Channels=%ld",
2139 array_length(&Conf_Opers, sizeof(struct Conf_Oper)),
2140 servers, servers_once,
2141 array_length(&Conf_Channels, sizeof(struct Conf_Channel)));
2144 return config_valid;
2148 * Output "line too long" warning.
2150 * @param Line Line number in configuration file.
2151 * @param Item Affected variable name.
2154 Config_Error_TooLong(const char *File, const int Line, const char *Item)
2156 Config_Error(LOG_WARNING, "%s, line %d: Value of \"%s\" too long!",
2161 * Output "unknown variable" warning.
2163 * @param Line Line number in configuration file.
2164 * @param Item Affected variable name.
2165 * @param Section Section name.
2168 Config_Error_Section(const char *File, const int Line, const char *Item,
2169 const char *Section)
2171 Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
2172 File, Line, Section, Item);
2176 * Output "not a number" warning.
2178 * @param Line Line number in configuration file.
2179 * @param Item Affected variable name.
2182 Config_Error_NaN(const char *File, const int Line, const char *Item )
2184 Config_Error(LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
2189 * Output configuration error to console and/or logfile.
2191 * On runtime, the normal log functions of the daemon are used. But when
2192 * testing the configuration ("--configtest"), all messages go directly
2195 * @param Level Severity level of the message.
2196 * @param Format Format string; see printf() function.
2199 static void Config_Error( const int Level, const char *Format, ... )
2201 static void Config_Error( Level, Format, va_alist )
2207 char msg[MAX_LOG_MSG_LEN];
2210 assert( Format != NULL );
2213 va_start( ap, Format );
2217 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
2221 if (Level <= LOG_WARNING)
2222 printf(" - %s\n", msg);
2226 Log(Level, "%s", msg);
2232 * Dump internal state of the "configuration module".
2235 Conf_DebugDump(void)
2239 Log(LOG_DEBUG, "Configured servers:");
2240 for (i = 0; i < MAX_SERVERS; i++) {
2241 if (! Conf_Server[i].name[0])
2244 " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
2245 Conf_Server[i].name, Conf_Server[i].host,
2246 Conf_Server[i].port, Conf_Server[i].lasttry,
2247 Conf_Server[i].group, Conf_Server[i].flags,
2248 Conf_Server[i].conn_id);
2255 * Initialize server configuration structure to default values.
2257 * @param Server Pointer to server structure to initialize.
2260 Init_Server_Struct( CONF_SERVER *Server )
2262 assert( Server != NULL );
2264 memset( Server, 0, sizeof (CONF_SERVER) );
2266 Server->group = NONE;
2267 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
2269 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
2271 Proc_InitStruct(&Server->res_stat);
2272 Server->conn_id = NONE;
2273 memset(&Server->bind_addr, 0, sizeof(Server->bind_addr));