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.35 2002/11/02 22:59:01 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 INT 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", (LONG)Conf_UID );
102 printf( " ServerGID = %ld\n", (LONG)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 if( Conf_MaxConnections > 0 ) printf( " MaxConnections = %ld\n", Conf_MaxConnections );
108 else printf( " MaxConnections = -1\n" );
109 puts( "" );
111 for( i = 0; i < Conf_Oper_Count; i++ )
113 if( ! Conf_Oper[i].name[0] ) continue;
115 /* gueltiger Operator-Block: ausgeben */
116 puts( "[OPERATOR]" );
117 printf( " Name = %s\n", Conf_Oper[i].name );
118 printf( " Password = %s\n", Conf_Oper[i].pwd );
119 puts( "" );
122 for( i = 0; i < Conf_Server_Count; i++ )
124 if( ! Conf_Server[i].name[0] ) continue;
125 if( ! Conf_Server[i].host[0] ) continue;
127 /* gueltiger Server-Block: ausgeben */
128 puts( "[SERVER]" );
129 printf( " Name = %s\n", Conf_Server[i].name );
130 printf( " Host = %s\n", Conf_Server[i].host );
131 printf( " Port = %d\n", Conf_Server[i].port );
132 printf( " Password = %s\n", Conf_Server[i].pwd );
133 printf( " Group = %d\n", Conf_Server[i].group );
134 puts( "" );
137 for( i = 0; i < Conf_Channel_Count; i++ )
139 if( ! Conf_Channel[i].name[0] ) continue;
141 /* gueltiger Channel-Block: ausgeben */
142 puts( "[CHANNEL]" );
143 printf( " Name = %s\n", Conf_Channel[i].name );
144 printf( " Modes = %s\n", Conf_Channel[i].modes );
145 printf( " Topic = %s\n", Conf_Channel[i].topic );
146 puts( "" );
149 return 0;
150 } /* Conf_Test */
153 LOCAL VOID
154 Set_Defaults( VOID )
156 /* Konfigurationsvariablen initialisieren, d.h. auf Default-Werte setzen. */
158 strcpy( Conf_ServerName, "" );
159 sprintf( Conf_ServerInfo, "%s %s", PACKAGE, VERSION );
160 strcpy( Conf_ServerPwd, "" );
162 strcpy( Conf_ServerAdmin1, "" );
163 strcpy( Conf_ServerAdmin2, "" );
164 strcpy( Conf_ServerAdminMail, "" );
166 strcpy( Conf_MotdFile, MOTD_FILE );
168 Conf_ListenPorts_Count = 0;
170 Conf_UID = Conf_GID = 0;
172 Conf_PingTimeout = 120;
173 Conf_PongTimeout = 20;
175 Conf_ConnectRetry = 60;
177 Conf_Oper_Count = 0;
178 Conf_Server_Count = 0;
179 Conf_Channel_Count = 0;
181 Conf_OperCanMode = FALSE;
183 Conf_MaxConnections = 0;
184 } /* Set_Defaults */
187 LOCAL VOID
188 Read_Config( VOID )
190 /* Konfigurationsdatei einlesen. */
192 CHAR section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
193 INT line;
194 FILE *fd;
196 fd = fopen( NGIRCd_ConfFile, "r" );
197 if( ! fd )
199 /* Keine Konfigurationsdatei gefunden */
200 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s", NGIRCd_ConfFile, strerror( errno ));
201 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
202 exit( 1 );
205 line = 0;
206 strcpy( section, "" );
207 while( TRUE )
209 if( ! fgets( str, LINE_LEN, fd )) break;
210 ngt_TrimStr( str );
211 line++;
213 /* Kommentarzeilen und leere Zeilen ueberspringen */
214 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
216 /* Anfang eines Abschnittes? */
217 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
219 strcpy( section, str );
220 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
221 if( strcasecmp( section, "[OPERATOR]" ) == 0 )
223 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Config_Error( LOG_ERR, "Too many operators configured." );
224 else
226 /* neuen Operator initialisieren */
227 strcpy( Conf_Oper[Conf_Oper_Count].name, "" );
228 strcpy( Conf_Oper[Conf_Oper_Count].pwd, "" );
229 Conf_Oper_Count++;
231 continue;
233 if( strcasecmp( section, "[SERVER]" ) == 0 )
235 if( Conf_Server_Count + 1 > MAX_SERVERS ) Config_Error( LOG_ERR, "Too many servers configured." );
236 else
238 /* neuen Server ("Peer") initialisieren */
239 strcpy( Conf_Server[Conf_Server_Count].host, "" );
240 strcpy( Conf_Server[Conf_Server_Count].ip, "" );
241 strcpy( Conf_Server[Conf_Server_Count].name, "" );
242 strcpy( Conf_Server[Conf_Server_Count].pwd, "" );
243 Conf_Server[Conf_Server_Count].port = 0;
244 Conf_Server[Conf_Server_Count].group = -1;
245 Conf_Server[Conf_Server_Count].lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
246 Conf_Server[Conf_Server_Count].res_stat = NULL;
247 Conf_Server_Count++;
249 continue;
251 if( strcasecmp( section, "[CHANNEL]" ) == 0 )
253 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
254 else
256 /* neuen vordefinierten Channel initialisieren */
257 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
258 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
259 strcpy( Conf_Channel[Conf_Channel_Count].topic, "" );
260 Conf_Channel_Count++;
262 continue;
264 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
265 section[0] = 0x1;
267 if( section[0] == 0x1 ) continue;
269 /* In Variable und Argument zerlegen */
270 ptr = strchr( str, '=' );
271 if( ! ptr )
273 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
274 continue;
276 *ptr = '\0';
277 var = str; ngt_TrimStr( var );
278 arg = ptr + 1; ngt_TrimStr( arg );
280 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
281 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
282 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
283 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
284 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
287 fclose( fd );
289 /* Wenn kein Port definiert wurde, Port 6667 als Default benutzen */
290 if( Conf_ListenPorts_Count < 1 )
292 Conf_ListenPorts_Count = 1;
293 Conf_ListenPorts[0] = 6667;
295 } /* Read_Config */
298 LOCAL VOID
299 Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
301 CHAR *ptr;
302 LONG port;
304 assert( Line > 0 );
305 assert( Var != NULL );
306 assert( Arg != NULL );
308 if( strcasecmp( Var, "Name" ) == 0 )
310 /* Der Server-Name */
311 strncpy( Conf_ServerName, Arg, CLIENT_ID_LEN - 1 );
312 Conf_ServerName[CLIENT_ID_LEN - 1] = '\0';
313 return;
315 if( strcasecmp( Var, "Info" ) == 0 )
317 /* Server-Info-Text */
318 strncpy( Conf_ServerInfo, Arg, CLIENT_INFO_LEN - 1 );
319 Conf_ServerInfo[CLIENT_INFO_LEN - 1] = '\0';
320 return;
322 if( strcasecmp( Var, "Password" ) == 0 )
324 /* Server-Passwort */
325 strncpy( Conf_ServerPwd, Arg, CLIENT_PASS_LEN - 1 );
326 Conf_ServerPwd[CLIENT_PASS_LEN - 1] = '\0';
327 return;
329 if( strcasecmp( Var, "AdminInfo1" ) == 0 )
331 /* Server-Info-Text */
332 strncpy( Conf_ServerAdmin1, Arg, CLIENT_INFO_LEN - 1 );
333 Conf_ServerAdmin1[CLIENT_INFO_LEN - 1] = '\0';
334 return;
336 if( strcasecmp( Var, "AdminInfo2" ) == 0 )
338 /* Server-Info-Text */
339 strncpy( Conf_ServerAdmin2, Arg, CLIENT_INFO_LEN - 1 );
340 Conf_ServerAdmin2[CLIENT_INFO_LEN - 1] = '\0';
341 return;
343 if( strcasecmp( Var, "AdminEMail" ) == 0 )
345 /* Server-Info-Text */
346 strncpy( Conf_ServerAdminMail, Arg, CLIENT_INFO_LEN - 1 );
347 Conf_ServerAdminMail[CLIENT_INFO_LEN - 1] = '\0';
348 return;
350 if( strcasecmp( Var, "Ports" ) == 0 )
352 /* Ports, durch "," getrennt, auf denen der Server
353 * Verbindungen annehmen soll */
354 ptr = strtok( Arg, "," );
355 while( ptr )
357 ngt_TrimStr( ptr );
358 port = atol( ptr );
359 if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Config_Error( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
360 else
362 if( port > 0 && port < 0xFFFF ) Conf_ListenPorts[Conf_ListenPorts_Count++] = (UINT)port;
363 else Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
365 ptr = strtok( NULL, "," );
367 return;
369 if( strcasecmp( Var, "MotdFile" ) == 0 )
371 /* Datei mit der "message of the day" (MOTD) */
372 strncpy( Conf_MotdFile, Arg, FNAME_LEN - 1 );
373 Conf_MotdFile[FNAME_LEN - 1] = '\0';
374 return;
376 if( strcasecmp( Var, "ServerUID" ) == 0 )
378 /* UID, mit der der Daemon laufen soll */
379 Conf_UID = (UINT)atoi( Arg );
380 return;
382 if( strcasecmp( Var, "ServerGID" ) == 0 )
384 /* GID, mit der der Daemon laufen soll */
385 Conf_GID = (UINT)atoi( Arg );
386 return;
388 if( strcasecmp( Var, "PingTimeout" ) == 0 )
390 /* PING-Timeout */
391 Conf_PingTimeout = atoi( Arg );
392 if(( Conf_PingTimeout ) < 5 ) Conf_PingTimeout = 5;
393 return;
395 if( strcasecmp( Var, "PongTimeout" ) == 0 )
397 /* PONG-Timeout */
398 Conf_PongTimeout = atoi( Arg );
399 if(( Conf_PongTimeout ) < 5 ) Conf_PongTimeout = 5;
400 return;
402 if( strcasecmp( Var, "ConnectRetry" ) == 0 )
404 /* Sekunden zwischen Verbindungsversuchen zu anderen Servern */
405 Conf_ConnectRetry = atoi( Arg );
406 if(( Conf_ConnectRetry ) < 5 ) Conf_ConnectRetry = 5;
407 return;
409 if( strcasecmp( Var, "OperCanUseMode" ) == 0 )
411 /* Koennen IRC-Operatoren immer MODE benutzen? */
412 if( strcasecmp( Arg, "yes" ) == 0 ) Conf_OperCanMode = TRUE;
413 else if( strcasecmp( Arg, "true" ) == 0 ) Conf_OperCanMode = TRUE;
414 else if( atoi( Arg ) != 0 ) Conf_OperCanMode = TRUE;
415 else Conf_OperCanMode = FALSE;
416 return;
418 if( strcasecmp( Var, "MaxConnections" ) == 0 )
420 /* Maximale Anzahl von Verbindungen. Werte <= 0 stehen
421 * fuer "kein Limit". */
422 Conf_MaxConnections = atol( Arg );
423 return;
426 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
427 } /* Handle_GLOBAL */
430 LOCAL VOID
431 Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
433 assert( Line > 0 );
434 assert( Var != NULL );
435 assert( Arg != NULL );
436 assert( Conf_Oper_Count > 0 );
438 if( strcasecmp( Var, "Name" ) == 0 )
440 /* Name des IRC Operator */
441 strncpy( Conf_Oper[Conf_Oper_Count - 1].name, Arg, CLIENT_PASS_LEN - 1 );
442 Conf_Oper[Conf_Oper_Count - 1].name[CLIENT_PASS_LEN - 1] = '\0';
443 return;
445 if( strcasecmp( Var, "Password" ) == 0 )
447 /* Passwort des IRC Operator */
448 strncpy( Conf_Oper[Conf_Oper_Count - 1].pwd, Arg, CLIENT_PASS_LEN - 1 );
449 Conf_Oper[Conf_Oper_Count - 1].pwd[CLIENT_PASS_LEN - 1] = '\0';
450 return;
453 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
454 } /* Handle_OPERATOR */
457 LOCAL VOID
458 Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
460 LONG port;
462 assert( Line > 0 );
463 assert( Var != NULL );
464 assert( Arg != NULL );
466 if( strcasecmp( Var, "Host" ) == 0 )
468 /* Hostname des Servers */
469 strncpy( Conf_Server[Conf_Server_Count - 1].host, Arg, HOST_LEN - 1 );
470 Conf_Server[Conf_Server_Count - 1].host[HOST_LEN - 1] = '\0';
471 return;
473 if( strcasecmp( Var, "Name" ) == 0 )
475 /* Name des Servers ("Nick") */
476 strncpy( Conf_Server[Conf_Server_Count - 1].name, Arg, CLIENT_ID_LEN - 1 );
477 Conf_Server[Conf_Server_Count - 1].name[CLIENT_ID_LEN - 1] = '\0';
478 return;
480 if( strcasecmp( Var, "Password" ) == 0 )
482 /* Passwort des Servers */
483 strncpy( Conf_Server[Conf_Server_Count - 1].pwd, Arg, CLIENT_PASS_LEN - 1 );
484 Conf_Server[Conf_Server_Count - 1].pwd[CLIENT_PASS_LEN - 1] = '\0';
485 return;
487 if( strcasecmp( Var, "Port" ) == 0 )
489 /* Port, zu dem Verbunden werden soll */
490 port = atol( Arg );
491 if( port > 0 && port < 0xFFFF ) Conf_Server[Conf_Server_Count - 1].port = (INT)port;
492 else Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
493 return;
495 if( strcasecmp( Var, "Group" ) == 0 )
497 /* Server-Gruppe */
498 Conf_Server[Conf_Server_Count - 1].group = atoi( Arg );
499 return;
502 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
503 } /* Handle_SERVER */
506 LOCAL VOID
507 Handle_CHANNEL( INT Line, CHAR *Var, CHAR *Arg )
509 assert( Line > 0 );
510 assert( Var != NULL );
511 assert( Arg != NULL );
513 if( strcasecmp( Var, "Name" ) == 0 )
515 /* Hostname des Servers */
516 strncpy( Conf_Channel[Conf_Channel_Count - 1].name, Arg, CHANNEL_NAME_LEN - 1 );
517 Conf_Channel[Conf_Channel_Count - 1].name[CHANNEL_NAME_LEN - 1] = '\0';
518 return;
520 if( strcasecmp( Var, "Modes" ) == 0 )
522 /* Name des Servers ("Nick") */
523 strncpy( Conf_Channel[Conf_Channel_Count - 1].modes, Arg, CHANNEL_MODE_LEN - 1 );
524 Conf_Channel[Conf_Channel_Count - 1].modes[CHANNEL_MODE_LEN - 1] = '\0';
525 return;
527 if( strcasecmp( Var, "Topic" ) == 0 )
529 /* Passwort des Servers */
530 strncpy( Conf_Channel[Conf_Channel_Count - 1].topic, Arg, CHANNEL_TOPIC_LEN - 1 );
531 Conf_Channel[Conf_Channel_Count - 1].topic[CHANNEL_TOPIC_LEN - 1] = '\0';
532 return;
535 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
536 } /* Handle_CHANNEL */
539 LOCAL VOID
540 Validate_Config( VOID )
542 /* Konfiguration ueberpruefen */
544 if( ! Conf_ServerName[0] )
546 /* Kein Servername konfiguriert */
547 Config_Error( LOG_ALERT, "No server name configured in \"%s\" ('ServerName')!", NGIRCd_ConfFile );
548 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
549 exit( 1 );
552 #ifdef STRICT_RFC
553 if( ! Conf_ServerAdminMail[0] )
555 /* Keine Server-Information konfiguriert */
556 Config_Error( LOG_ALERT, "No administrator email address configured in \"%s\" ('AdminEMail')!", NGIRCd_ConfFile );
557 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
558 exit( 1 );
560 #endif
562 if( ! Conf_ServerAdmin1[0] && ! Conf_ServerAdmin2[0] && ! Conf_ServerAdminMail[0] )
564 /* Keine Server-Information konfiguriert */
565 Log( LOG_WARNING, "No server information configured but required by RFC!" );
567 } /* Validate_Config */
570 #ifdef PROTOTYPES
571 LOCAL VOID Config_Error( CONST INT Level, CONST CHAR *Format, ... )
572 #else
573 LOCAL VOID Config_Error( Level, Format, va_alist )
574 CONST INT Level;
575 CONST CHAR *Format;
576 va_dcl
577 #endif
579 /* Fehler! Auf Console und/oder ins Log schreiben */
581 CHAR msg[MAX_LOG_MSG_LEN];
582 va_list ap;
584 assert( Format != NULL );
586 /* String mit variablen Argumenten zusammenbauen ... */
587 #ifdef PROTOTYPES
588 va_start( ap, Format );
589 #else
590 va_start( ap );
591 #endif
592 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
593 va_end( ap );
595 /* Im "normalen Betrieb" soll der Log-Mechanismus des ngIRCd verwendet
596 * werden, beim Testen der Konfiguration jedoch nicht, hier sollen alle
597 * Meldungen direkt auf die Konsole ausgegeben werden: */
598 if( Use_Log ) Log( Level, "%s", msg );
599 else puts( msg );
600 } /* Config_Error */
603 /* -eof- */