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);
99 ssl_print_configvar(const char *name, const char *file)
104 printf(" %s =\n", name);
108 fp = fopen(file, "r");
112 fprintf(stderr, "ERROR: %s \"%s\": %s\n",
113 name, file, strerror(errno));
115 printf(" %s = %s\n", name, file);
124 ret = ssl_print_configvar("SSLKeyFile", Conf_SSLOptions.KeyFile);
126 if (!ssl_print_configvar("SSLCertFile", Conf_SSLOptions.CertFile))
129 if (!ssl_print_configvar("SSLDHFile", Conf_SSLOptions.DHFile))
132 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
133 puts(" SSLKeyFilePassword = <secret>");
135 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
142 strdup_warn(const char *str)
144 char *ptr = strdup(str);
146 Config_Error(LOG_ERR, "Could not allocate mem for string: %s", str);
156 len = array_length(a, sizeof(UINT16));
158 ports = (UINT16*) array_start(a);
159 printf("%u", (unsigned int) *ports);
162 printf(", %u", (unsigned int) *ports);
170 ports_parse(array *a, int Line, char *Arg)
178 /* Ports on that the server should listen. More port numbers
179 * must be separated by "," */
180 ptr = strtok( Arg, "," );
184 if (port > 0 && port < 0xFFFF) {
185 port16 = (UINT16) port;
186 if (!array_catb(a, (char*)&port16, sizeof port16))
187 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
188 NGIRCd_ConfFile, Line, port, strerror(errno));
190 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
191 NGIRCd_ConfFile, Line, port );
194 ptr = strtok( NULL, "," );
203 Validate_Config(false, false);
210 if (!Read_Config(false))
212 Validate_Config(false, true);
214 /* Update CLIENT structure of local server */
215 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
217 } /* Config_Rehash */
221 yesno_to_str(int boolean_value)
232 /* Read configuration, validate and output it. */
238 size_t predef_channel_count;
239 struct Conf_Channel *predef_chan;
243 if (! Read_Config(true))
246 config_valid = Validate_Config(true, false);
248 /* If stdin and stdout ("you can read our nice message and we can
249 * read in your keypress") are valid tty's, wait for a key: */
250 if( isatty( fileno( stdin )) && isatty( fileno( stdout ))) {
251 puts( "OK, press enter to see a dump of your service configuration ..." );
254 puts( "Ok, dump of your server configuration follows:\n" );
258 printf( " Name = %s\n", Conf_ServerName );
259 printf( " Info = %s\n", Conf_ServerInfo );
260 printf( " Password = %s\n", Conf_ServerPwd );
261 printf( " AdminInfo1 = %s\n", Conf_ServerAdmin1 );
262 printf( " AdminInfo2 = %s\n", Conf_ServerAdmin2 );
263 printf( " AdminEMail = %s\n", Conf_ServerAdminMail );
264 printf( " MotdFile = %s\n", Conf_MotdFile );
265 printf( " MotdPhrase = %s\n", Conf_MotdPhrase );
266 printf( " ChrootDir = %s\n", Conf_Chroot );
267 printf( " PidFile = %s\n", Conf_PidFile);
268 printf(" Listen = %s\n", Conf_ListenAddress);
269 fputs(" Ports = ", stdout);
270 ports_puts(&Conf_ListenPorts);
272 fputs(" SSLPorts = ", stdout);
273 ports_puts(&Conf_SSLOptions.ListenPorts);
275 config_valid = false;
278 pwd = getpwuid(Conf_UID);
280 printf(" ServerUID = %s\n", pwd->pw_name);
282 printf(" ServerUID = %ld\n", (long)Conf_UID);
283 grp = getgrgid(Conf_GID);
285 printf(" ServerGID = %s\n", grp->gr_name);
287 printf(" ServerGID = %ld\n", (long)Conf_GID);
288 printf(" PingTimeout = %d\n", Conf_PingTimeout);
289 printf(" PongTimeout = %d\n", Conf_PongTimeout);
290 printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
291 printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
292 printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
293 printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
294 printf(" PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
295 printf(" NoDNS = %s\n", yesno_to_str(Conf_NoDNS));
296 printf(" NoIdent = %s\n", yesno_to_str(Conf_NoIdent));
299 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
300 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
302 printf(" MaxConnections = %ld\n", Conf_MaxConnections);
303 printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
304 printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
305 printf(" MaxNickLength = %u\n\n", Conf_MaxNickLength - 1);
307 for( i = 0; i < Conf_Oper_Count; i++ ) {
308 if( ! Conf_Oper[i].name[0] ) continue;
310 /* Valid "Operator" section */
311 puts( "[OPERATOR]" );
312 printf( " Name = %s\n", Conf_Oper[i].name );
313 printf( " Password = %s\n", Conf_Oper[i].pwd );
314 if ( Conf_Oper[i].mask ) printf( " Mask = %s\n", Conf_Oper[i].mask );
318 for( i = 0; i < MAX_SERVERS; i++ ) {
319 if( ! Conf_Server[i].name[0] ) continue;
321 /* Valid "Server" section */
323 printf( " Name = %s\n", Conf_Server[i].name );
324 printf( " Host = %s\n", Conf_Server[i].host );
325 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
327 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
329 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
330 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
331 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
332 printf( " Group = %d\n", Conf_Server[i].group );
333 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
336 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
337 predef_chan = array_start(&Conf_Channels);
339 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
340 if (!predef_chan->name[0])
343 /* Valid "Channel" section */
345 printf(" Name = %s\n", predef_chan->name);
346 printf(" Modes = %s\n", predef_chan->modes);
347 printf(" Key = %s\n", predef_chan->key);
348 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
349 printf(" Topic = %s\n", predef_chan->topic);
350 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
353 return (config_valid ? 0 : 1);
358 Conf_UnsetServer( CONN_ID Idx )
360 /* Set next time for next connection attempt, if this is a server
361 * link that is (still) configured here. If the server is set as
362 * "once", delete it from our configuration.
363 * Non-Server-Connections will be silently ignored. */
368 /* Check all our configured servers */
369 for( i = 0; i < MAX_SERVERS; i++ ) {
370 if( Conf_Server[i].conn_id != Idx ) continue;
372 /* Gotcha! Mark server configuration as "unused": */
373 Conf_Server[i].conn_id = NONE;
375 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
376 /* Delete configuration here */
377 Init_Server_Struct( &Conf_Server[i] );
379 /* Set time for next connect attempt */
381 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
382 /* The connection has been "long", so we don't
383 * require the next attempt to be delayed. */
384 Conf_Server[i].lasttry =
385 t - Conf_ConnectRetry + RECONNECT_DELAY;
387 Conf_Server[i].lasttry = t;
390 } /* Conf_UnsetServer */
394 Conf_SetServer( int ConfServer, CONN_ID Idx )
396 /* Set connection for specified configured server */
398 assert( ConfServer > NONE );
399 assert( Idx > NONE );
401 Conf_Server[ConfServer].conn_id = Idx;
402 } /* Conf_SetServer */
406 Conf_GetServer( CONN_ID Idx )
408 /* Get index of server in configuration structure */
412 assert( Idx > NONE );
414 for( i = 0; i < MAX_SERVERS; i++ ) {
415 if( Conf_Server[i].conn_id == Idx ) return i;
418 } /* Conf_GetServer */
422 Conf_EnableServer( const char *Name, UINT16 Port )
424 /* Enable specified server and adjust port */
428 assert( Name != NULL );
430 for( i = 0; i < MAX_SERVERS; i++ ) {
431 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
432 /* Gotcha! Set port and enable server: */
433 Conf_Server[i].port = Port;
434 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
435 return (Conf_Server[i].port && Conf_Server[i].host[0]);
439 } /* Conf_EnableServer */
443 Conf_EnablePassiveServer(const char *Name)
445 /* Enable specified server */
448 assert( Name != NULL );
449 for (i = 0; i < MAX_SERVERS; i++) {
450 if ((strcasecmp( Conf_Server[i].name, Name ) == 0) && (Conf_Server[i].port > 0)) {
451 /* BINGO! Enable server */
452 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
457 } /* Conf_EnablePassiveServer */
461 Conf_DisableServer( const char *Name )
463 /* Enable specified server and adjust port */
467 assert( Name != NULL );
469 for( i = 0; i < MAX_SERVERS; i++ ) {
470 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
471 /* Gotcha! Disable and disconnect server: */
472 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
473 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
478 } /* Conf_DisableServer */
482 Conf_AddServer( const char *Name, UINT16 Port, const char *Host, const char *MyPwd, const char *PeerPwd )
484 /* Add new server to configuration */
488 assert( Name != NULL );
489 assert( Host != NULL );
490 assert( MyPwd != NULL );
491 assert( PeerPwd != NULL );
493 /* Search unused item in server configuration structure */
494 for( i = 0; i < MAX_SERVERS; i++ ) {
495 /* Is this item used? */
496 if( ! Conf_Server[i].name[0] ) break;
498 if( i >= MAX_SERVERS ) return false;
500 Init_Server_Struct( &Conf_Server[i] );
501 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
502 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
503 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
504 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
505 Conf_Server[i].port = Port;
506 Conf_Server[i].flags = CONF_SFLAG_ONCE;
509 } /* Conf_AddServer */
513 * Check if the given nick name is an service
516 Conf_IsService(int ConfServer, const char *Nick)
518 return MatchCaseInsensitive(Conf_Server[ConfServer].svs_mask, Nick);
519 } /* Conf_IsService */
523 * Initialize configuration settings with their default values.
526 Set_Defaults(bool InitServers)
530 strcpy(Conf_ServerName, "");
531 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
532 PACKAGE_NAME, PACKAGE_VERSION);
533 strcpy(Conf_ServerPwd, "");
535 strcpy(Conf_ServerAdmin1, "");
536 strcpy(Conf_ServerAdmin2, "");
537 strcpy(Conf_ServerAdminMail, "");
539 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
540 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
541 strlcpy(Conf_MotdPhrase, MOTD_PHRASE, sizeof(Conf_MotdPhrase));
543 Conf_UID = Conf_GID = 0;
544 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
545 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
547 free(Conf_ListenAddress);
548 Conf_ListenAddress = NULL;
550 Conf_PingTimeout = 120;
551 Conf_PongTimeout = 20;
552 Conf_ConnectRetry = 60;
554 Conf_NoIdent = false;
557 Conf_Channel_Count = 0;
559 Conf_OperCanMode = false;
560 Conf_OperServerMode = false;
561 Conf_AllowRemoteOper = false;
562 Conf_PredefChannelsOnly = false;
564 Conf_ConnectIPv4 = true;
565 Conf_ConnectIPv6 = true;
567 Conf_MaxConnections = 0;
568 Conf_MaxConnectionsIP = 5;
570 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
572 /* Initialize server configuration structures */
574 for (i = 0; i < MAX_SERVERS;
575 Init_Server_Struct(&Conf_Server[i++]));
583 size_t cnt = array_bytes(&Conf_ListenPorts);
585 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
591 Read_Config( bool ngircd_starting )
593 /* Read configuration file. */
595 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
596 const UINT16 defaultport = 6667;
600 /* Open configuration file */
601 fd = fopen( NGIRCd_ConfFile, "r" );
603 /* No configuration file found! */
604 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
605 NGIRCd_ConfFile, strerror( errno ));
606 if (!ngircd_starting)
608 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
612 Set_Defaults( ngircd_starting );
614 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
616 /* Clean up server configuration structure: mark all already
617 * configured servers as "once" so that they are deleted
618 * after the next disconnect and delete all unused servers.
619 * And delete all servers which are "duplicates" of servers
620 * that are already marked as "once" (such servers have been
621 * created by the last rehash but are now useless). */
622 for( i = 0; i < MAX_SERVERS; i++ ) {
623 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
625 /* This structure is in use ... */
626 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
627 /* Check for duplicates */
628 for( n = 0; n < MAX_SERVERS; n++ ) {
629 if( n == i ) continue;
631 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
632 Init_Server_Struct( &Conf_Server[n] );
634 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
640 /* Mark server as "once" */
641 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
642 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
647 /* Initialize variables */
649 strcpy( section, "" );
650 Init_Server_Struct( &New_Server );
651 New_Server_Idx = NONE;
655 /* Read configuration file */
657 if( ! fgets( str, LINE_LEN, fd )) break;
661 /* Skip comments and empty lines */
662 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
664 /* Is this the beginning of a new section? */
665 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
666 strlcpy( section, str, sizeof( section ));
667 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
670 if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
671 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
672 Config_Error( LOG_ERR, "Too many operators configured.");
674 /* Initialize new operator structure */
675 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
676 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
677 if (Conf_Oper[Conf_Oper_Count].mask) {
678 free(Conf_Oper[Conf_Oper_Count].mask );
679 Conf_Oper[Conf_Oper_Count].mask = NULL;
685 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
686 /* Check if there is already a server to add */
687 if( New_Server.name[0] ) {
688 /* Copy data to "real" server structure */
689 assert( New_Server_Idx > NONE );
690 Conf_Server[New_Server_Idx] = New_Server;
693 /* Re-init structure for new server */
694 Init_Server_Struct( &New_Server );
696 /* Search unused item in server configuration structure */
697 for( i = 0; i < MAX_SERVERS; i++ ) {
698 /* Is this item used? */
699 if( ! Conf_Server[i].name[0] ) break;
701 if( i >= MAX_SERVERS ) {
702 /* Oops, no free item found! */
703 Config_Error( LOG_ERR, "Too many servers configured." );
704 New_Server_Idx = NONE;
706 else New_Server_Idx = i;
709 if (strcasecmp(section, "[CHANNEL]") == 0) {
710 Conf_Channel_Count++;
714 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
717 if( section[0] == 0x1 ) continue;
719 /* Split line into variable name and parameters */
720 ptr = strchr( str, '=' );
722 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
726 var = str; ngt_TrimStr( var );
727 arg = ptr + 1; ngt_TrimStr( arg );
729 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
730 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
731 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
732 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
733 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
736 /* Close configuration file */
739 /* Check if there is still a server to add */
740 if( New_Server.name[0] ) {
741 /* Copy data to "real" server structure */
742 assert( New_Server_Idx > NONE );
743 Conf_Server[New_Server_Idx] = New_Server;
746 /* not a single listening port? Add default. */
747 if (no_listenports() &&
748 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
750 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
751 (unsigned int) defaultport, strerror(errno));
756 if (!Conf_ListenAddress)
757 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
759 if (!Conf_ListenAddress) {
760 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
768 Check_ArgIsTrue( const char *Arg )
770 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
771 if( strcasecmp( Arg, "true" ) == 0 ) return true;
772 if( atoi( Arg ) != 0 ) return true;
775 } /* Check_ArgIsTrue */
778 static unsigned int Handle_MaxNickLength(int Line, const char *Arg)
782 new = (unsigned) atoi(Arg) + 1;
783 if (new > CLIENT_NICK_LEN) {
784 Config_Error(LOG_WARNING,
785 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
786 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
787 return CLIENT_NICK_LEN;
790 Config_Error(LOG_WARNING,
791 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
792 NGIRCd_ConfFile, Line);
796 } /* Handle_MaxNickLength */
800 Handle_GLOBAL( int Line, char *Var, char *Arg )
807 assert( Var != NULL );
808 assert( Arg != NULL );
810 if( strcasecmp( Var, "Name" ) == 0 ) {
812 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
813 if (len >= sizeof( Conf_ServerName ))
814 Config_Error_TooLong( Line, Var );
817 if( strcasecmp( Var, "Info" ) == 0 ) {
818 /* Info text of server */
819 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
820 if (len >= sizeof( Conf_ServerInfo ))
821 Config_Error_TooLong ( Line, Var );
824 if( strcasecmp( Var, "Password" ) == 0 ) {
825 /* Global server password */
826 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
827 if (len >= sizeof( Conf_ServerPwd ))
828 Config_Error_TooLong( Line, Var );
831 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
832 /* Administrative info #1 */
833 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
834 if (len >= sizeof( Conf_ServerAdmin1 ))
835 Config_Error_TooLong ( Line, Var );
838 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
839 /* Administrative info #2 */
840 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
841 if (len >= sizeof( Conf_ServerAdmin2 ))
842 Config_Error_TooLong ( Line, Var );
845 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
846 /* Administrative email contact */
847 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
848 if (len >= sizeof( Conf_ServerAdminMail ))
849 Config_Error_TooLong( Line, Var );
853 if( strcasecmp( Var, "Ports" ) == 0 ) {
854 ports_parse(&Conf_ListenPorts, Line, Arg);
857 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
858 /* "Message of the day" (MOTD) file */
859 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
860 if (len >= sizeof( Conf_MotdFile ))
861 Config_Error_TooLong( Line, Var );
864 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
865 /* "Message of the day" phrase (instead of file) */
866 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
867 if (len >= sizeof( Conf_MotdPhrase ))
868 Config_Error_TooLong( Line, Var );
871 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
872 /* directory for chroot() */
873 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
874 if (len >= sizeof( Conf_Chroot ))
875 Config_Error_TooLong( Line, Var );
878 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
879 /* name of pidfile */
880 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
881 if (len >= sizeof( Conf_PidFile ))
882 Config_Error_TooLong( Line, Var );
885 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
886 /* UID the daemon should switch to */
887 pwd = getpwnam( Arg );
888 if( pwd ) Conf_UID = pwd->pw_uid;
891 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
894 Conf_UID = (unsigned int)atoi( Arg );
898 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
899 /* GID the daemon should use */
900 grp = getgrnam( Arg );
901 if( grp ) Conf_GID = grp->gr_gid;
904 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
907 Conf_GID = (unsigned int)atoi( Arg );
911 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
913 Conf_PingTimeout = atoi( Arg );
914 if( Conf_PingTimeout < 5 ) {
915 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
916 NGIRCd_ConfFile, Line );
917 Conf_PingTimeout = 5;
921 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
923 Conf_PongTimeout = atoi( Arg );
924 if( Conf_PongTimeout < 5 ) {
925 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
926 NGIRCd_ConfFile, Line );
927 Conf_PongTimeout = 5;
931 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
932 /* Seconds between connection attempts to other servers */
933 Conf_ConnectRetry = atoi( Arg );
934 if( Conf_ConnectRetry < 5 ) {
935 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
936 NGIRCd_ConfFile, Line );
937 Conf_ConnectRetry = 5;
941 if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
942 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
943 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
946 if( strcasecmp( Var, "NoDNS" ) == 0 ) {
947 /* don't do reverse dns lookups when clients connect? */
948 Conf_NoDNS = Check_ArgIsTrue( Arg );
951 if (strcasecmp(Var, "NoIdent") == 0) {
952 /* don't do IDENT lookups when clients connect? */
953 Conf_NoIdent = Check_ArgIsTrue(Arg);
956 /* user has enabled ident lookups explicitly, but ... */
957 Config_Error(LOG_WARNING,
958 "%s: line %d: NoIdent=False, but ngircd was built without IDENT support",
959 NGIRCd_ConfFile, Line);
965 /* the default setting for all the WANT_IPV6 special options is 'true' */
966 if( strcasecmp( Var, "ConnectIPv6" ) == 0 ) {
967 /* connect to other hosts using ipv6, if they have an AAAA record? */
968 Conf_ConnectIPv6 = Check_ArgIsTrue( Arg );
971 if( strcasecmp( Var, "ConnectIPv4" ) == 0 ) {
972 /* connect to other hosts using ipv4.
973 * again, this can be used for ipv6-only setups */
974 Conf_ConnectIPv4 = Check_ArgIsTrue( Arg );
978 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
979 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
980 Conf_OperCanMode = Check_ArgIsTrue( Arg );
983 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
984 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
985 Conf_OperServerMode = Check_ArgIsTrue( Arg );
988 if(strcasecmp(Var, "AllowRemoteOper") == 0) {
989 /* Are remote IRC operators allowed to control this server? */
990 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
993 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
994 /* Maximum number of connections. 0 -> "no limit". */
996 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
999 Conf_MaxConnections = atol( Arg );
1002 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
1003 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
1005 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
1008 Conf_MaxConnectionsIP = atoi( Arg );
1011 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
1012 /* Maximum number of channels a user can join. 0 -> "no limit". */
1014 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
1017 Conf_MaxJoins = atoi( Arg );
1020 if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
1021 /* Maximum length of a nick name; must be same on all servers
1022 * within the IRC network! */
1023 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1027 if( strcasecmp( Var, "Listen" ) == 0 ) {
1028 /* IP-Address to bind sockets */
1029 if (Conf_ListenAddress) {
1030 Config_Error(LOG_ERR, "Multiple Listen= options, ignoring: %s", Arg);
1033 Conf_ListenAddress = strdup_warn(Arg);
1035 * if allocation fails, we're in trouble:
1036 * we cannot ignore the error -- otherwise ngircd
1037 * would listen on all interfaces.
1039 if (!Conf_ListenAddress) {
1040 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
1047 if( strcasecmp( Var, "SSLPorts" ) == 0 ) {
1048 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1052 if( strcasecmp( Var, "SSLKeyFile" ) == 0 ) {
1053 assert(Conf_SSLOptions.KeyFile == NULL );
1054 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1057 if( strcasecmp( Var, "SSLCertFile" ) == 0 ) {
1058 assert(Conf_SSLOptions.CertFile == NULL );
1059 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1063 if( strcasecmp( Var, "SSLKeyFilePassword" ) == 0 ) {
1064 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1065 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1066 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Could not copy %s: %s!",
1067 NGIRCd_ConfFile, Line, Var, strerror(errno));
1070 if( strcasecmp( Var, "SSLDHFile" ) == 0 ) {
1071 assert(Conf_SSLOptions.DHFile == NULL);
1072 Conf_SSLOptions.DHFile = strdup_warn( Arg );
1076 Config_Error(LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
1077 NGIRCd_ConfFile, Line, Var);
1078 } /* Handle_GLOBAL */
1082 Handle_OPERATOR( int Line, char *Var, char *Arg )
1084 unsigned int opercount;
1087 assert( Var != NULL );
1088 assert( Arg != NULL );
1089 assert( Conf_Oper_Count > 0 );
1091 if ( Conf_Oper_Count == 0 )
1094 opercount = Conf_Oper_Count - 1;
1096 if( strcasecmp( Var, "Name" ) == 0 ) {
1097 /* Name of IRC operator */
1098 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
1099 if (len >= sizeof( Conf_Oper[opercount].name ))
1100 Config_Error_TooLong( Line, Var );
1103 if( strcasecmp( Var, "Password" ) == 0 ) {
1104 /* Password of IRC operator */
1105 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
1106 if (len >= sizeof( Conf_Oper[opercount].pwd ))
1107 Config_Error_TooLong( Line, Var );
1110 if( strcasecmp( Var, "Mask" ) == 0 ) {
1111 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
1113 Conf_Oper[opercount].mask = strdup_warn( Arg );
1116 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
1117 NGIRCd_ConfFile, Line, Var );
1118 } /* Handle_OPERATOR */
1122 Handle_SERVER( int Line, char *Var, char *Arg )
1128 assert( Var != NULL );
1129 assert( Arg != NULL );
1131 /* Ignore server block if no space is left in server configuration structure */
1132 if( New_Server_Idx <= NONE ) return;
1134 if( strcasecmp( Var, "Host" ) == 0 ) {
1135 /* Hostname of the server */
1136 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1137 if (len >= sizeof( New_Server.host ))
1138 Config_Error_TooLong ( Line, Var );
1141 if( strcasecmp( Var, "Name" ) == 0 ) {
1142 /* Name of the server ("Nick"/"ID") */
1143 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1144 if (len >= sizeof( New_Server.name ))
1145 Config_Error_TooLong( Line, Var );
1148 if (strcasecmp(Var, "Bind") == 0) {
1149 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1152 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1153 NGIRCd_ConfFile, Line, Arg);
1156 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1157 /* Password of this server which is sent to the peer */
1159 Config_Error(LOG_ERR,
1160 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1161 NGIRCd_ConfFile, Line);
1163 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1164 if (len >= sizeof( New_Server.pwd_in ))
1165 Config_Error_TooLong( Line, Var );
1168 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1169 /* Passwort of the peer which must be received */
1170 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1171 if (len >= sizeof( New_Server.pwd_out ))
1172 Config_Error_TooLong( Line, Var );
1175 if( strcasecmp( Var, "Port" ) == 0 ) {
1176 /* Port to which this server should connect */
1178 if( port > 0 && port < 0xFFFF )
1179 New_Server.port = (UINT16)port;
1181 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
1182 NGIRCd_ConfFile, Line, port );
1186 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1187 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1191 if( strcasecmp( Var, "Group" ) == 0 ) {
1194 if( ! isdigit( (int)*Arg ))
1195 Config_Error_NaN( Line, Var );
1198 New_Server.group = atoi( Arg );
1201 if( strcasecmp( Var, "Passive" ) == 0 ) {
1202 if (Check_ArgIsTrue(Arg))
1203 New_Server.flags |= CONF_SFLAG_DISABLED;
1206 if (strcasecmp(Var, "ServiceMask") == 0) {
1207 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1208 sizeof(New_Server.svs_mask));
1209 if (len >= sizeof(New_Server.svs_mask))
1210 Config_Error_TooLong(Line, Var);
1214 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
1215 NGIRCd_ConfFile, Line, Var );
1216 } /* Handle_SERVER */
1220 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1222 size_t size = sizeof(new_chan->name);
1223 char *dest = new_chan->name;
1225 if (!Channel_IsValidName(name)) {
1227 * maybe user forgot to add a '#'.
1228 * This is only here for user convenience.
1234 return size > strlcpy(dest, name, size);
1239 Handle_CHANNEL(int Line, char *Var, char *Arg)
1243 struct Conf_Channel *chan;
1246 assert( Var != NULL );
1247 assert( Arg != NULL );
1248 assert(Conf_Channel_Count > 0);
1250 chancount = Conf_Channel_Count - 1;
1252 chan = array_alloc(&Conf_Channels, sizeof(*chan), chancount);
1254 Config_Error(LOG_ERR, "Could not allocate memory for predefined channel (%d:%s = %s)", Line, Var, Arg);
1257 if (strcasecmp(Var, "Name") == 0) {
1258 if (!Handle_Channelname(chan, Arg))
1259 Config_Error_TooLong(Line, Var);
1262 if (strcasecmp(Var, "Modes") == 0) {
1264 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1265 if (len >= sizeof(chan->modes))
1266 Config_Error_TooLong( Line, Var );
1269 if( strcasecmp( Var, "Topic" ) == 0 ) {
1271 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1272 if (len >= sizeof(chan->topic))
1273 Config_Error_TooLong( Line, Var );
1276 if( strcasecmp( Var, "Key" ) == 0 ) {
1277 /* Initial Channel Key (mode k) */
1278 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1279 if (len >= sizeof(chan->key))
1280 Config_Error_TooLong(Line, Var);
1283 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1284 /* maximum user limit, mode l */
1285 chan->maxusers = (unsigned long) atol(Arg);
1286 if (chan->maxusers == 0)
1287 Config_Error_NaN(Line, Var);
1290 if (strcasecmp(Var, "KeyFile") == 0) {
1292 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1293 if (len >= sizeof(chan->keyfile))
1294 Config_Error_TooLong(Line, Var);
1298 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1299 NGIRCd_ConfFile, Line, Var );
1300 } /* Handle_CHANNEL */
1304 Validate_Config(bool Configtest, bool Rehash)
1306 /* Validate configuration settings. */
1309 int i, servers, servers_once;
1311 bool config_valid = true;
1314 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1315 ptr = Conf_ServerName;
1317 if (*ptr >= 'a' && *ptr <= 'z') continue;
1318 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1319 if (*ptr >= '0' && *ptr <= '9') continue;
1320 if (ptr > Conf_ServerName) {
1321 if (*ptr == '.' || *ptr == '-')
1324 Conf_ServerName[0] = '\0';
1328 if (!Conf_ServerName[0]) {
1329 /* No server name configured! */
1330 config_valid = false;
1331 Config_Error(LOG_ALERT,
1332 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1334 if (!Configtest && !Rehash) {
1335 Config_Error(LOG_ALERT,
1336 "%s exiting due to fatal errors!",
1342 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1343 /* No dot in server name! */
1344 config_valid = false;
1345 Config_Error(LOG_ALERT,
1346 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1349 Config_Error(LOG_ALERT,
1350 "%s exiting due to fatal errors!",
1357 if (!Conf_ServerAdminMail[0]) {
1358 /* No administrative contact configured! */
1359 config_valid = false;
1360 Config_Error(LOG_ALERT,
1361 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1364 Config_Error(LOG_ALERT,
1365 "%s exiting due to fatal errors!",
1372 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1373 && !Conf_ServerAdminMail[0]) {
1374 /* No administrative information configured! */
1375 Config_Error(LOG_WARNING,
1376 "No administrative information configured but required by RFC!");
1380 servers = servers_once = 0;
1381 for (i = 0; i < MAX_SERVERS; i++) {
1382 if (Conf_Server[i].name[0]) {
1384 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1389 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1390 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1393 return config_valid;
1394 } /* Validate_Config */
1398 Config_Error_TooLong ( const int Line, const char *Item )
1400 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1405 Config_Error_NaN( const int Line, const char *Item )
1407 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1408 NGIRCd_ConfFile, Line, Item );
1413 static void Config_Error( const int Level, const char *Format, ... )
1415 static void Config_Error( Level, Format, va_alist )
1421 /* Error! Write to console and/or logfile. */
1423 char msg[MAX_LOG_MSG_LEN];
1426 assert( Format != NULL );
1429 va_start( ap, Format );
1433 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1436 /* During "normal operations" the log functions of the daemon should
1437 * be used, but during testing of the configuration file, all messages
1438 * should go directly to the console: */
1439 if (Use_Log) Log( Level, "%s", msg );
1441 } /* Config_Error */
1445 Init_Server_Struct( CONF_SERVER *Server )
1447 /* Initialize server configuration structur to default values */
1449 assert( Server != NULL );
1451 memset( Server, 0, sizeof (CONF_SERVER) );
1453 Server->group = NONE;
1454 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1456 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1458 Resolve_Init(&Server->res_stat);
1459 Server->conn_id = NONE;
1460 memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
1461 } /* Init_Server_Struct */