2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
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.
12 * $Id: conf.c,v 1.28 2002/09/02 14:59:17 alex Exp $
14 * conf.h: Konfiguration des ngircd
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, ... ));
68 /* Konfiguration einlesen, ueberpruefen und ausgeben. */
75 printf( "Using \"%s\" as configuration file ...\n", NGIRCd_ConfFile );
78 /* Wenn stdin ein ein TTY ist: auf Taste warten */
79 if( isatty( fileno( stdout )))
81 puts( "OK, press enter to see a dump of your service configuration ..." );
84 else puts( "Ok, dump of your server configuration follows:\n" );
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++ )
94 if( i != 0 ) printf( ", " );
95 printf( "%u", Conf_ListenPorts[i] );
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" );
106 for( i = 0; i < Conf_Oper_Count; i++ )
108 if( ! Conf_Oper[i].name[0] ) continue;
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 );
117 for( i = 0; i < Conf_Server_Count; i++ )
119 if( ! Conf_Server[i].name[0] ) continue;
120 if( ! Conf_Server[i].host[0] ) continue;
122 /* gueltiger Server-Block: ausgeben */
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 );
132 for( i = 0; i < Conf_Channel_Count; i++ )
134 if( ! Conf_Channel[i].name[0] ) continue;
136 /* gueltiger Channel-Block: ausgeben */
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 );
151 /* Konfigurationsvariablen initialisieren, d.h. auf Default-Werte setzen. */
153 strcpy( Conf_ServerName, "" );
154 sprintf( Conf_ServerInfo, "%s %s", PACKAGE, VERSION );
155 strcpy( Conf_ServerPwd, "" );
157 strcpy( Conf_MotdFile, MOTD_FILE );
159 Conf_ListenPorts_Count = 0;
161 Conf_UID = Conf_GID = 0;
163 Conf_PingTimeout = 120;
164 Conf_PongTimeout = 20;
166 Conf_ConnectRetry = 60;
169 Conf_Server_Count = 0;
170 Conf_Channel_Count = 0;
172 Conf_OperCanMode = FALSE;
179 /* Konfigurationsdatei einlesen. */
181 CHAR section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
185 fd = fopen( NGIRCd_ConfFile, "r" );
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 );
195 strcpy( section, "" );
198 if( ! fgets( str, LINE_LEN, fd )) break;
202 /* Kommentarzeilen und leere Zeilen ueberspringen */
203 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
205 /* Anfang eines Abschnittes? */
206 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
208 strcpy( section, str );
209 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
210 if( strcasecmp( section, "[OPERATOR]" ) == 0 )
212 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Config_Error( LOG_ERR, "Too many operators configured." );
215 /* neuen Operator initialisieren */
216 strcpy( Conf_Oper[Conf_Oper_Count].name, "" );
217 strcpy( Conf_Oper[Conf_Oper_Count].pwd, "" );
222 if( strcasecmp( section, "[SERVER]" ) == 0 )
224 if( Conf_Server_Count + 1 > MAX_SERVERS ) Config_Error( LOG_ERR, "Too many servers configured." );
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;
240 if( strcasecmp( section, "[CHANNEL]" ) == 0 )
242 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
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++;
253 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
256 if( section[0] == 0x1 ) continue;
258 /* In Variable und Argument zerlegen */
259 ptr = strchr( str, '=' );
262 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
266 var = str; ngt_TrimStr( var );
267 arg = ptr + 1; ngt_TrimStr( arg );
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 );
278 /* Wenn kein Port definiert wurde, Port 6667 als Default benutzen */
279 if( Conf_ListenPorts_Count < 1 )
281 Conf_ListenPorts_Count = 1;
282 Conf_ListenPorts[0] = 6667;
288 Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
294 assert( Var != NULL );
295 assert( Arg != NULL );
297 if( strcasecmp( Var, "Name" ) == 0 )
299 /* Der Server-Name */
300 strncpy( Conf_ServerName, Arg, CLIENT_ID_LEN - 1 );
301 Conf_ServerName[CLIENT_ID_LEN - 1] = '\0';
304 if( strcasecmp( Var, "Info" ) == 0 )
306 /* Server-Info-Text */
307 strncpy( Conf_ServerInfo, Arg, CLIENT_INFO_LEN - 1 );
308 Conf_ServerInfo[CLIENT_INFO_LEN - 1] = '\0';
311 if( strcasecmp( Var, "Password" ) == 0 )
313 /* Server-Passwort */
314 strncpy( Conf_ServerPwd, Arg, CLIENT_PASS_LEN - 1 );
315 Conf_ServerPwd[CLIENT_PASS_LEN - 1] = '\0';
318 if( strcasecmp( Var, "Ports" ) == 0 )
320 /* Ports, durch "," getrennt, auf denen der Server
321 * Verbindungen annehmen soll */
322 ptr = strtok( Arg, "," );
327 if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Config_Error( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
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 );
333 ptr = strtok( NULL, "," );
337 if( strcasecmp( Var, "MotdFile" ) == 0 )
339 /* Datei mit der "message of the day" (MOTD) */
340 strncpy( Conf_MotdFile, Arg, FNAME_LEN - 1 );
341 Conf_MotdFile[FNAME_LEN - 1] = '\0';
344 if( strcasecmp( Var, "ServerUID" ) == 0 )
346 /* UID, mit der der Daemon laufen soll */
347 Conf_UID = (UINT)atoi( Arg );
350 if( strcasecmp( Var, "ServerGID" ) == 0 )
352 /* GID, mit der der Daemon laufen soll */
353 Conf_GID = (UINT)atoi( Arg );
356 if( strcasecmp( Var, "PingTimeout" ) == 0 )
359 Conf_PingTimeout = atoi( Arg );
360 if(( Conf_PingTimeout ) < 5 ) Conf_PingTimeout = 5;
363 if( strcasecmp( Var, "PongTimeout" ) == 0 )
366 Conf_PongTimeout = atoi( Arg );
367 if(( Conf_PongTimeout ) < 5 ) Conf_PongTimeout = 5;
370 if( strcasecmp( Var, "ConnectRetry" ) == 0 )
372 /* Sekunden zwischen Verbindungsversuchen zu anderen Servern */
373 Conf_ConnectRetry = atoi( Arg );
374 if(( Conf_ConnectRetry ) < 5 ) Conf_ConnectRetry = 5;
377 if( strcasecmp( Var, "OperCanUseMode" ) == 0 )
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;
387 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
388 } /* Handle_GLOBAL */
392 Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
395 assert( Var != NULL );
396 assert( Arg != NULL );
397 assert( Conf_Oper_Count > 0 );
399 if( strcasecmp( Var, "Name" ) == 0 )
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';
406 if( strcasecmp( Var, "Password" ) == 0 )
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';
414 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
415 } /* Handle_OPERATOR */
419 Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
424 assert( Var != NULL );
425 assert( Arg != NULL );
427 if( strcasecmp( Var, "Host" ) == 0 )
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';
434 if( strcasecmp( Var, "Name" ) == 0 )
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';
441 if( strcasecmp( Var, "Password" ) == 0 )
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';
448 if( strcasecmp( Var, "Port" ) == 0 )
450 /* Port, zu dem Verbunden werden soll */
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 );
456 if( strcasecmp( Var, "Group" ) == 0 )
459 Conf_Server[Conf_Server_Count - 1].group = atoi( Arg );
463 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
464 } /* Handle_SERVER */
468 Handle_CHANNEL( INT Line, CHAR *Var, CHAR *Arg )
471 assert( Var != NULL );
472 assert( Arg != NULL );
474 if( strcasecmp( Var, "Name" ) == 0 )
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';
481 if( strcasecmp( Var, "Modes" ) == 0 )
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';
488 if( strcasecmp( Var, "Topic" ) == 0 )
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';
496 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
497 } /* Handle_CHANNEL */
501 Validate_Config( VOID )
503 /* Konfiguration ueberpruefen */
505 if( ! Conf_ServerName[0] )
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 );
512 } /* Validate_Config */
516 LOCAL VOID Config_Error( CONST INT Level, CONST CHAR *Format, ... )
518 LOCAL VOID Config_Error( Level, Format, va_alist )
524 /* Fehler! Auf Console und/oder ins Log schreiben */
526 CHAR msg[MAX_LOG_MSG_LEN];
529 assert( Format != NULL );
531 /* String mit variablen Argumenten zusammenbauen ... */
533 va_start( ap, Format );
537 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
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 );