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.96 2006/11/20 19:32:07 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( " MaxConnections = %ld\n", Conf_MaxConnections>0 ? Conf_MaxConnections : -1);
209 printf( " MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP>0 ? Conf_MaxConnectionsIP : -1);
210 printf( " MaxJoins = %d\n\n", Conf_MaxJoins>0 ? Conf_MaxJoins : -1);
212 for( i = 0; i < Conf_Oper_Count; i++ ) {
213 if( ! Conf_Oper[i].name[0] ) continue;
215 /* Valid "Operator" section */
216 puts( "[OPERATOR]" );
217 printf( " Name = %s\n", Conf_Oper[i].name );
218 printf( " Password = %s\n", Conf_Oper[i].pwd );
219 if ( Conf_Oper[i].mask ) printf( " Mask = %s\n", Conf_Oper[i].mask );
223 for( i = 0; i < MAX_SERVERS; i++ ) {
224 if( ! Conf_Server[i].name[0] ) continue;
226 /* Valid "Server" section */
228 printf( " Name = %s\n", Conf_Server[i].name );
229 printf( " Host = %s\n", Conf_Server[i].host );
230 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
231 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
232 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
233 printf( " Group = %d\n\n", Conf_Server[i].group );
236 for( i = 0; i < Conf_Channel_Count; i++ ) {
237 if( ! Conf_Channel[i].name[0] ) continue;
239 /* Valid "Channel" section */
241 printf( " Name = %s\n", Conf_Channel[i].name );
242 printf( " Modes = %s\n", Conf_Channel[i].modes );
244 topic = (char*)array_start(&Conf_Channel[i].topic);
245 printf( " Topic = %s\n\n", topic ? topic : "");
253 Conf_UnsetServer( CONN_ID Idx )
255 /* Set next time for next connection attempt, if this is a server
256 * link that is (still) configured here. If the server is set as
257 * "once", delete it from our configuration.
258 * Non-Server-Connections will be silently ignored. */
263 /* Check all our configured servers */
264 for( i = 0; i < MAX_SERVERS; i++ ) {
265 if( Conf_Server[i].conn_id != Idx ) continue;
267 /* Gotcha! Mark server configuration as "unused": */
268 Conf_Server[i].conn_id = NONE;
270 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
271 /* Delete configuration here */
272 Init_Server_Struct( &Conf_Server[i] );
274 /* Set time for next connect attempt */
276 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
277 /* The connection has been "long", so we don't
278 * require the next attempt to be delayed. */
279 Conf_Server[i].lasttry =
280 t - Conf_ConnectRetry + RECONNECT_DELAY;
282 Conf_Server[i].lasttry = t;
285 } /* Conf_UnsetServer */
289 Conf_SetServer( int ConfServer, CONN_ID Idx )
291 /* Set connection for specified configured server */
293 assert( ConfServer > NONE );
294 assert( Idx > NONE );
296 Conf_Server[ConfServer].conn_id = Idx;
297 } /* Conf_SetServer */
301 Conf_GetServer( CONN_ID Idx )
303 /* Get index of server in configuration structure */
307 assert( Idx > NONE );
309 for( i = 0; i < MAX_SERVERS; i++ ) {
310 if( Conf_Server[i].conn_id == Idx ) return i;
313 } /* Conf_GetServer */
317 Conf_EnableServer( char *Name, UINT16 Port )
319 /* Enable specified server and adjust port */
323 assert( Name != NULL );
325 for( i = 0; i < MAX_SERVERS; i++ ) {
326 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
327 /* Gotcha! Set port and enable server: */
328 Conf_Server[i].port = Port;
329 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
334 } /* Conf_EnableServer */
338 Conf_DisableServer( char *Name )
340 /* Enable specified server and adjust port */
344 assert( Name != NULL );
346 for( i = 0; i < MAX_SERVERS; i++ ) {
347 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
348 /* Gotcha! Disable and disconnect server: */
349 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
350 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
355 } /* Conf_DisableServer */
359 Conf_AddServer( char *Name, UINT16 Port, char *Host, char *MyPwd, char *PeerPwd )
361 /* Add new server to configuration */
365 assert( Name != NULL );
366 assert( Host != NULL );
367 assert( MyPwd != NULL );
368 assert( PeerPwd != NULL );
370 /* Search unused item in server configuration structure */
371 for( i = 0; i < MAX_SERVERS; i++ ) {
372 /* Is this item used? */
373 if( ! Conf_Server[i].name[0] ) break;
375 if( i >= MAX_SERVERS ) return false;
377 Init_Server_Struct( &Conf_Server[i] );
378 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
379 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
380 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
381 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
382 Conf_Server[i].port = Port;
383 Conf_Server[i].flags = CONF_SFLAG_ONCE;
386 } /* Conf_AddServer */
390 Set_Defaults( bool InitServers )
392 /* Initialize configuration variables with default values. */
396 strcpy( Conf_ServerName, "" );
397 snprintf( Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
398 strcpy( Conf_ServerPwd, "" );
400 strcpy( Conf_ServerAdmin1, "" );
401 strcpy( Conf_ServerAdmin2, "" );
402 strcpy( Conf_ServerAdminMail, "" );
404 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
405 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
407 strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
409 strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
411 strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
413 strcpy( Conf_ListenAddress, "" );
415 Conf_UID = Conf_GID = 0;
417 Conf_PingTimeout = 120;
418 Conf_PongTimeout = 20;
420 Conf_ConnectRetry = 60;
423 Conf_Channel_Count = 0;
425 Conf_OperCanMode = false;
426 Conf_PredefChannelsOnly = false;
427 Conf_OperServerMode = false;
429 Conf_MaxConnections = -1;
430 Conf_MaxConnectionsIP = 5;
433 /* Initialize server configuration structures */
434 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
441 /* Read configuration file. */
443 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
444 const UINT16 defaultport = 6667;
448 /* Open configuration file */
449 fd = fopen( NGIRCd_ConfFile, "r" );
451 /* No configuration file found! */
452 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
453 NGIRCd_ConfFile, strerror( errno ));
454 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
458 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
460 /* Clean up server configuration structure: mark all already
461 * configured servers as "once" so that they are deleted
462 * after the next disconnect and delete all unused servers.
463 * And delete all servers which are "duplicates" of servers
464 * that are already marked as "once" (such servers have been
465 * created by the last rehash but are now useless). */
466 for( i = 0; i < MAX_SERVERS; i++ ) {
467 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
469 /* This structure is in use ... */
470 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
471 /* Check for duplicates */
472 for( n = 0; n < MAX_SERVERS; n++ ) {
473 if( n == i ) continue;
475 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
476 Init_Server_Struct( &Conf_Server[n] );
478 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
484 /* Mark server as "once" */
485 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
486 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
491 /* Initialize variables */
493 strcpy( section, "" );
494 Init_Server_Struct( &New_Server );
495 New_Server_Idx = NONE;
497 /* Read configuration file */
499 if( ! fgets( str, LINE_LEN, fd )) break;
503 /* Skip comments and empty lines */
504 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
506 /* Is this the beginning of a new section? */
507 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
508 strlcpy( section, str, sizeof( section ));
509 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
512 if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
513 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
514 Config_Error( LOG_ERR, "Too many operators configured.");
516 /* Initialize new operator structure */
517 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
518 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
519 if (Conf_Oper[Conf_Oper_Count].mask) {
520 free(Conf_Oper[Conf_Oper_Count].mask );
521 Conf_Oper[Conf_Oper_Count].mask = NULL;
527 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
528 /* Check if there is already a server to add */
529 if( New_Server.name[0] ) {
530 /* Copy data to "real" server structure */
531 assert( New_Server_Idx > NONE );
532 Conf_Server[New_Server_Idx] = New_Server;
535 /* Re-init structure for new server */
536 Init_Server_Struct( &New_Server );
538 /* Search unused item in server configuration structure */
539 for( i = 0; i < MAX_SERVERS; i++ ) {
540 /* Is this item used? */
541 if( ! Conf_Server[i].name[0] ) break;
543 if( i >= MAX_SERVERS ) {
544 /* Oops, no free item found! */
545 Config_Error( LOG_ERR, "Too many servers configured." );
546 New_Server_Idx = NONE;
548 else New_Server_Idx = i;
551 if( strcasecmp( section, "[CHANNEL]" ) == 0 ) {
552 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) {
553 Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
555 /* Initialize new channel structure */
556 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
557 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
558 array_free(&Conf_Channel[Conf_Channel_Count].topic);
559 Conf_Channel_Count++;
563 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
566 if( section[0] == 0x1 ) continue;
568 /* Split line into variable name and parameters */
569 ptr = strchr( str, '=' );
571 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
575 var = str; ngt_TrimStr( var );
576 arg = ptr + 1; ngt_TrimStr( arg );
578 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
579 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
580 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
581 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
582 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
585 /* Close configuration file */
588 /* Check if there is still a server to add */
589 if( New_Server.name[0] ) {
590 /* Copy data to "real" server structure */
591 assert( New_Server_Idx > NONE );
592 Conf_Server[New_Server_Idx] = New_Server;
595 if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
596 if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
597 Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
598 (unsigned int) defaultport, strerror(errno));
606 Check_ArgIsTrue( const char *Arg )
608 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
609 if( strcasecmp( Arg, "true" ) == 0 ) return true;
610 if( atoi( Arg ) != 0 ) return true;
613 } /* Check_ArgIsTrue */
617 Handle_GLOBAL( int Line, char *Var, char *Arg )
624 assert( Var != NULL );
625 assert( Arg != NULL );
627 if( strcasecmp( Var, "Name" ) == 0 ) {
629 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
630 if (len >= sizeof( Conf_ServerName ))
631 Config_Error_TooLong( Line, Var );
634 if( strcasecmp( Var, "Info" ) == 0 ) {
635 /* Info text of server */
636 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
637 if (len >= sizeof( Conf_ServerInfo ))
638 Config_Error_TooLong ( Line, Var );
641 if( strcasecmp( Var, "Password" ) == 0 ) {
642 /* Global server password */
643 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
644 if (len >= sizeof( Conf_ServerPwd ))
645 Config_Error_TooLong( Line, Var );
648 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
649 /* Administrative info #1 */
650 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
651 if (len >= sizeof( Conf_ServerAdmin1 ))
652 Config_Error_TooLong ( Line, Var );
655 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
656 /* Administrative info #2 */
657 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
658 if (len >= sizeof( Conf_ServerAdmin2 ))
659 Config_Error_TooLong ( Line, Var );
662 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
663 /* Administrative email contact */
664 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
665 if (len >= sizeof( Conf_ServerAdminMail ))
666 Config_Error_TooLong( Line, Var );
670 if( strcasecmp( Var, "Ports" ) == 0 ) {
671 ports_parse(&Conf_ListenPorts, Line, Arg);
674 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
675 /* "Message of the day" (MOTD) file */
676 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
677 if (len >= sizeof( Conf_MotdFile ))
678 Config_Error_TooLong( Line, Var );
681 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
682 /* "Message of the day" phrase (instead of file) */
683 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
684 if (len >= sizeof( Conf_MotdPhrase ))
685 Config_Error_TooLong( Line, Var );
688 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
689 /* directory for chroot() */
690 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
691 if (len >= sizeof( Conf_Chroot ))
692 Config_Error_TooLong( Line, Var );
695 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
696 /* name of pidfile */
697 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
698 if (len >= sizeof( Conf_PidFile ))
699 Config_Error_TooLong( Line, Var );
702 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
703 /* UID the daemon should switch to */
704 pwd = getpwnam( Arg );
705 if( pwd ) Conf_UID = pwd->pw_uid;
708 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
711 Conf_UID = (unsigned int)atoi( Arg );
715 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
716 /* GID the daemon should use */
717 grp = getgrnam( Arg );
718 if( grp ) Conf_GID = grp->gr_gid;
721 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
724 Conf_GID = (unsigned int)atoi( Arg );
728 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
730 Conf_PingTimeout = atoi( Arg );
731 if( Conf_PingTimeout < 5 ) {
732 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
733 NGIRCd_ConfFile, Line );
734 Conf_PingTimeout = 5;
738 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
740 Conf_PongTimeout = atoi( Arg );
741 if( Conf_PongTimeout < 5 ) {
742 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
743 NGIRCd_ConfFile, Line );
744 Conf_PongTimeout = 5;
748 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
749 /* Seconds between connection attempts to other servers */
750 Conf_ConnectRetry = atoi( Arg );
751 if( Conf_ConnectRetry < 5 ) {
752 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
753 NGIRCd_ConfFile, Line );
754 Conf_ConnectRetry = 5;
758 if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
759 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
760 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
763 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
764 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
765 Conf_OperCanMode = Check_ArgIsTrue( Arg );
768 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
769 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
770 Conf_OperServerMode = Check_ArgIsTrue( Arg );
773 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
774 /* Maximum number of connections. Values <= 0 are equal to "no limit". */
776 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
779 Conf_MaxConnections = atol( Arg );
782 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
783 /* Maximum number of simultaneous connections from one IP. Values <= 0 -> "no limit" */
785 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
788 Conf_MaxConnectionsIP = atoi( Arg );
791 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
792 /* Maximum number of channels a user can join. Values <= 0 are equal to "no limit". */
794 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
797 Conf_MaxJoins = atoi( Arg );
800 if( strcasecmp( Var, "Listen" ) == 0 ) {
801 /* IP-Address to bind sockets */
802 len = strlcpy( Conf_ListenAddress, Arg, sizeof( Conf_ListenAddress ));
803 if (len >= sizeof( Conf_ListenAddress ))
804 Config_Error_TooLong( Line, Var );
808 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
809 NGIRCd_ConfFile, Line, Var );
810 } /* Handle_GLOBAL */
814 Handle_OPERATOR( int Line, char *Var, char *Arg )
816 unsigned int opercount;
819 assert( Var != NULL );
820 assert( Arg != NULL );
821 assert( Conf_Oper_Count > 0 );
823 if ( Conf_Oper_Count == 0 )
826 opercount = Conf_Oper_Count - 1;
828 if( strcasecmp( Var, "Name" ) == 0 ) {
829 /* Name of IRC operator */
830 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
831 if (len >= sizeof( Conf_Oper[opercount].name ))
832 Config_Error_TooLong( Line, Var );
835 if( strcasecmp( Var, "Password" ) == 0 ) {
836 /* Password of IRC operator */
837 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
838 if (len >= sizeof( Conf_Oper[opercount].pwd ))
839 Config_Error_TooLong( Line, Var );
842 if( strcasecmp( Var, "Mask" ) == 0 ) {
843 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
845 Conf_Oper[opercount].mask = strdup_warn( Arg );
848 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
849 NGIRCd_ConfFile, Line, Var );
850 } /* Handle_OPERATOR */
854 Handle_SERVER( int Line, char *Var, char *Arg )
860 assert( Var != NULL );
861 assert( Arg != NULL );
863 /* Ignore server block if no space is left in server configuration structure */
864 if( New_Server_Idx <= NONE ) return;
866 if( strcasecmp( Var, "Host" ) == 0 ) {
867 /* Hostname of the server */
868 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
869 if (len >= sizeof( New_Server.host ))
870 Config_Error_TooLong ( Line, Var );
873 if( strcasecmp( Var, "Name" ) == 0 ) {
874 /* Name of the server ("Nick"/"ID") */
875 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
876 if (len >= sizeof( New_Server.name ))
877 Config_Error_TooLong( Line, Var );
880 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
881 /* Password of this server which is sent to the peer */
883 Config_Error(LOG_ERR,
884 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
885 NGIRCd_ConfFile, Line);
887 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
888 if (len >= sizeof( New_Server.pwd_in ))
889 Config_Error_TooLong( Line, Var );
892 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
893 /* Passwort of the peer which must be received */
894 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
895 if (len >= sizeof( New_Server.pwd_out ))
896 Config_Error_TooLong( Line, Var );
899 if( strcasecmp( Var, "Port" ) == 0 ) {
900 /* Port to which this server should connect */
902 if( port > 0 && port < 0xFFFF )
903 New_Server.port = (UINT16)port;
905 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
906 NGIRCd_ConfFile, Line, port );
909 if( strcasecmp( Var, "Group" ) == 0 ) {
912 if( ! isdigit( (int)*Arg ))
913 Config_Error_NaN( Line, Var );
916 New_Server.group = atoi( Arg );
920 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
921 NGIRCd_ConfFile, Line, Var );
922 } /* Handle_SERVER */
926 Handle_Channelname(size_t chancount, const char *name)
928 size_t size = sizeof( Conf_Channel[chancount].name );
929 char *dest = Conf_Channel[chancount].name;
931 if (*name && *name != '#') {
936 return size > strlcpy(dest, name, size);
941 Handle_CHANNEL( int Line, char *Var, char *Arg )
944 size_t chancount = 0;
947 assert( Var != NULL );
948 assert( Arg != NULL );
949 if (Conf_Channel_Count > 0)
950 chancount = Conf_Channel_Count - 1;
952 if( strcasecmp( Var, "Name" ) == 0 ) {
953 if (!Handle_Channelname(chancount, Arg))
954 Config_Error_TooLong( Line, Var );
957 if( strcasecmp( Var, "Modes" ) == 0 ) {
959 len = strlcpy( Conf_Channel[chancount].modes, Arg, sizeof( Conf_Channel[chancount].modes ));
960 if (len >= sizeof( Conf_Channel[chancount].modes ))
961 Config_Error_TooLong( Line, Var );
964 if( strcasecmp( Var, "Topic" ) == 0 ) {
966 if (!array_copys( &Conf_Channel[chancount].topic, Arg))
967 Config_Error_TooLong( Line, Var );
971 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
972 NGIRCd_ConfFile, Line, Var );
973 } /* Handle_CHANNEL */
977 Validate_Config(bool Configtest, bool Rehash)
979 /* Validate configuration settings. */
982 int i, servers, servers_once;
986 /* Validate configured server name, see RFC 2812 section 2.3.1 */
987 ptr = Conf_ServerName;
989 if (*ptr >= 'a' && *ptr <= 'z') continue;
990 if (*ptr >= 'A' && *ptr <= 'Z') continue;
991 if (*ptr >= '0' && *ptr <= '9') continue;
992 if (ptr > Conf_ServerName) {
993 if (*ptr == '.' || *ptr == '-')
996 Conf_ServerName[0] = '\0';
1000 if (!Conf_ServerName[0]) {
1001 /* No server name configured! */
1002 Config_Error(LOG_ALERT,
1003 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1005 if (!Configtest && !Rehash) {
1006 Config_Error(LOG_ALERT,
1007 "%s exiting due to fatal errors!",
1013 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1014 /* No dot in server name! */
1015 Config_Error(LOG_ALERT,
1016 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1019 Config_Error(LOG_ALERT,
1020 "%s exiting due to fatal errors!",
1027 if (!Conf_ServerAdminMail[0]) {
1028 /* No administrative contact configured! */
1029 Config_Error(LOG_ALERT,
1030 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1033 Config_Error(LOG_ALERT,
1034 "%s exiting due to fatal errors!",
1041 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1042 && !Conf_ServerAdminMail[0]) {
1043 /* No administrative information configured! */
1044 Config_Error(LOG_WARNING,
1045 "No administrative information configured but required by RFC!");
1049 servers = servers_once = 0;
1050 for (i = 0; i < MAX_SERVERS; i++) {
1051 if (Conf_Server[i].name[0]) {
1053 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1058 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1059 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1061 } /* Validate_Config */
1065 Config_Error_TooLong ( const int Line, const char *Item )
1067 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1072 Config_Error_NaN( const int Line, const char *Item )
1074 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1075 NGIRCd_ConfFile, Line, Item );
1080 static void Config_Error( const int Level, const char *Format, ... )
1082 static void Config_Error( Level, Format, va_alist )
1088 /* Error! Write to console and/or logfile. */
1090 char msg[MAX_LOG_MSG_LEN];
1093 assert( Format != NULL );
1096 va_start( ap, Format );
1100 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1103 /* During "normal operations" the log functions of the daemon should
1104 * be used, but during testing of the configuration file, all messages
1105 * should go directly to the console: */
1106 if (Use_Log) Log( Level, "%s", msg );
1108 } /* Config_Error */
1112 Init_Server_Struct( CONF_SERVER *Server )
1114 /* Initialize server configuration structur to default values */
1116 assert( Server != NULL );
1118 memset( Server, 0, sizeof (CONF_SERVER) );
1120 Server->group = NONE;
1121 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1123 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1125 Resolve_Init(&Server->res_stat);
1126 Server->conn_id = NONE;
1127 } /* Init_Server_Struct */