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.101 2007/10/25 11:01:19 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, bool Rehash ));
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, false);
146 Set_Defaults( false );
148 Validate_Config(false, true);
150 /* Update CLIENT structure of local server */
151 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
152 } /* Config_Rehash */
158 /* Read configuration, validate and output it. */
166 Set_Defaults( true );
169 Validate_Config(true, false);
171 /* If stdin and stdout ("you can read our nice message and we can
172 * read in your keypress") are valid tty's, wait for a key: */
173 if( isatty( fileno( stdin )) && isatty( fileno( stdout ))) {
174 puts( "OK, press enter to see a dump of your service configuration ..." );
177 puts( "Ok, dump of your server configuration follows:\n" );
181 printf( " Name = %s\n", Conf_ServerName );
182 printf( " Info = %s\n", Conf_ServerInfo );
183 printf( " Password = %s\n", Conf_ServerPwd );
184 printf( " AdminInfo1 = %s\n", Conf_ServerAdmin1 );
185 printf( " AdminInfo2 = %s\n", Conf_ServerAdmin2 );
186 printf( " AdminEMail = %s\n", Conf_ServerAdminMail );
187 printf( " MotdFile = %s\n", Conf_MotdFile );
188 printf( " MotdPhrase = %s\n", Conf_MotdPhrase );
189 printf( " ChrootDir = %s\n", Conf_Chroot );
190 printf( " PidFile = %s\n", Conf_PidFile);
191 fputs(" Ports = ", stdout);
193 ports_puts(&Conf_ListenPorts);
195 printf( " Listen = %s\n", Conf_ListenAddress );
196 pwd = getpwuid( Conf_UID );
197 if( pwd ) printf( " ServerUID = %s\n", pwd->pw_name );
198 else printf( " ServerUID = %ld\n", (long)Conf_UID );
199 grp = getgrgid( Conf_GID );
200 if( grp ) printf( " ServerGID = %s\n", grp->gr_name );
201 else printf( " ServerGID = %ld\n", (long)Conf_GID );
202 printf( " PingTimeout = %d\n", Conf_PingTimeout );
203 printf( " PongTimeout = %d\n", Conf_PongTimeout );
204 printf( " ConnectRetry = %d\n", Conf_ConnectRetry );
205 printf( " OperCanUseMode = %s\n", Conf_OperCanMode == true ? "yes" : "no" );
206 printf( " OperServerMode = %s\n", Conf_OperServerMode == true? "yes" : "no" );
207 printf( " PredefChannelsOnly = %s\n", Conf_PredefChannelsOnly == true ? "yes" : "no" );
208 printf( " NoDNS = %s\n", Conf_NoDNS ? "yes" : "no");
209 printf( " MaxConnections = %ld\n", Conf_MaxConnections);
210 printf( " MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
211 printf( " MaxJoins = %d\n\n", Conf_MaxJoins);
213 for( i = 0; i < Conf_Oper_Count; i++ ) {
214 if( ! Conf_Oper[i].name[0] ) continue;
216 /* Valid "Operator" section */
217 puts( "[OPERATOR]" );
218 printf( " Name = %s\n", Conf_Oper[i].name );
219 printf( " Password = %s\n", Conf_Oper[i].pwd );
220 if ( Conf_Oper[i].mask ) printf( " Mask = %s\n", Conf_Oper[i].mask );
224 for( i = 0; i < MAX_SERVERS; i++ ) {
225 if( ! Conf_Server[i].name[0] ) continue;
227 /* Valid "Server" section */
229 printf( " Name = %s\n", Conf_Server[i].name );
230 printf( " Host = %s\n", Conf_Server[i].host );
231 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
232 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
233 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
234 printf( " Group = %d\n", Conf_Server[i].group );
235 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
238 for( i = 0; i < Conf_Channel_Count; i++ ) {
239 if( ! Conf_Channel[i].name[0] ) continue;
241 /* Valid "Channel" section */
243 printf( " Name = %s\n", Conf_Channel[i].name );
244 printf( " Modes = %s\n", Conf_Channel[i].modes );
245 printf( " Key = %s\n", Conf_Channel[i].key );
246 printf( " MaxUsers = %lu\n", Conf_Channel[i].maxusers );
248 topic = (char*)array_start(&Conf_Channel[i].topic);
249 printf( " Topic = %s\n\n", topic ? topic : "");
257 Conf_UnsetServer( CONN_ID Idx )
259 /* Set next time for next connection attempt, if this is a server
260 * link that is (still) configured here. If the server is set as
261 * "once", delete it from our configuration.
262 * Non-Server-Connections will be silently ignored. */
267 /* Check all our configured servers */
268 for( i = 0; i < MAX_SERVERS; i++ ) {
269 if( Conf_Server[i].conn_id != Idx ) continue;
271 /* Gotcha! Mark server configuration as "unused": */
272 Conf_Server[i].conn_id = NONE;
274 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
275 /* Delete configuration here */
276 Init_Server_Struct( &Conf_Server[i] );
278 /* Set time for next connect attempt */
280 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
281 /* The connection has been "long", so we don't
282 * require the next attempt to be delayed. */
283 Conf_Server[i].lasttry =
284 t - Conf_ConnectRetry + RECONNECT_DELAY;
286 Conf_Server[i].lasttry = t;
289 } /* Conf_UnsetServer */
293 Conf_SetServer( int ConfServer, CONN_ID Idx )
295 /* Set connection for specified configured server */
297 assert( ConfServer > NONE );
298 assert( Idx > NONE );
300 Conf_Server[ConfServer].conn_id = Idx;
301 } /* Conf_SetServer */
305 Conf_GetServer( CONN_ID Idx )
307 /* Get index of server in configuration structure */
311 assert( Idx > NONE );
313 for( i = 0; i < MAX_SERVERS; i++ ) {
314 if( Conf_Server[i].conn_id == Idx ) return i;
317 } /* Conf_GetServer */
321 Conf_EnableServer( char *Name, UINT16 Port )
323 /* Enable specified server and adjust port */
327 assert( Name != NULL );
329 for( i = 0; i < MAX_SERVERS; i++ ) {
330 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
331 /* Gotcha! Set port and enable server: */
332 Conf_Server[i].port = Port;
333 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
338 } /* Conf_EnableServer */
342 Conf_EnablePassiveServer(const char *Name)
344 /* Enable specified server */
347 assert( Name != NULL );
348 for (i = 0; i < MAX_SERVERS; i++) {
349 if ((strcasecmp( Conf_Server[i].name, Name ) == 0) && (Conf_Server[i].port > 0)) {
350 /* BINGO! Enable server */
351 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
356 } /* Conf_EnablePassiveServer */
360 Conf_DisableServer( char *Name )
362 /* Enable specified server and adjust port */
366 assert( Name != NULL );
368 for( i = 0; i < MAX_SERVERS; i++ ) {
369 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
370 /* Gotcha! Disable and disconnect server: */
371 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
372 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
377 } /* Conf_DisableServer */
381 Conf_AddServer( char *Name, UINT16 Port, char *Host, char *MyPwd, char *PeerPwd )
383 /* Add new server to configuration */
387 assert( Name != NULL );
388 assert( Host != NULL );
389 assert( MyPwd != NULL );
390 assert( PeerPwd != NULL );
392 /* Search unused item in server configuration structure */
393 for( i = 0; i < MAX_SERVERS; i++ ) {
394 /* Is this item used? */
395 if( ! Conf_Server[i].name[0] ) break;
397 if( i >= MAX_SERVERS ) return false;
399 Init_Server_Struct( &Conf_Server[i] );
400 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
401 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
402 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
403 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
404 Conf_Server[i].port = Port;
405 Conf_Server[i].flags = CONF_SFLAG_ONCE;
408 } /* Conf_AddServer */
412 Set_Defaults( bool InitServers )
414 /* Initialize configuration variables with default values. */
418 strcpy( Conf_ServerName, "" );
419 snprintf( Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
420 strcpy( Conf_ServerPwd, "" );
422 strcpy( Conf_ServerAdmin1, "" );
423 strcpy( Conf_ServerAdmin2, "" );
424 strcpy( Conf_ServerAdminMail, "" );
426 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
427 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
429 strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
431 strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
433 strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
435 strcpy( Conf_ListenAddress, "" );
437 Conf_UID = Conf_GID = 0;
439 Conf_PingTimeout = 120;
440 Conf_PongTimeout = 20;
442 Conf_ConnectRetry = 60;
445 Conf_Channel_Count = 0;
447 Conf_OperCanMode = false;
449 Conf_PredefChannelsOnly = false;
450 Conf_OperServerMode = false;
452 Conf_MaxConnections = 0;
453 Conf_MaxConnectionsIP = 5;
456 /* Initialize server configuration structures */
457 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
464 /* Read configuration file. */
466 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
467 const UINT16 defaultport = 6667;
471 /* Open configuration file */
472 fd = fopen( NGIRCd_ConfFile, "r" );
474 /* No configuration file found! */
475 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
476 NGIRCd_ConfFile, strerror( errno ));
477 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
481 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
483 /* Clean up server configuration structure: mark all already
484 * configured servers as "once" so that they are deleted
485 * after the next disconnect and delete all unused servers.
486 * And delete all servers which are "duplicates" of servers
487 * that are already marked as "once" (such servers have been
488 * created by the last rehash but are now useless). */
489 for( i = 0; i < MAX_SERVERS; i++ ) {
490 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
492 /* This structure is in use ... */
493 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
494 /* Check for duplicates */
495 for( n = 0; n < MAX_SERVERS; n++ ) {
496 if( n == i ) continue;
498 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
499 Init_Server_Struct( &Conf_Server[n] );
501 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
507 /* Mark server as "once" */
508 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
509 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
514 /* Initialize variables */
516 strcpy( section, "" );
517 Init_Server_Struct( &New_Server );
518 New_Server_Idx = NONE;
520 /* Read configuration file */
522 if( ! fgets( str, LINE_LEN, fd )) break;
526 /* Skip comments and empty lines */
527 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
529 /* Is this the beginning of a new section? */
530 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
531 strlcpy( section, str, sizeof( section ));
532 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
535 if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
536 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
537 Config_Error( LOG_ERR, "Too many operators configured.");
539 /* Initialize new operator structure */
540 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
541 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
542 if (Conf_Oper[Conf_Oper_Count].mask) {
543 free(Conf_Oper[Conf_Oper_Count].mask );
544 Conf_Oper[Conf_Oper_Count].mask = NULL;
550 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
551 /* Check if there is already a server to add */
552 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 /* Re-init structure for new server */
559 Init_Server_Struct( &New_Server );
561 /* Search unused item in server configuration structure */
562 for( i = 0; i < MAX_SERVERS; i++ ) {
563 /* Is this item used? */
564 if( ! Conf_Server[i].name[0] ) break;
566 if( i >= MAX_SERVERS ) {
567 /* Oops, no free item found! */
568 Config_Error( LOG_ERR, "Too many servers configured." );
569 New_Server_Idx = NONE;
571 else New_Server_Idx = i;
574 if( strcasecmp( section, "[CHANNEL]" ) == 0 ) {
575 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) {
576 Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
578 /* Initialize new channel structure */
579 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
580 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
581 strcpy( Conf_Channel[Conf_Channel_Count].key, "" );
582 Conf_Channel[Conf_Channel_Count].maxusers = 0;
583 array_free(&Conf_Channel[Conf_Channel_Count].topic);
584 Conf_Channel_Count++;
588 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
591 if( section[0] == 0x1 ) continue;
593 /* Split line into variable name and parameters */
594 ptr = strchr( str, '=' );
596 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
600 var = str; ngt_TrimStr( var );
601 arg = ptr + 1; ngt_TrimStr( arg );
603 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
604 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
605 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
606 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
607 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
610 /* Close configuration file */
613 /* Check if there is still a server to add */
614 if( New_Server.name[0] ) {
615 /* Copy data to "real" server structure */
616 assert( New_Server_Idx > NONE );
617 Conf_Server[New_Server_Idx] = New_Server;
620 if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
621 if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
622 Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
623 (unsigned int) defaultport, strerror(errno));
631 Check_ArgIsTrue( const char *Arg )
633 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
634 if( strcasecmp( Arg, "true" ) == 0 ) return true;
635 if( atoi( Arg ) != 0 ) return true;
638 } /* Check_ArgIsTrue */
642 Handle_GLOBAL( int Line, char *Var, char *Arg )
649 assert( Var != NULL );
650 assert( Arg != NULL );
652 if( strcasecmp( Var, "Name" ) == 0 ) {
654 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
655 if (len >= sizeof( Conf_ServerName ))
656 Config_Error_TooLong( Line, Var );
659 if( strcasecmp( Var, "Info" ) == 0 ) {
660 /* Info text of server */
661 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
662 if (len >= sizeof( Conf_ServerInfo ))
663 Config_Error_TooLong ( Line, Var );
666 if( strcasecmp( Var, "Password" ) == 0 ) {
667 /* Global server password */
668 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
669 if (len >= sizeof( Conf_ServerPwd ))
670 Config_Error_TooLong( Line, Var );
673 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
674 /* Administrative info #1 */
675 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
676 if (len >= sizeof( Conf_ServerAdmin1 ))
677 Config_Error_TooLong ( Line, Var );
680 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
681 /* Administrative info #2 */
682 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
683 if (len >= sizeof( Conf_ServerAdmin2 ))
684 Config_Error_TooLong ( Line, Var );
687 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
688 /* Administrative email contact */
689 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
690 if (len >= sizeof( Conf_ServerAdminMail ))
691 Config_Error_TooLong( Line, Var );
695 if( strcasecmp( Var, "Ports" ) == 0 ) {
696 ports_parse(&Conf_ListenPorts, Line, Arg);
699 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
700 /* "Message of the day" (MOTD) file */
701 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
702 if (len >= sizeof( Conf_MotdFile ))
703 Config_Error_TooLong( Line, Var );
706 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
707 /* "Message of the day" phrase (instead of file) */
708 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
709 if (len >= sizeof( Conf_MotdPhrase ))
710 Config_Error_TooLong( Line, Var );
713 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
714 /* directory for chroot() */
715 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
716 if (len >= sizeof( Conf_Chroot ))
717 Config_Error_TooLong( Line, Var );
720 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
721 /* name of pidfile */
722 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
723 if (len >= sizeof( Conf_PidFile ))
724 Config_Error_TooLong( Line, Var );
727 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
728 /* UID the daemon should switch to */
729 pwd = getpwnam( Arg );
730 if( pwd ) Conf_UID = pwd->pw_uid;
733 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
736 Conf_UID = (unsigned int)atoi( Arg );
740 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
741 /* GID the daemon should use */
742 grp = getgrnam( Arg );
743 if( grp ) Conf_GID = grp->gr_gid;
746 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
749 Conf_GID = (unsigned int)atoi( Arg );
753 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
755 Conf_PingTimeout = atoi( Arg );
756 if( Conf_PingTimeout < 5 ) {
757 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
758 NGIRCd_ConfFile, Line );
759 Conf_PingTimeout = 5;
763 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
765 Conf_PongTimeout = atoi( Arg );
766 if( Conf_PongTimeout < 5 ) {
767 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
768 NGIRCd_ConfFile, Line );
769 Conf_PongTimeout = 5;
773 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
774 /* Seconds between connection attempts to other servers */
775 Conf_ConnectRetry = atoi( Arg );
776 if( Conf_ConnectRetry < 5 ) {
777 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
778 NGIRCd_ConfFile, Line );
779 Conf_ConnectRetry = 5;
783 if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
784 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
785 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
788 if( strcasecmp( Var, "NoDNS" ) == 0 ) {
789 /* don't do reverse dns lookups when clients connect? */
790 Conf_NoDNS = Check_ArgIsTrue( Arg );
793 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
794 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
795 Conf_OperCanMode = Check_ArgIsTrue( Arg );
798 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
799 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
800 Conf_OperServerMode = Check_ArgIsTrue( Arg );
803 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
804 /* Maximum number of connections. 0 -> "no limit". */
806 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
809 Conf_MaxConnections = atol( Arg );
812 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
813 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
815 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
818 Conf_MaxConnectionsIP = atoi( Arg );
821 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
822 /* Maximum number of channels a user can join. 0 -> "no limit". */
824 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
827 Conf_MaxJoins = atoi( Arg );
830 if( strcasecmp( Var, "Listen" ) == 0 ) {
831 /* IP-Address to bind sockets */
832 len = strlcpy( Conf_ListenAddress, Arg, sizeof( Conf_ListenAddress ));
833 if (len >= sizeof( Conf_ListenAddress ))
834 Config_Error_TooLong( Line, Var );
838 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
839 NGIRCd_ConfFile, Line, Var );
840 } /* Handle_GLOBAL */
844 Handle_OPERATOR( int Line, char *Var, char *Arg )
846 unsigned int opercount;
849 assert( Var != NULL );
850 assert( Arg != NULL );
851 assert( Conf_Oper_Count > 0 );
853 if ( Conf_Oper_Count == 0 )
856 opercount = Conf_Oper_Count - 1;
858 if( strcasecmp( Var, "Name" ) == 0 ) {
859 /* Name of IRC operator */
860 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
861 if (len >= sizeof( Conf_Oper[opercount].name ))
862 Config_Error_TooLong( Line, Var );
865 if( strcasecmp( Var, "Password" ) == 0 ) {
866 /* Password of IRC operator */
867 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
868 if (len >= sizeof( Conf_Oper[opercount].pwd ))
869 Config_Error_TooLong( Line, Var );
872 if( strcasecmp( Var, "Mask" ) == 0 ) {
873 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
875 Conf_Oper[opercount].mask = strdup_warn( Arg );
878 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
879 NGIRCd_ConfFile, Line, Var );
880 } /* Handle_OPERATOR */
884 Handle_SERVER( int Line, char *Var, char *Arg )
890 assert( Var != NULL );
891 assert( Arg != NULL );
893 /* Ignore server block if no space is left in server configuration structure */
894 if( New_Server_Idx <= NONE ) return;
896 if( strcasecmp( Var, "Host" ) == 0 ) {
897 /* Hostname of the server */
898 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
899 if (len >= sizeof( New_Server.host ))
900 Config_Error_TooLong ( Line, Var );
903 if( strcasecmp( Var, "Name" ) == 0 ) {
904 /* Name of the server ("Nick"/"ID") */
905 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
906 if (len >= sizeof( New_Server.name ))
907 Config_Error_TooLong( Line, Var );
910 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
911 /* Password of this server which is sent to the peer */
913 Config_Error(LOG_ERR,
914 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
915 NGIRCd_ConfFile, Line);
917 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
918 if (len >= sizeof( New_Server.pwd_in ))
919 Config_Error_TooLong( Line, Var );
922 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
923 /* Passwort of the peer which must be received */
924 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
925 if (len >= sizeof( New_Server.pwd_out ))
926 Config_Error_TooLong( Line, Var );
929 if( strcasecmp( Var, "Port" ) == 0 ) {
930 /* Port to which this server should connect */
932 if( port > 0 && port < 0xFFFF )
933 New_Server.port = (UINT16)port;
935 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
936 NGIRCd_ConfFile, Line, port );
939 if( strcasecmp( Var, "Group" ) == 0 ) {
942 if( ! isdigit( (int)*Arg ))
943 Config_Error_NaN( Line, Var );
946 New_Server.group = atoi( Arg );
949 if( strcasecmp( Var, "Passive" ) == 0 ) {
950 if (Check_ArgIsTrue(Arg))
951 New_Server.flags |= CONF_SFLAG_DISABLED;
955 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
956 NGIRCd_ConfFile, Line, Var );
957 } /* Handle_SERVER */
961 Handle_Channelname(size_t chancount, const char *name)
963 size_t size = sizeof( Conf_Channel[chancount].name );
964 char *dest = Conf_Channel[chancount].name;
966 if (*name && *name != '#') {
971 return size > strlcpy(dest, name, size);
976 Handle_CHANNEL( int Line, char *Var, char *Arg )
979 size_t chancount = 0;
982 assert( Var != NULL );
983 assert( Arg != NULL );
984 if (Conf_Channel_Count > 0)
985 chancount = Conf_Channel_Count - 1;
987 if( strcasecmp( Var, "Name" ) == 0 ) {
988 if (!Handle_Channelname(chancount, Arg))
989 Config_Error_TooLong( Line, Var );
992 if( strcasecmp( Var, "Modes" ) == 0 ) {
994 len = strlcpy( Conf_Channel[chancount].modes, Arg, sizeof( Conf_Channel[chancount].modes ));
995 if (len >= sizeof( Conf_Channel[chancount].modes ))
996 Config_Error_TooLong( Line, Var );
999 if( strcasecmp( Var, "Topic" ) == 0 ) {
1001 if (!array_copys( &Conf_Channel[chancount].topic, Arg))
1002 Config_Error_TooLong( Line, Var );
1006 if( strcasecmp( Var, "Key" ) == 0 ) {
1007 /* Initial Channel Key (mode k) */
1008 len = strlcpy(Conf_Channel[chancount].key, Arg, sizeof(Conf_Channel[chancount].key));
1009 if (len >= sizeof( Conf_Channel[chancount].key ))
1010 Config_Error_TooLong(Line, Var);
1014 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1015 /* maximum user limit, mode l */
1016 Conf_Channel[chancount].maxusers = (unsigned long) atol(Arg);
1017 if (Conf_Channel[chancount].maxusers == 0)
1018 Config_Error_NaN(Line, Var);
1022 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1023 NGIRCd_ConfFile, Line, Var );
1024 } /* Handle_CHANNEL */
1028 Validate_Config(bool Configtest, bool Rehash)
1030 /* Validate configuration settings. */
1033 int i, servers, servers_once;
1037 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1038 ptr = Conf_ServerName;
1040 if (*ptr >= 'a' && *ptr <= 'z') continue;
1041 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1042 if (*ptr >= '0' && *ptr <= '9') continue;
1043 if (ptr > Conf_ServerName) {
1044 if (*ptr == '.' || *ptr == '-')
1047 Conf_ServerName[0] = '\0';
1051 if (!Conf_ServerName[0]) {
1052 /* No server name configured! */
1053 Config_Error(LOG_ALERT,
1054 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1056 if (!Configtest && !Rehash) {
1057 Config_Error(LOG_ALERT,
1058 "%s exiting due to fatal errors!",
1064 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1065 /* No dot in server name! */
1066 Config_Error(LOG_ALERT,
1067 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1070 Config_Error(LOG_ALERT,
1071 "%s exiting due to fatal errors!",
1078 if (!Conf_ServerAdminMail[0]) {
1079 /* No administrative contact configured! */
1080 Config_Error(LOG_ALERT,
1081 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1084 Config_Error(LOG_ALERT,
1085 "%s exiting due to fatal errors!",
1092 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1093 && !Conf_ServerAdminMail[0]) {
1094 /* No administrative information configured! */
1095 Config_Error(LOG_WARNING,
1096 "No administrative information configured but required by RFC!");
1100 servers = servers_once = 0;
1101 for (i = 0; i < MAX_SERVERS; i++) {
1102 if (Conf_Server[i].name[0]) {
1104 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1109 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1110 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1112 } /* Validate_Config */
1116 Config_Error_TooLong ( const int Line, const char *Item )
1118 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1123 Config_Error_NaN( const int Line, const char *Item )
1125 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1126 NGIRCd_ConfFile, Line, Item );
1131 static void Config_Error( const int Level, const char *Format, ... )
1133 static void Config_Error( Level, Format, va_alist )
1139 /* Error! Write to console and/or logfile. */
1141 char msg[MAX_LOG_MSG_LEN];
1144 assert( Format != NULL );
1147 va_start( ap, Format );
1151 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1154 /* During "normal operations" the log functions of the daemon should
1155 * be used, but during testing of the configuration file, all messages
1156 * should go directly to the console: */
1157 if (Use_Log) Log( Level, "%s", msg );
1159 } /* Config_Error */
1163 Init_Server_Struct( CONF_SERVER *Server )
1165 /* Initialize server configuration structur to default values */
1167 assert( Server != NULL );
1169 memset( Server, 0, sizeof (CONF_SERVER) );
1171 Server->group = NONE;
1172 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1174 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1176 Resolve_Init(&Server->res_stat);
1177 Server->conn_id = NONE;
1178 } /* Init_Server_Struct */