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.89 2005/11/21 16:31:30 alex Exp $";
34 #include <sys/types.h>
54 static bool Use_Log = true;
55 static CONF_SERVER New_Server;
56 static int New_Server_Idx;
59 static void Set_Defaults PARAMS(( bool InitServers ));
60 static void Read_Config PARAMS(( void ));
61 static void Validate_Config PARAMS(( bool TestOnly ));
63 static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
64 static void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
65 static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
66 static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
68 static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
70 static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
71 static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
73 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
77 strdup_warn(const char *str)
79 char *ptr = strdup(str);
81 Config_Error(LOG_ERR, "Could not allocate mem for string: %s", str);
91 len = array_length(a, sizeof(UINT16));
93 ports = (UINT16*) array_start(a);
94 printf("%u", (unsigned int) *ports);
97 printf(", %u", (unsigned int) *ports);
105 ports_parse(array *a, int Line, char *Arg)
113 /* Ports on that the server should listen. More port numbers
114 * must be separated by "," */
115 ptr = strtok( Arg, "," );
119 if (port > 0 && port < 0xFFFF) {
120 port16 = (UINT16) port;
121 if (!array_catb(a, (char*)&port16, sizeof port16))
122 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
123 NGIRCd_ConfFile, Line, port, strerror(errno));
125 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
126 NGIRCd_ConfFile, Line, port );
129 ptr = strtok( NULL, "," );
137 Set_Defaults( true );
139 Validate_Config( false );
146 Set_Defaults( false );
148 Validate_Config( false );
149 } /* Config_Rehash */
155 /* Read configuration, validate and output it. */
163 Set_Defaults( true );
166 Validate_Config( true );
168 /* If stdin and stdout ("you can read our nice message and we can
169 * read in your keypress") are valid tty's, wait for a key: */
170 if( isatty( fileno( stdin )) && isatty( fileno( stdout ))) {
171 puts( "OK, press enter to see a dump of your service configuration ..." );
174 puts( "Ok, dump of your server configuration follows:\n" );
178 printf( " Name = %s\n", Conf_ServerName );
179 printf( " Info = %s\n", Conf_ServerInfo );
180 printf( " Password = %s\n", Conf_ServerPwd );
181 printf( " AdminInfo1 = %s\n", Conf_ServerAdmin1 );
182 printf( " AdminInfo2 = %s\n", Conf_ServerAdmin2 );
183 printf( " AdminEMail = %s\n", Conf_ServerAdminMail );
184 printf( " MotdFile = %s\n", Conf_MotdFile );
185 printf( " MotdPhrase = %s\n", Conf_MotdPhrase );
186 printf( " ChrootDir = %s\n", Conf_Chroot );
187 printf( " PidFile = %s\n", Conf_PidFile);
188 fputs(" Ports = ", stdout);
190 ports_puts(&Conf_ListenPorts);
192 printf( " Listen = %s\n", Conf_ListenAddress );
193 pwd = getpwuid( Conf_UID );
194 if( pwd ) printf( " ServerUID = %s\n", pwd->pw_name );
195 else printf( " ServerUID = %ld\n", (long)Conf_UID );
196 grp = getgrgid( Conf_GID );
197 if( grp ) printf( " ServerGID = %s\n", grp->gr_name );
198 else printf( " ServerGID = %ld\n", (long)Conf_GID );
199 printf( " PingTimeout = %d\n", Conf_PingTimeout );
200 printf( " PongTimeout = %d\n", Conf_PongTimeout );
201 printf( " ConnectRetry = %d\n", Conf_ConnectRetry );
202 printf( " OperCanUseMode = %s\n", Conf_OperCanMode == true? "yes" : "no" );
203 printf( " OperServerMode = %s\n", Conf_OperServerMode == true? "yes" : "no" );
204 printf( " MaxConnections = %ld\n", Conf_MaxConnections>0 ? Conf_MaxConnections : -1);
205 printf( " MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP>0 ? Conf_MaxConnectionsIP : -1);
206 printf( " MaxJoins = %d\n\n", Conf_MaxJoins>0 ? Conf_MaxJoins : -1);
208 for( i = 0; i < Conf_Oper_Count; i++ ) {
209 if( ! Conf_Oper[i].name[0] ) continue;
211 /* Valid "Operator" section */
212 puts( "[OPERATOR]" );
213 printf( " Name = %s\n", Conf_Oper[i].name );
214 printf( " Password = %s\n", Conf_Oper[i].pwd );
215 if ( Conf_Oper[i].mask ) printf( " Mask = %s\n", Conf_Oper[i].mask );
219 for( i = 0; i < MAX_SERVERS; i++ ) {
220 if( ! Conf_Server[i].name[0] ) continue;
222 /* Valid "Server" section */
224 printf( " Name = %s\n", Conf_Server[i].name );
225 printf( " Host = %s\n", Conf_Server[i].host );
226 printf( " Port = %d\n", Conf_Server[i].port );
227 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
228 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
229 printf( " Group = %d\n\n", Conf_Server[i].group );
232 for( i = 0; i < Conf_Channel_Count; i++ ) {
233 if( ! Conf_Channel[i].name[0] ) continue;
235 /* Valid "Channel" section */
237 printf( " Name = %s\n", Conf_Channel[i].name );
238 printf( " Modes = %s\n", Conf_Channel[i].modes );
240 topic = (char*)array_start(&Conf_Channel[i].topic);
241 printf( " Topic = %s\n\n", topic ? topic : "");
249 Conf_UnsetServer( CONN_ID Idx )
251 /* Set next time for next connection attempt, if this is a server
252 * link that is (still) configured here. If the server is set as
253 * "once", delete it from our configuration.
254 * Non-Server-Connections will be silently ignored. */
259 /* Check all our configured servers */
260 for( i = 0; i < MAX_SERVERS; i++ ) {
261 if( Conf_Server[i].conn_id != Idx ) continue;
263 /* Gotcha! Mark server configuration as "unused": */
264 Conf_Server[i].conn_id = NONE;
266 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
267 /* Delete configuration here */
268 Init_Server_Struct( &Conf_Server[i] );
270 /* Set time for next connect attempt */
272 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
273 /* The connection has been "long", so we don't
274 * require the next attempt to be delayed. */
275 Conf_Server[i].lasttry =
276 t - Conf_ConnectRetry + RECONNECT_DELAY;
278 Conf_Server[i].lasttry = t;
281 } /* Conf_UnsetServer */
285 Conf_SetServer( int ConfServer, CONN_ID Idx )
287 /* Set connection for specified configured server */
289 assert( ConfServer > NONE );
290 assert( Idx > NONE );
292 Conf_Server[ConfServer].conn_id = Idx;
293 } /* Conf_SetServer */
297 Conf_GetServer( CONN_ID Idx )
299 /* Get index of server in configuration structure */
303 assert( Idx > NONE );
305 for( i = 0; i < MAX_SERVERS; i++ ) {
306 if( Conf_Server[i].conn_id == Idx ) return i;
309 } /* Conf_GetServer */
313 Conf_EnableServer( char *Name, UINT16 Port )
315 /* Enable specified server and adjust port */
319 assert( Name != NULL );
321 for( i = 0; i < MAX_SERVERS; i++ ) {
322 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
323 /* Gotcha! Set port and enable server: */
324 Conf_Server[i].port = Port;
325 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
330 } /* Conf_EnableServer */
334 Conf_DisableServer( char *Name )
336 /* Enable specified server and adjust port */
340 assert( Name != NULL );
342 for( i = 0; i < MAX_SERVERS; i++ ) {
343 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
344 /* Gotcha! Disable and disconnect server: */
345 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
346 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
351 } /* Conf_DisableServer */
355 Conf_AddServer( char *Name, UINT16 Port, char *Host, char *MyPwd, char *PeerPwd )
357 /* Add new server to configuration */
361 assert( Name != NULL );
362 assert( Host != NULL );
363 assert( MyPwd != NULL );
364 assert( PeerPwd != NULL );
366 /* Search unused item in server configuration structure */
367 for( i = 0; i < MAX_SERVERS; i++ ) {
368 /* Is this item used? */
369 if( ! Conf_Server[i].name[0] ) break;
371 if( i >= MAX_SERVERS ) return false;
373 Init_Server_Struct( &Conf_Server[i] );
374 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
375 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
376 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
377 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
378 Conf_Server[i].port = Port;
379 Conf_Server[i].flags = CONF_SFLAG_ONCE;
382 } /* Conf_AddServer */
386 Set_Defaults( bool InitServers )
388 /* Initialize configuration variables with default values. */
392 strcpy( Conf_ServerName, "" );
393 snprintf( Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
394 strcpy( Conf_ServerPwd, "" );
396 strcpy( Conf_ServerAdmin1, "" );
397 strcpy( Conf_ServerAdmin2, "" );
398 strcpy( Conf_ServerAdminMail, "" );
400 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
401 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
403 strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
405 strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
407 strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
409 strcpy( Conf_ListenAddress, "" );
411 Conf_UID = Conf_GID = 0;
413 Conf_PingTimeout = 120;
414 Conf_PongTimeout = 20;
416 Conf_ConnectRetry = 60;
419 Conf_Channel_Count = 0;
421 Conf_OperCanMode = false;
422 Conf_OperServerMode = false;
424 Conf_MaxConnections = -1;
425 Conf_MaxConnectionsIP = 5;
428 /* Initialize server configuration structures */
429 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
436 /* Read configuration file. */
438 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
439 const UINT16 defaultport = 6667;
443 /* Open configuration file */
444 fd = fopen( NGIRCd_ConfFile, "r" );
446 /* No configuration file found! */
447 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
448 NGIRCd_ConfFile, strerror( errno ));
449 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
453 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
455 /* Clean up server configuration structure: mark all already
456 * configured servers as "once" so that they are deleted
457 * after the next disconnect and delete all unused servers.
458 * And delete all servers which are "duplicates" of servers
459 * that are already marked as "once" (such servers have been
460 * created by the last rehash but are now useless). */
461 for( i = 0; i < MAX_SERVERS; i++ ) {
462 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
464 /* This structure is in use ... */
465 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
466 /* Check for duplicates */
467 for( n = 0; n < MAX_SERVERS; n++ ) {
468 if( n == i ) continue;
470 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
471 Init_Server_Struct( &Conf_Server[n] );
473 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
479 /* Mark server as "once" */
480 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
481 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
486 /* Initialize variables */
488 strcpy( section, "" );
489 Init_Server_Struct( &New_Server );
490 New_Server_Idx = NONE;
492 /* Read configuration file */
494 if( ! fgets( str, LINE_LEN, fd )) break;
498 /* Skip comments and empty lines */
499 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
501 /* Is this the beginning of a new section? */
502 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
503 strlcpy( section, str, sizeof( section ));
504 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
507 if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
508 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
509 Config_Error( LOG_ERR, "Too many operators configured.");
511 /* Initialize new operator structure */
512 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
513 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
514 if (Conf_Oper[Conf_Oper_Count].mask) {
515 free(Conf_Oper[Conf_Oper_Count].mask );
516 Conf_Oper[Conf_Oper_Count].mask = NULL;
522 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
523 /* Check if there is already a server to add */
524 if( New_Server.name[0] ) {
525 /* Copy data to "real" server structure */
526 assert( New_Server_Idx > NONE );
527 Conf_Server[New_Server_Idx] = New_Server;
530 /* Re-init structure for new server */
531 Init_Server_Struct( &New_Server );
533 /* Search unused item in server configuration structure */
534 for( i = 0; i < MAX_SERVERS; i++ ) {
535 /* Is this item used? */
536 if( ! Conf_Server[i].name[0] ) break;
538 if( i >= MAX_SERVERS ) {
539 /* Oops, no free item found! */
540 Config_Error( LOG_ERR, "Too many servers configured." );
541 New_Server_Idx = NONE;
543 else New_Server_Idx = i;
546 if( strcasecmp( section, "[CHANNEL]" ) == 0 ) {
547 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) {
548 Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
550 /* Initialize new channel structure */
551 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
552 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
553 array_free(&Conf_Channel[Conf_Channel_Count].topic);
554 Conf_Channel_Count++;
558 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
561 if( section[0] == 0x1 ) continue;
563 /* Split line into variable name and parameters */
564 ptr = strchr( str, '=' );
566 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
570 var = str; ngt_TrimStr( var );
571 arg = ptr + 1; ngt_TrimStr( arg );
573 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
574 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
575 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
576 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
577 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
580 /* Close configuration file */
583 /* Check if there is still a server to add */
584 if( New_Server.name[0] ) {
585 /* Copy data to "real" server structure */
586 assert( New_Server_Idx > NONE );
587 Conf_Server[New_Server_Idx] = New_Server;
590 if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
591 if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
592 Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
593 (unsigned int) defaultport, strerror(errno));
601 Check_ArgIsTrue( const char *Arg )
603 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
604 if( strcasecmp( Arg, "true" ) == 0 ) return true;
605 if( atoi( Arg ) != 0 ) return true;
608 } /* Check_ArgIsTrue */
612 Handle_GLOBAL( int Line, char *Var, char *Arg )
619 assert( Var != NULL );
620 assert( Arg != NULL );
622 if( strcasecmp( Var, "Name" ) == 0 ) {
624 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
625 if (len >= sizeof( Conf_ServerName ))
626 Config_Error_TooLong( Line, Var );
629 if( strcasecmp( Var, "Info" ) == 0 ) {
630 /* Info text of server */
631 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
632 if (len >= sizeof( Conf_ServerInfo ))
633 Config_Error_TooLong ( Line, Var );
636 if( strcasecmp( Var, "Password" ) == 0 ) {
637 /* Global server password */
638 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
639 if (len >= sizeof( Conf_ServerPwd ))
640 Config_Error_TooLong( Line, Var );
643 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
644 /* Administrative info #1 */
645 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
646 if (len >= sizeof( Conf_ServerAdmin1 ))
647 Config_Error_TooLong ( Line, Var );
650 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
651 /* Administrative info #2 */
652 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
653 if (len >= sizeof( Conf_ServerAdmin2 ))
654 Config_Error_TooLong ( Line, Var );
657 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
658 /* Administrative email contact */
659 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
660 if (len >= sizeof( Conf_ServerAdminMail ))
661 Config_Error_TooLong( Line, Var );
665 if( strcasecmp( Var, "Ports" ) == 0 ) {
666 ports_parse(&Conf_ListenPorts, Line, Arg);
669 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
670 /* "Message of the day" (MOTD) file */
671 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
672 if (len >= sizeof( Conf_MotdFile ))
673 Config_Error_TooLong( Line, Var );
676 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
677 /* "Message of the day" phrase (instead of file) */
678 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
679 if (len >= sizeof( Conf_MotdPhrase ))
680 Config_Error_TooLong( Line, Var );
683 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
684 /* directory for chroot() */
685 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
686 if (len >= sizeof( Conf_Chroot ))
687 Config_Error_TooLong( Line, Var );
690 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
691 /* name of pidfile */
692 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
693 if (len >= sizeof( Conf_PidFile ))
694 Config_Error_TooLong( Line, Var );
697 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
698 /* UID the daemon should switch to */
699 pwd = getpwnam( Arg );
700 if( pwd ) Conf_UID = pwd->pw_uid;
703 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
706 Conf_UID = (unsigned int)atoi( Arg );
710 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
711 /* GID the daemon should use */
712 grp = getgrnam( Arg );
713 if( grp ) Conf_GID = grp->gr_gid;
716 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
719 Conf_GID = (unsigned int)atoi( Arg );
723 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
725 Conf_PingTimeout = atoi( Arg );
726 if( Conf_PingTimeout < 5 ) {
727 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
728 NGIRCd_ConfFile, Line );
729 Conf_PingTimeout = 5;
733 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
735 Conf_PongTimeout = atoi( Arg );
736 if( Conf_PongTimeout < 5 ) {
737 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
738 NGIRCd_ConfFile, Line );
739 Conf_PongTimeout = 5;
743 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
744 /* Seconds between connection attempts to other servers */
745 Conf_ConnectRetry = atoi( Arg );
746 if( Conf_ConnectRetry < 5 ) {
747 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
748 NGIRCd_ConfFile, Line );
749 Conf_ConnectRetry = 5;
753 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
754 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
755 Conf_OperCanMode = Check_ArgIsTrue( Arg );
758 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
759 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
760 Conf_OperServerMode = Check_ArgIsTrue( Arg );
763 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
764 /* Maximum number of connections. Values <= 0 are equal to "no limit". */
766 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
769 Conf_MaxConnections = atol( Arg );
772 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
773 /* Maximum number of simultaneous connections from one IP. Values <= 0 -> "no limit" */
775 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
778 Conf_MaxConnectionsIP = atoi( Arg );
781 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
782 /* Maximum number of channels a user can join. Values <= 0 are equal to "no limit". */
784 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
787 Conf_MaxJoins = atoi( Arg );
790 if( strcasecmp( Var, "Listen" ) == 0 ) {
791 /* IP-Address to bind sockets */
792 len = strlcpy( Conf_ListenAddress, Arg, sizeof( Conf_ListenAddress ));
793 if (len >= sizeof( Conf_ListenAddress ))
794 Config_Error_TooLong( Line, Var );
798 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
799 NGIRCd_ConfFile, Line, Var );
800 } /* Handle_GLOBAL */
804 Handle_OPERATOR( int Line, char *Var, char *Arg )
806 unsigned int opercount;
809 assert( Var != NULL );
810 assert( Arg != NULL );
811 assert( Conf_Oper_Count > 0 );
813 if ( Conf_Oper_Count == 0 )
816 opercount = Conf_Oper_Count - 1;
818 if( strcasecmp( Var, "Name" ) == 0 ) {
819 /* Name of IRC operator */
820 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
821 if (len >= sizeof( Conf_Oper[opercount].name ))
822 Config_Error_TooLong( Line, Var );
825 if( strcasecmp( Var, "Password" ) == 0 ) {
826 /* Password of IRC operator */
827 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
828 if (len >= sizeof( Conf_Oper[opercount].pwd ))
829 Config_Error_TooLong( Line, Var );
832 if( strcasecmp( Var, "Mask" ) == 0 ) {
833 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
835 Conf_Oper[opercount].mask = strdup_warn( Arg );
838 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
839 NGIRCd_ConfFile, Line, Var );
840 } /* Handle_OPERATOR */
844 Handle_SERVER( int Line, char *Var, char *Arg )
850 assert( Var != NULL );
851 assert( Arg != NULL );
853 /* Ignore server block if no space is left in server configuration structure */
854 if( New_Server_Idx <= NONE ) return;
856 if( strcasecmp( Var, "Host" ) == 0 ) {
857 /* Hostname of the server */
858 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
859 if (len >= sizeof( New_Server.host ))
860 Config_Error_TooLong ( Line, Var );
863 if( strcasecmp( Var, "Name" ) == 0 ) {
864 /* Name of the server ("Nick"/"ID") */
865 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
866 if (len >= sizeof( New_Server.name ))
867 Config_Error_TooLong( Line, Var );
870 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
871 /* Password of this server which is sent to the peer */
873 Config_Error(LOG_ERR,
874 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
875 NGIRCd_ConfFile, Line);
877 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
878 if (len >= sizeof( New_Server.pwd_in ))
879 Config_Error_TooLong( Line, Var );
882 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
883 /* Passwort of the peer which must be received */
884 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
885 if (len >= sizeof( New_Server.pwd_out ))
886 Config_Error_TooLong( Line, Var );
889 if( strcasecmp( Var, "Port" ) == 0 ) {
890 /* Port to which this server should connect */
892 if( port > 0 && port < 0xFFFF )
893 New_Server.port = (UINT16)port;
895 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
896 NGIRCd_ConfFile, Line, port );
899 if( strcasecmp( Var, "Group" ) == 0 ) {
902 if( ! isdigit( (int)*Arg ))
903 Config_Error_NaN( Line, Var );
906 New_Server.group = atoi( Arg );
910 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
911 NGIRCd_ConfFile, Line, Var );
912 } /* Handle_SERVER */
916 Handle_CHANNEL( int Line, char *Var, char *Arg )
919 size_t chancount = 0;
922 assert( Var != NULL );
923 assert( Arg != NULL );
924 if (Conf_Channel_Count > 0)
925 chancount = Conf_Channel_Count - 1;
927 if( strcasecmp( Var, "Name" ) == 0 ) {
928 /* Name of the channel */
929 len = strlcpy( Conf_Channel[chancount].name, Arg, sizeof( Conf_Channel[chancount].name ));
930 if (len >= sizeof( Conf_Channel[chancount].name ))
931 Config_Error_TooLong( Line, Var );
934 if( strcasecmp( Var, "Modes" ) == 0 ) {
936 len = strlcpy( Conf_Channel[chancount].modes, Arg, sizeof( Conf_Channel[chancount].modes ));
937 if (len >= sizeof( Conf_Channel[chancount].modes ))
938 Config_Error_TooLong( Line, Var );
941 if( strcasecmp( Var, "Topic" ) == 0 ) {
943 if (!array_copys( &Conf_Channel[chancount].topic, Arg))
944 Config_Error_TooLong( Line, Var );
948 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
949 NGIRCd_ConfFile, Line, Var );
950 } /* Handle_CHANNEL */
954 Validate_Config( bool Configtest )
956 /* Validate configuration settings. */
959 int i, servers, servers_once;
962 if( ! Conf_ServerName[0] ) {
963 /* No server name configured! */
964 Config_Error( LOG_ALERT, "No server name configured in \"%s\" (section 'Global': 'Name')!",
967 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
972 if( Conf_ServerName[0] && ! strchr( Conf_ServerName, '.' )) {
973 /* No dot in server name! */
974 Config_Error( LOG_ALERT, "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!", NGIRCd_ConfFile );
976 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
982 if( ! Conf_ServerAdminMail[0] ) {
983 /* No administrative contact configured! */
984 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] ) {
993 /* No administrative information configured! */
994 Config_Error( LOG_WARNING, "No administrative information configured but required by RFC!" );
997 servers = servers_once = 0;
998 for( i = 0; i < MAX_SERVERS; i++ ) {
999 if( Conf_Server[i].name[0] ) {
1001 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) servers_once++;
1004 Log( LOG_DEBUG, "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1005 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count );
1007 } /* Validate_Config */
1011 Config_Error_TooLong ( const int Line, const char *Item )
1013 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1018 Config_Error_NaN( const int Line, const char *Item )
1020 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1021 NGIRCd_ConfFile, Line, Item );
1026 static void Config_Error( const int Level, const char *Format, ... )
1028 static void Config_Error( Level, Format, va_alist )
1034 /* Error! Write to console and/or logfile. */
1036 char msg[MAX_LOG_MSG_LEN];
1039 assert( Format != NULL );
1042 va_start( ap, Format );
1046 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1049 /* During "normal operations" the log functions of the daemon should
1050 * be used, but during testing of the configuration file, all messages
1051 * should go directly to the console: */
1052 if (Use_Log) Log( Level, "%s", msg );
1054 } /* Config_Error */
1058 Init_Server_Struct( CONF_SERVER *Server )
1060 /* Initialize server configuration structur to default values */
1062 assert( Server != NULL );
1064 memset( Server, 0, sizeof (CONF_SERVER) );
1066 Server->group = NONE;
1067 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1069 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1071 Server->conn_id = NONE;
1072 } /* Init_Server_Struct */