2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2012 Alexander Barton (alex@barton.de) and Contributors.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
16 * Configuration management (reading, parsing & validation)
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 char Conf_MotdFile[FNAME_LEN];
60 static void Set_Defaults PARAMS(( bool InitServers ));
61 static bool Read_Config PARAMS(( bool TestOnly, bool IsStarting ));
62 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
64 static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
65 static void Handle_LIMITS PARAMS(( int Line, char *Var, char *Arg ));
66 static void Handle_OPTIONS PARAMS(( int Line, char *Var, char *Arg ));
67 static void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
68 static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
69 static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
71 static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
73 static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
74 static void Config_Error_Section PARAMS(( const int Line, const char *Item,
75 const char *Section ));
76 static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
78 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
82 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
84 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
90 static void Handle_SSL PARAMS(( int Line, char *Var, char *Ark ));
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)
127 fp = fopen(Filename, "r");
131 Config_Error(LOG_ERR, "Can't read \"%s\" (\"%s\"): %s",
132 Filename, Var, strerror(errno));
139 * Duplicate string and warn on errors.
141 * @returns Pointer to string on success, NULL otherwise.
144 strdup_warn(const char *str)
146 char *ptr = strdup(str);
148 Config_Error(LOG_ERR,
149 "Could not allocate memory for string: %s", str);
154 * Output a comma separated list of ports (integer values).
161 len = array_length(a, sizeof(UINT16));
163 ports = (UINT16*) array_start(a);
164 printf("%u", (unsigned int) *ports);
167 printf(", %u", (unsigned int) *ports);
174 * Parse a comma separated string into an array of port numbers (integers).
177 ports_parse(array *a, int Line, char *Arg)
185 ptr = strtok( Arg, "," );
189 if (port > 0 && port < 0xFFFF) {
190 port16 = (UINT16) port;
191 if (!array_catb(a, (char*)&port16, sizeof port16))
192 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
193 NGIRCd_ConfFile, Line, port, strerror(errno));
195 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
196 NGIRCd_ConfFile, Line, port );
199 ptr = strtok( NULL, "," );
204 * Initialize configuration module.
209 Read_Config(false, true);
210 Validate_Config(false, false);
214 * "Rehash" (reload) server configuration.
216 * @returns true if configuration has been re-read, false on errors.
221 if (!Read_Config(false, false))
223 Validate_Config(false, true);
225 /* Update CLIENT structure of local server */
226 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
231 * Output a boolean value as "yes/no" string.
234 yesno_to_str(int boolean_value)
242 * Free all IRC operator configuration structures.
247 struct Conf_Oper *op;
250 len = array_length(&Conf_Opers, sizeof(*op));
251 op = array_start(&Conf_Opers);
256 array_free(&Conf_Opers);
260 * Output all IRC operator configuration structures.
265 struct Conf_Oper *op;
268 count = array_length(&Conf_Opers, sizeof(*op));
269 op = array_start(&Conf_Opers);
270 for (i = 0; i < count; i++, op++) {
275 printf(" Name = %s\n", op->name);
276 printf(" Password = %s\n", op->pwd);
277 printf(" Mask = %s\n\n", op->mask ? op->mask : "");
282 * Read configuration, validate and output it.
284 * This function waits for a keypress of the user when stdin/stdout are valid
285 * tty's ("you can read our nice message and we can read in your keypress").
287 * @return 0 on succes, 1 on failure(s); therefore the result code can
288 * directly be used by exit() when running "ngircd --configtest".
297 size_t predef_channel_count;
298 struct Conf_Channel *predef_chan;
302 if (!Read_Config(true, true))
305 config_valid = Validate_Config(true, false);
308 if(isatty(fileno(stdin)) && isatty(fileno(stdout))) {
309 puts("OK, press enter to see a dump of your server configuration ...");
312 puts("Ok, dump of your server configuration follows:\n");
315 printf(" Name = %s\n", Conf_ServerName);
316 printf(" AdminInfo1 = %s\n", Conf_ServerAdmin1);
317 printf(" AdminInfo2 = %s\n", Conf_ServerAdmin2);
318 printf(" AdminEMail = %s\n", Conf_ServerAdminMail);
319 printf(" Info = %s\n", Conf_ServerInfo);
320 printf(" Listen = %s\n", Conf_ListenAddress);
321 if (Using_MotdFile) {
322 printf(" MotdFile = %s\n", Conf_MotdFile);
323 printf(" MotdPhrase =\n");
325 printf(" MotdFile = \n");
326 printf(" MotdPhrase = %s\n", array_bytes(&Conf_Motd)
327 ? (const char*) array_start(&Conf_Motd) : "");
330 printf(" Password = %s\n", Conf_ServerPwd);
332 printf(" PidFile = %s\n", Conf_PidFile);
334 ports_puts(&Conf_ListenPorts);
335 grp = getgrgid(Conf_GID);
337 printf(" ServerGID = %s\n", grp->gr_name);
339 printf(" ServerGID = %ld\n", (long)Conf_GID);
340 pwd = getpwuid(Conf_UID);
342 printf(" ServerUID = %s\n", pwd->pw_name);
344 printf(" ServerUID = %ld\n", (long)Conf_UID);
348 printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
349 printf(" MaxConnections = %ld\n", Conf_MaxConnections);
350 printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
351 printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
352 printf(" MaxNickLength = %u\n", Conf_MaxNickLength - 1);
353 printf(" PingTimeout = %d\n", Conf_PingTimeout);
354 printf(" PongTimeout = %d\n", Conf_PongTimeout);
358 printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
359 printf(" ChrootDir = %s\n", Conf_Chroot);
360 printf(" CloakHost = %s\n", Conf_CloakHost);
361 printf(" CloakHostModeX = %s\n", Conf_CloakHostModeX);
362 printf(" CloakHostSalt = %s\n", Conf_CloakHostSalt);
363 printf(" CloakUserToNick = %s\n", yesno_to_str(Conf_CloakUserToNick));
365 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
366 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
368 printf(" DNS = %s\n", yesno_to_str(Conf_DNS));
370 printf(" Ident = %s\n", yesno_to_str(Conf_Ident));
372 printf(" MorePrivacy = %s\n", yesno_to_str(Conf_MorePrivacy));
373 printf(" NoticeAuth = %s\n", yesno_to_str(Conf_NoticeAuth));
374 printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
375 printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
377 printf(" PAM = %s\n", yesno_to_str(Conf_PAM));
378 printf(" PAMIsOptional = %s\n", yesno_to_str(Conf_PAMIsOptional));
380 printf(" PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
382 printf(" RequireAuthPing = %s\n", yesno_to_str(Conf_AuthPing));
384 printf(" ScrubCTCP = %s\n", yesno_to_str(Conf_ScrubCTCP));
386 printf(" SyslogFacility = %s\n",
387 ngt_SyslogFacilityName(Conf_SyslogFacility));
389 printf(" WebircPassword = %s\n", Conf_WebircPwd);
394 printf(" CertFile = %s\n", Conf_SSLOptions.CertFile
395 ? Conf_SSLOptions.CertFile : "");
396 printf(" DHFile = %s\n", Conf_SSLOptions.DHFile
397 ? Conf_SSLOptions.DHFile : "");
398 printf(" KeyFile = %s\n", Conf_SSLOptions.KeyFile
399 ? Conf_SSLOptions.KeyFile : "");
400 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
401 puts(" KeyFilePassword = <secret>");
403 puts(" KeyFilePassword = ");
404 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
406 ports_puts(&Conf_SSLOptions.ListenPorts);
412 for( i = 0; i < MAX_SERVERS; i++ ) {
413 if( ! Conf_Server[i].name[0] ) continue;
415 /* Valid "Server" section */
417 printf( " Name = %s\n", Conf_Server[i].name );
418 printf( " Host = %s\n", Conf_Server[i].host );
419 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
421 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
423 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
424 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
425 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
426 printf( " Group = %d\n", Conf_Server[i].group );
427 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
430 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
431 predef_chan = array_start(&Conf_Channels);
433 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
434 if (!predef_chan->name[0])
437 /* Valid "Channel" section */
439 printf(" Name = %s\n", predef_chan->name);
440 printf(" Modes = %s\n", predef_chan->modes);
441 printf(" Key = %s\n", predef_chan->key);
442 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
443 printf(" Topic = %s\n", predef_chan->topic);
444 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
447 return (config_valid ? 0 : 1);
451 * Remove connection information from configured server.
453 * If the server is set as "once", delete it from our configuration;
454 * otherwise set the time for the next connection attempt.
456 * Non-server connections will be silently ignored.
459 Conf_UnsetServer( CONN_ID Idx )
464 /* Check all our configured servers */
465 for( i = 0; i < MAX_SERVERS; i++ ) {
466 if( Conf_Server[i].conn_id != Idx ) continue;
468 /* Gotcha! Mark server configuration as "unused": */
469 Conf_Server[i].conn_id = NONE;
471 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
472 /* Delete configuration here */
473 Init_Server_Struct( &Conf_Server[i] );
475 /* Set time for next connect attempt */
477 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
478 /* The connection has been "long", so we don't
479 * require the next attempt to be delayed. */
480 Conf_Server[i].lasttry =
481 t - Conf_ConnectRetry + RECONNECT_DELAY;
483 Conf_Server[i].lasttry = t;
489 * Set connection information for specified configured server.
492 Conf_SetServer( int ConfServer, CONN_ID Idx )
494 assert( ConfServer > NONE );
495 assert( Idx > NONE );
497 if (Conf_Server[ConfServer].conn_id > NONE &&
498 Conf_Server[ConfServer].conn_id != Idx) {
500 "Trying to update connection index for already registered server \"%s\": %d/%d - ignored.",
501 Conf_Server[ConfServer].name,
502 Conf_Server[ConfServer].conn_id, Idx);
505 Conf_Server[ConfServer].conn_id = Idx;
509 * Get index of server in configuration structure.
512 Conf_GetServer( CONN_ID Idx )
516 assert( Idx > NONE );
518 for( i = 0; i < MAX_SERVERS; i++ ) {
519 if( Conf_Server[i].conn_id == Idx ) return i;
525 * Enable a server by name and adjust its port number.
527 * @returns true if a server has been enabled and now has a valid port
528 * number and host name for outgoing connections.
531 Conf_EnableServer( const char *Name, UINT16 Port )
535 assert( Name != NULL );
536 for( i = 0; i < MAX_SERVERS; i++ ) {
537 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
538 /* Gotcha! Set port and enable server: */
539 Conf_Server[i].port = Port;
540 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
541 return (Conf_Server[i].port && Conf_Server[i].host[0]);
548 * Enable a server by name.
550 * The server is only usable as outgoing server, if it has set a valid port
551 * number for outgoing connections!
552 * If not, you have to use Conf_EnableServer() function to make it available.
554 * @returns true if a server has been enabled; false otherwise.
557 Conf_EnablePassiveServer(const char *Name)
561 assert( Name != NULL );
562 for (i = 0; i < MAX_SERVERS; i++) {
563 if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
564 && (Conf_Server[i].port > 0)) {
565 /* BINGO! Enable server */
566 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
574 * Disable a server by name.
575 * An already established connection will be disconnected.
577 * @returns true if a server was found and has been disabled.
580 Conf_DisableServer( const char *Name )
584 assert( Name != NULL );
585 for( i = 0; i < MAX_SERVERS; i++ ) {
586 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
587 /* Gotcha! Disable and disconnect server: */
588 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
589 if( Conf_Server[i].conn_id > NONE )
590 Conn_Close(Conf_Server[i].conn_id, NULL,
591 "Server link terminated on operator request",
600 * Add a new remote server to our configuration.
602 * @param Name Name of the new server.
603 * @param Port Port number to connect to or 0 for incoming connections.
604 * @param Host Host name to connect to.
605 * @param MyPwd Password that will be sent to the peer.
606 * @param PeerPwd Password that must be received from the peer.
607 * @returns true if the new server has been added; false otherwise.
610 Conf_AddServer(const char *Name, UINT16 Port, const char *Host,
611 const char *MyPwd, const char *PeerPwd)
615 assert( Name != NULL );
616 assert( Host != NULL );
617 assert( MyPwd != NULL );
618 assert( PeerPwd != NULL );
620 /* Search unused item in server configuration structure */
621 for( i = 0; i < MAX_SERVERS; i++ ) {
622 /* Is this item used? */
623 if( ! Conf_Server[i].name[0] ) break;
625 if( i >= MAX_SERVERS ) return false;
627 Init_Server_Struct( &Conf_Server[i] );
628 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
629 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
630 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
631 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
632 Conf_Server[i].port = Port;
633 Conf_Server[i].flags = CONF_SFLAG_ONCE;
639 * Check if the given nick name is reserved for services on a particular server.
641 * @param ConfServer The server index to check.
642 * @param Nick The nick name to check.
643 * @returns true if the given nick name belongs to an "IRC service".
646 Conf_NickIsService(int ConfServer, const char *Nick)
648 assert (ConfServer >= 0);
649 assert (ConfServer < MAX_SERVERS);
651 return MatchCaseInsensitiveList(Conf_Server[ConfServer].svs_mask,
656 * Check if the given nick name is blocked for "normal client" use.
658 * @param ConfServer The server index or NONE to check all configured servers.
659 * @param Nick The nick name to check.
660 * @returns true if the given nick name belongs to an "IRC service".
663 Conf_NickIsBlocked(const char *Nick)
667 for(i = 0; i < MAX_SERVERS; i++) {
668 if (!Conf_Server[i].name[0])
670 if (Conf_NickIsService(i, Nick))
677 * Initialize configuration settings with their default values.
680 Set_Defaults(bool InitServers)
683 char random[RANDOM_SALT_LEN + 1];
686 strcpy(Conf_ServerName, "");
687 strcpy(Conf_ServerAdmin1, "");
688 strcpy(Conf_ServerAdmin2, "");
689 strcpy(Conf_ServerAdminMail, "");
690 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
691 PACKAGE_NAME, PACKAGE_VERSION);
692 free(Conf_ListenAddress);
693 Conf_ListenAddress = NULL;
694 array_free(&Conf_Motd);
695 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
696 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
697 strcpy(Conf_ServerPwd, "");
698 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
699 Conf_UID = Conf_GID = 0;
702 Conf_ConnectRetry = 60;
703 Conf_MaxConnections = 0;
704 Conf_MaxConnectionsIP = 5;
706 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
707 Conf_PingTimeout = 120;
708 Conf_PongTimeout = 20;
711 Conf_AllowRemoteOper = false;
713 Conf_AuthPing = false;
715 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
716 strcpy(Conf_CloakHost, "");
717 strcpy(Conf_CloakHostModeX, "");
718 strcpy(Conf_CloakHostSalt, ngt_RandomStr(random, RANDOM_SALT_LEN));
719 Conf_CloakUserToNick = false;
720 Conf_ConnectIPv4 = true;
722 Conf_ConnectIPv6 = true;
724 Conf_ConnectIPv6 = false;
732 Conf_MorePrivacy = false;
733 Conf_NoticeAuth = false;
734 Conf_OperCanMode = false;
735 Conf_OperServerMode = false;
741 Conf_PAMIsOptional = false;
742 Conf_PredefChannelsOnly = false;
744 Conf_ScrubCTCP = false;
746 Conf_SyslogFacility = LOG_LOCAL5;
748 Conf_SyslogFacility = 0;
752 /* Initialize server configuration structures */
754 for (i = 0; i < MAX_SERVERS;
755 Init_Server_Struct(&Conf_Server[i++]));
760 * Get number of configured listening ports.
762 * @returns The number of ports (IPv4+IPv6) on which the server should listen.
767 size_t cnt = array_bytes(&Conf_ListenPorts);
769 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
775 * Read MOTD ("message of the day") file.
777 * @param filename Name of the file to read.
780 Read_Motd(const char *filename)
785 if (*filename == '\0')
788 fp = fopen(filename, "r");
790 Config_Error(LOG_WARNING, "Can't read MOTD file \"%s\": %s",
791 filename, strerror(errno));
795 array_free(&Conf_Motd);
796 Using_MotdFile = true;
798 while (fgets(line, (int)sizeof line, fp)) {
799 ngt_TrimLastChr( line, '\n');
801 /* add text including \0 */
802 if (!array_catb(&Conf_Motd, line, strlen(line) + 1)) {
803 Log(LOG_WARNING, "Cannot add MOTD text: %s", strerror(errno));
811 * Read ngIRCd configuration file.
813 * Please note that this function uses exit(1) on fatal errors and therefore
814 * can result in ngIRCd terminating!
816 * @param ngircd_starting Flag indicating if ngIRCd is starting or not.
817 * @returns true when the configuration file has been read
818 * successfully; false otherwise.
821 Read_Config(bool TestOnly, bool IsStarting)
823 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
824 const UINT16 defaultport = 6667;
829 /* Open configuration file */
830 fd = fopen( NGIRCd_ConfFile, "r" );
832 /* No configuration file found! */
833 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
834 NGIRCd_ConfFile, strerror( errno ));
837 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
842 Set_Defaults(IsStarting);
845 Config_Error(LOG_INFO,
846 "Reading configuration from \"%s\" ...",
849 /* Clean up server configuration structure: mark all already
850 * configured servers as "once" so that they are deleted
851 * after the next disconnect and delete all unused servers.
852 * And delete all servers which are "duplicates" of servers
853 * that are already marked as "once" (such servers have been
854 * created by the last rehash but are now useless). */
855 for( i = 0; i < MAX_SERVERS; i++ ) {
856 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
858 /* This structure is in use ... */
859 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
860 /* Check for duplicates */
861 for( n = 0; n < MAX_SERVERS; n++ ) {
862 if( n == i ) continue;
864 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
865 Init_Server_Struct( &Conf_Server[n] );
867 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
873 /* Mark server as "once" */
874 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
875 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
880 /* Initialize variables */
882 strcpy( section, "" );
883 Init_Server_Struct( &New_Server );
884 New_Server_Idx = NONE;
888 /* Read configuration file */
890 if( ! fgets( str, LINE_LEN, fd )) break;
894 /* Skip comments and empty lines */
895 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
897 /* Is this the beginning of a new section? */
898 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
899 strlcpy( section, str, sizeof( section ));
900 if (strcasecmp(section, "[GLOBAL]") == 0
901 || strcasecmp(section, "[LIMITS]") == 0
902 || strcasecmp(section, "[OPTIONS]") == 0
904 || strcasecmp(section, "[SSL]") == 0
909 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
910 /* Check if there is already a server to add */
911 if( New_Server.name[0] ) {
912 /* Copy data to "real" server structure */
913 assert( New_Server_Idx > NONE );
914 Conf_Server[New_Server_Idx] = New_Server;
917 /* Re-init structure for new server */
918 Init_Server_Struct( &New_Server );
920 /* Search unused item in server configuration structure */
921 for( i = 0; i < MAX_SERVERS; i++ ) {
922 /* Is this item used? */
923 if( ! Conf_Server[i].name[0] ) break;
925 if( i >= MAX_SERVERS ) {
926 /* Oops, no free item found! */
927 Config_Error( LOG_ERR, "Too many servers configured." );
928 New_Server_Idx = NONE;
930 else New_Server_Idx = i;
934 if (strcasecmp(section, "[CHANNEL]") == 0) {
935 count = array_length(&Conf_Channels,
936 sizeof(struct Conf_Channel));
937 if (!array_alloc(&Conf_Channels,
938 sizeof(struct Conf_Channel),
940 Config_Error(LOG_ERR,
941 "Could not allocate memory for new operator (line %d)",
947 if (strcasecmp(section, "[OPERATOR]") == 0) {
948 count = array_length(&Conf_Opers,
949 sizeof(struct Conf_Oper));
950 if (!array_alloc(&Conf_Opers,
951 sizeof(struct Conf_Oper),
953 Config_Error(LOG_ERR,
954 "Could not allocate memory for new channel (line &d)",
960 Config_Error(LOG_ERR,
961 "%s, line %d: Unknown section \"%s\"!",
962 NGIRCd_ConfFile, line, section);
965 if( section[0] == 0x1 ) continue;
967 /* Split line into variable name and parameters */
968 ptr = strchr( str, '=' );
970 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
974 var = str; ngt_TrimStr( var );
975 arg = ptr + 1; ngt_TrimStr( arg );
977 if(strcasecmp(section, "[GLOBAL]") == 0)
978 Handle_GLOBAL(line, var, arg);
979 else if(strcasecmp(section, "[LIMITS]") == 0)
980 Handle_LIMITS(line, var, arg);
981 else if(strcasecmp(section, "[OPTIONS]") == 0)
982 Handle_OPTIONS(line, var, arg);
984 else if(strcasecmp(section, "[SSL]") == 0)
985 Handle_SSL(line, var, arg);
987 else if(strcasecmp(section, "[OPERATOR]") == 0)
988 Handle_OPERATOR(line, var, arg);
989 else if(strcasecmp(section, "[SERVER]") == 0)
990 Handle_SERVER(line, var, arg);
991 else if(strcasecmp(section, "[CHANNEL]") == 0)
992 Handle_CHANNEL(line, var, arg);
994 Config_Error(LOG_ERR,
995 "%s, line %d: Variable \"%s\" outside section!",
996 NGIRCd_ConfFile, line, var);
999 /* Close configuration file */
1002 /* Check if there is still a server to add */
1003 if( New_Server.name[0] ) {
1004 /* Copy data to "real" server structure */
1005 assert( New_Server_Idx > NONE );
1006 Conf_Server[New_Server_Idx] = New_Server;
1009 /* not a single listening port? Add default. */
1010 if (no_listenports() &&
1011 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
1013 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
1014 (unsigned int) defaultport, strerror(errno));
1019 if (!Conf_ListenAddress)
1020 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
1022 if (!Conf_ListenAddress) {
1023 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
1027 /* No MOTD phrase configured? (re)try motd file. */
1028 if (array_bytes(&Conf_Motd) == 0)
1029 Read_Motd(Conf_MotdFile);
1032 /* Make sure that all SSL-related files are readable */
1033 CheckFileReadable("CertFile", Conf_SSLOptions.CertFile);
1034 CheckFileReadable("DHFile", Conf_SSLOptions.DHFile);
1035 CheckFileReadable("KeyFile", Conf_SSLOptions.KeyFile);
1042 * Check whether a string argument is "true" or "false".
1044 * @param Arg Input string.
1045 * @returns true if the input string has been parsed as "yes", "true"
1046 * (case insensitive) or a non-zero integer value.
1049 Check_ArgIsTrue(const char *Arg)
1051 if (strcasecmp(Arg, "yes") == 0)
1053 if (strcasecmp(Arg, "true") == 0)
1062 * Handle setting of "MaxNickLength".
1064 * @param Line Line number in configuration file.
1065 * @raram Arg Input string.
1066 * @returns New configured maximum nick name length.
1069 Handle_MaxNickLength(int Line, const char *Arg)
1073 new = (unsigned) atoi(Arg) + 1;
1074 if (new > CLIENT_NICK_LEN) {
1075 Config_Error(LOG_WARNING,
1076 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
1077 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
1078 return CLIENT_NICK_LEN;
1081 Config_Error(LOG_WARNING,
1082 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
1083 NGIRCd_ConfFile, Line);
1090 * Output a warning messages if IDENT is configured but not compiled in.
1093 WarnIdent(int UNUSED Line)
1097 /* user has enabled ident lookups explicitly, but ... */
1098 Config_Error(LOG_WARNING,
1099 "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
1100 NGIRCd_ConfFile, Line);
1106 * Output a warning messages if IPv6 is configured but not compiled in.
1109 WarnIPv6(int UNUSED Line)
1112 if (Conf_ConnectIPv6) {
1113 /* user has enabled IPv6 explicitly, but ... */
1114 Config_Error(LOG_WARNING,
1115 "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
1116 NGIRCd_ConfFile, Line);
1122 * Output a warning messages if PAM is configured but not compiled in.
1125 WarnPAM(int UNUSED Line)
1129 Config_Error(LOG_WARNING,
1130 "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
1131 NGIRCd_ConfFile, Line);
1137 * Handle legacy "NoXXX" options in [GLOBAL] section.
1139 * TODO: This function and support for "NoXXX" could be removed starting
1140 * with ngIRCd release 19 (one release after marking it "deprecated").
1142 * @param Var Variable name.
1143 * @param Arg Argument string.
1144 * @returns true if a NoXXX option has been processed; false otherwise.
1147 CheckLegacyNoOption(const char *Var, const char *Arg)
1149 if(strcasecmp(Var, "NoDNS") == 0) {
1150 Conf_DNS = !Check_ArgIsTrue( Arg );
1153 if (strcasecmp(Var, "NoIdent") == 0) {
1154 Conf_Ident = !Check_ArgIsTrue(Arg);
1157 if(strcasecmp(Var, "NoPAM") == 0) {
1158 Conf_PAM = !Check_ArgIsTrue(Arg);
1165 * Handle deprecated legacy options in [GLOBAL] section.
1167 * TODO: This function and support for these options in the [Global] section
1168 * could be removed starting with ngIRCd release 19 (one release after
1169 * marking it "deprecated").
1171 * @param Var Variable name.
1172 * @param Arg Argument string.
1173 * @returns true if a legacy option has been processed; false otherwise.
1176 CheckLegacyGlobalOption(int Line, char *Var, char *Arg)
1178 if (strcasecmp(Var, "AllowRemoteOper") == 0
1179 || strcasecmp(Var, "ChrootDir") == 0
1180 || strcasecmp(Var, "ConnectIPv4") == 0
1181 || strcasecmp(Var, "ConnectIPv6") == 0
1182 || strcasecmp(Var, "OperCanUseMode") == 0
1183 || strcasecmp(Var, "OperServerMode") == 0
1184 || strcasecmp(Var, "PredefChannelsOnly") == 0
1185 || strcasecmp(Var, "SyslogFacility") == 0
1186 || strcasecmp(Var, "WebircPassword") == 0) {
1187 Handle_OPTIONS(Line, Var, Arg);
1190 if (strcasecmp(Var, "ConnectRetry") == 0
1191 || strcasecmp(Var, "MaxConnections") == 0
1192 || strcasecmp(Var, "MaxConnectionsIP") == 0
1193 || strcasecmp(Var, "MaxJoins") == 0
1194 || strcasecmp(Var, "MaxNickLength") == 0
1195 || strcasecmp(Var, "PingTimeout") == 0
1196 || strcasecmp(Var, "PongTimeout") == 0) {
1197 Handle_LIMITS(Line, Var, Arg);
1201 if (strcasecmp(Var, "SSLCertFile") == 0
1202 || strcasecmp(Var, "SSLDHFile") == 0
1203 || strcasecmp(Var, "SSLKeyFile") == 0
1204 || strcasecmp(Var, "SSLKeyFilePassword") == 0
1205 || strcasecmp(Var, "SSLPorts") == 0) {
1206 Handle_SSL(Line, Var + 3, Arg);
1215 * Strip "no" prefix of a string.
1217 * TODO: This function and support for "NoXXX" should be removed starting
1218 * with ngIRCd release 19! (One release after marking it "deprecated").
1220 * @param str Pointer to input string starting with "no".
1221 * @returns New pointer to string without "no" prefix.
1224 NoNo(const char *str)
1226 assert(strncasecmp("no", str, 2) == 0 && str[2]);
1231 * Invert "boolean" string.
1233 * TODO: This function and support for "NoXXX" should be removed starting
1234 * with ngIRCd release 19! (One release after marking it "deprecated").
1236 * @param arg "Boolean" input string.
1237 * @returns Pointer to inverted "boolean string".
1240 InvertArg(const char *arg)
1242 return yesno_to_str(!Check_ArgIsTrue(arg));
1246 * Handle variable in [Global] configuration section.
1248 * @param Line Line numer in configuration file.
1249 * @param Var Variable name.
1250 * @param Arg Variable argument.
1253 Handle_GLOBAL( int Line, char *Var, char *Arg )
1258 const char *section;
1261 assert(Var != NULL);
1262 assert(Arg != NULL);
1264 if (strcasecmp(Var, "Name") == 0) {
1265 len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
1266 if (len >= sizeof(Conf_ServerName))
1267 Config_Error_TooLong(Line, Var);
1270 if (strcasecmp(Var, "AdminInfo1") == 0) {
1271 len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
1272 if (len >= sizeof(Conf_ServerAdmin1))
1273 Config_Error_TooLong(Line, Var);
1276 if (strcasecmp(Var, "AdminInfo2") == 0) {
1277 len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
1278 if (len >= sizeof(Conf_ServerAdmin2))
1279 Config_Error_TooLong(Line, Var);
1282 if (strcasecmp(Var, "AdminEMail") == 0) {
1283 len = strlcpy(Conf_ServerAdminMail, Arg,
1284 sizeof(Conf_ServerAdminMail));
1285 if (len >= sizeof(Conf_ServerAdminMail))
1286 Config_Error_TooLong(Line, Var);
1289 if (strcasecmp(Var, "Info") == 0) {
1290 len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
1291 if (len >= sizeof(Conf_ServerInfo))
1292 Config_Error_TooLong(Line, Var);
1295 if (strcasecmp(Var, "Listen") == 0) {
1296 if (Conf_ListenAddress) {
1297 Config_Error(LOG_ERR,
1298 "Multiple Listen= options, ignoring: %s",
1302 Conf_ListenAddress = strdup_warn(Arg);
1303 /* If allocation fails, we're in trouble: we cannot ignore the
1304 * error -- otherwise ngircd would listen on all interfaces. */
1305 if (!Conf_ListenAddress) {
1306 Config_Error(LOG_ALERT,
1307 "%s exiting due to fatal errors!",
1313 if (strcasecmp(Var, "MotdFile") == 0) {
1314 len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
1315 if (len >= sizeof(Conf_MotdFile))
1316 Config_Error_TooLong(Line, Var);
1319 if (strcasecmp(Var, "MotdPhrase") == 0) {
1323 if (len >= LINE_LEN) {
1324 Config_Error_TooLong(Line, Var);
1327 if (!array_copyb(&Conf_Motd, Arg, len + 1))
1328 Config_Error(LOG_WARNING,
1329 "%s, line %d: Could not append MotdPhrase: %s",
1330 NGIRCd_ConfFile, Line, strerror(errno));
1331 Using_MotdFile = false;
1334 if(strcasecmp(Var, "Password") == 0) {
1335 len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
1336 if (len >= sizeof(Conf_ServerPwd))
1337 Config_Error_TooLong(Line, Var);
1340 if (strcasecmp(Var, "PidFile") == 0) {
1341 len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
1342 if (len >= sizeof(Conf_PidFile))
1343 Config_Error_TooLong(Line, Var);
1346 if (strcasecmp(Var, "Ports") == 0) {
1347 ports_parse(&Conf_ListenPorts, Line, Arg);
1350 if (strcasecmp(Var, "ServerGID") == 0) {
1351 grp = getgrnam(Arg);
1353 Conf_GID = grp->gr_gid;
1355 Conf_GID = (unsigned int)atoi(Arg);
1356 if (!Conf_GID && strcmp(Arg, "0"))
1357 Config_Error(LOG_WARNING,
1358 "%s, line %d: Value of \"%s\" is not a valid group name or ID!",
1359 NGIRCd_ConfFile, Line, Var);
1363 if (strcasecmp(Var, "ServerUID") == 0) {
1364 pwd = getpwnam(Arg);
1366 Conf_UID = pwd->pw_uid;
1368 Conf_UID = (unsigned int)atoi(Arg);
1369 if (!Conf_UID && strcmp(Arg, "0"))
1370 Config_Error(LOG_WARNING,
1371 "%s, line %d: Value of \"%s\" is not a valid user name or ID!",
1372 NGIRCd_ConfFile, Line, Var);
1377 if (CheckLegacyNoOption(Var, Arg)) {
1378 /* TODO: This function and support for "NoXXX" could be
1379 * be removed starting with ngIRCd release 19 (one release
1380 * after marking it "deprecated"). */
1381 Config_Error(LOG_WARNING,
1382 "%s, line %d (section \"Global\"): \"No\"-Prefix is deprecated, use \"%s = %s\" in [Options] section!",
1383 NGIRCd_ConfFile, Line, NoNo(Var), InvertArg(Arg));
1384 if (strcasecmp(Var, "NoIdent") == 0)
1386 else if (strcasecmp(Var, "NoPam") == 0)
1390 if ((section = CheckLegacyGlobalOption(Line, Var, Arg))) {
1391 /** TODO: This function and support for these options in the
1392 * [Global] section could be removed starting with ngIRCd
1393 * release 19 (one release after marking it "deprecated"). */
1394 if (strncasecmp(Var, "SSL", 3) == 0) {
1395 Config_Error(LOG_WARNING,
1396 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s and rename to \"%s\"!",
1397 NGIRCd_ConfFile, Line, Var, section,
1400 Config_Error(LOG_WARNING,
1401 "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s!",
1402 NGIRCd_ConfFile, Line, Var, section);
1407 Config_Error_Section(Line, Var, "Global");
1411 * Handle variable in [Limits] configuration section.
1413 * @param Line Line numer in configuration file.
1414 * @param Var Variable name.
1415 * @param Arg Variable argument.
1418 Handle_LIMITS(int Line, char *Var, char *Arg)
1421 assert(Var != NULL);
1422 assert(Arg != NULL);
1424 if (strcasecmp(Var, "ConnectRetry") == 0) {
1425 Conf_ConnectRetry = atoi(Arg);
1426 if (Conf_ConnectRetry < 5) {
1427 Config_Error(LOG_WARNING,
1428 "%s, line %d: Value of \"ConnectRetry\" too low!",
1429 NGIRCd_ConfFile, Line);
1430 Conf_ConnectRetry = 5;
1434 if (strcasecmp(Var, "MaxConnections") == 0) {
1435 Conf_MaxConnections = atol(Arg);
1436 if (!Conf_MaxConnections && strcmp(Arg, "0"))
1437 Config_Error_NaN(Line, Var);
1440 if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
1441 Conf_MaxConnectionsIP = atoi(Arg);
1442 if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
1443 Config_Error_NaN(Line, Var);
1446 if (strcasecmp(Var, "MaxJoins") == 0) {
1447 Conf_MaxJoins = atoi(Arg);
1448 if (!Conf_MaxJoins && strcmp(Arg, "0"))
1449 Config_Error_NaN(Line, Var);
1452 if (strcasecmp(Var, "MaxNickLength") == 0) {
1453 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1456 if (strcasecmp(Var, "PingTimeout") == 0) {
1457 Conf_PingTimeout = atoi(Arg);
1458 if (Conf_PingTimeout < 5) {
1459 Config_Error(LOG_WARNING,
1460 "%s, line %d: Value of \"PingTimeout\" too low!",
1461 NGIRCd_ConfFile, Line);
1462 Conf_PingTimeout = 5;
1466 if (strcasecmp(Var, "PongTimeout") == 0) {
1467 Conf_PongTimeout = atoi(Arg);
1468 if (Conf_PongTimeout < 5) {
1469 Config_Error(LOG_WARNING,
1470 "%s, line %d: Value of \"PongTimeout\" too low!",
1471 NGIRCd_ConfFile, Line);
1472 Conf_PongTimeout = 5;
1477 Config_Error_Section(Line, Var, "Limits");
1481 * Handle variable in [Options] configuration section.
1483 * @param Line Line numer in configuration file.
1484 * @param Var Variable name.
1485 * @param Arg Variable argument.
1488 Handle_OPTIONS(int Line, char *Var, char *Arg)
1493 assert(Var != NULL);
1494 assert(Arg != NULL);
1496 if (strcasecmp(Var, "AllowRemoteOper") == 0) {
1497 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1500 if (strcasecmp(Var, "ChrootDir") == 0) {
1501 len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
1502 if (len >= sizeof(Conf_Chroot))
1503 Config_Error_TooLong(Line, Var);
1506 if (strcasecmp(Var, "CloakHost") == 0) {
1507 len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
1508 if (len >= sizeof(Conf_CloakHost))
1509 Config_Error_TooLong(Line, Var);
1512 if (strcasecmp(Var, "CloakHostModeX") == 0) {
1513 len = strlcpy(Conf_CloakHostModeX, Arg, sizeof(Conf_CloakHostModeX));
1514 if (len >= sizeof(Conf_CloakHostModeX))
1515 Config_Error_TooLong(Line, Var);
1518 if (strcasecmp(Var, "CloakHostSalt") == 0) {
1519 len = strlcpy(Conf_CloakHostSalt, Arg, sizeof(Conf_CloakHostSalt));
1520 if (len >= sizeof(Conf_CloakHostSalt))
1521 Config_Error_TooLong(Line, Var);
1524 if (strcasecmp(Var, "CloakUserToNick") == 0) {
1525 Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
1528 if (strcasecmp(Var, "ConnectIPv6") == 0) {
1529 Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
1533 if (strcasecmp(Var, "ConnectIPv4") == 0) {
1534 Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
1537 if (strcasecmp(Var, "DNS") == 0) {
1538 Conf_DNS = Check_ArgIsTrue(Arg);
1541 if (strcasecmp(Var, "Ident") == 0) {
1542 Conf_Ident = Check_ArgIsTrue(Arg);
1546 if (strcasecmp(Var, "MorePrivacy") == 0) {
1547 Conf_MorePrivacy = Check_ArgIsTrue(Arg);
1550 if (strcasecmp(Var, "NoticeAuth") == 0) {
1551 Conf_NoticeAuth = Check_ArgIsTrue(Arg);
1554 if (strcasecmp(Var, "OperCanUseMode") == 0) {
1555 Conf_OperCanMode = Check_ArgIsTrue(Arg);
1558 if (strcasecmp(Var, "OperServerMode") == 0) {
1559 Conf_OperServerMode = Check_ArgIsTrue(Arg);
1562 if (strcasecmp(Var, "PAM") == 0) {
1563 Conf_PAM = Check_ArgIsTrue(Arg);
1567 if (strcasecmp(Var, "PAMIsOptional") == 0 ) {
1568 Conf_PAMIsOptional = Check_ArgIsTrue(Arg);
1571 if (strcasecmp(Var, "PredefChannelsOnly") == 0) {
1572 Conf_PredefChannelsOnly = Check_ArgIsTrue(Arg);
1576 if (strcasecmp(Var, "RequireAuthPing") == 0) {
1577 Conf_AuthPing = Check_ArgIsTrue(Arg);
1581 if (strcasecmp(Var, "ScrubCTCP") == 0) {
1582 Conf_ScrubCTCP = Check_ArgIsTrue(Arg);
1586 if (strcasecmp(Var, "SyslogFacility") == 0) {
1587 Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
1588 Conf_SyslogFacility);
1592 if (strcasecmp(Var, "WebircPassword") == 0) {
1593 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
1594 if (len >= sizeof(Conf_WebircPwd))
1595 Config_Error_TooLong(Line, Var);
1599 Config_Error_Section(Line, Var, "Options");
1605 * Handle variable in [SSL] configuration section.
1607 * @param Line Line numer in configuration file.
1608 * @param Var Variable name.
1609 * @param Arg Variable argument.
1612 Handle_SSL(int Line, char *Var, char *Arg)
1615 assert(Var != NULL);
1616 assert(Arg != NULL);
1618 if (strcasecmp(Var, "CertFile") == 0) {
1619 assert(Conf_SSLOptions.CertFile == NULL);
1620 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1623 if (strcasecmp(Var, "DHFile") == 0) {
1624 assert(Conf_SSLOptions.DHFile == NULL);
1625 Conf_SSLOptions.DHFile = strdup_warn(Arg);
1628 if (strcasecmp(Var, "KeyFile") == 0) {
1629 assert(Conf_SSLOptions.KeyFile == NULL);
1630 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1633 if (strcasecmp(Var, "KeyFilePassword") == 0) {
1634 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1635 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1636 Config_Error(LOG_ERR,
1637 "%s, line %d (section \"SSL\"): Could not copy %s: %s!",
1638 NGIRCd_ConfFile, Line, Var,
1642 if (strcasecmp(Var, "Ports") == 0) {
1643 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1647 Config_Error_Section(Line, Var, "SSL");
1653 * Handle variable in [Operator] configuration section.
1655 * @param Line Line numer in configuration file.
1656 * @param Var Variable name.
1657 * @param Arg Variable argument.
1660 Handle_OPERATOR( int Line, char *Var, char *Arg )
1663 struct Conf_Oper *op;
1666 assert( Var != NULL );
1667 assert( Arg != NULL );
1669 op = array_get(&Conf_Opers, sizeof(*op),
1670 array_length(&Conf_Opers, sizeof(*op)) - 1);
1674 if (strcasecmp(Var, "Name") == 0) {
1675 /* Name of IRC operator */
1676 len = strlcpy(op->name, Arg, sizeof(op->name));
1677 if (len >= sizeof(op->name))
1678 Config_Error_TooLong(Line, Var);
1681 if (strcasecmp(Var, "Password") == 0) {
1682 /* Password of IRC operator */
1683 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1684 if (len >= sizeof(op->pwd))
1685 Config_Error_TooLong(Line, Var);
1688 if (strcasecmp(Var, "Mask") == 0) {
1690 return; /* Hostname already configured */
1691 op->mask = strdup_warn( Arg );
1695 Config_Error_Section(Line, Var, "Operator");
1699 * Handle variable in [Server] configuration section.
1701 * @param Line Line numer in configuration file.
1702 * @param Var Variable name.
1703 * @param Arg Variable argument.
1706 Handle_SERVER( int Line, char *Var, char *Arg )
1712 assert( Var != NULL );
1713 assert( Arg != NULL );
1715 /* Ignore server block if no space is left in server configuration structure */
1716 if( New_Server_Idx <= NONE ) return;
1718 if( strcasecmp( Var, "Host" ) == 0 ) {
1719 /* Hostname of the server */
1720 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1721 if (len >= sizeof( New_Server.host ))
1722 Config_Error_TooLong ( Line, Var );
1725 if( strcasecmp( Var, "Name" ) == 0 ) {
1726 /* Name of the server ("Nick"/"ID") */
1727 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1728 if (len >= sizeof( New_Server.name ))
1729 Config_Error_TooLong( Line, Var );
1732 if (strcasecmp(Var, "Bind") == 0) {
1733 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1736 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1737 NGIRCd_ConfFile, Line, Arg);
1740 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1741 /* Password of this server which is sent to the peer */
1743 Config_Error(LOG_ERR,
1744 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1745 NGIRCd_ConfFile, Line);
1747 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1748 if (len >= sizeof( New_Server.pwd_in ))
1749 Config_Error_TooLong( Line, Var );
1752 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1753 /* Passwort of the peer which must be received */
1754 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1755 if (len >= sizeof( New_Server.pwd_out ))
1756 Config_Error_TooLong( Line, Var );
1759 if( strcasecmp( Var, "Port" ) == 0 ) {
1760 /* Port to which this server should connect */
1762 if (port >= 0 && port < 0xFFFF)
1763 New_Server.port = (UINT16)port;
1765 Config_Error(LOG_ERR,
1766 "%s, line %d (section \"Server\"): Illegal port number %ld!",
1767 NGIRCd_ConfFile, Line, port );
1771 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1772 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1776 if( strcasecmp( Var, "Group" ) == 0 ) {
1778 New_Server.group = atoi( Arg );
1779 if (!New_Server.group && strcmp(Arg, "0"))
1780 Config_Error_NaN(Line, Var);
1783 if( strcasecmp( Var, "Passive" ) == 0 ) {
1784 if (Check_ArgIsTrue(Arg))
1785 New_Server.flags |= CONF_SFLAG_DISABLED;
1788 if (strcasecmp(Var, "ServiceMask") == 0) {
1789 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1790 sizeof(New_Server.svs_mask));
1791 if (len >= sizeof(New_Server.svs_mask))
1792 Config_Error_TooLong(Line, Var);
1796 Config_Error_Section(Line, Var, "Server");
1800 * Copy channel name into channel structure.
1802 * If the channel name is not valid because of a missing prefix ('#', '&'),
1803 * a default prefix of '#' will be added.
1805 * @param new_chan New already allocated channel structure.
1806 * @param name Name of the new channel.
1807 * @returns true on success, false otherwise.
1810 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1812 size_t size = sizeof(new_chan->name);
1813 char *dest = new_chan->name;
1815 if (!Channel_IsValidName(name)) {
1817 * maybe user forgot to add a '#'.
1818 * This is only here for user convenience.
1824 return size > strlcpy(dest, name, size);
1828 * Handle variable in [Channel] configuration section.
1830 * @param Line Line numer in configuration file.
1831 * @param Var Variable name.
1832 * @param Arg Variable argument.
1835 Handle_CHANNEL(int Line, char *Var, char *Arg)
1838 struct Conf_Channel *chan;
1841 assert( Var != NULL );
1842 assert( Arg != NULL );
1844 chan = array_get(&Conf_Channels, sizeof(*chan),
1845 array_length(&Conf_Channels, sizeof(*chan)) - 1);
1849 if (strcasecmp(Var, "Name") == 0) {
1850 if (!Handle_Channelname(chan, Arg))
1851 Config_Error_TooLong(Line, Var);
1854 if (strcasecmp(Var, "Modes") == 0) {
1856 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1857 if (len >= sizeof(chan->modes))
1858 Config_Error_TooLong( Line, Var );
1861 if( strcasecmp( Var, "Topic" ) == 0 ) {
1863 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1864 if (len >= sizeof(chan->topic))
1865 Config_Error_TooLong( Line, Var );
1868 if( strcasecmp( Var, "Key" ) == 0 ) {
1869 /* Initial Channel Key (mode k) */
1870 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1871 if (len >= sizeof(chan->key))
1872 Config_Error_TooLong(Line, Var);
1875 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1876 /* maximum user limit, mode l */
1877 chan->maxusers = (unsigned long) atol(Arg);
1878 if (!chan->maxusers && strcmp(Arg, "0"))
1879 Config_Error_NaN(Line, Var);
1882 if (strcasecmp(Var, "KeyFile") == 0) {
1884 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1885 if (len >= sizeof(chan->keyfile))
1886 Config_Error_TooLong(Line, Var);
1890 Config_Error_Section(Line, Var, "Channel");
1894 * Validate server configuration.
1896 * Please note that this function uses exit(1) on fatal errors and therefore
1897 * can result in ngIRCd terminating!
1899 * @param Configtest true if the daemon has been called with "--configtest".
1900 * @param Rehash true if re-reading configuration on runtime.
1901 * @returns true if configuration is valid.
1904 Validate_Config(bool Configtest, bool Rehash)
1906 /* Validate configuration settings. */
1909 int i, servers, servers_once;
1911 bool config_valid = true;
1914 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1915 ptr = Conf_ServerName;
1917 if (*ptr >= 'a' && *ptr <= 'z') continue;
1918 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1919 if (*ptr >= '0' && *ptr <= '9') continue;
1920 if (ptr > Conf_ServerName) {
1921 if (*ptr == '.' || *ptr == '-')
1924 Conf_ServerName[0] = '\0';
1928 if (!Conf_ServerName[0]) {
1929 /* No server name configured! */
1930 config_valid = false;
1931 Config_Error(LOG_ALERT,
1932 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1934 if (!Configtest && !Rehash) {
1935 Config_Error(LOG_ALERT,
1936 "%s exiting due to fatal errors!",
1942 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1943 /* No dot in server name! */
1944 config_valid = false;
1945 Config_Error(LOG_ALERT,
1946 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1949 Config_Error(LOG_ALERT,
1950 "%s exiting due to fatal errors!",
1957 if (!Conf_ServerAdminMail[0]) {
1958 /* No administrative contact configured! */
1959 config_valid = false;
1960 Config_Error(LOG_ALERT,
1961 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1964 Config_Error(LOG_ALERT,
1965 "%s exiting due to fatal errors!",
1972 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1973 && !Conf_ServerAdminMail[0]) {
1974 /* No administrative information configured! */
1975 Config_Error(LOG_WARNING,
1976 "No administrative information configured but required by RFC!");
1980 if (Conf_ServerPwd[0])
1981 Config_Error(LOG_ERR,
1982 "This server uses PAM, \"Password\" in [Global] section will be ignored!");
1986 servers = servers_once = 0;
1987 for (i = 0; i < MAX_SERVERS; i++) {
1988 if (Conf_Server[i].name[0]) {
1990 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1995 "Configuration: Operators=%ld, Servers=%d[%d], Channels=%ld",
1996 array_length(&Conf_Opers, sizeof(struct Conf_Oper)),
1997 servers, servers_once,
1998 array_length(&Conf_Channels, sizeof(struct Conf_Channel)));
2001 return config_valid;
2005 * Output "line too long" warning.
2007 * @param Line Line number in configuration file.
2008 * @param Item Affected variable name.
2011 Config_Error_TooLong ( const int Line, const char *Item )
2013 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
2017 * Output "unknown variable" warning.
2019 * @param Line Line number in configuration file.
2020 * @param Item Affected variable name.
2021 * @param Section Section name.
2024 Config_Error_Section(const int Line, const char *Item, const char *Section)
2026 Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
2027 NGIRCd_ConfFile, Line, Section, Item);
2031 * Output "not a number" warning.
2033 * @param Line Line number in configuration file.
2034 * @param Item Affected variable name.
2037 Config_Error_NaN( const int Line, const char *Item )
2039 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
2040 NGIRCd_ConfFile, Line, Item );
2044 * Output configuration error to console and/or logfile.
2046 * On runtime, the normal log functions of the daemon are used. But when
2047 * testing the configuration ("--configtest"), all messages go directly
2050 * @param Level Severity level of the message.
2051 * @param Format Format string; see printf() function.
2054 static void Config_Error( const int Level, const char *Format, ... )
2056 static void Config_Error( Level, Format, va_alist )
2062 char msg[MAX_LOG_MSG_LEN];
2065 assert( Format != NULL );
2068 va_start( ap, Format );
2072 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
2076 if (Level <= LOG_WARNING)
2077 printf(" - %s\n", msg);
2081 Log(Level, "%s", msg);
2087 * Dump internal state of the "configuration module".
2090 Conf_DebugDump(void)
2094 Log(LOG_DEBUG, "Configured servers:");
2095 for (i = 0; i < MAX_SERVERS; i++) {
2096 if (! Conf_Server[i].name[0])
2099 " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
2100 Conf_Server[i].name, Conf_Server[i].host,
2101 Conf_Server[i].port, Conf_Server[i].lasttry,
2102 Conf_Server[i].group, Conf_Server[i].flags,
2103 Conf_Server[i].conn_id);
2110 * Initialize server configuration structur to default values.
2112 * @param Server Pointer to server structure to initialize.
2115 Init_Server_Struct( CONF_SERVER *Server )
2117 assert( Server != NULL );
2119 memset( Server, 0, sizeof (CONF_SERVER) );
2121 Server->group = NONE;
2122 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
2124 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
2126 Proc_InitStruct(&Server->res_stat);
2127 Server->conn_id = NONE;
2128 memset(&Server->bind_addr, 0, sizeof(Server->bind_addr));