2 * Unit tests for lsa functions
4 * Copyright (c) 2006 Robert Reif
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #define WIN32_NO_STATUS
34 #include "wine/test.h"
36 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
38 static HMODULE hadvapi32;
39 static NTSTATUS (WINAPI *pLsaClose)(LSA_HANDLE);
40 static NTSTATUS (WINAPI *pLsaEnumerateAccountRights)(LSA_HANDLE,PSID,PLSA_UNICODE_STRING*,PULONG);
41 static NTSTATUS (WINAPI *pLsaFreeMemory)(PVOID);
42 static NTSTATUS (WINAPI *pLsaOpenPolicy)(PLSA_UNICODE_STRING,PLSA_OBJECT_ATTRIBUTES,ACCESS_MASK,PLSA_HANDLE);
43 static NTSTATUS (WINAPI *pLsaQueryInformationPolicy)(LSA_HANDLE,POLICY_INFORMATION_CLASS,PVOID*);
44 static BOOL (WINAPI *pConvertSidToStringSidA)(PSID,LPSTR*);
45 static NTSTATUS (WINAPI *pLsaLookupNames2)(LSA_HANDLE,ULONG,ULONG,PLSA_UNICODE_STRING,PLSA_REFERENCED_DOMAIN_LIST*,PLSA_TRANSLATED_SID2*);
46 static NTSTATUS (WINAPI *pLsaLookupSids)(LSA_HANDLE,ULONG,PSID*,LSA_REFERENCED_DOMAIN_LIST**,LSA_TRANSLATED_NAME**);
48 static BOOL init(void)
50 hadvapi32 = GetModuleHandle("advapi32.dll");
52 pLsaClose = (void*)GetProcAddress(hadvapi32, "LsaClose");
53 pLsaEnumerateAccountRights = (void*)GetProcAddress(hadvapi32, "LsaEnumerateAccountRights");
54 pLsaFreeMemory = (void*)GetProcAddress(hadvapi32, "LsaFreeMemory");
55 pLsaOpenPolicy = (void*)GetProcAddress(hadvapi32, "LsaOpenPolicy");
56 pLsaQueryInformationPolicy = (void*)GetProcAddress(hadvapi32, "LsaQueryInformationPolicy");
57 pConvertSidToStringSidA = (void*)GetProcAddress(hadvapi32, "ConvertSidToStringSidA");
58 pLsaLookupNames2 = (void*)GetProcAddress(hadvapi32, "LsaLookupNames2");
59 pLsaLookupSids = (void*)GetProcAddress(hadvapi32, "LsaLookupSids");
61 if (pLsaClose && pLsaEnumerateAccountRights && pLsaFreeMemory && pLsaOpenPolicy && pLsaQueryInformationPolicy && pConvertSidToStringSidA)
67 static void test_lsa(void)
71 LSA_OBJECT_ATTRIBUTES object_attributes;
73 ZeroMemory(&object_attributes, sizeof(object_attributes));
74 object_attributes.Length = sizeof(object_attributes);
76 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_ALL_ACCESS, &handle);
77 ok(status == STATUS_SUCCESS || status == STATUS_ACCESS_DENIED,
78 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status);
80 /* try a more restricted access mask if necessary */
81 if (status == STATUS_ACCESS_DENIED) {
82 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION|POLICY_LOOKUP_NAMES\n");
83 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_VIEW_LOCAL_INFORMATION|POLICY_LOOKUP_NAMES, &handle);
84 ok(status == STATUS_SUCCESS, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION|POLICY_LOOKUP_NAMES) returned 0x%08x\n", status);
87 if (status == STATUS_SUCCESS) {
88 PPOLICY_AUDIT_EVENTS_INFO audit_events_info;
89 PPOLICY_PRIMARY_DOMAIN_INFO primary_domain_info;
90 PPOLICY_ACCOUNT_DOMAIN_INFO account_domain_info;
91 PPOLICY_DNS_DOMAIN_INFO dns_domain_info;
95 status = pLsaQueryInformationPolicy(handle, PolicyAuditEventsInformation, (PVOID*)&audit_events_info);
96 if (status == STATUS_ACCESS_DENIED)
97 skip("Not enough rights to retrieve PolicyAuditEventsInformation\n");
99 ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy(PolicyAuditEventsInformation) failed, returned 0x%08x\n", status);
100 if (status == STATUS_SUCCESS) {
101 pLsaFreeMemory((LPVOID)audit_events_info);
104 status = pLsaQueryInformationPolicy(handle, PolicyPrimaryDomainInformation, (PVOID*)&primary_domain_info);
105 ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy(PolicyPrimaryDomainInformation) failed, returned 0x%08x\n", status);
106 if (status == STATUS_SUCCESS) {
107 if (primary_domain_info->Sid) {
109 if (pConvertSidToStringSidA(primary_domain_info->Sid, &strsid))
111 if (primary_domain_info->Name.Buffer) {
114 len = WideCharToMultiByte( CP_ACP, 0, primary_domain_info->Name.Buffer, -1, NULL, 0, NULL, NULL );
115 name = LocalAlloc( 0, len );
116 WideCharToMultiByte( CP_ACP, 0, primary_domain_info->Name.Buffer, -1, name, len, NULL, NULL );
117 trace(" name: %s sid: %s\n", name, strsid);
120 trace(" name: NULL sid: %s\n", strsid);
124 trace("invalid sid\n");
127 trace("Running on a standalone system.\n");
128 pLsaFreeMemory((LPVOID)primary_domain_info);
131 status = pLsaQueryInformationPolicy(handle, PolicyAccountDomainInformation, (PVOID*)&account_domain_info);
132 ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) failed, returned 0x%08x\n", status);
133 if (status == STATUS_SUCCESS) {
134 pLsaFreeMemory((LPVOID)account_domain_info);
137 /* This isn't supported in NT4 */
138 status = pLsaQueryInformationPolicy(handle, PolicyDnsDomainInformation, (PVOID*)&dns_domain_info);
139 ok(status == STATUS_SUCCESS || status == STATUS_INVALID_PARAMETER,
140 "LsaQueryInformationPolicy(PolicyDnsDomainInformation) failed, returned 0x%08x\n", status);
141 if (status == STATUS_SUCCESS) {
142 if (dns_domain_info->Sid || !IsEqualGUID(&dns_domain_info->DomainGuid, &GUID_NULL)) {
147 LPSTR guidstr = NULL;
151 pConvertSidToStringSidA(dns_domain_info->Sid, &strsid);
152 StringFromGUID2(&dns_domain_info->DomainGuid, guidstrW, sizeof(guidstrW)/sizeof(WCHAR));
153 len = WideCharToMultiByte( CP_ACP, 0, guidstrW, -1, NULL, 0, NULL, NULL );
154 guidstr = LocalAlloc( 0, len );
155 WideCharToMultiByte( CP_ACP, 0, guidstrW, -1, guidstr, len, NULL, NULL );
156 if (dns_domain_info->Name.Buffer) {
157 len = WideCharToMultiByte( CP_ACP, 0, dns_domain_info->Name.Buffer, -1, NULL, 0, NULL, NULL );
158 name = LocalAlloc( 0, len );
159 WideCharToMultiByte( CP_ACP, 0, dns_domain_info->Name.Buffer, -1, name, len, NULL, NULL );
161 if (dns_domain_info->DnsDomainName.Buffer) {
162 len = WideCharToMultiByte( CP_ACP, 0, dns_domain_info->DnsDomainName.Buffer, -1, NULL, 0, NULL, NULL );
163 domain = LocalAlloc( 0, len );
164 WideCharToMultiByte( CP_ACP, 0, dns_domain_info->DnsDomainName.Buffer, -1, domain, len, NULL, NULL );
166 if (dns_domain_info->DnsForestName.Buffer) {
167 len = WideCharToMultiByte( CP_ACP, 0, dns_domain_info->DnsForestName.Buffer, -1, NULL, 0, NULL, NULL );
168 forest = LocalAlloc( 0, len );
169 WideCharToMultiByte( CP_ACP, 0, dns_domain_info->DnsForestName.Buffer, -1, forest, len, NULL, NULL );
171 trace(" name: %s domain: %s forest: %s guid: %s sid: %s\n",
172 name ? name : "NULL", domain ? domain : "NULL",
173 forest ? forest : "NULL", guidstr, strsid ? strsid : "NULL");
177 LocalFree( guidstr );
181 trace("Running on a standalone system.\n");
182 pLsaFreeMemory((LPVOID)dns_domain_info);
185 /* We need a valid SID to pass to LsaEnumerateAccountRights */
186 ret = OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &token );
187 ok(ret, "Unable to obtain process token, error %u\n", GetLastError( ));
191 TOKEN_USER *token_user = (TOKEN_USER *) buffer;
192 ret = GetTokenInformation( token, TokenUser, (LPVOID) token_user, sizeof(buffer), &len );
193 ok(ret || GetLastError( ) == ERROR_INSUFFICIENT_BUFFER, "Unable to obtain token information, error %u\n", GetLastError( ));
194 if (! ret && GetLastError( ) == ERROR_INSUFFICIENT_BUFFER) {
195 trace("Resizing buffer to %u.\n", len);
196 token_user = LocalAlloc( 0, len );
197 if (token_user != NULL)
198 ret = GetTokenInformation( token, TokenUser, (LPVOID) token_user, len, &len );
202 PLSA_UNICODE_STRING rights;
204 rights = (PLSA_UNICODE_STRING) 0xdeadbeaf;
205 rights_count = 0xcafecafe;
206 status = pLsaEnumerateAccountRights(handle, token_user->User.Sid, &rights, &rights_count);
207 ok(status == STATUS_SUCCESS || status == STATUS_OBJECT_NAME_NOT_FOUND, "Unexpected status 0x%x\n", status);
208 if (status == STATUS_SUCCESS)
209 pLsaFreeMemory( rights );
211 ok(rights == NULL && rights_count == 0, "Expected rights and rights_count to be set to 0 on failure\n");
213 if (token_user != NULL && token_user != (TOKEN_USER *) buffer)
214 LocalFree( token_user );
215 CloseHandle( token );
218 status = pLsaClose(handle);
219 ok(status == STATUS_SUCCESS, "LsaClose() failed, returned 0x%08x\n", status);
223 static void get_sid_info(PSID psid, LPSTR *user, LPSTR *dom)
225 static char account[257], domain[257];
226 DWORD user_size, dom_size;
233 user_size = dom_size = 257;
234 account[0] = domain[0] = 0;
235 ret = LookupAccountSidA(NULL, psid, account, &user_size, domain, &dom_size, &use);
236 ok(ret, "LookupAccountSidA failed %u\n", GetLastError());
239 static void test_LsaLookupNames2(void)
241 static const WCHAR n1[] = {'L','O','C','A','L',' ','S','E','R','V','I','C','E'};
242 static const WCHAR n2[] = {'N','T',' ','A','U','T','H','O','R','I','T','Y','\\','L','o','c','a','l','S','e','r','v','i','c','e'};
246 LSA_OBJECT_ATTRIBUTES attrs;
247 PLSA_REFERENCED_DOMAIN_LIST domains;
248 PLSA_TRANSLATED_SID2 sids;
249 LSA_UNICODE_STRING name[3];
250 LPSTR account, sid_dom;
252 if (!pLsaLookupNames2)
254 win_skip("LsaLookupNames2 not available\n");
258 if (PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale())) != LANG_ENGLISH)
260 skip("Non-English locale (skipping LsaLookupNames2 tests)\n");
264 memset(&attrs, 0, sizeof(attrs));
265 attrs.Length = sizeof(attrs);
267 status = pLsaOpenPolicy(NULL, &attrs, POLICY_ALL_ACCESS, &handle);
268 ok(status == STATUS_SUCCESS || status == STATUS_ACCESS_DENIED,
269 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status);
271 /* try a more restricted access mask if necessary */
272 if (status == STATUS_ACCESS_DENIED)
274 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
275 status = pLsaOpenPolicy(NULL, &attrs, POLICY_LOOKUP_NAMES, &handle);
276 ok(status == STATUS_SUCCESS, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status);
278 if (status != STATUS_SUCCESS)
280 skip("Cannot acquire policy handle\n");
284 name[0].Buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(n1));
285 name[0].Length = name[0].MaximumLength = sizeof(n1);
286 memcpy(name[0].Buffer, n1, sizeof(n1));
288 name[1].Buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(n1));
289 name[1].Length = name[1].MaximumLength = sizeof(n1) - sizeof(WCHAR);
290 memcpy(name[1].Buffer, n1, sizeof(n1) - sizeof(WCHAR));
292 name[2].Buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(n2));
293 name[2].Length = name[2].MaximumLength = sizeof(n2);
294 memcpy(name[2].Buffer, n2, sizeof(n2));
296 /* account name only */
299 status = pLsaLookupNames2(handle, 0, 1, &name[0], &domains, &sids);
300 ok(status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %x)\n", status);
301 ok(sids[0].Use == SidTypeWellKnownGroup, "expected SidTypeWellKnownGroup, got %u\n", sids[0].Use);
302 ok(sids[0].Flags == 0, "expected 0, got 0x%08x\n", sids[0].Flags);
303 ok(domains->Entries == 1, "expected 1, got %u\n", domains->Entries);
304 get_sid_info(sids[0].Sid, &account, &sid_dom);
305 ok(!strcmp(account, "LOCAL SERVICE"), "expected \"LOCAL SERVICE\", got \"%s\"\n", account);
306 ok(!strcmp(sid_dom, "NT AUTHORITY"), "expected \"NT AUTHORITY\", got \"%s\"\n", sid_dom);
307 pLsaFreeMemory(sids);
308 pLsaFreeMemory(domains);
310 /* unknown account name */
313 status = pLsaLookupNames2(handle, 0, 1, &name[1], &domains, &sids);
314 ok(status == STATUS_NONE_MAPPED, "expected STATUS_NONE_MAPPED, got %x)\n", status);
315 ok(sids[0].Use == SidTypeUnknown, "expected SidTypeUnknown, got %u\n", sids[0].Use);
316 ok(sids[0].Flags == 0, "expected 0, got 0x%08x\n", sids[0].Flags);
317 ok(domains->Entries == 0, "expected 0, got %u\n", domains->Entries);
318 pLsaFreeMemory(sids);
319 pLsaFreeMemory(domains);
321 /* account + domain */
324 status = pLsaLookupNames2(handle, 0, 1, &name[2], &domains, &sids);
325 ok(status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %x)\n", status);
326 ok(sids[0].Use == SidTypeWellKnownGroup, "expected SidTypeWellKnownGroup, got %u\n", sids[0].Use);
327 ok(sids[0].Flags == 0, "expected 0, got 0x%08x\n", sids[0].Flags);
328 ok(domains->Entries == 1, "expected 1, got %u\n", domains->Entries);
329 get_sid_info(sids[0].Sid, &account, &sid_dom);
330 ok(!strcmp(account, "LOCAL SERVICE"), "expected \"LOCAL SERVICE\", got \"%s\"\n", account);
331 ok(!strcmp(sid_dom, "NT AUTHORITY"), "expected \"NT AUTHORITY\", got \"%s\"\n", sid_dom);
332 pLsaFreeMemory(sids);
333 pLsaFreeMemory(domains);
338 status = pLsaLookupNames2(handle, 0, 3, name, &domains, &sids);
339 ok(status == STATUS_SOME_NOT_MAPPED, "expected STATUS_SOME_NOT_MAPPED, got %x)\n", status);
340 ok(sids[0].Use == SidTypeWellKnownGroup, "expected SidTypeWellKnownGroup, got %u\n", sids[0].Use);
341 ok(sids[1].Use == SidTypeUnknown, "expected SidTypeUnknown, got %u\n", sids[0].Use);
342 ok(sids[2].Use == SidTypeWellKnownGroup, "expected SidTypeWellKnownGroup, got %u\n", sids[0].Use);
343 ok(sids[0].DomainIndex == 0, "expected 0, got %u\n", sids[0].DomainIndex);
344 ok(domains->Entries == 1, "expected 1, got %u\n", domains->Entries);
345 pLsaFreeMemory(sids);
346 pLsaFreeMemory(domains);
348 HeapFree(GetProcessHeap(), 0, name[0].Buffer);
349 HeapFree(GetProcessHeap(), 0, name[1].Buffer);
350 HeapFree(GetProcessHeap(), 0, name[2].Buffer);
352 status = pLsaClose(handle);
353 ok(status == STATUS_SUCCESS, "LsaClose() failed, returned 0x%08x\n", status);
356 static void test_LsaLookupSids(void)
358 LSA_REFERENCED_DOMAIN_LIST *list;
359 LSA_OBJECT_ATTRIBUTES attrs;
360 LSA_TRANSLATED_NAME *names;
368 memset(&attrs, 0, sizeof(attrs));
369 attrs.Length = sizeof(attrs);
371 status = pLsaOpenPolicy(NULL, &attrs, POLICY_LOOKUP_NAMES, &policy);
372 ok(status == STATUS_SUCCESS, "got 0x%08x\n", status);
374 ret = OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED, &token);
375 ok(ret, "got %d\n", ret);
377 ret = GetTokenInformation(token, TokenUser, NULL, 0, &size);
378 ok(!ret, "gotr %d\n", ret);
380 user = HeapAlloc(GetProcessHeap(), 0, size);
381 ret = GetTokenInformation(token, TokenUser, user, size, &size);
382 ok(ret, "got %d\n", ret);
384 status = pLsaLookupSids(policy, 1, &user->User.Sid, &list, &names);
385 ok(status == STATUS_SUCCESS, "got 0x%08x\n", status);
387 ok(list->Entries > 0, "got %d\n", list->Entries);
390 ok((char*)list->Domains - (char*)list > 0, "%p, %p\n", list, list->Domains);
391 ok((char*)list->Domains[0].Sid - (char*)list->Domains > 0, "%p, %p\n", list->Domains, list->Domains[0].Sid);
394 pLsaFreeMemory(names);
395 pLsaFreeMemory(list);
397 HeapFree(GetProcessHeap(), 0, user);
401 status = pLsaClose(policy);
402 ok(status == STATUS_SUCCESS, "got 0x%08x\n", status);
408 win_skip("Needed functions are not available\n");
413 test_LsaLookupNames2();
414 test_LsaLookupSids();