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\n", predef_chan->topic);
319 return (config_valid ? 0 : 1);
324 Conf_UnsetServer( CONN_ID Idx )
326 /* Set next time for next connection attempt, if this is a server
327 * link that is (still) configured here. If the server is set as
328 * "once", delete it from our configuration.
329 * Non-Server-Connections will be silently ignored. */
334 /* Check all our configured servers */
335 for( i = 0; i < MAX_SERVERS; i++ ) {
336 if( Conf_Server[i].conn_id != Idx ) continue;
338 /* Gotcha! Mark server configuration as "unused": */
339 Conf_Server[i].conn_id = NONE;
341 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
342 /* Delete configuration here */
343 Init_Server_Struct( &Conf_Server[i] );
345 /* Set time for next connect attempt */
347 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
348 /* The connection has been "long", so we don't
349 * require the next attempt to be delayed. */
350 Conf_Server[i].lasttry =
351 t - Conf_ConnectRetry + RECONNECT_DELAY;
353 Conf_Server[i].lasttry = t;
356 } /* Conf_UnsetServer */
360 Conf_SetServer( int ConfServer, CONN_ID Idx )
362 /* Set connection for specified configured server */
364 assert( ConfServer > NONE );
365 assert( Idx > NONE );
367 Conf_Server[ConfServer].conn_id = Idx;
368 } /* Conf_SetServer */
372 Conf_GetServer( CONN_ID Idx )
374 /* Get index of server in configuration structure */
378 assert( Idx > NONE );
380 for( i = 0; i < MAX_SERVERS; i++ ) {
381 if( Conf_Server[i].conn_id == Idx ) return i;
384 } /* Conf_GetServer */
388 Conf_EnableServer( char *Name, UINT16 Port )
390 /* Enable specified server and adjust port */
394 assert( Name != NULL );
396 for( i = 0; i < MAX_SERVERS; i++ ) {
397 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
398 /* Gotcha! Set port and enable server: */
399 Conf_Server[i].port = Port;
400 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
405 } /* Conf_EnableServer */
409 Conf_EnablePassiveServer(const char *Name)
411 /* Enable specified server */
414 assert( Name != NULL );
415 for (i = 0; i < MAX_SERVERS; i++) {
416 if ((strcasecmp( Conf_Server[i].name, Name ) == 0) && (Conf_Server[i].port > 0)) {
417 /* BINGO! Enable server */
418 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
423 } /* Conf_EnablePassiveServer */
427 Conf_DisableServer( char *Name )
429 /* Enable specified server and adjust port */
433 assert( Name != NULL );
435 for( i = 0; i < MAX_SERVERS; i++ ) {
436 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
437 /* Gotcha! Disable and disconnect server: */
438 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
439 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
444 } /* Conf_DisableServer */
448 Conf_AddServer( char *Name, UINT16 Port, char *Host, char *MyPwd, char *PeerPwd )
450 /* Add new server to configuration */
454 assert( Name != NULL );
455 assert( Host != NULL );
456 assert( MyPwd != NULL );
457 assert( PeerPwd != NULL );
459 /* Search unused item in server configuration structure */
460 for( i = 0; i < MAX_SERVERS; i++ ) {
461 /* Is this item used? */
462 if( ! Conf_Server[i].name[0] ) break;
464 if( i >= MAX_SERVERS ) return false;
466 Init_Server_Struct( &Conf_Server[i] );
467 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
468 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
469 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
470 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
471 Conf_Server[i].port = Port;
472 Conf_Server[i].flags = CONF_SFLAG_ONCE;
475 } /* Conf_AddServer */
479 * Check if the given nick name is an service
482 Conf_IsService(int ConfServer, char *Nick)
484 return MatchCaseInsensitive(Conf_Server[ConfServer].svs_mask, Nick);
485 } /* Conf_IsService */
489 Set_Defaults( bool InitServers )
491 /* Initialize configuration variables with default values. */
495 strcpy( Conf_ServerName, "" );
496 snprintf( Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
497 strcpy( Conf_ServerPwd, "" );
499 strcpy( Conf_ServerAdmin1, "" );
500 strcpy( Conf_ServerAdmin2, "" );
501 strcpy( Conf_ServerAdminMail, "" );
503 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
504 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
506 strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
508 strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
510 strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
512 free(Conf_ListenAddress);
513 Conf_ListenAddress = NULL;
514 Conf_UID = Conf_GID = 0;
516 Conf_PingTimeout = 120;
517 Conf_PongTimeout = 20;
519 Conf_ConnectRetry = 60;
522 Conf_Channel_Count = 0;
524 Conf_OperCanMode = false;
526 Conf_NoIdent = false;
527 Conf_PredefChannelsOnly = false;
528 Conf_OperServerMode = false;
530 Conf_ConnectIPv4 = true;
531 Conf_ConnectIPv6 = true;
533 Conf_MaxConnections = 0;
534 Conf_MaxConnectionsIP = 5;
536 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
538 /* Initialize server configuration structures */
539 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
544 Read_Config( bool ngircd_starting )
546 /* Read configuration file. */
548 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
549 const UINT16 defaultport = 6667;
553 /* Open configuration file */
554 fd = fopen( NGIRCd_ConfFile, "r" );
556 /* No configuration file found! */
557 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
558 NGIRCd_ConfFile, strerror( errno ));
559 if (!ngircd_starting)
561 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
565 Set_Defaults( ngircd_starting );
567 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
569 /* Clean up server configuration structure: mark all already
570 * configured servers as "once" so that they are deleted
571 * after the next disconnect and delete all unused servers.
572 * And delete all servers which are "duplicates" of servers
573 * that are already marked as "once" (such servers have been
574 * created by the last rehash but are now useless). */
575 for( i = 0; i < MAX_SERVERS; i++ ) {
576 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
578 /* This structure is in use ... */
579 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
580 /* Check for duplicates */
581 for( n = 0; n < MAX_SERVERS; n++ ) {
582 if( n == i ) continue;
584 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
585 Init_Server_Struct( &Conf_Server[n] );
587 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
593 /* Mark server as "once" */
594 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
595 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
600 /* Initialize variables */
602 strcpy( section, "" );
603 Init_Server_Struct( &New_Server );
604 New_Server_Idx = NONE;
608 /* Read configuration file */
610 if( ! fgets( str, LINE_LEN, fd )) break;
614 /* Skip comments and empty lines */
615 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
617 /* Is this the beginning of a new section? */
618 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
619 strlcpy( section, str, sizeof( section ));
620 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
623 if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
624 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
625 Config_Error( LOG_ERR, "Too many operators configured.");
627 /* Initialize new operator structure */
628 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
629 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
630 if (Conf_Oper[Conf_Oper_Count].mask) {
631 free(Conf_Oper[Conf_Oper_Count].mask );
632 Conf_Oper[Conf_Oper_Count].mask = NULL;
638 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
639 /* Check if there is already a server to add */
640 if( New_Server.name[0] ) {
641 /* Copy data to "real" server structure */
642 assert( New_Server_Idx > NONE );
643 Conf_Server[New_Server_Idx] = New_Server;
646 /* Re-init structure for new server */
647 Init_Server_Struct( &New_Server );
649 /* Search unused item in server configuration structure */
650 for( i = 0; i < MAX_SERVERS; i++ ) {
651 /* Is this item used? */
652 if( ! Conf_Server[i].name[0] ) break;
654 if( i >= MAX_SERVERS ) {
655 /* Oops, no free item found! */
656 Config_Error( LOG_ERR, "Too many servers configured." );
657 New_Server_Idx = NONE;
659 else New_Server_Idx = i;
662 if (strcasecmp(section, "[CHANNEL]") == 0) {
663 Conf_Channel_Count++;
667 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
670 if( section[0] == 0x1 ) continue;
672 /* Split line into variable name and parameters */
673 ptr = strchr( str, '=' );
675 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
679 var = str; ngt_TrimStr( var );
680 arg = ptr + 1; ngt_TrimStr( arg );
682 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
683 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
684 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
685 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
686 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
689 /* Close configuration file */
692 /* Check if there is still a server to add */
693 if( New_Server.name[0] ) {
694 /* Copy data to "real" server structure */
695 assert( New_Server_Idx > NONE );
696 Conf_Server[New_Server_Idx] = New_Server;
699 if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
700 if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
701 Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
702 (unsigned int) defaultport, strerror(errno));
707 if (!Conf_ListenAddress)
708 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
710 if (!Conf_ListenAddress) {
711 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
719 Check_ArgIsTrue( const char *Arg )
721 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
722 if( strcasecmp( Arg, "true" ) == 0 ) return true;
723 if( atoi( Arg ) != 0 ) return true;
726 } /* Check_ArgIsTrue */
729 static unsigned int Handle_MaxNickLength(int Line, const char *Arg)
733 new = (unsigned) atoi(Arg) + 1;
734 if (new > CLIENT_NICK_LEN) {
735 Config_Error(LOG_WARNING,
736 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
737 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
738 return CLIENT_NICK_LEN;
741 Config_Error(LOG_WARNING,
742 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
743 NGIRCd_ConfFile, Line);
747 } /* Handle_MaxNickLength */
751 Handle_GLOBAL( int Line, char *Var, char *Arg )
758 assert( Var != NULL );
759 assert( Arg != NULL );
761 if( strcasecmp( Var, "Name" ) == 0 ) {
763 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
764 if (len >= sizeof( Conf_ServerName ))
765 Config_Error_TooLong( Line, Var );
768 if( strcasecmp( Var, "Info" ) == 0 ) {
769 /* Info text of server */
770 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
771 if (len >= sizeof( Conf_ServerInfo ))
772 Config_Error_TooLong ( Line, Var );
775 if( strcasecmp( Var, "Password" ) == 0 ) {
776 /* Global server password */
777 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
778 if (len >= sizeof( Conf_ServerPwd ))
779 Config_Error_TooLong( Line, Var );
782 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
783 /* Administrative info #1 */
784 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
785 if (len >= sizeof( Conf_ServerAdmin1 ))
786 Config_Error_TooLong ( Line, Var );
789 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
790 /* Administrative info #2 */
791 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
792 if (len >= sizeof( Conf_ServerAdmin2 ))
793 Config_Error_TooLong ( Line, Var );
796 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
797 /* Administrative email contact */
798 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
799 if (len >= sizeof( Conf_ServerAdminMail ))
800 Config_Error_TooLong( Line, Var );
804 if( strcasecmp( Var, "Ports" ) == 0 ) {
805 ports_parse(&Conf_ListenPorts, Line, Arg);
808 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
809 /* "Message of the day" (MOTD) file */
810 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
811 if (len >= sizeof( Conf_MotdFile ))
812 Config_Error_TooLong( Line, Var );
815 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
816 /* "Message of the day" phrase (instead of file) */
817 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
818 if (len >= sizeof( Conf_MotdPhrase ))
819 Config_Error_TooLong( Line, Var );
822 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
823 /* directory for chroot() */
824 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
825 if (len >= sizeof( Conf_Chroot ))
826 Config_Error_TooLong( Line, Var );
829 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
830 /* name of pidfile */
831 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
832 if (len >= sizeof( Conf_PidFile ))
833 Config_Error_TooLong( Line, Var );
836 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
837 /* UID the daemon should switch to */
838 pwd = getpwnam( Arg );
839 if( pwd ) Conf_UID = pwd->pw_uid;
842 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
845 Conf_UID = (unsigned int)atoi( Arg );
849 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
850 /* GID the daemon should use */
851 grp = getgrnam( Arg );
852 if( grp ) Conf_GID = grp->gr_gid;
855 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
858 Conf_GID = (unsigned int)atoi( Arg );
862 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
864 Conf_PingTimeout = atoi( Arg );
865 if( Conf_PingTimeout < 5 ) {
866 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
867 NGIRCd_ConfFile, Line );
868 Conf_PingTimeout = 5;
872 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
874 Conf_PongTimeout = atoi( Arg );
875 if( Conf_PongTimeout < 5 ) {
876 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
877 NGIRCd_ConfFile, Line );
878 Conf_PongTimeout = 5;
882 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
883 /* Seconds between connection attempts to other servers */
884 Conf_ConnectRetry = atoi( Arg );
885 if( Conf_ConnectRetry < 5 ) {
886 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
887 NGIRCd_ConfFile, Line );
888 Conf_ConnectRetry = 5;
892 if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
893 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
894 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
897 if( strcasecmp( Var, "NoDNS" ) == 0 ) {
898 /* don't do reverse dns lookups when clients connect? */
899 Conf_NoDNS = Check_ArgIsTrue( Arg );
902 if (strcasecmp(Var, "NoIdent") == 0) {
903 /* don't do IDENT lookups when clients connect? */
904 Conf_NoIdent = Check_ArgIsTrue(Arg);
907 /* user has enabled ident lookups explicitly, but ... */
908 Config_Error(LOG_WARNING,
909 "%s: line %d: NoIdent=False, but ngircd was built without IDENT support",
910 NGIRCd_ConfFile, Line);
916 /* the default setting for all the WANT_IPV6 special options is 'true' */
917 if( strcasecmp( Var, "ConnectIPv6" ) == 0 ) {
918 /* connect to other hosts using ipv6, if they have an AAAA record? */
919 Conf_ConnectIPv6 = Check_ArgIsTrue( Arg );
922 if( strcasecmp( Var, "ConnectIPv4" ) == 0 ) {
923 /* connect to other hosts using ipv4.
924 * again, this can be used for ipv6-only setups */
925 Conf_ConnectIPv4 = Check_ArgIsTrue( Arg );
929 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
930 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
931 Conf_OperCanMode = Check_ArgIsTrue( Arg );
934 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
935 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
936 Conf_OperServerMode = Check_ArgIsTrue( Arg );
939 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
940 /* Maximum number of connections. 0 -> "no limit". */
942 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
945 Conf_MaxConnections = atol( Arg );
948 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
949 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
951 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
954 Conf_MaxConnectionsIP = atoi( Arg );
957 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
958 /* Maximum number of channels a user can join. 0 -> "no limit". */
960 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
963 Conf_MaxJoins = atoi( Arg );
966 if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
967 /* Maximum length of a nick name; must be same on all servers
968 * within the IRC network! */
969 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
973 if( strcasecmp( Var, "Listen" ) == 0 ) {
974 /* IP-Address to bind sockets */
975 if (Conf_ListenAddress) {
976 Config_Error(LOG_ERR, "Multiple Listen= options, ignoring: %s", Arg);
979 Conf_ListenAddress = strdup_warn(Arg);
981 * if allocation fails, we're in trouble:
982 * we cannot ignore the error -- otherwise ngircd
983 * would listen on all interfaces.
985 if (!Conf_ListenAddress) {
986 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
993 if( strcasecmp( Var, "SSLPorts" ) == 0 ) {
994 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
998 if( strcasecmp( Var, "SSLKeyFile" ) == 0 ) {
999 assert(Conf_SSLOptions.KeyFile == NULL );
1000 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1003 if( strcasecmp( Var, "SSLCertFile" ) == 0 ) {
1004 assert(Conf_SSLOptions.CertFile == NULL );
1005 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1009 if( strcasecmp( Var, "SSLKeyFilePassword" ) == 0 ) {
1010 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1011 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1012 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Could not copy %s: %s!",
1013 NGIRCd_ConfFile, Line, Var, strerror(errno));
1016 if( strcasecmp( Var, "SSLDHFile" ) == 0 ) {
1017 assert(Conf_SSLOptions.DHFile == NULL);
1018 Conf_SSLOptions.DHFile = strdup_warn( Arg );
1022 Config_Error(LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
1023 NGIRCd_ConfFile, Line, Var);
1024 } /* Handle_GLOBAL */
1028 Handle_OPERATOR( int Line, char *Var, char *Arg )
1030 unsigned int opercount;
1033 assert( Var != NULL );
1034 assert( Arg != NULL );
1035 assert( Conf_Oper_Count > 0 );
1037 if ( Conf_Oper_Count == 0 )
1040 opercount = Conf_Oper_Count - 1;
1042 if( strcasecmp( Var, "Name" ) == 0 ) {
1043 /* Name of IRC operator */
1044 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
1045 if (len >= sizeof( Conf_Oper[opercount].name ))
1046 Config_Error_TooLong( Line, Var );
1049 if( strcasecmp( Var, "Password" ) == 0 ) {
1050 /* Password of IRC operator */
1051 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
1052 if (len >= sizeof( Conf_Oper[opercount].pwd ))
1053 Config_Error_TooLong( Line, Var );
1056 if( strcasecmp( Var, "Mask" ) == 0 ) {
1057 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
1059 Conf_Oper[opercount].mask = strdup_warn( Arg );
1062 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
1063 NGIRCd_ConfFile, Line, Var );
1064 } /* Handle_OPERATOR */
1068 Handle_SERVER( int Line, char *Var, char *Arg )
1074 assert( Var != NULL );
1075 assert( Arg != NULL );
1077 /* Ignore server block if no space is left in server configuration structure */
1078 if( New_Server_Idx <= NONE ) return;
1080 if( strcasecmp( Var, "Host" ) == 0 ) {
1081 /* Hostname of the server */
1082 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1083 if (len >= sizeof( New_Server.host ))
1084 Config_Error_TooLong ( Line, Var );
1087 if( strcasecmp( Var, "Name" ) == 0 ) {
1088 /* Name of the server ("Nick"/"ID") */
1089 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1090 if (len >= sizeof( New_Server.name ))
1091 Config_Error_TooLong( Line, Var );
1094 if (strcasecmp(Var, "Bind") == 0) {
1095 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1098 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1099 NGIRCd_ConfFile, Line, Arg);
1102 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1103 /* Password of this server which is sent to the peer */
1105 Config_Error(LOG_ERR,
1106 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1107 NGIRCd_ConfFile, Line);
1109 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1110 if (len >= sizeof( New_Server.pwd_in ))
1111 Config_Error_TooLong( Line, Var );
1114 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1115 /* Passwort of the peer which must be received */
1116 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1117 if (len >= sizeof( New_Server.pwd_out ))
1118 Config_Error_TooLong( Line, Var );
1121 if( strcasecmp( Var, "Port" ) == 0 ) {
1122 /* Port to which this server should connect */
1124 if( port > 0 && port < 0xFFFF )
1125 New_Server.port = (UINT16)port;
1127 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
1128 NGIRCd_ConfFile, Line, port );
1132 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1133 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1137 if( strcasecmp( Var, "Group" ) == 0 ) {
1140 if( ! isdigit( (int)*Arg ))
1141 Config_Error_NaN( Line, Var );
1144 New_Server.group = atoi( Arg );
1147 if( strcasecmp( Var, "Passive" ) == 0 ) {
1148 if (Check_ArgIsTrue(Arg))
1149 New_Server.flags |= CONF_SFLAG_DISABLED;
1152 if (strcasecmp(Var, "ServiceMask") == 0) {
1153 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1154 sizeof(New_Server.svs_mask));
1155 if (len >= sizeof(New_Server.svs_mask))
1156 Config_Error_TooLong(Line, Var);
1160 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
1161 NGIRCd_ConfFile, Line, Var );
1162 } /* Handle_SERVER */
1166 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1168 size_t size = sizeof(new_chan->name);
1169 char *dest = new_chan->name;
1171 /* Channels names must begin with "&" or "#", if it is
1172 * missing, add a '#'. This is only here for user convenience.
1174 if (*name && *name != '#' && *name != '&') {
1179 return size > strlcpy(dest, name, size);
1184 Handle_CHANNEL(int Line, char *Var, char *Arg)
1188 struct Conf_Channel *chan;
1191 assert( Var != NULL );
1192 assert( Arg != NULL );
1193 assert(Conf_Channel_Count > 0);
1195 chancount = Conf_Channel_Count - 1;
1197 chan = array_alloc(&Conf_Channels, sizeof(*chan), chancount);
1199 Config_Error(LOG_ERR, "Could not allocate memory for predefined channel (%d:%s = %s)", Line, Var, Arg);
1202 if (strcasecmp(Var, "Name") == 0) {
1203 if (!Handle_Channelname(chan, Arg))
1204 Config_Error_TooLong(Line, Var);
1207 if (strcasecmp(Var, "Modes") == 0) {
1209 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1210 if (len >= sizeof(chan->modes))
1211 Config_Error_TooLong( Line, Var );
1214 if( strcasecmp( Var, "Topic" ) == 0 ) {
1216 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1217 if (len >= sizeof(chan->topic))
1218 Config_Error_TooLong( Line, Var );
1221 if( strcasecmp( Var, "Key" ) == 0 ) {
1222 /* Initial Channel Key (mode k) */
1223 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1224 if (len >= sizeof(chan->key))
1225 Config_Error_TooLong(Line, Var);
1228 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1229 /* maximum user limit, mode l */
1230 chan->maxusers = (unsigned long) atol(Arg);
1231 if (chan->maxusers == 0)
1232 Config_Error_NaN(Line, Var);
1236 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1237 NGIRCd_ConfFile, Line, Var );
1238 } /* Handle_CHANNEL */
1242 Validate_Config(bool Configtest, bool Rehash)
1244 /* Validate configuration settings. */
1247 int i, servers, servers_once;
1249 bool config_valid = true;
1252 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1253 ptr = Conf_ServerName;
1255 if (*ptr >= 'a' && *ptr <= 'z') continue;
1256 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1257 if (*ptr >= '0' && *ptr <= '9') continue;
1258 if (ptr > Conf_ServerName) {
1259 if (*ptr == '.' || *ptr == '-')
1262 Conf_ServerName[0] = '\0';
1266 if (!Conf_ServerName[0]) {
1267 /* No server name configured! */
1268 config_valid = false;
1269 Config_Error(LOG_ALERT,
1270 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1272 if (!Configtest && !Rehash) {
1273 Config_Error(LOG_ALERT,
1274 "%s exiting due to fatal errors!",
1280 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1281 /* No dot in server name! */
1282 config_valid = false;
1283 Config_Error(LOG_ALERT,
1284 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1287 Config_Error(LOG_ALERT,
1288 "%s exiting due to fatal errors!",
1295 if (!Conf_ServerAdminMail[0]) {
1296 /* No administrative contact configured! */
1297 config_valid = false;
1298 Config_Error(LOG_ALERT,
1299 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1302 Config_Error(LOG_ALERT,
1303 "%s exiting due to fatal errors!",
1310 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1311 && !Conf_ServerAdminMail[0]) {
1312 /* No administrative information configured! */
1313 Config_Error(LOG_WARNING,
1314 "No administrative information configured but required by RFC!");
1318 servers = servers_once = 0;
1319 for (i = 0; i < MAX_SERVERS; i++) {
1320 if (Conf_Server[i].name[0]) {
1322 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1327 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1328 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1331 return config_valid;
1332 } /* Validate_Config */
1336 Config_Error_TooLong ( const int Line, const char *Item )
1338 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1343 Config_Error_NaN( const int Line, const char *Item )
1345 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1346 NGIRCd_ConfFile, Line, Item );
1351 static void Config_Error( const int Level, const char *Format, ... )
1353 static void Config_Error( Level, Format, va_alist )
1359 /* Error! Write to console and/or logfile. */
1361 char msg[MAX_LOG_MSG_LEN];
1364 assert( Format != NULL );
1367 va_start( ap, Format );
1371 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1374 /* During "normal operations" the log functions of the daemon should
1375 * be used, but during testing of the configuration file, all messages
1376 * should go directly to the console: */
1377 if (Use_Log) Log( Level, "%s", msg );
1379 } /* Config_Error */
1383 Init_Server_Struct( CONF_SERVER *Server )
1385 /* Initialize server configuration structur to default values */
1387 assert( Server != NULL );
1389 memset( Server, 0, sizeof (CONF_SERVER) );
1391 Server->group = NONE;
1392 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1394 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1396 Resolve_Init(&Server->res_stat);
1397 Server->conn_id = NONE;
1398 memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
1399 } /* Init_Server_Struct */