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