2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 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>
52 static bool Use_Log = true;
53 static CONF_SERVER New_Server;
54 static int New_Server_Idx;
57 static void Set_Defaults PARAMS(( bool InitServers ));
58 static bool Read_Config PARAMS(( bool ngircd_starting ));
59 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
61 static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
62 static void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
63 static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
64 static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
66 static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
68 static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
69 static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
71 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
74 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
76 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
80 strdup_warn(const char *str)
82 char *ptr = strdup(str);
84 Config_Error(LOG_ERR, "Could not allocate mem for string: %s", str);
94 len = array_length(a, sizeof(UINT16));
96 ports = (UINT16*) array_start(a);
97 printf("%u", (unsigned int) *ports);
100 printf(", %u", (unsigned int) *ports);
108 ports_parse(array *a, int Line, char *Arg)
116 /* Ports on that the server should listen. More port numbers
117 * must be separated by "," */
118 ptr = strtok( Arg, "," );
122 if (port > 0 && port < 0xFFFF) {
123 port16 = (UINT16) port;
124 if (!array_catb(a, (char*)&port16, sizeof port16))
125 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
126 NGIRCd_ConfFile, Line, port, strerror(errno));
128 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
129 NGIRCd_ConfFile, Line, port );
132 ptr = strtok( NULL, "," );
141 Validate_Config(false, false);
148 if (!Read_Config(false))
150 Validate_Config(false, true);
152 /* Update CLIENT structure of local server */
153 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
155 } /* Config_Rehash */
159 yesno_to_str(int boolean_value)
170 /* Read configuration, validate and output it. */
180 if (! Read_Config(true))
183 config_valid = Validate_Config(true, false);
185 /* If stdin and stdout ("you can read our nice message and we can
186 * read in your keypress") are valid tty's, wait for a key: */
187 if( isatty( fileno( stdin )) && isatty( fileno( stdout ))) {
188 puts( "OK, press enter to see a dump of your service configuration ..." );
191 puts( "Ok, dump of your server configuration follows:\n" );
195 printf( " Name = %s\n", Conf_ServerName );
196 printf( " Info = %s\n", Conf_ServerInfo );
197 printf( " Password = %s\n", Conf_ServerPwd );
198 printf( " AdminInfo1 = %s\n", Conf_ServerAdmin1 );
199 printf( " AdminInfo2 = %s\n", Conf_ServerAdmin2 );
200 printf( " AdminEMail = %s\n", Conf_ServerAdminMail );
201 printf( " MotdFile = %s\n", Conf_MotdFile );
202 printf( " MotdPhrase = %s\n", Conf_MotdPhrase );
203 printf( " ChrootDir = %s\n", Conf_Chroot );
204 printf( " PidFile = %s\n", Conf_PidFile);
205 fputs(" Ports = ", stdout);
207 ports_puts(&Conf_ListenPorts);
208 printf(" Listen = %s\n", Conf_ListenAddress);
209 pwd = getpwuid( Conf_UID );
210 if( pwd ) printf( " ServerUID = %s\n", pwd->pw_name );
211 else printf( " ServerUID = %ld\n", (long)Conf_UID );
212 grp = getgrgid( Conf_GID );
213 if( grp ) printf( " ServerGID = %s\n", grp->gr_name );
214 else printf( " ServerGID = %ld\n", (long)Conf_GID );
215 printf( " PingTimeout = %d\n", Conf_PingTimeout );
216 printf( " PongTimeout = %d\n", Conf_PongTimeout );
217 printf( " ConnectRetry = %d\n", Conf_ConnectRetry );
218 printf( " OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
219 printf( " OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
220 printf( " PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
221 printf( " NoDNS = %s\n", yesno_to_str(Conf_NoDNS));
224 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
225 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
227 printf( " MaxConnections = %ld\n", Conf_MaxConnections);
228 printf( " MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
229 printf( " MaxJoins = %d\n", Conf_MaxJoins>0 ? Conf_MaxJoins : -1);
230 printf( " MaxNickLength = %u\n\n", Conf_MaxNickLength - 1);
232 for( i = 0; i < Conf_Oper_Count; i++ ) {
233 if( ! Conf_Oper[i].name[0] ) continue;
235 /* Valid "Operator" section */
236 puts( "[OPERATOR]" );
237 printf( " Name = %s\n", Conf_Oper[i].name );
238 printf( " Password = %s\n", Conf_Oper[i].pwd );
239 if ( Conf_Oper[i].mask ) printf( " Mask = %s\n", Conf_Oper[i].mask );
243 for( i = 0; i < MAX_SERVERS; i++ ) {
244 if( ! Conf_Server[i].name[0] ) continue;
246 /* Valid "Server" section */
248 printf( " Name = %s\n", Conf_Server[i].name );
249 printf( " Host = %s\n", Conf_Server[i].host );
250 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
251 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
252 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
253 printf( " Group = %d\n", Conf_Server[i].group );
254 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
257 for( i = 0; i < Conf_Channel_Count; i++ ) {
258 if( ! Conf_Channel[i].name[0] ) continue;
260 /* Valid "Channel" section */
262 printf( " Name = %s\n", Conf_Channel[i].name );
263 printf( " Modes = %s\n", Conf_Channel[i].modes );
264 printf( " Key = %s\n", Conf_Channel[i].key );
265 printf( " MaxUsers = %lu\n", Conf_Channel[i].maxusers );
267 topic = (char*)array_start(&Conf_Channel[i].topic);
268 printf( " Topic = %s\n\n", topic ? topic : "");
271 return (config_valid ? 0 : 1);
276 Conf_UnsetServer( CONN_ID Idx )
278 /* Set next time for next connection attempt, if this is a server
279 * link that is (still) configured here. If the server is set as
280 * "once", delete it from our configuration.
281 * Non-Server-Connections will be silently ignored. */
286 /* Check all our configured servers */
287 for( i = 0; i < MAX_SERVERS; i++ ) {
288 if( Conf_Server[i].conn_id != Idx ) continue;
290 /* Gotcha! Mark server configuration as "unused": */
291 Conf_Server[i].conn_id = NONE;
293 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
294 /* Delete configuration here */
295 Init_Server_Struct( &Conf_Server[i] );
297 /* Set time for next connect attempt */
299 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
300 /* The connection has been "long", so we don't
301 * require the next attempt to be delayed. */
302 Conf_Server[i].lasttry =
303 t - Conf_ConnectRetry + RECONNECT_DELAY;
305 Conf_Server[i].lasttry = t;
308 } /* Conf_UnsetServer */
312 Conf_SetServer( int ConfServer, CONN_ID Idx )
314 /* Set connection for specified configured server */
316 assert( ConfServer > NONE );
317 assert( Idx > NONE );
319 Conf_Server[ConfServer].conn_id = Idx;
320 } /* Conf_SetServer */
324 Conf_GetServer( CONN_ID Idx )
326 /* Get index of server in configuration structure */
330 assert( Idx > NONE );
332 for( i = 0; i < MAX_SERVERS; i++ ) {
333 if( Conf_Server[i].conn_id == Idx ) return i;
336 } /* Conf_GetServer */
340 Conf_EnableServer( char *Name, UINT16 Port )
342 /* Enable specified server and adjust port */
346 assert( Name != NULL );
348 for( i = 0; i < MAX_SERVERS; i++ ) {
349 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
350 /* Gotcha! Set port and enable server: */
351 Conf_Server[i].port = Port;
352 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
357 } /* Conf_EnableServer */
361 Conf_EnablePassiveServer(const char *Name)
363 /* Enable specified server */
366 assert( Name != NULL );
367 for (i = 0; i < MAX_SERVERS; i++) {
368 if ((strcasecmp( Conf_Server[i].name, Name ) == 0) && (Conf_Server[i].port > 0)) {
369 /* BINGO! Enable server */
370 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
375 } /* Conf_EnablePassiveServer */
379 Conf_DisableServer( char *Name )
381 /* Enable specified server and adjust port */
385 assert( Name != NULL );
387 for( i = 0; i < MAX_SERVERS; i++ ) {
388 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
389 /* Gotcha! Disable and disconnect server: */
390 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
391 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
396 } /* Conf_DisableServer */
400 Conf_AddServer( char *Name, UINT16 Port, char *Host, char *MyPwd, char *PeerPwd )
402 /* Add new server to configuration */
406 assert( Name != NULL );
407 assert( Host != NULL );
408 assert( MyPwd != NULL );
409 assert( PeerPwd != NULL );
411 /* Search unused item in server configuration structure */
412 for( i = 0; i < MAX_SERVERS; i++ ) {
413 /* Is this item used? */
414 if( ! Conf_Server[i].name[0] ) break;
416 if( i >= MAX_SERVERS ) return false;
418 Init_Server_Struct( &Conf_Server[i] );
419 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
420 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
421 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
422 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
423 Conf_Server[i].port = Port;
424 Conf_Server[i].flags = CONF_SFLAG_ONCE;
427 } /* Conf_AddServer */
431 Set_Defaults( bool InitServers )
433 /* Initialize configuration variables with default values. */
437 strcpy( Conf_ServerName, "" );
438 snprintf( Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
439 strcpy( Conf_ServerPwd, "" );
441 strcpy( Conf_ServerAdmin1, "" );
442 strcpy( Conf_ServerAdmin2, "" );
443 strcpy( Conf_ServerAdminMail, "" );
445 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
446 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
448 strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
450 strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
452 strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
454 free(Conf_ListenAddress);
455 Conf_ListenAddress = NULL;
456 Conf_UID = Conf_GID = 0;
458 Conf_PingTimeout = 120;
459 Conf_PongTimeout = 20;
461 Conf_ConnectRetry = 60;
464 Conf_Channel_Count = 0;
466 Conf_OperCanMode = false;
468 Conf_PredefChannelsOnly = false;
469 Conf_OperServerMode = false;
471 Conf_ConnectIPv4 = true;
472 Conf_ConnectIPv6 = true;
474 Conf_MaxConnections = 0;
475 Conf_MaxConnectionsIP = 5;
477 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
479 /* Initialize server configuration structures */
480 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
485 Read_Config( bool ngircd_starting )
487 /* Read configuration file. */
489 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
490 const UINT16 defaultport = 6667;
494 /* Open configuration file */
495 fd = fopen( NGIRCd_ConfFile, "r" );
497 /* No configuration file found! */
498 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
499 NGIRCd_ConfFile, strerror( errno ));
500 if (!ngircd_starting)
502 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
506 Set_Defaults( ngircd_starting );
508 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
510 /* Clean up server configuration structure: mark all already
511 * configured servers as "once" so that they are deleted
512 * after the next disconnect and delete all unused servers.
513 * And delete all servers which are "duplicates" of servers
514 * that are already marked as "once" (such servers have been
515 * created by the last rehash but are now useless). */
516 for( i = 0; i < MAX_SERVERS; i++ ) {
517 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
519 /* This structure is in use ... */
520 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
521 /* Check for duplicates */
522 for( n = 0; n < MAX_SERVERS; n++ ) {
523 if( n == i ) continue;
525 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
526 Init_Server_Struct( &Conf_Server[n] );
528 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
534 /* Mark server as "once" */
535 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
536 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
541 /* Initialize variables */
543 strcpy( section, "" );
544 Init_Server_Struct( &New_Server );
545 New_Server_Idx = NONE;
547 /* Read configuration file */
549 if( ! fgets( str, LINE_LEN, fd )) break;
553 /* Skip comments and empty lines */
554 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
556 /* Is this the beginning of a new section? */
557 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
558 strlcpy( section, str, sizeof( section ));
559 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
562 if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
563 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
564 Config_Error( LOG_ERR, "Too many operators configured.");
566 /* Initialize new operator structure */
567 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
568 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
569 if (Conf_Oper[Conf_Oper_Count].mask) {
570 free(Conf_Oper[Conf_Oper_Count].mask );
571 Conf_Oper[Conf_Oper_Count].mask = NULL;
577 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
578 /* Check if there is already a server to add */
579 if( New_Server.name[0] ) {
580 /* Copy data to "real" server structure */
581 assert( New_Server_Idx > NONE );
582 Conf_Server[New_Server_Idx] = New_Server;
585 /* Re-init structure for new server */
586 Init_Server_Struct( &New_Server );
588 /* Search unused item in server configuration structure */
589 for( i = 0; i < MAX_SERVERS; i++ ) {
590 /* Is this item used? */
591 if( ! Conf_Server[i].name[0] ) break;
593 if( i >= MAX_SERVERS ) {
594 /* Oops, no free item found! */
595 Config_Error( LOG_ERR, "Too many servers configured." );
596 New_Server_Idx = NONE;
598 else New_Server_Idx = i;
601 if( strcasecmp( section, "[CHANNEL]" ) == 0 ) {
602 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) {
603 Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
605 /* Initialize new channel structure */
606 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
607 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
608 strcpy( Conf_Channel[Conf_Channel_Count].key, "" );
609 Conf_Channel[Conf_Channel_Count].maxusers = 0;
610 array_free(&Conf_Channel[Conf_Channel_Count].topic);
611 Conf_Channel_Count++;
615 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
618 if( section[0] == 0x1 ) continue;
620 /* Split line into variable name and parameters */
621 ptr = strchr( str, '=' );
623 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
627 var = str; ngt_TrimStr( var );
628 arg = ptr + 1; ngt_TrimStr( arg );
630 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
631 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
632 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
633 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
634 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
637 /* Close configuration file */
640 /* Check if there is still a server to add */
641 if( New_Server.name[0] ) {
642 /* Copy data to "real" server structure */
643 assert( New_Server_Idx > NONE );
644 Conf_Server[New_Server_Idx] = New_Server;
647 if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
648 if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
649 Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
650 (unsigned int) defaultport, strerror(errno));
655 if (!Conf_ListenAddress)
656 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
658 if (!Conf_ListenAddress) {
659 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
667 Check_ArgIsTrue( const char *Arg )
669 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
670 if( strcasecmp( Arg, "true" ) == 0 ) return true;
671 if( atoi( Arg ) != 0 ) return true;
674 } /* Check_ArgIsTrue */
677 static unsigned int Handle_MaxNickLength(int Line, const char *Arg)
681 new = (unsigned) atoi(Arg) + 1;
682 if (new > CLIENT_NICK_LEN) {
683 Config_Error(LOG_WARNING,
684 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
685 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
686 return CLIENT_NICK_LEN;
689 Config_Error(LOG_WARNING,
690 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
691 NGIRCd_ConfFile, Line);
695 } /* Handle_MaxNickLength */
699 Handle_GLOBAL( int Line, char *Var, char *Arg )
706 assert( Var != NULL );
707 assert( Arg != NULL );
709 if( strcasecmp( Var, "Name" ) == 0 ) {
711 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
712 if (len >= sizeof( Conf_ServerName ))
713 Config_Error_TooLong( Line, Var );
716 if( strcasecmp( Var, "Info" ) == 0 ) {
717 /* Info text of server */
718 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
719 if (len >= sizeof( Conf_ServerInfo ))
720 Config_Error_TooLong ( Line, Var );
723 if( strcasecmp( Var, "Password" ) == 0 ) {
724 /* Global server password */
725 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
726 if (len >= sizeof( Conf_ServerPwd ))
727 Config_Error_TooLong( Line, Var );
730 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
731 /* Administrative info #1 */
732 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
733 if (len >= sizeof( Conf_ServerAdmin1 ))
734 Config_Error_TooLong ( Line, Var );
737 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
738 /* Administrative info #2 */
739 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
740 if (len >= sizeof( Conf_ServerAdmin2 ))
741 Config_Error_TooLong ( Line, Var );
744 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
745 /* Administrative email contact */
746 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
747 if (len >= sizeof( Conf_ServerAdminMail ))
748 Config_Error_TooLong( Line, Var );
752 if( strcasecmp( Var, "Ports" ) == 0 ) {
753 ports_parse(&Conf_ListenPorts, Line, Arg);
756 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
757 /* "Message of the day" (MOTD) file */
758 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
759 if (len >= sizeof( Conf_MotdFile ))
760 Config_Error_TooLong( Line, Var );
763 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
764 /* "Message of the day" phrase (instead of file) */
765 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
766 if (len >= sizeof( Conf_MotdPhrase ))
767 Config_Error_TooLong( Line, Var );
770 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
771 /* directory for chroot() */
772 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
773 if (len >= sizeof( Conf_Chroot ))
774 Config_Error_TooLong( Line, Var );
777 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
778 /* name of pidfile */
779 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
780 if (len >= sizeof( Conf_PidFile ))
781 Config_Error_TooLong( Line, Var );
784 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
785 /* UID the daemon should switch to */
786 pwd = getpwnam( Arg );
787 if( pwd ) Conf_UID = pwd->pw_uid;
790 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
793 Conf_UID = (unsigned int)atoi( Arg );
797 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
798 /* GID the daemon should use */
799 grp = getgrnam( Arg );
800 if( grp ) Conf_GID = grp->gr_gid;
803 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
806 Conf_GID = (unsigned int)atoi( Arg );
810 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
812 Conf_PingTimeout = atoi( Arg );
813 if( Conf_PingTimeout < 5 ) {
814 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
815 NGIRCd_ConfFile, Line );
816 Conf_PingTimeout = 5;
820 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
822 Conf_PongTimeout = atoi( Arg );
823 if( Conf_PongTimeout < 5 ) {
824 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
825 NGIRCd_ConfFile, Line );
826 Conf_PongTimeout = 5;
830 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
831 /* Seconds between connection attempts to other servers */
832 Conf_ConnectRetry = atoi( Arg );
833 if( Conf_ConnectRetry < 5 ) {
834 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
835 NGIRCd_ConfFile, Line );
836 Conf_ConnectRetry = 5;
840 if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
841 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
842 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
845 if( strcasecmp( Var, "NoDNS" ) == 0 ) {
846 /* don't do reverse dns lookups when clients connect? */
847 Conf_NoDNS = Check_ArgIsTrue( Arg );
851 /* the default setting for all the WANT_IPV6 special options is 'true' */
852 if( strcasecmp( Var, "ConnectIPv6" ) == 0 ) {
853 /* connect to other hosts using ipv6, if they have an AAAA record? */
854 Conf_ConnectIPv6 = Check_ArgIsTrue( Arg );
857 if( strcasecmp( Var, "ConnectIPv4" ) == 0 ) {
858 /* connect to other hosts using ipv4.
859 * again, this can be used for ipv6-only setups */
860 Conf_ConnectIPv4 = Check_ArgIsTrue( Arg );
864 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
865 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
866 Conf_OperCanMode = Check_ArgIsTrue( Arg );
869 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
870 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
871 Conf_OperServerMode = Check_ArgIsTrue( Arg );
874 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
875 /* Maximum number of connections. 0 -> "no limit". */
877 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
880 Conf_MaxConnections = atol( Arg );
883 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
884 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
886 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
889 Conf_MaxConnectionsIP = atoi( Arg );
892 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
893 /* Maximum number of channels a user can join. 0 -> "no limit". */
895 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
898 Conf_MaxJoins = atoi( Arg );
901 if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
902 /* Maximum length of a nick name; must be same on all servers
903 * within the IRC network! */
904 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
908 if( strcasecmp( Var, "Listen" ) == 0 ) {
909 /* IP-Address to bind sockets */
910 if (Conf_ListenAddress) {
911 Config_Error(LOG_ERR, "Multiple Listen= options, ignoring: %s", Arg);
914 Conf_ListenAddress = strdup_warn(Arg);
916 * if allocation fails, we're in trouble:
917 * we cannot ignore the error -- otherwise ngircd
918 * would listen on all interfaces.
920 if (!Conf_ListenAddress) {
921 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
926 Config_Error(LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
927 NGIRCd_ConfFile, Line, Var);
928 } /* Handle_GLOBAL */
932 Handle_OPERATOR( int Line, char *Var, char *Arg )
934 unsigned int opercount;
937 assert( Var != NULL );
938 assert( Arg != NULL );
939 assert( Conf_Oper_Count > 0 );
941 if ( Conf_Oper_Count == 0 )
944 opercount = Conf_Oper_Count - 1;
946 if( strcasecmp( Var, "Name" ) == 0 ) {
947 /* Name of IRC operator */
948 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
949 if (len >= sizeof( Conf_Oper[opercount].name ))
950 Config_Error_TooLong( Line, Var );
953 if( strcasecmp( Var, "Password" ) == 0 ) {
954 /* Password of IRC operator */
955 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
956 if (len >= sizeof( Conf_Oper[opercount].pwd ))
957 Config_Error_TooLong( Line, Var );
960 if( strcasecmp( Var, "Mask" ) == 0 ) {
961 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
963 Conf_Oper[opercount].mask = strdup_warn( Arg );
966 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
967 NGIRCd_ConfFile, Line, Var );
968 } /* Handle_OPERATOR */
972 Handle_SERVER( int Line, char *Var, char *Arg )
978 assert( Var != NULL );
979 assert( Arg != NULL );
981 /* Ignore server block if no space is left in server configuration structure */
982 if( New_Server_Idx <= NONE ) return;
984 if( strcasecmp( Var, "Host" ) == 0 ) {
985 /* Hostname of the server */
986 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
987 if (len >= sizeof( New_Server.host ))
988 Config_Error_TooLong ( Line, Var );
991 if( strcasecmp( Var, "Name" ) == 0 ) {
992 /* Name of the server ("Nick"/"ID") */
993 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
994 if (len >= sizeof( New_Server.name ))
995 Config_Error_TooLong( Line, Var );
998 if (strcasecmp(Var, "Bind") == 0) {
999 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1002 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1003 NGIRCd_ConfFile, Line, Arg);
1006 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1007 /* Password of this server which is sent to the peer */
1009 Config_Error(LOG_ERR,
1010 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1011 NGIRCd_ConfFile, Line);
1013 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1014 if (len >= sizeof( New_Server.pwd_in ))
1015 Config_Error_TooLong( Line, Var );
1018 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1019 /* Passwort of the peer which must be received */
1020 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1021 if (len >= sizeof( New_Server.pwd_out ))
1022 Config_Error_TooLong( Line, Var );
1025 if( strcasecmp( Var, "Port" ) == 0 ) {
1026 /* Port to which this server should connect */
1028 if( port > 0 && port < 0xFFFF )
1029 New_Server.port = (UINT16)port;
1031 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
1032 NGIRCd_ConfFile, Line, port );
1035 if( strcasecmp( Var, "Group" ) == 0 ) {
1038 if( ! isdigit( (int)*Arg ))
1039 Config_Error_NaN( Line, Var );
1042 New_Server.group = atoi( Arg );
1045 if( strcasecmp( Var, "Passive" ) == 0 ) {
1046 if (Check_ArgIsTrue(Arg))
1047 New_Server.flags |= CONF_SFLAG_DISABLED;
1051 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
1052 NGIRCd_ConfFile, Line, Var );
1053 } /* Handle_SERVER */
1057 Handle_Channelname(size_t chancount, const char *name)
1059 size_t size = sizeof( Conf_Channel[chancount].name );
1060 char *dest = Conf_Channel[chancount].name;
1062 if (*name && *name != '#') {
1067 return size > strlcpy(dest, name, size);
1072 Handle_CHANNEL( int Line, char *Var, char *Arg )
1075 size_t chancount = 0;
1078 assert( Var != NULL );
1079 assert( Arg != NULL );
1080 if (Conf_Channel_Count > 0)
1081 chancount = Conf_Channel_Count - 1;
1083 if( strcasecmp( Var, "Name" ) == 0 ) {
1084 if (!Handle_Channelname(chancount, Arg))
1085 Config_Error_TooLong( Line, Var );
1088 if( strcasecmp( Var, "Modes" ) == 0 ) {
1090 len = strlcpy( Conf_Channel[chancount].modes, Arg, sizeof( Conf_Channel[chancount].modes ));
1091 if (len >= sizeof( Conf_Channel[chancount].modes ))
1092 Config_Error_TooLong( Line, Var );
1095 if( strcasecmp( Var, "Topic" ) == 0 ) {
1097 if (!array_copys( &Conf_Channel[chancount].topic, Arg))
1098 Config_Error_TooLong( Line, Var );
1102 if( strcasecmp( Var, "Key" ) == 0 ) {
1103 /* Initial Channel Key (mode k) */
1104 len = strlcpy(Conf_Channel[chancount].key, Arg, sizeof(Conf_Channel[chancount].key));
1105 if (len >= sizeof( Conf_Channel[chancount].key ))
1106 Config_Error_TooLong(Line, Var);
1110 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1111 /* maximum user limit, mode l */
1112 Conf_Channel[chancount].maxusers = (unsigned long) atol(Arg);
1113 if (Conf_Channel[chancount].maxusers == 0)
1114 Config_Error_NaN(Line, Var);
1118 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1119 NGIRCd_ConfFile, Line, Var );
1120 } /* Handle_CHANNEL */
1124 Validate_Config(bool Configtest, bool Rehash)
1126 /* Validate configuration settings. */
1129 int i, servers, servers_once;
1131 bool config_valid = true;
1134 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1135 ptr = Conf_ServerName;
1137 if (*ptr >= 'a' && *ptr <= 'z') continue;
1138 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1139 if (*ptr >= '0' && *ptr <= '9') continue;
1140 if (ptr > Conf_ServerName) {
1141 if (*ptr == '.' || *ptr == '-')
1144 Conf_ServerName[0] = '\0';
1148 if (!Conf_ServerName[0]) {
1149 /* No server name configured! */
1150 config_valid = false;
1151 Config_Error(LOG_ALERT,
1152 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1154 if (!Configtest && !Rehash) {
1155 Config_Error(LOG_ALERT,
1156 "%s exiting due to fatal errors!",
1162 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1163 /* No dot in server name! */
1164 config_valid = false;
1165 Config_Error(LOG_ALERT,
1166 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1169 Config_Error(LOG_ALERT,
1170 "%s exiting due to fatal errors!",
1177 if (!Conf_ServerAdminMail[0]) {
1178 /* No administrative contact configured! */
1179 config_valid = false;
1180 Config_Error(LOG_ALERT,
1181 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1184 Config_Error(LOG_ALERT,
1185 "%s exiting due to fatal errors!",
1192 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1193 && !Conf_ServerAdminMail[0]) {
1194 /* No administrative information configured! */
1195 Config_Error(LOG_WARNING,
1196 "No administrative information configured but required by RFC!");
1200 servers = servers_once = 0;
1201 for (i = 0; i < MAX_SERVERS; i++) {
1202 if (Conf_Server[i].name[0]) {
1204 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1209 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1210 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1213 return config_valid;
1214 } /* Validate_Config */
1218 Config_Error_TooLong ( const int Line, const char *Item )
1220 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1225 Config_Error_NaN( const int Line, const char *Item )
1227 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1228 NGIRCd_ConfFile, Line, Item );
1233 static void Config_Error( const int Level, const char *Format, ... )
1235 static void Config_Error( Level, Format, va_alist )
1241 /* Error! Write to console and/or logfile. */
1243 char msg[MAX_LOG_MSG_LEN];
1246 assert( Format != NULL );
1249 va_start( ap, Format );
1253 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1256 /* During "normal operations" the log functions of the daemon should
1257 * be used, but during testing of the configuration file, all messages
1258 * should go directly to the console: */
1259 if (Use_Log) Log( Level, "%s", msg );
1261 } /* Config_Error */
1265 Init_Server_Struct( CONF_SERVER *Server )
1267 /* Initialize server configuration structur to default values */
1269 assert( Server != NULL );
1271 memset( Server, 0, sizeof (CONF_SERVER) );
1273 Server->group = NONE;
1274 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1276 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1278 Resolve_Init(&Server->res_stat);
1279 Server->conn_id = NONE;
1280 memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
1281 } /* Init_Server_Struct */