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.72 2005/03/19 18:43:48 fw Exp $";
34 #include <sys/types.h>
53 LOCAL bool Use_Log = true;
54 LOCAL CONF_SERVER New_Server;
55 LOCAL int New_Server_Idx;
58 LOCAL void Set_Defaults PARAMS(( bool InitServers ));
59 LOCAL void Read_Config PARAMS(( void ));
60 LOCAL void Validate_Config PARAMS(( bool TestOnly ));
62 LOCAL void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
63 LOCAL void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
64 LOCAL void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
65 LOCAL void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
67 LOCAL void Config_Error PARAMS(( const int Level, const char *Format, ... ));
69 LOCAL void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
70 LOCAL void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
72 LOCAL void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
80 Validate_Config( false );
87 Set_Defaults( false );
89 Validate_Config( false );
96 /* Read configuration, validate and output it. */
103 Set_Defaults( true );
106 Validate_Config( true );
108 /* If stdin is a valid tty wait for a key: */
109 if( isatty( fileno( stdout )))
111 puts( "OK, press enter to see a dump of your service configuration ..." );
114 else puts( "Ok, dump of your server configuration follows:\n" );
117 printf( " Name = %s\n", Conf_ServerName );
118 printf( " Info = %s\n", Conf_ServerInfo );
119 printf( " Password = %s\n", Conf_ServerPwd );
120 printf( " AdminInfo1 = %s\n", Conf_ServerAdmin1 );
121 printf( " AdminInfo2 = %s\n", Conf_ServerAdmin2 );
122 printf( " AdminEMail = %s\n", Conf_ServerAdminMail );
123 printf( " MotdFile = %s\n", Conf_MotdFile );
124 printf( " MotdPhrase = %s\n", Conf_MotdPhrase );
125 printf( " ChrootDir = %s\n", Conf_Chroot );
126 printf( " PidFile = %s\n", Conf_PidFile);
127 printf( " Ports = " );
128 for( i = 0; i < Conf_ListenPorts_Count; i++ )
130 if( i != 0 ) printf( ", " );
131 printf( "%u", Conf_ListenPorts[i] );
134 printf( " Listen = %s\n", Conf_ListenAddress );
135 pwd = getpwuid( Conf_UID );
136 if( pwd ) printf( " ServerUID = %s\n", pwd->pw_name );
137 else printf( " ServerUID = %ld\n", (long)Conf_UID );
138 grp = getgrgid( Conf_GID );
139 if( grp ) printf( " ServerGID = %s\n", grp->gr_name );
140 else printf( " ServerGID = %ld\n", (long)Conf_GID );
141 printf( " PingTimeout = %d\n", Conf_PingTimeout );
142 printf( " PongTimeout = %d\n", Conf_PongTimeout );
143 printf( " ConnectRetry = %d\n", Conf_ConnectRetry );
144 printf( " OperCanUseMode = %s\n", Conf_OperCanMode == true? "yes" : "no" );
145 printf( " OperServerMode = %s\n", Conf_OperServerMode == true? "yes" : "no" );
146 if( Conf_MaxConnections > 0 ) printf( " MaxConnections = %ld\n", Conf_MaxConnections );
147 else printf( " MaxConnections = -1\n" );
148 if( Conf_MaxConnectionsIP > 0 ) printf( " MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP );
149 else printf( " MaxConnectionsIP = -1\n" );
150 if( Conf_MaxJoins > 0 ) printf( " MaxJoins = %d\n", Conf_MaxJoins );
151 else printf( " MaxJoins = -1\n" );
154 for( i = 0; i < Conf_Oper_Count; i++ )
156 if( ! Conf_Oper[i].name[0] ) continue;
158 /* Valid "Operator" section */
159 puts( "[OPERATOR]" );
160 printf( " Name = %s\n", Conf_Oper[i].name );
161 printf( " Password = %s\n", Conf_Oper[i].pwd );
162 if ( Conf_Oper[i].mask ) printf( " Mask = %s\n", Conf_Oper[i].mask );
166 for( i = 0; i < MAX_SERVERS; i++ )
168 if( ! Conf_Server[i].name[0] ) continue;
170 /* Valid "Server" section */
172 printf( " Name = %s\n", Conf_Server[i].name );
173 printf( " Host = %s\n", Conf_Server[i].host );
174 printf( " Port = %d\n", Conf_Server[i].port );
175 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
176 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
177 printf( " Group = %d\n", Conf_Server[i].group );
181 for( i = 0; i < Conf_Channel_Count; i++ )
183 if( ! Conf_Channel[i].name[0] ) continue;
185 /* Valid "Channel" section */
187 printf( " Name = %s\n", Conf_Channel[i].name );
188 printf( " Modes = %s\n", Conf_Channel[i].modes );
189 printf( " Topic = %s\n", Conf_Channel[i].topic );
198 Conf_UnsetServer( CONN_ID Idx )
200 /* Set next time for next connection attempt, if this is a server
201 * link that is (still) configured here. If the server is set as
202 * "once", delete it from our configuration.
203 * Non-Server-Connections will be silently ignored. */
207 /* Check all our configured servers */
208 for( i = 0; i < MAX_SERVERS; i++ )
210 if( Conf_Server[i].conn_id != Idx ) continue;
212 /* Gotcha! Mark server configuration as "unused": */
213 Conf_Server[i].conn_id = NONE;
215 if( Conf_Server[i].flags & CONF_SFLAG_ONCE )
217 /* Delete configuration here */
218 Init_Server_Struct( &Conf_Server[i] );
222 /* Set time for next connect attempt */
223 if( Conf_Server[i].lasttry < time( NULL ) - Conf_ConnectRetry )
225 /* Okay, the connection was established "long enough": */
226 Conf_Server[i].lasttry = time( NULL ) - Conf_ConnectRetry + RECONNECT_DELAY;
230 } /* Conf_UnsetServer */
234 Conf_SetServer( int ConfServer, CONN_ID Idx )
236 /* Set connection for specified configured server */
238 assert( ConfServer > NONE );
239 assert( Idx > NONE );
241 Conf_Server[ConfServer].conn_id = Idx;
242 } /* Conf_SetServer */
246 Conf_GetServer( CONN_ID Idx )
248 /* Get index of server in configuration structure */
252 assert( Idx > NONE );
254 for( i = 0; i < MAX_SERVERS; i++ )
256 if( Conf_Server[i].conn_id == Idx ) return i;
259 } /* Conf_GetServer */
263 Conf_EnableServer( char *Name, UINT16 Port )
265 /* Enable specified server and adjust port */
269 assert( Name != NULL );
271 for( i = 0; i < MAX_SERVERS; i++ )
273 if( strcasecmp( Conf_Server[i].name, Name ) == 0 )
275 /* Gotcha! Set port and enable server: */
276 Conf_Server[i].port = Port;
277 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
282 } /* Conf_EnableServer */
286 Conf_DisableServer( char *Name )
288 /* Enable specified server and adjust port */
292 assert( Name != NULL );
294 for( i = 0; i < MAX_SERVERS; i++ )
296 if( strcasecmp( Conf_Server[i].name, Name ) == 0 )
298 /* Gotcha! Disable and disconnect server: */
299 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
300 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
305 } /* Conf_DisableServer */
309 Conf_AddServer( char *Name, UINT16 Port, char *Host, char *MyPwd, char *PeerPwd )
311 /* Add new server to configuration */
315 assert( Name != NULL );
316 assert( Host != NULL );
317 assert( MyPwd != NULL );
318 assert( PeerPwd != NULL );
320 /* Search unused item in server configuration structure */
321 for( i = 0; i < MAX_SERVERS; i++ )
323 /* Is this item used? */
324 if( ! Conf_Server[i].name[0] ) break;
326 if( i >= MAX_SERVERS ) return false;
328 Init_Server_Struct( &Conf_Server[i] );
329 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
330 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
331 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
332 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
333 Conf_Server[i].port = Port;
334 Conf_Server[i].flags = CONF_SFLAG_ONCE;
337 } /* Conf_AddServer */
341 Set_Defaults( bool InitServers )
343 /* Initialize configuration variables with default values. */
347 strcpy( Conf_ServerName, "" );
348 sprintf( Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
349 strcpy( Conf_ServerPwd, "" );
351 strcpy( Conf_ServerAdmin1, "" );
352 strcpy( Conf_ServerAdmin2, "" );
353 strcpy( Conf_ServerAdminMail, "" );
355 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
356 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
358 strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
360 strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
362 strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
364 Conf_ListenPorts_Count = 0;
365 strcpy( Conf_ListenAddress, "" );
367 Conf_UID = Conf_GID = 0;
369 Conf_PingTimeout = 120;
370 Conf_PongTimeout = 20;
372 Conf_ConnectRetry = 60;
375 Conf_Channel_Count = 0;
377 Conf_OperCanMode = false;
378 Conf_OperServerMode = false;
380 Conf_MaxConnections = -1;
381 Conf_MaxConnectionsIP = 5;
384 /* Initialize server configuration structures */
385 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
392 /* Read configuration file. */
394 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
398 /* Open configuration file */
399 fd = fopen( NGIRCd_ConfFile, "r" );
402 /* No configuration file found! */
403 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s", NGIRCd_ConfFile, strerror( errno ));
404 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
408 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
410 /* Clean up server configuration structure: mark all already
411 * configured servers as "once" so that they are deleted
412 * after the next disconnect and delete all unused servers.
413 * And delete all servers which are "duplicates" of servers
414 * that are already marked as "once" (such servers have been
415 * created by the last rehash but are now useless). */
416 for( i = 0; i < MAX_SERVERS; i++ )
418 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
421 /* This structure is in use ... */
422 if( Conf_Server[i].flags & CONF_SFLAG_ONCE )
424 /* Check for duplicates */
425 for( n = 0; n < MAX_SERVERS; n++ )
427 if( n == i ) continue;
429 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id )
431 Init_Server_Struct( &Conf_Server[n] );
432 Log( LOG_DEBUG, "Deleted unused duplicate server %d (kept %d).", n, i );
438 /* Mark server as "once" */
439 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
440 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
445 /* Initialize variables */
447 strcpy( section, "" );
448 Init_Server_Struct( &New_Server );
449 New_Server_Idx = NONE;
451 /* Read configuration file */
454 if( ! fgets( str, LINE_LEN, fd )) break;
458 /* Skip comments and empty lines */
459 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
461 /* Is this the beginning of a new section? */
462 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
464 strlcpy( section, str, sizeof( section ));
465 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
466 if( strcasecmp( section, "[OPERATOR]" ) == 0 )
468 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Config_Error( LOG_ERR, "Too many operators configured." );
471 /* Initialize new operator structure */
472 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
473 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
474 if (Conf_Oper[Conf_Oper_Count].mask) {
475 free(Conf_Oper[Conf_Oper_Count].mask );
476 Conf_Oper[Conf_Oper_Count].mask = NULL;
482 if( strcasecmp( section, "[SERVER]" ) == 0 )
484 /* Check if there is already a server to add */
485 if( New_Server.name[0] )
487 /* Copy data to "real" server structure */
488 assert( New_Server_Idx > NONE );
489 Conf_Server[New_Server_Idx] = New_Server;
492 /* Re-init structure for new server */
493 Init_Server_Struct( &New_Server );
495 /* Search unused item in server configuration structure */
496 for( i = 0; i < MAX_SERVERS; i++ )
498 /* Is this item used? */
499 if( ! Conf_Server[i].name[0] ) break;
501 if( i >= MAX_SERVERS )
503 /* Oops, no free item found! */
504 Config_Error( LOG_ERR, "Too many servers configured." );
505 New_Server_Idx = NONE;
507 else New_Server_Idx = i;
510 if( strcasecmp( section, "[CHANNEL]" ) == 0 )
512 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
515 /* Initialize new channel structure */
516 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
517 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
518 strcpy( Conf_Channel[Conf_Channel_Count].topic, "" );
519 Conf_Channel_Count++;
523 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
526 if( section[0] == 0x1 ) continue;
528 /* Split line into variable name and parameters */
529 ptr = strchr( str, '=' );
532 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
536 var = str; ngt_TrimStr( var );
537 arg = ptr + 1; ngt_TrimStr( arg );
539 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
540 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
541 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
542 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
543 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
546 /* Close configuration file */
549 /* Check if there is still a server to add */
550 if( New_Server.name[0] )
552 /* Copy data to "real" server structure */
553 assert( New_Server_Idx > NONE );
554 Conf_Server[New_Server_Idx] = New_Server;
557 /* If there are no ports configured use the default: 6667 */
558 if( Conf_ListenPorts_Count < 1 )
560 Conf_ListenPorts_Count = 1;
561 Conf_ListenPorts[0] = 6667;
567 Check_ArgIsTrue( const char *Arg )
569 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
570 if( strcasecmp( Arg, "true" ) == 0 ) return true;
571 if( atoi( Arg ) != 0 ) return true;
574 } /* Check_ArgIsTrue */
578 Handle_GLOBAL( int Line, char *Var, char *Arg )
586 assert( Var != NULL );
587 assert( Arg != NULL );
589 if( strcasecmp( Var, "Name" ) == 0 )
592 if( strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName )) >= sizeof( Conf_ServerName ))
593 Config_Error_TooLong( Line, Var );
597 if( strcasecmp( Var, "Info" ) == 0 )
599 /* Info text of server */
600 if( strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo )) >= sizeof( Conf_ServerInfo ))
601 Config_Error_TooLong ( Line, Var );
605 if( strcasecmp( Var, "Password" ) == 0 )
607 /* Global server password */
608 if( strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd )) >= sizeof( Conf_ServerPwd ))
609 Config_Error_TooLong( Line, Var );
613 if( strcasecmp( Var, "AdminInfo1" ) == 0 )
615 /* Administrative info #1 */
616 if( strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 )) >= sizeof( Conf_ServerAdmin1 )) Config_Error_TooLong ( Line, Var );
619 if( strcasecmp( Var, "AdminInfo2" ) == 0 )
621 /* Administrative info #2 */
622 if( strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 )) >= sizeof( Conf_ServerAdmin2 )) Config_Error_TooLong ( Line, Var );
625 if( strcasecmp( Var, "AdminEMail" ) == 0 )
627 /* Administrative email contact */
628 if( strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail )) >= sizeof( Conf_ServerAdminMail )) Config_Error_TooLong( Line, Var );
631 if( strcasecmp( Var, "Ports" ) == 0 )
633 /* Ports on that the server should listen. More port numbers
634 * must be separated by "," */
635 ptr = strtok( Arg, "," );
640 if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Config_Error( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
643 if( port > 0 && port < 0xFFFF ) Conf_ListenPorts[Conf_ListenPorts_Count++] = (UINT16)port;
644 else Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
646 ptr = strtok( NULL, "," );
650 if( strcasecmp( Var, "MotdFile" ) == 0 )
652 /* "Message of the day" (MOTD) file */
653 if( strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile )) >= sizeof( Conf_MotdFile ))
654 Config_Error_TooLong( Line, Var );
658 if( strcasecmp( Var, "MotdPhrase" ) == 0 )
660 /* "Message of the day" phrase (instead of file) */
661 if( strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase )) >= sizeof( Conf_MotdPhrase ))
662 Config_Error_TooLong( Line, Var );
666 if( strcasecmp( Var, "ChrootDir" ) == 0 )
668 /* directory for chroot() */
669 if( strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot )) >= sizeof( Conf_Chroot ))
670 Config_Error_TooLong( Line, Var );
675 if ( strcasecmp( Var, "PidFile" ) == 0 )
677 /* name of pidfile */
678 if( strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile )) >= sizeof( Conf_PidFile ))
679 Config_Error_TooLong( Line, Var );
684 if( strcasecmp( Var, "ServerUID" ) == 0 )
686 /* UID the daemon should switch to */
687 pwd = getpwnam( Arg );
688 if( pwd ) Conf_UID = pwd->pw_uid;
692 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
695 Conf_UID = (unsigned int)atoi( Arg );
699 if( strcasecmp( Var, "ServerGID" ) == 0 )
701 /* GID the daemon should use */
702 grp = getgrnam( Arg );
703 if( grp ) Conf_GID = grp->gr_gid;
707 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
710 Conf_GID = (unsigned int)atoi( Arg );
714 if( strcasecmp( Var, "PingTimeout" ) == 0 )
717 Conf_PingTimeout = atoi( Arg );
718 if( Conf_PingTimeout < 5 )
720 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!", NGIRCd_ConfFile, Line );
721 Conf_PingTimeout = 5;
725 if( strcasecmp( Var, "PongTimeout" ) == 0 )
728 Conf_PongTimeout = atoi( Arg );
729 if( Conf_PongTimeout < 5 )
731 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!", NGIRCd_ConfFile, Line );
732 Conf_PongTimeout = 5;
736 if( strcasecmp( Var, "ConnectRetry" ) == 0 )
738 /* Seconds between connection attempts to other servers */
739 Conf_ConnectRetry = atoi( Arg );
740 if( Conf_ConnectRetry < 5 )
742 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!", NGIRCd_ConfFile, Line );
743 Conf_ConnectRetry = 5;
747 if( strcasecmp( Var, "OperCanUseMode" ) == 0 )
749 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
750 Conf_OperCanMode = Check_ArgIsTrue( Arg );
753 if( strcasecmp( Var, "OperServerMode" ) == 0 )
755 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
756 Conf_OperServerMode = Check_ArgIsTrue( Arg );
759 if( strcasecmp( Var, "MaxConnections" ) == 0 )
761 /* Maximum number of connections. Values <= 0 are equal to "no limit". */
763 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
766 Conf_MaxConnections = atol( Arg );
769 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 )
771 /* Maximum number of simoultanous connections from one IP. Values <= 0 are equal to "no limit". */
773 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
776 Conf_MaxConnectionsIP = atoi( Arg );
779 if( strcasecmp( Var, "MaxJoins" ) == 0 )
781 /* Maximum number of channels a user can join. Values <= 0 are equal to "no limit". */
783 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
786 Conf_MaxJoins = atoi( Arg );
789 if( strcasecmp( Var, "Listen" ) == 0 )
791 /* IP-Address to bind sockets */
792 if( strlcpy( Conf_ListenAddress, Arg, sizeof( Conf_ListenAddress )) >= sizeof( Conf_ListenAddress ))
794 Config_Error_TooLong( Line, Var );
799 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
800 } /* Handle_GLOBAL */
804 Handle_OPERATOR( int Line, char *Var, char *Arg )
808 assert( Var != NULL );
809 assert( Arg != NULL );
810 assert( Conf_Oper_Count > 0 );
812 if( strcasecmp( Var, "Name" ) == 0 )
814 /* Name of IRC operator */
815 if( strlcpy( Conf_Oper[Conf_Oper_Count - 1].name, Arg, sizeof( Conf_Oper[Conf_Oper_Count - 1].name )) >= sizeof( Conf_Oper[Conf_Oper_Count - 1].name )) Config_Error_TooLong( Line, Var );
818 if( strcasecmp( Var, "Password" ) == 0 )
820 /* Password of IRC operator */
821 if( strlcpy( Conf_Oper[Conf_Oper_Count - 1].pwd, Arg, sizeof( Conf_Oper[Conf_Oper_Count - 1].pwd )) >= sizeof( Conf_Oper[Conf_Oper_Count - 1].pwd )) Config_Error_TooLong( Line, Var );
824 if( strcasecmp( Var, "Mask" ) == 0 )
826 if (Conf_Oper[Conf_Oper_Count - 1].mask) return; /* Hostname already configured */
827 len = strlen( Arg ) + 1;
828 Conf_Oper[Conf_Oper_Count - 1].mask = malloc( len );
829 if (! Conf_Oper[Conf_Oper_Count - 1].mask) {
830 Config_Error( LOG_ERR, "%s, line %d: Cannot allocate memory for operator mask: %s", NGIRCd_ConfFile, Line, strerror(errno) );
834 strlcpy( Conf_Oper[Conf_Oper_Count - 1].mask, Arg, len);
837 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
838 } /* Handle_OPERATOR */
842 Handle_SERVER( int Line, char *Var, char *Arg )
847 assert( Var != NULL );
848 assert( Arg != NULL );
850 /* Ignore server block if no space is left in server configuration structure */
851 if( New_Server_Idx <= NONE ) return;
853 if( strcasecmp( Var, "Host" ) == 0 )
855 /* Hostname of the server */
856 if( strlcpy( New_Server.host, Arg, sizeof( New_Server.host )) >= sizeof( New_Server.host ))
857 Config_Error_TooLong ( Line, Var );
861 if( strcasecmp( Var, "Name" ) == 0 )
863 /* Name of the server ("Nick"/"ID") */
864 if( strlcpy( New_Server.name, Arg, sizeof( New_Server.name )) >= sizeof( New_Server.name ))
865 Config_Error_TooLong( Line, Var );
868 if( strcasecmp( Var, "MyPassword" ) == 0 )
870 /* Password of this server which is sent to the peer */
871 if( strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in )) >= sizeof( New_Server.pwd_in )) Config_Error_TooLong( Line, Var );
874 if( strcasecmp( Var, "PeerPassword" ) == 0 )
876 /* Passwort of the peer which must be received */
877 if( strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out )) >= sizeof( New_Server.pwd_out )) Config_Error_TooLong( Line, Var );
880 if( strcasecmp( Var, "Port" ) == 0 )
882 /* Port to which this server should connect */
884 if( port > 0 && port < 0xFFFF ) New_Server.port = (UINT16)port;
885 else Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
888 if( strcasecmp( Var, "Group" ) == 0 )
892 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
895 New_Server.group = atoi( Arg );
899 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
900 } /* Handle_SERVER */
904 Handle_CHANNEL( int Line, char *Var, char *Arg )
907 assert( Var != NULL );
908 assert( Arg != NULL );
910 if( strcasecmp( Var, "Name" ) == 0 )
912 /* Name of the channel */
913 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 ))
914 Config_Error_TooLong( Line, Var );
917 if( strcasecmp( Var, "Modes" ) == 0 )
920 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 ))
921 Config_Error_TooLong( Line, Var );
924 if( strcasecmp( Var, "Topic" ) == 0 )
927 if( strlcpy( Conf_Channel[Conf_Channel_Count - 1].topic, Arg, sizeof( Conf_Channel[Conf_Channel_Count - 1].topic )) >= sizeof( Conf_Channel[Conf_Channel_Count - 1].topic ))
928 Config_Error_TooLong( Line, Var );
933 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
934 } /* Handle_CHANNEL */
938 Validate_Config( bool Configtest )
940 /* Validate configuration settings. */
943 int i, servers, servers_once;
946 if( ! Conf_ServerName[0] )
948 /* No server name configured! */
949 Config_Error( LOG_ALERT, "No server name configured in \"%s\" (section 'Global': 'Name')!", NGIRCd_ConfFile );
952 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
957 if( Conf_ServerName[0] && ! strchr( Conf_ServerName, '.' ))
959 /* No dot in server name! */
960 Config_Error( LOG_ALERT, "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!", NGIRCd_ConfFile );
963 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
969 if( ! Conf_ServerAdminMail[0] )
971 /* No administrative contact configured! */
972 Config_Error( LOG_ALERT, "No administrator email address configured in \"%s\" ('AdminEMail')!", NGIRCd_ConfFile );
975 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
981 if( ! Conf_ServerAdmin1[0] && ! Conf_ServerAdmin2[0] && ! Conf_ServerAdminMail[0] )
983 /* No administrative information configured! */
984 Config_Error( LOG_WARNING, "No administrative information configured but required by RFC!" );
987 if(( Conf_MaxConnections > (long)FD_SETSIZE ) || ( Conf_MaxConnections < 1 ))
989 Conf_MaxConnections = (long)FD_SETSIZE;
990 Config_Error( LOG_ERR, "Setting MaxConnections to %ld, select() can't handle more file descriptors!", Conf_MaxConnections );
993 Config_Error( LOG_WARN, "Don't know how many file descriptors select() can handle on this system, don't set MaxConnections too high!" );
997 servers = servers_once = 0;
998 for( i = 0; i < MAX_SERVERS; i++ )
1000 if( Conf_Server[i].name[0] )
1003 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) servers_once++;
1006 Log( LOG_DEBUG, "Configuration: Operators=%d, Servers=%d[%d], Channels=%d", Conf_Oper_Count, servers, servers_once, Conf_Channel_Count );
1008 } /* Validate_Config */
1012 Config_Error_TooLong ( const int Line, const char *Item )
1014 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1018 Config_Error_NaN( const int Line, const char *Item )
1020 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!", NGIRCd_ConfFile, Line, Item );
1024 LOCAL void Config_Error( const int Level, const char *Format, ... )
1026 LOCAL void Config_Error( Level, Format, va_alist )
1032 /* Error! Write to console and/or logfile. */
1034 char msg[MAX_LOG_MSG_LEN];
1037 assert( Format != NULL );
1040 va_start( ap, Format );
1044 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1047 /* During "normal operations" the log functions of the daemon should
1048 * be used, but during testing of the configuration file, all messages
1049 * should go directly to the console: */
1050 if( Use_Log ) Log( Level, "%s", msg );
1052 } /* Config_Error */
1056 Init_Server_Struct( CONF_SERVER *Server )
1058 /* Initialize server configuration structur to default values */
1060 assert( Server != NULL );
1062 memset( Server, 0, sizeof (CONF_SERVER) );
1064 Server->group = NONE;
1065 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1067 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1069 Server->conn_id = NONE;
1070 } /* Init_Server_Struct */