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.21 2002/03/27 16:39:22 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 );
50 LOCAL VOID Config_Error( CONST INT Level, CONST CHAR *Format, ... );
53 GLOBAL VOID Conf_Init( VOID )
61 GLOBAL INT Conf_Test( VOID )
63 /* Konfiguration einlesen, ueberpruefen und ausgeben. */
70 printf( "Using \"%s\" as configuration file ...\n", NGIRCd_ConfFile );
73 /* Wenn stdin ein ein TTY ist: auf Taste warten */
74 if( isatty( fileno( stdout )))
76 puts( "OK, press enter to see a dump of your service configuration ..." );
79 else puts( "Ok, dump of your server configuration follows:\n" );
82 printf( " ServerName = %s\n", Conf_ServerName );
83 printf( " ServerInfo = %s\n", Conf_ServerInfo );
84 printf( " ServerPwd = %s\n", Conf_ServerPwd );
85 printf( " MotdFile = %s\n", Conf_MotdFile );
86 printf( " ListenPorts = " );
87 for( i = 0; i < Conf_ListenPorts_Count; i++ )
89 if( i != 0 ) printf( ", " );
90 printf( "%d", Conf_ListenPorts[i] );
92 if( Conf_ListenPorts_Count < 1 ) puts( "<none>");
94 printf( " PingTimeout = %d\n", Conf_PingTimeout );
95 printf( " PongTimeout = %d\n", Conf_PongTimeout );
96 printf( " ConnectRetry = %d\n", Conf_ConnectRetry );
99 for( i = 0; i < Conf_Oper_Count; i++ )
101 puts( "[OPERATOR]" );
102 printf( " Name = %s\n", Conf_Oper[i].name );
103 printf( " Password = %s\n", Conf_Oper[i].pwd );
107 for( i = 0; i < Conf_Server_Count; i++ )
110 printf( " Name = %s\n", Conf_Server[i].name );
111 printf( " Host = %s\n", Conf_Server[i].host );
112 printf( " Port = %d\n", Conf_Server[i].port );
113 printf( " Password = %s\n", Conf_Server[i].pwd );
114 printf( " Group = %d\n", Conf_Server[i].group );
122 GLOBAL VOID Conf_Exit( VOID )
128 LOCAL VOID Set_Defaults( VOID )
130 /* Konfigurationsvariablen initialisieren, d.h. auf Default-Werte setzen. */
132 strcpy( Conf_ServerName, "" );
133 strcpy( Conf_ServerInfo, PACKAGE" "VERSION );
134 strcpy( Conf_ServerPwd, "" );
136 strcpy( Conf_MotdFile, MOTD_FILE );
138 Conf_ListenPorts_Count = 0;
140 Conf_PingTimeout = 120;
141 Conf_PongTimeout = 20;
143 Conf_ConnectRetry = 60;
147 Conf_Server_Count = 0;
151 LOCAL VOID Read_Config( VOID )
153 /* Konfigurationsdatei einlesen. */
155 CHAR section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
159 fd = fopen( NGIRCd_ConfFile, "r" );
162 /* Keine Konfigurationsdatei gefunden */
163 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s", NGIRCd_ConfFile, strerror( errno ));
164 Config_Error( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
169 strcpy( section, "" );
172 if( ! fgets( str, LINE_LEN, fd )) break;
176 /* Kommentarzeilen und leere Zeilen ueberspringen */
177 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
179 /* Anfang eines Abschnittes? */
180 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
182 strcpy( section, str );
183 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
184 if( strcasecmp( section, "[OPERATOR]" ) == 0 )
186 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Config_Error( LOG_ERR, "Too many operators configured." );
189 /* neuen Operator initialisieren */
190 strcpy( Conf_Oper[Conf_Oper_Count].name, "" );
191 strcpy( Conf_Oper[Conf_Oper_Count].pwd, "" );
196 if( strcasecmp( section, "[SERVER]" ) == 0 )
198 if( Conf_Server_Count + 1 > MAX_SERVERS ) Config_Error( LOG_ERR, "Too many servers configured." );
201 /* neuen Server ("Peer") initialisieren */
202 strcpy( Conf_Server[Conf_Server_Count].host, "" );
203 strcpy( Conf_Server[Conf_Server_Count].ip, "" );
204 strcpy( Conf_Server[Conf_Server_Count].name, "" );
205 strcpy( Conf_Server[Conf_Server_Count].pwd, "" );
206 Conf_Server[Conf_Server_Count].port = 0;
207 Conf_Server[Conf_Server_Count].group = -1;
208 Conf_Server[Conf_Server_Count].lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
209 Conf_Server[Conf_Server_Count].res_stat = NULL;
214 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
217 if( section[0] == 0x1 ) continue;
219 /* In Variable und Argument zerlegen */
220 ptr = strchr( str, '=' );
223 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
227 var = str; ngt_TrimStr( var );
228 arg = ptr + 1; ngt_TrimStr( arg );
230 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
231 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
232 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
233 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
240 GLOBAL VOID Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
246 assert( Var != NULL );
247 assert( Arg != NULL );
249 if( strcasecmp( Var, "Name" ) == 0 )
251 /* Der Server-Name */
252 strncpy( Conf_ServerName, Arg, CLIENT_ID_LEN - 1 );
253 Conf_ServerName[CLIENT_ID_LEN - 1] = '\0';
256 if( strcasecmp( Var, "Info" ) == 0 )
258 /* Server-Info-Text */
259 strncpy( Conf_ServerInfo, Arg, CLIENT_INFO_LEN - 1 );
260 Conf_ServerInfo[CLIENT_INFO_LEN - 1] = '\0';
263 if( strcasecmp( Var, "Password" ) == 0 )
265 /* Server-Passwort */
266 strncpy( Conf_ServerPwd, Arg, CLIENT_PASS_LEN - 1 );
267 Conf_ServerPwd[CLIENT_PASS_LEN - 1] = '\0';
270 if( strcasecmp( Var, "Ports" ) == 0 )
272 /* Ports, durch "," getrennt, auf denen der Server
273 * Verbindungen annehmen soll */
274 ptr = strtok( Arg, "," );
279 if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Config_Error( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
282 if( port > 0 && port < 0xFFFF ) Conf_ListenPorts[Conf_ListenPorts_Count++] = (INT)port;
283 else Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
285 ptr = strtok( NULL, "," );
289 if( strcasecmp( Var, "MotdFile" ) == 0 )
291 /* Datei mit der "message of the day" (MOTD) */
292 strncpy( Conf_MotdFile, Arg, FNAME_LEN - 1 );
293 Conf_MotdFile[FNAME_LEN - 1] = '\0';
296 if( strcasecmp( Var, "PingTimeout" ) == 0 )
299 Conf_PingTimeout = atoi( Arg );
300 if(( Conf_PingTimeout ) < 5 ) Conf_PingTimeout = 5;
303 if( strcasecmp( Var, "PongTimeout" ) == 0 )
306 Conf_PongTimeout = atoi( Arg );
307 if(( Conf_PongTimeout ) < 5 ) Conf_PongTimeout = 5;
310 if( strcasecmp( Var, "ConnectRetry" ) == 0 )
312 /* Sekunden zwischen Verbindungsversuchen zu anderen Servern */
313 Conf_ConnectRetry = atoi( Arg );
314 if(( Conf_ConnectRetry ) < 5 ) Conf_ConnectRetry = 5;
318 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
319 } /* Handle_GLOBAL */
322 GLOBAL VOID Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
325 assert( Var != NULL );
326 assert( Arg != NULL );
327 assert( Conf_Oper_Count > 0 );
329 if( strcasecmp( Var, "Name" ) == 0 )
331 /* Name des IRC Operator */
332 strncpy( Conf_Oper[Conf_Oper_Count - 1].name, Arg, CLIENT_ID_LEN - 1 );
333 Conf_Oper[Conf_Oper_Count - 1].name[CLIENT_ID_LEN - 1] = '\0';
336 if( strcasecmp( Var, "Password" ) == 0 )
338 /* Passwort des IRC Operator */
339 strncpy( Conf_Oper[Conf_Oper_Count - 1].pwd, Arg, CLIENT_PASS_LEN - 1 );
340 Conf_Oper[Conf_Oper_Count - 1].pwd[CLIENT_PASS_LEN - 1] = '\0';
344 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
345 } /* Handle_OPERATOR */
348 GLOBAL VOID Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
353 assert( Var != NULL );
354 assert( Arg != NULL );
356 if( strcasecmp( Var, "Host" ) == 0 )
358 /* Hostname des Servers */
359 strncpy( Conf_Server[Conf_Server_Count - 1].host, Arg, HOST_LEN - 1 );
360 Conf_Server[Conf_Server_Count - 1].host[HOST_LEN - 1] = '\0';
363 if( strcasecmp( Var, "Name" ) == 0 )
365 /* Name des Servers ("Nick") */
366 strncpy( Conf_Server[Conf_Server_Count - 1].name, Arg, CLIENT_ID_LEN - 1 );
367 Conf_Server[Conf_Server_Count - 1].name[CLIENT_ID_LEN - 1] = '\0';
370 if( strcasecmp( Var, "Password" ) == 0 )
372 /* Passwort des Servers */
373 strncpy( Conf_Server[Conf_Server_Count - 1].pwd, Arg, CLIENT_PASS_LEN - 1 );
374 Conf_Server[Conf_Server_Count - 1].pwd[CLIENT_PASS_LEN - 1] = '\0';
377 if( strcasecmp( Var, "Port" ) == 0 )
379 /* Port, zu dem Verbunden werden soll */
381 if( port > 0 && port < 0xFFFF ) Conf_Server[Conf_Server_Count - 1].port = (INT)port;
382 else Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
385 if( strcasecmp( Var, "Group" ) == 0 )
388 Conf_Server[Conf_Server_Count - 1].group = atoi( Arg );
392 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
393 } /* Handle_SERVER */
396 LOCAL VOID Validate_Config( VOID )
398 /* Konfiguration ueberpruefen */
400 if( ! Conf_ServerName[0] )
402 /* Kein Servername konfiguriert */
403 Config_Error( LOG_ALERT, "No server name configured in \"%s\"!", NGIRCd_ConfFile );
404 Config_Error( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
407 } /* Validate_Config */
410 LOCAL VOID Config_Error( CONST INT Level, CONST CHAR *Format, ... )
412 /* Fehler! Auf Console und/oder ins Log schreiben */
414 CHAR msg[MAX_LOG_MSG_LEN];
417 assert( Format != NULL );
419 /* String mit variablen Argumenten zusammenbauen ... */
420 va_start( ap, Format );
421 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
424 /* Im "normalen Betrieb" soll der Log-Mechanismus des ngIRCd verwendet
425 * werden, beim Testen der Konfiguration jedoch nicht, hier sollen alle
426 * Meldungen direkt auf die Konsole ausgegeben werden: */
427 if( Use_Log ) Log( Level, msg );