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