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.25 2002/05/22 11:14:36 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 if( ! Conf_Oper[i].name[0] ) continue;
105 /* gueltiger Operator-Block: ausgeben */
106 puts( "[OPERATOR]" );
107 printf( " Name = %s\n", Conf_Oper[i].name );
108 printf( " Password = %s\n", Conf_Oper[i].pwd );
112 for( i = 0; i < Conf_Server_Count; i++ )
114 if( ! Conf_Server[i].name[0] ) continue;
115 if( ! Conf_Server[i].host[0] ) continue;
117 /* gueltiger Server-Block: ausgeben */
119 printf( " Name = %s\n", Conf_Server[i].name );
120 printf( " Host = %s\n", Conf_Server[i].host );
121 printf( " Port = %d\n", Conf_Server[i].port );
122 printf( " Password = %s\n", Conf_Server[i].pwd );
123 printf( " Group = %d\n", Conf_Server[i].group );
127 for( i = 0; i < Conf_Channel_Count; i++ )
129 if( ! Conf_Channel[i].name[0] ) continue;
131 /* gueltiger Channel-Block: ausgeben */
133 printf( " Name = %s\n", Conf_Channel[i].name );
134 printf( " Modes = %s\n", Conf_Channel[i].modes );
135 printf( " Topic = %s\n", Conf_Channel[i].topic );
143 GLOBAL VOID Conf_Exit( VOID )
149 LOCAL VOID Set_Defaults( VOID )
151 /* Konfigurationsvariablen initialisieren, d.h. auf Default-Werte setzen. */
153 strcpy( Conf_ServerName, "" );
154 strcpy( Conf_ServerInfo, PACKAGE" "VERSION );
155 strcpy( Conf_ServerPwd, "" );
157 strcpy( Conf_MotdFile, MOTD_FILE );
159 Conf_ListenPorts_Count = 0;
161 Conf_UID = Conf_GID = 0;
163 Conf_PingTimeout = 120;
164 Conf_PongTimeout = 20;
166 Conf_ConnectRetry = 60;
169 Conf_Server_Count = 0;
170 Conf_Channel_Count = 0;
174 LOCAL VOID Read_Config( VOID )
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, PACKAGE" exiting due to fatal errors!" );
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;
284 GLOBAL VOID Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
290 assert( Var != NULL );
291 assert( Arg != NULL );
293 if( strcasecmp( Var, "Name" ) == 0 )
295 /* Der Server-Name */
296 strncpy( Conf_ServerName, Arg, CLIENT_ID_LEN - 1 );
297 Conf_ServerName[CLIENT_ID_LEN - 1] = '\0';
300 if( strcasecmp( Var, "Info" ) == 0 )
302 /* Server-Info-Text */
303 strncpy( Conf_ServerInfo, Arg, CLIENT_INFO_LEN - 1 );
304 Conf_ServerInfo[CLIENT_INFO_LEN - 1] = '\0';
307 if( strcasecmp( Var, "Password" ) == 0 )
309 /* Server-Passwort */
310 strncpy( Conf_ServerPwd, Arg, CLIENT_PASS_LEN - 1 );
311 Conf_ServerPwd[CLIENT_PASS_LEN - 1] = '\0';
314 if( strcasecmp( Var, "Ports" ) == 0 )
316 /* Ports, durch "," getrennt, auf denen der Server
317 * Verbindungen annehmen soll */
318 ptr = strtok( Arg, "," );
323 if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Config_Error( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
326 if( port > 0 && port < 0xFFFF ) Conf_ListenPorts[Conf_ListenPorts_Count++] = (UINT)port;
327 else Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
329 ptr = strtok( NULL, "," );
333 if( strcasecmp( Var, "MotdFile" ) == 0 )
335 /* Datei mit der "message of the day" (MOTD) */
336 strncpy( Conf_MotdFile, Arg, FNAME_LEN - 1 );
337 Conf_MotdFile[FNAME_LEN - 1] = '\0';
340 if( strcasecmp( Var, "ServerUID" ) == 0 )
342 /* UID, mit der der Daemon laufen soll */
343 Conf_UID = (UINT)atoi( Arg );
346 if( strcasecmp( Var, "ServerGID" ) == 0 )
348 /* GID, mit der der Daemon laufen soll */
349 Conf_GID = (UINT)atoi( Arg );
352 if( strcasecmp( Var, "PingTimeout" ) == 0 )
355 Conf_PingTimeout = atoi( Arg );
356 if(( Conf_PingTimeout ) < 5 ) Conf_PingTimeout = 5;
359 if( strcasecmp( Var, "PongTimeout" ) == 0 )
362 Conf_PongTimeout = atoi( Arg );
363 if(( Conf_PongTimeout ) < 5 ) Conf_PongTimeout = 5;
366 if( strcasecmp( Var, "ConnectRetry" ) == 0 )
368 /* Sekunden zwischen Verbindungsversuchen zu anderen Servern */
369 Conf_ConnectRetry = atoi( Arg );
370 if(( Conf_ConnectRetry ) < 5 ) Conf_ConnectRetry = 5;
374 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
375 } /* Handle_GLOBAL */
378 GLOBAL VOID Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
381 assert( Var != NULL );
382 assert( Arg != NULL );
383 assert( Conf_Oper_Count > 0 );
385 if( strcasecmp( Var, "Name" ) == 0 )
387 /* Name des IRC Operator */
388 strncpy( Conf_Oper[Conf_Oper_Count - 1].name, Arg, CLIENT_ID_LEN - 1 );
389 Conf_Oper[Conf_Oper_Count - 1].name[CLIENT_ID_LEN - 1] = '\0';
392 if( strcasecmp( Var, "Password" ) == 0 )
394 /* Passwort des IRC Operator */
395 strncpy( Conf_Oper[Conf_Oper_Count - 1].pwd, Arg, CLIENT_PASS_LEN - 1 );
396 Conf_Oper[Conf_Oper_Count - 1].pwd[CLIENT_PASS_LEN - 1] = '\0';
400 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
401 } /* Handle_OPERATOR */
404 GLOBAL VOID Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
409 assert( Var != NULL );
410 assert( Arg != NULL );
412 if( strcasecmp( Var, "Host" ) == 0 )
414 /* Hostname des Servers */
415 strncpy( Conf_Server[Conf_Server_Count - 1].host, Arg, HOST_LEN - 1 );
416 Conf_Server[Conf_Server_Count - 1].host[HOST_LEN - 1] = '\0';
419 if( strcasecmp( Var, "Name" ) == 0 )
421 /* Name des Servers ("Nick") */
422 strncpy( Conf_Server[Conf_Server_Count - 1].name, Arg, CLIENT_ID_LEN - 1 );
423 Conf_Server[Conf_Server_Count - 1].name[CLIENT_ID_LEN - 1] = '\0';
426 if( strcasecmp( Var, "Password" ) == 0 )
428 /* Passwort des Servers */
429 strncpy( Conf_Server[Conf_Server_Count - 1].pwd, Arg, CLIENT_PASS_LEN - 1 );
430 Conf_Server[Conf_Server_Count - 1].pwd[CLIENT_PASS_LEN - 1] = '\0';
433 if( strcasecmp( Var, "Port" ) == 0 )
435 /* Port, zu dem Verbunden werden soll */
437 if( port > 0 && port < 0xFFFF ) Conf_Server[Conf_Server_Count - 1].port = (INT)port;
438 else Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
441 if( strcasecmp( Var, "Group" ) == 0 )
444 Conf_Server[Conf_Server_Count - 1].group = atoi( Arg );
448 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
449 } /* Handle_SERVER */
452 GLOBAL VOID Handle_CHANNEL( INT Line, CHAR *Var, CHAR *Arg )
455 assert( Var != NULL );
456 assert( Arg != NULL );
458 if( strcasecmp( Var, "Name" ) == 0 )
460 /* Hostname des Servers */
461 strncpy( Conf_Channel[Conf_Channel_Count - 1].name, Arg, CHANNEL_NAME_LEN - 1 );
462 Conf_Channel[Conf_Channel_Count - 1].name[CHANNEL_NAME_LEN - 1] = '\0';
465 if( strcasecmp( Var, "Modes" ) == 0 )
467 /* Name des Servers ("Nick") */
468 strncpy( Conf_Channel[Conf_Channel_Count - 1].modes, Arg, CHANNEL_MODE_LEN - 1 );
469 Conf_Channel[Conf_Channel_Count - 1].modes[CHANNEL_MODE_LEN - 1] = '\0';
472 if( strcasecmp( Var, "Topic" ) == 0 )
474 /* Passwort des Servers */
475 strncpy( Conf_Channel[Conf_Channel_Count - 1].topic, Arg, CHANNEL_TOPIC_LEN - 1 );
476 Conf_Channel[Conf_Channel_Count - 1].topic[CHANNEL_TOPIC_LEN - 1] = '\0';
480 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
481 } /* Handle_CHANNEL */
484 LOCAL VOID Validate_Config( VOID )
486 /* Konfiguration ueberpruefen */
488 if( ! Conf_ServerName[0] )
490 /* Kein Servername konfiguriert */
491 Config_Error( LOG_ALERT, "No server name configured in \"%s\"!", NGIRCd_ConfFile );
492 Config_Error( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
495 } /* Validate_Config */
498 LOCAL VOID Config_Error( CONST INT Level, CONST CHAR *Format, ... )
500 /* Fehler! Auf Console und/oder ins Log schreiben */
502 CHAR msg[MAX_LOG_MSG_LEN];
505 assert( Format != NULL );
507 /* String mit variablen Argumenten zusammenbauen ... */
508 va_start( ap, Format );
509 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
512 /* Im "normalen Betrieb" soll der Log-Mechanismus des ngIRCd verwendet
513 * werden, beim Testen der Konfiguration jedoch nicht, hier sollen alle
514 * Meldungen direkt auf die Konsole ausgegeben werden: */
515 if( Use_Log ) Log( Level, msg );