2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2009 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++] ));
548 size_t cnt = array_bytes(&Conf_ListenPorts);
550 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
556 Read_Config( bool ngircd_starting )
558 /* Read configuration file. */
560 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
561 const UINT16 defaultport = 6667;
565 /* Open configuration file */
566 fd = fopen( NGIRCd_ConfFile, "r" );
568 /* No configuration file found! */
569 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
570 NGIRCd_ConfFile, strerror( errno ));
571 if (!ngircd_starting)
573 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
577 Set_Defaults( ngircd_starting );
579 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
581 /* Clean up server configuration structure: mark all already
582 * configured servers as "once" so that they are deleted
583 * after the next disconnect and delete all unused servers.
584 * And delete all servers which are "duplicates" of servers
585 * that are already marked as "once" (such servers have been
586 * created by the last rehash but are now useless). */
587 for( i = 0; i < MAX_SERVERS; i++ ) {
588 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
590 /* This structure is in use ... */
591 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
592 /* Check for duplicates */
593 for( n = 0; n < MAX_SERVERS; n++ ) {
594 if( n == i ) continue;
596 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
597 Init_Server_Struct( &Conf_Server[n] );
599 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
605 /* Mark server as "once" */
606 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
607 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
612 /* Initialize variables */
614 strcpy( section, "" );
615 Init_Server_Struct( &New_Server );
616 New_Server_Idx = NONE;
620 /* Read configuration file */
622 if( ! fgets( str, LINE_LEN, fd )) break;
626 /* Skip comments and empty lines */
627 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
629 /* Is this the beginning of a new section? */
630 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
631 strlcpy( section, str, sizeof( section ));
632 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
635 if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
636 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
637 Config_Error( LOG_ERR, "Too many operators configured.");
639 /* Initialize new operator structure */
640 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
641 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
642 if (Conf_Oper[Conf_Oper_Count].mask) {
643 free(Conf_Oper[Conf_Oper_Count].mask );
644 Conf_Oper[Conf_Oper_Count].mask = NULL;
650 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
651 /* Check if there is already a server to add */
652 if( New_Server.name[0] ) {
653 /* Copy data to "real" server structure */
654 assert( New_Server_Idx > NONE );
655 Conf_Server[New_Server_Idx] = New_Server;
658 /* Re-init structure for new server */
659 Init_Server_Struct( &New_Server );
661 /* Search unused item in server configuration structure */
662 for( i = 0; i < MAX_SERVERS; i++ ) {
663 /* Is this item used? */
664 if( ! Conf_Server[i].name[0] ) break;
666 if( i >= MAX_SERVERS ) {
667 /* Oops, no free item found! */
668 Config_Error( LOG_ERR, "Too many servers configured." );
669 New_Server_Idx = NONE;
671 else New_Server_Idx = i;
674 if (strcasecmp(section, "[CHANNEL]") == 0) {
675 Conf_Channel_Count++;
679 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
682 if( section[0] == 0x1 ) continue;
684 /* Split line into variable name and parameters */
685 ptr = strchr( str, '=' );
687 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
691 var = str; ngt_TrimStr( var );
692 arg = ptr + 1; ngt_TrimStr( arg );
694 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
695 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
696 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
697 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
698 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
701 /* Close configuration file */
704 /* Check if there is still a server to add */
705 if( New_Server.name[0] ) {
706 /* Copy data to "real" server structure */
707 assert( New_Server_Idx > NONE );
708 Conf_Server[New_Server_Idx] = New_Server;
711 /* not a single listening port? Add default. */
712 if (no_listenports() &&
713 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
715 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
716 (unsigned int) defaultport, strerror(errno));
721 if (!Conf_ListenAddress)
722 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
724 if (!Conf_ListenAddress) {
725 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
733 Check_ArgIsTrue( const char *Arg )
735 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
736 if( strcasecmp( Arg, "true" ) == 0 ) return true;
737 if( atoi( Arg ) != 0 ) return true;
740 } /* Check_ArgIsTrue */
743 static unsigned int Handle_MaxNickLength(int Line, const char *Arg)
747 new = (unsigned) atoi(Arg) + 1;
748 if (new > CLIENT_NICK_LEN) {
749 Config_Error(LOG_WARNING,
750 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
751 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
752 return CLIENT_NICK_LEN;
755 Config_Error(LOG_WARNING,
756 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
757 NGIRCd_ConfFile, Line);
761 } /* Handle_MaxNickLength */
765 Handle_GLOBAL( int Line, char *Var, char *Arg )
772 assert( Var != NULL );
773 assert( Arg != NULL );
775 if( strcasecmp( Var, "Name" ) == 0 ) {
777 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
778 if (len >= sizeof( Conf_ServerName ))
779 Config_Error_TooLong( Line, Var );
782 if( strcasecmp( Var, "Info" ) == 0 ) {
783 /* Info text of server */
784 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
785 if (len >= sizeof( Conf_ServerInfo ))
786 Config_Error_TooLong ( Line, Var );
789 if( strcasecmp( Var, "Password" ) == 0 ) {
790 /* Global server password */
791 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
792 if (len >= sizeof( Conf_ServerPwd ))
793 Config_Error_TooLong( Line, Var );
796 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
797 /* Administrative info #1 */
798 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
799 if (len >= sizeof( Conf_ServerAdmin1 ))
800 Config_Error_TooLong ( Line, Var );
803 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
804 /* Administrative info #2 */
805 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
806 if (len >= sizeof( Conf_ServerAdmin2 ))
807 Config_Error_TooLong ( Line, Var );
810 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
811 /* Administrative email contact */
812 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
813 if (len >= sizeof( Conf_ServerAdminMail ))
814 Config_Error_TooLong( Line, Var );
818 if( strcasecmp( Var, "Ports" ) == 0 ) {
819 ports_parse(&Conf_ListenPorts, Line, Arg);
822 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
823 /* "Message of the day" (MOTD) file */
824 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
825 if (len >= sizeof( Conf_MotdFile ))
826 Config_Error_TooLong( Line, Var );
829 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
830 /* "Message of the day" phrase (instead of file) */
831 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
832 if (len >= sizeof( Conf_MotdPhrase ))
833 Config_Error_TooLong( Line, Var );
836 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
837 /* directory for chroot() */
838 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
839 if (len >= sizeof( Conf_Chroot ))
840 Config_Error_TooLong( Line, Var );
843 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
844 /* name of pidfile */
845 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
846 if (len >= sizeof( Conf_PidFile ))
847 Config_Error_TooLong( Line, Var );
850 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
851 /* UID the daemon should switch to */
852 pwd = getpwnam( Arg );
853 if( pwd ) Conf_UID = pwd->pw_uid;
856 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
859 Conf_UID = (unsigned int)atoi( Arg );
863 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
864 /* GID the daemon should use */
865 grp = getgrnam( Arg );
866 if( grp ) Conf_GID = grp->gr_gid;
869 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
872 Conf_GID = (unsigned int)atoi( Arg );
876 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
878 Conf_PingTimeout = atoi( Arg );
879 if( Conf_PingTimeout < 5 ) {
880 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
881 NGIRCd_ConfFile, Line );
882 Conf_PingTimeout = 5;
886 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
888 Conf_PongTimeout = atoi( Arg );
889 if( Conf_PongTimeout < 5 ) {
890 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
891 NGIRCd_ConfFile, Line );
892 Conf_PongTimeout = 5;
896 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
897 /* Seconds between connection attempts to other servers */
898 Conf_ConnectRetry = atoi( Arg );
899 if( Conf_ConnectRetry < 5 ) {
900 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
901 NGIRCd_ConfFile, Line );
902 Conf_ConnectRetry = 5;
906 if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
907 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
908 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
911 if( strcasecmp( Var, "NoDNS" ) == 0 ) {
912 /* don't do reverse dns lookups when clients connect? */
913 Conf_NoDNS = Check_ArgIsTrue( Arg );
916 if (strcasecmp(Var, "NoIdent") == 0) {
917 /* don't do IDENT lookups when clients connect? */
918 Conf_NoIdent = Check_ArgIsTrue(Arg);
921 /* user has enabled ident lookups explicitly, but ... */
922 Config_Error(LOG_WARNING,
923 "%s: line %d: NoIdent=False, but ngircd was built without IDENT support",
924 NGIRCd_ConfFile, Line);
930 /* the default setting for all the WANT_IPV6 special options is 'true' */
931 if( strcasecmp( Var, "ConnectIPv6" ) == 0 ) {
932 /* connect to other hosts using ipv6, if they have an AAAA record? */
933 Conf_ConnectIPv6 = Check_ArgIsTrue( Arg );
936 if( strcasecmp( Var, "ConnectIPv4" ) == 0 ) {
937 /* connect to other hosts using ipv4.
938 * again, this can be used for ipv6-only setups */
939 Conf_ConnectIPv4 = Check_ArgIsTrue( Arg );
943 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
944 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
945 Conf_OperCanMode = Check_ArgIsTrue( Arg );
948 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
949 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
950 Conf_OperServerMode = Check_ArgIsTrue( Arg );
953 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
954 /* Maximum number of connections. 0 -> "no limit". */
956 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
959 Conf_MaxConnections = atol( Arg );
962 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
963 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
965 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
968 Conf_MaxConnectionsIP = atoi( Arg );
971 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
972 /* Maximum number of channels a user can join. 0 -> "no limit". */
974 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
977 Conf_MaxJoins = atoi( Arg );
980 if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
981 /* Maximum length of a nick name; must be same on all servers
982 * within the IRC network! */
983 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
987 if( strcasecmp( Var, "Listen" ) == 0 ) {
988 /* IP-Address to bind sockets */
989 if (Conf_ListenAddress) {
990 Config_Error(LOG_ERR, "Multiple Listen= options, ignoring: %s", Arg);
993 Conf_ListenAddress = strdup_warn(Arg);
995 * if allocation fails, we're in trouble:
996 * we cannot ignore the error -- otherwise ngircd
997 * would listen on all interfaces.
999 if (!Conf_ListenAddress) {
1000 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
1007 if( strcasecmp( Var, "SSLPorts" ) == 0 ) {
1008 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1012 if( strcasecmp( Var, "SSLKeyFile" ) == 0 ) {
1013 assert(Conf_SSLOptions.KeyFile == NULL );
1014 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1017 if( strcasecmp( Var, "SSLCertFile" ) == 0 ) {
1018 assert(Conf_SSLOptions.CertFile == NULL );
1019 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1023 if( strcasecmp( Var, "SSLKeyFilePassword" ) == 0 ) {
1024 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1025 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1026 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Could not copy %s: %s!",
1027 NGIRCd_ConfFile, Line, Var, strerror(errno));
1030 if( strcasecmp( Var, "SSLDHFile" ) == 0 ) {
1031 assert(Conf_SSLOptions.DHFile == NULL);
1032 Conf_SSLOptions.DHFile = strdup_warn( Arg );
1036 Config_Error(LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
1037 NGIRCd_ConfFile, Line, Var);
1038 } /* Handle_GLOBAL */
1042 Handle_OPERATOR( int Line, char *Var, char *Arg )
1044 unsigned int opercount;
1047 assert( Var != NULL );
1048 assert( Arg != NULL );
1049 assert( Conf_Oper_Count > 0 );
1051 if ( Conf_Oper_Count == 0 )
1054 opercount = Conf_Oper_Count - 1;
1056 if( strcasecmp( Var, "Name" ) == 0 ) {
1057 /* Name of IRC operator */
1058 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
1059 if (len >= sizeof( Conf_Oper[opercount].name ))
1060 Config_Error_TooLong( Line, Var );
1063 if( strcasecmp( Var, "Password" ) == 0 ) {
1064 /* Password of IRC operator */
1065 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
1066 if (len >= sizeof( Conf_Oper[opercount].pwd ))
1067 Config_Error_TooLong( Line, Var );
1070 if( strcasecmp( Var, "Mask" ) == 0 ) {
1071 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
1073 Conf_Oper[opercount].mask = strdup_warn( Arg );
1076 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
1077 NGIRCd_ConfFile, Line, Var );
1078 } /* Handle_OPERATOR */
1082 Handle_SERVER( int Line, char *Var, char *Arg )
1088 assert( Var != NULL );
1089 assert( Arg != NULL );
1091 /* Ignore server block if no space is left in server configuration structure */
1092 if( New_Server_Idx <= NONE ) return;
1094 if( strcasecmp( Var, "Host" ) == 0 ) {
1095 /* Hostname of the server */
1096 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1097 if (len >= sizeof( New_Server.host ))
1098 Config_Error_TooLong ( Line, Var );
1101 if( strcasecmp( Var, "Name" ) == 0 ) {
1102 /* Name of the server ("Nick"/"ID") */
1103 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1104 if (len >= sizeof( New_Server.name ))
1105 Config_Error_TooLong( Line, Var );
1108 if (strcasecmp(Var, "Bind") == 0) {
1109 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1112 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1113 NGIRCd_ConfFile, Line, Arg);
1116 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1117 /* Password of this server which is sent to the peer */
1119 Config_Error(LOG_ERR,
1120 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1121 NGIRCd_ConfFile, Line);
1123 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1124 if (len >= sizeof( New_Server.pwd_in ))
1125 Config_Error_TooLong( Line, Var );
1128 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1129 /* Passwort of the peer which must be received */
1130 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1131 if (len >= sizeof( New_Server.pwd_out ))
1132 Config_Error_TooLong( Line, Var );
1135 if( strcasecmp( Var, "Port" ) == 0 ) {
1136 /* Port to which this server should connect */
1138 if( port > 0 && port < 0xFFFF )
1139 New_Server.port = (UINT16)port;
1141 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
1142 NGIRCd_ConfFile, Line, port );
1146 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1147 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1151 if( strcasecmp( Var, "Group" ) == 0 ) {
1154 if( ! isdigit( (int)*Arg ))
1155 Config_Error_NaN( Line, Var );
1158 New_Server.group = atoi( Arg );
1161 if( strcasecmp( Var, "Passive" ) == 0 ) {
1162 if (Check_ArgIsTrue(Arg))
1163 New_Server.flags |= CONF_SFLAG_DISABLED;
1166 if (strcasecmp(Var, "ServiceMask") == 0) {
1167 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1168 sizeof(New_Server.svs_mask));
1169 if (len >= sizeof(New_Server.svs_mask))
1170 Config_Error_TooLong(Line, Var);
1174 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
1175 NGIRCd_ConfFile, Line, Var );
1176 } /* Handle_SERVER */
1180 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1182 size_t size = sizeof(new_chan->name);
1183 char *dest = new_chan->name;
1185 if (!Channel_IsValidName(name)) {
1187 * maybe user forgot to add a '#'.
1188 * This is only here for user convenience.
1194 return size > strlcpy(dest, name, size);
1199 Handle_CHANNEL(int Line, char *Var, char *Arg)
1203 struct Conf_Channel *chan;
1206 assert( Var != NULL );
1207 assert( Arg != NULL );
1208 assert(Conf_Channel_Count > 0);
1210 chancount = Conf_Channel_Count - 1;
1212 chan = array_alloc(&Conf_Channels, sizeof(*chan), chancount);
1214 Config_Error(LOG_ERR, "Could not allocate memory for predefined channel (%d:%s = %s)", Line, Var, Arg);
1217 if (strcasecmp(Var, "Name") == 0) {
1218 if (!Handle_Channelname(chan, Arg))
1219 Config_Error_TooLong(Line, Var);
1222 if (strcasecmp(Var, "Modes") == 0) {
1224 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1225 if (len >= sizeof(chan->modes))
1226 Config_Error_TooLong( Line, Var );
1229 if( strcasecmp( Var, "Topic" ) == 0 ) {
1231 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1232 if (len >= sizeof(chan->topic))
1233 Config_Error_TooLong( Line, Var );
1236 if( strcasecmp( Var, "Key" ) == 0 ) {
1237 /* Initial Channel Key (mode k) */
1238 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1239 if (len >= sizeof(chan->key))
1240 Config_Error_TooLong(Line, Var);
1243 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1244 /* maximum user limit, mode l */
1245 chan->maxusers = (unsigned long) atol(Arg);
1246 if (chan->maxusers == 0)
1247 Config_Error_NaN(Line, Var);
1250 if (strcasecmp(Var, "KeyFile") == 0) {
1252 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1253 if (len >= sizeof(chan->keyfile))
1254 Config_Error_TooLong(Line, Var);
1258 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1259 NGIRCd_ConfFile, Line, Var );
1260 } /* Handle_CHANNEL */
1264 Validate_Config(bool Configtest, bool Rehash)
1266 /* Validate configuration settings. */
1269 int i, servers, servers_once;
1271 bool config_valid = true;
1274 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1275 ptr = Conf_ServerName;
1277 if (*ptr >= 'a' && *ptr <= 'z') continue;
1278 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1279 if (*ptr >= '0' && *ptr <= '9') continue;
1280 if (ptr > Conf_ServerName) {
1281 if (*ptr == '.' || *ptr == '-')
1284 Conf_ServerName[0] = '\0';
1288 if (!Conf_ServerName[0]) {
1289 /* No server name configured! */
1290 config_valid = false;
1291 Config_Error(LOG_ALERT,
1292 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1294 if (!Configtest && !Rehash) {
1295 Config_Error(LOG_ALERT,
1296 "%s exiting due to fatal errors!",
1302 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1303 /* No dot in server name! */
1304 config_valid = false;
1305 Config_Error(LOG_ALERT,
1306 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1309 Config_Error(LOG_ALERT,
1310 "%s exiting due to fatal errors!",
1317 if (!Conf_ServerAdminMail[0]) {
1318 /* No administrative contact configured! */
1319 config_valid = false;
1320 Config_Error(LOG_ALERT,
1321 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1324 Config_Error(LOG_ALERT,
1325 "%s exiting due to fatal errors!",
1332 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1333 && !Conf_ServerAdminMail[0]) {
1334 /* No administrative information configured! */
1335 Config_Error(LOG_WARNING,
1336 "No administrative information configured but required by RFC!");
1340 servers = servers_once = 0;
1341 for (i = 0; i < MAX_SERVERS; i++) {
1342 if (Conf_Server[i].name[0]) {
1344 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1349 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1350 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1353 return config_valid;
1354 } /* Validate_Config */
1358 Config_Error_TooLong ( const int Line, const char *Item )
1360 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1365 Config_Error_NaN( const int Line, const char *Item )
1367 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1368 NGIRCd_ConfFile, Line, Item );
1373 static void Config_Error( const int Level, const char *Format, ... )
1375 static void Config_Error( Level, Format, va_alist )
1381 /* Error! Write to console and/or logfile. */
1383 char msg[MAX_LOG_MSG_LEN];
1386 assert( Format != NULL );
1389 va_start( ap, Format );
1393 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1396 /* During "normal operations" the log functions of the daemon should
1397 * be used, but during testing of the configuration file, all messages
1398 * should go directly to the console: */
1399 if (Use_Log) Log( Level, "%s", msg );
1401 } /* Config_Error */
1405 Init_Server_Struct( CONF_SERVER *Server )
1407 /* Initialize server configuration structur to default values */
1409 assert( Server != NULL );
1411 memset( Server, 0, sizeof (CONF_SERVER) );
1413 Server->group = NONE;
1414 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1416 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1418 Resolve_Init(&Server->res_stat);
1419 Server->conn_id = NONE;
1420 memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
1421 } /* Init_Server_Struct */