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.17 2002/03/10 18:47:02 alex Exp $
14 * conf.h: Konfiguration des ngircd
17 * Revision 1.17 2002/03/10 18:47:02 alex
18 * *** empty log message ***
20 * Revision 1.16 2002/03/10 17:50:48 alex
21 * - Server-Gruppen implementiert.
23 * Revision 1.15 2002/03/06 15:35:19 alex
24 * - Dateinamen und Pfad sind nun in Konstanten definiert.
26 * Revision 1.14 2002/03/03 17:17:01 alex
27 * - strncpy() und vsnprintf() kopieren nun etwas "optimierter" (1 Byte weniger) :-)
29 * Revision 1.13 2002/01/18 15:51:44 alex
30 * - Server-Verbinungen werden beim Start erst nach kurzer Pause aufgebaut.
32 * Revision 1.12 2002/01/05 23:26:24 alex
33 * - Fehlermeldungen korrigiert.
35 * Revision 1.11 2002/01/05 16:51:49 alex
36 * - Bug bei Remote-Server-Namen entfernt: diese wurden falsch gekuerzt.
38 * Revision 1.10 2002/01/03 02:27:20 alex
39 * - das Server-Passwort kann nun konfiguriert werden.
41 * Revision 1.9 2002/01/02 02:49:15 alex
42 * - Konfigurationsdatei "Samba like" umgestellt.
43 * - es koennen nun mehrere Server und Oprtatoren konfiguriert werden.
45 * Revision 1.7 2002/01/01 18:25:44 alex
46 * - #include's fuer stdlib.h ergaenzt.
48 * Revision 1.6 2001/12/31 02:18:51 alex
49 * - viele neue Befehle (WHOIS, ISON, OPER, DIE, RESTART),
50 * - neuen Header "defines.h" mit (fast) allen Konstanten.
51 * - Code Cleanups und viele "kleine" Aenderungen & Bugfixes.
53 * Revision 1.5 2001/12/30 19:26:11 alex
54 * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
56 * Revision 1.4 2001/12/26 22:48:53 alex
57 * - MOTD-Datei ist nun konfigurierbar und wird gelesen.
59 * Revision 1.3 2001/12/26 14:45:37 alex
62 * Revision 1.2 2001/12/26 03:19:57 alex
63 * - erste Konfigurations-Variablen definiert: PING/PONG-Timeout.
65 * Revision 1.1 2001/12/12 17:18:20 alex
66 * - Modul fuer Server-Konfiguration begonnen.
88 LOCAL VOID Read_Config( VOID );
90 GLOBAL VOID Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg );
91 GLOBAL VOID Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg );
92 GLOBAL VOID Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg );
94 LOCAL VOID Validate_Config( VOID );
97 GLOBAL VOID Conf_Init( VOID )
99 /* Konfigurationsvariablen initialisieren: zunaechst Default-
100 * Werte setzen, dann Konfigurationsdtaei einlesen. */
102 strcpy( Conf_File, CONFIG_FILE );
104 strcpy( Conf_ServerName, "" );
105 strcpy( Conf_ServerInfo, PACKAGE" "VERSION );
106 strcpy( Conf_ServerPwd, "" );
108 strcpy( Conf_MotdFile, MOTD_FILE );
110 Conf_ListenPorts_Count = 0;
112 Conf_PingTimeout = 120;
113 Conf_PongTimeout = 10;
115 Conf_ConnectRetry = 60;
119 Conf_Server_Count = 0;
121 /* Konfigurationsdatei einlesen und validieren */
127 GLOBAL VOID Conf_Exit( VOID )
133 LOCAL VOID Read_Config( VOID )
135 /* Konfigurationsdatei einlesen. */
137 CHAR section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
141 fd = fopen( Conf_File, "r" );
144 /* Keine Konfigurationsdatei gefunden */
145 Log( LOG_ALERT, "Can't read configuration \"%s\": %s", Conf_File, strerror( errno ));
146 Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
151 strcpy( section, "" );
154 if( ! fgets( str, LINE_LEN, fd )) break;
158 /* Kommentarzeilen und leere Zeilen ueberspringen */
159 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
161 /* Anfang eines Abschnittes? */
162 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
164 strcpy( section, str );
165 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
166 if( strcasecmp( section, "[OPERATOR]" ) == 0 )
168 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Log( LOG_ERR, "Too many operators configured." );
171 /* neuen Operator initialisieren */
172 strcpy( Conf_Oper[Conf_Oper_Count].name, "" );
173 strcpy( Conf_Oper[Conf_Oper_Count].pwd, "" );
178 if( strcasecmp( section, "[SERVER]" ) == 0 )
180 if( Conf_Server_Count + 1 > MAX_SERVERS ) Log( LOG_ERR, "Too many servers configured." );
183 /* neuen Server ("Peer") initialisieren */
184 strcpy( Conf_Server[Conf_Server_Count].host, "" );
185 strcpy( Conf_Server[Conf_Server_Count].ip, "" );
186 strcpy( Conf_Server[Conf_Server_Count].name, "" );
187 strcpy( Conf_Server[Conf_Server_Count].pwd, "" );
188 Conf_Server[Conf_Server_Count].port = 0;
189 Conf_Server[Conf_Server_Count].group = -1;
190 Conf_Server[Conf_Server_Count].lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
191 Conf_Server[Conf_Server_Count].res_stat = NULL;
196 Log( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", Conf_File, line, section );
199 if( section[0] == 0x1 ) continue;
201 /* In Variable und Argument zerlegen */
202 ptr = strchr( str, '=' );
205 Log( LOG_ERR, "%s, line %d: Syntax error!", Conf_File, line );
209 var = str; ngt_TrimStr( var );
210 arg = ptr + 1; ngt_TrimStr( arg );
212 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
213 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
214 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
215 else Log( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", Conf_File, line, var );
222 GLOBAL VOID Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
228 assert( Var != NULL );
229 assert( Arg != NULL );
231 if( strcasecmp( Var, "Name" ) == 0 )
233 /* Der Server-Name */
234 strncpy( Conf_ServerName, Arg, CLIENT_ID_LEN - 1 );
235 Conf_ServerName[CLIENT_ID_LEN - 1] = '\0';
238 if( strcasecmp( Var, "Info" ) == 0 )
240 /* Server-Info-Text */
241 strncpy( Conf_ServerInfo, Arg, CLIENT_INFO_LEN - 1 );
242 Conf_ServerInfo[CLIENT_INFO_LEN - 1] = '\0';
245 if( strcasecmp( Var, "Password" ) == 0 )
247 /* Server-Passwort */
248 strncpy( Conf_ServerPwd, Arg, CLIENT_PASS_LEN - 1 );
249 Conf_ServerPwd[CLIENT_PASS_LEN - 1] = '\0';
252 if( strcasecmp( Var, "Ports" ) == 0 )
254 /* Ports, durch "," getrennt, auf denen der Server
255 * Verbindungen annehmen soll */
256 ptr = strtok( Arg, "," );
261 if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Log( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
264 if( port > 0 && port < 0xFFFF ) Conf_ListenPorts[Conf_ListenPorts_Count++] = port;
265 else Log( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!", Conf_File, Line, port );
267 ptr = strtok( NULL, "," );
271 if( strcasecmp( Var, "MotdFile" ) == 0 )
273 /* Datei mit der "message of the day" (MOTD) */
274 strncpy( Conf_MotdFile, Arg, FNAME_LEN - 1 );
275 Conf_MotdFile[FNAME_LEN - 1] = '\0';
278 if( strcasecmp( Var, "PingTimeout" ) == 0 )
281 Conf_PingTimeout = atoi( Arg );
282 if(( Conf_PingTimeout ) < 5 ) Conf_PingTimeout = 5;
285 if( strcasecmp( Var, "PongTimeout" ) == 0 )
288 Conf_PongTimeout = atoi( Arg );
289 if(( Conf_PongTimeout ) < 5 ) Conf_PongTimeout = 5;
292 if( strcasecmp( Var, "ConnectRetry" ) == 0 )
294 /* Sekunden zwischen Verbindungsversuchen zu anderen Servern */
295 Conf_ConnectRetry = atoi( Arg );
296 if(( Conf_ConnectRetry ) < 5 ) Conf_ConnectRetry = 5;
300 Log( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", Conf_File, Line, Var );
301 } /* Handle_GLOBAL */
304 GLOBAL VOID Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
307 assert( Var != NULL );
308 assert( Arg != NULL );
309 assert( Conf_Oper_Count > 0 );
311 if( strcasecmp( Var, "Name" ) == 0 )
313 /* Name des IRC Operator */
314 strncpy( Conf_Oper[Conf_Oper_Count - 1].name, Arg, CLIENT_ID_LEN - 1 );
315 Conf_Oper[Conf_Oper_Count - 1].name[CLIENT_ID_LEN - 1] = '\0';
318 if( strcasecmp( Var, "Password" ) == 0 )
320 /* Passwort des IRC Operator */
321 strncpy( Conf_Oper[Conf_Oper_Count - 1].pwd, Arg, CLIENT_PASS_LEN - 1 );
322 Conf_Oper[Conf_Oper_Count - 1].pwd[CLIENT_PASS_LEN - 1] = '\0';
326 Log( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", Conf_File, Line, Var );
327 } /* Handle_OPERATOR */
330 GLOBAL VOID Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
335 assert( Var != NULL );
336 assert( Arg != NULL );
338 if( strcasecmp( Var, "Host" ) == 0 )
340 /* Hostname des Servers */
341 strncpy( Conf_Server[Conf_Server_Count - 1].host, Arg, HOST_LEN - 1 );
342 Conf_Server[Conf_Server_Count - 1].host[HOST_LEN - 1] = '\0';
345 if( strcasecmp( Var, "Name" ) == 0 )
347 /* Name des Servers ("Nick") */
348 strncpy( Conf_Server[Conf_Server_Count - 1].name, Arg, CLIENT_ID_LEN - 1 );
349 Conf_Server[Conf_Server_Count - 1].name[CLIENT_ID_LEN - 1] = '\0';
352 if( strcasecmp( Var, "Password" ) == 0 )
354 /* Passwort des Servers */
355 strncpy( Conf_Server[Conf_Server_Count - 1].pwd, Arg, CLIENT_PASS_LEN - 1 );
356 Conf_Server[Conf_Server_Count - 1].pwd[CLIENT_PASS_LEN - 1] = '\0';
359 if( strcasecmp( Var, "Port" ) == 0 )
361 /* Port, zu dem Verbunden werden soll */
363 if( port > 0 && port < 0xFFFF ) Conf_Server[Conf_Server_Count - 1].port = (INT)port;
364 else Log( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!", Conf_File, Line, port );
367 if( strcasecmp( Var, "Group" ) == 0 )
370 Conf_Server[Conf_Server_Count - 1].group = atoi( Arg );
374 Log( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", Conf_File, Line, Var );
375 } /* Handle_SERVER */
378 LOCAL VOID Validate_Config( VOID )
380 /* Konfiguration ueberpruefen */
382 if( ! Conf_ServerName[0] )
384 /* Kein Servername konfiguriert */
385 Log( LOG_ALERT, "No server name configured in \"%s\"!", Conf_File );
386 Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
389 } /* Validate_Config */