]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conf.c
*** empty log message ***
[ngircd-alex.git] / src / ngircd / conf.c
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.17 2002/03/10 18:47:02 alex Exp $
13  *
14  * conf.h: Konfiguration des ngircd
15  *
16  * $Log: conf.c,v $
17  * Revision 1.17  2002/03/10 18:47:02  alex
18  * *** empty log message ***
19  *
20  * Revision 1.16  2002/03/10 17:50:48  alex
21  * - Server-Gruppen implementiert.
22  *
23  * Revision 1.15  2002/03/06 15:35:19  alex
24  * - Dateinamen und Pfad sind nun in Konstanten definiert.
25  *
26  * Revision 1.14  2002/03/03 17:17:01  alex
27  * - strncpy() und vsnprintf() kopieren nun etwas "optimierter" (1 Byte weniger) :-)
28  *
29  * Revision 1.13  2002/01/18 15:51:44  alex
30  * - Server-Verbinungen werden beim Start erst nach kurzer Pause aufgebaut.
31  *
32  * Revision 1.12  2002/01/05 23:26:24  alex
33  * - Fehlermeldungen korrigiert.
34  *
35  * Revision 1.11  2002/01/05 16:51:49  alex
36  * - Bug bei Remote-Server-Namen entfernt: diese wurden falsch gekuerzt.
37  *
38  * Revision 1.10  2002/01/03 02:27:20  alex
39  * - das Server-Passwort kann nun konfiguriert werden.
40  *
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.
44  *
45  * Revision 1.7  2002/01/01 18:25:44  alex
46  * - #include's fuer stdlib.h ergaenzt.
47  *
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.
52  *
53  * Revision 1.5  2001/12/30 19:26:11  alex
54  * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
55  *
56  * Revision 1.4  2001/12/26 22:48:53  alex
57  * - MOTD-Datei ist nun konfigurierbar und wird gelesen.
58  *
59  * Revision 1.3  2001/12/26 14:45:37  alex
60  * - "Code Cleanups".
61  *
62  * Revision 1.2  2001/12/26 03:19:57  alex
63  * - erste Konfigurations-Variablen definiert: PING/PONG-Timeout.
64  *
65  * Revision 1.1  2001/12/12 17:18:20  alex
66  * - Modul fuer Server-Konfiguration begonnen.
67  */
68
69
70 #include <portab.h>
71 #include "global.h"
72
73 #include <imp.h>
74 #include <assert.h>
75 #include <errno.h>
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <string.h>
79
80 #include "client.h"
81 #include "log.h"
82 #include "tool.h"
83
84 #include <exp.h>
85 #include "conf.h"
86
87
88 LOCAL VOID Read_Config( VOID );
89
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 );
93
94 LOCAL VOID Validate_Config( VOID );
95
96
97 GLOBAL VOID Conf_Init( VOID )
98 {
99         /* Konfigurationsvariablen initialisieren: zunaechst Default-
100          * Werte setzen, dann Konfigurationsdtaei einlesen. */
101
102         strcpy( Conf_File, CONFIG_FILE );
103
104         strcpy( Conf_ServerName, "" );
105         strcpy( Conf_ServerInfo, PACKAGE" "VERSION );
106         strcpy( Conf_ServerPwd, "" );
107
108         strcpy( Conf_MotdFile, MOTD_FILE );
109
110         Conf_ListenPorts_Count = 0;
111         
112         Conf_PingTimeout = 120;
113         Conf_PongTimeout = 10;
114
115         Conf_ConnectRetry = 60;
116
117         Conf_Oper_Count = 0;
118
119         Conf_Server_Count = 0;
120
121         /* Konfigurationsdatei einlesen und validieren */
122         Read_Config( );
123         Validate_Config( );
124 } /* Config_Init */
125
126
127 GLOBAL VOID Conf_Exit( VOID )
128 {
129         /* ... */
130 } /* Config_Exit */
131
132
133 LOCAL VOID Read_Config( VOID )
134 {
135         /* Konfigurationsdatei einlesen. */
136
137         CHAR section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
138         INT line;
139         FILE *fd;
140
141         fd = fopen( Conf_File, "r" );
142         if( ! fd )
143         {
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!" );
147                 exit( 1 );
148         }
149
150         line = 0;
151         strcpy( section, "" );
152         while( TRUE )
153         {
154                 if( ! fgets( str, LINE_LEN, fd )) break;
155                 ngt_TrimStr( str );
156                 line++;
157
158                 /* Kommentarzeilen und leere Zeilen ueberspringen */
159                 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
160
161                 /* Anfang eines Abschnittes? */
162                 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
163                 {
164                         strcpy( section, str );
165                         if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
166                         if( strcasecmp( section, "[OPERATOR]" ) == 0 )
167                         {
168                                 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Log( LOG_ERR, "Too many operators configured." );
169                                 else
170                                 {
171                                         /* neuen Operator initialisieren */
172                                         strcpy( Conf_Oper[Conf_Oper_Count].name, "" );
173                                         strcpy( Conf_Oper[Conf_Oper_Count].pwd, "" );
174                                         Conf_Oper_Count++;
175                                 }
176                                 continue;
177                         }
178                         if( strcasecmp( section, "[SERVER]" ) == 0 )
179                         {
180                                 if( Conf_Server_Count + 1 > MAX_SERVERS ) Log( LOG_ERR, "Too many servers configured." );
181                                 else
182                                 {
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;
192                                         Conf_Server_Count++;
193                                 }
194                                 continue;
195                         }
196                         Log( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", Conf_File, line, section );
197                         section[0] = 0x1;
198                 }
199                 if( section[0] == 0x1 ) continue;
200
201                 /* In Variable und Argument zerlegen */
202                 ptr = strchr( str, '=' );
203                 if( ! ptr )
204                 {
205                         Log( LOG_ERR, "%s, line %d: Syntax error!", Conf_File, line );
206                         continue;
207                 }
208                 *ptr = '\0';
209                 var = str; ngt_TrimStr( var );
210                 arg = ptr + 1; ngt_TrimStr( arg );
211
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 );
216         }
217         
218         fclose( fd );
219 } /* Read_Config */
220
221
222 GLOBAL VOID Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
223 {
224         CHAR *ptr;
225         INT port;
226         
227         assert( Line > 0 );
228         assert( Var != NULL );
229         assert( Arg != NULL );
230         
231         if( strcasecmp( Var, "Name" ) == 0 )
232         {
233                 /* Der Server-Name */
234                 strncpy( Conf_ServerName, Arg, CLIENT_ID_LEN - 1 );
235                 Conf_ServerName[CLIENT_ID_LEN - 1] = '\0';
236                 return;
237         }
238         if( strcasecmp( Var, "Info" ) == 0 )
239         {
240                 /* Server-Info-Text */
241                 strncpy( Conf_ServerInfo, Arg, CLIENT_INFO_LEN - 1 );
242                 Conf_ServerInfo[CLIENT_INFO_LEN - 1] = '\0';
243                 return;
244         }
245         if( strcasecmp( Var, "Password" ) == 0 )
246         {
247                 /* Server-Passwort */
248                 strncpy( Conf_ServerPwd, Arg, CLIENT_PASS_LEN - 1 );
249                 Conf_ServerPwd[CLIENT_PASS_LEN - 1] = '\0';
250                 return;
251         }
252         if( strcasecmp( Var, "Ports" ) == 0 )
253         {
254                 /* Ports, durch "," getrennt, auf denen der Server
255                 * Verbindungen annehmen soll */
256                 ptr = strtok( Arg, "," );
257                 while( ptr )
258                 {
259                         ngt_TrimStr( ptr );
260                         port = atol( ptr );
261                         if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Log( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
262                         else
263                         {
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 );
266                         }
267                         ptr = strtok( NULL, "," );
268                 }
269                 return;
270         }
271         if( strcasecmp( Var, "MotdFile" ) == 0 )
272         {
273                 /* Datei mit der "message of the day" (MOTD) */
274                 strncpy( Conf_MotdFile, Arg, FNAME_LEN - 1 );
275                 Conf_MotdFile[FNAME_LEN - 1] = '\0';
276                 return;
277         }
278         if( strcasecmp( Var, "PingTimeout" ) == 0 )
279         {
280                 /* PING-Timeout */
281                 Conf_PingTimeout = atoi( Arg );
282                 if(( Conf_PingTimeout ) < 5 ) Conf_PingTimeout = 5;
283                 return;
284         }
285         if( strcasecmp( Var, "PongTimeout" ) == 0 )
286         {
287                 /* PONG-Timeout */
288                 Conf_PongTimeout = atoi( Arg );
289                 if(( Conf_PongTimeout ) < 5 ) Conf_PongTimeout = 5;
290                 return;
291         }
292         if( strcasecmp( Var, "ConnectRetry" ) == 0 )
293         {
294                 /* Sekunden zwischen Verbindungsversuchen zu anderen Servern */
295                 Conf_ConnectRetry = atoi( Arg );
296                 if(( Conf_ConnectRetry ) < 5 ) Conf_ConnectRetry = 5;
297                 return;
298         }
299                 
300         Log( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", Conf_File, Line, Var );
301 } /* Handle_GLOBAL */
302
303
304 GLOBAL VOID Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
305 {
306         assert( Line > 0 );
307         assert( Var != NULL );
308         assert( Arg != NULL );
309         assert( Conf_Oper_Count > 0 );
310
311         if( strcasecmp( Var, "Name" ) == 0 )
312         {
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';
316                 return;
317         }
318         if( strcasecmp( Var, "Password" ) == 0 )
319         {
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';
323                 return;
324         }
325         
326         Log( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", Conf_File, Line, Var );
327 } /* Handle_OPERATOR */
328
329
330 GLOBAL VOID Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
331 {
332         INT32 port;
333         
334         assert( Line > 0 );
335         assert( Var != NULL );
336         assert( Arg != NULL );
337
338         if( strcasecmp( Var, "Host" ) == 0 )
339         {
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';
343                 return;
344         }
345         if( strcasecmp( Var, "Name" ) == 0 )
346         {
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';
350                 return;
351         }
352         if( strcasecmp( Var, "Password" ) == 0 )
353         {
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';
357                 return;
358         }
359         if( strcasecmp( Var, "Port" ) == 0 )
360         {
361                 /* Port, zu dem Verbunden werden soll */
362                 port = atol( Arg );
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 );
365                 return;
366         }
367         if( strcasecmp( Var, "Group" ) == 0 )
368         {
369                 /* Server-Gruppe */
370                 Conf_Server[Conf_Server_Count - 1].group = atoi( Arg );
371                 return;
372         }
373         
374         Log( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", Conf_File, Line, Var );
375 } /* Handle_SERVER */
376
377
378 LOCAL VOID Validate_Config( VOID )
379 {
380         /* Konfiguration ueberpruefen */
381         
382         if( ! Conf_ServerName[0] )
383         {
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!" );
387                 exit( 1 );
388         }
389 } /* Validate_Config */
390
391 /* -eof- */