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