Blob


1 /*
2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 Alexander Barton (alex@barton.de)
4 *
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.
10 *
11 * Configuration management (reading, parsing & validation)
12 */
15 #include "portab.h"
17 static char UNUSED id[] = "$Id: conf.c,v 1.53 2002/12/31 16:12:50 alex Exp $";
19 #include "imp.h"
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <pwd.h>
28 #include <grp.h>
29 #include <sys/types.h>
30 #include <unistd.h>
32 #ifdef HAVE_CTYPE_H
33 # include <ctype.h>
34 #endif
36 #include "ngircd.h"
37 #include "conn.h"
38 #include "client.h"
39 #include "defines.h"
40 #include "log.h"
41 #include "resolve.h"
42 #include "tool.h"
44 #include "exp.h"
45 #include "conf.h"
48 LOCAL BOOLEAN Use_Log = TRUE;
49 LOCAL CONF_SERVER New_Server;
50 LOCAL INT New_Server_Idx;
53 LOCAL VOID Set_Defaults PARAMS(( BOOLEAN InitServers ));
54 LOCAL VOID Read_Config PARAMS(( VOID ));
55 LOCAL VOID Validate_Config PARAMS(( BOOLEAN TestOnly ));
57 LOCAL VOID Handle_GLOBAL PARAMS(( INT Line, CHAR *Var, CHAR *Arg ));
58 LOCAL VOID Handle_OPERATOR PARAMS(( INT Line, CHAR *Var, CHAR *Arg ));
59 LOCAL VOID Handle_SERVER PARAMS(( INT Line, CHAR *Var, CHAR *Arg ));
60 LOCAL VOID Handle_CHANNEL PARAMS(( INT Line, CHAR *Var, CHAR *Arg ));
62 LOCAL VOID Config_Error PARAMS(( CONST INT Level, CONST CHAR *Format, ... ));
64 LOCAL VOID Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
67 GLOBAL VOID
68 Conf_Init( VOID )
69 {
70 Set_Defaults( TRUE );
71 Read_Config( );
72 Validate_Config( FALSE );
73 } /* Config_Init */
76 GLOBAL VOID
77 Conf_Rehash( VOID )
78 {
79 Set_Defaults( FALSE );
80 Read_Config( );
81 Validate_Config( FALSE );
82 } /* Config_Rehash */
85 GLOBAL INT
86 Conf_Test( VOID )
87 {
88 /* Read configuration, validate and output it. */
90 struct passwd *pwd;
91 struct group *grp;
92 INT i;
94 Use_Log = FALSE;
95 Set_Defaults( TRUE );
97 Read_Config( );
98 Validate_Config( TRUE );
100 /* If stdin is a valid tty wait for a key: */
101 if( isatty( fileno( stdout )))
103 puts( "OK, press enter to see a dump of your service configuration ..." );
104 getchar( );
106 else puts( "Ok, dump of your server configuration follows:\n" );
108 puts( "[GLOBAL]" );
109 printf( " ServerName = %s\n", Conf_ServerName );
110 printf( " ServerInfo = %s\n", Conf_ServerInfo );
111 printf( " ServerPwd = %s\n", Conf_ServerPwd );
112 printf( " AdminInfo1 = %s\n", Conf_ServerAdmin1 );
113 printf( " AdminInfo2 = %s\n", Conf_ServerAdmin2 );
114 printf( " AdminEMail = %s\n", Conf_ServerAdminMail );
115 printf( " MotdFile = %s\n", Conf_MotdFile );
116 printf( " Ports = " );
117 for( i = 0; i < Conf_ListenPorts_Count; i++ )
119 if( i != 0 ) printf( ", " );
120 printf( "%u", Conf_ListenPorts[i] );
122 puts( "" );
123 pwd = getpwuid( Conf_UID );
124 if( pwd ) printf( " ServerUID = %s\n", pwd->pw_name );
125 else printf( " ServerUID = %ld\n", (LONG)Conf_UID );
126 grp = getgrgid( Conf_GID );
127 if( grp ) printf( " ServerGID = %s\n", grp->gr_name );
128 else printf( " ServerGID = %ld\n", (LONG)Conf_GID );
129 printf( " PingTimeout = %d\n", Conf_PingTimeout );
130 printf( " PongTimeout = %d\n", Conf_PongTimeout );
131 printf( " ConnectRetry = %d\n", Conf_ConnectRetry );
132 printf( " OperCanUseMode = %s\n", Conf_OperCanMode == TRUE ? "yes" : "no" );
133 if( Conf_MaxConnections > 0 ) printf( " MaxConnections = %ld\n", Conf_MaxConnections );
134 else printf( " MaxConnections = -1\n" );
135 if( Conf_MaxJoins > 0 ) printf( " MaxJoins = %d\n", Conf_MaxJoins );
136 else printf( " MaxJoins = -1\n" );
137 puts( "" );
139 for( i = 0; i < Conf_Oper_Count; i++ )
141 if( ! Conf_Oper[i].name[0] ) continue;
143 /* Valid "Operator" section */
144 puts( "[OPERATOR]" );
145 printf( " Name = %s\n", Conf_Oper[i].name );
146 printf( " Password = %s\n", Conf_Oper[i].pwd );
147 puts( "" );
150 for( i = 0; i < MAX_SERVERS; i++ )
152 if( ! Conf_Server[i].name[0] ) continue;
154 /* Valid "Server" section */
155 puts( "[SERVER]" );
156 printf( " Name = %s\n", Conf_Server[i].name );
157 printf( " Host = %s\n", Conf_Server[i].host );
158 printf( " Port = %d\n", Conf_Server[i].port );
159 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
160 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
161 printf( " Group = %d\n", Conf_Server[i].group );
162 puts( "" );
165 for( i = 0; i < Conf_Channel_Count; i++ )
167 if( ! Conf_Channel[i].name[0] ) continue;
169 /* Valid "Channel" section */
170 puts( "[CHANNEL]" );
171 printf( " Name = %s\n", Conf_Channel[i].name );
172 printf( " Modes = %s\n", Conf_Channel[i].modes );
173 printf( " Topic = %s\n", Conf_Channel[i].topic );
174 puts( "" );
177 return 0;
178 } /* Conf_Test */
181 GLOBAL VOID
182 Conf_UnsetServer( CONN_ID Idx )
184 /* Set next time for next connection attempt, if this is a server
185 * link that is (still) configured here. If the server is set as
186 * "once", delete it from our configuration.
187 * Non-Server-Connections will be silently ignored. */
189 INT i;
191 /* Check all our configured servers */
192 for( i = 0; i < MAX_SERVERS; i++ )
194 if( Conf_Server[i].conn_id != Idx ) continue;
196 /* Gotcha! Mark server configuration as "unused": */
197 Conf_Server[i].conn_id = NONE;
199 if( Conf_Server[i].flags & CONF_SFLAG_ONCE )
201 /* Delete configuration here */
202 Init_Server_Struct( &Conf_Server[i] );
204 else
206 /* Set time for next connect attempt */
207 if( Conf_Server[i].lasttry < time( NULL ) - Conf_ConnectRetry )
209 /* Okay, the connection was established "long enough": */
210 Conf_Server[i].lasttry = time( NULL ) - Conf_ConnectRetry + RECONNECT_DELAY;
213 break;
215 } /* Conf_UnsetServer */
218 GLOBAL VOID
219 Conf_SetServer( INT ConfServer, CONN_ID Idx )
221 /* Set connection for specified configured server */
223 assert( ConfServer > NONE );
224 assert( Idx > NONE );
226 Conf_Server[ConfServer].conn_id = Idx;
227 } /* Conf_SetServer */
230 GLOBAL INT
231 Conf_GetServer( CONN_ID Idx )
233 /* Get index of server in configuration structure */
235 INT i;
237 assert( Idx > NONE );
239 for( i = 0; i < MAX_SERVERS; i++ )
241 if( Conf_Server[i].conn_id == Idx ) return i;
243 return NONE;
244 } /* Conf_GetServer */
247 GLOBAL BOOLEAN
248 Conf_EnableServer( CHAR *Name, INT Port )
250 /* Enable specified server and adjust port */
252 INT i;
254 assert( Name != NULL );
256 for( i = 0; i < MAX_SERVERS; i++ )
258 if( strcasecmp( Conf_Server[i].name, Name ) == 0 )
260 /* Gotcha! Set port and enable server: */
261 Conf_Server[i].port = Port;
262 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
263 return TRUE;
266 return FALSE;
267 } /* Conf_EnableServer */
270 GLOBAL BOOLEAN
271 Conf_DisableServer( CHAR *Name )
273 /* Enable specified server and adjust port */
275 INT i;
277 assert( Name != NULL );
279 for( i = 0; i < MAX_SERVERS; i++ )
281 if( strcasecmp( Conf_Server[i].name, Name ) == 0 )
283 /* Gotcha! Disable and disconnect server: */
284 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
285 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", TRUE );
286 return TRUE;
289 return FALSE;
290 } /* Conf_DisableServer */
293 GLOBAL BOOLEAN
294 Conf_AddServer( CHAR *Name, INT Port, CHAR *Host, CHAR *MyPwd, CHAR *PeerPwd )
296 /* Add new server to configuration */
298 INT i;
300 assert( Name != NULL );
301 assert( Host != NULL );
302 assert( MyPwd != NULL );
303 assert( PeerPwd != NULL );
305 /* Search unused item in server configuration structure */
306 for( i = 0; i < MAX_SERVERS; i++ )
308 /* Is this item used? */
309 if( ! Conf_Server[i].name[0] ) break;
311 if( i >= MAX_SERVERS ) return FALSE;
313 Init_Server_Struct( &Conf_Server[i] );
314 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
315 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
316 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
317 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
318 Conf_Server[i].port = Port;
319 Conf_Server[i].flags = CONF_SFLAG_ONCE;
321 return TRUE;
322 } /* Conf_AddServer */
325 LOCAL VOID
326 Set_Defaults( BOOLEAN InitServers )
328 /* Initialize configuration variables with default values. */
330 INT i;
332 strcpy( Conf_ServerName, "" );
333 sprintf( Conf_ServerInfo, "%s %s", PACKAGE, VERSION );
334 strcpy( Conf_ServerPwd, "" );
336 strcpy( Conf_ServerAdmin1, "" );
337 strcpy( Conf_ServerAdmin2, "" );
338 strcpy( Conf_ServerAdminMail, "" );
340 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
341 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
343 Conf_ListenPorts_Count = 0;
345 Conf_UID = Conf_GID = 0;
347 Conf_PingTimeout = 120;
348 Conf_PongTimeout = 20;
350 Conf_ConnectRetry = 60;
352 Conf_Oper_Count = 0;
353 Conf_Channel_Count = 0;
355 Conf_OperCanMode = FALSE;
357 Conf_MaxConnections = -1;
358 Conf_MaxJoins = 10;
360 /* Initialize server configuration structures */
361 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
362 } /* Set_Defaults */
365 LOCAL VOID
366 Read_Config( VOID )
368 /* Read configuration file. */
370 CHAR section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
371 INT line, i;
372 FILE *fd;
374 /* Open configuration file */
375 fd = fopen( NGIRCd_ConfFile, "r" );
376 if( ! fd )
378 /* No configuration file found! */
379 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s", NGIRCd_ConfFile, strerror( errno ));
380 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
381 exit( 1 );
384 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
386 /* Clean up server configuration structure: mark all already
387 * configured servers as "once" so that they are deleted
388 * after the next disconnect and delete all unused servers. */
389 for( i = 0; i < MAX_SERVERS; i++ )
391 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
392 else Conf_Server[i].flags |= CONF_SFLAG_ONCE;
395 /* Initialize variables */
396 line = 0;
397 strcpy( section, "" );
398 Init_Server_Struct( &New_Server );
399 New_Server_Idx = NONE;
401 /* Read configuration file */
402 while( TRUE )
404 if( ! fgets( str, LINE_LEN, fd )) break;
405 ngt_TrimStr( str );
406 line++;
408 /* Skip comments and empty lines */
409 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
411 /* Is this the beginning of a new section? */
412 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
414 strlcpy( section, str, sizeof( section ));
415 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
416 if( strcasecmp( section, "[OPERATOR]" ) == 0 )
418 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Config_Error( LOG_ERR, "Too many operators configured." );
419 else
421 /* Initialize new operator structure */
422 strcpy( Conf_Oper[Conf_Oper_Count].name, "" );
423 strcpy( Conf_Oper[Conf_Oper_Count].pwd, "" );
424 Conf_Oper_Count++;
426 continue;
428 if( strcasecmp( section, "[SERVER]" ) == 0 )
430 /* Check if there is already a server to add */
431 if( New_Server.name[0] )
433 /* Copy data to "real" server structure */
434 assert( New_Server_Idx > NONE );
435 Conf_Server[New_Server_Idx] = New_Server;
438 /* Re-init structure for new server */
439 Init_Server_Struct( &New_Server );
441 /* Search unused item in server configuration structure */
442 for( i = 0; i < MAX_SERVERS; i++ )
444 /* Is this item used? */
445 if( ! Conf_Server[i].name[0] ) break;
447 if( i >= MAX_SERVERS )
449 /* Oops, no free item found! */
450 Config_Error( LOG_ERR, "Too many servers configured." );
451 New_Server_Idx = NONE;
453 else New_Server_Idx = i;
454 continue;
456 if( strcasecmp( section, "[CHANNEL]" ) == 0 )
458 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
459 else
461 /* Initialize new channel structure */
462 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
463 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
464 strcpy( Conf_Channel[Conf_Channel_Count].topic, "" );
465 Conf_Channel_Count++;
467 continue;
469 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
470 section[0] = 0x1;
472 if( section[0] == 0x1 ) continue;
474 /* Split line into variable name and parameters */
475 ptr = strchr( str, '=' );
476 if( ! ptr )
478 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
479 continue;
481 *ptr = '\0';
482 var = str; ngt_TrimStr( var );
483 arg = ptr + 1; ngt_TrimStr( arg );
485 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
486 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
487 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
488 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
489 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
492 /* Close configuration file */
493 fclose( fd );
495 /* Check if there is still a server to add */
496 if( New_Server.name[0] )
498 /* Copy data to "real" server structure */
499 assert( New_Server_Idx > NONE );
500 Conf_Server[New_Server_Idx] = New_Server;
503 /* If there are no ports configured use the default: 6667 */
504 if( Conf_ListenPorts_Count < 1 )
506 Conf_ListenPorts_Count = 1;
507 Conf_ListenPorts[0] = 6667;
509 } /* Read_Config */
512 LOCAL VOID
513 Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
515 struct passwd *pwd;
516 struct group *grp;
517 CHAR *ptr;
518 LONG port;
520 assert( Line > 0 );
521 assert( Var != NULL );
522 assert( Arg != NULL );
524 if( strcasecmp( Var, "Name" ) == 0 )
526 /* Server name */
527 if( strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName )) >= sizeof( Conf_ServerName )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Name\" too long!", NGIRCd_ConfFile, Line );
528 return;
530 if( strcasecmp( Var, "Info" ) == 0 )
532 /* Info text of server */
533 if( strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo )) >= sizeof( Conf_ServerInfo )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Info\" too long!", NGIRCd_ConfFile, Line );
534 return;
536 if( strcasecmp( Var, "Password" ) == 0 )
538 /* Global server password */
539 if( strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd )) >= sizeof( Conf_ServerPwd )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Password\" too long!", NGIRCd_ConfFile, Line );
540 return;
542 if( strcasecmp( Var, "AdminInfo1" ) == 0 )
544 /* Administrative info #1 */
545 if( strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 )) >= sizeof( Conf_ServerAdmin1 )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"AdminInfo1\" too long!", NGIRCd_ConfFile, Line );
546 return;
548 if( strcasecmp( Var, "AdminInfo2" ) == 0 )
550 /* Administrative info #2 */
551 if( strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 )) >= sizeof( Conf_ServerAdmin2 )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"AdminInfo2\" too long!", NGIRCd_ConfFile, Line );
552 return;
554 if( strcasecmp( Var, "AdminEMail" ) == 0 )
556 /* Administrative email contact */
557 if( strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail )) >= sizeof( Conf_ServerAdminMail )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"AdminEMail\" too long!", NGIRCd_ConfFile, Line );
558 return;
560 if( strcasecmp( Var, "Ports" ) == 0 )
562 /* Ports on that the server should listen. More port numbers
563 * must be separated by "," */
564 ptr = strtok( Arg, "," );
565 while( ptr )
567 ngt_TrimStr( ptr );
568 port = atol( ptr );
569 if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Config_Error( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
570 else
572 if( port > 0 && port < 0xFFFF ) Conf_ListenPorts[Conf_ListenPorts_Count++] = (UINT)port;
573 else Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
575 ptr = strtok( NULL, "," );
577 return;
579 if( strcasecmp( Var, "MotdFile" ) == 0 )
581 /* "Message of the day" (MOTD) file */
582 if( strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile )) >= sizeof( Conf_MotdFile )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"MotdFile\" too long!", NGIRCd_ConfFile, Line );
583 return;
585 if( strcasecmp( Var, "ServerUID" ) == 0 )
587 /* UID the daemon should switch to */
588 pwd = getpwnam( Arg );
589 if( pwd ) Conf_UID = pwd->pw_uid;
590 else
592 #ifdef HAVE_ISDIGIT
593 if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"ServerUID\" is not a number!", NGIRCd_ConfFile, Line );
594 else
595 #endif
596 Conf_UID = (UINT)atoi( Arg );
598 return;
600 if( strcasecmp( Var, "ServerGID" ) == 0 )
602 /* GID the daemon should use */
603 grp = getgrnam( Arg );
604 if( grp ) Conf_GID = grp->gr_gid;
605 else
607 #ifdef HAVE_ISDIGIT
608 if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"ServerGID\" is not a number!", NGIRCd_ConfFile, Line );
609 else
610 #endif
611 Conf_GID = (UINT)atoi( Arg );
613 return;
615 if( strcasecmp( Var, "PingTimeout" ) == 0 )
617 /* PING timeout */
618 Conf_PingTimeout = atoi( Arg );
619 if( Conf_PingTimeout < 5 )
621 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!", NGIRCd_ConfFile, Line );
622 Conf_PingTimeout = 5;
624 return;
626 if( strcasecmp( Var, "PongTimeout" ) == 0 )
628 /* PONG timeout */
629 Conf_PongTimeout = atoi( Arg );
630 if( Conf_PongTimeout < 5 )
632 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!", NGIRCd_ConfFile, Line );
633 Conf_PongTimeout = 5;
635 return;
637 if( strcasecmp( Var, "ConnectRetry" ) == 0 )
639 /* Seconds between connection attempts to other servers */
640 Conf_ConnectRetry = atoi( Arg );
641 if( Conf_ConnectRetry < 5 )
643 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!", NGIRCd_ConfFile, Line );
644 Conf_ConnectRetry = 5;
646 return;
648 if( strcasecmp( Var, "OperCanUseMode" ) == 0 )
650 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
651 if( strcasecmp( Arg, "yes" ) == 0 ) Conf_OperCanMode = TRUE;
652 else if( strcasecmp( Arg, "true" ) == 0 ) Conf_OperCanMode = TRUE;
653 else if( atoi( Arg ) != 0 ) Conf_OperCanMode = TRUE;
654 else Conf_OperCanMode = FALSE;
655 return;
657 if( strcasecmp( Var, "MaxConnections" ) == 0 )
659 /* Maximum number of connections. Values <= 0 are equal to "no limit". */
660 #ifdef HAVE_ISDIGIT
661 if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"MaxConnections\" is not a number!", NGIRCd_ConfFile, Line );
662 else
663 #endif
664 Conf_MaxConnections = atol( Arg );
665 return;
667 if( strcasecmp( Var, "MaxJoins" ) == 0 )
669 /* Maximum number of channels a user can join. Values <= 0 are equal to "no limit". */
670 #ifdef HAVE_ISDIGIT
671 if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"MaxJoins\" is not a number!", NGIRCd_ConfFile, Line );
672 else
673 #endif
674 Conf_MaxJoins = atoi( Arg );
675 return;
678 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
679 } /* Handle_GLOBAL */
682 LOCAL VOID
683 Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
685 assert( Line > 0 );
686 assert( Var != NULL );
687 assert( Arg != NULL );
688 assert( Conf_Oper_Count > 0 );
690 if( strcasecmp( Var, "Name" ) == 0 )
692 /* Name of IRC operator */
693 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( LOG_WARNING, "%s, line %d: Value of \"Name\" too long!", NGIRCd_ConfFile, Line );
694 return;
696 if( strcasecmp( Var, "Password" ) == 0 )
698 /* Password of IRC operator */
699 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( LOG_WARNING, "%s, line %d: Value of \"Password\" too long!", NGIRCd_ConfFile, Line );
700 return;
703 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
704 } /* Handle_OPERATOR */
707 LOCAL VOID
708 Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
710 LONG port;
712 assert( Line > 0 );
713 assert( Var != NULL );
714 assert( Arg != NULL );
716 /* Ignore server block if no space is left in server configuration structure */
717 if( New_Server_Idx <= NONE ) return;
719 if( strcasecmp( Var, "Host" ) == 0 )
721 /* Hostname of the server */
722 if( strlcpy( New_Server.host, Arg, sizeof( New_Server.host )) >= sizeof( New_Server.host )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Host\" too long!", NGIRCd_ConfFile, Line );
723 return;
725 if( strcasecmp( Var, "Name" ) == 0 )
727 /* Name of the server ("Nick"/"ID") */
728 if( strlcpy( New_Server.name, Arg, sizeof( New_Server.name )) >= sizeof( New_Server.name )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Name\" too long!", NGIRCd_ConfFile, Line );
729 return;
731 if( strcasecmp( Var, "MyPassword" ) == 0 )
733 /* Password of this server which is sent to the peer */
734 if( strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in )) >= sizeof( New_Server.pwd_in )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"MyPassword\" too long!", NGIRCd_ConfFile, Line );
735 return;
737 if( strcasecmp( Var, "PeerPassword" ) == 0 )
739 /* Passwort of the peer which must be received */
740 if( strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out )) >= sizeof( New_Server.pwd_out )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"PeerPassword\" too long!", NGIRCd_ConfFile, Line );
741 return;
743 if( strcasecmp( Var, "Port" ) == 0 )
745 /* Port to which this server should connect */
746 port = atol( Arg );
747 if( port > 0 && port < 0xFFFF ) New_Server.port = (INT)port;
748 else Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
749 return;
751 if( strcasecmp( Var, "Group" ) == 0 )
753 /* Server group */
754 #ifdef HAVE_ISDIGIT
755 if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Group\" is not a number!", NGIRCd_ConfFile, Line );
756 else
757 #endif
758 New_Server.group = atoi( Arg );
759 return;
762 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
763 } /* Handle_SERVER */
766 LOCAL VOID
767 Handle_CHANNEL( INT Line, CHAR *Var, CHAR *Arg )
769 assert( Line > 0 );
770 assert( Var != NULL );
771 assert( Arg != NULL );
773 if( strcasecmp( Var, "Name" ) == 0 )
775 /* Name of the channel */
776 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 )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Name\" too long!", NGIRCd_ConfFile, Line );
777 return;
779 if( strcasecmp( Var, "Modes" ) == 0 )
781 /* Initial modes */
782 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 )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Modes\" too long!", NGIRCd_ConfFile, Line );
783 return;
785 if( strcasecmp( Var, "Topic" ) == 0 )
787 /* Initial topic */
788 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 )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Topic\" too long!", NGIRCd_ConfFile, Line );
789 return;
792 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
793 } /* Handle_CHANNEL */
796 LOCAL VOID
797 Validate_Config( BOOLEAN Configtest )
799 /* Validate configuration settings. */
801 #ifdef DEBUG
802 INT i, servers, servers_once;
803 #endif
805 if( ! Conf_ServerName[0] )
807 /* No server name configured! */
808 Config_Error( LOG_ALERT, "No server name configured in \"%s\" ('ServerName')!", NGIRCd_ConfFile );
809 if( ! Configtest )
811 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
812 exit( 1 );
816 #ifdef STRICT_RFC
817 if( ! Conf_ServerAdminMail[0] )
819 /* No administrative contact configured! */
820 Config_Error( LOG_ALERT, "No administrator email address configured in \"%s\" ('AdminEMail')!", NGIRCd_ConfFile );
821 if( ! Configtest )
823 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
824 exit( 1 );
827 #endif
829 if( ! Conf_ServerAdmin1[0] && ! Conf_ServerAdmin2[0] && ! Conf_ServerAdminMail[0] )
831 /* No administrative information configured! */
832 Config_Error( LOG_WARNING, "No administrative information configured but required by RFC!" );
834 #ifdef FD_SETSIZE
835 if(( Conf_MaxConnections > (LONG)FD_SETSIZE ) || ( Conf_MaxConnections < 1 ))
837 Conf_MaxConnections = (LONG)FD_SETSIZE;
838 Config_Error( LOG_ERR, "Setting MaxConnections to %ld, select() can't handle more file descriptors!", Conf_MaxConnections );
840 #else
841 Config_Error( LOG_WARN, "Don't know how many file descriptors select() can handle on this system, don't set MaxConnections too high!" );
842 #endif
844 #ifdef DEBUG
845 servers = servers_once = 0;
846 for( i = 0; i < MAX_SERVERS; i++ )
848 if( Conf_Server[i].name[0] )
850 servers++;
851 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) servers_once++;
854 Log( LOG_DEBUG, "Configuration: Operators=%d, Servers=%d[%d], Channels=%d", Conf_Oper_Count, servers, servers_once, Conf_Channel_Count );
855 #endif
856 } /* Validate_Config */
859 #ifdef PROTOTYPES
860 LOCAL VOID Config_Error( CONST INT Level, CONST CHAR *Format, ... )
861 #else
862 LOCAL VOID Config_Error( Level, Format, va_alist )
863 CONST INT Level;
864 CONST CHAR *Format;
865 va_dcl
866 #endif
868 /* Error! Write to console and/or logfile. */
870 CHAR msg[MAX_LOG_MSG_LEN];
871 va_list ap;
873 assert( Format != NULL );
875 #ifdef PROTOTYPES
876 va_start( ap, Format );
877 #else
878 va_start( ap );
879 #endif
880 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
881 va_end( ap );
883 /* During "normal operations" the log functions of the daemon should
884 * be used, but during testing of the configuration file, all messages
885 * should go directly to the console: */
886 if( Use_Log ) Log( Level, "%s", msg );
887 else puts( msg );
888 } /* Config_Error */
891 LOCAL VOID
892 Init_Server_Struct( CONF_SERVER *Server )
894 /* Initialize server configuration structur to default values */
896 assert( Server != NULL );
898 strcpy( Server->host, "" );
899 strcpy( Server->ip, "" );
900 strcpy( Server->name, "" );
901 strcpy( Server->pwd_in, "" );
902 strcpy( Server->pwd_out, "" );
903 Server->port = 0;
904 Server->group = NONE;
905 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
906 Server->res_stat = NULL;
907 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
908 else Server->flags = 0;
909 Server->conn_id = NONE;
910 } /* Init_Server_Struct */
913 /* -eof- */