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.93 2006/10/03 10:59:41 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, 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( " MaxConnections = %ld\n", Conf_MaxConnections>0 ? Conf_MaxConnections : -1);
208 printf( " MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP>0 ? Conf_MaxConnectionsIP : -1);
209 printf( " MaxJoins = %d\n\n", Conf_MaxJoins>0 ? Conf_MaxJoins : -1);
211 for( i = 0; i < Conf_Oper_Count; i++ ) {
212 if( ! Conf_Oper[i].name[0] ) continue;
214 /* Valid "Operator" section */
215 puts( "[OPERATOR]" );
216 printf( " Name = %s\n", Conf_Oper[i].name );
217 printf( " Password = %s\n", Conf_Oper[i].pwd );
218 if ( Conf_Oper[i].mask ) printf( " Mask = %s\n", Conf_Oper[i].mask );
222 for( i = 0; i < MAX_SERVERS; i++ ) {
223 if( ! Conf_Server[i].name[0] ) continue;
225 /* Valid "Server" section */
227 printf( " Name = %s\n", Conf_Server[i].name );
228 printf( " Host = %s\n", Conf_Server[i].host );
229 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
230 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
231 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
232 printf( " Group = %d\n\n", Conf_Server[i].group );
235 for( i = 0; i < Conf_Channel_Count; i++ ) {
236 if( ! Conf_Channel[i].name[0] ) continue;
238 /* Valid "Channel" section */
240 printf( " Name = %s\n", Conf_Channel[i].name );
241 printf( " Modes = %s\n", Conf_Channel[i].modes );
243 topic = (char*)array_start(&Conf_Channel[i].topic);
244 printf( " Topic = %s\n\n", topic ? topic : "");
252 Conf_UnsetServer( CONN_ID Idx )
254 /* Set next time for next connection attempt, if this is a server
255 * link that is (still) configured here. If the server is set as
256 * "once", delete it from our configuration.
257 * Non-Server-Connections will be silently ignored. */
262 /* Check all our configured servers */
263 for( i = 0; i < MAX_SERVERS; i++ ) {
264 if( Conf_Server[i].conn_id != Idx ) continue;
266 /* Gotcha! Mark server configuration as "unused": */
267 Conf_Server[i].conn_id = NONE;
269 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
270 /* Delete configuration here */
271 Init_Server_Struct( &Conf_Server[i] );
273 /* Set time for next connect attempt */
275 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
276 /* The connection has been "long", so we don't
277 * require the next attempt to be delayed. */
278 Conf_Server[i].lasttry =
279 t - Conf_ConnectRetry + RECONNECT_DELAY;
281 Conf_Server[i].lasttry = t;
284 } /* Conf_UnsetServer */
288 Conf_SetServer( int ConfServer, CONN_ID Idx )
290 /* Set connection for specified configured server */
292 assert( ConfServer > NONE );
293 assert( Idx > NONE );
295 Conf_Server[ConfServer].conn_id = Idx;
296 } /* Conf_SetServer */
300 Conf_GetServer( CONN_ID Idx )
302 /* Get index of server in configuration structure */
306 assert( Idx > NONE );
308 for( i = 0; i < MAX_SERVERS; i++ ) {
309 if( Conf_Server[i].conn_id == Idx ) return i;
312 } /* Conf_GetServer */
316 Conf_EnableServer( char *Name, UINT16 Port )
318 /* Enable specified server and adjust port */
322 assert( Name != NULL );
324 for( i = 0; i < MAX_SERVERS; i++ ) {
325 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
326 /* Gotcha! Set port and enable server: */
327 Conf_Server[i].port = Port;
328 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
333 } /* Conf_EnableServer */
337 Conf_DisableServer( char *Name )
339 /* Enable specified server and adjust port */
343 assert( Name != NULL );
345 for( i = 0; i < MAX_SERVERS; i++ ) {
346 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
347 /* Gotcha! Disable and disconnect server: */
348 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
349 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
354 } /* Conf_DisableServer */
358 Conf_AddServer( char *Name, UINT16 Port, char *Host, char *MyPwd, char *PeerPwd )
360 /* Add new server to configuration */
364 assert( Name != NULL );
365 assert( Host != NULL );
366 assert( MyPwd != NULL );
367 assert( PeerPwd != NULL );
369 /* Search unused item in server configuration structure */
370 for( i = 0; i < MAX_SERVERS; i++ ) {
371 /* Is this item used? */
372 if( ! Conf_Server[i].name[0] ) break;
374 if( i >= MAX_SERVERS ) return false;
376 Init_Server_Struct( &Conf_Server[i] );
377 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
378 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
379 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
380 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
381 Conf_Server[i].port = Port;
382 Conf_Server[i].flags = CONF_SFLAG_ONCE;
385 } /* Conf_AddServer */
389 Set_Defaults( bool InitServers )
391 /* Initialize configuration variables with default values. */
395 strcpy( Conf_ServerName, "" );
396 snprintf( Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
397 strcpy( Conf_ServerPwd, "" );
399 strcpy( Conf_ServerAdmin1, "" );
400 strcpy( Conf_ServerAdmin2, "" );
401 strcpy( Conf_ServerAdminMail, "" );
403 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
404 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
406 strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
408 strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
410 strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
412 strcpy( Conf_ListenAddress, "" );
414 Conf_UID = Conf_GID = 0;
416 Conf_PingTimeout = 120;
417 Conf_PongTimeout = 20;
419 Conf_ConnectRetry = 60;
422 Conf_Channel_Count = 0;
424 Conf_OperCanMode = false;
425 Conf_OperServerMode = false;
427 Conf_MaxConnections = -1;
428 Conf_MaxConnectionsIP = 5;
431 /* Initialize server configuration structures */
432 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
439 /* Read configuration file. */
441 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
442 const UINT16 defaultport = 6667;
446 /* Open configuration file */
447 fd = fopen( NGIRCd_ConfFile, "r" );
449 /* No configuration file found! */
450 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
451 NGIRCd_ConfFile, strerror( errno ));
452 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
456 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
458 /* Clean up server configuration structure: mark all already
459 * configured servers as "once" so that they are deleted
460 * after the next disconnect and delete all unused servers.
461 * And delete all servers which are "duplicates" of servers
462 * that are already marked as "once" (such servers have been
463 * created by the last rehash but are now useless). */
464 for( i = 0; i < MAX_SERVERS; i++ ) {
465 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
467 /* This structure is in use ... */
468 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
469 /* Check for duplicates */
470 for( n = 0; n < MAX_SERVERS; n++ ) {
471 if( n == i ) continue;
473 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
474 Init_Server_Struct( &Conf_Server[n] );
476 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
482 /* Mark server as "once" */
483 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
484 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
489 /* Initialize variables */
491 strcpy( section, "" );
492 Init_Server_Struct( &New_Server );
493 New_Server_Idx = NONE;
495 /* Read configuration file */
497 if( ! fgets( str, LINE_LEN, fd )) break;
501 /* Skip comments and empty lines */
502 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
504 /* Is this the beginning of a new section? */
505 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
506 strlcpy( section, str, sizeof( section ));
507 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
510 if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
511 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
512 Config_Error( LOG_ERR, "Too many operators configured.");
514 /* Initialize new operator structure */
515 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
516 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
517 if (Conf_Oper[Conf_Oper_Count].mask) {
518 free(Conf_Oper[Conf_Oper_Count].mask );
519 Conf_Oper[Conf_Oper_Count].mask = NULL;
525 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
526 /* Check if there is already a server to add */
527 if( New_Server.name[0] ) {
528 /* Copy data to "real" server structure */
529 assert( New_Server_Idx > NONE );
530 Conf_Server[New_Server_Idx] = New_Server;
533 /* Re-init structure for new server */
534 Init_Server_Struct( &New_Server );
536 /* Search unused item in server configuration structure */
537 for( i = 0; i < MAX_SERVERS; i++ ) {
538 /* Is this item used? */
539 if( ! Conf_Server[i].name[0] ) break;
541 if( i >= MAX_SERVERS ) {
542 /* Oops, no free item found! */
543 Config_Error( LOG_ERR, "Too many servers configured." );
544 New_Server_Idx = NONE;
546 else New_Server_Idx = i;
549 if( strcasecmp( section, "[CHANNEL]" ) == 0 ) {
550 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) {
551 Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
553 /* Initialize new channel structure */
554 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
555 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
556 array_free(&Conf_Channel[Conf_Channel_Count].topic);
557 Conf_Channel_Count++;
561 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
564 if( section[0] == 0x1 ) continue;
566 /* Split line into variable name and parameters */
567 ptr = strchr( str, '=' );
569 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
573 var = str; ngt_TrimStr( var );
574 arg = ptr + 1; ngt_TrimStr( arg );
576 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
577 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
578 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
579 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
580 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
583 /* Close configuration file */
586 /* Check if there is still a server to add */
587 if( New_Server.name[0] ) {
588 /* Copy data to "real" server structure */
589 assert( New_Server_Idx > NONE );
590 Conf_Server[New_Server_Idx] = New_Server;
593 if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
594 if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
595 Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
596 (unsigned int) defaultport, strerror(errno));
604 Check_ArgIsTrue( const char *Arg )
606 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
607 if( strcasecmp( Arg, "true" ) == 0 ) return true;
608 if( atoi( Arg ) != 0 ) return true;
611 } /* Check_ArgIsTrue */
615 Handle_GLOBAL( int Line, char *Var, char *Arg )
622 assert( Var != NULL );
623 assert( Arg != NULL );
625 if( strcasecmp( Var, "Name" ) == 0 ) {
627 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
628 if (len >= sizeof( Conf_ServerName ))
629 Config_Error_TooLong( Line, Var );
632 if( strcasecmp( Var, "Info" ) == 0 ) {
633 /* Info text of server */
634 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
635 if (len >= sizeof( Conf_ServerInfo ))
636 Config_Error_TooLong ( Line, Var );
639 if( strcasecmp( Var, "Password" ) == 0 ) {
640 /* Global server password */
641 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
642 if (len >= sizeof( Conf_ServerPwd ))
643 Config_Error_TooLong( Line, Var );
646 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
647 /* Administrative info #1 */
648 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
649 if (len >= sizeof( Conf_ServerAdmin1 ))
650 Config_Error_TooLong ( Line, Var );
653 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
654 /* Administrative info #2 */
655 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
656 if (len >= sizeof( Conf_ServerAdmin2 ))
657 Config_Error_TooLong ( Line, Var );
660 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
661 /* Administrative email contact */
662 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
663 if (len >= sizeof( Conf_ServerAdminMail ))
664 Config_Error_TooLong( Line, Var );
668 if( strcasecmp( Var, "Ports" ) == 0 ) {
669 ports_parse(&Conf_ListenPorts, Line, Arg);
672 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
673 /* "Message of the day" (MOTD) file */
674 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
675 if (len >= sizeof( Conf_MotdFile ))
676 Config_Error_TooLong( Line, Var );
679 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
680 /* "Message of the day" phrase (instead of file) */
681 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
682 if (len >= sizeof( Conf_MotdPhrase ))
683 Config_Error_TooLong( Line, Var );
686 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
687 /* directory for chroot() */
688 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
689 if (len >= sizeof( Conf_Chroot ))
690 Config_Error_TooLong( Line, Var );
693 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
694 /* name of pidfile */
695 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
696 if (len >= sizeof( Conf_PidFile ))
697 Config_Error_TooLong( Line, Var );
700 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
701 /* UID the daemon should switch to */
702 pwd = getpwnam( Arg );
703 if( pwd ) Conf_UID = pwd->pw_uid;
706 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
709 Conf_UID = (unsigned int)atoi( Arg );
713 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
714 /* GID the daemon should use */
715 grp = getgrnam( Arg );
716 if( grp ) Conf_GID = grp->gr_gid;
719 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
722 Conf_GID = (unsigned int)atoi( Arg );
726 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
728 Conf_PingTimeout = atoi( Arg );
729 if( Conf_PingTimeout < 5 ) {
730 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
731 NGIRCd_ConfFile, Line );
732 Conf_PingTimeout = 5;
736 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
738 Conf_PongTimeout = atoi( Arg );
739 if( Conf_PongTimeout < 5 ) {
740 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
741 NGIRCd_ConfFile, Line );
742 Conf_PongTimeout = 5;
746 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
747 /* Seconds between connection attempts to other servers */
748 Conf_ConnectRetry = atoi( Arg );
749 if( Conf_ConnectRetry < 5 ) {
750 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
751 NGIRCd_ConfFile, Line );
752 Conf_ConnectRetry = 5;
756 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
757 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
758 Conf_OperCanMode = Check_ArgIsTrue( Arg );
761 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
762 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
763 Conf_OperServerMode = Check_ArgIsTrue( Arg );
766 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
767 /* Maximum number of connections. Values <= 0 are equal to "no limit". */
769 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
772 Conf_MaxConnections = atol( Arg );
775 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
776 /* Maximum number of simultaneous connections from one IP. Values <= 0 -> "no limit" */
778 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
781 Conf_MaxConnectionsIP = atoi( Arg );
784 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
785 /* Maximum number of channels a user can join. Values <= 0 are equal to "no limit". */
787 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
790 Conf_MaxJoins = atoi( Arg );
793 if( strcasecmp( Var, "Listen" ) == 0 ) {
794 /* IP-Address to bind sockets */
795 len = strlcpy( Conf_ListenAddress, Arg, sizeof( Conf_ListenAddress ));
796 if (len >= sizeof( Conf_ListenAddress ))
797 Config_Error_TooLong( Line, Var );
801 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
802 NGIRCd_ConfFile, Line, Var );
803 } /* Handle_GLOBAL */
807 Handle_OPERATOR( int Line, char *Var, char *Arg )
809 unsigned int opercount;
812 assert( Var != NULL );
813 assert( Arg != NULL );
814 assert( Conf_Oper_Count > 0 );
816 if ( Conf_Oper_Count == 0 )
819 opercount = Conf_Oper_Count - 1;
821 if( strcasecmp( Var, "Name" ) == 0 ) {
822 /* Name of IRC operator */
823 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
824 if (len >= sizeof( Conf_Oper[opercount].name ))
825 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_warn( Arg );
841 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
842 NGIRCd_ConfFile, Line, Var );
843 } /* Handle_OPERATOR */
847 Handle_SERVER( int Line, char *Var, char *Arg )
853 assert( Var != NULL );
854 assert( Arg != NULL );
856 /* Ignore server block if no space is left in server configuration structure */
857 if( New_Server_Idx <= NONE ) return;
859 if( strcasecmp( Var, "Host" ) == 0 ) {
860 /* Hostname of the server */
861 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
862 if (len >= sizeof( New_Server.host ))
863 Config_Error_TooLong ( Line, Var );
866 if( strcasecmp( Var, "Name" ) == 0 ) {
867 /* Name of the server ("Nick"/"ID") */
868 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
869 if (len >= sizeof( New_Server.name ))
870 Config_Error_TooLong( Line, Var );
873 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
874 /* Password of this server which is sent to the peer */
876 Config_Error(LOG_ERR,
877 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
878 NGIRCd_ConfFile, Line);
880 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
881 if (len >= sizeof( New_Server.pwd_in ))
882 Config_Error_TooLong( Line, Var );
885 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
886 /* Passwort of the peer which must be received */
887 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
888 if (len >= sizeof( New_Server.pwd_out ))
889 Config_Error_TooLong( Line, Var );
892 if( strcasecmp( Var, "Port" ) == 0 ) {
893 /* Port to which this server should connect */
895 if( port > 0 && port < 0xFFFF )
896 New_Server.port = (UINT16)port;
898 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
899 NGIRCd_ConfFile, Line, port );
902 if( strcasecmp( Var, "Group" ) == 0 ) {
905 if( ! isdigit( (int)*Arg ))
906 Config_Error_NaN( Line, Var );
909 New_Server.group = atoi( Arg );
913 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
914 NGIRCd_ConfFile, Line, Var );
915 } /* Handle_SERVER */
919 Handle_CHANNEL( int Line, char *Var, char *Arg )
922 size_t chancount = 0;
925 assert( Var != NULL );
926 assert( Arg != NULL );
927 if (Conf_Channel_Count > 0)
928 chancount = Conf_Channel_Count - 1;
930 if( strcasecmp( Var, "Name" ) == 0 ) {
931 /* Name of the channel */
932 len = strlcpy( Conf_Channel[chancount].name, Arg, sizeof( Conf_Channel[chancount].name ));
933 if (len >= sizeof( Conf_Channel[chancount].name ))
934 Config_Error_TooLong( Line, Var );
937 if( strcasecmp( Var, "Modes" ) == 0 ) {
939 len = strlcpy( Conf_Channel[chancount].modes, Arg, sizeof( Conf_Channel[chancount].modes ));
940 if (len >= sizeof( Conf_Channel[chancount].modes ))
941 Config_Error_TooLong( Line, Var );
944 if( strcasecmp( Var, "Topic" ) == 0 ) {
946 if (!array_copys( &Conf_Channel[chancount].topic, Arg))
947 Config_Error_TooLong( Line, Var );
951 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
952 NGIRCd_ConfFile, Line, Var );
953 } /* Handle_CHANNEL */
957 Validate_Config(bool Configtest, bool Rehash)
959 /* Validate configuration settings. */
962 int i, servers, servers_once;
966 /* Validate configured server name, see RFC 2812 section 2.3.1 */
967 ptr = Conf_ServerName;
969 if (*ptr >= 'a' && *ptr <= 'z') continue;
970 if (*ptr >= 'A' && *ptr <= 'Z') continue;
971 if (*ptr >= '1' && *ptr <= '0') continue;
972 if (ptr > Conf_ServerName) {
973 if (*ptr == '.' || *ptr == '-')
976 Conf_ServerName[0] = '\0';
980 if (!Conf_ServerName[0]) {
981 /* No server name configured! */
982 Config_Error(LOG_ALERT,
983 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
985 if (!Configtest && !Rehash) {
986 Config_Error(LOG_ALERT,
987 "%s exiting due to fatal errors!",
993 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
994 /* No dot in server name! */
995 Config_Error(LOG_ALERT,
996 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
999 Config_Error(LOG_ALERT,
1000 "%s exiting due to fatal errors!",
1007 if (!Conf_ServerAdminMail[0]) {
1008 /* No administrative contact configured! */
1009 Config_Error(LOG_ALERT,
1010 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1013 Config_Error(LOG_ALERT,
1014 "%s exiting due to fatal errors!",
1021 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1022 && !Conf_ServerAdminMail[0]) {
1023 /* No administrative information configured! */
1024 Config_Error(LOG_WARNING,
1025 "No administrative information configured but required by RFC!");
1029 servers = servers_once = 0;
1030 for (i = 0; i < MAX_SERVERS; i++) {
1031 if (Conf_Server[i].name[0]) {
1033 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1038 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1039 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1041 } /* Validate_Config */
1045 Config_Error_TooLong ( const int Line, const char *Item )
1047 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1052 Config_Error_NaN( const int Line, const char *Item )
1054 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1055 NGIRCd_ConfFile, Line, Item );
1060 static void Config_Error( const int Level, const char *Format, ... )
1062 static void Config_Error( Level, Format, va_alist )
1068 /* Error! Write to console and/or logfile. */
1070 char msg[MAX_LOG_MSG_LEN];
1073 assert( Format != NULL );
1076 va_start( ap, Format );
1080 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1083 /* During "normal operations" the log functions of the daemon should
1084 * be used, but during testing of the configuration file, all messages
1085 * should go directly to the console: */
1086 if (Use_Log) Log( Level, "%s", msg );
1088 } /* Config_Error */
1092 Init_Server_Struct( CONF_SERVER *Server )
1094 /* Initialize server configuration structur to default values */
1096 assert( Server != NULL );
1098 memset( Server, 0, sizeof (CONF_SERVER) );
1100 Server->group = NONE;
1101 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1103 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1105 Resolve_Init(&Server->res_stat);
1106 Server->conn_id = NONE;
1107 } /* Init_Server_Struct */