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));
264 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
265 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
267 printf( " MaxConnections = %ld\n", Conf_MaxConnections);
268 printf( " MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
269 printf( " MaxJoins = %d\n", Conf_MaxJoins>0 ? Conf_MaxJoins : -1);
270 printf( " MaxNickLength = %u\n\n", Conf_MaxNickLength - 1);
272 for( i = 0; i < Conf_Oper_Count; i++ ) {
273 if( ! Conf_Oper[i].name[0] ) continue;
275 /* Valid "Operator" section */
276 puts( "[OPERATOR]" );
277 printf( " Name = %s\n", Conf_Oper[i].name );
278 printf( " Password = %s\n", Conf_Oper[i].pwd );
279 if ( Conf_Oper[i].mask ) printf( " Mask = %s\n", Conf_Oper[i].mask );
283 for( i = 0; i < MAX_SERVERS; i++ ) {
284 if( ! Conf_Server[i].name[0] ) continue;
286 /* Valid "Server" section */
288 printf( " Name = %s\n", Conf_Server[i].name );
289 printf( " Host = %s\n", Conf_Server[i].host );
290 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
292 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
294 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
295 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
296 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
297 printf( " Group = %d\n", Conf_Server[i].group );
298 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
301 for( i = 0; i < Conf_Channel_Count; i++ ) {
302 if( ! Conf_Channel[i].name[0] ) continue;
304 /* Valid "Channel" section */
306 printf( " Name = %s\n", Conf_Channel[i].name );
307 printf( " Modes = %s\n", Conf_Channel[i].modes );
308 printf( " Key = %s\n", Conf_Channel[i].key );
309 printf( " MaxUsers = %lu\n", Conf_Channel[i].maxusers );
311 topic = (char*)array_start(&Conf_Channel[i].topic);
312 printf( " Topic = %s\n\n", topic ? topic : "");
315 return (config_valid ? 0 : 1);
320 Conf_UnsetServer( CONN_ID Idx )
322 /* Set next time for next connection attempt, if this is a server
323 * link that is (still) configured here. If the server is set as
324 * "once", delete it from our configuration.
325 * Non-Server-Connections will be silently ignored. */
330 /* Check all our configured servers */
331 for( i = 0; i < MAX_SERVERS; i++ ) {
332 if( Conf_Server[i].conn_id != Idx ) continue;
334 /* Gotcha! Mark server configuration as "unused": */
335 Conf_Server[i].conn_id = NONE;
337 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
338 /* Delete configuration here */
339 Init_Server_Struct( &Conf_Server[i] );
341 /* Set time for next connect attempt */
343 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
344 /* The connection has been "long", so we don't
345 * require the next attempt to be delayed. */
346 Conf_Server[i].lasttry =
347 t - Conf_ConnectRetry + RECONNECT_DELAY;
349 Conf_Server[i].lasttry = t;
352 } /* Conf_UnsetServer */
356 Conf_SetServer( int ConfServer, CONN_ID Idx )
358 /* Set connection for specified configured server */
360 assert( ConfServer > NONE );
361 assert( Idx > NONE );
363 Conf_Server[ConfServer].conn_id = Idx;
364 } /* Conf_SetServer */
368 Conf_GetServer( CONN_ID Idx )
370 /* Get index of server in configuration structure */
374 assert( Idx > NONE );
376 for( i = 0; i < MAX_SERVERS; i++ ) {
377 if( Conf_Server[i].conn_id == Idx ) return i;
380 } /* Conf_GetServer */
384 Conf_EnableServer( char *Name, UINT16 Port )
386 /* Enable specified server and adjust port */
390 assert( Name != NULL );
392 for( i = 0; i < MAX_SERVERS; i++ ) {
393 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
394 /* Gotcha! Set port and enable server: */
395 Conf_Server[i].port = Port;
396 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
401 } /* Conf_EnableServer */
405 Conf_EnablePassiveServer(const char *Name)
407 /* Enable specified server */
410 assert( Name != NULL );
411 for (i = 0; i < MAX_SERVERS; i++) {
412 if ((strcasecmp( Conf_Server[i].name, Name ) == 0) && (Conf_Server[i].port > 0)) {
413 /* BINGO! Enable server */
414 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
419 } /* Conf_EnablePassiveServer */
423 Conf_DisableServer( char *Name )
425 /* Enable specified server and adjust port */
429 assert( Name != NULL );
431 for( i = 0; i < MAX_SERVERS; i++ ) {
432 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
433 /* Gotcha! Disable and disconnect server: */
434 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
435 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
440 } /* Conf_DisableServer */
444 Conf_AddServer( char *Name, UINT16 Port, char *Host, char *MyPwd, char *PeerPwd )
446 /* Add new server to configuration */
450 assert( Name != NULL );
451 assert( Host != NULL );
452 assert( MyPwd != NULL );
453 assert( PeerPwd != NULL );
455 /* Search unused item in server configuration structure */
456 for( i = 0; i < MAX_SERVERS; i++ ) {
457 /* Is this item used? */
458 if( ! Conf_Server[i].name[0] ) break;
460 if( i >= MAX_SERVERS ) return false;
462 Init_Server_Struct( &Conf_Server[i] );
463 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
464 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
465 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
466 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
467 Conf_Server[i].port = Port;
468 Conf_Server[i].flags = CONF_SFLAG_ONCE;
471 } /* Conf_AddServer */
475 * Check if the given nick name is an service
478 Conf_IsService(int ConfServer, char *Nick)
480 return MatchCaseInsensitive(Conf_Server[ConfServer].svs_mask, Nick);
481 } /* Conf_IsService */
485 Set_Defaults( bool InitServers )
487 /* Initialize configuration variables with default values. */
491 strcpy( Conf_ServerName, "" );
492 snprintf( Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
493 strcpy( Conf_ServerPwd, "" );
495 strcpy( Conf_ServerAdmin1, "" );
496 strcpy( Conf_ServerAdmin2, "" );
497 strcpy( Conf_ServerAdminMail, "" );
499 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
500 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
502 strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
504 strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
506 strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
508 free(Conf_ListenAddress);
509 Conf_ListenAddress = NULL;
510 Conf_UID = Conf_GID = 0;
512 Conf_PingTimeout = 120;
513 Conf_PongTimeout = 20;
515 Conf_ConnectRetry = 60;
518 Conf_Channel_Count = 0;
520 Conf_OperCanMode = false;
522 Conf_PredefChannelsOnly = false;
523 Conf_OperServerMode = false;
525 Conf_ConnectIPv4 = true;
526 Conf_ConnectIPv6 = true;
528 Conf_MaxConnections = 0;
529 Conf_MaxConnectionsIP = 5;
531 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
533 /* Initialize server configuration structures */
534 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
539 Read_Config( bool ngircd_starting )
541 /* Read configuration file. */
543 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
544 const UINT16 defaultport = 6667;
548 /* Open configuration file */
549 fd = fopen( NGIRCd_ConfFile, "r" );
551 /* No configuration file found! */
552 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
553 NGIRCd_ConfFile, strerror( errno ));
554 if (!ngircd_starting)
556 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
560 Set_Defaults( ngircd_starting );
562 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
564 /* Clean up server configuration structure: mark all already
565 * configured servers as "once" so that they are deleted
566 * after the next disconnect and delete all unused servers.
567 * And delete all servers which are "duplicates" of servers
568 * that are already marked as "once" (such servers have been
569 * created by the last rehash but are now useless). */
570 for( i = 0; i < MAX_SERVERS; i++ ) {
571 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
573 /* This structure is in use ... */
574 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
575 /* Check for duplicates */
576 for( n = 0; n < MAX_SERVERS; n++ ) {
577 if( n == i ) continue;
579 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
580 Init_Server_Struct( &Conf_Server[n] );
582 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
588 /* Mark server as "once" */
589 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
590 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
595 /* Initialize variables */
597 strcpy( section, "" );
598 Init_Server_Struct( &New_Server );
599 New_Server_Idx = NONE;
603 /* Read configuration file */
605 if( ! fgets( str, LINE_LEN, fd )) break;
609 /* Skip comments and empty lines */
610 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
612 /* Is this the beginning of a new section? */
613 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
614 strlcpy( section, str, sizeof( section ));
615 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
618 if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
619 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
620 Config_Error( LOG_ERR, "Too many operators configured.");
622 /* Initialize new operator structure */
623 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
624 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
625 if (Conf_Oper[Conf_Oper_Count].mask) {
626 free(Conf_Oper[Conf_Oper_Count].mask );
627 Conf_Oper[Conf_Oper_Count].mask = NULL;
633 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
634 /* Check if there is already a server to add */
635 if( New_Server.name[0] ) {
636 /* Copy data to "real" server structure */
637 assert( New_Server_Idx > NONE );
638 Conf_Server[New_Server_Idx] = New_Server;
641 /* Re-init structure for new server */
642 Init_Server_Struct( &New_Server );
644 /* Search unused item in server configuration structure */
645 for( i = 0; i < MAX_SERVERS; i++ ) {
646 /* Is this item used? */
647 if( ! Conf_Server[i].name[0] ) break;
649 if( i >= MAX_SERVERS ) {
650 /* Oops, no free item found! */
651 Config_Error( LOG_ERR, "Too many servers configured." );
652 New_Server_Idx = NONE;
654 else New_Server_Idx = i;
657 if( strcasecmp( section, "[CHANNEL]" ) == 0 ) {
658 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) {
659 Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
661 /* Initialize new channel structure */
662 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
663 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
664 strcpy( Conf_Channel[Conf_Channel_Count].key, "" );
665 Conf_Channel[Conf_Channel_Count].maxusers = 0;
666 array_free(&Conf_Channel[Conf_Channel_Count].topic);
667 Conf_Channel_Count++;
671 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
674 if( section[0] == 0x1 ) continue;
676 /* Split line into variable name and parameters */
677 ptr = strchr( str, '=' );
679 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
683 var = str; ngt_TrimStr( var );
684 arg = ptr + 1; ngt_TrimStr( arg );
686 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
687 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
688 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
689 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
690 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
693 /* Close configuration file */
696 /* Check if there is still a server to add */
697 if( New_Server.name[0] ) {
698 /* Copy data to "real" server structure */
699 assert( New_Server_Idx > NONE );
700 Conf_Server[New_Server_Idx] = New_Server;
703 if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
704 if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
705 Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
706 (unsigned int) defaultport, strerror(errno));
711 if (!Conf_ListenAddress)
712 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
714 if (!Conf_ListenAddress) {
715 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
723 Check_ArgIsTrue( const char *Arg )
725 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
726 if( strcasecmp( Arg, "true" ) == 0 ) return true;
727 if( atoi( Arg ) != 0 ) return true;
730 } /* Check_ArgIsTrue */
733 static unsigned int Handle_MaxNickLength(int Line, const char *Arg)
737 new = (unsigned) atoi(Arg) + 1;
738 if (new > CLIENT_NICK_LEN) {
739 Config_Error(LOG_WARNING,
740 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
741 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
742 return CLIENT_NICK_LEN;
745 Config_Error(LOG_WARNING,
746 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
747 NGIRCd_ConfFile, Line);
751 } /* Handle_MaxNickLength */
755 Handle_GLOBAL( int Line, char *Var, char *Arg )
762 assert( Var != NULL );
763 assert( Arg != NULL );
765 if( strcasecmp( Var, "Name" ) == 0 ) {
767 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
768 if (len >= sizeof( Conf_ServerName ))
769 Config_Error_TooLong( Line, Var );
772 if( strcasecmp( Var, "Info" ) == 0 ) {
773 /* Info text of server */
774 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
775 if (len >= sizeof( Conf_ServerInfo ))
776 Config_Error_TooLong ( Line, Var );
779 if( strcasecmp( Var, "Password" ) == 0 ) {
780 /* Global server password */
781 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
782 if (len >= sizeof( Conf_ServerPwd ))
783 Config_Error_TooLong( Line, Var );
786 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
787 /* Administrative info #1 */
788 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
789 if (len >= sizeof( Conf_ServerAdmin1 ))
790 Config_Error_TooLong ( Line, Var );
793 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
794 /* Administrative info #2 */
795 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
796 if (len >= sizeof( Conf_ServerAdmin2 ))
797 Config_Error_TooLong ( Line, Var );
800 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
801 /* Administrative email contact */
802 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
803 if (len >= sizeof( Conf_ServerAdminMail ))
804 Config_Error_TooLong( Line, Var );
808 if( strcasecmp( Var, "Ports" ) == 0 ) {
809 ports_parse(&Conf_ListenPorts, Line, Arg);
812 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
813 /* "Message of the day" (MOTD) file */
814 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
815 if (len >= sizeof( Conf_MotdFile ))
816 Config_Error_TooLong( Line, Var );
819 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
820 /* "Message of the day" phrase (instead of file) */
821 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
822 if (len >= sizeof( Conf_MotdPhrase ))
823 Config_Error_TooLong( Line, Var );
826 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
827 /* directory for chroot() */
828 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
829 if (len >= sizeof( Conf_Chroot ))
830 Config_Error_TooLong( Line, Var );
833 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
834 /* name of pidfile */
835 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
836 if (len >= sizeof( Conf_PidFile ))
837 Config_Error_TooLong( Line, Var );
840 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
841 /* UID the daemon should switch to */
842 pwd = getpwnam( Arg );
843 if( pwd ) Conf_UID = pwd->pw_uid;
846 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
849 Conf_UID = (unsigned int)atoi( Arg );
853 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
854 /* GID the daemon should use */
855 grp = getgrnam( Arg );
856 if( grp ) Conf_GID = grp->gr_gid;
859 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
862 Conf_GID = (unsigned int)atoi( Arg );
866 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
868 Conf_PingTimeout = atoi( Arg );
869 if( Conf_PingTimeout < 5 ) {
870 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
871 NGIRCd_ConfFile, Line );
872 Conf_PingTimeout = 5;
876 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
878 Conf_PongTimeout = atoi( Arg );
879 if( Conf_PongTimeout < 5 ) {
880 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
881 NGIRCd_ConfFile, Line );
882 Conf_PongTimeout = 5;
886 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
887 /* Seconds between connection attempts to other servers */
888 Conf_ConnectRetry = atoi( Arg );
889 if( Conf_ConnectRetry < 5 ) {
890 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
891 NGIRCd_ConfFile, Line );
892 Conf_ConnectRetry = 5;
896 if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
897 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
898 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
901 if( strcasecmp( Var, "NoDNS" ) == 0 ) {
902 /* don't do reverse dns lookups when clients connect? */
903 Conf_NoDNS = Check_ArgIsTrue( Arg );
907 /* the default setting for all the WANT_IPV6 special options is 'true' */
908 if( strcasecmp( Var, "ConnectIPv6" ) == 0 ) {
909 /* connect to other hosts using ipv6, if they have an AAAA record? */
910 Conf_ConnectIPv6 = Check_ArgIsTrue( Arg );
913 if( strcasecmp( Var, "ConnectIPv4" ) == 0 ) {
914 /* connect to other hosts using ipv4.
915 * again, this can be used for ipv6-only setups */
916 Conf_ConnectIPv4 = Check_ArgIsTrue( Arg );
920 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
921 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
922 Conf_OperCanMode = Check_ArgIsTrue( Arg );
925 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
926 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
927 Conf_OperServerMode = Check_ArgIsTrue( Arg );
930 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
931 /* Maximum number of connections. 0 -> "no limit". */
933 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
936 Conf_MaxConnections = atol( Arg );
939 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
940 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
942 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
945 Conf_MaxConnectionsIP = atoi( Arg );
948 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
949 /* Maximum number of channels a user can join. 0 -> "no limit". */
951 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
954 Conf_MaxJoins = atoi( Arg );
957 if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
958 /* Maximum length of a nick name; must be same on all servers
959 * within the IRC network! */
960 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
964 if( strcasecmp( Var, "Listen" ) == 0 ) {
965 /* IP-Address to bind sockets */
966 if (Conf_ListenAddress) {
967 Config_Error(LOG_ERR, "Multiple Listen= options, ignoring: %s", Arg);
970 Conf_ListenAddress = strdup_warn(Arg);
972 * if allocation fails, we're in trouble:
973 * we cannot ignore the error -- otherwise ngircd
974 * would listen on all interfaces.
976 if (!Conf_ListenAddress) {
977 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
984 if( strcasecmp( Var, "SSLPorts" ) == 0 ) {
985 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
989 if( strcasecmp( Var, "SSLKeyFile" ) == 0 ) {
990 assert(Conf_SSLOptions.KeyFile == NULL );
991 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
994 if( strcasecmp( Var, "SSLCertFile" ) == 0 ) {
995 assert(Conf_SSLOptions.CertFile == NULL );
996 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1000 if( strcasecmp( Var, "SSLKeyFilePassword" ) == 0 ) {
1001 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1002 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1003 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Could not copy %s: %s!",
1004 NGIRCd_ConfFile, Line, Var, strerror(errno));
1007 if( strcasecmp( Var, "SSLDHFile" ) == 0 ) {
1008 assert(Conf_SSLOptions.DHFile == NULL);
1009 Conf_SSLOptions.DHFile = strdup_warn( Arg );
1013 Config_Error(LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
1014 NGIRCd_ConfFile, Line, Var);
1015 } /* Handle_GLOBAL */
1019 Handle_OPERATOR( int Line, char *Var, char *Arg )
1021 unsigned int opercount;
1024 assert( Var != NULL );
1025 assert( Arg != NULL );
1026 assert( Conf_Oper_Count > 0 );
1028 if ( Conf_Oper_Count == 0 )
1031 opercount = Conf_Oper_Count - 1;
1033 if( strcasecmp( Var, "Name" ) == 0 ) {
1034 /* Name of IRC operator */
1035 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
1036 if (len >= sizeof( Conf_Oper[opercount].name ))
1037 Config_Error_TooLong( Line, Var );
1040 if( strcasecmp( Var, "Password" ) == 0 ) {
1041 /* Password of IRC operator */
1042 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
1043 if (len >= sizeof( Conf_Oper[opercount].pwd ))
1044 Config_Error_TooLong( Line, Var );
1047 if( strcasecmp( Var, "Mask" ) == 0 ) {
1048 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
1050 Conf_Oper[opercount].mask = strdup_warn( Arg );
1053 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
1054 NGIRCd_ConfFile, Line, Var );
1055 } /* Handle_OPERATOR */
1059 Handle_SERVER( int Line, char *Var, char *Arg )
1065 assert( Var != NULL );
1066 assert( Arg != NULL );
1068 /* Ignore server block if no space is left in server configuration structure */
1069 if( New_Server_Idx <= NONE ) return;
1071 if( strcasecmp( Var, "Host" ) == 0 ) {
1072 /* Hostname of the server */
1073 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1074 if (len >= sizeof( New_Server.host ))
1075 Config_Error_TooLong ( Line, Var );
1078 if( strcasecmp( Var, "Name" ) == 0 ) {
1079 /* Name of the server ("Nick"/"ID") */
1080 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1081 if (len >= sizeof( New_Server.name ))
1082 Config_Error_TooLong( Line, Var );
1085 if (strcasecmp(Var, "Bind") == 0) {
1086 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1089 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1090 NGIRCd_ConfFile, Line, Arg);
1093 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1094 /* Password of this server which is sent to the peer */
1096 Config_Error(LOG_ERR,
1097 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1098 NGIRCd_ConfFile, Line);
1100 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1101 if (len >= sizeof( New_Server.pwd_in ))
1102 Config_Error_TooLong( Line, Var );
1105 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1106 /* Passwort of the peer which must be received */
1107 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1108 if (len >= sizeof( New_Server.pwd_out ))
1109 Config_Error_TooLong( Line, Var );
1112 if( strcasecmp( Var, "Port" ) == 0 ) {
1113 /* Port to which this server should connect */
1115 if( port > 0 && port < 0xFFFF )
1116 New_Server.port = (UINT16)port;
1118 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
1119 NGIRCd_ConfFile, Line, port );
1123 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1124 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1128 if( strcasecmp( Var, "Group" ) == 0 ) {
1131 if( ! isdigit( (int)*Arg ))
1132 Config_Error_NaN( Line, Var );
1135 New_Server.group = atoi( Arg );
1138 if( strcasecmp( Var, "Passive" ) == 0 ) {
1139 if (Check_ArgIsTrue(Arg))
1140 New_Server.flags |= CONF_SFLAG_DISABLED;
1143 if (strcasecmp(Var, "ServiceMask") == 0) {
1144 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1145 sizeof(New_Server.svs_mask));
1146 if (len >= sizeof(New_Server.svs_mask))
1147 Config_Error_TooLong(Line, Var);
1151 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
1152 NGIRCd_ConfFile, Line, Var );
1153 } /* Handle_SERVER */
1157 Handle_Channelname(size_t chancount, const char *name)
1159 size_t size = sizeof( Conf_Channel[chancount].name );
1160 char *dest = Conf_Channel[chancount].name;
1162 if (*name && *name != '#') {
1167 return size > strlcpy(dest, name, size);
1172 Handle_CHANNEL( int Line, char *Var, char *Arg )
1175 size_t chancount = 0;
1178 assert( Var != NULL );
1179 assert( Arg != NULL );
1180 if (Conf_Channel_Count > 0)
1181 chancount = Conf_Channel_Count - 1;
1183 if( strcasecmp( Var, "Name" ) == 0 ) {
1184 if (!Handle_Channelname(chancount, Arg))
1185 Config_Error_TooLong( Line, Var );
1188 if( strcasecmp( Var, "Modes" ) == 0 ) {
1190 len = strlcpy( Conf_Channel[chancount].modes, Arg, sizeof( Conf_Channel[chancount].modes ));
1191 if (len >= sizeof( Conf_Channel[chancount].modes ))
1192 Config_Error_TooLong( Line, Var );
1195 if( strcasecmp( Var, "Topic" ) == 0 ) {
1197 if (!array_copys( &Conf_Channel[chancount].topic, Arg))
1198 Config_Error_TooLong( Line, Var );
1202 if( strcasecmp( Var, "Key" ) == 0 ) {
1203 /* Initial Channel Key (mode k) */
1204 len = strlcpy(Conf_Channel[chancount].key, Arg, sizeof(Conf_Channel[chancount].key));
1205 if (len >= sizeof( Conf_Channel[chancount].key ))
1206 Config_Error_TooLong(Line, Var);
1210 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1211 /* maximum user limit, mode l */
1212 Conf_Channel[chancount].maxusers = (unsigned long) atol(Arg);
1213 if (Conf_Channel[chancount].maxusers == 0)
1214 Config_Error_NaN(Line, Var);
1218 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1219 NGIRCd_ConfFile, Line, Var );
1220 } /* Handle_CHANNEL */
1224 Validate_Config(bool Configtest, bool Rehash)
1226 /* Validate configuration settings. */
1229 int i, servers, servers_once;
1231 bool config_valid = true;
1234 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1235 ptr = Conf_ServerName;
1237 if (*ptr >= 'a' && *ptr <= 'z') continue;
1238 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1239 if (*ptr >= '0' && *ptr <= '9') continue;
1240 if (ptr > Conf_ServerName) {
1241 if (*ptr == '.' || *ptr == '-')
1244 Conf_ServerName[0] = '\0';
1248 if (!Conf_ServerName[0]) {
1249 /* No server name configured! */
1250 config_valid = false;
1251 Config_Error(LOG_ALERT,
1252 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1254 if (!Configtest && !Rehash) {
1255 Config_Error(LOG_ALERT,
1256 "%s exiting due to fatal errors!",
1262 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1263 /* No dot in server name! */
1264 config_valid = false;
1265 Config_Error(LOG_ALERT,
1266 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1269 Config_Error(LOG_ALERT,
1270 "%s exiting due to fatal errors!",
1277 if (!Conf_ServerAdminMail[0]) {
1278 /* No administrative contact configured! */
1279 config_valid = false;
1280 Config_Error(LOG_ALERT,
1281 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1284 Config_Error(LOG_ALERT,
1285 "%s exiting due to fatal errors!",
1292 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1293 && !Conf_ServerAdminMail[0]) {
1294 /* No administrative information configured! */
1295 Config_Error(LOG_WARNING,
1296 "No administrative information configured but required by RFC!");
1300 servers = servers_once = 0;
1301 for (i = 0; i < MAX_SERVERS; i++) {
1302 if (Conf_Server[i].name[0]) {
1304 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1309 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1310 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1313 return config_valid;
1314 } /* Validate_Config */
1318 Config_Error_TooLong ( const int Line, const char *Item )
1320 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1325 Config_Error_NaN( const int Line, const char *Item )
1327 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1328 NGIRCd_ConfFile, Line, Item );
1333 static void Config_Error( const int Level, const char *Format, ... )
1335 static void Config_Error( Level, Format, va_alist )
1341 /* Error! Write to console and/or logfile. */
1343 char msg[MAX_LOG_MSG_LEN];
1346 assert( Format != NULL );
1349 va_start( ap, Format );
1353 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1356 /* During "normal operations" the log functions of the daemon should
1357 * be used, but during testing of the configuration file, all messages
1358 * should go directly to the console: */
1359 if (Use_Log) Log( Level, "%s", msg );
1361 } /* Config_Error */
1365 Init_Server_Struct( CONF_SERVER *Server )
1367 /* Initialize server configuration structur to default values */
1369 assert( Server != NULL );
1371 memset( Server, 0, sizeof (CONF_SERVER) );
1373 Server->group = NONE;
1374 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1376 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1378 Resolve_Init(&Server->res_stat);
1379 Server->conn_id = NONE;
1380 memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
1381 } /* Init_Server_Struct */