2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
5 * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6 * der GNU General Public License (GPL), wie von der Free Software Foundation
7 * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8 * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9 * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10 * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
12 * $Id: conf.c,v 1.24 2002/05/21 00:10:16 alex Exp $
14 * conf.h: Konfiguration des ngircd
39 LOCAL BOOLEAN Use_Log = TRUE;
42 LOCAL VOID Set_Defaults( VOID );
43 LOCAL VOID Read_Config( VOID );
44 LOCAL VOID Validate_Config( VOID );
46 GLOBAL VOID Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg );
47 GLOBAL VOID Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg );
48 GLOBAL VOID Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg );
49 GLOBAL VOID Handle_CHANNEL( INT Line, CHAR *Var, CHAR *Arg );
51 LOCAL VOID Config_Error( CONST INT Level, CONST CHAR *Format, ... );
54 GLOBAL VOID Conf_Init( VOID )
62 GLOBAL INT Conf_Test( VOID )
64 /* Konfiguration einlesen, ueberpruefen und ausgeben. */
71 printf( "Using \"%s\" as configuration file ...\n", NGIRCd_ConfFile );
74 /* Wenn stdin ein ein TTY ist: auf Taste warten */
75 if( isatty( fileno( stdout )))
77 puts( "OK, press enter to see a dump of your service configuration ..." );
80 else puts( "Ok, dump of your server configuration follows:\n" );
83 printf( " ServerName = %s\n", Conf_ServerName );
84 printf( " ServerInfo = %s\n", Conf_ServerInfo );
85 printf( " ServerPwd = %s\n", Conf_ServerPwd );
86 printf( " MotdFile = %s\n", Conf_MotdFile );
87 printf( " ListenPorts = " );
88 for( i = 0; i < Conf_ListenPorts_Count; i++ )
90 if( i != 0 ) printf( ", " );
91 printf( "%u", Conf_ListenPorts[i] );
94 printf( " ServerUID = %ld\n", (INT32)Conf_UID );
95 printf( " ServerGID = %ld\n", (INT32)Conf_GID );
96 printf( " PingTimeout = %d\n", Conf_PingTimeout );
97 printf( " PongTimeout = %d\n", Conf_PongTimeout );
98 printf( " ConnectRetry = %d\n", Conf_ConnectRetry );
101 for( i = 0; i < Conf_Oper_Count; i++ )
103 puts( "[OPERATOR]" );
104 printf( " Name = %s\n", Conf_Oper[i].name );
105 printf( " Password = %s\n", Conf_Oper[i].pwd );
109 for( i = 0; i < Conf_Server_Count; i++ )
112 printf( " Name = %s\n", Conf_Server[i].name );
113 printf( " Host = %s\n", Conf_Server[i].host );
114 printf( " Port = %d\n", Conf_Server[i].port );
115 printf( " Password = %s\n", Conf_Server[i].pwd );
116 printf( " Group = %d\n", Conf_Server[i].group );
120 for( i = 0; i < Conf_Channel_Count; i++ )
123 printf( " Name = %s\n", Conf_Channel[i].name );
124 printf( " Modes = %s\n", Conf_Channel[i].modes );
125 printf( " Topic = %s\n", Conf_Channel[i].topic );
133 GLOBAL VOID Conf_Exit( VOID )
139 LOCAL VOID Set_Defaults( VOID )
141 /* Konfigurationsvariablen initialisieren, d.h. auf Default-Werte setzen. */
143 strcpy( Conf_ServerName, "" );
144 strcpy( Conf_ServerInfo, PACKAGE" "VERSION );
145 strcpy( Conf_ServerPwd, "" );
147 strcpy( Conf_MotdFile, MOTD_FILE );
149 Conf_ListenPorts_Count = 0;
151 Conf_UID = Conf_GID = 0;
153 Conf_PingTimeout = 120;
154 Conf_PongTimeout = 20;
156 Conf_ConnectRetry = 60;
159 Conf_Server_Count = 0;
160 Conf_Channel_Count = 0;
164 LOCAL VOID Read_Config( VOID )
166 /* Konfigurationsdatei einlesen. */
168 CHAR section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
172 fd = fopen( NGIRCd_ConfFile, "r" );
175 /* Keine Konfigurationsdatei gefunden */
176 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s", NGIRCd_ConfFile, strerror( errno ));
177 Config_Error( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
182 strcpy( section, "" );
185 if( ! fgets( str, LINE_LEN, fd )) break;
189 /* Kommentarzeilen und leere Zeilen ueberspringen */
190 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
192 /* Anfang eines Abschnittes? */
193 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
195 strcpy( section, str );
196 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
197 if( strcasecmp( section, "[OPERATOR]" ) == 0 )
199 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Config_Error( LOG_ERR, "Too many operators configured." );
202 /* neuen Operator initialisieren */
203 strcpy( Conf_Oper[Conf_Oper_Count].name, "" );
204 strcpy( Conf_Oper[Conf_Oper_Count].pwd, "" );
209 if( strcasecmp( section, "[SERVER]" ) == 0 )
211 if( Conf_Server_Count + 1 > MAX_SERVERS ) Config_Error( LOG_ERR, "Too many servers configured." );
214 /* neuen Server ("Peer") initialisieren */
215 strcpy( Conf_Server[Conf_Server_Count].host, "" );
216 strcpy( Conf_Server[Conf_Server_Count].ip, "" );
217 strcpy( Conf_Server[Conf_Server_Count].name, "" );
218 strcpy( Conf_Server[Conf_Server_Count].pwd, "" );
219 Conf_Server[Conf_Server_Count].port = 0;
220 Conf_Server[Conf_Server_Count].group = -1;
221 Conf_Server[Conf_Server_Count].lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
222 Conf_Server[Conf_Server_Count].res_stat = NULL;
227 if( strcasecmp( section, "[CHANNEL]" ) == 0 )
229 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
232 /* neuen vordefinierten Channel initialisieren */
233 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
234 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
235 strcpy( Conf_Channel[Conf_Channel_Count].topic, "" );
236 Conf_Channel_Count++;
240 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
243 if( section[0] == 0x1 ) continue;
245 /* In Variable und Argument zerlegen */
246 ptr = strchr( str, '=' );
249 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
253 var = str; ngt_TrimStr( var );
254 arg = ptr + 1; ngt_TrimStr( arg );
256 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
257 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
258 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
259 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
260 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
265 /* Wenn kein Port definiert wurde, Port 6667 als Default benutzen */
266 if( Conf_ListenPorts_Count < 1 )
268 Conf_ListenPorts_Count = 1;
269 Conf_ListenPorts[0] = 6667;
274 GLOBAL VOID Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
280 assert( Var != NULL );
281 assert( Arg != NULL );
283 if( strcasecmp( Var, "Name" ) == 0 )
285 /* Der Server-Name */
286 strncpy( Conf_ServerName, Arg, CLIENT_ID_LEN - 1 );
287 Conf_ServerName[CLIENT_ID_LEN - 1] = '\0';
290 if( strcasecmp( Var, "Info" ) == 0 )
292 /* Server-Info-Text */
293 strncpy( Conf_ServerInfo, Arg, CLIENT_INFO_LEN - 1 );
294 Conf_ServerInfo[CLIENT_INFO_LEN - 1] = '\0';
297 if( strcasecmp( Var, "Password" ) == 0 )
299 /* Server-Passwort */
300 strncpy( Conf_ServerPwd, Arg, CLIENT_PASS_LEN - 1 );
301 Conf_ServerPwd[CLIENT_PASS_LEN - 1] = '\0';
304 if( strcasecmp( Var, "Ports" ) == 0 )
306 /* Ports, durch "," getrennt, auf denen der Server
307 * Verbindungen annehmen soll */
308 ptr = strtok( Arg, "," );
313 if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Config_Error( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
316 if( port > 0 && port < 0xFFFF ) Conf_ListenPorts[Conf_ListenPorts_Count++] = (UINT)port;
317 else Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
319 ptr = strtok( NULL, "," );
323 if( strcasecmp( Var, "MotdFile" ) == 0 )
325 /* Datei mit der "message of the day" (MOTD) */
326 strncpy( Conf_MotdFile, Arg, FNAME_LEN - 1 );
327 Conf_MotdFile[FNAME_LEN - 1] = '\0';
330 if( strcasecmp( Var, "ServerUID" ) == 0 )
332 /* UID, mit der der Daemon laufen soll */
333 Conf_UID = (UINT)atoi( Arg );
336 if( strcasecmp( Var, "ServerGID" ) == 0 )
338 /* GID, mit der der Daemon laufen soll */
339 Conf_GID = (UINT)atoi( Arg );
342 if( strcasecmp( Var, "PingTimeout" ) == 0 )
345 Conf_PingTimeout = atoi( Arg );
346 if(( Conf_PingTimeout ) < 5 ) Conf_PingTimeout = 5;
349 if( strcasecmp( Var, "PongTimeout" ) == 0 )
352 Conf_PongTimeout = atoi( Arg );
353 if(( Conf_PongTimeout ) < 5 ) Conf_PongTimeout = 5;
356 if( strcasecmp( Var, "ConnectRetry" ) == 0 )
358 /* Sekunden zwischen Verbindungsversuchen zu anderen Servern */
359 Conf_ConnectRetry = atoi( Arg );
360 if(( Conf_ConnectRetry ) < 5 ) Conf_ConnectRetry = 5;
364 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
365 } /* Handle_GLOBAL */
368 GLOBAL VOID Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
371 assert( Var != NULL );
372 assert( Arg != NULL );
373 assert( Conf_Oper_Count > 0 );
375 if( strcasecmp( Var, "Name" ) == 0 )
377 /* Name des IRC Operator */
378 strncpy( Conf_Oper[Conf_Oper_Count - 1].name, Arg, CLIENT_ID_LEN - 1 );
379 Conf_Oper[Conf_Oper_Count - 1].name[CLIENT_ID_LEN - 1] = '\0';
382 if( strcasecmp( Var, "Password" ) == 0 )
384 /* Passwort des IRC Operator */
385 strncpy( Conf_Oper[Conf_Oper_Count - 1].pwd, Arg, CLIENT_PASS_LEN - 1 );
386 Conf_Oper[Conf_Oper_Count - 1].pwd[CLIENT_PASS_LEN - 1] = '\0';
390 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
391 } /* Handle_OPERATOR */
394 GLOBAL VOID Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
399 assert( Var != NULL );
400 assert( Arg != NULL );
402 if( strcasecmp( Var, "Host" ) == 0 )
404 /* Hostname des Servers */
405 strncpy( Conf_Server[Conf_Server_Count - 1].host, Arg, HOST_LEN - 1 );
406 Conf_Server[Conf_Server_Count - 1].host[HOST_LEN - 1] = '\0';
409 if( strcasecmp( Var, "Name" ) == 0 )
411 /* Name des Servers ("Nick") */
412 strncpy( Conf_Server[Conf_Server_Count - 1].name, Arg, CLIENT_ID_LEN - 1 );
413 Conf_Server[Conf_Server_Count - 1].name[CLIENT_ID_LEN - 1] = '\0';
416 if( strcasecmp( Var, "Password" ) == 0 )
418 /* Passwort des Servers */
419 strncpy( Conf_Server[Conf_Server_Count - 1].pwd, Arg, CLIENT_PASS_LEN - 1 );
420 Conf_Server[Conf_Server_Count - 1].pwd[CLIENT_PASS_LEN - 1] = '\0';
423 if( strcasecmp( Var, "Port" ) == 0 )
425 /* Port, zu dem Verbunden werden soll */
427 if( port > 0 && port < 0xFFFF ) Conf_Server[Conf_Server_Count - 1].port = (INT)port;
428 else Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
431 if( strcasecmp( Var, "Group" ) == 0 )
434 Conf_Server[Conf_Server_Count - 1].group = atoi( Arg );
438 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
439 } /* Handle_SERVER */
442 GLOBAL VOID Handle_CHANNEL( INT Line, CHAR *Var, CHAR *Arg )
445 assert( Var != NULL );
446 assert( Arg != NULL );
448 if( strcasecmp( Var, "Name" ) == 0 )
450 /* Hostname des Servers */
451 strncpy( Conf_Channel[Conf_Channel_Count - 1].name, Arg, CHANNEL_NAME_LEN - 1 );
452 Conf_Channel[Conf_Channel_Count - 1].name[CHANNEL_NAME_LEN - 1] = '\0';
455 if( strcasecmp( Var, "Modes" ) == 0 )
457 /* Name des Servers ("Nick") */
458 strncpy( Conf_Channel[Conf_Channel_Count - 1].modes, Arg, CHANNEL_MODE_LEN - 1 );
459 Conf_Channel[Conf_Channel_Count - 1].modes[CHANNEL_MODE_LEN - 1] = '\0';
462 if( strcasecmp( Var, "Topic" ) == 0 )
464 /* Passwort des Servers */
465 strncpy( Conf_Channel[Conf_Channel_Count - 1].topic, Arg, CHANNEL_TOPIC_LEN - 1 );
466 Conf_Channel[Conf_Channel_Count - 1].topic[CHANNEL_TOPIC_LEN - 1] = '\0';
470 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
471 } /* Handle_CHANNEL */
474 LOCAL VOID Validate_Config( VOID )
476 /* Konfiguration ueberpruefen */
478 if( ! Conf_ServerName[0] )
480 /* Kein Servername konfiguriert */
481 Config_Error( LOG_ALERT, "No server name configured in \"%s\"!", NGIRCd_ConfFile );
482 Config_Error( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
485 } /* Validate_Config */
488 LOCAL VOID Config_Error( CONST INT Level, CONST CHAR *Format, ... )
490 /* Fehler! Auf Console und/oder ins Log schreiben */
492 CHAR msg[MAX_LOG_MSG_LEN];
495 assert( Format != NULL );
497 /* String mit variablen Argumenten zusammenbauen ... */
498 va_start( ap, Format );
499 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
502 /* Im "normalen Betrieb" soll der Log-Mechanismus des ngIRCd verwendet
503 * werden, beim Testen der Konfiguration jedoch nicht, hier sollen alle
504 * Meldungen direkt auf die Konsole ausgegeben werden: */
505 if( Use_Log ) Log( Level, msg );