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>
50 static bool Use_Log = true, Using_MotdFile = true;
51 static CONF_SERVER New_Server;
52 static int New_Server_Idx;
54 static char Conf_MotdFile[FNAME_LEN];
55 static char Conf_HelpFile[FNAME_LEN];
56 static char Conf_IncludeDir[FNAME_LEN];
58 static void Set_Defaults PARAMS(( bool InitServers ));
59 static bool Read_Config PARAMS(( bool TestOnly, bool IsStarting ));
60 static void Read_Config_File PARAMS(( const char *File, FILE *fd ));
61 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
63 static void Handle_GLOBAL PARAMS((const char *File, int Line,
64 char *Var, char *Arg ));
65 static void Handle_LIMITS PARAMS((const char *File, int Line,
66 char *Var, char *Arg ));
67 static void Handle_OPTIONS PARAMS((const char *File, int Line,
68 char *Var, char *Arg ));
69 static void Handle_OPERATOR PARAMS((const char *File, int Line,
70 char *Var, char *Arg ));
71 static void Handle_SERVER PARAMS((const char *File, int Line,
72 char *Var, char *Arg ));
73 static void Handle_CHANNEL PARAMS((const char *File, int Line,
74 char *Var, char *Arg ));
76 static void Config_Error PARAMS((const int Level, const char *Format, ...));
78 static void Config_Error_NaN PARAMS((const char *File, const int LINE,
80 static void Config_Error_Section PARAMS((const char *File, const int Line,
81 const char *Item, const char *Section));
82 static void Config_Error_TooLong PARAMS((const char *File, const int LINE,
85 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
89 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
91 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
95 #define DEFAULT_CIPHERS "HIGH:!aNULL:@STRENGTH"
98 #define DEFAULT_CIPHERS "SECURE128"
103 static void Handle_SSL PARAMS((const char *File, int Line, char *Var, char *Ark));
105 struct SSLOptions Conf_SSLOptions;
108 * Initialize SSL configuration.
113 free(Conf_SSLOptions.KeyFile);
114 Conf_SSLOptions.KeyFile = NULL;
116 free(Conf_SSLOptions.CertFile);
117 Conf_SSLOptions.CertFile = NULL;
119 free(Conf_SSLOptions.DHFile);
120 Conf_SSLOptions.DHFile = NULL;
121 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
123 array_free(&Conf_SSLOptions.ListenPorts);
125 free(Conf_SSLOptions.CipherList);
126 Conf_SSLOptions.CipherList = NULL;
130 * Check if the current configuration uses/requires SSL.
132 * @returns true if SSL is used and should be initialized.
139 /* SSL listen ports configured? */
140 if (array_bytes(&Conf_SSLOptions.ListenPorts))
143 for (i = 0; i < MAX_SERVERS; i++) {
144 if (Conf_Server[i].port > 0
145 && Conf_Server[i].SSLConnect)
152 * Make sure that a configured file is readable.
154 * Currently, this function is only used for SSL-related options ...
156 * @param Var Configuration variable
157 * @param Filename Configured filename
160 CheckFileReadable(const char *Var, const char *Filename)
167 fp = fopen(Filename, "r");
171 Config_Error(LOG_ERR, "Can't read \"%s\" (\"%s\"): %s",
172 Filename, Var, strerror(errno));
179 * Duplicate string and warn on errors.
181 * @returns Pointer to string on success, NULL otherwise.
184 strdup_warn(const char *str)
186 char *ptr = strdup(str);
188 Config_Error(LOG_ERR,
189 "Could not allocate memory for string: %s", str);
194 * Output a comma separated list of ports (integer values).
201 len = array_length(a, sizeof(UINT16));
203 ports = (UINT16*) array_start(a);
204 printf("%u", (unsigned int) *ports);
207 printf(", %u", (unsigned int) *ports);
214 * Parse a comma separated string into an array of port numbers (integers).
217 ports_parse(array *a, const char *File, int Line, char *Arg)
225 ptr = strtok( Arg, "," );
229 if (port > 0 && port < 0xFFFF) {
230 port16 = (UINT16) port;
231 if (!array_catb(a, (char*)&port16, sizeof port16))
232 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
233 File, Line, port, strerror(errno));
235 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
239 ptr = strtok( NULL, "," );
244 * Initialize configuration module.
249 Read_Config(false, true);
250 Validate_Config(false, false);
254 * "Rehash" (reload) server configuration.
256 * @returns true if configuration has been re-read, false on errors.
261 if (!Read_Config(false, false))
263 Validate_Config(false, true);
265 /* Update CLIENT structure of local server */
266 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
271 * Output a boolean value as "yes/no" string.
274 yesno_to_str(int boolean_value)
282 * Free all IRC operator configuration structures.
287 struct Conf_Oper *op;
290 len = array_length(&Conf_Opers, sizeof(*op));
291 op = array_start(&Conf_Opers);
296 array_free(&Conf_Opers);
300 * Output all IRC operator configuration structures.
305 struct Conf_Oper *op;
308 count = array_length(&Conf_Opers, sizeof(*op));
309 op = array_start(&Conf_Opers);
310 for (i = 0; i < count; i++, op++) {
315 printf(" Name = %s\n", op->name);
316 printf(" Password = %s\n", op->pwd);
317 printf(" Mask = %s\n\n", op->mask ? op->mask : "");
322 * Read configuration, validate and output it.
324 * This function waits for a keypress of the user when stdin/stdout are valid
325 * tty's ("you can read our nice message and we can read in your keypress").
327 * @return 0 on success, 1 on failure(s); therefore the result code can
328 * directly be used by exit() when running "ngircd --configtest".
337 size_t predef_channel_count;
338 struct Conf_Channel *predef_chan;
342 if (!Read_Config(true, true))
345 config_valid = Validate_Config(true, false);
348 if(isatty(fileno(stdin)) && isatty(fileno(stdout))) {
349 puts("OK, press enter to see a dump of your server configuration ...");
352 puts("Ok, dump of your server configuration follows:\n");
355 printf(" Name = %s\n", Conf_ServerName);
356 printf(" AdminInfo1 = %s\n", Conf_ServerAdmin1);
357 printf(" AdminInfo2 = %s\n", Conf_ServerAdmin2);
358 printf(" AdminEMail = %s\n", Conf_ServerAdminMail);
359 printf(" HelpFile = %s\n", Conf_HelpFile);
360 printf(" Info = %s\n", Conf_ServerInfo);
361 printf(" Listen = %s\n", Conf_ListenAddress);
362 if (Using_MotdFile) {
363 printf(" MotdFile = %s\n", Conf_MotdFile);
364 printf(" MotdPhrase =\n");
366 printf(" MotdFile = \n");
367 printf(" MotdPhrase = %s\n", array_bytes(&Conf_Motd)
368 ? (const char*) array_start(&Conf_Motd) : "");
370 printf(" Network = %s\n", Conf_Network);
372 printf(" Password = %s\n", Conf_ServerPwd);
373 printf(" PidFile = %s\n", Conf_PidFile);
375 ports_puts(&Conf_ListenPorts);
376 grp = getgrgid(Conf_GID);
378 printf(" ServerGID = %s\n", grp->gr_name);
380 printf(" ServerGID = %ld\n", (long)Conf_GID);
381 pwd = getpwuid(Conf_UID);
383 printf(" ServerUID = %s\n", pwd->pw_name);
385 printf(" ServerUID = %ld\n", (long)Conf_UID);
389 printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
390 printf(" IdleTimeout = %d\n", Conf_IdleTimeout);
391 printf(" MaxConnections = %d\n", Conf_MaxConnections);
392 printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
393 printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
394 printf(" MaxNickLength = %u\n", Conf_MaxNickLength - 1);
395 printf(" MaxListSize = %d\n", Conf_MaxListSize);
396 printf(" PingTimeout = %d\n", Conf_PingTimeout);
397 printf(" PongTimeout = %d\n", Conf_PongTimeout);
401 printf(" AllowedChannelTypes = %s\n", Conf_AllowedChannelTypes);
402 printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
403 printf(" ChrootDir = %s\n", Conf_Chroot);
404 printf(" CloakHost = %s\n", Conf_CloakHost);
405 printf(" CloakHostModeX = %s\n", Conf_CloakHostModeX);
406 printf(" CloakHostSalt = %s\n", Conf_CloakHostSalt);
407 printf(" CloakUserToNick = %s\n", yesno_to_str(Conf_CloakUserToNick));
409 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
410 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
412 printf(" DefaultUserModes = %s\n", Conf_DefaultUserModes);
413 printf(" DNS = %s\n", yesno_to_str(Conf_DNS));
415 printf(" Ident = %s\n", yesno_to_str(Conf_Ident));
417 printf(" IncludeDir = %s\n", Conf_IncludeDir);
418 printf(" MorePrivacy = %s\n", yesno_to_str(Conf_MorePrivacy));
419 printf(" NoticeAuth = %s\n", yesno_to_str(Conf_NoticeAuth));
420 printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
421 printf(" OperChanPAutoOp = %s\n", yesno_to_str(Conf_OperChanPAutoOp));
422 printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
424 printf(" PAM = %s\n", yesno_to_str(Conf_PAM));
425 printf(" PAMIsOptional = %s\n", yesno_to_str(Conf_PAMIsOptional));
428 printf(" RequireAuthPing = %s\n", yesno_to_str(Conf_AuthPing));
430 printf(" ScrubCTCP = %s\n", yesno_to_str(Conf_ScrubCTCP));
432 printf(" SyslogFacility = %s\n",
433 ngt_SyslogFacilityName(Conf_SyslogFacility));
435 printf(" WebircPassword = %s\n", Conf_WebircPwd);
440 printf(" CertFile = %s\n", Conf_SSLOptions.CertFile
441 ? Conf_SSLOptions.CertFile : "");
442 printf(" CipherList = %s\n", Conf_SSLOptions.CipherList ?
443 Conf_SSLOptions.CipherList : DEFAULT_CIPHERS);
444 printf(" DHFile = %s\n", Conf_SSLOptions.DHFile
445 ? Conf_SSLOptions.DHFile : "");
446 printf(" KeyFile = %s\n", Conf_SSLOptions.KeyFile
447 ? Conf_SSLOptions.KeyFile : "");
448 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
449 puts(" KeyFilePassword = <secret>");
451 puts(" KeyFilePassword = ");
452 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
454 ports_puts(&Conf_SSLOptions.ListenPorts);
460 for( i = 0; i < MAX_SERVERS; i++ ) {
461 if( ! Conf_Server[i].name[0] ) continue;
463 /* Valid "Server" section */
465 printf( " Name = %s\n", Conf_Server[i].name );
466 printf( " Host = %s\n", Conf_Server[i].host );
467 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
469 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
471 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
472 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
473 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
474 printf( " Group = %d\n", Conf_Server[i].group );
475 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
478 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
479 predef_chan = array_start(&Conf_Channels);
481 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
482 if (!predef_chan->name[0])
485 /* Valid "Channel" section */
487 printf(" Name = %s\n", predef_chan->name);
488 printf(" Modes = %s\n", predef_chan->modes);
489 printf(" Key = %s\n", predef_chan->key);
490 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
491 printf(" Topic = %s\n", predef_chan->topic);
492 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
495 return (config_valid ? 0 : 1);
499 * Remove connection information from configured server.
501 * If the server is set as "once", delete it from our configuration;
502 * otherwise set the time for the next connection attempt.
504 * Non-server connections will be silently ignored.
507 Conf_UnsetServer( CONN_ID Idx )
512 /* Check all our configured servers */
513 for( i = 0; i < MAX_SERVERS; i++ ) {
514 if( Conf_Server[i].conn_id != Idx ) continue;
516 /* Gotcha! Mark server configuration as "unused": */
517 Conf_Server[i].conn_id = NONE;
519 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
520 /* Delete configuration here */
521 Init_Server_Struct( &Conf_Server[i] );
523 /* Set time for next connect attempt */
525 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
526 /* The connection has been "long", so we don't
527 * require the next attempt to be delayed. */
528 Conf_Server[i].lasttry =
529 t - Conf_ConnectRetry + RECONNECT_DELAY;
531 /* "Short" connection, enforce "ConnectRetry"
532 * but randomize it a little bit: 15 seconds. */
533 Conf_Server[i].lasttry =
534 #ifdef HAVE_ARC4RANDOM
535 t + (arc4random() % 15);
537 t + rand() / (RAND_MAX / 15);
545 * Set connection information for specified configured server.
548 Conf_SetServer( int ConfServer, CONN_ID Idx )
550 assert( ConfServer > NONE );
551 assert( Idx > NONE );
553 if (Conf_Server[ConfServer].conn_id > NONE &&
554 Conf_Server[ConfServer].conn_id != Idx) {
556 "Connection %d: Server configuration of \"%s\" already in use by connection %d!",
557 Idx, Conf_Server[ConfServer].name,
558 Conf_Server[ConfServer].conn_id);
559 Conn_Close(Idx, NULL, "Server configuration already in use", true);
562 Conf_Server[ConfServer].conn_id = Idx;
567 * Get index of server in configuration structure.
570 Conf_GetServer( CONN_ID Idx )
574 assert( Idx > NONE );
576 for( i = 0; i < MAX_SERVERS; i++ ) {
577 if( Conf_Server[i].conn_id == Idx ) return i;
583 * Enable a server by name and adjust its port number.
585 * @returns true if a server has been enabled and now has a valid port
586 * number and host name for outgoing connections.
589 Conf_EnableServer( const char *Name, UINT16 Port )
593 assert( Name != NULL );
594 for( i = 0; i < MAX_SERVERS; i++ ) {
595 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
596 /* Gotcha! Set port and enable server: */
597 Conf_Server[i].port = Port;
598 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
599 return (Conf_Server[i].port && Conf_Server[i].host[0]);
606 * Enable a server by name.
608 * The server is only usable as outgoing server, if it has set a valid port
609 * number for outgoing connections!
610 * If not, you have to use Conf_EnableServer() function to make it available.
612 * @returns true if a server has been enabled; false otherwise.
615 Conf_EnablePassiveServer(const char *Name)
619 assert( Name != NULL );
620 for (i = 0; i < MAX_SERVERS; i++) {
621 if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
622 && (Conf_Server[i].port > 0)) {
623 /* BINGO! Enable server */
624 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
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 ConfServer The server index or NONE to check all configured servers.
717 * @param Nick The nickname to check.
718 * @returns true if the given nickname belongs to an "IRC service".
721 Conf_NickIsBlocked(const char *Nick)
725 for(i = 0; i < MAX_SERVERS; i++) {
726 if (!Conf_Server[i].name[0])
728 if (Conf_NickIsService(i, Nick))
735 * Initialize configuration settings with their default values.
738 Set_Defaults(bool InitServers)
741 char random[RANDOM_SALT_LEN + 1];
744 strcpy(Conf_ServerName, "");
745 strcpy(Conf_ServerAdmin1, "");
746 strcpy(Conf_ServerAdmin2, "");
747 strcpy(Conf_ServerAdminMail, "");
748 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
749 PACKAGE_NAME, PACKAGE_VERSION);
750 strcpy(Conf_Network, "");
751 free(Conf_ListenAddress);
752 Conf_ListenAddress = NULL;
753 array_free(&Conf_ListenPorts);
754 array_free(&Conf_Motd);
755 array_free(&Conf_Helptext);
756 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
757 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
758 strlcpy(Conf_HelpFile, DOCDIR, sizeof(Conf_HelpFile));
759 strlcat(Conf_HelpFile, HELP_FILE, sizeof(Conf_HelpFile));
760 strcpy(Conf_ServerPwd, "");
761 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
762 Conf_UID = Conf_GID = 0;
765 Conf_ConnectRetry = 60;
766 Conf_IdleTimeout = 0;
767 Conf_MaxConnections = 0;
768 Conf_MaxConnectionsIP = 5;
770 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
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_NoticeAuth = false;
804 Conf_OperCanMode = false;
805 Conf_OperChanPAutoOp = true;
806 Conf_OperServerMode = false;
812 Conf_PAMIsOptional = false;
814 Conf_ScrubCTCP = false;
816 Conf_SyslogFacility = LOG_LOCAL5;
818 Conf_SyslogFacility = 0;
822 /* Initialize server configuration structures */
824 for (i = 0; i < MAX_SERVERS;
825 Init_Server_Struct(&Conf_Server[i++]));
830 * Get number of configured listening ports.
832 * @returns The number of ports (IPv4+IPv6) on which the server should listen.
837 size_t cnt = array_bytes(&Conf_ListenPorts);
839 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
845 * Read contents of a text file into an array.
847 * This function is used to read the MOTD and help text file, for example.
849 * @param filename Name of the file to read.
850 * @return true, when the file has been read in.
853 Read_TextFile(const char *Filename, const char *Name, array *Destination)
859 if (*Filename == '\0')
862 fp = fopen(Filename, "r");
864 Config_Error(LOG_ERR, "Can't read %s file \"%s\": %s",
865 Name, Filename, strerror(errno));
869 array_free(Destination);
870 while (fgets(line, (int)sizeof line, fp)) {
871 ngt_TrimLastChr(line, '\n');
873 /* add text including \0 */
874 if (!array_catb(Destination, line, strlen(line) + 1)) {
875 Log(LOG_ERR, "Cannot read/add \"%s\", line %d: %s",
876 Filename, line_no, strerror(errno));
886 * Read ngIRCd configuration file.
888 * Please note that this function uses exit(1) on fatal errors and therefore
889 * can result in ngIRCd terminating!
891 * @param ngircd_starting Flag indicating if ngIRCd is starting or not.
892 * @returns true when the configuration file has been read
893 * successfully; false otherwise.
896 Read_Config(bool TestOnly, bool IsStarting)
898 const UINT16 defaultport = 6667;
899 char *ptr, file[FNAME_LEN];
900 struct dirent *entry;
905 /* Open configuration file */
906 fd = fopen( NGIRCd_ConfFile, "r" );
908 /* No configuration file found! */
909 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
910 NGIRCd_ConfFile, strerror( errno ));
913 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
918 Set_Defaults(IsStarting);
921 Config_Error(LOG_INFO,
922 "Reading configuration from \"%s\" ...",
925 /* Clean up server configuration structure: mark all already
926 * configured servers as "once" so that they are deleted
927 * after the next disconnect and delete all unused servers.
928 * And delete all servers which are "duplicates" of servers
929 * that are already marked as "once" (such servers have been
930 * created by the last rehash but are now useless). */
931 for( i = 0; i < MAX_SERVERS; i++ ) {
932 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
934 /* This structure is in use ... */
935 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
936 /* Check for duplicates */
937 for( n = 0; n < MAX_SERVERS; n++ ) {
938 if( n == i ) continue;
940 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
941 Init_Server_Struct( &Conf_Server[n] );
943 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
949 /* Mark server as "once" */
950 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
951 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
956 /* Initialize variables */
957 Init_Server_Struct( &New_Server );
958 New_Server_Idx = NONE;
963 Read_Config_File(NGIRCd_ConfFile, fd);
966 if (Conf_IncludeDir[0]) {
967 dh = opendir(Conf_IncludeDir);
969 Config_Error(LOG_ALERT,
970 "Can't open include directory \"%s\": %s",
971 Conf_IncludeDir, strerror(errno));
973 strlcpy(Conf_IncludeDir, SYSCONFDIR, sizeof(Conf_IncludeDir));
974 strlcat(Conf_IncludeDir, CONFIG_DIR, sizeof(Conf_IncludeDir));
975 dh = opendir(Conf_IncludeDir);
978 /* Include further configuration files, if IncludeDir is available */
980 while ((entry = readdir(dh)) != NULL) {
981 ptr = strrchr(entry->d_name, '.');
982 if (!ptr || strcasecmp(ptr, ".conf") != 0)
984 snprintf(file, sizeof(file), "%s/%s",
985 Conf_IncludeDir, entry->d_name);
987 Config_Error(LOG_INFO,
988 "Reading configuration from \"%s\" ...",
990 fd = fopen(file, "r");
992 Read_Config_File(file, fd);
995 Config_Error(LOG_ALERT,
996 "Can't read configuration \"%s\": %s",
997 file, strerror(errno));
1002 /* Check if there is still a server to add */
1003 if( New_Server.name[0] ) {
1004 /* Copy data to "real" server structure */
1005 assert( New_Server_Idx > NONE );
1006 Conf_Server[New_Server_Idx] = New_Server;
1009 /* not a single listening port? Add default. */
1010 if (no_listenports() &&
1011 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
1013 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
1014 (unsigned int) defaultport, strerror(errno));
1019 if (!Conf_ListenAddress)
1020 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
1022 if (!Conf_ListenAddress) {
1023 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
1027 /* No MOTD phrase configured? (re)try motd file. */
1028 if (array_bytes(&Conf_Motd) == 0) {
1029 if (Read_TextFile(Conf_MotdFile, "MOTD", &Conf_Motd))
1030 Using_MotdFile = true;
1033 /* Try to read ngIRCd help text file. */
1034 (void)Read_TextFile(Conf_HelpFile, "help text", &Conf_Helptext);
1035 if (!array_bytes(&Conf_Helptext))
1036 Config_Error(LOG_WARNING,
1037 "No help text available, HELP command will be of limited use.");
1040 /* Make sure that all SSL-related files are readable */
1041 CheckFileReadable("CertFile", Conf_SSLOptions.CertFile);
1042 CheckFileReadable("DHFile", Conf_SSLOptions.DHFile);
1043 CheckFileReadable("KeyFile", Conf_SSLOptions.KeyFile);
1045 /* Set the default ciphers if none were configured */
1046 if (!Conf_SSLOptions.CipherList)
1047 Conf_SSLOptions.CipherList = strdup_warn(DEFAULT_CIPHERS);
1054 * Read in and handle a configuration file.
1056 * @param File Name of the configuration file.
1057 * @param fd File descriptor already opened for reading.
1060 Read_Config_File(const char *File, FILE *fd)
1062 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
1066 /* Read configuration file */
1069 if (!fgets(str, sizeof(str), fd))
1074 /* Skip comments and empty lines */
1075 if (str[0] == ';' || str[0] == '#' || str[0] == '\0')
1078 if (strlen(str) >= sizeof(str) - 1) {
1079 Config_Error(LOG_WARNING, "%s, line %d too long!",
1084 /* Is this the beginning of a new section? */
1085 if ((str[0] == '[') && (str[strlen(str) - 1] == ']')) {
1086 strlcpy(section, str, sizeof(section));
1087 if (strcasecmp(section, "[GLOBAL]") == 0
1088 || strcasecmp(section, "[LIMITS]") == 0
1089 || strcasecmp(section, "[OPTIONS]") == 0
1091 || strcasecmp(section, "[SSL]") == 0
1096 if (strcasecmp(section, "[SERVER]") == 0) {
1097 /* Check if there is already a server to add */
1098 if (New_Server.name[0]) {
1099 /* Copy data to "real" server structure */
1100 assert(New_Server_Idx > NONE);
1101 Conf_Server[New_Server_Idx] =
1105 /* Re-init structure for new server */
1106 Init_Server_Struct(&New_Server);
1108 /* Search unused item in server configuration structure */
1109 for (i = 0; i < MAX_SERVERS; i++) {
1110 /* Is this item used? */
1111 if (!Conf_Server[i].name[0])
1114 if (i >= MAX_SERVERS) {
1115 /* Oops, no free item found! */
1116 Config_Error(LOG_ERR,
1117 "Too many servers configured.");
1118 New_Server_Idx = NONE;
1124 if (strcasecmp(section, "[CHANNEL]") == 0) {
1125 count = array_length(&Conf_Channels,
1130 sizeof(struct Conf_Channel), count)) {
1131 Config_Error(LOG_ERR,
1132 "Could not allocate memory for new operator (line %d)",
1138 if (strcasecmp(section, "[OPERATOR]") == 0) {
1139 count = array_length(&Conf_Opers,
1140 sizeof(struct Conf_Oper));
1141 if (!array_alloc(&Conf_Opers,
1142 sizeof(struct Conf_Oper),
1144 Config_Error(LOG_ERR,
1145 "Could not allocate memory for new channel (line &d)",
1151 Config_Error(LOG_ERR,
1152 "%s, line %d: Unknown section \"%s\"!",
1153 File, line, section);
1156 if (section[0] == 0x1)
1159 /* Split line into variable name and parameters */
1160 ptr = strchr(str, '=');
1162 Config_Error(LOG_ERR, "%s, line %d: Syntax error!",
1172 if (strcasecmp(section, "[GLOBAL]") == 0)
1173 Handle_GLOBAL(File, line, var, arg);
1174 else if (strcasecmp(section, "[LIMITS]") == 0)
1175 Handle_LIMITS(File, line, var, arg);
1176 else if (strcasecmp(section, "[OPTIONS]") == 0)
1177 Handle_OPTIONS(File, line, var, arg);
1179 else if (strcasecmp(section, "[SSL]") == 0)
1180 Handle_SSL(File, line, var, arg);
1182 else if (strcasecmp(section, "[OPERATOR]") == 0)
1183 Handle_OPERATOR(File, line, var, arg);
1184 else if (strcasecmp(section, "[SERVER]") == 0)
1185 Handle_SERVER(File, line, var, arg);
1186 else if (strcasecmp(section, "[CHANNEL]") == 0)
1187 Handle_CHANNEL(File, line, var, arg);
1189 Config_Error(LOG_ERR,
1190 "%s, line %d: Variable \"%s\" outside section!",
1196 * Check whether a string argument is "true" or "false".
1198 * @param Arg Input string.
1199 * @returns true if the input string has been parsed as "yes", "true"
1200 * (case insensitive) or a non-zero integer value.
1203 Check_ArgIsTrue(const char *Arg)
1205 if (strcasecmp(Arg, "yes") == 0)
1207 if (strcasecmp(Arg, "true") == 0)
1216 * Handle setting of "MaxNickLength".
1218 * @param Line Line number in configuration file.
1219 * @raram Arg Input string.
1220 * @returns New configured maximum nickname length.
1223 Handle_MaxNickLength(const char *File, int Line, const char *Arg)
1227 new = (unsigned) atoi(Arg) + 1;
1228 if (new > CLIENT_NICK_LEN) {
1229 Config_Error(LOG_WARNING,
1230 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
1231 File, Line, CLIENT_NICK_LEN - 1);
1232 return CLIENT_NICK_LEN;
1235 Config_Error(LOG_WARNING,
1236 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
1244 * Output a warning messages if IDENT is configured but not compiled in.
1247 WarnIdent(const char UNUSED *File, int UNUSED Line)
1251 /* user has enabled ident lookups explicitly, but ... */
1252 Config_Error(LOG_WARNING,
1253 "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
1260 * Output a warning messages if IPv6 is configured but not compiled in.
1263 WarnIPv6(const char UNUSED *File, int UNUSED Line)
1266 if (Conf_ConnectIPv6) {
1267 /* user has enabled IPv6 explicitly, but ... */
1268 Config_Error(LOG_WARNING,
1269 "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
1276 * Output a warning messages if PAM is configured but not compiled in.
1279 WarnPAM(const char UNUSED *File, int UNUSED Line)
1283 Config_Error(LOG_WARNING,
1284 "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
1291 * Handle legacy "NoXXX" options in [GLOBAL] section.
1293 * TODO: This function and support for "NoXXX" could be removed starting
1294 * with ngIRCd release 19 (one release after marking it "deprecated").
1296 * @param Var Variable name.
1297 * @param Arg Argument string.
1298 * @returns true if a NoXXX option has been processed; false otherwise.
1301 CheckLegacyNoOption(const char *Var, const char *Arg)
1303 if(strcasecmp(Var, "NoDNS") == 0) {
1304 Conf_DNS = !Check_ArgIsTrue( Arg );
1307 if (strcasecmp(Var, "NoIdent") == 0) {
1308 Conf_Ident = !Check_ArgIsTrue(Arg);
1311 if(strcasecmp(Var, "NoPAM") == 0) {
1312 Conf_PAM = !Check_ArgIsTrue(Arg);
1319 * Handle deprecated legacy options in [GLOBAL] section.
1321 * TODO: This function and support for these options in the [Global] section
1322 * could be removed starting with ngIRCd release 19 (one release after
1323 * marking it "deprecated").
1325 * @param Var Variable name.
1326 * @param Arg Argument string.
1327 * @returns true if a legacy option has been processed; false otherwise.
1330 CheckLegacyGlobalOption(const char *File, int Line, char *Var, char *Arg)
1332 if (strcasecmp(Var, "AllowRemoteOper") == 0
1333 || strcasecmp(Var, "ChrootDir") == 0
1334 || strcasecmp(Var, "ConnectIPv4") == 0
1335 || strcasecmp(Var, "ConnectIPv6") == 0
1336 || strcasecmp(Var, "OperCanUseMode") == 0
1337 || strcasecmp(Var, "OperChanPAutoOp") == 0
1338 || strcasecmp(Var, "OperServerMode") == 0
1339 || strcasecmp(Var, "PredefChannelsOnly") == 0
1340 || strcasecmp(Var, "SyslogFacility") == 0
1341 || strcasecmp(Var, "WebircPassword") == 0) {
1342 Handle_OPTIONS(File, Line, Var, Arg);
1345 if (strcasecmp(Var, "ConnectRetry") == 0
1346 || strcasecmp(Var, "IdleTimeout") == 0
1347 || strcasecmp(Var, "MaxConnections") == 0
1348 || strcasecmp(Var, "MaxConnectionsIP") == 0
1349 || strcasecmp(Var, "MaxJoins") == 0
1350 || strcasecmp(Var, "MaxNickLength") == 0
1351 || strcasecmp(Var, "PingTimeout") == 0
1352 || strcasecmp(Var, "PongTimeout") == 0) {
1353 Handle_LIMITS(File, Line, Var, Arg);
1357 if (strcasecmp(Var, "SSLCertFile") == 0
1358 || strcasecmp(Var, "SSLDHFile") == 0
1359 || strcasecmp(Var, "SSLKeyFile") == 0
1360 || strcasecmp(Var, "SSLKeyFilePassword") == 0
1361 || strcasecmp(Var, "SSLPorts") == 0) {
1362 Handle_SSL(File, Line, Var + 3, Arg);
1371 * Strip "no" prefix of a string.
1373 * TODO: This function and support for "NoXXX" should be removed starting
1374 * with ngIRCd release 19! (One release after marking it "deprecated").
1376 * @param str Pointer to input string starting with "no".
1377 * @returns New pointer to string without "no" prefix.
1380 NoNo(const char *str)
1382 assert(strncasecmp("no", str, 2) == 0 && str[2]);
1387 * Invert "boolean" string.
1389 * TODO: This function and support for "NoXXX" should be removed starting
1390 * with ngIRCd release 19! (One release after marking it "deprecated").
1392 * @param arg "Boolean" input string.
1393 * @returns Pointer to inverted "boolean string".
1396 InvertArg(const char *arg)
1398 return yesno_to_str(!Check_ArgIsTrue(arg));
1402 * Handle variable in [Global] configuration section.
1404 * @param Line Line numer in configuration file.
1405 * @param Var Variable name.
1406 * @param Arg Variable argument.
1409 Handle_GLOBAL(const char *File, int Line, char *Var, char *Arg )
1414 const char *section;
1417 assert(File != NULL);
1419 assert(Var != NULL);
1420 assert(Arg != NULL);
1422 if (strcasecmp(Var, "Name") == 0) {
1423 len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
1424 if (len >= sizeof(Conf_ServerName))
1425 Config_Error_TooLong(File, Line, Var);
1428 if (strcasecmp(Var, "AdminInfo1") == 0) {
1429 len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
1430 if (len >= sizeof(Conf_ServerAdmin1))
1431 Config_Error_TooLong(File, Line, Var);
1434 if (strcasecmp(Var, "AdminInfo2") == 0) {
1435 len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
1436 if (len >= sizeof(Conf_ServerAdmin2))
1437 Config_Error_TooLong(File, Line, Var);
1440 if (strcasecmp(Var, "AdminEMail") == 0) {
1441 len = strlcpy(Conf_ServerAdminMail, Arg,
1442 sizeof(Conf_ServerAdminMail));
1443 if (len >= sizeof(Conf_ServerAdminMail))
1444 Config_Error_TooLong(File, Line, Var);
1447 if (strcasecmp(Var, "Info") == 0) {
1448 len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
1449 if (len >= sizeof(Conf_ServerInfo))
1450 Config_Error_TooLong(File, Line, Var);
1453 if (strcasecmp(Var, "HelpFile") == 0) {
1454 len = strlcpy(Conf_HelpFile, Arg, sizeof(Conf_HelpFile));
1455 if (len >= sizeof(Conf_HelpFile))
1456 Config_Error_TooLong(File, Line, Var);
1459 if (strcasecmp(Var, "Listen") == 0) {
1460 if (Conf_ListenAddress) {
1461 Config_Error(LOG_ERR,
1462 "Multiple Listen= options, ignoring: %s",
1466 Conf_ListenAddress = strdup_warn(Arg);
1467 /* If allocation fails, we're in trouble: we cannot ignore the
1468 * error -- otherwise ngircd would listen on all interfaces. */
1469 if (!Conf_ListenAddress) {
1470 Config_Error(LOG_ALERT,
1471 "%s exiting due to fatal errors!",
1477 if (strcasecmp(Var, "MotdFile") == 0) {
1478 len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
1479 if (len >= sizeof(Conf_MotdFile))
1480 Config_Error_TooLong(File, Line, Var);
1483 if (strcasecmp(Var, "MotdPhrase") == 0) {
1488 Config_Error_TooLong(File, Line, Var);
1491 if (!array_copyb(&Conf_Motd, Arg, len + 1))
1492 Config_Error(LOG_WARNING,
1493 "%s, line %d: Could not append MotdPhrase: %s",
1494 File, Line, strerror(errno));
1495 Using_MotdFile = false;
1498 if (strcasecmp(Var, "Network") == 0) {
1499 len = strlcpy(Conf_Network, Arg, sizeof(Conf_Network));
1500 if (len >= sizeof(Conf_Network))
1501 Config_Error_TooLong(File, Line, Var);
1502 ptr = strchr(Conf_Network, ' ');
1504 Config_Error(LOG_WARNING,
1505 "%s, line %d: \"Network\" can't contain spaces!",
1511 if(strcasecmp(Var, "Password") == 0) {
1512 len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
1513 if (len >= sizeof(Conf_ServerPwd))
1514 Config_Error_TooLong(File, Line, Var);
1517 if (strcasecmp(Var, "PidFile") == 0) {
1518 len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
1519 if (len >= sizeof(Conf_PidFile))
1520 Config_Error_TooLong(File, Line, Var);
1523 if (strcasecmp(Var, "Ports") == 0) {
1524 ports_parse(&Conf_ListenPorts, File, Line, Arg);
1527 if (strcasecmp(Var, "ServerGID") == 0) {
1528 grp = getgrnam(Arg);
1530 Conf_GID = grp->gr_gid;
1532 Conf_GID = (unsigned int)atoi(Arg);
1533 if (!Conf_GID && strcmp(Arg, "0"))
1534 Config_Error(LOG_WARNING,
1535 "%s, line %d: Value of \"%s\" is not a valid group name or ID!",
1540 if (strcasecmp(Var, "ServerUID") == 0) {
1541 pwd = getpwnam(Arg);
1543 Conf_UID = pwd->pw_uid;
1545 Conf_UID = (unsigned int)atoi(Arg);
1546 if (!Conf_UID && strcmp(Arg, "0"))
1547 Config_Error(LOG_WARNING,
1548 "%s, line %d: Value of \"%s\" is not a valid user name or ID!",
1554 if (CheckLegacyNoOption(Var, Arg)) {
1555 /* TODO: This function and support for "NoXXX" could be
1556 * be removed starting with ngIRCd release 19 (one release
1557 * after marking it "deprecated"). */
1558 Config_Error(LOG_WARNING,
1559 "%s, line %d (section \"Global\"): \"No\"-Prefix is deprecated, use \"%s = %s\" in [Options] section!",
1560 File, Line, NoNo(Var), InvertArg(Arg));
1561 if (strcasecmp(Var, "NoIdent") == 0)
1562 WarnIdent(File, Line);
1563 else if (strcasecmp(Var, "NoPam") == 0)
1564 WarnPAM(File, Line);
1567 if ((section = CheckLegacyGlobalOption(File, Line, Var, Arg))) {
1568 /** TODO: This function and support for these options in the
1569 * [Global] section could be removed starting with ngIRCd
1570 * release 19 (one release after marking it "deprecated"). */
1571 if (strncasecmp(Var, "SSL", 3) == 0) {
1572 Config_Error(LOG_WARNING,
1573 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s and rename to \"%s\"!",
1574 File, Line, Var, section,
1577 Config_Error(LOG_WARNING,
1578 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s!",
1579 File, Line, Var, section);
1584 Config_Error_Section(File, Line, Var, "Global");
1588 * Handle variable in [Limits] configuration section.
1590 * @param Line Line numer in configuration file.
1591 * @param Var Variable name.
1592 * @param Arg Variable argument.
1595 Handle_LIMITS(const char *File, int Line, char *Var, char *Arg)
1597 assert(File != NULL);
1599 assert(Var != NULL);
1600 assert(Arg != NULL);
1602 if (strcasecmp(Var, "ConnectRetry") == 0) {
1603 Conf_ConnectRetry = atoi(Arg);
1604 if (Conf_ConnectRetry < 5) {
1605 Config_Error(LOG_WARNING,
1606 "%s, line %d: Value of \"ConnectRetry\" too low!",
1608 Conf_ConnectRetry = 5;
1612 if (strcasecmp(Var, "IdleTimeout") == 0) {
1613 Conf_IdleTimeout = atoi(Arg);
1614 if (!Conf_IdleTimeout && strcmp(Arg, "0"))
1615 Config_Error_NaN(File, Line, Var);
1618 if (strcasecmp(Var, "MaxConnections") == 0) {
1619 Conf_MaxConnections = atoi(Arg);
1620 if (!Conf_MaxConnections && strcmp(Arg, "0"))
1621 Config_Error_NaN(File, Line, Var);
1624 if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
1625 Conf_MaxConnectionsIP = atoi(Arg);
1626 if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
1627 Config_Error_NaN(File, Line, Var);
1630 if (strcasecmp(Var, "MaxJoins") == 0) {
1631 Conf_MaxJoins = atoi(Arg);
1632 if (!Conf_MaxJoins && strcmp(Arg, "0"))
1633 Config_Error_NaN(File, Line, Var);
1636 if (strcasecmp(Var, "MaxNickLength") == 0) {
1637 Conf_MaxNickLength = Handle_MaxNickLength(File, Line, Arg);
1640 if (strcasecmp(Var, "MaxListSize") == 0) {
1641 Conf_MaxListSize = atoi(Arg);
1642 if (!Conf_MaxListSize && strcmp(Arg, "0"))
1643 Config_Error_NaN(File, Line, Var);
1646 if (strcasecmp(Var, "PingTimeout") == 0) {
1647 Conf_PingTimeout = atoi(Arg);
1648 if (Conf_PingTimeout < 5) {
1649 Config_Error(LOG_WARNING,
1650 "%s, line %d: Value of \"PingTimeout\" too low!",
1652 Conf_PingTimeout = 5;
1656 if (strcasecmp(Var, "PongTimeout") == 0) {
1657 Conf_PongTimeout = atoi(Arg);
1658 if (Conf_PongTimeout < 5) {
1659 Config_Error(LOG_WARNING,
1660 "%s, line %d: Value of \"PongTimeout\" too low!",
1662 Conf_PongTimeout = 5;
1667 Config_Error_Section(File, Line, Var, "Limits");
1671 * Handle variable in [Options] configuration section.
1673 * @param Line Line numer in configuration file.
1674 * @param Var Variable name.
1675 * @param Arg Variable argument.
1678 Handle_OPTIONS(const char *File, int Line, char *Var, char *Arg)
1683 assert(File != NULL);
1685 assert(Var != NULL);
1686 assert(Arg != NULL);
1688 if (strcasecmp(Var, "AllowedChannelTypes") == 0) {
1690 Conf_AllowedChannelTypes[0] = '\0';
1692 if (strchr(Conf_AllowedChannelTypes, *p)) {
1693 /* Prefix is already included; ignore it */
1698 if (strchr(CHANTYPES, *p)) {
1699 len = strlen(Conf_AllowedChannelTypes) + 1;
1700 assert(len < sizeof(Conf_AllowedChannelTypes));
1701 Conf_AllowedChannelTypes[len - 1] = *p;
1702 Conf_AllowedChannelTypes[len] = '\0';
1704 Config_Error(LOG_WARNING,
1705 "%s, line %d: Unknown channel prefix \"%c\" in \"AllowedChannelTypes\"!",
1712 if (strcasecmp(Var, "AllowRemoteOper") == 0) {
1713 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1716 if (strcasecmp(Var, "ChrootDir") == 0) {
1717 len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
1718 if (len >= sizeof(Conf_Chroot))
1719 Config_Error_TooLong(File, Line, Var);
1722 if (strcasecmp(Var, "CloakHost") == 0) {
1723 len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
1724 if (len >= sizeof(Conf_CloakHost))
1725 Config_Error_TooLong(File, Line, Var);
1728 if (strcasecmp(Var, "CloakHostModeX") == 0) {
1729 len = strlcpy(Conf_CloakHostModeX, Arg, sizeof(Conf_CloakHostModeX));
1730 if (len >= sizeof(Conf_CloakHostModeX))
1731 Config_Error_TooLong(File, Line, Var);
1734 if (strcasecmp(Var, "CloakHostSalt") == 0) {
1735 len = strlcpy(Conf_CloakHostSalt, Arg, sizeof(Conf_CloakHostSalt));
1736 if (len >= sizeof(Conf_CloakHostSalt))
1737 Config_Error_TooLong(File, Line, Var);
1740 if (strcasecmp(Var, "CloakUserToNick") == 0) {
1741 Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
1744 if (strcasecmp(Var, "ConnectIPv6") == 0) {
1745 Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
1746 WarnIPv6(File, Line);
1749 if (strcasecmp(Var, "ConnectIPv4") == 0) {
1750 Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
1753 if (strcasecmp(Var, "DefaultUserModes") == 0) {
1755 Conf_DefaultUserModes[0] = '\0';
1757 if (strchr(Conf_DefaultUserModes, *p)) {
1758 /* Mode is already included; ignore it */
1763 if (strchr(USERMODES, *p)) {
1764 len = strlen(Conf_DefaultUserModes) + 1;
1765 assert(len < sizeof(Conf_DefaultUserModes));
1766 Conf_DefaultUserModes[len - 1] = *p;
1767 Conf_DefaultUserModes[len] = '\0';
1769 Config_Error(LOG_WARNING,
1770 "%s, line %d: Unknown user mode \"%c\" in \"DefaultUserModes\"!",
1777 if (strcasecmp(Var, "DNS") == 0) {
1778 Conf_DNS = Check_ArgIsTrue(Arg);
1781 if (strcasecmp(Var, "Ident") == 0) {
1782 Conf_Ident = Check_ArgIsTrue(Arg);
1783 WarnIdent(File, Line);
1786 if (strcasecmp(Var, "IncludeDir") == 0) {
1787 if (Conf_IncludeDir[0]) {
1788 Config_Error(LOG_ERR,
1789 "%s, line %d: Can't overwrite value of \"IncludeDir\" variable!",
1793 len = strlcpy(Conf_IncludeDir, Arg, sizeof(Conf_IncludeDir));
1794 if (len >= sizeof(Conf_IncludeDir))
1795 Config_Error_TooLong(File, Line, Var);
1798 if (strcasecmp(Var, "MorePrivacy") == 0) {
1799 Conf_MorePrivacy = Check_ArgIsTrue(Arg);
1802 if (strcasecmp(Var, "NoticeAuth") == 0) {
1803 Conf_NoticeAuth = Check_ArgIsTrue(Arg);
1806 if (strcasecmp(Var, "OperCanUseMode") == 0) {
1807 Conf_OperCanMode = Check_ArgIsTrue(Arg);
1810 if (strcasecmp(Var, "OperChanPAutoOp") == 0) {
1811 Conf_OperChanPAutoOp = Check_ArgIsTrue(Arg);
1814 if (strcasecmp(Var, "OperServerMode") == 0) {
1815 Conf_OperServerMode = Check_ArgIsTrue(Arg);
1818 if (strcasecmp(Var, "PAM") == 0) {
1819 Conf_PAM = Check_ArgIsTrue(Arg);
1820 WarnPAM(File, Line);
1823 if (strcasecmp(Var, "PAMIsOptional") == 0 ) {
1824 Conf_PAMIsOptional = Check_ArgIsTrue(Arg);
1827 if (strcasecmp(Var, "PredefChannelsOnly") == 0) {
1829 * TODO: This section and support for "PredefChannelsOnly"
1830 * could be removed starting with ngIRCd release 22 (one
1831 * release after marking it "deprecated") ...
1833 Config_Error(LOG_WARNING,
1834 "%s, line %d (section \"Options\"): \"%s\" is deprecated, please use \"AllowedChannelTypes\"!",
1836 if (Check_ArgIsTrue(Arg))
1837 Conf_AllowedChannelTypes[0] = '\0';
1839 strlcpy(Conf_AllowedChannelTypes, CHANTYPES,
1840 sizeof(Conf_AllowedChannelTypes));
1844 if (strcasecmp(Var, "RequireAuthPing") == 0) {
1845 Conf_AuthPing = Check_ArgIsTrue(Arg);
1849 if (strcasecmp(Var, "ScrubCTCP") == 0) {
1850 Conf_ScrubCTCP = Check_ArgIsTrue(Arg);
1854 if (strcasecmp(Var, "SyslogFacility") == 0) {
1855 Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
1856 Conf_SyslogFacility);
1860 if (strcasecmp(Var, "WebircPassword") == 0) {
1861 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
1862 if (len >= sizeof(Conf_WebircPwd))
1863 Config_Error_TooLong(File, Line, Var);
1867 Config_Error_Section(File, Line, Var, "Options");
1873 * Handle variable in [SSL] configuration section.
1875 * @param Line Line numer in configuration file.
1876 * @param Var Variable name.
1877 * @param Arg Variable argument.
1880 Handle_SSL(const char *File, int Line, char *Var, char *Arg)
1882 assert(File != NULL);
1884 assert(Var != NULL);
1885 assert(Arg != NULL);
1887 if (strcasecmp(Var, "CertFile") == 0) {
1888 assert(Conf_SSLOptions.CertFile == NULL);
1889 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1892 if (strcasecmp(Var, "DHFile") == 0) {
1893 assert(Conf_SSLOptions.DHFile == NULL);
1894 Conf_SSLOptions.DHFile = strdup_warn(Arg);
1897 if (strcasecmp(Var, "KeyFile") == 0) {
1898 assert(Conf_SSLOptions.KeyFile == NULL);
1899 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1902 if (strcasecmp(Var, "KeyFilePassword") == 0) {
1903 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1904 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1905 Config_Error(LOG_ERR,
1906 "%s, line %d (section \"SSL\"): Could not copy %s: %s!",
1907 File, Line, Var, strerror(errno));
1910 if (strcasecmp(Var, "Ports") == 0) {
1911 ports_parse(&Conf_SSLOptions.ListenPorts, File, Line, Arg);
1914 if (strcasecmp(Var, "CipherList") == 0) {
1915 assert(Conf_SSLOptions.CipherList == NULL);
1916 Conf_SSLOptions.CipherList = strdup_warn(Arg);
1920 Config_Error_Section(File, Line, Var, "SSL");
1926 * Handle variable in [Operator] configuration section.
1928 * @param Line Line numer in configuration file.
1929 * @param Var Variable name.
1930 * @param Arg Variable argument.
1933 Handle_OPERATOR(const char *File, int Line, char *Var, char *Arg )
1936 struct Conf_Oper *op;
1938 assert( File != NULL );
1940 assert( Var != NULL );
1941 assert( Arg != NULL );
1943 op = array_get(&Conf_Opers, sizeof(*op),
1944 array_length(&Conf_Opers, sizeof(*op)) - 1);
1948 if (strcasecmp(Var, "Name") == 0) {
1949 /* Name of IRC operator */
1950 len = strlcpy(op->name, Arg, sizeof(op->name));
1951 if (len >= sizeof(op->name))
1952 Config_Error_TooLong(File, Line, Var);
1955 if (strcasecmp(Var, "Password") == 0) {
1956 /* Password of IRC operator */
1957 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1958 if (len >= sizeof(op->pwd))
1959 Config_Error_TooLong(File, Line, Var);
1962 if (strcasecmp(Var, "Mask") == 0) {
1964 return; /* Hostname already configured */
1965 op->mask = strdup_warn( Arg );
1969 Config_Error_Section(File, Line, Var, "Operator");
1973 * Handle variable in [Server] configuration section.
1975 * @param Line Line numer in configuration file.
1976 * @param Var Variable name.
1977 * @param Arg Variable argument.
1980 Handle_SERVER(const char *File, int Line, char *Var, char *Arg )
1985 assert( File != NULL );
1987 assert( Var != NULL );
1988 assert( Arg != NULL );
1990 /* Ignore server block if no space is left in server configuration structure */
1991 if( New_Server_Idx <= NONE ) return;
1993 if( strcasecmp( Var, "Host" ) == 0 ) {
1994 /* Hostname of the server */
1995 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1996 if (len >= sizeof( New_Server.host ))
1997 Config_Error_TooLong(File, Line, Var);
2000 if( strcasecmp( Var, "Name" ) == 0 ) {
2001 /* Name of the server ("Nick"/"ID") */
2002 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
2003 if (len >= sizeof( New_Server.name ))
2004 Config_Error_TooLong(File, Line, Var);
2007 if (strcasecmp(Var, "Bind") == 0) {
2008 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
2011 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
2015 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
2016 /* Password of this server which is sent to the peer */
2018 Config_Error(LOG_ERR,
2019 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
2022 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
2023 if (len >= sizeof( New_Server.pwd_in ))
2024 Config_Error_TooLong(File, Line, Var);
2027 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
2028 /* Passwort of the peer which must be received */
2029 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
2030 if (len >= sizeof( New_Server.pwd_out ))
2031 Config_Error_TooLong(File, Line, Var);
2034 if( strcasecmp( Var, "Port" ) == 0 ) {
2035 /* Port to which this server should connect */
2037 if (port >= 0 && port < 0xFFFF)
2038 New_Server.port = (UINT16)port;
2040 Config_Error(LOG_ERR,
2041 "%s, line %d (section \"Server\"): Illegal port number %ld!",
2046 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
2047 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
2051 if( strcasecmp( Var, "Group" ) == 0 ) {
2053 New_Server.group = atoi( Arg );
2054 if (!New_Server.group && strcmp(Arg, "0"))
2055 Config_Error_NaN(File, Line, Var);
2058 if( strcasecmp( Var, "Passive" ) == 0 ) {
2059 if (Check_ArgIsTrue(Arg))
2060 New_Server.flags |= CONF_SFLAG_DISABLED;
2063 if (strcasecmp(Var, "ServiceMask") == 0) {
2064 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
2065 sizeof(New_Server.svs_mask));
2066 if (len >= sizeof(New_Server.svs_mask))
2067 Config_Error_TooLong(File, Line, Var);
2071 Config_Error_Section(File, Line, Var, "Server");
2075 * Copy channel name into channel structure.
2077 * If the channel name is not valid because of a missing prefix ('#', '&'),
2078 * a default prefix of '#' will be added.
2080 * @param new_chan New already allocated channel structure.
2081 * @param name Name of the new channel.
2082 * @returns true on success, false otherwise.
2085 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
2087 size_t size = sizeof(new_chan->name);
2088 char *dest = new_chan->name;
2090 if (!Channel_IsValidName(name)) {
2092 * maybe user forgot to add a '#'.
2093 * This is only here for user convenience.
2099 return size > strlcpy(dest, name, size);
2103 * Handle variable in [Channel] configuration section.
2105 * @param Line Line numer in configuration file.
2106 * @param Var Variable name.
2107 * @param Arg Variable argument.
2110 Handle_CHANNEL(const char *File, int Line, char *Var, char *Arg)
2113 struct Conf_Channel *chan;
2115 assert( File != NULL );
2117 assert( Var != NULL );
2118 assert( Arg != NULL );
2120 chan = array_get(&Conf_Channels, sizeof(*chan),
2121 array_length(&Conf_Channels, sizeof(*chan)) - 1);
2125 if (strcasecmp(Var, "Name") == 0) {
2126 if (!Handle_Channelname(chan, Arg))
2127 Config_Error_TooLong(File, Line, Var);
2130 if (strcasecmp(Var, "Modes") == 0) {
2132 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
2133 if (len >= sizeof(chan->modes))
2134 Config_Error_TooLong(File, Line, Var);
2137 if( strcasecmp( Var, "Topic" ) == 0 ) {
2139 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
2140 if (len >= sizeof(chan->topic))
2141 Config_Error_TooLong(File, Line, Var);
2144 if( strcasecmp( Var, "Key" ) == 0 ) {
2145 /* Initial Channel Key (mode k) */
2146 len = strlcpy(chan->key, Arg, sizeof(chan->key));
2147 if (len >= sizeof(chan->key))
2148 Config_Error_TooLong(File, Line, Var);
2151 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
2152 /* maximum user limit, mode l */
2153 chan->maxusers = (unsigned long) atol(Arg);
2154 if (!chan->maxusers && strcmp(Arg, "0"))
2155 Config_Error_NaN(File, Line, Var);
2158 if (strcasecmp(Var, "KeyFile") == 0) {
2160 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
2161 if (len >= sizeof(chan->keyfile))
2162 Config_Error_TooLong(File, Line, Var);
2166 Config_Error_Section(File, Line, Var, "Channel");
2170 * Validate server configuration.
2172 * Please note that this function uses exit(1) on fatal errors and therefore
2173 * can result in ngIRCd terminating!
2175 * @param Configtest true if the daemon has been called with "--configtest".
2176 * @param Rehash true if re-reading configuration on runtime.
2177 * @returns true if configuration is valid.
2180 Validate_Config(bool Configtest, bool Rehash)
2182 /* Validate configuration settings. */
2185 int i, servers, servers_once;
2187 bool config_valid = true;
2190 /* Emit a warning when the config file is not a full path name */
2191 if (NGIRCd_ConfFile[0] && NGIRCd_ConfFile[0] != '/') {
2192 Config_Error(LOG_WARNING,
2193 "Not specifying a full path name to \"%s\" can cause problems when rehashing the server!",
2197 /* Validate configured server name, see RFC 2812 section 2.3.1 */
2198 ptr = Conf_ServerName;
2200 if (*ptr >= 'a' && *ptr <= 'z') continue;
2201 if (*ptr >= 'A' && *ptr <= 'Z') continue;
2202 if (*ptr >= '0' && *ptr <= '9') continue;
2203 if (ptr > Conf_ServerName) {
2204 if (*ptr == '.' || *ptr == '-')
2207 Conf_ServerName[0] = '\0';
2211 if (!Conf_ServerName[0]) {
2212 /* No server name configured! */
2213 config_valid = false;
2214 Config_Error(LOG_ALERT,
2215 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
2217 if (!Configtest && !Rehash) {
2218 Config_Error(LOG_ALERT,
2219 "%s exiting due to fatal errors!",
2225 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
2226 /* No dot in server name! */
2227 config_valid = false;
2228 Config_Error(LOG_ALERT,
2229 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
2232 Config_Error(LOG_ALERT,
2233 "%s exiting due to fatal errors!",
2240 if (!Conf_ServerAdminMail[0]) {
2241 /* No administrative contact configured! */
2242 config_valid = false;
2243 Config_Error(LOG_ALERT,
2244 "No administrator email address configured in \"%s\" ('AdminEMail')!",
2247 Config_Error(LOG_ALERT,
2248 "%s exiting due to fatal errors!",
2255 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
2256 && !Conf_ServerAdminMail[0]) {
2257 /* No administrative information configured! */
2258 Config_Error(LOG_WARNING,
2259 "No administrative information configured but required by RFC!");
2263 if (Conf_PAM && Conf_ServerPwd[0])
2264 Config_Error(LOG_ERR,
2265 "This server uses PAM, \"Password\" in [Global] section will be ignored!");
2269 servers = servers_once = 0;
2270 for (i = 0; i < MAX_SERVERS; i++) {
2271 if (Conf_Server[i].name[0]) {
2273 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
2278 "Configuration: Operators=%ld, Servers=%d[%d], Channels=%ld",
2279 array_length(&Conf_Opers, sizeof(struct Conf_Oper)),
2280 servers, servers_once,
2281 array_length(&Conf_Channels, sizeof(struct Conf_Channel)));
2284 return config_valid;
2288 * Output "line too long" warning.
2290 * @param Line Line number in configuration file.
2291 * @param Item Affected variable name.
2294 Config_Error_TooLong(const char *File, const int Line, const char *Item)
2296 Config_Error(LOG_WARNING, "%s, line %d: Value of \"%s\" too long!",
2301 * Output "unknown variable" warning.
2303 * @param Line Line number in configuration file.
2304 * @param Item Affected variable name.
2305 * @param Section Section name.
2308 Config_Error_Section(const char *File, const int Line, const char *Item,
2309 const char *Section)
2311 Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
2312 File, Line, Section, Item);
2316 * Output "not a number" warning.
2318 * @param Line Line number in configuration file.
2319 * @param Item Affected variable name.
2322 Config_Error_NaN(const char *File, const int Line, const char *Item )
2324 Config_Error(LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
2329 * Output configuration error to console and/or logfile.
2331 * On runtime, the normal log functions of the daemon are used. But when
2332 * testing the configuration ("--configtest"), all messages go directly
2335 * @param Level Severity level of the message.
2336 * @param Format Format string; see printf() function.
2339 static void Config_Error( const int Level, const char *Format, ... )
2341 static void Config_Error( Level, Format, va_alist )
2347 char msg[MAX_LOG_MSG_LEN];
2350 assert( Format != NULL );
2353 va_start( ap, Format );
2357 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
2361 if (Level <= LOG_WARNING)
2362 printf(" - %s\n", msg);
2366 Log(Level, "%s", msg);
2372 * Dump internal state of the "configuration module".
2375 Conf_DebugDump(void)
2379 Log(LOG_DEBUG, "Configured servers:");
2380 for (i = 0; i < MAX_SERVERS; i++) {
2381 if (! Conf_Server[i].name[0])
2384 " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
2385 Conf_Server[i].name, Conf_Server[i].host,
2386 Conf_Server[i].port, Conf_Server[i].lasttry,
2387 Conf_Server[i].group, Conf_Server[i].flags,
2388 Conf_Server[i].conn_id);
2395 * Initialize server configuration structure to default values.
2397 * @param Server Pointer to server structure to initialize.
2400 Init_Server_Struct( CONF_SERVER *Server )
2402 assert( Server != NULL );
2404 memset( Server, 0, sizeof (CONF_SERVER) );
2406 Server->group = NONE;
2407 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
2409 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
2411 Proc_InitStruct(&Server->res_stat);
2412 Server->conn_id = NONE;
2413 memset(&Server->bind_addr, 0, sizeof(Server->bind_addr));