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