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.10 2002/01/03 02:27:20 alex Exp $
14 * conf.h: Konfiguration des ngircd
17 * Revision 1.10 2002/01/03 02:27:20 alex
18 * - das Server-Passwort kann nun konfiguriert werden.
20 * Revision 1.9 2002/01/02 02:49:15 alex
21 * - Konfigurationsdatei "Samba like" umgestellt.
22 * - es koennen nun mehrere Server und Oprtatoren konfiguriert werden.
24 * Revision 1.7 2002/01/01 18:25:44 alex
25 * - #include's fuer stdlib.h ergaenzt.
27 * Revision 1.6 2001/12/31 02:18:51 alex
28 * - viele neue Befehle (WHOIS, ISON, OPER, DIE, RESTART),
29 * - neuen Header "defines.h" mit (fast) allen Konstanten.
30 * - Code Cleanups und viele "kleine" Aenderungen & Bugfixes.
32 * Revision 1.5 2001/12/30 19:26:11 alex
33 * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
35 * Revision 1.4 2001/12/26 22:48:53 alex
36 * - MOTD-Datei ist nun konfigurierbar und wird gelesen.
38 * Revision 1.3 2001/12/26 14:45:37 alex
41 * Revision 1.2 2001/12/26 03:19:57 alex
42 * - erste Konfigurations-Variablen definiert: PING/PONG-Timeout.
44 * Revision 1.1 2001/12/12 17:18:20 alex
45 * - Modul fuer Server-Konfiguration begonnen.
67 LOCAL VOID Read_Config( VOID );
69 GLOBAL VOID Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg );
70 GLOBAL VOID Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg );
71 GLOBAL VOID Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg );
73 LOCAL VOID Validate_Config( VOID );
76 GLOBAL VOID Conf_Init( VOID )
78 /* Konfigurationsvariablen initialisieren: zunaechst Default-
79 * Werte setzen, dann Konfigurationsdtaei einlesen. */
81 strcpy( Conf_File, "/usr/local/etc/ngircd.conf" );
83 strcpy( Conf_ServerName, "" );
84 strcpy( Conf_ServerInfo, PACKAGE" "VERSION );
85 strcpy( Conf_ServerPwd, "" );
87 strcpy( Conf_MotdFile, "/usr/local/etc/ngircd.motd" );
89 Conf_ListenPorts_Count = 0;
91 Conf_PingTimeout = 120;
92 Conf_PongTimeout = 10;
94 Conf_ConnectRetry = 60;
98 Conf_Server_Count = 0;
100 /* Konfigurationsdatei einlesen und validieren */
106 GLOBAL VOID Conf_Exit( VOID )
112 LOCAL VOID Read_Config( VOID )
114 /* Konfigurationsdatei einlesen. */
116 CHAR section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
120 fd = fopen( Conf_File, "r" );
123 /* Keine Konfigurationsdatei gefunden */
124 Log( LOG_ALERT, "Can't read configuration \"%s\": %s", Conf_File, strerror( errno ));
125 Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
130 strcpy( section, "" );
133 if( ! fgets( str, LINE_LEN, fd )) break;
137 /* Kommentarzeilen und leere Zeilen ueberspringen */
138 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
140 /* Anfang eines Abschnittes? */
141 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
143 strcpy( section, str );
144 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
145 if( strcasecmp( section, "[OPERATOR]" ) == 0 )
147 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Log( LOG_ERR, "Too many operators configured." );
150 /* neuen Operator initialisieren */
151 strcpy( Conf_Oper[Conf_Oper_Count].name, "" );
152 strcpy( Conf_Oper[Conf_Oper_Count].pwd, "" );
157 if( strcasecmp( section, "[SERVER]" ) == 0 )
159 if( Conf_Server_Count + 1 > MAX_SERVERS ) Log( LOG_ERR, "Too many servers configured." );
162 /* neuen Server ("Peer") initialisieren */
163 strcpy( Conf_Server[Conf_Server_Count].host, "" );
164 strcpy( Conf_Server[Conf_Server_Count].ip, "" );
165 strcpy( Conf_Server[Conf_Server_Count].name, "" );
166 strcpy( Conf_Server[Conf_Server_Count].pwd, "" );
167 Conf_Server[Conf_Server_Count].port = 0;
168 Conf_Server[Conf_Server_Count].lasttry = 0;
169 Conf_Server[Conf_Server_Count].res_stat = NULL;
174 Log( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", Conf_File, line, section );
177 if( section[0] == 0x1 ) continue;
179 /* In Variable und Argument zerlegen */
180 ptr = strchr( str, '=' );
183 Log( LOG_ERR, "%s, line %d: Syntax error!", Conf_File, line );
187 var = str; ngt_TrimStr( var );
188 arg = ptr + 1; ngt_TrimStr( arg );
190 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
191 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
192 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
193 else Log( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", Conf_File, line, var );
200 GLOBAL VOID Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
206 assert( Var != NULL );
207 assert( Arg != NULL );
209 if( strcasecmp( Var, "Name" ) == 0 )
211 /* Der Server-Name */
212 strncpy( Conf_ServerName, Arg, CLIENT_ID_LEN );
213 Conf_ServerName[CLIENT_ID_LEN - 1] = '\0';
216 if( strcasecmp( Var, "Info" ) == 0 )
218 /* Server-Info-Text */
219 strncpy( Conf_ServerInfo, Arg, CLIENT_INFO_LEN );
220 Conf_ServerInfo[CLIENT_INFO_LEN - 1] = '\0';
223 if( strcasecmp( Var, "Password" ) == 0 )
225 /* Der Server-Name */
226 strncpy( Conf_ServerPwd, Arg, CLIENT_PASS_LEN );
227 Conf_ServerPwd[CLIENT_PASS_LEN - 1] = '\0';
230 if( strcasecmp( Var, "Ports" ) == 0 )
232 /* Ports, durch "," getrennt, auf denen der Server
233 * Verbindungen annehmen soll */
234 ptr = strtok( Arg, "," );
239 if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Log( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
242 if( port > 0 && port < 0xFFFF ) Conf_ListenPorts[Conf_ListenPorts_Count++] = port;
243 else Log( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!", Conf_File, Line, port );
245 ptr = strtok( NULL, "," );
249 if( strcasecmp( Var, "MotdFile" ) == 0 )
251 /* Datei mit der "message of the day" (MOTD) */
252 strncpy( Conf_MotdFile, Arg, FNAME_LEN );
253 Conf_MotdFile[FNAME_LEN - 1] = '\0';
256 if( strcasecmp( Var, "PingTimeout" ) == 0 )
259 Conf_PingTimeout = atoi( Arg );
260 if(( Conf_PingTimeout ) < 5 ) Conf_PingTimeout = 5;
263 if( strcasecmp( Var, "PongTimeout" ) == 0 )
266 Conf_PongTimeout = atoi( Arg );
267 if(( Conf_PongTimeout ) < 5 ) Conf_PongTimeout = 5;
270 if( strcasecmp( Var, "ConnectRetry" ) == 0 )
272 /* Sekunden zwischen Verbindungsversuchen zu anderen Servern */
273 Conf_ConnectRetry = atoi( Arg );
274 if(( Conf_ConnectRetry ) < 5 ) Conf_ConnectRetry = 5;
278 Log( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", Conf_File, Line, Var );
279 } /* Handle_GLOBAL */
282 GLOBAL VOID Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
285 assert( Var != NULL );
286 assert( Arg != NULL );
287 assert( Conf_Oper_Count > 0 );
289 if( strcasecmp( Var, "Name" ) == 0 )
291 /* Name des IRC Operator */
292 strncpy( Conf_Oper[Conf_Oper_Count - 1].name, Arg, CLIENT_ID_LEN );
293 Conf_Oper[Conf_Oper_Count - 1].name[CLIENT_ID_LEN - 1] = '\0';
296 if( strcasecmp( Var, "Password" ) == 0 )
298 /* Passwort des IRC Operator */
299 strncpy( Conf_Oper[Conf_Oper_Count - 1].pwd, Arg, CLIENT_PASS_LEN );
300 Conf_Oper[Conf_Oper_Count - 1].pwd[CLIENT_PASS_LEN - 1] = '\0';
304 Log( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", Conf_File, Line, Var );
305 } /* Handle_OPERATOR */
308 GLOBAL VOID Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
313 assert( Var != NULL );
314 assert( Arg != NULL );
316 if( strcasecmp( Var, "Host" ) == 0 )
318 /* Hostname des Servers */
319 strncpy( Conf_Server[Conf_Server_Count - 1].host, Arg, HOST_LEN );
320 Conf_Server[Conf_Server_Count - 1].host[HOST_LEN - 1] = '\0';
323 if( strcasecmp( Var, "Name" ) == 0 )
325 /* Name des Servers ("Nick") */
326 strncpy( Conf_Server[Conf_Server_Count - 1].name, Arg, CLIENT_PASS_LEN );
327 Conf_Server[Conf_Server_Count - 1].name[CLIENT_PASS_LEN - 1] = '\0';
330 if( strcasecmp( Var, "Password" ) == 0 )
332 /* Passwort des Servers */
333 strncpy( Conf_Server[Conf_Server_Count - 1].pwd, Arg, CLIENT_PASS_LEN );
334 Conf_Server[Conf_Server_Count - 1].pwd[CLIENT_PASS_LEN - 1] = '\0';
337 if( strcasecmp( Var, "Port" ) == 0 )
339 /* Port, zu dem Verbunden werden soll */
341 if( port > 0 && port < 0xFFFF ) Conf_Server[Conf_Server_Count - 1].port = port;
342 else Log( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!", Conf_File, Line, port );
346 Log( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", Conf_File, Line, Var );
347 } /* Handle_SERVER */
350 LOCAL VOID Validate_Config( VOID )
352 /* Konfiguration ueberpruefen */
354 if( ! Conf_ServerName[0] )
356 /* Kein Servername konfiguriert */
357 Log( LOG_ALERT, "No server name configured (use \"ServerName\")!", Conf_File, strerror( errno ));
358 Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
361 } /* Validate_Config */