2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 Alexander Barton (alex@barton.de)
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.
11 * Configuration management (reading, parsing & validation)
17 static char UNUSED id[] = "$Id: conf.c,v 1.81 2005/07/28 16:23:55 fw Exp $";
34 #include <sys/types.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 ));
80 Validate_Config( false );
87 Set_Defaults( false );
89 Validate_Config( false );
96 /* Read configuration, validate and output it. */
104 Set_Defaults( true );
107 Validate_Config( true );
109 /* If stdin and stdout ("you can read our nice message and we can
110 * read in your keypress") are valid tty's, wait for a key: */
111 if( isatty( fileno( stdin )) && isatty( fileno( stdout )))
113 puts( "OK, press enter to see a dump of your service configuration ..." );
116 else puts( "Ok, dump of your server configuration follows:\n" );
119 printf( " Name = %s\n", Conf_ServerName );
120 printf( " Info = %s\n", Conf_ServerInfo );
121 printf( " Password = %s\n", Conf_ServerPwd );
122 printf( " AdminInfo1 = %s\n", Conf_ServerAdmin1 );
123 printf( " AdminInfo2 = %s\n", Conf_ServerAdmin2 );
124 printf( " AdminEMail = %s\n", Conf_ServerAdminMail );
125 printf( " MotdFile = %s\n", Conf_MotdFile );
126 printf( " MotdPhrase = %s\n", Conf_MotdPhrase );
127 printf( " ChrootDir = %s\n", Conf_Chroot );
128 printf( " PidFile = %s\n", Conf_PidFile);
129 printf( " Ports = " );
130 for( i = 0; i < Conf_ListenPorts_Count; i++ )
132 if( i != 0 ) printf( ", " );
133 printf( "%u", (unsigned int) Conf_ListenPorts[i] );
136 printf( " Listen = %s\n", Conf_ListenAddress );
137 pwd = getpwuid( Conf_UID );
138 if( pwd ) printf( " ServerUID = %s\n", pwd->pw_name );
139 else printf( " ServerUID = %ld\n", (long)Conf_UID );
140 grp = getgrgid( Conf_GID );
141 if( grp ) printf( " ServerGID = %s\n", grp->gr_name );
142 else printf( " ServerGID = %ld\n", (long)Conf_GID );
143 printf( " PingTimeout = %d\n", Conf_PingTimeout );
144 printf( " PongTimeout = %d\n", Conf_PongTimeout );
145 printf( " ConnectRetry = %d\n", Conf_ConnectRetry );
146 printf( " OperCanUseMode = %s\n", Conf_OperCanMode == true? "yes" : "no" );
147 printf( " OperServerMode = %s\n", Conf_OperServerMode == true? "yes" : "no" );
148 if( Conf_MaxConnections > 0 ) printf( " MaxConnections = %ld\n", Conf_MaxConnections );
149 else printf( " MaxConnections = -1\n" );
150 if( Conf_MaxConnectionsIP > 0 ) printf( " MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP );
151 else printf( " MaxConnectionsIP = -1\n" );
152 if( Conf_MaxJoins > 0 ) printf( " MaxJoins = %d\n", Conf_MaxJoins );
153 else printf( " MaxJoins = -1\n" );
156 for( i = 0; i < Conf_Oper_Count; i++ )
158 if( ! Conf_Oper[i].name[0] ) continue;
160 /* Valid "Operator" section */
161 puts( "[OPERATOR]" );
162 printf( " Name = %s\n", Conf_Oper[i].name );
163 printf( " Password = %s\n", Conf_Oper[i].pwd );
164 if ( Conf_Oper[i].mask ) printf( " Mask = %s\n", Conf_Oper[i].mask );
168 for( i = 0; i < MAX_SERVERS; i++ )
170 if( ! Conf_Server[i].name[0] ) continue;
172 /* Valid "Server" section */
174 printf( " Name = %s\n", Conf_Server[i].name );
175 printf( " Host = %s\n", Conf_Server[i].host );
176 printf( " Port = %d\n", Conf_Server[i].port );
177 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
178 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
179 printf( " Group = %d\n", Conf_Server[i].group );
183 for( i = 0; i < Conf_Channel_Count; i++ )
185 if( ! Conf_Channel[i].name[0] ) continue;
187 /* Valid "Channel" section */
189 printf( " Name = %s\n", Conf_Channel[i].name );
190 printf( " Modes = %s\n", Conf_Channel[i].modes );
191 topic = (char*)array_start(&Conf_Channel[i].topic);
192 printf( " Topic = %s\n", topic ? topic : "");
201 Conf_UnsetServer( CONN_ID Idx )
203 /* Set next time for next connection attempt, if this is a server
204 * link that is (still) configured here. If the server is set as
205 * "once", delete it from our configuration.
206 * Non-Server-Connections will be silently ignored. */
210 /* Check all our configured servers */
211 for( i = 0; i < MAX_SERVERS; i++ )
213 if( Conf_Server[i].conn_id != Idx ) continue;
215 /* Gotcha! Mark server configuration as "unused": */
216 Conf_Server[i].conn_id = NONE;
218 if( Conf_Server[i].flags & CONF_SFLAG_ONCE )
220 /* Delete configuration here */
221 Init_Server_Struct( &Conf_Server[i] );
225 /* Set time for next connect attempt */
226 if( Conf_Server[i].lasttry < time( NULL ) - Conf_ConnectRetry )
228 /* Okay, the connection was established "long enough": */
229 Conf_Server[i].lasttry = time( NULL ) - Conf_ConnectRetry + RECONNECT_DELAY;
233 } /* Conf_UnsetServer */
237 Conf_SetServer( int ConfServer, CONN_ID Idx )
239 /* Set connection for specified configured server */
241 assert( ConfServer > NONE );
242 assert( Idx > NONE );
244 Conf_Server[ConfServer].conn_id = Idx;
245 } /* Conf_SetServer */
249 Conf_GetServer( CONN_ID Idx )
251 /* Get index of server in configuration structure */
255 assert( Idx > NONE );
257 for( i = 0; i < MAX_SERVERS; i++ )
259 if( Conf_Server[i].conn_id == Idx ) return i;
262 } /* Conf_GetServer */
266 Conf_EnableServer( char *Name, UINT16 Port )
268 /* Enable specified server and adjust port */
272 assert( Name != NULL );
274 for( i = 0; i < MAX_SERVERS; i++ )
276 if( strcasecmp( Conf_Server[i].name, Name ) == 0 )
278 /* Gotcha! Set port and enable server: */
279 Conf_Server[i].port = Port;
280 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
285 } /* Conf_EnableServer */
289 Conf_DisableServer( char *Name )
291 /* Enable specified server and adjust port */
295 assert( Name != NULL );
297 for( i = 0; i < MAX_SERVERS; i++ )
299 if( strcasecmp( Conf_Server[i].name, Name ) == 0 )
301 /* Gotcha! Disable and disconnect server: */
302 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
303 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
308 } /* Conf_DisableServer */
312 Conf_AddServer( char *Name, UINT16 Port, char *Host, char *MyPwd, char *PeerPwd )
314 /* Add new server to configuration */
318 assert( Name != NULL );
319 assert( Host != NULL );
320 assert( MyPwd != NULL );
321 assert( PeerPwd != NULL );
323 /* Search unused item in server configuration structure */
324 for( i = 0; i < MAX_SERVERS; i++ )
326 /* Is this item used? */
327 if( ! Conf_Server[i].name[0] ) break;
329 if( i >= MAX_SERVERS ) return false;
331 Init_Server_Struct( &Conf_Server[i] );
332 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
333 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
334 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
335 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
336 Conf_Server[i].port = Port;
337 Conf_Server[i].flags = CONF_SFLAG_ONCE;
340 } /* Conf_AddServer */
344 Set_Defaults( bool InitServers )
346 /* Initialize configuration variables with default values. */
350 strcpy( Conf_ServerName, "" );
351 snprintf( Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
352 strcpy( Conf_ServerPwd, "" );
354 strcpy( Conf_ServerAdmin1, "" );
355 strcpy( Conf_ServerAdmin2, "" );
356 strcpy( Conf_ServerAdminMail, "" );
358 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
359 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
361 strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
363 strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
365 strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
367 Conf_ListenPorts_Count = 0;
368 strcpy( Conf_ListenAddress, "" );
370 Conf_UID = Conf_GID = 0;
372 Conf_PingTimeout = 120;
373 Conf_PongTimeout = 20;
375 Conf_ConnectRetry = 60;
378 Conf_Channel_Count = 0;
380 Conf_OperCanMode = false;
381 Conf_OperServerMode = false;
383 Conf_MaxConnections = -1;
384 Conf_MaxConnectionsIP = 5;
387 /* Initialize server configuration structures */
388 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
395 /* Read configuration file. */
397 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
401 /* Open configuration file */
402 fd = fopen( NGIRCd_ConfFile, "r" );
405 /* No configuration file found! */
406 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s", NGIRCd_ConfFile, strerror( errno ));
407 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
411 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
413 /* Clean up server configuration structure: mark all already
414 * configured servers as "once" so that they are deleted
415 * after the next disconnect and delete all unused servers.
416 * And delete all servers which are "duplicates" of servers
417 * that are already marked as "once" (such servers have been
418 * created by the last rehash but are now useless). */
419 for( i = 0; i < MAX_SERVERS; i++ )
421 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
424 /* This structure is in use ... */
425 if( Conf_Server[i].flags & CONF_SFLAG_ONCE )
427 /* Check for duplicates */
428 for( n = 0; n < MAX_SERVERS; n++ )
430 if( n == i ) continue;
432 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id )
434 Init_Server_Struct( &Conf_Server[n] );
435 Log( LOG_DEBUG, "Deleted unused duplicate server %d (kept %d).", n, i );
441 /* Mark server as "once" */
442 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
443 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
448 /* Initialize variables */
450 strcpy( section, "" );
451 Init_Server_Struct( &New_Server );
452 New_Server_Idx = NONE;
454 /* Read configuration file */
457 if( ! fgets( str, LINE_LEN, fd )) break;
461 /* Skip comments and empty lines */
462 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
464 /* Is this the beginning of a new section? */
465 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
467 strlcpy( section, str, sizeof( section ));
468 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
469 if( strcasecmp( section, "[OPERATOR]" ) == 0 )
471 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Config_Error( LOG_ERR, "Too many operators configured." );
474 /* Initialize new operator structure */
475 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
476 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
477 if (Conf_Oper[Conf_Oper_Count].mask) {
478 free(Conf_Oper[Conf_Oper_Count].mask );
479 Conf_Oper[Conf_Oper_Count].mask = NULL;
485 if( strcasecmp( section, "[SERVER]" ) == 0 )
487 /* Check if there is already a server to add */
488 if( New_Server.name[0] )
490 /* Copy data to "real" server structure */
491 assert( New_Server_Idx > NONE );
492 Conf_Server[New_Server_Idx] = New_Server;
495 /* Re-init structure for new server */
496 Init_Server_Struct( &New_Server );
498 /* Search unused item in server configuration structure */
499 for( i = 0; i < MAX_SERVERS; i++ )
501 /* Is this item used? */
502 if( ! Conf_Server[i].name[0] ) break;
504 if( i >= MAX_SERVERS )
506 /* Oops, no free item found! */
507 Config_Error( LOG_ERR, "Too many servers configured." );
508 New_Server_Idx = NONE;
510 else New_Server_Idx = i;
513 if( strcasecmp( section, "[CHANNEL]" ) == 0 ) {
514 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) {
515 Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
517 /* Initialize new channel structure */
518 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
519 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
520 array_free(&Conf_Channel[Conf_Channel_Count].topic);
521 Conf_Channel_Count++;
525 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
528 if( section[0] == 0x1 ) continue;
530 /* Split line into variable name and parameters */
531 ptr = strchr( str, '=' );
534 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
538 var = str; ngt_TrimStr( var );
539 arg = ptr + 1; ngt_TrimStr( arg );
541 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
542 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
543 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
544 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
545 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
548 /* Close configuration file */
551 /* Check if there is still a server to add */
552 if( New_Server.name[0] )
554 /* Copy data to "real" server structure */
555 assert( New_Server_Idx > NONE );
556 Conf_Server[New_Server_Idx] = New_Server;
559 /* If there are no ports configured use the default: 6667 */
560 if( Conf_ListenPorts_Count < 1 )
562 Conf_ListenPorts_Count = 1;
563 Conf_ListenPorts[0] = 6667;
569 Check_ArgIsTrue( const char *Arg )
571 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
572 if( strcasecmp( Arg, "true" ) == 0 ) return true;
573 if( atoi( Arg ) != 0 ) return true;
576 } /* Check_ArgIsTrue */
580 Handle_GLOBAL( int Line, char *Var, char *Arg )
588 assert( Var != NULL );
589 assert( Arg != NULL );
591 if( strcasecmp( Var, "Name" ) == 0 )
594 if( strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName )) >= sizeof( Conf_ServerName ))
595 Config_Error_TooLong( Line, Var );
599 if( strcasecmp( Var, "Info" ) == 0 )
601 /* Info text of server */
602 if( strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo )) >= sizeof( Conf_ServerInfo ))
603 Config_Error_TooLong ( Line, Var );
607 if( strcasecmp( Var, "Password" ) == 0 )
609 /* Global server password */
610 if( strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd )) >= sizeof( Conf_ServerPwd ))
611 Config_Error_TooLong( Line, Var );
615 if( strcasecmp( Var, "AdminInfo1" ) == 0 )
617 /* Administrative info #1 */
618 if( strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 )) >= sizeof( Conf_ServerAdmin1 )) Config_Error_TooLong ( Line, Var );
621 if( strcasecmp( Var, "AdminInfo2" ) == 0 )
623 /* Administrative info #2 */
624 if( strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 )) >= sizeof( Conf_ServerAdmin2 )) Config_Error_TooLong ( Line, Var );
627 if( strcasecmp( Var, "AdminEMail" ) == 0 )
629 /* Administrative email contact */
630 if( strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail )) >= sizeof( Conf_ServerAdminMail )) Config_Error_TooLong( Line, Var );
633 if( strcasecmp( Var, "Ports" ) == 0 )
635 /* Ports on that the server should listen. More port numbers
636 * must be separated by "," */
637 ptr = strtok( Arg, "," );
642 if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Config_Error( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
645 if( port > 0 && port < 0xFFFF ) Conf_ListenPorts[Conf_ListenPorts_Count++] = (UINT16)port;
646 else Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
648 ptr = strtok( NULL, "," );
652 if( strcasecmp( Var, "MotdFile" ) == 0 )
654 /* "Message of the day" (MOTD) file */
655 if( strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile )) >= sizeof( Conf_MotdFile ))
656 Config_Error_TooLong( Line, Var );
660 if( strcasecmp( Var, "MotdPhrase" ) == 0 )
662 /* "Message of the day" phrase (instead of file) */
663 if( strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase )) >= sizeof( Conf_MotdPhrase ))
664 Config_Error_TooLong( Line, Var );
668 if( strcasecmp( Var, "ChrootDir" ) == 0 )
670 /* directory for chroot() */
671 if( strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot )) >= sizeof( Conf_Chroot ))
672 Config_Error_TooLong( Line, Var );
677 if ( strcasecmp( Var, "PidFile" ) == 0 )
679 /* name of pidfile */
680 if( strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile )) >= sizeof( Conf_PidFile ))
681 Config_Error_TooLong( Line, Var );
686 if( strcasecmp( Var, "ServerUID" ) == 0 )
688 /* UID the daemon should switch to */
689 pwd = getpwnam( Arg );
690 if( pwd ) Conf_UID = pwd->pw_uid;
694 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
697 Conf_UID = (unsigned int)atoi( Arg );
701 if( strcasecmp( Var, "ServerGID" ) == 0 )
703 /* GID the daemon should use */
704 grp = getgrnam( Arg );
705 if( grp ) Conf_GID = grp->gr_gid;
709 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
712 Conf_GID = (unsigned int)atoi( Arg );
716 if( strcasecmp( Var, "PingTimeout" ) == 0 )
719 Conf_PingTimeout = atoi( Arg );
720 if( Conf_PingTimeout < 5 )
722 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!", NGIRCd_ConfFile, Line );
723 Conf_PingTimeout = 5;
727 if( strcasecmp( Var, "PongTimeout" ) == 0 )
730 Conf_PongTimeout = atoi( Arg );
731 if( Conf_PongTimeout < 5 )
733 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!", NGIRCd_ConfFile, Line );
734 Conf_PongTimeout = 5;
738 if( strcasecmp( Var, "ConnectRetry" ) == 0 )
740 /* Seconds between connection attempts to other servers */
741 Conf_ConnectRetry = atoi( Arg );
742 if( Conf_ConnectRetry < 5 )
744 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!", NGIRCd_ConfFile, Line );
745 Conf_ConnectRetry = 5;
749 if( strcasecmp( Var, "OperCanUseMode" ) == 0 )
751 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
752 Conf_OperCanMode = Check_ArgIsTrue( Arg );
755 if( strcasecmp( Var, "OperServerMode" ) == 0 )
757 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
758 Conf_OperServerMode = Check_ArgIsTrue( Arg );
761 if( strcasecmp( Var, "MaxConnections" ) == 0 )
763 /* Maximum number of connections. Values <= 0 are equal to "no limit". */
765 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
768 Conf_MaxConnections = atol( Arg );
771 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 )
773 /* Maximum number of simoultanous connections from one IP. Values <= 0 are equal to "no limit". */
775 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
778 Conf_MaxConnectionsIP = atoi( Arg );
781 if( strcasecmp( Var, "MaxJoins" ) == 0 )
783 /* Maximum number of channels a user can join. Values <= 0 are equal to "no limit". */
785 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
788 Conf_MaxJoins = atoi( Arg );
791 if( strcasecmp( Var, "Listen" ) == 0 )
793 /* IP-Address to bind sockets */
794 if( strlcpy( Conf_ListenAddress, Arg, sizeof( Conf_ListenAddress )) >= sizeof( Conf_ListenAddress ))
796 Config_Error_TooLong( Line, Var );
801 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
802 } /* Handle_GLOBAL */
806 Handle_OPERATOR( int Line, char *Var, char *Arg )
808 unsigned int opercount;
811 assert( Var != NULL );
812 assert( Arg != NULL );
813 assert( Conf_Oper_Count > 0 );
815 if ( Conf_Oper_Count == 0 )
818 opercount = Conf_Oper_Count - 1;
820 if( strcasecmp( Var, "Name" ) == 0 ) {
821 /* Name of IRC operator */
822 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
823 if (len >= sizeof( Conf_Oper[opercount].name ))
824 Config_Error_TooLong( Line, Var );
828 if( strcasecmp( Var, "Password" ) == 0 ) {
829 /* Password of IRC operator */
830 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
831 if (len >= sizeof( Conf_Oper[opercount].pwd ))
832 Config_Error_TooLong( Line, Var );
835 if( strcasecmp( Var, "Mask" ) == 0 ) {
836 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
838 Conf_Oper[opercount].mask = strdup( Arg );
839 if (! Conf_Oper[opercount].mask) {
840 Config_Error( LOG_ERR, "%s, line %d: Cannot allocate memory for operator mask: %s",
841 NGIRCd_ConfFile, Line, strerror(errno) );
847 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
848 NGIRCd_ConfFile, Line, Var );
849 } /* Handle_OPERATOR */
853 Handle_SERVER( int Line, char *Var, char *Arg )
858 assert( Var != NULL );
859 assert( Arg != NULL );
861 /* Ignore server block if no space is left in server configuration structure */
862 if( New_Server_Idx <= NONE ) return;
864 if( strcasecmp( Var, "Host" ) == 0 )
866 /* Hostname of the server */
867 if( strlcpy( New_Server.host, Arg, sizeof( New_Server.host )) >= sizeof( New_Server.host ))
868 Config_Error_TooLong ( Line, Var );
872 if( strcasecmp( Var, "Name" ) == 0 )
874 /* Name of the server ("Nick"/"ID") */
875 if( strlcpy( New_Server.name, Arg, sizeof( New_Server.name )) >= sizeof( New_Server.name ))
876 Config_Error_TooLong( Line, Var );
879 if( strcasecmp( Var, "MyPassword" ) == 0 )
881 /* Password of this server which is sent to the peer */
882 if( strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in )) >= sizeof( New_Server.pwd_in )) Config_Error_TooLong( Line, Var );
885 if( strcasecmp( Var, "PeerPassword" ) == 0 )
887 /* Passwort of the peer which must be received */
888 if( strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out )) >= sizeof( New_Server.pwd_out )) Config_Error_TooLong( Line, Var );
891 if( strcasecmp( Var, "Port" ) == 0 )
893 /* Port to which this server should connect */
895 if( port > 0 && port < 0xFFFF ) New_Server.port = (UINT16)port;
896 else Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
899 if( strcasecmp( Var, "Group" ) == 0 )
903 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
906 New_Server.group = atoi( Arg );
910 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
911 } /* Handle_SERVER */
915 Handle_CHANNEL( int Line, char *Var, char *Arg )
918 assert( Var != NULL );
919 assert( Arg != NULL );
921 if( strcasecmp( Var, "Name" ) == 0 )
923 /* Name of the channel */
924 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 ))
925 Config_Error_TooLong( Line, Var );
928 if( strcasecmp( Var, "Modes" ) == 0 )
931 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 ))
932 Config_Error_TooLong( Line, Var );
935 if( strcasecmp( Var, "Topic" ) == 0 )
938 if (!array_copys( &Conf_Channel[Conf_Channel_Count - 1].topic, Arg))
939 Config_Error_TooLong( Line, Var );
944 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
945 } /* Handle_CHANNEL */
949 Validate_Config( bool Configtest )
951 /* Validate configuration settings. */
954 int i, servers, servers_once;
957 if( ! Conf_ServerName[0] )
959 /* No server name configured! */
960 Config_Error( LOG_ALERT, "No server name configured in \"%s\" (section 'Global': 'Name')!", NGIRCd_ConfFile );
963 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
968 if( Conf_ServerName[0] && ! strchr( Conf_ServerName, '.' ))
970 /* No dot in server name! */
971 Config_Error( LOG_ALERT, "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!", NGIRCd_ConfFile );
974 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
980 if( ! Conf_ServerAdminMail[0] )
982 /* No administrative contact configured! */
983 Config_Error( LOG_ALERT, "No administrator email address configured in \"%s\" ('AdminEMail')!", NGIRCd_ConfFile );
986 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
992 if( ! Conf_ServerAdmin1[0] && ! Conf_ServerAdmin2[0] && ! Conf_ServerAdminMail[0] )
994 /* No administrative information configured! */
995 Config_Error( LOG_WARNING, "No administrative information configured but required by RFC!" );
998 servers = servers_once = 0;
999 for( i = 0; i < MAX_SERVERS; i++ )
1001 if( Conf_Server[i].name[0] )
1004 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) servers_once++;
1007 Log( LOG_DEBUG, "Configuration: Operators=%d, Servers=%d[%d], Channels=%d", Conf_Oper_Count, servers, servers_once, Conf_Channel_Count );
1009 } /* Validate_Config */
1013 Config_Error_TooLong ( const int Line, const char *Item )
1015 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1019 Config_Error_NaN( const int Line, const char *Item )
1021 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!", NGIRCd_ConfFile, Line, Item );
1025 LOCAL void Config_Error( const int Level, const char *Format, ... )
1027 LOCAL void Config_Error( Level, Format, va_alist )
1033 /* Error! Write to console and/or logfile. */
1035 char msg[MAX_LOG_MSG_LEN];
1038 assert( Format != NULL );
1041 va_start( ap, Format );
1045 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1048 /* During "normal operations" the log functions of the daemon should
1049 * be used, but during testing of the configuration file, all messages
1050 * should go directly to the console: */
1051 if( Use_Log ) Log( Level, "%s", msg );
1053 } /* Config_Error */
1057 Init_Server_Struct( CONF_SERVER *Server )
1059 /* Initialize server configuration structur to default values */
1061 assert( Server != NULL );
1063 memset( Server, 0, sizeof (CONF_SERVER) );
1065 Server->group = NONE;
1066 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1068 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1070 Server->conn_id = NONE;
1071 } /* Init_Server_Struct */