]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conf.c
- Fehlermeldungen korrigiert.
[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.12 2002/01/05 23:26:24 alex Exp $
13  *
14  * conf.h: Konfiguration des ngircd
15  *
16  * $Log: conf.c,v $
17  * Revision 1.12  2002/01/05 23:26:24  alex
18  * - Fehlermeldungen korrigiert.
19  *
20  * Revision 1.11  2002/01/05 16:51:49  alex
21  * - Bug bei Remote-Server-Namen entfernt: diese wurden falsch gekuerzt.
22  *
23  * Revision 1.10  2002/01/03 02:27:20  alex
24  * - das Server-Passwort kann nun konfiguriert werden.
25  *
26  * Revision 1.9  2002/01/02 02:49:15  alex
27  * - Konfigurationsdatei "Samba like" umgestellt.
28  * - es koennen nun mehrere Server und Oprtatoren konfiguriert werden.
29  *
30  * Revision 1.7  2002/01/01 18:25:44  alex
31  * - #include's fuer stdlib.h ergaenzt.
32  *
33  * Revision 1.6  2001/12/31 02:18:51  alex
34  * - viele neue Befehle (WHOIS, ISON, OPER, DIE, RESTART),
35  * - neuen Header "defines.h" mit (fast) allen Konstanten.
36  * - Code Cleanups und viele "kleine" Aenderungen & Bugfixes.
37  *
38  * Revision 1.5  2001/12/30 19:26:11  alex
39  * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
40  *
41  * Revision 1.4  2001/12/26 22:48:53  alex
42  * - MOTD-Datei ist nun konfigurierbar und wird gelesen.
43  *
44  * Revision 1.3  2001/12/26 14:45:37  alex
45  * - "Code Cleanups".
46  *
47  * Revision 1.2  2001/12/26 03:19:57  alex
48  * - erste Konfigurations-Variablen definiert: PING/PONG-Timeout.
49  *
50  * Revision 1.1  2001/12/12 17:18:20  alex
51  * - Modul fuer Server-Konfiguration begonnen.
52  */
53
54
55 #include <portab.h>
56 #include "global.h"
57
58 #include <imp.h>
59 #include <assert.h>
60 #include <errno.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64
65 #include "client.h"
66 #include "log.h"
67 #include "tool.h"
68
69 #include <exp.h>
70 #include "conf.h"
71
72
73 LOCAL VOID Read_Config( VOID );
74
75 GLOBAL VOID Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg );
76 GLOBAL VOID Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg );
77 GLOBAL VOID Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg );
78
79 LOCAL VOID Validate_Config( VOID );
80
81
82 GLOBAL VOID Conf_Init( VOID )
83 {
84         /* Konfigurationsvariablen initialisieren: zunaechst Default-
85          * Werte setzen, dann Konfigurationsdtaei einlesen. */
86
87         strcpy( Conf_File, "/usr/local/etc/ngircd.conf" );
88
89         strcpy( Conf_ServerName, "" );
90         strcpy( Conf_ServerInfo, PACKAGE" "VERSION );
91         strcpy( Conf_ServerPwd, "" );
92
93         strcpy( Conf_MotdFile, "/usr/local/etc/ngircd.motd" );
94
95         Conf_ListenPorts_Count = 0;
96         
97         Conf_PingTimeout = 120;
98         Conf_PongTimeout = 10;
99
100         Conf_ConnectRetry = 60;
101
102         Conf_Oper_Count = 0;
103
104         Conf_Server_Count = 0;
105
106         /* Konfigurationsdatei einlesen und validieren */
107         Read_Config( );
108         Validate_Config( );
109 } /* Config_Init */
110
111
112 GLOBAL VOID Conf_Exit( VOID )
113 {
114         /* ... */
115 } /* Config_Exit */
116
117
118 LOCAL VOID Read_Config( VOID )
119 {
120         /* Konfigurationsdatei einlesen. */
121
122         CHAR section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
123         INT line;
124         FILE *fd;
125
126         fd = fopen( Conf_File, "r" );
127         if( ! fd )
128         {
129                 /* Keine Konfigurationsdatei gefunden */
130                 Log( LOG_ALERT, "Can't read configuration \"%s\": %s", Conf_File, strerror( errno ));
131                 Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
132                 exit( 1 );
133         }
134
135         line = 0;
136         strcpy( section, "" );
137         while( TRUE )
138         {
139                 if( ! fgets( str, LINE_LEN, fd )) break;
140                 ngt_TrimStr( str );
141                 line++;
142
143                 /* Kommentarzeilen und leere Zeilen ueberspringen */
144                 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
145
146                 /* Anfang eines Abschnittes? */
147                 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
148                 {
149                         strcpy( section, str );
150                         if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
151                         if( strcasecmp( section, "[OPERATOR]" ) == 0 )
152                         {
153                                 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Log( LOG_ERR, "Too many operators configured." );
154                                 else
155                                 {
156                                         /* neuen Operator initialisieren */
157                                         strcpy( Conf_Oper[Conf_Oper_Count].name, "" );
158                                         strcpy( Conf_Oper[Conf_Oper_Count].pwd, "" );
159                                         Conf_Oper_Count++;
160                                 }
161                                 continue;
162                         }
163                         if( strcasecmp( section, "[SERVER]" ) == 0 )
164                         {
165                                 if( Conf_Server_Count + 1 > MAX_SERVERS ) Log( LOG_ERR, "Too many servers configured." );
166                                 else
167                                 {
168                                         /* neuen Server ("Peer") initialisieren */
169                                         strcpy( Conf_Server[Conf_Server_Count].host, "" );
170                                         strcpy( Conf_Server[Conf_Server_Count].ip, "" );
171                                         strcpy( Conf_Server[Conf_Server_Count].name, "" );
172                                         strcpy( Conf_Server[Conf_Server_Count].pwd, "" );
173                                         Conf_Server[Conf_Server_Count].port = 0;
174                                         Conf_Server[Conf_Server_Count].lasttry = 0;
175                                         Conf_Server[Conf_Server_Count].res_stat = NULL;
176                                         Conf_Server_Count++;
177                                 }
178                                 continue;
179                         }
180                         Log( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", Conf_File, line, section );
181                         section[0] = 0x1;
182                 }
183                 if( section[0] == 0x1 ) continue;
184
185                 /* In Variable und Argument zerlegen */
186                 ptr = strchr( str, '=' );
187                 if( ! ptr )
188                 {
189                         Log( LOG_ERR, "%s, line %d: Syntax error!", Conf_File, line );
190                         continue;
191                 }
192                 *ptr = '\0';
193                 var = str; ngt_TrimStr( var );
194                 arg = ptr + 1; ngt_TrimStr( arg );
195
196                 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
197                 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
198                 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
199                 else Log( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", Conf_File, line, var );
200         }
201         
202         fclose( fd );
203 } /* Read_Config */
204
205
206 GLOBAL VOID Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
207 {
208         CHAR *ptr;
209         INT port;
210         
211         assert( Line > 0 );
212         assert( Var != NULL );
213         assert( Arg != NULL );
214         
215         if( strcasecmp( Var, "Name" ) == 0 )
216         {
217                 /* Der Server-Name */
218                 strncpy( Conf_ServerName, Arg, CLIENT_ID_LEN );
219                 Conf_ServerName[CLIENT_ID_LEN - 1] = '\0';
220                 return;
221         }
222         if( strcasecmp( Var, "Info" ) == 0 )
223         {
224                 /* Server-Info-Text */
225                 strncpy( Conf_ServerInfo, Arg, CLIENT_INFO_LEN );
226                 Conf_ServerInfo[CLIENT_INFO_LEN - 1] = '\0';
227                 return;
228         }
229         if( strcasecmp( Var, "Password" ) == 0 )
230         {
231                 /* Der Server-Name */
232                 strncpy( Conf_ServerPwd, Arg, CLIENT_PASS_LEN );
233                 Conf_ServerPwd[CLIENT_PASS_LEN - 1] = '\0';
234                 return;
235         }
236         if( strcasecmp( Var, "Ports" ) == 0 )
237         {
238                 /* Ports, durch "," getrennt, auf denen der Server
239                 * Verbindungen annehmen soll */
240                 ptr = strtok( Arg, "," );
241                 while( ptr )
242                 {
243                         ngt_TrimStr( ptr );
244                         port = atol( ptr );
245                         if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Log( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
246                         else
247                         {
248                                 if( port > 0 && port < 0xFFFF ) Conf_ListenPorts[Conf_ListenPorts_Count++] = port;
249                                 else Log( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!", Conf_File, Line, port );
250                         }
251                         ptr = strtok( NULL, "," );
252                 }
253                 return;
254         }
255         if( strcasecmp( Var, "MotdFile" ) == 0 )
256         {
257                 /* Datei mit der "message of the day" (MOTD) */
258                 strncpy( Conf_MotdFile, Arg, FNAME_LEN );
259                 Conf_MotdFile[FNAME_LEN - 1] = '\0';
260                 return;
261         }
262         if( strcasecmp( Var, "PingTimeout" ) == 0 )
263         {
264                 /* PING-Timeout */
265                 Conf_PingTimeout = atoi( Arg );
266                 if(( Conf_PingTimeout ) < 5 ) Conf_PingTimeout = 5;
267                 return;
268         }
269         if( strcasecmp( Var, "PongTimeout" ) == 0 )
270         {
271                 /* PONG-Timeout */
272                 Conf_PongTimeout = atoi( Arg );
273                 if(( Conf_PongTimeout ) < 5 ) Conf_PongTimeout = 5;
274                 return;
275         }
276         if( strcasecmp( Var, "ConnectRetry" ) == 0 )
277         {
278                 /* Sekunden zwischen Verbindungsversuchen zu anderen Servern */
279                 Conf_ConnectRetry = atoi( Arg );
280                 if(( Conf_ConnectRetry ) < 5 ) Conf_ConnectRetry = 5;
281                 return;
282         }
283                 
284         Log( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", Conf_File, Line, Var );
285 } /* Handle_GLOBAL */
286
287
288 GLOBAL VOID Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
289 {
290         assert( Line > 0 );
291         assert( Var != NULL );
292         assert( Arg != NULL );
293         assert( Conf_Oper_Count > 0 );
294
295         if( strcasecmp( Var, "Name" ) == 0 )
296         {
297                 /* Name des IRC Operator */
298                 strncpy( Conf_Oper[Conf_Oper_Count - 1].name, Arg, CLIENT_ID_LEN );
299                 Conf_Oper[Conf_Oper_Count - 1].name[CLIENT_ID_LEN - 1] = '\0';
300                 return;
301         }
302         if( strcasecmp( Var, "Password" ) == 0 )
303         {
304                 /* Passwort des IRC Operator */
305                 strncpy( Conf_Oper[Conf_Oper_Count - 1].pwd, Arg, CLIENT_PASS_LEN );
306                 Conf_Oper[Conf_Oper_Count - 1].pwd[CLIENT_PASS_LEN - 1] = '\0';
307                 return;
308         }
309         
310         Log( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", Conf_File, Line, Var );
311 } /* Handle_OPERATOR */
312
313
314 GLOBAL VOID Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
315 {
316         INT port;
317         
318         assert( Line > 0 );
319         assert( Var != NULL );
320         assert( Arg != NULL );
321
322         if( strcasecmp( Var, "Host" ) == 0 )
323         {
324                 /* Hostname des Servers */
325                 strncpy( Conf_Server[Conf_Server_Count - 1].host, Arg, HOST_LEN );
326                 Conf_Server[Conf_Server_Count - 1].host[HOST_LEN - 1] = '\0';
327                 return;
328         }
329         if( strcasecmp( Var, "Name" ) == 0 )
330         {
331                 /* Name des Servers ("Nick") */
332                 strncpy( Conf_Server[Conf_Server_Count - 1].name, Arg, CLIENT_ID_LEN );
333                 Conf_Server[Conf_Server_Count - 1].name[CLIENT_ID_LEN - 1] = '\0';
334                 return;
335         }
336         if( strcasecmp( Var, "Password" ) == 0 )
337         {
338                 /* Passwort des Servers */
339                 strncpy( Conf_Server[Conf_Server_Count - 1].pwd, Arg, CLIENT_PASS_LEN );
340                 Conf_Server[Conf_Server_Count - 1].pwd[CLIENT_PASS_LEN - 1] = '\0';
341                 return;
342         }
343         if( strcasecmp( Var, "Port" ) == 0 )
344         {
345                 /* Port, zu dem Verbunden werden soll */
346                 port = atol( Arg );
347                 if( port > 0 && port < 0xFFFF ) Conf_Server[Conf_Server_Count - 1].port = port;
348                 else Log( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!", Conf_File, Line, port );
349                 return;
350         }
351         
352         Log( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", Conf_File, Line, Var );
353 } /* Handle_SERVER */
354
355
356 LOCAL VOID Validate_Config( VOID )
357 {
358         /* Konfiguration ueberpruefen */
359         
360         if( ! Conf_ServerName[0] )
361         {
362                 /* Kein Servername konfiguriert */
363                 Log( LOG_ALERT, "No server name configured in \"%s\"!", Conf_File );
364                 Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
365                 exit( 1 );
366         }
367 } /* Validate_Config */
368
369 /* -eof- */