]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conf.c
- neue Funktion Conf_Test() zum Testen der Konfiguration.
[ngircd-alex.git] / src / ngircd / conf.c
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  */
16
17
18 #include "portab.h"
19
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>
28
29 #include "ngircd.h"
30 #include "client.h"
31 #include "defines.h"
32 #include "log.h"
33 #include "tool.h"
34
35 #include "exp.h"
36 #include "conf.h"
37
38
39 LOCAL BOOLEAN Use_Log = TRUE;
40
41
42 LOCAL VOID Set_Defaults( VOID );
43 LOCAL VOID Read_Config( VOID );
44 LOCAL VOID Validate_Config( VOID );
45
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
50 LOCAL VOID Config_Error( CONST INT Level, CONST CHAR *Format, ... );
51
52
53 GLOBAL VOID Conf_Init( VOID )
54 {
55         Set_Defaults( );
56         Read_Config( );
57         Validate_Config( );
58 } /* Config_Init */
59
60
61 GLOBAL INT Conf_Test( VOID )
62 {
63         /* Konfiguration einlesen, ueberpruefen und ausgeben. */
64
65         INT i;
66
67         Use_Log = FALSE;
68         Set_Defaults( );
69
70         printf( "Using \"%s\" as configuration file ...\n", NGIRCd_ConfFile );
71         Read_Config( );
72
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" );
80
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( "" );
98
99         for( i = 0; i < Conf_Oper_Count; i++ )
100         {
101                 puts( "[OPERATOR]" );
102                 printf( "  Name = %s\n", Conf_Oper[i].name );
103                 printf( "  Password = %s\n", Conf_Oper[i].pwd );
104                 puts( "" );
105         }
106
107         for( i = 0; i < Conf_Server_Count; i++ )
108         {
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( "" );
116         }
117         
118         return 0;
119 } /* Conf_Test */
120
121
122 GLOBAL VOID Conf_Exit( VOID )
123 {
124         /* ... */
125 } /* Config_Exit */
126
127
128 LOCAL VOID Set_Defaults( VOID )
129 {
130         /* Konfigurationsvariablen initialisieren, d.h. auf Default-Werte setzen. */
131
132         strcpy( Conf_ServerName, "" );
133         strcpy( Conf_ServerInfo, PACKAGE" "VERSION );
134         strcpy( Conf_ServerPwd, "" );
135
136         strcpy( Conf_MotdFile, MOTD_FILE );
137
138         Conf_ListenPorts_Count = 0;
139         
140         Conf_PingTimeout = 120;
141         Conf_PongTimeout = 20;
142
143         Conf_ConnectRetry = 60;
144
145         Conf_Oper_Count = 0;
146
147         Conf_Server_Count = 0;
148 } /* Set_Defaults */
149
150
151 LOCAL VOID Read_Config( VOID )
152 {
153         /* Konfigurationsdatei einlesen. */
154
155         CHAR section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
156         INT line;
157         FILE *fd;
158
159         fd = fopen( NGIRCd_ConfFile, "r" );
160         if( ! fd )
161         {
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 );
166         }
167
168         line = 0;
169         strcpy( section, "" );
170         while( TRUE )
171         {
172                 if( ! fgets( str, LINE_LEN, fd )) break;
173                 ngt_TrimStr( str );
174                 line++;
175
176                 /* Kommentarzeilen und leere Zeilen ueberspringen */
177                 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
178
179                 /* Anfang eines Abschnittes? */
180                 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
181                 {
182                         strcpy( section, str );
183                         if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
184                         if( strcasecmp( section, "[OPERATOR]" ) == 0 )
185                         {
186                                 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Config_Error( LOG_ERR, "Too many operators configured." );
187                                 else
188                                 {
189                                         /* neuen Operator initialisieren */
190                                         strcpy( Conf_Oper[Conf_Oper_Count].name, "" );
191                                         strcpy( Conf_Oper[Conf_Oper_Count].pwd, "" );
192                                         Conf_Oper_Count++;
193                                 }
194                                 continue;
195                         }
196                         if( strcasecmp( section, "[SERVER]" ) == 0 )
197                         {
198                                 if( Conf_Server_Count + 1 > MAX_SERVERS ) Config_Error( LOG_ERR, "Too many servers configured." );
199                                 else
200                                 {
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++;
211                                 }
212                                 continue;
213                         }
214                         Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
215                         section[0] = 0x1;
216                 }
217                 if( section[0] == 0x1 ) continue;
218
219                 /* In Variable und Argument zerlegen */
220                 ptr = strchr( str, '=' );
221                 if( ! ptr )
222                 {
223                         Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
224                         continue;
225                 }
226                 *ptr = '\0';
227                 var = str; ngt_TrimStr( var );
228                 arg = ptr + 1; ngt_TrimStr( arg );
229
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 );
234         }
235         
236         fclose( fd );
237 } /* Read_Config */
238
239
240 GLOBAL VOID Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
241 {
242         CHAR *ptr;
243         INT32 port;
244         
245         assert( Line > 0 );
246         assert( Var != NULL );
247         assert( Arg != NULL );
248         
249         if( strcasecmp( Var, "Name" ) == 0 )
250         {
251                 /* Der Server-Name */
252                 strncpy( Conf_ServerName, Arg, CLIENT_ID_LEN - 1 );
253                 Conf_ServerName[CLIENT_ID_LEN - 1] = '\0';
254                 return;
255         }
256         if( strcasecmp( Var, "Info" ) == 0 )
257         {
258                 /* Server-Info-Text */
259                 strncpy( Conf_ServerInfo, Arg, CLIENT_INFO_LEN - 1 );
260                 Conf_ServerInfo[CLIENT_INFO_LEN - 1] = '\0';
261                 return;
262         }
263         if( strcasecmp( Var, "Password" ) == 0 )
264         {
265                 /* Server-Passwort */
266                 strncpy( Conf_ServerPwd, Arg, CLIENT_PASS_LEN - 1 );
267                 Conf_ServerPwd[CLIENT_PASS_LEN - 1] = '\0';
268                 return;
269         }
270         if( strcasecmp( Var, "Ports" ) == 0 )
271         {
272                 /* Ports, durch "," getrennt, auf denen der Server
273                 * Verbindungen annehmen soll */
274                 ptr = strtok( Arg, "," );
275                 while( ptr )
276                 {
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
281                         {
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 );
284                         }
285                         ptr = strtok( NULL, "," );
286                 }
287                 return;
288         }
289         if( strcasecmp( Var, "MotdFile" ) == 0 )
290         {
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;
295         }
296         if( strcasecmp( Var, "PingTimeout" ) == 0 )
297         {
298                 /* PING-Timeout */
299                 Conf_PingTimeout = atoi( Arg );
300                 if(( Conf_PingTimeout ) < 5 ) Conf_PingTimeout = 5;
301                 return;
302         }
303         if( strcasecmp( Var, "PongTimeout" ) == 0 )
304         {
305                 /* PONG-Timeout */
306                 Conf_PongTimeout = atoi( Arg );
307                 if(( Conf_PongTimeout ) < 5 ) Conf_PongTimeout = 5;
308                 return;
309         }
310         if( strcasecmp( Var, "ConnectRetry" ) == 0 )
311         {
312                 /* Sekunden zwischen Verbindungsversuchen zu anderen Servern */
313                 Conf_ConnectRetry = atoi( Arg );
314                 if(( Conf_ConnectRetry ) < 5 ) Conf_ConnectRetry = 5;
315                 return;
316         }
317                 
318         Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
319 } /* Handle_GLOBAL */
320
321
322 GLOBAL VOID Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
323 {
324         assert( Line > 0 );
325         assert( Var != NULL );
326         assert( Arg != NULL );
327         assert( Conf_Oper_Count > 0 );
328
329         if( strcasecmp( Var, "Name" ) == 0 )
330         {
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;
335         }
336         if( strcasecmp( Var, "Password" ) == 0 )
337         {
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;
342         }
343         
344         Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
345 } /* Handle_OPERATOR */
346
347
348 GLOBAL VOID Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
349 {
350         INT32 port;
351         
352         assert( Line > 0 );
353         assert( Var != NULL );
354         assert( Arg != NULL );
355
356         if( strcasecmp( Var, "Host" ) == 0 )
357         {
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;
362         }
363         if( strcasecmp( Var, "Name" ) == 0 )
364         {
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;
369         }
370         if( strcasecmp( Var, "Password" ) == 0 )
371         {
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;
376         }
377         if( strcasecmp( Var, "Port" ) == 0 )
378         {
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;
384         }
385         if( strcasecmp( Var, "Group" ) == 0 )
386         {
387                 /* Server-Gruppe */
388                 Conf_Server[Conf_Server_Count - 1].group = atoi( Arg );
389                 return;
390         }
391         
392         Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
393 } /* Handle_SERVER */
394
395
396 LOCAL VOID Validate_Config( VOID )
397 {
398         /* Konfiguration ueberpruefen */
399         
400         if( ! Conf_ServerName[0] )
401         {
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 );
406         }
407 } /* Validate_Config */
408
409
410 LOCAL VOID Config_Error( CONST INT Level, CONST CHAR *Format, ... )
411 {
412         /* Fehler! Auf Console und/oder ins Log schreiben */
413
414         CHAR msg[MAX_LOG_MSG_LEN];
415         va_list ap;
416
417         assert( Format != NULL );
418
419         /* String mit variablen Argumenten zusammenbauen ... */
420         va_start( ap, Format );
421         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
422         va_end( ap );
423
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 */
430
431
432 /* -eof- */