Blame SOURCES/Implement-missing-TSG-debug-functions.patch

074271
From ea20ba13ec764b30d6642cd570929e71e97d00f1 Mon Sep 17 00:00:00 2001
074271
From: akallabeth <akallabeth@posteo.net>
074271
Date: Tue, 12 Oct 2021 13:33:41 +0200
074271
Subject: [PATCH] Implemented missing TSG debug functions
074271
074271
(cherry picked from commit c06c4638061b0305a269dbb8f7c87009cf886f02)
074271
---
074271
 libfreerdp/core/gateway/tsg.c | 330 ++++++++++++++++++++++++++++++++--
074271
 1 file changed, 313 insertions(+), 17 deletions(-)
074271
074271
diff --git a/libfreerdp/core/gateway/tsg.c b/libfreerdp/core/gateway/tsg.c
074271
index 1626b91..3376fb0 100644
074271
--- a/libfreerdp/core/gateway/tsg.c
074271
+++ b/libfreerdp/core/gateway/tsg.c
074271
@@ -24,6 +24,7 @@
074271
 #include "config.h"
074271
 #endif
074271
 
074271
+#include <winpr/assert.h>
074271
 #include <winpr/crt.h>
074271
 #include <winpr/ndr.h>
074271
 #include <winpr/error.h>
074271
@@ -39,13 +40,14 @@
074271
 
074271
 #define TAG FREERDP_TAG("core.gateway.tsg")
074271
 
074271
+#define TSG_CAPABILITY_TYPE_NAP 0x00000001
074271
+
074271
 #define TSG_PACKET_TYPE_HEADER 0x00004844
074271
 #define TSG_PACKET_TYPE_VERSIONCAPS 0x00005643
074271
 #define TSG_PACKET_TYPE_QUARCONFIGREQUEST 0x00005143
074271
 #define TSG_PACKET_TYPE_QUARREQUEST 0x00005152
074271
 #define TSG_PACKET_TYPE_RESPONSE 0x00005052
074271
 #define TSG_PACKET_TYPE_QUARENC_RESPONSE 0x00004552
074271
-#define TSG_CAPABILITY_TYPE_NAP 0x00000001
074271
 #define TSG_PACKET_TYPE_CAPS_RESPONSE 0x00004350
074271
 #define TSG_PACKET_TYPE_MSGREQUEST_PACKET 0x00004752
074271
 #define TSG_PACKET_TYPE_MESSAGE_PACKET 0x00004750
074271
@@ -308,16 +310,35 @@ static BOOL tsg_print(char** buffer, size_t* len, const char* fmt, ...)
074271
 static BOOL tsg_packet_header_to_string(char** buffer, size_t* length,
074271
                                         const TSG_PACKET_HEADER* header)
074271
 {
074271
+	WINPR_ASSERT(buffer);
074271
+	WINPR_ASSERT(length);
074271
+	WINPR_ASSERT(header);
074271
+
074271
 	return tsg_print(buffer, length,
074271
 	                 "header { ComponentId=0x%04" PRIx16 ", PacketId=0x%04" PRIx16 " }",
074271
 	                 header->ComponentId, header->PacketId);
074271
 }
074271
 
074271
+static BOOL tsg_type_capability_nap_to_string(char** buffer, size_t* length,
074271
+                                              const TSG_CAPABILITY_NAP* cur)
074271
+{
074271
+	WINPR_ASSERT(buffer);
074271
+	WINPR_ASSERT(length);
074271
+	WINPR_ASSERT(cur);
074271
+
074271
+	return tsg_print(buffer, length, "%s { capabilities=0x%08" PRIx32 " }",
074271
+	                 tsg_packet_id_to_string(TSG_CAPABILITY_TYPE_NAP), cur->capabilities);
074271
+}
074271
+
074271
 static BOOL tsg_packet_capabilities_to_string(char** buffer, size_t* length,
074271
                                               const TSG_PACKET_CAPABILITIES* caps, UINT32 numCaps)
