2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2013 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)
35 #include <sys/types.h>
52 static bool Use_Log = true, Using_MotdFile = true;
53 static CONF_SERVER New_Server;
54 static int New_Server_Idx;
56 static char Conf_MotdFile[FNAME_LEN];
57 static char Conf_HelpFile[FNAME_LEN];
59 static void Set_Defaults PARAMS(( bool InitServers ));
60 static bool Read_Config PARAMS(( bool TestOnly, bool IsStarting ));
61 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
63 static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
64 static void Handle_LIMITS PARAMS(( int Line, char *Var, char *Arg ));
65 static void Handle_OPTIONS PARAMS(( int Line, char *Var, char *Arg ));
66 static void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
67 static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
68 static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
70 static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
72 static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
73 static void Config_Error_Section PARAMS(( const int Line, const char *Item,
74 const char *Section ));
75 static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
77 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
81 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
83 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
89 static void Handle_SSL PARAMS(( int Line, char *Var, char *Ark ));
91 struct SSLOptions Conf_SSLOptions;
94 * Initialize SSL configuration.
99 free(Conf_SSLOptions.KeyFile);
100 Conf_SSLOptions.KeyFile = NULL;
102 free(Conf_SSLOptions.CertFile);
103 Conf_SSLOptions.CertFile = NULL;
105 free(Conf_SSLOptions.DHFile);
106 Conf_SSLOptions.DHFile = NULL;
107 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
109 array_free(&Conf_SSLOptions.ListenPorts);
113 * Check if the current configuration uses/requires SSL.
115 * @returns true if SSL is used and should be initialized.
122 /* SSL listen ports configured? */
123 if (array_bytes(&Conf_SSLOptions.ListenPorts))
126 for (i = 0; i < MAX_SERVERS; i++) {
127 if (Conf_Server[i].port > 0
128 && Conf_Server[i].SSLConnect)
135 * Make sure that a configured file is readable.
137 * Currently, this function is only used for SSL-related options ...
139 * @param Var Configuration variable
140 * @param Filename Configured filename
143 CheckFileReadable(const char *Var, const char *Filename)
150 fp = fopen(Filename, "r");
154 Config_Error(LOG_ERR, "Can't read \"%s\" (\"%s\"): %s",
155 Filename, Var, strerror(errno));
162 * Duplicate string and warn on errors.
164 * @returns Pointer to string on success, NULL otherwise.
167 strdup_warn(const char *str)
169 char *ptr = strdup(str);
171 Config_Error(LOG_ERR,
172 "Could not allocate memory for string: %s", str);
177 * Output a comma separated list of ports (integer values).
184 len = array_length(a, sizeof(UINT16));
186 ports = (UINT16*) array_start(a);
187 printf("%u", (unsigned int) *ports);
190 printf(", %u", (unsigned int) *ports);
197 * Parse a comma separated string into an array of port numbers (integers).
200 ports_parse(array *a, int Line, char *Arg)
208 ptr = strtok( Arg, "," );
212 if (port > 0 && port < 0xFFFF) {
213 port16 = (UINT16) port;
214 if (!array_catb(a, (char*)&port16, sizeof port16))
215 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
216 NGIRCd_ConfFile, Line, port, strerror(errno));
218 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
219 NGIRCd_ConfFile, Line, port );
222 ptr = strtok( NULL, "," );
227 * Initialize configuration module.
232 Read_Config(false, true);
233 Validate_Config(false, false);
237 * "Rehash" (reload) server configuration.
239 * @returns true if configuration has been re-read, false on errors.
244 if (!Read_Config(false, false))
246 Validate_Config(false, true);
248 /* Update CLIENT structure of local server */
249 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
254 * Output a boolean value as "yes/no" string.
257 yesno_to_str(int boolean_value)
265 * Free all IRC operator configuration structures.
270 struct Conf_Oper *op;
273 len = array_length(&Conf_Opers, sizeof(*op));
274 op = array_start(&Conf_Opers);
279 array_free(&Conf_Opers);
283 * Output all IRC operator configuration structures.
288 struct Conf_Oper *op;
291 count = array_length(&Conf_Opers, sizeof(*op));
292 op = array_start(&Conf_Opers);
293 for (i = 0; i < count; i++, op++) {
298 printf(" Name = %s\n", op->name);
299 printf(" Password = %s\n", op->pwd);
300 printf(" Mask = %s\n\n", op->mask ? op->mask : "");
305 * Read configuration, validate and output it.
307 * This function waits for a keypress of the user when stdin/stdout are valid
308 * tty's ("you can read our nice message and we can read in your keypress").
310 * @return 0 on succes, 1 on failure(s); therefore the result code can
311 * directly be used by exit() when running "ngircd --configtest".
320 size_t predef_channel_count;
321 struct Conf_Channel *predef_chan;
325 if (!Read_Config(true, true))
328 config_valid = Validate_Config(true, false);
331 if(isatty(fileno(stdin)) && isatty(fileno(stdout))) {
332 puts("OK, press enter to see a dump of your server configuration ...");
335 puts("Ok, dump of your server configuration follows:\n");
338 printf(" Name = %s\n", Conf_ServerName);
339 printf(" AdminInfo1 = %s\n", Conf_ServerAdmin1);
340 printf(" AdminInfo2 = %s\n", Conf_ServerAdmin2);
341 printf(" AdminEMail = %s\n", Conf_ServerAdminMail);
342 printf(" HelpFile = %s\n", Conf_HelpFile);
343 printf(" Info = %s\n", Conf_ServerInfo);
344 printf(" Listen = %s\n", Conf_ListenAddress);
345 if (Using_MotdFile) {
346 printf(" MotdFile = %s\n", Conf_MotdFile);
347 printf(" MotdPhrase =\n");
349 printf(" MotdFile = \n");
350 printf(" MotdPhrase = %s\n", array_bytes(&Conf_Motd)
351 ? (const char*) array_start(&Conf_Motd) : "");
354 printf(" Password = %s\n", Conf_ServerPwd);
356 printf(" PidFile = %s\n", Conf_PidFile);
358 ports_puts(&Conf_ListenPorts);
359 grp = getgrgid(Conf_GID);
361 printf(" ServerGID = %s\n", grp->gr_name);
363 printf(" ServerGID = %ld\n", (long)Conf_GID);
364 pwd = getpwuid(Conf_UID);
366 printf(" ServerUID = %s\n", pwd->pw_name);
368 printf(" ServerUID = %ld\n", (long)Conf_UID);
372 printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
373 printf(" MaxConnections = %d\n", Conf_MaxConnections);
374 printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
375 printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
376 printf(" MaxNickLength = %u\n", Conf_MaxNickLength - 1);
377 printf(" MaxListSize = %d\n", Conf_MaxListSize);
378 printf(" PingTimeout = %d\n", Conf_PingTimeout);
379 printf(" PongTimeout = %d\n", Conf_PongTimeout);
383 printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
384 printf(" ChrootDir = %s\n", Conf_Chroot);
385 printf(" CloakHost = %s\n", Conf_CloakHost);
386 printf(" CloakHostModeX = %s\n", Conf_CloakHostModeX);
387 printf(" CloakHostSalt = %s\n", Conf_CloakHostSalt);
388 printf(" CloakUserToNick = %s\n", yesno_to_str(Conf_CloakUserToNick));
390 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
391 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
393 printf(" DNS = %s\n", yesno_to_str(Conf_DNS));
395 printf(" Ident = %s\n", yesno_to_str(Conf_Ident));
397 printf(" MorePrivacy = %s\n", yesno_to_str(Conf_MorePrivacy));
398 printf(" NoticeAuth = %s\n", yesno_to_str(Conf_NoticeAuth));
399 printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
400 printf(" OperChanPAutoOp = %s\n", yesno_to_str(Conf_OperChanPAutoOp));
401 printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
403 printf(" PAM = %s\n", yesno_to_str(Conf_PAM));
404 printf(" PAMIsOptional = %s\n", yesno_to_str(Conf_PAMIsOptional));
406 printf(" PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
408 printf(" RequireAuthPing = %s\n", yesno_to_str(Conf_AuthPing));
410 printf(" ScrubCTCP = %s\n", yesno_to_str(Conf_ScrubCTCP));
412 printf(" SyslogFacility = %s\n",
413 ngt_SyslogFacilityName(Conf_SyslogFacility));
415 printf(" WebircPassword = %s\n", Conf_WebircPwd);
420 printf(" CertFile = %s\n", Conf_SSLOptions.CertFile
421 ? Conf_SSLOptions.CertFile : "");
422 printf(" DHFile = %s\n", Conf_SSLOptions.DHFile
423 ? Conf_SSLOptions.DHFile : "");
424 printf(" KeyFile = %s\n", Conf_SSLOptions.KeyFile
425 ? Conf_SSLOptions.KeyFile : "");
426 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
427 puts(" KeyFilePassword = <secret>");
429 puts(" KeyFilePassword = ");
430 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
432 ports_puts(&Conf_SSLOptions.ListenPorts);
438 for( i = 0; i < MAX_SERVERS; i++ ) {
439 if( ! Conf_Server[i].name[0] ) continue;
441 /* Valid "Server" section */
443 printf( " Name = %s\n", Conf_Server[i].name );
444 printf( " Host = %s\n", Conf_Server[i].host );
445 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
447 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
449 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
450 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
451 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
452 printf( " Group = %d\n", Conf_Server[i].group );
453 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
456 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
457 predef_chan = array_start(&Conf_Channels);
459 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
460 if (!predef_chan->name[0])
463 /* Valid "Channel" section */
465 printf(" Name = %s\n", predef_chan->name);
466 printf(" Modes = %s\n", predef_chan->modes);
467 printf(" Key = %s\n", predef_chan->key);
468 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
469 printf(" Topic = %s\n", predef_chan->topic);
470 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
473 return (config_valid ? 0 : 1);
477 * Remove connection information from configured server.
479 * If the server is set as "once", delete it from our configuration;
480 * otherwise set the time for the next connection attempt.
482 * Non-server connections will be silently ignored.
485 Conf_UnsetServer( CONN_ID Idx )
490 /* Check all our configured servers */
491 for( i = 0; i < MAX_SERVERS; i++ ) {
492 if( Conf_Server[i].conn_id != Idx ) continue;
494 /* Gotcha! Mark server configuration as "unused": */
495 Conf_Server[i].conn_id = NONE;
497 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
498 /* Delete configuration here */
499 Init_Server_Struct( &Conf_Server[i] );
501 /* Set time for next connect attempt */
503 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
504 /* The connection has been "long", so we don't
505 * require the next attempt to be delayed. */
506 Conf_Server[i].lasttry =
507 t - Conf_ConnectRetry + RECONNECT_DELAY;
509 /* "Short" connection, enforce "ConnectRetry"
510 * but randomize it a little bit: 15 seconds. */
511 Conf_Server[i].lasttry =
512 t + rand() / (RAND_MAX / 15);
519 * Set connection information for specified configured server.
522 Conf_SetServer( int ConfServer, CONN_ID Idx )
524 assert( ConfServer > NONE );
525 assert( Idx > NONE );
527 if (Conf_Server[ConfServer].conn_id > NONE &&
528 Conf_Server[ConfServer].conn_id != Idx) {
530 "Connection %d: Server configuration of \"%s\" already in use by connection %d!",
531 Idx, Conf_Server[ConfServer].name,
532 Conf_Server[ConfServer].conn_id);
533 Conn_Close(Idx, NULL, "Server configuration already in use", true);
536 Conf_Server[ConfServer].conn_id = Idx;
541 * Get index of server in configuration structure.
544 Conf_GetServer( CONN_ID Idx )
548 assert( Idx > NONE );
550 for( i = 0; i < MAX_SERVERS; i++ ) {
551 if( Conf_Server[i].conn_id == Idx ) return i;
557 * Enable a server by name and adjust its port number.
559 * @returns true if a server has been enabled and now has a valid port
560 * number and host name for outgoing connections.
563 Conf_EnableServer( const char *Name, UINT16 Port )
567 assert( Name != NULL );
568 for( i = 0; i < MAX_SERVERS; i++ ) {
569 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
570 /* Gotcha! Set port and enable server: */
571 Conf_Server[i].port = Port;
572 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
573 return (Conf_Server[i].port && Conf_Server[i].host[0]);
580 * Enable a server by name.
582 * The server is only usable as outgoing server, if it has set a valid port
583 * number for outgoing connections!
584 * If not, you have to use Conf_EnableServer() function to make it available.
586 * @returns true if a server has been enabled; false otherwise.
589 Conf_EnablePassiveServer(const char *Name)
593 assert( Name != NULL );
594 for (i = 0; i < MAX_SERVERS; i++) {
595 if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
596 && (Conf_Server[i].port > 0)) {
597 /* BINGO! Enable server */
598 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
606 * Disable a server by name.
607 * An already established connection will be disconnected.
609 * @returns true if a server was found and has been disabled.
612 Conf_DisableServer( const char *Name )
616 assert( Name != NULL );
617 for( i = 0; i < MAX_SERVERS; i++ ) {
618 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
619 /* Gotcha! Disable and disconnect server: */
620 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
621 if( Conf_Server[i].conn_id > NONE )
622 Conn_Close(Conf_Server[i].conn_id, NULL,
623 "Server link terminated on operator request",
632 * Add a new remote server to our configuration.
634 * @param Name Name of the new server.
635 * @param Port Port number to connect to or 0 for incoming connections.
636 * @param Host Host name to connect to.
637 * @param MyPwd Password that will be sent to the peer.
638 * @param PeerPwd Password that must be received from the peer.
639 * @returns true if the new server has been added; false otherwise.
642 Conf_AddServer(const char *Name, UINT16 Port, const char *Host,
643 const char *MyPwd, const char *PeerPwd)
647 assert( Name != NULL );
648 assert( Host != NULL );
649 assert( MyPwd != NULL );
650 assert( PeerPwd != NULL );
652 /* Search unused item in server configuration structure */
653 for( i = 0; i < MAX_SERVERS; i++ ) {
654 /* Is this item used? */
655 if( ! Conf_Server[i].name[0] ) break;
657 if( i >= MAX_SERVERS ) return false;
659 Init_Server_Struct( &Conf_Server[i] );
660 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
661 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
662 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
663 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
664 Conf_Server[i].port = Port;
665 Conf_Server[i].flags = CONF_SFLAG_ONCE;
671 * Check if the given nickname is reserved for services on a particular server.
673 * @param ConfServer The server index to check.
674 * @param Nick The nickname to check.
675 * @returns true if the given nickname belongs to an "IRC service".
678 Conf_NickIsService(int ConfServer, const char *Nick)
680 assert (ConfServer >= 0);
681 assert (ConfServer < MAX_SERVERS);
683 return MatchCaseInsensitiveList(Conf_Server[ConfServer].svs_mask,
688 * Check if the given nickname is blocked for "normal client" use.
690 * @param ConfServer The server index or NONE to check all configured servers.
691 * @param Nick The nickname to check.
692 * @returns true if the given nickname belongs to an "IRC service".
695 Conf_NickIsBlocked(const char *Nick)
699 for(i = 0; i < MAX_SERVERS; i++) {
700 if (!Conf_Server[i].name[0])
702 if (Conf_NickIsService(i, Nick))
709 * Initialize configuration settings with their default values.
712 Set_Defaults(bool InitServers)
715 char random[RANDOM_SALT_LEN + 1];
718 strcpy(Conf_ServerName, "");
719 strcpy(Conf_ServerAdmin1, "");
720 strcpy(Conf_ServerAdmin2, "");
721 strcpy(Conf_ServerAdminMail, "");
722 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
723 PACKAGE_NAME, PACKAGE_VERSION);
724 free(Conf_ListenAddress);
725 Conf_ListenAddress = NULL;
726 array_free(&Conf_ListenPorts);
727 array_free(&Conf_Motd);
728 array_free(&Conf_Helptext);
729 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
730 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
731 strlcpy(Conf_HelpFile, DOCDIR, sizeof(Conf_HelpFile));
732 strlcat(Conf_HelpFile, HELP_FILE, sizeof(Conf_HelpFile));
733 strcpy(Conf_ServerPwd, "");
734 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
735 Conf_UID = Conf_GID = 0;
738 Conf_ConnectRetry = 60;
739 Conf_MaxConnections = 0;
740 Conf_MaxConnectionsIP = 5;
742 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
743 Conf_MaxListSize = 100;
744 Conf_PingTimeout = 120;
745 Conf_PongTimeout = 20;
748 Conf_AllowRemoteOper = false;
750 Conf_AuthPing = false;
752 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
753 strcpy(Conf_CloakHost, "");
754 strcpy(Conf_CloakHostModeX, "");
755 strlcpy(Conf_CloakHostSalt, ngt_RandomStr(random, RANDOM_SALT_LEN),
756 sizeof(Conf_CloakHostSalt));
757 Conf_CloakUserToNick = false;
758 Conf_ConnectIPv4 = true;
760 Conf_ConnectIPv6 = true;
762 Conf_ConnectIPv6 = false;
770 Conf_MorePrivacy = false;
771 Conf_NoticeAuth = false;
772 Conf_OperCanMode = false;
773 Conf_OperChanPAutoOp = true;
774 Conf_OperServerMode = false;
780 Conf_PAMIsOptional = false;
781 Conf_PredefChannelsOnly = false;
783 Conf_ScrubCTCP = false;
785 Conf_SyslogFacility = LOG_LOCAL5;
787 Conf_SyslogFacility = 0;
791 /* Initialize server configuration structures */
793 for (i = 0; i < MAX_SERVERS;
794 Init_Server_Struct(&Conf_Server[i++]));
799 * Get number of configured listening ports.
801 * @returns The number of ports (IPv4+IPv6) on which the server should listen.
806 size_t cnt = array_bytes(&Conf_ListenPorts);
808 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
814 * Read contents of a text file into an array.
816 * This function is used to read the MOTD and help text file, for exampe.
818 * @param filename Name of the file to read.
819 * @return true, when the file has been read in.
822 Read_TextFile(const char *Filename, const char *Name, array *Destination)
828 if (*Filename == '\0')
831 fp = fopen(Filename, "r");
833 Config_Error(LOG_WARNING, "Can't read %s file \"%s\": %s",
834 Name, Filename, strerror(errno));
838 array_free(Destination);
839 while (fgets(line, (int)sizeof line, fp)) {
840 ngt_TrimLastChr(line, '\n');
842 /* add text including \0 */
843 if (!array_catb(Destination, line, strlen(line) + 1)) {
844 Log(LOG_WARNING, "Cannot read/add \"%s\", line %d: %s",
845 Filename, line_no, strerror(errno));
855 * Read ngIRCd configuration file.
857 * Please note that this function uses exit(1) on fatal errors and therefore
858 * can result in ngIRCd terminating!
860 * @param ngircd_starting Flag indicating if ngIRCd is starting or not.
861 * @returns true when the configuration file has been read
862 * successfully; false otherwise.
865 Read_Config(bool TestOnly, bool IsStarting)
867 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
868 const UINT16 defaultport = 6667;
873 /* Open configuration file */
874 fd = fopen( NGIRCd_ConfFile, "r" );
876 /* No configuration file found! */
877 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
878 NGIRCd_ConfFile, strerror( errno ));
881 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
886 Set_Defaults(IsStarting);
889 Config_Error(LOG_INFO,
890 "Reading configuration from \"%s\" ...",
893 /* Clean up server configuration structure: mark all already
894 * configured servers as "once" so that they are deleted
895 * after the next disconnect and delete all unused servers.
896 * And delete all servers which are "duplicates" of servers
897 * that are already marked as "once" (such servers have been
898 * created by the last rehash but are now useless). */
899 for( i = 0; i < MAX_SERVERS; i++ ) {
900 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
902 /* This structure is in use ... */
903 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
904 /* Check for duplicates */
905 for( n = 0; n < MAX_SERVERS; n++ ) {
906 if( n == i ) continue;
908 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
909 Init_Server_Struct( &Conf_Server[n] );
911 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
917 /* Mark server as "once" */
918 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
919 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
924 /* Initialize variables */
926 strcpy( section, "" );
927 Init_Server_Struct( &New_Server );
928 New_Server_Idx = NONE;
932 /* Read configuration file */
934 if( ! fgets( str, LINE_LEN, fd )) break;
938 /* Skip comments and empty lines */
939 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
941 /* Is this the beginning of a new section? */
942 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
943 strlcpy( section, str, sizeof( section ));
944 if (strcasecmp(section, "[GLOBAL]") == 0
945 || strcasecmp(section, "[LIMITS]") == 0
946 || strcasecmp(section, "[OPTIONS]") == 0
948 || strcasecmp(section, "[SSL]") == 0
953 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
954 /* Check if there is already a server to add */
955 if( New_Server.name[0] ) {
956 /* Copy data to "real" server structure */
957 assert( New_Server_Idx > NONE );
958 Conf_Server[New_Server_Idx] = New_Server;
961 /* Re-init structure for new server */
962 Init_Server_Struct( &New_Server );
964 /* Search unused item in server configuration structure */
965 for( i = 0; i < MAX_SERVERS; i++ ) {
966 /* Is this item used? */
967 if( ! Conf_Server[i].name[0] ) break;
969 if( i >= MAX_SERVERS ) {
970 /* Oops, no free item found! */
971 Config_Error( LOG_ERR, "Too many servers configured." );
972 New_Server_Idx = NONE;
974 else New_Server_Idx = i;
978 if (strcasecmp(section, "[CHANNEL]") == 0) {
979 count = array_length(&Conf_Channels,
980 sizeof(struct Conf_Channel));
981 if (!array_alloc(&Conf_Channels,
982 sizeof(struct Conf_Channel),
984 Config_Error(LOG_ERR,
985 "Could not allocate memory for new operator (line %d)",
991 if (strcasecmp(section, "[OPERATOR]") == 0) {
992 count = array_length(&Conf_Opers,
993 sizeof(struct Conf_Oper));
994 if (!array_alloc(&Conf_Opers,
995 sizeof(struct Conf_Oper),
997 Config_Error(LOG_ERR,
998 "Could not allocate memory for new channel (line &d)",
1004 Config_Error(LOG_ERR,
1005 "%s, line %d: Unknown section \"%s\"!",
1006 NGIRCd_ConfFile, line, section);
1009 if( section[0] == 0x1 ) continue;
1011 /* Split line into variable name and parameters */
1012 ptr = strchr( str, '=' );
1014 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
1018 var = str; ngt_TrimStr( var );
1019 arg = ptr + 1; ngt_TrimStr( arg );
1021 if(strcasecmp(section, "[GLOBAL]") == 0)
1022 Handle_GLOBAL(line, var, arg);
1023 else if(strcasecmp(section, "[LIMITS]") == 0)
1024 Handle_LIMITS(line, var, arg);
1025 else if(strcasecmp(section, "[OPTIONS]") == 0)
1026 Handle_OPTIONS(line, var, arg);
1028 else if(strcasecmp(section, "[SSL]") == 0)
1029 Handle_SSL(line, var, arg);
1031 else if(strcasecmp(section, "[OPERATOR]") == 0)
1032 Handle_OPERATOR(line, var, arg);
1033 else if(strcasecmp(section, "[SERVER]") == 0)
1034 Handle_SERVER(line, var, arg);
1035 else if(strcasecmp(section, "[CHANNEL]") == 0)
1036 Handle_CHANNEL(line, var, arg);
1038 Config_Error(LOG_ERR,
1039 "%s, line %d: Variable \"%s\" outside section!",
1040 NGIRCd_ConfFile, line, var);
1043 /* Close configuration file */
1046 /* Check if there is still a server to add */
1047 if( New_Server.name[0] ) {
1048 /* Copy data to "real" server structure */
1049 assert( New_Server_Idx > NONE );
1050 Conf_Server[New_Server_Idx] = New_Server;
1053 /* not a single listening port? Add default. */
1054 if (no_listenports() &&
1055 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
1057 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
1058 (unsigned int) defaultport, strerror(errno));
1063 if (!Conf_ListenAddress)
1064 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
1066 if (!Conf_ListenAddress) {
1067 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
1071 /* No MOTD phrase configured? (re)try motd file. */
1072 if (array_bytes(&Conf_Motd) == 0) {
1073 if (Read_TextFile(Conf_MotdFile, "MOTD", &Conf_Motd))
1074 Using_MotdFile = true;
1077 /* Try to read ngIRCd help text file. */
1078 (void)Read_TextFile(Conf_HelpFile, "help text", &Conf_Helptext);
1079 if (!array_bytes(&Conf_Helptext))
1080 Config_Error(LOG_WARNING,
1081 "No help text available, HELP command will be of limited use.");
1084 /* Make sure that all SSL-related files are readable */
1085 CheckFileReadable("CertFile", Conf_SSLOptions.CertFile);
1086 CheckFileReadable("DHFile", Conf_SSLOptions.DHFile);
1087 CheckFileReadable("KeyFile", Conf_SSLOptions.KeyFile);
1094 * Check whether a string argument is "true" or "false".
1096 * @param Arg Input string.
1097 * @returns true if the input string has been parsed as "yes", "true"
1098 * (case insensitive) or a non-zero integer value.
1101 Check_ArgIsTrue(const char *Arg)
1103 if (strcasecmp(Arg, "yes") == 0)
1105 if (strcasecmp(Arg, "true") == 0)
1114 * Handle setting of "MaxNickLength".
1116 * @param Line Line number in configuration file.
1117 * @raram Arg Input string.
1118 * @returns New configured maximum nickname length.
1121 Handle_MaxNickLength(int Line, const char *Arg)
1125 new = (unsigned) atoi(Arg) + 1;
1126 if (new > CLIENT_NICK_LEN) {
1127 Config_Error(LOG_WARNING,
1128 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
1129 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
1130 return CLIENT_NICK_LEN;
1133 Config_Error(LOG_WARNING,
1134 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
1135 NGIRCd_ConfFile, Line);
1142 * Output a warning messages if IDENT is configured but not compiled in.
1145 WarnIdent(int UNUSED Line)
1149 /* user has enabled ident lookups explicitly, but ... */
1150 Config_Error(LOG_WARNING,
1151 "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
1152 NGIRCd_ConfFile, Line);
1158 * Output a warning messages if IPv6 is configured but not compiled in.
1161 WarnIPv6(int UNUSED Line)
1164 if (Conf_ConnectIPv6) {
1165 /* user has enabled IPv6 explicitly, but ... */
1166 Config_Error(LOG_WARNING,
1167 "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
1168 NGIRCd_ConfFile, Line);
1174 * Output a warning messages if PAM is configured but not compiled in.
1177 WarnPAM(int UNUSED Line)
1181 Config_Error(LOG_WARNING,
1182 "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
1183 NGIRCd_ConfFile, Line);
1189 * Handle legacy "NoXXX" options in [GLOBAL] section.
1191 * TODO: This function and support for "NoXXX" could be removed starting
1192 * with ngIRCd release 19 (one release after marking it "deprecated").
1194 * @param Var Variable name.
1195 * @param Arg Argument string.
1196 * @returns true if a NoXXX option has been processed; false otherwise.
1199 CheckLegacyNoOption(const char *Var, const char *Arg)
1201 if(strcasecmp(Var, "NoDNS") == 0) {
1202 Conf_DNS = !Check_ArgIsTrue( Arg );
1205 if (strcasecmp(Var, "NoIdent") == 0) {
1206 Conf_Ident = !Check_ArgIsTrue(Arg);
1209 if(strcasecmp(Var, "NoPAM") == 0) {
1210 Conf_PAM = !Check_ArgIsTrue(Arg);
1217 * Handle deprecated legacy options in [GLOBAL] section.
1219 * TODO: This function and support for these options in the [Global] section
1220 * could be removed starting with ngIRCd release 19 (one release after
1221 * marking it "deprecated").
1223 * @param Var Variable name.
1224 * @param Arg Argument string.
1225 * @returns true if a legacy option has been processed; false otherwise.
1228 CheckLegacyGlobalOption(int Line, char *Var, char *Arg)
1230 if (strcasecmp(Var, "AllowRemoteOper") == 0
1231 || strcasecmp(Var, "ChrootDir") == 0
1232 || strcasecmp(Var, "ConnectIPv4") == 0
1233 || strcasecmp(Var, "ConnectIPv6") == 0
1234 || strcasecmp(Var, "OperCanUseMode") == 0
1235 || strcasecmp(Var, "OperChanPAutoOp") == 0
1236 || strcasecmp(Var, "OperServerMode") == 0
1237 || strcasecmp(Var, "PredefChannelsOnly") == 0
1238 || strcasecmp(Var, "SyslogFacility") == 0
1239 || strcasecmp(Var, "WebircPassword") == 0) {
1240 Handle_OPTIONS(Line, Var, Arg);
1243 if (strcasecmp(Var, "ConnectRetry") == 0
1244 || strcasecmp(Var, "MaxConnections") == 0
1245 || strcasecmp(Var, "MaxConnectionsIP") == 0
1246 || strcasecmp(Var, "MaxJoins") == 0
1247 || strcasecmp(Var, "MaxNickLength") == 0
1248 || strcasecmp(Var, "PingTimeout") == 0
1249 || strcasecmp(Var, "PongTimeout") == 0) {
1250 Handle_LIMITS(Line, Var, Arg);
1254 if (strcasecmp(Var, "SSLCertFile") == 0
1255 || strcasecmp(Var, "SSLDHFile") == 0
1256 || strcasecmp(Var, "SSLKeyFile") == 0
1257 || strcasecmp(Var, "SSLKeyFilePassword") == 0
1258 || strcasecmp(Var, "SSLPorts") == 0) {
1259 Handle_SSL(Line, Var + 3, Arg);
1268 * Strip "no" prefix of a string.
1270 * TODO: This function and support for "NoXXX" should be removed starting
1271 * with ngIRCd release 19! (One release after marking it "deprecated").
1273 * @param str Pointer to input string starting with "no".
1274 * @returns New pointer to string without "no" prefix.
1277 NoNo(const char *str)
1279 assert(strncasecmp("no", str, 2) == 0 && str[2]);
1284 * Invert "boolean" string.
1286 * TODO: This function and support for "NoXXX" should be removed starting
1287 * with ngIRCd release 19! (One release after marking it "deprecated").
1289 * @param arg "Boolean" input string.
1290 * @returns Pointer to inverted "boolean string".
1293 InvertArg(const char *arg)
1295 return yesno_to_str(!Check_ArgIsTrue(arg));
1299 * Handle variable in [Global] configuration section.
1301 * @param Line Line numer in configuration file.
1302 * @param Var Variable name.
1303 * @param Arg Variable argument.
1306 Handle_GLOBAL( int Line, char *Var, char *Arg )
1311 const char *section;
1314 assert(Var != NULL);
1315 assert(Arg != NULL);
1317 if (strcasecmp(Var, "Name") == 0) {
1318 len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
1319 if (len >= sizeof(Conf_ServerName))
1320 Config_Error_TooLong(Line, Var);
1323 if (strcasecmp(Var, "AdminInfo1") == 0) {
1324 len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
1325 if (len >= sizeof(Conf_ServerAdmin1))
1326 Config_Error_TooLong(Line, Var);
1329 if (strcasecmp(Var, "AdminInfo2") == 0) {
1330 len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
1331 if (len >= sizeof(Conf_ServerAdmin2))
1332 Config_Error_TooLong(Line, Var);
1335 if (strcasecmp(Var, "AdminEMail") == 0) {
1336 len = strlcpy(Conf_ServerAdminMail, Arg,
1337 sizeof(Conf_ServerAdminMail));
1338 if (len >= sizeof(Conf_ServerAdminMail))
1339 Config_Error_TooLong(Line, Var);
1342 if (strcasecmp(Var, "Info") == 0) {
1343 len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
1344 if (len >= sizeof(Conf_ServerInfo))
1345 Config_Error_TooLong(Line, Var);
1348 if (strcasecmp(Var, "HelpFile") == 0) {
1349 len = strlcpy(Conf_HelpFile, Arg, sizeof(Conf_HelpFile));
1350 if (len >= sizeof(Conf_HelpFile))
1351 Config_Error_TooLong(Line, Var);
1354 if (strcasecmp(Var, "Listen") == 0) {
1355 if (Conf_ListenAddress) {
1356 Config_Error(LOG_ERR,
1357 "Multiple Listen= options, ignoring: %s",
1361 Conf_ListenAddress = strdup_warn(Arg);
1362 /* If allocation fails, we're in trouble: we cannot ignore the
1363 * error -- otherwise ngircd would listen on all interfaces. */
1364 if (!Conf_ListenAddress) {
1365 Config_Error(LOG_ALERT,
1366 "%s exiting due to fatal errors!",
1372 if (strcasecmp(Var, "MotdFile") == 0) {
1373 len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
1374 if (len >= sizeof(Conf_MotdFile))
1375 Config_Error_TooLong(Line, Var);
1378 if (strcasecmp(Var, "MotdPhrase") == 0) {
1382 if (len >= LINE_LEN) {
1383 Config_Error_TooLong(Line, Var);
1386 if (!array_copyb(&Conf_Motd, Arg, len + 1))
1387 Config_Error(LOG_WARNING,
1388 "%s, line %d: Could not append MotdPhrase: %s",
1389 NGIRCd_ConfFile, Line, strerror(errno));
1390 Using_MotdFile = false;
1393 if(strcasecmp(Var, "Password") == 0) {
1394 len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
1395 if (len >= sizeof(Conf_ServerPwd))
1396 Config_Error_TooLong(Line, Var);
1399 if (strcasecmp(Var, "PidFile") == 0) {
1400 len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
1401 if (len >= sizeof(Conf_PidFile))
1402 Config_Error_TooLong(Line, Var);
1405 if (strcasecmp(Var, "Ports") == 0) {
1406 ports_parse(&Conf_ListenPorts, Line, Arg);
1409 if (strcasecmp(Var, "ServerGID") == 0) {
1410 grp = getgrnam(Arg);
1412 Conf_GID = grp->gr_gid;
1414 Conf_GID = (unsigned int)atoi(Arg);
1415 if (!Conf_GID && strcmp(Arg, "0"))
1416 Config_Error(LOG_WARNING,
1417 "%s, line %d: Value of \"%s\" is not a valid group name or ID!",
1418 NGIRCd_ConfFile, Line, Var);
1422 if (strcasecmp(Var, "ServerUID") == 0) {
1423 pwd = getpwnam(Arg);
1425 Conf_UID = pwd->pw_uid;
1427 Conf_UID = (unsigned int)atoi(Arg);
1428 if (!Conf_UID && strcmp(Arg, "0"))
1429 Config_Error(LOG_WARNING,
1430 "%s, line %d: Value of \"%s\" is not a valid user name or ID!",
1431 NGIRCd_ConfFile, Line, Var);
1436 if (CheckLegacyNoOption(Var, Arg)) {
1437 /* TODO: This function and support for "NoXXX" could be
1438 * be removed starting with ngIRCd release 19 (one release
1439 * after marking it "deprecated"). */
1440 Config_Error(LOG_WARNING,
1441 "%s, line %d (section \"Global\"): \"No\"-Prefix is deprecated, use \"%s = %s\" in [Options] section!",
1442 NGIRCd_ConfFile, Line, NoNo(Var), InvertArg(Arg));
1443 if (strcasecmp(Var, "NoIdent") == 0)
1445 else if (strcasecmp(Var, "NoPam") == 0)
1449 if ((section = CheckLegacyGlobalOption(Line, Var, Arg))) {
1450 /** TODO: This function and support for these options in the
1451 * [Global] section could be removed starting with ngIRCd
1452 * release 19 (one release after marking it "deprecated"). */
1453 if (strncasecmp(Var, "SSL", 3) == 0) {
1454 Config_Error(LOG_WARNING,
1455 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s and rename to \"%s\"!",
1456 NGIRCd_ConfFile, Line, Var, section,
1459 Config_Error(LOG_WARNING,
1460 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s!",
1461 NGIRCd_ConfFile, Line, Var, section);
1466 Config_Error_Section(Line, Var, "Global");
1470 * Handle variable in [Limits] configuration section.
1472 * @param Line Line numer in configuration file.
1473 * @param Var Variable name.
1474 * @param Arg Variable argument.
1477 Handle_LIMITS(int Line, char *Var, char *Arg)
1480 assert(Var != NULL);
1481 assert(Arg != NULL);
1483 if (strcasecmp(Var, "ConnectRetry") == 0) {
1484 Conf_ConnectRetry = atoi(Arg);
1485 if (Conf_ConnectRetry < 5) {
1486 Config_Error(LOG_WARNING,
1487 "%s, line %d: Value of \"ConnectRetry\" too low!",
1488 NGIRCd_ConfFile, Line);
1489 Conf_ConnectRetry = 5;
1493 if (strcasecmp(Var, "MaxConnections") == 0) {
1494 Conf_MaxConnections = atoi(Arg);
1495 if (!Conf_MaxConnections && strcmp(Arg, "0"))
1496 Config_Error_NaN(Line, Var);
1499 if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
1500 Conf_MaxConnectionsIP = atoi(Arg);
1501 if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
1502 Config_Error_NaN(Line, Var);
1505 if (strcasecmp(Var, "MaxJoins") == 0) {
1506 Conf_MaxJoins = atoi(Arg);
1507 if (!Conf_MaxJoins && strcmp(Arg, "0"))
1508 Config_Error_NaN(Line, Var);
1511 if (strcasecmp(Var, "MaxNickLength") == 0) {
1512 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1515 if (strcasecmp(Var, "MaxListSize") == 0) {
1516 Conf_MaxListSize = atoi(Arg);
1517 if (!Conf_MaxListSize && strcmp(Arg, "0"))
1518 Config_Error_NaN(Line, Var);
1521 if (strcasecmp(Var, "PingTimeout") == 0) {
1522 Conf_PingTimeout = atoi(Arg);
1523 if (Conf_PingTimeout < 5) {
1524 Config_Error(LOG_WARNING,
1525 "%s, line %d: Value of \"PingTimeout\" too low!",
1526 NGIRCd_ConfFile, Line);
1527 Conf_PingTimeout = 5;
1531 if (strcasecmp(Var, "PongTimeout") == 0) {
1532 Conf_PongTimeout = atoi(Arg);
1533 if (Conf_PongTimeout < 5) {
1534 Config_Error(LOG_WARNING,
1535 "%s, line %d: Value of \"PongTimeout\" too low!",
1536 NGIRCd_ConfFile, Line);
1537 Conf_PongTimeout = 5;
1542 Config_Error_Section(Line, Var, "Limits");
1546 * Handle variable in [Options] configuration section.
1548 * @param Line Line numer in configuration file.
1549 * @param Var Variable name.
1550 * @param Arg Variable argument.
1553 Handle_OPTIONS(int Line, char *Var, char *Arg)
1558 assert(Var != NULL);
1559 assert(Arg != NULL);
1561 if (strcasecmp(Var, "AllowRemoteOper") == 0) {
1562 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1565 if (strcasecmp(Var, "ChrootDir") == 0) {
1566 len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
1567 if (len >= sizeof(Conf_Chroot))
1568 Config_Error_TooLong(Line, Var);
1571 if (strcasecmp(Var, "CloakHost") == 0) {
1572 len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
1573 if (len >= sizeof(Conf_CloakHost))
1574 Config_Error_TooLong(Line, Var);
1577 if (strcasecmp(Var, "CloakHostModeX") == 0) {
1578 len = strlcpy(Conf_CloakHostModeX, Arg, sizeof(Conf_CloakHostModeX));
1579 if (len >= sizeof(Conf_CloakHostModeX))
1580 Config_Error_TooLong(Line, Var);
1583 if (strcasecmp(Var, "CloakHostSalt") == 0) {
1584 len = strlcpy(Conf_CloakHostSalt, Arg, sizeof(Conf_CloakHostSalt));
1585 if (len >= sizeof(Conf_CloakHostSalt))
1586 Config_Error_TooLong(Line, Var);
1589 if (strcasecmp(Var, "CloakUserToNick") == 0) {
1590 Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
1593 if (strcasecmp(Var, "ConnectIPv6") == 0) {
1594 Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
1598 if (strcasecmp(Var, "ConnectIPv4") == 0) {
1599 Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
1602 if (strcasecmp(Var, "DNS") == 0) {
1603 Conf_DNS = Check_ArgIsTrue(Arg);
1606 if (strcasecmp(Var, "Ident") == 0) {
1607 Conf_Ident = Check_ArgIsTrue(Arg);
1611 if (strcasecmp(Var, "MorePrivacy") == 0) {
1612 Conf_MorePrivacy = Check_ArgIsTrue(Arg);
1615 if (strcasecmp(Var, "NoticeAuth") == 0) {
1616 Conf_NoticeAuth = Check_ArgIsTrue(Arg);
1619 if (strcasecmp(Var, "OperCanUseMode") == 0) {
1620 Conf_OperCanMode = Check_ArgIsTrue(Arg);
1623 if (strcasecmp(Var, "OperChanPAutoOp") == 0) {
1624 Conf_OperChanPAutoOp = Check_ArgIsTrue(Arg);
1627 if (strcasecmp(Var, "OperServerMode") == 0) {
1628 Conf_OperServerMode = Check_ArgIsTrue(Arg);
1631 if (strcasecmp(Var, "PAM") == 0) {
1632 Conf_PAM = Check_ArgIsTrue(Arg);
1636 if (strcasecmp(Var, "PAMIsOptional") == 0 ) {
1637 Conf_PAMIsOptional = Check_ArgIsTrue(Arg);
1640 if (strcasecmp(Var, "PredefChannelsOnly") == 0) {
1641 Conf_PredefChannelsOnly = Check_ArgIsTrue(Arg);
1645 if (strcasecmp(Var, "RequireAuthPing") == 0) {
1646 Conf_AuthPing = Check_ArgIsTrue(Arg);
1650 if (strcasecmp(Var, "ScrubCTCP") == 0) {
1651 Conf_ScrubCTCP = Check_ArgIsTrue(Arg);
1655 if (strcasecmp(Var, "SyslogFacility") == 0) {
1656 Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
1657 Conf_SyslogFacility);
1661 if (strcasecmp(Var, "WebircPassword") == 0) {
1662 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
1663 if (len >= sizeof(Conf_WebircPwd))
1664 Config_Error_TooLong(Line, Var);
1668 Config_Error_Section(Line, Var, "Options");
1674 * Handle variable in [SSL] configuration section.
1676 * @param Line Line numer in configuration file.
1677 * @param Var Variable name.
1678 * @param Arg Variable argument.
1681 Handle_SSL(int Line, char *Var, char *Arg)
1684 assert(Var != NULL);
1685 assert(Arg != NULL);
1687 if (strcasecmp(Var, "CertFile") == 0) {
1688 assert(Conf_SSLOptions.CertFile == NULL);
1689 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1692 if (strcasecmp(Var, "DHFile") == 0) {
1693 assert(Conf_SSLOptions.DHFile == NULL);
1694 Conf_SSLOptions.DHFile = strdup_warn(Arg);
1697 if (strcasecmp(Var, "KeyFile") == 0) {
1698 assert(Conf_SSLOptions.KeyFile == NULL);
1699 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1702 if (strcasecmp(Var, "KeyFilePassword") == 0) {
1703 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1704 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1705 Config_Error(LOG_ERR,
1706 "%s, line %d (section \"SSL\"): Could not copy %s: %s!",
1707 NGIRCd_ConfFile, Line, Var,
1711 if (strcasecmp(Var, "Ports") == 0) {
1712 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1716 Config_Error_Section(Line, Var, "SSL");
1722 * Handle variable in [Operator] configuration section.
1724 * @param Line Line numer in configuration file.
1725 * @param Var Variable name.
1726 * @param Arg Variable argument.
1729 Handle_OPERATOR( int Line, char *Var, char *Arg )
1732 struct Conf_Oper *op;
1735 assert( Var != NULL );
1736 assert( Arg != NULL );
1738 op = array_get(&Conf_Opers, sizeof(*op),
1739 array_length(&Conf_Opers, sizeof(*op)) - 1);
1743 if (strcasecmp(Var, "Name") == 0) {
1744 /* Name of IRC operator */
1745 len = strlcpy(op->name, Arg, sizeof(op->name));
1746 if (len >= sizeof(op->name))
1747 Config_Error_TooLong(Line, Var);
1750 if (strcasecmp(Var, "Password") == 0) {
1751 /* Password of IRC operator */
1752 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1753 if (len >= sizeof(op->pwd))
1754 Config_Error_TooLong(Line, Var);
1757 if (strcasecmp(Var, "Mask") == 0) {
1759 return; /* Hostname already configured */
1760 op->mask = strdup_warn( Arg );
1764 Config_Error_Section(Line, Var, "Operator");
1768 * Handle variable in [Server] configuration section.
1770 * @param Line Line numer in configuration file.
1771 * @param Var Variable name.
1772 * @param Arg Variable argument.
1775 Handle_SERVER( int Line, char *Var, char *Arg )
1781 assert( Var != NULL );
1782 assert( Arg != NULL );
1784 /* Ignore server block if no space is left in server configuration structure */
1785 if( New_Server_Idx <= NONE ) return;
1787 if( strcasecmp( Var, "Host" ) == 0 ) {
1788 /* Hostname of the server */
1789 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1790 if (len >= sizeof( New_Server.host ))
1791 Config_Error_TooLong ( Line, Var );
1794 if( strcasecmp( Var, "Name" ) == 0 ) {
1795 /* Name of the server ("Nick"/"ID") */
1796 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1797 if (len >= sizeof( New_Server.name ))
1798 Config_Error_TooLong( Line, Var );
1801 if (strcasecmp(Var, "Bind") == 0) {
1802 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1805 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1806 NGIRCd_ConfFile, Line, Arg);
1809 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1810 /* Password of this server which is sent to the peer */
1812 Config_Error(LOG_ERR,
1813 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1814 NGIRCd_ConfFile, Line);
1816 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1817 if (len >= sizeof( New_Server.pwd_in ))
1818 Config_Error_TooLong( Line, Var );
1821 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1822 /* Passwort of the peer which must be received */
1823 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1824 if (len >= sizeof( New_Server.pwd_out ))
1825 Config_Error_TooLong( Line, Var );
1828 if( strcasecmp( Var, "Port" ) == 0 ) {
1829 /* Port to which this server should connect */
1831 if (port >= 0 && port < 0xFFFF)
1832 New_Server.port = (UINT16)port;
1834 Config_Error(LOG_ERR,
1835 "%s, line %d (section \"Server\"): Illegal port number %ld!",
1836 NGIRCd_ConfFile, Line, port );
1840 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1841 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1845 if( strcasecmp( Var, "Group" ) == 0 ) {
1847 New_Server.group = atoi( Arg );
1848 if (!New_Server.group && strcmp(Arg, "0"))
1849 Config_Error_NaN(Line, Var);
1852 if( strcasecmp( Var, "Passive" ) == 0 ) {
1853 if (Check_ArgIsTrue(Arg))
1854 New_Server.flags |= CONF_SFLAG_DISABLED;
1857 if (strcasecmp(Var, "ServiceMask") == 0) {
1858 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1859 sizeof(New_Server.svs_mask));
1860 if (len >= sizeof(New_Server.svs_mask))
1861 Config_Error_TooLong(Line, Var);
1865 Config_Error_Section(Line, Var, "Server");
1869 * Copy channel name into channel structure.
1871 * If the channel name is not valid because of a missing prefix ('#', '&'),
1872 * a default prefix of '#' will be added.
1874 * @param new_chan New already allocated channel structure.
1875 * @param name Name of the new channel.
1876 * @returns true on success, false otherwise.
1879 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1881 size_t size = sizeof(new_chan->name);
1882 char *dest = new_chan->name;
1884 if (!Channel_IsValidName(name)) {
1886 * maybe user forgot to add a '#'.
1887 * This is only here for user convenience.
1893 return size > strlcpy(dest, name, size);
1897 * Handle variable in [Channel] configuration section.
1899 * @param Line Line numer in configuration file.
1900 * @param Var Variable name.
1901 * @param Arg Variable argument.
1904 Handle_CHANNEL(int Line, char *Var, char *Arg)
1907 struct Conf_Channel *chan;
1910 assert( Var != NULL );
1911 assert( Arg != NULL );
1913 chan = array_get(&Conf_Channels, sizeof(*chan),
1914 array_length(&Conf_Channels, sizeof(*chan)) - 1);
1918 if (strcasecmp(Var, "Name") == 0) {
1919 if (!Handle_Channelname(chan, Arg))
1920 Config_Error_TooLong(Line, Var);
1923 if (strcasecmp(Var, "Modes") == 0) {
1925 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1926 if (len >= sizeof(chan->modes))
1927 Config_Error_TooLong( Line, Var );
1930 if( strcasecmp( Var, "Topic" ) == 0 ) {
1932 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1933 if (len >= sizeof(chan->topic))
1934 Config_Error_TooLong( Line, Var );
1937 if( strcasecmp( Var, "Key" ) == 0 ) {
1938 /* Initial Channel Key (mode k) */
1939 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1940 if (len >= sizeof(chan->key))
1941 Config_Error_TooLong(Line, Var);
1944 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1945 /* maximum user limit, mode l */
1946 chan->maxusers = (unsigned long) atol(Arg);
1947 if (!chan->maxusers && strcmp(Arg, "0"))
1948 Config_Error_NaN(Line, Var);
1951 if (strcasecmp(Var, "KeyFile") == 0) {
1953 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1954 if (len >= sizeof(chan->keyfile))
1955 Config_Error_TooLong(Line, Var);
1959 Config_Error_Section(Line, Var, "Channel");
1963 * Validate server configuration.
1965 * Please note that this function uses exit(1) on fatal errors and therefore
1966 * can result in ngIRCd terminating!
1968 * @param Configtest true if the daemon has been called with "--configtest".
1969 * @param Rehash true if re-reading configuration on runtime.
1970 * @returns true if configuration is valid.
1973 Validate_Config(bool Configtest, bool Rehash)
1975 /* Validate configuration settings. */
1978 int i, servers, servers_once;
1980 bool config_valid = true;
1983 /* Emit a warning when the config file is not a full path name */
1984 if (NGIRCd_ConfFile[0] && NGIRCd_ConfFile[0] != '/') {
1985 Config_Error(LOG_WARNING,
1986 "Not specifying a full path name to \"%s\" can cause problems when rehashing the server!",
1990 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1991 ptr = Conf_ServerName;
1993 if (*ptr >= 'a' && *ptr <= 'z') continue;
1994 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1995 if (*ptr >= '0' && *ptr <= '9') continue;
1996 if (ptr > Conf_ServerName) {
1997 if (*ptr == '.' || *ptr == '-')
2000 Conf_ServerName[0] = '\0';
2004 if (!Conf_ServerName[0]) {
2005 /* No server name configured! */
2006 config_valid = false;
2007 Config_Error(LOG_ALERT,
2008 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
2010 if (!Configtest && !Rehash) {
2011 Config_Error(LOG_ALERT,
2012 "%s exiting due to fatal errors!",
2018 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
2019 /* No dot in server name! */
2020 config_valid = false;
2021 Config_Error(LOG_ALERT,
2022 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
2025 Config_Error(LOG_ALERT,
2026 "%s exiting due to fatal errors!",
2033 if (!Conf_ServerAdminMail[0]) {
2034 /* No administrative contact configured! */
2035 config_valid = false;
2036 Config_Error(LOG_ALERT,
2037 "No administrator email address configured in \"%s\" ('AdminEMail')!",
2040 Config_Error(LOG_ALERT,
2041 "%s exiting due to fatal errors!",
2048 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
2049 && !Conf_ServerAdminMail[0]) {
2050 /* No administrative information configured! */
2051 Config_Error(LOG_WARNING,
2052 "No administrative information configured but required by RFC!");
2056 if (Conf_ServerPwd[0])
2057 Config_Error(LOG_ERR,
2058 "This server uses PAM, \"Password\" in [Global] section will be ignored!");
2062 servers = servers_once = 0;
2063 for (i = 0; i < MAX_SERVERS; i++) {
2064 if (Conf_Server[i].name[0]) {
2066 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
2071 "Configuration: Operators=%ld, Servers=%d[%d], Channels=%ld",
2072 array_length(&Conf_Opers, sizeof(struct Conf_Oper)),
2073 servers, servers_once,
2074 array_length(&Conf_Channels, sizeof(struct Conf_Channel)));
2077 return config_valid;
2081 * Output "line too long" warning.
2083 * @param Line Line number in configuration file.
2084 * @param Item Affected variable name.
2087 Config_Error_TooLong ( const int Line, const char *Item )
2089 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
2093 * Output "unknown variable" warning.
2095 * @param Line Line number in configuration file.
2096 * @param Item Affected variable name.
2097 * @param Section Section name.
2100 Config_Error_Section(const int Line, const char *Item, const char *Section)
2102 Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
2103 NGIRCd_ConfFile, Line, Section, Item);
2107 * Output "not a number" warning.
2109 * @param Line Line number in configuration file.
2110 * @param Item Affected variable name.
2113 Config_Error_NaN( const int Line, const char *Item )
2115 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
2116 NGIRCd_ConfFile, Line, Item );
2120 * Output configuration error to console and/or logfile.
2122 * On runtime, the normal log functions of the daemon are used. But when
2123 * testing the configuration ("--configtest"), all messages go directly
2126 * @param Level Severity level of the message.
2127 * @param Format Format string; see printf() function.
2130 static void Config_Error( const int Level, const char *Format, ... )
2132 static void Config_Error( Level, Format, va_alist )
2138 char msg[MAX_LOG_MSG_LEN];
2141 assert( Format != NULL );
2144 va_start( ap, Format );
2148 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
2152 if (Level <= LOG_WARNING)
2153 printf(" - %s\n", msg);
2157 Log(Level, "%s", msg);
2163 * Dump internal state of the "configuration module".
2166 Conf_DebugDump(void)
2170 Log(LOG_DEBUG, "Configured servers:");
2171 for (i = 0; i < MAX_SERVERS; i++) {
2172 if (! Conf_Server[i].name[0])
2175 " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
2176 Conf_Server[i].name, Conf_Server[i].host,
2177 Conf_Server[i].port, Conf_Server[i].lasttry,
2178 Conf_Server[i].group, Conf_Server[i].flags,
2179 Conf_Server[i].conn_id);
2186 * Initialize server configuration structur to default values.
2188 * @param Server Pointer to server structure to initialize.
2191 Init_Server_Struct( CONF_SERVER *Server )
2193 assert( Server != NULL );
2195 memset( Server, 0, sizeof (CONF_SERVER) );
2197 Server->group = NONE;
2198 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
2200 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
2202 Proc_InitStruct(&Server->res_stat);
2203 Server->conn_id = NONE;
2204 memset(&Server->bind_addr, 0, sizeof(Server->bind_addr));