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_ConnectIPv6 = true;
485 Conf_ListenIPv4 = true;
486 Conf_ListenIPv6 = true;
488 Conf_MaxConnections = 0;
489 Conf_MaxConnectionsIP = 5;
491 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
493 /* Initialize server configuration structures */
494 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
499 Read_Config( bool ngircd_starting )
501 /* Read configuration file. */
503 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
504 const UINT16 defaultport = 6667;
508 /* Open configuration file */
509 fd = fopen( NGIRCd_ConfFile, "r" );
511 /* No configuration file found! */
512 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
513 NGIRCd_ConfFile, strerror( errno ));
514 if (!ngircd_starting)
516 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
520 Set_Defaults( ngircd_starting );
522 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
524 /* Clean up server configuration structure: mark all already
525 * configured servers as "once" so that they are deleted
526 * after the next disconnect and delete all unused servers.
527 * And delete all servers which are "duplicates" of servers
528 * that are already marked as "once" (such servers have been
529 * created by the last rehash but are now useless). */
530 for( i = 0; i < MAX_SERVERS; i++ ) {
531 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
533 /* This structure is in use ... */
534 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
535 /* Check for duplicates */
536 for( n = 0; n < MAX_SERVERS; n++ ) {
537 if( n == i ) continue;
539 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
540 Init_Server_Struct( &Conf_Server[n] );
542 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
548 /* Mark server as "once" */
549 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
550 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
555 /* Initialize variables */
557 strcpy( section, "" );
558 Init_Server_Struct( &New_Server );
559 New_Server_Idx = NONE;
561 /* Read configuration file */
563 if( ! fgets( str, LINE_LEN, fd )) break;
567 /* Skip comments and empty lines */
568 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
570 /* Is this the beginning of a new section? */
571 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
572 strlcpy( section, str, sizeof( section ));
573 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
576 if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
577 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
578 Config_Error( LOG_ERR, "Too many operators configured.");
580 /* Initialize new operator structure */
581 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
582 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
583 if (Conf_Oper[Conf_Oper_Count].mask) {
584 free(Conf_Oper[Conf_Oper_Count].mask );
585 Conf_Oper[Conf_Oper_Count].mask = NULL;
591 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
592 /* Check if there is already 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 /* Re-init structure for new server */
600 Init_Server_Struct( &New_Server );
602 /* Search unused item in server configuration structure */
603 for( i = 0; i < MAX_SERVERS; i++ ) {
604 /* Is this item used? */
605 if( ! Conf_Server[i].name[0] ) break;
607 if( i >= MAX_SERVERS ) {
608 /* Oops, no free item found! */
609 Config_Error( LOG_ERR, "Too many servers configured." );
610 New_Server_Idx = NONE;
612 else New_Server_Idx = i;
615 if( strcasecmp( section, "[CHANNEL]" ) == 0 ) {
616 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) {
617 Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
619 /* Initialize new channel structure */
620 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
621 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
622 strcpy( Conf_Channel[Conf_Channel_Count].key, "" );
623 Conf_Channel[Conf_Channel_Count].maxusers = 0;
624 array_free(&Conf_Channel[Conf_Channel_Count].topic);
625 Conf_Channel_Count++;
629 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
632 if( section[0] == 0x1 ) continue;
634 /* Split line into variable name and parameters */
635 ptr = strchr( str, '=' );
637 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
641 var = str; ngt_TrimStr( var );
642 arg = ptr + 1; ngt_TrimStr( arg );
644 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
645 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
646 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
647 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
648 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
651 /* Close configuration file */
654 /* Check if there is still a server to add */
655 if( New_Server.name[0] ) {
656 /* Copy data to "real" server structure */
657 assert( New_Server_Idx > NONE );
658 Conf_Server[New_Server_Idx] = New_Server;
661 if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
662 if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
663 Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
664 (unsigned int) defaultport, strerror(errno));
669 if (!Conf_ListenAddress) {
670 /* no Listen addresses configured, use default */
672 /* Conf_ListenIPv6/4 should no longer be used */
673 if (Conf_ListenIPv6 && Conf_ListenIPv4)
674 Conf_ListenAddress = strdup_warn("::,0.0.0.0");
675 else if (Conf_ListenIPv6)
676 Conf_ListenAddress = strdup_warn("::");
679 Conf_ListenAddress = strdup_warn("0.0.0.0");
681 if (!Conf_ListenAddress) {
682 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
690 Check_ArgIsTrue( const char *Arg )
692 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
693 if( strcasecmp( Arg, "true" ) == 0 ) return true;
694 if( atoi( Arg ) != 0 ) return true;
697 } /* Check_ArgIsTrue */
700 static unsigned int Handle_MaxNickLength(int Line, const char *Arg)
704 new = (unsigned) atoi(Arg) + 1;
705 if (new > CLIENT_NICK_LEN) {
706 Config_Error(LOG_WARNING,
707 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
708 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
709 return CLIENT_NICK_LEN;
712 Config_Error(LOG_WARNING,
713 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
714 NGIRCd_ConfFile, Line);
718 } /* Handle_MaxNickLength */
722 Handle_GLOBAL( int Line, char *Var, char *Arg )
729 assert( Var != NULL );
730 assert( Arg != NULL );
732 if( strcasecmp( Var, "Name" ) == 0 ) {
734 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
735 if (len >= sizeof( Conf_ServerName ))
736 Config_Error_TooLong( Line, Var );
739 if( strcasecmp( Var, "Info" ) == 0 ) {
740 /* Info text of server */
741 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
742 if (len >= sizeof( Conf_ServerInfo ))
743 Config_Error_TooLong ( Line, Var );
746 if( strcasecmp( Var, "Password" ) == 0 ) {
747 /* Global server password */
748 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
749 if (len >= sizeof( Conf_ServerPwd ))
750 Config_Error_TooLong( Line, Var );
753 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
754 /* Administrative info #1 */
755 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
756 if (len >= sizeof( Conf_ServerAdmin1 ))
757 Config_Error_TooLong ( Line, Var );
760 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
761 /* Administrative info #2 */
762 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
763 if (len >= sizeof( Conf_ServerAdmin2 ))
764 Config_Error_TooLong ( Line, Var );
767 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
768 /* Administrative email contact */
769 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
770 if (len >= sizeof( Conf_ServerAdminMail ))
771 Config_Error_TooLong( Line, Var );
775 if( strcasecmp( Var, "Ports" ) == 0 ) {
776 ports_parse(&Conf_ListenPorts, Line, Arg);
779 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
780 /* "Message of the day" (MOTD) file */
781 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
782 if (len >= sizeof( Conf_MotdFile ))
783 Config_Error_TooLong( Line, Var );
786 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
787 /* "Message of the day" phrase (instead of file) */
788 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
789 if (len >= sizeof( Conf_MotdPhrase ))
790 Config_Error_TooLong( Line, Var );
793 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
794 /* directory for chroot() */
795 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
796 if (len >= sizeof( Conf_Chroot ))
797 Config_Error_TooLong( Line, Var );
800 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
801 /* name of pidfile */
802 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
803 if (len >= sizeof( Conf_PidFile ))
804 Config_Error_TooLong( Line, Var );
807 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
808 /* UID the daemon should switch to */
809 pwd = getpwnam( Arg );
810 if( pwd ) Conf_UID = pwd->pw_uid;
813 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
816 Conf_UID = (unsigned int)atoi( Arg );
820 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
821 /* GID the daemon should use */
822 grp = getgrnam( Arg );
823 if( grp ) Conf_GID = grp->gr_gid;
826 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
829 Conf_GID = (unsigned int)atoi( Arg );
833 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
835 Conf_PingTimeout = atoi( Arg );
836 if( Conf_PingTimeout < 5 ) {
837 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
838 NGIRCd_ConfFile, Line );
839 Conf_PingTimeout = 5;
843 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
845 Conf_PongTimeout = atoi( Arg );
846 if( Conf_PongTimeout < 5 ) {
847 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
848 NGIRCd_ConfFile, Line );
849 Conf_PongTimeout = 5;
853 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
854 /* Seconds between connection attempts to other servers */
855 Conf_ConnectRetry = atoi( Arg );
856 if( Conf_ConnectRetry < 5 ) {
857 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
858 NGIRCd_ConfFile, Line );
859 Conf_ConnectRetry = 5;
863 if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
864 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
865 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
868 if( strcasecmp( Var, "NoDNS" ) == 0 ) {
869 /* don't do reverse dns lookups when clients connect? */
870 Conf_NoDNS = Check_ArgIsTrue( Arg );
874 /* the default setting for all the WANT_IPV6 special options is 'true' */
875 if (strcasecmp(Var, "ListenIPv6") == 0) { /* DEPRECATED, option appeared in 0.12.0 */
877 * listen on ipv6 sockets, if available?
878 * Deprecated use "Listen = 0.0.0.0" (or, rather, do not list "::")
880 Conf_ListenIPv6 = Check_ArgIsTrue( Arg );
881 Config_Error(LOG_WARNING, "%s, line %d: %s=%s is deprecated, %sinclude '::' in \"Listen =\" option instead",
882 NGIRCd_ConfFile, Line, Var, yesno_to_str(Conf_ListenIPv6), Conf_ListenIPv6 ? " ":"do not ");
885 if (strcasecmp(Var, "ListenIPv4") == 0) { /* DEPRECATED, option appeared in 0.12.0 */
887 * listen on ipv4 sockets, if available?
888 * this allows "ipv6-only" setups
889 * Deprecated use "Listen = ::" (or, rather, do not list "0.0.0.0")
891 Conf_ListenIPv4 = Check_ArgIsTrue( Arg );
892 Config_Error(LOG_WARNING, "%s, line %d: %s=%s is deprecated, %sinclude '0.0.0.0' in \"Listen =\" option instead",
893 NGIRCd_ConfFile, Line, Var, yesno_to_str(Conf_ListenIPv4), Conf_ListenIPv4 ? " ":"do not ");
896 if( strcasecmp( Var, "ConnectIPv6" ) == 0 ) {
897 /* connect to other hosts using ipv6, if they have an AAAA record? */
898 Conf_ConnectIPv6 = Check_ArgIsTrue( Arg );
901 if( strcasecmp( Var, "ConnectIPv4" ) == 0 ) {
902 /* connect to other hosts using ipv4.
903 * again, this can be used for ipv6-only setups */
904 Conf_ConnectIPv4 = Check_ArgIsTrue( Arg );
908 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
909 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
910 Conf_OperCanMode = Check_ArgIsTrue( Arg );
913 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
914 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
915 Conf_OperServerMode = Check_ArgIsTrue( Arg );
918 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
919 /* Maximum number of connections. 0 -> "no limit". */
921 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
924 Conf_MaxConnections = atol( Arg );
927 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
928 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
930 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
933 Conf_MaxConnectionsIP = atoi( Arg );
936 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
937 /* Maximum number of channels a user can join. 0 -> "no limit". */
939 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
942 Conf_MaxJoins = atoi( Arg );
945 if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
946 /* Maximum length of a nick name; must be same on all servers
947 * within the IRC network! */
948 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
952 if( strcasecmp( Var, "Listen" ) == 0 ) {
953 /* IP-Address to bind sockets */
954 if (Conf_ListenAddress) {
955 Config_Error(LOG_ERR, "Multiple Listen= options, ignoring: %s", Arg);
958 Conf_ListenAddress = strdup_warn(Arg);
960 * if allocation fails, we're in trouble:
961 * we cannot ignore the error -- otherwise ngircd
962 * would listen on all interfaces.
964 if (!Conf_ListenAddress) {
965 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
970 Config_Error(LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
971 NGIRCd_ConfFile, Line, Var);
972 } /* Handle_GLOBAL */
976 Handle_OPERATOR( int Line, char *Var, char *Arg )
978 unsigned int opercount;
981 assert( Var != NULL );
982 assert( Arg != NULL );
983 assert( Conf_Oper_Count > 0 );
985 if ( Conf_Oper_Count == 0 )
988 opercount = Conf_Oper_Count - 1;
990 if( strcasecmp( Var, "Name" ) == 0 ) {
991 /* Name of IRC operator */
992 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
993 if (len >= sizeof( Conf_Oper[opercount].name ))
994 Config_Error_TooLong( Line, Var );
997 if( strcasecmp( Var, "Password" ) == 0 ) {
998 /* Password of IRC operator */
999 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
1000 if (len >= sizeof( Conf_Oper[opercount].pwd ))
1001 Config_Error_TooLong( Line, Var );
1004 if( strcasecmp( Var, "Mask" ) == 0 ) {
1005 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
1007 Conf_Oper[opercount].mask = strdup_warn( Arg );
1010 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
1011 NGIRCd_ConfFile, Line, Var );
1012 } /* Handle_OPERATOR */
1016 Handle_SERVER( int Line, char *Var, char *Arg )
1022 assert( Var != NULL );
1023 assert( Arg != NULL );
1025 /* Ignore server block if no space is left in server configuration structure */
1026 if( New_Server_Idx <= NONE ) return;
1028 if( strcasecmp( Var, "Host" ) == 0 ) {
1029 /* Hostname of the server */
1030 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1031 if (len >= sizeof( New_Server.host ))
1032 Config_Error_TooLong ( Line, Var );
1035 if( strcasecmp( Var, "Name" ) == 0 ) {
1036 /* Name of the server ("Nick"/"ID") */
1037 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1038 if (len >= sizeof( New_Server.name ))
1039 Config_Error_TooLong( Line, Var );
1042 if (strcasecmp(Var, "Bind") == 0) {
1043 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1046 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1047 NGIRCd_ConfFile, Line, Arg);
1050 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1051 /* Password of this server which is sent to the peer */
1053 Config_Error(LOG_ERR,
1054 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1055 NGIRCd_ConfFile, Line);
1057 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1058 if (len >= sizeof( New_Server.pwd_in ))
1059 Config_Error_TooLong( Line, Var );
1062 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1063 /* Passwort of the peer which must be received */
1064 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1065 if (len >= sizeof( New_Server.pwd_out ))
1066 Config_Error_TooLong( Line, Var );
1069 if( strcasecmp( Var, "Port" ) == 0 ) {
1070 /* Port to which this server should connect */
1072 if( port > 0 && port < 0xFFFF )
1073 New_Server.port = (UINT16)port;
1075 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
1076 NGIRCd_ConfFile, Line, port );
1079 if( strcasecmp( Var, "Group" ) == 0 ) {
1082 if( ! isdigit( (int)*Arg ))
1083 Config_Error_NaN( Line, Var );
1086 New_Server.group = atoi( Arg );
1089 if( strcasecmp( Var, "Passive" ) == 0 ) {
1090 if (Check_ArgIsTrue(Arg))
1091 New_Server.flags |= CONF_SFLAG_DISABLED;
1095 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
1096 NGIRCd_ConfFile, Line, Var );
1097 } /* Handle_SERVER */
1101 Handle_Channelname(size_t chancount, const char *name)
1103 size_t size = sizeof( Conf_Channel[chancount].name );
1104 char *dest = Conf_Channel[chancount].name;
1106 if (*name && *name != '#') {
1111 return size > strlcpy(dest, name, size);
1116 Handle_CHANNEL( int Line, char *Var, char *Arg )
1119 size_t chancount = 0;
1122 assert( Var != NULL );
1123 assert( Arg != NULL );
1124 if (Conf_Channel_Count > 0)
1125 chancount = Conf_Channel_Count - 1;
1127 if( strcasecmp( Var, "Name" ) == 0 ) {
1128 if (!Handle_Channelname(chancount, Arg))
1129 Config_Error_TooLong( Line, Var );
1132 if( strcasecmp( Var, "Modes" ) == 0 ) {
1134 len = strlcpy( Conf_Channel[chancount].modes, Arg, sizeof( Conf_Channel[chancount].modes ));
1135 if (len >= sizeof( Conf_Channel[chancount].modes ))
1136 Config_Error_TooLong( Line, Var );
1139 if( strcasecmp( Var, "Topic" ) == 0 ) {
1141 if (!array_copys( &Conf_Channel[chancount].topic, Arg))
1142 Config_Error_TooLong( Line, Var );
1146 if( strcasecmp( Var, "Key" ) == 0 ) {
1147 /* Initial Channel Key (mode k) */
1148 len = strlcpy(Conf_Channel[chancount].key, Arg, sizeof(Conf_Channel[chancount].key));
1149 if (len >= sizeof( Conf_Channel[chancount].key ))
1150 Config_Error_TooLong(Line, Var);
1154 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1155 /* maximum user limit, mode l */
1156 Conf_Channel[chancount].maxusers = (unsigned long) atol(Arg);
1157 if (Conf_Channel[chancount].maxusers == 0)
1158 Config_Error_NaN(Line, Var);
1162 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1163 NGIRCd_ConfFile, Line, Var );
1164 } /* Handle_CHANNEL */
1168 Validate_Config(bool Configtest, bool Rehash)
1170 /* Validate configuration settings. */
1173 int i, servers, servers_once;
1177 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1178 ptr = Conf_ServerName;
1180 if (*ptr >= 'a' && *ptr <= 'z') continue;
1181 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1182 if (*ptr >= '0' && *ptr <= '9') continue;
1183 if (ptr > Conf_ServerName) {
1184 if (*ptr == '.' || *ptr == '-')
1187 Conf_ServerName[0] = '\0';
1191 if (!Conf_ServerName[0]) {
1192 /* No server name configured! */
1193 Config_Error(LOG_ALERT,
1194 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1196 if (!Configtest && !Rehash) {
1197 Config_Error(LOG_ALERT,
1198 "%s exiting due to fatal errors!",
1204 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1205 /* No dot in server name! */
1206 Config_Error(LOG_ALERT,
1207 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1210 Config_Error(LOG_ALERT,
1211 "%s exiting due to fatal errors!",
1218 if (!Conf_ServerAdminMail[0]) {
1219 /* No administrative contact configured! */
1220 Config_Error(LOG_ALERT,
1221 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1224 Config_Error(LOG_ALERT,
1225 "%s exiting due to fatal errors!",
1232 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1233 && !Conf_ServerAdminMail[0]) {
1234 /* No administrative information configured! */
1235 Config_Error(LOG_WARNING,
1236 "No administrative information configured but required by RFC!");
1240 servers = servers_once = 0;
1241 for (i = 0; i < MAX_SERVERS; i++) {
1242 if (Conf_Server[i].name[0]) {
1244 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1249 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1250 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1252 } /* Validate_Config */
1256 Config_Error_TooLong ( const int Line, const char *Item )
1258 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1263 Config_Error_NaN( const int Line, const char *Item )
1265 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1266 NGIRCd_ConfFile, Line, Item );
1271 static void Config_Error( const int Level, const char *Format, ... )
1273 static void Config_Error( Level, Format, va_alist )
1279 /* Error! Write to console and/or logfile. */
1281 char msg[MAX_LOG_MSG_LEN];
1284 assert( Format != NULL );
1287 va_start( ap, Format );
1291 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1294 /* During "normal operations" the log functions of the daemon should
1295 * be used, but during testing of the configuration file, all messages
1296 * should go directly to the console: */
1297 if (Use_Log) Log( Level, "%s", msg );
1299 } /* Config_Error */
1303 Init_Server_Struct( CONF_SERVER *Server )
1305 /* Initialize server configuration structur to default values */
1307 assert( Server != NULL );
1309 memset( Server, 0, sizeof (CONF_SERVER) );
1311 Server->group = NONE;
1312 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1314 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1316 Resolve_Init(&Server->res_stat);
1317 Server->conn_id = NONE;
1318 memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
1319 } /* Init_Server_Struct */