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