Blob


1 /*
2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
4 *
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.
11 *
12 * $Id: conf.c,v 1.21 2002/03/27 16:39:22 alex Exp $
13 *
14 * conf.h: Konfiguration des ngircd
15 */
18 #include "portab.h"
20 #include "imp.h"
21 #include <assert.h>
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 #include "ngircd.h"
30 #include "client.h"
31 #include "defines.h"
32 #include "log.h"
33 #include "tool.h"
35 #include "exp.h"
36 #include "conf.h"
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 )
54 {
55 Set_Defaults( );
56 Read_Config( );
57 Validate_Config( );
58 } /* Config_Init */
61 GLOBAL INT Conf_Test( VOID )
62 {
63 /* Konfiguration einlesen, ueberpruefen und ausgeben. */
65 INT i;
67 Use_Log = FALSE;
68 Set_Defaults( );
70 printf( "Using \"%s\" as configuration file ...\n", NGIRCd_ConfFile );
71 Read_Config( );
73 /* Wenn stdin ein ein TTY ist: auf Taste warten */
74 if( isatty( fileno( stdout )))
75 {
76 puts( "OK, press enter to see a dump of your service configuration ..." );
77 getchar( );
78 }
79 else puts( "Ok, dump of your server configuration follows:\n" );
81 puts( "[GLOBAL]" );
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++ )
88 {
89 if( i != 0 ) printf( ", " );
90 printf( "%d", Conf_ListenPorts[i] );
91 }
92 if( Conf_ListenPorts_Count < 1 ) puts( "<none>");
93 else puts( "" );
94 printf( " PingTimeout = %d\n", Conf_PingTimeout );
95 printf( " PongTimeout = %d\n", Conf_PongTimeout );
96 printf( " ConnectRetry = %d\n", Conf_ConnectRetry );
97 puts( "" );
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 );
104 puts( "" );
107 for( i = 0; i < Conf_Server_Count; i++ )
109 puts( "[SERVER]" );
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 );
115 puts( "" );
118 return 0;
119 } /* Conf_Test */
122 GLOBAL VOID Conf_Exit( VOID )
124 /* ... */
125 } /* Config_Exit */
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;
145 Conf_Oper_Count = 0;
147 Conf_Server_Count = 0;
148 } /* Set_Defaults */
151 LOCAL VOID Read_Config( VOID )
153 /* Konfigurationsdatei einlesen. */
155 CHAR section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
156 INT line;
157 FILE *fd;
159 fd = fopen( NGIRCd_ConfFile, "r" );
160 if( ! fd )
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!" );
165 exit( 1 );
168 line = 0;
169 strcpy( section, "" );
170 while( TRUE )
172 if( ! fgets( str, LINE_LEN, fd )) break;
173 ngt_TrimStr( str );
174 line++;
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." );
187 else
189 /* neuen Operator initialisieren */
190 strcpy( Conf_Oper[Conf_Oper_Count].name, "" );
191 strcpy( Conf_Oper[Conf_Oper_Count].pwd, "" );
192 Conf_Oper_Count++;
194 continue;
196 if( strcasecmp( section, "[SERVER]" ) == 0 )
198 if( Conf_Server_Count + 1 > MAX_SERVERS ) Config_Error( LOG_ERR, "Too many servers configured." );
199 else
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;
210 Conf_Server_Count++;
212 continue;
214 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
215 section[0] = 0x1;
217 if( section[0] == 0x1 ) continue;
219 /* In Variable und Argument zerlegen */
220 ptr = strchr( str, '=' );
221 if( ! ptr )
223 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
224 continue;
226 *ptr = '\0';
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 );
236 fclose( fd );
237 } /* Read_Config */
240 GLOBAL VOID Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
242 CHAR *ptr;
243 INT32 port;
245 assert( Line > 0 );
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';
254 return;
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';
261 return;
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';
268 return;
270 if( strcasecmp( Var, "Ports" ) == 0 )
272 /* Ports, durch "," getrennt, auf denen der Server
273 * Verbindungen annehmen soll */
274 ptr = strtok( Arg, "," );
275 while( ptr )
277 ngt_TrimStr( ptr );
278 port = atol( ptr );
279 if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Config_Error( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
280 else
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, "," );
287 return;
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';
294 return;
296 if( strcasecmp( Var, "PingTimeout" ) == 0 )
298 /* PING-Timeout */
299 Conf_PingTimeout = atoi( Arg );
300 if(( Conf_PingTimeout ) < 5 ) Conf_PingTimeout = 5;
301 return;
303 if( strcasecmp( Var, "PongTimeout" ) == 0 )
305 /* PONG-Timeout */
306 Conf_PongTimeout = atoi( Arg );
307 if(( Conf_PongTimeout ) < 5 ) Conf_PongTimeout = 5;
308 return;
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;
315 return;
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 )
324 assert( Line > 0 );
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';
334 return;
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';
341 return;
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 )
350 INT32 port;
352 assert( Line > 0 );
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';
361 return;
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';
368 return;
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';
375 return;
377 if( strcasecmp( Var, "Port" ) == 0 )
379 /* Port, zu dem Verbunden werden soll */
380 port = atol( Arg );
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 );
383 return;
385 if( strcasecmp( Var, "Group" ) == 0 )
387 /* Server-Gruppe */
388 Conf_Server[Conf_Server_Count - 1].group = atoi( Arg );
389 return;
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!" );
405 exit( 1 );
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];
415 va_list ap;
417 assert( Format != NULL );
419 /* String mit variablen Argumenten zusammenbauen ... */
420 va_start( ap, Format );
421 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
422 va_end( 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 );
428 else puts( msg );
429 } /* Config_Error */
432 /* -eof- */