From 3e1347d378e7ff0e21fb53b61e0317b8dfe52fc9 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Sun, 8 Sep 2019 20:15:32 +0200
Subject: Used the SSL link with more care.

---
 src/analysis/binary.c    | 30 ++++++++++++++++++++--------
 src/analysis/db/client.c | 52 ++++++++++++++++++++++++++++++++++--------------
 2 files changed, 59 insertions(+), 23 deletions(-)

diff --git a/src/analysis/binary.c b/src/analysis/binary.c
index 5d72c41..070e175 100644
--- a/src/analysis/binary.c
+++ b/src/analysis/binary.c
@@ -1102,12 +1102,19 @@ bool _g_loaded_binary_add_to_collection(GLoadedBinary *binary, GDbItem *item, bo
 
             tls_fd = g_hub_client_get_ssl_fd(client);
 
-            result = g_db_collection_pack(collec, &out_pbuf, DBA_ADD_ITEM, item);
+            if (tls_fd == NULL)
+                result = false;
 
-            if (result)
-                result = ssl_send_packed_buffer(&out_pbuf, tls_fd);
+            else
+            {
+                result = g_db_collection_pack(collec, &out_pbuf, DBA_ADD_ITEM, item);
 
-            g_hub_client_put_ssl_fd(client, tls_fd);
+                if (result)
+                    result = ssl_send_packed_buffer(&out_pbuf, tls_fd);
+
+                g_hub_client_put_ssl_fd(client, tls_fd);
+
+            }
 
             exit_packed_buffer(&out_pbuf);
 
@@ -1171,12 +1178,19 @@ bool _g_loaded_binary_remove_from_collection(GLoadedBinary *binary, DBFeatures f
 
         tls_fd = g_hub_client_get_ssl_fd(client);
 
-        result = g_db_collection_pack(collec, &out_pbuf, DBA_REM_ITEM, item);
+        if (tls_fd == NULL)
+            result = false;
+
+        else
+        {
+            result = g_db_collection_pack(collec, &out_pbuf, DBA_REM_ITEM, item);
 
-        if (result)
-            result = ssl_send_packed_buffer(&out_pbuf, tls_fd);
+            if (result)
+                result = ssl_send_packed_buffer(&out_pbuf, tls_fd);
+
+            g_hub_client_put_ssl_fd(client, tls_fd);
 
-        g_hub_client_put_ssl_fd(client, tls_fd);
+        }
 
         exit_packed_buffer(&out_pbuf);
 
diff --git a/src/analysis/db/client.c b/src/analysis/db/client.c
index 329d57d..3197a8c 100644
--- a/src/analysis/db/client.c
+++ b/src/analysis/db/client.c
@@ -900,12 +900,18 @@ SSL *g_hub_client_get_ssl_fd(GHubClient *client)
 
     result = client->tls_fd;
 
+    if (result == NULL)
+        g_mutex_unlock(&client->sending_lock);
+
+    else
+    {
 #ifndef NDEBUG
-    ret = SSL_up_ref(result);
-    assert(ret == 1);
+        ret = SSL_up_ref(result);
+        assert(ret == 1);
 #else
-    SSL_up_ref(result);
+        SSL_up_ref(result);
 #endif
+    }
 
     return result;
 
@@ -950,17 +956,25 @@ bool g_hub_client_save(GHubClient *client)
 {
     bool result;                            /* Bilan partiel à remonter    */
     packed_buffer out_pbuf;                 /* Tampon d'émission           */
+    SSL *tls_fd;                            /* Canal de communication SSL  */
 
     init_packed_buffer(&out_pbuf);
 
-    g_hub_client_get_ssl_fd(client);
+    tls_fd = g_hub_client_get_ssl_fd(client);
 
-    result = extend_packed_buffer(&out_pbuf, (uint32_t []) { DBC_SAVE }, sizeof(uint32_t), true);
+    if (tls_fd == NULL)
+        result = false;
 
-    if (result)
-        result = ssl_send_packed_buffer(&out_pbuf, client->tls_fd);
+    else
+    {
+        result = extend_packed_buffer(&out_pbuf, (uint32_t []) { DBC_SAVE }, sizeof(uint32_t), true);
+
+        if (result)
+            result = ssl_send_packed_buffer(&out_pbuf, tls_fd);
 
-    g_hub_client_put_ssl_fd(client, client->tls_fd);
+        g_hub_client_put_ssl_fd(client, tls_fd);
+
+    }
 
     exit_packed_buffer(&out_pbuf);
 
@@ -986,20 +1000,28 @@ bool g_hub_client_set_last_active(GHubClient *client, timestamp_t timestamp)
 {
     bool result;                            /* Bilan partiel à remonter    */
     packed_buffer out_pbuf;                 /* Tampon d'émission           */
+    SSL *tls_fd;                            /* Canal de communication SSL  */
 
     init_packed_buffer(&out_pbuf);
 
-    g_hub_client_get_ssl_fd(client);
+    tls_fd = g_hub_client_get_ssl_fd(client);
+
+    if (tls_fd == NULL)
+        result = false;
 
-    result = extend_packed_buffer(&out_pbuf, (uint32_t []) { DBC_SET_LAST_ACTIVE }, sizeof(uint32_t), true);
+    else
+    {
+        result = extend_packed_buffer(&out_pbuf, (uint32_t []) { DBC_SET_LAST_ACTIVE }, sizeof(uint32_t), true);
 
-    if (result)
-        result = pack_timestamp(&timestamp, &out_pbuf);
+        if (result)
+            result = pack_timestamp(&timestamp, &out_pbuf);
 
-    if (result)
-        result = ssl_send_packed_buffer(&out_pbuf, client->tls_fd);
+        if (result)
+            result = ssl_send_packed_buffer(&out_pbuf, tls_fd);
 
-    g_hub_client_put_ssl_fd(client, client->tls_fd);
+        g_hub_client_put_ssl_fd(client, tls_fd);
+
+    }
 
     exit_packed_buffer(&out_pbuf);
 
-- 
cgit v0.11.2-87-g4458