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.104 2008/02/26 22:04:17 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( " NoDNS = %s\n", Conf_NoDNS ? "yes" : "no");
209 printf( " MaxConnections = %ld\n", Conf_MaxConnections);
210 printf( " MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
211 printf( " MaxJoins = %d\n", Conf_MaxJoins>0 ? Conf_MaxJoins : -1);
212 printf( " MaxNickLength = %u\n\n", Conf_MaxNickLength - 1);
214 for( i = 0; i < Conf_Oper_Count; i++ ) {
215 if( ! Conf_Oper[i].name[0] ) continue;
217 /* Valid "Operator" section */
218 puts( "[OPERATOR]" );
219 printf( " Name = %s\n", Conf_Oper[i].name );
220 printf( " Password = %s\n", Conf_Oper[i].pwd );
221 if ( Conf_Oper[i].mask ) printf( " Mask = %s\n", Conf_Oper[i].mask );
225 for( i = 0; i < MAX_SERVERS; i++ ) {
226 if( ! Conf_Server[i].name[0] ) continue;
228 /* Valid "Server" section */
230 printf( " Name = %s\n", Conf_Server[i].name );
231 printf( " Host = %s\n", Conf_Server[i].host );
232 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
233 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
234 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
235 printf( " Group = %d\n", Conf_Server[i].group );
236 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
239 for( i = 0; i < Conf_Channel_Count; i++ ) {
240 if( ! Conf_Channel[i].name[0] ) continue;
242 /* Valid "Channel" section */
244 printf( " Name = %s\n", Conf_Channel[i].name );
245 printf( " Modes = %s\n", Conf_Channel[i].modes );
246 printf( " Key = %s\n", Conf_Channel[i].key );
247 printf( " MaxUsers = %lu\n", Conf_Channel[i].maxusers );
249 topic = (char*)array_start(&Conf_Channel[i].topic);
250 printf( " Topic = %s\n\n", topic ? topic : "");
258 Conf_UnsetServer( CONN_ID Idx )
260 /* Set next time for next connection attempt, if this is a server
261 * link that is (still) configured here. If the server is set as
262 * "once", delete it from our configuration.
263 * Non-Server-Connections will be silently ignored. */
268 /* Check all our configured servers */
269 for( i = 0; i < MAX_SERVERS; i++ ) {
270 if( Conf_Server[i].conn_id != Idx ) continue;
272 /* Gotcha! Mark server configuration as "unused": */
273 Conf_Server[i].conn_id = NONE;
275 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
276 /* Delete configuration here */
277 Init_Server_Struct( &Conf_Server[i] );
279 /* Set time for next connect attempt */
281 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
282 /* The connection has been "long", so we don't
283 * require the next attempt to be delayed. */
284 Conf_Server[i].lasttry =
285 t - Conf_ConnectRetry + RECONNECT_DELAY;
287 Conf_Server[i].lasttry = t;
290 } /* Conf_UnsetServer */
294 Conf_SetServer( int ConfServer, CONN_ID Idx )
296 /* Set connection for specified configured server */
298 assert( ConfServer > NONE );
299 assert( Idx > NONE );
301 Conf_Server[ConfServer].conn_id = Idx;
302 } /* Conf_SetServer */
306 Conf_GetServer( CONN_ID Idx )
308 /* Get index of server in configuration structure */
312 assert( Idx > NONE );
314 for( i = 0; i < MAX_SERVERS; i++ ) {
315 if( Conf_Server[i].conn_id == Idx ) return i;
318 } /* Conf_GetServer */
322 Conf_EnableServer( char *Name, UINT16 Port )
324 /* Enable specified server and adjust port */
328 assert( Name != NULL );
330 for( i = 0; i < MAX_SERVERS; i++ ) {
331 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
332 /* Gotcha! Set port and enable server: */
333 Conf_Server[i].port = Port;
334 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
339 } /* Conf_EnableServer */
343 Conf_EnablePassiveServer(const char *Name)
345 /* Enable specified server */
348 assert( Name != NULL );
349 for (i = 0; i < MAX_SERVERS; i++) {
350 if ((strcasecmp( Conf_Server[i].name, Name ) == 0) && (Conf_Server[i].port > 0)) {
351 /* BINGO! Enable server */
352 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
357 } /* Conf_EnablePassiveServer */
361 Conf_DisableServer( char *Name )
363 /* Enable specified server and adjust port */
367 assert( Name != NULL );
369 for( i = 0; i < MAX_SERVERS; i++ ) {
370 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
371 /* Gotcha! Disable and disconnect server: */
372 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
373 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
378 } /* Conf_DisableServer */
382 Conf_AddServer( char *Name, UINT16 Port, char *Host, char *MyPwd, char *PeerPwd )
384 /* Add new server to configuration */
388 assert( Name != NULL );
389 assert( Host != NULL );
390 assert( MyPwd != NULL );
391 assert( PeerPwd != NULL );
393 /* Search unused item in server configuration structure */
394 for( i = 0; i < MAX_SERVERS; i++ ) {
395 /* Is this item used? */
396 if( ! Conf_Server[i].name[0] ) break;
398 if( i >= MAX_SERVERS ) return false;
400 Init_Server_Struct( &Conf_Server[i] );
401 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
402 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
403 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
404 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
405 Conf_Server[i].port = Port;
406 Conf_Server[i].flags = CONF_SFLAG_ONCE;
409 } /* Conf_AddServer */
413 Set_Defaults( bool InitServers )
415 /* Initialize configuration variables with default values. */
419 strcpy( Conf_ServerName, "" );
420 snprintf( Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
421 strcpy( Conf_ServerPwd, "" );
423 strcpy( Conf_ServerAdmin1, "" );
424 strcpy( Conf_ServerAdmin2, "" );
425 strcpy( Conf_ServerAdminMail, "" );
427 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
428 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
430 strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
432 strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
434 strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
436 strcpy( Conf_ListenAddress, "" );
438 Conf_UID = Conf_GID = 0;
440 Conf_PingTimeout = 120;
441 Conf_PongTimeout = 20;
443 Conf_ConnectRetry = 60;
446 Conf_Channel_Count = 0;
448 Conf_OperCanMode = false;
450 Conf_PredefChannelsOnly = false;
451 Conf_OperServerMode = false;
453 Conf_MaxConnections = 0;
454 Conf_MaxConnectionsIP = 5;
456 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
458 /* Initialize server configuration structures */
459 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
466 /* Read configuration file. */
468 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
469 const UINT16 defaultport = 6667;
473 /* Open configuration file */
474 fd = fopen( NGIRCd_ConfFile, "r" );
476 /* No configuration file found! */
477 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
478 NGIRCd_ConfFile, strerror( errno ));
479 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
483 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
485 /* Clean up server configuration structure: mark all already
486 * configured servers as "once" so that they are deleted
487 * after the next disconnect and delete all unused servers.
488 * And delete all servers which are "duplicates" of servers
489 * that are already marked as "once" (such servers have been
490 * created by the last rehash but are now useless). */
491 for( i = 0; i < MAX_SERVERS; i++ ) {
492 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
494 /* This structure is in use ... */
495 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
496 /* Check for duplicates */
497 for( n = 0; n < MAX_SERVERS; n++ ) {
498 if( n == i ) continue;
500 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
501 Init_Server_Struct( &Conf_Server[n] );
503 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
509 /* Mark server as "once" */
510 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
511 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
516 /* Initialize variables */
518 strcpy( section, "" );
519 Init_Server_Struct( &New_Server );
520 New_Server_Idx = NONE;
522 /* Read configuration file */
524 if( ! fgets( str, LINE_LEN, fd )) break;
528 /* Skip comments and empty lines */
529 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
531 /* Is this the beginning of a new section? */
532 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
533 strlcpy( section, str, sizeof( section ));
534 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
537 if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
538 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
539 Config_Error( LOG_ERR, "Too many operators configured.");
541 /* Initialize new operator structure */
542 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
543 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
544 if (Conf_Oper[Conf_Oper_Count].mask) {
545 free(Conf_Oper[Conf_Oper_Count].mask );
546 Conf_Oper[Conf_Oper_Count].mask = NULL;
552 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
553 /* Check if there is already a server to add */
554 if( New_Server.name[0] ) {
555 /* Copy data to "real" server structure */
556 assert( New_Server_Idx > NONE );
557 Conf_Server[New_Server_Idx] = New_Server;
560 /* Re-init structure for new server */
561 Init_Server_Struct( &New_Server );
563 /* Search unused item in server configuration structure */
564 for( i = 0; i < MAX_SERVERS; i++ ) {
565 /* Is this item used? */
566 if( ! Conf_Server[i].name[0] ) break;
568 if( i >= MAX_SERVERS ) {
569 /* Oops, no free item found! */
570 Config_Error( LOG_ERR, "Too many servers configured." );
571 New_Server_Idx = NONE;
573 else New_Server_Idx = i;
576 if( strcasecmp( section, "[CHANNEL]" ) == 0 ) {
577 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) {
578 Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
580 /* Initialize new channel structure */
581 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
582 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
583 strcpy( Conf_Channel[Conf_Channel_Count].key, "" );
584 Conf_Channel[Conf_Channel_Count].maxusers = 0;
585 array_free(&Conf_Channel[Conf_Channel_Count].topic);
586 Conf_Channel_Count++;
590 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
593 if( section[0] == 0x1 ) continue;
595 /* Split line into variable name and parameters */
596 ptr = strchr( str, '=' );
598 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
602 var = str; ngt_TrimStr( var );
603 arg = ptr + 1; ngt_TrimStr( arg );
605 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
606 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
607 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
608 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
609 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
612 /* Close configuration file */
615 /* Check if there is still a server to add */
616 if( New_Server.name[0] ) {
617 /* Copy data to "real" server structure */
618 assert( New_Server_Idx > NONE );
619 Conf_Server[New_Server_Idx] = New_Server;
622 if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
623 if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
624 Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
625 (unsigned int) defaultport, strerror(errno));
633 Check_ArgIsTrue( const char *Arg )
635 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
636 if( strcasecmp( Arg, "true" ) == 0 ) return true;
637 if( atoi( Arg ) != 0 ) return true;
640 } /* Check_ArgIsTrue */
643 static unsigned int Handle_MaxNickLength(int Line, const char *Arg)
647 new = (unsigned) atoi(Arg) + 1;
648 if (new > CLIENT_NICK_LEN) {
649 Config_Error(LOG_WARNING,
650 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
651 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
652 return CLIENT_NICK_LEN;
655 Config_Error(LOG_WARNING,
656 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
657 NGIRCd_ConfFile, Line);
661 } /* Handle_MaxNickLength */
665 Handle_GLOBAL( int Line, char *Var, char *Arg )
672 assert( Var != NULL );
673 assert( Arg != NULL );
675 if( strcasecmp( Var, "Name" ) == 0 ) {
677 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
678 if (len >= sizeof( Conf_ServerName ))
679 Config_Error_TooLong( Line, Var );
682 if( strcasecmp( Var, "Info" ) == 0 ) {
683 /* Info text of server */
684 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
685 if (len >= sizeof( Conf_ServerInfo ))
686 Config_Error_TooLong ( Line, Var );
689 if( strcasecmp( Var, "Password" ) == 0 ) {
690 /* Global server password */
691 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
692 if (len >= sizeof( Conf_ServerPwd ))
693 Config_Error_TooLong( Line, Var );
696 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
697 /* Administrative info #1 */
698 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
699 if (len >= sizeof( Conf_ServerAdmin1 ))
700 Config_Error_TooLong ( Line, Var );
703 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
704 /* Administrative info #2 */
705 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
706 if (len >= sizeof( Conf_ServerAdmin2 ))
707 Config_Error_TooLong ( Line, Var );
710 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
711 /* Administrative email contact */
712 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
713 if (len >= sizeof( Conf_ServerAdminMail ))
714 Config_Error_TooLong( Line, Var );
718 if( strcasecmp( Var, "Ports" ) == 0 ) {
719 ports_parse(&Conf_ListenPorts, Line, Arg);
722 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
723 /* "Message of the day" (MOTD) file */
724 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
725 if (len >= sizeof( Conf_MotdFile ))
726 Config_Error_TooLong( Line, Var );
729 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
730 /* "Message of the day" phrase (instead of file) */
731 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
732 if (len >= sizeof( Conf_MotdPhrase ))
733 Config_Error_TooLong( Line, Var );
736 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
737 /* directory for chroot() */
738 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
739 if (len >= sizeof( Conf_Chroot ))
740 Config_Error_TooLong( Line, Var );
743 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
744 /* name of pidfile */
745 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
746 if (len >= sizeof( Conf_PidFile ))
747 Config_Error_TooLong( Line, Var );
750 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
751 /* UID the daemon should switch to */
752 pwd = getpwnam( Arg );
753 if( pwd ) Conf_UID = pwd->pw_uid;
756 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
759 Conf_UID = (unsigned int)atoi( Arg );
763 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
764 /* GID the daemon should use */
765 grp = getgrnam( Arg );
766 if( grp ) Conf_GID = grp->gr_gid;
769 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
772 Conf_GID = (unsigned int)atoi( Arg );
776 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
778 Conf_PingTimeout = atoi( Arg );
779 if( Conf_PingTimeout < 5 ) {
780 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
781 NGIRCd_ConfFile, Line );
782 Conf_PingTimeout = 5;
786 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
788 Conf_PongTimeout = atoi( Arg );
789 if( Conf_PongTimeout < 5 ) {
790 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
791 NGIRCd_ConfFile, Line );
792 Conf_PongTimeout = 5;
796 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
797 /* Seconds between connection attempts to other servers */
798 Conf_ConnectRetry = atoi( Arg );
799 if( Conf_ConnectRetry < 5 ) {
800 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
801 NGIRCd_ConfFile, Line );
802 Conf_ConnectRetry = 5;
806 if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
807 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
808 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
811 if( strcasecmp( Var, "NoDNS" ) == 0 ) {
812 /* don't do reverse dns lookups when clients connect? */
813 Conf_NoDNS = Check_ArgIsTrue( Arg );
816 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
817 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
818 Conf_OperCanMode = Check_ArgIsTrue( Arg );
821 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
822 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
823 Conf_OperServerMode = Check_ArgIsTrue( Arg );
826 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
827 /* Maximum number of connections. 0 -> "no limit". */
829 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
832 Conf_MaxConnections = atol( Arg );
835 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
836 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
838 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
841 Conf_MaxConnectionsIP = atoi( Arg );
844 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
845 /* Maximum number of channels a user can join. 0 -> "no limit". */
847 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
850 Conf_MaxJoins = atoi( Arg );
853 if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
854 /* Maximum length of a nick name; must be same on all servers
855 * within the IRC network! */
856 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
860 if( strcasecmp( Var, "Listen" ) == 0 ) {
861 /* IP-Address to bind sockets */
862 len = strlcpy( Conf_ListenAddress, Arg, sizeof( Conf_ListenAddress ));
863 if (len >= sizeof( Conf_ListenAddress ))
864 Config_Error_TooLong( Line, Var );
868 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
869 NGIRCd_ConfFile, Line, Var );
870 } /* Handle_GLOBAL */
874 Handle_OPERATOR( int Line, char *Var, char *Arg )
876 unsigned int opercount;
879 assert( Var != NULL );
880 assert( Arg != NULL );
881 assert( Conf_Oper_Count > 0 );
883 if ( Conf_Oper_Count == 0 )
886 opercount = Conf_Oper_Count - 1;
888 if( strcasecmp( Var, "Name" ) == 0 ) {
889 /* Name of IRC operator */
890 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
891 if (len >= sizeof( Conf_Oper[opercount].name ))
892 Config_Error_TooLong( Line, Var );
895 if( strcasecmp( Var, "Password" ) == 0 ) {
896 /* Password of IRC operator */
897 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
898 if (len >= sizeof( Conf_Oper[opercount].pwd ))
899 Config_Error_TooLong( Line, Var );
902 if( strcasecmp( Var, "Mask" ) == 0 ) {
903 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
905 Conf_Oper[opercount].mask = strdup_warn( Arg );
908 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
909 NGIRCd_ConfFile, Line, Var );
910 } /* Handle_OPERATOR */
914 Handle_SERVER( int Line, char *Var, char *Arg )
920 assert( Var != NULL );
921 assert( Arg != NULL );
923 /* Ignore server block if no space is left in server configuration structure */
924 if( New_Server_Idx <= NONE ) return;
926 if( strcasecmp( Var, "Host" ) == 0 ) {
927 /* Hostname of the server */
928 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
929 if (len >= sizeof( New_Server.host ))
930 Config_Error_TooLong ( Line, Var );
933 if( strcasecmp( Var, "Name" ) == 0 ) {
934 /* Name of the server ("Nick"/"ID") */
935 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
936 if (len >= sizeof( New_Server.name ))
937 Config_Error_TooLong( Line, Var );
940 if (strcasecmp(Var, "Bind") == 0) {
941 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
944 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
945 NGIRCd_ConfFile, Line, Arg);
948 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
949 /* Password of this server which is sent to the peer */
951 Config_Error(LOG_ERR,
952 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
953 NGIRCd_ConfFile, Line);
955 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
956 if (len >= sizeof( New_Server.pwd_in ))
957 Config_Error_TooLong( Line, Var );
960 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
961 /* Passwort of the peer which must be received */
962 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
963 if (len >= sizeof( New_Server.pwd_out ))
964 Config_Error_TooLong( Line, Var );
967 if( strcasecmp( Var, "Port" ) == 0 ) {
968 /* Port to which this server should connect */
970 if( port > 0 && port < 0xFFFF )
971 New_Server.port = (UINT16)port;
973 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
974 NGIRCd_ConfFile, Line, port );
977 if( strcasecmp( Var, "Group" ) == 0 ) {
980 if( ! isdigit( (int)*Arg ))
981 Config_Error_NaN( Line, Var );
984 New_Server.group = atoi( Arg );
987 if( strcasecmp( Var, "Passive" ) == 0 ) {
988 if (Check_ArgIsTrue(Arg))
989 New_Server.flags |= CONF_SFLAG_DISABLED;
993 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
994 NGIRCd_ConfFile, Line, Var );
995 } /* Handle_SERVER */
999 Handle_Channelname(size_t chancount, const char *name)
1001 size_t size = sizeof( Conf_Channel[chancount].name );
1002 char *dest = Conf_Channel[chancount].name;
1004 if (*name && *name != '#') {
1009 return size > strlcpy(dest, name, size);
1014 Handle_CHANNEL( int Line, char *Var, char *Arg )
1017 size_t chancount = 0;
1020 assert( Var != NULL );
1021 assert( Arg != NULL );
1022 if (Conf_Channel_Count > 0)
1023 chancount = Conf_Channel_Count - 1;
1025 if( strcasecmp( Var, "Name" ) == 0 ) {
1026 if (!Handle_Channelname(chancount, Arg))
1027 Config_Error_TooLong( Line, Var );
1030 if( strcasecmp( Var, "Modes" ) == 0 ) {
1032 len = strlcpy( Conf_Channel[chancount].modes, Arg, sizeof( Conf_Channel[chancount].modes ));
1033 if (len >= sizeof( Conf_Channel[chancount].modes ))
1034 Config_Error_TooLong( Line, Var );
1037 if( strcasecmp( Var, "Topic" ) == 0 ) {
1039 if (!array_copys( &Conf_Channel[chancount].topic, Arg))
1040 Config_Error_TooLong( Line, Var );
1044 if( strcasecmp( Var, "Key" ) == 0 ) {
1045 /* Initial Channel Key (mode k) */
1046 len = strlcpy(Conf_Channel[chancount].key, Arg, sizeof(Conf_Channel[chancount].key));
1047 if (len >= sizeof( Conf_Channel[chancount].key ))
1048 Config_Error_TooLong(Line, Var);
1052 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1053 /* maximum user limit, mode l */
1054 Conf_Channel[chancount].maxusers = (unsigned long) atol(Arg);
1055 if (Conf_Channel[chancount].maxusers == 0)
1056 Config_Error_NaN(Line, Var);
1060 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1061 NGIRCd_ConfFile, Line, Var );
1062 } /* Handle_CHANNEL */
1066 Validate_Config(bool Configtest, bool Rehash)
1068 /* Validate configuration settings. */
1071 int i, servers, servers_once;
1075 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1076 ptr = Conf_ServerName;
1078 if (*ptr >= 'a' && *ptr <= 'z') continue;
1079 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1080 if (*ptr >= '0' && *ptr <= '9') continue;
1081 if (ptr > Conf_ServerName) {
1082 if (*ptr == '.' || *ptr == '-')
1085 Conf_ServerName[0] = '\0';
1089 if (!Conf_ServerName[0]) {
1090 /* No server name configured! */
1091 Config_Error(LOG_ALERT,
1092 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1094 if (!Configtest && !Rehash) {
1095 Config_Error(LOG_ALERT,
1096 "%s exiting due to fatal errors!",
1102 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1103 /* No dot in server name! */
1104 Config_Error(LOG_ALERT,
1105 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1108 Config_Error(LOG_ALERT,
1109 "%s exiting due to fatal errors!",
1116 if (!Conf_ServerAdminMail[0]) {
1117 /* No administrative contact configured! */
1118 Config_Error(LOG_ALERT,
1119 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1122 Config_Error(LOG_ALERT,
1123 "%s exiting due to fatal errors!",
1130 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1131 && !Conf_ServerAdminMail[0]) {
1132 /* No administrative information configured! */
1133 Config_Error(LOG_WARNING,
1134 "No administrative information configured but required by RFC!");
1138 servers = servers_once = 0;
1139 for (i = 0; i < MAX_SERVERS; i++) {
1140 if (Conf_Server[i].name[0]) {
1142 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1147 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1148 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1150 } /* Validate_Config */
1154 Config_Error_TooLong ( const int Line, const char *Item )
1156 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1161 Config_Error_NaN( const int Line, const char *Item )
1163 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1164 NGIRCd_ConfFile, Line, Item );
1169 static void Config_Error( const int Level, const char *Format, ... )
1171 static void Config_Error( Level, Format, va_alist )
1177 /* Error! Write to console and/or logfile. */
1179 char msg[MAX_LOG_MSG_LEN];
1182 assert( Format != NULL );
1185 va_start( ap, Format );
1189 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1192 /* During "normal operations" the log functions of the daemon should
1193 * be used, but during testing of the configuration file, all messages
1194 * should go directly to the console: */
1195 if (Use_Log) Log( Level, "%s", msg );
1197 } /* Config_Error */
1201 Init_Server_Struct( CONF_SERVER *Server )
1203 /* Initialize server configuration structur to default values */
1205 assert( Server != NULL );
1207 memset( Server, 0, sizeof (CONF_SERVER) );
1209 Server->group = NONE;
1210 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1212 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1214 Resolve_Init(&Server->res_stat);
1215 Server->conn_id = NONE;
1216 memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
1217 } /* Init_Server_Struct */