2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2011 Alexander Barton (alex@barton.de) and Contributors.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
16 * Configuration management (reading, parsing & validation)
34 #include <sys/types.h>
54 static bool Use_Log = true, Using_MotdFile = true;
55 static CONF_SERVER New_Server;
56 static int New_Server_Idx;
58 static size_t Conf_Oper_Count;
59 static size_t Conf_Channel_Count;
60 static char Conf_MotdFile[FNAME_LEN];
62 static void Set_Defaults PARAMS(( bool InitServers ));
63 static bool Read_Config PARAMS(( bool ngircd_starting ));
64 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
66 static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
67 static void Handle_LIMITS PARAMS(( int Line, char *Var, char *Arg ));
68 static void Handle_OPTIONS PARAMS(( int Line, char *Var, char *Arg ));
69 static void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
70 static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
71 static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
73 static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
75 static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
76 static void Config_Error_Section PARAMS(( const int Line, const char *Item,
77 const char *Section ));
78 static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
80 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
84 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
86 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
92 static void Handle_SSL PARAMS(( int Line, char *Var, char *Ark ));
94 struct SSLOptions Conf_SSLOptions;
97 * Initialize SSL configuration.
102 free(Conf_SSLOptions.KeyFile);
103 Conf_SSLOptions.KeyFile = NULL;
105 free(Conf_SSLOptions.CertFile);
106 Conf_SSLOptions.CertFile = NULL;
108 free(Conf_SSLOptions.DHFile);
109 Conf_SSLOptions.DHFile = NULL;
110 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
114 * Make sure that a configured file is readable.
116 * Currently, this function is only used for SSL-related options ...
118 * @param Var Configuration variable
119 * @param Filename Configured filename
122 CheckFileReadable(const char *Var, const char *Filename)
129 fp = fopen(Filename, "r");
133 Config_Error(LOG_ERR, "Can't read \"%s\" (\"%s\"): %s",
134 Filename, Var, strerror(errno));
141 * Duplicate string and warn on errors.
143 * @returns Pointer to string on success, NULL otherwise.
146 strdup_warn(const char *str)
148 char *ptr = strdup(str);
150 Config_Error(LOG_ERR,
151 "Could not allocate memory for string: %s", str);
156 * Output a comma separated list of ports (integer values).
163 len = array_length(a, sizeof(UINT16));
165 ports = (UINT16*) array_start(a);
166 printf("%u", (unsigned int) *ports);
169 printf(", %u", (unsigned int) *ports);
176 * Parse a comma separated string into an array of port numbers (integers).
179 ports_parse(array *a, int Line, char *Arg)
187 ptr = strtok( Arg, "," );
191 if (port > 0 && port < 0xFFFF) {
192 port16 = (UINT16) port;
193 if (!array_catb(a, (char*)&port16, sizeof port16))
194 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
195 NGIRCd_ConfFile, Line, port, strerror(errno));
197 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
198 NGIRCd_ConfFile, Line, port );
201 ptr = strtok( NULL, "," );
206 * Initialize configuration module.
212 Validate_Config(false, false);
216 * "Rehash" (reload) server configuration.
218 * @returns true if configuration has been re-read, false on errors.
223 if (!Read_Config(false))
225 Validate_Config(false, true);
227 /* Update CLIENT structure of local server */
228 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
233 * Output a boolean value as "yes/no" string.
236 yesno_to_str(int boolean_value)
244 * Free all IRC operator configuration structures.
249 struct Conf_Oper *op;
252 len = array_length(&Conf_Opers, sizeof(*op));
253 op = array_start(&Conf_Opers);
258 array_free(&Conf_Opers);
262 * Output all IRC operator configuration structures.
267 struct Conf_Oper *op;
270 len = array_length(&Conf_Opers, sizeof(*op));
271 op = array_start(&Conf_Opers);
276 printf(" Name = %s\n", op->name);
277 printf(" Password = %s\n", op->pwd);
278 printf(" Mask = %s\n\n", op->mask ? op->mask : "");
284 * Read configuration, validate and output it.
286 * This function waits for a keypress of the user when stdin/stdout are valid
287 * tty's ("you can read our nice message and we can read in your keypress").
289 * @return 0 on succes, 1 on failure(s); therefore the result code can
290 * directly be used by exit() when running "ngircd --configtest".
299 size_t predef_channel_count;
300 struct Conf_Channel *predef_chan;
304 if (! Read_Config(true))
307 config_valid = Validate_Config(true, false);
310 if(isatty(fileno(stdin)) && isatty(fileno(stdout))) {
311 puts("OK, press enter to see a dump of your server configuration ...");
314 puts("Ok, dump of your server configuration follows:\n");
317 printf(" Name = %s\n", Conf_ServerName);
318 printf(" AdminInfo1 = %s\n", Conf_ServerAdmin1);
319 printf(" AdminInfo2 = %s\n", Conf_ServerAdmin2);
320 printf(" AdminEMail = %s\n", Conf_ServerAdminMail);
321 printf(" Info = %s\n", Conf_ServerInfo);
322 printf(" Listen = %s\n", Conf_ListenAddress);
323 if (Using_MotdFile) {
324 printf(" MotdFile = %s\n", Conf_MotdFile);
325 printf(" MotdPhrase =\n");
327 printf(" MotdFile = \n");
328 printf(" MotdPhrase = %s\n", array_bytes(&Conf_Motd)
329 ? (const char*) array_start(&Conf_Motd) : "");
332 printf(" Password = %s\n", Conf_ServerPwd);
334 printf(" PidFile = %s\n", Conf_PidFile);
336 ports_puts(&Conf_ListenPorts);
337 grp = getgrgid(Conf_GID);
339 printf(" ServerGID = %s\n", grp->gr_name);
341 printf(" ServerGID = %ld\n", (long)Conf_GID);
342 pwd = getpwuid(Conf_UID);
344 printf(" ServerUID = %s\n", pwd->pw_name);
346 printf(" ServerUID = %ld\n", (long)Conf_UID);
350 printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
351 printf(" MaxConnections = %ld\n", Conf_MaxConnections);
352 printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
353 printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
354 printf(" MaxNickLength = %u\n", Conf_MaxNickLength - 1);
355 printf(" PingTimeout = %d\n", Conf_PingTimeout);
356 printf(" PongTimeout = %d\n", Conf_PongTimeout);
360 printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
361 printf(" ChrootDir = %s\n", Conf_Chroot);
362 printf(" CloakHost = %s\n", Conf_CloakHost);
363 printf(" CloakUserToNick = %s\n", yesno_to_str(Conf_CloakUserToNick));
365 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
366 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
368 printf(" DNS = %s\n", yesno_to_str(Conf_DNS));
370 printf(" Ident = %s\n", yesno_to_str(Conf_Ident));
372 printf(" NoticeAuth = %s\n", yesno_to_str(Conf_NoticeAuth));
373 printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
374 printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
376 printf(" PAM = %s\n", yesno_to_str(Conf_PAM));
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 Conf_Server[ConfServer].conn_id = Idx;
499 * Get index of server in configuration structure.
502 Conf_GetServer( CONN_ID Idx )
506 assert( Idx > NONE );
508 for( i = 0; i < MAX_SERVERS; i++ ) {
509 if( Conf_Server[i].conn_id == Idx ) return i;
515 * Enable a server by name and adjust its port number.
517 * @returns true if a server has been enabled and now has a valid port
518 * number and host name for outgoing connections.
521 Conf_EnableServer( const char *Name, UINT16 Port )
525 assert( Name != NULL );
526 for( i = 0; i < MAX_SERVERS; i++ ) {
527 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
528 /* Gotcha! Set port and enable server: */
529 Conf_Server[i].port = Port;
530 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
531 return (Conf_Server[i].port && Conf_Server[i].host[0]);
538 * Enable a server by name.
540 * The server is only usable as outgoing server, if it has set a valid port
541 * number for outgoing connections!
542 * If not, you have to use Conf_EnableServer() function to make it available.
544 * @returns true if a server has been enabled; false otherwise.
547 Conf_EnablePassiveServer(const char *Name)
551 assert( Name != NULL );
552 for (i = 0; i < MAX_SERVERS; i++) {
553 if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
554 && (Conf_Server[i].port > 0)) {
555 /* BINGO! Enable server */
556 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
564 * Disable a server by name.
565 * An already established connection will be disconnected.
567 * @returns true if a server was found and has been disabled.
570 Conf_DisableServer( const char *Name )
574 assert( Name != NULL );
575 for( i = 0; i < MAX_SERVERS; i++ ) {
576 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
577 /* Gotcha! Disable and disconnect server: */
578 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
579 if( Conf_Server[i].conn_id > NONE )
580 Conn_Close(Conf_Server[i].conn_id, NULL,
581 "Server link terminated on operator request",
590 * Add a new remote server to our configuration.
592 * @param Name Name of the new server.
593 * @param Port Port number to connect to or 0 for incoming connections.
594 * @param Host Host name to connect to.
595 * @param MyPwd Password that will be sent to the peer.
596 * @param PeerPwd Password that must be received from the peer.
597 * @returns true if the new server has been added; false otherwise.
600 Conf_AddServer(const char *Name, UINT16 Port, const char *Host,
601 const char *MyPwd, const char *PeerPwd)
605 assert( Name != NULL );
606 assert( Host != NULL );
607 assert( MyPwd != NULL );
608 assert( PeerPwd != NULL );
610 /* Search unused item in server configuration structure */
611 for( i = 0; i < MAX_SERVERS; i++ ) {
612 /* Is this item used? */
613 if( ! Conf_Server[i].name[0] ) break;
615 if( i >= MAX_SERVERS ) return false;
617 Init_Server_Struct( &Conf_Server[i] );
618 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
619 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
620 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
621 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
622 Conf_Server[i].port = Port;
623 Conf_Server[i].flags = CONF_SFLAG_ONCE;
629 * Check if the given nick name is an service.
631 * @returns true if the given nick name belongs to an "IRC service".
634 Conf_IsService(int ConfServer, const char *Nick)
636 return MatchCaseInsensitive(Conf_Server[ConfServer].svs_mask, Nick);
640 * Initialize configuration settings with their default values.
643 Set_Defaults(bool InitServers)
648 strcpy(Conf_ServerName, "");
649 strcpy(Conf_ServerAdmin1, "");
650 strcpy(Conf_ServerAdmin2, "");
651 strcpy(Conf_ServerAdminMail, "");
652 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
653 PACKAGE_NAME, PACKAGE_VERSION);
654 free(Conf_ListenAddress);
655 Conf_ListenAddress = NULL;
656 array_free(&Conf_Motd);
657 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
658 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
659 strcpy(Conf_ServerPwd, "");
660 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
661 Conf_UID = Conf_GID = 0;
664 Conf_ConnectRetry = 60;
665 Conf_MaxConnections = 0;
666 Conf_MaxConnectionsIP = 5;
668 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
669 Conf_PingTimeout = 120;
670 Conf_PongTimeout = 20;
673 Conf_AllowRemoteOper = false;
675 Conf_AuthPing = false;
677 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
678 strcpy(Conf_CloakHost, "");
679 Conf_CloakUserToNick = false;
680 Conf_ConnectIPv4 = true;
682 Conf_ConnectIPv6 = true;
684 Conf_ConnectIPv6 = false;
692 Conf_NoticeAuth = false;
693 Conf_OperCanMode = false;
694 Conf_OperServerMode = false;
700 Conf_PredefChannelsOnly = false;
702 Conf_ScrubCTCP = false;
704 Conf_SyslogFacility = LOG_LOCAL5;
706 Conf_SyslogFacility = 0;
710 /* Initialize IRC operators and channels */
712 Conf_Channel_Count = 0;
714 /* Initialize server configuration structures */
716 for (i = 0; i < MAX_SERVERS;
717 Init_Server_Struct(&Conf_Server[i++]));
722 * Get number of configured listening ports.
724 * @returns The number of ports (IPv4+IPv6) on which the server should listen.
729 size_t cnt = array_bytes(&Conf_ListenPorts);
731 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
737 * Read MOTD ("message of the day") file.
739 * @param filename Name of the file to read.
742 Read_Motd(const char *filename)
747 if (*filename == '\0')
750 fp = fopen(filename, "r");
752 Config_Error(LOG_WARNING, "Can't read MOTD file \"%s\": %s",
753 filename, strerror(errno));
757 array_free(&Conf_Motd);
758 Using_MotdFile = true;
760 while (fgets(line, (int)sizeof line, fp)) {
761 ngt_TrimLastChr( line, '\n');
763 /* add text including \0 */
764 if (!array_catb(&Conf_Motd, line, strlen(line) + 1)) {
765 Log(LOG_WARNING, "Cannot add MOTD text: %s", strerror(errno));
773 * Read ngIRCd configuration file.
775 * Please note that this function uses exit(1) on fatal errors and therefore
776 * can result in ngIRCd terminating!
778 * @param ngircd_starting Flag indicating if ngIRCd is starting or not.
779 * @returns true when the configuration file has been read
780 * successfully; false otherwise.
783 Read_Config( bool ngircd_starting )
785 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
786 const UINT16 defaultport = 6667;
790 /* Open configuration file */
791 fd = fopen( NGIRCd_ConfFile, "r" );
793 /* No configuration file found! */
794 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
795 NGIRCd_ConfFile, strerror( errno ));
796 if (!ngircd_starting)
798 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
803 Set_Defaults( ngircd_starting );
805 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
807 /* Clean up server configuration structure: mark all already
808 * configured servers as "once" so that they are deleted
809 * after the next disconnect and delete all unused servers.
810 * And delete all servers which are "duplicates" of servers
811 * that are already marked as "once" (such servers have been
812 * created by the last rehash but are now useless). */
813 for( i = 0; i < MAX_SERVERS; i++ ) {
814 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
816 /* This structure is in use ... */
817 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
818 /* Check for duplicates */
819 for( n = 0; n < MAX_SERVERS; n++ ) {
820 if( n == i ) continue;
822 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
823 Init_Server_Struct( &Conf_Server[n] );
825 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
831 /* Mark server as "once" */
832 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
833 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
838 /* Initialize variables */
840 strcpy( section, "" );
841 Init_Server_Struct( &New_Server );
842 New_Server_Idx = NONE;
846 /* Read configuration file */
848 if( ! fgets( str, LINE_LEN, fd )) break;
852 /* Skip comments and empty lines */
853 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
855 /* Is this the beginning of a new section? */
856 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
857 strlcpy( section, str, sizeof( section ));
858 if (strcasecmp(section, "[GLOBAL]") == 0 ||
859 strcasecmp(section, "[LIMITS]") == 0 ||
860 strcasecmp(section, "[OPTIONS]") == 0 ||
861 strcasecmp(section, "[SSL]") == 0)
864 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
865 /* Check if there is already a server to add */
866 if( New_Server.name[0] ) {
867 /* Copy data to "real" server structure */
868 assert( New_Server_Idx > NONE );
869 Conf_Server[New_Server_Idx] = New_Server;
872 /* Re-init structure for new server */
873 Init_Server_Struct( &New_Server );
875 /* Search unused item in server configuration structure */
876 for( i = 0; i < MAX_SERVERS; i++ ) {
877 /* Is this item used? */
878 if( ! Conf_Server[i].name[0] ) break;
880 if( i >= MAX_SERVERS ) {
881 /* Oops, no free item found! */
882 Config_Error( LOG_ERR, "Too many servers configured." );
883 New_Server_Idx = NONE;
885 else New_Server_Idx = i;
888 if (strcasecmp(section, "[CHANNEL]") == 0) {
889 Conf_Channel_Count++;
892 if (strcasecmp(section, "[OPERATOR]") == 0) {
897 Config_Error(LOG_ERR,
898 "%s, line %d: Unknown section \"%s\"!",
899 NGIRCd_ConfFile, line, section);
902 if( section[0] == 0x1 ) continue;
904 /* Split line into variable name and parameters */
905 ptr = strchr( str, '=' );
907 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
911 var = str; ngt_TrimStr( var );
912 arg = ptr + 1; ngt_TrimStr( arg );
914 if(strcasecmp(section, "[GLOBAL]") == 0)
915 Handle_GLOBAL(line, var, arg);
916 else if(strcasecmp(section, "[LIMITS]") == 0)
917 Handle_LIMITS(line, var, arg);
918 else if(strcasecmp(section, "[OPTIONS]") == 0)
919 Handle_OPTIONS(line, var, arg);
921 else if(strcasecmp(section, "[SSL]") == 0)
922 Handle_SSL(line, var, arg);
924 else if(strcasecmp(section, "[OPERATOR]") == 0)
925 Handle_OPERATOR(line, var, arg);
926 else if(strcasecmp(section, "[SERVER]") == 0)
927 Handle_SERVER(line, var, arg);
928 else if(strcasecmp(section, "[CHANNEL]") == 0)
929 Handle_CHANNEL(line, var, arg);
931 Config_Error(LOG_ERR,
932 "%s, line %d: Variable \"%s\" outside section!",
933 NGIRCd_ConfFile, line, var);
936 /* Close configuration file */
939 /* Check if there is still a server to add */
940 if( New_Server.name[0] ) {
941 /* Copy data to "real" server structure */
942 assert( New_Server_Idx > NONE );
943 Conf_Server[New_Server_Idx] = New_Server;
946 /* not a single listening port? Add default. */
947 if (no_listenports() &&
948 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
950 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
951 (unsigned int) defaultport, strerror(errno));
956 if (!Conf_ListenAddress)
957 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
959 if (!Conf_ListenAddress) {
960 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
964 /* No MOTD phrase configured? (re)try motd file. */
965 if (array_bytes(&Conf_Motd) == 0)
966 Read_Motd(Conf_MotdFile);
969 /* Make sure that all SSL-related files are readable */
970 CheckFileReadable("CertFile", Conf_SSLOptions.CertFile);
971 CheckFileReadable("DHFile", Conf_SSLOptions.DHFile);
972 CheckFileReadable("KeyFile", Conf_SSLOptions.KeyFile);
979 * Check whether an string argument is true or false.
981 * @param Arg Input string.
982 * @returns true if string has been parsed as "yes"/"true"/"on".
985 Check_ArgIsTrue( const char *Arg )
987 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
988 if( strcasecmp( Arg, "true" ) == 0 ) return true;
989 if( atoi( Arg ) != 0 ) return true;
995 * Handle setting of "MaxNickLength".
997 * @param Line Line number in configuration file.
998 * @raram Arg Input string.
999 * @returns New configured maximum nick name length.
1002 Handle_MaxNickLength(int Line, const char *Arg)
1006 new = (unsigned) atoi(Arg) + 1;
1007 if (new > CLIENT_NICK_LEN) {
1008 Config_Error(LOG_WARNING,
1009 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
1010 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
1011 return CLIENT_NICK_LEN;
1014 Config_Error(LOG_WARNING,
1015 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
1016 NGIRCd_ConfFile, Line);
1023 * Output a warning messages if IDENT is configured but not compiled in.
1026 WarnIdent(int UNUSED Line)
1030 /* user has enabled ident lookups explicitly, but ... */
1031 Config_Error(LOG_WARNING,
1032 "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
1033 NGIRCd_ConfFile, Line);
1039 * Output a warning messages if IPv6 is configured but not compiled in.
1042 WarnIPv6(int UNUSED Line)
1045 if (Conf_ConnectIPv6) {
1046 /* user has enabled IPv6 explicitly, but ... */
1047 Config_Error(LOG_WARNING,
1048 "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
1049 NGIRCd_ConfFile, Line);
1055 * Output a warning messages if PAM is configured but not compiled in.
1058 WarnPAM(int UNUSED Line)
1062 Config_Error(LOG_WARNING,
1063 "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
1064 NGIRCd_ConfFile, Line);
1070 * Handle legacy "NoXXX" options in [GLOBAL] section.
1072 * TODO: This function and support for "NoXXX" could be removed starting
1073 * with ngIRCd release 19 (one release after marking it "deprecated").
1075 * @param Var Variable name.
1076 * @param Arg Argument string.
1077 * @returns true if a NoXXX option has been processed; false otherwise.
1080 CheckLegacyNoOption(const char *Var, const char *Arg)
1082 if(strcasecmp(Var, "NoDNS") == 0) {
1083 Conf_DNS = !Check_ArgIsTrue( Arg );
1086 if (strcasecmp(Var, "NoIdent") == 0) {
1087 Conf_Ident = !Check_ArgIsTrue(Arg);
1090 if(strcasecmp(Var, "NoPAM") == 0) {
1091 Conf_PAM = !Check_ArgIsTrue(Arg);
1098 * Handle deprecated legacy options in [GLOBAL] section.
1100 * TODO: This function and support for these options in the [Global] section
1101 * could be removed starting with ngIRCd release 19 (one release after
1102 * marking it "deprecated").
1104 * @param Var Variable name.
1105 * @param Arg Argument string.
1106 * @returns true if a legacy option has been processed; false otherwise.
1109 CheckLegacyGlobalOption(int Line, char *Var, char *Arg)
1111 if (strcasecmp(Var, "AllowRemoteOper") == 0
1112 || strcasecmp(Var, "ChrootDir") == 0
1113 || strcasecmp(Var, "ConnectIPv4") == 0
1114 || strcasecmp(Var, "ConnectIPv6") == 0
1115 || strcasecmp(Var, "OperCanUseMode") == 0
1116 || strcasecmp(Var, "OperServerMode") == 0
1117 || strcasecmp(Var, "PredefChannelsOnly") == 0
1118 || strcasecmp(Var, "SyslogFacility") == 0
1119 || strcasecmp(Var, "WebircPassword") == 0) {
1120 Handle_OPTIONS(Line, Var, Arg);
1123 if (strcasecmp(Var, "ConnectRetry") == 0
1124 || strcasecmp(Var, "MaxConnections") == 0
1125 || strcasecmp(Var, "MaxConnectionsIP") == 0
1126 || strcasecmp(Var, "MaxJoins") == 0
1127 || strcasecmp(Var, "MaxNickLength") == 0
1128 || strcasecmp(Var, "PingTimeout") == 0
1129 || strcasecmp(Var, "PongTimeout") == 0) {
1130 Handle_LIMITS(Line, Var, Arg);
1134 if (strcasecmp(Var, "SSLCertFile") == 0
1135 || strcasecmp(Var, "SSLDHFile") == 0
1136 || strcasecmp(Var, "SSLKeyFile") == 0
1137 || strcasecmp(Var, "SSLKeyFilePassword") == 0
1138 || strcasecmp(Var, "SSLPorts") == 0) {
1139 Handle_SSL(Line, Var + 3, Arg);
1148 * Strip "no" prefix of a string.
1150 * TODO: This function and support for "NoXXX" should be removed starting
1151 * with ngIRCd release 19! (One release after marking it "deprecated").
1153 * @param str Pointer to input string starting with "no".
1154 * @returns New pointer to string without "no" prefix.
1157 NoNo(const char *str)
1159 assert(strncasecmp("no", str, 2) == 0 && str[2]);
1164 * Invert "boolean" string.
1166 * TODO: This function and support for "NoXXX" should be removed starting
1167 * with ngIRCd release 19! (One release after marking it "deprecated").
1169 * @param arg "Boolean" input string.
1170 * @returns Pointer to inverted "boolean string".
1173 InvertArg(const char *arg)
1175 return yesno_to_str(!Check_ArgIsTrue(arg));
1179 * Handle variable in [Global] configuration section.
1181 * @param Line Line numer in configuration file.
1182 * @param Var Variable name.
1183 * @param Arg Variable argument.
1186 Handle_GLOBAL( int Line, char *Var, char *Arg )
1191 const char *section;
1194 assert(Var != NULL);
1195 assert(Arg != NULL);
1197 if (strcasecmp(Var, "Name") == 0) {
1198 len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
1199 if (len >= sizeof(Conf_ServerName))
1200 Config_Error_TooLong(Line, Var);
1203 if (strcasecmp(Var, "AdminInfo1") == 0) {
1204 len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
1205 if (len >= sizeof(Conf_ServerAdmin1))
1206 Config_Error_TooLong(Line, Var);
1209 if (strcasecmp(Var, "AdminInfo2") == 0) {
1210 len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
1211 if (len >= sizeof(Conf_ServerAdmin2))
1212 Config_Error_TooLong(Line, Var);
1215 if (strcasecmp(Var, "AdminEMail") == 0) {
1216 len = strlcpy(Conf_ServerAdminMail, Arg,
1217 sizeof(Conf_ServerAdminMail));
1218 if (len >= sizeof(Conf_ServerAdminMail))
1219 Config_Error_TooLong(Line, Var);
1222 if (strcasecmp(Var, "Info") == 0) {
1223 len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
1224 if (len >= sizeof(Conf_ServerInfo))
1225 Config_Error_TooLong(Line, Var);
1228 if (strcasecmp(Var, "Listen") == 0) {
1229 if (Conf_ListenAddress) {
1230 Config_Error(LOG_ERR,
1231 "Multiple Listen= options, ignoring: %s",
1235 Conf_ListenAddress = strdup_warn(Arg);
1236 /* If allocation fails, we're in trouble: we cannot ignore the
1237 * error -- otherwise ngircd would listen on all interfaces. */
1238 if (!Conf_ListenAddress) {
1239 Config_Error(LOG_ALERT,
1240 "%s exiting due to fatal errors!",
1246 if (strcasecmp(Var, "MotdFile") == 0) {
1247 len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
1248 if (len >= sizeof(Conf_MotdFile))
1249 Config_Error_TooLong(Line, Var);
1252 if (strcasecmp(Var, "MotdPhrase") == 0) {
1256 if (len >= LINE_LEN) {
1257 Config_Error_TooLong(Line, Var);
1260 if (!array_copyb(&Conf_Motd, Arg, len + 1))
1261 Config_Error(LOG_WARNING,
1262 "%s, line %d: Could not append MotdPhrase: %s",
1263 NGIRCd_ConfFile, Line, strerror(errno));
1264 Using_MotdFile = false;
1267 if(strcasecmp(Var, "Password") == 0) {
1268 len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
1269 if (len >= sizeof(Conf_ServerPwd))
1270 Config_Error_TooLong(Line, Var);
1273 if (strcasecmp(Var, "PidFile") == 0) {
1274 len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
1275 if (len >= sizeof(Conf_PidFile))
1276 Config_Error_TooLong(Line, Var);
1279 if (strcasecmp(Var, "Ports") == 0) {
1280 ports_parse(&Conf_ListenPorts, Line, Arg);
1283 if (strcasecmp(Var, "ServerGID") == 0) {
1284 grp = getgrnam(Arg);
1286 Conf_GID = grp->gr_gid;
1288 Conf_GID = (unsigned int)atoi(Arg);
1289 if (!Conf_GID && strcmp(Arg, "0"))
1290 Config_Error_NaN(Line, Var);
1294 if (strcasecmp(Var, "ServerUID") == 0) {
1295 pwd = getpwnam(Arg);
1297 Conf_UID = pwd->pw_uid;
1299 Conf_UID = (unsigned int)atoi(Arg);
1300 if (!Conf_UID && strcmp(Arg, "0"))
1301 Config_Error_NaN(Line, Var);
1306 if (CheckLegacyNoOption(Var, Arg)) {
1307 /* TODO: This function and support for "NoXXX" could be
1308 * be removed starting with ngIRCd release 19 (one release
1309 * after marking it "deprecated"). */
1310 Config_Error(LOG_WARNING,
1311 "%s, line %d (section \"Global\"): \"No\"-Prefix is deprecated, use \"%s = %s\" in [Options] section!",
1312 NGIRCd_ConfFile, Line, NoNo(Var), InvertArg(Arg));
1313 if (strcasecmp(Var, "NoIdent") == 0)
1315 else if (strcasecmp(Var, "NoPam") == 0)
1319 if ((section = CheckLegacyGlobalOption(Line, Var, Arg))) {
1320 /** TODO: This function and support for these options in the
1321 * [Global] section could be removed starting with ngIRCd
1322 * release 19 (one release after marking it "deprecated"). */
1323 if (strncasecmp(Var, "SSL", 3) == 0) {
1324 Config_Error(LOG_WARNING,
1325 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s and rename to \"%s\"!",
1326 NGIRCd_ConfFile, Line, Var, section,
1329 Config_Error(LOG_WARNING,
1330 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s!",
1331 NGIRCd_ConfFile, Line, Var, section);
1336 Config_Error_Section(Line, Var, "Global");
1340 * Handle variable in [Limits] configuration section.
1342 * @param Line Line numer in configuration file.
1343 * @param Var Variable name.
1344 * @param Arg Variable argument.
1347 Handle_LIMITS(int Line, char *Var, char *Arg)
1350 assert(Var != NULL);
1351 assert(Arg != NULL);
1353 if (strcasecmp(Var, "ConnectRetry") == 0) {
1354 Conf_ConnectRetry = atoi(Arg);
1355 if (Conf_ConnectRetry < 5) {
1356 Config_Error(LOG_WARNING,
1357 "%s, line %d: Value of \"ConnectRetry\" too low!",
1358 NGIRCd_ConfFile, Line);
1359 Conf_ConnectRetry = 5;
1363 if (strcasecmp(Var, "MaxConnections") == 0) {
1364 Conf_MaxConnections = atol(Arg);
1365 if (!Conf_MaxConnections && strcmp(Arg, "0"))
1366 Config_Error_NaN(Line, Var);
1369 if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
1370 Conf_MaxConnectionsIP = atoi(Arg);
1371 if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
1372 Config_Error_NaN(Line, Var);
1375 if (strcasecmp(Var, "MaxJoins") == 0) {
1376 Conf_MaxJoins = atoi(Arg);
1377 if (!Conf_MaxJoins && strcmp(Arg, "0"))
1378 Config_Error_NaN(Line, Var);
1381 if (strcasecmp(Var, "MaxNickLength") == 0) {
1382 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1385 if (strcasecmp(Var, "PingTimeout") == 0) {
1386 Conf_PingTimeout = atoi(Arg);
1387 if (Conf_PingTimeout < 5) {
1388 Config_Error(LOG_WARNING,
1389 "%s, line %d: Value of \"PingTimeout\" too low!",
1390 NGIRCd_ConfFile, Line);
1391 Conf_PingTimeout = 5;
1395 if (strcasecmp(Var, "PongTimeout") == 0) {
1396 Conf_PongTimeout = atoi(Arg);
1397 if (Conf_PongTimeout < 5) {
1398 Config_Error(LOG_WARNING,
1399 "%s, line %d: Value of \"PongTimeout\" too low!",
1400 NGIRCd_ConfFile, Line);
1401 Conf_PongTimeout = 5;
1406 Config_Error_Section(Line, Var, "Limits");
1410 * Handle variable in [Options] configuration section.
1412 * @param Line Line numer in configuration file.
1413 * @param Var Variable name.
1414 * @param Arg Variable argument.
1417 Handle_OPTIONS(int Line, char *Var, char *Arg)
1422 assert(Var != NULL);
1423 assert(Arg != NULL);
1425 if (strcasecmp(Var, "AllowRemoteOper") == 0) {
1426 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1429 if (strcasecmp(Var, "ChrootDir") == 0) {
1430 len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
1431 if (len >= sizeof(Conf_Chroot))
1432 Config_Error_TooLong(Line, Var);
1435 if (strcasecmp(Var, "CloakHost") == 0) {
1436 len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
1437 if (len >= sizeof(Conf_CloakHost))
1438 Config_Error_TooLong(Line, Var);
1441 if (strcasecmp(Var, "CloakUserToNick") == 0) {
1442 Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
1445 if (strcasecmp(Var, "ConnectIPv6") == 0) {
1446 Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
1450 if (strcasecmp(Var, "ConnectIPv4") == 0) {
1451 Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
1454 if (strcasecmp(Var, "DNS") == 0) {
1455 Conf_DNS = Check_ArgIsTrue(Arg);
1458 if (strcasecmp(Var, "Ident") == 0) {
1459 Conf_Ident = Check_ArgIsTrue(Arg);
1463 if (strcasecmp(Var, "NoticeAuth") == 0) {
1464 Conf_NoticeAuth = Check_ArgIsTrue(Arg);
1467 if (strcasecmp(Var, "OperCanUseMode") == 0) {
1468 Conf_OperCanMode = Check_ArgIsTrue(Arg);
1471 if (strcasecmp(Var, "OperServerMode") == 0) {
1472 Conf_OperServerMode = Check_ArgIsTrue(Arg);
1475 if (strcasecmp(Var, "PAM") == 0) {
1476 Conf_PAM = Check_ArgIsTrue(Arg);
1480 if (strcasecmp(Var, "PredefChannelsOnly") == 0) {
1481 Conf_PredefChannelsOnly = Check_ArgIsTrue(Arg);
1485 if (strcasecmp(Var, "RequireAuthPing") == 0) {
1486 Conf_AuthPing = Check_ArgIsTrue(Arg);
1490 if (strcasecmp(Var, "ScrubCTCP") == 0) {
1491 Conf_ScrubCTCP = Check_ArgIsTrue(Arg);
1495 if (strcasecmp(Var, "SyslogFacility") == 0) {
1496 Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
1497 Conf_SyslogFacility);
1501 if (strcasecmp(Var, "WebircPassword") == 0) {
1502 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
1503 if (len >= sizeof(Conf_WebircPwd))
1504 Config_Error_TooLong(Line, Var);
1508 Config_Error_Section(Line, Var, "Options");
1514 * Handle variable in [SSL] configuration section.
1516 * @param Line Line numer in configuration file.
1517 * @param Var Variable name.
1518 * @param Arg Variable argument.
1521 Handle_SSL(int Line, char *Var, char *Arg)
1524 assert(Var != NULL);
1525 assert(Arg != NULL);
1527 if (strcasecmp(Var, "CertFile") == 0) {
1528 assert(Conf_SSLOptions.CertFile == NULL);
1529 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1532 if (strcasecmp(Var, "DHFile") == 0) {
1533 assert(Conf_SSLOptions.DHFile == NULL);
1534 Conf_SSLOptions.DHFile = strdup_warn(Arg);
1537 if (strcasecmp(Var, "KeyFile") == 0) {
1538 assert(Conf_SSLOptions.KeyFile == NULL);
1539 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1542 if (strcasecmp(Var, "KeyFilePassword") == 0) {
1543 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1544 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1545 Config_Error(LOG_ERR,
1546 "%s, line %d (section \"SSL\"): Could not copy %s: %s!",
1547 NGIRCd_ConfFile, Line, Var,
1551 if (strcasecmp(Var, "Ports") == 0) {
1552 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1556 Config_Error_Section(Line, Var, "SSL");
1562 * Handle variable in [Operator] configuration section.
1564 * @param Line Line numer in configuration file.
1565 * @param Var Variable name.
1566 * @param Arg Variable argument.
1569 Handle_OPERATOR( int Line, char *Var, char *Arg )
1572 struct Conf_Oper *op;
1575 assert( Var != NULL );
1576 assert( Arg != NULL );
1577 assert( Conf_Oper_Count > 0 );
1579 op = array_alloc(&Conf_Opers, sizeof(*op), Conf_Oper_Count - 1);
1581 Config_Error(LOG_ERR, "Could not allocate memory for operator (%d:%s = %s)", Line, Var, Arg);
1585 if (strcasecmp(Var, "Name") == 0) {
1586 /* Name of IRC operator */
1587 len = strlcpy(op->name, Arg, sizeof(op->name));
1588 if (len >= sizeof(op->name))
1589 Config_Error_TooLong(Line, Var);
1592 if (strcasecmp(Var, "Password") == 0) {
1593 /* Password of IRC operator */
1594 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1595 if (len >= sizeof(op->pwd))
1596 Config_Error_TooLong(Line, Var);
1599 if (strcasecmp(Var, "Mask") == 0) {
1601 return; /* Hostname already configured */
1602 op->mask = strdup_warn( Arg );
1606 Config_Error_Section(Line, Var, "Operator");
1610 * Handle variable in [Server] configuration section.
1612 * @param Line Line numer in configuration file.
1613 * @param Var Variable name.
1614 * @param Arg Variable argument.
1617 Handle_SERVER( int Line, char *Var, char *Arg )
1623 assert( Var != NULL );
1624 assert( Arg != NULL );
1626 /* Ignore server block if no space is left in server configuration structure */
1627 if( New_Server_Idx <= NONE ) return;
1629 if( strcasecmp( Var, "Host" ) == 0 ) {
1630 /* Hostname of the server */
1631 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1632 if (len >= sizeof( New_Server.host ))
1633 Config_Error_TooLong ( Line, Var );
1636 if( strcasecmp( Var, "Name" ) == 0 ) {
1637 /* Name of the server ("Nick"/"ID") */
1638 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1639 if (len >= sizeof( New_Server.name ))
1640 Config_Error_TooLong( Line, Var );
1643 if (strcasecmp(Var, "Bind") == 0) {
1644 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1647 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1648 NGIRCd_ConfFile, Line, Arg);
1651 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1652 /* Password of this server which is sent to the peer */
1654 Config_Error(LOG_ERR,
1655 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1656 NGIRCd_ConfFile, Line);
1658 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1659 if (len >= sizeof( New_Server.pwd_in ))
1660 Config_Error_TooLong( Line, Var );
1663 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1664 /* Passwort of the peer which must be received */
1665 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1666 if (len >= sizeof( New_Server.pwd_out ))
1667 Config_Error_TooLong( Line, Var );
1670 if( strcasecmp( Var, "Port" ) == 0 ) {
1671 /* Port to which this server should connect */
1673 if (port >= 0 && port < 0xFFFF)
1674 New_Server.port = (UINT16)port;
1676 Config_Error(LOG_ERR,
1677 "%s, line %d (section \"Server\"): Illegal port number %ld!",
1678 NGIRCd_ConfFile, Line, port );
1682 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1683 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1687 if( strcasecmp( Var, "Group" ) == 0 ) {
1689 New_Server.group = atoi( Arg );
1690 if (!New_Server.group && strcmp(Arg, "0"))
1691 Config_Error_NaN(Line, Var);
1694 if( strcasecmp( Var, "Passive" ) == 0 ) {
1695 if (Check_ArgIsTrue(Arg))
1696 New_Server.flags |= CONF_SFLAG_DISABLED;
1699 if (strcasecmp(Var, "ServiceMask") == 0) {
1700 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1701 sizeof(New_Server.svs_mask));
1702 if (len >= sizeof(New_Server.svs_mask))
1703 Config_Error_TooLong(Line, Var);
1707 Config_Error_Section(Line, Var, "Server");
1711 * Copy channel name into channel structure.
1713 * If the channel name is not valid because of a missing prefix ('#', '&'),
1714 * a default prefix of '#' will be added.
1716 * @param new_chan New already allocated channel structure.
1717 * @param name Name of the new channel.
1718 * @returns true on success, false otherwise.
1721 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1723 size_t size = sizeof(new_chan->name);
1724 char *dest = new_chan->name;
1726 if (!Channel_IsValidName(name)) {
1728 * maybe user forgot to add a '#'.
1729 * This is only here for user convenience.
1735 return size > strlcpy(dest, name, size);
1739 * Handle variable in [Channel] configuration section.
1741 * @param Line Line numer in configuration file.
1742 * @param Var Variable name.
1743 * @param Arg Variable argument.
1746 Handle_CHANNEL(int Line, char *Var, char *Arg)
1750 struct Conf_Channel *chan;
1753 assert( Var != NULL );
1754 assert( Arg != NULL );
1755 assert(Conf_Channel_Count > 0);
1757 chancount = Conf_Channel_Count - 1;
1759 chan = array_alloc(&Conf_Channels, sizeof(*chan), chancount);
1761 Config_Error(LOG_ERR, "Could not allocate memory for predefined channel (%d:%s = %s)", Line, Var, Arg);
1764 if (strcasecmp(Var, "Name") == 0) {
1765 if (!Handle_Channelname(chan, Arg))
1766 Config_Error_TooLong(Line, Var);
1769 if (strcasecmp(Var, "Modes") == 0) {
1771 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1772 if (len >= sizeof(chan->modes))
1773 Config_Error_TooLong( Line, Var );
1776 if( strcasecmp( Var, "Topic" ) == 0 ) {
1778 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1779 if (len >= sizeof(chan->topic))
1780 Config_Error_TooLong( Line, Var );
1783 if( strcasecmp( Var, "Key" ) == 0 ) {
1784 /* Initial Channel Key (mode k) */
1785 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1786 if (len >= sizeof(chan->key))
1787 Config_Error_TooLong(Line, Var);
1790 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1791 /* maximum user limit, mode l */
1792 chan->maxusers = (unsigned long) atol(Arg);
1793 if (!chan->maxusers && strcmp(Arg, "0"))
1794 Config_Error_NaN(Line, Var);
1797 if (strcasecmp(Var, "KeyFile") == 0) {
1799 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1800 if (len >= sizeof(chan->keyfile))
1801 Config_Error_TooLong(Line, Var);
1805 Config_Error_Section(Line, Var, "Channel");
1809 * Validate server configuration.
1811 * Please note that this function uses exit(1) on fatal errors and therefore
1812 * can result in ngIRCd terminating!
1814 * @param Configtest true if the daemon has been called with "--configtest".
1815 * @param Rehash true if re-reading configuration on runtime.
1816 * @returns true if configuration is valid.
1819 Validate_Config(bool Configtest, bool Rehash)
1821 /* Validate configuration settings. */
1824 int i, servers, servers_once;
1826 bool config_valid = true;
1829 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1830 ptr = Conf_ServerName;
1832 if (*ptr >= 'a' && *ptr <= 'z') continue;
1833 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1834 if (*ptr >= '0' && *ptr <= '9') continue;
1835 if (ptr > Conf_ServerName) {
1836 if (*ptr == '.' || *ptr == '-')
1839 Conf_ServerName[0] = '\0';
1843 if (!Conf_ServerName[0]) {
1844 /* No server name configured! */
1845 config_valid = false;
1846 Config_Error(LOG_ALERT,
1847 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1849 if (!Configtest && !Rehash) {
1850 Config_Error(LOG_ALERT,
1851 "%s exiting due to fatal errors!",
1857 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1858 /* No dot in server name! */
1859 config_valid = false;
1860 Config_Error(LOG_ALERT,
1861 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1864 Config_Error(LOG_ALERT,
1865 "%s exiting due to fatal errors!",
1872 if (!Conf_ServerAdminMail[0]) {
1873 /* No administrative contact configured! */
1874 config_valid = false;
1875 Config_Error(LOG_ALERT,
1876 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1879 Config_Error(LOG_ALERT,
1880 "%s exiting due to fatal errors!",
1887 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1888 && !Conf_ServerAdminMail[0]) {
1889 /* No administrative information configured! */
1890 Config_Error(LOG_WARNING,
1891 "No administrative information configured but required by RFC!");
1895 if (Conf_ServerPwd[0])
1896 Config_Error(LOG_ERR,
1897 "This server uses PAM, \"Password\" in [Global] section will be ignored!");
1901 servers = servers_once = 0;
1902 for (i = 0; i < MAX_SERVERS; i++) {
1903 if (Conf_Server[i].name[0]) {
1905 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1910 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1911 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1914 return config_valid;
1918 * Output "line too long" warning.
1920 * @param Line Line number in configuration file.
1921 * @param Item Affected variable name.
1924 Config_Error_TooLong ( const int Line, const char *Item )
1926 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1930 * Output "unknown variable" warning.
1932 * @param Line Line number in configuration file.
1933 * @param Item Affected variable name.
1934 * @param Section Section name.
1937 Config_Error_Section(const int Line, const char *Item, const char *Section)
1939 Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
1940 NGIRCd_ConfFile, Line, Section, Item);
1944 * Output "not a number" warning.
1946 * @param Line Line number in configuration file.
1947 * @param Item Affected variable name.
1950 Config_Error_NaN( const int Line, const char *Item )
1952 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1953 NGIRCd_ConfFile, Line, Item );
1957 * Output configuration error to console and/or logfile.
1959 * On runtime, the normal log functions of the daemon are used. But when
1960 * testing the configuration ("--configtest"), all messages go directly
1963 * @param Level Severity level of the message.
1964 * @param Format Format string; see printf() function.
1967 static void Config_Error( const int Level, const char *Format, ... )
1969 static void Config_Error( Level, Format, va_alist )
1975 char msg[MAX_LOG_MSG_LEN];
1978 assert( Format != NULL );
1981 va_start( ap, Format );
1985 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1989 if (Level <= LOG_WARNING)
1990 printf(" - %s\n", msg);
1994 Log(Level, "%s", msg);
2000 * Dump internal state of the "configuration module".
2003 Conf_DebugDump(void)
2007 Log(LOG_DEBUG, "Configured servers:");
2008 for (i = 0; i < MAX_SERVERS; i++) {
2009 if (! Conf_Server[i].name[0])
2012 " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
2013 Conf_Server[i].name, Conf_Server[i].host,
2014 Conf_Server[i].port, Conf_Server[i].lasttry,
2015 Conf_Server[i].group, Conf_Server[i].flags,
2016 Conf_Server[i].conn_id);
2023 * Initialize server configuration structur to default values.
2025 * @param Server Pointer to server structure to initialize.
2028 Init_Server_Struct( CONF_SERVER *Server )
2030 assert( Server != NULL );
2032 memset( Server, 0, sizeof (CONF_SERVER) );
2034 Server->group = NONE;
2035 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
2037 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
2039 Proc_InitStruct(&Server->res_stat);
2040 Server->conn_id = NONE;
2041 memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));