2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 Alexander Barton (alex@barton.de)
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.
11 * Configuration management (reading, parsing & validation)
17 static char UNUSED id[] = "$Id: conf.c,v 1.85 2005/09/02 13:58:52 fw Exp $";
34 #include <sys/types.h>
54 static bool Use_Log = true;
55 static CONF_SERVER New_Server;
56 static int New_Server_Idx;
59 static void Set_Defaults PARAMS(( bool InitServers ));
60 static void Read_Config PARAMS(( void ));
61 static void Validate_Config PARAMS(( bool TestOnly ));
63 static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
64 static void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
65 static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
66 static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
68 static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
70 static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
71 static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
73 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
81 len = array_length(a, sizeof(UINT16));
83 ports = (UINT16*) array_start(a);
84 printf("%u", (unsigned int) *ports);
87 printf(", %u", (unsigned int) *ports);
95 ports_parse(array *a, int Line, char *Arg)
103 /* Ports on that the server should listen. More port numbers
104 * must be separated by "," */
105 ptr = strtok( Arg, "," );
109 if (port > 0 && port < 0xFFFF) {
110 port16 = (UINT16) port;
111 if (!array_catb(a, (char*)&port16, sizeof port16))
112 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
113 NGIRCd_ConfFile, Line, port, strerror(errno));
115 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
116 NGIRCd_ConfFile, Line, port );
119 ptr = strtok( NULL, "," );
127 Set_Defaults( true );
129 Validate_Config( false );
136 Set_Defaults( false );
138 Validate_Config( false );
139 } /* Config_Rehash */
145 /* Read configuration, validate and output it. */
153 Set_Defaults( true );
156 Validate_Config( true );
158 /* If stdin and stdout ("you can read our nice message and we can
159 * read in your keypress") are valid tty's, wait for a key: */
160 if( isatty( fileno( stdin )) && isatty( fileno( stdout )))
162 puts( "OK, press enter to see a dump of your service configuration ..." );
165 else puts( "Ok, dump of your server configuration follows:\n" );
168 printf( " Name = %s\n", Conf_ServerName );
169 printf( " Info = %s\n", Conf_ServerInfo );
170 printf( " Password = %s\n", Conf_ServerPwd );
171 printf( " AdminInfo1 = %s\n", Conf_ServerAdmin1 );
172 printf( " AdminInfo2 = %s\n", Conf_ServerAdmin2 );
173 printf( " AdminEMail = %s\n", Conf_ServerAdminMail );
174 printf( " MotdFile = %s\n", Conf_MotdFile );
175 printf( " MotdPhrase = %s\n", Conf_MotdPhrase );
176 printf( " ChrootDir = %s\n", Conf_Chroot );
177 printf( " PidFile = %s\n", Conf_PidFile);
178 fputs(" Ports = ", stdout);
180 ports_puts(&Conf_ListenPorts);
182 printf( " Listen = %s\n", Conf_ListenAddress );
183 pwd = getpwuid( Conf_UID );
184 if( pwd ) printf( " ServerUID = %s\n", pwd->pw_name );
185 else printf( " ServerUID = %ld\n", (long)Conf_UID );
186 grp = getgrgid( Conf_GID );
187 if( grp ) printf( " ServerGID = %s\n", grp->gr_name );
188 else printf( " ServerGID = %ld\n", (long)Conf_GID );
189 printf( " PingTimeout = %d\n", Conf_PingTimeout );
190 printf( " PongTimeout = %d\n", Conf_PongTimeout );
191 printf( " ConnectRetry = %d\n", Conf_ConnectRetry );
192 printf( " OperCanUseMode = %s\n", Conf_OperCanMode == true? "yes" : "no" );
193 printf( " OperServerMode = %s\n", Conf_OperServerMode == true? "yes" : "no" );
194 if( Conf_MaxConnections > 0 ) printf( " MaxConnections = %ld\n", Conf_MaxConnections );
195 else printf( " MaxConnections = -1\n" );
196 if( Conf_MaxConnectionsIP > 0 ) printf( " MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP );
197 else printf( " MaxConnectionsIP = -1\n" );
198 if( Conf_MaxJoins > 0 ) printf( " MaxJoins = %d\n", Conf_MaxJoins );
199 else printf( " MaxJoins = -1\n" );
202 for( i = 0; i < Conf_Oper_Count; i++ )
204 if( ! Conf_Oper[i].name[0] ) continue;
206 /* Valid "Operator" section */
207 puts( "[OPERATOR]" );
208 printf( " Name = %s\n", Conf_Oper[i].name );
209 printf( " Password = %s\n", Conf_Oper[i].pwd );
210 if ( Conf_Oper[i].mask ) printf( " Mask = %s\n", Conf_Oper[i].mask );
214 for( i = 0; i < MAX_SERVERS; i++ )
216 if( ! Conf_Server[i].name[0] ) continue;
218 /* Valid "Server" section */
220 printf( " Name = %s\n", Conf_Server[i].name );
221 printf( " Host = %s\n", Conf_Server[i].host );
222 printf( " Port = %d\n", Conf_Server[i].port );
223 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
224 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
225 printf( " Group = %d\n", Conf_Server[i].group );
229 for( i = 0; i < Conf_Channel_Count; i++ )
231 if( ! Conf_Channel[i].name[0] ) continue;
233 /* Valid "Channel" section */
235 printf( " Name = %s\n", Conf_Channel[i].name );
236 printf( " Modes = %s\n", Conf_Channel[i].modes );
238 topic = (char*)array_start(&Conf_Channel[i].topic);
239 printf( " Topic = %s\n", topic ? topic : "");
248 Conf_UnsetServer( CONN_ID Idx )
250 /* Set next time for next connection attempt, if this is a server
251 * link that is (still) configured here. If the server is set as
252 * "once", delete it from our configuration.
253 * Non-Server-Connections will be silently ignored. */
257 /* Check all our configured servers */
258 for( i = 0; i < MAX_SERVERS; i++ )
260 if( Conf_Server[i].conn_id != Idx ) continue;
262 /* Gotcha! Mark server configuration as "unused": */
263 Conf_Server[i].conn_id = NONE;
265 if( Conf_Server[i].flags & CONF_SFLAG_ONCE )
267 /* Delete configuration here */
268 Init_Server_Struct( &Conf_Server[i] );
272 /* Set time for next connect attempt */
273 if( Conf_Server[i].lasttry < time( NULL ) - Conf_ConnectRetry )
275 /* Okay, the connection was established "long enough": */
276 Conf_Server[i].lasttry = time( NULL ) - Conf_ConnectRetry + RECONNECT_DELAY;
280 } /* Conf_UnsetServer */
284 Conf_SetServer( int ConfServer, CONN_ID Idx )
286 /* Set connection for specified configured server */
288 assert( ConfServer > NONE );
289 assert( Idx > NONE );
291 Conf_Server[ConfServer].conn_id = Idx;
292 } /* Conf_SetServer */
296 Conf_GetServer( CONN_ID Idx )
298 /* Get index of server in configuration structure */
302 assert( Idx > NONE );
304 for( i = 0; i < MAX_SERVERS; i++ )
306 if( Conf_Server[i].conn_id == Idx ) return i;
309 } /* Conf_GetServer */
313 Conf_EnableServer( char *Name, UINT16 Port )
315 /* Enable specified server and adjust port */
319 assert( Name != NULL );
321 for( i = 0; i < MAX_SERVERS; i++ )
323 if( strcasecmp( Conf_Server[i].name, Name ) == 0 )
325 /* Gotcha! Set port and enable server: */
326 Conf_Server[i].port = Port;
327 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
332 } /* Conf_EnableServer */
336 Conf_DisableServer( char *Name )
338 /* Enable specified server and adjust port */
342 assert( Name != NULL );
344 for( i = 0; i < MAX_SERVERS; i++ )
346 if( strcasecmp( Conf_Server[i].name, Name ) == 0 )
348 /* Gotcha! Disable and disconnect server: */
349 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
350 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
355 } /* Conf_DisableServer */
359 Conf_AddServer( char *Name, UINT16 Port, char *Host, char *MyPwd, char *PeerPwd )
361 /* Add new server to configuration */
365 assert( Name != NULL );
366 assert( Host != NULL );
367 assert( MyPwd != NULL );
368 assert( PeerPwd != NULL );
370 /* Search unused item in server configuration structure */
371 for( i = 0; i < MAX_SERVERS; i++ )
373 /* Is this item used? */
374 if( ! Conf_Server[i].name[0] ) break;
376 if( i >= MAX_SERVERS ) return false;
378 Init_Server_Struct( &Conf_Server[i] );
379 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
380 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
381 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
382 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
383 Conf_Server[i].port = Port;
384 Conf_Server[i].flags = CONF_SFLAG_ONCE;
387 } /* Conf_AddServer */
391 Set_Defaults( bool InitServers )
393 /* Initialize configuration variables with default values. */
397 strcpy( Conf_ServerName, "" );
398 snprintf( Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
399 strcpy( Conf_ServerPwd, "" );
401 strcpy( Conf_ServerAdmin1, "" );
402 strcpy( Conf_ServerAdmin2, "" );
403 strcpy( Conf_ServerAdminMail, "" );
405 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
406 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
408 strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
410 strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
412 strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
414 strcpy( Conf_ListenAddress, "" );
416 Conf_UID = Conf_GID = 0;
418 Conf_PingTimeout = 120;
419 Conf_PongTimeout = 20;
421 Conf_ConnectRetry = 60;
424 Conf_Channel_Count = 0;
426 Conf_OperCanMode = false;
427 Conf_OperServerMode = false;
429 Conf_MaxConnections = -1;
430 Conf_MaxConnectionsIP = 5;
433 /* Initialize server configuration structures */
434 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
441 /* Read configuration file. */
443 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
444 const UINT16 defaultport = 6667;
448 /* Open configuration file */
449 fd = fopen( NGIRCd_ConfFile, "r" );
452 /* No configuration file found! */
453 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s", NGIRCd_ConfFile, strerror( errno ));
454 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
458 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
460 /* Clean up server configuration structure: mark all already
461 * configured servers as "once" so that they are deleted
462 * after the next disconnect and delete all unused servers.
463 * And delete all servers which are "duplicates" of servers
464 * that are already marked as "once" (such servers have been
465 * created by the last rehash but are now useless). */
466 for( i = 0; i < MAX_SERVERS; i++ )
468 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
471 /* This structure is in use ... */
472 if( Conf_Server[i].flags & CONF_SFLAG_ONCE )
474 /* Check for duplicates */
475 for( n = 0; n < MAX_SERVERS; n++ )
477 if( n == i ) continue;
479 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id )
481 Init_Server_Struct( &Conf_Server[n] );
482 Log( LOG_DEBUG, "Deleted unused duplicate server %d (kept %d).", n, i );
488 /* Mark server as "once" */
489 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
490 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
495 /* Initialize variables */
497 strcpy( section, "" );
498 Init_Server_Struct( &New_Server );
499 New_Server_Idx = NONE;
501 /* Read configuration file */
504 if( ! fgets( str, LINE_LEN, fd )) break;
508 /* Skip comments and empty lines */
509 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
511 /* Is this the beginning of a new section? */
512 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
514 strlcpy( section, str, sizeof( section ));
515 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
516 if( strcasecmp( section, "[OPERATOR]" ) == 0 )
518 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Config_Error( LOG_ERR, "Too many operators configured." );
521 /* Initialize new operator structure */
522 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
523 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
524 if (Conf_Oper[Conf_Oper_Count].mask) {
525 free(Conf_Oper[Conf_Oper_Count].mask );
526 Conf_Oper[Conf_Oper_Count].mask = NULL;
532 if( strcasecmp( section, "[SERVER]" ) == 0 )
534 /* Check if there is already a server to add */
535 if( New_Server.name[0] )
537 /* Copy data to "real" server structure */
538 assert( New_Server_Idx > NONE );
539 Conf_Server[New_Server_Idx] = New_Server;
542 /* Re-init structure for new server */
543 Init_Server_Struct( &New_Server );
545 /* Search unused item in server configuration structure */
546 for( i = 0; i < MAX_SERVERS; i++ )
548 /* Is this item used? */
549 if( ! Conf_Server[i].name[0] ) break;
551 if( i >= MAX_SERVERS )
553 /* Oops, no free item found! */
554 Config_Error( LOG_ERR, "Too many servers configured." );
555 New_Server_Idx = NONE;
557 else New_Server_Idx = i;
560 if( strcasecmp( section, "[CHANNEL]" ) == 0 ) {
561 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) {
562 Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
564 /* Initialize new channel structure */
565 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
566 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
567 array_free(&Conf_Channel[Conf_Channel_Count].topic);
568 Conf_Channel_Count++;
572 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
575 if( section[0] == 0x1 ) continue;
577 /* Split line into variable name and parameters */
578 ptr = strchr( str, '=' );
581 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
585 var = str; ngt_TrimStr( var );
586 arg = ptr + 1; ngt_TrimStr( arg );
588 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
589 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
590 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
591 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
592 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
595 /* Close configuration file */
598 /* Check if there is still a server to add */
599 if( New_Server.name[0] )
601 /* Copy data to "real" server structure */
602 assert( New_Server_Idx > NONE );
603 Conf_Server[New_Server_Idx] = New_Server;
606 if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
607 if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
608 Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
609 (unsigned int) defaultport, strerror(errno));
617 Check_ArgIsTrue( const char *Arg )
619 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
620 if( strcasecmp( Arg, "true" ) == 0 ) return true;
621 if( atoi( Arg ) != 0 ) return true;
624 } /* Check_ArgIsTrue */
628 Handle_GLOBAL( int Line, char *Var, char *Arg )
634 assert( Var != NULL );
635 assert( Arg != NULL );
637 if( strcasecmp( Var, "Name" ) == 0 )
640 if( strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName )) >= sizeof( Conf_ServerName ))
641 Config_Error_TooLong( Line, Var );
645 if( strcasecmp( Var, "Info" ) == 0 )
647 /* Info text of server */
648 if( strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo )) >= sizeof( Conf_ServerInfo ))
649 Config_Error_TooLong ( Line, Var );
653 if( strcasecmp( Var, "Password" ) == 0 )
655 /* Global server password */
656 if( strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd )) >= sizeof( Conf_ServerPwd ))
657 Config_Error_TooLong( Line, Var );
661 if( strcasecmp( Var, "AdminInfo1" ) == 0 )
663 /* Administrative info #1 */
664 if( strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 )) >= sizeof( Conf_ServerAdmin1 )) Config_Error_TooLong ( Line, Var );
667 if( strcasecmp( Var, "AdminInfo2" ) == 0 )
669 /* Administrative info #2 */
670 if( strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 )) >= sizeof( Conf_ServerAdmin2 )) Config_Error_TooLong ( Line, Var );
673 if( strcasecmp( Var, "AdminEMail" ) == 0 )
675 /* Administrative email contact */
676 if( strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail )) >= sizeof( Conf_ServerAdminMail )) Config_Error_TooLong( Line, Var );
680 if( strcasecmp( Var, "Ports" ) == 0 ) {
681 ports_parse(&Conf_ListenPorts, Line, Arg);
684 if( strcasecmp( Var, "MotdFile" ) == 0 )
686 /* "Message of the day" (MOTD) file */
687 if( strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile )) >= sizeof( Conf_MotdFile ))
688 Config_Error_TooLong( Line, Var );
692 if( strcasecmp( Var, "MotdPhrase" ) == 0 )
694 /* "Message of the day" phrase (instead of file) */
695 if( strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase )) >= sizeof( Conf_MotdPhrase ))
696 Config_Error_TooLong( Line, Var );
700 if( strcasecmp( Var, "ChrootDir" ) == 0 )
702 /* directory for chroot() */
703 if( strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot )) >= sizeof( Conf_Chroot ))
704 Config_Error_TooLong( Line, Var );
709 if ( strcasecmp( Var, "PidFile" ) == 0 )
711 /* name of pidfile */
712 if( strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile )) >= sizeof( Conf_PidFile ))
713 Config_Error_TooLong( Line, Var );
718 if( strcasecmp( Var, "ServerUID" ) == 0 )
720 /* UID the daemon should switch to */
721 pwd = getpwnam( Arg );
722 if( pwd ) Conf_UID = pwd->pw_uid;
726 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
729 Conf_UID = (unsigned int)atoi( Arg );
733 if( strcasecmp( Var, "ServerGID" ) == 0 )
735 /* GID the daemon should use */
736 grp = getgrnam( Arg );
737 if( grp ) Conf_GID = grp->gr_gid;
741 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
744 Conf_GID = (unsigned int)atoi( Arg );
748 if( strcasecmp( Var, "PingTimeout" ) == 0 )
751 Conf_PingTimeout = atoi( Arg );
752 if( Conf_PingTimeout < 5 )
754 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!", NGIRCd_ConfFile, Line );
755 Conf_PingTimeout = 5;
759 if( strcasecmp( Var, "PongTimeout" ) == 0 )
762 Conf_PongTimeout = atoi( Arg );
763 if( Conf_PongTimeout < 5 )
765 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!", NGIRCd_ConfFile, Line );
766 Conf_PongTimeout = 5;
770 if( strcasecmp( Var, "ConnectRetry" ) == 0 )
772 /* Seconds between connection attempts to other servers */
773 Conf_ConnectRetry = atoi( Arg );
774 if( Conf_ConnectRetry < 5 )
776 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!", NGIRCd_ConfFile, Line );
777 Conf_ConnectRetry = 5;
781 if( strcasecmp( Var, "OperCanUseMode" ) == 0 )
783 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
784 Conf_OperCanMode = Check_ArgIsTrue( Arg );
787 if( strcasecmp( Var, "OperServerMode" ) == 0 )
789 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
790 Conf_OperServerMode = Check_ArgIsTrue( Arg );
793 if( strcasecmp( Var, "MaxConnections" ) == 0 )
795 /* Maximum number of connections. Values <= 0 are equal to "no limit". */
797 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
800 Conf_MaxConnections = atol( Arg );
803 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 )
805 /* Maximum number of simoultanous connections from one IP. Values <= 0 are equal to "no limit". */
807 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
810 Conf_MaxConnectionsIP = atoi( Arg );
813 if( strcasecmp( Var, "MaxJoins" ) == 0 )
815 /* Maximum number of channels a user can join. Values <= 0 are equal to "no limit". */
817 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
820 Conf_MaxJoins = atoi( Arg );
823 if( strcasecmp( Var, "Listen" ) == 0 )
825 /* IP-Address to bind sockets */
826 if( strlcpy( Conf_ListenAddress, Arg, sizeof( Conf_ListenAddress )) >= sizeof( Conf_ListenAddress ))
828 Config_Error_TooLong( Line, Var );
833 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
834 } /* Handle_GLOBAL */
838 Handle_OPERATOR( int Line, char *Var, char *Arg )
840 unsigned int opercount;
843 assert( Var != NULL );
844 assert( Arg != NULL );
845 assert( Conf_Oper_Count > 0 );
847 if ( Conf_Oper_Count == 0 )
850 opercount = Conf_Oper_Count - 1;
852 if( strcasecmp( Var, "Name" ) == 0 ) {
853 /* Name of IRC operator */
854 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
855 if (len >= sizeof( Conf_Oper[opercount].name ))
856 Config_Error_TooLong( Line, Var );
860 if( strcasecmp( Var, "Password" ) == 0 ) {
861 /* Password of IRC operator */
862 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
863 if (len >= sizeof( Conf_Oper[opercount].pwd ))
864 Config_Error_TooLong( Line, Var );
867 if( strcasecmp( Var, "Mask" ) == 0 ) {
868 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
870 Conf_Oper[opercount].mask = strdup( Arg );
871 if (! Conf_Oper[opercount].mask) {
872 Config_Error( LOG_ERR, "%s, line %d: Cannot allocate memory for operator mask: %s",
873 NGIRCd_ConfFile, Line, strerror(errno) );
879 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
880 NGIRCd_ConfFile, Line, Var );
881 } /* Handle_OPERATOR */
885 Handle_SERVER( int Line, char *Var, char *Arg )
891 assert( Var != NULL );
892 assert( Arg != NULL );
894 /* Ignore server block if no space is left in server configuration structure */
895 if( New_Server_Idx <= NONE ) return;
897 if( strcasecmp( Var, "Host" ) == 0 ) {
898 /* Hostname of the server */
899 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
900 if (len >= sizeof( New_Server.host ))
901 Config_Error_TooLong ( Line, Var );
904 if( strcasecmp( Var, "Name" ) == 0 ) {
905 /* Name of the server ("Nick"/"ID") */
906 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
907 if (len >= sizeof( New_Server.name ))
908 Config_Error_TooLong( Line, Var );
911 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
912 /* Password of this server which is sent to the peer */
914 Config_Error(LOG_ERR,
915 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
916 NGIRCd_ConfFile, Line);
918 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
919 if (len >= sizeof( New_Server.pwd_in ))
920 Config_Error_TooLong( Line, Var );
923 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
924 /* Passwort of the peer which must be received */
925 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
926 if (len >= sizeof( New_Server.pwd_out ))
927 Config_Error_TooLong( Line, Var );
930 if( strcasecmp( Var, "Port" ) == 0 ) {
931 /* Port to which this server should connect */
933 if( port > 0 && port < 0xFFFF )
934 New_Server.port = (UINT16)port;
936 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
937 NGIRCd_ConfFile, Line, port );
940 if( strcasecmp( Var, "Group" ) == 0 ) {
943 if( ! isdigit( (int)*Arg ))
944 Config_Error_NaN( Line, Var );
947 New_Server.group = atoi( Arg );
951 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
952 NGIRCd_ConfFile, Line, Var );
953 } /* Handle_SERVER */
957 Handle_CHANNEL( int Line, char *Var, char *Arg )
960 assert( Var != NULL );
961 assert( Arg != NULL );
963 if( strcasecmp( Var, "Name" ) == 0 )
965 /* Name of the channel */
966 if( strlcpy( Conf_Channel[Conf_Channel_Count - 1].name, Arg, sizeof( Conf_Channel[Conf_Channel_Count - 1].name )) >= sizeof( Conf_Channel[Conf_Channel_Count - 1].name ))
967 Config_Error_TooLong( Line, Var );
970 if( strcasecmp( Var, "Modes" ) == 0 )
973 if( strlcpy( Conf_Channel[Conf_Channel_Count - 1].modes, Arg, sizeof( Conf_Channel[Conf_Channel_Count - 1].modes )) >= sizeof( Conf_Channel[Conf_Channel_Count - 1].modes ))
974 Config_Error_TooLong( Line, Var );
977 if( strcasecmp( Var, "Topic" ) == 0 )
980 if (!array_copys( &Conf_Channel[Conf_Channel_Count - 1].topic, Arg))
981 Config_Error_TooLong( Line, Var );
986 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
987 } /* Handle_CHANNEL */
991 Validate_Config( bool Configtest )
993 /* Validate configuration settings. */
996 int i, servers, servers_once;
999 if( ! Conf_ServerName[0] )
1001 /* No server name configured! */
1002 Config_Error( LOG_ALERT, "No server name configured in \"%s\" (section 'Global': 'Name')!", NGIRCd_ConfFile );
1005 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
1010 if( Conf_ServerName[0] && ! strchr( Conf_ServerName, '.' ))
1012 /* No dot in server name! */
1013 Config_Error( LOG_ALERT, "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!", NGIRCd_ConfFile );
1016 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
1022 if( ! Conf_ServerAdminMail[0] )
1024 /* No administrative contact configured! */
1025 Config_Error( LOG_ALERT, "No administrator email address configured in \"%s\" ('AdminEMail')!", NGIRCd_ConfFile );
1028 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
1034 if( ! Conf_ServerAdmin1[0] && ! Conf_ServerAdmin2[0] && ! Conf_ServerAdminMail[0] )
1036 /* No administrative information configured! */
1037 Config_Error( LOG_WARNING, "No administrative information configured but required by RFC!" );
1040 servers = servers_once = 0;
1041 for( i = 0; i < MAX_SERVERS; i++ )
1043 if( Conf_Server[i].name[0] )
1046 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) servers_once++;
1049 Log( LOG_DEBUG, "Configuration: Operators=%d, Servers=%d[%d], Channels=%d", Conf_Oper_Count, servers, servers_once, Conf_Channel_Count );
1051 } /* Validate_Config */
1055 Config_Error_TooLong ( const int Line, const char *Item )
1057 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1061 Config_Error_NaN( const int Line, const char *Item )
1063 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!", NGIRCd_ConfFile, Line, Item );
1067 static void Config_Error( const int Level, const char *Format, ... )
1069 static void Config_Error( Level, Format, va_alist )
1075 /* Error! Write to console and/or logfile. */
1077 char msg[MAX_LOG_MSG_LEN];
1080 assert( Format != NULL );
1083 va_start( ap, Format );
1087 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1090 /* During "normal operations" the log functions of the daemon should
1091 * be used, but during testing of the configuration file, all messages
1092 * should go directly to the console: */
1093 if( Use_Log ) Log( Level, "%s", msg );
1095 } /* Config_Error */
1099 Init_Server_Struct( CONF_SERVER *Server )
1101 /* Initialize server configuration structur to default values */
1103 assert( Server != NULL );
1105 memset( Server, 0, sizeof (CONF_SERVER) );
1107 Server->group = NONE;
1108 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1110 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1112 Server->conn_id = NONE;
1113 } /* Init_Server_Struct */