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 void 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 Validate_Config(true, false);
182 /* If stdin and stdout ("you can read our nice message and we can
183 * read in your keypress") are valid tty's, wait for a key: */
184 if( isatty( fileno( stdin )) && isatty( fileno( stdout ))) {
185 puts( "OK, press enter to see a dump of your service configuration ..." );
188 puts( "Ok, dump of your server configuration follows:\n" );
192 printf( " Name = %s\n", Conf_ServerName );
193 printf( " Info = %s\n", Conf_ServerInfo );
194 printf( " Password = %s\n", Conf_ServerPwd );
195 printf( " AdminInfo1 = %s\n", Conf_ServerAdmin1 );
196 printf( " AdminInfo2 = %s\n", Conf_ServerAdmin2 );
197 printf( " AdminEMail = %s\n", Conf_ServerAdminMail );
198 printf( " MotdFile = %s\n", Conf_MotdFile );
199 printf( " MotdPhrase = %s\n", Conf_MotdPhrase );
200 printf( " ChrootDir = %s\n", Conf_Chroot );
201 printf( " PidFile = %s\n", Conf_PidFile);
202 fputs(" Ports = ", stdout);
204 ports_puts(&Conf_ListenPorts);
205 printf(" Listen = %s\n", Conf_ListenAddress);
206 pwd = getpwuid( Conf_UID );
207 if( pwd ) printf( " ServerUID = %s\n", pwd->pw_name );
208 else printf( " ServerUID = %ld\n", (long)Conf_UID );
209 grp = getgrgid( Conf_GID );
210 if( grp ) printf( " ServerGID = %s\n", grp->gr_name );
211 else printf( " ServerGID = %ld\n", (long)Conf_GID );
212 printf( " PingTimeout = %d\n", Conf_PingTimeout );
213 printf( " PongTimeout = %d\n", Conf_PongTimeout );
214 printf( " ConnectRetry = %d\n", Conf_ConnectRetry );
215 printf( " OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
216 printf( " OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
217 printf( " PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
218 printf( " NoDNS = %s\n", yesno_to_str(Conf_NoDNS));
221 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
222 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
224 printf( " MaxConnections = %ld\n", Conf_MaxConnections);
225 printf( " MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
226 printf( " MaxJoins = %d\n", Conf_MaxJoins>0 ? Conf_MaxJoins : -1);
227 printf( " MaxNickLength = %u\n\n", Conf_MaxNickLength - 1);
229 for( i = 0; i < Conf_Oper_Count; i++ ) {
230 if( ! Conf_Oper[i].name[0] ) continue;
232 /* Valid "Operator" section */
233 puts( "[OPERATOR]" );
234 printf( " Name = %s\n", Conf_Oper[i].name );
235 printf( " Password = %s\n", Conf_Oper[i].pwd );
236 if ( Conf_Oper[i].mask ) printf( " Mask = %s\n", Conf_Oper[i].mask );
240 for( i = 0; i < MAX_SERVERS; i++ ) {
241 if( ! Conf_Server[i].name[0] ) continue;
243 /* Valid "Server" section */
245 printf( " Name = %s\n", Conf_Server[i].name );
246 printf( " Host = %s\n", Conf_Server[i].host );
247 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
248 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
249 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
250 printf( " Group = %d\n", Conf_Server[i].group );
251 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
254 for( i = 0; i < Conf_Channel_Count; i++ ) {
255 if( ! Conf_Channel[i].name[0] ) continue;
257 /* Valid "Channel" section */
259 printf( " Name = %s\n", Conf_Channel[i].name );
260 printf( " Modes = %s\n", Conf_Channel[i].modes );
261 printf( " Key = %s\n", Conf_Channel[i].key );
262 printf( " MaxUsers = %lu\n", Conf_Channel[i].maxusers );
264 topic = (char*)array_start(&Conf_Channel[i].topic);
265 printf( " Topic = %s\n\n", topic ? topic : "");
273 Conf_UnsetServer( CONN_ID Idx )
275 /* Set next time for next connection attempt, if this is a server
276 * link that is (still) configured here. If the server is set as
277 * "once", delete it from our configuration.
278 * Non-Server-Connections will be silently ignored. */
283 /* Check all our configured servers */
284 for( i = 0; i < MAX_SERVERS; i++ ) {
285 if( Conf_Server[i].conn_id != Idx ) continue;
287 /* Gotcha! Mark server configuration as "unused": */
288 Conf_Server[i].conn_id = NONE;
290 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
291 /* Delete configuration here */
292 Init_Server_Struct( &Conf_Server[i] );
294 /* Set time for next connect attempt */
296 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
297 /* The connection has been "long", so we don't
298 * require the next attempt to be delayed. */
299 Conf_Server[i].lasttry =
300 t - Conf_ConnectRetry + RECONNECT_DELAY;
302 Conf_Server[i].lasttry = t;
305 } /* Conf_UnsetServer */
309 Conf_SetServer( int ConfServer, CONN_ID Idx )
311 /* Set connection for specified configured server */
313 assert( ConfServer > NONE );
314 assert( Idx > NONE );
316 Conf_Server[ConfServer].conn_id = Idx;
317 } /* Conf_SetServer */
321 Conf_GetServer( CONN_ID Idx )
323 /* Get index of server in configuration structure */
327 assert( Idx > NONE );
329 for( i = 0; i < MAX_SERVERS; i++ ) {
330 if( Conf_Server[i].conn_id == Idx ) return i;
333 } /* Conf_GetServer */
337 Conf_EnableServer( char *Name, UINT16 Port )
339 /* Enable specified server and adjust port */
343 assert( Name != NULL );
345 for( i = 0; i < MAX_SERVERS; i++ ) {
346 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
347 /* Gotcha! Set port and enable server: */
348 Conf_Server[i].port = Port;
349 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
354 } /* Conf_EnableServer */
358 Conf_EnablePassiveServer(const char *Name)
360 /* Enable specified server */
363 assert( Name != NULL );
364 for (i = 0; i < MAX_SERVERS; i++) {
365 if ((strcasecmp( Conf_Server[i].name, Name ) == 0) && (Conf_Server[i].port > 0)) {
366 /* BINGO! Enable server */
367 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
372 } /* Conf_EnablePassiveServer */
376 Conf_DisableServer( char *Name )
378 /* Enable specified server and adjust port */
382 assert( Name != NULL );
384 for( i = 0; i < MAX_SERVERS; i++ ) {
385 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
386 /* Gotcha! Disable and disconnect server: */
387 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
388 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
393 } /* Conf_DisableServer */
397 Conf_AddServer( char *Name, UINT16 Port, char *Host, char *MyPwd, char *PeerPwd )
399 /* Add new server to configuration */
403 assert( Name != NULL );
404 assert( Host != NULL );
405 assert( MyPwd != NULL );
406 assert( PeerPwd != NULL );
408 /* Search unused item in server configuration structure */
409 for( i = 0; i < MAX_SERVERS; i++ ) {
410 /* Is this item used? */
411 if( ! Conf_Server[i].name[0] ) break;
413 if( i >= MAX_SERVERS ) return false;
415 Init_Server_Struct( &Conf_Server[i] );
416 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
417 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
418 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
419 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
420 Conf_Server[i].port = Port;
421 Conf_Server[i].flags = CONF_SFLAG_ONCE;
424 } /* Conf_AddServer */
428 Set_Defaults( bool InitServers )
430 /* Initialize configuration variables with default values. */
434 strcpy( Conf_ServerName, "" );
435 snprintf( Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
436 strcpy( Conf_ServerPwd, "" );
438 strcpy( Conf_ServerAdmin1, "" );
439 strcpy( Conf_ServerAdmin2, "" );
440 strcpy( Conf_ServerAdminMail, "" );
442 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
443 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
445 strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
447 strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
449 strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
451 free(Conf_ListenAddress);
452 Conf_ListenAddress = NULL;
453 Conf_UID = Conf_GID = 0;
455 Conf_PingTimeout = 120;
456 Conf_PongTimeout = 20;
458 Conf_ConnectRetry = 60;
461 Conf_Channel_Count = 0;
463 Conf_OperCanMode = false;
465 Conf_PredefChannelsOnly = false;
466 Conf_OperServerMode = false;
468 Conf_ConnectIPv4 = true;
469 Conf_ConnectIPv6 = true;
471 Conf_MaxConnections = 0;
472 Conf_MaxConnectionsIP = 5;
474 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
476 /* Initialize server configuration structures */
477 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
482 Read_Config( bool ngircd_starting )
484 /* Read configuration file. */
486 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
487 const UINT16 defaultport = 6667;
491 /* Open configuration file */
492 fd = fopen( NGIRCd_ConfFile, "r" );
494 /* No configuration file found! */
495 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
496 NGIRCd_ConfFile, strerror( errno ));
497 if (!ngircd_starting)
499 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
503 Set_Defaults( ngircd_starting );
505 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
507 /* Clean up server configuration structure: mark all already
508 * configured servers as "once" so that they are deleted
509 * after the next disconnect and delete all unused servers.
510 * And delete all servers which are "duplicates" of servers
511 * that are already marked as "once" (such servers have been
512 * created by the last rehash but are now useless). */
513 for( i = 0; i < MAX_SERVERS; i++ ) {
514 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
516 /* This structure is in use ... */
517 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
518 /* Check for duplicates */
519 for( n = 0; n < MAX_SERVERS; n++ ) {
520 if( n == i ) continue;
522 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
523 Init_Server_Struct( &Conf_Server[n] );
525 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
531 /* Mark server as "once" */
532 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
533 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
538 /* Initialize variables */
540 strcpy( section, "" );
541 Init_Server_Struct( &New_Server );
542 New_Server_Idx = NONE;
544 /* Read configuration file */
546 if( ! fgets( str, LINE_LEN, fd )) break;
550 /* Skip comments and empty lines */
551 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
553 /* Is this the beginning of a new section? */
554 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
555 strlcpy( section, str, sizeof( section ));
556 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
559 if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
560 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
561 Config_Error( LOG_ERR, "Too many operators configured.");
563 /* Initialize new operator structure */
564 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
565 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
566 if (Conf_Oper[Conf_Oper_Count].mask) {
567 free(Conf_Oper[Conf_Oper_Count].mask );
568 Conf_Oper[Conf_Oper_Count].mask = NULL;
574 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
575 /* Check if there is already a server to add */
576 if( New_Server.name[0] ) {
577 /* Copy data to "real" server structure */
578 assert( New_Server_Idx > NONE );
579 Conf_Server[New_Server_Idx] = New_Server;
582 /* Re-init structure for new server */
583 Init_Server_Struct( &New_Server );
585 /* Search unused item in server configuration structure */
586 for( i = 0; i < MAX_SERVERS; i++ ) {
587 /* Is this item used? */
588 if( ! Conf_Server[i].name[0] ) break;
590 if( i >= MAX_SERVERS ) {
591 /* Oops, no free item found! */
592 Config_Error( LOG_ERR, "Too many servers configured." );
593 New_Server_Idx = NONE;
595 else New_Server_Idx = i;
598 if( strcasecmp( section, "[CHANNEL]" ) == 0 ) {
599 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) {
600 Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
602 /* Initialize new channel structure */
603 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
604 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
605 strcpy( Conf_Channel[Conf_Channel_Count].key, "" );
606 Conf_Channel[Conf_Channel_Count].maxusers = 0;
607 array_free(&Conf_Channel[Conf_Channel_Count].topic);
608 Conf_Channel_Count++;
612 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
615 if( section[0] == 0x1 ) continue;
617 /* Split line into variable name and parameters */
618 ptr = strchr( str, '=' );
620 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
624 var = str; ngt_TrimStr( var );
625 arg = ptr + 1; ngt_TrimStr( arg );
627 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
628 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
629 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
630 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
631 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
634 /* Close configuration file */
637 /* Check if there is still a server to add */
638 if( New_Server.name[0] ) {
639 /* Copy data to "real" server structure */
640 assert( New_Server_Idx > NONE );
641 Conf_Server[New_Server_Idx] = New_Server;
644 if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
645 if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
646 Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
647 (unsigned int) defaultport, strerror(errno));
652 if (!Conf_ListenAddress)
653 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
655 if (!Conf_ListenAddress) {
656 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
664 Check_ArgIsTrue( const char *Arg )
666 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
667 if( strcasecmp( Arg, "true" ) == 0 ) return true;
668 if( atoi( Arg ) != 0 ) return true;
671 } /* Check_ArgIsTrue */
674 static unsigned int Handle_MaxNickLength(int Line, const char *Arg)
678 new = (unsigned) atoi(Arg) + 1;
679 if (new > CLIENT_NICK_LEN) {
680 Config_Error(LOG_WARNING,
681 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
682 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
683 return CLIENT_NICK_LEN;
686 Config_Error(LOG_WARNING,
687 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
688 NGIRCd_ConfFile, Line);
692 } /* Handle_MaxNickLength */
696 Handle_GLOBAL( int Line, char *Var, char *Arg )
703 assert( Var != NULL );
704 assert( Arg != NULL );
706 if( strcasecmp( Var, "Name" ) == 0 ) {
708 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
709 if (len >= sizeof( Conf_ServerName ))
710 Config_Error_TooLong( Line, Var );
713 if( strcasecmp( Var, "Info" ) == 0 ) {
714 /* Info text of server */
715 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
716 if (len >= sizeof( Conf_ServerInfo ))
717 Config_Error_TooLong ( Line, Var );
720 if( strcasecmp( Var, "Password" ) == 0 ) {
721 /* Global server password */
722 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
723 if (len >= sizeof( Conf_ServerPwd ))
724 Config_Error_TooLong( Line, Var );
727 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
728 /* Administrative info #1 */
729 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
730 if (len >= sizeof( Conf_ServerAdmin1 ))
731 Config_Error_TooLong ( Line, Var );
734 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
735 /* Administrative info #2 */
736 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
737 if (len >= sizeof( Conf_ServerAdmin2 ))
738 Config_Error_TooLong ( Line, Var );
741 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
742 /* Administrative email contact */
743 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
744 if (len >= sizeof( Conf_ServerAdminMail ))
745 Config_Error_TooLong( Line, Var );
749 if( strcasecmp( Var, "Ports" ) == 0 ) {
750 ports_parse(&Conf_ListenPorts, Line, Arg);
753 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
754 /* "Message of the day" (MOTD) file */
755 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
756 if (len >= sizeof( Conf_MotdFile ))
757 Config_Error_TooLong( Line, Var );
760 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
761 /* "Message of the day" phrase (instead of file) */
762 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
763 if (len >= sizeof( Conf_MotdPhrase ))
764 Config_Error_TooLong( Line, Var );
767 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
768 /* directory for chroot() */
769 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
770 if (len >= sizeof( Conf_Chroot ))
771 Config_Error_TooLong( Line, Var );
774 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
775 /* name of pidfile */
776 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
777 if (len >= sizeof( Conf_PidFile ))
778 Config_Error_TooLong( Line, Var );
781 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
782 /* UID the daemon should switch to */
783 pwd = getpwnam( Arg );
784 if( pwd ) Conf_UID = pwd->pw_uid;
787 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
790 Conf_UID = (unsigned int)atoi( Arg );
794 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
795 /* GID the daemon should use */
796 grp = getgrnam( Arg );
797 if( grp ) Conf_GID = grp->gr_gid;
800 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
803 Conf_GID = (unsigned int)atoi( Arg );
807 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
809 Conf_PingTimeout = atoi( Arg );
810 if( Conf_PingTimeout < 5 ) {
811 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
812 NGIRCd_ConfFile, Line );
813 Conf_PingTimeout = 5;
817 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
819 Conf_PongTimeout = atoi( Arg );
820 if( Conf_PongTimeout < 5 ) {
821 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
822 NGIRCd_ConfFile, Line );
823 Conf_PongTimeout = 5;
827 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
828 /* Seconds between connection attempts to other servers */
829 Conf_ConnectRetry = atoi( Arg );
830 if( Conf_ConnectRetry < 5 ) {
831 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
832 NGIRCd_ConfFile, Line );
833 Conf_ConnectRetry = 5;
837 if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
838 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
839 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
842 if( strcasecmp( Var, "NoDNS" ) == 0 ) {
843 /* don't do reverse dns lookups when clients connect? */
844 Conf_NoDNS = Check_ArgIsTrue( Arg );
848 /* the default setting for all the WANT_IPV6 special options is 'true' */
849 if( strcasecmp( Var, "ConnectIPv6" ) == 0 ) {
850 /* connect to other hosts using ipv6, if they have an AAAA record? */
851 Conf_ConnectIPv6 = Check_ArgIsTrue( Arg );
854 if( strcasecmp( Var, "ConnectIPv4" ) == 0 ) {
855 /* connect to other hosts using ipv4.
856 * again, this can be used for ipv6-only setups */
857 Conf_ConnectIPv4 = Check_ArgIsTrue( Arg );
861 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
862 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
863 Conf_OperCanMode = Check_ArgIsTrue( Arg );
866 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
867 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
868 Conf_OperServerMode = Check_ArgIsTrue( Arg );
871 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
872 /* Maximum number of connections. 0 -> "no limit". */
874 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
877 Conf_MaxConnections = atol( Arg );
880 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
881 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
883 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
886 Conf_MaxConnectionsIP = atoi( Arg );
889 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
890 /* Maximum number of channels a user can join. 0 -> "no limit". */
892 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
895 Conf_MaxJoins = atoi( Arg );
898 if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
899 /* Maximum length of a nick name; must be same on all servers
900 * within the IRC network! */
901 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
905 if( strcasecmp( Var, "Listen" ) == 0 ) {
906 /* IP-Address to bind sockets */
907 if (Conf_ListenAddress) {
908 Config_Error(LOG_ERR, "Multiple Listen= options, ignoring: %s", Arg);
911 Conf_ListenAddress = strdup_warn(Arg);
913 * if allocation fails, we're in trouble:
914 * we cannot ignore the error -- otherwise ngircd
915 * would listen on all interfaces.
917 if (!Conf_ListenAddress) {
918 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
923 Config_Error(LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
924 NGIRCd_ConfFile, Line, Var);
925 } /* Handle_GLOBAL */
929 Handle_OPERATOR( int Line, char *Var, char *Arg )
931 unsigned int opercount;
934 assert( Var != NULL );
935 assert( Arg != NULL );
936 assert( Conf_Oper_Count > 0 );
938 if ( Conf_Oper_Count == 0 )
941 opercount = Conf_Oper_Count - 1;
943 if( strcasecmp( Var, "Name" ) == 0 ) {
944 /* Name of IRC operator */
945 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
946 if (len >= sizeof( Conf_Oper[opercount].name ))
947 Config_Error_TooLong( Line, Var );
950 if( strcasecmp( Var, "Password" ) == 0 ) {
951 /* Password of IRC operator */
952 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
953 if (len >= sizeof( Conf_Oper[opercount].pwd ))
954 Config_Error_TooLong( Line, Var );
957 if( strcasecmp( Var, "Mask" ) == 0 ) {
958 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
960 Conf_Oper[opercount].mask = strdup_warn( Arg );
963 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
964 NGIRCd_ConfFile, Line, Var );
965 } /* Handle_OPERATOR */
969 Handle_SERVER( int Line, char *Var, char *Arg )
975 assert( Var != NULL );
976 assert( Arg != NULL );
978 /* Ignore server block if no space is left in server configuration structure */
979 if( New_Server_Idx <= NONE ) return;
981 if( strcasecmp( Var, "Host" ) == 0 ) {
982 /* Hostname of the server */
983 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
984 if (len >= sizeof( New_Server.host ))
985 Config_Error_TooLong ( Line, Var );
988 if( strcasecmp( Var, "Name" ) == 0 ) {
989 /* Name of the server ("Nick"/"ID") */
990 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
991 if (len >= sizeof( New_Server.name ))
992 Config_Error_TooLong( Line, Var );
995 if (strcasecmp(Var, "Bind") == 0) {
996 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
999 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1000 NGIRCd_ConfFile, Line, Arg);
1003 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1004 /* Password of this server which is sent to the peer */
1006 Config_Error(LOG_ERR,
1007 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1008 NGIRCd_ConfFile, Line);
1010 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1011 if (len >= sizeof( New_Server.pwd_in ))
1012 Config_Error_TooLong( Line, Var );
1015 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1016 /* Passwort of the peer which must be received */
1017 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1018 if (len >= sizeof( New_Server.pwd_out ))
1019 Config_Error_TooLong( Line, Var );
1022 if( strcasecmp( Var, "Port" ) == 0 ) {
1023 /* Port to which this server should connect */
1025 if( port > 0 && port < 0xFFFF )
1026 New_Server.port = (UINT16)port;
1028 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
1029 NGIRCd_ConfFile, Line, port );
1032 if( strcasecmp( Var, "Group" ) == 0 ) {
1035 if( ! isdigit( (int)*Arg ))
1036 Config_Error_NaN( Line, Var );
1039 New_Server.group = atoi( Arg );
1042 if( strcasecmp( Var, "Passive" ) == 0 ) {
1043 if (Check_ArgIsTrue(Arg))
1044 New_Server.flags |= CONF_SFLAG_DISABLED;
1048 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
1049 NGIRCd_ConfFile, Line, Var );
1050 } /* Handle_SERVER */
1054 Handle_Channelname(size_t chancount, const char *name)
1056 size_t size = sizeof( Conf_Channel[chancount].name );
1057 char *dest = Conf_Channel[chancount].name;
1059 if (*name && *name != '#') {
1064 return size > strlcpy(dest, name, size);
1069 Handle_CHANNEL( int Line, char *Var, char *Arg )
1072 size_t chancount = 0;
1075 assert( Var != NULL );
1076 assert( Arg != NULL );
1077 if (Conf_Channel_Count > 0)
1078 chancount = Conf_Channel_Count - 1;
1080 if( strcasecmp( Var, "Name" ) == 0 ) {
1081 if (!Handle_Channelname(chancount, Arg))
1082 Config_Error_TooLong( Line, Var );
1085 if( strcasecmp( Var, "Modes" ) == 0 ) {
1087 len = strlcpy( Conf_Channel[chancount].modes, Arg, sizeof( Conf_Channel[chancount].modes ));
1088 if (len >= sizeof( Conf_Channel[chancount].modes ))
1089 Config_Error_TooLong( Line, Var );
1092 if( strcasecmp( Var, "Topic" ) == 0 ) {
1094 if (!array_copys( &Conf_Channel[chancount].topic, Arg))
1095 Config_Error_TooLong( Line, Var );
1099 if( strcasecmp( Var, "Key" ) == 0 ) {
1100 /* Initial Channel Key (mode k) */
1101 len = strlcpy(Conf_Channel[chancount].key, Arg, sizeof(Conf_Channel[chancount].key));
1102 if (len >= sizeof( Conf_Channel[chancount].key ))
1103 Config_Error_TooLong(Line, Var);
1107 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1108 /* maximum user limit, mode l */
1109 Conf_Channel[chancount].maxusers = (unsigned long) atol(Arg);
1110 if (Conf_Channel[chancount].maxusers == 0)
1111 Config_Error_NaN(Line, Var);
1115 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1116 NGIRCd_ConfFile, Line, Var );
1117 } /* Handle_CHANNEL */
1121 Validate_Config(bool Configtest, bool Rehash)
1123 /* Validate configuration settings. */
1126 int i, servers, servers_once;
1130 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1131 ptr = Conf_ServerName;
1133 if (*ptr >= 'a' && *ptr <= 'z') continue;
1134 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1135 if (*ptr >= '0' && *ptr <= '9') continue;
1136 if (ptr > Conf_ServerName) {
1137 if (*ptr == '.' || *ptr == '-')
1140 Conf_ServerName[0] = '\0';
1144 if (!Conf_ServerName[0]) {
1145 /* No server name configured! */
1146 Config_Error(LOG_ALERT,
1147 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1149 if (!Configtest && !Rehash) {
1150 Config_Error(LOG_ALERT,
1151 "%s exiting due to fatal errors!",
1157 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1158 /* No dot in server name! */
1159 Config_Error(LOG_ALERT,
1160 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1163 Config_Error(LOG_ALERT,
1164 "%s exiting due to fatal errors!",
1171 if (!Conf_ServerAdminMail[0]) {
1172 /* No administrative contact configured! */
1173 Config_Error(LOG_ALERT,
1174 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1177 Config_Error(LOG_ALERT,
1178 "%s exiting due to fatal errors!",
1185 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1186 && !Conf_ServerAdminMail[0]) {
1187 /* No administrative information configured! */
1188 Config_Error(LOG_WARNING,
1189 "No administrative information configured but required by RFC!");
1193 servers = servers_once = 0;
1194 for (i = 0; i < MAX_SERVERS; i++) {
1195 if (Conf_Server[i].name[0]) {
1197 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1202 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1203 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1205 } /* Validate_Config */
1209 Config_Error_TooLong ( const int Line, const char *Item )
1211 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1216 Config_Error_NaN( const int Line, const char *Item )
1218 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1219 NGIRCd_ConfFile, Line, Item );
1224 static void Config_Error( const int Level, const char *Format, ... )
1226 static void Config_Error( Level, Format, va_alist )
1232 /* Error! Write to console and/or logfile. */
1234 char msg[MAX_LOG_MSG_LEN];
1237 assert( Format != NULL );
1240 va_start( ap, Format );
1244 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1247 /* During "normal operations" the log functions of the daemon should
1248 * be used, but during testing of the configuration file, all messages
1249 * should go directly to the console: */
1250 if (Use_Log) Log( Level, "%s", msg );
1252 } /* Config_Error */
1256 Init_Server_Struct( CONF_SERVER *Server )
1258 /* Initialize server configuration structur to default values */
1260 assert( Server != NULL );
1262 memset( Server, 0, sizeof (CONF_SERVER) );
1264 Server->group = NONE;
1265 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1267 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1269 Resolve_Init(&Server->res_stat);
1270 Server->conn_id = NONE;
1271 memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
1272 } /* Init_Server_Struct */