From: Iain Patterson Date: Mon, 30 Dec 2013 13:55:18 +0000 (+0000) Subject: Added get_service_username(). X-Git-Tag: v2.22~99 X-Git-Url: http://git.iain.cx/?a=commitdiff_plain;h=aa519d5f870a1069107c04fca707f84ea5d204a6;p=nssm.git Added get_service_username(). New function to get the service username. --- diff --git a/service.cpp b/service.cpp index 781f269..cccfa2b 100644 --- a/service.cpp +++ b/service.cpp @@ -142,6 +142,30 @@ int get_service_startup(const TCHAR *service_name, SC_HANDLE service_handle, con return 0; } +int get_service_username(const TCHAR *service_name, const QUERY_SERVICE_CONFIG *qsc, TCHAR **username, size_t *usernamelen) { + if (! username) return 1; + if (! usernamelen) return 1; + + *username = 0; + *usernamelen = 0; + + if (! qsc) return 1; + + if (str_equiv(qsc->lpServiceStartName, NSSM_LOCALSYSTEM_ACCOUNT)) return 0; + + size_t len = _tcslen(qsc->lpServiceStartName); + *username = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(TCHAR)); + if (! *username) { + print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("username"), _T("get_service_username()")); + return 2; + } + + memmove(*username, qsc->lpServiceStartName, (len + 1) * sizeof(TCHAR)); + *usernamelen = len; + + return 0; +} + static int grant_logon_as_service(const TCHAR *username) { if (str_equiv(username, NSSM_LOCALSYSTEM_ACCOUNT)) return 0; @@ -424,21 +448,13 @@ int pre_edit_service(int argc, TCHAR **argv) { return 4; } - if (! str_equiv(qsc->lpServiceStartName, NSSM_LOCALSYSTEM_ACCOUNT)) { - size_t len = _tcslen(qsc->lpServiceStartName); - service->username = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(TCHAR)); - if (service->username) { - memmove(service->username, qsc->lpServiceStartName, (len + 1) * sizeof(TCHAR)); - service->usernamelen = (unsigned long) len; - } - else { - HeapFree(GetProcessHeap(), 0, qsc); - CloseHandle(service->handle); - CloseServiceHandle(services); - print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("username"), _T("pre_edit_service()")); - return 5; - } + if (get_service_username(service->name, qsc, &service->username, &service->usernamelen)) { + HeapFree(GetProcessHeap(), 0, qsc); + CloseHandle(service->handle); + CloseServiceHandle(services); + return 5; } + _sntprintf_s(service->displayname, _countof(service->displayname), _TRUNCATE, _T("%s"), qsc->lpDisplayName); /* Get the canonical service name. We open it case insensitively. */ diff --git a/service.h b/service.h index 18a9850..d4d2e0f 100644 --- 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); int get_service_description(const TCHAR *, SC_HANDLE, unsigned long, TCHAR *); int get_service_startup(const TCHAR *, SC_HANDLE, const QUERY_SERVICE_CONFIG *, unsigned long *); +int get_service_username(const TCHAR *, const QUERY_SERVICE_CONFIG *, TCHAR **, size_t *); int pre_install_service(int, TCHAR **); int pre_remove_service(int, TCHAR **); int pre_edit_service(int, TCHAR **);