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.27 2002/05/30 16:52:21 alex Exp $
14 * conf.h: Konfiguration des ngircd
41 LOCAL BOOLEAN Use_Log = TRUE;
44 LOCAL VOID Set_Defaults PARAMS(( VOID ));
45 LOCAL VOID Read_Config PARAMS(( VOID ));
46 LOCAL VOID Validate_Config PARAMS(( VOID ));
48 LOCAL VOID Handle_GLOBAL PARAMS(( INT Line, CHAR *Var, CHAR *Arg ));
49 LOCAL VOID Handle_OPERATOR PARAMS(( INT Line, CHAR *Var, CHAR *Arg ));
50 LOCAL VOID Handle_SERVER PARAMS(( INT Line, CHAR *Var, CHAR *Arg ));
51 LOCAL VOID Handle_CHANNEL PARAMS(( INT Line, CHAR *Var, CHAR *Arg ));
53 LOCAL VOID Config_Error PARAMS(( CONST INT Level, CONST CHAR *Format, ... ));
68 /* Konfiguration einlesen, ueberpruefen und ausgeben. */
75 printf( "Using \"%s\" as configuration file ...\n", NGIRCd_ConfFile );
78 /* Wenn stdin ein ein TTY ist: auf Taste warten */
79 if( isatty( fileno( stdout )))
81 puts( "OK, press enter to see a dump of your service configuration ..." );
84 else puts( "Ok, dump of your server configuration follows:\n" );
87 printf( " ServerName = %s\n", Conf_ServerName );
88 printf( " ServerInfo = %s\n", Conf_ServerInfo );
89 printf( " ServerPwd = %s\n", Conf_ServerPwd );
90 printf( " MotdFile = %s\n", Conf_MotdFile );
91 printf( " ListenPorts = " );
92 for( i = 0; i < Conf_ListenPorts_Count; i++ )
94 if( i != 0 ) printf( ", " );
95 printf( "%u", Conf_ListenPorts[i] );
98 printf( " ServerUID = %ld\n", (INT32)Conf_UID );
99 printf( " ServerGID = %ld\n", (INT32)Conf_GID );
100 printf( " PingTimeout = %d\n", Conf_PingTimeout );
101 printf( " PongTimeout = %d\n", Conf_PongTimeout );
102 printf( " ConnectRetry = %d\n", Conf_ConnectRetry );
105 for( i = 0; i < Conf_Oper_Count; i++ )
107 if( ! Conf_Oper[i].name[0] ) continue;
109 /* gueltiger Operator-Block: ausgeben */
110 puts( "[OPERATOR]" );
111 printf( " Name = %s\n", Conf_Oper[i].name );
112 printf( " Password = %s\n", Conf_Oper[i].pwd );
116 for( i = 0; i < Conf_Server_Count; i++ )
118 if( ! Conf_Server[i].name[0] ) continue;
119 if( ! Conf_Server[i].host[0] ) continue;
121 /* gueltiger Server-Block: ausgeben */
123 printf( " Name = %s\n", Conf_Server[i].name );
124 printf( " Host = %s\n", Conf_Server[i].host );
125 printf( " Port = %d\n", Conf_Server[i].port );
126 printf( " Password = %s\n", Conf_Server[i].pwd );
127 printf( " Group = %d\n", Conf_Server[i].group );
131 for( i = 0; i < Conf_Channel_Count; i++ )
133 if( ! Conf_Channel[i].name[0] ) continue;
135 /* gueltiger Channel-Block: ausgeben */
137 printf( " Name = %s\n", Conf_Channel[i].name );
138 printf( " Modes = %s\n", Conf_Channel[i].modes );
139 printf( " Topic = %s\n", Conf_Channel[i].topic );
150 /* Konfigurationsvariablen initialisieren, d.h. auf Default-Werte setzen. */
152 strcpy( Conf_ServerName, "" );
153 sprintf( Conf_ServerInfo, "%s %s", PACKAGE, VERSION );
154 strcpy( Conf_ServerPwd, "" );
156 strcpy( Conf_MotdFile, MOTD_FILE );
158 Conf_ListenPorts_Count = 0;
160 Conf_UID = Conf_GID = 0;
162 Conf_PingTimeout = 120;
163 Conf_PongTimeout = 20;
165 Conf_ConnectRetry = 60;
168 Conf_Server_Count = 0;
169 Conf_Channel_Count = 0;
176 /* Konfigurationsdatei einlesen. */
178 CHAR section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
182 fd = fopen( NGIRCd_ConfFile, "r" );
185 /* Keine Konfigurationsdatei gefunden */
186 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s", NGIRCd_ConfFile, strerror( errno ));
187 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
192 strcpy( section, "" );
195 if( ! fgets( str, LINE_LEN, fd )) break;
199 /* Kommentarzeilen und leere Zeilen ueberspringen */
200 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
202 /* Anfang eines Abschnittes? */
203 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
205 strcpy( section, str );
206 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
207 if( strcasecmp( section, "[OPERATOR]" ) == 0 )
209 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Config_Error( LOG_ERR, "Too many operators configured." );
212 /* neuen Operator initialisieren */
213 strcpy( Conf_Oper[Conf_Oper_Count].name, "" );
214 strcpy( Conf_Oper[Conf_Oper_Count].pwd, "" );
219 if( strcasecmp( section, "[SERVER]" ) == 0 )
221 if( Conf_Server_Count + 1 > MAX_SERVERS ) Config_Error( LOG_ERR, "Too many servers configured." );
224 /* neuen Server ("Peer") initialisieren */
225 strcpy( Conf_Server[Conf_Server_Count].host, "" );
226 strcpy( Conf_Server[Conf_Server_Count].ip, "" );
227 strcpy( Conf_Server[Conf_Server_Count].name, "" );
228 strcpy( Conf_Server[Conf_Server_Count].pwd, "" );
229 Conf_Server[Conf_Server_Count].port = 0;
230 Conf_Server[Conf_Server_Count].group = -1;
231 Conf_Server[Conf_Server_Count].lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
232 Conf_Server[Conf_Server_Count].res_stat = NULL;
237 if( strcasecmp( section, "[CHANNEL]" ) == 0 )
239 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
242 /* neuen vordefinierten Channel initialisieren */
243 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
244 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
245 strcpy( Conf_Channel[Conf_Channel_Count].topic, "" );
246 Conf_Channel_Count++;
250 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
253 if( section[0] == 0x1 ) continue;
255 /* In Variable und Argument zerlegen */
256 ptr = strchr( str, '=' );
259 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
263 var = str; ngt_TrimStr( var );
264 arg = ptr + 1; ngt_TrimStr( arg );
266 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
267 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
268 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
269 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
270 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
275 /* Wenn kein Port definiert wurde, Port 6667 als Default benutzen */
276 if( Conf_ListenPorts_Count < 1 )
278 Conf_ListenPorts_Count = 1;
279 Conf_ListenPorts[0] = 6667;
285 Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
291 assert( Var != NULL );
292 assert( Arg != NULL );
294 if( strcasecmp( Var, "Name" ) == 0 )
296 /* Der Server-Name */
297 strncpy( Conf_ServerName, Arg, CLIENT_ID_LEN - 1 );
298 Conf_ServerName[CLIENT_ID_LEN - 1] = '\0';
301 if( strcasecmp( Var, "Info" ) == 0 )
303 /* Server-Info-Text */
304 strncpy( Conf_ServerInfo, Arg, CLIENT_INFO_LEN - 1 );
305 Conf_ServerInfo[CLIENT_INFO_LEN - 1] = '\0';
308 if( strcasecmp( Var, "Password" ) == 0 )
310 /* Server-Passwort */
311 strncpy( Conf_ServerPwd, Arg, CLIENT_PASS_LEN - 1 );
312 Conf_ServerPwd[CLIENT_PASS_LEN - 1] = '\0';
315 if( strcasecmp( Var, "Ports" ) == 0 )
317 /* Ports, durch "," getrennt, auf denen der Server
318 * Verbindungen annehmen soll */
319 ptr = strtok( Arg, "," );
324 if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Config_Error( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
327 if( port > 0 && port < 0xFFFF ) Conf_ListenPorts[Conf_ListenPorts_Count++] = (UINT)port;
328 else Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
330 ptr = strtok( NULL, "," );
334 if( strcasecmp( Var, "MotdFile" ) == 0 )
336 /* Datei mit der "message of the day" (MOTD) */
337 strncpy( Conf_MotdFile, Arg, FNAME_LEN - 1 );
338 Conf_MotdFile[FNAME_LEN - 1] = '\0';
341 if( strcasecmp( Var, "ServerUID" ) == 0 )
343 /* UID, mit der der Daemon laufen soll */
344 Conf_UID = (UINT)atoi( Arg );
347 if( strcasecmp( Var, "ServerGID" ) == 0 )
349 /* GID, mit der der Daemon laufen soll */
350 Conf_GID = (UINT)atoi( Arg );
353 if( strcasecmp( Var, "PingTimeout" ) == 0 )
356 Conf_PingTimeout = atoi( Arg );
357 if(( Conf_PingTimeout ) < 5 ) Conf_PingTimeout = 5;
360 if( strcasecmp( Var, "PongTimeout" ) == 0 )
363 Conf_PongTimeout = atoi( Arg );
364 if(( Conf_PongTimeout ) < 5 ) Conf_PongTimeout = 5;
367 if( strcasecmp( Var, "ConnectRetry" ) == 0 )
369 /* Sekunden zwischen Verbindungsversuchen zu anderen Servern */
370 Conf_ConnectRetry = atoi( Arg );
371 if(( Conf_ConnectRetry ) < 5 ) Conf_ConnectRetry = 5;
375 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
376 } /* Handle_GLOBAL */
380 Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
383 assert( Var != NULL );
384 assert( Arg != NULL );
385 assert( Conf_Oper_Count > 0 );
387 if( strcasecmp( Var, "Name" ) == 0 )
389 /* Name des IRC Operator */
390 strncpy( Conf_Oper[Conf_Oper_Count - 1].name, Arg, CLIENT_ID_LEN - 1 );
391 Conf_Oper[Conf_Oper_Count - 1].name[CLIENT_ID_LEN - 1] = '\0';
394 if( strcasecmp( Var, "Password" ) == 0 )
396 /* Passwort des IRC Operator */
397 strncpy( Conf_Oper[Conf_Oper_Count - 1].pwd, Arg, CLIENT_PASS_LEN - 1 );
398 Conf_Oper[Conf_Oper_Count - 1].pwd[CLIENT_PASS_LEN - 1] = '\0';
402 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
403 } /* Handle_OPERATOR */
407 Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
412 assert( Var != NULL );
413 assert( Arg != NULL );
415 if( strcasecmp( Var, "Host" ) == 0 )
417 /* Hostname des Servers */
418 strncpy( Conf_Server[Conf_Server_Count - 1].host, Arg, HOST_LEN - 1 );
419 Conf_Server[Conf_Server_Count - 1].host[HOST_LEN - 1] = '\0';
422 if( strcasecmp( Var, "Name" ) == 0 )
424 /* Name des Servers ("Nick") */
425 strncpy( Conf_Server[Conf_Server_Count - 1].name, Arg, CLIENT_ID_LEN - 1 );
426 Conf_Server[Conf_Server_Count - 1].name[CLIENT_ID_LEN - 1] = '\0';
429 if( strcasecmp( Var, "Password" ) == 0 )
431 /* Passwort des Servers */
432 strncpy( Conf_Server[Conf_Server_Count - 1].pwd, Arg, CLIENT_PASS_LEN - 1 );
433 Conf_Server[Conf_Server_Count - 1].pwd[CLIENT_PASS_LEN - 1] = '\0';
436 if( strcasecmp( Var, "Port" ) == 0 )
438 /* Port, zu dem Verbunden werden soll */
440 if( port > 0 && port < 0xFFFF ) Conf_Server[Conf_Server_Count - 1].port = (INT)port;
441 else Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
444 if( strcasecmp( Var, "Group" ) == 0 )
447 Conf_Server[Conf_Server_Count - 1].group = atoi( Arg );
451 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
452 } /* Handle_SERVER */
456 Handle_CHANNEL( INT Line, CHAR *Var, CHAR *Arg )
459 assert( Var != NULL );
460 assert( Arg != NULL );
462 if( strcasecmp( Var, "Name" ) == 0 )
464 /* Hostname des Servers */
465 strncpy( Conf_Channel[Conf_Channel_Count - 1].name, Arg, CHANNEL_NAME_LEN - 1 );
466 Conf_Channel[Conf_Channel_Count - 1].name[CHANNEL_NAME_LEN - 1] = '\0';
469 if( strcasecmp( Var, "Modes" ) == 0 )
471 /* Name des Servers ("Nick") */
472 strncpy( Conf_Channel[Conf_Channel_Count - 1].modes, Arg, CHANNEL_MODE_LEN - 1 );
473 Conf_Channel[Conf_Channel_Count - 1].modes[CHANNEL_MODE_LEN - 1] = '\0';
476 if( strcasecmp( Var, "Topic" ) == 0 )
478 /* Passwort des Servers */
479 strncpy( Conf_Channel[Conf_Channel_Count - 1].topic, Arg, CHANNEL_TOPIC_LEN - 1 );
480 Conf_Channel[Conf_Channel_Count - 1].topic[CHANNEL_TOPIC_LEN - 1] = '\0';
484 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
485 } /* Handle_CHANNEL */
489 Validate_Config( VOID )
491 /* Konfiguration ueberpruefen */
493 if( ! Conf_ServerName[0] )
495 /* Kein Servername konfiguriert */
496 Config_Error( LOG_ALERT, "No server name configured in \"%s\"!", NGIRCd_ConfFile );
497 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
500 } /* Validate_Config */
504 LOCAL VOID Config_Error( CONST INT Level, CONST CHAR *Format, ... )
506 LOCAL VOID Config_Error( Level, Format, va_alist )
512 /* Fehler! Auf Console und/oder ins Log schreiben */
514 CHAR msg[MAX_LOG_MSG_LEN];
517 assert( Format != NULL );
519 /* String mit variablen Argumenten zusammenbauen ... */
521 va_start( ap, Format );
525 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
528 /* Im "normalen Betrieb" soll der Log-Mechanismus des ngIRCd verwendet
529 * werden, beim Testen der Konfiguration jedoch nicht, hier sollen alle
530 * Meldungen direkt auf die Konsole ausgegeben werden: */
531 if( Use_Log ) Log( Level, msg );