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.105 2008/03/18 20:12:47 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;
61 * these options appeared in ngircd 0.12; they are here
62 * for backwards compatibility. They should be removed
63 * in the future. Instead of setting these options,
64 * the "Listen" option should be set accordingly.
66 static bool Conf_ListenIPv6;
67 static bool Conf_ListenIPv4;
71 static void Set_Defaults PARAMS(( bool InitServers ));
72 static bool Read_Config PARAMS(( bool ngircd_starting ));
73 static void Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
75 static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
76 static void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
77 static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
78 static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
80 static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
82 static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
83 static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
85 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
89 strdup_warn(const char *str)
91 char *ptr = strdup(str);
93 Config_Error(LOG_ERR, "Could not allocate mem for string: %s", str);
103 len = array_length(a, sizeof(UINT16));
105 ports = (UINT16*) array_start(a);
106 printf("%u", (unsigned int) *ports);
109 printf(", %u", (unsigned int) *ports);
117 ports_parse(array *a, int Line, char *Arg)
125 /* Ports on that the server should listen. More port numbers
126 * must be separated by "," */
127 ptr = strtok( Arg, "," );
131 if (port > 0 && port < 0xFFFF) {
132 port16 = (UINT16) port;
133 if (!array_catb(a, (char*)&port16, sizeof port16))
134 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
135 NGIRCd_ConfFile, Line, port, strerror(errno));
137 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
138 NGIRCd_ConfFile, Line, port );
141 ptr = strtok( NULL, "," );
150 Validate_Config(false, false);
157 if (!Read_Config(false))
159 Validate_Config(false, true);
161 /* Update CLIENT structure of local server */
162 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
164 } /* Config_Rehash */
168 yesno_to_str(int boolean_value)
179 /* Read configuration, validate and output it. */
189 Validate_Config(true, false);
191 /* If stdin and stdout ("you can read our nice message and we can
192 * read in your keypress") are valid tty's, wait for a key: */
193 if( isatty( fileno( stdin )) && isatty( fileno( stdout ))) {
194 puts( "OK, press enter to see a dump of your service configuration ..." );
197 puts( "Ok, dump of your server configuration follows:\n" );
201 printf( " Name = %s\n", Conf_ServerName );
202 printf( " Info = %s\n", Conf_ServerInfo );
203 printf( " Password = %s\n", Conf_ServerPwd );
204 printf( " AdminInfo1 = %s\n", Conf_ServerAdmin1 );
205 printf( " AdminInfo2 = %s\n", Conf_ServerAdmin2 );
206 printf( " AdminEMail = %s\n", Conf_ServerAdminMail );
207 printf( " MotdFile = %s\n", Conf_MotdFile );
208 printf( " MotdPhrase = %s\n", Conf_MotdPhrase );
209 printf( " ChrootDir = %s\n", Conf_Chroot );
210 printf( " PidFile = %s\n", Conf_PidFile);
211 fputs(" Ports = ", stdout);
213 ports_puts(&Conf_ListenPorts);
214 printf(" Listen = %s\n", Conf_ListenAddress);
215 pwd = getpwuid( Conf_UID );
216 if( pwd ) printf( " ServerUID = %s\n", pwd->pw_name );
217 else printf( " ServerUID = %ld\n", (long)Conf_UID );
218 grp = getgrgid( Conf_GID );
219 if( grp ) printf( " ServerGID = %s\n", grp->gr_name );
220 else printf( " ServerGID = %ld\n", (long)Conf_GID );
221 printf( " PingTimeout = %d\n", Conf_PingTimeout );
222 printf( " PongTimeout = %d\n", Conf_PongTimeout );
223 printf( " ConnectRetry = %d\n", Conf_ConnectRetry );
224 printf( " OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
225 printf( " OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
226 printf( " PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
227 printf( " NoDNS = %s\n", yesno_to_str(Conf_NoDNS));
230 /* both are deprecated, only mention them if their default value changed. */
231 if (!Conf_ListenIPv6)
232 puts(" ListenIPv6 = no");
233 if (!Conf_ListenIPv4)
234 puts(" ListenIPv4 = no");
235 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
236 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
238 printf( " MaxConnections = %ld\n", Conf_MaxConnections);
239 printf( " MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
240 printf( " MaxJoins = %d\n", Conf_MaxJoins>0 ? Conf_MaxJoins : -1);
241 printf( " MaxNickLength = %u\n\n", Conf_MaxNickLength - 1);
243 for( i = 0; i < Conf_Oper_Count; i++ ) {
244 if( ! Conf_Oper[i].name[0] ) continue;
246 /* Valid "Operator" section */
247 puts( "[OPERATOR]" );
248 printf( " Name = %s\n", Conf_Oper[i].name );
249 printf( " Password = %s\n", Conf_Oper[i].pwd );
250 if ( Conf_Oper[i].mask ) printf( " Mask = %s\n", Conf_Oper[i].mask );
254 for( i = 0; i < MAX_SERVERS; i++ ) {
255 if( ! Conf_Server[i].name[0] ) continue;
257 /* Valid "Server" section */
259 printf( " Name = %s\n", Conf_Server[i].name );
260 printf( " Host = %s\n", Conf_Server[i].host );
261 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
262 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
263 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
264 printf( " Group = %d\n", Conf_Server[i].group );
265 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
268 for( i = 0; i < Conf_Channel_Count; i++ ) {
269 if( ! Conf_Channel[i].name[0] ) continue;
271 /* Valid "Channel" section */
273 printf( " Name = %s\n", Conf_Channel[i].name );
274 printf( " Modes = %s\n", Conf_Channel[i].modes );
275 printf( " Key = %s\n", Conf_Channel[i].key );
276 printf( " MaxUsers = %lu\n", Conf_Channel[i].maxusers );
278 topic = (char*)array_start(&Conf_Channel[i].topic);
279 printf( " Topic = %s\n\n", topic ? topic : "");
287 Conf_UnsetServer( CONN_ID Idx )
289 /* Set next time for next connection attempt, if this is a server
290 * link that is (still) configured here. If the server is set as
291 * "once", delete it from our configuration.
292 * Non-Server-Connections will be silently ignored. */
297 /* Check all our configured servers */
298 for( i = 0; i < MAX_SERVERS; i++ ) {
299 if( Conf_Server[i].conn_id != Idx ) continue;
301 /* Gotcha! Mark server configuration as "unused": */
302 Conf_Server[i].conn_id = NONE;
304 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
305 /* Delete configuration here */
306 Init_Server_Struct( &Conf_Server[i] );
308 /* Set time for next connect attempt */
310 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
311 /* The connection has been "long", so we don't
312 * require the next attempt to be delayed. */
313 Conf_Server[i].lasttry =
314 t - Conf_ConnectRetry + RECONNECT_DELAY;
316 Conf_Server[i].lasttry = t;
319 } /* Conf_UnsetServer */
323 Conf_SetServer( int ConfServer, CONN_ID Idx )
325 /* Set connection for specified configured server */
327 assert( ConfServer > NONE );
328 assert( Idx > NONE );
330 Conf_Server[ConfServer].conn_id = Idx;
331 } /* Conf_SetServer */
335 Conf_GetServer( CONN_ID Idx )
337 /* Get index of server in configuration structure */
341 assert( Idx > NONE );
343 for( i = 0; i < MAX_SERVERS; i++ ) {
344 if( Conf_Server[i].conn_id == Idx ) return i;
347 } /* Conf_GetServer */
351 Conf_EnableServer( char *Name, UINT16 Port )
353 /* Enable specified server and adjust port */
357 assert( Name != NULL );
359 for( i = 0; i < MAX_SERVERS; i++ ) {
360 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
361 /* Gotcha! Set port and enable server: */
362 Conf_Server[i].port = Port;
363 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
368 } /* Conf_EnableServer */
372 Conf_EnablePassiveServer(const char *Name)
374 /* Enable specified server */
377 assert( Name != NULL );
378 for (i = 0; i < MAX_SERVERS; i++) {
379 if ((strcasecmp( Conf_Server[i].name, Name ) == 0) && (Conf_Server[i].port > 0)) {
380 /* BINGO! Enable server */
381 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
386 } /* Conf_EnablePassiveServer */
390 Conf_DisableServer( char *Name )
392 /* Enable specified server and adjust port */
396 assert( Name != NULL );
398 for( i = 0; i < MAX_SERVERS; i++ ) {
399 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
400 /* Gotcha! Disable and disconnect server: */
401 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
402 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
407 } /* Conf_DisableServer */
411 Conf_AddServer( char *Name, UINT16 Port, char *Host, char *MyPwd, char *PeerPwd )
413 /* Add new server to configuration */
417 assert( Name != NULL );
418 assert( Host != NULL );
419 assert( MyPwd != NULL );
420 assert( PeerPwd != NULL );
422 /* Search unused item in server configuration structure */
423 for( i = 0; i < MAX_SERVERS; i++ ) {
424 /* Is this item used? */
425 if( ! Conf_Server[i].name[0] ) break;
427 if( i >= MAX_SERVERS ) return false;
429 Init_Server_Struct( &Conf_Server[i] );
430 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
431 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
432 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
433 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
434 Conf_Server[i].port = Port;
435 Conf_Server[i].flags = CONF_SFLAG_ONCE;
438 } /* Conf_AddServer */
442 Set_Defaults( bool InitServers )
444 /* Initialize configuration variables with default values. */
448 strcpy( Conf_ServerName, "" );
449 snprintf( Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
450 strcpy( Conf_ServerPwd, "" );
452 strcpy( Conf_ServerAdmin1, "" );
453 strcpy( Conf_ServerAdmin2, "" );
454 strcpy( Conf_ServerAdminMail, "" );
456 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
457 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
459 strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
461 strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
463 strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
465 free(Conf_ListenAddress);
466 Conf_ListenAddress = NULL;
467 Conf_UID = Conf_GID = 0;
469 Conf_PingTimeout = 120;
470 Conf_PongTimeout = 20;
472 Conf_ConnectRetry = 60;
475 Conf_Channel_Count = 0;
477 Conf_OperCanMode = false;
479 Conf_PredefChannelsOnly = false;
480 Conf_OperServerMode = false;
482 Conf_ConnectIPv4 = true;
483 Conf_ListenIPv4 = true;
484 Conf_ConnectIPv6 = true;
485 Conf_ListenIPv6 = true;
487 Conf_MaxConnections = 0;
488 Conf_MaxConnectionsIP = 5;
490 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
492 /* Initialize server configuration structures */
493 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
498 Read_Config( bool ngircd_starting )
500 /* Read configuration file. */
502 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
503 const UINT16 defaultport = 6667;
507 /* Open configuration file */
508 fd = fopen( NGIRCd_ConfFile, "r" );
510 /* No configuration file found! */
511 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
512 NGIRCd_ConfFile, strerror( errno ));
513 if (!ngircd_starting)
515 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
519 Set_Defaults( ngircd_starting );
521 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
523 /* Clean up server configuration structure: mark all already
524 * configured servers as "once" so that they are deleted
525 * after the next disconnect and delete all unused servers.
526 * And delete all servers which are "duplicates" of servers
527 * that are already marked as "once" (such servers have been
528 * created by the last rehash but are now useless). */
529 for( i = 0; i < MAX_SERVERS; i++ ) {
530 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
532 /* This structure is in use ... */
533 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
534 /* Check for duplicates */
535 for( n = 0; n < MAX_SERVERS; n++ ) {
536 if( n == i ) continue;
538 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
539 Init_Server_Struct( &Conf_Server[n] );
541 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
547 /* Mark server as "once" */
548 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
549 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
554 /* Initialize variables */
556 strcpy( section, "" );
557 Init_Server_Struct( &New_Server );
558 New_Server_Idx = NONE;
560 /* Read configuration file */
562 if( ! fgets( str, LINE_LEN, fd )) break;
566 /* Skip comments and empty lines */
567 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
569 /* Is this the beginning of a new section? */
570 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
571 strlcpy( section, str, sizeof( section ));
572 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
575 if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
576 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
577 Config_Error( LOG_ERR, "Too many operators configured.");
579 /* Initialize new operator structure */
580 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
581 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
582 if (Conf_Oper[Conf_Oper_Count].mask) {
583 free(Conf_Oper[Conf_Oper_Count].mask );
584 Conf_Oper[Conf_Oper_Count].mask = NULL;
590 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
591 /* Check if there is already a server to add */
592 if( New_Server.name[0] ) {
593 /* Copy data to "real" server structure */
594 assert( New_Server_Idx > NONE );
595 Conf_Server[New_Server_Idx] = New_Server;
598 /* Re-init structure for new server */
599 Init_Server_Struct( &New_Server );
601 /* Search unused item in server configuration structure */
602 for( i = 0; i < MAX_SERVERS; i++ ) {
603 /* Is this item used? */
604 if( ! Conf_Server[i].name[0] ) break;
606 if( i >= MAX_SERVERS ) {
607 /* Oops, no free item found! */
608 Config_Error( LOG_ERR, "Too many servers configured." );
609 New_Server_Idx = NONE;
611 else New_Server_Idx = i;
614 if( strcasecmp( section, "[CHANNEL]" ) == 0 ) {
615 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) {
616 Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
618 /* Initialize new channel structure */
619 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
620 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
621 strcpy( Conf_Channel[Conf_Channel_Count].key, "" );
622 Conf_Channel[Conf_Channel_Count].maxusers = 0;
623 array_free(&Conf_Channel[Conf_Channel_Count].topic);
624 Conf_Channel_Count++;
628 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
631 if( section[0] == 0x1 ) continue;
633 /* Split line into variable name and parameters */
634 ptr = strchr( str, '=' );
636 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
640 var = str; ngt_TrimStr( var );
641 arg = ptr + 1; ngt_TrimStr( arg );
643 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
644 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
645 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
646 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
647 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
650 /* Close configuration file */
653 /* Check if there is still a server to add */
654 if( New_Server.name[0] ) {
655 /* Copy data to "real" server structure */
656 assert( New_Server_Idx > NONE );
657 Conf_Server[New_Server_Idx] = New_Server;
660 if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
661 if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
662 Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
663 (unsigned int) defaultport, strerror(errno));
668 if (!Conf_ListenAddress) {
669 /* no Listen addresses configured, use default */
671 /* Conf_ListenIPv6/4 should no longer be used */
672 if (Conf_ListenIPv6 && Conf_ListenIPv4)
673 Conf_ListenAddress = strdup_warn("::,0.0.0.0");
674 else if (Conf_ListenIPv6)
675 Conf_ListenAddress = strdup_warn("::");
678 Conf_ListenAddress = strdup_warn("0.0.0.0");
680 if (!Conf_ListenAddress) {
681 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
689 Check_ArgIsTrue( const char *Arg )
691 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
692 if( strcasecmp( Arg, "true" ) == 0 ) return true;
693 if( atoi( Arg ) != 0 ) return true;
696 } /* Check_ArgIsTrue */
699 static unsigned int Handle_MaxNickLength(int Line, const char *Arg)
703 new = (unsigned) atoi(Arg) + 1;
704 if (new > CLIENT_NICK_LEN) {
705 Config_Error(LOG_WARNING,
706 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
707 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
708 return CLIENT_NICK_LEN;
711 Config_Error(LOG_WARNING,
712 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
713 NGIRCd_ConfFile, Line);
717 } /* Handle_MaxNickLength */
721 Handle_GLOBAL( int Line, char *Var, char *Arg )
728 assert( Var != NULL );
729 assert( Arg != NULL );
731 if( strcasecmp( Var, "Name" ) == 0 ) {
733 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
734 if (len >= sizeof( Conf_ServerName ))
735 Config_Error_TooLong( Line, Var );
738 if( strcasecmp( Var, "Info" ) == 0 ) {
739 /* Info text of server */
740 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
741 if (len >= sizeof( Conf_ServerInfo ))
742 Config_Error_TooLong ( Line, Var );
745 if( strcasecmp( Var, "Password" ) == 0 ) {
746 /* Global server password */
747 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
748 if (len >= sizeof( Conf_ServerPwd ))
749 Config_Error_TooLong( Line, Var );
752 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
753 /* Administrative info #1 */
754 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
755 if (len >= sizeof( Conf_ServerAdmin1 ))
756 Config_Error_TooLong ( Line, Var );
759 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
760 /* Administrative info #2 */
761 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
762 if (len >= sizeof( Conf_ServerAdmin2 ))
763 Config_Error_TooLong ( Line, Var );
766 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
767 /* Administrative email contact */
768 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
769 if (len >= sizeof( Conf_ServerAdminMail ))
770 Config_Error_TooLong( Line, Var );
774 if( strcasecmp( Var, "Ports" ) == 0 ) {
775 ports_parse(&Conf_ListenPorts, Line, Arg);
778 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
779 /* "Message of the day" (MOTD) file */
780 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
781 if (len >= sizeof( Conf_MotdFile ))
782 Config_Error_TooLong( Line, Var );
785 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
786 /* "Message of the day" phrase (instead of file) */
787 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
788 if (len >= sizeof( Conf_MotdPhrase ))
789 Config_Error_TooLong( Line, Var );
792 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
793 /* directory for chroot() */
794 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
795 if (len >= sizeof( Conf_Chroot ))
796 Config_Error_TooLong( Line, Var );
799 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
800 /* name of pidfile */
801 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
802 if (len >= sizeof( Conf_PidFile ))
803 Config_Error_TooLong( Line, Var );
806 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
807 /* UID the daemon should switch to */
808 pwd = getpwnam( Arg );
809 if( pwd ) Conf_UID = pwd->pw_uid;
812 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
815 Conf_UID = (unsigned int)atoi( Arg );
819 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
820 /* GID the daemon should use */
821 grp = getgrnam( Arg );
822 if( grp ) Conf_GID = grp->gr_gid;
825 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
828 Conf_GID = (unsigned int)atoi( Arg );
832 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
834 Conf_PingTimeout = atoi( Arg );
835 if( Conf_PingTimeout < 5 ) {
836 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
837 NGIRCd_ConfFile, Line );
838 Conf_PingTimeout = 5;
842 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
844 Conf_PongTimeout = atoi( Arg );
845 if( Conf_PongTimeout < 5 ) {
846 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
847 NGIRCd_ConfFile, Line );
848 Conf_PongTimeout = 5;
852 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
853 /* Seconds between connection attempts to other servers */
854 Conf_ConnectRetry = atoi( Arg );
855 if( Conf_ConnectRetry < 5 ) {
856 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
857 NGIRCd_ConfFile, Line );
858 Conf_ConnectRetry = 5;
862 if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
863 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
864 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
867 if( strcasecmp( Var, "NoDNS" ) == 0 ) {
868 /* don't do reverse dns lookups when clients connect? */
869 Conf_NoDNS = Check_ArgIsTrue( Arg );
873 /* the default setting for all the WANT_IPV6 special options is 'true' */
874 if (strcasecmp(Var, "ListenIPv6") == 0) { /* DEPRECATED, option appeared in 0.12.0 */
876 * listen on ipv6 sockets, if available?
877 * Deprecated use "Listen = 0.0.0.0" (or, rather, do not list "::")
879 Conf_ListenIPv6 = Check_ArgIsTrue( Arg );
880 Config_Error(LOG_WARNING, "%s, line %d: %s=%s is deprecated, %sinclude '::' in \"Listen =\" option instead",
881 NGIRCd_ConfFile, Line, Var, yesno_to_str(Conf_ListenIPv6), Conf_ListenIPv6 ? " ":"do not ");
884 if (strcasecmp(Var, "ListenIPv4") == 0) { /* DEPRECATED, option appeared in 0.12.0 */
886 * listen on ipv4 sockets, if available?
887 * this allows "ipv6-only" setups
888 * Deprecated use "Listen = ::" (or, rather, do not list "0.0.0.0")
890 Conf_ListenIPv4 = Check_ArgIsTrue( Arg );
891 Config_Error(LOG_WARNING, "%s, line %d: %s=%s is deprecated, %sinclude '0.0.0.0' in \"Listen =\" option instead",
892 NGIRCd_ConfFile, Line, Var, yesno_to_str(Conf_ListenIPv4), Conf_ListenIPv4 ? " ":"do not ");
895 if( strcasecmp( Var, "ConnectIPv6" ) == 0 ) {
896 /* connect to other hosts using ipv6, if they have an AAAA record? */
897 Conf_ConnectIPv6 = Check_ArgIsTrue( Arg );
900 if( strcasecmp( Var, "ConnectIPv4" ) == 0 ) {
901 /* connect to other hosts using ipv4.
902 * again, this can be used for ipv6-only setups */
903 Conf_ConnectIPv4 = Check_ArgIsTrue( Arg );
907 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
908 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
909 Conf_OperCanMode = Check_ArgIsTrue( Arg );
912 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
913 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
914 Conf_OperServerMode = Check_ArgIsTrue( Arg );
917 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
918 /* Maximum number of connections. 0 -> "no limit". */
920 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
923 Conf_MaxConnections = atol( Arg );
926 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
927 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
929 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
932 Conf_MaxConnectionsIP = atoi( Arg );
935 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
936 /* Maximum number of channels a user can join. 0 -> "no limit". */
938 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
941 Conf_MaxJoins = atoi( Arg );
944 if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
945 /* Maximum length of a nick name; must be same on all servers
946 * within the IRC network! */
947 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
951 if( strcasecmp( Var, "Listen" ) == 0 ) {
952 /* IP-Address to bind sockets */
953 if (Conf_ListenAddress) {
954 Config_Error(LOG_ERR, "Multiple Listen= options, ignoring: %s", Arg);
957 Conf_ListenAddress = strdup_warn(Arg);
959 * if allocation fails, we're in trouble:
960 * we cannot ignore the error -- otherwise ngircd
961 * would listen on all interfaces.
963 if (!Conf_ListenAddress) {
964 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
969 Config_Error(LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
970 NGIRCd_ConfFile, Line, Var);
971 } /* Handle_GLOBAL */
975 Handle_OPERATOR( int Line, char *Var, char *Arg )
977 unsigned int opercount;
980 assert( Var != NULL );
981 assert( Arg != NULL );
982 assert( Conf_Oper_Count > 0 );
984 if ( Conf_Oper_Count == 0 )
987 opercount = Conf_Oper_Count - 1;
989 if( strcasecmp( Var, "Name" ) == 0 ) {
990 /* Name of IRC operator */
991 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
992 if (len >= sizeof( Conf_Oper[opercount].name ))
993 Config_Error_TooLong( Line, Var );
996 if( strcasecmp( Var, "Password" ) == 0 ) {
997 /* Password of IRC operator */
998 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
999 if (len >= sizeof( Conf_Oper[opercount].pwd ))
1000 Config_Error_TooLong( Line, Var );
1003 if( strcasecmp( Var, "Mask" ) == 0 ) {
1004 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
1006 Conf_Oper[opercount].mask = strdup_warn( Arg );
1009 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
1010 NGIRCd_ConfFile, Line, Var );
1011 } /* Handle_OPERATOR */
1015 Handle_SERVER( int Line, char *Var, char *Arg )
1021 assert( Var != NULL );
1022 assert( Arg != NULL );
1024 /* Ignore server block if no space is left in server configuration structure */
1025 if( New_Server_Idx <= NONE ) return;
1027 if( strcasecmp( Var, "Host" ) == 0 ) {
1028 /* Hostname of the server */
1029 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1030 if (len >= sizeof( New_Server.host ))
1031 Config_Error_TooLong ( Line, Var );
1034 if( strcasecmp( Var, "Name" ) == 0 ) {
1035 /* Name of the server ("Nick"/"ID") */
1036 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1037 if (len >= sizeof( New_Server.name ))
1038 Config_Error_TooLong( Line, Var );
1041 if (strcasecmp(Var, "Bind") == 0) {
1042 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1045 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1046 NGIRCd_ConfFile, Line, Arg);
1049 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1050 /* Password of this server which is sent to the peer */
1052 Config_Error(LOG_ERR,
1053 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1054 NGIRCd_ConfFile, Line);
1056 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1057 if (len >= sizeof( New_Server.pwd_in ))
1058 Config_Error_TooLong( Line, Var );
1061 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1062 /* Passwort of the peer which must be received */
1063 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1064 if (len >= sizeof( New_Server.pwd_out ))
1065 Config_Error_TooLong( Line, Var );
1068 if( strcasecmp( Var, "Port" ) == 0 ) {
1069 /* Port to which this server should connect */
1071 if( port > 0 && port < 0xFFFF )
1072 New_Server.port = (UINT16)port;
1074 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
1075 NGIRCd_ConfFile, Line, port );
1078 if( strcasecmp( Var, "Group" ) == 0 ) {
1081 if( ! isdigit( (int)*Arg ))
1082 Config_Error_NaN( Line, Var );
1085 New_Server.group = atoi( Arg );
1088 if( strcasecmp( Var, "Passive" ) == 0 ) {
1089 if (Check_ArgIsTrue(Arg))
1090 New_Server.flags |= CONF_SFLAG_DISABLED;
1094 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
1095 NGIRCd_ConfFile, Line, Var );
1096 } /* Handle_SERVER */
1100 Handle_Channelname(size_t chancount, const char *name)
1102 size_t size = sizeof( Conf_Channel[chancount].name );
1103 char *dest = Conf_Channel[chancount].name;
1105 if (*name && *name != '#') {
1110 return size > strlcpy(dest, name, size);
1115 Handle_CHANNEL( int Line, char *Var, char *Arg )
1118 size_t chancount = 0;
1121 assert( Var != NULL );
1122 assert( Arg != NULL );
1123 if (Conf_Channel_Count > 0)
1124 chancount = Conf_Channel_Count - 1;
1126 if( strcasecmp( Var, "Name" ) == 0 ) {
1127 if (!Handle_Channelname(chancount, Arg))
1128 Config_Error_TooLong( Line, Var );
1131 if( strcasecmp( Var, "Modes" ) == 0 ) {
1133 len = strlcpy( Conf_Channel[chancount].modes, Arg, sizeof( Conf_Channel[chancount].modes ));
1134 if (len >= sizeof( Conf_Channel[chancount].modes ))
1135 Config_Error_TooLong( Line, Var );
1138 if( strcasecmp( Var, "Topic" ) == 0 ) {
1140 if (!array_copys( &Conf_Channel[chancount].topic, Arg))
1141 Config_Error_TooLong( Line, Var );
1145 if( strcasecmp( Var, "Key" ) == 0 ) {
1146 /* Initial Channel Key (mode k) */
1147 len = strlcpy(Conf_Channel[chancount].key, Arg, sizeof(Conf_Channel[chancount].key));
1148 if (len >= sizeof( Conf_Channel[chancount].key ))
1149 Config_Error_TooLong(Line, Var);
1153 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1154 /* maximum user limit, mode l */
1155 Conf_Channel[chancount].maxusers = (unsigned long) atol(Arg);
1156 if (Conf_Channel[chancount].maxusers == 0)
1157 Config_Error_NaN(Line, Var);
1161 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1162 NGIRCd_ConfFile, Line, Var );
1163 } /* Handle_CHANNEL */
1167 Validate_Config(bool Configtest, bool Rehash)
1169 /* Validate configuration settings. */
1172 int i, servers, servers_once;
1176 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1177 ptr = Conf_ServerName;
1179 if (*ptr >= 'a' && *ptr <= 'z') continue;
1180 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1181 if (*ptr >= '0' && *ptr <= '9') continue;
1182 if (ptr > Conf_ServerName) {
1183 if (*ptr == '.' || *ptr == '-')
1186 Conf_ServerName[0] = '\0';
1190 if (!Conf_ServerName[0]) {
1191 /* No server name configured! */
1192 Config_Error(LOG_ALERT,
1193 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1195 if (!Configtest && !Rehash) {
1196 Config_Error(LOG_ALERT,
1197 "%s exiting due to fatal errors!",
1203 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1204 /* No dot in server name! */
1205 Config_Error(LOG_ALERT,
1206 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1209 Config_Error(LOG_ALERT,
1210 "%s exiting due to fatal errors!",
1217 if (!Conf_ServerAdminMail[0]) {
1218 /* No administrative contact configured! */
1219 Config_Error(LOG_ALERT,
1220 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1223 Config_Error(LOG_ALERT,
1224 "%s exiting due to fatal errors!",
1231 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1232 && !Conf_ServerAdminMail[0]) {
1233 /* No administrative information configured! */
1234 Config_Error(LOG_WARNING,
1235 "No administrative information configured but required by RFC!");
1239 servers = servers_once = 0;
1240 for (i = 0; i < MAX_SERVERS; i++) {
1241 if (Conf_Server[i].name[0]) {
1243 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1248 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1249 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1251 } /* Validate_Config */
1255 Config_Error_TooLong ( const int Line, const char *Item )
1257 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1262 Config_Error_NaN( const int Line, const char *Item )
1264 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1265 NGIRCd_ConfFile, Line, Item );
1270 static void Config_Error( const int Level, const char *Format, ... )
1272 static void Config_Error( Level, Format, va_alist )
1278 /* Error! Write to console and/or logfile. */
1280 char msg[MAX_LOG_MSG_LEN];
1283 assert( Format != NULL );
1286 va_start( ap, Format );
1290 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1293 /* During "normal operations" the log functions of the daemon should
1294 * be used, but during testing of the configuration file, all messages
1295 * should go directly to the console: */
1296 if (Use_Log) Log( Level, "%s", msg );
1298 } /* Config_Error */
1302 Init_Server_Struct( CONF_SERVER *Server )
1304 /* Initialize server configuration structur to default values */
1306 assert( Server != NULL );
1308 memset( Server, 0, sizeof (CONF_SERVER) );
1310 Server->group = NONE;
1311 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1313 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1315 Resolve_Init(&Server->res_stat);
1316 Server->conn_id = NONE;
1317 memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
1318 } /* Init_Server_Struct */