]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conf.c
a1f8fdc37e35576f8de938c49a19b5bf4874141c
[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.28 2002/09/02 14:59:17 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         printf( "  OperCanUseMode = %s\n", Conf_OperCanMode == TRUE ? "yes" : "no" );
104         puts( "" );
105
106         for( i = 0; i < Conf_Oper_Count; i++ )
107         {
108                 if( ! Conf_Oper[i].name[0] ) continue;
109                 
110                 /* gueltiger Operator-Block: ausgeben */
111                 puts( "[OPERATOR]" );
112                 printf( "  Name = %s\n", Conf_Oper[i].name );
113                 printf( "  Password = %s\n", Conf_Oper[i].pwd );
114                 puts( "" );
115         }
116
117         for( i = 0; i < Conf_Server_Count; i++ )
118         {
119                 if( ! Conf_Server[i].name[0] ) continue;
120                 if( ! Conf_Server[i].host[0] ) continue;
121                 
122                 /* gueltiger Server-Block: ausgeben */
123                 puts( "[SERVER]" );
124                 printf( "  Name = %s\n", Conf_Server[i].name );
125                 printf( "  Host = %s\n", Conf_Server[i].host );
126                 printf( "  Port = %d\n", Conf_Server[i].port );
127                 printf( "  Password = %s\n", Conf_Server[i].pwd );
128                 printf( "  Group = %d\n", Conf_Server[i].group );
129                 puts( "" );
130         }
131
132         for( i = 0; i < Conf_Channel_Count; i++ )
133         {
134                 if( ! Conf_Channel[i].name[0] ) continue;
135                 
136                 /* gueltiger Channel-Block: ausgeben */
137                 puts( "[CHANNEL]" );
138                 printf( "  Name = %s\n", Conf_Channel[i].name );
139                 printf( "  Modes = %s\n", Conf_Channel[i].modes );
140                 printf( "  Topic = %s\n", Conf_Channel[i].topic );
141                 puts( "" );
142         }
143         
144         return 0;
145 } /* Conf_Test */
146
147
148 LOCAL VOID
149 Set_Defaults( VOID )
150 {
151         /* Konfigurationsvariablen initialisieren, d.h. auf Default-Werte setzen. */
152
153         strcpy( Conf_ServerName, "" );
154         sprintf( Conf_ServerInfo, "%s %s", 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
172         Conf_OperCanMode = FALSE;
173 } /* Set_Defaults */
174
175
176 LOCAL VOID
177 Read_Config( VOID )
178 {
179         /* Konfigurationsdatei einlesen. */
180
181         CHAR section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
182         INT line;
183         FILE *fd;
184
185         fd = fopen( NGIRCd_ConfFile, "r" );
186         if( ! fd )
187         {
188                 /* Keine Konfigurationsdatei gefunden */
189                 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s", NGIRCd_ConfFile, strerror( errno ));
190                 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
191                 exit( 1 );
192         }
193
194         line = 0;
195         strcpy( section, "" );
196         while( TRUE )
197         {
198                 if( ! fgets( str, LINE_LEN, fd )) break;
199                 ngt_TrimStr( str );
200                 line++;
201
202                 /* Kommentarzeilen und leere Zeilen ueberspringen */
203                 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
204
205                 /* Anfang eines Abschnittes? */
206                 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
207                 {
208                         strcpy( section, str );
209                         if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
210                         if( strcasecmp( section, "[OPERATOR]" ) == 0 )
211                         {
212                                 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Config_Error( LOG_ERR, "Too many operators configured." );
213                                 else
214                                 {
215                                         /* neuen Operator initialisieren */
216                                         strcpy( Conf_Oper[Conf_Oper_Count].name, "" );
217                                         strcpy( Conf_Oper[Conf_Oper_Count].pwd, "" );
218                                         Conf_Oper_Count++;
219                                 }
220                                 continue;
221                         }
222                         if( strcasecmp( section, "[SERVER]" ) == 0 )
223                         {
224                                 if( Conf_Server_Count + 1 > MAX_SERVERS ) Config_Error( LOG_ERR, "Too many servers configured." );
225                                 else
226                                 {
227                                         /* neuen Server ("Peer") initialisieren */
228                                         strcpy( Conf_Server[Conf_Server_Count].host, "" );
229                                         strcpy( Conf_Server[Conf_Server_Count].ip, "" );
230                                         strcpy( Conf_Server[Conf_Server_Count].name, "" );
231                                         strcpy( Conf_Server[Conf_Server_Count].pwd, "" );
232                                         Conf_Server[Conf_Server_Count].port = 0;
233                                         Conf_Server[Conf_Server_Count].group = -1;
234                                         Conf_Server[Conf_Server_Count].lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
235                                         Conf_Server[Conf_Server_Count].res_stat = NULL;
236                                         Conf_Server_Count++;
237                                 }
238                                 continue;
239                         }
240                         if( strcasecmp( section, "[CHANNEL]" ) == 0 )
241                         {
242                                 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
243                                 else
244                                 {
245                                         /* neuen vordefinierten Channel initialisieren */
246                                         strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
247                                         strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
248                                         strcpy( Conf_Channel[Conf_Channel_Count].topic, "" );
249                                         Conf_Channel_Count++;
250                                 }
251                                 continue;
252                         }
253                         Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
254                         section[0] = 0x1;
255                 }
256                 if( section[0] == 0x1 ) continue;
257
258                 /* In Variable und Argument zerlegen */
259                 ptr = strchr( str, '=' );
260                 if( ! ptr )
261                 {
262                         Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
263                         continue;
264                 }
265                 *ptr = '\0';
266                 var = str; ngt_TrimStr( var );
267                 arg = ptr + 1; ngt_TrimStr( arg );
268
269                 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
270                 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
271                 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
272                 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
273                 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
274         }
275         
276         fclose( fd );
277         
278         /* Wenn kein Port definiert wurde, Port 6667 als Default benutzen */
279         if( Conf_ListenPorts_Count < 1 )
280         {
281                 Conf_ListenPorts_Count = 1;
282                 Conf_ListenPorts[0] = 6667;
283         }
284 } /* Read_Config */
285
286
287 LOCAL VOID
288 Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
289 {
290         CHAR *ptr;
291         INT32 port;
292         
293         assert( Line > 0 );
294         assert( Var != NULL );
295         assert( Arg != NULL );
296         
297         if( strcasecmp( Var, "Name" ) == 0 )
298         {
299                 /* Der Server-Name */
300                 strncpy( Conf_ServerName, Arg, CLIENT_ID_LEN - 1 );
301                 Conf_ServerName[CLIENT_ID_LEN - 1] = '\0';
302                 return;
303         }
304         if( strcasecmp( Var, "Info" ) == 0 )
305         {
306                 /* Server-Info-Text */
307                 strncpy( Conf_ServerInfo, Arg, CLIENT_INFO_LEN - 1 );
308                 Conf_ServerInfo[CLIENT_INFO_LEN - 1] = '\0';
309                 return;
310         }
311         if( strcasecmp( Var, "Password" ) == 0 )
312         {
313                 /* Server-Passwort */
314                 strncpy( Conf_ServerPwd, Arg, CLIENT_PASS_LEN - 1 );
315                 Conf_ServerPwd[CLIENT_PASS_LEN - 1] = '\0';
316                 return;
317         }
318         if( strcasecmp( Var, "Ports" ) == 0 )
319         {
320                 /* Ports, durch "," getrennt, auf denen der Server
321                 * Verbindungen annehmen soll */
322                 ptr = strtok( Arg, "," );
323                 while( ptr )
324                 {
325                         ngt_TrimStr( ptr );
326                         port = atol( ptr );
327                         if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Config_Error( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
328                         else
329                         {
330                                 if( port > 0 && port < 0xFFFF ) Conf_ListenPorts[Conf_ListenPorts_Count++] = (UINT)port;
331                                 else Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
332                         }
333                         ptr = strtok( NULL, "," );
334                 }
335                 return;
336         }
337         if( strcasecmp( Var, "MotdFile" ) == 0 )
338         {
339                 /* Datei mit der "message of the day" (MOTD) */
340                 strncpy( Conf_MotdFile, Arg, FNAME_LEN - 1 );
341                 Conf_MotdFile[FNAME_LEN - 1] = '\0';
342                 return;
343         }
344         if( strcasecmp( Var, "ServerUID" ) == 0 )
345         {
346                 /* UID, mit der der Daemon laufen soll */
347                 Conf_UID = (UINT)atoi( Arg );
348                 return;
349         }
350         if( strcasecmp( Var, "ServerGID" ) == 0 )
351         {
352                 /* GID, mit der der Daemon laufen soll */
353                 Conf_GID = (UINT)atoi( Arg );
354                 return;
355         }
356         if( strcasecmp( Var, "PingTimeout" ) == 0 )
357         {
358                 /* PING-Timeout */
359                 Conf_PingTimeout = atoi( Arg );
360                 if(( Conf_PingTimeout ) < 5 ) Conf_PingTimeout = 5;
361                 return;
362         }
363         if( strcasecmp( Var, "PongTimeout" ) == 0 )
364         {
365                 /* PONG-Timeout */
366                 Conf_PongTimeout = atoi( Arg );
367                 if(( Conf_PongTimeout ) < 5 ) Conf_PongTimeout = 5;
368                 return;
369         }
370         if( strcasecmp( Var, "ConnectRetry" ) == 0 )
371         {
372                 /* Sekunden zwischen Verbindungsversuchen zu anderen Servern */
373                 Conf_ConnectRetry = atoi( Arg );
374                 if(( Conf_ConnectRetry ) < 5 ) Conf_ConnectRetry = 5;
375                 return;
376         }
377         if( strcasecmp( Var, "OperCanUseMode" ) == 0 )
378         {
379                 /* Koennen IRC-Operatoren immer MODE benutzen? */
380                 if( strcasecmp( Arg, "yes" ) == 0 ) Conf_OperCanMode = TRUE;
381                 else if( strcasecmp( Arg, "true" ) == 0 ) Conf_OperCanMode = TRUE;
382                 else if( atoi( Arg ) != 0 ) Conf_OperCanMode = TRUE;
383                 else Conf_OperCanMode = FALSE;
384                 return;
385         }
386                 
387         Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
388 } /* Handle_GLOBAL */
389
390
391 LOCAL VOID
392 Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
393 {
394         assert( Line > 0 );
395         assert( Var != NULL );
396         assert( Arg != NULL );
397         assert( Conf_Oper_Count > 0 );
398
399         if( strcasecmp( Var, "Name" ) == 0 )
400         {
401                 /* Name des IRC Operator */
402                 strncpy( Conf_Oper[Conf_Oper_Count - 1].name, Arg, CLIENT_ID_LEN - 1 );
403                 Conf_Oper[Conf_Oper_Count - 1].name[CLIENT_ID_LEN - 1] = '\0';
404                 return;
405         }
406         if( strcasecmp( Var, "Password" ) == 0 )
407         {
408                 /* Passwort des IRC Operator */
409                 strncpy( Conf_Oper[Conf_Oper_Count - 1].pwd, Arg, CLIENT_PASS_LEN - 1 );
410                 Conf_Oper[Conf_Oper_Count - 1].pwd[CLIENT_PASS_LEN - 1] = '\0';
411                 return;
412         }
413         
414         Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
415 } /* Handle_OPERATOR */
416
417
418 LOCAL VOID
419 Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
420 {
421         INT32 port;
422         
423         assert( Line > 0 );
424         assert( Var != NULL );
425         assert( Arg != NULL );
426
427         if( strcasecmp( Var, "Host" ) == 0 )
428         {
429                 /* Hostname des Servers */
430                 strncpy( Conf_Server[Conf_Server_Count - 1].host, Arg, HOST_LEN - 1 );
431                 Conf_Server[Conf_Server_Count - 1].host[HOST_LEN - 1] = '\0';
432                 return;
433         }
434         if( strcasecmp( Var, "Name" ) == 0 )
435         {
436                 /* Name des Servers ("Nick") */
437                 strncpy( Conf_Server[Conf_Server_Count - 1].name, Arg, CLIENT_ID_LEN - 1 );
438                 Conf_Server[Conf_Server_Count - 1].name[CLIENT_ID_LEN - 1] = '\0';
439                 return;
440         }
441         if( strcasecmp( Var, "Password" ) == 0 )
442         {
443                 /* Passwort des Servers */
444                 strncpy( Conf_Server[Conf_Server_Count - 1].pwd, Arg, CLIENT_PASS_LEN - 1 );
445                 Conf_Server[Conf_Server_Count - 1].pwd[CLIENT_PASS_LEN - 1] = '\0';
446                 return;
447         }
448         if( strcasecmp( Var, "Port" ) == 0 )
449         {
450                 /* Port, zu dem Verbunden werden soll */
451                 port = atol( Arg );
452                 if( port > 0 && port < 0xFFFF ) Conf_Server[Conf_Server_Count - 1].port = (INT)port;
453                 else Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
454                 return;
455         }
456         if( strcasecmp( Var, "Group" ) == 0 )
457         {
458                 /* Server-Gruppe */
459                 Conf_Server[Conf_Server_Count - 1].group = atoi( Arg );
460                 return;
461         }
462         
463         Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
464 } /* Handle_SERVER */
465
466
467 LOCAL VOID
468 Handle_CHANNEL( INT Line, CHAR *Var, CHAR *Arg )
469 {
470         assert( Line > 0 );
471         assert( Var != NULL );
472         assert( Arg != NULL );
473
474         if( strcasecmp( Var, "Name" ) == 0 )
475         {
476                 /* Hostname des Servers */
477                 strncpy( Conf_Channel[Conf_Channel_Count - 1].name, Arg, CHANNEL_NAME_LEN - 1 );
478                 Conf_Channel[Conf_Channel_Count - 1].name[CHANNEL_NAME_LEN - 1] = '\0';
479                 return;
480         }
481         if( strcasecmp( Var, "Modes" ) == 0 )
482         {
483                 /* Name des Servers ("Nick") */
484                 strncpy( Conf_Channel[Conf_Channel_Count - 1].modes, Arg, CHANNEL_MODE_LEN - 1 );
485                 Conf_Channel[Conf_Channel_Count - 1].modes[CHANNEL_MODE_LEN - 1] = '\0';
486                 return;
487         }
488         if( strcasecmp( Var, "Topic" ) == 0 )
489         {
490                 /* Passwort des Servers */
491                 strncpy( Conf_Channel[Conf_Channel_Count - 1].topic, Arg, CHANNEL_TOPIC_LEN - 1 );
492                 Conf_Channel[Conf_Channel_Count - 1].topic[CHANNEL_TOPIC_LEN - 1] = '\0';
493                 return;
494         }
495
496         Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
497 } /* Handle_CHANNEL */
498
499
500 LOCAL VOID
501 Validate_Config( VOID )
502 {
503         /* Konfiguration ueberpruefen */
504         
505         if( ! Conf_ServerName[0] )
506         {
507                 /* Kein Servername konfiguriert */
508                 Config_Error( LOG_ALERT, "No server name configured in \"%s\"!", NGIRCd_ConfFile );
509                 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
510                 exit( 1 );
511         }
512 } /* Validate_Config */
513
514
515 #ifdef PROTOTYPES
516 LOCAL VOID Config_Error( CONST INT Level, CONST CHAR *Format, ... )
517 #else
518 LOCAL VOID Config_Error( Level, Format, va_alist )
519 CONST INT Level;
520 CONST CHAR *Format;
521 va_dcl
522 #endif
523 {
524         /* Fehler! Auf Console und/oder ins Log schreiben */
525
526         CHAR msg[MAX_LOG_MSG_LEN];
527         va_list ap;
528
529         assert( Format != NULL );
530
531         /* String mit variablen Argumenten zusammenbauen ... */
532 #ifdef PROTOTYPES
533         va_start( ap, Format );
534 #else
535         va_start( ap );
536 #endif
537         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
538         va_end( ap );
539
540         /* Im "normalen Betrieb" soll der Log-Mechanismus des ngIRCd verwendet
541          * werden, beim Testen der Konfiguration jedoch nicht, hier sollen alle
542          * Meldungen direkt auf die Konsole ausgegeben werden: */
543         if( Use_Log ) Log( Level, msg );
544         else puts( msg );
545 } /* Config_Error */
546
547
548 /* -eof- */