2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2012 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];
58 static void Set_Defaults PARAMS(( bool InitServers ));
59 static bool Read_Config PARAMS(( bool TestOnly, bool IsStarting ));
60 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
62 static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
63 static void Handle_LIMITS PARAMS(( int Line, char *Var, char *Arg ));
64 static void Handle_OPTIONS PARAMS(( int Line, char *Var, char *Arg ));
65 static void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
66 static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
67 static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
69 static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
71 static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
72 static void Config_Error_Section PARAMS(( const int Line, const char *Item,
73 const char *Section ));
74 static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
76 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
80 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
82 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
88 static void Handle_SSL PARAMS(( int Line, char *Var, char *Ark ));
90 struct SSLOptions Conf_SSLOptions;
93 * Initialize SSL configuration.
98 free(Conf_SSLOptions.KeyFile);
99 Conf_SSLOptions.KeyFile = NULL;
101 free(Conf_SSLOptions.CertFile);
102 Conf_SSLOptions.CertFile = NULL;
104 free(Conf_SSLOptions.DHFile);
105 Conf_SSLOptions.DHFile = NULL;
106 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
108 array_free(&Conf_SSLOptions.ListenPorts);
112 * Check if the current configuration uses/requires SSL.
114 * @returns true if SSL is used and should be initialized.
121 /* SSL listen ports configured? */
122 if (array_bytes(&Conf_SSLOptions.ListenPorts))
125 for (i = 0; i < MAX_SERVERS; i++) {
126 if (Conf_Server[i].port > 0
127 && Conf_Server[i].SSLConnect)
134 * Make sure that a configured file is readable.
136 * Currently, this function is only used for SSL-related options ...
138 * @param Var Configuration variable
139 * @param Filename Configured filename
142 CheckFileReadable(const char *Var, const char *Filename)
149 fp = fopen(Filename, "r");
153 Config_Error(LOG_ERR, "Can't read \"%s\" (\"%s\"): %s",
154 Filename, Var, strerror(errno));
161 * Duplicate string and warn on errors.
163 * @returns Pointer to string on success, NULL otherwise.
166 strdup_warn(const char *str)
168 char *ptr = strdup(str);
170 Config_Error(LOG_ERR,
171 "Could not allocate memory for string: %s", str);
176 * Output a comma separated list of ports (integer values).
183 len = array_length(a, sizeof(UINT16));
185 ports = (UINT16*) array_start(a);
186 printf("%u", (unsigned int) *ports);
189 printf(", %u", (unsigned int) *ports);
196 * Parse a comma separated string into an array of port numbers (integers).
199 ports_parse(array *a, int Line, char *Arg)
207 ptr = strtok( Arg, "," );
211 if (port > 0 && port < 0xFFFF) {
212 port16 = (UINT16) port;
213 if (!array_catb(a, (char*)&port16, sizeof port16))
214 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
215 NGIRCd_ConfFile, Line, port, strerror(errno));
217 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
218 NGIRCd_ConfFile, Line, port );
221 ptr = strtok( NULL, "," );
226 * Initialize configuration module.
231 Read_Config(false, true);
232 Validate_Config(false, false);
236 * "Rehash" (reload) server configuration.
238 * @returns true if configuration has been re-read, false on errors.
243 if (!Read_Config(false, false))
245 Validate_Config(false, true);
247 /* Update CLIENT structure of local server */
248 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
253 * Output a boolean value as "yes/no" string.
256 yesno_to_str(int boolean_value)
264 * Free all IRC operator configuration structures.
269 struct Conf_Oper *op;
272 len = array_length(&Conf_Opers, sizeof(*op));
273 op = array_start(&Conf_Opers);
278 array_free(&Conf_Opers);
282 * Output all IRC operator configuration structures.
287 struct Conf_Oper *op;
290 count = array_length(&Conf_Opers, sizeof(*op));
291 op = array_start(&Conf_Opers);
292 for (i = 0; i < count; i++, op++) {
297 printf(" Name = %s\n", op->name);
298 printf(" Password = %s\n", op->pwd);
299 printf(" Mask = %s\n\n", op->mask ? op->mask : "");
304 * Read configuration, validate and output it.
306 * This function waits for a keypress of the user when stdin/stdout are valid
307 * tty's ("you can read our nice message and we can read in your keypress").
309 * @return 0 on succes, 1 on failure(s); therefore the result code can
310 * directly be used by exit() when running "ngircd --configtest".
319 size_t predef_channel_count;
320 struct Conf_Channel *predef_chan;
324 if (!Read_Config(true, true))
327 config_valid = Validate_Config(true, false);
330 if(isatty(fileno(stdin)) && isatty(fileno(stdout))) {
331 puts("OK, press enter to see a dump of your server configuration ...");
334 puts("Ok, dump of your server configuration follows:\n");
337 printf(" Name = %s\n", Conf_ServerName);
338 printf(" AdminInfo1 = %s\n", Conf_ServerAdmin1);
339 printf(" AdminInfo2 = %s\n", Conf_ServerAdmin2);
340 printf(" AdminEMail = %s\n", Conf_ServerAdminMail);
341 printf(" Info = %s\n", Conf_ServerInfo);
342 printf(" Listen = %s\n", Conf_ListenAddress);
343 if (Using_MotdFile) {
344 printf(" MotdFile = %s\n", Conf_MotdFile);
345 printf(" MotdPhrase =\n");
347 printf(" MotdFile = \n");
348 printf(" MotdPhrase = %s\n", array_bytes(&Conf_Motd)
349 ? (const char*) array_start(&Conf_Motd) : "");
352 printf(" Password = %s\n", Conf_ServerPwd);
354 printf(" PidFile = %s\n", Conf_PidFile);
356 ports_puts(&Conf_ListenPorts);
357 grp = getgrgid(Conf_GID);
359 printf(" ServerGID = %s\n", grp->gr_name);
361 printf(" ServerGID = %ld\n", (long)Conf_GID);
362 pwd = getpwuid(Conf_UID);
364 printf(" ServerUID = %s\n", pwd->pw_name);
366 printf(" ServerUID = %ld\n", (long)Conf_UID);
370 printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
371 printf(" MaxConnections = %d\n", Conf_MaxConnections);
372 printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
373 printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
374 printf(" MaxNickLength = %u\n", Conf_MaxNickLength - 1);
375 printf(" MaxListSize = %d\n", Conf_MaxListSize);
376 printf(" PingTimeout = %d\n", Conf_PingTimeout);
377 printf(" PongTimeout = %d\n", Conf_PongTimeout);
381 printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
382 printf(" ChrootDir = %s\n", Conf_Chroot);
383 printf(" CloakHost = %s\n", Conf_CloakHost);
384 printf(" CloakHostModeX = %s\n", Conf_CloakHostModeX);
385 printf(" CloakHostSalt = %s\n", Conf_CloakHostSalt);
386 printf(" CloakUserToNick = %s\n", yesno_to_str(Conf_CloakUserToNick));
388 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
389 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
391 printf(" DNS = %s\n", yesno_to_str(Conf_DNS));
393 printf(" Ident = %s\n", yesno_to_str(Conf_Ident));
395 printf(" MorePrivacy = %s\n", yesno_to_str(Conf_MorePrivacy));
396 printf(" NoticeAuth = %s\n", yesno_to_str(Conf_NoticeAuth));
397 printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
398 printf(" OperChanPAutoOp = %s\n", yesno_to_str(Conf_OperChanPAutoOp));
399 printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
401 printf(" PAM = %s\n", yesno_to_str(Conf_PAM));
402 printf(" PAMIsOptional = %s\n", yesno_to_str(Conf_PAMIsOptional));
404 printf(" PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
406 printf(" RequireAuthPing = %s\n", yesno_to_str(Conf_AuthPing));
408 printf(" ScrubCTCP = %s\n", yesno_to_str(Conf_ScrubCTCP));
410 printf(" SyslogFacility = %s\n",
411 ngt_SyslogFacilityName(Conf_SyslogFacility));
413 printf(" WebircPassword = %s\n", Conf_WebircPwd);
418 printf(" CertFile = %s\n", Conf_SSLOptions.CertFile
419 ? Conf_SSLOptions.CertFile : "");
420 printf(" DHFile = %s\n", Conf_SSLOptions.DHFile
421 ? Conf_SSLOptions.DHFile : "");
422 printf(" KeyFile = %s\n", Conf_SSLOptions.KeyFile
423 ? Conf_SSLOptions.KeyFile : "");
424 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
425 puts(" KeyFilePassword = <secret>");
427 puts(" KeyFilePassword = ");
428 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
430 ports_puts(&Conf_SSLOptions.ListenPorts);
436 for( i = 0; i < MAX_SERVERS; i++ ) {
437 if( ! Conf_Server[i].name[0] ) continue;
439 /* Valid "Server" section */
441 printf( " Name = %s\n", Conf_Server[i].name );
442 printf( " Host = %s\n", Conf_Server[i].host );
443 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
445 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
447 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
448 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
449 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
450 printf( " Group = %d\n", Conf_Server[i].group );
451 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
454 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
455 predef_chan = array_start(&Conf_Channels);
457 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
458 if (!predef_chan->name[0])
461 /* Valid "Channel" section */
463 printf(" Name = %s\n", predef_chan->name);
464 printf(" Modes = %s\n", predef_chan->modes);
465 printf(" Key = %s\n", predef_chan->key);
466 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
467 printf(" Topic = %s\n", predef_chan->topic);
468 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
471 return (config_valid ? 0 : 1);
475 * Remove connection information from configured server.
477 * If the server is set as "once", delete it from our configuration;
478 * otherwise set the time for the next connection attempt.
480 * Non-server connections will be silently ignored.
483 Conf_UnsetServer( CONN_ID Idx )
488 /* Check all our configured servers */
489 for( i = 0; i < MAX_SERVERS; i++ ) {
490 if( Conf_Server[i].conn_id != Idx ) continue;
492 /* Gotcha! Mark server configuration as "unused": */
493 Conf_Server[i].conn_id = NONE;
495 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
496 /* Delete configuration here */
497 Init_Server_Struct( &Conf_Server[i] );
499 /* Set time for next connect attempt */
501 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
502 /* The connection has been "long", so we don't
503 * require the next attempt to be delayed. */
504 Conf_Server[i].lasttry =
505 t - Conf_ConnectRetry + RECONNECT_DELAY;
507 /* "Short" connection, enforce "ConnectRetry"
508 * but randomize it a little bit: 15 seconds. */
509 Conf_Server[i].lasttry =
510 t + rand() / (RAND_MAX / 15);
517 * Set connection information for specified configured server.
520 Conf_SetServer( int ConfServer, CONN_ID Idx )
522 assert( ConfServer > NONE );
523 assert( Idx > NONE );
525 if (Conf_Server[ConfServer].conn_id > NONE &&
526 Conf_Server[ConfServer].conn_id != Idx) {
528 "Connection %d: Server configuration of \"%s\" already in use by connection %d!",
529 Idx, Conf_Server[ConfServer].name,
530 Conf_Server[ConfServer].conn_id);
531 Conn_Close(Idx, NULL, "Server configuration already in use", true);
534 Conf_Server[ConfServer].conn_id = Idx;
539 * Get index of server in configuration structure.
542 Conf_GetServer( CONN_ID Idx )
546 assert( Idx > NONE );
548 for( i = 0; i < MAX_SERVERS; i++ ) {
549 if( Conf_Server[i].conn_id == Idx ) return i;
555 * Enable a server by name and adjust its port number.
557 * @returns true if a server has been enabled and now has a valid port
558 * number and host name for outgoing connections.
561 Conf_EnableServer( const char *Name, UINT16 Port )
565 assert( Name != NULL );
566 for( i = 0; i < MAX_SERVERS; i++ ) {
567 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
568 /* Gotcha! Set port and enable server: */
569 Conf_Server[i].port = Port;
570 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
571 return (Conf_Server[i].port && Conf_Server[i].host[0]);
578 * Enable a server by name.
580 * The server is only usable as outgoing server, if it has set a valid port
581 * number for outgoing connections!
582 * If not, you have to use Conf_EnableServer() function to make it available.
584 * @returns true if a server has been enabled; false otherwise.
587 Conf_EnablePassiveServer(const char *Name)
591 assert( Name != NULL );
592 for (i = 0; i < MAX_SERVERS; i++) {
593 if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
594 && (Conf_Server[i].port > 0)) {
595 /* BINGO! Enable server */
596 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
604 * Disable a server by name.
605 * An already established connection will be disconnected.
607 * @returns true if a server was found and has been disabled.
610 Conf_DisableServer( const char *Name )
614 assert( Name != NULL );
615 for( i = 0; i < MAX_SERVERS; i++ ) {
616 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
617 /* Gotcha! Disable and disconnect server: */
618 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
619 if( Conf_Server[i].conn_id > NONE )
620 Conn_Close(Conf_Server[i].conn_id, NULL,
621 "Server link terminated on operator request",
630 * Add a new remote server to our configuration.
632 * @param Name Name of the new server.
633 * @param Port Port number to connect to or 0 for incoming connections.
634 * @param Host Host name to connect to.
635 * @param MyPwd Password that will be sent to the peer.
636 * @param PeerPwd Password that must be received from the peer.
637 * @returns true if the new server has been added; false otherwise.
640 Conf_AddServer(const char *Name, UINT16 Port, const char *Host,
641 const char *MyPwd, const char *PeerPwd)
645 assert( Name != NULL );
646 assert( Host != NULL );
647 assert( MyPwd != NULL );
648 assert( PeerPwd != NULL );
650 /* Search unused item in server configuration structure */
651 for( i = 0; i < MAX_SERVERS; i++ ) {
652 /* Is this item used? */
653 if( ! Conf_Server[i].name[0] ) break;
655 if( i >= MAX_SERVERS ) return false;
657 Init_Server_Struct( &Conf_Server[i] );
658 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
659 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
660 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
661 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
662 Conf_Server[i].port = Port;
663 Conf_Server[i].flags = CONF_SFLAG_ONCE;
669 * Check if the given nickname is reserved for services on a particular server.
671 * @param ConfServer The server index to check.
672 * @param Nick The nickname to check.
673 * @returns true if the given nickname belongs to an "IRC service".
676 Conf_NickIsService(int ConfServer, const char *Nick)
678 assert (ConfServer >= 0);
679 assert (ConfServer < MAX_SERVERS);
681 return MatchCaseInsensitiveList(Conf_Server[ConfServer].svs_mask,
686 * Check if the given nickname is blocked for "normal client" use.
688 * @param ConfServer The server index or NONE to check all configured servers.
689 * @param Nick The nickname to check.
690 * @returns true if the given nickname belongs to an "IRC service".
693 Conf_NickIsBlocked(const char *Nick)
697 for(i = 0; i < MAX_SERVERS; i++) {
698 if (!Conf_Server[i].name[0])
700 if (Conf_NickIsService(i, Nick))
707 * Initialize configuration settings with their default values.
710 Set_Defaults(bool InitServers)
713 char random[RANDOM_SALT_LEN + 1];
716 strcpy(Conf_ServerName, "");
717 strcpy(Conf_ServerAdmin1, "");
718 strcpy(Conf_ServerAdmin2, "");
719 strcpy(Conf_ServerAdminMail, "");
720 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
721 PACKAGE_NAME, PACKAGE_VERSION);
722 free(Conf_ListenAddress);
723 Conf_ListenAddress = NULL;
724 array_free(&Conf_ListenPorts);
725 array_free(&Conf_Motd);
726 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
727 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
728 strcpy(Conf_ServerPwd, "");
729 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
730 Conf_UID = Conf_GID = 0;
733 Conf_ConnectRetry = 60;
734 Conf_MaxConnections = 0;
735 Conf_MaxConnectionsIP = 5;
737 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
738 Conf_MaxListSize = 100;
739 Conf_PingTimeout = 120;
740 Conf_PongTimeout = 20;
743 Conf_AllowRemoteOper = false;
745 Conf_AuthPing = false;
747 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
748 strcpy(Conf_CloakHost, "");
749 strcpy(Conf_CloakHostModeX, "");
750 strlcpy(Conf_CloakHostSalt, ngt_RandomStr(random, RANDOM_SALT_LEN),
751 sizeof(Conf_CloakHostSalt));
752 Conf_CloakUserToNick = false;
753 Conf_ConnectIPv4 = true;
755 Conf_ConnectIPv6 = true;
757 Conf_ConnectIPv6 = false;
765 Conf_MorePrivacy = false;
766 Conf_NoticeAuth = false;
767 Conf_OperCanMode = false;
768 Conf_OperChanPAutoOp = true;
769 Conf_OperServerMode = false;
775 Conf_PAMIsOptional = false;
776 Conf_PredefChannelsOnly = false;
778 Conf_ScrubCTCP = false;
780 Conf_SyslogFacility = LOG_LOCAL5;
782 Conf_SyslogFacility = 0;
786 /* Initialize server configuration structures */
788 for (i = 0; i < MAX_SERVERS;
789 Init_Server_Struct(&Conf_Server[i++]));
794 * Get number of configured listening ports.
796 * @returns The number of ports (IPv4+IPv6) on which the server should listen.
801 size_t cnt = array_bytes(&Conf_ListenPorts);
803 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
809 * Read MOTD ("message of the day") file.
811 * @param filename Name of the file to read.
814 Read_Motd(const char *filename)
819 if (*filename == '\0')
822 fp = fopen(filename, "r");
824 Config_Error(LOG_WARNING, "Can't read MOTD file \"%s\": %s",
825 filename, strerror(errno));
829 array_free(&Conf_Motd);
830 Using_MotdFile = true;
832 while (fgets(line, (int)sizeof line, fp)) {
833 ngt_TrimLastChr( line, '\n');
835 /* add text including \0 */
836 if (!array_catb(&Conf_Motd, line, strlen(line) + 1)) {
837 Log(LOG_WARNING, "Cannot add MOTD text: %s", strerror(errno));
845 * Read ngIRCd configuration file.
847 * Please note that this function uses exit(1) on fatal errors and therefore
848 * can result in ngIRCd terminating!
850 * @param ngircd_starting Flag indicating if ngIRCd is starting or not.
851 * @returns true when the configuration file has been read
852 * successfully; false otherwise.
855 Read_Config(bool TestOnly, bool IsStarting)
857 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
858 const UINT16 defaultport = 6667;
863 /* Open configuration file */
864 fd = fopen( NGIRCd_ConfFile, "r" );
866 /* No configuration file found! */
867 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
868 NGIRCd_ConfFile, strerror( errno ));
871 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
876 Set_Defaults(IsStarting);
879 Config_Error(LOG_INFO,
880 "Reading configuration from \"%s\" ...",
883 /* Clean up server configuration structure: mark all already
884 * configured servers as "once" so that they are deleted
885 * after the next disconnect and delete all unused servers.
886 * And delete all servers which are "duplicates" of servers
887 * that are already marked as "once" (such servers have been
888 * created by the last rehash but are now useless). */
889 for( i = 0; i < MAX_SERVERS; i++ ) {
890 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
892 /* This structure is in use ... */
893 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
894 /* Check for duplicates */
895 for( n = 0; n < MAX_SERVERS; n++ ) {
896 if( n == i ) continue;
898 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
899 Init_Server_Struct( &Conf_Server[n] );
901 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
907 /* Mark server as "once" */
908 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
909 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
914 /* Initialize variables */
916 strcpy( section, "" );
917 Init_Server_Struct( &New_Server );
918 New_Server_Idx = NONE;
922 /* Read configuration file */
924 if( ! fgets( str, LINE_LEN, fd )) break;
928 /* Skip comments and empty lines */
929 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
931 /* Is this the beginning of a new section? */
932 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
933 strlcpy( section, str, sizeof( section ));
934 if (strcasecmp(section, "[GLOBAL]") == 0
935 || strcasecmp(section, "[LIMITS]") == 0
936 || strcasecmp(section, "[OPTIONS]") == 0
938 || strcasecmp(section, "[SSL]") == 0
943 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
944 /* Check if there is already a server to add */
945 if( New_Server.name[0] ) {
946 /* Copy data to "real" server structure */
947 assert( New_Server_Idx > NONE );
948 Conf_Server[New_Server_Idx] = New_Server;
951 /* Re-init structure for new server */
952 Init_Server_Struct( &New_Server );
954 /* Search unused item in server configuration structure */
955 for( i = 0; i < MAX_SERVERS; i++ ) {
956 /* Is this item used? */
957 if( ! Conf_Server[i].name[0] ) break;
959 if( i >= MAX_SERVERS ) {
960 /* Oops, no free item found! */
961 Config_Error( LOG_ERR, "Too many servers configured." );
962 New_Server_Idx = NONE;
964 else New_Server_Idx = i;
968 if (strcasecmp(section, "[CHANNEL]") == 0) {
969 count = array_length(&Conf_Channels,
970 sizeof(struct Conf_Channel));
971 if (!array_alloc(&Conf_Channels,
972 sizeof(struct Conf_Channel),
974 Config_Error(LOG_ERR,
975 "Could not allocate memory for new operator (line %d)",
981 if (strcasecmp(section, "[OPERATOR]") == 0) {
982 count = array_length(&Conf_Opers,
983 sizeof(struct Conf_Oper));
984 if (!array_alloc(&Conf_Opers,
985 sizeof(struct Conf_Oper),
987 Config_Error(LOG_ERR,
988 "Could not allocate memory for new channel (line &d)",
994 Config_Error(LOG_ERR,
995 "%s, line %d: Unknown section \"%s\"!",
996 NGIRCd_ConfFile, line, section);
999 if( section[0] == 0x1 ) continue;
1001 /* Split line into variable name and parameters */
1002 ptr = strchr( str, '=' );
1004 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
1008 var = str; ngt_TrimStr( var );
1009 arg = ptr + 1; ngt_TrimStr( arg );
1011 if(strcasecmp(section, "[GLOBAL]") == 0)
1012 Handle_GLOBAL(line, var, arg);
1013 else if(strcasecmp(section, "[LIMITS]") == 0)
1014 Handle_LIMITS(line, var, arg);
1015 else if(strcasecmp(section, "[OPTIONS]") == 0)
1016 Handle_OPTIONS(line, var, arg);
1018 else if(strcasecmp(section, "[SSL]") == 0)
1019 Handle_SSL(line, var, arg);
1021 else if(strcasecmp(section, "[OPERATOR]") == 0)
1022 Handle_OPERATOR(line, var, arg);
1023 else if(strcasecmp(section, "[SERVER]") == 0)
1024 Handle_SERVER(line, var, arg);
1025 else if(strcasecmp(section, "[CHANNEL]") == 0)
1026 Handle_CHANNEL(line, var, arg);
1028 Config_Error(LOG_ERR,
1029 "%s, line %d: Variable \"%s\" outside section!",
1030 NGIRCd_ConfFile, line, var);
1033 /* Close configuration file */
1036 /* Check if there is still a server to add */
1037 if( New_Server.name[0] ) {
1038 /* Copy data to "real" server structure */
1039 assert( New_Server_Idx > NONE );
1040 Conf_Server[New_Server_Idx] = New_Server;
1043 /* not a single listening port? Add default. */
1044 if (no_listenports() &&
1045 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
1047 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
1048 (unsigned int) defaultport, strerror(errno));
1053 if (!Conf_ListenAddress)
1054 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
1056 if (!Conf_ListenAddress) {
1057 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
1061 /* No MOTD phrase configured? (re)try motd file. */
1062 if (array_bytes(&Conf_Motd) == 0)
1063 Read_Motd(Conf_MotdFile);
1066 /* Make sure that all SSL-related files are readable */
1067 CheckFileReadable("CertFile", Conf_SSLOptions.CertFile);
1068 CheckFileReadable("DHFile", Conf_SSLOptions.DHFile);
1069 CheckFileReadable("KeyFile", Conf_SSLOptions.KeyFile);
1076 * Check whether a string argument is "true" or "false".
1078 * @param Arg Input string.
1079 * @returns true if the input string has been parsed as "yes", "true"
1080 * (case insensitive) or a non-zero integer value.
1083 Check_ArgIsTrue(const char *Arg)
1085 if (strcasecmp(Arg, "yes") == 0)
1087 if (strcasecmp(Arg, "true") == 0)
1096 * Handle setting of "MaxNickLength".
1098 * @param Line Line number in configuration file.
1099 * @raram Arg Input string.
1100 * @returns New configured maximum nickname length.
1103 Handle_MaxNickLength(int Line, const char *Arg)
1107 new = (unsigned) atoi(Arg) + 1;
1108 if (new > CLIENT_NICK_LEN) {
1109 Config_Error(LOG_WARNING,
1110 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
1111 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
1112 return CLIENT_NICK_LEN;
1115 Config_Error(LOG_WARNING,
1116 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
1117 NGIRCd_ConfFile, Line);
1124 * Output a warning messages if IDENT is configured but not compiled in.
1127 WarnIdent(int UNUSED Line)
1131 /* user has enabled ident lookups explicitly, but ... */
1132 Config_Error(LOG_WARNING,
1133 "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
1134 NGIRCd_ConfFile, Line);
1140 * Output a warning messages if IPv6 is configured but not compiled in.
1143 WarnIPv6(int UNUSED Line)
1146 if (Conf_ConnectIPv6) {
1147 /* user has enabled IPv6 explicitly, but ... */
1148 Config_Error(LOG_WARNING,
1149 "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
1150 NGIRCd_ConfFile, Line);
1156 * Output a warning messages if PAM is configured but not compiled in.
1159 WarnPAM(int UNUSED Line)
1163 Config_Error(LOG_WARNING,
1164 "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
1165 NGIRCd_ConfFile, Line);
1171 * Handle legacy "NoXXX" options in [GLOBAL] section.
1173 * TODO: This function and support for "NoXXX" could be removed starting
1174 * with ngIRCd release 19 (one release after marking it "deprecated").
1176 * @param Var Variable name.
1177 * @param Arg Argument string.
1178 * @returns true if a NoXXX option has been processed; false otherwise.
1181 CheckLegacyNoOption(const char *Var, const char *Arg)
1183 if(strcasecmp(Var, "NoDNS") == 0) {
1184 Conf_DNS = !Check_ArgIsTrue( Arg );
1187 if (strcasecmp(Var, "NoIdent") == 0) {
1188 Conf_Ident = !Check_ArgIsTrue(Arg);
1191 if(strcasecmp(Var, "NoPAM") == 0) {
1192 Conf_PAM = !Check_ArgIsTrue(Arg);
1199 * Handle deprecated legacy options in [GLOBAL] section.
1201 * TODO: This function and support for these options in the [Global] section
1202 * could be removed starting with ngIRCd release 19 (one release after
1203 * marking it "deprecated").
1205 * @param Var Variable name.
1206 * @param Arg Argument string.
1207 * @returns true if a legacy option has been processed; false otherwise.
1210 CheckLegacyGlobalOption(int Line, char *Var, char *Arg)
1212 if (strcasecmp(Var, "AllowRemoteOper") == 0
1213 || strcasecmp(Var, "ChrootDir") == 0
1214 || strcasecmp(Var, "ConnectIPv4") == 0
1215 || strcasecmp(Var, "ConnectIPv6") == 0
1216 || strcasecmp(Var, "OperCanUseMode") == 0
1217 || strcasecmp(Var, "OperChanPAutoOp") == 0
1218 || strcasecmp(Var, "OperServerMode") == 0
1219 || strcasecmp(Var, "PredefChannelsOnly") == 0
1220 || strcasecmp(Var, "SyslogFacility") == 0
1221 || strcasecmp(Var, "WebircPassword") == 0) {
1222 Handle_OPTIONS(Line, Var, Arg);
1225 if (strcasecmp(Var, "ConnectRetry") == 0
1226 || strcasecmp(Var, "MaxConnections") == 0
1227 || strcasecmp(Var, "MaxConnectionsIP") == 0
1228 || strcasecmp(Var, "MaxJoins") == 0
1229 || strcasecmp(Var, "MaxNickLength") == 0
1230 || strcasecmp(Var, "PingTimeout") == 0
1231 || strcasecmp(Var, "PongTimeout") == 0) {
1232 Handle_LIMITS(Line, Var, Arg);
1236 if (strcasecmp(Var, "SSLCertFile") == 0
1237 || strcasecmp(Var, "SSLDHFile") == 0
1238 || strcasecmp(Var, "SSLKeyFile") == 0
1239 || strcasecmp(Var, "SSLKeyFilePassword") == 0
1240 || strcasecmp(Var, "SSLPorts") == 0) {
1241 Handle_SSL(Line, Var + 3, Arg);
1250 * Strip "no" prefix of a string.
1252 * TODO: This function and support for "NoXXX" should be removed starting
1253 * with ngIRCd release 19! (One release after marking it "deprecated").
1255 * @param str Pointer to input string starting with "no".
1256 * @returns New pointer to string without "no" prefix.
1259 NoNo(const char *str)
1261 assert(strncasecmp("no", str, 2) == 0 && str[2]);
1266 * Invert "boolean" string.
1268 * TODO: This function and support for "NoXXX" should be removed starting
1269 * with ngIRCd release 19! (One release after marking it "deprecated").
1271 * @param arg "Boolean" input string.
1272 * @returns Pointer to inverted "boolean string".
1275 InvertArg(const char *arg)
1277 return yesno_to_str(!Check_ArgIsTrue(arg));
1281 * Handle variable in [Global] configuration section.
1283 * @param Line Line numer in configuration file.
1284 * @param Var Variable name.
1285 * @param Arg Variable argument.
1288 Handle_GLOBAL( int Line, char *Var, char *Arg )
1293 const char *section;
1296 assert(Var != NULL);
1297 assert(Arg != NULL);
1299 if (strcasecmp(Var, "Name") == 0) {
1300 len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
1301 if (len >= sizeof(Conf_ServerName))
1302 Config_Error_TooLong(Line, Var);
1305 if (strcasecmp(Var, "AdminInfo1") == 0) {
1306 len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
1307 if (len >= sizeof(Conf_ServerAdmin1))
1308 Config_Error_TooLong(Line, Var);
1311 if (strcasecmp(Var, "AdminInfo2") == 0) {
1312 len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
1313 if (len >= sizeof(Conf_ServerAdmin2))
1314 Config_Error_TooLong(Line, Var);
1317 if (strcasecmp(Var, "AdminEMail") == 0) {
1318 len = strlcpy(Conf_ServerAdminMail, Arg,
1319 sizeof(Conf_ServerAdminMail));
1320 if (len >= sizeof(Conf_ServerAdminMail))
1321 Config_Error_TooLong(Line, Var);
1324 if (strcasecmp(Var, "Info") == 0) {
1325 len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
1326 if (len >= sizeof(Conf_ServerInfo))
1327 Config_Error_TooLong(Line, Var);
1330 if (strcasecmp(Var, "Listen") == 0) {
1331 if (Conf_ListenAddress) {
1332 Config_Error(LOG_ERR,
1333 "Multiple Listen= options, ignoring: %s",
1337 Conf_ListenAddress = strdup_warn(Arg);
1338 /* If allocation fails, we're in trouble: we cannot ignore the
1339 * error -- otherwise ngircd would listen on all interfaces. */
1340 if (!Conf_ListenAddress) {
1341 Config_Error(LOG_ALERT,
1342 "%s exiting due to fatal errors!",
1348 if (strcasecmp(Var, "MotdFile") == 0) {
1349 len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
1350 if (len >= sizeof(Conf_MotdFile))
1351 Config_Error_TooLong(Line, Var);
1354 if (strcasecmp(Var, "MotdPhrase") == 0) {
1358 if (len >= LINE_LEN) {
1359 Config_Error_TooLong(Line, Var);
1362 if (!array_copyb(&Conf_Motd, Arg, len + 1))
1363 Config_Error(LOG_WARNING,
1364 "%s, line %d: Could not append MotdPhrase: %s",
1365 NGIRCd_ConfFile, Line, strerror(errno));
1366 Using_MotdFile = false;
1369 if(strcasecmp(Var, "Password") == 0) {
1370 len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
1371 if (len >= sizeof(Conf_ServerPwd))
1372 Config_Error_TooLong(Line, Var);
1375 if (strcasecmp(Var, "PidFile") == 0) {
1376 len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
1377 if (len >= sizeof(Conf_PidFile))
1378 Config_Error_TooLong(Line, Var);
1381 if (strcasecmp(Var, "Ports") == 0) {
1382 ports_parse(&Conf_ListenPorts, Line, Arg);
1385 if (strcasecmp(Var, "ServerGID") == 0) {
1386 grp = getgrnam(Arg);
1388 Conf_GID = grp->gr_gid;
1390 Conf_GID = (unsigned int)atoi(Arg);
1391 if (!Conf_GID && strcmp(Arg, "0"))
1392 Config_Error(LOG_WARNING,
1393 "%s, line %d: Value of \"%s\" is not a valid group name or ID!",
1394 NGIRCd_ConfFile, Line, Var);
1398 if (strcasecmp(Var, "ServerUID") == 0) {
1399 pwd = getpwnam(Arg);
1401 Conf_UID = pwd->pw_uid;
1403 Conf_UID = (unsigned int)atoi(Arg);
1404 if (!Conf_UID && strcmp(Arg, "0"))
1405 Config_Error(LOG_WARNING,
1406 "%s, line %d: Value of \"%s\" is not a valid user name or ID!",
1407 NGIRCd_ConfFile, Line, Var);
1412 if (CheckLegacyNoOption(Var, Arg)) {
1413 /* TODO: This function and support for "NoXXX" could be
1414 * be removed starting with ngIRCd release 19 (one release
1415 * after marking it "deprecated"). */
1416 Config_Error(LOG_WARNING,
1417 "%s, line %d (section \"Global\"): \"No\"-Prefix is deprecated, use \"%s = %s\" in [Options] section!",
1418 NGIRCd_ConfFile, Line, NoNo(Var), InvertArg(Arg));
1419 if (strcasecmp(Var, "NoIdent") == 0)
1421 else if (strcasecmp(Var, "NoPam") == 0)
1425 if ((section = CheckLegacyGlobalOption(Line, Var, Arg))) {
1426 /** TODO: This function and support for these options in the
1427 * [Global] section could be removed starting with ngIRCd
1428 * release 19 (one release after marking it "deprecated"). */
1429 if (strncasecmp(Var, "SSL", 3) == 0) {
1430 Config_Error(LOG_WARNING,
1431 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s and rename to \"%s\"!",
1432 NGIRCd_ConfFile, Line, Var, section,
1435 Config_Error(LOG_WARNING,
1436 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s!",
1437 NGIRCd_ConfFile, Line, Var, section);
1442 Config_Error_Section(Line, Var, "Global");
1446 * Handle variable in [Limits] configuration section.
1448 * @param Line Line numer in configuration file.
1449 * @param Var Variable name.
1450 * @param Arg Variable argument.
1453 Handle_LIMITS(int Line, char *Var, char *Arg)
1456 assert(Var != NULL);
1457 assert(Arg != NULL);
1459 if (strcasecmp(Var, "ConnectRetry") == 0) {
1460 Conf_ConnectRetry = atoi(Arg);
1461 if (Conf_ConnectRetry < 5) {
1462 Config_Error(LOG_WARNING,
1463 "%s, line %d: Value of \"ConnectRetry\" too low!",
1464 NGIRCd_ConfFile, Line);
1465 Conf_ConnectRetry = 5;
1469 if (strcasecmp(Var, "MaxConnections") == 0) {
1470 Conf_MaxConnections = atoi(Arg);
1471 if (!Conf_MaxConnections && strcmp(Arg, "0"))
1472 Config_Error_NaN(Line, Var);
1475 if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
1476 Conf_MaxConnectionsIP = atoi(Arg);
1477 if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
1478 Config_Error_NaN(Line, Var);
1481 if (strcasecmp(Var, "MaxJoins") == 0) {
1482 Conf_MaxJoins = atoi(Arg);
1483 if (!Conf_MaxJoins && strcmp(Arg, "0"))
1484 Config_Error_NaN(Line, Var);
1487 if (strcasecmp(Var, "MaxNickLength") == 0) {
1488 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1491 if (strcasecmp(Var, "MaxListSize") == 0) {
1492 Conf_MaxListSize = atoi(Arg);
1493 if (!Conf_MaxListSize && strcmp(Arg, "0"))
1494 Config_Error_NaN(Line, Var);
1497 if (strcasecmp(Var, "PingTimeout") == 0) {
1498 Conf_PingTimeout = atoi(Arg);
1499 if (Conf_PingTimeout < 5) {
1500 Config_Error(LOG_WARNING,
1501 "%s, line %d: Value of \"PingTimeout\" too low!",
1502 NGIRCd_ConfFile, Line);
1503 Conf_PingTimeout = 5;
1507 if (strcasecmp(Var, "PongTimeout") == 0) {
1508 Conf_PongTimeout = atoi(Arg);
1509 if (Conf_PongTimeout < 5) {
1510 Config_Error(LOG_WARNING,
1511 "%s, line %d: Value of \"PongTimeout\" too low!",
1512 NGIRCd_ConfFile, Line);
1513 Conf_PongTimeout = 5;
1518 Config_Error_Section(Line, Var, "Limits");
1522 * Handle variable in [Options] configuration section.
1524 * @param Line Line numer in configuration file.
1525 * @param Var Variable name.
1526 * @param Arg Variable argument.
1529 Handle_OPTIONS(int Line, char *Var, char *Arg)
1534 assert(Var != NULL);
1535 assert(Arg != NULL);
1537 if (strcasecmp(Var, "AllowRemoteOper") == 0) {
1538 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1541 if (strcasecmp(Var, "ChrootDir") == 0) {
1542 len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
1543 if (len >= sizeof(Conf_Chroot))
1544 Config_Error_TooLong(Line, Var);
1547 if (strcasecmp(Var, "CloakHost") == 0) {
1548 len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
1549 if (len >= sizeof(Conf_CloakHost))
1550 Config_Error_TooLong(Line, Var);
1553 if (strcasecmp(Var, "CloakHostModeX") == 0) {
1554 len = strlcpy(Conf_CloakHostModeX, Arg, sizeof(Conf_CloakHostModeX));
1555 if (len >= sizeof(Conf_CloakHostModeX))
1556 Config_Error_TooLong(Line, Var);
1559 if (strcasecmp(Var, "CloakHostSalt") == 0) {
1560 len = strlcpy(Conf_CloakHostSalt, Arg, sizeof(Conf_CloakHostSalt));
1561 if (len >= sizeof(Conf_CloakHostSalt))
1562 Config_Error_TooLong(Line, Var);
1565 if (strcasecmp(Var, "CloakUserToNick") == 0) {
1566 Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
1569 if (strcasecmp(Var, "ConnectIPv6") == 0) {
1570 Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
1574 if (strcasecmp(Var, "ConnectIPv4") == 0) {
1575 Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
1578 if (strcasecmp(Var, "DNS") == 0) {
1579 Conf_DNS = Check_ArgIsTrue(Arg);
1582 if (strcasecmp(Var, "Ident") == 0) {
1583 Conf_Ident = Check_ArgIsTrue(Arg);
1587 if (strcasecmp(Var, "MorePrivacy") == 0) {
1588 Conf_MorePrivacy = Check_ArgIsTrue(Arg);
1591 if (strcasecmp(Var, "NoticeAuth") == 0) {
1592 Conf_NoticeAuth = Check_ArgIsTrue(Arg);
1595 if (strcasecmp(Var, "OperCanUseMode") == 0) {
1596 Conf_OperCanMode = Check_ArgIsTrue(Arg);
1599 if (strcasecmp(Var, "OperChanPAutoOp") == 0) {
1600 Conf_OperChanPAutoOp = Check_ArgIsTrue(Arg);
1603 if (strcasecmp(Var, "OperServerMode") == 0) {
1604 Conf_OperServerMode = Check_ArgIsTrue(Arg);
1607 if (strcasecmp(Var, "PAM") == 0) {
1608 Conf_PAM = Check_ArgIsTrue(Arg);
1612 if (strcasecmp(Var, "PAMIsOptional") == 0 ) {
1613 Conf_PAMIsOptional = Check_ArgIsTrue(Arg);
1616 if (strcasecmp(Var, "PredefChannelsOnly") == 0) {
1617 Conf_PredefChannelsOnly = Check_ArgIsTrue(Arg);
1621 if (strcasecmp(Var, "RequireAuthPing") == 0) {
1622 Conf_AuthPing = Check_ArgIsTrue(Arg);
1626 if (strcasecmp(Var, "ScrubCTCP") == 0) {
1627 Conf_ScrubCTCP = Check_ArgIsTrue(Arg);
1631 if (strcasecmp(Var, "SyslogFacility") == 0) {
1632 Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
1633 Conf_SyslogFacility);
1637 if (strcasecmp(Var, "WebircPassword") == 0) {
1638 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
1639 if (len >= sizeof(Conf_WebircPwd))
1640 Config_Error_TooLong(Line, Var);
1644 Config_Error_Section(Line, Var, "Options");
1650 * Handle variable in [SSL] configuration section.
1652 * @param Line Line numer in configuration file.
1653 * @param Var Variable name.
1654 * @param Arg Variable argument.
1657 Handle_SSL(int Line, char *Var, char *Arg)
1660 assert(Var != NULL);
1661 assert(Arg != NULL);
1663 if (strcasecmp(Var, "CertFile") == 0) {
1664 assert(Conf_SSLOptions.CertFile == NULL);
1665 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1668 if (strcasecmp(Var, "DHFile") == 0) {
1669 assert(Conf_SSLOptions.DHFile == NULL);
1670 Conf_SSLOptions.DHFile = strdup_warn(Arg);
1673 if (strcasecmp(Var, "KeyFile") == 0) {
1674 assert(Conf_SSLOptions.KeyFile == NULL);
1675 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1678 if (strcasecmp(Var, "KeyFilePassword") == 0) {
1679 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1680 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1681 Config_Error(LOG_ERR,
1682 "%s, line %d (section \"SSL\"): Could not copy %s: %s!",
1683 NGIRCd_ConfFile, Line, Var,
1687 if (strcasecmp(Var, "Ports") == 0) {
1688 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1692 Config_Error_Section(Line, Var, "SSL");
1698 * Handle variable in [Operator] configuration section.
1700 * @param Line Line numer in configuration file.
1701 * @param Var Variable name.
1702 * @param Arg Variable argument.
1705 Handle_OPERATOR( int Line, char *Var, char *Arg )
1708 struct Conf_Oper *op;
1711 assert( Var != NULL );
1712 assert( Arg != NULL );
1714 op = array_get(&Conf_Opers, sizeof(*op),
1715 array_length(&Conf_Opers, sizeof(*op)) - 1);
1719 if (strcasecmp(Var, "Name") == 0) {
1720 /* Name of IRC operator */
1721 len = strlcpy(op->name, Arg, sizeof(op->name));
1722 if (len >= sizeof(op->name))
1723 Config_Error_TooLong(Line, Var);
1726 if (strcasecmp(Var, "Password") == 0) {
1727 /* Password of IRC operator */
1728 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1729 if (len >= sizeof(op->pwd))
1730 Config_Error_TooLong(Line, Var);
1733 if (strcasecmp(Var, "Mask") == 0) {
1735 return; /* Hostname already configured */
1736 op->mask = strdup_warn( Arg );
1740 Config_Error_Section(Line, Var, "Operator");
1744 * Handle variable in [Server] configuration section.
1746 * @param Line Line numer in configuration file.
1747 * @param Var Variable name.
1748 * @param Arg Variable argument.
1751 Handle_SERVER( int Line, char *Var, char *Arg )
1757 assert( Var != NULL );
1758 assert( Arg != NULL );
1760 /* Ignore server block if no space is left in server configuration structure */
1761 if( New_Server_Idx <= NONE ) return;
1763 if( strcasecmp( Var, "Host" ) == 0 ) {
1764 /* Hostname of the server */
1765 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1766 if (len >= sizeof( New_Server.host ))
1767 Config_Error_TooLong ( Line, Var );
1770 if( strcasecmp( Var, "Name" ) == 0 ) {
1771 /* Name of the server ("Nick"/"ID") */
1772 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1773 if (len >= sizeof( New_Server.name ))
1774 Config_Error_TooLong( Line, Var );
1777 if (strcasecmp(Var, "Bind") == 0) {
1778 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1781 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1782 NGIRCd_ConfFile, Line, Arg);
1785 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1786 /* Password of this server which is sent to the peer */
1788 Config_Error(LOG_ERR,
1789 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1790 NGIRCd_ConfFile, Line);
1792 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1793 if (len >= sizeof( New_Server.pwd_in ))
1794 Config_Error_TooLong( Line, Var );
1797 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1798 /* Passwort of the peer which must be received */
1799 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1800 if (len >= sizeof( New_Server.pwd_out ))
1801 Config_Error_TooLong( Line, Var );
1804 if( strcasecmp( Var, "Port" ) == 0 ) {
1805 /* Port to which this server should connect */
1807 if (port >= 0 && port < 0xFFFF)
1808 New_Server.port = (UINT16)port;
1810 Config_Error(LOG_ERR,
1811 "%s, line %d (section \"Server\"): Illegal port number %ld!",
1812 NGIRCd_ConfFile, Line, port );
1816 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1817 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1821 if( strcasecmp( Var, "Group" ) == 0 ) {
1823 New_Server.group = atoi( Arg );
1824 if (!New_Server.group && strcmp(Arg, "0"))
1825 Config_Error_NaN(Line, Var);
1828 if( strcasecmp( Var, "Passive" ) == 0 ) {
1829 if (Check_ArgIsTrue(Arg))
1830 New_Server.flags |= CONF_SFLAG_DISABLED;
1833 if (strcasecmp(Var, "ServiceMask") == 0) {
1834 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1835 sizeof(New_Server.svs_mask));
1836 if (len >= sizeof(New_Server.svs_mask))
1837 Config_Error_TooLong(Line, Var);
1841 Config_Error_Section(Line, Var, "Server");
1845 * Copy channel name into channel structure.
1847 * If the channel name is not valid because of a missing prefix ('#', '&'),
1848 * a default prefix of '#' will be added.
1850 * @param new_chan New already allocated channel structure.
1851 * @param name Name of the new channel.
1852 * @returns true on success, false otherwise.
1855 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1857 size_t size = sizeof(new_chan->name);
1858 char *dest = new_chan->name;
1860 if (!Channel_IsValidName(name)) {
1862 * maybe user forgot to add a '#'.
1863 * This is only here for user convenience.
1869 return size > strlcpy(dest, name, size);
1873 * Handle variable in [Channel] configuration section.
1875 * @param Line Line numer in configuration file.
1876 * @param Var Variable name.
1877 * @param Arg Variable argument.
1880 Handle_CHANNEL(int Line, char *Var, char *Arg)
1883 struct Conf_Channel *chan;
1886 assert( Var != NULL );
1887 assert( Arg != NULL );
1889 chan = array_get(&Conf_Channels, sizeof(*chan),
1890 array_length(&Conf_Channels, sizeof(*chan)) - 1);
1894 if (strcasecmp(Var, "Name") == 0) {
1895 if (!Handle_Channelname(chan, Arg))
1896 Config_Error_TooLong(Line, Var);
1899 if (strcasecmp(Var, "Modes") == 0) {
1901 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1902 if (len >= sizeof(chan->modes))
1903 Config_Error_TooLong( Line, Var );
1906 if( strcasecmp( Var, "Topic" ) == 0 ) {
1908 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1909 if (len >= sizeof(chan->topic))
1910 Config_Error_TooLong( Line, Var );
1913 if( strcasecmp( Var, "Key" ) == 0 ) {
1914 /* Initial Channel Key (mode k) */
1915 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1916 if (len >= sizeof(chan->key))
1917 Config_Error_TooLong(Line, Var);
1920 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1921 /* maximum user limit, mode l */
1922 chan->maxusers = (unsigned long) atol(Arg);
1923 if (!chan->maxusers && strcmp(Arg, "0"))
1924 Config_Error_NaN(Line, Var);
1927 if (strcasecmp(Var, "KeyFile") == 0) {
1929 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1930 if (len >= sizeof(chan->keyfile))
1931 Config_Error_TooLong(Line, Var);
1935 Config_Error_Section(Line, Var, "Channel");
1939 * Validate server configuration.
1941 * Please note that this function uses exit(1) on fatal errors and therefore
1942 * can result in ngIRCd terminating!
1944 * @param Configtest true if the daemon has been called with "--configtest".
1945 * @param Rehash true if re-reading configuration on runtime.
1946 * @returns true if configuration is valid.
1949 Validate_Config(bool Configtest, bool Rehash)
1951 /* Validate configuration settings. */
1954 int i, servers, servers_once;
1956 bool config_valid = true;
1959 /* Emit a warning when the config file is not a full path name */
1960 if (NGIRCd_ConfFile[0] && NGIRCd_ConfFile[0] != '/') {
1961 Config_Error(LOG_WARNING,
1962 "Not specifying a full path name to \"%s\" can cause problems when rehashing the server!",
1966 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1967 ptr = Conf_ServerName;
1969 if (*ptr >= 'a' && *ptr <= 'z') continue;
1970 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1971 if (*ptr >= '0' && *ptr <= '9') continue;
1972 if (ptr > Conf_ServerName) {
1973 if (*ptr == '.' || *ptr == '-')
1976 Conf_ServerName[0] = '\0';
1980 if (!Conf_ServerName[0]) {
1981 /* No server name configured! */
1982 config_valid = false;
1983 Config_Error(LOG_ALERT,
1984 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1986 if (!Configtest && !Rehash) {
1987 Config_Error(LOG_ALERT,
1988 "%s exiting due to fatal errors!",
1994 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1995 /* No dot in server name! */
1996 config_valid = false;
1997 Config_Error(LOG_ALERT,
1998 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
2001 Config_Error(LOG_ALERT,
2002 "%s exiting due to fatal errors!",
2009 if (!Conf_ServerAdminMail[0]) {
2010 /* No administrative contact configured! */
2011 config_valid = false;
2012 Config_Error(LOG_ALERT,
2013 "No administrator email address configured in \"%s\" ('AdminEMail')!",
2016 Config_Error(LOG_ALERT,
2017 "%s exiting due to fatal errors!",
2024 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
2025 && !Conf_ServerAdminMail[0]) {
2026 /* No administrative information configured! */
2027 Config_Error(LOG_WARNING,
2028 "No administrative information configured but required by RFC!");
2032 if (Conf_ServerPwd[0])
2033 Config_Error(LOG_ERR,
2034 "This server uses PAM, \"Password\" in [Global] section will be ignored!");
2038 servers = servers_once = 0;
2039 for (i = 0; i < MAX_SERVERS; i++) {
2040 if (Conf_Server[i].name[0]) {
2042 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
2047 "Configuration: Operators=%ld, Servers=%d[%d], Channels=%ld",
2048 array_length(&Conf_Opers, sizeof(struct Conf_Oper)),
2049 servers, servers_once,
2050 array_length(&Conf_Channels, sizeof(struct Conf_Channel)));
2053 return config_valid;
2057 * Output "line too long" warning.
2059 * @param Line Line number in configuration file.
2060 * @param Item Affected variable name.
2063 Config_Error_TooLong ( const int Line, const char *Item )
2065 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
2069 * Output "unknown variable" warning.
2071 * @param Line Line number in configuration file.
2072 * @param Item Affected variable name.
2073 * @param Section Section name.
2076 Config_Error_Section(const int Line, const char *Item, const char *Section)
2078 Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
2079 NGIRCd_ConfFile, Line, Section, Item);
2083 * Output "not a number" warning.
2085 * @param Line Line number in configuration file.
2086 * @param Item Affected variable name.
2089 Config_Error_NaN( const int Line, const char *Item )
2091 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
2092 NGIRCd_ConfFile, Line, Item );
2096 * Output configuration error to console and/or logfile.
2098 * On runtime, the normal log functions of the daemon are used. But when
2099 * testing the configuration ("--configtest"), all messages go directly
2102 * @param Level Severity level of the message.
2103 * @param Format Format string; see printf() function.
2106 static void Config_Error( const int Level, const char *Format, ... )
2108 static void Config_Error( Level, Format, va_alist )
2114 char msg[MAX_LOG_MSG_LEN];
2117 assert( Format != NULL );
2120 va_start( ap, Format );
2124 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
2128 if (Level <= LOG_WARNING)
2129 printf(" - %s\n", msg);
2133 Log(Level, "%s", msg);
2139 * Dump internal state of the "configuration module".
2142 Conf_DebugDump(void)
2146 Log(LOG_DEBUG, "Configured servers:");
2147 for (i = 0; i < MAX_SERVERS; i++) {
2148 if (! Conf_Server[i].name[0])
2151 " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
2152 Conf_Server[i].name, Conf_Server[i].host,
2153 Conf_Server[i].port, Conf_Server[i].lasttry,
2154 Conf_Server[i].group, Conf_Server[i].flags,
2155 Conf_Server[i].conn_id);
2162 * Initialize server configuration structur to default values.
2164 * @param Server Pointer to server structure to initialize.
2167 Init_Server_Struct( CONF_SERVER *Server )
2169 assert( Server != NULL );
2171 memset( Server, 0, sizeof (CONF_SERVER) );
2173 Server->group = NONE;
2174 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
2176 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
2178 Proc_InitStruct(&Server->res_stat);
2179 Server->conn_id = NONE;
2180 memset(&Server->bind_addr, 0, sizeof(Server->bind_addr));