Cleaned up ntlm_fetch_ntlm_v2_hash
@@ -263,10 +263,11 @@ void ntlm_generate_timestamp(NTLM_CONTEXT* context)
ntlm_current_time(context->Timestamp);
}
-static int ntlm_fetch_ntlm_v2_hash(NTLM_CONTEXT* context, BYTE* hash)
+static BOOL ntlm_fetch_ntlm_v2_hash(NTLM_CONTEXT* context, BYTE* hash)
{
- WINPR_SAM* sam;
- WINPR_SAM_ENTRY* entry;
+ BOOL rc = FALSE;
+ WINPR_SAM* sam = NULL;
+ WINPR_SAM_ENTRY* entry = NULL;
SSPI_CREDENTIALS* credentials;
WINPR_ASSERT(context);
@@ -276,48 +277,39 @@ static int ntlm_fetch_ntlm_v2_hash(NTLM_CONTEXT* context, BYTE* hash)
sam = SamOpen(context->SamFile, TRUE);
if (!sam)
- return -1;
+ goto fail;
entry = SamLookupUserW(
- sam, (LPWSTR)credentials->identity.User, credentials->identity.UserLength * 2,
- (LPWSTR)credentials->identity.Domain, credentials->identity.DomainLength * 2);
+ sam, (LPWSTR)credentials->identity.User, credentials->identity.UserLength * sizeof(WCHAR),
+ (LPWSTR)credentials->identity.Domain, credentials->identity.DomainLength * sizeof(WCHAR));
- if (entry)
+ if (!entry)
{
-#ifdef WITH_DEBUG_NTLM
- WLog_VRB(TAG, "NTLM Hash:");
- winpr_HexDump(TAG, WLOG_DEBUG, entry->NtHash, 16);
-#endif
- NTOWFv2FromHashW(entry->NtHash, (LPWSTR)credentials->identity.User,
- credentials->identity.UserLength * 2, (LPWSTR)credentials->identity.Domain,
- credentials->identity.DomainLength * 2, (BYTE*)hash);
- SamFreeEntry(sam, entry);
- SamClose(sam);
- return 1;
+ entry = SamLookupUserW(sam, (LPWSTR)credentials->identity.User,
+ credentials->identity.UserLength * sizeof(WCHAR), NULL, 0);
}
- entry = SamLookupUserW(sam, (LPWSTR)credentials->identity.User,
- credentials->identity.UserLength * 2, NULL, 0);
+ if (!entry)
+ goto fail;
- if (entry)
- {
#ifdef WITH_DEBUG_NTLM
WLog_VRB(TAG, "NTLM Hash:");
winpr_HexDump(TAG, WLOG_DEBUG, entry->NtHash, 16);
#endif
NTOWFv2FromHashW(entry->NtHash, (LPWSTR)credentials->identity.User,
- credentials->identity.UserLength * 2, (LPWSTR)credentials->identity.Domain,
- credentials->identity.DomainLength * 2, (BYTE*)hash);
+ credentials->identity.UserLength * sizeof(WCHAR),
+ (LPWSTR)credentials->identity.Domain,
+ credentials->identity.DomainLength * sizeof(WCHAR), (BYTE*)hash);
+
+ rc = TRUE;
+
+fail:
SamFreeEntry(sam, entry);
SamClose(sam);
- return 1;
- }
- else
- {
- SamClose(sam);
+ if (!rc)
WLog_ERR(TAG, "Error: Could not find user in SAM database");
- return 0;
- }
+
+ return rc;
}
static int ntlm_convert_password_hash(NTLM_CONTEXT* context, BYTE* hash)
