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.23 2002/03/30 13:08:10 alex Exp $
14 * conf.h: Konfiguration des ngircd
39 LOCAL BOOLEAN Use_Log = TRUE;
42 LOCAL VOID Set_Defaults( VOID );
43 LOCAL VOID Read_Config( VOID );
44 LOCAL VOID Validate_Config( VOID );
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 );
50 LOCAL VOID Config_Error( CONST INT Level, CONST CHAR *Format, ... );
53 GLOBAL VOID Conf_Init( VOID )
61 GLOBAL INT Conf_Test( VOID )
63 /* Konfiguration einlesen, ueberpruefen und ausgeben. */
70 printf( "Using \"%s\" as configuration file ...\n", NGIRCd_ConfFile );
73 /* Wenn stdin ein ein TTY ist: auf Taste warten */
74 if( isatty( fileno( stdout )))
76 puts( "OK, press enter to see a dump of your service configuration ..." );
79 else puts( "Ok, dump of your server configuration follows:\n" );
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++ )
89 if( i != 0 ) printf( ", " );
90 printf( "%u", Conf_ListenPorts[i] );
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 );
100 for( i = 0; i < Conf_Oper_Count; i++ )
102 puts( "[OPERATOR]" );
103 printf( " Name = %s\n", Conf_Oper[i].name );
104 printf( " Password = %s\n", Conf_Oper[i].pwd );
108 for( i = 0; i < Conf_Server_Count; i++ )
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 );
123 GLOBAL VOID Conf_Exit( VOID )
129 LOCAL VOID Set_Defaults( VOID )
131 /* Konfigurationsvariablen initialisieren, d.h. auf Default-Werte setzen. */
133 strcpy( Conf_ServerName, "" );
134 strcpy( Conf_ServerInfo, PACKAGE" "VERSION );
135 strcpy( Conf_ServerPwd, "" );
137 strcpy( Conf_MotdFile, MOTD_FILE );
139 Conf_ListenPorts_Count = 0;
141 Conf_UID = Conf_GID = 0;
143 Conf_PingTimeout = 120;
144 Conf_PongTimeout = 20;
146 Conf_ConnectRetry = 60;
150 Conf_Server_Count = 0;
154 LOCAL VOID Read_Config( VOID )
156 /* Konfigurationsdatei einlesen. */
158 CHAR section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
162 fd = fopen( NGIRCd_ConfFile, "r" );
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!" );
172 strcpy( section, "" );
175 if( ! fgets( str, LINE_LEN, fd )) break;
179 /* Kommentarzeilen und leere Zeilen ueberspringen */
180 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
182 /* Anfang eines Abschnittes? */
183 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
185 strcpy( section, str );
186 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
187 if( strcasecmp( section, "[OPERATOR]" ) == 0 )
189 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Config_Error( LOG_ERR, "Too many operators configured." );
192 /* neuen Operator initialisieren */
193 strcpy( Conf_Oper[Conf_Oper_Count].name, "" );
194 strcpy( Conf_Oper[Conf_Oper_Count].pwd, "" );
199 if( strcasecmp( section, "[SERVER]" ) == 0 )
201 if( Conf_Server_Count + 1 > MAX_SERVERS ) Config_Error( LOG_ERR, "Too many servers configured." );
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;
217 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
220 if( section[0] == 0x1 ) continue;
222 /* In Variable und Argument zerlegen */
223 ptr = strchr( str, '=' );
226 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
230 var = str; ngt_TrimStr( var );
231 arg = ptr + 1; ngt_TrimStr( arg );
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 );
241 /* Wenn kein Port definiert wurde, Port 6667 als Default benutzen */
242 if( Conf_ListenPorts_Count < 1 )
244 Conf_ListenPorts_Count = 1;
245 Conf_ListenPorts[0] = 6667;
250 GLOBAL VOID Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
256 assert( Var != NULL );
257 assert( Arg != NULL );
259 if( strcasecmp( Var, "Name" ) == 0 )
261 /* Der Server-Name */
262 strncpy( Conf_ServerName, Arg, CLIENT_ID_LEN - 1 );
263 Conf_ServerName[CLIENT_ID_LEN - 1] = '\0';
266 if( strcasecmp( Var, "Info" ) == 0 )
268 /* Server-Info-Text */
269 strncpy( Conf_ServerInfo, Arg, CLIENT_INFO_LEN - 1 );
270 Conf_ServerInfo[CLIENT_INFO_LEN - 1] = '\0';
273 if( strcasecmp( Var, "Password" ) == 0 )
275 /* Server-Passwort */
276 strncpy( Conf_ServerPwd, Arg, CLIENT_PASS_LEN - 1 );
277 Conf_ServerPwd[CLIENT_PASS_LEN - 1] = '\0';
280 if( strcasecmp( Var, "Ports" ) == 0 )
282 /* Ports, durch "," getrennt, auf denen der Server
283 * Verbindungen annehmen soll */
284 ptr = strtok( Arg, "," );
289 if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Config_Error( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
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 );
295 ptr = strtok( NULL, "," );
299 if( strcasecmp( Var, "MotdFile" ) == 0 )
301 /* Datei mit der "message of the day" (MOTD) */
302 strncpy( Conf_MotdFile, Arg, FNAME_LEN - 1 );
303 Conf_MotdFile[FNAME_LEN - 1] = '\0';
306 if( strcasecmp( Var, "ServerUID" ) == 0 )
308 /* UID, mit der der Daemon laufen soll */
309 Conf_UID = (UINT)atoi( Arg );
312 if( strcasecmp( Var, "ServerGID" ) == 0 )
314 /* GID, mit der der Daemon laufen soll */
315 Conf_GID = (UINT)atoi( Arg );
318 if( strcasecmp( Var, "PingTimeout" ) == 0 )
321 Conf_PingTimeout = atoi( Arg );
322 if(( Conf_PingTimeout ) < 5 ) Conf_PingTimeout = 5;
325 if( strcasecmp( Var, "PongTimeout" ) == 0 )
328 Conf_PongTimeout = atoi( Arg );
329 if(( Conf_PongTimeout ) < 5 ) Conf_PongTimeout = 5;
332 if( strcasecmp( Var, "ConnectRetry" ) == 0 )
334 /* Sekunden zwischen Verbindungsversuchen zu anderen Servern */
335 Conf_ConnectRetry = atoi( Arg );
336 if(( Conf_ConnectRetry ) < 5 ) Conf_ConnectRetry = 5;
340 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
341 } /* Handle_GLOBAL */
344 GLOBAL VOID Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
347 assert( Var != NULL );
348 assert( Arg != NULL );
349 assert( Conf_Oper_Count > 0 );
351 if( strcasecmp( Var, "Name" ) == 0 )
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';
358 if( strcasecmp( Var, "Password" ) == 0 )
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';
366 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
367 } /* Handle_OPERATOR */
370 GLOBAL VOID Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
375 assert( Var != NULL );
376 assert( Arg != NULL );
378 if( strcasecmp( Var, "Host" ) == 0 )
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';
385 if( strcasecmp( Var, "Name" ) == 0 )
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';
392 if( strcasecmp( Var, "Password" ) == 0 )
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';
399 if( strcasecmp( Var, "Port" ) == 0 )
401 /* Port, zu dem Verbunden werden soll */
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 );
407 if( strcasecmp( Var, "Group" ) == 0 )
410 Conf_Server[Conf_Server_Count - 1].group = atoi( Arg );
414 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
415 } /* Handle_SERVER */
418 LOCAL VOID Validate_Config( VOID )
420 /* Konfiguration ueberpruefen */
422 if( ! Conf_ServerName[0] )
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!" );
429 } /* Validate_Config */
432 LOCAL VOID Config_Error( CONST INT Level, CONST CHAR *Format, ... )
434 /* Fehler! Auf Console und/oder ins Log schreiben */
436 CHAR msg[MAX_LOG_MSG_LEN];
439 assert( Format != NULL );
441 /* String mit variablen Argumenten zusammenbauen ... */
442 va_start( ap, Format );
443 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
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 );