Skip to content

Commit b76766e

Browse files
author
Iain Patterson
committed
Handle virtual accounts when dumping service config.
If we are copying the service we need to build the virtual service account name for the new service.
1 parent 71764fe commit b76766e

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

account.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,19 +234,26 @@ int is_localsystem(const TCHAR *username) {
234234
return ret;
235235
}
236236

237+
/* Build the virtual account name. */
238+
TCHAR *virtual_account(const TCHAR *service_name) {
239+
size_t len = _tcslen(NSSM_VIRTUAL_SERVICE_ACCOUNT_DOMAIN) + _tcslen(service_name) + 2;
240+
TCHAR *name = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR));
241+
if (! name) {
242+
print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("name"), _T("virtual_account"));
243+
return 0;
244+
}
245+
246+
_sntprintf_s(name, len, _TRUNCATE, _T("%s\\%s"), NSSM_VIRTUAL_SERVICE_ACCOUNT_DOMAIN, service_name);
247+
return name;
248+
}
249+
237250
/* Does the username represent a virtual account for the service? */
238251
int is_virtual_account(const TCHAR *service_name, const TCHAR *username) {
239252
if (! imports.IsWellKnownSid) return 0;
240253
if (! service_name) return 0;
241254
if (! username) return 0;
242255

243-
size_t len = _tcslen(NSSM_VIRTUAL_SERVICE_ACCOUNT_DOMAIN) + _tcslen(service_name) + 2;
244-
TCHAR *canon = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR));
245-
if (! canon) {
246-
print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("canon"), _T("is_virtual_account"));
247-
return 0;
248-
}
249-
_sntprintf_s(canon, len, _TRUNCATE, _T("%s\\%s"), NSSM_VIRTUAL_SERVICE_ACCOUNT_DOMAIN, service_name);
256+
TCHAR *canon = virtual_account(service_name);
250257
int ret = str_equiv(canon, username);
251258
HeapFree(GetProcessHeap(), 0, canon);
252259
return ret;

account.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ int username_sid(const TCHAR *, SID **);
1919
int username_equiv(const TCHAR *, const TCHAR *);
2020
int canonicalise_username(const TCHAR *, TCHAR **);
2121
int is_localsystem(const TCHAR *);
22+
TCHAR *virtual_account(const TCHAR *);
2223
int is_virtual_account(const TCHAR *, const TCHAR *);
2324
const TCHAR *well_known_sid(SID *);
2425
const TCHAR *well_known_username(const TCHAR *);

gui.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,13 +465,12 @@ int configure(HWND window, nssm_service_t *service, nssm_service_t *orig_service
465465
}
466466
else if (SendDlgItemMessage(tablist[NSSM_TAB_LOGON], IDC_VIRTUAL_SERVICE, BM_GETCHECK, 0, 0) & BST_CHECKED) {
467467
if (service->username) HeapFree(GetProcessHeap(), 0, service->username);
468-
service->usernamelen = _tcslen(NSSM_VIRTUAL_SERVICE_ACCOUNT_DOMAIN) + _tcslen(service->name) + 2;
469-
service->username = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, service->usernamelen * sizeof(TCHAR));
468+
service->username = virtual_account(service->name);
470469
if (! service->username) {
471470
popup_message(window, MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("account name"), _T("install()"));
472471
return 6;
473472
}
474-
_sntprintf_s(service->username, service->usernamelen, _TRUNCATE, _T("%s\\%s"), NSSM_VIRTUAL_SERVICE_ACCOUNT_DOMAIN, service->name);
473+
service->usernamelen = _tcslen(service->username) + 1;
475474
service->password = 0;
476475
service->passwordlen = 0;
477476
}

settings.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,12 +1152,21 @@ int native_dump_objectname(const TCHAR *service_name, void *param, const TCHAR *
11521152
int ret = native_get_objectname(service_name, param, name, default_value, value, additional);
11531153
if (ret != 1) return ret;
11541154

1155-
/* Do we need to dump a dummy password? */
1156-
if (! well_known_username(value->string)) {
1157-
/* Parameters are the other way round. */
1158-
value_t inverted;
1159-
inverted.string = _T("****");
1160-
return setting_dump_string(service_name, (void *) REG_SZ, name, &inverted, value->string);
1155+
/* Properly checking for a virtual account requires the actual service name. */
1156+
if (! _tcsnicmp(NSSM_VIRTUAL_SERVICE_ACCOUNT_DOMAIN, value->string, _tcslen(NSSM_VIRTUAL_SERVICE_ACCOUNT_DOMAIN))) {
1157+
TCHAR *name = virtual_account(service_name);
1158+
if (! name) return -1;
1159+
HeapFree(GetProcessHeap(), 0, value->string);
1160+
value->string = name;
1161+
}
1162+
else {
1163+
/* Do we need to dump a dummy password? */
1164+
if (! well_known_username(value->string)) {
1165+
/* Parameters are the other way round. */
1166+
value_t inverted;
1167+
inverted.string = _T("****");
1168+
return setting_dump_string(service_name, (void *) REG_SZ, name, &inverted, value->string);
1169+
}
11611170
}
11621171
return setting_dump_string(service_name, (void *) REG_SZ, name, value, 0);
11631172
}

0 commit comments

Comments
 (0)