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