]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conf.c
New configuration option "MaxConnectionsIP".
[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.61 2003/11/05 21:41:02 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( "  Password = %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         printf( "  Listen = %s\n", Conf_ListenAddress );
124         pwd = getpwuid( Conf_UID );
125         if( pwd ) printf( "  ServerUID = %s\n", pwd->pw_name );
126         else printf( "  ServerUID = %ld\n", (LONG)Conf_UID );
127         grp = getgrgid( Conf_GID );
128         if( grp ) printf( "  ServerGID = %s\n", grp->gr_name );
129         else printf( "  ServerGID = %ld\n", (LONG)Conf_GID );
130         printf( "  PingTimeout = %d\n", Conf_PingTimeout );
131         printf( "  PongTimeout = %d\n", Conf_PongTimeout );
132         printf( "  ConnectRetry = %d\n", Conf_ConnectRetry );
133         printf( "  OperCanUseMode = %s\n", Conf_OperCanMode == TRUE ? "yes" : "no" );
134         if( Conf_MaxConnections > 0 ) printf( "  MaxConnections = %ld\n", Conf_MaxConnections );
135         else printf( "  MaxConnections = -1\n" );
136         if( Conf_MaxConnectionsIP > 0 ) printf( "  MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP );
137         else printf( "  MaxConnectionsIP = -1\n" );
138         if( Conf_MaxJoins > 0 ) printf( "  MaxJoins = %d\n", Conf_MaxJoins );
139         else printf( "  MaxJoins = -1\n" );
140         puts( "" );
141
142         for( i = 0; i < Conf_Oper_Count; i++ )
143         {
144                 if( ! Conf_Oper[i].name[0] ) continue;
145                 
146                 /* Valid "Operator" section */
147                 puts( "[OPERATOR]" );
148                 printf( "  Name = %s\n", Conf_Oper[i].name );
149                 printf( "  Password = %s\n", Conf_Oper[i].pwd );
150                 puts( "" );
151         }
152
153         for( i = 0; i < MAX_SERVERS; i++ )
154         {
155                 if( ! Conf_Server[i].name[0] ) continue;
156                 
157                 /* Valid "Server" section */
158                 puts( "[SERVER]" );
159                 printf( "  Name = %s\n", Conf_Server[i].name );
160                 printf( "  Host = %s\n", Conf_Server[i].host );
161                 printf( "  Port = %d\n", Conf_Server[i].port );
162                 printf( "  MyPassword = %s\n", Conf_Server[i].pwd_in );
163                 printf( "  PeerPassword = %s\n", Conf_Server[i].pwd_out );
164                 printf( "  Group = %d\n", Conf_Server[i].group );
165                 puts( "" );
166         }
167
168         for( i = 0; i < Conf_Channel_Count; i++ )
169         {
170                 if( ! Conf_Channel[i].name[0] ) continue;
171                 
172                 /* Valid "Channel" section */
173                 puts( "[CHANNEL]" );
174                 printf( "  Name = %s\n", Conf_Channel[i].name );
175                 printf( "  Modes = %s\n", Conf_Channel[i].modes );
176                 printf( "  Topic = %s\n", Conf_Channel[i].topic );
177                 puts( "" );
178         }
179         
180         return 0;
181 } /* Conf_Test */
182
183
184 GLOBAL VOID
185 Conf_UnsetServer( CONN_ID Idx )
186 {
187         /* Set next time for next connection attempt, if this is a server
188          * link that is (still) configured here. If the server is set as
189          * "once", delete it from our configuration.
190          * Non-Server-Connections will be silently ignored. */
191
192         INT i;
193
194         /* Check all our configured servers */
195         for( i = 0; i < MAX_SERVERS; i++ )
196         {
197                 if( Conf_Server[i].conn_id != Idx ) continue;
198
199                 /* Gotcha! Mark server configuration as "unused": */
200                 Conf_Server[i].conn_id = NONE;
201
202                 if( Conf_Server[i].flags & CONF_SFLAG_ONCE )
203                 {
204                         /* Delete configuration here */
205                         Init_Server_Struct( &Conf_Server[i] );
206                 }
207                 else
208                 {
209                         /* Set time for next connect attempt */
210                         if( Conf_Server[i].lasttry <  time( NULL ) - Conf_ConnectRetry )
211                         {
212                                 /* Okay, the connection was established "long enough": */
213                                 Conf_Server[i].lasttry = time( NULL ) - Conf_ConnectRetry + RECONNECT_DELAY;
214                         }
215                 }
216         }
217 } /* Conf_UnsetServer */
218
219
220 GLOBAL VOID
221 Conf_SetServer( INT ConfServer, CONN_ID Idx )
222 {
223         /* Set connection for specified configured server */
224
225         assert( ConfServer > NONE );
226         assert( Idx > NONE );
227
228         Conf_Server[ConfServer].conn_id = Idx;
229 } /* Conf_SetServer */
230
231
232 GLOBAL INT
233 Conf_GetServer( CONN_ID Idx )
234 {
235         /* Get index of server in configuration structure */
236         
237         INT i = 0;
238         
239         assert( Idx > NONE );
240
241         for( i = 0; i < MAX_SERVERS; i++ )
242         {
243                 if( Conf_Server[i].conn_id == Idx ) return i;
244         }
245         return NONE;
246 } /* Conf_GetServer */
247
248
249 GLOBAL BOOLEAN
250 Conf_EnableServer( CHAR *Name, INT Port )
251 {
252         /* Enable specified server and adjust port */
253
254         INT i;
255
256         assert( Name != NULL );
257
258         for( i = 0; i < MAX_SERVERS; i++ )
259         {
260                 if( strcasecmp( Conf_Server[i].name, Name ) == 0 )
261                 {
262                         /* Gotcha! Set port and enable server: */
263                         Conf_Server[i].port = Port;
264                         Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
265                         return TRUE;
266                 }
267         }
268         return FALSE;
269 } /* Conf_EnableServer */
270
271
272 GLOBAL BOOLEAN
273 Conf_DisableServer( CHAR *Name )
274 {
275         /* Enable specified server and adjust port */
276
277         INT i;
278
279         assert( Name != NULL );
280
281         for( i = 0; i < MAX_SERVERS; i++ )
282         {
283                 if( strcasecmp( Conf_Server[i].name, Name ) == 0 )
284                 {
285                         /* Gotcha! Disable and disconnect server: */
286                         Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
287                         if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", TRUE );
288                         return TRUE;
289                 }
290         }
291         return FALSE;
292 } /* Conf_DisableServer */
293
294
295 GLOBAL BOOLEAN
296 Conf_AddServer( CHAR *Name, INT Port, CHAR *Host, CHAR *MyPwd, CHAR *PeerPwd )
297 {
298         /* Add new server to configuration */
299
300         INT i;
301
302         assert( Name != NULL );
303         assert( Host != NULL );
304         assert( MyPwd != NULL );
305         assert( PeerPwd != NULL );
306
307         /* Search unused item in server configuration structure */
308         for( i = 0; i < MAX_SERVERS; i++ )
309         {
310                 /* Is this item used? */
311                 if( ! Conf_Server[i].name[0] ) break;
312         }
313         if( i >= MAX_SERVERS ) return FALSE;
314
315         Init_Server_Struct( &Conf_Server[i] );
316         strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
317         strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
318         strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
319         strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
320         Conf_Server[i].port = Port;
321         Conf_Server[i].flags = CONF_SFLAG_ONCE;
322         
323         return TRUE;
324 } /* Conf_AddServer */
325
326
327 LOCAL VOID
328 Set_Defaults( BOOLEAN InitServers )
329 {
330         /* Initialize configuration variables with default values. */
331
332         INT i;
333
334         strcpy( Conf_ServerName, "" );
335         sprintf( Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
336         strcpy( Conf_ServerPwd, "" );
337
338         strcpy( Conf_ServerAdmin1, "" );
339         strcpy( Conf_ServerAdmin2, "" );
340         strcpy( Conf_ServerAdminMail, "" );
341
342         strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
343         strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
344
345         Conf_ListenPorts_Count = 0;
346         strcpy( Conf_ListenAddress, "" );
347
348         Conf_UID = Conf_GID = 0;
349         
350         Conf_PingTimeout = 120;
351         Conf_PongTimeout = 20;
352
353         Conf_ConnectRetry = 60;
354
355         Conf_Oper_Count = 0;
356         Conf_Channel_Count = 0;
357
358         Conf_OperCanMode = FALSE;
359         
360         Conf_MaxConnections = -1;
361         Conf_MaxConnectionsIP = 5;
362         Conf_MaxJoins = 10;
363
364         /* Initialize server configuration structures */
365         if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
366 } /* Set_Defaults */
367
368
369 LOCAL VOID
370 Read_Config( VOID )
371 {
372         /* Read configuration file. */
373
374         CHAR section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
375         INT line, i, n;
376         FILE *fd;
377
378         /* Open configuration file */
379         fd = fopen( NGIRCd_ConfFile, "r" );
380         if( ! fd )
381         {
382                 /* No configuration file found! */
383                 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s", NGIRCd_ConfFile, strerror( errno ));
384                 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
385                 exit( 1 );
386         }
387
388         Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
389
390         /* Clean up server configuration structure: mark all already
391          * configured servers as "once" so that they are deleted
392          * after the next disconnect and delete all unused servers.
393          * And delete all servers which are "duplicates" of servers
394          * that are already marked as "once" (such servers have been
395          * created by the last rehash but are now useless). */
396         for( i = 0; i < MAX_SERVERS; i++ )
397         {
398                 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
399                 else
400                 {
401                         /* This structure is in use ... */
402                         if( Conf_Server[i].flags & CONF_SFLAG_ONCE )
403                         {
404                                 /* Check for duplicates */
405                                 for( n = 0; n < MAX_SERVERS; n++ )
406                                 {
407                                         if( n == i ) continue;
408
409                                         if( Conf_Server[i].conn_id == Conf_Server[n].conn_id )
410                                         {
411                                                 Init_Server_Struct( &Conf_Server[n] );
412                                                 Log( LOG_DEBUG, "Deleted unused duplicate server %d (kept %d).", n, i );
413                                         }
414                                 }
415                         }
416                         else
417                         {
418                                 /* Mark server as "once" */
419                                 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
420                                 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
421                         }
422                 }
423         }
424
425         /* Initialize variables */
426         line = 0;
427         strcpy( section, "" );
428         Init_Server_Struct( &New_Server );
429         New_Server_Idx = NONE;
430
431         /* Read configuration file */
432         while( TRUE )
433         {
434                 if( ! fgets( str, LINE_LEN, fd )) break;
435                 ngt_TrimStr( str );
436                 line++;
437
438                 /* Skip comments and empty lines */
439                 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
440
441                 /* Is this the beginning of a new section? */
442                 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
443                 {
444                         strlcpy( section, str, sizeof( section ));
445                         if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
446                         if( strcasecmp( section, "[OPERATOR]" ) == 0 )
447                         {
448                                 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Config_Error( LOG_ERR, "Too many operators configured." );
449                                 else
450                                 {
451                                         /* Initialize new operator structure */
452                                         strcpy( Conf_Oper[Conf_Oper_Count].name, "" );
453                                         strcpy( Conf_Oper[Conf_Oper_Count].pwd, "" );
454                                         Conf_Oper_Count++;
455                                 }
456                                 continue;
457                         }
458                         if( strcasecmp( section, "[SERVER]" ) == 0 )
459                         {
460                                 /* Check if there is already a server to add */
461                                 if( New_Server.name[0] )
462                                 {
463                                         /* Copy data to "real" server structure */
464                                         assert( New_Server_Idx > NONE );
465                                         Conf_Server[New_Server_Idx] = New_Server;
466                                 }
467
468                                 /* Re-init structure for new server */
469                                 Init_Server_Struct( &New_Server );
470
471                                 /* Search unused item in server configuration structure */
472                                 for( i = 0; i < MAX_SERVERS; i++ )
473                                 {
474                                         /* Is this item used? */
475                                         if( ! Conf_Server[i].name[0] ) break;
476                                 }
477                                 if( i >= MAX_SERVERS )
478                                 {
479                                         /* Oops, no free item found! */
480                                         Config_Error( LOG_ERR, "Too many servers configured." );
481                                         New_Server_Idx = NONE;
482                                 }
483                                 else New_Server_Idx = i;
484                                 continue;
485                         }
486                         if( strcasecmp( section, "[CHANNEL]" ) == 0 )
487                         {
488                                 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
489                                 else
490                                 {
491                                         /* Initialize new channel structure */
492                                         strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
493                                         strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
494                                         strcpy( Conf_Channel[Conf_Channel_Count].topic, "" );
495                                         Conf_Channel_Count++;
496                                 }
497                                 continue;
498                         }
499                         Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
500                         section[0] = 0x1;
501                 }
502                 if( section[0] == 0x1 ) continue;
503
504                 /* Split line into variable name and parameters */
505                 ptr = strchr( str, '=' );
506                 if( ! ptr )
507                 {
508                         Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
509                         continue;
510                 }
511                 *ptr = '\0';
512                 var = str; ngt_TrimStr( var );
513                 arg = ptr + 1; ngt_TrimStr( arg );
514
515                 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
516                 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
517                 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
518                 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
519                 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
520         }
521
522         /* Close configuration file */
523         fclose( fd );
524
525         /* Check if there is still a server to add */
526         if( New_Server.name[0] )
527         {
528                 /* Copy data to "real" server structure */
529                 assert( New_Server_Idx > NONE );
530                 Conf_Server[New_Server_Idx] = New_Server;
531         }
532         
533         /* If there are no ports configured use the default: 6667 */
534         if( Conf_ListenPorts_Count < 1 )
535         {
536                 Conf_ListenPorts_Count = 1;
537                 Conf_ListenPorts[0] = 6667;
538         }
539 } /* Read_Config */
540
541
542 LOCAL VOID
543 Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
544 {
545         struct passwd *pwd;
546         struct group *grp;
547         CHAR *ptr;
548         LONG port;
549         
550         assert( Line > 0 );
551         assert( Var != NULL );
552         assert( Arg != NULL );
553         
554         if( strcasecmp( Var, "Name" ) == 0 )
555         {
556                 /* Server name */
557                 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 );
558                 return;
559         }
560         if( strcasecmp( Var, "Info" ) == 0 )
561         {
562                 /* Info text of server */
563                 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 );
564                 return;
565         }
566         if( strcasecmp( Var, "Password" ) == 0 )
567         {
568                 /* Global server password */
569                 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 );
570                 return;
571         }
572         if( strcasecmp( Var, "AdminInfo1" ) == 0 )
573         {
574                 /* Administrative info #1 */
575                 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 );
576                 return;
577         }
578         if( strcasecmp( Var, "AdminInfo2" ) == 0 )
579         {
580                 /* Administrative info #2 */
581                 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 );
582                 return;
583         }
584         if( strcasecmp( Var, "AdminEMail" ) == 0 )
585         {
586                 /* Administrative email contact */
587                 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 );
588                 return;
589         }
590         if( strcasecmp( Var, "Ports" ) == 0 )
591         {
592                 /* Ports on that the server should listen. More port numbers
593                  * must be separated by "," */
594                 ptr = strtok( Arg, "," );
595                 while( ptr )
596                 {
597                         ngt_TrimStr( ptr );
598                         port = atol( ptr );
599                         if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Config_Error( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
600                         else
601                         {
602                                 if( port > 0 && port < 0xFFFF ) Conf_ListenPorts[Conf_ListenPorts_Count++] = (UINT)port;
603                                 else Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
604                         }
605                         ptr = strtok( NULL, "," );
606                 }
607                 return;
608         }
609         if( strcasecmp( Var, "MotdFile" ) == 0 )
610         {
611                 /* "Message of the day" (MOTD) file */
612                 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 );
613                 return;
614         }
615         if( strcasecmp( Var, "ServerUID" ) == 0 )
616         {
617                 /* UID the daemon should switch to */
618                 pwd = getpwnam( Arg );
619                 if( pwd ) Conf_UID = pwd->pw_uid;
620                 else
621                 {
622 #ifdef HAVE_ISDIGIT
623                         if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"ServerUID\" is not a number!", NGIRCd_ConfFile, Line );
624                         else
625 #endif
626                         Conf_UID = (UINT)atoi( Arg );
627                 }
628                 return;
629         }
630         if( strcasecmp( Var, "ServerGID" ) == 0 )
631         {
632                 /* GID the daemon should use */
633                 grp = getgrnam( Arg );
634                 if( grp ) Conf_GID = grp->gr_gid;
635                 else
636                 {
637 #ifdef HAVE_ISDIGIT
638                         if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"ServerGID\" is not a number!", NGIRCd_ConfFile, Line );
639                         else
640 #endif
641                         Conf_GID = (UINT)atoi( Arg );
642                 }
643                 return;
644         }
645         if( strcasecmp( Var, "PingTimeout" ) == 0 )
646         {
647                 /* PING timeout */
648                 Conf_PingTimeout = atoi( Arg );
649                 if( Conf_PingTimeout < 5 )
650                 {
651                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!", NGIRCd_ConfFile, Line );
652                         Conf_PingTimeout = 5;
653                 }
654                 return;
655         }
656         if( strcasecmp( Var, "PongTimeout" ) == 0 )
657         {
658                 /* PONG timeout */
659                 Conf_PongTimeout = atoi( Arg );
660                 if( Conf_PongTimeout < 5 )
661                 {
662                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!", NGIRCd_ConfFile, Line );
663                         Conf_PongTimeout = 5;
664                 }
665                 return;
666         }
667         if( strcasecmp( Var, "ConnectRetry" ) == 0 )
668         {
669                 /* Seconds between connection attempts to other servers */
670                 Conf_ConnectRetry = atoi( Arg );
671                 if( Conf_ConnectRetry < 5 )
672                 {
673                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!", NGIRCd_ConfFile, Line );
674                         Conf_ConnectRetry = 5;
675                 }
676                 return;
677         }
678         if( strcasecmp( Var, "OperCanUseMode" ) == 0 )
679         {
680                 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
681                 if( strcasecmp( Arg, "yes" ) == 0 ) Conf_OperCanMode = TRUE;
682                 else if( strcasecmp( Arg, "true" ) == 0 ) Conf_OperCanMode = TRUE;
683                 else if( atoi( Arg ) != 0 ) Conf_OperCanMode = TRUE;
684                 else Conf_OperCanMode = FALSE;
685                 return;
686         }
687         if( strcasecmp( Var, "MaxConnections" ) == 0 )
688         {
689                 /* Maximum number of connections. Values <= 0 are equal to "no limit". */
690 #ifdef HAVE_ISDIGIT
691                 if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"MaxConnections\" is not a number!", NGIRCd_ConfFile, Line );
692                 else
693 #endif
694                 Conf_MaxConnections = atol( Arg );
695                 return;
696         }
697         if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 )
698         {
699                 /* Maximum number of simoultanous connections from one IP. Values <= 0 are equal to "no limit". */
700 #ifdef HAVE_ISDIGIT
701                 if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"MaxConnectionsIP\" is not a number!", NGIRCd_ConfFile, Line );
702                 else
703 #endif
704                 Conf_MaxConnectionsIP = atoi( Arg );
705                 return;
706         }
707         if( strcasecmp( Var, "MaxJoins" ) == 0 )
708         {
709                 /* Maximum number of channels a user can join. Values <= 0 are equal to "no limit". */
710 #ifdef HAVE_ISDIGIT
711                 if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"MaxJoins\" is not a number!", NGIRCd_ConfFile, Line );
712                 else
713 #endif
714                 Conf_MaxJoins = atoi( Arg );
715                 return;
716         }
717         if( strcasecmp( Var, "Listen" ) == 0 )
718         {
719                 /* IP-Address to bind sockets */
720                 if( strlcpy( Conf_ListenAddress, Arg, sizeof( Conf_ListenAddress )) >= sizeof( Conf_ListenAddress ))
721                 {
722                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"Listen\" too long!", NGIRCd_ConfFile, Line );
723                 }
724                 return;
725         }
726
727         Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
728 } /* Handle_GLOBAL */
729
730
731 LOCAL VOID
732 Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
733 {
734         assert( Line > 0 );
735         assert( Var != NULL );
736         assert( Arg != NULL );
737         assert( Conf_Oper_Count > 0 );
738
739         if( strcasecmp( Var, "Name" ) == 0 )
740         {
741                 /* Name of IRC operator */
742                 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 );
743                 return;
744         }
745         if( strcasecmp( Var, "Password" ) == 0 )
746         {
747                 /* Password of IRC operator */
748                 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 );
749                 return;
750         }
751         
752         Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
753 } /* Handle_OPERATOR */
754
755
756 LOCAL VOID
757 Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
758 {
759         LONG port;
760         
761         assert( Line > 0 );
762         assert( Var != NULL );
763         assert( Arg != NULL );
764
765         /* Ignore server block if no space is left in server configuration structure */
766         if( New_Server_Idx <= NONE ) return;
767
768         if( strcasecmp( Var, "Host" ) == 0 )
769         {
770                 /* Hostname of the server */
771                 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 );
772                 return;
773         }
774         if( strcasecmp( Var, "Name" ) == 0 )
775         {
776                 /* Name of the server ("Nick"/"ID") */
777                 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 );
778                 return;
779         }
780         if( strcasecmp( Var, "MyPassword" ) == 0 )
781         {
782                 /* Password of this server which is sent to the peer */
783                 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 );
784                 return;
785         }
786         if( strcasecmp( Var, "PeerPassword" ) == 0 )
787         {
788                 /* Passwort of the peer which must be received */
789                 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 );
790                 return;
791         }
792         if( strcasecmp( Var, "Port" ) == 0 )
793         {
794                 /* Port to which this server should connect */
795                 port = atol( Arg );
796                 if( port > 0 && port < 0xFFFF ) New_Server.port = (INT)port;
797                 else Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
798                 return;
799         }
800         if( strcasecmp( Var, "Group" ) == 0 )
801         {
802                 /* Server group */
803 #ifdef HAVE_ISDIGIT
804                 if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Group\" is not a number!", NGIRCd_ConfFile, Line );
805                 else
806 #endif
807                 New_Server.group = atoi( Arg );
808                 return;
809         }
810         
811         Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
812 } /* Handle_SERVER */
813
814
815 LOCAL VOID
816 Handle_CHANNEL( INT Line, CHAR *Var, CHAR *Arg )
817 {
818         assert( Line > 0 );
819         assert( Var != NULL );
820         assert( Arg != NULL );
821
822         if( strcasecmp( Var, "Name" ) == 0 )
823         {
824                 /* Name of the channel */
825                 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 );
826                 return;
827         }
828         if( strcasecmp( Var, "Modes" ) == 0 )
829         {
830                 /* Initial modes */
831                 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 );
832                 return;
833         }
834         if( strcasecmp( Var, "Topic" ) == 0 )
835         {
836                 /* Initial topic */
837                 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 );
838                 return;
839         }
840
841         Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
842 } /* Handle_CHANNEL */
843
844
845 LOCAL VOID
846 Validate_Config( BOOLEAN Configtest )
847 {
848         /* Validate configuration settings. */
849
850 #ifdef DEBUG
851         INT i, servers, servers_once;
852 #endif
853
854         if( ! Conf_ServerName[0] )
855         {
856                 /* No server name configured! */
857                 Config_Error( LOG_ALERT, "No server name configured in \"%s\" ('ServerName')!", NGIRCd_ConfFile );
858                 if( ! Configtest )
859                 {
860                         Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
861                         exit( 1 );
862                 }
863         }
864         
865         if( ! strchr( Conf_ServerName, '.' ))
866         {
867                 /* No dot in server name! */
868                 Config_Error( LOG_ALERT, "Invalid server name configured in \"%s\" ('ServerName'): Dot missing!", NGIRCd_ConfFile );
869                 if( ! Configtest )
870                 {
871                         Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
872                         exit( 1 );
873                 }
874         }
875
876 #ifdef STRICT_RFC
877         if( ! Conf_ServerAdminMail[0] )
878         {
879                 /* No administrative contact configured! */
880                 Config_Error( LOG_ALERT, "No administrator email address configured in \"%s\" ('AdminEMail')!", NGIRCd_ConfFile );
881                 if( ! Configtest )
882                 {
883                         Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
884                         exit( 1 );
885                 }
886         }
887 #endif
888
889         if( ! Conf_ServerAdmin1[0] && ! Conf_ServerAdmin2[0] && ! Conf_ServerAdminMail[0] )
890         {
891                 /* No administrative information configured! */
892                 Config_Error( LOG_WARNING, "No administrative information configured but required by RFC!" );
893         }
894 #ifdef FD_SETSIZE       
895         if(( Conf_MaxConnections > (LONG)FD_SETSIZE ) || ( Conf_MaxConnections < 1 ))
896         {
897                 Conf_MaxConnections = (LONG)FD_SETSIZE;
898                 Config_Error( LOG_ERR, "Setting MaxConnections to %ld, select() can't handle more file descriptors!", Conf_MaxConnections );
899         }
900 #else
901         Config_Error( LOG_WARN, "Don't know how many file descriptors select() can handle on this system, don't set MaxConnections too high!" );
902 #endif
903
904 #ifdef DEBUG
905         servers = servers_once = 0;
906         for( i = 0; i < MAX_SERVERS; i++ )
907         {
908                 if( Conf_Server[i].name[0] )
909                 {
910                         servers++;
911                         if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) servers_once++;
912                 }
913         }
914         Log( LOG_DEBUG, "Configuration: Operators=%d, Servers=%d[%d], Channels=%d", Conf_Oper_Count, servers, servers_once, Conf_Channel_Count );
915 #endif
916 } /* Validate_Config */
917
918
919 #ifdef PROTOTYPES
920 LOCAL VOID Config_Error( CONST INT Level, CONST CHAR *Format, ... )
921 #else
922 LOCAL VOID Config_Error( Level, Format, va_alist )
923 CONST INT Level;
924 CONST CHAR *Format;
925 va_dcl
926 #endif
927 {
928         /* Error! Write to console and/or logfile. */
929
930         CHAR msg[MAX_LOG_MSG_LEN];
931         va_list ap;
932
933         assert( Format != NULL );
934
935 #ifdef PROTOTYPES
936         va_start( ap, Format );
937 #else
938         va_start( ap );
939 #endif
940         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
941         va_end( ap );
942         
943         /* During "normal operations" the log functions of the daemon should
944          * be used, but during testing of the configuration file, all messages
945          * should go directly to the console: */
946         if( Use_Log ) Log( Level, "%s", msg );
947         else puts( msg );
948 } /* Config_Error */
949
950
951 LOCAL VOID
952 Init_Server_Struct( CONF_SERVER *Server )
953 {
954         /* Initialize server configuration structur to default values */
955
956         assert( Server != NULL );
957
958         strcpy( Server->host, "" );
959         strcpy( Server->ip, "" );
960         strcpy( Server->name, "" );
961         strcpy( Server->pwd_in, "" );
962         strcpy( Server->pwd_out, "" );
963         Server->port = 0;
964         Server->group = NONE;
965         Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
966         Server->res_stat = NULL;
967         if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
968         else Server->flags = 0;
969         Server->conn_id = NONE;
970 } /* Init_Server_Struct */
971
972
973 /* -eof- */