2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2008 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>
54 static bool Use_Log = true;
55 static CONF_SERVER New_Server;
56 static int New_Server_Idx;
58 static size_t Conf_Channel_Count;
59 static void Set_Defaults PARAMS(( bool InitServers ));
60 static bool Read_Config PARAMS(( bool ngircd_starting ));
61 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
63 static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
64 static void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
65 static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
66 static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
68 static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
70 static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
71 static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
73 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
76 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
78 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
82 struct SSLOptions Conf_SSLOptions;
87 free(Conf_SSLOptions.KeyFile);
88 Conf_SSLOptions.KeyFile = NULL;
90 free(Conf_SSLOptions.CertFile);
91 Conf_SSLOptions.CertFile = NULL;
93 free(Conf_SSLOptions.DHFile);
94 Conf_SSLOptions.DHFile = NULL;
95 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
102 if (Conf_SSLOptions.KeyFile)
103 printf( " SSLKeyFile = %s\n", Conf_SSLOptions.KeyFile);
104 if (Conf_SSLOptions.CertFile)
105 printf( " SSLCertFile = %s\n", Conf_SSLOptions.CertFile);
106 if (Conf_SSLOptions.DHFile)
107 printf( " SSLDHFile = %s\n", Conf_SSLOptions.DHFile);
108 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
109 puts(" SSLKeyFilePassword = <secret>" );
110 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
115 strdup_warn(const char *str)
117 char *ptr = strdup(str);
119 Config_Error(LOG_ERR, "Could not allocate mem for string: %s", str);
129 len = array_length(a, sizeof(UINT16));
131 ports = (UINT16*) array_start(a);
132 printf("%u", (unsigned int) *ports);
135 printf(", %u", (unsigned int) *ports);
143 ports_parse(array *a, int Line, char *Arg)
151 /* Ports on that the server should listen. More port numbers
152 * must be separated by "," */
153 ptr = strtok( Arg, "," );
157 if (port > 0 && port < 0xFFFF) {
158 port16 = (UINT16) port;
159 if (!array_catb(a, (char*)&port16, sizeof port16))
160 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
161 NGIRCd_ConfFile, Line, port, strerror(errno));
163 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
164 NGIRCd_ConfFile, Line, port );
167 ptr = strtok( NULL, "," );
176 Validate_Config(false, false);
183 if (!Read_Config(false))
185 Validate_Config(false, true);
187 /* Update CLIENT structure of local server */
188 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
190 } /* Config_Rehash */
194 yesno_to_str(int boolean_value)
205 /* Read configuration, validate and output it. */
211 size_t predef_channel_count;
212 struct Conf_Channel *predef_chan;
216 if (! Read_Config(true))
219 config_valid = Validate_Config(true, false);
221 /* If stdin and stdout ("you can read our nice message and we can
222 * read in your keypress") are valid tty's, wait for a key: */
223 if( isatty( fileno( stdin )) && isatty( fileno( stdout ))) {
224 puts( "OK, press enter to see a dump of your service configuration ..." );
227 puts( "Ok, dump of your server configuration follows:\n" );
231 printf( " Name = %s\n", Conf_ServerName );
232 printf( " Info = %s\n", Conf_ServerInfo );
233 printf( " Password = %s\n", Conf_ServerPwd );
234 printf( " AdminInfo1 = %s\n", Conf_ServerAdmin1 );
235 printf( " AdminInfo2 = %s\n", Conf_ServerAdmin2 );
236 printf( " AdminEMail = %s\n", Conf_ServerAdminMail );
237 printf( " MotdFile = %s\n", Conf_MotdFile );
238 printf( " MotdPhrase = %s\n", Conf_MotdPhrase );
239 printf( " ChrootDir = %s\n", Conf_Chroot );
240 printf( " PidFile = %s\n", Conf_PidFile);
241 printf(" Listen = %s\n", Conf_ListenAddress);
242 fputs(" Ports = ", stdout);
244 ports_puts(&Conf_ListenPorts);
246 fputs(" SSLPorts = ", stdout);
247 ports_puts(&Conf_SSLOptions.ListenPorts);
251 pwd = getpwuid( Conf_UID );
252 if( pwd ) printf( " ServerUID = %s\n", pwd->pw_name );
253 else printf( " ServerUID = %ld\n", (long)Conf_UID );
254 grp = getgrgid( Conf_GID );
255 if( grp ) printf( " ServerGID = %s\n", grp->gr_name );
256 else printf( " ServerGID = %ld\n", (long)Conf_GID );
257 printf( " PingTimeout = %d\n", Conf_PingTimeout );
258 printf( " PongTimeout = %d\n", Conf_PongTimeout );
259 printf( " ConnectRetry = %d\n", Conf_ConnectRetry );
260 printf( " OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
261 printf( " OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
262 printf( " PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
263 printf( " NoDNS = %s\n", yesno_to_str(Conf_NoDNS));
264 printf( " NoIdent = %s\n", yesno_to_str(Conf_NoIdent));
267 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
268 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
270 printf( " MaxConnections = %ld\n", Conf_MaxConnections);
271 printf( " MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
272 printf( " MaxJoins = %d\n", Conf_MaxJoins>0 ? Conf_MaxJoins : -1);
273 printf( " MaxNickLength = %u\n\n", Conf_MaxNickLength - 1);
275 for( i = 0; i < Conf_Oper_Count; i++ ) {
276 if( ! Conf_Oper[i].name[0] ) continue;
278 /* Valid "Operator" section */
279 puts( "[OPERATOR]" );
280 printf( " Name = %s\n", Conf_Oper[i].name );
281 printf( " Password = %s\n", Conf_Oper[i].pwd );
282 if ( Conf_Oper[i].mask ) printf( " Mask = %s\n", Conf_Oper[i].mask );
286 for( i = 0; i < MAX_SERVERS; i++ ) {
287 if( ! Conf_Server[i].name[0] ) continue;
289 /* Valid "Server" section */
291 printf( " Name = %s\n", Conf_Server[i].name );
292 printf( " Host = %s\n", Conf_Server[i].host );
293 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
295 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
297 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
298 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
299 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
300 printf( " Group = %d\n", Conf_Server[i].group );
301 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
304 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
305 predef_chan = array_start(&Conf_Channels);
307 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
308 if (!predef_chan->name[0])
311 /* Valid "Channel" section */
313 printf(" Name = %s\n", predef_chan->name);
314 printf(" Modes = %s\n", predef_chan->modes);
315 printf(" Key = %s\n", predef_chan->key);
316 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
317 printf(" Topic = %s\n", predef_chan->topic);
318 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
321 return (config_valid ? 0 : 1);
326 Conf_UnsetServer( CONN_ID Idx )
328 /* Set next time for next connection attempt, if this is a server
329 * link that is (still) configured here. If the server is set as
330 * "once", delete it from our configuration.
331 * Non-Server-Connections will be silently ignored. */
336 /* Check all our configured servers */
337 for( i = 0; i < MAX_SERVERS; i++ ) {
338 if( Conf_Server[i].conn_id != Idx ) continue;
340 /* Gotcha! Mark server configuration as "unused": */
341 Conf_Server[i].conn_id = NONE;
343 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
344 /* Delete configuration here */
345 Init_Server_Struct( &Conf_Server[i] );
347 /* Set time for next connect attempt */
349 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
350 /* The connection has been "long", so we don't
351 * require the next attempt to be delayed. */
352 Conf_Server[i].lasttry =
353 t - Conf_ConnectRetry + RECONNECT_DELAY;
355 Conf_Server[i].lasttry = t;
358 } /* Conf_UnsetServer */
362 Conf_SetServer( int ConfServer, CONN_ID Idx )
364 /* Set connection for specified configured server */
366 assert( ConfServer > NONE );
367 assert( Idx > NONE );
369 Conf_Server[ConfServer].conn_id = Idx;
370 } /* Conf_SetServer */
374 Conf_GetServer( CONN_ID Idx )
376 /* Get index of server in configuration structure */
380 assert( Idx > NONE );
382 for( i = 0; i < MAX_SERVERS; i++ ) {
383 if( Conf_Server[i].conn_id == Idx ) return i;
386 } /* Conf_GetServer */
390 Conf_EnableServer( const char *Name, UINT16 Port )
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! Set port and enable server: */
401 Conf_Server[i].port = Port;
402 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
403 return (Conf_Server[i].port && Conf_Server[i].host[0]);
407 } /* Conf_EnableServer */
411 Conf_EnablePassiveServer(const char *Name)
413 /* Enable specified server */
416 assert( Name != NULL );
417 for (i = 0; i < MAX_SERVERS; i++) {
418 if ((strcasecmp( Conf_Server[i].name, Name ) == 0) && (Conf_Server[i].port > 0)) {
419 /* BINGO! Enable server */
420 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
425 } /* Conf_EnablePassiveServer */
429 Conf_DisableServer( const char *Name )
431 /* Enable specified server and adjust port */
435 assert( Name != NULL );
437 for( i = 0; i < MAX_SERVERS; i++ ) {
438 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
439 /* Gotcha! Disable and disconnect server: */
440 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
441 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
446 } /* Conf_DisableServer */
450 Conf_AddServer( const char *Name, UINT16 Port, const char *Host, const char *MyPwd, const char *PeerPwd )
452 /* Add new server to configuration */
456 assert( Name != NULL );
457 assert( Host != NULL );
458 assert( MyPwd != NULL );
459 assert( PeerPwd != NULL );
461 /* Search unused item in server configuration structure */
462 for( i = 0; i < MAX_SERVERS; i++ ) {
463 /* Is this item used? */
464 if( ! Conf_Server[i].name[0] ) break;
466 if( i >= MAX_SERVERS ) return false;
468 Init_Server_Struct( &Conf_Server[i] );
469 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
470 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
471 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
472 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
473 Conf_Server[i].port = Port;
474 Conf_Server[i].flags = CONF_SFLAG_ONCE;
477 } /* Conf_AddServer */
481 * Check if the given nick name is an service
484 Conf_IsService(int ConfServer, const char *Nick)
486 return MatchCaseInsensitive(Conf_Server[ConfServer].svs_mask, Nick);
487 } /* Conf_IsService */
491 Set_Defaults( bool InitServers )
493 /* Initialize configuration variables with default values. */
497 strcpy( Conf_ServerName, "" );
498 snprintf( Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
499 strcpy( Conf_ServerPwd, "" );
501 strcpy( Conf_ServerAdmin1, "" );
502 strcpy( Conf_ServerAdmin2, "" );
503 strcpy( Conf_ServerAdminMail, "" );
505 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
506 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
508 strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
510 strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
512 strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
514 free(Conf_ListenAddress);
515 Conf_ListenAddress = NULL;
516 Conf_UID = Conf_GID = 0;
518 Conf_PingTimeout = 120;
519 Conf_PongTimeout = 20;
521 Conf_ConnectRetry = 60;
524 Conf_Channel_Count = 0;
526 Conf_OperCanMode = false;
528 Conf_NoIdent = false;
529 Conf_PredefChannelsOnly = false;
530 Conf_OperServerMode = false;
532 Conf_ConnectIPv4 = true;
533 Conf_ConnectIPv6 = true;
535 Conf_MaxConnections = 0;
536 Conf_MaxConnectionsIP = 5;
538 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
540 /* Initialize server configuration structures */
541 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
546 Read_Config( bool ngircd_starting )
548 /* Read configuration file. */
550 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
551 const UINT16 defaultport = 6667;
555 /* Open configuration file */
556 fd = fopen( NGIRCd_ConfFile, "r" );
558 /* No configuration file found! */
559 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
560 NGIRCd_ConfFile, strerror( errno ));
561 if (!ngircd_starting)
563 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
567 Set_Defaults( ngircd_starting );
569 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
571 /* Clean up server configuration structure: mark all already
572 * configured servers as "once" so that they are deleted
573 * after the next disconnect and delete all unused servers.
574 * And delete all servers which are "duplicates" of servers
575 * that are already marked as "once" (such servers have been
576 * created by the last rehash but are now useless). */
577 for( i = 0; i < MAX_SERVERS; i++ ) {
578 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
580 /* This structure is in use ... */
581 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
582 /* Check for duplicates */
583 for( n = 0; n < MAX_SERVERS; n++ ) {
584 if( n == i ) continue;
586 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
587 Init_Server_Struct( &Conf_Server[n] );
589 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
595 /* Mark server as "once" */
596 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
597 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
602 /* Initialize variables */
604 strcpy( section, "" );
605 Init_Server_Struct( &New_Server );
606 New_Server_Idx = NONE;
610 /* Read configuration file */
612 if( ! fgets( str, LINE_LEN, fd )) break;
616 /* Skip comments and empty lines */
617 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
619 /* Is this the beginning of a new section? */
620 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
621 strlcpy( section, str, sizeof( section ));
622 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
625 if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
626 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
627 Config_Error( LOG_ERR, "Too many operators configured.");
629 /* Initialize new operator structure */
630 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
631 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
632 if (Conf_Oper[Conf_Oper_Count].mask) {
633 free(Conf_Oper[Conf_Oper_Count].mask );
634 Conf_Oper[Conf_Oper_Count].mask = NULL;
640 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
641 /* Check if there is already a server to add */
642 if( New_Server.name[0] ) {
643 /* Copy data to "real" server structure */
644 assert( New_Server_Idx > NONE );
645 Conf_Server[New_Server_Idx] = New_Server;
648 /* Re-init structure for new server */
649 Init_Server_Struct( &New_Server );
651 /* Search unused item in server configuration structure */
652 for( i = 0; i < MAX_SERVERS; i++ ) {
653 /* Is this item used? */
654 if( ! Conf_Server[i].name[0] ) break;
656 if( i >= MAX_SERVERS ) {
657 /* Oops, no free item found! */
658 Config_Error( LOG_ERR, "Too many servers configured." );
659 New_Server_Idx = NONE;
661 else New_Server_Idx = i;
664 if (strcasecmp(section, "[CHANNEL]") == 0) {
665 Conf_Channel_Count++;
669 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
672 if( section[0] == 0x1 ) continue;
674 /* Split line into variable name and parameters */
675 ptr = strchr( str, '=' );
677 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
681 var = str; ngt_TrimStr( var );
682 arg = ptr + 1; ngt_TrimStr( arg );
684 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
685 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
686 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
687 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
688 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
691 /* Close configuration file */
694 /* Check if there is still a server to add */
695 if( New_Server.name[0] ) {
696 /* Copy data to "real" server structure */
697 assert( New_Server_Idx > NONE );
698 Conf_Server[New_Server_Idx] = New_Server;
701 if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
702 if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
703 Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
704 (unsigned int) defaultport, strerror(errno));
709 if (!Conf_ListenAddress)
710 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
712 if (!Conf_ListenAddress) {
713 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
721 Check_ArgIsTrue( const char *Arg )
723 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
724 if( strcasecmp( Arg, "true" ) == 0 ) return true;
725 if( atoi( Arg ) != 0 ) return true;
728 } /* Check_ArgIsTrue */
731 static unsigned int Handle_MaxNickLength(int Line, const char *Arg)
735 new = (unsigned) atoi(Arg) + 1;
736 if (new > CLIENT_NICK_LEN) {
737 Config_Error(LOG_WARNING,
738 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
739 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
740 return CLIENT_NICK_LEN;
743 Config_Error(LOG_WARNING,
744 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
745 NGIRCd_ConfFile, Line);
749 } /* Handle_MaxNickLength */
753 Handle_GLOBAL( int Line, char *Var, char *Arg )
760 assert( Var != NULL );
761 assert( Arg != NULL );
763 if( strcasecmp( Var, "Name" ) == 0 ) {
765 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
766 if (len >= sizeof( Conf_ServerName ))
767 Config_Error_TooLong( Line, Var );
770 if( strcasecmp( Var, "Info" ) == 0 ) {
771 /* Info text of server */
772 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
773 if (len >= sizeof( Conf_ServerInfo ))
774 Config_Error_TooLong ( Line, Var );
777 if( strcasecmp( Var, "Password" ) == 0 ) {
778 /* Global server password */
779 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
780 if (len >= sizeof( Conf_ServerPwd ))
781 Config_Error_TooLong( Line, Var );
784 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
785 /* Administrative info #1 */
786 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
787 if (len >= sizeof( Conf_ServerAdmin1 ))
788 Config_Error_TooLong ( Line, Var );
791 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
792 /* Administrative info #2 */
793 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
794 if (len >= sizeof( Conf_ServerAdmin2 ))
795 Config_Error_TooLong ( Line, Var );
798 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
799 /* Administrative email contact */
800 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
801 if (len >= sizeof( Conf_ServerAdminMail ))
802 Config_Error_TooLong( Line, Var );
806 if( strcasecmp( Var, "Ports" ) == 0 ) {
807 ports_parse(&Conf_ListenPorts, Line, Arg);
810 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
811 /* "Message of the day" (MOTD) file */
812 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
813 if (len >= sizeof( Conf_MotdFile ))
814 Config_Error_TooLong( Line, Var );
817 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
818 /* "Message of the day" phrase (instead of file) */
819 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
820 if (len >= sizeof( Conf_MotdPhrase ))
821 Config_Error_TooLong( Line, Var );
824 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
825 /* directory for chroot() */
826 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
827 if (len >= sizeof( Conf_Chroot ))
828 Config_Error_TooLong( Line, Var );
831 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
832 /* name of pidfile */
833 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
834 if (len >= sizeof( Conf_PidFile ))
835 Config_Error_TooLong( Line, Var );
838 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
839 /* UID the daemon should switch to */
840 pwd = getpwnam( Arg );
841 if( pwd ) Conf_UID = pwd->pw_uid;
844 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
847 Conf_UID = (unsigned int)atoi( Arg );
851 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
852 /* GID the daemon should use */
853 grp = getgrnam( Arg );
854 if( grp ) Conf_GID = grp->gr_gid;
857 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
860 Conf_GID = (unsigned int)atoi( Arg );
864 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
866 Conf_PingTimeout = atoi( Arg );
867 if( Conf_PingTimeout < 5 ) {
868 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
869 NGIRCd_ConfFile, Line );
870 Conf_PingTimeout = 5;
874 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
876 Conf_PongTimeout = atoi( Arg );
877 if( Conf_PongTimeout < 5 ) {
878 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
879 NGIRCd_ConfFile, Line );
880 Conf_PongTimeout = 5;
884 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
885 /* Seconds between connection attempts to other servers */
886 Conf_ConnectRetry = atoi( Arg );
887 if( Conf_ConnectRetry < 5 ) {
888 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
889 NGIRCd_ConfFile, Line );
890 Conf_ConnectRetry = 5;
894 if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
895 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
896 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
899 if( strcasecmp( Var, "NoDNS" ) == 0 ) {
900 /* don't do reverse dns lookups when clients connect? */
901 Conf_NoDNS = Check_ArgIsTrue( Arg );
904 if (strcasecmp(Var, "NoIdent") == 0) {
905 /* don't do IDENT lookups when clients connect? */
906 Conf_NoIdent = Check_ArgIsTrue(Arg);
909 /* user has enabled ident lookups explicitly, but ... */
910 Config_Error(LOG_WARNING,
911 "%s: line %d: NoIdent=False, but ngircd was built without IDENT support",
912 NGIRCd_ConfFile, Line);
918 /* the default setting for all the WANT_IPV6 special options is 'true' */
919 if( strcasecmp( Var, "ConnectIPv6" ) == 0 ) {
920 /* connect to other hosts using ipv6, if they have an AAAA record? */
921 Conf_ConnectIPv6 = Check_ArgIsTrue( Arg );
924 if( strcasecmp( Var, "ConnectIPv4" ) == 0 ) {
925 /* connect to other hosts using ipv4.
926 * again, this can be used for ipv6-only setups */
927 Conf_ConnectIPv4 = Check_ArgIsTrue( Arg );
931 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
932 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
933 Conf_OperCanMode = Check_ArgIsTrue( Arg );
936 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
937 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
938 Conf_OperServerMode = Check_ArgIsTrue( Arg );
941 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
942 /* Maximum number of connections. 0 -> "no limit". */
944 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
947 Conf_MaxConnections = atol( Arg );
950 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
951 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
953 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
956 Conf_MaxConnectionsIP = atoi( Arg );
959 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
960 /* Maximum number of channels a user can join. 0 -> "no limit". */
962 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
965 Conf_MaxJoins = atoi( Arg );
968 if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
969 /* Maximum length of a nick name; must be same on all servers
970 * within the IRC network! */
971 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
975 if( strcasecmp( Var, "Listen" ) == 0 ) {
976 /* IP-Address to bind sockets */
977 if (Conf_ListenAddress) {
978 Config_Error(LOG_ERR, "Multiple Listen= options, ignoring: %s", Arg);
981 Conf_ListenAddress = strdup_warn(Arg);
983 * if allocation fails, we're in trouble:
984 * we cannot ignore the error -- otherwise ngircd
985 * would listen on all interfaces.
987 if (!Conf_ListenAddress) {
988 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
995 if( strcasecmp( Var, "SSLPorts" ) == 0 ) {
996 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1000 if( strcasecmp( Var, "SSLKeyFile" ) == 0 ) {
1001 assert(Conf_SSLOptions.KeyFile == NULL );
1002 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1005 if( strcasecmp( Var, "SSLCertFile" ) == 0 ) {
1006 assert(Conf_SSLOptions.CertFile == NULL );
1007 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1011 if( strcasecmp( Var, "SSLKeyFilePassword" ) == 0 ) {
1012 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1013 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1014 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Could not copy %s: %s!",
1015 NGIRCd_ConfFile, Line, Var, strerror(errno));
1018 if( strcasecmp( Var, "SSLDHFile" ) == 0 ) {
1019 assert(Conf_SSLOptions.DHFile == NULL);
1020 Conf_SSLOptions.DHFile = strdup_warn( Arg );
1024 Config_Error(LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
1025 NGIRCd_ConfFile, Line, Var);
1026 } /* Handle_GLOBAL */
1030 Handle_OPERATOR( int Line, char *Var, char *Arg )
1032 unsigned int opercount;
1035 assert( Var != NULL );
1036 assert( Arg != NULL );
1037 assert( Conf_Oper_Count > 0 );
1039 if ( Conf_Oper_Count == 0 )
1042 opercount = Conf_Oper_Count - 1;
1044 if( strcasecmp( Var, "Name" ) == 0 ) {
1045 /* Name of IRC operator */
1046 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
1047 if (len >= sizeof( Conf_Oper[opercount].name ))
1048 Config_Error_TooLong( Line, Var );
1051 if( strcasecmp( Var, "Password" ) == 0 ) {
1052 /* Password of IRC operator */
1053 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
1054 if (len >= sizeof( Conf_Oper[opercount].pwd ))
1055 Config_Error_TooLong( Line, Var );
1058 if( strcasecmp( Var, "Mask" ) == 0 ) {
1059 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
1061 Conf_Oper[opercount].mask = strdup_warn( Arg );
1064 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
1065 NGIRCd_ConfFile, Line, Var );
1066 } /* Handle_OPERATOR */
1070 Handle_SERVER( int Line, char *Var, char *Arg )
1076 assert( Var != NULL );
1077 assert( Arg != NULL );
1079 /* Ignore server block if no space is left in server configuration structure */
1080 if( New_Server_Idx <= NONE ) return;
1082 if( strcasecmp( Var, "Host" ) == 0 ) {
1083 /* Hostname of the server */
1084 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1085 if (len >= sizeof( New_Server.host ))
1086 Config_Error_TooLong ( Line, Var );
1089 if( strcasecmp( Var, "Name" ) == 0 ) {
1090 /* Name of the server ("Nick"/"ID") */
1091 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1092 if (len >= sizeof( New_Server.name ))
1093 Config_Error_TooLong( Line, Var );
1096 if (strcasecmp(Var, "Bind") == 0) {
1097 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1100 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1101 NGIRCd_ConfFile, Line, Arg);
1104 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1105 /* Password of this server which is sent to the peer */
1107 Config_Error(LOG_ERR,
1108 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1109 NGIRCd_ConfFile, Line);
1111 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1112 if (len >= sizeof( New_Server.pwd_in ))
1113 Config_Error_TooLong( Line, Var );
1116 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1117 /* Passwort of the peer which must be received */
1118 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1119 if (len >= sizeof( New_Server.pwd_out ))
1120 Config_Error_TooLong( Line, Var );
1123 if( strcasecmp( Var, "Port" ) == 0 ) {
1124 /* Port to which this server should connect */
1126 if( port > 0 && port < 0xFFFF )
1127 New_Server.port = (UINT16)port;
1129 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
1130 NGIRCd_ConfFile, Line, port );
1134 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1135 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1139 if( strcasecmp( Var, "Group" ) == 0 ) {
1142 if( ! isdigit( (int)*Arg ))
1143 Config_Error_NaN( Line, Var );
1146 New_Server.group = atoi( Arg );
1149 if( strcasecmp( Var, "Passive" ) == 0 ) {
1150 if (Check_ArgIsTrue(Arg))
1151 New_Server.flags |= CONF_SFLAG_DISABLED;
1154 if (strcasecmp(Var, "ServiceMask") == 0) {
1155 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1156 sizeof(New_Server.svs_mask));
1157 if (len >= sizeof(New_Server.svs_mask))
1158 Config_Error_TooLong(Line, Var);
1162 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
1163 NGIRCd_ConfFile, Line, Var );
1164 } /* Handle_SERVER */
1168 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1170 size_t size = sizeof(new_chan->name);
1171 char *dest = new_chan->name;
1173 if (!Channel_IsValidName(name)) {
1175 * maybe user forgot to add a '#'.
1176 * This is only here for user convenience.
1182 return size > strlcpy(dest, name, size);
1187 Handle_CHANNEL(int Line, char *Var, char *Arg)
1191 struct Conf_Channel *chan;
1194 assert( Var != NULL );
1195 assert( Arg != NULL );
1196 assert(Conf_Channel_Count > 0);
1198 chancount = Conf_Channel_Count - 1;
1200 chan = array_alloc(&Conf_Channels, sizeof(*chan), chancount);
1202 Config_Error(LOG_ERR, "Could not allocate memory for predefined channel (%d:%s = %s)", Line, Var, Arg);
1205 if (strcasecmp(Var, "Name") == 0) {
1206 if (!Handle_Channelname(chan, Arg))
1207 Config_Error_TooLong(Line, Var);
1210 if (strcasecmp(Var, "Modes") == 0) {
1212 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1213 if (len >= sizeof(chan->modes))
1214 Config_Error_TooLong( Line, Var );
1217 if( strcasecmp( Var, "Topic" ) == 0 ) {
1219 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1220 if (len >= sizeof(chan->topic))
1221 Config_Error_TooLong( Line, Var );
1224 if( strcasecmp( Var, "Key" ) == 0 ) {
1225 /* Initial Channel Key (mode k) */
1226 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1227 if (len >= sizeof(chan->key))
1228 Config_Error_TooLong(Line, Var);
1231 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1232 /* maximum user limit, mode l */
1233 chan->maxusers = (unsigned long) atol(Arg);
1234 if (chan->maxusers == 0)
1235 Config_Error_NaN(Line, Var);
1238 if (strcasecmp(Var, "KeyFile") == 0) {
1240 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1241 if (len >= sizeof(chan->keyfile))
1242 Config_Error_TooLong(Line, Var);
1246 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1247 NGIRCd_ConfFile, Line, Var );
1248 } /* Handle_CHANNEL */
1252 Validate_Config(bool Configtest, bool Rehash)
1254 /* Validate configuration settings. */
1257 int i, servers, servers_once;
1259 bool config_valid = true;
1262 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1263 ptr = Conf_ServerName;
1265 if (*ptr >= 'a' && *ptr <= 'z') continue;
1266 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1267 if (*ptr >= '0' && *ptr <= '9') continue;
1268 if (ptr > Conf_ServerName) {
1269 if (*ptr == '.' || *ptr == '-')
1272 Conf_ServerName[0] = '\0';
1276 if (!Conf_ServerName[0]) {
1277 /* No server name configured! */
1278 config_valid = false;
1279 Config_Error(LOG_ALERT,
1280 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1282 if (!Configtest && !Rehash) {
1283 Config_Error(LOG_ALERT,
1284 "%s exiting due to fatal errors!",
1290 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1291 /* No dot in server name! */
1292 config_valid = false;
1293 Config_Error(LOG_ALERT,
1294 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1297 Config_Error(LOG_ALERT,
1298 "%s exiting due to fatal errors!",
1305 if (!Conf_ServerAdminMail[0]) {
1306 /* No administrative contact configured! */
1307 config_valid = false;
1308 Config_Error(LOG_ALERT,
1309 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1312 Config_Error(LOG_ALERT,
1313 "%s exiting due to fatal errors!",
1320 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1321 && !Conf_ServerAdminMail[0]) {
1322 /* No administrative information configured! */
1323 Config_Error(LOG_WARNING,
1324 "No administrative information configured but required by RFC!");
1328 servers = servers_once = 0;
1329 for (i = 0; i < MAX_SERVERS; i++) {
1330 if (Conf_Server[i].name[0]) {
1332 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1337 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1338 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1341 return config_valid;
1342 } /* Validate_Config */
1346 Config_Error_TooLong ( const int Line, const char *Item )
1348 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1353 Config_Error_NaN( const int Line, const char *Item )
1355 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1356 NGIRCd_ConfFile, Line, Item );
1361 static void Config_Error( const int Level, const char *Format, ... )
1363 static void Config_Error( Level, Format, va_alist )
1369 /* Error! Write to console and/or logfile. */
1371 char msg[MAX_LOG_MSG_LEN];
1374 assert( Format != NULL );
1377 va_start( ap, Format );
1381 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1384 /* During "normal operations" the log functions of the daemon should
1385 * be used, but during testing of the configuration file, all messages
1386 * should go directly to the console: */
1387 if (Use_Log) Log( Level, "%s", msg );
1389 } /* Config_Error */
1393 Init_Server_Struct( CONF_SERVER *Server )
1395 /* Initialize server configuration structur to default values */
1397 assert( Server != NULL );
1399 memset( Server, 0, sizeof (CONF_SERVER) );
1401 Server->group = NONE;
1402 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1404 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1406 Resolve_Init(&Server->res_stat);
1407 Server->conn_id = NONE;
1408 memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
1409 } /* Init_Server_Struct */