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>
38 #ifdef HAVE_SYS_RESOURCE_H
39 # include <sys/resource.h>
51 static bool Use_Log = true, Using_MotdFile = true;
52 static CONF_SERVER New_Server;
53 static int New_Server_Idx;
55 static char Conf_MotdFile[FNAME_LEN];
56 static char Conf_HelpFile[FNAME_LEN];
57 static char Conf_IncludeDir[FNAME_LEN];
59 static void Set_Defaults PARAMS(( bool InitServers ));
60 static bool Read_Config PARAMS(( bool TestOnly, bool IsStarting ));
61 static void Read_Config_File PARAMS(( const char *File, FILE *fd ));
62 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
64 static void Handle_GLOBAL PARAMS((const char *File, int Line,
65 char *Var, char *Arg ));
66 static void Handle_LIMITS PARAMS((const char *File, int Line,
67 char *Var, char *Arg ));
68 static void Handle_OPTIONS PARAMS((const char *File, int Line,
69 char *Var, char *Arg ));
70 static void Handle_OPERATOR PARAMS((const char *File, int Line,
71 char *Var, char *Arg ));
72 static void Handle_SERVER PARAMS((const char *File, int Line,
73 char *Var, char *Arg ));
74 static void Handle_CHANNEL PARAMS((const char *File, int Line,
75 char *Var, char *Arg ));
77 static void Config_Error PARAMS((const int Level, const char *Format, ...));
79 static void Config_Error_NaN PARAMS((const char *File, const int LINE,
81 static void Config_Error_Section PARAMS((const char *File, const int Line,
82 const char *Item, const char *Section));
83 static void Config_Error_TooLong PARAMS((const char *File, const int LINE,
86 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
90 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
92 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
96 #define DEFAULT_CIPHERS "HIGH:!aNULL:@STRENGTH:!SSLv3"
99 #define DEFAULT_CIPHERS "SECURE128:-VERS-SSL3.0"
104 static void Handle_SSL PARAMS((const char *File, int Line, char *Var, char *Ark));
106 struct SSLOptions Conf_SSLOptions;
109 * Initialize SSL configuration.
114 free(Conf_SSLOptions.KeyFile);
115 Conf_SSLOptions.KeyFile = NULL;
117 free(Conf_SSLOptions.CertFile);
118 Conf_SSLOptions.CertFile = NULL;
120 free(Conf_SSLOptions.CAFile);
121 Conf_SSLOptions.CAFile = NULL;
123 free(Conf_SSLOptions.CRLFile);
124 Conf_SSLOptions.CRLFile = NULL;
126 free(Conf_SSLOptions.DHFile);
127 Conf_SSLOptions.DHFile = NULL;
128 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
130 array_free(&Conf_SSLOptions.ListenPorts);
132 free(Conf_SSLOptions.CipherList);
133 Conf_SSLOptions.CipherList = NULL;
137 * Check if the current configuration uses/requires SSL.
139 * @returns true if SSL is used and should be initialized.
146 /* SSL listen ports configured? */
147 if (array_bytes(&Conf_SSLOptions.ListenPorts))
150 for (i = 0; i < MAX_SERVERS; i++) {
151 if (Conf_Server[i].port > 0
152 && Conf_Server[i].SSLConnect)
159 * Make sure that a configured file is readable.
161 * Currently, this function is only used for SSL-related options ...
163 * @param Var Configuration variable
164 * @param Filename Configured filename
167 CheckFileReadable(const char *Var, const char *Filename)
174 fp = fopen(Filename, "r");
178 Config_Error(LOG_ERR, "Can't read \"%s\" (\"%s\"): %s",
179 Filename, Var, strerror(errno));
186 * Duplicate string and warn on errors.
188 * @returns Pointer to string on success, NULL otherwise.
191 strdup_warn(const char *str)
193 char *ptr = strdup(str);
195 Config_Error(LOG_ERR,
196 "Could not allocate memory for string: %s", str);
201 * Output a comma separated list of ports (integer values).
208 len = array_length(a, sizeof(UINT16));
210 ports = (UINT16*) array_start(a);
211 printf("%u", (unsigned int) *ports);
214 printf(", %u", (unsigned int) *ports);
221 * Parse a comma separated string into an array of port numbers (integers).
224 ports_parse(array *a, const char *File, int Line, char *Arg)
232 ptr = strtok( Arg, "," );
236 if (port > 0 && port < 0xFFFF) {
237 port16 = (UINT16) port;
238 if (!array_catb(a, (char*)&port16, sizeof port16))
239 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
240 File, Line, port, strerror(errno));
242 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
246 ptr = strtok( NULL, "," );
251 * Initialize configuration module.
256 Read_Config(false, true);
257 Validate_Config(false, false);
261 * "Rehash" (reload) server configuration.
263 * @returns true if configuration has been re-read, false on errors.
268 if (!Read_Config(false, false))
270 Validate_Config(false, true);
272 /* Update CLIENT structure of local server */
273 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
278 * Output a boolean value as "yes/no" string.
281 yesno_to_str(int boolean_value)
289 * Free all IRC operator configuration structures.
294 struct Conf_Oper *op;
297 len = array_length(&Conf_Opers, sizeof(*op));
298 op = array_start(&Conf_Opers);
303 array_free(&Conf_Opers);
307 * Output all IRC operator configuration structures.
312 struct Conf_Oper *op;
315 count = array_length(&Conf_Opers, sizeof(*op));
316 op = array_start(&Conf_Opers);
317 for (i = 0; i < count; i++, op++) {
322 printf(" Name = %s\n", op->name);
323 printf(" Password = %s\n", op->pwd);
324 printf(" Mask = %s\n\n", op->mask ? op->mask : "");
329 * Read configuration, validate and output it.
331 * This function waits for a keypress of the user when stdin/stdout are valid
332 * tty's ("you can read our nice message and we can read in your keypress").
334 * @return 0 on success, 1 on failure(s); therefore the result code can
335 * directly be used by exit() when running "ngircd --configtest".
344 size_t predef_channel_count;
345 struct Conf_Channel *predef_chan;
349 if (!Read_Config(true, true))
352 config_valid = Validate_Config(true, false);
355 if(isatty(fileno(stdin)) && isatty(fileno(stdout))) {
356 puts("OK, press enter to see a dump of your server configuration ...");
359 puts("Ok, dump of your server configuration follows:\n");
362 printf(" Name = %s\n", Conf_ServerName);
363 printf(" AdminInfo1 = %s\n", Conf_ServerAdmin1);
364 printf(" AdminInfo2 = %s\n", Conf_ServerAdmin2);
365 printf(" AdminEMail = %s\n", Conf_ServerAdminMail);
366 printf(" HelpFile = %s\n", Conf_HelpFile);
367 printf(" Info = %s\n", Conf_ServerInfo);
368 printf(" Listen = %s\n", Conf_ListenAddress);
369 if (Using_MotdFile) {
370 printf(" MotdFile = %s\n", Conf_MotdFile);
371 printf(" MotdPhrase =\n");
373 printf(" MotdFile = \n");
374 printf(" MotdPhrase = %s\n", array_bytes(&Conf_Motd)
375 ? (const char*) array_start(&Conf_Motd) : "");
377 printf(" Network = %s\n", Conf_Network);
379 printf(" Password = %s\n", Conf_ServerPwd);
380 printf(" PidFile = %s\n", Conf_PidFile);
382 ports_puts(&Conf_ListenPorts);
383 grp = getgrgid(Conf_GID);
385 printf(" ServerGID = %s\n", grp->gr_name);
387 printf(" ServerGID = %ld\n", (long)Conf_GID);
388 pwd = getpwuid(Conf_UID);
390 printf(" ServerUID = %s\n", pwd->pw_name);
392 printf(" ServerUID = %ld\n", (long)Conf_UID);
396 printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
397 printf(" IdleTimeout = %d\n", Conf_IdleTimeout);
398 printf(" MaxConnections = %d\n", Conf_MaxConnections);
399 printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
400 printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
401 printf(" MaxNickLength = %u\n", Conf_MaxNickLength - 1);
402 printf(" MaxPenaltyTime = %ld\n", (long)Conf_MaxPenaltyTime);
403 printf(" MaxListSize = %d\n", Conf_MaxListSize);
404 printf(" PingTimeout = %d\n", Conf_PingTimeout);
405 printf(" PongTimeout = %d\n", Conf_PongTimeout);
409 printf(" AllowedChannelTypes = %s\n", Conf_AllowedChannelTypes);
410 printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
411 printf(" ChrootDir = %s\n", Conf_Chroot);
412 printf(" CloakHost = %s\n", Conf_CloakHost);
413 printf(" CloakHostModeX = %s\n", Conf_CloakHostModeX);
414 printf(" CloakHostSalt = %s\n", Conf_CloakHostSalt);
415 printf(" CloakUserToNick = %s\n", yesno_to_str(Conf_CloakUserToNick));
417 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
418 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
420 printf(" DefaultUserModes = %s\n", Conf_DefaultUserModes);
421 printf(" DNS = %s\n", yesno_to_str(Conf_DNS));
423 printf(" Ident = %s\n", yesno_to_str(Conf_Ident));
425 printf(" IncludeDir = %s\n", Conf_IncludeDir);
426 printf(" MorePrivacy = %s\n", yesno_to_str(Conf_MorePrivacy));
427 printf(" NoticeBeforeRegistration = %s\n", yesno_to_str(Conf_NoticeBeforeRegistration));
428 printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
429 printf(" OperChanPAutoOp = %s\n", yesno_to_str(Conf_OperChanPAutoOp));
430 printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
432 printf(" PAM = %s\n", yesno_to_str(Conf_PAM));
433 printf(" PAMIsOptional = %s\n", yesno_to_str(Conf_PAMIsOptional));
434 printf(" PAMServiceName = %s\n", Conf_PAMServiceName);
437 printf(" RequireAuthPing = %s\n", yesno_to_str(Conf_AuthPing));
439 printf(" ScrubCTCP = %s\n", yesno_to_str(Conf_ScrubCTCP));
441 printf(" SyslogFacility = %s\n",
442 ngt_SyslogFacilityName(Conf_SyslogFacility));
444 printf(" WebircPassword = %s\n", Conf_WebircPwd);
449 printf(" CAFile = %s\n", Conf_SSLOptions.CAFile
450 ? Conf_SSLOptions.CAFile : "");
451 printf(" CertFile = %s\n", Conf_SSLOptions.CertFile
452 ? Conf_SSLOptions.CertFile : "");
453 printf(" CipherList = %s\n", Conf_SSLOptions.CipherList ?
454 Conf_SSLOptions.CipherList : DEFAULT_CIPHERS);
455 printf(" CRLFile = %s\n", Conf_SSLOptions.CRLFile
456 ? Conf_SSLOptions.CRLFile : "");
457 printf(" DHFile = %s\n", Conf_SSLOptions.DHFile
458 ? Conf_SSLOptions.DHFile : "");
459 printf(" KeyFile = %s\n", Conf_SSLOptions.KeyFile
460 ? Conf_SSLOptions.KeyFile : "");
461 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
462 puts(" KeyFilePassword = <secret>");
464 puts(" KeyFilePassword = ");
465 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
467 ports_puts(&Conf_SSLOptions.ListenPorts);
473 for( i = 0; i < MAX_SERVERS; i++ ) {
474 if( ! Conf_Server[i].name[0] ) continue;
476 /* Valid "Server" section */
478 printf( " Name = %s\n", Conf_Server[i].name );
479 printf( " Host = %s\n", Conf_Server[i].host );
480 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
482 printf(" SSLConnect = %s\n",
483 yesno_to_str(Conf_Server[i].SSLConnect));
484 printf(" SSLVerify = %s\n",
485 yesno_to_str(Conf_Server[i].SSLVerify));
487 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
488 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
489 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
490 printf( " Group = %d\n", Conf_Server[i].group );
491 printf( " Passive = %s\n\n", yesno_to_str(Conf_Server[i].flags & CONF_SFLAG_DISABLED));
494 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
495 predef_chan = array_start(&Conf_Channels);
497 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
498 if (!predef_chan->name[0])
501 /* Valid "Channel" section */
503 printf(" Name = %s\n", predef_chan->name);
504 for(j = 0; j < predef_chan->modes_num; j++)
505 printf(" Modes = %s\n", predef_chan->modes[j]);
506 printf(" Key = %s\n", predef_chan->key);
507 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
508 printf(" Topic = %s\n", predef_chan->topic);
509 printf(" Autojoin = %s\n", yesno_to_str(predef_chan->autojoin));
510 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
513 return (config_valid ? 0 : 1);
517 * Remove connection information from configured server.
519 * If the server is set as "once", delete it from our configuration;
520 * otherwise set the time for the next connection attempt.
522 * Non-server connections will be silently ignored.
525 Conf_UnsetServer( CONN_ID Idx )
530 /* Check all our configured servers */
531 for( i = 0; i < MAX_SERVERS; i++ ) {
532 if( Conf_Server[i].conn_id != Idx ) continue;
534 /* Gotcha! Mark server configuration as "unused": */
535 Conf_Server[i].conn_id = NONE;
537 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
538 /* Delete configuration here */
539 Init_Server_Struct( &Conf_Server[i] );
541 /* Set time for next connect attempt */
543 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
544 /* The connection has been "long", so we don't
545 * require the next attempt to be delayed. */
546 Conf_Server[i].lasttry =
547 t - Conf_ConnectRetry + RECONNECT_DELAY;
549 /* "Short" connection, enforce "ConnectRetry"
550 * but randomize it a little bit: 15 seconds. */
551 Conf_Server[i].lasttry =
552 #ifdef HAVE_ARC4RANDOM
553 t + (arc4random() % 15);
555 t + rand() / (RAND_MAX / 15);
563 * Set connection information for specified configured server.
566 Conf_SetServer( int ConfServer, CONN_ID Idx )
568 assert( ConfServer > NONE );
569 assert( Idx > NONE );
571 if (Conf_Server[ConfServer].conn_id > NONE &&
572 Conf_Server[ConfServer].conn_id != Idx) {
574 "Connection %d: Server configuration of \"%s\" already in use by connection %d!",
575 Idx, Conf_Server[ConfServer].name,
576 Conf_Server[ConfServer].conn_id);
577 Conn_Close(Idx, NULL, "Server configuration already in use", true);
580 Conf_Server[ConfServer].conn_id = Idx;
585 * Get index of server in configuration structure.
588 Conf_GetServer( CONN_ID Idx )
592 assert( Idx > NONE );
594 for( i = 0; i < MAX_SERVERS; i++ ) {
595 if( Conf_Server[i].conn_id == Idx ) return i;
601 * Enable a server by name and adjust its port number.
603 * @returns true if a server has been enabled and now has a valid port
604 * number and host name for outgoing connections.
607 Conf_EnableServer( const char *Name, UINT16 Port )
611 assert( Name != NULL );
612 for( i = 0; i < MAX_SERVERS; i++ ) {
613 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
614 /* Gotcha! Set port and enable server: */
615 Conf_Server[i].port = Port;
616 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
617 return (Conf_Server[i].port && Conf_Server[i].host[0]);
624 * Enable a server by name.
626 * The server is only usable as outgoing server, if it has set a valid port
627 * number for outgoing connections!
628 * If not, you have to use Conf_EnableServer() function to make it available.
630 * @returns true if a server has been enabled; false otherwise.
633 Conf_EnablePassiveServer(const char *Name)
637 assert( Name != NULL );
638 for (i = 0; i < MAX_SERVERS; i++) {
639 if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
640 && (Conf_Server[i].port > 0)) {
641 /* BINGO! Enable server */
642 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
643 Conf_Server[i].lasttry = 0;
651 * Disable a server by name.
652 * An already established connection will be disconnected.
654 * @returns true if a server was found and has been disabled.
657 Conf_DisableServer( const char *Name )
661 assert( Name != NULL );
662 for( i = 0; i < MAX_SERVERS; i++ ) {
663 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
664 /* Gotcha! Disable and disconnect server: */
665 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
666 if( Conf_Server[i].conn_id > NONE )
667 Conn_Close(Conf_Server[i].conn_id, NULL,
668 "Server link terminated on operator request",
677 * Add a new remote server to our configuration.
679 * @param Name Name of the new server.
680 * @param Port Port number to connect to or 0 for incoming connections.
681 * @param Host Host name to connect to.
682 * @param MyPwd Password that will be sent to the peer.
683 * @param PeerPwd Password that must be received from the peer.
684 * @returns true if the new server has been added; false otherwise.
687 Conf_AddServer(const char *Name, UINT16 Port, const char *Host,
688 const char *MyPwd, const char *PeerPwd)
692 assert( Name != NULL );
693 assert( Host != NULL );
694 assert( MyPwd != NULL );
695 assert( PeerPwd != NULL );
697 /* Search unused item in server configuration structure */
698 for( i = 0; i < MAX_SERVERS; i++ ) {
699 /* Is this item used? */
700 if( ! Conf_Server[i].name[0] ) break;
702 if( i >= MAX_SERVERS ) return false;
704 Init_Server_Struct( &Conf_Server[i] );
705 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
706 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
707 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
708 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
709 Conf_Server[i].port = Port;
710 Conf_Server[i].flags = CONF_SFLAG_ONCE;
716 * Check if the given nickname is reserved for services on a particular server.
718 * @param ConfServer The server index to check.
719 * @param Nick The nickname to check.
720 * @returns true if the given nickname belongs to an "IRC service".
723 Conf_NickIsService(int ConfServer, const char *Nick)
725 assert (ConfServer >= 0);
726 assert (ConfServer < MAX_SERVERS);
728 return MatchCaseInsensitiveList(Conf_Server[ConfServer].svs_mask,
733 * Check if the given nickname is blocked for "normal client" use.
735 * @param Nick The nickname to check.
736 * @returns true if the given nickname belongs to an "IRC service".
739 Conf_NickIsBlocked(const char *Nick)
743 for(i = 0; i < MAX_SERVERS; i++) {
744 if (!Conf_Server[i].name[0])
746 if (Conf_NickIsService(i, Nick))
753 * Initialize configuration settings with their default values.
756 Set_Defaults(bool InitServers)
759 char random[RANDOM_SALT_LEN + 1];
762 strcpy(Conf_ServerName, "");
763 strcpy(Conf_ServerAdmin1, "");
764 strcpy(Conf_ServerAdmin2, "");
765 strcpy(Conf_ServerAdminMail, "");
766 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
767 PACKAGE_NAME, PACKAGE_VERSION);
768 strcpy(Conf_Network, "");
769 free(Conf_ListenAddress);
770 Conf_ListenAddress = NULL;
771 array_free(&Conf_ListenPorts);
772 array_free(&Conf_Motd);
773 array_free(&Conf_Helptext);
774 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
775 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
776 strlcpy(Conf_HelpFile, DOCDIR, sizeof(Conf_HelpFile));
777 strlcat(Conf_HelpFile, HELP_FILE, sizeof(Conf_HelpFile));
778 strcpy(Conf_ServerPwd, "");
779 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
780 Conf_UID = Conf_GID = 0;
783 Conf_ConnectRetry = 60;
784 Conf_IdleTimeout = 0;
785 Conf_MaxConnections = 0;
786 Conf_MaxConnectionsIP = 5;
788 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
789 Conf_MaxPenaltyTime = -1;
790 Conf_MaxListSize = 100;
791 Conf_PingTimeout = 120;
792 Conf_PongTimeout = 20;
795 strlcpy(Conf_AllowedChannelTypes, CHANTYPES,
796 sizeof(Conf_AllowedChannelTypes));
797 Conf_AllowRemoteOper = false;
799 Conf_AuthPing = false;
801 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
802 strcpy(Conf_CloakHost, "");
803 strcpy(Conf_CloakHostModeX, "");
804 strlcpy(Conf_CloakHostSalt, ngt_RandomStr(random, RANDOM_SALT_LEN),
805 sizeof(Conf_CloakHostSalt));
806 Conf_CloakUserToNick = false;
807 Conf_ConnectIPv4 = true;
809 Conf_ConnectIPv6 = true;
811 Conf_ConnectIPv6 = false;
813 strcpy(Conf_DefaultUserModes, "");
820 strcpy(Conf_IncludeDir, "");
821 Conf_MorePrivacy = false;
822 Conf_NoticeBeforeRegistration = false;
823 Conf_OperCanMode = false;
824 Conf_OperChanPAutoOp = true;
825 Conf_OperServerMode = false;
831 Conf_PAMIsOptional = false;
832 strcpy(Conf_PAMServiceName, "ngircd");
833 Conf_ScrubCTCP = false;
836 Conf_SyslogFacility = LOG_LOCAL5;
838 Conf_SyslogFacility = 0;
842 /* Initialize server configuration structures */
844 for (i = 0; i < MAX_SERVERS;
845 Init_Server_Struct(&Conf_Server[i++]));
850 * Get number of configured listening ports.
852 * @returns The number of ports (IPv4+IPv6) on which the server should listen.
857 size_t cnt = array_bytes(&Conf_ListenPorts);
859 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
865 * Read contents of a text file into an array.
867 * This function is used to read the MOTD and help text file, for example.
869 * @param Filename Name of the file to read.
870 * @return true, when the file has been read in.
873 Read_TextFile(const char *Filename, const char *Name, array *Destination)
875 char line[COMMAND_LEN];
879 if (*Filename == '\0')
882 fp = fopen(Filename, "r");
884 Config_Error(LOG_ERR, "Can't read %s file \"%s\": %s",
885 Name, Filename, strerror(errno));
889 array_free(Destination);
890 while (fgets(line, (int)sizeof line, fp)) {
891 ngt_TrimLastChr(line, '\n');
893 /* add text including \0 */
894 if (!array_catb(Destination, line, strlen(line) + 1)) {
895 Log(LOG_ERR, "Cannot read/add \"%s\", line %d: %s",
896 Filename, line_no, strerror(errno));
906 * Read ngIRCd configuration file.
908 * Please note that this function uses exit(1) on fatal errors and therefore
909 * can result in ngIRCd terminating!
911 * @param IsStarting Flag indicating if ngIRCd is starting or not.
912 * @returns true when the configuration file has been read
913 * successfully; false otherwise.
916 Read_Config(bool TestOnly, bool IsStarting)
918 const UINT16 defaultport = 6667;
919 char *ptr, file[FNAME_LEN];
920 struct dirent *entry;
925 if (!NGIRCd_ConfFile[0]) {
926 /* No configuration file name explicitly given on the command
927 * line, use defaults but ignore errors when this file can't be
929 strlcpy(file, SYSCONFDIR, sizeof(file));
930 strlcat(file, CONFIG_FILE, sizeof(file));
933 ptr = NGIRCd_ConfFile;
935 Config_Error(LOG_INFO, "Using %s configuration file \"%s\" ...",
936 !NGIRCd_ConfFile[0] ? "default" : "specified", ptr);
938 /* Open configuration file */
939 fd = fopen(ptr, "r");
941 if (NGIRCd_ConfFile[0]) {
942 Config_Error(LOG_ALERT,
943 "Can't read specified configuration file \"%s\": %s",
944 ptr, strerror(errno));
946 Config_Error(LOG_ALERT,
947 "%s exiting due to fatal errors!",
952 Config_Error(LOG_WARNING,
953 "Can't read default configuration file \"%s\": %s - Ignored.",
954 ptr, strerror(errno));
958 Set_Defaults(IsStarting);
961 Config_Error(LOG_INFO,
962 "Reading configuration from \"%s\" ...", ptr);
964 /* Clean up server configuration structure: mark all already
965 * configured servers as "once" so that they are deleted
966 * after the next disconnect and delete all unused servers.
967 * And delete all servers which are "duplicates" of servers
968 * that are already marked as "once" (such servers have been
969 * created by the last rehash but are now useless). */
970 for( i = 0; i < MAX_SERVERS; i++ ) {
971 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
973 /* This structure is in use ... */
974 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
975 /* Check for duplicates */
976 for( n = 0; n < MAX_SERVERS; n++ ) {
977 if( n == i ) continue;
979 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
980 Init_Server_Struct( &Conf_Server[n] );
981 LogDebug("Deleted unused duplicate server %d (kept %d).", n, i);
985 /* Mark server as "once" */
986 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
987 LogDebug("Marked server %d as \"once\"", i);
992 /* Initialize variables */
993 Init_Server_Struct( &New_Server );
994 New_Server_Idx = NONE;
1000 Read_Config_File(ptr, fd);
1004 if (Conf_IncludeDir[0]) {
1005 /* Include directory was set in the main configuration file. So
1006 * use it and show errors. */
1007 dh = opendir(Conf_IncludeDir);
1009 Config_Error(LOG_ALERT,
1010 "Can't open include directory \"%s\": %s",
1011 Conf_IncludeDir, strerror(errno));
1012 } else if (!NGIRCd_ConfFile[0]) {
1013 /* No include dir set in the configuration file used (if any)
1014 * but no config file explicitly specified either: so use the
1015 * default include path here as well! */
1016 strlcpy(Conf_IncludeDir, SYSCONFDIR, sizeof(Conf_IncludeDir));
1017 strlcat(Conf_IncludeDir, CONFIG_DIR, sizeof(Conf_IncludeDir));
1018 dh = opendir(Conf_IncludeDir);
1021 /* Include further configuration files, if IncludeDir is available */
1023 while ((entry = readdir(dh)) != NULL) {
1024 ptr = strrchr(entry->d_name, '.');
1025 if (!ptr || strcasecmp(ptr, ".conf") != 0)
1027 snprintf(file, sizeof(file), "%s/%s",
1028 Conf_IncludeDir, entry->d_name);
1030 Config_Error(LOG_INFO,
1031 "Reading configuration from \"%s\" ...",
1033 fd = fopen(file, "r");
1035 Read_Config_File(file, fd);
1038 Config_Error(LOG_ALERT,
1039 "Can't read configuration \"%s\": %s",
1040 file, strerror(errno));
1045 /* Check if there is still a server to add */
1046 if( New_Server.name[0] ) {
1047 /* Copy data to "real" server structure */
1048 assert( New_Server_Idx > NONE );
1049 Conf_Server[New_Server_Idx] = New_Server;
1052 /* not a single listening port? Add default. */
1053 if (no_listenports() &&
1054 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
1056 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
1057 (unsigned int) defaultport, strerror(errno));
1062 if (!Conf_ListenAddress)
1063 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
1065 if (!Conf_ListenAddress) {
1066 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
1070 /* No MOTD phrase configured? (re)try motd file. */
1071 if (array_bytes(&Conf_Motd) == 0) {
1072 if (Read_TextFile(Conf_MotdFile, "MOTD", &Conf_Motd))
1073 Using_MotdFile = true;
1076 /* Try to read ngIRCd help text file. */
1077 (void)Read_TextFile(Conf_HelpFile, "help text", &Conf_Helptext);
1078 if (!array_bytes(&Conf_Helptext))
1079 Config_Error(LOG_WARNING,
1080 "No help text available, HELP command will be of limited use.");
1083 /* Make sure that all SSL-related files are readable */
1084 CheckFileReadable("CertFile", Conf_SSLOptions.CertFile);
1085 CheckFileReadable("DHFile", Conf_SSLOptions.DHFile);
1086 CheckFileReadable("KeyFile", Conf_SSLOptions.KeyFile);
1088 /* Set the default ciphers if none were configured */
1089 if (!Conf_SSLOptions.CipherList)
1090 Conf_SSLOptions.CipherList = strdup_warn(DEFAULT_CIPHERS);
1097 * Read in and handle a configuration file.
1099 * @param File Name of the configuration file.
1100 * @param fd File descriptor already opened for reading.
1103 Read_Config_File(const char *File, FILE *fd)
1105 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
1109 /* Read configuration file */
1112 if (!fgets(str, sizeof(str), fd))
1117 /* Skip comments and empty lines */
1118 if (str[0] == ';' || str[0] == '#' || str[0] == '\0')
1121 if (strlen(str) >= sizeof(str) - 1) {
1122 Config_Error(LOG_WARNING, "%s, line %d too long!",
1127 /* Is this the beginning of a new section? */
1128 if ((str[0] == '[') && (str[strlen(str) - 1] == ']')) {
1129 strlcpy(section, str, sizeof(section));
1130 if (strcasecmp(section, "[GLOBAL]") == 0
1131 || strcasecmp(section, "[LIMITS]") == 0
1132 || strcasecmp(section, "[OPTIONS]") == 0
1134 || strcasecmp(section, "[SSL]") == 0
1139 if (strcasecmp(section, "[SERVER]") == 0) {
1140 /* Check if there is already a server to add */
1141 if (New_Server.name[0]) {
1142 /* Copy data to "real" server structure */
1143 assert(New_Server_Idx > NONE);
1144 Conf_Server[New_Server_Idx] =
1148 /* Re-init structure for new server */
1149 Init_Server_Struct(&New_Server);
1151 /* Search unused item in server configuration structure */
1152 for (i = 0; i < MAX_SERVERS; i++) {
1153 /* Is this item used? */
1154 if (!Conf_Server[i].name[0])
1157 if (i >= MAX_SERVERS) {
1158 /* Oops, no free item found! */
1159 Config_Error(LOG_ERR,
1160 "Too many servers configured.");
1161 New_Server_Idx = NONE;
1167 if (strcasecmp(section, "[CHANNEL]") == 0) {
1168 count = array_length(&Conf_Channels,
1173 sizeof(struct Conf_Channel), count)) {
1174 Config_Error(LOG_ERR,
1175 "Could not allocate memory for new operator (line %d)",
1181 if (strcasecmp(section, "[OPERATOR]") == 0) {
1182 count = array_length(&Conf_Opers,
1183 sizeof(struct Conf_Oper));
1184 if (!array_alloc(&Conf_Opers,
1185 sizeof(struct Conf_Oper),
1187 Config_Error(LOG_ERR,
1188 "Could not allocate memory for new channel (line &d)",
1194 Config_Error(LOG_ERR,
1195 "%s, line %d: Unknown section \"%s\"!",
1196 File, line, section);
1199 if (section[0] == 0x1)
1202 /* Split line into variable name and parameters */
1203 ptr = strchr(str, '=');
1205 Config_Error(LOG_ERR, "%s, line %d: Syntax error!",
1215 if (strcasecmp(section, "[GLOBAL]") == 0)
1216 Handle_GLOBAL(File, line, var, arg);
1217 else if (strcasecmp(section, "[LIMITS]") == 0)
1218 Handle_LIMITS(File, line, var, arg);
1219 else if (strcasecmp(section, "[OPTIONS]") == 0)
1220 Handle_OPTIONS(File, line, var, arg);
1222 else if (strcasecmp(section, "[SSL]") == 0)
1223 Handle_SSL(File, line, var, arg);
1225 else if (strcasecmp(section, "[OPERATOR]") == 0)
1226 Handle_OPERATOR(File, line, var, arg);
1227 else if (strcasecmp(section, "[SERVER]") == 0)
1228 Handle_SERVER(File, line, var, arg);
1229 else if (strcasecmp(section, "[CHANNEL]") == 0)
1230 Handle_CHANNEL(File, line, var, arg);
1232 Config_Error(LOG_ERR,
1233 "%s, line %d: Variable \"%s\" outside section!",
1239 * Check whether a string argument is "true" or "false".
1241 * @param Arg Input string.
1242 * @returns true if the input string has been parsed as "yes", "true"
1243 * (case insensitive) or a non-zero integer value.
1246 Check_ArgIsTrue(const char *Arg)
1248 if (strcasecmp(Arg, "yes") == 0)
1250 if (strcasecmp(Arg, "true") == 0)
1259 * Handle setting of "MaxNickLength".
1261 * @param Line Line number in configuration file.
1262 * @raram Arg Input string.
1263 * @returns New configured maximum nickname length.
1266 Handle_MaxNickLength(const char *File, int Line, const char *Arg)
1270 new = (unsigned) atoi(Arg) + 1;
1271 if (new > CLIENT_NICK_LEN) {
1272 Config_Error(LOG_WARNING,
1273 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
1274 File, Line, CLIENT_NICK_LEN - 1);
1275 return CLIENT_NICK_LEN;
1278 Config_Error(LOG_WARNING,
1279 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
1287 * Output a warning messages if IDENT is configured but not compiled in.
1290 WarnIdent(const char UNUSED *File, int UNUSED Line)
1294 /* user has enabled ident lookups explicitly, but ... */
1295 Config_Error(LOG_WARNING,
1296 "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
1303 * Output a warning messages if IPv6 is configured but not compiled in.
1306 WarnIPv6(const char UNUSED *File, int UNUSED Line)
1309 if (Conf_ConnectIPv6) {
1310 /* user has enabled IPv6 explicitly, but ... */
1311 Config_Error(LOG_WARNING,
1312 "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
1319 * Output a warning messages if PAM is configured but not compiled in.
1322 WarnPAM(const char UNUSED *File, int UNUSED Line)
1326 Config_Error(LOG_WARNING,
1327 "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
1335 * Handle variable in [Global] configuration section.
1337 * @param Line Line number in configuration file.
1338 * @param Var Variable name.
1339 * @param Arg Variable argument.
1342 Handle_GLOBAL(const char *File, int Line, char *Var, char *Arg )
1349 assert(File != NULL);
1351 assert(Var != NULL);
1352 assert(Arg != NULL);
1354 if (strcasecmp(Var, "Name") == 0) {
1355 len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
1356 if (len >= sizeof(Conf_ServerName))
1357 Config_Error_TooLong(File, Line, Var);
1360 if (strcasecmp(Var, "AdminInfo1") == 0) {
1361 len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
1362 if (len >= sizeof(Conf_ServerAdmin1))
1363 Config_Error_TooLong(File, Line, Var);
1366 if (strcasecmp(Var, "AdminInfo2") == 0) {
1367 len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
1368 if (len >= sizeof(Conf_ServerAdmin2))
1369 Config_Error_TooLong(File, Line, Var);
1372 if (strcasecmp(Var, "AdminEMail") == 0) {
1373 len = strlcpy(Conf_ServerAdminMail, Arg,
1374 sizeof(Conf_ServerAdminMail));
1375 if (len >= sizeof(Conf_ServerAdminMail))
1376 Config_Error_TooLong(File, Line, Var);
1379 if (strcasecmp(Var, "Info") == 0) {
1380 len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
1381 if (len >= sizeof(Conf_ServerInfo))
1382 Config_Error_TooLong(File, Line, Var);
1385 if (strcasecmp(Var, "HelpFile") == 0) {
1386 len = strlcpy(Conf_HelpFile, Arg, sizeof(Conf_HelpFile));
1387 if (len >= sizeof(Conf_HelpFile))
1388 Config_Error_TooLong(File, Line, Var);
1391 if (strcasecmp(Var, "Listen") == 0) {
1392 if (Conf_ListenAddress) {
1393 Config_Error(LOG_ERR,
1394 "Multiple Listen= options, ignoring: %s",
1398 Conf_ListenAddress = strdup_warn(Arg);
1399 /* If allocation fails, we're in trouble: we cannot ignore the
1400 * error -- otherwise ngircd would listen on all interfaces. */
1401 if (!Conf_ListenAddress) {
1402 Config_Error(LOG_ALERT,
1403 "%s exiting due to fatal errors!",
1409 if (strcasecmp(Var, "MotdFile") == 0) {
1410 len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
1411 if (len >= sizeof(Conf_MotdFile))
1412 Config_Error_TooLong(File, Line, Var);
1415 if (strcasecmp(Var, "MotdPhrase") == 0) {
1420 Config_Error_TooLong(File, Line, Var);
1423 if (!array_copyb(&Conf_Motd, Arg, len + 1))
1424 Config_Error(LOG_WARNING,
1425 "%s, line %d: Could not append MotdPhrase: %s",
1426 File, Line, strerror(errno));
1427 Using_MotdFile = false;
1430 if (strcasecmp(Var, "Network") == 0) {
1431 len = strlcpy(Conf_Network, Arg, sizeof(Conf_Network));
1432 if (len >= sizeof(Conf_Network))
1433 Config_Error_TooLong(File, Line, Var);
1434 ptr = strchr(Conf_Network, ' ');
1436 Config_Error(LOG_WARNING,
1437 "%s, line %d: \"Network\" can't contain spaces!",
1443 if(strcasecmp(Var, "Password") == 0) {
1444 len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
1445 if (len >= sizeof(Conf_ServerPwd))
1446 Config_Error_TooLong(File, Line, Var);
1449 if (strcasecmp(Var, "PidFile") == 0) {
1450 len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
1451 if (len >= sizeof(Conf_PidFile))
1452 Config_Error_TooLong(File, Line, Var);
1455 if (strcasecmp(Var, "Ports") == 0) {
1456 ports_parse(&Conf_ListenPorts, File, Line, Arg);
1459 if (strcasecmp(Var, "ServerGID") == 0) {
1460 grp = getgrnam(Arg);
1462 Conf_GID = grp->gr_gid;
1464 Conf_GID = (unsigned int)atoi(Arg);
1465 if (!Conf_GID && strcmp(Arg, "0"))
1466 Config_Error(LOG_WARNING,
1467 "%s, line %d: Value of \"%s\" is not a valid group name or ID!",
1472 if (strcasecmp(Var, "ServerUID") == 0) {
1473 pwd = getpwnam(Arg);
1475 Conf_UID = pwd->pw_uid;
1477 Conf_UID = (unsigned int)atoi(Arg);
1478 if (!Conf_UID && strcmp(Arg, "0"))
1479 Config_Error(LOG_WARNING,
1480 "%s, line %d: Value of \"%s\" is not a valid user name or ID!",
1486 Config_Error_Section(File, Line, Var, "Global");
1490 * Handle variable in [Limits] configuration section.
1492 * @param Line Line number in configuration file.
1493 * @param Var Variable name.
1494 * @param Arg Variable argument.
1497 Handle_LIMITS(const char *File, int Line, char *Var, char *Arg)
1499 assert(File != NULL);
1501 assert(Var != NULL);
1502 assert(Arg != NULL);
1504 if (strcasecmp(Var, "ConnectRetry") == 0) {
1505 Conf_ConnectRetry = atoi(Arg);
1506 if (Conf_ConnectRetry < 5) {
1507 Config_Error(LOG_WARNING,
1508 "%s, line %d: Value of \"ConnectRetry\" too low!",
1510 Conf_ConnectRetry = 5;
1514 if (strcasecmp(Var, "IdleTimeout") == 0) {
1515 Conf_IdleTimeout = atoi(Arg);
1516 if (!Conf_IdleTimeout && strcmp(Arg, "0"))
1517 Config_Error_NaN(File, Line, Var);
1520 if (strcasecmp(Var, "MaxConnections") == 0) {
1521 Conf_MaxConnections = atoi(Arg);
1522 if (!Conf_MaxConnections && strcmp(Arg, "0"))
1523 Config_Error_NaN(File, Line, Var);
1526 if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
1527 Conf_MaxConnectionsIP = atoi(Arg);
1528 if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
1529 Config_Error_NaN(File, Line, Var);
1532 if (strcasecmp(Var, "MaxJoins") == 0) {
1533 Conf_MaxJoins = atoi(Arg);
1534 if (!Conf_MaxJoins && strcmp(Arg, "0"))
1535 Config_Error_NaN(File, Line, Var);
1538 if (strcasecmp(Var, "MaxNickLength") == 0) {
1539 Conf_MaxNickLength = Handle_MaxNickLength(File, Line, Arg);
1542 if (strcasecmp(Var, "MaxListSize") == 0) {
1543 Conf_MaxListSize = atoi(Arg);
1544 if (!Conf_MaxListSize && strcmp(Arg, "0"))
1545 Config_Error_NaN(File, Line, Var);
1548 if (strcasecmp(Var, "MaxPenaltyTime") == 0) {
1549 Conf_MaxPenaltyTime = atol(Arg);
1550 if (Conf_MaxPenaltyTime < -1)
1551 Conf_MaxPenaltyTime = -1; /* "unlimited" */
1554 if (strcasecmp(Var, "PingTimeout") == 0) {
1555 Conf_PingTimeout = atoi(Arg);
1556 if (Conf_PingTimeout < 5) {
1557 Config_Error(LOG_WARNING,
1558 "%s, line %d: Value of \"PingTimeout\" too low!",
1560 Conf_PingTimeout = 5;
1564 if (strcasecmp(Var, "PongTimeout") == 0) {
1565 Conf_PongTimeout = atoi(Arg);
1566 if (Conf_PongTimeout < 5) {
1567 Config_Error(LOG_WARNING,
1568 "%s, line %d: Value of \"PongTimeout\" too low!",
1570 Conf_PongTimeout = 5;
1575 Config_Error_Section(File, Line, Var, "Limits");
1579 * Handle variable in [Options] configuration section.
1581 * @param Line Line number in configuration file.
1582 * @param Var Variable name.
1583 * @param Arg Variable argument.
1586 Handle_OPTIONS(const char *File, int Line, char *Var, char *Arg)
1591 assert(File != NULL);
1593 assert(Var != NULL);
1594 assert(Arg != NULL);
1596 if (strcasecmp(Var, "AllowedChannelTypes") == 0) {
1598 Conf_AllowedChannelTypes[0] = '\0';
1600 if (strchr(Conf_AllowedChannelTypes, *p)) {
1601 /* Prefix is already included; ignore it */
1606 if (strchr(CHANTYPES, *p)) {
1607 len = strlen(Conf_AllowedChannelTypes) + 1;
1608 assert(len < sizeof(Conf_AllowedChannelTypes));
1609 Conf_AllowedChannelTypes[len - 1] = *p;
1610 Conf_AllowedChannelTypes[len] = '\0';
1612 Config_Error(LOG_WARNING,
1613 "%s, line %d: Unknown channel prefix \"%c\" in \"AllowedChannelTypes\"!",
1620 if (strcasecmp(Var, "AllowRemoteOper") == 0) {
1621 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1624 if (strcasecmp(Var, "ChrootDir") == 0) {
1625 len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
1626 if (len >= sizeof(Conf_Chroot))
1627 Config_Error_TooLong(File, Line, Var);
1630 if (strcasecmp(Var, "CloakHost") == 0) {
1631 len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
1632 if (len >= sizeof(Conf_CloakHost))
1633 Config_Error_TooLong(File, Line, Var);
1636 if (strcasecmp(Var, "CloakHostModeX") == 0) {
1637 len = strlcpy(Conf_CloakHostModeX, Arg, sizeof(Conf_CloakHostModeX));
1638 if (len >= sizeof(Conf_CloakHostModeX))
1639 Config_Error_TooLong(File, Line, Var);
1642 if (strcasecmp(Var, "CloakHostSalt") == 0) {
1643 len = strlcpy(Conf_CloakHostSalt, Arg, sizeof(Conf_CloakHostSalt));
1644 if (len >= sizeof(Conf_CloakHostSalt))
1645 Config_Error_TooLong(File, Line, Var);
1648 if (strcasecmp(Var, "CloakUserToNick") == 0) {
1649 Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
1652 if (strcasecmp(Var, "ConnectIPv6") == 0) {
1653 Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
1654 WarnIPv6(File, Line);
1657 if (strcasecmp(Var, "ConnectIPv4") == 0) {
1658 Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
1661 if (strcasecmp(Var, "DefaultUserModes") == 0) {
1663 Conf_DefaultUserModes[0] = '\0';
1665 if (strchr(Conf_DefaultUserModes, *p)) {
1666 /* Mode is already included; ignore it */
1671 if (strchr(USERMODES, *p)) {
1672 len = strlen(Conf_DefaultUserModes) + 1;
1673 assert(len < sizeof(Conf_DefaultUserModes));
1674 Conf_DefaultUserModes[len - 1] = *p;
1675 Conf_DefaultUserModes[len] = '\0';
1677 Config_Error(LOG_WARNING,
1678 "%s, line %d: Unknown user mode \"%c\" in \"DefaultUserModes\"!",
1685 if (strcasecmp(Var, "DNS") == 0) {
1686 Conf_DNS = Check_ArgIsTrue(Arg);
1689 if (strcasecmp(Var, "Ident") == 0) {
1690 Conf_Ident = Check_ArgIsTrue(Arg);
1691 WarnIdent(File, Line);
1694 if (strcasecmp(Var, "IncludeDir") == 0) {
1695 if (Conf_IncludeDir[0]) {
1696 Config_Error(LOG_ERR,
1697 "%s, line %d: Can't overwrite value of \"IncludeDir\" variable!",
1701 len = strlcpy(Conf_IncludeDir, Arg, sizeof(Conf_IncludeDir));
1702 if (len >= sizeof(Conf_IncludeDir))
1703 Config_Error_TooLong(File, Line, Var);
1706 if (strcasecmp(Var, "MorePrivacy") == 0) {
1707 Conf_MorePrivacy = Check_ArgIsTrue(Arg);
1710 if (strcasecmp(Var, "NoticeBeforeRegistration") == 0) {
1711 Conf_NoticeBeforeRegistration = Check_ArgIsTrue(Arg);
1714 if (strcasecmp(Var, "OperCanUseMode") == 0) {
1715 Conf_OperCanMode = Check_ArgIsTrue(Arg);
1718 if (strcasecmp(Var, "OperChanPAutoOp") == 0) {
1719 Conf_OperChanPAutoOp = Check_ArgIsTrue(Arg);
1722 if (strcasecmp(Var, "OperServerMode") == 0) {
1723 Conf_OperServerMode = Check_ArgIsTrue(Arg);
1726 if (strcasecmp(Var, "PAM") == 0) {
1727 Conf_PAM = Check_ArgIsTrue(Arg);
1728 WarnPAM(File, Line);
1731 if (strcasecmp(Var, "PAMIsOptional") == 0 ) {
1732 Conf_PAMIsOptional = Check_ArgIsTrue(Arg);
1735 if (strcasecmp(Var, "PAMServiceName") == 0) {
1736 len = strlcpy(Conf_PAMServiceName, Arg, sizeof(Conf_PAMServiceName));
1737 if (len >= sizeof(Conf_PAMServiceName))
1738 Config_Error_TooLong(File, Line, Var);
1742 if (strcasecmp(Var, "RequireAuthPing") == 0) {
1743 Conf_AuthPing = Check_ArgIsTrue(Arg);
1747 if (strcasecmp(Var, "ScrubCTCP") == 0) {
1748 Conf_ScrubCTCP = Check_ArgIsTrue(Arg);
1752 if (strcasecmp(Var, "SyslogFacility") == 0) {
1753 Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
1754 Conf_SyslogFacility);
1758 if (strcasecmp(Var, "WebircPassword") == 0) {
1759 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
1760 if (len >= sizeof(Conf_WebircPwd))
1761 Config_Error_TooLong(File, Line, Var);
1765 Config_Error_Section(File, Line, Var, "Options");
1771 * Handle variable in [SSL] configuration section.
1773 * @param Line Line number in configuration file.
1774 * @param Var Variable name.
1775 * @param Arg Variable argument.
1778 Handle_SSL(const char *File, int Line, char *Var, char *Arg)
1780 assert(File != NULL);
1782 assert(Var != NULL);
1783 assert(Arg != NULL);
1785 if (strcasecmp(Var, "CertFile") == 0) {
1786 assert(Conf_SSLOptions.CertFile == NULL);
1787 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1790 if (strcasecmp(Var, "DHFile") == 0) {
1791 assert(Conf_SSLOptions.DHFile == NULL);
1792 Conf_SSLOptions.DHFile = strdup_warn(Arg);
1795 if (strcasecmp(Var, "KeyFile") == 0) {
1796 assert(Conf_SSLOptions.KeyFile == NULL);
1797 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1800 if (strcasecmp(Var, "KeyFilePassword") == 0) {
1801 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1802 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1803 Config_Error(LOG_ERR,
1804 "%s, line %d (section \"SSL\"): Could not copy %s: %s!",
1805 File, Line, Var, strerror(errno));
1808 if (strcasecmp(Var, "Ports") == 0) {
1809 ports_parse(&Conf_SSLOptions.ListenPorts, File, Line, Arg);
1812 if (strcasecmp(Var, "CipherList") == 0) {
1813 assert(Conf_SSLOptions.CipherList == NULL);
1814 Conf_SSLOptions.CipherList = strdup_warn(Arg);
1817 if (strcasecmp(Var, "CAFile") == 0) {
1818 assert(Conf_SSLOptions.CAFile == NULL);
1819 Conf_SSLOptions.CAFile = strdup_warn(Arg);
1822 if (strcasecmp(Var, "CRLFile") == 0) {
1823 assert(Conf_SSLOptions.CRLFile == NULL);
1824 Conf_SSLOptions.CRLFile = strdup_warn(Arg);
1828 Config_Error_Section(File, Line, Var, "SSL");
1834 * Handle variable in [Operator] configuration section.
1836 * @param Line Line number in configuration file.
1837 * @param Var Variable name.
1838 * @param Arg Variable argument.
1841 Handle_OPERATOR(const char *File, int Line, char *Var, char *Arg )
1844 struct Conf_Oper *op;
1846 assert( File != NULL );
1848 assert( Var != NULL );
1849 assert( Arg != NULL );
1851 op = array_get(&Conf_Opers, sizeof(*op),
1852 array_length(&Conf_Opers, sizeof(*op)) - 1);
1856 if (strcasecmp(Var, "Name") == 0) {
1857 /* Name of IRC operator */
1858 len = strlcpy(op->name, Arg, sizeof(op->name));
1859 if (len >= sizeof(op->name))
1860 Config_Error_TooLong(File, Line, Var);
1863 if (strcasecmp(Var, "Password") == 0) {
1864 /* Password of IRC operator */
1865 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1866 if (len >= sizeof(op->pwd))
1867 Config_Error_TooLong(File, Line, Var);
1870 if (strcasecmp(Var, "Mask") == 0) {
1872 return; /* Hostname already configured */
1873 op->mask = strdup_warn( Arg );
1877 Config_Error_Section(File, Line, Var, "Operator");
1881 * Handle variable in [Server] configuration section.
1883 * @param Line Line number in configuration file.
1884 * @param Var Variable name.
1885 * @param Arg Variable argument.
1888 Handle_SERVER(const char *File, int Line, char *Var, char *Arg )
1893 assert( File != NULL );
1895 assert( Var != NULL );
1896 assert( Arg != NULL );
1898 /* Ignore server block if no space is left in server configuration structure */
1899 if( New_Server_Idx <= NONE ) return;
1901 if( strcasecmp( Var, "Host" ) == 0 ) {
1902 /* Hostname of the server */
1903 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1904 if (len >= sizeof( New_Server.host ))
1905 Config_Error_TooLong(File, Line, Var);
1908 if( strcasecmp( Var, "Name" ) == 0 ) {
1909 /* Name of the server ("Nick"/"ID") */
1910 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1911 if (len >= sizeof( New_Server.name ))
1912 Config_Error_TooLong(File, Line, Var);
1915 if (strcasecmp(Var, "Bind") == 0) {
1916 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1919 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1923 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1924 /* Password of this server which is sent to the peer */
1926 Config_Error(LOG_ERR,
1927 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1930 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1931 if (len >= sizeof( New_Server.pwd_in ))
1932 Config_Error_TooLong(File, Line, Var);
1935 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1936 /* Passwort of the peer which must be received */
1937 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1938 if (len >= sizeof( New_Server.pwd_out ))
1939 Config_Error_TooLong(File, Line, Var);
1942 if( strcasecmp( Var, "Port" ) == 0 ) {
1943 /* Port to which this server should connect */
1945 if (port >= 0 && port < 0xFFFF)
1946 New_Server.port = (UINT16)port;
1948 Config_Error(LOG_ERR,
1949 "%s, line %d (section \"Server\"): Illegal port number %ld!",
1954 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1955 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1958 if (strcasecmp(Var, "SSLVerify") == 0) {
1959 New_Server.SSLVerify = Check_ArgIsTrue(Arg);
1963 if( strcasecmp( Var, "Group" ) == 0 ) {
1965 New_Server.group = atoi( Arg );
1966 if (!New_Server.group && strcmp(Arg, "0"))
1967 Config_Error_NaN(File, Line, Var);
1970 if( strcasecmp( Var, "Passive" ) == 0 ) {
1971 if (Check_ArgIsTrue(Arg))
1972 New_Server.flags |= CONF_SFLAG_DISABLED;
1975 if (strcasecmp(Var, "ServiceMask") == 0) {
1976 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1977 sizeof(New_Server.svs_mask));
1978 if (len >= sizeof(New_Server.svs_mask))
1979 Config_Error_TooLong(File, Line, Var);
1983 Config_Error_Section(File, Line, Var, "Server");
1987 * Copy channel name into channel structure.
1989 * If the channel name is not valid because of a missing prefix ('#', '&'),
1990 * a default prefix of '#' will be added.
1992 * @param new_chan New already allocated channel structure.
1993 * @param name Name of the new channel.
1994 * @returns true on success, false otherwise.
1997 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1999 size_t size = sizeof(new_chan->name);
2000 char *dest = new_chan->name;
2002 if (!Channel_IsValidName(name)) {
2004 * maybe user forgot to add a '#'.
2005 * This is only here for user convenience.
2011 return size > strlcpy(dest, name, size);
2015 * Handle variable in [Channel] configuration section.
2017 * @param Line Line number in configuration file.
2018 * @param Var Variable name.
2019 * @param Arg Variable argument.
2022 Handle_CHANNEL(const char *File, int Line, char *Var, char *Arg)
2025 struct Conf_Channel *chan;
2027 assert( File != NULL );
2029 assert( Var != NULL );
2030 assert( Arg != NULL );
2032 chan = array_get(&Conf_Channels, sizeof(*chan),
2033 array_length(&Conf_Channels, sizeof(*chan)) - 1);
2037 if (strcasecmp(Var, "Name") == 0) {
2038 if (!Handle_Channelname(chan, Arg))
2039 Config_Error_TooLong(File, Line, Var);
2042 if (strcasecmp(Var, "Modes") == 0) {
2044 if(chan->modes_num >= sizeof(chan->modes)) {
2045 Config_Error(LOG_ERR, "Too many Modes, option ignored.");
2048 chan->modes[chan->modes_num++] = strndup(Arg, COMMAND_LEN);
2049 if(strlen(Arg) >= COMMAND_LEN)
2050 Config_Error_TooLong(File, Line, Var);
2053 if( strcasecmp( Var, "Topic" ) == 0 ) {
2055 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
2056 if (len >= sizeof(chan->topic))
2057 Config_Error_TooLong(File, Line, Var);
2060 if( strcasecmp( Var, "Autojoin" ) == 0 ) {
2061 /* Check autojoin */
2062 chan->autojoin = Check_ArgIsTrue(Arg);
2065 if( strcasecmp( Var, "Key" ) == 0 ) {
2066 /* Initial Channel Key (mode k) */
2067 len = strlcpy(chan->key, Arg, sizeof(chan->key));
2068 if (len >= sizeof(chan->key))
2069 Config_Error_TooLong(File, Line, Var);
2070 Config_Error(LOG_WARNING,
2071 "%s, line %d (section \"Channel\"): \"%s\" is deprecated here, use \"Modes = +k <key>\"!",
2075 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
2076 /* maximum user limit, mode l */
2077 chan->maxusers = (unsigned long) atol(Arg);
2078 if (!chan->maxusers && strcmp(Arg, "0"))
2079 Config_Error_NaN(File, Line, Var);
2080 Config_Error(LOG_WARNING,
2081 "%s, line %d (section \"Channel\"): \"%s\" is deprecated here, use \"Modes = +l <limit>\"!",
2085 if (strcasecmp(Var, "KeyFile") == 0) {
2087 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
2088 if (len >= sizeof(chan->keyfile))
2089 Config_Error_TooLong(File, Line, Var);
2093 Config_Error_Section(File, Line, Var, "Channel");
2097 * Validate server configuration.
2099 * Please note that this function uses exit(1) on fatal errors and therefore
2100 * can result in ngIRCd terminating!
2102 * @param Configtest true if the daemon has been called with "--configtest".
2103 * @param Rehash true if re-reading configuration on runtime.
2104 * @returns true if configuration is valid.
2107 Validate_Config(bool Configtest, bool Rehash)
2109 /* Validate configuration settings. */
2111 int i, servers, servers_once;
2113 bool config_valid = true;
2115 #ifdef HAVE_SETRLIMIT
2120 /* Emit a warning when the config file is not a full path name */
2121 if (NGIRCd_ConfFile[0] && NGIRCd_ConfFile[0] != '/') {
2122 Config_Error(LOG_WARNING,
2123 "Not specifying a full path name to \"%s\" can cause problems when rehashing the server!",
2127 if (!Conf_ServerName[0]) {
2128 /* No server name configured, try to get a sane name from the
2129 * host name. Note: the IRC server name MUST contain
2130 * at least one dot, so the "node name" is not sufficient! */
2131 gethostname(Conf_ServerName, sizeof(Conf_ServerName));
2133 /* Try to get a proper host name ... */
2134 h = gethostbyname(Conf_ServerName);
2136 strlcpy(Conf_ServerName, h->h_name,
2137 sizeof(Conf_ServerName));
2139 if (!strchr(Conf_ServerName, '.')) {
2140 /* (Still) No dot in the name! */
2141 strlcat(Conf_ServerName, ".host",
2142 sizeof(Conf_ServerName));
2144 Config_Error(LOG_WARNING,
2145 "No server name configured, using host name \"%s\".",
2149 /* Validate configured server name, see RFC 2812 section 2.3.1 */
2150 ptr = Conf_ServerName;
2152 if (*ptr >= 'a' && *ptr <= 'z') continue;
2153 if (*ptr >= 'A' && *ptr <= 'Z') continue;
2154 if (*ptr >= '0' && *ptr <= '9') continue;
2155 if (ptr > Conf_ServerName) {
2156 if (*ptr == '.' || *ptr == '-')
2159 Conf_ServerName[0] = '\0';
2163 if (!Conf_ServerName[0] || !strchr(Conf_ServerName, '.')) {
2164 config_valid = false;
2165 Config_Error(LOG_ALERT,
2166 "No (valid) server name configured (section 'Global': 'Name')!");
2167 if (!Configtest && !Rehash) {
2168 Config_Error(LOG_ALERT,
2169 "%s exiting due to fatal errors!",
2176 if (!Conf_ServerAdminMail[0]) {
2177 /* No administrative contact configured! */
2178 config_valid = false;
2179 Config_Error(LOG_ALERT,
2180 "No administrator email address configured ('AdminEMail')!");
2182 Config_Error(LOG_ALERT,
2183 "%s exiting due to fatal errors!",
2190 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
2191 && !Conf_ServerAdminMail[0]) {
2192 /* No administrative information configured! */
2193 Config_Error(LOG_WARNING,
2194 "No administrative information configured but required by RFC!");
2198 if (Conf_PAM && Conf_ServerPwd[0])
2199 Config_Error(LOG_ERR,
2200 "This server uses PAM, \"Password\" in [Global] section will be ignored!");
2203 if (Conf_MaxPenaltyTime != -1)
2204 Config_Error(LOG_WARNING,
2205 "Maximum penalty increase ('MaxPenaltyTime') is set to %ld, this is not recommended!",
2206 Conf_MaxPenaltyTime);
2208 #ifdef HAVE_SETRLIMIT
2209 if(getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
2210 LogDebug("Current file descriptor limit is %ld, maximum %ld. \"MaxConnections\" is %ld.",
2211 (long)rlim.rlim_cur, (long)rlim.rlim_max,
2212 Conf_MaxConnections);
2213 fd_lim_old = rlim.rlim_cur;
2214 /* Don't request "infinite" file descriptors, use a limit! */
2215 if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_max < MAX_FD_LIMIT)
2216 rlim.rlim_cur = rlim.rlim_max;
2218 rlim.rlim_cur = MAX_FD_LIMIT;
2219 if ((long)rlim.rlim_cur != fd_lim_old) {
2220 /* Try to adjust the current file descriptor limit: */
2221 LogDebug("Trying to upgrade \"soft\" file descriptor limit: %ld -> %ld ...",
2222 fd_lim_old, (long)rlim.rlim_cur);
2223 if(setrlimit(RLIMIT_NOFILE, &rlim) != 0)
2224 Config_Error(LOG_ERR, "Failed to adjust file descriptor limit from %ld to %ld: %s",
2225 fd_lim_old, (long)rlim.rlim_cur,
2228 /* Check the (updated?) file descriptor limit: */
2229 getrlimit(RLIMIT_NOFILE, &rlim);
2230 if (rlim.rlim_cur != RLIM_INFINITY
2231 && (long)rlim.rlim_cur <= (long)Conf_MaxConnections) {
2232 Config_Error(LOG_WARNING,
2233 "Current file descriptor limit (%ld) is not higher than configured \"MaxConnections\" (%ld)!",
2234 (long)rlim.rlim_cur, Conf_MaxConnections);
2235 } else if (!Configtest) {
2236 if (Conf_MaxConnections > 0)
2238 "File descriptor limit is %ld; \"MaxConnections\" is set to %ld.",
2239 (long)rlim.rlim_cur, Conf_MaxConnections);
2242 "File descriptor limit is %ld; \"MaxConnections\" is not set.",
2243 (long)rlim.rlim_cur);
2246 Config_Error(LOG_ERR, "Failed to get file descriptor limit: %s",
2250 servers = servers_once = 0;
2251 for (i = 0; i < MAX_SERVERS; i++) {
2252 if (Conf_Server[i].name[0]) {
2254 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
2258 LogDebug("Configuration: Operators=%ld, Servers=%d[%d], Channels=%ld",
2259 array_length(&Conf_Opers, sizeof(struct Conf_Oper)),
2260 servers, servers_once,
2261 array_length(&Conf_Channels, sizeof(struct Conf_Channel)));
2263 return config_valid;
2267 * Output "line too long" warning.
2269 * @param Line Line number in configuration file.
2270 * @param Item Affected variable name.
2273 Config_Error_TooLong(const char *File, const int Line, const char *Item)
2275 Config_Error(LOG_WARNING, "%s, line %d: Value of \"%s\" too long!",
2280 * Output "unknown variable" warning.
2282 * @param Line Line number in configuration file.
2283 * @param Item Affected variable name.
2284 * @param Section Section name.
2287 Config_Error_Section(const char *File, const int Line, const char *Item,
2288 const char *Section)
2290 Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
2291 File, Line, Section, Item);
2295 * Output "not a number" warning.
2297 * @param Line Line number in configuration file.
2298 * @param Item Affected variable name.
2301 Config_Error_NaN(const char *File, const int Line, const char *Item )
2303 Config_Error(LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
2308 * Output configuration error to console and/or logfile.
2310 * On runtime, the normal log functions of the daemon are used. But when
2311 * testing the configuration ("--configtest"), all messages go directly
2314 * @param Level Severity level of the message.
2315 * @param Format Format string; see printf() function.
2318 static void Config_Error( const int Level, const char *Format, ... )
2320 static void Config_Error( Level, Format, va_alist )
2326 char msg[MAX_LOG_MSG_LEN];
2329 assert( Format != NULL );
2332 va_start( ap, Format );
2336 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
2340 if (Level <= LOG_WARNING)
2341 printf(" - %s\n", msg);
2345 Log(Level, "%s", msg);
2350 * Dump internal state of the "configuration module".
2353 Conf_DebugDump(void)
2357 LogDebug("Configured servers:");
2358 for (i = 0; i < MAX_SERVERS; i++) {
2359 if (! Conf_Server[i].name[0])
2362 " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
2363 Conf_Server[i].name, Conf_Server[i].host,
2364 Conf_Server[i].port, Conf_Server[i].lasttry,
2365 Conf_Server[i].group, Conf_Server[i].flags,
2366 Conf_Server[i].conn_id);
2372 * Initialize server configuration structure to default values.
2374 * @param Server Pointer to server structure to initialize.
2377 Init_Server_Struct( CONF_SERVER *Server )
2379 assert( Server != NULL );
2381 memset( Server, 0, sizeof (CONF_SERVER) );
2383 Server->group = NONE;
2384 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
2386 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
2388 Proc_InitStruct(&Server->res_stat);
2389 Server->conn_id = NONE;
2390 memset(&Server->bind_addr, 0, sizeof(Server->bind_addr));
2393 /* Verify SSL connections by default! */
2394 Server->SSLVerify = true;