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
26 #define WIN32_NO_STATUS
36 #include "wine/test.h"
38 /* copied from Wine winternl.h - not included in the Windows SDK */
39 typedef enum _OBJECT_INFORMATION_CLASS {
40 ObjectBasicInformation,
41 ObjectNameInformation,
42 ObjectTypeInformation,
45 } OBJECT_INFORMATION_CLASS, *POBJECT_INFORMATION_CLASS;
47 typedef struct _OBJECT_BASIC_INFORMATION {
49 ACCESS_MASK GrantedAccess;
53 ULONG NonPagedPoolUsage;
55 ULONG NameInformationLength;
56 ULONG TypeInformationLength;
57 ULONG SecurityDescriptorLength;
58 LARGE_INTEGER CreateTime;
59 } OBJECT_BASIC_INFORMATION, *POBJECT_BASIC_INFORMATION;
61 #define expect_eq(expr, value, type, format) { type ret = expr; ok((value) == ret, #expr " expected " format " got " format "\n", (value), (ret)); }
63 static BOOL (WINAPI *pAddAccessAllowedAceEx)(PACL, DWORD, DWORD, DWORD, PSID);
64 static BOOL (WINAPI *pAddAccessDeniedAceEx)(PACL, DWORD, DWORD, DWORD, PSID);
65 static BOOL (WINAPI *pAddAuditAccessAceEx)(PACL, DWORD, DWORD, DWORD, PSID, BOOL, BOOL);
66 static VOID (WINAPI *pBuildTrusteeWithSidA)( PTRUSTEEA pTrustee, PSID pSid );
67 static VOID (WINAPI *pBuildTrusteeWithNameA)( PTRUSTEEA pTrustee, LPSTR pName );
68 static VOID (WINAPI *pBuildTrusteeWithObjectsAndNameA)( PTRUSTEEA pTrustee,
69 POBJECTS_AND_NAME_A pObjName,
70 SE_OBJECT_TYPE ObjectType,
72 LPSTR InheritedObjectTypeName,
74 static VOID (WINAPI *pBuildTrusteeWithObjectsAndSidA)( PTRUSTEEA pTrustee,
75 POBJECTS_AND_SID pObjSid,
77 GUID* pInheritedObjectGuid,
79 static LPSTR (WINAPI *pGetTrusteeNameA)( PTRUSTEEA pTrustee );
80 static BOOL (WINAPI *pMakeSelfRelativeSD)( PSECURITY_DESCRIPTOR, PSECURITY_DESCRIPTOR, LPDWORD );
81 static BOOL (WINAPI *pConvertSidToStringSidA)( PSID pSid, LPSTR *str );
82 static BOOL (WINAPI *pConvertStringSidToSidA)( LPCSTR str, PSID pSid );
83 static BOOL (WINAPI *pCheckTokenMembership)(HANDLE, PSID, PBOOL);
84 static BOOL (WINAPI *pConvertStringSecurityDescriptorToSecurityDescriptorA)(LPCSTR, DWORD,
85 PSECURITY_DESCRIPTOR*, PULONG );
86 static BOOL (WINAPI *pConvertStringSecurityDescriptorToSecurityDescriptorW)(LPCWSTR, DWORD,
87 PSECURITY_DESCRIPTOR*, PULONG );
88 static BOOL (WINAPI *pConvertSecurityDescriptorToStringSecurityDescriptorA)(PSECURITY_DESCRIPTOR, DWORD,
89 SECURITY_INFORMATION, LPSTR *, PULONG );
90 static BOOL (WINAPI *pGetFileSecurityA)(LPCSTR, SECURITY_INFORMATION,
91 PSECURITY_DESCRIPTOR, DWORD, LPDWORD);
92 static BOOL (WINAPI *pSetFileSecurityA)(LPCSTR, SECURITY_INFORMATION,
93 PSECURITY_DESCRIPTOR);
94 static DWORD (WINAPI *pGetNamedSecurityInfoA)(LPSTR, SE_OBJECT_TYPE, SECURITY_INFORMATION,
95 PSID*, PSID*, PACL*, PACL*,
96 PSECURITY_DESCRIPTOR*);
97 static PDWORD (WINAPI *pGetSidSubAuthority)(PSID, DWORD);
98 static PUCHAR (WINAPI *pGetSidSubAuthorityCount)(PSID);
99 static BOOL (WINAPI *pIsValidSid)(PSID);
100 static DWORD (WINAPI *pRtlAdjustPrivilege)(ULONG,BOOLEAN,BOOLEAN,PBOOLEAN);
101 static BOOL (WINAPI *pCreateWellKnownSid)(WELL_KNOWN_SID_TYPE,PSID,PSID,DWORD*);
102 static BOOL (WINAPI *pDuplicateTokenEx)(HANDLE,DWORD,LPSECURITY_ATTRIBUTES,
103 SECURITY_IMPERSONATION_LEVEL,TOKEN_TYPE,PHANDLE);
105 static NTSTATUS (WINAPI *pLsaQueryInformationPolicy)(LSA_HANDLE,POLICY_INFORMATION_CLASS,PVOID*);
106 static NTSTATUS (WINAPI *pLsaClose)(LSA_HANDLE);
107 static NTSTATUS (WINAPI *pLsaFreeMemory)(PVOID);
108 static NTSTATUS (WINAPI *pLsaOpenPolicy)(PLSA_UNICODE_STRING,PLSA_OBJECT_ATTRIBUTES,ACCESS_MASK,PLSA_HANDLE);
109 static NTSTATUS (WINAPI *pNtQueryObject)(HANDLE,OBJECT_INFORMATION_CLASS,PVOID,ULONG,PULONG);
110 static DWORD (WINAPI *pSetEntriesInAclW)(ULONG, PEXPLICIT_ACCESSW, PACL, PACL*);
111 static DWORD (WINAPI *pSetEntriesInAclA)(ULONG, PEXPLICIT_ACCESSA, PACL, PACL*);
112 static BOOL (WINAPI *pSetSecurityDescriptorControl)(PSECURITY_DESCRIPTOR, SECURITY_DESCRIPTOR_CONTROL,
113 SECURITY_DESCRIPTOR_CONTROL);
114 static DWORD (WINAPI *pGetSecurityInfo)(HANDLE, SE_OBJECT_TYPE, SECURITY_INFORMATION,
115 PSID*, PSID*, PACL*, PACL*, PSECURITY_DESCRIPTOR*);
116 static NTSTATUS (WINAPI *pNtAccessCheck)(PSECURITY_DESCRIPTOR, HANDLE, ACCESS_MASK, PGENERIC_MAPPING,
117 PPRIVILEGE_SET, PULONG, PULONG, NTSTATUS*);
121 static char** myARGV;
125 SID_IDENTIFIER_AUTHORITY auth;
129 static void init(void)
133 hntdll = GetModuleHandleA("ntdll.dll");
134 pNtQueryObject = (void *)GetProcAddress( hntdll, "NtQueryObject" );
135 pNtAccessCheck = (void *)GetProcAddress( hntdll, "NtAccessCheck" );
137 hmod = GetModuleHandle("advapi32.dll");
138 pAddAccessAllowedAceEx = (void *)GetProcAddress(hmod, "AddAccessAllowedAceEx");
139 pAddAccessDeniedAceEx = (void *)GetProcAddress(hmod, "AddAccessDeniedAceEx");
140 pAddAuditAccessAceEx = (void *)GetProcAddress(hmod, "AddAuditAccessAceEx");
141 pCheckTokenMembership = (void *)GetProcAddress(hmod, "CheckTokenMembership");
142 pConvertStringSecurityDescriptorToSecurityDescriptorA =
143 (void *)GetProcAddress(hmod, "ConvertStringSecurityDescriptorToSecurityDescriptorA" );
144 pConvertStringSecurityDescriptorToSecurityDescriptorW =
145 (void *)GetProcAddress(hmod, "ConvertStringSecurityDescriptorToSecurityDescriptorW" );
146 pConvertSecurityDescriptorToStringSecurityDescriptorA =
147 (void *)GetProcAddress(hmod, "ConvertSecurityDescriptorToStringSecurityDescriptorA" );
148 pGetFileSecurityA = (void *)GetProcAddress(hmod, "GetFileSecurityA" );
149 pSetFileSecurityA = (void *)GetProcAddress(hmod, "SetFileSecurityA" );
150 pCreateWellKnownSid = (void *)GetProcAddress( hmod, "CreateWellKnownSid" );
151 pGetNamedSecurityInfoA = (void *)GetProcAddress(hmod, "GetNamedSecurityInfoA");
152 pGetSidSubAuthority = (void *)GetProcAddress(hmod, "GetSidSubAuthority");
153 pGetSidSubAuthorityCount = (void *)GetProcAddress(hmod, "GetSidSubAuthorityCount");
154 pIsValidSid = (void *)GetProcAddress(hmod, "IsValidSid");
155 pMakeSelfRelativeSD = (void *)GetProcAddress(hmod, "MakeSelfRelativeSD");
156 pSetEntriesInAclW = (void *)GetProcAddress(hmod, "SetEntriesInAclW");
157 pSetEntriesInAclA = (void *)GetProcAddress(hmod, "SetEntriesInAclA");
158 pSetSecurityDescriptorControl = (void *)GetProcAddress(hmod, "SetSecurityDescriptorControl");
159 pGetSecurityInfo = (void *)GetProcAddress(hmod, "GetSecurityInfo");
161 myARGC = winetest_get_mainargs( &myARGV );
164 static void test_str_sid(const char *str_sid)
169 if (pConvertStringSidToSidA(str_sid, &psid))
171 if (pConvertSidToStringSidA(psid, &temp))
173 trace(" %s: %s\n", str_sid, temp);
180 if (GetLastError() != ERROR_INVALID_SID)
181 trace(" %s: couldn't be converted, returned %d\n", str_sid, GetLastError());
183 trace(" %s: couldn't be converted\n", str_sid);
187 static void test_sid(void)
189 struct sidRef refs[] = {
190 { { {0x00,0x00,0x33,0x44,0x55,0x66} }, "S-1-860116326-1" },
191 { { {0x00,0x00,0x01,0x02,0x03,0x04} }, "S-1-16909060-1" },
192 { { {0x00,0x00,0x00,0x01,0x02,0x03} }, "S-1-66051-1" },
193 { { {0x00,0x00,0x00,0x00,0x01,0x02} }, "S-1-258-1" },
194 { { {0x00,0x00,0x00,0x00,0x00,0x02} }, "S-1-2-1" },
195 { { {0x00,0x00,0x00,0x00,0x00,0x0c} }, "S-1-12-1" },
197 const char noSubAuthStr[] = "S-1-5";
204 pConvertSidToStringSidA = (void *)GetProcAddress( hmod, "ConvertSidToStringSidA" );
205 if( !pConvertSidToStringSidA )
207 pConvertStringSidToSidA = (void *)GetProcAddress( hmod, "ConvertStringSidToSidA" );
208 if( !pConvertStringSidToSidA )
211 r = pConvertStringSidToSidA( NULL, NULL );
212 ok( !r, "expected failure with NULL parameters\n" );
213 if( GetLastError() == ERROR_CALL_NOT_IMPLEMENTED )
215 ok( GetLastError() == ERROR_INVALID_PARAMETER,
216 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
219 r = pConvertStringSidToSidA( refs[0].refStr, NULL );
220 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
221 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
224 r = pConvertStringSidToSidA( NULL, &str );
225 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
226 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
229 r = pConvertStringSidToSidA( noSubAuthStr, &psid );
231 "expected failure with no sub authorities\n" );
232 ok( GetLastError() == ERROR_INVALID_SID,
233 "expected GetLastError() is ERROR_INVALID_SID, got %d\n",
236 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid), "ConvertStringSidToSidA failed\n");
238 ok(pisid->SubAuthorityCount == 4, "Invalid sub authority count - expected 4, got %d\n", pisid->SubAuthorityCount);
239 ok(pisid->SubAuthority[0] == 21, "Invalid subauthority 0 - expceted 21, got %d\n", pisid->SubAuthority[0]);
240 ok(pisid->SubAuthority[3] == 4576, "Invalid subauthority 0 - expceted 4576, got %d\n", pisid->SubAuthority[3]);
244 for( i = 0; i < sizeof(refs) / sizeof(refs[0]); i++ )
248 r = AllocateAndInitializeSid( &refs[i].auth, 1,1,0,0,0,0,0,0,0,
250 ok( r, "failed to allocate sid\n" );
251 r = pConvertSidToStringSidA( psid, &str );
252 ok( r, "failed to convert sid\n" );
255 ok( !strcmp( str, refs[i].refStr ),
256 "incorrect sid, expected %s, got %s\n", refs[i].refStr, str );
262 r = pConvertStringSidToSidA( refs[i].refStr, &psid );
263 ok( r, "failed to parse sid string\n" );
266 !memcmp( pisid->IdentifierAuthority.Value, refs[i].auth.Value,
267 sizeof(refs[i].auth) ),
268 "string sid %s didn't parse to expected value\n"
269 "(got 0x%04x%08x, expected 0x%04x%08x)\n",
271 MAKEWORD( pisid->IdentifierAuthority.Value[1],
272 pisid->IdentifierAuthority.Value[0] ),
273 MAKELONG( MAKEWORD( pisid->IdentifierAuthority.Value[5],
274 pisid->IdentifierAuthority.Value[4] ),
275 MAKEWORD( pisid->IdentifierAuthority.Value[3],
276 pisid->IdentifierAuthority.Value[2] ) ),
277 MAKEWORD( refs[i].auth.Value[1], refs[i].auth.Value[0] ),
278 MAKELONG( MAKEWORD( refs[i].auth.Value[5], refs[i].auth.Value[4] ),
279 MAKEWORD( refs[i].auth.Value[3], refs[i].auth.Value[2] ) ) );
284 trace("String SIDs:\n");
325 static void test_trustee(void)
327 GUID ObjectType = {0x12345678, 0x1234, 0x5678, {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}};
328 GUID InheritedObjectType = {0x23456789, 0x2345, 0x6786, {0x2, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}};
330 OBJECTS_AND_NAME_ oan;
334 char szObjectTypeName[] = "ObjectTypeName";
335 char szInheritedObjectTypeName[] = "InheritedObjectTypeName";
336 char szTrusteeName[] = "szTrusteeName";
337 SID_IDENTIFIER_AUTHORITY auth = { {0x11,0x22,0,0,0, 0} };
339 memset( &ZeroGuid, 0x00, sizeof (ZeroGuid) );
341 pBuildTrusteeWithSidA = (void *)GetProcAddress( hmod, "BuildTrusteeWithSidA" );
342 pBuildTrusteeWithNameA = (void *)GetProcAddress( hmod, "BuildTrusteeWithNameA" );
343 pBuildTrusteeWithObjectsAndNameA = (void *)GetProcAddress (hmod, "BuildTrusteeWithObjectsAndNameA" );
344 pBuildTrusteeWithObjectsAndSidA = (void *)GetProcAddress (hmod, "BuildTrusteeWithObjectsAndSidA" );
345 pGetTrusteeNameA = (void *)GetProcAddress (hmod, "GetTrusteeNameA" );
346 if( !pBuildTrusteeWithSidA || !pBuildTrusteeWithNameA ||
347 !pBuildTrusteeWithObjectsAndNameA || !pBuildTrusteeWithObjectsAndSidA ||
351 if ( ! AllocateAndInitializeSid( &auth, 1, 42, 0,0,0,0,0,0,0,&psid ) )
353 trace( "failed to init SID\n" );
357 /* test BuildTrusteeWithSidA */
358 memset( &trustee, 0xff, sizeof trustee );
359 pBuildTrusteeWithSidA( &trustee, psid );
361 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
362 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
363 "MultipleTrusteeOperation wrong\n");
364 ok( trustee.TrusteeForm == TRUSTEE_IS_SID, "TrusteeForm wrong\n");
365 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
366 ok( trustee.ptstrName == psid, "ptstrName wrong\n" );
368 /* test BuildTrusteeWithObjectsAndSidA (test 1) */
369 memset( &trustee, 0xff, sizeof trustee );
370 memset( &oas, 0xff, sizeof(oas) );
371 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, &ObjectType,
372 &InheritedObjectType, psid);
374 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
375 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
376 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
377 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
378 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
380 ok(oas.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
381 ok(!memcmp(&oas.ObjectTypeGuid, &ObjectType, sizeof(GUID)), "ObjectTypeGuid wrong\n");
382 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
383 ok(oas.pSid == psid, "pSid wrong\n");
385 /* test GetTrusteeNameA */
386 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oas, "GetTrusteeName returned wrong value\n");
388 /* test BuildTrusteeWithObjectsAndSidA (test 2) */
389 memset( &trustee, 0xff, sizeof trustee );
390 memset( &oas, 0xff, sizeof(oas) );
391 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, NULL,
392 &InheritedObjectType, psid);
394 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
395 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
396 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
397 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
398 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
400 ok(oas.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
401 ok(!memcmp(&oas.ObjectTypeGuid, &ZeroGuid, sizeof(GUID)), "ObjectTypeGuid wrong\n");
402 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
403 ok(oas.pSid == psid, "pSid wrong\n");
407 /* test BuildTrusteeWithNameA */
408 memset( &trustee, 0xff, sizeof trustee );
409 pBuildTrusteeWithNameA( &trustee, szTrusteeName );
411 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
412 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
413 "MultipleTrusteeOperation wrong\n");
414 ok( trustee.TrusteeForm == TRUSTEE_IS_NAME, "TrusteeForm wrong\n");
415 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
416 ok( trustee.ptstrName == szTrusteeName, "ptstrName wrong\n" );
418 /* test BuildTrusteeWithObjectsAndNameA (test 1) */
419 memset( &trustee, 0xff, sizeof trustee );
420 memset( &oan, 0xff, sizeof(oan) );
421 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
422 szInheritedObjectTypeName, szTrusteeName);
424 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
425 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
426 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
427 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
428 ok(trustee.ptstrName == (LPTSTR)&oan, "ptstrName wrong\n");
430 ok(oan.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
431 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
432 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
433 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
435 /* test GetTrusteeNameA */
436 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oan, "GetTrusteeName returned wrong value\n");
438 /* test BuildTrusteeWithObjectsAndNameA (test 2) */
439 memset( &trustee, 0xff, sizeof trustee );
440 memset( &oan, 0xff, sizeof(oan) );
441 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, NULL,
442 szInheritedObjectTypeName, szTrusteeName);
444 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
445 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
446 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
447 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
448 ok(trustee.ptstrName == (LPSTR)&oan, "ptstrName wrong\n");
450 ok(oan.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
451 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
452 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
453 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
455 /* test BuildTrusteeWithObjectsAndNameA (test 3) */
456 memset( &trustee, 0xff, sizeof trustee );
457 memset( &oan, 0xff, sizeof(oan) );
458 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
459 NULL, szTrusteeName);
461 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
462 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
463 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
464 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
465 ok(trustee.ptstrName == (LPTSTR)&oan, "ptstrName wrong\n");
467 ok(oan.ObjectsPresent == ACE_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
468 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
469 ok(oan.InheritedObjectTypeName == NULL, "InheritedObjectTypeName wrong\n");
470 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
473 /* If the first isn't defined, assume none is */
474 #ifndef SE_MIN_WELL_KNOWN_PRIVILEGE
475 #define SE_MIN_WELL_KNOWN_PRIVILEGE 2L
476 #define SE_CREATE_TOKEN_PRIVILEGE 2L
477 #define SE_ASSIGNPRIMARYTOKEN_PRIVILEGE 3L
478 #define SE_LOCK_MEMORY_PRIVILEGE 4L
479 #define SE_INCREASE_QUOTA_PRIVILEGE 5L
480 #define SE_MACHINE_ACCOUNT_PRIVILEGE 6L
481 #define SE_TCB_PRIVILEGE 7L
482 #define SE_SECURITY_PRIVILEGE 8L
483 #define SE_TAKE_OWNERSHIP_PRIVILEGE 9L
484 #define SE_LOAD_DRIVER_PRIVILEGE 10L
485 #define SE_SYSTEM_PROFILE_PRIVILEGE 11L
486 #define SE_SYSTEMTIME_PRIVILEGE 12L
487 #define SE_PROF_SINGLE_PROCESS_PRIVILEGE 13L
488 #define SE_INC_BASE_PRIORITY_PRIVILEGE 14L
489 #define SE_CREATE_PAGEFILE_PRIVILEGE 15L
490 #define SE_CREATE_PERMANENT_PRIVILEGE 16L
491 #define SE_BACKUP_PRIVILEGE 17L
492 #define SE_RESTORE_PRIVILEGE 18L
493 #define SE_SHUTDOWN_PRIVILEGE 19L
494 #define SE_DEBUG_PRIVILEGE 20L
495 #define SE_AUDIT_PRIVILEGE 21L
496 #define SE_SYSTEM_ENVIRONMENT_PRIVILEGE 22L
497 #define SE_CHANGE_NOTIFY_PRIVILEGE 23L
498 #define SE_REMOTE_SHUTDOWN_PRIVILEGE 24L
499 #define SE_UNDOCK_PRIVILEGE 25L
500 #define SE_SYNC_AGENT_PRIVILEGE 26L
501 #define SE_ENABLE_DELEGATION_PRIVILEGE 27L
502 #define SE_MANAGE_VOLUME_PRIVILEGE 28L
503 #define SE_IMPERSONATE_PRIVILEGE 29L
504 #define SE_CREATE_GLOBAL_PRIVILEGE 30L
505 #define SE_MAX_WELL_KNOWN_PRIVILEGE SE_CREATE_GLOBAL_PRIVILEGE
506 #endif /* ndef SE_MIN_WELL_KNOWN_PRIVILEGE */
508 static void test_allocateLuid(void)
510 BOOL (WINAPI *pAllocateLocallyUniqueId)(PLUID);
514 pAllocateLocallyUniqueId = (void*)GetProcAddress(hmod, "AllocateLocallyUniqueId");
515 if (!pAllocateLocallyUniqueId) return;
517 ret = pAllocateLocallyUniqueId(&luid1);
518 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
522 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
523 ret = pAllocateLocallyUniqueId(&luid2);
525 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
526 ok(luid1.LowPart > SE_MAX_WELL_KNOWN_PRIVILEGE || luid1.HighPart != 0,
527 "AllocateLocallyUniqueId returned a well-known LUID\n");
528 ok(luid1.LowPart != luid2.LowPart || luid1.HighPart != luid2.HighPart,
529 "AllocateLocallyUniqueId returned non-unique LUIDs\n");
530 ret = pAllocateLocallyUniqueId(NULL);
531 ok( !ret && GetLastError() == ERROR_NOACCESS,
532 "AllocateLocallyUniqueId(NULL) didn't return ERROR_NOACCESS: %d\n",
536 static void test_lookupPrivilegeName(void)
538 BOOL (WINAPI *pLookupPrivilegeNameA)(LPCSTR, PLUID, LPSTR, LPDWORD);
539 char buf[MAX_PATH]; /* arbitrary, seems long enough */
540 DWORD cchName = sizeof(buf);
541 LUID luid = { 0, 0 };
545 /* check whether it's available first */
546 pLookupPrivilegeNameA = (void*)GetProcAddress(hmod, "LookupPrivilegeNameA");
547 if (!pLookupPrivilegeNameA) return;
548 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
549 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
550 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
553 /* check with a short buffer */
555 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
556 ret = pLookupPrivilegeNameA(NULL, &luid, NULL, &cchName);
557 ok( !ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
558 "LookupPrivilegeNameA didn't fail with ERROR_INSUFFICIENT_BUFFER: %d\n",
560 ok(cchName == strlen("SeCreateTokenPrivilege") + 1,
561 "LookupPrivilegeNameA returned an incorrect required length for\n"
562 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName,
563 lstrlenA("SeCreateTokenPrivilege") + 1);
564 /* check a known value and its returned length on success */
565 cchName = sizeof(buf);
566 ok(pLookupPrivilegeNameA(NULL, &luid, buf, &cchName) &&
567 cchName == strlen("SeCreateTokenPrivilege"),
568 "LookupPrivilegeNameA returned an incorrect output length for\n"
569 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName,
570 (int)strlen("SeCreateTokenPrivilege"));
571 /* check known values */
572 for (i = SE_MIN_WELL_KNOWN_PRIVILEGE; i <= SE_MAX_WELL_KNOWN_PRIVILEGE; i++)
575 cchName = sizeof(buf);
576 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
577 ok( ret || GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
578 "LookupPrivilegeNameA(0.%d) failed: %d\n", i, GetLastError());
580 /* check a bogus LUID */
581 luid.LowPart = 0xdeadbeef;
582 cchName = sizeof(buf);
583 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
584 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
585 "LookupPrivilegeNameA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
587 /* check on a bogus system */
588 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
589 cchName = sizeof(buf);
590 ret = pLookupPrivilegeNameA("b0gu5.Nam3", &luid, buf, &cchName);
591 ok( !ret && (GetLastError() == RPC_S_SERVER_UNAVAILABLE ||
592 GetLastError() == RPC_S_INVALID_NET_ADDR) /* w2k8 */,
593 "LookupPrivilegeNameA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
603 static void test_lookupPrivilegeValue(void)
605 static const struct NameToLUID privs[] = {
606 { "SeCreateTokenPrivilege", SE_CREATE_TOKEN_PRIVILEGE },
607 { "SeAssignPrimaryTokenPrivilege", SE_ASSIGNPRIMARYTOKEN_PRIVILEGE },
608 { "SeLockMemoryPrivilege", SE_LOCK_MEMORY_PRIVILEGE },
609 { "SeIncreaseQuotaPrivilege", SE_INCREASE_QUOTA_PRIVILEGE },
610 { "SeMachineAccountPrivilege", SE_MACHINE_ACCOUNT_PRIVILEGE },
611 { "SeTcbPrivilege", SE_TCB_PRIVILEGE },
612 { "SeSecurityPrivilege", SE_SECURITY_PRIVILEGE },
613 { "SeTakeOwnershipPrivilege", SE_TAKE_OWNERSHIP_PRIVILEGE },
614 { "SeLoadDriverPrivilege", SE_LOAD_DRIVER_PRIVILEGE },
615 { "SeSystemProfilePrivilege", SE_SYSTEM_PROFILE_PRIVILEGE },
616 { "SeSystemtimePrivilege", SE_SYSTEMTIME_PRIVILEGE },
617 { "SeProfileSingleProcessPrivilege", SE_PROF_SINGLE_PROCESS_PRIVILEGE },
618 { "SeIncreaseBasePriorityPrivilege", SE_INC_BASE_PRIORITY_PRIVILEGE },
619 { "SeCreatePagefilePrivilege", SE_CREATE_PAGEFILE_PRIVILEGE },
620 { "SeCreatePermanentPrivilege", SE_CREATE_PERMANENT_PRIVILEGE },
621 { "SeBackupPrivilege", SE_BACKUP_PRIVILEGE },
622 { "SeRestorePrivilege", SE_RESTORE_PRIVILEGE },
623 { "SeShutdownPrivilege", SE_SHUTDOWN_PRIVILEGE },
624 { "SeDebugPrivilege", SE_DEBUG_PRIVILEGE },
625 { "SeAuditPrivilege", SE_AUDIT_PRIVILEGE },
626 { "SeSystemEnvironmentPrivilege", SE_SYSTEM_ENVIRONMENT_PRIVILEGE },
627 { "SeChangeNotifyPrivilege", SE_CHANGE_NOTIFY_PRIVILEGE },
628 { "SeRemoteShutdownPrivilege", SE_REMOTE_SHUTDOWN_PRIVILEGE },
629 { "SeUndockPrivilege", SE_UNDOCK_PRIVILEGE },
630 { "SeSyncAgentPrivilege", SE_SYNC_AGENT_PRIVILEGE },
631 { "SeEnableDelegationPrivilege", SE_ENABLE_DELEGATION_PRIVILEGE },
632 { "SeManageVolumePrivilege", SE_MANAGE_VOLUME_PRIVILEGE },
633 { "SeImpersonatePrivilege", SE_IMPERSONATE_PRIVILEGE },
634 { "SeCreateGlobalPrivilege", SE_CREATE_GLOBAL_PRIVILEGE },
636 BOOL (WINAPI *pLookupPrivilegeValueA)(LPCSTR, LPCSTR, PLUID);
641 /* check whether it's available first */
642 pLookupPrivilegeValueA = (void*)GetProcAddress(hmod, "LookupPrivilegeValueA");
643 if (!pLookupPrivilegeValueA) return;
644 ret = pLookupPrivilegeValueA(NULL, "SeCreateTokenPrivilege", &luid);
645 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
648 /* check a bogus system name */
649 ret = pLookupPrivilegeValueA("b0gu5.Nam3", "SeCreateTokenPrivilege", &luid);
650 ok( !ret && (GetLastError() == RPC_S_SERVER_UNAVAILABLE ||
651 GetLastError() == RPC_S_INVALID_NET_ADDR) /* w2k8 */,
652 "LookupPrivilegeValueA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
654 /* check a NULL string */
655 ret = pLookupPrivilegeValueA(NULL, 0, &luid);
656 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
657 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
659 /* check a bogus privilege name */
660 ret = pLookupPrivilegeValueA(NULL, "SeBogusPrivilege", &luid);
661 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
662 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
664 /* check case insensitive */
665 ret = pLookupPrivilegeValueA(NULL, "sEcREATEtOKENpRIVILEGE", &luid);
667 "LookupPrivilegeValueA(NULL, sEcREATEtOKENpRIVILEGE, &luid) failed: %d\n",
669 for (i = 0; i < sizeof(privs) / sizeof(privs[0]); i++)
671 /* Not all privileges are implemented on all Windows versions, so
672 * don't worry if the call fails
674 if (pLookupPrivilegeValueA(NULL, privs[i].name, &luid))
676 ok(luid.LowPart == privs[i].lowPart,
677 "LookupPrivilegeValueA returned an invalid LUID for %s\n",
683 static void test_luid(void)
686 test_lookupPrivilegeName();
687 test_lookupPrivilegeValue();
690 static void test_FileSecurity(void)
692 char wintmpdir [MAX_PATH];
693 char path [MAX_PATH];
694 char file [MAX_PATH];
696 DWORD sdSize, retSize, rc, granted, priv_set_len;
697 PRIVILEGE_SET priv_set;
700 GENERIC_MAPPING mapping = { FILE_READ_DATA, FILE_WRITE_DATA, FILE_EXECUTE, FILE_ALL_ACCESS };
701 const SECURITY_INFORMATION request = OWNER_SECURITY_INFORMATION
702 | GROUP_SECURITY_INFORMATION
703 | DACL_SECURITY_INFORMATION;
705 if (!pGetFileSecurityA) {
706 win_skip ("GetFileSecurity is not available\n");
710 if (!pSetFileSecurityA) {
711 win_skip ("SetFileSecurity is not available\n");
715 if (!GetTempPathA (sizeof (wintmpdir), wintmpdir)) {
716 win_skip ("GetTempPathA failed\n");
720 /* Create a temporary directory and in it a temporary file */
721 strcat (strcpy (path, wintmpdir), "rary");
722 SetLastError(0xdeadbeef);
723 rc = CreateDirectoryA (path, NULL);
724 ok (rc || GetLastError() == ERROR_ALREADY_EXISTS, "CreateDirectoryA "
725 "failed for '%s' with %d\n", path, GetLastError());
727 strcat (strcpy (file, path), "\\ess");
728 SetLastError(0xdeadbeef);
729 fh = CreateFileA (file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
730 ok (fh != INVALID_HANDLE_VALUE, "CreateFileA "
731 "failed for '%s' with %d\n", file, GetLastError());
734 /* For the temporary file ... */
736 /* Get size needed */
738 SetLastError(0xdeadbeef);
739 rc = pGetFileSecurityA (file, request, NULL, 0, &retSize);
740 if (!rc && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)) {
741 win_skip("GetFileSecurityA is not implemented\n");
744 ok (!rc, "GetFileSecurityA "
745 "was expected to fail for '%s'\n", file);
746 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetFileSecurityA "
747 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
748 ok (retSize > sizeof (SECURITY_DESCRIPTOR), "GetFileSecurityA returned size %d\n", retSize);
751 sd = HeapAlloc (GetProcessHeap (), 0, sdSize);
753 /* Get security descriptor for real */
755 SetLastError(0xdeadbeef);
756 rc = pGetFileSecurityA (file, request, sd, sdSize, &retSize);
757 ok (rc, "GetFileSecurityA "
758 "was not expected to fail '%s': %d\n", file, GetLastError());
759 ok (retSize == sdSize ||
760 broken(retSize == 0), /* NT4 */
761 "GetFileSecurityA returned size %d; expected %d\n", retSize, sdSize);
763 /* Use it to set security descriptor */
764 SetLastError(0xdeadbeef);
765 rc = pSetFileSecurityA (file, request, sd);
766 ok (rc, "SetFileSecurityA "
767 "was not expected to fail '%s': %d\n", file, GetLastError());
769 HeapFree (GetProcessHeap (), 0, sd);
771 /* Repeat for the temporary directory ... */
773 /* Get size needed */
775 SetLastError(0xdeadbeef);
776 rc = pGetFileSecurityA (path, request, NULL, 0, &retSize);
777 ok (!rc, "GetFileSecurityA "
778 "was expected to fail for '%s'\n", path);
779 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetFileSecurityA "
780 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
781 ok (retSize > sizeof (SECURITY_DESCRIPTOR), "GetFileSecurityA returned size %d\n", retSize);
784 sd = HeapAlloc (GetProcessHeap (), 0, sdSize);
786 /* Get security descriptor for real */
788 SetLastError(0xdeadbeef);
789 rc = pGetFileSecurityA (path, request, sd, sdSize, &retSize);
790 ok (rc, "GetFileSecurityA "
791 "was not expected to fail '%s': %d\n", path, GetLastError());
792 ok (retSize == sdSize ||
793 broken(retSize == 0), /* NT4 */
794 "GetFileSecurityA returned size %d; expected %d\n", retSize, sdSize);
796 /* Use it to set security descriptor */
797 SetLastError(0xdeadbeef);
798 rc = pSetFileSecurityA (path, request, sd);
799 ok (rc, "SetFileSecurityA "
800 "was not expected to fail '%s': %d\n", path, GetLastError());
801 HeapFree (GetProcessHeap (), 0, sd);
804 strcpy (wintmpdir, "\\Should not exist");
805 SetLastError(0xdeadbeef);
806 rc = pGetFileSecurityA (wintmpdir, OWNER_SECURITY_INFORMATION, NULL, 0, &sdSize);
807 ok (!rc, "GetFileSecurityA should fail for not existing directories/files\n");
808 ok (GetLastError() == ERROR_FILE_NOT_FOUND,
809 "last error ERROR_FILE_NOT_FOUND expected, got %d\n", GetLastError());
812 /* Remove temporary file and directory */
814 RemoveDirectoryA(path);
816 /* Test file access permissions for a file with FILE_ATTRIBUTE_ARCHIVE */
817 SetLastError(0xdeadbeef);
818 rc = GetTempPath(sizeof(wintmpdir), wintmpdir);
819 ok(rc, "GetTempPath error %d\n", GetLastError());
821 SetLastError(0xdeadbeef);
822 rc = GetTempFileName(wintmpdir, "tmp", 0, file);
823 ok(rc, "GetTempFileName error %d\n", GetLastError());
825 rc = GetFileAttributes(file);
826 rc &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
827 ok(rc == FILE_ATTRIBUTE_ARCHIVE, "expected FILE_ATTRIBUTE_ARCHIVE got %#x\n", rc);
829 retSize = 0xdeadbeef;
830 rc = GetFileSecurity(file, OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION,
832 ok(!rc, "GetFileSecurity should fail\n");
833 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
834 "expected ERROR_INSUFFICIENT_BUFFER got %d\n", GetLastError());
835 ok(sdSize > sizeof(SECURITY_DESCRIPTOR), "got sd size %d\n", sdSize);
837 sd = HeapAlloc(GetProcessHeap (), 0, sdSize);
838 retSize = 0xdeadbeef;
839 SetLastError(0xdeadbeef);
840 rc = GetFileSecurity(file, OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION,
841 sd, sdSize, &retSize);
842 ok(rc, "GetFileSecurity error %d\n", GetLastError());
843 ok(retSize == sdSize || broken(retSize == 0) /* NT4 */, "expected %d, got %d\n", sdSize, retSize);
845 SetLastError(0xdeadbeef);
846 rc = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE, &token);
847 ok(!rc, "OpenThreadToken should fail\n");
848 ok(GetLastError() == ERROR_NO_TOKEN, "expected ERROR_NO_TOKEN, got %d\n", GetLastError());
850 SetLastError(0xdeadbeef);
851 rc = ImpersonateSelf(SecurityIdentification);
852 ok(rc, "ImpersonateSelf error %d\n", GetLastError());
854 SetLastError(0xdeadbeef);
855 rc = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE, &token);
856 ok(rc, "OpenThreadToken error %d\n", GetLastError());
858 SetLastError(0xdeadbeef);
860 ok(rc, "RevertToSelf error %d\n", GetLastError());
862 priv_set_len = sizeof(priv_set);
863 granted = 0xdeadbeef;
865 SetLastError(0xdeadbeef);
866 rc = AccessCheck(sd, token, FILE_READ_DATA, &mapping, &priv_set, &priv_set_len, &granted, &status);
867 ok(rc, "AccessCheck error %d\n", GetLastError());
868 ok(status == 1, "expected 1, got %d\n", status);
869 ok(granted == FILE_READ_DATA, "expected FILE_READ_DATA, got %#x\n", granted);
871 granted = 0xdeadbeef;
873 SetLastError(0xdeadbeef);
874 rc = AccessCheck(sd, token, FILE_WRITE_DATA, &mapping, &priv_set, &priv_set_len, &granted, &status);
875 ok(rc, "AccessCheck error %d\n", GetLastError());
876 ok(status == 1, "expected 1, got %d\n", status);
877 ok(granted == FILE_WRITE_DATA, "expected FILE_WRITE_DATA, got %#x\n", granted);
879 granted = 0xdeadbeef;
881 SetLastError(0xdeadbeef);
882 rc = AccessCheck(sd, token, FILE_EXECUTE, &mapping, &priv_set, &priv_set_len, &granted, &status);
883 ok(rc, "AccessCheck error %d\n", GetLastError());
884 ok(status == 1, "expected 1, got %d\n", status);
885 ok(granted == FILE_EXECUTE, "expected FILE_EXECUTE, got %#x\n", granted);
887 granted = 0xdeadbeef;
889 SetLastError(0xdeadbeef);
890 rc = AccessCheck(sd, token, DELETE, &mapping, &priv_set, &priv_set_len, &granted, &status);
891 ok(rc, "AccessCheck error %d\n", GetLastError());
892 ok(status == 1, "expected 1, got %d\n", status);
893 ok(granted == DELETE, "expected DELETE, got %#x\n", granted);
895 granted = 0xdeadbeef;
897 SetLastError(0xdeadbeef);
898 rc = AccessCheck(sd, token, FILE_DELETE_CHILD, &mapping, &priv_set, &priv_set_len, &granted, &status);
899 ok(rc, "AccessCheck error %d\n", GetLastError());
900 ok(status == 1, "expected 1, got %d\n", status);
901 ok(granted == FILE_DELETE_CHILD, "expected FILE_DELETE_CHILD, got %#x\n", granted);
903 granted = 0xdeadbeef;
905 SetLastError(0xdeadbeef);
906 rc = AccessCheck(sd, token, 0x1ff, &mapping, &priv_set, &priv_set_len, &granted, &status);
907 ok(rc, "AccessCheck error %d\n", GetLastError());
908 ok(status == 1, "expected 1, got %d\n", status);
909 ok(granted == 0x1ff, "expected 0x1ff, got %#x\n", granted);
911 granted = 0xdeadbeef;
913 SetLastError(0xdeadbeef);
914 rc = AccessCheck(sd, token, FILE_ALL_ACCESS, &mapping, &priv_set, &priv_set_len, &granted, &status);
915 ok(rc, "AccessCheck error %d\n", GetLastError());
916 ok(status == 1, "expected 1, got %d\n", status);
917 ok(granted == FILE_ALL_ACCESS, "expected FILE_ALL_ACCESS, got %#x\n", granted);
919 SetLastError(0xdeadbeef);
920 rc = AccessCheck(sd, token, 0xffffffff, &mapping, &priv_set, &priv_set_len, &granted, &status);
921 ok(!rc, "AccessCheck should fail\n");
922 ok(GetLastError() == ERROR_GENERIC_NOT_MAPPED, "expected ERROR_GENERIC_NOT_MAPPED, got %d\n", GetLastError());
924 /* Test file access permissions for a file with FILE_ATTRIBUTE_READONLY */
925 SetLastError(0xdeadbeef);
926 fh = CreateFile(file, FILE_READ_DATA, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_READONLY, 0);
927 ok(fh != INVALID_HANDLE_VALUE, "CreateFile error %d\n", GetLastError());
928 retSize = 0xdeadbeef;
929 SetLastError(0xdeadbeef);
930 rc = WriteFile(fh, "1", 1, &retSize, NULL);
931 ok(!rc, "WriteFile should fail\n");
932 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
933 ok(retSize == 0, "expected 0, got %d\n", retSize);
936 rc = GetFileAttributes(file);
937 rc &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
939 ok(rc == (FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY),
940 "expected FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY got %#x\n", rc);
942 SetLastError(0xdeadbeef);
943 rc = SetFileAttributes(file, FILE_ATTRIBUTE_ARCHIVE);
944 ok(rc, "SetFileAttributes error %d\n", GetLastError());
945 SetLastError(0xdeadbeef);
946 rc = DeleteFile(file);
947 ok(rc, "DeleteFile error %d\n", GetLastError());
949 SetLastError(0xdeadbeef);
950 fh = CreateFile(file, FILE_READ_DATA, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_READONLY, 0);
951 ok(fh != INVALID_HANDLE_VALUE, "CreateFile error %d\n", GetLastError());
952 retSize = 0xdeadbeef;
953 SetLastError(0xdeadbeef);
954 rc = WriteFile(fh, "1", 1, &retSize, NULL);
955 ok(!rc, "WriteFile should fail\n");
956 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
957 ok(retSize == 0, "expected 0, got %d\n", retSize);
960 rc = GetFileAttributes(file);
961 rc &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
962 ok(rc == (FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY),
963 "expected FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY got %#x\n", rc);
965 retSize = 0xdeadbeef;
966 SetLastError(0xdeadbeef);
967 rc = GetFileSecurity(file, OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION,
968 sd, sdSize, &retSize);
969 ok(rc, "GetFileSecurity error %d\n", GetLastError());
970 ok(retSize == sdSize || broken(retSize == 0) /* NT4 */, "expected %d, got %d\n", sdSize, retSize);
972 priv_set_len = sizeof(priv_set);
973 granted = 0xdeadbeef;
975 SetLastError(0xdeadbeef);
976 rc = AccessCheck(sd, token, FILE_READ_DATA, &mapping, &priv_set, &priv_set_len, &granted, &status);
977 ok(rc, "AccessCheck error %d\n", GetLastError());
978 ok(status == 1, "expected 1, got %d\n", status);
979 ok(granted == FILE_READ_DATA, "expected FILE_READ_DATA, got %#x\n", granted);
981 granted = 0xdeadbeef;
983 SetLastError(0xdeadbeef);
984 rc = AccessCheck(sd, token, FILE_WRITE_DATA, &mapping, &priv_set, &priv_set_len, &granted, &status);
985 ok(rc, "AccessCheck error %d\n", GetLastError());
987 ok(status == 1, "expected 1, got %d\n", status);
988 ok(granted == FILE_WRITE_DATA, "expected FILE_WRITE_DATA, got %#x\n", granted);
990 granted = 0xdeadbeef;
992 SetLastError(0xdeadbeef);
993 rc = AccessCheck(sd, token, FILE_EXECUTE, &mapping, &priv_set, &priv_set_len, &granted, &status);
994 ok(rc, "AccessCheck error %d\n", GetLastError());
995 ok(status == 1, "expected 1, got %d\n", status);
996 ok(granted == FILE_EXECUTE, "expected FILE_EXECUTE, got %#x\n", granted);
998 granted = 0xdeadbeef;
1000 SetLastError(0xdeadbeef);
1001 rc = AccessCheck(sd, token, DELETE, &mapping, &priv_set, &priv_set_len, &granted, &status);
1002 ok(rc, "AccessCheck error %d\n", GetLastError());
1004 ok(status == 1, "expected 1, got %d\n", status);
1005 ok(granted == DELETE, "expected DELETE, got %#x\n", granted);
1007 granted = 0xdeadbeef;
1008 status = 0xdeadbeef;
1009 SetLastError(0xdeadbeef);
1010 rc = AccessCheck(sd, token, FILE_DELETE_CHILD, &mapping, &priv_set, &priv_set_len, &granted, &status);
1011 ok(rc, "AccessCheck error %d\n", GetLastError());
1013 ok(status == 1, "expected 1, got %d\n", status);
1014 ok(granted == FILE_DELETE_CHILD, "expected FILE_DELETE_CHILD, got %#x\n", granted);
1016 granted = 0xdeadbeef;
1017 status = 0xdeadbeef;
1018 SetLastError(0xdeadbeef);
1019 rc = AccessCheck(sd, token, 0x1ff, &mapping, &priv_set, &priv_set_len, &granted, &status);
1020 ok(rc, "AccessCheck error %d\n", GetLastError());
1022 ok(status == 1, "expected 1, got %d\n", status);
1023 ok(granted == 0x1ff, "expected 0x1ff, got %#x\n", granted);
1025 granted = 0xdeadbeef;
1026 status = 0xdeadbeef;
1027 SetLastError(0xdeadbeef);
1028 rc = AccessCheck(sd, token, FILE_ALL_ACCESS, &mapping, &priv_set, &priv_set_len, &granted, &status);
1029 ok(rc, "AccessCheck error %d\n", GetLastError());
1031 ok(status == 1, "expected 1, got %d\n", status);
1032 ok(granted == FILE_ALL_ACCESS, "expected FILE_ALL_ACCESS, got %#x\n", granted);
1034 SetLastError(0xdeadbeef);
1035 rc = DeleteFile(file);
1036 ok(!rc, "DeleteFile should fail\n");
1037 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1038 SetLastError(0xdeadbeef);
1039 rc = SetFileAttributes(file, FILE_ATTRIBUTE_ARCHIVE);
1040 ok(rc, "SetFileAttributes error %d\n", GetLastError());
1041 SetLastError(0xdeadbeef);
1042 rc = DeleteFile(file);
1043 ok(rc, "DeleteFile error %d\n", GetLastError());
1046 HeapFree(GetProcessHeap(), 0, sd);
1049 static void test_AccessCheck(void)
1051 PSID EveryoneSid = NULL, AdminSid = NULL, UsersSid = NULL;
1053 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL;
1054 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
1055 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
1056 GENERIC_MAPPING Mapping = { KEY_READ, KEY_WRITE, KEY_EXECUTE, KEY_ALL_ACCESS };
1060 HANDLE ProcessToken;
1063 PRIVILEGE_SET *PrivSet;
1065 HMODULE NtDllModule;
1068 NTSTATUS ntret, ntAccessStatus;
1070 NtDllModule = GetModuleHandle("ntdll.dll");
1073 skip("not running on NT, skipping test\n");
1076 pRtlAdjustPrivilege = (void *)GetProcAddress(NtDllModule, "RtlAdjustPrivilege");
1077 if (!pRtlAdjustPrivilege)
1079 win_skip("missing RtlAdjustPrivilege, skipping test\n");
1083 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
1084 res = InitializeAcl(Acl, 256, ACL_REVISION);
1085 if(!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1087 skip("ACLs not implemented - skipping tests\n");
1088 HeapFree(GetProcessHeap(), 0, Acl);
1091 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
1093 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
1094 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1096 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
1097 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdminSid);
1098 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1100 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
1101 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &UsersSid);
1102 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1104 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
1106 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
1107 ok(res, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
1109 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
1110 ok(res, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1112 PrivSetLen = FIELD_OFFSET(PRIVILEGE_SET, Privilege[16]);
1113 PrivSet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, PrivSetLen);
1114 PrivSet->PrivilegeCount = 16;
1116 res = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &ProcessToken);
1117 ok(res, "OpenProcessToken failed with error %d\n", GetLastError());
1119 pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, FALSE, TRUE, &Enabled);
1121 res = DuplicateToken(ProcessToken, SecurityImpersonation, &Token);
1122 ok(res, "DuplicateToken failed with error %d\n", GetLastError());
1124 /* SD without owner/group */
1125 SetLastError(0xdeadbeef);
1126 Access = AccessStatus = 0x1abe11ed;
1127 ret = AccessCheck(SecurityDescriptor, Token, KEY_QUERY_VALUE, &Mapping,
1128 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1129 err = GetLastError();
1130 ok(!ret && err == ERROR_INVALID_SECURITY_DESCR, "AccessCheck should have "
1131 "failed with ERROR_INVALID_SECURITY_DESCR, instead of %d\n", err);
1132 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
1133 "Access and/or AccessStatus were changed!\n");
1135 /* Set owner and group */
1136 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
1137 ok(res, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
1138 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, TRUE);
1139 ok(res, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
1141 /* Generic access mask */
1142 SetLastError(0xdeadbeef);
1143 Access = AccessStatus = 0x1abe11ed;
1144 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1145 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1146 err = GetLastError();
1147 ok(!ret && err == ERROR_GENERIC_NOT_MAPPED, "AccessCheck should have failed "
1148 "with ERROR_GENERIC_NOT_MAPPED, instead of %d\n", err);
1149 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
1150 "Access and/or AccessStatus were changed!\n");
1152 /* Generic access mask - no privilegeset buffer */
1153 SetLastError(0xdeadbeef);
1154 Access = AccessStatus = 0x1abe11ed;
1155 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1156 NULL, &PrivSetLen, &Access, &AccessStatus);
1157 err = GetLastError();
1158 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
1159 "with ERROR_NOACCESS, instead of %d\n", err);
1160 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
1161 "Access and/or AccessStatus were changed!\n");
1163 /* Generic access mask - no returnlength */
1164 SetLastError(0xdeadbeef);
1165 Access = AccessStatus = 0x1abe11ed;
1166 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1167 PrivSet, NULL, &Access, &AccessStatus);
1168 err = GetLastError();
1169 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
1170 "with ERROR_NOACCESS, instead of %d\n", err);
1171 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
1172 "Access and/or AccessStatus were changed!\n");
1174 /* Generic access mask - no privilegeset buffer, no returnlength */
1175 SetLastError(0xdeadbeef);
1176 Access = AccessStatus = 0x1abe11ed;
1177 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1178 NULL, NULL, &Access, &AccessStatus);
1179 err = GetLastError();
1180 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
1181 "with ERROR_NOACCESS, instead of %d\n", err);
1182 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
1183 "Access and/or AccessStatus were changed!\n");
1185 /* sd with no dacl present */
1186 Access = AccessStatus = 0x1abe11ed;
1187 ret = SetSecurityDescriptorDacl(SecurityDescriptor, FALSE, NULL, FALSE);
1188 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1189 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1190 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1191 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1192 ok(AccessStatus && (Access == KEY_READ),
1193 "AccessCheck failed to grant access with error %d\n",
1196 /* sd with no dacl present - no privilegeset buffer */
1197 SetLastError(0xdeadbeef);
1198 Access = AccessStatus = 0x1abe11ed;
1199 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1200 NULL, &PrivSetLen, &Access, &AccessStatus);
1201 err = GetLastError();
1202 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
1203 "with ERROR_NOACCESS, instead of %d\n", err);
1204 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
1205 "Access and/or AccessStatus were changed!\n");
1209 /* Generic access mask - no privilegeset buffer */
1210 SetLastError(0xdeadbeef);
1211 Access = ntAccessStatus = 0x1abe11ed;
1212 ntret = pNtAccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1213 NULL, &PrivSetLen, &Access, &ntAccessStatus);
1214 err = GetLastError();
1215 ok(ntret == STATUS_ACCESS_VIOLATION,
1216 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret);
1217 ok(err == 0xdeadbeef,
1218 "NtAccessCheck shouldn't set last error, got %d\n", err);
1219 ok(Access == 0x1abe11ed && ntAccessStatus == 0x1abe11ed,
1220 "Access and/or AccessStatus were changed!\n");
1222 /* Generic access mask - no returnlength */
1223 SetLastError(0xdeadbeef);
1224 Access = ntAccessStatus = 0x1abe11ed;
1225 ntret = pNtAccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1226 PrivSet, NULL, &Access, &ntAccessStatus);
1227 err = GetLastError();
1228 ok(ntret == STATUS_ACCESS_VIOLATION,
1229 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret);
1230 ok(err == 0xdeadbeef,
1231 "NtAccessCheck shouldn't set last error, got %d\n", err);
1232 ok(Access == 0x1abe11ed && ntAccessStatus == 0x1abe11ed,
1233 "Access and/or AccessStatus were changed!\n");
1235 /* Generic access mask - no privilegeset buffer, no returnlength */
1236 SetLastError(0xdeadbeef);
1237 Access = ntAccessStatus = 0x1abe11ed;
1238 ntret = pNtAccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1239 NULL, NULL, &Access, &ntAccessStatus);
1240 err = GetLastError();
1241 ok(ntret == STATUS_ACCESS_VIOLATION,
1242 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret);
1243 ok(err == 0xdeadbeef,
1244 "NtAccessCheck shouldn't set last error, got %d\n", err);
1245 ok(Access == 0x1abe11ed && ntAccessStatus == 0x1abe11ed,
1246 "Access and/or AccessStatus were changed!\n");
1249 win_skip("NtAccessCheck unavailable. Skipping.\n");
1251 /* sd with NULL dacl */
1252 Access = AccessStatus = 0x1abe11ed;
1253 ret = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, NULL, FALSE);
1254 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1255 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1256 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1257 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1258 ok(AccessStatus && (Access == KEY_READ),
1259 "AccessCheck failed to grant access with error %d\n",
1262 /* sd with blank dacl */
1263 ret = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, 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 err = GetLastError();
1269 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
1270 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
1271 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
1273 res = AddAccessAllowedAce(Acl, ACL_REVISION, KEY_READ, EveryoneSid);
1274 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
1276 res = AddAccessDeniedAce(Acl, ACL_REVISION, KEY_SET_VALUE, AdminSid);
1277 ok(res, "AddAccessDeniedAce failed with error %d\n", GetLastError());
1280 Access = AccessStatus = 0x1abe11ed;
1281 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1282 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1283 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1284 ok(AccessStatus && (Access == KEY_READ),
1285 "AccessCheck failed to grant access with error %d\n",
1288 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
1289 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1290 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1292 "AccessCheck failed to grant any access with error %d\n",
1294 trace("AccessCheck with MAXIMUM_ALLOWED got Access 0x%08x\n", Access);
1296 /* Access denied by SD */
1297 SetLastError(0xdeadbeef);
1298 Access = AccessStatus = 0x1abe11ed;
1299 ret = AccessCheck(SecurityDescriptor, Token, KEY_WRITE, &Mapping,
1300 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1301 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1302 err = GetLastError();
1303 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
1304 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
1305 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
1308 PrivSet->PrivilegeCount = 16;
1309 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
1310 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1311 ok(ret && !AccessStatus && GetLastError() == ERROR_PRIVILEGE_NOT_HELD,
1312 "AccessCheck should have failed with ERROR_PRIVILEGE_NOT_HELD, instead of %d\n",
1315 ret = ImpersonateLoggedOnUser(Token);
1316 ok(ret, "ImpersonateLoggedOnUser failed with error %d\n", GetLastError());
1317 ret = pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, TRUE, TRUE, &Enabled);
1321 PrivSet->PrivilegeCount = 16;
1322 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
1323 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1324 ok(ret && AccessStatus && GetLastError() == 0,
1325 "AccessCheck should have succeeded, error %d\n",
1327 ok(Access == ACCESS_SYSTEM_SECURITY,
1328 "Access should be equal to ACCESS_SYSTEM_SECURITY instead of 0x%08x\n",
1332 trace("Couldn't get SE_SECURITY_PRIVILEGE (0x%08x), skipping ACCESS_SYSTEM_SECURITY test\n",
1334 ret = RevertToSelf();
1335 ok(ret, "RevertToSelf failed with error %d\n", GetLastError());
1337 /* test INHERIT_ONLY_ACE */
1338 ret = InitializeAcl(Acl, 256, ACL_REVISION);
1339 ok(ret, "InitializeAcl failed with error %d\n", GetLastError());
1341 /* NT doesn't have AddAccessAllowedAceEx. Skipping this call/test doesn't influence
1344 if (pAddAccessAllowedAceEx)
1346 ret = pAddAccessAllowedAceEx(Acl, ACL_REVISION, INHERIT_ONLY_ACE, KEY_READ, EveryoneSid);
1347 ok(ret, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
1350 win_skip("AddAccessAllowedAceEx is not available\n");
1352 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1353 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1354 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1355 err = GetLastError();
1356 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
1357 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
1358 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
1362 res = DuplicateToken(ProcessToken, SecurityAnonymous, &Token);
1363 ok(res, "DuplicateToken failed with error %d\n", GetLastError());
1365 SetLastError(0xdeadbeef);
1366 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
1367 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1368 err = GetLastError();
1369 ok(!ret && err == ERROR_BAD_IMPERSONATION_LEVEL, "AccessCheck should have failed "
1370 "with ERROR_BAD_IMPERSONATION_LEVEL, instead of %d\n", err);
1374 SetLastError(0xdeadbeef);
1375 ret = AccessCheck(SecurityDescriptor, ProcessToken, KEY_READ, &Mapping,
1376 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1377 err = GetLastError();
1378 ok(!ret && err == ERROR_NO_IMPERSONATION_TOKEN, "AccessCheck should have failed "
1379 "with ERROR_NO_IMPERSONATION_TOKEN, instead of %d\n", err);
1381 CloseHandle(ProcessToken);
1384 FreeSid(EveryoneSid);
1389 HeapFree(GetProcessHeap(), 0, Acl);
1390 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
1391 HeapFree(GetProcessHeap(), 0, PrivSet);
1394 /* test GetTokenInformation for the various attributes */
1395 static void test_token_attr(void)
1397 HANDLE Token, ImpersonationToken;
1399 TOKEN_PRIVILEGES *Privileges;
1400 TOKEN_GROUPS *Groups;
1402 TOKEN_DEFAULT_DACL *Dacl;
1406 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel;
1409 /* cygwin-like use case */
1410 SetLastError(0xdeadbeef);
1411 ret = OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED, &Token);
1412 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1414 win_skip("OpenProcessToken is not implemented\n");
1417 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1420 DWORD buf[256]; /* GetTokenInformation wants a dword-aligned buffer */
1422 ret = GetTokenInformation(Token, TokenUser,(void*)buf, Size, &Size);
1423 ok(ret, "GetTokenInformation failed with error %d\n", GetLastError());
1424 Size = sizeof(ImpersonationLevel);
1425 ret = GetTokenInformation(Token, TokenImpersonationLevel, &ImpersonationLevel, Size, &Size);
1426 GLE = GetLastError();
1427 ok(!ret && (GLE == ERROR_INVALID_PARAMETER), "GetTokenInformation(TokenImpersonationLevel) on primary token should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GLE);
1431 if(!pConvertSidToStringSidA)
1433 win_skip("ConvertSidToStringSidA is not available\n");
1437 SetLastError(0xdeadbeef);
1438 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &Token);
1439 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1442 SetLastError(0xdeadbeef);
1443 ret = GetTokenInformation(Token, TokenGroups, NULL, 0, &Size);
1444 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1445 "GetTokenInformation(TokenGroups) %s with error %d\n",
1446 ret ? "succeeded" : "failed", GetLastError());
1447 Groups = HeapAlloc(GetProcessHeap(), 0, Size);
1448 SetLastError(0xdeadbeef);
1449 ret = GetTokenInformation(Token, TokenGroups, Groups, Size, &Size);
1450 ok(ret, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
1451 ok(GetLastError() == 0xdeadbeef,
1452 "GetTokenInformation shouldn't have set last error to %d\n",
1454 trace("TokenGroups:\n");
1455 for (i = 0; i < Groups->GroupCount; i++)
1457 DWORD NameLength = 255;
1459 DWORD DomainLength = 255;
1461 SID_NAME_USE SidNameUse;
1464 ret = LookupAccountSid(NULL, Groups->Groups[i].Sid, Name, &NameLength, Domain, &DomainLength, &SidNameUse);
1467 pConvertSidToStringSidA(Groups->Groups[i].Sid, &SidString);
1468 trace("%s, %s\\%s use: %d attr: 0x%08x\n", SidString, Domain, Name, SidNameUse, Groups->Groups[i].Attributes);
1469 LocalFree(SidString);
1471 else trace("attr: 0x%08x LookupAccountSid failed with error %d\n", Groups->Groups[i].Attributes, GetLastError());
1473 HeapFree(GetProcessHeap(), 0, Groups);
1476 ret = GetTokenInformation(Token, TokenUser, NULL, 0, &Size);
1477 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1478 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1479 User = HeapAlloc(GetProcessHeap(), 0, Size);
1480 ret = GetTokenInformation(Token, TokenUser, User, Size, &Size);
1482 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1484 pConvertSidToStringSidA(User->User.Sid, &SidString);
1485 trace("TokenUser: %s attr: 0x%08x\n", SidString, User->User.Attributes);
1486 LocalFree(SidString);
1487 HeapFree(GetProcessHeap(), 0, User);
1490 ret = GetTokenInformation(Token, TokenPrivileges, NULL, 0, &Size);
1491 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1492 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1493 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
1494 ret = GetTokenInformation(Token, TokenPrivileges, Privileges, Size, &Size);
1496 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1497 trace("TokenPrivileges:\n");
1498 for (i = 0; i < Privileges->PrivilegeCount; i++)
1501 DWORD NameLen = sizeof(Name)/sizeof(Name[0]);
1502 LookupPrivilegeName(NULL, &Privileges->Privileges[i].Luid, Name, &NameLen);
1503 trace("\t%s, 0x%x\n", Name, Privileges->Privileges[i].Attributes);
1505 HeapFree(GetProcessHeap(), 0, Privileges);
1507 ret = DuplicateToken(Token, SecurityAnonymous, &ImpersonationToken);
1508 ok(ret, "DuplicateToken failed with error %d\n", GetLastError());
1510 Size = sizeof(ImpersonationLevel);
1511 ret = GetTokenInformation(ImpersonationToken, TokenImpersonationLevel, &ImpersonationLevel, Size, &Size);
1512 ok(ret, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1513 ok(ImpersonationLevel == SecurityAnonymous, "ImpersonationLevel should have been SecurityAnonymous instead of %d\n", ImpersonationLevel);
1515 CloseHandle(ImpersonationToken);
1518 ret = GetTokenInformation(Token, TokenDefaultDacl, NULL, 0, &Size);
1519 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1520 "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1522 Dacl = HeapAlloc(GetProcessHeap(), 0, Size);
1523 ret = GetTokenInformation(Token, TokenDefaultDacl, Dacl, Size, &Size);
1524 ok(ret, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1526 SetLastError(0xdeadbeef);
1527 ret = SetTokenInformation(Token, TokenDefaultDacl, NULL, 0);
1528 GLE = GetLastError();
1529 ok(!ret, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1530 ok(GLE == ERROR_BAD_LENGTH, "expected ERROR_BAD_LENGTH got %u\n", GLE);
1532 SetLastError(0xdeadbeef);
1533 ret = SetTokenInformation(Token, TokenDefaultDacl, NULL, Size);
1534 GLE = GetLastError();
1535 ok(!ret, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1536 ok(GLE == ERROR_NOACCESS, "expected ERROR_NOACCESS got %u\n", GLE);
1538 acl = Dacl->DefaultDacl;
1539 Dacl->DefaultDacl = NULL;
1541 ret = SetTokenInformation(Token, TokenDefaultDacl, Dacl, Size);
1542 ok(ret, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1545 Dacl->DefaultDacl = (ACL *)0xdeadbeef;
1546 ret = GetTokenInformation(Token, TokenDefaultDacl, Dacl, Size, &Size2);
1547 ok(ret, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1548 ok(Dacl->DefaultDacl == NULL, "expected NULL, got %p\n", Dacl->DefaultDacl);
1549 ok(Size2 == sizeof(TOKEN_DEFAULT_DACL) || broken(Size2 == 2*sizeof(TOKEN_DEFAULT_DACL)), /* WoW64 */
1550 "got %u expected sizeof(TOKEN_DEFAULT_DACL)\n", Size2);
1552 Dacl->DefaultDacl = acl;
1553 ret = SetTokenInformation(Token, TokenDefaultDacl, Dacl, Size);
1554 ok(ret, "SetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1556 if (Size2 == sizeof(TOKEN_DEFAULT_DACL)) {
1557 ret = GetTokenInformation(Token, TokenDefaultDacl, Dacl, Size, &Size2);
1558 ok(ret, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1560 win_skip("TOKEN_DEFAULT_DACL size too small on WoW64\n");
1562 HeapFree(GetProcessHeap(), 0, Dacl);
1566 typedef union _MAX_SID
1569 char max[SECURITY_MAX_SID_SIZE];
1572 static void test_sid_str(PSID * sid)
1575 BOOL ret = pConvertSidToStringSidA(sid, &str_sid);
1576 ok(ret, "ConvertSidToStringSidA() failed: %d\n", GetLastError());
1579 char account[MAX_PATH], domain[MAX_PATH];
1581 DWORD acc_size = MAX_PATH;
1582 DWORD dom_size = MAX_PATH;
1583 ret = LookupAccountSid(NULL, sid, account, &acc_size, domain, &dom_size, &use);
1584 ok(ret || (!ret && (GetLastError() == ERROR_NONE_MAPPED)),
1585 "LookupAccountSid(%s) failed: %d\n", str_sid, GetLastError());
1587 trace(" %s %s\\%s %d\n", str_sid, domain, account, use);
1588 else if (GetLastError() == ERROR_NONE_MAPPED)
1589 trace(" %s couldn't be mapped\n", str_sid);
1594 static const struct well_known_sid_value
1596 BOOL without_domain;
1597 const char *sid_string;
1598 } well_known_sid_values[] = {
1599 /* 0 */ {TRUE, "S-1-0-0"}, {TRUE, "S-1-1-0"}, {TRUE, "S-1-2-0"}, {TRUE, "S-1-3-0"},
1600 /* 4 */ {TRUE, "S-1-3-1"}, {TRUE, "S-1-3-2"}, {TRUE, "S-1-3-3"}, {TRUE, "S-1-5"},
1601 /* 8 */ {FALSE, "S-1-5-1"}, {TRUE, "S-1-5-2"}, {TRUE, "S-1-5-3"}, {TRUE, "S-1-5-4"},
1602 /* 12 */ {TRUE, "S-1-5-6"}, {TRUE, "S-1-5-7"}, {TRUE, "S-1-5-8"}, {TRUE, "S-1-5-9"},
1603 /* 16 */ {TRUE, "S-1-5-10"}, {TRUE, "S-1-5-11"}, {TRUE, "S-1-5-12"}, {TRUE, "S-1-5-13"},
1604 /* 20 */ {TRUE, "S-1-5-14"}, {FALSE, NULL}, {TRUE, "S-1-5-18"}, {TRUE, "S-1-5-19"},
1605 /* 24 */ {TRUE, "S-1-5-20"}, {TRUE, "S-1-5-32"},
1606 /* 26 */ {FALSE, "S-1-5-32-544"}, {TRUE, "S-1-5-32-545"}, {TRUE, "S-1-5-32-546"},
1607 /* 29 */ {TRUE, "S-1-5-32-547"}, {TRUE, "S-1-5-32-548"}, {TRUE, "S-1-5-32-549"},
1608 /* 32 */ {TRUE, "S-1-5-32-550"}, {TRUE, "S-1-5-32-551"}, {TRUE, "S-1-5-32-552"},
1609 /* 35 */ {TRUE, "S-1-5-32-554"}, {TRUE, "S-1-5-32-555"}, {TRUE, "S-1-5-32-556"},
1610 /* 38 */ {FALSE, "S-1-5-21-12-23-34-45-56-500"}, {FALSE, "S-1-5-21-12-23-34-45-56-501"},
1611 /* 40 */ {FALSE, "S-1-5-21-12-23-34-45-56-502"}, {FALSE, "S-1-5-21-12-23-34-45-56-512"},
1612 /* 42 */ {FALSE, "S-1-5-21-12-23-34-45-56-513"}, {FALSE, "S-1-5-21-12-23-34-45-56-514"},
1613 /* 44 */ {FALSE, "S-1-5-21-12-23-34-45-56-515"}, {FALSE, "S-1-5-21-12-23-34-45-56-516"},
1614 /* 46 */ {FALSE, "S-1-5-21-12-23-34-45-56-517"}, {FALSE, "S-1-5-21-12-23-34-45-56-518"},
1615 /* 48 */ {FALSE, "S-1-5-21-12-23-34-45-56-519"}, {FALSE, "S-1-5-21-12-23-34-45-56-520"},
1616 /* 50 */ {FALSE, "S-1-5-21-12-23-34-45-56-553"},
1617 /* Added in Windows Server 2003 */
1618 /* 51 */ {TRUE, "S-1-5-64-10"}, {TRUE, "S-1-5-64-21"}, {TRUE, "S-1-5-64-14"},
1619 /* 54 */ {TRUE, "S-1-5-15"}, {TRUE, "S-1-5-1000"}, {FALSE, "S-1-5-32-557"},
1620 /* 57 */ {TRUE, "S-1-5-32-558"}, {TRUE, "S-1-5-32-559"}, {TRUE, "S-1-5-32-560"},
1621 /* 60 */ {TRUE, "S-1-5-32-561"}, {TRUE, "S-1-5-32-562"},
1622 /* Added in Windows Vista: */
1623 /* 62 */ {TRUE, "S-1-5-32-568"},
1624 /* 63 */ {TRUE, "S-1-5-17"}, {FALSE, "S-1-5-32-569"}, {TRUE, "S-1-16-0"},
1625 /* 66 */ {TRUE, "S-1-16-4096"}, {TRUE, "S-1-16-8192"}, {TRUE, "S-1-16-12288"},
1626 /* 69 */ {TRUE, "S-1-16-16384"}, {TRUE, "S-1-5-33"}, {TRUE, "S-1-3-4"},
1627 /* 72 */ {FALSE, "S-1-5-21-12-23-34-45-56-571"}, {FALSE, "S-1-5-21-12-23-34-45-56-572"},
1628 /* 74 */ {TRUE, "S-1-5-22"}, {FALSE, "S-1-5-21-12-23-34-45-56-521"}, {TRUE, "S-1-5-32-573"}
1631 static void test_CreateWellKnownSid(void)
1633 SID_IDENTIFIER_AUTHORITY ident = { SECURITY_NT_AUTHORITY };
1634 PSID domainsid, sid;
1639 if (!pCreateWellKnownSid)
1641 win_skip("CreateWellKnownSid not available\n");
1646 SetLastError(0xdeadbeef);
1647 ret = pCreateWellKnownSid(WinInteractiveSid, NULL, NULL, &size);
1648 error = GetLastError();
1649 ok(!ret, "CreateWellKnownSid succeeded\n");
1650 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
1651 ok(size, "expected size > 0\n");
1653 SetLastError(0xdeadbeef);
1654 ret = pCreateWellKnownSid(WinInteractiveSid, NULL, NULL, &size);
1655 error = GetLastError();
1656 ok(!ret, "CreateWellKnownSid succeeded\n");
1657 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
1659 sid = HeapAlloc(GetProcessHeap(), 0, size);
1660 ret = pCreateWellKnownSid(WinInteractiveSid, NULL, sid, &size);
1661 ok(ret, "CreateWellKnownSid failed %u\n", GetLastError());
1662 HeapFree(GetProcessHeap(), 0, sid);
1664 /* a domain sid usually have three subauthorities but we test that CreateWellKnownSid doesn't check it */
1665 AllocateAndInitializeSid(&ident, 6, SECURITY_NT_NON_UNIQUE, 12, 23, 34, 45, 56, 0, 0, &domainsid);
1667 for (i = 0; i < sizeof(well_known_sid_values)/sizeof(well_known_sid_values[0]); i++)
1669 const struct well_known_sid_value *value = &well_known_sid_values[i];
1670 char sid_buffer[SECURITY_MAX_SID_SIZE];
1674 if (value->sid_string == NULL)
1677 if (i > WinAccountRasAndIasServersSid)
1679 /* These SIDs aren't implemented by all Windows versions - detect it and break the loop */
1680 cb = sizeof(sid_buffer);
1681 if (!pCreateWellKnownSid(i, domainsid, sid_buffer, &cb))
1683 skip("Well known SIDs starting from %u are not implemented\n", i);
1688 cb = sizeof(sid_buffer);
1689 ok(pCreateWellKnownSid(i, value->without_domain ? NULL : domainsid, sid_buffer, &cb), "Couldn't create well known sid %u\n", i);
1690 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer)), cb, DWORD, "%d");
1691 ok(IsValidSid(sid_buffer), "The sid is not valid\n");
1692 ok(pConvertSidToStringSidA(sid_buffer, &str), "Couldn't convert SID to string\n");
1693 ok(strcmp(str, value->sid_string) == 0, "SID mismatch - expected %s, got %s\n",
1694 value->sid_string, str);
1697 if (value->without_domain)
1699 char buf2[SECURITY_MAX_SID_SIZE];
1701 ok(pCreateWellKnownSid(i, domainsid, buf2, &cb), "Couldn't create well known sid %u with optional domain\n", i);
1702 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer)), cb, DWORD, "%d");
1703 ok(memcmp(buf2, sid_buffer, cb) == 0, "SID create with domain is different than without (%u)\n", i);
1710 static void test_LookupAccountSid(void)
1712 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
1713 CHAR accountA[MAX_PATH], domainA[MAX_PATH], usernameA[MAX_PATH];
1714 DWORD acc_sizeA, dom_sizeA, user_sizeA;
1715 DWORD real_acc_sizeA, real_dom_sizeA;
1716 WCHAR accountW[MAX_PATH], domainW[MAX_PATH];
1717 DWORD acc_sizeW, dom_sizeW;
1718 DWORD real_acc_sizeW, real_dom_sizeW;
1719 PSID pUsersSid = NULL;
1722 DWORD error, size, cbti = 0;
1727 PTOKEN_USER ptiUser = NULL;
1729 /* native windows crashes if account size, domain size, or name use is NULL */
1731 ret = AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
1732 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &pUsersSid);
1733 ok(ret || (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED),
1734 "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1736 /* not running on NT so give up */
1737 if (!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1740 real_acc_sizeA = MAX_PATH;
1741 real_dom_sizeA = MAX_PATH;
1742 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &real_acc_sizeA, domainA, &real_dom_sizeA, &use);
1743 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1745 /* try NULL account */
1746 acc_sizeA = MAX_PATH;
1747 dom_sizeA = MAX_PATH;
1748 ret = LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
1749 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1751 /* try NULL domain */
1752 acc_sizeA = MAX_PATH;
1753 dom_sizeA = MAX_PATH;
1754 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
1755 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1757 /* try a small account buffer */
1759 dom_sizeA = MAX_PATH;
1761 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1762 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1763 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1764 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1766 /* try a 0 sized account buffer */
1768 dom_sizeA = MAX_PATH;
1770 LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1771 /* this can fail or succeed depending on OS version but the size will always be returned */
1772 ok(acc_sizeA == real_acc_sizeA + 1,
1773 "LookupAccountSidA() Expected acc_size = %u, got %u\n",
1774 real_acc_sizeA + 1, acc_sizeA);
1776 /* try a 0 sized account buffer */
1778 dom_sizeA = MAX_PATH;
1779 LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
1780 /* this can fail or succeed depending on OS version but the size will always be returned */
1781 ok(acc_sizeA == real_acc_sizeA + 1,
1782 "LookupAccountSid() Expected acc_size = %u, got %u\n",
1783 real_acc_sizeA + 1, acc_sizeA);
1785 /* try a small domain buffer */
1787 acc_sizeA = MAX_PATH;
1789 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1790 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1791 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1792 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1794 /* try a 0 sized domain buffer */
1796 acc_sizeA = MAX_PATH;
1798 LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1799 /* this can fail or succeed depending on OS version but the size will always be returned */
1800 ok(dom_sizeA == real_dom_sizeA + 1,
1801 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1802 real_dom_sizeA + 1, dom_sizeA);
1804 /* try a 0 sized domain buffer */
1806 acc_sizeA = MAX_PATH;
1807 LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
1808 /* this can fail or succeed depending on OS version but the size will always be returned */
1809 ok(dom_sizeA == real_dom_sizeA + 1,
1810 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1811 real_dom_sizeA + 1, dom_sizeA);
1813 real_acc_sizeW = MAX_PATH;
1814 real_dom_sizeW = MAX_PATH;
1815 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &real_acc_sizeW, domainW, &real_dom_sizeW, &use);
1816 ok(ret, "LookupAccountSidW() Expected TRUE, got FALSE\n");
1818 /* try an invalid system name */
1819 real_acc_sizeA = MAX_PATH;
1820 real_dom_sizeA = MAX_PATH;
1821 ret = LookupAccountSidA("deepthought", pUsersSid, accountA, &real_acc_sizeA, domainA, &real_dom_sizeA, &use);
1822 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1823 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE || GetLastError() == RPC_S_INVALID_NET_ADDR /* Vista */,
1824 "LookupAccountSidA() Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %u\n", GetLastError());
1826 /* native windows crashes if domainW or accountW is NULL */
1828 /* try a small account buffer */
1830 dom_sizeW = MAX_PATH;
1832 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1833 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1834 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1835 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1837 /* try a 0 sized account buffer */
1839 dom_sizeW = MAX_PATH;
1841 LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1842 /* this can fail or succeed depending on OS version but the size will always be returned */
1843 ok(acc_sizeW == real_acc_sizeW + 1,
1844 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1845 real_acc_sizeW + 1, acc_sizeW);
1847 /* try a 0 sized account buffer */
1849 dom_sizeW = MAX_PATH;
1850 LookupAccountSidW(NULL, pUsersSid, NULL, &acc_sizeW, domainW, &dom_sizeW, &use);
1851 /* this can fail or succeed depending on OS version but the size will always be returned */
1852 ok(acc_sizeW == real_acc_sizeW + 1,
1853 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1854 real_acc_sizeW + 1, acc_sizeW);
1856 /* try a small domain buffer */
1858 acc_sizeW = MAX_PATH;
1860 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1861 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1862 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1863 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1865 /* try a 0 sized domain buffer */
1867 acc_sizeW = MAX_PATH;
1869 LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1870 /* this can fail or succeed depending on OS version but the size will always be returned */
1871 ok(dom_sizeW == real_dom_sizeW + 1,
1872 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1873 real_dom_sizeW + 1, dom_sizeW);
1875 /* try a 0 sized domain buffer */
1877 acc_sizeW = MAX_PATH;
1878 LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, NULL, &dom_sizeW, &use);
1879 /* this can fail or succeed depending on OS version but the size will always be returned */
1880 ok(dom_sizeW == real_dom_sizeW + 1,
1881 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1882 real_dom_sizeW + 1, dom_sizeW);
1884 acc_sizeW = dom_sizeW = use = 0;
1885 SetLastError(0xdeadbeef);
1886 ret = LookupAccountSidW(NULL, pUsersSid, NULL, &acc_sizeW, NULL, &dom_sizeW, &use);
1887 error = GetLastError();
1888 ok(!ret, "LookupAccountSidW failed %u\n", GetLastError());
1889 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
1890 ok(acc_sizeW, "expected non-zero account size\n");
1891 ok(dom_sizeW, "expected non-zero domain size\n");
1892 ok(!use, "expected zero use %u\n", use);
1896 /* Test LookupAccountSid with Sid retrieved from token information.
1897 This assumes this process is running under the account of the current user.*/
1898 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &hToken);
1899 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1900 ret = GetTokenInformation(hToken, TokenUser, NULL, 0, &cbti);
1901 ok(!ret, "GetTokenInformation failed with error %d\n", GetLastError());
1902 ptiUser = HeapAlloc(GetProcessHeap(), 0, cbti);
1903 if (GetTokenInformation(hToken, TokenUser, ptiUser, cbti, &cbti))
1905 acc_sizeA = dom_sizeA = MAX_PATH;
1906 ret = LookupAccountSidA(NULL, ptiUser->User.Sid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1907 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1908 user_sizeA = MAX_PATH;
1909 ret = GetUserNameA(usernameA , &user_sizeA);
1910 ok(ret, "GetUserNameA() Expected TRUE, got FALSE\n");
1911 ok(lstrcmpA(usernameA, accountA) == 0, "LookupAccountSidA() Expected account name: %s got: %s\n", usernameA, accountA );
1913 HeapFree(GetProcessHeap(), 0, ptiUser);
1915 if (pCreateWellKnownSid && pConvertSidToStringSidA)
1917 trace("Well Known SIDs:\n");
1918 for (i = 0; i <= 60; i++)
1920 size = SECURITY_MAX_SID_SIZE;
1921 if (pCreateWellKnownSid(i, NULL, &max_sid.sid, &size))
1923 if (pConvertSidToStringSidA(&max_sid.sid, &str_sidA))
1925 acc_sizeA = MAX_PATH;
1926 dom_sizeA = MAX_PATH;
1927 if (LookupAccountSidA(NULL, &max_sid.sid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use))
1928 trace(" %d: %s %s\\%s %d\n", i, str_sidA, domainA, accountA, use);
1929 LocalFree(str_sidA);
1934 if (GetLastError() != ERROR_INVALID_PARAMETER)
1935 trace(" CreateWellKnownSid(%d) failed: %d\n", i, GetLastError());
1937 trace(" %d: not supported\n", i);
1941 pLsaQueryInformationPolicy = (void *)GetProcAddress( hmod, "LsaQueryInformationPolicy");
1942 pLsaOpenPolicy = (void *)GetProcAddress( hmod, "LsaOpenPolicy");
1943 pLsaFreeMemory = (void *)GetProcAddress( hmod, "LsaFreeMemory");
1944 pLsaClose = (void *)GetProcAddress( hmod, "LsaClose");
1946 if (pLsaQueryInformationPolicy && pLsaOpenPolicy && pLsaFreeMemory && pLsaClose)
1950 LSA_OBJECT_ATTRIBUTES object_attributes;
1952 ZeroMemory(&object_attributes, sizeof(object_attributes));
1953 object_attributes.Length = sizeof(object_attributes);
1955 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_ALL_ACCESS, &handle);
1956 ok(status == STATUS_SUCCESS || status == STATUS_ACCESS_DENIED,
1957 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status);
1959 /* try a more restricted access mask if necessary */
1960 if (status == STATUS_ACCESS_DENIED) {
1961 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
1962 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_VIEW_LOCAL_INFORMATION, &handle);
1963 ok(status == STATUS_SUCCESS, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status);
1966 if (status == STATUS_SUCCESS)
1968 PPOLICY_ACCOUNT_DOMAIN_INFO info;
1969 status = pLsaQueryInformationPolicy(handle, PolicyAccountDomainInformation, (PVOID*)&info);
1970 ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy() failed, returned 0x%08x\n", status);
1971 if (status == STATUS_SUCCESS)
1973 ok(info->DomainSid!=0, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) missing SID\n");
1974 if (info->DomainSid)
1976 int count = *GetSidSubAuthorityCount(info->DomainSid);
1977 CopySid(GetSidLengthRequired(count), &max_sid, info->DomainSid);
1978 test_sid_str((PSID)&max_sid.sid);
1979 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_ADMIN;
1980 max_sid.sid.SubAuthorityCount = count + 1;
1981 test_sid_str((PSID)&max_sid.sid);
1982 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_GUEST;
1983 test_sid_str((PSID)&max_sid.sid);
1984 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ADMINS;
1985 test_sid_str((PSID)&max_sid.sid);
1986 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_USERS;
1987 test_sid_str((PSID)&max_sid.sid);
1988 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_GUESTS;
1989 test_sid_str((PSID)&max_sid.sid);
1990 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_COMPUTERS;
1991 test_sid_str((PSID)&max_sid.sid);
1992 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CONTROLLERS;
1993 test_sid_str((PSID)&max_sid.sid);
1994 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CERT_ADMINS;
1995 test_sid_str((PSID)&max_sid.sid);
1996 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_SCHEMA_ADMINS;
1997 test_sid_str((PSID)&max_sid.sid);
1998 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ENTERPRISE_ADMINS;
1999 test_sid_str((PSID)&max_sid.sid);
2000 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_POLICY_ADMINS;
2001 test_sid_str((PSID)&max_sid.sid);
2002 max_sid.sid.SubAuthority[count] = DOMAIN_ALIAS_RID_RAS_SERVERS;
2003 test_sid_str((PSID)&max_sid.sid);
2004 max_sid.sid.SubAuthority[count] = 1000; /* first user account */
2005 test_sid_str((PSID)&max_sid.sid);
2008 pLsaFreeMemory((LPVOID)info);
2011 status = pLsaClose(handle);
2012 ok(status == STATUS_SUCCESS, "LsaClose() failed, returned 0x%08x\n", status);
2018 static BOOL get_sid_info(PSID psid, LPSTR *user, LPSTR *dom)
2020 static CHAR account[UNLEN + 1];
2021 static CHAR domain[UNLEN + 1];
2022 DWORD size, dom_size;
2028 size = dom_size = UNLEN + 1;
2031 SetLastError(0xdeadbeef);
2032 return LookupAccountSidA(NULL, psid, account, &size, domain, &dom_size, &use);
2035 static void check_wellknown_name(const char* name, WELL_KNOWN_SID_TYPE result)
2037 SID_IDENTIFIER_AUTHORITY ident = { SECURITY_NT_AUTHORITY };
2038 PSID domainsid = NULL;
2039 char wk_sid[SECURITY_MAX_SID_SIZE];
2042 DWORD sid_size, domain_size;
2043 SID_NAME_USE sid_use;
2044 LPSTR domain, account, sid_domain, wk_domain, wk_account;
2050 ret = LookupAccountNameA(NULL, name, NULL, &sid_size, NULL, &domain_size, &sid_use);
2051 ok(!ret, " %s Should have failed to lookup account name\n", name);
2052 psid = HeapAlloc(GetProcessHeap(),0,sid_size);
2053 domain = HeapAlloc(GetProcessHeap(),0,domain_size);
2054 ret = LookupAccountNameA(NULL, name, psid, &sid_size, domain, &domain_size, &sid_use);
2058 ok(!ret, " %s Should have failed to lookup account name\n",name);
2062 AllocateAndInitializeSid(&ident, 6, SECURITY_NT_NON_UNIQUE, 12, 23, 34, 45, 56, 0, 0, &domainsid);
2063 cb = sizeof(wk_sid);
2064 if (!pCreateWellKnownSid(result, domainsid, wk_sid, &cb))
2066 win_skip("SID %i is not available on the system\n",result);
2070 ret2 = get_sid_info(wk_sid, &wk_account, &wk_domain);
2071 if (!ret2 && GetLastError() == ERROR_NONE_MAPPED)
2073 win_skip("CreateWellKnownSid() succeeded but the account '%s' is not present (W2K)\n", name);
2077 get_sid_info(psid, &account, &sid_domain);
2079 ok(ret, "Failed to lookup account name %s\n",name);
2080 ok(sid_size != 0, "sid_size was zero\n");
2082 ok(EqualSid(psid,wk_sid),"(%s) Sids fail to match well known sid!\n",name);
2084 ok(!lstrcmp(account, wk_account), "Expected %s , got %s\n", account, wk_account);
2085 ok(!lstrcmp(domain, wk_domain), "Expected %s, got %s\n", wk_domain, domain);
2086 ok(sid_use == SidTypeWellKnownGroup , "Expected Use (5), got %d\n", sid_use);
2090 HeapFree(GetProcessHeap(),0,psid);
2091 HeapFree(GetProcessHeap(),0,domain);
2094 static void test_LookupAccountName(void)
2096 DWORD sid_size, domain_size, user_size;
2097 DWORD sid_save, domain_save;
2098 CHAR user_name[UNLEN + 1];
2099 CHAR computer_name[UNLEN + 1];
2100 SID_NAME_USE sid_use;
2101 LPSTR domain, account, sid_dom;
2105 /* native crashes if (assuming all other parameters correct):
2107 * - Sid is NULL and cbSid is > 0
2108 * - cbSid or cchReferencedDomainName are NULL
2109 * - ReferencedDomainName is NULL and cchReferencedDomainName is the correct size
2112 user_size = UNLEN + 1;
2113 SetLastError(0xdeadbeef);
2114 ret = GetUserNameA(user_name, &user_size);
2115 ok(ret, "Failed to get user name : %d\n", GetLastError());
2120 sid_use = 0xcafebabe;
2121 SetLastError(0xdeadbeef);
2122 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, NULL, &domain_size, &sid_use);
2123 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2125 win_skip("LookupAccountNameA is not implemented\n");
2128 ok(!ret, "Expected 0, got %d\n", ret);
2129 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2130 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2131 ok(sid_size != 0, "Expected non-zero sid size\n");
2132 ok(domain_size != 0, "Expected non-zero domain size\n");
2133 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
2135 sid_save = sid_size;
2136 domain_save = domain_size;
2138 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
2139 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
2141 /* try valid account name */
2142 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, domain, &domain_size, &sid_use);
2143 get_sid_info(psid, &account, &sid_dom);
2144 ok(ret, "Failed to lookup account name\n");
2145 ok(sid_size == GetLengthSid(psid), "Expected %d, got %d\n", GetLengthSid(psid), sid_size);
2146 ok(!lstrcmp(account, user_name), "Expected %s, got %s\n", user_name, account);
2147 ok(!lstrcmp(domain, sid_dom), "Expected %s, got %s\n", sid_dom, domain);
2148 ok(domain_size == domain_save - 1, "Expected %d, got %d\n", domain_save - 1, domain_size);
2149 ok(strlen(domain) == domain_size, "Expected %d, got %d\n", lstrlen(domain), domain_size);
2150 ok(sid_use == SidTypeUser, "Expected SidTypeUser (%d), got %d\n", SidTypeUser, sid_use);
2151 domain_size = domain_save;
2152 sid_size = sid_save;
2154 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH)
2156 skip("Non-english locale (test with hardcoded 'Everyone')\n");
2160 ret = LookupAccountNameA(NULL, "Everyone", psid, &sid_size, domain, &domain_size, &sid_use);
2161 get_sid_info(psid, &account, &sid_dom);
2162 ok(ret, "Failed to lookup account name\n");
2163 ok(sid_size != 0, "sid_size was zero\n");
2164 ok(!lstrcmp(account, "Everyone"), "Expected Everyone, got %s\n", account);
2165 ok(!lstrcmp(domain, sid_dom), "Expected %s, got %s\n", sid_dom, domain);
2166 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
2167 ok(strlen(domain) == domain_size, "Expected %d, got %d\n", lstrlen(domain), domain_size);
2168 ok(sid_use == SidTypeWellKnownGroup, "Expected SidTypeWellKnownGroup (%d), got %d\n", SidTypeWellKnownGroup, sid_use);
2169 domain_size = domain_save;
2172 /* NULL Sid with zero sid size */
2173 SetLastError(0xdeadbeef);
2175 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
2176 ok(!ret, "Expected 0, got %d\n", ret);
2177 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2178 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2179 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
2180 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
2182 /* try cchReferencedDomainName - 1 */
2183 SetLastError(0xdeadbeef);
2185 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
2186 ok(!ret, "Expected 0, got %d\n", ret);
2187 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2188 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2189 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
2190 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
2192 /* NULL ReferencedDomainName with zero domain name size */
2193 SetLastError(0xdeadbeef);
2195 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, NULL, &domain_size, &sid_use);
2196 ok(!ret, "Expected 0, got %d\n", ret);
2197 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2198 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2199 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
2200 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
2202 HeapFree(GetProcessHeap(), 0, psid);
2203 HeapFree(GetProcessHeap(), 0, domain);
2205 /* get sizes for NULL account name */
2208 sid_use = 0xcafebabe;
2209 SetLastError(0xdeadbeef);
2210 ret = LookupAccountNameA(NULL, NULL, NULL, &sid_size, NULL, &domain_size, &sid_use);
2211 if (!ret && GetLastError() == ERROR_NONE_MAPPED)
2212 win_skip("NULL account name doesn't work on NT4\n");
2215 ok(!ret, "Expected 0, got %d\n", ret);
2216 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2217 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2218 ok(sid_size != 0, "Expected non-zero sid size\n");
2219 ok(domain_size != 0, "Expected non-zero domain size\n");
2220 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
2222 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
2223 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
2225 /* try NULL account name */
2226 ret = LookupAccountNameA(NULL, NULL, psid, &sid_size, domain, &domain_size, &sid_use);
2227 get_sid_info(psid, &account, &sid_dom);
2228 ok(ret, "Failed to lookup account name\n");
2229 /* Using a fixed string will not work on different locales */
2230 ok(!lstrcmp(account, domain),
2231 "Got %s for account and %s for domain, these should be the same\n",
2233 ok(sid_use == SidTypeDomain, "Expected SidTypeDomain (%d), got %d\n", SidTypeDomain, sid_use);
2235 HeapFree(GetProcessHeap(), 0, psid);
2236 HeapFree(GetProcessHeap(), 0, domain);
2239 /* try an invalid account name */
2240 SetLastError(0xdeadbeef);
2243 ret = LookupAccountNameA(NULL, "oogabooga", NULL, &sid_size, NULL, &domain_size, &sid_use);
2244 ok(!ret, "Expected 0, got %d\n", ret);
2245 ok(GetLastError() == ERROR_NONE_MAPPED ||
2246 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE),
2247 "Expected ERROR_NONE_MAPPED, got %d\n", GetLastError());
2248 ok(sid_size == 0, "Expected 0, got %d\n", sid_size);
2249 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
2251 /* try an invalid system name */
2252 SetLastError(0xdeadbeef);
2255 ret = LookupAccountNameA("deepthought", NULL, NULL, &sid_size, NULL, &domain_size, &sid_use);
2256 ok(!ret, "Expected 0, got %d\n", ret);
2257 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE || GetLastError() == RPC_S_INVALID_NET_ADDR /* Vista */,
2258 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
2259 ok(sid_size == 0, "Expected 0, got %d\n", sid_size);
2260 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
2262 /* try with the computer name as the account name */
2263 domain_size = sizeof(computer_name);
2264 GetComputerNameA(computer_name, &domain_size);
2267 ret = LookupAccountNameA(NULL, computer_name, NULL, &sid_size, NULL, &domain_size, &sid_use);
2268 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER ||
2269 GetLastError() == ERROR_NONE_MAPPED /* in a domain */ ||
2270 broken(GetLastError() == ERROR_TRUSTED_DOMAIN_FAILURE) ||
2271 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE)),
2272 "LookupAccountNameA failed: %d\n", GetLastError());
2273 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
2275 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
2276 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
2277 ret = LookupAccountNameA(NULL, computer_name, psid, &sid_size, domain, &domain_size, &sid_use);
2278 ok(ret, "LookupAccountNameA failed: %d\n", GetLastError());
2279 ok(sid_use == SidTypeDomain ||
2280 (sid_use == SidTypeUser && ! strcmp(computer_name, user_name)), "expected SidTypeDomain for %s, got %d\n", computer_name, sid_use);
2281 HeapFree(GetProcessHeap(), 0, domain);
2282 HeapFree(GetProcessHeap(), 0, psid);
2285 /* Well Known names */
2286 if (!pCreateWellKnownSid)
2288 win_skip("CreateWellKnownSid not available\n");
2292 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH)
2294 skip("Non-english locale (skipping well known name creation tests)\n");
2298 check_wellknown_name("LocalService", WinLocalServiceSid);
2299 check_wellknown_name("Local Service", WinLocalServiceSid);
2301 check_wellknown_name("Local Service", 0);
2302 check_wellknown_name("NetworkService", WinNetworkServiceSid);
2303 check_wellknown_name("Network Service", WinNetworkServiceSid);
2305 /* example of some names where the spaces are not optional */
2306 check_wellknown_name("Terminal Server User", WinTerminalServerSid);
2307 check_wellknown_name("TerminalServer User", 0);
2308 check_wellknown_name("TerminalServerUser", 0);
2309 check_wellknown_name("Terminal ServerUser", 0);
2311 check_wellknown_name("enterprise domain controllers",WinEnterpriseControllersSid);
2312 check_wellknown_name("enterprisedomain controllers", 0);
2313 check_wellknown_name("enterprise domaincontrollers", 0);
2314 check_wellknown_name("enterprisedomaincontrollers", 0);
2316 /* case insensitivity */
2317 check_wellknown_name("lOCAlServICE", WinLocalServiceSid);
2319 /* fully qualified account names */
2320 check_wellknown_name("NT AUTHORITY\\LocalService", WinLocalServiceSid);
2321 check_wellknown_name("nt authority\\Network Service", WinNetworkServiceSid);
2322 check_wellknown_name("nt authority test\\Network Service", 0);
2323 check_wellknown_name("Dummy\\Network Service", 0);
2324 check_wellknown_name("ntauthority\\Network Service", 0);
2327 static void test_security_descriptor(void)
2329 SECURITY_DESCRIPTOR sd;
2332 BOOL isDefault, isPresent, ret;
2336 SetLastError(0xdeadbeef);
2337 ret = InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
2338 if (ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2340 win_skip("InitializeSecurityDescriptor is not implemented\n");
2344 ok(GetSecurityDescriptorOwner(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
2345 expect_eq(psid, NULL, PSID, "%p");
2346 expect_eq(isDefault, FALSE, BOOL, "%d");
2347 sd.Control |= SE_DACL_PRESENT | SE_SACL_PRESENT;
2349 SetLastError(0xdeadbeef);
2351 expect_eq(MakeSelfRelativeSD(&sd, buf, &size), FALSE, BOOL, "%d");
2352 expect_eq(GetLastError(), ERROR_INSUFFICIENT_BUFFER, DWORD, "%u");
2353 ok(size > 5, "Size not increased\n");
2356 expect_eq(MakeSelfRelativeSD(&sd, buf, &size), TRUE, BOOL, "%d");
2357 ok(GetSecurityDescriptorOwner(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
2358 expect_eq(psid, NULL, PSID, "%p");
2359 expect_eq(isDefault, FALSE, BOOL, "%d");
2360 ok(GetSecurityDescriptorGroup(&sd, &psid, &isDefault), "GetSecurityDescriptorGroup failed\n");
2361 expect_eq(psid, NULL, PSID, "%p");
2362 expect_eq(isDefault, FALSE, BOOL, "%d");
2363 ok(GetSecurityDescriptorDacl(&sd, &isPresent, &pacl, &isDefault), "GetSecurityDescriptorDacl failed\n");
2364 expect_eq(isPresent, TRUE, BOOL, "%d");
2365 expect_eq(psid, NULL, PSID, "%p");
2366 expect_eq(isDefault, FALSE, BOOL, "%d");
2367 ok(GetSecurityDescriptorSacl(&sd, &isPresent, &pacl, &isDefault), "GetSecurityDescriptorSacl failed\n");
2368 expect_eq(isPresent, TRUE, BOOL, "%d");
2369 expect_eq(psid, NULL, PSID, "%p");
2370 expect_eq(isDefault, FALSE, BOOL, "%d");
2374 #define TEST_GRANTED_ACCESS(a,b) test_granted_access(a,b,0,__LINE__)
2375 #define TEST_GRANTED_ACCESS2(a,b,c) test_granted_access(a,b,c,__LINE__)
2376 static void test_granted_access(HANDLE handle, ACCESS_MASK access,
2377 ACCESS_MASK alt, int line)
2379 OBJECT_BASIC_INFORMATION obj_info;
2382 if (!pNtQueryObject)
2384 skip_(__FILE__, line)("Not NT platform - skipping tests\n");
2388 status = pNtQueryObject( handle, ObjectBasicInformation, &obj_info,
2389 sizeof(obj_info), NULL );
2390 ok_(__FILE__, line)(!status, "NtQueryObject with err: %08x\n", status);
2392 ok_(__FILE__, line)(obj_info.GrantedAccess == access ||
2393 obj_info.GrantedAccess == alt, "Granted access should be 0x%08x "
2394 "or 0x%08x, instead of 0x%08x\n", access, alt, obj_info.GrantedAccess);
2396 ok_(__FILE__, line)(obj_info.GrantedAccess == access, "Granted access should "
2397 "be 0x%08x, instead of 0x%08x\n", access, obj_info.GrantedAccess);
2400 #define CHECK_SET_SECURITY(o,i,e) \
2404 SetLastError( 0xdeadbeef ); \
2405 res = SetKernelObjectSecurity( o, i, SecurityDescriptor ); \
2406 err = GetLastError(); \
2407 if (e == ERROR_SUCCESS) \
2408 ok(res, "SetKernelObjectSecurity failed with %d\n", err); \
2410 ok(!res && err == e, "SetKernelObjectSecurity should have failed " \
2411 "with %s, instead of %d\n", #e, err); \
2414 static void test_process_security(void)
2418 PTOKEN_PRIMARY_GROUP group;
2419 PSID AdminSid = NULL, UsersSid = NULL;
2421 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL;
2422 char buffer[MAX_PATH];
2423 PROCESS_INFORMATION info;
2424 STARTUPINFOA startup;
2425 SECURITY_ATTRIBUTES psa;
2426 HANDLE token, event;
2429 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
2430 res = InitializeAcl(Acl, 256, ACL_REVISION);
2431 if (!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2433 win_skip("ACLs not implemented - skipping tests\n");
2434 HeapFree(GetProcessHeap(), 0, Acl);
2437 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
2439 /* get owner from the token we might be running as a user not admin */
2440 res = OpenProcessToken( GetCurrentProcess(), MAXIMUM_ALLOWED, &token );
2441 ok(res, "OpenProcessToken failed with error %d\n", GetLastError());
2444 HeapFree(GetProcessHeap(), 0, Acl);
2448 res = GetTokenInformation( token, TokenOwner, NULL, 0, &size );
2449 ok(!res, "Expected failure, got %d\n", res);
2450 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2451 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2453 owner = HeapAlloc(GetProcessHeap(), 0, size);
2454 res = GetTokenInformation( token, TokenOwner, owner, size, &size );
2455 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
2456 AdminSid = ((TOKEN_OWNER*)owner)->Owner;
2458 res = GetTokenInformation( token, TokenPrimaryGroup, NULL, 0, &size );
2459 ok(!res, "Expected failure, got %d\n", res);
2460 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2461 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2463 group = HeapAlloc(GetProcessHeap(), 0, size);
2464 res = GetTokenInformation( token, TokenPrimaryGroup, group, size, &size );
2465 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
2466 UsersSid = ((TOKEN_PRIMARY_GROUP*)group)->PrimaryGroup;
2468 CloseHandle( token );
2471 HeapFree(GetProcessHeap(), 0, group);
2472 HeapFree(GetProcessHeap(), 0, owner);
2473 HeapFree(GetProcessHeap(), 0, Acl);
2477 res = AddAccessDeniedAce(Acl, ACL_REVISION, PROCESS_VM_READ, AdminSid);
2478 ok(res, "AddAccessDeniedAce failed with error %d\n", GetLastError());
2479 res = AddAccessAllowedAce(Acl, ACL_REVISION, PROCESS_ALL_ACCESS, AdminSid);
2480 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2482 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
2483 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
2484 ok(res, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
2486 event = CreateEvent( NULL, TRUE, TRUE, "test_event" );
2487 ok(event != NULL, "CreateEvent %d\n", GetLastError());
2489 SecurityDescriptor->Revision = 0;
2490 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_UNKNOWN_REVISION );
2491 SecurityDescriptor->Revision = SECURITY_DESCRIPTOR_REVISION;
2493 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_INVALID_SECURITY_DESCR );
2494 CHECK_SET_SECURITY( event, GROUP_SECURITY_INFORMATION, ERROR_INVALID_SECURITY_DESCR );
2495 CHECK_SET_SECURITY( event, SACL_SECURITY_INFORMATION, ERROR_ACCESS_DENIED );
2496 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
2497 /* NULL DACL is valid and means default DACL from token */
2498 SecurityDescriptor->Control |= SE_DACL_PRESENT;
2499 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
2501 /* Set owner and group and dacl */
2502 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
2503 ok(res, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
2504 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_SUCCESS );
2505 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, FALSE);
2506 ok(res, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2507 CHECK_SET_SECURITY( event, GROUP_SECURITY_INFORMATION, ERROR_SUCCESS );
2508 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
2509 ok(res, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
2510 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
2512 sprintf(buffer, "%s tests/security.c test", myARGV[0]);
2513 memset(&startup, 0, sizeof(startup));
2514 startup.cb = sizeof(startup);
2515 startup.dwFlags = STARTF_USESHOWWINDOW;
2516 startup.wShowWindow = SW_SHOWNORMAL;
2518 psa.nLength = sizeof(psa);
2519 psa.lpSecurityDescriptor = SecurityDescriptor;
2520 psa.bInheritHandle = TRUE;
2522 /* Doesn't matter what ACL say we should get full access for ourselves */
2523 res = CreateProcessA( NULL, buffer, &psa, NULL, FALSE, 0, NULL, NULL, &startup, &info );
2524 ok(res, "CreateProcess with err:%d\n", GetLastError());
2525 TEST_GRANTED_ACCESS2( info.hProcess, PROCESS_ALL_ACCESS,
2526 STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL );
2527 winetest_wait_child_process( info.hProcess );
2529 CloseHandle( info.hProcess );
2530 CloseHandle( info.hThread );
2531 CloseHandle( event );
2532 HeapFree(GetProcessHeap(), 0, group);
2533 HeapFree(GetProcessHeap(), 0, owner);
2534 HeapFree(GetProcessHeap(), 0, Acl);
2535 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
2538 static void test_process_security_child(void)
2540 HANDLE handle, handle1;
2544 handle = OpenProcess( PROCESS_TERMINATE, FALSE, GetCurrentProcessId() );
2545 ok(handle != NULL, "OpenProcess(PROCESS_TERMINATE) with err:%d\n", GetLastError());
2546 TEST_GRANTED_ACCESS( handle, PROCESS_TERMINATE );
2548 ret = DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
2549 &handle1, 0, TRUE, DUPLICATE_SAME_ACCESS );
2550 ok(ret, "duplicating handle err:%d\n", GetLastError());
2551 TEST_GRANTED_ACCESS( handle1, PROCESS_TERMINATE );
2553 CloseHandle( handle1 );
2555 SetLastError( 0xdeadbeef );
2556 ret = DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
2557 &handle1, PROCESS_ALL_ACCESS, TRUE, 0 );
2558 err = GetLastError();
2560 ok(!ret && err == ERROR_ACCESS_DENIED, "duplicating handle should have failed "
2561 "with STATUS_ACCESS_DENIED, instead of err:%d\n", err);
2563 CloseHandle( handle );
2565 /* These two should fail - they are denied by ACL */
2566 handle = OpenProcess( PROCESS_VM_READ, FALSE, GetCurrentProcessId() );
2568 ok(handle == NULL, "OpenProcess(PROCESS_VM_READ) should have failed\n");
2569 handle = OpenProcess( PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId() );
2571 ok(handle == NULL, "OpenProcess(PROCESS_ALL_ACCESS) should have failed\n");
2573 /* Documented privilege elevation */
2574 ret = DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2575 &handle, 0, TRUE, DUPLICATE_SAME_ACCESS );
2576 ok(ret, "duplicating handle err:%d\n", GetLastError());
2577 TEST_GRANTED_ACCESS2( handle, PROCESS_ALL_ACCESS,
2578 STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL );
2580 CloseHandle( handle );
2582 /* Same only explicitly asking for all access rights */
2583 ret = DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2584 &handle, PROCESS_ALL_ACCESS, TRUE, 0 );
2585 ok(ret, "duplicating handle err:%d\n", GetLastError());
2586 TEST_GRANTED_ACCESS2( handle, PROCESS_ALL_ACCESS,
2587 PROCESS_ALL_ACCESS | PROCESS_QUERY_LIMITED_INFORMATION );
2588 ret = DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
2589 &handle1, PROCESS_VM_READ, TRUE, 0 );
2590 ok(ret, "duplicating handle err:%d\n", GetLastError());
2591 TEST_GRANTED_ACCESS( handle1, PROCESS_VM_READ );
2592 CloseHandle( handle1 );
2593 CloseHandle( handle );
2596 static void test_impersonation_level(void)
2598 HANDLE Token, ProcessToken;
2601 TOKEN_PRIVILEGES *Privileges;
2603 PRIVILEGE_SET *PrivilegeSet;
2609 pDuplicateTokenEx = (void *)GetProcAddress(hmod, "DuplicateTokenEx");
2610 if( !pDuplicateTokenEx ) {
2611 win_skip("DuplicateTokenEx is not available\n");
2614 SetLastError(0xdeadbeef);
2615 ret = ImpersonateSelf(SecurityAnonymous);
2616 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2618 win_skip("ImpersonateSelf is not implemented\n");
2621 ok(ret, "ImpersonateSelf(SecurityAnonymous) failed with error %d\n", GetLastError());
2622 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
2623 ok(!ret, "OpenThreadToken should have failed\n");
2624 error = GetLastError();
2625 ok(error == ERROR_CANT_OPEN_ANONYMOUS, "OpenThreadToken on anonymous token should have returned ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error);
2626 /* can't perform access check when opening object against an anonymous impersonation token */
2628 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
2629 ok(error == ERROR_INVALID_HANDLE || error == ERROR_CANT_OPEN_ANONYMOUS,
2630 "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE or ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error);
2634 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, &ProcessToken);
2635 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
2637 ret = pDuplicateTokenEx(ProcessToken,
2638 TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_IMPERSONATE, NULL,
2639 SecurityAnonymous, TokenImpersonation, &Token);
2640 ok(ret, "DuplicateTokenEx failed with error %d\n", GetLastError());
2641 /* can't increase the impersonation level */
2642 ret = DuplicateToken(Token, SecurityIdentification, &Token2);
2643 error = GetLastError();
2644 ok(!ret && error == ERROR_BAD_IMPERSONATION_LEVEL,
2645 "Duplicating a token and increasing the impersonation level should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
2646 /* we can query anything from an anonymous token, including the user */
2647 ret = GetTokenInformation(Token, TokenUser, NULL, 0, &Size);
2648 error = GetLastError();
2649 ok(!ret && error == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenUser) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error);
2650 User = HeapAlloc(GetProcessHeap(), 0, Size);
2651 ret = GetTokenInformation(Token, TokenUser, User, Size, &Size);
2652 ok(ret, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
2653 HeapFree(GetProcessHeap(), 0, User);
2655 /* PrivilegeCheck fails with SecurityAnonymous level */
2656 ret = GetTokenInformation(Token, TokenPrivileges, NULL, 0, &Size);
2657 error = GetLastError();
2658 ok(!ret && error == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenPrivileges) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error);
2659 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
2660 ret = GetTokenInformation(Token, TokenPrivileges, Privileges, Size, &Size);
2661 ok(ret, "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
2663 PrivilegeSet = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PRIVILEGE_SET, Privilege[Privileges->PrivilegeCount]));
2664 PrivilegeSet->PrivilegeCount = Privileges->PrivilegeCount;
2665 memcpy(PrivilegeSet->Privilege, Privileges->Privileges, PrivilegeSet->PrivilegeCount * sizeof(PrivilegeSet->Privilege[0]));
2666 PrivilegeSet->Control = PRIVILEGE_SET_ALL_NECESSARY;
2667 HeapFree(GetProcessHeap(), 0, Privileges);
2669 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2670 error = GetLastError();
2671 ok(!ret && error == ERROR_BAD_IMPERSONATION_LEVEL, "PrivilegeCheck for SecurityAnonymous token should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
2675 ret = ImpersonateSelf(SecurityIdentification);
2676 ok(ret, "ImpersonateSelf(SecurityIdentification) failed with error %d\n", GetLastError());
2677 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
2678 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
2680 /* can't perform access check when opening object against an identification impersonation token */
2681 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
2683 ok(error == ERROR_INVALID_HANDLE || error == ERROR_BAD_IMPERSONATION_LEVEL,
2684 "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE or ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
2686 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2687 ok(ret, "PrivilegeCheck for SecurityIdentification failed with error %d\n", GetLastError());
2691 ret = ImpersonateSelf(SecurityImpersonation);
2692 ok(ret, "ImpersonateSelf(SecurityImpersonation) failed with error %d\n", GetLastError());
2693 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
2694 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
2695 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
2696 ok(error == ERROR_SUCCESS, "RegOpenKeyEx should have succeeded instead of failing with %d\n", error);
2698 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2699 ok(ret, "PrivilegeCheck for SecurityImpersonation failed with error %d\n", GetLastError());
2703 CloseHandle(ProcessToken);
2705 HeapFree(GetProcessHeap(), 0, PrivilegeSet);
2708 static void test_SetEntriesInAclW(void)
2711 PSID EveryoneSid = NULL, UsersSid = NULL;
2712 PACL OldAcl = NULL, NewAcl;
2713 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
2714 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
2715 EXPLICIT_ACCESSW ExplicitAccess;
2716 static const WCHAR wszEveryone[] = {'E','v','e','r','y','o','n','e',0};
2717 static const WCHAR wszCurrentUser[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
2719 if (!pSetEntriesInAclW)
2721 win_skip("SetEntriesInAclW is not available\n");
2725 NewAcl = (PACL)0xdeadbeef;
2726 res = pSetEntriesInAclW(0, NULL, NULL, &NewAcl);
2727 if(res == ERROR_CALL_NOT_IMPLEMENTED)
2729 win_skip("SetEntriesInAclW is not implemented\n");
2732 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2733 ok(NewAcl == NULL ||
2734 broken(NewAcl != NULL), /* NT4 */
2735 "NewAcl=%p, expected NULL\n", NewAcl);
2738 OldAcl = HeapAlloc(GetProcessHeap(), 0, 256);
2739 res = InitializeAcl(OldAcl, 256, ACL_REVISION);
2740 if(!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2742 win_skip("ACLs not implemented - skipping tests\n");
2743 HeapFree(GetProcessHeap(), 0, OldAcl);
2746 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
2748 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
2749 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2751 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
2752 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &UsersSid);
2753 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2755 res = AddAccessAllowedAce(OldAcl, ACL_REVISION, KEY_READ, UsersSid);
2756 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2758 ExplicitAccess.grfAccessPermissions = KEY_WRITE;
2759 ExplicitAccess.grfAccessMode = GRANT_ACCESS;
2760 ExplicitAccess.grfInheritance = NO_INHERITANCE;
2761 ExplicitAccess.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
2762 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
2763 ExplicitAccess.Trustee.ptstrName = EveryoneSid;
2764 ExplicitAccess.Trustee.MultipleTrusteeOperation = 0xDEADBEEF;
2765 ExplicitAccess.Trustee.pMultipleTrustee = (PVOID)0xDEADBEEF;
2766 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2767 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2768 ok(NewAcl != NULL, "returned acl was NULL\n");
2771 ExplicitAccess.Trustee.TrusteeType = TRUSTEE_IS_UNKNOWN;
2772 ExplicitAccess.Trustee.pMultipleTrustee = NULL;
2773 ExplicitAccess.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2774 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2775 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2776 ok(NewAcl != NULL, "returned acl was NULL\n");
2779 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH)
2781 skip("Non-english locale (test with hardcoded 'Everyone')\n");
2785 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
2786 ExplicitAccess.Trustee.ptstrName = (LPWSTR)wszEveryone;
2787 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2788 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2789 ok(NewAcl != NULL, "returned acl was NULL\n");
2792 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_BAD_FORM;
2793 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2794 ok(res == ERROR_INVALID_PARAMETER ||
2795 broken(res == ERROR_NOT_SUPPORTED), /* NT4 */
2796 "SetEntriesInAclW failed: %u\n", res);
2797 ok(NewAcl == NULL ||
2798 broken(NewAcl != NULL), /* NT4 */
2799 "returned acl wasn't NULL: %p\n", NewAcl);
2801 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
2802 ExplicitAccess.Trustee.MultipleTrusteeOperation = TRUSTEE_IS_IMPERSONATE;
2803 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2804 ok(res == ERROR_INVALID_PARAMETER ||
2805 broken(res == ERROR_NOT_SUPPORTED), /* NT4 */
2806 "SetEntriesInAclW failed: %u\n", res);
2807 ok(NewAcl == NULL ||
2808 broken(NewAcl != NULL), /* NT4 */
2809 "returned acl wasn't NULL: %p\n", NewAcl);
2811 ExplicitAccess.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2812 ExplicitAccess.grfAccessMode = SET_ACCESS;
2813 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2814 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2815 ok(NewAcl != NULL, "returned acl was NULL\n");
2819 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
2820 ExplicitAccess.Trustee.ptstrName = (LPWSTR)wszCurrentUser;
2821 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2822 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2823 ok(NewAcl != NULL, "returned acl was NULL\n");
2826 ExplicitAccess.grfAccessMode = REVOKE_ACCESS;
2827 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
2828 ExplicitAccess.Trustee.ptstrName = UsersSid;
2829 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2830 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2831 ok(NewAcl != NULL, "returned acl was NULL\n");
2835 FreeSid(EveryoneSid);
2836 HeapFree(GetProcessHeap(), 0, OldAcl);
2839 static void test_SetEntriesInAclA(void)
2842 PSID EveryoneSid = NULL, UsersSid = NULL;
2843 PACL OldAcl = NULL, NewAcl;
2844 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
2845 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
2846 EXPLICIT_ACCESS ExplicitAccess;
2847 static const CHAR szEveryone[] = {'E','v','e','r','y','o','n','e',0};
2848 static const CHAR szCurrentUser[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
2850 if (!pSetEntriesInAclA)
2852 win_skip("SetEntriesInAclA is not available\n");
2856 NewAcl = (PACL)0xdeadbeef;
2857 res = pSetEntriesInAclA(0, NULL, NULL, &NewAcl);
2858 if(res == ERROR_CALL_NOT_IMPLEMENTED)
2860 win_skip("SetEntriesInAclA is not implemented\n");
2863 ok(res == ERROR_SUCCESS, "SetEntriesInAclA failed: %u\n", res);
2864 ok(NewAcl == NULL ||
2865 broken(NewAcl != NULL), /* NT4 */
2866 "NewAcl=%p, expected NULL\n", NewAcl);
2869 OldAcl = HeapAlloc(GetProcessHeap(), 0, 256);
2870 res = InitializeAcl(OldAcl, 256, ACL_REVISION);
2871 if(!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2873 win_skip("ACLs not implemented - skipping tests\n");
2874 HeapFree(GetProcessHeap(), 0, OldAcl);
2877 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
2879 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
2880 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2882 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
2883 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &UsersSid);
2884 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2886 res = AddAccessAllowedAce(OldAcl, ACL_REVISION, KEY_READ, UsersSid);
2887 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2889 ExplicitAccess.grfAccessPermissions = KEY_WRITE;
2890 ExplicitAccess.grfAccessMode = GRANT_ACCESS;
2891 ExplicitAccess.grfInheritance = NO_INHERITANCE;
2892 ExplicitAccess.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
2893 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
2894 ExplicitAccess.Trustee.ptstrName = EveryoneSid;
2895 ExplicitAccess.Trustee.MultipleTrusteeOperation = 0xDEADBEEF;
2896 ExplicitAccess.Trustee.pMultipleTrustee = (PVOID)0xDEADBEEF;
2897 res = pSetEntriesInAclA(1, &ExplicitAccess, OldAcl, &NewAcl);
2898 ok(res == ERROR_SUCCESS, "SetEntriesInAclA failed: %u\n", res);
2899 ok(NewAcl != NULL, "returned acl was NULL\n");
2902 ExplicitAccess.Trustee.TrusteeType = TRUSTEE_IS_UNKNOWN;
2903 ExplicitAccess.Trustee.pMultipleTrustee = NULL;
2904 ExplicitAccess.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2905 res = pSetEntriesInAclA(1, &ExplicitAccess, OldAcl, &NewAcl);
2906 ok(res == ERROR_SUCCESS, "SetEntriesInAclA failed: %u\n", res);
2907 ok(NewAcl != NULL, "returned acl was NULL\n");
2910 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH)
2912 skip("Non-english locale (test with hardcoded 'Everyone')\n");
2916 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
2917 ExplicitAccess.Trustee.ptstrName = (LPSTR)szEveryone;
2918 res = pSetEntriesInAclA(1, &ExplicitAccess, OldAcl, &NewAcl);
2919 ok(res == ERROR_SUCCESS, "SetEntriesInAclA failed: %u\n", res);
2920 ok(NewAcl != NULL, "returned acl was NULL\n");
2923 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_BAD_FORM;
2924 res = pSetEntriesInAclA(1, &ExplicitAccess, OldAcl, &NewAcl);
2925 ok(res == ERROR_INVALID_PARAMETER ||
2926 broken(res == ERROR_NOT_SUPPORTED), /* NT4 */
2927 "SetEntriesInAclA failed: %u\n", res);
2928 ok(NewAcl == NULL ||
2929 broken(NewAcl != NULL), /* NT4 */
2930 "returned acl wasn't NULL: %p\n", NewAcl);
2932 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
2933 ExplicitAccess.Trustee.MultipleTrusteeOperation = TRUSTEE_IS_IMPERSONATE;
2934 res = pSetEntriesInAclA(1, &ExplicitAccess, OldAcl, &NewAcl);
2935 ok(res == ERROR_INVALID_PARAMETER ||
2936 broken(res == ERROR_NOT_SUPPORTED), /* NT4 */
2937 "SetEntriesInAclA failed: %u\n", res);
2938 ok(NewAcl == NULL ||
2939 broken(NewAcl != NULL), /* NT4 */
2940 "returned acl wasn't NULL: %p\n", NewAcl);
2942 ExplicitAccess.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2943 ExplicitAccess.grfAccessMode = SET_ACCESS;
2944 res = pSetEntriesInAclA(1, &ExplicitAccess, OldAcl, &NewAcl);
2945 ok(res == ERROR_SUCCESS, "SetEntriesInAclA failed: %u\n", res);
2946 ok(NewAcl != NULL, "returned acl was NULL\n");
2950 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
2951 ExplicitAccess.Trustee.ptstrName = (LPSTR)szCurrentUser;
2952 res = pSetEntriesInAclA(1, &ExplicitAccess, OldAcl, &NewAcl);
2953 ok(res == ERROR_SUCCESS, "SetEntriesInAclA failed: %u\n", res);
2954 ok(NewAcl != NULL, "returned acl was NULL\n");
2957 ExplicitAccess.grfAccessMode = REVOKE_ACCESS;
2958 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
2959 ExplicitAccess.Trustee.ptstrName = UsersSid;
2960 res = pSetEntriesInAclA(1, &ExplicitAccess, OldAcl, &NewAcl);
2961 ok(res == ERROR_SUCCESS, "SetEntriesInAclA failed: %u\n", res);
2962 ok(NewAcl != NULL, "returned acl was NULL\n");
2966 FreeSid(EveryoneSid);
2967 HeapFree(GetProcessHeap(), 0, OldAcl);
2970 static void test_GetNamedSecurityInfoA(void)
2972 PSECURITY_DESCRIPTOR pSecDesc;
2974 SECURITY_DESCRIPTOR_CONTROL control;
2978 BOOL owner_defaulted;
2979 BOOL group_defaulted;
2982 CHAR windows_dir[MAX_PATH];
2984 if (!pGetNamedSecurityInfoA)
2986 win_skip("GetNamedSecurityInfoA is not available\n");
2990 ret = GetWindowsDirectoryA(windows_dir, MAX_PATH);
2991 ok(ret, "GetWindowsDirectory failed with error %d\n", GetLastError());
2993 SetLastError(0xdeadbeef);
2994 error = pGetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,
2995 OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION,
2996 NULL, NULL, NULL, NULL, &pSecDesc);
2997 if (error != ERROR_SUCCESS && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2999 win_skip("GetNamedSecurityInfoA is not implemented\n");
3002 ok(!error, "GetNamedSecurityInfo failed with error %d\n", error);
3004 ret = GetSecurityDescriptorControl(pSecDesc, &control, &revision);
3005 ok(ret, "GetSecurityDescriptorControl failed with error %d\n", GetLastError());
3006 ok((control & (SE_SELF_RELATIVE|SE_DACL_PRESENT)) == (SE_SELF_RELATIVE|SE_DACL_PRESENT) ||
3007 broken((control & (SE_SELF_RELATIVE|SE_DACL_PRESENT)) == SE_DACL_PRESENT), /* NT4 */
3008 "control (0x%x) doesn't have (SE_SELF_RELATIVE|SE_DACL_PRESENT) flags set\n", control);
3009 ok(revision == SECURITY_DESCRIPTOR_REVISION1, "revision was %d instead of 1\n", revision);
3011 isNT4 = (control & (SE_SELF_RELATIVE|SE_DACL_PRESENT)) == SE_DACL_PRESENT;
3013 ret = GetSecurityDescriptorOwner(pSecDesc, &owner, &owner_defaulted);
3014 ok(ret, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
3015 ok(owner != NULL, "owner should not be NULL\n");
3017 ret = GetSecurityDescriptorGroup(pSecDesc, &group, &group_defaulted);
3018 ok(ret, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
3019 ok(group != NULL, "group should not be NULL\n");
3020 LocalFree(pSecDesc);
3023 /* NULL descriptor tests */
3026 win_skip("NT4 does not support GetNamedSecutityInfo with a NULL descriptor\n");
3030 error = pGetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,DACL_SECURITY_INFORMATION,
3031 NULL, NULL, NULL, NULL, NULL);
3032 ok(error==ERROR_INVALID_PARAMETER, "GetNamedSecurityInfo failed with error %d\n", error);
3034 error = pGetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,DACL_SECURITY_INFORMATION,
3035 NULL, NULL, &dacl, NULL, NULL);
3036 ok(!error, "GetNamedSecurityInfo failed with error %d\n", error);
3037 ok(dacl != NULL, "dacl should not be NULL\n");
3039 error = pGetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,OWNER_SECURITY_INFORMATION,
3040 NULL, NULL, &dacl, NULL, NULL);
3041 ok(error==ERROR_INVALID_PARAMETER, "GetNamedSecurityInfo failed with error %d\n", error);
3044 static void test_ConvertStringSecurityDescriptor(void)
3047 PSECURITY_DESCRIPTOR pSD;
3048 static const WCHAR Blank[] = { 0 };
3052 const char *sidstring;
3059 { "D:(A;;GA;;;WD)", 0xdeadbeef, FALSE, ERROR_UNKNOWN_REVISION },
3060 /* test ACE string type */
3061 { "D:(A;;GA;;;WD)", SDDL_REVISION_1, TRUE },
3062 { "D:(D;;GA;;;WD)", SDDL_REVISION_1, TRUE },
3063 { "ERROR:(D;;GA;;;WD)", SDDL_REVISION_1, FALSE, ERROR_INVALID_PARAMETER },
3064 /* test ACE string with spaces */
3065 { " D:(D;;GA;;;WD)", SDDL_REVISION_1, TRUE },
3066 { "D: (D;;GA;;;WD)", SDDL_REVISION_1, TRUE },
3067 { "D:( D;;GA;;;WD)", SDDL_REVISION_1, TRUE },
3068 { "D:(D ;;GA;;;WD)", SDDL_REVISION_1, FALSE, RPC_S_INVALID_STRING_UUID, ERROR_INVALID_ACL }, /* Vista+ */
3069 { "D:(D; ;GA;;;WD)", SDDL_REVISION_1, TRUE },
3070 { "D:(D;; GA;;;WD)", SDDL_REVISION_1, TRUE },
3071 { "D:(D;;GA ;;;WD)", SDDL_REVISION_1, FALSE, ERROR_INVALID_ACL },
3072 { "D:(D;;GA; ;;WD)", SDDL_REVISION_1, TRUE },
3073 { "D:(D;;GA;; ;WD)", SDDL_REVISION_1, TRUE },
3074 { "D:(D;;GA;;; WD)", SDDL_REVISION_1, TRUE },
3075 { "D:(D;;GA;;;WD )", SDDL_REVISION_1, TRUE },
3076 /* test ACE string access rights */
3077 { "D:(A;;GA;;;WD)", SDDL_REVISION_1, TRUE },
3078 { "D:(A;;GRGWGX;;;WD)", SDDL_REVISION_1, TRUE },
3079 { "D:(A;;RCSDWDWO;;;WD)", SDDL_REVISION_1, TRUE },
3080 { "D:(A;;RPWPCCDCLCSWLODTCR;;;WD)", SDDL_REVISION_1, TRUE },
3081 { "D:(A;;FAFRFWFX;;;WD)", SDDL_REVISION_1, TRUE },
3082 { "D:(A;;KAKRKWKX;;;WD)", SDDL_REVISION_1, TRUE },
3083 { "D:(A;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1, TRUE },
3084 { "S:(AU;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1, TRUE },
3085 /* test ACE string access right error case */
3086 { "D:(A;;ROB;;;WD)", SDDL_REVISION_1, FALSE, ERROR_INVALID_ACL },
3087 /* test behaviour with empty strings */
3088 { "", SDDL_REVISION_1, TRUE },
3089 /* test ACE string SID */
3090 { "D:(D;;GA;;;S-1-0-0)", SDDL_REVISION_1, TRUE },
3091 { "D:(D;;GA;;;Nonexistent account)", SDDL_REVISION_1, FALSE, ERROR_INVALID_ACL, ERROR_INVALID_SID } /* W2K */
3094 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA)
3096 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
3100 for (i = 0; i < sizeof(cssd)/sizeof(cssd[0]); i++)
3104 SetLastError(0xdeadbeef);
3105 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
3106 cssd[i].sidstring, cssd[i].revision, &pSD, NULL);
3107 GLE = GetLastError();
3108 ok(ret == cssd[i].ret, "(%02u) Expected %s (%d)\n", i, cssd[i].ret ? "success" : "failure", GLE);
3110 ok(GLE == cssd[i].GLE ||
3111 (cssd[i].altGLE && GLE == cssd[i].altGLE),
3112 "(%02u) Unexpected last error %d\n", i, GLE);
3117 /* test behaviour with NULL parameters */
3118 SetLastError(0xdeadbeef);
3119 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
3120 NULL, 0xdeadbeef, &pSD, NULL);
3122 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
3123 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3126 SetLastError(0xdeadbeef);
3127 ret = pConvertStringSecurityDescriptorToSecurityDescriptorW(
3128 NULL, 0xdeadbeef, &pSD, NULL);
3129 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
3130 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3133 SetLastError(0xdeadbeef);
3134 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
3135 "D:(A;;ROB;;;WD)", 0xdeadbeef, NULL, NULL);
3136 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
3137 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3140 SetLastError(0xdeadbeef);
3141 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
3142 "D:(A;;ROB;;;WD)", SDDL_REVISION_1, NULL, NULL);
3143 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
3144 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3147 /* test behaviour with empty strings */
3148 SetLastError(0xdeadbeef);
3149 ret = pConvertStringSecurityDescriptorToSecurityDescriptorW(
3150 Blank, SDDL_REVISION_1, &pSD, NULL);
3151 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
3155 static void test_ConvertSecurityDescriptorToString(void)
3157 SECURITY_DESCRIPTOR desc;
3158 SECURITY_INFORMATION sec_info = OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION|SACL_SECURITY_INFORMATION;
3167 if (!pConvertSecurityDescriptorToStringSecurityDescriptorA)
3169 win_skip("ConvertSecurityDescriptorToStringSecurityDescriptor is not available\n");
3172 if (!pCreateWellKnownSid)
3174 win_skip("CreateWellKnownSid is not available\n");
3178 /* It seems Windows XP adds an extra character to the length of the string for each ACE in an ACL. We
3179 * don't replicate this feature so we only test len >= strlen+1. */
3180 #define CHECK_RESULT_AND_FREE(exp_str) \
3181 ok(strcmp(string, (exp_str)) == 0, "String mismatch (expected \"%s\", got \"%s\")\n", (exp_str), string); \
3182 ok(len >= (strlen(exp_str) + 1), "Length mismatch (expected %d, got %d)\n", lstrlen(exp_str) + 1, len); \
3185 #define CHECK_ONE_OF_AND_FREE(exp_str1, exp_str2) \
3186 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); \
3187 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); \
3190 InitializeSecurityDescriptor(&desc, SECURITY_DESCRIPTOR_REVISION);
3191 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3192 CHECK_RESULT_AND_FREE("");
3195 pCreateWellKnownSid(WinLocalSid, NULL, sid_buf, &size);
3196 SetSecurityDescriptorOwner(&desc, sid_buf, FALSE);
3197 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3198 CHECK_RESULT_AND_FREE("O:S-1-2-0");
3200 SetSecurityDescriptorOwner(&desc, sid_buf, TRUE);
3201 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3202 CHECK_RESULT_AND_FREE("O:S-1-2-0");
3204 size = sizeof(sid_buf);
3205 pCreateWellKnownSid(WinLocalSystemSid, NULL, sid_buf, &size);
3206 SetSecurityDescriptorOwner(&desc, sid_buf, TRUE);
3207 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3208 CHECK_RESULT_AND_FREE("O:SY");
3210 pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid);
3211 SetSecurityDescriptorGroup(&desc, psid, TRUE);
3212 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3213 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576");
3215 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, GROUP_SECURITY_INFORMATION, &string, &len), "Conversion failed\n");
3216 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
3218 pacl = (PACL)acl_buf;
3219 InitializeAcl(pacl, sizeof(acl_buf), ACL_REVISION);
3220 SetSecurityDescriptorDacl(&desc, TRUE, pacl, TRUE);
3221 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3222 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
3224 SetSecurityDescriptorDacl(&desc, TRUE, pacl, FALSE);
3225 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3226 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
3228 pConvertStringSidToSidA("S-1-5-6", &psid2);
3229 pAddAccessAllowedAceEx(pacl, ACL_REVISION, NO_PROPAGATE_INHERIT_ACE, 0xf0000000, psid2);
3230 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3231 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)");
3233 pAddAccessAllowedAceEx(pacl, ACL_REVISION, INHERIT_ONLY_ACE|INHERITED_ACE, 0x00000003, psid2);
3234 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3235 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)");
3237 pAddAccessDeniedAceEx(pacl, ACL_REVISION, OBJECT_INHERIT_ACE|CONTAINER_INHERIT_ACE, 0xffffffff, psid);
3238 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3239 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)");
3242 pacl = (PACL)acl_buf;
3243 InitializeAcl(pacl, sizeof(acl_buf), ACL_REVISION);
3244 SetSecurityDescriptorSacl(&desc, TRUE, pacl, FALSE);
3245 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3246 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:");
3248 /* fails in win2k */
3249 SetSecurityDescriptorDacl(&desc, TRUE, NULL, FALSE);
3250 pAddAuditAccessAceEx(pacl, ACL_REVISION, VALID_INHERIT_FLAGS, KEY_READ|KEY_WRITE, psid2, TRUE, TRUE);
3251 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len))
3253 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)", /* XP */
3254 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)" /* Vista */);
3257 /* fails in win2k */
3258 pAddAuditAccessAceEx(pacl, ACL_REVISION, NO_PROPAGATE_INHERIT_ACE, FILE_GENERIC_READ|FILE_GENERIC_WRITE, psid2, TRUE, FALSE);
3259 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len))
3261 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", /* XP */
3262 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)" /* Vista */);
3269 static void test_SetSecurityDescriptorControl (PSECURITY_DESCRIPTOR sec)
3271 SECURITY_DESCRIPTOR_CONTROL ref;
3272 SECURITY_DESCRIPTOR_CONTROL test;
3274 SECURITY_DESCRIPTOR_CONTROL const mutable
3275 = SE_DACL_AUTO_INHERIT_REQ | SE_SACL_AUTO_INHERIT_REQ
3276 | SE_DACL_AUTO_INHERITED | SE_SACL_AUTO_INHERITED
3277 | SE_DACL_PROTECTED | SE_SACL_PROTECTED
3278 | 0x00000040 | 0x00000080 /* not defined in winnt.h */
3280 SECURITY_DESCRIPTOR_CONTROL const immutable
3281 = SE_OWNER_DEFAULTED | SE_GROUP_DEFAULTED
3282 | SE_DACL_PRESENT | SE_DACL_DEFAULTED
3283 | SE_SACL_PRESENT | SE_SACL_DEFAULTED
3284 | SE_RM_CONTROL_VALID | SE_SELF_RELATIVE
3289 LPCSTR fmt = "Expected error %s, got %u\n";
3291 GetSecurityDescriptorControl (sec, &ref, &dwRevision);
3293 /* The mutable bits are mutable regardless of the truth of
3294 SE_DACL_PRESENT and/or SE_SACL_PRESENT */
3296 /* Check call barfs if any bit-of-interest is immutable */
3297 for (bit = 0; bit < 16; ++bit)
3299 SECURITY_DESCRIPTOR_CONTROL const bitOfInterest = 1 << bit;
3300 SECURITY_DESCRIPTOR_CONTROL setOrClear = ref & bitOfInterest;
3302 SECURITY_DESCRIPTOR_CONTROL ctrl;
3304 DWORD dwExpect = (bitOfInterest & immutable)
3305 ? ERROR_INVALID_PARAMETER : 0xbebecaca;
3306 LPCSTR strExpect = (bitOfInterest & immutable)
3307 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
3309 ctrl = (bitOfInterest & mutable) ? ref + bitOfInterest : ref;
3310 setOrClear ^= bitOfInterest;
3311 SetLastError (0xbebecaca);
3312 pSetSecurityDescriptorControl (sec, bitOfInterest, setOrClear);
3313 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
3314 GetSecurityDescriptorControl(sec, &test, &dwRevision);
3315 expect_eq(test, ctrl, int, "%x");
3317 setOrClear ^= bitOfInterest;
3318 SetLastError (0xbebecaca);
3319 pSetSecurityDescriptorControl (sec, bitOfInterest, setOrClear);
3320 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
3321 GetSecurityDescriptorControl (sec, &test, &dwRevision);
3322 expect_eq(test, ref, int, "%x");
3325 /* Check call barfs if any bit-to-set is immutable
3326 even when not a bit-of-interest */
3327 for (bit = 0; bit < 16; ++bit)
3329 SECURITY_DESCRIPTOR_CONTROL const bitsOfInterest = mutable;
3330 SECURITY_DESCRIPTOR_CONTROL setOrClear = ref & bitsOfInterest;
3332 SECURITY_DESCRIPTOR_CONTROL ctrl;
3334 DWORD dwExpect = ((1 << bit) & immutable)
3335 ? ERROR_INVALID_PARAMETER : 0xbebecaca;
3336 LPCSTR strExpect = ((1 << bit) & immutable)
3337 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
3339 ctrl = ((1 << bit) & immutable) ? test : ref | mutable;
3340 setOrClear ^= bitsOfInterest;
3341 SetLastError (0xbebecaca);
3342 pSetSecurityDescriptorControl (sec, bitsOfInterest, setOrClear | (1 << bit));
3343 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
3344 GetSecurityDescriptorControl(sec, &test, &dwRevision);
3345 expect_eq(test, ctrl, int, "%x");
3347 ctrl = ((1 << bit) & immutable) ? test : ref | (1 << bit);
3348 setOrClear ^= bitsOfInterest;
3349 SetLastError (0xbebecaca);
3350 pSetSecurityDescriptorControl (sec, bitsOfInterest, setOrClear | (1 << bit));
3351 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
3352 GetSecurityDescriptorControl(sec, &test, &dwRevision);
3353 expect_eq(test, ctrl, int, "%x");
3357 static void test_PrivateObjectSecurity(void)
3359 SECURITY_INFORMATION sec_info = OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION|SACL_SECURITY_INFORMATION;
3360 SECURITY_DESCRIPTOR_CONTROL ctrl;
3361 PSECURITY_DESCRIPTOR sec;
3367 PSECURITY_DESCRIPTOR buf;
3370 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA)
3372 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
3376 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3378 "G:S-1-5-21-93476-23408-4576"
3379 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)"
3380 "(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3381 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
3382 SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
3384 test_SetSecurityDescriptorControl(sec);
3388 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3390 "G:S-1-5-21-93476-23408-4576",
3391 SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
3393 test_SetSecurityDescriptorControl(sec);
3397 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3399 "G:S-1-5-21-93476-23408-4576"
3400 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3401 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
3402 buf = HeapAlloc(GetProcessHeap(), 0, dwDescSize);
3403 pSetSecurityDescriptorControl(sec, SE_DACL_PROTECTED, SE_DACL_PROTECTED);
3404 GetSecurityDescriptorControl(sec, &ctrl, &dwRevision);
3405 expect_eq(ctrl, 0x9014, int, "%x");
3407 ret = GetPrivateObjectSecurity(sec, GROUP_SECURITY_INFORMATION, buf, dwDescSize, &retSize);
3408 ok(ret, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3409 ok(retSize <= dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
3410 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3411 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
3412 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
3413 expect_eq(ctrl, 0x8000, int, "%x");
3415 ret = GetPrivateObjectSecurity(sec, GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION, buf, dwDescSize, &retSize);
3416 ok(ret, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3417 ok(retSize <= dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
3418 ret = pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len);
3419 ok(ret, "Conversion failed err=%u\n", GetLastError());
3420 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)",
3421 "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 */
3422 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
3423 expect_eq(ctrl & (~ SE_DACL_PROTECTED), 0x8004, int, "%x");
3425 ret = GetPrivateObjectSecurity(sec, sec_info, buf, dwDescSize, &retSize);
3426 ok(ret, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3427 ok(retSize == dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
3428 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3429 CHECK_ONE_OF_AND_FREE("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)",
3434 "G:S-1-5-21-93476-23408-4576"
3435 "D:P(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3436 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)"); /* Win7 */
3437 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
3438 expect_eq(ctrl & (~ SE_DACL_PROTECTED), 0x8014, int, "%x");
3440 SetLastError(0xdeadbeef);
3441 ok(GetPrivateObjectSecurity(sec, sec_info, buf, 5, &retSize) == FALSE, "GetPrivateObjectSecurity should have failed\n");
3442 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected error ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
3445 HeapFree(GetProcessHeap(), 0, buf);
3447 #undef CHECK_RESULT_AND_FREE
3448 #undef CHECK_ONE_OF_AND_FREE
3450 static void test_acls(void)
3453 PACL pAcl = (PACL)buffer;
3456 SetLastError(0xdeadbeef);
3457 ret = InitializeAcl(pAcl, sizeof(ACL) - 1, ACL_REVISION);
3458 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
3460 win_skip("InitializeAcl is not implemented\n");
3464 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InitializeAcl with too small a buffer should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
3466 SetLastError(0xdeadbeef);
3467 ret = InitializeAcl(pAcl, 0xffffffff, ACL_REVISION);
3468 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "InitializeAcl with too large a buffer should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3470 SetLastError(0xdeadbeef);
3471 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION1);
3472 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "InitializeAcl(ACL_REVISION1) should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3474 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION2);
3475 ok(ret, "InitializeAcl(ACL_REVISION2) failed with error %d\n", GetLastError());
3477 ret = IsValidAcl(pAcl);
3478 ok(ret, "IsValidAcl failed with error %d\n", GetLastError());
3480 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION3);
3481 ok(ret, "InitializeAcl(ACL_REVISION3) failed with error %d\n", GetLastError());
3483 ret = IsValidAcl(pAcl);
3484 ok(ret, "IsValidAcl failed with error %d\n", GetLastError());
3486 SetLastError(0xdeadbeef);
3487 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION4);
3488 if (GetLastError() != ERROR_INVALID_PARAMETER)
3490 ok(ret, "InitializeAcl(ACL_REVISION4) failed with error %d\n", GetLastError());
3492 ret = IsValidAcl(pAcl);
3493 ok(ret, "IsValidAcl failed with error %d\n", GetLastError());
3496 win_skip("ACL_REVISION4 is not implemented on NT4\n");
3498 SetLastError(0xdeadbeef);
3499 ret = InitializeAcl(pAcl, sizeof(buffer), -1);
3500 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "InitializeAcl(-1) failed with error %d\n", GetLastError());
3503 static void test_GetSecurityInfo(void)
3506 PSECURITY_DESCRIPTOR sd;
3511 if (!pGetSecurityInfo)
3513 win_skip("GetSecurityInfo is not available\n");
3517 /* Create something. Files have lots of associated security info. */
3518 obj = CreateFile(myARGV[0], GENERIC_READ, FILE_SHARE_READ, NULL,
3519 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
3520 if (obj == INVALID_HANDLE_VALUE)
3522 skip("Couldn't create an object for GetSecurityInfo test\n");
3526 ret = pGetSecurityInfo(obj, SE_FILE_OBJECT,
3527 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
3528 &owner, &group, &dacl, NULL, &sd);
3529 if (ret == ERROR_CALL_NOT_IMPLEMENTED)
3531 win_skip("GetSecurityInfo is not implemented\n");
3535 ok(ret == ERROR_SUCCESS, "GetSecurityInfo returned %d\n", ret);
3536 ok(sd != NULL, "GetSecurityInfo\n");
3537 ok(owner != NULL, "GetSecurityInfo\n");
3538 ok(group != NULL, "GetSecurityInfo\n");
3540 ok(IsValidAcl(dacl), "GetSecurityInfo\n");
3542 win_skip("No ACL information returned\n");
3546 if (!pCreateWellKnownSid)
3548 win_skip("NULL parameter test would crash on NT4\n");
3553 /* If we don't ask for the security descriptor, Windows will still give us
3554 the other stuff, leaving us no way to free it. */
3555 ret = pGetSecurityInfo(obj, SE_FILE_OBJECT,
3556 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
3557 &owner, &group, &dacl, NULL, NULL);
3558 ok(ret == ERROR_SUCCESS, "GetSecurityInfo returned %d\n", ret);
3559 ok(owner != NULL, "GetSecurityInfo\n");
3560 ok(group != NULL, "GetSecurityInfo\n");
3562 ok(IsValidAcl(dacl), "GetSecurityInfo\n");
3564 win_skip("No ACL information returned\n");
3569 static void test_GetSidSubAuthority(void)
3573 if (!pGetSidSubAuthority || !pConvertStringSidToSidA || !pIsValidSid || !pGetSidSubAuthorityCount)
3575 win_skip("Some functions not available\n");
3578 /* Note: on windows passing in an invalid index like -1, lets GetSidSubAuthority return 0x05000000 but
3579 still GetLastError returns ERROR_SUCCESS then. We don't test these unlikely cornercases here for now */
3580 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576",&psid),"ConvertStringSidToSidA failed\n");
3581 ok(pIsValidSid(psid),"Sid is not valid\n");
3582 SetLastError(0xbebecaca);
3583 ok(*pGetSidSubAuthorityCount(psid) == 4,"GetSidSubAuthorityCount gave %d expected 4\n",*pGetSidSubAuthorityCount(psid));
3584 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3585 SetLastError(0xbebecaca);
3586 ok(*pGetSidSubAuthority(psid,0) == 21,"GetSidSubAuthority gave %d expected 21\n",*pGetSidSubAuthority(psid,0));
3587 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3588 SetLastError(0xbebecaca);
3589 ok(*pGetSidSubAuthority(psid,1) == 93476,"GetSidSubAuthority gave %d expected 93476\n",*pGetSidSubAuthority(psid,1));
3590 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3591 SetLastError(0xbebecaca);
3592 ok(pGetSidSubAuthority(psid,4) != NULL,"Expected out of bounds GetSidSubAuthority to return a non-NULL pointer\n");
3593 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3597 static void test_CheckTokenMembership(void)
3599 PTOKEN_GROUPS token_groups;
3601 HANDLE process_token, token;
3606 if (!pCheckTokenMembership)
3608 win_skip("CheckTokenMembership is not available\n");
3611 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &process_token);
3612 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
3614 ret = DuplicateToken(process_token, SecurityImpersonation, &token);
3615 ok(ret, "DuplicateToken failed with error %d\n", GetLastError());
3618 ret = GetTokenInformation(token, TokenGroups, NULL, 0, &size);
3619 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
3620 "GetTokenInformation(TokenGroups) %s with error %d\n",
3621 ret ? "succeeded" : "failed", GetLastError());
3622 token_groups = HeapAlloc(GetProcessHeap(), 0, size);
3623 ret = GetTokenInformation(token, TokenGroups, token_groups, size, &size);
3624 ok(ret, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
3626 for (i = 0; i < token_groups->GroupCount; i++)
3628 if (token_groups->Groups[i].Attributes & SE_GROUP_ENABLED)
3632 if (i == token_groups->GroupCount)
3634 HeapFree(GetProcessHeap(), 0, token_groups);
3636 skip("user not a member of any group\n");
3640 ret = pCheckTokenMembership(token, token_groups->Groups[i].Sid, &is_member);
3641 ok(ret, "CheckTokenMembership failed with error %d\n", GetLastError());
3642 ok(is_member, "CheckTokenMembership should have detected sid as member\n");
3644 ret = pCheckTokenMembership(NULL, token_groups->Groups[i].Sid, &is_member);
3645 ok(ret, "CheckTokenMembership failed with error %d\n", GetLastError());
3646 ok(is_member, "CheckTokenMembership should have detected sid as member\n");
3648 ret = pCheckTokenMembership(process_token, token_groups->Groups[i].Sid, &is_member);
3650 ok(!ret && GetLastError() == ERROR_NO_IMPERSONATION_TOKEN,
3651 "CheckTokenMembership with process token %s with error %d\n",
3652 ret ? "succeeded" : "failed", GetLastError());
3653 ok(!is_member, "CheckTokenMembership should have cleared is_member\n");
3656 HeapFree(GetProcessHeap(), 0, token_groups);
3658 CloseHandle(process_token);
3661 static void test_EqualSid(void)
3665 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
3666 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
3668 SetLastError(0xdeadbeef);
3669 ret = AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
3670 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &sid1);
3671 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
3673 win_skip("AllocateAndInitializeSid is not implemented\n");
3676 ok(ret, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3677 ok(GetLastError() == 0xdeadbeef,
3678 "AllocateAndInitializeSid shouldn't have set last error to %d\n",
3681 ret = AllocateAndInitializeSid(&SIDAuthWorld, 1, SECURITY_WORLD_RID,
3682 0, 0, 0, 0, 0, 0, 0, &sid2);
3683 ok(ret, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3685 SetLastError(0xdeadbeef);
3686 ret = EqualSid(sid1, sid2);
3687 ok(!ret, "World and domain admins sids shouldn't have been equal\n");
3688 ok(GetLastError() == ERROR_SUCCESS ||
3689 broken(GetLastError() == 0xdeadbeef), /* NT4 */
3690 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
3693 SetLastError(0xdeadbeef);
3694 sid2 = FreeSid(sid2);
3695 ok(!sid2, "FreeSid should have returned NULL instead of %p\n", sid2);
3696 ok(GetLastError() == 0xdeadbeef,
3697 "FreeSid shouldn't have set last error to %d\n",
3700 ret = AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
3701 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &sid2);
3702 ok(ret, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3704 SetLastError(0xdeadbeef);
3705 ret = EqualSid(sid1, sid2);
3706 ok(ret, "Same sids should have been equal\n");
3707 ok(GetLastError() == ERROR_SUCCESS ||
3708 broken(GetLastError() == 0xdeadbeef), /* NT4 */
3709 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
3712 ((SID *)sid2)->Revision = 2;
3713 SetLastError(0xdeadbeef);
3714 ret = EqualSid(sid1, sid2);
3715 ok(!ret, "EqualSid with invalid sid should have returned FALSE\n");
3716 ok(GetLastError() == ERROR_SUCCESS ||
3717 broken(GetLastError() == 0xdeadbeef), /* NT4 */
3718 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
3720 ((SID *)sid2)->Revision = SID_REVISION;
3726 START_TEST(security)
3733 test_process_security_child();
3739 test_CreateWellKnownSid();
3740 test_FileSecurity();
3743 test_LookupAccountSid();
3744 test_LookupAccountName();
3745 test_security_descriptor();
3746 test_process_security();
3747 test_impersonation_level();
3748 test_SetEntriesInAclW();
3749 test_SetEntriesInAclA();
3750 test_GetNamedSecurityInfoA();
3751 test_ConvertStringSecurityDescriptor();
3752 test_ConvertSecurityDescriptorToString();
3753 test_PrivateObjectSecurity();
3755 test_GetSecurityInfo();
3756 test_GetSidSubAuthority();
3757 test_CheckTokenMembership();