]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conf.c
aab8f068b11168fac0aaff7d582c9b9c491fdae2
[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.25 2002/05/22 11:14:36 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 GLOBAL VOID Handle_CHANNEL( INT Line, CHAR *Var, CHAR *Arg );
50
51 LOCAL VOID Config_Error( CONST INT Level, CONST CHAR *Format, ... );
52
53
54 GLOBAL VOID Conf_Init( VOID )
55 {
56         Set_Defaults( );
57         Read_Config( );
58         Validate_Config( );
59 } /* Config_Init */
60
61
62 GLOBAL INT Conf_Test( VOID )
63 {
64         /* Konfiguration einlesen, ueberpruefen und ausgeben. */
65
66         UINT i;
67
68         Use_Log = FALSE;
69         Set_Defaults( );
70
71         printf( "Using \"%s\" as configuration file ...\n", NGIRCd_ConfFile );
72         Read_Config( );
73
74         /* Wenn stdin ein ein TTY ist: auf Taste warten */
75         if( isatty( fileno( stdout )))
76         {
77                 puts( "OK, press enter to see a dump of your service configuration ..." );
78                 getchar( );
79         }
80         else puts( "Ok, dump of your server configuration follows:\n" );
81
82         puts( "[GLOBAL]" );
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++ )
89         {
90                 if( i != 0 ) printf( ", " );
91                 printf( "%u", Conf_ListenPorts[i] );
92         }
93         puts( "" );
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 );
99         puts( "" );
100
101         for( i = 0; i < Conf_Oper_Count; i++ )
102         {
103                 if( ! Conf_Oper[i].name[0] ) continue;
104                 
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 );
109                 puts( "" );
110         }
111
112         for( i = 0; i < Conf_Server_Count; i++ )
113         {
114                 if( ! Conf_Server[i].name[0] ) continue;
115                 if( ! Conf_Server[i].host[0] ) continue;
116                 
117                 /* gueltiger Server-Block: ausgeben */
118                 puts( "[SERVER]" );
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 );
124                 puts( "" );
125         }
126
127         for( i = 0; i < Conf_Channel_Count; i++ )
128         {
129                 if( ! Conf_Channel[i].name[0] ) continue;
130                 
131                 /* gueltiger Channel-Block: ausgeben */
132                 puts( "[CHANNEL]" );
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 );
136                 puts( "" );
137         }
138         
139         return 0;
140 } /* Conf_Test */
141
142
143 GLOBAL VOID Conf_Exit( VOID )
144 {
145         /* ... */
146 } /* Config_Exit */
147
148
149 LOCAL VOID Set_Defaults( VOID )
150 {
151         /* Konfigurationsvariablen initialisieren, d.h. auf Default-Werte setzen. */
152
153         strcpy( Conf_ServerName, "" );
154         strcpy( Conf_ServerInfo, PACKAGE" "VERSION );
155         strcpy( Conf_ServerPwd, "" );
156
157         strcpy( Conf_MotdFile, MOTD_FILE );
158
159         Conf_ListenPorts_Count = 0;
160
161         Conf_UID = Conf_GID = 0;
162         
163         Conf_PingTimeout = 120;
164         Conf_PongTimeout = 20;
165
166         Conf_ConnectRetry = 60;
167
168         Conf_Oper_Count = 0;
169         Conf_Server_Count = 0;
170         Conf_Channel_Count = 0;
171 } /* Set_Defaults */
172
173
174 LOCAL VOID 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, PACKAGE" exiting due to fatal errors!" );
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 GLOBAL VOID Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
285 {
286         CHAR *ptr;
287         INT32 port;
288         
289         assert( Line > 0 );
290         assert( Var != NULL );
291         assert( Arg != NULL );
292         
293         if( strcasecmp( Var, "Name" ) == 0 )
294         {
295                 /* Der Server-Name */
296                 strncpy( Conf_ServerName, Arg, CLIENT_ID_LEN - 1 );
297                 Conf_ServerName[CLIENT_ID_LEN - 1] = '\0';
298                 return;
299         }
300         if( strcasecmp( Var, "Info" ) == 0 )
301         {
302                 /* Server-Info-Text */
303                 strncpy( Conf_ServerInfo, Arg, CLIENT_INFO_LEN - 1 );
304                 Conf_ServerInfo[CLIENT_INFO_LEN - 1] = '\0';
305                 return;
306         }
307         if( strcasecmp( Var, "Password" ) == 0 )
308         {
309                 /* Server-Passwort */
310                 strncpy( Conf_ServerPwd, Arg, CLIENT_PASS_LEN - 1 );
311                 Conf_ServerPwd[CLIENT_PASS_LEN - 1] = '\0';
312                 return;
313         }
314         if( strcasecmp( Var, "Ports" ) == 0 )
315         {
316                 /* Ports, durch "," getrennt, auf denen der Server
317                 * Verbindungen annehmen soll */
318                 ptr = strtok( Arg, "," );
319                 while( ptr )
320                 {
321                         ngt_TrimStr( ptr );
322                         port = atol( ptr );
323                         if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Config_Error( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
324                         else
325                         {
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 );
328                         }
329                         ptr = strtok( NULL, "," );
330                 }
331                 return;
332         }
333         if( strcasecmp( Var, "MotdFile" ) == 0 )
334         {
335                 /* Datei mit der "message of the day" (MOTD) */
336                 strncpy( Conf_MotdFile, Arg, FNAME_LEN - 1 );
337                 Conf_MotdFile[FNAME_LEN - 1] = '\0';
338                 return;
339         }
340         if( strcasecmp( Var, "ServerUID" ) == 0 )
341         {
342                 /* UID, mit der der Daemon laufen soll */
343                 Conf_UID = (UINT)atoi( Arg );
344                 return;
345         }
346         if( strcasecmp( Var, "ServerGID" ) == 0 )
347         {
348                 /* GID, mit der der Daemon laufen soll */
349                 Conf_GID = (UINT)atoi( Arg );
350                 return;
351         }
352         if( strcasecmp( Var, "PingTimeout" ) == 0 )
353         {
354                 /* PING-Timeout */
355                 Conf_PingTimeout = atoi( Arg );
356                 if(( Conf_PingTimeout ) < 5 ) Conf_PingTimeout = 5;
357                 return;
358         }
359         if( strcasecmp( Var, "PongTimeout" ) == 0 )
360         {
361                 /* PONG-Timeout */
362                 Conf_PongTimeout = atoi( Arg );
363                 if(( Conf_PongTimeout ) < 5 ) Conf_PongTimeout = 5;
364                 return;
365         }
366         if( strcasecmp( Var, "ConnectRetry" ) == 0 )
367         {
368                 /* Sekunden zwischen Verbindungsversuchen zu anderen Servern */
369                 Conf_ConnectRetry = atoi( Arg );
370                 if(( Conf_ConnectRetry ) < 5 ) Conf_ConnectRetry = 5;
371                 return;
372         }
373                 
374         Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
375 } /* Handle_GLOBAL */
376
377
378 GLOBAL VOID Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
379 {
380         assert( Line > 0 );
381         assert( Var != NULL );
382         assert( Arg != NULL );
383         assert( Conf_Oper_Count > 0 );
384
385         if( strcasecmp( Var, "Name" ) == 0 )
386         {
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';
390                 return;
391         }
392         if( strcasecmp( Var, "Password" ) == 0 )
393         {
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';
397                 return;
398         }
399         
400         Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
401 } /* Handle_OPERATOR */
402
403
404 GLOBAL VOID Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
405 {
406         INT32 port;
407         
408         assert( Line > 0 );
409         assert( Var != NULL );
410         assert( Arg != NULL );
411
412         if( strcasecmp( Var, "Host" ) == 0 )
413         {
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';
417                 return;
418         }
419         if( strcasecmp( Var, "Name" ) == 0 )
420         {
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';
424                 return;
425         }
426         if( strcasecmp( Var, "Password" ) == 0 )
427         {
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';
431                 return;
432         }
433         if( strcasecmp( Var, "Port" ) == 0 )
434         {
435                 /* Port, zu dem Verbunden werden soll */
436                 port = atol( Arg );
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 );
439                 return;
440         }
441         if( strcasecmp( Var, "Group" ) == 0 )
442         {
443                 /* Server-Gruppe */
444                 Conf_Server[Conf_Server_Count - 1].group = atoi( Arg );
445                 return;
446         }
447         
448         Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
449 } /* Handle_SERVER */
450
451
452 GLOBAL VOID Handle_CHANNEL( INT Line, CHAR *Var, CHAR *Arg )
453 {
454         assert( Line > 0 );
455         assert( Var != NULL );
456         assert( Arg != NULL );
457
458         if( strcasecmp( Var, "Name" ) == 0 )
459         {
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';
463                 return;
464         }
465         if( strcasecmp( Var, "Modes" ) == 0 )
466         {
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';
470                 return;
471         }
472         if( strcasecmp( Var, "Topic" ) == 0 )
473         {
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';
477                 return;
478         }
479
480         Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
481 } /* Handle_CHANNEL */
482
483
484 LOCAL VOID Validate_Config( VOID )
485 {
486         /* Konfiguration ueberpruefen */
487         
488         if( ! Conf_ServerName[0] )
489         {
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!" );
493                 exit( 1 );
494         }
495 } /* Validate_Config */
496
497
498 LOCAL VOID Config_Error( CONST INT Level, CONST CHAR *Format, ... )
499 {
500         /* Fehler! Auf Console und/oder ins Log schreiben */
501
502         CHAR msg[MAX_LOG_MSG_LEN];
503         va_list ap;
504
505         assert( Format != NULL );
506
507         /* String mit variablen Argumenten zusammenbauen ... */
508         va_start( ap, Format );
509         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
510         va_end( ap );
511
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 );
516         else puts( msg );
517 } /* Config_Error */
518
519
520 /* -eof- */