From 474442dd564cab52d7732403eba5cd630764ba26 Mon Sep 17 00:00:00 2001
From: Steve Holme <steve_holme@hotmail.com>
Date: Thu, 9 Oct 2014 11:03:14 +0100
Subject: [PATCH] ntlm: Moved SSPI clean-up code into SASL module

---
 lib/curl_ntlm.c      |  5 +++--
 lib/curl_ntlm_msgs.c | 35 +++--------------------------------
 lib/curl_ntlm_msgs.h |  8 --------
 lib/curl_sasl.c      |  2 +-
 lib/curl_sasl.h      |  5 +++++
 lib/curl_sasl_sspi.c | 40 ++++++++++++++++++++++++++++++++++++++++
 6 files changed, 52 insertions(+), 43 deletions(-)

diff --git a/lib/curl_ntlm.c b/lib/curl_ntlm.c
index a792190eac..a8f8d5326f 100644
--- a/lib/curl_ntlm.c
+++ b/lib/curl_ntlm.c
@@ -39,6 +39,7 @@
 #include "curl_ntlm.h"
 #include "curl_ntlm_msgs.h"
 #include "curl_ntlm_wb.h"
+#include "curl_sasl.h"
 #include "url.h"
 #include "curl_memory.h"
 
@@ -229,8 +230,8 @@ CURLcode Curl_output_ntlm(struct connectdata *conn, bool proxy)
 void Curl_http_ntlm_cleanup(struct connectdata *conn)
 {
 #ifdef USE_WINDOWS_SSPI
-  Curl_ntlm_sspi_cleanup(&conn->ntlm);
-  Curl_ntlm_sspi_cleanup(&conn->proxyntlm);
+  Curl_sasl_ntlm_cleanup(&conn->ntlm);
+  Curl_sasl_ntlm_cleanup(&conn->proxyntlm);
 #elif defined(NTLM_WB_ENABLED)
   Curl_ntlm_wb_cleanup(conn);
 #else
diff --git a/lib/curl_ntlm_msgs.c b/lib/curl_ntlm_msgs.c
index f8172b51bc..ae433af015 100644
--- a/lib/curl_ntlm_msgs.c
+++ b/lib/curl_ntlm_msgs.c
@@ -51,6 +51,7 @@
 
 #define BUILDING_CURL_NTLM_MSGS_C
 #include "curl_ntlm_msgs.h"
+#include "curl_sasl.h"
 
 #define _MPRINTF_REPLACE /* use our functions only */
 #include <curl/mprintf.h>
@@ -338,36 +339,6 @@ CURLcode Curl_ntlm_decode_type2_message(struct SessionHandle *data,
   return CURLE_OK;
 }
 
-#ifdef USE_WINDOWS_SSPI
-void Curl_ntlm_sspi_cleanup(struct ntlmdata *ntlm)
-{
-  /* Free our security context */
-  if(ntlm->context) {
-    s_pSecFn->DeleteSecurityContext(ntlm->context);
-    free(ntlm->context);
-    ntlm->context = NULL;
-  }
-
-  /* Free our credentials handle */
-  if(ntlm->credentials) {
-    s_pSecFn->FreeCredentialsHandle(ntlm->credentials);
-    free(ntlm->credentials);
-    ntlm->credentials = NULL;
-  }
-
-  /* Free our identity */
-  Curl_sspi_free_identity(ntlm->p_identity);
-  ntlm->p_identity = NULL;
-
-  /* Free the input and output tokens */
-  Curl_safefree(ntlm->input_token);
-  Curl_safefree(ntlm->output_token);
-
-  /* Reset any variables */
-  ntlm->token_max = 0;
-}
-#endif
-
 #ifndef USE_WINDOWS_SSPI
 /* copy the source to the destination and fill in zeroes in every
    other destination byte! */
@@ -430,7 +401,7 @@ CURLcode Curl_ntlm_create_type1_message(const char *userp,
   unsigned long attrs;
   TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
 
-  Curl_ntlm_sspi_cleanup(ntlm);
+  Curl_sasl_ntlm_cleanup(ntlm);
 
   /* Query the security package for NTLM */
   status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *) TEXT(SP_NAME_NTLM),
@@ -695,7 +666,7 @@ CURLcode Curl_ntlm_create_type3_message(struct SessionHandle *data,
   result = Curl_base64_encode(NULL, (char *)ntlm->output_token, size,
                               outptr, outlen);
 
-  Curl_ntlm_sspi_cleanup(ntlm);
+  Curl_sasl_ntlm_cleanup(ntlm);
 
   return result;
 
diff --git a/lib/curl_ntlm_msgs.h b/lib/curl_ntlm_msgs.h
index 80413c885a..2c97331b05 100644
--- a/lib/curl_ntlm_msgs.h
+++ b/lib/curl_ntlm_msgs.h
@@ -52,14 +52,6 @@ CURLcode Curl_ntlm_decode_type2_target(struct SessionHandle *data,
                                        size_t size,
                                        struct ntlmdata* ntlm);
 
-
-/* This is to clean up the ntlm data structure */
-#ifdef USE_WINDOWS_SSPI
-void Curl_ntlm_sspi_cleanup(struct ntlmdata *ntlm);
-#else
-#define Curl_ntlm_sspi_cleanup(x)
-#endif
-
 /* NTLM buffer fixed size, large enough for long user + host + domain */
 #define NTLM_BUFSIZE 1024
 
diff --git a/lib/curl_sasl.c b/lib/curl_sasl.c
index 1edeb20176..e50d3d9d5d 100644
--- a/lib/curl_sasl.c
+++ b/lib/curl_sasl.c
@@ -1243,7 +1243,7 @@ void Curl_sasl_cleanup(struct connectdata *conn, unsigned int authused)
 #ifdef USE_NTLM
   /* Cleanup the ntlm structure */
   else if(authused == SASL_MECH_NTLM) {
-    Curl_ntlm_sspi_cleanup(&conn->ntlm);
+    Curl_sasl_ntlm_cleanup(&conn->ntlm);
   }
 #endif
 #else
diff --git a/lib/curl_sasl.h b/lib/curl_sasl.h
index d5a6ff38cb..88a9d9364c 100644
--- a/lib/curl_sasl.h
+++ b/lib/curl_sasl.h
@@ -149,6 +149,11 @@ CURLcode Curl_sasl_create_ntlm_type3_message(struct SessionHandle *data,
                                              struct ntlmdata *ntlm,
                                              char **outptr, size_t *outlen);
 
+#if defined(USE_WINDOWS_SSPI)
+/* This is used to clean up the ntlm specific data */
+void Curl_sasl_ntlm_cleanup(struct ntlmdata *ntlm);
+#endif
+
 #endif /* USE_NTLM */
 
 #if defined(USE_KRB5)
diff --git a/lib/curl_sasl_sspi.c b/lib/curl_sasl_sspi.c
index 44f909f21a..de247cc2cf 100644
--- a/lib/curl_sasl_sspi.c
+++ b/lib/curl_sasl_sspi.c
@@ -485,6 +485,46 @@ void Curl_sasl_digest_cleanup(struct digestdata *digest)
 }
 #endif /* !CURL_DISABLE_CRYPTO_AUTH */
 
+#if defined USE_NTLM
+/*
+ * Curl_sasl_ntlm_cleanup()
+ *
+ * This is used to clean up the ntlm specific data.
+ *
+ * Parameters:
+ *
+ * ntlm    [in/out] - The ntlm data struct being cleaned up.
+ *
+ */
+void Curl_sasl_ntlm_cleanup(struct ntlmdata *ntlm)
+{
+  /* Free our security context */
+  if(ntlm->context) {
+    s_pSecFn->DeleteSecurityContext(ntlm->context);
+    free(ntlm->context);
+    ntlm->context = NULL;
+  }
+
+  /* Free our credentials handle */
+  if(ntlm->credentials) {
+    s_pSecFn->FreeCredentialsHandle(ntlm->credentials);
+    free(ntlm->credentials);
+    ntlm->credentials = NULL;
+  }
+
+  /* Free our identity */
+  Curl_sspi_free_identity(ntlm->p_identity);
+  ntlm->p_identity = NULL;
+
+  /* Free the input and output tokens */
+  Curl_safefree(ntlm->input_token);
+  Curl_safefree(ntlm->output_token);
+
+  /* Reset any variables */
+  ntlm->token_max = 0;
+}
+#endif /* USE_NTLM */
+
 #if defined(USE_KRB5)
 /*
  * Curl_sasl_create_gssapi_user_message()
-- 
GitLab