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