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