2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2014 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(" MaxListSize = %d\n", Conf_MaxListSize);
392 printf(" PingTimeout = %d\n", Conf_PingTimeout);
393 printf(" PongTimeout = %d\n", Conf_PongTimeout);
397 printf(" AllowedChannelTypes = %s\n", Conf_AllowedChannelTypes);
398 printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
399 printf(" ChrootDir = %s\n", Conf_Chroot);
400 printf(" CloakHost = %s\n", Conf_CloakHost);
401 printf(" CloakHostModeX = %s\n", Conf_CloakHostModeX);
402 printf(" CloakHostSalt = %s\n", Conf_CloakHostSalt);
403 printf(" CloakUserToNick = %s\n", yesno_to_str(Conf_CloakUserToNick));
405 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
406 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
408 printf(" DefaultUserModes = %s\n", Conf_DefaultUserModes);
409 printf(" DNS = %s\n", yesno_to_str(Conf_DNS));
411 printf(" Ident = %s\n", yesno_to_str(Conf_Ident));
413 printf(" IncludeDir = %s\n", Conf_IncludeDir);
414 printf(" MorePrivacy = %s\n", yesno_to_str(Conf_MorePrivacy));
415 printf(" NoticeBeforeRegistration = %s\n", yesno_to_str(Conf_NoticeBeforeRegistration));
416 printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
417 printf(" OperChanPAutoOp = %s\n", yesno_to_str(Conf_OperChanPAutoOp));
418 printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
420 printf(" PAM = %s\n", yesno_to_str(Conf_PAM));
421 printf(" PAMIsOptional = %s\n", yesno_to_str(Conf_PAMIsOptional));
424 printf(" RequireAuthPing = %s\n", yesno_to_str(Conf_AuthPing));
426 printf(" ScrubCTCP = %s\n", yesno_to_str(Conf_ScrubCTCP));
428 printf(" SyslogFacility = %s\n",
429 ngt_SyslogFacilityName(Conf_SyslogFacility));
431 printf(" WebircPassword = %s\n", Conf_WebircPwd);
436 printf(" CertFile = %s\n", Conf_SSLOptions.CertFile
437 ? Conf_SSLOptions.CertFile : "");
438 printf(" CipherList = %s\n", Conf_SSLOptions.CipherList ?
439 Conf_SSLOptions.CipherList : DEFAULT_CIPHERS);
440 printf(" DHFile = %s\n", Conf_SSLOptions.DHFile
441 ? Conf_SSLOptions.DHFile : "");
442 printf(" KeyFile = %s\n", Conf_SSLOptions.KeyFile
443 ? Conf_SSLOptions.KeyFile : "");
444 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
445 puts(" KeyFilePassword = <secret>");
447 puts(" KeyFilePassword = ");
448 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
450 ports_puts(&Conf_SSLOptions.ListenPorts);
456 for( i = 0; i < MAX_SERVERS; i++ ) {
457 if( ! Conf_Server[i].name[0] ) continue;
459 /* Valid "Server" section */
461 printf( " Name = %s\n", Conf_Server[i].name );
462 printf( " Host = %s\n", Conf_Server[i].host );
463 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
465 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
467 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
468 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
469 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
470 printf( " Group = %d\n", Conf_Server[i].group );
471 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
474 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
475 predef_chan = array_start(&Conf_Channels);
477 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
478 if (!predef_chan->name[0])
481 /* Valid "Channel" section */
483 printf(" Name = %s\n", predef_chan->name);
484 printf(" Modes = %s\n", predef_chan->modes);
485 printf(" Key = %s\n", predef_chan->key);
486 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
487 printf(" Topic = %s\n", predef_chan->topic);
488 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
491 return (config_valid ? 0 : 1);
495 * Remove connection information from configured server.
497 * If the server is set as "once", delete it from our configuration;
498 * otherwise set the time for the next connection attempt.
500 * Non-server connections will be silently ignored.
503 Conf_UnsetServer( CONN_ID Idx )
508 /* Check all our configured servers */
509 for( i = 0; i < MAX_SERVERS; i++ ) {
510 if( Conf_Server[i].conn_id != Idx ) continue;
512 /* Gotcha! Mark server configuration as "unused": */
513 Conf_Server[i].conn_id = NONE;
515 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
516 /* Delete configuration here */
517 Init_Server_Struct( &Conf_Server[i] );
519 /* Set time for next connect attempt */
521 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
522 /* The connection has been "long", so we don't
523 * require the next attempt to be delayed. */
524 Conf_Server[i].lasttry =
525 t - Conf_ConnectRetry + RECONNECT_DELAY;
527 /* "Short" connection, enforce "ConnectRetry"
528 * but randomize it a little bit: 15 seconds. */
529 Conf_Server[i].lasttry =
530 #ifdef HAVE_ARC4RANDOM
531 t + (arc4random() % 15);
533 t + rand() / (RAND_MAX / 15);
541 * Set connection information for specified configured server.
544 Conf_SetServer( int ConfServer, CONN_ID Idx )
546 assert( ConfServer > NONE );
547 assert( Idx > NONE );
549 if (Conf_Server[ConfServer].conn_id > NONE &&
550 Conf_Server[ConfServer].conn_id != Idx) {
552 "Connection %d: Server configuration of \"%s\" already in use by connection %d!",
553 Idx, Conf_Server[ConfServer].name,
554 Conf_Server[ConfServer].conn_id);
555 Conn_Close(Idx, NULL, "Server configuration already in use", true);
558 Conf_Server[ConfServer].conn_id = Idx;
563 * Get index of server in configuration structure.
566 Conf_GetServer( CONN_ID Idx )
570 assert( Idx > NONE );
572 for( i = 0; i < MAX_SERVERS; i++ ) {
573 if( Conf_Server[i].conn_id == Idx ) return i;
579 * Enable a server by name and adjust its port number.
581 * @returns true if a server has been enabled and now has a valid port
582 * number and host name for outgoing connections.
585 Conf_EnableServer( const char *Name, UINT16 Port )
589 assert( Name != NULL );
590 for( i = 0; i < MAX_SERVERS; i++ ) {
591 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
592 /* Gotcha! Set port and enable server: */
593 Conf_Server[i].port = Port;
594 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
595 return (Conf_Server[i].port && Conf_Server[i].host[0]);
602 * Enable a server by name.
604 * The server is only usable as outgoing server, if it has set a valid port
605 * number for outgoing connections!
606 * If not, you have to use Conf_EnableServer() function to make it available.
608 * @returns true if a server has been enabled; false otherwise.
611 Conf_EnablePassiveServer(const char *Name)
615 assert( Name != NULL );
616 for (i = 0; i < MAX_SERVERS; i++) {
617 if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
618 && (Conf_Server[i].port > 0)) {
619 /* BINGO! Enable server */
620 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
621 Conf_Server[i].lasttry = 0;
629 * Disable a server by name.
630 * An already established connection will be disconnected.
632 * @returns true if a server was found and has been disabled.
635 Conf_DisableServer( const char *Name )
639 assert( Name != NULL );
640 for( i = 0; i < MAX_SERVERS; i++ ) {
641 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
642 /* Gotcha! Disable and disconnect server: */
643 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
644 if( Conf_Server[i].conn_id > NONE )
645 Conn_Close(Conf_Server[i].conn_id, NULL,
646 "Server link terminated on operator request",
655 * Add a new remote server to our configuration.
657 * @param Name Name of the new server.
658 * @param Port Port number to connect to or 0 for incoming connections.
659 * @param Host Host name to connect to.
660 * @param MyPwd Password that will be sent to the peer.
661 * @param PeerPwd Password that must be received from the peer.
662 * @returns true if the new server has been added; false otherwise.
665 Conf_AddServer(const char *Name, UINT16 Port, const char *Host,
666 const char *MyPwd, const char *PeerPwd)
670 assert( Name != NULL );
671 assert( Host != NULL );
672 assert( MyPwd != NULL );
673 assert( PeerPwd != NULL );
675 /* Search unused item in server configuration structure */
676 for( i = 0; i < MAX_SERVERS; i++ ) {
677 /* Is this item used? */
678 if( ! Conf_Server[i].name[0] ) break;
680 if( i >= MAX_SERVERS ) return false;
682 Init_Server_Struct( &Conf_Server[i] );
683 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
684 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
685 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
686 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
687 Conf_Server[i].port = Port;
688 Conf_Server[i].flags = CONF_SFLAG_ONCE;
694 * Check if the given nickname is reserved for services on a particular server.
696 * @param ConfServer The server index to check.
697 * @param Nick The nickname to check.
698 * @returns true if the given nickname belongs to an "IRC service".
701 Conf_NickIsService(int ConfServer, const char *Nick)
703 assert (ConfServer >= 0);
704 assert (ConfServer < MAX_SERVERS);
706 return MatchCaseInsensitiveList(Conf_Server[ConfServer].svs_mask,
711 * Check if the given nickname is blocked for "normal client" use.
713 * @param ConfServer The server index or NONE to check all configured servers.
714 * @param Nick The nickname to check.
715 * @returns true if the given nickname belongs to an "IRC service".
718 Conf_NickIsBlocked(const char *Nick)
722 for(i = 0; i < MAX_SERVERS; i++) {
723 if (!Conf_Server[i].name[0])
725 if (Conf_NickIsService(i, Nick))
732 * Initialize configuration settings with their default values.
735 Set_Defaults(bool InitServers)
738 char random[RANDOM_SALT_LEN + 1];
741 strcpy(Conf_ServerName, "");
742 strcpy(Conf_ServerAdmin1, "");
743 strcpy(Conf_ServerAdmin2, "");
744 strcpy(Conf_ServerAdminMail, "");
745 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
746 PACKAGE_NAME, PACKAGE_VERSION);
747 strcpy(Conf_Network, "");
748 free(Conf_ListenAddress);
749 Conf_ListenAddress = NULL;
750 array_free(&Conf_ListenPorts);
751 array_free(&Conf_Motd);
752 array_free(&Conf_Helptext);
753 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
754 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
755 strlcpy(Conf_HelpFile, DOCDIR, sizeof(Conf_HelpFile));
756 strlcat(Conf_HelpFile, HELP_FILE, sizeof(Conf_HelpFile));
757 strcpy(Conf_ServerPwd, "");
758 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
759 Conf_UID = Conf_GID = 0;
762 Conf_ConnectRetry = 60;
763 Conf_IdleTimeout = 0;
764 Conf_MaxConnections = 0;
765 Conf_MaxConnectionsIP = 5;
767 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
768 Conf_MaxListSize = 100;
769 Conf_PingTimeout = 120;
770 Conf_PongTimeout = 20;
773 strlcpy(Conf_AllowedChannelTypes, CHANTYPES,
774 sizeof(Conf_AllowedChannelTypes));
775 Conf_AllowRemoteOper = false;
777 Conf_AuthPing = false;
779 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
780 strcpy(Conf_CloakHost, "");
781 strcpy(Conf_CloakHostModeX, "");
782 strlcpy(Conf_CloakHostSalt, ngt_RandomStr(random, RANDOM_SALT_LEN),
783 sizeof(Conf_CloakHostSalt));
784 Conf_CloakUserToNick = false;
785 Conf_ConnectIPv4 = true;
787 Conf_ConnectIPv6 = true;
789 Conf_ConnectIPv6 = false;
791 strcpy(Conf_DefaultUserModes, "");
798 strcpy(Conf_IncludeDir, "");
799 Conf_MorePrivacy = false;
800 Conf_NoticeBeforeRegistration = false;
801 Conf_OperCanMode = false;
802 Conf_OperChanPAutoOp = true;
803 Conf_OperServerMode = false;
809 Conf_PAMIsOptional = false;
810 Conf_ScrubCTCP = false;
813 Conf_SyslogFacility = LOG_LOCAL5;
815 Conf_SyslogFacility = 0;
819 /* Initialize server configuration structures */
821 for (i = 0; i < MAX_SERVERS;
822 Init_Server_Struct(&Conf_Server[i++]));
827 * Get number of configured listening ports.
829 * @returns The number of ports (IPv4+IPv6) on which the server should listen.
834 size_t cnt = array_bytes(&Conf_ListenPorts);
836 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
842 * Read contents of a text file into an array.
844 * This function is used to read the MOTD and help text file, for example.
846 * @param filename Name of the file to read.
847 * @return true, when the file has been read in.
850 Read_TextFile(const char *Filename, const char *Name, array *Destination)
856 if (*Filename == '\0')
859 fp = fopen(Filename, "r");
861 Config_Error(LOG_ERR, "Can't read %s file \"%s\": %s",
862 Name, Filename, strerror(errno));
866 array_free(Destination);
867 while (fgets(line, (int)sizeof line, fp)) {
868 ngt_TrimLastChr(line, '\n');
870 /* add text including \0 */
871 if (!array_catb(Destination, line, strlen(line) + 1)) {
872 Log(LOG_ERR, "Cannot read/add \"%s\", line %d: %s",
873 Filename, line_no, strerror(errno));
883 * Read ngIRCd configuration file.
885 * Please note that this function uses exit(1) on fatal errors and therefore
886 * can result in ngIRCd terminating!
888 * @param ngircd_starting Flag indicating if ngIRCd is starting or not.
889 * @returns true when the configuration file has been read
890 * successfully; false otherwise.
893 Read_Config(bool TestOnly, bool IsStarting)
895 const UINT16 defaultport = 6667;
896 char *ptr, file[FNAME_LEN];
897 struct dirent *entry;
902 /* Open configuration file */
903 fd = fopen( NGIRCd_ConfFile, "r" );
905 /* No configuration file found! */
906 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
907 NGIRCd_ConfFile, strerror( errno ));
910 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
915 Set_Defaults(IsStarting);
918 Config_Error(LOG_INFO,
919 "Reading configuration from \"%s\" ...",
922 /* Clean up server configuration structure: mark all already
923 * configured servers as "once" so that they are deleted
924 * after the next disconnect and delete all unused servers.
925 * And delete all servers which are "duplicates" of servers
926 * that are already marked as "once" (such servers have been
927 * created by the last rehash but are now useless). */
928 for( i = 0; i < MAX_SERVERS; i++ ) {
929 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
931 /* This structure is in use ... */
932 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
933 /* Check for duplicates */
934 for( n = 0; n < MAX_SERVERS; n++ ) {
935 if( n == i ) continue;
937 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
938 Init_Server_Struct( &Conf_Server[n] );
940 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
946 /* Mark server as "once" */
947 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
948 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
953 /* Initialize variables */
954 Init_Server_Struct( &New_Server );
955 New_Server_Idx = NONE;
960 Read_Config_File(NGIRCd_ConfFile, fd);
963 if (Conf_IncludeDir[0]) {
964 dh = opendir(Conf_IncludeDir);
966 Config_Error(LOG_ALERT,
967 "Can't open include directory \"%s\": %s",
968 Conf_IncludeDir, strerror(errno));
970 strlcpy(Conf_IncludeDir, SYSCONFDIR, sizeof(Conf_IncludeDir));
971 strlcat(Conf_IncludeDir, CONFIG_DIR, sizeof(Conf_IncludeDir));
972 dh = opendir(Conf_IncludeDir);
975 /* Include further configuration files, if IncludeDir is available */
977 while ((entry = readdir(dh)) != NULL) {
978 ptr = strrchr(entry->d_name, '.');
979 if (!ptr || strcasecmp(ptr, ".conf") != 0)
981 snprintf(file, sizeof(file), "%s/%s",
982 Conf_IncludeDir, entry->d_name);
984 Config_Error(LOG_INFO,
985 "Reading configuration from \"%s\" ...",
987 fd = fopen(file, "r");
989 Read_Config_File(file, fd);
992 Config_Error(LOG_ALERT,
993 "Can't read configuration \"%s\": %s",
994 file, strerror(errno));
999 /* Check if there is still a server to add */
1000 if( New_Server.name[0] ) {
1001 /* Copy data to "real" server structure */
1002 assert( New_Server_Idx > NONE );
1003 Conf_Server[New_Server_Idx] = New_Server;
1006 /* not a single listening port? Add default. */
1007 if (no_listenports() &&
1008 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
1010 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
1011 (unsigned int) defaultport, strerror(errno));
1016 if (!Conf_ListenAddress)
1017 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
1019 if (!Conf_ListenAddress) {
1020 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
1024 /* No MOTD phrase configured? (re)try motd file. */
1025 if (array_bytes(&Conf_Motd) == 0) {
1026 if (Read_TextFile(Conf_MotdFile, "MOTD", &Conf_Motd))
1027 Using_MotdFile = true;
1030 /* Try to read ngIRCd help text file. */
1031 (void)Read_TextFile(Conf_HelpFile, "help text", &Conf_Helptext);
1032 if (!array_bytes(&Conf_Helptext))
1033 Config_Error(LOG_WARNING,
1034 "No help text available, HELP command will be of limited use.");
1037 /* Make sure that all SSL-related files are readable */
1038 CheckFileReadable("CertFile", Conf_SSLOptions.CertFile);
1039 CheckFileReadable("DHFile", Conf_SSLOptions.DHFile);
1040 CheckFileReadable("KeyFile", Conf_SSLOptions.KeyFile);
1042 /* Set the default ciphers if none were configured */
1043 if (!Conf_SSLOptions.CipherList)
1044 Conf_SSLOptions.CipherList = strdup_warn(DEFAULT_CIPHERS);
1051 * Read in and handle a configuration file.
1053 * @param File Name of the configuration file.
1054 * @param fd File descriptor already opened for reading.
1057 Read_Config_File(const char *File, FILE *fd)
1059 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
1063 /* Read configuration file */
1066 if (!fgets(str, sizeof(str), fd))
1071 /* Skip comments and empty lines */
1072 if (str[0] == ';' || str[0] == '#' || str[0] == '\0')
1075 if (strlen(str) >= sizeof(str) - 1) {
1076 Config_Error(LOG_WARNING, "%s, line %d too long!",
1081 /* Is this the beginning of a new section? */
1082 if ((str[0] == '[') && (str[strlen(str) - 1] == ']')) {
1083 strlcpy(section, str, sizeof(section));
1084 if (strcasecmp(section, "[GLOBAL]") == 0
1085 || strcasecmp(section, "[LIMITS]") == 0
1086 || strcasecmp(section, "[OPTIONS]") == 0
1088 || strcasecmp(section, "[SSL]") == 0
1093 if (strcasecmp(section, "[SERVER]") == 0) {
1094 /* Check if there is already a server to add */
1095 if (New_Server.name[0]) {
1096 /* Copy data to "real" server structure */
1097 assert(New_Server_Idx > NONE);
1098 Conf_Server[New_Server_Idx] =
1102 /* Re-init structure for new server */
1103 Init_Server_Struct(&New_Server);
1105 /* Search unused item in server configuration structure */
1106 for (i = 0; i < MAX_SERVERS; i++) {
1107 /* Is this item used? */
1108 if (!Conf_Server[i].name[0])
1111 if (i >= MAX_SERVERS) {
1112 /* Oops, no free item found! */
1113 Config_Error(LOG_ERR,
1114 "Too many servers configured.");
1115 New_Server_Idx = NONE;
1121 if (strcasecmp(section, "[CHANNEL]") == 0) {
1122 count = array_length(&Conf_Channels,
1127 sizeof(struct Conf_Channel), count)) {
1128 Config_Error(LOG_ERR,
1129 "Could not allocate memory for new operator (line %d)",
1135 if (strcasecmp(section, "[OPERATOR]") == 0) {
1136 count = array_length(&Conf_Opers,
1137 sizeof(struct Conf_Oper));
1138 if (!array_alloc(&Conf_Opers,
1139 sizeof(struct Conf_Oper),
1141 Config_Error(LOG_ERR,
1142 "Could not allocate memory for new channel (line &d)",
1148 Config_Error(LOG_ERR,
1149 "%s, line %d: Unknown section \"%s\"!",
1150 File, line, section);
1153 if (section[0] == 0x1)
1156 /* Split line into variable name and parameters */
1157 ptr = strchr(str, '=');
1159 Config_Error(LOG_ERR, "%s, line %d: Syntax error!",
1169 if (strcasecmp(section, "[GLOBAL]") == 0)
1170 Handle_GLOBAL(File, line, var, arg);
1171 else if (strcasecmp(section, "[LIMITS]") == 0)
1172 Handle_LIMITS(File, line, var, arg);
1173 else if (strcasecmp(section, "[OPTIONS]") == 0)
1174 Handle_OPTIONS(File, line, var, arg);
1176 else if (strcasecmp(section, "[SSL]") == 0)
1177 Handle_SSL(File, line, var, arg);
1179 else if (strcasecmp(section, "[OPERATOR]") == 0)
1180 Handle_OPERATOR(File, line, var, arg);
1181 else if (strcasecmp(section, "[SERVER]") == 0)
1182 Handle_SERVER(File, line, var, arg);
1183 else if (strcasecmp(section, "[CHANNEL]") == 0)
1184 Handle_CHANNEL(File, line, var, arg);
1186 Config_Error(LOG_ERR,
1187 "%s, line %d: Variable \"%s\" outside section!",
1193 * Check whether a string argument is "true" or "false".
1195 * @param Arg Input string.
1196 * @returns true if the input string has been parsed as "yes", "true"
1197 * (case insensitive) or a non-zero integer value.
1200 Check_ArgIsTrue(const char *Arg)
1202 if (strcasecmp(Arg, "yes") == 0)
1204 if (strcasecmp(Arg, "true") == 0)
1213 * Handle setting of "MaxNickLength".
1215 * @param Line Line number in configuration file.
1216 * @raram Arg Input string.
1217 * @returns New configured maximum nickname length.
1220 Handle_MaxNickLength(const char *File, int Line, const char *Arg)
1224 new = (unsigned) atoi(Arg) + 1;
1225 if (new > CLIENT_NICK_LEN) {
1226 Config_Error(LOG_WARNING,
1227 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
1228 File, Line, CLIENT_NICK_LEN - 1);
1229 return CLIENT_NICK_LEN;
1232 Config_Error(LOG_WARNING,
1233 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
1241 * Output a warning messages if IDENT is configured but not compiled in.
1244 WarnIdent(const char UNUSED *File, int UNUSED Line)
1248 /* user has enabled ident lookups explicitly, but ... */
1249 Config_Error(LOG_WARNING,
1250 "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
1257 * Output a warning messages if IPv6 is configured but not compiled in.
1260 WarnIPv6(const char UNUSED *File, int UNUSED Line)
1263 if (Conf_ConnectIPv6) {
1264 /* user has enabled IPv6 explicitly, but ... */
1265 Config_Error(LOG_WARNING,
1266 "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
1273 * Output a warning messages if PAM is configured but not compiled in.
1276 WarnPAM(const char UNUSED *File, int UNUSED Line)
1280 Config_Error(LOG_WARNING,
1281 "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
1288 * Handle legacy "NoXXX" options in [GLOBAL] section.
1290 * TODO: This function and support for "NoXXX" could be removed starting
1291 * with ngIRCd release 19 (one release after marking it "deprecated").
1293 * @param Var Variable name.
1294 * @param Arg Argument string.
1295 * @returns true if a NoXXX option has been processed; false otherwise.
1298 CheckLegacyNoOption(const char *Var, const char *Arg)
1300 if(strcasecmp(Var, "NoDNS") == 0) {
1301 Conf_DNS = !Check_ArgIsTrue( Arg );
1304 if (strcasecmp(Var, "NoIdent") == 0) {
1305 Conf_Ident = !Check_ArgIsTrue(Arg);
1308 if(strcasecmp(Var, "NoPAM") == 0) {
1309 Conf_PAM = !Check_ArgIsTrue(Arg);
1316 * Handle deprecated legacy options in [GLOBAL] section.
1318 * TODO: This function and support for these options in the [Global] section
1319 * could be removed starting with ngIRCd release 19 (one release after
1320 * marking it "deprecated").
1322 * @param Var Variable name.
1323 * @param Arg Argument string.
1324 * @returns true if a legacy option has been processed; false otherwise.
1327 CheckLegacyGlobalOption(const char *File, int Line, char *Var, char *Arg)
1329 if (strcasecmp(Var, "AllowRemoteOper") == 0
1330 || strcasecmp(Var, "ChrootDir") == 0
1331 || strcasecmp(Var, "ConnectIPv4") == 0
1332 || strcasecmp(Var, "ConnectIPv6") == 0
1333 || strcasecmp(Var, "OperCanUseMode") == 0
1334 || strcasecmp(Var, "OperChanPAutoOp") == 0
1335 || strcasecmp(Var, "OperServerMode") == 0
1336 || strcasecmp(Var, "PredefChannelsOnly") == 0
1337 || strcasecmp(Var, "SyslogFacility") == 0
1338 || strcasecmp(Var, "WebircPassword") == 0) {
1339 Handle_OPTIONS(File, Line, Var, Arg);
1342 if (strcasecmp(Var, "ConnectRetry") == 0
1343 || strcasecmp(Var, "IdleTimeout") == 0
1344 || strcasecmp(Var, "MaxConnections") == 0
1345 || strcasecmp(Var, "MaxConnectionsIP") == 0
1346 || strcasecmp(Var, "MaxJoins") == 0
1347 || strcasecmp(Var, "MaxNickLength") == 0
1348 || strcasecmp(Var, "PingTimeout") == 0
1349 || strcasecmp(Var, "PongTimeout") == 0) {
1350 Handle_LIMITS(File, Line, Var, Arg);
1354 if (strcasecmp(Var, "SSLCertFile") == 0
1355 || strcasecmp(Var, "SSLDHFile") == 0
1356 || strcasecmp(Var, "SSLKeyFile") == 0
1357 || strcasecmp(Var, "SSLKeyFilePassword") == 0
1358 || strcasecmp(Var, "SSLPorts") == 0) {
1359 Handle_SSL(File, Line, Var + 3, Arg);
1368 * Strip "no" prefix of a string.
1370 * TODO: This function and support for "NoXXX" should be removed starting
1371 * with ngIRCd release 19! (One release after marking it "deprecated").
1373 * @param str Pointer to input string starting with "no".
1374 * @returns New pointer to string without "no" prefix.
1377 NoNo(const char *str)
1379 assert(strncasecmp("no", str, 2) == 0 && str[2]);
1384 * Invert "boolean" string.
1386 * TODO: This function and support for "NoXXX" should be removed starting
1387 * with ngIRCd release 19! (One release after marking it "deprecated").
1389 * @param arg "Boolean" input string.
1390 * @returns Pointer to inverted "boolean string".
1393 InvertArg(const char *arg)
1395 return yesno_to_str(!Check_ArgIsTrue(arg));
1399 * Handle variable in [Global] configuration section.
1401 * @param Line Line numer in configuration file.
1402 * @param Var Variable name.
1403 * @param Arg Variable argument.
1406 Handle_GLOBAL(const char *File, int Line, char *Var, char *Arg )
1411 const char *section;
1414 assert(File != NULL);
1416 assert(Var != NULL);
1417 assert(Arg != NULL);
1419 if (strcasecmp(Var, "Name") == 0) {
1420 len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
1421 if (len >= sizeof(Conf_ServerName))
1422 Config_Error_TooLong(File, Line, Var);
1425 if (strcasecmp(Var, "AdminInfo1") == 0) {
1426 len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
1427 if (len >= sizeof(Conf_ServerAdmin1))
1428 Config_Error_TooLong(File, Line, Var);
1431 if (strcasecmp(Var, "AdminInfo2") == 0) {
1432 len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
1433 if (len >= sizeof(Conf_ServerAdmin2))
1434 Config_Error_TooLong(File, Line, Var);
1437 if (strcasecmp(Var, "AdminEMail") == 0) {
1438 len = strlcpy(Conf_ServerAdminMail, Arg,
1439 sizeof(Conf_ServerAdminMail));
1440 if (len >= sizeof(Conf_ServerAdminMail))
1441 Config_Error_TooLong(File, Line, Var);
1444 if (strcasecmp(Var, "Info") == 0) {
1445 len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
1446 if (len >= sizeof(Conf_ServerInfo))
1447 Config_Error_TooLong(File, Line, Var);
1450 if (strcasecmp(Var, "HelpFile") == 0) {
1451 len = strlcpy(Conf_HelpFile, Arg, sizeof(Conf_HelpFile));
1452 if (len >= sizeof(Conf_HelpFile))
1453 Config_Error_TooLong(File, Line, Var);
1456 if (strcasecmp(Var, "Listen") == 0) {
1457 if (Conf_ListenAddress) {
1458 Config_Error(LOG_ERR,
1459 "Multiple Listen= options, ignoring: %s",
1463 Conf_ListenAddress = strdup_warn(Arg);
1464 /* If allocation fails, we're in trouble: we cannot ignore the
1465 * error -- otherwise ngircd would listen on all interfaces. */
1466 if (!Conf_ListenAddress) {
1467 Config_Error(LOG_ALERT,
1468 "%s exiting due to fatal errors!",
1474 if (strcasecmp(Var, "MotdFile") == 0) {
1475 len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
1476 if (len >= sizeof(Conf_MotdFile))
1477 Config_Error_TooLong(File, Line, Var);
1480 if (strcasecmp(Var, "MotdPhrase") == 0) {
1485 Config_Error_TooLong(File, Line, Var);
1488 if (!array_copyb(&Conf_Motd, Arg, len + 1))
1489 Config_Error(LOG_WARNING,
1490 "%s, line %d: Could not append MotdPhrase: %s",
1491 File, Line, strerror(errno));
1492 Using_MotdFile = false;
1495 if (strcasecmp(Var, "Network") == 0) {
1496 len = strlcpy(Conf_Network, Arg, sizeof(Conf_Network));
1497 if (len >= sizeof(Conf_Network))
1498 Config_Error_TooLong(File, Line, Var);
1499 ptr = strchr(Conf_Network, ' ');
1501 Config_Error(LOG_WARNING,
1502 "%s, line %d: \"Network\" can't contain spaces!",
1508 if(strcasecmp(Var, "Password") == 0) {
1509 len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
1510 if (len >= sizeof(Conf_ServerPwd))
1511 Config_Error_TooLong(File, Line, Var);
1514 if (strcasecmp(Var, "PidFile") == 0) {
1515 len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
1516 if (len >= sizeof(Conf_PidFile))
1517 Config_Error_TooLong(File, Line, Var);
1520 if (strcasecmp(Var, "Ports") == 0) {
1521 ports_parse(&Conf_ListenPorts, File, Line, Arg);
1524 if (strcasecmp(Var, "ServerGID") == 0) {
1525 grp = getgrnam(Arg);
1527 Conf_GID = grp->gr_gid;
1529 Conf_GID = (unsigned int)atoi(Arg);
1530 if (!Conf_GID && strcmp(Arg, "0"))
1531 Config_Error(LOG_WARNING,
1532 "%s, line %d: Value of \"%s\" is not a valid group name or ID!",
1537 if (strcasecmp(Var, "ServerUID") == 0) {
1538 pwd = getpwnam(Arg);
1540 Conf_UID = pwd->pw_uid;
1542 Conf_UID = (unsigned int)atoi(Arg);
1543 if (!Conf_UID && strcmp(Arg, "0"))
1544 Config_Error(LOG_WARNING,
1545 "%s, line %d: Value of \"%s\" is not a valid user name or ID!",
1551 if (CheckLegacyNoOption(Var, Arg)) {
1552 /* TODO: This function and support for "NoXXX" could be
1553 * be removed starting with ngIRCd release 19 (one release
1554 * after marking it "deprecated"). */
1555 Config_Error(LOG_WARNING,
1556 "%s, line %d (section \"Global\"): \"No\"-Prefix is deprecated, use \"%s = %s\" in [Options] section!",
1557 File, Line, NoNo(Var), InvertArg(Arg));
1558 if (strcasecmp(Var, "NoIdent") == 0)
1559 WarnIdent(File, Line);
1560 else if (strcasecmp(Var, "NoPam") == 0)
1561 WarnPAM(File, Line);
1564 if ((section = CheckLegacyGlobalOption(File, Line, Var, Arg))) {
1565 /** TODO: This function and support for these options in the
1566 * [Global] section could be removed starting with ngIRCd
1567 * release 19 (one release after marking it "deprecated"). */
1568 if (strncasecmp(Var, "SSL", 3) == 0) {
1569 Config_Error(LOG_WARNING,
1570 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s and rename to \"%s\"!",
1571 File, Line, Var, section,
1574 Config_Error(LOG_WARNING,
1575 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s!",
1576 File, Line, Var, section);
1581 Config_Error_Section(File, Line, Var, "Global");
1585 * Handle variable in [Limits] configuration section.
1587 * @param Line Line numer in configuration file.
1588 * @param Var Variable name.
1589 * @param Arg Variable argument.
1592 Handle_LIMITS(const char *File, int Line, char *Var, char *Arg)
1594 assert(File != NULL);
1596 assert(Var != NULL);
1597 assert(Arg != NULL);
1599 if (strcasecmp(Var, "ConnectRetry") == 0) {
1600 Conf_ConnectRetry = atoi(Arg);
1601 if (Conf_ConnectRetry < 5) {
1602 Config_Error(LOG_WARNING,
1603 "%s, line %d: Value of \"ConnectRetry\" too low!",
1605 Conf_ConnectRetry = 5;
1609 if (strcasecmp(Var, "IdleTimeout") == 0) {
1610 Conf_IdleTimeout = atoi(Arg);
1611 if (!Conf_IdleTimeout && strcmp(Arg, "0"))
1612 Config_Error_NaN(File, Line, Var);
1615 if (strcasecmp(Var, "MaxConnections") == 0) {
1616 Conf_MaxConnections = atoi(Arg);
1617 if (!Conf_MaxConnections && strcmp(Arg, "0"))
1618 Config_Error_NaN(File, Line, Var);
1621 if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
1622 Conf_MaxConnectionsIP = atoi(Arg);
1623 if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
1624 Config_Error_NaN(File, Line, Var);
1627 if (strcasecmp(Var, "MaxJoins") == 0) {
1628 Conf_MaxJoins = atoi(Arg);
1629 if (!Conf_MaxJoins && strcmp(Arg, "0"))
1630 Config_Error_NaN(File, Line, Var);
1633 if (strcasecmp(Var, "MaxNickLength") == 0) {
1634 Conf_MaxNickLength = Handle_MaxNickLength(File, Line, Arg);
1637 if (strcasecmp(Var, "MaxListSize") == 0) {
1638 Conf_MaxListSize = atoi(Arg);
1639 if (!Conf_MaxListSize && strcmp(Arg, "0"))
1640 Config_Error_NaN(File, Line, Var);
1643 if (strcasecmp(Var, "PingTimeout") == 0) {
1644 Conf_PingTimeout = atoi(Arg);
1645 if (Conf_PingTimeout < 5) {
1646 Config_Error(LOG_WARNING,
1647 "%s, line %d: Value of \"PingTimeout\" too low!",
1649 Conf_PingTimeout = 5;
1653 if (strcasecmp(Var, "PongTimeout") == 0) {
1654 Conf_PongTimeout = atoi(Arg);
1655 if (Conf_PongTimeout < 5) {
1656 Config_Error(LOG_WARNING,
1657 "%s, line %d: Value of \"PongTimeout\" too low!",
1659 Conf_PongTimeout = 5;
1664 Config_Error_Section(File, Line, Var, "Limits");
1668 * Handle variable in [Options] configuration section.
1670 * @param Line Line numer in configuration file.
1671 * @param Var Variable name.
1672 * @param Arg Variable argument.
1675 Handle_OPTIONS(const char *File, int Line, char *Var, char *Arg)
1680 assert(File != NULL);
1682 assert(Var != NULL);
1683 assert(Arg != NULL);
1685 if (strcasecmp(Var, "AllowedChannelTypes") == 0) {
1687 Conf_AllowedChannelTypes[0] = '\0';
1689 if (strchr(Conf_AllowedChannelTypes, *p)) {
1690 /* Prefix is already included; ignore it */
1695 if (strchr(CHANTYPES, *p)) {
1696 len = strlen(Conf_AllowedChannelTypes) + 1;
1697 assert(len < sizeof(Conf_AllowedChannelTypes));
1698 Conf_AllowedChannelTypes[len - 1] = *p;
1699 Conf_AllowedChannelTypes[len] = '\0';
1701 Config_Error(LOG_WARNING,
1702 "%s, line %d: Unknown channel prefix \"%c\" in \"AllowedChannelTypes\"!",
1709 if (strcasecmp(Var, "AllowRemoteOper") == 0) {
1710 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1713 if (strcasecmp(Var, "ChrootDir") == 0) {
1714 len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
1715 if (len >= sizeof(Conf_Chroot))
1716 Config_Error_TooLong(File, Line, Var);
1719 if (strcasecmp(Var, "CloakHost") == 0) {
1720 len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
1721 if (len >= sizeof(Conf_CloakHost))
1722 Config_Error_TooLong(File, Line, Var);
1725 if (strcasecmp(Var, "CloakHostModeX") == 0) {
1726 len = strlcpy(Conf_CloakHostModeX, Arg, sizeof(Conf_CloakHostModeX));
1727 if (len >= sizeof(Conf_CloakHostModeX))
1728 Config_Error_TooLong(File, Line, Var);
1731 if (strcasecmp(Var, "CloakHostSalt") == 0) {
1732 len = strlcpy(Conf_CloakHostSalt, Arg, sizeof(Conf_CloakHostSalt));
1733 if (len >= sizeof(Conf_CloakHostSalt))
1734 Config_Error_TooLong(File, Line, Var);
1737 if (strcasecmp(Var, "CloakUserToNick") == 0) {
1738 Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
1741 if (strcasecmp(Var, "ConnectIPv6") == 0) {
1742 Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
1743 WarnIPv6(File, Line);
1746 if (strcasecmp(Var, "ConnectIPv4") == 0) {
1747 Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
1750 if (strcasecmp(Var, "DefaultUserModes") == 0) {
1752 Conf_DefaultUserModes[0] = '\0';
1754 if (strchr(Conf_DefaultUserModes, *p)) {
1755 /* Mode is already included; ignore it */
1760 if (strchr(USERMODES, *p)) {
1761 len = strlen(Conf_DefaultUserModes) + 1;
1762 assert(len < sizeof(Conf_DefaultUserModes));
1763 Conf_DefaultUserModes[len - 1] = *p;
1764 Conf_DefaultUserModes[len] = '\0';
1766 Config_Error(LOG_WARNING,
1767 "%s, line %d: Unknown user mode \"%c\" in \"DefaultUserModes\"!",
1774 if (strcasecmp(Var, "DNS") == 0) {
1775 Conf_DNS = Check_ArgIsTrue(Arg);
1778 if (strcasecmp(Var, "Ident") == 0) {
1779 Conf_Ident = Check_ArgIsTrue(Arg);
1780 WarnIdent(File, Line);
1783 if (strcasecmp(Var, "IncludeDir") == 0) {
1784 if (Conf_IncludeDir[0]) {
1785 Config_Error(LOG_ERR,
1786 "%s, line %d: Can't overwrite value of \"IncludeDir\" variable!",
1790 len = strlcpy(Conf_IncludeDir, Arg, sizeof(Conf_IncludeDir));
1791 if (len >= sizeof(Conf_IncludeDir))
1792 Config_Error_TooLong(File, Line, Var);
1795 if (strcasecmp(Var, "MorePrivacy") == 0) {
1796 Conf_MorePrivacy = Check_ArgIsTrue(Arg);
1799 if (strcasecmp(Var, "NoticeAuth") == 0) {
1801 * TODO: This section and support for "NoticeAuth" variable
1802 * could be removed starting with ngIRCd release 24 (one
1803 * release after marking it "deprecated") ...
1805 Config_Error(LOG_WARNING,
1806 "%s, line %d (section \"Options\"): \"%s\" is deprecated, please use \"NoticeBeforeRegistration\"!",
1808 Conf_NoticeBeforeRegistration = Check_ArgIsTrue(Arg);
1811 if (strcasecmp(Var, "NoticeBeforeRegistration") == 0) {
1812 Conf_NoticeBeforeRegistration = Check_ArgIsTrue(Arg);
1815 if (strcasecmp(Var, "OperCanUseMode") == 0) {
1816 Conf_OperCanMode = Check_ArgIsTrue(Arg);
1819 if (strcasecmp(Var, "OperChanPAutoOp") == 0) {
1820 Conf_OperChanPAutoOp = Check_ArgIsTrue(Arg);
1823 if (strcasecmp(Var, "OperServerMode") == 0) {
1824 Conf_OperServerMode = Check_ArgIsTrue(Arg);
1827 if (strcasecmp(Var, "PAM") == 0) {
1828 Conf_PAM = Check_ArgIsTrue(Arg);
1829 WarnPAM(File, Line);
1832 if (strcasecmp(Var, "PAMIsOptional") == 0 ) {
1833 Conf_PAMIsOptional = Check_ArgIsTrue(Arg);
1836 if (strcasecmp(Var, "PredefChannelsOnly") == 0) {
1838 * TODO: This section and support for "PredefChannelsOnly"
1839 * could be removed starting with ngIRCd release 22 (one
1840 * release after marking it "deprecated") ...
1842 Config_Error(LOG_WARNING,
1843 "%s, line %d (section \"Options\"): \"%s\" is deprecated, please use \"AllowedChannelTypes\"!",
1845 if (Check_ArgIsTrue(Arg))
1846 Conf_AllowedChannelTypes[0] = '\0';
1848 strlcpy(Conf_AllowedChannelTypes, CHANTYPES,
1849 sizeof(Conf_AllowedChannelTypes));
1853 if (strcasecmp(Var, "RequireAuthPing") == 0) {
1854 Conf_AuthPing = Check_ArgIsTrue(Arg);
1858 if (strcasecmp(Var, "ScrubCTCP") == 0) {
1859 Conf_ScrubCTCP = Check_ArgIsTrue(Arg);
1863 if (strcasecmp(Var, "SyslogFacility") == 0) {
1864 Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
1865 Conf_SyslogFacility);
1869 if (strcasecmp(Var, "WebircPassword") == 0) {
1870 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
1871 if (len >= sizeof(Conf_WebircPwd))
1872 Config_Error_TooLong(File, Line, Var);
1876 Config_Error_Section(File, Line, Var, "Options");
1882 * Handle variable in [SSL] configuration section.
1884 * @param Line Line numer in configuration file.
1885 * @param Var Variable name.
1886 * @param Arg Variable argument.
1889 Handle_SSL(const char *File, int Line, char *Var, char *Arg)
1891 assert(File != NULL);
1893 assert(Var != NULL);
1894 assert(Arg != NULL);
1896 if (strcasecmp(Var, "CertFile") == 0) {
1897 assert(Conf_SSLOptions.CertFile == NULL);
1898 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1901 if (strcasecmp(Var, "DHFile") == 0) {
1902 assert(Conf_SSLOptions.DHFile == NULL);
1903 Conf_SSLOptions.DHFile = strdup_warn(Arg);
1906 if (strcasecmp(Var, "KeyFile") == 0) {
1907 assert(Conf_SSLOptions.KeyFile == NULL);
1908 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1911 if (strcasecmp(Var, "KeyFilePassword") == 0) {
1912 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1913 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1914 Config_Error(LOG_ERR,
1915 "%s, line %d (section \"SSL\"): Could not copy %s: %s!",
1916 File, Line, Var, strerror(errno));
1919 if (strcasecmp(Var, "Ports") == 0) {
1920 ports_parse(&Conf_SSLOptions.ListenPorts, File, Line, Arg);
1923 if (strcasecmp(Var, "CipherList") == 0) {
1924 assert(Conf_SSLOptions.CipherList == NULL);
1925 Conf_SSLOptions.CipherList = strdup_warn(Arg);
1929 Config_Error_Section(File, Line, Var, "SSL");
1935 * Handle variable in [Operator] configuration section.
1937 * @param Line Line numer in configuration file.
1938 * @param Var Variable name.
1939 * @param Arg Variable argument.
1942 Handle_OPERATOR(const char *File, int Line, char *Var, char *Arg )
1945 struct Conf_Oper *op;
1947 assert( File != NULL );
1949 assert( Var != NULL );
1950 assert( Arg != NULL );
1952 op = array_get(&Conf_Opers, sizeof(*op),
1953 array_length(&Conf_Opers, sizeof(*op)) - 1);
1957 if (strcasecmp(Var, "Name") == 0) {
1958 /* Name of IRC operator */
1959 len = strlcpy(op->name, Arg, sizeof(op->name));
1960 if (len >= sizeof(op->name))
1961 Config_Error_TooLong(File, Line, Var);
1964 if (strcasecmp(Var, "Password") == 0) {
1965 /* Password of IRC operator */
1966 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1967 if (len >= sizeof(op->pwd))
1968 Config_Error_TooLong(File, Line, Var);
1971 if (strcasecmp(Var, "Mask") == 0) {
1973 return; /* Hostname already configured */
1974 op->mask = strdup_warn( Arg );
1978 Config_Error_Section(File, Line, Var, "Operator");
1982 * Handle variable in [Server] configuration section.
1984 * @param Line Line numer in configuration file.
1985 * @param Var Variable name.
1986 * @param Arg Variable argument.
1989 Handle_SERVER(const char *File, int Line, char *Var, char *Arg )
1994 assert( File != NULL );
1996 assert( Var != NULL );
1997 assert( Arg != NULL );
1999 /* Ignore server block if no space is left in server configuration structure */
2000 if( New_Server_Idx <= NONE ) return;
2002 if( strcasecmp( Var, "Host" ) == 0 ) {
2003 /* Hostname of the server */
2004 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
2005 if (len >= sizeof( New_Server.host ))
2006 Config_Error_TooLong(File, Line, Var);
2009 if( strcasecmp( Var, "Name" ) == 0 ) {
2010 /* Name of the server ("Nick"/"ID") */
2011 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
2012 if (len >= sizeof( New_Server.name ))
2013 Config_Error_TooLong(File, Line, Var);
2016 if (strcasecmp(Var, "Bind") == 0) {
2017 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
2020 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
2024 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
2025 /* Password of this server which is sent to the peer */
2027 Config_Error(LOG_ERR,
2028 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
2031 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
2032 if (len >= sizeof( New_Server.pwd_in ))
2033 Config_Error_TooLong(File, Line, Var);
2036 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
2037 /* Passwort of the peer which must be received */
2038 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
2039 if (len >= sizeof( New_Server.pwd_out ))
2040 Config_Error_TooLong(File, Line, Var);
2043 if( strcasecmp( Var, "Port" ) == 0 ) {
2044 /* Port to which this server should connect */
2046 if (port >= 0 && port < 0xFFFF)
2047 New_Server.port = (UINT16)port;
2049 Config_Error(LOG_ERR,
2050 "%s, line %d (section \"Server\"): Illegal port number %ld!",
2055 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
2056 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
2060 if( strcasecmp( Var, "Group" ) == 0 ) {
2062 New_Server.group = atoi( Arg );
2063 if (!New_Server.group && strcmp(Arg, "0"))
2064 Config_Error_NaN(File, Line, Var);
2067 if( strcasecmp( Var, "Passive" ) == 0 ) {
2068 if (Check_ArgIsTrue(Arg))
2069 New_Server.flags |= CONF_SFLAG_DISABLED;
2072 if (strcasecmp(Var, "ServiceMask") == 0) {
2073 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
2074 sizeof(New_Server.svs_mask));
2075 if (len >= sizeof(New_Server.svs_mask))
2076 Config_Error_TooLong(File, Line, Var);
2080 Config_Error_Section(File, Line, Var, "Server");
2084 * Copy channel name into channel structure.
2086 * If the channel name is not valid because of a missing prefix ('#', '&'),
2087 * a default prefix of '#' will be added.
2089 * @param new_chan New already allocated channel structure.
2090 * @param name Name of the new channel.
2091 * @returns true on success, false otherwise.
2094 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
2096 size_t size = sizeof(new_chan->name);
2097 char *dest = new_chan->name;
2099 if (!Channel_IsValidName(name)) {
2101 * maybe user forgot to add a '#'.
2102 * This is only here for user convenience.
2108 return size > strlcpy(dest, name, size);
2112 * Handle variable in [Channel] configuration section.
2114 * @param Line Line numer in configuration file.
2115 * @param Var Variable name.
2116 * @param Arg Variable argument.
2119 Handle_CHANNEL(const char *File, int Line, char *Var, char *Arg)
2122 struct Conf_Channel *chan;
2124 assert( File != NULL );
2126 assert( Var != NULL );
2127 assert( Arg != NULL );
2129 chan = array_get(&Conf_Channels, sizeof(*chan),
2130 array_length(&Conf_Channels, sizeof(*chan)) - 1);
2134 if (strcasecmp(Var, "Name") == 0) {
2135 if (!Handle_Channelname(chan, Arg))
2136 Config_Error_TooLong(File, Line, Var);
2139 if (strcasecmp(Var, "Modes") == 0) {
2141 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
2142 if (len >= sizeof(chan->modes))
2143 Config_Error_TooLong(File, Line, Var);
2146 if( strcasecmp( Var, "Topic" ) == 0 ) {
2148 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
2149 if (len >= sizeof(chan->topic))
2150 Config_Error_TooLong(File, Line, Var);
2153 if( strcasecmp( Var, "Key" ) == 0 ) {
2154 /* Initial Channel Key (mode k) */
2155 len = strlcpy(chan->key, Arg, sizeof(chan->key));
2156 if (len >= sizeof(chan->key))
2157 Config_Error_TooLong(File, Line, Var);
2160 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
2161 /* maximum user limit, mode l */
2162 chan->maxusers = (unsigned long) atol(Arg);
2163 if (!chan->maxusers && strcmp(Arg, "0"))
2164 Config_Error_NaN(File, Line, Var);
2167 if (strcasecmp(Var, "KeyFile") == 0) {
2169 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
2170 if (len >= sizeof(chan->keyfile))
2171 Config_Error_TooLong(File, Line, Var);
2175 Config_Error_Section(File, Line, Var, "Channel");
2179 * Validate server configuration.
2181 * Please note that this function uses exit(1) on fatal errors and therefore
2182 * can result in ngIRCd terminating!
2184 * @param Configtest true if the daemon has been called with "--configtest".
2185 * @param Rehash true if re-reading configuration on runtime.
2186 * @returns true if configuration is valid.
2189 Validate_Config(bool Configtest, bool Rehash)
2191 /* Validate configuration settings. */
2194 int i, servers, servers_once;
2196 bool config_valid = true;
2199 /* Emit a warning when the config file is not a full path name */
2200 if (NGIRCd_ConfFile[0] && NGIRCd_ConfFile[0] != '/') {
2201 Config_Error(LOG_WARNING,
2202 "Not specifying a full path name to \"%s\" can cause problems when rehashing the server!",
2206 /* Validate configured server name, see RFC 2812 section 2.3.1 */
2207 ptr = Conf_ServerName;
2209 if (*ptr >= 'a' && *ptr <= 'z') continue;
2210 if (*ptr >= 'A' && *ptr <= 'Z') continue;
2211 if (*ptr >= '0' && *ptr <= '9') continue;
2212 if (ptr > Conf_ServerName) {
2213 if (*ptr == '.' || *ptr == '-')
2216 Conf_ServerName[0] = '\0';
2220 if (!Conf_ServerName[0]) {
2221 /* No server name configured! */
2222 config_valid = false;
2223 Config_Error(LOG_ALERT,
2224 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
2226 if (!Configtest && !Rehash) {
2227 Config_Error(LOG_ALERT,
2228 "%s exiting due to fatal errors!",
2234 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
2235 /* No dot in server name! */
2236 config_valid = false;
2237 Config_Error(LOG_ALERT,
2238 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
2241 Config_Error(LOG_ALERT,
2242 "%s exiting due to fatal errors!",
2249 if (!Conf_ServerAdminMail[0]) {
2250 /* No administrative contact configured! */
2251 config_valid = false;
2252 Config_Error(LOG_ALERT,
2253 "No administrator email address configured in \"%s\" ('AdminEMail')!",
2256 Config_Error(LOG_ALERT,
2257 "%s exiting due to fatal errors!",
2264 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
2265 && !Conf_ServerAdminMail[0]) {
2266 /* No administrative information configured! */
2267 Config_Error(LOG_WARNING,
2268 "No administrative information configured but required by RFC!");
2272 if (Conf_PAM && Conf_ServerPwd[0])
2273 Config_Error(LOG_ERR,
2274 "This server uses PAM, \"Password\" in [Global] section will be ignored!");
2278 servers = servers_once = 0;
2279 for (i = 0; i < MAX_SERVERS; i++) {
2280 if (Conf_Server[i].name[0]) {
2282 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
2287 "Configuration: Operators=%ld, Servers=%d[%d], Channels=%ld",
2288 array_length(&Conf_Opers, sizeof(struct Conf_Oper)),
2289 servers, servers_once,
2290 array_length(&Conf_Channels, sizeof(struct Conf_Channel)));
2293 return config_valid;
2297 * Output "line too long" warning.
2299 * @param Line Line number in configuration file.
2300 * @param Item Affected variable name.
2303 Config_Error_TooLong(const char *File, const int Line, const char *Item)
2305 Config_Error(LOG_WARNING, "%s, line %d: Value of \"%s\" too long!",
2310 * Output "unknown variable" warning.
2312 * @param Line Line number in configuration file.
2313 * @param Item Affected variable name.
2314 * @param Section Section name.
2317 Config_Error_Section(const char *File, const int Line, const char *Item,
2318 const char *Section)
2320 Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
2321 File, Line, Section, Item);
2325 * Output "not a number" warning.
2327 * @param Line Line number in configuration file.
2328 * @param Item Affected variable name.
2331 Config_Error_NaN(const char *File, const int Line, const char *Item )
2333 Config_Error(LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
2338 * Output configuration error to console and/or logfile.
2340 * On runtime, the normal log functions of the daemon are used. But when
2341 * testing the configuration ("--configtest"), all messages go directly
2344 * @param Level Severity level of the message.
2345 * @param Format Format string; see printf() function.
2348 static void Config_Error( const int Level, const char *Format, ... )
2350 static void Config_Error( Level, Format, va_alist )
2356 char msg[MAX_LOG_MSG_LEN];
2359 assert( Format != NULL );
2362 va_start( ap, Format );
2366 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
2370 if (Level <= LOG_WARNING)
2371 printf(" - %s\n", msg);
2375 Log(Level, "%s", msg);
2381 * Dump internal state of the "configuration module".
2384 Conf_DebugDump(void)
2388 Log(LOG_DEBUG, "Configured servers:");
2389 for (i = 0; i < MAX_SERVERS; i++) {
2390 if (! Conf_Server[i].name[0])
2393 " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
2394 Conf_Server[i].name, Conf_Server[i].host,
2395 Conf_Server[i].port, Conf_Server[i].lasttry,
2396 Conf_Server[i].group, Conf_Server[i].flags,
2397 Conf_Server[i].conn_id);
2404 * Initialize server configuration structure to default values.
2406 * @param Server Pointer to server structure to initialize.
2409 Init_Server_Struct( CONF_SERVER *Server )
2411 assert( Server != NULL );
2413 memset( Server, 0, sizeof (CONF_SERVER) );
2415 Server->group = NONE;
2416 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
2418 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
2420 Proc_InitStruct(&Server->res_stat);
2421 Server->conn_id = NONE;
2422 memset(&Server->bind_addr, 0, sizeof(Server->bind_addr));