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 "Trying to update connection index for already registered server \"%s\": %d/%d - ignored.",
503 Conf_Server[ConfServer].name,
504 Conf_Server[ConfServer].conn_id, Idx);
507 Conf_Server[ConfServer].conn_id = Idx;
511 * Get index of server in configuration structure.
514 Conf_GetServer( CONN_ID Idx )
518 assert( Idx > NONE );
520 for( i = 0; i < MAX_SERVERS; i++ ) {
521 if( Conf_Server[i].conn_id == Idx ) return i;
527 * Enable a server by name and adjust its port number.
529 * @returns true if a server has been enabled and now has a valid port
530 * number and host name for outgoing connections.
533 Conf_EnableServer( const char *Name, UINT16 Port )
537 assert( Name != NULL );
538 for( i = 0; i < MAX_SERVERS; i++ ) {
539 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
540 /* Gotcha! Set port and enable server: */
541 Conf_Server[i].port = Port;
542 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
543 return (Conf_Server[i].port && Conf_Server[i].host[0]);
550 * Enable a server by name.
552 * The server is only usable as outgoing server, if it has set a valid port
553 * number for outgoing connections!
554 * If not, you have to use Conf_EnableServer() function to make it available.
556 * @returns true if a server has been enabled; false otherwise.
559 Conf_EnablePassiveServer(const char *Name)
563 assert( Name != NULL );
564 for (i = 0; i < MAX_SERVERS; i++) {
565 if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
566 && (Conf_Server[i].port > 0)) {
567 /* BINGO! Enable server */
568 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
576 * Disable a server by name.
577 * An already established connection will be disconnected.
579 * @returns true if a server was found and has been disabled.
582 Conf_DisableServer( const char *Name )
586 assert( Name != NULL );
587 for( i = 0; i < MAX_SERVERS; i++ ) {
588 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
589 /* Gotcha! Disable and disconnect server: */
590 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
591 if( Conf_Server[i].conn_id > NONE )
592 Conn_Close(Conf_Server[i].conn_id, NULL,
593 "Server link terminated on operator request",
602 * Add a new remote server to our configuration.
604 * @param Name Name of the new server.
605 * @param Port Port number to connect to or 0 for incoming connections.
606 * @param Host Host name to connect to.
607 * @param MyPwd Password that will be sent to the peer.
608 * @param PeerPwd Password that must be received from the peer.
609 * @returns true if the new server has been added; false otherwise.
612 Conf_AddServer(const char *Name, UINT16 Port, const char *Host,
613 const char *MyPwd, const char *PeerPwd)
617 assert( Name != NULL );
618 assert( Host != NULL );
619 assert( MyPwd != NULL );
620 assert( PeerPwd != NULL );
622 /* Search unused item in server configuration structure */
623 for( i = 0; i < MAX_SERVERS; i++ ) {
624 /* Is this item used? */
625 if( ! Conf_Server[i].name[0] ) break;
627 if( i >= MAX_SERVERS ) return false;
629 Init_Server_Struct( &Conf_Server[i] );
630 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
631 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
632 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
633 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
634 Conf_Server[i].port = Port;
635 Conf_Server[i].flags = CONF_SFLAG_ONCE;
641 * Check if the given nick name is reserved for services on a particular server.
643 * @param ConfServer The server index to check.
644 * @param Nick The nick name to check.
645 * @returns true if the given nick name belongs to an "IRC service".
648 Conf_NickIsService(int ConfServer, const char *Nick)
650 assert (ConfServer >= 0);
651 assert (ConfServer < MAX_SERVERS);
653 return MatchCaseInsensitiveList(Conf_Server[ConfServer].svs_mask,
658 * Check if the given nick name is blocked for "normal client" use.
660 * @param ConfServer The server index or NONE to check all configured servers.
661 * @param Nick The nick name to check.
662 * @returns true if the given nick name belongs to an "IRC service".
665 Conf_NickIsBlocked(const char *Nick)
669 for(i = 0; i < MAX_SERVERS; i++) {
670 if (!Conf_Server[i].name[0])
672 if (Conf_NickIsService(i, Nick))
679 * Initialize configuration settings with their default values.
682 Set_Defaults(bool InitServers)
685 char random[RANDOM_SALT_LEN + 1];
688 strcpy(Conf_ServerName, "");
689 strcpy(Conf_ServerAdmin1, "");
690 strcpy(Conf_ServerAdmin2, "");
691 strcpy(Conf_ServerAdminMail, "");
692 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
693 PACKAGE_NAME, PACKAGE_VERSION);
694 free(Conf_ListenAddress);
695 Conf_ListenAddress = NULL;
696 array_free(&Conf_ListenPorts);
697 array_free(&Conf_Motd);
698 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
699 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
700 strcpy(Conf_ServerPwd, "");
701 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
702 Conf_UID = Conf_GID = 0;
705 Conf_ConnectRetry = 60;
706 Conf_MaxConnections = 0;
707 Conf_MaxConnectionsIP = 5;
709 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
710 Conf_MaxListSize = 100;
711 Conf_PingTimeout = 120;
712 Conf_PongTimeout = 20;
715 Conf_AllowRemoteOper = false;
717 Conf_AuthPing = false;
719 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
720 strcpy(Conf_CloakHost, "");
721 strcpy(Conf_CloakHostModeX, "");
722 strcpy(Conf_CloakHostSalt, ngt_RandomStr(random, RANDOM_SALT_LEN));
723 Conf_CloakUserToNick = false;
724 Conf_ConnectIPv4 = true;
726 Conf_ConnectIPv6 = true;
728 Conf_ConnectIPv6 = false;
736 Conf_MorePrivacy = false;
737 Conf_NoticeAuth = false;
738 Conf_OperCanMode = false;
739 Conf_OperChanPAutoOp = true;
740 Conf_OperServerMode = false;
746 Conf_PAMIsOptional = false;
747 Conf_PredefChannelsOnly = false;
749 Conf_ScrubCTCP = false;
751 Conf_SyslogFacility = LOG_LOCAL5;
753 Conf_SyslogFacility = 0;
757 /* Initialize server configuration structures */
759 for (i = 0; i < MAX_SERVERS;
760 Init_Server_Struct(&Conf_Server[i++]));
765 * Get number of configured listening ports.
767 * @returns The number of ports (IPv4+IPv6) on which the server should listen.
772 size_t cnt = array_bytes(&Conf_ListenPorts);
774 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
780 * Read MOTD ("message of the day") file.
782 * @param filename Name of the file to read.
785 Read_Motd(const char *filename)
790 if (*filename == '\0')
793 fp = fopen(filename, "r");
795 Config_Error(LOG_WARNING, "Can't read MOTD file \"%s\": %s",
796 filename, strerror(errno));
800 array_free(&Conf_Motd);
801 Using_MotdFile = true;
803 while (fgets(line, (int)sizeof line, fp)) {
804 ngt_TrimLastChr( line, '\n');
806 /* add text including \0 */
807 if (!array_catb(&Conf_Motd, line, strlen(line) + 1)) {
808 Log(LOG_WARNING, "Cannot add MOTD text: %s", strerror(errno));
816 * Read ngIRCd configuration file.
818 * Please note that this function uses exit(1) on fatal errors and therefore
819 * can result in ngIRCd terminating!
821 * @param ngircd_starting Flag indicating if ngIRCd is starting or not.
822 * @returns true when the configuration file has been read
823 * successfully; false otherwise.
826 Read_Config(bool TestOnly, bool IsStarting)
828 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
829 const UINT16 defaultport = 6667;
834 /* Open configuration file */
835 fd = fopen( NGIRCd_ConfFile, "r" );
837 /* No configuration file found! */
838 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
839 NGIRCd_ConfFile, strerror( errno ));
842 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
847 Set_Defaults(IsStarting);
850 Config_Error(LOG_INFO,
851 "Reading configuration from \"%s\" ...",
854 /* Clean up server configuration structure: mark all already
855 * configured servers as "once" so that they are deleted
856 * after the next disconnect and delete all unused servers.
857 * And delete all servers which are "duplicates" of servers
858 * that are already marked as "once" (such servers have been
859 * created by the last rehash but are now useless). */
860 for( i = 0; i < MAX_SERVERS; i++ ) {
861 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
863 /* This structure is in use ... */
864 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
865 /* Check for duplicates */
866 for( n = 0; n < MAX_SERVERS; n++ ) {
867 if( n == i ) continue;
869 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
870 Init_Server_Struct( &Conf_Server[n] );
872 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
878 /* Mark server as "once" */
879 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
880 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
885 /* Initialize variables */
887 strcpy( section, "" );
888 Init_Server_Struct( &New_Server );
889 New_Server_Idx = NONE;
893 /* Read configuration file */
895 if( ! fgets( str, LINE_LEN, fd )) break;
899 /* Skip comments and empty lines */
900 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
902 /* Is this the beginning of a new section? */
903 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
904 strlcpy( section, str, sizeof( section ));
905 if (strcasecmp(section, "[GLOBAL]") == 0
906 || strcasecmp(section, "[LIMITS]") == 0
907 || strcasecmp(section, "[OPTIONS]") == 0
909 || strcasecmp(section, "[SSL]") == 0
914 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
915 /* Check if there is already a server to add */
916 if( New_Server.name[0] ) {
917 /* Copy data to "real" server structure */
918 assert( New_Server_Idx > NONE );
919 Conf_Server[New_Server_Idx] = New_Server;
922 /* Re-init structure for new server */
923 Init_Server_Struct( &New_Server );
925 /* Search unused item in server configuration structure */
926 for( i = 0; i < MAX_SERVERS; i++ ) {
927 /* Is this item used? */
928 if( ! Conf_Server[i].name[0] ) break;
930 if( i >= MAX_SERVERS ) {
931 /* Oops, no free item found! */
932 Config_Error( LOG_ERR, "Too many servers configured." );
933 New_Server_Idx = NONE;
935 else New_Server_Idx = i;
939 if (strcasecmp(section, "[CHANNEL]") == 0) {
940 count = array_length(&Conf_Channels,
941 sizeof(struct Conf_Channel));
942 if (!array_alloc(&Conf_Channels,
943 sizeof(struct Conf_Channel),
945 Config_Error(LOG_ERR,
946 "Could not allocate memory for new operator (line %d)",
952 if (strcasecmp(section, "[OPERATOR]") == 0) {
953 count = array_length(&Conf_Opers,
954 sizeof(struct Conf_Oper));
955 if (!array_alloc(&Conf_Opers,
956 sizeof(struct Conf_Oper),
958 Config_Error(LOG_ERR,
959 "Could not allocate memory for new channel (line &d)",
965 Config_Error(LOG_ERR,
966 "%s, line %d: Unknown section \"%s\"!",
967 NGIRCd_ConfFile, line, section);
970 if( section[0] == 0x1 ) continue;
972 /* Split line into variable name and parameters */
973 ptr = strchr( str, '=' );
975 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
979 var = str; ngt_TrimStr( var );
980 arg = ptr + 1; ngt_TrimStr( arg );
982 if(strcasecmp(section, "[GLOBAL]") == 0)
983 Handle_GLOBAL(line, var, arg);
984 else if(strcasecmp(section, "[LIMITS]") == 0)
985 Handle_LIMITS(line, var, arg);
986 else if(strcasecmp(section, "[OPTIONS]") == 0)
987 Handle_OPTIONS(line, var, arg);
989 else if(strcasecmp(section, "[SSL]") == 0)
990 Handle_SSL(line, var, arg);
992 else if(strcasecmp(section, "[OPERATOR]") == 0)
993 Handle_OPERATOR(line, var, arg);
994 else if(strcasecmp(section, "[SERVER]") == 0)
995 Handle_SERVER(line, var, arg);
996 else if(strcasecmp(section, "[CHANNEL]") == 0)
997 Handle_CHANNEL(line, var, arg);
999 Config_Error(LOG_ERR,
1000 "%s, line %d: Variable \"%s\" outside section!",
1001 NGIRCd_ConfFile, line, var);
1004 /* Close configuration file */
1007 /* Check if there is still a server to add */
1008 if( New_Server.name[0] ) {
1009 /* Copy data to "real" server structure */
1010 assert( New_Server_Idx > NONE );
1011 Conf_Server[New_Server_Idx] = New_Server;
1014 /* not a single listening port? Add default. */
1015 if (no_listenports() &&
1016 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
1018 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
1019 (unsigned int) defaultport, strerror(errno));
1024 if (!Conf_ListenAddress)
1025 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
1027 if (!Conf_ListenAddress) {
1028 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
1032 /* No MOTD phrase configured? (re)try motd file. */
1033 if (array_bytes(&Conf_Motd) == 0)
1034 Read_Motd(Conf_MotdFile);
1037 /* Make sure that all SSL-related files are readable */
1038 CheckFileReadable("CertFile", Conf_SSLOptions.CertFile);
1039 CheckFileReadable("DHFile", Conf_SSLOptions.DHFile);
1040 CheckFileReadable("KeyFile", Conf_SSLOptions.KeyFile);
1047 * Check whether a string argument is "true" or "false".
1049 * @param Arg Input string.
1050 * @returns true if the input string has been parsed as "yes", "true"
1051 * (case insensitive) or a non-zero integer value.
1054 Check_ArgIsTrue(const char *Arg)
1056 if (strcasecmp(Arg, "yes") == 0)
1058 if (strcasecmp(Arg, "true") == 0)
1067 * Handle setting of "MaxNickLength".
1069 * @param Line Line number in configuration file.
1070 * @raram Arg Input string.
1071 * @returns New configured maximum nick name length.
1074 Handle_MaxNickLength(int Line, const char *Arg)
1078 new = (unsigned) atoi(Arg) + 1;
1079 if (new > CLIENT_NICK_LEN) {
1080 Config_Error(LOG_WARNING,
1081 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
1082 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
1083 return CLIENT_NICK_LEN;
1086 Config_Error(LOG_WARNING,
1087 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
1088 NGIRCd_ConfFile, Line);
1095 * Output a warning messages if IDENT is configured but not compiled in.
1098 WarnIdent(int UNUSED Line)
1102 /* user has enabled ident lookups explicitly, but ... */
1103 Config_Error(LOG_WARNING,
1104 "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
1105 NGIRCd_ConfFile, Line);
1111 * Output a warning messages if IPv6 is configured but not compiled in.
1114 WarnIPv6(int UNUSED Line)
1117 if (Conf_ConnectIPv6) {
1118 /* user has enabled IPv6 explicitly, but ... */
1119 Config_Error(LOG_WARNING,
1120 "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
1121 NGIRCd_ConfFile, Line);
1127 * Output a warning messages if PAM is configured but not compiled in.
1130 WarnPAM(int UNUSED Line)
1134 Config_Error(LOG_WARNING,
1135 "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
1136 NGIRCd_ConfFile, Line);
1142 * Handle legacy "NoXXX" options in [GLOBAL] section.
1144 * TODO: This function and support for "NoXXX" could be removed starting
1145 * with ngIRCd release 19 (one release after marking it "deprecated").
1147 * @param Var Variable name.
1148 * @param Arg Argument string.
1149 * @returns true if a NoXXX option has been processed; false otherwise.
1152 CheckLegacyNoOption(const char *Var, const char *Arg)
1154 if(strcasecmp(Var, "NoDNS") == 0) {
1155 Conf_DNS = !Check_ArgIsTrue( Arg );
1158 if (strcasecmp(Var, "NoIdent") == 0) {
1159 Conf_Ident = !Check_ArgIsTrue(Arg);
1162 if(strcasecmp(Var, "NoPAM") == 0) {
1163 Conf_PAM = !Check_ArgIsTrue(Arg);
1170 * Handle deprecated legacy options in [GLOBAL] section.
1172 * TODO: This function and support for these options in the [Global] section
1173 * could be removed starting with ngIRCd release 19 (one release after
1174 * marking it "deprecated").
1176 * @param Var Variable name.
1177 * @param Arg Argument string.
1178 * @returns true if a legacy option has been processed; false otherwise.
1181 CheckLegacyGlobalOption(int Line, char *Var, char *Arg)
1183 if (strcasecmp(Var, "AllowRemoteOper") == 0
1184 || strcasecmp(Var, "ChrootDir") == 0
1185 || strcasecmp(Var, "ConnectIPv4") == 0
1186 || strcasecmp(Var, "ConnectIPv6") == 0
1187 || strcasecmp(Var, "OperCanUseMode") == 0
1188 || strcasecmp(Var, "OperChanPAutoOp") == 0
1189 || strcasecmp(Var, "OperServerMode") == 0
1190 || strcasecmp(Var, "PredefChannelsOnly") == 0
1191 || strcasecmp(Var, "SyslogFacility") == 0
1192 || strcasecmp(Var, "WebircPassword") == 0) {
1193 Handle_OPTIONS(Line, Var, Arg);
1196 if (strcasecmp(Var, "ConnectRetry") == 0
1197 || strcasecmp(Var, "MaxConnections") == 0
1198 || strcasecmp(Var, "MaxConnectionsIP") == 0
1199 || strcasecmp(Var, "MaxJoins") == 0
1200 || strcasecmp(Var, "MaxNickLength") == 0
1201 || strcasecmp(Var, "PingTimeout") == 0
1202 || strcasecmp(Var, "PongTimeout") == 0) {
1203 Handle_LIMITS(Line, Var, Arg);
1207 if (strcasecmp(Var, "SSLCertFile") == 0
1208 || strcasecmp(Var, "SSLDHFile") == 0
1209 || strcasecmp(Var, "SSLKeyFile") == 0
1210 || strcasecmp(Var, "SSLKeyFilePassword") == 0
1211 || strcasecmp(Var, "SSLPorts") == 0) {
1212 Handle_SSL(Line, Var + 3, Arg);
1221 * Strip "no" prefix of a string.
1223 * TODO: This function and support for "NoXXX" should be removed starting
1224 * with ngIRCd release 19! (One release after marking it "deprecated").
1226 * @param str Pointer to input string starting with "no".
1227 * @returns New pointer to string without "no" prefix.
1230 NoNo(const char *str)
1232 assert(strncasecmp("no", str, 2) == 0 && str[2]);
1237 * Invert "boolean" string.
1239 * TODO: This function and support for "NoXXX" should be removed starting
1240 * with ngIRCd release 19! (One release after marking it "deprecated").
1242 * @param arg "Boolean" input string.
1243 * @returns Pointer to inverted "boolean string".
1246 InvertArg(const char *arg)
1248 return yesno_to_str(!Check_ArgIsTrue(arg));
1252 * Handle variable in [Global] configuration section.
1254 * @param Line Line numer in configuration file.
1255 * @param Var Variable name.
1256 * @param Arg Variable argument.
1259 Handle_GLOBAL( int Line, char *Var, char *Arg )
1264 const char *section;
1267 assert(Var != NULL);
1268 assert(Arg != NULL);
1270 if (strcasecmp(Var, "Name") == 0) {
1271 len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
1272 if (len >= sizeof(Conf_ServerName))
1273 Config_Error_TooLong(Line, Var);
1276 if (strcasecmp(Var, "AdminInfo1") == 0) {
1277 len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
1278 if (len >= sizeof(Conf_ServerAdmin1))
1279 Config_Error_TooLong(Line, Var);
1282 if (strcasecmp(Var, "AdminInfo2") == 0) {
1283 len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
1284 if (len >= sizeof(Conf_ServerAdmin2))
1285 Config_Error_TooLong(Line, Var);
1288 if (strcasecmp(Var, "AdminEMail") == 0) {
1289 len = strlcpy(Conf_ServerAdminMail, Arg,
1290 sizeof(Conf_ServerAdminMail));
1291 if (len >= sizeof(Conf_ServerAdminMail))
1292 Config_Error_TooLong(Line, Var);
1295 if (strcasecmp(Var, "Info") == 0) {
1296 len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
1297 if (len >= sizeof(Conf_ServerInfo))
1298 Config_Error_TooLong(Line, Var);
1301 if (strcasecmp(Var, "Listen") == 0) {
1302 if (Conf_ListenAddress) {
1303 Config_Error(LOG_ERR,
1304 "Multiple Listen= options, ignoring: %s",
1308 Conf_ListenAddress = strdup_warn(Arg);
1309 /* If allocation fails, we're in trouble: we cannot ignore the
1310 * error -- otherwise ngircd would listen on all interfaces. */
1311 if (!Conf_ListenAddress) {
1312 Config_Error(LOG_ALERT,
1313 "%s exiting due to fatal errors!",
1319 if (strcasecmp(Var, "MotdFile") == 0) {
1320 len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
1321 if (len >= sizeof(Conf_MotdFile))
1322 Config_Error_TooLong(Line, Var);
1325 if (strcasecmp(Var, "MotdPhrase") == 0) {
1329 if (len >= LINE_LEN) {
1330 Config_Error_TooLong(Line, Var);
1333 if (!array_copyb(&Conf_Motd, Arg, len + 1))
1334 Config_Error(LOG_WARNING,
1335 "%s, line %d: Could not append MotdPhrase: %s",
1336 NGIRCd_ConfFile, Line, strerror(errno));
1337 Using_MotdFile = false;
1340 if(strcasecmp(Var, "Password") == 0) {
1341 len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
1342 if (len >= sizeof(Conf_ServerPwd))
1343 Config_Error_TooLong(Line, Var);
1346 if (strcasecmp(Var, "PidFile") == 0) {
1347 len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
1348 if (len >= sizeof(Conf_PidFile))
1349 Config_Error_TooLong(Line, Var);
1352 if (strcasecmp(Var, "Ports") == 0) {
1353 ports_parse(&Conf_ListenPorts, Line, Arg);
1356 if (strcasecmp(Var, "ServerGID") == 0) {
1357 grp = getgrnam(Arg);
1359 Conf_GID = grp->gr_gid;
1361 Conf_GID = (unsigned int)atoi(Arg);
1362 if (!Conf_GID && strcmp(Arg, "0"))
1363 Config_Error(LOG_WARNING,
1364 "%s, line %d: Value of \"%s\" is not a valid group name or ID!",
1365 NGIRCd_ConfFile, Line, Var);
1369 if (strcasecmp(Var, "ServerUID") == 0) {
1370 pwd = getpwnam(Arg);
1372 Conf_UID = pwd->pw_uid;
1374 Conf_UID = (unsigned int)atoi(Arg);
1375 if (!Conf_UID && strcmp(Arg, "0"))
1376 Config_Error(LOG_WARNING,
1377 "%s, line %d: Value of \"%s\" is not a valid user name or ID!",
1378 NGIRCd_ConfFile, Line, Var);
1383 if (CheckLegacyNoOption(Var, Arg)) {
1384 /* TODO: This function and support for "NoXXX" could be
1385 * be removed starting with ngIRCd release 19 (one release
1386 * after marking it "deprecated"). */
1387 Config_Error(LOG_WARNING,
1388 "%s, line %d (section \"Global\"): \"No\"-Prefix is deprecated, use \"%s = %s\" in [Options] section!",
1389 NGIRCd_ConfFile, Line, NoNo(Var), InvertArg(Arg));
1390 if (strcasecmp(Var, "NoIdent") == 0)
1392 else if (strcasecmp(Var, "NoPam") == 0)
1396 if ((section = CheckLegacyGlobalOption(Line, Var, Arg))) {
1397 /** TODO: This function and support for these options in the
1398 * [Global] section could be removed starting with ngIRCd
1399 * release 19 (one release after marking it "deprecated"). */
1400 if (strncasecmp(Var, "SSL", 3) == 0) {
1401 Config_Error(LOG_WARNING,
1402 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s and rename to \"%s\"!",
1403 NGIRCd_ConfFile, Line, Var, section,
1406 Config_Error(LOG_WARNING,
1407 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s!",
1408 NGIRCd_ConfFile, Line, Var, section);
1413 Config_Error_Section(Line, Var, "Global");
1417 * Handle variable in [Limits] configuration section.
1419 * @param Line Line numer in configuration file.
1420 * @param Var Variable name.
1421 * @param Arg Variable argument.
1424 Handle_LIMITS(int Line, char *Var, char *Arg)
1427 assert(Var != NULL);
1428 assert(Arg != NULL);
1430 if (strcasecmp(Var, "ConnectRetry") == 0) {
1431 Conf_ConnectRetry = atoi(Arg);
1432 if (Conf_ConnectRetry < 5) {
1433 Config_Error(LOG_WARNING,
1434 "%s, line %d: Value of \"ConnectRetry\" too low!",
1435 NGIRCd_ConfFile, Line);
1436 Conf_ConnectRetry = 5;
1440 if (strcasecmp(Var, "MaxConnections") == 0) {
1441 Conf_MaxConnections = atoi(Arg);
1442 if (!Conf_MaxConnections && strcmp(Arg, "0"))
1443 Config_Error_NaN(Line, Var);
1446 if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
1447 Conf_MaxConnectionsIP = atoi(Arg);
1448 if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
1449 Config_Error_NaN(Line, Var);
1452 if (strcasecmp(Var, "MaxJoins") == 0) {
1453 Conf_MaxJoins = atoi(Arg);
1454 if (!Conf_MaxJoins && strcmp(Arg, "0"))
1455 Config_Error_NaN(Line, Var);
1458 if (strcasecmp(Var, "MaxNickLength") == 0) {
1459 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1462 if (strcasecmp(Var, "MaxListSize") == 0) {
1463 Conf_MaxListSize = atoi(Arg);
1464 if (!Conf_MaxListSize && strcmp(Arg, "0"))
1465 Config_Error_NaN(Line, Var);
1468 if (strcasecmp(Var, "PingTimeout") == 0) {
1469 Conf_PingTimeout = atoi(Arg);
1470 if (Conf_PingTimeout < 5) {
1471 Config_Error(LOG_WARNING,
1472 "%s, line %d: Value of \"PingTimeout\" too low!",
1473 NGIRCd_ConfFile, Line);
1474 Conf_PingTimeout = 5;
1478 if (strcasecmp(Var, "PongTimeout") == 0) {
1479 Conf_PongTimeout = atoi(Arg);
1480 if (Conf_PongTimeout < 5) {
1481 Config_Error(LOG_WARNING,
1482 "%s, line %d: Value of \"PongTimeout\" too low!",
1483 NGIRCd_ConfFile, Line);
1484 Conf_PongTimeout = 5;
1489 Config_Error_Section(Line, Var, "Limits");
1493 * Handle variable in [Options] configuration section.
1495 * @param Line Line numer in configuration file.
1496 * @param Var Variable name.
1497 * @param Arg Variable argument.
1500 Handle_OPTIONS(int Line, char *Var, char *Arg)
1505 assert(Var != NULL);
1506 assert(Arg != NULL);
1508 if (strcasecmp(Var, "AllowRemoteOper") == 0) {
1509 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1512 if (strcasecmp(Var, "ChrootDir") == 0) {
1513 len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
1514 if (len >= sizeof(Conf_Chroot))
1515 Config_Error_TooLong(Line, Var);
1518 if (strcasecmp(Var, "CloakHost") == 0) {
1519 len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
1520 if (len >= sizeof(Conf_CloakHost))
1521 Config_Error_TooLong(Line, Var);
1524 if (strcasecmp(Var, "CloakHostModeX") == 0) {
1525 len = strlcpy(Conf_CloakHostModeX, Arg, sizeof(Conf_CloakHostModeX));
1526 if (len >= sizeof(Conf_CloakHostModeX))
1527 Config_Error_TooLong(Line, Var);
1530 if (strcasecmp(Var, "CloakHostSalt") == 0) {
1531 len = strlcpy(Conf_CloakHostSalt, Arg, sizeof(Conf_CloakHostSalt));
1532 if (len >= sizeof(Conf_CloakHostSalt))
1533 Config_Error_TooLong(Line, Var);
1536 if (strcasecmp(Var, "CloakUserToNick") == 0) {
1537 Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
1540 if (strcasecmp(Var, "ConnectIPv6") == 0) {
1541 Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
1545 if (strcasecmp(Var, "ConnectIPv4") == 0) {
1546 Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
1549 if (strcasecmp(Var, "DNS") == 0) {
1550 Conf_DNS = Check_ArgIsTrue(Arg);
1553 if (strcasecmp(Var, "Ident") == 0) {
1554 Conf_Ident = Check_ArgIsTrue(Arg);
1558 if (strcasecmp(Var, "MorePrivacy") == 0) {
1559 Conf_MorePrivacy = Check_ArgIsTrue(Arg);
1562 if (strcasecmp(Var, "NoticeAuth") == 0) {
1563 Conf_NoticeAuth = Check_ArgIsTrue(Arg);
1566 if (strcasecmp(Var, "OperCanUseMode") == 0) {
1567 Conf_OperCanMode = Check_ArgIsTrue(Arg);
1570 if (strcasecmp(Var, "OperChanPAutoOp") == 0) {
1571 Conf_OperChanPAutoOp = Check_ArgIsTrue(Arg);
1574 if (strcasecmp(Var, "OperServerMode") == 0) {
1575 Conf_OperServerMode = Check_ArgIsTrue(Arg);
1578 if (strcasecmp(Var, "PAM") == 0) {
1579 Conf_PAM = Check_ArgIsTrue(Arg);
1583 if (strcasecmp(Var, "PAMIsOptional") == 0 ) {
1584 Conf_PAMIsOptional = Check_ArgIsTrue(Arg);
1587 if (strcasecmp(Var, "PredefChannelsOnly") == 0) {
1588 Conf_PredefChannelsOnly = Check_ArgIsTrue(Arg);
1592 if (strcasecmp(Var, "RequireAuthPing") == 0) {
1593 Conf_AuthPing = Check_ArgIsTrue(Arg);
1597 if (strcasecmp(Var, "ScrubCTCP") == 0) {
1598 Conf_ScrubCTCP = Check_ArgIsTrue(Arg);
1602 if (strcasecmp(Var, "SyslogFacility") == 0) {
1603 Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
1604 Conf_SyslogFacility);
1608 if (strcasecmp(Var, "WebircPassword") == 0) {
1609 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
1610 if (len >= sizeof(Conf_WebircPwd))
1611 Config_Error_TooLong(Line, Var);
1615 Config_Error_Section(Line, Var, "Options");
1621 * Handle variable in [SSL] configuration section.
1623 * @param Line Line numer in configuration file.
1624 * @param Var Variable name.
1625 * @param Arg Variable argument.
1628 Handle_SSL(int Line, char *Var, char *Arg)
1631 assert(Var != NULL);
1632 assert(Arg != NULL);
1634 if (strcasecmp(Var, "CertFile") == 0) {
1635 assert(Conf_SSLOptions.CertFile == NULL);
1636 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1639 if (strcasecmp(Var, "DHFile") == 0) {
1640 assert(Conf_SSLOptions.DHFile == NULL);
1641 Conf_SSLOptions.DHFile = strdup_warn(Arg);
1644 if (strcasecmp(Var, "KeyFile") == 0) {
1645 assert(Conf_SSLOptions.KeyFile == NULL);
1646 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1649 if (strcasecmp(Var, "KeyFilePassword") == 0) {
1650 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1651 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1652 Config_Error(LOG_ERR,
1653 "%s, line %d (section \"SSL\"): Could not copy %s: %s!",
1654 NGIRCd_ConfFile, Line, Var,
1658 if (strcasecmp(Var, "Ports") == 0) {
1659 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1663 Config_Error_Section(Line, Var, "SSL");
1669 * Handle variable in [Operator] configuration section.
1671 * @param Line Line numer in configuration file.
1672 * @param Var Variable name.
1673 * @param Arg Variable argument.
1676 Handle_OPERATOR( int Line, char *Var, char *Arg )
1679 struct Conf_Oper *op;
1682 assert( Var != NULL );
1683 assert( Arg != NULL );
1685 op = array_get(&Conf_Opers, sizeof(*op),
1686 array_length(&Conf_Opers, sizeof(*op)) - 1);
1690 if (strcasecmp(Var, "Name") == 0) {
1691 /* Name of IRC operator */
1692 len = strlcpy(op->name, Arg, sizeof(op->name));
1693 if (len >= sizeof(op->name))
1694 Config_Error_TooLong(Line, Var);
1697 if (strcasecmp(Var, "Password") == 0) {
1698 /* Password of IRC operator */
1699 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1700 if (len >= sizeof(op->pwd))
1701 Config_Error_TooLong(Line, Var);
1704 if (strcasecmp(Var, "Mask") == 0) {
1706 return; /* Hostname already configured */
1707 op->mask = strdup_warn( Arg );
1711 Config_Error_Section(Line, Var, "Operator");
1715 * Handle variable in [Server] configuration section.
1717 * @param Line Line numer in configuration file.
1718 * @param Var Variable name.
1719 * @param Arg Variable argument.
1722 Handle_SERVER( int Line, char *Var, char *Arg )
1728 assert( Var != NULL );
1729 assert( Arg != NULL );
1731 /* Ignore server block if no space is left in server configuration structure */
1732 if( New_Server_Idx <= NONE ) return;
1734 if( strcasecmp( Var, "Host" ) == 0 ) {
1735 /* Hostname of the server */
1736 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1737 if (len >= sizeof( New_Server.host ))
1738 Config_Error_TooLong ( Line, Var );
1741 if( strcasecmp( Var, "Name" ) == 0 ) {
1742 /* Name of the server ("Nick"/"ID") */
1743 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1744 if (len >= sizeof( New_Server.name ))
1745 Config_Error_TooLong( Line, Var );
1748 if (strcasecmp(Var, "Bind") == 0) {
1749 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1752 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1753 NGIRCd_ConfFile, Line, Arg);
1756 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1757 /* Password of this server which is sent to the peer */
1759 Config_Error(LOG_ERR,
1760 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1761 NGIRCd_ConfFile, Line);
1763 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1764 if (len >= sizeof( New_Server.pwd_in ))
1765 Config_Error_TooLong( Line, Var );
1768 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1769 /* Passwort of the peer which must be received */
1770 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1771 if (len >= sizeof( New_Server.pwd_out ))
1772 Config_Error_TooLong( Line, Var );
1775 if( strcasecmp( Var, "Port" ) == 0 ) {
1776 /* Port to which this server should connect */
1778 if (port >= 0 && port < 0xFFFF)
1779 New_Server.port = (UINT16)port;
1781 Config_Error(LOG_ERR,
1782 "%s, line %d (section \"Server\"): Illegal port number %ld!",
1783 NGIRCd_ConfFile, Line, port );
1787 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1788 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1792 if( strcasecmp( Var, "Group" ) == 0 ) {
1794 New_Server.group = atoi( Arg );
1795 if (!New_Server.group && strcmp(Arg, "0"))
1796 Config_Error_NaN(Line, Var);
1799 if( strcasecmp( Var, "Passive" ) == 0 ) {
1800 if (Check_ArgIsTrue(Arg))
1801 New_Server.flags |= CONF_SFLAG_DISABLED;
1804 if (strcasecmp(Var, "ServiceMask") == 0) {
1805 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1806 sizeof(New_Server.svs_mask));
1807 if (len >= sizeof(New_Server.svs_mask))
1808 Config_Error_TooLong(Line, Var);
1812 Config_Error_Section(Line, Var, "Server");
1816 * Copy channel name into channel structure.
1818 * If the channel name is not valid because of a missing prefix ('#', '&'),
1819 * a default prefix of '#' will be added.
1821 * @param new_chan New already allocated channel structure.
1822 * @param name Name of the new channel.
1823 * @returns true on success, false otherwise.
1826 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1828 size_t size = sizeof(new_chan->name);
1829 char *dest = new_chan->name;
1831 if (!Channel_IsValidName(name)) {
1833 * maybe user forgot to add a '#'.
1834 * This is only here for user convenience.
1840 return size > strlcpy(dest, name, size);
1844 * Handle variable in [Channel] configuration section.
1846 * @param Line Line numer in configuration file.
1847 * @param Var Variable name.
1848 * @param Arg Variable argument.
1851 Handle_CHANNEL(int Line, char *Var, char *Arg)
1854 struct Conf_Channel *chan;
1857 assert( Var != NULL );
1858 assert( Arg != NULL );
1860 chan = array_get(&Conf_Channels, sizeof(*chan),
1861 array_length(&Conf_Channels, sizeof(*chan)) - 1);
1865 if (strcasecmp(Var, "Name") == 0) {
1866 if (!Handle_Channelname(chan, Arg))
1867 Config_Error_TooLong(Line, Var);
1870 if (strcasecmp(Var, "Modes") == 0) {
1872 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1873 if (len >= sizeof(chan->modes))
1874 Config_Error_TooLong( Line, Var );
1877 if( strcasecmp( Var, "Topic" ) == 0 ) {
1879 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1880 if (len >= sizeof(chan->topic))
1881 Config_Error_TooLong( Line, Var );
1884 if( strcasecmp( Var, "Key" ) == 0 ) {
1885 /* Initial Channel Key (mode k) */
1886 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1887 if (len >= sizeof(chan->key))
1888 Config_Error_TooLong(Line, Var);
1891 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1892 /* maximum user limit, mode l */
1893 chan->maxusers = (unsigned long) atol(Arg);
1894 if (!chan->maxusers && strcmp(Arg, "0"))
1895 Config_Error_NaN(Line, Var);
1898 if (strcasecmp(Var, "KeyFile") == 0) {
1900 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1901 if (len >= sizeof(chan->keyfile))
1902 Config_Error_TooLong(Line, Var);
1906 Config_Error_Section(Line, Var, "Channel");
1910 * Validate server configuration.
1912 * Please note that this function uses exit(1) on fatal errors and therefore
1913 * can result in ngIRCd terminating!
1915 * @param Configtest true if the daemon has been called with "--configtest".
1916 * @param Rehash true if re-reading configuration on runtime.
1917 * @returns true if configuration is valid.
1920 Validate_Config(bool Configtest, bool Rehash)
1922 /* Validate configuration settings. */
1925 int i, servers, servers_once;
1927 bool config_valid = true;
1930 /* Emit a warning when the config file is not a full path name */
1931 if (NGIRCd_ConfFile[0] && NGIRCd_ConfFile[0] != '/') {
1932 Config_Error(LOG_WARNING,
1933 "Not specifying a full path name to \"%s\" can cause problems when rehashing the server!",
1937 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1938 ptr = Conf_ServerName;
1940 if (*ptr >= 'a' && *ptr <= 'z') continue;
1941 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1942 if (*ptr >= '0' && *ptr <= '9') continue;
1943 if (ptr > Conf_ServerName) {
1944 if (*ptr == '.' || *ptr == '-')
1947 Conf_ServerName[0] = '\0';
1951 if (!Conf_ServerName[0]) {
1952 /* No server name configured! */
1953 config_valid = false;
1954 Config_Error(LOG_ALERT,
1955 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1957 if (!Configtest && !Rehash) {
1958 Config_Error(LOG_ALERT,
1959 "%s exiting due to fatal errors!",
1965 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1966 /* No dot in server name! */
1967 config_valid = false;
1968 Config_Error(LOG_ALERT,
1969 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1972 Config_Error(LOG_ALERT,
1973 "%s exiting due to fatal errors!",
1980 if (!Conf_ServerAdminMail[0]) {
1981 /* No administrative contact configured! */
1982 config_valid = false;
1983 Config_Error(LOG_ALERT,
1984 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1987 Config_Error(LOG_ALERT,
1988 "%s exiting due to fatal errors!",
1995 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1996 && !Conf_ServerAdminMail[0]) {
1997 /* No administrative information configured! */
1998 Config_Error(LOG_WARNING,
1999 "No administrative information configured but required by RFC!");
2003 if (Conf_ServerPwd[0])
2004 Config_Error(LOG_ERR,
2005 "This server uses PAM, \"Password\" in [Global] section will be ignored!");
2009 servers = servers_once = 0;
2010 for (i = 0; i < MAX_SERVERS; i++) {
2011 if (Conf_Server[i].name[0]) {
2013 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
2018 "Configuration: Operators=%ld, Servers=%d[%d], Channels=%ld",
2019 array_length(&Conf_Opers, sizeof(struct Conf_Oper)),
2020 servers, servers_once,
2021 array_length(&Conf_Channels, sizeof(struct Conf_Channel)));
2024 return config_valid;
2028 * Output "line too long" warning.
2030 * @param Line Line number in configuration file.
2031 * @param Item Affected variable name.
2034 Config_Error_TooLong ( const int Line, const char *Item )
2036 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
2040 * Output "unknown variable" warning.
2042 * @param Line Line number in configuration file.
2043 * @param Item Affected variable name.
2044 * @param Section Section name.
2047 Config_Error_Section(const int Line, const char *Item, const char *Section)
2049 Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
2050 NGIRCd_ConfFile, Line, Section, Item);
2054 * Output "not a number" warning.
2056 * @param Line Line number in configuration file.
2057 * @param Item Affected variable name.
2060 Config_Error_NaN( const int Line, const char *Item )
2062 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
2063 NGIRCd_ConfFile, Line, Item );
2067 * Output configuration error to console and/or logfile.
2069 * On runtime, the normal log functions of the daemon are used. But when
2070 * testing the configuration ("--configtest"), all messages go directly
2073 * @param Level Severity level of the message.
2074 * @param Format Format string; see printf() function.
2077 static void Config_Error( const int Level, const char *Format, ... )
2079 static void Config_Error( Level, Format, va_alist )
2085 char msg[MAX_LOG_MSG_LEN];
2088 assert( Format != NULL );
2091 va_start( ap, Format );
2095 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
2099 if (Level <= LOG_WARNING)
2100 printf(" - %s\n", msg);
2104 Log(Level, "%s", msg);
2110 * Dump internal state of the "configuration module".
2113 Conf_DebugDump(void)
2117 Log(LOG_DEBUG, "Configured servers:");
2118 for (i = 0; i < MAX_SERVERS; i++) {
2119 if (! Conf_Server[i].name[0])
2122 " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
2123 Conf_Server[i].name, Conf_Server[i].host,
2124 Conf_Server[i].port, Conf_Server[i].lasttry,
2125 Conf_Server[i].group, Conf_Server[i].flags,
2126 Conf_Server[i].conn_id);
2133 * Initialize server configuration structur to default values.
2135 * @param Server Pointer to server structure to initialize.
2138 Init_Server_Struct( CONF_SERVER *Server )
2140 assert( Server != NULL );
2142 memset( Server, 0, sizeof (CONF_SERVER) );
2144 Server->group = NONE;
2145 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
2147 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
2149 Proc_InitStruct(&Server->res_stat);
2150 Server->conn_id = NONE;
2151 memset(&Server->bind_addr, 0, sizeof(Server->bind_addr));