2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 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)
17 static char UNUSED id[] = "$Id: conf.c,v 1.105 2008/03/18 20:12:47 fw Exp $";
34 #include <sys/types.h>
54 static bool Use_Log = true;
55 static CONF_SERVER New_Server;
56 static int New_Server_Idx;
59 static void Set_Defaults PARAMS(( bool InitServers ));
60 static bool Read_Config PARAMS(( bool ngircd_starting ));
61 static void Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
63 static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
64 static void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
65 static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
66 static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
68 static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
70 static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
71 static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
73 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
77 strdup_warn(const char *str)
79 char *ptr = strdup(str);
81 Config_Error(LOG_ERR, "Could not allocate mem for string: %s", str);
91 len = array_length(a, sizeof(UINT16));
93 ports = (UINT16*) array_start(a);
94 printf("%u", (unsigned int) *ports);
97 printf(", %u", (unsigned int) *ports);
105 ports_parse(array *a, int Line, char *Arg)
113 /* Ports on that the server should listen. More port numbers
114 * must be separated by "," */
115 ptr = strtok( Arg, "," );
119 if (port > 0 && port < 0xFFFF) {
120 port16 = (UINT16) port;
121 if (!array_catb(a, (char*)&port16, sizeof port16))
122 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
123 NGIRCd_ConfFile, Line, port, strerror(errno));
125 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
126 NGIRCd_ConfFile, Line, port );
129 ptr = strtok( NULL, "," );
138 Validate_Config(false, false);
145 if (!Read_Config(false))
147 Validate_Config(false, true);
149 /* Update CLIENT structure of local server */
150 Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
152 } /* Config_Rehash */
156 yesno_to_str(int boolean_value)
167 /* Read configuration, validate and output it. */
177 Validate_Config(true, false);
179 /* If stdin and stdout ("you can read our nice message and we can
180 * read in your keypress") are valid tty's, wait for a key: */
181 if( isatty( fileno( stdin )) && isatty( fileno( stdout ))) {
182 puts( "OK, press enter to see a dump of your service configuration ..." );
185 puts( "Ok, dump of your server configuration follows:\n" );
189 printf( " Name = %s\n", Conf_ServerName );
190 printf( " Info = %s\n", Conf_ServerInfo );
191 printf( " Password = %s\n", Conf_ServerPwd );
192 printf( " AdminInfo1 = %s\n", Conf_ServerAdmin1 );
193 printf( " AdminInfo2 = %s\n", Conf_ServerAdmin2 );
194 printf( " AdminEMail = %s\n", Conf_ServerAdminMail );
195 printf( " MotdFile = %s\n", Conf_MotdFile );
196 printf( " MotdPhrase = %s\n", Conf_MotdPhrase );
197 printf( " ChrootDir = %s\n", Conf_Chroot );
198 printf( " PidFile = %s\n", Conf_PidFile);
199 fputs(" Ports = ", stdout);
201 ports_puts(&Conf_ListenPorts);
203 printf( " Listen = %s\n", Conf_ListenAddress );
204 pwd = getpwuid( Conf_UID );
205 if( pwd ) printf( " ServerUID = %s\n", pwd->pw_name );
206 else printf( " ServerUID = %ld\n", (long)Conf_UID );
207 grp = getgrgid( Conf_GID );
208 if( grp ) printf( " ServerGID = %s\n", grp->gr_name );
209 else printf( " ServerGID = %ld\n", (long)Conf_GID );
210 printf( " PingTimeout = %d\n", Conf_PingTimeout );
211 printf( " PongTimeout = %d\n", Conf_PongTimeout );
212 printf( " ConnectRetry = %d\n", Conf_ConnectRetry );
213 printf( " OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
214 printf( " OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
215 printf( " PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
216 printf( " NoDNS = %s\n", yesno_to_str(Conf_NoDNS));
219 printf(" ListenIPv6 = %s\n", yesno_to_str(Conf_ListenIPv6));
220 printf(" ListenIPv4 = %s\n", yesno_to_str(Conf_ListenIPv4));
221 printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
222 printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
224 printf( " MaxConnections = %ld\n", Conf_MaxConnections);
225 printf( " MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
226 printf( " MaxJoins = %d\n", Conf_MaxJoins>0 ? Conf_MaxJoins : -1);
227 printf( " MaxNickLength = %u\n\n", Conf_MaxNickLength - 1);
229 for( i = 0; i < Conf_Oper_Count; i++ ) {
230 if( ! Conf_Oper[i].name[0] ) continue;
232 /* Valid "Operator" section */
233 puts( "[OPERATOR]" );
234 printf( " Name = %s\n", Conf_Oper[i].name );
235 printf( " Password = %s\n", Conf_Oper[i].pwd );
236 if ( Conf_Oper[i].mask ) printf( " Mask = %s\n", Conf_Oper[i].mask );
240 for( i = 0; i < MAX_SERVERS; i++ ) {
241 if( ! Conf_Server[i].name[0] ) continue;
243 /* Valid "Server" section */
245 printf( " Name = %s\n", Conf_Server[i].name );
246 printf( " Host = %s\n", Conf_Server[i].host );
247 printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
248 printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
249 printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
250 printf( " Group = %d\n", Conf_Server[i].group );
251 printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
254 for( i = 0; i < Conf_Channel_Count; i++ ) {
255 if( ! Conf_Channel[i].name[0] ) continue;
257 /* Valid "Channel" section */
259 printf( " Name = %s\n", Conf_Channel[i].name );
260 printf( " Modes = %s\n", Conf_Channel[i].modes );
261 printf( " Key = %s\n", Conf_Channel[i].key );
262 printf( " MaxUsers = %lu\n", Conf_Channel[i].maxusers );
264 topic = (char*)array_start(&Conf_Channel[i].topic);
265 printf( " Topic = %s\n\n", topic ? topic : "");
273 Conf_UnsetServer( CONN_ID Idx )
275 /* Set next time for next connection attempt, if this is a server
276 * link that is (still) configured here. If the server is set as
277 * "once", delete it from our configuration.
278 * Non-Server-Connections will be silently ignored. */
283 /* Check all our configured servers */
284 for( i = 0; i < MAX_SERVERS; i++ ) {
285 if( Conf_Server[i].conn_id != Idx ) continue;
287 /* Gotcha! Mark server configuration as "unused": */
288 Conf_Server[i].conn_id = NONE;
290 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
291 /* Delete configuration here */
292 Init_Server_Struct( &Conf_Server[i] );
294 /* Set time for next connect attempt */
296 if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
297 /* The connection has been "long", so we don't
298 * require the next attempt to be delayed. */
299 Conf_Server[i].lasttry =
300 t - Conf_ConnectRetry + RECONNECT_DELAY;
302 Conf_Server[i].lasttry = t;
305 } /* Conf_UnsetServer */
309 Conf_SetServer( int ConfServer, CONN_ID Idx )
311 /* Set connection for specified configured server */
313 assert( ConfServer > NONE );
314 assert( Idx > NONE );
316 Conf_Server[ConfServer].conn_id = Idx;
317 } /* Conf_SetServer */
321 Conf_GetServer( CONN_ID Idx )
323 /* Get index of server in configuration structure */
327 assert( Idx > NONE );
329 for( i = 0; i < MAX_SERVERS; i++ ) {
330 if( Conf_Server[i].conn_id == Idx ) return i;
333 } /* Conf_GetServer */
337 Conf_EnableServer( char *Name, UINT16 Port )
339 /* Enable specified server and adjust port */
343 assert( Name != NULL );
345 for( i = 0; i < MAX_SERVERS; i++ ) {
346 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
347 /* Gotcha! Set port and enable server: */
348 Conf_Server[i].port = Port;
349 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
354 } /* Conf_EnableServer */
358 Conf_EnablePassiveServer(const char *Name)
360 /* Enable specified server */
363 assert( Name != NULL );
364 for (i = 0; i < MAX_SERVERS; i++) {
365 if ((strcasecmp( Conf_Server[i].name, Name ) == 0) && (Conf_Server[i].port > 0)) {
366 /* BINGO! Enable server */
367 Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
372 } /* Conf_EnablePassiveServer */
376 Conf_DisableServer( char *Name )
378 /* Enable specified server and adjust port */
382 assert( Name != NULL );
384 for( i = 0; i < MAX_SERVERS; i++ ) {
385 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
386 /* Gotcha! Disable and disconnect server: */
387 Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
388 if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
393 } /* Conf_DisableServer */
397 Conf_AddServer( char *Name, UINT16 Port, char *Host, char *MyPwd, char *PeerPwd )
399 /* Add new server to configuration */
403 assert( Name != NULL );
404 assert( Host != NULL );
405 assert( MyPwd != NULL );
406 assert( PeerPwd != NULL );
408 /* Search unused item in server configuration structure */
409 for( i = 0; i < MAX_SERVERS; i++ ) {
410 /* Is this item used? */
411 if( ! Conf_Server[i].name[0] ) break;
413 if( i >= MAX_SERVERS ) return false;
415 Init_Server_Struct( &Conf_Server[i] );
416 strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
417 strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
418 strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
419 strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
420 Conf_Server[i].port = Port;
421 Conf_Server[i].flags = CONF_SFLAG_ONCE;
424 } /* Conf_AddServer */
428 Set_Defaults( bool InitServers )
430 /* Initialize configuration variables with default values. */
434 strcpy( Conf_ServerName, "" );
435 snprintf( Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
436 strcpy( Conf_ServerPwd, "" );
438 strcpy( Conf_ServerAdmin1, "" );
439 strcpy( Conf_ServerAdmin2, "" );
440 strcpy( Conf_ServerAdminMail, "" );
442 strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
443 strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
445 strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
447 strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
449 strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
451 strcpy( Conf_ListenAddress, "" );
453 Conf_UID = Conf_GID = 0;
455 Conf_PingTimeout = 120;
456 Conf_PongTimeout = 20;
458 Conf_ConnectRetry = 60;
461 Conf_Channel_Count = 0;
463 Conf_OperCanMode = false;
465 Conf_PredefChannelsOnly = false;
466 Conf_OperServerMode = false;
468 Conf_ConnectIPv4 = true;
469 Conf_ListenIPv4 = true;
470 Conf_ConnectIPv6 = true;
471 Conf_ListenIPv6 = true;
473 Conf_MaxConnections = 0;
474 Conf_MaxConnectionsIP = 5;
476 Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
478 /* Initialize server configuration structures */
479 if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
484 Read_Config( bool ngircd_starting )
486 /* Read configuration file. */
488 char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
489 const UINT16 defaultport = 6667;
493 /* Open configuration file */
494 fd = fopen( NGIRCd_ConfFile, "r" );
496 /* No configuration file found! */
497 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
498 NGIRCd_ConfFile, strerror( errno ));
499 if (!ngircd_starting)
501 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
505 Set_Defaults( ngircd_starting );
507 Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
509 /* Clean up server configuration structure: mark all already
510 * configured servers as "once" so that they are deleted
511 * after the next disconnect and delete all unused servers.
512 * And delete all servers which are "duplicates" of servers
513 * that are already marked as "once" (such servers have been
514 * created by the last rehash but are now useless). */
515 for( i = 0; i < MAX_SERVERS; i++ ) {
516 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
518 /* This structure is in use ... */
519 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
520 /* Check for duplicates */
521 for( n = 0; n < MAX_SERVERS; n++ ) {
522 if( n == i ) continue;
524 if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
525 Init_Server_Struct( &Conf_Server[n] );
527 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
533 /* Mark server as "once" */
534 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
535 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
540 /* Initialize variables */
542 strcpy( section, "" );
543 Init_Server_Struct( &New_Server );
544 New_Server_Idx = NONE;
546 /* Read configuration file */
548 if( ! fgets( str, LINE_LEN, fd )) break;
552 /* Skip comments and empty lines */
553 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
555 /* Is this the beginning of a new section? */
556 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
557 strlcpy( section, str, sizeof( section ));
558 if( strcasecmp( section, "[GLOBAL]" ) == 0 )
561 if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
562 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
563 Config_Error( LOG_ERR, "Too many operators configured.");
565 /* Initialize new operator structure */
566 Conf_Oper[Conf_Oper_Count].name[0] = '\0';
567 Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
568 if (Conf_Oper[Conf_Oper_Count].mask) {
569 free(Conf_Oper[Conf_Oper_Count].mask );
570 Conf_Oper[Conf_Oper_Count].mask = NULL;
576 if( strcasecmp( section, "[SERVER]" ) == 0 ) {
577 /* Check if there is already a server to add */
578 if( New_Server.name[0] ) {
579 /* Copy data to "real" server structure */
580 assert( New_Server_Idx > NONE );
581 Conf_Server[New_Server_Idx] = New_Server;
584 /* Re-init structure for new server */
585 Init_Server_Struct( &New_Server );
587 /* Search unused item in server configuration structure */
588 for( i = 0; i < MAX_SERVERS; i++ ) {
589 /* Is this item used? */
590 if( ! Conf_Server[i].name[0] ) break;
592 if( i >= MAX_SERVERS ) {
593 /* Oops, no free item found! */
594 Config_Error( LOG_ERR, "Too many servers configured." );
595 New_Server_Idx = NONE;
597 else New_Server_Idx = i;
600 if( strcasecmp( section, "[CHANNEL]" ) == 0 ) {
601 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) {
602 Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
604 /* Initialize new channel structure */
605 strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
606 strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
607 strcpy( Conf_Channel[Conf_Channel_Count].key, "" );
608 Conf_Channel[Conf_Channel_Count].maxusers = 0;
609 array_free(&Conf_Channel[Conf_Channel_Count].topic);
610 Conf_Channel_Count++;
614 Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
617 if( section[0] == 0x1 ) continue;
619 /* Split line into variable name and parameters */
620 ptr = strchr( str, '=' );
622 Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
626 var = str; ngt_TrimStr( var );
627 arg = ptr + 1; ngt_TrimStr( arg );
629 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
630 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
631 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
632 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
633 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
636 /* Close configuration file */
639 /* Check if there is still a server to add */
640 if( New_Server.name[0] ) {
641 /* Copy data to "real" server structure */
642 assert( New_Server_Idx > NONE );
643 Conf_Server[New_Server_Idx] = New_Server;
646 if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
647 if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
648 Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
649 (unsigned int) defaultport, strerror(errno));
658 Check_ArgIsTrue( const char *Arg )
660 if( strcasecmp( Arg, "yes" ) == 0 ) return true;
661 if( strcasecmp( Arg, "true" ) == 0 ) return true;
662 if( atoi( Arg ) != 0 ) return true;
665 } /* Check_ArgIsTrue */
668 static unsigned int Handle_MaxNickLength(int Line, const char *Arg)
672 new = (unsigned) atoi(Arg) + 1;
673 if (new > CLIENT_NICK_LEN) {
674 Config_Error(LOG_WARNING,
675 "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
676 NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
677 return CLIENT_NICK_LEN;
680 Config_Error(LOG_WARNING,
681 "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
682 NGIRCd_ConfFile, Line);
686 } /* Handle_MaxNickLength */
690 Handle_GLOBAL( int Line, char *Var, char *Arg )
697 assert( Var != NULL );
698 assert( Arg != NULL );
700 if( strcasecmp( Var, "Name" ) == 0 ) {
702 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
703 if (len >= sizeof( Conf_ServerName ))
704 Config_Error_TooLong( Line, Var );
707 if( strcasecmp( Var, "Info" ) == 0 ) {
708 /* Info text of server */
709 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
710 if (len >= sizeof( Conf_ServerInfo ))
711 Config_Error_TooLong ( Line, Var );
714 if( strcasecmp( Var, "Password" ) == 0 ) {
715 /* Global server password */
716 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
717 if (len >= sizeof( Conf_ServerPwd ))
718 Config_Error_TooLong( Line, Var );
721 if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
722 /* Administrative info #1 */
723 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
724 if (len >= sizeof( Conf_ServerAdmin1 ))
725 Config_Error_TooLong ( Line, Var );
728 if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
729 /* Administrative info #2 */
730 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
731 if (len >= sizeof( Conf_ServerAdmin2 ))
732 Config_Error_TooLong ( Line, Var );
735 if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
736 /* Administrative email contact */
737 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
738 if (len >= sizeof( Conf_ServerAdminMail ))
739 Config_Error_TooLong( Line, Var );
743 if( strcasecmp( Var, "Ports" ) == 0 ) {
744 ports_parse(&Conf_ListenPorts, Line, Arg);
747 if( strcasecmp( Var, "MotdFile" ) == 0 ) {
748 /* "Message of the day" (MOTD) file */
749 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
750 if (len >= sizeof( Conf_MotdFile ))
751 Config_Error_TooLong( Line, Var );
754 if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
755 /* "Message of the day" phrase (instead of file) */
756 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
757 if (len >= sizeof( Conf_MotdPhrase ))
758 Config_Error_TooLong( Line, Var );
761 if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
762 /* directory for chroot() */
763 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
764 if (len >= sizeof( Conf_Chroot ))
765 Config_Error_TooLong( Line, Var );
768 if ( strcasecmp( Var, "PidFile" ) == 0 ) {
769 /* name of pidfile */
770 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
771 if (len >= sizeof( Conf_PidFile ))
772 Config_Error_TooLong( Line, Var );
775 if( strcasecmp( Var, "ServerUID" ) == 0 ) {
776 /* UID the daemon should switch to */
777 pwd = getpwnam( Arg );
778 if( pwd ) Conf_UID = pwd->pw_uid;
781 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
784 Conf_UID = (unsigned int)atoi( Arg );
788 if( strcasecmp( Var, "ServerGID" ) == 0 ) {
789 /* GID the daemon should use */
790 grp = getgrnam( Arg );
791 if( grp ) Conf_GID = grp->gr_gid;
794 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
797 Conf_GID = (unsigned int)atoi( Arg );
801 if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
803 Conf_PingTimeout = atoi( Arg );
804 if( Conf_PingTimeout < 5 ) {
805 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
806 NGIRCd_ConfFile, Line );
807 Conf_PingTimeout = 5;
811 if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
813 Conf_PongTimeout = atoi( Arg );
814 if( Conf_PongTimeout < 5 ) {
815 Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
816 NGIRCd_ConfFile, Line );
817 Conf_PongTimeout = 5;
821 if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
822 /* Seconds between connection attempts to other servers */
823 Conf_ConnectRetry = atoi( Arg );
824 if( Conf_ConnectRetry < 5 ) {
825 Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
826 NGIRCd_ConfFile, Line );
827 Conf_ConnectRetry = 5;
831 if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
832 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
833 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
836 if( strcasecmp( Var, "NoDNS" ) == 0 ) {
837 /* don't do reverse dns lookups when clients connect? */
838 Conf_NoDNS = Check_ArgIsTrue( Arg );
842 /* the default setting for all the WANT_IPV6 special options is 'true' */
843 if( strcasecmp( Var, "ListenIPv6" ) == 0 ) {
844 /* listen on ipv6 sockets, if available? */
845 Conf_ListenIPv6 = Check_ArgIsTrue( Arg );
848 if( strcasecmp( Var, "ListenIPv4" ) == 0 ) {
850 * listen on ipv4 sockets, if available?
851 * this allows "ipv6-only" setups.
853 Conf_ListenIPv4 = Check_ArgIsTrue( Arg );
856 if( strcasecmp( Var, "ConnectIPv6" ) == 0 ) {
857 /* connect to other hosts using ipv6, if they have an AAAA record? */
858 Conf_ConnectIPv6 = Check_ArgIsTrue( Arg );
861 if( strcasecmp( Var, "ConnectIPv4" ) == 0 ) {
862 /* connect to other hosts using ipv4.
863 * again, this can be used for ipv6-only setups */
864 Conf_ConnectIPv4 = Check_ArgIsTrue( Arg );
868 if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
869 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
870 Conf_OperCanMode = Check_ArgIsTrue( Arg );
873 if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
874 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
875 Conf_OperServerMode = Check_ArgIsTrue( Arg );
878 if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
879 /* Maximum number of connections. 0 -> "no limit". */
881 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
884 Conf_MaxConnections = atol( Arg );
887 if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
888 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
890 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
893 Conf_MaxConnectionsIP = atoi( Arg );
896 if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
897 /* Maximum number of channels a user can join. 0 -> "no limit". */
899 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
902 Conf_MaxJoins = atoi( Arg );
905 if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
906 /* Maximum length of a nick name; must be same on all servers
907 * within the IRC network! */
908 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
912 if( strcasecmp( Var, "Listen" ) == 0 ) {
913 /* IP-Address to bind sockets */
914 len = strlcpy( Conf_ListenAddress, Arg, sizeof( Conf_ListenAddress ));
915 if (len >= sizeof( Conf_ListenAddress ))
916 Config_Error_TooLong( Line, Var );
920 Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
921 NGIRCd_ConfFile, Line, Var );
922 } /* Handle_GLOBAL */
926 Handle_OPERATOR( int Line, char *Var, char *Arg )
928 unsigned int opercount;
931 assert( Var != NULL );
932 assert( Arg != NULL );
933 assert( Conf_Oper_Count > 0 );
935 if ( Conf_Oper_Count == 0 )
938 opercount = Conf_Oper_Count - 1;
940 if( strcasecmp( Var, "Name" ) == 0 ) {
941 /* Name of IRC operator */
942 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
943 if (len >= sizeof( Conf_Oper[opercount].name ))
944 Config_Error_TooLong( Line, Var );
947 if( strcasecmp( Var, "Password" ) == 0 ) {
948 /* Password of IRC operator */
949 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
950 if (len >= sizeof( Conf_Oper[opercount].pwd ))
951 Config_Error_TooLong( Line, Var );
954 if( strcasecmp( Var, "Mask" ) == 0 ) {
955 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
957 Conf_Oper[opercount].mask = strdup_warn( Arg );
960 Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
961 NGIRCd_ConfFile, Line, Var );
962 } /* Handle_OPERATOR */
966 Handle_SERVER( int Line, char *Var, char *Arg )
972 assert( Var != NULL );
973 assert( Arg != NULL );
975 /* Ignore server block if no space is left in server configuration structure */
976 if( New_Server_Idx <= NONE ) return;
978 if( strcasecmp( Var, "Host" ) == 0 ) {
979 /* Hostname of the server */
980 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
981 if (len >= sizeof( New_Server.host ))
982 Config_Error_TooLong ( Line, Var );
985 if( strcasecmp( Var, "Name" ) == 0 ) {
986 /* Name of the server ("Nick"/"ID") */
987 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
988 if (len >= sizeof( New_Server.name ))
989 Config_Error_TooLong( Line, Var );
992 if (strcasecmp(Var, "Bind") == 0) {
993 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
996 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
997 NGIRCd_ConfFile, Line, Arg);
1000 if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1001 /* Password of this server which is sent to the peer */
1003 Config_Error(LOG_ERR,
1004 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1005 NGIRCd_ConfFile, Line);
1007 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1008 if (len >= sizeof( New_Server.pwd_in ))
1009 Config_Error_TooLong( Line, Var );
1012 if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1013 /* Passwort of the peer which must be received */
1014 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1015 if (len >= sizeof( New_Server.pwd_out ))
1016 Config_Error_TooLong( Line, Var );
1019 if( strcasecmp( Var, "Port" ) == 0 ) {
1020 /* Port to which this server should connect */
1022 if( port > 0 && port < 0xFFFF )
1023 New_Server.port = (UINT16)port;
1025 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
1026 NGIRCd_ConfFile, Line, port );
1029 if( strcasecmp( Var, "Group" ) == 0 ) {
1032 if( ! isdigit( (int)*Arg ))
1033 Config_Error_NaN( Line, Var );
1036 New_Server.group = atoi( Arg );
1039 if( strcasecmp( Var, "Passive" ) == 0 ) {
1040 if (Check_ArgIsTrue(Arg))
1041 New_Server.flags |= CONF_SFLAG_DISABLED;
1045 Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
1046 NGIRCd_ConfFile, Line, Var );
1047 } /* Handle_SERVER */
1051 Handle_Channelname(size_t chancount, const char *name)
1053 size_t size = sizeof( Conf_Channel[chancount].name );
1054 char *dest = Conf_Channel[chancount].name;
1056 if (*name && *name != '#') {
1061 return size > strlcpy(dest, name, size);
1066 Handle_CHANNEL( int Line, char *Var, char *Arg )
1069 size_t chancount = 0;
1072 assert( Var != NULL );
1073 assert( Arg != NULL );
1074 if (Conf_Channel_Count > 0)
1075 chancount = Conf_Channel_Count - 1;
1077 if( strcasecmp( Var, "Name" ) == 0 ) {
1078 if (!Handle_Channelname(chancount, Arg))
1079 Config_Error_TooLong( Line, Var );
1082 if( strcasecmp( Var, "Modes" ) == 0 ) {
1084 len = strlcpy( Conf_Channel[chancount].modes, Arg, sizeof( Conf_Channel[chancount].modes ));
1085 if (len >= sizeof( Conf_Channel[chancount].modes ))
1086 Config_Error_TooLong( Line, Var );
1089 if( strcasecmp( Var, "Topic" ) == 0 ) {
1091 if (!array_copys( &Conf_Channel[chancount].topic, Arg))
1092 Config_Error_TooLong( Line, Var );
1096 if( strcasecmp( Var, "Key" ) == 0 ) {
1097 /* Initial Channel Key (mode k) */
1098 len = strlcpy(Conf_Channel[chancount].key, Arg, sizeof(Conf_Channel[chancount].key));
1099 if (len >= sizeof( Conf_Channel[chancount].key ))
1100 Config_Error_TooLong(Line, Var);
1104 if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1105 /* maximum user limit, mode l */
1106 Conf_Channel[chancount].maxusers = (unsigned long) atol(Arg);
1107 if (Conf_Channel[chancount].maxusers == 0)
1108 Config_Error_NaN(Line, Var);
1112 Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1113 NGIRCd_ConfFile, Line, Var );
1114 } /* Handle_CHANNEL */
1118 Validate_Config(bool Configtest, bool Rehash)
1120 /* Validate configuration settings. */
1123 int i, servers, servers_once;
1127 /* Validate configured server name, see RFC 2812 section 2.3.1 */
1128 ptr = Conf_ServerName;
1130 if (*ptr >= 'a' && *ptr <= 'z') continue;
1131 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1132 if (*ptr >= '0' && *ptr <= '9') continue;
1133 if (ptr > Conf_ServerName) {
1134 if (*ptr == '.' || *ptr == '-')
1137 Conf_ServerName[0] = '\0';
1141 if (!Conf_ServerName[0]) {
1142 /* No server name configured! */
1143 Config_Error(LOG_ALERT,
1144 "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1146 if (!Configtest && !Rehash) {
1147 Config_Error(LOG_ALERT,
1148 "%s exiting due to fatal errors!",
1154 if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1155 /* No dot in server name! */
1156 Config_Error(LOG_ALERT,
1157 "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1160 Config_Error(LOG_ALERT,
1161 "%s exiting due to fatal errors!",
1168 if (!Conf_ServerAdminMail[0]) {
1169 /* No administrative contact configured! */
1170 Config_Error(LOG_ALERT,
1171 "No administrator email address configured in \"%s\" ('AdminEMail')!",
1174 Config_Error(LOG_ALERT,
1175 "%s exiting due to fatal errors!",
1182 if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1183 && !Conf_ServerAdminMail[0]) {
1184 /* No administrative information configured! */
1185 Config_Error(LOG_WARNING,
1186 "No administrative information configured but required by RFC!");
1190 if (!Conf_ListenIPv4 && !Conf_ListenIPv6)
1191 Config_Error(LOG_ALERT,
1192 "Both \"ListenIPv4\" and \"ListenIPv6\" are set to 'no'; no network protocol available!");
1194 if (!Conf_ConnectIPv4 && !Conf_ConnectIPv6)
1195 Config_Error(LOG_ALERT,
1196 "Both \"ConnectIPv4\" and \"ConnectIPv6\" are set to 'no'; ngircd will fail to connect to other irc servers");
1200 servers = servers_once = 0;
1201 for (i = 0; i < MAX_SERVERS; i++) {
1202 if (Conf_Server[i].name[0]) {
1204 if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1209 "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1210 Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1212 } /* Validate_Config */
1216 Config_Error_TooLong ( const int Line, const char *Item )
1218 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1223 Config_Error_NaN( const int Line, const char *Item )
1225 Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1226 NGIRCd_ConfFile, Line, Item );
1231 static void Config_Error( const int Level, const char *Format, ... )
1233 static void Config_Error( Level, Format, va_alist )
1239 /* Error! Write to console and/or logfile. */
1241 char msg[MAX_LOG_MSG_LEN];
1244 assert( Format != NULL );
1247 va_start( ap, Format );
1251 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1254 /* During "normal operations" the log functions of the daemon should
1255 * be used, but during testing of the configuration file, all messages
1256 * should go directly to the console: */
1257 if (Use_Log) Log( Level, "%s", msg );
1259 } /* Config_Error */
1263 Init_Server_Struct( CONF_SERVER *Server )
1265 /* Initialize server configuration structur to default values */
1267 assert( Server != NULL );
1269 memset( Server, 0, sizeof (CONF_SERVER) );
1271 Server->group = NONE;
1272 Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1274 if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1276 Resolve_Init(&Server->res_stat);
1277 Server->conn_id = NONE;
1278 memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
1279 } /* Init_Server_Struct */