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