Fixed DsParse implementations and tests

* Implemented DsMakeSpnW
* Removed unimplemented DsCrackSpnA and DsCrackSpnW
This commit is contained in:
Armin Novak 2021-07-29 11:04:58 +02:00 committed by akallabeth
parent 04ff6b8824
commit cbb06a7c17
6 changed files with 167 additions and 114 deletions

View File

@ -98,18 +98,10 @@ extern "C"
{
#endif
WINPR_API DWORD DsCrackSpnW(LPCWSTR pszSpn, DWORD* pcServiceClass, LPWSTR ServiceClass,
DWORD* pcServiceName, LPWSTR ServiceName, DWORD* pcInstanceName,
LPWSTR InstanceName, USHORT* pInstancePort);
WINPR_API DWORD DsCrackSpnA(LPCSTR pszSpn, LPDWORD pcServiceClass, LPSTR ServiceClass,
LPDWORD pcServiceName, LPSTR ServiceName, LPDWORD pcInstanceName,
LPSTR InstanceName, USHORT* pInstancePort);
#ifdef UNICODE
#define DsCrackSpn DsCrackSpnW
#define DsMakeSpn DsMakeSpnW
#else
#define DsCrackSpn DsCrackSpnA
#define DsMakeSpn DsMakeSpnA
#endif
WINPR_API DWORD DsMakeSpnW(LPCWSTR ServiceClass, LPCWSTR ServiceName, LPCWSTR InstanceName,

View File

@ -22,6 +22,7 @@
#endif
#include <winpr/dsparse.h>
#include <winpr/assert.h>
/**
* dsparse.dll:
@ -45,24 +46,66 @@
#if !defined(_WIN32) || defined(_UWP)
DWORD DsCrackSpnW(LPCWSTR pszSpn, DWORD* pcServiceClass, LPWSTR ServiceClass, DWORD* pcServiceName,
LPWSTR ServiceName, DWORD* pcInstanceName, LPWSTR InstanceName,
USHORT* pInstancePort)
{
return 0;
}
DWORD DsCrackSpnA(LPCSTR pszSpn, LPDWORD pcServiceClass, LPSTR ServiceClass, LPDWORD pcServiceName,
LPSTR ServiceName, LPDWORD pcInstanceName, LPSTR InstanceName,
USHORT* pInstancePort)
{
return 0;
}
DWORD DsMakeSpnW(LPCWSTR ServiceClass, LPCWSTR ServiceName, LPCWSTR InstanceName,
USHORT InstancePort, LPCWSTR Referrer, DWORD* pcSpnLength, LPWSTR pszSpn)
{
return 0;
DWORD res = ERROR_OUTOFMEMORY;
char* ServiceClassA = NULL;
char* ServiceNameA = NULL;
char* InstanceNameA = NULL;
char* ReferrerA = NULL;
char* pszSpnA = NULL;
DWORD length;
WINPR_ASSERT(ServiceClass);
WINPR_ASSERT(ServiceName);
WINPR_ASSERT(pcSpnLength);
length = *pcSpnLength;
if ((length > 0) && pszSpn)
pszSpnA = calloc(length + 1, sizeof(char));
if (ServiceClass)
{
int rc = ConvertFromUnicode(CP_UTF8, 0, ServiceClass, -1, &ServiceClassA, 0, NULL, NULL);
if (rc <= 0)
goto fail;
}
if (ServiceName)
{
int rc = ConvertFromUnicode(CP_UTF8, 0, ServiceName, -1, &ServiceNameA, 0, NULL, NULL);
if (rc <= 0)
goto fail;
}
if (InstanceName)
{
int rc = ConvertFromUnicode(CP_UTF8, 0, InstanceName, -1, &InstanceNameA, 0, NULL, NULL);
if (rc <= 0)
goto fail;
}
if (Referrer)
{
int rc = ConvertFromUnicode(CP_UTF8, 0, Referrer, -1, &ReferrerA, 0, NULL, NULL);
if (rc <= 0)
goto fail;
}
res = DsMakeSpnA(ServiceClassA, ServiceNameA, InstanceNameA, InstancePort, ReferrerA,
pcSpnLength, pszSpnA);
if (res == ERROR_SUCCESS)
{
int rc = ConvertToUnicode(CP_UTF8, 0, pszSpnA, *pcSpnLength, &pszSpn, length);
if (rc <= 0)
res = ERROR_OUTOFMEMORY;
}
fail:
free(ServiceClassA);
free(ServiceNameA);
free(InstanceNameA);
free(ReferrerA);
free(pszSpnA);
return res;
}
DWORD DsMakeSpnA(LPCSTR ServiceClass, LPCSTR ServiceName, LPCSTR InstanceName, USHORT InstancePort,
@ -72,6 +115,14 @@ DWORD DsMakeSpnA(LPCSTR ServiceClass, LPCSTR ServiceName, LPCSTR InstanceName, U
DWORD ServiceClassLength;
DWORD ServiceNameLength;
WINPR_ASSERT(ServiceClass);
WINPR_ASSERT(ServiceName);
WINPR_ASSERT(pcSpnLength);
WINPR_UNUSED(InstanceName);
WINPR_UNUSED(InstancePort);
WINPR_UNUSED(Referrer);
if ((*pcSpnLength != 0) && (pszSpn == NULL))
return ERROR_INVALID_PARAMETER;

View File

@ -1,4 +0,0 @@
TestDs
TestDs.c
TestDsParse
TestDsParse.c

View File

@ -5,8 +5,7 @@ set(MODULE_PREFIX "TEST_DSPARSE")
set(${MODULE_PREFIX}_DRIVER ${MODULE_NAME}.c)
set(${MODULE_PREFIX}_TESTS
TestDsMakeSpn.c
TestDsCrackNames.c)
TestDsMakeSpn.c)
create_test_sourcelist(${MODULE_PREFIX}_SRCS
${${MODULE_PREFIX}_DRIVER}

View File

@ -1,49 +0,0 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/winpr.h>
#include <winpr/tchar.h>
#include <winpr/dsparse.h>
// LPCTSTR testName = _T("LAB1\\JohnDoe");
int TestDsCrackNames(int argc, char* argv[])
{
#if 0
HANDLE ds;
DWORD status;
PDS_NAME_RESULT pResult;
status = DsBind(NULL, NULL, &ds);
if (status != ERROR_SUCCESS)
{
_tprintf(_T("DsBind: expected ERROR_SUCCESS: 0x%08")_T(PRIX32)_T("\n"), status);
return -1;
}
status = DsCrackNames(ds, DS_NAME_FLAG_SYNTACTICAL_ONLY, DS_NT4_ACCOUNT_NAME,
DS_USER_PRINCIPAL_NAME, 1, &testName, &pResult);
if (status != ERROR_SUCCESS)
{
_tprintf(_T("DsCrackNames: expected ERROR_SUCCESS\n"));
return -1;
}
_tprintf(_T("DsCrackNames: pResult->cItems: %")_T(PRIu32)_T("\n"), pResult->cItems);
_tprintf(_T("DsCrackNames: pResult->rItems[0]: Domain: %s Name: %s Status: 0x%08")_T(PRIX32)_T("\n"),
pResult->rItems[0].pDomain, pResult->rItems[0].pName, pResult->rItems[0].status);
status = DsUnBind(&ds);
if (status != ERROR_SUCCESS)
{
_tprintf(_T("DsUnBind: expected ERROR_SUCCESS\n"));
return -1;
}
#endif
return 0;
}

View File

@ -5,71 +5,135 @@
#include <winpr/tchar.h>
#include <winpr/dsparse.h>
static LPCTSTR testServiceClass = _T("HTTP");
static LPCTSTR testServiceName = _T("LAB1-W2K8R2-GW.lab1.awake.local");
static LPCTSTR testSpn = _T("HTTP/LAB1-W2K8R2-GW.lab1.awake.local");
int TestDsMakeSpn(int argc, char* argv[])
static BOOL test_DsMakeSpnA(void)
{
int rc = -1;
LPTSTR Spn = NULL;
LPCSTR testServiceClass = "HTTP";
LPCSTR testServiceName = "LAB1-W2K8R2-GW.lab1.awake.local";
LPCSTR testSpn = "HTTP/LAB1-W2K8R2-GW.lab1.awake.local";
BOOL rc = FALSE;
CHAR Spn[100] = { 0 };
DWORD status;
DWORD SpnLength;
SpnLength = -1;
DWORD SpnLength = -1;
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
status = DsMakeSpn(testServiceClass, testServiceName, NULL, 0, NULL, &SpnLength, NULL);
status = DsMakeSpnA(testServiceClass, testServiceName, NULL, 0, NULL, &SpnLength, NULL);
if (status != ERROR_INVALID_PARAMETER)
{
_tprintf(_T("DsMakeSpn: expected ERROR_INVALID_PARAMETER\n"));
printf("DsMakeSpnA: expected ERROR_INVALID_PARAMETER\n");
goto fail;
}
SpnLength = 0;
status = DsMakeSpn(testServiceClass, testServiceName, NULL, 0, NULL, &SpnLength, NULL);
status = DsMakeSpnA(testServiceClass, testServiceName, NULL, 0, NULL, &SpnLength, NULL);
if (status != ERROR_BUFFER_OVERFLOW)
{
_tprintf(_T("DsMakeSpn: expected ERROR_BUFFER_OVERFLOW\n"));
printf("DsMakeSpnA: expected ERROR_BUFFER_OVERFLOW\n");
goto fail;
}
if (SpnLength != 37)
{
_tprintf(_T("DsMakeSpn: SpnLength mismatch: Actual: %") _T(PRIu32) _T(", Expected: 37\n"),
SpnLength);
printf("DsMakeSpnA: SpnLength mismatch: Actual: %" PRIu32 ", Expected: 37\n", SpnLength);
goto fail;
}
/* SpnLength includes null terminator */
Spn = (LPTSTR)calloc(SpnLength, sizeof(TCHAR));
if (!Spn)
{
_tprintf(_T("DsMakeSpn: Unable to allocate memroy\n"));
goto fail;
}
status = DsMakeSpn(testServiceClass, testServiceName, NULL, 0, NULL, &SpnLength, Spn);
status = DsMakeSpnA(testServiceClass, testServiceName, NULL, 0, NULL, &SpnLength, Spn);
if (status != ERROR_SUCCESS)
{
_tprintf(_T("DsMakeSpn: expected ERROR_SUCCESS\n"));
printf("DsMakeSpnA: expected ERROR_SUCCESS\n");
goto fail;
}
if (_tcscmp(Spn, testSpn) != 0)
if (strcmp(Spn, testSpn) != 0)
{
_tprintf(_T("DsMakeSpn: SPN mismatch: Actual: %s, Expected: %s\n"), Spn, testSpn);
printf("DsMakeSpnA: SPN mismatch: Actual: %s, Expected: %s\n", Spn, testSpn);
goto fail;
}
_tprintf(_T("DsMakeSpn: %s\n"), Spn);
rc = 0;
printf("DsMakeSpnA: %s\n", Spn);
rc = TRUE;
fail:
free(Spn);
return rc;
}
static BOOL test_DsMakeSpnW(void)
{
WCHAR testServiceClass[] = { 'H', 'T', 'T', 'P', '\0' };
WCHAR testServiceName[] = { 'L', 'A', 'B', '1', '-', 'W', '2', 'K', '8', 'R', '2',
'-', 'G', 'W', '.', 'l', 'a', 'b', '1', '.', 'a', 'w',
'a', 'k', 'e', '.', 'l', 'o', 'c', 'a', 'l', '\0' };
WCHAR testSpn[] = { 'H', 'T', 'T', 'P', '/', 'L', 'A', 'B', '1', '-', 'W', '2', 'K',
'8', 'R', '2', '-', 'G', 'W', '.', 'l', 'a', 'b', '1', '.', 'a',
'w', 'a', 'k', 'e', '.', 'l', 'o', 'c', 'a', 'l', '\0' };
BOOL rc = FALSE;
WCHAR Spn[100] = { 0 };
DWORD status;
DWORD SpnLength = -1;
status = DsMakeSpnW(testServiceClass, testServiceName, NULL, 0, NULL, &SpnLength, NULL);
if (status != ERROR_INVALID_PARAMETER)
{
printf("DsMakeSpnW: expected ERROR_INVALID_PARAMETER\n");
goto fail;
}
SpnLength = 0;
status = DsMakeSpnW(testServiceClass, testServiceName, NULL, 0, NULL, &SpnLength, NULL);
if (status != ERROR_BUFFER_OVERFLOW)
{
printf("DsMakeSpnW: expected ERROR_BUFFER_OVERFLOW\n");
goto fail;
}
if (SpnLength != 37)
{
printf("DsMakeSpnW: SpnLength mismatch: Actual: %" PRIu32 ", Expected: 37\n", SpnLength);
goto fail;
}
status = DsMakeSpnW(testServiceClass, testServiceName, NULL, 0, NULL, &SpnLength, Spn);
if (status != ERROR_SUCCESS)
{
printf("DsMakeSpnW: expected ERROR_SUCCESS\n");
goto fail;
}
if (_wcscmp(Spn, testSpn) != 0)
{
char buffer1[8192];
char buffer2[8192];
char* SpnA = buffer1;
char* testSpnA = buffer2;
ConvertFromUnicode(CP_UTF8, 0, Spn, -1, &SpnA, sizeof(SpnA), NULL, NULL);
ConvertFromUnicode(CP_UTF8, 0, testSpn, -1, &testSpnA, sizeof(testSpnA), NULL, NULL);
printf("DsMakeSpnW: SPN mismatch: Actual: %s, Expected: %s\n", SpnA, testSpnA);
goto fail;
}
{
char buffer[8192];
char* SpnA = buffer;
ConvertFromUnicode(CP_UTF8, 0, Spn, -1, &SpnA, sizeof(SpnA), NULL, NULL);
printf("DsMakeSpnW: %s\n", SpnA);
}
rc = TRUE;
fail:
return rc;
}
int TestDsMakeSpn(int argc, char* argv[])
{
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
if (!test_DsMakeSpnA())
return -1;
if (!test_DsMakeSpnW())
return -2;
return 0;
}