2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2012 Alexander Barton (alex@barton.de) and Contributors.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
16 * Configuration management (reading, parsing & validation)
35 #include <sys/types.h>
52 static bool Use_Log = true, Using_MotdFile = true;
53 static CONF_SERVER New_Server;
54 static int New_Server_Idx;
56 static char Conf_MotdFile[FNAME_LEN];
58 static void Set_Defaults PARAMS(( bool InitServers ));
59 static bool Read_Config PARAMS(( bool TestOnly, bool IsStarting ));
60 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
62 static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
63 static void Handle_LIMITS PARAMS(( int Line, char *Var, char *Arg ));
64 static void Handle_OPTIONS PARAMS(( int Line, char *Var, char *Arg ));
65 static void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
66 static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
67 static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
69 static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
71 static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
72 static void Config_Error_Section PARAMS(( const int Line, const char *Item,
73 const char *Section ));
74 static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
76 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
80 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
82 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
88 static void Handle_SSL PARAMS(( int Line, char *Var, char *Ark ));
90 struct SSLOptions Conf_SSLOptions;
93 * Initialize SSL configuration.
98 free(Conf_SSLOptions.KeyFile);
99 Conf_SSLOptions.KeyFile = NULL;
101 free(Conf_SSLOptions.CertFile);
102 Conf_SSLOptions.CertFile = NULL;
104 free(Conf_SSLOptions.DHFile);
105 Conf_SSLOptions.DHFile = NULL;
106 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
110 * Make sure that a configured file is readable.
112 * Currently, this function is only used for SSL-related options ...
114 * @param Var Configuration variable
115 * @param Filename Configured filename
118 CheckFileReadable(const char *Var, const char *Filename)
125 fp = fopen(Filename, "r");
129 Config_Error(LOG_ERR, "Can't read \"%s\" (\"%s\"): %s",
130 Filename, Var, strerror(errno));
137 * Duplicate string and warn on errors.
139 * @returns Pointer to string on success, NULL otherwise.
142 strdup_warn(const char *str)
144 char *ptr = strdup(str);
146 Config_Error(LOG_ERR,
147 "Could not allocate memory for string: %s", str);
152 * Output a comma separated list of ports (integer values).
159 len = array_length(a, sizeof(UINT16));
161 ports = (UINT16*) array_start(a);
162 printf("%u", (unsigned int) *ports);
165 printf(", %u", (unsigned int) *ports);
172 * Parse a comma separated string into an array of port numbers (integers).
175 ports_parse(array *a, int Line, char *Arg)
183 ptr = strtok( Arg, "," );
187 if (port > 0 && port < 0xFFFF) {
188 port16 = (UINT16) port;
189 if (!array_catb(a, (char*)&port16, sizeof port16))
190 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
191 NGIRCd_ConfFile, Line, port, strerror(errno));
193 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
194 NGIRCd_ConfFile, Line, port );
197 ptr = strtok( NULL, "," );
202 * Initialize configuration module.
207 Read_Config(false, true);
208 Validate_Config(false, false);
212 * "Rehash" (reload) server configuration.
214 * @returns true if configuration has been re-read, false on errors.
219 if (!Read_Config(false, false))
221 Validate_Config(false, true);
223 /* Update CLIENT structure of local server */
224 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
229 * Output a boolean value as "yes/no" string.
232 yesno_to_str(int boolean_value)
240 * Free all IRC operator configuration structures.
245 struct Conf_Oper *op;
248 len = array_length(&Conf_Opers, sizeof(*op));
249 op = array_start(&Conf_Opers);
254 array_free(&Conf_Opers);
258 * Output all IRC operator configuration structures.
263 struct Conf_Oper *op;
266 count = array_length(&Conf_Opers, sizeof(*op));
267 op = array_start(&Conf_Opers);
268 for (i = 0; i < count; i++, op++) {
273 printf(" Name = %s\n", op->name);
274 printf(" Password = %s\n", op->pwd);
275 printf(" Mask = %s\n\n", op->mask ? op->mask : "");
280 * Read configuration, validate and output it.
282 * This function waits for a keypress of the user when stdin/stdout are valid
283 * tty's ("you can read our nice message and we can read in your keypress").
285 * @return 0 on succes, 1 on failure(s); therefore the result code can
286 * directly be used by exit() when running "ngircd --configtest".
295 size_t predef_channel_count;
296 struct Conf_Channel *predef_chan;
300 if (!Read_Config(true, true))
303 config_valid = Validate_Config(true, false);
306 if(isatty(fileno(stdin)) && isatty(fileno(stdout))) {
307 puts("OK, press enter to see a dump of your server configuration ...");
310 puts("Ok, dump of your server configuration follows:\n");
313 printf(" Name = %s\n", Conf_ServerName);
314 printf(" AdminInfo1 = %s\n", Conf_ServerAdmin1);
315 printf(" AdminInfo2 = %s\n", Conf_ServerAdmin2);
316 printf(" AdminEMail = %s\n", Conf_ServerAdminMail);
317 printf(" Info = %s\n", Conf_ServerInfo);
318 printf(" Listen = %s\n", Conf_ListenAddress);
319 if (Using_MotdFile) {
320 printf(" MotdFile = %s\n", Conf_MotdFile);
321 printf(" MotdPhrase =\n");
323 printf(" MotdFile = \n");
324 printf(" MotdPhrase = %s\n", array_bytes(&Conf_Motd)
325 ? (const char*) array_start(&Conf_Motd) : "");
328 printf(" Password = %s\n", Conf_ServerPwd);
330 printf(" PidFile = %s\n", Conf_PidFile);
332 ports_puts(&Conf_ListenPorts);
333 grp = getgrgid(Conf_GID);
335 printf(" ServerGID = %s\n", grp->gr_name);
337 printf(" ServerGID = %ld\n", (long)Conf_GID);
338 pwd = getpwuid(Conf_UID);
340 printf(" ServerUID = %s\n", pwd->pw_name);
342 printf(" ServerUID = %ld\n", (long)Conf_UID);
346 printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
347 printf(" MaxConnections = %d\n", Conf_MaxConnections);
348 printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
349 printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
350 printf(" MaxNickLength = %u\n", Conf_MaxNickLength - 1);
351 printf(" PingTimeout = %d\n", Conf_PingTimeout);
352 printf(" PongTimeout = %d\n", Conf_PongTimeout);
356 printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
357 printf(" ChrootDir = %s\n", Conf_Chroot);
358 printf(" CloakHost = %s\n", Conf_CloakHost);
359 printf(" CloakHostModeX = %s\n", Conf_CloakHostModeX);
360 printf(" CloakHostSalt = %s\n", Conf_CloakHostSalt);
361 printf(" CloakUserToNick = %s\n", yesno_to_str(Conf_CloakUserToNick));
363 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
364 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
366 printf(" DNS = %s\n", yesno_to_str(Conf_DNS));
368 printf(" Ident = %s\n", yesno_to_str(Conf_Ident));
370 printf(" MorePrivacy = %s\n", yesno_to_str(Conf_MorePrivacy));
371 printf(" NoticeAuth = %s\n", yesno_to_str(Conf_NoticeAuth));
372 printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
373 printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
375 printf(" PAM = %s\n", yesno_to_str(Conf_PAM));
376 printf(" PAMIsOptional = %s\n", yesno_to_str(Conf_PAMIsOptional));
378 printf(" PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
380 printf(" RequireAuthPing = %s\n", yesno_to_str(Conf_AuthPing));
382 printf(" ScrubCTCP = %s\n", yesno_to_str(Conf_ScrubCTCP));
384 printf(" SyslogFacility = %s\n",
385 ngt_SyslogFacilityName(Conf_SyslogFacility));
387 printf(" WebircPassword = %s\n", Conf_WebircPwd);
392 printf(" CertFile = %s\n", Conf_SSLOptions.CertFile
393 ? Conf_SSLOptions.CertFile : "");
394 printf(" DHFile = %s\n", Conf_SSLOptions.DHFile
395 ? Conf_SSLOptions.DHFile : "");
396 printf(" KeyFile = %s\n", Conf_SSLOptions.KeyFile
397 ? Conf_SSLOptions.KeyFile : "");
398 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
399 puts(" KeyFilePassword = <secret>");
401 puts(" KeyFilePassword = ");
402 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
404 ports_puts(&Conf_SSLOptions.ListenPorts);
410 for( i = 0; i < MAX_SERVERS; i++ ) {
411 if( ! Conf_Server[i].name[0] ) continue;
413 /* Valid "Server" section */
415 printf( " Name = %s\n", Conf_Server[i].name );
416 printf( " Host = %s\n", Conf_Server[i].host );
417 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
419 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
421 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
422 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
423 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
424 printf( " Group = %d\n", Conf_Server[i].group );
425 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
428 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
429 predef_chan = array_start(&Conf_Channels);
431 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
432 if (!predef_chan->name[0])
435 /* Valid "Channel" section */
437 printf(" Name = %s\n", predef_chan->name);
438 printf(" Modes = %s\n", predef_chan->modes);
439 printf(" Key = %s\n", predef_chan->key);
440 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
441 printf(" Topic = %s\n", predef_chan->topic);
442 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
445 return (config_valid ? 0 : 1);
449 * Remove connection information from configured server.
451 * If the server is set as "once", delete it from our configuration;
452 * otherwise set the time for the next connection attempt.
454 * Non-server connections will be silently ignored.
457 Conf_UnsetServer( CONN_ID Idx )
462 /* Check all our configured servers */
463 for( i = 0; i < MAX_SERVERS; i++ ) {
464 if( Conf_Server[i].conn_id != Idx ) continue;
466 /* Gotcha! Mark server configuration as "unused": */
467 Conf_Server[i].conn_id = NONE;
469 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
470 /* Delete configuration here */
471 Init_Server_Struct( &Conf_Server[i] );
473 /* Set time for next connect attempt */
475 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
476 /* The connection has been "long", so we don't
477 * require the next attempt to be delayed. */
478 Conf_Server[i].lasttry =
479 t - Conf_ConnectRetry + RECONNECT_DELAY;
481 Conf_Server[i].lasttry = t;
487 * Set connection information for specified configured server.
490 Conf_SetServer( int ConfServer, CONN_ID Idx )
492 assert( ConfServer > NONE );
493 assert( Idx > NONE );
495 if (Conf_Server[ConfServer].conn_id > NONE &&
496 Conf_Server[ConfServer].conn_id != Idx) {
498 "Trying to update connection index for already registered server \"%s\": %d/%d - ignored.",
499 Conf_Server[ConfServer].name,
500 Conf_Server[ConfServer].conn_id, Idx);
503 Conf_Server[ConfServer].conn_id = Idx;
507 * Get index of server in configuration structure.
510 Conf_GetServer( CONN_ID Idx )
514 assert( Idx > NONE );
516 for( i = 0; i < MAX_SERVERS; i++ ) {
517 if( Conf_Server[i].conn_id == Idx ) return i;
523 * Enable a server by name and adjust its port number.
525 * @returns true if a server has been enabled and now has a valid port
526 * number and host name for outgoing connections.
529 Conf_EnableServer( const char *Name, UINT16 Port )
533 assert( Name != NULL );
534 for( i = 0; i < MAX_SERVERS; i++ ) {
535 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
536 /* Gotcha! Set port and enable server: */
537 Conf_Server[i].port = Port;
538 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
539 return (Conf_Server[i].port && Conf_Server[i].host[0]);
546 * Enable a server by name.
548 * The server is only usable as outgoing server, if it has set a valid port
549 * number for outgoing connections!
550 * If not, you have to use Conf_EnableServer() function to make it available.
552 * @returns true if a server has been enabled; false otherwise.
555 Conf_EnablePassiveServer(const char *Name)
559 assert( Name != NULL );
560 for (i = 0; i < MAX_SERVERS; i++) {
561 if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
562 && (Conf_Server[i].port > 0)) {
563 /* BINGO! Enable server */
564 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
572 * Disable a server by name.
573 * An already established connection will be disconnected.
575 * @returns true if a server was found and has been disabled.
578 Conf_DisableServer( const char *Name )
582 assert( Name != NULL );
583 for( i = 0; i < MAX_SERVERS; i++ ) {
584 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
585 /* Gotcha! Disable and disconnect server: */
586 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
587 if( Conf_Server[i].conn_id > NONE )
588 Conn_Close(Conf_Server[i].conn_id, NULL,
589 "Server link terminated on operator request",
598 * Add a new remote server to our configuration.
600 * @param Name Name of the new server.
601 * @param Port Port number to connect to or 0 for incoming connections.
602 * @param Host Host name to connect to.
603 * @param MyPwd Password that will be sent to the peer.
604 * @param PeerPwd Password that must be received from the peer.
605 * @returns true if the new server has been added; false otherwise.
608 Conf_AddServer(const char *Name, UINT16 Port, const char *Host,
609 const char *MyPwd, const char *PeerPwd)
613 assert( Name != NULL );
614 assert( Host != NULL );
615 assert( MyPwd != NULL );
616 assert( PeerPwd != NULL );
618 /* Search unused item in server configuration structure */
619 for( i = 0; i < MAX_SERVERS; i++ ) {
620 /* Is this item used? */
621 if( ! Conf_Server[i].name[0] ) break;
623 if( i >= MAX_SERVERS ) return false;
625 Init_Server_Struct( &Conf_Server[i] );
626 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
627 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
628 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
629 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
630 Conf_Server[i].port = Port;
631 Conf_Server[i].flags = CONF_SFLAG_ONCE;
637 * Check if the given nick name is reserved for services on a particular server.
639 * @param ConfServer The server index to check.
640 * @param Nick The nick name to check.
641 * @returns true if the given nick name belongs to an "IRC service".
644 Conf_NickIsService(int ConfServer, const char *Nick)
646 assert (ConfServer >= 0);
647 assert (ConfServer < MAX_SERVERS);
649 return MatchCaseInsensitiveList(Conf_Server[ConfServer].svs_mask,
654 * Check if the given nick name is blocked for "normal client" use.
656 * @param ConfServer The server index or NONE to check all configured servers.
657 * @param Nick The nick name to check.
658 * @returns true if the given nick name belongs to an "IRC service".
661 Conf_NickIsBlocked(const char *Nick)
665 for(i = 0; i < MAX_SERVERS; i++) {
666 if (!Conf_Server[i].name[0])
668 if (Conf_NickIsService(i, Nick))
675 * Initialize configuration settings with their default values.
678 Set_Defaults(bool InitServers)
681 char random[RANDOM_SALT_LEN + 1];
684 strcpy(Conf_ServerName, "");
685 strcpy(Conf_ServerAdmin1, "");
686 strcpy(Conf_ServerAdmin2, "");
687 strcpy(Conf_ServerAdminMail, "");
688 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
689 PACKAGE_NAME, PACKAGE_VERSION);
690 free(Conf_ListenAddress);
691 Conf_ListenAddress = NULL;
692 array_free(&Conf_Motd);
693 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
694 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
695 strcpy(Conf_ServerPwd, "");
696 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
697 Conf_UID = Conf_GID = 0;
700 Conf_ConnectRetry = 60;
701 Conf_MaxConnections = 0;
702 Conf_MaxConnectionsIP = 5;
704 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
705 Conf_PingTimeout = 120;
706 Conf_PongTimeout = 20;
709 Conf_AllowRemoteOper = false;
711 Conf_AuthPing = false;
713 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
714 strcpy(Conf_CloakHost, "");
715 strcpy(Conf_CloakHostModeX, "");
716 strcpy(Conf_CloakHostSalt, ngt_RandomStr(random, RANDOM_SALT_LEN));
717 Conf_CloakUserToNick = false;
718 Conf_ConnectIPv4 = true;
720 Conf_ConnectIPv6 = true;
722 Conf_ConnectIPv6 = false;
730 Conf_MorePrivacy = false;
731 Conf_NoticeAuth = false;
732 Conf_OperCanMode = false;
733 Conf_OperServerMode = false;
739 Conf_PAMIsOptional = false;
740 Conf_PredefChannelsOnly = false;
742 Conf_ScrubCTCP = false;
744 Conf_SyslogFacility = LOG_LOCAL5;
746 Conf_SyslogFacility = 0;
750 /* Initialize server configuration structures */
752 for (i = 0; i < MAX_SERVERS;
753 Init_Server_Struct(&Conf_Server[i++]));
758 * Get number of configured listening ports.
760 * @returns The number of ports (IPv4+IPv6) on which the server should listen.
765 size_t cnt = array_bytes(&Conf_ListenPorts);
767 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
773 * Read MOTD ("message of the day") file.
775 * @param filename Name of the file to read.
778 Read_Motd(const char *filename)
783 if (*filename == '\0')
786 fp = fopen(filename, "r");
788 Config_Error(LOG_WARNING, "Can't read MOTD file \"%s\": %s",
789 filename, strerror(errno));
793 array_free(&Conf_Motd);
794 Using_MotdFile = true;
796 while (fgets(line, (int)sizeof line, fp)) {
797 ngt_TrimLastChr( line, '\n');
799 /* add text including \0 */
800 if (!array_catb(&Conf_Motd, line, strlen(line) + 1)) {
801 Log(LOG_WARNING, "Cannot add MOTD text: %s", strerror(errno));
809 * Read ngIRCd configuration file.
811 * Please note that this function uses exit(1) on fatal errors and therefore
812 * can result in ngIRCd terminating!
814 * @param ngircd_starting Flag indicating if ngIRCd is starting or not.
815 * @returns true when the configuration file has been read
816 * successfully; false otherwise.
819 Read_Config(bool TestOnly, bool IsStarting)
821 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
822 const UINT16 defaultport = 6667;
827 /* Open configuration file */
828 fd = fopen( NGIRCd_ConfFile, "r" );
830 /* No configuration file found! */
831 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
832 NGIRCd_ConfFile, strerror( errno ));
835 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
840 Set_Defaults(IsStarting);
843 Config_Error(LOG_INFO,
844 "Reading configuration from \"%s\" ...",
847 /* Clean up server configuration structure: mark all already
848 * configured servers as "once" so that they are deleted
849 * after the next disconnect and delete all unused servers.
850 * And delete all servers which are "duplicates" of servers
851 * that are already marked as "once" (such servers have been
852 * created by the last rehash but are now useless). */
853 for( i = 0; i < MAX_SERVERS; i++ ) {
854 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
856 /* This structure is in use ... */
857 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
858 /* Check for duplicates */
859 for( n = 0; n < MAX_SERVERS; n++ ) {
860 if( n == i ) continue;
862 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
863 Init_Server_Struct( &Conf_Server[n] );
865 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
871 /* Mark server as "once" */
872 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
873 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
878 /* Initialize variables */
880 strcpy( section, "" );
881 Init_Server_Struct( &New_Server );
882 New_Server_Idx = NONE;
886 /* Read configuration file */
888 if( ! fgets( str, LINE_LEN, fd )) break;
892 /* Skip comments and empty lines */
893 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
895 /* Is this the beginning of a new section? */
896 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
897 strlcpy( section, str, sizeof( section ));
898 if (strcasecmp(section, "[GLOBAL]") == 0
899 || strcasecmp(section, "[LIMITS]") == 0
900 || strcasecmp(section, "[OPTIONS]") == 0
902 || strcasecmp(section, "[SSL]") == 0
907 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
908 /* Check if there is already a server to add */
909 if( New_Server.name[0] ) {
910 /* Copy data to "real" server structure */
911 assert( New_Server_Idx > NONE );
912 Conf_Server[New_Server_Idx] = New_Server;
915 /* Re-init structure for new server */
916 Init_Server_Struct( &New_Server );
918 /* Search unused item in server configuration structure */
919 for( i = 0; i < MAX_SERVERS; i++ ) {
920 /* Is this item used? */
921 if( ! Conf_Server[i].name[0] ) break;
923 if( i >= MAX_SERVERS ) {
924 /* Oops, no free item found! */
925 Config_Error( LOG_ERR, "Too many servers configured." );
926 New_Server_Idx = NONE;
928 else New_Server_Idx = i;
932 if (strcasecmp(section, "[CHANNEL]") == 0) {
933 count = array_length(&Conf_Channels,
934 sizeof(struct Conf_Channel));
935 if (!array_alloc(&Conf_Channels,
936 sizeof(struct Conf_Channel),
938 Config_Error(LOG_ERR,
939 "Could not allocate memory for new operator (line %d)",
945 if (strcasecmp(section, "[OPERATOR]") == 0) {
946 count = array_length(&Conf_Opers,
947 sizeof(struct Conf_Oper));
948 if (!array_alloc(&Conf_Opers,
949 sizeof(struct Conf_Oper),
951 Config_Error(LOG_ERR,
952 "Could not allocate memory for new channel (line &d)",
958 Config_Error(LOG_ERR,
959 "%s, line %d: Unknown section \"%s\"!",
960 NGIRCd_ConfFile, line, section);
963 if( section[0] == 0x1 ) continue;
965 /* Split line into variable name and parameters */
966 ptr = strchr( str, '=' );
968 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
972 var = str; ngt_TrimStr( var );
973 arg = ptr + 1; ngt_TrimStr( arg );
975 if(strcasecmp(section, "[GLOBAL]") == 0)
976 Handle_GLOBAL(line, var, arg);
977 else if(strcasecmp(section, "[LIMITS]") == 0)
978 Handle_LIMITS(line, var, arg);
979 else if(strcasecmp(section, "[OPTIONS]") == 0)
980 Handle_OPTIONS(line, var, arg);
982 else if(strcasecmp(section, "[SSL]") == 0)
983 Handle_SSL(line, var, arg);
985 else if(strcasecmp(section, "[OPERATOR]") == 0)
986 Handle_OPERATOR(line, var, arg);
987 else if(strcasecmp(section, "[SERVER]") == 0)
988 Handle_SERVER(line, var, arg);
989 else if(strcasecmp(section, "[CHANNEL]") == 0)
990 Handle_CHANNEL(line, var, arg);
992 Config_Error(LOG_ERR,
993 "%s, line %d: Variable \"%s\" outside section!",
994 NGIRCd_ConfFile, line, var);
997 /* Close configuration file */
1000 /* Check if there is still a server to add */
1001 if( New_Server.name[0] ) {
1002 /* Copy data to "real" server structure */
1003 assert( New_Server_Idx > NONE );
1004 Conf_Server[New_Server_Idx] = New_Server;
1007 /* not a single listening port? Add default. */
1008 if (no_listenports() &&
1009 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
1011 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
1012 (unsigned int) defaultport, strerror(errno));
1017 if (!Conf_ListenAddress)
1018 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
1020 if (!Conf_ListenAddress) {
1021 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
1025 /* No MOTD phrase configured? (re)try motd file. */
1026 if (array_bytes(&Conf_Motd) == 0)
1027 Read_Motd(Conf_MotdFile);
1030 /* Make sure that all SSL-related files are readable */
1031 CheckFileReadable("CertFile", Conf_SSLOptions.CertFile);
1032 CheckFileReadable("DHFile", Conf_SSLOptions.DHFile);
1033 CheckFileReadable("KeyFile", Conf_SSLOptions.KeyFile);
1040 * Check whether a string argument is "true" or "false".
1042 * @param Arg Input string.
1043 * @returns true if the input string has been parsed as "yes", "true"
1044 * (case insensitive) or a non-zero integer value.
1047 Check_ArgIsTrue(const char *Arg)
1049 if (strcasecmp(Arg, "yes") == 0)
1051 if (strcasecmp(Arg, "true") == 0)
1060 * Handle setting of "MaxNickLength".
1062 * @param Line Line number in configuration file.
1063 * @raram Arg Input string.
1064 * @returns New configured maximum nick name length.
1067 Handle_MaxNickLength(int Line, const char *Arg)
1071 new = (unsigned) atoi(Arg) + 1;
1072 if (new > CLIENT_NICK_LEN) {
1073 Config_Error(LOG_WARNING,
1074 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
1075 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
1076 return CLIENT_NICK_LEN;
1079 Config_Error(LOG_WARNING,
1080 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
1081 NGIRCd_ConfFile, Line);
1088 * Output a warning messages if IDENT is configured but not compiled in.
1091 WarnIdent(int UNUSED Line)
1095 /* user has enabled ident lookups explicitly, but ... */
1096 Config_Error(LOG_WARNING,
1097 "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
1098 NGIRCd_ConfFile, Line);
1104 * Output a warning messages if IPv6 is configured but not compiled in.
1107 WarnIPv6(int UNUSED Line)
1110 if (Conf_ConnectIPv6) {
1111 /* user has enabled IPv6 explicitly, but ... */
1112 Config_Error(LOG_WARNING,
1113 "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
1114 NGIRCd_ConfFile, Line);
1120 * Output a warning messages if PAM is configured but not compiled in.
1123 WarnPAM(int UNUSED Line)
1127 Config_Error(LOG_WARNING,
1128 "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
1129 NGIRCd_ConfFile, Line);
1135 * Handle legacy "NoXXX" options in [GLOBAL] section.
1137 * TODO: This function and support for "NoXXX" could be removed starting
1138 * with ngIRCd release 19 (one release after marking it "deprecated").
1140 * @param Var Variable name.
1141 * @param Arg Argument string.
1142 * @returns true if a NoXXX option has been processed; false otherwise.
1145 CheckLegacyNoOption(const char *Var, const char *Arg)
1147 if(strcasecmp(Var, "NoDNS") == 0) {
1148 Conf_DNS = !Check_ArgIsTrue( Arg );
1151 if (strcasecmp(Var, "NoIdent") == 0) {
1152 Conf_Ident = !Check_ArgIsTrue(Arg);
1155 if(strcasecmp(Var, "NoPAM") == 0) {
1156 Conf_PAM = !Check_ArgIsTrue(Arg);
1163 * Handle deprecated legacy options in [GLOBAL] section.
1165 * TODO: This function and support for these options in the [Global] section
1166 * could be removed starting with ngIRCd release 19 (one release after
1167 * marking it "deprecated").
1169 * @param Var Variable name.
1170 * @param Arg Argument string.
1171 * @returns true if a legacy option has been processed; false otherwise.
1174 CheckLegacyGlobalOption(int Line, char *Var, char *Arg)
1176 if (strcasecmp(Var, "AllowRemoteOper") == 0
1177 || strcasecmp(Var, "ChrootDir") == 0
1178 || strcasecmp(Var, "ConnectIPv4") == 0
1179 || strcasecmp(Var, "ConnectIPv6") == 0
1180 || strcasecmp(Var, "OperCanUseMode") == 0
1181 || strcasecmp(Var, "OperServerMode") == 0
1182 || strcasecmp(Var, "PredefChannelsOnly") == 0
1183 || strcasecmp(Var, "SyslogFacility") == 0
1184 || strcasecmp(Var, "WebircPassword") == 0) {
1185 Handle_OPTIONS(Line, Var, Arg);
1188 if (strcasecmp(Var, "ConnectRetry") == 0
1189 || strcasecmp(Var, "MaxConnections") == 0
1190 || strcasecmp(Var, "MaxConnectionsIP") == 0
1191 || strcasecmp(Var, "MaxJoins") == 0
1192 || strcasecmp(Var, "MaxNickLength") == 0
1193 || strcasecmp(Var, "PingTimeout") == 0
1194 || strcasecmp(Var, "PongTimeout") == 0) {
1195 Handle_LIMITS(Line, Var, Arg);
1199 if (strcasecmp(Var, "SSLCertFile") == 0
1200 || strcasecmp(Var, "SSLDHFile") == 0
1201 || strcasecmp(Var, "SSLKeyFile") == 0
1202 || strcasecmp(Var, "SSLKeyFilePassword") == 0
1203 || strcasecmp(Var, "SSLPorts") == 0) {
1204 Handle_SSL(Line, Var + 3, Arg);
1213 * Strip "no" prefix of a string.
1215 * TODO: This function and support for "NoXXX" should be removed starting
1216 * with ngIRCd release 19! (One release after marking it "deprecated").
1218 * @param str Pointer to input string starting with "no".
1219 * @returns New pointer to string without "no" prefix.
1222 NoNo(const char *str)
1224 assert(strncasecmp("no", str, 2) == 0 && str[2]);
1229 * Invert "boolean" string.
1231 * TODO: This function and support for "NoXXX" should be removed starting
1232 * with ngIRCd release 19! (One release after marking it "deprecated").
1234 * @param arg "Boolean" input string.
1235 * @returns Pointer to inverted "boolean string".
1238 InvertArg(const char *arg)
1240 return yesno_to_str(!Check_ArgIsTrue(arg));
1244 * Handle variable in [Global] configuration section.
1246 * @param Line Line numer in configuration file.
1247 * @param Var Variable name.
1248 * @param Arg Variable argument.
1251 Handle_GLOBAL( int Line, char *Var, char *Arg )
1256 const char *section;
1259 assert(Var != NULL);
1260 assert(Arg != NULL);
1262 if (strcasecmp(Var, "Name") == 0) {
1263 len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
1264 if (len >= sizeof(Conf_ServerName))
1265 Config_Error_TooLong(Line, Var);
1268 if (strcasecmp(Var, "AdminInfo1") == 0) {
1269 len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
1270 if (len >= sizeof(Conf_ServerAdmin1))
1271 Config_Error_TooLong(Line, Var);
1274 if (strcasecmp(Var, "AdminInfo2") == 0) {
1275 len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
1276 if (len >= sizeof(Conf_ServerAdmin2))
1277 Config_Error_TooLong(Line, Var);
1280 if (strcasecmp(Var, "AdminEMail") == 0) {
1281 len = strlcpy(Conf_ServerAdminMail, Arg,
1282 sizeof(Conf_ServerAdminMail));
1283 if (len >= sizeof(Conf_ServerAdminMail))
1284 Config_Error_TooLong(Line, Var);
1287 if (strcasecmp(Var, "Info") == 0) {
1288 len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
1289 if (len >= sizeof(Conf_ServerInfo))
1290 Config_Error_TooLong(Line, Var);
1293 if (strcasecmp(Var, "Listen") == 0) {
1294 if (Conf_ListenAddress) {
1295 Config_Error(LOG_ERR,
1296 "Multiple Listen= options, ignoring: %s",
1300 Conf_ListenAddress = strdup_warn(Arg);
1301 /* If allocation fails, we're in trouble: we cannot ignore the
1302 * error -- otherwise ngircd would listen on all interfaces. */
1303 if (!Conf_ListenAddress) {
1304 Config_Error(LOG_ALERT,
1305 "%s exiting due to fatal errors!",
1311 if (strcasecmp(Var, "MotdFile") == 0) {
1312 len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
1313 if (len >= sizeof(Conf_MotdFile))
1314 Config_Error_TooLong(Line, Var);
1317 if (strcasecmp(Var, "MotdPhrase") == 0) {
1321 if (len >= LINE_LEN) {
1322 Config_Error_TooLong(Line, Var);
1325 if (!array_copyb(&Conf_Motd, Arg, len + 1))
1326 Config_Error(LOG_WARNING,
1327 "%s, line %d: Could not append MotdPhrase: %s",
1328 NGIRCd_ConfFile, Line, strerror(errno));
1329 Using_MotdFile = false;
1332 if(strcasecmp(Var, "Password") == 0) {
1333 len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
1334 if (len >= sizeof(Conf_ServerPwd))
1335 Config_Error_TooLong(Line, Var);
1338 if (strcasecmp(Var, "PidFile") == 0) {
1339 len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
1340 if (len >= sizeof(Conf_PidFile))
1341 Config_Error_TooLong(Line, Var);
1344 if (strcasecmp(Var, "Ports") == 0) {
1345 ports_parse(&Conf_ListenPorts, Line, Arg);
1348 if (strcasecmp(Var, "ServerGID") == 0) {
1349 grp = getgrnam(Arg);
1351 Conf_GID = grp->gr_gid;
1353 Conf_GID = (unsigned int)atoi(Arg);
1354 if (!Conf_GID && strcmp(Arg, "0"))
1355 Config_Error(LOG_WARNING,
1356 "%s, line %d: Value of \"%s\" is not a valid group name or ID!",
1357 NGIRCd_ConfFile, Line, Var);
1361 if (strcasecmp(Var, "ServerUID") == 0) {
1362 pwd = getpwnam(Arg);
1364 Conf_UID = pwd->pw_uid;
1366 Conf_UID = (unsigned int)atoi(Arg);
1367 if (!Conf_UID && strcmp(Arg, "0"))
1368 Config_Error(LOG_WARNING,
1369 "%s, line %d: Value of \"%s\" is not a valid user name or ID!",
1370 NGIRCd_ConfFile, Line, Var);
1375 if (CheckLegacyNoOption(Var, Arg)) {
1376 /* TODO: This function and support for "NoXXX" could be
1377 * be removed starting with ngIRCd release 19 (one release
1378 * after marking it "deprecated"). */
1379 Config_Error(LOG_WARNING,
1380 "%s, line %d (section \"Global\"): \"No\"-Prefix is deprecated, use \"%s = %s\" in [Options] section!",
1381 NGIRCd_ConfFile, Line, NoNo(Var), InvertArg(Arg));
1382 if (strcasecmp(Var, "NoIdent") == 0)
1384 else if (strcasecmp(Var, "NoPam") == 0)
1388 if ((section = CheckLegacyGlobalOption(Line, Var, Arg))) {
1389 /** TODO: This function and support for these options in the
1390 * [Global] section could be removed starting with ngIRCd
1391 * release 19 (one release after marking it "deprecated"). */
1392 if (strncasecmp(Var, "SSL", 3) == 0) {
1393 Config_Error(LOG_WARNING,
1394 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s and rename to \"%s\"!",
1395 NGIRCd_ConfFile, Line, Var, section,
1398 Config_Error(LOG_WARNING,
1399 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s!",
1400 NGIRCd_ConfFile, Line, Var, section);
1405 Config_Error_Section(Line, Var, "Global");
1409 * Handle variable in [Limits] configuration section.
1411 * @param Line Line numer in configuration file.
1412 * @param Var Variable name.
1413 * @param Arg Variable argument.
1416 Handle_LIMITS(int Line, char *Var, char *Arg)
1419 assert(Var != NULL);
1420 assert(Arg != NULL);
1422 if (strcasecmp(Var, "ConnectRetry") == 0) {
1423 Conf_ConnectRetry = atoi(Arg);
1424 if (Conf_ConnectRetry < 5) {
1425 Config_Error(LOG_WARNING,
1426 "%s, line %d: Value of \"ConnectRetry\" too low!",
1427 NGIRCd_ConfFile, Line);
1428 Conf_ConnectRetry = 5;
1432 if (strcasecmp(Var, "MaxConnections") == 0) {
1433 Conf_MaxConnections = atoi(Arg);
1434 if (!Conf_MaxConnections && strcmp(Arg, "0"))
1435 Config_Error_NaN(Line, Var);
1438 if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
1439 Conf_MaxConnectionsIP = atoi(Arg);
1440 if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
1441 Config_Error_NaN(Line, Var);
1444 if (strcasecmp(Var, "MaxJoins") == 0) {
1445 Conf_MaxJoins = atoi(Arg);
1446 if (!Conf_MaxJoins && strcmp(Arg, "0"))
1447 Config_Error_NaN(Line, Var);
1450 if (strcasecmp(Var, "MaxNickLength") == 0) {
1451 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1454 if (strcasecmp(Var, "PingTimeout") == 0) {
1455 Conf_PingTimeout = atoi(Arg);
1456 if (Conf_PingTimeout < 5) {
1457 Config_Error(LOG_WARNING,
1458 "%s, line %d: Value of \"PingTimeout\" too low!",
1459 NGIRCd_ConfFile, Line);
1460 Conf_PingTimeout = 5;
1464 if (strcasecmp(Var, "PongTimeout") == 0) {
1465 Conf_PongTimeout = atoi(Arg);
1466 if (Conf_PongTimeout < 5) {
1467 Config_Error(LOG_WARNING,
1468 "%s, line %d: Value of \"PongTimeout\" too low!",
1469 NGIRCd_ConfFile, Line);
1470 Conf_PongTimeout = 5;
1475 Config_Error_Section(Line, Var, "Limits");
1479 * Handle variable in [Options] configuration section.
1481 * @param Line Line numer in configuration file.
1482 * @param Var Variable name.
1483 * @param Arg Variable argument.
1486 Handle_OPTIONS(int Line, char *Var, char *Arg)
1491 assert(Var != NULL);
1492 assert(Arg != NULL);
1494 if (strcasecmp(Var, "AllowRemoteOper") == 0) {
1495 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1498 if (strcasecmp(Var, "ChrootDir") == 0) {
1499 len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
1500 if (len >= sizeof(Conf_Chroot))
1501 Config_Error_TooLong(Line, Var);
1504 if (strcasecmp(Var, "CloakHost") == 0) {
1505 len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
1506 if (len >= sizeof(Conf_CloakHost))
1507 Config_Error_TooLong(Line, Var);
1510 if (strcasecmp(Var, "CloakHostModeX") == 0) {
1511 len = strlcpy(Conf_CloakHostModeX, Arg, sizeof(Conf_CloakHostModeX));
1512 if (len >= sizeof(Conf_CloakHostModeX))
1513 Config_Error_TooLong(Line, Var);
1516 if (strcasecmp(Var, "CloakHostSalt") == 0) {
1517 len = strlcpy(Conf_CloakHostSalt, Arg, sizeof(Conf_CloakHostSalt));
1518 if (len >= sizeof(Conf_CloakHostSalt))
1519 Config_Error_TooLong(Line, Var);
1522 if (strcasecmp(Var, "CloakUserToNick") == 0) {
1523 Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
1526 if (strcasecmp(Var, "ConnectIPv6") == 0) {
1527 Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
1531 if (strcasecmp(Var, "ConnectIPv4") == 0) {
1532 Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
1535 if (strcasecmp(Var, "DNS") == 0) {
1536 Conf_DNS = Check_ArgIsTrue(Arg);
1539 if (strcasecmp(Var, "Ident") == 0) {
1540 Conf_Ident = Check_ArgIsTrue(Arg);
1544 if (strcasecmp(Var, "MorePrivacy") == 0) {
1545 Conf_MorePrivacy = Check_ArgIsTrue(Arg);
1548 if (strcasecmp(Var, "NoticeAuth") == 0) {
1549 Conf_NoticeAuth = Check_ArgIsTrue(Arg);
1552 if (strcasecmp(Var, "OperCanUseMode") == 0) {
1553 Conf_OperCanMode = Check_ArgIsTrue(Arg);
1556 if (strcasecmp(Var, "OperServerMode") == 0) {
1557 Conf_OperServerMode = Check_ArgIsTrue(Arg);
1560 if (strcasecmp(Var, "PAM") == 0) {
1561 Conf_PAM = Check_ArgIsTrue(Arg);
1565 if (strcasecmp(Var, "PAMIsOptional") == 0 ) {
1566 Conf_PAMIsOptional = Check_ArgIsTrue(Arg);
1569 if (strcasecmp(Var, "PredefChannelsOnly") == 0) {
1570 Conf_PredefChannelsOnly = Check_ArgIsTrue(Arg);
1574 if (strcasecmp(Var, "RequireAuthPing") == 0) {
1575 Conf_AuthPing = Check_ArgIsTrue(Arg);
1579 if (strcasecmp(Var, "ScrubCTCP") == 0) {
1580 Conf_ScrubCTCP = Check_ArgIsTrue(Arg);
1584 if (strcasecmp(Var, "SyslogFacility") == 0) {
1585 Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
1586 Conf_SyslogFacility);
1590 if (strcasecmp(Var, "WebircPassword") == 0) {
1591 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
1592 if (len >= sizeof(Conf_WebircPwd))
1593 Config_Error_TooLong(Line, Var);
1597 Config_Error_Section(Line, Var, "Options");
1603 * Handle variable in [SSL] configuration section.
1605 * @param Line Line numer in configuration file.
1606 * @param Var Variable name.
1607 * @param Arg Variable argument.
1610 Handle_SSL(int Line, char *Var, char *Arg)
1613 assert(Var != NULL);
1614 assert(Arg != NULL);
1616 if (strcasecmp(Var, "CertFile") == 0) {
1617 assert(Conf_SSLOptions.CertFile == NULL);
1618 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1621 if (strcasecmp(Var, "DHFile") == 0) {
1622 assert(Conf_SSLOptions.DHFile == NULL);
1623 Conf_SSLOptions.DHFile = strdup_warn(Arg);
1626 if (strcasecmp(Var, "KeyFile") == 0) {
1627 assert(Conf_SSLOptions.KeyFile == NULL);
1628 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1631 if (strcasecmp(Var, "KeyFilePassword") == 0) {
1632 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1633 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1634 Config_Error(LOG_ERR,
1635 "%s, line %d (section \"SSL\"): Could not copy %s: %s!",
1636 NGIRCd_ConfFile, Line, Var,
1640 if (strcasecmp(Var, "Ports") == 0) {
1641 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1645 Config_Error_Section(Line, Var, "SSL");
1651 * Handle variable in [Operator] configuration section.
1653 * @param Line Line numer in configuration file.
1654 * @param Var Variable name.
1655 * @param Arg Variable argument.
1658 Handle_OPERATOR( int Line, char *Var, char *Arg )
1661 struct Conf_Oper *op;
1664 assert( Var != NULL );
1665 assert( Arg != NULL );
1667 op = array_get(&Conf_Opers, sizeof(*op),
1668 array_length(&Conf_Opers, sizeof(*op)) - 1);
1672 if (strcasecmp(Var, "Name") == 0) {
1673 /* Name of IRC operator */
1674 len = strlcpy(op->name, Arg, sizeof(op->name));
1675 if (len >= sizeof(op->name))
1676 Config_Error_TooLong(Line, Var);
1679 if (strcasecmp(Var, "Password") == 0) {
1680 /* Password of IRC operator */
1681 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1682 if (len >= sizeof(op->pwd))
1683 Config_Error_TooLong(Line, Var);
1686 if (strcasecmp(Var, "Mask") == 0) {
1688 return; /* Hostname already configured */
1689 op->mask = strdup_warn( Arg );
1693 Config_Error_Section(Line, Var, "Operator");
1697 * Handle variable in [Server] configuration section.
1699 * @param Line Line numer in configuration file.
1700 * @param Var Variable name.
1701 * @param Arg Variable argument.
1704 Handle_SERVER( int Line, char *Var, char *Arg )
1710 assert( Var != NULL );
1711 assert( Arg != NULL );
1713 /* Ignore server block if no space is left in server configuration structure */
1714 if( New_Server_Idx <= NONE ) return;
1716 if( strcasecmp( Var, "Host" ) == 0 ) {
1717 /* Hostname of the server */
1718 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1719 if (len >= sizeof( New_Server.host ))
1720 Config_Error_TooLong ( Line, Var );
1723 if( strcasecmp( Var, "Name" ) == 0 ) {
1724 /* Name of the server ("Nick"/"ID") */
1725 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1726 if (len >= sizeof( New_Server.name ))
1727 Config_Error_TooLong( Line, Var );
1730 if (strcasecmp(Var, "Bind") == 0) {
1731 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1734 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1735 NGIRCd_ConfFile, Line, Arg);
1738 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1739 /* Password of this server which is sent to the peer */
1741 Config_Error(LOG_ERR,
1742 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1743 NGIRCd_ConfFile, Line);
1745 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1746 if (len >= sizeof( New_Server.pwd_in ))
1747 Config_Error_TooLong( Line, Var );
1750 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1751 /* Passwort of the peer which must be received */
1752 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1753 if (len >= sizeof( New_Server.pwd_out ))
1754 Config_Error_TooLong( Line, Var );
1757 if( strcasecmp( Var, "Port" ) == 0 ) {
1758 /* Port to which this server should connect */
1760 if (port >= 0 && port < 0xFFFF)
1761 New_Server.port = (UINT16)port;
1763 Config_Error(LOG_ERR,
1764 "%s, line %d (section \"Server\"): Illegal port number %ld!",
1765 NGIRCd_ConfFile, Line, port );
1769 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1770 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1774 if( strcasecmp( Var, "Group" ) == 0 ) {
1776 New_Server.group = atoi( Arg );
1777 if (!New_Server.group && strcmp(Arg, "0"))
1778 Config_Error_NaN(Line, Var);
1781 if( strcasecmp( Var, "Passive" ) == 0 ) {
1782 if (Check_ArgIsTrue(Arg))
1783 New_Server.flags |= CONF_SFLAG_DISABLED;
1786 if (strcasecmp(Var, "ServiceMask") == 0) {
1787 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1788 sizeof(New_Server.svs_mask));
1789 if (len >= sizeof(New_Server.svs_mask))
1790 Config_Error_TooLong(Line, Var);
1794 Config_Error_Section(Line, Var, "Server");
1798 * Copy channel name into channel structure.
1800 * If the channel name is not valid because of a missing prefix ('#', '&'),
1801 * a default prefix of '#' will be added.
1803 * @param new_chan New already allocated channel structure.
1804 * @param name Name of the new channel.
1805 * @returns true on success, false otherwise.
1808 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1810 size_t size = sizeof(new_chan->name);
1811 char *dest = new_chan->name;
1813 if (!Channel_IsValidName(name)) {
1815 * maybe user forgot to add a '#'.
1816 * This is only here for user convenience.
1822 return size > strlcpy(dest, name, size);
1826 * Handle variable in [Channel] configuration section.
1828 * @param Line Line numer in configuration file.
1829 * @param Var Variable name.
1830 * @param Arg Variable argument.
1833 Handle_CHANNEL(int Line, char *Var, char *Arg)
1836 struct Conf_Channel *chan;
1839 assert( Var != NULL );
1840 assert( Arg != NULL );
1842 chan = array_get(&Conf_Channels, sizeof(*chan),
1843 array_length(&Conf_Channels, sizeof(*chan)) - 1);
1847 if (strcasecmp(Var, "Name") == 0) {
1848 if (!Handle_Channelname(chan, Arg))
1849 Config_Error_TooLong(Line, Var);
1852 if (strcasecmp(Var, "Modes") == 0) {
1854 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1855 if (len >= sizeof(chan->modes))
1856 Config_Error_TooLong( Line, Var );
1859 if( strcasecmp( Var, "Topic" ) == 0 ) {
1861 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1862 if (len >= sizeof(chan->topic))
1863 Config_Error_TooLong( Line, Var );
1866 if( strcasecmp( Var, "Key" ) == 0 ) {
1867 /* Initial Channel Key (mode k) */
1868 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1869 if (len >= sizeof(chan->key))
1870 Config_Error_TooLong(Line, Var);
1873 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1874 /* maximum user limit, mode l */
1875 chan->maxusers = (unsigned long) atol(Arg);
1876 if (!chan->maxusers && strcmp(Arg, "0"))
1877 Config_Error_NaN(Line, Var);
1880 if (strcasecmp(Var, "KeyFile") == 0) {
1882 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1883 if (len >= sizeof(chan->keyfile))
1884 Config_Error_TooLong(Line, Var);
1888 Config_Error_Section(Line, Var, "Channel");
1892 * Validate server configuration.
1894 * Please note that this function uses exit(1) on fatal errors and therefore
1895 * can result in ngIRCd terminating!
1897 * @param Configtest true if the daemon has been called with "--configtest".
1898 * @param Rehash true if re-reading configuration on runtime.
1899 * @returns true if configuration is valid.
1902 Validate_Config(bool Configtest, bool Rehash)
1904 /* Validate configuration settings. */
1907 int i, servers, servers_once;
1909 bool config_valid = true;
1912 /* Emit a warning when the config file is not a full path name */
1913 if (NGIRCd_ConfFile[0] && NGIRCd_ConfFile[0] != '/') {
1914 Config_Error(LOG_WARNING,
1915 "Not specifying a full path name to \"%s\" can cause problems when rehashing the server!",
1919 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1920 ptr = Conf_ServerName;
1922 if (*ptr >= 'a' && *ptr <= 'z') continue;
1923 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1924 if (*ptr >= '0' && *ptr <= '9') continue;
1925 if (ptr > Conf_ServerName) {
1926 if (*ptr == '.' || *ptr == '-')
1929 Conf_ServerName[0] = '\0';
1933 if (!Conf_ServerName[0]) {
1934 /* No server name configured! */
1935 config_valid = false;
1936 Config_Error(LOG_ALERT,
1937 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1939 if (!Configtest && !Rehash) {
1940 Config_Error(LOG_ALERT,
1941 "%s exiting due to fatal errors!",
1947 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1948 /* No dot in server name! */
1949 config_valid = false;
1950 Config_Error(LOG_ALERT,
1951 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1954 Config_Error(LOG_ALERT,
1955 "%s exiting due to fatal errors!",
1962 if (!Conf_ServerAdminMail[0]) {
1963 /* No administrative contact configured! */
1964 config_valid = false;
1965 Config_Error(LOG_ALERT,
1966 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1969 Config_Error(LOG_ALERT,
1970 "%s exiting due to fatal errors!",
1977 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1978 && !Conf_ServerAdminMail[0]) {
1979 /* No administrative information configured! */
1980 Config_Error(LOG_WARNING,
1981 "No administrative information configured but required by RFC!");
1985 if (Conf_ServerPwd[0])
1986 Config_Error(LOG_ERR,
1987 "This server uses PAM, \"Password\" in [Global] section will be ignored!");
1991 servers = servers_once = 0;
1992 for (i = 0; i < MAX_SERVERS; i++) {
1993 if (Conf_Server[i].name[0]) {
1995 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
2000 "Configuration: Operators=%ld, Servers=%d[%d], Channels=%ld",
2001 array_length(&Conf_Opers, sizeof(struct Conf_Oper)),
2002 servers, servers_once,
2003 array_length(&Conf_Channels, sizeof(struct Conf_Channel)));
2006 return config_valid;
2010 * Output "line too long" warning.
2012 * @param Line Line number in configuration file.
2013 * @param Item Affected variable name.
2016 Config_Error_TooLong ( const int Line, const char *Item )
2018 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
2022 * Output "unknown variable" warning.
2024 * @param Line Line number in configuration file.
2025 * @param Item Affected variable name.
2026 * @param Section Section name.
2029 Config_Error_Section(const int Line, const char *Item, const char *Section)
2031 Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
2032 NGIRCd_ConfFile, Line, Section, Item);
2036 * Output "not a number" warning.
2038 * @param Line Line number in configuration file.
2039 * @param Item Affected variable name.
2042 Config_Error_NaN( const int Line, const char *Item )
2044 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
2045 NGIRCd_ConfFile, Line, Item );
2049 * Output configuration error to console and/or logfile.
2051 * On runtime, the normal log functions of the daemon are used. But when
2052 * testing the configuration ("--configtest"), all messages go directly
2055 * @param Level Severity level of the message.
2056 * @param Format Format string; see printf() function.
2059 static void Config_Error( const int Level, const char *Format, ... )
2061 static void Config_Error( Level, Format, va_alist )
2067 char msg[MAX_LOG_MSG_LEN];
2070 assert( Format != NULL );
2073 va_start( ap, Format );
2077 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
2081 if (Level <= LOG_WARNING)
2082 printf(" - %s\n", msg);
2086 Log(Level, "%s", msg);
2092 * Dump internal state of the "configuration module".
2095 Conf_DebugDump(void)
2099 Log(LOG_DEBUG, "Configured servers:");
2100 for (i = 0; i < MAX_SERVERS; i++) {
2101 if (! Conf_Server[i].name[0])
2104 " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
2105 Conf_Server[i].name, Conf_Server[i].host,
2106 Conf_Server[i].port, Conf_Server[i].lasttry,
2107 Conf_Server[i].group, Conf_Server[i].flags,
2108 Conf_Server[i].conn_id);
2115 * Initialize server configuration structur to default values.
2117 * @param Server Pointer to server structure to initialize.
2120 Init_Server_Struct( CONF_SERVER *Server )
2122 assert( Server != NULL );
2124 memset( Server, 0, sizeof (CONF_SERVER) );
2126 Server->group = NONE;
2127 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
2129 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
2131 Proc_InitStruct(&Server->res_stat);
2132 Server->conn_id = NONE;
2133 memset(&Server->bind_addr, 0, sizeof(Server->bind_addr));