Browse Source

Use UTF-8 functions when working with explicit encodings.

LSA functions explicitly operate on UTF-16 strings.
GetProcAddress() explicitly operates on multibyte strings.
Iain Patterson 5 years ago
parent
commit
2f3ab42db3
2 changed files with 12 additions and 39 deletions
  1. 8 27
      account.cpp
  2. 4 12
      imports.cpp

+ 8 - 27
account.cpp

@@ -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);
@@ -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;
@@ -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);

+ 4 - 12
imports.cpp

@@ -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);
+      }
     }
   }