074271
 {
074271
 	UINT32 x;
074271
 
074271
+	WINPR_ASSERT(buffer);
074271
+	WINPR_ASSERT(length);
074271
+	WINPR_ASSERT(caps);
074271
+
074271
 	if (!tsg_print(buffer, length, "capabilities { "))
074271
 		return FALSE;
074271
 
074271
@@ -327,9 +348,7 @@ static BOOL tsg_packet_capabilities_to_string(char** buffer, size_t* length,
074271
 		switch (cur->capabilityType)
074271
 		{
074271
 			case TSG_CAPABILITY_TYPE_NAP:
074271
-				if (!tsg_print(buffer, length, "%s { capabilities=0x%08" PRIx32 " }",
074271
-				               tsg_packet_id_to_string(cur->capabilityType),
074271
-				               cur->tsgPacket.tsgCapNap.capabilities))
074271
+				if (!tsg_type_capability_nap_to_string(buffer, length, &cur->tsgPacket.tsgCapNap))
074271
 					return FALSE;
074271
 				break;
074271
 			default:
074271
@@ -344,6 +363,10 @@ static BOOL tsg_packet_capabilities_to_string(char** buffer, size_t* length,
074271
 static BOOL tsg_packet_versioncaps_to_string(char** buffer, size_t* length,
074271
                                              const TSG_PACKET_VERSIONCAPS* caps)
074271
 {
074271
+	WINPR_ASSERT(buffer);
074271
+	WINPR_ASSERT(length);
074271
+	WINPR_ASSERT(caps);
074271
+
074271
 	if (!tsg_print(buffer, length, "versioncaps { "))
074271
 		return FALSE;
074271
 	if (!tsg_packet_header_to_string(buffer, length, &caps->tsgHeader))
074271
@@ -365,6 +388,277 @@ static BOOL tsg_packet_versioncaps_to_string(char** buffer, size_t* length,
074271
 	return tsg_print(buffer, length, " }");
074271
 }
074271
 
074271
+static BOOL tsg_packet_quarconfigrequest_to_string(char** buffer, size_t* length,
074271
+                                                   const TSG_PACKET_QUARCONFIGREQUEST* caps)
074271
+{
074271
+	WINPR_ASSERT(buffer);
074271
+	WINPR_ASSERT(length);
074271
+	WINPR_ASSERT(caps);
074271
+
074271
+	if (!tsg_print(buffer, length, "quarconfigrequest { "))
074271
+		return FALSE;
074271
+
074271
+	if (!tsg_print(buffer, length, " "))
074271
+		return FALSE;
074271
+
074271
+	if (!tsg_print(buffer, length, " flags=0x%08" PRIx32, caps->flags))
074271
+		return FALSE;
074271
+
074271
+	return tsg_print(buffer, length, " }");
074271
+}
074271
+
074271
+static BOOL tsg_packet_quarrequest_to_string(char** buffer, size_t* length,
074271
+                                             const TSG_PACKET_QUARREQUEST* caps)
074271
+{
074271
+	BOOL rc = FALSE;
074271
+	char* name = NULL;
074271
+	char* strdata = NULL;
074271
+
074271
+	WINPR_ASSERT(buffer);
074271
+	WINPR_ASSERT(length);
074271
+	WINPR_ASSERT(caps);
074271
+
074271
+	if (!tsg_print(buffer, length, "quarrequest { "))
074271
+		return FALSE;
074271
+
074271
+	if (!tsg_print(buffer, length, " "))
074271
+		return FALSE;
074271
+
074271
+	if (caps->nameLength > 0)
074271
+	{
074271
+		if (ConvertFromUnicode(CP_UTF8, 0, caps->machineName, caps->nameLength, &name, 0, NULL,
074271
+		                       NULL) < 0)
074271
+			return FALSE;
074271
+	}
074271
+
074271
+	strdata = winpr_BinToHexString(caps->data, caps->dataLen, TRUE);
074271
+	if (strdata || (caps->dataLen == 0))
074271
+		rc = tsg_print(buffer, length,
074271
+		               " flags=0x%08" PRIx32 ", machineName=%s [%" PRIu32 "], data[%" PRIu32 "]=%s",
074271
+		               caps->flags, name, caps->nameLength, caps->dataLen, strdata);
074271
+	free(name);
074271
+	free(strdata);
074271
+	if (!rc)
074271
+		return FALSE;
074271
+
074271
+	return tsg_print(buffer, length, " }");
074271
+}
074271
+
074271
+static const char* tsg_bool_to_string(BOOL val)
074271
+{
074271
+	if (val)
074271
+		return "true";
074271
+	return "false";
074271
+}
074271
+
074271
+static const char* tsg_redirection_flags_to_string(char* buffer, size_t size,
074271
+                                                   const TSG_REDIRECTION_FLAGS* flags)
074271
+{
074271
+	WINPR_ASSERT(buffer || (size == 0));
074271
+	WINPR_ASSERT(flags);
074271
+
074271
+	_snprintf(buffer, size,
074271
+	          "enableAllRedirections=%s,  disableAllRedirections=%s, driveRedirectionDisabled=%s, "
074271
+	          "printerRedirectionDisabled=%s, portRedirectionDisabled=%s, reserved=%s, "
074271
+	          "clipboardRedirectionDisabled=%s, pnpRedirectionDisabled=%s",
074271
+	          tsg_bool_to_string(flags->enableAllRedirections),
074271
+	          tsg_bool_to_string(flags->disableAllRedirections),
074271
+	          tsg_bool_to_string(flags->driveRedirectionDisabled),
074271
+	          tsg_bool_to_string(flags->printerRedirectionDisabled),
074271
+	          tsg_bool_to_string(flags->portRedirectionDisabled),
074271
+	          tsg_bool_to_string(flags->reserved),
074271
+	          tsg_bool_to_string(flags->clipboardRedirectionDisabled),
074271
+	          tsg_bool_to_string(flags->pnpRedirectionDisabled));
074271
+	return buffer;
074271
+}
074271
+
074271
+static BOOL tsg_packet_response_to_string(char** buffer, size_t* length,
074271
+                                          const TSG_PACKET_RESPONSE* caps)
074271
+{
074271
+	BOOL rc = FALSE;
074271
+	char* strdata = NULL;
074271
+	char tbuffer[8192] = { 0 };
074271
+
074271
+	WINPR_ASSERT(buffer);
074271
+	WINPR_ASSERT(length);
074271
+	WINPR_ASSERT(caps);
074271
+
074271
+	if (!tsg_print(buffer, length, "response { "))
074271
+		return FALSE;
074271
+
074271
+	if (!tsg_print(buffer, length, " "))
074271
+		return FALSE;
074271
+
074271
+	strdata = winpr_BinToHexString(caps->responseData, caps->responseDataLen, TRUE);
074271
+	if (strdata || (caps->responseDataLen == 0))
074271
+		rc = tsg_print(
074271
+		    buffer, length,
074271
+		    " flags=0x%08" PRIx32 ", reserved=0x%08" PRIx32 ", responseData[%" PRIu32
074271
+		    "]=%s, redirectionFlags={ %s }",
074271
+		    caps->flags, caps->reserved, caps->responseDataLen, strdata,
074271
+		    tsg_redirection_flags_to_string(tbuffer, ARRAYSIZE(tbuffer), &caps->redirectionFlags));
074271
+	free(strdata);
074271
+	if (!rc)
074271
+		return FALSE;
074271
+
074271
+	return tsg_print(buffer, length, " }");
074271
+}
074271
+
074271
+static BOOL tsg_packet_quarenc_response_to_string(char** buffer, size_t* length,
074271
+                                                  const TSG_PACKET_QUARENC_RESPONSE* caps)
074271
+{
074271
+	BOOL rc = FALSE;
074271
+	char* strdata = NULL;
074271
+	RPC_CSTR uuid;
074271
+	char tbuffer[8192] = { 0 };
074271
+	size_t size = ARRAYSIZE(tbuffer);
074271
+	char* ptbuffer = tbuffer;
074271
+
074271
+	WINPR_ASSERT(buffer);
074271
+	WINPR_ASSERT(length);
074271
+	WINPR_ASSERT(caps);
074271
+
074271
+	if (!tsg_print(buffer, length, "quarenc_response { "))
074271
+		return FALSE;
074271
+
074271
+	if (!tsg_print(buffer, length, " "))
074271
+		return FALSE;
074271
+
074271
+	if (caps->certChainLen > 0)
074271
+	{
074271
+		if (ConvertFromUnicode(CP_UTF8, 0, caps->certChainData, caps->certChainLen, &strdata, 0,
074271
+		                       NULL, NULL) <= 0)
074271
+			return FALSE;
074271
+	}
074271
+
074271
+	tsg_packet_versioncaps_to_string(&ptbuffer, &size, caps->versionCaps);
074271
+	UuidToStringA(&caps->nonce, &uuid);
074271
+	if (strdata || (caps->certChainLen == 0))
074271
+		rc =
074271
+		    tsg_print(buffer, length,
074271
+		              " flags=0x%08" PRIx32 ", certChain[%" PRIu32 "]=%s, nonce=%s, versionCaps=%s",
074271
+		              caps->flags, caps->certChainLen, strdata, uuid, tbuffer);
074271
+	free(strdata);
074271
+	free(uuid);
074271
+	if (!rc)
074271
+		return FALSE;
074271
+
074271
+	return tsg_print(buffer, length, " }");
074271
+}
074271
+
074271
+static BOOL tsg_packet_message_response_to_string(char** buffer, size_t* length,
074271
+                                                  const TSG_PACKET_MSG_RESPONSE* caps)
074271
+{
074271
+	WINPR_ASSERT(buffer);
074271
+	WINPR_ASSERT(length);
074271
+	WINPR_ASSERT(caps);
074271
+
074271
+	if (!tsg_print(buffer, length, "msg_response { "))
074271
+		return FALSE;
074271
+
074271
+	if (!tsg_print(buffer, length,
074271
+	               " msgID=0x%08" PRIx32 ", msgType=0x%08" PRIx32 ", isMsgPresent=%" PRId32,
074271
+	               caps->msgID, caps->msgType, caps->isMsgPresent))
074271
+		return FALSE;
074271
+
074271
+	return tsg_print(buffer, length, " }");
074271
+}
074271
+
074271
+static BOOL tsg_packet_caps_response_to_string(char** buffer, size_t* length,
074271
+                                               const TSG_PACKET_CAPS_RESPONSE* caps)
074271
+{
074271
+	WINPR_ASSERT(buffer);
074271
+	WINPR_ASSERT(length);
074271
+	WINPR_ASSERT(caps);
074271
+
074271
+	if (!tsg_print(buffer, length, "caps_response { "))
074271
+		return FALSE;
074271
+
074271
+	if (!tsg_packet_quarenc_response_to_string(buffer, length, &caps->pktQuarEncResponse))
074271
+		return FALSE;
074271
+
074271
+	if (!tsg_packet_message_response_to_string(buffer, length, &caps->pktConsentMessage))
074271
+		return FALSE;
074271
+
074271
+	return tsg_print(buffer, length, " }");
074271
+}
074271
+
074271
+static BOOL tsg_packet_message_request_to_string(char** buffer, size_t* length,
074271
+                                                 const TSG_PACKET_MSG_REQUEST* caps)
074271
+{
074271
+	WINPR_ASSERT(buffer);
074271
+	WINPR_ASSERT(length);
074271
+	WINPR_ASSERT(caps);
074271
+
074271
+	if (!tsg_print(buffer, length, "caps_message_request { "))
074271
+		return FALSE;
074271
+
074271
+	if (!tsg_print(buffer, length, " maxMessagesPerBatch=%" PRIu32, caps->maxMessagesPerBatch))
074271
+		return FALSE;
074271
+
074271
+	return tsg_print(buffer, length, " }");
074271
+}
074271
+
074271
+static BOOL tsg_packet_auth_to_string(char** buffer, size_t* length, const TSG_PACKET_AUTH* caps)
074271
+{
074271
+	BOOL rc = FALSE;
074271
+	char* strdata = NULL;
074271
+	WINPR_ASSERT(buffer);
074271
+	WINPR_ASSERT(length);
074271
+	WINPR_ASSERT(caps);
074271
+
074271
+	if (!tsg_print(buffer, length, "caps_message_request { "))
074271
+		return FALSE;
074271
+
074271
+	if (!tsg_packet_versioncaps_to_string(buffer, length, &caps->tsgVersionCaps))
074271
+		return FALSE;
074271
+
074271
+	strdata = winpr_BinToHexString(caps->cookie, caps->cookieLen, TRUE);
074271
+	if (strdata || (caps->cookieLen == 0))
074271
+		rc = tsg_print(buffer, length, " cookie[%" PRIu32 "]=%s", caps->cookieLen, strdata);
074271
+	free(strdata);
074271
+	if (!rc)
074271
+		return FALSE;
074271
+
074271
+	return tsg_print(buffer, length, " }");
074271
+}
074271
+
074271
+static BOOL tsg_packet_reauth_to_string(char** buffer, size_t* length,
074271
+                                        const TSG_PACKET_REAUTH* caps)
074271
+{
074271
+	BOOL rc = FALSE;
074271
+	WINPR_ASSERT(buffer);
074271
+	WINPR_ASSERT(length);
074271
+	WINPR_ASSERT(caps);
074271
+
074271
+	if (!tsg_print(buffer, length, "caps_message_request { "))
074271
+		return FALSE;
074271
+
074271
+	if (!tsg_print(buffer, length, " tunnelContext=0x%08" PRIx32 ", packetId=%s [0x%08" PRIx32 "]",
074271
+	               caps->tunnelContext, tsg_packet_id_to_string(caps->packetId), caps->packetId))
074271
+		return FALSE;
074271
+
074271
+	switch (caps->packetId)
074271
+	{
074271
+		case TSG_PACKET_TYPE_VERSIONCAPS:
074271
+			rc = tsg_packet_versioncaps_to_string(buffer, length,
074271
+			                                      caps->tsgInitialPacket.packetVersionCaps);
074271
+			break;
074271
+		case TSG_PACKET_TYPE_AUTH:
074271
+			rc = tsg_packet_auth_to_string(buffer, length, caps->tsgInitialPacket.packetAuth);
074271
+			break;
074271
+		default:
074271
+			rc = tsg_print(buffer, length, "TODO: Unhandled packet type %s [0x%08" PRIx32 "]",
074271
+			               tsg_packet_id_to_string(caps->packetId), caps->packetId);
074271
+			break;
074271
+	}
074271
+
074271
+	if (!rc)
074271
+		return FALSE;
074271
+
074271
+	return tsg_print(buffer, length, " }");
074271
+}
074271
+
074271
 static const char* tsg_packet_to_string(const TSG_PACKET* packet)
074271
 {
074271
 	size_t len = 8192;
074271
@@ -387,43 +681,45 @@ static const char* tsg_packet_to_string(const TSG_PACKET* packet)
074271
 				goto fail;
074271
 			break;
074271
 		case TSG_PACKET_TYPE_QUARCONFIGREQUEST:
074271
-			if (!tsg_print(&buffer, &len, "TODO"))
074271
+			if (!tsg_packet_quarconfigrequest_to_string(&buffer, &len,
074271
+			                                            packet->tsgPacket.packetQuarConfigRequest))
074271
 				goto fail;
074271
 			break;
074271
 		case TSG_PACKET_TYPE_QUARREQUEST:
074271
-			if (!tsg_print(&buffer, &len, "TODO"))
074271
+			if (!tsg_packet_quarrequest_to_string(&buffer, &len,
074271
+			                                      packet->tsgPacket.packetQuarRequest))
074271
 				goto fail;
074271
 			break;
074271
 		case TSG_PACKET_TYPE_RESPONSE:
074271
-			if (!tsg_print(&buffer, &len, "TODO"))
074271
+			if (!tsg_packet_response_to_string(&buffer, &len, packet->tsgPacket.packetResponse))
074271
 				goto fail;
074271
 			break;
074271
 		case TSG_PACKET_TYPE_QUARENC_RESPONSE:
074271
-			if (!tsg_print(&buffer, &len, "TODO"))
074271
-				goto fail;
074271
-			break;
074271
-		case TSG_CAPABILITY_TYPE_NAP:
074271
-			if (!tsg_print(&buffer, &len, "TODO"))
074271
+			if (!tsg_packet_quarenc_response_to_string(&buffer, &len,
074271
+			                                           packet->tsgPacket.packetQuarEncResponse))
074271
 				goto fail;
074271
 			break;
074271
 		case TSG_PACKET_TYPE_CAPS_RESPONSE:
074271
-			if (!tsg_print(&buffer, &len, "TODO"))
074271
+			if (!tsg_packet_caps_response_to_string(&buffer, &len,
074271
+			                                        packet->tsgPacket.packetCapsResponse))
074271
 				goto fail;
074271
 			break;
074271
 		case TSG_PACKET_TYPE_MSGREQUEST_PACKET:
074271
-			if (!tsg_print(&buffer, &len, "TODO"))
074271
+			if (!tsg_packet_message_request_to_string(&buffer, &len,
074271
+			                                          packet->tsgPacket.packetMsgRequest))
074271
 				goto fail;
074271
 			break;
074271
 		case TSG_PACKET_TYPE_MESSAGE_PACKET:
074271
-			if (!tsg_print(&buffer, &len, "TODO"))
074271
+			if (!tsg_packet_message_response_to_string(&buffer, &len,
074271
+			                                           packet->tsgPacket.packetMsgResponse))
074271
 				goto fail;
074271
 			break;
074271
 		case TSG_PACKET_TYPE_AUTH:
074271
-			if (!tsg_print(&buffer, &len, "TODO"))
074271
+			if (!tsg_packet_auth_to_string(&buffer, &len, packet->tsgPacket.packetAuth))
074271
 				goto fail;
074271
 			break;
074271
 		case TSG_PACKET_TYPE_REAUTH:
074271
-			if (!tsg_print(&buffer, &len, "TODO"))
074271
+			if (!tsg_packet_reauth_to_string(&buffer, &len, packet->tsgPacket.packetReauth))
074271
 				goto fail;
074271
 			break;
074271
 		default: