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.105 2008/03/18 20:12:47 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 bool Read_Config PARAMS(( bool ngircd_starting ));
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, "," );
138 Validate_Config(false, false);
145 if (!Read_Config(false))
147 Validate_Config(false, true);
149 /* Update CLIENT structure of local server */
150 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
152 } /* Config_Rehash */
158 /* Read configuration, validate and output it. */
168 Validate_Config(true, false);
170 /* If stdin and stdout ("you can read our nice message and we can
171 * read in your keypress") are valid tty's, wait for a key: */
172 if( isatty( fileno( stdin )) && isatty( fileno( stdout ))) {
173 puts( "OK, press enter to see a dump of your service configuration ..." );
176 puts( "Ok, dump of your server configuration follows:\n" );
180 printf( " Name = %s\n", Conf_ServerName );
181 printf( " Info = %s\n", Conf_ServerInfo );
182 printf( " Password = %s\n", Conf_ServerPwd );
183 printf( " AdminInfo1 = %s\n", Conf_ServerAdmin1 );
184 printf( " AdminInfo2 = %s\n", Conf_ServerAdmin2 );
185 printf( " AdminEMail = %s\n", Conf_ServerAdminMail );
186 printf( " MotdFile = %s\n", Conf_MotdFile );
187 printf( " MotdPhrase = %s\n", Conf_MotdPhrase );
188 printf( " ChrootDir = %s\n", Conf_Chroot );
189 printf( " PidFile = %s\n", Conf_PidFile);
190 fputs(" Ports = ", stdout);
192 ports_puts(&Conf_ListenPorts);
194 printf( " Listen = %s\n", Conf_ListenAddress );
195 pwd = getpwuid( Conf_UID );
196 if( pwd ) printf( " ServerUID = %s\n", pwd->pw_name );
197 else printf( " ServerUID = %ld\n", (long)Conf_UID );
198 grp = getgrgid( Conf_GID );
199 if( grp ) printf( " ServerGID = %s\n", grp->gr_name );
200 else printf( " ServerGID = %ld\n", (long)Conf_GID );
201 printf( " PingTimeout = %d\n", Conf_PingTimeout );
202 printf( " PongTimeout = %d\n", Conf_PongTimeout );
203 printf( " ConnectRetry = %d\n", Conf_ConnectRetry );
204 printf( " OperCanUseMode = %s\n", Conf_OperCanMode == true ? "yes" : "no" );
205 printf( " OperServerMode = %s\n", Conf_OperServerMode == true? "yes" : "no" );
206 printf( " PredefChannelsOnly = %s\n", Conf_PredefChannelsOnly == true ? "yes" : "no" );
207 printf( " NoDNS = %s\n", Conf_NoDNS ? "yes" : "no");
208 printf( " MaxConnections = %ld\n", Conf_MaxConnections);
209 printf( " MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
210 printf( " MaxJoins = %d\n", Conf_MaxJoins>0 ? Conf_MaxJoins : -1);
211 printf( " MaxNickLength = %u\n\n", Conf_MaxNickLength - 1);
213 for( i = 0; i < Conf_Oper_Count; i++ ) {
214 if( ! Conf_Oper[i].name[0] ) continue;
216 /* Valid "Operator" section */
217 puts( "[OPERATOR]" );
218 printf( " Name = %s\n", Conf_Oper[i].name );
219 printf( " Password = %s\n", Conf_Oper[i].pwd );
220 if ( Conf_Oper[i].mask ) printf( " Mask = %s\n", Conf_Oper[i].mask );
224 for( i = 0; i < MAX_SERVERS; i++ ) {
225 if( ! Conf_Server[i].name[0] ) continue;
227 /* Valid "Server" section */
229 printf( " Name = %s\n", Conf_Server[i].name );
230 printf( " Host = %s\n", Conf_Server[i].host );
231 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
232 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
233 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
234 printf( " Group = %d\n", Conf_Server[i].group );
235 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
238 for( i = 0; i < Conf_Channel_Count; i++ ) {
239 if( ! Conf_Channel[i].name[0] ) continue;
241 /* Valid "Channel" section */
243 printf( " Name = %s\n", Conf_Channel[i].name );
244 printf( " Modes = %s\n", Conf_Channel[i].modes );
245 printf( " Key = %s\n", Conf_Channel[i].key );
246 printf( " MaxUsers = %lu\n", Conf_Channel[i].maxusers );
248 topic = (char*)array_start(&Conf_Channel[i].topic);
249 printf( " Topic = %s\n\n", topic ? topic : "");
257 Conf_UnsetServer( CONN_ID Idx )
259 /* Set next time for next connection attempt, if this is a server
260 * link that is (still) configured here. If the server is set as
261 * "once", delete it from our configuration.
262 * Non-Server-Connections will be silently ignored. */
267 /* Check all our configured servers */
268 for( i = 0; i < MAX_SERVERS; i++ ) {
269 if( Conf_Server[i].conn_id != Idx ) continue;
271 /* Gotcha! Mark server configuration as "unused": */
272 Conf_Server[i].conn_id = NONE;
274 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
275 /* Delete configuration here */
276 Init_Server_Struct( &Conf_Server[i] );
278 /* Set time for next connect attempt */
280 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
281 /* The connection has been "long", so we don't
282 * require the next attempt to be delayed. */
283 Conf_Server[i].lasttry =
284 t - Conf_ConnectRetry + RECONNECT_DELAY;
286 Conf_Server[i].lasttry = t;
289 } /* Conf_UnsetServer */
293 Conf_SetServer( int ConfServer, CONN_ID Idx )
295 /* Set connection for specified configured server */
297 assert( ConfServer > NONE );
298 assert( Idx > NONE );
300 Conf_Server[ConfServer].conn_id = Idx;
301 } /* Conf_SetServer */
305 Conf_GetServer( CONN_ID Idx )
307 /* Get index of server in configuration structure */
311 assert( Idx > NONE );
313 for( i = 0; i < MAX_SERVERS; i++ ) {
314 if( Conf_Server[i].conn_id == Idx ) return i;
317 } /* Conf_GetServer */
321 Conf_EnableServer( char *Name, UINT16 Port )
323 /* Enable specified server and adjust port */
327 assert( Name != NULL );
329 for( i = 0; i < MAX_SERVERS; i++ ) {
330 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
331 /* Gotcha! Set port and enable server: */
332 Conf_Server[i].port = Port;
333 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
338 } /* Conf_EnableServer */
342 Conf_EnablePassiveServer(const char *Name)
344 /* Enable specified server */
347 assert( Name != NULL );
348 for (i = 0; i < MAX_SERVERS; i++) {
349 if ((strcasecmp( Conf_Server[i].name, Name ) == 0) && (Conf_Server[i].port > 0)) {
350 /* BINGO! Enable server */
351 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
356 } /* Conf_EnablePassiveServer */
360 Conf_DisableServer( char *Name )
362 /* Enable specified server and adjust port */
366 assert( Name != NULL );
368 for( i = 0; i < MAX_SERVERS; i++ ) {
369 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
370 /* Gotcha! Disable and disconnect server: */
371 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
372 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
377 } /* Conf_DisableServer */
381 Conf_AddServer( char *Name, UINT16 Port, char *Host, char *MyPwd, char *PeerPwd )
383 /* Add new server to configuration */
387 assert( Name != NULL );
388 assert( Host != NULL );
389 assert( MyPwd != NULL );
390 assert( PeerPwd != NULL );
392 /* Search unused item in server configuration structure */
393 for( i = 0; i < MAX_SERVERS; i++ ) {
394 /* Is this item used? */
395 if( ! Conf_Server[i].name[0] ) break;
397 if( i >= MAX_SERVERS ) return false;
399 Init_Server_Struct( &Conf_Server[i] );
400 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
401 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
402 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
403 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
404 Conf_Server[i].port = Port;
405 Conf_Server[i].flags = CONF_SFLAG_ONCE;
408 } /* Conf_AddServer */
412 Set_Defaults( bool InitServers )
414 /* Initialize configuration variables with default values. */
418 strcpy( Conf_ServerName, "" );
419 snprintf( Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
420 strcpy( Conf_ServerPwd, "" );
422 strcpy( Conf_ServerAdmin1, "" );
423 strcpy( Conf_ServerAdmin2, "" );
424 strcpy( Conf_ServerAdminMail, "" );
426 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
427 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
429 strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
431 strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
433 strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
435 strcpy( Conf_ListenAddress, "" );
437 Conf_UID = Conf_GID = 0;
439 Conf_PingTimeout = 120;
440 Conf_PongTimeout = 20;
442 Conf_ConnectRetry = 60;
445 Conf_Channel_Count = 0;
447 Conf_OperCanMode = false;
449 Conf_PredefChannelsOnly = false;
450 Conf_OperServerMode = false;
452 Conf_MaxConnections = 0;
453 Conf_MaxConnectionsIP = 5;
455 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
457 /* Initialize server configuration structures */
458 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
463 Read_Config( bool ngircd_starting )
465 /* Read configuration file. */
467 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
468 const UINT16 defaultport = 6667;
472 /* Open configuration file */
473 fd = fopen( NGIRCd_ConfFile, "r" );
475 /* No configuration file found! */
476 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
477 NGIRCd_ConfFile, strerror( errno ));
478 if (!ngircd_starting)
480 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
484 Set_Defaults( ngircd_starting );
486 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
488 /* Clean up server configuration structure: mark all already
489 * configured servers as "once" so that they are deleted
490 * after the next disconnect and delete all unused servers.
491 * And delete all servers which are "duplicates" of servers
492 * that are already marked as "once" (such servers have been
493 * created by the last rehash but are now useless). */
494 for( i = 0; i < MAX_SERVERS; i++ ) {
495 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
497 /* This structure is in use ... */
498 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
499 /* Check for duplicates */
500 for( n = 0; n < MAX_SERVERS; n++ ) {
501 if( n == i ) continue;
503 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
504 Init_Server_Struct( &Conf_Server[n] );
506 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
512 /* Mark server as "once" */
513 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
514 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
519 /* Initialize variables */
521 strcpy( section, "" );
522 Init_Server_Struct( &New_Server );
523 New_Server_Idx = NONE;
525 /* Read configuration file */
527 if( ! fgets( str, LINE_LEN, fd )) break;
531 /* Skip comments and empty lines */
532 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
534 /* Is this the beginning of a new section? */
535 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
536 strlcpy( section, str, sizeof( section ));
537 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
540 if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
541 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
542 Config_Error( LOG_ERR, "Too many operators configured.");
544 /* Initialize new operator structure */
545 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
546 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
547 if (Conf_Oper[Conf_Oper_Count].mask) {
548 free(Conf_Oper[Conf_Oper_Count].mask );
549 Conf_Oper[Conf_Oper_Count].mask = NULL;
555 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
556 /* Check if there is already a server to add */
557 if( New_Server.name[0] ) {
558 /* Copy data to "real" server structure */
559 assert( New_Server_Idx > NONE );
560 Conf_Server[New_Server_Idx] = New_Server;
563 /* Re-init structure for new server */
564 Init_Server_Struct( &New_Server );
566 /* Search unused item in server configuration structure */
567 for( i = 0; i < MAX_SERVERS; i++ ) {
568 /* Is this item used? */
569 if( ! Conf_Server[i].name[0] ) break;
571 if( i >= MAX_SERVERS ) {
572 /* Oops, no free item found! */
573 Config_Error( LOG_ERR, "Too many servers configured." );
574 New_Server_Idx = NONE;
576 else New_Server_Idx = i;
579 if( strcasecmp( section, "[CHANNEL]" ) == 0 ) {
580 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) {
581 Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
583 /* Initialize new channel structure */
584 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
585 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
586 strcpy( Conf_Channel[Conf_Channel_Count].key, "" );
587 Conf_Channel[Conf_Channel_Count].maxusers = 0;
588 array_free(&Conf_Channel[Conf_Channel_Count].topic);
589 Conf_Channel_Count++;
593 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
596 if( section[0] == 0x1 ) continue;
598 /* Split line into variable name and parameters */
599 ptr = strchr( str, '=' );
601 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
605 var = str; ngt_TrimStr( var );
606 arg = ptr + 1; ngt_TrimStr( arg );
608 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
609 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
610 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
611 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
612 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
615 /* Close configuration file */
618 /* Check if there is still a server to add */
619 if( New_Server.name[0] ) {
620 /* Copy data to "real" server structure */
621 assert( New_Server_Idx > NONE );
622 Conf_Server[New_Server_Idx] = New_Server;
625 if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
626 if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
627 Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
628 (unsigned int) defaultport, strerror(errno));
637 Check_ArgIsTrue( const char *Arg )
639 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
640 if( strcasecmp( Arg, "true" ) == 0 ) return true;
641 if( atoi( Arg ) != 0 ) return true;
644 } /* Check_ArgIsTrue */
647 static unsigned int Handle_MaxNickLength(int Line, const char *Arg)
651 new = (unsigned) atoi(Arg) + 1;
652 if (new > CLIENT_NICK_LEN) {
653 Config_Error(LOG_WARNING,
654 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
655 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
656 return CLIENT_NICK_LEN;
659 Config_Error(LOG_WARNING,
660 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
661 NGIRCd_ConfFile, Line);
665 } /* Handle_MaxNickLength */
669 Handle_GLOBAL( int Line, char *Var, char *Arg )
676 assert( Var != NULL );
677 assert( Arg != NULL );
679 if( strcasecmp( Var, "Name" ) == 0 ) {
681 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
682 if (len >= sizeof( Conf_ServerName ))
683 Config_Error_TooLong( Line, Var );
686 if( strcasecmp( Var, "Info" ) == 0 ) {
687 /* Info text of server */
688 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
689 if (len >= sizeof( Conf_ServerInfo ))
690 Config_Error_TooLong ( Line, Var );
693 if( strcasecmp( Var, "Password" ) == 0 ) {
694 /* Global server password */
695 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
696 if (len >= sizeof( Conf_ServerPwd ))
697 Config_Error_TooLong( Line, Var );
700 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
701 /* Administrative info #1 */
702 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
703 if (len >= sizeof( Conf_ServerAdmin1 ))
704 Config_Error_TooLong ( Line, Var );
707 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
708 /* Administrative info #2 */
709 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
710 if (len >= sizeof( Conf_ServerAdmin2 ))
711 Config_Error_TooLong ( Line, Var );
714 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
715 /* Administrative email contact */
716 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
717 if (len >= sizeof( Conf_ServerAdminMail ))
718 Config_Error_TooLong( Line, Var );
722 if( strcasecmp( Var, "Ports" ) == 0 ) {
723 ports_parse(&Conf_ListenPorts, Line, Arg);
726 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
727 /* "Message of the day" (MOTD) file */
728 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
729 if (len >= sizeof( Conf_MotdFile ))
730 Config_Error_TooLong( Line, Var );
733 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
734 /* "Message of the day" phrase (instead of file) */
735 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
736 if (len >= sizeof( Conf_MotdPhrase ))
737 Config_Error_TooLong( Line, Var );
740 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
741 /* directory for chroot() */
742 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
743 if (len >= sizeof( Conf_Chroot ))
744 Config_Error_TooLong( Line, Var );
747 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
748 /* name of pidfile */
749 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
750 if (len >= sizeof( Conf_PidFile ))
751 Config_Error_TooLong( Line, Var );
754 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
755 /* UID the daemon should switch to */
756 pwd = getpwnam( Arg );
757 if( pwd ) Conf_UID = pwd->pw_uid;
760 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
763 Conf_UID = (unsigned int)atoi( Arg );
767 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
768 /* GID the daemon should use */
769 grp = getgrnam( Arg );
770 if( grp ) Conf_GID = grp->gr_gid;
773 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
776 Conf_GID = (unsigned int)atoi( Arg );
780 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
782 Conf_PingTimeout = atoi( Arg );
783 if( Conf_PingTimeout < 5 ) {
784 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
785 NGIRCd_ConfFile, Line );
786 Conf_PingTimeout = 5;
790 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
792 Conf_PongTimeout = atoi( Arg );
793 if( Conf_PongTimeout < 5 ) {
794 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
795 NGIRCd_ConfFile, Line );
796 Conf_PongTimeout = 5;
800 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
801 /* Seconds between connection attempts to other servers */
802 Conf_ConnectRetry = atoi( Arg );
803 if( Conf_ConnectRetry < 5 ) {
804 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
805 NGIRCd_ConfFile, Line );
806 Conf_ConnectRetry = 5;
810 if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
811 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
812 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
815 if( strcasecmp( Var, "NoDNS" ) == 0 ) {
816 /* don't do reverse dns lookups when clients connect? */
817 Conf_NoDNS = Check_ArgIsTrue( Arg );
820 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
821 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
822 Conf_OperCanMode = Check_ArgIsTrue( Arg );
825 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
826 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
827 Conf_OperServerMode = Check_ArgIsTrue( Arg );
830 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
831 /* Maximum number of connections. 0 -> "no limit". */
833 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
836 Conf_MaxConnections = atol( Arg );
839 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
840 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
842 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
845 Conf_MaxConnectionsIP = atoi( Arg );
848 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
849 /* Maximum number of channels a user can join. 0 -> "no limit". */
851 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
854 Conf_MaxJoins = atoi( Arg );
857 if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
858 /* Maximum length of a nick name; must be same on all servers
859 * within the IRC network! */
860 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
864 if( strcasecmp( Var, "Listen" ) == 0 ) {
865 /* IP-Address to bind sockets */
866 len = strlcpy( Conf_ListenAddress, Arg, sizeof( Conf_ListenAddress ));
867 if (len >= sizeof( Conf_ListenAddress ))
868 Config_Error_TooLong( Line, Var );
872 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
873 NGIRCd_ConfFile, Line, Var );
874 } /* Handle_GLOBAL */
878 Handle_OPERATOR( int Line, char *Var, char *Arg )
880 unsigned int opercount;
883 assert( Var != NULL );
884 assert( Arg != NULL );
885 assert( Conf_Oper_Count > 0 );
887 if ( Conf_Oper_Count == 0 )
890 opercount = Conf_Oper_Count - 1;
892 if( strcasecmp( Var, "Name" ) == 0 ) {
893 /* Name of IRC operator */
894 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
895 if (len >= sizeof( Conf_Oper[opercount].name ))
896 Config_Error_TooLong( Line, Var );
899 if( strcasecmp( Var, "Password" ) == 0 ) {
900 /* Password of IRC operator */
901 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
902 if (len >= sizeof( Conf_Oper[opercount].pwd ))
903 Config_Error_TooLong( Line, Var );
906 if( strcasecmp( Var, "Mask" ) == 0 ) {
907 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
909 Conf_Oper[opercount].mask = strdup_warn( Arg );
912 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
913 NGIRCd_ConfFile, Line, Var );
914 } /* Handle_OPERATOR */
918 Handle_SERVER( int Line, char *Var, char *Arg )
924 assert( Var != NULL );
925 assert( Arg != NULL );
927 /* Ignore server block if no space is left in server configuration structure */
928 if( New_Server_Idx <= NONE ) return;
930 if( strcasecmp( Var, "Host" ) == 0 ) {
931 /* Hostname of the server */
932 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
933 if (len >= sizeof( New_Server.host ))
934 Config_Error_TooLong ( Line, Var );
937 if( strcasecmp( Var, "Name" ) == 0 ) {
938 /* Name of the server ("Nick"/"ID") */
939 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
940 if (len >= sizeof( New_Server.name ))
941 Config_Error_TooLong( Line, Var );
944 if (strcasecmp(Var, "Bind") == 0) {
945 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
948 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
949 NGIRCd_ConfFile, Line, Arg);
952 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
953 /* Password of this server which is sent to the peer */
955 Config_Error(LOG_ERR,
956 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
957 NGIRCd_ConfFile, Line);
959 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
960 if (len >= sizeof( New_Server.pwd_in ))
961 Config_Error_TooLong( Line, Var );
964 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
965 /* Passwort of the peer which must be received */
966 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
967 if (len >= sizeof( New_Server.pwd_out ))
968 Config_Error_TooLong( Line, Var );
971 if( strcasecmp( Var, "Port" ) == 0 ) {
972 /* Port to which this server should connect */
974 if( port > 0 && port < 0xFFFF )
975 New_Server.port = (UINT16)port;
977 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
978 NGIRCd_ConfFile, Line, port );
981 if( strcasecmp( Var, "Group" ) == 0 ) {
984 if( ! isdigit( (int)*Arg ))
985 Config_Error_NaN( Line, Var );
988 New_Server.group = atoi( Arg );
991 if( strcasecmp( Var, "Passive" ) == 0 ) {
992 if (Check_ArgIsTrue(Arg))
993 New_Server.flags |= CONF_SFLAG_DISABLED;
997 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
998 NGIRCd_ConfFile, Line, Var );
999 } /* Handle_SERVER */
1003 Handle_Channelname(size_t chancount, const char *name)
1005 size_t size = sizeof( Conf_Channel[chancount].name );
1006 char *dest = Conf_Channel[chancount].name;
1008 if (*name && *name != '#') {
1013 return size > strlcpy(dest, name, size);
1018 Handle_CHANNEL( int Line, char *Var, char *Arg )
1021 size_t chancount = 0;
1024 assert( Var != NULL );
1025 assert( Arg != NULL );
1026 if (Conf_Channel_Count > 0)
1027 chancount = Conf_Channel_Count - 1;
1029 if( strcasecmp( Var, "Name" ) == 0 ) {
1030 if (!Handle_Channelname(chancount, Arg))
1031 Config_Error_TooLong( Line, Var );
1034 if( strcasecmp( Var, "Modes" ) == 0 ) {
1036 len = strlcpy( Conf_Channel[chancount].modes, Arg, sizeof( Conf_Channel[chancount].modes ));
1037 if (len >= sizeof( Conf_Channel[chancount].modes ))
1038 Config_Error_TooLong( Line, Var );
1041 if( strcasecmp( Var, "Topic" ) == 0 ) {
1043 if (!array_copys( &Conf_Channel[chancount].topic, Arg))
1044 Config_Error_TooLong( Line, Var );
1048 if( strcasecmp( Var, "Key" ) == 0 ) {
1049 /* Initial Channel Key (mode k) */
1050 len = strlcpy(Conf_Channel[chancount].key, Arg, sizeof(Conf_Channel[chancount].key));
1051 if (len >= sizeof( Conf_Channel[chancount].key ))
1052 Config_Error_TooLong(Line, Var);
1056 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1057 /* maximum user limit, mode l */
1058 Conf_Channel[chancount].maxusers = (unsigned long) atol(Arg);
1059 if (Conf_Channel[chancount].maxusers == 0)
1060 Config_Error_NaN(Line, Var);
1064 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1065 NGIRCd_ConfFile, Line, Var );
1066 } /* Handle_CHANNEL */
1070 Validate_Config(bool Configtest, bool Rehash)
1072 /* Validate configuration settings. */
1075 int i, servers, servers_once;
1079 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1080 ptr = Conf_ServerName;
1082 if (*ptr >= 'a' && *ptr <= 'z') continue;
1083 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1084 if (*ptr >= '0' && *ptr <= '9') continue;
1085 if (ptr > Conf_ServerName) {
1086 if (*ptr == '.' || *ptr == '-')
1089 Conf_ServerName[0] = '\0';
1093 if (!Conf_ServerName[0]) {
1094 /* No server name configured! */
1095 Config_Error(LOG_ALERT,
1096 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1098 if (!Configtest && !Rehash) {
1099 Config_Error(LOG_ALERT,
1100 "%s exiting due to fatal errors!",
1106 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1107 /* No dot in server name! */
1108 Config_Error(LOG_ALERT,
1109 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1112 Config_Error(LOG_ALERT,
1113 "%s exiting due to fatal errors!",
1120 if (!Conf_ServerAdminMail[0]) {
1121 /* No administrative contact configured! */
1122 Config_Error(LOG_ALERT,
1123 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1126 Config_Error(LOG_ALERT,
1127 "%s exiting due to fatal errors!",
1134 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1135 && !Conf_ServerAdminMail[0]) {
1136 /* No administrative information configured! */
1137 Config_Error(LOG_WARNING,
1138 "No administrative information configured but required by RFC!");
1142 servers = servers_once = 0;
1143 for (i = 0; i < MAX_SERVERS; i++) {
1144 if (Conf_Server[i].name[0]) {
1146 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1151 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1152 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1154 } /* Validate_Config */
1158 Config_Error_TooLong ( const int Line, const char *Item )
1160 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1165 Config_Error_NaN( const int Line, const char *Item )
1167 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1168 NGIRCd_ConfFile, Line, Item );
1173 static void Config_Error( const int Level, const char *Format, ... )
1175 static void Config_Error( Level, Format, va_alist )
1181 /* Error! Write to console and/or logfile. */
1183 char msg[MAX_LOG_MSG_LEN];
1186 assert( Format != NULL );
1189 va_start( ap, Format );
1193 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1196 /* During "normal operations" the log functions of the daemon should
1197 * be used, but during testing of the configuration file, all messages
1198 * should go directly to the console: */
1199 if (Use_Log) Log( Level, "%s", msg );
1201 } /* Config_Error */
1205 Init_Server_Struct( CONF_SERVER *Server )
1207 /* Initialize server configuration structur to default values */
1209 assert( Server != NULL );
1211 memset( Server, 0, sizeof (CONF_SERVER) );
1213 Server->group = NONE;
1214 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1216 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1218 Resolve_Init(&Server->res_stat);
1219 Server->conn_id = NONE;
1220 memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
1221 } /* Init_Server_Struct */