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 * Make sure that a configured file is readable.
114 * Currently, this function is only used for SSL-related options ...
116 * @param Var Configuration variable
117 * @param Filename Configured filename
120 CheckFileReadable(const char *Var, const char *Filename)
127 fp = fopen(Filename, "r");
131 Config_Error(LOG_ERR, "Can't read \"%s\" (\"%s\"): %s",
132 Filename, Var, strerror(errno));
139 * Duplicate string and warn on errors.
141 * @returns Pointer to string on success, NULL otherwise.
144 strdup_warn(const char *str)
146 char *ptr = strdup(str);
148 Config_Error(LOG_ERR,
149 "Could not allocate memory for string: %s", str);
154 * Output a comma separated list of ports (integer values).
161 len = array_length(a, sizeof(UINT16));
163 ports = (UINT16*) array_start(a);
164 printf("%u", (unsigned int) *ports);
167 printf(", %u", (unsigned int) *ports);
174 * Parse a comma separated string into an array of port numbers (integers).
177 ports_parse(array *a, int Line, char *Arg)
185 ptr = strtok( Arg, "," );
189 if (port > 0 && port < 0xFFFF) {
190 port16 = (UINT16) port;
191 if (!array_catb(a, (char*)&port16, sizeof port16))
192 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
193 NGIRCd_ConfFile, Line, port, strerror(errno));
195 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
196 NGIRCd_ConfFile, Line, port );
199 ptr = strtok( NULL, "," );
204 * Initialize configuration module.
209 Read_Config(false, true);
210 Validate_Config(false, false);
214 * "Rehash" (reload) server configuration.
216 * @returns true if configuration has been re-read, false on errors.
221 if (!Read_Config(false, false))
223 Validate_Config(false, true);
225 /* Update CLIENT structure of local server */
226 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
231 * Output a boolean value as "yes/no" string.
234 yesno_to_str(int boolean_value)
242 * Free all IRC operator configuration structures.
247 struct Conf_Oper *op;
250 len = array_length(&Conf_Opers, sizeof(*op));
251 op = array_start(&Conf_Opers);
256 array_free(&Conf_Opers);
260 * Output all IRC operator configuration structures.
265 struct Conf_Oper *op;
268 count = array_length(&Conf_Opers, sizeof(*op));
269 op = array_start(&Conf_Opers);
270 for (i = 0; i < count; i++, op++) {
275 printf(" Name = %s\n", op->name);
276 printf(" Password = %s\n", op->pwd);
277 printf(" Mask = %s\n\n", op->mask ? op->mask : "");
282 * Read configuration, validate and output it.
284 * This function waits for a keypress of the user when stdin/stdout are valid
285 * tty's ("you can read our nice message and we can read in your keypress").
287 * @return 0 on succes, 1 on failure(s); therefore the result code can
288 * directly be used by exit() when running "ngircd --configtest".
297 size_t predef_channel_count;
298 struct Conf_Channel *predef_chan;
302 if (!Read_Config(true, true))
305 config_valid = Validate_Config(true, false);
308 if(isatty(fileno(stdin)) && isatty(fileno(stdout))) {
309 puts("OK, press enter to see a dump of your server configuration ...");
312 puts("Ok, dump of your server configuration follows:\n");
315 printf(" Name = %s\n", Conf_ServerName);
316 printf(" AdminInfo1 = %s\n", Conf_ServerAdmin1);
317 printf(" AdminInfo2 = %s\n", Conf_ServerAdmin2);
318 printf(" AdminEMail = %s\n", Conf_ServerAdminMail);
319 printf(" Info = %s\n", Conf_ServerInfo);
320 printf(" Listen = %s\n", Conf_ListenAddress);
321 if (Using_MotdFile) {
322 printf(" MotdFile = %s\n", Conf_MotdFile);
323 printf(" MotdPhrase =\n");
325 printf(" MotdFile = \n");
326 printf(" MotdPhrase = %s\n", array_bytes(&Conf_Motd)
327 ? (const char*) array_start(&Conf_Motd) : "");
330 printf(" Password = %s\n", Conf_ServerPwd);
332 printf(" PidFile = %s\n", Conf_PidFile);
334 ports_puts(&Conf_ListenPorts);
335 grp = getgrgid(Conf_GID);
337 printf(" ServerGID = %s\n", grp->gr_name);
339 printf(" ServerGID = %ld\n", (long)Conf_GID);
340 pwd = getpwuid(Conf_UID);
342 printf(" ServerUID = %s\n", pwd->pw_name);
344 printf(" ServerUID = %ld\n", (long)Conf_UID);
348 printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
349 printf(" MaxConnections = %d\n", Conf_MaxConnections);
350 printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
351 printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
352 printf(" MaxNickLength = %u\n", Conf_MaxNickLength - 1);
353 printf(" MaxListSize = %d\n", Conf_MaxListSize);
354 printf(" PingTimeout = %d\n", Conf_PingTimeout);
355 printf(" PongTimeout = %d\n", Conf_PongTimeout);
359 printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
360 printf(" ChrootDir = %s\n", Conf_Chroot);
361 printf(" CloakHost = %s\n", Conf_CloakHost);
362 printf(" CloakHostModeX = %s\n", Conf_CloakHostModeX);
363 printf(" CloakHostSalt = %s\n", Conf_CloakHostSalt);
364 printf(" CloakUserToNick = %s\n", yesno_to_str(Conf_CloakUserToNick));
366 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
367 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
369 printf(" DNS = %s\n", yesno_to_str(Conf_DNS));
371 printf(" Ident = %s\n", yesno_to_str(Conf_Ident));
373 printf(" MorePrivacy = %s\n", yesno_to_str(Conf_MorePrivacy));
374 printf(" NoticeAuth = %s\n", yesno_to_str(Conf_NoticeAuth));
375 printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
376 printf(" OperChanPAutoOp = %s\n", yesno_to_str(Conf_OperChanPAutoOp));
377 printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
379 printf(" PAM = %s\n", yesno_to_str(Conf_PAM));
380 printf(" PAMIsOptional = %s\n", yesno_to_str(Conf_PAMIsOptional));
382 printf(" PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
384 printf(" RequireAuthPing = %s\n", yesno_to_str(Conf_AuthPing));
386 printf(" ScrubCTCP = %s\n", yesno_to_str(Conf_ScrubCTCP));
388 printf(" SyslogFacility = %s\n",
389 ngt_SyslogFacilityName(Conf_SyslogFacility));
391 printf(" WebircPassword = %s\n", Conf_WebircPwd);
396 printf(" CertFile = %s\n", Conf_SSLOptions.CertFile
397 ? Conf_SSLOptions.CertFile : "");
398 printf(" DHFile = %s\n", Conf_SSLOptions.DHFile
399 ? Conf_SSLOptions.DHFile : "");
400 printf(" KeyFile = %s\n", Conf_SSLOptions.KeyFile
401 ? Conf_SSLOptions.KeyFile : "");
402 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
403 puts(" KeyFilePassword = <secret>");
405 puts(" KeyFilePassword = ");
406 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
408 ports_puts(&Conf_SSLOptions.ListenPorts);
414 for( i = 0; i < MAX_SERVERS; i++ ) {
415 if( ! Conf_Server[i].name[0] ) continue;
417 /* Valid "Server" section */
419 printf( " Name = %s\n", Conf_Server[i].name );
420 printf( " Host = %s\n", Conf_Server[i].host );
421 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
423 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
425 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
426 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
427 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
428 printf( " Group = %d\n", Conf_Server[i].group );
429 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
432 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
433 predef_chan = array_start(&Conf_Channels);
435 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
436 if (!predef_chan->name[0])
439 /* Valid "Channel" section */
441 printf(" Name = %s\n", predef_chan->name);
442 printf(" Modes = %s\n", predef_chan->modes);
443 printf(" Key = %s\n", predef_chan->key);
444 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
445 printf(" Topic = %s\n", predef_chan->topic);
446 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
449 return (config_valid ? 0 : 1);
453 * Remove connection information from configured server.
455 * If the server is set as "once", delete it from our configuration;
456 * otherwise set the time for the next connection attempt.
458 * Non-server connections will be silently ignored.
461 Conf_UnsetServer( CONN_ID Idx )
466 /* Check all our configured servers */
467 for( i = 0; i < MAX_SERVERS; i++ ) {
468 if( Conf_Server[i].conn_id != Idx ) continue;
470 /* Gotcha! Mark server configuration as "unused": */
471 Conf_Server[i].conn_id = NONE;
473 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
474 /* Delete configuration here */
475 Init_Server_Struct( &Conf_Server[i] );
477 /* Set time for next connect attempt */
479 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
480 /* The connection has been "long", so we don't
481 * require the next attempt to be delayed. */
482 Conf_Server[i].lasttry =
483 t - Conf_ConnectRetry + RECONNECT_DELAY;
485 Conf_Server[i].lasttry = t;
491 * Set connection information for specified configured server.
494 Conf_SetServer( int ConfServer, CONN_ID Idx )
496 assert( ConfServer > NONE );
497 assert( Idx > NONE );
499 if (Conf_Server[ConfServer].conn_id > NONE &&
500 Conf_Server[ConfServer].conn_id != Idx) {
502 "Connection %d: Server configuration of \"%s\" already in use by connection %d!",
503 Idx, Conf_Server[ConfServer].name,
504 Conf_Server[ConfServer].conn_id);
505 Conn_Close(Idx, NULL, "Server configuration already in use", true);
508 Conf_Server[ConfServer].conn_id = Idx;
513 * Get index of server in configuration structure.
516 Conf_GetServer( CONN_ID Idx )
520 assert( Idx > NONE );
522 for( i = 0; i < MAX_SERVERS; i++ ) {
523 if( Conf_Server[i].conn_id == Idx ) return i;
529 * Enable a server by name and adjust its port number.
531 * @returns true if a server has been enabled and now has a valid port
532 * number and host name for outgoing connections.
535 Conf_EnableServer( const char *Name, UINT16 Port )
539 assert( Name != NULL );
540 for( i = 0; i < MAX_SERVERS; i++ ) {
541 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
542 /* Gotcha! Set port and enable server: */
543 Conf_Server[i].port = Port;
544 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
545 return (Conf_Server[i].port && Conf_Server[i].host[0]);
552 * Enable a server by name.
554 * The server is only usable as outgoing server, if it has set a valid port
555 * number for outgoing connections!
556 * If not, you have to use Conf_EnableServer() function to make it available.
558 * @returns true if a server has been enabled; false otherwise.
561 Conf_EnablePassiveServer(const char *Name)
565 assert( Name != NULL );
566 for (i = 0; i < MAX_SERVERS; i++) {
567 if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
568 && (Conf_Server[i].port > 0)) {
569 /* BINGO! Enable server */
570 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
578 * Disable a server by name.
579 * An already established connection will be disconnected.
581 * @returns true if a server was found and has been disabled.
584 Conf_DisableServer( const char *Name )
588 assert( Name != NULL );
589 for( i = 0; i < MAX_SERVERS; i++ ) {
590 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
591 /* Gotcha! Disable and disconnect server: */
592 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
593 if( Conf_Server[i].conn_id > NONE )
594 Conn_Close(Conf_Server[i].conn_id, NULL,
595 "Server link terminated on operator request",
604 * Add a new remote server to our configuration.
606 * @param Name Name of the new server.
607 * @param Port Port number to connect to or 0 for incoming connections.
608 * @param Host Host name to connect to.
609 * @param MyPwd Password that will be sent to the peer.
610 * @param PeerPwd Password that must be received from the peer.
611 * @returns true if the new server has been added; false otherwise.
614 Conf_AddServer(const char *Name, UINT16 Port, const char *Host,
615 const char *MyPwd, const char *PeerPwd)
619 assert( Name != NULL );
620 assert( Host != NULL );
621 assert( MyPwd != NULL );
622 assert( PeerPwd != NULL );
624 /* Search unused item in server configuration structure */
625 for( i = 0; i < MAX_SERVERS; i++ ) {
626 /* Is this item used? */
627 if( ! Conf_Server[i].name[0] ) break;
629 if( i >= MAX_SERVERS ) return false;
631 Init_Server_Struct( &Conf_Server[i] );
632 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
633 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
634 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
635 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
636 Conf_Server[i].port = Port;
637 Conf_Server[i].flags = CONF_SFLAG_ONCE;
643 * Check if the given nick name is reserved for services on a particular server.
645 * @param ConfServer The server index to check.
646 * @param Nick The nick name to check.
647 * @returns true if the given nick name belongs to an "IRC service".
650 Conf_NickIsService(int ConfServer, const char *Nick)
652 assert (ConfServer >= 0);
653 assert (ConfServer < MAX_SERVERS);
655 return MatchCaseInsensitiveList(Conf_Server[ConfServer].svs_mask,
660 * Check if the given nick name is blocked for "normal client" use.
662 * @param ConfServer The server index or NONE to check all configured servers.
663 * @param Nick The nick name to check.
664 * @returns true if the given nick name belongs to an "IRC service".
667 Conf_NickIsBlocked(const char *Nick)
671 for(i = 0; i < MAX_SERVERS; i++) {
672 if (!Conf_Server[i].name[0])
674 if (Conf_NickIsService(i, Nick))
681 * Initialize configuration settings with their default values.
684 Set_Defaults(bool InitServers)
687 char random[RANDOM_SALT_LEN + 1];
690 strcpy(Conf_ServerName, "");
691 strcpy(Conf_ServerAdmin1, "");
692 strcpy(Conf_ServerAdmin2, "");
693 strcpy(Conf_ServerAdminMail, "");
694 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
695 PACKAGE_NAME, PACKAGE_VERSION);
696 free(Conf_ListenAddress);
697 Conf_ListenAddress = NULL;
698 array_free(&Conf_ListenPorts);
699 array_free(&Conf_Motd);
700 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
701 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
702 strcpy(Conf_ServerPwd, "");
703 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
704 Conf_UID = Conf_GID = 0;
707 Conf_ConnectRetry = 60;
708 Conf_MaxConnections = 0;
709 Conf_MaxConnectionsIP = 5;
711 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
712 Conf_MaxListSize = 100;
713 Conf_PingTimeout = 120;
714 Conf_PongTimeout = 20;
717 Conf_AllowRemoteOper = false;
719 Conf_AuthPing = false;
721 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
722 strcpy(Conf_CloakHost, "");
723 strcpy(Conf_CloakHostModeX, "");
724 strcpy(Conf_CloakHostSalt, ngt_RandomStr(random, RANDOM_SALT_LEN));
725 Conf_CloakUserToNick = false;
726 Conf_ConnectIPv4 = true;
728 Conf_ConnectIPv6 = true;
730 Conf_ConnectIPv6 = false;
738 Conf_MorePrivacy = false;
739 Conf_NoticeAuth = false;
740 Conf_OperCanMode = false;
741 Conf_OperChanPAutoOp = true;
742 Conf_OperServerMode = false;
748 Conf_PAMIsOptional = false;
749 Conf_PredefChannelsOnly = false;
751 Conf_ScrubCTCP = false;
753 Conf_SyslogFacility = LOG_LOCAL5;
755 Conf_SyslogFacility = 0;
759 /* Initialize server configuration structures */
761 for (i = 0; i < MAX_SERVERS;
762 Init_Server_Struct(&Conf_Server[i++]));
767 * Get number of configured listening ports.
769 * @returns The number of ports (IPv4+IPv6) on which the server should listen.
774 size_t cnt = array_bytes(&Conf_ListenPorts);
776 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
782 * Read MOTD ("message of the day") file.
784 * @param filename Name of the file to read.
787 Read_Motd(const char *filename)
792 if (*filename == '\0')
795 fp = fopen(filename, "r");
797 Config_Error(LOG_WARNING, "Can't read MOTD file \"%s\": %s",
798 filename, strerror(errno));
802 array_free(&Conf_Motd);
803 Using_MotdFile = true;
805 while (fgets(line, (int)sizeof line, fp)) {
806 ngt_TrimLastChr( line, '\n');
808 /* add text including \0 */
809 if (!array_catb(&Conf_Motd, line, strlen(line) + 1)) {
810 Log(LOG_WARNING, "Cannot add MOTD text: %s", strerror(errno));
818 * Read ngIRCd configuration file.
820 * Please note that this function uses exit(1) on fatal errors and therefore
821 * can result in ngIRCd terminating!
823 * @param ngircd_starting Flag indicating if ngIRCd is starting or not.
824 * @returns true when the configuration file has been read
825 * successfully; false otherwise.
828 Read_Config(bool TestOnly, bool IsStarting)
830 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
831 const UINT16 defaultport = 6667;
836 /* Open configuration file */
837 fd = fopen( NGIRCd_ConfFile, "r" );
839 /* No configuration file found! */
840 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
841 NGIRCd_ConfFile, strerror( errno ));
844 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
849 Set_Defaults(IsStarting);
852 Config_Error(LOG_INFO,
853 "Reading configuration from \"%s\" ...",
856 /* Clean up server configuration structure: mark all already
857 * configured servers as "once" so that they are deleted
858 * after the next disconnect and delete all unused servers.
859 * And delete all servers which are "duplicates" of servers
860 * that are already marked as "once" (such servers have been
861 * created by the last rehash but are now useless). */
862 for( i = 0; i < MAX_SERVERS; i++ ) {
863 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
865 /* This structure is in use ... */
866 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
867 /* Check for duplicates */
868 for( n = 0; n < MAX_SERVERS; n++ ) {
869 if( n == i ) continue;
871 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
872 Init_Server_Struct( &Conf_Server[n] );
874 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
880 /* Mark server as "once" */
881 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
882 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
887 /* Initialize variables */
889 strcpy( section, "" );
890 Init_Server_Struct( &New_Server );
891 New_Server_Idx = NONE;
895 /* Read configuration file */
897 if( ! fgets( str, LINE_LEN, fd )) break;
901 /* Skip comments and empty lines */
902 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
904 /* Is this the beginning of a new section? */
905 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
906 strlcpy( section, str, sizeof( section ));
907 if (strcasecmp(section, "[GLOBAL]") == 0
908 || strcasecmp(section, "[LIMITS]") == 0
909 || strcasecmp(section, "[OPTIONS]") == 0
911 || strcasecmp(section, "[SSL]") == 0
916 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
917 /* Check if there is already a server to add */
918 if( New_Server.name[0] ) {
919 /* Copy data to "real" server structure */
920 assert( New_Server_Idx > NONE );
921 Conf_Server[New_Server_Idx] = New_Server;
924 /* Re-init structure for new server */
925 Init_Server_Struct( &New_Server );
927 /* Search unused item in server configuration structure */
928 for( i = 0; i < MAX_SERVERS; i++ ) {
929 /* Is this item used? */
930 if( ! Conf_Server[i].name[0] ) break;
932 if( i >= MAX_SERVERS ) {
933 /* Oops, no free item found! */
934 Config_Error( LOG_ERR, "Too many servers configured." );
935 New_Server_Idx = NONE;
937 else New_Server_Idx = i;
941 if (strcasecmp(section, "[CHANNEL]") == 0) {
942 count = array_length(&Conf_Channels,
943 sizeof(struct Conf_Channel));
944 if (!array_alloc(&Conf_Channels,
945 sizeof(struct Conf_Channel),
947 Config_Error(LOG_ERR,
948 "Could not allocate memory for new operator (line %d)",
954 if (strcasecmp(section, "[OPERATOR]") == 0) {
955 count = array_length(&Conf_Opers,
956 sizeof(struct Conf_Oper));
957 if (!array_alloc(&Conf_Opers,
958 sizeof(struct Conf_Oper),
960 Config_Error(LOG_ERR,
961 "Could not allocate memory for new channel (line &d)",
967 Config_Error(LOG_ERR,
968 "%s, line %d: Unknown section \"%s\"!",
969 NGIRCd_ConfFile, line, section);
972 if( section[0] == 0x1 ) continue;
974 /* Split line into variable name and parameters */
975 ptr = strchr( str, '=' );
977 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
981 var = str; ngt_TrimStr( var );
982 arg = ptr + 1; ngt_TrimStr( arg );
984 if(strcasecmp(section, "[GLOBAL]") == 0)
985 Handle_GLOBAL(line, var, arg);
986 else if(strcasecmp(section, "[LIMITS]") == 0)
987 Handle_LIMITS(line, var, arg);
988 else if(strcasecmp(section, "[OPTIONS]") == 0)
989 Handle_OPTIONS(line, var, arg);
991 else if(strcasecmp(section, "[SSL]") == 0)
992 Handle_SSL(line, var, arg);
994 else if(strcasecmp(section, "[OPERATOR]") == 0)
995 Handle_OPERATOR(line, var, arg);
996 else if(strcasecmp(section, "[SERVER]") == 0)
997 Handle_SERVER(line, var, arg);
998 else if(strcasecmp(section, "[CHANNEL]") == 0)
999 Handle_CHANNEL(line, var, arg);
1001 Config_Error(LOG_ERR,
1002 "%s, line %d: Variable \"%s\" outside section!",
1003 NGIRCd_ConfFile, line, var);
1006 /* Close configuration file */
1009 /* Check if there is still a server to add */
1010 if( New_Server.name[0] ) {
1011 /* Copy data to "real" server structure */
1012 assert( New_Server_Idx > NONE );
1013 Conf_Server[New_Server_Idx] = New_Server;
1016 /* not a single listening port? Add default. */
1017 if (no_listenports() &&
1018 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
1020 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
1021 (unsigned int) defaultport, strerror(errno));
1026 if (!Conf_ListenAddress)
1027 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
1029 if (!Conf_ListenAddress) {
1030 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
1034 /* No MOTD phrase configured? (re)try motd file. */
1035 if (array_bytes(&Conf_Motd) == 0)
1036 Read_Motd(Conf_MotdFile);
1039 /* Make sure that all SSL-related files are readable */
1040 CheckFileReadable("CertFile", Conf_SSLOptions.CertFile);
1041 CheckFileReadable("DHFile", Conf_SSLOptions.DHFile);
1042 CheckFileReadable("KeyFile", Conf_SSLOptions.KeyFile);
1049 * Check whether a string argument is "true" or "false".
1051 * @param Arg Input string.
1052 * @returns true if the input string has been parsed as "yes", "true"
1053 * (case insensitive) or a non-zero integer value.
1056 Check_ArgIsTrue(const char *Arg)
1058 if (strcasecmp(Arg, "yes") == 0)
1060 if (strcasecmp(Arg, "true") == 0)
1069 * Handle setting of "MaxNickLength".
1071 * @param Line Line number in configuration file.
1072 * @raram Arg Input string.
1073 * @returns New configured maximum nick name length.
1076 Handle_MaxNickLength(int Line, const char *Arg)
1080 new = (unsigned) atoi(Arg) + 1;
1081 if (new > CLIENT_NICK_LEN) {
1082 Config_Error(LOG_WARNING,
1083 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
1084 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
1085 return CLIENT_NICK_LEN;
1088 Config_Error(LOG_WARNING,
1089 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
1090 NGIRCd_ConfFile, Line);
1097 * Output a warning messages if IDENT is configured but not compiled in.
1100 WarnIdent(int UNUSED Line)
1104 /* user has enabled ident lookups explicitly, but ... */
1105 Config_Error(LOG_WARNING,
1106 "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
1107 NGIRCd_ConfFile, Line);
1113 * Output a warning messages if IPv6 is configured but not compiled in.
1116 WarnIPv6(int UNUSED Line)
1119 if (Conf_ConnectIPv6) {
1120 /* user has enabled IPv6 explicitly, but ... */
1121 Config_Error(LOG_WARNING,
1122 "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
1123 NGIRCd_ConfFile, Line);
1129 * Output a warning messages if PAM is configured but not compiled in.
1132 WarnPAM(int UNUSED Line)
1136 Config_Error(LOG_WARNING,
1137 "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
1138 NGIRCd_ConfFile, Line);
1144 * Handle legacy "NoXXX" options in [GLOBAL] section.
1146 * TODO: This function and support for "NoXXX" could be removed starting
1147 * with ngIRCd release 19 (one release after marking it "deprecated").
1149 * @param Var Variable name.
1150 * @param Arg Argument string.
1151 * @returns true if a NoXXX option has been processed; false otherwise.
1154 CheckLegacyNoOption(const char *Var, const char *Arg)
1156 if(strcasecmp(Var, "NoDNS") == 0) {
1157 Conf_DNS = !Check_ArgIsTrue( Arg );
1160 if (strcasecmp(Var, "NoIdent") == 0) {
1161 Conf_Ident = !Check_ArgIsTrue(Arg);
1164 if(strcasecmp(Var, "NoPAM") == 0) {
1165 Conf_PAM = !Check_ArgIsTrue(Arg);
1172 * Handle deprecated legacy options in [GLOBAL] section.
1174 * TODO: This function and support for these options in the [Global] section
1175 * could be removed starting with ngIRCd release 19 (one release after
1176 * marking it "deprecated").
1178 * @param Var Variable name.
1179 * @param Arg Argument string.
1180 * @returns true if a legacy option has been processed; false otherwise.
1183 CheckLegacyGlobalOption(int Line, char *Var, char *Arg)
1185 if (strcasecmp(Var, "AllowRemoteOper") == 0
1186 || strcasecmp(Var, "ChrootDir") == 0
1187 || strcasecmp(Var, "ConnectIPv4") == 0
1188 || strcasecmp(Var, "ConnectIPv6") == 0
1189 || strcasecmp(Var, "OperCanUseMode") == 0
1190 || strcasecmp(Var, "OperChanPAutoOp") == 0
1191 || strcasecmp(Var, "OperServerMode") == 0
1192 || strcasecmp(Var, "PredefChannelsOnly") == 0
1193 || strcasecmp(Var, "SyslogFacility") == 0
1194 || strcasecmp(Var, "WebircPassword") == 0) {
1195 Handle_OPTIONS(Line, Var, Arg);
1198 if (strcasecmp(Var, "ConnectRetry") == 0
1199 || strcasecmp(Var, "MaxConnections") == 0
1200 || strcasecmp(Var, "MaxConnectionsIP") == 0
1201 || strcasecmp(Var, "MaxJoins") == 0
1202 || strcasecmp(Var, "MaxNickLength") == 0
1203 || strcasecmp(Var, "PingTimeout") == 0
1204 || strcasecmp(Var, "PongTimeout") == 0) {
1205 Handle_LIMITS(Line, Var, Arg);
1209 if (strcasecmp(Var, "SSLCertFile") == 0
1210 || strcasecmp(Var, "SSLDHFile") == 0
1211 || strcasecmp(Var, "SSLKeyFile") == 0
1212 || strcasecmp(Var, "SSLKeyFilePassword") == 0
1213 || strcasecmp(Var, "SSLPorts") == 0) {
1214 Handle_SSL(Line, Var + 3, Arg);
1223 * Strip "no" prefix of a string.
1225 * TODO: This function and support for "NoXXX" should be removed starting
1226 * with ngIRCd release 19! (One release after marking it "deprecated").
1228 * @param str Pointer to input string starting with "no".
1229 * @returns New pointer to string without "no" prefix.
1232 NoNo(const char *str)
1234 assert(strncasecmp("no", str, 2) == 0 && str[2]);
1239 * Invert "boolean" string.
1241 * TODO: This function and support for "NoXXX" should be removed starting
1242 * with ngIRCd release 19! (One release after marking it "deprecated").
1244 * @param arg "Boolean" input string.
1245 * @returns Pointer to inverted "boolean string".
1248 InvertArg(const char *arg)
1250 return yesno_to_str(!Check_ArgIsTrue(arg));
1254 * Handle variable in [Global] configuration section.
1256 * @param Line Line numer in configuration file.
1257 * @param Var Variable name.
1258 * @param Arg Variable argument.
1261 Handle_GLOBAL( int Line, char *Var, char *Arg )
1266 const char *section;
1269 assert(Var != NULL);
1270 assert(Arg != NULL);
1272 if (strcasecmp(Var, "Name") == 0) {
1273 len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
1274 if (len >= sizeof(Conf_ServerName))
1275 Config_Error_TooLong(Line, Var);
1278 if (strcasecmp(Var, "AdminInfo1") == 0) {
1279 len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
1280 if (len >= sizeof(Conf_ServerAdmin1))
1281 Config_Error_TooLong(Line, Var);
1284 if (strcasecmp(Var, "AdminInfo2") == 0) {
1285 len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
1286 if (len >= sizeof(Conf_ServerAdmin2))
1287 Config_Error_TooLong(Line, Var);
1290 if (strcasecmp(Var, "AdminEMail") == 0) {
1291 len = strlcpy(Conf_ServerAdminMail, Arg,
1292 sizeof(Conf_ServerAdminMail));
1293 if (len >= sizeof(Conf_ServerAdminMail))
1294 Config_Error_TooLong(Line, Var);
1297 if (strcasecmp(Var, "Info") == 0) {
1298 len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
1299 if (len >= sizeof(Conf_ServerInfo))
1300 Config_Error_TooLong(Line, Var);
1303 if (strcasecmp(Var, "Listen") == 0) {
1304 if (Conf_ListenAddress) {
1305 Config_Error(LOG_ERR,
1306 "Multiple Listen= options, ignoring: %s",
1310 Conf_ListenAddress = strdup_warn(Arg);
1311 /* If allocation fails, we're in trouble: we cannot ignore the
1312 * error -- otherwise ngircd would listen on all interfaces. */
1313 if (!Conf_ListenAddress) {
1314 Config_Error(LOG_ALERT,
1315 "%s exiting due to fatal errors!",
1321 if (strcasecmp(Var, "MotdFile") == 0) {
1322 len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
1323 if (len >= sizeof(Conf_MotdFile))
1324 Config_Error_TooLong(Line, Var);
1327 if (strcasecmp(Var, "MotdPhrase") == 0) {
1331 if (len >= LINE_LEN) {
1332 Config_Error_TooLong(Line, Var);
1335 if (!array_copyb(&Conf_Motd, Arg, len + 1))
1336 Config_Error(LOG_WARNING,
1337 "%s, line %d: Could not append MotdPhrase: %s",
1338 NGIRCd_ConfFile, Line, strerror(errno));
1339 Using_MotdFile = false;
1342 if(strcasecmp(Var, "Password") == 0) {
1343 len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
1344 if (len >= sizeof(Conf_ServerPwd))
1345 Config_Error_TooLong(Line, Var);
1348 if (strcasecmp(Var, "PidFile") == 0) {
1349 len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
1350 if (len >= sizeof(Conf_PidFile))
1351 Config_Error_TooLong(Line, Var);
1354 if (strcasecmp(Var, "Ports") == 0) {
1355 ports_parse(&Conf_ListenPorts, Line, Arg);
1358 if (strcasecmp(Var, "ServerGID") == 0) {
1359 grp = getgrnam(Arg);
1361 Conf_GID = grp->gr_gid;
1363 Conf_GID = (unsigned int)atoi(Arg);
1364 if (!Conf_GID && strcmp(Arg, "0"))
1365 Config_Error(LOG_WARNING,
1366 "%s, line %d: Value of \"%s\" is not a valid group name or ID!",
1367 NGIRCd_ConfFile, Line, Var);
1371 if (strcasecmp(Var, "ServerUID") == 0) {
1372 pwd = getpwnam(Arg);
1374 Conf_UID = pwd->pw_uid;
1376 Conf_UID = (unsigned int)atoi(Arg);
1377 if (!Conf_UID && strcmp(Arg, "0"))
1378 Config_Error(LOG_WARNING,
1379 "%s, line %d: Value of \"%s\" is not a valid user name or ID!",
1380 NGIRCd_ConfFile, Line, Var);
1385 if (CheckLegacyNoOption(Var, Arg)) {
1386 /* TODO: This function and support for "NoXXX" could be
1387 * be removed starting with ngIRCd release 19 (one release
1388 * after marking it "deprecated"). */
1389 Config_Error(LOG_WARNING,
1390 "%s, line %d (section \"Global\"): \"No\"-Prefix is deprecated, use \"%s = %s\" in [Options] section!",
1391 NGIRCd_ConfFile, Line, NoNo(Var), InvertArg(Arg));
1392 if (strcasecmp(Var, "NoIdent") == 0)
1394 else if (strcasecmp(Var, "NoPam") == 0)
1398 if ((section = CheckLegacyGlobalOption(Line, Var, Arg))) {
1399 /** TODO: This function and support for these options in the
1400 * [Global] section could be removed starting with ngIRCd
1401 * release 19 (one release after marking it "deprecated"). */
1402 if (strncasecmp(Var, "SSL", 3) == 0) {
1403 Config_Error(LOG_WARNING,
1404 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s and rename to \"%s\"!",
1405 NGIRCd_ConfFile, Line, Var, section,
1408 Config_Error(LOG_WARNING,
1409 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s!",
1410 NGIRCd_ConfFile, Line, Var, section);
1415 Config_Error_Section(Line, Var, "Global");
1419 * Handle variable in [Limits] configuration section.
1421 * @param Line Line numer in configuration file.
1422 * @param Var Variable name.
1423 * @param Arg Variable argument.
1426 Handle_LIMITS(int Line, char *Var, char *Arg)
1429 assert(Var != NULL);
1430 assert(Arg != NULL);
1432 if (strcasecmp(Var, "ConnectRetry") == 0) {
1433 Conf_ConnectRetry = atoi(Arg);
1434 if (Conf_ConnectRetry < 5) {
1435 Config_Error(LOG_WARNING,
1436 "%s, line %d: Value of \"ConnectRetry\" too low!",
1437 NGIRCd_ConfFile, Line);
1438 Conf_ConnectRetry = 5;
1442 if (strcasecmp(Var, "MaxConnections") == 0) {
1443 Conf_MaxConnections = atoi(Arg);
1444 if (!Conf_MaxConnections && strcmp(Arg, "0"))
1445 Config_Error_NaN(Line, Var);
1448 if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
1449 Conf_MaxConnectionsIP = atoi(Arg);
1450 if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
1451 Config_Error_NaN(Line, Var);
1454 if (strcasecmp(Var, "MaxJoins") == 0) {
1455 Conf_MaxJoins = atoi(Arg);
1456 if (!Conf_MaxJoins && strcmp(Arg, "0"))
1457 Config_Error_NaN(Line, Var);
1460 if (strcasecmp(Var, "MaxNickLength") == 0) {
1461 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1464 if (strcasecmp(Var, "MaxListSize") == 0) {
1465 Conf_MaxListSize = atoi(Arg);
1466 if (!Conf_MaxListSize && strcmp(Arg, "0"))
1467 Config_Error_NaN(Line, Var);
1470 if (strcasecmp(Var, "PingTimeout") == 0) {
1471 Conf_PingTimeout = atoi(Arg);
1472 if (Conf_PingTimeout < 5) {
1473 Config_Error(LOG_WARNING,
1474 "%s, line %d: Value of \"PingTimeout\" too low!",
1475 NGIRCd_ConfFile, Line);
1476 Conf_PingTimeout = 5;
1480 if (strcasecmp(Var, "PongTimeout") == 0) {
1481 Conf_PongTimeout = atoi(Arg);
1482 if (Conf_PongTimeout < 5) {
1483 Config_Error(LOG_WARNING,
1484 "%s, line %d: Value of \"PongTimeout\" too low!",
1485 NGIRCd_ConfFile, Line);
1486 Conf_PongTimeout = 5;
1491 Config_Error_Section(Line, Var, "Limits");
1495 * Handle variable in [Options] configuration section.
1497 * @param Line Line numer in configuration file.
1498 * @param Var Variable name.
1499 * @param Arg Variable argument.
1502 Handle_OPTIONS(int Line, char *Var, char *Arg)
1507 assert(Var != NULL);
1508 assert(Arg != NULL);
1510 if (strcasecmp(Var, "AllowRemoteOper") == 0) {
1511 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1514 if (strcasecmp(Var, "ChrootDir") == 0) {
1515 len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
1516 if (len >= sizeof(Conf_Chroot))
1517 Config_Error_TooLong(Line, Var);
1520 if (strcasecmp(Var, "CloakHost") == 0) {
1521 len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
1522 if (len >= sizeof(Conf_CloakHost))
1523 Config_Error_TooLong(Line, Var);
1526 if (strcasecmp(Var, "CloakHostModeX") == 0) {
1527 len = strlcpy(Conf_CloakHostModeX, Arg, sizeof(Conf_CloakHostModeX));
1528 if (len >= sizeof(Conf_CloakHostModeX))
1529 Config_Error_TooLong(Line, Var);
1532 if (strcasecmp(Var, "CloakHostSalt") == 0) {
1533 len = strlcpy(Conf_CloakHostSalt, Arg, sizeof(Conf_CloakHostSalt));
1534 if (len >= sizeof(Conf_CloakHostSalt))
1535 Config_Error_TooLong(Line, Var);
1538 if (strcasecmp(Var, "CloakUserToNick") == 0) {
1539 Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
1542 if (strcasecmp(Var, "ConnectIPv6") == 0) {
1543 Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
1547 if (strcasecmp(Var, "ConnectIPv4") == 0) {
1548 Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
1551 if (strcasecmp(Var, "DNS") == 0) {
1552 Conf_DNS = Check_ArgIsTrue(Arg);
1555 if (strcasecmp(Var, "Ident") == 0) {
1556 Conf_Ident = Check_ArgIsTrue(Arg);
1560 if (strcasecmp(Var, "MorePrivacy") == 0) {
1561 Conf_MorePrivacy = Check_ArgIsTrue(Arg);
1564 if (strcasecmp(Var, "NoticeAuth") == 0) {
1565 Conf_NoticeAuth = Check_ArgIsTrue(Arg);
1568 if (strcasecmp(Var, "OperCanUseMode") == 0) {
1569 Conf_OperCanMode = Check_ArgIsTrue(Arg);
1572 if (strcasecmp(Var, "OperChanPAutoOp") == 0) {
1573 Conf_OperChanPAutoOp = Check_ArgIsTrue(Arg);
1576 if (strcasecmp(Var, "OperServerMode") == 0) {
1577 Conf_OperServerMode = Check_ArgIsTrue(Arg);
1580 if (strcasecmp(Var, "PAM") == 0) {
1581 Conf_PAM = Check_ArgIsTrue(Arg);
1585 if (strcasecmp(Var, "PAMIsOptional") == 0 ) {
1586 Conf_PAMIsOptional = Check_ArgIsTrue(Arg);
1589 if (strcasecmp(Var, "PredefChannelsOnly") == 0) {
1590 Conf_PredefChannelsOnly = Check_ArgIsTrue(Arg);
1594 if (strcasecmp(Var, "RequireAuthPing") == 0) {
1595 Conf_AuthPing = Check_ArgIsTrue(Arg);
1599 if (strcasecmp(Var, "ScrubCTCP") == 0) {
1600 Conf_ScrubCTCP = Check_ArgIsTrue(Arg);
1604 if (strcasecmp(Var, "SyslogFacility") == 0) {
1605 Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
1606 Conf_SyslogFacility);
1610 if (strcasecmp(Var, "WebircPassword") == 0) {
1611 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
1612 if (len >= sizeof(Conf_WebircPwd))
1613 Config_Error_TooLong(Line, Var);
1617 Config_Error_Section(Line, Var, "Options");
1623 * Handle variable in [SSL] configuration section.
1625 * @param Line Line numer in configuration file.
1626 * @param Var Variable name.
1627 * @param Arg Variable argument.
1630 Handle_SSL(int Line, char *Var, char *Arg)
1633 assert(Var != NULL);
1634 assert(Arg != NULL);
1636 if (strcasecmp(Var, "CertFile") == 0) {
1637 assert(Conf_SSLOptions.CertFile == NULL);
1638 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1641 if (strcasecmp(Var, "DHFile") == 0) {
1642 assert(Conf_SSLOptions.DHFile == NULL);
1643 Conf_SSLOptions.DHFile = strdup_warn(Arg);
1646 if (strcasecmp(Var, "KeyFile") == 0) {
1647 assert(Conf_SSLOptions.KeyFile == NULL);
1648 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1651 if (strcasecmp(Var, "KeyFilePassword") == 0) {
1652 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1653 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1654 Config_Error(LOG_ERR,
1655 "%s, line %d (section \"SSL\"): Could not copy %s: %s!",
1656 NGIRCd_ConfFile, Line, Var,
1660 if (strcasecmp(Var, "Ports") == 0) {
1661 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1665 Config_Error_Section(Line, Var, "SSL");
1671 * Handle variable in [Operator] configuration section.
1673 * @param Line Line numer in configuration file.
1674 * @param Var Variable name.
1675 * @param Arg Variable argument.
1678 Handle_OPERATOR( int Line, char *Var, char *Arg )
1681 struct Conf_Oper *op;
1684 assert( Var != NULL );
1685 assert( Arg != NULL );
1687 op = array_get(&Conf_Opers, sizeof(*op),
1688 array_length(&Conf_Opers, sizeof(*op)) - 1);
1692 if (strcasecmp(Var, "Name") == 0) {
1693 /* Name of IRC operator */
1694 len = strlcpy(op->name, Arg, sizeof(op->name));
1695 if (len >= sizeof(op->name))
1696 Config_Error_TooLong(Line, Var);
1699 if (strcasecmp(Var, "Password") == 0) {
1700 /* Password of IRC operator */
1701 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1702 if (len >= sizeof(op->pwd))
1703 Config_Error_TooLong(Line, Var);
1706 if (strcasecmp(Var, "Mask") == 0) {
1708 return; /* Hostname already configured */
1709 op->mask = strdup_warn( Arg );
1713 Config_Error_Section(Line, Var, "Operator");
1717 * Handle variable in [Server] configuration section.
1719 * @param Line Line numer in configuration file.
1720 * @param Var Variable name.
1721 * @param Arg Variable argument.
1724 Handle_SERVER( int Line, char *Var, char *Arg )
1730 assert( Var != NULL );
1731 assert( Arg != NULL );
1733 /* Ignore server block if no space is left in server configuration structure */
1734 if( New_Server_Idx <= NONE ) return;
1736 if( strcasecmp( Var, "Host" ) == 0 ) {
1737 /* Hostname of the server */
1738 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1739 if (len >= sizeof( New_Server.host ))
1740 Config_Error_TooLong ( Line, Var );
1743 if( strcasecmp( Var, "Name" ) == 0 ) {
1744 /* Name of the server ("Nick"/"ID") */
1745 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1746 if (len >= sizeof( New_Server.name ))
1747 Config_Error_TooLong( Line, Var );
1750 if (strcasecmp(Var, "Bind") == 0) {
1751 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1754 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1755 NGIRCd_ConfFile, Line, Arg);
1758 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1759 /* Password of this server which is sent to the peer */
1761 Config_Error(LOG_ERR,
1762 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1763 NGIRCd_ConfFile, Line);
1765 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1766 if (len >= sizeof( New_Server.pwd_in ))
1767 Config_Error_TooLong( Line, Var );
1770 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1771 /* Passwort of the peer which must be received */
1772 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1773 if (len >= sizeof( New_Server.pwd_out ))
1774 Config_Error_TooLong( Line, Var );
1777 if( strcasecmp( Var, "Port" ) == 0 ) {
1778 /* Port to which this server should connect */
1780 if (port >= 0 && port < 0xFFFF)
1781 New_Server.port = (UINT16)port;
1783 Config_Error(LOG_ERR,
1784 "%s, line %d (section \"Server\"): Illegal port number %ld!",
1785 NGIRCd_ConfFile, Line, port );
1789 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1790 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1794 if( strcasecmp( Var, "Group" ) == 0 ) {
1796 New_Server.group = atoi( Arg );
1797 if (!New_Server.group && strcmp(Arg, "0"))
1798 Config_Error_NaN(Line, Var);
1801 if( strcasecmp( Var, "Passive" ) == 0 ) {
1802 if (Check_ArgIsTrue(Arg))
1803 New_Server.flags |= CONF_SFLAG_DISABLED;
1806 if (strcasecmp(Var, "ServiceMask") == 0) {
1807 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1808 sizeof(New_Server.svs_mask));
1809 if (len >= sizeof(New_Server.svs_mask))
1810 Config_Error_TooLong(Line, Var);
1814 Config_Error_Section(Line, Var, "Server");
1818 * Copy channel name into channel structure.
1820 * If the channel name is not valid because of a missing prefix ('#', '&'),
1821 * a default prefix of '#' will be added.
1823 * @param new_chan New already allocated channel structure.
1824 * @param name Name of the new channel.
1825 * @returns true on success, false otherwise.
1828 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1830 size_t size = sizeof(new_chan->name);
1831 char *dest = new_chan->name;
1833 if (!Channel_IsValidName(name)) {
1835 * maybe user forgot to add a '#'.
1836 * This is only here for user convenience.
1842 return size > strlcpy(dest, name, size);
1846 * Handle variable in [Channel] configuration section.
1848 * @param Line Line numer in configuration file.
1849 * @param Var Variable name.
1850 * @param Arg Variable argument.
1853 Handle_CHANNEL(int Line, char *Var, char *Arg)
1856 struct Conf_Channel *chan;
1859 assert( Var != NULL );
1860 assert( Arg != NULL );
1862 chan = array_get(&Conf_Channels, sizeof(*chan),
1863 array_length(&Conf_Channels, sizeof(*chan)) - 1);
1867 if (strcasecmp(Var, "Name") == 0) {
1868 if (!Handle_Channelname(chan, Arg))
1869 Config_Error_TooLong(Line, Var);
1872 if (strcasecmp(Var, "Modes") == 0) {
1874 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1875 if (len >= sizeof(chan->modes))
1876 Config_Error_TooLong( Line, Var );
1879 if( strcasecmp( Var, "Topic" ) == 0 ) {
1881 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1882 if (len >= sizeof(chan->topic))
1883 Config_Error_TooLong( Line, Var );
1886 if( strcasecmp( Var, "Key" ) == 0 ) {
1887 /* Initial Channel Key (mode k) */
1888 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1889 if (len >= sizeof(chan->key))
1890 Config_Error_TooLong(Line, Var);
1893 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1894 /* maximum user limit, mode l */
1895 chan->maxusers = (unsigned long) atol(Arg);
1896 if (!chan->maxusers && strcmp(Arg, "0"))
1897 Config_Error_NaN(Line, Var);
1900 if (strcasecmp(Var, "KeyFile") == 0) {
1902 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1903 if (len >= sizeof(chan->keyfile))
1904 Config_Error_TooLong(Line, Var);
1908 Config_Error_Section(Line, Var, "Channel");
1912 * Validate server configuration.
1914 * Please note that this function uses exit(1) on fatal errors and therefore
1915 * can result in ngIRCd terminating!
1917 * @param Configtest true if the daemon has been called with "--configtest".
1918 * @param Rehash true if re-reading configuration on runtime.
1919 * @returns true if configuration is valid.
1922 Validate_Config(bool Configtest, bool Rehash)
1924 /* Validate configuration settings. */
1927 int i, servers, servers_once;
1929 bool config_valid = true;
1932 /* Emit a warning when the config file is not a full path name */
1933 if (NGIRCd_ConfFile[0] && NGIRCd_ConfFile[0] != '/') {
1934 Config_Error(LOG_WARNING,
1935 "Not specifying a full path name to \"%s\" can cause problems when rehashing the server!",
1939 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1940 ptr = Conf_ServerName;
1942 if (*ptr >= 'a' && *ptr <= 'z') continue;
1943 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1944 if (*ptr >= '0' && *ptr <= '9') continue;
1945 if (ptr > Conf_ServerName) {
1946 if (*ptr == '.' || *ptr == '-')
1949 Conf_ServerName[0] = '\0';
1953 if (!Conf_ServerName[0]) {
1954 /* No server name configured! */
1955 config_valid = false;
1956 Config_Error(LOG_ALERT,
1957 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1959 if (!Configtest && !Rehash) {
1960 Config_Error(LOG_ALERT,
1961 "%s exiting due to fatal errors!",
1967 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1968 /* No dot in server name! */
1969 config_valid = false;
1970 Config_Error(LOG_ALERT,
1971 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1974 Config_Error(LOG_ALERT,
1975 "%s exiting due to fatal errors!",
1982 if (!Conf_ServerAdminMail[0]) {
1983 /* No administrative contact configured! */
1984 config_valid = false;
1985 Config_Error(LOG_ALERT,
1986 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1989 Config_Error(LOG_ALERT,
1990 "%s exiting due to fatal errors!",
1997 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1998 && !Conf_ServerAdminMail[0]) {
1999 /* No administrative information configured! */
2000 Config_Error(LOG_WARNING,
2001 "No administrative information configured but required by RFC!");
2005 if (Conf_ServerPwd[0])
2006 Config_Error(LOG_ERR,
2007 "This server uses PAM, \"Password\" in [Global] section will be ignored!");
2011 servers = servers_once = 0;
2012 for (i = 0; i < MAX_SERVERS; i++) {
2013 if (Conf_Server[i].name[0]) {
2015 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
2020 "Configuration: Operators=%ld, Servers=%d[%d], Channels=%ld",
2021 array_length(&Conf_Opers, sizeof(struct Conf_Oper)),
2022 servers, servers_once,
2023 array_length(&Conf_Channels, sizeof(struct Conf_Channel)));
2026 return config_valid;
2030 * Output "line too long" warning.
2032 * @param Line Line number in configuration file.
2033 * @param Item Affected variable name.
2036 Config_Error_TooLong ( const int Line, const char *Item )
2038 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
2042 * Output "unknown variable" warning.
2044 * @param Line Line number in configuration file.
2045 * @param Item Affected variable name.
2046 * @param Section Section name.
2049 Config_Error_Section(const int Line, const char *Item, const char *Section)
2051 Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
2052 NGIRCd_ConfFile, Line, Section, Item);
2056 * Output "not a number" warning.
2058 * @param Line Line number in configuration file.
2059 * @param Item Affected variable name.
2062 Config_Error_NaN( const int Line, const char *Item )
2064 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
2065 NGIRCd_ConfFile, Line, Item );
2069 * Output configuration error to console and/or logfile.
2071 * On runtime, the normal log functions of the daemon are used. But when
2072 * testing the configuration ("--configtest"), all messages go directly
2075 * @param Level Severity level of the message.
2076 * @param Format Format string; see printf() function.
2079 static void Config_Error( const int Level, const char *Format, ... )
2081 static void Config_Error( Level, Format, va_alist )
2087 char msg[MAX_LOG_MSG_LEN];
2090 assert( Format != NULL );
2093 va_start( ap, Format );
2097 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
2101 if (Level <= LOG_WARNING)
2102 printf(" - %s\n", msg);
2106 Log(Level, "%s", msg);
2112 * Dump internal state of the "configuration module".
2115 Conf_DebugDump(void)
2119 Log(LOG_DEBUG, "Configured servers:");
2120 for (i = 0; i < MAX_SERVERS; i++) {
2121 if (! Conf_Server[i].name[0])
2124 " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
2125 Conf_Server[i].name, Conf_Server[i].host,
2126 Conf_Server[i].port, Conf_Server[i].lasttry,
2127 Conf_Server[i].group, Conf_Server[i].flags,
2128 Conf_Server[i].conn_id);
2135 * Initialize server configuration structur to default values.
2137 * @param Server Pointer to server structure to initialize.
2140 Init_Server_Struct( CONF_SERVER *Server )
2142 assert( Server != NULL );
2144 memset( Server, 0, sizeof (CONF_SERVER) );
2146 Server->group = NONE;
2147 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
2149 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
2151 Proc_InitStruct(&Server->res_stat);
2152 Server->conn_id = NONE;
2153 memset(&Server->bind_addr, 0, sizeof(Server->bind_addr));