summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlexander Barton <alex@barton.de>2012-10-28 19:48:24 +0100
committerAlexander Barton <alex@barton.de>2012-10-29 11:33:49 +0100
commiteb4f9eac0c35071838c9367f1204db0d0b98ad2e (patch)
tree74621ec97623d67f7bb865929a33d9153af5a112
parentd7b5dd1bbf44ff9c8537def6d2083891b4f7d102 (diff)
downloadngircd-eb4f9eac0c35071838c9367f1204db0d0b98ad2e.tar.gz
ngircd-eb4f9eac0c35071838c9367f1204db0d0b98ad2e.zip
Don't accept connections for servers already beeing linked
If two servers try to link each other, there was a time frame that
could result in one connection overwriting the other, e. g. the incoming
connection overwriting the status of the outgoing one. And this could
lead to all kind of weirdness (even crashes!) later on.

So now such incoming connections are dropped. But this most probably
prevents the two servers from linking until timing changes somehow
(network latency?) because each server drops the incoming connection of
the other one, so no connection survives in the end.

But this has to be addressed by an other patch ...
-rw-r--r--src/ngircd/conf.c14
-rw-r--r--src/ngircd/conf.h2
-rw-r--r--src/ngircd/conn.c3
-rw-r--r--src/ngircd/irc-server.c7
4 files changed, 15 insertions, 11 deletions
diff --git a/src/ngircd/conf.c b/src/ngircd/conf.c
index b6005707..6a7f6339 100644
--- a/src/ngircd/conf.c
+++ b/src/ngircd/conf.c
@@ -490,7 +490,7 @@ Conf_UnsetServer( CONN_ID Idx )
 /**
  * Set connection information for specified configured server.
  */
-GLOBAL void
+GLOBAL bool
 Conf_SetServer( int ConfServer, CONN_ID Idx )
 {
 	assert( ConfServer > NONE );
@@ -498,13 +498,15 @@ Conf_SetServer( int ConfServer, CONN_ID Idx )
 
 	if (Conf_Server[ConfServer].conn_id > NONE &&
 	    Conf_Server[ConfServer].conn_id != Idx) {
-		Log(LOG_ALERT,
-			"Trying to update connection index for already registered server \"%s\": %d/%d - ignored.",
-			Conf_Server[ConfServer].name,
-			Conf_Server[ConfServer].conn_id, Idx);
-		return;
+		Log(LOG_ERR,
+		    "Connection %d: Server configuration of \"%s\" already in use by connection %d!",
+		    Idx, Conf_Server[ConfServer].name,
+		    Conf_Server[ConfServer].conn_id);
+		Conn_Close(Idx, NULL, "Server configuration already in use", true);
+		return false;
 	}
 	Conf_Server[ConfServer].conn_id = Idx;
+	return true;
 }
 
 /**
diff --git a/src/ngircd/conf.h b/src/ngircd/conf.h
index 541fdb29..7860f019 100644
--- a/src/ngircd/conf.h
+++ b/src/ngircd/conf.h
@@ -242,7 +242,7 @@ GLOBAL bool Conf_Rehash PARAMS((void));
 GLOBAL int Conf_Test PARAMS((void));
 
 GLOBAL void Conf_UnsetServer PARAMS(( CONN_ID Idx ));
-GLOBAL void Conf_SetServer PARAMS(( int ConfServer, CONN_ID Idx ));
+GLOBAL bool Conf_SetServer PARAMS(( int ConfServer, CONN_ID Idx ));
 GLOBAL int Conf_GetServer PARAMS(( CONN_ID Idx ));
 
 GLOBAL bool Conf_EnableServer PARAMS(( const char *Name, UINT16 Port ));
diff --git a/src/ngircd/conn.c b/src/ngircd/conn.c
index 5d086857..80b085a8 100644
--- a/src/ngircd/conn.c
+++ b/src/ngircd/conn.c
@@ -2079,7 +2079,8 @@ New_Server( int Server , ng_ipaddr_t *dest)
 	Client_SetToken( c, TOKEN_OUTBOUND );
 
 	/* Register connection */
-	Conf_SetServer(Server, new_sock);
+	if (!Conf_SetServer(Server, new_sock))
+		return;
 	My_Connections[new_sock].sock = new_sock;
 	My_Connections[new_sock].addr = *dest;
 	My_Connections[new_sock].client = c;
diff --git a/src/ngircd/irc-server.c b/src/ngircd/irc-server.c
index 79facf5e..02e3ae82 100644
--- a/src/ngircd/irc-server.c
+++ b/src/ngircd/irc-server.c
@@ -104,6 +104,10 @@ IRC_SERVER( CLIENT *Client, REQUEST *Req )
 		if (!Client_CheckID(Client, Req->argv[0]))
 			return DISCONNECTED;
 
+		/* Mark this connection as belonging to an configured server */
+		if (!Conf_SetServer(i, Client_Conn(Client)))
+			return DISCONNECTED;
+
 		Client_SetID( Client, Req->argv[0] );
 		Client_SetHops( Client, 1 );
 		Client_SetInfo( Client, Req->argv[Req->argc - 1] );
@@ -131,9 +135,6 @@ IRC_SERVER( CLIENT *Client, REQUEST *Req )
 			Client_SetToken(Client, atoi(Req->argv[1]));
 		}
 
-		/* Mark this connection as belonging to an configured server */
-		Conf_SetServer(i, Client_Conn(Client));
-
 		/* Check protocol level */
 		if (Client_Type(Client) == CLIENT_GOTPASS) {
 			/* We got a "simple" PASS command, so the peer is