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(" CAFile = %s\n", Conf_SSLOptions.CAFile
446 ? Conf_SSLOptions.CAFile : "");
447 printf(" CertFile = %s\n", Conf_SSLOptions.CertFile
448 ? Conf_SSLOptions.CertFile : "");
449 printf(" CipherList = %s\n", Conf_SSLOptions.CipherList ?
450 Conf_SSLOptions.CipherList : DEFAULT_CIPHERS);
451 printf(" CRLFile = %s\n", Conf_SSLOptions.CRLFile
452 ? Conf_SSLOptions.CRLFile : "");
453 printf(" DHFile = %s\n", Conf_SSLOptions.DHFile
454 ? Conf_SSLOptions.DHFile : "");
455 printf(" KeyFile = %s\n", Conf_SSLOptions.KeyFile
456 ? Conf_SSLOptions.KeyFile : "");
457 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
458 puts(" KeyFilePassword = <secret>");
460 puts(" KeyFilePassword = ");
461 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
463 ports_puts(&Conf_SSLOptions.ListenPorts);
469 for( i = 0; i < MAX_SERVERS; i++ ) {
470 if( ! Conf_Server[i].name[0] ) continue;
472 /* Valid "Server" section */
474 printf( " Name = %s\n", Conf_Server[i].name );
475 printf( " Host = %s\n", Conf_Server[i].host );
476 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
478 printf(" SSLConnect = %s\n",
479 yesno_to_str(Conf_Server[i].SSLConnect));
480 printf(" SSLVerify = %s\n",
481 yesno_to_str(Conf_Server[i].SSLVerify));
483 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
484 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
485 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
486 printf( " Group = %d\n", Conf_Server[i].group );
487 printf( " Passive = %s\n\n", yesno_to_str(Conf_Server[i].flags & CONF_SFLAG_DISABLED));
490 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
491 predef_chan = array_start(&Conf_Channels);
493 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
494 if (!predef_chan->name[0])
497 /* Valid "Channel" section */
499 printf(" Name = %s\n", predef_chan->name);
500 for(j = 0; j < predef_chan->modes_num; j++)
501 printf(" Modes = %s\n", predef_chan->modes[j]);
502 printf(" Key = %s\n", predef_chan->key);
503 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
504 printf(" Topic = %s\n", predef_chan->topic);
505 printf(" Autojoin = %s\n", yesno_to_str(predef_chan->autojoin));
506 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
509 return (config_valid ? 0 : 1);
513 * Remove connection information from configured server.
515 * If the server is set as "once", delete it from our configuration;
516 * otherwise set the time for the next connection attempt.
518 * Non-server connections will be silently ignored.
521 Conf_UnsetServer( CONN_ID Idx )
526 /* Check all our configured servers */
527 for( i = 0; i < MAX_SERVERS; i++ ) {
528 if( Conf_Server[i].conn_id != Idx ) continue;
530 /* Gotcha! Mark server configuration as "unused": */
531 Conf_Server[i].conn_id = NONE;
533 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
534 /* Delete configuration here */
535 Init_Server_Struct( &Conf_Server[i] );
537 /* Set time for next connect attempt */
539 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
540 /* The connection has been "long", so we don't
541 * require the next attempt to be delayed. */
542 Conf_Server[i].lasttry =
543 t - Conf_ConnectRetry + RECONNECT_DELAY;
545 /* "Short" connection, enforce "ConnectRetry"
546 * but randomize it a little bit: 15 seconds. */
547 Conf_Server[i].lasttry =
548 #ifdef HAVE_ARC4RANDOM
549 t + (arc4random() % 15);
551 t + rand() / (RAND_MAX / 15);
559 * Set connection information for specified configured server.
562 Conf_SetServer( int ConfServer, CONN_ID Idx )
564 assert( ConfServer > NONE );
565 assert( Idx > NONE );
567 if (Conf_Server[ConfServer].conn_id > NONE &&
568 Conf_Server[ConfServer].conn_id != Idx) {
570 "Connection %d: Server configuration of \"%s\" already in use by connection %d!",
571 Idx, Conf_Server[ConfServer].name,
572 Conf_Server[ConfServer].conn_id);
573 Conn_Close(Idx, NULL, "Server configuration already in use", true);
576 Conf_Server[ConfServer].conn_id = Idx;
581 * Get index of server in configuration structure.
584 Conf_GetServer( CONN_ID Idx )
588 assert( Idx > NONE );
590 for( i = 0; i < MAX_SERVERS; i++ ) {
591 if( Conf_Server[i].conn_id == Idx ) return i;
597 * Enable a server by name and adjust its port number.
599 * @returns true if a server has been enabled and now has a valid port
600 * number and host name for outgoing connections.
603 Conf_EnableServer( const char *Name, UINT16 Port )
607 assert( Name != NULL );
608 for( i = 0; i < MAX_SERVERS; i++ ) {
609 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
610 /* Gotcha! Set port and enable server: */
611 Conf_Server[i].port = Port;
612 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
613 return (Conf_Server[i].port && Conf_Server[i].host[0]);
620 * Enable a server by name.
622 * The server is only usable as outgoing server, if it has set a valid port
623 * number for outgoing connections!
624 * If not, you have to use Conf_EnableServer() function to make it available.
626 * @returns true if a server has been enabled; false otherwise.
629 Conf_EnablePassiveServer(const char *Name)
633 assert( Name != NULL );
634 for (i = 0; i < MAX_SERVERS; i++) {
635 if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
636 && (Conf_Server[i].port > 0)) {
637 /* BINGO! Enable server */
638 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
639 Conf_Server[i].lasttry = 0;
647 * Disable a server by name.
648 * An already established connection will be disconnected.
650 * @returns true if a server was found and has been disabled.
653 Conf_DisableServer( const char *Name )
657 assert( Name != NULL );
658 for( i = 0; i < MAX_SERVERS; i++ ) {
659 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
660 /* Gotcha! Disable and disconnect server: */
661 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
662 if( Conf_Server[i].conn_id > NONE )
663 Conn_Close(Conf_Server[i].conn_id, NULL,
664 "Server link terminated on operator request",
673 * Add a new remote server to our configuration.
675 * @param Name Name of the new server.
676 * @param Port Port number to connect to or 0 for incoming connections.
677 * @param Host Host name to connect to.
678 * @param MyPwd Password that will be sent to the peer.
679 * @param PeerPwd Password that must be received from the peer.
680 * @returns true if the new server has been added; false otherwise.
683 Conf_AddServer(const char *Name, UINT16 Port, const char *Host,
684 const char *MyPwd, const char *PeerPwd)
688 assert( Name != NULL );
689 assert( Host != NULL );
690 assert( MyPwd != NULL );
691 assert( PeerPwd != NULL );
693 /* Search unused item in server configuration structure */
694 for( i = 0; i < MAX_SERVERS; i++ ) {
695 /* Is this item used? */
696 if( ! Conf_Server[i].name[0] ) break;
698 if( i >= MAX_SERVERS ) return false;
700 Init_Server_Struct( &Conf_Server[i] );
701 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
702 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
703 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
704 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
705 Conf_Server[i].port = Port;
706 Conf_Server[i].flags = CONF_SFLAG_ONCE;
712 * Check if the given nickname is reserved for services on a particular server.
714 * @param ConfServer The server index to check.
715 * @param Nick The nickname to check.
716 * @returns true if the given nickname belongs to an "IRC service".
719 Conf_NickIsService(int ConfServer, const char *Nick)
721 assert (ConfServer >= 0);
722 assert (ConfServer < MAX_SERVERS);
724 return MatchCaseInsensitiveList(Conf_Server[ConfServer].svs_mask,
729 * Check if the given nickname is blocked for "normal client" use.
731 * @param Nick The nickname to check.
732 * @returns true if the given nickname belongs to an "IRC service".
735 Conf_NickIsBlocked(const char *Nick)
739 for(i = 0; i < MAX_SERVERS; i++) {
740 if (!Conf_Server[i].name[0])
742 if (Conf_NickIsService(i, Nick))
749 * Initialize configuration settings with their default values.
752 Set_Defaults(bool InitServers)
755 char random[RANDOM_SALT_LEN + 1];
758 strcpy(Conf_ServerName, "");
759 strcpy(Conf_ServerAdmin1, "");
760 strcpy(Conf_ServerAdmin2, "");
761 strcpy(Conf_ServerAdminMail, "");
762 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
763 PACKAGE_NAME, PACKAGE_VERSION);
764 strcpy(Conf_Network, "");
765 free(Conf_ListenAddress);
766 Conf_ListenAddress = NULL;
767 array_free(&Conf_ListenPorts);
768 array_free(&Conf_Motd);
769 array_free(&Conf_Helptext);
770 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
771 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
772 strlcpy(Conf_HelpFile, DOCDIR, sizeof(Conf_HelpFile));
773 strlcat(Conf_HelpFile, HELP_FILE, sizeof(Conf_HelpFile));
774 strcpy(Conf_ServerPwd, "");
775 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
776 Conf_UID = Conf_GID = 0;
779 Conf_ConnectRetry = 60;
780 Conf_IdleTimeout = 0;
781 Conf_MaxConnections = 0;
782 Conf_MaxConnectionsIP = 5;
784 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
785 Conf_MaxPenaltyTime = -1;
786 Conf_MaxListSize = 100;
787 Conf_PingTimeout = 120;
788 Conf_PongTimeout = 20;
791 strlcpy(Conf_AllowedChannelTypes, CHANTYPES,
792 sizeof(Conf_AllowedChannelTypes));
793 Conf_AllowRemoteOper = false;
795 Conf_AuthPing = false;
797 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
798 strcpy(Conf_CloakHost, "");
799 strcpy(Conf_CloakHostModeX, "");
800 strlcpy(Conf_CloakHostSalt, ngt_RandomStr(random, RANDOM_SALT_LEN),
801 sizeof(Conf_CloakHostSalt));
802 Conf_CloakUserToNick = false;
803 Conf_ConnectIPv4 = true;
805 Conf_ConnectIPv6 = true;
807 Conf_ConnectIPv6 = false;
809 strcpy(Conf_DefaultUserModes, "");
816 strcpy(Conf_IncludeDir, "");
817 Conf_MorePrivacy = false;
818 Conf_NoticeBeforeRegistration = false;
819 Conf_OperCanMode = false;
820 Conf_OperChanPAutoOp = true;
821 Conf_OperServerMode = false;
827 Conf_PAMIsOptional = false;
828 strcpy(Conf_PAMServiceName, "ngircd");
829 Conf_ScrubCTCP = false;
832 Conf_SyslogFacility = LOG_LOCAL5;
834 Conf_SyslogFacility = 0;
838 /* Initialize server configuration structures */
840 for (i = 0; i < MAX_SERVERS;
841 Init_Server_Struct(&Conf_Server[i++]));
846 * Get number of configured listening ports.
848 * @returns The number of ports (IPv4+IPv6) on which the server should listen.
853 size_t cnt = array_bytes(&Conf_ListenPorts);
855 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
861 * Read contents of a text file into an array.
863 * This function is used to read the MOTD and help text file, for example.
865 * @param Filename Name of the file to read.
866 * @return true, when the file has been read in.
869 Read_TextFile(const char *Filename, const char *Name, array *Destination)
871 char line[COMMAND_LEN];
875 if (*Filename == '\0')
878 fp = fopen(Filename, "r");
880 Config_Error(LOG_ERR, "Can't read %s file \"%s\": %s",
881 Name, Filename, strerror(errno));
885 array_free(Destination);
886 while (fgets(line, (int)sizeof line, fp)) {
887 ngt_TrimLastChr(line, '\n');
889 /* add text including \0 */
890 if (!array_catb(Destination, line, strlen(line) + 1)) {
891 Log(LOG_ERR, "Cannot read/add \"%s\", line %d: %s",
892 Filename, line_no, strerror(errno));
902 * Read ngIRCd configuration file.
904 * Please note that this function uses exit(1) on fatal errors and therefore
905 * can result in ngIRCd terminating!
907 * @param IsStarting Flag indicating if ngIRCd is starting or not.
908 * @returns true when the configuration file has been read
909 * successfully; false otherwise.
912 Read_Config(bool TestOnly, bool IsStarting)
914 const UINT16 defaultport = 6667;
915 char *ptr, file[FNAME_LEN];
916 struct dirent *entry;
921 if (!NGIRCd_ConfFile[0]) {
922 /* No configuration file name explicitly given on the command
923 * line, use defaults but ignore errors when this file can't be
925 strlcpy(file, SYSCONFDIR, sizeof(file));
926 strlcat(file, CONFIG_FILE, sizeof(file));
929 ptr = NGIRCd_ConfFile;
931 Config_Error(LOG_INFO, "Using %s configuration file \"%s\" ...",
932 !NGIRCd_ConfFile[0] ? "default" : "specified", ptr);
934 /* Open configuration file */
935 fd = fopen(ptr, "r");
937 if (NGIRCd_ConfFile[0]) {
938 Config_Error(LOG_ALERT,
939 "Can't read specified configuration file \"%s\": %s",
940 ptr, strerror(errno));
942 Config_Error(LOG_ALERT,
943 "%s exiting due to fatal errors!",
948 Config_Error(LOG_WARNING,
949 "Can't read default configuration file \"%s\": %s - Ignored.",
950 ptr, strerror(errno));
954 Set_Defaults(IsStarting);
957 Config_Error(LOG_INFO,
958 "Reading configuration from \"%s\" ...", ptr);
960 /* Clean up server configuration structure: mark all already
961 * configured servers as "once" so that they are deleted
962 * after the next disconnect and delete all unused servers.
963 * And delete all servers which are "duplicates" of servers
964 * that are already marked as "once" (such servers have been
965 * created by the last rehash but are now useless). */
966 for( i = 0; i < MAX_SERVERS; i++ ) {
967 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
969 /* This structure is in use ... */
970 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
971 /* Check for duplicates */
972 for( n = 0; n < MAX_SERVERS; n++ ) {
973 if( n == i ) continue;
975 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
976 Init_Server_Struct( &Conf_Server[n] );
977 LogDebug("Deleted unused duplicate server %d (kept %d).", n, i);
981 /* Mark server as "once" */
982 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
983 LogDebug("Marked server %d as \"once\"", i);
988 /* Initialize variables */
989 Init_Server_Struct( &New_Server );
990 New_Server_Idx = NONE;
996 Read_Config_File(ptr, fd);
1000 if (Conf_IncludeDir[0]) {
1001 /* Include directory was set in the main configuration file. So
1002 * use it and show errors. */
1003 dh = opendir(Conf_IncludeDir);
1005 Config_Error(LOG_ALERT,
1006 "Can't open include directory \"%s\": %s",
1007 Conf_IncludeDir, strerror(errno));
1008 } else if (!NGIRCd_ConfFile[0]) {
1009 /* No include dir set in the configuration file used (if any)
1010 * but no config file explicitly specified either: so use the
1011 * default include path here as well! */
1012 strlcpy(Conf_IncludeDir, SYSCONFDIR, sizeof(Conf_IncludeDir));
1013 strlcat(Conf_IncludeDir, CONFIG_DIR, sizeof(Conf_IncludeDir));
1014 dh = opendir(Conf_IncludeDir);
1017 /* Include further configuration files, if IncludeDir is available */
1019 while ((entry = readdir(dh)) != NULL) {
1020 ptr = strrchr(entry->d_name, '.');
1021 if (!ptr || strcasecmp(ptr, ".conf") != 0)
1023 snprintf(file, sizeof(file), "%s/%s",
1024 Conf_IncludeDir, entry->d_name);
1026 Config_Error(LOG_INFO,
1027 "Reading configuration from \"%s\" ...",
1029 fd = fopen(file, "r");
1031 Read_Config_File(file, fd);
1034 Config_Error(LOG_ALERT,
1035 "Can't read configuration \"%s\": %s",
1036 file, strerror(errno));
1041 /* Check if there is still a server to add */
1042 if( New_Server.name[0] ) {
1043 /* Copy data to "real" server structure */
1044 assert( New_Server_Idx > NONE );
1045 Conf_Server[New_Server_Idx] = New_Server;
1048 /* not a single listening port? Add default. */
1049 if (no_listenports() &&
1050 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
1052 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
1053 (unsigned int) defaultport, strerror(errno));
1058 if (!Conf_ListenAddress)
1059 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
1061 if (!Conf_ListenAddress) {
1062 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
1066 /* No MOTD phrase configured? (re)try motd file. */
1067 if (array_bytes(&Conf_Motd) == 0) {
1068 if (Read_TextFile(Conf_MotdFile, "MOTD", &Conf_Motd))
1069 Using_MotdFile = true;
1072 /* Try to read ngIRCd help text file. */
1073 (void)Read_TextFile(Conf_HelpFile, "help text", &Conf_Helptext);
1074 if (!array_bytes(&Conf_Helptext))
1075 Config_Error(LOG_WARNING,
1076 "No help text available, HELP command will be of limited use.");
1079 /* Make sure that all SSL-related files are readable */
1080 CheckFileReadable("CertFile", Conf_SSLOptions.CertFile);
1081 CheckFileReadable("DHFile", Conf_SSLOptions.DHFile);
1082 CheckFileReadable("KeyFile", Conf_SSLOptions.KeyFile);
1084 /* Set the default ciphers if none were configured */
1085 if (!Conf_SSLOptions.CipherList)
1086 Conf_SSLOptions.CipherList = strdup_warn(DEFAULT_CIPHERS);
1093 * Read in and handle a configuration file.
1095 * @param File Name of the configuration file.
1096 * @param fd File descriptor already opened for reading.
1099 Read_Config_File(const char *File, FILE *fd)
1101 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
1105 /* Read configuration file */
1108 if (!fgets(str, sizeof(str), fd))
1113 /* Skip comments and empty lines */
1114 if (str[0] == ';' || str[0] == '#' || str[0] == '\0')
1117 if (strlen(str) >= sizeof(str) - 1) {
1118 Config_Error(LOG_WARNING, "%s, line %d too long!",
1123 /* Is this the beginning of a new section? */
1124 if ((str[0] == '[') && (str[strlen(str) - 1] == ']')) {
1125 strlcpy(section, str, sizeof(section));
1126 if (strcasecmp(section, "[GLOBAL]") == 0
1127 || strcasecmp(section, "[LIMITS]") == 0
1128 || strcasecmp(section, "[OPTIONS]") == 0
1130 || strcasecmp(section, "[SSL]") == 0
1135 if (strcasecmp(section, "[SERVER]") == 0) {
1136 /* Check if there is already a server to add */
1137 if (New_Server.name[0]) {
1138 /* Copy data to "real" server structure */
1139 assert(New_Server_Idx > NONE);
1140 Conf_Server[New_Server_Idx] =
1144 /* Re-init structure for new server */
1145 Init_Server_Struct(&New_Server);
1147 /* Search unused item in server configuration structure */
1148 for (i = 0; i < MAX_SERVERS; i++) {
1149 /* Is this item used? */
1150 if (!Conf_Server[i].name[0])
1153 if (i >= MAX_SERVERS) {
1154 /* Oops, no free item found! */
1155 Config_Error(LOG_ERR,
1156 "Too many servers configured.");
1157 New_Server_Idx = NONE;
1163 if (strcasecmp(section, "[CHANNEL]") == 0) {
1164 count = array_length(&Conf_Channels,
1169 sizeof(struct Conf_Channel), count)) {
1170 Config_Error(LOG_ERR,
1171 "Could not allocate memory for new operator (line %d)",
1177 if (strcasecmp(section, "[OPERATOR]") == 0) {
1178 count = array_length(&Conf_Opers,
1179 sizeof(struct Conf_Oper));
1180 if (!array_alloc(&Conf_Opers,
1181 sizeof(struct Conf_Oper),
1183 Config_Error(LOG_ERR,
1184 "Could not allocate memory for new channel (line &d)",
1190 Config_Error(LOG_ERR,
1191 "%s, line %d: Unknown section \"%s\"!",
1192 File, line, section);
1195 if (section[0] == 0x1)
1198 /* Split line into variable name and parameters */
1199 ptr = strchr(str, '=');
1201 Config_Error(LOG_ERR, "%s, line %d: Syntax error!",
1211 if (strcasecmp(section, "[GLOBAL]") == 0)
1212 Handle_GLOBAL(File, line, var, arg);
1213 else if (strcasecmp(section, "[LIMITS]") == 0)
1214 Handle_LIMITS(File, line, var, arg);
1215 else if (strcasecmp(section, "[OPTIONS]") == 0)
1216 Handle_OPTIONS(File, line, var, arg);
1218 else if (strcasecmp(section, "[SSL]") == 0)
1219 Handle_SSL(File, line, var, arg);
1221 else if (strcasecmp(section, "[OPERATOR]") == 0)
1222 Handle_OPERATOR(File, line, var, arg);
1223 else if (strcasecmp(section, "[SERVER]") == 0)
1224 Handle_SERVER(File, line, var, arg);
1225 else if (strcasecmp(section, "[CHANNEL]") == 0)
1226 Handle_CHANNEL(File, line, var, arg);
1228 Config_Error(LOG_ERR,
1229 "%s, line %d: Variable \"%s\" outside section!",
1235 * Check whether a string argument is "true" or "false".
1237 * @param Arg Input string.
1238 * @returns true if the input string has been parsed as "yes", "true"
1239 * (case insensitive) or a non-zero integer value.
1242 Check_ArgIsTrue(const char *Arg)
1244 if (strcasecmp(Arg, "yes") == 0)
1246 if (strcasecmp(Arg, "true") == 0)
1255 * Handle setting of "MaxNickLength".
1257 * @param Line Line number in configuration file.
1258 * @raram Arg Input string.
1259 * @returns New configured maximum nickname length.
1262 Handle_MaxNickLength(const char *File, int Line, const char *Arg)
1266 new = (unsigned) atoi(Arg) + 1;
1267 if (new > CLIENT_NICK_LEN) {
1268 Config_Error(LOG_WARNING,
1269 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
1270 File, Line, CLIENT_NICK_LEN - 1);
1271 return CLIENT_NICK_LEN;
1274 Config_Error(LOG_WARNING,
1275 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
1283 * Output a warning messages if IDENT is configured but not compiled in.
1286 WarnIdent(const char UNUSED *File, int UNUSED Line)
1290 /* user has enabled ident lookups explicitly, but ... */
1291 Config_Error(LOG_WARNING,
1292 "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
1299 * Output a warning messages if IPv6 is configured but not compiled in.
1302 WarnIPv6(const char UNUSED *File, int UNUSED Line)
1305 if (Conf_ConnectIPv6) {
1306 /* user has enabled IPv6 explicitly, but ... */
1307 Config_Error(LOG_WARNING,
1308 "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
1315 * Output a warning messages if PAM is configured but not compiled in.
1318 WarnPAM(const char UNUSED *File, int UNUSED Line)
1322 Config_Error(LOG_WARNING,
1323 "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
1331 * Handle variable in [Global] configuration section.
1333 * @param Line Line number in configuration file.
1334 * @param Var Variable name.
1335 * @param Arg Variable argument.
1338 Handle_GLOBAL(const char *File, int Line, char *Var, char *Arg )
1345 assert(File != NULL);
1347 assert(Var != NULL);
1348 assert(Arg != NULL);
1350 if (strcasecmp(Var, "Name") == 0) {
1351 len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
1352 if (len >= sizeof(Conf_ServerName))
1353 Config_Error_TooLong(File, Line, Var);
1356 if (strcasecmp(Var, "AdminInfo1") == 0) {
1357 len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
1358 if (len >= sizeof(Conf_ServerAdmin1))
1359 Config_Error_TooLong(File, Line, Var);
1362 if (strcasecmp(Var, "AdminInfo2") == 0) {
1363 len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
1364 if (len >= sizeof(Conf_ServerAdmin2))
1365 Config_Error_TooLong(File, Line, Var);
1368 if (strcasecmp(Var, "AdminEMail") == 0) {
1369 len = strlcpy(Conf_ServerAdminMail, Arg,
1370 sizeof(Conf_ServerAdminMail));
1371 if (len >= sizeof(Conf_ServerAdminMail))
1372 Config_Error_TooLong(File, Line, Var);
1375 if (strcasecmp(Var, "Info") == 0) {
1376 len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
1377 if (len >= sizeof(Conf_ServerInfo))
1378 Config_Error_TooLong(File, Line, Var);
1381 if (strcasecmp(Var, "HelpFile") == 0) {
1382 len = strlcpy(Conf_HelpFile, Arg, sizeof(Conf_HelpFile));
1383 if (len >= sizeof(Conf_HelpFile))
1384 Config_Error_TooLong(File, Line, Var);
1387 if (strcasecmp(Var, "Listen") == 0) {
1388 if (Conf_ListenAddress) {
1389 Config_Error(LOG_ERR,
1390 "Multiple Listen= options, ignoring: %s",
1394 Conf_ListenAddress = strdup_warn(Arg);
1395 /* If allocation fails, we're in trouble: we cannot ignore the
1396 * error -- otherwise ngircd would listen on all interfaces. */
1397 if (!Conf_ListenAddress) {
1398 Config_Error(LOG_ALERT,
1399 "%s exiting due to fatal errors!",
1405 if (strcasecmp(Var, "MotdFile") == 0) {
1406 len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
1407 if (len >= sizeof(Conf_MotdFile))
1408 Config_Error_TooLong(File, Line, Var);
1411 if (strcasecmp(Var, "MotdPhrase") == 0) {
1416 Config_Error_TooLong(File, Line, Var);
1419 if (!array_copyb(&Conf_Motd, Arg, len + 1))
1420 Config_Error(LOG_WARNING,
1421 "%s, line %d: Could not append MotdPhrase: %s",
1422 File, Line, strerror(errno));
1423 Using_MotdFile = false;
1426 if (strcasecmp(Var, "Network") == 0) {
1427 len = strlcpy(Conf_Network, Arg, sizeof(Conf_Network));
1428 if (len >= sizeof(Conf_Network))
1429 Config_Error_TooLong(File, Line, Var);
1430 ptr = strchr(Conf_Network, ' ');
1432 Config_Error(LOG_WARNING,
1433 "%s, line %d: \"Network\" can't contain spaces!",
1439 if(strcasecmp(Var, "Password") == 0) {
1440 len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
1441 if (len >= sizeof(Conf_ServerPwd))
1442 Config_Error_TooLong(File, Line, Var);
1445 if (strcasecmp(Var, "PidFile") == 0) {
1446 len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
1447 if (len >= sizeof(Conf_PidFile))
1448 Config_Error_TooLong(File, Line, Var);
1451 if (strcasecmp(Var, "Ports") == 0) {
1452 ports_parse(&Conf_ListenPorts, File, Line, Arg);
1455 if (strcasecmp(Var, "ServerGID") == 0) {
1456 grp = getgrnam(Arg);
1458 Conf_GID = grp->gr_gid;
1460 Conf_GID = (unsigned int)atoi(Arg);
1461 if (!Conf_GID && strcmp(Arg, "0"))
1462 Config_Error(LOG_WARNING,
1463 "%s, line %d: Value of \"%s\" is not a valid group name or ID!",
1468 if (strcasecmp(Var, "ServerUID") == 0) {
1469 pwd = getpwnam(Arg);
1471 Conf_UID = pwd->pw_uid;
1473 Conf_UID = (unsigned int)atoi(Arg);
1474 if (!Conf_UID && strcmp(Arg, "0"))
1475 Config_Error(LOG_WARNING,
1476 "%s, line %d: Value of \"%s\" is not a valid user name or ID!",
1482 Config_Error_Section(File, Line, Var, "Global");
1486 * Handle variable in [Limits] configuration section.
1488 * @param Line Line number in configuration file.
1489 * @param Var Variable name.
1490 * @param Arg Variable argument.
1493 Handle_LIMITS(const char *File, int Line, char *Var, char *Arg)
1495 assert(File != NULL);
1497 assert(Var != NULL);
1498 assert(Arg != NULL);
1500 if (strcasecmp(Var, "ConnectRetry") == 0) {
1501 Conf_ConnectRetry = atoi(Arg);
1502 if (Conf_ConnectRetry < 5) {
1503 Config_Error(LOG_WARNING,
1504 "%s, line %d: Value of \"ConnectRetry\" too low!",
1506 Conf_ConnectRetry = 5;
1510 if (strcasecmp(Var, "IdleTimeout") == 0) {
1511 Conf_IdleTimeout = atoi(Arg);
1512 if (!Conf_IdleTimeout && strcmp(Arg, "0"))
1513 Config_Error_NaN(File, Line, Var);
1516 if (strcasecmp(Var, "MaxConnections") == 0) {
1517 Conf_MaxConnections = atoi(Arg);
1518 if (!Conf_MaxConnections && strcmp(Arg, "0"))
1519 Config_Error_NaN(File, Line, Var);
1522 if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
1523 Conf_MaxConnectionsIP = atoi(Arg);
1524 if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
1525 Config_Error_NaN(File, Line, Var);
1528 if (strcasecmp(Var, "MaxJoins") == 0) {
1529 Conf_MaxJoins = atoi(Arg);
1530 if (!Conf_MaxJoins && strcmp(Arg, "0"))
1531 Config_Error_NaN(File, Line, Var);
1534 if (strcasecmp(Var, "MaxNickLength") == 0) {
1535 Conf_MaxNickLength = Handle_MaxNickLength(File, Line, Arg);
1538 if (strcasecmp(Var, "MaxListSize") == 0) {
1539 Conf_MaxListSize = atoi(Arg);
1540 if (!Conf_MaxListSize && strcmp(Arg, "0"))
1541 Config_Error_NaN(File, Line, Var);
1544 if (strcasecmp(Var, "MaxPenaltyTime") == 0) {
1545 Conf_MaxPenaltyTime = atol(Arg);
1546 if (Conf_MaxPenaltyTime < -1)
1547 Conf_MaxPenaltyTime = -1; /* "unlimited" */
1550 if (strcasecmp(Var, "PingTimeout") == 0) {
1551 Conf_PingTimeout = atoi(Arg);
1552 if (Conf_PingTimeout < 5) {
1553 Config_Error(LOG_WARNING,
1554 "%s, line %d: Value of \"PingTimeout\" too low!",
1556 Conf_PingTimeout = 5;
1560 if (strcasecmp(Var, "PongTimeout") == 0) {
1561 Conf_PongTimeout = atoi(Arg);
1562 if (Conf_PongTimeout < 5) {
1563 Config_Error(LOG_WARNING,
1564 "%s, line %d: Value of \"PongTimeout\" too low!",
1566 Conf_PongTimeout = 5;
1571 Config_Error_Section(File, Line, Var, "Limits");
1575 * Handle variable in [Options] configuration section.
1577 * @param Line Line number in configuration file.
1578 * @param Var Variable name.
1579 * @param Arg Variable argument.
1582 Handle_OPTIONS(const char *File, int Line, char *Var, char *Arg)
1587 assert(File != NULL);
1589 assert(Var != NULL);
1590 assert(Arg != NULL);
1592 if (strcasecmp(Var, "AllowedChannelTypes") == 0) {
1594 Conf_AllowedChannelTypes[0] = '\0';
1596 if (strchr(Conf_AllowedChannelTypes, *p)) {
1597 /* Prefix is already included; ignore it */
1602 if (strchr(CHANTYPES, *p)) {
1603 len = strlen(Conf_AllowedChannelTypes) + 1;
1604 assert(len < sizeof(Conf_AllowedChannelTypes));
1605 Conf_AllowedChannelTypes[len - 1] = *p;
1606 Conf_AllowedChannelTypes[len] = '\0';
1608 Config_Error(LOG_WARNING,
1609 "%s, line %d: Unknown channel prefix \"%c\" in \"AllowedChannelTypes\"!",
1616 if (strcasecmp(Var, "AllowRemoteOper") == 0) {
1617 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1620 if (strcasecmp(Var, "ChrootDir") == 0) {
1621 len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
1622 if (len >= sizeof(Conf_Chroot))
1623 Config_Error_TooLong(File, Line, Var);
1626 if (strcasecmp(Var, "CloakHost") == 0) {
1627 len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
1628 if (len >= sizeof(Conf_CloakHost))
1629 Config_Error_TooLong(File, Line, Var);
1632 if (strcasecmp(Var, "CloakHostModeX") == 0) {
1633 len = strlcpy(Conf_CloakHostModeX, Arg, sizeof(Conf_CloakHostModeX));
1634 if (len >= sizeof(Conf_CloakHostModeX))
1635 Config_Error_TooLong(File, Line, Var);
1638 if (strcasecmp(Var, "CloakHostSalt") == 0) {
1639 len = strlcpy(Conf_CloakHostSalt, Arg, sizeof(Conf_CloakHostSalt));
1640 if (len >= sizeof(Conf_CloakHostSalt))
1641 Config_Error_TooLong(File, Line, Var);
1644 if (strcasecmp(Var, "CloakUserToNick") == 0) {
1645 Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
1648 if (strcasecmp(Var, "ConnectIPv6") == 0) {
1649 Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
1650 WarnIPv6(File, Line);
1653 if (strcasecmp(Var, "ConnectIPv4") == 0) {
1654 Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
1657 if (strcasecmp(Var, "DefaultUserModes") == 0) {
1659 Conf_DefaultUserModes[0] = '\0';
1661 if (strchr(Conf_DefaultUserModes, *p)) {
1662 /* Mode is already included; ignore it */
1667 if (strchr(USERMODES, *p)) {
1668 len = strlen(Conf_DefaultUserModes) + 1;
1669 assert(len < sizeof(Conf_DefaultUserModes));
1670 Conf_DefaultUserModes[len - 1] = *p;
1671 Conf_DefaultUserModes[len] = '\0';
1673 Config_Error(LOG_WARNING,
1674 "%s, line %d: Unknown user mode \"%c\" in \"DefaultUserModes\"!",
1681 if (strcasecmp(Var, "DNS") == 0) {
1682 Conf_DNS = Check_ArgIsTrue(Arg);
1685 if (strcasecmp(Var, "Ident") == 0) {
1686 Conf_Ident = Check_ArgIsTrue(Arg);
1687 WarnIdent(File, Line);
1690 if (strcasecmp(Var, "IncludeDir") == 0) {
1691 if (Conf_IncludeDir[0]) {
1692 Config_Error(LOG_ERR,
1693 "%s, line %d: Can't overwrite value of \"IncludeDir\" variable!",
1697 len = strlcpy(Conf_IncludeDir, Arg, sizeof(Conf_IncludeDir));
1698 if (len >= sizeof(Conf_IncludeDir))
1699 Config_Error_TooLong(File, Line, Var);
1702 if (strcasecmp(Var, "MorePrivacy") == 0) {
1703 Conf_MorePrivacy = Check_ArgIsTrue(Arg);
1706 if (strcasecmp(Var, "NoticeBeforeRegistration") == 0) {
1707 Conf_NoticeBeforeRegistration = Check_ArgIsTrue(Arg);
1710 if (strcasecmp(Var, "OperCanUseMode") == 0) {
1711 Conf_OperCanMode = Check_ArgIsTrue(Arg);
1714 if (strcasecmp(Var, "OperChanPAutoOp") == 0) {
1715 Conf_OperChanPAutoOp = Check_ArgIsTrue(Arg);
1718 if (strcasecmp(Var, "OperServerMode") == 0) {
1719 Conf_OperServerMode = Check_ArgIsTrue(Arg);
1722 if (strcasecmp(Var, "PAM") == 0) {
1723 Conf_PAM = Check_ArgIsTrue(Arg);
1724 WarnPAM(File, Line);
1727 if (strcasecmp(Var, "PAMIsOptional") == 0 ) {
1728 Conf_PAMIsOptional = Check_ArgIsTrue(Arg);
1731 if (strcasecmp(Var, "PAMServiceName") == 0) {
1732 len = strlcpy(Conf_PAMServiceName, Arg, sizeof(Conf_PAMServiceName));
1733 if (len >= sizeof(Conf_PAMServiceName))
1734 Config_Error_TooLong(File, Line, Var);
1738 if (strcasecmp(Var, "RequireAuthPing") == 0) {
1739 Conf_AuthPing = Check_ArgIsTrue(Arg);
1743 if (strcasecmp(Var, "ScrubCTCP") == 0) {
1744 Conf_ScrubCTCP = Check_ArgIsTrue(Arg);
1748 if (strcasecmp(Var, "SyslogFacility") == 0) {
1749 Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
1750 Conf_SyslogFacility);
1754 if (strcasecmp(Var, "WebircPassword") == 0) {
1755 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
1756 if (len >= sizeof(Conf_WebircPwd))
1757 Config_Error_TooLong(File, Line, Var);
1761 Config_Error_Section(File, Line, Var, "Options");
1767 * Handle variable in [SSL] configuration section.
1769 * @param Line Line number in configuration file.
1770 * @param Var Variable name.
1771 * @param Arg Variable argument.
1774 Handle_SSL(const char *File, int Line, char *Var, char *Arg)
1776 assert(File != NULL);
1778 assert(Var != NULL);
1779 assert(Arg != NULL);
1781 if (strcasecmp(Var, "CertFile") == 0) {
1782 assert(Conf_SSLOptions.CertFile == NULL);
1783 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1786 if (strcasecmp(Var, "DHFile") == 0) {
1787 assert(Conf_SSLOptions.DHFile == NULL);
1788 Conf_SSLOptions.DHFile = strdup_warn(Arg);
1791 if (strcasecmp(Var, "KeyFile") == 0) {
1792 assert(Conf_SSLOptions.KeyFile == NULL);
1793 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1796 if (strcasecmp(Var, "KeyFilePassword") == 0) {
1797 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1798 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1799 Config_Error(LOG_ERR,
1800 "%s, line %d (section \"SSL\"): Could not copy %s: %s!",
1801 File, Line, Var, strerror(errno));
1804 if (strcasecmp(Var, "Ports") == 0) {
1805 ports_parse(&Conf_SSLOptions.ListenPorts, File, Line, Arg);
1808 if (strcasecmp(Var, "CipherList") == 0) {
1809 assert(Conf_SSLOptions.CipherList == NULL);
1810 Conf_SSLOptions.CipherList = strdup_warn(Arg);
1813 if (strcasecmp(Var, "CAFile") == 0) {
1814 assert(Conf_SSLOptions.CAFile == NULL);
1815 Conf_SSLOptions.CAFile = strdup_warn(Arg);
1818 if (strcasecmp(Var, "CRLFile") == 0) {
1819 assert(Conf_SSLOptions.CRLFile == NULL);
1820 Conf_SSLOptions.CRLFile = strdup_warn(Arg);
1824 Config_Error_Section(File, Line, Var, "SSL");
1830 * Handle variable in [Operator] configuration section.
1832 * @param Line Line number in configuration file.
1833 * @param Var Variable name.
1834 * @param Arg Variable argument.
1837 Handle_OPERATOR(const char *File, int Line, char *Var, char *Arg )
1840 struct Conf_Oper *op;
1842 assert( File != NULL );
1844 assert( Var != NULL );
1845 assert( Arg != NULL );
1847 op = array_get(&Conf_Opers, sizeof(*op),
1848 array_length(&Conf_Opers, sizeof(*op)) - 1);
1852 if (strcasecmp(Var, "Name") == 0) {
1853 /* Name of IRC operator */
1854 len = strlcpy(op->name, Arg, sizeof(op->name));
1855 if (len >= sizeof(op->name))
1856 Config_Error_TooLong(File, Line, Var);
1859 if (strcasecmp(Var, "Password") == 0) {
1860 /* Password of IRC operator */
1861 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1862 if (len >= sizeof(op->pwd))
1863 Config_Error_TooLong(File, Line, Var);
1866 if (strcasecmp(Var, "Mask") == 0) {
1868 return; /* Hostname already configured */
1869 op->mask = strdup_warn( Arg );
1873 Config_Error_Section(File, Line, Var, "Operator");
1877 * Handle variable in [Server] configuration section.
1879 * @param Line Line number in configuration file.
1880 * @param Var Variable name.
1881 * @param Arg Variable argument.
1884 Handle_SERVER(const char *File, int Line, char *Var, char *Arg )
1889 assert( File != NULL );
1891 assert( Var != NULL );
1892 assert( Arg != NULL );
1894 /* Ignore server block if no space is left in server configuration structure */
1895 if( New_Server_Idx <= NONE ) return;
1897 if( strcasecmp( Var, "Host" ) == 0 ) {
1898 /* Hostname of the server */
1899 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1900 if (len >= sizeof( New_Server.host ))
1901 Config_Error_TooLong(File, Line, Var);
1904 if( strcasecmp( Var, "Name" ) == 0 ) {
1905 /* Name of the server ("Nick"/"ID") */
1906 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1907 if (len >= sizeof( New_Server.name ))
1908 Config_Error_TooLong(File, Line, Var);
1911 if (strcasecmp(Var, "Bind") == 0) {
1912 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1915 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1919 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1920 /* Password of this server which is sent to the peer */
1922 Config_Error(LOG_ERR,
1923 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1926 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1927 if (len >= sizeof( New_Server.pwd_in ))
1928 Config_Error_TooLong(File, Line, Var);
1931 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1932 /* Passwort of the peer which must be received */
1933 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1934 if (len >= sizeof( New_Server.pwd_out ))
1935 Config_Error_TooLong(File, Line, Var);
1938 if( strcasecmp( Var, "Port" ) == 0 ) {
1939 /* Port to which this server should connect */
1941 if (port >= 0 && port < 0xFFFF)
1942 New_Server.port = (UINT16)port;
1944 Config_Error(LOG_ERR,
1945 "%s, line %d (section \"Server\"): Illegal port number %ld!",
1950 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1951 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1954 if (strcasecmp(Var, "SSLVerify") == 0) {
1955 New_Server.SSLVerify = Check_ArgIsTrue(Arg);
1959 if( strcasecmp( Var, "Group" ) == 0 ) {
1961 New_Server.group = atoi( Arg );
1962 if (!New_Server.group && strcmp(Arg, "0"))
1963 Config_Error_NaN(File, Line, Var);
1966 if( strcasecmp( Var, "Passive" ) == 0 ) {
1967 if (Check_ArgIsTrue(Arg))
1968 New_Server.flags |= CONF_SFLAG_DISABLED;
1971 if (strcasecmp(Var, "ServiceMask") == 0) {
1972 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1973 sizeof(New_Server.svs_mask));
1974 if (len >= sizeof(New_Server.svs_mask))
1975 Config_Error_TooLong(File, Line, Var);
1979 Config_Error_Section(File, Line, Var, "Server");
1983 * Copy channel name into channel structure.
1985 * If the channel name is not valid because of a missing prefix ('#', '&'),
1986 * a default prefix of '#' will be added.
1988 * @param new_chan New already allocated channel structure.
1989 * @param name Name of the new channel.
1990 * @returns true on success, false otherwise.
1993 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1995 size_t size = sizeof(new_chan->name);
1996 char *dest = new_chan->name;
1998 if (!Channel_IsValidName(name)) {
2000 * maybe user forgot to add a '#'.
2001 * This is only here for user convenience.
2007 return size > strlcpy(dest, name, size);
2011 * Handle variable in [Channel] configuration section.
2013 * @param Line Line number in configuration file.
2014 * @param Var Variable name.
2015 * @param Arg Variable argument.
2018 Handle_CHANNEL(const char *File, int Line, char *Var, char *Arg)
2021 struct Conf_Channel *chan;
2023 assert( File != NULL );
2025 assert( Var != NULL );
2026 assert( Arg != NULL );
2028 chan = array_get(&Conf_Channels, sizeof(*chan),
2029 array_length(&Conf_Channels, sizeof(*chan)) - 1);
2033 if (strcasecmp(Var, "Name") == 0) {
2034 if (!Handle_Channelname(chan, Arg))
2035 Config_Error_TooLong(File, Line, Var);
2038 if (strcasecmp(Var, "Modes") == 0) {
2040 if(chan->modes_num >= sizeof(chan->modes)) {
2041 Config_Error(LOG_ERR, "Too many Modes, option ignored.");
2044 chan->modes[chan->modes_num++] = strndup(Arg, COMMAND_LEN);
2045 if(strlen(Arg) >= COMMAND_LEN)
2046 Config_Error_TooLong(File, Line, Var);
2049 if( strcasecmp( Var, "Topic" ) == 0 ) {
2051 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
2052 if (len >= sizeof(chan->topic))
2053 Config_Error_TooLong(File, Line, Var);
2056 if( strcasecmp( Var, "Autojoin" ) == 0 ) {
2057 /* Check autojoin */
2058 chan->autojoin = Check_ArgIsTrue(Arg);
2061 if( strcasecmp( Var, "Key" ) == 0 ) {
2062 /* Initial Channel Key (mode k) */
2063 len = strlcpy(chan->key, Arg, sizeof(chan->key));
2064 if (len >= sizeof(chan->key))
2065 Config_Error_TooLong(File, Line, Var);
2066 Config_Error(LOG_WARNING,
2067 "%s, line %d (section \"Channel\"): \"%s\" is deprecated here, use \"Modes = +k <key>\"!",
2071 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
2072 /* maximum user limit, mode l */
2073 chan->maxusers = (unsigned long) atol(Arg);
2074 if (!chan->maxusers && strcmp(Arg, "0"))
2075 Config_Error_NaN(File, Line, Var);
2076 Config_Error(LOG_WARNING,
2077 "%s, line %d (section \"Channel\"): \"%s\" is deprecated here, use \"Modes = +l <limit>\"!",
2081 if (strcasecmp(Var, "KeyFile") == 0) {
2083 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
2084 if (len >= sizeof(chan->keyfile))
2085 Config_Error_TooLong(File, Line, Var);
2089 Config_Error_Section(File, Line, Var, "Channel");
2093 * Validate server configuration.
2095 * Please note that this function uses exit(1) on fatal errors and therefore
2096 * can result in ngIRCd terminating!
2098 * @param Configtest true if the daemon has been called with "--configtest".
2099 * @param Rehash true if re-reading configuration on runtime.
2100 * @returns true if configuration is valid.
2103 Validate_Config(bool Configtest, bool Rehash)
2105 /* Validate configuration settings. */
2107 int i, servers, servers_once;
2109 bool config_valid = true;
2112 /* Emit a warning when the config file is not a full path name */
2113 if (NGIRCd_ConfFile[0] && NGIRCd_ConfFile[0] != '/') {
2114 Config_Error(LOG_WARNING,
2115 "Not specifying a full path name to \"%s\" can cause problems when rehashing the server!",
2119 if (!Conf_ServerName[0]) {
2120 /* No server name configured, try to get a sane name from the
2121 * host name. Note: the IRC server name MUST contain
2122 * at least one dot, so the "node name" is not sufficient! */
2123 gethostname(Conf_ServerName, sizeof(Conf_ServerName));
2125 /* Try to get a proper host name ... */
2126 h = gethostbyname(Conf_ServerName);
2128 strlcpy(Conf_ServerName, h->h_name,
2129 sizeof(Conf_ServerName));
2131 if (!strchr(Conf_ServerName, '.')) {
2132 /* (Still) No dot in the name! */
2133 strlcat(Conf_ServerName, ".host",
2134 sizeof(Conf_ServerName));
2136 Config_Error(LOG_WARNING,
2137 "No server name configured, using host name \"%s\".",
2141 /* Validate configured server name, see RFC 2812 section 2.3.1 */
2142 ptr = Conf_ServerName;
2144 if (*ptr >= 'a' && *ptr <= 'z') continue;
2145 if (*ptr >= 'A' && *ptr <= 'Z') continue;
2146 if (*ptr >= '0' && *ptr <= '9') continue;
2147 if (ptr > Conf_ServerName) {
2148 if (*ptr == '.' || *ptr == '-')
2151 Conf_ServerName[0] = '\0';
2155 if (!Conf_ServerName[0] || !strchr(Conf_ServerName, '.')) {
2156 config_valid = false;
2157 Config_Error(LOG_ALERT,
2158 "No (valid) server name configured (section 'Global': 'Name')!");
2159 if (!Configtest && !Rehash) {
2160 Config_Error(LOG_ALERT,
2161 "%s exiting due to fatal errors!",
2168 if (!Conf_ServerAdminMail[0]) {
2169 /* No administrative contact configured! */
2170 config_valid = false;
2171 Config_Error(LOG_ALERT,
2172 "No administrator email address configured ('AdminEMail')!");
2174 Config_Error(LOG_ALERT,
2175 "%s exiting due to fatal errors!",
2182 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
2183 && !Conf_ServerAdminMail[0]) {
2184 /* No administrative information configured! */
2185 Config_Error(LOG_WARNING,
2186 "No administrative information configured but required by RFC!");
2190 if (Conf_PAM && Conf_ServerPwd[0])
2191 Config_Error(LOG_ERR,
2192 "This server uses PAM, \"Password\" in [Global] section will be ignored!");
2195 if (Conf_MaxPenaltyTime != -1)
2196 Config_Error(LOG_WARNING,
2197 "Maximum penalty increase ('MaxPenaltyTime') is set to %ld, this is not recommended!",
2198 Conf_MaxPenaltyTime);
2200 servers = servers_once = 0;
2201 for (i = 0; i < MAX_SERVERS; i++) {
2202 if (Conf_Server[i].name[0]) {
2204 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
2208 LogDebug("Configuration: Operators=%ld, Servers=%d[%d], Channels=%ld",
2209 array_length(&Conf_Opers, sizeof(struct Conf_Oper)),
2210 servers, servers_once,
2211 array_length(&Conf_Channels, sizeof(struct Conf_Channel)));
2213 return config_valid;
2217 * Output "line too long" warning.
2219 * @param Line Line number in configuration file.
2220 * @param Item Affected variable name.
2223 Config_Error_TooLong(const char *File, const int Line, const char *Item)
2225 Config_Error(LOG_WARNING, "%s, line %d: Value of \"%s\" too long!",
2230 * Output "unknown variable" warning.
2232 * @param Line Line number in configuration file.
2233 * @param Item Affected variable name.
2234 * @param Section Section name.
2237 Config_Error_Section(const char *File, const int Line, const char *Item,
2238 const char *Section)
2240 Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
2241 File, Line, Section, Item);
2245 * Output "not a number" warning.
2247 * @param Line Line number in configuration file.
2248 * @param Item Affected variable name.
2251 Config_Error_NaN(const char *File, const int Line, const char *Item )
2253 Config_Error(LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
2258 * Output configuration error to console and/or logfile.
2260 * On runtime, the normal log functions of the daemon are used. But when
2261 * testing the configuration ("--configtest"), all messages go directly
2264 * @param Level Severity level of the message.
2265 * @param Format Format string; see printf() function.
2268 static void Config_Error( const int Level, const char *Format, ... )
2270 static void Config_Error( Level, Format, va_alist )
2276 char msg[MAX_LOG_MSG_LEN];
2279 assert( Format != NULL );
2282 va_start( ap, Format );
2286 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
2290 if (Level <= LOG_WARNING)
2291 printf(" - %s\n", msg);
2295 Log(Level, "%s", msg);
2300 * Dump internal state of the "configuration module".
2303 Conf_DebugDump(void)
2307 LogDebug("Configured servers:");
2308 for (i = 0; i < MAX_SERVERS; i++) {
2309 if (! Conf_Server[i].name[0])
2312 " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
2313 Conf_Server[i].name, Conf_Server[i].host,
2314 Conf_Server[i].port, Conf_Server[i].lasttry,
2315 Conf_Server[i].group, Conf_Server[i].flags,
2316 Conf_Server[i].conn_id);
2322 * Initialize server configuration structure to default values.
2324 * @param Server Pointer to server structure to initialize.
2327 Init_Server_Struct( CONF_SERVER *Server )
2329 assert( Server != NULL );
2331 memset( Server, 0, sizeof (CONF_SERVER) );
2333 Server->group = NONE;
2334 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
2336 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
2338 Proc_InitStruct(&Server->res_stat);
2339 Server->conn_id = NONE;
2340 memset(&Server->bind_addr, 0, sizeof(Server->bind_addr));
2343 /* Verify SSL connections by default! */
2344 Server->SSLVerify = true;