2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2011 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>
54 static bool Use_Log = true, Using_MotdFile = true;
55 static CONF_SERVER New_Server;
56 static int New_Server_Idx;
58 static size_t Conf_Oper_Count;
59 static size_t Conf_Channel_Count;
60 static char Conf_MotdFile[FNAME_LEN];
62 static void Set_Defaults PARAMS(( bool InitServers ));
63 static bool Read_Config PARAMS(( bool ngircd_starting ));
64 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
66 static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
67 static void Handle_LIMITS PARAMS(( int Line, char *Var, char *Arg ));
68 static void Handle_OPTIONS PARAMS(( int Line, char *Var, char *Arg ));
69 static void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
70 static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
71 static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
73 static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
75 static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
76 static void Config_Error_Section PARAMS(( const int Line, const char *Item,
77 const char *Section ));
78 static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
80 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
84 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
86 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
92 struct SSLOptions Conf_SSLOptions;
95 * Initialize SSL configuration.
100 free(Conf_SSLOptions.KeyFile);
101 Conf_SSLOptions.KeyFile = NULL;
103 free(Conf_SSLOptions.CertFile);
104 Conf_SSLOptions.CertFile = NULL;
106 free(Conf_SSLOptions.DHFile);
107 Conf_SSLOptions.DHFile = NULL;
108 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
112 * Output SSL configuration variable containing a file name.
113 * And make sure that the given file is readable.
115 * @returns true when the file is readable.
118 ssl_print_configvar(const char *name, const char *file)
123 printf(" %s =\n", name);
127 fp = fopen(file, "r");
131 fprintf(stderr, "ERROR: %s \"%s\": %s\n",
132 name, file, strerror(errno));
134 printf(" %s = %s\n", name, file);
139 * Output SSL-related configuration variables.
141 * @returns true when all SSL-related configuration variables are valid.
148 ret = ssl_print_configvar("SSLKeyFile", Conf_SSLOptions.KeyFile);
150 if (!ssl_print_configvar("SSLCertFile", Conf_SSLOptions.CertFile))
153 if (!ssl_print_configvar("SSLDHFile", Conf_SSLOptions.DHFile))
156 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
157 puts(" SSLKeyFilePassword = <secret>");
159 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
168 * Duplicate string and warn on errors.
170 * @returns Pointer to string on success, NULL otherwise.
173 strdup_warn(const char *str)
175 char *ptr = strdup(str);
177 Config_Error(LOG_ERR,
178 "Could not allocate memory for string: %s", str);
183 * Output a comma separated list of ports (integer values).
190 len = array_length(a, sizeof(UINT16));
192 ports = (UINT16*) array_start(a);
193 printf("%u", (unsigned int) *ports);
196 printf(", %u", (unsigned int) *ports);
203 * Parse a comma separated string into an array of port numbers (integers).
206 ports_parse(array *a, int Line, char *Arg)
214 ptr = strtok( Arg, "," );
218 if (port > 0 && port < 0xFFFF) {
219 port16 = (UINT16) port;
220 if (!array_catb(a, (char*)&port16, sizeof port16))
221 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
222 NGIRCd_ConfFile, Line, port, strerror(errno));
224 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
225 NGIRCd_ConfFile, Line, port );
228 ptr = strtok( NULL, "," );
233 * Initialize configuration module.
239 Validate_Config(false, false);
243 * "Rehash" (reload) server configuration.
245 * @returns true if configuration has been re-read, false on errors.
250 if (!Read_Config(false))
252 Validate_Config(false, true);
254 /* Update CLIENT structure of local server */
255 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
260 * Output a boolean value as "yes/no" string.
263 yesno_to_str(int boolean_value)
271 * Free all IRC operator configuration structures.
276 struct Conf_Oper *op;
279 len = array_length(&Conf_Opers, sizeof(*op));
280 op = array_start(&Conf_Opers);
285 array_free(&Conf_Opers);
289 * Output 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 printf(" Name = %s\n", op->name);
304 printf(" Password = %s\n", op->pwd);
305 printf(" Mask = %s\n\n", op->mask ? op->mask : "");
311 * Read configuration, validate and output it.
313 * This function waits for a keypress of the user when stdin/stdout are valid
314 * tty's ("you can read our nice message and we can read in your keypress").
316 * @return 0 on succes, 1 on failure(s); therefore the result code can
317 * directly be used by exit() when running "ngircd --configtest".
326 size_t predef_channel_count;
327 struct Conf_Channel *predef_chan;
331 if (! Read_Config(true))
334 config_valid = Validate_Config(true, false);
337 if( isatty( fileno( stdin )) && isatty( fileno( stdout ))) {
338 puts( "OK, press enter to see a dump of your service configuration ..." );
341 puts( "Ok, dump of your server configuration follows:\n" );
345 printf(" Name = %s\n", Conf_ServerName);
346 printf(" AdminInfo1 = %s\n", Conf_ServerAdmin1);
347 printf(" AdminInfo2 = %s\n", Conf_ServerAdmin2);
348 printf(" AdminEMail = %s\n", Conf_ServerAdminMail);
349 printf(" Info = %s\n", Conf_ServerInfo);
350 printf(" Listen = %s\n", Conf_ListenAddress);
351 if (Using_MotdFile) {
352 printf(" MotdFile = %s\n", Conf_MotdFile);
353 printf(" MotdPhrase =\n");
355 printf(" MotdFile = \n");
356 printf(" MotdPhrase = %s\n", array_bytes(&Conf_Motd)
357 ? (const char*) array_start(&Conf_Motd) : "");
360 printf(" Password = %s\n", Conf_ServerPwd);
362 printf(" PidFile = %s\n", Conf_PidFile);
364 ports_puts(&Conf_ListenPorts);
365 grp = getgrgid(Conf_GID);
367 printf(" ServerGID = %s\n", grp->gr_name);
369 printf(" ServerGID = %ld\n", (long)Conf_GID);
370 pwd = getpwuid(Conf_UID);
372 printf(" ServerUID = %s\n", pwd->pw_name);
374 printf(" ServerUID = %ld\n", (long)Conf_UID);
378 printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
379 printf(" MaxConnections = %ld\n", Conf_MaxConnections);
380 printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
381 printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
382 printf(" MaxNickLength = %u\n", Conf_MaxNickLength - 1);
383 printf(" PingTimeout = %d\n", Conf_PingTimeout);
384 printf(" PongTimeout = %d\n", Conf_PongTimeout);
388 printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
389 printf(" ChrootDir = %s\n", Conf_Chroot);
390 printf(" CloakHost = %s\n", Conf_CloakHost);
391 printf(" CloakUserToNick = %s\n", yesno_to_str(Conf_CloakUserToNick));
393 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
394 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
396 printf(" DNS = %s\n", yesno_to_str(Conf_DNS));
398 printf(" Ident = %s\n", yesno_to_str(Conf_Ident));
400 printf(" NoticeAuth = %s\n", yesno_to_str(Conf_NoticeAuth));
401 printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
402 printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
404 printf(" PAM = %s\n", yesno_to_str(Conf_PAM));
406 printf(" PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
408 printf(" RequireAuthPing = %s\n", yesno_to_str(Conf_AuthPing));
411 printf(" SSLCertFile = %s\n", Conf_SSLOptions.CertFile);
412 printf(" SSLDHFile = %s\n", Conf_SSLOptions.DHFile);
413 printf(" SSLKeyFile = %s\n", Conf_SSLOptions.KeyFile);
414 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
415 puts(" SSLKeyFilePassword = <secret>");
417 puts(" SSLKeyFilePassword = ");
418 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
419 printf(" SSLPorts = ");
420 ports_puts(&Conf_SSLOptions.ListenPorts);
423 printf(" SyslogFacility = %s\n",
424 ngt_SyslogFacilityName(Conf_SyslogFacility));
426 printf(" WebircPassword = %s\n", Conf_WebircPwd);
431 for( i = 0; i < MAX_SERVERS; i++ ) {
432 if( ! Conf_Server[i].name[0] ) continue;
434 /* Valid "Server" section */
436 printf( " Name = %s\n", Conf_Server[i].name );
437 printf( " Host = %s\n", Conf_Server[i].host );
438 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
440 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
442 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
443 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
444 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
445 printf( " Group = %d\n", Conf_Server[i].group );
446 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
449 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
450 predef_chan = array_start(&Conf_Channels);
452 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
453 if (!predef_chan->name[0])
456 /* Valid "Channel" section */
458 printf(" Name = %s\n", predef_chan->name);
459 printf(" Modes = %s\n", predef_chan->modes);
460 printf(" Key = %s\n", predef_chan->key);
461 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
462 printf(" Topic = %s\n", predef_chan->topic);
463 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
466 return (config_valid ? 0 : 1);
470 * Remove connection information from configured server.
472 * If the server is set as "once", delete it from our configuration;
473 * otherwise set the time for the next connection attempt.
475 * Non-server connections will be silently ignored.
478 Conf_UnsetServer( CONN_ID Idx )
483 /* Check all our configured servers */
484 for( i = 0; i < MAX_SERVERS; i++ ) {
485 if( Conf_Server[i].conn_id != Idx ) continue;
487 /* Gotcha! Mark server configuration as "unused": */
488 Conf_Server[i].conn_id = NONE;
490 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
491 /* Delete configuration here */
492 Init_Server_Struct( &Conf_Server[i] );
494 /* Set time for next connect attempt */
496 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
497 /* The connection has been "long", so we don't
498 * require the next attempt to be delayed. */
499 Conf_Server[i].lasttry =
500 t - Conf_ConnectRetry + RECONNECT_DELAY;
502 Conf_Server[i].lasttry = t;
508 * Set connection information for specified configured server.
511 Conf_SetServer( int ConfServer, CONN_ID Idx )
513 assert( ConfServer > NONE );
514 assert( Idx > NONE );
516 Conf_Server[ConfServer].conn_id = Idx;
520 * Get index of server in configuration structure.
523 Conf_GetServer( CONN_ID Idx )
527 assert( Idx > NONE );
529 for( i = 0; i < MAX_SERVERS; i++ ) {
530 if( Conf_Server[i].conn_id == Idx ) return i;
536 * Enable a server by name and adjust its port number.
538 * @returns true if a server has been enabled and now has a valid port
539 * number and host name for outgoing connections.
542 Conf_EnableServer( const char *Name, UINT16 Port )
546 assert( Name != NULL );
547 for( i = 0; i < MAX_SERVERS; i++ ) {
548 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
549 /* Gotcha! Set port and enable server: */
550 Conf_Server[i].port = Port;
551 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
552 return (Conf_Server[i].port && Conf_Server[i].host[0]);
559 * Enable a server by name.
561 * The server is only usable as outgoing server, if it has set a valid port
562 * number for outgoing connections!
563 * If not, you have to use Conf_EnableServer() function to make it available.
565 * @returns true if a server has been enabled; false otherwise.
568 Conf_EnablePassiveServer(const char *Name)
572 assert( Name != NULL );
573 for (i = 0; i < MAX_SERVERS; i++) {
574 if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
575 && (Conf_Server[i].port > 0)) {
576 /* BINGO! Enable server */
577 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
585 * Disable a server by name.
586 * An already established connection will be disconnected.
588 * @returns true if a server was found and has been disabled.
591 Conf_DisableServer( const char *Name )
595 assert( Name != NULL );
596 for( i = 0; i < MAX_SERVERS; i++ ) {
597 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
598 /* Gotcha! Disable and disconnect server: */
599 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
600 if( Conf_Server[i].conn_id > NONE )
601 Conn_Close(Conf_Server[i].conn_id, NULL,
602 "Server link terminated on operator request",
611 * Add a new remote server to our configuration.
613 * @param Name Name of the new server.
614 * @param Port Port number to connect to or 0 for incoming connections.
615 * @param Host Host name to connect to.
616 * @param MyPwd Password that will be sent to the peer.
617 * @param PeerPwd Password that must be received from the peer.
618 * @returns true if the new server has been added; false otherwise.
621 Conf_AddServer(const char *Name, UINT16 Port, const char *Host,
622 const char *MyPwd, const char *PeerPwd)
626 assert( Name != NULL );
627 assert( Host != NULL );
628 assert( MyPwd != NULL );
629 assert( PeerPwd != NULL );
631 /* Search unused item in server configuration structure */
632 for( i = 0; i < MAX_SERVERS; i++ ) {
633 /* Is this item used? */
634 if( ! Conf_Server[i].name[0] ) break;
636 if( i >= MAX_SERVERS ) return false;
638 Init_Server_Struct( &Conf_Server[i] );
639 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
640 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
641 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
642 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
643 Conf_Server[i].port = Port;
644 Conf_Server[i].flags = CONF_SFLAG_ONCE;
650 * Check if the given nick name is an service.
652 * @returns true if the given nick name belongs to an "IRC service".
655 Conf_IsService(int ConfServer, const char *Nick)
657 return MatchCaseInsensitive(Conf_Server[ConfServer].svs_mask, Nick);
661 * Initialize configuration settings with their default values.
664 Set_Defaults(bool InitServers)
669 strcpy(Conf_ServerName, "");
670 strcpy(Conf_ServerAdmin1, "");
671 strcpy(Conf_ServerAdmin2, "");
672 strcpy(Conf_ServerAdminMail, "");
673 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
674 PACKAGE_NAME, PACKAGE_VERSION);
675 free(Conf_ListenAddress);
676 Conf_ListenAddress = NULL;
677 array_free(&Conf_Motd);
678 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
679 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
680 strcpy(Conf_ServerPwd, "");
681 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
682 Conf_UID = Conf_GID = 0;
685 Conf_ConnectRetry = 60;
686 Conf_MaxConnections = 0;
687 Conf_MaxConnectionsIP = 5;
689 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
690 Conf_PingTimeout = 120;
691 Conf_PongTimeout = 20;
694 Conf_AllowRemoteOper = false;
696 Conf_AuthPing = false;
698 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
699 strcpy(Conf_CloakHost, "");
700 Conf_CloakUserToNick = false;
701 Conf_ConnectIPv4 = true;
703 Conf_ConnectIPv6 = true;
705 Conf_ConnectIPv6 = false;
713 Conf_NoticeAuth = false;
714 Conf_OperCanMode = false;
715 Conf_OperServerMode = false;
721 Conf_PredefChannelsOnly = false;
724 Conf_SyslogFacility = LOG_LOCAL5;
726 Conf_SyslogFacility = 0;
730 /* Initialize IRC operators and channels */
732 Conf_Channel_Count = 0;
734 /* Initialize server configuration structures */
736 for (i = 0; i < MAX_SERVERS;
737 Init_Server_Struct(&Conf_Server[i++]));
742 * Get number of configured listening ports.
744 * @returns The number of ports (IPv4+IPv6) on which the server should listen.
749 size_t cnt = array_bytes(&Conf_ListenPorts);
751 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
757 * Read MOTD ("message of the day") file.
759 * @param filename Name of the file to read.
762 Read_Motd(const char *filename)
767 if (*filename == '\0')
770 fp = fopen(filename, "r");
772 Config_Error(LOG_WARNING, "Can't read MOTD file \"%s\": %s",
773 filename, strerror(errno));
777 array_free(&Conf_Motd);
778 Using_MotdFile = true;
780 while (fgets(line, (int)sizeof line, fp)) {
781 ngt_TrimLastChr( line, '\n');
783 /* add text including \0 */
784 if (!array_catb(&Conf_Motd, line, strlen(line) + 1)) {
785 Log(LOG_WARNING, "Cannot add MOTD text: %s", strerror(errno));
793 * Read ngIRCd configuration file.
795 * Please note that this function uses exit(1) on fatal errors and therefore
796 * can result in ngIRCd terminating!
798 * @param ngircd_starting Flag indicating if ngIRCd is starting or not.
799 * @returns true when the configuration file has been read
800 * successfully; false otherwise.
803 Read_Config( bool ngircd_starting )
805 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
806 const UINT16 defaultport = 6667;
810 /* Open configuration file */
811 fd = fopen( NGIRCd_ConfFile, "r" );
813 /* No configuration file found! */
814 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
815 NGIRCd_ConfFile, strerror( errno ));
816 if (!ngircd_starting)
818 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
823 Set_Defaults( ngircd_starting );
825 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
827 /* Clean up server configuration structure: mark all already
828 * configured servers as "once" so that they are deleted
829 * after the next disconnect and delete all unused servers.
830 * And delete all servers which are "duplicates" of servers
831 * that are already marked as "once" (such servers have been
832 * created by the last rehash but are now useless). */
833 for( i = 0; i < MAX_SERVERS; i++ ) {
834 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
836 /* This structure is in use ... */
837 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
838 /* Check for duplicates */
839 for( n = 0; n < MAX_SERVERS; n++ ) {
840 if( n == i ) continue;
842 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
843 Init_Server_Struct( &Conf_Server[n] );
845 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
851 /* Mark server as "once" */
852 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
853 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
858 /* Initialize variables */
860 strcpy( section, "" );
861 Init_Server_Struct( &New_Server );
862 New_Server_Idx = NONE;
866 /* Read configuration file */
868 if( ! fgets( str, LINE_LEN, fd )) break;
872 /* Skip comments and empty lines */
873 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
875 /* Is this the beginning of a new section? */
876 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
877 strlcpy( section, str, sizeof( section ));
878 if (strcasecmp(section, "[GLOBAL]") == 0 ||
879 strcasecmp(section, "[LIMITS]") == 0 ||
880 strcasecmp(section, "[OPTIONS]") == 0)
883 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
884 /* Check if there is already a server to add */
885 if( New_Server.name[0] ) {
886 /* Copy data to "real" server structure */
887 assert( New_Server_Idx > NONE );
888 Conf_Server[New_Server_Idx] = New_Server;
891 /* Re-init structure for new server */
892 Init_Server_Struct( &New_Server );
894 /* Search unused item in server configuration structure */
895 for( i = 0; i < MAX_SERVERS; i++ ) {
896 /* Is this item used? */
897 if( ! Conf_Server[i].name[0] ) break;
899 if( i >= MAX_SERVERS ) {
900 /* Oops, no free item found! */
901 Config_Error( LOG_ERR, "Too many servers configured." );
902 New_Server_Idx = NONE;
904 else New_Server_Idx = i;
907 if (strcasecmp(section, "[CHANNEL]") == 0) {
908 Conf_Channel_Count++;
911 if (strcasecmp(section, "[OPERATOR]") == 0) {
916 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
919 if( section[0] == 0x1 ) continue;
921 /* Split line into variable name and parameters */
922 ptr = strchr( str, '=' );
924 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
928 var = str; ngt_TrimStr( var );
929 arg = ptr + 1; ngt_TrimStr( arg );
931 if(strcasecmp(section, "[GLOBAL]") == 0)
932 Handle_GLOBAL(line, var, arg);
933 else if(strcasecmp(section, "[LIMITS]") == 0)
934 Handle_LIMITS(line, var, arg);
935 else if(strcasecmp(section, "[OPTIONS]") == 0)
936 Handle_OPTIONS(line, var, arg);
937 else if(strcasecmp(section, "[OPERATOR]") == 0)
938 Handle_OPERATOR(line, var, arg);
939 else if(strcasecmp(section, "[SERVER]") == 0)
940 Handle_SERVER(line, var, arg);
941 else if(strcasecmp(section, "[CHANNEL]") == 0)
942 Handle_CHANNEL(line, var, arg);
944 Config_Error(LOG_ERR,
945 "%s, line %d: Variable \"%s\" outside section!",
946 NGIRCd_ConfFile, line, var);
949 /* Close configuration file */
952 /* Check if there is still a server to add */
953 if( New_Server.name[0] ) {
954 /* Copy data to "real" server structure */
955 assert( New_Server_Idx > NONE );
956 Conf_Server[New_Server_Idx] = New_Server;
959 /* not a single listening port? Add default. */
960 if (no_listenports() &&
961 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
963 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
964 (unsigned int) defaultport, strerror(errno));
969 if (!Conf_ListenAddress)
970 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
972 if (!Conf_ListenAddress) {
973 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
977 /* No MOTD phrase configured? (re)try motd file. */
978 if (array_bytes(&Conf_Motd) == 0)
979 Read_Motd(Conf_MotdFile);
984 * Check whether an string argument is true or false.
986 * @param Arg Input string.
987 * @returns true if string has been parsed as "yes"/"true"/"on".
990 Check_ArgIsTrue( const char *Arg )
992 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
993 if( strcasecmp( Arg, "true" ) == 0 ) return true;
994 if( atoi( Arg ) != 0 ) return true;
1000 * Handle setting of "MaxNickLength".
1002 * @param Line Line number in configuration file.
1003 * @raram Arg Input string.
1004 * @returns New configured maximum nick name length.
1007 Handle_MaxNickLength(int Line, const char *Arg)
1011 new = (unsigned) atoi(Arg) + 1;
1012 if (new > CLIENT_NICK_LEN) {
1013 Config_Error(LOG_WARNING,
1014 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
1015 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
1016 return CLIENT_NICK_LEN;
1019 Config_Error(LOG_WARNING,
1020 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
1021 NGIRCd_ConfFile, Line);
1028 * Output a warning messages if IDENT is configured but not compiled in.
1031 WarnIdent(int UNUSED Line)
1035 /* user has enabled ident lookups explicitly, but ... */
1036 Config_Error(LOG_WARNING,
1037 "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
1038 NGIRCd_ConfFile, Line);
1044 * Output a warning messages if IPv6 is configured but not compiled in.
1047 WarnIPv6(int UNUSED Line)
1050 if (Conf_ConnectIPv6) {
1051 /* user has enabled IPv6 explicitly, but ... */
1052 Config_Error(LOG_WARNING,
1053 "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
1054 NGIRCd_ConfFile, Line);
1060 * Output a warning messages if PAM is configured but not compiled in.
1063 WarnPAM(int UNUSED Line)
1067 Config_Error(LOG_WARNING,
1068 "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
1069 NGIRCd_ConfFile, Line);
1075 * Handle legacy "NoXXX" options in [GLOBAL] section.
1077 * TODO: This function and support for "NoXXX" should be removed starting
1078 * with ngIRCd release 19! (One release after marking it "deprecated").
1080 * @param Var Variable name.
1081 * @param Arg Argument string.
1082 * @returns true if a NoXXX option has been processed; false otherwise.
1085 CheckLegacyNoOption(const char *Var, const char *Arg)
1087 if( strcasecmp( Var, "NoDNS" ) == 0 ) {
1088 Conf_DNS = !Check_ArgIsTrue( Arg );
1091 if (strcasecmp(Var, "NoIdent") == 0) {
1092 Conf_Ident = !Check_ArgIsTrue(Arg);
1095 if(strcasecmp(Var, "NoPAM") == 0) {
1096 Conf_PAM = !Check_ArgIsTrue(Arg);
1103 * Handle deprecated legacy options in [GLOBAL] section.
1105 * TODO: This function and support for these options in the [Global] section
1106 * could be removed starting with ngIRCd release 19 (one release after
1107 * marking it "deprecated").
1109 * @param Var Variable name.
1110 * @param Arg Argument string.
1111 * @returns true if a legacy option has been processed; false otherwise.
1114 CheckLegacyGlobalOption(int Line, char *Var, char *Arg)
1116 if (strcasecmp(Var, "AllowRemoteOper") == 0
1117 || strcasecmp(Var, "ChrootDir") == 0
1118 || strcasecmp(Var, "ConnectIPv4") == 0
1119 || strcasecmp(Var, "ConnectIPv6") == 0
1120 || strcasecmp(Var, "OperCanUseMode") == 0
1121 || strcasecmp(Var, "OperServerMode") == 0
1122 || strcasecmp(Var, "PredefChannelsOnly") == 0
1124 || strcasecmp(Var, "SSLCertFile") == 0
1125 || strcasecmp(Var, "SSLDHFile") == 0
1126 || strcasecmp(Var, "SSLKeyFile") == 0
1127 || strcasecmp(Var, "SSLKeyFilePassword") == 0
1128 || strcasecmp(Var, "SSLPorts") == 0
1130 || strcasecmp(Var, "SyslogFacility") == 0
1131 || strcasecmp(Var, "WebircPassword") == 0) {
1132 Handle_OPTIONS(Line, Var, Arg);
1135 if (strcasecmp(Var, "ConnectRetry") == 0
1136 || strcasecmp(Var, "MaxConnections") == 0
1137 || strcasecmp(Var, "MaxConnectionsIP") == 0
1138 || strcasecmp(Var, "MaxJoins") == 0
1139 || strcasecmp(Var, "MaxNickLength") == 0
1140 || strcasecmp(Var, "PingTimeout") == 0
1141 || strcasecmp(Var, "PongTimeout") == 0) {
1142 Handle_LIMITS(Line, Var, Arg);
1150 * Strip "no" prefix of a string.
1152 * TODO: This function and support for "NoXXX" should be removed starting
1153 * with ngIRCd release 19! (One release after marking it "deprecated").
1155 * @param str Pointer to input string starting with "no".
1156 * @returns New pointer to string without "no" prefix.
1159 NoNo(const char *str)
1161 assert(strncasecmp("no", str, 2) == 0 && str[2]);
1166 * Invert "boolean" string.
1168 * TODO: This function and support for "NoXXX" should be removed starting
1169 * with ngIRCd release 19! (One release after marking it "deprecated").
1171 * @param arg "Boolean" input string.
1172 * @returns Pointer to inverted "boolean string".
1175 InvertArg(const char *arg)
1177 return yesno_to_str(!Check_ArgIsTrue(arg));
1181 * Handle variable in [Global] configuration section.
1183 * @param Line Line numer in configuration file.
1184 * @param Var Variable name.
1185 * @param Arg Variable argument.
1188 Handle_GLOBAL( int Line, char *Var, char *Arg )
1193 const char *section;
1196 assert( Var != NULL );
1197 assert( Arg != NULL );
1199 if (strcasecmp(Var, "Name") == 0) {
1200 len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
1201 if (len >= sizeof(Conf_ServerName))
1202 Config_Error_TooLong(Line, Var);
1205 if (strcasecmp(Var, "AdminInfo1") == 0) {
1206 len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
1207 if (len >= sizeof(Conf_ServerAdmin1))
1208 Config_Error_TooLong(Line, Var);
1211 if (strcasecmp(Var, "AdminInfo2") == 0) {
1212 len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
1213 if (len >= sizeof(Conf_ServerAdmin2))
1214 Config_Error_TooLong(Line, Var);
1217 if (strcasecmp(Var, "AdminEMail") == 0) {
1218 len = strlcpy(Conf_ServerAdminMail, Arg,
1219 sizeof(Conf_ServerAdminMail));
1220 if (len >= sizeof(Conf_ServerAdminMail))
1221 Config_Error_TooLong(Line, Var);
1224 if (strcasecmp(Var, "Info") == 0) {
1225 len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
1226 if (len >= sizeof(Conf_ServerInfo))
1227 Config_Error_TooLong(Line, Var);
1230 if (strcasecmp(Var, "Listen") == 0) {
1231 if (Conf_ListenAddress) {
1232 Config_Error(LOG_ERR,
1233 "Multiple Listen= options, ignoring: %s",
1237 Conf_ListenAddress = strdup_warn(Arg);
1238 /* If allocation fails, we're in trouble: we cannot ignore the
1239 * error -- otherwise ngircd would listen on all interfaces. */
1240 if (!Conf_ListenAddress) {
1241 Config_Error(LOG_ALERT,
1242 "%s exiting due to fatal errors!",
1248 if (strcasecmp(Var, "MotdFile") == 0) {
1249 len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
1250 if (len >= sizeof(Conf_MotdFile))
1251 Config_Error_TooLong(Line, Var);
1254 if (strcasecmp(Var, "MotdPhrase") == 0) {
1258 if (len >= LINE_LEN) {
1259 Config_Error_TooLong( Line, Var );
1262 if (!array_copyb(&Conf_Motd, Arg, len + 1))
1263 Config_Error(LOG_WARNING, "%s, line %d: Could not append MotdPhrase: %s",
1264 NGIRCd_ConfFile, Line, strerror(errno));
1265 Using_MotdFile = false;
1268 if(strcasecmp(Var, "Password") == 0) {
1269 len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
1270 if (len >= sizeof(Conf_ServerPwd))
1271 Config_Error_TooLong(Line, Var);
1274 if (strcasecmp(Var, "PidFile") == 0) {
1275 len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
1276 if (len >= sizeof(Conf_PidFile))
1277 Config_Error_TooLong(Line, Var);
1280 if (strcasecmp(Var, "Ports") == 0) {
1281 ports_parse(&Conf_ListenPorts, Line, Arg);
1284 if (strcasecmp(Var, "ServerGID") == 0) {
1285 grp = getgrnam(Arg);
1287 Conf_GID = grp->gr_gid;
1289 Conf_GID = (unsigned int)atoi(Arg);
1290 if (!Conf_GID && strcmp(Arg, "0"))
1291 Config_Error_NaN(Line, Var);
1295 if (strcasecmp(Var, "ServerUID") == 0) {
1296 pwd = getpwnam(Arg);
1298 Conf_UID = pwd->pw_uid;
1300 Conf_UID = (unsigned int)atoi(Arg);
1301 if (!Conf_UID && strcmp(Arg, "0"))
1302 Config_Error_NaN(Line, Var);
1307 if (CheckLegacyNoOption(Var, Arg)) {
1308 Config_Error(LOG_WARNING, "%s, line %d: \"No\"-Prefix has been removed, use \"%s = %s\" in [FEATURES] section instead",
1309 NGIRCd_ConfFile, Line, NoNo(Var), InvertArg(Arg));
1310 if (strcasecmp(Var, "NoIdent") == 0)
1312 else if (strcasecmp(Var, "NoPam") == 0)
1316 if ((section = CheckLegacyGlobalOption(Line, Var, Arg))) {
1317 /** TODO: This function and support for these options in the
1318 * [Global] section could be removed starting with ngIRCd
1319 * release 19 (one release after marking it "deprecated"). */
1320 Config_Error(LOG_WARNING,
1321 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s!",
1322 NGIRCd_ConfFile, Line, Var, section);
1326 Config_Error_Section(Line, Var, "Global");
1330 * Handle variable in [Limits] configuration section.
1332 * @param Line Line numer in configuration file.
1333 * @param Var Variable name.
1334 * @param Arg Variable argument.
1337 Handle_LIMITS(int Line, char *Var, char *Arg)
1340 assert(Var != NULL);
1341 assert(Arg != NULL);
1343 if (strcasecmp(Var, "ConnectRetry") == 0) {
1344 Conf_ConnectRetry = atoi(Arg);
1345 if (Conf_ConnectRetry < 5) {
1346 Config_Error(LOG_WARNING,
1347 "%s, line %d: Value of \"ConnectRetry\" too low!",
1348 NGIRCd_ConfFile, Line);
1349 Conf_ConnectRetry = 5;
1353 if (strcasecmp(Var, "MaxConnections") == 0) {
1354 Conf_MaxConnections = atol(Arg);
1355 if (!Conf_MaxConnections && strcmp(Arg, "0"))
1356 Config_Error_NaN(Line, Var);
1359 if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
1360 Conf_MaxConnectionsIP = atoi(Arg);
1361 if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
1362 Config_Error_NaN(Line, Var);
1365 if (strcasecmp(Var, "MaxJoins") == 0) {
1366 Conf_MaxJoins = atoi(Arg);
1367 if (!Conf_MaxJoins && strcmp(Arg, "0"))
1368 Config_Error_NaN(Line, Var);
1371 if (strcasecmp(Var, "MaxNickLength") == 0) {
1372 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1375 if (strcasecmp(Var, "PingTimeout") == 0) {
1376 Conf_PingTimeout = atoi(Arg);
1377 if (Conf_PingTimeout < 5) {
1378 Config_Error(LOG_WARNING,
1379 "%s, line %d: Value of \"PingTimeout\" too low!",
1380 NGIRCd_ConfFile, Line);
1381 Conf_PingTimeout = 5;
1385 if (strcasecmp(Var, "PongTimeout") == 0) {
1386 Conf_PongTimeout = atoi(Arg);
1387 if (Conf_PongTimeout < 5) {
1388 Config_Error(LOG_WARNING,
1389 "%s, line %d: Value of \"PongTimeout\" too low!",
1390 NGIRCd_ConfFile, Line);
1391 Conf_PongTimeout = 5;
1396 Config_Error_Section(Line, Var, "Limits");
1400 * Handle variable in [Options] configuration section.
1402 * @param Line Line numer in configuration file.
1403 * @param Var Variable name.
1404 * @param Arg Variable argument.
1407 Handle_OPTIONS(int Line, char *Var, char *Arg)
1412 assert(Var != NULL);
1413 assert(Arg != NULL);
1415 if (strcasecmp(Var, "AllowRemoteOper") == 0) {
1416 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1419 if (strcasecmp(Var, "ChrootDir") == 0) {
1420 len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
1421 if (len >= sizeof(Conf_Chroot))
1422 Config_Error_TooLong(Line, Var);
1425 if (strcasecmp(Var, "CloakHost") == 0) {
1426 len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
1427 if (len >= sizeof(Conf_CloakHost))
1428 Config_Error_TooLong(Line, Var);
1431 if (strcasecmp(Var, "CloakUserToNick") == 0) {
1432 Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
1435 if (strcasecmp(Var, "ConnectIPv6") == 0) {
1436 Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
1440 if (strcasecmp(Var, "ConnectIPv4") == 0) {
1441 Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
1444 if (strcasecmp(Var, "DNS") == 0) {
1445 Conf_DNS = Check_ArgIsTrue(Arg);
1448 if (strcasecmp(Var, "Ident") == 0) {
1449 Conf_Ident = Check_ArgIsTrue(Arg);
1453 if (strcasecmp(Var, "NoticeAuth") == 0) {
1454 Conf_NoticeAuth = Check_ArgIsTrue(Arg);
1457 if (strcasecmp(Var, "OperCanUseMode") == 0) {
1458 Conf_OperCanMode = Check_ArgIsTrue(Arg);
1461 if (strcasecmp(Var, "OperServerMode") == 0) {
1462 Conf_OperServerMode = Check_ArgIsTrue(Arg);
1465 if (strcasecmp(Var, "PAM") == 0) {
1466 Conf_PAM = Check_ArgIsTrue(Arg);
1470 if (strcasecmp(Var, "PredefChannelsOnly") == 0) {
1471 Conf_PredefChannelsOnly = Check_ArgIsTrue(Arg);
1475 if (strcasecmp(Var, "RequireAuthPing") == 0) {
1476 Conf_AuthPing = Check_ArgIsTrue(Arg);
1481 if (strcasecmp(Var, "SSLCertFile") == 0) {
1482 assert(Conf_SSLOptions.CertFile == NULL);
1483 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1486 if (strcasecmp(Var, "SSLDHFile") == 0) {
1487 assert(Conf_SSLOptions.DHFile == NULL);
1488 Conf_SSLOptions.DHFile = strdup_warn(Arg);
1491 if (strcasecmp(Var, "SSLKeyFile") == 0) {
1492 assert(Conf_SSLOptions.KeyFile == NULL);
1493 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1496 if (strcasecmp(Var, "SSLKeyFilePassword") == 0) {
1497 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1498 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1499 Config_Error(LOG_ERR,
1500 "%s, line %d (section \"Global\"): Could not copy %s: %s!",
1501 NGIRCd_ConfFile, Line, Var,
1505 if (strcasecmp(Var, "SSLPorts") == 0) {
1506 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1511 if (strcasecmp(Var, "SyslogFacility") == 0) {
1512 Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
1513 Conf_SyslogFacility);
1517 if (strcasecmp(Var, "WebircPassword") == 0) {
1518 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
1519 if (len >= sizeof(Conf_WebircPwd))
1520 Config_Error_TooLong(Line, Var);
1524 Config_Error_Section(Line, Var, "Options");
1528 * Handle variable in [Operator] configuration section.
1530 * @param Line Line numer in configuration file.
1531 * @param Var Variable name.
1532 * @param Arg Variable argument.
1535 Handle_OPERATOR( int Line, char *Var, char *Arg )
1538 struct Conf_Oper *op;
1541 assert( Var != NULL );
1542 assert( Arg != NULL );
1543 assert( Conf_Oper_Count > 0 );
1545 op = array_alloc(&Conf_Opers, sizeof(*op), Conf_Oper_Count - 1);
1547 Config_Error(LOG_ERR, "Could not allocate memory for operator (%d:%s = %s)", Line, Var, Arg);
1551 if (strcasecmp(Var, "Name") == 0) {
1552 /* Name of IRC operator */
1553 len = strlcpy(op->name, Arg, sizeof(op->name));
1554 if (len >= sizeof(op->name))
1555 Config_Error_TooLong(Line, Var);
1558 if (strcasecmp(Var, "Password") == 0) {
1559 /* Password of IRC operator */
1560 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1561 if (len >= sizeof(op->pwd))
1562 Config_Error_TooLong(Line, Var);
1565 if (strcasecmp(Var, "Mask") == 0) {
1567 return; /* Hostname already configured */
1568 op->mask = strdup_warn( Arg );
1572 Config_Error_Section(Line, Var, "Operator");
1576 * Handle variable in [Server] configuration section.
1578 * @param Line Line numer in configuration file.
1579 * @param Var Variable name.
1580 * @param Arg Variable argument.
1583 Handle_SERVER( int Line, char *Var, char *Arg )
1589 assert( Var != NULL );
1590 assert( Arg != NULL );
1592 /* Ignore server block if no space is left in server configuration structure */
1593 if( New_Server_Idx <= NONE ) return;
1595 if( strcasecmp( Var, "Host" ) == 0 ) {
1596 /* Hostname of the server */
1597 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1598 if (len >= sizeof( New_Server.host ))
1599 Config_Error_TooLong ( Line, Var );
1602 if( strcasecmp( Var, "Name" ) == 0 ) {
1603 /* Name of the server ("Nick"/"ID") */
1604 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1605 if (len >= sizeof( New_Server.name ))
1606 Config_Error_TooLong( Line, Var );
1609 if (strcasecmp(Var, "Bind") == 0) {
1610 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1613 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1614 NGIRCd_ConfFile, Line, Arg);
1617 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1618 /* Password of this server which is sent to the peer */
1620 Config_Error(LOG_ERR,
1621 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1622 NGIRCd_ConfFile, Line);
1624 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1625 if (len >= sizeof( New_Server.pwd_in ))
1626 Config_Error_TooLong( Line, Var );
1629 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1630 /* Passwort of the peer which must be received */
1631 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1632 if (len >= sizeof( New_Server.pwd_out ))
1633 Config_Error_TooLong( Line, Var );
1636 if( strcasecmp( Var, "Port" ) == 0 ) {
1637 /* Port to which this server should connect */
1639 if (port >= 0 && port < 0xFFFF)
1640 New_Server.port = (UINT16)port;
1642 Config_Error(LOG_ERR,
1643 "%s, line %d (section \"Server\"): Illegal port number %ld!",
1644 NGIRCd_ConfFile, Line, port );
1648 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1649 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1653 if( strcasecmp( Var, "Group" ) == 0 ) {
1655 New_Server.group = atoi( Arg );
1656 if (!New_Server.group && strcmp(Arg, "0"))
1657 Config_Error_NaN(Line, Var);
1660 if( strcasecmp( Var, "Passive" ) == 0 ) {
1661 if (Check_ArgIsTrue(Arg))
1662 New_Server.flags |= CONF_SFLAG_DISABLED;
1665 if (strcasecmp(Var, "ServiceMask") == 0) {
1666 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1667 sizeof(New_Server.svs_mask));
1668 if (len >= sizeof(New_Server.svs_mask))
1669 Config_Error_TooLong(Line, Var);
1673 Config_Error_Section(Line, Var, "Server");
1677 * Copy channel name into channel structure.
1679 * If the channel name is not valid because of a missing prefix ('#', '&'),
1680 * a default prefix of '#' will be added.
1682 * @param new_chan New already allocated channel structure.
1683 * @param name Name of the new channel.
1684 * @returns true on success, false otherwise.
1687 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1689 size_t size = sizeof(new_chan->name);
1690 char *dest = new_chan->name;
1692 if (!Channel_IsValidName(name)) {
1694 * maybe user forgot to add a '#'.
1695 * This is only here for user convenience.
1701 return size > strlcpy(dest, name, size);
1705 * Handle variable in [Channel] configuration section.
1707 * @param Line Line numer in configuration file.
1708 * @param Var Variable name.
1709 * @param Arg Variable argument.
1712 Handle_CHANNEL(int Line, char *Var, char *Arg)
1716 struct Conf_Channel *chan;
1719 assert( Var != NULL );
1720 assert( Arg != NULL );
1721 assert(Conf_Channel_Count > 0);
1723 chancount = Conf_Channel_Count - 1;
1725 chan = array_alloc(&Conf_Channels, sizeof(*chan), chancount);
1727 Config_Error(LOG_ERR, "Could not allocate memory for predefined channel (%d:%s = %s)", Line, Var, Arg);
1730 if (strcasecmp(Var, "Name") == 0) {
1731 if (!Handle_Channelname(chan, Arg))
1732 Config_Error_TooLong(Line, Var);
1735 if (strcasecmp(Var, "Modes") == 0) {
1737 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1738 if (len >= sizeof(chan->modes))
1739 Config_Error_TooLong( Line, Var );
1742 if( strcasecmp( Var, "Topic" ) == 0 ) {
1744 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1745 if (len >= sizeof(chan->topic))
1746 Config_Error_TooLong( Line, Var );
1749 if( strcasecmp( Var, "Key" ) == 0 ) {
1750 /* Initial Channel Key (mode k) */
1751 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1752 if (len >= sizeof(chan->key))
1753 Config_Error_TooLong(Line, Var);
1756 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1757 /* maximum user limit, mode l */
1758 chan->maxusers = (unsigned long) atol(Arg);
1759 if (!chan->maxusers && strcmp(Arg, "0"))
1760 Config_Error_NaN(Line, Var);
1763 if (strcasecmp(Var, "KeyFile") == 0) {
1765 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1766 if (len >= sizeof(chan->keyfile))
1767 Config_Error_TooLong(Line, Var);
1771 Config_Error_Section(Line, Var, "Channel");
1775 * Validate server configuration.
1777 * Please note that this function uses exit(1) on fatal errors and therefore
1778 * can result in ngIRCd terminating!
1780 * @param Configtest true if the daemon has been called with "--configtest".
1781 * @param Rehash true if re-reading configuration on runtime.
1782 * @returns true if configuration is valid.
1785 Validate_Config(bool Configtest, bool Rehash)
1787 /* Validate configuration settings. */
1790 int i, servers, servers_once;
1792 bool config_valid = true;
1795 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1796 ptr = Conf_ServerName;
1798 if (*ptr >= 'a' && *ptr <= 'z') continue;
1799 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1800 if (*ptr >= '0' && *ptr <= '9') continue;
1801 if (ptr > Conf_ServerName) {
1802 if (*ptr == '.' || *ptr == '-')
1805 Conf_ServerName[0] = '\0';
1809 if (!Conf_ServerName[0]) {
1810 /* No server name configured! */
1811 config_valid = false;
1812 Config_Error(LOG_ALERT,
1813 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1815 if (!Configtest && !Rehash) {
1816 Config_Error(LOG_ALERT,
1817 "%s exiting due to fatal errors!",
1823 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1824 /* No dot in server name! */
1825 config_valid = false;
1826 Config_Error(LOG_ALERT,
1827 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1830 Config_Error(LOG_ALERT,
1831 "%s exiting due to fatal errors!",
1838 if (!Conf_ServerAdminMail[0]) {
1839 /* No administrative contact configured! */
1840 config_valid = false;
1841 Config_Error(LOG_ALERT,
1842 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1845 Config_Error(LOG_ALERT,
1846 "%s exiting due to fatal errors!",
1853 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1854 && !Conf_ServerAdminMail[0]) {
1855 /* No administrative information configured! */
1856 Config_Error(LOG_WARNING,
1857 "No administrative information configured but required by RFC!");
1861 if (Conf_ServerPwd[0])
1862 Config_Error(LOG_ERR,
1863 "This server uses PAM, \"Password\" will be ignored!");
1867 servers = servers_once = 0;
1868 for (i = 0; i < MAX_SERVERS; i++) {
1869 if (Conf_Server[i].name[0]) {
1871 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1876 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1877 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1880 return config_valid;
1884 * Output "line too long" warning.
1886 * @param Line Line number in configuration file.
1887 * @param Item Affected variable name.
1890 Config_Error_TooLong ( const int Line, const char *Item )
1892 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1896 * Output "unknown variable" warning.
1898 * @param Line Line number in configuration file.
1899 * @param Item Affected variable name.
1900 * @param Section Section name.
1903 Config_Error_Section(const int Line, const char *Item, const char *Section)
1905 Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
1906 NGIRCd_ConfFile, Line, Section, Item);
1910 * Output "not a number" warning.
1912 * @param Line Line number in configuration file.
1913 * @param Item Affected variable name.
1916 Config_Error_NaN( const int Line, const char *Item )
1918 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1919 NGIRCd_ConfFile, Line, Item );
1923 * Output configuration error to console and/or logfile.
1925 * On runtime, the normal log functions of the daemon are used. But when
1926 * testing the configuration ("--configtest"), all messages go directly
1929 * @param Level Severity level of the message.
1930 * @param Format Format string; see printf() function.
1933 static void Config_Error( const int Level, const char *Format, ... )
1935 static void Config_Error( Level, Format, va_alist )
1941 char msg[MAX_LOG_MSG_LEN];
1944 assert( Format != NULL );
1947 va_start( ap, Format );
1951 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1954 if (Use_Log) Log( Level, "%s", msg );
1961 * Dump internal state of the "configuration module".
1964 Conf_DebugDump(void)
1968 Log(LOG_DEBUG, "Configured servers:");
1969 for (i = 0; i < MAX_SERVERS; i++) {
1970 if (! Conf_Server[i].name[0])
1973 " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
1974 Conf_Server[i].name, Conf_Server[i].host,
1975 Conf_Server[i].port, Conf_Server[i].lasttry,
1976 Conf_Server[i].group, Conf_Server[i].flags,
1977 Conf_Server[i].conn_id);
1984 * Initialize server configuration structur to default values.
1986 * @param Server Pointer to server structure to initialize.
1989 Init_Server_Struct( CONF_SERVER *Server )
1991 assert( Server != NULL );
1993 memset( Server, 0, sizeof (CONF_SERVER) );
1995 Server->group = NONE;
1996 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1998 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
2000 Proc_InitStruct(&Server->res_stat);
2001 Server->conn_id = NONE;
2002 memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));