]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conf.c
- Weitere Anpassungen an pre-ANSI-Compiler.
[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.27 2002/05/30 16:52:21 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 "conn.h"
31 #include "client.h"
32 #include "defines.h"
33 #include "log.h"
34 #include "resolve.h"
35 #include "tool.h"
36
37 #include "exp.h"
38 #include "conf.h"
39
40
41 LOCAL BOOLEAN Use_Log = TRUE;
42
43
44 LOCAL VOID Set_Defaults PARAMS(( VOID ));
45 LOCAL VOID Read_Config PARAMS(( VOID ));
46 LOCAL VOID Validate_Config PARAMS(( VOID ));
47
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 ));
52
53 LOCAL VOID Config_Error PARAMS(( CONST INT Level, CONST CHAR *Format, ... ));
54
55
56 GLOBAL VOID
57 Conf_Init( VOID )
58 {
59         Set_Defaults( );
60         Read_Config( );
61         Validate_Config( );
62 } /* Config_Init */
63
64
65 GLOBAL INT
66 Conf_Test( VOID )
67 {
68         /* Konfiguration einlesen, ueberpruefen und ausgeben. */
69
70         UINT i;
71
72         Use_Log = FALSE;
73         Set_Defaults( );
74
75         printf( "Using \"%s\" as configuration file ...\n", NGIRCd_ConfFile );
76         Read_Config( );
77
78         /* Wenn stdin ein ein TTY ist: auf Taste warten */
79         if( isatty( fileno( stdout )))
80         {
81                 puts( "OK, press enter to see a dump of your service configuration ..." );
82                 getchar( );
83         }
84         else puts( "Ok, dump of your server configuration follows:\n" );
85
86         puts( "[GLOBAL]" );
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++ )
93         {
94                 if( i != 0 ) printf( ", " );
95                 printf( "%u", Conf_ListenPorts[i] );
96         }
97         puts( "" );
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 );
103         puts( "" );
104
105         for( i = 0; i < Conf_Oper_Count; i++ )
106         {
107                 if( ! Conf_Oper[i].name[0] ) continue;
108                 
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 );
113                 puts( "" );
114         }
115
116         for( i = 0; i < Conf_Server_Count; i++ )
117         {
118                 if( ! Conf_Server[i].name[0] ) continue;
119                 if( ! Conf_Server[i].host[0] ) continue;
120                 
121                 /* gueltiger Server-Block: ausgeben */
122                 puts( "[SERVER]" );
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 );
128                 puts( "" );
129         }
130
131         for( i = 0; i < Conf_Channel_Count; i++ )
132         {
133                 if( ! Conf_Channel[i].name[0] ) continue;
134                 
135                 /* gueltiger Channel-Block: ausgeben */
136                 puts( "[CHANNEL]" );
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 );
140                 puts( "" );
141         }
142         
143         return 0;
144 } /* Conf_Test */
145
146
147 LOCAL VOID
148 Set_Defaults( VOID )
149 {
150         /* Konfigurationsvariablen initialisieren, d.h. auf Default-Werte setzen. */
151
152         strcpy( Conf_ServerName, "" );
153         sprintf( Conf_ServerInfo, "%s %s", PACKAGE, VERSION );
154         strcpy( Conf_ServerPwd, "" );
155
156         strcpy( Conf_MotdFile, MOTD_FILE );
157
158         Conf_ListenPorts_Count = 0;
159
160         Conf_UID = Conf_GID = 0;
161         
162         Conf_PingTimeout = 120;
163         Conf_PongTimeout = 20;
164
165         Conf_ConnectRetry = 60;
166
167         Conf_Oper_Count = 0;
168         Conf_Server_Count = 0;
169         Conf_Channel_Count = 0;
170 } /* Set_Defaults */
171
172
173 LOCAL VOID
174 Read_Config( VOID )
175 {
176         /* Konfigurationsdatei einlesen. */
177
178         CHAR section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
179         INT line;
180         FILE *fd;
181
182         fd = fopen( NGIRCd_ConfFile, "r" );
183         if( ! fd )
184         {
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 );
188                 exit( 1 );
189         }
190
191         line = 0;
192         strcpy( section, "" );
193         while( TRUE )
194         {
195                 if( ! fgets( str, LINE_LEN, fd )) break;
196                 ngt_TrimStr( str );
197                 line++;
198
199                 /* Kommentarzeilen und leere Zeilen ueberspringen */
200                 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
201
202                 /* Anfang eines Abschnittes? */
203                 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
204                 {
205                         strcpy( section, str );
206                         if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
207                         if( strcasecmp( section, "[OPERATOR]" ) == 0 )
208                         {
209                                 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Config_Error( LOG_ERR, "Too many operators configured." );
210                                 else
211                                 {
212                                         /* neuen Operator initialisieren */
213                                         strcpy( Conf_Oper[Conf_Oper_Count].name, "" );
214                                         strcpy( Conf_Oper[Conf_Oper_Count].pwd, "" );
215                                         Conf_Oper_Count++;
216                                 }
217                                 continue;
218                         }
219                         if( strcasecmp( section, "[SERVER]" ) == 0 )
220                         {
221                                 if( Conf_Server_Count + 1 > MAX_SERVERS ) Config_Error( LOG_ERR, "Too many servers configured." );
222                                 else
223                                 {
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;
233                                         Conf_Server_Count++;
234                                 }
235                                 continue;
236                         }
237                         if( strcasecmp( section, "[CHANNEL]" ) == 0 )
238                         {
239                                 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
240                                 else
241                                 {
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++;
247                                 }
248                                 continue;
249                         }
250                         Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
251                         section[0] = 0x1;
252                 }
253                 if( section[0] == 0x1 ) continue;
254
255                 /* In Variable und Argument zerlegen */
256                 ptr = strchr( str, '=' );
257                 if( ! ptr )
258                 {
259                         Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
260                         continue;
261                 }
262                 *ptr = '\0';
263                 var = str; ngt_TrimStr( var );
264                 arg = ptr + 1; ngt_TrimStr( arg );
265
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 );
271         }
272         
273         fclose( fd );
274         
275         /* Wenn kein Port definiert wurde, Port 6667 als Default benutzen */
276         if( Conf_ListenPorts_Count < 1 )
277         {
278                 Conf_ListenPorts_Count = 1;
279                 Conf_ListenPorts[0] = 6667;
280         }
281 } /* Read_Config */
282
283
284 LOCAL VOID
285 Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
286 {
287         CHAR *ptr;
288         INT32 port;
289         
290         assert( Line > 0 );
291         assert( Var != NULL );
292         assert( Arg != NULL );
293         
294         if( strcasecmp( Var, "Name" ) == 0 )
295         {
296                 /* Der Server-Name */
297                 strncpy( Conf_ServerName, Arg, CLIENT_ID_LEN - 1 );
298                 Conf_ServerName[CLIENT_ID_LEN - 1] = '\0';
299                 return;
300         }
301         if( strcasecmp( Var, "Info" ) == 0 )
302         {
303                 /* Server-Info-Text */
304                 strncpy( Conf_ServerInfo, Arg, CLIENT_INFO_LEN - 1 );
305                 Conf_ServerInfo[CLIENT_INFO_LEN - 1] = '\0';
306                 return;
307         }
308         if( strcasecmp( Var, "Password" ) == 0 )
309         {
310                 /* Server-Passwort */
311                 strncpy( Conf_ServerPwd, Arg, CLIENT_PASS_LEN - 1 );
312                 Conf_ServerPwd[CLIENT_PASS_LEN - 1] = '\0';
313                 return;
314         }
315         if( strcasecmp( Var, "Ports" ) == 0 )
316         {
317                 /* Ports, durch "," getrennt, auf denen der Server
318                 * Verbindungen annehmen soll */
319                 ptr = strtok( Arg, "," );
320                 while( ptr )
321                 {
322                         ngt_TrimStr( ptr );
323                         port = atol( ptr );
324                         if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Config_Error( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
325                         else
326                         {
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 );
329                         }
330                         ptr = strtok( NULL, "," );
331                 }
332                 return;
333         }
334         if( strcasecmp( Var, "MotdFile" ) == 0 )
335         {
336                 /* Datei mit der "message of the day" (MOTD) */
337                 strncpy( Conf_MotdFile, Arg, FNAME_LEN - 1 );
338                 Conf_MotdFile[FNAME_LEN - 1] = '\0';
339                 return;
340         }
341         if( strcasecmp( Var, "ServerUID" ) == 0 )
342         {
343                 /* UID, mit der der Daemon laufen soll */
344                 Conf_UID = (UINT)atoi( Arg );
345                 return;
346         }
347         if( strcasecmp( Var, "ServerGID" ) == 0 )
348         {
349                 /* GID, mit der der Daemon laufen soll */
350                 Conf_GID = (UINT)atoi( Arg );
351                 return;
352         }
353         if( strcasecmp( Var, "PingTimeout" ) == 0 )
354         {
355                 /* PING-Timeout */
356                 Conf_PingTimeout = atoi( Arg );
357                 if(( Conf_PingTimeout ) < 5 ) Conf_PingTimeout = 5;
358                 return;
359         }
360         if( strcasecmp( Var, "PongTimeout" ) == 0 )
361         {
362                 /* PONG-Timeout */
363                 Conf_PongTimeout = atoi( Arg );
364                 if(( Conf_PongTimeout ) < 5 ) Conf_PongTimeout = 5;
365                 return;
366         }
367         if( strcasecmp( Var, "ConnectRetry" ) == 0 )
368         {
369                 /* Sekunden zwischen Verbindungsversuchen zu anderen Servern */
370                 Conf_ConnectRetry = atoi( Arg );
371                 if(( Conf_ConnectRetry ) < 5 ) Conf_ConnectRetry = 5;
372                 return;
373         }
374                 
375         Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
376 } /* Handle_GLOBAL */
377
378
379 LOCAL VOID
380 Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
381 {
382         assert( Line > 0 );
383         assert( Var != NULL );
384         assert( Arg != NULL );
385         assert( Conf_Oper_Count > 0 );
386
387         if( strcasecmp( Var, "Name" ) == 0 )
388         {
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';
392                 return;
393         }
394         if( strcasecmp( Var, "Password" ) == 0 )
395         {
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';
399                 return;
400         }
401         
402         Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
403 } /* Handle_OPERATOR */
404
405
406 LOCAL VOID
407 Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
408 {
409         INT32 port;
410         
411         assert( Line > 0 );
412         assert( Var != NULL );
413         assert( Arg != NULL );
414
415         if( strcasecmp( Var, "Host" ) == 0 )
416         {
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';
420                 return;
421         }
422         if( strcasecmp( Var, "Name" ) == 0 )
423         {
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';
427                 return;
428         }
429         if( strcasecmp( Var, "Password" ) == 0 )
430         {
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';
434                 return;
435         }
436         if( strcasecmp( Var, "Port" ) == 0 )
437         {
438                 /* Port, zu dem Verbunden werden soll */
439                 port = atol( Arg );
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 );
442                 return;
443         }
444         if( strcasecmp( Var, "Group" ) == 0 )
445         {
446                 /* Server-Gruppe */
447                 Conf_Server[Conf_Server_Count - 1].group = atoi( Arg );
448                 return;
449         }
450         
451         Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
452 } /* Handle_SERVER */
453
454
455 LOCAL VOID
456 Handle_CHANNEL( INT Line, CHAR *Var, CHAR *Arg )
457 {
458         assert( Line > 0 );
459         assert( Var != NULL );
460         assert( Arg != NULL );
461
462         if( strcasecmp( Var, "Name" ) == 0 )
463         {
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';
467                 return;
468         }
469         if( strcasecmp( Var, "Modes" ) == 0 )
470         {
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';
474                 return;
475         }
476         if( strcasecmp( Var, "Topic" ) == 0 )
477         {
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';
481                 return;
482         }
483
484         Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
485 } /* Handle_CHANNEL */
486
487
488 LOCAL VOID
489 Validate_Config( VOID )
490 {
491         /* Konfiguration ueberpruefen */
492         
493         if( ! Conf_ServerName[0] )
494         {
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 );
498                 exit( 1 );
499         }
500 } /* Validate_Config */
501
502
503 #ifdef PROTOTYPES
504 LOCAL VOID Config_Error( CONST INT Level, CONST CHAR *Format, ... )
505 #else
506 LOCAL VOID Config_Error( Level, Format, va_alist )
507 CONST INT Level;
508 CONST CHAR *Format;
509 va_dcl
510 #endif
511 {
512         /* Fehler! Auf Console und/oder ins Log schreiben */
513
514         CHAR msg[MAX_LOG_MSG_LEN];
515         va_list ap;
516
517         assert( Format != NULL );
518
519         /* String mit variablen Argumenten zusammenbauen ... */
520 #ifdef PROTOTYPES
521         va_start( ap, Format );
522 #else
523         va_start( ap );
524 #endif
525         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
526         va_end( ap );
527
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 );
532         else puts( msg );
533 } /* Config_Error */
534
535
536 /* -eof- */