2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2009 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>
54 static bool Use_Log = true;
55 static CONF_SERVER New_Server;
56 static int New_Server_Idx;
58 static size_t Conf_Oper_Count;
59 static size_t Conf_Channel_Count;
60 static void Set_Defaults PARAMS(( bool InitServers ));
61 static bool Read_Config PARAMS(( bool ngircd_starting ));
62 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
64 static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
65 static void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
66 static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
67 static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
69 static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
71 static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
72 static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
74 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
77 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
79 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
83 struct SSLOptions Conf_SSLOptions;
88 free(Conf_SSLOptions.KeyFile);
89 Conf_SSLOptions.KeyFile = NULL;
91 free(Conf_SSLOptions.CertFile);
92 Conf_SSLOptions.CertFile = NULL;
94 free(Conf_SSLOptions.DHFile);
95 Conf_SSLOptions.DHFile = NULL;
96 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
100 ssl_print_configvar(const char *name, const char *file)
105 printf(" %s =\n", name);
109 fp = fopen(file, "r");
113 fprintf(stderr, "ERROR: %s \"%s\": %s\n",
114 name, file, strerror(errno));
116 printf(" %s = %s\n", name, file);
125 ret = ssl_print_configvar("SSLKeyFile", Conf_SSLOptions.KeyFile);
127 if (!ssl_print_configvar("SSLCertFile", Conf_SSLOptions.CertFile))
130 if (!ssl_print_configvar("SSLDHFile", Conf_SSLOptions.DHFile))
133 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
134 puts(" SSLKeyFilePassword = <secret>");
136 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
143 strdup_warn(const char *str)
145 char *ptr = strdup(str);
147 Config_Error(LOG_ERR, "Could not allocate mem for string: %s", str);
157 len = array_length(a, sizeof(UINT16));
159 ports = (UINT16*) array_start(a);
160 printf("%u", (unsigned int) *ports);
163 printf(", %u", (unsigned int) *ports);
171 ports_parse(array *a, int Line, char *Arg)
179 /* Ports on that the server should listen. More port numbers
180 * must be separated by "," */
181 ptr = strtok( Arg, "," );
185 if (port > 0 && port < 0xFFFF) {
186 port16 = (UINT16) port;
187 if (!array_catb(a, (char*)&port16, sizeof port16))
188 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
189 NGIRCd_ConfFile, Line, port, strerror(errno));
191 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
192 NGIRCd_ConfFile, Line, port );
195 ptr = strtok( NULL, "," );
204 Validate_Config(false, false);
211 if (!Read_Config(false))
213 Validate_Config(false, true);
215 /* Update CLIENT structure of local server */
216 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
218 } /* Config_Rehash */
222 yesno_to_str(int boolean_value)
233 struct Conf_Oper *op;
236 len = array_length(&Conf_Opers, sizeof(*op));
237 op = array_start(&Conf_Opers);
242 array_free(&Conf_Opers);
248 struct Conf_Oper *op;
251 len = array_length(&Conf_Opers, sizeof(*op));
252 op = array_start(&Conf_Opers);
257 printf(" Name = %s\n", op->name);
258 printf(" Password = %s\n", op->pwd);
259 printf(" Mask = %s\n\n", op->mask ? op->mask : "");
268 /* Read configuration, validate and output it. */
274 size_t predef_channel_count;
275 struct Conf_Channel *predef_chan;
279 if (! Read_Config(true))
282 config_valid = Validate_Config(true, false);
284 /* If stdin and stdout ("you can read our nice message and we can
285 * read in your keypress") are valid tty's, wait for a key: */
286 if( isatty( fileno( stdin )) && isatty( fileno( stdout ))) {
287 puts( "OK, press enter to see a dump of your service configuration ..." );
290 puts( "Ok, dump of your server configuration follows:\n" );
294 printf( " Name = %s\n", Conf_ServerName );
295 printf( " Info = %s\n", Conf_ServerInfo );
296 printf( " Password = %s\n", Conf_ServerPwd );
297 printf( " AdminInfo1 = %s\n", Conf_ServerAdmin1 );
298 printf( " AdminInfo2 = %s\n", Conf_ServerAdmin2 );
299 printf( " AdminEMail = %s\n", Conf_ServerAdminMail );
300 printf( " MotdFile = %s\n", Conf_MotdFile );
301 printf( " MotdPhrase = %s\n", Conf_MotdPhrase );
302 printf( " ChrootDir = %s\n", Conf_Chroot );
303 printf( " PidFile = %s\n", Conf_PidFile);
304 printf(" Listen = %s\n", Conf_ListenAddress);
305 fputs(" Ports = ", stdout);
306 ports_puts(&Conf_ListenPorts);
308 fputs(" SSLPorts = ", stdout);
309 ports_puts(&Conf_SSLOptions.ListenPorts);
311 config_valid = false;
314 pwd = getpwuid(Conf_UID);
316 printf(" ServerUID = %s\n", pwd->pw_name);
318 printf(" ServerUID = %ld\n", (long)Conf_UID);
319 grp = getgrgid(Conf_GID);
321 printf(" ServerGID = %s\n", grp->gr_name);
323 printf(" ServerGID = %ld\n", (long)Conf_GID);
324 printf(" PingTimeout = %d\n", Conf_PingTimeout);
325 printf(" PongTimeout = %d\n", Conf_PongTimeout);
326 printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
327 printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
328 printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
329 printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
330 printf(" PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
331 printf(" NoDNS = %s\n", yesno_to_str(Conf_NoDNS));
332 printf(" NoIdent = %s\n", yesno_to_str(Conf_NoIdent));
335 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
336 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
338 printf(" MaxConnections = %ld\n", Conf_MaxConnections);
339 printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
340 printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
341 printf(" MaxNickLength = %u\n\n", Conf_MaxNickLength - 1);
345 for( i = 0; i < MAX_SERVERS; i++ ) {
346 if( ! Conf_Server[i].name[0] ) continue;
348 /* Valid "Server" section */
350 printf( " Name = %s\n", Conf_Server[i].name );
351 printf( " Host = %s\n", Conf_Server[i].host );
352 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
354 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
356 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
357 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
358 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
359 printf( " Group = %d\n", Conf_Server[i].group );
360 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
363 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
364 predef_chan = array_start(&Conf_Channels);
366 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
367 if (!predef_chan->name[0])
370 /* Valid "Channel" section */
372 printf(" Name = %s\n", predef_chan->name);
373 printf(" Modes = %s\n", predef_chan->modes);
374 printf(" Key = %s\n", predef_chan->key);
375 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
376 printf(" Topic = %s\n", predef_chan->topic);
377 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
380 return (config_valid ? 0 : 1);
385 Conf_UnsetServer( CONN_ID Idx )
387 /* Set next time for next connection attempt, if this is a server
388 * link that is (still) configured here. If the server is set as
389 * "once", delete it from our configuration.
390 * Non-Server-Connections will be silently ignored. */
395 /* Check all our configured servers */
396 for( i = 0; i < MAX_SERVERS; i++ ) {
397 if( Conf_Server[i].conn_id != Idx ) continue;
399 /* Gotcha! Mark server configuration as "unused": */
400 Conf_Server[i].conn_id = NONE;
402 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
403 /* Delete configuration here */
404 Init_Server_Struct( &Conf_Server[i] );
406 /* Set time for next connect attempt */
408 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
409 /* The connection has been "long", so we don't
410 * require the next attempt to be delayed. */
411 Conf_Server[i].lasttry =
412 t - Conf_ConnectRetry + RECONNECT_DELAY;
414 Conf_Server[i].lasttry = t;
417 } /* Conf_UnsetServer */
421 Conf_SetServer( int ConfServer, CONN_ID Idx )
423 /* Set connection for specified configured server */
425 assert( ConfServer > NONE );
426 assert( Idx > NONE );
428 Conf_Server[ConfServer].conn_id = Idx;
429 } /* Conf_SetServer */
433 Conf_GetServer( CONN_ID Idx )
435 /* Get index of server in configuration structure */
439 assert( Idx > NONE );
441 for( i = 0; i < MAX_SERVERS; i++ ) {
442 if( Conf_Server[i].conn_id == Idx ) return i;
445 } /* Conf_GetServer */
449 Conf_EnableServer( const char *Name, UINT16 Port )
451 /* Enable specified server and adjust port */
455 assert( Name != NULL );
457 for( i = 0; i < MAX_SERVERS; i++ ) {
458 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
459 /* Gotcha! Set port and enable server: */
460 Conf_Server[i].port = Port;
461 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
462 return (Conf_Server[i].port && Conf_Server[i].host[0]);
466 } /* Conf_EnableServer */
470 Conf_EnablePassiveServer(const char *Name)
472 /* Enable specified server */
475 assert( Name != NULL );
476 for (i = 0; i < MAX_SERVERS; i++) {
477 if ((strcasecmp( Conf_Server[i].name, Name ) == 0) && (Conf_Server[i].port > 0)) {
478 /* BINGO! Enable server */
479 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
484 } /* Conf_EnablePassiveServer */
488 Conf_DisableServer( const char *Name )
490 /* Enable specified server and adjust port */
494 assert( Name != NULL );
496 for( i = 0; i < MAX_SERVERS; i++ ) {
497 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
498 /* Gotcha! Disable and disconnect server: */
499 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
500 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
505 } /* Conf_DisableServer */
509 Conf_AddServer( const char *Name, UINT16 Port, const char *Host, const char *MyPwd, const char *PeerPwd )
511 /* Add new server to configuration */
515 assert( Name != NULL );
516 assert( Host != NULL );
517 assert( MyPwd != NULL );
518 assert( PeerPwd != NULL );
520 /* Search unused item in server configuration structure */
521 for( i = 0; i < MAX_SERVERS; i++ ) {
522 /* Is this item used? */
523 if( ! Conf_Server[i].name[0] ) break;
525 if( i >= MAX_SERVERS ) return false;
527 Init_Server_Struct( &Conf_Server[i] );
528 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
529 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
530 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
531 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
532 Conf_Server[i].port = Port;
533 Conf_Server[i].flags = CONF_SFLAG_ONCE;
536 } /* Conf_AddServer */
540 * Check if the given nick name is an service
543 Conf_IsService(int ConfServer, const char *Nick)
545 return MatchCaseInsensitive(Conf_Server[ConfServer].svs_mask, Nick);
546 } /* Conf_IsService */
550 * Initialize configuration settings with their default values.
553 Set_Defaults(bool InitServers)
557 strcpy(Conf_ServerName, "");
558 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
559 PACKAGE_NAME, PACKAGE_VERSION);
560 strcpy(Conf_ServerPwd, "");
562 strcpy(Conf_ServerAdmin1, "");
563 strcpy(Conf_ServerAdmin2, "");
564 strcpy(Conf_ServerAdminMail, "");
566 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
567 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
568 strlcpy(Conf_MotdPhrase, MOTD_PHRASE, sizeof(Conf_MotdPhrase));
570 Conf_UID = Conf_GID = 0;
571 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
572 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
574 free(Conf_ListenAddress);
575 Conf_ListenAddress = NULL;
577 Conf_PingTimeout = 120;
578 Conf_PongTimeout = 20;
579 Conf_ConnectRetry = 60;
581 Conf_NoIdent = false;
584 Conf_Channel_Count = 0;
586 Conf_OperCanMode = false;
587 Conf_OperServerMode = false;
588 Conf_AllowRemoteOper = false;
589 Conf_PredefChannelsOnly = false;
591 Conf_ConnectIPv4 = true;
592 Conf_ConnectIPv6 = true;
594 Conf_MaxConnections = 0;
595 Conf_MaxConnectionsIP = 5;
597 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
599 /* Initialize server configuration structures */
601 for (i = 0; i < MAX_SERVERS;
602 Init_Server_Struct(&Conf_Server[i++]));
610 size_t cnt = array_bytes(&Conf_ListenPorts);
612 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
618 Read_Config( bool ngircd_starting )
620 /* Read configuration file. */
622 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
623 const UINT16 defaultport = 6667;
627 /* Open configuration file */
628 fd = fopen( NGIRCd_ConfFile, "r" );
630 /* No configuration file found! */
631 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
632 NGIRCd_ConfFile, strerror( errno ));
633 if (!ngircd_starting)
635 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
640 Set_Defaults( ngircd_starting );
642 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
644 /* Clean up server configuration structure: mark all already
645 * configured servers as "once" so that they are deleted
646 * after the next disconnect and delete all unused servers.
647 * And delete all servers which are "duplicates" of servers
648 * that are already marked as "once" (such servers have been
649 * created by the last rehash but are now useless). */
650 for( i = 0; i < MAX_SERVERS; i++ ) {
651 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
653 /* This structure is in use ... */
654 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
655 /* Check for duplicates */
656 for( n = 0; n < MAX_SERVERS; n++ ) {
657 if( n == i ) continue;
659 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
660 Init_Server_Struct( &Conf_Server[n] );
662 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
668 /* Mark server as "once" */
669 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
670 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
675 /* Initialize variables */
677 strcpy( section, "" );
678 Init_Server_Struct( &New_Server );
679 New_Server_Idx = NONE;
683 /* Read configuration file */
685 if( ! fgets( str, LINE_LEN, fd )) break;
689 /* Skip comments and empty lines */
690 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
692 /* Is this the beginning of a new section? */
693 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
694 strlcpy( section, str, sizeof( section ));
695 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
698 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
699 /* Check if there is already a server to add */
700 if( New_Server.name[0] ) {
701 /* Copy data to "real" server structure */
702 assert( New_Server_Idx > NONE );
703 Conf_Server[New_Server_Idx] = New_Server;
706 /* Re-init structure for new server */
707 Init_Server_Struct( &New_Server );
709 /* Search unused item in server configuration structure */
710 for( i = 0; i < MAX_SERVERS; i++ ) {
711 /* Is this item used? */
712 if( ! Conf_Server[i].name[0] ) break;
714 if( i >= MAX_SERVERS ) {
715 /* Oops, no free item found! */
716 Config_Error( LOG_ERR, "Too many servers configured." );
717 New_Server_Idx = NONE;
719 else New_Server_Idx = i;
722 if (strcasecmp(section, "[CHANNEL]") == 0) {
723 Conf_Channel_Count++;
726 if (strcasecmp(section, "[OPERATOR]") == 0) {
731 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
734 if( section[0] == 0x1 ) continue;
736 /* Split line into variable name and parameters */
737 ptr = strchr( str, '=' );
739 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
743 var = str; ngt_TrimStr( var );
744 arg = ptr + 1; ngt_TrimStr( arg );
746 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
747 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
748 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
749 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
750 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
753 /* Close configuration file */
756 /* Check if there is still a server to add */
757 if( New_Server.name[0] ) {
758 /* Copy data to "real" server structure */
759 assert( New_Server_Idx > NONE );
760 Conf_Server[New_Server_Idx] = New_Server;
763 /* not a single listening port? Add default. */
764 if (no_listenports() &&
765 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
767 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
768 (unsigned int) defaultport, strerror(errno));
773 if (!Conf_ListenAddress)
774 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
776 if (!Conf_ListenAddress) {
777 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
785 Check_ArgIsTrue( const char *Arg )
787 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
788 if( strcasecmp( Arg, "true" ) == 0 ) return true;
789 if( atoi( Arg ) != 0 ) return true;
792 } /* Check_ArgIsTrue */
795 static unsigned int Handle_MaxNickLength(int Line, const char *Arg)
799 new = (unsigned) atoi(Arg) + 1;
800 if (new > CLIENT_NICK_LEN) {
801 Config_Error(LOG_WARNING,
802 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
803 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
804 return CLIENT_NICK_LEN;
807 Config_Error(LOG_WARNING,
808 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
809 NGIRCd_ConfFile, Line);
813 } /* Handle_MaxNickLength */
817 Handle_GLOBAL( int Line, char *Var, char *Arg )
824 assert( Var != NULL );
825 assert( Arg != NULL );
827 if( strcasecmp( Var, "Name" ) == 0 ) {
829 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
830 if (len >= sizeof( Conf_ServerName ))
831 Config_Error_TooLong( Line, Var );
834 if( strcasecmp( Var, "Info" ) == 0 ) {
835 /* Info text of server */
836 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
837 if (len >= sizeof( Conf_ServerInfo ))
838 Config_Error_TooLong ( Line, Var );
841 if( strcasecmp( Var, "Password" ) == 0 ) {
842 /* Global server password */
843 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
844 if (len >= sizeof( Conf_ServerPwd ))
845 Config_Error_TooLong( Line, Var );
848 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
849 /* Administrative info #1 */
850 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
851 if (len >= sizeof( Conf_ServerAdmin1 ))
852 Config_Error_TooLong ( Line, Var );
855 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
856 /* Administrative info #2 */
857 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
858 if (len >= sizeof( Conf_ServerAdmin2 ))
859 Config_Error_TooLong ( Line, Var );
862 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
863 /* Administrative email contact */
864 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
865 if (len >= sizeof( Conf_ServerAdminMail ))
866 Config_Error_TooLong( Line, Var );
870 if( strcasecmp( Var, "Ports" ) == 0 ) {
871 ports_parse(&Conf_ListenPorts, Line, Arg);
874 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
875 /* "Message of the day" (MOTD) file */
876 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
877 if (len >= sizeof( Conf_MotdFile ))
878 Config_Error_TooLong( Line, Var );
881 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
882 /* "Message of the day" phrase (instead of file) */
883 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
884 if (len >= sizeof( Conf_MotdPhrase ))
885 Config_Error_TooLong( Line, Var );
888 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
889 /* directory for chroot() */
890 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
891 if (len >= sizeof( Conf_Chroot ))
892 Config_Error_TooLong( Line, Var );
895 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
896 /* name of pidfile */
897 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
898 if (len >= sizeof( Conf_PidFile ))
899 Config_Error_TooLong( Line, Var );
902 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
903 /* UID the daemon should switch to */
904 pwd = getpwnam( Arg );
905 if( pwd ) Conf_UID = pwd->pw_uid;
908 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
911 Conf_UID = (unsigned int)atoi( Arg );
915 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
916 /* GID the daemon should use */
917 grp = getgrnam( Arg );
918 if( grp ) Conf_GID = grp->gr_gid;
921 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
924 Conf_GID = (unsigned int)atoi( Arg );
928 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
930 Conf_PingTimeout = atoi( Arg );
931 if( Conf_PingTimeout < 5 ) {
932 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
933 NGIRCd_ConfFile, Line );
934 Conf_PingTimeout = 5;
938 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
940 Conf_PongTimeout = atoi( Arg );
941 if( Conf_PongTimeout < 5 ) {
942 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
943 NGIRCd_ConfFile, Line );
944 Conf_PongTimeout = 5;
948 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
949 /* Seconds between connection attempts to other servers */
950 Conf_ConnectRetry = atoi( Arg );
951 if( Conf_ConnectRetry < 5 ) {
952 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
953 NGIRCd_ConfFile, Line );
954 Conf_ConnectRetry = 5;
958 if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
959 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
960 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
963 if( strcasecmp( Var, "NoDNS" ) == 0 ) {
964 /* don't do reverse dns lookups when clients connect? */
965 Conf_NoDNS = Check_ArgIsTrue( Arg );
968 if (strcasecmp(Var, "NoIdent") == 0) {
969 /* don't do IDENT lookups when clients connect? */
970 Conf_NoIdent = Check_ArgIsTrue(Arg);
973 /* user has enabled ident lookups explicitly, but ... */
974 Config_Error(LOG_WARNING,
975 "%s: line %d: NoIdent=False, but ngircd was built without IDENT support",
976 NGIRCd_ConfFile, Line);
982 /* the default setting for all the WANT_IPV6 special options is 'true' */
983 if( strcasecmp( Var, "ConnectIPv6" ) == 0 ) {
984 /* connect to other hosts using ipv6, if they have an AAAA record? */
985 Conf_ConnectIPv6 = Check_ArgIsTrue( Arg );
988 if( strcasecmp( Var, "ConnectIPv4" ) == 0 ) {
989 /* connect to other hosts using ipv4.
990 * again, this can be used for ipv6-only setups */
991 Conf_ConnectIPv4 = Check_ArgIsTrue( Arg );
995 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
996 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
997 Conf_OperCanMode = Check_ArgIsTrue( Arg );
1000 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
1001 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
1002 Conf_OperServerMode = Check_ArgIsTrue( Arg );
1005 if(strcasecmp(Var, "AllowRemoteOper") == 0) {
1006 /* Are remote IRC operators allowed to control this server? */
1007 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1010 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
1011 /* Maximum number of connections. 0 -> "no limit". */
1013 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
1016 Conf_MaxConnections = atol( Arg );
1019 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
1020 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
1022 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
1025 Conf_MaxConnectionsIP = atoi( Arg );
1028 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
1029 /* Maximum number of channels a user can join. 0 -> "no limit". */
1031 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
1034 Conf_MaxJoins = atoi( Arg );
1037 if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
1038 /* Maximum length of a nick name; must be same on all servers
1039 * within the IRC network! */
1040 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1044 if( strcasecmp( Var, "Listen" ) == 0 ) {
1045 /* IP-Address to bind sockets */
1046 if (Conf_ListenAddress) {
1047 Config_Error(LOG_ERR, "Multiple Listen= options, ignoring: %s", Arg);
1050 Conf_ListenAddress = strdup_warn(Arg);
1052 * if allocation fails, we're in trouble:
1053 * we cannot ignore the error -- otherwise ngircd
1054 * would listen on all interfaces.
1056 if (!Conf_ListenAddress) {
1057 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
1064 if( strcasecmp( Var, "SSLPorts" ) == 0 ) {
1065 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1069 if( strcasecmp( Var, "SSLKeyFile" ) == 0 ) {
1070 assert(Conf_SSLOptions.KeyFile == NULL );
1071 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1074 if( strcasecmp( Var, "SSLCertFile" ) == 0 ) {
1075 assert(Conf_SSLOptions.CertFile == NULL );
1076 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1080 if( strcasecmp( Var, "SSLKeyFilePassword" ) == 0 ) {
1081 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1082 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1083 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Could not copy %s: %s!",
1084 NGIRCd_ConfFile, Line, Var, strerror(errno));
1087 if( strcasecmp( Var, "SSLDHFile" ) == 0 ) {
1088 assert(Conf_SSLOptions.DHFile == NULL);
1089 Conf_SSLOptions.DHFile = strdup_warn( Arg );
1093 Config_Error(LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
1094 NGIRCd_ConfFile, Line, Var);
1095 } /* Handle_GLOBAL */
1099 Handle_OPERATOR( int Line, char *Var, char *Arg )
1102 struct Conf_Oper *op;
1105 assert( Var != NULL );
1106 assert( Arg != NULL );
1107 assert( Conf_Oper_Count > 0 );
1109 op = array_alloc(&Conf_Opers, sizeof(*op), Conf_Oper_Count - 1);
1111 Config_Error(LOG_ERR, "Could not allocate memory for operator (%d:%s = %s)", Line, Var, Arg);
1115 if (strcasecmp(Var, "Name") == 0) {
1116 /* Name of IRC operator */
1117 len = strlcpy(op->name, Arg, sizeof(op->name));
1118 if (len >= sizeof(op->name))
1119 Config_Error_TooLong(Line, Var);
1122 if (strcasecmp(Var, "Password") == 0) {
1123 /* Password of IRC operator */
1124 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1125 if (len >= sizeof(op->pwd))
1126 Config_Error_TooLong(Line, Var);
1129 if (strcasecmp(Var, "Mask") == 0) {
1131 return; /* Hostname already configured */
1132 op->mask = strdup_warn( Arg );
1135 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
1136 NGIRCd_ConfFile, Line, Var );
1137 } /* Handle_OPERATOR */
1141 Handle_SERVER( int Line, char *Var, char *Arg )
1147 assert( Var != NULL );
1148 assert( Arg != NULL );
1150 /* Ignore server block if no space is left in server configuration structure */
1151 if( New_Server_Idx <= NONE ) return;
1153 if( strcasecmp( Var, "Host" ) == 0 ) {
1154 /* Hostname of the server */
1155 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1156 if (len >= sizeof( New_Server.host ))
1157 Config_Error_TooLong ( Line, Var );
1160 if( strcasecmp( Var, "Name" ) == 0 ) {
1161 /* Name of the server ("Nick"/"ID") */
1162 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1163 if (len >= sizeof( New_Server.name ))
1164 Config_Error_TooLong( Line, Var );
1167 if (strcasecmp(Var, "Bind") == 0) {
1168 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1171 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1172 NGIRCd_ConfFile, Line, Arg);
1175 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1176 /* Password of this server which is sent to the peer */
1178 Config_Error(LOG_ERR,
1179 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1180 NGIRCd_ConfFile, Line);
1182 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1183 if (len >= sizeof( New_Server.pwd_in ))
1184 Config_Error_TooLong( Line, Var );
1187 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1188 /* Passwort of the peer which must be received */
1189 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1190 if (len >= sizeof( New_Server.pwd_out ))
1191 Config_Error_TooLong( Line, Var );
1194 if( strcasecmp( Var, "Port" ) == 0 ) {
1195 /* Port to which this server should connect */
1197 if( port > 0 && port < 0xFFFF )
1198 New_Server.port = (UINT16)port;
1200 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
1201 NGIRCd_ConfFile, Line, port );
1205 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1206 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1210 if( strcasecmp( Var, "Group" ) == 0 ) {
1213 if( ! isdigit( (int)*Arg ))
1214 Config_Error_NaN( Line, Var );
1217 New_Server.group = atoi( Arg );
1220 if( strcasecmp( Var, "Passive" ) == 0 ) {
1221 if (Check_ArgIsTrue(Arg))
1222 New_Server.flags |= CONF_SFLAG_DISABLED;
1225 if (strcasecmp(Var, "ServiceMask") == 0) {
1226 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1227 sizeof(New_Server.svs_mask));
1228 if (len >= sizeof(New_Server.svs_mask))
1229 Config_Error_TooLong(Line, Var);
1233 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
1234 NGIRCd_ConfFile, Line, Var );
1235 } /* Handle_SERVER */
1239 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1241 size_t size = sizeof(new_chan->name);
1242 char *dest = new_chan->name;
1244 if (!Channel_IsValidName(name)) {
1246 * maybe user forgot to add a '#'.
1247 * This is only here for user convenience.
1253 return size > strlcpy(dest, name, size);
1258 Handle_CHANNEL(int Line, char *Var, char *Arg)
1262 struct Conf_Channel *chan;
1265 assert( Var != NULL );
1266 assert( Arg != NULL );
1267 assert(Conf_Channel_Count > 0);
1269 chancount = Conf_Channel_Count - 1;
1271 chan = array_alloc(&Conf_Channels, sizeof(*chan), chancount);
1273 Config_Error(LOG_ERR, "Could not allocate memory for predefined channel (%d:%s = %s)", Line, Var, Arg);
1276 if (strcasecmp(Var, "Name") == 0) {
1277 if (!Handle_Channelname(chan, Arg))
1278 Config_Error_TooLong(Line, Var);
1281 if (strcasecmp(Var, "Modes") == 0) {
1283 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1284 if (len >= sizeof(chan->modes))
1285 Config_Error_TooLong( Line, Var );
1288 if( strcasecmp( Var, "Topic" ) == 0 ) {
1290 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1291 if (len >= sizeof(chan->topic))
1292 Config_Error_TooLong( Line, Var );
1295 if( strcasecmp( Var, "Key" ) == 0 ) {
1296 /* Initial Channel Key (mode k) */
1297 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1298 if (len >= sizeof(chan->key))
1299 Config_Error_TooLong(Line, Var);
1302 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1303 /* maximum user limit, mode l */
1304 chan->maxusers = (unsigned long) atol(Arg);
1305 if (chan->maxusers == 0)
1306 Config_Error_NaN(Line, Var);
1309 if (strcasecmp(Var, "KeyFile") == 0) {
1311 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1312 if (len >= sizeof(chan->keyfile))
1313 Config_Error_TooLong(Line, Var);
1317 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1318 NGIRCd_ConfFile, Line, Var );
1319 } /* Handle_CHANNEL */
1323 Validate_Config(bool Configtest, bool Rehash)
1325 /* Validate configuration settings. */
1328 int i, servers, servers_once;
1330 bool config_valid = true;
1333 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1334 ptr = Conf_ServerName;
1336 if (*ptr >= 'a' && *ptr <= 'z') continue;
1337 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1338 if (*ptr >= '0' && *ptr <= '9') continue;
1339 if (ptr > Conf_ServerName) {
1340 if (*ptr == '.' || *ptr == '-')
1343 Conf_ServerName[0] = '\0';
1347 if (!Conf_ServerName[0]) {
1348 /* No server name configured! */
1349 config_valid = false;
1350 Config_Error(LOG_ALERT,
1351 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1353 if (!Configtest && !Rehash) {
1354 Config_Error(LOG_ALERT,
1355 "%s exiting due to fatal errors!",
1361 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1362 /* No dot in server name! */
1363 config_valid = false;
1364 Config_Error(LOG_ALERT,
1365 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1368 Config_Error(LOG_ALERT,
1369 "%s exiting due to fatal errors!",
1376 if (!Conf_ServerAdminMail[0]) {
1377 /* No administrative contact configured! */
1378 config_valid = false;
1379 Config_Error(LOG_ALERT,
1380 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1383 Config_Error(LOG_ALERT,
1384 "%s exiting due to fatal errors!",
1391 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1392 && !Conf_ServerAdminMail[0]) {
1393 /* No administrative information configured! */
1394 Config_Error(LOG_WARNING,
1395 "No administrative information configured but required by RFC!");
1399 servers = servers_once = 0;
1400 for (i = 0; i < MAX_SERVERS; i++) {
1401 if (Conf_Server[i].name[0]) {
1403 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1408 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1409 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1412 return config_valid;
1413 } /* Validate_Config */
1417 Config_Error_TooLong ( const int Line, const char *Item )
1419 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1424 Config_Error_NaN( const int Line, const char *Item )
1426 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1427 NGIRCd_ConfFile, Line, Item );
1432 static void Config_Error( const int Level, const char *Format, ... )
1434 static void Config_Error( Level, Format, va_alist )
1440 /* Error! Write to console and/or logfile. */
1442 char msg[MAX_LOG_MSG_LEN];
1445 assert( Format != NULL );
1448 va_start( ap, Format );
1452 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1455 /* During "normal operations" the log functions of the daemon should
1456 * be used, but during testing of the configuration file, all messages
1457 * should go directly to the console: */
1458 if (Use_Log) Log( Level, "%s", msg );
1460 } /* Config_Error */
1464 Init_Server_Struct( CONF_SERVER *Server )
1466 /* Initialize server configuration structur to default values */
1468 assert( Server != NULL );
1470 memset( Server, 0, sizeof (CONF_SERVER) );
1472 Server->group = NONE;
1473 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1475 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1477 Resolve_Init(&Server->res_stat);
1478 Server->conn_id = NONE;
1479 memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
1480 } /* Init_Server_Struct */