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.76 2005/04/16 09:23:01 fw 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 bool Use_Log = true;
54 LOCAL CONF_SERVER New_Server;
55 LOCAL int New_Server_Idx;
58 LOCAL void Set_Defaults PARAMS(( bool InitServers ));
59 LOCAL void Read_Config PARAMS(( void ));
60 LOCAL void Validate_Config PARAMS(( bool 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(( const int LINE, const char *Value ));
70 LOCAL void Config_Error_TooLong PARAMS(( const 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 and stdout ("you can read our nice message and we can
109 * read in your keypress") are valid tty's, wait for a key: */
110 if( isatty( fileno( stdin )) && isatty( fileno( stdout )))
112 puts( "OK, press enter to see a dump of your service configuration ..." );
113 getchar( );
115 else puts( "Ok, dump of your server configuration follows:\n" );
117 puts( "[GLOBAL]" );
118 printf( " Name = %s\n", Conf_ServerName );
119 printf( " Info = %s\n", Conf_ServerInfo );
120 printf( " Password = %s\n", Conf_ServerPwd );
121 printf( " AdminInfo1 = %s\n", Conf_ServerAdmin1 );
122 printf( " AdminInfo2 = %s\n", Conf_ServerAdmin2 );
123 printf( " AdminEMail = %s\n", Conf_ServerAdminMail );
124 printf( " MotdFile = %s\n", Conf_MotdFile );
125 printf( " MotdPhrase = %s\n", Conf_MotdPhrase );
126 printf( " ChrootDir = %s\n", Conf_Chroot );
127 printf( " PidFile = %s\n", Conf_PidFile);
128 printf( " Ports = " );
129 for( i = 0; i < Conf_ListenPorts_Count; i++ )
131 if( i != 0 ) printf( ", " );
132 printf( "%u", (unsigned int) Conf_ListenPorts[i] );
134 puts( "" );
135 printf( " Listen = %s\n", Conf_ListenAddress );
136 pwd = getpwuid( Conf_UID );
137 if( pwd ) printf( " ServerUID = %s\n", pwd->pw_name );
138 else printf( " ServerUID = %ld\n", (long)Conf_UID );
139 grp = getgrgid( Conf_GID );
140 if( grp ) printf( " ServerGID = %s\n", grp->gr_name );
141 else printf( " ServerGID = %ld\n", (long)Conf_GID );
142 printf( " PingTimeout = %d\n", Conf_PingTimeout );
143 printf( " PongTimeout = %d\n", Conf_PongTimeout );
144 printf( " ConnectRetry = %d\n", Conf_ConnectRetry );
145 printf( " OperCanUseMode = %s\n", Conf_OperCanMode == true? "yes" : "no" );
146 printf( " OperServerMode = %s\n", Conf_OperServerMode == true? "yes" : "no" );
147 if( Conf_MaxConnections > 0 ) printf( " MaxConnections = %ld\n", Conf_MaxConnections );
148 else printf( " MaxConnections = -1\n" );
149 if( Conf_MaxConnectionsIP > 0 ) printf( " MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP );
150 else printf( " MaxConnectionsIP = -1\n" );
151 if( Conf_MaxJoins > 0 ) printf( " MaxJoins = %d\n", Conf_MaxJoins );
152 else printf( " MaxJoins = -1\n" );
153 puts( "" );
155 for( i = 0; i < Conf_Oper_Count; i++ )
157 if( ! Conf_Oper[i].name[0] ) continue;
159 /* Valid "Operator" section */
160 puts( "[OPERATOR]" );
161 printf( " Name = %s\n", Conf_Oper[i].name );
162 printf( " Password = %s\n", Conf_Oper[i].pwd );
163 if ( Conf_Oper[i].mask ) printf( " Mask = %s\n", Conf_Oper[i].mask );
164 puts( "" );
167 for( i = 0; i < MAX_SERVERS; i++ )
169 if( ! Conf_Server[i].name[0] ) continue;
171 /* Valid "Server" section */
172 puts( "[SERVER]" );
173 printf( " Name = %s\n", Conf_Server[i].name );
174 printf( " Host = %s\n", Conf_Server[i].host );
175 printf( " Port = %d\n", Conf_Server[i].port );
176 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
177 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
178 printf( " Group = %d\n", Conf_Server[i].group );
179 puts( "" );
182 for( i = 0; i < Conf_Channel_Count; i++ )
184 if( ! Conf_Channel[i].name[0] ) continue;
186 /* Valid "Channel" section */
187 puts( "[CHANNEL]" );
188 printf( " Name = %s\n", Conf_Channel[i].name );
189 printf( " Modes = %s\n", Conf_Channel[i].modes );
190 printf( " Topic = %s\n", Conf_Channel[i].topic );
191 puts( "" );
194 return 0;
195 } /* Conf_Test */
198 GLOBAL void
199 Conf_UnsetServer( CONN_ID Idx )
201 /* Set next time for next connection attempt, if this is a server
202 * link that is (still) configured here. If the server is set as
203 * "once", delete it from our configuration.
204 * Non-Server-Connections will be silently ignored. */
206 int i;
208 /* Check all our configured servers */
209 for( i = 0; i < MAX_SERVERS; i++ )
211 if( Conf_Server[i].conn_id != Idx ) continue;
213 /* Gotcha! Mark server configuration as "unused": */
214 Conf_Server[i].conn_id = NONE;
216 if( Conf_Server[i].flags & CONF_SFLAG_ONCE )
218 /* Delete configuration here */
219 Init_Server_Struct( &Conf_Server[i] );
221 else
223 /* Set time for next connect attempt */
224 if( Conf_Server[i].lasttry < time( NULL ) - Conf_ConnectRetry )
226 /* Okay, the connection was established "long enough": */
227 Conf_Server[i].lasttry = time( NULL ) - Conf_ConnectRetry + RECONNECT_DELAY;
231 } /* Conf_UnsetServer */
234 GLOBAL void
235 Conf_SetServer( int ConfServer, CONN_ID Idx )
237 /* Set connection for specified configured server */
239 assert( ConfServer > NONE );
240 assert( Idx > NONE );
242 Conf_Server[ConfServer].conn_id = Idx;
243 } /* Conf_SetServer */
246 GLOBAL int
247 Conf_GetServer( CONN_ID Idx )
249 /* Get index of server in configuration structure */
251 int i = 0;
253 assert( Idx > NONE );
255 for( i = 0; i < MAX_SERVERS; i++ )
257 if( Conf_Server[i].conn_id == Idx ) return i;
259 return NONE;
260 } /* Conf_GetServer */
263 GLOBAL bool
264 Conf_EnableServer( char *Name, UINT16 Port )
266 /* Enable specified server and adjust port */
268 int i;
270 assert( Name != NULL );
272 for( i = 0; i < MAX_SERVERS; i++ )
274 if( strcasecmp( Conf_Server[i].name, Name ) == 0 )
276 /* Gotcha! Set port and enable server: */
277 Conf_Server[i].port = Port;
278 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
279 return true;
282 return false;
283 } /* Conf_EnableServer */
286 GLOBAL bool
287 Conf_DisableServer( char *Name )
289 /* Enable specified server and adjust port */
291 int i;
293 assert( Name != NULL );
295 for( i = 0; i < MAX_SERVERS; i++ )
297 if( strcasecmp( Conf_Server[i].name, Name ) == 0 )
299 /* Gotcha! Disable and disconnect server: */
300 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
301 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
302 return true;
305 return false;
306 } /* Conf_DisableServer */
309 GLOBAL bool
310 Conf_AddServer( char *Name, UINT16 Port, char *Host, char *MyPwd, char *PeerPwd )
312 /* Add new server to configuration */
314 int i;
316 assert( Name != NULL );
317 assert( Host != NULL );
318 assert( MyPwd != NULL );
319 assert( PeerPwd != NULL );
321 /* Search unused item in server configuration structure */
322 for( i = 0; i < MAX_SERVERS; i++ )
324 /* Is this item used? */
325 if( ! Conf_Server[i].name[0] ) break;
327 if( i >= MAX_SERVERS ) return false;
329 Init_Server_Struct( &Conf_Server[i] );
330 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
331 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
332 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
333 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
334 Conf_Server[i].port = Port;
335 Conf_Server[i].flags = CONF_SFLAG_ONCE;
337 return true;
338 } /* Conf_AddServer */
341 LOCAL void
342 Set_Defaults( bool InitServers )
344 /* Initialize configuration variables with default values. */
346 int i;
348 strcpy( Conf_ServerName, "" );
349 sprintf( Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
350 strcpy( Conf_ServerPwd, "" );
352 strcpy( Conf_ServerAdmin1, "" );
353 strcpy( Conf_ServerAdmin2, "" );
354 strcpy( Conf_ServerAdminMail, "" );
356 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
357 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
359 strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
361 strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
363 strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
365 Conf_ListenPorts_Count = 0;
366 strcpy( Conf_ListenAddress, "" );
368 Conf_UID = Conf_GID = 0;
370 Conf_PingTimeout = 120;
371 Conf_PongTimeout = 20;
373 Conf_ConnectRetry = 60;
375 Conf_Oper_Count = 0;
376 Conf_Channel_Count = 0;
378 Conf_OperCanMode = false;
379 Conf_OperServerMode = false;
381 Conf_MaxConnections = -1;
382 Conf_MaxConnectionsIP = 5;
383 Conf_MaxJoins = 10;
385 /* Initialize server configuration structures */
386 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
387 } /* Set_Defaults */
390 LOCAL void
391 Read_Config( void )
393 /* Read configuration file. */
395 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
396 int line, i, n;
397 FILE *fd;
399 /* Open configuration file */
400 fd = fopen( NGIRCd_ConfFile, "r" );
401 if( ! fd )
403 /* No configuration file found! */
404 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s", NGIRCd_ConfFile, strerror( errno ));
405 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
406 exit( 1 );
409 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
411 /* Clean up server configuration structure: mark all already
412 * configured servers as "once" so that they are deleted
413 * after the next disconnect and delete all unused servers.
414 * And delete all servers which are "duplicates" of servers
415 * that are already marked as "once" (such servers have been
416 * created by the last rehash but are now useless). */
417 for( i = 0; i < MAX_SERVERS; i++ )
419 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
420 else
422 /* This structure is in use ... */
423 if( Conf_Server[i].flags & CONF_SFLAG_ONCE )
425 /* Check for duplicates */
426 for( n = 0; n < MAX_SERVERS; n++ )
428 if( n == i ) continue;
430 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id )
432 Init_Server_Struct( &Conf_Server[n] );
433 Log( LOG_DEBUG, "Deleted unused duplicate server %d (kept %d).", n, i );
437 else
439 /* Mark server as "once" */
440 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
441 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
446 /* Initialize variables */
447 line = 0;
448 strcpy( section, "" );
449 Init_Server_Struct( &New_Server );
450 New_Server_Idx = NONE;
452 /* Read configuration file */
453 while( true )
455 if( ! fgets( str, LINE_LEN, fd )) break;
456 ngt_TrimStr( str );
457 line++;
459 /* Skip comments and empty lines */
460 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
462 /* Is this the beginning of a new section? */
463 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
465 strlcpy( section, str, sizeof( section ));
466 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
467 if( strcasecmp( section, "[OPERATOR]" ) == 0 )
469 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Config_Error( LOG_ERR, "Too many operators configured." );
470 else
472 /* Initialize new operator structure */
473 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
474 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
475 if (Conf_Oper[Conf_Oper_Count].mask) {
476 free(Conf_Oper[Conf_Oper_Count].mask );
477 Conf_Oper[Conf_Oper_Count].mask = NULL;
479 Conf_Oper_Count++;
481 continue;
483 if( strcasecmp( section, "[SERVER]" ) == 0 )
485 /* Check if there is already a server to add */
486 if( New_Server.name[0] )
488 /* Copy data to "real" server structure */
489 assert( New_Server_Idx > NONE );
490 Conf_Server[New_Server_Idx] = New_Server;
493 /* Re-init structure for new server */
494 Init_Server_Struct( &New_Server );
496 /* Search unused item in server configuration structure */
497 for( i = 0; i < MAX_SERVERS; i++ )
499 /* Is this item used? */
500 if( ! Conf_Server[i].name[0] ) break;
502 if( i >= MAX_SERVERS )
504 /* Oops, no free item found! */
505 Config_Error( LOG_ERR, "Too many servers configured." );
506 New_Server_Idx = NONE;
508 else New_Server_Idx = i;
509 continue;
511 if( strcasecmp( section, "[CHANNEL]" ) == 0 )
513 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
514 else
516 /* Initialize new channel structure */
517 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
518 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
519 strcpy( Conf_Channel[Conf_Channel_Count].topic, "" );
520 Conf_Channel_Count++;
522 continue;
524 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
525 section[0] = 0x1;
527 if( section[0] == 0x1 ) continue;
529 /* Split line into variable name and parameters */
530 ptr = strchr( str, '=' );
531 if( ! ptr )
533 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
534 continue;
536 *ptr = '\0';
537 var = str; ngt_TrimStr( var );
538 arg = ptr + 1; ngt_TrimStr( arg );
540 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
541 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
542 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
543 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
544 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
547 /* Close configuration file */
548 fclose( fd );
550 /* Check if there is still a server to add */
551 if( New_Server.name[0] )
553 /* Copy data to "real" server structure */
554 assert( New_Server_Idx > NONE );
555 Conf_Server[New_Server_Idx] = New_Server;
558 /* If there are no ports configured use the default: 6667 */
559 if( Conf_ListenPorts_Count < 1 )
561 Conf_ListenPorts_Count = 1;
562 Conf_ListenPorts[0] = 6667;
564 } /* Read_Config */
567 LOCAL bool
568 Check_ArgIsTrue( const char *Arg )
570 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
571 if( strcasecmp( Arg, "true" ) == 0 ) return true;
572 if( atoi( Arg ) != 0 ) return true;
574 return false;
575 } /* Check_ArgIsTrue */
578 LOCAL void
579 Handle_GLOBAL( int Line, char *Var, char *Arg )
581 struct passwd *pwd;
582 struct group *grp;
583 char *ptr;
584 long port;
586 assert( Line > 0 );
587 assert( Var != NULL );
588 assert( Arg != NULL );
590 if( strcasecmp( Var, "Name" ) == 0 )
592 /* Server name */
593 if( strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName )) >= sizeof( Conf_ServerName ))
594 Config_Error_TooLong( Line, Var );
596 return;
598 if( strcasecmp( Var, "Info" ) == 0 )
600 /* Info text of server */
601 if( strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo )) >= sizeof( Conf_ServerInfo ))
602 Config_Error_TooLong ( Line, Var );
604 return;
606 if( strcasecmp( Var, "Password" ) == 0 )
608 /* Global server password */
609 if( strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd )) >= sizeof( Conf_ServerPwd ))
610 Config_Error_TooLong( Line, Var );
612 return;
614 if( strcasecmp( Var, "AdminInfo1" ) == 0 )
616 /* Administrative info #1 */
617 if( strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 )) >= sizeof( Conf_ServerAdmin1 )) Config_Error_TooLong ( Line, Var );
618 return;
620 if( strcasecmp( Var, "AdminInfo2" ) == 0 )
622 /* Administrative info #2 */
623 if( strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 )) >= sizeof( Conf_ServerAdmin2 )) Config_Error_TooLong ( Line, Var );
624 return;
626 if( strcasecmp( Var, "AdminEMail" ) == 0 )
628 /* Administrative email contact */
629 if( strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail )) >= sizeof( Conf_ServerAdminMail )) Config_Error_TooLong( Line, Var );
630 return;
632 if( strcasecmp( Var, "Ports" ) == 0 )
634 /* Ports on that the server should listen. More port numbers
635 * must be separated by "," */
636 ptr = strtok( Arg, "," );
637 while( ptr )
639 ngt_TrimStr( ptr );
640 port = atol( ptr );
641 if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Config_Error( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
642 else
644 if( port > 0 && port < 0xFFFF ) Conf_ListenPorts[Conf_ListenPorts_Count++] = (UINT16)port;
645 else Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
647 ptr = strtok( NULL, "," );
649 return;
651 if( strcasecmp( Var, "MotdFile" ) == 0 )
653 /* "Message of the day" (MOTD) file */
654 if( strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile )) >= sizeof( Conf_MotdFile ))
655 Config_Error_TooLong( Line, Var );
657 return;
659 if( strcasecmp( Var, "MotdPhrase" ) == 0 )
661 /* "Message of the day" phrase (instead of file) */
662 if( strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase )) >= sizeof( Conf_MotdPhrase ))
663 Config_Error_TooLong( Line, Var );
665 return;
667 if( strcasecmp( Var, "ChrootDir" ) == 0 )
669 /* directory for chroot() */
670 if( strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot )) >= sizeof( Conf_Chroot ))
671 Config_Error_TooLong( Line, Var );
673 return;
676 if ( strcasecmp( Var, "PidFile" ) == 0 )
678 /* name of pidfile */
679 if( strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile )) >= sizeof( Conf_PidFile ))
680 Config_Error_TooLong( Line, Var );
682 return;
685 if( strcasecmp( Var, "ServerUID" ) == 0 )
687 /* UID the daemon should switch to */
688 pwd = getpwnam( Arg );
689 if( pwd ) Conf_UID = pwd->pw_uid;
690 else
692 #ifdef HAVE_ISDIGIT
693 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
694 else
695 #endif
696 Conf_UID = (unsigned int)atoi( Arg );
698 return;
700 if( strcasecmp( Var, "ServerGID" ) == 0 )
702 /* GID the daemon should use */
703 grp = getgrnam( Arg );
704 if( grp ) Conf_GID = grp->gr_gid;
705 else
707 #ifdef HAVE_ISDIGIT
708 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
709 else
710 #endif
711 Conf_GID = (unsigned int)atoi( Arg );
713 return;
715 if( strcasecmp( Var, "PingTimeout" ) == 0 )
717 /* PING timeout */
718 Conf_PingTimeout = atoi( Arg );
719 if( Conf_PingTimeout < 5 )
721 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!", NGIRCd_ConfFile, Line );
722 Conf_PingTimeout = 5;
724 return;
726 if( strcasecmp( Var, "PongTimeout" ) == 0 )
728 /* PONG timeout */
729 Conf_PongTimeout = atoi( Arg );
730 if( Conf_PongTimeout < 5 )
732 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!", NGIRCd_ConfFile, Line );
733 Conf_PongTimeout = 5;
735 return;
737 if( strcasecmp( Var, "ConnectRetry" ) == 0 )
739 /* Seconds between connection attempts to other servers */
740 Conf_ConnectRetry = atoi( Arg );
741 if( Conf_ConnectRetry < 5 )
743 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!", NGIRCd_ConfFile, Line );
744 Conf_ConnectRetry = 5;
746 return;
748 if( strcasecmp( Var, "OperCanUseMode" ) == 0 )
750 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
751 Conf_OperCanMode = Check_ArgIsTrue( Arg );
752 return;
754 if( strcasecmp( Var, "OperServerMode" ) == 0 )
756 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
757 Conf_OperServerMode = Check_ArgIsTrue( Arg );
758 return;
760 if( strcasecmp( Var, "MaxConnections" ) == 0 )
762 /* Maximum number of connections. Values <= 0 are equal to "no limit". */
763 #ifdef HAVE_ISDIGIT
764 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
765 else
766 #endif
767 Conf_MaxConnections = atol( Arg );
768 return;
770 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 )
772 /* Maximum number of simoultanous connections from one IP. Values <= 0 are equal to "no limit". */
773 #ifdef HAVE_ISDIGIT
774 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
775 else
776 #endif
777 Conf_MaxConnectionsIP = atoi( Arg );
778 return;
780 if( strcasecmp( Var, "MaxJoins" ) == 0 )
782 /* Maximum number of channels a user can join. Values <= 0 are equal to "no limit". */
783 #ifdef HAVE_ISDIGIT
784 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
785 else
786 #endif
787 Conf_MaxJoins = atoi( Arg );
788 return;
790 if( strcasecmp( Var, "Listen" ) == 0 )
792 /* IP-Address to bind sockets */
793 if( strlcpy( Conf_ListenAddress, Arg, sizeof( Conf_ListenAddress )) >= sizeof( Conf_ListenAddress ))
795 Config_Error_TooLong( Line, Var );
797 return;
800 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
801 } /* Handle_GLOBAL */
804 LOCAL void
805 Handle_OPERATOR( int Line, char *Var, char *Arg )
807 assert( Line > 0 );
808 assert( Var != NULL );
809 assert( Arg != NULL );
810 assert( Conf_Oper_Count > 0 );
812 if( strcasecmp( Var, "Name" ) == 0 )
814 /* Name of IRC operator */
815 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 );
816 return;
818 if( strcasecmp( Var, "Password" ) == 0 )
820 /* Password of IRC operator */
821 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 );
822 return;
824 if( strcasecmp( Var, "Mask" ) == 0 )
826 if (Conf_Oper[Conf_Oper_Count - 1].mask) return; /* Hostname already configured */
827 Conf_Oper[Conf_Oper_Count - 1].mask = strdup( Arg );
828 if (! Conf_Oper[Conf_Oper_Count - 1].mask) {
829 Config_Error( LOG_ERR, "%s, line %d: Cannot allocate memory for operator mask: %s", NGIRCd_ConfFile, Line, strerror(errno) );
830 return;
833 return;
835 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
836 } /* Handle_OPERATOR */
839 LOCAL void
840 Handle_SERVER( int Line, char *Var, char *Arg )
842 long port;
844 assert( Line > 0 );
845 assert( Var != NULL );
846 assert( Arg != NULL );
848 /* Ignore server block if no space is left in server configuration structure */
849 if( New_Server_Idx <= NONE ) return;
851 if( strcasecmp( Var, "Host" ) == 0 )
853 /* Hostname of the server */
854 if( strlcpy( New_Server.host, Arg, sizeof( New_Server.host )) >= sizeof( New_Server.host ))
855 Config_Error_TooLong ( Line, Var );
857 return;
859 if( strcasecmp( Var, "Name" ) == 0 )
861 /* Name of the server ("Nick"/"ID") */
862 if( strlcpy( New_Server.name, Arg, sizeof( New_Server.name )) >= sizeof( New_Server.name ))
863 Config_Error_TooLong( Line, Var );
864 return;
866 if( strcasecmp( Var, "MyPassword" ) == 0 )
868 /* Password of this server which is sent to the peer */
869 if( strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in )) >= sizeof( New_Server.pwd_in )) Config_Error_TooLong( Line, Var );
870 return;
872 if( strcasecmp( Var, "PeerPassword" ) == 0 )
874 /* Passwort of the peer which must be received */
875 if( strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out )) >= sizeof( New_Server.pwd_out )) Config_Error_TooLong( Line, Var );
876 return;
878 if( strcasecmp( Var, "Port" ) == 0 )
880 /* Port to which this server should connect */
881 port = atol( Arg );
882 if( port > 0 && port < 0xFFFF ) New_Server.port = (UINT16)port;
883 else Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
884 return;
886 if( strcasecmp( Var, "Group" ) == 0 )
888 /* Server group */
889 #ifdef HAVE_ISDIGIT
890 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
891 else
892 #endif
893 New_Server.group = atoi( Arg );
894 return;
897 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
898 } /* Handle_SERVER */
901 LOCAL void
902 Handle_CHANNEL( int Line, char *Var, char *Arg )
904 assert( Line > 0 );
905 assert( Var != NULL );
906 assert( Arg != NULL );
908 if( strcasecmp( Var, "Name" ) == 0 )
910 /* Name of the channel */
911 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 ))
912 Config_Error_TooLong( Line, Var );
913 return;
915 if( strcasecmp( Var, "Modes" ) == 0 )
917 /* Initial modes */
918 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 ))
919 Config_Error_TooLong( Line, Var );
920 return;
922 if( strcasecmp( Var, "Topic" ) == 0 )
924 /* Initial topic */
925 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 ))
926 Config_Error_TooLong( Line, Var );
928 return;
931 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
932 } /* Handle_CHANNEL */
935 LOCAL void
936 Validate_Config( bool Configtest )
938 /* Validate configuration settings. */
940 #ifdef DEBUG
941 int i, servers, servers_once;
942 #endif
944 if( ! Conf_ServerName[0] )
946 /* No server name configured! */
947 Config_Error( LOG_ALERT, "No server name configured in \"%s\" (section 'Global': 'Name')!", NGIRCd_ConfFile );
948 if( ! Configtest )
950 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
951 exit( 1 );
955 if( Conf_ServerName[0] && ! strchr( Conf_ServerName, '.' ))
957 /* No dot in server name! */
958 Config_Error( LOG_ALERT, "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!", NGIRCd_ConfFile );
959 if( ! Configtest )
961 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
962 exit( 1 );
966 #ifdef STRICT_RFC
967 if( ! Conf_ServerAdminMail[0] )
969 /* No administrative contact configured! */
970 Config_Error( LOG_ALERT, "No administrator email address configured in \"%s\" ('AdminEMail')!", NGIRCd_ConfFile );
971 if( ! Configtest )
973 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
974 exit( 1 );
977 #endif
979 if( ! Conf_ServerAdmin1[0] && ! Conf_ServerAdmin2[0] && ! Conf_ServerAdminMail[0] )
981 /* No administrative information configured! */
982 Config_Error( LOG_WARNING, "No administrative information configured but required by RFC!" );
984 #ifdef FD_SETSIZE
985 if(( Conf_MaxConnections > (long)FD_SETSIZE ) || ( Conf_MaxConnections < 1 ))
987 Conf_MaxConnections = (long)FD_SETSIZE;
988 Config_Error( LOG_ERR, "Setting MaxConnections to %ld, select() can't handle more file descriptors!", Conf_MaxConnections );
990 #else
991 Config_Error( LOG_WARN, "Don't know how many file descriptors select() can handle on this system, don't set MaxConnections too high!" );
992 #endif
994 #ifdef DEBUG
995 servers = servers_once = 0;
996 for( i = 0; i < MAX_SERVERS; i++ )
998 if( Conf_Server[i].name[0] )
1000 servers++;
1001 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) servers_once++;
1004 Log( LOG_DEBUG, "Configuration: Operators=%d, Servers=%d[%d], Channels=%d", Conf_Oper_Count, servers, servers_once, Conf_Channel_Count );
1005 #endif
1006 } /* Validate_Config */
1009 LOCAL void
1010 Config_Error_TooLong ( const int Line, const char *Item )
1012 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1015 LOCAL void
1016 Config_Error_NaN( const int Line, const char *Item )
1018 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!", NGIRCd_ConfFile, Line, Item );
1021 #ifdef PROTOTYPES
1022 LOCAL void Config_Error( const int Level, const char *Format, ... )
1023 #else
1024 LOCAL void Config_Error( Level, Format, va_alist )
1025 const int Level;
1026 const char *Format;
1027 va_dcl
1028 #endif
1030 /* Error! Write to console and/or logfile. */
1032 char msg[MAX_LOG_MSG_LEN];
1033 va_list ap;
1035 assert( Format != NULL );
1037 #ifdef PROTOTYPES
1038 va_start( ap, Format );
1039 #else
1040 va_start( ap );
1041 #endif
1042 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1043 va_end( ap );
1045 /* During "normal operations" the log functions of the daemon should
1046 * be used, but during testing of the configuration file, all messages
1047 * should go directly to the console: */
1048 if( Use_Log ) Log( Level, "%s", msg );
1049 else puts( msg );
1050 } /* Config_Error */
1053 LOCAL void
1054 Init_Server_Struct( CONF_SERVER *Server )
1056 /* Initialize server configuration structur to default values */
1058 assert( Server != NULL );
1060 memset( Server, 0, sizeof (CONF_SERVER) );
1062 Server->group = NONE;
1063 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1065 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1067 Server->conn_id = NONE;
1068 } /* Init_Server_Struct */
1071 /* -eof- */