5 extern imports_t imports;
7 /* Open Policy object. */
8 int open_lsa_policy(LSA_HANDLE *policy) {
9 LSA_OBJECT_ATTRIBUTES attributes;
10 ZeroMemory(&attributes, sizeof(attributes));
12 NTSTATUS status = LsaOpenPolicy(0, &attributes, POLICY_ALL_ACCESS, policy);
14 print_message(stderr, NSSM_MESSAGE_LSAOPENPOLICY_FAILED, error_string(LsaNtStatusToWinError(status)));
21 /* Look up SID for an account. */
22 int username_sid(const TCHAR *username, SID **sid, LSA_HANDLE *policy) {
26 if (open_lsa_policy(policy)) return 1;
30 LsaLookupNames() can't look up .\username but can look up
31 %COMPUTERNAME%\username. ChangeServiceConfig() writes .\username to the
32 registry when %COMPUTERNAME%\username is a passed as a parameter. We
33 need to preserve .\username when calling ChangeServiceConfig() without
34 changing the username, but expand to %COMPUTERNAME%\username when calling
38 unsigned long expandedlen;
39 if (_tcsnicmp(_T(".\\"), username, 2)) {
40 expandedlen = (unsigned long) (_tcslen(username) + 1) * sizeof(TCHAR);
41 expanded = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, expandedlen);
43 print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("expanded"), _T("username_sid"));
44 if (policy == &handle) LsaClose(handle);
47 memmove(expanded, username, expandedlen);
50 TCHAR computername[MAX_COMPUTERNAME_LENGTH + 1];
51 expandedlen = _countof(computername);
52 GetComputerName(computername, &expandedlen);
53 expandedlen += (unsigned long) _tcslen(username);
55 expanded = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, expandedlen * sizeof(TCHAR));
57 print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("expanded"), _T("username_sid"));
58 if (policy == &handle) LsaClose(handle);
61 _sntprintf_s(expanded, expandedlen, _TRUNCATE, _T("%s\\%s"), computername, username + 2);
64 LSA_UNICODE_STRING lsa_username;
66 lsa_username.Buffer = (wchar_t *) expanded;
67 lsa_username.Length = (unsigned short) _tcslen(expanded) * sizeof(TCHAR);
68 lsa_username.MaximumLength = lsa_username.Length + sizeof(TCHAR);
71 mbstowcs_s(&buflen, NULL, 0, expanded, _TRUNCATE);
72 lsa_username.MaximumLength = (unsigned short) buflen * sizeof(wchar_t);
73 lsa_username.Length = lsa_username.MaximumLength - sizeof(wchar_t);
74 lsa_username.Buffer = (wchar_t *) HeapAlloc(GetProcessHeap(), 0, lsa_username.MaximumLength);
75 if (lsa_username.Buffer) mbstowcs_s(&buflen, lsa_username.Buffer, lsa_username.MaximumLength, expanded, _TRUNCATE);
77 if (policy == &handle) LsaClose(handle);
78 HeapFree(GetProcessHeap(), 0, expanded);
79 print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("LSA_UNICODE_STRING"), _T("username_sid()"));
84 LSA_REFERENCED_DOMAIN_LIST *translated_domains;
85 LSA_TRANSLATED_SID *translated_sid;
86 NTSTATUS status = LsaLookupNames(*policy, 1, &lsa_username, &translated_domains, &translated_sid);
88 HeapFree(GetProcessHeap(), 0, lsa_username.Buffer);
90 HeapFree(GetProcessHeap(), 0, expanded);
91 if (policy == &handle) LsaClose(handle);
93 LsaFreeMemory(translated_domains);
94 LsaFreeMemory(translated_sid);
95 print_message(stderr, NSSM_MESSAGE_LSALOOKUPNAMES_FAILED, username, error_string(LsaNtStatusToWinError(status)));
99 if (translated_sid->Use != SidTypeUser && translated_sid->Use != SidTypeWellKnownGroup) {
100 LsaFreeMemory(translated_domains);
101 LsaFreeMemory(translated_sid);
102 print_message(stderr, NSSM_GUI_INVALID_USERNAME, username);
106 LSA_TRUST_INFORMATION *trust = &translated_domains->Domains[translated_sid->DomainIndex];
107 if (! trust || ! IsValidSid(trust->Sid)) {
108 LsaFreeMemory(translated_domains);
109 LsaFreeMemory(translated_sid);
110 print_message(stderr, NSSM_GUI_INVALID_USERNAME, username);
114 /* GetSidSubAuthority*() return pointers! */
115 unsigned char *n = GetSidSubAuthorityCount(trust->Sid);
117 /* Convert translated SID to SID. */
118 *sid = (SID *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, GetSidLengthRequired(*n + 1));
120 LsaFreeMemory(translated_domains);
121 LsaFreeMemory(translated_sid);
122 print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("SID"), _T("username_sid"));
127 if (! InitializeSid(*sid, GetSidIdentifierAuthority(trust->Sid), *n + 1)) {
128 error = GetLastError();
129 HeapFree(GetProcessHeap(), 0, *sid);
130 LsaFreeMemory(translated_domains);
131 LsaFreeMemory(translated_sid);
132 print_message(stderr, NSSM_MESSAGE_INITIALIZESID_FAILED, username, error_string(error));
136 for (unsigned char i = 0; i <= *n; i++) {
137 unsigned long *sub = GetSidSubAuthority(*sid, i);
138 if (i < *n) *sub = *GetSidSubAuthority(trust->Sid, i);
139 else *sub = translated_sid->RelativeId;
143 if (translated_sid->Use == SidTypeWellKnownGroup && ! well_known_sid(*sid)) {
144 print_message(stderr, NSSM_GUI_INVALID_USERNAME, username);
148 LsaFreeMemory(translated_domains);
149 LsaFreeMemory(translated_sid);
154 int username_sid(const TCHAR *username, SID **sid) {
155 return username_sid(username, sid, 0);
158 int canonicalise_username(const TCHAR *username, TCHAR **canon) {
160 if (open_lsa_policy(&policy)) return 1;
163 if (username_sid(username, &sid, &policy)) return 2;
166 LSA_REFERENCED_DOMAIN_LIST *translated_domains;
167 LSA_TRANSLATED_NAME *translated_name;
168 NTSTATUS status = LsaLookupSids(policy, 1, &sids, &translated_domains, &translated_name);
170 LsaFreeMemory(translated_domains);
171 LsaFreeMemory(translated_name);
172 print_message(stderr, NSSM_MESSAGE_LSALOOKUPSIDS_FAILED, error_string(LsaNtStatusToWinError(status)));
176 LSA_TRUST_INFORMATION *trust = &translated_domains->Domains[translated_name->DomainIndex];
177 LSA_UNICODE_STRING lsa_canon;
178 lsa_canon.Length = translated_name->Name.Length + trust->Name.Length + sizeof(wchar_t);
179 lsa_canon.MaximumLength = lsa_canon.Length + sizeof(wchar_t);
180 lsa_canon.Buffer = (wchar_t *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lsa_canon.MaximumLength);
181 if (! lsa_canon.Buffer) {
182 LsaFreeMemory(translated_domains);
183 LsaFreeMemory(translated_name);
184 print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("lsa_canon"), _T("username_sid"));
188 /* Buffer is wchar_t but Length is in bytes. */
189 memmove((char *) lsa_canon.Buffer, trust->Name.Buffer, trust->Name.Length);
190 memmove((char *) lsa_canon.Buffer + trust->Name.Length, L"\\", sizeof(wchar_t));
191 memmove((char *) lsa_canon.Buffer + trust->Name.Length + sizeof(wchar_t), translated_name->Name.Buffer, translated_name->Name.Length);
194 *canon = lsa_canon.Buffer;
197 wcstombs_s(&buflen, NULL, 0, lsa_canon.Buffer, _TRUNCATE);
198 *canon = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, buflen);
200 LsaFreeMemory(translated_domains);
201 LsaFreeMemory(translated_name);
202 print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("canon"), _T("username_sid"));
205 wcstombs_s(&buflen, *canon, buflen, lsa_canon.Buffer, _TRUNCATE);
206 HeapFree(GetProcessHeap(), 0, lsa_canon.Buffer);
209 LsaFreeMemory(translated_domains);
210 LsaFreeMemory(translated_name);
215 /* Do two usernames map to the same SID? */
216 int username_equiv(const TCHAR *a, const TCHAR *b) {
218 if (username_sid(a, &sid_a)) return 0;
220 if (username_sid(b, &sid_b)) {
226 if (EqualSid(sid_a, sid_b)) ret = 1;
234 /* Does the username represent the LocalSystem account? */
235 int is_localsystem(const TCHAR *username) {
236 if (str_equiv(username, NSSM_LOCALSYSTEM_ACCOUNT)) return 1;
237 if (! imports.IsWellKnownSid) return 0;
240 if (username_sid(username, &sid)) return 0;
243 if (imports.IsWellKnownSid(sid, WinLocalSystemSid)) ret = 1;
251 Get well-known alias for LocalSystem and friends.
252 Returns a pointer to a static string. DO NOT try to free it.
254 const TCHAR *well_known_sid(SID *sid) {
255 if (! imports.IsWellKnownSid) return 0;
256 if (imports.IsWellKnownSid(sid, WinLocalSystemSid)) return NSSM_LOCALSYSTEM_ACCOUNT;
257 if (imports.IsWellKnownSid(sid, WinLocalServiceSid)) return NSSM_LOCALSERVICE_ACCOUNT;
258 if (imports.IsWellKnownSid(sid, WinNetworkServiceSid)) return NSSM_NETWORKSERVICE_ACCOUNT;
262 const TCHAR *well_known_username(const TCHAR *username) {
263 if (! username) return NSSM_LOCALSYSTEM_ACCOUNT;
264 if (str_equiv(username, NSSM_LOCALSYSTEM_ACCOUNT)) return NSSM_LOCALSYSTEM_ACCOUNT;
266 if (username_sid(username, &sid)) return 0;
268 const TCHAR *well_known = well_known_sid(sid);
274 int grant_logon_as_service(const TCHAR *username) {
275 if (! username) return 0;
277 /* Open Policy object. */
278 LSA_OBJECT_ATTRIBUTES attributes;
279 ZeroMemory(&attributes, sizeof(attributes));
284 if (open_lsa_policy(&policy)) return 1;
286 /* Look up SID for the account. */
288 if (username_sid(username, &sid, &policy)) {
294 Shouldn't happen because it should have been checked before callling this function.
296 if (well_known_sid(sid)) {
301 /* Check if the SID has the "Log on as a service" right. */
302 LSA_UNICODE_STRING lsa_right;
303 lsa_right.Buffer = NSSM_LOGON_AS_SERVICE_RIGHT;
304 lsa_right.Length = (unsigned short) wcslen(lsa_right.Buffer) * sizeof(wchar_t);
305 lsa_right.MaximumLength = lsa_right.Length + sizeof(wchar_t);
307 LSA_UNICODE_STRING *rights;
308 unsigned long count = ~0;
309 status = LsaEnumerateAccountRights(policy, sid, &rights, &count);
312 If the account has no rights set LsaEnumerateAccountRights() will return
313 STATUS_OBJECT_NAME_NOT_FOUND and set count to 0.
315 unsigned long error = LsaNtStatusToWinError(status);
316 if (error != ERROR_FILE_NOT_FOUND) {
319 print_message(stderr, NSSM_MESSAGE_LSAENUMERATEACCOUNTRIGHTS_FAILED, username, error_string(error));
324 for (unsigned long i = 0; i < count; i++) {
325 if (rights[i].Length != lsa_right.Length) continue;
326 if (_wcsnicmp(rights[i].Buffer, lsa_right.Buffer, lsa_right.MaximumLength)) continue;
327 /* The SID has the right. */
329 LsaFreeMemory(rights);
333 LsaFreeMemory(rights);
336 status = LsaAddAccountRights(policy, sid, &lsa_right, 1);
340 print_message(stderr, NSSM_MESSAGE_LSAADDACCOUNTRIGHTS_FAILED, error_string(LsaNtStatusToWinError(status)));
344 print_message(stdout, NSSM_MESSAGE_GRANTED_LOGON_AS_SERVICE, username);