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>
53 static bool Use_Log = true;
54 static CONF_SERVER New_Server;
55 static int New_Server_Idx;
57 static size_t Conf_Channel_Count;
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. */
210 size_t predef_channel_count;
211 struct Conf_Channel *predef_chan;
215 if (! Read_Config(true))
218 config_valid = Validate_Config(true, false);
220 /* If stdin and stdout ("you can read our nice message and we can
221 * read in your keypress") are valid tty's, wait for a key: */
222 if( isatty( fileno( stdin )) && isatty( fileno( stdout ))) {
223 puts( "OK, press enter to see a dump of your service configuration ..." );
226 puts( "Ok, dump of your server configuration follows:\n" );
230 printf( " Name = %s\n", Conf_ServerName );
231 printf( " Info = %s\n", Conf_ServerInfo );
232 printf( " Password = %s\n", Conf_ServerPwd );
233 printf( " AdminInfo1 = %s\n", Conf_ServerAdmin1 );
234 printf( " AdminInfo2 = %s\n", Conf_ServerAdmin2 );
235 printf( " AdminEMail = %s\n", Conf_ServerAdminMail );
236 printf( " MotdFile = %s\n", Conf_MotdFile );
237 printf( " MotdPhrase = %s\n", Conf_MotdPhrase );
238 printf( " ChrootDir = %s\n", Conf_Chroot );
239 printf( " PidFile = %s\n", Conf_PidFile);
240 printf(" Listen = %s\n", Conf_ListenAddress);
241 fputs(" Ports = ", stdout);
243 ports_puts(&Conf_ListenPorts);
245 fputs(" SSLPorts = ", stdout);
246 ports_puts(&Conf_SSLOptions.ListenPorts);
250 pwd = getpwuid( Conf_UID );
251 if( pwd ) printf( " ServerUID = %s\n", pwd->pw_name );
252 else printf( " ServerUID = %ld\n", (long)Conf_UID );
253 grp = getgrgid( Conf_GID );
254 if( grp ) printf( " ServerGID = %s\n", grp->gr_name );
255 else printf( " ServerGID = %ld\n", (long)Conf_GID );
256 printf( " PingTimeout = %d\n", Conf_PingTimeout );
257 printf( " PongTimeout = %d\n", Conf_PongTimeout );
258 printf( " ConnectRetry = %d\n", Conf_ConnectRetry );
259 printf( " OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
260 printf( " OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
261 printf( " PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
262 printf( " NoDNS = %s\n", yesno_to_str(Conf_NoDNS));
263 printf( " NoIdent = %s\n", yesno_to_str(Conf_NoIdent));
266 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
267 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
269 printf( " MaxConnections = %ld\n", Conf_MaxConnections);
270 printf( " MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
271 printf( " MaxJoins = %d\n", Conf_MaxJoins>0 ? Conf_MaxJoins : -1);
272 printf( " MaxNickLength = %u\n\n", Conf_MaxNickLength - 1);
274 for( i = 0; i < Conf_Oper_Count; i++ ) {
275 if( ! Conf_Oper[i].name[0] ) continue;
277 /* Valid "Operator" section */
278 puts( "[OPERATOR]" );
279 printf( " Name = %s\n", Conf_Oper[i].name );
280 printf( " Password = %s\n", Conf_Oper[i].pwd );
281 if ( Conf_Oper[i].mask ) printf( " Mask = %s\n", Conf_Oper[i].mask );
285 for( i = 0; i < MAX_SERVERS; i++ ) {
286 if( ! Conf_Server[i].name[0] ) continue;
288 /* Valid "Server" section */
290 printf( " Name = %s\n", Conf_Server[i].name );
291 printf( " Host = %s\n", Conf_Server[i].host );
292 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
294 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
296 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
297 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
298 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
299 printf( " Group = %d\n", Conf_Server[i].group );
300 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
303 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
304 predef_chan = array_start(&Conf_Channels);
306 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
307 if (!predef_chan->name[0])
310 /* Valid "Channel" section */
312 printf(" Name = %s\n", predef_chan->name);
313 printf(" Modes = %s\n", predef_chan->modes);
314 printf(" Key = %s\n", predef_chan->key);
315 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
316 printf(" Topic = %s\n", predef_chan->topic);
317 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
320 return (config_valid ? 0 : 1);
325 Conf_UnsetServer( CONN_ID Idx )
327 /* Set next time for next connection attempt, if this is a server
328 * link that is (still) configured here. If the server is set as
329 * "once", delete it from our configuration.
330 * Non-Server-Connections will be silently ignored. */
335 /* Check all our configured servers */
336 for( i = 0; i < MAX_SERVERS; i++ ) {
337 if( Conf_Server[i].conn_id != Idx ) continue;
339 /* Gotcha! Mark server configuration as "unused": */
340 Conf_Server[i].conn_id = NONE;
342 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
343 /* Delete configuration here */
344 Init_Server_Struct( &Conf_Server[i] );
346 /* Set time for next connect attempt */
348 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
349 /* The connection has been "long", so we don't
350 * require the next attempt to be delayed. */
351 Conf_Server[i].lasttry =
352 t - Conf_ConnectRetry + RECONNECT_DELAY;
354 Conf_Server[i].lasttry = t;
357 } /* Conf_UnsetServer */
361 Conf_SetServer( int ConfServer, CONN_ID Idx )
363 /* Set connection for specified configured server */
365 assert( ConfServer > NONE );
366 assert( Idx > NONE );
368 Conf_Server[ConfServer].conn_id = Idx;
369 } /* Conf_SetServer */
373 Conf_GetServer( CONN_ID Idx )
375 /* Get index of server in configuration structure */
379 assert( Idx > NONE );
381 for( i = 0; i < MAX_SERVERS; i++ ) {
382 if( Conf_Server[i].conn_id == Idx ) return i;
385 } /* Conf_GetServer */
389 Conf_EnableServer( char *Name, UINT16 Port )
391 /* Enable specified server and adjust port */
395 assert( Name != NULL );
397 for( i = 0; i < MAX_SERVERS; i++ ) {
398 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
399 /* Gotcha! Set port and enable server: */
400 Conf_Server[i].port = Port;
401 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
406 } /* Conf_EnableServer */
410 Conf_EnablePassiveServer(const char *Name)
412 /* Enable specified server */
415 assert( Name != NULL );
416 for (i = 0; i < MAX_SERVERS; i++) {
417 if ((strcasecmp( Conf_Server[i].name, Name ) == 0) && (Conf_Server[i].port > 0)) {
418 /* BINGO! Enable server */
419 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
424 } /* Conf_EnablePassiveServer */
428 Conf_DisableServer( char *Name )
430 /* Enable specified server and adjust port */
434 assert( Name != NULL );
436 for( i = 0; i < MAX_SERVERS; i++ ) {
437 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
438 /* Gotcha! Disable and disconnect server: */
439 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
440 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
445 } /* Conf_DisableServer */
449 Conf_AddServer( char *Name, UINT16 Port, char *Host, char *MyPwd, char *PeerPwd )
451 /* Add new server to configuration */
455 assert( Name != NULL );
456 assert( Host != NULL );
457 assert( MyPwd != NULL );
458 assert( PeerPwd != NULL );
460 /* Search unused item in server configuration structure */
461 for( i = 0; i < MAX_SERVERS; i++ ) {
462 /* Is this item used? */
463 if( ! Conf_Server[i].name[0] ) break;
465 if( i >= MAX_SERVERS ) return false;
467 Init_Server_Struct( &Conf_Server[i] );
468 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
469 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
470 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
471 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
472 Conf_Server[i].port = Port;
473 Conf_Server[i].flags = CONF_SFLAG_ONCE;
476 } /* Conf_AddServer */
480 * Check if the given nick name is an service
483 Conf_IsService(int ConfServer, char *Nick)
485 return MatchCaseInsensitive(Conf_Server[ConfServer].svs_mask, Nick);
486 } /* Conf_IsService */
490 Set_Defaults( bool InitServers )
492 /* Initialize configuration variables with default values. */
496 strcpy( Conf_ServerName, "" );
497 snprintf( Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
498 strcpy( Conf_ServerPwd, "" );
500 strcpy( Conf_ServerAdmin1, "" );
501 strcpy( Conf_ServerAdmin2, "" );
502 strcpy( Conf_ServerAdminMail, "" );
504 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
505 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
507 strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
509 strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
511 strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
513 free(Conf_ListenAddress);
514 Conf_ListenAddress = NULL;
515 Conf_UID = Conf_GID = 0;
517 Conf_PingTimeout = 120;
518 Conf_PongTimeout = 20;
520 Conf_ConnectRetry = 60;
523 Conf_Channel_Count = 0;
525 Conf_OperCanMode = false;
527 Conf_NoIdent = false;
528 Conf_PredefChannelsOnly = false;
529 Conf_OperServerMode = false;
531 Conf_ConnectIPv4 = true;
532 Conf_ConnectIPv6 = true;
534 Conf_MaxConnections = 0;
535 Conf_MaxConnectionsIP = 5;
537 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
539 /* Initialize server configuration structures */
540 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
545 Read_Config( bool ngircd_starting )
547 /* Read configuration file. */
549 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
550 const UINT16 defaultport = 6667;
554 /* Open configuration file */
555 fd = fopen( NGIRCd_ConfFile, "r" );
557 /* No configuration file found! */
558 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
559 NGIRCd_ConfFile, strerror( errno ));
560 if (!ngircd_starting)
562 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
566 Set_Defaults( ngircd_starting );
568 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
570 /* Clean up server configuration structure: mark all already
571 * configured servers as "once" so that they are deleted
572 * after the next disconnect and delete all unused servers.
573 * And delete all servers which are "duplicates" of servers
574 * that are already marked as "once" (such servers have been
575 * created by the last rehash but are now useless). */
576 for( i = 0; i < MAX_SERVERS; i++ ) {
577 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
579 /* This structure is in use ... */
580 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
581 /* Check for duplicates */
582 for( n = 0; n < MAX_SERVERS; n++ ) {
583 if( n == i ) continue;
585 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
586 Init_Server_Struct( &Conf_Server[n] );
588 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
594 /* Mark server as "once" */
595 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
596 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
601 /* Initialize variables */
603 strcpy( section, "" );
604 Init_Server_Struct( &New_Server );
605 New_Server_Idx = NONE;
609 /* Read configuration file */
611 if( ! fgets( str, LINE_LEN, fd )) break;
615 /* Skip comments and empty lines */
616 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
618 /* Is this the beginning of a new section? */
619 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
620 strlcpy( section, str, sizeof( section ));
621 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
624 if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
625 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
626 Config_Error( LOG_ERR, "Too many operators configured.");
628 /* Initialize new operator structure */
629 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
630 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
631 if (Conf_Oper[Conf_Oper_Count].mask) {
632 free(Conf_Oper[Conf_Oper_Count].mask );
633 Conf_Oper[Conf_Oper_Count].mask = NULL;
639 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
640 /* Check if there is already a server to add */
641 if( New_Server.name[0] ) {
642 /* Copy data to "real" server structure */
643 assert( New_Server_Idx > NONE );
644 Conf_Server[New_Server_Idx] = New_Server;
647 /* Re-init structure for new server */
648 Init_Server_Struct( &New_Server );
650 /* Search unused item in server configuration structure */
651 for( i = 0; i < MAX_SERVERS; i++ ) {
652 /* Is this item used? */
653 if( ! Conf_Server[i].name[0] ) break;
655 if( i >= MAX_SERVERS ) {
656 /* Oops, no free item found! */
657 Config_Error( LOG_ERR, "Too many servers configured." );
658 New_Server_Idx = NONE;
660 else New_Server_Idx = i;
663 if (strcasecmp(section, "[CHANNEL]") == 0) {
664 Conf_Channel_Count++;
668 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
671 if( section[0] == 0x1 ) continue;
673 /* Split line into variable name and parameters */
674 ptr = strchr( str, '=' );
676 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
680 var = str; ngt_TrimStr( var );
681 arg = ptr + 1; ngt_TrimStr( arg );
683 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
684 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
685 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
686 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
687 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
690 /* Close configuration file */
693 /* Check if there is still a server to add */
694 if( New_Server.name[0] ) {
695 /* Copy data to "real" server structure */
696 assert( New_Server_Idx > NONE );
697 Conf_Server[New_Server_Idx] = New_Server;
700 if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
701 if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
702 Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
703 (unsigned int) defaultport, strerror(errno));
708 if (!Conf_ListenAddress)
709 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
711 if (!Conf_ListenAddress) {
712 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
720 Check_ArgIsTrue( const char *Arg )
722 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
723 if( strcasecmp( Arg, "true" ) == 0 ) return true;
724 if( atoi( Arg ) != 0 ) return true;
727 } /* Check_ArgIsTrue */
730 static unsigned int Handle_MaxNickLength(int Line, const char *Arg)
734 new = (unsigned) atoi(Arg) + 1;
735 if (new > CLIENT_NICK_LEN) {
736 Config_Error(LOG_WARNING,
737 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
738 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
739 return CLIENT_NICK_LEN;
742 Config_Error(LOG_WARNING,
743 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
744 NGIRCd_ConfFile, Line);
748 } /* Handle_MaxNickLength */
752 Handle_GLOBAL( int Line, char *Var, char *Arg )
759 assert( Var != NULL );
760 assert( Arg != NULL );
762 if( strcasecmp( Var, "Name" ) == 0 ) {
764 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
765 if (len >= sizeof( Conf_ServerName ))
766 Config_Error_TooLong( Line, Var );
769 if( strcasecmp( Var, "Info" ) == 0 ) {
770 /* Info text of server */
771 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
772 if (len >= sizeof( Conf_ServerInfo ))
773 Config_Error_TooLong ( Line, Var );
776 if( strcasecmp( Var, "Password" ) == 0 ) {
777 /* Global server password */
778 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
779 if (len >= sizeof( Conf_ServerPwd ))
780 Config_Error_TooLong( Line, Var );
783 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
784 /* Administrative info #1 */
785 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
786 if (len >= sizeof( Conf_ServerAdmin1 ))
787 Config_Error_TooLong ( Line, Var );
790 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
791 /* Administrative info #2 */
792 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
793 if (len >= sizeof( Conf_ServerAdmin2 ))
794 Config_Error_TooLong ( Line, Var );
797 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
798 /* Administrative email contact */
799 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
800 if (len >= sizeof( Conf_ServerAdminMail ))
801 Config_Error_TooLong( Line, Var );
805 if( strcasecmp( Var, "Ports" ) == 0 ) {
806 ports_parse(&Conf_ListenPorts, Line, Arg);
809 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
810 /* "Message of the day" (MOTD) file */
811 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
812 if (len >= sizeof( Conf_MotdFile ))
813 Config_Error_TooLong( Line, Var );
816 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
817 /* "Message of the day" phrase (instead of file) */
818 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
819 if (len >= sizeof( Conf_MotdPhrase ))
820 Config_Error_TooLong( Line, Var );
823 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
824 /* directory for chroot() */
825 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
826 if (len >= sizeof( Conf_Chroot ))
827 Config_Error_TooLong( Line, Var );
830 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
831 /* name of pidfile */
832 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
833 if (len >= sizeof( Conf_PidFile ))
834 Config_Error_TooLong( Line, Var );
837 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
838 /* UID the daemon should switch to */
839 pwd = getpwnam( Arg );
840 if( pwd ) Conf_UID = pwd->pw_uid;
843 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
846 Conf_UID = (unsigned int)atoi( Arg );
850 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
851 /* GID the daemon should use */
852 grp = getgrnam( Arg );
853 if( grp ) Conf_GID = grp->gr_gid;
856 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
859 Conf_GID = (unsigned int)atoi( Arg );
863 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
865 Conf_PingTimeout = atoi( Arg );
866 if( Conf_PingTimeout < 5 ) {
867 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
868 NGIRCd_ConfFile, Line );
869 Conf_PingTimeout = 5;
873 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
875 Conf_PongTimeout = atoi( Arg );
876 if( Conf_PongTimeout < 5 ) {
877 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
878 NGIRCd_ConfFile, Line );
879 Conf_PongTimeout = 5;
883 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
884 /* Seconds between connection attempts to other servers */
885 Conf_ConnectRetry = atoi( Arg );
886 if( Conf_ConnectRetry < 5 ) {
887 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
888 NGIRCd_ConfFile, Line );
889 Conf_ConnectRetry = 5;
893 if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
894 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
895 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
898 if( strcasecmp( Var, "NoDNS" ) == 0 ) {
899 /* don't do reverse dns lookups when clients connect? */
900 Conf_NoDNS = Check_ArgIsTrue( Arg );
903 if (strcasecmp(Var, "NoIdent") == 0) {
904 /* don't do IDENT lookups when clients connect? */
905 Conf_NoIdent = Check_ArgIsTrue(Arg);
908 /* user has enabled ident lookups explicitly, but ... */
909 Config_Error(LOG_WARNING,
910 "%s: line %d: NoIdent=False, but ngircd was built without IDENT support",
911 NGIRCd_ConfFile, Line);
917 /* the default setting for all the WANT_IPV6 special options is 'true' */
918 if( strcasecmp( Var, "ConnectIPv6" ) == 0 ) {
919 /* connect to other hosts using ipv6, if they have an AAAA record? */
920 Conf_ConnectIPv6 = Check_ArgIsTrue( Arg );
923 if( strcasecmp( Var, "ConnectIPv4" ) == 0 ) {
924 /* connect to other hosts using ipv4.
925 * again, this can be used for ipv6-only setups */
926 Conf_ConnectIPv4 = Check_ArgIsTrue( Arg );
930 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
931 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
932 Conf_OperCanMode = Check_ArgIsTrue( Arg );
935 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
936 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
937 Conf_OperServerMode = Check_ArgIsTrue( Arg );
940 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
941 /* Maximum number of connections. 0 -> "no limit". */
943 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
946 Conf_MaxConnections = atol( Arg );
949 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
950 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
952 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
955 Conf_MaxConnectionsIP = atoi( Arg );
958 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
959 /* Maximum number of channels a user can join. 0 -> "no limit". */
961 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
964 Conf_MaxJoins = atoi( Arg );
967 if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
968 /* Maximum length of a nick name; must be same on all servers
969 * within the IRC network! */
970 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
974 if( strcasecmp( Var, "Listen" ) == 0 ) {
975 /* IP-Address to bind sockets */
976 if (Conf_ListenAddress) {
977 Config_Error(LOG_ERR, "Multiple Listen= options, ignoring: %s", Arg);
980 Conf_ListenAddress = strdup_warn(Arg);
982 * if allocation fails, we're in trouble:
983 * we cannot ignore the error -- otherwise ngircd
984 * would listen on all interfaces.
986 if (!Conf_ListenAddress) {
987 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
994 if( strcasecmp( Var, "SSLPorts" ) == 0 ) {
995 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
999 if( strcasecmp( Var, "SSLKeyFile" ) == 0 ) {
1000 assert(Conf_SSLOptions.KeyFile == NULL );
1001 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1004 if( strcasecmp( Var, "SSLCertFile" ) == 0 ) {
1005 assert(Conf_SSLOptions.CertFile == NULL );
1006 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1010 if( strcasecmp( Var, "SSLKeyFilePassword" ) == 0 ) {
1011 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1012 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1013 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Could not copy %s: %s!",
1014 NGIRCd_ConfFile, Line, Var, strerror(errno));
1017 if( strcasecmp( Var, "SSLDHFile" ) == 0 ) {
1018 assert(Conf_SSLOptions.DHFile == NULL);
1019 Conf_SSLOptions.DHFile = strdup_warn( Arg );
1023 Config_Error(LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
1024 NGIRCd_ConfFile, Line, Var);
1025 } /* Handle_GLOBAL */
1029 Handle_OPERATOR( int Line, char *Var, char *Arg )
1031 unsigned int opercount;
1034 assert( Var != NULL );
1035 assert( Arg != NULL );
1036 assert( Conf_Oper_Count > 0 );
1038 if ( Conf_Oper_Count == 0 )
1041 opercount = Conf_Oper_Count - 1;
1043 if( strcasecmp( Var, "Name" ) == 0 ) {
1044 /* Name of IRC operator */
1045 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
1046 if (len >= sizeof( Conf_Oper[opercount].name ))
1047 Config_Error_TooLong( Line, Var );
1050 if( strcasecmp( Var, "Password" ) == 0 ) {
1051 /* Password of IRC operator */
1052 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
1053 if (len >= sizeof( Conf_Oper[opercount].pwd ))
1054 Config_Error_TooLong( Line, Var );
1057 if( strcasecmp( Var, "Mask" ) == 0 ) {
1058 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
1060 Conf_Oper[opercount].mask = strdup_warn( Arg );
1063 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
1064 NGIRCd_ConfFile, Line, Var );
1065 } /* Handle_OPERATOR */
1069 Handle_SERVER( int Line, char *Var, char *Arg )
1075 assert( Var != NULL );
1076 assert( Arg != NULL );
1078 /* Ignore server block if no space is left in server configuration structure */
1079 if( New_Server_Idx <= NONE ) return;
1081 if( strcasecmp( Var, "Host" ) == 0 ) {
1082 /* Hostname of the server */
1083 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1084 if (len >= sizeof( New_Server.host ))
1085 Config_Error_TooLong ( Line, Var );
1088 if( strcasecmp( Var, "Name" ) == 0 ) {
1089 /* Name of the server ("Nick"/"ID") */
1090 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1091 if (len >= sizeof( New_Server.name ))
1092 Config_Error_TooLong( Line, Var );
1095 if (strcasecmp(Var, "Bind") == 0) {
1096 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1099 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1100 NGIRCd_ConfFile, Line, Arg);
1103 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1104 /* Password of this server which is sent to the peer */
1106 Config_Error(LOG_ERR,
1107 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1108 NGIRCd_ConfFile, Line);
1110 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1111 if (len >= sizeof( New_Server.pwd_in ))
1112 Config_Error_TooLong( Line, Var );
1115 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1116 /* Passwort of the peer which must be received */
1117 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1118 if (len >= sizeof( New_Server.pwd_out ))
1119 Config_Error_TooLong( Line, Var );
1122 if( strcasecmp( Var, "Port" ) == 0 ) {
1123 /* Port to which this server should connect */
1125 if( port > 0 && port < 0xFFFF )
1126 New_Server.port = (UINT16)port;
1128 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
1129 NGIRCd_ConfFile, Line, port );
1133 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1134 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1138 if( strcasecmp( Var, "Group" ) == 0 ) {
1141 if( ! isdigit( (int)*Arg ))
1142 Config_Error_NaN( Line, Var );
1145 New_Server.group = atoi( Arg );
1148 if( strcasecmp( Var, "Passive" ) == 0 ) {
1149 if (Check_ArgIsTrue(Arg))
1150 New_Server.flags |= CONF_SFLAG_DISABLED;
1153 if (strcasecmp(Var, "ServiceMask") == 0) {
1154 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1155 sizeof(New_Server.svs_mask));
1156 if (len >= sizeof(New_Server.svs_mask))
1157 Config_Error_TooLong(Line, Var);
1161 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
1162 NGIRCd_ConfFile, Line, Var );
1163 } /* Handle_SERVER */
1167 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1169 size_t size = sizeof(new_chan->name);
1170 char *dest = new_chan->name;
1172 /* Channels names must begin with "&" or "#", if it is
1173 * missing, add a '#'. This is only here for user convenience.
1175 if (*name && *name != '#' && *name != '&') {
1180 return size > strlcpy(dest, name, size);
1185 Handle_CHANNEL(int Line, char *Var, char *Arg)
1189 struct Conf_Channel *chan;
1192 assert( Var != NULL );
1193 assert( Arg != NULL );
1194 assert(Conf_Channel_Count > 0);
1196 chancount = Conf_Channel_Count - 1;
1198 chan = array_alloc(&Conf_Channels, sizeof(*chan), chancount);
1200 Config_Error(LOG_ERR, "Could not allocate memory for predefined channel (%d:%s = %s)", Line, Var, Arg);
1203 if (strcasecmp(Var, "Name") == 0) {
1204 if (!Handle_Channelname(chan, Arg))
1205 Config_Error_TooLong(Line, Var);
1208 if (strcasecmp(Var, "Modes") == 0) {
1210 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1211 if (len >= sizeof(chan->modes))
1212 Config_Error_TooLong( Line, Var );
1215 if( strcasecmp( Var, "Topic" ) == 0 ) {
1217 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1218 if (len >= sizeof(chan->topic))
1219 Config_Error_TooLong( Line, Var );
1222 if( strcasecmp( Var, "Key" ) == 0 ) {
1223 /* Initial Channel Key (mode k) */
1224 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1225 if (len >= sizeof(chan->key))
1226 Config_Error_TooLong(Line, Var);
1229 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1230 /* maximum user limit, mode l */
1231 chan->maxusers = (unsigned long) atol(Arg);
1232 if (chan->maxusers == 0)
1233 Config_Error_NaN(Line, Var);
1236 if (strcasecmp(Var, "KeyFile") == 0) {
1238 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1239 if (len >= sizeof(chan->keyfile))
1240 Config_Error_TooLong(Line, Var);
1244 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1245 NGIRCd_ConfFile, Line, Var );
1246 } /* Handle_CHANNEL */
1250 Validate_Config(bool Configtest, bool Rehash)
1252 /* Validate configuration settings. */
1255 int i, servers, servers_once;
1257 bool config_valid = true;
1260 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1261 ptr = Conf_ServerName;
1263 if (*ptr >= 'a' && *ptr <= 'z') continue;
1264 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1265 if (*ptr >= '0' && *ptr <= '9') continue;
1266 if (ptr > Conf_ServerName) {
1267 if (*ptr == '.' || *ptr == '-')
1270 Conf_ServerName[0] = '\0';
1274 if (!Conf_ServerName[0]) {
1275 /* No server name configured! */
1276 config_valid = false;
1277 Config_Error(LOG_ALERT,
1278 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1280 if (!Configtest && !Rehash) {
1281 Config_Error(LOG_ALERT,
1282 "%s exiting due to fatal errors!",
1288 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1289 /* No dot in server name! */
1290 config_valid = false;
1291 Config_Error(LOG_ALERT,
1292 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1295 Config_Error(LOG_ALERT,
1296 "%s exiting due to fatal errors!",
1303 if (!Conf_ServerAdminMail[0]) {
1304 /* No administrative contact configured! */
1305 config_valid = false;
1306 Config_Error(LOG_ALERT,
1307 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1310 Config_Error(LOG_ALERT,
1311 "%s exiting due to fatal errors!",
1318 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1319 && !Conf_ServerAdminMail[0]) {
1320 /* No administrative information configured! */
1321 Config_Error(LOG_WARNING,
1322 "No administrative information configured but required by RFC!");
1326 servers = servers_once = 0;
1327 for (i = 0; i < MAX_SERVERS; i++) {
1328 if (Conf_Server[i].name[0]) {
1330 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1335 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1336 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1339 return config_valid;
1340 } /* Validate_Config */
1344 Config_Error_TooLong ( const int Line, const char *Item )
1346 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1351 Config_Error_NaN( const int Line, const char *Item )
1353 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1354 NGIRCd_ConfFile, Line, Item );
1359 static void Config_Error( const int Level, const char *Format, ... )
1361 static void Config_Error( Level, Format, va_alist )
1367 /* Error! Write to console and/or logfile. */
1369 char msg[MAX_LOG_MSG_LEN];
1372 assert( Format != NULL );
1375 va_start( ap, Format );
1379 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1382 /* During "normal operations" the log functions of the daemon should
1383 * be used, but during testing of the configuration file, all messages
1384 * should go directly to the console: */
1385 if (Use_Log) Log( Level, "%s", msg );
1387 } /* Config_Error */
1391 Init_Server_Struct( CONF_SERVER *Server )
1393 /* Initialize server configuration structur to default values */
1395 assert( Server != NULL );
1397 memset( Server, 0, sizeof (CONF_SERVER) );
1399 Server->group = NONE;
1400 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1402 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1404 Resolve_Init(&Server->res_stat);
1405 Server->conn_id = NONE;
1406 memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
1407 } /* Init_Server_Struct */