2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
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.
16 * Configuration management (reading, parsing & validation)
35 #include <sys/types.h>
52 static bool Use_Log = true, Using_MotdFile = true;
53 static CONF_SERVER New_Server;
54 static int New_Server_Idx;
56 static char Conf_MotdFile[FNAME_LEN];
57 static char Conf_HelpFile[FNAME_LEN];
59 static void Set_Defaults PARAMS(( bool InitServers ));
60 static bool Read_Config PARAMS(( bool TestOnly, bool IsStarting ));
61 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
63 static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
64 static void Handle_LIMITS PARAMS(( int Line, char *Var, char *Arg ));
65 static void Handle_OPTIONS PARAMS(( int Line, char *Var, char *Arg ));
66 static void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
67 static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
68 static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
70 static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
72 static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
73 static void Config_Error_Section PARAMS(( const int Line, const char *Item,
74 const char *Section ));
75 static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
77 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
81 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
83 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
89 static void Handle_SSL PARAMS(( int Line, char *Var, char *Ark ));
91 struct SSLOptions Conf_SSLOptions;
94 * Initialize SSL configuration.
99 free(Conf_SSLOptions.KeyFile);
100 Conf_SSLOptions.KeyFile = NULL;
102 free(Conf_SSLOptions.CertFile);
103 Conf_SSLOptions.CertFile = NULL;
105 free(Conf_SSLOptions.DHFile);
106 Conf_SSLOptions.DHFile = NULL;
107 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
109 array_free(&Conf_SSLOptions.ListenPorts);
113 * Check if the current configuration uses/requires SSL.
115 * @returns true if SSL is used and should be initialized.
122 /* SSL listen ports configured? */
123 if (array_bytes(&Conf_SSLOptions.ListenPorts))
126 for (i = 0; i < MAX_SERVERS; i++) {
127 if (Conf_Server[i].port > 0
128 && Conf_Server[i].SSLConnect)
135 * Make sure that a configured file is readable.
137 * Currently, this function is only used for SSL-related options ...
139 * @param Var Configuration variable
140 * @param Filename Configured filename
143 CheckFileReadable(const char *Var, const char *Filename)
150 fp = fopen(Filename, "r");
154 Config_Error(LOG_ERR, "Can't read \"%s\" (\"%s\"): %s",
155 Filename, Var, strerror(errno));
162 * Duplicate string and warn on errors.
164 * @returns Pointer to string on success, NULL otherwise.
167 strdup_warn(const char *str)
169 char *ptr = strdup(str);
171 Config_Error(LOG_ERR,
172 "Could not allocate memory for string: %s", str);
177 * Output a comma separated list of ports (integer values).
184 len = array_length(a, sizeof(UINT16));
186 ports = (UINT16*) array_start(a);
187 printf("%u", (unsigned int) *ports);
190 printf(", %u", (unsigned int) *ports);
197 * Parse a comma separated string into an array of port numbers (integers).
200 ports_parse(array *a, int Line, char *Arg)
208 ptr = strtok( Arg, "," );
212 if (port > 0 && port < 0xFFFF) {
213 port16 = (UINT16) port;
214 if (!array_catb(a, (char*)&port16, sizeof port16))
215 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
216 NGIRCd_ConfFile, Line, port, strerror(errno));
218 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
219 NGIRCd_ConfFile, Line, port );
222 ptr = strtok( NULL, "," );
227 * Initialize configuration module.
232 Read_Config(false, true);
233 Validate_Config(false, false);
237 * "Rehash" (reload) server configuration.
239 * @returns true if configuration has been re-read, false on errors.
244 if (!Read_Config(false, false))
246 Validate_Config(false, true);
248 /* Update CLIENT structure of local server */
249 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
254 * Output a boolean value as "yes/no" string.
257 yesno_to_str(int boolean_value)
265 * Free all IRC operator configuration structures.
270 struct Conf_Oper *op;
273 len = array_length(&Conf_Opers, sizeof(*op));
274 op = array_start(&Conf_Opers);
279 array_free(&Conf_Opers);
283 * Output all IRC operator configuration structures.
288 struct Conf_Oper *op;
291 count = array_length(&Conf_Opers, sizeof(*op));
292 op = array_start(&Conf_Opers);
293 for (i = 0; i < count; i++, op++) {
298 printf(" Name = %s\n", op->name);
299 printf(" Password = %s\n", op->pwd);
300 printf(" Mask = %s\n\n", op->mask ? op->mask : "");
305 * Read configuration, validate and output it.
307 * This function waits for a keypress of the user when stdin/stdout are valid
308 * tty's ("you can read our nice message and we can read in your keypress").
310 * @return 0 on succes, 1 on failure(s); therefore the result code can
311 * directly be used by exit() when running "ngircd --configtest".
320 size_t predef_channel_count;
321 struct Conf_Channel *predef_chan;
325 if (!Read_Config(true, true))
328 config_valid = Validate_Config(true, false);
331 if(isatty(fileno(stdin)) && isatty(fileno(stdout))) {
332 puts("OK, press enter to see a dump of your server configuration ...");
335 puts("Ok, dump of your server configuration follows:\n");
338 printf(" Name = %s\n", Conf_ServerName);
339 printf(" AdminInfo1 = %s\n", Conf_ServerAdmin1);
340 printf(" AdminInfo2 = %s\n", Conf_ServerAdmin2);
341 printf(" AdminEMail = %s\n", Conf_ServerAdminMail);
342 printf(" HelpFile = %s\n", Conf_HelpFile);
343 printf(" Info = %s\n", Conf_ServerInfo);
344 printf(" Listen = %s\n", Conf_ListenAddress);
345 if (Using_MotdFile) {
346 printf(" MotdFile = %s\n", Conf_MotdFile);
347 printf(" MotdPhrase =\n");
349 printf(" MotdFile = \n");
350 printf(" MotdPhrase = %s\n", array_bytes(&Conf_Motd)
351 ? (const char*) array_start(&Conf_Motd) : "");
354 printf(" Password = %s\n", Conf_ServerPwd);
356 printf(" PidFile = %s\n", Conf_PidFile);
358 ports_puts(&Conf_ListenPorts);
359 grp = getgrgid(Conf_GID);
361 printf(" ServerGID = %s\n", grp->gr_name);
363 printf(" ServerGID = %ld\n", (long)Conf_GID);
364 pwd = getpwuid(Conf_UID);
366 printf(" ServerUID = %s\n", pwd->pw_name);
368 printf(" ServerUID = %ld\n", (long)Conf_UID);
372 printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
373 printf(" IdleTimeout = %d\n", Conf_IdleTimeout);
374 printf(" MaxConnections = %d\n", Conf_MaxConnections);
375 printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
376 printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
377 printf(" MaxNickLength = %u\n", Conf_MaxNickLength - 1);
378 printf(" MaxListSize = %d\n", Conf_MaxListSize);
379 printf(" PingTimeout = %d\n", Conf_PingTimeout);
380 printf(" PongTimeout = %d\n", Conf_PongTimeout);
384 printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
385 printf(" ChrootDir = %s\n", Conf_Chroot);
386 printf(" CloakHost = %s\n", Conf_CloakHost);
387 printf(" CloakHostModeX = %s\n", Conf_CloakHostModeX);
388 printf(" CloakHostSalt = %s\n", Conf_CloakHostSalt);
389 printf(" CloakUserToNick = %s\n", yesno_to_str(Conf_CloakUserToNick));
391 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
392 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
394 printf(" DNS = %s\n", yesno_to_str(Conf_DNS));
396 printf(" Ident = %s\n", yesno_to_str(Conf_Ident));
398 printf(" MorePrivacy = %s\n", yesno_to_str(Conf_MorePrivacy));
399 printf(" NoticeAuth = %s\n", yesno_to_str(Conf_NoticeAuth));
400 printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
401 printf(" OperChanPAutoOp = %s\n", yesno_to_str(Conf_OperChanPAutoOp));
402 printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
404 printf(" PAM = %s\n", yesno_to_str(Conf_PAM));
405 printf(" PAMIsOptional = %s\n", yesno_to_str(Conf_PAMIsOptional));
407 printf(" PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
409 printf(" RequireAuthPing = %s\n", yesno_to_str(Conf_AuthPing));
411 printf(" ScrubCTCP = %s\n", yesno_to_str(Conf_ScrubCTCP));
413 printf(" SyslogFacility = %s\n",
414 ngt_SyslogFacilityName(Conf_SyslogFacility));
416 printf(" WebircPassword = %s\n", Conf_WebircPwd);
421 printf(" CertFile = %s\n", Conf_SSLOptions.CertFile
422 ? Conf_SSLOptions.CertFile : "");
423 printf(" DHFile = %s\n", Conf_SSLOptions.DHFile
424 ? Conf_SSLOptions.DHFile : "");
425 printf(" KeyFile = %s\n", Conf_SSLOptions.KeyFile
426 ? Conf_SSLOptions.KeyFile : "");
427 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
428 puts(" KeyFilePassword = <secret>");
430 puts(" KeyFilePassword = ");
431 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
433 ports_puts(&Conf_SSLOptions.ListenPorts);
439 for( i = 0; i < MAX_SERVERS; i++ ) {
440 if( ! Conf_Server[i].name[0] ) continue;
442 /* Valid "Server" section */
444 printf( " Name = %s\n", Conf_Server[i].name );
445 printf( " Host = %s\n", Conf_Server[i].host );
446 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
448 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
450 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
451 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
452 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
453 printf( " Group = %d\n", Conf_Server[i].group );
454 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
457 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
458 predef_chan = array_start(&Conf_Channels);
460 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
461 if (!predef_chan->name[0])
464 /* Valid "Channel" section */
466 printf(" Name = %s\n", predef_chan->name);
467 printf(" Modes = %s\n", predef_chan->modes);
468 printf(" Key = %s\n", predef_chan->key);
469 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
470 printf(" Topic = %s\n", predef_chan->topic);
471 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
474 return (config_valid ? 0 : 1);
478 * Remove connection information from configured server.
480 * If the server is set as "once", delete it from our configuration;
481 * otherwise set the time for the next connection attempt.
483 * Non-server connections will be silently ignored.
486 Conf_UnsetServer( CONN_ID Idx )
491 /* Check all our configured servers */
492 for( i = 0; i < MAX_SERVERS; i++ ) {
493 if( Conf_Server[i].conn_id != Idx ) continue;
495 /* Gotcha! Mark server configuration as "unused": */
496 Conf_Server[i].conn_id = NONE;
498 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
499 /* Delete configuration here */
500 Init_Server_Struct( &Conf_Server[i] );
502 /* Set time for next connect attempt */
504 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
505 /* The connection has been "long", so we don't
506 * require the next attempt to be delayed. */
507 Conf_Server[i].lasttry =
508 t - Conf_ConnectRetry + RECONNECT_DELAY;
510 /* "Short" connection, enforce "ConnectRetry"
511 * but randomize it a little bit: 15 seconds. */
512 Conf_Server[i].lasttry =
513 t + rand() / (RAND_MAX / 15);
520 * Set connection information for specified configured server.
523 Conf_SetServer( int ConfServer, CONN_ID Idx )
525 assert( ConfServer > NONE );
526 assert( Idx > NONE );
528 if (Conf_Server[ConfServer].conn_id > NONE &&
529 Conf_Server[ConfServer].conn_id != Idx) {
531 "Connection %d: Server configuration of \"%s\" already in use by connection %d!",
532 Idx, Conf_Server[ConfServer].name,
533 Conf_Server[ConfServer].conn_id);
534 Conn_Close(Idx, NULL, "Server configuration already in use", true);
537 Conf_Server[ConfServer].conn_id = Idx;
542 * Get index of server in configuration structure.
545 Conf_GetServer( CONN_ID Idx )
549 assert( Idx > NONE );
551 for( i = 0; i < MAX_SERVERS; i++ ) {
552 if( Conf_Server[i].conn_id == Idx ) return i;
558 * Enable a server by name and adjust its port number.
560 * @returns true if a server has been enabled and now has a valid port
561 * number and host name for outgoing connections.
564 Conf_EnableServer( const char *Name, UINT16 Port )
568 assert( Name != NULL );
569 for( i = 0; i < MAX_SERVERS; i++ ) {
570 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
571 /* Gotcha! Set port and enable server: */
572 Conf_Server[i].port = Port;
573 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
574 return (Conf_Server[i].port && Conf_Server[i].host[0]);
581 * Enable a server by name.
583 * The server is only usable as outgoing server, if it has set a valid port
584 * number for outgoing connections!
585 * If not, you have to use Conf_EnableServer() function to make it available.
587 * @returns true if a server has been enabled; false otherwise.
590 Conf_EnablePassiveServer(const char *Name)
594 assert( Name != NULL );
595 for (i = 0; i < MAX_SERVERS; i++) {
596 if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
597 && (Conf_Server[i].port > 0)) {
598 /* BINGO! Enable server */
599 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
607 * Disable a server by name.
608 * An already established connection will be disconnected.
610 * @returns true if a server was found and has been disabled.
613 Conf_DisableServer( const char *Name )
617 assert( Name != NULL );
618 for( i = 0; i < MAX_SERVERS; i++ ) {
619 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
620 /* Gotcha! Disable and disconnect server: */
621 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
622 if( Conf_Server[i].conn_id > NONE )
623 Conn_Close(Conf_Server[i].conn_id, NULL,
624 "Server link terminated on operator request",
633 * Add a new remote server to our configuration.
635 * @param Name Name of the new server.
636 * @param Port Port number to connect to or 0 for incoming connections.
637 * @param Host Host name to connect to.
638 * @param MyPwd Password that will be sent to the peer.
639 * @param PeerPwd Password that must be received from the peer.
640 * @returns true if the new server has been added; false otherwise.
643 Conf_AddServer(const char *Name, UINT16 Port, const char *Host,
644 const char *MyPwd, const char *PeerPwd)
648 assert( Name != NULL );
649 assert( Host != NULL );
650 assert( MyPwd != NULL );
651 assert( PeerPwd != NULL );
653 /* Search unused item in server configuration structure */
654 for( i = 0; i < MAX_SERVERS; i++ ) {
655 /* Is this item used? */
656 if( ! Conf_Server[i].name[0] ) break;
658 if( i >= MAX_SERVERS ) return false;
660 Init_Server_Struct( &Conf_Server[i] );
661 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
662 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
663 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
664 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
665 Conf_Server[i].port = Port;
666 Conf_Server[i].flags = CONF_SFLAG_ONCE;
672 * Check if the given nickname is reserved for services on a particular server.
674 * @param ConfServer The server index to check.
675 * @param Nick The nickname to check.
676 * @returns true if the given nickname belongs to an "IRC service".
679 Conf_NickIsService(int ConfServer, const char *Nick)
681 assert (ConfServer >= 0);
682 assert (ConfServer < MAX_SERVERS);
684 return MatchCaseInsensitiveList(Conf_Server[ConfServer].svs_mask,
689 * Check if the given nickname is blocked for "normal client" use.
691 * @param ConfServer The server index or NONE to check all configured servers.
692 * @param Nick The nickname to check.
693 * @returns true if the given nickname belongs to an "IRC service".
696 Conf_NickIsBlocked(const char *Nick)
700 for(i = 0; i < MAX_SERVERS; i++) {
701 if (!Conf_Server[i].name[0])
703 if (Conf_NickIsService(i, Nick))
710 * Initialize configuration settings with their default values.
713 Set_Defaults(bool InitServers)
716 char random[RANDOM_SALT_LEN + 1];
719 strcpy(Conf_ServerName, "");
720 strcpy(Conf_ServerAdmin1, "");
721 strcpy(Conf_ServerAdmin2, "");
722 strcpy(Conf_ServerAdminMail, "");
723 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
724 PACKAGE_NAME, PACKAGE_VERSION);
725 free(Conf_ListenAddress);
726 Conf_ListenAddress = NULL;
727 array_free(&Conf_ListenPorts);
728 array_free(&Conf_Motd);
729 array_free(&Conf_Helptext);
730 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
731 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
732 strlcpy(Conf_HelpFile, DOCDIR, sizeof(Conf_HelpFile));
733 strlcat(Conf_HelpFile, HELP_FILE, sizeof(Conf_HelpFile));
734 strcpy(Conf_ServerPwd, "");
735 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
736 Conf_UID = Conf_GID = 0;
739 Conf_ConnectRetry = 60;
740 Conf_IdleTimeout = 0;
741 Conf_MaxConnections = 0;
742 Conf_MaxConnectionsIP = 5;
744 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
745 Conf_MaxListSize = 100;
746 Conf_PingTimeout = 120;
747 Conf_PongTimeout = 20;
750 Conf_AllowRemoteOper = false;
752 Conf_AuthPing = false;
754 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
755 strcpy(Conf_CloakHost, "");
756 strcpy(Conf_CloakHostModeX, "");
757 strlcpy(Conf_CloakHostSalt, ngt_RandomStr(random, RANDOM_SALT_LEN),
758 sizeof(Conf_CloakHostSalt));
759 Conf_CloakUserToNick = false;
760 Conf_ConnectIPv4 = true;
762 Conf_ConnectIPv6 = true;
764 Conf_ConnectIPv6 = false;
772 Conf_MorePrivacy = false;
773 Conf_NoticeAuth = false;
774 Conf_OperCanMode = false;
775 Conf_OperChanPAutoOp = true;
776 Conf_OperServerMode = false;
782 Conf_PAMIsOptional = false;
783 Conf_PredefChannelsOnly = false;
785 Conf_ScrubCTCP = false;
787 Conf_SyslogFacility = LOG_LOCAL5;
789 Conf_SyslogFacility = 0;
793 /* Initialize server configuration structures */
795 for (i = 0; i < MAX_SERVERS;
796 Init_Server_Struct(&Conf_Server[i++]));
801 * Get number of configured listening ports.
803 * @returns The number of ports (IPv4+IPv6) on which the server should listen.
808 size_t cnt = array_bytes(&Conf_ListenPorts);
810 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
816 * Read contents of a text file into an array.
818 * This function is used to read the MOTD and help text file, for exampe.
820 * @param filename Name of the file to read.
821 * @return true, when the file has been read in.
824 Read_TextFile(const char *Filename, const char *Name, array *Destination)
830 if (*Filename == '\0')
833 fp = fopen(Filename, "r");
835 Config_Error(LOG_ERR, "Can't read %s file \"%s\": %s",
836 Name, Filename, strerror(errno));
840 array_free(Destination);
841 while (fgets(line, (int)sizeof line, fp)) {
842 ngt_TrimLastChr(line, '\n');
844 /* add text including \0 */
845 if (!array_catb(Destination, line, strlen(line) + 1)) {
846 Log(LOG_ERR, "Cannot read/add \"%s\", line %d: %s",
847 Filename, line_no, strerror(errno));
857 * Read ngIRCd configuration file.
859 * Please note that this function uses exit(1) on fatal errors and therefore
860 * can result in ngIRCd terminating!
862 * @param ngircd_starting Flag indicating if ngIRCd is starting or not.
863 * @returns true when the configuration file has been read
864 * successfully; false otherwise.
867 Read_Config(bool TestOnly, bool IsStarting)
869 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
870 const UINT16 defaultport = 6667;
875 /* Open configuration file */
876 fd = fopen( NGIRCd_ConfFile, "r" );
878 /* No configuration file found! */
879 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
880 NGIRCd_ConfFile, strerror( errno ));
883 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
888 Set_Defaults(IsStarting);
891 Config_Error(LOG_INFO,
892 "Reading configuration from \"%s\" ...",
895 /* Clean up server configuration structure: mark all already
896 * configured servers as "once" so that they are deleted
897 * after the next disconnect and delete all unused servers.
898 * And delete all servers which are "duplicates" of servers
899 * that are already marked as "once" (such servers have been
900 * created by the last rehash but are now useless). */
901 for( i = 0; i < MAX_SERVERS; i++ ) {
902 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
904 /* This structure is in use ... */
905 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
906 /* Check for duplicates */
907 for( n = 0; n < MAX_SERVERS; n++ ) {
908 if( n == i ) continue;
910 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
911 Init_Server_Struct( &Conf_Server[n] );
913 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
919 /* Mark server as "once" */
920 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
921 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
926 /* Initialize variables */
928 strcpy( section, "" );
929 Init_Server_Struct( &New_Server );
930 New_Server_Idx = NONE;
934 /* Read configuration file */
936 if( ! fgets( str, LINE_LEN, fd )) break;
940 /* Skip comments and empty lines */
941 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
943 /* Is this the beginning of a new section? */
944 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
945 strlcpy( section, str, sizeof( section ));
946 if (strcasecmp(section, "[GLOBAL]") == 0
947 || strcasecmp(section, "[LIMITS]") == 0
948 || strcasecmp(section, "[OPTIONS]") == 0
950 || strcasecmp(section, "[SSL]") == 0
955 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
956 /* Check if there is already a server to add */
957 if( New_Server.name[0] ) {
958 /* Copy data to "real" server structure */
959 assert( New_Server_Idx > NONE );
960 Conf_Server[New_Server_Idx] = New_Server;
963 /* Re-init structure for new server */
964 Init_Server_Struct( &New_Server );
966 /* Search unused item in server configuration structure */
967 for( i = 0; i < MAX_SERVERS; i++ ) {
968 /* Is this item used? */
969 if( ! Conf_Server[i].name[0] ) break;
971 if( i >= MAX_SERVERS ) {
972 /* Oops, no free item found! */
973 Config_Error( LOG_ERR, "Too many servers configured." );
974 New_Server_Idx = NONE;
976 else New_Server_Idx = i;
980 if (strcasecmp(section, "[CHANNEL]") == 0) {
981 count = array_length(&Conf_Channels,
982 sizeof(struct Conf_Channel));
983 if (!array_alloc(&Conf_Channels,
984 sizeof(struct Conf_Channel),
986 Config_Error(LOG_ERR,
987 "Could not allocate memory for new operator (line %d)",
993 if (strcasecmp(section, "[OPERATOR]") == 0) {
994 count = array_length(&Conf_Opers,
995 sizeof(struct Conf_Oper));
996 if (!array_alloc(&Conf_Opers,
997 sizeof(struct Conf_Oper),
999 Config_Error(LOG_ERR,
1000 "Could not allocate memory for new channel (line &d)",
1006 Config_Error(LOG_ERR,
1007 "%s, line %d: Unknown section \"%s\"!",
1008 NGIRCd_ConfFile, line, section);
1011 if( section[0] == 0x1 ) continue;
1013 /* Split line into variable name and parameters */
1014 ptr = strchr( str, '=' );
1016 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
1020 var = str; ngt_TrimStr( var );
1021 arg = ptr + 1; ngt_TrimStr( arg );
1023 if(strcasecmp(section, "[GLOBAL]") == 0)
1024 Handle_GLOBAL(line, var, arg);
1025 else if(strcasecmp(section, "[LIMITS]") == 0)
1026 Handle_LIMITS(line, var, arg);
1027 else if(strcasecmp(section, "[OPTIONS]") == 0)
1028 Handle_OPTIONS(line, var, arg);
1030 else if(strcasecmp(section, "[SSL]") == 0)
1031 Handle_SSL(line, var, arg);
1033 else if(strcasecmp(section, "[OPERATOR]") == 0)
1034 Handle_OPERATOR(line, var, arg);
1035 else if(strcasecmp(section, "[SERVER]") == 0)
1036 Handle_SERVER(line, var, arg);
1037 else if(strcasecmp(section, "[CHANNEL]") == 0)
1038 Handle_CHANNEL(line, var, arg);
1040 Config_Error(LOG_ERR,
1041 "%s, line %d: Variable \"%s\" outside section!",
1042 NGIRCd_ConfFile, line, var);
1045 /* Close configuration file */
1048 /* Check if there is still a server to add */
1049 if( New_Server.name[0] ) {
1050 /* Copy data to "real" server structure */
1051 assert( New_Server_Idx > NONE );
1052 Conf_Server[New_Server_Idx] = New_Server;
1055 /* not a single listening port? Add default. */
1056 if (no_listenports() &&
1057 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
1059 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
1060 (unsigned int) defaultport, strerror(errno));
1065 if (!Conf_ListenAddress)
1066 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
1068 if (!Conf_ListenAddress) {
1069 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
1073 /* No MOTD phrase configured? (re)try motd file. */
1074 if (array_bytes(&Conf_Motd) == 0) {
1075 if (Read_TextFile(Conf_MotdFile, "MOTD", &Conf_Motd))
1076 Using_MotdFile = true;
1079 /* Try to read ngIRCd help text file. */
1080 (void)Read_TextFile(Conf_HelpFile, "help text", &Conf_Helptext);
1081 if (!array_bytes(&Conf_Helptext))
1082 Config_Error(LOG_WARNING,
1083 "No help text available, HELP command will be of limited use.");
1086 /* Make sure that all SSL-related files are readable */
1087 CheckFileReadable("CertFile", Conf_SSLOptions.CertFile);
1088 CheckFileReadable("DHFile", Conf_SSLOptions.DHFile);
1089 CheckFileReadable("KeyFile", Conf_SSLOptions.KeyFile);
1096 * Check whether a string argument is "true" or "false".
1098 * @param Arg Input string.
1099 * @returns true if the input string has been parsed as "yes", "true"
1100 * (case insensitive) or a non-zero integer value.
1103 Check_ArgIsTrue(const char *Arg)
1105 if (strcasecmp(Arg, "yes") == 0)
1107 if (strcasecmp(Arg, "true") == 0)
1116 * Handle setting of "MaxNickLength".
1118 * @param Line Line number in configuration file.
1119 * @raram Arg Input string.
1120 * @returns New configured maximum nickname length.
1123 Handle_MaxNickLength(int Line, const char *Arg)
1127 new = (unsigned) atoi(Arg) + 1;
1128 if (new > CLIENT_NICK_LEN) {
1129 Config_Error(LOG_WARNING,
1130 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
1131 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
1132 return CLIENT_NICK_LEN;
1135 Config_Error(LOG_WARNING,
1136 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
1137 NGIRCd_ConfFile, Line);
1144 * Output a warning messages if IDENT is configured but not compiled in.
1147 WarnIdent(int UNUSED Line)
1151 /* user has enabled ident lookups explicitly, but ... */
1152 Config_Error(LOG_WARNING,
1153 "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
1154 NGIRCd_ConfFile, Line);
1160 * Output a warning messages if IPv6 is configured but not compiled in.
1163 WarnIPv6(int UNUSED Line)
1166 if (Conf_ConnectIPv6) {
1167 /* user has enabled IPv6 explicitly, but ... */
1168 Config_Error(LOG_WARNING,
1169 "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
1170 NGIRCd_ConfFile, Line);
1176 * Output a warning messages if PAM is configured but not compiled in.
1179 WarnPAM(int UNUSED Line)
1183 Config_Error(LOG_WARNING,
1184 "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
1185 NGIRCd_ConfFile, Line);
1191 * Handle legacy "NoXXX" options in [GLOBAL] section.
1193 * TODO: This function and support for "NoXXX" could be removed starting
1194 * with ngIRCd release 19 (one release after marking it "deprecated").
1196 * @param Var Variable name.
1197 * @param Arg Argument string.
1198 * @returns true if a NoXXX option has been processed; false otherwise.
1201 CheckLegacyNoOption(const char *Var, const char *Arg)
1203 if(strcasecmp(Var, "NoDNS") == 0) {
1204 Conf_DNS = !Check_ArgIsTrue( Arg );
1207 if (strcasecmp(Var, "NoIdent") == 0) {
1208 Conf_Ident = !Check_ArgIsTrue(Arg);
1211 if(strcasecmp(Var, "NoPAM") == 0) {
1212 Conf_PAM = !Check_ArgIsTrue(Arg);
1219 * Handle deprecated legacy options in [GLOBAL] section.
1221 * TODO: This function and support for these options in the [Global] section
1222 * could be removed starting with ngIRCd release 19 (one release after
1223 * marking it "deprecated").
1225 * @param Var Variable name.
1226 * @param Arg Argument string.
1227 * @returns true if a legacy option has been processed; false otherwise.
1230 CheckLegacyGlobalOption(int Line, char *Var, char *Arg)
1232 if (strcasecmp(Var, "AllowRemoteOper") == 0
1233 || strcasecmp(Var, "ChrootDir") == 0
1234 || strcasecmp(Var, "ConnectIPv4") == 0
1235 || strcasecmp(Var, "ConnectIPv6") == 0
1236 || strcasecmp(Var, "OperCanUseMode") == 0
1237 || strcasecmp(Var, "OperChanPAutoOp") == 0
1238 || strcasecmp(Var, "OperServerMode") == 0
1239 || strcasecmp(Var, "PredefChannelsOnly") == 0
1240 || strcasecmp(Var, "SyslogFacility") == 0
1241 || strcasecmp(Var, "WebircPassword") == 0) {
1242 Handle_OPTIONS(Line, Var, Arg);
1245 if (strcasecmp(Var, "ConnectRetry") == 0
1246 || strcasecmp(Var, "IdleTimeout") == 0
1247 || strcasecmp(Var, "MaxConnections") == 0
1248 || strcasecmp(Var, "MaxConnectionsIP") == 0
1249 || strcasecmp(Var, "MaxJoins") == 0
1250 || strcasecmp(Var, "MaxNickLength") == 0
1251 || strcasecmp(Var, "PingTimeout") == 0
1252 || strcasecmp(Var, "PongTimeout") == 0) {
1253 Handle_LIMITS(Line, Var, Arg);
1257 if (strcasecmp(Var, "SSLCertFile") == 0
1258 || strcasecmp(Var, "SSLDHFile") == 0
1259 || strcasecmp(Var, "SSLKeyFile") == 0
1260 || strcasecmp(Var, "SSLKeyFilePassword") == 0
1261 || strcasecmp(Var, "SSLPorts") == 0) {
1262 Handle_SSL(Line, Var + 3, Arg);
1271 * Strip "no" prefix of a string.
1273 * TODO: This function and support for "NoXXX" should be removed starting
1274 * with ngIRCd release 19! (One release after marking it "deprecated").
1276 * @param str Pointer to input string starting with "no".
1277 * @returns New pointer to string without "no" prefix.
1280 NoNo(const char *str)
1282 assert(strncasecmp("no", str, 2) == 0 && str[2]);
1287 * Invert "boolean" string.
1289 * TODO: This function and support for "NoXXX" should be removed starting
1290 * with ngIRCd release 19! (One release after marking it "deprecated").
1292 * @param arg "Boolean" input string.
1293 * @returns Pointer to inverted "boolean string".
1296 InvertArg(const char *arg)
1298 return yesno_to_str(!Check_ArgIsTrue(arg));
1302 * Handle variable in [Global] configuration section.
1304 * @param Line Line numer in configuration file.
1305 * @param Var Variable name.
1306 * @param Arg Variable argument.
1309 Handle_GLOBAL( int Line, char *Var, char *Arg )
1314 const char *section;
1317 assert(Var != NULL);
1318 assert(Arg != NULL);
1320 if (strcasecmp(Var, "Name") == 0) {
1321 len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
1322 if (len >= sizeof(Conf_ServerName))
1323 Config_Error_TooLong(Line, Var);
1326 if (strcasecmp(Var, "AdminInfo1") == 0) {
1327 len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
1328 if (len >= sizeof(Conf_ServerAdmin1))
1329 Config_Error_TooLong(Line, Var);
1332 if (strcasecmp(Var, "AdminInfo2") == 0) {
1333 len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
1334 if (len >= sizeof(Conf_ServerAdmin2))
1335 Config_Error_TooLong(Line, Var);
1338 if (strcasecmp(Var, "AdminEMail") == 0) {
1339 len = strlcpy(Conf_ServerAdminMail, Arg,
1340 sizeof(Conf_ServerAdminMail));
1341 if (len >= sizeof(Conf_ServerAdminMail))
1342 Config_Error_TooLong(Line, Var);
1345 if (strcasecmp(Var, "Info") == 0) {
1346 len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
1347 if (len >= sizeof(Conf_ServerInfo))
1348 Config_Error_TooLong(Line, Var);
1351 if (strcasecmp(Var, "HelpFile") == 0) {
1352 len = strlcpy(Conf_HelpFile, Arg, sizeof(Conf_HelpFile));
1353 if (len >= sizeof(Conf_HelpFile))
1354 Config_Error_TooLong(Line, Var);
1357 if (strcasecmp(Var, "Listen") == 0) {
1358 if (Conf_ListenAddress) {
1359 Config_Error(LOG_ERR,
1360 "Multiple Listen= options, ignoring: %s",
1364 Conf_ListenAddress = strdup_warn(Arg);
1365 /* If allocation fails, we're in trouble: we cannot ignore the
1366 * error -- otherwise ngircd would listen on all interfaces. */
1367 if (!Conf_ListenAddress) {
1368 Config_Error(LOG_ALERT,
1369 "%s exiting due to fatal errors!",
1375 if (strcasecmp(Var, "MotdFile") == 0) {
1376 len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
1377 if (len >= sizeof(Conf_MotdFile))
1378 Config_Error_TooLong(Line, Var);
1381 if (strcasecmp(Var, "MotdPhrase") == 0) {
1385 if (len >= LINE_LEN) {
1386 Config_Error_TooLong(Line, Var);
1389 if (!array_copyb(&Conf_Motd, Arg, len + 1))
1390 Config_Error(LOG_WARNING,
1391 "%s, line %d: Could not append MotdPhrase: %s",
1392 NGIRCd_ConfFile, Line, strerror(errno));
1393 Using_MotdFile = false;
1396 if(strcasecmp(Var, "Password") == 0) {
1397 len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
1398 if (len >= sizeof(Conf_ServerPwd))
1399 Config_Error_TooLong(Line, Var);
1402 if (strcasecmp(Var, "PidFile") == 0) {
1403 len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
1404 if (len >= sizeof(Conf_PidFile))
1405 Config_Error_TooLong(Line, Var);
1408 if (strcasecmp(Var, "Ports") == 0) {
1409 ports_parse(&Conf_ListenPorts, Line, Arg);
1412 if (strcasecmp(Var, "ServerGID") == 0) {
1413 grp = getgrnam(Arg);
1415 Conf_GID = grp->gr_gid;
1417 Conf_GID = (unsigned int)atoi(Arg);
1418 if (!Conf_GID && strcmp(Arg, "0"))
1419 Config_Error(LOG_WARNING,
1420 "%s, line %d: Value of \"%s\" is not a valid group name or ID!",
1421 NGIRCd_ConfFile, Line, Var);
1425 if (strcasecmp(Var, "ServerUID") == 0) {
1426 pwd = getpwnam(Arg);
1428 Conf_UID = pwd->pw_uid;
1430 Conf_UID = (unsigned int)atoi(Arg);
1431 if (!Conf_UID && strcmp(Arg, "0"))
1432 Config_Error(LOG_WARNING,
1433 "%s, line %d: Value of \"%s\" is not a valid user name or ID!",
1434 NGIRCd_ConfFile, Line, Var);
1439 if (CheckLegacyNoOption(Var, Arg)) {
1440 /* TODO: This function and support for "NoXXX" could be
1441 * be removed starting with ngIRCd release 19 (one release
1442 * after marking it "deprecated"). */
1443 Config_Error(LOG_WARNING,
1444 "%s, line %d (section \"Global\"): \"No\"-Prefix is deprecated, use \"%s = %s\" in [Options] section!",
1445 NGIRCd_ConfFile, Line, NoNo(Var), InvertArg(Arg));
1446 if (strcasecmp(Var, "NoIdent") == 0)
1448 else if (strcasecmp(Var, "NoPam") == 0)
1452 if ((section = CheckLegacyGlobalOption(Line, Var, Arg))) {
1453 /** TODO: This function and support for these options in the
1454 * [Global] section could be removed starting with ngIRCd
1455 * release 19 (one release after marking it "deprecated"). */
1456 if (strncasecmp(Var, "SSL", 3) == 0) {
1457 Config_Error(LOG_WARNING,
1458 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s and rename to \"%s\"!",
1459 NGIRCd_ConfFile, Line, Var, section,
1462 Config_Error(LOG_WARNING,
1463 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s!",
1464 NGIRCd_ConfFile, Line, Var, section);
1469 Config_Error_Section(Line, Var, "Global");
1473 * Handle variable in [Limits] configuration section.
1475 * @param Line Line numer in configuration file.
1476 * @param Var Variable name.
1477 * @param Arg Variable argument.
1480 Handle_LIMITS(int Line, char *Var, char *Arg)
1483 assert(Var != NULL);
1484 assert(Arg != NULL);
1486 if (strcasecmp(Var, "ConnectRetry") == 0) {
1487 Conf_ConnectRetry = atoi(Arg);
1488 if (Conf_ConnectRetry < 5) {
1489 Config_Error(LOG_WARNING,
1490 "%s, line %d: Value of \"ConnectRetry\" too low!",
1491 NGIRCd_ConfFile, Line);
1492 Conf_ConnectRetry = 5;
1496 if (strcasecmp(Var, "IdleTimeout") == 0) {
1497 Conf_IdleTimeout = atoi(Arg);
1498 if (!Conf_IdleTimeout && strcmp(Arg, "0"))
1499 Config_Error_NaN(Line, Var);
1502 if (strcasecmp(Var, "MaxConnections") == 0) {
1503 Conf_MaxConnections = atoi(Arg);
1504 if (!Conf_MaxConnections && strcmp(Arg, "0"))
1505 Config_Error_NaN(Line, Var);
1508 if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
1509 Conf_MaxConnectionsIP = atoi(Arg);
1510 if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
1511 Config_Error_NaN(Line, Var);
1514 if (strcasecmp(Var, "MaxJoins") == 0) {
1515 Conf_MaxJoins = atoi(Arg);
1516 if (!Conf_MaxJoins && strcmp(Arg, "0"))
1517 Config_Error_NaN(Line, Var);
1520 if (strcasecmp(Var, "MaxNickLength") == 0) {
1521 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1524 if (strcasecmp(Var, "MaxListSize") == 0) {
1525 Conf_MaxListSize = atoi(Arg);
1526 if (!Conf_MaxListSize && strcmp(Arg, "0"))
1527 Config_Error_NaN(Line, Var);
1530 if (strcasecmp(Var, "PingTimeout") == 0) {
1531 Conf_PingTimeout = atoi(Arg);
1532 if (Conf_PingTimeout < 5) {
1533 Config_Error(LOG_WARNING,
1534 "%s, line %d: Value of \"PingTimeout\" too low!",
1535 NGIRCd_ConfFile, Line);
1536 Conf_PingTimeout = 5;
1540 if (strcasecmp(Var, "PongTimeout") == 0) {
1541 Conf_PongTimeout = atoi(Arg);
1542 if (Conf_PongTimeout < 5) {
1543 Config_Error(LOG_WARNING,
1544 "%s, line %d: Value of \"PongTimeout\" too low!",
1545 NGIRCd_ConfFile, Line);
1546 Conf_PongTimeout = 5;
1551 Config_Error_Section(Line, Var, "Limits");
1555 * Handle variable in [Options] configuration section.
1557 * @param Line Line numer in configuration file.
1558 * @param Var Variable name.
1559 * @param Arg Variable argument.
1562 Handle_OPTIONS(int Line, char *Var, char *Arg)
1567 assert(Var != NULL);
1568 assert(Arg != NULL);
1570 if (strcasecmp(Var, "AllowRemoteOper") == 0) {
1571 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1574 if (strcasecmp(Var, "ChrootDir") == 0) {
1575 len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
1576 if (len >= sizeof(Conf_Chroot))
1577 Config_Error_TooLong(Line, Var);
1580 if (strcasecmp(Var, "CloakHost") == 0) {
1581 len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
1582 if (len >= sizeof(Conf_CloakHost))
1583 Config_Error_TooLong(Line, Var);
1586 if (strcasecmp(Var, "CloakHostModeX") == 0) {
1587 len = strlcpy(Conf_CloakHostModeX, Arg, sizeof(Conf_CloakHostModeX));
1588 if (len >= sizeof(Conf_CloakHostModeX))
1589 Config_Error_TooLong(Line, Var);
1592 if (strcasecmp(Var, "CloakHostSalt") == 0) {
1593 len = strlcpy(Conf_CloakHostSalt, Arg, sizeof(Conf_CloakHostSalt));
1594 if (len >= sizeof(Conf_CloakHostSalt))
1595 Config_Error_TooLong(Line, Var);
1598 if (strcasecmp(Var, "CloakUserToNick") == 0) {
1599 Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
1602 if (strcasecmp(Var, "ConnectIPv6") == 0) {
1603 Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
1607 if (strcasecmp(Var, "ConnectIPv4") == 0) {
1608 Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
1611 if (strcasecmp(Var, "DNS") == 0) {
1612 Conf_DNS = Check_ArgIsTrue(Arg);
1615 if (strcasecmp(Var, "Ident") == 0) {
1616 Conf_Ident = Check_ArgIsTrue(Arg);
1620 if (strcasecmp(Var, "MorePrivacy") == 0) {
1621 Conf_MorePrivacy = Check_ArgIsTrue(Arg);
1624 if (strcasecmp(Var, "NoticeAuth") == 0) {
1625 Conf_NoticeAuth = Check_ArgIsTrue(Arg);
1628 if (strcasecmp(Var, "OperCanUseMode") == 0) {
1629 Conf_OperCanMode = Check_ArgIsTrue(Arg);
1632 if (strcasecmp(Var, "OperChanPAutoOp") == 0) {
1633 Conf_OperChanPAutoOp = Check_ArgIsTrue(Arg);
1636 if (strcasecmp(Var, "OperServerMode") == 0) {
1637 Conf_OperServerMode = Check_ArgIsTrue(Arg);
1640 if (strcasecmp(Var, "PAM") == 0) {
1641 Conf_PAM = Check_ArgIsTrue(Arg);
1645 if (strcasecmp(Var, "PAMIsOptional") == 0 ) {
1646 Conf_PAMIsOptional = Check_ArgIsTrue(Arg);
1649 if (strcasecmp(Var, "PredefChannelsOnly") == 0) {
1650 Conf_PredefChannelsOnly = Check_ArgIsTrue(Arg);
1654 if (strcasecmp(Var, "RequireAuthPing") == 0) {
1655 Conf_AuthPing = Check_ArgIsTrue(Arg);
1659 if (strcasecmp(Var, "ScrubCTCP") == 0) {
1660 Conf_ScrubCTCP = Check_ArgIsTrue(Arg);
1664 if (strcasecmp(Var, "SyslogFacility") == 0) {
1665 Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
1666 Conf_SyslogFacility);
1670 if (strcasecmp(Var, "WebircPassword") == 0) {
1671 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
1672 if (len >= sizeof(Conf_WebircPwd))
1673 Config_Error_TooLong(Line, Var);
1677 Config_Error_Section(Line, Var, "Options");
1683 * Handle variable in [SSL] configuration section.
1685 * @param Line Line numer in configuration file.
1686 * @param Var Variable name.
1687 * @param Arg Variable argument.
1690 Handle_SSL(int Line, char *Var, char *Arg)
1693 assert(Var != NULL);
1694 assert(Arg != NULL);
1696 if (strcasecmp(Var, "CertFile") == 0) {
1697 assert(Conf_SSLOptions.CertFile == NULL);
1698 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1701 if (strcasecmp(Var, "DHFile") == 0) {
1702 assert(Conf_SSLOptions.DHFile == NULL);
1703 Conf_SSLOptions.DHFile = strdup_warn(Arg);
1706 if (strcasecmp(Var, "KeyFile") == 0) {
1707 assert(Conf_SSLOptions.KeyFile == NULL);
1708 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1711 if (strcasecmp(Var, "KeyFilePassword") == 0) {
1712 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1713 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1714 Config_Error(LOG_ERR,
1715 "%s, line %d (section \"SSL\"): Could not copy %s: %s!",
1716 NGIRCd_ConfFile, Line, Var,
1720 if (strcasecmp(Var, "Ports") == 0) {
1721 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1725 Config_Error_Section(Line, Var, "SSL");
1731 * Handle variable in [Operator] configuration section.
1733 * @param Line Line numer in configuration file.
1734 * @param Var Variable name.
1735 * @param Arg Variable argument.
1738 Handle_OPERATOR( int Line, char *Var, char *Arg )
1741 struct Conf_Oper *op;
1744 assert( Var != NULL );
1745 assert( Arg != NULL );
1747 op = array_get(&Conf_Opers, sizeof(*op),
1748 array_length(&Conf_Opers, sizeof(*op)) - 1);
1752 if (strcasecmp(Var, "Name") == 0) {
1753 /* Name of IRC operator */
1754 len = strlcpy(op->name, Arg, sizeof(op->name));
1755 if (len >= sizeof(op->name))
1756 Config_Error_TooLong(Line, Var);
1759 if (strcasecmp(Var, "Password") == 0) {
1760 /* Password of IRC operator */
1761 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1762 if (len >= sizeof(op->pwd))
1763 Config_Error_TooLong(Line, Var);
1766 if (strcasecmp(Var, "Mask") == 0) {
1768 return; /* Hostname already configured */
1769 op->mask = strdup_warn( Arg );
1773 Config_Error_Section(Line, Var, "Operator");
1777 * Handle variable in [Server] configuration section.
1779 * @param Line Line numer in configuration file.
1780 * @param Var Variable name.
1781 * @param Arg Variable argument.
1784 Handle_SERVER( int Line, char *Var, char *Arg )
1790 assert( Var != NULL );
1791 assert( Arg != NULL );
1793 /* Ignore server block if no space is left in server configuration structure */
1794 if( New_Server_Idx <= NONE ) return;
1796 if( strcasecmp( Var, "Host" ) == 0 ) {
1797 /* Hostname of the server */
1798 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1799 if (len >= sizeof( New_Server.host ))
1800 Config_Error_TooLong ( Line, Var );
1803 if( strcasecmp( Var, "Name" ) == 0 ) {
1804 /* Name of the server ("Nick"/"ID") */
1805 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1806 if (len >= sizeof( New_Server.name ))
1807 Config_Error_TooLong( Line, Var );
1810 if (strcasecmp(Var, "Bind") == 0) {
1811 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1814 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1815 NGIRCd_ConfFile, Line, Arg);
1818 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1819 /* Password of this server which is sent to the peer */
1821 Config_Error(LOG_ERR,
1822 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1823 NGIRCd_ConfFile, Line);
1825 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1826 if (len >= sizeof( New_Server.pwd_in ))
1827 Config_Error_TooLong( Line, Var );
1830 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1831 /* Passwort of the peer which must be received */
1832 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1833 if (len >= sizeof( New_Server.pwd_out ))
1834 Config_Error_TooLong( Line, Var );
1837 if( strcasecmp( Var, "Port" ) == 0 ) {
1838 /* Port to which this server should connect */
1840 if (port >= 0 && port < 0xFFFF)
1841 New_Server.port = (UINT16)port;
1843 Config_Error(LOG_ERR,
1844 "%s, line %d (section \"Server\"): Illegal port number %ld!",
1845 NGIRCd_ConfFile, Line, port );
1849 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1850 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1854 if( strcasecmp( Var, "Group" ) == 0 ) {
1856 New_Server.group = atoi( Arg );
1857 if (!New_Server.group && strcmp(Arg, "0"))
1858 Config_Error_NaN(Line, Var);
1861 if( strcasecmp( Var, "Passive" ) == 0 ) {
1862 if (Check_ArgIsTrue(Arg))
1863 New_Server.flags |= CONF_SFLAG_DISABLED;
1866 if (strcasecmp(Var, "ServiceMask") == 0) {
1867 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1868 sizeof(New_Server.svs_mask));
1869 if (len >= sizeof(New_Server.svs_mask))
1870 Config_Error_TooLong(Line, Var);
1874 Config_Error_Section(Line, Var, "Server");
1878 * Copy channel name into channel structure.
1880 * If the channel name is not valid because of a missing prefix ('#', '&'),
1881 * a default prefix of '#' will be added.
1883 * @param new_chan New already allocated channel structure.
1884 * @param name Name of the new channel.
1885 * @returns true on success, false otherwise.
1888 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1890 size_t size = sizeof(new_chan->name);
1891 char *dest = new_chan->name;
1893 if (!Channel_IsValidName(name)) {
1895 * maybe user forgot to add a '#'.
1896 * This is only here for user convenience.
1902 return size > strlcpy(dest, name, size);
1906 * Handle variable in [Channel] configuration section.
1908 * @param Line Line numer in configuration file.
1909 * @param Var Variable name.
1910 * @param Arg Variable argument.
1913 Handle_CHANNEL(int Line, char *Var, char *Arg)
1916 struct Conf_Channel *chan;
1919 assert( Var != NULL );
1920 assert( Arg != NULL );
1922 chan = array_get(&Conf_Channels, sizeof(*chan),
1923 array_length(&Conf_Channels, sizeof(*chan)) - 1);
1927 if (strcasecmp(Var, "Name") == 0) {
1928 if (!Handle_Channelname(chan, Arg))
1929 Config_Error_TooLong(Line, Var);
1932 if (strcasecmp(Var, "Modes") == 0) {
1934 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1935 if (len >= sizeof(chan->modes))
1936 Config_Error_TooLong( Line, Var );
1939 if( strcasecmp( Var, "Topic" ) == 0 ) {
1941 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1942 if (len >= sizeof(chan->topic))
1943 Config_Error_TooLong( Line, Var );
1946 if( strcasecmp( Var, "Key" ) == 0 ) {
1947 /* Initial Channel Key (mode k) */
1948 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1949 if (len >= sizeof(chan->key))
1950 Config_Error_TooLong(Line, Var);
1953 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1954 /* maximum user limit, mode l */
1955 chan->maxusers = (unsigned long) atol(Arg);
1956 if (!chan->maxusers && strcmp(Arg, "0"))
1957 Config_Error_NaN(Line, Var);
1960 if (strcasecmp(Var, "KeyFile") == 0) {
1962 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1963 if (len >= sizeof(chan->keyfile))
1964 Config_Error_TooLong(Line, Var);
1968 Config_Error_Section(Line, Var, "Channel");
1972 * Validate server configuration.
1974 * Please note that this function uses exit(1) on fatal errors and therefore
1975 * can result in ngIRCd terminating!
1977 * @param Configtest true if the daemon has been called with "--configtest".
1978 * @param Rehash true if re-reading configuration on runtime.
1979 * @returns true if configuration is valid.
1982 Validate_Config(bool Configtest, bool Rehash)
1984 /* Validate configuration settings. */
1987 int i, servers, servers_once;
1989 bool config_valid = true;
1992 /* Emit a warning when the config file is not a full path name */
1993 if (NGIRCd_ConfFile[0] && NGIRCd_ConfFile[0] != '/') {
1994 Config_Error(LOG_WARNING,
1995 "Not specifying a full path name to \"%s\" can cause problems when rehashing the server!",
1999 /* Validate configured server name, see RFC 2812 section 2.3.1 */
2000 ptr = Conf_ServerName;
2002 if (*ptr >= 'a' && *ptr <= 'z') continue;
2003 if (*ptr >= 'A' && *ptr <= 'Z') continue;
2004 if (*ptr >= '0' && *ptr <= '9') continue;
2005 if (ptr > Conf_ServerName) {
2006 if (*ptr == '.' || *ptr == '-')
2009 Conf_ServerName[0] = '\0';
2013 if (!Conf_ServerName[0]) {
2014 /* No server name configured! */
2015 config_valid = false;
2016 Config_Error(LOG_ALERT,
2017 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
2019 if (!Configtest && !Rehash) {
2020 Config_Error(LOG_ALERT,
2021 "%s exiting due to fatal errors!",
2027 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
2028 /* No dot in server name! */
2029 config_valid = false;
2030 Config_Error(LOG_ALERT,
2031 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
2034 Config_Error(LOG_ALERT,
2035 "%s exiting due to fatal errors!",
2042 if (!Conf_ServerAdminMail[0]) {
2043 /* No administrative contact configured! */
2044 config_valid = false;
2045 Config_Error(LOG_ALERT,
2046 "No administrator email address configured in \"%s\" ('AdminEMail')!",
2049 Config_Error(LOG_ALERT,
2050 "%s exiting due to fatal errors!",
2057 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
2058 && !Conf_ServerAdminMail[0]) {
2059 /* No administrative information configured! */
2060 Config_Error(LOG_WARNING,
2061 "No administrative information configured but required by RFC!");
2065 if (Conf_ServerPwd[0])
2066 Config_Error(LOG_ERR,
2067 "This server uses PAM, \"Password\" in [Global] section will be ignored!");
2071 servers = servers_once = 0;
2072 for (i = 0; i < MAX_SERVERS; i++) {
2073 if (Conf_Server[i].name[0]) {
2075 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
2080 "Configuration: Operators=%ld, Servers=%d[%d], Channels=%ld",
2081 array_length(&Conf_Opers, sizeof(struct Conf_Oper)),
2082 servers, servers_once,
2083 array_length(&Conf_Channels, sizeof(struct Conf_Channel)));
2086 return config_valid;
2090 * Output "line too long" warning.
2092 * @param Line Line number in configuration file.
2093 * @param Item Affected variable name.
2096 Config_Error_TooLong ( const int Line, const char *Item )
2098 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
2102 * Output "unknown variable" warning.
2104 * @param Line Line number in configuration file.
2105 * @param Item Affected variable name.
2106 * @param Section Section name.
2109 Config_Error_Section(const int Line, const char *Item, const char *Section)
2111 Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
2112 NGIRCd_ConfFile, Line, Section, Item);
2116 * Output "not a number" warning.
2118 * @param Line Line number in configuration file.
2119 * @param Item Affected variable name.
2122 Config_Error_NaN( const int Line, const char *Item )
2124 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
2125 NGIRCd_ConfFile, Line, Item );
2129 * Output configuration error to console and/or logfile.
2131 * On runtime, the normal log functions of the daemon are used. But when
2132 * testing the configuration ("--configtest"), all messages go directly
2135 * @param Level Severity level of the message.
2136 * @param Format Format string; see printf() function.
2139 static void Config_Error( const int Level, const char *Format, ... )
2141 static void Config_Error( Level, Format, va_alist )
2147 char msg[MAX_LOG_MSG_LEN];
2150 assert( Format != NULL );
2153 va_start( ap, Format );
2157 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
2161 if (Level <= LOG_WARNING)
2162 printf(" - %s\n", msg);
2166 Log(Level, "%s", msg);
2172 * Dump internal state of the "configuration module".
2175 Conf_DebugDump(void)
2179 Log(LOG_DEBUG, "Configured servers:");
2180 for (i = 0; i < MAX_SERVERS; i++) {
2181 if (! Conf_Server[i].name[0])
2184 " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
2185 Conf_Server[i].name, Conf_Server[i].host,
2186 Conf_Server[i].port, Conf_Server[i].lasttry,
2187 Conf_Server[i].group, Conf_Server[i].flags,
2188 Conf_Server[i].conn_id);
2195 * Initialize server configuration structur to default values.
2197 * @param Server Pointer to server structure to initialize.
2200 Init_Server_Struct( CONF_SERVER *Server )
2202 assert( Server != NULL );
2204 memset( Server, 0, sizeof (CONF_SERVER) );
2206 Server->group = NONE;
2207 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
2209 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
2211 Proc_InitStruct(&Server->res_stat);
2212 Server->conn_id = NONE;
2213 memset(&Server->bind_addr, 0, sizeof(Server->bind_addr));