Add AppDefaults app selection to control panel
[wine/gsoc-2012-control.git] / dlls / advapi32 / tests / security.c
blob28e43ec73f98046d56fd1cc5854f9f22b23f5655
1 /*
2 * Unit tests for security functions
4 * Copyright (c) 2004 Mike McCormack
5 * Copyright (c) 2011 Dmitry Timoshkov
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.
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.
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
22 #include <stdarg.h>
23 #include <stdio.h>
25 #include "ntstatus.h"
26 #define WIN32_NO_STATUS
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30 #include "aclapi.h"
31 #include "winnt.h"
32 #include "sddl.h"
33 #include "ntsecapi.h"
34 #include "lmcons.h"
36 #include "wine/test.h"
38 /* PROCESS_ALL_ACCESS in Vista+ PSDKs is incompatible with older Windows versions */
39 #define PROCESS_ALL_ACCESS_NT4 (PROCESS_ALL_ACCESS & ~0xf000)
41 #ifndef EVENT_QUERY_STATE
42 #define EVENT_QUERY_STATE 0x0001
43 #endif
45 /* copied from Wine winternl.h - not included in the Windows SDK */
46 typedef enum _OBJECT_INFORMATION_CLASS {
47 ObjectBasicInformation,
48 ObjectNameInformation,
49 ObjectTypeInformation,
50 ObjectAllInformation,
51 ObjectDataInformation
52 } OBJECT_INFORMATION_CLASS, *POBJECT_INFORMATION_CLASS;
54 typedef struct _OBJECT_BASIC_INFORMATION {
55 ULONG Attributes;
56 ACCESS_MASK GrantedAccess;
57 ULONG HandleCount;
58 ULONG PointerCount;
59 ULONG PagedPoolUsage;
60 ULONG NonPagedPoolUsage;
61 ULONG Reserved[3];
62 ULONG NameInformationLength;
63 ULONG TypeInformationLength;
64 ULONG SecurityDescriptorLength;
65 LARGE_INTEGER CreateTime;
66 } OBJECT_BASIC_INFORMATION, *POBJECT_BASIC_INFORMATION;
68 #define expect_eq(expr, value, type, format) { type ret = expr; ok((value) == ret, #expr " expected " format " got " format "\n", (value), (ret)); }
70 static BOOL (WINAPI *pAddAccessAllowedAceEx)(PACL, DWORD, DWORD, DWORD, PSID);
71 static BOOL (WINAPI *pAddAccessDeniedAceEx)(PACL, DWORD, DWORD, DWORD, PSID);
72 static BOOL (WINAPI *pAddAuditAccessAceEx)(PACL, DWORD, DWORD, DWORD, PSID, BOOL, BOOL);
73 static VOID (WINAPI *pBuildTrusteeWithSidA)( PTRUSTEEA pTrustee, PSID pSid );
74 static VOID (WINAPI *pBuildTrusteeWithNameA)( PTRUSTEEA pTrustee, LPSTR pName );
75 static VOID (WINAPI *pBuildTrusteeWithObjectsAndNameA)( PTRUSTEEA pTrustee,
76 POBJECTS_AND_NAME_A pObjName,
77 SE_OBJECT_TYPE ObjectType,
78 LPSTR ObjectTypeName,
79 LPSTR InheritedObjectTypeName,
80 LPSTR Name );
81 static VOID (WINAPI *pBuildTrusteeWithObjectsAndSidA)( PTRUSTEEA pTrustee,
82 POBJECTS_AND_SID pObjSid,
83 GUID* pObjectGuid,
84 GUID* pInheritedObjectGuid,
85 PSID pSid );
86 static LPSTR (WINAPI *pGetTrusteeNameA)( PTRUSTEEA pTrustee );
87 static BOOL (WINAPI *pMakeSelfRelativeSD)( PSECURITY_DESCRIPTOR, PSECURITY_DESCRIPTOR, LPDWORD );
88 static BOOL (WINAPI *pConvertSidToStringSidA)( PSID pSid, LPSTR *str );
89 static BOOL (WINAPI *pConvertStringSidToSidA)( LPCSTR str, PSID pSid );
90 static BOOL (WINAPI *pCheckTokenMembership)(HANDLE, PSID, PBOOL);
91 static BOOL (WINAPI *pConvertStringSecurityDescriptorToSecurityDescriptorA)(LPCSTR, DWORD,
92 PSECURITY_DESCRIPTOR*, PULONG );
93 static BOOL (WINAPI *pConvertStringSecurityDescriptorToSecurityDescriptorW)(LPCWSTR, DWORD,
94 PSECURITY_DESCRIPTOR*, PULONG );
95 static BOOL (WINAPI *pConvertSecurityDescriptorToStringSecurityDescriptorA)(PSECURITY_DESCRIPTOR, DWORD,
96 SECURITY_INFORMATION, LPSTR *, PULONG );
97 static BOOL (WINAPI *pGetFileSecurityA)(LPCSTR, SECURITY_INFORMATION,
98 PSECURITY_DESCRIPTOR, DWORD, LPDWORD);
99 static BOOL (WINAPI *pSetFileSecurityA)(LPCSTR, SECURITY_INFORMATION,
100 PSECURITY_DESCRIPTOR);
101 static DWORD (WINAPI *pGetNamedSecurityInfoA)(LPSTR, SE_OBJECT_TYPE, SECURITY_INFORMATION,
102 PSID*, PSID*, PACL*, PACL*,
103 PSECURITY_DESCRIPTOR*);
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);
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 NTSTATUS (WINAPI *pNtAccessCheck)(PSECURITY_DESCRIPTOR, HANDLE, ACCESS_MASK, PGENERIC_MAPPING,
124 PPRIVILEGE_SET, PULONG, PULONG, NTSTATUS*);
125 static BOOL (WINAPI *pCreateRestrictedToken)(HANDLE, DWORD, DWORD, PSID_AND_ATTRIBUTES, DWORD,
126 PLUID_AND_ATTRIBUTES, DWORD, PSID_AND_ATTRIBUTES, PHANDLE);
128 static HMODULE hmod;
129 static int myARGC;
130 static char** myARGV;
132 struct sidRef
134 SID_IDENTIFIER_AUTHORITY auth;
135 const char *refStr;
138 static void init(void)
140 HMODULE hntdll;
142 hntdll = GetModuleHandleA("ntdll.dll");
143 pNtQueryObject = (void *)GetProcAddress( hntdll, "NtQueryObject" );
144 pNtAccessCheck = (void *)GetProcAddress( hntdll, "NtAccessCheck" );
146 hmod = GetModuleHandle("advapi32.dll");
147 pAddAccessAllowedAceEx = (void *)GetProcAddress(hmod, "AddAccessAllowedAceEx");
148 pAddAccessDeniedAceEx = (void *)GetProcAddress(hmod, "AddAccessDeniedAceEx");
149 pAddAuditAccessAceEx = (void *)GetProcAddress(hmod, "AddAuditAccessAceEx");
150 pCheckTokenMembership = (void *)GetProcAddress(hmod, "CheckTokenMembership");
151 pConvertStringSecurityDescriptorToSecurityDescriptorA =
152 (void *)GetProcAddress(hmod, "ConvertStringSecurityDescriptorToSecurityDescriptorA" );
153 pConvertStringSecurityDescriptorToSecurityDescriptorW =
154 (void *)GetProcAddress(hmod, "ConvertStringSecurityDescriptorToSecurityDescriptorW" );
155 pConvertSecurityDescriptorToStringSecurityDescriptorA =
156 (void *)GetProcAddress(hmod, "ConvertSecurityDescriptorToStringSecurityDescriptorA" );
157 pGetFileSecurityA = (void *)GetProcAddress(hmod, "GetFileSecurityA" );
158 pSetFileSecurityA = (void *)GetProcAddress(hmod, "SetFileSecurityA" );
159 pCreateWellKnownSid = (void *)GetProcAddress( hmod, "CreateWellKnownSid" );
160 pGetNamedSecurityInfoA = (void *)GetProcAddress(hmod, "GetNamedSecurityInfoA");
161 pGetSidSubAuthority = (void *)GetProcAddress(hmod, "GetSidSubAuthority");
162 pGetSidSubAuthorityCount = (void *)GetProcAddress(hmod, "GetSidSubAuthorityCount");
163 pIsValidSid = (void *)GetProcAddress(hmod, "IsValidSid");
164 pMakeSelfRelativeSD = (void *)GetProcAddress(hmod, "MakeSelfRelativeSD");
165 pSetEntriesInAclW = (void *)GetProcAddress(hmod, "SetEntriesInAclW");
166 pSetEntriesInAclA = (void *)GetProcAddress(hmod, "SetEntriesInAclA");
167 pSetSecurityDescriptorControl = (void *)GetProcAddress(hmod, "SetSecurityDescriptorControl");
168 pGetSecurityInfo = (void *)GetProcAddress(hmod, "GetSecurityInfo");
169 pCreateRestrictedToken = (void *)GetProcAddress(hmod, "CreateRestrictedToken");
171 myARGC = winetest_get_mainargs( &myARGV );
174 static void test_str_sid(const char *str_sid)
176 PSID psid;
177 char *temp;
179 if (pConvertStringSidToSidA(str_sid, &psid))
181 if (pConvertSidToStringSidA(psid, &temp))
183 trace(" %s: %s\n", str_sid, temp);
184 LocalFree(temp);
186 LocalFree(psid);
188 else
190 if (GetLastError() != ERROR_INVALID_SID)
191 trace(" %s: couldn't be converted, returned %d\n", str_sid, GetLastError());
192 else
193 trace(" %s: couldn't be converted\n", str_sid);
197 static void test_sid(void)
199 struct sidRef refs[] = {
200 { { {0x00,0x00,0x33,0x44,0x55,0x66} }, "S-1-860116326-1" },
201 { { {0x00,0x00,0x01,0x02,0x03,0x04} }, "S-1-16909060-1" },
202 { { {0x00,0x00,0x00,0x01,0x02,0x03} }, "S-1-66051-1" },
203 { { {0x00,0x00,0x00,0x00,0x01,0x02} }, "S-1-258-1" },
204 { { {0x00,0x00,0x00,0x00,0x00,0x02} }, "S-1-2-1" },
205 { { {0x00,0x00,0x00,0x00,0x00,0x0c} }, "S-1-12-1" },
207 const char noSubAuthStr[] = "S-1-5";
208 unsigned int i;
209 PSID psid = NULL;
210 SID *pisid;
211 BOOL r;
212 LPSTR str = NULL;
214 pConvertSidToStringSidA = (void *)GetProcAddress( hmod, "ConvertSidToStringSidA" );
215 if( !pConvertSidToStringSidA )
216 return;
217 pConvertStringSidToSidA = (void *)GetProcAddress( hmod, "ConvertStringSidToSidA" );
218 if( !pConvertStringSidToSidA )
219 return;
221 r = pConvertStringSidToSidA( NULL, NULL );
222 ok( !r, "expected failure with NULL parameters\n" );
223 if( GetLastError() == ERROR_CALL_NOT_IMPLEMENTED )
224 return;
225 ok( GetLastError() == ERROR_INVALID_PARAMETER,
226 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
227 GetLastError() );
229 r = pConvertStringSidToSidA( refs[0].refStr, NULL );
230 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
231 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
232 GetLastError() );
234 r = pConvertStringSidToSidA( NULL, &str );
235 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
236 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
237 GetLastError() );
239 r = pConvertStringSidToSidA( noSubAuthStr, &psid );
240 ok( !r,
241 "expected failure with no sub authorities\n" );
242 ok( GetLastError() == ERROR_INVALID_SID,
243 "expected GetLastError() is ERROR_INVALID_SID, got %d\n",
244 GetLastError() );
246 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid), "ConvertStringSidToSidA failed\n");
247 pisid = psid;
248 ok(pisid->SubAuthorityCount == 4, "Invalid sub authority count - expected 4, got %d\n", pisid->SubAuthorityCount);
249 ok(pisid->SubAuthority[0] == 21, "Invalid subauthority 0 - expected 21, got %d\n", pisid->SubAuthority[0]);
250 ok(pisid->SubAuthority[3] == 4576, "Invalid subauthority 0 - expected 4576, got %d\n", pisid->SubAuthority[3]);
251 LocalFree(str);
252 LocalFree(psid);
254 for( i = 0; i < sizeof(refs) / sizeof(refs[0]); i++ )
256 PISID pisid;
258 r = AllocateAndInitializeSid( &refs[i].auth, 1,1,0,0,0,0,0,0,0,
259 &psid );
260 ok( r, "failed to allocate sid\n" );
261 r = pConvertSidToStringSidA( psid, &str );
262 ok( r, "failed to convert sid\n" );
263 if (r)
265 ok( !strcmp( str, refs[i].refStr ),
266 "incorrect sid, expected %s, got %s\n", refs[i].refStr, str );
267 LocalFree( str );
269 if( psid )
270 FreeSid( psid );
272 r = pConvertStringSidToSidA( refs[i].refStr, &psid );
273 ok( r, "failed to parse sid string\n" );
274 pisid = psid;
275 ok( pisid &&
276 !memcmp( pisid->IdentifierAuthority.Value, refs[i].auth.Value,
277 sizeof(refs[i].auth) ),
278 "string sid %s didn't parse to expected value\n"
279 "(got 0x%04x%08x, expected 0x%04x%08x)\n",
280 refs[i].refStr,
281 MAKEWORD( pisid->IdentifierAuthority.Value[1],
282 pisid->IdentifierAuthority.Value[0] ),
283 MAKELONG( MAKEWORD( pisid->IdentifierAuthority.Value[5],
284 pisid->IdentifierAuthority.Value[4] ),
285 MAKEWORD( pisid->IdentifierAuthority.Value[3],
286 pisid->IdentifierAuthority.Value[2] ) ),
287 MAKEWORD( refs[i].auth.Value[1], refs[i].auth.Value[0] ),
288 MAKELONG( MAKEWORD( refs[i].auth.Value[5], refs[i].auth.Value[4] ),
289 MAKEWORD( refs[i].auth.Value[3], refs[i].auth.Value[2] ) ) );
290 if( psid )
291 LocalFree( psid );
294 trace("String SIDs:\n");
295 test_str_sid("AO");
296 test_str_sid("RU");
297 test_str_sid("AN");
298 test_str_sid("AU");
299 test_str_sid("BA");
300 test_str_sid("BG");
301 test_str_sid("BO");
302 test_str_sid("BU");
303 test_str_sid("CA");
304 test_str_sid("CG");
305 test_str_sid("CO");
306 test_str_sid("DA");
307 test_str_sid("DC");
308 test_str_sid("DD");
309 test_str_sid("DG");
310 test_str_sid("DU");
311 test_str_sid("EA");
312 test_str_sid("ED");
313 test_str_sid("WD");
314 test_str_sid("PA");
315 test_str_sid("IU");
316 test_str_sid("LA");
317 test_str_sid("LG");
318 test_str_sid("LS");
319 test_str_sid("SY");
320 test_str_sid("NU");
321 test_str_sid("NO");
322 test_str_sid("NS");
323 test_str_sid("PO");
324 test_str_sid("PS");
325 test_str_sid("PU");
326 test_str_sid("RS");
327 test_str_sid("RD");
328 test_str_sid("RE");
329 test_str_sid("RC");
330 test_str_sid("SA");
331 test_str_sid("SO");
332 test_str_sid("SU");
335 static void test_trustee(void)
337 GUID ObjectType = {0x12345678, 0x1234, 0x5678, {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}};
338 GUID InheritedObjectType = {0x23456789, 0x2345, 0x6786, {0x2, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}};
339 GUID ZeroGuid;
340 OBJECTS_AND_NAME_ oan;
341 OBJECTS_AND_SID oas;
342 TRUSTEE trustee;
343 PSID psid;
344 char szObjectTypeName[] = "ObjectTypeName";
345 char szInheritedObjectTypeName[] = "InheritedObjectTypeName";
346 char szTrusteeName[] = "szTrusteeName";
347 SID_IDENTIFIER_AUTHORITY auth = { {0x11,0x22,0,0,0, 0} };
349 memset( &ZeroGuid, 0x00, sizeof (ZeroGuid) );
351 pBuildTrusteeWithSidA = (void *)GetProcAddress( hmod, "BuildTrusteeWithSidA" );
352 pBuildTrusteeWithNameA = (void *)GetProcAddress( hmod, "BuildTrusteeWithNameA" );
353 pBuildTrusteeWithObjectsAndNameA = (void *)GetProcAddress (hmod, "BuildTrusteeWithObjectsAndNameA" );
354 pBuildTrusteeWithObjectsAndSidA = (void *)GetProcAddress (hmod, "BuildTrusteeWithObjectsAndSidA" );
355 pGetTrusteeNameA = (void *)GetProcAddress (hmod, "GetTrusteeNameA" );
356 if( !pBuildTrusteeWithSidA || !pBuildTrusteeWithNameA ||
357 !pBuildTrusteeWithObjectsAndNameA || !pBuildTrusteeWithObjectsAndSidA ||
358 !pGetTrusteeNameA )
359 return;
361 if ( ! AllocateAndInitializeSid( &auth, 1, 42, 0,0,0,0,0,0,0,&psid ) )
363 trace( "failed to init SID\n" );
364 return;
367 /* test BuildTrusteeWithSidA */
368 memset( &trustee, 0xff, sizeof trustee );
369 pBuildTrusteeWithSidA( &trustee, psid );
371 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
372 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
373 "MultipleTrusteeOperation wrong\n");
374 ok( trustee.TrusteeForm == TRUSTEE_IS_SID, "TrusteeForm wrong\n");
375 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
376 ok( trustee.ptstrName == psid, "ptstrName wrong\n" );
378 /* test BuildTrusteeWithObjectsAndSidA (test 1) */
379 memset( &trustee, 0xff, sizeof trustee );
380 memset( &oas, 0xff, sizeof(oas) );
381 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, &ObjectType,
382 &InheritedObjectType, psid);
384 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
385 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
386 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
387 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
388 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
390 ok(oas.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
391 ok(!memcmp(&oas.ObjectTypeGuid, &ObjectType, sizeof(GUID)), "ObjectTypeGuid wrong\n");
392 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
393 ok(oas.pSid == psid, "pSid wrong\n");
395 /* test GetTrusteeNameA */
396 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oas, "GetTrusteeName returned wrong value\n");
398 /* test BuildTrusteeWithObjectsAndSidA (test 2) */
399 memset( &trustee, 0xff, sizeof trustee );
400 memset( &oas, 0xff, sizeof(oas) );
401 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, NULL,
402 &InheritedObjectType, psid);
404 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
405 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
406 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
407 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
408 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
410 ok(oas.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
411 ok(!memcmp(&oas.ObjectTypeGuid, &ZeroGuid, sizeof(GUID)), "ObjectTypeGuid wrong\n");
412 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
413 ok(oas.pSid == psid, "pSid wrong\n");
415 FreeSid( psid );
417 /* test BuildTrusteeWithNameA */
418 memset( &trustee, 0xff, sizeof trustee );
419 pBuildTrusteeWithNameA( &trustee, szTrusteeName );
421 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
422 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
423 "MultipleTrusteeOperation wrong\n");
424 ok( trustee.TrusteeForm == TRUSTEE_IS_NAME, "TrusteeForm wrong\n");
425 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
426 ok( trustee.ptstrName == szTrusteeName, "ptstrName wrong\n" );
428 /* test BuildTrusteeWithObjectsAndNameA (test 1) */
429 memset( &trustee, 0xff, sizeof trustee );
430 memset( &oan, 0xff, sizeof(oan) );
431 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
432 szInheritedObjectTypeName, szTrusteeName);
434 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
435 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
436 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
437 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
438 ok(trustee.ptstrName == (LPTSTR)&oan, "ptstrName wrong\n");
440 ok(oan.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
441 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
442 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
443 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
445 /* test GetTrusteeNameA */
446 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oan, "GetTrusteeName returned wrong value\n");
448 /* test BuildTrusteeWithObjectsAndNameA (test 2) */
449 memset( &trustee, 0xff, sizeof trustee );
450 memset( &oan, 0xff, sizeof(oan) );
451 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, NULL,
452 szInheritedObjectTypeName, szTrusteeName);
454 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
455 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
456 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
457 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
458 ok(trustee.ptstrName == (LPSTR)&oan, "ptstrName wrong\n");
460 ok(oan.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
461 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
462 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
463 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
465 /* test BuildTrusteeWithObjectsAndNameA (test 3) */
466 memset( &trustee, 0xff, sizeof trustee );
467 memset( &oan, 0xff, sizeof(oan) );
468 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
469 NULL, szTrusteeName);
471 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
472 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
473 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
474 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
475 ok(trustee.ptstrName == (LPTSTR)&oan, "ptstrName wrong\n");
477 ok(oan.ObjectsPresent == ACE_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
478 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
479 ok(oan.InheritedObjectTypeName == NULL, "InheritedObjectTypeName wrong\n");
480 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
483 /* If the first isn't defined, assume none is */
484 #ifndef SE_MIN_WELL_KNOWN_PRIVILEGE
485 #define SE_MIN_WELL_KNOWN_PRIVILEGE 2L
486 #define SE_CREATE_TOKEN_PRIVILEGE 2L
487 #define SE_ASSIGNPRIMARYTOKEN_PRIVILEGE 3L
488 #define SE_LOCK_MEMORY_PRIVILEGE 4L
489 #define SE_INCREASE_QUOTA_PRIVILEGE 5L
490 #define SE_MACHINE_ACCOUNT_PRIVILEGE 6L
491 #define SE_TCB_PRIVILEGE 7L
492 #define SE_SECURITY_PRIVILEGE 8L
493 #define SE_TAKE_OWNERSHIP_PRIVILEGE 9L
494 #define SE_LOAD_DRIVER_PRIVILEGE 10L
495 #define SE_SYSTEM_PROFILE_PRIVILEGE 11L
496 #define SE_SYSTEMTIME_PRIVILEGE 12L
497 #define SE_PROF_SINGLE_PROCESS_PRIVILEGE 13L
498 #define SE_INC_BASE_PRIORITY_PRIVILEGE 14L
499 #define SE_CREATE_PAGEFILE_PRIVILEGE 15L
500 #define SE_CREATE_PERMANENT_PRIVILEGE 16L
501 #define SE_BACKUP_PRIVILEGE 17L
502 #define SE_RESTORE_PRIVILEGE 18L
503 #define SE_SHUTDOWN_PRIVILEGE 19L
504 #define SE_DEBUG_PRIVILEGE 20L
505 #define SE_AUDIT_PRIVILEGE 21L
506 #define SE_SYSTEM_ENVIRONMENT_PRIVILEGE 22L
507 #define SE_CHANGE_NOTIFY_PRIVILEGE 23L
508 #define SE_REMOTE_SHUTDOWN_PRIVILEGE 24L
509 #define SE_UNDOCK_PRIVILEGE 25L
510 #define SE_SYNC_AGENT_PRIVILEGE 26L
511 #define SE_ENABLE_DELEGATION_PRIVILEGE 27L
512 #define SE_MANAGE_VOLUME_PRIVILEGE 28L
513 #define SE_IMPERSONATE_PRIVILEGE 29L
514 #define SE_CREATE_GLOBAL_PRIVILEGE 30L
515 #define SE_MAX_WELL_KNOWN_PRIVILEGE SE_CREATE_GLOBAL_PRIVILEGE
516 #endif /* ndef SE_MIN_WELL_KNOWN_PRIVILEGE */
518 static void test_allocateLuid(void)
520 BOOL (WINAPI *pAllocateLocallyUniqueId)(PLUID);
521 LUID luid1, luid2;
522 BOOL ret;
524 pAllocateLocallyUniqueId = (void*)GetProcAddress(hmod, "AllocateLocallyUniqueId");
525 if (!pAllocateLocallyUniqueId) return;
527 ret = pAllocateLocallyUniqueId(&luid1);
528 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
529 return;
531 ok(ret,
532 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
533 ret = pAllocateLocallyUniqueId(&luid2);
534 ok( ret,
535 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
536 ok(luid1.LowPart > SE_MAX_WELL_KNOWN_PRIVILEGE || luid1.HighPart != 0,
537 "AllocateLocallyUniqueId returned a well-known LUID\n");
538 ok(luid1.LowPart != luid2.LowPart || luid1.HighPart != luid2.HighPart,
539 "AllocateLocallyUniqueId returned non-unique LUIDs\n");
540 ret = pAllocateLocallyUniqueId(NULL);
541 ok( !ret && GetLastError() == ERROR_NOACCESS,
542 "AllocateLocallyUniqueId(NULL) didn't return ERROR_NOACCESS: %d\n",
543 GetLastError());
546 static void test_lookupPrivilegeName(void)
548 BOOL (WINAPI *pLookupPrivilegeNameA)(LPCSTR, PLUID, LPSTR, LPDWORD);
549 char buf[MAX_PATH]; /* arbitrary, seems long enough */
550 DWORD cchName = sizeof(buf);
551 LUID luid = { 0, 0 };
552 LONG i;
553 BOOL ret;
555 /* check whether it's available first */
556 pLookupPrivilegeNameA = (void*)GetProcAddress(hmod, "LookupPrivilegeNameA");
557 if (!pLookupPrivilegeNameA) return;
558 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
559 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
560 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
561 return;
563 /* check with a short buffer */
564 cchName = 0;
565 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
566 ret = pLookupPrivilegeNameA(NULL, &luid, NULL, &cchName);
567 ok( !ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
568 "LookupPrivilegeNameA didn't fail with ERROR_INSUFFICIENT_BUFFER: %d\n",
569 GetLastError());
570 ok(cchName == strlen("SeCreateTokenPrivilege") + 1,
571 "LookupPrivilegeNameA returned an incorrect required length for\n"
572 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName,
573 lstrlenA("SeCreateTokenPrivilege") + 1);
574 /* check a known value and its returned length on success */
575 cchName = sizeof(buf);
576 ok(pLookupPrivilegeNameA(NULL, &luid, buf, &cchName) &&
577 cchName == strlen("SeCreateTokenPrivilege"),
578 "LookupPrivilegeNameA returned an incorrect output length for\n"
579 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName,
580 (int)strlen("SeCreateTokenPrivilege"));
581 /* check known values */
582 for (i = SE_MIN_WELL_KNOWN_PRIVILEGE; i <= SE_MAX_WELL_KNOWN_PRIVILEGE; i++)
584 luid.LowPart = i;
585 cchName = sizeof(buf);
586 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
587 ok( ret || GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
588 "LookupPrivilegeNameA(0.%d) failed: %d\n", i, GetLastError());
590 /* check a bogus LUID */
591 luid.LowPart = 0xdeadbeef;
592 cchName = sizeof(buf);
593 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
594 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
595 "LookupPrivilegeNameA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
596 GetLastError());
597 /* check on a bogus system */
598 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
599 cchName = sizeof(buf);
600 ret = pLookupPrivilegeNameA("b0gu5.Nam3", &luid, buf, &cchName);
601 ok( !ret && (GetLastError() == RPC_S_SERVER_UNAVAILABLE ||
602 GetLastError() == RPC_S_INVALID_NET_ADDR) /* w2k8 */,
603 "LookupPrivilegeNameA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
604 GetLastError());
607 struct NameToLUID
609 const char *name;
610 DWORD lowPart;
613 static void test_lookupPrivilegeValue(void)
615 static const struct NameToLUID privs[] = {
616 { "SeCreateTokenPrivilege", SE_CREATE_TOKEN_PRIVILEGE },
617 { "SeAssignPrimaryTokenPrivilege", SE_ASSIGNPRIMARYTOKEN_PRIVILEGE },
618 { "SeLockMemoryPrivilege", SE_LOCK_MEMORY_PRIVILEGE },
619 { "SeIncreaseQuotaPrivilege", SE_INCREASE_QUOTA_PRIVILEGE },
620 { "SeMachineAccountPrivilege", SE_MACHINE_ACCOUNT_PRIVILEGE },
621 { "SeTcbPrivilege", SE_TCB_PRIVILEGE },
622 { "SeSecurityPrivilege", SE_SECURITY_PRIVILEGE },
623 { "SeTakeOwnershipPrivilege", SE_TAKE_OWNERSHIP_PRIVILEGE },
624 { "SeLoadDriverPrivilege", SE_LOAD_DRIVER_PRIVILEGE },
625 { "SeSystemProfilePrivilege", SE_SYSTEM_PROFILE_PRIVILEGE },
626 { "SeSystemtimePrivilege", SE_SYSTEMTIME_PRIVILEGE },
627 { "SeProfileSingleProcessPrivilege", SE_PROF_SINGLE_PROCESS_PRIVILEGE },
628 { "SeIncreaseBasePriorityPrivilege", SE_INC_BASE_PRIORITY_PRIVILEGE },
629 { "SeCreatePagefilePrivilege", SE_CREATE_PAGEFILE_PRIVILEGE },
630 { "SeCreatePermanentPrivilege", SE_CREATE_PERMANENT_PRIVILEGE },
631 { "SeBackupPrivilege", SE_BACKUP_PRIVILEGE },
632 { "SeRestorePrivilege", SE_RESTORE_PRIVILEGE },
633 { "SeShutdownPrivilege", SE_SHUTDOWN_PRIVILEGE },
634 { "SeDebugPrivilege", SE_DEBUG_PRIVILEGE },
635 { "SeAuditPrivilege", SE_AUDIT_PRIVILEGE },
636 { "SeSystemEnvironmentPrivilege", SE_SYSTEM_ENVIRONMENT_PRIVILEGE },
637 { "SeChangeNotifyPrivilege", SE_CHANGE_NOTIFY_PRIVILEGE },
638 { "SeRemoteShutdownPrivilege", SE_REMOTE_SHUTDOWN_PRIVILEGE },
639 { "SeUndockPrivilege", SE_UNDOCK_PRIVILEGE },
640 { "SeSyncAgentPrivilege", SE_SYNC_AGENT_PRIVILEGE },
641 { "SeEnableDelegationPrivilege", SE_ENABLE_DELEGATION_PRIVILEGE },
642 { "SeManageVolumePrivilege", SE_MANAGE_VOLUME_PRIVILEGE },
643 { "SeImpersonatePrivilege", SE_IMPERSONATE_PRIVILEGE },
644 { "SeCreateGlobalPrivilege", SE_CREATE_GLOBAL_PRIVILEGE },
646 BOOL (WINAPI *pLookupPrivilegeValueA)(LPCSTR, LPCSTR, PLUID);
647 unsigned int i;
648 LUID luid;
649 BOOL ret;
651 /* check whether it's available first */
652 pLookupPrivilegeValueA = (void*)GetProcAddress(hmod, "LookupPrivilegeValueA");
653 if (!pLookupPrivilegeValueA) return;
654 ret = pLookupPrivilegeValueA(NULL, "SeCreateTokenPrivilege", &luid);
655 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
656 return;
658 /* check a bogus system name */
659 ret = pLookupPrivilegeValueA("b0gu5.Nam3", "SeCreateTokenPrivilege", &luid);
660 ok( !ret && (GetLastError() == RPC_S_SERVER_UNAVAILABLE ||
661 GetLastError() == RPC_S_INVALID_NET_ADDR) /* w2k8 */,
662 "LookupPrivilegeValueA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
663 GetLastError());
664 /* check a NULL string */
665 ret = pLookupPrivilegeValueA(NULL, 0, &luid);
666 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
667 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
668 GetLastError());
669 /* check a bogus privilege name */
670 ret = pLookupPrivilegeValueA(NULL, "SeBogusPrivilege", &luid);
671 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
672 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
673 GetLastError());
674 /* check case insensitive */
675 ret = pLookupPrivilegeValueA(NULL, "sEcREATEtOKENpRIVILEGE", &luid);
676 ok( ret,
677 "LookupPrivilegeValueA(NULL, sEcREATEtOKENpRIVILEGE, &luid) failed: %d\n",
678 GetLastError());
679 for (i = 0; i < sizeof(privs) / sizeof(privs[0]); i++)
681 /* Not all privileges are implemented on all Windows versions, so
682 * don't worry if the call fails
684 if (pLookupPrivilegeValueA(NULL, privs[i].name, &luid))
686 ok(luid.LowPart == privs[i].lowPart,
687 "LookupPrivilegeValueA returned an invalid LUID for %s\n",
688 privs[i].name);
693 static void test_luid(void)
695 test_allocateLuid();
696 test_lookupPrivilegeName();
697 test_lookupPrivilegeValue();
700 static void test_FileSecurity(void)
702 char wintmpdir [MAX_PATH];
703 char path [MAX_PATH];
704 char file [MAX_PATH];
705 HANDLE fh, token;
706 DWORD sdSize, retSize, rc, granted, priv_set_len;
707 PRIVILEGE_SET priv_set;
708 BOOL status;
709 BYTE *sd;
710 GENERIC_MAPPING mapping = { FILE_READ_DATA, FILE_WRITE_DATA, FILE_EXECUTE, FILE_ALL_ACCESS };
711 const SECURITY_INFORMATION request = OWNER_SECURITY_INFORMATION
712 | GROUP_SECURITY_INFORMATION
713 | DACL_SECURITY_INFORMATION;
715 if (!pGetFileSecurityA) {
716 win_skip ("GetFileSecurity is not available\n");
717 return;
720 if (!pSetFileSecurityA) {
721 win_skip ("SetFileSecurity is not available\n");
722 return;
725 if (!GetTempPathA (sizeof (wintmpdir), wintmpdir)) {
726 win_skip ("GetTempPathA failed\n");
727 return;
730 /* Create a temporary directory and in it a temporary file */
731 strcat (strcpy (path, wintmpdir), "rary");
732 SetLastError(0xdeadbeef);
733 rc = CreateDirectoryA (path, NULL);
734 ok (rc || GetLastError() == ERROR_ALREADY_EXISTS, "CreateDirectoryA "
735 "failed for '%s' with %d\n", path, GetLastError());
737 strcat (strcpy (file, path), "\\ess");
738 SetLastError(0xdeadbeef);
739 fh = CreateFileA (file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
740 ok (fh != INVALID_HANDLE_VALUE, "CreateFileA "
741 "failed for '%s' with %d\n", file, GetLastError());
742 CloseHandle (fh);
744 /* For the temporary file ... */
746 /* Get size needed */
747 retSize = 0;
748 SetLastError(0xdeadbeef);
749 rc = pGetFileSecurityA (file, request, NULL, 0, &retSize);
750 if (!rc && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)) {
751 win_skip("GetFileSecurityA is not implemented\n");
752 goto cleanup;
754 ok (!rc, "GetFileSecurityA "
755 "was expected to fail for '%s'\n", file);
756 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetFileSecurityA "
757 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
758 ok (retSize > sizeof (SECURITY_DESCRIPTOR), "GetFileSecurityA returned size %d\n", retSize);
760 sdSize = retSize;
761 sd = HeapAlloc (GetProcessHeap (), 0, sdSize);
763 /* Get security descriptor for real */
764 retSize = -1;
765 SetLastError(0xdeadbeef);
766 rc = pGetFileSecurityA (file, request, sd, sdSize, &retSize);
767 ok (rc, "GetFileSecurityA "
768 "was not expected to fail '%s': %d\n", file, GetLastError());
769 ok (retSize == sdSize ||
770 broken(retSize == 0), /* NT4 */
771 "GetFileSecurityA returned size %d; expected %d\n", retSize, sdSize);
773 /* Use it to set security descriptor */
774 SetLastError(0xdeadbeef);
775 rc = pSetFileSecurityA (file, request, sd);
776 ok (rc, "SetFileSecurityA "
777 "was not expected to fail '%s': %d\n", file, GetLastError());
779 HeapFree (GetProcessHeap (), 0, sd);
781 /* Repeat for the temporary directory ... */
783 /* Get size needed */
784 retSize = 0;
785 SetLastError(0xdeadbeef);
786 rc = pGetFileSecurityA (path, request, NULL, 0, &retSize);
787 ok (!rc, "GetFileSecurityA "
788 "was expected to fail for '%s'\n", path);
789 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetFileSecurityA "
790 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
791 ok (retSize > sizeof (SECURITY_DESCRIPTOR), "GetFileSecurityA returned size %d\n", retSize);
793 sdSize = retSize;
794 sd = HeapAlloc (GetProcessHeap (), 0, sdSize);
796 /* Get security descriptor for real */
797 retSize = -1;
798 SetLastError(0xdeadbeef);
799 rc = pGetFileSecurityA (path, request, sd, sdSize, &retSize);
800 ok (rc, "GetFileSecurityA "
801 "was not expected to fail '%s': %d\n", path, GetLastError());
802 ok (retSize == sdSize ||
803 broken(retSize == 0), /* NT4 */
804 "GetFileSecurityA returned size %d; expected %d\n", retSize, sdSize);
806 /* Use it to set security descriptor */
807 SetLastError(0xdeadbeef);
808 rc = pSetFileSecurityA (path, request, sd);
809 ok (rc, "SetFileSecurityA "
810 "was not expected to fail '%s': %d\n", path, GetLastError());
811 HeapFree (GetProcessHeap (), 0, sd);
813 /* Old test */
814 strcpy (wintmpdir, "\\Should not exist");
815 SetLastError(0xdeadbeef);
816 rc = pGetFileSecurityA (wintmpdir, OWNER_SECURITY_INFORMATION, NULL, 0, &sdSize);
817 ok (!rc, "GetFileSecurityA should fail for not existing directories/files\n");
818 ok (GetLastError() == ERROR_FILE_NOT_FOUND,
819 "last error ERROR_FILE_NOT_FOUND expected, got %d\n", GetLastError());
821 cleanup:
822 /* Remove temporary file and directory */
823 DeleteFileA(file);
824 RemoveDirectoryA(path);
826 /* Test file access permissions for a file with FILE_ATTRIBUTE_ARCHIVE */
827 SetLastError(0xdeadbeef);
828 rc = GetTempPath(sizeof(wintmpdir), wintmpdir);
829 ok(rc, "GetTempPath error %d\n", GetLastError());
831 SetLastError(0xdeadbeef);
832 rc = GetTempFileName(wintmpdir, "tmp", 0, file);
833 ok(rc, "GetTempFileName error %d\n", GetLastError());
835 rc = GetFileAttributes(file);
836 rc &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
837 ok(rc == FILE_ATTRIBUTE_ARCHIVE, "expected FILE_ATTRIBUTE_ARCHIVE got %#x\n", rc);
839 retSize = 0xdeadbeef;
840 rc = GetFileSecurity(file, OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION,
841 NULL, 0, &sdSize);
842 ok(!rc, "GetFileSecurity should fail\n");
843 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
844 "expected ERROR_INSUFFICIENT_BUFFER got %d\n", GetLastError());
845 ok(sdSize > sizeof(SECURITY_DESCRIPTOR), "got sd size %d\n", sdSize);
847 sd = HeapAlloc(GetProcessHeap (), 0, sdSize);
848 retSize = 0xdeadbeef;
849 SetLastError(0xdeadbeef);
850 rc = GetFileSecurity(file, OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION,
851 sd, sdSize, &retSize);
852 ok(rc, "GetFileSecurity error %d\n", GetLastError());
853 ok(retSize == sdSize || broken(retSize == 0) /* NT4 */, "expected %d, got %d\n", sdSize, retSize);
855 SetLastError(0xdeadbeef);
856 rc = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE, &token);
857 ok(!rc, "OpenThreadToken should fail\n");
858 ok(GetLastError() == ERROR_NO_TOKEN, "expected ERROR_NO_TOKEN, got %d\n", GetLastError());
860 SetLastError(0xdeadbeef);
861 rc = ImpersonateSelf(SecurityIdentification);
862 ok(rc, "ImpersonateSelf error %d\n", GetLastError());
864 SetLastError(0xdeadbeef);
865 rc = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE, &token);
866 ok(rc, "OpenThreadToken error %d\n", GetLastError());
868 SetLastError(0xdeadbeef);
869 rc = RevertToSelf();
870 ok(rc, "RevertToSelf error %d\n", GetLastError());
872 priv_set_len = sizeof(priv_set);
873 granted = 0xdeadbeef;
874 status = 0xdeadbeef;
875 SetLastError(0xdeadbeef);
876 rc = AccessCheck(sd, token, FILE_READ_DATA, &mapping, &priv_set, &priv_set_len, &granted, &status);
877 ok(rc, "AccessCheck error %d\n", GetLastError());
878 ok(status == 1, "expected 1, got %d\n", status);
879 ok(granted == FILE_READ_DATA, "expected FILE_READ_DATA, got %#x\n", granted);
881 granted = 0xdeadbeef;
882 status = 0xdeadbeef;
883 SetLastError(0xdeadbeef);
884 rc = AccessCheck(sd, token, FILE_WRITE_DATA, &mapping, &priv_set, &priv_set_len, &granted, &status);
885 ok(rc, "AccessCheck error %d\n", GetLastError());
886 ok(status == 1, "expected 1, got %d\n", status);
887 ok(granted == FILE_WRITE_DATA, "expected FILE_WRITE_DATA, got %#x\n", granted);
889 granted = 0xdeadbeef;
890 status = 0xdeadbeef;
891 SetLastError(0xdeadbeef);
892 rc = AccessCheck(sd, token, FILE_EXECUTE, &mapping, &priv_set, &priv_set_len, &granted, &status);
893 ok(rc, "AccessCheck error %d\n", GetLastError());
894 ok(status == 1, "expected 1, got %d\n", status);
895 ok(granted == FILE_EXECUTE, "expected FILE_EXECUTE, got %#x\n", granted);
897 granted = 0xdeadbeef;
898 status = 0xdeadbeef;
899 SetLastError(0xdeadbeef);
900 rc = AccessCheck(sd, token, DELETE, &mapping, &priv_set, &priv_set_len, &granted, &status);
901 ok(rc, "AccessCheck error %d\n", GetLastError());
902 ok(status == 1, "expected 1, got %d\n", status);
903 ok(granted == DELETE, "expected DELETE, got %#x\n", granted);
905 granted = 0xdeadbeef;
906 status = 0xdeadbeef;
907 SetLastError(0xdeadbeef);
908 rc = AccessCheck(sd, token, FILE_DELETE_CHILD, &mapping, &priv_set, &priv_set_len, &granted, &status);
909 ok(rc, "AccessCheck error %d\n", GetLastError());
910 ok(status == 1, "expected 1, got %d\n", status);
911 ok(granted == FILE_DELETE_CHILD, "expected FILE_DELETE_CHILD, got %#x\n", granted);
913 granted = 0xdeadbeef;
914 status = 0xdeadbeef;
915 SetLastError(0xdeadbeef);
916 rc = AccessCheck(sd, token, 0x1ff, &mapping, &priv_set, &priv_set_len, &granted, &status);
917 ok(rc, "AccessCheck error %d\n", GetLastError());
918 ok(status == 1, "expected 1, got %d\n", status);
919 ok(granted == 0x1ff, "expected 0x1ff, got %#x\n", granted);
921 granted = 0xdeadbeef;
922 status = 0xdeadbeef;
923 SetLastError(0xdeadbeef);
924 rc = AccessCheck(sd, token, FILE_ALL_ACCESS, &mapping, &priv_set, &priv_set_len, &granted, &status);
925 ok(rc, "AccessCheck error %d\n", GetLastError());
926 ok(status == 1, "expected 1, got %d\n", status);
927 ok(granted == FILE_ALL_ACCESS, "expected FILE_ALL_ACCESS, got %#x\n", granted);
929 SetLastError(0xdeadbeef);
930 rc = AccessCheck(sd, token, 0xffffffff, &mapping, &priv_set, &priv_set_len, &granted, &status);
931 ok(!rc, "AccessCheck should fail\n");
932 ok(GetLastError() == ERROR_GENERIC_NOT_MAPPED, "expected ERROR_GENERIC_NOT_MAPPED, got %d\n", GetLastError());
934 /* Test file access permissions for a file with FILE_ATTRIBUTE_READONLY */
935 SetLastError(0xdeadbeef);
936 fh = CreateFile(file, FILE_READ_DATA, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_READONLY, 0);
937 ok(fh != INVALID_HANDLE_VALUE, "CreateFile error %d\n", GetLastError());
938 retSize = 0xdeadbeef;
939 SetLastError(0xdeadbeef);
940 rc = WriteFile(fh, "1", 1, &retSize, NULL);
941 ok(!rc, "WriteFile should fail\n");
942 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
943 ok(retSize == 0, "expected 0, got %d\n", retSize);
944 CloseHandle(fh);
946 rc = GetFileAttributes(file);
947 rc &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
948 todo_wine
949 ok(rc == (FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY),
950 "expected FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY got %#x\n", rc);
952 SetLastError(0xdeadbeef);
953 rc = SetFileAttributes(file, FILE_ATTRIBUTE_ARCHIVE);
954 ok(rc, "SetFileAttributes error %d\n", GetLastError());
955 SetLastError(0xdeadbeef);
956 rc = DeleteFile(file);
957 ok(rc, "DeleteFile error %d\n", GetLastError());
959 SetLastError(0xdeadbeef);
960 fh = CreateFile(file, FILE_READ_DATA, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_READONLY, 0);
961 ok(fh != INVALID_HANDLE_VALUE, "CreateFile error %d\n", GetLastError());
962 retSize = 0xdeadbeef;
963 SetLastError(0xdeadbeef);
964 rc = WriteFile(fh, "1", 1, &retSize, NULL);
965 ok(!rc, "WriteFile should fail\n");
966 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
967 ok(retSize == 0, "expected 0, got %d\n", retSize);
968 CloseHandle(fh);
970 rc = GetFileAttributes(file);
971 rc &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
972 ok(rc == (FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY),
973 "expected FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY got %#x\n", rc);
975 retSize = 0xdeadbeef;
976 SetLastError(0xdeadbeef);
977 rc = GetFileSecurity(file, OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION,
978 sd, sdSize, &retSize);
979 ok(rc, "GetFileSecurity error %d\n", GetLastError());
980 ok(retSize == sdSize || broken(retSize == 0) /* NT4 */, "expected %d, got %d\n", sdSize, retSize);
982 priv_set_len = sizeof(priv_set);
983 granted = 0xdeadbeef;
984 status = 0xdeadbeef;
985 SetLastError(0xdeadbeef);
986 rc = AccessCheck(sd, token, FILE_READ_DATA, &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_READ_DATA, "expected FILE_READ_DATA, got %#x\n", granted);
991 granted = 0xdeadbeef;
992 status = 0xdeadbeef;
993 SetLastError(0xdeadbeef);
994 rc = AccessCheck(sd, token, FILE_WRITE_DATA, &mapping, &priv_set, &priv_set_len, &granted, &status);
995 ok(rc, "AccessCheck error %d\n", GetLastError());
996 todo_wine {
997 ok(status == 1, "expected 1, got %d\n", status);
998 ok(granted == FILE_WRITE_DATA, "expected FILE_WRITE_DATA, got %#x\n", granted);
1000 granted = 0xdeadbeef;
1001 status = 0xdeadbeef;
1002 SetLastError(0xdeadbeef);
1003 rc = AccessCheck(sd, token, FILE_EXECUTE, &mapping, &priv_set, &priv_set_len, &granted, &status);
1004 ok(rc, "AccessCheck error %d\n", GetLastError());
1005 ok(status == 1, "expected 1, got %d\n", status);
1006 ok(granted == FILE_EXECUTE, "expected FILE_EXECUTE, got %#x\n", granted);
1008 granted = 0xdeadbeef;
1009 status = 0xdeadbeef;
1010 SetLastError(0xdeadbeef);
1011 rc = AccessCheck(sd, token, DELETE, &mapping, &priv_set, &priv_set_len, &granted, &status);
1012 ok(rc, "AccessCheck error %d\n", GetLastError());
1013 todo_wine {
1014 ok(status == 1, "expected 1, got %d\n", status);
1015 ok(granted == DELETE, "expected DELETE, got %#x\n", granted);
1017 granted = 0xdeadbeef;
1018 status = 0xdeadbeef;
1019 SetLastError(0xdeadbeef);
1020 rc = AccessCheck(sd, token, FILE_DELETE_CHILD, &mapping, &priv_set, &priv_set_len, &granted, &status);
1021 ok(rc, "AccessCheck error %d\n", GetLastError());
1022 todo_wine {
1023 ok(status == 1, "expected 1, got %d\n", status);
1024 ok(granted == FILE_DELETE_CHILD, "expected FILE_DELETE_CHILD, got %#x\n", granted);
1026 granted = 0xdeadbeef;
1027 status = 0xdeadbeef;
1028 SetLastError(0xdeadbeef);
1029 rc = AccessCheck(sd, token, 0x1ff, &mapping, &priv_set, &priv_set_len, &granted, &status);
1030 ok(rc, "AccessCheck error %d\n", GetLastError());
1031 todo_wine {
1032 ok(status == 1, "expected 1, got %d\n", status);
1033 ok(granted == 0x1ff, "expected 0x1ff, got %#x\n", granted);
1035 granted = 0xdeadbeef;
1036 status = 0xdeadbeef;
1037 SetLastError(0xdeadbeef);
1038 rc = AccessCheck(sd, token, FILE_ALL_ACCESS, &mapping, &priv_set, &priv_set_len, &granted, &status);
1039 ok(rc, "AccessCheck error %d\n", GetLastError());
1040 todo_wine {
1041 ok(status == 1, "expected 1, got %d\n", status);
1042 ok(granted == FILE_ALL_ACCESS, "expected FILE_ALL_ACCESS, got %#x\n", granted);
1044 SetLastError(0xdeadbeef);
1045 rc = DeleteFile(file);
1046 ok(!rc, "DeleteFile should fail\n");
1047 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1048 SetLastError(0xdeadbeef);
1049 rc = SetFileAttributes(file, FILE_ATTRIBUTE_ARCHIVE);
1050 ok(rc, "SetFileAttributes error %d\n", GetLastError());
1051 SetLastError(0xdeadbeef);
1052 rc = DeleteFile(file);
1053 ok(rc, "DeleteFile error %d\n", GetLastError());
1055 CloseHandle(token);
1056 HeapFree(GetProcessHeap(), 0, sd);
1059 static void test_AccessCheck(void)
1061 PSID EveryoneSid = NULL, AdminSid = NULL, UsersSid = NULL;
1062 PACL Acl = NULL;
1063 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL;
1064 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
1065 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
1066 GENERIC_MAPPING Mapping = { KEY_READ, KEY_WRITE, KEY_EXECUTE, KEY_ALL_ACCESS };
1067 ACCESS_MASK Access;
1068 BOOL AccessStatus;
1069 HANDLE Token;
1070 HANDLE ProcessToken;
1071 BOOL ret;
1072 DWORD PrivSetLen;
1073 PRIVILEGE_SET *PrivSet;
1074 BOOL res;
1075 HMODULE NtDllModule;
1076 BOOLEAN Enabled;
1077 DWORD err;
1078 NTSTATUS ntret, ntAccessStatus;
1080 NtDllModule = GetModuleHandle("ntdll.dll");
1081 if (!NtDllModule)
1083 skip("not running on NT, skipping test\n");
1084 return;
1086 pRtlAdjustPrivilege = (void *)GetProcAddress(NtDllModule, "RtlAdjustPrivilege");
1087 if (!pRtlAdjustPrivilege)
1089 win_skip("missing RtlAdjustPrivilege, skipping test\n");
1090 return;
1093 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
1094 res = InitializeAcl(Acl, 256, ACL_REVISION);
1095 if(!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1097 skip("ACLs not implemented - skipping tests\n");
1098 HeapFree(GetProcessHeap(), 0, Acl);
1099 return;
1101 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
1103 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
1104 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1106 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
1107 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdminSid);
1108 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1110 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
1111 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &UsersSid);
1112 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1114 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
1116 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
1117 ok(res, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
1119 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
1120 ok(res, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1122 PrivSetLen = FIELD_OFFSET(PRIVILEGE_SET, Privilege[16]);
1123 PrivSet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, PrivSetLen);
1124 PrivSet->PrivilegeCount = 16;
1126 res = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &ProcessToken);
1127 ok(res, "OpenProcessToken failed with error %d\n", GetLastError());
1129 pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, FALSE, TRUE, &Enabled);
1131 res = DuplicateToken(ProcessToken, SecurityImpersonation, &Token);
1132 ok(res, "DuplicateToken failed with error %d\n", GetLastError());
1134 /* SD without owner/group */
1135 SetLastError(0xdeadbeef);
1136 Access = AccessStatus = 0x1abe11ed;
1137 ret = AccessCheck(SecurityDescriptor, Token, KEY_QUERY_VALUE, &Mapping,
1138 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1139 err = GetLastError();
1140 ok(!ret && err == ERROR_INVALID_SECURITY_DESCR, "AccessCheck should have "
1141 "failed with ERROR_INVALID_SECURITY_DESCR, instead of %d\n", err);
1142 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
1143 "Access and/or AccessStatus were changed!\n");
1145 /* Set owner and group */
1146 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
1147 ok(res, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
1148 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, TRUE);
1149 ok(res, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
1151 /* Generic access mask */
1152 SetLastError(0xdeadbeef);
1153 Access = AccessStatus = 0x1abe11ed;
1154 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1155 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1156 err = GetLastError();
1157 ok(!ret && err == ERROR_GENERIC_NOT_MAPPED, "AccessCheck should have failed "
1158 "with ERROR_GENERIC_NOT_MAPPED, instead of %d\n", err);
1159 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
1160 "Access and/or AccessStatus were changed!\n");
1162 /* Generic access mask - no privilegeset buffer */
1163 SetLastError(0xdeadbeef);
1164 Access = AccessStatus = 0x1abe11ed;
1165 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1166 NULL, &PrivSetLen, &Access, &AccessStatus);
1167 err = GetLastError();
1168 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
1169 "with ERROR_NOACCESS, instead of %d\n", err);
1170 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
1171 "Access and/or AccessStatus were changed!\n");
1173 /* Generic access mask - no returnlength */
1174 SetLastError(0xdeadbeef);
1175 Access = AccessStatus = 0x1abe11ed;
1176 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1177 PrivSet, NULL, &Access, &AccessStatus);
1178 err = GetLastError();
1179 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
1180 "with ERROR_NOACCESS, instead of %d\n", err);
1181 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
1182 "Access and/or AccessStatus were changed!\n");
1184 /* Generic access mask - no privilegeset buffer, no returnlength */
1185 SetLastError(0xdeadbeef);
1186 Access = AccessStatus = 0x1abe11ed;
1187 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1188 NULL, NULL, &Access, &AccessStatus);
1189 err = GetLastError();
1190 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
1191 "with ERROR_NOACCESS, instead of %d\n", err);
1192 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
1193 "Access and/or AccessStatus were changed!\n");
1195 /* sd with no dacl present */
1196 Access = AccessStatus = 0x1abe11ed;
1197 ret = SetSecurityDescriptorDacl(SecurityDescriptor, FALSE, NULL, FALSE);
1198 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1199 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1200 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1201 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1202 ok(AccessStatus && (Access == KEY_READ),
1203 "AccessCheck failed to grant access with error %d\n",
1204 GetLastError());
1206 /* sd with no dacl present - no privilegeset buffer */
1207 SetLastError(0xdeadbeef);
1208 Access = AccessStatus = 0x1abe11ed;
1209 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1210 NULL, &PrivSetLen, &Access, &AccessStatus);
1211 err = GetLastError();
1212 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
1213 "with ERROR_NOACCESS, instead of %d\n", err);
1214 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
1215 "Access and/or AccessStatus were changed!\n");
1217 if(pNtAccessCheck)
1219 /* Generic access mask - no privilegeset buffer */
1220 SetLastError(0xdeadbeef);
1221 Access = ntAccessStatus = 0x1abe11ed;
1222 ntret = pNtAccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1223 NULL, &PrivSetLen, &Access, &ntAccessStatus);
1224 err = GetLastError();
1225 ok(ntret == STATUS_ACCESS_VIOLATION,
1226 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret);
1227 ok(err == 0xdeadbeef,
1228 "NtAccessCheck shouldn't set last error, got %d\n", err);
1229 ok(Access == 0x1abe11ed && ntAccessStatus == 0x1abe11ed,
1230 "Access and/or AccessStatus were changed!\n");
1232 /* Generic access mask - no returnlength */
1233 SetLastError(0xdeadbeef);
1234 Access = ntAccessStatus = 0x1abe11ed;
1235 ntret = pNtAccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1236 PrivSet, NULL, &Access, &ntAccessStatus);
1237 err = GetLastError();
1238 ok(ntret == STATUS_ACCESS_VIOLATION,
1239 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret);
1240 ok(err == 0xdeadbeef,
1241 "NtAccessCheck shouldn't set last error, got %d\n", err);
1242 ok(Access == 0x1abe11ed && ntAccessStatus == 0x1abe11ed,
1243 "Access and/or AccessStatus were changed!\n");
1245 /* Generic access mask - no privilegeset buffer, no returnlength */
1246 SetLastError(0xdeadbeef);
1247 Access = ntAccessStatus = 0x1abe11ed;
1248 ntret = pNtAccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1249 NULL, NULL, &Access, &ntAccessStatus);
1250 err = GetLastError();
1251 ok(ntret == STATUS_ACCESS_VIOLATION,
1252 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret);
1253 ok(err == 0xdeadbeef,
1254 "NtAccessCheck shouldn't set last error, got %d\n", err);
1255 ok(Access == 0x1abe11ed && ntAccessStatus == 0x1abe11ed,
1256 "Access and/or AccessStatus were changed!\n");
1258 else
1259 win_skip("NtAccessCheck unavailable. Skipping.\n");
1261 /* sd with NULL dacl */
1262 Access = AccessStatus = 0x1abe11ed;
1263 ret = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, NULL, FALSE);
1264 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1265 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1266 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1267 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1268 ok(AccessStatus && (Access == KEY_READ),
1269 "AccessCheck failed to grant access with error %d\n",
1270 GetLastError());
1272 /* sd with blank dacl */
1273 ret = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
1274 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1275 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1276 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1277 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1278 err = GetLastError();
1279 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
1280 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
1281 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
1283 res = AddAccessAllowedAce(Acl, ACL_REVISION, KEY_READ, EveryoneSid);
1284 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
1286 res = AddAccessDeniedAce(Acl, ACL_REVISION, KEY_SET_VALUE, AdminSid);
1287 ok(res, "AddAccessDeniedAce failed with error %d\n", GetLastError());
1289 /* sd with dacl */
1290 Access = AccessStatus = 0x1abe11ed;
1291 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1292 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1293 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1294 ok(AccessStatus && (Access == KEY_READ),
1295 "AccessCheck failed to grant access with error %d\n",
1296 GetLastError());
1298 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
1299 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1300 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1301 ok(AccessStatus,
1302 "AccessCheck failed to grant any access with error %d\n",
1303 GetLastError());
1304 trace("AccessCheck with MAXIMUM_ALLOWED got Access 0x%08x\n", Access);
1306 /* Access denied by SD */
1307 SetLastError(0xdeadbeef);
1308 Access = AccessStatus = 0x1abe11ed;
1309 ret = AccessCheck(SecurityDescriptor, Token, KEY_WRITE, &Mapping,
1310 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1311 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1312 err = GetLastError();
1313 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
1314 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
1315 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
1317 SetLastError(0);
1318 PrivSet->PrivilegeCount = 16;
1319 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
1320 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1321 ok(ret && !AccessStatus && GetLastError() == ERROR_PRIVILEGE_NOT_HELD,
1322 "AccessCheck should have failed with ERROR_PRIVILEGE_NOT_HELD, instead of %d\n",
1323 GetLastError());
1325 ret = ImpersonateLoggedOnUser(Token);
1326 ok(ret, "ImpersonateLoggedOnUser failed with error %d\n", GetLastError());
1327 ret = pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, TRUE, TRUE, &Enabled);
1328 if (!ret)
1330 SetLastError(0);
1331 PrivSet->PrivilegeCount = 16;
1332 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
1333 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1334 ok(ret && AccessStatus && GetLastError() == 0,
1335 "AccessCheck should have succeeded, error %d\n",
1336 GetLastError());
1337 ok(Access == ACCESS_SYSTEM_SECURITY,
1338 "Access should be equal to ACCESS_SYSTEM_SECURITY instead of 0x%08x\n",
1339 Access);
1341 else
1342 trace("Couldn't get SE_SECURITY_PRIVILEGE (0x%08x), skipping ACCESS_SYSTEM_SECURITY test\n",
1343 ret);
1344 ret = RevertToSelf();
1345 ok(ret, "RevertToSelf failed with error %d\n", GetLastError());
1347 /* test INHERIT_ONLY_ACE */
1348 ret = InitializeAcl(Acl, 256, ACL_REVISION);
1349 ok(ret, "InitializeAcl failed with error %d\n", GetLastError());
1351 /* NT doesn't have AddAccessAllowedAceEx. Skipping this call/test doesn't influence
1352 * the next ones.
1354 if (pAddAccessAllowedAceEx)
1356 ret = pAddAccessAllowedAceEx(Acl, ACL_REVISION, INHERIT_ONLY_ACE, KEY_READ, EveryoneSid);
1357 ok(ret, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
1359 else
1360 win_skip("AddAccessAllowedAceEx is not available\n");
1362 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1363 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1364 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1365 err = GetLastError();
1366 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
1367 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
1368 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
1370 CloseHandle(Token);
1372 res = DuplicateToken(ProcessToken, SecurityAnonymous, &Token);
1373 ok(res, "DuplicateToken failed with error %d\n", GetLastError());
1375 SetLastError(0xdeadbeef);
1376 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
1377 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1378 err = GetLastError();
1379 ok(!ret && err == ERROR_BAD_IMPERSONATION_LEVEL, "AccessCheck should have failed "
1380 "with ERROR_BAD_IMPERSONATION_LEVEL, instead of %d\n", err);
1382 CloseHandle(Token);
1384 SetLastError(0xdeadbeef);
1385 ret = AccessCheck(SecurityDescriptor, ProcessToken, KEY_READ, &Mapping,
1386 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1387 err = GetLastError();
1388 ok(!ret && err == ERROR_NO_IMPERSONATION_TOKEN, "AccessCheck should have failed "
1389 "with ERROR_NO_IMPERSONATION_TOKEN, instead of %d\n", err);
1391 CloseHandle(ProcessToken);
1393 if (EveryoneSid)
1394 FreeSid(EveryoneSid);
1395 if (AdminSid)
1396 FreeSid(AdminSid);
1397 if (UsersSid)
1398 FreeSid(UsersSid);
1399 HeapFree(GetProcessHeap(), 0, Acl);
1400 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
1401 HeapFree(GetProcessHeap(), 0, PrivSet);
1404 /* test GetTokenInformation for the various attributes */
1405 static void test_token_attr(void)
1407 HANDLE Token, ImpersonationToken;
1408 DWORD Size, Size2;
1409 TOKEN_PRIVILEGES *Privileges;
1410 TOKEN_GROUPS *Groups;
1411 TOKEN_USER *User;
1412 TOKEN_DEFAULT_DACL *Dacl;
1413 BOOL ret;
1414 DWORD i, GLE;
1415 LPSTR SidString;
1416 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel;
1417 ACL *acl;
1419 /* cygwin-like use case */
1420 SetLastError(0xdeadbeef);
1421 ret = OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED, &Token);
1422 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1424 win_skip("OpenProcessToken is not implemented\n");
1425 return;
1427 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1428 if (ret)
1430 DWORD buf[256]; /* GetTokenInformation wants a dword-aligned buffer */
1431 Size = sizeof(buf);
1432 ret = GetTokenInformation(Token, TokenUser,(void*)buf, Size, &Size);
1433 ok(ret, "GetTokenInformation failed with error %d\n", GetLastError());
1434 Size = sizeof(ImpersonationLevel);
1435 ret = GetTokenInformation(Token, TokenImpersonationLevel, &ImpersonationLevel, Size, &Size);
1436 GLE = GetLastError();
1437 ok(!ret && (GLE == ERROR_INVALID_PARAMETER), "GetTokenInformation(TokenImpersonationLevel) on primary token should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GLE);
1438 CloseHandle(Token);
1441 if(!pConvertSidToStringSidA)
1443 win_skip("ConvertSidToStringSidA is not available\n");
1444 return;
1447 SetLastError(0xdeadbeef);
1448 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &Token);
1449 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1451 /* groups */
1452 /* insufficient buffer length */
1453 SetLastError(0xdeadbeef);
1454 Size2 = 0;
1455 ret = GetTokenInformation(Token, TokenGroups, NULL, 0, &Size2);
1456 ok(Size2 > 1, "got %d\n", Size2);
1457 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1458 "%d with error %d\n", ret, GetLastError());
1459 Size2 -= 1;
1460 Groups = HeapAlloc(GetProcessHeap(), 0, Size2);
1461 memset(Groups, 0xcc, Size2);
1462 Size = 0;
1463 ret = GetTokenInformation(Token, TokenGroups, Groups, Size2, &Size);
1464 ok(Size > 1, "got %d\n", Size);
1465 ok((!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER) || broken(ret) /* wow64 */,
1466 "%d with error %d\n", ret, GetLastError());
1467 if(!ret)
1468 ok(*((BYTE*)Groups) == 0xcc, "buffer altered\n");
1470 HeapFree(GetProcessHeap(), 0, Groups);
1472 SetLastError(0xdeadbeef);
1473 ret = GetTokenInformation(Token, TokenGroups, NULL, 0, &Size);
1474 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1475 "GetTokenInformation(TokenGroups) %s with error %d\n",
1476 ret ? "succeeded" : "failed", GetLastError());
1477 Groups = HeapAlloc(GetProcessHeap(), 0, Size);
1478 SetLastError(0xdeadbeef);
1479 ret = GetTokenInformation(Token, TokenGroups, Groups, Size, &Size);
1480 ok(ret, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
1481 ok(GetLastError() == 0xdeadbeef,
1482 "GetTokenInformation shouldn't have set last error to %d\n",
1483 GetLastError());
1484 trace("TokenGroups:\n");
1485 for (i = 0; i < Groups->GroupCount; i++)
1487 DWORD NameLength = 255;
1488 TCHAR Name[255];
1489 DWORD DomainLength = 255;
1490 TCHAR Domain[255];
1491 SID_NAME_USE SidNameUse;
1492 Name[0] = '\0';
1493 Domain[0] = '\0';
1494 ret = LookupAccountSid(NULL, Groups->Groups[i].Sid, Name, &NameLength, Domain, &DomainLength, &SidNameUse);
1495 if (ret)
1497 pConvertSidToStringSidA(Groups->Groups[i].Sid, &SidString);
1498 trace("%s, %s\\%s use: %d attr: 0x%08x\n", SidString, Domain, Name, SidNameUse, Groups->Groups[i].Attributes);
1499 LocalFree(SidString);
1501 else trace("attr: 0x%08x LookupAccountSid failed with error %d\n", Groups->Groups[i].Attributes, GetLastError());
1503 HeapFree(GetProcessHeap(), 0, Groups);
1505 /* user */
1506 ret = GetTokenInformation(Token, TokenUser, NULL, 0, &Size);
1507 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1508 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1509 User = HeapAlloc(GetProcessHeap(), 0, Size);
1510 ret = GetTokenInformation(Token, TokenUser, User, Size, &Size);
1511 ok(ret,
1512 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1514 pConvertSidToStringSidA(User->User.Sid, &SidString);
1515 trace("TokenUser: %s attr: 0x%08x\n", SidString, User->User.Attributes);
1516 LocalFree(SidString);
1517 HeapFree(GetProcessHeap(), 0, User);
1519 /* privileges */
1520 ret = GetTokenInformation(Token, TokenPrivileges, NULL, 0, &Size);
1521 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1522 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1523 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
1524 ret = GetTokenInformation(Token, TokenPrivileges, Privileges, Size, &Size);
1525 ok(ret,
1526 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1527 trace("TokenPrivileges:\n");
1528 for (i = 0; i < Privileges->PrivilegeCount; i++)
1530 TCHAR Name[256];
1531 DWORD NameLen = sizeof(Name)/sizeof(Name[0]);
1532 LookupPrivilegeName(NULL, &Privileges->Privileges[i].Luid, Name, &NameLen);
1533 trace("\t%s, 0x%x\n", Name, Privileges->Privileges[i].Attributes);
1535 HeapFree(GetProcessHeap(), 0, Privileges);
1537 ret = DuplicateToken(Token, SecurityAnonymous, &ImpersonationToken);
1538 ok(ret, "DuplicateToken failed with error %d\n", GetLastError());
1540 Size = sizeof(ImpersonationLevel);
1541 ret = GetTokenInformation(ImpersonationToken, TokenImpersonationLevel, &ImpersonationLevel, Size, &Size);
1542 ok(ret, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1543 ok(ImpersonationLevel == SecurityAnonymous, "ImpersonationLevel should have been SecurityAnonymous instead of %d\n", ImpersonationLevel);
1545 CloseHandle(ImpersonationToken);
1547 /* default dacl */
1548 ret = GetTokenInformation(Token, TokenDefaultDacl, NULL, 0, &Size);
1549 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1550 "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1552 Dacl = HeapAlloc(GetProcessHeap(), 0, Size);
1553 ret = GetTokenInformation(Token, TokenDefaultDacl, Dacl, Size, &Size);
1554 ok(ret, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1556 SetLastError(0xdeadbeef);
1557 ret = SetTokenInformation(Token, TokenDefaultDacl, NULL, 0);
1558 GLE = GetLastError();
1559 ok(!ret, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1560 ok(GLE == ERROR_BAD_LENGTH, "expected ERROR_BAD_LENGTH got %u\n", GLE);
1562 SetLastError(0xdeadbeef);
1563 ret = SetTokenInformation(Token, TokenDefaultDacl, NULL, Size);
1564 GLE = GetLastError();
1565 ok(!ret, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1566 ok(GLE == ERROR_NOACCESS, "expected ERROR_NOACCESS got %u\n", GLE);
1568 acl = Dacl->DefaultDacl;
1569 Dacl->DefaultDacl = NULL;
1571 ret = SetTokenInformation(Token, TokenDefaultDacl, Dacl, Size);
1572 ok(ret, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1574 Size2 = 0;
1575 Dacl->DefaultDacl = (ACL *)0xdeadbeef;
1576 ret = GetTokenInformation(Token, TokenDefaultDacl, Dacl, Size, &Size2);
1577 ok(ret, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1578 ok(Dacl->DefaultDacl == NULL, "expected NULL, got %p\n", Dacl->DefaultDacl);
1579 ok(Size2 == sizeof(TOKEN_DEFAULT_DACL) || broken(Size2 == 2*sizeof(TOKEN_DEFAULT_DACL)), /* WoW64 */
1580 "got %u expected sizeof(TOKEN_DEFAULT_DACL)\n", Size2);
1582 Dacl->DefaultDacl = acl;
1583 ret = SetTokenInformation(Token, TokenDefaultDacl, Dacl, Size);
1584 ok(ret, "SetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1586 if (Size2 == sizeof(TOKEN_DEFAULT_DACL)) {
1587 ret = GetTokenInformation(Token, TokenDefaultDacl, Dacl, Size, &Size2);
1588 ok(ret, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1589 } else
1590 win_skip("TOKEN_DEFAULT_DACL size too small on WoW64\n");
1592 HeapFree(GetProcessHeap(), 0, Dacl);
1593 CloseHandle(Token);
1596 typedef union _MAX_SID
1598 SID sid;
1599 char max[SECURITY_MAX_SID_SIZE];
1600 } MAX_SID;
1602 static void test_sid_str(PSID * sid)
1604 char *str_sid;
1605 BOOL ret = pConvertSidToStringSidA(sid, &str_sid);
1606 ok(ret, "ConvertSidToStringSidA() failed: %d\n", GetLastError());
1607 if (ret)
1609 char account[MAX_PATH], domain[MAX_PATH];
1610 SID_NAME_USE use;
1611 DWORD acc_size = MAX_PATH;
1612 DWORD dom_size = MAX_PATH;
1613 ret = LookupAccountSid(NULL, sid, account, &acc_size, domain, &dom_size, &use);
1614 ok(ret || (!ret && (GetLastError() == ERROR_NONE_MAPPED)),
1615 "LookupAccountSid(%s) failed: %d\n", str_sid, GetLastError());
1616 if (ret)
1617 trace(" %s %s\\%s %d\n", str_sid, domain, account, use);
1618 else if (GetLastError() == ERROR_NONE_MAPPED)
1619 trace(" %s couldn't be mapped\n", str_sid);
1620 LocalFree(str_sid);
1624 static const struct well_known_sid_value
1626 BOOL without_domain;
1627 const char *sid_string;
1628 } well_known_sid_values[] = {
1629 /* 0 */ {TRUE, "S-1-0-0"}, {TRUE, "S-1-1-0"}, {TRUE, "S-1-2-0"}, {TRUE, "S-1-3-0"},
1630 /* 4 */ {TRUE, "S-1-3-1"}, {TRUE, "S-1-3-2"}, {TRUE, "S-1-3-3"}, {TRUE, "S-1-5"},
1631 /* 8 */ {FALSE, "S-1-5-1"}, {TRUE, "S-1-5-2"}, {TRUE, "S-1-5-3"}, {TRUE, "S-1-5-4"},
1632 /* 12 */ {TRUE, "S-1-5-6"}, {TRUE, "S-1-5-7"}, {TRUE, "S-1-5-8"}, {TRUE, "S-1-5-9"},
1633 /* 16 */ {TRUE, "S-1-5-10"}, {TRUE, "S-1-5-11"}, {TRUE, "S-1-5-12"}, {TRUE, "S-1-5-13"},
1634 /* 20 */ {TRUE, "S-1-5-14"}, {FALSE, NULL}, {TRUE, "S-1-5-18"}, {TRUE, "S-1-5-19"},
1635 /* 24 */ {TRUE, "S-1-5-20"}, {TRUE, "S-1-5-32"},
1636 /* 26 */ {FALSE, "S-1-5-32-544"}, {TRUE, "S-1-5-32-545"}, {TRUE, "S-1-5-32-546"},
1637 /* 29 */ {TRUE, "S-1-5-32-547"}, {TRUE, "S-1-5-32-548"}, {TRUE, "S-1-5-32-549"},
1638 /* 32 */ {TRUE, "S-1-5-32-550"}, {TRUE, "S-1-5-32-551"}, {TRUE, "S-1-5-32-552"},
1639 /* 35 */ {TRUE, "S-1-5-32-554"}, {TRUE, "S-1-5-32-555"}, {TRUE, "S-1-5-32-556"},
1640 /* 38 */ {FALSE, "S-1-5-21-12-23-34-45-56-500"}, {FALSE, "S-1-5-21-12-23-34-45-56-501"},
1641 /* 40 */ {FALSE, "S-1-5-21-12-23-34-45-56-502"}, {FALSE, "S-1-5-21-12-23-34-45-56-512"},
1642 /* 42 */ {FALSE, "S-1-5-21-12-23-34-45-56-513"}, {FALSE, "S-1-5-21-12-23-34-45-56-514"},
1643 /* 44 */ {FALSE, "S-1-5-21-12-23-34-45-56-515"}, {FALSE, "S-1-5-21-12-23-34-45-56-516"},
1644 /* 46 */ {FALSE, "S-1-5-21-12-23-34-45-56-517"}, {FALSE, "S-1-5-21-12-23-34-45-56-518"},
1645 /* 48 */ {FALSE, "S-1-5-21-12-23-34-45-56-519"}, {FALSE, "S-1-5-21-12-23-34-45-56-520"},
1646 /* 50 */ {FALSE, "S-1-5-21-12-23-34-45-56-553"},
1647 /* Added in Windows Server 2003 */
1648 /* 51 */ {TRUE, "S-1-5-64-10"}, {TRUE, "S-1-5-64-21"}, {TRUE, "S-1-5-64-14"},
1649 /* 54 */ {TRUE, "S-1-5-15"}, {TRUE, "S-1-5-1000"}, {FALSE, "S-1-5-32-557"},
1650 /* 57 */ {TRUE, "S-1-5-32-558"}, {TRUE, "S-1-5-32-559"}, {TRUE, "S-1-5-32-560"},
1651 /* 60 */ {TRUE, "S-1-5-32-561"}, {TRUE, "S-1-5-32-562"},
1652 /* Added in Windows Vista: */
1653 /* 62 */ {TRUE, "S-1-5-32-568"},
1654 /* 63 */ {TRUE, "S-1-5-17"}, {FALSE, "S-1-5-32-569"}, {TRUE, "S-1-16-0"},
1655 /* 66 */ {TRUE, "S-1-16-4096"}, {TRUE, "S-1-16-8192"}, {TRUE, "S-1-16-12288"},
1656 /* 69 */ {TRUE, "S-1-16-16384"}, {TRUE, "S-1-5-33"}, {TRUE, "S-1-3-4"},
1657 /* 72 */ {FALSE, "S-1-5-21-12-23-34-45-56-571"}, {FALSE, "S-1-5-21-12-23-34-45-56-572"},
1658 /* 74 */ {TRUE, "S-1-5-22"}, {FALSE, "S-1-5-21-12-23-34-45-56-521"}, {TRUE, "S-1-5-32-573"},
1659 /* 77 */ {FALSE, "S-1-5-21-12-23-34-45-56-498"}, {TRUE, "S-1-5-32-574"}, {TRUE, "S-1-16-8448"}
1662 static void test_CreateWellKnownSid(void)
1664 SID_IDENTIFIER_AUTHORITY ident = { SECURITY_NT_AUTHORITY };
1665 PSID domainsid, sid;
1666 DWORD size, error;
1667 BOOL ret;
1668 unsigned int i;
1670 if (!pCreateWellKnownSid)
1672 win_skip("CreateWellKnownSid not available\n");
1673 return;
1676 size = 0;
1677 SetLastError(0xdeadbeef);
1678 ret = pCreateWellKnownSid(WinInteractiveSid, NULL, NULL, &size);
1679 error = GetLastError();
1680 ok(!ret, "CreateWellKnownSid succeeded\n");
1681 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
1682 ok(size, "expected size > 0\n");
1684 SetLastError(0xdeadbeef);
1685 ret = pCreateWellKnownSid(WinInteractiveSid, NULL, NULL, &size);
1686 error = GetLastError();
1687 ok(!ret, "CreateWellKnownSid succeeded\n");
1688 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
1690 sid = HeapAlloc(GetProcessHeap(), 0, size);
1691 ret = pCreateWellKnownSid(WinInteractiveSid, NULL, sid, &size);
1692 ok(ret, "CreateWellKnownSid failed %u\n", GetLastError());
1693 HeapFree(GetProcessHeap(), 0, sid);
1695 /* a domain sid usually have three subauthorities but we test that CreateWellKnownSid doesn't check it */
1696 AllocateAndInitializeSid(&ident, 6, SECURITY_NT_NON_UNIQUE, 12, 23, 34, 45, 56, 0, 0, &domainsid);
1698 for (i = 0; i < sizeof(well_known_sid_values)/sizeof(well_known_sid_values[0]); i++)
1700 const struct well_known_sid_value *value = &well_known_sid_values[i];
1701 char sid_buffer[SECURITY_MAX_SID_SIZE];
1702 LPSTR str;
1703 DWORD cb;
1705 if (value->sid_string == NULL)
1706 continue;
1708 if (i > WinAccountRasAndIasServersSid)
1710 /* These SIDs aren't implemented by all Windows versions - detect it and break the loop */
1711 cb = sizeof(sid_buffer);
1712 if (!pCreateWellKnownSid(i, domainsid, sid_buffer, &cb))
1714 skip("Well known SIDs starting from %u are not implemented\n", i);
1715 break;
1719 cb = sizeof(sid_buffer);
1720 ok(pCreateWellKnownSid(i, value->without_domain ? NULL : domainsid, sid_buffer, &cb), "Couldn't create well known sid %u\n", i);
1721 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer)), cb, DWORD, "%d");
1722 ok(IsValidSid(sid_buffer), "The sid is not valid\n");
1723 ok(pConvertSidToStringSidA(sid_buffer, &str), "Couldn't convert SID to string\n");
1724 ok(strcmp(str, value->sid_string) == 0, "%d: SID mismatch - expected %s, got %s\n", i,
1725 value->sid_string, str);
1726 LocalFree(str);
1728 if (value->without_domain)
1730 char buf2[SECURITY_MAX_SID_SIZE];
1731 cb = sizeof(buf2);
1732 ok(pCreateWellKnownSid(i, domainsid, buf2, &cb), "Couldn't create well known sid %u with optional domain\n", i);
1733 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer)), cb, DWORD, "%d");
1734 ok(memcmp(buf2, sid_buffer, cb) == 0, "SID create with domain is different than without (%u)\n", i);
1738 FreeSid(domainsid);
1741 static void test_LookupAccountSid(void)
1743 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
1744 CHAR accountA[MAX_PATH], domainA[MAX_PATH], usernameA[MAX_PATH];
1745 DWORD acc_sizeA, dom_sizeA, user_sizeA;
1746 DWORD real_acc_sizeA, real_dom_sizeA;
1747 WCHAR accountW[MAX_PATH], domainW[MAX_PATH];
1748 DWORD acc_sizeW, dom_sizeW;
1749 DWORD real_acc_sizeW, real_dom_sizeW;
1750 PSID pUsersSid = NULL;
1751 SID_NAME_USE use;
1752 BOOL ret;
1753 DWORD error, size, cbti = 0;
1754 MAX_SID max_sid;
1755 CHAR *str_sidA;
1756 int i;
1757 HANDLE hToken;
1758 PTOKEN_USER ptiUser = NULL;
1760 /* native windows crashes if account size, domain size, or name use is NULL */
1762 ret = AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
1763 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &pUsersSid);
1764 ok(ret || (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED),
1765 "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1767 /* not running on NT so give up */
1768 if (!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1769 return;
1771 real_acc_sizeA = MAX_PATH;
1772 real_dom_sizeA = MAX_PATH;
1773 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &real_acc_sizeA, domainA, &real_dom_sizeA, &use);
1774 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1776 /* try NULL account */
1777 acc_sizeA = MAX_PATH;
1778 dom_sizeA = MAX_PATH;
1779 ret = LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
1780 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1782 /* try NULL domain */
1783 acc_sizeA = MAX_PATH;
1784 dom_sizeA = MAX_PATH;
1785 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
1786 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1788 /* try a small account buffer */
1789 acc_sizeA = 1;
1790 dom_sizeA = MAX_PATH;
1791 accountA[0] = 0;
1792 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1793 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1794 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1795 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1797 /* try a 0 sized account buffer */
1798 acc_sizeA = 0;
1799 dom_sizeA = MAX_PATH;
1800 accountA[0] = 0;
1801 LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1802 /* this can fail or succeed depending on OS version but the size will always be returned */
1803 ok(acc_sizeA == real_acc_sizeA + 1,
1804 "LookupAccountSidA() Expected acc_size = %u, got %u\n",
1805 real_acc_sizeA + 1, acc_sizeA);
1807 /* try a 0 sized account buffer */
1808 acc_sizeA = 0;
1809 dom_sizeA = MAX_PATH;
1810 LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
1811 /* this can fail or succeed depending on OS version but the size will always be returned */
1812 ok(acc_sizeA == real_acc_sizeA + 1,
1813 "LookupAccountSid() Expected acc_size = %u, got %u\n",
1814 real_acc_sizeA + 1, acc_sizeA);
1816 /* try a small domain buffer */
1817 dom_sizeA = 1;
1818 acc_sizeA = MAX_PATH;
1819 accountA[0] = 0;
1820 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1821 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1822 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1823 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1825 /* try a 0 sized domain buffer */
1826 dom_sizeA = 0;
1827 acc_sizeA = MAX_PATH;
1828 accountA[0] = 0;
1829 LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1830 /* this can fail or succeed depending on OS version but the size will always be returned */
1831 ok(dom_sizeA == real_dom_sizeA + 1,
1832 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1833 real_dom_sizeA + 1, dom_sizeA);
1835 /* try a 0 sized domain buffer */
1836 dom_sizeA = 0;
1837 acc_sizeA = MAX_PATH;
1838 LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
1839 /* this can fail or succeed depending on OS version but the size will always be returned */
1840 ok(dom_sizeA == real_dom_sizeA + 1,
1841 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1842 real_dom_sizeA + 1, dom_sizeA);
1844 real_acc_sizeW = MAX_PATH;
1845 real_dom_sizeW = MAX_PATH;
1846 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &real_acc_sizeW, domainW, &real_dom_sizeW, &use);
1847 ok(ret, "LookupAccountSidW() Expected TRUE, got FALSE\n");
1849 /* try an invalid system name */
1850 real_acc_sizeA = MAX_PATH;
1851 real_dom_sizeA = MAX_PATH;
1852 ret = LookupAccountSidA("deepthought", pUsersSid, accountA, &real_acc_sizeA, domainA, &real_dom_sizeA, &use);
1853 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1854 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE || GetLastError() == RPC_S_INVALID_NET_ADDR /* Vista */,
1855 "LookupAccountSidA() Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %u\n", GetLastError());
1857 /* native windows crashes if domainW or accountW is NULL */
1859 /* try a small account buffer */
1860 acc_sizeW = 1;
1861 dom_sizeW = MAX_PATH;
1862 accountW[0] = 0;
1863 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1864 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1865 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1866 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1868 /* try a 0 sized account buffer */
1869 acc_sizeW = 0;
1870 dom_sizeW = MAX_PATH;
1871 accountW[0] = 0;
1872 LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1873 /* this can fail or succeed depending on OS version but the size will always be returned */
1874 ok(acc_sizeW == real_acc_sizeW + 1,
1875 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1876 real_acc_sizeW + 1, acc_sizeW);
1878 /* try a 0 sized account buffer */
1879 acc_sizeW = 0;
1880 dom_sizeW = MAX_PATH;
1881 LookupAccountSidW(NULL, pUsersSid, NULL, &acc_sizeW, domainW, &dom_sizeW, &use);
1882 /* this can fail or succeed depending on OS version but the size will always be returned */
1883 ok(acc_sizeW == real_acc_sizeW + 1,
1884 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1885 real_acc_sizeW + 1, acc_sizeW);
1887 /* try a small domain buffer */
1888 dom_sizeW = 1;
1889 acc_sizeW = MAX_PATH;
1890 accountW[0] = 0;
1891 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1892 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1893 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1894 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1896 /* try a 0 sized domain buffer */
1897 dom_sizeW = 0;
1898 acc_sizeW = MAX_PATH;
1899 accountW[0] = 0;
1900 LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1901 /* this can fail or succeed depending on OS version but the size will always be returned */
1902 ok(dom_sizeW == real_dom_sizeW + 1,
1903 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1904 real_dom_sizeW + 1, dom_sizeW);
1906 /* try a 0 sized domain buffer */
1907 dom_sizeW = 0;
1908 acc_sizeW = MAX_PATH;
1909 LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, NULL, &dom_sizeW, &use);
1910 /* this can fail or succeed depending on OS version but the size will always be returned */
1911 ok(dom_sizeW == real_dom_sizeW + 1,
1912 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1913 real_dom_sizeW + 1, dom_sizeW);
1915 acc_sizeW = dom_sizeW = use = 0;
1916 SetLastError(0xdeadbeef);
1917 ret = LookupAccountSidW(NULL, pUsersSid, NULL, &acc_sizeW, NULL, &dom_sizeW, &use);
1918 error = GetLastError();
1919 ok(!ret, "LookupAccountSidW failed %u\n", GetLastError());
1920 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
1921 ok(acc_sizeW, "expected non-zero account size\n");
1922 ok(dom_sizeW, "expected non-zero domain size\n");
1923 ok(!use, "expected zero use %u\n", use);
1925 FreeSid(pUsersSid);
1927 /* Test LookupAccountSid with Sid retrieved from token information.
1928 This assumes this process is running under the account of the current user.*/
1929 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &hToken);
1930 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1931 ret = GetTokenInformation(hToken, TokenUser, NULL, 0, &cbti);
1932 ok(!ret, "GetTokenInformation failed with error %d\n", GetLastError());
1933 ptiUser = HeapAlloc(GetProcessHeap(), 0, cbti);
1934 if (GetTokenInformation(hToken, TokenUser, ptiUser, cbti, &cbti))
1936 acc_sizeA = dom_sizeA = MAX_PATH;
1937 ret = LookupAccountSidA(NULL, ptiUser->User.Sid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1938 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1939 user_sizeA = MAX_PATH;
1940 ret = GetUserNameA(usernameA , &user_sizeA);
1941 ok(ret, "GetUserNameA() Expected TRUE, got FALSE\n");
1942 ok(lstrcmpA(usernameA, accountA) == 0, "LookupAccountSidA() Expected account name: %s got: %s\n", usernameA, accountA );
1944 HeapFree(GetProcessHeap(), 0, ptiUser);
1946 if (pCreateWellKnownSid && pConvertSidToStringSidA)
1948 trace("Well Known SIDs:\n");
1949 for (i = 0; i <= 60; i++)
1951 size = SECURITY_MAX_SID_SIZE;
1952 if (pCreateWellKnownSid(i, NULL, &max_sid.sid, &size))
1954 if (pConvertSidToStringSidA(&max_sid.sid, &str_sidA))
1956 acc_sizeA = MAX_PATH;
1957 dom_sizeA = MAX_PATH;
1958 if (LookupAccountSidA(NULL, &max_sid.sid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use))
1959 trace(" %d: %s %s\\%s %d\n", i, str_sidA, domainA, accountA, use);
1960 LocalFree(str_sidA);
1963 else
1965 if (GetLastError() != ERROR_INVALID_PARAMETER)
1966 trace(" CreateWellKnownSid(%d) failed: %d\n", i, GetLastError());
1967 else
1968 trace(" %d: not supported\n", i);
1972 pLsaQueryInformationPolicy = (void *)GetProcAddress( hmod, "LsaQueryInformationPolicy");
1973 pLsaOpenPolicy = (void *)GetProcAddress( hmod, "LsaOpenPolicy");
1974 pLsaFreeMemory = (void *)GetProcAddress( hmod, "LsaFreeMemory");
1975 pLsaClose = (void *)GetProcAddress( hmod, "LsaClose");
1977 if (pLsaQueryInformationPolicy && pLsaOpenPolicy && pLsaFreeMemory && pLsaClose)
1979 NTSTATUS status;
1980 LSA_HANDLE handle;
1981 LSA_OBJECT_ATTRIBUTES object_attributes;
1983 ZeroMemory(&object_attributes, sizeof(object_attributes));
1984 object_attributes.Length = sizeof(object_attributes);
1986 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_ALL_ACCESS, &handle);
1987 ok(status == STATUS_SUCCESS || status == STATUS_ACCESS_DENIED,
1988 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status);
1990 /* try a more restricted access mask if necessary */
1991 if (status == STATUS_ACCESS_DENIED) {
1992 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
1993 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_VIEW_LOCAL_INFORMATION, &handle);
1994 ok(status == STATUS_SUCCESS, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status);
1997 if (status == STATUS_SUCCESS)
1999 PPOLICY_ACCOUNT_DOMAIN_INFO info;
2000 status = pLsaQueryInformationPolicy(handle, PolicyAccountDomainInformation, (PVOID*)&info);
2001 ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy() failed, returned 0x%08x\n", status);
2002 if (status == STATUS_SUCCESS)
2004 ok(info->DomainSid!=0, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) missing SID\n");
2005 if (info->DomainSid)
2007 int count = *GetSidSubAuthorityCount(info->DomainSid);
2008 CopySid(GetSidLengthRequired(count), &max_sid, info->DomainSid);
2009 test_sid_str((PSID)&max_sid.sid);
2010 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_ADMIN;
2011 max_sid.sid.SubAuthorityCount = count + 1;
2012 test_sid_str((PSID)&max_sid.sid);
2013 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_GUEST;
2014 test_sid_str((PSID)&max_sid.sid);
2015 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ADMINS;
2016 test_sid_str((PSID)&max_sid.sid);
2017 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_USERS;
2018 test_sid_str((PSID)&max_sid.sid);
2019 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_GUESTS;
2020 test_sid_str((PSID)&max_sid.sid);
2021 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_COMPUTERS;
2022 test_sid_str((PSID)&max_sid.sid);
2023 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CONTROLLERS;
2024 test_sid_str((PSID)&max_sid.sid);
2025 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CERT_ADMINS;
2026 test_sid_str((PSID)&max_sid.sid);
2027 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_SCHEMA_ADMINS;
2028 test_sid_str((PSID)&max_sid.sid);
2029 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ENTERPRISE_ADMINS;
2030 test_sid_str((PSID)&max_sid.sid);
2031 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_POLICY_ADMINS;
2032 test_sid_str((PSID)&max_sid.sid);
2033 max_sid.sid.SubAuthority[count] = DOMAIN_ALIAS_RID_RAS_SERVERS;
2034 test_sid_str((PSID)&max_sid.sid);
2035 max_sid.sid.SubAuthority[count] = 1000; /* first user account */
2036 test_sid_str((PSID)&max_sid.sid);
2039 pLsaFreeMemory((LPVOID)info);
2042 status = pLsaClose(handle);
2043 ok(status == STATUS_SUCCESS, "LsaClose() failed, returned 0x%08x\n", status);
2049 static BOOL get_sid_info(PSID psid, LPSTR *user, LPSTR *dom)
2051 static CHAR account[UNLEN + 1];
2052 static CHAR domain[UNLEN + 1];
2053 DWORD size, dom_size;
2054 SID_NAME_USE use;
2056 *user = account;
2057 *dom = domain;
2059 size = dom_size = UNLEN + 1;
2060 account[0] = '\0';
2061 domain[0] = '\0';
2062 SetLastError(0xdeadbeef);
2063 return LookupAccountSidA(NULL, psid, account, &size, domain, &dom_size, &use);
2066 static void check_wellknown_name(const char* name, WELL_KNOWN_SID_TYPE result)
2068 SID_IDENTIFIER_AUTHORITY ident = { SECURITY_NT_AUTHORITY };
2069 PSID domainsid = NULL;
2070 char wk_sid[SECURITY_MAX_SID_SIZE];
2071 DWORD cb;
2073 DWORD sid_size, domain_size;
2074 SID_NAME_USE sid_use;
2075 LPSTR domain, account, sid_domain, wk_domain, wk_account;
2076 PSID psid;
2077 BOOL ret ,ret2;
2079 sid_size = 0;
2080 domain_size = 0;
2081 ret = LookupAccountNameA(NULL, name, NULL, &sid_size, NULL, &domain_size, &sid_use);
2082 ok(!ret, " %s Should have failed to lookup account name\n", name);
2083 psid = HeapAlloc(GetProcessHeap(),0,sid_size);
2084 domain = HeapAlloc(GetProcessHeap(),0,domain_size);
2085 ret = LookupAccountNameA(NULL, name, psid, &sid_size, domain, &domain_size, &sid_use);
2087 if (!result)
2089 ok(!ret, " %s Should have failed to lookup account name\n",name);
2090 goto cleanup;
2093 AllocateAndInitializeSid(&ident, 6, SECURITY_NT_NON_UNIQUE, 12, 23, 34, 45, 56, 0, 0, &domainsid);
2094 cb = sizeof(wk_sid);
2095 if (!pCreateWellKnownSid(result, domainsid, wk_sid, &cb))
2097 win_skip("SID %i is not available on the system\n",result);
2098 goto cleanup;
2101 ret2 = get_sid_info(wk_sid, &wk_account, &wk_domain);
2102 if (!ret2 && GetLastError() == ERROR_NONE_MAPPED)
2104 win_skip("CreateWellKnownSid() succeeded but the account '%s' is not present (W2K)\n", name);
2105 goto cleanup;
2108 get_sid_info(psid, &account, &sid_domain);
2110 ok(ret, "Failed to lookup account name %s\n",name);
2111 ok(sid_size != 0, "sid_size was zero\n");
2113 ok(EqualSid(psid,wk_sid),"(%s) Sids fail to match well known sid!\n",name);
2115 ok(!lstrcmp(account, wk_account), "Expected %s , got %s\n", account, wk_account);
2116 ok(!lstrcmp(domain, wk_domain), "Expected %s, got %s\n", wk_domain, domain);
2117 ok(sid_use == SidTypeWellKnownGroup , "Expected Use (5), got %d\n", sid_use);
2119 cleanup:
2120 FreeSid(domainsid);
2121 HeapFree(GetProcessHeap(),0,psid);
2122 HeapFree(GetProcessHeap(),0,domain);
2125 static void test_LookupAccountName(void)
2127 DWORD sid_size, domain_size, user_size;
2128 DWORD sid_save, domain_save;
2129 CHAR user_name[UNLEN + 1];
2130 CHAR computer_name[UNLEN + 1];
2131 SID_NAME_USE sid_use;
2132 LPSTR domain, account, sid_dom;
2133 PSID psid;
2134 BOOL ret;
2136 /* native crashes if (assuming all other parameters correct):
2137 * - peUse is NULL
2138 * - Sid is NULL and cbSid is > 0
2139 * - cbSid or cchReferencedDomainName are NULL
2140 * - ReferencedDomainName is NULL and cchReferencedDomainName is the correct size
2143 user_size = UNLEN + 1;
2144 SetLastError(0xdeadbeef);
2145 ret = GetUserNameA(user_name, &user_size);
2146 ok(ret, "Failed to get user name : %d\n", GetLastError());
2148 /* get sizes */
2149 sid_size = 0;
2150 domain_size = 0;
2151 sid_use = 0xcafebabe;
2152 SetLastError(0xdeadbeef);
2153 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, NULL, &domain_size, &sid_use);
2154 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2156 win_skip("LookupAccountNameA is not implemented\n");
2157 return;
2159 ok(!ret, "Expected 0, got %d\n", ret);
2160 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2161 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2162 ok(sid_size != 0, "Expected non-zero sid size\n");
2163 ok(domain_size != 0, "Expected non-zero domain size\n");
2164 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
2166 sid_save = sid_size;
2167 domain_save = domain_size;
2169 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
2170 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
2172 /* try valid account name */
2173 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, domain, &domain_size, &sid_use);
2174 get_sid_info(psid, &account, &sid_dom);
2175 ok(ret, "Failed to lookup account name\n");
2176 ok(sid_size == GetLengthSid(psid), "Expected %d, got %d\n", GetLengthSid(psid), sid_size);
2177 ok(!lstrcmp(account, user_name), "Expected %s, got %s\n", user_name, account);
2178 ok(!lstrcmp(domain, sid_dom), "Expected %s, got %s\n", sid_dom, domain);
2179 ok(domain_size == domain_save - 1, "Expected %d, got %d\n", domain_save - 1, domain_size);
2180 ok(strlen(domain) == domain_size, "Expected %d, got %d\n", lstrlen(domain), domain_size);
2181 ok(sid_use == SidTypeUser, "Expected SidTypeUser (%d), got %d\n", SidTypeUser, sid_use);
2182 domain_size = domain_save;
2183 sid_size = sid_save;
2185 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH)
2187 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2189 else
2191 ret = LookupAccountNameA(NULL, "Everyone", psid, &sid_size, domain, &domain_size, &sid_use);
2192 get_sid_info(psid, &account, &sid_dom);
2193 ok(ret, "Failed to lookup account name\n");
2194 ok(sid_size != 0, "sid_size was zero\n");
2195 ok(!lstrcmp(account, "Everyone"), "Expected Everyone, got %s\n", account);
2196 ok(!lstrcmp(domain, sid_dom), "Expected %s, got %s\n", sid_dom, domain);
2197 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
2198 ok(strlen(domain) == domain_size, "Expected %d, got %d\n", lstrlen(domain), domain_size);
2199 ok(sid_use == SidTypeWellKnownGroup, "Expected SidTypeWellKnownGroup (%d), got %d\n", SidTypeWellKnownGroup, sid_use);
2200 domain_size = domain_save;
2203 /* NULL Sid with zero sid size */
2204 SetLastError(0xdeadbeef);
2205 sid_size = 0;
2206 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
2207 ok(!ret, "Expected 0, got %d\n", ret);
2208 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2209 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2210 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
2211 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
2213 /* try cchReferencedDomainName - 1 */
2214 SetLastError(0xdeadbeef);
2215 domain_size--;
2216 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
2217 ok(!ret, "Expected 0, got %d\n", ret);
2218 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2219 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2220 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
2221 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
2223 /* NULL ReferencedDomainName with zero domain name size */
2224 SetLastError(0xdeadbeef);
2225 domain_size = 0;
2226 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, NULL, &domain_size, &sid_use);
2227 ok(!ret, "Expected 0, got %d\n", ret);
2228 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2229 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2230 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
2231 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
2233 HeapFree(GetProcessHeap(), 0, psid);
2234 HeapFree(GetProcessHeap(), 0, domain);
2236 /* get sizes for NULL account name */
2237 sid_size = 0;
2238 domain_size = 0;
2239 sid_use = 0xcafebabe;
2240 SetLastError(0xdeadbeef);
2241 ret = LookupAccountNameA(NULL, NULL, NULL, &sid_size, NULL, &domain_size, &sid_use);
2242 if (!ret && GetLastError() == ERROR_NONE_MAPPED)
2243 win_skip("NULL account name doesn't work on NT4\n");
2244 else
2246 ok(!ret, "Expected 0, got %d\n", ret);
2247 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2248 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2249 ok(sid_size != 0, "Expected non-zero sid size\n");
2250 ok(domain_size != 0, "Expected non-zero domain size\n");
2251 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
2253 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
2254 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
2256 /* try NULL account name */
2257 ret = LookupAccountNameA(NULL, NULL, psid, &sid_size, domain, &domain_size, &sid_use);
2258 get_sid_info(psid, &account, &sid_dom);
2259 ok(ret, "Failed to lookup account name\n");
2260 /* Using a fixed string will not work on different locales */
2261 ok(!lstrcmp(account, domain),
2262 "Got %s for account and %s for domain, these should be the same\n",
2263 account, domain);
2264 ok(sid_use == SidTypeDomain, "Expected SidTypeDomain (%d), got %d\n", SidTypeDomain, sid_use);
2266 HeapFree(GetProcessHeap(), 0, psid);
2267 HeapFree(GetProcessHeap(), 0, domain);
2270 /* try an invalid account name */
2271 SetLastError(0xdeadbeef);
2272 sid_size = 0;
2273 domain_size = 0;
2274 ret = LookupAccountNameA(NULL, "oogabooga", NULL, &sid_size, NULL, &domain_size, &sid_use);
2275 ok(!ret, "Expected 0, got %d\n", ret);
2276 ok(GetLastError() == ERROR_NONE_MAPPED ||
2277 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE),
2278 "Expected ERROR_NONE_MAPPED, got %d\n", GetLastError());
2279 ok(sid_size == 0, "Expected 0, got %d\n", sid_size);
2280 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
2282 /* try an invalid system name */
2283 SetLastError(0xdeadbeef);
2284 sid_size = 0;
2285 domain_size = 0;
2286 ret = LookupAccountNameA("deepthought", NULL, NULL, &sid_size, NULL, &domain_size, &sid_use);
2287 ok(!ret, "Expected 0, got %d\n", ret);
2288 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE || GetLastError() == RPC_S_INVALID_NET_ADDR /* Vista */,
2289 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
2290 ok(sid_size == 0, "Expected 0, got %d\n", sid_size);
2291 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
2293 /* try with the computer name as the account name */
2294 domain_size = sizeof(computer_name);
2295 GetComputerNameA(computer_name, &domain_size);
2296 sid_size = 0;
2297 domain_size = 0;
2298 ret = LookupAccountNameA(NULL, computer_name, NULL, &sid_size, NULL, &domain_size, &sid_use);
2299 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER ||
2300 GetLastError() == ERROR_NONE_MAPPED /* in a domain */ ||
2301 broken(GetLastError() == ERROR_TRUSTED_DOMAIN_FAILURE) ||
2302 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE)),
2303 "LookupAccountNameA failed: %d\n", GetLastError());
2304 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
2306 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
2307 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
2308 ret = LookupAccountNameA(NULL, computer_name, psid, &sid_size, domain, &domain_size, &sid_use);
2309 ok(ret, "LookupAccountNameA failed: %d\n", GetLastError());
2310 ok(sid_use == SidTypeDomain ||
2311 (sid_use == SidTypeUser && ! strcmp(computer_name, user_name)), "expected SidTypeDomain for %s, got %d\n", computer_name, sid_use);
2312 HeapFree(GetProcessHeap(), 0, domain);
2313 HeapFree(GetProcessHeap(), 0, psid);
2316 /* Well Known names */
2317 if (!pCreateWellKnownSid)
2319 win_skip("CreateWellKnownSid not available\n");
2320 return;
2323 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH)
2325 skip("Non-English locale (skipping well known name creation tests)\n");
2326 return;
2329 check_wellknown_name("LocalService", WinLocalServiceSid);
2330 check_wellknown_name("Local Service", WinLocalServiceSid);
2331 /* 2 spaces */
2332 check_wellknown_name("Local Service", 0);
2333 check_wellknown_name("NetworkService", WinNetworkServiceSid);
2334 check_wellknown_name("Network Service", WinNetworkServiceSid);
2336 /* example of some names where the spaces are not optional */
2337 check_wellknown_name("Terminal Server User", WinTerminalServerSid);
2338 check_wellknown_name("TerminalServer User", 0);
2339 check_wellknown_name("TerminalServerUser", 0);
2340 check_wellknown_name("Terminal ServerUser", 0);
2342 check_wellknown_name("enterprise domain controllers",WinEnterpriseControllersSid);
2343 check_wellknown_name("enterprisedomain controllers", 0);
2344 check_wellknown_name("enterprise domaincontrollers", 0);
2345 check_wellknown_name("enterprisedomaincontrollers", 0);
2347 /* case insensitivity */
2348 check_wellknown_name("lOCAlServICE", WinLocalServiceSid);
2350 /* fully qualified account names */
2351 check_wellknown_name("NT AUTHORITY\\LocalService", WinLocalServiceSid);
2352 check_wellknown_name("nt authority\\Network Service", WinNetworkServiceSid);
2353 check_wellknown_name("nt authority test\\Network Service", 0);
2354 check_wellknown_name("Dummy\\Network Service", 0);
2355 check_wellknown_name("ntauthority\\Network Service", 0);
2358 static void test_security_descriptor(void)
2360 SECURITY_DESCRIPTOR sd;
2361 char buf[8192];
2362 DWORD size;
2363 BOOL isDefault, isPresent, ret;
2364 PACL pacl;
2365 PSID psid;
2367 SetLastError(0xdeadbeef);
2368 ret = InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
2369 if (ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2371 win_skip("InitializeSecurityDescriptor is not implemented\n");
2372 return;
2375 ok(GetSecurityDescriptorOwner(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
2376 expect_eq(psid, NULL, PSID, "%p");
2377 expect_eq(isDefault, FALSE, BOOL, "%d");
2378 sd.Control |= SE_DACL_PRESENT | SE_SACL_PRESENT;
2380 SetLastError(0xdeadbeef);
2381 size = 5;
2382 expect_eq(MakeSelfRelativeSD(&sd, buf, &size), FALSE, BOOL, "%d");
2383 expect_eq(GetLastError(), ERROR_INSUFFICIENT_BUFFER, DWORD, "%u");
2384 ok(size > 5, "Size not increased\n");
2385 if (size <= 8192)
2387 expect_eq(MakeSelfRelativeSD(&sd, buf, &size), TRUE, BOOL, "%d");
2388 ok(GetSecurityDescriptorOwner(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
2389 expect_eq(psid, NULL, PSID, "%p");
2390 expect_eq(isDefault, FALSE, BOOL, "%d");
2391 ok(GetSecurityDescriptorGroup(&sd, &psid, &isDefault), "GetSecurityDescriptorGroup failed\n");
2392 expect_eq(psid, NULL, PSID, "%p");
2393 expect_eq(isDefault, FALSE, BOOL, "%d");
2394 ok(GetSecurityDescriptorDacl(&sd, &isPresent, &pacl, &isDefault), "GetSecurityDescriptorDacl failed\n");
2395 expect_eq(isPresent, TRUE, BOOL, "%d");
2396 expect_eq(psid, NULL, PSID, "%p");
2397 expect_eq(isDefault, FALSE, BOOL, "%d");
2398 ok(GetSecurityDescriptorSacl(&sd, &isPresent, &pacl, &isDefault), "GetSecurityDescriptorSacl failed\n");
2399 expect_eq(isPresent, TRUE, BOOL, "%d");
2400 expect_eq(psid, NULL, PSID, "%p");
2401 expect_eq(isDefault, FALSE, BOOL, "%d");
2405 #define TEST_GRANTED_ACCESS(a,b) test_granted_access(a,b,0,__LINE__)
2406 #define TEST_GRANTED_ACCESS2(a,b,c) test_granted_access(a,b,c,__LINE__)
2407 static void test_granted_access(HANDLE handle, ACCESS_MASK access,
2408 ACCESS_MASK alt, int line)
2410 OBJECT_BASIC_INFORMATION obj_info;
2411 NTSTATUS status;
2413 if (!pNtQueryObject)
2415 skip_(__FILE__, line)("Not NT platform - skipping tests\n");
2416 return;
2419 status = pNtQueryObject( handle, ObjectBasicInformation, &obj_info,
2420 sizeof(obj_info), NULL );
2421 ok_(__FILE__, line)(!status, "NtQueryObject with err: %08x\n", status);
2422 if (alt)
2423 ok_(__FILE__, line)(obj_info.GrantedAccess == access ||
2424 obj_info.GrantedAccess == alt, "Granted access should be 0x%08x "
2425 "or 0x%08x, instead of 0x%08x\n", access, alt, obj_info.GrantedAccess);
2426 else
2427 ok_(__FILE__, line)(obj_info.GrantedAccess == access, "Granted access should "
2428 "be 0x%08x, instead of 0x%08x\n", access, obj_info.GrantedAccess);
2431 #define CHECK_SET_SECURITY(o,i,e) \
2432 do{ \
2433 BOOL res; \
2434 DWORD err; \
2435 SetLastError( 0xdeadbeef ); \
2436 res = SetKernelObjectSecurity( o, i, SecurityDescriptor ); \
2437 err = GetLastError(); \
2438 if (e == ERROR_SUCCESS) \
2439 ok(res, "SetKernelObjectSecurity failed with %d\n", err); \
2440 else \
2441 ok(!res && err == e, "SetKernelObjectSecurity should have failed " \
2442 "with %s, instead of %d\n", #e, err); \
2443 }while(0)
2445 static void test_process_security(void)
2447 BOOL res;
2448 PTOKEN_OWNER owner;
2449 PTOKEN_PRIMARY_GROUP group;
2450 PSID AdminSid = NULL, UsersSid = NULL;
2451 PACL Acl = NULL;
2452 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL;
2453 char buffer[MAX_PATH];
2454 PROCESS_INFORMATION info;
2455 STARTUPINFOA startup;
2456 SECURITY_ATTRIBUTES psa;
2457 HANDLE token, event;
2458 DWORD size;
2460 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
2461 res = InitializeAcl(Acl, 256, ACL_REVISION);
2462 if (!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2464 win_skip("ACLs not implemented - skipping tests\n");
2465 HeapFree(GetProcessHeap(), 0, Acl);
2466 return;
2468 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
2470 /* get owner from the token we might be running as a user not admin */
2471 res = OpenProcessToken( GetCurrentProcess(), MAXIMUM_ALLOWED, &token );
2472 ok(res, "OpenProcessToken failed with error %d\n", GetLastError());
2473 if (!res)
2475 HeapFree(GetProcessHeap(), 0, Acl);
2476 return;
2479 res = GetTokenInformation( token, TokenOwner, NULL, 0, &size );
2480 ok(!res, "Expected failure, got %d\n", res);
2481 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2482 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2484 owner = HeapAlloc(GetProcessHeap(), 0, size);
2485 res = GetTokenInformation( token, TokenOwner, owner, size, &size );
2486 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
2487 AdminSid = ((TOKEN_OWNER*)owner)->Owner;
2489 res = GetTokenInformation( token, TokenPrimaryGroup, NULL, 0, &size );
2490 ok(!res, "Expected failure, got %d\n", res);
2491 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2492 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2494 group = HeapAlloc(GetProcessHeap(), 0, size);
2495 res = GetTokenInformation( token, TokenPrimaryGroup, group, size, &size );
2496 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
2497 UsersSid = ((TOKEN_PRIMARY_GROUP*)group)->PrimaryGroup;
2499 CloseHandle( token );
2500 if (!res)
2502 HeapFree(GetProcessHeap(), 0, group);
2503 HeapFree(GetProcessHeap(), 0, owner);
2504 HeapFree(GetProcessHeap(), 0, Acl);
2505 return;
2508 res = AddAccessDeniedAce(Acl, ACL_REVISION, PROCESS_VM_READ, AdminSid);
2509 ok(res, "AddAccessDeniedAce failed with error %d\n", GetLastError());
2510 res = AddAccessAllowedAce(Acl, ACL_REVISION, PROCESS_ALL_ACCESS, AdminSid);
2511 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2513 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
2514 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
2515 ok(res, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
2517 event = CreateEvent( NULL, TRUE, TRUE, "test_event" );
2518 ok(event != NULL, "CreateEvent %d\n", GetLastError());
2520 SecurityDescriptor->Revision = 0;
2521 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_UNKNOWN_REVISION );
2522 SecurityDescriptor->Revision = SECURITY_DESCRIPTOR_REVISION;
2524 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_INVALID_SECURITY_DESCR );
2525 CHECK_SET_SECURITY( event, GROUP_SECURITY_INFORMATION, ERROR_INVALID_SECURITY_DESCR );
2526 CHECK_SET_SECURITY( event, SACL_SECURITY_INFORMATION, ERROR_ACCESS_DENIED );
2527 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
2528 /* NULL DACL is valid and means default DACL from token */
2529 SecurityDescriptor->Control |= SE_DACL_PRESENT;
2530 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
2532 /* Set owner and group and dacl */
2533 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
2534 ok(res, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
2535 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_SUCCESS );
2536 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, FALSE);
2537 ok(res, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2538 CHECK_SET_SECURITY( event, GROUP_SECURITY_INFORMATION, ERROR_SUCCESS );
2539 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
2540 ok(res, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
2541 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
2543 sprintf(buffer, "%s tests/security.c test", myARGV[0]);
2544 memset(&startup, 0, sizeof(startup));
2545 startup.cb = sizeof(startup);
2546 startup.dwFlags = STARTF_USESHOWWINDOW;
2547 startup.wShowWindow = SW_SHOWNORMAL;
2549 psa.nLength = sizeof(psa);
2550 psa.lpSecurityDescriptor = SecurityDescriptor;
2551 psa.bInheritHandle = TRUE;
2553 /* Doesn't matter what ACL say we should get full access for ourselves */
2554 res = CreateProcessA( NULL, buffer, &psa, NULL, FALSE, 0, NULL, NULL, &startup, &info );
2555 ok(res, "CreateProcess with err:%d\n", GetLastError());
2556 TEST_GRANTED_ACCESS2( info.hProcess, PROCESS_ALL_ACCESS_NT4,
2557 STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL );
2558 winetest_wait_child_process( info.hProcess );
2560 CloseHandle( info.hProcess );
2561 CloseHandle( info.hThread );
2562 CloseHandle( event );
2563 HeapFree(GetProcessHeap(), 0, group);
2564 HeapFree(GetProcessHeap(), 0, owner);
2565 HeapFree(GetProcessHeap(), 0, Acl);
2566 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
2569 static void test_process_security_child(void)
2571 HANDLE handle, handle1;
2572 BOOL ret;
2573 DWORD err;
2575 handle = OpenProcess( PROCESS_TERMINATE, FALSE, GetCurrentProcessId() );
2576 ok(handle != NULL, "OpenProcess(PROCESS_TERMINATE) with err:%d\n", GetLastError());
2577 TEST_GRANTED_ACCESS( handle, PROCESS_TERMINATE );
2579 ret = DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
2580 &handle1, 0, TRUE, DUPLICATE_SAME_ACCESS );
2581 ok(ret, "duplicating handle err:%d\n", GetLastError());
2582 TEST_GRANTED_ACCESS( handle1, PROCESS_TERMINATE );
2584 CloseHandle( handle1 );
2586 SetLastError( 0xdeadbeef );
2587 ret = DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
2588 &handle1, PROCESS_ALL_ACCESS, TRUE, 0 );
2589 err = GetLastError();
2590 todo_wine
2591 ok(!ret && err == ERROR_ACCESS_DENIED, "duplicating handle should have failed "
2592 "with STATUS_ACCESS_DENIED, instead of err:%d\n", err);
2594 CloseHandle( handle );
2596 /* These two should fail - they are denied by ACL */
2597 handle = OpenProcess( PROCESS_VM_READ, FALSE, GetCurrentProcessId() );
2598 todo_wine
2599 ok(handle == NULL, "OpenProcess(PROCESS_VM_READ) should have failed\n");
2600 handle = OpenProcess( PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId() );
2601 todo_wine
2602 ok(handle == NULL, "OpenProcess(PROCESS_ALL_ACCESS) should have failed\n");
2604 /* Documented privilege elevation */
2605 ret = DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2606 &handle, 0, TRUE, DUPLICATE_SAME_ACCESS );
2607 ok(ret, "duplicating handle err:%d\n", GetLastError());
2608 TEST_GRANTED_ACCESS2( handle, PROCESS_ALL_ACCESS_NT4,
2609 STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL );
2611 CloseHandle( handle );
2613 /* Same only explicitly asking for all access rights */
2614 ret = DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2615 &handle, PROCESS_ALL_ACCESS, TRUE, 0 );
2616 ok(ret, "duplicating handle err:%d\n", GetLastError());
2617 TEST_GRANTED_ACCESS2( handle, PROCESS_ALL_ACCESS_NT4,
2618 PROCESS_ALL_ACCESS | PROCESS_QUERY_LIMITED_INFORMATION );
2619 ret = DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
2620 &handle1, PROCESS_VM_READ, TRUE, 0 );
2621 ok(ret, "duplicating handle err:%d\n", GetLastError());
2622 TEST_GRANTED_ACCESS( handle1, PROCESS_VM_READ );
2623 CloseHandle( handle1 );
2624 CloseHandle( handle );
2627 static void test_impersonation_level(void)
2629 HANDLE Token, ProcessToken;
2630 HANDLE Token2;
2631 DWORD Size;
2632 TOKEN_PRIVILEGES *Privileges;
2633 TOKEN_USER *User;
2634 PRIVILEGE_SET *PrivilegeSet;
2635 BOOL AccessGranted;
2636 BOOL ret;
2637 HKEY hkey;
2638 DWORD error;
2640 pDuplicateTokenEx = (void *)GetProcAddress(hmod, "DuplicateTokenEx");
2641 if( !pDuplicateTokenEx ) {
2642 win_skip("DuplicateTokenEx is not available\n");
2643 return;
2645 SetLastError(0xdeadbeef);
2646 ret = ImpersonateSelf(SecurityAnonymous);
2647 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2649 win_skip("ImpersonateSelf is not implemented\n");
2650 return;
2652 ok(ret, "ImpersonateSelf(SecurityAnonymous) failed with error %d\n", GetLastError());
2653 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
2654 ok(!ret, "OpenThreadToken should have failed\n");
2655 error = GetLastError();
2656 ok(error == ERROR_CANT_OPEN_ANONYMOUS, "OpenThreadToken on anonymous token should have returned ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error);
2657 /* can't perform access check when opening object against an anonymous impersonation token */
2658 todo_wine {
2659 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
2660 ok(error == ERROR_INVALID_HANDLE || error == ERROR_CANT_OPEN_ANONYMOUS,
2661 "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE or ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error);
2663 RevertToSelf();
2665 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, &ProcessToken);
2666 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
2668 ret = pDuplicateTokenEx(ProcessToken,
2669 TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_IMPERSONATE, NULL,
2670 SecurityAnonymous, TokenImpersonation, &Token);
2671 ok(ret, "DuplicateTokenEx failed with error %d\n", GetLastError());
2672 /* can't increase the impersonation level */
2673 ret = DuplicateToken(Token, SecurityIdentification, &Token2);
2674 error = GetLastError();
2675 ok(!ret && error == ERROR_BAD_IMPERSONATION_LEVEL,
2676 "Duplicating a token and increasing the impersonation level should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
2677 /* we can query anything from an anonymous token, including the user */
2678 ret = GetTokenInformation(Token, TokenUser, NULL, 0, &Size);
2679 error = GetLastError();
2680 ok(!ret && error == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenUser) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error);
2681 User = HeapAlloc(GetProcessHeap(), 0, Size);
2682 ret = GetTokenInformation(Token, TokenUser, User, Size, &Size);
2683 ok(ret, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
2684 HeapFree(GetProcessHeap(), 0, User);
2686 /* PrivilegeCheck fails with SecurityAnonymous level */
2687 ret = GetTokenInformation(Token, TokenPrivileges, NULL, 0, &Size);
2688 error = GetLastError();
2689 ok(!ret && error == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenPrivileges) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error);
2690 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
2691 ret = GetTokenInformation(Token, TokenPrivileges, Privileges, Size, &Size);
2692 ok(ret, "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
2694 PrivilegeSet = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PRIVILEGE_SET, Privilege[Privileges->PrivilegeCount]));
2695 PrivilegeSet->PrivilegeCount = Privileges->PrivilegeCount;
2696 memcpy(PrivilegeSet->Privilege, Privileges->Privileges, PrivilegeSet->PrivilegeCount * sizeof(PrivilegeSet->Privilege[0]));
2697 PrivilegeSet->Control = PRIVILEGE_SET_ALL_NECESSARY;
2698 HeapFree(GetProcessHeap(), 0, Privileges);
2700 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2701 error = GetLastError();
2702 ok(!ret && error == ERROR_BAD_IMPERSONATION_LEVEL, "PrivilegeCheck for SecurityAnonymous token should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
2704 CloseHandle(Token);
2706 ret = ImpersonateSelf(SecurityIdentification);
2707 ok(ret, "ImpersonateSelf(SecurityIdentification) failed with error %d\n", GetLastError());
2708 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
2709 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
2711 /* can't perform access check when opening object against an identification impersonation token */
2712 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
2713 todo_wine {
2714 ok(error == ERROR_INVALID_HANDLE || error == ERROR_BAD_IMPERSONATION_LEVEL,
2715 "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE or ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
2717 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2718 ok(ret, "PrivilegeCheck for SecurityIdentification failed with error %d\n", GetLastError());
2719 CloseHandle(Token);
2720 RevertToSelf();
2722 ret = ImpersonateSelf(SecurityImpersonation);
2723 ok(ret, "ImpersonateSelf(SecurityImpersonation) failed with error %d\n", GetLastError());
2724 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
2725 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
2726 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
2727 ok(error == ERROR_SUCCESS, "RegOpenKeyEx should have succeeded instead of failing with %d\n", error);
2728 RegCloseKey(hkey);
2729 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2730 ok(ret, "PrivilegeCheck for SecurityImpersonation failed with error %d\n", GetLastError());
2731 RevertToSelf();
2733 CloseHandle(Token);
2734 CloseHandle(ProcessToken);
2736 HeapFree(GetProcessHeap(), 0, PrivilegeSet);
2739 static void test_SetEntriesInAclW(void)
2741 DWORD res;
2742 PSID EveryoneSid = NULL, UsersSid = NULL;
2743 PACL OldAcl = NULL, NewAcl;
2744 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
2745 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
2746 EXPLICIT_ACCESSW ExplicitAccess;
2747 static const WCHAR wszEveryone[] = {'E','v','e','r','y','o','n','e',0};
2748 static const WCHAR wszCurrentUser[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
2750 if (!pSetEntriesInAclW)
2752 win_skip("SetEntriesInAclW is not available\n");
2753 return;
2756 NewAcl = (PACL)0xdeadbeef;
2757 res = pSetEntriesInAclW(0, NULL, NULL, &NewAcl);
2758 if(res == ERROR_CALL_NOT_IMPLEMENTED)
2760 win_skip("SetEntriesInAclW is not implemented\n");
2761 return;
2763 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2764 ok(NewAcl == NULL ||
2765 broken(NewAcl != NULL), /* NT4 */
2766 "NewAcl=%p, expected NULL\n", NewAcl);
2767 LocalFree(NewAcl);
2769 OldAcl = HeapAlloc(GetProcessHeap(), 0, 256);
2770 res = InitializeAcl(OldAcl, 256, ACL_REVISION);
2771 if(!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2773 win_skip("ACLs not implemented - skipping tests\n");
2774 HeapFree(GetProcessHeap(), 0, OldAcl);
2775 return;
2777 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
2779 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
2780 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2782 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
2783 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &UsersSid);
2784 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2786 res = AddAccessAllowedAce(OldAcl, ACL_REVISION, KEY_READ, UsersSid);
2787 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2789 ExplicitAccess.grfAccessPermissions = KEY_WRITE;
2790 ExplicitAccess.grfAccessMode = GRANT_ACCESS;
2791 ExplicitAccess.grfInheritance = NO_INHERITANCE;
2792 ExplicitAccess.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
2793 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
2794 ExplicitAccess.Trustee.ptstrName = EveryoneSid;
2795 ExplicitAccess.Trustee.MultipleTrusteeOperation = 0xDEADBEEF;
2796 ExplicitAccess.Trustee.pMultipleTrustee = (PVOID)0xDEADBEEF;
2797 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2798 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2799 ok(NewAcl != NULL, "returned acl was NULL\n");
2800 LocalFree(NewAcl);
2802 ExplicitAccess.Trustee.TrusteeType = TRUSTEE_IS_UNKNOWN;
2803 ExplicitAccess.Trustee.pMultipleTrustee = NULL;
2804 ExplicitAccess.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2805 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2806 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2807 ok(NewAcl != NULL, "returned acl was NULL\n");
2808 LocalFree(NewAcl);
2810 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH)
2812 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2814 else
2816 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
2817 ExplicitAccess.Trustee.ptstrName = (LPWSTR)wszEveryone;
2818 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2819 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2820 ok(NewAcl != NULL, "returned acl was NULL\n");
2821 LocalFree(NewAcl);
2823 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_BAD_FORM;
2824 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2825 ok(res == ERROR_INVALID_PARAMETER ||
2826 broken(res == ERROR_NOT_SUPPORTED), /* NT4 */
2827 "SetEntriesInAclW failed: %u\n", res);
2828 ok(NewAcl == NULL ||
2829 broken(NewAcl != NULL), /* NT4 */
2830 "returned acl wasn't NULL: %p\n", NewAcl);
2832 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
2833 ExplicitAccess.Trustee.MultipleTrusteeOperation = TRUSTEE_IS_IMPERSONATE;
2834 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2835 ok(res == ERROR_INVALID_PARAMETER ||
2836 broken(res == ERROR_NOT_SUPPORTED), /* NT4 */
2837 "SetEntriesInAclW failed: %u\n", res);
2838 ok(NewAcl == NULL ||
2839 broken(NewAcl != NULL), /* NT4 */
2840 "returned acl wasn't NULL: %p\n", NewAcl);
2842 ExplicitAccess.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2843 ExplicitAccess.grfAccessMode = SET_ACCESS;
2844 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2845 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2846 ok(NewAcl != NULL, "returned acl was NULL\n");
2847 LocalFree(NewAcl);
2850 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
2851 ExplicitAccess.Trustee.ptstrName = (LPWSTR)wszCurrentUser;
2852 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2853 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2854 ok(NewAcl != NULL, "returned acl was NULL\n");
2855 LocalFree(NewAcl);
2857 ExplicitAccess.grfAccessMode = REVOKE_ACCESS;
2858 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
2859 ExplicitAccess.Trustee.ptstrName = UsersSid;
2860 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2861 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2862 ok(NewAcl != NULL, "returned acl was NULL\n");
2863 LocalFree(NewAcl);
2865 FreeSid(UsersSid);
2866 FreeSid(EveryoneSid);
2867 HeapFree(GetProcessHeap(), 0, OldAcl);
2870 static void test_SetEntriesInAclA(void)
2872 DWORD res;
2873 PSID EveryoneSid = NULL, UsersSid = NULL;
2874 PACL OldAcl = NULL, NewAcl;
2875 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
2876 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
2877 EXPLICIT_ACCESS ExplicitAccess;
2878 static const CHAR szEveryone[] = {'E','v','e','r','y','o','n','e',0};
2879 static const CHAR szCurrentUser[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
2881 if (!pSetEntriesInAclA)
2883 win_skip("SetEntriesInAclA is not available\n");
2884 return;
2887 NewAcl = (PACL)0xdeadbeef;
2888 res = pSetEntriesInAclA(0, NULL, NULL, &NewAcl);
2889 if(res == ERROR_CALL_NOT_IMPLEMENTED)
2891 win_skip("SetEntriesInAclA is not implemented\n");
2892 return;
2894 ok(res == ERROR_SUCCESS, "SetEntriesInAclA failed: %u\n", res);
2895 ok(NewAcl == NULL ||
2896 broken(NewAcl != NULL), /* NT4 */
2897 "NewAcl=%p, expected NULL\n", NewAcl);
2898 LocalFree(NewAcl);
2900 OldAcl = HeapAlloc(GetProcessHeap(), 0, 256);
2901 res = InitializeAcl(OldAcl, 256, ACL_REVISION);
2902 if(!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2904 win_skip("ACLs not implemented - skipping tests\n");
2905 HeapFree(GetProcessHeap(), 0, OldAcl);
2906 return;
2908 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
2910 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
2911 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2913 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
2914 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &UsersSid);
2915 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2917 res = AddAccessAllowedAce(OldAcl, ACL_REVISION, KEY_READ, UsersSid);
2918 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2920 ExplicitAccess.grfAccessPermissions = KEY_WRITE;
2921 ExplicitAccess.grfAccessMode = GRANT_ACCESS;
2922 ExplicitAccess.grfInheritance = NO_INHERITANCE;
2923 ExplicitAccess.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
2924 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
2925 ExplicitAccess.Trustee.ptstrName = EveryoneSid;
2926 ExplicitAccess.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2927 ExplicitAccess.Trustee.pMultipleTrustee = NULL;
2928 res = pSetEntriesInAclA(1, &ExplicitAccess, OldAcl, &NewAcl);
2929 ok(res == ERROR_SUCCESS, "SetEntriesInAclA failed: %u\n", res);
2930 ok(NewAcl != NULL, "returned acl was NULL\n");
2931 LocalFree(NewAcl);
2933 ExplicitAccess.Trustee.TrusteeType = TRUSTEE_IS_UNKNOWN;
2934 ExplicitAccess.Trustee.pMultipleTrustee = NULL;
2935 ExplicitAccess.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2936 res = pSetEntriesInAclA(1, &ExplicitAccess, OldAcl, &NewAcl);
2937 ok(res == ERROR_SUCCESS, "SetEntriesInAclA failed: %u\n", res);
2938 ok(NewAcl != NULL, "returned acl was NULL\n");
2939 LocalFree(NewAcl);
2941 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH)
2943 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2945 else
2947 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
2948 ExplicitAccess.Trustee.ptstrName = (LPSTR)szEveryone;
2949 res = pSetEntriesInAclA(1, &ExplicitAccess, OldAcl, &NewAcl);
2950 ok(res == ERROR_SUCCESS, "SetEntriesInAclA failed: %u\n", res);
2951 ok(NewAcl != NULL, "returned acl was NULL\n");
2952 LocalFree(NewAcl);
2954 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_BAD_FORM;
2955 res = pSetEntriesInAclA(1, &ExplicitAccess, OldAcl, &NewAcl);
2956 ok(res == ERROR_INVALID_PARAMETER ||
2957 broken(res == ERROR_NOT_SUPPORTED), /* NT4 */
2958 "SetEntriesInAclA failed: %u\n", res);
2959 ok(NewAcl == NULL ||
2960 broken(NewAcl != NULL), /* NT4 */
2961 "returned acl wasn't NULL: %p\n", NewAcl);
2963 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
2964 ExplicitAccess.Trustee.MultipleTrusteeOperation = TRUSTEE_IS_IMPERSONATE;
2965 res = pSetEntriesInAclA(1, &ExplicitAccess, OldAcl, &NewAcl);
2966 ok(res == ERROR_INVALID_PARAMETER ||
2967 broken(res == ERROR_NOT_SUPPORTED), /* NT4 */
2968 "SetEntriesInAclA failed: %u\n", res);
2969 ok(NewAcl == NULL ||
2970 broken(NewAcl != NULL), /* NT4 */
2971 "returned acl wasn't NULL: %p\n", NewAcl);
2973 ExplicitAccess.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2974 ExplicitAccess.grfAccessMode = SET_ACCESS;
2975 res = pSetEntriesInAclA(1, &ExplicitAccess, OldAcl, &NewAcl);
2976 ok(res == ERROR_SUCCESS, "SetEntriesInAclA failed: %u\n", res);
2977 ok(NewAcl != NULL, "returned acl was NULL\n");
2978 LocalFree(NewAcl);
2981 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
2982 ExplicitAccess.Trustee.ptstrName = (LPSTR)szCurrentUser;
2983 res = pSetEntriesInAclA(1, &ExplicitAccess, OldAcl, &NewAcl);
2984 ok(res == ERROR_SUCCESS, "SetEntriesInAclA failed: %u\n", res);
2985 ok(NewAcl != NULL, "returned acl was NULL\n");
2986 LocalFree(NewAcl);
2988 ExplicitAccess.grfAccessMode = REVOKE_ACCESS;
2989 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
2990 ExplicitAccess.Trustee.ptstrName = UsersSid;
2991 res = pSetEntriesInAclA(1, &ExplicitAccess, OldAcl, &NewAcl);
2992 ok(res == ERROR_SUCCESS, "SetEntriesInAclA failed: %u\n", res);
2993 ok(NewAcl != NULL, "returned acl was NULL\n");
2994 LocalFree(NewAcl);
2996 FreeSid(UsersSid);
2997 FreeSid(EveryoneSid);
2998 HeapFree(GetProcessHeap(), 0, OldAcl);
3001 static void test_GetNamedSecurityInfoA(void)
3003 PSECURITY_DESCRIPTOR pSecDesc;
3004 DWORD revision;
3005 SECURITY_DESCRIPTOR_CONTROL control;
3006 PSID owner;
3007 PSID group;
3008 PACL dacl;
3009 BOOL owner_defaulted;
3010 BOOL group_defaulted;
3011 DWORD error;
3012 BOOL ret, isNT4;
3013 CHAR windows_dir[MAX_PATH];
3015 if (!pGetNamedSecurityInfoA)
3017 win_skip("GetNamedSecurityInfoA is not available\n");
3018 return;
3021 ret = GetWindowsDirectoryA(windows_dir, MAX_PATH);
3022 ok(ret, "GetWindowsDirectory failed with error %d\n", GetLastError());
3024 SetLastError(0xdeadbeef);
3025 error = pGetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,
3026 OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION,
3027 NULL, NULL, NULL, NULL, &pSecDesc);
3028 if (error != ERROR_SUCCESS && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
3030 win_skip("GetNamedSecurityInfoA is not implemented\n");
3031 return;
3033 ok(!error, "GetNamedSecurityInfo failed with error %d\n", error);
3035 ret = GetSecurityDescriptorControl(pSecDesc, &control, &revision);
3036 ok(ret, "GetSecurityDescriptorControl failed with error %d\n", GetLastError());
3037 ok((control & (SE_SELF_RELATIVE|SE_DACL_PRESENT)) == (SE_SELF_RELATIVE|SE_DACL_PRESENT) ||
3038 broken((control & (SE_SELF_RELATIVE|SE_DACL_PRESENT)) == SE_DACL_PRESENT), /* NT4 */
3039 "control (0x%x) doesn't have (SE_SELF_RELATIVE|SE_DACL_PRESENT) flags set\n", control);
3040 ok(revision == SECURITY_DESCRIPTOR_REVISION1, "revision was %d instead of 1\n", revision);
3042 isNT4 = (control & (SE_SELF_RELATIVE|SE_DACL_PRESENT)) == SE_DACL_PRESENT;
3044 ret = GetSecurityDescriptorOwner(pSecDesc, &owner, &owner_defaulted);
3045 ok(ret, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
3046 ok(owner != NULL, "owner should not be NULL\n");
3048 ret = GetSecurityDescriptorGroup(pSecDesc, &group, &group_defaulted);
3049 ok(ret, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
3050 ok(group != NULL, "group should not be NULL\n");
3051 LocalFree(pSecDesc);
3054 /* NULL descriptor tests */
3055 if(isNT4)
3057 win_skip("NT4 does not support GetNamedSecutityInfo with a NULL descriptor\n");
3058 return;
3061 error = pGetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,DACL_SECURITY_INFORMATION,
3062 NULL, NULL, NULL, NULL, NULL);
3063 ok(error==ERROR_INVALID_PARAMETER, "GetNamedSecurityInfo failed with error %d\n", error);
3065 error = pGetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,DACL_SECURITY_INFORMATION,
3066 NULL, NULL, &dacl, NULL, NULL);
3067 ok(!error, "GetNamedSecurityInfo failed with error %d\n", error);
3068 ok(dacl != NULL, "dacl should not be NULL\n");
3070 error = pGetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,OWNER_SECURITY_INFORMATION,
3071 NULL, NULL, &dacl, NULL, NULL);
3072 ok(error==ERROR_INVALID_PARAMETER, "GetNamedSecurityInfo failed with error %d\n", error);
3075 static void test_ConvertStringSecurityDescriptor(void)
3077 BOOL ret;
3078 PSECURITY_DESCRIPTOR pSD;
3079 static const WCHAR Blank[] = { 0 };
3080 unsigned int i;
3081 static const struct
3083 const char *sidstring;
3084 DWORD revision;
3085 BOOL ret;
3086 DWORD GLE;
3087 DWORD altGLE;
3088 } cssd[] =
3090 { "D:(A;;GA;;;WD)", 0xdeadbeef, FALSE, ERROR_UNKNOWN_REVISION },
3091 /* test ACE string type */
3092 { "D:(A;;GA;;;WD)", SDDL_REVISION_1, TRUE },
3093 { "D:(D;;GA;;;WD)", SDDL_REVISION_1, TRUE },
3094 { "ERROR:(D;;GA;;;WD)", SDDL_REVISION_1, FALSE, ERROR_INVALID_PARAMETER },
3095 /* test ACE string with spaces */
3096 { " D:(D;;GA;;;WD)", SDDL_REVISION_1, TRUE },
3097 { "D: (D;;GA;;;WD)", SDDL_REVISION_1, TRUE },
3098 { "D:( D;;GA;;;WD)", SDDL_REVISION_1, TRUE },
3099 { "D:(D ;;GA;;;WD)", SDDL_REVISION_1, FALSE, RPC_S_INVALID_STRING_UUID, ERROR_INVALID_ACL }, /* Vista+ */
3100 { "D:(D; ;GA;;;WD)", SDDL_REVISION_1, TRUE },
3101 { "D:(D;; GA;;;WD)", SDDL_REVISION_1, TRUE },
3102 { "D:(D;;GA ;;;WD)", SDDL_REVISION_1, FALSE, ERROR_INVALID_ACL },
3103 { "D:(D;;GA; ;;WD)", SDDL_REVISION_1, TRUE },
3104 { "D:(D;;GA;; ;WD)", SDDL_REVISION_1, TRUE },
3105 { "D:(D;;GA;;; WD)", SDDL_REVISION_1, TRUE },
3106 { "D:(D;;GA;;;WD )", SDDL_REVISION_1, TRUE },
3107 /* test ACE string access rights */
3108 { "D:(A;;GA;;;WD)", SDDL_REVISION_1, TRUE },
3109 { "D:(A;;GRGWGX;;;WD)", SDDL_REVISION_1, TRUE },
3110 { "D:(A;;RCSDWDWO;;;WD)", SDDL_REVISION_1, TRUE },
3111 { "D:(A;;RPWPCCDCLCSWLODTCR;;;WD)", SDDL_REVISION_1, TRUE },
3112 { "D:(A;;FAFRFWFX;;;WD)", SDDL_REVISION_1, TRUE },
3113 { "D:(A;;KAKRKWKX;;;WD)", SDDL_REVISION_1, TRUE },
3114 { "D:(A;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1, TRUE },
3115 { "S:(AU;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1, TRUE },
3116 /* test ACE string access right error case */
3117 { "D:(A;;ROB;;;WD)", SDDL_REVISION_1, FALSE, ERROR_INVALID_ACL },
3118 /* test behaviour with empty strings */
3119 { "", SDDL_REVISION_1, TRUE },
3120 /* test ACE string SID */
3121 { "D:(D;;GA;;;S-1-0-0)", SDDL_REVISION_1, TRUE },
3122 { "D:(D;;GA;;;Nonexistent account)", SDDL_REVISION_1, FALSE, ERROR_INVALID_ACL, ERROR_INVALID_SID } /* W2K */
3125 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA)
3127 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
3128 return;
3131 for (i = 0; i < sizeof(cssd)/sizeof(cssd[0]); i++)
3133 DWORD GLE;
3135 SetLastError(0xdeadbeef);
3136 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
3137 cssd[i].sidstring, cssd[i].revision, &pSD, NULL);
3138 GLE = GetLastError();
3139 ok(ret == cssd[i].ret, "(%02u) Expected %s (%d)\n", i, cssd[i].ret ? "success" : "failure", GLE);
3140 if (!cssd[i].ret)
3141 ok(GLE == cssd[i].GLE ||
3142 (cssd[i].altGLE && GLE == cssd[i].altGLE),
3143 "(%02u) Unexpected last error %d\n", i, GLE);
3144 if (ret)
3145 LocalFree(pSD);
3148 /* test behaviour with NULL parameters */
3149 SetLastError(0xdeadbeef);
3150 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
3151 NULL, 0xdeadbeef, &pSD, NULL);
3152 todo_wine
3153 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
3154 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3155 GetLastError());
3157 SetLastError(0xdeadbeef);
3158 ret = pConvertStringSecurityDescriptorToSecurityDescriptorW(
3159 NULL, 0xdeadbeef, &pSD, NULL);
3160 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
3161 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3162 GetLastError());
3164 SetLastError(0xdeadbeef);
3165 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
3166 "D:(A;;ROB;;;WD)", 0xdeadbeef, NULL, NULL);
3167 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
3168 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3169 GetLastError());
3171 SetLastError(0xdeadbeef);
3172 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
3173 "D:(A;;ROB;;;WD)", SDDL_REVISION_1, NULL, NULL);
3174 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
3175 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3176 GetLastError());
3178 /* test behaviour with empty strings */
3179 SetLastError(0xdeadbeef);
3180 ret = pConvertStringSecurityDescriptorToSecurityDescriptorW(
3181 Blank, SDDL_REVISION_1, &pSD, NULL);
3182 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
3183 LocalFree(pSD);
3186 static void test_ConvertSecurityDescriptorToString(void)
3188 SECURITY_DESCRIPTOR desc;
3189 SECURITY_INFORMATION sec_info = OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION|SACL_SECURITY_INFORMATION;
3190 LPSTR string;
3191 DWORD size;
3192 PSID psid, psid2;
3193 PACL pacl;
3194 char sid_buf[256];
3195 char acl_buf[8192];
3196 ULONG len;
3198 if (!pConvertSecurityDescriptorToStringSecurityDescriptorA)
3200 win_skip("ConvertSecurityDescriptorToStringSecurityDescriptor is not available\n");
3201 return;
3203 if (!pCreateWellKnownSid)
3205 win_skip("CreateWellKnownSid is not available\n");
3206 return;
3209 /* It seems Windows XP adds an extra character to the length of the string for each ACE in an ACL. We
3210 * don't replicate this feature so we only test len >= strlen+1. */
3211 #define CHECK_RESULT_AND_FREE(exp_str) \
3212 ok(strcmp(string, (exp_str)) == 0, "String mismatch (expected \"%s\", got \"%s\")\n", (exp_str), string); \
3213 ok(len >= (strlen(exp_str) + 1), "Length mismatch (expected %d, got %d)\n", lstrlen(exp_str) + 1, len); \
3214 LocalFree(string);
3216 #define CHECK_ONE_OF_AND_FREE(exp_str1, exp_str2) \
3217 ok(strcmp(string, (exp_str1)) == 0 || strcmp(string, (exp_str2)) == 0, "String mismatch (expected\n\"%s\" or\n\"%s\", got\n\"%s\")\n", (exp_str1), (exp_str2), string); \
3218 ok(len >= (strlen(exp_str1) + 1) || len >= (strlen(exp_str2) + 1), "Length mismatch (expected %d or %d, got %d)\n", lstrlen(exp_str1) + 1, lstrlen(exp_str2) + 1, len); \
3219 LocalFree(string);
3221 InitializeSecurityDescriptor(&desc, SECURITY_DESCRIPTOR_REVISION);
3222 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3223 CHECK_RESULT_AND_FREE("");
3225 size = 4096;
3226 pCreateWellKnownSid(WinLocalSid, NULL, sid_buf, &size);
3227 SetSecurityDescriptorOwner(&desc, sid_buf, FALSE);
3228 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3229 CHECK_RESULT_AND_FREE("O:S-1-2-0");
3231 SetSecurityDescriptorOwner(&desc, sid_buf, TRUE);
3232 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3233 CHECK_RESULT_AND_FREE("O:S-1-2-0");
3235 size = sizeof(sid_buf);
3236 pCreateWellKnownSid(WinLocalSystemSid, NULL, sid_buf, &size);
3237 SetSecurityDescriptorOwner(&desc, sid_buf, TRUE);
3238 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3239 CHECK_RESULT_AND_FREE("O:SY");
3241 pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid);
3242 SetSecurityDescriptorGroup(&desc, psid, TRUE);
3243 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3244 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576");
3246 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, GROUP_SECURITY_INFORMATION, &string, &len), "Conversion failed\n");
3247 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
3249 pacl = (PACL)acl_buf;
3250 InitializeAcl(pacl, sizeof(acl_buf), ACL_REVISION);
3251 SetSecurityDescriptorDacl(&desc, TRUE, pacl, TRUE);
3252 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3253 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
3255 SetSecurityDescriptorDacl(&desc, TRUE, pacl, FALSE);
3256 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3257 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
3259 pConvertStringSidToSidA("S-1-5-6", &psid2);
3260 pAddAccessAllowedAceEx(pacl, ACL_REVISION, NO_PROPAGATE_INHERIT_ACE, 0xf0000000, psid2);
3261 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3262 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)");
3264 pAddAccessAllowedAceEx(pacl, ACL_REVISION, INHERIT_ONLY_ACE|INHERITED_ACE, 0x00000003, psid2);
3265 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3266 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)");
3268 pAddAccessDeniedAceEx(pacl, ACL_REVISION, OBJECT_INHERIT_ACE|CONTAINER_INHERIT_ACE, 0xffffffff, psid);
3269 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3270 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)");
3273 pacl = (PACL)acl_buf;
3274 InitializeAcl(pacl, sizeof(acl_buf), ACL_REVISION);
3275 SetSecurityDescriptorSacl(&desc, TRUE, pacl, FALSE);
3276 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3277 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:");
3279 /* fails in win2k */
3280 SetSecurityDescriptorDacl(&desc, TRUE, NULL, FALSE);
3281 pAddAuditAccessAceEx(pacl, ACL_REVISION, VALID_INHERIT_FLAGS, KEY_READ|KEY_WRITE, psid2, TRUE, TRUE);
3282 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len))
3284 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)", /* XP */
3285 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)" /* Vista */);
3288 /* fails in win2k */
3289 pAddAuditAccessAceEx(pacl, ACL_REVISION, NO_PROPAGATE_INHERIT_ACE, FILE_GENERIC_READ|FILE_GENERIC_WRITE, psid2, TRUE, FALSE);
3290 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len))
3292 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", /* XP */
3293 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)" /* Vista */);
3296 LocalFree(psid2);
3297 LocalFree(psid);
3300 static void test_SetSecurityDescriptorControl (PSECURITY_DESCRIPTOR sec)
3302 SECURITY_DESCRIPTOR_CONTROL ref;
3303 SECURITY_DESCRIPTOR_CONTROL test;
3305 SECURITY_DESCRIPTOR_CONTROL const mutable
3306 = SE_DACL_AUTO_INHERIT_REQ | SE_SACL_AUTO_INHERIT_REQ
3307 | SE_DACL_AUTO_INHERITED | SE_SACL_AUTO_INHERITED
3308 | SE_DACL_PROTECTED | SE_SACL_PROTECTED
3309 | 0x00000040 | 0x00000080 /* not defined in winnt.h */
3311 SECURITY_DESCRIPTOR_CONTROL const immutable
3312 = SE_OWNER_DEFAULTED | SE_GROUP_DEFAULTED
3313 | SE_DACL_PRESENT | SE_DACL_DEFAULTED
3314 | SE_SACL_PRESENT | SE_SACL_DEFAULTED
3315 | SE_RM_CONTROL_VALID | SE_SELF_RELATIVE
3318 int bit;
3319 DWORD dwRevision;
3320 LPCSTR fmt = "Expected error %s, got %u\n";
3322 GetSecurityDescriptorControl (sec, &ref, &dwRevision);
3324 /* The mutable bits are mutable regardless of the truth of
3325 SE_DACL_PRESENT and/or SE_SACL_PRESENT */
3327 /* Check call barfs if any bit-of-interest is immutable */
3328 for (bit = 0; bit < 16; ++bit)
3330 SECURITY_DESCRIPTOR_CONTROL const bitOfInterest = 1 << bit;
3331 SECURITY_DESCRIPTOR_CONTROL setOrClear = ref & bitOfInterest;
3333 SECURITY_DESCRIPTOR_CONTROL ctrl;
3335 DWORD dwExpect = (bitOfInterest & immutable)
3336 ? ERROR_INVALID_PARAMETER : 0xbebecaca;
3337 LPCSTR strExpect = (bitOfInterest & immutable)
3338 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
3340 ctrl = (bitOfInterest & mutable) ? ref + bitOfInterest : ref;
3341 setOrClear ^= bitOfInterest;
3342 SetLastError (0xbebecaca);
3343 pSetSecurityDescriptorControl (sec, bitOfInterest, setOrClear);
3344 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
3345 GetSecurityDescriptorControl(sec, &test, &dwRevision);
3346 expect_eq(test, ctrl, int, "%x");
3348 setOrClear ^= bitOfInterest;
3349 SetLastError (0xbebecaca);
3350 pSetSecurityDescriptorControl (sec, bitOfInterest, setOrClear);
3351 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
3352 GetSecurityDescriptorControl (sec, &test, &dwRevision);
3353 expect_eq(test, ref, int, "%x");
3356 /* Check call barfs if any bit-to-set is immutable
3357 even when not a bit-of-interest */
3358 for (bit = 0; bit < 16; ++bit)
3360 SECURITY_DESCRIPTOR_CONTROL const bitsOfInterest = mutable;
3361 SECURITY_DESCRIPTOR_CONTROL setOrClear = ref & bitsOfInterest;
3363 SECURITY_DESCRIPTOR_CONTROL ctrl;
3365 DWORD dwExpect = ((1 << bit) & immutable)
3366 ? ERROR_INVALID_PARAMETER : 0xbebecaca;
3367 LPCSTR strExpect = ((1 << bit) & immutable)
3368 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
3370 ctrl = ((1 << bit) & immutable) ? test : ref | mutable;
3371 setOrClear ^= bitsOfInterest;
3372 SetLastError (0xbebecaca);
3373 pSetSecurityDescriptorControl (sec, bitsOfInterest, setOrClear | (1 << bit));
3374 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
3375 GetSecurityDescriptorControl(sec, &test, &dwRevision);
3376 expect_eq(test, ctrl, int, "%x");
3378 ctrl = ((1 << bit) & immutable) ? test : ref | (1 << bit);
3379 setOrClear ^= bitsOfInterest;
3380 SetLastError (0xbebecaca);
3381 pSetSecurityDescriptorControl (sec, bitsOfInterest, setOrClear | (1 << bit));
3382 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
3383 GetSecurityDescriptorControl(sec, &test, &dwRevision);
3384 expect_eq(test, ctrl, int, "%x");
3388 static void test_PrivateObjectSecurity(void)
3390 SECURITY_INFORMATION sec_info = OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION|SACL_SECURITY_INFORMATION;
3391 SECURITY_DESCRIPTOR_CONTROL ctrl;
3392 PSECURITY_DESCRIPTOR sec;
3393 DWORD dwDescSize;
3394 DWORD dwRevision;
3395 DWORD retSize;
3396 LPSTR string;
3397 ULONG len;
3398 PSECURITY_DESCRIPTOR buf;
3399 BOOL ret;
3401 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA)
3403 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
3404 return;
3407 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3408 "O:SY"
3409 "G:S-1-5-21-93476-23408-4576"
3410 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)"
3411 "(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3412 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
3413 SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
3415 test_SetSecurityDescriptorControl(sec);
3417 LocalFree(sec);
3419 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3420 "O:SY"
3421 "G:S-1-5-21-93476-23408-4576",
3422 SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
3424 test_SetSecurityDescriptorControl(sec);
3426 LocalFree(sec);
3428 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3429 "O:SY"
3430 "G:S-1-5-21-93476-23408-4576"
3431 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3432 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
3433 buf = HeapAlloc(GetProcessHeap(), 0, dwDescSize);
3434 pSetSecurityDescriptorControl(sec, SE_DACL_PROTECTED, SE_DACL_PROTECTED);
3435 GetSecurityDescriptorControl(sec, &ctrl, &dwRevision);
3436 expect_eq(ctrl, 0x9014, int, "%x");
3438 ret = GetPrivateObjectSecurity(sec, GROUP_SECURITY_INFORMATION, buf, dwDescSize, &retSize);
3439 ok(ret, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3440 ok(retSize <= dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
3441 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3442 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
3443 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
3444 expect_eq(ctrl, 0x8000, int, "%x");
3446 ret = GetPrivateObjectSecurity(sec, GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION, buf, dwDescSize, &retSize);
3447 ok(ret, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3448 ok(retSize <= dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
3449 ret = pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len);
3450 ok(ret, "Conversion failed err=%u\n", GetLastError());
3451 CHECK_ONE_OF_AND_FREE("G:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)",
3452 "G:S-1-5-21-93476-23408-4576D:P(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"); /* Win7 */
3453 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
3454 expect_eq(ctrl & (~ SE_DACL_PROTECTED), 0x8004, int, "%x");
3456 ret = GetPrivateObjectSecurity(sec, sec_info, buf, dwDescSize, &retSize);
3457 ok(ret, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3458 ok(retSize == dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
3459 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3460 CHECK_ONE_OF_AND_FREE("O:SY"
3461 "G:S-1-5-21-93476-23408-4576"
3462 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3463 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
3464 "O:SY"
3465 "G:S-1-5-21-93476-23408-4576"
3466 "D:P(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3467 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)"); /* Win7 */
3468 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
3469 expect_eq(ctrl & (~ SE_DACL_PROTECTED), 0x8014, int, "%x");
3471 SetLastError(0xdeadbeef);
3472 ok(GetPrivateObjectSecurity(sec, sec_info, buf, 5, &retSize) == FALSE, "GetPrivateObjectSecurity should have failed\n");
3473 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected error ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
3475 LocalFree(sec);
3476 HeapFree(GetProcessHeap(), 0, buf);
3478 #undef CHECK_RESULT_AND_FREE
3479 #undef CHECK_ONE_OF_AND_FREE
3481 static void test_acls(void)
3483 char buffer[256];
3484 PACL pAcl = (PACL)buffer;
3485 BOOL ret;
3487 SetLastError(0xdeadbeef);
3488 ret = InitializeAcl(pAcl, sizeof(ACL) - 1, ACL_REVISION);
3489 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
3491 win_skip("InitializeAcl is not implemented\n");
3492 return;
3495 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InitializeAcl with too small a buffer should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
3497 SetLastError(0xdeadbeef);
3498 ret = InitializeAcl(pAcl, 0xffffffff, ACL_REVISION);
3499 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "InitializeAcl with too large a buffer should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3501 SetLastError(0xdeadbeef);
3502 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION1);
3503 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "InitializeAcl(ACL_REVISION1) should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3505 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION2);
3506 ok(ret, "InitializeAcl(ACL_REVISION2) failed with error %d\n", GetLastError());
3508 ret = IsValidAcl(pAcl);
3509 ok(ret, "IsValidAcl failed with error %d\n", GetLastError());
3511 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION3);
3512 ok(ret, "InitializeAcl(ACL_REVISION3) failed with error %d\n", GetLastError());
3514 ret = IsValidAcl(pAcl);
3515 ok(ret, "IsValidAcl failed with error %d\n", GetLastError());
3517 SetLastError(0xdeadbeef);
3518 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION4);
3519 if (GetLastError() != ERROR_INVALID_PARAMETER)
3521 ok(ret, "InitializeAcl(ACL_REVISION4) failed with error %d\n", GetLastError());
3523 ret = IsValidAcl(pAcl);
3524 ok(ret, "IsValidAcl failed with error %d\n", GetLastError());
3526 else
3527 win_skip("ACL_REVISION4 is not implemented on NT4\n");
3529 SetLastError(0xdeadbeef);
3530 ret = InitializeAcl(pAcl, sizeof(buffer), -1);
3531 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "InitializeAcl(-1) failed with error %d\n", GetLastError());
3534 static void test_GetSecurityInfo(void)
3536 HANDLE obj;
3537 PSECURITY_DESCRIPTOR sd;
3538 PSID owner, group;
3539 PACL dacl;
3540 DWORD ret;
3542 if (!pGetSecurityInfo)
3544 win_skip("GetSecurityInfo is not available\n");
3545 return;
3548 /* Create something. Files have lots of associated security info. */
3549 obj = CreateFile(myARGV[0], GENERIC_READ, FILE_SHARE_READ, NULL,
3550 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
3551 if (obj == INVALID_HANDLE_VALUE)
3553 skip("Couldn't create an object for GetSecurityInfo test\n");
3554 return;
3557 ret = pGetSecurityInfo(obj, SE_FILE_OBJECT,
3558 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
3559 &owner, &group, &dacl, NULL, &sd);
3560 if (ret == ERROR_CALL_NOT_IMPLEMENTED)
3562 win_skip("GetSecurityInfo is not implemented\n");
3563 CloseHandle(obj);
3564 return;
3566 ok(ret == ERROR_SUCCESS, "GetSecurityInfo returned %d\n", ret);
3567 ok(sd != NULL, "GetSecurityInfo\n");
3568 ok(owner != NULL, "GetSecurityInfo\n");
3569 ok(group != NULL, "GetSecurityInfo\n");
3570 if (dacl != NULL)
3571 ok(IsValidAcl(dacl), "GetSecurityInfo\n");
3572 else
3573 win_skip("No ACL information returned\n");
3575 LocalFree(sd);
3577 if (!pCreateWellKnownSid)
3579 win_skip("NULL parameter test would crash on NT4\n");
3580 CloseHandle(obj);
3581 return;
3584 /* If we don't ask for the security descriptor, Windows will still give us
3585 the other stuff, leaving us no way to free it. */
3586 ret = pGetSecurityInfo(obj, SE_FILE_OBJECT,
3587 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
3588 &owner, &group, &dacl, NULL, NULL);
3589 ok(ret == ERROR_SUCCESS, "GetSecurityInfo returned %d\n", ret);
3590 ok(owner != NULL, "GetSecurityInfo\n");
3591 ok(group != NULL, "GetSecurityInfo\n");
3592 if (dacl != NULL)
3593 ok(IsValidAcl(dacl), "GetSecurityInfo\n");
3594 else
3595 win_skip("No ACL information returned\n");
3597 CloseHandle(obj);
3600 static void test_GetSidSubAuthority(void)
3602 PSID psid = NULL;
3604 if (!pGetSidSubAuthority || !pConvertStringSidToSidA || !pIsValidSid || !pGetSidSubAuthorityCount)
3606 win_skip("Some functions not available\n");
3607 return;
3609 /* Note: on windows passing in an invalid index like -1, lets GetSidSubAuthority return 0x05000000 but
3610 still GetLastError returns ERROR_SUCCESS then. We don't test these unlikely cornercases here for now */
3611 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576",&psid),"ConvertStringSidToSidA failed\n");
3612 ok(pIsValidSid(psid),"Sid is not valid\n");
3613 SetLastError(0xbebecaca);
3614 ok(*pGetSidSubAuthorityCount(psid) == 4,"GetSidSubAuthorityCount gave %d expected 4\n",*pGetSidSubAuthorityCount(psid));
3615 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3616 SetLastError(0xbebecaca);
3617 ok(*pGetSidSubAuthority(psid,0) == 21,"GetSidSubAuthority gave %d expected 21\n",*pGetSidSubAuthority(psid,0));
3618 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3619 SetLastError(0xbebecaca);
3620 ok(*pGetSidSubAuthority(psid,1) == 93476,"GetSidSubAuthority gave %d expected 93476\n",*pGetSidSubAuthority(psid,1));
3621 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3622 SetLastError(0xbebecaca);
3623 ok(pGetSidSubAuthority(psid,4) != NULL,"Expected out of bounds GetSidSubAuthority to return a non-NULL pointer\n");
3624 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3625 LocalFree(psid);
3628 static void test_CheckTokenMembership(void)
3630 PTOKEN_GROUPS token_groups;
3631 DWORD size;
3632 HANDLE process_token, token;
3633 BOOL is_member;
3634 BOOL ret;
3635 DWORD i;
3637 if (!pCheckTokenMembership)
3639 win_skip("CheckTokenMembership is not available\n");
3640 return;
3642 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &process_token);
3643 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
3645 ret = DuplicateToken(process_token, SecurityImpersonation, &token);
3646 ok(ret, "DuplicateToken failed with error %d\n", GetLastError());
3648 /* groups */
3649 ret = GetTokenInformation(token, TokenGroups, NULL, 0, &size);
3650 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
3651 "GetTokenInformation(TokenGroups) %s with error %d\n",
3652 ret ? "succeeded" : "failed", GetLastError());
3653 token_groups = HeapAlloc(GetProcessHeap(), 0, size);
3654 ret = GetTokenInformation(token, TokenGroups, token_groups, size, &size);
3655 ok(ret, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
3657 for (i = 0; i < token_groups->GroupCount; i++)
3659 if (token_groups->Groups[i].Attributes & SE_GROUP_ENABLED)
3660 break;
3663 if (i == token_groups->GroupCount)
3665 HeapFree(GetProcessHeap(), 0, token_groups);
3666 CloseHandle(token);
3667 skip("user not a member of any group\n");
3668 return;
3671 is_member = FALSE;
3672 ret = pCheckTokenMembership(token, token_groups->Groups[i].Sid, &is_member);
3673 ok(ret, "CheckTokenMembership failed with error %d\n", GetLastError());
3674 ok(is_member, "CheckTokenMembership should have detected sid as member\n");
3676 is_member = FALSE;
3677 ret = pCheckTokenMembership(NULL, token_groups->Groups[i].Sid, &is_member);
3678 ok(ret, "CheckTokenMembership failed with error %d\n", GetLastError());
3679 ok(is_member, "CheckTokenMembership should have detected sid as member\n");
3681 is_member = TRUE;
3682 SetLastError(0xdeadbeef);
3683 ret = pCheckTokenMembership(process_token, token_groups->Groups[i].Sid, &is_member);
3684 ok(!ret && GetLastError() == ERROR_NO_IMPERSONATION_TOKEN,
3685 "CheckTokenMembership with process token %s with error %d\n",
3686 ret ? "succeeded" : "failed", GetLastError());
3687 ok(!is_member, "CheckTokenMembership should have cleared is_member\n");
3689 HeapFree(GetProcessHeap(), 0, token_groups);
3690 CloseHandle(token);
3691 CloseHandle(process_token);
3694 static void test_EqualSid(void)
3696 PSID sid1, sid2;
3697 BOOL ret;
3698 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
3699 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
3701 SetLastError(0xdeadbeef);
3702 ret = AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
3703 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &sid1);
3704 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
3706 win_skip("AllocateAndInitializeSid is not implemented\n");
3707 return;
3709 ok(ret, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3710 ok(GetLastError() == 0xdeadbeef,
3711 "AllocateAndInitializeSid shouldn't have set last error to %d\n",
3712 GetLastError());
3714 ret = AllocateAndInitializeSid(&SIDAuthWorld, 1, SECURITY_WORLD_RID,
3715 0, 0, 0, 0, 0, 0, 0, &sid2);
3716 ok(ret, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3718 SetLastError(0xdeadbeef);
3719 ret = EqualSid(sid1, sid2);
3720 ok(!ret, "World and domain admins sids shouldn't have been equal\n");
3721 ok(GetLastError() == ERROR_SUCCESS ||
3722 broken(GetLastError() == 0xdeadbeef), /* NT4 */
3723 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
3724 GetLastError());
3726 SetLastError(0xdeadbeef);
3727 sid2 = FreeSid(sid2);
3728 ok(!sid2, "FreeSid should have returned NULL instead of %p\n", sid2);
3729 ok(GetLastError() == 0xdeadbeef,
3730 "FreeSid shouldn't have set last error to %d\n",
3731 GetLastError());
3733 ret = AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
3734 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &sid2);
3735 ok(ret, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3737 SetLastError(0xdeadbeef);
3738 ret = EqualSid(sid1, sid2);
3739 ok(ret, "Same sids should have been equal\n");
3740 ok(GetLastError() == ERROR_SUCCESS ||
3741 broken(GetLastError() == 0xdeadbeef), /* NT4 */
3742 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
3743 GetLastError());
3745 ((SID *)sid2)->Revision = 2;
3746 SetLastError(0xdeadbeef);
3747 ret = EqualSid(sid1, sid2);
3748 ok(!ret, "EqualSid with invalid sid should have returned FALSE\n");
3749 ok(GetLastError() == ERROR_SUCCESS ||
3750 broken(GetLastError() == 0xdeadbeef), /* NT4 */
3751 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
3752 GetLastError());
3753 ((SID *)sid2)->Revision = SID_REVISION;
3755 FreeSid(sid1);
3756 FreeSid(sid2);
3759 static void test_GetUserNameA(void)
3761 char buffer[UNLEN + 1], filler[UNLEN + 1];
3762 DWORD required_len, buffer_len;
3763 BOOL ret;
3765 /* Test crashes on Windows. */
3766 if (0)
3768 SetLastError(0xdeadbeef);
3769 GetUserNameA(NULL, NULL);
3772 SetLastError(0xdeadbeef);
3773 required_len = 0;
3774 ret = GetUserNameA(NULL, &required_len);
3775 ok(ret == FALSE, "GetUserNameA returned %d\n", ret);
3776 ok(required_len != 0, "Outputted buffer length was %u\n", required_len);
3777 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Last error was %u\n", GetLastError());
3779 SetLastError(0xdeadbeef);
3780 required_len = 1;
3781 ret = GetUserNameA(NULL, &required_len);
3782 ok(ret == FALSE, "GetUserNameA returned %d\n", ret);
3783 ok(required_len != 0 && required_len != 1, "Outputted buffer length was %u\n", required_len);
3784 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Last error was %u\n", GetLastError());
3786 /* Tests crashes on Windows. */
3787 if (0)
3789 SetLastError(0xdeadbeef);
3790 required_len = UNLEN + 1;
3791 GetUserNameA(NULL, &required_len);
3793 SetLastError(0xdeadbeef);
3794 GetUserNameA(buffer, NULL);
3797 memset(filler, 'x', sizeof(filler));
3799 /* Note that GetUserNameA on XP and newer outputs the number of bytes
3800 * required for a Unicode string, which affects a test in the next block. */
3801 SetLastError(0xdeadbeef);
3802 memcpy(buffer, filler, sizeof(filler));
3803 required_len = 0;
3804 ret = GetUserNameA(buffer, &required_len);
3805 ok(ret == FALSE, "GetUserNameA returned %d\n", ret);
3806 ok(!memcmp(buffer, filler, sizeof(filler)), "Output buffer was altered\n");
3807 ok(required_len != 0, "Outputted buffer length was %u\n", required_len);
3808 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Last error was %u\n", GetLastError());
3810 SetLastError(0xdeadbeef);
3811 memcpy(buffer, filler, sizeof(filler));
3812 buffer_len = required_len;
3813 ret = GetUserNameA(buffer, &buffer_len);
3814 ok(ret == TRUE, "GetUserNameA returned %d, last error %u\n", ret, GetLastError());
3815 ok(memcmp(buffer, filler, sizeof(filler)) != 0, "Output buffer was untouched\n");
3816 ok(buffer_len == required_len ||
3817 broken(buffer_len == required_len / sizeof(WCHAR)), /* XP+ */
3818 "Outputted buffer length was %u\n", buffer_len);
3820 /* Use the reported buffer size from the last GetUserNameA call and pass
3821 * a length that is one less than the required value. */
3822 SetLastError(0xdeadbeef);
3823 memcpy(buffer, filler, sizeof(filler));
3824 buffer_len--;
3825 ret = GetUserNameA(buffer, &buffer_len);
3826 ok(ret == FALSE, "GetUserNameA returned %d\n", ret);
3827 ok(!memcmp(buffer, filler, sizeof(filler)), "Output buffer was untouched\n");
3828 ok(buffer_len == required_len, "Outputted buffer length was %u\n", buffer_len);
3829 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Last error was %u\n", GetLastError());
3832 static void test_GetUserNameW(void)
3834 WCHAR buffer[UNLEN + 1], filler[UNLEN + 1];
3835 DWORD required_len, buffer_len;
3836 BOOL ret;
3838 /* Test crashes on Windows. */
3839 if (0)
3841 SetLastError(0xdeadbeef);
3842 GetUserNameW(NULL, NULL);
3845 SetLastError(0xdeadbeef);
3846 required_len = 0;
3847 ret = GetUserNameW(NULL, &required_len);
3848 ok(ret == FALSE, "GetUserNameW returned %d\n", ret);
3849 ok(required_len != 0, "Outputted buffer length was %u\n", required_len);
3850 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Last error was %u\n", GetLastError());
3852 SetLastError(0xdeadbeef);
3853 required_len = 1;
3854 ret = GetUserNameW(NULL, &required_len);
3855 ok(ret == FALSE, "GetUserNameW returned %d\n", ret);
3856 ok(required_len != 0 && required_len != 1, "Outputted buffer length was %u\n", required_len);
3857 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Last error was %u\n", GetLastError());
3859 /* Tests crash on Windows. */
3860 if (0)
3862 SetLastError(0xdeadbeef);
3863 required_len = UNLEN + 1;
3864 GetUserNameW(NULL, &required_len);
3866 SetLastError(0xdeadbeef);
3867 GetUserNameW(buffer, NULL);
3870 memset(filler, 'x', sizeof(filler));
3872 SetLastError(0xdeadbeef);
3873 memcpy(buffer, filler, sizeof(filler));
3874 required_len = 0;
3875 ret = GetUserNameW(buffer, &required_len);
3876 ok(ret == FALSE, "GetUserNameW returned %d\n", ret);
3877 ok(!memcmp(buffer, filler, sizeof(filler)), "Output buffer was altered\n");
3878 ok(required_len != 0, "Outputted buffer length was %u\n", required_len);
3879 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Last error was %u\n", GetLastError());
3881 SetLastError(0xdeadbeef);
3882 memcpy(buffer, filler, sizeof(filler));
3883 buffer_len = required_len;
3884 ret = GetUserNameW(buffer, &buffer_len);
3885 ok(ret == TRUE, "GetUserNameW returned %d, last error %u\n", ret, GetLastError());
3886 ok(memcmp(buffer, filler, sizeof(filler)) != 0, "Output buffer was untouched\n");
3887 ok(buffer_len == required_len, "Outputted buffer length was %u\n", buffer_len);
3889 /* GetUserNameW on XP and newer writes a truncated portion of the username string to the buffer. */
3890 SetLastError(0xdeadbeef);
3891 memcpy(buffer, filler, sizeof(filler));
3892 buffer_len--;
3893 ret = GetUserNameW(buffer, &buffer_len);
3894 ok(ret == FALSE, "GetUserNameW returned %d\n", ret);
3895 ok(!memcmp(buffer, filler, sizeof(filler)) ||
3896 broken(memcmp(buffer, filler, sizeof(filler)) != 0), /* XP+ */
3897 "Output buffer was altered\n");
3898 ok(buffer_len == required_len, "Outputted buffer length was %u\n", buffer_len);
3899 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Last error was %u\n", GetLastError());
3902 static void test_CreateRestrictedToken(void)
3904 HANDLE process_token, token, r_token;
3905 PTOKEN_GROUPS token_groups, groups2;
3906 SID_AND_ATTRIBUTES sattr;
3907 BOOL is_member;
3908 DWORD size;
3909 BOOL ret;
3910 DWORD i, j;
3912 if (!pCreateRestrictedToken)
3914 win_skip("CreateRestrictedToken is not available\n");
3915 return;
3918 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &process_token);
3919 ok(ret, "got error %d\n", GetLastError());
3921 ret = DuplicateTokenEx(process_token, TOKEN_DUPLICATE|TOKEN_ADJUST_GROUPS|TOKEN_QUERY,
3922 NULL, SecurityImpersonation, TokenImpersonation, &token);
3923 ok(ret, "got error %d\n", GetLastError());
3925 /* groups */
3926 ret = GetTokenInformation(token, TokenGroups, NULL, 0, &size);
3927 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
3928 "got %d with error %d\n", ret, GetLastError());
3929 token_groups = HeapAlloc(GetProcessHeap(), 0, size);
3930 ret = GetTokenInformation(token, TokenGroups, token_groups, size, &size);
3931 ok(ret, "got error %d\n", GetLastError());
3933 for (i = 0; i < token_groups->GroupCount; i++)
3935 if (token_groups->Groups[i].Attributes & SE_GROUP_ENABLED)
3936 break;
3939 if (i == token_groups->GroupCount)
3941 HeapFree(GetProcessHeap(), 0, token_groups);
3942 CloseHandle(token);
3943 skip("User not a member of any group\n");
3944 return;
3947 is_member = FALSE;
3948 ret = pCheckTokenMembership(token, token_groups->Groups[i].Sid, &is_member);
3949 ok(ret, "got error %d\n", GetLastError());
3950 ok(is_member, "not a member\n");
3952 /* disable a SID in new token */
3953 sattr.Sid = token_groups->Groups[i].Sid;
3954 sattr.Attributes = 0;
3955 r_token = NULL;
3956 ret = pCreateRestrictedToken(token, 0, 1, &sattr, 0, NULL, 0, NULL, &r_token);
3957 todo_wine ok(ret, "got error %d\n", GetLastError());
3959 if (ret)
3961 /* check if a SID is enabled */
3962 is_member = TRUE;
3963 ret = pCheckTokenMembership(r_token, token_groups->Groups[i].Sid, &is_member);
3964 ok(ret, "got error %d\n", GetLastError());
3965 ok(!is_member, "not a member\n");
3967 ret = GetTokenInformation(r_token, TokenGroups, NULL, 0, &size);
3968 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "got %d with error %d\n",
3969 ret, GetLastError());
3970 groups2 = HeapAlloc(GetProcessHeap(), 0, size);
3971 ret = GetTokenInformation(r_token, TokenGroups, groups2, size, &size);
3972 ok(ret, "got error %d\n", GetLastError());
3974 for (j = 0; j < groups2->GroupCount; j++)
3976 if (EqualSid(groups2->Groups[j].Sid, token_groups->Groups[i].Sid))
3977 break;
3980 ok(groups2->Groups[j].Attributes & SE_GROUP_USE_FOR_DENY_ONLY,
3981 "got wrong attributes\n");
3982 ok((groups2->Groups[j].Attributes & SE_GROUP_ENABLED) == 0,
3983 "got wrong attributes\n");
3985 HeapFree(GetProcessHeap(), 0, groups2);
3988 HeapFree(GetProcessHeap(), 0, token_groups);
3989 CloseHandle(r_token);
3990 CloseHandle(token);
3991 CloseHandle(process_token);
3994 static void validate_default_security_descriptor(SECURITY_DESCRIPTOR *sd)
3996 BOOL ret, present, defaulted;
3997 ACL *acl;
3998 void *sid;
4000 ret = IsValidSecurityDescriptor(sd);
4001 ok(ret, "security descriptor is not valid\n");
4003 present = -1;
4004 defaulted = -1;
4005 acl = (void *)0xdeadbeef;
4006 SetLastError(0xdeadbeef);
4007 ret = GetSecurityDescriptorDacl(sd, &present, &acl, &defaulted);
4008 ok(ret, "GetSecurityDescriptorDacl error %d\n", GetLastError());
4009 todo_wine
4010 ok(present == 1, "acl is not present\n");
4011 todo_wine
4012 ok(acl != (void *)0xdeadbeef && acl != NULL, "acl pointer is not set\n");
4013 ok(defaulted == 0, "defaulted is set to TRUE\n");
4015 defaulted = -1;
4016 sid = (void *)0xdeadbeef;
4017 SetLastError(0xdeadbeef);
4018 ret = GetSecurityDescriptorOwner(sd, &sid, &defaulted);
4019 ok(ret, "GetSecurityDescriptorOwner error %d\n", GetLastError());
4020 todo_wine
4021 ok(sid != (void *)0xdeadbeef && sid != NULL, "sid pointer is not set\n");
4022 ok(defaulted == 0, "defaulted is set to TRUE\n");
4024 defaulted = -1;
4025 sid = (void *)0xdeadbeef;
4026 SetLastError(0xdeadbeef);
4027 ret = GetSecurityDescriptorGroup(sd, &sid, &defaulted);
4028 ok(ret, "GetSecurityDescriptorGroup error %d\n", GetLastError());
4029 todo_wine
4030 ok(sid != (void *)0xdeadbeef && sid != NULL, "sid pointer is not set\n");
4031 ok(defaulted == 0, "defaulted is set to TRUE\n");
4034 static void test_default_handle_security(HANDLE token, HANDLE handle, GENERIC_MAPPING *mapping)
4036 DWORD ret, length, needed, granted, priv_set_len;
4037 BOOL status;
4038 PRIVILEGE_SET priv_set;
4039 SECURITY_DESCRIPTOR *sd;
4041 needed = 0xdeadbeef;
4042 SetLastError(0xdeadbeef);
4043 ret = GetKernelObjectSecurity(handle, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
4044 NULL, 0, &needed);
4045 ok(!ret, "GetKernelObjectSecurity should fail\n");
4046 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
4047 ok(needed != 0xdeadbeef, "GetKernelObjectSecurity should return required buffer length\n");
4049 length = needed;
4050 sd = HeapAlloc(GetProcessHeap(), 0, length);
4052 needed = 0xdeadbeef;
4053 SetLastError(0xdeadbeef);
4054 ret = GetKernelObjectSecurity(handle, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
4055 sd, length, &needed);
4056 ok(ret, "GetKernelObjectSecurity error %d\n", GetLastError());
4057 ok(needed == length || needed == 0 /* file, pipe */, "GetKernelObjectSecurity should return %u instead of %u\n", length, needed);
4059 validate_default_security_descriptor(sd);
4061 priv_set_len = sizeof(priv_set);
4062 granted = 0xdeadbeef;
4063 status = 0xdeadbeef;
4064 SetLastError(0xdeadbeef);
4065 ret = AccessCheck(sd, token, MAXIMUM_ALLOWED, mapping, &priv_set, &priv_set_len, &granted, &status);
4066 todo_wine {
4067 ok(ret, "AccessCheck error %d\n", GetLastError());
4068 ok(status == 1, "expected 1, got %d\n", status);
4069 ok(granted == mapping->GenericAll, "expected all access %#x, got %#x\n", mapping->GenericAll, granted);
4071 priv_set_len = sizeof(priv_set);
4072 granted = 0xdeadbeef;
4073 status = 0xdeadbeef;
4074 SetLastError(0xdeadbeef);
4075 ret = AccessCheck(sd, token, 0, mapping, &priv_set, &priv_set_len, &granted, &status);
4076 todo_wine {
4077 ok(ret, "AccessCheck error %d\n", GetLastError());
4078 ok(status == 0 || broken(status == 1) /* NT4 */, "expected 0, got %d\n", status);
4079 ok(granted == 0 || broken(granted == mapping->GenericRead) /* NT4 */, "expected 0, got %#x\n", granted);
4081 priv_set_len = sizeof(priv_set);
4082 granted = 0xdeadbeef;
4083 status = 0xdeadbeef;
4084 SetLastError(0xdeadbeef);
4085 ret = AccessCheck(sd, token, ACCESS_SYSTEM_SECURITY, mapping, &priv_set, &priv_set_len, &granted, &status);
4086 todo_wine {
4087 ok(ret, "AccessCheck error %d\n", GetLastError());
4088 ok(status == 0, "expected 0, got %d\n", status);
4089 ok(granted == 0, "expected 0, got %#x\n", granted);
4091 priv_set_len = sizeof(priv_set);
4092 granted = 0xdeadbeef;
4093 status = 0xdeadbeef;
4094 SetLastError(0xdeadbeef);
4095 ret = AccessCheck(sd, token, mapping->GenericRead, mapping, &priv_set, &priv_set_len, &granted, &status);
4096 todo_wine {
4097 ok(ret, "AccessCheck error %d\n", GetLastError());
4098 ok(status == 1, "expected 1, got %d\n", status);
4099 ok(granted == mapping->GenericRead, "expected read access %#x, got %#x\n", mapping->GenericRead, granted);
4101 priv_set_len = sizeof(priv_set);
4102 granted = 0xdeadbeef;
4103 status = 0xdeadbeef;
4104 SetLastError(0xdeadbeef);
4105 ret = AccessCheck(sd, token, mapping->GenericWrite, mapping, &priv_set, &priv_set_len, &granted, &status);
4106 todo_wine {
4107 ok(ret, "AccessCheck error %d\n", GetLastError());
4108 ok(status == 1, "expected 1, got %d\n", status);
4109 ok(granted == mapping->GenericWrite, "expected write access %#x, got %#x\n", mapping->GenericWrite, granted);
4111 priv_set_len = sizeof(priv_set);
4112 granted = 0xdeadbeef;
4113 status = 0xdeadbeef;
4114 SetLastError(0xdeadbeef);
4115 ret = AccessCheck(sd, token, mapping->GenericExecute, mapping, &priv_set, &priv_set_len, &granted, &status);
4116 todo_wine {
4117 ok(ret, "AccessCheck error %d\n", GetLastError());
4118 ok(status == 1, "expected 1, got %d\n", status);
4119 ok(granted == mapping->GenericExecute, "expected execute access %#x, got %#x\n", mapping->GenericExecute, granted);
4121 HeapFree(GetProcessHeap(), 0, sd);
4124 static void test_mutex_security(HANDLE token)
4126 HANDLE mutex;
4127 GENERIC_MAPPING mapping = { STANDARD_RIGHTS_READ | MUTANT_QUERY_STATE | SYNCHRONIZE,
4128 STANDARD_RIGHTS_WRITE | MUTEX_MODIFY_STATE | SYNCHRONIZE,
4129 STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE,
4130 STANDARD_RIGHTS_ALL | MUTEX_ALL_ACCESS };
4132 SetLastError(0xdeadbeef);
4133 mutex = OpenMutex(0, FALSE, "WineTestMutex");
4134 ok(!mutex, "mutex should not exist\n");
4135 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "wrong error %u\n", GetLastError());
4137 SetLastError(0xdeadbeef);
4138 mutex = CreateMutex(NULL, FALSE, "WineTestMutex");
4139 ok(mutex != 0, "CreateMutex error %d\n", GetLastError());
4141 test_default_handle_security(token, mutex, &mapping);
4143 CloseHandle (mutex);
4146 static void test_event_security(HANDLE token)
4148 HANDLE event;
4149 GENERIC_MAPPING mapping = { STANDARD_RIGHTS_READ | EVENT_QUERY_STATE | SYNCHRONIZE,
4150 STANDARD_RIGHTS_WRITE | EVENT_MODIFY_STATE | SYNCHRONIZE,
4151 STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE,
4152 STANDARD_RIGHTS_ALL | EVENT_ALL_ACCESS };
4154 SetLastError(0xdeadbeef);
4155 event = OpenEvent(0, FALSE, "WineTestEvent");
4156 ok(!event, "event should not exist\n");
4157 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "wrong error %u\n", GetLastError());
4159 SetLastError(0xdeadbeef);
4160 event = CreateEvent(NULL, FALSE, FALSE, "WineTestEvent");
4161 ok(event != 0, "CreateEvent error %d\n", GetLastError());
4163 test_default_handle_security(token, event, &mapping);
4165 CloseHandle(event);
4168 #define WINE_TEST_PIPE "\\\\.\\pipe\\WineTestPipe"
4169 static void test_named_pipe_security(HANDLE token)
4171 HANDLE pipe, file;
4172 GENERIC_MAPPING mapping = { FILE_GENERIC_READ,
4173 FILE_GENERIC_WRITE,
4174 FILE_GENERIC_EXECUTE,
4175 STANDARD_RIGHTS_ALL | FILE_ALL_ACCESS };
4177 SetLastError(0xdeadbeef);
4178 pipe = CreateNamedPipe(WINE_TEST_PIPE, PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE,
4179 PIPE_TYPE_BYTE | PIPE_NOWAIT, PIPE_UNLIMITED_INSTANCES,
4180 0, 0, NMPWAIT_USE_DEFAULT_WAIT, NULL);
4181 ok(pipe != INVALID_HANDLE_VALUE, "CreateNamedPipe error %d\n", GetLastError());
4183 test_default_handle_security(token, pipe, &mapping);
4185 SetLastError(0xdeadbeef);
4186 file = CreateFile(WINE_TEST_PIPE, FILE_ALL_ACCESS, 0, NULL, OPEN_EXISTING, 0, 0);
4187 ok(file != INVALID_HANDLE_VALUE, "CreateFile error %d\n", GetLastError());
4188 CloseHandle(file);
4190 CloseHandle(pipe);
4192 SetLastError(0xdeadbeef);
4193 file = CreateFile("\\\\.\\pipe\\", FILE_ALL_ACCESS, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
4194 ok(file != INVALID_HANDLE_VALUE || broken(file == INVALID_HANDLE_VALUE) /* before Vista */, "CreateFile error %d\n", GetLastError());
4195 CloseHandle(file);
4198 static BOOL validate_impersonation_token(HANDLE token, DWORD *token_type)
4200 DWORD ret, needed;
4201 TOKEN_TYPE type;
4202 SECURITY_IMPERSONATION_LEVEL sil;
4204 type = 0xdeadbeef;
4205 needed = 0;
4206 SetLastError(0xdeadbeef);
4207 ret = GetTokenInformation(token, TokenType, &type, sizeof(type), &needed);
4208 ok(ret, "GetTokenInformation error %d\n", GetLastError());
4209 ok(needed == sizeof(type), "GetTokenInformation should return required buffer length\n");
4210 ok(type == TokenPrimary || type == TokenImpersonation, "expected TokenPrimary or TokenImpersonation, got %d\n", type);
4212 *token_type = type;
4213 if (type != TokenImpersonation) return FALSE;
4215 needed = 0;
4216 SetLastError(0xdeadbeef);
4217 ret = GetTokenInformation(token, TokenImpersonationLevel, &sil, sizeof(sil), &needed);
4218 ok(ret, "GetTokenInformation error %d\n", GetLastError());
4219 ok(needed == sizeof(sil), "GetTokenInformation should return required buffer length\n");
4220 ok(sil == SecurityImpersonation, "expected SecurityImpersonation, got %d\n", sil);
4222 needed = 0xdeadbeef;
4223 SetLastError(0xdeadbeef);
4224 ret = GetTokenInformation(token, TokenDefaultDacl, NULL, 0, &needed);
4225 ok(!ret, "GetTokenInformation should fail\n");
4226 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
4227 ok(needed != 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
4228 ok(needed > sizeof(TOKEN_DEFAULT_DACL), "GetTokenInformation returned empty default DACL\n");
4230 needed = 0xdeadbeef;
4231 SetLastError(0xdeadbeef);
4232 ret = GetTokenInformation(token, TokenOwner, NULL, 0, &needed);
4233 ok(!ret, "GetTokenInformation should fail\n");
4234 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
4235 ok(needed != 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
4236 ok(needed > sizeof(TOKEN_OWNER), "GetTokenInformation returned empty token owner\n");
4238 needed = 0xdeadbeef;
4239 SetLastError(0xdeadbeef);
4240 ret = GetTokenInformation(token, TokenPrimaryGroup, NULL, 0, &needed);
4241 ok(!ret, "GetTokenInformation should fail\n");
4242 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
4243 ok(needed != 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
4244 ok(needed > sizeof(TOKEN_PRIMARY_GROUP), "GetTokenInformation returned empty token primary group\n");
4246 return TRUE;
4249 static void test_kernel_objects_security(void)
4251 HANDLE token, process_token;
4252 DWORD ret, token_type;
4254 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE | TOKEN_QUERY, &process_token);
4255 ok(ret, "OpenProcessToken error %d\n", GetLastError());
4257 ret = validate_impersonation_token(process_token, &token_type);
4258 ok(token_type == TokenPrimary, "expected TokenPrimary, got %d\n", token_type);
4259 ok(!ret, "access token should not be an impersonation token\n");
4261 ret = DuplicateToken(process_token, SecurityImpersonation, &token);
4262 ok(ret, "DuplicateToken error %d\n", GetLastError());
4264 ret = validate_impersonation_token(token, &token_type);
4265 ok(ret, "access token should be a valid impersonation token\n");
4266 ok(token_type == TokenImpersonation, "expected TokenImpersonation, got %d\n", token_type);
4268 test_mutex_security(token);
4269 test_event_security(token);
4270 test_named_pipe_security(token);
4271 /* FIXME: test other kernel object types */
4273 CloseHandle(process_token);
4274 CloseHandle(token);
4277 START_TEST(security)
4279 init();
4280 if (!hmod) return;
4282 if (myARGC >= 3)
4284 test_process_security_child();
4285 return;
4287 test_kernel_objects_security();
4288 test_sid();
4289 test_trustee();
4290 test_luid();
4291 test_CreateWellKnownSid();
4292 test_FileSecurity();
4293 test_AccessCheck();
4294 test_token_attr();
4295 test_LookupAccountSid();
4296 test_LookupAccountName();
4297 test_security_descriptor();
4298 test_process_security();
4299 test_impersonation_level();
4300 test_SetEntriesInAclW();
4301 test_SetEntriesInAclA();
4302 test_GetNamedSecurityInfoA();
4303 test_ConvertStringSecurityDescriptor();
4304 test_ConvertSecurityDescriptorToString();
4305 test_PrivateObjectSecurity();
4306 test_acls();
4307 test_GetSecurityInfo();
4308 test_GetSidSubAuthority();
4309 test_CheckTokenMembership();
4310 test_EqualSid();
4311 test_GetUserNameA();
4312 test_GetUserNameW();
4313 test_CreateRestrictedToken();