2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2011 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)
34 #include <sys/types.h>
54 static bool Use_Log = true, Using_MotdFile = true;
55 static CONF_SERVER New_Server;
56 static int New_Server_Idx;
58 static size_t Conf_Oper_Count;
59 static size_t Conf_Channel_Count;
60 static char Conf_MotdFile[FNAME_LEN];
62 static void Set_Defaults PARAMS(( bool InitServers ));
63 static bool Read_Config PARAMS(( bool ngircd_starting ));
64 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
66 static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
67 static void Handle_LIMITS PARAMS(( int Line, char *Var, char *Arg ));
68 static void Handle_OPTIONS PARAMS(( int Line, char *Var, char *Arg ));
69 static void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
70 static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
71 static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
73 static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
75 static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
76 static void Config_Error_Section PARAMS(( const int Line, const char *Item,
77 const char *Section ));
78 static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
80 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
84 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
86 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
92 static void Handle_SSL PARAMS(( int Line, char *Var, char *Ark ));
94 struct SSLOptions Conf_SSLOptions;
97 * Initialize SSL configuration.
102 free(Conf_SSLOptions.KeyFile);
103 Conf_SSLOptions.KeyFile = NULL;
105 free(Conf_SSLOptions.CertFile);
106 Conf_SSLOptions.CertFile = NULL;
108 free(Conf_SSLOptions.DHFile);
109 Conf_SSLOptions.DHFile = NULL;
110 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
114 * Make sure that a configured file is readable.
116 * Currently, this function is only used for SSL-related options ...
118 * @param Var Configuration variable
119 * @param Filename Configured filename
122 CheckFileReadable(const char *Var, const char *Filename)
129 fp = fopen(Filename, "r");
133 Config_Error(LOG_ERR, "Can't read \"%s\" (\"%s\"): %s",
134 Filename, Var, strerror(errno));
141 * Duplicate string and warn on errors.
143 * @returns Pointer to string on success, NULL otherwise.
146 strdup_warn(const char *str)
148 char *ptr = strdup(str);
150 Config_Error(LOG_ERR,
151 "Could not allocate memory for string: %s", str);
156 * Output a comma separated list of ports (integer values).
163 len = array_length(a, sizeof(UINT16));
165 ports = (UINT16*) array_start(a);
166 printf("%u", (unsigned int) *ports);
169 printf(", %u", (unsigned int) *ports);
176 * Parse a comma separated string into an array of port numbers (integers).
179 ports_parse(array *a, int Line, char *Arg)
187 ptr = strtok( Arg, "," );
191 if (port > 0 && port < 0xFFFF) {
192 port16 = (UINT16) port;
193 if (!array_catb(a, (char*)&port16, sizeof port16))
194 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
195 NGIRCd_ConfFile, Line, port, strerror(errno));
197 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
198 NGIRCd_ConfFile, Line, port );
201 ptr = strtok( NULL, "," );
206 * Initialize configuration module.
212 Validate_Config(false, false);
216 * "Rehash" (reload) server configuration.
218 * @returns true if configuration has been re-read, false on errors.
223 if (!Read_Config(false))
225 Validate_Config(false, true);
227 /* Update CLIENT structure of local server */
228 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
233 * Output a boolean value as "yes/no" string.
236 yesno_to_str(int boolean_value)
244 * Free all IRC operator configuration structures.
249 struct Conf_Oper *op;
252 len = array_length(&Conf_Opers, sizeof(*op));
253 op = array_start(&Conf_Opers);
258 array_free(&Conf_Opers);
262 * Output all IRC operator configuration structures.
267 struct Conf_Oper *op;
270 len = array_length(&Conf_Opers, sizeof(*op));
271 op = array_start(&Conf_Opers);
276 printf(" Name = %s\n", op->name);
277 printf(" Password = %s\n", op->pwd);
278 printf(" Mask = %s\n\n", op->mask ? op->mask : "");
284 * Read configuration, validate and output it.
286 * This function waits for a keypress of the user when stdin/stdout are valid
287 * tty's ("you can read our nice message and we can read in your keypress").
289 * @return 0 on succes, 1 on failure(s); therefore the result code can
290 * directly be used by exit() when running "ngircd --configtest".
299 size_t predef_channel_count;
300 struct Conf_Channel *predef_chan;
304 if (! Read_Config(true))
307 config_valid = Validate_Config(true, false);
310 if(isatty(fileno(stdin)) && isatty(fileno(stdout))) {
311 puts("OK, press enter to see a dump of your server configuration ...");
314 puts("Ok, dump of your server configuration follows:\n");
317 printf(" Name = %s\n", Conf_ServerName);
318 printf(" AdminInfo1 = %s\n", Conf_ServerAdmin1);
319 printf(" AdminInfo2 = %s\n", Conf_ServerAdmin2);
320 printf(" AdminEMail = %s\n", Conf_ServerAdminMail);
321 printf(" Info = %s\n", Conf_ServerInfo);
322 printf(" Listen = %s\n", Conf_ListenAddress);
323 if (Using_MotdFile) {
324 printf(" MotdFile = %s\n", Conf_MotdFile);
325 printf(" MotdPhrase =\n");
327 printf(" MotdFile = \n");
328 printf(" MotdPhrase = %s\n", array_bytes(&Conf_Motd)
329 ? (const char*) array_start(&Conf_Motd) : "");
332 printf(" Password = %s\n", Conf_ServerPwd);
334 printf(" PidFile = %s\n", Conf_PidFile);
336 ports_puts(&Conf_ListenPorts);
337 grp = getgrgid(Conf_GID);
339 printf(" ServerGID = %s\n", grp->gr_name);
341 printf(" ServerGID = %ld\n", (long)Conf_GID);
342 pwd = getpwuid(Conf_UID);
344 printf(" ServerUID = %s\n", pwd->pw_name);
346 printf(" ServerUID = %ld\n", (long)Conf_UID);
350 printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
351 printf(" MaxConnections = %ld\n", Conf_MaxConnections);
352 printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
353 printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
354 printf(" MaxNickLength = %u\n", Conf_MaxNickLength - 1);
355 printf(" PingTimeout = %d\n", Conf_PingTimeout);
356 printf(" PongTimeout = %d\n", Conf_PongTimeout);
360 printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
361 printf(" ChrootDir = %s\n", Conf_Chroot);
362 printf(" CloakHost = %s\n", Conf_CloakHost);
363 printf(" CloakUserToNick = %s\n", yesno_to_str(Conf_CloakUserToNick));
365 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
366 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
368 printf(" DNS = %s\n", yesno_to_str(Conf_DNS));
370 printf(" Ident = %s\n", yesno_to_str(Conf_Ident));
372 printf(" MorePrivacy = %s\n", yesno_to_str(Conf_MorePrivacy));
373 printf(" NoticeAuth = %s\n", yesno_to_str(Conf_NoticeAuth));
374 printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
375 printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
377 printf(" PAM = %s\n", yesno_to_str(Conf_PAM));
379 printf(" PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
381 printf(" RequireAuthPing = %s\n", yesno_to_str(Conf_AuthPing));
383 printf(" ScrubCTCP = %s\n", yesno_to_str(Conf_ScrubCTCP));
385 printf(" SyslogFacility = %s\n",
386 ngt_SyslogFacilityName(Conf_SyslogFacility));
388 printf(" WebircPassword = %s\n", Conf_WebircPwd);
393 printf(" CertFile = %s\n", Conf_SSLOptions.CertFile
394 ? Conf_SSLOptions.CertFile : "");
395 printf(" DHFile = %s\n", Conf_SSLOptions.DHFile
396 ? Conf_SSLOptions.DHFile : "");
397 printf(" KeyFile = %s\n", Conf_SSLOptions.KeyFile
398 ? Conf_SSLOptions.KeyFile : "");
399 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
400 puts(" KeyFilePassword = <secret>");
402 puts(" KeyFilePassword = ");
403 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
405 ports_puts(&Conf_SSLOptions.ListenPorts);
411 for( i = 0; i < MAX_SERVERS; i++ ) {
412 if( ! Conf_Server[i].name[0] ) continue;
414 /* Valid "Server" section */
416 printf( " Name = %s\n", Conf_Server[i].name );
417 printf( " Host = %s\n", Conf_Server[i].host );
418 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
420 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
422 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
423 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
424 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
425 printf( " Group = %d\n", Conf_Server[i].group );
426 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
429 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
430 predef_chan = array_start(&Conf_Channels);
432 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
433 if (!predef_chan->name[0])
436 /* Valid "Channel" section */
438 printf(" Name = %s\n", predef_chan->name);
439 printf(" Modes = %s\n", predef_chan->modes);
440 printf(" Key = %s\n", predef_chan->key);
441 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
442 printf(" Topic = %s\n", predef_chan->topic);
443 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
446 return (config_valid ? 0 : 1);
450 * Remove connection information from configured server.
452 * If the server is set as "once", delete it from our configuration;
453 * otherwise set the time for the next connection attempt.
455 * Non-server connections will be silently ignored.
458 Conf_UnsetServer( CONN_ID Idx )
463 /* Check all our configured servers */
464 for( i = 0; i < MAX_SERVERS; i++ ) {
465 if( Conf_Server[i].conn_id != Idx ) continue;
467 /* Gotcha! Mark server configuration as "unused": */
468 Conf_Server[i].conn_id = NONE;
470 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
471 /* Delete configuration here */
472 Init_Server_Struct( &Conf_Server[i] );
474 /* Set time for next connect attempt */
476 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
477 /* The connection has been "long", so we don't
478 * require the next attempt to be delayed. */
479 Conf_Server[i].lasttry =
480 t - Conf_ConnectRetry + RECONNECT_DELAY;
482 Conf_Server[i].lasttry = t;
488 * Set connection information for specified configured server.
491 Conf_SetServer( int ConfServer, CONN_ID Idx )
493 assert( ConfServer > NONE );
494 assert( Idx > NONE );
496 Conf_Server[ConfServer].conn_id = Idx;
500 * Get index of server in configuration structure.
503 Conf_GetServer( CONN_ID Idx )
507 assert( Idx > NONE );
509 for( i = 0; i < MAX_SERVERS; i++ ) {
510 if( Conf_Server[i].conn_id == Idx ) return i;
516 * Enable a server by name and adjust its port number.
518 * @returns true if a server has been enabled and now has a valid port
519 * number and host name for outgoing connections.
522 Conf_EnableServer( const char *Name, UINT16 Port )
526 assert( Name != NULL );
527 for( i = 0; i < MAX_SERVERS; i++ ) {
528 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
529 /* Gotcha! Set port and enable server: */
530 Conf_Server[i].port = Port;
531 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
532 return (Conf_Server[i].port && Conf_Server[i].host[0]);
539 * Enable a server by name.
541 * The server is only usable as outgoing server, if it has set a valid port
542 * number for outgoing connections!
543 * If not, you have to use Conf_EnableServer() function to make it available.
545 * @returns true if a server has been enabled; false otherwise.
548 Conf_EnablePassiveServer(const char *Name)
552 assert( Name != NULL );
553 for (i = 0; i < MAX_SERVERS; i++) {
554 if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
555 && (Conf_Server[i].port > 0)) {
556 /* BINGO! Enable server */
557 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
565 * Disable a server by name.
566 * An already established connection will be disconnected.
568 * @returns true if a server was found and has been disabled.
571 Conf_DisableServer( const char *Name )
575 assert( Name != NULL );
576 for( i = 0; i < MAX_SERVERS; i++ ) {
577 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
578 /* Gotcha! Disable and disconnect server: */
579 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
580 if( Conf_Server[i].conn_id > NONE )
581 Conn_Close(Conf_Server[i].conn_id, NULL,
582 "Server link terminated on operator request",
591 * Add a new remote server to our configuration.
593 * @param Name Name of the new server.
594 * @param Port Port number to connect to or 0 for incoming connections.
595 * @param Host Host name to connect to.
596 * @param MyPwd Password that will be sent to the peer.
597 * @param PeerPwd Password that must be received from the peer.
598 * @returns true if the new server has been added; false otherwise.
601 Conf_AddServer(const char *Name, UINT16 Port, const char *Host,
602 const char *MyPwd, const char *PeerPwd)
606 assert( Name != NULL );
607 assert( Host != NULL );
608 assert( MyPwd != NULL );
609 assert( PeerPwd != NULL );
611 /* Search unused item in server configuration structure */
612 for( i = 0; i < MAX_SERVERS; i++ ) {
613 /* Is this item used? */
614 if( ! Conf_Server[i].name[0] ) break;
616 if( i >= MAX_SERVERS ) return false;
618 Init_Server_Struct( &Conf_Server[i] );
619 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
620 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
621 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
622 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
623 Conf_Server[i].port = Port;
624 Conf_Server[i].flags = CONF_SFLAG_ONCE;
630 * Check if the given nick name is an service.
632 * @returns true if the given nick name belongs to an "IRC service".
635 Conf_IsService(int ConfServer, const char *Nick)
637 return MatchCaseInsensitive(Conf_Server[ConfServer].svs_mask, Nick);
641 * Initialize configuration settings with their default values.
644 Set_Defaults(bool InitServers)
649 strcpy(Conf_ServerName, "");
650 strcpy(Conf_ServerAdmin1, "");
651 strcpy(Conf_ServerAdmin2, "");
652 strcpy(Conf_ServerAdminMail, "");
653 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
654 PACKAGE_NAME, PACKAGE_VERSION);
655 free(Conf_ListenAddress);
656 Conf_ListenAddress = NULL;
657 array_free(&Conf_Motd);
658 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
659 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
660 strcpy(Conf_ServerPwd, "");
661 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
662 Conf_UID = Conf_GID = 0;
665 Conf_ConnectRetry = 60;
666 Conf_MaxConnections = 0;
667 Conf_MaxConnectionsIP = 5;
669 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
670 Conf_PingTimeout = 120;
671 Conf_PongTimeout = 20;
674 Conf_AllowRemoteOper = false;
676 Conf_AuthPing = false;
678 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
679 strcpy(Conf_CloakHost, "");
680 Conf_CloakUserToNick = false;
681 Conf_ConnectIPv4 = true;
683 Conf_ConnectIPv6 = true;
685 Conf_ConnectIPv6 = false;
693 Conf_MorePrivacy = false;
694 Conf_NoticeAuth = false;
695 Conf_OperCanMode = false;
696 Conf_OperServerMode = false;
702 Conf_PredefChannelsOnly = false;
704 Conf_ScrubCTCP = false;
706 Conf_SyslogFacility = LOG_LOCAL5;
708 Conf_SyslogFacility = 0;
712 /* Initialize IRC operators and channels */
714 Conf_Channel_Count = 0;
716 /* Initialize server configuration structures */
718 for (i = 0; i < MAX_SERVERS;
719 Init_Server_Struct(&Conf_Server[i++]));
724 * Get number of configured listening ports.
726 * @returns The number of ports (IPv4+IPv6) on which the server should listen.
731 size_t cnt = array_bytes(&Conf_ListenPorts);
733 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
739 * Read MOTD ("message of the day") file.
741 * @param filename Name of the file to read.
744 Read_Motd(const char *filename)
749 if (*filename == '\0')
752 fp = fopen(filename, "r");
754 Config_Error(LOG_WARNING, "Can't read MOTD file \"%s\": %s",
755 filename, strerror(errno));
759 array_free(&Conf_Motd);
760 Using_MotdFile = true;
762 while (fgets(line, (int)sizeof line, fp)) {
763 ngt_TrimLastChr( line, '\n');
765 /* add text including \0 */
766 if (!array_catb(&Conf_Motd, line, strlen(line) + 1)) {
767 Log(LOG_WARNING, "Cannot add MOTD text: %s", strerror(errno));
775 * Read ngIRCd configuration file.
777 * Please note that this function uses exit(1) on fatal errors and therefore
778 * can result in ngIRCd terminating!
780 * @param ngircd_starting Flag indicating if ngIRCd is starting or not.
781 * @returns true when the configuration file has been read
782 * successfully; false otherwise.
785 Read_Config( bool ngircd_starting )
787 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
788 const UINT16 defaultport = 6667;
792 /* Open configuration file */
793 fd = fopen( NGIRCd_ConfFile, "r" );
795 /* No configuration file found! */
796 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
797 NGIRCd_ConfFile, strerror( errno ));
798 if (!ngircd_starting)
800 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
805 Set_Defaults( ngircd_starting );
807 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
809 /* Clean up server configuration structure: mark all already
810 * configured servers as "once" so that they are deleted
811 * after the next disconnect and delete all unused servers.
812 * And delete all servers which are "duplicates" of servers
813 * that are already marked as "once" (such servers have been
814 * created by the last rehash but are now useless). */
815 for( i = 0; i < MAX_SERVERS; i++ ) {
816 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
818 /* This structure is in use ... */
819 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
820 /* Check for duplicates */
821 for( n = 0; n < MAX_SERVERS; n++ ) {
822 if( n == i ) continue;
824 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
825 Init_Server_Struct( &Conf_Server[n] );
827 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
833 /* Mark server as "once" */
834 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
835 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
840 /* Initialize variables */
842 strcpy( section, "" );
843 Init_Server_Struct( &New_Server );
844 New_Server_Idx = NONE;
848 /* Read configuration file */
850 if( ! fgets( str, LINE_LEN, fd )) break;
854 /* Skip comments and empty lines */
855 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
857 /* Is this the beginning of a new section? */
858 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
859 strlcpy( section, str, sizeof( section ));
860 if (strcasecmp(section, "[GLOBAL]") == 0 ||
861 strcasecmp(section, "[LIMITS]") == 0 ||
862 strcasecmp(section, "[OPTIONS]") == 0 ||
863 strcasecmp(section, "[SSL]") == 0)
866 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
867 /* Check if there is already a server to add */
868 if( New_Server.name[0] ) {
869 /* Copy data to "real" server structure */
870 assert( New_Server_Idx > NONE );
871 Conf_Server[New_Server_Idx] = New_Server;
874 /* Re-init structure for new server */
875 Init_Server_Struct( &New_Server );
877 /* Search unused item in server configuration structure */
878 for( i = 0; i < MAX_SERVERS; i++ ) {
879 /* Is this item used? */
880 if( ! Conf_Server[i].name[0] ) break;
882 if( i >= MAX_SERVERS ) {
883 /* Oops, no free item found! */
884 Config_Error( LOG_ERR, "Too many servers configured." );
885 New_Server_Idx = NONE;
887 else New_Server_Idx = i;
890 if (strcasecmp(section, "[CHANNEL]") == 0) {
891 Conf_Channel_Count++;
894 if (strcasecmp(section, "[OPERATOR]") == 0) {
899 Config_Error(LOG_ERR,
900 "%s, line %d: Unknown section \"%s\"!",
901 NGIRCd_ConfFile, line, section);
904 if( section[0] == 0x1 ) continue;
906 /* Split line into variable name and parameters */
907 ptr = strchr( str, '=' );
909 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
913 var = str; ngt_TrimStr( var );
914 arg = ptr + 1; ngt_TrimStr( arg );
916 if(strcasecmp(section, "[GLOBAL]") == 0)
917 Handle_GLOBAL(line, var, arg);
918 else if(strcasecmp(section, "[LIMITS]") == 0)
919 Handle_LIMITS(line, var, arg);
920 else if(strcasecmp(section, "[OPTIONS]") == 0)
921 Handle_OPTIONS(line, var, arg);
923 else if(strcasecmp(section, "[SSL]") == 0)
924 Handle_SSL(line, var, arg);
926 else if(strcasecmp(section, "[OPERATOR]") == 0)
927 Handle_OPERATOR(line, var, arg);
928 else if(strcasecmp(section, "[SERVER]") == 0)
929 Handle_SERVER(line, var, arg);
930 else if(strcasecmp(section, "[CHANNEL]") == 0)
931 Handle_CHANNEL(line, var, arg);
933 Config_Error(LOG_ERR,
934 "%s, line %d: Variable \"%s\" outside section!",
935 NGIRCd_ConfFile, line, var);
938 /* Close configuration file */
941 /* Check if there is still a server to add */
942 if( New_Server.name[0] ) {
943 /* Copy data to "real" server structure */
944 assert( New_Server_Idx > NONE );
945 Conf_Server[New_Server_Idx] = New_Server;
948 /* not a single listening port? Add default. */
949 if (no_listenports() &&
950 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
952 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
953 (unsigned int) defaultport, strerror(errno));
958 if (!Conf_ListenAddress)
959 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
961 if (!Conf_ListenAddress) {
962 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
966 /* No MOTD phrase configured? (re)try motd file. */
967 if (array_bytes(&Conf_Motd) == 0)
968 Read_Motd(Conf_MotdFile);
971 /* Make sure that all SSL-related files are readable */
972 CheckFileReadable("CertFile", Conf_SSLOptions.CertFile);
973 CheckFileReadable("DHFile", Conf_SSLOptions.DHFile);
974 CheckFileReadable("KeyFile", Conf_SSLOptions.KeyFile);
981 * Check whether an string argument is true or false.
983 * @param Arg Input string.
984 * @returns true if string has been parsed as "yes"/"true"/"on".
987 Check_ArgIsTrue( const char *Arg )
989 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
990 if( strcasecmp( Arg, "true" ) == 0 ) return true;
991 if( atoi( Arg ) != 0 ) return true;
997 * Handle setting of "MaxNickLength".
999 * @param Line Line number in configuration file.
1000 * @raram Arg Input string.
1001 * @returns New configured maximum nick name length.
1004 Handle_MaxNickLength(int Line, const char *Arg)
1008 new = (unsigned) atoi(Arg) + 1;
1009 if (new > CLIENT_NICK_LEN) {
1010 Config_Error(LOG_WARNING,
1011 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
1012 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
1013 return CLIENT_NICK_LEN;
1016 Config_Error(LOG_WARNING,
1017 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
1018 NGIRCd_ConfFile, Line);
1025 * Output a warning messages if IDENT is configured but not compiled in.
1028 WarnIdent(int UNUSED Line)
1032 /* user has enabled ident lookups explicitly, but ... */
1033 Config_Error(LOG_WARNING,
1034 "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
1035 NGIRCd_ConfFile, Line);
1041 * Output a warning messages if IPv6 is configured but not compiled in.
1044 WarnIPv6(int UNUSED Line)
1047 if (Conf_ConnectIPv6) {
1048 /* user has enabled IPv6 explicitly, but ... */
1049 Config_Error(LOG_WARNING,
1050 "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
1051 NGIRCd_ConfFile, Line);
1057 * Output a warning messages if PAM is configured but not compiled in.
1060 WarnPAM(int UNUSED Line)
1064 Config_Error(LOG_WARNING,
1065 "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
1066 NGIRCd_ConfFile, Line);
1072 * Handle legacy "NoXXX" options in [GLOBAL] section.
1074 * TODO: This function and support for "NoXXX" could be removed starting
1075 * with ngIRCd release 19 (one release after marking it "deprecated").
1077 * @param Var Variable name.
1078 * @param Arg Argument string.
1079 * @returns true if a NoXXX option has been processed; false otherwise.
1082 CheckLegacyNoOption(const char *Var, const char *Arg)
1084 if(strcasecmp(Var, "NoDNS") == 0) {
1085 Conf_DNS = !Check_ArgIsTrue( Arg );
1088 if (strcasecmp(Var, "NoIdent") == 0) {
1089 Conf_Ident = !Check_ArgIsTrue(Arg);
1092 if(strcasecmp(Var, "NoPAM") == 0) {
1093 Conf_PAM = !Check_ArgIsTrue(Arg);
1100 * Handle deprecated legacy options in [GLOBAL] section.
1102 * TODO: This function and support for these options in the [Global] section
1103 * could be removed starting with ngIRCd release 19 (one release after
1104 * marking it "deprecated").
1106 * @param Var Variable name.
1107 * @param Arg Argument string.
1108 * @returns true if a legacy option has been processed; false otherwise.
1111 CheckLegacyGlobalOption(int Line, char *Var, char *Arg)
1113 if (strcasecmp(Var, "AllowRemoteOper") == 0
1114 || strcasecmp(Var, "ChrootDir") == 0
1115 || strcasecmp(Var, "ConnectIPv4") == 0
1116 || strcasecmp(Var, "ConnectIPv6") == 0
1117 || strcasecmp(Var, "OperCanUseMode") == 0
1118 || strcasecmp(Var, "OperServerMode") == 0
1119 || strcasecmp(Var, "PredefChannelsOnly") == 0
1120 || strcasecmp(Var, "SyslogFacility") == 0
1121 || strcasecmp(Var, "WebircPassword") == 0) {
1122 Handle_OPTIONS(Line, Var, Arg);
1125 if (strcasecmp(Var, "ConnectRetry") == 0
1126 || strcasecmp(Var, "MaxConnections") == 0
1127 || strcasecmp(Var, "MaxConnectionsIP") == 0
1128 || strcasecmp(Var, "MaxJoins") == 0
1129 || strcasecmp(Var, "MaxNickLength") == 0
1130 || strcasecmp(Var, "PingTimeout") == 0
1131 || strcasecmp(Var, "PongTimeout") == 0) {
1132 Handle_LIMITS(Line, Var, Arg);
1136 if (strcasecmp(Var, "SSLCertFile") == 0
1137 || strcasecmp(Var, "SSLDHFile") == 0
1138 || strcasecmp(Var, "SSLKeyFile") == 0
1139 || strcasecmp(Var, "SSLKeyFilePassword") == 0
1140 || strcasecmp(Var, "SSLPorts") == 0) {
1141 Handle_SSL(Line, Var + 3, Arg);
1150 * Strip "no" prefix of a string.
1152 * TODO: This function and support for "NoXXX" should be removed starting
1153 * with ngIRCd release 19! (One release after marking it "deprecated").
1155 * @param str Pointer to input string starting with "no".
1156 * @returns New pointer to string without "no" prefix.
1159 NoNo(const char *str)
1161 assert(strncasecmp("no", str, 2) == 0 && str[2]);
1166 * Invert "boolean" string.
1168 * TODO: This function and support for "NoXXX" should be removed starting
1169 * with ngIRCd release 19! (One release after marking it "deprecated").
1171 * @param arg "Boolean" input string.
1172 * @returns Pointer to inverted "boolean string".
1175 InvertArg(const char *arg)
1177 return yesno_to_str(!Check_ArgIsTrue(arg));
1181 * Handle variable in [Global] configuration section.
1183 * @param Line Line numer in configuration file.
1184 * @param Var Variable name.
1185 * @param Arg Variable argument.
1188 Handle_GLOBAL( int Line, char *Var, char *Arg )
1193 const char *section;
1196 assert(Var != NULL);
1197 assert(Arg != NULL);
1199 if (strcasecmp(Var, "Name") == 0) {
1200 len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
1201 if (len >= sizeof(Conf_ServerName))
1202 Config_Error_TooLong(Line, Var);
1205 if (strcasecmp(Var, "AdminInfo1") == 0) {
1206 len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
1207 if (len >= sizeof(Conf_ServerAdmin1))
1208 Config_Error_TooLong(Line, Var);
1211 if (strcasecmp(Var, "AdminInfo2") == 0) {
1212 len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
1213 if (len >= sizeof(Conf_ServerAdmin2))
1214 Config_Error_TooLong(Line, Var);
1217 if (strcasecmp(Var, "AdminEMail") == 0) {
1218 len = strlcpy(Conf_ServerAdminMail, Arg,
1219 sizeof(Conf_ServerAdminMail));
1220 if (len >= sizeof(Conf_ServerAdminMail))
1221 Config_Error_TooLong(Line, Var);
1224 if (strcasecmp(Var, "Info") == 0) {
1225 len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
1226 if (len >= sizeof(Conf_ServerInfo))
1227 Config_Error_TooLong(Line, Var);
1230 if (strcasecmp(Var, "Listen") == 0) {
1231 if (Conf_ListenAddress) {
1232 Config_Error(LOG_ERR,
1233 "Multiple Listen= options, ignoring: %s",
1237 Conf_ListenAddress = strdup_warn(Arg);
1238 /* If allocation fails, we're in trouble: we cannot ignore the
1239 * error -- otherwise ngircd would listen on all interfaces. */
1240 if (!Conf_ListenAddress) {
1241 Config_Error(LOG_ALERT,
1242 "%s exiting due to fatal errors!",
1248 if (strcasecmp(Var, "MotdFile") == 0) {
1249 len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
1250 if (len >= sizeof(Conf_MotdFile))
1251 Config_Error_TooLong(Line, Var);
1254 if (strcasecmp(Var, "MotdPhrase") == 0) {
1258 if (len >= LINE_LEN) {
1259 Config_Error_TooLong(Line, Var);
1262 if (!array_copyb(&Conf_Motd, Arg, len + 1))
1263 Config_Error(LOG_WARNING,
1264 "%s, line %d: Could not append MotdPhrase: %s",
1265 NGIRCd_ConfFile, Line, strerror(errno));
1266 Using_MotdFile = false;
1269 if(strcasecmp(Var, "Password") == 0) {
1270 len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
1271 if (len >= sizeof(Conf_ServerPwd))
1272 Config_Error_TooLong(Line, Var);
1275 if (strcasecmp(Var, "PidFile") == 0) {
1276 len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
1277 if (len >= sizeof(Conf_PidFile))
1278 Config_Error_TooLong(Line, Var);
1281 if (strcasecmp(Var, "Ports") == 0) {
1282 ports_parse(&Conf_ListenPorts, Line, Arg);
1285 if (strcasecmp(Var, "ServerGID") == 0) {
1286 grp = getgrnam(Arg);
1288 Conf_GID = grp->gr_gid;
1290 Conf_GID = (unsigned int)atoi(Arg);
1291 if (!Conf_GID && strcmp(Arg, "0"))
1292 Config_Error_NaN(Line, Var);
1296 if (strcasecmp(Var, "ServerUID") == 0) {
1297 pwd = getpwnam(Arg);
1299 Conf_UID = pwd->pw_uid;
1301 Conf_UID = (unsigned int)atoi(Arg);
1302 if (!Conf_UID && strcmp(Arg, "0"))
1303 Config_Error_NaN(Line, Var);
1308 if (CheckLegacyNoOption(Var, Arg)) {
1309 /* TODO: This function and support for "NoXXX" could be
1310 * be removed starting with ngIRCd release 19 (one release
1311 * after marking it "deprecated"). */
1312 Config_Error(LOG_WARNING,
1313 "%s, line %d (section \"Global\"): \"No\"-Prefix is deprecated, use \"%s = %s\" in [Options] section!",
1314 NGIRCd_ConfFile, Line, NoNo(Var), InvertArg(Arg));
1315 if (strcasecmp(Var, "NoIdent") == 0)
1317 else if (strcasecmp(Var, "NoPam") == 0)
1321 if ((section = CheckLegacyGlobalOption(Line, Var, Arg))) {
1322 /** TODO: This function and support for these options in the
1323 * [Global] section could be removed starting with ngIRCd
1324 * release 19 (one release after marking it "deprecated"). */
1325 if (strncasecmp(Var, "SSL", 3) == 0) {
1326 Config_Error(LOG_WARNING,
1327 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s and rename to \"%s\"!",
1328 NGIRCd_ConfFile, Line, Var, section,
1331 Config_Error(LOG_WARNING,
1332 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s!",
1333 NGIRCd_ConfFile, Line, Var, section);
1338 Config_Error_Section(Line, Var, "Global");
1342 * Handle variable in [Limits] configuration section.
1344 * @param Line Line numer in configuration file.
1345 * @param Var Variable name.
1346 * @param Arg Variable argument.
1349 Handle_LIMITS(int Line, char *Var, char *Arg)
1352 assert(Var != NULL);
1353 assert(Arg != NULL);
1355 if (strcasecmp(Var, "ConnectRetry") == 0) {
1356 Conf_ConnectRetry = atoi(Arg);
1357 if (Conf_ConnectRetry < 5) {
1358 Config_Error(LOG_WARNING,
1359 "%s, line %d: Value of \"ConnectRetry\" too low!",
1360 NGIRCd_ConfFile, Line);
1361 Conf_ConnectRetry = 5;
1365 if (strcasecmp(Var, "MaxConnections") == 0) {
1366 Conf_MaxConnections = atol(Arg);
1367 if (!Conf_MaxConnections && strcmp(Arg, "0"))
1368 Config_Error_NaN(Line, Var);
1371 if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
1372 Conf_MaxConnectionsIP = atoi(Arg);
1373 if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
1374 Config_Error_NaN(Line, Var);
1377 if (strcasecmp(Var, "MaxJoins") == 0) {
1378 Conf_MaxJoins = atoi(Arg);
1379 if (!Conf_MaxJoins && strcmp(Arg, "0"))
1380 Config_Error_NaN(Line, Var);
1383 if (strcasecmp(Var, "MaxNickLength") == 0) {
1384 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1387 if (strcasecmp(Var, "PingTimeout") == 0) {
1388 Conf_PingTimeout = atoi(Arg);
1389 if (Conf_PingTimeout < 5) {
1390 Config_Error(LOG_WARNING,
1391 "%s, line %d: Value of \"PingTimeout\" too low!",
1392 NGIRCd_ConfFile, Line);
1393 Conf_PingTimeout = 5;
1397 if (strcasecmp(Var, "PongTimeout") == 0) {
1398 Conf_PongTimeout = atoi(Arg);
1399 if (Conf_PongTimeout < 5) {
1400 Config_Error(LOG_WARNING,
1401 "%s, line %d: Value of \"PongTimeout\" too low!",
1402 NGIRCd_ConfFile, Line);
1403 Conf_PongTimeout = 5;
1408 Config_Error_Section(Line, Var, "Limits");
1412 * Handle variable in [Options] configuration section.
1414 * @param Line Line numer in configuration file.
1415 * @param Var Variable name.
1416 * @param Arg Variable argument.
1419 Handle_OPTIONS(int Line, char *Var, char *Arg)
1424 assert(Var != NULL);
1425 assert(Arg != NULL);
1427 if (strcasecmp(Var, "AllowRemoteOper") == 0) {
1428 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1431 if (strcasecmp(Var, "ChrootDir") == 0) {
1432 len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
1433 if (len >= sizeof(Conf_Chroot))
1434 Config_Error_TooLong(Line, Var);
1437 if (strcasecmp(Var, "CloakHost") == 0) {
1438 len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
1439 if (len >= sizeof(Conf_CloakHost))
1440 Config_Error_TooLong(Line, Var);
1443 if (strcasecmp(Var, "CloakUserToNick") == 0) {
1444 Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
1447 if (strcasecmp(Var, "ConnectIPv6") == 0) {
1448 Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
1452 if (strcasecmp(Var, "ConnectIPv4") == 0) {
1453 Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
1456 if (strcasecmp(Var, "DNS") == 0) {
1457 Conf_DNS = Check_ArgIsTrue(Arg);
1460 if (strcasecmp(Var, "Ident") == 0) {
1461 Conf_Ident = Check_ArgIsTrue(Arg);
1465 if (strcasecmp(Var, "MorePrivacy") == 0) {
1466 Conf_MorePrivacy = Check_ArgIsTrue(Arg);
1469 if (strcasecmp(Var, "NoticeAuth") == 0) {
1470 Conf_NoticeAuth = Check_ArgIsTrue(Arg);
1473 if (strcasecmp(Var, "OperCanUseMode") == 0) {
1474 Conf_OperCanMode = Check_ArgIsTrue(Arg);
1477 if (strcasecmp(Var, "OperServerMode") == 0) {
1478 Conf_OperServerMode = Check_ArgIsTrue(Arg);
1481 if (strcasecmp(Var, "PAM") == 0) {
1482 Conf_PAM = Check_ArgIsTrue(Arg);
1486 if (strcasecmp(Var, "PredefChannelsOnly") == 0) {
1487 Conf_PredefChannelsOnly = Check_ArgIsTrue(Arg);
1491 if (strcasecmp(Var, "RequireAuthPing") == 0) {
1492 Conf_AuthPing = Check_ArgIsTrue(Arg);
1496 if (strcasecmp(Var, "ScrubCTCP") == 0) {
1497 Conf_ScrubCTCP = Check_ArgIsTrue(Arg);
1501 if (strcasecmp(Var, "SyslogFacility") == 0) {
1502 Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
1503 Conf_SyslogFacility);
1507 if (strcasecmp(Var, "WebircPassword") == 0) {
1508 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
1509 if (len >= sizeof(Conf_WebircPwd))
1510 Config_Error_TooLong(Line, Var);
1514 Config_Error_Section(Line, Var, "Options");
1520 * Handle variable in [SSL] configuration section.
1522 * @param Line Line numer in configuration file.
1523 * @param Var Variable name.
1524 * @param Arg Variable argument.
1527 Handle_SSL(int Line, char *Var, char *Arg)
1530 assert(Var != NULL);
1531 assert(Arg != NULL);
1533 if (strcasecmp(Var, "CertFile") == 0) {
1534 assert(Conf_SSLOptions.CertFile == NULL);
1535 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1538 if (strcasecmp(Var, "DHFile") == 0) {
1539 assert(Conf_SSLOptions.DHFile == NULL);
1540 Conf_SSLOptions.DHFile = strdup_warn(Arg);
1543 if (strcasecmp(Var, "KeyFile") == 0) {
1544 assert(Conf_SSLOptions.KeyFile == NULL);
1545 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1548 if (strcasecmp(Var, "KeyFilePassword") == 0) {
1549 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1550 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1551 Config_Error(LOG_ERR,
1552 "%s, line %d (section \"SSL\"): Could not copy %s: %s!",
1553 NGIRCd_ConfFile, Line, Var,
1557 if (strcasecmp(Var, "Ports") == 0) {
1558 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1562 Config_Error_Section(Line, Var, "SSL");
1568 * Handle variable in [Operator] configuration section.
1570 * @param Line Line numer in configuration file.
1571 * @param Var Variable name.
1572 * @param Arg Variable argument.
1575 Handle_OPERATOR( int Line, char *Var, char *Arg )
1578 struct Conf_Oper *op;
1581 assert( Var != NULL );
1582 assert( Arg != NULL );
1583 assert( Conf_Oper_Count > 0 );
1585 op = array_alloc(&Conf_Opers, sizeof(*op), Conf_Oper_Count - 1);
1587 Config_Error(LOG_ERR, "Could not allocate memory for operator (%d:%s = %s)", Line, Var, Arg);
1591 if (strcasecmp(Var, "Name") == 0) {
1592 /* Name of IRC operator */
1593 len = strlcpy(op->name, Arg, sizeof(op->name));
1594 if (len >= sizeof(op->name))
1595 Config_Error_TooLong(Line, Var);
1598 if (strcasecmp(Var, "Password") == 0) {
1599 /* Password of IRC operator */
1600 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1601 if (len >= sizeof(op->pwd))
1602 Config_Error_TooLong(Line, Var);
1605 if (strcasecmp(Var, "Mask") == 0) {
1607 return; /* Hostname already configured */
1608 op->mask = strdup_warn( Arg );
1612 Config_Error_Section(Line, Var, "Operator");
1616 * Handle variable in [Server] configuration section.
1618 * @param Line Line numer in configuration file.
1619 * @param Var Variable name.
1620 * @param Arg Variable argument.
1623 Handle_SERVER( int Line, char *Var, char *Arg )
1629 assert( Var != NULL );
1630 assert( Arg != NULL );
1632 /* Ignore server block if no space is left in server configuration structure */
1633 if( New_Server_Idx <= NONE ) return;
1635 if( strcasecmp( Var, "Host" ) == 0 ) {
1636 /* Hostname of the server */
1637 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1638 if (len >= sizeof( New_Server.host ))
1639 Config_Error_TooLong ( Line, Var );
1642 if( strcasecmp( Var, "Name" ) == 0 ) {
1643 /* Name of the server ("Nick"/"ID") */
1644 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1645 if (len >= sizeof( New_Server.name ))
1646 Config_Error_TooLong( Line, Var );
1649 if (strcasecmp(Var, "Bind") == 0) {
1650 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1653 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1654 NGIRCd_ConfFile, Line, Arg);
1657 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1658 /* Password of this server which is sent to the peer */
1660 Config_Error(LOG_ERR,
1661 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1662 NGIRCd_ConfFile, Line);
1664 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1665 if (len >= sizeof( New_Server.pwd_in ))
1666 Config_Error_TooLong( Line, Var );
1669 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1670 /* Passwort of the peer which must be received */
1671 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1672 if (len >= sizeof( New_Server.pwd_out ))
1673 Config_Error_TooLong( Line, Var );
1676 if( strcasecmp( Var, "Port" ) == 0 ) {
1677 /* Port to which this server should connect */
1679 if (port >= 0 && port < 0xFFFF)
1680 New_Server.port = (UINT16)port;
1682 Config_Error(LOG_ERR,
1683 "%s, line %d (section \"Server\"): Illegal port number %ld!",
1684 NGIRCd_ConfFile, Line, port );
1688 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1689 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1693 if( strcasecmp( Var, "Group" ) == 0 ) {
1695 New_Server.group = atoi( Arg );
1696 if (!New_Server.group && strcmp(Arg, "0"))
1697 Config_Error_NaN(Line, Var);
1700 if( strcasecmp( Var, "Passive" ) == 0 ) {
1701 if (Check_ArgIsTrue(Arg))
1702 New_Server.flags |= CONF_SFLAG_DISABLED;
1705 if (strcasecmp(Var, "ServiceMask") == 0) {
1706 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1707 sizeof(New_Server.svs_mask));
1708 if (len >= sizeof(New_Server.svs_mask))
1709 Config_Error_TooLong(Line, Var);
1713 Config_Error_Section(Line, Var, "Server");
1717 * Copy channel name into channel structure.
1719 * If the channel name is not valid because of a missing prefix ('#', '&'),
1720 * a default prefix of '#' will be added.
1722 * @param new_chan New already allocated channel structure.
1723 * @param name Name of the new channel.
1724 * @returns true on success, false otherwise.
1727 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1729 size_t size = sizeof(new_chan->name);
1730 char *dest = new_chan->name;
1732 if (!Channel_IsValidName(name)) {
1734 * maybe user forgot to add a '#'.
1735 * This is only here for user convenience.
1741 return size > strlcpy(dest, name, size);
1745 * Handle variable in [Channel] configuration section.
1747 * @param Line Line numer in configuration file.
1748 * @param Var Variable name.
1749 * @param Arg Variable argument.
1752 Handle_CHANNEL(int Line, char *Var, char *Arg)
1756 struct Conf_Channel *chan;
1759 assert( Var != NULL );
1760 assert( Arg != NULL );
1761 assert(Conf_Channel_Count > 0);
1763 chancount = Conf_Channel_Count - 1;
1765 chan = array_alloc(&Conf_Channels, sizeof(*chan), chancount);
1767 Config_Error(LOG_ERR, "Could not allocate memory for predefined channel (%d:%s = %s)", Line, Var, Arg);
1770 if (strcasecmp(Var, "Name") == 0) {
1771 if (!Handle_Channelname(chan, Arg))
1772 Config_Error_TooLong(Line, Var);
1775 if (strcasecmp(Var, "Modes") == 0) {
1777 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1778 if (len >= sizeof(chan->modes))
1779 Config_Error_TooLong( Line, Var );
1782 if( strcasecmp( Var, "Topic" ) == 0 ) {
1784 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1785 if (len >= sizeof(chan->topic))
1786 Config_Error_TooLong( Line, Var );
1789 if( strcasecmp( Var, "Key" ) == 0 ) {
1790 /* Initial Channel Key (mode k) */
1791 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1792 if (len >= sizeof(chan->key))
1793 Config_Error_TooLong(Line, Var);
1796 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1797 /* maximum user limit, mode l */
1798 chan->maxusers = (unsigned long) atol(Arg);
1799 if (!chan->maxusers && strcmp(Arg, "0"))
1800 Config_Error_NaN(Line, Var);
1803 if (strcasecmp(Var, "KeyFile") == 0) {
1805 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1806 if (len >= sizeof(chan->keyfile))
1807 Config_Error_TooLong(Line, Var);
1811 Config_Error_Section(Line, Var, "Channel");
1815 * Validate server configuration.
1817 * Please note that this function uses exit(1) on fatal errors and therefore
1818 * can result in ngIRCd terminating!
1820 * @param Configtest true if the daemon has been called with "--configtest".
1821 * @param Rehash true if re-reading configuration on runtime.
1822 * @returns true if configuration is valid.
1825 Validate_Config(bool Configtest, bool Rehash)
1827 /* Validate configuration settings. */
1830 int i, servers, servers_once;
1832 bool config_valid = true;
1835 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1836 ptr = Conf_ServerName;
1838 if (*ptr >= 'a' && *ptr <= 'z') continue;
1839 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1840 if (*ptr >= '0' && *ptr <= '9') continue;
1841 if (ptr > Conf_ServerName) {
1842 if (*ptr == '.' || *ptr == '-')
1845 Conf_ServerName[0] = '\0';
1849 if (!Conf_ServerName[0]) {
1850 /* No server name configured! */
1851 config_valid = false;
1852 Config_Error(LOG_ALERT,
1853 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1855 if (!Configtest && !Rehash) {
1856 Config_Error(LOG_ALERT,
1857 "%s exiting due to fatal errors!",
1863 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1864 /* No dot in server name! */
1865 config_valid = false;
1866 Config_Error(LOG_ALERT,
1867 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1870 Config_Error(LOG_ALERT,
1871 "%s exiting due to fatal errors!",
1878 if (!Conf_ServerAdminMail[0]) {
1879 /* No administrative contact configured! */
1880 config_valid = false;
1881 Config_Error(LOG_ALERT,
1882 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1885 Config_Error(LOG_ALERT,
1886 "%s exiting due to fatal errors!",
1893 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1894 && !Conf_ServerAdminMail[0]) {
1895 /* No administrative information configured! */
1896 Config_Error(LOG_WARNING,
1897 "No administrative information configured but required by RFC!");
1901 if (Conf_ServerPwd[0])
1902 Config_Error(LOG_ERR,
1903 "This server uses PAM, \"Password\" in [Global] section will be ignored!");
1907 servers = servers_once = 0;
1908 for (i = 0; i < MAX_SERVERS; i++) {
1909 if (Conf_Server[i].name[0]) {
1911 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1916 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1917 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1920 return config_valid;
1924 * Output "line too long" warning.
1926 * @param Line Line number in configuration file.
1927 * @param Item Affected variable name.
1930 Config_Error_TooLong ( const int Line, const char *Item )
1932 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1936 * Output "unknown variable" warning.
1938 * @param Line Line number in configuration file.
1939 * @param Item Affected variable name.
1940 * @param Section Section name.
1943 Config_Error_Section(const int Line, const char *Item, const char *Section)
1945 Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
1946 NGIRCd_ConfFile, Line, Section, Item);
1950 * Output "not a number" warning.
1952 * @param Line Line number in configuration file.
1953 * @param Item Affected variable name.
1956 Config_Error_NaN( const int Line, const char *Item )
1958 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1959 NGIRCd_ConfFile, Line, Item );
1963 * Output configuration error to console and/or logfile.
1965 * On runtime, the normal log functions of the daemon are used. But when
1966 * testing the configuration ("--configtest"), all messages go directly
1969 * @param Level Severity level of the message.
1970 * @param Format Format string; see printf() function.
1973 static void Config_Error( const int Level, const char *Format, ... )
1975 static void Config_Error( Level, Format, va_alist )
1981 char msg[MAX_LOG_MSG_LEN];
1984 assert( Format != NULL );
1987 va_start( ap, Format );
1991 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1995 if (Level <= LOG_WARNING)
1996 printf(" - %s\n", msg);
2000 Log(Level, "%s", msg);
2006 * Dump internal state of the "configuration module".
2009 Conf_DebugDump(void)
2013 Log(LOG_DEBUG, "Configured servers:");
2014 for (i = 0; i < MAX_SERVERS; i++) {
2015 if (! Conf_Server[i].name[0])
2018 " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
2019 Conf_Server[i].name, Conf_Server[i].host,
2020 Conf_Server[i].port, Conf_Server[i].lasttry,
2021 Conf_Server[i].group, Conf_Server[i].flags,
2022 Conf_Server[i].conn_id);
2029 * Initialize server configuration structur to default values.
2031 * @param Server Pointer to server structure to initialize.
2034 Init_Server_Struct( CONF_SERVER *Server )
2036 assert( Server != NULL );
2038 memset( Server, 0, sizeof (CONF_SERVER) );
2040 Server->group = NONE;
2041 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
2043 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
2045 Proc_InitStruct(&Server->res_stat);
2046 Server->conn_id = NONE;
2047 memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));