Skip to content

Commit

Permalink
Use UTF-8 functions when working with explicit encodings.
Browse files Browse the repository at this point in the history
LSA functions explicitly operate on UTF-16 strings.
GetProcAddress() explicitly operates on multibyte strings.
  • Loading branch information
Iain Patterson committed Jul 28, 2016
1 parent 800ca0c commit 2f3ab42
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 39 deletions.
35 changes: 8 additions & 27 deletions account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,32 +66,20 @@ int username_sid(const TCHAR *username, SID **sid, LSA_HANDLE *policy) {
}

LSA_UNICODE_STRING lsa_username;
#ifdef UNICODE
lsa_username.Buffer = (wchar_t *) expanded;
lsa_username.Length = (unsigned short) _tcslen(expanded) * sizeof(TCHAR);
lsa_username.MaximumLength = lsa_username.Length + sizeof(TCHAR);
#else
size_t buflen;
mbstowcs_s(&buflen, NULL, 0, expanded, _TRUNCATE);
lsa_username.MaximumLength = (unsigned short) buflen * sizeof(wchar_t);
lsa_username.Length = lsa_username.MaximumLength - sizeof(wchar_t);
lsa_username.Buffer = (wchar_t *) HeapAlloc(GetProcessHeap(), 0, lsa_username.MaximumLength);
if (lsa_username.Buffer) mbstowcs_s(&buflen, lsa_username.Buffer, lsa_username.MaximumLength, expanded, _TRUNCATE);
else {
int ret = to_utf16(expanded, &lsa_username.Buffer, (unsigned long *) &lsa_username.Length);
HeapFree(GetProcessHeap(), 0, expanded);
if (ret) {
if (policy == &handle) LsaClose(handle);
HeapFree(GetProcessHeap(), 0, expanded);
print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("LSA_UNICODE_STRING"), _T("username_sid()"));
return 4;
}
#endif
lsa_username.Length *= sizeof(wchar_t);
lsa_username.MaximumLength = lsa_username.Length + sizeof(wchar_t);

LSA_REFERENCED_DOMAIN_LIST *translated_domains;
LSA_TRANSLATED_SID *translated_sid;
NTSTATUS status = LsaLookupNames(*policy, 1, &lsa_username, &translated_domains, &translated_sid);
#ifndef UNICODE
HeapFree(GetProcessHeap(), 0, lsa_username.Buffer);
#endif
HeapFree(GetProcessHeap(), 0, expanded);
if (policy == &handle) LsaClose(handle);
if (status != STATUS_SUCCESS) {
LsaFreeMemory(translated_domains);
Expand Down Expand Up @@ -143,7 +131,7 @@ int username_sid(const TCHAR *username, SID **sid, LSA_HANDLE *policy) {
else *sub = translated_sid->RelativeId;
}

int ret = 0;
ret = 0;
if (translated_sid->Use == SidTypeWellKnownGroup && ! well_known_sid(*sid)) {
print_message(stderr, NSSM_GUI_INVALID_USERNAME, username);
ret = 10;
Expand Down Expand Up @@ -194,21 +182,14 @@ int canonicalise_username(const TCHAR *username, TCHAR **canon) {
memmove((char *) lsa_canon.Buffer + trust->Name.Length, L"\\", sizeof(wchar_t));
memmove((char *) lsa_canon.Buffer + trust->Name.Length + sizeof(wchar_t), translated_name->Name.Buffer, translated_name->Name.Length);

#ifdef UNICODE
*canon = lsa_canon.Buffer;
#else
size_t buflen;
wcstombs_s(&buflen, NULL, 0, lsa_canon.Buffer, _TRUNCATE);
*canon = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, buflen);
if (! *canon) {
unsigned long canonlen;
if (from_utf16(lsa_canon.Buffer, canon, &canonlen)) {
LsaFreeMemory(translated_domains);
LsaFreeMemory(translated_name);
print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("canon"), _T("username_sid"));
return 10;
}
wcstombs_s(&buflen, *canon, buflen, lsa_canon.Buffer, _TRUNCATE);
HeapFree(GetProcessHeap(), 0, lsa_canon.Buffer);
#endif

LsaFreeMemory(translated_domains);
LsaFreeMemory(translated_name);
Expand Down
16 changes: 4 additions & 12 deletions imports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,10 @@ FARPROC get_import(HMODULE library, const char *function, unsigned long *error)
*error = GetLastError();
if (*error != ERROR_PROC_NOT_FOUND) {
TCHAR *function_name;
#ifdef UNICODE
size_t buflen;
mbstowcs_s(&buflen, NULL, 0, function, _TRUNCATE);
function_name = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, buflen * sizeof(TCHAR));
if (function_name) mbstowcs_s(&buflen, function_name, buflen * sizeof(TCHAR), function, _TRUNCATE);
#else
function_name = (TCHAR *) function;
#endif
log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_GETPROCADDRESS_FAILED, function_name, error_string(*error), 0);
#ifdef UNICODE
if (function_name) HeapFree(GetProcessHeap(), 0, function_name);
#endif
if (! from_utf8(function, &function_name, 0)) {
log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_GETPROCADDRESS_FAILED, function_name, error_string(*error), 0);
HeapFree(GetProcessHeap(), 0, function_name);
}
}
}

Expand Down

0 comments on commit 2f3ab42

Please sign in to comment.