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)
17 static char UNUSED id[] = "$Id: conf.c,v 1.99 2007/10/13 19:11:06 fw Exp $";
34 #include <sys/types.h>
54 static bool Use_Log = true;
55 static CONF_SERVER New_Server;
56 static int New_Server_Idx;
59 static void Set_Defaults PARAMS(( bool InitServers ));
60 static void Read_Config PARAMS(( void ));
61 static void 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 ));
77 strdup_warn(const char *str)
79 char *ptr = strdup(str);
81 Config_Error(LOG_ERR, "Could not allocate mem for string: %s", str);
91 len = array_length(a, sizeof(UINT16));
93 ports = (UINT16*) array_start(a);
94 printf("%u", (unsigned int) *ports);
97 printf(", %u", (unsigned int) *ports);
105 ports_parse(array *a, int Line, char *Arg)
113 /* Ports on that the server should listen. More port numbers
114 * must be separated by "," */
115 ptr = strtok( Arg, "," );
119 if (port > 0 && port < 0xFFFF) {
120 port16 = (UINT16) port;
121 if (!array_catb(a, (char*)&port16, sizeof port16))
122 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
123 NGIRCd_ConfFile, Line, port, strerror(errno));
125 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
126 NGIRCd_ConfFile, Line, port );
129 ptr = strtok( NULL, "," );
137 Set_Defaults( true );
139 Validate_Config(false, false);
146 Set_Defaults( false );
148 Validate_Config(false, true);
150 /* Update CLIENT structure of local server */
151 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
152 } /* Config_Rehash */
158 /* Read configuration, validate and output it. */
166 Set_Defaults( true );
169 Validate_Config(true, false);
171 /* If stdin and stdout ("you can read our nice message and we can
172 * read in your keypress") are valid tty's, wait for a key: */
173 if( isatty( fileno( stdin )) && isatty( fileno( stdout ))) {
174 puts( "OK, press enter to see a dump of your service configuration ..." );
177 puts( "Ok, dump of your server configuration follows:\n" );
181 printf( " Name = %s\n", Conf_ServerName );
182 printf( " Info = %s\n", Conf_ServerInfo );
183 printf( " Password = %s\n", Conf_ServerPwd );
184 printf( " AdminInfo1 = %s\n", Conf_ServerAdmin1 );
185 printf( " AdminInfo2 = %s\n", Conf_ServerAdmin2 );
186 printf( " AdminEMail = %s\n", Conf_ServerAdminMail );
187 printf( " MotdFile = %s\n", Conf_MotdFile );
188 printf( " MotdPhrase = %s\n", Conf_MotdPhrase );
189 printf( " ChrootDir = %s\n", Conf_Chroot );
190 printf( " PidFile = %s\n", Conf_PidFile);
191 fputs(" Ports = ", stdout);
193 ports_puts(&Conf_ListenPorts);
195 printf( " Listen = %s\n", Conf_ListenAddress );
196 pwd = getpwuid( Conf_UID );
197 if( pwd ) printf( " ServerUID = %s\n", pwd->pw_name );
198 else printf( " ServerUID = %ld\n", (long)Conf_UID );
199 grp = getgrgid( Conf_GID );
200 if( grp ) printf( " ServerGID = %s\n", grp->gr_name );
201 else printf( " ServerGID = %ld\n", (long)Conf_GID );
202 printf( " PingTimeout = %d\n", Conf_PingTimeout );
203 printf( " PongTimeout = %d\n", Conf_PongTimeout );
204 printf( " ConnectRetry = %d\n", Conf_ConnectRetry );
205 printf( " OperCanUseMode = %s\n", Conf_OperCanMode == true ? "yes" : "no" );
206 printf( " OperServerMode = %s\n", Conf_OperServerMode == true? "yes" : "no" );
207 printf( " PredefChannelsOnly = %s\n", Conf_PredefChannelsOnly == true ? "yes" : "no" );
208 printf( " MaxConnections = %ld\n", Conf_MaxConnections>0 ? Conf_MaxConnections : -1);
209 printf( " MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP>0 ? Conf_MaxConnectionsIP : -1);
210 printf( " MaxJoins = %d\n\n", Conf_MaxJoins>0 ? Conf_MaxJoins : -1);
212 for( i = 0; i < Conf_Oper_Count; i++ ) {
213 if( ! Conf_Oper[i].name[0] ) continue;
215 /* Valid "Operator" section */
216 puts( "[OPERATOR]" );
217 printf( " Name = %s\n", Conf_Oper[i].name );
218 printf( " Password = %s\n", Conf_Oper[i].pwd );
219 if ( Conf_Oper[i].mask ) printf( " Mask = %s\n", Conf_Oper[i].mask );
223 for( i = 0; i < MAX_SERVERS; i++ ) {
224 if( ! Conf_Server[i].name[0] ) continue;
226 /* Valid "Server" section */
228 printf( " Name = %s\n", Conf_Server[i].name );
229 printf( " Host = %s\n", Conf_Server[i].host );
230 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
231 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
232 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
233 printf( " Group = %d\n", Conf_Server[i].group );
234 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
237 for( i = 0; i < Conf_Channel_Count; i++ ) {
238 if( ! Conf_Channel[i].name[0] ) continue;
240 /* Valid "Channel" section */
242 printf( " Name = %s\n", Conf_Channel[i].name );
243 printf( " Modes = %s\n", Conf_Channel[i].modes );
244 printf( " Key = %s\n", Conf_Channel[i].key );
245 printf( " MaxUsers = %lu\n", Conf_Channel[i].maxusers );
247 topic = (char*)array_start(&Conf_Channel[i].topic);
248 printf( " Topic = %s\n\n", topic ? topic : "");
256 Conf_UnsetServer( CONN_ID Idx )
258 /* Set next time for next connection attempt, if this is a server
259 * link that is (still) configured here. If the server is set as
260 * "once", delete it from our configuration.
261 * Non-Server-Connections will be silently ignored. */
266 /* Check all our configured servers */
267 for( i = 0; i < MAX_SERVERS; i++ ) {
268 if( Conf_Server[i].conn_id != Idx ) continue;
270 /* Gotcha! Mark server configuration as "unused": */
271 Conf_Server[i].conn_id = NONE;
273 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
274 /* Delete configuration here */
275 Init_Server_Struct( &Conf_Server[i] );
277 /* Set time for next connect attempt */
279 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
280 /* The connection has been "long", so we don't
281 * require the next attempt to be delayed. */
282 Conf_Server[i].lasttry =
283 t - Conf_ConnectRetry + RECONNECT_DELAY;
285 Conf_Server[i].lasttry = t;
288 } /* Conf_UnsetServer */
292 Conf_SetServer( int ConfServer, CONN_ID Idx )
294 /* Set connection for specified configured server */
296 assert( ConfServer > NONE );
297 assert( Idx > NONE );
299 Conf_Server[ConfServer].conn_id = Idx;
300 } /* Conf_SetServer */
304 Conf_GetServer( CONN_ID Idx )
306 /* Get index of server in configuration structure */
310 assert( Idx > NONE );
312 for( i = 0; i < MAX_SERVERS; i++ ) {
313 if( Conf_Server[i].conn_id == Idx ) return i;
316 } /* Conf_GetServer */
320 Conf_EnableServer( char *Name, UINT16 Port )
322 /* Enable specified server and adjust port */
326 assert( Name != NULL );
328 for( i = 0; i < MAX_SERVERS; i++ ) {
329 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
330 /* Gotcha! Set port and enable server: */
331 Conf_Server[i].port = Port;
332 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
337 } /* Conf_EnableServer */
341 Conf_EnablePassiveServer(const char *Name)
343 /* Enable specified server */
346 assert( Name != NULL );
347 for (i = 0; i < MAX_SERVERS; i++) {
348 if ((strcasecmp( Conf_Server[i].name, Name ) == 0) && (Conf_Server[i].port > 0)) {
349 /* BINGO! Enable server */
350 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
355 } /* Conf_EnablePassiveServer */
359 Conf_DisableServer( char *Name )
361 /* Enable specified server and adjust port */
365 assert( Name != NULL );
367 for( i = 0; i < MAX_SERVERS; i++ ) {
368 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
369 /* Gotcha! Disable and disconnect server: */
370 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
371 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
376 } /* Conf_DisableServer */
380 Conf_AddServer( char *Name, UINT16 Port, char *Host, char *MyPwd, char *PeerPwd )
382 /* Add new server to configuration */
386 assert( Name != NULL );
387 assert( Host != NULL );
388 assert( MyPwd != NULL );
389 assert( PeerPwd != NULL );
391 /* Search unused item in server configuration structure */
392 for( i = 0; i < MAX_SERVERS; i++ ) {
393 /* Is this item used? */
394 if( ! Conf_Server[i].name[0] ) break;
396 if( i >= MAX_SERVERS ) return false;
398 Init_Server_Struct( &Conf_Server[i] );
399 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
400 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
401 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
402 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
403 Conf_Server[i].port = Port;
404 Conf_Server[i].flags = CONF_SFLAG_ONCE;
407 } /* Conf_AddServer */
411 Set_Defaults( bool InitServers )
413 /* Initialize configuration variables with default values. */
417 strcpy( Conf_ServerName, "" );
418 snprintf( Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
419 strcpy( Conf_ServerPwd, "" );
421 strcpy( Conf_ServerAdmin1, "" );
422 strcpy( Conf_ServerAdmin2, "" );
423 strcpy( Conf_ServerAdminMail, "" );
425 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
426 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
428 strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
430 strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
432 strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
434 strcpy( Conf_ListenAddress, "" );
436 Conf_UID = Conf_GID = 0;
438 Conf_PingTimeout = 120;
439 Conf_PongTimeout = 20;
441 Conf_ConnectRetry = 60;
444 Conf_Channel_Count = 0;
446 Conf_OperCanMode = false;
447 Conf_PredefChannelsOnly = false;
448 Conf_OperServerMode = false;
450 Conf_MaxConnections = -1;
451 Conf_MaxConnectionsIP = 5;
454 /* Initialize server configuration structures */
455 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
462 /* Read configuration file. */
464 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
465 const UINT16 defaultport = 6667;
469 /* Open configuration file */
470 fd = fopen( NGIRCd_ConfFile, "r" );
472 /* No configuration file found! */
473 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
474 NGIRCd_ConfFile, strerror( errno ));
475 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
479 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
481 /* Clean up server configuration structure: mark all already
482 * configured servers as "once" so that they are deleted
483 * after the next disconnect and delete all unused servers.
484 * And delete all servers which are "duplicates" of servers
485 * that are already marked as "once" (such servers have been
486 * created by the last rehash but are now useless). */
487 for( i = 0; i < MAX_SERVERS; i++ ) {
488 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
490 /* This structure is in use ... */
491 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
492 /* Check for duplicates */
493 for( n = 0; n < MAX_SERVERS; n++ ) {
494 if( n == i ) continue;
496 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
497 Init_Server_Struct( &Conf_Server[n] );
499 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
505 /* Mark server as "once" */
506 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
507 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
512 /* Initialize variables */
514 strcpy( section, "" );
515 Init_Server_Struct( &New_Server );
516 New_Server_Idx = NONE;
518 /* Read configuration file */
520 if( ! fgets( str, LINE_LEN, fd )) break;
524 /* Skip comments and empty lines */
525 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
527 /* Is this the beginning of a new section? */
528 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
529 strlcpy( section, str, sizeof( section ));
530 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
533 if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
534 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
535 Config_Error( LOG_ERR, "Too many operators configured.");
537 /* Initialize new operator structure */
538 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
539 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
540 if (Conf_Oper[Conf_Oper_Count].mask) {
541 free(Conf_Oper[Conf_Oper_Count].mask );
542 Conf_Oper[Conf_Oper_Count].mask = NULL;
548 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
549 /* Check if there is already a server to add */
550 if( New_Server.name[0] ) {
551 /* Copy data to "real" server structure */
552 assert( New_Server_Idx > NONE );
553 Conf_Server[New_Server_Idx] = New_Server;
556 /* Re-init structure for new server */
557 Init_Server_Struct( &New_Server );
559 /* Search unused item in server configuration structure */
560 for( i = 0; i < MAX_SERVERS; i++ ) {
561 /* Is this item used? */
562 if( ! Conf_Server[i].name[0] ) break;
564 if( i >= MAX_SERVERS ) {
565 /* Oops, no free item found! */
566 Config_Error( LOG_ERR, "Too many servers configured." );
567 New_Server_Idx = NONE;
569 else New_Server_Idx = i;
572 if( strcasecmp( section, "[CHANNEL]" ) == 0 ) {
573 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) {
574 Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
576 /* Initialize new channel structure */
577 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
578 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
579 strcpy( Conf_Channel[Conf_Channel_Count].key, "" );
580 Conf_Channel[Conf_Channel_Count].maxusers = 0;
581 array_free(&Conf_Channel[Conf_Channel_Count].topic);
582 Conf_Channel_Count++;
586 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
589 if( section[0] == 0x1 ) continue;
591 /* Split line into variable name and parameters */
592 ptr = strchr( str, '=' );
594 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
598 var = str; ngt_TrimStr( var );
599 arg = ptr + 1; ngt_TrimStr( arg );
601 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
602 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
603 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
604 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
605 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
608 /* Close configuration file */
611 /* Check if there is still a server to add */
612 if( New_Server.name[0] ) {
613 /* Copy data to "real" server structure */
614 assert( New_Server_Idx > NONE );
615 Conf_Server[New_Server_Idx] = New_Server;
618 if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
619 if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
620 Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
621 (unsigned int) defaultport, strerror(errno));
629 Check_ArgIsTrue( const char *Arg )
631 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
632 if( strcasecmp( Arg, "true" ) == 0 ) return true;
633 if( atoi( Arg ) != 0 ) return true;
636 } /* Check_ArgIsTrue */
640 Handle_GLOBAL( int Line, char *Var, char *Arg )
647 assert( Var != NULL );
648 assert( Arg != NULL );
650 if( strcasecmp( Var, "Name" ) == 0 ) {
652 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
653 if (len >= sizeof( Conf_ServerName ))
654 Config_Error_TooLong( Line, Var );
657 if( strcasecmp( Var, "Info" ) == 0 ) {
658 /* Info text of server */
659 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
660 if (len >= sizeof( Conf_ServerInfo ))
661 Config_Error_TooLong ( Line, Var );
664 if( strcasecmp( Var, "Password" ) == 0 ) {
665 /* Global server password */
666 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
667 if (len >= sizeof( Conf_ServerPwd ))
668 Config_Error_TooLong( Line, Var );
671 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
672 /* Administrative info #1 */
673 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
674 if (len >= sizeof( Conf_ServerAdmin1 ))
675 Config_Error_TooLong ( Line, Var );
678 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
679 /* Administrative info #2 */
680 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
681 if (len >= sizeof( Conf_ServerAdmin2 ))
682 Config_Error_TooLong ( Line, Var );
685 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
686 /* Administrative email contact */
687 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
688 if (len >= sizeof( Conf_ServerAdminMail ))
689 Config_Error_TooLong( Line, Var );
693 if( strcasecmp( Var, "Ports" ) == 0 ) {
694 ports_parse(&Conf_ListenPorts, Line, Arg);
697 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
698 /* "Message of the day" (MOTD) file */
699 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
700 if (len >= sizeof( Conf_MotdFile ))
701 Config_Error_TooLong( Line, Var );
704 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
705 /* "Message of the day" phrase (instead of file) */
706 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
707 if (len >= sizeof( Conf_MotdPhrase ))
708 Config_Error_TooLong( Line, Var );
711 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
712 /* directory for chroot() */
713 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
714 if (len >= sizeof( Conf_Chroot ))
715 Config_Error_TooLong( Line, Var );
718 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
719 /* name of pidfile */
720 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
721 if (len >= sizeof( Conf_PidFile ))
722 Config_Error_TooLong( Line, Var );
725 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
726 /* UID the daemon should switch to */
727 pwd = getpwnam( Arg );
728 if( pwd ) Conf_UID = pwd->pw_uid;
731 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
734 Conf_UID = (unsigned int)atoi( Arg );
738 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
739 /* GID the daemon should use */
740 grp = getgrnam( Arg );
741 if( grp ) Conf_GID = grp->gr_gid;
744 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
747 Conf_GID = (unsigned int)atoi( Arg );
751 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
753 Conf_PingTimeout = atoi( Arg );
754 if( Conf_PingTimeout < 5 ) {
755 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
756 NGIRCd_ConfFile, Line );
757 Conf_PingTimeout = 5;
761 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
763 Conf_PongTimeout = atoi( Arg );
764 if( Conf_PongTimeout < 5 ) {
765 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
766 NGIRCd_ConfFile, Line );
767 Conf_PongTimeout = 5;
771 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
772 /* Seconds between connection attempts to other servers */
773 Conf_ConnectRetry = atoi( Arg );
774 if( Conf_ConnectRetry < 5 ) {
775 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
776 NGIRCd_ConfFile, Line );
777 Conf_ConnectRetry = 5;
781 if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
782 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
783 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
786 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
787 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
788 Conf_OperCanMode = Check_ArgIsTrue( Arg );
791 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
792 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
793 Conf_OperServerMode = Check_ArgIsTrue( Arg );
796 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
797 /* Maximum number of connections. 0 -> "no limit". */
799 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
802 Conf_MaxConnections = atol( Arg );
805 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
806 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
808 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
811 Conf_MaxConnectionsIP = atoi( Arg );
814 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
815 /* Maximum number of channels a user can join. 0 -> "no limit". */
817 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
820 Conf_MaxJoins = atoi( Arg );
823 if( strcasecmp( Var, "Listen" ) == 0 ) {
824 /* IP-Address to bind sockets */
825 len = strlcpy( Conf_ListenAddress, Arg, sizeof( Conf_ListenAddress ));
826 if (len >= sizeof( Conf_ListenAddress ))
827 Config_Error_TooLong( Line, Var );
831 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
832 NGIRCd_ConfFile, Line, Var );
833 } /* Handle_GLOBAL */
837 Handle_OPERATOR( int Line, char *Var, char *Arg )
839 unsigned int opercount;
842 assert( Var != NULL );
843 assert( Arg != NULL );
844 assert( Conf_Oper_Count > 0 );
846 if ( Conf_Oper_Count == 0 )
849 opercount = Conf_Oper_Count - 1;
851 if( strcasecmp( Var, "Name" ) == 0 ) {
852 /* Name of IRC operator */
853 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
854 if (len >= sizeof( Conf_Oper[opercount].name ))
855 Config_Error_TooLong( Line, Var );
858 if( strcasecmp( Var, "Password" ) == 0 ) {
859 /* Password of IRC operator */
860 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
861 if (len >= sizeof( Conf_Oper[opercount].pwd ))
862 Config_Error_TooLong( Line, Var );
865 if( strcasecmp( Var, "Mask" ) == 0 ) {
866 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
868 Conf_Oper[opercount].mask = strdup_warn( Arg );
871 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
872 NGIRCd_ConfFile, Line, Var );
873 } /* Handle_OPERATOR */
877 Handle_SERVER( int Line, char *Var, char *Arg )
883 assert( Var != NULL );
884 assert( Arg != NULL );
886 /* Ignore server block if no space is left in server configuration structure */
887 if( New_Server_Idx <= NONE ) return;
889 if( strcasecmp( Var, "Host" ) == 0 ) {
890 /* Hostname of the server */
891 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
892 if (len >= sizeof( New_Server.host ))
893 Config_Error_TooLong ( Line, Var );
896 if( strcasecmp( Var, "Name" ) == 0 ) {
897 /* Name of the server ("Nick"/"ID") */
898 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
899 if (len >= sizeof( New_Server.name ))
900 Config_Error_TooLong( Line, Var );
903 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
904 /* Password of this server which is sent to the peer */
906 Config_Error(LOG_ERR,
907 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
908 NGIRCd_ConfFile, Line);
910 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
911 if (len >= sizeof( New_Server.pwd_in ))
912 Config_Error_TooLong( Line, Var );
915 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
916 /* Passwort of the peer which must be received */
917 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
918 if (len >= sizeof( New_Server.pwd_out ))
919 Config_Error_TooLong( Line, Var );
922 if( strcasecmp( Var, "Port" ) == 0 ) {
923 /* Port to which this server should connect */
925 if( port > 0 && port < 0xFFFF )
926 New_Server.port = (UINT16)port;
928 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
929 NGIRCd_ConfFile, Line, port );
932 if( strcasecmp( Var, "Group" ) == 0 ) {
935 if( ! isdigit( (int)*Arg ))
936 Config_Error_NaN( Line, Var );
939 New_Server.group = atoi( Arg );
942 if( strcasecmp( Var, "Passive" ) == 0 ) {
943 if (Check_ArgIsTrue(Arg))
944 New_Server.flags |= CONF_SFLAG_DISABLED;
948 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
949 NGIRCd_ConfFile, Line, Var );
950 } /* Handle_SERVER */
954 Handle_Channelname(size_t chancount, const char *name)
956 size_t size = sizeof( Conf_Channel[chancount].name );
957 char *dest = Conf_Channel[chancount].name;
959 if (*name && *name != '#') {
964 return size > strlcpy(dest, name, size);
969 Handle_CHANNEL( int Line, char *Var, char *Arg )
972 size_t chancount = 0;
975 assert( Var != NULL );
976 assert( Arg != NULL );
977 if (Conf_Channel_Count > 0)
978 chancount = Conf_Channel_Count - 1;
980 if( strcasecmp( Var, "Name" ) == 0 ) {
981 if (!Handle_Channelname(chancount, Arg))
982 Config_Error_TooLong( Line, Var );
985 if( strcasecmp( Var, "Modes" ) == 0 ) {
987 len = strlcpy( Conf_Channel[chancount].modes, Arg, sizeof( Conf_Channel[chancount].modes ));
988 if (len >= sizeof( Conf_Channel[chancount].modes ))
989 Config_Error_TooLong( Line, Var );
992 if( strcasecmp( Var, "Topic" ) == 0 ) {
994 if (!array_copys( &Conf_Channel[chancount].topic, Arg))
995 Config_Error_TooLong( Line, Var );
999 if( strcasecmp( Var, "Key" ) == 0 ) {
1000 /* Initial Channel Key (mode k) */
1001 len = strlcpy(Conf_Channel[chancount].key, Arg, sizeof(Conf_Channel[chancount].key));
1002 if (len >= sizeof( Conf_Channel[chancount].key ))
1003 Config_Error_TooLong(Line, Var);
1007 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1008 /* maximum user limit, mode l */
1009 Conf_Channel[chancount].maxusers = (unsigned long) atol(Arg);
1010 if (Conf_Channel[chancount].maxusers == 0)
1011 Config_Error_NaN(Line, Var);
1015 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1016 NGIRCd_ConfFile, Line, Var );
1017 } /* Handle_CHANNEL */
1021 Validate_Config(bool Configtest, bool Rehash)
1023 /* Validate configuration settings. */
1026 int i, servers, servers_once;
1030 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1031 ptr = Conf_ServerName;
1033 if (*ptr >= 'a' && *ptr <= 'z') continue;
1034 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1035 if (*ptr >= '0' && *ptr <= '9') continue;
1036 if (ptr > Conf_ServerName) {
1037 if (*ptr == '.' || *ptr == '-')
1040 Conf_ServerName[0] = '\0';
1044 if (!Conf_ServerName[0]) {
1045 /* No server name configured! */
1046 Config_Error(LOG_ALERT,
1047 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1049 if (!Configtest && !Rehash) {
1050 Config_Error(LOG_ALERT,
1051 "%s exiting due to fatal errors!",
1057 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1058 /* No dot in server name! */
1059 Config_Error(LOG_ALERT,
1060 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1063 Config_Error(LOG_ALERT,
1064 "%s exiting due to fatal errors!",
1071 if (!Conf_ServerAdminMail[0]) {
1072 /* No administrative contact configured! */
1073 Config_Error(LOG_ALERT,
1074 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1077 Config_Error(LOG_ALERT,
1078 "%s exiting due to fatal errors!",
1085 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1086 && !Conf_ServerAdminMail[0]) {
1087 /* No administrative information configured! */
1088 Config_Error(LOG_WARNING,
1089 "No administrative information configured but required by RFC!");
1093 servers = servers_once = 0;
1094 for (i = 0; i < MAX_SERVERS; i++) {
1095 if (Conf_Server[i].name[0]) {
1097 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1102 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1103 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1105 } /* Validate_Config */
1109 Config_Error_TooLong ( const int Line, const char *Item )
1111 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1116 Config_Error_NaN( const int Line, const char *Item )
1118 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1119 NGIRCd_ConfFile, Line, Item );
1124 static void Config_Error( const int Level, const char *Format, ... )
1126 static void Config_Error( Level, Format, va_alist )
1132 /* Error! Write to console and/or logfile. */
1134 char msg[MAX_LOG_MSG_LEN];
1137 assert( Format != NULL );
1140 va_start( ap, Format );
1144 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1147 /* During "normal operations" the log functions of the daemon should
1148 * be used, but during testing of the configuration file, all messages
1149 * should go directly to the console: */
1150 if (Use_Log) Log( Level, "%s", msg );
1152 } /* Config_Error */
1156 Init_Server_Struct( CONF_SERVER *Server )
1158 /* Initialize server configuration structur to default values */
1160 assert( Server != NULL );
1162 memset( Server, 0, sizeof (CONF_SERVER) );
1164 Server->group = NONE;
1165 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1167 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1169 Resolve_Init(&Server->res_stat);
1170 Server->conn_id = NONE;
1171 } /* Init_Server_Struct */