From 900f06d038b3879e4e254b4517b0b6f6ffdb17e4 Mon Sep 17 00:00:00 2001
From: Fabian Keil <fk@fabiankeil.de>
Date: Thu, 10 Sep 2020 12:42:15 +0200
Subject: [PATCH] Check the socks user name and password when comparing
 forwarding settings

---
 gateway.c | 20 ++++++++++++++++++++
 jcc.c     | 16 ++++++++++++++++
 project.h |  3 +++
 3 files changed, 39 insertions(+)

diff --git a/gateway.c b/gateway.c
index f1de5380..66ec64ae 100644
--- a/gateway.c
+++ b/gateway.c
@@ -287,6 +287,8 @@ void mark_connection_closed(struct reusable_connection *closed_connection)
    closed_connection->forwarder_type = SOCKS_NONE;
    freez(closed_connection->gateway_host);
    closed_connection->gateway_port = 0;
+   freez(closed_connection->auth_username);
+   freez(closed_connection->auth_password);
    freez(closed_connection->forward_host);
    closed_connection->forward_port = 0;
 }
@@ -409,6 +411,24 @@ int connection_destination_matches(const struct reusable_connection *connection,
       return FALSE;
    }
 
+   if (!connection_detail_matches(connection->auth_username, fwd->auth_username))
+   {
+      log_error(LOG_LEVEL_CONNECT, "Socks user name mismatch. "
+         "Previous user name: %s. Current user name: %s",
+         connection->auth_username != NULL ? connection->auth_username : "none",
+         fwd->auth_username != NULL ? fwd->auth_username : "none");
+      return FALSE;
+   }
+
+   if (!connection_detail_matches(connection->auth_password, fwd->auth_password))
+   {
+      log_error(LOG_LEVEL_CONNECT, "Socks user name mismatch. "
+         "Previous password: %s. Current password: %s",
+         connection->auth_password != NULL ? connection->auth_password : "none",
+         fwd->auth_password != NULL ?  fwd->auth_password : "none");
+      return FALSE;
+   }
+
    if (!connection_detail_matches(connection->forward_host, fwd->forward_host))
    {
       log_error(LOG_LEVEL_CONNECT,
diff --git a/jcc.c b/jcc.c
index 581768f7..49a6b108 100644
--- a/jcc.c
+++ b/jcc.c
@@ -1176,6 +1176,22 @@ void save_connection_destination(jb_socket sfd,
       server_connection->gateway_host = NULL;
    }
    server_connection->gateway_port = fwd->gateway_port;
+   if (NULL != fwd->auth_username)
+   {
+      server_connection->auth_username = strdup_or_die(fwd->auth_username);
+   }
+   else
+   {
+      server_connection->auth_username = NULL;
+   }
+   if (NULL != fwd->auth_password)
+   {
+      server_connection->auth_password = strdup_or_die(fwd->auth_password);
+   }
+   else
+   {
+      server_connection->auth_password = NULL;
+   }
 
    if (NULL != fwd->forward_host)
    {
diff --git a/project.h b/project.h
index 6821aa03..a9fe8946 100644
--- a/project.h
+++ b/project.h
@@ -766,6 +766,9 @@ struct reusable_connection
    enum forwarder_type forwarder_type;
    char *gateway_host;
    int  gateway_port;
+   char *auth_username;
+   char *auth_password;
+
    char *forward_host;
    int  forward_port;
 };
-- 
2.49.0