2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
16 * Configuration management (reading, parsing & validation)
35 #include <sys/types.h>
52 static bool Use_Log = true, Using_MotdFile = true;
53 static CONF_SERVER New_Server;
54 static int New_Server_Idx;
56 static char Conf_MotdFile[FNAME_LEN];
57 static char Conf_HelpFile[FNAME_LEN];
59 static void Set_Defaults PARAMS(( bool InitServers ));
60 static bool Read_Config PARAMS(( bool TestOnly, bool IsStarting ));
61 static void Read_Config_File PARAMS(( const char *File, FILE *fd ));
62 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
64 static void Handle_GLOBAL PARAMS((const char *File, int Line,
65 char *Var, char *Arg ));
66 static void Handle_LIMITS PARAMS((const char *File, int Line,
67 char *Var, char *Arg ));
68 static void Handle_OPTIONS PARAMS((const char *File, int Line,
69 char *Var, char *Arg ));
70 static void Handle_OPERATOR PARAMS((const char *File, int Line,
71 char *Var, char *Arg ));
72 static void Handle_SERVER PARAMS((const char *File, int Line,
73 char *Var, char *Arg ));
74 static void Handle_CHANNEL PARAMS((const char *File, int Line,
75 char *Var, char *Arg ));
77 static void Config_Error PARAMS((const int Level, const char *Format, ...));
79 static void Config_Error_NaN PARAMS((const char *File, const int LINE,
81 static void Config_Error_Section PARAMS((const char *File, const int Line,
82 const char *Item, const char *Section));
83 static void Config_Error_TooLong PARAMS((const char *File, const int LINE,
86 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
90 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
92 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
98 static void Handle_SSL PARAMS((const char *File, int Line, char *Var, char *Ark));
100 struct SSLOptions Conf_SSLOptions;
103 * Initialize SSL configuration.
108 free(Conf_SSLOptions.KeyFile);
109 Conf_SSLOptions.KeyFile = NULL;
111 free(Conf_SSLOptions.CertFile);
112 Conf_SSLOptions.CertFile = NULL;
114 free(Conf_SSLOptions.DHFile);
115 Conf_SSLOptions.DHFile = NULL;
116 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
118 array_free(&Conf_SSLOptions.ListenPorts);
122 * Check if the current configuration uses/requires SSL.
124 * @returns true if SSL is used and should be initialized.
131 /* SSL listen ports configured? */
132 if (array_bytes(&Conf_SSLOptions.ListenPorts))
135 for (i = 0; i < MAX_SERVERS; i++) {
136 if (Conf_Server[i].port > 0
137 && Conf_Server[i].SSLConnect)
144 * Make sure that a configured file is readable.
146 * Currently, this function is only used for SSL-related options ...
148 * @param Var Configuration variable
149 * @param Filename Configured filename
152 CheckFileReadable(const char *Var, const char *Filename)
159 fp = fopen(Filename, "r");
163 Config_Error(LOG_ERR, "Can't read \"%s\" (\"%s\"): %s",
164 Filename, Var, strerror(errno));
171 * Duplicate string and warn on errors.
173 * @returns Pointer to string on success, NULL otherwise.
176 strdup_warn(const char *str)
178 char *ptr = strdup(str);
180 Config_Error(LOG_ERR,
181 "Could not allocate memory for string: %s", str);
186 * Output a comma separated list of ports (integer values).
193 len = array_length(a, sizeof(UINT16));
195 ports = (UINT16*) array_start(a);
196 printf("%u", (unsigned int) *ports);
199 printf(", %u", (unsigned int) *ports);
206 * Parse a comma separated string into an array of port numbers (integers).
209 ports_parse(array *a, int Line, char *Arg)
217 ptr = strtok( Arg, "," );
221 if (port > 0 && port < 0xFFFF) {
222 port16 = (UINT16) port;
223 if (!array_catb(a, (char*)&port16, sizeof port16))
224 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
225 NGIRCd_ConfFile, Line, port, strerror(errno));
227 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
228 NGIRCd_ConfFile, Line, port );
231 ptr = strtok( NULL, "," );
236 * Initialize configuration module.
241 Read_Config(false, true);
242 Validate_Config(false, false);
246 * "Rehash" (reload) server configuration.
248 * @returns true if configuration has been re-read, false on errors.
253 if (!Read_Config(false, false))
255 Validate_Config(false, true);
257 /* Update CLIENT structure of local server */
258 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
263 * Output a boolean value as "yes/no" string.
266 yesno_to_str(int boolean_value)
274 * Free all IRC operator configuration structures.
279 struct Conf_Oper *op;
282 len = array_length(&Conf_Opers, sizeof(*op));
283 op = array_start(&Conf_Opers);
288 array_free(&Conf_Opers);
292 * Output all IRC operator configuration structures.
297 struct Conf_Oper *op;
300 count = array_length(&Conf_Opers, sizeof(*op));
301 op = array_start(&Conf_Opers);
302 for (i = 0; i < count; i++, op++) {
307 printf(" Name = %s\n", op->name);
308 printf(" Password = %s\n", op->pwd);
309 printf(" Mask = %s\n\n", op->mask ? op->mask : "");
314 * Read configuration, validate and output it.
316 * This function waits for a keypress of the user when stdin/stdout are valid
317 * tty's ("you can read our nice message and we can read in your keypress").
319 * @return 0 on succes, 1 on failure(s); therefore the result code can
320 * directly be used by exit() when running "ngircd --configtest".
329 size_t predef_channel_count;
330 struct Conf_Channel *predef_chan;
334 if (!Read_Config(true, true))
337 config_valid = Validate_Config(true, false);
340 if(isatty(fileno(stdin)) && isatty(fileno(stdout))) {
341 puts("OK, press enter to see a dump of your server configuration ...");
344 puts("Ok, dump of your server configuration follows:\n");
347 printf(" Name = %s\n", Conf_ServerName);
348 printf(" AdminInfo1 = %s\n", Conf_ServerAdmin1);
349 printf(" AdminInfo2 = %s\n", Conf_ServerAdmin2);
350 printf(" AdminEMail = %s\n", Conf_ServerAdminMail);
351 printf(" HelpFile = %s\n", Conf_HelpFile);
352 printf(" Info = %s\n", Conf_ServerInfo);
353 printf(" Listen = %s\n", Conf_ListenAddress);
354 if (Using_MotdFile) {
355 printf(" MotdFile = %s\n", Conf_MotdFile);
356 printf(" MotdPhrase =\n");
358 printf(" MotdFile = \n");
359 printf(" MotdPhrase = %s\n", array_bytes(&Conf_Motd)
360 ? (const char*) array_start(&Conf_Motd) : "");
363 printf(" Password = %s\n", Conf_ServerPwd);
365 printf(" PidFile = %s\n", Conf_PidFile);
367 ports_puts(&Conf_ListenPorts);
368 grp = getgrgid(Conf_GID);
370 printf(" ServerGID = %s\n", grp->gr_name);
372 printf(" ServerGID = %ld\n", (long)Conf_GID);
373 pwd = getpwuid(Conf_UID);
375 printf(" ServerUID = %s\n", pwd->pw_name);
377 printf(" ServerUID = %ld\n", (long)Conf_UID);
381 printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
382 printf(" IdleTimeout = %d\n", Conf_IdleTimeout);
383 printf(" MaxConnections = %d\n", Conf_MaxConnections);
384 printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
385 printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
386 printf(" MaxNickLength = %u\n", Conf_MaxNickLength - 1);
387 printf(" MaxListSize = %d\n", Conf_MaxListSize);
388 printf(" PingTimeout = %d\n", Conf_PingTimeout);
389 printf(" PongTimeout = %d\n", Conf_PongTimeout);
393 printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
394 printf(" ChrootDir = %s\n", Conf_Chroot);
395 printf(" CloakHost = %s\n", Conf_CloakHost);
396 printf(" CloakHostModeX = %s\n", Conf_CloakHostModeX);
397 printf(" CloakHostSalt = %s\n", Conf_CloakHostSalt);
398 printf(" CloakUserToNick = %s\n", yesno_to_str(Conf_CloakUserToNick));
400 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
401 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
403 printf(" DNS = %s\n", yesno_to_str(Conf_DNS));
405 printf(" Ident = %s\n", yesno_to_str(Conf_Ident));
407 printf(" MorePrivacy = %s\n", yesno_to_str(Conf_MorePrivacy));
408 printf(" NoticeAuth = %s\n", yesno_to_str(Conf_NoticeAuth));
409 printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
410 printf(" OperChanPAutoOp = %s\n", yesno_to_str(Conf_OperChanPAutoOp));
411 printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
413 printf(" PAM = %s\n", yesno_to_str(Conf_PAM));
414 printf(" PAMIsOptional = %s\n", yesno_to_str(Conf_PAMIsOptional));
416 printf(" PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
418 printf(" RequireAuthPing = %s\n", yesno_to_str(Conf_AuthPing));
420 printf(" ScrubCTCP = %s\n", yesno_to_str(Conf_ScrubCTCP));
422 printf(" SyslogFacility = %s\n",
423 ngt_SyslogFacilityName(Conf_SyslogFacility));
425 printf(" WebircPassword = %s\n", Conf_WebircPwd);
430 printf(" CertFile = %s\n", Conf_SSLOptions.CertFile
431 ? Conf_SSLOptions.CertFile : "");
432 printf(" DHFile = %s\n", Conf_SSLOptions.DHFile
433 ? Conf_SSLOptions.DHFile : "");
434 printf(" KeyFile = %s\n", Conf_SSLOptions.KeyFile
435 ? Conf_SSLOptions.KeyFile : "");
436 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
437 puts(" KeyFilePassword = <secret>");
439 puts(" KeyFilePassword = ");
440 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
442 ports_puts(&Conf_SSLOptions.ListenPorts);
448 for( i = 0; i < MAX_SERVERS; i++ ) {
449 if( ! Conf_Server[i].name[0] ) continue;
451 /* Valid "Server" section */
453 printf( " Name = %s\n", Conf_Server[i].name );
454 printf( " Host = %s\n", Conf_Server[i].host );
455 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
457 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
459 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
460 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
461 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
462 printf( " Group = %d\n", Conf_Server[i].group );
463 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
466 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
467 predef_chan = array_start(&Conf_Channels);
469 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
470 if (!predef_chan->name[0])
473 /* Valid "Channel" section */
475 printf(" Name = %s\n", predef_chan->name);
476 printf(" Modes = %s\n", predef_chan->modes);
477 printf(" Key = %s\n", predef_chan->key);
478 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
479 printf(" Topic = %s\n", predef_chan->topic);
480 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
483 return (config_valid ? 0 : 1);
487 * Remove connection information from configured server.
489 * If the server is set as "once", delete it from our configuration;
490 * otherwise set the time for the next connection attempt.
492 * Non-server connections will be silently ignored.
495 Conf_UnsetServer( CONN_ID Idx )
500 /* Check all our configured servers */
501 for( i = 0; i < MAX_SERVERS; i++ ) {
502 if( Conf_Server[i].conn_id != Idx ) continue;
504 /* Gotcha! Mark server configuration as "unused": */
505 Conf_Server[i].conn_id = NONE;
507 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
508 /* Delete configuration here */
509 Init_Server_Struct( &Conf_Server[i] );
511 /* Set time for next connect attempt */
513 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
514 /* The connection has been "long", so we don't
515 * require the next attempt to be delayed. */
516 Conf_Server[i].lasttry =
517 t - Conf_ConnectRetry + RECONNECT_DELAY;
519 /* "Short" connection, enforce "ConnectRetry"
520 * but randomize it a little bit: 15 seconds. */
521 Conf_Server[i].lasttry =
522 t + rand() / (RAND_MAX / 15);
529 * Set connection information for specified configured server.
532 Conf_SetServer( int ConfServer, CONN_ID Idx )
534 assert( ConfServer > NONE );
535 assert( Idx > NONE );
537 if (Conf_Server[ConfServer].conn_id > NONE &&
538 Conf_Server[ConfServer].conn_id != Idx) {
540 "Connection %d: Server configuration of \"%s\" already in use by connection %d!",
541 Idx, Conf_Server[ConfServer].name,
542 Conf_Server[ConfServer].conn_id);
543 Conn_Close(Idx, NULL, "Server configuration already in use", true);
546 Conf_Server[ConfServer].conn_id = Idx;
551 * Get index of server in configuration structure.
554 Conf_GetServer( CONN_ID Idx )
558 assert( Idx > NONE );
560 for( i = 0; i < MAX_SERVERS; i++ ) {
561 if( Conf_Server[i].conn_id == Idx ) return i;
567 * Enable a server by name and adjust its port number.
569 * @returns true if a server has been enabled and now has a valid port
570 * number and host name for outgoing connections.
573 Conf_EnableServer( const char *Name, UINT16 Port )
577 assert( Name != NULL );
578 for( i = 0; i < MAX_SERVERS; i++ ) {
579 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
580 /* Gotcha! Set port and enable server: */
581 Conf_Server[i].port = Port;
582 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
583 return (Conf_Server[i].port && Conf_Server[i].host[0]);
590 * Enable a server by name.
592 * The server is only usable as outgoing server, if it has set a valid port
593 * number for outgoing connections!
594 * If not, you have to use Conf_EnableServer() function to make it available.
596 * @returns true if a server has been enabled; false otherwise.
599 Conf_EnablePassiveServer(const char *Name)
603 assert( Name != NULL );
604 for (i = 0; i < MAX_SERVERS; i++) {
605 if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
606 && (Conf_Server[i].port > 0)) {
607 /* BINGO! Enable server */
608 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
616 * Disable a server by name.
617 * An already established connection will be disconnected.
619 * @returns true if a server was found and has been disabled.
622 Conf_DisableServer( const char *Name )
626 assert( Name != NULL );
627 for( i = 0; i < MAX_SERVERS; i++ ) {
628 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
629 /* Gotcha! Disable and disconnect server: */
630 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
631 if( Conf_Server[i].conn_id > NONE )
632 Conn_Close(Conf_Server[i].conn_id, NULL,
633 "Server link terminated on operator request",
642 * Add a new remote server to our configuration.
644 * @param Name Name of the new server.
645 * @param Port Port number to connect to or 0 for incoming connections.
646 * @param Host Host name to connect to.
647 * @param MyPwd Password that will be sent to the peer.
648 * @param PeerPwd Password that must be received from the peer.
649 * @returns true if the new server has been added; false otherwise.
652 Conf_AddServer(const char *Name, UINT16 Port, const char *Host,
653 const char *MyPwd, const char *PeerPwd)
657 assert( Name != NULL );
658 assert( Host != NULL );
659 assert( MyPwd != NULL );
660 assert( PeerPwd != NULL );
662 /* Search unused item in server configuration structure */
663 for( i = 0; i < MAX_SERVERS; i++ ) {
664 /* Is this item used? */
665 if( ! Conf_Server[i].name[0] ) break;
667 if( i >= MAX_SERVERS ) return false;
669 Init_Server_Struct( &Conf_Server[i] );
670 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
671 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
672 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
673 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
674 Conf_Server[i].port = Port;
675 Conf_Server[i].flags = CONF_SFLAG_ONCE;
681 * Check if the given nickname is reserved for services on a particular server.
683 * @param ConfServer The server index to check.
684 * @param Nick The nickname to check.
685 * @returns true if the given nickname belongs to an "IRC service".
688 Conf_NickIsService(int ConfServer, const char *Nick)
690 assert (ConfServer >= 0);
691 assert (ConfServer < MAX_SERVERS);
693 return MatchCaseInsensitiveList(Conf_Server[ConfServer].svs_mask,
698 * Check if the given nickname is blocked for "normal client" use.
700 * @param ConfServer The server index or NONE to check all configured servers.
701 * @param Nick The nickname to check.
702 * @returns true if the given nickname belongs to an "IRC service".
705 Conf_NickIsBlocked(const char *Nick)
709 for(i = 0; i < MAX_SERVERS; i++) {
710 if (!Conf_Server[i].name[0])
712 if (Conf_NickIsService(i, Nick))
719 * Initialize configuration settings with their default values.
722 Set_Defaults(bool InitServers)
725 char random[RANDOM_SALT_LEN + 1];
728 strcpy(Conf_ServerName, "");
729 strcpy(Conf_ServerAdmin1, "");
730 strcpy(Conf_ServerAdmin2, "");
731 strcpy(Conf_ServerAdminMail, "");
732 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
733 PACKAGE_NAME, PACKAGE_VERSION);
734 free(Conf_ListenAddress);
735 Conf_ListenAddress = NULL;
736 array_free(&Conf_ListenPorts);
737 array_free(&Conf_Motd);
738 array_free(&Conf_Helptext);
739 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
740 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
741 strlcpy(Conf_HelpFile, DOCDIR, sizeof(Conf_HelpFile));
742 strlcat(Conf_HelpFile, HELP_FILE, sizeof(Conf_HelpFile));
743 strcpy(Conf_ServerPwd, "");
744 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
745 Conf_UID = Conf_GID = 0;
748 Conf_ConnectRetry = 60;
749 Conf_IdleTimeout = 0;
750 Conf_MaxConnections = 0;
751 Conf_MaxConnectionsIP = 5;
753 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
754 Conf_MaxListSize = 100;
755 Conf_PingTimeout = 120;
756 Conf_PongTimeout = 20;
759 Conf_AllowRemoteOper = false;
761 Conf_AuthPing = false;
763 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
764 strcpy(Conf_CloakHost, "");
765 strcpy(Conf_CloakHostModeX, "");
766 strlcpy(Conf_CloakHostSalt, ngt_RandomStr(random, RANDOM_SALT_LEN),
767 sizeof(Conf_CloakHostSalt));
768 Conf_CloakUserToNick = false;
769 Conf_ConnectIPv4 = true;
771 Conf_ConnectIPv6 = true;
773 Conf_ConnectIPv6 = false;
781 Conf_MorePrivacy = false;
782 Conf_NoticeAuth = false;
783 Conf_OperCanMode = false;
784 Conf_OperChanPAutoOp = true;
785 Conf_OperServerMode = false;
791 Conf_PAMIsOptional = false;
792 Conf_PredefChannelsOnly = false;
794 Conf_ScrubCTCP = false;
796 Conf_SyslogFacility = LOG_LOCAL5;
798 Conf_SyslogFacility = 0;
802 /* Initialize server configuration structures */
804 for (i = 0; i < MAX_SERVERS;
805 Init_Server_Struct(&Conf_Server[i++]));
810 * Get number of configured listening ports.
812 * @returns The number of ports (IPv4+IPv6) on which the server should listen.
817 size_t cnt = array_bytes(&Conf_ListenPorts);
819 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
825 * Read contents of a text file into an array.
827 * This function is used to read the MOTD and help text file, for exampe.
829 * @param filename Name of the file to read.
830 * @return true, when the file has been read in.
833 Read_TextFile(const char *Filename, const char *Name, array *Destination)
839 if (*Filename == '\0')
842 fp = fopen(Filename, "r");
844 Config_Error(LOG_ERR, "Can't read %s file \"%s\": %s",
845 Name, Filename, strerror(errno));
849 array_free(Destination);
850 while (fgets(line, (int)sizeof line, fp)) {
851 ngt_TrimLastChr(line, '\n');
853 /* add text including \0 */
854 if (!array_catb(Destination, line, strlen(line) + 1)) {
855 Log(LOG_ERR, "Cannot read/add \"%s\", line %d: %s",
856 Filename, line_no, strerror(errno));
866 * Read ngIRCd configuration file.
868 * Please note that this function uses exit(1) on fatal errors and therefore
869 * can result in ngIRCd terminating!
871 * @param ngircd_starting Flag indicating if ngIRCd is starting or not.
872 * @returns true when the configuration file has been read
873 * successfully; false otherwise.
876 Read_Config(bool TestOnly, bool IsStarting)
878 const UINT16 defaultport = 6667;
882 /* Open configuration file */
883 fd = fopen( NGIRCd_ConfFile, "r" );
885 /* No configuration file found! */
886 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
887 NGIRCd_ConfFile, strerror( errno ));
890 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
895 Set_Defaults(IsStarting);
898 Config_Error(LOG_INFO,
899 "Reading configuration from \"%s\" ...",
902 /* Clean up server configuration structure: mark all already
903 * configured servers as "once" so that they are deleted
904 * after the next disconnect and delete all unused servers.
905 * And delete all servers which are "duplicates" of servers
906 * that are already marked as "once" (such servers have been
907 * created by the last rehash but are now useless). */
908 for( i = 0; i < MAX_SERVERS; i++ ) {
909 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
911 /* This structure is in use ... */
912 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
913 /* Check for duplicates */
914 for( n = 0; n < MAX_SERVERS; n++ ) {
915 if( n == i ) continue;
917 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
918 Init_Server_Struct( &Conf_Server[n] );
920 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
926 /* Mark server as "once" */
927 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
928 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
933 /* Initialize variables */
934 Init_Server_Struct( &New_Server );
935 New_Server_Idx = NONE;
940 Read_Config_File(NGIRCd_ConfFile, fd);
942 /* Close configuration file */
945 /* Check if there is still a server to add */
946 if( New_Server.name[0] ) {
947 /* Copy data to "real" server structure */
948 assert( New_Server_Idx > NONE );
949 Conf_Server[New_Server_Idx] = New_Server;
952 /* not a single listening port? Add default. */
953 if (no_listenports() &&
954 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
956 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
957 (unsigned int) defaultport, strerror(errno));
962 if (!Conf_ListenAddress)
963 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
965 if (!Conf_ListenAddress) {
966 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
970 /* No MOTD phrase configured? (re)try motd file. */
971 if (array_bytes(&Conf_Motd) == 0) {
972 if (Read_TextFile(Conf_MotdFile, "MOTD", &Conf_Motd))
973 Using_MotdFile = true;
976 /* Try to read ngIRCd help text file. */
977 (void)Read_TextFile(Conf_HelpFile, "help text", &Conf_Helptext);
978 if (!array_bytes(&Conf_Helptext))
979 Config_Error(LOG_WARNING,
980 "No help text available, HELP command will be of limited use.");
983 /* Make sure that all SSL-related files are readable */
984 CheckFileReadable("CertFile", Conf_SSLOptions.CertFile);
985 CheckFileReadable("DHFile", Conf_SSLOptions.DHFile);
986 CheckFileReadable("KeyFile", Conf_SSLOptions.KeyFile);
995 static void Read_Config_File(const char *File, FILE *fd)
997 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
1001 /* Read configuration file */
1003 if (!fgets(str, LINE_LEN, fd))
1008 /* Skip comments and empty lines */
1009 if (str[0] == ';' || str[0] == '#' || str[0] == '\0')
1012 /* Is this the beginning of a new section? */
1013 if ((str[0] == '[') && (str[strlen(str) - 1] == ']')) {
1014 strlcpy(section, str, sizeof(section));
1015 if (strcasecmp(section, "[GLOBAL]") == 0
1016 || strcasecmp(section, "[LIMITS]") == 0
1017 || strcasecmp(section, "[OPTIONS]") == 0
1019 || strcasecmp(section, "[SSL]") == 0
1024 if (strcasecmp(section, "[SERVER]") == 0) {
1025 /* Check if there is already a server to add */
1026 if (New_Server.name[0]) {
1027 /* Copy data to "real" server structure */
1028 assert(New_Server_Idx > NONE);
1029 Conf_Server[New_Server_Idx] =
1033 /* Re-init structure for new server */
1034 Init_Server_Struct(&New_Server);
1036 /* Search unused item in server configuration structure */
1037 for (i = 0; i < MAX_SERVERS; i++) {
1038 /* Is this item used? */
1039 if (!Conf_Server[i].name[0])
1042 if (i >= MAX_SERVERS) {
1043 /* Oops, no free item found! */
1044 Config_Error(LOG_ERR,
1045 "Too many servers configured.");
1046 New_Server_Idx = NONE;
1052 if (strcasecmp(section, "[CHANNEL]") == 0) {
1053 count = array_length(&Conf_Channels,
1058 sizeof(struct Conf_Channel), count)) {
1059 Config_Error(LOG_ERR,
1060 "Could not allocate memory for new operator (line %d)",
1066 if (strcasecmp(section, "[OPERATOR]") == 0) {
1067 count = array_length(&Conf_Opers,
1068 sizeof(struct Conf_Oper));
1069 if (!array_alloc(&Conf_Opers,
1070 sizeof(struct Conf_Oper),
1072 Config_Error(LOG_ERR,
1073 "Could not allocate memory for new channel (line &d)",
1079 Config_Error(LOG_ERR,
1080 "%s, line %d: Unknown section \"%s\"!",
1081 NGIRCd_ConfFile, line, section);
1084 if (section[0] == 0x1)
1087 /* Split line into variable name and parameters */
1088 ptr = strchr(str, '=');
1090 Config_Error(LOG_ERR, "%s, line %d: Syntax error!",
1091 NGIRCd_ConfFile, line);
1100 if (strcasecmp(section, "[GLOBAL]") == 0)
1101 Handle_GLOBAL(File, line, var, arg);
1102 else if (strcasecmp(section, "[LIMITS]") == 0)
1103 Handle_LIMITS(File, line, var, arg);
1104 else if (strcasecmp(section, "[OPTIONS]") == 0)
1105 Handle_OPTIONS(File, line, var, arg);
1107 else if (strcasecmp(section, "[SSL]") == 0)
1108 Handle_SSL(File, line, var, arg);
1110 else if (strcasecmp(section, "[OPERATOR]") == 0)
1111 Handle_OPERATOR(File, line, var, arg);
1112 else if (strcasecmp(section, "[SERVER]") == 0)
1113 Handle_SERVER(File, line, var, arg);
1114 else if (strcasecmp(section, "[CHANNEL]") == 0)
1115 Handle_CHANNEL(File, line, var, arg);
1117 Config_Error(LOG_ERR,
1118 "%s, line %d: Variable \"%s\" outside section!",
1119 NGIRCd_ConfFile, line, var);
1124 * Check whether a string argument is "true" or "false".
1126 * @param Arg Input string.
1127 * @returns true if the input string has been parsed as "yes", "true"
1128 * (case insensitive) or a non-zero integer value.
1131 Check_ArgIsTrue(const char *Arg)
1133 if (strcasecmp(Arg, "yes") == 0)
1135 if (strcasecmp(Arg, "true") == 0)
1144 * Handle setting of "MaxNickLength".
1146 * @param Line Line number in configuration file.
1147 * @raram Arg Input string.
1148 * @returns New configured maximum nickname length.
1151 Handle_MaxNickLength(int Line, const char *Arg)
1155 new = (unsigned) atoi(Arg) + 1;
1156 if (new > CLIENT_NICK_LEN) {
1157 Config_Error(LOG_WARNING,
1158 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
1159 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
1160 return CLIENT_NICK_LEN;
1163 Config_Error(LOG_WARNING,
1164 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
1165 NGIRCd_ConfFile, Line);
1172 * Output a warning messages if IDENT is configured but not compiled in.
1175 WarnIdent(int UNUSED Line)
1179 /* user has enabled ident lookups explicitly, but ... */
1180 Config_Error(LOG_WARNING,
1181 "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
1182 NGIRCd_ConfFile, Line);
1188 * Output a warning messages if IPv6 is configured but not compiled in.
1191 WarnIPv6(int UNUSED Line)
1194 if (Conf_ConnectIPv6) {
1195 /* user has enabled IPv6 explicitly, but ... */
1196 Config_Error(LOG_WARNING,
1197 "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
1198 NGIRCd_ConfFile, Line);
1204 * Output a warning messages if PAM is configured but not compiled in.
1207 WarnPAM(int UNUSED Line)
1211 Config_Error(LOG_WARNING,
1212 "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
1213 NGIRCd_ConfFile, Line);
1219 * Handle legacy "NoXXX" options in [GLOBAL] section.
1221 * TODO: This function and support for "NoXXX" could be removed starting
1222 * with ngIRCd release 19 (one release after marking it "deprecated").
1224 * @param Var Variable name.
1225 * @param Arg Argument string.
1226 * @returns true if a NoXXX option has been processed; false otherwise.
1229 CheckLegacyNoOption(const char *Var, const char *Arg)
1231 if(strcasecmp(Var, "NoDNS") == 0) {
1232 Conf_DNS = !Check_ArgIsTrue( Arg );
1235 if (strcasecmp(Var, "NoIdent") == 0) {
1236 Conf_Ident = !Check_ArgIsTrue(Arg);
1239 if(strcasecmp(Var, "NoPAM") == 0) {
1240 Conf_PAM = !Check_ArgIsTrue(Arg);
1247 * Handle deprecated legacy options in [GLOBAL] section.
1249 * TODO: This function and support for these options in the [Global] section
1250 * could be removed starting with ngIRCd release 19 (one release after
1251 * marking it "deprecated").
1253 * @param Var Variable name.
1254 * @param Arg Argument string.
1255 * @returns true if a legacy option has been processed; false otherwise.
1258 CheckLegacyGlobalOption(const char *File, int Line, char *Var, char *Arg)
1260 if (strcasecmp(Var, "AllowRemoteOper") == 0
1261 || strcasecmp(Var, "ChrootDir") == 0
1262 || strcasecmp(Var, "ConnectIPv4") == 0
1263 || strcasecmp(Var, "ConnectIPv6") == 0
1264 || strcasecmp(Var, "OperCanUseMode") == 0
1265 || strcasecmp(Var, "OperChanPAutoOp") == 0
1266 || strcasecmp(Var, "OperServerMode") == 0
1267 || strcasecmp(Var, "PredefChannelsOnly") == 0
1268 || strcasecmp(Var, "SyslogFacility") == 0
1269 || strcasecmp(Var, "WebircPassword") == 0) {
1270 Handle_OPTIONS(File, Line, Var, Arg);
1273 if (strcasecmp(Var, "ConnectRetry") == 0
1274 || strcasecmp(Var, "IdleTimeout") == 0
1275 || strcasecmp(Var, "MaxConnections") == 0
1276 || strcasecmp(Var, "MaxConnectionsIP") == 0
1277 || strcasecmp(Var, "MaxJoins") == 0
1278 || strcasecmp(Var, "MaxNickLength") == 0
1279 || strcasecmp(Var, "PingTimeout") == 0
1280 || strcasecmp(Var, "PongTimeout") == 0) {
1281 Handle_LIMITS(File, Line, Var, Arg);
1285 if (strcasecmp(Var, "SSLCertFile") == 0
1286 || strcasecmp(Var, "SSLDHFile") == 0
1287 || strcasecmp(Var, "SSLKeyFile") == 0
1288 || strcasecmp(Var, "SSLKeyFilePassword") == 0
1289 || strcasecmp(Var, "SSLPorts") == 0) {
1290 Handle_SSL(File, Line, Var + 3, Arg);
1299 * Strip "no" prefix of a string.
1301 * TODO: This function and support for "NoXXX" should be removed starting
1302 * with ngIRCd release 19! (One release after marking it "deprecated").
1304 * @param str Pointer to input string starting with "no".
1305 * @returns New pointer to string without "no" prefix.
1308 NoNo(const char *str)
1310 assert(strncasecmp("no", str, 2) == 0 && str[2]);
1315 * Invert "boolean" string.
1317 * TODO: This function and support for "NoXXX" should be removed starting
1318 * with ngIRCd release 19! (One release after marking it "deprecated").
1320 * @param arg "Boolean" input string.
1321 * @returns Pointer to inverted "boolean string".
1324 InvertArg(const char *arg)
1326 return yesno_to_str(!Check_ArgIsTrue(arg));
1330 * Handle variable in [Global] configuration section.
1332 * @param Line Line numer in configuration file.
1333 * @param Var Variable name.
1334 * @param Arg Variable argument.
1337 Handle_GLOBAL(const char *File, int Line, char *Var, char *Arg )
1342 const char *section;
1344 assert(File != NULL);
1346 assert(Var != NULL);
1347 assert(Arg != NULL);
1349 if (strcasecmp(Var, "Name") == 0) {
1350 len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
1351 if (len >= sizeof(Conf_ServerName))
1352 Config_Error_TooLong(File, Line, Var);
1355 if (strcasecmp(Var, "AdminInfo1") == 0) {
1356 len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
1357 if (len >= sizeof(Conf_ServerAdmin1))
1358 Config_Error_TooLong(File, Line, Var);
1361 if (strcasecmp(Var, "AdminInfo2") == 0) {
1362 len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
1363 if (len >= sizeof(Conf_ServerAdmin2))
1364 Config_Error_TooLong(File, Line, Var);
1367 if (strcasecmp(Var, "AdminEMail") == 0) {
1368 len = strlcpy(Conf_ServerAdminMail, Arg,
1369 sizeof(Conf_ServerAdminMail));
1370 if (len >= sizeof(Conf_ServerAdminMail))
1371 Config_Error_TooLong(File, Line, Var);
1374 if (strcasecmp(Var, "Info") == 0) {
1375 len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
1376 if (len >= sizeof(Conf_ServerInfo))
1377 Config_Error_TooLong(File, Line, Var);
1380 if (strcasecmp(Var, "HelpFile") == 0) {
1381 len = strlcpy(Conf_HelpFile, Arg, sizeof(Conf_HelpFile));
1382 if (len >= sizeof(Conf_HelpFile))
1383 Config_Error_TooLong(File, Line, Var);
1386 if (strcasecmp(Var, "Listen") == 0) {
1387 if (Conf_ListenAddress) {
1388 Config_Error(LOG_ERR,
1389 "Multiple Listen= options, ignoring: %s",
1393 Conf_ListenAddress = strdup_warn(Arg);
1394 /* If allocation fails, we're in trouble: we cannot ignore the
1395 * error -- otherwise ngircd would listen on all interfaces. */
1396 if (!Conf_ListenAddress) {
1397 Config_Error(LOG_ALERT,
1398 "%s exiting due to fatal errors!",
1404 if (strcasecmp(Var, "MotdFile") == 0) {
1405 len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
1406 if (len >= sizeof(Conf_MotdFile))
1407 Config_Error_TooLong(File, Line, Var);
1410 if (strcasecmp(Var, "MotdPhrase") == 0) {
1414 if (len >= LINE_LEN) {
1415 Config_Error_TooLong(File, Line, Var);
1418 if (!array_copyb(&Conf_Motd, Arg, len + 1))
1419 Config_Error(LOG_WARNING,
1420 "%s, line %d: Could not append MotdPhrase: %s",
1421 NGIRCd_ConfFile, Line, strerror(errno));
1422 Using_MotdFile = false;
1425 if(strcasecmp(Var, "Password") == 0) {
1426 len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
1427 if (len >= sizeof(Conf_ServerPwd))
1428 Config_Error_TooLong(File, Line, Var);
1431 if (strcasecmp(Var, "PidFile") == 0) {
1432 len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
1433 if (len >= sizeof(Conf_PidFile))
1434 Config_Error_TooLong(File, Line, Var);
1437 if (strcasecmp(Var, "Ports") == 0) {
1438 ports_parse(&Conf_ListenPorts, Line, Arg);
1441 if (strcasecmp(Var, "ServerGID") == 0) {
1442 grp = getgrnam(Arg);
1444 Conf_GID = grp->gr_gid;
1446 Conf_GID = (unsigned int)atoi(Arg);
1447 if (!Conf_GID && strcmp(Arg, "0"))
1448 Config_Error(LOG_WARNING,
1449 "%s, line %d: Value of \"%s\" is not a valid group name or ID!",
1450 NGIRCd_ConfFile, Line, Var);
1454 if (strcasecmp(Var, "ServerUID") == 0) {
1455 pwd = getpwnam(Arg);
1457 Conf_UID = pwd->pw_uid;
1459 Conf_UID = (unsigned int)atoi(Arg);
1460 if (!Conf_UID && strcmp(Arg, "0"))
1461 Config_Error(LOG_WARNING,
1462 "%s, line %d: Value of \"%s\" is not a valid user name or ID!",
1463 NGIRCd_ConfFile, Line, Var);
1468 if (CheckLegacyNoOption(Var, Arg)) {
1469 /* TODO: This function and support for "NoXXX" could be
1470 * be removed starting with ngIRCd release 19 (one release
1471 * after marking it "deprecated"). */
1472 Config_Error(LOG_WARNING,
1473 "%s, line %d (section \"Global\"): \"No\"-Prefix is deprecated, use \"%s = %s\" in [Options] section!",
1474 NGIRCd_ConfFile, Line, NoNo(Var), InvertArg(Arg));
1475 if (strcasecmp(Var, "NoIdent") == 0)
1477 else if (strcasecmp(Var, "NoPam") == 0)
1481 if ((section = CheckLegacyGlobalOption(File, Line, Var, Arg))) {
1482 /** TODO: This function and support for these options in the
1483 * [Global] section could be removed starting with ngIRCd
1484 * release 19 (one release after marking it "deprecated"). */
1485 if (strncasecmp(Var, "SSL", 3) == 0) {
1486 Config_Error(LOG_WARNING,
1487 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s and rename to \"%s\"!",
1488 NGIRCd_ConfFile, Line, Var, section,
1491 Config_Error(LOG_WARNING,
1492 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s!",
1493 NGIRCd_ConfFile, Line, Var, section);
1498 Config_Error_Section(File, Line, Var, "Global");
1502 * Handle variable in [Limits] configuration section.
1504 * @param Line Line numer in configuration file.
1505 * @param Var Variable name.
1506 * @param Arg Variable argument.
1509 Handle_LIMITS(const char *File, int Line, char *Var, char *Arg)
1511 assert(File != NULL);
1513 assert(Var != NULL);
1514 assert(Arg != NULL);
1516 if (strcasecmp(Var, "ConnectRetry") == 0) {
1517 Conf_ConnectRetry = atoi(Arg);
1518 if (Conf_ConnectRetry < 5) {
1519 Config_Error(LOG_WARNING,
1520 "%s, line %d: Value of \"ConnectRetry\" too low!",
1521 NGIRCd_ConfFile, Line);
1522 Conf_ConnectRetry = 5;
1526 if (strcasecmp(Var, "IdleTimeout") == 0) {
1527 Conf_IdleTimeout = atoi(Arg);
1528 if (!Conf_IdleTimeout && strcmp(Arg, "0"))
1529 Config_Error_NaN(File, Line, Var);
1532 if (strcasecmp(Var, "MaxConnections") == 0) {
1533 Conf_MaxConnections = atoi(Arg);
1534 if (!Conf_MaxConnections && strcmp(Arg, "0"))
1535 Config_Error_NaN(File, Line, Var);
1538 if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
1539 Conf_MaxConnectionsIP = atoi(Arg);
1540 if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
1541 Config_Error_NaN(File, Line, Var);
1544 if (strcasecmp(Var, "MaxJoins") == 0) {
1545 Conf_MaxJoins = atoi(Arg);
1546 if (!Conf_MaxJoins && strcmp(Arg, "0"))
1547 Config_Error_NaN(File, Line, Var);
1550 if (strcasecmp(Var, "MaxNickLength") == 0) {
1551 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1554 if (strcasecmp(Var, "MaxListSize") == 0) {
1555 Conf_MaxListSize = atoi(Arg);
1556 if (!Conf_MaxListSize && strcmp(Arg, "0"))
1557 Config_Error_NaN(File, Line, Var);
1560 if (strcasecmp(Var, "PingTimeout") == 0) {
1561 Conf_PingTimeout = atoi(Arg);
1562 if (Conf_PingTimeout < 5) {
1563 Config_Error(LOG_WARNING,
1564 "%s, line %d: Value of \"PingTimeout\" too low!",
1565 NGIRCd_ConfFile, Line);
1566 Conf_PingTimeout = 5;
1570 if (strcasecmp(Var, "PongTimeout") == 0) {
1571 Conf_PongTimeout = atoi(Arg);
1572 if (Conf_PongTimeout < 5) {
1573 Config_Error(LOG_WARNING,
1574 "%s, line %d: Value of \"PongTimeout\" too low!",
1575 NGIRCd_ConfFile, Line);
1576 Conf_PongTimeout = 5;
1581 Config_Error_Section(File, Line, Var, "Limits");
1585 * Handle variable in [Options] configuration section.
1587 * @param Line Line numer in configuration file.
1588 * @param Var Variable name.
1589 * @param Arg Variable argument.
1592 Handle_OPTIONS(const char *File, int Line, char *Var, char *Arg)
1596 assert(File != NULL);
1598 assert(Var != NULL);
1599 assert(Arg != NULL);
1601 if (strcasecmp(Var, "AllowRemoteOper") == 0) {
1602 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1605 if (strcasecmp(Var, "ChrootDir") == 0) {
1606 len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
1607 if (len >= sizeof(Conf_Chroot))
1608 Config_Error_TooLong(File, Line, Var);
1611 if (strcasecmp(Var, "CloakHost") == 0) {
1612 len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
1613 if (len >= sizeof(Conf_CloakHost))
1614 Config_Error_TooLong(File, Line, Var);
1617 if (strcasecmp(Var, "CloakHostModeX") == 0) {
1618 len = strlcpy(Conf_CloakHostModeX, Arg, sizeof(Conf_CloakHostModeX));
1619 if (len >= sizeof(Conf_CloakHostModeX))
1620 Config_Error_TooLong(File, Line, Var);
1623 if (strcasecmp(Var, "CloakHostSalt") == 0) {
1624 len = strlcpy(Conf_CloakHostSalt, Arg, sizeof(Conf_CloakHostSalt));
1625 if (len >= sizeof(Conf_CloakHostSalt))
1626 Config_Error_TooLong(File, Line, Var);
1629 if (strcasecmp(Var, "CloakUserToNick") == 0) {
1630 Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
1633 if (strcasecmp(Var, "ConnectIPv6") == 0) {
1634 Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
1638 if (strcasecmp(Var, "ConnectIPv4") == 0) {
1639 Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
1642 if (strcasecmp(Var, "DNS") == 0) {
1643 Conf_DNS = Check_ArgIsTrue(Arg);
1646 if (strcasecmp(Var, "Ident") == 0) {
1647 Conf_Ident = Check_ArgIsTrue(Arg);
1651 if (strcasecmp(Var, "MorePrivacy") == 0) {
1652 Conf_MorePrivacy = Check_ArgIsTrue(Arg);
1655 if (strcasecmp(Var, "NoticeAuth") == 0) {
1656 Conf_NoticeAuth = Check_ArgIsTrue(Arg);
1659 if (strcasecmp(Var, "OperCanUseMode") == 0) {
1660 Conf_OperCanMode = Check_ArgIsTrue(Arg);
1663 if (strcasecmp(Var, "OperChanPAutoOp") == 0) {
1664 Conf_OperChanPAutoOp = Check_ArgIsTrue(Arg);
1667 if (strcasecmp(Var, "OperServerMode") == 0) {
1668 Conf_OperServerMode = Check_ArgIsTrue(Arg);
1671 if (strcasecmp(Var, "PAM") == 0) {
1672 Conf_PAM = Check_ArgIsTrue(Arg);
1676 if (strcasecmp(Var, "PAMIsOptional") == 0 ) {
1677 Conf_PAMIsOptional = Check_ArgIsTrue(Arg);
1680 if (strcasecmp(Var, "PredefChannelsOnly") == 0) {
1681 Conf_PredefChannelsOnly = Check_ArgIsTrue(Arg);
1685 if (strcasecmp(Var, "RequireAuthPing") == 0) {
1686 Conf_AuthPing = Check_ArgIsTrue(Arg);
1690 if (strcasecmp(Var, "ScrubCTCP") == 0) {
1691 Conf_ScrubCTCP = Check_ArgIsTrue(Arg);
1695 if (strcasecmp(Var, "SyslogFacility") == 0) {
1696 Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
1697 Conf_SyslogFacility);
1701 if (strcasecmp(Var, "WebircPassword") == 0) {
1702 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
1703 if (len >= sizeof(Conf_WebircPwd))
1704 Config_Error_TooLong(File, Line, Var);
1708 Config_Error_Section(File, Line, Var, "Options");
1714 * Handle variable in [SSL] configuration section.
1716 * @param Line Line numer in configuration file.
1717 * @param Var Variable name.
1718 * @param Arg Variable argument.
1721 Handle_SSL(const char *File, int Line, char *Var, char *Arg)
1723 assert(File != NULL);
1725 assert(Var != NULL);
1726 assert(Arg != NULL);
1728 if (strcasecmp(Var, "CertFile") == 0) {
1729 assert(Conf_SSLOptions.CertFile == NULL);
1730 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1733 if (strcasecmp(Var, "DHFile") == 0) {
1734 assert(Conf_SSLOptions.DHFile == NULL);
1735 Conf_SSLOptions.DHFile = strdup_warn(Arg);
1738 if (strcasecmp(Var, "KeyFile") == 0) {
1739 assert(Conf_SSLOptions.KeyFile == NULL);
1740 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1743 if (strcasecmp(Var, "KeyFilePassword") == 0) {
1744 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1745 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1746 Config_Error(LOG_ERR,
1747 "%s, line %d (section \"SSL\"): Could not copy %s: %s!",
1748 File, Line, Var, strerror(errno));
1751 if (strcasecmp(Var, "Ports") == 0) {
1752 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1756 Config_Error_Section(File, Line, Var, "SSL");
1762 * Handle variable in [Operator] configuration section.
1764 * @param Line Line numer in configuration file.
1765 * @param Var Variable name.
1766 * @param Arg Variable argument.
1769 Handle_OPERATOR(const char *File, int Line, char *Var, char *Arg )
1772 struct Conf_Oper *op;
1774 assert( File != NULL );
1776 assert( Var != NULL );
1777 assert( Arg != NULL );
1779 op = array_get(&Conf_Opers, sizeof(*op),
1780 array_length(&Conf_Opers, sizeof(*op)) - 1);
1784 if (strcasecmp(Var, "Name") == 0) {
1785 /* Name of IRC operator */
1786 len = strlcpy(op->name, Arg, sizeof(op->name));
1787 if (len >= sizeof(op->name))
1788 Config_Error_TooLong(File, Line, Var);
1791 if (strcasecmp(Var, "Password") == 0) {
1792 /* Password of IRC operator */
1793 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1794 if (len >= sizeof(op->pwd))
1795 Config_Error_TooLong(File, Line, Var);
1798 if (strcasecmp(Var, "Mask") == 0) {
1800 return; /* Hostname already configured */
1801 op->mask = strdup_warn( Arg );
1805 Config_Error_Section(File, Line, Var, "Operator");
1809 * Handle variable in [Server] configuration section.
1811 * @param Line Line numer in configuration file.
1812 * @param Var Variable name.
1813 * @param Arg Variable argument.
1816 Handle_SERVER(const char *File, int Line, char *Var, char *Arg )
1821 assert( File != NULL );
1823 assert( Var != NULL );
1824 assert( Arg != NULL );
1826 /* Ignore server block if no space is left in server configuration structure */
1827 if( New_Server_Idx <= NONE ) return;
1829 if( strcasecmp( Var, "Host" ) == 0 ) {
1830 /* Hostname of the server */
1831 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1832 if (len >= sizeof( New_Server.host ))
1833 Config_Error_TooLong(File, Line, Var);
1836 if( strcasecmp( Var, "Name" ) == 0 ) {
1837 /* Name of the server ("Nick"/"ID") */
1838 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1839 if (len >= sizeof( New_Server.name ))
1840 Config_Error_TooLong(File, Line, Var);
1843 if (strcasecmp(Var, "Bind") == 0) {
1844 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1847 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1848 NGIRCd_ConfFile, Line, Arg);
1851 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1852 /* Password of this server which is sent to the peer */
1854 Config_Error(LOG_ERR,
1855 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1856 NGIRCd_ConfFile, Line);
1858 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1859 if (len >= sizeof( New_Server.pwd_in ))
1860 Config_Error_TooLong(File, Line, Var);
1863 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1864 /* Passwort of the peer which must be received */
1865 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1866 if (len >= sizeof( New_Server.pwd_out ))
1867 Config_Error_TooLong(File, Line, Var);
1870 if( strcasecmp( Var, "Port" ) == 0 ) {
1871 /* Port to which this server should connect */
1873 if (port >= 0 && port < 0xFFFF)
1874 New_Server.port = (UINT16)port;
1876 Config_Error(LOG_ERR,
1877 "%s, line %d (section \"Server\"): Illegal port number %ld!",
1878 NGIRCd_ConfFile, Line, port );
1882 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1883 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1887 if( strcasecmp( Var, "Group" ) == 0 ) {
1889 New_Server.group = atoi( Arg );
1890 if (!New_Server.group && strcmp(Arg, "0"))
1891 Config_Error_NaN(File, Line, Var);
1894 if( strcasecmp( Var, "Passive" ) == 0 ) {
1895 if (Check_ArgIsTrue(Arg))
1896 New_Server.flags |= CONF_SFLAG_DISABLED;
1899 if (strcasecmp(Var, "ServiceMask") == 0) {
1900 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1901 sizeof(New_Server.svs_mask));
1902 if (len >= sizeof(New_Server.svs_mask))
1903 Config_Error_TooLong(File, Line, Var);
1907 Config_Error_Section(File, Line, Var, "Server");
1911 * Copy channel name into channel structure.
1913 * If the channel name is not valid because of a missing prefix ('#', '&'),
1914 * a default prefix of '#' will be added.
1916 * @param new_chan New already allocated channel structure.
1917 * @param name Name of the new channel.
1918 * @returns true on success, false otherwise.
1921 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1923 size_t size = sizeof(new_chan->name);
1924 char *dest = new_chan->name;
1926 if (!Channel_IsValidName(name)) {
1928 * maybe user forgot to add a '#'.
1929 * This is only here for user convenience.
1935 return size > strlcpy(dest, name, size);
1939 * Handle variable in [Channel] configuration section.
1941 * @param Line Line numer in configuration file.
1942 * @param Var Variable name.
1943 * @param Arg Variable argument.
1946 Handle_CHANNEL(const char *File, int Line, char *Var, char *Arg)
1949 struct Conf_Channel *chan;
1951 assert( File != NULL );
1953 assert( Var != NULL );
1954 assert( Arg != NULL );
1956 chan = array_get(&Conf_Channels, sizeof(*chan),
1957 array_length(&Conf_Channels, sizeof(*chan)) - 1);
1961 if (strcasecmp(Var, "Name") == 0) {
1962 if (!Handle_Channelname(chan, Arg))
1963 Config_Error_TooLong(File, Line, Var);
1966 if (strcasecmp(Var, "Modes") == 0) {
1968 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1969 if (len >= sizeof(chan->modes))
1970 Config_Error_TooLong(File, Line, Var);
1973 if( strcasecmp( Var, "Topic" ) == 0 ) {
1975 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1976 if (len >= sizeof(chan->topic))
1977 Config_Error_TooLong(File, Line, Var);
1980 if( strcasecmp( Var, "Key" ) == 0 ) {
1981 /* Initial Channel Key (mode k) */
1982 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1983 if (len >= sizeof(chan->key))
1984 Config_Error_TooLong(File, Line, Var);
1987 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1988 /* maximum user limit, mode l */
1989 chan->maxusers = (unsigned long) atol(Arg);
1990 if (!chan->maxusers && strcmp(Arg, "0"))
1991 Config_Error_NaN(File, Line, Var);
1994 if (strcasecmp(Var, "KeyFile") == 0) {
1996 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1997 if (len >= sizeof(chan->keyfile))
1998 Config_Error_TooLong(File, Line, Var);
2002 Config_Error_Section(File, Line, Var, "Channel");
2006 * Validate server configuration.
2008 * Please note that this function uses exit(1) on fatal errors and therefore
2009 * can result in ngIRCd terminating!
2011 * @param Configtest true if the daemon has been called with "--configtest".
2012 * @param Rehash true if re-reading configuration on runtime.
2013 * @returns true if configuration is valid.
2016 Validate_Config(bool Configtest, bool Rehash)
2018 /* Validate configuration settings. */
2021 int i, servers, servers_once;
2023 bool config_valid = true;
2026 /* Emit a warning when the config file is not a full path name */
2027 if (NGIRCd_ConfFile[0] && NGIRCd_ConfFile[0] != '/') {
2028 Config_Error(LOG_WARNING,
2029 "Not specifying a full path name to \"%s\" can cause problems when rehashing the server!",
2033 /* Validate configured server name, see RFC 2812 section 2.3.1 */
2034 ptr = Conf_ServerName;
2036 if (*ptr >= 'a' && *ptr <= 'z') continue;
2037 if (*ptr >= 'A' && *ptr <= 'Z') continue;
2038 if (*ptr >= '0' && *ptr <= '9') continue;
2039 if (ptr > Conf_ServerName) {
2040 if (*ptr == '.' || *ptr == '-')
2043 Conf_ServerName[0] = '\0';
2047 if (!Conf_ServerName[0]) {
2048 /* No server name configured! */
2049 config_valid = false;
2050 Config_Error(LOG_ALERT,
2051 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
2053 if (!Configtest && !Rehash) {
2054 Config_Error(LOG_ALERT,
2055 "%s exiting due to fatal errors!",
2061 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
2062 /* No dot in server name! */
2063 config_valid = false;
2064 Config_Error(LOG_ALERT,
2065 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
2068 Config_Error(LOG_ALERT,
2069 "%s exiting due to fatal errors!",
2076 if (!Conf_ServerAdminMail[0]) {
2077 /* No administrative contact configured! */
2078 config_valid = false;
2079 Config_Error(LOG_ALERT,
2080 "No administrator email address configured in \"%s\" ('AdminEMail')!",
2083 Config_Error(LOG_ALERT,
2084 "%s exiting due to fatal errors!",
2091 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
2092 && !Conf_ServerAdminMail[0]) {
2093 /* No administrative information configured! */
2094 Config_Error(LOG_WARNING,
2095 "No administrative information configured but required by RFC!");
2099 if (Conf_ServerPwd[0])
2100 Config_Error(LOG_ERR,
2101 "This server uses PAM, \"Password\" in [Global] section will be ignored!");
2105 servers = servers_once = 0;
2106 for (i = 0; i < MAX_SERVERS; i++) {
2107 if (Conf_Server[i].name[0]) {
2109 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
2114 "Configuration: Operators=%ld, Servers=%d[%d], Channels=%ld",
2115 array_length(&Conf_Opers, sizeof(struct Conf_Oper)),
2116 servers, servers_once,
2117 array_length(&Conf_Channels, sizeof(struct Conf_Channel)));
2120 return config_valid;
2124 * Output "line too long" warning.
2126 * @param Line Line number in configuration file.
2127 * @param Item Affected variable name.
2130 Config_Error_TooLong(const char *File, const int Line, const char *Item)
2132 Config_Error(LOG_WARNING, "%s, line %d: Value of \"%s\" too long!",
2137 * Output "unknown variable" warning.
2139 * @param Line Line number in configuration file.
2140 * @param Item Affected variable name.
2141 * @param Section Section name.
2144 Config_Error_Section(const char *File, const int Line, const char *Item,
2145 const char *Section)
2147 Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
2148 File, Line, Section, Item);
2152 * Output "not a number" warning.
2154 * @param Line Line number in configuration file.
2155 * @param Item Affected variable name.
2158 Config_Error_NaN(const char *File, const int Line, const char *Item )
2160 Config_Error(LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
2165 * Output configuration error to console and/or logfile.
2167 * On runtime, the normal log functions of the daemon are used. But when
2168 * testing the configuration ("--configtest"), all messages go directly
2171 * @param Level Severity level of the message.
2172 * @param Format Format string; see printf() function.
2175 static void Config_Error( const int Level, const char *Format, ... )
2177 static void Config_Error( Level, Format, va_alist )
2183 char msg[MAX_LOG_MSG_LEN];
2186 assert( Format != NULL );
2189 va_start( ap, Format );
2193 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
2197 if (Level <= LOG_WARNING)
2198 printf(" - %s\n", msg);
2202 Log(Level, "%s", msg);
2208 * Dump internal state of the "configuration module".
2211 Conf_DebugDump(void)
2215 Log(LOG_DEBUG, "Configured servers:");
2216 for (i = 0; i < MAX_SERVERS; i++) {
2217 if (! Conf_Server[i].name[0])
2220 " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
2221 Conf_Server[i].name, Conf_Server[i].host,
2222 Conf_Server[i].port, Conf_Server[i].lasttry,
2223 Conf_Server[i].group, Conf_Server[i].flags,
2224 Conf_Server[i].conn_id);
2231 * Initialize server configuration structur to default values.
2233 * @param Server Pointer to server structure to initialize.
2236 Init_Server_Struct( CONF_SERVER *Server )
2238 assert( Server != NULL );
2240 memset( Server, 0, sizeof (CONF_SERVER) );
2242 Server->group = NONE;
2243 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
2245 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
2247 Proc_InitStruct(&Server->res_stat);
2248 Server->conn_id = NONE;
2249 memset(&Server->bind_addr, 0, sizeof(Server->bind_addr));