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