2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2024 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>
47 static bool Use_Log = true, Using_MotdFile = true;
48 static CONF_SERVER New_Server;
49 static int New_Server_Idx;
51 static char Conf_MotdFile[FNAME_LEN];
52 static char Conf_HelpFile[FNAME_LEN];
53 static char Conf_IncludeDir[FNAME_LEN];
55 static void Set_Defaults PARAMS(( bool InitServers ));
56 static bool Read_Config PARAMS(( bool TestOnly, bool IsStarting ));
57 static void Read_Config_File PARAMS(( const char *File, FILE *fd ));
58 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
60 static void Handle_GLOBAL PARAMS((const char *File, int Line,
61 char *Var, char *Arg ));
62 static void Handle_LIMITS PARAMS((const char *File, int Line,
63 char *Var, char *Arg ));
64 static void Handle_OPTIONS PARAMS((const char *File, int Line,
65 char *Var, char *Arg ));
66 static void Handle_OPERATOR PARAMS((const char *File, int Line,
67 char *Var, char *Arg ));
68 static void Handle_SERVER PARAMS((const char *File, int Line,
69 char *Var, char *Arg ));
70 static void Handle_CHANNEL PARAMS((const char *File, int Line,
71 char *Var, char *Arg ));
73 static void Config_Error PARAMS((const int Level, const char *Format, ...));
75 static void Config_Error_NaN PARAMS((const char *File, const int LINE,
77 static void Config_Error_Section PARAMS((const char *File, const int Line,
78 const char *Item, const char *Section));
79 static void Config_Error_TooLong PARAMS((const char *File, const int LINE,
82 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
86 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
88 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
92 #define DEFAULT_CIPHERS "HIGH:!aNULL:@STRENGTH:!SSLv3"
95 #define DEFAULT_CIPHERS "SECURE128:-VERS-SSL3.0"
100 static void Handle_SSL PARAMS((const char *File, int Line, char *Var, char *Ark));
102 struct SSLOptions Conf_SSLOptions;
105 * Initialize SSL configuration.
110 free(Conf_SSLOptions.KeyFile);
111 Conf_SSLOptions.KeyFile = NULL;
113 free(Conf_SSLOptions.CertFile);
114 Conf_SSLOptions.CertFile = NULL;
116 free(Conf_SSLOptions.CAFile);
117 Conf_SSLOptions.CAFile = NULL;
119 free(Conf_SSLOptions.CRLFile);
120 Conf_SSLOptions.CRLFile = NULL;
122 free(Conf_SSLOptions.DHFile);
123 Conf_SSLOptions.DHFile = NULL;
124 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
126 array_free(&Conf_SSLOptions.ListenPorts);
128 free(Conf_SSLOptions.CipherList);
129 Conf_SSLOptions.CipherList = NULL;
133 * Check if the current configuration uses/requires SSL.
135 * @returns true if SSL is used and should be initialized.
142 /* SSL listen ports configured? */
143 if (array_bytes(&Conf_SSLOptions.ListenPorts))
146 for (i = 0; i < MAX_SERVERS; i++) {
147 if (Conf_Server[i].port > 0
148 && Conf_Server[i].SSLConnect)
155 * Make sure that a configured file is readable.
157 * Currently, this function is only used for SSL-related options ...
159 * @param Var Configuration variable
160 * @param Filename Configured filename
163 CheckFileReadable(const char *Var, const char *Filename)
170 fp = fopen(Filename, "r");
174 Config_Error(LOG_ERR, "Can't read \"%s\" (\"%s\"): %s",
175 Filename, Var, strerror(errno));
182 * Duplicate string and warn on errors.
184 * @returns Pointer to string on success, NULL otherwise.
187 strdup_warn(const char *str)
189 char *ptr = strdup(str);
191 Config_Error(LOG_ERR,
192 "Could not allocate memory for string: %s", str);
197 * Output a comma separated list of ports (integer values).
204 len = array_length(a, sizeof(UINT16));
206 ports = (UINT16*) array_start(a);
207 printf("%u", (unsigned int) *ports);
210 printf(", %u", (unsigned int) *ports);
217 * Parse a comma separated string into an array of port numbers (integers).
220 ports_parse(array *a, const char *File, int Line, char *Arg)
228 ptr = strtok( Arg, "," );
232 if (port > 0 && port < 0xFFFF) {
233 port16 = (UINT16) port;
234 if (!array_catb(a, (char*)&port16, sizeof port16))
235 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
236 File, Line, port, strerror(errno));
238 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
242 ptr = strtok( NULL, "," );
247 * Initialize configuration module.
252 Read_Config(false, true);
253 Validate_Config(false, false);
257 * "Rehash" (reload) server configuration.
259 * @returns true if configuration has been re-read, false on errors.
264 if (!Read_Config(false, false))
266 Validate_Config(false, true);
268 /* Update CLIENT structure of local server */
269 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
274 * Output a boolean value as "yes/no" string.
277 yesno_to_str(int boolean_value)
285 * Free all IRC operator configuration structures.
290 struct Conf_Oper *op;
293 len = array_length(&Conf_Opers, sizeof(*op));
294 op = array_start(&Conf_Opers);
299 array_free(&Conf_Opers);
303 * Output all IRC operator configuration structures.
308 struct Conf_Oper *op;
311 count = array_length(&Conf_Opers, sizeof(*op));
312 op = array_start(&Conf_Opers);
313 for (i = 0; i < count; i++, op++) {
318 printf(" Name = %s\n", op->name);
319 printf(" Password = %s\n", op->pwd);
320 printf(" Mask = %s\n\n", op->mask ? op->mask : "");
325 * Read configuration, validate and output it.
327 * This function waits for a keypress of the user when stdin/stdout are valid
328 * tty's ("you can read our nice message and we can read in your keypress").
330 * @return 0 on success, 1 on failure(s); therefore the result code can
331 * directly be used by exit() when running "ngircd --configtest".
340 size_t predef_channel_count;
341 struct Conf_Channel *predef_chan;
345 if (!Read_Config(true, true))
348 config_valid = Validate_Config(true, false);
351 if(isatty(fileno(stdin)) && isatty(fileno(stdout))) {
352 puts("OK, press enter to see a dump of your server configuration ...");
355 puts("Ok, dump of your server configuration follows:\n");
358 printf(" Name = %s\n", Conf_ServerName);
359 printf(" AdminInfo1 = %s\n", Conf_ServerAdmin1);
360 printf(" AdminInfo2 = %s\n", Conf_ServerAdmin2);
361 printf(" AdminEMail = %s\n", Conf_ServerAdminMail);
362 printf(" HelpFile = %s\n", Conf_HelpFile);
363 printf(" Info = %s\n", Conf_ServerInfo);
364 printf(" Listen = %s\n", Conf_ListenAddress);
365 if (Using_MotdFile) {
366 printf(" MotdFile = %s\n", Conf_MotdFile);
367 printf(" MotdPhrase =\n");
369 printf(" MotdFile = \n");
370 printf(" MotdPhrase = %s\n", array_bytes(&Conf_Motd)
371 ? (const char*) array_start(&Conf_Motd) : "");
373 printf(" Network = %s\n", Conf_Network);
375 printf(" Password = %s\n", Conf_ServerPwd);
376 printf(" PidFile = %s\n", Conf_PidFile);
378 ports_puts(&Conf_ListenPorts);
379 grp = getgrgid(Conf_GID);
381 printf(" ServerGID = %s\n", grp->gr_name);
383 printf(" ServerGID = %ld\n", (long)Conf_GID);
384 pwd = getpwuid(Conf_UID);
386 printf(" ServerUID = %s\n", pwd->pw_name);
388 printf(" ServerUID = %ld\n", (long)Conf_UID);
392 printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
393 printf(" IdleTimeout = %d\n", Conf_IdleTimeout);
394 printf(" MaxConnections = %d\n", Conf_MaxConnections);
395 printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
396 printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
397 printf(" MaxNickLength = %u\n", Conf_MaxNickLength - 1);
398 printf(" MaxPenaltyTime = %ld\n", (long)Conf_MaxPenaltyTime);
399 printf(" MaxListSize = %d\n", Conf_MaxListSize);
400 printf(" PingTimeout = %d\n", Conf_PingTimeout);
401 printf(" PongTimeout = %d\n", Conf_PongTimeout);
405 printf(" AllowedChannelTypes = %s\n", Conf_AllowedChannelTypes);
406 printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
407 printf(" ChrootDir = %s\n", Conf_Chroot);
408 printf(" CloakHost = %s\n", Conf_CloakHost);
409 printf(" CloakHostModeX = %s\n", Conf_CloakHostModeX);
410 printf(" CloakHostSalt = %s\n", Conf_CloakHostSalt);
411 printf(" CloakUserToNick = %s\n", yesno_to_str(Conf_CloakUserToNick));
413 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
414 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
416 printf(" DefaultUserModes = %s\n", Conf_DefaultUserModes);
417 printf(" DNS = %s\n", yesno_to_str(Conf_DNS));
419 printf(" Ident = %s\n", yesno_to_str(Conf_Ident));
421 printf(" IncludeDir = %s\n", Conf_IncludeDir);
422 printf(" MorePrivacy = %s\n", yesno_to_str(Conf_MorePrivacy));
423 printf(" NoticeBeforeRegistration = %s\n", yesno_to_str(Conf_NoticeBeforeRegistration));
424 printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
425 printf(" OperChanPAutoOp = %s\n", yesno_to_str(Conf_OperChanPAutoOp));
426 printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
428 printf(" PAM = %s\n", yesno_to_str(Conf_PAM));
429 printf(" PAMIsOptional = %s\n", yesno_to_str(Conf_PAMIsOptional));
430 printf(" PAMServiceName = %s\n", Conf_PAMServiceName);
433 printf(" RequireAuthPing = %s\n", yesno_to_str(Conf_AuthPing));
435 printf(" ScrubCTCP = %s\n", yesno_to_str(Conf_ScrubCTCP));
437 printf(" SyslogFacility = %s\n",
438 ngt_SyslogFacilityName(Conf_SyslogFacility));
440 printf(" WebircPassword = %s\n", Conf_WebircPwd);
445 printf(" CertFile = %s\n", Conf_SSLOptions.CertFile
446 ? Conf_SSLOptions.CertFile : "");
447 printf(" CipherList = %s\n", Conf_SSLOptions.CipherList ?
448 Conf_SSLOptions.CipherList : DEFAULT_CIPHERS);
449 printf(" DHFile = %s\n", Conf_SSLOptions.DHFile
450 ? Conf_SSLOptions.DHFile : "");
451 printf(" KeyFile = %s\n", Conf_SSLOptions.KeyFile
452 ? Conf_SSLOptions.KeyFile : "");
453 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
454 puts(" KeyFilePassword = <secret>");
456 puts(" KeyFilePassword = ");
457 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
459 ports_puts(&Conf_SSLOptions.ListenPorts);
465 for( i = 0; i < MAX_SERVERS; i++ ) {
466 if( ! Conf_Server[i].name[0] ) continue;
468 /* Valid "Server" section */
470 printf( " Name = %s\n", Conf_Server[i].name );
471 printf( " Host = %s\n", Conf_Server[i].host );
472 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
474 printf(" SSLConnect = %s\n",
475 yesno_to_str(Conf_Server[i].SSLConnect));
476 printf(" SSLVerify = %s\n",
477 yesno_to_str(Conf_Server[i].SSLVerify));
479 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
480 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
481 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
482 printf( " Group = %d\n", Conf_Server[i].group );
483 printf( " Passive = %s\n\n", yesno_to_str(Conf_Server[i].flags & CONF_SFLAG_DISABLED));
486 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
487 predef_chan = array_start(&Conf_Channels);
489 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
490 if (!predef_chan->name[0])
493 /* Valid "Channel" section */
495 printf(" Name = %s\n", predef_chan->name);
496 for(j = 0; j < predef_chan->modes_num; j++)
497 printf(" Modes = %s\n", predef_chan->modes[j]);
498 printf(" Key = %s\n", predef_chan->key);
499 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
500 printf(" Topic = %s\n", predef_chan->topic);
501 printf(" Autojoin = %s\n", yesno_to_str(predef_chan->autojoin));
502 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
505 return (config_valid ? 0 : 1);
509 * Remove connection information from configured server.
511 * If the server is set as "once", delete it from our configuration;
512 * otherwise set the time for the next connection attempt.
514 * Non-server connections will be silently ignored.
517 Conf_UnsetServer( CONN_ID Idx )
522 /* Check all our configured servers */
523 for( i = 0; i < MAX_SERVERS; i++ ) {
524 if( Conf_Server[i].conn_id != Idx ) continue;
526 /* Gotcha! Mark server configuration as "unused": */
527 Conf_Server[i].conn_id = NONE;
529 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
530 /* Delete configuration here */
531 Init_Server_Struct( &Conf_Server[i] );
533 /* Set time for next connect attempt */
535 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
536 /* The connection has been "long", so we don't
537 * require the next attempt to be delayed. */
538 Conf_Server[i].lasttry =
539 t - Conf_ConnectRetry + RECONNECT_DELAY;
541 /* "Short" connection, enforce "ConnectRetry"
542 * but randomize it a little bit: 15 seconds. */
543 Conf_Server[i].lasttry =
544 #ifdef HAVE_ARC4RANDOM
545 t + (arc4random() % 15);
547 t + rand() / (RAND_MAX / 15);
555 * Set connection information for specified configured server.
558 Conf_SetServer( int ConfServer, CONN_ID Idx )
560 assert( ConfServer > NONE );
561 assert( Idx > NONE );
563 if (Conf_Server[ConfServer].conn_id > NONE &&
564 Conf_Server[ConfServer].conn_id != Idx) {
566 "Connection %d: Server configuration of \"%s\" already in use by connection %d!",
567 Idx, Conf_Server[ConfServer].name,
568 Conf_Server[ConfServer].conn_id);
569 Conn_Close(Idx, NULL, "Server configuration already in use", true);
572 Conf_Server[ConfServer].conn_id = Idx;
577 * Get index of server in configuration structure.
580 Conf_GetServer( CONN_ID Idx )
584 assert( Idx > NONE );
586 for( i = 0; i < MAX_SERVERS; i++ ) {
587 if( Conf_Server[i].conn_id == Idx ) return i;
593 * Enable a server by name and adjust its port number.
595 * @returns true if a server has been enabled and now has a valid port
596 * number and host name for outgoing connections.
599 Conf_EnableServer( const char *Name, UINT16 Port )
603 assert( Name != NULL );
604 for( i = 0; i < MAX_SERVERS; i++ ) {
605 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
606 /* Gotcha! Set port and enable server: */
607 Conf_Server[i].port = Port;
608 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
609 return (Conf_Server[i].port && Conf_Server[i].host[0]);
616 * Enable a server by name.
618 * The server is only usable as outgoing server, if it has set a valid port
619 * number for outgoing connections!
620 * If not, you have to use Conf_EnableServer() function to make it available.
622 * @returns true if a server has been enabled; false otherwise.
625 Conf_EnablePassiveServer(const char *Name)
629 assert( Name != NULL );
630 for (i = 0; i < MAX_SERVERS; i++) {
631 if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
632 && (Conf_Server[i].port > 0)) {
633 /* BINGO! Enable server */
634 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
635 Conf_Server[i].lasttry = 0;
643 * Disable a server by name.
644 * An already established connection will be disconnected.
646 * @returns true if a server was found and has been disabled.
649 Conf_DisableServer( const char *Name )
653 assert( Name != NULL );
654 for( i = 0; i < MAX_SERVERS; i++ ) {
655 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
656 /* Gotcha! Disable and disconnect server: */
657 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
658 if( Conf_Server[i].conn_id > NONE )
659 Conn_Close(Conf_Server[i].conn_id, NULL,
660 "Server link terminated on operator request",
669 * Add a new remote server to our configuration.
671 * @param Name Name of the new server.
672 * @param Port Port number to connect to or 0 for incoming connections.
673 * @param Host Host name to connect to.
674 * @param MyPwd Password that will be sent to the peer.
675 * @param PeerPwd Password that must be received from the peer.
676 * @returns true if the new server has been added; false otherwise.
679 Conf_AddServer(const char *Name, UINT16 Port, const char *Host,
680 const char *MyPwd, const char *PeerPwd)
684 assert( Name != NULL );
685 assert( Host != NULL );
686 assert( MyPwd != NULL );
687 assert( PeerPwd != NULL );
689 /* Search unused item in server configuration structure */
690 for( i = 0; i < MAX_SERVERS; i++ ) {
691 /* Is this item used? */
692 if( ! Conf_Server[i].name[0] ) break;
694 if( i >= MAX_SERVERS ) return false;
696 Init_Server_Struct( &Conf_Server[i] );
697 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
698 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
699 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
700 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
701 Conf_Server[i].port = Port;
702 Conf_Server[i].flags = CONF_SFLAG_ONCE;
708 * Check if the given nickname is reserved for services on a particular server.
710 * @param ConfServer The server index to check.
711 * @param Nick The nickname to check.
712 * @returns true if the given nickname belongs to an "IRC service".
715 Conf_NickIsService(int ConfServer, const char *Nick)
717 assert (ConfServer >= 0);
718 assert (ConfServer < MAX_SERVERS);
720 return MatchCaseInsensitiveList(Conf_Server[ConfServer].svs_mask,
725 * Check if the given nickname is blocked for "normal client" use.
727 * @param Nick The nickname to check.
728 * @returns true if the given nickname belongs to an "IRC service".
731 Conf_NickIsBlocked(const char *Nick)
735 for(i = 0; i < MAX_SERVERS; i++) {
736 if (!Conf_Server[i].name[0])
738 if (Conf_NickIsService(i, Nick))
745 * Initialize configuration settings with their default values.
748 Set_Defaults(bool InitServers)
751 char random[RANDOM_SALT_LEN + 1];
754 strcpy(Conf_ServerName, "");
755 strcpy(Conf_ServerAdmin1, "");
756 strcpy(Conf_ServerAdmin2, "");
757 strcpy(Conf_ServerAdminMail, "");
758 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
759 PACKAGE_NAME, PACKAGE_VERSION);
760 strcpy(Conf_Network, "");
761 free(Conf_ListenAddress);
762 Conf_ListenAddress = NULL;
763 array_free(&Conf_ListenPorts);
764 array_free(&Conf_Motd);
765 array_free(&Conf_Helptext);
766 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
767 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
768 strlcpy(Conf_HelpFile, DOCDIR, sizeof(Conf_HelpFile));
769 strlcat(Conf_HelpFile, HELP_FILE, sizeof(Conf_HelpFile));
770 strcpy(Conf_ServerPwd, "");
771 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
772 Conf_UID = Conf_GID = 0;
775 Conf_ConnectRetry = 60;
776 Conf_IdleTimeout = 0;
777 Conf_MaxConnections = 0;
778 Conf_MaxConnectionsIP = 5;
780 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
781 Conf_MaxPenaltyTime = -1;
782 Conf_MaxListSize = 100;
783 Conf_PingTimeout = 120;
784 Conf_PongTimeout = 20;
787 strlcpy(Conf_AllowedChannelTypes, CHANTYPES,
788 sizeof(Conf_AllowedChannelTypes));
789 Conf_AllowRemoteOper = false;
791 Conf_AuthPing = false;
793 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
794 strcpy(Conf_CloakHost, "");
795 strcpy(Conf_CloakHostModeX, "");
796 strlcpy(Conf_CloakHostSalt, ngt_RandomStr(random, RANDOM_SALT_LEN),
797 sizeof(Conf_CloakHostSalt));
798 Conf_CloakUserToNick = false;
799 Conf_ConnectIPv4 = true;
801 Conf_ConnectIPv6 = true;
803 Conf_ConnectIPv6 = false;
805 strcpy(Conf_DefaultUserModes, "");
812 strcpy(Conf_IncludeDir, "");
813 Conf_MorePrivacy = false;
814 Conf_NoticeBeforeRegistration = false;
815 Conf_OperCanMode = false;
816 Conf_OperChanPAutoOp = true;
817 Conf_OperServerMode = false;
823 Conf_PAMIsOptional = false;
824 strcpy(Conf_PAMServiceName, "ngircd");
825 Conf_ScrubCTCP = false;
828 Conf_SyslogFacility = LOG_LOCAL5;
830 Conf_SyslogFacility = 0;
834 /* Initialize server configuration structures */
836 for (i = 0; i < MAX_SERVERS;
837 Init_Server_Struct(&Conf_Server[i++]));
842 * Get number of configured listening ports.
844 * @returns The number of ports (IPv4+IPv6) on which the server should listen.
849 size_t cnt = array_bytes(&Conf_ListenPorts);
851 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
857 * Read contents of a text file into an array.
859 * This function is used to read the MOTD and help text file, for example.
861 * @param Filename Name of the file to read.
862 * @return true, when the file has been read in.
865 Read_TextFile(const char *Filename, const char *Name, array *Destination)
867 char line[COMMAND_LEN];
871 if (*Filename == '\0')
874 fp = fopen(Filename, "r");
876 Config_Error(LOG_ERR, "Can't read %s file \"%s\": %s",
877 Name, Filename, strerror(errno));
881 array_free(Destination);
882 while (fgets(line, (int)sizeof line, fp)) {
883 ngt_TrimLastChr(line, '\n');
885 /* add text including \0 */
886 if (!array_catb(Destination, line, strlen(line) + 1)) {
887 Log(LOG_ERR, "Cannot read/add \"%s\", line %d: %s",
888 Filename, line_no, strerror(errno));
898 * Read ngIRCd configuration file.
900 * Please note that this function uses exit(1) on fatal errors and therefore
901 * can result in ngIRCd terminating!
903 * @param IsStarting Flag indicating if ngIRCd is starting or not.
904 * @returns true when the configuration file has been read
905 * successfully; false otherwise.
908 Read_Config(bool TestOnly, bool IsStarting)
910 const UINT16 defaultport = 6667;
911 char *ptr, file[FNAME_LEN];
912 struct dirent *entry;
917 if (!NGIRCd_ConfFile[0]) {
918 /* No configuration file name explicitly given on the command
919 * line, use defaults but ignore errors when this file can't be
921 strlcpy(file, SYSCONFDIR, sizeof(file));
922 strlcat(file, CONFIG_FILE, sizeof(file));
925 ptr = NGIRCd_ConfFile;
927 Config_Error(LOG_INFO, "Using %s configuration file \"%s\" ...",
928 !NGIRCd_ConfFile[0] ? "default" : "specified", ptr);
930 /* Open configuration file */
931 fd = fopen(ptr, "r");
933 if (NGIRCd_ConfFile[0]) {
934 Config_Error(LOG_ALERT,
935 "Can't read specified configuration file \"%s\": %s",
936 ptr, strerror(errno));
938 Config_Error(LOG_ALERT,
939 "%s exiting due to fatal errors!",
944 Config_Error(LOG_WARNING,
945 "Can't read default configuration file \"%s\": %s - Ignored.",
946 ptr, strerror(errno));
950 Set_Defaults(IsStarting);
953 Config_Error(LOG_INFO,
954 "Reading configuration from \"%s\" ...", ptr);
956 /* Clean up server configuration structure: mark all already
957 * configured servers as "once" so that they are deleted
958 * after the next disconnect and delete all unused servers.
959 * And delete all servers which are "duplicates" of servers
960 * that are already marked as "once" (such servers have been
961 * created by the last rehash but are now useless). */
962 for( i = 0; i < MAX_SERVERS; i++ ) {
963 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
965 /* This structure is in use ... */
966 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
967 /* Check for duplicates */
968 for( n = 0; n < MAX_SERVERS; n++ ) {
969 if( n == i ) continue;
971 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
972 Init_Server_Struct( &Conf_Server[n] );
973 LogDebug("Deleted unused duplicate server %d (kept %d).", n, i);
977 /* Mark server as "once" */
978 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
979 LogDebug("Marked server %d as \"once\"", i);
984 /* Initialize variables */
985 Init_Server_Struct( &New_Server );
986 New_Server_Idx = NONE;
992 Read_Config_File(ptr, fd);
996 if (Conf_IncludeDir[0]) {
997 /* Include directory was set in the main configuration file. So
998 * use it and show errors. */
999 dh = opendir(Conf_IncludeDir);
1001 Config_Error(LOG_ALERT,
1002 "Can't open include directory \"%s\": %s",
1003 Conf_IncludeDir, strerror(errno));
1004 } else if (!NGIRCd_ConfFile[0]) {
1005 /* No include dir set in the configuration file used (if any)
1006 * but no config file explicitly specified either: so use the
1007 * default include path here as well! */
1008 strlcpy(Conf_IncludeDir, SYSCONFDIR, sizeof(Conf_IncludeDir));
1009 strlcat(Conf_IncludeDir, CONFIG_DIR, sizeof(Conf_IncludeDir));
1010 dh = opendir(Conf_IncludeDir);
1013 /* Include further configuration files, if IncludeDir is available */
1015 while ((entry = readdir(dh)) != NULL) {
1016 ptr = strrchr(entry->d_name, '.');
1017 if (!ptr || strcasecmp(ptr, ".conf") != 0)
1019 snprintf(file, sizeof(file), "%s/%s",
1020 Conf_IncludeDir, entry->d_name);
1022 Config_Error(LOG_INFO,
1023 "Reading configuration from \"%s\" ...",
1025 fd = fopen(file, "r");
1027 Read_Config_File(file, fd);
1030 Config_Error(LOG_ALERT,
1031 "Can't read configuration \"%s\": %s",
1032 file, strerror(errno));
1037 /* Check if there is still a server to add */
1038 if( New_Server.name[0] ) {
1039 /* Copy data to "real" server structure */
1040 assert( New_Server_Idx > NONE );
1041 Conf_Server[New_Server_Idx] = New_Server;
1044 /* not a single listening port? Add default. */
1045 if (no_listenports() &&
1046 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
1048 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
1049 (unsigned int) defaultport, strerror(errno));
1054 if (!Conf_ListenAddress)
1055 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
1057 if (!Conf_ListenAddress) {
1058 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
1062 /* No MOTD phrase configured? (re)try motd file. */
1063 if (array_bytes(&Conf_Motd) == 0) {
1064 if (Read_TextFile(Conf_MotdFile, "MOTD", &Conf_Motd))
1065 Using_MotdFile = true;
1068 /* Try to read ngIRCd help text file. */
1069 (void)Read_TextFile(Conf_HelpFile, "help text", &Conf_Helptext);
1070 if (!array_bytes(&Conf_Helptext))
1071 Config_Error(LOG_WARNING,
1072 "No help text available, HELP command will be of limited use.");
1075 /* Make sure that all SSL-related files are readable */
1076 CheckFileReadable("CertFile", Conf_SSLOptions.CertFile);
1077 CheckFileReadable("DHFile", Conf_SSLOptions.DHFile);
1078 CheckFileReadable("KeyFile", Conf_SSLOptions.KeyFile);
1080 /* Set the default ciphers if none were configured */
1081 if (!Conf_SSLOptions.CipherList)
1082 Conf_SSLOptions.CipherList = strdup_warn(DEFAULT_CIPHERS);
1089 * Read in and handle a configuration file.
1091 * @param File Name of the configuration file.
1092 * @param fd File descriptor already opened for reading.
1095 Read_Config_File(const char *File, FILE *fd)
1097 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
1101 /* Read configuration file */
1104 if (!fgets(str, sizeof(str), fd))
1109 /* Skip comments and empty lines */
1110 if (str[0] == ';' || str[0] == '#' || str[0] == '\0')
1113 if (strlen(str) >= sizeof(str) - 1) {
1114 Config_Error(LOG_WARNING, "%s, line %d too long!",
1119 /* Is this the beginning of a new section? */
1120 if ((str[0] == '[') && (str[strlen(str) - 1] == ']')) {
1121 strlcpy(section, str, sizeof(section));
1122 if (strcasecmp(section, "[GLOBAL]") == 0
1123 || strcasecmp(section, "[LIMITS]") == 0
1124 || strcasecmp(section, "[OPTIONS]") == 0
1126 || strcasecmp(section, "[SSL]") == 0
1131 if (strcasecmp(section, "[SERVER]") == 0) {
1132 /* Check if there is already a server to add */
1133 if (New_Server.name[0]) {
1134 /* Copy data to "real" server structure */
1135 assert(New_Server_Idx > NONE);
1136 Conf_Server[New_Server_Idx] =
1140 /* Re-init structure for new server */
1141 Init_Server_Struct(&New_Server);
1143 /* Search unused item in server configuration structure */
1144 for (i = 0; i < MAX_SERVERS; i++) {
1145 /* Is this item used? */
1146 if (!Conf_Server[i].name[0])
1149 if (i >= MAX_SERVERS) {
1150 /* Oops, no free item found! */
1151 Config_Error(LOG_ERR,
1152 "Too many servers configured.");
1153 New_Server_Idx = NONE;
1159 if (strcasecmp(section, "[CHANNEL]") == 0) {
1160 count = array_length(&Conf_Channels,
1165 sizeof(struct Conf_Channel), count)) {
1166 Config_Error(LOG_ERR,
1167 "Could not allocate memory for new operator (line %d)",
1173 if (strcasecmp(section, "[OPERATOR]") == 0) {
1174 count = array_length(&Conf_Opers,
1175 sizeof(struct Conf_Oper));
1176 if (!array_alloc(&Conf_Opers,
1177 sizeof(struct Conf_Oper),
1179 Config_Error(LOG_ERR,
1180 "Could not allocate memory for new channel (line &d)",
1186 Config_Error(LOG_ERR,
1187 "%s, line %d: Unknown section \"%s\"!",
1188 File, line, section);
1191 if (section[0] == 0x1)
1194 /* Split line into variable name and parameters */
1195 ptr = strchr(str, '=');
1197 Config_Error(LOG_ERR, "%s, line %d: Syntax error!",
1207 if (strcasecmp(section, "[GLOBAL]") == 0)
1208 Handle_GLOBAL(File, line, var, arg);
1209 else if (strcasecmp(section, "[LIMITS]") == 0)
1210 Handle_LIMITS(File, line, var, arg);
1211 else if (strcasecmp(section, "[OPTIONS]") == 0)
1212 Handle_OPTIONS(File, line, var, arg);
1214 else if (strcasecmp(section, "[SSL]") == 0)
1215 Handle_SSL(File, line, var, arg);
1217 else if (strcasecmp(section, "[OPERATOR]") == 0)
1218 Handle_OPERATOR(File, line, var, arg);
1219 else if (strcasecmp(section, "[SERVER]") == 0)
1220 Handle_SERVER(File, line, var, arg);
1221 else if (strcasecmp(section, "[CHANNEL]") == 0)
1222 Handle_CHANNEL(File, line, var, arg);
1224 Config_Error(LOG_ERR,
1225 "%s, line %d: Variable \"%s\" outside section!",
1231 * Check whether a string argument is "true" or "false".
1233 * @param Arg Input string.
1234 * @returns true if the input string has been parsed as "yes", "true"
1235 * (case insensitive) or a non-zero integer value.
1238 Check_ArgIsTrue(const char *Arg)
1240 if (strcasecmp(Arg, "yes") == 0)
1242 if (strcasecmp(Arg, "true") == 0)
1251 * Handle setting of "MaxNickLength".
1253 * @param Line Line number in configuration file.
1254 * @raram Arg Input string.
1255 * @returns New configured maximum nickname length.
1258 Handle_MaxNickLength(const char *File, int Line, const char *Arg)
1262 new = (unsigned) atoi(Arg) + 1;
1263 if (new > CLIENT_NICK_LEN) {
1264 Config_Error(LOG_WARNING,
1265 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
1266 File, Line, CLIENT_NICK_LEN - 1);
1267 return CLIENT_NICK_LEN;
1270 Config_Error(LOG_WARNING,
1271 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
1279 * Output a warning messages if IDENT is configured but not compiled in.
1282 WarnIdent(const char UNUSED *File, int UNUSED Line)
1286 /* user has enabled ident lookups explicitly, but ... */
1287 Config_Error(LOG_WARNING,
1288 "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
1295 * Output a warning messages if IPv6 is configured but not compiled in.
1298 WarnIPv6(const char UNUSED *File, int UNUSED Line)
1301 if (Conf_ConnectIPv6) {
1302 /* user has enabled IPv6 explicitly, but ... */
1303 Config_Error(LOG_WARNING,
1304 "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
1311 * Output a warning messages if PAM is configured but not compiled in.
1314 WarnPAM(const char UNUSED *File, int UNUSED Line)
1318 Config_Error(LOG_WARNING,
1319 "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
1327 * Handle variable in [Global] configuration section.
1329 * @param Line Line number in configuration file.
1330 * @param Var Variable name.
1331 * @param Arg Variable argument.
1334 Handle_GLOBAL(const char *File, int Line, char *Var, char *Arg )
1341 assert(File != NULL);
1343 assert(Var != NULL);
1344 assert(Arg != NULL);
1346 if (strcasecmp(Var, "Name") == 0) {
1347 len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
1348 if (len >= sizeof(Conf_ServerName))
1349 Config_Error_TooLong(File, Line, Var);
1352 if (strcasecmp(Var, "AdminInfo1") == 0) {
1353 len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
1354 if (len >= sizeof(Conf_ServerAdmin1))
1355 Config_Error_TooLong(File, Line, Var);
1358 if (strcasecmp(Var, "AdminInfo2") == 0) {
1359 len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
1360 if (len >= sizeof(Conf_ServerAdmin2))
1361 Config_Error_TooLong(File, Line, Var);
1364 if (strcasecmp(Var, "AdminEMail") == 0) {
1365 len = strlcpy(Conf_ServerAdminMail, Arg,
1366 sizeof(Conf_ServerAdminMail));
1367 if (len >= sizeof(Conf_ServerAdminMail))
1368 Config_Error_TooLong(File, Line, Var);
1371 if (strcasecmp(Var, "Info") == 0) {
1372 len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
1373 if (len >= sizeof(Conf_ServerInfo))
1374 Config_Error_TooLong(File, Line, Var);
1377 if (strcasecmp(Var, "HelpFile") == 0) {
1378 len = strlcpy(Conf_HelpFile, Arg, sizeof(Conf_HelpFile));
1379 if (len >= sizeof(Conf_HelpFile))
1380 Config_Error_TooLong(File, Line, Var);
1383 if (strcasecmp(Var, "Listen") == 0) {
1384 if (Conf_ListenAddress) {
1385 Config_Error(LOG_ERR,
1386 "Multiple Listen= options, ignoring: %s",
1390 Conf_ListenAddress = strdup_warn(Arg);
1391 /* If allocation fails, we're in trouble: we cannot ignore the
1392 * error -- otherwise ngircd would listen on all interfaces. */
1393 if (!Conf_ListenAddress) {
1394 Config_Error(LOG_ALERT,
1395 "%s exiting due to fatal errors!",
1401 if (strcasecmp(Var, "MotdFile") == 0) {
1402 len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
1403 if (len >= sizeof(Conf_MotdFile))
1404 Config_Error_TooLong(File, Line, Var);
1407 if (strcasecmp(Var, "MotdPhrase") == 0) {
1412 Config_Error_TooLong(File, Line, Var);
1415 if (!array_copyb(&Conf_Motd, Arg, len + 1))
1416 Config_Error(LOG_WARNING,
1417 "%s, line %d: Could not append MotdPhrase: %s",
1418 File, Line, strerror(errno));
1419 Using_MotdFile = false;
1422 if (strcasecmp(Var, "Network") == 0) {
1423 len = strlcpy(Conf_Network, Arg, sizeof(Conf_Network));
1424 if (len >= sizeof(Conf_Network))
1425 Config_Error_TooLong(File, Line, Var);
1426 ptr = strchr(Conf_Network, ' ');
1428 Config_Error(LOG_WARNING,
1429 "%s, line %d: \"Network\" can't contain spaces!",
1435 if(strcasecmp(Var, "Password") == 0) {
1436 len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
1437 if (len >= sizeof(Conf_ServerPwd))
1438 Config_Error_TooLong(File, Line, Var);
1441 if (strcasecmp(Var, "PidFile") == 0) {
1442 len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
1443 if (len >= sizeof(Conf_PidFile))
1444 Config_Error_TooLong(File, Line, Var);
1447 if (strcasecmp(Var, "Ports") == 0) {
1448 ports_parse(&Conf_ListenPorts, File, Line, Arg);
1451 if (strcasecmp(Var, "ServerGID") == 0) {
1452 grp = getgrnam(Arg);
1454 Conf_GID = grp->gr_gid;
1456 Conf_GID = (unsigned int)atoi(Arg);
1457 if (!Conf_GID && strcmp(Arg, "0"))
1458 Config_Error(LOG_WARNING,
1459 "%s, line %d: Value of \"%s\" is not a valid group name or ID!",
1464 if (strcasecmp(Var, "ServerUID") == 0) {
1465 pwd = getpwnam(Arg);
1467 Conf_UID = pwd->pw_uid;
1469 Conf_UID = (unsigned int)atoi(Arg);
1470 if (!Conf_UID && strcmp(Arg, "0"))
1471 Config_Error(LOG_WARNING,
1472 "%s, line %d: Value of \"%s\" is not a valid user name or ID!",
1478 Config_Error_Section(File, Line, Var, "Global");
1482 * Handle variable in [Limits] configuration section.
1484 * @param Line Line number in configuration file.
1485 * @param Var Variable name.
1486 * @param Arg Variable argument.
1489 Handle_LIMITS(const char *File, int Line, char *Var, char *Arg)
1491 assert(File != NULL);
1493 assert(Var != NULL);
1494 assert(Arg != NULL);
1496 if (strcasecmp(Var, "ConnectRetry") == 0) {
1497 Conf_ConnectRetry = atoi(Arg);
1498 if (Conf_ConnectRetry < 5) {
1499 Config_Error(LOG_WARNING,
1500 "%s, line %d: Value of \"ConnectRetry\" too low!",
1502 Conf_ConnectRetry = 5;
1506 if (strcasecmp(Var, "IdleTimeout") == 0) {
1507 Conf_IdleTimeout = atoi(Arg);
1508 if (!Conf_IdleTimeout && strcmp(Arg, "0"))
1509 Config_Error_NaN(File, Line, Var);
1512 if (strcasecmp(Var, "MaxConnections") == 0) {
1513 Conf_MaxConnections = atoi(Arg);
1514 if (!Conf_MaxConnections && strcmp(Arg, "0"))
1515 Config_Error_NaN(File, Line, Var);
1518 if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
1519 Conf_MaxConnectionsIP = atoi(Arg);
1520 if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
1521 Config_Error_NaN(File, Line, Var);
1524 if (strcasecmp(Var, "MaxJoins") == 0) {
1525 Conf_MaxJoins = atoi(Arg);
1526 if (!Conf_MaxJoins && strcmp(Arg, "0"))
1527 Config_Error_NaN(File, Line, Var);
1530 if (strcasecmp(Var, "MaxNickLength") == 0) {
1531 Conf_MaxNickLength = Handle_MaxNickLength(File, Line, Arg);
1534 if (strcasecmp(Var, "MaxListSize") == 0) {
1535 Conf_MaxListSize = atoi(Arg);
1536 if (!Conf_MaxListSize && strcmp(Arg, "0"))
1537 Config_Error_NaN(File, Line, Var);
1540 if (strcasecmp(Var, "MaxPenaltyTime") == 0) {
1541 Conf_MaxPenaltyTime = atol(Arg);
1542 if (Conf_MaxPenaltyTime < -1)
1543 Conf_MaxPenaltyTime = -1; /* "unlimited" */
1546 if (strcasecmp(Var, "PingTimeout") == 0) {
1547 Conf_PingTimeout = atoi(Arg);
1548 if (Conf_PingTimeout < 5) {
1549 Config_Error(LOG_WARNING,
1550 "%s, line %d: Value of \"PingTimeout\" too low!",
1552 Conf_PingTimeout = 5;
1556 if (strcasecmp(Var, "PongTimeout") == 0) {
1557 Conf_PongTimeout = atoi(Arg);
1558 if (Conf_PongTimeout < 5) {
1559 Config_Error(LOG_WARNING,
1560 "%s, line %d: Value of \"PongTimeout\" too low!",
1562 Conf_PongTimeout = 5;
1567 Config_Error_Section(File, Line, Var, "Limits");
1571 * Handle variable in [Options] configuration section.
1573 * @param Line Line number in configuration file.
1574 * @param Var Variable name.
1575 * @param Arg Variable argument.
1578 Handle_OPTIONS(const char *File, int Line, char *Var, char *Arg)
1583 assert(File != NULL);
1585 assert(Var != NULL);
1586 assert(Arg != NULL);
1588 if (strcasecmp(Var, "AllowedChannelTypes") == 0) {
1590 Conf_AllowedChannelTypes[0] = '\0';
1592 if (strchr(Conf_AllowedChannelTypes, *p)) {
1593 /* Prefix is already included; ignore it */
1598 if (strchr(CHANTYPES, *p)) {
1599 len = strlen(Conf_AllowedChannelTypes) + 1;
1600 assert(len < sizeof(Conf_AllowedChannelTypes));
1601 Conf_AllowedChannelTypes[len - 1] = *p;
1602 Conf_AllowedChannelTypes[len] = '\0';
1604 Config_Error(LOG_WARNING,
1605 "%s, line %d: Unknown channel prefix \"%c\" in \"AllowedChannelTypes\"!",
1612 if (strcasecmp(Var, "AllowRemoteOper") == 0) {
1613 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1616 if (strcasecmp(Var, "ChrootDir") == 0) {
1617 len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
1618 if (len >= sizeof(Conf_Chroot))
1619 Config_Error_TooLong(File, Line, Var);
1622 if (strcasecmp(Var, "CloakHost") == 0) {
1623 len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
1624 if (len >= sizeof(Conf_CloakHost))
1625 Config_Error_TooLong(File, Line, Var);
1628 if (strcasecmp(Var, "CloakHostModeX") == 0) {
1629 len = strlcpy(Conf_CloakHostModeX, Arg, sizeof(Conf_CloakHostModeX));
1630 if (len >= sizeof(Conf_CloakHostModeX))
1631 Config_Error_TooLong(File, Line, Var);
1634 if (strcasecmp(Var, "CloakHostSalt") == 0) {
1635 len = strlcpy(Conf_CloakHostSalt, Arg, sizeof(Conf_CloakHostSalt));
1636 if (len >= sizeof(Conf_CloakHostSalt))
1637 Config_Error_TooLong(File, Line, Var);
1640 if (strcasecmp(Var, "CloakUserToNick") == 0) {
1641 Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
1644 if (strcasecmp(Var, "ConnectIPv6") == 0) {
1645 Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
1646 WarnIPv6(File, Line);
1649 if (strcasecmp(Var, "ConnectIPv4") == 0) {
1650 Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
1653 if (strcasecmp(Var, "DefaultUserModes") == 0) {
1655 Conf_DefaultUserModes[0] = '\0';
1657 if (strchr(Conf_DefaultUserModes, *p)) {
1658 /* Mode is already included; ignore it */
1663 if (strchr(USERMODES, *p)) {
1664 len = strlen(Conf_DefaultUserModes) + 1;
1665 assert(len < sizeof(Conf_DefaultUserModes));
1666 Conf_DefaultUserModes[len - 1] = *p;
1667 Conf_DefaultUserModes[len] = '\0';
1669 Config_Error(LOG_WARNING,
1670 "%s, line %d: Unknown user mode \"%c\" in \"DefaultUserModes\"!",
1677 if (strcasecmp(Var, "DNS") == 0) {
1678 Conf_DNS = Check_ArgIsTrue(Arg);
1681 if (strcasecmp(Var, "Ident") == 0) {
1682 Conf_Ident = Check_ArgIsTrue(Arg);
1683 WarnIdent(File, Line);
1686 if (strcasecmp(Var, "IncludeDir") == 0) {
1687 if (Conf_IncludeDir[0]) {
1688 Config_Error(LOG_ERR,
1689 "%s, line %d: Can't overwrite value of \"IncludeDir\" variable!",
1693 len = strlcpy(Conf_IncludeDir, Arg, sizeof(Conf_IncludeDir));
1694 if (len >= sizeof(Conf_IncludeDir))
1695 Config_Error_TooLong(File, Line, Var);
1698 if (strcasecmp(Var, "MorePrivacy") == 0) {
1699 Conf_MorePrivacy = Check_ArgIsTrue(Arg);
1702 if (strcasecmp(Var, "NoticeBeforeRegistration") == 0) {
1703 Conf_NoticeBeforeRegistration = Check_ArgIsTrue(Arg);
1706 if (strcasecmp(Var, "OperCanUseMode") == 0) {
1707 Conf_OperCanMode = Check_ArgIsTrue(Arg);
1710 if (strcasecmp(Var, "OperChanPAutoOp") == 0) {
1711 Conf_OperChanPAutoOp = Check_ArgIsTrue(Arg);
1714 if (strcasecmp(Var, "OperServerMode") == 0) {
1715 Conf_OperServerMode = Check_ArgIsTrue(Arg);
1718 if (strcasecmp(Var, "PAM") == 0) {
1719 Conf_PAM = Check_ArgIsTrue(Arg);
1720 WarnPAM(File, Line);
1723 if (strcasecmp(Var, "PAMIsOptional") == 0 ) {
1724 Conf_PAMIsOptional = Check_ArgIsTrue(Arg);
1727 if (strcasecmp(Var, "PAMServiceName") == 0) {
1728 len = strlcpy(Conf_PAMServiceName, Arg, sizeof(Conf_PAMServiceName));
1729 if (len >= sizeof(Conf_PAMServiceName))
1730 Config_Error_TooLong(File, Line, Var);
1734 if (strcasecmp(Var, "RequireAuthPing") == 0) {
1735 Conf_AuthPing = Check_ArgIsTrue(Arg);
1739 if (strcasecmp(Var, "ScrubCTCP") == 0) {
1740 Conf_ScrubCTCP = Check_ArgIsTrue(Arg);
1744 if (strcasecmp(Var, "SyslogFacility") == 0) {
1745 Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
1746 Conf_SyslogFacility);
1750 if (strcasecmp(Var, "WebircPassword") == 0) {
1751 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
1752 if (len >= sizeof(Conf_WebircPwd))
1753 Config_Error_TooLong(File, Line, Var);
1757 Config_Error_Section(File, Line, Var, "Options");
1763 * Handle variable in [SSL] configuration section.
1765 * @param Line Line number in configuration file.
1766 * @param Var Variable name.
1767 * @param Arg Variable argument.
1770 Handle_SSL(const char *File, int Line, char *Var, char *Arg)
1772 assert(File != NULL);
1774 assert(Var != NULL);
1775 assert(Arg != NULL);
1777 if (strcasecmp(Var, "CertFile") == 0) {
1778 assert(Conf_SSLOptions.CertFile == NULL);
1779 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1782 if (strcasecmp(Var, "DHFile") == 0) {
1783 assert(Conf_SSLOptions.DHFile == NULL);
1784 Conf_SSLOptions.DHFile = strdup_warn(Arg);
1787 if (strcasecmp(Var, "KeyFile") == 0) {
1788 assert(Conf_SSLOptions.KeyFile == NULL);
1789 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1792 if (strcasecmp(Var, "KeyFilePassword") == 0) {
1793 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1794 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1795 Config_Error(LOG_ERR,
1796 "%s, line %d (section \"SSL\"): Could not copy %s: %s!",
1797 File, Line, Var, strerror(errno));
1800 if (strcasecmp(Var, "Ports") == 0) {
1801 ports_parse(&Conf_SSLOptions.ListenPorts, File, Line, Arg);
1804 if (strcasecmp(Var, "CipherList") == 0) {
1805 assert(Conf_SSLOptions.CipherList == NULL);
1806 Conf_SSLOptions.CipherList = strdup_warn(Arg);
1809 if (strcasecmp(Var, "CAFile") == 0) {
1810 assert(Conf_SSLOptions.CAFile == NULL);
1811 Conf_SSLOptions.CAFile = strdup_warn(Arg);
1814 if (strcasecmp(Var, "CRLFile") == 0) {
1815 assert(Conf_SSLOptions.CRLFile == NULL);
1816 Conf_SSLOptions.CRLFile = strdup_warn(Arg);
1820 Config_Error_Section(File, Line, Var, "SSL");
1826 * Handle variable in [Operator] configuration section.
1828 * @param Line Line number in configuration file.
1829 * @param Var Variable name.
1830 * @param Arg Variable argument.
1833 Handle_OPERATOR(const char *File, int Line, char *Var, char *Arg )
1836 struct Conf_Oper *op;
1838 assert( File != NULL );
1840 assert( Var != NULL );
1841 assert( Arg != NULL );
1843 op = array_get(&Conf_Opers, sizeof(*op),
1844 array_length(&Conf_Opers, sizeof(*op)) - 1);
1848 if (strcasecmp(Var, "Name") == 0) {
1849 /* Name of IRC operator */
1850 len = strlcpy(op->name, Arg, sizeof(op->name));
1851 if (len >= sizeof(op->name))
1852 Config_Error_TooLong(File, Line, Var);
1855 if (strcasecmp(Var, "Password") == 0) {
1856 /* Password of IRC operator */
1857 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1858 if (len >= sizeof(op->pwd))
1859 Config_Error_TooLong(File, Line, Var);
1862 if (strcasecmp(Var, "Mask") == 0) {
1864 return; /* Hostname already configured */
1865 op->mask = strdup_warn( Arg );
1869 Config_Error_Section(File, Line, Var, "Operator");
1873 * Handle variable in [Server] configuration section.
1875 * @param Line Line number in configuration file.
1876 * @param Var Variable name.
1877 * @param Arg Variable argument.
1880 Handle_SERVER(const char *File, int Line, char *Var, char *Arg )
1885 assert( File != NULL );
1887 assert( Var != NULL );
1888 assert( Arg != NULL );
1890 /* Ignore server block if no space is left in server configuration structure */
1891 if( New_Server_Idx <= NONE ) return;
1893 if( strcasecmp( Var, "Host" ) == 0 ) {
1894 /* Hostname of the server */
1895 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1896 if (len >= sizeof( New_Server.host ))
1897 Config_Error_TooLong(File, Line, Var);
1900 if( strcasecmp( Var, "Name" ) == 0 ) {
1901 /* Name of the server ("Nick"/"ID") */
1902 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1903 if (len >= sizeof( New_Server.name ))
1904 Config_Error_TooLong(File, Line, Var);
1907 if (strcasecmp(Var, "Bind") == 0) {
1908 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1911 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1915 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1916 /* Password of this server which is sent to the peer */
1918 Config_Error(LOG_ERR,
1919 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1922 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1923 if (len >= sizeof( New_Server.pwd_in ))
1924 Config_Error_TooLong(File, Line, Var);
1927 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1928 /* Passwort of the peer which must be received */
1929 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1930 if (len >= sizeof( New_Server.pwd_out ))
1931 Config_Error_TooLong(File, Line, Var);
1934 if( strcasecmp( Var, "Port" ) == 0 ) {
1935 /* Port to which this server should connect */
1937 if (port >= 0 && port < 0xFFFF)
1938 New_Server.port = (UINT16)port;
1940 Config_Error(LOG_ERR,
1941 "%s, line %d (section \"Server\"): Illegal port number %ld!",
1946 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1947 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1950 if (strcasecmp(Var, "SSLVerify") == 0) {
1951 New_Server.SSLVerify = Check_ArgIsTrue(Arg);
1955 if( strcasecmp( Var, "Group" ) == 0 ) {
1957 New_Server.group = atoi( Arg );
1958 if (!New_Server.group && strcmp(Arg, "0"))
1959 Config_Error_NaN(File, Line, Var);
1962 if( strcasecmp( Var, "Passive" ) == 0 ) {
1963 if (Check_ArgIsTrue(Arg))
1964 New_Server.flags |= CONF_SFLAG_DISABLED;
1967 if (strcasecmp(Var, "ServiceMask") == 0) {
1968 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1969 sizeof(New_Server.svs_mask));
1970 if (len >= sizeof(New_Server.svs_mask))
1971 Config_Error_TooLong(File, Line, Var);
1975 Config_Error_Section(File, Line, Var, "Server");
1979 * Copy channel name into channel structure.
1981 * If the channel name is not valid because of a missing prefix ('#', '&'),
1982 * a default prefix of '#' will be added.
1984 * @param new_chan New already allocated channel structure.
1985 * @param name Name of the new channel.
1986 * @returns true on success, false otherwise.
1989 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1991 size_t size = sizeof(new_chan->name);
1992 char *dest = new_chan->name;
1994 if (!Channel_IsValidName(name)) {
1996 * maybe user forgot to add a '#'.
1997 * This is only here for user convenience.
2003 return size > strlcpy(dest, name, size);
2007 * Handle variable in [Channel] configuration section.
2009 * @param Line Line number in configuration file.
2010 * @param Var Variable name.
2011 * @param Arg Variable argument.
2014 Handle_CHANNEL(const char *File, int Line, char *Var, char *Arg)
2017 struct Conf_Channel *chan;
2019 assert( File != NULL );
2021 assert( Var != NULL );
2022 assert( Arg != NULL );
2024 chan = array_get(&Conf_Channels, sizeof(*chan),
2025 array_length(&Conf_Channels, sizeof(*chan)) - 1);
2029 if (strcasecmp(Var, "Name") == 0) {
2030 if (!Handle_Channelname(chan, Arg))
2031 Config_Error_TooLong(File, Line, Var);
2034 if (strcasecmp(Var, "Modes") == 0) {
2036 if(chan->modes_num >= sizeof(chan->modes)) {
2037 Config_Error(LOG_ERR, "Too many Modes, option ignored.");
2040 chan->modes[chan->modes_num++] = strndup(Arg, COMMAND_LEN);
2041 if(strlen(Arg) >= COMMAND_LEN)
2042 Config_Error_TooLong(File, Line, Var);
2045 if( strcasecmp( Var, "Topic" ) == 0 ) {
2047 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
2048 if (len >= sizeof(chan->topic))
2049 Config_Error_TooLong(File, Line, Var);
2052 if( strcasecmp( Var, "Autojoin" ) == 0 ) {
2053 /* Check autojoin */
2054 chan->autojoin = Check_ArgIsTrue(Arg);
2057 if( strcasecmp( Var, "Key" ) == 0 ) {
2058 /* Initial Channel Key (mode k) */
2059 len = strlcpy(chan->key, Arg, sizeof(chan->key));
2060 if (len >= sizeof(chan->key))
2061 Config_Error_TooLong(File, Line, Var);
2062 Config_Error(LOG_WARNING,
2063 "%s, line %d (section \"Channel\"): \"%s\" is deprecated here, use \"Modes = +k <key>\"!",
2067 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
2068 /* maximum user limit, mode l */
2069 chan->maxusers = (unsigned long) atol(Arg);
2070 if (!chan->maxusers && strcmp(Arg, "0"))
2071 Config_Error_NaN(File, Line, Var);
2072 Config_Error(LOG_WARNING,
2073 "%s, line %d (section \"Channel\"): \"%s\" is deprecated here, use \"Modes = +l <limit>\"!",
2077 if (strcasecmp(Var, "KeyFile") == 0) {
2079 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
2080 if (len >= sizeof(chan->keyfile))
2081 Config_Error_TooLong(File, Line, Var);
2085 Config_Error_Section(File, Line, Var, "Channel");
2089 * Validate server configuration.
2091 * Please note that this function uses exit(1) on fatal errors and therefore
2092 * can result in ngIRCd terminating!
2094 * @param Configtest true if the daemon has been called with "--configtest".
2095 * @param Rehash true if re-reading configuration on runtime.
2096 * @returns true if configuration is valid.
2099 Validate_Config(bool Configtest, bool Rehash)
2101 /* Validate configuration settings. */
2103 int i, servers, servers_once;
2105 bool config_valid = true;
2108 /* Emit a warning when the config file is not a full path name */
2109 if (NGIRCd_ConfFile[0] && NGIRCd_ConfFile[0] != '/') {
2110 Config_Error(LOG_WARNING,
2111 "Not specifying a full path name to \"%s\" can cause problems when rehashing the server!",
2115 if (!Conf_ServerName[0]) {
2116 /* No server name configured, try to get a sane name from the
2117 * host name. Note: the IRC server name MUST contain
2118 * at least one dot, so the "node name" is not sufficient! */
2119 gethostname(Conf_ServerName, sizeof(Conf_ServerName));
2121 /* Try to get a proper host name ... */
2122 h = gethostbyname(Conf_ServerName);
2124 strlcpy(Conf_ServerName, h->h_name,
2125 sizeof(Conf_ServerName));
2127 if (!strchr(Conf_ServerName, '.')) {
2128 /* (Still) No dot in the name! */
2129 strlcat(Conf_ServerName, ".host",
2130 sizeof(Conf_ServerName));
2132 Config_Error(LOG_WARNING,
2133 "No server name configured, using host name \"%s\".",
2137 /* Validate configured server name, see RFC 2812 section 2.3.1 */
2138 ptr = Conf_ServerName;
2140 if (*ptr >= 'a' && *ptr <= 'z') continue;
2141 if (*ptr >= 'A' && *ptr <= 'Z') continue;
2142 if (*ptr >= '0' && *ptr <= '9') continue;
2143 if (ptr > Conf_ServerName) {
2144 if (*ptr == '.' || *ptr == '-')
2147 Conf_ServerName[0] = '\0';
2151 if (!Conf_ServerName[0] || !strchr(Conf_ServerName, '.')) {
2152 config_valid = false;
2153 Config_Error(LOG_ALERT,
2154 "No (valid) server name configured (section 'Global': 'Name')!");
2155 if (!Configtest && !Rehash) {
2156 Config_Error(LOG_ALERT,
2157 "%s exiting due to fatal errors!",
2164 if (!Conf_ServerAdminMail[0]) {
2165 /* No administrative contact configured! */
2166 config_valid = false;
2167 Config_Error(LOG_ALERT,
2168 "No administrator email address configured ('AdminEMail')!");
2170 Config_Error(LOG_ALERT,
2171 "%s exiting due to fatal errors!",
2178 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
2179 && !Conf_ServerAdminMail[0]) {
2180 /* No administrative information configured! */
2181 Config_Error(LOG_WARNING,
2182 "No administrative information configured but required by RFC!");
2186 if (Conf_PAM && Conf_ServerPwd[0])
2187 Config_Error(LOG_ERR,
2188 "This server uses PAM, \"Password\" in [Global] section will be ignored!");
2191 if (Conf_MaxPenaltyTime != -1)
2192 Config_Error(LOG_WARNING,
2193 "Maximum penalty increase ('MaxPenaltyTime') is set to %ld, this is not recommended!",
2194 Conf_MaxPenaltyTime);
2196 servers = servers_once = 0;
2197 for (i = 0; i < MAX_SERVERS; i++) {
2198 if (Conf_Server[i].name[0]) {
2200 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
2204 LogDebug("Configuration: Operators=%ld, Servers=%d[%d], Channels=%ld",
2205 array_length(&Conf_Opers, sizeof(struct Conf_Oper)),
2206 servers, servers_once,
2207 array_length(&Conf_Channels, sizeof(struct Conf_Channel)));
2209 return config_valid;
2213 * Output "line too long" warning.
2215 * @param Line Line number in configuration file.
2216 * @param Item Affected variable name.
2219 Config_Error_TooLong(const char *File, const int Line, const char *Item)
2221 Config_Error(LOG_WARNING, "%s, line %d: Value of \"%s\" too long!",
2226 * Output "unknown variable" warning.
2228 * @param Line Line number in configuration file.
2229 * @param Item Affected variable name.
2230 * @param Section Section name.
2233 Config_Error_Section(const char *File, const int Line, const char *Item,
2234 const char *Section)
2236 Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
2237 File, Line, Section, Item);
2241 * Output "not a number" warning.
2243 * @param Line Line number in configuration file.
2244 * @param Item Affected variable name.
2247 Config_Error_NaN(const char *File, const int Line, const char *Item )
2249 Config_Error(LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
2254 * Output configuration error to console and/or logfile.
2256 * On runtime, the normal log functions of the daemon are used. But when
2257 * testing the configuration ("--configtest"), all messages go directly
2260 * @param Level Severity level of the message.
2261 * @param Format Format string; see printf() function.
2264 static void Config_Error( const int Level, const char *Format, ... )
2266 static void Config_Error( Level, Format, va_alist )
2272 char msg[MAX_LOG_MSG_LEN];
2275 assert( Format != NULL );
2278 va_start( ap, Format );
2282 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
2286 if (Level <= LOG_WARNING)
2287 printf(" - %s\n", msg);
2291 Log(Level, "%s", msg);
2296 * Dump internal state of the "configuration module".
2299 Conf_DebugDump(void)
2303 LogDebug("Configured servers:");
2304 for (i = 0; i < MAX_SERVERS; i++) {
2305 if (! Conf_Server[i].name[0])
2308 " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
2309 Conf_Server[i].name, Conf_Server[i].host,
2310 Conf_Server[i].port, Conf_Server[i].lasttry,
2311 Conf_Server[i].group, Conf_Server[i].flags,
2312 Conf_Server[i].conn_id);
2318 * Initialize server configuration structure to default values.
2320 * @param Server Pointer to server structure to initialize.
2323 Init_Server_Struct( CONF_SERVER *Server )
2325 assert( Server != NULL );
2327 memset( Server, 0, sizeof (CONF_SERVER) );
2329 Server->group = NONE;
2330 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
2332 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
2334 Proc_InitStruct(&Server->res_stat);
2335 Server->conn_id = NONE;
2336 memset(&Server->bind_addr, 0, sizeof(Server->bind_addr));