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.66 2004/10/20 13:47:32 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 Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
72 GLOBAL VOID
73 Conf_Init( VOID )
74 {
75 Set_Defaults( TRUE );
76 Read_Config( );
77 Validate_Config( FALSE );
78 } /* Config_Init */
81 GLOBAL VOID
82 Conf_Rehash( VOID )
83 {
84 Set_Defaults( FALSE );
85 Read_Config( );
86 Validate_Config( FALSE );
87 } /* Config_Rehash */
90 GLOBAL INT
91 Conf_Test( VOID )
92 {
93 /* Read configuration, validate and output it. */
95 struct passwd *pwd;
96 struct group *grp;
97 INT i;
99 Use_Log = FALSE;
100 Set_Defaults( TRUE );
102 Read_Config( );
103 Validate_Config( TRUE );
105 /* If stdin is a valid tty wait for a key: */
106 if( isatty( fileno( stdout )))
108 puts( "OK, press enter to see a dump of your service configuration ..." );
109 getchar( );
111 else puts( "Ok, dump of your server configuration follows:\n" );
113 puts( "[GLOBAL]" );
114 printf( " Name = %s\n", Conf_ServerName );
115 printf( " Info = %s\n", Conf_ServerInfo );
116 printf( " Password = %s\n", Conf_ServerPwd );
117 printf( " AdminInfo1 = %s\n", Conf_ServerAdmin1 );
118 printf( " AdminInfo2 = %s\n", Conf_ServerAdmin2 );
119 printf( " AdminEMail = %s\n", Conf_ServerAdminMail );
120 printf( " MotdFile = %s\n", Conf_MotdFile );
121 printf( " MotdPhrase = %s\n", Conf_MotdPhrase );
122 printf( " ChrootDir= %s\n", Conf_Chroot );
123 printf( " Ports = " );
124 for( i = 0; i < Conf_ListenPorts_Count; i++ )
126 if( i != 0 ) printf( ", " );
127 printf( "%u", Conf_ListenPorts[i] );
129 puts( "" );
130 printf( " Listen = %s\n", Conf_ListenAddress );
131 pwd = getpwuid( Conf_UID );
132 if( pwd ) printf( " ServerUID = %s\n", pwd->pw_name );
133 else printf( " ServerUID = %ld\n", (LONG)Conf_UID );
134 grp = getgrgid( Conf_GID );
135 if( grp ) printf( " ServerGID = %s\n", grp->gr_name );
136 else printf( " ServerGID = %ld\n", (LONG)Conf_GID );
137 printf( " PingTimeout = %d\n", Conf_PingTimeout );
138 printf( " PongTimeout = %d\n", Conf_PongTimeout );
139 printf( " ConnectRetry = %d\n", Conf_ConnectRetry );
140 printf( " OperCanUseMode = %s\n", Conf_OperCanMode == TRUE ? "yes" : "no" );
141 if( Conf_MaxConnections > 0 ) printf( " MaxConnections = %ld\n", Conf_MaxConnections );
142 else printf( " MaxConnections = -1\n" );
143 if( Conf_MaxConnectionsIP > 0 ) printf( " MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP );
144 else printf( " MaxConnectionsIP = -1\n" );
145 if( Conf_MaxJoins > 0 ) printf( " MaxJoins = %d\n", Conf_MaxJoins );
146 else printf( " MaxJoins = -1\n" );
147 puts( "" );
149 for( i = 0; i < Conf_Oper_Count; i++ )
151 if( ! Conf_Oper[i].name[0] ) continue;
153 /* Valid "Operator" section */
154 puts( "[OPERATOR]" );
155 printf( " Name = %s\n", Conf_Oper[i].name );
156 printf( " Password = %s\n", Conf_Oper[i].pwd );
157 puts( "" );
160 for( i = 0; i < MAX_SERVERS; i++ )
162 if( ! Conf_Server[i].name[0] ) continue;
164 /* Valid "Server" section */
165 puts( "[SERVER]" );
166 printf( " Name = %s\n", Conf_Server[i].name );
167 printf( " Host = %s\n", Conf_Server[i].host );
168 printf( " Port = %d\n", Conf_Server[i].port );
169 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
170 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
171 printf( " Group = %d\n", Conf_Server[i].group );
172 puts( "" );
175 for( i = 0; i < Conf_Channel_Count; i++ )
177 if( ! Conf_Channel[i].name[0] ) continue;
179 /* Valid "Channel" section */
180 puts( "[CHANNEL]" );
181 printf( " Name = %s\n", Conf_Channel[i].name );
182 printf( " Modes = %s\n", Conf_Channel[i].modes );
183 printf( " Topic = %s\n", Conf_Channel[i].topic );
184 puts( "" );
187 return 0;
188 } /* Conf_Test */
191 GLOBAL VOID
192 Conf_UnsetServer( CONN_ID Idx )
194 /* Set next time for next connection attempt, if this is a server
195 * link that is (still) configured here. If the server is set as
196 * "once", delete it from our configuration.
197 * Non-Server-Connections will be silently ignored. */
199 INT i;
201 /* Check all our configured servers */
202 for( i = 0; i < MAX_SERVERS; i++ )
204 if( Conf_Server[i].conn_id != Idx ) continue;
206 /* Gotcha! Mark server configuration as "unused": */
207 Conf_Server[i].conn_id = NONE;
209 if( Conf_Server[i].flags & CONF_SFLAG_ONCE )
211 /* Delete configuration here */
212 Init_Server_Struct( &Conf_Server[i] );
214 else
216 /* Set time for next connect attempt */
217 if( Conf_Server[i].lasttry < time( NULL ) - Conf_ConnectRetry )
219 /* Okay, the connection was established "long enough": */
220 Conf_Server[i].lasttry = time( NULL ) - Conf_ConnectRetry + RECONNECT_DELAY;
224 } /* Conf_UnsetServer */
227 GLOBAL VOID
228 Conf_SetServer( INT ConfServer, CONN_ID Idx )
230 /* Set connection for specified configured server */
232 assert( ConfServer > NONE );
233 assert( Idx > NONE );
235 Conf_Server[ConfServer].conn_id = Idx;
236 } /* Conf_SetServer */
239 GLOBAL INT
240 Conf_GetServer( CONN_ID Idx )
242 /* Get index of server in configuration structure */
244 INT i = 0;
246 assert( Idx > NONE );
248 for( i = 0; i < MAX_SERVERS; i++ )
250 if( Conf_Server[i].conn_id == Idx ) return i;
252 return NONE;
253 } /* Conf_GetServer */
256 GLOBAL BOOLEAN
257 Conf_EnableServer( CHAR *Name, INT Port )
259 /* Enable specified server and adjust port */
261 INT i;
263 assert( Name != NULL );
265 for( i = 0; i < MAX_SERVERS; i++ )
267 if( strcasecmp( Conf_Server[i].name, Name ) == 0 )
269 /* Gotcha! Set port and enable server: */
270 Conf_Server[i].port = Port;
271 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
272 return TRUE;
275 return FALSE;
276 } /* Conf_EnableServer */
279 GLOBAL BOOLEAN
280 Conf_DisableServer( CHAR *Name )
282 /* Enable specified server and adjust port */
284 INT i;
286 assert( Name != NULL );
288 for( i = 0; i < MAX_SERVERS; i++ )
290 if( strcasecmp( Conf_Server[i].name, Name ) == 0 )
292 /* Gotcha! Disable and disconnect server: */
293 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
294 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", TRUE );
295 return TRUE;
298 return FALSE;
299 } /* Conf_DisableServer */
302 GLOBAL BOOLEAN
303 Conf_AddServer( CHAR *Name, INT Port, CHAR *Host, CHAR *MyPwd, CHAR *PeerPwd )
305 /* Add new server to configuration */
307 INT i;
309 assert( Name != NULL );
310 assert( Host != NULL );
311 assert( MyPwd != NULL );
312 assert( PeerPwd != NULL );
314 /* Search unused item in server configuration structure */
315 for( i = 0; i < MAX_SERVERS; i++ )
317 /* Is this item used? */
318 if( ! Conf_Server[i].name[0] ) break;
320 if( i >= MAX_SERVERS ) return FALSE;
322 Init_Server_Struct( &Conf_Server[i] );
323 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
324 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
325 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
326 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
327 Conf_Server[i].port = Port;
328 Conf_Server[i].flags = CONF_SFLAG_ONCE;
330 return TRUE;
331 } /* Conf_AddServer */
334 LOCAL VOID
335 Set_Defaults( BOOLEAN InitServers )
337 /* Initialize configuration variables with default values. */
339 INT i;
341 strcpy( Conf_ServerName, "" );
342 sprintf( Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
343 strcpy( Conf_ServerPwd, "" );
345 strcpy( Conf_ServerAdmin1, "" );
346 strcpy( Conf_ServerAdmin2, "" );
347 strcpy( Conf_ServerAdminMail, "" );
349 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
350 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
352 strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
354 strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
356 Conf_ListenPorts_Count = 0;
357 strcpy( Conf_ListenAddress, "" );
359 Conf_UID = Conf_GID = 0;
361 Conf_PingTimeout = 120;
362 Conf_PongTimeout = 20;
364 Conf_ConnectRetry = 60;
366 Conf_Oper_Count = 0;
367 Conf_Channel_Count = 0;
369 Conf_OperCanMode = FALSE;
371 Conf_MaxConnections = -1;
372 Conf_MaxConnectionsIP = 5;
373 Conf_MaxJoins = 10;
375 /* Initialize server configuration structures */
376 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
377 } /* Set_Defaults */
380 LOCAL VOID
381 Read_Config( VOID )
383 /* Read configuration file. */
385 CHAR section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
386 INT line, i, n;
387 FILE *fd;
389 /* Open configuration file */
390 fd = fopen( NGIRCd_ConfFile, "r" );
391 if( ! fd )
393 /* No configuration file found! */
394 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s", NGIRCd_ConfFile, strerror( errno ));
395 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
396 exit( 1 );
399 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
401 /* Clean up server configuration structure: mark all already
402 * configured servers as "once" so that they are deleted
403 * after the next disconnect and delete all unused servers.
404 * And delete all servers which are "duplicates" of servers
405 * that are already marked as "once" (such servers have been
406 * created by the last rehash but are now useless). */
407 for( i = 0; i < MAX_SERVERS; i++ )
409 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
410 else
412 /* This structure is in use ... */
413 if( Conf_Server[i].flags & CONF_SFLAG_ONCE )
415 /* Check for duplicates */
416 for( n = 0; n < MAX_SERVERS; n++ )
418 if( n == i ) continue;
420 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id )
422 Init_Server_Struct( &Conf_Server[n] );
423 Log( LOG_DEBUG, "Deleted unused duplicate server %d (kept %d).", n, i );
427 else
429 /* Mark server as "once" */
430 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
431 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
436 /* Initialize variables */
437 line = 0;
438 strcpy( section, "" );
439 Init_Server_Struct( &New_Server );
440 New_Server_Idx = NONE;
442 /* Read configuration file */
443 while( TRUE )
445 if( ! fgets( str, LINE_LEN, fd )) break;
446 ngt_TrimStr( str );
447 line++;
449 /* Skip comments and empty lines */
450 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
452 /* Is this the beginning of a new section? */
453 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
455 strlcpy( section, str, sizeof( section ));
456 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
457 if( strcasecmp( section, "[OPERATOR]" ) == 0 )
459 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Config_Error( LOG_ERR, "Too many operators configured." );
460 else
462 /* Initialize new operator structure */
463 strcpy( Conf_Oper[Conf_Oper_Count].name, "" );
464 strcpy( Conf_Oper[Conf_Oper_Count].pwd, "" );
465 Conf_Oper_Count++;
467 continue;
469 if( strcasecmp( section, "[SERVER]" ) == 0 )
471 /* Check if there is already a server to add */
472 if( New_Server.name[0] )
474 /* Copy data to "real" server structure */
475 assert( New_Server_Idx > NONE );
476 Conf_Server[New_Server_Idx] = New_Server;
479 /* Re-init structure for new server */
480 Init_Server_Struct( &New_Server );
482 /* Search unused item in server configuration structure */
483 for( i = 0; i < MAX_SERVERS; i++ )
485 /* Is this item used? */
486 if( ! Conf_Server[i].name[0] ) break;
488 if( i >= MAX_SERVERS )
490 /* Oops, no free item found! */
491 Config_Error( LOG_ERR, "Too many servers configured." );
492 New_Server_Idx = NONE;
494 else New_Server_Idx = i;
495 continue;
497 if( strcasecmp( section, "[CHANNEL]" ) == 0 )
499 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
500 else
502 /* Initialize new channel structure */
503 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
504 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
505 strcpy( Conf_Channel[Conf_Channel_Count].topic, "" );
506 Conf_Channel_Count++;
508 continue;
510 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
511 section[0] = 0x1;
513 if( section[0] == 0x1 ) continue;
515 /* Split line into variable name and parameters */
516 ptr = strchr( str, '=' );
517 if( ! ptr )
519 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
520 continue;
522 *ptr = '\0';
523 var = str; ngt_TrimStr( var );
524 arg = ptr + 1; ngt_TrimStr( arg );
526 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
527 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
528 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
529 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
530 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
533 /* Close configuration file */
534 fclose( fd );
536 /* Check if there is still a server to add */
537 if( New_Server.name[0] )
539 /* Copy data to "real" server structure */
540 assert( New_Server_Idx > NONE );
541 Conf_Server[New_Server_Idx] = New_Server;
544 /* If there are no ports configured use the default: 6667 */
545 if( Conf_ListenPorts_Count < 1 )
547 Conf_ListenPorts_Count = 1;
548 Conf_ListenPorts[0] = 6667;
550 } /* Read_Config */
553 LOCAL VOID
554 Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
556 struct passwd *pwd;
557 struct group *grp;
558 CHAR *ptr;
559 LONG port;
561 assert( Line > 0 );
562 assert( Var != NULL );
563 assert( Arg != NULL );
565 if( strcasecmp( Var, "Name" ) == 0 )
567 /* Server name */
568 if( strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName )) >= sizeof( Conf_ServerName )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Name\" too long!", NGIRCd_ConfFile, Line );
569 return;
571 if( strcasecmp( Var, "Info" ) == 0 )
573 /* Info text of server */
574 if( strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo )) >= sizeof( Conf_ServerInfo )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Info\" too long!", NGIRCd_ConfFile, Line );
575 return;
577 if( strcasecmp( Var, "Password" ) == 0 )
579 /* Global server password */
580 if( strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd )) >= sizeof( Conf_ServerPwd )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Password\" too long!", NGIRCd_ConfFile, Line );
581 return;
583 if( strcasecmp( Var, "AdminInfo1" ) == 0 )
585 /* Administrative info #1 */
586 if( strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 )) >= sizeof( Conf_ServerAdmin1 )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"AdminInfo1\" too long!", NGIRCd_ConfFile, Line );
587 return;
589 if( strcasecmp( Var, "AdminInfo2" ) == 0 )
591 /* Administrative info #2 */
592 if( strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 )) >= sizeof( Conf_ServerAdmin2 )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"AdminInfo2\" too long!", NGIRCd_ConfFile, Line );
593 return;
595 if( strcasecmp( Var, "AdminEMail" ) == 0 )
597 /* Administrative email contact */
598 if( strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail )) >= sizeof( Conf_ServerAdminMail )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"AdminEMail\" too long!", NGIRCd_ConfFile, Line );
599 return;
601 if( strcasecmp( Var, "Ports" ) == 0 )
603 /* Ports on that the server should listen. More port numbers
604 * must be separated by "," */
605 ptr = strtok( Arg, "," );
606 while( ptr )
608 ngt_TrimStr( ptr );
609 port = atol( ptr );
610 if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Config_Error( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
611 else
613 if( port > 0 && port < 0xFFFF ) Conf_ListenPorts[Conf_ListenPorts_Count++] = (UINT)port;
614 else Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
616 ptr = strtok( NULL, "," );
618 return;
620 if( strcasecmp( Var, "MotdFile" ) == 0 )
622 /* "Message of the day" (MOTD) file */
623 if( strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile )) >= sizeof( Conf_MotdFile )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"MotdFile\" too long!", NGIRCd_ConfFile, Line );
624 return;
626 if( strcasecmp( Var, "MotdPhrase" ) == 0 )
628 /* "Message of the day" phrase (instead of file) */
629 if( strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase )) >= sizeof( Conf_MotdPhrase )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"MotdPhrase\" too long!", NGIRCd_ConfFile, Line );
630 return;
632 if( strcasecmp( Var, "ChrootDir" ) == 0 )
634 /* directory for chroot() */
635 if( strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot )) >= sizeof( Conf_Chroot )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"ChrootDir\" too long!", NGIRCd_ConfFile, Line );
636 return;
638 if( strcasecmp( Var, "ServerUID" ) == 0 )
640 /* UID the daemon should switch to */
641 pwd = getpwnam( Arg );
642 if( pwd ) Conf_UID = pwd->pw_uid;
643 else
645 #ifdef HAVE_ISDIGIT
646 if( ! isdigit( (INT)*Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"ServerUID\" is not a number!", NGIRCd_ConfFile, Line );
647 else
648 #endif
649 Conf_UID = (UINT)atoi( Arg );
651 return;
653 if( strcasecmp( Var, "ServerGID" ) == 0 )
655 /* GID the daemon should use */
656 grp = getgrnam( Arg );
657 if( grp ) Conf_GID = grp->gr_gid;
658 else
660 #ifdef HAVE_ISDIGIT
661 if( ! isdigit( (INT)*Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"ServerGID\" is not a number!", NGIRCd_ConfFile, Line );
662 else
663 #endif
664 Conf_GID = (UINT)atoi( Arg );
666 return;
668 if( strcasecmp( Var, "PingTimeout" ) == 0 )
670 /* PING timeout */
671 Conf_PingTimeout = atoi( Arg );
672 if( Conf_PingTimeout < 5 )
674 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!", NGIRCd_ConfFile, Line );
675 Conf_PingTimeout = 5;
677 return;
679 if( strcasecmp( Var, "PongTimeout" ) == 0 )
681 /* PONG timeout */
682 Conf_PongTimeout = atoi( Arg );
683 if( Conf_PongTimeout < 5 )
685 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!", NGIRCd_ConfFile, Line );
686 Conf_PongTimeout = 5;
688 return;
690 if( strcasecmp( Var, "ConnectRetry" ) == 0 )
692 /* Seconds between connection attempts to other servers */
693 Conf_ConnectRetry = atoi( Arg );
694 if( Conf_ConnectRetry < 5 )
696 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!", NGIRCd_ConfFile, Line );
697 Conf_ConnectRetry = 5;
699 return;
701 if( strcasecmp( Var, "OperCanUseMode" ) == 0 )
703 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
704 if( strcasecmp( Arg, "yes" ) == 0 ) Conf_OperCanMode = TRUE;
705 else if( strcasecmp( Arg, "true" ) == 0 ) Conf_OperCanMode = TRUE;
706 else if( atoi( Arg ) != 0 ) Conf_OperCanMode = TRUE;
707 else Conf_OperCanMode = FALSE;
708 return;
710 if( strcasecmp( Var, "MaxConnections" ) == 0 )
712 /* Maximum number of connections. Values <= 0 are equal to "no limit". */
713 #ifdef HAVE_ISDIGIT
714 if( ! isdigit( (INT)*Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"MaxConnections\" is not a number!", NGIRCd_ConfFile, Line );
715 else
716 #endif
717 Conf_MaxConnections = atol( Arg );
718 return;
720 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 )
722 /* Maximum number of simoultanous connections from one IP. Values <= 0 are equal to "no limit". */
723 #ifdef HAVE_ISDIGIT
724 if( ! isdigit( (INT)*Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"MaxConnectionsIP\" is not a number!", NGIRCd_ConfFile, Line );
725 else
726 #endif
727 Conf_MaxConnectionsIP = atoi( Arg );
728 return;
730 if( strcasecmp( Var, "MaxJoins" ) == 0 )
732 /* Maximum number of channels a user can join. Values <= 0 are equal to "no limit". */
733 #ifdef HAVE_ISDIGIT
734 if( ! isdigit( (INT)*Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"MaxJoins\" is not a number!", NGIRCd_ConfFile, Line );
735 else
736 #endif
737 Conf_MaxJoins = atoi( Arg );
738 return;
740 if( strcasecmp( Var, "Listen" ) == 0 )
742 /* IP-Address to bind sockets */
743 if( strlcpy( Conf_ListenAddress, Arg, sizeof( Conf_ListenAddress )) >= sizeof( Conf_ListenAddress ))
745 Config_Error( LOG_WARNING, "%s, line %d: Value of \"Listen\" too long!", NGIRCd_ConfFile, Line );
747 return;
750 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
751 } /* Handle_GLOBAL */
754 LOCAL VOID
755 Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
757 assert( Line > 0 );
758 assert( Var != NULL );
759 assert( Arg != NULL );
760 assert( Conf_Oper_Count > 0 );
762 if( strcasecmp( Var, "Name" ) == 0 )
764 /* Name of IRC operator */
765 if( strlcpy( Conf_Oper[Conf_Oper_Count - 1].name, Arg, sizeof( Conf_Oper[Conf_Oper_Count - 1].name )) >= sizeof( Conf_Oper[Conf_Oper_Count - 1].name )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Name\" too long!", NGIRCd_ConfFile, Line );
766 return;
768 if( strcasecmp( Var, "Password" ) == 0 )
770 /* Password of IRC operator */
771 if( strlcpy( Conf_Oper[Conf_Oper_Count - 1].pwd, Arg, sizeof( Conf_Oper[Conf_Oper_Count - 1].pwd )) >= sizeof( Conf_Oper[Conf_Oper_Count - 1].pwd )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Password\" too long!", NGIRCd_ConfFile, Line );
772 return;
775 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
776 } /* Handle_OPERATOR */
779 LOCAL VOID
780 Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
782 LONG port;
784 assert( Line > 0 );
785 assert( Var != NULL );
786 assert( Arg != NULL );
788 /* Ignore server block if no space is left in server configuration structure */
789 if( New_Server_Idx <= NONE ) return;
791 if( strcasecmp( Var, "Host" ) == 0 )
793 /* Hostname of the server */
794 if( strlcpy( New_Server.host, Arg, sizeof( New_Server.host )) >= sizeof( New_Server.host )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Host\" too long!", NGIRCd_ConfFile, Line );
795 return;
797 if( strcasecmp( Var, "Name" ) == 0 )
799 /* Name of the server ("Nick"/"ID") */
800 if( strlcpy( New_Server.name, Arg, sizeof( New_Server.name )) >= sizeof( New_Server.name )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Name\" too long!", NGIRCd_ConfFile, Line );
801 return;
803 if( strcasecmp( Var, "MyPassword" ) == 0 )
805 /* Password of this server which is sent to the peer */
806 if( strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in )) >= sizeof( New_Server.pwd_in )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"MyPassword\" too long!", NGIRCd_ConfFile, Line );
807 return;
809 if( strcasecmp( Var, "PeerPassword" ) == 0 )
811 /* Passwort of the peer which must be received */
812 if( strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out )) >= sizeof( New_Server.pwd_out )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"PeerPassword\" too long!", NGIRCd_ConfFile, Line );
813 return;
815 if( strcasecmp( Var, "Port" ) == 0 )
817 /* Port to which this server should connect */
818 port = atol( Arg );
819 if( port > 0 && port < 0xFFFF ) New_Server.port = (INT)port;
820 else Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
821 return;
823 if( strcasecmp( Var, "Group" ) == 0 )
825 /* Server group */
826 #ifdef HAVE_ISDIGIT
827 if( ! isdigit( (INT)*Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Group\" is not a number!", NGIRCd_ConfFile, Line );
828 else
829 #endif
830 New_Server.group = atoi( Arg );
831 return;
834 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
835 } /* Handle_SERVER */
838 LOCAL VOID
839 Handle_CHANNEL( INT Line, CHAR *Var, CHAR *Arg )
841 assert( Line > 0 );
842 assert( Var != NULL );
843 assert( Arg != NULL );
845 if( strcasecmp( Var, "Name" ) == 0 )
847 /* Name of the channel */
848 if( strlcpy( Conf_Channel[Conf_Channel_Count - 1].name, Arg, sizeof( Conf_Channel[Conf_Channel_Count - 1].name )) >= sizeof( Conf_Channel[Conf_Channel_Count - 1].name )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Name\" too long!", NGIRCd_ConfFile, Line );
849 return;
851 if( strcasecmp( Var, "Modes" ) == 0 )
853 /* Initial modes */
854 if( strlcpy( Conf_Channel[Conf_Channel_Count - 1].modes, Arg, sizeof( Conf_Channel[Conf_Channel_Count - 1].modes )) >= sizeof( Conf_Channel[Conf_Channel_Count - 1].modes )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Modes\" too long!", NGIRCd_ConfFile, Line );
855 return;
857 if( strcasecmp( Var, "Topic" ) == 0 )
859 /* Initial topic */
860 if( strlcpy( Conf_Channel[Conf_Channel_Count - 1].topic, Arg, sizeof( Conf_Channel[Conf_Channel_Count - 1].topic )) >= sizeof( Conf_Channel[Conf_Channel_Count - 1].topic )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Topic\" too long!", NGIRCd_ConfFile, Line );
861 return;
864 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
865 } /* Handle_CHANNEL */
868 LOCAL VOID
869 Validate_Config( BOOLEAN Configtest )
871 /* Validate configuration settings. */
873 #ifdef DEBUG
874 INT i, servers, servers_once;
875 #endif
877 if( ! Conf_ServerName[0] )
879 /* No server name configured! */
880 Config_Error( LOG_ALERT, "No server name configured in \"%s\" (section 'Global': 'Name')!", NGIRCd_ConfFile );
881 if( ! Configtest )
883 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
884 exit( 1 );
888 if( Conf_ServerName[0] && ! strchr( Conf_ServerName, '.' ))
890 /* No dot in server name! */
891 Config_Error( LOG_ALERT, "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!", NGIRCd_ConfFile );
892 if( ! Configtest )
894 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
895 exit( 1 );
899 #ifdef STRICT_RFC
900 if( ! Conf_ServerAdminMail[0] )
902 /* No administrative contact configured! */
903 Config_Error( LOG_ALERT, "No administrator email address configured in \"%s\" ('AdminEMail')!", NGIRCd_ConfFile );
904 if( ! Configtest )
906 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
907 exit( 1 );
910 #endif
912 if( ! Conf_ServerAdmin1[0] && ! Conf_ServerAdmin2[0] && ! Conf_ServerAdminMail[0] )
914 /* No administrative information configured! */
915 Config_Error( LOG_WARNING, "No administrative information configured but required by RFC!" );
917 #ifdef FD_SETSIZE
918 if(( Conf_MaxConnections > (LONG)FD_SETSIZE ) || ( Conf_MaxConnections < 1 ))
920 Conf_MaxConnections = (LONG)FD_SETSIZE;
921 Config_Error( LOG_ERR, "Setting MaxConnections to %ld, select() can't handle more file descriptors!", Conf_MaxConnections );
923 #else
924 Config_Error( LOG_WARN, "Don't know how many file descriptors select() can handle on this system, don't set MaxConnections too high!" );
925 #endif
927 #ifdef DEBUG
928 servers = servers_once = 0;
929 for( i = 0; i < MAX_SERVERS; i++ )
931 if( Conf_Server[i].name[0] )
933 servers++;
934 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) servers_once++;
937 Log( LOG_DEBUG, "Configuration: Operators=%d, Servers=%d[%d], Channels=%d", Conf_Oper_Count, servers, servers_once, Conf_Channel_Count );
938 #endif
939 } /* Validate_Config */
942 #ifdef PROTOTYPES
943 LOCAL VOID Config_Error( CONST INT Level, CONST CHAR *Format, ... )
944 #else
945 LOCAL VOID Config_Error( Level, Format, va_alist )
946 CONST INT Level;
947 CONST CHAR *Format;
948 va_dcl
949 #endif
951 /* Error! Write to console and/or logfile. */
953 CHAR msg[MAX_LOG_MSG_LEN];
954 va_list ap;
956 assert( Format != NULL );
958 #ifdef PROTOTYPES
959 va_start( ap, Format );
960 #else
961 va_start( ap );
962 #endif
963 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
964 va_end( ap );
966 /* During "normal operations" the log functions of the daemon should
967 * be used, but during testing of the configuration file, all messages
968 * should go directly to the console: */
969 if( Use_Log ) Log( Level, "%s", msg );
970 else puts( msg );
971 } /* Config_Error */
974 LOCAL VOID
975 Init_Server_Struct( CONF_SERVER *Server )
977 /* Initialize server configuration structur to default values */
979 assert( Server != NULL );
981 strcpy( Server->host, "" );
982 strcpy( Server->ip, "" );
983 strcpy( Server->name, "" );
984 strcpy( Server->pwd_in, "" );
985 strcpy( Server->pwd_out, "" );
986 Server->port = 0;
987 Server->group = NONE;
988 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
989 Server->res_stat = NULL;
990 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
991 else Server->flags = 0;
992 Server->conn_id = NONE;
993 } /* Init_Server_Struct */
996 /* -eof- */