2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2010 Alexander Barton (alex@barton.de)
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.
11 * Configuration management (reading, parsing & validation)
32 #include <sys/types.h>
52 static bool Use_Log = true;
53 static CONF_SERVER New_Server;
54 static int New_Server_Idx;
56 static size_t Conf_Oper_Count;
57 static size_t Conf_Channel_Count;
58 static char Conf_MotdFile[FNAME_LEN];
60 static void Set_Defaults PARAMS(( bool InitServers ));
61 static bool Read_Config PARAMS(( bool ngircd_starting ));
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_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
66 static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
67 static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
69 static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
71 static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
72 static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
74 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
77 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
79 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
83 struct SSLOptions Conf_SSLOptions;
88 free(Conf_SSLOptions.KeyFile);
89 Conf_SSLOptions.KeyFile = NULL;
91 free(Conf_SSLOptions.CertFile);
92 Conf_SSLOptions.CertFile = NULL;
94 free(Conf_SSLOptions.DHFile);
95 Conf_SSLOptions.DHFile = NULL;
96 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
100 ssl_print_configvar(const char *name, const char *file)
105 printf(" %s =\n", name);
109 fp = fopen(file, "r");
113 fprintf(stderr, "ERROR: %s \"%s\": %s\n",
114 name, file, strerror(errno));
116 printf(" %s = %s\n", name, file);
125 ret = ssl_print_configvar("SSLKeyFile", Conf_SSLOptions.KeyFile);
127 if (!ssl_print_configvar("SSLCertFile", Conf_SSLOptions.CertFile))
130 if (!ssl_print_configvar("SSLDHFile", Conf_SSLOptions.DHFile))
133 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
134 puts(" SSLKeyFilePassword = <secret>");
136 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
143 strdup_warn(const char *str)
145 char *ptr = strdup(str);
147 Config_Error(LOG_ERR, "Could not allocate mem for string: %s", str);
157 len = array_length(a, sizeof(UINT16));
159 ports = (UINT16*) array_start(a);
160 printf("%u", (unsigned int) *ports);
163 printf(", %u", (unsigned int) *ports);
171 ports_parse(array *a, int Line, char *Arg)
179 /* Ports on that the server should listen. More port numbers
180 * must be separated by "," */
181 ptr = strtok( Arg, "," );
185 if (port > 0 && port < 0xFFFF) {
186 port16 = (UINT16) port;
187 if (!array_catb(a, (char*)&port16, sizeof port16))
188 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
189 NGIRCd_ConfFile, Line, port, strerror(errno));
191 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
192 NGIRCd_ConfFile, Line, port );
195 ptr = strtok( NULL, "," );
204 Validate_Config(false, false);
211 if (!Read_Config(false))
213 Validate_Config(false, true);
215 /* Update CLIENT structure of local server */
216 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
218 } /* Config_Rehash */
222 yesno_to_str(int boolean_value)
233 struct Conf_Oper *op;
236 len = array_length(&Conf_Opers, sizeof(*op));
237 op = array_start(&Conf_Opers);
242 array_free(&Conf_Opers);
248 struct Conf_Oper *op;
251 len = array_length(&Conf_Opers, sizeof(*op));
252 op = array_start(&Conf_Opers);
257 printf(" Name = %s\n", op->name);
258 printf(" Password = %s\n", op->pwd);
259 printf(" Mask = %s\n\n", op->mask ? op->mask : "");
268 /* Read configuration, validate and output it. */
274 size_t predef_channel_count;
275 struct Conf_Channel *predef_chan;
279 if (! Read_Config(true))
282 config_valid = Validate_Config(true, false);
284 /* If stdin and stdout ("you can read our nice message and we can
285 * read in your keypress") are valid tty's, wait for a key: */
286 if( isatty( fileno( stdin )) && isatty( fileno( stdout ))) {
287 puts( "OK, press enter to see a dump of your service configuration ..." );
290 puts( "Ok, dump of your server configuration follows:\n" );
294 printf(" Name = %s\n", Conf_ServerName);
295 printf(" Info = %s\n", Conf_ServerInfo);
297 printf(" Password = %s\n", Conf_ServerPwd);
299 printf(" WebircPassword = %s\n", Conf_WebircPwd);
300 printf(" AdminInfo1 = %s\n", Conf_ServerAdmin1);
301 printf(" AdminInfo2 = %s\n", Conf_ServerAdmin2);
302 printf(" AdminEMail = %s\n", Conf_ServerAdminMail);
303 printf(" MotdFile = %s\n", Conf_MotdFile);
304 printf(" MotdPhrase = %.32s\n", array_bytes(&Conf_Motd) ? (const char*) array_start(&Conf_Motd) : "");
305 printf(" ChrootDir = %s\n", Conf_Chroot);
306 printf(" PidFile = %s\n", Conf_PidFile);
307 printf(" Listen = %s\n", Conf_ListenAddress);
308 fputs(" Ports = ", stdout);
309 ports_puts(&Conf_ListenPorts);
311 fputs(" SSLPorts = ", stdout);
312 ports_puts(&Conf_SSLOptions.ListenPorts);
314 config_valid = false;
317 pwd = getpwuid(Conf_UID);
319 printf(" ServerUID = %s\n", pwd->pw_name);
321 printf(" ServerUID = %ld\n", (long)Conf_UID);
322 grp = getgrgid(Conf_GID);
324 printf(" ServerGID = %s\n", grp->gr_name);
326 printf(" ServerGID = %ld\n", (long)Conf_GID);
327 printf(" PingTimeout = %d\n", Conf_PingTimeout);
328 printf(" PongTimeout = %d\n", Conf_PongTimeout);
329 printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
330 printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
331 printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
332 printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
333 printf(" PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
334 printf(" NoDNS = %s\n", yesno_to_str(Conf_NoDNS));
335 printf(" NoIdent = %s\n", yesno_to_str(Conf_NoIdent));
336 printf(" NoPAM = %s\n", yesno_to_str(Conf_NoPAM));
339 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
340 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
342 printf(" MaxConnections = %ld\n", Conf_MaxConnections);
343 printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
344 printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
345 printf(" MaxNickLength = %u\n\n", Conf_MaxNickLength - 1);
349 for( i = 0; i < MAX_SERVERS; i++ ) {
350 if( ! Conf_Server[i].name[0] ) continue;
352 /* Valid "Server" section */
354 printf( " Name = %s\n", Conf_Server[i].name );
355 printf( " Host = %s\n", Conf_Server[i].host );
356 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
358 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
360 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
361 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
362 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
363 printf( " Group = %d\n", Conf_Server[i].group );
364 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
367 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
368 predef_chan = array_start(&Conf_Channels);
370 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
371 if (!predef_chan->name[0])
374 /* Valid "Channel" section */
376 printf(" Name = %s\n", predef_chan->name);
377 printf(" Modes = %s\n", predef_chan->modes);
378 printf(" Key = %s\n", predef_chan->key);
379 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
380 printf(" Topic = %s\n", predef_chan->topic);
381 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
384 return (config_valid ? 0 : 1);
389 Conf_UnsetServer( CONN_ID Idx )
391 /* Set next time for next connection attempt, if this is a server
392 * link that is (still) configured here. If the server is set as
393 * "once", delete it from our configuration.
394 * Non-Server-Connections will be silently ignored. */
399 /* Check all our configured servers */
400 for( i = 0; i < MAX_SERVERS; i++ ) {
401 if( Conf_Server[i].conn_id != Idx ) continue;
403 /* Gotcha! Mark server configuration as "unused": */
404 Conf_Server[i].conn_id = NONE;
406 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
407 /* Delete configuration here */
408 Init_Server_Struct( &Conf_Server[i] );
410 /* Set time for next connect attempt */
412 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
413 /* The connection has been "long", so we don't
414 * require the next attempt to be delayed. */
415 Conf_Server[i].lasttry =
416 t - Conf_ConnectRetry + RECONNECT_DELAY;
418 Conf_Server[i].lasttry = t;
421 } /* Conf_UnsetServer */
425 Conf_SetServer( int ConfServer, CONN_ID Idx )
427 /* Set connection for specified configured server */
429 assert( ConfServer > NONE );
430 assert( Idx > NONE );
432 Conf_Server[ConfServer].conn_id = Idx;
433 } /* Conf_SetServer */
437 Conf_GetServer( CONN_ID Idx )
439 /* Get index of server in configuration structure */
443 assert( Idx > NONE );
445 for( i = 0; i < MAX_SERVERS; i++ ) {
446 if( Conf_Server[i].conn_id == Idx ) return i;
449 } /* Conf_GetServer */
453 Conf_EnableServer( const char *Name, UINT16 Port )
455 /* Enable specified server and adjust port */
459 assert( Name != NULL );
461 for( i = 0; i < MAX_SERVERS; i++ ) {
462 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
463 /* Gotcha! Set port and enable server: */
464 Conf_Server[i].port = Port;
465 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
466 return (Conf_Server[i].port && Conf_Server[i].host[0]);
470 } /* Conf_EnableServer */
474 Conf_EnablePassiveServer(const char *Name)
476 /* Enable specified server */
479 assert( Name != NULL );
480 for (i = 0; i < MAX_SERVERS; i++) {
481 if ((strcasecmp( Conf_Server[i].name, Name ) == 0) && (Conf_Server[i].port > 0)) {
482 /* BINGO! Enable server */
483 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
488 } /* Conf_EnablePassiveServer */
492 Conf_DisableServer( const char *Name )
494 /* Enable specified server and adjust port */
498 assert( Name != NULL );
500 for( i = 0; i < MAX_SERVERS; i++ ) {
501 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
502 /* Gotcha! Disable and disconnect server: */
503 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
504 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
509 } /* Conf_DisableServer */
513 Conf_AddServer( const char *Name, UINT16 Port, const char *Host, const char *MyPwd, const char *PeerPwd )
515 /* Add new server to configuration */
519 assert( Name != NULL );
520 assert( Host != NULL );
521 assert( MyPwd != NULL );
522 assert( PeerPwd != NULL );
524 /* Search unused item in server configuration structure */
525 for( i = 0; i < MAX_SERVERS; i++ ) {
526 /* Is this item used? */
527 if( ! Conf_Server[i].name[0] ) break;
529 if( i >= MAX_SERVERS ) return false;
531 Init_Server_Struct( &Conf_Server[i] );
532 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
533 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
534 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
535 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
536 Conf_Server[i].port = Port;
537 Conf_Server[i].flags = CONF_SFLAG_ONCE;
540 } /* Conf_AddServer */
544 * Check if the given nick name is an service
547 Conf_IsService(int ConfServer, const char *Nick)
549 return MatchCaseInsensitive(Conf_Server[ConfServer].svs_mask, Nick);
550 } /* Conf_IsService */
554 * Initialize configuration settings with their default values.
557 Set_Defaults(bool InitServers)
561 strcpy(Conf_ServerName, "");
562 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
563 PACKAGE_NAME, PACKAGE_VERSION);
564 strcpy(Conf_ServerPwd, "");
566 strcpy(Conf_ServerAdmin1, "");
567 strcpy(Conf_ServerAdmin2, "");
568 strcpy(Conf_ServerAdminMail, "");
570 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
571 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
573 Conf_UID = Conf_GID = 0;
574 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
575 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
577 free(Conf_ListenAddress);
578 Conf_ListenAddress = NULL;
580 Conf_PingTimeout = 120;
581 Conf_PongTimeout = 20;
582 Conf_ConnectRetry = 60;
584 Conf_NoIdent = false;
588 Conf_Channel_Count = 0;
590 Conf_OperCanMode = false;
591 Conf_OperServerMode = false;
592 Conf_AllowRemoteOper = false;
593 Conf_PredefChannelsOnly = false;
595 Conf_ConnectIPv4 = true;
596 Conf_ConnectIPv6 = true;
598 Conf_MaxConnections = 0;
599 Conf_MaxConnectionsIP = 5;
601 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
603 /* Initialize server configuration structures */
605 for (i = 0; i < MAX_SERVERS;
606 Init_Server_Struct(&Conf_Server[i++]));
614 size_t cnt = array_bytes(&Conf_ListenPorts);
616 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
622 Read_Motd(const char *filename)
627 if (*filename == '\0')
630 fp = fopen(filename, "r");
632 Log(LOG_WARNING, "Can't read MOTD file \"%s\": %s",
633 filename, strerror(errno));
637 array_free(&Conf_Motd);
639 while (fgets(line, (int)sizeof line, fp)) {
640 ngt_TrimLastChr( line, '\n');
642 /* add text including \0 */
643 if (!array_catb(&Conf_Motd, line, strlen(line) + 1)) {
644 Log(LOG_WARNING, "Cannot add MOTD text: %s", strerror(errno));
652 Read_Config( bool ngircd_starting )
654 /* Read configuration file. */
656 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
657 const UINT16 defaultport = 6667;
661 /* Open configuration file */
662 fd = fopen( NGIRCd_ConfFile, "r" );
664 /* No configuration file found! */
665 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
666 NGIRCd_ConfFile, strerror( errno ));
667 if (!ngircd_starting)
669 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
674 Set_Defaults( ngircd_starting );
676 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
678 /* Clean up server configuration structure: mark all already
679 * configured servers as "once" so that they are deleted
680 * after the next disconnect and delete all unused servers.
681 * And delete all servers which are "duplicates" of servers
682 * that are already marked as "once" (such servers have been
683 * created by the last rehash but are now useless). */
684 for( i = 0; i < MAX_SERVERS; i++ ) {
685 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
687 /* This structure is in use ... */
688 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
689 /* Check for duplicates */
690 for( n = 0; n < MAX_SERVERS; n++ ) {
691 if( n == i ) continue;
693 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
694 Init_Server_Struct( &Conf_Server[n] );
696 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
702 /* Mark server as "once" */
703 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
704 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
709 /* Initialize variables */
711 strcpy( section, "" );
712 Init_Server_Struct( &New_Server );
713 New_Server_Idx = NONE;
717 /* Read configuration file */
719 if( ! fgets( str, LINE_LEN, fd )) break;
723 /* Skip comments and empty lines */
724 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
726 /* Is this the beginning of a new section? */
727 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
728 strlcpy( section, str, sizeof( section ));
729 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
732 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
733 /* Check if there is already a server to add */
734 if( New_Server.name[0] ) {
735 /* Copy data to "real" server structure */
736 assert( New_Server_Idx > NONE );
737 Conf_Server[New_Server_Idx] = New_Server;
740 /* Re-init structure for new server */
741 Init_Server_Struct( &New_Server );
743 /* Search unused item in server configuration structure */
744 for( i = 0; i < MAX_SERVERS; i++ ) {
745 /* Is this item used? */
746 if( ! Conf_Server[i].name[0] ) break;
748 if( i >= MAX_SERVERS ) {
749 /* Oops, no free item found! */
750 Config_Error( LOG_ERR, "Too many servers configured." );
751 New_Server_Idx = NONE;
753 else New_Server_Idx = i;
756 if (strcasecmp(section, "[CHANNEL]") == 0) {
757 Conf_Channel_Count++;
760 if (strcasecmp(section, "[OPERATOR]") == 0) {
765 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
768 if( section[0] == 0x1 ) continue;
770 /* Split line into variable name and parameters */
771 ptr = strchr( str, '=' );
773 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
777 var = str; ngt_TrimStr( var );
778 arg = ptr + 1; ngt_TrimStr( arg );
780 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
781 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
782 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
783 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
784 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
787 /* Close configuration file */
790 /* Check if there is still a server to add */
791 if( New_Server.name[0] ) {
792 /* Copy data to "real" server structure */
793 assert( New_Server_Idx > NONE );
794 Conf_Server[New_Server_Idx] = New_Server;
797 /* not a single listening port? Add default. */
798 if (no_listenports() &&
799 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
801 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
802 (unsigned int) defaultport, strerror(errno));
807 if (!Conf_ListenAddress)
808 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
810 if (!Conf_ListenAddress) {
811 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
815 /* No MOTD phrase configured? (re)try motd file. */
816 if (array_bytes(&Conf_Motd) == 0)
817 Read_Motd(Conf_MotdFile);
823 Check_ArgIsTrue( const char *Arg )
825 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
826 if( strcasecmp( Arg, "true" ) == 0 ) return true;
827 if( atoi( Arg ) != 0 ) return true;
830 } /* Check_ArgIsTrue */
833 static unsigned int Handle_MaxNickLength(int Line, const char *Arg)
837 new = (unsigned) atoi(Arg) + 1;
838 if (new > CLIENT_NICK_LEN) {
839 Config_Error(LOG_WARNING,
840 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
841 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
842 return CLIENT_NICK_LEN;
845 Config_Error(LOG_WARNING,
846 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
847 NGIRCd_ConfFile, Line);
851 } /* Handle_MaxNickLength */
856 Handle_GLOBAL( int Line, char *Var, char *Arg )
863 assert( Var != NULL );
864 assert( Arg != NULL );
866 if( strcasecmp( Var, "Name" ) == 0 ) {
868 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
869 if (len >= sizeof( Conf_ServerName ))
870 Config_Error_TooLong( Line, Var );
873 if( strcasecmp( Var, "Info" ) == 0 ) {
874 /* Info text of server */
875 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
876 if (len >= sizeof( Conf_ServerInfo ))
877 Config_Error_TooLong ( Line, Var );
880 if( strcasecmp( Var, "Password" ) == 0 ) {
881 /* Global server password */
882 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
883 if (len >= sizeof( Conf_ServerPwd ))
884 Config_Error_TooLong( Line, Var );
887 if (strcasecmp(Var, "WebircPassword") == 0) {
888 /* Password required for WEBIRC command */
889 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
890 if (len >= sizeof(Conf_WebircPwd))
891 Config_Error_TooLong(Line, Var);
894 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
895 /* Administrative info #1 */
896 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
897 if (len >= sizeof( Conf_ServerAdmin1 ))
898 Config_Error_TooLong ( Line, Var );
901 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
902 /* Administrative info #2 */
903 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
904 if (len >= sizeof( Conf_ServerAdmin2 ))
905 Config_Error_TooLong ( Line, Var );
908 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
909 /* Administrative email contact */
910 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
911 if (len >= sizeof( Conf_ServerAdminMail ))
912 Config_Error_TooLong( Line, Var );
916 if( strcasecmp( Var, "Ports" ) == 0 ) {
917 ports_parse(&Conf_ListenPorts, Line, Arg);
920 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
921 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
922 if (len >= sizeof( Conf_MotdFile ))
923 Config_Error_TooLong( Line, Var );
927 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
928 /* "Message of the day" phrase (instead of file) */
932 if (len >= LINE_LEN) {
933 Config_Error_TooLong( Line, Var );
936 if (!array_copyb(&Conf_Motd, Arg, len + 1))
937 Config_Error(LOG_WARNING, "%s, line %d: Could not append MotdPhrase: %s",
938 NGIRCd_ConfFile, Line, strerror(errno));
941 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
942 /* directory for chroot() */
943 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
944 if (len >= sizeof( Conf_Chroot ))
945 Config_Error_TooLong( Line, Var );
948 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
949 /* name of pidfile */
950 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
951 if (len >= sizeof( Conf_PidFile ))
952 Config_Error_TooLong( Line, Var );
955 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
956 /* UID the daemon should switch to */
957 pwd = getpwnam( Arg );
958 if( pwd ) Conf_UID = pwd->pw_uid;
961 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
964 Conf_UID = (unsigned int)atoi( Arg );
968 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
969 /* GID the daemon should use */
970 grp = getgrnam( Arg );
971 if( grp ) Conf_GID = grp->gr_gid;
974 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
977 Conf_GID = (unsigned int)atoi( Arg );
981 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
983 Conf_PingTimeout = atoi( Arg );
984 if( Conf_PingTimeout < 5 ) {
985 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
986 NGIRCd_ConfFile, Line );
987 Conf_PingTimeout = 5;
991 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
993 Conf_PongTimeout = atoi( Arg );
994 if( Conf_PongTimeout < 5 ) {
995 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
996 NGIRCd_ConfFile, Line );
997 Conf_PongTimeout = 5;
1001 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
1002 /* Seconds between connection attempts to other servers */
1003 Conf_ConnectRetry = atoi( Arg );
1004 if( Conf_ConnectRetry < 5 ) {
1005 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
1006 NGIRCd_ConfFile, Line );
1007 Conf_ConnectRetry = 5;
1011 if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
1012 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
1013 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
1016 if( strcasecmp( Var, "NoDNS" ) == 0 ) {
1017 /* don't do reverse dns lookups when clients connect? */
1018 Conf_NoDNS = Check_ArgIsTrue( Arg );
1021 if (strcasecmp(Var, "NoIdent") == 0) {
1022 /* don't do IDENT lookups when clients connect? */
1023 Conf_NoIdent = Check_ArgIsTrue(Arg);
1025 if (!Conf_NoIdent) {
1026 /* user has enabled ident lookups explicitly, but ... */
1027 Config_Error(LOG_WARNING,
1028 "%s: line %d: NoIdent=False, but ngircd was built without IDENT support",
1029 NGIRCd_ConfFile, Line);
1034 if(strcasecmp(Var, "NoPAM") == 0) {
1035 /* don't use PAM library to authenticate users */
1036 Conf_NoPAM = Check_ArgIsTrue(Arg);
1040 /* the default setting for all the WANT_IPV6 special options is 'true' */
1041 if( strcasecmp( Var, "ConnectIPv6" ) == 0 ) {
1042 /* connect to other hosts using ipv6, if they have an AAAA record? */
1043 Conf_ConnectIPv6 = Check_ArgIsTrue( Arg );
1046 if( strcasecmp( Var, "ConnectIPv4" ) == 0 ) {
1047 /* connect to other hosts using ipv4.
1048 * again, this can be used for ipv6-only setups */
1049 Conf_ConnectIPv4 = Check_ArgIsTrue( Arg );
1053 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
1054 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
1055 Conf_OperCanMode = Check_ArgIsTrue( Arg );
1058 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
1059 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
1060 Conf_OperServerMode = Check_ArgIsTrue( Arg );
1063 if(strcasecmp(Var, "AllowRemoteOper") == 0) {
1064 /* Are remote IRC operators allowed to control this server? */
1065 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1068 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
1069 /* Maximum number of connections. 0 -> "no limit". */
1071 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
1074 Conf_MaxConnections = atol( Arg );
1077 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
1078 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
1080 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
1083 Conf_MaxConnectionsIP = atoi( Arg );
1086 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
1087 /* Maximum number of channels a user can join. 0 -> "no limit". */
1089 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
1092 Conf_MaxJoins = atoi( Arg );
1095 if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
1096 /* Maximum length of a nick name; must be same on all servers
1097 * within the IRC network! */
1098 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1102 if( strcasecmp( Var, "Listen" ) == 0 ) {
1103 /* IP-Address to bind sockets */
1104 if (Conf_ListenAddress) {
1105 Config_Error(LOG_ERR, "Multiple Listen= options, ignoring: %s", Arg);
1108 Conf_ListenAddress = strdup_warn(Arg);
1110 * if allocation fails, we're in trouble:
1111 * we cannot ignore the error -- otherwise ngircd
1112 * would listen on all interfaces.
1114 if (!Conf_ListenAddress) {
1115 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
1122 if( strcasecmp( Var, "SSLPorts" ) == 0 ) {
1123 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1127 if( strcasecmp( Var, "SSLKeyFile" ) == 0 ) {
1128 assert(Conf_SSLOptions.KeyFile == NULL );
1129 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1132 if( strcasecmp( Var, "SSLCertFile" ) == 0 ) {
1133 assert(Conf_SSLOptions.CertFile == NULL );
1134 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1138 if( strcasecmp( Var, "SSLKeyFilePassword" ) == 0 ) {
1139 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1140 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1141 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Could not copy %s: %s!",
1142 NGIRCd_ConfFile, Line, Var, strerror(errno));
1145 if( strcasecmp( Var, "SSLDHFile" ) == 0 ) {
1146 assert(Conf_SSLOptions.DHFile == NULL);
1147 Conf_SSLOptions.DHFile = strdup_warn( Arg );
1151 Config_Error(LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
1152 NGIRCd_ConfFile, Line, Var);
1153 } /* Handle_GLOBAL */
1157 Handle_OPERATOR( int Line, char *Var, char *Arg )
1160 struct Conf_Oper *op;
1163 assert( Var != NULL );
1164 assert( Arg != NULL );
1165 assert( Conf_Oper_Count > 0 );
1167 op = array_alloc(&Conf_Opers, sizeof(*op), Conf_Oper_Count - 1);
1169 Config_Error(LOG_ERR, "Could not allocate memory for operator (%d:%s = %s)", Line, Var, Arg);
1173 if (strcasecmp(Var, "Name") == 0) {
1174 /* Name of IRC operator */
1175 len = strlcpy(op->name, Arg, sizeof(op->name));
1176 if (len >= sizeof(op->name))
1177 Config_Error_TooLong(Line, Var);
1180 if (strcasecmp(Var, "Password") == 0) {
1181 /* Password of IRC operator */
1182 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1183 if (len >= sizeof(op->pwd))
1184 Config_Error_TooLong(Line, Var);
1187 if (strcasecmp(Var, "Mask") == 0) {
1189 return; /* Hostname already configured */
1190 op->mask = strdup_warn( Arg );
1193 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
1194 NGIRCd_ConfFile, Line, Var );
1195 } /* Handle_OPERATOR */
1199 Handle_SERVER( int Line, char *Var, char *Arg )
1205 assert( Var != NULL );
1206 assert( Arg != NULL );
1208 /* Ignore server block if no space is left in server configuration structure */
1209 if( New_Server_Idx <= NONE ) return;
1211 if( strcasecmp( Var, "Host" ) == 0 ) {
1212 /* Hostname of the server */
1213 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1214 if (len >= sizeof( New_Server.host ))
1215 Config_Error_TooLong ( Line, Var );
1218 if( strcasecmp( Var, "Name" ) == 0 ) {
1219 /* Name of the server ("Nick"/"ID") */
1220 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1221 if (len >= sizeof( New_Server.name ))
1222 Config_Error_TooLong( Line, Var );
1225 if (strcasecmp(Var, "Bind") == 0) {
1226 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1229 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1230 NGIRCd_ConfFile, Line, Arg);
1233 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1234 /* Password of this server which is sent to the peer */
1236 Config_Error(LOG_ERR,
1237 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1238 NGIRCd_ConfFile, Line);
1240 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1241 if (len >= sizeof( New_Server.pwd_in ))
1242 Config_Error_TooLong( Line, Var );
1245 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1246 /* Passwort of the peer which must be received */
1247 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1248 if (len >= sizeof( New_Server.pwd_out ))
1249 Config_Error_TooLong( Line, Var );
1252 if( strcasecmp( Var, "Port" ) == 0 ) {
1253 /* Port to which this server should connect */
1255 if( port > 0 && port < 0xFFFF )
1256 New_Server.port = (UINT16)port;
1258 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
1259 NGIRCd_ConfFile, Line, port );
1263 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1264 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1268 if( strcasecmp( Var, "Group" ) == 0 ) {
1271 if( ! isdigit( (int)*Arg ))
1272 Config_Error_NaN( Line, Var );
1275 New_Server.group = atoi( Arg );
1278 if( strcasecmp( Var, "Passive" ) == 0 ) {
1279 if (Check_ArgIsTrue(Arg))
1280 New_Server.flags |= CONF_SFLAG_DISABLED;
1283 if (strcasecmp(Var, "ServiceMask") == 0) {
1284 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1285 sizeof(New_Server.svs_mask));
1286 if (len >= sizeof(New_Server.svs_mask))
1287 Config_Error_TooLong(Line, Var);
1291 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
1292 NGIRCd_ConfFile, Line, Var );
1293 } /* Handle_SERVER */
1297 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1299 size_t size = sizeof(new_chan->name);
1300 char *dest = new_chan->name;
1302 if (!Channel_IsValidName(name)) {
1304 * maybe user forgot to add a '#'.
1305 * This is only here for user convenience.
1311 return size > strlcpy(dest, name, size);
1316 Handle_CHANNEL(int Line, char *Var, char *Arg)
1320 struct Conf_Channel *chan;
1323 assert( Var != NULL );
1324 assert( Arg != NULL );
1325 assert(Conf_Channel_Count > 0);
1327 chancount = Conf_Channel_Count - 1;
1329 chan = array_alloc(&Conf_Channels, sizeof(*chan), chancount);
1331 Config_Error(LOG_ERR, "Could not allocate memory for predefined channel (%d:%s = %s)", Line, Var, Arg);
1334 if (strcasecmp(Var, "Name") == 0) {
1335 if (!Handle_Channelname(chan, Arg))
1336 Config_Error_TooLong(Line, Var);
1339 if (strcasecmp(Var, "Modes") == 0) {
1341 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1342 if (len >= sizeof(chan->modes))
1343 Config_Error_TooLong( Line, Var );
1346 if( strcasecmp( Var, "Topic" ) == 0 ) {
1348 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1349 if (len >= sizeof(chan->topic))
1350 Config_Error_TooLong( Line, Var );
1353 if( strcasecmp( Var, "Key" ) == 0 ) {
1354 /* Initial Channel Key (mode k) */
1355 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1356 if (len >= sizeof(chan->key))
1357 Config_Error_TooLong(Line, Var);
1360 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1361 /* maximum user limit, mode l */
1362 chan->maxusers = (unsigned long) atol(Arg);
1363 if (chan->maxusers == 0)
1364 Config_Error_NaN(Line, Var);
1367 if (strcasecmp(Var, "KeyFile") == 0) {
1369 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1370 if (len >= sizeof(chan->keyfile))
1371 Config_Error_TooLong(Line, Var);
1375 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1376 NGIRCd_ConfFile, Line, Var );
1377 } /* Handle_CHANNEL */
1381 Validate_Config(bool Configtest, bool Rehash)
1383 /* Validate configuration settings. */
1386 int i, servers, servers_once;
1388 bool config_valid = true;
1391 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1392 ptr = Conf_ServerName;
1394 if (*ptr >= 'a' && *ptr <= 'z') continue;
1395 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1396 if (*ptr >= '0' && *ptr <= '9') continue;
1397 if (ptr > Conf_ServerName) {
1398 if (*ptr == '.' || *ptr == '-')
1401 Conf_ServerName[0] = '\0';
1405 if (!Conf_ServerName[0]) {
1406 /* No server name configured! */
1407 config_valid = false;
1408 Config_Error(LOG_ALERT,
1409 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1411 if (!Configtest && !Rehash) {
1412 Config_Error(LOG_ALERT,
1413 "%s exiting due to fatal errors!",
1419 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1420 /* No dot in server name! */
1421 config_valid = false;
1422 Config_Error(LOG_ALERT,
1423 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1426 Config_Error(LOG_ALERT,
1427 "%s exiting due to fatal errors!",
1434 if (!Conf_ServerAdminMail[0]) {
1435 /* No administrative contact configured! */
1436 config_valid = false;
1437 Config_Error(LOG_ALERT,
1438 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1441 Config_Error(LOG_ALERT,
1442 "%s exiting due to fatal errors!",
1449 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1450 && !Conf_ServerAdminMail[0]) {
1451 /* No administrative information configured! */
1452 Config_Error(LOG_WARNING,
1453 "No administrative information configured but required by RFC!");
1457 if (Conf_ServerPwd[0])
1458 Config_Error(LOG_ERR,
1459 "This server uses PAM, \"Password\" will be ignored!");
1463 servers = servers_once = 0;
1464 for (i = 0; i < MAX_SERVERS; i++) {
1465 if (Conf_Server[i].name[0]) {
1467 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1472 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1473 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1476 return config_valid;
1477 } /* Validate_Config */
1481 Config_Error_TooLong ( const int Line, const char *Item )
1483 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1488 Config_Error_NaN( const int Line, const char *Item )
1490 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1491 NGIRCd_ConfFile, Line, Item );
1496 static void Config_Error( const int Level, const char *Format, ... )
1498 static void Config_Error( Level, Format, va_alist )
1504 /* Error! Write to console and/or logfile. */
1506 char msg[MAX_LOG_MSG_LEN];
1509 assert( Format != NULL );
1512 va_start( ap, Format );
1516 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1519 /* During "normal operations" the log functions of the daemon should
1520 * be used, but during testing of the configuration file, all messages
1521 * should go directly to the console: */
1522 if (Use_Log) Log( Level, "%s", msg );
1524 } /* Config_Error */
1528 Init_Server_Struct( CONF_SERVER *Server )
1530 /* Initialize server configuration structur to default values */
1532 assert( Server != NULL );
1534 memset( Server, 0, sizeof (CONF_SERVER) );
1536 Server->group = NONE;
1537 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1539 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1541 Proc_InitStruct(&Server->res_stat);
1542 Server->conn_id = NONE;
1543 memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
1544 } /* Init_Server_Struct */