Added get_service_username().
authorIain Patterson <me@iain.cx>
Mon, 30 Dec 2013 13:55:18 +0000 (13:55 +0000)
committerIain Patterson <me@iain.cx>
Wed, 1 Jan 2014 12:41:56 +0000 (12:41 +0000)
New function to get the service username.

service.cpp
service.h

index 781f269..cccfa2b 100644 (file)
@@ -142,6 +142,30 @@ int get_service_startup(const TCHAR *service_name, SC_HANDLE service_handle, con
   return 0;\r
 }\r
 \r
+int get_service_username(const TCHAR *service_name, const QUERY_SERVICE_CONFIG *qsc, TCHAR **username, size_t *usernamelen) {\r
+  if (! username) return 1;\r
+  if (! usernamelen) return 1;\r
+\r
+  *username = 0;\r
+  *usernamelen = 0;\r
+\r
+  if (! qsc) return 1;\r
+\r
+  if (str_equiv(qsc->lpServiceStartName, NSSM_LOCALSYSTEM_ACCOUNT)) return 0;\r
+\r
+  size_t len = _tcslen(qsc->lpServiceStartName);\r
+  *username = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(TCHAR));\r
+  if (! *username) {\r
+    print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("username"), _T("get_service_username()"));\r
+    return 2;\r
+  }\r
+\r
+  memmove(*username, qsc->lpServiceStartName, (len + 1) * sizeof(TCHAR));\r
+  *usernamelen = len;\r
+\r
+  return 0;\r
+}\r
+\r
 static int grant_logon_as_service(const TCHAR *username) {\r
   if (str_equiv(username, NSSM_LOCALSYSTEM_ACCOUNT)) return 0;\r
 \r
@@ -424,21 +448,13 @@ int pre_edit_service(int argc, TCHAR **argv) {
     return 4;\r
   }\r
 \r
-  if (! str_equiv(qsc->lpServiceStartName, NSSM_LOCALSYSTEM_ACCOUNT)) {\r
-    size_t len = _tcslen(qsc->lpServiceStartName);\r
-    service->username = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(TCHAR));\r
-    if (service->username) {\r
-      memmove(service->username, qsc->lpServiceStartName, (len + 1) * sizeof(TCHAR));\r
-      service->usernamelen = (unsigned long) len;\r
-    }\r
-    else {\r
-      HeapFree(GetProcessHeap(), 0, qsc);\r
-      CloseHandle(service->handle);\r
-      CloseServiceHandle(services);\r
-      print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("username"), _T("pre_edit_service()"));\r
-      return 5;\r
-    }\r
+  if (get_service_username(service->name, qsc, &service->username, &service->usernamelen)) {\r
+    HeapFree(GetProcessHeap(), 0, qsc);\r
+    CloseHandle(service->handle);\r
+    CloseServiceHandle(services);\r
+    return 5;\r
   }\r
+\r
   _sntprintf_s(service->displayname, _countof(service->displayname), _TRUNCATE, _T("%s"), qsc->lpDisplayName);\r
 \r
   /* Get the canonical service name. We open it case insensitively. */\r
index 18a9850..d4d2e0f 100644 (file)
--- a/service.h
+++ b/service.h
@@ -92,6 +92,7 @@ SC_HANDLE open_service_manager();
 QUERY_SERVICE_CONFIG *query_service_config(const TCHAR *, SC_HANDLE);\r
 int get_service_description(const TCHAR *, SC_HANDLE, unsigned long, TCHAR *);\r
 int get_service_startup(const TCHAR *, SC_HANDLE, const QUERY_SERVICE_CONFIG *, unsigned long *);\r
+int get_service_username(const TCHAR *, const QUERY_SERVICE_CONFIG *, TCHAR **, size_t *);\r
 int pre_install_service(int, TCHAR **);\r
 int pre_remove_service(int, TCHAR **);\r
 int pre_edit_service(int, TCHAR **);\r