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 can_open(const char *name, const char *file)
101 FILE *fp = fopen(file, "r");
105 fprintf(stderr, "ERROR: %s \"%s\": %s\n",
106 name, file, strerror(errno));
115 if (Conf_SSLOptions.KeyFile) {
116 printf( " SSLKeyFile = %s\n", Conf_SSLOptions.KeyFile);
117 ret = can_open("SSLKeyFile", Conf_SSLOptions.KeyFile);
119 if (Conf_SSLOptions.CertFile) {
120 printf( " SSLCertFile = %s\n", Conf_SSLOptions.CertFile);
121 if (!can_open("SSLCertFile", Conf_SSLOptions.CertFile))
124 if (Conf_SSLOptions.DHFile) {
125 printf( " SSLDHFile = %s\n", Conf_SSLOptions.DHFile);
126 if (!can_open("SSLDHFile", Conf_SSLOptions.DHFile))
129 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
130 puts(" SSLKeyFilePassword = <secret>" );
131 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
138 strdup_warn(const char *str)
140 char *ptr = strdup(str);
142 Config_Error(LOG_ERR, "Could not allocate mem for string: %s", str);
152 len = array_length(a, sizeof(UINT16));
154 ports = (UINT16*) array_start(a);
155 printf("%u", (unsigned int) *ports);
158 printf(", %u", (unsigned int) *ports);
166 ports_parse(array *a, int Line, char *Arg)
174 /* Ports on that the server should listen. More port numbers
175 * must be separated by "," */
176 ptr = strtok( Arg, "," );
180 if (port > 0 && port < 0xFFFF) {
181 port16 = (UINT16) port;
182 if (!array_catb(a, (char*)&port16, sizeof port16))
183 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
184 NGIRCd_ConfFile, Line, port, strerror(errno));
186 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
187 NGIRCd_ConfFile, Line, port );
190 ptr = strtok( NULL, "," );
199 Validate_Config(false, false);
206 if (!Read_Config(false))
208 Validate_Config(false, true);
210 /* Update CLIENT structure of local server */
211 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
213 } /* Config_Rehash */
217 yesno_to_str(int boolean_value)
228 /* Read configuration, validate and output it. */
234 size_t predef_channel_count;
235 struct Conf_Channel *predef_chan;
239 if (! Read_Config(true))
242 config_valid = Validate_Config(true, false);
244 /* If stdin and stdout ("you can read our nice message and we can
245 * read in your keypress") are valid tty's, wait for a key: */
246 if( isatty( fileno( stdin )) && isatty( fileno( stdout ))) {
247 puts( "OK, press enter to see a dump of your service configuration ..." );
250 puts( "Ok, dump of your server configuration follows:\n" );
254 printf( " Name = %s\n", Conf_ServerName );
255 printf( " Info = %s\n", Conf_ServerInfo );
256 printf( " Password = %s\n", Conf_ServerPwd );
257 printf( " AdminInfo1 = %s\n", Conf_ServerAdmin1 );
258 printf( " AdminInfo2 = %s\n", Conf_ServerAdmin2 );
259 printf( " AdminEMail = %s\n", Conf_ServerAdminMail );
260 printf( " MotdFile = %s\n", Conf_MotdFile );
261 printf( " MotdPhrase = %s\n", Conf_MotdPhrase );
262 printf( " ChrootDir = %s\n", Conf_Chroot );
263 printf( " PidFile = %s\n", Conf_PidFile);
264 printf(" Listen = %s\n", Conf_ListenAddress);
265 fputs(" Ports = ", stdout);
266 ports_puts(&Conf_ListenPorts);
268 fputs(" SSLPorts = ", stdout);
269 ports_puts(&Conf_SSLOptions.ListenPorts);
271 config_valid = false;
274 pwd = getpwuid(Conf_UID);
276 printf(" ServerUID = %s\n", pwd->pw_name);
278 printf(" ServerUID = %ld\n", (long)Conf_UID);
279 grp = getgrgid(Conf_GID);
281 printf(" ServerGID = %s\n", grp->gr_name);
283 printf(" ServerGID = %ld\n", (long)Conf_GID);
284 printf(" PingTimeout = %d\n", Conf_PingTimeout);
285 printf(" PongTimeout = %d\n", Conf_PongTimeout);
286 printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
287 printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
288 printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
289 printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
290 printf(" PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
291 printf(" NoDNS = %s\n", yesno_to_str(Conf_NoDNS));
292 printf(" NoIdent = %s\n", yesno_to_str(Conf_NoIdent));
295 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
296 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
298 printf(" MaxConnections = %ld\n", Conf_MaxConnections);
299 printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
300 printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
301 printf(" MaxNickLength = %u\n\n", Conf_MaxNickLength - 1);
303 for( i = 0; i < Conf_Oper_Count; i++ ) {
304 if( ! Conf_Oper[i].name[0] ) continue;
306 /* Valid "Operator" section */
307 puts( "[OPERATOR]" );
308 printf( " Name = %s\n", Conf_Oper[i].name );
309 printf( " Password = %s\n", Conf_Oper[i].pwd );
310 if ( Conf_Oper[i].mask ) printf( " Mask = %s\n", Conf_Oper[i].mask );
314 for( i = 0; i < MAX_SERVERS; i++ ) {
315 if( ! Conf_Server[i].name[0] ) continue;
317 /* Valid "Server" section */
319 printf( " Name = %s\n", Conf_Server[i].name );
320 printf( " Host = %s\n", Conf_Server[i].host );
321 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
323 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
325 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
326 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
327 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
328 printf( " Group = %d\n", Conf_Server[i].group );
329 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
332 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
333 predef_chan = array_start(&Conf_Channels);
335 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
336 if (!predef_chan->name[0])
339 /* Valid "Channel" section */
341 printf(" Name = %s\n", predef_chan->name);
342 printf(" Modes = %s\n", predef_chan->modes);
343 printf(" Key = %s\n", predef_chan->key);
344 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
345 printf(" Topic = %s\n", predef_chan->topic);
346 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
349 return (config_valid ? 0 : 1);
354 Conf_UnsetServer( CONN_ID Idx )
356 /* Set next time for next connection attempt, if this is a server
357 * link that is (still) configured here. If the server is set as
358 * "once", delete it from our configuration.
359 * Non-Server-Connections will be silently ignored. */
364 /* Check all our configured servers */
365 for( i = 0; i < MAX_SERVERS; i++ ) {
366 if( Conf_Server[i].conn_id != Idx ) continue;
368 /* Gotcha! Mark server configuration as "unused": */
369 Conf_Server[i].conn_id = NONE;
371 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
372 /* Delete configuration here */
373 Init_Server_Struct( &Conf_Server[i] );
375 /* Set time for next connect attempt */
377 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
378 /* The connection has been "long", so we don't
379 * require the next attempt to be delayed. */
380 Conf_Server[i].lasttry =
381 t - Conf_ConnectRetry + RECONNECT_DELAY;
383 Conf_Server[i].lasttry = t;
386 } /* Conf_UnsetServer */
390 Conf_SetServer( int ConfServer, CONN_ID Idx )
392 /* Set connection for specified configured server */
394 assert( ConfServer > NONE );
395 assert( Idx > NONE );
397 Conf_Server[ConfServer].conn_id = Idx;
398 } /* Conf_SetServer */
402 Conf_GetServer( CONN_ID Idx )
404 /* Get index of server in configuration structure */
408 assert( Idx > NONE );
410 for( i = 0; i < MAX_SERVERS; i++ ) {
411 if( Conf_Server[i].conn_id == Idx ) return i;
414 } /* Conf_GetServer */
418 Conf_EnableServer( const char *Name, UINT16 Port )
420 /* Enable specified server and adjust port */
424 assert( Name != NULL );
426 for( i = 0; i < MAX_SERVERS; i++ ) {
427 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
428 /* Gotcha! Set port and enable server: */
429 Conf_Server[i].port = Port;
430 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
431 return (Conf_Server[i].port && Conf_Server[i].host[0]);
435 } /* Conf_EnableServer */
439 Conf_EnablePassiveServer(const char *Name)
441 /* Enable specified server */
444 assert( Name != NULL );
445 for (i = 0; i < MAX_SERVERS; i++) {
446 if ((strcasecmp( Conf_Server[i].name, Name ) == 0) && (Conf_Server[i].port > 0)) {
447 /* BINGO! Enable server */
448 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
453 } /* Conf_EnablePassiveServer */
457 Conf_DisableServer( const char *Name )
459 /* Enable specified server and adjust port */
463 assert( Name != NULL );
465 for( i = 0; i < MAX_SERVERS; i++ ) {
466 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
467 /* Gotcha! Disable and disconnect server: */
468 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
469 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
474 } /* Conf_DisableServer */
478 Conf_AddServer( const char *Name, UINT16 Port, const char *Host, const char *MyPwd, const char *PeerPwd )
480 /* Add new server to configuration */
484 assert( Name != NULL );
485 assert( Host != NULL );
486 assert( MyPwd != NULL );
487 assert( PeerPwd != NULL );
489 /* Search unused item in server configuration structure */
490 for( i = 0; i < MAX_SERVERS; i++ ) {
491 /* Is this item used? */
492 if( ! Conf_Server[i].name[0] ) break;
494 if( i >= MAX_SERVERS ) return false;
496 Init_Server_Struct( &Conf_Server[i] );
497 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
498 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
499 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
500 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
501 Conf_Server[i].port = Port;
502 Conf_Server[i].flags = CONF_SFLAG_ONCE;
505 } /* Conf_AddServer */
509 * Check if the given nick name is an service
512 Conf_IsService(int ConfServer, const char *Nick)
514 return MatchCaseInsensitive(Conf_Server[ConfServer].svs_mask, Nick);
515 } /* Conf_IsService */
519 * Initialize configuration settings with their default values.
522 Set_Defaults(bool InitServers)
526 strcpy(Conf_ServerName, "");
527 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
528 PACKAGE_NAME, PACKAGE_VERSION);
529 strcpy(Conf_ServerPwd, "");
531 strcpy(Conf_ServerAdmin1, "");
532 strcpy(Conf_ServerAdmin2, "");
533 strcpy(Conf_ServerAdminMail, "");
535 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
536 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
537 strlcpy(Conf_MotdPhrase, MOTD_PHRASE, sizeof(Conf_MotdPhrase));
539 Conf_UID = Conf_GID = 0;
540 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
541 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
543 free(Conf_ListenAddress);
544 Conf_ListenAddress = NULL;
546 Conf_PingTimeout = 120;
547 Conf_PongTimeout = 20;
548 Conf_ConnectRetry = 60;
550 Conf_NoIdent = false;
553 Conf_Channel_Count = 0;
555 Conf_OperCanMode = false;
556 Conf_OperServerMode = false;
557 Conf_AllowRemoteOper = false;
558 Conf_PredefChannelsOnly = false;
560 Conf_ConnectIPv4 = true;
561 Conf_ConnectIPv6 = true;
563 Conf_MaxConnections = 0;
564 Conf_MaxConnectionsIP = 5;
566 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
568 /* Initialize server configuration structures */
570 for (i = 0; i < MAX_SERVERS;
571 Init_Server_Struct(&Conf_Server[i++]));
579 size_t cnt = array_bytes(&Conf_ListenPorts);
581 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
587 Read_Config( bool ngircd_starting )
589 /* Read configuration file. */
591 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
592 const UINT16 defaultport = 6667;
596 /* Open configuration file */
597 fd = fopen( NGIRCd_ConfFile, "r" );
599 /* No configuration file found! */
600 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
601 NGIRCd_ConfFile, strerror( errno ));
602 if (!ngircd_starting)
604 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
608 Set_Defaults( ngircd_starting );
610 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
612 /* Clean up server configuration structure: mark all already
613 * configured servers as "once" so that they are deleted
614 * after the next disconnect and delete all unused servers.
615 * And delete all servers which are "duplicates" of servers
616 * that are already marked as "once" (such servers have been
617 * created by the last rehash but are now useless). */
618 for( i = 0; i < MAX_SERVERS; i++ ) {
619 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
621 /* This structure is in use ... */
622 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
623 /* Check for duplicates */
624 for( n = 0; n < MAX_SERVERS; n++ ) {
625 if( n == i ) continue;
627 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
628 Init_Server_Struct( &Conf_Server[n] );
630 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
636 /* Mark server as "once" */
637 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
638 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
643 /* Initialize variables */
645 strcpy( section, "" );
646 Init_Server_Struct( &New_Server );
647 New_Server_Idx = NONE;
651 /* Read configuration file */
653 if( ! fgets( str, LINE_LEN, fd )) break;
657 /* Skip comments and empty lines */
658 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
660 /* Is this the beginning of a new section? */
661 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
662 strlcpy( section, str, sizeof( section ));
663 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
666 if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
667 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
668 Config_Error( LOG_ERR, "Too many operators configured.");
670 /* Initialize new operator structure */
671 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
672 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
673 if (Conf_Oper[Conf_Oper_Count].mask) {
674 free(Conf_Oper[Conf_Oper_Count].mask );
675 Conf_Oper[Conf_Oper_Count].mask = NULL;
681 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
682 /* Check if there is already a server to add */
683 if( New_Server.name[0] ) {
684 /* Copy data to "real" server structure */
685 assert( New_Server_Idx > NONE );
686 Conf_Server[New_Server_Idx] = New_Server;
689 /* Re-init structure for new server */
690 Init_Server_Struct( &New_Server );
692 /* Search unused item in server configuration structure */
693 for( i = 0; i < MAX_SERVERS; i++ ) {
694 /* Is this item used? */
695 if( ! Conf_Server[i].name[0] ) break;
697 if( i >= MAX_SERVERS ) {
698 /* Oops, no free item found! */
699 Config_Error( LOG_ERR, "Too many servers configured." );
700 New_Server_Idx = NONE;
702 else New_Server_Idx = i;
705 if (strcasecmp(section, "[CHANNEL]") == 0) {
706 Conf_Channel_Count++;
710 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
713 if( section[0] == 0x1 ) continue;
715 /* Split line into variable name and parameters */
716 ptr = strchr( str, '=' );
718 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
722 var = str; ngt_TrimStr( var );
723 arg = ptr + 1; ngt_TrimStr( arg );
725 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
726 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
727 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
728 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
729 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
732 /* Close configuration file */
735 /* Check if there is still a server to add */
736 if( New_Server.name[0] ) {
737 /* Copy data to "real" server structure */
738 assert( New_Server_Idx > NONE );
739 Conf_Server[New_Server_Idx] = New_Server;
742 /* not a single listening port? Add default. */
743 if (no_listenports() &&
744 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
746 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
747 (unsigned int) defaultport, strerror(errno));
752 if (!Conf_ListenAddress)
753 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
755 if (!Conf_ListenAddress) {
756 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
764 Check_ArgIsTrue( const char *Arg )
766 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
767 if( strcasecmp( Arg, "true" ) == 0 ) return true;
768 if( atoi( Arg ) != 0 ) return true;
771 } /* Check_ArgIsTrue */
774 static unsigned int Handle_MaxNickLength(int Line, const char *Arg)
778 new = (unsigned) atoi(Arg) + 1;
779 if (new > CLIENT_NICK_LEN) {
780 Config_Error(LOG_WARNING,
781 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
782 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
783 return CLIENT_NICK_LEN;
786 Config_Error(LOG_WARNING,
787 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
788 NGIRCd_ConfFile, Line);
792 } /* Handle_MaxNickLength */
796 Handle_GLOBAL( int Line, char *Var, char *Arg )
803 assert( Var != NULL );
804 assert( Arg != NULL );
806 if( strcasecmp( Var, "Name" ) == 0 ) {
808 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
809 if (len >= sizeof( Conf_ServerName ))
810 Config_Error_TooLong( Line, Var );
813 if( strcasecmp( Var, "Info" ) == 0 ) {
814 /* Info text of server */
815 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
816 if (len >= sizeof( Conf_ServerInfo ))
817 Config_Error_TooLong ( Line, Var );
820 if( strcasecmp( Var, "Password" ) == 0 ) {
821 /* Global server password */
822 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
823 if (len >= sizeof( Conf_ServerPwd ))
824 Config_Error_TooLong( Line, Var );
827 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
828 /* Administrative info #1 */
829 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
830 if (len >= sizeof( Conf_ServerAdmin1 ))
831 Config_Error_TooLong ( Line, Var );
834 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
835 /* Administrative info #2 */
836 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
837 if (len >= sizeof( Conf_ServerAdmin2 ))
838 Config_Error_TooLong ( Line, Var );
841 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
842 /* Administrative email contact */
843 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
844 if (len >= sizeof( Conf_ServerAdminMail ))
845 Config_Error_TooLong( Line, Var );
849 if( strcasecmp( Var, "Ports" ) == 0 ) {
850 ports_parse(&Conf_ListenPorts, Line, Arg);
853 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
854 /* "Message of the day" (MOTD) file */
855 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
856 if (len >= sizeof( Conf_MotdFile ))
857 Config_Error_TooLong( Line, Var );
860 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
861 /* "Message of the day" phrase (instead of file) */
862 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
863 if (len >= sizeof( Conf_MotdPhrase ))
864 Config_Error_TooLong( Line, Var );
867 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
868 /* directory for chroot() */
869 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
870 if (len >= sizeof( Conf_Chroot ))
871 Config_Error_TooLong( Line, Var );
874 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
875 /* name of pidfile */
876 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
877 if (len >= sizeof( Conf_PidFile ))
878 Config_Error_TooLong( Line, Var );
881 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
882 /* UID the daemon should switch to */
883 pwd = getpwnam( Arg );
884 if( pwd ) Conf_UID = pwd->pw_uid;
887 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
890 Conf_UID = (unsigned int)atoi( Arg );
894 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
895 /* GID the daemon should use */
896 grp = getgrnam( Arg );
897 if( grp ) Conf_GID = grp->gr_gid;
900 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
903 Conf_GID = (unsigned int)atoi( Arg );
907 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
909 Conf_PingTimeout = atoi( Arg );
910 if( Conf_PingTimeout < 5 ) {
911 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
912 NGIRCd_ConfFile, Line );
913 Conf_PingTimeout = 5;
917 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
919 Conf_PongTimeout = atoi( Arg );
920 if( Conf_PongTimeout < 5 ) {
921 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
922 NGIRCd_ConfFile, Line );
923 Conf_PongTimeout = 5;
927 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
928 /* Seconds between connection attempts to other servers */
929 Conf_ConnectRetry = atoi( Arg );
930 if( Conf_ConnectRetry < 5 ) {
931 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
932 NGIRCd_ConfFile, Line );
933 Conf_ConnectRetry = 5;
937 if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
938 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
939 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
942 if( strcasecmp( Var, "NoDNS" ) == 0 ) {
943 /* don't do reverse dns lookups when clients connect? */
944 Conf_NoDNS = Check_ArgIsTrue( Arg );
947 if (strcasecmp(Var, "NoIdent") == 0) {
948 /* don't do IDENT lookups when clients connect? */
949 Conf_NoIdent = Check_ArgIsTrue(Arg);
952 /* user has enabled ident lookups explicitly, but ... */
953 Config_Error(LOG_WARNING,
954 "%s: line %d: NoIdent=False, but ngircd was built without IDENT support",
955 NGIRCd_ConfFile, Line);
961 /* the default setting for all the WANT_IPV6 special options is 'true' */
962 if( strcasecmp( Var, "ConnectIPv6" ) == 0 ) {
963 /* connect to other hosts using ipv6, if they have an AAAA record? */
964 Conf_ConnectIPv6 = Check_ArgIsTrue( Arg );
967 if( strcasecmp( Var, "ConnectIPv4" ) == 0 ) {
968 /* connect to other hosts using ipv4.
969 * again, this can be used for ipv6-only setups */
970 Conf_ConnectIPv4 = Check_ArgIsTrue( Arg );
974 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
975 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
976 Conf_OperCanMode = Check_ArgIsTrue( Arg );
979 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
980 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
981 Conf_OperServerMode = Check_ArgIsTrue( Arg );
984 if(strcasecmp(Var, "AllowRemoteOper") == 0) {
985 /* Are remote IRC operators allowed to control this server? */
986 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
989 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
990 /* Maximum number of connections. 0 -> "no limit". */
992 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
995 Conf_MaxConnections = atol( Arg );
998 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
999 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
1001 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
1004 Conf_MaxConnectionsIP = atoi( Arg );
1007 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
1008 /* Maximum number of channels a user can join. 0 -> "no limit". */
1010 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
1013 Conf_MaxJoins = atoi( Arg );
1016 if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
1017 /* Maximum length of a nick name; must be same on all servers
1018 * within the IRC network! */
1019 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1023 if( strcasecmp( Var, "Listen" ) == 0 ) {
1024 /* IP-Address to bind sockets */
1025 if (Conf_ListenAddress) {
1026 Config_Error(LOG_ERR, "Multiple Listen= options, ignoring: %s", Arg);
1029 Conf_ListenAddress = strdup_warn(Arg);
1031 * if allocation fails, we're in trouble:
1032 * we cannot ignore the error -- otherwise ngircd
1033 * would listen on all interfaces.
1035 if (!Conf_ListenAddress) {
1036 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
1043 if( strcasecmp( Var, "SSLPorts" ) == 0 ) {
1044 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1048 if( strcasecmp( Var, "SSLKeyFile" ) == 0 ) {
1049 assert(Conf_SSLOptions.KeyFile == NULL );
1050 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1053 if( strcasecmp( Var, "SSLCertFile" ) == 0 ) {
1054 assert(Conf_SSLOptions.CertFile == NULL );
1055 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1059 if( strcasecmp( Var, "SSLKeyFilePassword" ) == 0 ) {
1060 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1061 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1062 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Could not copy %s: %s!",
1063 NGIRCd_ConfFile, Line, Var, strerror(errno));
1066 if( strcasecmp( Var, "SSLDHFile" ) == 0 ) {
1067 assert(Conf_SSLOptions.DHFile == NULL);
1068 Conf_SSLOptions.DHFile = strdup_warn( Arg );
1072 Config_Error(LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
1073 NGIRCd_ConfFile, Line, Var);
1074 } /* Handle_GLOBAL */
1078 Handle_OPERATOR( int Line, char *Var, char *Arg )
1080 unsigned int opercount;
1083 assert( Var != NULL );
1084 assert( Arg != NULL );
1085 assert( Conf_Oper_Count > 0 );
1087 if ( Conf_Oper_Count == 0 )
1090 opercount = Conf_Oper_Count - 1;
1092 if( strcasecmp( Var, "Name" ) == 0 ) {
1093 /* Name of IRC operator */
1094 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
1095 if (len >= sizeof( Conf_Oper[opercount].name ))
1096 Config_Error_TooLong( Line, Var );
1099 if( strcasecmp( Var, "Password" ) == 0 ) {
1100 /* Password of IRC operator */
1101 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
1102 if (len >= sizeof( Conf_Oper[opercount].pwd ))
1103 Config_Error_TooLong( Line, Var );
1106 if( strcasecmp( Var, "Mask" ) == 0 ) {
1107 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
1109 Conf_Oper[opercount].mask = strdup_warn( Arg );
1112 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
1113 NGIRCd_ConfFile, Line, Var );
1114 } /* Handle_OPERATOR */
1118 Handle_SERVER( int Line, char *Var, char *Arg )
1124 assert( Var != NULL );
1125 assert( Arg != NULL );
1127 /* Ignore server block if no space is left in server configuration structure */
1128 if( New_Server_Idx <= NONE ) return;
1130 if( strcasecmp( Var, "Host" ) == 0 ) {
1131 /* Hostname of the server */
1132 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1133 if (len >= sizeof( New_Server.host ))
1134 Config_Error_TooLong ( Line, Var );
1137 if( strcasecmp( Var, "Name" ) == 0 ) {
1138 /* Name of the server ("Nick"/"ID") */
1139 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1140 if (len >= sizeof( New_Server.name ))
1141 Config_Error_TooLong( Line, Var );
1144 if (strcasecmp(Var, "Bind") == 0) {
1145 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1148 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1149 NGIRCd_ConfFile, Line, Arg);
1152 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1153 /* Password of this server which is sent to the peer */
1155 Config_Error(LOG_ERR,
1156 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1157 NGIRCd_ConfFile, Line);
1159 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1160 if (len >= sizeof( New_Server.pwd_in ))
1161 Config_Error_TooLong( Line, Var );
1164 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1165 /* Passwort of the peer which must be received */
1166 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1167 if (len >= sizeof( New_Server.pwd_out ))
1168 Config_Error_TooLong( Line, Var );
1171 if( strcasecmp( Var, "Port" ) == 0 ) {
1172 /* Port to which this server should connect */
1174 if( port > 0 && port < 0xFFFF )
1175 New_Server.port = (UINT16)port;
1177 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
1178 NGIRCd_ConfFile, Line, port );
1182 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1183 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1187 if( strcasecmp( Var, "Group" ) == 0 ) {
1190 if( ! isdigit( (int)*Arg ))
1191 Config_Error_NaN( Line, Var );
1194 New_Server.group = atoi( Arg );
1197 if( strcasecmp( Var, "Passive" ) == 0 ) {
1198 if (Check_ArgIsTrue(Arg))
1199 New_Server.flags |= CONF_SFLAG_DISABLED;
1202 if (strcasecmp(Var, "ServiceMask") == 0) {
1203 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1204 sizeof(New_Server.svs_mask));
1205 if (len >= sizeof(New_Server.svs_mask))
1206 Config_Error_TooLong(Line, Var);
1210 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
1211 NGIRCd_ConfFile, Line, Var );
1212 } /* Handle_SERVER */
1216 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1218 size_t size = sizeof(new_chan->name);
1219 char *dest = new_chan->name;
1221 if (!Channel_IsValidName(name)) {
1223 * maybe user forgot to add a '#'.
1224 * This is only here for user convenience.
1230 return size > strlcpy(dest, name, size);
1235 Handle_CHANNEL(int Line, char *Var, char *Arg)
1239 struct Conf_Channel *chan;
1242 assert( Var != NULL );
1243 assert( Arg != NULL );
1244 assert(Conf_Channel_Count > 0);
1246 chancount = Conf_Channel_Count - 1;
1248 chan = array_alloc(&Conf_Channels, sizeof(*chan), chancount);
1250 Config_Error(LOG_ERR, "Could not allocate memory for predefined channel (%d:%s = %s)", Line, Var, Arg);
1253 if (strcasecmp(Var, "Name") == 0) {
1254 if (!Handle_Channelname(chan, Arg))
1255 Config_Error_TooLong(Line, Var);
1258 if (strcasecmp(Var, "Modes") == 0) {
1260 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1261 if (len >= sizeof(chan->modes))
1262 Config_Error_TooLong( Line, Var );
1265 if( strcasecmp( Var, "Topic" ) == 0 ) {
1267 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1268 if (len >= sizeof(chan->topic))
1269 Config_Error_TooLong( Line, Var );
1272 if( strcasecmp( Var, "Key" ) == 0 ) {
1273 /* Initial Channel Key (mode k) */
1274 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1275 if (len >= sizeof(chan->key))
1276 Config_Error_TooLong(Line, Var);
1279 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1280 /* maximum user limit, mode l */
1281 chan->maxusers = (unsigned long) atol(Arg);
1282 if (chan->maxusers == 0)
1283 Config_Error_NaN(Line, Var);
1286 if (strcasecmp(Var, "KeyFile") == 0) {
1288 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1289 if (len >= sizeof(chan->keyfile))
1290 Config_Error_TooLong(Line, Var);
1294 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1295 NGIRCd_ConfFile, Line, Var );
1296 } /* Handle_CHANNEL */
1300 Validate_Config(bool Configtest, bool Rehash)
1302 /* Validate configuration settings. */
1305 int i, servers, servers_once;
1307 bool config_valid = true;
1310 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1311 ptr = Conf_ServerName;
1313 if (*ptr >= 'a' && *ptr <= 'z') continue;
1314 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1315 if (*ptr >= '0' && *ptr <= '9') continue;
1316 if (ptr > Conf_ServerName) {
1317 if (*ptr == '.' || *ptr == '-')
1320 Conf_ServerName[0] = '\0';
1324 if (!Conf_ServerName[0]) {
1325 /* No server name configured! */
1326 config_valid = false;
1327 Config_Error(LOG_ALERT,
1328 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1330 if (!Configtest && !Rehash) {
1331 Config_Error(LOG_ALERT,
1332 "%s exiting due to fatal errors!",
1338 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1339 /* No dot in server name! */
1340 config_valid = false;
1341 Config_Error(LOG_ALERT,
1342 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1345 Config_Error(LOG_ALERT,
1346 "%s exiting due to fatal errors!",
1353 if (!Conf_ServerAdminMail[0]) {
1354 /* No administrative contact configured! */
1355 config_valid = false;
1356 Config_Error(LOG_ALERT,
1357 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1360 Config_Error(LOG_ALERT,
1361 "%s exiting due to fatal errors!",
1368 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1369 && !Conf_ServerAdminMail[0]) {
1370 /* No administrative information configured! */
1371 Config_Error(LOG_WARNING,
1372 "No administrative information configured but required by RFC!");
1376 servers = servers_once = 0;
1377 for (i = 0; i < MAX_SERVERS; i++) {
1378 if (Conf_Server[i].name[0]) {
1380 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1385 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1386 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1389 return config_valid;
1390 } /* Validate_Config */
1394 Config_Error_TooLong ( const int Line, const char *Item )
1396 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1401 Config_Error_NaN( const int Line, const char *Item )
1403 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1404 NGIRCd_ConfFile, Line, Item );
1409 static void Config_Error( const int Level, const char *Format, ... )
1411 static void Config_Error( Level, Format, va_alist )
1417 /* Error! Write to console and/or logfile. */
1419 char msg[MAX_LOG_MSG_LEN];
1422 assert( Format != NULL );
1425 va_start( ap, Format );
1429 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1432 /* During "normal operations" the log functions of the daemon should
1433 * be used, but during testing of the configuration file, all messages
1434 * should go directly to the console: */
1435 if (Use_Log) Log( Level, "%s", msg );
1437 } /* Config_Error */
1441 Init_Server_Struct( CONF_SERVER *Server )
1443 /* Initialize server configuration structur to default values */
1445 assert( Server != NULL );
1447 memset( Server, 0, sizeof (CONF_SERVER) );
1449 Server->group = NONE;
1450 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1452 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1454 Resolve_Init(&Server->res_stat);
1455 Server->conn_id = NONE;
1456 memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
1457 } /* Init_Server_Struct */