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.86 2005/09/10 23:42:12 fw 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 *dup = 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. */
258 /* Check all our configured servers */
259 for( i = 0; i < MAX_SERVERS; i++ ) {
260 if( Conf_Server[i].conn_id != Idx ) continue;
262 /* Gotcha! Mark server configuration as "unused": */
263 Conf_Server[i].conn_id = NONE;
265 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
266 /* Delete configuration here */
267 Init_Server_Struct( &Conf_Server[i] );
269 /* Set time for next connect attempt */
270 if( Conf_Server[i].lasttry < time( NULL ) - Conf_ConnectRetry ) {
271 /* Okay, the connection was established "long enough": */
272 Conf_Server[i].lasttry = time( NULL ) - Conf_ConnectRetry + RECONNECT_DELAY;
276 } /* Conf_UnsetServer */
280 Conf_SetServer( int ConfServer, CONN_ID Idx )
282 /* Set connection for specified configured server */
284 assert( ConfServer > NONE );
285 assert( Idx > NONE );
287 Conf_Server[ConfServer].conn_id = Idx;
288 } /* Conf_SetServer */
292 Conf_GetServer( CONN_ID Idx )
294 /* Get index of server in configuration structure */
298 assert( Idx > NONE );
300 for( i = 0; i < MAX_SERVERS; i++ ) {
301 if( Conf_Server[i].conn_id == Idx ) return i;
304 } /* Conf_GetServer */
308 Conf_EnableServer( char *Name, UINT16 Port )
310 /* Enable specified server and adjust port */
314 assert( Name != NULL );
316 for( i = 0; i < MAX_SERVERS; i++ ) {
317 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
318 /* Gotcha! Set port and enable server: */
319 Conf_Server[i].port = Port;
320 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
325 } /* Conf_EnableServer */
329 Conf_DisableServer( char *Name )
331 /* Enable specified server and adjust port */
335 assert( Name != NULL );
337 for( i = 0; i < MAX_SERVERS; i++ ) {
338 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
339 /* Gotcha! Disable and disconnect server: */
340 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
341 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
346 } /* Conf_DisableServer */
350 Conf_AddServer( char *Name, UINT16 Port, char *Host, char *MyPwd, char *PeerPwd )
352 /* Add new server to configuration */
356 assert( Name != NULL );
357 assert( Host != NULL );
358 assert( MyPwd != NULL );
359 assert( PeerPwd != NULL );
361 /* Search unused item in server configuration structure */
362 for( i = 0; i < MAX_SERVERS; i++ ) {
363 /* Is this item used? */
364 if( ! Conf_Server[i].name[0] ) break;
366 if( i >= MAX_SERVERS ) return false;
368 Init_Server_Struct( &Conf_Server[i] );
369 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
370 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
371 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
372 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
373 Conf_Server[i].port = Port;
374 Conf_Server[i].flags = CONF_SFLAG_ONCE;
377 } /* Conf_AddServer */
381 Set_Defaults( bool InitServers )
383 /* Initialize configuration variables with default values. */
387 strcpy( Conf_ServerName, "" );
388 snprintf( Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
389 strcpy( Conf_ServerPwd, "" );
391 strcpy( Conf_ServerAdmin1, "" );
392 strcpy( Conf_ServerAdmin2, "" );
393 strcpy( Conf_ServerAdminMail, "" );
395 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
396 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
398 strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
400 strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
402 strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
404 strcpy( Conf_ListenAddress, "" );
406 Conf_UID = Conf_GID = 0;
408 Conf_PingTimeout = 120;
409 Conf_PongTimeout = 20;
411 Conf_ConnectRetry = 60;
414 Conf_Channel_Count = 0;
416 Conf_OperCanMode = false;
417 Conf_OperServerMode = false;
419 Conf_MaxConnections = -1;
420 Conf_MaxConnectionsIP = 5;
423 /* Initialize server configuration structures */
424 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
431 /* Read configuration file. */
433 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
434 const UINT16 defaultport = 6667;
438 /* Open configuration file */
439 fd = fopen( NGIRCd_ConfFile, "r" );
441 /* No configuration file found! */
442 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
443 NGIRCd_ConfFile, strerror( errno ));
444 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
448 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
450 /* Clean up server configuration structure: mark all already
451 * configured servers as "once" so that they are deleted
452 * after the next disconnect and delete all unused servers.
453 * And delete all servers which are "duplicates" of servers
454 * that are already marked as "once" (such servers have been
455 * created by the last rehash but are now useless). */
456 for( i = 0; i < MAX_SERVERS; i++ ) {
457 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
459 /* This structure is in use ... */
460 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
461 /* Check for duplicates */
462 for( n = 0; n < MAX_SERVERS; n++ ) {
463 if( n == i ) continue;
465 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
466 Init_Server_Struct( &Conf_Server[n] );
468 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
474 /* Mark server as "once" */
475 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
476 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
481 /* Initialize variables */
483 strcpy( section, "" );
484 Init_Server_Struct( &New_Server );
485 New_Server_Idx = NONE;
487 /* Read configuration file */
489 if( ! fgets( str, LINE_LEN, fd )) break;
493 /* Skip comments and empty lines */
494 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
496 /* Is this the beginning of a new section? */
497 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
498 strlcpy( section, str, sizeof( section ));
499 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
502 if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
503 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
504 Config_Error( LOG_ERR, "Too many operators configured.");
506 /* Initialize new operator structure */
507 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
508 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
509 if (Conf_Oper[Conf_Oper_Count].mask) {
510 free(Conf_Oper[Conf_Oper_Count].mask );
511 Conf_Oper[Conf_Oper_Count].mask = NULL;
517 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
518 /* Check if there is already a server to add */
519 if( New_Server.name[0] ) {
520 /* Copy data to "real" server structure */
521 assert( New_Server_Idx > NONE );
522 Conf_Server[New_Server_Idx] = New_Server;
525 /* Re-init structure for new server */
526 Init_Server_Struct( &New_Server );
528 /* Search unused item in server configuration structure */
529 for( i = 0; i < MAX_SERVERS; i++ ) {
530 /* Is this item used? */
531 if( ! Conf_Server[i].name[0] ) break;
533 if( i >= MAX_SERVERS ) {
534 /* Oops, no free item found! */
535 Config_Error( LOG_ERR, "Too many servers configured." );
536 New_Server_Idx = NONE;
538 else New_Server_Idx = i;
541 if( strcasecmp( section, "[CHANNEL]" ) == 0 ) {
542 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) {
543 Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
545 /* Initialize new channel structure */
546 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
547 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
548 array_free(&Conf_Channel[Conf_Channel_Count].topic);
549 Conf_Channel_Count++;
553 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
556 if( section[0] == 0x1 ) continue;
558 /* Split line into variable name and parameters */
559 ptr = strchr( str, '=' );
561 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
565 var = str; ngt_TrimStr( var );
566 arg = ptr + 1; ngt_TrimStr( arg );
568 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
569 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
570 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
571 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
572 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
575 /* Close configuration file */
578 /* Check if there is still a server to add */
579 if( New_Server.name[0] ) {
580 /* Copy data to "real" server structure */
581 assert( New_Server_Idx > NONE );
582 Conf_Server[New_Server_Idx] = New_Server;
585 if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
586 if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
587 Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
588 (unsigned int) defaultport, strerror(errno));
596 Check_ArgIsTrue( const char *Arg )
598 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
599 if( strcasecmp( Arg, "true" ) == 0 ) return true;
600 if( atoi( Arg ) != 0 ) return true;
603 } /* Check_ArgIsTrue */
607 Handle_GLOBAL( int Line, char *Var, char *Arg )
614 assert( Var != NULL );
615 assert( Arg != NULL );
617 if( strcasecmp( Var, "Name" ) == 0 ) {
619 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
620 if (len >= sizeof( Conf_ServerName ))
621 Config_Error_TooLong( Line, Var );
624 if( strcasecmp( Var, "Info" ) == 0 ) {
625 /* Info text of server */
626 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
627 if (len >= sizeof( Conf_ServerInfo ))
628 Config_Error_TooLong ( Line, Var );
631 if( strcasecmp( Var, "Password" ) == 0 ) {
632 /* Global server password */
633 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
634 if (len >= sizeof( Conf_ServerPwd ))
635 Config_Error_TooLong( Line, Var );
638 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
639 /* Administrative info #1 */
640 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
641 if (len >= sizeof( Conf_ServerAdmin1 ))
642 Config_Error_TooLong ( Line, Var );
645 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
646 /* Administrative info #2 */
647 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
648 if (len >= sizeof( Conf_ServerAdmin2 ))
649 Config_Error_TooLong ( Line, Var );
652 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
653 /* Administrative email contact */
654 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
655 if (len >= sizeof( Conf_ServerAdminMail ))
656 Config_Error_TooLong( Line, Var );
660 if( strcasecmp( Var, "Ports" ) == 0 ) {
661 ports_parse(&Conf_ListenPorts, Line, Arg);
664 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
665 /* "Message of the day" (MOTD) file */
666 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
667 if (len >= sizeof( Conf_MotdFile ))
668 Config_Error_TooLong( Line, Var );
671 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
672 /* "Message of the day" phrase (instead of file) */
673 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
674 if (len >= sizeof( Conf_MotdPhrase ))
675 Config_Error_TooLong( Line, Var );
678 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
679 /* directory for chroot() */
680 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
681 if (len >= sizeof( Conf_Chroot ))
682 Config_Error_TooLong( Line, Var );
685 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
686 /* name of pidfile */
687 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
688 if (len >= sizeof( Conf_PidFile ))
689 Config_Error_TooLong( Line, Var );
692 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
693 /* UID the daemon should switch to */
694 pwd = getpwnam( Arg );
695 if( pwd ) Conf_UID = pwd->pw_uid;
698 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
701 Conf_UID = (unsigned int)atoi( Arg );
705 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
706 /* GID the daemon should use */
707 grp = getgrnam( Arg );
708 if( grp ) Conf_GID = grp->gr_gid;
711 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
714 Conf_GID = (unsigned int)atoi( Arg );
718 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
720 Conf_PingTimeout = atoi( Arg );
721 if( Conf_PingTimeout < 5 ) {
722 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
723 NGIRCd_ConfFile, Line );
724 Conf_PingTimeout = 5;
728 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
730 Conf_PongTimeout = atoi( Arg );
731 if( Conf_PongTimeout < 5 ) {
732 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
733 NGIRCd_ConfFile, Line );
734 Conf_PongTimeout = 5;
738 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
739 /* Seconds between connection attempts to other servers */
740 Conf_ConnectRetry = atoi( Arg );
741 if( Conf_ConnectRetry < 5 ) {
742 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
743 NGIRCd_ConfFile, Line );
744 Conf_ConnectRetry = 5;
748 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
749 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
750 Conf_OperCanMode = Check_ArgIsTrue( Arg );
753 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
754 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
755 Conf_OperServerMode = Check_ArgIsTrue( Arg );
758 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
759 /* Maximum number of connections. Values <= 0 are equal to "no limit". */
761 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
764 Conf_MaxConnections = atol( Arg );
767 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
768 /* Maximum number of simultaneous connections from one IP. Values <= 0 -> "no limit" */
770 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
773 Conf_MaxConnectionsIP = atoi( Arg );
776 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
777 /* Maximum number of channels a user can join. Values <= 0 are equal to "no limit". */
779 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
782 Conf_MaxJoins = atoi( Arg );
785 if( strcasecmp( Var, "Listen" ) == 0 ) {
786 /* IP-Address to bind sockets */
787 len = strlcpy( Conf_ListenAddress, Arg, sizeof( Conf_ListenAddress ));
788 if (len >= sizeof( Conf_ListenAddress ))
789 Config_Error_TooLong( Line, Var );
793 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
794 NGIRCd_ConfFile, Line, Var );
795 } /* Handle_GLOBAL */
799 Handle_OPERATOR( int Line, char *Var, char *Arg )
801 unsigned int opercount;
804 assert( Var != NULL );
805 assert( Arg != NULL );
806 assert( Conf_Oper_Count > 0 );
808 if ( Conf_Oper_Count == 0 )
811 opercount = Conf_Oper_Count - 1;
813 if( strcasecmp( Var, "Name" ) == 0 ) {
814 /* Name of IRC operator */
815 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
816 if (len >= sizeof( Conf_Oper[opercount].name ))
817 Config_Error_TooLong( Line, Var );
820 if( strcasecmp( Var, "Password" ) == 0 ) {
821 /* Password of IRC operator */
822 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
823 if (len >= sizeof( Conf_Oper[opercount].pwd ))
824 Config_Error_TooLong( Line, Var );
827 if( strcasecmp( Var, "Mask" ) == 0 ) {
828 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
830 Conf_Oper[opercount].mask = strdup_warn( Arg );
833 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
834 NGIRCd_ConfFile, Line, Var );
835 } /* Handle_OPERATOR */
839 Handle_SERVER( int Line, char *Var, char *Arg )
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 ) {
852 /* Hostname of the server */
853 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
854 if (len >= sizeof( New_Server.host ))
855 Config_Error_TooLong ( Line, Var );
858 if( strcasecmp( Var, "Name" ) == 0 ) {
859 /* Name of the server ("Nick"/"ID") */
860 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
861 if (len >= sizeof( New_Server.name ))
862 Config_Error_TooLong( Line, Var );
865 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
866 /* Password of this server which is sent to the peer */
868 Config_Error(LOG_ERR,
869 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
870 NGIRCd_ConfFile, Line);
872 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
873 if (len >= sizeof( New_Server.pwd_in ))
874 Config_Error_TooLong( Line, Var );
877 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
878 /* Passwort of the peer which must be received */
879 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
880 if (len >= sizeof( New_Server.pwd_out ))
881 Config_Error_TooLong( Line, Var );
884 if( strcasecmp( Var, "Port" ) == 0 ) {
885 /* Port to which this server should connect */
887 if( port > 0 && port < 0xFFFF )
888 New_Server.port = (UINT16)port;
890 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
891 NGIRCd_ConfFile, Line, port );
894 if( strcasecmp( Var, "Group" ) == 0 ) {
897 if( ! isdigit( (int)*Arg ))
898 Config_Error_NaN( Line, Var );
901 New_Server.group = atoi( Arg );
905 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
906 NGIRCd_ConfFile, Line, Var );
907 } /* Handle_SERVER */
911 Handle_CHANNEL( int Line, char *Var, char *Arg )
914 size_t chancount = 0;
917 assert( Var != NULL );
918 assert( Arg != NULL );
919 if (Conf_Channel_Count > 0)
920 chancount = Conf_Channel_Count - 1;
922 if( strcasecmp( Var, "Name" ) == 0 ) {
923 /* Name of the channel */
924 len = strlcpy( Conf_Channel[chancount].name, Arg, sizeof( Conf_Channel[chancount].name ));
925 if (len >= sizeof( Conf_Channel[chancount].name ))
926 Config_Error_TooLong( Line, Var );
929 if( strcasecmp( Var, "Modes" ) == 0 ) {
931 len = strlcpy( Conf_Channel[chancount].modes, Arg, sizeof( Conf_Channel[chancount].modes ));
932 if (len >= sizeof( Conf_Channel[chancount].modes ))
933 Config_Error_TooLong( Line, Var );
936 if( strcasecmp( Var, "Topic" ) == 0 ) {
938 if (!array_copys( &Conf_Channel[chancount].topic, Arg))
939 Config_Error_TooLong( Line, Var );
943 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
944 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] ) {
958 /* No server name configured! */
959 Config_Error( LOG_ALERT, "No server name configured in \"%s\" (section 'Global': 'Name')!",
962 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
967 if( Conf_ServerName[0] && ! strchr( Conf_ServerName, '.' )) {
968 /* No dot in server name! */
969 Config_Error( LOG_ALERT, "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!", NGIRCd_ConfFile );
971 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
977 if( ! Conf_ServerAdminMail[0] ) {
978 /* No administrative contact configured! */
979 Config_Error( LOG_ALERT, "No administrator email address configured in \"%s\" ('AdminEMail')!", NGIRCd_ConfFile );
981 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
987 if( ! Conf_ServerAdmin1[0] && ! Conf_ServerAdmin2[0] && ! Conf_ServerAdminMail[0] ) {
988 /* No administrative information configured! */
989 Config_Error( LOG_WARNING, "No administrative information configured but required by RFC!" );
992 servers = servers_once = 0;
993 for( i = 0; i < MAX_SERVERS; i++ ) {
994 if( Conf_Server[i].name[0] ) {
996 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) servers_once++;
999 Log( LOG_DEBUG, "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1000 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count );
1002 } /* Validate_Config */
1006 Config_Error_TooLong ( const int Line, const char *Item )
1008 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1012 Config_Error_NaN( const int Line, const char *Item )
1014 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1015 NGIRCd_ConfFile, Line, Item );
1019 static void Config_Error( const int Level, const char *Format, ... )
1021 static void Config_Error( Level, Format, va_alist )
1027 /* Error! Write to console and/or logfile. */
1029 char msg[MAX_LOG_MSG_LEN];
1032 assert( Format != NULL );
1035 va_start( ap, Format );
1039 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1042 /* During "normal operations" the log functions of the daemon should
1043 * be used, but during testing of the configuration file, all messages
1044 * should go directly to the console: */
1045 if (Use_Log) Log( Level, "%s", msg );
1047 } /* Config_Error */
1051 Init_Server_Struct( CONF_SERVER *Server )
1053 /* Initialize server configuration structur to default values */
1055 assert( Server != NULL );
1057 memset( Server, 0, sizeof (CONF_SERVER) );
1059 Server->group = NONE;
1060 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1062 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1064 Server->conn_id = NONE;
1065 } /* Init_Server_Struct */