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