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 struct SSLOptions Conf_SSLOptions;
95 * Initialize SSL configuration.
100 free(Conf_SSLOptions.KeyFile);
101 Conf_SSLOptions.KeyFile = NULL;
103 free(Conf_SSLOptions.CertFile);
104 Conf_SSLOptions.CertFile = NULL;
106 free(Conf_SSLOptions.DHFile);
107 Conf_SSLOptions.DHFile = NULL;
108 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
112 * Make sure that a configured file is readable.
114 * Currently, this function is only used for SSL-related options ...
116 * @param Var Configuration variable
117 * @param Filename Configured filename
120 CheckFileReadable(const char *Var, const char *Filename)
124 fp = fopen(Filename, "r");
128 Config_Error(LOG_ERR, "Can't read \"%s\" (\"%s\"): %s",
129 Filename, Var, strerror(errno));
136 * Duplicate string and warn on errors.
138 * @returns Pointer to string on success, NULL otherwise.
141 strdup_warn(const char *str)
143 char *ptr = strdup(str);
145 Config_Error(LOG_ERR,
146 "Could not allocate memory for string: %s", str);
151 * Output a comma separated list of ports (integer values).
158 len = array_length(a, sizeof(UINT16));
160 ports = (UINT16*) array_start(a);
161 printf("%u", (unsigned int) *ports);
164 printf(", %u", (unsigned int) *ports);
171 * Parse a comma separated string into an array of port numbers (integers).
174 ports_parse(array *a, int Line, char *Arg)
182 ptr = strtok( Arg, "," );
186 if (port > 0 && port < 0xFFFF) {
187 port16 = (UINT16) port;
188 if (!array_catb(a, (char*)&port16, sizeof port16))
189 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
190 NGIRCd_ConfFile, Line, port, strerror(errno));
192 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
193 NGIRCd_ConfFile, Line, port );
196 ptr = strtok( NULL, "," );
201 * Initialize configuration module.
207 Validate_Config(false, false);
211 * "Rehash" (reload) server configuration.
213 * @returns true if configuration has been re-read, false on errors.
218 if (!Read_Config(false))
220 Validate_Config(false, true);
222 /* Update CLIENT structure of local server */
223 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
228 * Output a boolean value as "yes/no" string.
231 yesno_to_str(int boolean_value)
239 * Free all IRC operator configuration structures.
244 struct Conf_Oper *op;
247 len = array_length(&Conf_Opers, sizeof(*op));
248 op = array_start(&Conf_Opers);
253 array_free(&Conf_Opers);
257 * Output all IRC operator configuration structures.
262 struct Conf_Oper *op;
265 len = array_length(&Conf_Opers, sizeof(*op));
266 op = array_start(&Conf_Opers);
271 printf(" Name = %s\n", op->name);
272 printf(" Password = %s\n", op->pwd);
273 printf(" Mask = %s\n\n", op->mask ? op->mask : "");
279 * Read configuration, validate and output it.
281 * This function waits for a keypress of the user when stdin/stdout are valid
282 * tty's ("you can read our nice message and we can read in your keypress").
284 * @return 0 on succes, 1 on failure(s); therefore the result code can
285 * directly be used by exit() when running "ngircd --configtest".
294 size_t predef_channel_count;
295 struct Conf_Channel *predef_chan;
299 if (! Read_Config(true))
302 config_valid = Validate_Config(true, false);
305 if(isatty(fileno(stdin)) && isatty(fileno(stdout))) {
306 puts("OK, press enter to see a dump of your server configuration ...");
309 puts("Ok, dump of your server configuration follows:\n");
312 printf(" Name = %s\n", Conf_ServerName);
313 printf(" AdminInfo1 = %s\n", Conf_ServerAdmin1);
314 printf(" AdminInfo2 = %s\n", Conf_ServerAdmin2);
315 printf(" AdminEMail = %s\n", Conf_ServerAdminMail);
316 printf(" Info = %s\n", Conf_ServerInfo);
317 printf(" Listen = %s\n", Conf_ListenAddress);
318 if (Using_MotdFile) {
319 printf(" MotdFile = %s\n", Conf_MotdFile);
320 printf(" MotdPhrase =\n");
322 printf(" MotdFile = \n");
323 printf(" MotdPhrase = %s\n", array_bytes(&Conf_Motd)
324 ? (const char*) array_start(&Conf_Motd) : "");
327 printf(" Password = %s\n", Conf_ServerPwd);
329 printf(" PidFile = %s\n", Conf_PidFile);
331 ports_puts(&Conf_ListenPorts);
332 grp = getgrgid(Conf_GID);
334 printf(" ServerGID = %s\n", grp->gr_name);
336 printf(" ServerGID = %ld\n", (long)Conf_GID);
337 pwd = getpwuid(Conf_UID);
339 printf(" ServerUID = %s\n", pwd->pw_name);
341 printf(" ServerUID = %ld\n", (long)Conf_UID);
345 printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
346 printf(" MaxConnections = %ld\n", Conf_MaxConnections);
347 printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
348 printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
349 printf(" MaxNickLength = %u\n", Conf_MaxNickLength - 1);
350 printf(" PingTimeout = %d\n", Conf_PingTimeout);
351 printf(" PongTimeout = %d\n", Conf_PongTimeout);
355 printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
356 printf(" ChrootDir = %s\n", Conf_Chroot);
357 printf(" CloakHost = %s\n", Conf_CloakHost);
358 printf(" CloakUserToNick = %s\n", yesno_to_str(Conf_CloakUserToNick));
360 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
361 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
363 printf(" DNS = %s\n", yesno_to_str(Conf_DNS));
365 printf(" Ident = %s\n", yesno_to_str(Conf_Ident));
367 printf(" MorePrivacy = %s\n", yesno_to_str(Conf_MorePrivacy));
368 printf(" NoticeAuth = %s\n", yesno_to_str(Conf_NoticeAuth));
369 printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
370 printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
372 printf(" PAM = %s\n", yesno_to_str(Conf_PAM));
374 printf(" PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
376 printf(" RequireAuthPing = %s\n", yesno_to_str(Conf_AuthPing));
379 printf(" SSLCertFile = %s\n", Conf_SSLOptions.CertFile);
380 printf(" SSLDHFile = %s\n", Conf_SSLOptions.DHFile);
381 printf(" SSLKeyFile = %s\n", Conf_SSLOptions.KeyFile);
382 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
383 puts(" SSLKeyFilePassword = <secret>");
385 puts(" SSLKeyFilePassword = ");
386 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
387 printf(" SSLPorts = ");
388 ports_puts(&Conf_SSLOptions.ListenPorts);
391 printf(" SyslogFacility = %s\n",
392 ngt_SyslogFacilityName(Conf_SyslogFacility));
394 printf(" WebircPassword = %s\n", Conf_WebircPwd);
399 for( i = 0; i < MAX_SERVERS; i++ ) {
400 if( ! Conf_Server[i].name[0] ) continue;
402 /* Valid "Server" section */
404 printf( " Name = %s\n", Conf_Server[i].name );
405 printf( " Host = %s\n", Conf_Server[i].host );
406 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
408 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
410 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
411 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
412 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
413 printf( " Group = %d\n", Conf_Server[i].group );
414 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
417 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
418 predef_chan = array_start(&Conf_Channels);
420 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
421 if (!predef_chan->name[0])
424 /* Valid "Channel" section */
426 printf(" Name = %s\n", predef_chan->name);
427 printf(" Modes = %s\n", predef_chan->modes);
428 printf(" Key = %s\n", predef_chan->key);
429 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
430 printf(" Topic = %s\n", predef_chan->topic);
431 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
434 return (config_valid ? 0 : 1);
438 * Remove connection information from configured server.
440 * If the server is set as "once", delete it from our configuration;
441 * otherwise set the time for the next connection attempt.
443 * Non-server connections will be silently ignored.
446 Conf_UnsetServer( CONN_ID Idx )
451 /* Check all our configured servers */
452 for( i = 0; i < MAX_SERVERS; i++ ) {
453 if( Conf_Server[i].conn_id != Idx ) continue;
455 /* Gotcha! Mark server configuration as "unused": */
456 Conf_Server[i].conn_id = NONE;
458 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
459 /* Delete configuration here */
460 Init_Server_Struct( &Conf_Server[i] );
462 /* Set time for next connect attempt */
464 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
465 /* The connection has been "long", so we don't
466 * require the next attempt to be delayed. */
467 Conf_Server[i].lasttry =
468 t - Conf_ConnectRetry + RECONNECT_DELAY;
470 Conf_Server[i].lasttry = t;
476 * Set connection information for specified configured server.
479 Conf_SetServer( int ConfServer, CONN_ID Idx )
481 assert( ConfServer > NONE );
482 assert( Idx > NONE );
484 Conf_Server[ConfServer].conn_id = Idx;
488 * Get index of server in configuration structure.
491 Conf_GetServer( CONN_ID Idx )
495 assert( Idx > NONE );
497 for( i = 0; i < MAX_SERVERS; i++ ) {
498 if( Conf_Server[i].conn_id == Idx ) return i;
504 * Enable a server by name and adjust its port number.
506 * @returns true if a server has been enabled and now has a valid port
507 * number and host name for outgoing connections.
510 Conf_EnableServer( const char *Name, UINT16 Port )
514 assert( Name != NULL );
515 for( i = 0; i < MAX_SERVERS; i++ ) {
516 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
517 /* Gotcha! Set port and enable server: */
518 Conf_Server[i].port = Port;
519 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
520 return (Conf_Server[i].port && Conf_Server[i].host[0]);
527 * Enable a server by name.
529 * The server is only usable as outgoing server, if it has set a valid port
530 * number for outgoing connections!
531 * If not, you have to use Conf_EnableServer() function to make it available.
533 * @returns true if a server has been enabled; false otherwise.
536 Conf_EnablePassiveServer(const char *Name)
540 assert( Name != NULL );
541 for (i = 0; i < MAX_SERVERS; i++) {
542 if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
543 && (Conf_Server[i].port > 0)) {
544 /* BINGO! Enable server */
545 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
553 * Disable a server by name.
554 * An already established connection will be disconnected.
556 * @returns true if a server was found and has been disabled.
559 Conf_DisableServer( const char *Name )
563 assert( Name != NULL );
564 for( i = 0; i < MAX_SERVERS; i++ ) {
565 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
566 /* Gotcha! Disable and disconnect server: */
567 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
568 if( Conf_Server[i].conn_id > NONE )
569 Conn_Close(Conf_Server[i].conn_id, NULL,
570 "Server link terminated on operator request",
579 * Add a new remote server to our configuration.
581 * @param Name Name of the new server.
582 * @param Port Port number to connect to or 0 for incoming connections.
583 * @param Host Host name to connect to.
584 * @param MyPwd Password that will be sent to the peer.
585 * @param PeerPwd Password that must be received from the peer.
586 * @returns true if the new server has been added; false otherwise.
589 Conf_AddServer(const char *Name, UINT16 Port, const char *Host,
590 const char *MyPwd, const char *PeerPwd)
594 assert( Name != NULL );
595 assert( Host != NULL );
596 assert( MyPwd != NULL );
597 assert( PeerPwd != NULL );
599 /* Search unused item in server configuration structure */
600 for( i = 0; i < MAX_SERVERS; i++ ) {
601 /* Is this item used? */
602 if( ! Conf_Server[i].name[0] ) break;
604 if( i >= MAX_SERVERS ) return false;
606 Init_Server_Struct( &Conf_Server[i] );
607 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
608 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
609 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
610 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
611 Conf_Server[i].port = Port;
612 Conf_Server[i].flags = CONF_SFLAG_ONCE;
618 * Check if the given nick name is an service.
620 * @returns true if the given nick name belongs to an "IRC service".
623 Conf_IsService(int ConfServer, const char *Nick)
625 return MatchCaseInsensitive(Conf_Server[ConfServer].svs_mask, Nick);
629 * Initialize configuration settings with their default values.
632 Set_Defaults(bool InitServers)
637 strcpy(Conf_ServerName, "");
638 strcpy(Conf_ServerAdmin1, "");
639 strcpy(Conf_ServerAdmin2, "");
640 strcpy(Conf_ServerAdminMail, "");
641 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
642 PACKAGE_NAME, PACKAGE_VERSION);
643 free(Conf_ListenAddress);
644 Conf_ListenAddress = NULL;
645 array_free(&Conf_Motd);
646 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
647 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
648 strcpy(Conf_ServerPwd, "");
649 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
650 Conf_UID = Conf_GID = 0;
653 Conf_ConnectRetry = 60;
654 Conf_MaxConnections = 0;
655 Conf_MaxConnectionsIP = 5;
657 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
658 Conf_PingTimeout = 120;
659 Conf_PongTimeout = 20;
662 Conf_AllowRemoteOper = false;
664 Conf_AuthPing = false;
666 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
667 strcpy(Conf_CloakHost, "");
668 Conf_CloakUserToNick = false;
669 Conf_ConnectIPv4 = true;
671 Conf_ConnectIPv6 = true;
673 Conf_ConnectIPv6 = false;
681 Conf_MorePrivacy = false;
682 Conf_NoticeAuth = false;
683 Conf_OperCanMode = false;
684 Conf_OperServerMode = false;
690 Conf_PredefChannelsOnly = false;
693 Conf_SyslogFacility = LOG_LOCAL5;
695 Conf_SyslogFacility = 0;
699 /* Initialize IRC operators and channels */
701 Conf_Channel_Count = 0;
703 /* Initialize server configuration structures */
705 for (i = 0; i < MAX_SERVERS;
706 Init_Server_Struct(&Conf_Server[i++]));
711 * Get number of configured listening ports.
713 * @returns The number of ports (IPv4+IPv6) on which the server should listen.
718 size_t cnt = array_bytes(&Conf_ListenPorts);
720 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
726 * Read MOTD ("message of the day") file.
728 * @param filename Name of the file to read.
731 Read_Motd(const char *filename)
736 if (*filename == '\0')
739 fp = fopen(filename, "r");
741 Config_Error(LOG_WARNING, "Can't read MOTD file \"%s\": %s",
742 filename, strerror(errno));
746 array_free(&Conf_Motd);
747 Using_MotdFile = true;
749 while (fgets(line, (int)sizeof line, fp)) {
750 ngt_TrimLastChr( line, '\n');
752 /* add text including \0 */
753 if (!array_catb(&Conf_Motd, line, strlen(line) + 1)) {
754 Log(LOG_WARNING, "Cannot add MOTD text: %s", strerror(errno));
762 * Read ngIRCd configuration file.
764 * Please note that this function uses exit(1) on fatal errors and therefore
765 * can result in ngIRCd terminating!
767 * @param ngircd_starting Flag indicating if ngIRCd is starting or not.
768 * @returns true when the configuration file has been read
769 * successfully; false otherwise.
772 Read_Config( bool ngircd_starting )
774 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
775 const UINT16 defaultport = 6667;
779 /* Open configuration file */
780 fd = fopen( NGIRCd_ConfFile, "r" );
782 /* No configuration file found! */
783 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
784 NGIRCd_ConfFile, strerror( errno ));
785 if (!ngircd_starting)
787 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
792 Set_Defaults( ngircd_starting );
794 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
796 /* Clean up server configuration structure: mark all already
797 * configured servers as "once" so that they are deleted
798 * after the next disconnect and delete all unused servers.
799 * And delete all servers which are "duplicates" of servers
800 * that are already marked as "once" (such servers have been
801 * created by the last rehash but are now useless). */
802 for( i = 0; i < MAX_SERVERS; i++ ) {
803 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
805 /* This structure is in use ... */
806 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
807 /* Check for duplicates */
808 for( n = 0; n < MAX_SERVERS; n++ ) {
809 if( n == i ) continue;
811 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
812 Init_Server_Struct( &Conf_Server[n] );
814 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
820 /* Mark server as "once" */
821 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
822 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
827 /* Initialize variables */
829 strcpy( section, "" );
830 Init_Server_Struct( &New_Server );
831 New_Server_Idx = NONE;
835 /* Read configuration file */
837 if( ! fgets( str, LINE_LEN, fd )) break;
841 /* Skip comments and empty lines */
842 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
844 /* Is this the beginning of a new section? */
845 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
846 strlcpy( section, str, sizeof( section ));
847 if (strcasecmp(section, "[GLOBAL]") == 0 ||
848 strcasecmp(section, "[LIMITS]") == 0 ||
849 strcasecmp(section, "[OPTIONS]") == 0)
852 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
853 /* Check if there is already a server to add */
854 if( New_Server.name[0] ) {
855 /* Copy data to "real" server structure */
856 assert( New_Server_Idx > NONE );
857 Conf_Server[New_Server_Idx] = New_Server;
860 /* Re-init structure for new server */
861 Init_Server_Struct( &New_Server );
863 /* Search unused item in server configuration structure */
864 for( i = 0; i < MAX_SERVERS; i++ ) {
865 /* Is this item used? */
866 if( ! Conf_Server[i].name[0] ) break;
868 if( i >= MAX_SERVERS ) {
869 /* Oops, no free item found! */
870 Config_Error( LOG_ERR, "Too many servers configured." );
871 New_Server_Idx = NONE;
873 else New_Server_Idx = i;
876 if (strcasecmp(section, "[CHANNEL]") == 0) {
877 Conf_Channel_Count++;
880 if (strcasecmp(section, "[OPERATOR]") == 0) {
885 Config_Error(LOG_ERR,
886 "%s, line %d: Unknown section \"%s\"!",
887 NGIRCd_ConfFile, line, section);
890 if( section[0] == 0x1 ) continue;
892 /* Split line into variable name and parameters */
893 ptr = strchr( str, '=' );
895 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
899 var = str; ngt_TrimStr( var );
900 arg = ptr + 1; ngt_TrimStr( arg );
902 if(strcasecmp(section, "[GLOBAL]") == 0)
903 Handle_GLOBAL(line, var, arg);
904 else if(strcasecmp(section, "[LIMITS]") == 0)
905 Handle_LIMITS(line, var, arg);
906 else if(strcasecmp(section, "[OPTIONS]") == 0)
907 Handle_OPTIONS(line, var, arg);
908 else if(strcasecmp(section, "[OPERATOR]") == 0)
909 Handle_OPERATOR(line, var, arg);
910 else if(strcasecmp(section, "[SERVER]") == 0)
911 Handle_SERVER(line, var, arg);
912 else if(strcasecmp(section, "[CHANNEL]") == 0)
913 Handle_CHANNEL(line, var, arg);
915 Config_Error(LOG_ERR,
916 "%s, line %d: Variable \"%s\" outside section!",
917 NGIRCd_ConfFile, line, var);
920 /* Close configuration file */
923 /* Check if there is still a server to add */
924 if( New_Server.name[0] ) {
925 /* Copy data to "real" server structure */
926 assert( New_Server_Idx > NONE );
927 Conf_Server[New_Server_Idx] = New_Server;
930 /* not a single listening port? Add default. */
931 if (no_listenports() &&
932 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
934 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
935 (unsigned int) defaultport, strerror(errno));
940 if (!Conf_ListenAddress)
941 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
943 if (!Conf_ListenAddress) {
944 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
948 /* No MOTD phrase configured? (re)try motd file. */
949 if (array_bytes(&Conf_Motd) == 0)
950 Read_Motd(Conf_MotdFile);
953 /* Make sure that all SSL-related files are readable */
954 CheckFileReadable("SSLCertFile", Conf_SSLOptions.CertFile);
955 CheckFileReadable("SSLDHFile", Conf_SSLOptions.DHFile);
956 CheckFileReadable("SSLKeyFile", Conf_SSLOptions.KeyFile);
963 * Check whether an string argument is true or false.
965 * @param Arg Input string.
966 * @returns true if string has been parsed as "yes"/"true"/"on".
969 Check_ArgIsTrue( const char *Arg )
971 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
972 if( strcasecmp( Arg, "true" ) == 0 ) return true;
973 if( atoi( Arg ) != 0 ) return true;
979 * Handle setting of "MaxNickLength".
981 * @param Line Line number in configuration file.
982 * @raram Arg Input string.
983 * @returns New configured maximum nick name length.
986 Handle_MaxNickLength(int Line, const char *Arg)
990 new = (unsigned) atoi(Arg) + 1;
991 if (new > CLIENT_NICK_LEN) {
992 Config_Error(LOG_WARNING,
993 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
994 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
995 return CLIENT_NICK_LEN;
998 Config_Error(LOG_WARNING,
999 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
1000 NGIRCd_ConfFile, Line);
1007 * Output a warning messages if IDENT is configured but not compiled in.
1010 WarnIdent(int UNUSED Line)
1014 /* user has enabled ident lookups explicitly, but ... */
1015 Config_Error(LOG_WARNING,
1016 "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
1017 NGIRCd_ConfFile, Line);
1023 * Output a warning messages if IPv6 is configured but not compiled in.
1026 WarnIPv6(int UNUSED Line)
1029 if (Conf_ConnectIPv6) {
1030 /* user has enabled IPv6 explicitly, but ... */
1031 Config_Error(LOG_WARNING,
1032 "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
1033 NGIRCd_ConfFile, Line);
1039 * Output a warning messages if PAM is configured but not compiled in.
1042 WarnPAM(int UNUSED Line)
1046 Config_Error(LOG_WARNING,
1047 "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
1048 NGIRCd_ConfFile, Line);
1054 * Handle legacy "NoXXX" options in [GLOBAL] section.
1056 * TODO: This function and support for "NoXXX" could be removed starting
1057 * with ngIRCd release 19 (one release after marking it "deprecated").
1059 * @param Var Variable name.
1060 * @param Arg Argument string.
1061 * @returns true if a NoXXX option has been processed; false otherwise.
1064 CheckLegacyNoOption(const char *Var, const char *Arg)
1066 if(strcasecmp(Var, "NoDNS") == 0) {
1067 Conf_DNS = !Check_ArgIsTrue( Arg );
1070 if (strcasecmp(Var, "NoIdent") == 0) {
1071 Conf_Ident = !Check_ArgIsTrue(Arg);
1074 if(strcasecmp(Var, "NoPAM") == 0) {
1075 Conf_PAM = !Check_ArgIsTrue(Arg);
1082 * Handle deprecated legacy options in [GLOBAL] section.
1084 * TODO: This function and support for these options in the [Global] section
1085 * could be removed starting with ngIRCd release 19 (one release after
1086 * marking it "deprecated").
1088 * @param Var Variable name.
1089 * @param Arg Argument string.
1090 * @returns true if a legacy option has been processed; false otherwise.
1093 CheckLegacyGlobalOption(int Line, char *Var, char *Arg)
1095 if (strcasecmp(Var, "AllowRemoteOper") == 0
1096 || strcasecmp(Var, "ChrootDir") == 0
1097 || strcasecmp(Var, "ConnectIPv4") == 0
1098 || strcasecmp(Var, "ConnectIPv6") == 0
1099 || strcasecmp(Var, "OperCanUseMode") == 0
1100 || strcasecmp(Var, "OperServerMode") == 0
1101 || strcasecmp(Var, "PredefChannelsOnly") == 0
1103 || strcasecmp(Var, "SSLCertFile") == 0
1104 || strcasecmp(Var, "SSLDHFile") == 0
1105 || strcasecmp(Var, "SSLKeyFile") == 0
1106 || strcasecmp(Var, "SSLKeyFilePassword") == 0
1107 || strcasecmp(Var, "SSLPorts") == 0
1109 || strcasecmp(Var, "SyslogFacility") == 0
1110 || strcasecmp(Var, "WebircPassword") == 0) {
1111 Handle_OPTIONS(Line, Var, Arg);
1114 if (strcasecmp(Var, "ConnectRetry") == 0
1115 || strcasecmp(Var, "MaxConnections") == 0
1116 || strcasecmp(Var, "MaxConnectionsIP") == 0
1117 || strcasecmp(Var, "MaxJoins") == 0
1118 || strcasecmp(Var, "MaxNickLength") == 0
1119 || strcasecmp(Var, "PingTimeout") == 0
1120 || strcasecmp(Var, "PongTimeout") == 0) {
1121 Handle_LIMITS(Line, Var, Arg);
1129 * Strip "no" prefix of a string.
1131 * TODO: This function and support for "NoXXX" should be removed starting
1132 * with ngIRCd release 19! (One release after marking it "deprecated").
1134 * @param str Pointer to input string starting with "no".
1135 * @returns New pointer to string without "no" prefix.
1138 NoNo(const char *str)
1140 assert(strncasecmp("no", str, 2) == 0 && str[2]);
1145 * Invert "boolean" string.
1147 * TODO: This function and support for "NoXXX" should be removed starting
1148 * with ngIRCd release 19! (One release after marking it "deprecated").
1150 * @param arg "Boolean" input string.
1151 * @returns Pointer to inverted "boolean string".
1154 InvertArg(const char *arg)
1156 return yesno_to_str(!Check_ArgIsTrue(arg));
1160 * Handle variable in [Global] configuration section.
1162 * @param Line Line numer in configuration file.
1163 * @param Var Variable name.
1164 * @param Arg Variable argument.
1167 Handle_GLOBAL( int Line, char *Var, char *Arg )
1172 const char *section;
1175 assert(Var != NULL);
1176 assert(Arg != NULL);
1178 if (strcasecmp(Var, "Name") == 0) {
1179 len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
1180 if (len >= sizeof(Conf_ServerName))
1181 Config_Error_TooLong(Line, Var);
1184 if (strcasecmp(Var, "AdminInfo1") == 0) {
1185 len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
1186 if (len >= sizeof(Conf_ServerAdmin1))
1187 Config_Error_TooLong(Line, Var);
1190 if (strcasecmp(Var, "AdminInfo2") == 0) {
1191 len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
1192 if (len >= sizeof(Conf_ServerAdmin2))
1193 Config_Error_TooLong(Line, Var);
1196 if (strcasecmp(Var, "AdminEMail") == 0) {
1197 len = strlcpy(Conf_ServerAdminMail, Arg,
1198 sizeof(Conf_ServerAdminMail));
1199 if (len >= sizeof(Conf_ServerAdminMail))
1200 Config_Error_TooLong(Line, Var);
1203 if (strcasecmp(Var, "Info") == 0) {
1204 len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
1205 if (len >= sizeof(Conf_ServerInfo))
1206 Config_Error_TooLong(Line, Var);
1209 if (strcasecmp(Var, "Listen") == 0) {
1210 if (Conf_ListenAddress) {
1211 Config_Error(LOG_ERR,
1212 "Multiple Listen= options, ignoring: %s",
1216 Conf_ListenAddress = strdup_warn(Arg);
1217 /* If allocation fails, we're in trouble: we cannot ignore the
1218 * error -- otherwise ngircd would listen on all interfaces. */
1219 if (!Conf_ListenAddress) {
1220 Config_Error(LOG_ALERT,
1221 "%s exiting due to fatal errors!",
1227 if (strcasecmp(Var, "MotdFile") == 0) {
1228 len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
1229 if (len >= sizeof(Conf_MotdFile))
1230 Config_Error_TooLong(Line, Var);
1233 if (strcasecmp(Var, "MotdPhrase") == 0) {
1237 if (len >= LINE_LEN) {
1238 Config_Error_TooLong(Line, Var);
1241 if (!array_copyb(&Conf_Motd, Arg, len + 1))
1242 Config_Error(LOG_WARNING,
1243 "%s, line %d: Could not append MotdPhrase: %s",
1244 NGIRCd_ConfFile, Line, strerror(errno));
1245 Using_MotdFile = false;
1248 if(strcasecmp(Var, "Password") == 0) {
1249 len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
1250 if (len >= sizeof(Conf_ServerPwd))
1251 Config_Error_TooLong(Line, Var);
1254 if (strcasecmp(Var, "PidFile") == 0) {
1255 len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
1256 if (len >= sizeof(Conf_PidFile))
1257 Config_Error_TooLong(Line, Var);
1260 if (strcasecmp(Var, "Ports") == 0) {
1261 ports_parse(&Conf_ListenPorts, Line, Arg);
1264 if (strcasecmp(Var, "ServerGID") == 0) {
1265 grp = getgrnam(Arg);
1267 Conf_GID = grp->gr_gid;
1269 Conf_GID = (unsigned int)atoi(Arg);
1270 if (!Conf_GID && strcmp(Arg, "0"))
1271 Config_Error_NaN(Line, Var);
1275 if (strcasecmp(Var, "ServerUID") == 0) {
1276 pwd = getpwnam(Arg);
1278 Conf_UID = pwd->pw_uid;
1280 Conf_UID = (unsigned int)atoi(Arg);
1281 if (!Conf_UID && strcmp(Arg, "0"))
1282 Config_Error_NaN(Line, Var);
1287 if (CheckLegacyNoOption(Var, Arg)) {
1288 /* TODO: This function and support for "NoXXX" could be
1289 * be removed starting with ngIRCd release 19 (one release
1290 * after marking it "deprecated"). */
1291 Config_Error(LOG_WARNING,
1292 "%s, line %d (section \"Global\"): \"No\"-Prefix is deprecated, use \"%s = %s\" in [Options] section!",
1293 NGIRCd_ConfFile, Line, NoNo(Var), InvertArg(Arg));
1294 if (strcasecmp(Var, "NoIdent") == 0)
1296 else if (strcasecmp(Var, "NoPam") == 0)
1300 if ((section = CheckLegacyGlobalOption(Line, Var, Arg))) {
1301 /** TODO: This function and support for these options in the
1302 * [Global] section could be removed starting with ngIRCd
1303 * release 19 (one release after marking it "deprecated"). */
1304 Config_Error(LOG_WARNING,
1305 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s!",
1306 NGIRCd_ConfFile, Line, Var, section);
1310 Config_Error_Section(Line, Var, "Global");
1314 * Handle variable in [Limits] configuration section.
1316 * @param Line Line numer in configuration file.
1317 * @param Var Variable name.
1318 * @param Arg Variable argument.
1321 Handle_LIMITS(int Line, char *Var, char *Arg)
1324 assert(Var != NULL);
1325 assert(Arg != NULL);
1327 if (strcasecmp(Var, "ConnectRetry") == 0) {
1328 Conf_ConnectRetry = atoi(Arg);
1329 if (Conf_ConnectRetry < 5) {
1330 Config_Error(LOG_WARNING,
1331 "%s, line %d: Value of \"ConnectRetry\" too low!",
1332 NGIRCd_ConfFile, Line);
1333 Conf_ConnectRetry = 5;
1337 if (strcasecmp(Var, "MaxConnections") == 0) {
1338 Conf_MaxConnections = atol(Arg);
1339 if (!Conf_MaxConnections && strcmp(Arg, "0"))
1340 Config_Error_NaN(Line, Var);
1343 if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
1344 Conf_MaxConnectionsIP = atoi(Arg);
1345 if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
1346 Config_Error_NaN(Line, Var);
1349 if (strcasecmp(Var, "MaxJoins") == 0) {
1350 Conf_MaxJoins = atoi(Arg);
1351 if (!Conf_MaxJoins && strcmp(Arg, "0"))
1352 Config_Error_NaN(Line, Var);
1355 if (strcasecmp(Var, "MaxNickLength") == 0) {
1356 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1359 if (strcasecmp(Var, "PingTimeout") == 0) {
1360 Conf_PingTimeout = atoi(Arg);
1361 if (Conf_PingTimeout < 5) {
1362 Config_Error(LOG_WARNING,
1363 "%s, line %d: Value of \"PingTimeout\" too low!",
1364 NGIRCd_ConfFile, Line);
1365 Conf_PingTimeout = 5;
1369 if (strcasecmp(Var, "PongTimeout") == 0) {
1370 Conf_PongTimeout = atoi(Arg);
1371 if (Conf_PongTimeout < 5) {
1372 Config_Error(LOG_WARNING,
1373 "%s, line %d: Value of \"PongTimeout\" too low!",
1374 NGIRCd_ConfFile, Line);
1375 Conf_PongTimeout = 5;
1380 Config_Error_Section(Line, Var, "Limits");
1384 * Handle variable in [Options] configuration section.
1386 * @param Line Line numer in configuration file.
1387 * @param Var Variable name.
1388 * @param Arg Variable argument.
1391 Handle_OPTIONS(int Line, char *Var, char *Arg)
1396 assert(Var != NULL);
1397 assert(Arg != NULL);
1399 if (strcasecmp(Var, "AllowRemoteOper") == 0) {
1400 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1403 if (strcasecmp(Var, "ChrootDir") == 0) {
1404 len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
1405 if (len >= sizeof(Conf_Chroot))
1406 Config_Error_TooLong(Line, Var);
1409 if (strcasecmp(Var, "CloakHost") == 0) {
1410 len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
1411 if (len >= sizeof(Conf_CloakHost))
1412 Config_Error_TooLong(Line, Var);
1415 if (strcasecmp(Var, "CloakUserToNick") == 0) {
1416 Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
1419 if (strcasecmp(Var, "ConnectIPv6") == 0) {
1420 Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
1424 if (strcasecmp(Var, "ConnectIPv4") == 0) {
1425 Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
1428 if (strcasecmp(Var, "DNS") == 0) {
1429 Conf_DNS = Check_ArgIsTrue(Arg);
1432 if (strcasecmp(Var, "Ident") == 0) {
1433 Conf_Ident = Check_ArgIsTrue(Arg);
1437 if (strcasecmp(Var, "MorePrivacy") == 0) {
1438 Conf_MorePrivacy = Check_ArgIsTrue(Arg);
1441 if (strcasecmp(Var, "NoticeAuth") == 0) {
1442 Conf_NoticeAuth = Check_ArgIsTrue(Arg);
1445 if (strcasecmp(Var, "OperCanUseMode") == 0) {
1446 Conf_OperCanMode = Check_ArgIsTrue(Arg);
1449 if (strcasecmp(Var, "OperServerMode") == 0) {
1450 Conf_OperServerMode = Check_ArgIsTrue(Arg);
1453 if (strcasecmp(Var, "PAM") == 0) {
1454 Conf_PAM = Check_ArgIsTrue(Arg);
1458 if (strcasecmp(Var, "PredefChannelsOnly") == 0) {
1459 Conf_PredefChannelsOnly = Check_ArgIsTrue(Arg);
1463 if (strcasecmp(Var, "RequireAuthPing") == 0) {
1464 Conf_AuthPing = Check_ArgIsTrue(Arg);
1469 if (strcasecmp(Var, "SSLCertFile") == 0) {
1470 assert(Conf_SSLOptions.CertFile == NULL);
1471 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1474 if (strcasecmp(Var, "SSLDHFile") == 0) {
1475 assert(Conf_SSLOptions.DHFile == NULL);
1476 Conf_SSLOptions.DHFile = strdup_warn(Arg);
1479 if (strcasecmp(Var, "SSLKeyFile") == 0) {
1480 assert(Conf_SSLOptions.KeyFile == NULL);
1481 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1484 if (strcasecmp(Var, "SSLKeyFilePassword") == 0) {
1485 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1486 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1487 Config_Error(LOG_ERR,
1488 "%s, line %d (section \"Global\"): Could not copy %s: %s!",
1489 NGIRCd_ConfFile, Line, Var,
1493 if (strcasecmp(Var, "SSLPorts") == 0) {
1494 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1499 if (strcasecmp(Var, "SyslogFacility") == 0) {
1500 Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
1501 Conf_SyslogFacility);
1505 if (strcasecmp(Var, "WebircPassword") == 0) {
1506 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
1507 if (len >= sizeof(Conf_WebircPwd))
1508 Config_Error_TooLong(Line, Var);
1512 Config_Error_Section(Line, Var, "Options");
1516 * Handle variable in [Operator] configuration section.
1518 * @param Line Line numer in configuration file.
1519 * @param Var Variable name.
1520 * @param Arg Variable argument.
1523 Handle_OPERATOR( int Line, char *Var, char *Arg )
1526 struct Conf_Oper *op;
1529 assert( Var != NULL );
1530 assert( Arg != NULL );
1531 assert( Conf_Oper_Count > 0 );
1533 op = array_alloc(&Conf_Opers, sizeof(*op), Conf_Oper_Count - 1);
1535 Config_Error(LOG_ERR, "Could not allocate memory for operator (%d:%s = %s)", Line, Var, Arg);
1539 if (strcasecmp(Var, "Name") == 0) {
1540 /* Name of IRC operator */
1541 len = strlcpy(op->name, Arg, sizeof(op->name));
1542 if (len >= sizeof(op->name))
1543 Config_Error_TooLong(Line, Var);
1546 if (strcasecmp(Var, "Password") == 0) {
1547 /* Password of IRC operator */
1548 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1549 if (len >= sizeof(op->pwd))
1550 Config_Error_TooLong(Line, Var);
1553 if (strcasecmp(Var, "Mask") == 0) {
1555 return; /* Hostname already configured */
1556 op->mask = strdup_warn( Arg );
1560 Config_Error_Section(Line, Var, "Operator");
1564 * Handle variable in [Server] configuration section.
1566 * @param Line Line numer in configuration file.
1567 * @param Var Variable name.
1568 * @param Arg Variable argument.
1571 Handle_SERVER( int Line, char *Var, char *Arg )
1577 assert( Var != NULL );
1578 assert( Arg != NULL );
1580 /* Ignore server block if no space is left in server configuration structure */
1581 if( New_Server_Idx <= NONE ) return;
1583 if( strcasecmp( Var, "Host" ) == 0 ) {
1584 /* Hostname of the server */
1585 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1586 if (len >= sizeof( New_Server.host ))
1587 Config_Error_TooLong ( Line, Var );
1590 if( strcasecmp( Var, "Name" ) == 0 ) {
1591 /* Name of the server ("Nick"/"ID") */
1592 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1593 if (len >= sizeof( New_Server.name ))
1594 Config_Error_TooLong( Line, Var );
1597 if (strcasecmp(Var, "Bind") == 0) {
1598 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1601 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1602 NGIRCd_ConfFile, Line, Arg);
1605 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1606 /* Password of this server which is sent to the peer */
1608 Config_Error(LOG_ERR,
1609 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1610 NGIRCd_ConfFile, Line);
1612 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1613 if (len >= sizeof( New_Server.pwd_in ))
1614 Config_Error_TooLong( Line, Var );
1617 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1618 /* Passwort of the peer which must be received */
1619 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1620 if (len >= sizeof( New_Server.pwd_out ))
1621 Config_Error_TooLong( Line, Var );
1624 if( strcasecmp( Var, "Port" ) == 0 ) {
1625 /* Port to which this server should connect */
1627 if (port >= 0 && port < 0xFFFF)
1628 New_Server.port = (UINT16)port;
1630 Config_Error(LOG_ERR,
1631 "%s, line %d (section \"Server\"): Illegal port number %ld!",
1632 NGIRCd_ConfFile, Line, port );
1636 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1637 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1641 if( strcasecmp( Var, "Group" ) == 0 ) {
1643 New_Server.group = atoi( Arg );
1644 if (!New_Server.group && strcmp(Arg, "0"))
1645 Config_Error_NaN(Line, Var);
1648 if( strcasecmp( Var, "Passive" ) == 0 ) {
1649 if (Check_ArgIsTrue(Arg))
1650 New_Server.flags |= CONF_SFLAG_DISABLED;
1653 if (strcasecmp(Var, "ServiceMask") == 0) {
1654 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1655 sizeof(New_Server.svs_mask));
1656 if (len >= sizeof(New_Server.svs_mask))
1657 Config_Error_TooLong(Line, Var);
1661 Config_Error_Section(Line, Var, "Server");
1665 * Copy channel name into channel structure.
1667 * If the channel name is not valid because of a missing prefix ('#', '&'),
1668 * a default prefix of '#' will be added.
1670 * @param new_chan New already allocated channel structure.
1671 * @param name Name of the new channel.
1672 * @returns true on success, false otherwise.
1675 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1677 size_t size = sizeof(new_chan->name);
1678 char *dest = new_chan->name;
1680 if (!Channel_IsValidName(name)) {
1682 * maybe user forgot to add a '#'.
1683 * This is only here for user convenience.
1689 return size > strlcpy(dest, name, size);
1693 * Handle variable in [Channel] configuration section.
1695 * @param Line Line numer in configuration file.
1696 * @param Var Variable name.
1697 * @param Arg Variable argument.
1700 Handle_CHANNEL(int Line, char *Var, char *Arg)
1704 struct Conf_Channel *chan;
1707 assert( Var != NULL );
1708 assert( Arg != NULL );
1709 assert(Conf_Channel_Count > 0);
1711 chancount = Conf_Channel_Count - 1;
1713 chan = array_alloc(&Conf_Channels, sizeof(*chan), chancount);
1715 Config_Error(LOG_ERR, "Could not allocate memory for predefined channel (%d:%s = %s)", Line, Var, Arg);
1718 if (strcasecmp(Var, "Name") == 0) {
1719 if (!Handle_Channelname(chan, Arg))
1720 Config_Error_TooLong(Line, Var);
1723 if (strcasecmp(Var, "Modes") == 0) {
1725 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1726 if (len >= sizeof(chan->modes))
1727 Config_Error_TooLong( Line, Var );
1730 if( strcasecmp( Var, "Topic" ) == 0 ) {
1732 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1733 if (len >= sizeof(chan->topic))
1734 Config_Error_TooLong( Line, Var );
1737 if( strcasecmp( Var, "Key" ) == 0 ) {
1738 /* Initial Channel Key (mode k) */
1739 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1740 if (len >= sizeof(chan->key))
1741 Config_Error_TooLong(Line, Var);
1744 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1745 /* maximum user limit, mode l */
1746 chan->maxusers = (unsigned long) atol(Arg);
1747 if (!chan->maxusers && strcmp(Arg, "0"))
1748 Config_Error_NaN(Line, Var);
1751 if (strcasecmp(Var, "KeyFile") == 0) {
1753 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1754 if (len >= sizeof(chan->keyfile))
1755 Config_Error_TooLong(Line, Var);
1759 Config_Error_Section(Line, Var, "Channel");
1763 * Validate server configuration.
1765 * Please note that this function uses exit(1) on fatal errors and therefore
1766 * can result in ngIRCd terminating!
1768 * @param Configtest true if the daemon has been called with "--configtest".
1769 * @param Rehash true if re-reading configuration on runtime.
1770 * @returns true if configuration is valid.
1773 Validate_Config(bool Configtest, bool Rehash)
1775 /* Validate configuration settings. */
1778 int i, servers, servers_once;
1780 bool config_valid = true;
1783 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1784 ptr = Conf_ServerName;
1786 if (*ptr >= 'a' && *ptr <= 'z') continue;
1787 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1788 if (*ptr >= '0' && *ptr <= '9') continue;
1789 if (ptr > Conf_ServerName) {
1790 if (*ptr == '.' || *ptr == '-')
1793 Conf_ServerName[0] = '\0';
1797 if (!Conf_ServerName[0]) {
1798 /* No server name configured! */
1799 config_valid = false;
1800 Config_Error(LOG_ALERT,
1801 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1803 if (!Configtest && !Rehash) {
1804 Config_Error(LOG_ALERT,
1805 "%s exiting due to fatal errors!",
1811 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1812 /* No dot in server name! */
1813 config_valid = false;
1814 Config_Error(LOG_ALERT,
1815 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1818 Config_Error(LOG_ALERT,
1819 "%s exiting due to fatal errors!",
1826 if (!Conf_ServerAdminMail[0]) {
1827 /* No administrative contact configured! */
1828 config_valid = false;
1829 Config_Error(LOG_ALERT,
1830 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1833 Config_Error(LOG_ALERT,
1834 "%s exiting due to fatal errors!",
1841 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1842 && !Conf_ServerAdminMail[0]) {
1843 /* No administrative information configured! */
1844 Config_Error(LOG_WARNING,
1845 "No administrative information configured but required by RFC!");
1849 if (Conf_ServerPwd[0])
1850 Config_Error(LOG_ERR,
1851 "This server uses PAM, \"Password\" will be ignored!");
1855 servers = servers_once = 0;
1856 for (i = 0; i < MAX_SERVERS; i++) {
1857 if (Conf_Server[i].name[0]) {
1859 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1864 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1865 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1868 return config_valid;
1872 * Output "line too long" warning.
1874 * @param Line Line number in configuration file.
1875 * @param Item Affected variable name.
1878 Config_Error_TooLong ( const int Line, const char *Item )
1880 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1884 * Output "unknown variable" warning.
1886 * @param Line Line number in configuration file.
1887 * @param Item Affected variable name.
1888 * @param Section Section name.
1891 Config_Error_Section(const int Line, const char *Item, const char *Section)
1893 Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
1894 NGIRCd_ConfFile, Line, Section, Item);
1898 * Output "not a number" warning.
1900 * @param Line Line number in configuration file.
1901 * @param Item Affected variable name.
1904 Config_Error_NaN( const int Line, const char *Item )
1906 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1907 NGIRCd_ConfFile, Line, Item );
1911 * Output configuration error to console and/or logfile.
1913 * On runtime, the normal log functions of the daemon are used. But when
1914 * testing the configuration ("--configtest"), all messages go directly
1917 * @param Level Severity level of the message.
1918 * @param Format Format string; see printf() function.
1921 static void Config_Error( const int Level, const char *Format, ... )
1923 static void Config_Error( Level, Format, va_alist )
1929 char msg[MAX_LOG_MSG_LEN];
1932 assert( Format != NULL );
1935 va_start( ap, Format );
1939 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1943 if (Level <= LOG_WARNING)
1944 printf(" - %s\n", msg);
1948 Log(Level, "%s", msg);
1954 * Dump internal state of the "configuration module".
1957 Conf_DebugDump(void)
1961 Log(LOG_DEBUG, "Configured servers:");
1962 for (i = 0; i < MAX_SERVERS; i++) {
1963 if (! Conf_Server[i].name[0])
1966 " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
1967 Conf_Server[i].name, Conf_Server[i].host,
1968 Conf_Server[i].port, Conf_Server[i].lasttry,
1969 Conf_Server[i].group, Conf_Server[i].flags,
1970 Conf_Server[i].conn_id);
1977 * Initialize server configuration structur to default values.
1979 * @param Server Pointer to server structure to initialize.
1982 Init_Server_Struct( CONF_SERVER *Server )
1984 assert( Server != NULL );
1986 memset( Server, 0, sizeof (CONF_SERVER) );
1988 Server->group = NONE;
1989 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1991 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1993 Proc_InitStruct(&Server->res_stat);
1994 Server->conn_id = NONE;
1995 memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));