2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2010 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;
56 static size_t Conf_Oper_Count;
57 static size_t Conf_Channel_Count;
58 static void Set_Defaults PARAMS(( bool InitServers ));
59 static bool Read_Config PARAMS(( bool ngircd_starting ));
60 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
62 static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
63 static void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
64 static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
65 static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
67 static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
69 static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
70 static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
72 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
75 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
77 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
81 struct SSLOptions Conf_SSLOptions;
86 free(Conf_SSLOptions.KeyFile);
87 Conf_SSLOptions.KeyFile = NULL;
89 free(Conf_SSLOptions.CertFile);
90 Conf_SSLOptions.CertFile = NULL;
92 free(Conf_SSLOptions.DHFile);
93 Conf_SSLOptions.DHFile = NULL;
94 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
98 ssl_print_configvar(const char *name, const char *file)
103 printf(" %s =\n", name);
107 fp = fopen(file, "r");
111 fprintf(stderr, "ERROR: %s \"%s\": %s\n",
112 name, file, strerror(errno));
114 printf(" %s = %s\n", name, file);
123 ret = ssl_print_configvar("SSLKeyFile", Conf_SSLOptions.KeyFile);
125 if (!ssl_print_configvar("SSLCertFile", Conf_SSLOptions.CertFile))
128 if (!ssl_print_configvar("SSLDHFile", Conf_SSLOptions.DHFile))
131 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
132 puts(" SSLKeyFilePassword = <secret>");
134 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
141 strdup_warn(const char *str)
143 char *ptr = strdup(str);
145 Config_Error(LOG_ERR, "Could not allocate mem for string: %s", str);
155 len = array_length(a, sizeof(UINT16));
157 ports = (UINT16*) array_start(a);
158 printf("%u", (unsigned int) *ports);
161 printf(", %u", (unsigned int) *ports);
169 ports_parse(array *a, int Line, char *Arg)
177 /* Ports on that the server should listen. More port numbers
178 * must be separated by "," */
179 ptr = strtok( Arg, "," );
183 if (port > 0 && port < 0xFFFF) {
184 port16 = (UINT16) port;
185 if (!array_catb(a, (char*)&port16, sizeof port16))
186 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
187 NGIRCd_ConfFile, Line, port, strerror(errno));
189 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
190 NGIRCd_ConfFile, Line, port );
193 ptr = strtok( NULL, "," );
202 Validate_Config(false, false);
209 if (!Read_Config(false))
211 Validate_Config(false, true);
213 /* Update CLIENT structure of local server */
214 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
216 } /* Config_Rehash */
220 yesno_to_str(int boolean_value)
231 struct Conf_Oper *op;
234 len = array_length(&Conf_Opers, sizeof(*op));
235 op = array_start(&Conf_Opers);
240 array_free(&Conf_Opers);
246 struct Conf_Oper *op;
249 len = array_length(&Conf_Opers, sizeof(*op));
250 op = array_start(&Conf_Opers);
255 printf(" Name = %s\n", op->name);
256 printf(" Password = %s\n", op->pwd);
257 printf(" Mask = %s\n\n", op->mask ? op->mask : "");
266 /* Read configuration, validate and output it. */
272 size_t predef_channel_count;
273 struct Conf_Channel *predef_chan;
277 if (! Read_Config(true))
280 config_valid = Validate_Config(true, false);
282 /* If stdin and stdout ("you can read our nice message and we can
283 * read in your keypress") are valid tty's, wait for a key: */
284 if( isatty( fileno( stdin )) && isatty( fileno( stdout ))) {
285 puts( "OK, press enter to see a dump of your service configuration ..." );
288 puts( "Ok, dump of your server configuration follows:\n" );
292 printf(" Name = %s\n", Conf_ServerName);
293 printf(" Info = %s\n", Conf_ServerInfo);
294 printf(" Password = %s\n", Conf_ServerPwd);
295 printf(" WebircPassword = %s\n", Conf_WebircPwd);
296 printf(" AdminInfo1 = %s\n", Conf_ServerAdmin1);
297 printf(" AdminInfo2 = %s\n", Conf_ServerAdmin2);
298 printf(" AdminEMail = %s\n", Conf_ServerAdminMail);
299 printf(" MotdFile = %s\n", Conf_MotdFile);
300 printf(" MotdPhrase = %s\n", Conf_MotdPhrase);
301 printf(" ChrootDir = %s\n", Conf_Chroot);
302 printf(" PidFile = %s\n", Conf_PidFile);
303 printf(" Listen = %s\n", Conf_ListenAddress);
304 fputs(" Ports = ", stdout);
305 ports_puts(&Conf_ListenPorts);
307 fputs(" SSLPorts = ", stdout);
308 ports_puts(&Conf_SSLOptions.ListenPorts);
310 config_valid = false;
313 pwd = getpwuid(Conf_UID);
315 printf(" ServerUID = %s\n", pwd->pw_name);
317 printf(" ServerUID = %ld\n", (long)Conf_UID);
318 grp = getgrgid(Conf_GID);
320 printf(" ServerGID = %s\n", grp->gr_name);
322 printf(" ServerGID = %ld\n", (long)Conf_GID);
323 printf(" PingTimeout = %d\n", Conf_PingTimeout);
324 printf(" PongTimeout = %d\n", Conf_PongTimeout);
325 printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
326 printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
327 printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
328 printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
329 printf(" PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
330 printf(" NoDNS = %s\n", yesno_to_str(Conf_NoDNS));
331 printf(" NoIdent = %s\n", yesno_to_str(Conf_NoIdent));
334 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
335 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
337 printf(" MaxConnections = %ld\n", Conf_MaxConnections);
338 printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
339 printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
340 printf(" MaxNickLength = %u\n\n", Conf_MaxNickLength - 1);
344 for( i = 0; i < MAX_SERVERS; i++ ) {
345 if( ! Conf_Server[i].name[0] ) continue;
347 /* Valid "Server" section */
349 printf( " Name = %s\n", Conf_Server[i].name );
350 printf( " Host = %s\n", Conf_Server[i].host );
351 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
353 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
355 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
356 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
357 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
358 printf( " Group = %d\n", Conf_Server[i].group );
359 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
362 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
363 predef_chan = array_start(&Conf_Channels);
365 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
366 if (!predef_chan->name[0])
369 /* Valid "Channel" section */
371 printf(" Name = %s\n", predef_chan->name);
372 printf(" Modes = %s\n", predef_chan->modes);
373 printf(" Key = %s\n", predef_chan->key);
374 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
375 printf(" Topic = %s\n", predef_chan->topic);
376 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
379 return (config_valid ? 0 : 1);
384 Conf_UnsetServer( CONN_ID Idx )
386 /* Set next time for next connection attempt, if this is a server
387 * link that is (still) configured here. If the server is set as
388 * "once", delete it from our configuration.
389 * Non-Server-Connections will be silently ignored. */
394 /* Check all our configured servers */
395 for( i = 0; i < MAX_SERVERS; i++ ) {
396 if( Conf_Server[i].conn_id != Idx ) continue;
398 /* Gotcha! Mark server configuration as "unused": */
399 Conf_Server[i].conn_id = NONE;
401 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
402 /* Delete configuration here */
403 Init_Server_Struct( &Conf_Server[i] );
405 /* Set time for next connect attempt */
407 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
408 /* The connection has been "long", so we don't
409 * require the next attempt to be delayed. */
410 Conf_Server[i].lasttry =
411 t - Conf_ConnectRetry + RECONNECT_DELAY;
413 Conf_Server[i].lasttry = t;
416 } /* Conf_UnsetServer */
420 Conf_SetServer( int ConfServer, CONN_ID Idx )
422 /* Set connection for specified configured server */
424 assert( ConfServer > NONE );
425 assert( Idx > NONE );
427 Conf_Server[ConfServer].conn_id = Idx;
428 } /* Conf_SetServer */
432 Conf_GetServer( CONN_ID Idx )
434 /* Get index of server in configuration structure */
438 assert( Idx > NONE );
440 for( i = 0; i < MAX_SERVERS; i++ ) {
441 if( Conf_Server[i].conn_id == Idx ) return i;
444 } /* Conf_GetServer */
448 Conf_EnableServer( const char *Name, UINT16 Port )
450 /* Enable specified server and adjust port */
454 assert( Name != NULL );
456 for( i = 0; i < MAX_SERVERS; i++ ) {
457 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
458 /* Gotcha! Set port and enable server: */
459 Conf_Server[i].port = Port;
460 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
461 return (Conf_Server[i].port && Conf_Server[i].host[0]);
465 } /* Conf_EnableServer */
469 Conf_EnablePassiveServer(const char *Name)
471 /* Enable specified server */
474 assert( Name != NULL );
475 for (i = 0; i < MAX_SERVERS; i++) {
476 if ((strcasecmp( Conf_Server[i].name, Name ) == 0) && (Conf_Server[i].port > 0)) {
477 /* BINGO! Enable server */
478 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
483 } /* Conf_EnablePassiveServer */
487 Conf_DisableServer( const char *Name )
489 /* Enable specified server and adjust port */
493 assert( Name != NULL );
495 for( i = 0; i < MAX_SERVERS; i++ ) {
496 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
497 /* Gotcha! Disable and disconnect server: */
498 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
499 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
504 } /* Conf_DisableServer */
508 Conf_AddServer( const char *Name, UINT16 Port, const char *Host, const char *MyPwd, const char *PeerPwd )
510 /* Add new server to configuration */
514 assert( Name != NULL );
515 assert( Host != NULL );
516 assert( MyPwd != NULL );
517 assert( PeerPwd != NULL );
519 /* Search unused item in server configuration structure */
520 for( i = 0; i < MAX_SERVERS; i++ ) {
521 /* Is this item used? */
522 if( ! Conf_Server[i].name[0] ) break;
524 if( i >= MAX_SERVERS ) return false;
526 Init_Server_Struct( &Conf_Server[i] );
527 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
528 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
529 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
530 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
531 Conf_Server[i].port = Port;
532 Conf_Server[i].flags = CONF_SFLAG_ONCE;
535 } /* Conf_AddServer */
539 * Check if the given nick name is an service
542 Conf_IsService(int ConfServer, const char *Nick)
544 return MatchCaseInsensitive(Conf_Server[ConfServer].svs_mask, Nick);
545 } /* Conf_IsService */
549 * Initialize configuration settings with their default values.
552 Set_Defaults(bool InitServers)
556 strcpy(Conf_ServerName, "");
557 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
558 PACKAGE_NAME, PACKAGE_VERSION);
559 strcpy(Conf_ServerPwd, "");
561 strcpy(Conf_ServerAdmin1, "");
562 strcpy(Conf_ServerAdmin2, "");
563 strcpy(Conf_ServerAdminMail, "");
565 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
566 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
567 strlcpy(Conf_MotdPhrase, MOTD_PHRASE, sizeof(Conf_MotdPhrase));
569 Conf_UID = Conf_GID = 0;
570 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
571 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
573 free(Conf_ListenAddress);
574 Conf_ListenAddress = NULL;
576 Conf_PingTimeout = 120;
577 Conf_PongTimeout = 20;
578 Conf_ConnectRetry = 60;
580 Conf_NoIdent = false;
583 Conf_Channel_Count = 0;
585 Conf_OperCanMode = false;
586 Conf_OperServerMode = false;
587 Conf_AllowRemoteOper = false;
588 Conf_PredefChannelsOnly = false;
590 Conf_ConnectIPv4 = true;
591 Conf_ConnectIPv6 = true;
593 Conf_MaxConnections = 0;
594 Conf_MaxConnectionsIP = 5;
596 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
598 /* Initialize server configuration structures */
600 for (i = 0; i < MAX_SERVERS;
601 Init_Server_Struct(&Conf_Server[i++]));
609 size_t cnt = array_bytes(&Conf_ListenPorts);
611 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
617 Read_Config( bool ngircd_starting )
619 /* Read configuration file. */
621 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
622 const UINT16 defaultport = 6667;
626 /* Open configuration file */
627 fd = fopen( NGIRCd_ConfFile, "r" );
629 /* No configuration file found! */
630 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
631 NGIRCd_ConfFile, strerror( errno ));
632 if (!ngircd_starting)
634 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
639 Set_Defaults( ngircd_starting );
641 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
643 /* Clean up server configuration structure: mark all already
644 * configured servers as "once" so that they are deleted
645 * after the next disconnect and delete all unused servers.
646 * And delete all servers which are "duplicates" of servers
647 * that are already marked as "once" (such servers have been
648 * created by the last rehash but are now useless). */
649 for( i = 0; i < MAX_SERVERS; i++ ) {
650 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
652 /* This structure is in use ... */
653 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
654 /* Check for duplicates */
655 for( n = 0; n < MAX_SERVERS; n++ ) {
656 if( n == i ) continue;
658 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
659 Init_Server_Struct( &Conf_Server[n] );
661 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
667 /* Mark server as "once" */
668 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
669 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
674 /* Initialize variables */
676 strcpy( section, "" );
677 Init_Server_Struct( &New_Server );
678 New_Server_Idx = NONE;
682 /* Read configuration file */
684 if( ! fgets( str, LINE_LEN, fd )) break;
688 /* Skip comments and empty lines */
689 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
691 /* Is this the beginning of a new section? */
692 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
693 strlcpy( section, str, sizeof( section ));
694 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
697 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
698 /* Check if there is already a server to add */
699 if( New_Server.name[0] ) {
700 /* Copy data to "real" server structure */
701 assert( New_Server_Idx > NONE );
702 Conf_Server[New_Server_Idx] = New_Server;
705 /* Re-init structure for new server */
706 Init_Server_Struct( &New_Server );
708 /* Search unused item in server configuration structure */
709 for( i = 0; i < MAX_SERVERS; i++ ) {
710 /* Is this item used? */
711 if( ! Conf_Server[i].name[0] ) break;
713 if( i >= MAX_SERVERS ) {
714 /* Oops, no free item found! */
715 Config_Error( LOG_ERR, "Too many servers configured." );
716 New_Server_Idx = NONE;
718 else New_Server_Idx = i;
721 if (strcasecmp(section, "[CHANNEL]") == 0) {
722 Conf_Channel_Count++;
725 if (strcasecmp(section, "[OPERATOR]") == 0) {
730 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
733 if( section[0] == 0x1 ) continue;
735 /* Split line into variable name and parameters */
736 ptr = strchr( str, '=' );
738 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
742 var = str; ngt_TrimStr( var );
743 arg = ptr + 1; ngt_TrimStr( arg );
745 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
746 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
747 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
748 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
749 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
752 /* Close configuration file */
755 /* Check if there is still a server to add */
756 if( New_Server.name[0] ) {
757 /* Copy data to "real" server structure */
758 assert( New_Server_Idx > NONE );
759 Conf_Server[New_Server_Idx] = New_Server;
762 /* not a single listening port? Add default. */
763 if (no_listenports() &&
764 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
766 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
767 (unsigned int) defaultport, strerror(errno));
772 if (!Conf_ListenAddress)
773 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
775 if (!Conf_ListenAddress) {
776 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
784 Check_ArgIsTrue( const char *Arg )
786 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
787 if( strcasecmp( Arg, "true" ) == 0 ) return true;
788 if( atoi( Arg ) != 0 ) return true;
791 } /* Check_ArgIsTrue */
794 static unsigned int Handle_MaxNickLength(int Line, const char *Arg)
798 new = (unsigned) atoi(Arg) + 1;
799 if (new > CLIENT_NICK_LEN) {
800 Config_Error(LOG_WARNING,
801 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
802 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
803 return CLIENT_NICK_LEN;
806 Config_Error(LOG_WARNING,
807 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
808 NGIRCd_ConfFile, Line);
812 } /* Handle_MaxNickLength */
816 Handle_GLOBAL( int Line, char *Var, char *Arg )
823 assert( Var != NULL );
824 assert( Arg != NULL );
826 if( strcasecmp( Var, "Name" ) == 0 ) {
828 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
829 if (len >= sizeof( Conf_ServerName ))
830 Config_Error_TooLong( Line, Var );
833 if( strcasecmp( Var, "Info" ) == 0 ) {
834 /* Info text of server */
835 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
836 if (len >= sizeof( Conf_ServerInfo ))
837 Config_Error_TooLong ( Line, Var );
840 if( strcasecmp( Var, "Password" ) == 0 ) {
841 /* Global server password */
842 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
843 if (len >= sizeof( Conf_ServerPwd ))
844 Config_Error_TooLong( Line, Var );
847 if (strcasecmp(Var, "WebircPassword") == 0) {
848 /* Password required for WEBIRC command */
849 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
850 if (len >= sizeof(Conf_WebircPwd))
851 Config_Error_TooLong(Line, Var);
854 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
855 /* Administrative info #1 */
856 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
857 if (len >= sizeof( Conf_ServerAdmin1 ))
858 Config_Error_TooLong ( Line, Var );
861 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
862 /* Administrative info #2 */
863 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
864 if (len >= sizeof( Conf_ServerAdmin2 ))
865 Config_Error_TooLong ( Line, Var );
868 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
869 /* Administrative email contact */
870 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
871 if (len >= sizeof( Conf_ServerAdminMail ))
872 Config_Error_TooLong( Line, Var );
876 if( strcasecmp( Var, "Ports" ) == 0 ) {
877 ports_parse(&Conf_ListenPorts, Line, Arg);
880 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
881 /* "Message of the day" (MOTD) file */
882 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
883 if (len >= sizeof( Conf_MotdFile ))
884 Config_Error_TooLong( Line, Var );
887 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
888 /* "Message of the day" phrase (instead of file) */
889 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
890 if (len >= sizeof( Conf_MotdPhrase ))
891 Config_Error_TooLong( Line, Var );
894 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
895 /* directory for chroot() */
896 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
897 if (len >= sizeof( Conf_Chroot ))
898 Config_Error_TooLong( Line, Var );
901 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
902 /* name of pidfile */
903 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
904 if (len >= sizeof( Conf_PidFile ))
905 Config_Error_TooLong( Line, Var );
908 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
909 /* UID the daemon should switch to */
910 pwd = getpwnam( Arg );
911 if( pwd ) Conf_UID = pwd->pw_uid;
914 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
917 Conf_UID = (unsigned int)atoi( Arg );
921 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
922 /* GID the daemon should use */
923 grp = getgrnam( Arg );
924 if( grp ) Conf_GID = grp->gr_gid;
927 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
930 Conf_GID = (unsigned int)atoi( Arg );
934 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
936 Conf_PingTimeout = atoi( Arg );
937 if( Conf_PingTimeout < 5 ) {
938 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
939 NGIRCd_ConfFile, Line );
940 Conf_PingTimeout = 5;
944 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
946 Conf_PongTimeout = atoi( Arg );
947 if( Conf_PongTimeout < 5 ) {
948 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
949 NGIRCd_ConfFile, Line );
950 Conf_PongTimeout = 5;
954 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
955 /* Seconds between connection attempts to other servers */
956 Conf_ConnectRetry = atoi( Arg );
957 if( Conf_ConnectRetry < 5 ) {
958 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
959 NGIRCd_ConfFile, Line );
960 Conf_ConnectRetry = 5;
964 if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
965 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
966 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
969 if( strcasecmp( Var, "NoDNS" ) == 0 ) {
970 /* don't do reverse dns lookups when clients connect? */
971 Conf_NoDNS = Check_ArgIsTrue( Arg );
974 if (strcasecmp(Var, "NoIdent") == 0) {
975 /* don't do IDENT lookups when clients connect? */
976 Conf_NoIdent = Check_ArgIsTrue(Arg);
979 /* user has enabled ident lookups explicitly, but ... */
980 Config_Error(LOG_WARNING,
981 "%s: line %d: NoIdent=False, but ngircd was built without IDENT support",
982 NGIRCd_ConfFile, Line);
988 /* the default setting for all the WANT_IPV6 special options is 'true' */
989 if( strcasecmp( Var, "ConnectIPv6" ) == 0 ) {
990 /* connect to other hosts using ipv6, if they have an AAAA record? */
991 Conf_ConnectIPv6 = Check_ArgIsTrue( Arg );
994 if( strcasecmp( Var, "ConnectIPv4" ) == 0 ) {
995 /* connect to other hosts using ipv4.
996 * again, this can be used for ipv6-only setups */
997 Conf_ConnectIPv4 = Check_ArgIsTrue( Arg );
1001 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
1002 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
1003 Conf_OperCanMode = Check_ArgIsTrue( Arg );
1006 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
1007 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
1008 Conf_OperServerMode = Check_ArgIsTrue( Arg );
1011 if(strcasecmp(Var, "AllowRemoteOper") == 0) {
1012 /* Are remote IRC operators allowed to control this server? */
1013 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1016 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
1017 /* Maximum number of connections. 0 -> "no limit". */
1019 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
1022 Conf_MaxConnections = atol( Arg );
1025 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
1026 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
1028 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
1031 Conf_MaxConnectionsIP = atoi( Arg );
1034 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
1035 /* Maximum number of channels a user can join. 0 -> "no limit". */
1037 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
1040 Conf_MaxJoins = atoi( Arg );
1043 if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
1044 /* Maximum length of a nick name; must be same on all servers
1045 * within the IRC network! */
1046 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1050 if( strcasecmp( Var, "Listen" ) == 0 ) {
1051 /* IP-Address to bind sockets */
1052 if (Conf_ListenAddress) {
1053 Config_Error(LOG_ERR, "Multiple Listen= options, ignoring: %s", Arg);
1056 Conf_ListenAddress = strdup_warn(Arg);
1058 * if allocation fails, we're in trouble:
1059 * we cannot ignore the error -- otherwise ngircd
1060 * would listen on all interfaces.
1062 if (!Conf_ListenAddress) {
1063 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
1070 if( strcasecmp( Var, "SSLPorts" ) == 0 ) {
1071 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1075 if( strcasecmp( Var, "SSLKeyFile" ) == 0 ) {
1076 assert(Conf_SSLOptions.KeyFile == NULL );
1077 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1080 if( strcasecmp( Var, "SSLCertFile" ) == 0 ) {
1081 assert(Conf_SSLOptions.CertFile == NULL );
1082 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1086 if( strcasecmp( Var, "SSLKeyFilePassword" ) == 0 ) {
1087 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1088 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1089 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Could not copy %s: %s!",
1090 NGIRCd_ConfFile, Line, Var, strerror(errno));
1093 if( strcasecmp( Var, "SSLDHFile" ) == 0 ) {
1094 assert(Conf_SSLOptions.DHFile == NULL);
1095 Conf_SSLOptions.DHFile = strdup_warn( Arg );
1099 Config_Error(LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
1100 NGIRCd_ConfFile, Line, Var);
1101 } /* Handle_GLOBAL */
1105 Handle_OPERATOR( int Line, char *Var, char *Arg )
1108 struct Conf_Oper *op;
1111 assert( Var != NULL );
1112 assert( Arg != NULL );
1113 assert( Conf_Oper_Count > 0 );
1115 op = array_alloc(&Conf_Opers, sizeof(*op), Conf_Oper_Count - 1);
1117 Config_Error(LOG_ERR, "Could not allocate memory for operator (%d:%s = %s)", Line, Var, Arg);
1121 if (strcasecmp(Var, "Name") == 0) {
1122 /* Name of IRC operator */
1123 len = strlcpy(op->name, Arg, sizeof(op->name));
1124 if (len >= sizeof(op->name))
1125 Config_Error_TooLong(Line, Var);
1128 if (strcasecmp(Var, "Password") == 0) {
1129 /* Password of IRC operator */
1130 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1131 if (len >= sizeof(op->pwd))
1132 Config_Error_TooLong(Line, Var);
1135 if (strcasecmp(Var, "Mask") == 0) {
1137 return; /* Hostname already configured */
1138 op->mask = strdup_warn( Arg );
1141 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
1142 NGIRCd_ConfFile, Line, Var );
1143 } /* Handle_OPERATOR */
1147 Handle_SERVER( int Line, char *Var, char *Arg )
1153 assert( Var != NULL );
1154 assert( Arg != NULL );
1156 /* Ignore server block if no space is left in server configuration structure */
1157 if( New_Server_Idx <= NONE ) return;
1159 if( strcasecmp( Var, "Host" ) == 0 ) {
1160 /* Hostname of the server */
1161 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1162 if (len >= sizeof( New_Server.host ))
1163 Config_Error_TooLong ( Line, Var );
1166 if( strcasecmp( Var, "Name" ) == 0 ) {
1167 /* Name of the server ("Nick"/"ID") */
1168 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1169 if (len >= sizeof( New_Server.name ))
1170 Config_Error_TooLong( Line, Var );
1173 if (strcasecmp(Var, "Bind") == 0) {
1174 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1177 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1178 NGIRCd_ConfFile, Line, Arg);
1181 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1182 /* Password of this server which is sent to the peer */
1184 Config_Error(LOG_ERR,
1185 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1186 NGIRCd_ConfFile, Line);
1188 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1189 if (len >= sizeof( New_Server.pwd_in ))
1190 Config_Error_TooLong( Line, Var );
1193 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1194 /* Passwort of the peer which must be received */
1195 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1196 if (len >= sizeof( New_Server.pwd_out ))
1197 Config_Error_TooLong( Line, Var );
1200 if( strcasecmp( Var, "Port" ) == 0 ) {
1201 /* Port to which this server should connect */
1203 if( port > 0 && port < 0xFFFF )
1204 New_Server.port = (UINT16)port;
1206 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
1207 NGIRCd_ConfFile, Line, port );
1211 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1212 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1216 if( strcasecmp( Var, "Group" ) == 0 ) {
1219 if( ! isdigit( (int)*Arg ))
1220 Config_Error_NaN( Line, Var );
1223 New_Server.group = atoi( Arg );
1226 if( strcasecmp( Var, "Passive" ) == 0 ) {
1227 if (Check_ArgIsTrue(Arg))
1228 New_Server.flags |= CONF_SFLAG_DISABLED;
1231 if (strcasecmp(Var, "ServiceMask") == 0) {
1232 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1233 sizeof(New_Server.svs_mask));
1234 if (len >= sizeof(New_Server.svs_mask))
1235 Config_Error_TooLong(Line, Var);
1239 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
1240 NGIRCd_ConfFile, Line, Var );
1241 } /* Handle_SERVER */
1245 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1247 size_t size = sizeof(new_chan->name);
1248 char *dest = new_chan->name;
1250 if (!Channel_IsValidName(name)) {
1252 * maybe user forgot to add a '#'.
1253 * This is only here for user convenience.
1259 return size > strlcpy(dest, name, size);
1264 Handle_CHANNEL(int Line, char *Var, char *Arg)
1268 struct Conf_Channel *chan;
1271 assert( Var != NULL );
1272 assert( Arg != NULL );
1273 assert(Conf_Channel_Count > 0);
1275 chancount = Conf_Channel_Count - 1;
1277 chan = array_alloc(&Conf_Channels, sizeof(*chan), chancount);
1279 Config_Error(LOG_ERR, "Could not allocate memory for predefined channel (%d:%s = %s)", Line, Var, Arg);
1282 if (strcasecmp(Var, "Name") == 0) {
1283 if (!Handle_Channelname(chan, Arg))
1284 Config_Error_TooLong(Line, Var);
1287 if (strcasecmp(Var, "Modes") == 0) {
1289 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1290 if (len >= sizeof(chan->modes))
1291 Config_Error_TooLong( Line, Var );
1294 if( strcasecmp( Var, "Topic" ) == 0 ) {
1296 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1297 if (len >= sizeof(chan->topic))
1298 Config_Error_TooLong( Line, Var );
1301 if( strcasecmp( Var, "Key" ) == 0 ) {
1302 /* Initial Channel Key (mode k) */
1303 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1304 if (len >= sizeof(chan->key))
1305 Config_Error_TooLong(Line, Var);
1308 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1309 /* maximum user limit, mode l */
1310 chan->maxusers = (unsigned long) atol(Arg);
1311 if (chan->maxusers == 0)
1312 Config_Error_NaN(Line, Var);
1315 if (strcasecmp(Var, "KeyFile") == 0) {
1317 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1318 if (len >= sizeof(chan->keyfile))
1319 Config_Error_TooLong(Line, Var);
1323 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1324 NGIRCd_ConfFile, Line, Var );
1325 } /* Handle_CHANNEL */
1329 Validate_Config(bool Configtest, bool Rehash)
1331 /* Validate configuration settings. */
1334 int i, servers, servers_once;
1336 bool config_valid = true;
1339 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1340 ptr = Conf_ServerName;
1342 if (*ptr >= 'a' && *ptr <= 'z') continue;
1343 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1344 if (*ptr >= '0' && *ptr <= '9') continue;
1345 if (ptr > Conf_ServerName) {
1346 if (*ptr == '.' || *ptr == '-')
1349 Conf_ServerName[0] = '\0';
1353 if (!Conf_ServerName[0]) {
1354 /* No server name configured! */
1355 config_valid = false;
1356 Config_Error(LOG_ALERT,
1357 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1359 if (!Configtest && !Rehash) {
1360 Config_Error(LOG_ALERT,
1361 "%s exiting due to fatal errors!",
1367 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1368 /* No dot in server name! */
1369 config_valid = false;
1370 Config_Error(LOG_ALERT,
1371 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1374 Config_Error(LOG_ALERT,
1375 "%s exiting due to fatal errors!",
1382 if (!Conf_ServerAdminMail[0]) {
1383 /* No administrative contact configured! */
1384 config_valid = false;
1385 Config_Error(LOG_ALERT,
1386 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1389 Config_Error(LOG_ALERT,
1390 "%s exiting due to fatal errors!",
1397 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1398 && !Conf_ServerAdminMail[0]) {
1399 /* No administrative information configured! */
1400 Config_Error(LOG_WARNING,
1401 "No administrative information configured but required by RFC!");
1405 servers = servers_once = 0;
1406 for (i = 0; i < MAX_SERVERS; i++) {
1407 if (Conf_Server[i].name[0]) {
1409 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1414 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1415 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1418 return config_valid;
1419 } /* Validate_Config */
1423 Config_Error_TooLong ( const int Line, const char *Item )
1425 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1430 Config_Error_NaN( const int Line, const char *Item )
1432 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1433 NGIRCd_ConfFile, Line, Item );
1438 static void Config_Error( const int Level, const char *Format, ... )
1440 static void Config_Error( Level, Format, va_alist )
1446 /* Error! Write to console and/or logfile. */
1448 char msg[MAX_LOG_MSG_LEN];
1451 assert( Format != NULL );
1454 va_start( ap, Format );
1458 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1461 /* During "normal operations" the log functions of the daemon should
1462 * be used, but during testing of the configuration file, all messages
1463 * should go directly to the console: */
1464 if (Use_Log) Log( Level, "%s", msg );
1466 } /* Config_Error */
1470 Init_Server_Struct( CONF_SERVER *Server )
1472 /* Initialize server configuration structur to default values */
1474 assert( Server != NULL );
1476 memset( Server, 0, sizeof (CONF_SERVER) );
1478 Server->group = NONE;
1479 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1481 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1483 Proc_InitStruct(&Server->res_stat);
1484 Server->conn_id = NONE;
1485 memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
1486 } /* Init_Server_Struct */