2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 Alexander Barton (alex@barton.de)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
11 * Configuration management (reading, parsing & validation)
32 #include <sys/types.h>
52 static bool Use_Log = true;
53 static CONF_SERVER New_Server;
54 static int New_Server_Idx;
57 static void Set_Defaults PARAMS(( bool InitServers ));
58 static bool Read_Config PARAMS(( bool ngircd_starting ));
59 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
61 static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
62 static void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
63 static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
64 static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
66 static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
68 static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
69 static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
71 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
74 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
76 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
80 struct SSLOptions Conf_SSLOptions;
85 free(Conf_SSLOptions.KeyFile);
86 Conf_SSLOptions.KeyFile = NULL;
88 free(Conf_SSLOptions.CertFile);
89 Conf_SSLOptions.CertFile = NULL;
91 free(Conf_SSLOptions.DHFile);
92 Conf_SSLOptions.DHFile = NULL;
93 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
100 if (Conf_SSLOptions.KeyFile)
101 printf( " SSLKeyFile = %s\n", Conf_SSLOptions.KeyFile);
102 if (Conf_SSLOptions.CertFile)
103 printf( " SSLCertFile = %s\n", Conf_SSLOptions.CertFile);
104 if (Conf_SSLOptions.DHFile)
105 printf( " SSLDHFile = %s\n", Conf_SSLOptions.DHFile);
106 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
107 puts(" SSLKeyFilePassword = <secret>" );
108 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
113 strdup_warn(const char *str)
115 char *ptr = strdup(str);
117 Config_Error(LOG_ERR, "Could not allocate mem for string: %s", str);
127 len = array_length(a, sizeof(UINT16));
129 ports = (UINT16*) array_start(a);
130 printf("%u", (unsigned int) *ports);
133 printf(", %u", (unsigned int) *ports);
141 ports_parse(array *a, int Line, char *Arg)
149 /* Ports on that the server should listen. More port numbers
150 * must be separated by "," */
151 ptr = strtok( Arg, "," );
155 if (port > 0 && port < 0xFFFF) {
156 port16 = (UINT16) port;
157 if (!array_catb(a, (char*)&port16, sizeof port16))
158 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
159 NGIRCd_ConfFile, Line, port, strerror(errno));
161 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
162 NGIRCd_ConfFile, Line, port );
165 ptr = strtok( NULL, "," );
174 Validate_Config(false, false);
181 if (!Read_Config(false))
183 Validate_Config(false, true);
185 /* Update CLIENT structure of local server */
186 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
188 } /* Config_Rehash */
192 yesno_to_str(int boolean_value)
203 /* Read configuration, validate and output it. */
213 if (! Read_Config(true))
216 config_valid = Validate_Config(true, false);
218 /* If stdin and stdout ("you can read our nice message and we can
219 * read in your keypress") are valid tty's, wait for a key: */
220 if( isatty( fileno( stdin )) && isatty( fileno( stdout ))) {
221 puts( "OK, press enter to see a dump of your service configuration ..." );
224 puts( "Ok, dump of your server configuration follows:\n" );
228 printf( " Name = %s\n", Conf_ServerName );
229 printf( " Info = %s\n", Conf_ServerInfo );
230 printf( " Password = %s\n", Conf_ServerPwd );
231 printf( " AdminInfo1 = %s\n", Conf_ServerAdmin1 );
232 printf( " AdminInfo2 = %s\n", Conf_ServerAdmin2 );
233 printf( " AdminEMail = %s\n", Conf_ServerAdminMail );
234 printf( " MotdFile = %s\n", Conf_MotdFile );
235 printf( " MotdPhrase = %s\n", Conf_MotdPhrase );
236 printf( " ChrootDir = %s\n", Conf_Chroot );
237 printf( " PidFile = %s\n", Conf_PidFile);
238 printf(" Listen = %s\n", Conf_ListenAddress);
239 fputs(" Ports = ", stdout);
241 ports_puts(&Conf_ListenPorts);
243 fputs(" SSLPorts = ", stdout);
244 ports_puts(&Conf_SSLOptions.ListenPorts);
248 pwd = getpwuid( Conf_UID );
249 if( pwd ) printf( " ServerUID = %s\n", pwd->pw_name );
250 else printf( " ServerUID = %ld\n", (long)Conf_UID );
251 grp = getgrgid( Conf_GID );
252 if( grp ) printf( " ServerGID = %s\n", grp->gr_name );
253 else printf( " ServerGID = %ld\n", (long)Conf_GID );
254 printf( " PingTimeout = %d\n", Conf_PingTimeout );
255 printf( " PongTimeout = %d\n", Conf_PongTimeout );
256 printf( " ConnectRetry = %d\n", Conf_ConnectRetry );
257 printf( " OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
258 printf( " OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
259 printf( " PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
260 printf( " NoDNS = %s\n", yesno_to_str(Conf_NoDNS));
263 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
264 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
266 printf( " MaxConnections = %ld\n", Conf_MaxConnections);
267 printf( " MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
268 printf( " MaxJoins = %d\n", Conf_MaxJoins>0 ? Conf_MaxJoins : -1);
269 printf( " MaxNickLength = %u\n\n", Conf_MaxNickLength - 1);
271 for( i = 0; i < Conf_Oper_Count; i++ ) {
272 if( ! Conf_Oper[i].name[0] ) continue;
274 /* Valid "Operator" section */
275 puts( "[OPERATOR]" );
276 printf( " Name = %s\n", Conf_Oper[i].name );
277 printf( " Password = %s\n", Conf_Oper[i].pwd );
278 if ( Conf_Oper[i].mask ) printf( " Mask = %s\n", Conf_Oper[i].mask );
282 for( i = 0; i < MAX_SERVERS; i++ ) {
283 if( ! Conf_Server[i].name[0] ) continue;
285 /* Valid "Server" section */
287 printf( " Name = %s\n", Conf_Server[i].name );
288 printf( " Host = %s\n", Conf_Server[i].host );
289 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
291 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
293 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
294 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
295 printf( " Group = %d\n", Conf_Server[i].group );
296 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
299 for( i = 0; i < Conf_Channel_Count; i++ ) {
300 if( ! Conf_Channel[i].name[0] ) continue;
302 /* Valid "Channel" section */
304 printf( " Name = %s\n", Conf_Channel[i].name );
305 printf( " Modes = %s\n", Conf_Channel[i].modes );
306 printf( " Key = %s\n", Conf_Channel[i].key );
307 printf( " MaxUsers = %lu\n", Conf_Channel[i].maxusers );
309 topic = (char*)array_start(&Conf_Channel[i].topic);
310 printf( " Topic = %s\n\n", topic ? topic : "");
313 return (config_valid ? 0 : 1);
318 Conf_UnsetServer( CONN_ID Idx )
320 /* Set next time for next connection attempt, if this is a server
321 * link that is (still) configured here. If the server is set as
322 * "once", delete it from our configuration.
323 * Non-Server-Connections will be silently ignored. */
328 /* Check all our configured servers */
329 for( i = 0; i < MAX_SERVERS; i++ ) {
330 if( Conf_Server[i].conn_id != Idx ) continue;
332 /* Gotcha! Mark server configuration as "unused": */
333 Conf_Server[i].conn_id = NONE;
335 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
336 /* Delete configuration here */
337 Init_Server_Struct( &Conf_Server[i] );
339 /* Set time for next connect attempt */
341 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
342 /* The connection has been "long", so we don't
343 * require the next attempt to be delayed. */
344 Conf_Server[i].lasttry =
345 t - Conf_ConnectRetry + RECONNECT_DELAY;
347 Conf_Server[i].lasttry = t;
350 } /* Conf_UnsetServer */
354 Conf_SetServer( int ConfServer, CONN_ID Idx )
356 /* Set connection for specified configured server */
358 assert( ConfServer > NONE );
359 assert( Idx > NONE );
361 Conf_Server[ConfServer].conn_id = Idx;
362 } /* Conf_SetServer */
366 Conf_GetServer( CONN_ID Idx )
368 /* Get index of server in configuration structure */
372 assert( Idx > NONE );
374 for( i = 0; i < MAX_SERVERS; i++ ) {
375 if( Conf_Server[i].conn_id == Idx ) return i;
378 } /* Conf_GetServer */
382 Conf_EnableServer( char *Name, UINT16 Port )
384 /* Enable specified server and adjust port */
388 assert( Name != NULL );
390 for( i = 0; i < MAX_SERVERS; i++ ) {
391 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
392 /* Gotcha! Set port and enable server: */
393 Conf_Server[i].port = Port;
394 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
399 } /* Conf_EnableServer */
403 Conf_EnablePassiveServer(const char *Name)
405 /* Enable specified server */
408 assert( Name != NULL );
409 for (i = 0; i < MAX_SERVERS; i++) {
410 if ((strcasecmp( Conf_Server[i].name, Name ) == 0) && (Conf_Server[i].port > 0)) {
411 /* BINGO! Enable server */
412 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
417 } /* Conf_EnablePassiveServer */
421 Conf_DisableServer( char *Name )
423 /* Enable specified server and adjust port */
427 assert( Name != NULL );
429 for( i = 0; i < MAX_SERVERS; i++ ) {
430 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
431 /* Gotcha! Disable and disconnect server: */
432 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
433 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
438 } /* Conf_DisableServer */
442 Conf_AddServer( char *Name, UINT16 Port, char *Host, char *MyPwd, char *PeerPwd )
444 /* Add new server to configuration */
448 assert( Name != NULL );
449 assert( Host != NULL );
450 assert( MyPwd != NULL );
451 assert( PeerPwd != NULL );
453 /* Search unused item in server configuration structure */
454 for( i = 0; i < MAX_SERVERS; i++ ) {
455 /* Is this item used? */
456 if( ! Conf_Server[i].name[0] ) break;
458 if( i >= MAX_SERVERS ) return false;
460 Init_Server_Struct( &Conf_Server[i] );
461 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
462 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
463 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
464 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
465 Conf_Server[i].port = Port;
466 Conf_Server[i].flags = CONF_SFLAG_ONCE;
469 } /* Conf_AddServer */
473 Set_Defaults( bool InitServers )
475 /* Initialize configuration variables with default values. */
479 strcpy( Conf_ServerName, "" );
480 snprintf( Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
481 strcpy( Conf_ServerPwd, "" );
483 strcpy( Conf_ServerAdmin1, "" );
484 strcpy( Conf_ServerAdmin2, "" );
485 strcpy( Conf_ServerAdminMail, "" );
487 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
488 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
490 strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
492 strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
494 strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
496 free(Conf_ListenAddress);
497 Conf_ListenAddress = NULL;
498 Conf_UID = Conf_GID = 0;
500 Conf_PingTimeout = 120;
501 Conf_PongTimeout = 20;
503 Conf_ConnectRetry = 60;
506 Conf_Channel_Count = 0;
508 Conf_OperCanMode = false;
510 Conf_PredefChannelsOnly = false;
511 Conf_OperServerMode = false;
513 Conf_ConnectIPv4 = true;
514 Conf_ConnectIPv6 = true;
516 Conf_MaxConnections = 0;
517 Conf_MaxConnectionsIP = 5;
519 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
521 /* Initialize server configuration structures */
522 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
527 Read_Config( bool ngircd_starting )
529 /* Read configuration file. */
531 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
532 const UINT16 defaultport = 6667;
536 /* Open configuration file */
537 fd = fopen( NGIRCd_ConfFile, "r" );
539 /* No configuration file found! */
540 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
541 NGIRCd_ConfFile, strerror( errno ));
542 if (!ngircd_starting)
544 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
548 Set_Defaults( ngircd_starting );
550 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
552 /* Clean up server configuration structure: mark all already
553 * configured servers as "once" so that they are deleted
554 * after the next disconnect and delete all unused servers.
555 * And delete all servers which are "duplicates" of servers
556 * that are already marked as "once" (such servers have been
557 * created by the last rehash but are now useless). */
558 for( i = 0; i < MAX_SERVERS; i++ ) {
559 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
561 /* This structure is in use ... */
562 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
563 /* Check for duplicates */
564 for( n = 0; n < MAX_SERVERS; n++ ) {
565 if( n == i ) continue;
567 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
568 Init_Server_Struct( &Conf_Server[n] );
570 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
576 /* Mark server as "once" */
577 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
578 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
583 /* Initialize variables */
585 strcpy( section, "" );
586 Init_Server_Struct( &New_Server );
587 New_Server_Idx = NONE;
591 /* Read configuration file */
593 if( ! fgets( str, LINE_LEN, fd )) break;
597 /* Skip comments and empty lines */
598 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
600 /* Is this the beginning of a new section? */
601 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
602 strlcpy( section, str, sizeof( section ));
603 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
606 if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
607 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
608 Config_Error( LOG_ERR, "Too many operators configured.");
610 /* Initialize new operator structure */
611 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
612 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
613 if (Conf_Oper[Conf_Oper_Count].mask) {
614 free(Conf_Oper[Conf_Oper_Count].mask );
615 Conf_Oper[Conf_Oper_Count].mask = NULL;
621 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
622 /* Check if there is already a server to add */
623 if( New_Server.name[0] ) {
624 /* Copy data to "real" server structure */
625 assert( New_Server_Idx > NONE );
626 Conf_Server[New_Server_Idx] = New_Server;
629 /* Re-init structure for new server */
630 Init_Server_Struct( &New_Server );
632 /* Search unused item in server configuration structure */
633 for( i = 0; i < MAX_SERVERS; i++ ) {
634 /* Is this item used? */
635 if( ! Conf_Server[i].name[0] ) break;
637 if( i >= MAX_SERVERS ) {
638 /* Oops, no free item found! */
639 Config_Error( LOG_ERR, "Too many servers configured." );
640 New_Server_Idx = NONE;
642 else New_Server_Idx = i;
645 if( strcasecmp( section, "[CHANNEL]" ) == 0 ) {
646 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) {
647 Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
649 /* Initialize new channel structure */
650 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
651 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
652 strcpy( Conf_Channel[Conf_Channel_Count].key, "" );
653 Conf_Channel[Conf_Channel_Count].maxusers = 0;
654 array_free(&Conf_Channel[Conf_Channel_Count].topic);
655 Conf_Channel_Count++;
659 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
662 if( section[0] == 0x1 ) continue;
664 /* Split line into variable name and parameters */
665 ptr = strchr( str, '=' );
667 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
671 var = str; ngt_TrimStr( var );
672 arg = ptr + 1; ngt_TrimStr( arg );
674 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
675 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
676 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
677 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
678 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
681 /* Close configuration file */
684 /* Check if there is still a server to add */
685 if( New_Server.name[0] ) {
686 /* Copy data to "real" server structure */
687 assert( New_Server_Idx > NONE );
688 Conf_Server[New_Server_Idx] = New_Server;
691 if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
692 if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
693 Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
694 (unsigned int) defaultport, strerror(errno));
699 if (!Conf_ListenAddress)
700 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
702 if (!Conf_ListenAddress) {
703 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
711 Check_ArgIsTrue( const char *Arg )
713 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
714 if( strcasecmp( Arg, "true" ) == 0 ) return true;
715 if( atoi( Arg ) != 0 ) return true;
718 } /* Check_ArgIsTrue */
721 static unsigned int Handle_MaxNickLength(int Line, const char *Arg)
725 new = (unsigned) atoi(Arg) + 1;
726 if (new > CLIENT_NICK_LEN) {
727 Config_Error(LOG_WARNING,
728 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
729 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
730 return CLIENT_NICK_LEN;
733 Config_Error(LOG_WARNING,
734 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
735 NGIRCd_ConfFile, Line);
739 } /* Handle_MaxNickLength */
743 Handle_GLOBAL( int Line, char *Var, char *Arg )
750 assert( Var != NULL );
751 assert( Arg != NULL );
753 if( strcasecmp( Var, "Name" ) == 0 ) {
755 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
756 if (len >= sizeof( Conf_ServerName ))
757 Config_Error_TooLong( Line, Var );
760 if( strcasecmp( Var, "Info" ) == 0 ) {
761 /* Info text of server */
762 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
763 if (len >= sizeof( Conf_ServerInfo ))
764 Config_Error_TooLong ( Line, Var );
767 if( strcasecmp( Var, "Password" ) == 0 ) {
768 /* Global server password */
769 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
770 if (len >= sizeof( Conf_ServerPwd ))
771 Config_Error_TooLong( Line, Var );
774 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
775 /* Administrative info #1 */
776 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
777 if (len >= sizeof( Conf_ServerAdmin1 ))
778 Config_Error_TooLong ( Line, Var );
781 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
782 /* Administrative info #2 */
783 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
784 if (len >= sizeof( Conf_ServerAdmin2 ))
785 Config_Error_TooLong ( Line, Var );
788 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
789 /* Administrative email contact */
790 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
791 if (len >= sizeof( Conf_ServerAdminMail ))
792 Config_Error_TooLong( Line, Var );
796 if( strcasecmp( Var, "Ports" ) == 0 ) {
797 ports_parse(&Conf_ListenPorts, Line, Arg);
800 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
801 /* "Message of the day" (MOTD) file */
802 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
803 if (len >= sizeof( Conf_MotdFile ))
804 Config_Error_TooLong( Line, Var );
807 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
808 /* "Message of the day" phrase (instead of file) */
809 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
810 if (len >= sizeof( Conf_MotdPhrase ))
811 Config_Error_TooLong( Line, Var );
814 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
815 /* directory for chroot() */
816 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
817 if (len >= sizeof( Conf_Chroot ))
818 Config_Error_TooLong( Line, Var );
821 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
822 /* name of pidfile */
823 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
824 if (len >= sizeof( Conf_PidFile ))
825 Config_Error_TooLong( Line, Var );
828 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
829 /* UID the daemon should switch to */
830 pwd = getpwnam( Arg );
831 if( pwd ) Conf_UID = pwd->pw_uid;
834 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
837 Conf_UID = (unsigned int)atoi( Arg );
841 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
842 /* GID the daemon should use */
843 grp = getgrnam( Arg );
844 if( grp ) Conf_GID = grp->gr_gid;
847 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
850 Conf_GID = (unsigned int)atoi( Arg );
854 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
856 Conf_PingTimeout = atoi( Arg );
857 if( Conf_PingTimeout < 5 ) {
858 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
859 NGIRCd_ConfFile, Line );
860 Conf_PingTimeout = 5;
864 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
866 Conf_PongTimeout = atoi( Arg );
867 if( Conf_PongTimeout < 5 ) {
868 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
869 NGIRCd_ConfFile, Line );
870 Conf_PongTimeout = 5;
874 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
875 /* Seconds between connection attempts to other servers */
876 Conf_ConnectRetry = atoi( Arg );
877 if( Conf_ConnectRetry < 5 ) {
878 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
879 NGIRCd_ConfFile, Line );
880 Conf_ConnectRetry = 5;
884 if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
885 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
886 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
889 if( strcasecmp( Var, "NoDNS" ) == 0 ) {
890 /* don't do reverse dns lookups when clients connect? */
891 Conf_NoDNS = Check_ArgIsTrue( Arg );
895 /* the default setting for all the WANT_IPV6 special options is 'true' */
896 if( strcasecmp( Var, "ConnectIPv6" ) == 0 ) {
897 /* connect to other hosts using ipv6, if they have an AAAA record? */
898 Conf_ConnectIPv6 = Check_ArgIsTrue( Arg );
901 if( strcasecmp( Var, "ConnectIPv4" ) == 0 ) {
902 /* connect to other hosts using ipv4.
903 * again, this can be used for ipv6-only setups */
904 Conf_ConnectIPv4 = Check_ArgIsTrue( Arg );
908 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
909 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
910 Conf_OperCanMode = Check_ArgIsTrue( Arg );
913 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
914 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
915 Conf_OperServerMode = Check_ArgIsTrue( Arg );
918 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
919 /* Maximum number of connections. 0 -> "no limit". */
921 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
924 Conf_MaxConnections = atol( Arg );
927 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
928 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
930 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
933 Conf_MaxConnectionsIP = atoi( Arg );
936 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
937 /* Maximum number of channels a user can join. 0 -> "no limit". */
939 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
942 Conf_MaxJoins = atoi( Arg );
945 if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
946 /* Maximum length of a nick name; must be same on all servers
947 * within the IRC network! */
948 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
952 if( strcasecmp( Var, "Listen" ) == 0 ) {
953 /* IP-Address to bind sockets */
954 if (Conf_ListenAddress) {
955 Config_Error(LOG_ERR, "Multiple Listen= options, ignoring: %s", Arg);
958 Conf_ListenAddress = strdup_warn(Arg);
960 * if allocation fails, we're in trouble:
961 * we cannot ignore the error -- otherwise ngircd
962 * would listen on all interfaces.
964 if (!Conf_ListenAddress) {
965 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
972 if( strcasecmp( Var, "SSLPorts" ) == 0 ) {
973 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
977 if( strcasecmp( Var, "SSLKeyFile" ) == 0 ) {
978 assert(Conf_SSLOptions.KeyFile == NULL );
979 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
982 if( strcasecmp( Var, "SSLCertFile" ) == 0 ) {
983 assert(Conf_SSLOptions.CertFile == NULL );
984 Conf_SSLOptions.CertFile = strdup_warn(Arg);
988 if( strcasecmp( Var, "SSLKeyFilePassword" ) == 0 ) {
989 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
990 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
991 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Could not copy %s: %s!",
992 NGIRCd_ConfFile, Line, Var, strerror(errno));
995 if( strcasecmp( Var, "SSLDHFile" ) == 0 ) {
996 assert(Conf_SSLOptions.DHFile == NULL);
997 Conf_SSLOptions.DHFile = strdup_warn( Arg );
1001 Config_Error(LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
1002 NGIRCd_ConfFile, Line, Var);
1003 } /* Handle_GLOBAL */
1007 Handle_OPERATOR( int Line, char *Var, char *Arg )
1009 unsigned int opercount;
1012 assert( Var != NULL );
1013 assert( Arg != NULL );
1014 assert( Conf_Oper_Count > 0 );
1016 if ( Conf_Oper_Count == 0 )
1019 opercount = Conf_Oper_Count - 1;
1021 if( strcasecmp( Var, "Name" ) == 0 ) {
1022 /* Name of IRC operator */
1023 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
1024 if (len >= sizeof( Conf_Oper[opercount].name ))
1025 Config_Error_TooLong( Line, Var );
1028 if( strcasecmp( Var, "Password" ) == 0 ) {
1029 /* Password of IRC operator */
1030 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
1031 if (len >= sizeof( Conf_Oper[opercount].pwd ))
1032 Config_Error_TooLong( Line, Var );
1035 if( strcasecmp( Var, "Mask" ) == 0 ) {
1036 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
1038 Conf_Oper[opercount].mask = strdup_warn( Arg );
1041 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
1042 NGIRCd_ConfFile, Line, Var );
1043 } /* Handle_OPERATOR */
1047 Handle_SERVER( int Line, char *Var, char *Arg )
1053 assert( Var != NULL );
1054 assert( Arg != NULL );
1056 /* Ignore server block if no space is left in server configuration structure */
1057 if( New_Server_Idx <= NONE ) return;
1059 if( strcasecmp( Var, "Host" ) == 0 ) {
1060 /* Hostname of the server */
1061 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1062 if (len >= sizeof( New_Server.host ))
1063 Config_Error_TooLong ( Line, Var );
1066 if( strcasecmp( Var, "Name" ) == 0 ) {
1067 /* Name of the server ("Nick"/"ID") */
1068 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1069 if (len >= sizeof( New_Server.name ))
1070 Config_Error_TooLong( Line, Var );
1073 if (strcasecmp(Var, "Bind") == 0) {
1074 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1077 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1078 NGIRCd_ConfFile, Line, Arg);
1081 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1082 /* Password of this server which is sent to the peer */
1084 Config_Error(LOG_ERR,
1085 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1086 NGIRCd_ConfFile, Line);
1088 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1089 if (len >= sizeof( New_Server.pwd_in ))
1090 Config_Error_TooLong( Line, Var );
1093 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1094 /* Passwort of the peer which must be received */
1095 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1096 if (len >= sizeof( New_Server.pwd_out ))
1097 Config_Error_TooLong( Line, Var );
1100 if( strcasecmp( Var, "Port" ) == 0 ) {
1101 /* Port to which this server should connect */
1103 if( port > 0 && port < 0xFFFF )
1104 New_Server.port = (UINT16)port;
1106 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
1107 NGIRCd_ConfFile, Line, port );
1111 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1112 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1116 if( strcasecmp( Var, "Group" ) == 0 ) {
1119 if( ! isdigit( (int)*Arg ))
1120 Config_Error_NaN( Line, Var );
1123 New_Server.group = atoi( Arg );
1126 if( strcasecmp( Var, "Passive" ) == 0 ) {
1127 if (Check_ArgIsTrue(Arg))
1128 New_Server.flags |= CONF_SFLAG_DISABLED;
1132 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
1133 NGIRCd_ConfFile, Line, Var );
1134 } /* Handle_SERVER */
1138 Handle_Channelname(size_t chancount, const char *name)
1140 size_t size = sizeof( Conf_Channel[chancount].name );
1141 char *dest = Conf_Channel[chancount].name;
1143 if (*name && *name != '#') {
1148 return size > strlcpy(dest, name, size);
1153 Handle_CHANNEL( int Line, char *Var, char *Arg )
1156 size_t chancount = 0;
1159 assert( Var != NULL );
1160 assert( Arg != NULL );
1161 if (Conf_Channel_Count > 0)
1162 chancount = Conf_Channel_Count - 1;
1164 if( strcasecmp( Var, "Name" ) == 0 ) {
1165 if (!Handle_Channelname(chancount, Arg))
1166 Config_Error_TooLong( Line, Var );
1169 if( strcasecmp( Var, "Modes" ) == 0 ) {
1171 len = strlcpy( Conf_Channel[chancount].modes, Arg, sizeof( Conf_Channel[chancount].modes ));
1172 if (len >= sizeof( Conf_Channel[chancount].modes ))
1173 Config_Error_TooLong( Line, Var );
1176 if( strcasecmp( Var, "Topic" ) == 0 ) {
1178 if (!array_copys( &Conf_Channel[chancount].topic, Arg))
1179 Config_Error_TooLong( Line, Var );
1183 if( strcasecmp( Var, "Key" ) == 0 ) {
1184 /* Initial Channel Key (mode k) */
1185 len = strlcpy(Conf_Channel[chancount].key, Arg, sizeof(Conf_Channel[chancount].key));
1186 if (len >= sizeof( Conf_Channel[chancount].key ))
1187 Config_Error_TooLong(Line, Var);
1191 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1192 /* maximum user limit, mode l */
1193 Conf_Channel[chancount].maxusers = (unsigned long) atol(Arg);
1194 if (Conf_Channel[chancount].maxusers == 0)
1195 Config_Error_NaN(Line, Var);
1199 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1200 NGIRCd_ConfFile, Line, Var );
1201 } /* Handle_CHANNEL */
1205 Validate_Config(bool Configtest, bool Rehash)
1207 /* Validate configuration settings. */
1210 int i, servers, servers_once;
1212 bool config_valid = true;
1215 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1216 ptr = Conf_ServerName;
1218 if (*ptr >= 'a' && *ptr <= 'z') continue;
1219 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1220 if (*ptr >= '0' && *ptr <= '9') continue;
1221 if (ptr > Conf_ServerName) {
1222 if (*ptr == '.' || *ptr == '-')
1225 Conf_ServerName[0] = '\0';
1229 if (!Conf_ServerName[0]) {
1230 /* No server name configured! */
1231 config_valid = false;
1232 Config_Error(LOG_ALERT,
1233 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1235 if (!Configtest && !Rehash) {
1236 Config_Error(LOG_ALERT,
1237 "%s exiting due to fatal errors!",
1243 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1244 /* No dot in server name! */
1245 config_valid = false;
1246 Config_Error(LOG_ALERT,
1247 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1250 Config_Error(LOG_ALERT,
1251 "%s exiting due to fatal errors!",
1258 if (!Conf_ServerAdminMail[0]) {
1259 /* No administrative contact configured! */
1260 config_valid = false;
1261 Config_Error(LOG_ALERT,
1262 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1265 Config_Error(LOG_ALERT,
1266 "%s exiting due to fatal errors!",
1273 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1274 && !Conf_ServerAdminMail[0]) {
1275 /* No administrative information configured! */
1276 Config_Error(LOG_WARNING,
1277 "No administrative information configured but required by RFC!");
1281 servers = servers_once = 0;
1282 for (i = 0; i < MAX_SERVERS; i++) {
1283 if (Conf_Server[i].name[0]) {
1285 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1290 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1291 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1294 return config_valid;
1295 } /* Validate_Config */
1299 Config_Error_TooLong ( const int Line, const char *Item )
1301 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1306 Config_Error_NaN( const int Line, const char *Item )
1308 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1309 NGIRCd_ConfFile, Line, Item );
1314 static void Config_Error( const int Level, const char *Format, ... )
1316 static void Config_Error( Level, Format, va_alist )
1322 /* Error! Write to console and/or logfile. */
1324 char msg[MAX_LOG_MSG_LEN];
1327 assert( Format != NULL );
1330 va_start( ap, Format );
1334 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1337 /* During "normal operations" the log functions of the daemon should
1338 * be used, but during testing of the configuration file, all messages
1339 * should go directly to the console: */
1340 if (Use_Log) Log( Level, "%s", msg );
1342 } /* Config_Error */
1346 Init_Server_Struct( CONF_SERVER *Server )
1348 /* Initialize server configuration structur to default values */
1350 assert( Server != NULL );
1352 memset( Server, 0, sizeof (CONF_SERVER) );
1354 Server->group = NONE;
1355 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1357 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1359 Resolve_Init(&Server->res_stat);
1360 Server->conn_id = NONE;
1361 memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
1362 } /* Init_Server_Struct */