2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2011 Alexander Barton (alex@barton.de) and Contributors.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
16 * Configuration management (reading, parsing & validation)
34 #include <sys/types.h>
54 static bool Use_Log = true, Using_MotdFile = true;
55 static CONF_SERVER New_Server;
56 static int New_Server_Idx;
58 static size_t Conf_Oper_Count;
59 static size_t Conf_Channel_Count;
60 static char Conf_MotdFile[FNAME_LEN];
62 static void Set_Defaults PARAMS(( bool InitServers ));
63 static bool Read_Config PARAMS(( bool ngircd_starting ));
64 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
66 static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
67 static void Handle_LIMITS PARAMS(( int Line, char *Var, char *Arg ));
68 static void Handle_OPTIONS PARAMS(( int Line, char *Var, char *Arg ));
69 static void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
70 static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
71 static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
73 static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
75 static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
76 static void Config_Error_Section PARAMS(( const int Line, const char *Item,
77 const char *Section ));
78 static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
80 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
84 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
86 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
92 struct SSLOptions Conf_SSLOptions;
95 * Initialize SSL configuration.
100 free(Conf_SSLOptions.KeyFile);
101 Conf_SSLOptions.KeyFile = NULL;
103 free(Conf_SSLOptions.CertFile);
104 Conf_SSLOptions.CertFile = NULL;
106 free(Conf_SSLOptions.DHFile);
107 Conf_SSLOptions.DHFile = NULL;
108 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
112 * Make sure that a configured file is readable.
114 * Currently, this function is only used for SSL-related options ...
116 * @param Var Configuration variable
117 * @param Filename Configured filename
120 CheckFileReadable(const char *Var, const char *Filename)
124 fp = fopen(Filename, "r");
128 Config_Error(LOG_ERR, "Can't read \"%s\" (\"%s\"): %s",
129 Filename, Var, strerror(errno));
136 * Duplicate string and warn on errors.
138 * @returns Pointer to string on success, NULL otherwise.
141 strdup_warn(const char *str)
143 char *ptr = strdup(str);
145 Config_Error(LOG_ERR,
146 "Could not allocate memory for string: %s", str);
151 * Output a comma separated list of ports (integer values).
158 len = array_length(a, sizeof(UINT16));
160 ports = (UINT16*) array_start(a);
161 printf("%u", (unsigned int) *ports);
164 printf(", %u", (unsigned int) *ports);
171 * Parse a comma separated string into an array of port numbers (integers).
174 ports_parse(array *a, int Line, char *Arg)
182 ptr = strtok( Arg, "," );
186 if (port > 0 && port < 0xFFFF) {
187 port16 = (UINT16) port;
188 if (!array_catb(a, (char*)&port16, sizeof port16))
189 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
190 NGIRCd_ConfFile, Line, port, strerror(errno));
192 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
193 NGIRCd_ConfFile, Line, port );
196 ptr = strtok( NULL, "," );
201 * Initialize configuration module.
207 Validate_Config(false, false);
211 * "Rehash" (reload) server configuration.
213 * @returns true if configuration has been re-read, false on errors.
218 if (!Read_Config(false))
220 Validate_Config(false, true);
222 /* Update CLIENT structure of local server */
223 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
228 * Output a boolean value as "yes/no" string.
231 yesno_to_str(int boolean_value)
239 * Free all IRC operator configuration structures.
244 struct Conf_Oper *op;
247 len = array_length(&Conf_Opers, sizeof(*op));
248 op = array_start(&Conf_Opers);
253 array_free(&Conf_Opers);
257 * Output all IRC operator configuration structures.
262 struct Conf_Oper *op;
265 len = array_length(&Conf_Opers, sizeof(*op));
266 op = array_start(&Conf_Opers);
271 printf(" Name = %s\n", op->name);
272 printf(" Password = %s\n", op->pwd);
273 printf(" Mask = %s\n\n", op->mask ? op->mask : "");
279 * Read configuration, validate and output it.
281 * This function waits for a keypress of the user when stdin/stdout are valid
282 * tty's ("you can read our nice message and we can read in your keypress").
284 * @return 0 on succes, 1 on failure(s); therefore the result code can
285 * directly be used by exit() when running "ngircd --configtest".
294 size_t predef_channel_count;
295 struct Conf_Channel *predef_chan;
299 if (! Read_Config(true))
302 config_valid = Validate_Config(true, false);
305 if(isatty(fileno(stdin)) && isatty(fileno(stdout))) {
306 puts("OK, press enter to see a dump of your server configuration ...");
309 puts("Ok, dump of your server configuration follows:\n");
312 printf(" Name = %s\n", Conf_ServerName);
313 printf(" AdminInfo1 = %s\n", Conf_ServerAdmin1);
314 printf(" AdminInfo2 = %s\n", Conf_ServerAdmin2);
315 printf(" AdminEMail = %s\n", Conf_ServerAdminMail);
316 printf(" Info = %s\n", Conf_ServerInfo);
317 printf(" Listen = %s\n", Conf_ListenAddress);
318 if (Using_MotdFile) {
319 printf(" MotdFile = %s\n", Conf_MotdFile);
320 printf(" MotdPhrase =\n");
322 printf(" MotdFile = \n");
323 printf(" MotdPhrase = %s\n", array_bytes(&Conf_Motd)
324 ? (const char*) array_start(&Conf_Motd) : "");
327 printf(" Password = %s\n", Conf_ServerPwd);
329 printf(" PidFile = %s\n", Conf_PidFile);
331 ports_puts(&Conf_ListenPorts);
332 grp = getgrgid(Conf_GID);
334 printf(" ServerGID = %s\n", grp->gr_name);
336 printf(" ServerGID = %ld\n", (long)Conf_GID);
337 pwd = getpwuid(Conf_UID);
339 printf(" ServerUID = %s\n", pwd->pw_name);
341 printf(" ServerUID = %ld\n", (long)Conf_UID);
345 printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
346 printf(" MaxConnections = %ld\n", Conf_MaxConnections);
347 printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
348 printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
349 printf(" MaxNickLength = %u\n", Conf_MaxNickLength - 1);
350 printf(" PingTimeout = %d\n", Conf_PingTimeout);
351 printf(" PongTimeout = %d\n", Conf_PongTimeout);
355 printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
356 printf(" ChrootDir = %s\n", Conf_Chroot);
357 printf(" CloakHost = %s\n", Conf_CloakHost);
358 printf(" CloakUserToNick = %s\n", yesno_to_str(Conf_CloakUserToNick));
360 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
361 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
363 printf(" DNS = %s\n", yesno_to_str(Conf_DNS));
365 printf(" Ident = %s\n", yesno_to_str(Conf_Ident));
367 printf(" NoticeAuth = %s\n", yesno_to_str(Conf_NoticeAuth));
368 printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
369 printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
371 printf(" PAM = %s\n", yesno_to_str(Conf_PAM));
373 printf(" PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
375 printf(" RequireAuthPing = %s\n", yesno_to_str(Conf_AuthPing));
378 printf(" SSLCertFile = %s\n", Conf_SSLOptions.CertFile);
379 printf(" SSLDHFile = %s\n", Conf_SSLOptions.DHFile);
380 printf(" SSLKeyFile = %s\n", Conf_SSLOptions.KeyFile);
381 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
382 puts(" SSLKeyFilePassword = <secret>");
384 puts(" SSLKeyFilePassword = ");
385 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
386 printf(" SSLPorts = ");
387 ports_puts(&Conf_SSLOptions.ListenPorts);
390 printf(" SyslogFacility = %s\n",
391 ngt_SyslogFacilityName(Conf_SyslogFacility));
393 printf(" WebircPassword = %s\n", Conf_WebircPwd);
398 for( i = 0; i < MAX_SERVERS; i++ ) {
399 if( ! Conf_Server[i].name[0] ) continue;
401 /* Valid "Server" section */
403 printf( " Name = %s\n", Conf_Server[i].name );
404 printf( " Host = %s\n", Conf_Server[i].host );
405 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
407 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
409 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
410 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
411 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
412 printf( " Group = %d\n", Conf_Server[i].group );
413 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
416 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
417 predef_chan = array_start(&Conf_Channels);
419 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
420 if (!predef_chan->name[0])
423 /* Valid "Channel" section */
425 printf(" Name = %s\n", predef_chan->name);
426 printf(" Modes = %s\n", predef_chan->modes);
427 printf(" Key = %s\n", predef_chan->key);
428 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
429 printf(" Topic = %s\n", predef_chan->topic);
430 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
433 return (config_valid ? 0 : 1);
437 * Remove connection information from configured server.
439 * If the server is set as "once", delete it from our configuration;
440 * otherwise set the time for the next connection attempt.
442 * Non-server connections will be silently ignored.
445 Conf_UnsetServer( CONN_ID Idx )
450 /* Check all our configured servers */
451 for( i = 0; i < MAX_SERVERS; i++ ) {
452 if( Conf_Server[i].conn_id != Idx ) continue;
454 /* Gotcha! Mark server configuration as "unused": */
455 Conf_Server[i].conn_id = NONE;
457 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
458 /* Delete configuration here */
459 Init_Server_Struct( &Conf_Server[i] );
461 /* Set time for next connect attempt */
463 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
464 /* The connection has been "long", so we don't
465 * require the next attempt to be delayed. */
466 Conf_Server[i].lasttry =
467 t - Conf_ConnectRetry + RECONNECT_DELAY;
469 Conf_Server[i].lasttry = t;
475 * Set connection information for specified configured server.
478 Conf_SetServer( int ConfServer, CONN_ID Idx )
480 assert( ConfServer > NONE );
481 assert( Idx > NONE );
483 Conf_Server[ConfServer].conn_id = Idx;
487 * Get index of server in configuration structure.
490 Conf_GetServer( CONN_ID Idx )
494 assert( Idx > NONE );
496 for( i = 0; i < MAX_SERVERS; i++ ) {
497 if( Conf_Server[i].conn_id == Idx ) return i;
503 * Enable a server by name and adjust its port number.
505 * @returns true if a server has been enabled and now has a valid port
506 * number and host name for outgoing connections.
509 Conf_EnableServer( const char *Name, UINT16 Port )
513 assert( Name != NULL );
514 for( i = 0; i < MAX_SERVERS; i++ ) {
515 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
516 /* Gotcha! Set port and enable server: */
517 Conf_Server[i].port = Port;
518 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
519 return (Conf_Server[i].port && Conf_Server[i].host[0]);
526 * Enable a server by name.
528 * The server is only usable as outgoing server, if it has set a valid port
529 * number for outgoing connections!
530 * If not, you have to use Conf_EnableServer() function to make it available.
532 * @returns true if a server has been enabled; false otherwise.
535 Conf_EnablePassiveServer(const char *Name)
539 assert( Name != NULL );
540 for (i = 0; i < MAX_SERVERS; i++) {
541 if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
542 && (Conf_Server[i].port > 0)) {
543 /* BINGO! Enable server */
544 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
552 * Disable a server by name.
553 * An already established connection will be disconnected.
555 * @returns true if a server was found and has been disabled.
558 Conf_DisableServer( const char *Name )
562 assert( Name != NULL );
563 for( i = 0; i < MAX_SERVERS; i++ ) {
564 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
565 /* Gotcha! Disable and disconnect server: */
566 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
567 if( Conf_Server[i].conn_id > NONE )
568 Conn_Close(Conf_Server[i].conn_id, NULL,
569 "Server link terminated on operator request",
578 * Add a new remote server to our configuration.
580 * @param Name Name of the new server.
581 * @param Port Port number to connect to or 0 for incoming connections.
582 * @param Host Host name to connect to.
583 * @param MyPwd Password that will be sent to the peer.
584 * @param PeerPwd Password that must be received from the peer.
585 * @returns true if the new server has been added; false otherwise.
588 Conf_AddServer(const char *Name, UINT16 Port, const char *Host,
589 const char *MyPwd, const char *PeerPwd)
593 assert( Name != NULL );
594 assert( Host != NULL );
595 assert( MyPwd != NULL );
596 assert( PeerPwd != NULL );
598 /* Search unused item in server configuration structure */
599 for( i = 0; i < MAX_SERVERS; i++ ) {
600 /* Is this item used? */
601 if( ! Conf_Server[i].name[0] ) break;
603 if( i >= MAX_SERVERS ) return false;
605 Init_Server_Struct( &Conf_Server[i] );
606 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
607 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
608 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
609 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
610 Conf_Server[i].port = Port;
611 Conf_Server[i].flags = CONF_SFLAG_ONCE;
617 * Check if the given nick name is an service.
619 * @returns true if the given nick name belongs to an "IRC service".
622 Conf_IsService(int ConfServer, const char *Nick)
624 return MatchCaseInsensitive(Conf_Server[ConfServer].svs_mask, Nick);
628 * Initialize configuration settings with their default values.
631 Set_Defaults(bool InitServers)
636 strcpy(Conf_ServerName, "");
637 strcpy(Conf_ServerAdmin1, "");
638 strcpy(Conf_ServerAdmin2, "");
639 strcpy(Conf_ServerAdminMail, "");
640 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
641 PACKAGE_NAME, PACKAGE_VERSION);
642 free(Conf_ListenAddress);
643 Conf_ListenAddress = NULL;
644 array_free(&Conf_Motd);
645 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
646 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
647 strcpy(Conf_ServerPwd, "");
648 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
649 Conf_UID = Conf_GID = 0;
652 Conf_ConnectRetry = 60;
653 Conf_MaxConnections = 0;
654 Conf_MaxConnectionsIP = 5;
656 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
657 Conf_PingTimeout = 120;
658 Conf_PongTimeout = 20;
661 Conf_AllowRemoteOper = false;
663 Conf_AuthPing = false;
665 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
666 strcpy(Conf_CloakHost, "");
667 Conf_CloakUserToNick = false;
668 Conf_ConnectIPv4 = true;
670 Conf_ConnectIPv6 = true;
672 Conf_ConnectIPv6 = false;
680 Conf_NoticeAuth = false;
681 Conf_OperCanMode = false;
682 Conf_OperServerMode = false;
688 Conf_PredefChannelsOnly = false;
691 Conf_SyslogFacility = LOG_LOCAL5;
693 Conf_SyslogFacility = 0;
697 /* Initialize IRC operators and channels */
699 Conf_Channel_Count = 0;
701 /* Initialize server configuration structures */
703 for (i = 0; i < MAX_SERVERS;
704 Init_Server_Struct(&Conf_Server[i++]));
709 * Get number of configured listening ports.
711 * @returns The number of ports (IPv4+IPv6) on which the server should listen.
716 size_t cnt = array_bytes(&Conf_ListenPorts);
718 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
724 * Read MOTD ("message of the day") file.
726 * @param filename Name of the file to read.
729 Read_Motd(const char *filename)
734 if (*filename == '\0')
737 fp = fopen(filename, "r");
739 Config_Error(LOG_WARNING, "Can't read MOTD file \"%s\": %s",
740 filename, strerror(errno));
744 array_free(&Conf_Motd);
745 Using_MotdFile = true;
747 while (fgets(line, (int)sizeof line, fp)) {
748 ngt_TrimLastChr( line, '\n');
750 /* add text including \0 */
751 if (!array_catb(&Conf_Motd, line, strlen(line) + 1)) {
752 Log(LOG_WARNING, "Cannot add MOTD text: %s", strerror(errno));
760 * Read ngIRCd configuration file.
762 * Please note that this function uses exit(1) on fatal errors and therefore
763 * can result in ngIRCd terminating!
765 * @param ngircd_starting Flag indicating if ngIRCd is starting or not.
766 * @returns true when the configuration file has been read
767 * successfully; false otherwise.
770 Read_Config( bool ngircd_starting )
772 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
773 const UINT16 defaultport = 6667;
777 /* Open configuration file */
778 fd = fopen( NGIRCd_ConfFile, "r" );
780 /* No configuration file found! */
781 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
782 NGIRCd_ConfFile, strerror( errno ));
783 if (!ngircd_starting)
785 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
790 Set_Defaults( ngircd_starting );
792 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
794 /* Clean up server configuration structure: mark all already
795 * configured servers as "once" so that they are deleted
796 * after the next disconnect and delete all unused servers.
797 * And delete all servers which are "duplicates" of servers
798 * that are already marked as "once" (such servers have been
799 * created by the last rehash but are now useless). */
800 for( i = 0; i < MAX_SERVERS; i++ ) {
801 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
803 /* This structure is in use ... */
804 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
805 /* Check for duplicates */
806 for( n = 0; n < MAX_SERVERS; n++ ) {
807 if( n == i ) continue;
809 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
810 Init_Server_Struct( &Conf_Server[n] );
812 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
818 /* Mark server as "once" */
819 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
820 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
825 /* Initialize variables */
827 strcpy( section, "" );
828 Init_Server_Struct( &New_Server );
829 New_Server_Idx = NONE;
833 /* Read configuration file */
835 if( ! fgets( str, LINE_LEN, fd )) break;
839 /* Skip comments and empty lines */
840 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
842 /* Is this the beginning of a new section? */
843 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
844 strlcpy( section, str, sizeof( section ));
845 if (strcasecmp(section, "[GLOBAL]") == 0 ||
846 strcasecmp(section, "[LIMITS]") == 0 ||
847 strcasecmp(section, "[OPTIONS]") == 0)
850 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
851 /* Check if there is already a server to add */
852 if( New_Server.name[0] ) {
853 /* Copy data to "real" server structure */
854 assert( New_Server_Idx > NONE );
855 Conf_Server[New_Server_Idx] = New_Server;
858 /* Re-init structure for new server */
859 Init_Server_Struct( &New_Server );
861 /* Search unused item in server configuration structure */
862 for( i = 0; i < MAX_SERVERS; i++ ) {
863 /* Is this item used? */
864 if( ! Conf_Server[i].name[0] ) break;
866 if( i >= MAX_SERVERS ) {
867 /* Oops, no free item found! */
868 Config_Error( LOG_ERR, "Too many servers configured." );
869 New_Server_Idx = NONE;
871 else New_Server_Idx = i;
874 if (strcasecmp(section, "[CHANNEL]") == 0) {
875 Conf_Channel_Count++;
878 if (strcasecmp(section, "[OPERATOR]") == 0) {
883 Config_Error(LOG_ERR,
884 "%s, line %d: Unknown section \"%s\"!",
885 NGIRCd_ConfFile, line, section);
888 if( section[0] == 0x1 ) continue;
890 /* Split line into variable name and parameters */
891 ptr = strchr( str, '=' );
893 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
897 var = str; ngt_TrimStr( var );
898 arg = ptr + 1; ngt_TrimStr( arg );
900 if(strcasecmp(section, "[GLOBAL]") == 0)
901 Handle_GLOBAL(line, var, arg);
902 else if(strcasecmp(section, "[LIMITS]") == 0)
903 Handle_LIMITS(line, var, arg);
904 else if(strcasecmp(section, "[OPTIONS]") == 0)
905 Handle_OPTIONS(line, var, arg);
906 else if(strcasecmp(section, "[OPERATOR]") == 0)
907 Handle_OPERATOR(line, var, arg);
908 else if(strcasecmp(section, "[SERVER]") == 0)
909 Handle_SERVER(line, var, arg);
910 else if(strcasecmp(section, "[CHANNEL]") == 0)
911 Handle_CHANNEL(line, var, arg);
913 Config_Error(LOG_ERR,
914 "%s, line %d: Variable \"%s\" outside section!",
915 NGIRCd_ConfFile, line, var);
918 /* Close configuration file */
921 /* Check if there is still a server to add */
922 if( New_Server.name[0] ) {
923 /* Copy data to "real" server structure */
924 assert( New_Server_Idx > NONE );
925 Conf_Server[New_Server_Idx] = New_Server;
928 /* not a single listening port? Add default. */
929 if (no_listenports() &&
930 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
932 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
933 (unsigned int) defaultport, strerror(errno));
938 if (!Conf_ListenAddress)
939 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
941 if (!Conf_ListenAddress) {
942 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
946 /* No MOTD phrase configured? (re)try motd file. */
947 if (array_bytes(&Conf_Motd) == 0)
948 Read_Motd(Conf_MotdFile);
951 /* Make sure that all SSL-related files are readable */
952 CheckFileReadable("SSLCertFile", Conf_SSLOptions.CertFile);
953 CheckFileReadable("SSLDHFile", Conf_SSLOptions.DHFile);
954 CheckFileReadable("SSLKeyFile", Conf_SSLOptions.KeyFile);
961 * Check whether an string argument is true or false.
963 * @param Arg Input string.
964 * @returns true if string has been parsed as "yes"/"true"/"on".
967 Check_ArgIsTrue( const char *Arg )
969 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
970 if( strcasecmp( Arg, "true" ) == 0 ) return true;
971 if( atoi( Arg ) != 0 ) return true;
977 * Handle setting of "MaxNickLength".
979 * @param Line Line number in configuration file.
980 * @raram Arg Input string.
981 * @returns New configured maximum nick name length.
984 Handle_MaxNickLength(int Line, const char *Arg)
988 new = (unsigned) atoi(Arg) + 1;
989 if (new > CLIENT_NICK_LEN) {
990 Config_Error(LOG_WARNING,
991 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
992 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
993 return CLIENT_NICK_LEN;
996 Config_Error(LOG_WARNING,
997 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
998 NGIRCd_ConfFile, Line);
1005 * Output a warning messages if IDENT is configured but not compiled in.
1008 WarnIdent(int UNUSED Line)
1012 /* user has enabled ident lookups explicitly, but ... */
1013 Config_Error(LOG_WARNING,
1014 "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
1015 NGIRCd_ConfFile, Line);
1021 * Output a warning messages if IPv6 is configured but not compiled in.
1024 WarnIPv6(int UNUSED Line)
1027 if (Conf_ConnectIPv6) {
1028 /* user has enabled IPv6 explicitly, but ... */
1029 Config_Error(LOG_WARNING,
1030 "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
1031 NGIRCd_ConfFile, Line);
1037 * Output a warning messages if PAM is configured but not compiled in.
1040 WarnPAM(int UNUSED Line)
1044 Config_Error(LOG_WARNING,
1045 "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
1046 NGIRCd_ConfFile, Line);
1052 * Handle legacy "NoXXX" options in [GLOBAL] section.
1054 * TODO: This function and support for "NoXXX" could be removed starting
1055 * with ngIRCd release 19 (one release after marking it "deprecated").
1057 * @param Var Variable name.
1058 * @param Arg Argument string.
1059 * @returns true if a NoXXX option has been processed; false otherwise.
1062 CheckLegacyNoOption(const char *Var, const char *Arg)
1064 if(strcasecmp(Var, "NoDNS") == 0) {
1065 Conf_DNS = !Check_ArgIsTrue( Arg );
1068 if (strcasecmp(Var, "NoIdent") == 0) {
1069 Conf_Ident = !Check_ArgIsTrue(Arg);
1072 if(strcasecmp(Var, "NoPAM") == 0) {
1073 Conf_PAM = !Check_ArgIsTrue(Arg);
1080 * Handle deprecated legacy options in [GLOBAL] section.
1082 * TODO: This function and support for these options in the [Global] section
1083 * could be removed starting with ngIRCd release 19 (one release after
1084 * marking it "deprecated").
1086 * @param Var Variable name.
1087 * @param Arg Argument string.
1088 * @returns true if a legacy option has been processed; false otherwise.
1091 CheckLegacyGlobalOption(int Line, char *Var, char *Arg)
1093 if (strcasecmp(Var, "AllowRemoteOper") == 0
1094 || strcasecmp(Var, "ChrootDir") == 0
1095 || strcasecmp(Var, "ConnectIPv4") == 0
1096 || strcasecmp(Var, "ConnectIPv6") == 0
1097 || strcasecmp(Var, "OperCanUseMode") == 0
1098 || strcasecmp(Var, "OperServerMode") == 0
1099 || strcasecmp(Var, "PredefChannelsOnly") == 0
1101 || strcasecmp(Var, "SSLCertFile") == 0
1102 || strcasecmp(Var, "SSLDHFile") == 0
1103 || strcasecmp(Var, "SSLKeyFile") == 0
1104 || strcasecmp(Var, "SSLKeyFilePassword") == 0
1105 || strcasecmp(Var, "SSLPorts") == 0
1107 || strcasecmp(Var, "SyslogFacility") == 0
1108 || strcasecmp(Var, "WebircPassword") == 0) {
1109 Handle_OPTIONS(Line, Var, Arg);
1112 if (strcasecmp(Var, "ConnectRetry") == 0
1113 || strcasecmp(Var, "MaxConnections") == 0
1114 || strcasecmp(Var, "MaxConnectionsIP") == 0
1115 || strcasecmp(Var, "MaxJoins") == 0
1116 || strcasecmp(Var, "MaxNickLength") == 0
1117 || strcasecmp(Var, "PingTimeout") == 0
1118 || strcasecmp(Var, "PongTimeout") == 0) {
1119 Handle_LIMITS(Line, Var, Arg);
1127 * Strip "no" prefix of a string.
1129 * TODO: This function and support for "NoXXX" should be removed starting
1130 * with ngIRCd release 19! (One release after marking it "deprecated").
1132 * @param str Pointer to input string starting with "no".
1133 * @returns New pointer to string without "no" prefix.
1136 NoNo(const char *str)
1138 assert(strncasecmp("no", str, 2) == 0 && str[2]);
1143 * Invert "boolean" string.
1145 * TODO: This function and support for "NoXXX" should be removed starting
1146 * with ngIRCd release 19! (One release after marking it "deprecated").
1148 * @param arg "Boolean" input string.
1149 * @returns Pointer to inverted "boolean string".
1152 InvertArg(const char *arg)
1154 return yesno_to_str(!Check_ArgIsTrue(arg));
1158 * Handle variable in [Global] configuration section.
1160 * @param Line Line numer in configuration file.
1161 * @param Var Variable name.
1162 * @param Arg Variable argument.
1165 Handle_GLOBAL( int Line, char *Var, char *Arg )
1170 const char *section;
1173 assert(Var != NULL);
1174 assert(Arg != NULL);
1176 if (strcasecmp(Var, "Name") == 0) {
1177 len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
1178 if (len >= sizeof(Conf_ServerName))
1179 Config_Error_TooLong(Line, Var);
1182 if (strcasecmp(Var, "AdminInfo1") == 0) {
1183 len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
1184 if (len >= sizeof(Conf_ServerAdmin1))
1185 Config_Error_TooLong(Line, Var);
1188 if (strcasecmp(Var, "AdminInfo2") == 0) {
1189 len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
1190 if (len >= sizeof(Conf_ServerAdmin2))
1191 Config_Error_TooLong(Line, Var);
1194 if (strcasecmp(Var, "AdminEMail") == 0) {
1195 len = strlcpy(Conf_ServerAdminMail, Arg,
1196 sizeof(Conf_ServerAdminMail));
1197 if (len >= sizeof(Conf_ServerAdminMail))
1198 Config_Error_TooLong(Line, Var);
1201 if (strcasecmp(Var, "Info") == 0) {
1202 len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
1203 if (len >= sizeof(Conf_ServerInfo))
1204 Config_Error_TooLong(Line, Var);
1207 if (strcasecmp(Var, "Listen") == 0) {
1208 if (Conf_ListenAddress) {
1209 Config_Error(LOG_ERR,
1210 "Multiple Listen= options, ignoring: %s",
1214 Conf_ListenAddress = strdup_warn(Arg);
1215 /* If allocation fails, we're in trouble: we cannot ignore the
1216 * error -- otherwise ngircd would listen on all interfaces. */
1217 if (!Conf_ListenAddress) {
1218 Config_Error(LOG_ALERT,
1219 "%s exiting due to fatal errors!",
1225 if (strcasecmp(Var, "MotdFile") == 0) {
1226 len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
1227 if (len >= sizeof(Conf_MotdFile))
1228 Config_Error_TooLong(Line, Var);
1231 if (strcasecmp(Var, "MotdPhrase") == 0) {
1235 if (len >= LINE_LEN) {
1236 Config_Error_TooLong(Line, Var);
1239 if (!array_copyb(&Conf_Motd, Arg, len + 1))
1240 Config_Error(LOG_WARNING,
1241 "%s, line %d: Could not append MotdPhrase: %s",
1242 NGIRCd_ConfFile, Line, strerror(errno));
1243 Using_MotdFile = false;
1246 if(strcasecmp(Var, "Password") == 0) {
1247 len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
1248 if (len >= sizeof(Conf_ServerPwd))
1249 Config_Error_TooLong(Line, Var);
1252 if (strcasecmp(Var, "PidFile") == 0) {
1253 len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
1254 if (len >= sizeof(Conf_PidFile))
1255 Config_Error_TooLong(Line, Var);
1258 if (strcasecmp(Var, "Ports") == 0) {
1259 ports_parse(&Conf_ListenPorts, Line, Arg);
1262 if (strcasecmp(Var, "ServerGID") == 0) {
1263 grp = getgrnam(Arg);
1265 Conf_GID = grp->gr_gid;
1267 Conf_GID = (unsigned int)atoi(Arg);
1268 if (!Conf_GID && strcmp(Arg, "0"))
1269 Config_Error_NaN(Line, Var);
1273 if (strcasecmp(Var, "ServerUID") == 0) {
1274 pwd = getpwnam(Arg);
1276 Conf_UID = pwd->pw_uid;
1278 Conf_UID = (unsigned int)atoi(Arg);
1279 if (!Conf_UID && strcmp(Arg, "0"))
1280 Config_Error_NaN(Line, Var);
1285 if (CheckLegacyNoOption(Var, Arg)) {
1286 /* TODO: This function and support for "NoXXX" could be
1287 * be removed starting with ngIRCd release 19 (one release
1288 * after marking it "deprecated"). */
1289 Config_Error(LOG_WARNING,
1290 "%s, line %d (section \"Global\"): \"No\"-Prefix is deprecated, use \"%s = %s\" in [Options] section!",
1291 NGIRCd_ConfFile, Line, NoNo(Var), InvertArg(Arg));
1292 if (strcasecmp(Var, "NoIdent") == 0)
1294 else if (strcasecmp(Var, "NoPam") == 0)
1298 if ((section = CheckLegacyGlobalOption(Line, Var, Arg))) {
1299 /** TODO: This function and support for these options in the
1300 * [Global] section could be removed starting with ngIRCd
1301 * release 19 (one release after marking it "deprecated"). */
1302 Config_Error(LOG_WARNING,
1303 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s!",
1304 NGIRCd_ConfFile, Line, Var, section);
1308 Config_Error_Section(Line, Var, "Global");
1312 * Handle variable in [Limits] configuration section.
1314 * @param Line Line numer in configuration file.
1315 * @param Var Variable name.
1316 * @param Arg Variable argument.
1319 Handle_LIMITS(int Line, char *Var, char *Arg)
1322 assert(Var != NULL);
1323 assert(Arg != NULL);
1325 if (strcasecmp(Var, "ConnectRetry") == 0) {
1326 Conf_ConnectRetry = atoi(Arg);
1327 if (Conf_ConnectRetry < 5) {
1328 Config_Error(LOG_WARNING,
1329 "%s, line %d: Value of \"ConnectRetry\" too low!",
1330 NGIRCd_ConfFile, Line);
1331 Conf_ConnectRetry = 5;
1335 if (strcasecmp(Var, "MaxConnections") == 0) {
1336 Conf_MaxConnections = atol(Arg);
1337 if (!Conf_MaxConnections && strcmp(Arg, "0"))
1338 Config_Error_NaN(Line, Var);
1341 if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
1342 Conf_MaxConnectionsIP = atoi(Arg);
1343 if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
1344 Config_Error_NaN(Line, Var);
1347 if (strcasecmp(Var, "MaxJoins") == 0) {
1348 Conf_MaxJoins = atoi(Arg);
1349 if (!Conf_MaxJoins && strcmp(Arg, "0"))
1350 Config_Error_NaN(Line, Var);
1353 if (strcasecmp(Var, "MaxNickLength") == 0) {
1354 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1357 if (strcasecmp(Var, "PingTimeout") == 0) {
1358 Conf_PingTimeout = atoi(Arg);
1359 if (Conf_PingTimeout < 5) {
1360 Config_Error(LOG_WARNING,
1361 "%s, line %d: Value of \"PingTimeout\" too low!",
1362 NGIRCd_ConfFile, Line);
1363 Conf_PingTimeout = 5;
1367 if (strcasecmp(Var, "PongTimeout") == 0) {
1368 Conf_PongTimeout = atoi(Arg);
1369 if (Conf_PongTimeout < 5) {
1370 Config_Error(LOG_WARNING,
1371 "%s, line %d: Value of \"PongTimeout\" too low!",
1372 NGIRCd_ConfFile, Line);
1373 Conf_PongTimeout = 5;
1378 Config_Error_Section(Line, Var, "Limits");
1382 * Handle variable in [Options] configuration section.
1384 * @param Line Line numer in configuration file.
1385 * @param Var Variable name.
1386 * @param Arg Variable argument.
1389 Handle_OPTIONS(int Line, char *Var, char *Arg)
1394 assert(Var != NULL);
1395 assert(Arg != NULL);
1397 if (strcasecmp(Var, "AllowRemoteOper") == 0) {
1398 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1401 if (strcasecmp(Var, "ChrootDir") == 0) {
1402 len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
1403 if (len >= sizeof(Conf_Chroot))
1404 Config_Error_TooLong(Line, Var);
1407 if (strcasecmp(Var, "CloakHost") == 0) {
1408 len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
1409 if (len >= sizeof(Conf_CloakHost))
1410 Config_Error_TooLong(Line, Var);
1413 if (strcasecmp(Var, "CloakUserToNick") == 0) {
1414 Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
1417 if (strcasecmp(Var, "ConnectIPv6") == 0) {
1418 Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
1422 if (strcasecmp(Var, "ConnectIPv4") == 0) {
1423 Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
1426 if (strcasecmp(Var, "DNS") == 0) {
1427 Conf_DNS = Check_ArgIsTrue(Arg);
1430 if (strcasecmp(Var, "Ident") == 0) {
1431 Conf_Ident = Check_ArgIsTrue(Arg);
1435 if (strcasecmp(Var, "NoticeAuth") == 0) {
1436 Conf_NoticeAuth = Check_ArgIsTrue(Arg);
1439 if (strcasecmp(Var, "OperCanUseMode") == 0) {
1440 Conf_OperCanMode = Check_ArgIsTrue(Arg);
1443 if (strcasecmp(Var, "OperServerMode") == 0) {
1444 Conf_OperServerMode = Check_ArgIsTrue(Arg);
1447 if (strcasecmp(Var, "PAM") == 0) {
1448 Conf_PAM = Check_ArgIsTrue(Arg);
1452 if (strcasecmp(Var, "PredefChannelsOnly") == 0) {
1453 Conf_PredefChannelsOnly = Check_ArgIsTrue(Arg);
1457 if (strcasecmp(Var, "RequireAuthPing") == 0) {
1458 Conf_AuthPing = Check_ArgIsTrue(Arg);
1463 if (strcasecmp(Var, "SSLCertFile") == 0) {
1464 assert(Conf_SSLOptions.CertFile == NULL);
1465 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1468 if (strcasecmp(Var, "SSLDHFile") == 0) {
1469 assert(Conf_SSLOptions.DHFile == NULL);
1470 Conf_SSLOptions.DHFile = strdup_warn(Arg);
1473 if (strcasecmp(Var, "SSLKeyFile") == 0) {
1474 assert(Conf_SSLOptions.KeyFile == NULL);
1475 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1478 if (strcasecmp(Var, "SSLKeyFilePassword") == 0) {
1479 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1480 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1481 Config_Error(LOG_ERR,
1482 "%s, line %d (section \"Global\"): Could not copy %s: %s!",
1483 NGIRCd_ConfFile, Line, Var,
1487 if (strcasecmp(Var, "SSLPorts") == 0) {
1488 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1493 if (strcasecmp(Var, "SyslogFacility") == 0) {
1494 Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
1495 Conf_SyslogFacility);
1499 if (strcasecmp(Var, "WebircPassword") == 0) {
1500 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
1501 if (len >= sizeof(Conf_WebircPwd))
1502 Config_Error_TooLong(Line, Var);
1506 Config_Error_Section(Line, Var, "Options");
1510 * Handle variable in [Operator] configuration section.
1512 * @param Line Line numer in configuration file.
1513 * @param Var Variable name.
1514 * @param Arg Variable argument.
1517 Handle_OPERATOR( int Line, char *Var, char *Arg )
1520 struct Conf_Oper *op;
1523 assert( Var != NULL );
1524 assert( Arg != NULL );
1525 assert( Conf_Oper_Count > 0 );
1527 op = array_alloc(&Conf_Opers, sizeof(*op), Conf_Oper_Count - 1);
1529 Config_Error(LOG_ERR, "Could not allocate memory for operator (%d:%s = %s)", Line, Var, Arg);
1533 if (strcasecmp(Var, "Name") == 0) {
1534 /* Name of IRC operator */
1535 len = strlcpy(op->name, Arg, sizeof(op->name));
1536 if (len >= sizeof(op->name))
1537 Config_Error_TooLong(Line, Var);
1540 if (strcasecmp(Var, "Password") == 0) {
1541 /* Password of IRC operator */
1542 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1543 if (len >= sizeof(op->pwd))
1544 Config_Error_TooLong(Line, Var);
1547 if (strcasecmp(Var, "Mask") == 0) {
1549 return; /* Hostname already configured */
1550 op->mask = strdup_warn( Arg );
1554 Config_Error_Section(Line, Var, "Operator");
1558 * Handle variable in [Server] configuration section.
1560 * @param Line Line numer in configuration file.
1561 * @param Var Variable name.
1562 * @param Arg Variable argument.
1565 Handle_SERVER( int Line, char *Var, char *Arg )
1571 assert( Var != NULL );
1572 assert( Arg != NULL );
1574 /* Ignore server block if no space is left in server configuration structure */
1575 if( New_Server_Idx <= NONE ) return;
1577 if( strcasecmp( Var, "Host" ) == 0 ) {
1578 /* Hostname of the server */
1579 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1580 if (len >= sizeof( New_Server.host ))
1581 Config_Error_TooLong ( Line, Var );
1584 if( strcasecmp( Var, "Name" ) == 0 ) {
1585 /* Name of the server ("Nick"/"ID") */
1586 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1587 if (len >= sizeof( New_Server.name ))
1588 Config_Error_TooLong( Line, Var );
1591 if (strcasecmp(Var, "Bind") == 0) {
1592 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1595 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1596 NGIRCd_ConfFile, Line, Arg);
1599 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1600 /* Password of this server which is sent to the peer */
1602 Config_Error(LOG_ERR,
1603 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1604 NGIRCd_ConfFile, Line);
1606 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1607 if (len >= sizeof( New_Server.pwd_in ))
1608 Config_Error_TooLong( Line, Var );
1611 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1612 /* Passwort of the peer which must be received */
1613 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1614 if (len >= sizeof( New_Server.pwd_out ))
1615 Config_Error_TooLong( Line, Var );
1618 if( strcasecmp( Var, "Port" ) == 0 ) {
1619 /* Port to which this server should connect */
1621 if (port >= 0 && port < 0xFFFF)
1622 New_Server.port = (UINT16)port;
1624 Config_Error(LOG_ERR,
1625 "%s, line %d (section \"Server\"): Illegal port number %ld!",
1626 NGIRCd_ConfFile, Line, port );
1630 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1631 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1635 if( strcasecmp( Var, "Group" ) == 0 ) {
1637 New_Server.group = atoi( Arg );
1638 if (!New_Server.group && strcmp(Arg, "0"))
1639 Config_Error_NaN(Line, Var);
1642 if( strcasecmp( Var, "Passive" ) == 0 ) {
1643 if (Check_ArgIsTrue(Arg))
1644 New_Server.flags |= CONF_SFLAG_DISABLED;
1647 if (strcasecmp(Var, "ServiceMask") == 0) {
1648 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1649 sizeof(New_Server.svs_mask));
1650 if (len >= sizeof(New_Server.svs_mask))
1651 Config_Error_TooLong(Line, Var);
1655 Config_Error_Section(Line, Var, "Server");
1659 * Copy channel name into channel structure.
1661 * If the channel name is not valid because of a missing prefix ('#', '&'),
1662 * a default prefix of '#' will be added.
1664 * @param new_chan New already allocated channel structure.
1665 * @param name Name of the new channel.
1666 * @returns true on success, false otherwise.
1669 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1671 size_t size = sizeof(new_chan->name);
1672 char *dest = new_chan->name;
1674 if (!Channel_IsValidName(name)) {
1676 * maybe user forgot to add a '#'.
1677 * This is only here for user convenience.
1683 return size > strlcpy(dest, name, size);
1687 * Handle variable in [Channel] configuration section.
1689 * @param Line Line numer in configuration file.
1690 * @param Var Variable name.
1691 * @param Arg Variable argument.
1694 Handle_CHANNEL(int Line, char *Var, char *Arg)
1698 struct Conf_Channel *chan;
1701 assert( Var != NULL );
1702 assert( Arg != NULL );
1703 assert(Conf_Channel_Count > 0);
1705 chancount = Conf_Channel_Count - 1;
1707 chan = array_alloc(&Conf_Channels, sizeof(*chan), chancount);
1709 Config_Error(LOG_ERR, "Could not allocate memory for predefined channel (%d:%s = %s)", Line, Var, Arg);
1712 if (strcasecmp(Var, "Name") == 0) {
1713 if (!Handle_Channelname(chan, Arg))
1714 Config_Error_TooLong(Line, Var);
1717 if (strcasecmp(Var, "Modes") == 0) {
1719 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1720 if (len >= sizeof(chan->modes))
1721 Config_Error_TooLong( Line, Var );
1724 if( strcasecmp( Var, "Topic" ) == 0 ) {
1726 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1727 if (len >= sizeof(chan->topic))
1728 Config_Error_TooLong( Line, Var );
1731 if( strcasecmp( Var, "Key" ) == 0 ) {
1732 /* Initial Channel Key (mode k) */
1733 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1734 if (len >= sizeof(chan->key))
1735 Config_Error_TooLong(Line, Var);
1738 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1739 /* maximum user limit, mode l */
1740 chan->maxusers = (unsigned long) atol(Arg);
1741 if (!chan->maxusers && strcmp(Arg, "0"))
1742 Config_Error_NaN(Line, Var);
1745 if (strcasecmp(Var, "KeyFile") == 0) {
1747 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1748 if (len >= sizeof(chan->keyfile))
1749 Config_Error_TooLong(Line, Var);
1753 Config_Error_Section(Line, Var, "Channel");
1757 * Validate server configuration.
1759 * Please note that this function uses exit(1) on fatal errors and therefore
1760 * can result in ngIRCd terminating!
1762 * @param Configtest true if the daemon has been called with "--configtest".
1763 * @param Rehash true if re-reading configuration on runtime.
1764 * @returns true if configuration is valid.
1767 Validate_Config(bool Configtest, bool Rehash)
1769 /* Validate configuration settings. */
1772 int i, servers, servers_once;
1774 bool config_valid = true;
1777 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1778 ptr = Conf_ServerName;
1780 if (*ptr >= 'a' && *ptr <= 'z') continue;
1781 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1782 if (*ptr >= '0' && *ptr <= '9') continue;
1783 if (ptr > Conf_ServerName) {
1784 if (*ptr == '.' || *ptr == '-')
1787 Conf_ServerName[0] = '\0';
1791 if (!Conf_ServerName[0]) {
1792 /* No server name configured! */
1793 config_valid = false;
1794 Config_Error(LOG_ALERT,
1795 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1797 if (!Configtest && !Rehash) {
1798 Config_Error(LOG_ALERT,
1799 "%s exiting due to fatal errors!",
1805 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1806 /* No dot in server name! */
1807 config_valid = false;
1808 Config_Error(LOG_ALERT,
1809 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1812 Config_Error(LOG_ALERT,
1813 "%s exiting due to fatal errors!",
1820 if (!Conf_ServerAdminMail[0]) {
1821 /* No administrative contact configured! */
1822 config_valid = false;
1823 Config_Error(LOG_ALERT,
1824 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1827 Config_Error(LOG_ALERT,
1828 "%s exiting due to fatal errors!",
1835 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1836 && !Conf_ServerAdminMail[0]) {
1837 /* No administrative information configured! */
1838 Config_Error(LOG_WARNING,
1839 "No administrative information configured but required by RFC!");
1843 if (Conf_ServerPwd[0])
1844 Config_Error(LOG_ERR,
1845 "This server uses PAM, \"Password\" will be ignored!");
1849 servers = servers_once = 0;
1850 for (i = 0; i < MAX_SERVERS; i++) {
1851 if (Conf_Server[i].name[0]) {
1853 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1858 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1859 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1862 return config_valid;
1866 * Output "line too long" warning.
1868 * @param Line Line number in configuration file.
1869 * @param Item Affected variable name.
1872 Config_Error_TooLong ( const int Line, const char *Item )
1874 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1878 * Output "unknown variable" warning.
1880 * @param Line Line number in configuration file.
1881 * @param Item Affected variable name.
1882 * @param Section Section name.
1885 Config_Error_Section(const int Line, const char *Item, const char *Section)
1887 Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
1888 NGIRCd_ConfFile, Line, Section, Item);
1892 * Output "not a number" warning.
1894 * @param Line Line number in configuration file.
1895 * @param Item Affected variable name.
1898 Config_Error_NaN( const int Line, const char *Item )
1900 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1901 NGIRCd_ConfFile, Line, Item );
1905 * Output configuration error to console and/or logfile.
1907 * On runtime, the normal log functions of the daemon are used. But when
1908 * testing the configuration ("--configtest"), all messages go directly
1911 * @param Level Severity level of the message.
1912 * @param Format Format string; see printf() function.
1915 static void Config_Error( const int Level, const char *Format, ... )
1917 static void Config_Error( Level, Format, va_alist )
1923 char msg[MAX_LOG_MSG_LEN];
1926 assert( Format != NULL );
1929 va_start( ap, Format );
1933 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1937 if (Level <= LOG_WARNING)
1938 printf(" - %s\n", msg);
1942 Log(Level, "%s", msg);
1948 * Dump internal state of the "configuration module".
1951 Conf_DebugDump(void)
1955 Log(LOG_DEBUG, "Configured servers:");
1956 for (i = 0; i < MAX_SERVERS; i++) {
1957 if (! Conf_Server[i].name[0])
1960 " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
1961 Conf_Server[i].name, Conf_Server[i].host,
1962 Conf_Server[i].port, Conf_Server[i].lasttry,
1963 Conf_Server[i].group, Conf_Server[i].flags,
1964 Conf_Server[i].conn_id);
1971 * Initialize server configuration structur to default values.
1973 * @param Server Pointer to server structure to initialize.
1976 Init_Server_Struct( CONF_SERVER *Server )
1978 assert( Server != NULL );
1980 memset( Server, 0, sizeof (CONF_SERVER) );
1982 Server->group = NONE;
1983 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1985 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1987 Proc_InitStruct(&Server->res_stat);
1988 Server->conn_id = NONE;
1989 memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));