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 void Set_Defaults PARAMS(( bool InitServers ));
59 static bool Read_Config PARAMS(( bool ngircd_starting ));
60 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
62 static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
63 static void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
64 static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
65 static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
67 static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
69 static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
70 static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
72 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
75 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
77 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
81 struct SSLOptions Conf_SSLOptions;
86 free(Conf_SSLOptions.KeyFile);
87 Conf_SSLOptions.KeyFile = NULL;
89 free(Conf_SSLOptions.CertFile);
90 Conf_SSLOptions.CertFile = NULL;
92 free(Conf_SSLOptions.DHFile);
93 Conf_SSLOptions.DHFile = NULL;
94 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
98 ssl_print_configvar(const char *name, const char *file)
103 printf(" %s =\n", name);
107 fp = fopen(file, "r");
111 fprintf(stderr, "ERROR: %s \"%s\": %s\n",
112 name, file, strerror(errno));
114 printf(" %s = %s\n", name, file);
123 ret = ssl_print_configvar("SSLKeyFile", Conf_SSLOptions.KeyFile);
125 if (!ssl_print_configvar("SSLCertFile", Conf_SSLOptions.CertFile))
128 if (!ssl_print_configvar("SSLDHFile", Conf_SSLOptions.DHFile))
131 if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
132 puts(" SSLKeyFilePassword = <secret>");
134 array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
141 strdup_warn(const char *str)
143 char *ptr = strdup(str);
145 Config_Error(LOG_ERR, "Could not allocate mem for string: %s", str);
155 len = array_length(a, sizeof(UINT16));
157 ports = (UINT16*) array_start(a);
158 printf("%u", (unsigned int) *ports);
161 printf(", %u", (unsigned int) *ports);
169 ports_parse(array *a, int Line, char *Arg)
177 /* Ports on that the server should listen. More port numbers
178 * must be separated by "," */
179 ptr = strtok( Arg, "," );
183 if (port > 0 && port < 0xFFFF) {
184 port16 = (UINT16) port;
185 if (!array_catb(a, (char*)&port16, sizeof port16))
186 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
187 NGIRCd_ConfFile, Line, port, strerror(errno));
189 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
190 NGIRCd_ConfFile, Line, port );
193 ptr = strtok( NULL, "," );
202 Validate_Config(false, false);
209 if (!Read_Config(false))
211 Validate_Config(false, true);
213 /* Update CLIENT structure of local server */
214 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
216 } /* Config_Rehash */
220 yesno_to_str(int boolean_value)
231 struct Conf_Oper *op;
234 len = array_length(&Conf_Opers, sizeof(*op));
235 op = array_start(&Conf_Opers);
240 array_free(&Conf_Opers);
246 struct Conf_Oper *op;
249 len = array_length(&Conf_Opers, sizeof(*op));
250 op = array_start(&Conf_Opers);
255 printf(" Name = %s\n", op->name);
256 printf(" Password = %s\n", op->pwd);
257 printf(" Mask = %s\n\n", op->mask ? op->mask : "");
266 /* Read configuration, validate and output it. */
272 size_t predef_channel_count;
273 struct Conf_Channel *predef_chan;
277 if (! Read_Config(true))
280 config_valid = Validate_Config(true, false);
282 /* If stdin and stdout ("you can read our nice message and we can
283 * read in your keypress") are valid tty's, wait for a key: */
284 if( isatty( fileno( stdin )) && isatty( fileno( stdout ))) {
285 puts( "OK, press enter to see a dump of your service configuration ..." );
288 puts( "Ok, dump of your server configuration follows:\n" );
292 printf(" Name = %s\n", Conf_ServerName);
293 printf(" Info = %s\n", Conf_ServerInfo);
295 printf(" Password = %s\n", Conf_ServerPwd);
297 printf(" WebircPassword = %s\n", Conf_WebircPwd);
298 printf(" AdminInfo1 = %s\n", Conf_ServerAdmin1);
299 printf(" AdminInfo2 = %s\n", Conf_ServerAdmin2);
300 printf(" AdminEMail = %s\n", Conf_ServerAdminMail);
301 printf(" MotdFile = %s\n", Conf_MotdFile);
302 printf(" MotdPhrase = %s\n", Conf_MotdPhrase);
303 printf(" ChrootDir = %s\n", Conf_Chroot);
304 printf(" PidFile = %s\n", Conf_PidFile);
305 printf(" Listen = %s\n", Conf_ListenAddress);
306 fputs(" Ports = ", stdout);
307 ports_puts(&Conf_ListenPorts);
309 fputs(" SSLPorts = ", stdout);
310 ports_puts(&Conf_SSLOptions.ListenPorts);
312 config_valid = false;
315 pwd = getpwuid(Conf_UID);
317 printf(" ServerUID = %s\n", pwd->pw_name);
319 printf(" ServerUID = %ld\n", (long)Conf_UID);
320 grp = getgrgid(Conf_GID);
322 printf(" ServerGID = %s\n", grp->gr_name);
324 printf(" ServerGID = %ld\n", (long)Conf_GID);
325 printf(" PingTimeout = %d\n", Conf_PingTimeout);
326 printf(" PongTimeout = %d\n", Conf_PongTimeout);
327 printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
328 printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
329 printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
330 printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
331 printf(" PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
332 printf(" NoDNS = %s\n", yesno_to_str(Conf_NoDNS));
333 printf(" NoIdent = %s\n", yesno_to_str(Conf_NoIdent));
334 printf(" NoPAM = %s\n", yesno_to_str(Conf_NoPAM));
337 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
338 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
340 printf(" MaxConnections = %ld\n", Conf_MaxConnections);
341 printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
342 printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
343 printf(" MaxNickLength = %u\n\n", Conf_MaxNickLength - 1);
347 for( i = 0; i < MAX_SERVERS; i++ ) {
348 if( ! Conf_Server[i].name[0] ) continue;
350 /* Valid "Server" section */
352 printf( " Name = %s\n", Conf_Server[i].name );
353 printf( " Host = %s\n", Conf_Server[i].host );
354 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
356 printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
358 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
359 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
360 printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
361 printf( " Group = %d\n", Conf_Server[i].group );
362 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
365 predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
366 predef_chan = array_start(&Conf_Channels);
368 for (i = 0; i < predef_channel_count; i++, predef_chan++) {
369 if (!predef_chan->name[0])
372 /* Valid "Channel" section */
374 printf(" Name = %s\n", predef_chan->name);
375 printf(" Modes = %s\n", predef_chan->modes);
376 printf(" Key = %s\n", predef_chan->key);
377 printf(" MaxUsers = %lu\n", predef_chan->maxusers);
378 printf(" Topic = %s\n", predef_chan->topic);
379 printf(" KeyFile = %s\n\n", predef_chan->keyfile);
382 return (config_valid ? 0 : 1);
387 Conf_UnsetServer( CONN_ID Idx )
389 /* Set next time for next connection attempt, if this is a server
390 * link that is (still) configured here. If the server is set as
391 * "once", delete it from our configuration.
392 * Non-Server-Connections will be silently ignored. */
397 /* Check all our configured servers */
398 for( i = 0; i < MAX_SERVERS; i++ ) {
399 if( Conf_Server[i].conn_id != Idx ) continue;
401 /* Gotcha! Mark server configuration as "unused": */
402 Conf_Server[i].conn_id = NONE;
404 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
405 /* Delete configuration here */
406 Init_Server_Struct( &Conf_Server[i] );
408 /* Set time for next connect attempt */
410 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
411 /* The connection has been "long", so we don't
412 * require the next attempt to be delayed. */
413 Conf_Server[i].lasttry =
414 t - Conf_ConnectRetry + RECONNECT_DELAY;
416 Conf_Server[i].lasttry = t;
419 } /* Conf_UnsetServer */
423 Conf_SetServer( int ConfServer, CONN_ID Idx )
425 /* Set connection for specified configured server */
427 assert( ConfServer > NONE );
428 assert( Idx > NONE );
430 Conf_Server[ConfServer].conn_id = Idx;
431 } /* Conf_SetServer */
435 Conf_GetServer( CONN_ID Idx )
437 /* Get index of server in configuration structure */
441 assert( Idx > NONE );
443 for( i = 0; i < MAX_SERVERS; i++ ) {
444 if( Conf_Server[i].conn_id == Idx ) return i;
447 } /* Conf_GetServer */
451 Conf_EnableServer( const char *Name, UINT16 Port )
453 /* Enable specified server and adjust port */
457 assert( Name != NULL );
459 for( i = 0; i < MAX_SERVERS; i++ ) {
460 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
461 /* Gotcha! Set port and enable server: */
462 Conf_Server[i].port = Port;
463 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
464 return (Conf_Server[i].port && Conf_Server[i].host[0]);
468 } /* Conf_EnableServer */
472 Conf_EnablePassiveServer(const char *Name)
474 /* Enable specified server */
477 assert( Name != NULL );
478 for (i = 0; i < MAX_SERVERS; i++) {
479 if ((strcasecmp( Conf_Server[i].name, Name ) == 0) && (Conf_Server[i].port > 0)) {
480 /* BINGO! Enable server */
481 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
486 } /* Conf_EnablePassiveServer */
490 Conf_DisableServer( const char *Name )
492 /* Enable specified server and adjust port */
496 assert( Name != NULL );
498 for( i = 0; i < MAX_SERVERS; i++ ) {
499 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
500 /* Gotcha! Disable and disconnect server: */
501 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
502 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
507 } /* Conf_DisableServer */
511 Conf_AddServer( const char *Name, UINT16 Port, const char *Host, const char *MyPwd, const char *PeerPwd )
513 /* Add new server to configuration */
517 assert( Name != NULL );
518 assert( Host != NULL );
519 assert( MyPwd != NULL );
520 assert( PeerPwd != NULL );
522 /* Search unused item in server configuration structure */
523 for( i = 0; i < MAX_SERVERS; i++ ) {
524 /* Is this item used? */
525 if( ! Conf_Server[i].name[0] ) break;
527 if( i >= MAX_SERVERS ) return false;
529 Init_Server_Struct( &Conf_Server[i] );
530 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
531 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
532 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
533 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
534 Conf_Server[i].port = Port;
535 Conf_Server[i].flags = CONF_SFLAG_ONCE;
538 } /* Conf_AddServer */
542 * Check if the given nick name is an service
545 Conf_IsService(int ConfServer, const char *Nick)
547 return MatchCaseInsensitive(Conf_Server[ConfServer].svs_mask, Nick);
548 } /* Conf_IsService */
552 * Initialize configuration settings with their default values.
555 Set_Defaults(bool InitServers)
559 strcpy(Conf_ServerName, "");
560 snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
561 PACKAGE_NAME, PACKAGE_VERSION);
562 strcpy(Conf_ServerPwd, "");
564 strcpy(Conf_ServerAdmin1, "");
565 strcpy(Conf_ServerAdmin2, "");
566 strcpy(Conf_ServerAdminMail, "");
568 strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
569 strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
570 strlcpy(Conf_MotdPhrase, MOTD_PHRASE, sizeof(Conf_MotdPhrase));
572 Conf_UID = Conf_GID = 0;
573 strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
574 strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
576 free(Conf_ListenAddress);
577 Conf_ListenAddress = NULL;
579 Conf_PingTimeout = 120;
580 Conf_PongTimeout = 20;
581 Conf_ConnectRetry = 60;
583 Conf_NoIdent = false;
587 Conf_Channel_Count = 0;
589 Conf_OperCanMode = false;
590 Conf_OperServerMode = false;
591 Conf_AllowRemoteOper = false;
592 Conf_PredefChannelsOnly = false;
594 Conf_ConnectIPv4 = true;
595 Conf_ConnectIPv6 = true;
597 Conf_MaxConnections = 0;
598 Conf_MaxConnectionsIP = 5;
600 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
602 /* Initialize server configuration structures */
604 for (i = 0; i < MAX_SERVERS;
605 Init_Server_Struct(&Conf_Server[i++]));
613 size_t cnt = array_bytes(&Conf_ListenPorts);
615 cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
621 Read_Config( bool ngircd_starting )
623 /* Read configuration file. */
625 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
626 const UINT16 defaultport = 6667;
630 /* Open configuration file */
631 fd = fopen( NGIRCd_ConfFile, "r" );
633 /* No configuration file found! */
634 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
635 NGIRCd_ConfFile, strerror( errno ));
636 if (!ngircd_starting)
638 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
643 Set_Defaults( ngircd_starting );
645 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
647 /* Clean up server configuration structure: mark all already
648 * configured servers as "once" so that they are deleted
649 * after the next disconnect and delete all unused servers.
650 * And delete all servers which are "duplicates" of servers
651 * that are already marked as "once" (such servers have been
652 * created by the last rehash but are now useless). */
653 for( i = 0; i < MAX_SERVERS; i++ ) {
654 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
656 /* This structure is in use ... */
657 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
658 /* Check for duplicates */
659 for( n = 0; n < MAX_SERVERS; n++ ) {
660 if( n == i ) continue;
662 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
663 Init_Server_Struct( &Conf_Server[n] );
665 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
671 /* Mark server as "once" */
672 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
673 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
678 /* Initialize variables */
680 strcpy( section, "" );
681 Init_Server_Struct( &New_Server );
682 New_Server_Idx = NONE;
686 /* Read configuration file */
688 if( ! fgets( str, LINE_LEN, fd )) break;
692 /* Skip comments and empty lines */
693 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
695 /* Is this the beginning of a new section? */
696 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
697 strlcpy( section, str, sizeof( section ));
698 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
701 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
702 /* Check if there is already a server to add */
703 if( New_Server.name[0] ) {
704 /* Copy data to "real" server structure */
705 assert( New_Server_Idx > NONE );
706 Conf_Server[New_Server_Idx] = New_Server;
709 /* Re-init structure for new server */
710 Init_Server_Struct( &New_Server );
712 /* Search unused item in server configuration structure */
713 for( i = 0; i < MAX_SERVERS; i++ ) {
714 /* Is this item used? */
715 if( ! Conf_Server[i].name[0] ) break;
717 if( i >= MAX_SERVERS ) {
718 /* Oops, no free item found! */
719 Config_Error( LOG_ERR, "Too many servers configured." );
720 New_Server_Idx = NONE;
722 else New_Server_Idx = i;
725 if (strcasecmp(section, "[CHANNEL]") == 0) {
726 Conf_Channel_Count++;
729 if (strcasecmp(section, "[OPERATOR]") == 0) {
734 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
737 if( section[0] == 0x1 ) continue;
739 /* Split line into variable name and parameters */
740 ptr = strchr( str, '=' );
742 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
746 var = str; ngt_TrimStr( var );
747 arg = ptr + 1; ngt_TrimStr( arg );
749 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
750 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
751 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
752 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
753 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
756 /* Close configuration file */
759 /* Check if there is still a server to add */
760 if( New_Server.name[0] ) {
761 /* Copy data to "real" server structure */
762 assert( New_Server_Idx > NONE );
763 Conf_Server[New_Server_Idx] = New_Server;
766 /* not a single listening port? Add default. */
767 if (no_listenports() &&
768 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
770 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
771 (unsigned int) defaultport, strerror(errno));
776 if (!Conf_ListenAddress)
777 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
779 if (!Conf_ListenAddress) {
780 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
788 Check_ArgIsTrue( const char *Arg )
790 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
791 if( strcasecmp( Arg, "true" ) == 0 ) return true;
792 if( atoi( Arg ) != 0 ) return true;
795 } /* Check_ArgIsTrue */
798 static unsigned int Handle_MaxNickLength(int Line, const char *Arg)
802 new = (unsigned) atoi(Arg) + 1;
803 if (new > CLIENT_NICK_LEN) {
804 Config_Error(LOG_WARNING,
805 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
806 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
807 return CLIENT_NICK_LEN;
810 Config_Error(LOG_WARNING,
811 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
812 NGIRCd_ConfFile, Line);
816 } /* Handle_MaxNickLength */
820 Handle_GLOBAL( int Line, char *Var, char *Arg )
827 assert( Var != NULL );
828 assert( Arg != NULL );
830 if( strcasecmp( Var, "Name" ) == 0 ) {
832 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
833 if (len >= sizeof( Conf_ServerName ))
834 Config_Error_TooLong( Line, Var );
837 if( strcasecmp( Var, "Info" ) == 0 ) {
838 /* Info text of server */
839 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
840 if (len >= sizeof( Conf_ServerInfo ))
841 Config_Error_TooLong ( Line, Var );
844 if( strcasecmp( Var, "Password" ) == 0 ) {
845 /* Global server password */
846 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
847 if (len >= sizeof( Conf_ServerPwd ))
848 Config_Error_TooLong( Line, Var );
851 if (strcasecmp(Var, "WebircPassword") == 0) {
852 /* Password required for WEBIRC command */
853 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
854 if (len >= sizeof(Conf_WebircPwd))
855 Config_Error_TooLong(Line, Var);
858 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
859 /* Administrative info #1 */
860 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
861 if (len >= sizeof( Conf_ServerAdmin1 ))
862 Config_Error_TooLong ( Line, Var );
865 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
866 /* Administrative info #2 */
867 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
868 if (len >= sizeof( Conf_ServerAdmin2 ))
869 Config_Error_TooLong ( Line, Var );
872 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
873 /* Administrative email contact */
874 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
875 if (len >= sizeof( Conf_ServerAdminMail ))
876 Config_Error_TooLong( Line, Var );
880 if( strcasecmp( Var, "Ports" ) == 0 ) {
881 ports_parse(&Conf_ListenPorts, Line, Arg);
884 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
885 /* "Message of the day" (MOTD) file */
886 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
887 if (len >= sizeof( Conf_MotdFile ))
888 Config_Error_TooLong( Line, Var );
891 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
892 /* "Message of the day" phrase (instead of file) */
893 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
894 if (len >= sizeof( Conf_MotdPhrase ))
895 Config_Error_TooLong( Line, Var );
898 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
899 /* directory for chroot() */
900 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
901 if (len >= sizeof( Conf_Chroot ))
902 Config_Error_TooLong( Line, Var );
905 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
906 /* name of pidfile */
907 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
908 if (len >= sizeof( Conf_PidFile ))
909 Config_Error_TooLong( Line, Var );
912 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
913 /* UID the daemon should switch to */
914 pwd = getpwnam( Arg );
915 if( pwd ) Conf_UID = pwd->pw_uid;
918 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
921 Conf_UID = (unsigned int)atoi( Arg );
925 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
926 /* GID the daemon should use */
927 grp = getgrnam( Arg );
928 if( grp ) Conf_GID = grp->gr_gid;
931 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
934 Conf_GID = (unsigned int)atoi( Arg );
938 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
940 Conf_PingTimeout = atoi( Arg );
941 if( Conf_PingTimeout < 5 ) {
942 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
943 NGIRCd_ConfFile, Line );
944 Conf_PingTimeout = 5;
948 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
950 Conf_PongTimeout = atoi( Arg );
951 if( Conf_PongTimeout < 5 ) {
952 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
953 NGIRCd_ConfFile, Line );
954 Conf_PongTimeout = 5;
958 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
959 /* Seconds between connection attempts to other servers */
960 Conf_ConnectRetry = atoi( Arg );
961 if( Conf_ConnectRetry < 5 ) {
962 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
963 NGIRCd_ConfFile, Line );
964 Conf_ConnectRetry = 5;
968 if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
969 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
970 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
973 if( strcasecmp( Var, "NoDNS" ) == 0 ) {
974 /* don't do reverse dns lookups when clients connect? */
975 Conf_NoDNS = Check_ArgIsTrue( Arg );
978 if (strcasecmp(Var, "NoIdent") == 0) {
979 /* don't do IDENT lookups when clients connect? */
980 Conf_NoIdent = Check_ArgIsTrue(Arg);
983 /* user has enabled ident lookups explicitly, but ... */
984 Config_Error(LOG_WARNING,
985 "%s: line %d: NoIdent=False, but ngircd was built without IDENT support",
986 NGIRCd_ConfFile, Line);
991 if(strcasecmp(Var, "NoPAM") == 0) {
992 /* don't use PAM library to authenticate users */
993 Conf_NoPAM = Check_ArgIsTrue(Arg);
997 /* the default setting for all the WANT_IPV6 special options is 'true' */
998 if( strcasecmp( Var, "ConnectIPv6" ) == 0 ) {
999 /* connect to other hosts using ipv6, if they have an AAAA record? */
1000 Conf_ConnectIPv6 = Check_ArgIsTrue( Arg );
1003 if( strcasecmp( Var, "ConnectIPv4" ) == 0 ) {
1004 /* connect to other hosts using ipv4.
1005 * again, this can be used for ipv6-only setups */
1006 Conf_ConnectIPv4 = Check_ArgIsTrue( Arg );
1010 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
1011 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
1012 Conf_OperCanMode = Check_ArgIsTrue( Arg );
1015 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
1016 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
1017 Conf_OperServerMode = Check_ArgIsTrue( Arg );
1020 if(strcasecmp(Var, "AllowRemoteOper") == 0) {
1021 /* Are remote IRC operators allowed to control this server? */
1022 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1025 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
1026 /* Maximum number of connections. 0 -> "no limit". */
1028 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
1031 Conf_MaxConnections = atol( Arg );
1034 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
1035 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
1037 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
1040 Conf_MaxConnectionsIP = atoi( Arg );
1043 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
1044 /* Maximum number of channels a user can join. 0 -> "no limit". */
1046 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
1049 Conf_MaxJoins = atoi( Arg );
1052 if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
1053 /* Maximum length of a nick name; must be same on all servers
1054 * within the IRC network! */
1055 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1059 if( strcasecmp( Var, "Listen" ) == 0 ) {
1060 /* IP-Address to bind sockets */
1061 if (Conf_ListenAddress) {
1062 Config_Error(LOG_ERR, "Multiple Listen= options, ignoring: %s", Arg);
1065 Conf_ListenAddress = strdup_warn(Arg);
1067 * if allocation fails, we're in trouble:
1068 * we cannot ignore the error -- otherwise ngircd
1069 * would listen on all interfaces.
1071 if (!Conf_ListenAddress) {
1072 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
1079 if( strcasecmp( Var, "SSLPorts" ) == 0 ) {
1080 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1084 if( strcasecmp( Var, "SSLKeyFile" ) == 0 ) {
1085 assert(Conf_SSLOptions.KeyFile == NULL );
1086 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1089 if( strcasecmp( Var, "SSLCertFile" ) == 0 ) {
1090 assert(Conf_SSLOptions.CertFile == NULL );
1091 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1095 if( strcasecmp( Var, "SSLKeyFilePassword" ) == 0 ) {
1096 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1097 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1098 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Could not copy %s: %s!",
1099 NGIRCd_ConfFile, Line, Var, strerror(errno));
1102 if( strcasecmp( Var, "SSLDHFile" ) == 0 ) {
1103 assert(Conf_SSLOptions.DHFile == NULL);
1104 Conf_SSLOptions.DHFile = strdup_warn( Arg );
1108 Config_Error(LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
1109 NGIRCd_ConfFile, Line, Var);
1110 } /* Handle_GLOBAL */
1114 Handle_OPERATOR( int Line, char *Var, char *Arg )
1117 struct Conf_Oper *op;
1120 assert( Var != NULL );
1121 assert( Arg != NULL );
1122 assert( Conf_Oper_Count > 0 );
1124 op = array_alloc(&Conf_Opers, sizeof(*op), Conf_Oper_Count - 1);
1126 Config_Error(LOG_ERR, "Could not allocate memory for operator (%d:%s = %s)", Line, Var, Arg);
1130 if (strcasecmp(Var, "Name") == 0) {
1131 /* Name of IRC operator */
1132 len = strlcpy(op->name, Arg, sizeof(op->name));
1133 if (len >= sizeof(op->name))
1134 Config_Error_TooLong(Line, Var);
1137 if (strcasecmp(Var, "Password") == 0) {
1138 /* Password of IRC operator */
1139 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1140 if (len >= sizeof(op->pwd))
1141 Config_Error_TooLong(Line, Var);
1144 if (strcasecmp(Var, "Mask") == 0) {
1146 return; /* Hostname already configured */
1147 op->mask = strdup_warn( Arg );
1150 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
1151 NGIRCd_ConfFile, Line, Var );
1152 } /* Handle_OPERATOR */
1156 Handle_SERVER( int Line, char *Var, char *Arg )
1162 assert( Var != NULL );
1163 assert( Arg != NULL );
1165 /* Ignore server block if no space is left in server configuration structure */
1166 if( New_Server_Idx <= NONE ) return;
1168 if( strcasecmp( Var, "Host" ) == 0 ) {
1169 /* Hostname of the server */
1170 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1171 if (len >= sizeof( New_Server.host ))
1172 Config_Error_TooLong ( Line, Var );
1175 if( strcasecmp( Var, "Name" ) == 0 ) {
1176 /* Name of the server ("Nick"/"ID") */
1177 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1178 if (len >= sizeof( New_Server.name ))
1179 Config_Error_TooLong( Line, Var );
1182 if (strcasecmp(Var, "Bind") == 0) {
1183 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1186 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1187 NGIRCd_ConfFile, Line, Arg);
1190 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1191 /* Password of this server which is sent to the peer */
1193 Config_Error(LOG_ERR,
1194 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1195 NGIRCd_ConfFile, Line);
1197 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1198 if (len >= sizeof( New_Server.pwd_in ))
1199 Config_Error_TooLong( Line, Var );
1202 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1203 /* Passwort of the peer which must be received */
1204 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1205 if (len >= sizeof( New_Server.pwd_out ))
1206 Config_Error_TooLong( Line, Var );
1209 if( strcasecmp( Var, "Port" ) == 0 ) {
1210 /* Port to which this server should connect */
1212 if( port > 0 && port < 0xFFFF )
1213 New_Server.port = (UINT16)port;
1215 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
1216 NGIRCd_ConfFile, Line, port );
1220 if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1221 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1225 if( strcasecmp( Var, "Group" ) == 0 ) {
1228 if( ! isdigit( (int)*Arg ))
1229 Config_Error_NaN( Line, Var );
1232 New_Server.group = atoi( Arg );
1235 if( strcasecmp( Var, "Passive" ) == 0 ) {
1236 if (Check_ArgIsTrue(Arg))
1237 New_Server.flags |= CONF_SFLAG_DISABLED;
1240 if (strcasecmp(Var, "ServiceMask") == 0) {
1241 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1242 sizeof(New_Server.svs_mask));
1243 if (len >= sizeof(New_Server.svs_mask))
1244 Config_Error_TooLong(Line, Var);
1248 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
1249 NGIRCd_ConfFile, Line, Var );
1250 } /* Handle_SERVER */
1254 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1256 size_t size = sizeof(new_chan->name);
1257 char *dest = new_chan->name;
1259 if (!Channel_IsValidName(name)) {
1261 * maybe user forgot to add a '#'.
1262 * This is only here for user convenience.
1268 return size > strlcpy(dest, name, size);
1273 Handle_CHANNEL(int Line, char *Var, char *Arg)
1277 struct Conf_Channel *chan;
1280 assert( Var != NULL );
1281 assert( Arg != NULL );
1282 assert(Conf_Channel_Count > 0);
1284 chancount = Conf_Channel_Count - 1;
1286 chan = array_alloc(&Conf_Channels, sizeof(*chan), chancount);
1288 Config_Error(LOG_ERR, "Could not allocate memory for predefined channel (%d:%s = %s)", Line, Var, Arg);
1291 if (strcasecmp(Var, "Name") == 0) {
1292 if (!Handle_Channelname(chan, Arg))
1293 Config_Error_TooLong(Line, Var);
1296 if (strcasecmp(Var, "Modes") == 0) {
1298 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1299 if (len >= sizeof(chan->modes))
1300 Config_Error_TooLong( Line, Var );
1303 if( strcasecmp( Var, "Topic" ) == 0 ) {
1305 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1306 if (len >= sizeof(chan->topic))
1307 Config_Error_TooLong( Line, Var );
1310 if( strcasecmp( Var, "Key" ) == 0 ) {
1311 /* Initial Channel Key (mode k) */
1312 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1313 if (len >= sizeof(chan->key))
1314 Config_Error_TooLong(Line, Var);
1317 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1318 /* maximum user limit, mode l */
1319 chan->maxusers = (unsigned long) atol(Arg);
1320 if (chan->maxusers == 0)
1321 Config_Error_NaN(Line, Var);
1324 if (strcasecmp(Var, "KeyFile") == 0) {
1326 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1327 if (len >= sizeof(chan->keyfile))
1328 Config_Error_TooLong(Line, Var);
1332 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1333 NGIRCd_ConfFile, Line, Var );
1334 } /* Handle_CHANNEL */
1338 Validate_Config(bool Configtest, bool Rehash)
1340 /* Validate configuration settings. */
1343 int i, servers, servers_once;
1345 bool config_valid = true;
1348 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1349 ptr = Conf_ServerName;
1351 if (*ptr >= 'a' && *ptr <= 'z') continue;
1352 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1353 if (*ptr >= '0' && *ptr <= '9') continue;
1354 if (ptr > Conf_ServerName) {
1355 if (*ptr == '.' || *ptr == '-')
1358 Conf_ServerName[0] = '\0';
1362 if (!Conf_ServerName[0]) {
1363 /* No server name configured! */
1364 config_valid = false;
1365 Config_Error(LOG_ALERT,
1366 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1368 if (!Configtest && !Rehash) {
1369 Config_Error(LOG_ALERT,
1370 "%s exiting due to fatal errors!",
1376 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1377 /* No dot in server name! */
1378 config_valid = false;
1379 Config_Error(LOG_ALERT,
1380 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1383 Config_Error(LOG_ALERT,
1384 "%s exiting due to fatal errors!",
1391 if (!Conf_ServerAdminMail[0]) {
1392 /* No administrative contact configured! */
1393 config_valid = false;
1394 Config_Error(LOG_ALERT,
1395 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1398 Config_Error(LOG_ALERT,
1399 "%s exiting due to fatal errors!",
1406 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1407 && !Conf_ServerAdminMail[0]) {
1408 /* No administrative information configured! */
1409 Config_Error(LOG_WARNING,
1410 "No administrative information configured but required by RFC!");
1414 if (Conf_ServerPwd[0])
1415 Config_Error(LOG_ERR,
1416 "This server uses PAM, \"Password\" will be ignored!");
1420 servers = servers_once = 0;
1421 for (i = 0; i < MAX_SERVERS; i++) {
1422 if (Conf_Server[i].name[0]) {
1424 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1429 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1430 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1433 return config_valid;
1434 } /* Validate_Config */
1438 Config_Error_TooLong ( const int Line, const char *Item )
1440 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1445 Config_Error_NaN( const int Line, const char *Item )
1447 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1448 NGIRCd_ConfFile, Line, Item );
1453 static void Config_Error( const int Level, const char *Format, ... )
1455 static void Config_Error( Level, Format, va_alist )
1461 /* Error! Write to console and/or logfile. */
1463 char msg[MAX_LOG_MSG_LEN];
1466 assert( Format != NULL );
1469 va_start( ap, Format );
1473 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1476 /* During "normal operations" the log functions of the daemon should
1477 * be used, but during testing of the configuration file, all messages
1478 * should go directly to the console: */
1479 if (Use_Log) Log( Level, "%s", msg );
1481 } /* Config_Error */
1485 Init_Server_Struct( CONF_SERVER *Server )
1487 /* Initialize server configuration structur to default values */
1489 assert( Server != NULL );
1491 memset( Server, 0, sizeof (CONF_SERVER) );
1493 Server->group = NONE;
1494 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1496 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1498 Proc_InitStruct(&Server->res_stat);
1499 Server->conn_id = NONE;
1500 memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
1501 } /* Init_Server_Struct */