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)
32 #include <sys/types.h>
53 static bool Use_Log = true;
54 static CONF_SERVER New_Server;
55 static int New_Server_Idx;
58 static void Set_Defaults PARAMS(( bool InitServers ));
59 static bool Read_Config PARAMS(( bool ngircd_starting ));
60 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
62 static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
63 static void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
64 static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
65 static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
67 static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
69 static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
70 static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
72 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
75 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
77 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
81 struct SSLOptions Conf_SSLOptions;
86 free(Conf_SSLOptions.KeyFile);
87 Conf_SSLOptions.KeyFile = NULL;
89 free(Conf_SSLOptions.CertFile);
90 Conf_SSLOptions.CertFile = NULL;
92 free(Conf_SSLOptions.DHFile);
93 Conf_SSLOptions.DHFile = NULL;
94 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
101 if (Conf_SSLOptions.KeyFile)
102 printf( " SSLKeyFile = %s\n", Conf_SSLOptions.KeyFile);
103 if (Conf_SSLOptions.CertFile)
104 printf( " SSLCertFile = %s\n", Conf_SSLOptions.CertFile);
105 if (Conf_SSLOptions.DHFile)
106 printf( " SSLDHFile = %s\n", Conf_SSLOptions.DHFile);
107 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
108 puts(" SSLKeyFilePassword = <secret>" );
109 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
114 strdup_warn(const char *str)
116 char *ptr = strdup(str);
118 Config_Error(LOG_ERR, "Could not allocate mem for string: %s", str);
128 len = array_length(a, sizeof(UINT16));
130 ports = (UINT16*) array_start(a);
131 printf("%u", (unsigned int) *ports);
134 printf(", %u", (unsigned int) *ports);
142 ports_parse(array *a, int Line, char *Arg)
150 /* Ports on that the server should listen. More port numbers
151 * must be separated by "," */
152 ptr = strtok( Arg, "," );
156 if (port > 0 && port < 0xFFFF) {
157 port16 = (UINT16) port;
158 if (!array_catb(a, (char*)&port16, sizeof port16))
159 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
160 NGIRCd_ConfFile, Line, port, strerror(errno));
162 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
163 NGIRCd_ConfFile, Line, port );
166 ptr = strtok( NULL, "," );
175 Validate_Config(false, false);
182 if (!Read_Config(false))
184 Validate_Config(false, true);
186 /* Update CLIENT structure of local server */
187 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
189 } /* Config_Rehash */
193 yesno_to_str(int boolean_value)
204 /* Read configuration, validate and output it. */
214 if (! Read_Config(true))
217 config_valid = Validate_Config(true, false);
219 /* If stdin and stdout ("you can read our nice message and we can
220 * read in your keypress") are valid tty's, wait for a key: */
221 if( isatty( fileno( stdin )) && isatty( fileno( stdout ))) {
222 puts( "OK, press enter to see a dump of your service configuration ..." );
225 puts( "Ok, dump of your server configuration follows:\n" );
229 printf( " Name = %s\n", Conf_ServerName );
230 printf( " Info = %s\n", Conf_ServerInfo );
231 printf( " Password = %s\n", Conf_ServerPwd );
232 printf( " AdminInfo1 = %s\n", Conf_ServerAdmin1 );
233 printf( " AdminInfo2 = %s\n", Conf_ServerAdmin2 );
234 printf( " AdminEMail = %s\n", Conf_ServerAdminMail );
235 printf( " MotdFile = %s\n", Conf_MotdFile );
236 printf( " MotdPhrase = %s\n", Conf_MotdPhrase );
237 printf( " ChrootDir = %s\n", Conf_Chroot );
238 printf( " PidFile = %s\n", Conf_PidFile);
239 printf(" Listen = %s\n", Conf_ListenAddress);
240 fputs(" Ports = ", stdout);
242 ports_puts(&Conf_ListenPorts);
244 fputs(" SSLPorts = ", stdout);
245 ports_puts(&Conf_SSLOptions.ListenPorts);
249 pwd = getpwuid( Conf_UID );
250 if( pwd ) printf( " ServerUID = %s\n", pwd->pw_name );
251 else printf( " ServerUID = %ld\n", (long)Conf_UID );
252 grp = getgrgid( Conf_GID );
253 if( grp ) printf( " ServerGID = %s\n", grp->gr_name );
254 else printf( " ServerGID = %ld\n", (long)Conf_GID );
255 printf( " PingTimeout = %d\n", Conf_PingTimeout );
256 printf( " PongTimeout = %d\n", Conf_PongTimeout );
257 printf( " ConnectRetry = %d\n", Conf_ConnectRetry );
258 printf( " OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
259 printf( " OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
260 printf( " PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
261 printf( " NoDNS = %s\n", yesno_to_str(Conf_NoDNS));
262 printf( " NoIdent = %s\n", yesno_to_str(Conf_NoIdent));
265 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
266 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
268 printf( " MaxConnections = %ld\n", Conf_MaxConnections);
269 printf( " MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
270 printf( " MaxJoins = %d\n", Conf_MaxJoins>0 ? Conf_MaxJoins : -1);
271 printf( " MaxNickLength = %u\n\n", Conf_MaxNickLength - 1);
273 for( i = 0; i < Conf_Oper_Count; i++ ) {
274 if( ! Conf_Oper[i].name[0] ) continue;
276 /* Valid "Operator" section */
277 puts( "[OPERATOR]" );
278 printf( " Name = %s\n", Conf_Oper[i].name );
279 printf( " Password = %s\n", Conf_Oper[i].pwd );
280 if ( Conf_Oper[i].mask ) printf( " Mask = %s\n", Conf_Oper[i].mask );
284 for( i = 0; i < MAX_SERVERS; i++ ) {
285 if( ! Conf_Server[i].name[0] ) continue;
287 /* Valid "Server" section */
289 printf( " Name = %s\n", Conf_Server[i].name );
290 printf( " Host = %s\n", Conf_Server[i].host );
291 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
293 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
295 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
296 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
297 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
298 printf( " Group = %d\n", Conf_Server[i].group );
299 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
302 for( i = 0; i < Conf_Channel_Count; i++ ) {
303 if( ! Conf_Channel[i].name[0] ) continue;
305 /* Valid "Channel" section */
307 printf( " Name = %s\n", Conf_Channel[i].name );
308 printf( " Modes = %s\n", Conf_Channel[i].modes );
309 printf( " Key = %s\n", Conf_Channel[i].key );
310 printf( " MaxUsers = %lu\n", Conf_Channel[i].maxusers );
312 topic = (char*)array_start(&Conf_Channel[i].topic);
313 printf( " Topic = %s\n\n", topic ? topic : "");
316 return (config_valid ? 0 : 1);
321 Conf_UnsetServer( CONN_ID Idx )
323 /* Set next time for next connection attempt, if this is a server
324 * link that is (still) configured here. If the server is set as
325 * "once", delete it from our configuration.
326 * Non-Server-Connections will be silently ignored. */
331 /* Check all our configured servers */
332 for( i = 0; i < MAX_SERVERS; i++ ) {
333 if( Conf_Server[i].conn_id != Idx ) continue;
335 /* Gotcha! Mark server configuration as "unused": */
336 Conf_Server[i].conn_id = NONE;
338 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
339 /* Delete configuration here */
340 Init_Server_Struct( &Conf_Server[i] );
342 /* Set time for next connect attempt */
344 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
345 /* The connection has been "long", so we don't
346 * require the next attempt to be delayed. */
347 Conf_Server[i].lasttry =
348 t - Conf_ConnectRetry + RECONNECT_DELAY;
350 Conf_Server[i].lasttry = t;
353 } /* Conf_UnsetServer */
357 Conf_SetServer( int ConfServer, CONN_ID Idx )
359 /* Set connection for specified configured server */
361 assert( ConfServer > NONE );
362 assert( Idx > NONE );
364 Conf_Server[ConfServer].conn_id = Idx;
365 } /* Conf_SetServer */
369 Conf_GetServer( CONN_ID Idx )
371 /* Get index of server in configuration structure */
375 assert( Idx > NONE );
377 for( i = 0; i < MAX_SERVERS; i++ ) {
378 if( Conf_Server[i].conn_id == Idx ) return i;
381 } /* Conf_GetServer */
385 Conf_EnableServer( char *Name, UINT16 Port )
387 /* Enable specified server and adjust port */
391 assert( Name != NULL );
393 for( i = 0; i < MAX_SERVERS; i++ ) {
394 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
395 /* Gotcha! Set port and enable server: */
396 Conf_Server[i].port = Port;
397 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
402 } /* Conf_EnableServer */
406 Conf_EnablePassiveServer(const char *Name)
408 /* Enable specified server */
411 assert( Name != NULL );
412 for (i = 0; i < MAX_SERVERS; i++) {
413 if ((strcasecmp( Conf_Server[i].name, Name ) == 0) && (Conf_Server[i].port > 0)) {
414 /* BINGO! Enable server */
415 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
420 } /* Conf_EnablePassiveServer */
424 Conf_DisableServer( char *Name )
426 /* Enable specified server and adjust port */
430 assert( Name != NULL );
432 for( i = 0; i < MAX_SERVERS; i++ ) {
433 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
434 /* Gotcha! Disable and disconnect server: */
435 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
436 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
441 } /* Conf_DisableServer */
445 Conf_AddServer( char *Name, UINT16 Port, char *Host, char *MyPwd, char *PeerPwd )
447 /* Add new server to configuration */
451 assert( Name != NULL );
452 assert( Host != NULL );
453 assert( MyPwd != NULL );
454 assert( PeerPwd != NULL );
456 /* Search unused item in server configuration structure */
457 for( i = 0; i < MAX_SERVERS; i++ ) {
458 /* Is this item used? */
459 if( ! Conf_Server[i].name[0] ) break;
461 if( i >= MAX_SERVERS ) return false;
463 Init_Server_Struct( &Conf_Server[i] );
464 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
465 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
466 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
467 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
468 Conf_Server[i].port = Port;
469 Conf_Server[i].flags = CONF_SFLAG_ONCE;
472 } /* Conf_AddServer */
476 * Check if the given nick name is an service
479 Conf_IsService(int ConfServer, char *Nick)
481 return MatchCaseInsensitive(Conf_Server[ConfServer].svs_mask, Nick);
482 } /* Conf_IsService */
486 Set_Defaults( bool InitServers )
488 /* Initialize configuration variables with default values. */
492 strcpy( Conf_ServerName, "" );
493 snprintf( Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
494 strcpy( Conf_ServerPwd, "" );
496 strcpy( Conf_ServerAdmin1, "" );
497 strcpy( Conf_ServerAdmin2, "" );
498 strcpy( Conf_ServerAdminMail, "" );
500 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
501 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
503 strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
505 strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
507 strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
509 free(Conf_ListenAddress);
510 Conf_ListenAddress = NULL;
511 Conf_UID = Conf_GID = 0;
513 Conf_PingTimeout = 120;
514 Conf_PongTimeout = 20;
516 Conf_ConnectRetry = 60;
519 Conf_Channel_Count = 0;
521 Conf_OperCanMode = false;
523 Conf_NoIdent = false;
524 Conf_PredefChannelsOnly = false;
525 Conf_OperServerMode = false;
527 Conf_ConnectIPv4 = true;
528 Conf_ConnectIPv6 = true;
530 Conf_MaxConnections = 0;
531 Conf_MaxConnectionsIP = 5;
533 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
535 /* Initialize server configuration structures */
536 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
541 Read_Config( bool ngircd_starting )
543 /* Read configuration file. */
545 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
546 const UINT16 defaultport = 6667;
550 /* Open configuration file */
551 fd = fopen( NGIRCd_ConfFile, "r" );
553 /* No configuration file found! */
554 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
555 NGIRCd_ConfFile, strerror( errno ));
556 if (!ngircd_starting)
558 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
562 Set_Defaults( ngircd_starting );
564 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
566 /* Clean up server configuration structure: mark all already
567 * configured servers as "once" so that they are deleted
568 * after the next disconnect and delete all unused servers.
569 * And delete all servers which are "duplicates" of servers
570 * that are already marked as "once" (such servers have been
571 * created by the last rehash but are now useless). */
572 for( i = 0; i < MAX_SERVERS; i++ ) {
573 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
575 /* This structure is in use ... */
576 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
577 /* Check for duplicates */
578 for( n = 0; n < MAX_SERVERS; n++ ) {
579 if( n == i ) continue;
581 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
582 Init_Server_Struct( &Conf_Server[n] );
584 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
590 /* Mark server as "once" */
591 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
592 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
597 /* Initialize variables */
599 strcpy( section, "" );
600 Init_Server_Struct( &New_Server );
601 New_Server_Idx = NONE;
605 /* Read configuration file */
607 if( ! fgets( str, LINE_LEN, fd )) break;
611 /* Skip comments and empty lines */
612 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
614 /* Is this the beginning of a new section? */
615 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
616 strlcpy( section, str, sizeof( section ));
617 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
620 if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
621 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
622 Config_Error( LOG_ERR, "Too many operators configured.");
624 /* Initialize new operator structure */
625 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
626 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
627 if (Conf_Oper[Conf_Oper_Count].mask) {
628 free(Conf_Oper[Conf_Oper_Count].mask );
629 Conf_Oper[Conf_Oper_Count].mask = NULL;
635 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
636 /* Check if there is already a server to add */
637 if( New_Server.name[0] ) {
638 /* Copy data to "real" server structure */
639 assert( New_Server_Idx > NONE );
640 Conf_Server[New_Server_Idx] = New_Server;
643 /* Re-init structure for new server */
644 Init_Server_Struct( &New_Server );
646 /* Search unused item in server configuration structure */
647 for( i = 0; i < MAX_SERVERS; i++ ) {
648 /* Is this item used? */
649 if( ! Conf_Server[i].name[0] ) break;
651 if( i >= MAX_SERVERS ) {
652 /* Oops, no free item found! */
653 Config_Error( LOG_ERR, "Too many servers configured." );
654 New_Server_Idx = NONE;
656 else New_Server_Idx = i;
659 if( strcasecmp( section, "[CHANNEL]" ) == 0 ) {
660 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) {
661 Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
663 /* Initialize new channel structure */
664 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
665 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
666 strcpy( Conf_Channel[Conf_Channel_Count].key, "" );
667 Conf_Channel[Conf_Channel_Count].maxusers = 0;
668 array_free(&Conf_Channel[Conf_Channel_Count].topic);
669 Conf_Channel_Count++;
673 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
676 if( section[0] == 0x1 ) continue;
678 /* Split line into variable name and parameters */
679 ptr = strchr( str, '=' );
681 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
685 var = str; ngt_TrimStr( var );
686 arg = ptr + 1; ngt_TrimStr( arg );
688 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
689 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
690 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
691 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
692 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
695 /* Close configuration file */
698 /* Check if there is still a server to add */
699 if( New_Server.name[0] ) {
700 /* Copy data to "real" server structure */
701 assert( New_Server_Idx > NONE );
702 Conf_Server[New_Server_Idx] = New_Server;
705 if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
706 if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
707 Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
708 (unsigned int) defaultport, strerror(errno));
713 if (!Conf_ListenAddress)
714 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
716 if (!Conf_ListenAddress) {
717 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
725 Check_ArgIsTrue( const char *Arg )
727 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
728 if( strcasecmp( Arg, "true" ) == 0 ) return true;
729 if( atoi( Arg ) != 0 ) return true;
732 } /* Check_ArgIsTrue */
735 static unsigned int Handle_MaxNickLength(int Line, const char *Arg)
739 new = (unsigned) atoi(Arg) + 1;
740 if (new > CLIENT_NICK_LEN) {
741 Config_Error(LOG_WARNING,
742 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
743 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
744 return CLIENT_NICK_LEN;
747 Config_Error(LOG_WARNING,
748 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
749 NGIRCd_ConfFile, Line);
753 } /* Handle_MaxNickLength */
757 Handle_GLOBAL( int Line, char *Var, char *Arg )
764 assert( Var != NULL );
765 assert( Arg != NULL );
767 if( strcasecmp( Var, "Name" ) == 0 ) {
769 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
770 if (len >= sizeof( Conf_ServerName ))
771 Config_Error_TooLong( Line, Var );
774 if( strcasecmp( Var, "Info" ) == 0 ) {
775 /* Info text of server */
776 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
777 if (len >= sizeof( Conf_ServerInfo ))
778 Config_Error_TooLong ( Line, Var );
781 if( strcasecmp( Var, "Password" ) == 0 ) {
782 /* Global server password */
783 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
784 if (len >= sizeof( Conf_ServerPwd ))
785 Config_Error_TooLong( Line, Var );
788 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
789 /* Administrative info #1 */
790 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
791 if (len >= sizeof( Conf_ServerAdmin1 ))
792 Config_Error_TooLong ( Line, Var );
795 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
796 /* Administrative info #2 */
797 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
798 if (len >= sizeof( Conf_ServerAdmin2 ))
799 Config_Error_TooLong ( Line, Var );
802 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
803 /* Administrative email contact */
804 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
805 if (len >= sizeof( Conf_ServerAdminMail ))
806 Config_Error_TooLong( Line, Var );
810 if( strcasecmp( Var, "Ports" ) == 0 ) {
811 ports_parse(&Conf_ListenPorts, Line, Arg);
814 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
815 /* "Message of the day" (MOTD) file */
816 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
817 if (len >= sizeof( Conf_MotdFile ))
818 Config_Error_TooLong( Line, Var );
821 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
822 /* "Message of the day" phrase (instead of file) */
823 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
824 if (len >= sizeof( Conf_MotdPhrase ))
825 Config_Error_TooLong( Line, Var );
828 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
829 /* directory for chroot() */
830 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
831 if (len >= sizeof( Conf_Chroot ))
832 Config_Error_TooLong( Line, Var );
835 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
836 /* name of pidfile */
837 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
838 if (len >= sizeof( Conf_PidFile ))
839 Config_Error_TooLong( Line, Var );
842 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
843 /* UID the daemon should switch to */
844 pwd = getpwnam( Arg );
845 if( pwd ) Conf_UID = pwd->pw_uid;
848 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
851 Conf_UID = (unsigned int)atoi( Arg );
855 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
856 /* GID the daemon should use */
857 grp = getgrnam( Arg );
858 if( grp ) Conf_GID = grp->gr_gid;
861 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
864 Conf_GID = (unsigned int)atoi( Arg );
868 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
870 Conf_PingTimeout = atoi( Arg );
871 if( Conf_PingTimeout < 5 ) {
872 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
873 NGIRCd_ConfFile, Line );
874 Conf_PingTimeout = 5;
878 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
880 Conf_PongTimeout = atoi( Arg );
881 if( Conf_PongTimeout < 5 ) {
882 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
883 NGIRCd_ConfFile, Line );
884 Conf_PongTimeout = 5;
888 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
889 /* Seconds between connection attempts to other servers */
890 Conf_ConnectRetry = atoi( Arg );
891 if( Conf_ConnectRetry < 5 ) {
892 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
893 NGIRCd_ConfFile, Line );
894 Conf_ConnectRetry = 5;
898 if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
899 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
900 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
903 if( strcasecmp( Var, "NoDNS" ) == 0 ) {
904 /* don't do reverse dns lookups when clients connect? */
905 Conf_NoDNS = Check_ArgIsTrue( Arg );
908 if (strcasecmp(Var, "NoIdent") == 0) {
909 /* don't do IDENT lookups when clients connect? */
910 Conf_NoIdent = Check_ArgIsTrue(Arg);
913 /* user has enabled ident lookups explicitly, but ... */
914 Config_Error(LOG_WARNING,
915 "%s: line %d: NoIdent=False, but ngircd was built without IDENT support",
916 NGIRCd_ConfFile, Line);
922 /* the default setting for all the WANT_IPV6 special options is 'true' */
923 if( strcasecmp( Var, "ConnectIPv6" ) == 0 ) {
924 /* connect to other hosts using ipv6, if they have an AAAA record? */
925 Conf_ConnectIPv6 = Check_ArgIsTrue( Arg );
928 if( strcasecmp( Var, "ConnectIPv4" ) == 0 ) {
929 /* connect to other hosts using ipv4.
930 * again, this can be used for ipv6-only setups */
931 Conf_ConnectIPv4 = Check_ArgIsTrue( Arg );
935 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
936 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
937 Conf_OperCanMode = Check_ArgIsTrue( Arg );
940 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
941 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
942 Conf_OperServerMode = Check_ArgIsTrue( Arg );
945 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
946 /* Maximum number of connections. 0 -> "no limit". */
948 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
951 Conf_MaxConnections = atol( Arg );
954 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
955 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
957 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
960 Conf_MaxConnectionsIP = atoi( Arg );
963 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
964 /* Maximum number of channels a user can join. 0 -> "no limit". */
966 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
969 Conf_MaxJoins = atoi( Arg );
972 if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
973 /* Maximum length of a nick name; must be same on all servers
974 * within the IRC network! */
975 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
979 if( strcasecmp( Var, "Listen" ) == 0 ) {
980 /* IP-Address to bind sockets */
981 if (Conf_ListenAddress) {
982 Config_Error(LOG_ERR, "Multiple Listen= options, ignoring: %s", Arg);
985 Conf_ListenAddress = strdup_warn(Arg);
987 * if allocation fails, we're in trouble:
988 * we cannot ignore the error -- otherwise ngircd
989 * would listen on all interfaces.
991 if (!Conf_ListenAddress) {
992 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
999 if( strcasecmp( Var, "SSLPorts" ) == 0 ) {
1000 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1004 if( strcasecmp( Var, "SSLKeyFile" ) == 0 ) {
1005 assert(Conf_SSLOptions.KeyFile == NULL );
1006 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1009 if( strcasecmp( Var, "SSLCertFile" ) == 0 ) {
1010 assert(Conf_SSLOptions.CertFile == NULL );
1011 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1015 if( strcasecmp( Var, "SSLKeyFilePassword" ) == 0 ) {
1016 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1017 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1018 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Could not copy %s: %s!",
1019 NGIRCd_ConfFile, Line, Var, strerror(errno));
1022 if( strcasecmp( Var, "SSLDHFile" ) == 0 ) {
1023 assert(Conf_SSLOptions.DHFile == NULL);
1024 Conf_SSLOptions.DHFile = strdup_warn( Arg );
1028 Config_Error(LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
1029 NGIRCd_ConfFile, Line, Var);
1030 } /* Handle_GLOBAL */
1034 Handle_OPERATOR( int Line, char *Var, char *Arg )
1036 unsigned int opercount;
1039 assert( Var != NULL );
1040 assert( Arg != NULL );
1041 assert( Conf_Oper_Count > 0 );
1043 if ( Conf_Oper_Count == 0 )
1046 opercount = Conf_Oper_Count - 1;
1048 if( strcasecmp( Var, "Name" ) == 0 ) {
1049 /* Name of IRC operator */
1050 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
1051 if (len >= sizeof( Conf_Oper[opercount].name ))
1052 Config_Error_TooLong( Line, Var );
1055 if( strcasecmp( Var, "Password" ) == 0 ) {
1056 /* Password of IRC operator */
1057 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
1058 if (len >= sizeof( Conf_Oper[opercount].pwd ))
1059 Config_Error_TooLong( Line, Var );
1062 if( strcasecmp( Var, "Mask" ) == 0 ) {
1063 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
1065 Conf_Oper[opercount].mask = strdup_warn( Arg );
1068 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
1069 NGIRCd_ConfFile, Line, Var );
1070 } /* Handle_OPERATOR */
1074 Handle_SERVER( int Line, char *Var, char *Arg )
1080 assert( Var != NULL );
1081 assert( Arg != NULL );
1083 /* Ignore server block if no space is left in server configuration structure */
1084 if( New_Server_Idx <= NONE ) return;
1086 if( strcasecmp( Var, "Host" ) == 0 ) {
1087 /* Hostname of the server */
1088 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1089 if (len >= sizeof( New_Server.host ))
1090 Config_Error_TooLong ( Line, Var );
1093 if( strcasecmp( Var, "Name" ) == 0 ) {
1094 /* Name of the server ("Nick"/"ID") */
1095 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1096 if (len >= sizeof( New_Server.name ))
1097 Config_Error_TooLong( Line, Var );
1100 if (strcasecmp(Var, "Bind") == 0) {
1101 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1104 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1105 NGIRCd_ConfFile, Line, Arg);
1108 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1109 /* Password of this server which is sent to the peer */
1111 Config_Error(LOG_ERR,
1112 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1113 NGIRCd_ConfFile, Line);
1115 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1116 if (len >= sizeof( New_Server.pwd_in ))
1117 Config_Error_TooLong( Line, Var );
1120 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1121 /* Passwort of the peer which must be received */
1122 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1123 if (len >= sizeof( New_Server.pwd_out ))
1124 Config_Error_TooLong( Line, Var );
1127 if( strcasecmp( Var, "Port" ) == 0 ) {
1128 /* Port to which this server should connect */
1130 if( port > 0 && port < 0xFFFF )
1131 New_Server.port = (UINT16)port;
1133 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
1134 NGIRCd_ConfFile, Line, port );
1138 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1139 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1143 if( strcasecmp( Var, "Group" ) == 0 ) {
1146 if( ! isdigit( (int)*Arg ))
1147 Config_Error_NaN( Line, Var );
1150 New_Server.group = atoi( Arg );
1153 if( strcasecmp( Var, "Passive" ) == 0 ) {
1154 if (Check_ArgIsTrue(Arg))
1155 New_Server.flags |= CONF_SFLAG_DISABLED;
1158 if (strcasecmp(Var, "ServiceMask") == 0) {
1159 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1160 sizeof(New_Server.svs_mask));
1161 if (len >= sizeof(New_Server.svs_mask))
1162 Config_Error_TooLong(Line, Var);
1166 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
1167 NGIRCd_ConfFile, Line, Var );
1168 } /* Handle_SERVER */
1172 Handle_Channelname(size_t chancount, const char *name)
1174 size_t size = sizeof( Conf_Channel[chancount].name );
1175 char *dest = Conf_Channel[chancount].name;
1177 if (*name && *name != '#') {
1182 return size > strlcpy(dest, name, size);
1187 Handle_CHANNEL( int Line, char *Var, char *Arg )
1190 size_t chancount = 0;
1193 assert( Var != NULL );
1194 assert( Arg != NULL );
1195 if (Conf_Channel_Count > 0)
1196 chancount = Conf_Channel_Count - 1;
1198 if( strcasecmp( Var, "Name" ) == 0 ) {
1199 if (!Handle_Channelname(chancount, Arg))
1200 Config_Error_TooLong( Line, Var );
1203 if( strcasecmp( Var, "Modes" ) == 0 ) {
1205 len = strlcpy( Conf_Channel[chancount].modes, Arg, sizeof( Conf_Channel[chancount].modes ));
1206 if (len >= sizeof( Conf_Channel[chancount].modes ))
1207 Config_Error_TooLong( Line, Var );
1210 if( strcasecmp( Var, "Topic" ) == 0 ) {
1212 if (!array_copys( &Conf_Channel[chancount].topic, Arg))
1213 Config_Error_TooLong( Line, Var );
1217 if( strcasecmp( Var, "Key" ) == 0 ) {
1218 /* Initial Channel Key (mode k) */
1219 len = strlcpy(Conf_Channel[chancount].key, Arg, sizeof(Conf_Channel[chancount].key));
1220 if (len >= sizeof( Conf_Channel[chancount].key ))
1221 Config_Error_TooLong(Line, Var);
1225 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1226 /* maximum user limit, mode l */
1227 Conf_Channel[chancount].maxusers = (unsigned long) atol(Arg);
1228 if (Conf_Channel[chancount].maxusers == 0)
1229 Config_Error_NaN(Line, Var);
1233 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1234 NGIRCd_ConfFile, Line, Var );
1235 } /* Handle_CHANNEL */
1239 Validate_Config(bool Configtest, bool Rehash)
1241 /* Validate configuration settings. */
1244 int i, servers, servers_once;
1246 bool config_valid = true;
1249 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1250 ptr = Conf_ServerName;
1252 if (*ptr >= 'a' && *ptr <= 'z') continue;
1253 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1254 if (*ptr >= '0' && *ptr <= '9') continue;
1255 if (ptr > Conf_ServerName) {
1256 if (*ptr == '.' || *ptr == '-')
1259 Conf_ServerName[0] = '\0';
1263 if (!Conf_ServerName[0]) {
1264 /* No server name configured! */
1265 config_valid = false;
1266 Config_Error(LOG_ALERT,
1267 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1269 if (!Configtest && !Rehash) {
1270 Config_Error(LOG_ALERT,
1271 "%s exiting due to fatal errors!",
1277 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1278 /* No dot in server name! */
1279 config_valid = false;
1280 Config_Error(LOG_ALERT,
1281 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1284 Config_Error(LOG_ALERT,
1285 "%s exiting due to fatal errors!",
1292 if (!Conf_ServerAdminMail[0]) {
1293 /* No administrative contact configured! */
1294 config_valid = false;
1295 Config_Error(LOG_ALERT,
1296 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1299 Config_Error(LOG_ALERT,
1300 "%s exiting due to fatal errors!",
1307 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1308 && !Conf_ServerAdminMail[0]) {
1309 /* No administrative information configured! */
1310 Config_Error(LOG_WARNING,
1311 "No administrative information configured but required by RFC!");
1315 servers = servers_once = 0;
1316 for (i = 0; i < MAX_SERVERS; i++) {
1317 if (Conf_Server[i].name[0]) {
1319 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1324 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1325 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1328 return config_valid;
1329 } /* Validate_Config */
1333 Config_Error_TooLong ( const int Line, const char *Item )
1335 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1340 Config_Error_NaN( const int Line, const char *Item )
1342 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1343 NGIRCd_ConfFile, Line, Item );
1348 static void Config_Error( const int Level, const char *Format, ... )
1350 static void Config_Error( Level, Format, va_alist )
1356 /* Error! Write to console and/or logfile. */
1358 char msg[MAX_LOG_MSG_LEN];
1361 assert( Format != NULL );
1364 va_start( ap, Format );
1368 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1371 /* During "normal operations" the log functions of the daemon should
1372 * be used, but during testing of the configuration file, all messages
1373 * should go directly to the console: */
1374 if (Use_Log) Log( Level, "%s", msg );
1376 } /* Config_Error */
1380 Init_Server_Struct( CONF_SERVER *Server )
1382 /* Initialize server configuration structur to default values */
1384 assert( Server != NULL );
1386 memset( Server, 0, sizeof (CONF_SERVER) );
1388 Server->group = NONE;
1389 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1391 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1393 Resolve_Init(&Server->res_stat);
1394 Server->conn_id = NONE;
1395 memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
1396 } /* Init_Server_Struct */