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];
58 static char Conf_IncludeDir[FNAME_LEN];
60 static void Set_Defaults PARAMS(( bool InitServers ));
61 static bool Read_Config PARAMS(( bool TestOnly, bool IsStarting ));
62 static void Read_Config_File PARAMS(( const char *File, FILE *fd ));
63 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
65 static void Handle_GLOBAL PARAMS((const char *File, int Line,
66 char *Var, char *Arg ));
67 static void Handle_LIMITS PARAMS((const char *File, int Line,
68 char *Var, char *Arg ));
69 static void Handle_OPTIONS PARAMS((const char *File, int Line,
70 char *Var, char *Arg ));
71 static void Handle_OPERATOR PARAMS((const char *File, int Line,
72 char *Var, char *Arg ));
73 static void Handle_SERVER PARAMS((const char *File, int Line,
74 char *Var, char *Arg ));
75 static void Handle_CHANNEL PARAMS((const char *File, int Line,
76 char *Var, char *Arg ));
78 static void Config_Error PARAMS((const int Level, const char *Format, ...));
80 static void Config_Error_NaN PARAMS((const char *File, const int LINE,
82 static void Config_Error_Section PARAMS((const char *File, const int Line,
83 const char *Item, const char *Section));
84 static void Config_Error_TooLong PARAMS((const char *File, const int LINE,
87 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
91 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
93 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
97 #define DEFAULT_CIPHERS "HIGH:!aNULL:@STRENGTH"
100 #define DEFAULT_CIPHERS "SECURE128"
105 static void Handle_SSL PARAMS((const char *File, int Line, char *Var, char *Ark));
107 struct SSLOptions Conf_SSLOptions;
110 * Initialize SSL configuration.
115 free(Conf_SSLOptions.KeyFile);
116 Conf_SSLOptions.KeyFile = NULL;
118 free(Conf_SSLOptions.CertFile);
119 Conf_SSLOptions.CertFile = NULL;
121 free(Conf_SSLOptions.DHFile);
122 Conf_SSLOptions.DHFile = NULL;
123 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
125 array_free(&Conf_SSLOptions.ListenPorts);
127 free(Conf_SSLOptions.CipherList);
128 Conf_SSLOptions.CipherList = NULL;
132 * Check if the current configuration uses/requires SSL.
134 * @returns true if SSL is used and should be initialized.
141 /* SSL listen ports configured? */
142 if (array_bytes(&Conf_SSLOptions.ListenPorts))
145 for (i = 0; i < MAX_SERVERS; i++) {
146 if (Conf_Server[i].port > 0
147 && Conf_Server[i].SSLConnect)
154 * Make sure that a configured file is readable.
156 * Currently, this function is only used for SSL-related options ...
158 * @param Var Configuration variable
159 * @param Filename Configured filename
162 CheckFileReadable(const char *Var, const char *Filename)
169 fp = fopen(Filename, "r");
173 Config_Error(LOG_ERR, "Can't read \"%s\" (\"%s\"): %s",
174 Filename, Var, strerror(errno));
181 * Duplicate string and warn on errors.
183 * @returns Pointer to string on success, NULL otherwise.
186 strdup_warn(const char *str)
188 char *ptr = strdup(str);
190 Config_Error(LOG_ERR,
191 "Could not allocate memory for string: %s", str);
196 * Output a comma separated list of ports (integer values).
203 len = array_length(a, sizeof(UINT16));
205 ports = (UINT16*) array_start(a);
206 printf("%u", (unsigned int) *ports);
209 printf(", %u", (unsigned int) *ports);
216 * Parse a comma separated string into an array of port numbers (integers).
219 ports_parse(array *a, int Line, char *Arg)
227 ptr = strtok( Arg, "," );
231 if (port > 0 && port < 0xFFFF) {
232 port16 = (UINT16) port;
233 if (!array_catb(a, (char*)&port16, sizeof port16))
234 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
235 NGIRCd_ConfFile, Line, port, strerror(errno));
237 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
238 NGIRCd_ConfFile, Line, port );
241 ptr = strtok( NULL, "," );
246 * Initialize configuration module.
251 Read_Config(false, true);
252 Validate_Config(false, false);
256 * "Rehash" (reload) server configuration.
258 * @returns true if configuration has been re-read, false on errors.
263 if (!Read_Config(false, false))
265 Validate_Config(false, true);
267 /* Update CLIENT structure of local server */
268 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
273 * Output a boolean value as "yes/no" string.
276 yesno_to_str(int boolean_value)
284 * Free all IRC operator configuration structures.
289 struct Conf_Oper *op;
292 len = array_length(&Conf_Opers, sizeof(*op));
293 op = array_start(&Conf_Opers);
298 array_free(&Conf_Opers);
302 * Output all IRC operator configuration structures.
307 struct Conf_Oper *op;
310 count = array_length(&Conf_Opers, sizeof(*op));
311 op = array_start(&Conf_Opers);
312 for (i = 0; i < count; i++, op++) {
317 printf(" Name = %s\n", op->name);
318 printf(" Password = %s\n", op->pwd);
319 printf(" Mask = %s\n\n", op->mask ? op->mask : "");
324 * Read configuration, validate and output it.
326 * This function waits for a keypress of the user when stdin/stdout are valid
327 * tty's ("you can read our nice message and we can read in your keypress").
329 * @return 0 on success, 1 on failure(s); therefore the result code can
330 * directly be used by exit() when running "ngircd --configtest".
339 size_t predef_channel_count;
340 struct Conf_Channel *predef_chan;
344 if (!Read_Config(true, true))
347 config_valid = Validate_Config(true, false);
350 if(isatty(fileno(stdin)) && isatty(fileno(stdout))) {
351 puts("OK, press enter to see a dump of your server configuration ...");
354 puts("Ok, dump of your server configuration follows:\n");
357 printf(" Name = %s\n", Conf_ServerName);
358 printf(" AdminInfo1 = %s\n", Conf_ServerAdmin1);
359 printf(" AdminInfo2 = %s\n", Conf_ServerAdmin2);
360 printf(" AdminEMail = %s\n", Conf_ServerAdminMail);
361 printf(" HelpFile = %s\n", Conf_HelpFile);
362 printf(" Info = %s\n", Conf_ServerInfo);
363 printf(" Listen = %s\n", Conf_ListenAddress);
364 if (Using_MotdFile) {
365 printf(" MotdFile = %s\n", Conf_MotdFile);
366 printf(" MotdPhrase =\n");
368 printf(" MotdFile = \n");
369 printf(" MotdPhrase = %s\n", array_bytes(&Conf_Motd)
370 ? (const char*) array_start(&Conf_Motd) : "");
373 printf(" Password = %s\n", Conf_ServerPwd);
375 printf(" PidFile = %s\n", Conf_PidFile);
377 ports_puts(&Conf_ListenPorts);
378 grp = getgrgid(Conf_GID);
380 printf(" ServerGID = %s\n", grp->gr_name);
382 printf(" ServerGID = %ld\n", (long)Conf_GID);
383 pwd = getpwuid(Conf_UID);
385 printf(" ServerUID = %s\n", pwd->pw_name);
387 printf(" ServerUID = %ld\n", (long)Conf_UID);
391 printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
392 printf(" IdleTimeout = %d\n", Conf_IdleTimeout);
393 printf(" MaxConnections = %d\n", Conf_MaxConnections);
394 printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
395 printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
396 printf(" MaxNickLength = %u\n", Conf_MaxNickLength - 1);
397 printf(" MaxListSize = %d\n", Conf_MaxListSize);
398 printf(" PingTimeout = %d\n", Conf_PingTimeout);
399 printf(" PongTimeout = %d\n", Conf_PongTimeout);
403 printf(" AllowedChannelTypes = %s\n", Conf_AllowedChannelTypes);
404 printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
405 printf(" ChrootDir = %s\n", Conf_Chroot);
406 printf(" CloakHost = %s\n", Conf_CloakHost);
407 printf(" CloakHostModeX = %s\n", Conf_CloakHostModeX);
408 printf(" CloakHostSalt = %s\n", Conf_CloakHostSalt);
409 printf(" CloakUserToNick = %s\n", yesno_to_str(Conf_CloakUserToNick));
411 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
412 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
414 printf(" DefaultUserModes = %s\n", Conf_DefaultUserModes);
415 printf(" DNS = %s\n", yesno_to_str(Conf_DNS));
417 printf(" Ident = %s\n", yesno_to_str(Conf_Ident));
419 printf(" IncludeDir = %s\n", Conf_IncludeDir);
420 printf(" MorePrivacy = %s\n", yesno_to_str(Conf_MorePrivacy));
421 printf(" NoticeAuth = %s\n", yesno_to_str(Conf_NoticeAuth));
422 printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
423 printf(" OperChanPAutoOp = %s\n", yesno_to_str(Conf_OperChanPAutoOp));
424 printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
426 printf(" PAM = %s\n", yesno_to_str(Conf_PAM));
427 printf(" PAMIsOptional = %s\n", yesno_to_str(Conf_PAMIsOptional));
430 printf(" RequireAuthPing = %s\n", yesno_to_str(Conf_AuthPing));
432 printf(" ScrubCTCP = %s\n", yesno_to_str(Conf_ScrubCTCP));
434 printf(" SyslogFacility = %s\n",
435 ngt_SyslogFacilityName(Conf_SyslogFacility));
437 printf(" WebircPassword = %s\n", Conf_WebircPwd);
442 printf(" CertFile = %s\n", Conf_SSLOptions.CertFile
443 ? Conf_SSLOptions.CertFile : "");
444 printf(" CipherList = %s\n", Conf_SSLOptions.CipherList ?
445 Conf_SSLOptions.CipherList : DEFAULT_CIPHERS);
446 printf(" DHFile = %s\n", Conf_SSLOptions.DHFile
447 ? Conf_SSLOptions.DHFile : "");
448 printf(" KeyFile = %s\n", Conf_SSLOptions.KeyFile
449 ? Conf_SSLOptions.KeyFile : "");
450 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
451 puts(" KeyFilePassword = <secret>");
453 puts(" KeyFilePassword = ");
454 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
456 ports_puts(&Conf_SSLOptions.ListenPorts);
462 for( i = 0; i < MAX_SERVERS; i++ ) {
463 if( ! Conf_Server[i].name[0] ) continue;
465 /* Valid "Server" section */
467 printf( " Name = %s\n", Conf_Server[i].name );
468 printf( " Host = %s\n", Conf_Server[i].host );
469 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
471 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
473 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
474 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
475 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
476 printf( " Group = %d\n", Conf_Server[i].group );
477 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
480 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
481 predef_chan = array_start(&Conf_Channels);
483 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
484 if (!predef_chan->name[0])
487 /* Valid "Channel" section */
489 printf(" Name = %s\n", predef_chan->name);
490 printf(" Modes = %s\n", predef_chan->modes);
491 printf(" Key = %s\n", predef_chan->key);
492 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
493 printf(" Topic = %s\n", predef_chan->topic);
494 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
497 return (config_valid ? 0 : 1);
501 * Remove connection information from configured server.
503 * If the server is set as "once", delete it from our configuration;
504 * otherwise set the time for the next connection attempt.
506 * Non-server connections will be silently ignored.
509 Conf_UnsetServer( CONN_ID Idx )
514 /* Check all our configured servers */
515 for( i = 0; i < MAX_SERVERS; i++ ) {
516 if( Conf_Server[i].conn_id != Idx ) continue;
518 /* Gotcha! Mark server configuration as "unused": */
519 Conf_Server[i].conn_id = NONE;
521 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
522 /* Delete configuration here */
523 Init_Server_Struct( &Conf_Server[i] );
525 /* Set time for next connect attempt */
527 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
528 /* The connection has been "long", so we don't
529 * require the next attempt to be delayed. */
530 Conf_Server[i].lasttry =
531 t - Conf_ConnectRetry + RECONNECT_DELAY;
533 /* "Short" connection, enforce "ConnectRetry"
534 * but randomize it a little bit: 15 seconds. */
535 Conf_Server[i].lasttry =
536 #ifdef HAVE_ARC4RANDOM
537 t + (arc4random() % 15);
539 t + rand() / (RAND_MAX / 15);
547 * Set connection information for specified configured server.
550 Conf_SetServer( int ConfServer, CONN_ID Idx )
552 assert( ConfServer > NONE );
553 assert( Idx > NONE );
555 if (Conf_Server[ConfServer].conn_id > NONE &&
556 Conf_Server[ConfServer].conn_id != Idx) {
558 "Connection %d: Server configuration of \"%s\" already in use by connection %d!",
559 Idx, Conf_Server[ConfServer].name,
560 Conf_Server[ConfServer].conn_id);
561 Conn_Close(Idx, NULL, "Server configuration already in use", true);
564 Conf_Server[ConfServer].conn_id = Idx;
569 * Get index of server in configuration structure.
572 Conf_GetServer( CONN_ID Idx )
576 assert( Idx > NONE );
578 for( i = 0; i < MAX_SERVERS; i++ ) {
579 if( Conf_Server[i].conn_id == Idx ) return i;
585 * Enable a server by name and adjust its port number.
587 * @returns true if a server has been enabled and now has a valid port
588 * number and host name for outgoing connections.
591 Conf_EnableServer( const char *Name, UINT16 Port )
595 assert( Name != NULL );
596 for( i = 0; i < MAX_SERVERS; i++ ) {
597 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
598 /* Gotcha! Set port and enable server: */
599 Conf_Server[i].port = Port;
600 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
601 return (Conf_Server[i].port && Conf_Server[i].host[0]);
608 * Enable a server by name.
610 * The server is only usable as outgoing server, if it has set a valid port
611 * number for outgoing connections!
612 * If not, you have to use Conf_EnableServer() function to make it available.
614 * @returns true if a server has been enabled; false otherwise.
617 Conf_EnablePassiveServer(const char *Name)
621 assert( Name != NULL );
622 for (i = 0; i < MAX_SERVERS; i++) {
623 if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
624 && (Conf_Server[i].port > 0)) {
625 /* BINGO! Enable server */
626 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
634 * Disable a server by name.
635 * An already established connection will be disconnected.
637 * @returns true if a server was found and has been disabled.
640 Conf_DisableServer( const char *Name )
644 assert( Name != NULL );
645 for( i = 0; i < MAX_SERVERS; i++ ) {
646 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
647 /* Gotcha! Disable and disconnect server: */
648 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
649 if( Conf_Server[i].conn_id > NONE )
650 Conn_Close(Conf_Server[i].conn_id, NULL,
651 "Server link terminated on operator request",
660 * Add a new remote server to our configuration.
662 * @param Name Name of the new server.
663 * @param Port Port number to connect to or 0 for incoming connections.
664 * @param Host Host name to connect to.
665 * @param MyPwd Password that will be sent to the peer.
666 * @param PeerPwd Password that must be received from the peer.
667 * @returns true if the new server has been added; false otherwise.
670 Conf_AddServer(const char *Name, UINT16 Port, const char *Host,
671 const char *MyPwd, const char *PeerPwd)
675 assert( Name != NULL );
676 assert( Host != NULL );
677 assert( MyPwd != NULL );
678 assert( PeerPwd != NULL );
680 /* Search unused item in server configuration structure */
681 for( i = 0; i < MAX_SERVERS; i++ ) {
682 /* Is this item used? */
683 if( ! Conf_Server[i].name[0] ) break;
685 if( i >= MAX_SERVERS ) return false;
687 Init_Server_Struct( &Conf_Server[i] );
688 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
689 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
690 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
691 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
692 Conf_Server[i].port = Port;
693 Conf_Server[i].flags = CONF_SFLAG_ONCE;
699 * Check if the given nickname is reserved for services on a particular server.
701 * @param ConfServer The server index to check.
702 * @param Nick The nickname to check.
703 * @returns true if the given nickname belongs to an "IRC service".
706 Conf_NickIsService(int ConfServer, const char *Nick)
708 assert (ConfServer >= 0);
709 assert (ConfServer < MAX_SERVERS);
711 return MatchCaseInsensitiveList(Conf_Server[ConfServer].svs_mask,
716 * Check if the given nickname is blocked for "normal client" use.
718 * @param ConfServer The server index or NONE to check all configured servers.
719 * @param Nick The nickname to check.
720 * @returns true if the given nickname belongs to an "IRC service".
723 Conf_NickIsBlocked(const char *Nick)
727 for(i = 0; i < MAX_SERVERS; i++) {
728 if (!Conf_Server[i].name[0])
730 if (Conf_NickIsService(i, Nick))
737 * Initialize configuration settings with their default values.
740 Set_Defaults(bool InitServers)
743 char random[RANDOM_SALT_LEN + 1];
746 strcpy(Conf_ServerName, "");
747 strcpy(Conf_ServerAdmin1, "");
748 strcpy(Conf_ServerAdmin2, "");
749 strcpy(Conf_ServerAdminMail, "");
750 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
751 PACKAGE_NAME, PACKAGE_VERSION);
752 free(Conf_ListenAddress);
753 Conf_ListenAddress = NULL;
754 array_free(&Conf_ListenPorts);
755 array_free(&Conf_Motd);
756 array_free(&Conf_Helptext);
757 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
758 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
759 strlcpy(Conf_HelpFile, DOCDIR, sizeof(Conf_HelpFile));
760 strlcat(Conf_HelpFile, HELP_FILE, sizeof(Conf_HelpFile));
761 strcpy(Conf_ServerPwd, "");
762 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
763 Conf_UID = Conf_GID = 0;
766 Conf_ConnectRetry = 60;
767 Conf_IdleTimeout = 0;
768 Conf_MaxConnections = 0;
769 Conf_MaxConnectionsIP = 5;
771 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
772 Conf_MaxListSize = 100;
773 Conf_PingTimeout = 120;
774 Conf_PongTimeout = 20;
777 strlcpy(Conf_AllowedChannelTypes, CHANTYPES,
778 sizeof(Conf_AllowedChannelTypes));
779 Conf_AllowRemoteOper = false;
781 Conf_AuthPing = false;
783 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
784 strcpy(Conf_CloakHost, "");
785 strcpy(Conf_CloakHostModeX, "");
786 strlcpy(Conf_CloakHostSalt, ngt_RandomStr(random, RANDOM_SALT_LEN),
787 sizeof(Conf_CloakHostSalt));
788 Conf_CloakUserToNick = false;
789 Conf_ConnectIPv4 = true;
791 Conf_ConnectIPv6 = true;
793 Conf_ConnectIPv6 = false;
795 strcpy(Conf_DefaultUserModes, "");
802 strcpy(Conf_IncludeDir, "");
803 Conf_MorePrivacy = false;
804 Conf_NoticeAuth = false;
805 Conf_OperCanMode = false;
806 Conf_OperChanPAutoOp = true;
807 Conf_OperServerMode = false;
813 Conf_PAMIsOptional = false;
815 Conf_ScrubCTCP = false;
817 Conf_SyslogFacility = LOG_LOCAL5;
819 Conf_SyslogFacility = 0;
823 /* Initialize server configuration structures */
825 for (i = 0; i < MAX_SERVERS;
826 Init_Server_Struct(&Conf_Server[i++]));
831 * Get number of configured listening ports.
833 * @returns The number of ports (IPv4+IPv6) on which the server should listen.
838 size_t cnt = array_bytes(&Conf_ListenPorts);
840 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
846 * Read contents of a text file into an array.
848 * This function is used to read the MOTD and help text file, for example.
850 * @param filename Name of the file to read.
851 * @return true, when the file has been read in.
854 Read_TextFile(const char *Filename, const char *Name, array *Destination)
860 if (*Filename == '\0')
863 fp = fopen(Filename, "r");
865 Config_Error(LOG_ERR, "Can't read %s file \"%s\": %s",
866 Name, Filename, strerror(errno));
870 array_free(Destination);
871 while (fgets(line, (int)sizeof line, fp)) {
872 ngt_TrimLastChr(line, '\n');
874 /* add text including \0 */
875 if (!array_catb(Destination, line, strlen(line) + 1)) {
876 Log(LOG_ERR, "Cannot read/add \"%s\", line %d: %s",
877 Filename, line_no, strerror(errno));
887 * Read ngIRCd configuration file.
889 * Please note that this function uses exit(1) on fatal errors and therefore
890 * can result in ngIRCd terminating!
892 * @param ngircd_starting Flag indicating if ngIRCd is starting or not.
893 * @returns true when the configuration file has been read
894 * successfully; false otherwise.
897 Read_Config(bool TestOnly, bool IsStarting)
899 const UINT16 defaultport = 6667;
900 char *ptr, file[FNAME_LEN];
901 struct dirent *entry;
906 /* Open configuration file */
907 fd = fopen( NGIRCd_ConfFile, "r" );
909 /* No configuration file found! */
910 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
911 NGIRCd_ConfFile, strerror( errno ));
914 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
919 Set_Defaults(IsStarting);
922 Config_Error(LOG_INFO,
923 "Reading configuration from \"%s\" ...",
926 /* Clean up server configuration structure: mark all already
927 * configured servers as "once" so that they are deleted
928 * after the next disconnect and delete all unused servers.
929 * And delete all servers which are "duplicates" of servers
930 * that are already marked as "once" (such servers have been
931 * created by the last rehash but are now useless). */
932 for( i = 0; i < MAX_SERVERS; i++ ) {
933 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
935 /* This structure is in use ... */
936 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
937 /* Check for duplicates */
938 for( n = 0; n < MAX_SERVERS; n++ ) {
939 if( n == i ) continue;
941 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
942 Init_Server_Struct( &Conf_Server[n] );
944 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
950 /* Mark server as "once" */
951 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
952 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
957 /* Initialize variables */
958 Init_Server_Struct( &New_Server );
959 New_Server_Idx = NONE;
964 Read_Config_File(NGIRCd_ConfFile, fd);
967 if (Conf_IncludeDir[0]) {
968 dh = opendir(Conf_IncludeDir);
970 Config_Error(LOG_ALERT,
971 "Can't open include directory \"%s\": %s",
972 Conf_IncludeDir, strerror(errno));
974 strlcpy(Conf_IncludeDir, SYSCONFDIR, sizeof(Conf_IncludeDir));
975 strlcat(Conf_IncludeDir, CONFIG_DIR, sizeof(Conf_IncludeDir));
976 dh = opendir(Conf_IncludeDir);
979 /* Include further configuration files, if IncludeDir is available */
981 while ((entry = readdir(dh)) != NULL) {
982 ptr = strrchr(entry->d_name, '.');
983 if (!ptr || strcasecmp(ptr, ".conf") != 0)
985 snprintf(file, sizeof(file), "%s/%s",
986 Conf_IncludeDir, entry->d_name);
988 Config_Error(LOG_INFO,
989 "Reading configuration from \"%s\" ...",
991 fd = fopen(file, "r");
993 Read_Config_File(file, fd);
996 Config_Error(LOG_ALERT,
997 "Can't read configuration \"%s\": %s",
998 file, strerror(errno));
1003 /* Check if there is still a server to add */
1004 if( New_Server.name[0] ) {
1005 /* Copy data to "real" server structure */
1006 assert( New_Server_Idx > NONE );
1007 Conf_Server[New_Server_Idx] = New_Server;
1010 /* not a single listening port? Add default. */
1011 if (no_listenports() &&
1012 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
1014 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
1015 (unsigned int) defaultport, strerror(errno));
1020 if (!Conf_ListenAddress)
1021 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
1023 if (!Conf_ListenAddress) {
1024 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
1028 /* No MOTD phrase configured? (re)try motd file. */
1029 if (array_bytes(&Conf_Motd) == 0) {
1030 if (Read_TextFile(Conf_MotdFile, "MOTD", &Conf_Motd))
1031 Using_MotdFile = true;
1034 /* Try to read ngIRCd help text file. */
1035 (void)Read_TextFile(Conf_HelpFile, "help text", &Conf_Helptext);
1036 if (!array_bytes(&Conf_Helptext))
1037 Config_Error(LOG_WARNING,
1038 "No help text available, HELP command will be of limited use.");
1041 /* Make sure that all SSL-related files are readable */
1042 CheckFileReadable("CertFile", Conf_SSLOptions.CertFile);
1043 CheckFileReadable("DHFile", Conf_SSLOptions.DHFile);
1044 CheckFileReadable("KeyFile", Conf_SSLOptions.KeyFile);
1046 /* Set the default ciphers if none were configured */
1047 if (!Conf_SSLOptions.CipherList)
1048 Conf_SSLOptions.CipherList = strdup_warn(DEFAULT_CIPHERS);
1057 static void Read_Config_File(const char *File, FILE *fd)
1059 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
1063 /* Read configuration file */
1066 if (!fgets(str, LINE_LEN, fd))
1071 /* Skip comments and empty lines */
1072 if (str[0] == ';' || str[0] == '#' || str[0] == '\0')
1075 /* Is this the beginning of a new section? */
1076 if ((str[0] == '[') && (str[strlen(str) - 1] == ']')) {
1077 strlcpy(section, str, sizeof(section));
1078 if (strcasecmp(section, "[GLOBAL]") == 0
1079 || strcasecmp(section, "[LIMITS]") == 0
1080 || strcasecmp(section, "[OPTIONS]") == 0
1082 || strcasecmp(section, "[SSL]") == 0
1087 if (strcasecmp(section, "[SERVER]") == 0) {
1088 /* Check if there is already a server to add */
1089 if (New_Server.name[0]) {
1090 /* Copy data to "real" server structure */
1091 assert(New_Server_Idx > NONE);
1092 Conf_Server[New_Server_Idx] =
1096 /* Re-init structure for new server */
1097 Init_Server_Struct(&New_Server);
1099 /* Search unused item in server configuration structure */
1100 for (i = 0; i < MAX_SERVERS; i++) {
1101 /* Is this item used? */
1102 if (!Conf_Server[i].name[0])
1105 if (i >= MAX_SERVERS) {
1106 /* Oops, no free item found! */
1107 Config_Error(LOG_ERR,
1108 "Too many servers configured.");
1109 New_Server_Idx = NONE;
1115 if (strcasecmp(section, "[CHANNEL]") == 0) {
1116 count = array_length(&Conf_Channels,
1121 sizeof(struct Conf_Channel), count)) {
1122 Config_Error(LOG_ERR,
1123 "Could not allocate memory for new operator (line %d)",
1129 if (strcasecmp(section, "[OPERATOR]") == 0) {
1130 count = array_length(&Conf_Opers,
1131 sizeof(struct Conf_Oper));
1132 if (!array_alloc(&Conf_Opers,
1133 sizeof(struct Conf_Oper),
1135 Config_Error(LOG_ERR,
1136 "Could not allocate memory for new channel (line &d)",
1142 Config_Error(LOG_ERR,
1143 "%s, line %d: Unknown section \"%s\"!",
1144 NGIRCd_ConfFile, line, section);
1147 if (section[0] == 0x1)
1150 /* Split line into variable name and parameters */
1151 ptr = strchr(str, '=');
1153 Config_Error(LOG_ERR, "%s, line %d: Syntax error!",
1154 NGIRCd_ConfFile, line);
1163 if (strcasecmp(section, "[GLOBAL]") == 0)
1164 Handle_GLOBAL(File, line, var, arg);
1165 else if (strcasecmp(section, "[LIMITS]") == 0)
1166 Handle_LIMITS(File, line, var, arg);
1167 else if (strcasecmp(section, "[OPTIONS]") == 0)
1168 Handle_OPTIONS(File, line, var, arg);
1170 else if (strcasecmp(section, "[SSL]") == 0)
1171 Handle_SSL(File, line, var, arg);
1173 else if (strcasecmp(section, "[OPERATOR]") == 0)
1174 Handle_OPERATOR(File, line, var, arg);
1175 else if (strcasecmp(section, "[SERVER]") == 0)
1176 Handle_SERVER(File, line, var, arg);
1177 else if (strcasecmp(section, "[CHANNEL]") == 0)
1178 Handle_CHANNEL(File, line, var, arg);
1180 Config_Error(LOG_ERR,
1181 "%s, line %d: Variable \"%s\" outside section!",
1182 NGIRCd_ConfFile, line, var);
1187 * Check whether a string argument is "true" or "false".
1189 * @param Arg Input string.
1190 * @returns true if the input string has been parsed as "yes", "true"
1191 * (case insensitive) or a non-zero integer value.
1194 Check_ArgIsTrue(const char *Arg)
1196 if (strcasecmp(Arg, "yes") == 0)
1198 if (strcasecmp(Arg, "true") == 0)
1207 * Handle setting of "MaxNickLength".
1209 * @param Line Line number in configuration file.
1210 * @raram Arg Input string.
1211 * @returns New configured maximum nickname length.
1214 Handle_MaxNickLength(int Line, const char *Arg)
1218 new = (unsigned) atoi(Arg) + 1;
1219 if (new > CLIENT_NICK_LEN) {
1220 Config_Error(LOG_WARNING,
1221 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
1222 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
1223 return CLIENT_NICK_LEN;
1226 Config_Error(LOG_WARNING,
1227 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
1228 NGIRCd_ConfFile, Line);
1235 * Output a warning messages if IDENT is configured but not compiled in.
1238 WarnIdent(int UNUSED Line)
1242 /* user has enabled ident lookups explicitly, but ... */
1243 Config_Error(LOG_WARNING,
1244 "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
1245 NGIRCd_ConfFile, Line);
1251 * Output a warning messages if IPv6 is configured but not compiled in.
1254 WarnIPv6(int UNUSED Line)
1257 if (Conf_ConnectIPv6) {
1258 /* user has enabled IPv6 explicitly, but ... */
1259 Config_Error(LOG_WARNING,
1260 "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
1261 NGIRCd_ConfFile, Line);
1267 * Output a warning messages if PAM is configured but not compiled in.
1270 WarnPAM(int UNUSED Line)
1274 Config_Error(LOG_WARNING,
1275 "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
1276 NGIRCd_ConfFile, Line);
1282 * Handle legacy "NoXXX" options in [GLOBAL] section.
1284 * TODO: This function and support for "NoXXX" could be removed starting
1285 * with ngIRCd release 19 (one release after marking it "deprecated").
1287 * @param Var Variable name.
1288 * @param Arg Argument string.
1289 * @returns true if a NoXXX option has been processed; false otherwise.
1292 CheckLegacyNoOption(const char *Var, const char *Arg)
1294 if(strcasecmp(Var, "NoDNS") == 0) {
1295 Conf_DNS = !Check_ArgIsTrue( Arg );
1298 if (strcasecmp(Var, "NoIdent") == 0) {
1299 Conf_Ident = !Check_ArgIsTrue(Arg);
1302 if(strcasecmp(Var, "NoPAM") == 0) {
1303 Conf_PAM = !Check_ArgIsTrue(Arg);
1310 * Handle deprecated legacy options in [GLOBAL] section.
1312 * TODO: This function and support for these options in the [Global] section
1313 * could be removed starting with ngIRCd release 19 (one release after
1314 * marking it "deprecated").
1316 * @param Var Variable name.
1317 * @param Arg Argument string.
1318 * @returns true if a legacy option has been processed; false otherwise.
1321 CheckLegacyGlobalOption(const char *File, int Line, char *Var, char *Arg)
1323 if (strcasecmp(Var, "AllowRemoteOper") == 0
1324 || strcasecmp(Var, "ChrootDir") == 0
1325 || strcasecmp(Var, "ConnectIPv4") == 0
1326 || strcasecmp(Var, "ConnectIPv6") == 0
1327 || strcasecmp(Var, "OperCanUseMode") == 0
1328 || strcasecmp(Var, "OperChanPAutoOp") == 0
1329 || strcasecmp(Var, "OperServerMode") == 0
1330 || strcasecmp(Var, "PredefChannelsOnly") == 0
1331 || strcasecmp(Var, "SyslogFacility") == 0
1332 || strcasecmp(Var, "WebircPassword") == 0) {
1333 Handle_OPTIONS(File, Line, Var, Arg);
1336 if (strcasecmp(Var, "ConnectRetry") == 0
1337 || strcasecmp(Var, "IdleTimeout") == 0
1338 || strcasecmp(Var, "MaxConnections") == 0
1339 || strcasecmp(Var, "MaxConnectionsIP") == 0
1340 || strcasecmp(Var, "MaxJoins") == 0
1341 || strcasecmp(Var, "MaxNickLength") == 0
1342 || strcasecmp(Var, "PingTimeout") == 0
1343 || strcasecmp(Var, "PongTimeout") == 0) {
1344 Handle_LIMITS(File, Line, Var, Arg);
1348 if (strcasecmp(Var, "SSLCertFile") == 0
1349 || strcasecmp(Var, "SSLDHFile") == 0
1350 || strcasecmp(Var, "SSLKeyFile") == 0
1351 || strcasecmp(Var, "SSLKeyFilePassword") == 0
1352 || strcasecmp(Var, "SSLPorts") == 0) {
1353 Handle_SSL(File, Line, Var + 3, Arg);
1362 * Strip "no" prefix of a string.
1364 * TODO: This function and support for "NoXXX" should be removed starting
1365 * with ngIRCd release 19! (One release after marking it "deprecated").
1367 * @param str Pointer to input string starting with "no".
1368 * @returns New pointer to string without "no" prefix.
1371 NoNo(const char *str)
1373 assert(strncasecmp("no", str, 2) == 0 && str[2]);
1378 * Invert "boolean" string.
1380 * TODO: This function and support for "NoXXX" should be removed starting
1381 * with ngIRCd release 19! (One release after marking it "deprecated").
1383 * @param arg "Boolean" input string.
1384 * @returns Pointer to inverted "boolean string".
1387 InvertArg(const char *arg)
1389 return yesno_to_str(!Check_ArgIsTrue(arg));
1393 * Handle variable in [Global] configuration section.
1395 * @param Line Line numer in configuration file.
1396 * @param Var Variable name.
1397 * @param Arg Variable argument.
1400 Handle_GLOBAL(const char *File, int Line, char *Var, char *Arg )
1405 const char *section;
1407 assert(File != NULL);
1409 assert(Var != NULL);
1410 assert(Arg != NULL);
1412 if (strcasecmp(Var, "Name") == 0) {
1413 len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
1414 if (len >= sizeof(Conf_ServerName))
1415 Config_Error_TooLong(File, Line, Var);
1418 if (strcasecmp(Var, "AdminInfo1") == 0) {
1419 len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
1420 if (len >= sizeof(Conf_ServerAdmin1))
1421 Config_Error_TooLong(File, Line, Var);
1424 if (strcasecmp(Var, "AdminInfo2") == 0) {
1425 len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
1426 if (len >= sizeof(Conf_ServerAdmin2))
1427 Config_Error_TooLong(File, Line, Var);
1430 if (strcasecmp(Var, "AdminEMail") == 0) {
1431 len = strlcpy(Conf_ServerAdminMail, Arg,
1432 sizeof(Conf_ServerAdminMail));
1433 if (len >= sizeof(Conf_ServerAdminMail))
1434 Config_Error_TooLong(File, Line, Var);
1437 if (strcasecmp(Var, "Info") == 0) {
1438 len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
1439 if (len >= sizeof(Conf_ServerInfo))
1440 Config_Error_TooLong(File, Line, Var);
1443 if (strcasecmp(Var, "HelpFile") == 0) {
1444 len = strlcpy(Conf_HelpFile, Arg, sizeof(Conf_HelpFile));
1445 if (len >= sizeof(Conf_HelpFile))
1446 Config_Error_TooLong(File, Line, Var);
1449 if (strcasecmp(Var, "Listen") == 0) {
1450 if (Conf_ListenAddress) {
1451 Config_Error(LOG_ERR,
1452 "Multiple Listen= options, ignoring: %s",
1456 Conf_ListenAddress = strdup_warn(Arg);
1457 /* If allocation fails, we're in trouble: we cannot ignore the
1458 * error -- otherwise ngircd would listen on all interfaces. */
1459 if (!Conf_ListenAddress) {
1460 Config_Error(LOG_ALERT,
1461 "%s exiting due to fatal errors!",
1467 if (strcasecmp(Var, "MotdFile") == 0) {
1468 len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
1469 if (len >= sizeof(Conf_MotdFile))
1470 Config_Error_TooLong(File, Line, Var);
1473 if (strcasecmp(Var, "MotdPhrase") == 0) {
1477 if (len >= LINE_LEN) {
1478 Config_Error_TooLong(File, Line, Var);
1481 if (!array_copyb(&Conf_Motd, Arg, len + 1))
1482 Config_Error(LOG_WARNING,
1483 "%s, line %d: Could not append MotdPhrase: %s",
1484 NGIRCd_ConfFile, Line, strerror(errno));
1485 Using_MotdFile = false;
1488 if(strcasecmp(Var, "Password") == 0) {
1489 len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
1490 if (len >= sizeof(Conf_ServerPwd))
1491 Config_Error_TooLong(File, Line, Var);
1494 if (strcasecmp(Var, "PidFile") == 0) {
1495 len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
1496 if (len >= sizeof(Conf_PidFile))
1497 Config_Error_TooLong(File, Line, Var);
1500 if (strcasecmp(Var, "Ports") == 0) {
1501 ports_parse(&Conf_ListenPorts, Line, Arg);
1504 if (strcasecmp(Var, "ServerGID") == 0) {
1505 grp = getgrnam(Arg);
1507 Conf_GID = grp->gr_gid;
1509 Conf_GID = (unsigned int)atoi(Arg);
1510 if (!Conf_GID && strcmp(Arg, "0"))
1511 Config_Error(LOG_WARNING,
1512 "%s, line %d: Value of \"%s\" is not a valid group name or ID!",
1513 NGIRCd_ConfFile, Line, Var);
1517 if (strcasecmp(Var, "ServerUID") == 0) {
1518 pwd = getpwnam(Arg);
1520 Conf_UID = pwd->pw_uid;
1522 Conf_UID = (unsigned int)atoi(Arg);
1523 if (!Conf_UID && strcmp(Arg, "0"))
1524 Config_Error(LOG_WARNING,
1525 "%s, line %d: Value of \"%s\" is not a valid user name or ID!",
1526 NGIRCd_ConfFile, Line, Var);
1531 if (CheckLegacyNoOption(Var, Arg)) {
1532 /* TODO: This function and support for "NoXXX" could be
1533 * be removed starting with ngIRCd release 19 (one release
1534 * after marking it "deprecated"). */
1535 Config_Error(LOG_WARNING,
1536 "%s, line %d (section \"Global\"): \"No\"-Prefix is deprecated, use \"%s = %s\" in [Options] section!",
1537 NGIRCd_ConfFile, Line, NoNo(Var), InvertArg(Arg));
1538 if (strcasecmp(Var, "NoIdent") == 0)
1540 else if (strcasecmp(Var, "NoPam") == 0)
1544 if ((section = CheckLegacyGlobalOption(File, Line, Var, Arg))) {
1545 /** TODO: This function and support for these options in the
1546 * [Global] section could be removed starting with ngIRCd
1547 * release 19 (one release after marking it "deprecated"). */
1548 if (strncasecmp(Var, "SSL", 3) == 0) {
1549 Config_Error(LOG_WARNING,
1550 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s and rename to \"%s\"!",
1551 NGIRCd_ConfFile, Line, Var, section,
1554 Config_Error(LOG_WARNING,
1555 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s!",
1556 NGIRCd_ConfFile, Line, Var, section);
1561 Config_Error_Section(File, Line, Var, "Global");
1565 * Handle variable in [Limits] configuration section.
1567 * @param Line Line numer in configuration file.
1568 * @param Var Variable name.
1569 * @param Arg Variable argument.
1572 Handle_LIMITS(const char *File, int Line, char *Var, char *Arg)
1574 assert(File != NULL);
1576 assert(Var != NULL);
1577 assert(Arg != NULL);
1579 if (strcasecmp(Var, "ConnectRetry") == 0) {
1580 Conf_ConnectRetry = atoi(Arg);
1581 if (Conf_ConnectRetry < 5) {
1582 Config_Error(LOG_WARNING,
1583 "%s, line %d: Value of \"ConnectRetry\" too low!",
1584 NGIRCd_ConfFile, Line);
1585 Conf_ConnectRetry = 5;
1589 if (strcasecmp(Var, "IdleTimeout") == 0) {
1590 Conf_IdleTimeout = atoi(Arg);
1591 if (!Conf_IdleTimeout && strcmp(Arg, "0"))
1592 Config_Error_NaN(File, Line, Var);
1595 if (strcasecmp(Var, "MaxConnections") == 0) {
1596 Conf_MaxConnections = atoi(Arg);
1597 if (!Conf_MaxConnections && strcmp(Arg, "0"))
1598 Config_Error_NaN(File, Line, Var);
1601 if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
1602 Conf_MaxConnectionsIP = atoi(Arg);
1603 if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
1604 Config_Error_NaN(File, Line, Var);
1607 if (strcasecmp(Var, "MaxJoins") == 0) {
1608 Conf_MaxJoins = atoi(Arg);
1609 if (!Conf_MaxJoins && strcmp(Arg, "0"))
1610 Config_Error_NaN(File, Line, Var);
1613 if (strcasecmp(Var, "MaxNickLength") == 0) {
1614 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1617 if (strcasecmp(Var, "MaxListSize") == 0) {
1618 Conf_MaxListSize = atoi(Arg);
1619 if (!Conf_MaxListSize && strcmp(Arg, "0"))
1620 Config_Error_NaN(File, Line, Var);
1623 if (strcasecmp(Var, "PingTimeout") == 0) {
1624 Conf_PingTimeout = atoi(Arg);
1625 if (Conf_PingTimeout < 5) {
1626 Config_Error(LOG_WARNING,
1627 "%s, line %d: Value of \"PingTimeout\" too low!",
1628 NGIRCd_ConfFile, Line);
1629 Conf_PingTimeout = 5;
1633 if (strcasecmp(Var, "PongTimeout") == 0) {
1634 Conf_PongTimeout = atoi(Arg);
1635 if (Conf_PongTimeout < 5) {
1636 Config_Error(LOG_WARNING,
1637 "%s, line %d: Value of \"PongTimeout\" too low!",
1638 NGIRCd_ConfFile, Line);
1639 Conf_PongTimeout = 5;
1644 Config_Error_Section(File, Line, Var, "Limits");
1648 * Handle variable in [Options] configuration section.
1650 * @param Line Line numer in configuration file.
1651 * @param Var Variable name.
1652 * @param Arg Variable argument.
1655 Handle_OPTIONS(const char *File, int Line, char *Var, char *Arg)
1660 assert(File != NULL);
1662 assert(Var != NULL);
1663 assert(Arg != NULL);
1665 if (strcasecmp(Var, "AllowedChannelTypes") == 0) {
1667 Conf_AllowedChannelTypes[0] = '\0';
1669 if (strchr(Conf_AllowedChannelTypes, *p)) {
1670 /* Prefix is already included; ignore it */
1675 if (strchr(CHANTYPES, *p)) {
1676 len = strlen(Conf_AllowedChannelTypes) + 1;
1677 assert(len < sizeof(Conf_AllowedChannelTypes));
1678 Conf_AllowedChannelTypes[len - 1] = *p;
1679 Conf_AllowedChannelTypes[len] = '\0';
1681 Config_Error(LOG_WARNING,
1682 "%s, line %d: Unknown channel prefix \"%c\" in \"AllowedChannelTypes\"!",
1689 if (strcasecmp(Var, "AllowRemoteOper") == 0) {
1690 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1693 if (strcasecmp(Var, "ChrootDir") == 0) {
1694 len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
1695 if (len >= sizeof(Conf_Chroot))
1696 Config_Error_TooLong(File, Line, Var);
1699 if (strcasecmp(Var, "CloakHost") == 0) {
1700 len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
1701 if (len >= sizeof(Conf_CloakHost))
1702 Config_Error_TooLong(File, Line, Var);
1705 if (strcasecmp(Var, "CloakHostModeX") == 0) {
1706 len = strlcpy(Conf_CloakHostModeX, Arg, sizeof(Conf_CloakHostModeX));
1707 if (len >= sizeof(Conf_CloakHostModeX))
1708 Config_Error_TooLong(File, Line, Var);
1711 if (strcasecmp(Var, "CloakHostSalt") == 0) {
1712 len = strlcpy(Conf_CloakHostSalt, Arg, sizeof(Conf_CloakHostSalt));
1713 if (len >= sizeof(Conf_CloakHostSalt))
1714 Config_Error_TooLong(File, Line, Var);
1717 if (strcasecmp(Var, "CloakUserToNick") == 0) {
1718 Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
1721 if (strcasecmp(Var, "ConnectIPv6") == 0) {
1722 Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
1726 if (strcasecmp(Var, "ConnectIPv4") == 0) {
1727 Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
1730 if (strcasecmp(Var, "DefaultUserModes") == 0) {
1732 Conf_DefaultUserModes[0] = '\0';
1734 if (strchr(Conf_DefaultUserModes, *p)) {
1735 /* Mode is already included; ignore it */
1740 if (strchr(USERMODES, *p)) {
1741 len = strlen(Conf_DefaultUserModes) + 1;
1742 assert(len < sizeof(Conf_DefaultUserModes));
1743 Conf_DefaultUserModes[len - 1] = *p;
1744 Conf_DefaultUserModes[len] = '\0';
1746 Config_Error(LOG_WARNING,
1747 "%s, line %d: Unknown user mode \"%c\" in \"DefaultUserModes\"!",
1754 if (strcasecmp(Var, "DNS") == 0) {
1755 Conf_DNS = Check_ArgIsTrue(Arg);
1758 if (strcasecmp(Var, "Ident") == 0) {
1759 Conf_Ident = Check_ArgIsTrue(Arg);
1763 if (strcasecmp(Var, "IncludeDir") == 0) {
1764 if (Conf_IncludeDir[0]) {
1765 Config_Error(LOG_ERR,
1766 "%s, line %d: Can't overwrite value of \"IncludeDir\" variable!",
1770 len = strlcpy(Conf_IncludeDir, Arg, sizeof(Conf_IncludeDir));
1771 if (len >= sizeof(Conf_IncludeDir))
1772 Config_Error_TooLong(File, Line, Var);
1775 if (strcasecmp(Var, "MorePrivacy") == 0) {
1776 Conf_MorePrivacy = Check_ArgIsTrue(Arg);
1779 if (strcasecmp(Var, "NoticeAuth") == 0) {
1780 Conf_NoticeAuth = Check_ArgIsTrue(Arg);
1783 if (strcasecmp(Var, "OperCanUseMode") == 0) {
1784 Conf_OperCanMode = Check_ArgIsTrue(Arg);
1787 if (strcasecmp(Var, "OperChanPAutoOp") == 0) {
1788 Conf_OperChanPAutoOp = Check_ArgIsTrue(Arg);
1791 if (strcasecmp(Var, "OperServerMode") == 0) {
1792 Conf_OperServerMode = Check_ArgIsTrue(Arg);
1795 if (strcasecmp(Var, "PAM") == 0) {
1796 Conf_PAM = Check_ArgIsTrue(Arg);
1800 if (strcasecmp(Var, "PAMIsOptional") == 0 ) {
1801 Conf_PAMIsOptional = Check_ArgIsTrue(Arg);
1804 if (strcasecmp(Var, "PredefChannelsOnly") == 0) {
1806 * TODO: This section and support for "PredefChannelsOnly"
1807 * could be removed starting with ngIRCd release 22 (one
1808 * release after marking it "deprecated") ...
1810 Config_Error(LOG_WARNING,
1811 "%s, line %d (section \"Options\"): \"%s\" is deprecated, please use \"AllowedChannelTypes\"!",
1813 if (Check_ArgIsTrue(Arg))
1814 Conf_AllowedChannelTypes[0] = '\0';
1816 strlcpy(Conf_AllowedChannelTypes, CHANTYPES,
1817 sizeof(Conf_AllowedChannelTypes));
1821 if (strcasecmp(Var, "RequireAuthPing") == 0) {
1822 Conf_AuthPing = Check_ArgIsTrue(Arg);
1826 if (strcasecmp(Var, "ScrubCTCP") == 0) {
1827 Conf_ScrubCTCP = Check_ArgIsTrue(Arg);
1831 if (strcasecmp(Var, "SyslogFacility") == 0) {
1832 Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
1833 Conf_SyslogFacility);
1837 if (strcasecmp(Var, "WebircPassword") == 0) {
1838 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
1839 if (len >= sizeof(Conf_WebircPwd))
1840 Config_Error_TooLong(File, Line, Var);
1844 Config_Error_Section(File, Line, Var, "Options");
1850 * Handle variable in [SSL] configuration section.
1852 * @param Line Line numer in configuration file.
1853 * @param Var Variable name.
1854 * @param Arg Variable argument.
1857 Handle_SSL(const char *File, int Line, char *Var, char *Arg)
1859 assert(File != NULL);
1861 assert(Var != NULL);
1862 assert(Arg != NULL);
1864 if (strcasecmp(Var, "CertFile") == 0) {
1865 assert(Conf_SSLOptions.CertFile == NULL);
1866 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1869 if (strcasecmp(Var, "DHFile") == 0) {
1870 assert(Conf_SSLOptions.DHFile == NULL);
1871 Conf_SSLOptions.DHFile = strdup_warn(Arg);
1874 if (strcasecmp(Var, "KeyFile") == 0) {
1875 assert(Conf_SSLOptions.KeyFile == NULL);
1876 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1879 if (strcasecmp(Var, "KeyFilePassword") == 0) {
1880 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1881 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1882 Config_Error(LOG_ERR,
1883 "%s, line %d (section \"SSL\"): Could not copy %s: %s!",
1884 File, Line, Var, strerror(errno));
1887 if (strcasecmp(Var, "Ports") == 0) {
1888 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1891 if (strcasecmp(Var, "CipherList") == 0) {
1892 assert(Conf_SSLOptions.CipherList == NULL);
1893 Conf_SSLOptions.CipherList = strdup_warn(Arg);
1897 Config_Error_Section(File, Line, Var, "SSL");
1903 * Handle variable in [Operator] configuration section.
1905 * @param Line Line numer in configuration file.
1906 * @param Var Variable name.
1907 * @param Arg Variable argument.
1910 Handle_OPERATOR(const char *File, int Line, char *Var, char *Arg )
1913 struct Conf_Oper *op;
1915 assert( File != NULL );
1917 assert( Var != NULL );
1918 assert( Arg != NULL );
1920 op = array_get(&Conf_Opers, sizeof(*op),
1921 array_length(&Conf_Opers, sizeof(*op)) - 1);
1925 if (strcasecmp(Var, "Name") == 0) {
1926 /* Name of IRC operator */
1927 len = strlcpy(op->name, Arg, sizeof(op->name));
1928 if (len >= sizeof(op->name))
1929 Config_Error_TooLong(File, Line, Var);
1932 if (strcasecmp(Var, "Password") == 0) {
1933 /* Password of IRC operator */
1934 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1935 if (len >= sizeof(op->pwd))
1936 Config_Error_TooLong(File, Line, Var);
1939 if (strcasecmp(Var, "Mask") == 0) {
1941 return; /* Hostname already configured */
1942 op->mask = strdup_warn( Arg );
1946 Config_Error_Section(File, Line, Var, "Operator");
1950 * Handle variable in [Server] configuration section.
1952 * @param Line Line numer in configuration file.
1953 * @param Var Variable name.
1954 * @param Arg Variable argument.
1957 Handle_SERVER(const char *File, int Line, char *Var, char *Arg )
1962 assert( File != NULL );
1964 assert( Var != NULL );
1965 assert( Arg != NULL );
1967 /* Ignore server block if no space is left in server configuration structure */
1968 if( New_Server_Idx <= NONE ) return;
1970 if( strcasecmp( Var, "Host" ) == 0 ) {
1971 /* Hostname of the server */
1972 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1973 if (len >= sizeof( New_Server.host ))
1974 Config_Error_TooLong(File, Line, Var);
1977 if( strcasecmp( Var, "Name" ) == 0 ) {
1978 /* Name of the server ("Nick"/"ID") */
1979 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1980 if (len >= sizeof( New_Server.name ))
1981 Config_Error_TooLong(File, Line, Var);
1984 if (strcasecmp(Var, "Bind") == 0) {
1985 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1988 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1989 NGIRCd_ConfFile, Line, Arg);
1992 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1993 /* Password of this server which is sent to the peer */
1995 Config_Error(LOG_ERR,
1996 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1997 NGIRCd_ConfFile, Line);
1999 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
2000 if (len >= sizeof( New_Server.pwd_in ))
2001 Config_Error_TooLong(File, Line, Var);
2004 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
2005 /* Passwort of the peer which must be received */
2006 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
2007 if (len >= sizeof( New_Server.pwd_out ))
2008 Config_Error_TooLong(File, Line, Var);
2011 if( strcasecmp( Var, "Port" ) == 0 ) {
2012 /* Port to which this server should connect */
2014 if (port >= 0 && port < 0xFFFF)
2015 New_Server.port = (UINT16)port;
2017 Config_Error(LOG_ERR,
2018 "%s, line %d (section \"Server\"): Illegal port number %ld!",
2019 NGIRCd_ConfFile, Line, port );
2023 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
2024 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
2028 if( strcasecmp( Var, "Group" ) == 0 ) {
2030 New_Server.group = atoi( Arg );
2031 if (!New_Server.group && strcmp(Arg, "0"))
2032 Config_Error_NaN(File, Line, Var);
2035 if( strcasecmp( Var, "Passive" ) == 0 ) {
2036 if (Check_ArgIsTrue(Arg))
2037 New_Server.flags |= CONF_SFLAG_DISABLED;
2040 if (strcasecmp(Var, "ServiceMask") == 0) {
2041 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
2042 sizeof(New_Server.svs_mask));
2043 if (len >= sizeof(New_Server.svs_mask))
2044 Config_Error_TooLong(File, Line, Var);
2048 Config_Error_Section(File, Line, Var, "Server");
2052 * Copy channel name into channel structure.
2054 * If the channel name is not valid because of a missing prefix ('#', '&'),
2055 * a default prefix of '#' will be added.
2057 * @param new_chan New already allocated channel structure.
2058 * @param name Name of the new channel.
2059 * @returns true on success, false otherwise.
2062 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
2064 size_t size = sizeof(new_chan->name);
2065 char *dest = new_chan->name;
2067 if (!Channel_IsValidName(name)) {
2069 * maybe user forgot to add a '#'.
2070 * This is only here for user convenience.
2076 return size > strlcpy(dest, name, size);
2080 * Handle variable in [Channel] configuration section.
2082 * @param Line Line numer in configuration file.
2083 * @param Var Variable name.
2084 * @param Arg Variable argument.
2087 Handle_CHANNEL(const char *File, int Line, char *Var, char *Arg)
2090 struct Conf_Channel *chan;
2092 assert( File != NULL );
2094 assert( Var != NULL );
2095 assert( Arg != NULL );
2097 chan = array_get(&Conf_Channels, sizeof(*chan),
2098 array_length(&Conf_Channels, sizeof(*chan)) - 1);
2102 if (strcasecmp(Var, "Name") == 0) {
2103 if (!Handle_Channelname(chan, Arg))
2104 Config_Error_TooLong(File, Line, Var);
2107 if (strcasecmp(Var, "Modes") == 0) {
2109 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
2110 if (len >= sizeof(chan->modes))
2111 Config_Error_TooLong(File, Line, Var);
2114 if( strcasecmp( Var, "Topic" ) == 0 ) {
2116 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
2117 if (len >= sizeof(chan->topic))
2118 Config_Error_TooLong(File, Line, Var);
2121 if( strcasecmp( Var, "Key" ) == 0 ) {
2122 /* Initial Channel Key (mode k) */
2123 len = strlcpy(chan->key, Arg, sizeof(chan->key));
2124 if (len >= sizeof(chan->key))
2125 Config_Error_TooLong(File, Line, Var);
2128 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
2129 /* maximum user limit, mode l */
2130 chan->maxusers = (unsigned long) atol(Arg);
2131 if (!chan->maxusers && strcmp(Arg, "0"))
2132 Config_Error_NaN(File, Line, Var);
2135 if (strcasecmp(Var, "KeyFile") == 0) {
2137 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
2138 if (len >= sizeof(chan->keyfile))
2139 Config_Error_TooLong(File, Line, Var);
2143 Config_Error_Section(File, Line, Var, "Channel");
2147 * Validate server configuration.
2149 * Please note that this function uses exit(1) on fatal errors and therefore
2150 * can result in ngIRCd terminating!
2152 * @param Configtest true if the daemon has been called with "--configtest".
2153 * @param Rehash true if re-reading configuration on runtime.
2154 * @returns true if configuration is valid.
2157 Validate_Config(bool Configtest, bool Rehash)
2159 /* Validate configuration settings. */
2162 int i, servers, servers_once;
2164 bool config_valid = true;
2167 /* Emit a warning when the config file is not a full path name */
2168 if (NGIRCd_ConfFile[0] && NGIRCd_ConfFile[0] != '/') {
2169 Config_Error(LOG_WARNING,
2170 "Not specifying a full path name to \"%s\" can cause problems when rehashing the server!",
2174 /* Validate configured server name, see RFC 2812 section 2.3.1 */
2175 ptr = Conf_ServerName;
2177 if (*ptr >= 'a' && *ptr <= 'z') continue;
2178 if (*ptr >= 'A' && *ptr <= 'Z') continue;
2179 if (*ptr >= '0' && *ptr <= '9') continue;
2180 if (ptr > Conf_ServerName) {
2181 if (*ptr == '.' || *ptr == '-')
2184 Conf_ServerName[0] = '\0';
2188 if (!Conf_ServerName[0]) {
2189 /* No server name configured! */
2190 config_valid = false;
2191 Config_Error(LOG_ALERT,
2192 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
2194 if (!Configtest && !Rehash) {
2195 Config_Error(LOG_ALERT,
2196 "%s exiting due to fatal errors!",
2202 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
2203 /* No dot in server name! */
2204 config_valid = false;
2205 Config_Error(LOG_ALERT,
2206 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
2209 Config_Error(LOG_ALERT,
2210 "%s exiting due to fatal errors!",
2217 if (!Conf_ServerAdminMail[0]) {
2218 /* No administrative contact configured! */
2219 config_valid = false;
2220 Config_Error(LOG_ALERT,
2221 "No administrator email address configured in \"%s\" ('AdminEMail')!",
2224 Config_Error(LOG_ALERT,
2225 "%s exiting due to fatal errors!",
2232 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
2233 && !Conf_ServerAdminMail[0]) {
2234 /* No administrative information configured! */
2235 Config_Error(LOG_WARNING,
2236 "No administrative information configured but required by RFC!");
2240 if (Conf_ServerPwd[0])
2241 Config_Error(LOG_ERR,
2242 "This server uses PAM, \"Password\" in [Global] section will be ignored!");
2246 servers = servers_once = 0;
2247 for (i = 0; i < MAX_SERVERS; i++) {
2248 if (Conf_Server[i].name[0]) {
2250 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
2255 "Configuration: Operators=%ld, Servers=%d[%d], Channels=%ld",
2256 array_length(&Conf_Opers, sizeof(struct Conf_Oper)),
2257 servers, servers_once,
2258 array_length(&Conf_Channels, sizeof(struct Conf_Channel)));
2261 return config_valid;
2265 * Output "line too long" warning.
2267 * @param Line Line number in configuration file.
2268 * @param Item Affected variable name.
2271 Config_Error_TooLong(const char *File, const int Line, const char *Item)
2273 Config_Error(LOG_WARNING, "%s, line %d: Value of \"%s\" too long!",
2278 * Output "unknown variable" warning.
2280 * @param Line Line number in configuration file.
2281 * @param Item Affected variable name.
2282 * @param Section Section name.
2285 Config_Error_Section(const char *File, const int Line, const char *Item,
2286 const char *Section)
2288 Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
2289 File, Line, Section, Item);
2293 * Output "not a number" warning.
2295 * @param Line Line number in configuration file.
2296 * @param Item Affected variable name.
2299 Config_Error_NaN(const char *File, const int Line, const char *Item )
2301 Config_Error(LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
2306 * Output configuration error to console and/or logfile.
2308 * On runtime, the normal log functions of the daemon are used. But when
2309 * testing the configuration ("--configtest"), all messages go directly
2312 * @param Level Severity level of the message.
2313 * @param Format Format string; see printf() function.
2316 static void Config_Error( const int Level, const char *Format, ... )
2318 static void Config_Error( Level, Format, va_alist )
2324 char msg[MAX_LOG_MSG_LEN];
2327 assert( Format != NULL );
2330 va_start( ap, Format );
2334 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
2338 if (Level <= LOG_WARNING)
2339 printf(" - %s\n", msg);
2343 Log(Level, "%s", msg);
2349 * Dump internal state of the "configuration module".
2352 Conf_DebugDump(void)
2356 Log(LOG_DEBUG, "Configured servers:");
2357 for (i = 0; i < MAX_SERVERS; i++) {
2358 if (! Conf_Server[i].name[0])
2361 " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
2362 Conf_Server[i].name, Conf_Server[i].host,
2363 Conf_Server[i].port, Conf_Server[i].lasttry,
2364 Conf_Server[i].group, Conf_Server[i].flags,
2365 Conf_Server[i].conn_id);
2372 * Initialize server configuration structure to default values.
2374 * @param Server Pointer to server structure to initialize.
2377 Init_Server_Struct( CONF_SERVER *Server )
2379 assert( Server != NULL );
2381 memset( Server, 0, sizeof (CONF_SERVER) );
2383 Server->group = NONE;
2384 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
2386 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
2388 Proc_InitStruct(&Server->res_stat);
2389 Server->conn_id = NONE;
2390 memset(&Server->bind_addr, 0, sizeof(Server->bind_addr));