]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conf.c
- Cleaned up handling of server configuration structures.
[ngircd-alex.git] / src / ngircd / conf.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001,2002 Alexander Barton (alex@barton.de)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  *
11  * Configuration management (reading, parsing & validation)
12  */
13
14
15 #include "portab.h"
16
17 static char UNUSED id[] = "$Id: conf.c,v 1.52 2002/12/30 00:01:45 alex Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <pwd.h>
28 #include <grp.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31
32 #ifdef HAVE_CTYPE_H
33 # include <ctype.h>
34 #endif
35
36 #include "ngircd.h"
37 #include "conn.h"
38 #include "client.h"
39 #include "defines.h"
40 #include "log.h"
41 #include "resolve.h"
42 #include "tool.h"
43
44 #include "exp.h"
45 #include "conf.h"
46
47
48 LOCAL BOOLEAN Use_Log = TRUE;
49 LOCAL CONF_SERVER New_Server;
50 LOCAL INT New_Server_Idx;
51
52
53 LOCAL VOID Set_Defaults PARAMS(( BOOLEAN InitServers ));
54 LOCAL VOID Read_Config PARAMS(( VOID ));
55 LOCAL VOID Validate_Config PARAMS(( BOOLEAN TestOnly ));
56
57 LOCAL VOID Handle_GLOBAL PARAMS(( INT Line, CHAR *Var, CHAR *Arg ));
58 LOCAL VOID Handle_OPERATOR PARAMS(( INT Line, CHAR *Var, CHAR *Arg ));
59 LOCAL VOID Handle_SERVER PARAMS(( INT Line, CHAR *Var, CHAR *Arg ));
60 LOCAL VOID Handle_CHANNEL PARAMS(( INT Line, CHAR *Var, CHAR *Arg ));
61
62 LOCAL VOID Config_Error PARAMS(( CONST INT Level, CONST CHAR *Format, ... ));
63
64 LOCAL VOID Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
65
66
67 GLOBAL VOID
68 Conf_Init( VOID )
69 {
70         Set_Defaults( TRUE );
71         Read_Config( );
72         Validate_Config( FALSE );
73 } /* Config_Init */
74
75
76 GLOBAL VOID
77 Conf_Rehash( VOID )
78 {
79         Set_Defaults( FALSE );
80         Read_Config( );
81         Validate_Config( FALSE );
82 } /* Config_Rehash */
83
84
85 GLOBAL INT
86 Conf_Test( VOID )
87 {
88         /* Read configuration, validate and output it. */
89
90         struct passwd *pwd;
91         struct group *grp;
92         INT i;
93
94         Use_Log = FALSE;
95         Set_Defaults( TRUE );
96
97         Read_Config( );
98         Validate_Config( TRUE );
99
100         /* If stdin is a valid tty wait for a key: */
101         if( isatty( fileno( stdout )))
102         {
103                 puts( "OK, press enter to see a dump of your service configuration ..." );
104                 getchar( );
105         }
106         else puts( "Ok, dump of your server configuration follows:\n" );
107
108         puts( "[GLOBAL]" );
109         printf( "  ServerName = %s\n", Conf_ServerName );
110         printf( "  ServerInfo = %s\n", Conf_ServerInfo );
111         printf( "  ServerPwd = %s\n", Conf_ServerPwd );
112         printf( "  AdminInfo1 = %s\n", Conf_ServerAdmin1 );
113         printf( "  AdminInfo2 = %s\n", Conf_ServerAdmin2 );
114         printf( "  AdminEMail = %s\n", Conf_ServerAdminMail );
115         printf( "  MotdFile = %s\n", Conf_MotdFile );
116         printf( "  Ports = " );
117         for( i = 0; i < Conf_ListenPorts_Count; i++ )
118         {
119                 if( i != 0 ) printf( ", " );
120                 printf( "%u", Conf_ListenPorts[i] );
121         }
122         puts( "" );
123         pwd = getpwuid( Conf_UID );
124         if( pwd ) printf( "  ServerUID = %s\n", pwd->pw_name );
125         else printf( "  ServerUID = %ld\n", (LONG)Conf_UID );
126         grp = getgrgid( Conf_GID );
127         if( grp ) printf( "  ServerGID = %s\n", grp->gr_name );
128         else printf( "  ServerGID = %ld\n", (LONG)Conf_GID );
129         printf( "  PingTimeout = %d\n", Conf_PingTimeout );
130         printf( "  PongTimeout = %d\n", Conf_PongTimeout );
131         printf( "  ConnectRetry = %d\n", Conf_ConnectRetry );
132         printf( "  OperCanUseMode = %s\n", Conf_OperCanMode == TRUE ? "yes" : "no" );
133         if( Conf_MaxConnections > 0 ) printf( "  MaxConnections = %ld\n", Conf_MaxConnections );
134         else printf( "  MaxConnections = -1\n" );
135         if( Conf_MaxJoins > 0 ) printf( "  MaxJoins = %d\n", Conf_MaxJoins );
136         else printf( "  MaxJoins = -1\n" );
137         puts( "" );
138
139         for( i = 0; i < Conf_Oper_Count; i++ )
140         {
141                 if( ! Conf_Oper[i].name[0] ) continue;
142                 
143                 /* Valid "Operator" section */
144                 puts( "[OPERATOR]" );
145                 printf( "  Name = %s\n", Conf_Oper[i].name );
146                 printf( "  Password = %s\n", Conf_Oper[i].pwd );
147                 puts( "" );
148         }
149
150         for( i = 0; i < MAX_SERVERS; i++ )
151         {
152                 if( ! Conf_Server[i].name[0] ) continue;
153                 
154                 /* Valid "Server" section */
155                 puts( "[SERVER]" );
156                 printf( "  Name = %s\n", Conf_Server[i].name );
157                 printf( "  Host = %s\n", Conf_Server[i].host );
158                 printf( "  Port = %d\n", Conf_Server[i].port );
159                 printf( "  MyPassword = %s\n", Conf_Server[i].pwd_in );
160                 printf( "  PeerPassword = %s\n", Conf_Server[i].pwd_out );
161                 printf( "  Group = %d\n", Conf_Server[i].group );
162                 puts( "" );
163         }
164
165         for( i = 0; i < Conf_Channel_Count; i++ )
166         {
167                 if( ! Conf_Channel[i].name[0] ) continue;
168                 
169                 /* Valid "Channel" section */
170                 puts( "[CHANNEL]" );
171                 printf( "  Name = %s\n", Conf_Channel[i].name );
172                 printf( "  Modes = %s\n", Conf_Channel[i].modes );
173                 printf( "  Topic = %s\n", Conf_Channel[i].topic );
174                 puts( "" );
175         }
176         
177         return 0;
178 } /* Conf_Test */
179
180
181 GLOBAL VOID
182 Conf_UnsetServer( CONN_ID Idx )
183 {
184         /* Set next time for next connection attempt, if this is a server
185          * link that is (still) configured here. If the server is set as
186          * "once", delete it from our configuration.
187          * Non-Server-Connections will be silently ignored. */
188
189         INT i;
190
191         /* Check all our configured servers */
192         for( i = 0; i < MAX_SERVERS; i++ )
193         {
194                 if( Conf_Server[i].conn_id != Idx ) continue;
195
196                 /* Gotcha! Mark server configuration as "unused": */
197                 Conf_Server[i].conn_id = NONE;
198
199                 if( Conf_Server[i].once )
200                 {
201                         /* Delete configuration here */
202                         Init_Server_Struct( &Conf_Server[i] );
203                 }
204                 else
205                 {
206                         /* Set time for next connect attempt */
207                         if( Conf_Server[i].lasttry <  time( NULL ) - Conf_ConnectRetry )
208                         {
209                                 /* Okay, the connection was established "long enough": */
210                                 Conf_Server[i].lasttry = time( NULL ) - Conf_ConnectRetry + RECONNECT_DELAY;
211                         }
212                 }
213                 break;
214         }
215 } /* Conf_UnsetServer */
216
217
218 GLOBAL VOID
219 Conf_SetServer( INT ConfServer, CONN_ID Idx )
220 {
221         /* Set connection for specified configured server */
222
223         assert( ConfServer > NONE );
224         assert( Idx > NONE );
225
226         Conf_Server[ConfServer].conn_id = Idx;
227 } /* Conf_SetServer */
228
229
230 GLOBAL INT
231 Conf_GetServer( CONN_ID Idx )
232 {
233         /* Get index of server in configuration structure */
234
235         INT i;
236
237         assert( Idx > NONE );
238
239         for( i = 0; i < MAX_SERVERS; i++ )
240         {
241                 if( Conf_Server[i].conn_id == Idx ) return i;
242         }
243         return NONE;
244 } /* Conf_GetServer */
245
246
247 LOCAL VOID
248 Set_Defaults( BOOLEAN InitServers )
249 {
250         /* Initialize configuration variables with default values. */
251
252         INT i;
253
254         strcpy( Conf_ServerName, "" );
255         sprintf( Conf_ServerInfo, "%s %s", PACKAGE, VERSION );
256         strcpy( Conf_ServerPwd, "" );
257
258         strcpy( Conf_ServerAdmin1, "" );
259         strcpy( Conf_ServerAdmin2, "" );
260         strcpy( Conf_ServerAdminMail, "" );
261
262         strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
263         strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
264
265         Conf_ListenPorts_Count = 0;
266
267         Conf_UID = Conf_GID = 0;
268         
269         Conf_PingTimeout = 120;
270         Conf_PongTimeout = 20;
271
272         Conf_ConnectRetry = 60;
273
274         Conf_Oper_Count = 0;
275         Conf_Channel_Count = 0;
276
277         Conf_OperCanMode = FALSE;
278         
279         Conf_MaxConnections = -1;
280         Conf_MaxJoins = 10;
281
282         /* Initialize server configuration structures */
283         if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
284 } /* Set_Defaults */
285
286
287 LOCAL VOID
288 Read_Config( VOID )
289 {
290         /* Read configuration file. */
291
292         CHAR section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
293         INT line, i;
294         FILE *fd;
295
296         /* Open configuration file */
297         fd = fopen( NGIRCd_ConfFile, "r" );
298         if( ! fd )
299         {
300                 /* No configuration file found! */
301                 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s", NGIRCd_ConfFile, strerror( errno ));
302                 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
303                 exit( 1 );
304         }
305
306         Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
307
308         /* Clean up server configuration structure: mark all already
309          * configured servers as "once" so that they are deleted
310          * after the next disconnect and delete all unused servers. */
311         for( i = 0; i < MAX_SERVERS; i++ )
312         {
313                 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
314                 else Conf_Server[i].once = TRUE;
315         }
316
317         /* Initialize variables */
318         line = 0;
319         strcpy( section, "" );
320         Init_Server_Struct( &New_Server );
321         New_Server_Idx = NONE;
322
323         /* Read configuration file */
324         while( TRUE )
325         {
326                 if( ! fgets( str, LINE_LEN, fd )) break;
327                 ngt_TrimStr( str );
328                 line++;
329
330                 /* Skip comments and empty lines */
331                 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
332
333                 /* Is this the beginning of a new section? */
334                 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
335                 {
336                         strlcpy( section, str, sizeof( section ));
337                         if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
338                         if( strcasecmp( section, "[OPERATOR]" ) == 0 )
339                         {
340                                 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Config_Error( LOG_ERR, "Too many operators configured." );
341                                 else
342                                 {
343                                         /* Initialize new operator structure */
344                                         strcpy( Conf_Oper[Conf_Oper_Count].name, "" );
345                                         strcpy( Conf_Oper[Conf_Oper_Count].pwd, "" );
346                                         Conf_Oper_Count++;
347                                 }
348                                 continue;
349                         }
350                         if( strcasecmp( section, "[SERVER]" ) == 0 )
351                         {
352                                 /* Check if there is already a server to add */
353                                 if( New_Server.name[0] )
354                                 {
355                                         /* Copy data to "real" server structure */
356                                         assert( New_Server_Idx > NONE );
357                                         Conf_Server[New_Server_Idx] = New_Server;
358                                 }
359
360                                 /* Re-init structure for new server */
361                                 Init_Server_Struct( &New_Server );
362
363                                 /* Search unused item in server configuration structure */
364                                 for( i = 0; i < MAX_SERVERS; i++ )
365                                 {
366                                         /* Is this item used? */
367                                         if( ! Conf_Server[i].name[0] ) break;
368                                 }
369                                 if( i >= MAX_SERVERS )
370                                 {
371                                         /* Oops, no free item found! */
372                                         Config_Error( LOG_ERR, "Too many servers configured." );
373                                         New_Server_Idx = NONE;
374                                 }
375                                 else New_Server_Idx = i;
376                                 continue;
377                         }
378                         if( strcasecmp( section, "[CHANNEL]" ) == 0 )
379                         {
380                                 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
381                                 else
382                                 {
383                                         /* Initialize new channel structure */
384                                         strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
385                                         strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
386                                         strcpy( Conf_Channel[Conf_Channel_Count].topic, "" );
387                                         Conf_Channel_Count++;
388                                 }
389                                 continue;
390                         }
391                         Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
392                         section[0] = 0x1;
393                 }
394                 if( section[0] == 0x1 ) continue;
395
396                 /* Split line into variable name and parameters */
397                 ptr = strchr( str, '=' );
398                 if( ! ptr )
399                 {
400                         Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
401                         continue;
402                 }
403                 *ptr = '\0';
404                 var = str; ngt_TrimStr( var );
405                 arg = ptr + 1; ngt_TrimStr( arg );
406
407                 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
408                 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
409                 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
410                 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
411                 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
412         }
413
414         /* Close configuration file */
415         fclose( fd );
416
417         /* Check if there is still a server to add */
418         if( New_Server.name[0] )
419         {
420                 /* Copy data to "real" server structure */
421                 assert( New_Server_Idx > NONE );
422                 Conf_Server[New_Server_Idx] = New_Server;
423         }
424         
425         /* If there are no ports configured use the default: 6667 */
426         if( Conf_ListenPorts_Count < 1 )
427         {
428                 Conf_ListenPorts_Count = 1;
429                 Conf_ListenPorts[0] = 6667;
430         }
431 } /* Read_Config */
432
433
434 LOCAL VOID
435 Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
436 {
437         struct passwd *pwd;
438         struct group *grp;
439         CHAR *ptr;
440         LONG port;
441         
442         assert( Line > 0 );
443         assert( Var != NULL );
444         assert( Arg != NULL );
445         
446         if( strcasecmp( Var, "Name" ) == 0 )
447         {
448                 /* Server name */
449                 if( strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName )) >= sizeof( Conf_ServerName )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Name\" too long!", NGIRCd_ConfFile, Line );
450                 return;
451         }
452         if( strcasecmp( Var, "Info" ) == 0 )
453         {
454                 /* Info text of server */
455                 if( strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo )) >= sizeof( Conf_ServerInfo )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Info\" too long!", NGIRCd_ConfFile, Line );
456                 return;
457         }
458         if( strcasecmp( Var, "Password" ) == 0 )
459         {
460                 /* Global server password */
461                 if( strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd )) >= sizeof( Conf_ServerPwd )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Password\" too long!", NGIRCd_ConfFile, Line );
462                 return;
463         }
464         if( strcasecmp( Var, "AdminInfo1" ) == 0 )
465         {
466                 /* Administrative info #1 */
467                 if( strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 )) >= sizeof( Conf_ServerAdmin1 )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"AdminInfo1\" too long!", NGIRCd_ConfFile, Line );
468                 return;
469         }
470         if( strcasecmp( Var, "AdminInfo2" ) == 0 )
471         {
472                 /* Administrative info #2 */
473                 if( strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 )) >= sizeof( Conf_ServerAdmin2 )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"AdminInfo2\" too long!", NGIRCd_ConfFile, Line );
474                 return;
475         }
476         if( strcasecmp( Var, "AdminEMail" ) == 0 )
477         {
478                 /* Administrative email contact */
479                 if( strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail )) >= sizeof( Conf_ServerAdminMail )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"AdminEMail\" too long!", NGIRCd_ConfFile, Line );
480                 return;
481         }
482         if( strcasecmp( Var, "Ports" ) == 0 )
483         {
484                 /* Ports on that the server should listen. More port numbers
485                  * must be separated by "," */
486                 ptr = strtok( Arg, "," );
487                 while( ptr )
488                 {
489                         ngt_TrimStr( ptr );
490                         port = atol( ptr );
491                         if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Config_Error( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
492                         else
493                         {
494                                 if( port > 0 && port < 0xFFFF ) Conf_ListenPorts[Conf_ListenPorts_Count++] = (UINT)port;
495                                 else Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
496                         }
497                         ptr = strtok( NULL, "," );
498                 }
499                 return;
500         }
501         if( strcasecmp( Var, "MotdFile" ) == 0 )
502         {
503                 /* "Message of the day" (MOTD) file */
504                 if( strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile )) >= sizeof( Conf_MotdFile )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"MotdFile\" too long!", NGIRCd_ConfFile, Line );
505                 return;
506         }
507         if( strcasecmp( Var, "ServerUID" ) == 0 )
508         {
509                 /* UID the daemon should switch to */
510                 pwd = getpwnam( Arg );
511                 if( pwd ) Conf_UID = pwd->pw_uid;
512                 else
513                 {
514 #ifdef HAVE_ISDIGIT
515                         if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"ServerUID\" is not a number!", NGIRCd_ConfFile, Line );
516                         else
517 #endif
518                         Conf_UID = (UINT)atoi( Arg );
519                 }
520                 return;
521         }
522         if( strcasecmp( Var, "ServerGID" ) == 0 )
523         {
524                 /* GID the daemon should use */
525                 grp = getgrnam( Arg );
526                 if( grp ) Conf_GID = grp->gr_gid;
527                 else
528                 {
529 #ifdef HAVE_ISDIGIT
530                         if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"ServerGID\" is not a number!", NGIRCd_ConfFile, Line );
531                         else
532 #endif
533                         Conf_GID = (UINT)atoi( Arg );
534                 }
535                 return;
536         }
537         if( strcasecmp( Var, "PingTimeout" ) == 0 )
538         {
539                 /* PING timeout */
540                 Conf_PingTimeout = atoi( Arg );
541                 if( Conf_PingTimeout < 5 )
542                 {
543                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!", NGIRCd_ConfFile, Line );
544                         Conf_PingTimeout = 5;
545                 }
546                 return;
547         }
548         if( strcasecmp( Var, "PongTimeout" ) == 0 )
549         {
550                 /* PONG timeout */
551                 Conf_PongTimeout = atoi( Arg );
552                 if( Conf_PongTimeout < 5 )
553                 {
554                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!", NGIRCd_ConfFile, Line );
555                         Conf_PongTimeout = 5;
556                 }
557                 return;
558         }
559         if( strcasecmp( Var, "ConnectRetry" ) == 0 )
560         {
561                 /* Seconds between connection attempts to other servers */
562                 Conf_ConnectRetry = atoi( Arg );
563                 if( Conf_ConnectRetry < 5 )
564                 {
565                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!", NGIRCd_ConfFile, Line );
566                         Conf_ConnectRetry = 5;
567                 }
568                 return;
569         }
570         if( strcasecmp( Var, "OperCanUseMode" ) == 0 )
571         {
572                 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
573                 if( strcasecmp( Arg, "yes" ) == 0 ) Conf_OperCanMode = TRUE;
574                 else if( strcasecmp( Arg, "true" ) == 0 ) Conf_OperCanMode = TRUE;
575                 else if( atoi( Arg ) != 0 ) Conf_OperCanMode = TRUE;
576                 else Conf_OperCanMode = FALSE;
577                 return;
578         }
579         if( strcasecmp( Var, "MaxConnections" ) == 0 )
580         {
581                 /* Maximum number of connections. Values <= 0 are equal to "no limit". */
582 #ifdef HAVE_ISDIGIT
583                 if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"MaxConnections\" is not a number!", NGIRCd_ConfFile, Line );
584                 else
585 #endif
586                 Conf_MaxConnections = atol( Arg );
587                 return;
588         }
589         if( strcasecmp( Var, "MaxJoins" ) == 0 )
590         {
591                 /* Maximum number of channels a user can join. Values <= 0 are equal to "no limit". */
592 #ifdef HAVE_ISDIGIT
593                 if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"MaxJoins\" is not a number!", NGIRCd_ConfFile, Line );
594                 else
595 #endif
596                 Conf_MaxJoins = atoi( Arg );
597                 return;
598         }
599
600         Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
601 } /* Handle_GLOBAL */
602
603
604 LOCAL VOID
605 Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
606 {
607         assert( Line > 0 );
608         assert( Var != NULL );
609         assert( Arg != NULL );
610         assert( Conf_Oper_Count > 0 );
611
612         if( strcasecmp( Var, "Name" ) == 0 )
613         {
614                 /* Name of IRC operator */
615                 if( strlcpy( Conf_Oper[Conf_Oper_Count - 1].name, Arg, sizeof( Conf_Oper[Conf_Oper_Count - 1].name )) >= sizeof( Conf_Oper[Conf_Oper_Count - 1].name )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Name\" too long!", NGIRCd_ConfFile, Line );
616                 return;
617         }
618         if( strcasecmp( Var, "Password" ) == 0 )
619         {
620                 /* Password of IRC operator */
621                 if( strlcpy( Conf_Oper[Conf_Oper_Count - 1].pwd, Arg, sizeof( Conf_Oper[Conf_Oper_Count - 1].pwd )) >= sizeof( Conf_Oper[Conf_Oper_Count - 1].pwd )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Password\" too long!", NGIRCd_ConfFile, Line );
622                 return;
623         }
624         
625         Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
626 } /* Handle_OPERATOR */
627
628
629 LOCAL VOID
630 Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
631 {
632         LONG port;
633         
634         assert( Line > 0 );
635         assert( Var != NULL );
636         assert( Arg != NULL );
637
638         /* Ignore server block if no space is left in server configuration structure */
639         if( New_Server_Idx <= NONE ) return;
640
641         if( strcasecmp( Var, "Host" ) == 0 )
642         {
643                 /* Hostname of the server */
644                 if( strlcpy( New_Server.host, Arg, sizeof( New_Server.host )) >= sizeof( New_Server.host )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Host\" too long!", NGIRCd_ConfFile, Line );
645                 return;
646         }
647         if( strcasecmp( Var, "Name" ) == 0 )
648         {
649                 /* Name of the server ("Nick"/"ID") */
650                 if( strlcpy( New_Server.name, Arg, sizeof( New_Server.name )) >= sizeof( New_Server.name )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Name\" too long!", NGIRCd_ConfFile, Line );
651                 return;
652         }
653         if( strcasecmp( Var, "MyPassword" ) == 0 )
654         {
655                 /* Password of this server which is sent to the peer */
656                 if( strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in )) >= sizeof( New_Server.pwd_in )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"MyPassword\" too long!", NGIRCd_ConfFile, Line );
657                 return;
658         }
659         if( strcasecmp( Var, "PeerPassword" ) == 0 )
660         {
661                 /* Passwort of the peer which must be received */
662                 if( strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out )) >= sizeof( New_Server.pwd_out )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"PeerPassword\" too long!", NGIRCd_ConfFile, Line );
663                 return;
664         }
665         if( strcasecmp( Var, "Port" ) == 0 )
666         {
667                 /* Port to which this server should connect */
668                 port = atol( Arg );
669                 if( port > 0 && port < 0xFFFF ) New_Server.port = (INT)port;
670                 else Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
671                 return;
672         }
673         if( strcasecmp( Var, "Group" ) == 0 )
674         {
675                 /* Server group */
676 #ifdef HAVE_ISDIGIT
677                 if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Group\" is not a number!", NGIRCd_ConfFile, Line );
678                 else
679 #endif
680                 New_Server.group = atoi( Arg );
681                 return;
682         }
683         
684         Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
685 } /* Handle_SERVER */
686
687
688 LOCAL VOID
689 Handle_CHANNEL( INT Line, CHAR *Var, CHAR *Arg )
690 {
691         assert( Line > 0 );
692         assert( Var != NULL );
693         assert( Arg != NULL );
694
695         if( strcasecmp( Var, "Name" ) == 0 )
696         {
697                 /* Name of the channel */
698                 if( strlcpy( Conf_Channel[Conf_Channel_Count - 1].name, Arg, sizeof( Conf_Channel[Conf_Channel_Count - 1].name )) >= sizeof( Conf_Channel[Conf_Channel_Count - 1].name )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Name\" too long!", NGIRCd_ConfFile, Line );
699                 return;
700         }
701         if( strcasecmp( Var, "Modes" ) == 0 )
702         {
703                 /* Initial modes */
704                 if( strlcpy( Conf_Channel[Conf_Channel_Count - 1].modes, Arg, sizeof( Conf_Channel[Conf_Channel_Count - 1].modes )) >= sizeof( Conf_Channel[Conf_Channel_Count - 1].modes )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Modes\" too long!", NGIRCd_ConfFile, Line );
705                 return;
706         }
707         if( strcasecmp( Var, "Topic" ) == 0 )
708         {
709                 /* Initial topic */
710                 if( strlcpy( Conf_Channel[Conf_Channel_Count - 1].topic, Arg, sizeof( Conf_Channel[Conf_Channel_Count - 1].topic )) >= sizeof( Conf_Channel[Conf_Channel_Count - 1].topic )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Topic\" too long!", NGIRCd_ConfFile, Line );
711                 return;
712         }
713
714         Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
715 } /* Handle_CHANNEL */
716
717
718 LOCAL VOID
719 Validate_Config( BOOLEAN Configtest )
720 {
721         /* Validate configuration settings. */
722
723 #ifdef DEBUG
724         INT i, servers, servers_once;
725 #endif
726
727         if( ! Conf_ServerName[0] )
728         {
729                 /* No server name configured! */
730                 Config_Error( LOG_ALERT, "No server name configured in \"%s\" ('ServerName')!", NGIRCd_ConfFile );
731                 if( ! Configtest )
732                 {
733                         Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
734                         exit( 1 );
735                 }
736         }
737
738 #ifdef STRICT_RFC
739         if( ! Conf_ServerAdminMail[0] )
740         {
741                 /* No administrative contact configured! */
742                 Config_Error( LOG_ALERT, "No administrator email address configured in \"%s\" ('AdminEMail')!", NGIRCd_ConfFile );
743                 if( ! Configtest )
744                 {
745                         Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
746                         exit( 1 );
747                 }
748         }
749 #endif
750
751         if( ! Conf_ServerAdmin1[0] && ! Conf_ServerAdmin2[0] && ! Conf_ServerAdminMail[0] )
752         {
753                 /* No administrative information configured! */
754                 Config_Error( LOG_WARNING, "No administrative information configured but required by RFC!" );
755         }
756 #ifdef FD_SETSIZE       
757         if(( Conf_MaxConnections > (LONG)FD_SETSIZE ) || ( Conf_MaxConnections < 1 ))
758         {
759                 Conf_MaxConnections = (LONG)FD_SETSIZE;
760                 Config_Error( LOG_ERR, "Setting MaxConnections to %ld, select() can't handle more file descriptors!", Conf_MaxConnections );
761         }
762 #else
763         Config_Error( LOG_WARN, "Don't know how many file descriptors select() can handle on this system, don't set MaxConnections too high!" );
764 #endif
765
766 #ifdef DEBUG
767         servers = servers_once = 0;
768         for( i = 0; i < MAX_SERVERS; i++ )
769         {
770                 if( Conf_Server[i].name[0] )
771                 {
772                         servers++;
773                         if( Conf_Server[i].once ) servers_once++;
774                 }
775         }
776         Log( LOG_DEBUG, "Configuration: Operators=%d, Servers=%d[%d], Channels=%d", Conf_Oper_Count, servers, servers_once, Conf_Channel_Count );
777 #endif
778 } /* Validate_Config */
779
780
781 #ifdef PROTOTYPES
782 LOCAL VOID Config_Error( CONST INT Level, CONST CHAR *Format, ... )
783 #else
784 LOCAL VOID Config_Error( Level, Format, va_alist )
785 CONST INT Level;
786 CONST CHAR *Format;
787 va_dcl
788 #endif
789 {
790         /* Error! Write to console and/or logfile. */
791
792         CHAR msg[MAX_LOG_MSG_LEN];
793         va_list ap;
794
795         assert( Format != NULL );
796
797 #ifdef PROTOTYPES
798         va_start( ap, Format );
799 #else
800         va_start( ap );
801 #endif
802         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
803         va_end( ap );
804         
805         /* During "normal operations" the log functions of the daemon should
806          * be used, but during testing of the configuration file, all messages
807          * should go directly to the console: */
808         if( Use_Log ) Log( Level, "%s", msg );
809         else puts( msg );
810 } /* Config_Error */
811
812
813 LOCAL VOID
814 Init_Server_Struct( CONF_SERVER *Server )
815 {
816         /* Initialize server configuration structur to default values */
817
818         assert( Server != NULL );
819
820         strcpy( Server->host, "" );
821         strcpy( Server->ip, "" );
822         strcpy( Server->name, "" );
823         strcpy( Server->pwd_in, "" );
824         strcpy( Server->pwd_out, "" );
825         Server->port = 0;
826         Server->group = NONE;
827         Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
828         Server->res_stat = NULL;
829         Server->once = FALSE;
830         Server->conn_id = NONE;
831 } /* Init_Server_Struct */
832
833
834 /* -eof- */