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.97 2006/12/29 14:09:50 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 );
243 printf( " Key = %s\n", Conf_Channel[i].key );
244 printf( " MaxUsers = %lu\n", Conf_Channel[i].maxusers );
246 topic = (char*)array_start(&Conf_Channel[i].topic);
247 printf( " Topic = %s\n\n", topic ? topic : "");
255 Conf_UnsetServer( CONN_ID Idx )
257 /* Set next time for next connection attempt, if this is a server
258 * link that is (still) configured here. If the server is set as
259 * "once", delete it from our configuration.
260 * Non-Server-Connections will be silently ignored. */
265 /* Check all our configured servers */
266 for( i = 0; i < MAX_SERVERS; i++ ) {
267 if( Conf_Server[i].conn_id != Idx ) continue;
269 /* Gotcha! Mark server configuration as "unused": */
270 Conf_Server[i].conn_id = NONE;
272 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
273 /* Delete configuration here */
274 Init_Server_Struct( &Conf_Server[i] );
276 /* Set time for next connect attempt */
278 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
279 /* The connection has been "long", so we don't
280 * require the next attempt to be delayed. */
281 Conf_Server[i].lasttry =
282 t - Conf_ConnectRetry + RECONNECT_DELAY;
284 Conf_Server[i].lasttry = t;
287 } /* Conf_UnsetServer */
291 Conf_SetServer( int ConfServer, CONN_ID Idx )
293 /* Set connection for specified configured server */
295 assert( ConfServer > NONE );
296 assert( Idx > NONE );
298 Conf_Server[ConfServer].conn_id = Idx;
299 } /* Conf_SetServer */
303 Conf_GetServer( CONN_ID Idx )
305 /* Get index of server in configuration structure */
309 assert( Idx > NONE );
311 for( i = 0; i < MAX_SERVERS; i++ ) {
312 if( Conf_Server[i].conn_id == Idx ) return i;
315 } /* Conf_GetServer */
319 Conf_EnableServer( char *Name, UINT16 Port )
321 /* Enable specified server and adjust port */
325 assert( Name != NULL );
327 for( i = 0; i < MAX_SERVERS; i++ ) {
328 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
329 /* Gotcha! Set port and enable server: */
330 Conf_Server[i].port = Port;
331 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
336 } /* Conf_EnableServer */
340 Conf_DisableServer( char *Name )
342 /* Enable specified server and adjust port */
346 assert( Name != NULL );
348 for( i = 0; i < MAX_SERVERS; i++ ) {
349 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
350 /* Gotcha! Disable and disconnect server: */
351 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
352 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
357 } /* Conf_DisableServer */
361 Conf_AddServer( char *Name, UINT16 Port, char *Host, char *MyPwd, char *PeerPwd )
363 /* Add new server to configuration */
367 assert( Name != NULL );
368 assert( Host != NULL );
369 assert( MyPwd != NULL );
370 assert( PeerPwd != NULL );
372 /* Search unused item in server configuration structure */
373 for( i = 0; i < MAX_SERVERS; i++ ) {
374 /* Is this item used? */
375 if( ! Conf_Server[i].name[0] ) break;
377 if( i >= MAX_SERVERS ) return false;
379 Init_Server_Struct( &Conf_Server[i] );
380 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
381 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
382 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
383 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
384 Conf_Server[i].port = Port;
385 Conf_Server[i].flags = CONF_SFLAG_ONCE;
388 } /* Conf_AddServer */
392 Set_Defaults( bool InitServers )
394 /* Initialize configuration variables with default values. */
398 strcpy( Conf_ServerName, "" );
399 snprintf( Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
400 strcpy( Conf_ServerPwd, "" );
402 strcpy( Conf_ServerAdmin1, "" );
403 strcpy( Conf_ServerAdmin2, "" );
404 strcpy( Conf_ServerAdminMail, "" );
406 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
407 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
409 strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
411 strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
413 strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
415 strcpy( Conf_ListenAddress, "" );
417 Conf_UID = Conf_GID = 0;
419 Conf_PingTimeout = 120;
420 Conf_PongTimeout = 20;
422 Conf_ConnectRetry = 60;
425 Conf_Channel_Count = 0;
427 Conf_OperCanMode = false;
428 Conf_PredefChannelsOnly = false;
429 Conf_OperServerMode = false;
431 Conf_MaxConnections = -1;
432 Conf_MaxConnectionsIP = 5;
435 /* Initialize server configuration structures */
436 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
443 /* Read configuration file. */
445 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
446 const UINT16 defaultport = 6667;
450 /* Open configuration file */
451 fd = fopen( NGIRCd_ConfFile, "r" );
453 /* No configuration file found! */
454 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
455 NGIRCd_ConfFile, strerror( errno ));
456 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
460 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
462 /* Clean up server configuration structure: mark all already
463 * configured servers as "once" so that they are deleted
464 * after the next disconnect and delete all unused servers.
465 * And delete all servers which are "duplicates" of servers
466 * that are already marked as "once" (such servers have been
467 * created by the last rehash but are now useless). */
468 for( i = 0; i < MAX_SERVERS; i++ ) {
469 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
471 /* This structure is in use ... */
472 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
473 /* Check for duplicates */
474 for( n = 0; n < MAX_SERVERS; n++ ) {
475 if( n == i ) continue;
477 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
478 Init_Server_Struct( &Conf_Server[n] );
480 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
486 /* Mark server as "once" */
487 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
488 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
493 /* Initialize variables */
495 strcpy( section, "" );
496 Init_Server_Struct( &New_Server );
497 New_Server_Idx = NONE;
499 /* Read configuration file */
501 if( ! fgets( str, LINE_LEN, fd )) break;
505 /* Skip comments and empty lines */
506 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
508 /* Is this the beginning of a new section? */
509 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
510 strlcpy( section, str, sizeof( section ));
511 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
514 if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
515 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
516 Config_Error( LOG_ERR, "Too many operators configured.");
518 /* Initialize new operator structure */
519 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
520 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
521 if (Conf_Oper[Conf_Oper_Count].mask) {
522 free(Conf_Oper[Conf_Oper_Count].mask );
523 Conf_Oper[Conf_Oper_Count].mask = NULL;
529 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
530 /* Check if there is already a server to add */
531 if( New_Server.name[0] ) {
532 /* Copy data to "real" server structure */
533 assert( New_Server_Idx > NONE );
534 Conf_Server[New_Server_Idx] = New_Server;
537 /* Re-init structure for new server */
538 Init_Server_Struct( &New_Server );
540 /* Search unused item in server configuration structure */
541 for( i = 0; i < MAX_SERVERS; i++ ) {
542 /* Is this item used? */
543 if( ! Conf_Server[i].name[0] ) break;
545 if( i >= MAX_SERVERS ) {
546 /* Oops, no free item found! */
547 Config_Error( LOG_ERR, "Too many servers configured." );
548 New_Server_Idx = NONE;
550 else New_Server_Idx = i;
553 if( strcasecmp( section, "[CHANNEL]" ) == 0 ) {
554 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) {
555 Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
557 /* Initialize new channel structure */
558 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
559 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
560 strcpy( Conf_Channel[Conf_Channel_Count].key, "" );
561 Conf_Channel[Conf_Channel_Count].maxusers = 0;
562 array_free(&Conf_Channel[Conf_Channel_Count].topic);
563 Conf_Channel_Count++;
567 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
570 if( section[0] == 0x1 ) continue;
572 /* Split line into variable name and parameters */
573 ptr = strchr( str, '=' );
575 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
579 var = str; ngt_TrimStr( var );
580 arg = ptr + 1; ngt_TrimStr( arg );
582 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
583 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
584 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
585 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
586 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
589 /* Close configuration file */
592 /* Check if there is still a server to add */
593 if( New_Server.name[0] ) {
594 /* Copy data to "real" server structure */
595 assert( New_Server_Idx > NONE );
596 Conf_Server[New_Server_Idx] = New_Server;
599 if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
600 if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
601 Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
602 (unsigned int) defaultport, strerror(errno));
610 Check_ArgIsTrue( const char *Arg )
612 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
613 if( strcasecmp( Arg, "true" ) == 0 ) return true;
614 if( atoi( Arg ) != 0 ) return true;
617 } /* Check_ArgIsTrue */
621 Handle_GLOBAL( int Line, char *Var, char *Arg )
628 assert( Var != NULL );
629 assert( Arg != NULL );
631 if( strcasecmp( Var, "Name" ) == 0 ) {
633 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
634 if (len >= sizeof( Conf_ServerName ))
635 Config_Error_TooLong( Line, Var );
638 if( strcasecmp( Var, "Info" ) == 0 ) {
639 /* Info text of server */
640 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
641 if (len >= sizeof( Conf_ServerInfo ))
642 Config_Error_TooLong ( Line, Var );
645 if( strcasecmp( Var, "Password" ) == 0 ) {
646 /* Global server password */
647 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
648 if (len >= sizeof( Conf_ServerPwd ))
649 Config_Error_TooLong( Line, Var );
652 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
653 /* Administrative info #1 */
654 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
655 if (len >= sizeof( Conf_ServerAdmin1 ))
656 Config_Error_TooLong ( Line, Var );
659 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
660 /* Administrative info #2 */
661 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
662 if (len >= sizeof( Conf_ServerAdmin2 ))
663 Config_Error_TooLong ( Line, Var );
666 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
667 /* Administrative email contact */
668 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
669 if (len >= sizeof( Conf_ServerAdminMail ))
670 Config_Error_TooLong( Line, Var );
674 if( strcasecmp( Var, "Ports" ) == 0 ) {
675 ports_parse(&Conf_ListenPorts, Line, Arg);
678 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
679 /* "Message of the day" (MOTD) file */
680 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
681 if (len >= sizeof( Conf_MotdFile ))
682 Config_Error_TooLong( Line, Var );
685 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
686 /* "Message of the day" phrase (instead of file) */
687 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
688 if (len >= sizeof( Conf_MotdPhrase ))
689 Config_Error_TooLong( Line, Var );
692 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
693 /* directory for chroot() */
694 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
695 if (len >= sizeof( Conf_Chroot ))
696 Config_Error_TooLong( Line, Var );
699 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
700 /* name of pidfile */
701 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
702 if (len >= sizeof( Conf_PidFile ))
703 Config_Error_TooLong( Line, Var );
706 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
707 /* UID the daemon should switch to */
708 pwd = getpwnam( Arg );
709 if( pwd ) Conf_UID = pwd->pw_uid;
712 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
715 Conf_UID = (unsigned int)atoi( Arg );
719 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
720 /* GID the daemon should use */
721 grp = getgrnam( Arg );
722 if( grp ) Conf_GID = grp->gr_gid;
725 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
728 Conf_GID = (unsigned int)atoi( Arg );
732 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
734 Conf_PingTimeout = atoi( Arg );
735 if( Conf_PingTimeout < 5 ) {
736 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
737 NGIRCd_ConfFile, Line );
738 Conf_PingTimeout = 5;
742 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
744 Conf_PongTimeout = atoi( Arg );
745 if( Conf_PongTimeout < 5 ) {
746 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
747 NGIRCd_ConfFile, Line );
748 Conf_PongTimeout = 5;
752 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
753 /* Seconds between connection attempts to other servers */
754 Conf_ConnectRetry = atoi( Arg );
755 if( Conf_ConnectRetry < 5 ) {
756 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
757 NGIRCd_ConfFile, Line );
758 Conf_ConnectRetry = 5;
762 if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
763 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
764 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
767 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
768 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
769 Conf_OperCanMode = Check_ArgIsTrue( Arg );
772 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
773 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
774 Conf_OperServerMode = Check_ArgIsTrue( Arg );
777 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
778 /* Maximum number of connections. Values <= 0 are equal to "no limit". */
780 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
783 Conf_MaxConnections = atol( Arg );
786 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
787 /* Maximum number of simultaneous connections from one IP. Values <= 0 -> "no limit" */
789 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
792 Conf_MaxConnectionsIP = atoi( Arg );
795 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
796 /* Maximum number of channels a user can join. Values <= 0 are equal to "no limit". */
798 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
801 Conf_MaxJoins = atoi( Arg );
804 if( strcasecmp( Var, "Listen" ) == 0 ) {
805 /* IP-Address to bind sockets */
806 len = strlcpy( Conf_ListenAddress, Arg, sizeof( Conf_ListenAddress ));
807 if (len >= sizeof( Conf_ListenAddress ))
808 Config_Error_TooLong( Line, Var );
812 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
813 NGIRCd_ConfFile, Line, Var );
814 } /* Handle_GLOBAL */
818 Handle_OPERATOR( int Line, char *Var, char *Arg )
820 unsigned int opercount;
823 assert( Var != NULL );
824 assert( Arg != NULL );
825 assert( Conf_Oper_Count > 0 );
827 if ( Conf_Oper_Count == 0 )
830 opercount = Conf_Oper_Count - 1;
832 if( strcasecmp( Var, "Name" ) == 0 ) {
833 /* Name of IRC operator */
834 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
835 if (len >= sizeof( Conf_Oper[opercount].name ))
836 Config_Error_TooLong( Line, Var );
839 if( strcasecmp( Var, "Password" ) == 0 ) {
840 /* Password of IRC operator */
841 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
842 if (len >= sizeof( Conf_Oper[opercount].pwd ))
843 Config_Error_TooLong( Line, Var );
846 if( strcasecmp( Var, "Mask" ) == 0 ) {
847 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
849 Conf_Oper[opercount].mask = strdup_warn( Arg );
852 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
853 NGIRCd_ConfFile, Line, Var );
854 } /* Handle_OPERATOR */
858 Handle_SERVER( int Line, char *Var, char *Arg )
864 assert( Var != NULL );
865 assert( Arg != NULL );
867 /* Ignore server block if no space is left in server configuration structure */
868 if( New_Server_Idx <= NONE ) return;
870 if( strcasecmp( Var, "Host" ) == 0 ) {
871 /* Hostname of the server */
872 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
873 if (len >= sizeof( New_Server.host ))
874 Config_Error_TooLong ( Line, Var );
877 if( strcasecmp( Var, "Name" ) == 0 ) {
878 /* Name of the server ("Nick"/"ID") */
879 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
880 if (len >= sizeof( New_Server.name ))
881 Config_Error_TooLong( Line, Var );
884 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
885 /* Password of this server which is sent to the peer */
887 Config_Error(LOG_ERR,
888 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
889 NGIRCd_ConfFile, Line);
891 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
892 if (len >= sizeof( New_Server.pwd_in ))
893 Config_Error_TooLong( Line, Var );
896 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
897 /* Passwort of the peer which must be received */
898 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
899 if (len >= sizeof( New_Server.pwd_out ))
900 Config_Error_TooLong( Line, Var );
903 if( strcasecmp( Var, "Port" ) == 0 ) {
904 /* Port to which this server should connect */
906 if( port > 0 && port < 0xFFFF )
907 New_Server.port = (UINT16)port;
909 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
910 NGIRCd_ConfFile, Line, port );
913 if( strcasecmp( Var, "Group" ) == 0 ) {
916 if( ! isdigit( (int)*Arg ))
917 Config_Error_NaN( Line, Var );
920 New_Server.group = atoi( Arg );
924 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
925 NGIRCd_ConfFile, Line, Var );
926 } /* Handle_SERVER */
930 Handle_Channelname(size_t chancount, const char *name)
932 size_t size = sizeof( Conf_Channel[chancount].name );
933 char *dest = Conf_Channel[chancount].name;
935 if (*name && *name != '#') {
940 return size > strlcpy(dest, name, size);
945 Handle_CHANNEL( int Line, char *Var, char *Arg )
948 size_t chancount = 0;
951 assert( Var != NULL );
952 assert( Arg != NULL );
953 if (Conf_Channel_Count > 0)
954 chancount = Conf_Channel_Count - 1;
956 if( strcasecmp( Var, "Name" ) == 0 ) {
957 if (!Handle_Channelname(chancount, Arg))
958 Config_Error_TooLong( Line, Var );
961 if( strcasecmp( Var, "Modes" ) == 0 ) {
963 len = strlcpy( Conf_Channel[chancount].modes, Arg, sizeof( Conf_Channel[chancount].modes ));
964 if (len >= sizeof( Conf_Channel[chancount].modes ))
965 Config_Error_TooLong( Line, Var );
968 if( strcasecmp( Var, "Topic" ) == 0 ) {
970 if (!array_copys( &Conf_Channel[chancount].topic, Arg))
971 Config_Error_TooLong( Line, Var );
975 if( strcasecmp( Var, "Key" ) == 0 ) {
976 /* Initial Channel Key (mode k) */
977 len = strlcpy(Conf_Channel[chancount].key, Arg, sizeof(Conf_Channel[chancount].key));
978 if (len >= sizeof( Conf_Channel[chancount].key ))
979 Config_Error_TooLong(Line, Var);
983 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
984 /* maximum user limit, mode l */
985 Conf_Channel[chancount].maxusers = (unsigned long) atol(Arg);
986 if (Conf_Channel[chancount].maxusers == 0)
987 Config_Error_NaN(Line, Var);
991 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
992 NGIRCd_ConfFile, Line, Var );
993 } /* Handle_CHANNEL */
997 Validate_Config(bool Configtest, bool Rehash)
999 /* Validate configuration settings. */
1002 int i, servers, servers_once;
1006 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1007 ptr = Conf_ServerName;
1009 if (*ptr >= 'a' && *ptr <= 'z') continue;
1010 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1011 if (*ptr >= '0' && *ptr <= '9') continue;
1012 if (ptr > Conf_ServerName) {
1013 if (*ptr == '.' || *ptr == '-')
1016 Conf_ServerName[0] = '\0';
1020 if (!Conf_ServerName[0]) {
1021 /* No server name configured! */
1022 Config_Error(LOG_ALERT,
1023 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1025 if (!Configtest && !Rehash) {
1026 Config_Error(LOG_ALERT,
1027 "%s exiting due to fatal errors!",
1033 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1034 /* No dot in server name! */
1035 Config_Error(LOG_ALERT,
1036 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1039 Config_Error(LOG_ALERT,
1040 "%s exiting due to fatal errors!",
1047 if (!Conf_ServerAdminMail[0]) {
1048 /* No administrative contact configured! */
1049 Config_Error(LOG_ALERT,
1050 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1053 Config_Error(LOG_ALERT,
1054 "%s exiting due to fatal errors!",
1061 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1062 && !Conf_ServerAdminMail[0]) {
1063 /* No administrative information configured! */
1064 Config_Error(LOG_WARNING,
1065 "No administrative information configured but required by RFC!");
1069 servers = servers_once = 0;
1070 for (i = 0; i < MAX_SERVERS; i++) {
1071 if (Conf_Server[i].name[0]) {
1073 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1078 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1079 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1081 } /* Validate_Config */
1085 Config_Error_TooLong ( const int Line, const char *Item )
1087 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1092 Config_Error_NaN( const int Line, const char *Item )
1094 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1095 NGIRCd_ConfFile, Line, Item );
1100 static void Config_Error( const int Level, const char *Format, ... )
1102 static void Config_Error( Level, Format, va_alist )
1108 /* Error! Write to console and/or logfile. */
1110 char msg[MAX_LOG_MSG_LEN];
1113 assert( Format != NULL );
1116 va_start( ap, Format );
1120 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1123 /* During "normal operations" the log functions of the daemon should
1124 * be used, but during testing of the configuration file, all messages
1125 * should go directly to the console: */
1126 if (Use_Log) Log( Level, "%s", msg );
1128 } /* Config_Error */
1132 Init_Server_Struct( CONF_SERVER *Server )
1134 /* Initialize server configuration structur to default values */
1136 assert( Server != NULL );
1138 memset( Server, 0, sizeof (CONF_SERVER) );
1140 Server->group = NONE;
1141 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1143 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1145 Resolve_Init(&Server->res_stat);
1146 Server->conn_id = NONE;
1147 } /* Init_Server_Struct */