Blob


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.30 2002/09/19 10:19:12 alex Exp $
13 *
14 * conf.h: Konfiguration des ngircd
15 */
18 #include "portab.h"
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>
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"
37 #include "exp.h"
38 #include "conf.h"
41 LOCAL BOOLEAN Use_Log = TRUE;
44 LOCAL VOID Set_Defaults PARAMS(( VOID ));
45 LOCAL VOID Read_Config PARAMS(( VOID ));
46 LOCAL VOID Validate_Config PARAMS(( VOID ));
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 ));
53 LOCAL VOID Config_Error PARAMS(( CONST INT Level, CONST CHAR *Format, ... ));
56 GLOBAL VOID
57 Conf_Init( VOID )
58 {
59 Set_Defaults( );
60 Read_Config( );
61 Validate_Config( );
62 } /* Config_Init */
65 GLOBAL INT
66 Conf_Test( VOID )
67 {
68 /* Konfiguration einlesen, ueberpruefen und ausgeben. */
70 UINT i;
72 Use_Log = FALSE;
73 Set_Defaults( );
75 printf( "Using \"%s\" as configuration file ...\n", NGIRCd_ConfFile );
76 Read_Config( );
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" );
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( " AdminInfo1 = %s\n", Conf_ServerAdmin1 );
91 printf( " AdminInfo2 = %s\n", Conf_ServerAdmin2 );
92 printf( " AdminEMail = %s\n", Conf_ServerAdminMail );
93 printf( " MotdFile = %s\n", Conf_MotdFile );
94 printf( " ListenPorts = " );
95 for( i = 0; i < Conf_ListenPorts_Count; i++ )
96 {
97 if( i != 0 ) printf( ", " );
98 printf( "%u", Conf_ListenPorts[i] );
99 }
100 puts( "" );
101 printf( " ServerUID = %ld\n", (INT32)Conf_UID );
102 printf( " ServerGID = %ld\n", (INT32)Conf_GID );
103 printf( " PingTimeout = %d\n", Conf_PingTimeout );
104 printf( " PongTimeout = %d\n", Conf_PongTimeout );
105 printf( " ConnectRetry = %d\n", Conf_ConnectRetry );
106 printf( " OperCanUseMode = %s\n", Conf_OperCanMode == TRUE ? "yes" : "no" );
107 puts( "" );
109 for( i = 0; i < Conf_Oper_Count; i++ )
111 if( ! Conf_Oper[i].name[0] ) continue;
113 /* gueltiger Operator-Block: ausgeben */
114 puts( "[OPERATOR]" );
115 printf( " Name = %s\n", Conf_Oper[i].name );
116 printf( " Password = %s\n", Conf_Oper[i].pwd );
117 puts( "" );
120 for( i = 0; i < Conf_Server_Count; i++ )
122 if( ! Conf_Server[i].name[0] ) continue;
123 if( ! Conf_Server[i].host[0] ) continue;
125 /* gueltiger Server-Block: ausgeben */
126 puts( "[SERVER]" );
127 printf( " Name = %s\n", Conf_Server[i].name );
128 printf( " Host = %s\n", Conf_Server[i].host );
129 printf( " Port = %d\n", Conf_Server[i].port );
130 printf( " Password = %s\n", Conf_Server[i].pwd );
131 printf( " Group = %d\n", Conf_Server[i].group );
132 puts( "" );
135 for( i = 0; i < Conf_Channel_Count; i++ )
137 if( ! Conf_Channel[i].name[0] ) continue;
139 /* gueltiger Channel-Block: ausgeben */
140 puts( "[CHANNEL]" );
141 printf( " Name = %s\n", Conf_Channel[i].name );
142 printf( " Modes = %s\n", Conf_Channel[i].modes );
143 printf( " Topic = %s\n", Conf_Channel[i].topic );
144 puts( "" );
147 return 0;
148 } /* Conf_Test */
151 LOCAL VOID
152 Set_Defaults( VOID )
154 /* Konfigurationsvariablen initialisieren, d.h. auf Default-Werte setzen. */
156 strcpy( Conf_ServerName, "" );
157 sprintf( Conf_ServerInfo, "%s %s", PACKAGE, VERSION );
158 strcpy( Conf_ServerPwd, "" );
160 strcpy( Conf_ServerAdmin1, "" );
161 strcpy( Conf_ServerAdmin2, "" );
162 strcpy( Conf_ServerAdminMail, "" );
164 strcpy( Conf_MotdFile, MOTD_FILE );
166 Conf_ListenPorts_Count = 0;
168 Conf_UID = Conf_GID = 0;
170 Conf_PingTimeout = 120;
171 Conf_PongTimeout = 20;
173 Conf_ConnectRetry = 60;
175 Conf_Oper_Count = 0;
176 Conf_Server_Count = 0;
177 Conf_Channel_Count = 0;
179 Conf_OperCanMode = FALSE;
180 } /* Set_Defaults */
183 LOCAL VOID
184 Read_Config( VOID )
186 /* Konfigurationsdatei einlesen. */
188 CHAR section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
189 INT line;
190 FILE *fd;
192 fd = fopen( NGIRCd_ConfFile, "r" );
193 if( ! fd )
195 /* Keine Konfigurationsdatei gefunden */
196 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s", NGIRCd_ConfFile, strerror( errno ));
197 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
198 exit( 1 );
201 line = 0;
202 strcpy( section, "" );
203 while( TRUE )
205 if( ! fgets( str, LINE_LEN, fd )) break;
206 ngt_TrimStr( str );
207 line++;
209 /* Kommentarzeilen und leere Zeilen ueberspringen */
210 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
212 /* Anfang eines Abschnittes? */
213 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
215 strcpy( section, str );
216 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
217 if( strcasecmp( section, "[OPERATOR]" ) == 0 )
219 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Config_Error( LOG_ERR, "Too many operators configured." );
220 else
222 /* neuen Operator initialisieren */
223 strcpy( Conf_Oper[Conf_Oper_Count].name, "" );
224 strcpy( Conf_Oper[Conf_Oper_Count].pwd, "" );
225 Conf_Oper_Count++;
227 continue;
229 if( strcasecmp( section, "[SERVER]" ) == 0 )
231 if( Conf_Server_Count + 1 > MAX_SERVERS ) Config_Error( LOG_ERR, "Too many servers configured." );
232 else
234 /* neuen Server ("Peer") initialisieren */
235 strcpy( Conf_Server[Conf_Server_Count].host, "" );
236 strcpy( Conf_Server[Conf_Server_Count].ip, "" );
237 strcpy( Conf_Server[Conf_Server_Count].name, "" );
238 strcpy( Conf_Server[Conf_Server_Count].pwd, "" );
239 Conf_Server[Conf_Server_Count].port = 0;
240 Conf_Server[Conf_Server_Count].group = -1;
241 Conf_Server[Conf_Server_Count].lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
242 Conf_Server[Conf_Server_Count].res_stat = NULL;
243 Conf_Server_Count++;
245 continue;
247 if( strcasecmp( section, "[CHANNEL]" ) == 0 )
249 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
250 else
252 /* neuen vordefinierten Channel initialisieren */
253 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
254 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
255 strcpy( Conf_Channel[Conf_Channel_Count].topic, "" );
256 Conf_Channel_Count++;
258 continue;
260 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
261 section[0] = 0x1;
263 if( section[0] == 0x1 ) continue;
265 /* In Variable und Argument zerlegen */
266 ptr = strchr( str, '=' );
267 if( ! ptr )
269 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
270 continue;
272 *ptr = '\0';
273 var = str; ngt_TrimStr( var );
274 arg = ptr + 1; ngt_TrimStr( arg );
276 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
277 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
278 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
279 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
280 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
283 fclose( fd );
285 /* Wenn kein Port definiert wurde, Port 6667 als Default benutzen */
286 if( Conf_ListenPorts_Count < 1 )
288 Conf_ListenPorts_Count = 1;
289 Conf_ListenPorts[0] = 6667;
291 } /* Read_Config */
294 LOCAL VOID
295 Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
297 CHAR *ptr;
298 INT32 port;
300 assert( Line > 0 );
301 assert( Var != NULL );
302 assert( Arg != NULL );
304 if( strcasecmp( Var, "Name" ) == 0 )
306 /* Der Server-Name */
307 strncpy( Conf_ServerName, Arg, CLIENT_ID_LEN - 1 );
308 Conf_ServerName[CLIENT_ID_LEN - 1] = '\0';
309 return;
311 if( strcasecmp( Var, "Info" ) == 0 )
313 /* Server-Info-Text */
314 strncpy( Conf_ServerInfo, Arg, CLIENT_INFO_LEN - 1 );
315 Conf_ServerInfo[CLIENT_INFO_LEN - 1] = '\0';
316 return;
318 if( strcasecmp( Var, "Password" ) == 0 )
320 /* Server-Passwort */
321 strncpy( Conf_ServerPwd, Arg, CLIENT_PASS_LEN - 1 );
322 Conf_ServerPwd[CLIENT_PASS_LEN - 1] = '\0';
323 return;
325 if( strcasecmp( Var, "AdminInfo1" ) == 0 )
327 /* Server-Info-Text */
328 strncpy( Conf_ServerAdmin1, Arg, CLIENT_INFO_LEN - 1 );
329 Conf_ServerAdmin1[CLIENT_INFO_LEN - 1] = '\0';
330 return;
332 if( strcasecmp( Var, "AdminInfo2" ) == 0 )
334 /* Server-Info-Text */
335 strncpy( Conf_ServerAdmin2, Arg, CLIENT_INFO_LEN - 1 );
336 Conf_ServerAdmin2[CLIENT_INFO_LEN - 1] = '\0';
337 return;
339 if( strcasecmp( Var, "AdminEMail" ) == 0 )
341 /* Server-Info-Text */
342 strncpy( Conf_ServerAdminMail, Arg, CLIENT_INFO_LEN - 1 );
343 Conf_ServerAdminMail[CLIENT_INFO_LEN - 1] = '\0';
344 return;
346 if( strcasecmp( Var, "Ports" ) == 0 )
348 /* Ports, durch "," getrennt, auf denen der Server
349 * Verbindungen annehmen soll */
350 ptr = strtok( Arg, "," );
351 while( ptr )
353 ngt_TrimStr( ptr );
354 port = atol( ptr );
355 if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Config_Error( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
356 else
358 if( port > 0 && port < 0xFFFF ) Conf_ListenPorts[Conf_ListenPorts_Count++] = (UINT)port;
359 else Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
361 ptr = strtok( NULL, "," );
363 return;
365 if( strcasecmp( Var, "MotdFile" ) == 0 )
367 /* Datei mit der "message of the day" (MOTD) */
368 strncpy( Conf_MotdFile, Arg, FNAME_LEN - 1 );
369 Conf_MotdFile[FNAME_LEN - 1] = '\0';
370 return;
372 if( strcasecmp( Var, "ServerUID" ) == 0 )
374 /* UID, mit der der Daemon laufen soll */
375 Conf_UID = (UINT)atoi( Arg );
376 return;
378 if( strcasecmp( Var, "ServerGID" ) == 0 )
380 /* GID, mit der der Daemon laufen soll */
381 Conf_GID = (UINT)atoi( Arg );
382 return;
384 if( strcasecmp( Var, "PingTimeout" ) == 0 )
386 /* PING-Timeout */
387 Conf_PingTimeout = atoi( Arg );
388 if(( Conf_PingTimeout ) < 5 ) Conf_PingTimeout = 5;
389 return;
391 if( strcasecmp( Var, "PongTimeout" ) == 0 )
393 /* PONG-Timeout */
394 Conf_PongTimeout = atoi( Arg );
395 if(( Conf_PongTimeout ) < 5 ) Conf_PongTimeout = 5;
396 return;
398 if( strcasecmp( Var, "ConnectRetry" ) == 0 )
400 /* Sekunden zwischen Verbindungsversuchen zu anderen Servern */
401 Conf_ConnectRetry = atoi( Arg );
402 if(( Conf_ConnectRetry ) < 5 ) Conf_ConnectRetry = 5;
403 return;
405 if( strcasecmp( Var, "OperCanUseMode" ) == 0 )
407 /* Koennen IRC-Operatoren immer MODE benutzen? */
408 if( strcasecmp( Arg, "yes" ) == 0 ) Conf_OperCanMode = TRUE;
409 else if( strcasecmp( Arg, "true" ) == 0 ) Conf_OperCanMode = TRUE;
410 else if( atoi( Arg ) != 0 ) Conf_OperCanMode = TRUE;
411 else Conf_OperCanMode = FALSE;
412 return;
415 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
416 } /* Handle_GLOBAL */
419 LOCAL VOID
420 Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
422 assert( Line > 0 );
423 assert( Var != NULL );
424 assert( Arg != NULL );
425 assert( Conf_Oper_Count > 0 );
427 if( strcasecmp( Var, "Name" ) == 0 )
429 /* Name des IRC Operator */
430 strncpy( Conf_Oper[Conf_Oper_Count - 1].name, Arg, CLIENT_PASS_LEN - 1 );
431 Conf_Oper[Conf_Oper_Count - 1].name[CLIENT_PASS_LEN - 1] = '\0';
432 return;
434 if( strcasecmp( Var, "Password" ) == 0 )
436 /* Passwort des IRC Operator */
437 strncpy( Conf_Oper[Conf_Oper_Count - 1].pwd, Arg, CLIENT_PASS_LEN - 1 );
438 Conf_Oper[Conf_Oper_Count - 1].pwd[CLIENT_PASS_LEN - 1] = '\0';
439 return;
442 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
443 } /* Handle_OPERATOR */
446 LOCAL VOID
447 Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
449 INT32 port;
451 assert( Line > 0 );
452 assert( Var != NULL );
453 assert( Arg != NULL );
455 if( strcasecmp( Var, "Host" ) == 0 )
457 /* Hostname des Servers */
458 strncpy( Conf_Server[Conf_Server_Count - 1].host, Arg, HOST_LEN - 1 );
459 Conf_Server[Conf_Server_Count - 1].host[HOST_LEN - 1] = '\0';
460 return;
462 if( strcasecmp( Var, "Name" ) == 0 )
464 /* Name des Servers ("Nick") */
465 strncpy( Conf_Server[Conf_Server_Count - 1].name, Arg, CLIENT_ID_LEN - 1 );
466 Conf_Server[Conf_Server_Count - 1].name[CLIENT_ID_LEN - 1] = '\0';
467 return;
469 if( strcasecmp( Var, "Password" ) == 0 )
471 /* Passwort des Servers */
472 strncpy( Conf_Server[Conf_Server_Count - 1].pwd, Arg, CLIENT_PASS_LEN - 1 );
473 Conf_Server[Conf_Server_Count - 1].pwd[CLIENT_PASS_LEN - 1] = '\0';
474 return;
476 if( strcasecmp( Var, "Port" ) == 0 )
478 /* Port, zu dem Verbunden werden soll */
479 port = atol( Arg );
480 if( port > 0 && port < 0xFFFF ) Conf_Server[Conf_Server_Count - 1].port = (INT)port;
481 else Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
482 return;
484 if( strcasecmp( Var, "Group" ) == 0 )
486 /* Server-Gruppe */
487 Conf_Server[Conf_Server_Count - 1].group = atoi( Arg );
488 return;
491 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
492 } /* Handle_SERVER */
495 LOCAL VOID
496 Handle_CHANNEL( INT Line, CHAR *Var, CHAR *Arg )
498 assert( Line > 0 );
499 assert( Var != NULL );
500 assert( Arg != NULL );
502 if( strcasecmp( Var, "Name" ) == 0 )
504 /* Hostname des Servers */
505 strncpy( Conf_Channel[Conf_Channel_Count - 1].name, Arg, CHANNEL_NAME_LEN - 1 );
506 Conf_Channel[Conf_Channel_Count - 1].name[CHANNEL_NAME_LEN - 1] = '\0';
507 return;
509 if( strcasecmp( Var, "Modes" ) == 0 )
511 /* Name des Servers ("Nick") */
512 strncpy( Conf_Channel[Conf_Channel_Count - 1].modes, Arg, CHANNEL_MODE_LEN - 1 );
513 Conf_Channel[Conf_Channel_Count - 1].modes[CHANNEL_MODE_LEN - 1] = '\0';
514 return;
516 if( strcasecmp( Var, "Topic" ) == 0 )
518 /* Passwort des Servers */
519 strncpy( Conf_Channel[Conf_Channel_Count - 1].topic, Arg, CHANNEL_TOPIC_LEN - 1 );
520 Conf_Channel[Conf_Channel_Count - 1].topic[CHANNEL_TOPIC_LEN - 1] = '\0';
521 return;
524 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
525 } /* Handle_CHANNEL */
528 LOCAL VOID
529 Validate_Config( VOID )
531 /* Konfiguration ueberpruefen */
533 if( ! Conf_ServerName[0] )
535 /* Kein Servername konfiguriert */
536 Config_Error( LOG_ALERT, "No server name configured in \"%s\" ('ServerName')!", NGIRCd_ConfFile );
537 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
538 exit( 1 );
541 #ifdef STRICT_RFC
542 if( ! ConfAdminMail[0] )
544 /* Keine Server-Information konfiguriert */
545 Config_Error( LOG_ALERT, "No administrator email address configured in \"%s\" ('AdminEMail')!", NGIRCd_ConfFile );
546 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
547 exit( 1 );
549 #endif
551 if( ! Conf_ServerAdmin1[0] && ! Conf_ServerAdmin2[0] && ! Conf_ServerAdminMail[0] )
553 /* Keine Server-Information konfiguriert */
554 Log( LOG_WARNING, "No server information configured but required by RFC!" );
556 } /* Validate_Config */
559 #ifdef PROTOTYPES
560 LOCAL VOID Config_Error( CONST INT Level, CONST CHAR *Format, ... )
561 #else
562 LOCAL VOID Config_Error( Level, Format, va_alist )
563 CONST INT Level;
564 CONST CHAR *Format;
565 va_dcl
566 #endif
568 /* Fehler! Auf Console und/oder ins Log schreiben */
570 CHAR msg[MAX_LOG_MSG_LEN];
571 va_list ap;
573 assert( Format != NULL );
575 /* String mit variablen Argumenten zusammenbauen ... */
576 #ifdef PROTOTYPES
577 va_start( ap, Format );
578 #else
579 va_start( ap );
580 #endif
581 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
582 va_end( ap );
584 /* Im "normalen Betrieb" soll der Log-Mechanismus des ngIRCd verwendet
585 * werden, beim Testen der Konfiguration jedoch nicht, hier sollen alle
586 * Meldungen direkt auf die Konsole ausgegeben werden: */
587 if( Use_Log ) Log( Level, msg );
588 else puts( msg );
589 } /* Config_Error */
592 /* -eof- */