Blob


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 */
15 #include "portab.h"
17 static char UNUSED id[] = "$Id: conf.c,v 1.68 2005/02/04 14:24:21 alex Exp $";
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>
37 #ifdef HAVE_CTYPE_H
38 # include <ctype.h>
39 #endif
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"
49 #include "exp.h"
50 #include "conf.h"
53 LOCAL BOOLEAN Use_Log = TRUE;
54 LOCAL CONF_SERVER New_Server;
55 LOCAL INT New_Server_Idx;
58 LOCAL VOID Set_Defaults PARAMS(( BOOLEAN InitServers ));
59 LOCAL VOID Read_Config PARAMS(( VOID ));
60 LOCAL VOID Validate_Config PARAMS(( BOOLEAN TestOnly ));
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 ));
67 LOCAL VOID Config_Error PARAMS(( CONST INT Level, CONST CHAR *Format, ... ));
69 LOCAL VOID Config_Error_NaN PARAMS(( INT LINE, CONST CHAR *Value ));
70 LOCAL VOID Config_Error_TooLong PARAMS(( INT LINE, CONST CHAR *Value ));
72 LOCAL VOID Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
75 GLOBAL VOID
76 Conf_Init( VOID )
77 {
78 Set_Defaults( TRUE );
79 Read_Config( );
80 Validate_Config( FALSE );
81 } /* Config_Init */
84 GLOBAL VOID
85 Conf_Rehash( VOID )
86 {
87 Set_Defaults( FALSE );
88 Read_Config( );
89 Validate_Config( FALSE );
90 } /* Config_Rehash */
93 GLOBAL INT
94 Conf_Test( VOID )
95 {
96 /* Read configuration, validate and output it. */
98 struct passwd *pwd;
99 struct group *grp;
100 INT i;
102 Use_Log = FALSE;
103 Set_Defaults( TRUE );
105 Read_Config( );
106 Validate_Config( TRUE );
108 /* If stdin is a valid tty wait for a key: */
109 if( isatty( fileno( stdout )))
111 puts( "OK, press enter to see a dump of your service configuration ..." );
112 getchar( );
114 else puts( "Ok, dump of your server configuration follows:\n" );
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++ )
130 if( i != 0 ) printf( ", " );
131 printf( "%u", Conf_ListenPorts[i] );
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( "" );
153 for( i = 0; i < Conf_Oper_Count; i++ )
155 if( ! Conf_Oper[i].name[0] ) continue;
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( "" );
164 for( i = 0; i < MAX_SERVERS; i++ )
166 if( ! Conf_Server[i].name[0] ) continue;
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( "" );
179 for( i = 0; i < Conf_Channel_Count; i++ )
181 if( ! Conf_Channel[i].name[0] ) continue;
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( "" );
191 return 0;
192 } /* Conf_Test */
195 GLOBAL VOID
196 Conf_UnsetServer( CONN_ID Idx )
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. */
203 INT i;
205 /* Check all our configured servers */
206 for( i = 0; i < MAX_SERVERS; i++ )
208 if( Conf_Server[i].conn_id != Idx ) continue;
210 /* Gotcha! Mark server configuration as "unused": */
211 Conf_Server[i].conn_id = NONE;
213 if( Conf_Server[i].flags & CONF_SFLAG_ONCE )
215 /* Delete configuration here */
216 Init_Server_Struct( &Conf_Server[i] );
218 else
220 /* Set time for next connect attempt */
221 if( Conf_Server[i].lasttry < time( NULL ) - Conf_ConnectRetry )
223 /* Okay, the connection was established "long enough": */
224 Conf_Server[i].lasttry = time( NULL ) - Conf_ConnectRetry + RECONNECT_DELAY;
228 } /* Conf_UnsetServer */
231 GLOBAL VOID
232 Conf_SetServer( INT ConfServer, CONN_ID Idx )
234 /* Set connection for specified configured server */
236 assert( ConfServer > NONE );
237 assert( Idx > NONE );
239 Conf_Server[ConfServer].conn_id = Idx;
240 } /* Conf_SetServer */
243 GLOBAL INT
244 Conf_GetServer( CONN_ID Idx )
246 /* Get index of server in configuration structure */
248 INT i = 0;
250 assert( Idx > NONE );
252 for( i = 0; i < MAX_SERVERS; i++ )
254 if( Conf_Server[i].conn_id == Idx ) return i;
256 return NONE;
257 } /* Conf_GetServer */
260 GLOBAL BOOLEAN
261 Conf_EnableServer( CHAR *Name, INT Port )
263 /* Enable specified server and adjust port */
265 INT i;
267 assert( Name != NULL );
269 for( i = 0; i < MAX_SERVERS; i++ )
271 if( strcasecmp( Conf_Server[i].name, Name ) == 0 )
273 /* Gotcha! Set port and enable server: */
274 Conf_Server[i].port = Port;
275 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
276 return TRUE;
279 return FALSE;
280 } /* Conf_EnableServer */
283 GLOBAL BOOLEAN
284 Conf_DisableServer( CHAR *Name )
286 /* Enable specified server and adjust port */
288 INT i;
290 assert( Name != NULL );
292 for( i = 0; i < MAX_SERVERS; i++ )
294 if( strcasecmp( Conf_Server[i].name, Name ) == 0 )
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;
302 return FALSE;
303 } /* Conf_DisableServer */
306 GLOBAL BOOLEAN
307 Conf_AddServer( CHAR *Name, INT Port, CHAR *Host, CHAR *MyPwd, CHAR *PeerPwd )
309 /* Add new server to configuration */
311 INT i;
313 assert( Name != NULL );
314 assert( Host != NULL );
315 assert( MyPwd != NULL );
316 assert( PeerPwd != NULL );
318 /* Search unused item in server configuration structure */
319 for( i = 0; i < MAX_SERVERS; i++ )
321 /* Is this item used? */
322 if( ! Conf_Server[i].name[0] ) break;
324 if( i >= MAX_SERVERS ) return FALSE;
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;
334 return TRUE;
335 } /* Conf_AddServer */
338 LOCAL VOID
339 Set_Defaults( BOOLEAN InitServers )
341 /* Initialize configuration variables with default values. */
343 INT i;
345 strcpy( Conf_ServerName, "" );
346 sprintf( Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
347 strcpy( Conf_ServerPwd, "" );
349 strcpy( Conf_ServerAdmin1, "" );
350 strcpy( Conf_ServerAdmin2, "" );
351 strcpy( Conf_ServerAdminMail, "" );
353 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
354 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
356 strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
358 strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
360 strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
362 Conf_ListenPorts_Count = 0;
363 strcpy( Conf_ListenAddress, "" );
365 Conf_UID = Conf_GID = 0;
367 Conf_PingTimeout = 120;
368 Conf_PongTimeout = 20;
370 Conf_ConnectRetry = 60;
372 Conf_Oper_Count = 0;
373 Conf_Channel_Count = 0;
375 Conf_OperCanMode = FALSE;
377 Conf_MaxConnections = -1;
378 Conf_MaxConnectionsIP = 5;
379 Conf_MaxJoins = 10;
381 /* Initialize server configuration structures */
382 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
383 } /* Set_Defaults */
386 LOCAL VOID
387 Read_Config( VOID )
389 /* Read configuration file. */
391 CHAR section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
392 INT line, i, n;
393 FILE *fd;
395 /* Open configuration file */
396 fd = fopen( NGIRCd_ConfFile, "r" );
397 if( ! fd )
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 );
405 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
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++ )
415 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
416 else
418 /* This structure is in use ... */
419 if( Conf_Server[i].flags & CONF_SFLAG_ONCE )
421 /* Check for duplicates */
422 for( n = 0; n < MAX_SERVERS; n++ )
424 if( n == i ) continue;
426 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id )
428 Init_Server_Struct( &Conf_Server[n] );
429 Log( LOG_DEBUG, "Deleted unused duplicate server %d (kept %d).", n, i );
433 else
435 /* Mark server as "once" */
436 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
437 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
442 /* Initialize variables */
443 line = 0;
444 strcpy( section, "" );
445 Init_Server_Struct( &New_Server );
446 New_Server_Idx = NONE;
448 /* Read configuration file */
449 while( TRUE )
451 if( ! fgets( str, LINE_LEN, fd )) break;
452 ngt_TrimStr( str );
453 line++;
455 /* Skip comments and empty lines */
456 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
458 /* Is this the beginning of a new section? */
459 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
461 strlcpy( section, str, sizeof( section ));
462 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
463 if( strcasecmp( section, "[OPERATOR]" ) == 0 )
465 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Config_Error( LOG_ERR, "Too many operators configured." );
466 else
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++;
473 continue;
475 if( strcasecmp( section, "[SERVER]" ) == 0 )
477 /* Check if there is already a server to add */
478 if( New_Server.name[0] )
480 /* Copy data to "real" server structure */
481 assert( New_Server_Idx > NONE );
482 Conf_Server[New_Server_Idx] = New_Server;
485 /* Re-init structure for new server */
486 Init_Server_Struct( &New_Server );
488 /* Search unused item in server configuration structure */
489 for( i = 0; i < MAX_SERVERS; i++ )
491 /* Is this item used? */
492 if( ! Conf_Server[i].name[0] ) break;
494 if( i >= MAX_SERVERS )
496 /* Oops, no free item found! */
497 Config_Error( LOG_ERR, "Too many servers configured." );
498 New_Server_Idx = NONE;
500 else New_Server_Idx = i;
501 continue;
503 if( strcasecmp( section, "[CHANNEL]" ) == 0 )
505 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
506 else
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++;
514 continue;
516 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
517 section[0] = 0x1;
519 if( section[0] == 0x1 ) continue;
521 /* Split line into variable name and parameters */
522 ptr = strchr( str, '=' );
523 if( ! ptr )
525 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
526 continue;
528 *ptr = '\0';
529 var = str; ngt_TrimStr( var );
530 arg = ptr + 1; ngt_TrimStr( arg );
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 );
539 /* Close configuration file */
540 fclose( fd );
542 /* Check if there is still a server to add */
543 if( New_Server.name[0] )
545 /* Copy data to "real" server structure */
546 assert( New_Server_Idx > NONE );
547 Conf_Server[New_Server_Idx] = New_Server;
550 /* If there are no ports configured use the default: 6667 */
551 if( Conf_ListenPorts_Count < 1 )
553 Conf_ListenPorts_Count = 1;
554 Conf_ListenPorts[0] = 6667;
556 } /* Read_Config */
559 LOCAL VOID
560 Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
562 struct passwd *pwd;
563 struct group *grp;
564 CHAR *ptr;
565 LONG port;
567 assert( Line > 0 );
568 assert( Var != NULL );
569 assert( Arg != NULL );
571 if( strcasecmp( Var, "Name" ) == 0 )
573 /* Server name */
574 if( strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName )) >= sizeof( Conf_ServerName ))
575 Config_Error_TooLong( Line, Var );
577 return;
579 if( strcasecmp( Var, "Info" ) == 0 )
581 /* Info text of server */
582 if( strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo )) >= sizeof( Conf_ServerInfo ))
583 Config_Error_TooLong ( Line, Var );
585 return;
587 if( strcasecmp( Var, "Password" ) == 0 )
589 /* Global server password */
590 if( strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd )) >= sizeof( Conf_ServerPwd ))
591 Config_Error_TooLong( Line, Var );
593 return;
595 if( strcasecmp( Var, "AdminInfo1" ) == 0 )
597 /* Administrative info #1 */
598 if( strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 )) >= sizeof( Conf_ServerAdmin1 )) Config_Error_TooLong ( Line, Var );
599 return;
601 if( strcasecmp( Var, "AdminInfo2" ) == 0 )
603 /* Administrative info #2 */
604 if( strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 )) >= sizeof( Conf_ServerAdmin2 )) Config_Error_TooLong ( Line, Var );
605 return;
607 if( strcasecmp( Var, "AdminEMail" ) == 0 )
609 /* Administrative email contact */
610 if( strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail )) >= sizeof( Conf_ServerAdminMail )) Config_Error_TooLong( Line, Var );
611 return;
613 if( strcasecmp( Var, "Ports" ) == 0 )
615 /* Ports on that the server should listen. More port numbers
616 * must be separated by "," */
617 ptr = strtok( Arg, "," );
618 while( ptr )
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
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 );
628 ptr = strtok( NULL, "," );
630 return;
632 if( strcasecmp( Var, "MotdFile" ) == 0 )
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 );
638 return;
640 if( strcasecmp( Var, "MotdPhrase" ) == 0 )
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 );
646 return;
648 if( strcasecmp( Var, "ChrootDir" ) == 0 )
650 /* directory for chroot() */
651 if( strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot )) >= sizeof( Conf_Chroot ))
652 Config_Error_TooLong( Line, Var );
654 return;
657 if ( strcasecmp( Var, "PidFile" ) == 0 )
659 /* name of pidfile */
660 if( strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile )) >= sizeof( Conf_PidFile ))
661 Config_Error_TooLong( Line, Var );
663 return;
666 if( strcasecmp( Var, "ServerUID" ) == 0 )
668 /* UID the daemon should switch to */
669 pwd = getpwnam( Arg );
670 if( pwd ) Conf_UID = pwd->pw_uid;
671 else
673 #ifdef HAVE_ISDIGIT
674 if( ! isdigit( (INT)*Arg )) Config_Error_NaN( Line, Var );
675 else
676 #endif
677 Conf_UID = (UINT)atoi( Arg );
679 return;
681 if( strcasecmp( Var, "ServerGID" ) == 0 )
683 /* GID the daemon should use */
684 grp = getgrnam( Arg );
685 if( grp ) Conf_GID = grp->gr_gid;
686 else
688 #ifdef HAVE_ISDIGIT
689 if( ! isdigit( (INT)*Arg )) Config_Error_NaN( Line, Var );
690 else
691 #endif
692 Conf_GID = (UINT)atoi( Arg );
694 return;
696 if( strcasecmp( Var, "PingTimeout" ) == 0 )
698 /* PING timeout */
699 Conf_PingTimeout = atoi( Arg );
700 if( Conf_PingTimeout < 5 )
702 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!", NGIRCd_ConfFile, Line );
703 Conf_PingTimeout = 5;
705 return;
707 if( strcasecmp( Var, "PongTimeout" ) == 0 )
709 /* PONG timeout */
710 Conf_PongTimeout = atoi( Arg );
711 if( Conf_PongTimeout < 5 )
713 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!", NGIRCd_ConfFile, Line );
714 Conf_PongTimeout = 5;
716 return;
718 if( strcasecmp( Var, "ConnectRetry" ) == 0 )
720 /* Seconds between connection attempts to other servers */
721 Conf_ConnectRetry = atoi( Arg );
722 if( Conf_ConnectRetry < 5 )
724 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!", NGIRCd_ConfFile, Line );
725 Conf_ConnectRetry = 5;
727 return;
729 if( strcasecmp( Var, "OperCanUseMode" ) == 0 )
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;
738 if( strcasecmp( Var, "MaxConnections" ) == 0 )
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;
748 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 )
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;
758 if( strcasecmp( Var, "MaxJoins" ) == 0 )
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;
768 if( strcasecmp( Var, "Listen" ) == 0 )
770 /* IP-Address to bind sockets */
771 if( strlcpy( Conf_ListenAddress, Arg, sizeof( Conf_ListenAddress )) >= sizeof( Conf_ListenAddress ))
773 Config_Error_TooLong( Line, Var );
775 return;
778 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
779 } /* Handle_GLOBAL */
782 LOCAL VOID
783 Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
785 assert( Line > 0 );
786 assert( Var != NULL );
787 assert( Arg != NULL );
788 assert( Conf_Oper_Count > 0 );
790 if( strcasecmp( Var, "Name" ) == 0 )
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;
796 if( strcasecmp( Var, "Password" ) == 0 )
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;
803 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
804 } /* Handle_OPERATOR */
807 LOCAL VOID
808 Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
810 LONG port;
812 assert( Line > 0 );
813 assert( Var != NULL );
814 assert( Arg != NULL );
816 /* Ignore server block if no space is left in server configuration structure */
817 if( New_Server_Idx <= NONE ) return;
819 if( strcasecmp( Var, "Host" ) == 0 )
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 );
825 return;
827 if( strcasecmp( Var, "Name" ) == 0 )
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;
834 if( strcasecmp( Var, "MyPassword" ) == 0 )
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;
840 if( strcasecmp( Var, "PeerPassword" ) == 0 )
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;
846 if( strcasecmp( Var, "Port" ) == 0 )
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;
854 if( strcasecmp( Var, "Group" ) == 0 )
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;
865 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
866 } /* Handle_SERVER */
869 LOCAL VOID
870 Handle_CHANNEL( INT Line, CHAR *Var, CHAR *Arg )
872 assert( Line > 0 );
873 assert( Var != NULL );
874 assert( Arg != NULL );
876 if( strcasecmp( Var, "Name" ) == 0 )
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;
883 if( strcasecmp( Var, "Modes" ) == 0 )
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;
890 if( strcasecmp( Var, "Topic" ) == 0 )
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 );
896 return;
899 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
900 } /* Handle_CHANNEL */
903 LOCAL VOID
904 Validate_Config( BOOLEAN Configtest )
906 /* Validate configuration settings. */
908 #ifdef DEBUG
909 INT i, servers, servers_once;
910 #endif
912 if( ! Conf_ServerName[0] )
914 /* No server name configured! */
915 Config_Error( LOG_ALERT, "No server name configured in \"%s\" (section 'Global': 'Name')!", NGIRCd_ConfFile );
916 if( ! Configtest )
918 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
919 exit( 1 );
923 if( Conf_ServerName[0] && ! strchr( Conf_ServerName, '.' ))
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 )
929 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
930 exit( 1 );
934 #ifdef STRICT_RFC
935 if( ! Conf_ServerAdminMail[0] )
937 /* No administrative contact configured! */
938 Config_Error( LOG_ALERT, "No administrator email address configured in \"%s\" ('AdminEMail')!", NGIRCd_ConfFile );
939 if( ! Configtest )
941 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
942 exit( 1 );
945 #endif
947 if( ! Conf_ServerAdmin1[0] && ! Conf_ServerAdmin2[0] && ! Conf_ServerAdminMail[0] )
949 /* No administrative information configured! */
950 Config_Error( LOG_WARNING, "No administrative information configured but required by RFC!" );
952 #ifdef FD_SETSIZE
953 if(( Conf_MaxConnections > (LONG)FD_SETSIZE ) || ( Conf_MaxConnections < 1 ))
955 Conf_MaxConnections = (LONG)FD_SETSIZE;
956 Config_Error( LOG_ERR, "Setting MaxConnections to %ld, select() can't handle more file descriptors!", Conf_MaxConnections );
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
962 #ifdef DEBUG
963 servers = servers_once = 0;
964 for( i = 0; i < MAX_SERVERS; i++ )
966 if( Conf_Server[i].name[0] )
968 servers++;
969 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) servers_once++;
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 */
977 LOCAL VOID
978 Config_Error_TooLong ( CONST INT Line, CONST CHAR *Item )
980 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
983 LOCAL VOID
984 Config_Error_NaN( CONST INT Line, CONST CHAR *Item )
986 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!", NGIRCd_ConfFile, Line, Item );
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
998 /* Error! Write to console and/or logfile. */
1000 CHAR msg[MAX_LOG_MSG_LEN];
1001 va_list ap;
1003 assert( Format != NULL );
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 );
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 */
1021 LOCAL VOID
1022 Init_Server_Struct( CONF_SERVER *Server )
1024 /* Initialize server configuration structur to default values */
1026 assert( Server != NULL );
1028 memset( Server, 0, sizeof (CONF_SERVER) );
1030 Server->group = NONE;
1031 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1033 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1035 Server->conn_id = NONE;
1036 } /* Init_Server_Struct */
1039 /* -eof- */