[ADVAPI32_WINETEST] Sync with Wine Staging 1.7.55. CORE-10536
[reactos.git] / rostests / winetests / advapi32 / security.c
1 /*
2 * Unit tests for security functions
3 *
4 * Copyright (c) 2004 Mike McCormack
5 * Copyright (c) 2011 Dmitry Timoshkov
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include <stdarg.h>
23 #include <stdio.h>
24
25 #include "ntstatus.h"
26 #define WIN32_NO_STATUS
27 #define WIN32_LEAN_AND_MEAN
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "wine/winternl.h"
32 #include "aclapi.h"
33 #include "winnt.h"
34 #include "sddl.h"
35 #include "ntsecapi.h"
36 #include "lmcons.h"
37
38 #include <winsvc.h>
39
40 #include "wine/test.h"
41
42 #ifndef PROCESS_QUERY_LIMITED_INFORMATION
43 #define PROCESS_QUERY_LIMITED_INFORMATION 0x1000
44 #endif
45
46 /* PROCESS_ALL_ACCESS in Vista+ PSDKs is incompatible with older Windows versions */
47 #define PROCESS_ALL_ACCESS_NT4 (PROCESS_ALL_ACCESS & ~0xf000)
48 #define PROCESS_ALL_ACCESS_VISTA (PROCESS_ALL_ACCESS | 0xf000)
49
50 #ifndef EVENT_QUERY_STATE
51 #define EVENT_QUERY_STATE 0x0001
52 #endif
53
54 #ifndef SEMAPHORE_QUERY_STATE
55 #define SEMAPHORE_QUERY_STATE 0x0001
56 #endif
57
58 #ifndef THREAD_SET_LIMITED_INFORMATION
59 #define THREAD_SET_LIMITED_INFORMATION 0x0400
60 #define THREAD_QUERY_LIMITED_INFORMATION 0x0800
61 #endif
62
63 #define THREAD_ALL_ACCESS_NT4 (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x3ff)
64 #define THREAD_ALL_ACCESS_VISTA (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0xffff)
65
66 #define expect_eq(expr, value, type, format) { type ret_ = expr; ok((value) == ret_, #expr " expected " format " got " format "\n", (value), (ret_)); }
67
68 static BOOL (WINAPI *pAddAccessAllowedAceEx)(PACL, DWORD, DWORD, DWORD, PSID);
69 static BOOL (WINAPI *pAddAccessDeniedAceEx)(PACL, DWORD, DWORD, DWORD, PSID);
70 static BOOL (WINAPI *pAddAuditAccessAceEx)(PACL, DWORD, DWORD, DWORD, PSID, BOOL, BOOL);
71 static VOID (WINAPI *pBuildTrusteeWithSidA)( PTRUSTEEA pTrustee, PSID pSid );
72 static VOID (WINAPI *pBuildTrusteeWithNameA)( PTRUSTEEA pTrustee, LPSTR pName );
73 static VOID (WINAPI *pBuildTrusteeWithObjectsAndNameA)( PTRUSTEEA pTrustee,
74 POBJECTS_AND_NAME_A pObjName,
75 SE_OBJECT_TYPE ObjectType,
76 LPSTR ObjectTypeName,
77 LPSTR InheritedObjectTypeName,
78 LPSTR Name );
79 static VOID (WINAPI *pBuildTrusteeWithObjectsAndSidA)( PTRUSTEEA pTrustee,
80 POBJECTS_AND_SID pObjSid,
81 GUID* pObjectGuid,
82 GUID* pInheritedObjectGuid,
83 PSID pSid );
84 static LPSTR (WINAPI *pGetTrusteeNameA)( PTRUSTEEA pTrustee );
85 static BOOL (WINAPI *pMakeSelfRelativeSD)( PSECURITY_DESCRIPTOR, PSECURITY_DESCRIPTOR, LPDWORD );
86 static BOOL (WINAPI *pConvertSidToStringSidA)( PSID pSid, LPSTR *str );
87 static BOOL (WINAPI *pConvertStringSidToSidA)( LPCSTR str, PSID pSid );
88 static BOOL (WINAPI *pCheckTokenMembership)(HANDLE, PSID, PBOOL);
89 static BOOL (WINAPI *pConvertStringSecurityDescriptorToSecurityDescriptorA)(LPCSTR, DWORD,
90 PSECURITY_DESCRIPTOR*, PULONG );
91 static BOOL (WINAPI *pConvertStringSecurityDescriptorToSecurityDescriptorW)(LPCWSTR, DWORD,
92 PSECURITY_DESCRIPTOR*, PULONG );
93 static BOOL (WINAPI *pConvertSecurityDescriptorToStringSecurityDescriptorA)(PSECURITY_DESCRIPTOR, DWORD,
94 SECURITY_INFORMATION, LPSTR *, PULONG );
95 static BOOL (WINAPI *pGetFileSecurityA)(LPCSTR, SECURITY_INFORMATION,
96 PSECURITY_DESCRIPTOR, DWORD, LPDWORD);
97 static BOOL (WINAPI *pSetFileSecurityA)(LPCSTR, SECURITY_INFORMATION,
98 PSECURITY_DESCRIPTOR);
99 static DWORD (WINAPI *pGetNamedSecurityInfoA)(LPSTR, SE_OBJECT_TYPE, SECURITY_INFORMATION,
100 PSID*, PSID*, PACL*, PACL*,
101 PSECURITY_DESCRIPTOR*);
102 static DWORD (WINAPI *pSetNamedSecurityInfoA)(LPSTR, SE_OBJECT_TYPE, SECURITY_INFORMATION,
103 PSID, PSID, PACL, PACL);
104 static PDWORD (WINAPI *pGetSidSubAuthority)(PSID, DWORD);
105 static PUCHAR (WINAPI *pGetSidSubAuthorityCount)(PSID);
106 static BOOL (WINAPI *pIsValidSid)(PSID);
107 static DWORD (WINAPI *pRtlAdjustPrivilege)(ULONG,BOOLEAN,BOOLEAN,PBOOLEAN);
108 static BOOL (WINAPI *pCreateWellKnownSid)(WELL_KNOWN_SID_TYPE,PSID,PSID,DWORD*);
109 static BOOL (WINAPI *pDuplicateTokenEx)(HANDLE,DWORD,LPSECURITY_ATTRIBUTES,
110 SECURITY_IMPERSONATION_LEVEL,TOKEN_TYPE,PHANDLE);
111
112 static NTSTATUS (WINAPI *pLsaQueryInformationPolicy)(LSA_HANDLE,POLICY_INFORMATION_CLASS,PVOID*);
113 static NTSTATUS (WINAPI *pLsaClose)(LSA_HANDLE);
114 static NTSTATUS (WINAPI *pLsaFreeMemory)(PVOID);
115 static NTSTATUS (WINAPI *pLsaOpenPolicy)(PLSA_UNICODE_STRING,PLSA_OBJECT_ATTRIBUTES,ACCESS_MASK,PLSA_HANDLE);
116 static NTSTATUS (WINAPI *pNtQueryObject)(HANDLE,OBJECT_INFORMATION_CLASS,PVOID,ULONG,PULONG);
117 static DWORD (WINAPI *pSetEntriesInAclW)(ULONG, PEXPLICIT_ACCESSW, PACL, PACL*);
118 static DWORD (WINAPI *pSetEntriesInAclA)(ULONG, PEXPLICIT_ACCESSA, PACL, PACL*);
119 static BOOL (WINAPI *pSetSecurityDescriptorControl)(PSECURITY_DESCRIPTOR, SECURITY_DESCRIPTOR_CONTROL,
120 SECURITY_DESCRIPTOR_CONTROL);
121 static DWORD (WINAPI *pGetSecurityInfo)(HANDLE, SE_OBJECT_TYPE, SECURITY_INFORMATION,
122 PSID*, PSID*, PACL*, PACL*, PSECURITY_DESCRIPTOR*);
123 static DWORD (WINAPI *pSetSecurityInfo)(HANDLE, SE_OBJECT_TYPE, SECURITY_INFORMATION,
124 PSID, PSID, PACL, PACL);
125 static NTSTATUS (WINAPI *pNtAccessCheck)(PSECURITY_DESCRIPTOR, HANDLE, ACCESS_MASK, PGENERIC_MAPPING,
126 PPRIVILEGE_SET, PULONG, PULONG, NTSTATUS*);
127 static BOOL (WINAPI *pCreateRestrictedToken)(HANDLE, DWORD, DWORD, PSID_AND_ATTRIBUTES, DWORD,
128 PLUID_AND_ATTRIBUTES, DWORD, PSID_AND_ATTRIBUTES, PHANDLE);
129 static BOOL (WINAPI *pGetAclInformation)(PACL,LPVOID,DWORD,ACL_INFORMATION_CLASS);
130 static BOOL (WINAPI *pGetAce)(PACL,DWORD,LPVOID*);
131 static NTSTATUS (WINAPI *pNtSetSecurityObject)(HANDLE,SECURITY_INFORMATION,PSECURITY_DESCRIPTOR);
132 static NTSTATUS (WINAPI *pNtCreateFile)(PHANDLE,ACCESS_MASK,POBJECT_ATTRIBUTES,PIO_STATUS_BLOCK,PLARGE_INTEGER,ULONG,ULONG,ULONG,ULONG,PVOID,ULONG);
133 static BOOL (WINAPI *pRtlDosPathNameToNtPathName_U)(LPCWSTR,PUNICODE_STRING,PWSTR*,CURDIR*);
134 static NTSTATUS (WINAPI *pRtlAnsiStringToUnicodeString)(PUNICODE_STRING,PCANSI_STRING,BOOLEAN);
135 static BOOL (WINAPI *pGetWindowsAccountDomainSid)(PSID,PSID,DWORD*);
136 static void (WINAPI *pRtlInitAnsiString)(PANSI_STRING,PCSZ);
137 static NTSTATUS (WINAPI *pRtlFreeUnicodeString)(PUNICODE_STRING);
138
139 static HMODULE hmod;
140 static int myARGC;
141 static char** myARGV;
142
143 struct strsid_entry
144 {
145 const char *str;
146 DWORD flags;
147 };
148 #define STRSID_OK 0
149 #define STRSID_OPT 1
150
151 struct sidRef
152 {
153 SID_IDENTIFIER_AUTHORITY auth;
154 const char *refStr;
155 };
156
157 static void init(void)
158 {
159 HMODULE hntdll;
160
161 hntdll = GetModuleHandleA("ntdll.dll");
162 pNtQueryObject = (void *)GetProcAddress( hntdll, "NtQueryObject" );
163 pNtAccessCheck = (void *)GetProcAddress( hntdll, "NtAccessCheck" );
164 pNtSetSecurityObject = (void *)GetProcAddress(hntdll, "NtSetSecurityObject");
165 pNtCreateFile = (void *)GetProcAddress(hntdll, "NtCreateFile");
166 pRtlDosPathNameToNtPathName_U = (void *)GetProcAddress(hntdll, "RtlDosPathNameToNtPathName_U");
167 pRtlAnsiStringToUnicodeString = (void *)GetProcAddress(hntdll, "RtlAnsiStringToUnicodeString");
168 pRtlInitAnsiString = (void *)GetProcAddress(hntdll, "RtlInitAnsiString");
169 pRtlFreeUnicodeString = (void *)GetProcAddress(hntdll, "RtlFreeUnicodeString");
170
171 hmod = GetModuleHandleA("advapi32.dll");
172 pAddAccessAllowedAceEx = (void *)GetProcAddress(hmod, "AddAccessAllowedAceEx");
173 pAddAccessDeniedAceEx = (void *)GetProcAddress(hmod, "AddAccessDeniedAceEx");
174 pAddAuditAccessAceEx = (void *)GetProcAddress(hmod, "AddAuditAccessAceEx");
175 pCheckTokenMembership = (void *)GetProcAddress(hmod, "CheckTokenMembership");
176 pConvertStringSecurityDescriptorToSecurityDescriptorA =
177 (void *)GetProcAddress(hmod, "ConvertStringSecurityDescriptorToSecurityDescriptorA" );
178 pConvertStringSecurityDescriptorToSecurityDescriptorW =
179 (void *)GetProcAddress(hmod, "ConvertStringSecurityDescriptorToSecurityDescriptorW" );
180 pConvertSecurityDescriptorToStringSecurityDescriptorA =
181 (void *)GetProcAddress(hmod, "ConvertSecurityDescriptorToStringSecurityDescriptorA" );
182 pGetFileSecurityA = (void *)GetProcAddress(hmod, "GetFileSecurityA" );
183 pSetFileSecurityA = (void *)GetProcAddress(hmod, "SetFileSecurityA" );
184 pCreateWellKnownSid = (void *)GetProcAddress( hmod, "CreateWellKnownSid" );
185 pGetNamedSecurityInfoA = (void *)GetProcAddress(hmod, "GetNamedSecurityInfoA");
186 pSetNamedSecurityInfoA = (void *)GetProcAddress(hmod, "SetNamedSecurityInfoA");
187 pGetSidSubAuthority = (void *)GetProcAddress(hmod, "GetSidSubAuthority");
188 pGetSidSubAuthorityCount = (void *)GetProcAddress(hmod, "GetSidSubAuthorityCount");
189 pIsValidSid = (void *)GetProcAddress(hmod, "IsValidSid");
190 pMakeSelfRelativeSD = (void *)GetProcAddress(hmod, "MakeSelfRelativeSD");
191 pSetEntriesInAclW = (void *)GetProcAddress(hmod, "SetEntriesInAclW");
192 pSetEntriesInAclA = (void *)GetProcAddress(hmod, "SetEntriesInAclA");
193 pSetSecurityDescriptorControl = (void *)GetProcAddress(hmod, "SetSecurityDescriptorControl");
194 pGetSecurityInfo = (void *)GetProcAddress(hmod, "GetSecurityInfo");
195 pSetSecurityInfo = (void *)GetProcAddress(hmod, "SetSecurityInfo");
196 pCreateRestrictedToken = (void *)GetProcAddress(hmod, "CreateRestrictedToken");
197 pConvertSidToStringSidA = (void *)GetProcAddress(hmod, "ConvertSidToStringSidA");
198 pConvertStringSidToSidA = (void *)GetProcAddress(hmod, "ConvertStringSidToSidA");
199 pGetAclInformation = (void *)GetProcAddress(hmod, "GetAclInformation");
200 pGetAce = (void *)GetProcAddress(hmod, "GetAce");
201 pGetWindowsAccountDomainSid = (void *)GetProcAddress(hmod, "GetWindowsAccountDomainSid");
202
203 myARGC = winetest_get_mainargs( &myARGV );
204 }
205
206 static SECURITY_DESCRIPTOR* test_get_security_descriptor(HANDLE handle, int line)
207 {
208 /* use HeapFree(GetProcessHeap(), 0, sd); when done */
209 DWORD ret, length, needed;
210 SECURITY_DESCRIPTOR *sd;
211
212 needed = 0xdeadbeef;
213 SetLastError(0xdeadbeef);
214 ret = GetKernelObjectSecurity(handle, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
215 NULL, 0, &needed);
216 ok_(__FILE__, line)(!ret, "GetKernelObjectSecurity should fail\n");
217 ok_(__FILE__, line)(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
218 ok_(__FILE__, line)(needed != 0xdeadbeef, "GetKernelObjectSecurity should return required buffer length\n");
219
220 length = needed;
221 sd = HeapAlloc(GetProcessHeap(), 0, length);
222
223 needed = 0xdeadbeef;
224 SetLastError(0xdeadbeef);
225 ret = GetKernelObjectSecurity(handle, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
226 sd, length, &needed);
227 ok_(__FILE__, line)(ret, "GetKernelObjectSecurity error %d\n", GetLastError());
228 ok_(__FILE__, line)(needed == length || needed == 0 /* file, pipe */, "GetKernelObjectSecurity should return %u instead of %u\n", length, needed);
229 return sd;
230 }
231
232 static void test_owner_equal(HANDLE Handle, PSID expected, int line)
233 {
234 BOOL res;
235 SECURITY_DESCRIPTOR *queriedSD = NULL;
236 PSID owner;
237 BOOL owner_defaulted;
238
239 queriedSD = test_get_security_descriptor( Handle, line );
240
241 res = GetSecurityDescriptorOwner(queriedSD, &owner, &owner_defaulted);
242 ok_(__FILE__, line)(res, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
243
244 ok_(__FILE__, line)(EqualSid(owner, expected), "Owner SIDs are not equal\n");
245 ok_(__FILE__, line)(!owner_defaulted, "Defaulted is true\n");
246
247 HeapFree(GetProcessHeap(), 0, queriedSD);
248 }
249
250 static void test_group_equal(HANDLE Handle, PSID expected, int line)
251 {
252 BOOL res;
253 SECURITY_DESCRIPTOR *queriedSD = NULL;
254 PSID group;
255 BOOL group_defaulted;
256
257 queriedSD = test_get_security_descriptor( Handle, line );
258
259 res = GetSecurityDescriptorGroup(queriedSD, &group, &group_defaulted);
260 ok_(__FILE__, line)(res, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
261
262 ok_(__FILE__, line)(EqualSid(group, expected), "Group SIDs are not equal\n");
263 ok_(__FILE__, line)(!group_defaulted, "Defaulted is true\n");
264
265 HeapFree(GetProcessHeap(), 0, queriedSD);
266 }
267
268 static void test_sid(void)
269 {
270 struct sidRef refs[] = {
271 { { {0x00,0x00,0x33,0x44,0x55,0x66} }, "S-1-860116326-1" },
272 { { {0x00,0x00,0x01,0x02,0x03,0x04} }, "S-1-16909060-1" },
273 { { {0x00,0x00,0x00,0x01,0x02,0x03} }, "S-1-66051-1" },
274 { { {0x00,0x00,0x00,0x00,0x01,0x02} }, "S-1-258-1" },
275 { { {0x00,0x00,0x00,0x00,0x00,0x02} }, "S-1-2-1" },
276 { { {0x00,0x00,0x00,0x00,0x00,0x0c} }, "S-1-12-1" },
277 };
278 struct strsid_entry strsid_table[] = {
279 {"AO", STRSID_OK}, {"RU", STRSID_OK}, {"AN", STRSID_OK}, {"AU", STRSID_OK},
280 {"BA", STRSID_OK}, {"BG", STRSID_OK}, {"BO", STRSID_OK}, {"BU", STRSID_OK},
281 {"CA", STRSID_OPT}, {"CG", STRSID_OK}, {"CO", STRSID_OK}, {"DA", STRSID_OPT},
282 {"DC", STRSID_OPT}, {"DD", STRSID_OPT}, {"DG", STRSID_OPT}, {"DU", STRSID_OPT},
283 {"EA", STRSID_OPT}, {"ED", STRSID_OK}, {"WD", STRSID_OK}, {"PA", STRSID_OPT},
284 {"IU", STRSID_OK}, {"LA", STRSID_OK}, {"LG", STRSID_OK}, {"LS", STRSID_OK},
285 {"SY", STRSID_OK}, {"NU", STRSID_OK}, {"NO", STRSID_OK}, {"NS", STRSID_OK},
286 {"PO", STRSID_OK}, {"PS", STRSID_OK}, {"PU", STRSID_OK}, {"RS", STRSID_OPT},
287 {"RD", STRSID_OK}, {"RE", STRSID_OK}, {"RC", STRSID_OK}, {"SA", STRSID_OPT},
288 {"SO", STRSID_OK}, {"SU", STRSID_OK}};
289
290 const char noSubAuthStr[] = "S-1-5";
291 unsigned int i;
292 PSID psid = NULL;
293 SID *pisid;
294 BOOL r;
295 LPSTR str = NULL;
296
297 if( !pConvertSidToStringSidA || !pConvertStringSidToSidA )
298 {
299 win_skip("ConvertSidToStringSidA or ConvertStringSidToSidA not available\n");
300 return;
301 }
302
303 r = pConvertStringSidToSidA( NULL, NULL );
304 ok( !r, "expected failure with NULL parameters\n" );
305 if( GetLastError() == ERROR_CALL_NOT_IMPLEMENTED )
306 return;
307 ok( GetLastError() == ERROR_INVALID_PARAMETER,
308 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
309 GetLastError() );
310
311 r = pConvertStringSidToSidA( refs[0].refStr, NULL );
312 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
313 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
314 GetLastError() );
315
316 r = pConvertStringSidToSidA( NULL, &str );
317 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
318 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
319 GetLastError() );
320
321 r = pConvertStringSidToSidA( noSubAuthStr, &psid );
322 ok( !r,
323 "expected failure with no sub authorities\n" );
324 ok( GetLastError() == ERROR_INVALID_SID,
325 "expected GetLastError() is ERROR_INVALID_SID, got %d\n",
326 GetLastError() );
327
328 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid), "ConvertStringSidToSidA failed\n");
329 pisid = psid;
330 ok(pisid->SubAuthorityCount == 4, "Invalid sub authority count - expected 4, got %d\n", pisid->SubAuthorityCount);
331 ok(pisid->SubAuthority[0] == 21, "Invalid subauthority 0 - expected 21, got %d\n", pisid->SubAuthority[0]);
332 ok(pisid->SubAuthority[3] == 4576, "Invalid subauthority 0 - expected 4576, got %d\n", pisid->SubAuthority[3]);
333 LocalFree(str);
334 LocalFree(psid);
335
336 for( i = 0; i < sizeof(refs) / sizeof(refs[0]); i++ )
337 {
338 r = AllocateAndInitializeSid( &refs[i].auth, 1,1,0,0,0,0,0,0,0,
339 &psid );
340 ok( r, "failed to allocate sid\n" );
341 r = pConvertSidToStringSidA( psid, &str );
342 ok( r, "failed to convert sid\n" );
343 if (r)
344 {
345 ok( !strcmp( str, refs[i].refStr ),
346 "incorrect sid, expected %s, got %s\n", refs[i].refStr, str );
347 LocalFree( str );
348 }
349 if( psid )
350 FreeSid( psid );
351
352 r = pConvertStringSidToSidA( refs[i].refStr, &psid );
353 ok( r, "failed to parse sid string\n" );
354 pisid = psid;
355 ok( pisid &&
356 !memcmp( pisid->IdentifierAuthority.Value, refs[i].auth.Value,
357 sizeof(refs[i].auth) ),
358 "string sid %s didn't parse to expected value\n"
359 "(got 0x%04x%08x, expected 0x%04x%08x)\n",
360 refs[i].refStr,
361 MAKEWORD( pisid->IdentifierAuthority.Value[1],
362 pisid->IdentifierAuthority.Value[0] ),
363 MAKELONG( MAKEWORD( pisid->IdentifierAuthority.Value[5],
364 pisid->IdentifierAuthority.Value[4] ),
365 MAKEWORD( pisid->IdentifierAuthority.Value[3],
366 pisid->IdentifierAuthority.Value[2] ) ),
367 MAKEWORD( refs[i].auth.Value[1], refs[i].auth.Value[0] ),
368 MAKELONG( MAKEWORD( refs[i].auth.Value[5], refs[i].auth.Value[4] ),
369 MAKEWORD( refs[i].auth.Value[3], refs[i].auth.Value[2] ) ) );
370 if( psid )
371 LocalFree( psid );
372 }
373
374 /* string constant format not supported before XP */
375 r = pConvertStringSidToSidA(strsid_table[0].str, &psid);
376 if(!r)
377 {
378 win_skip("String constant format not supported\n");
379 return;
380 }
381 LocalFree(psid);
382
383 for(i = 0; i < sizeof(strsid_table) / sizeof(strsid_table[0]); i++)
384 {
385 char *temp;
386
387 SetLastError(0xdeadbeef);
388 r = pConvertStringSidToSidA(strsid_table[i].str, &psid);
389
390 if (!(strsid_table[i].flags & STRSID_OPT))
391 {
392 ok(r, "%s: got %u\n", strsid_table[i].str, GetLastError());
393 }
394
395 if (r)
396 {
397 if ((winetest_debug > 1) && (pConvertSidToStringSidA(psid, &temp)))
398 {
399 trace(" %s: %s\n", strsid_table[i].str, temp);
400 LocalFree(temp);
401 }
402 LocalFree(psid);
403 }
404 else
405 {
406 if (GetLastError() != ERROR_INVALID_SID)
407 trace(" %s: couldn't be converted, returned %d\n", strsid_table[i].str, GetLastError());
408 else
409 trace(" %s: couldn't be converted\n", strsid_table[i].str);
410 }
411 }
412 }
413
414 static void test_trustee(void)
415 {
416 GUID ObjectType = {0x12345678, 0x1234, 0x5678, {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}};
417 GUID InheritedObjectType = {0x23456789, 0x2345, 0x6786, {0x2, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}};
418 GUID ZeroGuid;
419 OBJECTS_AND_NAME_A oan;
420 OBJECTS_AND_SID oas;
421 TRUSTEEA trustee;
422 PSID psid;
423 char szObjectTypeName[] = "ObjectTypeName";
424 char szInheritedObjectTypeName[] = "InheritedObjectTypeName";
425 char szTrusteeName[] = "szTrusteeName";
426 SID_IDENTIFIER_AUTHORITY auth = { {0x11,0x22,0,0,0, 0} };
427
428 memset( &ZeroGuid, 0x00, sizeof (ZeroGuid) );
429
430 pBuildTrusteeWithSidA = (void *)GetProcAddress( hmod, "BuildTrusteeWithSidA" );
431 pBuildTrusteeWithNameA = (void *)GetProcAddress( hmod, "BuildTrusteeWithNameA" );
432 pBuildTrusteeWithObjectsAndNameA = (void *)GetProcAddress (hmod, "BuildTrusteeWithObjectsAndNameA" );
433 pBuildTrusteeWithObjectsAndSidA = (void *)GetProcAddress (hmod, "BuildTrusteeWithObjectsAndSidA" );
434 pGetTrusteeNameA = (void *)GetProcAddress (hmod, "GetTrusteeNameA" );
435 if( !pBuildTrusteeWithSidA || !pBuildTrusteeWithNameA ||
436 !pBuildTrusteeWithObjectsAndNameA || !pBuildTrusteeWithObjectsAndSidA ||
437 !pGetTrusteeNameA )
438 return;
439
440 if ( ! AllocateAndInitializeSid( &auth, 1, 42, 0,0,0,0,0,0,0,&psid ) )
441 {
442 trace( "failed to init SID\n" );
443 return;
444 }
445
446 /* test BuildTrusteeWithSidA */
447 memset( &trustee, 0xff, sizeof trustee );
448 pBuildTrusteeWithSidA( &trustee, psid );
449
450 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
451 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
452 "MultipleTrusteeOperation wrong\n");
453 ok( trustee.TrusteeForm == TRUSTEE_IS_SID, "TrusteeForm wrong\n");
454 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
455 ok( trustee.ptstrName == psid, "ptstrName wrong\n" );
456
457 /* test BuildTrusteeWithObjectsAndSidA (test 1) */
458 memset( &trustee, 0xff, sizeof trustee );
459 memset( &oas, 0xff, sizeof(oas) );
460 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, &ObjectType,
461 &InheritedObjectType, psid);
462
463 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
464 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
465 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
466 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
467 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
468
469 ok(oas.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
470 ok(!memcmp(&oas.ObjectTypeGuid, &ObjectType, sizeof(GUID)), "ObjectTypeGuid wrong\n");
471 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
472 ok(oas.pSid == psid, "pSid wrong\n");
473
474 /* test GetTrusteeNameA */
475 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oas, "GetTrusteeName returned wrong value\n");
476
477 /* test BuildTrusteeWithObjectsAndSidA (test 2) */
478 memset( &trustee, 0xff, sizeof trustee );
479 memset( &oas, 0xff, sizeof(oas) );
480 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, NULL,
481 &InheritedObjectType, psid);
482
483 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
484 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
485 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
486 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
487 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
488
489 ok(oas.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
490 ok(!memcmp(&oas.ObjectTypeGuid, &ZeroGuid, sizeof(GUID)), "ObjectTypeGuid wrong\n");
491 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
492 ok(oas.pSid == psid, "pSid wrong\n");
493
494 FreeSid( psid );
495
496 /* test BuildTrusteeWithNameA */
497 memset( &trustee, 0xff, sizeof trustee );
498 pBuildTrusteeWithNameA( &trustee, szTrusteeName );
499
500 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
501 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
502 "MultipleTrusteeOperation wrong\n");
503 ok( trustee.TrusteeForm == TRUSTEE_IS_NAME, "TrusteeForm wrong\n");
504 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
505 ok( trustee.ptstrName == szTrusteeName, "ptstrName wrong\n" );
506
507 /* test BuildTrusteeWithObjectsAndNameA (test 1) */
508 memset( &trustee, 0xff, sizeof trustee );
509 memset( &oan, 0xff, sizeof(oan) );
510 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
511 szInheritedObjectTypeName, szTrusteeName);
512
513 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
514 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
515 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
516 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
517 ok(trustee.ptstrName == (LPSTR)&oan, "ptstrName wrong\n");
518
519 ok(oan.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
520 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
521 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
522 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
523
524 /* test GetTrusteeNameA */
525 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oan, "GetTrusteeName returned wrong value\n");
526
527 /* test BuildTrusteeWithObjectsAndNameA (test 2) */
528 memset( &trustee, 0xff, sizeof trustee );
529 memset( &oan, 0xff, sizeof(oan) );
530 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, NULL,
531 szInheritedObjectTypeName, szTrusteeName);
532
533 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
534 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
535 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
536 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
537 ok(trustee.ptstrName == (LPSTR)&oan, "ptstrName wrong\n");
538
539 ok(oan.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
540 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
541 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
542 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
543
544 /* test BuildTrusteeWithObjectsAndNameA (test 3) */
545 memset( &trustee, 0xff, sizeof trustee );
546 memset( &oan, 0xff, sizeof(oan) );
547 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
548 NULL, szTrusteeName);
549
550 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
551 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
552 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
553 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
554 ok(trustee.ptstrName == (LPSTR)&oan, "ptstrName wrong\n");
555
556 ok(oan.ObjectsPresent == ACE_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
557 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
558 ok(oan.InheritedObjectTypeName == NULL, "InheritedObjectTypeName wrong\n");
559 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
560 }
561
562 /* If the first isn't defined, assume none is */
563 #ifndef SE_MIN_WELL_KNOWN_PRIVILEGE
564 #define SE_MIN_WELL_KNOWN_PRIVILEGE 2L
565 #define SE_CREATE_TOKEN_PRIVILEGE 2L
566 #define SE_ASSIGNPRIMARYTOKEN_PRIVILEGE 3L
567 #define SE_LOCK_MEMORY_PRIVILEGE 4L
568 #define SE_INCREASE_QUOTA_PRIVILEGE 5L
569 #define SE_MACHINE_ACCOUNT_PRIVILEGE 6L
570 #define SE_TCB_PRIVILEGE 7L
571 #define SE_SECURITY_PRIVILEGE 8L
572 #define SE_TAKE_OWNERSHIP_PRIVILEGE 9L
573 #define SE_LOAD_DRIVER_PRIVILEGE 10L
574 #define SE_SYSTEM_PROFILE_PRIVILEGE 11L
575 #define SE_SYSTEMTIME_PRIVILEGE 12L
576 #define SE_PROF_SINGLE_PROCESS_PRIVILEGE 13L
577 #define SE_INC_BASE_PRIORITY_PRIVILEGE 14L
578 #define SE_CREATE_PAGEFILE_PRIVILEGE 15L
579 #define SE_CREATE_PERMANENT_PRIVILEGE 16L
580 #define SE_BACKUP_PRIVILEGE 17L
581 #define SE_RESTORE_PRIVILEGE 18L
582 #define SE_SHUTDOWN_PRIVILEGE 19L
583 #define SE_DEBUG_PRIVILEGE 20L
584 #define SE_AUDIT_PRIVILEGE 21L
585 #define SE_SYSTEM_ENVIRONMENT_PRIVILEGE 22L
586 #define SE_CHANGE_NOTIFY_PRIVILEGE 23L
587 #define SE_REMOTE_SHUTDOWN_PRIVILEGE 24L
588 #define SE_UNDOCK_PRIVILEGE 25L
589 #define SE_SYNC_AGENT_PRIVILEGE 26L
590 #define SE_ENABLE_DELEGATION_PRIVILEGE 27L
591 #define SE_MANAGE_VOLUME_PRIVILEGE 28L
592 #define SE_IMPERSONATE_PRIVILEGE 29L
593 #define SE_CREATE_GLOBAL_PRIVILEGE 30L
594 #define SE_MAX_WELL_KNOWN_PRIVILEGE SE_CREATE_GLOBAL_PRIVILEGE
595 #endif /* ndef SE_MIN_WELL_KNOWN_PRIVILEGE */
596
597 static void test_allocateLuid(void)
598 {
599 BOOL (WINAPI *pAllocateLocallyUniqueId)(PLUID);
600 LUID luid1, luid2;
601 BOOL ret;
602
603 pAllocateLocallyUniqueId = (void*)GetProcAddress(hmod, "AllocateLocallyUniqueId");
604 if (!pAllocateLocallyUniqueId) return;
605
606 ret = pAllocateLocallyUniqueId(&luid1);
607 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
608 return;
609
610 ok(ret,
611 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
612 ret = pAllocateLocallyUniqueId(&luid2);
613 ok( ret,
614 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
615 ok(luid1.LowPart > SE_MAX_WELL_KNOWN_PRIVILEGE || luid1.HighPart != 0,
616 "AllocateLocallyUniqueId returned a well-known LUID\n");
617 ok(luid1.LowPart != luid2.LowPart || luid1.HighPart != luid2.HighPart,
618 "AllocateLocallyUniqueId returned non-unique LUIDs\n");
619 ret = pAllocateLocallyUniqueId(NULL);
620 ok( !ret && GetLastError() == ERROR_NOACCESS,
621 "AllocateLocallyUniqueId(NULL) didn't return ERROR_NOACCESS: %d\n",
622 GetLastError());
623 }
624
625 static void test_lookupPrivilegeName(void)
626 {
627 BOOL (WINAPI *pLookupPrivilegeNameA)(LPCSTR, PLUID, LPSTR, LPDWORD);
628 char buf[MAX_PATH]; /* arbitrary, seems long enough */
629 DWORD cchName = sizeof(buf);
630 LUID luid = { 0, 0 };
631 LONG i;
632 BOOL ret;
633
634 /* check whether it's available first */
635 pLookupPrivilegeNameA = (void*)GetProcAddress(hmod, "LookupPrivilegeNameA");
636 if (!pLookupPrivilegeNameA) return;
637 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
638 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
639 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
640 return;
641
642 /* check with a short buffer */
643 cchName = 0;
644 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
645 ret = pLookupPrivilegeNameA(NULL, &luid, NULL, &cchName);
646 ok( !ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
647 "LookupPrivilegeNameA didn't fail with ERROR_INSUFFICIENT_BUFFER: %d\n",
648 GetLastError());
649 ok(cchName == strlen("SeCreateTokenPrivilege") + 1,
650 "LookupPrivilegeNameA returned an incorrect required length for\n"
651 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName,
652 lstrlenA("SeCreateTokenPrivilege") + 1);
653 /* check a known value and its returned length on success */
654 cchName = sizeof(buf);
655 ok(pLookupPrivilegeNameA(NULL, &luid, buf, &cchName) &&
656 cchName == strlen("SeCreateTokenPrivilege"),
657 "LookupPrivilegeNameA returned an incorrect output length for\n"
658 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName,
659 (int)strlen("SeCreateTokenPrivilege"));
660 /* check known values */
661 for (i = SE_MIN_WELL_KNOWN_PRIVILEGE; i <= SE_MAX_WELL_KNOWN_PRIVILEGE; i++)
662 {
663 luid.LowPart = i;
664 cchName = sizeof(buf);
665 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
666 ok( ret || GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
667 "LookupPrivilegeNameA(0.%d) failed: %d\n", i, GetLastError());
668 }
669 /* check a bogus LUID */
670 luid.LowPart = 0xdeadbeef;
671 cchName = sizeof(buf);
672 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
673 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
674 "LookupPrivilegeNameA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
675 GetLastError());
676 /* check on a bogus system */
677 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
678 cchName = sizeof(buf);
679 ret = pLookupPrivilegeNameA("b0gu5.Nam3", &luid, buf, &cchName);
680 ok( !ret && (GetLastError() == RPC_S_SERVER_UNAVAILABLE ||
681 GetLastError() == RPC_S_INVALID_NET_ADDR) /* w2k8 */,
682 "LookupPrivilegeNameA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
683 GetLastError());
684 }
685
686 struct NameToLUID
687 {
688 const char *name;
689 DWORD lowPart;
690 };
691
692 static void test_lookupPrivilegeValue(void)
693 {
694 static const struct NameToLUID privs[] = {
695 { "SeCreateTokenPrivilege", SE_CREATE_TOKEN_PRIVILEGE },
696 { "SeAssignPrimaryTokenPrivilege", SE_ASSIGNPRIMARYTOKEN_PRIVILEGE },
697 { "SeLockMemoryPrivilege", SE_LOCK_MEMORY_PRIVILEGE },
698 { "SeIncreaseQuotaPrivilege", SE_INCREASE_QUOTA_PRIVILEGE },
699 { "SeMachineAccountPrivilege", SE_MACHINE_ACCOUNT_PRIVILEGE },
700 { "SeTcbPrivilege", SE_TCB_PRIVILEGE },
701 { "SeSecurityPrivilege", SE_SECURITY_PRIVILEGE },
702 { "SeTakeOwnershipPrivilege", SE_TAKE_OWNERSHIP_PRIVILEGE },
703 { "SeLoadDriverPrivilege", SE_LOAD_DRIVER_PRIVILEGE },
704 { "SeSystemProfilePrivilege", SE_SYSTEM_PROFILE_PRIVILEGE },
705 { "SeSystemtimePrivilege", SE_SYSTEMTIME_PRIVILEGE },
706 { "SeProfileSingleProcessPrivilege", SE_PROF_SINGLE_PROCESS_PRIVILEGE },
707 { "SeIncreaseBasePriorityPrivilege", SE_INC_BASE_PRIORITY_PRIVILEGE },
708 { "SeCreatePagefilePrivilege", SE_CREATE_PAGEFILE_PRIVILEGE },
709 { "SeCreatePermanentPrivilege", SE_CREATE_PERMANENT_PRIVILEGE },
710 { "SeBackupPrivilege", SE_BACKUP_PRIVILEGE },
711 { "SeRestorePrivilege", SE_RESTORE_PRIVILEGE },
712 { "SeShutdownPrivilege", SE_SHUTDOWN_PRIVILEGE },
713 { "SeDebugPrivilege", SE_DEBUG_PRIVILEGE },
714 { "SeAuditPrivilege", SE_AUDIT_PRIVILEGE },
715 { "SeSystemEnvironmentPrivilege", SE_SYSTEM_ENVIRONMENT_PRIVILEGE },
716 { "SeChangeNotifyPrivilege", SE_CHANGE_NOTIFY_PRIVILEGE },
717 { "SeRemoteShutdownPrivilege", SE_REMOTE_SHUTDOWN_PRIVILEGE },
718 { "SeUndockPrivilege", SE_UNDOCK_PRIVILEGE },
719 { "SeSyncAgentPrivilege", SE_SYNC_AGENT_PRIVILEGE },
720 { "SeEnableDelegationPrivilege", SE_ENABLE_DELEGATION_PRIVILEGE },
721 { "SeManageVolumePrivilege", SE_MANAGE_VOLUME_PRIVILEGE },
722 { "SeImpersonatePrivilege", SE_IMPERSONATE_PRIVILEGE },
723 { "SeCreateGlobalPrivilege", SE_CREATE_GLOBAL_PRIVILEGE },
724 };
725 BOOL (WINAPI *pLookupPrivilegeValueA)(LPCSTR, LPCSTR, PLUID);
726 unsigned int i;
727 LUID luid;
728 BOOL ret;
729
730 /* check whether it's available first */
731 pLookupPrivilegeValueA = (void*)GetProcAddress(hmod, "LookupPrivilegeValueA");
732 if (!pLookupPrivilegeValueA) return;
733 ret = pLookupPrivilegeValueA(NULL, "SeCreateTokenPrivilege", &luid);
734 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
735 return;
736
737 /* check a bogus system name */
738 ret = pLookupPrivilegeValueA("b0gu5.Nam3", "SeCreateTokenPrivilege", &luid);
739 ok( !ret && (GetLastError() == RPC_S_SERVER_UNAVAILABLE ||
740 GetLastError() == RPC_S_INVALID_NET_ADDR) /* w2k8 */,
741 "LookupPrivilegeValueA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
742 GetLastError());
743 /* check a NULL string */
744 ret = pLookupPrivilegeValueA(NULL, 0, &luid);
745 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
746 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
747 GetLastError());
748 /* check a bogus privilege name */
749 ret = pLookupPrivilegeValueA(NULL, "SeBogusPrivilege", &luid);
750 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
751 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
752 GetLastError());
753 /* check case insensitive */
754 ret = pLookupPrivilegeValueA(NULL, "sEcREATEtOKENpRIVILEGE", &luid);
755 ok( ret,
756 "LookupPrivilegeValueA(NULL, sEcREATEtOKENpRIVILEGE, &luid) failed: %d\n",
757 GetLastError());
758 for (i = 0; i < sizeof(privs) / sizeof(privs[0]); i++)
759 {
760 /* Not all privileges are implemented on all Windows versions, so
761 * don't worry if the call fails
762 */
763 if (pLookupPrivilegeValueA(NULL, privs[i].name, &luid))
764 {
765 ok(luid.LowPart == privs[i].lowPart,
766 "LookupPrivilegeValueA returned an invalid LUID for %s\n",
767 privs[i].name);
768 }
769 }
770 }
771
772 static void test_luid(void)
773 {
774 test_allocateLuid();
775 test_lookupPrivilegeName();
776 test_lookupPrivilegeValue();
777 }
778
779 static void test_FileSecurity(void)
780 {
781 char wintmpdir [MAX_PATH];
782 char path [MAX_PATH];
783 char file [MAX_PATH];
784 HANDLE fh, token;
785 DWORD sdSize, retSize, rc, granted, priv_set_len;
786 PRIVILEGE_SET priv_set;
787 BOOL status;
788 BYTE *sd;
789 GENERIC_MAPPING mapping = { FILE_READ_DATA, FILE_WRITE_DATA, FILE_EXECUTE, FILE_ALL_ACCESS };
790 const SECURITY_INFORMATION request = OWNER_SECURITY_INFORMATION
791 | GROUP_SECURITY_INFORMATION
792 | DACL_SECURITY_INFORMATION;
793
794 if (!pGetFileSecurityA) {
795 win_skip ("GetFileSecurity is not available\n");
796 return;
797 }
798
799 if (!pSetFileSecurityA) {
800 win_skip ("SetFileSecurity is not available\n");
801 return;
802 }
803
804 if (!GetTempPathA (sizeof (wintmpdir), wintmpdir)) {
805 win_skip ("GetTempPathA failed\n");
806 return;
807 }
808
809 /* Create a temporary directory and in it a temporary file */
810 strcat (strcpy (path, wintmpdir), "rary");
811 SetLastError(0xdeadbeef);
812 rc = CreateDirectoryA (path, NULL);
813 ok (rc || GetLastError() == ERROR_ALREADY_EXISTS, "CreateDirectoryA "
814 "failed for '%s' with %d\n", path, GetLastError());
815
816 strcat (strcpy (file, path), "\\ess");
817 SetLastError(0xdeadbeef);
818 fh = CreateFileA (file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
819 ok (fh != INVALID_HANDLE_VALUE, "CreateFileA "
820 "failed for '%s' with %d\n", file, GetLastError());
821 CloseHandle (fh);
822
823 /* For the temporary file ... */
824
825 /* Get size needed */
826 retSize = 0;
827 SetLastError(0xdeadbeef);
828 rc = pGetFileSecurityA (file, request, NULL, 0, &retSize);
829 if (!rc && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)) {
830 win_skip("GetFileSecurityA is not implemented\n");
831 goto cleanup;
832 }
833 ok (!rc, "GetFileSecurityA "
834 "was expected to fail for '%s'\n", file);
835 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetFileSecurityA "
836 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
837 ok (retSize > sizeof (SECURITY_DESCRIPTOR), "GetFileSecurityA returned size %d\n", retSize);
838
839 sdSize = retSize;
840 sd = HeapAlloc (GetProcessHeap (), 0, sdSize);
841
842 /* Get security descriptor for real */
843 retSize = -1;
844 SetLastError(0xdeadbeef);
845 rc = pGetFileSecurityA (file, request, sd, sdSize, &retSize);
846 ok (rc, "GetFileSecurityA "
847 "was not expected to fail '%s': %d\n", file, GetLastError());
848 ok (retSize == sdSize ||
849 broken(retSize == 0), /* NT4 */
850 "GetFileSecurityA returned size %d; expected %d\n", retSize, sdSize);
851
852 /* Use it to set security descriptor */
853 SetLastError(0xdeadbeef);
854 rc = pSetFileSecurityA (file, request, sd);
855 ok (rc, "SetFileSecurityA "
856 "was not expected to fail '%s': %d\n", file, GetLastError());
857
858 HeapFree (GetProcessHeap (), 0, sd);
859
860 /* Repeat for the temporary directory ... */
861
862 /* Get size needed */
863 retSize = 0;
864 SetLastError(0xdeadbeef);
865 rc = pGetFileSecurityA (path, request, NULL, 0, &retSize);
866 ok (!rc, "GetFileSecurityA "
867 "was expected to fail for '%s'\n", path);
868 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetFileSecurityA "
869 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
870 ok (retSize > sizeof (SECURITY_DESCRIPTOR), "GetFileSecurityA returned size %d\n", retSize);
871
872 sdSize = retSize;
873 sd = HeapAlloc (GetProcessHeap (), 0, sdSize);
874
875 /* Get security descriptor for real */
876 retSize = -1;
877 SetLastError(0xdeadbeef);
878 rc = pGetFileSecurityA (path, request, sd, sdSize, &retSize);
879 ok (rc, "GetFileSecurityA "
880 "was not expected to fail '%s': %d\n", path, GetLastError());
881 ok (retSize == sdSize ||
882 broken(retSize == 0), /* NT4 */
883 "GetFileSecurityA returned size %d; expected %d\n", retSize, sdSize);
884
885 /* Use it to set security descriptor */
886 SetLastError(0xdeadbeef);
887 rc = pSetFileSecurityA (path, request, sd);
888 ok (rc, "SetFileSecurityA "
889 "was not expected to fail '%s': %d\n", path, GetLastError());
890 HeapFree (GetProcessHeap (), 0, sd);
891
892 /* Old test */
893 strcpy (wintmpdir, "\\Should not exist");
894 SetLastError(0xdeadbeef);
895 rc = pGetFileSecurityA (wintmpdir, OWNER_SECURITY_INFORMATION, NULL, 0, &sdSize);
896 ok (!rc, "GetFileSecurityA should fail for not existing directories/files\n");
897 ok (GetLastError() == ERROR_FILE_NOT_FOUND,
898 "last error ERROR_FILE_NOT_FOUND expected, got %d\n", GetLastError());
899
900 cleanup:
901 /* Remove temporary file and directory */
902 DeleteFileA(file);
903 RemoveDirectoryA(path);
904
905 /* Test file access permissions for a file with FILE_ATTRIBUTE_ARCHIVE */
906 SetLastError(0xdeadbeef);
907 rc = GetTempPathA(sizeof(wintmpdir), wintmpdir);
908 ok(rc, "GetTempPath error %d\n", GetLastError());
909
910 SetLastError(0xdeadbeef);
911 rc = GetTempFileNameA(wintmpdir, "tmp", 0, file);
912 ok(rc, "GetTempFileName error %d\n", GetLastError());
913
914 rc = GetFileAttributesA(file);
915 rc &= ~(FILE_ATTRIBUTE_NOT_CONTENT_INDEXED|FILE_ATTRIBUTE_COMPRESSED);
916 ok(rc == FILE_ATTRIBUTE_ARCHIVE, "expected FILE_ATTRIBUTE_ARCHIVE got %#x\n", rc);
917
918 rc = GetFileSecurityA(file, OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION,
919 NULL, 0, &sdSize);
920 ok(!rc, "GetFileSecurity should fail\n");
921 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
922 "expected ERROR_INSUFFICIENT_BUFFER got %d\n", GetLastError());
923 ok(sdSize > sizeof(SECURITY_DESCRIPTOR), "got sd size %d\n", sdSize);
924
925 sd = HeapAlloc(GetProcessHeap (), 0, sdSize);
926 retSize = 0xdeadbeef;
927 SetLastError(0xdeadbeef);
928 rc = GetFileSecurityA(file, OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION,
929 sd, sdSize, &retSize);
930 ok(rc, "GetFileSecurity error %d\n", GetLastError());
931 ok(retSize == sdSize || broken(retSize == 0) /* NT4 */, "expected %d, got %d\n", sdSize, retSize);
932
933 SetLastError(0xdeadbeef);
934 rc = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE, &token);
935 ok(!rc, "OpenThreadToken should fail\n");
936 ok(GetLastError() == ERROR_NO_TOKEN, "expected ERROR_NO_TOKEN, got %d\n", GetLastError());
937
938 SetLastError(0xdeadbeef);
939 rc = ImpersonateSelf(SecurityIdentification);
940 ok(rc, "ImpersonateSelf error %d\n", GetLastError());
941
942 SetLastError(0xdeadbeef);
943 rc = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE, &token);
944 ok(rc, "OpenThreadToken error %d\n", GetLastError());
945
946 SetLastError(0xdeadbeef);
947 rc = RevertToSelf();
948 ok(rc, "RevertToSelf error %d\n", GetLastError());
949
950 priv_set_len = sizeof(priv_set);
951 granted = 0xdeadbeef;
952 status = 0xdeadbeef;
953 SetLastError(0xdeadbeef);
954 rc = AccessCheck(sd, token, FILE_READ_DATA, &mapping, &priv_set, &priv_set_len, &granted, &status);
955 ok(rc, "AccessCheck error %d\n", GetLastError());
956 ok(status == 1, "expected 1, got %d\n", status);
957 ok(granted == FILE_READ_DATA, "expected FILE_READ_DATA, got %#x\n", granted);
958
959 granted = 0xdeadbeef;
960 status = 0xdeadbeef;
961 SetLastError(0xdeadbeef);
962 rc = AccessCheck(sd, token, FILE_WRITE_DATA, &mapping, &priv_set, &priv_set_len, &granted, &status);
963 ok(rc, "AccessCheck error %d\n", GetLastError());
964 ok(status == 1, "expected 1, got %d\n", status);
965 ok(granted == FILE_WRITE_DATA, "expected FILE_WRITE_DATA, got %#x\n", granted);
966
967 granted = 0xdeadbeef;
968 status = 0xdeadbeef;
969 SetLastError(0xdeadbeef);
970 rc = AccessCheck(sd, token, FILE_EXECUTE, &mapping, &priv_set, &priv_set_len, &granted, &status);
971 ok(rc, "AccessCheck error %d\n", GetLastError());
972 ok(status == 1, "expected 1, got %d\n", status);
973 ok(granted == FILE_EXECUTE, "expected FILE_EXECUTE, got %#x\n", granted);
974
975 granted = 0xdeadbeef;
976 status = 0xdeadbeef;
977 SetLastError(0xdeadbeef);
978 rc = AccessCheck(sd, token, DELETE, &mapping, &priv_set, &priv_set_len, &granted, &status);
979 ok(rc, "AccessCheck error %d\n", GetLastError());
980 ok(status == 1, "expected 1, got %d\n", status);
981 ok(granted == DELETE, "expected DELETE, got %#x\n", granted);
982
983 granted = 0xdeadbeef;
984 status = 0xdeadbeef;
985 SetLastError(0xdeadbeef);
986 rc = AccessCheck(sd, token, FILE_DELETE_CHILD, &mapping, &priv_set, &priv_set_len, &granted, &status);
987 ok(rc, "AccessCheck error %d\n", GetLastError());
988 ok(status == 1, "expected 1, got %d\n", status);
989 ok(granted == FILE_DELETE_CHILD, "expected FILE_DELETE_CHILD, got %#x\n", granted);
990
991 granted = 0xdeadbeef;
992 status = 0xdeadbeef;
993 SetLastError(0xdeadbeef);
994 rc = AccessCheck(sd, token, 0x1ff, &mapping, &priv_set, &priv_set_len, &granted, &status);
995 ok(rc, "AccessCheck error %d\n", GetLastError());
996 ok(status == 1, "expected 1, got %d\n", status);
997 ok(granted == 0x1ff, "expected 0x1ff, got %#x\n", granted);
998
999 granted = 0xdeadbeef;
1000 status = 0xdeadbeef;
1001 SetLastError(0xdeadbeef);
1002 rc = AccessCheck(sd, token, FILE_ALL_ACCESS, &mapping, &priv_set, &priv_set_len, &granted, &status);
1003 ok(rc, "AccessCheck error %d\n", GetLastError());
1004 ok(status == 1, "expected 1, got %d\n", status);
1005 ok(granted == FILE_ALL_ACCESS, "expected FILE_ALL_ACCESS, got %#x\n", granted);
1006
1007 SetLastError(0xdeadbeef);
1008 rc = AccessCheck(sd, token, 0xffffffff, &mapping, &priv_set, &priv_set_len, &granted, &status);
1009 ok(!rc, "AccessCheck should fail\n");
1010 ok(GetLastError() == ERROR_GENERIC_NOT_MAPPED, "expected ERROR_GENERIC_NOT_MAPPED, got %d\n", GetLastError());
1011
1012 /* Test file access permissions for a file with FILE_ATTRIBUTE_READONLY */
1013 SetLastError(0xdeadbeef);
1014 fh = CreateFileA(file, FILE_READ_DATA, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_READONLY, 0);
1015 ok(fh != INVALID_HANDLE_VALUE, "CreateFile error %d\n", GetLastError());
1016 retSize = 0xdeadbeef;
1017 SetLastError(0xdeadbeef);
1018 rc = WriteFile(fh, "1", 1, &retSize, NULL);
1019 ok(!rc, "WriteFile should fail\n");
1020 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1021 ok(retSize == 0, "expected 0, got %d\n", retSize);
1022 CloseHandle(fh);
1023
1024 rc = GetFileAttributesA(file);
1025 rc &= ~(FILE_ATTRIBUTE_NOT_CONTENT_INDEXED|FILE_ATTRIBUTE_COMPRESSED);
1026 todo_wine
1027 ok(rc == (FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY),
1028 "expected FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY got %#x\n", rc);
1029
1030 SetLastError(0xdeadbeef);
1031 rc = SetFileAttributesA(file, FILE_ATTRIBUTE_ARCHIVE);
1032 ok(rc, "SetFileAttributes error %d\n", GetLastError());
1033 SetLastError(0xdeadbeef);
1034 rc = DeleteFileA(file);
1035 ok(rc, "DeleteFile error %d\n", GetLastError());
1036
1037 SetLastError(0xdeadbeef);
1038 fh = CreateFileA(file, FILE_READ_DATA, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_READONLY, 0);
1039 ok(fh != INVALID_HANDLE_VALUE, "CreateFile error %d\n", GetLastError());
1040 retSize = 0xdeadbeef;
1041 SetLastError(0xdeadbeef);
1042 rc = WriteFile(fh, "1", 1, &retSize, NULL);
1043 ok(!rc, "WriteFile should fail\n");
1044 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1045 ok(retSize == 0, "expected 0, got %d\n", retSize);
1046 CloseHandle(fh);
1047
1048 rc = GetFileAttributesA(file);
1049 rc &= ~(FILE_ATTRIBUTE_NOT_CONTENT_INDEXED|FILE_ATTRIBUTE_COMPRESSED);
1050 ok(rc == (FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY),
1051 "expected FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY got %#x\n", rc);
1052
1053 retSize = 0xdeadbeef;
1054 SetLastError(0xdeadbeef);
1055 rc = GetFileSecurityA(file, OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION,
1056 sd, sdSize, &retSize);
1057 ok(rc, "GetFileSecurity error %d\n", GetLastError());
1058 ok(retSize == sdSize || broken(retSize == 0) /* NT4 */, "expected %d, got %d\n", sdSize, retSize);
1059
1060 priv_set_len = sizeof(priv_set);
1061 granted = 0xdeadbeef;
1062 status = 0xdeadbeef;
1063 SetLastError(0xdeadbeef);
1064 rc = AccessCheck(sd, token, FILE_READ_DATA, &mapping, &priv_set, &priv_set_len, &granted, &status);
1065 ok(rc, "AccessCheck error %d\n", GetLastError());
1066 ok(status == 1, "expected 1, got %d\n", status);
1067 ok(granted == FILE_READ_DATA, "expected FILE_READ_DATA, got %#x\n", granted);
1068
1069 granted = 0xdeadbeef;
1070 status = 0xdeadbeef;
1071 SetLastError(0xdeadbeef);
1072 rc = AccessCheck(sd, token, FILE_WRITE_DATA, &mapping, &priv_set, &priv_set_len, &granted, &status);
1073 ok(rc, "AccessCheck error %d\n", GetLastError());
1074 todo_wine {
1075 ok(status == 1, "expected 1, got %d\n", status);
1076 ok(granted == FILE_WRITE_DATA, "expected FILE_WRITE_DATA, got %#x\n", granted);
1077 }
1078 granted = 0xdeadbeef;
1079 status = 0xdeadbeef;
1080 SetLastError(0xdeadbeef);
1081 rc = AccessCheck(sd, token, FILE_EXECUTE, &mapping, &priv_set, &priv_set_len, &granted, &status);
1082 ok(rc, "AccessCheck error %d\n", GetLastError());
1083 ok(status == 1, "expected 1, got %d\n", status);
1084 ok(granted == FILE_EXECUTE, "expected FILE_EXECUTE, got %#x\n", granted);
1085
1086 granted = 0xdeadbeef;
1087 status = 0xdeadbeef;
1088 SetLastError(0xdeadbeef);
1089 rc = AccessCheck(sd, token, DELETE, &mapping, &priv_set, &priv_set_len, &granted, &status);
1090 ok(rc, "AccessCheck error %d\n", GetLastError());
1091 todo_wine {
1092 ok(status == 1, "expected 1, got %d\n", status);
1093 ok(granted == DELETE, "expected DELETE, got %#x\n", granted);
1094 }
1095 granted = 0xdeadbeef;
1096 status = 0xdeadbeef;
1097 SetLastError(0xdeadbeef);
1098 rc = AccessCheck(sd, token, FILE_DELETE_CHILD, &mapping, &priv_set, &priv_set_len, &granted, &status);
1099 ok(rc, "AccessCheck error %d\n", GetLastError());
1100 todo_wine {
1101 ok(status == 1, "expected 1, got %d\n", status);
1102 ok(granted == FILE_DELETE_CHILD, "expected FILE_DELETE_CHILD, got %#x\n", granted);
1103 }
1104 granted = 0xdeadbeef;
1105 status = 0xdeadbeef;
1106 SetLastError(0xdeadbeef);
1107 rc = AccessCheck(sd, token, 0x1ff, &mapping, &priv_set, &priv_set_len, &granted, &status);
1108 ok(rc, "AccessCheck error %d\n", GetLastError());
1109 todo_wine {
1110 ok(status == 1, "expected 1, got %d\n", status);
1111 ok(granted == 0x1ff, "expected 0x1ff, got %#x\n", granted);
1112 }
1113 granted = 0xdeadbeef;
1114 status = 0xdeadbeef;
1115 SetLastError(0xdeadbeef);
1116 rc = AccessCheck(sd, token, FILE_ALL_ACCESS, &mapping, &priv_set, &priv_set_len, &granted, &status);
1117 ok(rc, "AccessCheck error %d\n", GetLastError());
1118 todo_wine {
1119 ok(status == 1, "expected 1, got %d\n", status);
1120 ok(granted == FILE_ALL_ACCESS, "expected FILE_ALL_ACCESS, got %#x\n", granted);
1121 }
1122 SetLastError(0xdeadbeef);
1123 rc = DeleteFileA(file);
1124 ok(!rc, "DeleteFile should fail\n");
1125 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1126 SetLastError(0xdeadbeef);
1127 rc = SetFileAttributesA(file, FILE_ATTRIBUTE_ARCHIVE);
1128 ok(rc, "SetFileAttributes error %d\n", GetLastError());
1129 SetLastError(0xdeadbeef);
1130 rc = DeleteFileA(file);
1131 ok(rc, "DeleteFile error %d\n", GetLastError());
1132
1133 CloseHandle(token);
1134 HeapFree(GetProcessHeap(), 0, sd);
1135 }
1136
1137 static void test_AccessCheck(void)
1138 {
1139 PSID EveryoneSid = NULL, AdminSid = NULL, UsersSid = NULL;
1140 PACL Acl = NULL;
1141 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL;
1142 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
1143 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
1144 GENERIC_MAPPING Mapping = { KEY_READ, KEY_WRITE, KEY_EXECUTE, KEY_ALL_ACCESS };
1145 ACCESS_MASK Access;
1146 BOOL AccessStatus;
1147 HANDLE Token;
1148 HANDLE ProcessToken;
1149 BOOL ret;
1150 DWORD PrivSetLen;
1151 PRIVILEGE_SET *PrivSet;
1152 BOOL res;
1153 HMODULE NtDllModule;
1154 BOOLEAN Enabled;
1155 DWORD err;
1156 NTSTATUS ntret, ntAccessStatus;
1157
1158 NtDllModule = GetModuleHandleA("ntdll.dll");
1159 if (!NtDllModule)
1160 {
1161 skip("not running on NT, skipping test\n");
1162 return;
1163 }
1164 pRtlAdjustPrivilege = (void *)GetProcAddress(NtDllModule, "RtlAdjustPrivilege");
1165 if (!pRtlAdjustPrivilege)
1166 {
1167 win_skip("missing RtlAdjustPrivilege, skipping test\n");
1168 return;
1169 }
1170
1171 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
1172 res = InitializeAcl(Acl, 256, ACL_REVISION);
1173 if(!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1174 {
1175 skip("ACLs not implemented - skipping tests\n");
1176 HeapFree(GetProcessHeap(), 0, Acl);
1177 return;
1178 }
1179 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
1180
1181 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
1182 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1183
1184 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
1185 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdminSid);
1186 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1187
1188 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
1189 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &UsersSid);
1190 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1191
1192 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
1193
1194 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
1195 ok(res, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
1196
1197 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
1198 ok(res, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1199
1200 PrivSetLen = FIELD_OFFSET(PRIVILEGE_SET, Privilege[16]);
1201 PrivSet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, PrivSetLen);
1202 PrivSet->PrivilegeCount = 16;
1203
1204 res = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &ProcessToken);
1205 ok(res, "OpenProcessToken failed with error %d\n", GetLastError());
1206
1207 pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, FALSE, TRUE, &Enabled);
1208
1209 res = DuplicateToken(ProcessToken, SecurityImpersonation, &Token);
1210 ok(res, "DuplicateToken failed with error %d\n", GetLastError());
1211
1212 /* SD without owner/group */
1213 SetLastError(0xdeadbeef);
1214 Access = AccessStatus = 0x1abe11ed;
1215 ret = AccessCheck(SecurityDescriptor, Token, KEY_QUERY_VALUE, &Mapping,
1216 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1217 err = GetLastError();
1218 ok(!ret && err == ERROR_INVALID_SECURITY_DESCR, "AccessCheck should have "
1219 "failed with ERROR_INVALID_SECURITY_DESCR, instead of %d\n", err);
1220 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
1221 "Access and/or AccessStatus were changed!\n");
1222
1223 /* Set owner and group */
1224 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
1225 ok(res, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
1226 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, TRUE);
1227 ok(res, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
1228
1229 /* Generic access mask */
1230 SetLastError(0xdeadbeef);
1231 Access = AccessStatus = 0x1abe11ed;
1232 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1233 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1234 err = GetLastError();
1235 ok(!ret && err == ERROR_GENERIC_NOT_MAPPED, "AccessCheck should have failed "
1236 "with ERROR_GENERIC_NOT_MAPPED, instead of %d\n", err);
1237 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
1238 "Access and/or AccessStatus were changed!\n");
1239
1240 /* Generic access mask - no privilegeset buffer */
1241 SetLastError(0xdeadbeef);
1242 Access = AccessStatus = 0x1abe11ed;
1243 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1244 NULL, &PrivSetLen, &Access, &AccessStatus);
1245 err = GetLastError();
1246 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
1247 "with ERROR_NOACCESS, instead of %d\n", err);
1248 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
1249 "Access and/or AccessStatus were changed!\n");
1250
1251 /* Generic access mask - no returnlength */
1252 SetLastError(0xdeadbeef);
1253 Access = AccessStatus = 0x1abe11ed;
1254 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1255 PrivSet, NULL, &Access, &AccessStatus);
1256 err = GetLastError();
1257 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
1258 "with ERROR_NOACCESS, instead of %d\n", err);
1259 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
1260 "Access and/or AccessStatus were changed!\n");
1261
1262 /* Generic access mask - no privilegeset buffer, no returnlength */
1263 SetLastError(0xdeadbeef);
1264 Access = AccessStatus = 0x1abe11ed;
1265 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1266 NULL, NULL, &Access, &AccessStatus);
1267 err = GetLastError();
1268 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
1269 "with ERROR_NOACCESS, instead of %d\n", err);
1270 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
1271 "Access and/or AccessStatus were changed!\n");
1272
1273 /* sd with no dacl present */
1274 Access = AccessStatus = 0x1abe11ed;
1275 ret = SetSecurityDescriptorDacl(SecurityDescriptor, FALSE, NULL, FALSE);
1276 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1277 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1278 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1279 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1280 ok(AccessStatus && (Access == KEY_READ),
1281 "AccessCheck failed to grant access with error %d\n",
1282 GetLastError());
1283
1284 /* sd with no dacl present - no privilegeset buffer */
1285 SetLastError(0xdeadbeef);
1286 Access = AccessStatus = 0x1abe11ed;
1287 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1288 NULL, &PrivSetLen, &Access, &AccessStatus);
1289 err = GetLastError();
1290 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
1291 "with ERROR_NOACCESS, instead of %d\n", err);
1292 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
1293 "Access and/or AccessStatus were changed!\n");
1294
1295 if(pNtAccessCheck)
1296 {
1297 /* Generic access mask - no privilegeset buffer */
1298 SetLastError(0xdeadbeef);
1299 Access = ntAccessStatus = 0x1abe11ed;
1300 ntret = pNtAccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1301 NULL, &PrivSetLen, &Access, &ntAccessStatus);
1302 err = GetLastError();
1303 ok(ntret == STATUS_ACCESS_VIOLATION,
1304 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret);
1305 ok(err == 0xdeadbeef,
1306 "NtAccessCheck shouldn't set last error, got %d\n", err);
1307 ok(Access == 0x1abe11ed && ntAccessStatus == 0x1abe11ed,
1308 "Access and/or AccessStatus were changed!\n");
1309
1310 /* Generic access mask - no returnlength */
1311 SetLastError(0xdeadbeef);
1312 Access = ntAccessStatus = 0x1abe11ed;
1313 ntret = pNtAccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1314 PrivSet, NULL, &Access, &ntAccessStatus);
1315 err = GetLastError();
1316 ok(ntret == STATUS_ACCESS_VIOLATION,
1317 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret);
1318 ok(err == 0xdeadbeef,
1319 "NtAccessCheck shouldn't set last error, got %d\n", err);
1320 ok(Access == 0x1abe11ed && ntAccessStatus == 0x1abe11ed,
1321 "Access and/or AccessStatus were changed!\n");
1322
1323 /* Generic access mask - no privilegeset buffer, no returnlength */
1324 SetLastError(0xdeadbeef);
1325 Access = ntAccessStatus = 0x1abe11ed;
1326 ntret = pNtAccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1327 NULL, NULL, &Access, &ntAccessStatus);
1328 err = GetLastError();
1329 ok(ntret == STATUS_ACCESS_VIOLATION,
1330 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret);
1331 ok(err == 0xdeadbeef,
1332 "NtAccessCheck shouldn't set last error, got %d\n", err);
1333 ok(Access == 0x1abe11ed && ntAccessStatus == 0x1abe11ed,
1334 "Access and/or AccessStatus were changed!\n");
1335 }
1336 else
1337 win_skip("NtAccessCheck unavailable. Skipping.\n");
1338
1339 /* sd with NULL dacl */
1340 Access = AccessStatus = 0x1abe11ed;
1341 ret = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, NULL, FALSE);
1342 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1343 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1344 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1345 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1346 ok(AccessStatus && (Access == KEY_READ),
1347 "AccessCheck failed to grant access with error %d\n",
1348 GetLastError());
1349
1350 /* sd with blank dacl */
1351 ret = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
1352 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1353 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1354 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1355 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1356 err = GetLastError();
1357 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
1358 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
1359 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
1360
1361 res = AddAccessAllowedAce(Acl, ACL_REVISION, KEY_READ, EveryoneSid);
1362 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
1363
1364 res = AddAccessDeniedAce(Acl, ACL_REVISION, KEY_SET_VALUE, AdminSid);
1365 ok(res, "AddAccessDeniedAce failed with error %d\n", GetLastError());
1366
1367 /* sd with dacl */
1368 Access = AccessStatus = 0x1abe11ed;
1369 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1370 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1371 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1372 ok(AccessStatus && (Access == KEY_READ),
1373 "AccessCheck failed to grant access with error %d\n",
1374 GetLastError());
1375
1376 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
1377 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1378 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1379 ok(AccessStatus,
1380 "AccessCheck failed to grant any access with error %d\n",
1381 GetLastError());
1382 trace("AccessCheck with MAXIMUM_ALLOWED got Access 0x%08x\n", Access);
1383
1384 /* Access denied by SD */
1385 SetLastError(0xdeadbeef);
1386 Access = AccessStatus = 0x1abe11ed;
1387 ret = AccessCheck(SecurityDescriptor, Token, KEY_WRITE, &Mapping,
1388 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1389 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1390 err = GetLastError();
1391 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
1392 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
1393 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
1394
1395 SetLastError(0);
1396 PrivSet->PrivilegeCount = 16;
1397 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
1398 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1399 ok(ret && !AccessStatus && GetLastError() == ERROR_PRIVILEGE_NOT_HELD,
1400 "AccessCheck should have failed with ERROR_PRIVILEGE_NOT_HELD, instead of %d\n",
1401 GetLastError());
1402
1403 ret = ImpersonateLoggedOnUser(Token);
1404 ok(ret, "ImpersonateLoggedOnUser failed with error %d\n", GetLastError());
1405 ret = pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, TRUE, TRUE, &Enabled);
1406 if (!ret)
1407 {
1408 SetLastError(0);
1409 PrivSet->PrivilegeCount = 16;
1410 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
1411 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1412 ok(ret && AccessStatus && GetLastError() == 0,
1413 "AccessCheck should have succeeded, error %d\n",
1414 GetLastError());
1415 ok(Access == ACCESS_SYSTEM_SECURITY,
1416 "Access should be equal to ACCESS_SYSTEM_SECURITY instead of 0x%08x\n",
1417 Access);
1418 }
1419 else
1420 trace("Couldn't get SE_SECURITY_PRIVILEGE (0x%08x), skipping ACCESS_SYSTEM_SECURITY test\n",
1421 ret);
1422 ret = RevertToSelf();
1423 ok(ret, "RevertToSelf failed with error %d\n", GetLastError());
1424
1425 /* test INHERIT_ONLY_ACE */
1426 ret = InitializeAcl(Acl, 256, ACL_REVISION);
1427 ok(ret, "InitializeAcl failed with error %d\n", GetLastError());
1428
1429 /* NT doesn't have AddAccessAllowedAceEx. Skipping this call/test doesn't influence
1430 * the next ones.
1431 */
1432 if (pAddAccessAllowedAceEx)
1433 {
1434 ret = pAddAccessAllowedAceEx(Acl, ACL_REVISION, INHERIT_ONLY_ACE, KEY_READ, EveryoneSid);
1435 ok(ret, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
1436 }
1437 else
1438 win_skip("AddAccessAllowedAceEx is not available\n");
1439
1440 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1441 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1442 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1443 err = GetLastError();
1444 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
1445 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
1446 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
1447
1448 CloseHandle(Token);
1449
1450 res = DuplicateToken(ProcessToken, SecurityAnonymous, &Token);
1451 ok(res, "DuplicateToken failed with error %d\n", GetLastError());
1452
1453 SetLastError(0xdeadbeef);
1454 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
1455 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1456 err = GetLastError();
1457 ok(!ret && err == ERROR_BAD_IMPERSONATION_LEVEL, "AccessCheck should have failed "
1458 "with ERROR_BAD_IMPERSONATION_LEVEL, instead of %d\n", err);
1459
1460 CloseHandle(Token);
1461
1462 SetLastError(0xdeadbeef);
1463 ret = AccessCheck(SecurityDescriptor, ProcessToken, KEY_READ, &Mapping,
1464 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1465 err = GetLastError();
1466 ok(!ret && err == ERROR_NO_IMPERSONATION_TOKEN, "AccessCheck should have failed "
1467 "with ERROR_NO_IMPERSONATION_TOKEN, instead of %d\n", err);
1468
1469 CloseHandle(ProcessToken);
1470
1471 if (EveryoneSid)
1472 FreeSid(EveryoneSid);
1473 if (AdminSid)
1474 FreeSid(AdminSid);
1475 if (UsersSid)
1476 FreeSid(UsersSid);
1477 HeapFree(GetProcessHeap(), 0, Acl);
1478 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
1479 HeapFree(GetProcessHeap(), 0, PrivSet);
1480 }
1481
1482 /* test GetTokenInformation for the various attributes */
1483 static void test_token_attr(void)
1484 {
1485 HANDLE Token, ImpersonationToken;
1486 DWORD Size, Size2;
1487 TOKEN_PRIVILEGES *Privileges;
1488 TOKEN_GROUPS *Groups;
1489 TOKEN_USER *User;
1490 TOKEN_DEFAULT_DACL *Dacl;
1491 BOOL ret;
1492 DWORD i, GLE;
1493 LPSTR SidString;
1494 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel;
1495 ACL *acl;
1496
1497 /* cygwin-like use case */
1498 SetLastError(0xdeadbeef);
1499 ret = OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED, &Token);
1500 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1501 {
1502 win_skip("OpenProcessToken is not implemented\n");
1503 return;
1504 }
1505 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1506 if (ret)
1507 {
1508 DWORD buf[256]; /* GetTokenInformation wants a dword-aligned buffer */
1509 Size = sizeof(buf);
1510 ret = GetTokenInformation(Token, TokenUser,(void*)buf, Size, &Size);
1511 ok(ret, "GetTokenInformation failed with error %d\n", GetLastError());
1512 Size = sizeof(ImpersonationLevel);
1513 ret = GetTokenInformation(Token, TokenImpersonationLevel, &ImpersonationLevel, Size, &Size);
1514 GLE = GetLastError();
1515 ok(!ret && (GLE == ERROR_INVALID_PARAMETER), "GetTokenInformation(TokenImpersonationLevel) on primary token should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GLE);
1516 CloseHandle(Token);
1517 }
1518
1519 if(!pConvertSidToStringSidA)
1520 {
1521 win_skip("ConvertSidToStringSidA is not available\n");
1522 return;
1523 }
1524
1525 SetLastError(0xdeadbeef);
1526 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &Token);
1527 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1528
1529 /* groups */
1530 /* insufficient buffer length */
1531 SetLastError(0xdeadbeef);
1532 Size2 = 0;
1533 ret = GetTokenInformation(Token, TokenGroups, NULL, 0, &Size2);
1534 ok(Size2 > 1, "got %d\n", Size2);
1535 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1536 "%d with error %d\n", ret, GetLastError());
1537 Size2 -= 1;
1538 Groups = HeapAlloc(GetProcessHeap(), 0, Size2);
1539 memset(Groups, 0xcc, Size2);
1540 Size = 0;
1541 ret = GetTokenInformation(Token, TokenGroups, Groups, Size2, &Size);
1542 ok(Size > 1, "got %d\n", Size);
1543 ok((!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER) || broken(ret) /* wow64 */,
1544 "%d with error %d\n", ret, GetLastError());
1545 if(!ret)
1546 ok(*((BYTE*)Groups) == 0xcc, "buffer altered\n");
1547
1548 HeapFree(GetProcessHeap(), 0, Groups);
1549
1550 SetLastError(0xdeadbeef);
1551 ret = GetTokenInformation(Token, TokenGroups, NULL, 0, &Size);
1552 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1553 "GetTokenInformation(TokenGroups) %s with error %d\n",
1554 ret ? "succeeded" : "failed", GetLastError());
1555 Groups = HeapAlloc(GetProcessHeap(), 0, Size);
1556 SetLastError(0xdeadbeef);
1557 ret = GetTokenInformation(Token, TokenGroups, Groups, Size, &Size);
1558 ok(ret, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
1559 ok(GetLastError() == 0xdeadbeef,
1560 "GetTokenInformation shouldn't have set last error to %d\n",
1561 GetLastError());
1562 trace("TokenGroups:\n");
1563 for (i = 0; i < Groups->GroupCount; i++)
1564 {
1565 DWORD NameLength = 255;
1566 CHAR Name[255];
1567 DWORD DomainLength = 255;
1568 CHAR Domain[255];
1569 SID_NAME_USE SidNameUse;
1570 Name[0] = '\0';
1571 Domain[0] = '\0';
1572 ret = LookupAccountSidA(NULL, Groups->Groups[i].Sid, Name, &NameLength, Domain, &DomainLength, &SidNameUse);
1573 if (ret)
1574 {
1575 pConvertSidToStringSidA(Groups->Groups[i].Sid, &SidString);
1576 trace("%s, %s\\%s use: %d attr: 0x%08x\n", SidString, Domain, Name, SidNameUse, Groups->Groups[i].Attributes);
1577 LocalFree(SidString);
1578 }
1579 else trace("attr: 0x%08x LookupAccountSid failed with error %d\n", Groups->Groups[i].Attributes, GetLastError());
1580 }
1581 HeapFree(GetProcessHeap(), 0, Groups);
1582
1583 /* user */
1584 ret = GetTokenInformation(Token, TokenUser, NULL, 0, &Size);
1585 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1586 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1587 User = HeapAlloc(GetProcessHeap(), 0, Size);
1588 ret = GetTokenInformation(Token, TokenUser, User, Size, &Size);
1589 ok(ret,
1590 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1591
1592 pConvertSidToStringSidA(User->User.Sid, &SidString);
1593 trace("TokenUser: %s attr: 0x%08x\n", SidString, User->User.Attributes);
1594 LocalFree(SidString);
1595 HeapFree(GetProcessHeap(), 0, User);
1596
1597 /* privileges */
1598 ret = GetTokenInformation(Token, TokenPrivileges, NULL, 0, &Size);
1599 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1600 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1601 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
1602 ret = GetTokenInformation(Token, TokenPrivileges, Privileges, Size, &Size);
1603 ok(ret,
1604 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1605 trace("TokenPrivileges:\n");
1606 for (i = 0; i < Privileges->PrivilegeCount; i++)
1607 {
1608 CHAR Name[256];
1609 DWORD NameLen = sizeof(Name)/sizeof(Name[0]);
1610 LookupPrivilegeNameA(NULL, &Privileges->Privileges[i].Luid, Name, &NameLen);
1611 trace("\t%s, 0x%x\n", Name, Privileges->Privileges[i].Attributes);
1612 }
1613 HeapFree(GetProcessHeap(), 0, Privileges);
1614
1615 ret = DuplicateToken(Token, SecurityAnonymous, &ImpersonationToken);
1616 ok(ret, "DuplicateToken failed with error %d\n", GetLastError());
1617
1618 Size = sizeof(ImpersonationLevel);
1619 ret = GetTokenInformation(ImpersonationToken, TokenImpersonationLevel, &ImpersonationLevel, Size, &Size);
1620 ok(ret, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1621 ok(ImpersonationLevel == SecurityAnonymous, "ImpersonationLevel should have been SecurityAnonymous instead of %d\n", ImpersonationLevel);
1622
1623 CloseHandle(ImpersonationToken);
1624
1625 /* default dacl */
1626 ret = GetTokenInformation(Token, TokenDefaultDacl, NULL, 0, &Size);
1627 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1628 "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1629
1630 Dacl = HeapAlloc(GetProcessHeap(), 0, Size);
1631 ret = GetTokenInformation(Token, TokenDefaultDacl, Dacl, Size, &Size);
1632 ok(ret, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1633
1634 SetLastError(0xdeadbeef);
1635 ret = SetTokenInformation(Token, TokenDefaultDacl, NULL, 0);
1636 GLE = GetLastError();
1637 ok(!ret, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1638 ok(GLE == ERROR_BAD_LENGTH, "expected ERROR_BAD_LENGTH got %u\n", GLE);
1639
1640 SetLastError(0xdeadbeef);
1641 ret = SetTokenInformation(Token, TokenDefaultDacl, NULL, Size);
1642 GLE = GetLastError();
1643 ok(!ret, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1644 ok(GLE == ERROR_NOACCESS, "expected ERROR_NOACCESS got %u\n", GLE);
1645
1646 acl = Dacl->DefaultDacl;
1647 Dacl->DefaultDacl = NULL;
1648
1649 ret = SetTokenInformation(Token, TokenDefaultDacl, Dacl, Size);
1650 ok(ret, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1651
1652 Size2 = 0;
1653 Dacl->DefaultDacl = (ACL *)0xdeadbeef;
1654 ret = GetTokenInformation(Token, TokenDefaultDacl, Dacl, Size, &Size2);
1655 ok(ret, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1656 ok(Dacl->DefaultDacl == NULL, "expected NULL, got %p\n", Dacl->DefaultDacl);
1657 ok(Size2 == sizeof(TOKEN_DEFAULT_DACL) || broken(Size2 == 2*sizeof(TOKEN_DEFAULT_DACL)), /* WoW64 */
1658 "got %u expected sizeof(TOKEN_DEFAULT_DACL)\n", Size2);
1659
1660 Dacl->DefaultDacl = acl;
1661 ret = SetTokenInformation(Token, TokenDefaultDacl, Dacl, Size);
1662 ok(ret, "SetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1663
1664 if (Size2 == sizeof(TOKEN_DEFAULT_DACL)) {
1665 ret = GetTokenInformation(Token, TokenDefaultDacl, Dacl, Size, &Size2);
1666 ok(ret, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1667 } else
1668 win_skip("TOKEN_DEFAULT_DACL size too small on WoW64\n");
1669
1670 HeapFree(GetProcessHeap(), 0, Dacl);
1671 CloseHandle(Token);
1672 }
1673
1674 typedef union _MAX_SID
1675 {
1676 SID sid;
1677 char max[SECURITY_MAX_SID_SIZE];
1678 } MAX_SID;
1679
1680 static void test_sid_str(PSID * sid)
1681 {
1682 char *str_sid;
1683 BOOL ret = pConvertSidToStringSidA(sid, &str_sid);
1684 ok(ret, "ConvertSidToStringSidA() failed: %d\n", GetLastError());
1685 if (ret)
1686 {
1687 char account[MAX_PATH], domain[MAX_PATH];
1688 SID_NAME_USE use;
1689 DWORD acc_size = MAX_PATH;
1690 DWORD dom_size = MAX_PATH;
1691 ret = LookupAccountSidA (NULL, sid, account, &acc_size, domain, &dom_size, &use);
1692 ok(ret || GetLastError() == ERROR_NONE_MAPPED,
1693 "LookupAccountSid(%s) failed: %d\n", str_sid, GetLastError());
1694 if (ret)
1695 trace(" %s %s\\%s %d\n", str_sid, domain, account, use);
1696 else if (GetLastError() == ERROR_NONE_MAPPED)
1697 trace(" %s couldn't be mapped\n", str_sid);
1698 LocalFree(str_sid);
1699 }
1700 }
1701
1702 static const struct well_known_sid_value
1703 {
1704 BOOL without_domain;
1705 const char *sid_string;
1706 } well_known_sid_values[] = {
1707 /* 0 */ {TRUE, "S-1-0-0"}, {TRUE, "S-1-1-0"}, {TRUE, "S-1-2-0"}, {TRUE, "S-1-3-0"},
1708 /* 4 */ {TRUE, "S-1-3-1"}, {TRUE, "S-1-3-2"}, {TRUE, "S-1-3-3"}, {TRUE, "S-1-5"},
1709 /* 8 */ {FALSE, "S-1-5-1"}, {TRUE, "S-1-5-2"}, {TRUE, "S-1-5-3"}, {TRUE, "S-1-5-4"},
1710 /* 12 */ {TRUE, "S-1-5-6"}, {TRUE, "S-1-5-7"}, {TRUE, "S-1-5-8"}, {TRUE, "S-1-5-9"},
1711 /* 16 */ {TRUE, "S-1-5-10"}, {TRUE, "S-1-5-11"}, {TRUE, "S-1-5-12"}, {TRUE, "S-1-5-13"},
1712 /* 20 */ {TRUE, "S-1-5-14"}, {FALSE, NULL}, {TRUE, "S-1-5-18"}, {TRUE, "S-1-5-19"},
1713 /* 24 */ {TRUE, "S-1-5-20"}, {TRUE, "S-1-5-32"},
1714 /* 26 */ {FALSE, "S-1-5-32-544"}, {TRUE, "S-1-5-32-545"}, {TRUE, "S-1-5-32-546"},
1715 /* 29 */ {TRUE, "S-1-5-32-547"}, {TRUE, "S-1-5-32-548"}, {TRUE, "S-1-5-32-549"},
1716 /* 32 */ {TRUE, "S-1-5-32-550"}, {TRUE, "S-1-5-32-551"}, {TRUE, "S-1-5-32-552"},
1717 /* 35 */ {TRUE, "S-1-5-32-554"}, {TRUE, "S-1-5-32-555"}, {TRUE, "S-1-5-32-556"},
1718 /* 38 */ {FALSE, "S-1-5-21-12-23-34-45-56-500"}, {FALSE, "S-1-5-21-12-23-34-45-56-501"},
1719 /* 40 */ {FALSE, "S-1-5-21-12-23-34-45-56-502"}, {FALSE, "S-1-5-21-12-23-34-45-56-512"},
1720 /* 42 */ {FALSE, "S-1-5-21-12-23-34-45-56-513"}, {FALSE, "S-1-5-21-12-23-34-45-56-514"},
1721 /* 44 */ {FALSE, "S-1-5-21-12-23-34-45-56-515"}, {FALSE, "S-1-5-21-12-23-34-45-56-516"},
1722 /* 46 */ {FALSE, "S-1-5-21-12-23-34-45-56-517"}, {FALSE, "S-1-5-21-12-23-34-45-56-518"},
1723 /* 48 */ {FALSE, "S-1-5-21-12-23-34-45-56-519"}, {FALSE, "S-1-5-21-12-23-34-45-56-520"},
1724 /* 50 */ {FALSE, "S-1-5-21-12-23-34-45-56-553"},
1725 /* Added in Windows Server 2003 */
1726 /* 51 */ {TRUE, "S-1-5-64-10"}, {TRUE, "S-1-5-64-21"}, {TRUE, "S-1-5-64-14"},
1727 /* 54 */ {TRUE, "S-1-5-15"}, {TRUE, "S-1-5-1000"}, {FALSE, "S-1-5-32-557"},
1728 /* 57 */ {TRUE, "S-1-5-32-558"}, {TRUE, "S-1-5-32-559"}, {TRUE, "S-1-5-32-560"},
1729 /* 60 */ {TRUE, "S-1-5-32-561"}, {TRUE, "S-1-5-32-562"},
1730 /* Added in Windows Vista: */
1731 /* 62 */ {TRUE, "S-1-5-32-568"},
1732 /* 63 */ {TRUE, "S-1-5-17"}, {FALSE, "S-1-5-32-569"}, {TRUE, "S-1-16-0"},
1733 /* 66 */ {TRUE, "S-1-16-4096"}, {TRUE, "S-1-16-8192"}, {TRUE, "S-1-16-12288"},
1734 /* 69 */ {TRUE, "S-1-16-16384"}, {TRUE, "S-1-5-33"}, {TRUE, "S-1-3-4"},
1735 /* 72 */ {FALSE, "S-1-5-21-12-23-34-45-56-571"}, {FALSE, "S-1-5-21-12-23-34-45-56-572"},
1736 /* 74 */ {TRUE, "S-1-5-22"}, {FALSE, "S-1-5-21-12-23-34-45-56-521"}, {TRUE, "S-1-5-32-573"},
1737 /* 77 */ {FALSE, "S-1-5-21-12-23-34-45-56-498"}, {TRUE, "S-1-5-32-574"}, {TRUE, "S-1-16-8448"},
1738 /* 80 */ {FALSE, NULL}, {TRUE, "S-1-2-1"}, {TRUE, "S-1-5-65-1"}, {FALSE, NULL},
1739 /* 84 */ {TRUE, "S-1-15-2-1"},
1740 };
1741
1742 static void test_CreateWellKnownSid(void)
1743 {
1744 SID_IDENTIFIER_AUTHORITY ident = { SECURITY_NT_AUTHORITY };
1745 PSID domainsid, sid;
1746 DWORD size, error;
1747 BOOL ret;
1748 unsigned int i;
1749
1750 if (!pCreateWellKnownSid)
1751 {
1752 win_skip("CreateWellKnownSid not available\n");
1753 return;
1754 }
1755
1756 size = 0;
1757 SetLastError(0xdeadbeef);
1758 ret = pCreateWellKnownSid(WinInteractiveSid, NULL, NULL, &size);
1759 error = GetLastError();
1760 ok(!ret, "CreateWellKnownSid succeeded\n");
1761 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
1762 ok(size, "expected size > 0\n");
1763
1764 SetLastError(0xdeadbeef);
1765 ret = pCreateWellKnownSid(WinInteractiveSid, NULL, NULL, &size);
1766 error = GetLastError();
1767 ok(!ret, "CreateWellKnownSid succeeded\n");
1768 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
1769
1770 sid = HeapAlloc(GetProcessHeap(), 0, size);
1771 ret = pCreateWellKnownSid(WinInteractiveSid, NULL, sid, &size);
1772 ok(ret, "CreateWellKnownSid failed %u\n", GetLastError());
1773 HeapFree(GetProcessHeap(), 0, sid);
1774
1775 /* a domain sid usually have three subauthorities but we test that CreateWellKnownSid doesn't check it */
1776 AllocateAndInitializeSid(&ident, 6, SECURITY_NT_NON_UNIQUE, 12, 23, 34, 45, 56, 0, 0, &domainsid);
1777
1778 for (i = 0; i < sizeof(well_known_sid_values)/sizeof(well_known_sid_values[0]); i++)
1779 {
1780 const struct well_known_sid_value *value = &well_known_sid_values[i];
1781 char sid_buffer[SECURITY_MAX_SID_SIZE];
1782 LPSTR str;
1783 DWORD cb;
1784
1785 if (value->sid_string == NULL)
1786 continue;
1787
1788 /* some SIDs aren't implemented by all Windows versions - detect it */
1789 cb = sizeof(sid_buffer);
1790 if (!pCreateWellKnownSid(i, NULL, sid_buffer, &cb))
1791 {
1792 skip("Well known SID %u not implemented\n", i);
1793 continue;
1794 }
1795
1796 cb = sizeof(sid_buffer);
1797 ok(pCreateWellKnownSid(i, value->without_domain ? NULL : domainsid, sid_buffer, &cb), "Couldn't create well known sid %u\n", i);
1798 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer)), cb, DWORD, "%d");
1799 ok(IsValidSid(sid_buffer), "The sid is not valid\n");
1800 ok(pConvertSidToStringSidA(sid_buffer, &str), "Couldn't convert SID to string\n");
1801 ok(strcmp(str, value->sid_string) == 0, "%d: SID mismatch - expected %s, got %s\n", i,
1802 value->sid_string, str);
1803 LocalFree(str);
1804
1805 if (value->without_domain)
1806 {
1807 char buf2[SECURITY_MAX_SID_SIZE];
1808 cb = sizeof(buf2);
1809 ok(pCreateWellKnownSid(i, domainsid, buf2, &cb), "Couldn't create well known sid %u with optional domain\n", i);
1810 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer)), cb, DWORD, "%d");
1811 ok(memcmp(buf2, sid_buffer, cb) == 0, "SID create with domain is different than without (%u)\n", i);
1812 }
1813 }
1814
1815 FreeSid(domainsid);
1816 }
1817
1818 static void test_LookupAccountSid(void)
1819 {
1820 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
1821 CHAR accountA[MAX_PATH], domainA[MAX_PATH], usernameA[MAX_PATH];
1822 DWORD acc_sizeA, dom_sizeA, user_sizeA;
1823 DWORD real_acc_sizeA, real_dom_sizeA;
1824 WCHAR accountW[MAX_PATH], domainW[MAX_PATH];
1825 DWORD acc_sizeW, dom_sizeW;
1826 DWORD real_acc_sizeW, real_dom_sizeW;
1827 PSID pUsersSid = NULL;
1828 SID_NAME_USE use;
1829 BOOL ret;
1830 DWORD error, size, cbti = 0;
1831 MAX_SID max_sid;
1832 CHAR *str_sidA;
1833 int i;
1834 HANDLE hToken;
1835 PTOKEN_USER ptiUser = NULL;
1836
1837 /* native windows crashes if account size, domain size, or name use is NULL */
1838
1839 ret = AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
1840 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &pUsersSid);
1841 ok(ret || (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED),
1842 "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1843
1844 /* not running on NT so give up */
1845 if (!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1846 return;
1847
1848 real_acc_sizeA = MAX_PATH;
1849 real_dom_sizeA = MAX_PATH;
1850 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &real_acc_sizeA, domainA, &real_dom_sizeA, &use);
1851 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1852
1853 /* try NULL account */
1854 acc_sizeA = MAX_PATH;
1855 dom_sizeA = MAX_PATH;
1856 ret = LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
1857 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1858
1859 /* try NULL domain */
1860 acc_sizeA = MAX_PATH;
1861 dom_sizeA = MAX_PATH;
1862 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
1863 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1864
1865 /* try a small account buffer */
1866 acc_sizeA = 1;
1867 dom_sizeA = MAX_PATH;
1868 accountA[0] = 0;
1869 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1870 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1871 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1872 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1873
1874 /* try a 0 sized account buffer */
1875 acc_sizeA = 0;
1876 dom_sizeA = MAX_PATH;
1877 accountA[0] = 0;
1878 LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1879 /* this can fail or succeed depending on OS version but the size will always be returned */
1880 ok(acc_sizeA == real_acc_sizeA + 1,
1881 "LookupAccountSidA() Expected acc_size = %u, got %u\n",
1882 real_acc_sizeA + 1, acc_sizeA);
1883
1884 /* try a 0 sized account buffer */
1885 acc_sizeA = 0;
1886 dom_sizeA = MAX_PATH;
1887 LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
1888 /* this can fail or succeed depending on OS version but the size will always be returned */
1889 ok(acc_sizeA == real_acc_sizeA + 1,
1890 "LookupAccountSid() Expected acc_size = %u, got %u\n",
1891 real_acc_sizeA + 1, acc_sizeA);
1892
1893 /* try a small domain buffer */
1894 dom_sizeA = 1;
1895 acc_sizeA = MAX_PATH;
1896 accountA[0] = 0;
1897 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1898 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1899 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1900 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1901
1902 /* try a 0 sized domain buffer */
1903 dom_sizeA = 0;
1904 acc_sizeA = MAX_PATH;
1905 accountA[0] = 0;
1906 LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1907 /* this can fail or succeed depending on OS version but the size will always be returned */
1908 ok(dom_sizeA == real_dom_sizeA + 1,
1909 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1910 real_dom_sizeA + 1, dom_sizeA);
1911
1912 /* try a 0 sized domain buffer */
1913 dom_sizeA = 0;
1914 acc_sizeA = MAX_PATH;
1915 LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
1916 /* this can fail or succeed depending on OS version but the size will always be returned */
1917 ok(dom_sizeA == real_dom_sizeA + 1,
1918 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1919 real_dom_sizeA + 1, dom_sizeA);
1920
1921 real_acc_sizeW = MAX_PATH;
1922 real_dom_sizeW = MAX_PATH;
1923 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &real_acc_sizeW, domainW, &real_dom_sizeW, &use);
1924 ok(ret, "LookupAccountSidW() Expected TRUE, got FALSE\n");
1925
1926 /* try an invalid system name */
1927 real_acc_sizeA = MAX_PATH;
1928 real_dom_sizeA = MAX_PATH;
1929 ret = LookupAccountSidA("deepthought", pUsersSid, accountA, &real_acc_sizeA, domainA, &real_dom_sizeA, &use);
1930 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1931 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE || GetLastError() == RPC_S_INVALID_NET_ADDR /* Vista */,
1932 "LookupAccountSidA() Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %u\n", GetLastError());
1933
1934 /* native windows crashes if domainW or accountW is NULL */
1935
1936 /* try a small account buffer */
1937 acc_sizeW = 1;
1938 dom_sizeW = MAX_PATH;
1939 accountW[0] = 0;
1940 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1941 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1942 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1943 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1944
1945 /* try a 0 sized account buffer */
1946 acc_sizeW = 0;
1947 dom_sizeW = MAX_PATH;
1948 accountW[0] = 0;
1949 LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1950 /* this can fail or succeed depending on OS version but the size will always be returned */
1951 ok(acc_sizeW == real_acc_sizeW + 1,
1952 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1953 real_acc_sizeW + 1, acc_sizeW);
1954
1955 /* try a 0 sized account buffer */
1956 acc_sizeW = 0;
1957 dom_sizeW = MAX_PATH;
1958 LookupAccountSidW(NULL, pUsersSid, NULL, &acc_sizeW, domainW, &dom_sizeW, &use);
1959 /* this can fail or succeed depending on OS version but the size will always be returned */
1960 ok(acc_sizeW == real_acc_sizeW + 1,
1961 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1962 real_acc_sizeW + 1, acc_sizeW);
1963
1964 /* try a small domain buffer */
1965 dom_sizeW = 1;
1966 acc_sizeW = MAX_PATH;
1967 accountW[0] = 0;
1968 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1969 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1970 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1971 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1972
1973 /* try a 0 sized domain buffer */
1974 dom_sizeW = 0;
1975 acc_sizeW = MAX_PATH;
1976 accountW[0] = 0;
1977 LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1978 /* this can fail or succeed depending on OS version but the size will always be returned */
1979 ok(dom_sizeW == real_dom_sizeW + 1,
1980 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1981 real_dom_sizeW + 1, dom_sizeW);
1982
1983 /* try a 0 sized domain buffer */
1984 dom_sizeW = 0;
1985 acc_sizeW = MAX_PATH;
1986 LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, NULL, &dom_sizeW, &use);
1987 /* this can fail or succeed depending on OS version but the size will always be returned */
1988 ok(dom_sizeW == real_dom_sizeW + 1,
1989 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1990 real_dom_sizeW + 1, dom_sizeW);
1991
1992 acc_sizeW = dom_sizeW = use = 0;
1993 SetLastError(0xdeadbeef);
1994 ret = LookupAccountSidW(NULL, pUsersSid, NULL, &acc_sizeW, NULL, &dom_sizeW, &use);
1995 error = GetLastError();
1996 ok(!ret, "LookupAccountSidW failed %u\n", GetLastError());
1997 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
1998 ok(acc_sizeW, "expected non-zero account size\n");
1999 ok(dom_sizeW, "expected non-zero domain size\n");
2000 ok(!use, "expected zero use %u\n", use);
2001
2002 FreeSid(pUsersSid);
2003
2004 /* Test LookupAccountSid with Sid retrieved from token information.
2005 This assumes this process is running under the account of the current user.*/
2006 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &hToken);
2007 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
2008 ret = GetTokenInformation(hToken, TokenUser, NULL, 0, &cbti);
2009 ok(!ret, "GetTokenInformation failed with error %d\n", GetLastError());
2010 ptiUser = HeapAlloc(GetProcessHeap(), 0, cbti);
2011 if (GetTokenInformation(hToken, TokenUser, ptiUser, cbti, &cbti))
2012 {
2013 acc_sizeA = dom_sizeA = MAX_PATH;
2014 ret = LookupAccountSidA(NULL, ptiUser->User.Sid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
2015 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
2016 user_sizeA = MAX_PATH;
2017 ret = GetUserNameA(usernameA , &user_sizeA);
2018 ok(ret, "GetUserNameA() Expected TRUE, got FALSE\n");
2019 ok(lstrcmpA(usernameA, accountA) == 0, "LookupAccountSidA() Expected account name: %s got: %s\n", usernameA, accountA );
2020 }
2021 HeapFree(GetProcessHeap(), 0, ptiUser);
2022
2023 if (pCreateWellKnownSid && pConvertSidToStringSidA)
2024 {
2025 trace("Well Known SIDs:\n");
2026 for (i = 0; i <= 60; i++)
2027 {
2028 size = SECURITY_MAX_SID_SIZE;
2029 if (pCreateWellKnownSid(i, NULL, &max_sid.sid, &size))
2030 {
2031 if (pConvertSidToStringSidA(&max_sid.sid, &str_sidA))
2032 {
2033 acc_sizeA = MAX_PATH;
2034 dom_sizeA = MAX_PATH;
2035 if (LookupAccountSidA(NULL, &max_sid.sid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use))
2036 trace(" %d: %s %s\\%s %d\n", i, str_sidA, domainA, accountA, use);
2037 LocalFree(str_sidA);
2038 }
2039 }
2040 else
2041 {
2042 if (GetLastError() != ERROR_INVALID_PARAMETER)
2043 trace(" CreateWellKnownSid(%d) failed: %d\n", i, GetLastError());
2044 else
2045 trace(" %d: not supported\n", i);
2046 }
2047 }
2048
2049 pLsaQueryInformationPolicy = (void *)GetProcAddress( hmod, "LsaQueryInformationPolicy");
2050 pLsaOpenPolicy = (void *)GetProcAddress( hmod, "LsaOpenPolicy");
2051 pLsaFreeMemory = (void *)GetProcAddress( hmod, "LsaFreeMemory");
2052 pLsaClose = (void *)GetProcAddress( hmod, "LsaClose");
2053
2054 if (pLsaQueryInformationPolicy && pLsaOpenPolicy && pLsaFreeMemory && pLsaClose)
2055 {
2056 NTSTATUS status;
2057 LSA_HANDLE handle;
2058 LSA_OBJECT_ATTRIBUTES object_attributes;
2059
2060 ZeroMemory(&object_attributes, sizeof(object_attributes));
2061 object_attributes.Length = sizeof(object_attributes);
2062
2063 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_ALL_ACCESS, &handle);
2064 ok(status == STATUS_SUCCESS || status == STATUS_ACCESS_DENIED,
2065 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status);
2066
2067 /* try a more restricted access mask if necessary */
2068 if (status == STATUS_ACCESS_DENIED) {
2069 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
2070 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_VIEW_LOCAL_INFORMATION, &handle);
2071 ok(status == STATUS_SUCCESS, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status);
2072 }
2073
2074 if (status == STATUS_SUCCESS)
2075 {
2076 PPOLICY_ACCOUNT_DOMAIN_INFO info;
2077 status = pLsaQueryInformationPolicy(handle, PolicyAccountDomainInformation, (PVOID*)&info);
2078 ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy() failed, returned 0x%08x\n", status);
2079 if (status == STATUS_SUCCESS)
2080 {
2081 ok(info->DomainSid!=0, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) missing SID\n");
2082 if (info->DomainSid)
2083 {
2084 int count = *GetSidSubAuthorityCount(info->DomainSid);
2085 CopySid(GetSidLengthRequired(count), &max_sid, info->DomainSid);
2086 test_sid_str((PSID)&max_sid.sid);
2087 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_ADMIN;
2088 max_sid.sid.SubAuthorityCount = count + 1;
2089 test_sid_str((PSID)&max_sid.sid);
2090 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_GUEST;
2091 test_sid_str((PSID)&max_sid.sid);
2092 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ADMINS;
2093 test_sid_str((PSID)&max_sid.sid);
2094 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_USERS;
2095 test_sid_str((PSID)&max_sid.sid);
2096 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_GUESTS;
2097 test_sid_str((PSID)&max_sid.sid);
2098 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_COMPUTERS;
2099 test_sid_str((PSID)&max_sid.sid);
2100 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CONTROLLERS;
2101 test_sid_str((PSID)&max_sid.sid);
2102 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CERT_ADMINS;
2103 test_sid_str((PSID)&max_sid.sid);
2104 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_SCHEMA_ADMINS;
2105 test_sid_str((PSID)&max_sid.sid);
2106 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ENTERPRISE_ADMINS;
2107 test_sid_str((PSID)&max_sid.sid);
2108 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_POLICY_ADMINS;
2109 test_sid_str((PSID)&max_sid.sid);
2110 max_sid.sid.SubAuthority[count] = DOMAIN_ALIAS_RID_RAS_SERVERS;
2111 test_sid_str((PSID)&max_sid.sid);
2112 max_sid.sid.SubAuthority[count] = 1000; /* first user account */
2113 test_sid_str((PSID)&max_sid.sid);
2114 }
2115
2116 pLsaFreeMemory((LPVOID)info);
2117 }
2118
2119 status = pLsaClose(handle);
2120 ok(status == STATUS_SUCCESS, "LsaClose() failed, returned 0x%08x\n", status);
2121 }
2122 }
2123 }
2124 }
2125
2126 static BOOL get_sid_info(PSID psid, LPSTR *user, LPSTR *dom)
2127 {
2128 static CHAR account[UNLEN + 1];
2129 static CHAR domain[UNLEN + 1];
2130 DWORD size, dom_size;
2131 SID_NAME_USE use;
2132
2133 *user = account;
2134 *dom = domain;
2135
2136 size = dom_size = UNLEN + 1;
2137 account[0] = '\0';
2138 domain[0] = '\0';
2139 SetLastError(0xdeadbeef);
2140 return LookupAccountSidA(NULL, psid, account, &size, domain, &dom_size, &use);
2141 }
2142
2143 static void check_wellknown_name(const char* name, WELL_KNOWN_SID_TYPE result)
2144 {
2145 SID_IDENTIFIER_AUTHORITY ident = { SECURITY_NT_AUTHORITY };
2146 PSID domainsid = NULL;
2147 char wk_sid[SECURITY_MAX_SID_SIZE];
2148 DWORD cb;
2149
2150 DWORD sid_size, domain_size;
2151 SID_NAME_USE sid_use;
2152 LPSTR domain, account, sid_domain, wk_domain, wk_account;
2153 PSID psid;
2154 BOOL ret ,ret2;
2155
2156 sid_size = 0;
2157 domain_size = 0;
2158 ret = LookupAccountNameA(NULL, name, NULL, &sid_size, NULL, &domain_size, &sid_use);
2159 ok(!ret, " %s Should have failed to lookup account name\n", name);
2160 psid = HeapAlloc(GetProcessHeap(),0,sid_size);
2161 domain = HeapAlloc(GetProcessHeap(),0,domain_size);
2162 ret = LookupAccountNameA(NULL, name, psid, &sid_size, domain, &domain_size, &sid_use);
2163
2164 if (!result)
2165 {
2166 ok(!ret, " %s Should have failed to lookup account name\n",name);
2167 goto cleanup;
2168 }
2169
2170 AllocateAndInitializeSid(&ident, 6, SECURITY_NT_NON_UNIQUE, 12, 23, 34, 45, 56, 0, 0, &domainsid);
2171 cb = sizeof(wk_sid);
2172 if (!pCreateWellKnownSid(result, domainsid, wk_sid, &cb))
2173 {
2174 win_skip("SID %i is not available on the system\n",result);
2175 goto cleanup;
2176 }
2177
2178 ret2 = get_sid_info(wk_sid, &wk_account, &wk_domain);
2179 if (!ret2 && GetLastError() == ERROR_NONE_MAPPED)
2180 {
2181 win_skip("CreateWellKnownSid() succeeded but the account '%s' is not present (W2K)\n", name);
2182 goto cleanup;
2183 }
2184
2185 get_sid_info(psid, &account, &sid_domain);
2186
2187 ok(ret, "Failed to lookup account name %s\n",name);
2188 ok(sid_size != 0, "sid_size was zero\n");
2189
2190 ok(EqualSid(psid,wk_sid),"(%s) Sids fail to match well known sid!\n",name);
2191
2192 ok(!lstrcmpA(account, wk_account), "Expected %s , got %s\n", account, wk_account);
2193 ok(!lstrcmpA(domain, wk_domain), "Expected %s, got %s\n", wk_domain, domain);
2194 ok(sid_use == SidTypeWellKnownGroup , "Expected Use (5), got %d\n", sid_use);
2195
2196 cleanup:
2197 FreeSid(domainsid);
2198 HeapFree(GetProcessHeap(),0,psid);
2199 HeapFree(GetProcessHeap(),0,domain);
2200 }
2201
2202 static void test_LookupAccountName(void)
2203 {
2204 DWORD sid_size, domain_size, user_size;
2205 DWORD sid_save, domain_save;
2206 CHAR user_name[UNLEN + 1];
2207 CHAR computer_name[UNLEN + 1];
2208 SID_NAME_USE sid_use;
2209 LPSTR domain, account, sid_dom;
2210 PSID psid;
2211 BOOL ret;
2212
2213 /* native crashes if (assuming all other parameters correct):
2214 * - peUse is NULL
2215 * - Sid is NULL and cbSid is > 0
2216 * - cbSid or cchReferencedDomainName are NULL
2217 * - ReferencedDomainName is NULL and cchReferencedDomainName is the correct size
2218 */
2219
2220 user_size = UNLEN + 1;
2221 SetLastError(0xdeadbeef);
2222 ret = GetUserNameA(user_name, &user_size);
2223 ok(ret, "Failed to get user name : %d\n", GetLastError());
2224
2225 /* get sizes */
2226 sid_size = 0;
2227 domain_size = 0;
2228 sid_use = 0xcafebabe;
2229 SetLastError(0xdeadbeef);
2230 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, NULL, &domain_size, &sid_use);
2231 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2232 {
2233 win_skip("LookupAccountNameA is not implemented\n");
2234 return;
2235 }
2236 ok(!ret, "Expected 0, got %d\n", ret);
2237 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2238 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2239 ok(sid_size != 0, "Expected non-zero sid size\n");
2240 ok(domain_size != 0, "Expected non-zero domain size\n");
2241 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
2242
2243 sid_save = sid_size;
2244 domain_save = domain_size;
2245
2246 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
2247 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
2248
2249 /* try valid account name */
2250 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, domain, &domain_size, &sid_use);
2251 get_sid_info(psid, &account, &sid_dom);
2252 ok(ret, "Failed to lookup account name\n");
2253 ok(sid_size == GetLengthSid(psid), "Expected %d, got %d\n", GetLengthSid(psid), sid_size);
2254 ok(!lstrcmpA(account, user_name), "Expected %s, got %s\n", user_name, account);
2255 ok(!lstrcmpiA(domain, sid_dom), "Expected %s, got %s\n", sid_dom, domain);
2256 ok(domain_size == domain_save - 1, "Expected %d, got %d\n", domain_save - 1, domain_size);
2257 ok(strlen(domain) == domain_size, "Expected %d, got %d\n", lstrlenA(domain), domain_size);
2258 ok(sid_use == SidTypeUser, "Expected SidTypeUser (%d), got %d\n", SidTypeUser, sid_use);
2259 domain_size = domain_save;
2260 sid_size = sid_save;
2261
2262 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH)
2263 {
2264 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2265 }
2266 else
2267 {
2268 ret = LookupAccountNameA(NULL, "Everyone", psid, &sid_size, domain, &domain_size, &sid_use);
2269 get_sid_info(psid, &account, &sid_dom);
2270 ok(ret, "Failed to lookup account name\n");
2271 ok(sid_size != 0, "sid_size was zero\n");
2272 ok(!lstrcmpA(account, "Everyone"), "Expected Everyone, got %s\n", account);
2273 ok(!lstrcmpiA(domain, sid_dom), "Expected %s, got %s\n", sid_dom, domain);
2274 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
2275 ok(strlen(domain) == domain_size, "Expected %d, got %d\n", lstrlenA(domain), domain_size);
2276 ok(sid_use == SidTypeWellKnownGroup, "Expected SidTypeWellKnownGroup (%d), got %d\n", SidTypeWellKnownGroup, sid_use);
2277 domain_size = domain_save;
2278 }
2279
2280 /* NULL Sid with zero sid size */
2281 SetLastError(0xdeadbeef);
2282 sid_size = 0;
2283 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
2284 ok(!ret, "Expected 0, got %d\n", ret);
2285 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2286 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2287 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
2288 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
2289
2290 /* try cchReferencedDomainName - 1 */
2291 SetLastError(0xdeadbeef);
2292 domain_size--;
2293 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
2294 ok(!ret, "Expected 0, got %d\n", ret);
2295 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2296 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2297 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
2298 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
2299
2300 /* NULL ReferencedDomainName with zero domain name size */
2301 SetLastError(0xdeadbeef);
2302 domain_size = 0;
2303 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, NULL, &domain_size, &sid_use);
2304 ok(!ret, "Expected 0, got %d\n", ret);
2305 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2306 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2307 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
2308 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
2309
2310 HeapFree(GetProcessHeap(), 0, psid);
2311 HeapFree(GetProcessHeap(), 0, domain);
2312
2313 /* get sizes for NULL account name */
2314 sid_size = 0;
2315 domain_size = 0;
2316 sid_use = 0xcafebabe;
2317 SetLastError(0xdeadbeef);
2318 ret = LookupAccountNameA(NULL, NULL, NULL, &sid_size, NULL, &domain_size, &sid_use);
2319 if (!ret && GetLastError() == ERROR_NONE_MAPPED)
2320 win_skip("NULL account name doesn't work on NT4\n");
2321 else
2322 {
2323 ok(!ret, "Expected 0, got %d\n", ret);
2324 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2325 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2326 ok(sid_size != 0, "Expected non-zero sid size\n");
2327 ok(domain_size != 0, "Expected non-zero domain size\n");
2328 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
2329
2330 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
2331 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
2332
2333 /* try NULL account name */
2334 ret = LookupAccountNameA(NULL, NULL, psid, &sid_size, domain, &domain_size, &sid_use);
2335 get_sid_info(psid, &account, &sid_dom);
2336 ok(ret, "Failed to lookup account name\n");
2337 /* Using a fixed string will not work on different locales */
2338 ok(!lstrcmpiA(account, domain),
2339 "Got %s for account and %s for domain, these should be the same\n", account, domain);
2340 ok(sid_use == SidTypeDomain, "Expected SidTypeDomain (%d), got %d\n", SidTypeDomain, sid_use);
2341
2342 HeapFree(GetProcessHeap(), 0, psid);
2343 HeapFree(GetProcessHeap(), 0, domain);
2344 }
2345
2346 /* try an invalid account name */
2347 SetLastError(0xdeadbeef);
2348 sid_size = 0;
2349 domain_size = 0;
2350 ret = LookupAccountNameA(NULL, "oogabooga", NULL, &sid_size, NULL, &domain_size, &sid_use);
2351 ok(!ret, "Expected 0, got %d\n", ret);
2352 ok(GetLastError() == ERROR_NONE_MAPPED ||
2353 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE),
2354 "Expected ERROR_NONE_MAPPED, got %d\n", GetLastError());
2355 ok(sid_size == 0, "Expected 0, got %d\n", sid_size);
2356 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
2357
2358 /* try an invalid system name */
2359 SetLastError(0xdeadbeef);
2360 sid_size = 0;
2361 domain_size = 0;
2362 ret = LookupAccountNameA("deepthought", NULL, NULL, &sid_size, NULL, &domain_size, &sid_use);
2363 ok(!ret, "Expected 0, got %d\n", ret);
2364 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE || GetLastError() == RPC_S_INVALID_NET_ADDR /* Vista */,
2365 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
2366 ok(sid_size == 0, "Expected 0, got %d\n", sid_size);
2367 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
2368
2369 /* try with the computer name as the account name */
2370 domain_size = sizeof(computer_name);
2371 GetComputerNameA(computer_name, &domain_size);
2372 sid_size = 0;
2373 domain_size = 0;
2374 ret = LookupAccountNameA(NULL, computer_name, NULL, &sid_size, NULL, &domain_size, &sid_use);
2375 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER ||
2376 GetLastError() == ERROR_NONE_MAPPED /* in a domain */ ||
2377 broken(GetLastError() == ERROR_TRUSTED_DOMAIN_FAILURE) ||
2378 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE)),
2379 "LookupAccountNameA failed: %d\n", GetLastError());
2380 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
2381 {
2382 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
2383 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
2384 ret = LookupAccountNameA(NULL, computer_name, psid, &sid_size, domain, &domain_size, &sid_use);
2385 ok(ret, "LookupAccountNameA failed: %d\n", GetLastError());
2386 ok(sid_use == SidTypeDomain ||
2387 (sid_use == SidTypeUser && ! strcmp(computer_name, user_name)), "expected SidTypeDomain for %s, got %d\n", computer_name, sid_use);
2388 HeapFree(GetProcessHeap(), 0, domain);
2389 HeapFree(GetProcessHeap(), 0, psid);
2390 }
2391
2392 /* Well Known names */
2393 if (!pCreateWellKnownSid)
2394 {
2395 win_skip("CreateWellKnownSid not available\n");
2396 return;
2397 }
2398
2399 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH)
2400 {
2401 skip("Non-English locale (skipping well known name creation tests)\n");
2402 return;
2403 }
2404
2405 check_wellknown_name("LocalService", WinLocalServiceSid);
2406 check_wellknown_name("Local Service", WinLocalServiceSid);
2407 /* 2 spaces */
2408 check_wellknown_name("Local Service", 0);
2409 check_wellknown_name("NetworkService", WinNetworkServiceSid);
2410 check_wellknown_name("Network Service", WinNetworkServiceSid);
2411
2412 /* example of some names where the spaces are not optional */
2413 check_wellknown_name("Terminal Server User", WinTerminalServerSid);
2414 check_wellknown_name("TerminalServer User", 0);
2415 check_wellknown_name("TerminalServerUser", 0);
2416 check_wellknown_name("Terminal ServerUser", 0);
2417
2418 check_wellknown_name("enterprise domain controllers",WinEnterpriseControllersSid);
2419 check_wellknown_name("enterprisedomain controllers", 0);
2420 check_wellknown_name("enterprise domaincontrollers", 0);
2421 check_wellknown_name("enterprisedomaincontrollers", 0);
2422
2423 /* case insensitivity */
2424 check_wellknown_name("lOCAlServICE", WinLocalServiceSid);
2425
2426 /* fully qualified account names */
2427 check_wellknown_name("NT AUTHORITY\\LocalService", WinLocalServiceSid);
2428 check_wellknown_name("nt authority\\Network Service", WinNetworkServiceSid);
2429 check_wellknown_name("nt authority test\\Network Service", 0);
2430 check_wellknown_name("Dummy\\Network Service", 0);
2431 check_wellknown_name("ntauthority\\Network Service", 0);
2432 }
2433
2434 static void test_security_descriptor(void)
2435 {
2436 SECURITY_DESCRIPTOR sd;
2437 char buf[8192];
2438 DWORD size;
2439 BOOL isDefault, isPresent, ret;
2440 PACL pacl;
2441 PSID psid;
2442
2443 SetLastError(0xdeadbeef);
2444 ret = InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
2445 if (ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2446 {
2447 win_skip("InitializeSecurityDescriptor is not implemented\n");
2448 return;
2449 }
2450
2451 ok(GetSecurityDescriptorOwner(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
2452 expect_eq(psid, NULL, PSID, "%p");
2453 expect_eq(isDefault, FALSE, BOOL, "%d");
2454 sd.Control |= SE_DACL_PRESENT | SE_SACL_PRESENT;
2455
2456 SetLastError(0xdeadbeef);
2457 size = 5;
2458 expect_eq(MakeSelfRelativeSD(&sd, buf, &size), FALSE, BOOL, "%d");
2459 expect_eq(GetLastError(), ERROR_INSUFFICIENT_BUFFER, DWORD, "%u");
2460 ok(size > 5, "Size not increased\n");
2461 if (size <= 8192)
2462 {
2463 expect_eq(MakeSelfRelativeSD(&sd, buf, &size), TRUE, BOOL, "%d");
2464 ok(GetSecurityDescriptorOwner(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
2465 expect_eq(psid, NULL, PSID, "%p");
2466 expect_eq(isDefault, FALSE, BOOL, "%d");
2467 ok(GetSecurityDescriptorGroup(&sd, &psid, &isDefault), "GetSecurityDescriptorGroup failed\n");
2468 expect_eq(psid, NULL, PSID, "%p");
2469 expect_eq(isDefault, FALSE, BOOL, "%d");
2470 ok(GetSecurityDescriptorDacl(&sd, &isPresent, &pacl, &isDefault), "GetSecurityDescriptorDacl failed\n");
2471 expect_eq(isPresent, TRUE, BOOL, "%d");
2472 expect_eq(psid, NULL, PSID, "%p");
2473 expect_eq(isDefault, FALSE, BOOL, "%d");
2474 ok(GetSecurityDescriptorSacl(&sd, &isPresent, &pacl, &isDefault), "GetSecurityDescriptorSacl failed\n");
2475 expect_eq(isPresent, TRUE, BOOL, "%d");
2476 expect_eq(psid, NULL, PSID, "%p");
2477 expect_eq(isDefault, FALSE, BOOL, "%d");
2478 }
2479 }
2480
2481 #define TEST_GRANTED_ACCESS(a,b) test_granted_access(a,b,0,__LINE__)
2482 #define TEST_GRANTED_ACCESS2(a,b,c) test_granted_access(a,b,c,__LINE__)
2483 static void test_granted_access(HANDLE handle, ACCESS_MASK access,
2484 ACCESS_MASK alt, int line)
2485 {
2486 OBJECT_BASIC_INFORMATION obj_info;
2487 NTSTATUS status;
2488
2489 if (!pNtQueryObject)
2490 {
2491 skip_(__FILE__, line)("Not NT platform - skipping tests\n");
2492 return;
2493 }
2494
2495 status = pNtQueryObject( handle, ObjectBasicInformation, &obj_info,
2496 sizeof(obj_info), NULL );
2497 ok_(__FILE__, line)(!status, "NtQueryObject with err: %08x\n", status);
2498 if (alt)
2499 ok_(__FILE__, line)(obj_info.GrantedAccess == access ||
2500 obj_info.GrantedAccess == alt, "Granted access should be 0x%08x "
2501 "or 0x%08x, instead of 0x%08x\n", access, alt, obj_info.GrantedAccess);
2502 else
2503 ok_(__FILE__, line)(obj_info.GrantedAccess == access, "Granted access should "
2504 "be 0x%08x, instead of 0x%08x\n", access, obj_info.GrantedAccess);
2505 }
2506
2507 #define CHECK_SET_SECURITY(o,i,e) \
2508 do{ \
2509 BOOL res_; \
2510 DWORD err; \
2511 SetLastError( 0xdeadbeef ); \
2512 res_ = SetKernelObjectSecurity( o, i, SecurityDescriptor ); \
2513 err = GetLastError(); \
2514 if (e == ERROR_SUCCESS) \
2515 ok(res_, "SetKernelObjectSecurity failed with %d\n", err); \
2516 else \
2517 ok(!res_ && err == e, "SetKernelObjectSecurity should have failed " \
2518 "with %s, instead of %d\n", #e, err); \
2519 }while(0)
2520
2521 static void test_process_security(void)
2522 {
2523 BOOL res;
2524 PTOKEN_USER user;
2525 PTOKEN_OWNER owner;
2526 PTOKEN_PRIMARY_GROUP group;
2527 PSID AdminSid = NULL, UsersSid = NULL, UserSid = NULL;
2528 PACL Acl = NULL, ThreadAcl = NULL;
2529 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL, *ThreadSecurityDescriptor = NULL;
2530 char buffer[MAX_PATH], account[MAX_PATH], domain[MAX_PATH];
2531 PROCESS_INFORMATION info;
2532 STARTUPINFOA startup;
2533 SECURITY_ATTRIBUTES psa, tsa;
2534 HANDLE token, event;
2535 DWORD size, acc_size, dom_size, ret;
2536 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
2537 PSID EveryoneSid = NULL;
2538 SID_NAME_USE use;
2539
2540 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
2541 res = InitializeAcl(Acl, 256, ACL_REVISION);
2542 if (!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2543 {
2544 win_skip("ACLs not implemented - skipping tests\n");
2545 HeapFree(GetProcessHeap(), 0, Acl);
2546 return;
2547 }
2548 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
2549
2550 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
2551 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2552
2553 /* get owner from the token we might be running as a user not admin */
2554 res = OpenProcessToken( GetCurrentProcess(), MAXIMUM_ALLOWED, &token );
2555 ok(res, "OpenProcessToken failed with error %d\n", GetLastError());
2556 if (!res)
2557 {
2558 HeapFree(GetProcessHeap(), 0, Acl);
2559 return;
2560 }
2561
2562 res = GetTokenInformation( token, TokenOwner, NULL, 0, &size );
2563 ok(!res, "Expected failure, got %d\n", res);
2564 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2565 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2566
2567 owner = HeapAlloc(GetProcessHeap(), 0, size);
2568 res = GetTokenInformation( token, TokenOwner, owner, size, &size );
2569 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
2570 AdminSid = owner->Owner;
2571 test_sid_str(AdminSid);
2572
2573 res = GetTokenInformation( token, TokenPrimaryGroup, NULL, 0, &size );
2574 ok(!res, "Expected failure, got %d\n", res);
2575 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2576 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2577
2578 group = HeapAlloc(GetProcessHeap(), 0, size);
2579 res = GetTokenInformation( token, TokenPrimaryGroup, group, size, &size );
2580 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
2581 UsersSid = group->PrimaryGroup;
2582 test_sid_str(UsersSid);
2583
2584 acc_size = sizeof(account);
2585 dom_size = sizeof(domain);
2586 ret = LookupAccountSidA( NULL, UsersSid, account, &acc_size, domain, &dom_size, &use );
2587 ok(ret, "LookupAccountSid failed with %d\n", ret);
2588 ok(use == SidTypeGroup, "expect SidTypeGroup, got %d\n", use);
2589 ok(!strcmp(account, "None"), "expect None, got %s\n", account);
2590
2591 res = GetTokenInformation( token, TokenUser, NULL, 0, &size );
2592 ok(!res, "Expected failure, got %d\n", res);
2593 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2594 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2595
2596 user = HeapAlloc(GetProcessHeap(), 0, size);
2597 res = GetTokenInformation( token, TokenUser, user, size, &size );
2598 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
2599 UserSid = user->User.Sid;
2600 test_sid_str(UserSid);
2601 ok(EqualPrefixSid(UsersSid, UserSid), "TokenPrimaryGroup Sid and TokenUser Sid don't match.\n");
2602
2603 CloseHandle( token );
2604 if (!res)
2605 {
2606 HeapFree(GetProcessHeap(), 0, group);
2607 HeapFree(GetProcessHeap(), 0, owner);
2608 HeapFree(GetProcessHeap(), 0, user);
2609 HeapFree(GetProcessHeap(), 0, Acl);
2610 return;
2611 }
2612
2613 res = AddAccessDeniedAce(Acl, ACL_REVISION, PROCESS_VM_READ, AdminSid);
2614 ok(res, "AddAccessDeniedAce failed with error %d\n", GetLastError());