2 * Unit tests for security functions
4 * Copyright (c) 2004 Mike McCormack
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #define WIN32_NO_STATUS
35 #include "wine/test.h"
37 /* copied from Wine winternl.h - not included in the Windows SDK */
38 typedef enum _OBJECT_INFORMATION_CLASS {
39 ObjectBasicInformation,
40 ObjectNameInformation,
41 ObjectTypeInformation,
44 } OBJECT_INFORMATION_CLASS, *POBJECT_INFORMATION_CLASS;
46 typedef struct _OBJECT_BASIC_INFORMATION {
48 ACCESS_MASK GrantedAccess;
52 ULONG NonPagedPoolUsage;
54 ULONG NameInformationLength;
55 ULONG TypeInformationLength;
56 ULONG SecurityDescriptorLength;
57 LARGE_INTEGER CreateTime;
58 } OBJECT_BASIC_INFORMATION, *POBJECT_BASIC_INFORMATION;
60 #define expect_eq(expr, value, type, format) { type ret = expr; ok((value) == ret, #expr " expected " format " got " format "\n", (value), (ret)); }
62 static BOOL (WINAPI *pAddAccessAllowedAceEx)(PACL, DWORD, DWORD, DWORD, PSID);
63 static BOOL (WINAPI *pAddAccessDeniedAceEx)(PACL, DWORD, DWORD, DWORD, PSID);
64 static BOOL (WINAPI *pAddAuditAccessAceEx)(PACL, DWORD, DWORD, DWORD, PSID, BOOL, BOOL);
65 static VOID (WINAPI *pBuildTrusteeWithSidA)( PTRUSTEEA pTrustee, PSID pSid );
66 static VOID (WINAPI *pBuildTrusteeWithNameA)( PTRUSTEEA pTrustee, LPSTR pName );
67 static VOID (WINAPI *pBuildTrusteeWithObjectsAndNameA)( PTRUSTEEA pTrustee,
68 POBJECTS_AND_NAME_A pObjName,
69 SE_OBJECT_TYPE ObjectType,
71 LPSTR InheritedObjectTypeName,
73 static VOID (WINAPI *pBuildTrusteeWithObjectsAndSidA)( PTRUSTEEA pTrustee,
74 POBJECTS_AND_SID pObjSid,
76 GUID* pInheritedObjectGuid,
78 static LPSTR (WINAPI *pGetTrusteeNameA)( PTRUSTEEA pTrustee );
79 static BOOL (WINAPI *pMakeSelfRelativeSD)( PSECURITY_DESCRIPTOR, PSECURITY_DESCRIPTOR, LPDWORD );
80 static BOOL (WINAPI *pConvertSidToStringSidA)( PSID pSid, LPSTR *str );
81 static BOOL (WINAPI *pConvertStringSidToSidA)( LPCSTR str, PSID pSid );
82 static BOOL (WINAPI *pCheckTokenMembership)(HANDLE, PSID, PBOOL);
83 static BOOL (WINAPI *pConvertStringSecurityDescriptorToSecurityDescriptorA)(LPCSTR, DWORD,
84 PSECURITY_DESCRIPTOR*, PULONG );
85 static BOOL (WINAPI *pConvertStringSecurityDescriptorToSecurityDescriptorW)(LPCWSTR, DWORD,
86 PSECURITY_DESCRIPTOR*, PULONG );
87 static BOOL (WINAPI *pConvertSecurityDescriptorToStringSecurityDescriptorA)(PSECURITY_DESCRIPTOR, DWORD,
88 SECURITY_INFORMATION, LPSTR *, PULONG );
89 static BOOL (WINAPI *pGetFileSecurityA)(LPCSTR, SECURITY_INFORMATION,
90 PSECURITY_DESCRIPTOR, DWORD, LPDWORD);
91 static BOOL (WINAPI *pSetFileSecurityA)(LPCSTR, SECURITY_INFORMATION,
92 PSECURITY_DESCRIPTOR);
93 static DWORD (WINAPI *pGetNamedSecurityInfoA)(LPSTR, SE_OBJECT_TYPE, SECURITY_INFORMATION,
94 PSID*, PSID*, PACL*, PACL*,
95 PSECURITY_DESCRIPTOR*);
96 static PDWORD (WINAPI *pGetSidSubAuthority)(PSID, DWORD);
97 static PUCHAR (WINAPI *pGetSidSubAuthorityCount)(PSID);
98 static BOOL (WINAPI *pIsValidSid)(PSID);
99 static DWORD (WINAPI *pRtlAdjustPrivilege)(ULONG,BOOLEAN,BOOLEAN,PBOOLEAN);
100 static BOOL (WINAPI *pCreateWellKnownSid)(WELL_KNOWN_SID_TYPE,PSID,PSID,DWORD*);
101 static BOOL (WINAPI *pDuplicateTokenEx)(HANDLE,DWORD,LPSECURITY_ATTRIBUTES,
102 SECURITY_IMPERSONATION_LEVEL,TOKEN_TYPE,PHANDLE);
104 static NTSTATUS (WINAPI *pLsaQueryInformationPolicy)(LSA_HANDLE,POLICY_INFORMATION_CLASS,PVOID*);
105 static NTSTATUS (WINAPI *pLsaClose)(LSA_HANDLE);
106 static NTSTATUS (WINAPI *pLsaFreeMemory)(PVOID);
107 static NTSTATUS (WINAPI *pLsaOpenPolicy)(PLSA_UNICODE_STRING,PLSA_OBJECT_ATTRIBUTES,ACCESS_MASK,PLSA_HANDLE);
108 static NTSTATUS (WINAPI *pNtQueryObject)(HANDLE,OBJECT_INFORMATION_CLASS,PVOID,ULONG,PULONG);
109 static DWORD (WINAPI *pSetEntriesInAclW)(ULONG, PEXPLICIT_ACCESSW, PACL, PACL*);
110 static BOOL (WINAPI *pSetSecurityDescriptorControl)(PSECURITY_DESCRIPTOR, SECURITY_DESCRIPTOR_CONTROL,
111 SECURITY_DESCRIPTOR_CONTROL);
112 static DWORD (WINAPI *pGetSecurityInfo)(HANDLE, SE_OBJECT_TYPE, SECURITY_INFORMATION,
113 PSID*, PSID*, PACL*, PACL*, PSECURITY_DESCRIPTOR*);
114 static NTSTATUS (WINAPI *pNtAccessCheck)(PSECURITY_DESCRIPTOR, HANDLE, ACCESS_MASK, PGENERIC_MAPPING,
115 PPRIVILEGE_SET, PULONG, PULONG, NTSTATUS*);
119 static char** myARGV;
123 SID_IDENTIFIER_AUTHORITY auth;
127 static void init(void)
131 hntdll = GetModuleHandleA("ntdll.dll");
132 pNtQueryObject = (void *)GetProcAddress( hntdll, "NtQueryObject" );
133 pNtAccessCheck = (void *)GetProcAddress( hntdll, "NtAccessCheck" );
135 hmod = GetModuleHandle("advapi32.dll");
136 pAddAccessAllowedAceEx = (void *)GetProcAddress(hmod, "AddAccessAllowedAceEx");
137 pAddAccessDeniedAceEx = (void *)GetProcAddress(hmod, "AddAccessDeniedAceEx");
138 pAddAuditAccessAceEx = (void *)GetProcAddress(hmod, "AddAuditAccessAceEx");
139 pCheckTokenMembership = (void *)GetProcAddress(hmod, "CheckTokenMembership");
140 pConvertStringSecurityDescriptorToSecurityDescriptorA =
141 (void *)GetProcAddress(hmod, "ConvertStringSecurityDescriptorToSecurityDescriptorA" );
142 pConvertStringSecurityDescriptorToSecurityDescriptorW =
143 (void *)GetProcAddress(hmod, "ConvertStringSecurityDescriptorToSecurityDescriptorW" );
144 pConvertSecurityDescriptorToStringSecurityDescriptorA =
145 (void *)GetProcAddress(hmod, "ConvertSecurityDescriptorToStringSecurityDescriptorA" );
146 pGetFileSecurityA = (void *)GetProcAddress(hmod, "GetFileSecurityA" );
147 pSetFileSecurityA = (void *)GetProcAddress(hmod, "SetFileSecurityA" );
148 pCreateWellKnownSid = (void *)GetProcAddress( hmod, "CreateWellKnownSid" );
149 pGetNamedSecurityInfoA = (void *)GetProcAddress(hmod, "GetNamedSecurityInfoA");
150 pGetSidSubAuthority = (void *)GetProcAddress(hmod, "GetSidSubAuthority");
151 pGetSidSubAuthorityCount = (void *)GetProcAddress(hmod, "GetSidSubAuthorityCount");
152 pIsValidSid = (void *)GetProcAddress(hmod, "IsValidSid");
153 pMakeSelfRelativeSD = (void *)GetProcAddress(hmod, "MakeSelfRelativeSD");
154 pSetEntriesInAclW = (void *)GetProcAddress(hmod, "SetEntriesInAclW");
155 pSetSecurityDescriptorControl = (void *)GetProcAddress(hmod, "SetSecurityDescriptorControl");
156 pGetSecurityInfo = (void *)GetProcAddress(hmod, "GetSecurityInfo");
158 myARGC = winetest_get_mainargs( &myARGV );
161 static void test_str_sid(const char *str_sid)
166 if (pConvertStringSidToSidA(str_sid, &psid))
168 if (pConvertSidToStringSidA(psid, &temp))
170 trace(" %s: %s\n", str_sid, temp);
177 if (GetLastError() != ERROR_INVALID_SID)
178 trace(" %s: couldn't be converted, returned %d\n", str_sid, GetLastError());
180 trace(" %s: couldn't be converted\n", str_sid);
184 static void test_sid(void)
186 struct sidRef refs[] = {
187 { { {0x00,0x00,0x33,0x44,0x55,0x66} }, "S-1-860116326-1" },
188 { { {0x00,0x00,0x01,0x02,0x03,0x04} }, "S-1-16909060-1" },
189 { { {0x00,0x00,0x00,0x01,0x02,0x03} }, "S-1-66051-1" },
190 { { {0x00,0x00,0x00,0x00,0x01,0x02} }, "S-1-258-1" },
191 { { {0x00,0x00,0x00,0x00,0x00,0x02} }, "S-1-2-1" },
192 { { {0x00,0x00,0x00,0x00,0x00,0x0c} }, "S-1-12-1" },
194 const char noSubAuthStr[] = "S-1-5";
201 pConvertSidToStringSidA = (void *)GetProcAddress( hmod, "ConvertSidToStringSidA" );
202 if( !pConvertSidToStringSidA )
204 pConvertStringSidToSidA = (void *)GetProcAddress( hmod, "ConvertStringSidToSidA" );
205 if( !pConvertStringSidToSidA )
208 r = pConvertStringSidToSidA( NULL, NULL );
209 ok( !r, "expected failure with NULL parameters\n" );
210 if( GetLastError() == ERROR_CALL_NOT_IMPLEMENTED )
212 ok( GetLastError() == ERROR_INVALID_PARAMETER,
213 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
216 r = pConvertStringSidToSidA( refs[0].refStr, NULL );
217 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
218 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
221 r = pConvertStringSidToSidA( NULL, &str );
222 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
223 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
226 r = pConvertStringSidToSidA( noSubAuthStr, &psid );
228 "expected failure with no sub authorities\n" );
229 ok( GetLastError() == ERROR_INVALID_SID,
230 "expected GetLastError() is ERROR_INVALID_SID, got %d\n",
233 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid), "ConvertStringSidToSidA failed\n");
235 ok(pisid->SubAuthorityCount == 4, "Invalid sub authority count - expected 4, got %d\n", pisid->SubAuthorityCount);
236 ok(pisid->SubAuthority[0] == 21, "Invalid subauthority 0 - expceted 21, got %d\n", pisid->SubAuthority[0]);
237 ok(pisid->SubAuthority[3] == 4576, "Invalid subauthority 0 - expceted 4576, got %d\n", pisid->SubAuthority[3]);
241 for( i = 0; i < sizeof(refs) / sizeof(refs[0]); i++ )
245 r = AllocateAndInitializeSid( &refs[i].auth, 1,1,0,0,0,0,0,0,0,
247 ok( r, "failed to allocate sid\n" );
248 r = pConvertSidToStringSidA( psid, &str );
249 ok( r, "failed to convert sid\n" );
252 ok( !strcmp( str, refs[i].refStr ),
253 "incorrect sid, expected %s, got %s\n", refs[i].refStr, str );
259 r = pConvertStringSidToSidA( refs[i].refStr, &psid );
260 ok( r, "failed to parse sid string\n" );
263 !memcmp( pisid->IdentifierAuthority.Value, refs[i].auth.Value,
264 sizeof(refs[i].auth) ),
265 "string sid %s didn't parse to expected value\n"
266 "(got 0x%04x%08x, expected 0x%04x%08x)\n",
268 MAKEWORD( pisid->IdentifierAuthority.Value[1],
269 pisid->IdentifierAuthority.Value[0] ),
270 MAKELONG( MAKEWORD( pisid->IdentifierAuthority.Value[5],
271 pisid->IdentifierAuthority.Value[4] ),
272 MAKEWORD( pisid->IdentifierAuthority.Value[3],
273 pisid->IdentifierAuthority.Value[2] ) ),
274 MAKEWORD( refs[i].auth.Value[1], refs[i].auth.Value[0] ),
275 MAKELONG( MAKEWORD( refs[i].auth.Value[5], refs[i].auth.Value[4] ),
276 MAKEWORD( refs[i].auth.Value[3], refs[i].auth.Value[2] ) ) );
281 trace("String SIDs:\n");
322 static void test_trustee(void)
324 GUID ObjectType = {0x12345678, 0x1234, 0x5678, {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}};
325 GUID InheritedObjectType = {0x23456789, 0x2345, 0x6786, {0x2, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}};
327 OBJECTS_AND_NAME_ oan;
331 char szObjectTypeName[] = "ObjectTypeName";
332 char szInheritedObjectTypeName[] = "InheritedObjectTypeName";
333 char szTrusteeName[] = "szTrusteeName";
334 SID_IDENTIFIER_AUTHORITY auth = { {0x11,0x22,0,0,0, 0} };
336 memset( &ZeroGuid, 0x00, sizeof (ZeroGuid) );
338 pBuildTrusteeWithSidA = (void *)GetProcAddress( hmod, "BuildTrusteeWithSidA" );
339 pBuildTrusteeWithNameA = (void *)GetProcAddress( hmod, "BuildTrusteeWithNameA" );
340 pBuildTrusteeWithObjectsAndNameA = (void *)GetProcAddress (hmod, "BuildTrusteeWithObjectsAndNameA" );
341 pBuildTrusteeWithObjectsAndSidA = (void *)GetProcAddress (hmod, "BuildTrusteeWithObjectsAndSidA" );
342 pGetTrusteeNameA = (void *)GetProcAddress (hmod, "GetTrusteeNameA" );
343 if( !pBuildTrusteeWithSidA || !pBuildTrusteeWithNameA ||
344 !pBuildTrusteeWithObjectsAndNameA || !pBuildTrusteeWithObjectsAndSidA ||
348 if ( ! AllocateAndInitializeSid( &auth, 1, 42, 0,0,0,0,0,0,0,&psid ) )
350 trace( "failed to init SID\n" );
354 /* test BuildTrusteeWithSidA */
355 memset( &trustee, 0xff, sizeof trustee );
356 pBuildTrusteeWithSidA( &trustee, psid );
358 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
359 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
360 "MultipleTrusteeOperation wrong\n");
361 ok( trustee.TrusteeForm == TRUSTEE_IS_SID, "TrusteeForm wrong\n");
362 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
363 ok( trustee.ptstrName == psid, "ptstrName wrong\n" );
365 /* test BuildTrusteeWithObjectsAndSidA (test 1) */
366 memset( &trustee, 0xff, sizeof trustee );
367 memset( &oas, 0xff, sizeof(oas) );
368 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, &ObjectType,
369 &InheritedObjectType, psid);
371 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
372 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
373 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
374 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
375 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
377 ok(oas.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
378 ok(!memcmp(&oas.ObjectTypeGuid, &ObjectType, sizeof(GUID)), "ObjectTypeGuid wrong\n");
379 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
380 ok(oas.pSid == psid, "pSid wrong\n");
382 /* test GetTrusteeNameA */
383 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oas, "GetTrusteeName returned wrong value\n");
385 /* test BuildTrusteeWithObjectsAndSidA (test 2) */
386 memset( &trustee, 0xff, sizeof trustee );
387 memset( &oas, 0xff, sizeof(oas) );
388 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, NULL,
389 &InheritedObjectType, psid);
391 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
392 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
393 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
394 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
395 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
397 ok(oas.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
398 ok(!memcmp(&oas.ObjectTypeGuid, &ZeroGuid, sizeof(GUID)), "ObjectTypeGuid wrong\n");
399 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
400 ok(oas.pSid == psid, "pSid wrong\n");
404 /* test BuildTrusteeWithNameA */
405 memset( &trustee, 0xff, sizeof trustee );
406 pBuildTrusteeWithNameA( &trustee, szTrusteeName );
408 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
409 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
410 "MultipleTrusteeOperation wrong\n");
411 ok( trustee.TrusteeForm == TRUSTEE_IS_NAME, "TrusteeForm wrong\n");
412 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
413 ok( trustee.ptstrName == szTrusteeName, "ptstrName wrong\n" );
415 /* test BuildTrusteeWithObjectsAndNameA (test 1) */
416 memset( &trustee, 0xff, sizeof trustee );
417 memset( &oan, 0xff, sizeof(oan) );
418 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
419 szInheritedObjectTypeName, szTrusteeName);
421 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
422 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
423 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
424 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
425 ok(trustee.ptstrName == (LPTSTR)&oan, "ptstrName wrong\n");
427 ok(oan.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
428 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
429 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
430 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
432 /* test GetTrusteeNameA */
433 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oan, "GetTrusteeName returned wrong value\n");
435 /* test BuildTrusteeWithObjectsAndNameA (test 2) */
436 memset( &trustee, 0xff, sizeof trustee );
437 memset( &oan, 0xff, sizeof(oan) );
438 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, NULL,
439 szInheritedObjectTypeName, szTrusteeName);
441 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
442 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
443 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
444 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
445 ok(trustee.ptstrName == (LPSTR)&oan, "ptstrName wrong\n");
447 ok(oan.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
448 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
449 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
450 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
452 /* test BuildTrusteeWithObjectsAndNameA (test 3) */
453 memset( &trustee, 0xff, sizeof trustee );
454 memset( &oan, 0xff, sizeof(oan) );
455 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
456 NULL, szTrusteeName);
458 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
459 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
460 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
461 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
462 ok(trustee.ptstrName == (LPTSTR)&oan, "ptstrName wrong\n");
464 ok(oan.ObjectsPresent == ACE_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
465 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
466 ok(oan.InheritedObjectTypeName == NULL, "InheritedObjectTypeName wrong\n");
467 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
470 /* If the first isn't defined, assume none is */
471 #ifndef SE_MIN_WELL_KNOWN_PRIVILEGE
472 #define SE_MIN_WELL_KNOWN_PRIVILEGE 2L
473 #define SE_CREATE_TOKEN_PRIVILEGE 2L
474 #define SE_ASSIGNPRIMARYTOKEN_PRIVILEGE 3L
475 #define SE_LOCK_MEMORY_PRIVILEGE 4L
476 #define SE_INCREASE_QUOTA_PRIVILEGE 5L
477 #define SE_MACHINE_ACCOUNT_PRIVILEGE 6L
478 #define SE_TCB_PRIVILEGE 7L
479 #define SE_SECURITY_PRIVILEGE 8L
480 #define SE_TAKE_OWNERSHIP_PRIVILEGE 9L
481 #define SE_LOAD_DRIVER_PRIVILEGE 10L
482 #define SE_SYSTEM_PROFILE_PRIVILEGE 11L
483 #define SE_SYSTEMTIME_PRIVILEGE 12L
484 #define SE_PROF_SINGLE_PROCESS_PRIVILEGE 13L
485 #define SE_INC_BASE_PRIORITY_PRIVILEGE 14L
486 #define SE_CREATE_PAGEFILE_PRIVILEGE 15L
487 #define SE_CREATE_PERMANENT_PRIVILEGE 16L
488 #define SE_BACKUP_PRIVILEGE 17L
489 #define SE_RESTORE_PRIVILEGE 18L
490 #define SE_SHUTDOWN_PRIVILEGE 19L
491 #define SE_DEBUG_PRIVILEGE 20L
492 #define SE_AUDIT_PRIVILEGE 21L
493 #define SE_SYSTEM_ENVIRONMENT_PRIVILEGE 22L
494 #define SE_CHANGE_NOTIFY_PRIVILEGE 23L
495 #define SE_REMOTE_SHUTDOWN_PRIVILEGE 24L
496 #define SE_UNDOCK_PRIVILEGE 25L
497 #define SE_SYNC_AGENT_PRIVILEGE 26L
498 #define SE_ENABLE_DELEGATION_PRIVILEGE 27L
499 #define SE_MANAGE_VOLUME_PRIVILEGE 28L
500 #define SE_IMPERSONATE_PRIVILEGE 29L
501 #define SE_CREATE_GLOBAL_PRIVILEGE 30L
502 #define SE_MAX_WELL_KNOWN_PRIVILEGE SE_CREATE_GLOBAL_PRIVILEGE
503 #endif /* ndef SE_MIN_WELL_KNOWN_PRIVILEGE */
505 static void test_allocateLuid(void)
507 BOOL (WINAPI *pAllocateLocallyUniqueId)(PLUID);
511 pAllocateLocallyUniqueId = (void*)GetProcAddress(hmod, "AllocateLocallyUniqueId");
512 if (!pAllocateLocallyUniqueId) return;
514 ret = pAllocateLocallyUniqueId(&luid1);
515 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
519 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
520 ret = pAllocateLocallyUniqueId(&luid2);
522 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
523 ok(luid1.LowPart > SE_MAX_WELL_KNOWN_PRIVILEGE || luid1.HighPart != 0,
524 "AllocateLocallyUniqueId returned a well-known LUID\n");
525 ok(luid1.LowPart != luid2.LowPart || luid1.HighPart != luid2.HighPart,
526 "AllocateLocallyUniqueId returned non-unique LUIDs\n");
527 ret = pAllocateLocallyUniqueId(NULL);
528 ok( !ret && GetLastError() == ERROR_NOACCESS,
529 "AllocateLocallyUniqueId(NULL) didn't return ERROR_NOACCESS: %d\n",
533 static void test_lookupPrivilegeName(void)
535 BOOL (WINAPI *pLookupPrivilegeNameA)(LPCSTR, PLUID, LPSTR, LPDWORD);
536 char buf[MAX_PATH]; /* arbitrary, seems long enough */
537 DWORD cchName = sizeof(buf);
538 LUID luid = { 0, 0 };
542 /* check whether it's available first */
543 pLookupPrivilegeNameA = (void*)GetProcAddress(hmod, "LookupPrivilegeNameA");
544 if (!pLookupPrivilegeNameA) return;
545 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
546 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
547 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
550 /* check with a short buffer */
552 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
553 ret = pLookupPrivilegeNameA(NULL, &luid, NULL, &cchName);
554 ok( !ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
555 "LookupPrivilegeNameA didn't fail with ERROR_INSUFFICIENT_BUFFER: %d\n",
557 ok(cchName == strlen("SeCreateTokenPrivilege") + 1,
558 "LookupPrivilegeNameA returned an incorrect required length for\n"
559 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName,
560 lstrlenA("SeCreateTokenPrivilege") + 1);
561 /* check a known value and its returned length on success */
562 cchName = sizeof(buf);
563 ok(pLookupPrivilegeNameA(NULL, &luid, buf, &cchName) &&
564 cchName == strlen("SeCreateTokenPrivilege"),
565 "LookupPrivilegeNameA returned an incorrect output length for\n"
566 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName,
567 (int)strlen("SeCreateTokenPrivilege"));
568 /* check known values */
569 for (i = SE_MIN_WELL_KNOWN_PRIVILEGE; i <= SE_MAX_WELL_KNOWN_PRIVILEGE; i++)
572 cchName = sizeof(buf);
573 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
574 ok( ret || GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
575 "LookupPrivilegeNameA(0.%d) failed: %d\n", i, GetLastError());
577 /* check a bogus LUID */
578 luid.LowPart = 0xdeadbeef;
579 cchName = sizeof(buf);
580 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
581 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
582 "LookupPrivilegeNameA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
584 /* check on a bogus system */
585 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
586 cchName = sizeof(buf);
587 ret = pLookupPrivilegeNameA("b0gu5.Nam3", &luid, buf, &cchName);
588 ok( !ret && (GetLastError() == RPC_S_SERVER_UNAVAILABLE ||
589 GetLastError() == RPC_S_INVALID_NET_ADDR) /* w2k8 */,
590 "LookupPrivilegeNameA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
600 static void test_lookupPrivilegeValue(void)
602 static const struct NameToLUID privs[] = {
603 { "SeCreateTokenPrivilege", SE_CREATE_TOKEN_PRIVILEGE },
604 { "SeAssignPrimaryTokenPrivilege", SE_ASSIGNPRIMARYTOKEN_PRIVILEGE },
605 { "SeLockMemoryPrivilege", SE_LOCK_MEMORY_PRIVILEGE },
606 { "SeIncreaseQuotaPrivilege", SE_INCREASE_QUOTA_PRIVILEGE },
607 { "SeMachineAccountPrivilege", SE_MACHINE_ACCOUNT_PRIVILEGE },
608 { "SeTcbPrivilege", SE_TCB_PRIVILEGE },
609 { "SeSecurityPrivilege", SE_SECURITY_PRIVILEGE },
610 { "SeTakeOwnershipPrivilege", SE_TAKE_OWNERSHIP_PRIVILEGE },
611 { "SeLoadDriverPrivilege", SE_LOAD_DRIVER_PRIVILEGE },
612 { "SeSystemProfilePrivilege", SE_SYSTEM_PROFILE_PRIVILEGE },
613 { "SeSystemtimePrivilege", SE_SYSTEMTIME_PRIVILEGE },
614 { "SeProfileSingleProcessPrivilege", SE_PROF_SINGLE_PROCESS_PRIVILEGE },
615 { "SeIncreaseBasePriorityPrivilege", SE_INC_BASE_PRIORITY_PRIVILEGE },
616 { "SeCreatePagefilePrivilege", SE_CREATE_PAGEFILE_PRIVILEGE },
617 { "SeCreatePermanentPrivilege", SE_CREATE_PERMANENT_PRIVILEGE },
618 { "SeBackupPrivilege", SE_BACKUP_PRIVILEGE },
619 { "SeRestorePrivilege", SE_RESTORE_PRIVILEGE },
620 { "SeShutdownPrivilege", SE_SHUTDOWN_PRIVILEGE },
621 { "SeDebugPrivilege", SE_DEBUG_PRIVILEGE },
622 { "SeAuditPrivilege", SE_AUDIT_PRIVILEGE },
623 { "SeSystemEnvironmentPrivilege", SE_SYSTEM_ENVIRONMENT_PRIVILEGE },
624 { "SeChangeNotifyPrivilege", SE_CHANGE_NOTIFY_PRIVILEGE },
625 { "SeRemoteShutdownPrivilege", SE_REMOTE_SHUTDOWN_PRIVILEGE },
626 { "SeUndockPrivilege", SE_UNDOCK_PRIVILEGE },
627 { "SeSyncAgentPrivilege", SE_SYNC_AGENT_PRIVILEGE },
628 { "SeEnableDelegationPrivilege", SE_ENABLE_DELEGATION_PRIVILEGE },
629 { "SeManageVolumePrivilege", SE_MANAGE_VOLUME_PRIVILEGE },
630 { "SeImpersonatePrivilege", SE_IMPERSONATE_PRIVILEGE },
631 { "SeCreateGlobalPrivilege", SE_CREATE_GLOBAL_PRIVILEGE },
633 BOOL (WINAPI *pLookupPrivilegeValueA)(LPCSTR, LPCSTR, PLUID);
638 /* check whether it's available first */
639 pLookupPrivilegeValueA = (void*)GetProcAddress(hmod, "LookupPrivilegeValueA");
640 if (!pLookupPrivilegeValueA) return;
641 ret = pLookupPrivilegeValueA(NULL, "SeCreateTokenPrivilege", &luid);
642 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
645 /* check a bogus system name */
646 ret = pLookupPrivilegeValueA("b0gu5.Nam3", "SeCreateTokenPrivilege", &luid);
647 ok( !ret && (GetLastError() == RPC_S_SERVER_UNAVAILABLE ||
648 GetLastError() == RPC_S_INVALID_NET_ADDR) /* w2k8 */,
649 "LookupPrivilegeValueA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
651 /* check a NULL string */
652 ret = pLookupPrivilegeValueA(NULL, 0, &luid);
653 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
654 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
656 /* check a bogus privilege name */
657 ret = pLookupPrivilegeValueA(NULL, "SeBogusPrivilege", &luid);
658 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
659 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
661 /* check case insensitive */
662 ret = pLookupPrivilegeValueA(NULL, "sEcREATEtOKENpRIVILEGE", &luid);
664 "LookupPrivilegeValueA(NULL, sEcREATEtOKENpRIVILEGE, &luid) failed: %d\n",
666 for (i = 0; i < sizeof(privs) / sizeof(privs[0]); i++)
668 /* Not all privileges are implemented on all Windows versions, so
669 * don't worry if the call fails
671 if (pLookupPrivilegeValueA(NULL, privs[i].name, &luid))
673 ok(luid.LowPart == privs[i].lowPart,
674 "LookupPrivilegeValueA returned an invalid LUID for %s\n",
680 static void test_luid(void)
683 test_lookupPrivilegeName();
684 test_lookupPrivilegeValue();
687 static void test_FileSecurity(void)
689 char wintmpdir [MAX_PATH];
690 char path [MAX_PATH];
691 char file [MAX_PATH];
697 SECURITY_INFORMATION const request = OWNER_SECURITY_INFORMATION
698 | GROUP_SECURITY_INFORMATION
699 | DACL_SECURITY_INFORMATION;
701 if (!pGetFileSecurityA) {
702 win_skip ("GetFileSecurity is not available\n");
706 if (!pSetFileSecurityA) {
707 win_skip ("SetFileSecurity is not available\n");
711 if (!GetTempPathA (sizeof (wintmpdir), wintmpdir)) {
712 win_skip ("GetTempPathA failed\n");
716 /* Create a temporary directory and in it a temporary file */
717 strcat (strcpy (path, wintmpdir), "rary");
718 SetLastError(0xdeadbeef);
719 rc = CreateDirectoryA (path, NULL);
720 ok (rc || GetLastError() == ERROR_ALREADY_EXISTS, "CreateDirectoryA "
721 "failed for '%s' with %d\n", path, GetLastError());
723 strcat (strcpy (file, path), "\\ess");
724 SetLastError(0xdeadbeef);
725 fh = CreateFileA (file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
726 ok (fh != INVALID_HANDLE_VALUE, "CreateFileA "
727 "failed for '%s' with %d\n", file, GetLastError());
730 /* For the temporary file ... */
732 /* Get size needed */
734 SetLastError(0xdeadbeef);
735 rc = pGetFileSecurityA (file, request, NULL, 0, &retSize);
736 if (!rc && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)) {
737 win_skip("GetFileSecurityA is not implemented\n");
740 ok (!rc, "GetFileSecurityA "
741 "was expected to fail for '%s'\n", file);
742 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetFileSecurityA "
743 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
744 ok (retSize > sizeof (SECURITY_DESCRIPTOR), "GetFileSecurityA returned size %d\n", retSize);
747 sd = HeapAlloc (GetProcessHeap (), 0, sdSize);
749 /* Get security descriptor for real */
751 SetLastError(0xdeadbeef);
752 rc = pGetFileSecurityA (file, request, sd, sdSize, &retSize);
753 ok (rc, "GetFileSecurityA "
754 "was not expected to fail '%s': %d\n", file, GetLastError());
755 ok (retSize == sdSize ||
756 broken(retSize == 0), /* NT4 */
757 "GetFileSecurityA returned size %d; expected %d\n", retSize, sdSize);
759 /* Use it to set security descriptor */
760 SetLastError(0xdeadbeef);
761 rc = pSetFileSecurityA (file, request, sd);
762 ok (rc, "SetFileSecurityA "
763 "was not expected to fail '%s': %d\n", file, GetLastError());
765 HeapFree (GetProcessHeap (), 0, sd);
767 /* Repeat for the temporary directory ... */
769 /* Get size needed */
771 SetLastError(0xdeadbeef);
772 rc = pGetFileSecurityA (path, request, NULL, 0, &retSize);
773 ok (!rc, "GetFileSecurityA "
774 "was expected to fail for '%s'\n", path);
775 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetFileSecurityA "
776 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
777 ok (retSize > sizeof (SECURITY_DESCRIPTOR), "GetFileSecurityA returned size %d\n", retSize);
780 sd = HeapAlloc (GetProcessHeap (), 0, sdSize);
782 /* Get security descriptor for real */
784 SetLastError(0xdeadbeef);
785 rc = pGetFileSecurityA (path, request, sd, sdSize, &retSize);
786 ok (rc, "GetFileSecurityA "
787 "was not expected to fail '%s': %d\n", path, GetLastError());
788 ok (retSize == sdSize ||
789 broken(retSize == 0), /* NT4 */
790 "GetFileSecurityA returned size %d; expected %d\n", retSize, sdSize);
792 /* Use it to set security descriptor */
793 SetLastError(0xdeadbeef);
794 rc = pSetFileSecurityA (path, request, sd);
795 ok (rc, "SetFileSecurityA "
796 "was not expected to fail '%s': %d\n", path, GetLastError());
797 HeapFree (GetProcessHeap (), 0, sd);
800 strcpy (wintmpdir, "\\Should not exist");
801 SetLastError(0xdeadbeef);
802 rc = pGetFileSecurityA (wintmpdir, OWNER_SECURITY_INFORMATION, NULL, 0, &sdSize);
803 ok (!rc, "GetFileSecurityA should fail for not existing directories/files\n");
804 ok (GetLastError() == ERROR_FILE_NOT_FOUND,
805 "last error ERROR_FILE_NOT_FOUND expected, got %d\n", GetLastError());
808 /* Remove temporary file and directory */
810 RemoveDirectoryA(path);
813 static void test_AccessCheck(void)
815 PSID EveryoneSid = NULL, AdminSid = NULL, UsersSid = NULL;
817 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL;
818 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
819 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
820 GENERIC_MAPPING Mapping = { KEY_READ, KEY_WRITE, KEY_EXECUTE, KEY_ALL_ACCESS };
827 PRIVILEGE_SET *PrivSet;
832 NTSTATUS ntret, ntAccessStatus;
834 NtDllModule = GetModuleHandle("ntdll.dll");
837 skip("not running on NT, skipping test\n");
840 pRtlAdjustPrivilege = (void *)GetProcAddress(NtDllModule, "RtlAdjustPrivilege");
841 if (!pRtlAdjustPrivilege)
843 win_skip("missing RtlAdjustPrivilege, skipping test\n");
847 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
848 res = InitializeAcl(Acl, 256, ACL_REVISION);
849 if(!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
851 skip("ACLs not implemented - skipping tests\n");
852 HeapFree(GetProcessHeap(), 0, Acl);
855 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
857 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
858 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
860 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
861 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdminSid);
862 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
864 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
865 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &UsersSid);
866 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
868 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
870 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
871 ok(res, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
873 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
874 ok(res, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
876 PrivSetLen = FIELD_OFFSET(PRIVILEGE_SET, Privilege[16]);
877 PrivSet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, PrivSetLen);
878 PrivSet->PrivilegeCount = 16;
880 res = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &ProcessToken);
881 ok(res, "OpenProcessToken failed with error %d\n", GetLastError());
883 pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, FALSE, TRUE, &Enabled);
885 res = DuplicateToken(ProcessToken, SecurityImpersonation, &Token);
886 ok(res, "DuplicateToken failed with error %d\n", GetLastError());
888 /* SD without owner/group */
889 SetLastError(0xdeadbeef);
890 Access = AccessStatus = 0x1abe11ed;
891 ret = AccessCheck(SecurityDescriptor, Token, KEY_QUERY_VALUE, &Mapping,
892 PrivSet, &PrivSetLen, &Access, &AccessStatus);
893 err = GetLastError();
894 ok(!ret && err == ERROR_INVALID_SECURITY_DESCR, "AccessCheck should have "
895 "failed with ERROR_INVALID_SECURITY_DESCR, instead of %d\n", err);
896 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
897 "Access and/or AccessStatus were changed!\n");
899 /* Set owner and group */
900 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
901 ok(res, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
902 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, TRUE);
903 ok(res, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
905 /* Generic access mask */
906 SetLastError(0xdeadbeef);
907 Access = AccessStatus = 0x1abe11ed;
908 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
909 PrivSet, &PrivSetLen, &Access, &AccessStatus);
910 err = GetLastError();
911 ok(!ret && err == ERROR_GENERIC_NOT_MAPPED, "AccessCheck should have failed "
912 "with ERROR_GENERIC_NOT_MAPPED, instead of %d\n", err);
913 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
914 "Access and/or AccessStatus were changed!\n");
916 /* Generic access mask - no privilegeset buffer */
917 SetLastError(0xdeadbeef);
918 Access = AccessStatus = 0x1abe11ed;
919 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
920 NULL, &PrivSetLen, &Access, &AccessStatus);
921 err = GetLastError();
922 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
923 "with ERROR_NOACCESS, instead of %d\n", err);
924 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
925 "Access and/or AccessStatus were changed!\n");
927 /* Generic access mask - no returnlength */
928 SetLastError(0xdeadbeef);
929 Access = AccessStatus = 0x1abe11ed;
930 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
931 PrivSet, NULL, &Access, &AccessStatus);
932 err = GetLastError();
933 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
934 "with ERROR_NOACCESS, instead of %d\n", err);
935 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
936 "Access and/or AccessStatus were changed!\n");
938 /* Generic access mask - no privilegeset buffer, no returnlength */
939 SetLastError(0xdeadbeef);
940 Access = AccessStatus = 0x1abe11ed;
941 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
942 NULL, NULL, &Access, &AccessStatus);
943 err = GetLastError();
944 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
945 "with ERROR_NOACCESS, instead of %d\n", err);
946 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
947 "Access and/or AccessStatus were changed!\n");
949 /* sd with no dacl present */
950 Access = AccessStatus = 0x1abe11ed;
951 ret = SetSecurityDescriptorDacl(SecurityDescriptor, FALSE, NULL, FALSE);
952 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
953 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
954 PrivSet, &PrivSetLen, &Access, &AccessStatus);
955 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
956 ok(AccessStatus && (Access == KEY_READ),
957 "AccessCheck failed to grant access with error %d\n",
960 /* sd with no dacl present - no privilegeset buffer */
961 SetLastError(0xdeadbeef);
962 Access = AccessStatus = 0x1abe11ed;
963 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
964 NULL, &PrivSetLen, &Access, &AccessStatus);
965 err = GetLastError();
966 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
967 "with ERROR_NOACCESS, instead of %d\n", err);
968 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
969 "Access and/or AccessStatus were changed!\n");
973 /* Generic access mask - no privilegeset buffer */
974 SetLastError(0xdeadbeef);
975 Access = ntAccessStatus = 0x1abe11ed;
976 ntret = pNtAccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
977 NULL, &PrivSetLen, &Access, &ntAccessStatus);
978 err = GetLastError();
979 ok(ntret == STATUS_ACCESS_VIOLATION,
980 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret);
981 ok(err == 0xdeadbeef,
982 "NtAccessCheck shouldn't set last error, got %d\n", err);
983 ok(Access == 0x1abe11ed && ntAccessStatus == 0x1abe11ed,
984 "Access and/or AccessStatus were changed!\n");
986 /* Generic access mask - no returnlength */
987 SetLastError(0xdeadbeef);
988 Access = ntAccessStatus = 0x1abe11ed;
989 ntret = pNtAccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
990 PrivSet, NULL, &Access, &ntAccessStatus);
991 err = GetLastError();
992 ok(ntret == STATUS_ACCESS_VIOLATION,
993 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret);
994 ok(err == 0xdeadbeef,
995 "NtAccessCheck shouldn't set last error, got %d\n", err);
996 ok(Access == 0x1abe11ed && ntAccessStatus == 0x1abe11ed,
997 "Access and/or AccessStatus were changed!\n");
999 /* Generic access mask - no privilegeset buffer, no returnlength */
1000 SetLastError(0xdeadbeef);
1001 Access = ntAccessStatus = 0x1abe11ed;
1002 ntret = pNtAccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1003 NULL, NULL, &Access, &ntAccessStatus);
1004 err = GetLastError();
1005 ok(ntret == STATUS_ACCESS_VIOLATION,
1006 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret);
1007 ok(err == 0xdeadbeef,
1008 "NtAccessCheck shouldn't set last error, got %d\n", err);
1009 ok(Access == 0x1abe11ed && ntAccessStatus == 0x1abe11ed,
1010 "Access and/or AccessStatus were changed!\n");
1013 win_skip("NtAccessCheck unavailable. Skipping.\n");
1015 /* sd with NULL dacl */
1016 Access = AccessStatus = 0x1abe11ed;
1017 ret = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, NULL, FALSE);
1018 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1019 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1020 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1021 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1022 ok(AccessStatus && (Access == KEY_READ),
1023 "AccessCheck failed to grant access with error %d\n",
1026 /* sd with blank dacl */
1027 ret = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
1028 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1029 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1030 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1031 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1032 err = GetLastError();
1033 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
1034 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
1035 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
1037 res = AddAccessAllowedAce(Acl, ACL_REVISION, KEY_READ, EveryoneSid);
1038 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
1040 res = AddAccessDeniedAce(Acl, ACL_REVISION, KEY_SET_VALUE, AdminSid);
1041 ok(res, "AddAccessDeniedAce failed with error %d\n", GetLastError());
1044 Access = AccessStatus = 0x1abe11ed;
1045 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1046 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1047 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1048 ok(AccessStatus && (Access == KEY_READ),
1049 "AccessCheck failed to grant access with error %d\n",
1052 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
1053 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1054 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1056 "AccessCheck failed to grant any access with error %d\n",
1058 trace("AccessCheck with MAXIMUM_ALLOWED got Access 0x%08x\n", Access);
1060 /* Access denied by SD */
1061 SetLastError(0xdeadbeef);
1062 Access = AccessStatus = 0x1abe11ed;
1063 ret = AccessCheck(SecurityDescriptor, Token, KEY_WRITE, &Mapping,
1064 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1065 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1066 err = GetLastError();
1067 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
1068 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
1069 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
1072 PrivSet->PrivilegeCount = 16;
1073 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
1074 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1075 ok(ret && !AccessStatus && GetLastError() == ERROR_PRIVILEGE_NOT_HELD,
1076 "AccessCheck should have failed with ERROR_PRIVILEGE_NOT_HELD, instead of %d\n",
1079 ret = ImpersonateLoggedOnUser(Token);
1080 ok(ret, "ImpersonateLoggedOnUser failed with error %d\n", GetLastError());
1081 ret = pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, TRUE, TRUE, &Enabled);
1085 PrivSet->PrivilegeCount = 16;
1086 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
1087 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1088 ok(ret && AccessStatus && GetLastError() == 0,
1089 "AccessCheck should have succeeded, error %d\n",
1091 ok(Access == ACCESS_SYSTEM_SECURITY,
1092 "Access should be equal to ACCESS_SYSTEM_SECURITY instead of 0x%08x\n",
1096 trace("Couldn't get SE_SECURITY_PRIVILEGE (0x%08x), skipping ACCESS_SYSTEM_SECURITY test\n",
1098 ret = RevertToSelf();
1099 ok(ret, "RevertToSelf failed with error %d\n", GetLastError());
1101 /* test INHERIT_ONLY_ACE */
1102 ret = InitializeAcl(Acl, 256, ACL_REVISION);
1103 ok(ret, "InitializeAcl failed with error %d\n", GetLastError());
1105 /* NT doesn't have AddAccessAllowedAceEx. Skipping this call/test doesn't influence
1108 if (pAddAccessAllowedAceEx)
1110 ret = pAddAccessAllowedAceEx(Acl, ACL_REVISION, INHERIT_ONLY_ACE, KEY_READ, EveryoneSid);
1111 ok(ret, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
1114 win_skip("AddAccessAllowedAceEx is not available\n");
1116 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1117 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1118 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1119 err = GetLastError();
1120 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
1121 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
1122 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
1126 res = DuplicateToken(ProcessToken, SecurityAnonymous, &Token);
1127 ok(res, "DuplicateToken failed with error %d\n", GetLastError());
1129 SetLastError(0xdeadbeef);
1130 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
1131 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1132 err = GetLastError();
1133 ok(!ret && err == ERROR_BAD_IMPERSONATION_LEVEL, "AccessCheck should have failed "
1134 "with ERROR_BAD_IMPERSONATION_LEVEL, instead of %d\n", err);
1138 SetLastError(0xdeadbeef);
1139 ret = AccessCheck(SecurityDescriptor, ProcessToken, KEY_READ, &Mapping,
1140 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1141 err = GetLastError();
1142 ok(!ret && err == ERROR_NO_IMPERSONATION_TOKEN, "AccessCheck should have failed "
1143 "with ERROR_NO_IMPERSONATION_TOKEN, instead of %d\n", err);
1145 CloseHandle(ProcessToken);
1148 FreeSid(EveryoneSid);
1153 HeapFree(GetProcessHeap(), 0, Acl);
1154 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
1155 HeapFree(GetProcessHeap(), 0, PrivSet);
1158 /* test GetTokenInformation for the various attributes */
1159 static void test_token_attr(void)
1161 HANDLE Token, ImpersonationToken;
1163 TOKEN_PRIVILEGES *Privileges;
1164 TOKEN_GROUPS *Groups;
1166 TOKEN_DEFAULT_DACL *Dacl;
1170 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel;
1173 /* cygwin-like use case */
1174 SetLastError(0xdeadbeef);
1175 ret = OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED, &Token);
1176 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1178 win_skip("OpenProcessToken is not implemented\n");
1181 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1184 DWORD buf[256]; /* GetTokenInformation wants a dword-aligned buffer */
1186 ret = GetTokenInformation(Token, TokenUser,(void*)buf, Size, &Size);
1187 ok(ret, "GetTokenInformation failed with error %d\n", GetLastError());
1188 Size = sizeof(ImpersonationLevel);
1189 ret = GetTokenInformation(Token, TokenImpersonationLevel, &ImpersonationLevel, Size, &Size);
1190 GLE = GetLastError();
1191 ok(!ret && (GLE == ERROR_INVALID_PARAMETER), "GetTokenInformation(TokenImpersonationLevel) on primary token should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GLE);
1195 if(!pConvertSidToStringSidA)
1197 win_skip("ConvertSidToStringSidA is not available\n");
1201 SetLastError(0xdeadbeef);
1202 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &Token);
1203 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1206 SetLastError(0xdeadbeef);
1207 ret = GetTokenInformation(Token, TokenGroups, NULL, 0, &Size);
1208 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1209 "GetTokenInformation(TokenGroups) %s with error %d\n",
1210 ret ? "succeeded" : "failed", GetLastError());
1211 Groups = HeapAlloc(GetProcessHeap(), 0, Size);
1212 SetLastError(0xdeadbeef);
1213 ret = GetTokenInformation(Token, TokenGroups, Groups, Size, &Size);
1214 ok(ret, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
1215 ok(GetLastError() == 0xdeadbeef,
1216 "GetTokenInformation shouldn't have set last error to %d\n",
1218 trace("TokenGroups:\n");
1219 for (i = 0; i < Groups->GroupCount; i++)
1221 DWORD NameLength = 255;
1223 DWORD DomainLength = 255;
1225 SID_NAME_USE SidNameUse;
1228 ret = LookupAccountSid(NULL, Groups->Groups[i].Sid, Name, &NameLength, Domain, &DomainLength, &SidNameUse);
1231 pConvertSidToStringSidA(Groups->Groups[i].Sid, &SidString);
1232 trace("%s, %s\\%s use: %d attr: 0x%08x\n", SidString, Domain, Name, SidNameUse, Groups->Groups[i].Attributes);
1233 LocalFree(SidString);
1235 else trace("attr: 0x%08x LookupAccountSid failed with error %d\n", Groups->Groups[i].Attributes, GetLastError());
1237 HeapFree(GetProcessHeap(), 0, Groups);
1240 ret = GetTokenInformation(Token, TokenUser, NULL, 0, &Size);
1241 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1242 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1243 User = HeapAlloc(GetProcessHeap(), 0, Size);
1244 ret = GetTokenInformation(Token, TokenUser, User, Size, &Size);
1246 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1248 pConvertSidToStringSidA(User->User.Sid, &SidString);
1249 trace("TokenUser: %s attr: 0x%08x\n", SidString, User->User.Attributes);
1250 LocalFree(SidString);
1251 HeapFree(GetProcessHeap(), 0, User);
1254 ret = GetTokenInformation(Token, TokenPrivileges, NULL, 0, &Size);
1255 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1256 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1257 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
1258 ret = GetTokenInformation(Token, TokenPrivileges, Privileges, Size, &Size);
1260 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1261 trace("TokenPrivileges:\n");
1262 for (i = 0; i < Privileges->PrivilegeCount; i++)
1265 DWORD NameLen = sizeof(Name)/sizeof(Name[0]);
1266 LookupPrivilegeName(NULL, &Privileges->Privileges[i].Luid, Name, &NameLen);
1267 trace("\t%s, 0x%x\n", Name, Privileges->Privileges[i].Attributes);
1269 HeapFree(GetProcessHeap(), 0, Privileges);
1271 ret = DuplicateToken(Token, SecurityAnonymous, &ImpersonationToken);
1272 ok(ret, "DuplicateToken failed with error %d\n", GetLastError());
1274 Size = sizeof(ImpersonationLevel);
1275 ret = GetTokenInformation(ImpersonationToken, TokenImpersonationLevel, &ImpersonationLevel, Size, &Size);
1276 ok(ret, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1277 ok(ImpersonationLevel == SecurityAnonymous, "ImpersonationLevel should have been SecurityAnonymous instead of %d\n", ImpersonationLevel);
1279 CloseHandle(ImpersonationToken);
1282 ret = GetTokenInformation(Token, TokenDefaultDacl, NULL, 0, &Size);
1283 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1284 "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1286 Dacl = HeapAlloc(GetProcessHeap(), 0, Size);
1287 ret = GetTokenInformation(Token, TokenDefaultDacl, Dacl, Size, &Size);
1288 ok(ret, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1290 SetLastError(0xdeadbeef);
1291 ret = SetTokenInformation(Token, TokenDefaultDacl, NULL, 0);
1292 GLE = GetLastError();
1293 ok(!ret, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1294 ok(GLE == ERROR_BAD_LENGTH, "expected ERROR_BAD_LENGTH got %u\n", GLE);
1296 SetLastError(0xdeadbeef);
1297 ret = SetTokenInformation(Token, TokenDefaultDacl, NULL, Size);
1298 GLE = GetLastError();
1299 ok(!ret, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1300 ok(GLE == ERROR_NOACCESS, "expected ERROR_NOACCESS got %u\n", GLE);
1302 acl = Dacl->DefaultDacl;
1303 Dacl->DefaultDacl = NULL;
1305 ret = SetTokenInformation(Token, TokenDefaultDacl, Dacl, Size);
1306 ok(ret, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1309 Dacl->DefaultDacl = (ACL *)0xdeadbeef;
1310 ret = GetTokenInformation(Token, TokenDefaultDacl, Dacl, Size, &Size2);
1311 ok(ret, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1312 ok(Dacl->DefaultDacl == NULL, "expected NULL, got %p\n", Dacl->DefaultDacl);
1313 ok(Size2 == sizeof(TOKEN_DEFAULT_DACL), "got %u expected sizeof(TOKEN_DEFAULT_DACL)\n", Size2);
1315 Dacl->DefaultDacl = acl;
1316 ret = SetTokenInformation(Token, TokenDefaultDacl, Dacl, Size);
1317 ok(ret, "SetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1319 ret = GetTokenInformation(Token, TokenDefaultDacl, Dacl, Size, &Size2);
1320 ok(ret, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1322 HeapFree(GetProcessHeap(), 0, Dacl);
1326 typedef union _MAX_SID
1329 char max[SECURITY_MAX_SID_SIZE];
1332 static void test_sid_str(PSID * sid)
1335 BOOL ret = pConvertSidToStringSidA(sid, &str_sid);
1336 ok(ret, "ConvertSidToStringSidA() failed: %d\n", GetLastError());
1339 char account[MAX_PATH], domain[MAX_PATH];
1341 DWORD acc_size = MAX_PATH;
1342 DWORD dom_size = MAX_PATH;
1343 ret = LookupAccountSid(NULL, sid, account, &acc_size, domain, &dom_size, &use);
1344 ok(ret || (!ret && (GetLastError() == ERROR_NONE_MAPPED)),
1345 "LookupAccountSid(%s) failed: %d\n", str_sid, GetLastError());
1347 trace(" %s %s\\%s %d\n", str_sid, domain, account, use);
1348 else if (GetLastError() == ERROR_NONE_MAPPED)
1349 trace(" %s couldn't be mapped\n", str_sid);
1354 static const struct well_known_sid_value
1356 BOOL without_domain;
1357 const char *sid_string;
1358 } well_known_sid_values[] = {
1359 /* 0 */ {TRUE, "S-1-0-0"}, {TRUE, "S-1-1-0"}, {TRUE, "S-1-2-0"}, {TRUE, "S-1-3-0"},
1360 /* 4 */ {TRUE, "S-1-3-1"}, {TRUE, "S-1-3-2"}, {TRUE, "S-1-3-3"}, {TRUE, "S-1-5"},
1361 /* 8 */ {FALSE, "S-1-5-1"}, {TRUE, "S-1-5-2"}, {TRUE, "S-1-5-3"}, {TRUE, "S-1-5-4"},
1362 /* 12 */ {TRUE, "S-1-5-6"}, {TRUE, "S-1-5-7"}, {TRUE, "S-1-5-8"}, {TRUE, "S-1-5-9"},
1363 /* 16 */ {TRUE, "S-1-5-10"}, {TRUE, "S-1-5-11"}, {TRUE, "S-1-5-12"}, {TRUE, "S-1-5-13"},
1364 /* 20 */ {TRUE, "S-1-5-14"}, {FALSE, NULL}, {TRUE, "S-1-5-18"}, {TRUE, "S-1-5-19"},
1365 /* 24 */ {TRUE, "S-1-5-20"}, {TRUE, "S-1-5-32"},
1366 /* 26 */ {FALSE, "S-1-5-32-544"}, {TRUE, "S-1-5-32-545"}, {TRUE, "S-1-5-32-546"},
1367 /* 29 */ {TRUE, "S-1-5-32-547"}, {TRUE, "S-1-5-32-548"}, {TRUE, "S-1-5-32-549"},
1368 /* 32 */ {TRUE, "S-1-5-32-550"}, {TRUE, "S-1-5-32-551"}, {TRUE, "S-1-5-32-552"},
1369 /* 35 */ {TRUE, "S-1-5-32-554"}, {TRUE, "S-1-5-32-555"}, {TRUE, "S-1-5-32-556"},
1370 /* 38 */ {FALSE, "S-1-5-21-12-23-34-45-56-500"}, {FALSE, "S-1-5-21-12-23-34-45-56-501"},
1371 /* 40 */ {FALSE, "S-1-5-21-12-23-34-45-56-502"}, {FALSE, "S-1-5-21-12-23-34-45-56-512"},
1372 /* 42 */ {FALSE, "S-1-5-21-12-23-34-45-56-513"}, {FALSE, "S-1-5-21-12-23-34-45-56-514"},
1373 /* 44 */ {FALSE, "S-1-5-21-12-23-34-45-56-515"}, {FALSE, "S-1-5-21-12-23-34-45-56-516"},
1374 /* 46 */ {FALSE, "S-1-5-21-12-23-34-45-56-517"}, {FALSE, "S-1-5-21-12-23-34-45-56-518"},
1375 /* 48 */ {FALSE, "S-1-5-21-12-23-34-45-56-519"}, {FALSE, "S-1-5-21-12-23-34-45-56-520"},
1376 /* 50 */ {FALSE, "S-1-5-21-12-23-34-45-56-553"},
1377 /* Added in Windows Server 2003 */
1378 /* 51 */ {TRUE, "S-1-5-64-10"}, {TRUE, "S-1-5-64-21"}, {TRUE, "S-1-5-64-14"},
1379 /* 54 */ {TRUE, "S-1-5-15"}, {TRUE, "S-1-5-1000"}, {FALSE, "S-1-5-32-557"},
1380 /* 57 */ {TRUE, "S-1-5-32-558"}, {TRUE, "S-1-5-32-559"}, {TRUE, "S-1-5-32-560"},
1381 /* 60 */ {TRUE, "S-1-5-32-561"}, {TRUE, "S-1-5-32-562"},
1382 /* Added in Windows Vista: */
1383 /* 62 */ {TRUE, "S-1-5-32-568"},
1384 /* 63 */ {TRUE, "S-1-5-17"}, {FALSE, "S-1-5-32-569"}, {TRUE, "S-1-16-0"},
1385 /* 66 */ {TRUE, "S-1-16-4096"}, {TRUE, "S-1-16-8192"}, {TRUE, "S-1-16-12288"},
1386 /* 69 */ {TRUE, "S-1-16-16384"}, {TRUE, "S-1-5-33"}, {TRUE, "S-1-3-4"},
1387 /* 72 */ {FALSE, "S-1-5-21-12-23-34-45-56-571"}, {FALSE, "S-1-5-21-12-23-34-45-56-572"},
1388 /* 74 */ {TRUE, "S-1-5-22"}, {FALSE, "S-1-5-21-12-23-34-45-56-521"}, {TRUE, "S-1-5-32-573"}
1391 static void test_CreateWellKnownSid(void)
1393 SID_IDENTIFIER_AUTHORITY ident = { SECURITY_NT_AUTHORITY };
1394 PSID domainsid, sid;
1399 if (!pCreateWellKnownSid)
1401 win_skip("CreateWellKnownSid not available\n");
1406 SetLastError(0xdeadbeef);
1407 ret = pCreateWellKnownSid(WinInteractiveSid, NULL, NULL, &size);
1408 error = GetLastError();
1409 ok(!ret, "CreateWellKnownSid succeeded\n");
1410 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
1411 ok(size, "expected size > 0\n");
1413 SetLastError(0xdeadbeef);
1414 ret = pCreateWellKnownSid(WinInteractiveSid, NULL, NULL, &size);
1415 error = GetLastError();
1416 ok(!ret, "CreateWellKnownSid succeeded\n");
1417 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
1419 sid = HeapAlloc(GetProcessHeap(), 0, size);
1420 ret = pCreateWellKnownSid(WinInteractiveSid, NULL, sid, &size);
1421 ok(ret, "CreateWellKnownSid failed %u\n", GetLastError());
1422 HeapFree(GetProcessHeap(), 0, sid);
1424 /* a domain sid usually have three subauthorities but we test that CreateWellKnownSid doesn't check it */
1425 AllocateAndInitializeSid(&ident, 6, SECURITY_NT_NON_UNIQUE, 12, 23, 34, 45, 56, 0, 0, &domainsid);
1427 for (i = 0; i < sizeof(well_known_sid_values)/sizeof(well_known_sid_values[0]); i++)
1429 const struct well_known_sid_value *value = &well_known_sid_values[i];
1430 char sid_buffer[SECURITY_MAX_SID_SIZE];
1434 if (value->sid_string == NULL)
1437 if (i > WinAccountRasAndIasServersSid)
1439 /* These SIDs aren't implemented by all Windows versions - detect it and break the loop */
1440 cb = sizeof(sid_buffer);
1441 if (!pCreateWellKnownSid(i, domainsid, sid_buffer, &cb))
1443 skip("Well known SIDs starting from %u are not implemented\n", i);
1448 cb = sizeof(sid_buffer);
1449 ok(pCreateWellKnownSid(i, value->without_domain ? NULL : domainsid, sid_buffer, &cb), "Couldn't create well known sid %u\n", i);
1450 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer)), cb, DWORD, "%d");
1451 ok(IsValidSid(sid_buffer), "The sid is not valid\n");
1452 ok(pConvertSidToStringSidA(sid_buffer, &str), "Couldn't convert SID to string\n");
1453 ok(strcmp(str, value->sid_string) == 0, "SID mismatch - expected %s, got %s\n",
1454 value->sid_string, str);
1457 if (value->without_domain)
1459 char buf2[SECURITY_MAX_SID_SIZE];
1461 ok(pCreateWellKnownSid(i, domainsid, buf2, &cb), "Couldn't create well known sid %u with optional domain\n", i);
1462 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer)), cb, DWORD, "%d");
1463 ok(memcmp(buf2, sid_buffer, cb) == 0, "SID create with domain is different than without (%u)\n", i);
1470 static void test_LookupAccountSid(void)
1472 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
1473 CHAR accountA[MAX_PATH], domainA[MAX_PATH], usernameA[MAX_PATH];
1474 DWORD acc_sizeA, dom_sizeA, user_sizeA;
1475 DWORD real_acc_sizeA, real_dom_sizeA;
1476 WCHAR accountW[MAX_PATH], domainW[MAX_PATH];
1477 DWORD acc_sizeW, dom_sizeW;
1478 DWORD real_acc_sizeW, real_dom_sizeW;
1479 PSID pUsersSid = NULL;
1482 DWORD error, size, cbti = 0;
1487 PTOKEN_USER ptiUser = NULL;
1489 /* native windows crashes if account size, domain size, or name use is NULL */
1491 ret = AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
1492 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &pUsersSid);
1493 ok(ret || (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED),
1494 "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1496 /* not running on NT so give up */
1497 if (!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1500 real_acc_sizeA = MAX_PATH;
1501 real_dom_sizeA = MAX_PATH;
1502 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &real_acc_sizeA, domainA, &real_dom_sizeA, &use);
1503 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1505 /* try NULL account */
1506 acc_sizeA = MAX_PATH;
1507 dom_sizeA = MAX_PATH;
1508 ret = LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
1509 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1511 /* try NULL domain */
1512 acc_sizeA = MAX_PATH;
1513 dom_sizeA = MAX_PATH;
1514 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
1515 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1517 /* try a small account buffer */
1519 dom_sizeA = MAX_PATH;
1521 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1522 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1523 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1524 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1526 /* try a 0 sized account buffer */
1528 dom_sizeA = MAX_PATH;
1530 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1531 /* this can fail or succeed depending on OS version but the size will always be returned */
1532 ok(acc_sizeA == real_acc_sizeA + 1,
1533 "LookupAccountSidA() Expected acc_size = %u, got %u\n",
1534 real_acc_sizeA + 1, acc_sizeA);
1536 /* try a 0 sized account buffer */
1538 dom_sizeA = MAX_PATH;
1539 ret = LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
1540 /* this can fail or succeed depending on OS version but the size will always be returned */
1541 ok(acc_sizeA == real_acc_sizeA + 1,
1542 "LookupAccountSid() Expected acc_size = %u, got %u\n",
1543 real_acc_sizeA + 1, acc_sizeA);
1545 /* try a small domain buffer */
1547 acc_sizeA = MAX_PATH;
1549 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1550 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1551 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1552 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1554 /* try a 0 sized domain buffer */
1556 acc_sizeA = MAX_PATH;
1558 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1559 /* this can fail or succeed depending on OS version but the size will always be returned */
1560 ok(dom_sizeA == real_dom_sizeA + 1,
1561 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1562 real_dom_sizeA + 1, dom_sizeA);
1564 /* try a 0 sized domain buffer */
1566 acc_sizeA = MAX_PATH;
1567 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
1568 /* this can fail or succeed depending on OS version but the size will always be returned */
1569 ok(dom_sizeA == real_dom_sizeA + 1,
1570 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1571 real_dom_sizeA + 1, dom_sizeA);
1573 real_acc_sizeW = MAX_PATH;
1574 real_dom_sizeW = MAX_PATH;
1575 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &real_acc_sizeW, domainW, &real_dom_sizeW, &use);
1576 ok(ret, "LookupAccountSidW() Expected TRUE, got FALSE\n");
1578 /* try an invalid system name */
1579 real_acc_sizeA = MAX_PATH;
1580 real_dom_sizeA = MAX_PATH;
1581 ret = LookupAccountSidA("deepthought", pUsersSid, accountA, &real_acc_sizeA, domainA, &real_dom_sizeA, &use);
1582 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1583 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE || GetLastError() == RPC_S_INVALID_NET_ADDR /* Vista */,
1584 "LookupAccountSidA() Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %u\n", GetLastError());
1586 /* native windows crashes if domainW or accountW is NULL */
1588 /* try a small account buffer */
1590 dom_sizeW = MAX_PATH;
1592 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1593 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1594 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1595 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1597 /* try a 0 sized account buffer */
1599 dom_sizeW = MAX_PATH;
1601 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1602 /* this can fail or succeed depending on OS version but the size will always be returned */
1603 ok(acc_sizeW == real_acc_sizeW + 1,
1604 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1605 real_acc_sizeW + 1, acc_sizeW);
1607 /* try a 0 sized account buffer */
1609 dom_sizeW = MAX_PATH;
1610 ret = LookupAccountSidW(NULL, pUsersSid, NULL, &acc_sizeW, domainW, &dom_sizeW, &use);
1611 /* this can fail or succeed depending on OS version but the size will always be returned */
1612 ok(acc_sizeW == real_acc_sizeW + 1,
1613 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1614 real_acc_sizeW + 1, acc_sizeW);
1616 /* try a small domain buffer */
1618 acc_sizeW = MAX_PATH;
1620 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1621 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1622 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1623 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1625 /* try a 0 sized domain buffer */
1627 acc_sizeW = MAX_PATH;
1629 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1630 /* this can fail or succeed depending on OS version but the size will always be returned */
1631 ok(dom_sizeW == real_dom_sizeW + 1,
1632 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1633 real_dom_sizeW + 1, dom_sizeW);
1635 /* try a 0 sized domain buffer */
1637 acc_sizeW = MAX_PATH;
1638 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, NULL, &dom_sizeW, &use);
1639 /* this can fail or succeed depending on OS version but the size will always be returned */
1640 ok(dom_sizeW == real_dom_sizeW + 1,
1641 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1642 real_dom_sizeW + 1, dom_sizeW);
1644 acc_sizeW = dom_sizeW = use = 0;
1645 SetLastError(0xdeadbeef);
1646 ret = LookupAccountSidW(NULL, pUsersSid, NULL, &acc_sizeW, NULL, &dom_sizeW, &use);
1647 error = GetLastError();
1648 ok(!ret, "LookupAccountSidW failed %u\n", GetLastError());
1649 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
1650 ok(acc_sizeW, "expected non-zero account size\n");
1651 ok(dom_sizeW, "expected non-zero domain size\n");
1652 ok(!use, "expected zero use %u\n", use);
1656 /* Test LookupAccountSid with Sid retrieved from token information.
1657 This assumes this process is running under the account of the current user.*/
1658 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &hToken);
1659 ret = GetTokenInformation(hToken, TokenUser, NULL, 0, &cbti);
1660 ptiUser = HeapAlloc(GetProcessHeap(), 0, cbti);
1661 if (GetTokenInformation(hToken, TokenUser, ptiUser, cbti, &cbti))
1663 acc_sizeA = dom_sizeA = MAX_PATH;
1664 ret = LookupAccountSidA(NULL, ptiUser->User.Sid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1665 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1666 user_sizeA = MAX_PATH;
1667 ret = GetUserNameA(usernameA , &user_sizeA);
1668 ok(ret, "GetUserNameA() Expected TRUE, got FALSE\n");
1671 ok(lstrcmpA(usernameA, accountA) == 0, "LookupAccountSidA() Expected account name: %s got: %s\n", usernameA, accountA );
1674 HeapFree(GetProcessHeap(), 0, ptiUser);
1676 if (pCreateWellKnownSid && pConvertSidToStringSidA)
1678 trace("Well Known SIDs:\n");
1679 for (i = 0; i <= 60; i++)
1681 size = SECURITY_MAX_SID_SIZE;
1682 if (pCreateWellKnownSid(i, NULL, &max_sid.sid, &size))
1684 if (pConvertSidToStringSidA(&max_sid.sid, &str_sidA))
1686 acc_sizeA = MAX_PATH;
1687 dom_sizeA = MAX_PATH;
1688 if (LookupAccountSidA(NULL, &max_sid.sid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use))
1689 trace(" %d: %s %s\\%s %d\n", i, str_sidA, domainA, accountA, use);
1690 LocalFree(str_sidA);
1695 if (GetLastError() != ERROR_INVALID_PARAMETER)
1696 trace(" CreateWellKnownSid(%d) failed: %d\n", i, GetLastError());
1698 trace(" %d: not supported\n", i);
1702 pLsaQueryInformationPolicy = (void *)GetProcAddress( hmod, "LsaQueryInformationPolicy");
1703 pLsaOpenPolicy = (void *)GetProcAddress( hmod, "LsaOpenPolicy");
1704 pLsaFreeMemory = (void *)GetProcAddress( hmod, "LsaFreeMemory");
1705 pLsaClose = (void *)GetProcAddress( hmod, "LsaClose");
1707 if (pLsaQueryInformationPolicy && pLsaOpenPolicy && pLsaFreeMemory && pLsaClose)
1711 LSA_OBJECT_ATTRIBUTES object_attributes;
1713 ZeroMemory(&object_attributes, sizeof(object_attributes));
1714 object_attributes.Length = sizeof(object_attributes);
1716 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_ALL_ACCESS, &handle);
1717 ok(status == STATUS_SUCCESS || status == STATUS_ACCESS_DENIED,
1718 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status);
1720 /* try a more restricted access mask if necessary */
1721 if (status == STATUS_ACCESS_DENIED) {
1722 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
1723 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_VIEW_LOCAL_INFORMATION, &handle);
1724 ok(status == STATUS_SUCCESS, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status);
1727 if (status == STATUS_SUCCESS)
1729 PPOLICY_ACCOUNT_DOMAIN_INFO info;
1730 status = pLsaQueryInformationPolicy(handle, PolicyAccountDomainInformation, (PVOID*)&info);
1731 ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy() failed, returned 0x%08x\n", status);
1732 if (status == STATUS_SUCCESS)
1734 ok(info->DomainSid!=0, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) missing SID\n");
1735 if (info->DomainSid)
1737 int count = *GetSidSubAuthorityCount(info->DomainSid);
1738 CopySid(GetSidLengthRequired(count), &max_sid, info->DomainSid);
1739 test_sid_str((PSID)&max_sid.sid);
1740 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_ADMIN;
1741 max_sid.sid.SubAuthorityCount = count + 1;
1742 test_sid_str((PSID)&max_sid.sid);
1743 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_GUEST;
1744 test_sid_str((PSID)&max_sid.sid);
1745 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ADMINS;
1746 test_sid_str((PSID)&max_sid.sid);
1747 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_USERS;
1748 test_sid_str((PSID)&max_sid.sid);
1749 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_GUESTS;
1750 test_sid_str((PSID)&max_sid.sid);
1751 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_COMPUTERS;
1752 test_sid_str((PSID)&max_sid.sid);
1753 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CONTROLLERS;
1754 test_sid_str((PSID)&max_sid.sid);
1755 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CERT_ADMINS;
1756 test_sid_str((PSID)&max_sid.sid);
1757 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_SCHEMA_ADMINS;
1758 test_sid_str((PSID)&max_sid.sid);
1759 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ENTERPRISE_ADMINS;
1760 test_sid_str((PSID)&max_sid.sid);
1761 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_POLICY_ADMINS;
1762 test_sid_str((PSID)&max_sid.sid);
1763 max_sid.sid.SubAuthority[count] = DOMAIN_ALIAS_RID_RAS_SERVERS;
1764 test_sid_str((PSID)&max_sid.sid);
1765 max_sid.sid.SubAuthority[count] = 1000; /* first user account */
1766 test_sid_str((PSID)&max_sid.sid);
1769 pLsaFreeMemory((LPVOID)info);
1772 status = pLsaClose(handle);
1773 ok(status == STATUS_SUCCESS, "LsaClose() failed, returned 0x%08x\n", status);
1779 static BOOL get_sid_info(PSID psid, LPSTR *user, LPSTR *dom)
1781 static CHAR account[UNLEN + 1];
1782 static CHAR domain[UNLEN + 1];
1783 DWORD size, dom_size;
1789 size = dom_size = UNLEN + 1;
1792 SetLastError(0xdeadbeef);
1793 return LookupAccountSidA(NULL, psid, account, &size, domain, &dom_size, &use);
1796 static void check_wellknown_name(const char* name, WELL_KNOWN_SID_TYPE result)
1798 SID_IDENTIFIER_AUTHORITY ident = { SECURITY_NT_AUTHORITY };
1799 PSID domainsid = NULL;
1800 char wk_sid[SECURITY_MAX_SID_SIZE];
1803 DWORD sid_size, domain_size;
1804 SID_NAME_USE sid_use;
1805 LPSTR domain, account, sid_domain, wk_domain, wk_account;
1811 ret = LookupAccountNameA(NULL, name, NULL, &sid_size, NULL, &domain_size, &sid_use);
1812 psid = HeapAlloc(GetProcessHeap(),0,sid_size);
1813 domain = HeapAlloc(GetProcessHeap(),0,domain_size);
1814 ret = LookupAccountNameA(NULL, name, psid, &sid_size, domain, &domain_size, &sid_use);
1818 ok(!ret, " %s Should have failed to lookup account name\n",name);
1822 AllocateAndInitializeSid(&ident, 6, SECURITY_NT_NON_UNIQUE, 12, 23, 34, 45, 56, 0, 0, &domainsid);
1823 cb = sizeof(wk_sid);
1824 if (!pCreateWellKnownSid(result, domainsid, wk_sid, &cb))
1826 win_skip("SID %i is not available on the system\n",result);
1830 ret2 = get_sid_info(wk_sid, &wk_account, &wk_domain);
1831 if (!ret2 && GetLastError() == ERROR_NONE_MAPPED)
1833 win_skip("CreateWellKnownSid() succeeded but the account '%s' is not present (W2K)\n", name);
1837 get_sid_info(psid, &account, &sid_domain);
1839 ok(ret, "Failed to lookup account name %s\n",name);
1840 ok(sid_size != 0, "sid_size was zero\n");
1842 ok(EqualSid(psid,wk_sid),"(%s) Sids fail to match well known sid!\n",name);
1844 ok(!lstrcmp(account, wk_account), "Expected %s , got %s\n", account, wk_account);
1845 ok(!lstrcmp(domain, wk_domain), "Expected %s, got %s\n", wk_domain, domain);
1846 ok(sid_use == SidTypeWellKnownGroup , "Expected Use (5), got %d\n", sid_use);
1850 HeapFree(GetProcessHeap(),0,psid);
1851 HeapFree(GetProcessHeap(),0,domain);
1854 static void test_LookupAccountName(void)
1856 DWORD sid_size, domain_size, user_size;
1857 DWORD sid_save, domain_save;
1858 CHAR user_name[UNLEN + 1];
1859 CHAR computer_name[UNLEN + 1];
1860 SID_NAME_USE sid_use;
1861 LPSTR domain, account, sid_dom;
1865 /* native crashes if (assuming all other parameters correct):
1867 * - Sid is NULL and cbSid is > 0
1868 * - cbSid or cchReferencedDomainName are NULL
1869 * - ReferencedDomainName is NULL and cchReferencedDomainName is the correct size
1872 user_size = UNLEN + 1;
1873 SetLastError(0xdeadbeef);
1874 ret = GetUserNameA(user_name, &user_size);
1875 if (!ret && (GetLastError() == ERROR_NOT_LOGGED_ON))
1877 /* Probably on win9x where the user used 'Cancel' instead of properly logging in */
1878 skip("Cannot get the user name (win9x and not logged in properly)\n");
1881 ok(ret, "Failed to get user name : %d\n", GetLastError());
1886 sid_use = 0xcafebabe;
1887 SetLastError(0xdeadbeef);
1888 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, NULL, &domain_size, &sid_use);
1889 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1891 win_skip("LookupAccountNameA is not implemented\n");
1894 ok(!ret, "Expected 0, got %d\n", ret);
1895 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1896 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1897 ok(sid_size != 0, "Expected non-zero sid size\n");
1898 ok(domain_size != 0, "Expected non-zero domain size\n");
1899 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
1901 sid_save = sid_size;
1902 domain_save = domain_size;
1904 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
1905 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
1907 /* try valid account name */
1908 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, domain, &domain_size, &sid_use);
1909 get_sid_info(psid, &account, &sid_dom);
1910 ok(ret, "Failed to lookup account name\n");
1911 ok(sid_size == GetLengthSid(psid), "Expected %d, got %d\n", GetLengthSid(psid), sid_size);
1914 ok(!lstrcmp(account, user_name), "Expected %s, got %s\n", user_name, account);
1915 ok(!lstrcmp(domain, sid_dom), "Expected %s, got %s\n", sid_dom, domain);
1917 ok(domain_size == domain_save - 1, "Expected %d, got %d\n", domain_save - 1, domain_size);
1918 ok(strlen(domain) == domain_size, "Expected %d, got %d\n", lstrlen(domain), domain_size);
1919 ok(sid_use == SidTypeUser, "Expected SidTypeUser (%d), got %d\n", SidTypeUser, sid_use);
1920 domain_size = domain_save;
1921 sid_size = sid_save;
1923 if (PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale())) != LANG_ENGLISH)
1925 skip("Non-english locale (test with hardcoded 'Everyone')\n");
1929 ret = LookupAccountNameA(NULL, "Everyone", psid, &sid_size, domain, &domain_size, &sid_use);
1930 get_sid_info(psid, &account, &sid_dom);
1931 ok(ret, "Failed to lookup account name\n");
1932 ok(sid_size != 0, "sid_size was zero\n");
1933 ok(!lstrcmp(account, "Everyone"), "Expected Everyone, got %s\n", account);
1934 ok(!lstrcmp(domain, sid_dom), "Expected %s, got %s\n", sid_dom, domain);
1935 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
1936 ok(strlen(domain) == domain_size, "Expected %d, got %d\n", lstrlen(domain), domain_size);
1937 ok(sid_use == SidTypeWellKnownGroup, "Expected SidTypeWellKnownGroup (%d), got %d\n", SidTypeWellKnownGroup, sid_use);
1938 domain_size = domain_save;
1941 /* NULL Sid with zero sid size */
1942 SetLastError(0xdeadbeef);
1944 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
1945 ok(!ret, "Expected 0, got %d\n", ret);
1946 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1947 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1948 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1949 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1951 /* try cchReferencedDomainName - 1 */
1952 SetLastError(0xdeadbeef);
1954 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
1955 ok(!ret, "Expected 0, got %d\n", ret);
1956 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1957 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1958 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1959 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1961 /* NULL ReferencedDomainName with zero domain name size */
1962 SetLastError(0xdeadbeef);
1964 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, NULL, &domain_size, &sid_use);
1965 ok(!ret, "Expected 0, got %d\n", ret);
1966 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1967 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1968 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1969 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1971 HeapFree(GetProcessHeap(), 0, psid);
1972 HeapFree(GetProcessHeap(), 0, domain);
1974 /* get sizes for NULL account name */
1977 sid_use = 0xcafebabe;
1978 SetLastError(0xdeadbeef);
1979 ret = LookupAccountNameA(NULL, NULL, NULL, &sid_size, NULL, &domain_size, &sid_use);
1980 if (!ret && GetLastError() == ERROR_NONE_MAPPED)
1981 win_skip("NULL account name doesn't work on NT4\n");
1984 ok(!ret, "Expected 0, got %d\n", ret);
1985 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1986 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1987 ok(sid_size != 0, "Expected non-zero sid size\n");
1988 ok(domain_size != 0, "Expected non-zero domain size\n");
1989 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
1991 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
1992 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
1994 /* try NULL account name */
1995 ret = LookupAccountNameA(NULL, NULL, psid, &sid_size, domain, &domain_size, &sid_use);
1996 get_sid_info(psid, &account, &sid_dom);
1997 ok(ret, "Failed to lookup account name\n");
1998 /* Using a fixed string will not work on different locales */
1999 ok(!lstrcmp(account, domain),
2000 "Got %s for account and %s for domain, these should be the same\n",
2002 ok(sid_use == SidTypeDomain, "Expected SidTypeDomain (%d), got %d\n", SidTypeDomain, sid_use);
2004 HeapFree(GetProcessHeap(), 0, psid);
2005 HeapFree(GetProcessHeap(), 0, domain);
2008 /* try an invalid account name */
2009 SetLastError(0xdeadbeef);
2012 ret = LookupAccountNameA(NULL, "oogabooga", NULL, &sid_size, NULL, &domain_size, &sid_use);
2013 ok(!ret, "Expected 0, got %d\n", ret);
2014 ok(GetLastError() == ERROR_NONE_MAPPED ||
2015 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE),
2016 "Expected ERROR_NONE_MAPPED, got %d\n", GetLastError());
2017 ok(sid_size == 0, "Expected 0, got %d\n", sid_size);
2018 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
2020 /* try an invalid system name */
2021 SetLastError(0xdeadbeef);
2024 ret = LookupAccountNameA("deepthought", NULL, NULL, &sid_size, NULL, &domain_size, &sid_use);
2025 ok(!ret, "Expected 0, got %d\n", ret);
2026 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE || GetLastError() == RPC_S_INVALID_NET_ADDR /* Vista */,
2027 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
2028 ok(sid_size == 0, "Expected 0, got %d\n", sid_size);
2029 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
2031 /* try with the computer name as the account name */
2032 domain_size = sizeof(computer_name);
2033 GetComputerNameA(computer_name, &domain_size);
2036 ret = LookupAccountNameA(NULL, computer_name, NULL, &sid_size, NULL, &domain_size, &sid_use);
2037 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER ||
2038 GetLastError() == ERROR_NONE_MAPPED /* in a domain */ ||
2039 broken(GetLastError() == ERROR_TRUSTED_DOMAIN_FAILURE) ||
2040 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE)),
2041 "LookupAccountNameA failed: %d\n", GetLastError());
2042 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
2044 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
2045 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
2046 ret = LookupAccountNameA(NULL, computer_name, psid, &sid_size, domain, &domain_size, &sid_use);
2047 ok(ret, "LookupAccountNameA failed: %d\n", GetLastError());
2048 ok(sid_use == SidTypeDomain ||
2049 (sid_use == SidTypeUser && ! strcmp(computer_name, user_name)), "expected SidTypeDomain for %s, got %d\n", computer_name, sid_use);
2050 HeapFree(GetProcessHeap(), 0, domain);
2051 HeapFree(GetProcessHeap(), 0, psid);
2054 /* Well Known names */
2055 if (!pCreateWellKnownSid)
2057 win_skip("CreateWellKnownSid not available\n");
2061 if (PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale())) != LANG_ENGLISH)
2063 skip("Non-english locale (skipping well known name creation tests)\n");
2067 check_wellknown_name("LocalService", WinLocalServiceSid);
2068 check_wellknown_name("Local Service", WinLocalServiceSid);
2070 check_wellknown_name("Local Service", 0);
2071 check_wellknown_name("NetworkService", WinNetworkServiceSid);
2072 check_wellknown_name("Network Service", WinNetworkServiceSid);
2074 /* example of some names where the spaces are not optional */
2075 check_wellknown_name("Terminal Server User", WinTerminalServerSid);
2076 check_wellknown_name("TerminalServer User", 0);
2077 check_wellknown_name("TerminalServerUser", 0);
2078 check_wellknown_name("Terminal ServerUser", 0);
2080 check_wellknown_name("enterprise domain controllers",WinEnterpriseControllersSid);
2081 check_wellknown_name("enterprisedomain controllers", 0);
2082 check_wellknown_name("enterprise domaincontrollers", 0);
2083 check_wellknown_name("enterprisedomaincontrollers", 0);
2085 /* case insensitivity */
2086 check_wellknown_name("lOCAlServICE", WinLocalServiceSid);
2088 /* fully qualified account names */
2089 check_wellknown_name("NT AUTHORITY\\LocalService", WinLocalServiceSid);
2090 check_wellknown_name("nt authority\\Network Service", WinNetworkServiceSid);
2091 check_wellknown_name("nt authority test\\Network Service", 0);
2092 check_wellknown_name("Dummy\\Network Service", 0);
2093 check_wellknown_name("ntauthority\\Network Service", 0);
2096 static void test_security_descriptor(void)
2098 SECURITY_DESCRIPTOR sd;
2101 BOOL isDefault, isPresent, ret;
2105 SetLastError(0xdeadbeef);
2106 ret = InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
2107 if (ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2109 win_skip("InitializeSecurityDescriptor is not implemented\n");
2113 ok(GetSecurityDescriptorOwner(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
2114 expect_eq(psid, NULL, PSID, "%p");
2115 expect_eq(isDefault, FALSE, BOOL, "%d");
2116 sd.Control |= SE_DACL_PRESENT | SE_SACL_PRESENT;
2118 SetLastError(0xdeadbeef);
2120 expect_eq(MakeSelfRelativeSD(&sd, buf, &size), FALSE, BOOL, "%d");
2121 expect_eq(GetLastError(), ERROR_INSUFFICIENT_BUFFER, DWORD, "%u");
2122 ok(size > 5, "Size not increased\n");
2125 expect_eq(MakeSelfRelativeSD(&sd, buf, &size), TRUE, BOOL, "%d");
2126 ok(GetSecurityDescriptorOwner(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
2127 expect_eq(psid, NULL, PSID, "%p");
2128 expect_eq(isDefault, FALSE, BOOL, "%d");
2129 ok(GetSecurityDescriptorGroup(&sd, &psid, &isDefault), "GetSecurityDescriptorGroup failed\n");
2130 expect_eq(psid, NULL, PSID, "%p");
2131 expect_eq(isDefault, FALSE, BOOL, "%d");
2132 ok(GetSecurityDescriptorDacl(&sd, &isPresent, &pacl, &isDefault), "GetSecurityDescriptorDacl failed\n");
2133 expect_eq(isPresent, TRUE, BOOL, "%d");
2134 expect_eq(psid, NULL, PSID, "%p");
2135 expect_eq(isDefault, FALSE, BOOL, "%d");
2136 ok(GetSecurityDescriptorSacl(&sd, &isPresent, &pacl, &isDefault), "GetSecurityDescriptorSacl failed\n");
2137 expect_eq(isPresent, TRUE, BOOL, "%d");
2138 expect_eq(psid, NULL, PSID, "%p");
2139 expect_eq(isDefault, FALSE, BOOL, "%d");
2143 #define TEST_GRANTED_ACCESS(a,b) test_granted_access(a,b,0,__LINE__)
2144 #define TEST_GRANTED_ACCESS2(a,b,c) test_granted_access(a,b,c,__LINE__)
2145 static void test_granted_access(HANDLE handle, ACCESS_MASK access,
2146 ACCESS_MASK alt, int line)
2148 OBJECT_BASIC_INFORMATION obj_info;
2151 if (!pNtQueryObject)
2153 skip_(__FILE__, line)("Not NT platform - skipping tests\n");
2157 status = pNtQueryObject( handle, ObjectBasicInformation, &obj_info,
2158 sizeof(obj_info), NULL );
2159 ok_(__FILE__, line)(!status, "NtQueryObject with err: %08x\n", status);
2161 ok_(__FILE__, line)(obj_info.GrantedAccess == access ||
2162 obj_info.GrantedAccess == alt, "Granted access should be 0x%08x "
2163 "or 0x%08x, instead of 0x%08x\n", access, alt, obj_info.GrantedAccess);
2165 ok_(__FILE__, line)(obj_info.GrantedAccess == access, "Granted access should "
2166 "be 0x%08x, instead of 0x%08x\n", access, obj_info.GrantedAccess);
2169 #define CHECK_SET_SECURITY(o,i,e) \
2173 SetLastError( 0xdeadbeef ); \
2174 res = SetKernelObjectSecurity( o, i, SecurityDescriptor ); \
2175 err = GetLastError(); \
2176 if (e == ERROR_SUCCESS) \
2177 ok(res, "SetKernelObjectSecurity failed with %d\n", err); \
2179 ok(!res && err == e, "SetKernelObjectSecurity should have failed " \
2180 "with %s, instead of %d\n", #e, err); \
2183 static void test_process_security(void)
2187 PTOKEN_PRIMARY_GROUP group;
2188 PSID AdminSid = NULL, UsersSid = NULL;
2190 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL;
2191 char buffer[MAX_PATH];
2192 PROCESS_INFORMATION info;
2193 STARTUPINFOA startup;
2194 SECURITY_ATTRIBUTES psa;
2195 HANDLE token, event;
2198 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
2199 res = InitializeAcl(Acl, 256, ACL_REVISION);
2200 if (!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2202 win_skip("ACLs not implemented - skipping tests\n");
2203 HeapFree(GetProcessHeap(), 0, Acl);
2206 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
2208 /* get owner from the token we might be running as a user not admin */
2209 res = OpenProcessToken( GetCurrentProcess(), MAXIMUM_ALLOWED, &token );
2210 ok(res, "OpenProcessToken failed with error %d\n", GetLastError());
2213 HeapFree(GetProcessHeap(), 0, Acl);
2217 res = GetTokenInformation( token, TokenOwner, NULL, 0, &size );
2218 ok(!res, "Expected failure, got %d\n", res);
2219 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2220 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2222 owner = HeapAlloc(GetProcessHeap(), 0, size);
2223 res = GetTokenInformation( token, TokenOwner, owner, size, &size );
2224 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
2225 AdminSid = ((TOKEN_OWNER*)owner)->Owner;
2227 res = GetTokenInformation( token, TokenPrimaryGroup, NULL, 0, &size );
2228 ok(!res, "Expected failure, got %d\n", res);
2229 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2230 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2232 group = HeapAlloc(GetProcessHeap(), 0, size);
2233 res = GetTokenInformation( token, TokenPrimaryGroup, group, size, &size );
2234 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
2235 UsersSid = ((TOKEN_PRIMARY_GROUP*)group)->PrimaryGroup;
2237 CloseHandle( token );
2240 HeapFree(GetProcessHeap(), 0, group);
2241 HeapFree(GetProcessHeap(), 0, owner);
2242 HeapFree(GetProcessHeap(), 0, Acl);
2246 res = AddAccessDeniedAce(Acl, ACL_REVISION, PROCESS_VM_READ, AdminSid);
2247 ok(res, "AddAccessDeniedAce failed with error %d\n", GetLastError());
2248 res = AddAccessAllowedAce(Acl, ACL_REVISION, PROCESS_ALL_ACCESS, AdminSid);
2249 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2251 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
2252 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
2253 ok(res, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
2255 event = CreateEvent( NULL, TRUE, TRUE, "test_event" );
2256 ok(event != NULL, "CreateEvent %d\n", GetLastError());
2258 SecurityDescriptor->Revision = 0;
2259 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_UNKNOWN_REVISION );
2260 SecurityDescriptor->Revision = SECURITY_DESCRIPTOR_REVISION;
2262 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_INVALID_SECURITY_DESCR );
2263 CHECK_SET_SECURITY( event, GROUP_SECURITY_INFORMATION, ERROR_INVALID_SECURITY_DESCR );
2264 CHECK_SET_SECURITY( event, SACL_SECURITY_INFORMATION, ERROR_ACCESS_DENIED );
2265 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
2266 /* NULL DACL is valid and means default DACL from token */
2267 SecurityDescriptor->Control |= SE_DACL_PRESENT;
2268 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
2270 /* Set owner and group and dacl */
2271 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
2272 ok(res, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
2273 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_SUCCESS );
2274 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, FALSE);
2275 ok(res, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2276 CHECK_SET_SECURITY( event, GROUP_SECURITY_INFORMATION, ERROR_SUCCESS );
2277 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
2278 ok(res, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
2279 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
2281 sprintf(buffer, "%s tests/security.c test", myARGV[0]);
2282 memset(&startup, 0, sizeof(startup));
2283 startup.cb = sizeof(startup);
2284 startup.dwFlags = STARTF_USESHOWWINDOW;
2285 startup.wShowWindow = SW_SHOWNORMAL;
2287 psa.nLength = sizeof(psa);
2288 psa.lpSecurityDescriptor = SecurityDescriptor;
2289 psa.bInheritHandle = TRUE;
2291 /* Doesn't matter what ACL say we should get full access for ourselves */
2292 ok(CreateProcessA( NULL, buffer, &psa, NULL, FALSE, 0, NULL, NULL, &startup, &info ),
2293 "CreateProcess with err:%d\n", GetLastError());
2294 TEST_GRANTED_ACCESS2( info.hProcess, PROCESS_ALL_ACCESS,
2295 STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL );
2296 winetest_wait_child_process( info.hProcess );
2298 CloseHandle( info.hProcess );
2299 CloseHandle( info.hThread );
2300 CloseHandle( event );
2301 HeapFree(GetProcessHeap(), 0, group);
2302 HeapFree(GetProcessHeap(), 0, owner);
2303 HeapFree(GetProcessHeap(), 0, Acl);
2304 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
2307 static void test_process_security_child(void)
2309 HANDLE handle, handle1;
2313 handle = OpenProcess( PROCESS_TERMINATE, FALSE, GetCurrentProcessId() );
2314 ok(handle != NULL, "OpenProcess(PROCESS_TERMINATE) with err:%d\n", GetLastError());
2315 TEST_GRANTED_ACCESS( handle, PROCESS_TERMINATE );
2317 ok(DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
2318 &handle1, 0, TRUE, DUPLICATE_SAME_ACCESS ),
2319 "duplicating handle err:%d\n", GetLastError());
2320 TEST_GRANTED_ACCESS( handle1, PROCESS_TERMINATE );
2322 CloseHandle( handle1 );
2324 SetLastError( 0xdeadbeef );
2325 ret = DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
2326 &handle1, PROCESS_ALL_ACCESS, TRUE, 0 );
2327 err = GetLastError();
2329 ok(!ret && err == ERROR_ACCESS_DENIED, "duplicating handle should have failed "
2330 "with STATUS_ACCESS_DENIED, instead of err:%d\n", err);
2332 CloseHandle( handle );
2334 /* These two should fail - they are denied by ACL */
2335 handle = OpenProcess( PROCESS_VM_READ, FALSE, GetCurrentProcessId() );
2337 ok(handle == NULL, "OpenProcess(PROCESS_VM_READ) should have failed\n");
2338 handle = OpenProcess( PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId() );
2340 ok(handle == NULL, "OpenProcess(PROCESS_ALL_ACCESS) should have failed\n");
2342 /* Documented privilege elevation */
2343 ok(DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2344 &handle, 0, TRUE, DUPLICATE_SAME_ACCESS ),
2345 "duplicating handle err:%d\n", GetLastError());
2346 TEST_GRANTED_ACCESS2( handle, PROCESS_ALL_ACCESS,
2347 STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL );
2349 CloseHandle( handle );
2351 /* Same only explicitly asking for all access rights */
2352 ok(DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2353 &handle, PROCESS_ALL_ACCESS, TRUE, 0 ),
2354 "duplicating handle err:%d\n", GetLastError());
2355 TEST_GRANTED_ACCESS2( handle, PROCESS_ALL_ACCESS,
2356 PROCESS_ALL_ACCESS | PROCESS_QUERY_LIMITED_INFORMATION );
2357 ok(DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
2358 &handle1, PROCESS_VM_READ, TRUE, 0 ),
2359 "duplicating handle err:%d\n", GetLastError());
2360 TEST_GRANTED_ACCESS( handle1, PROCESS_VM_READ );
2361 CloseHandle( handle1 );
2362 CloseHandle( handle );
2365 static void test_impersonation_level(void)
2367 HANDLE Token, ProcessToken;
2370 TOKEN_PRIVILEGES *Privileges;
2372 PRIVILEGE_SET *PrivilegeSet;
2378 pDuplicateTokenEx = (void *)GetProcAddress(hmod, "DuplicateTokenEx");
2379 if( !pDuplicateTokenEx ) {
2380 win_skip("DuplicateTokenEx is not available\n");
2383 SetLastError(0xdeadbeef);
2384 ret = ImpersonateSelf(SecurityAnonymous);
2385 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2387 win_skip("ImpersonateSelf is not implemented\n");
2390 ok(ret, "ImpersonateSelf(SecurityAnonymous) failed with error %d\n", GetLastError());
2391 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
2392 ok(!ret, "OpenThreadToken should have failed\n");
2393 error = GetLastError();
2394 ok(error == ERROR_CANT_OPEN_ANONYMOUS, "OpenThreadToken on anonymous token should have returned ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error);
2395 /* can't perform access check when opening object against an anonymous impersonation token */
2397 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
2398 ok(error == ERROR_INVALID_HANDLE || error == ERROR_CANT_OPEN_ANONYMOUS,
2399 "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE or ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error);
2403 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, &ProcessToken);
2404 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
2406 ret = pDuplicateTokenEx(ProcessToken,
2407 TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_IMPERSONATE, NULL,
2408 SecurityAnonymous, TokenImpersonation, &Token);
2409 ok(ret, "DuplicateTokenEx failed with error %d\n", GetLastError());
2410 /* can't increase the impersonation level */
2411 ret = DuplicateToken(Token, SecurityIdentification, &Token2);
2412 error = GetLastError();
2413 ok(!ret && error == ERROR_BAD_IMPERSONATION_LEVEL,
2414 "Duplicating a token and increasing the impersonation level should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
2415 /* we can query anything from an anonymous token, including the user */
2416 ret = GetTokenInformation(Token, TokenUser, NULL, 0, &Size);
2417 error = GetLastError();
2418 ok(!ret && error == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenUser) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error);
2419 User = HeapAlloc(GetProcessHeap(), 0, Size);
2420 ret = GetTokenInformation(Token, TokenUser, User, Size, &Size);
2421 ok(ret, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
2422 HeapFree(GetProcessHeap(), 0, User);
2424 /* PrivilegeCheck fails with SecurityAnonymous level */
2425 ret = GetTokenInformation(Token, TokenPrivileges, NULL, 0, &Size);
2426 error = GetLastError();
2427 ok(!ret && error == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenPrivileges) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error);
2428 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
2429 ret = GetTokenInformation(Token, TokenPrivileges, Privileges, Size, &Size);
2430 ok(ret, "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
2432 PrivilegeSet = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PRIVILEGE_SET, Privilege[Privileges->PrivilegeCount]));
2433 PrivilegeSet->PrivilegeCount = Privileges->PrivilegeCount;
2434 memcpy(PrivilegeSet->Privilege, Privileges->Privileges, PrivilegeSet->PrivilegeCount * sizeof(PrivilegeSet->Privilege[0]));
2435 PrivilegeSet->Control = PRIVILEGE_SET_ALL_NECESSARY;
2436 HeapFree(GetProcessHeap(), 0, Privileges);
2438 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2439 error = GetLastError();
2440 ok(!ret && error == ERROR_BAD_IMPERSONATION_LEVEL, "PrivilegeCheck for SecurityAnonymous token should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
2444 ret = ImpersonateSelf(SecurityIdentification);
2445 ok(ret, "ImpersonateSelf(SecurityIdentification) failed with error %d\n", GetLastError());
2446 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
2447 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
2449 /* can't perform access check when opening object against an identification impersonation token */
2450 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
2452 ok(error == ERROR_INVALID_HANDLE || error == ERROR_BAD_IMPERSONATION_LEVEL,
2453 "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE or ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
2455 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2456 ok(ret, "PrivilegeCheck for SecurityIdentification failed with error %d\n", GetLastError());
2460 ret = ImpersonateSelf(SecurityImpersonation);
2461 ok(ret, "ImpersonateSelf(SecurityImpersonation) failed with error %d\n", GetLastError());
2462 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
2463 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
2464 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
2465 ok(error == ERROR_SUCCESS, "RegOpenKeyEx should have succeeded instead of failing with %d\n", error);
2467 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2468 ok(ret, "PrivilegeCheck for SecurityImpersonation failed with error %d\n", GetLastError());
2472 CloseHandle(ProcessToken);
2474 HeapFree(GetProcessHeap(), 0, PrivilegeSet);
2477 static void test_SetEntriesInAcl(void)
2480 PSID EveryoneSid = NULL, UsersSid = NULL;
2481 PACL OldAcl = NULL, NewAcl;
2482 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
2483 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
2484 EXPLICIT_ACCESSW ExplicitAccess;
2485 static const WCHAR wszEveryone[] = {'E','v','e','r','y','o','n','e',0};
2486 static const WCHAR wszCurrentUser[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
2488 if (!pSetEntriesInAclW)
2490 win_skip("SetEntriesInAclW is not available\n");
2494 NewAcl = (PACL)0xdeadbeef;
2495 res = pSetEntriesInAclW(0, NULL, NULL, &NewAcl);
2496 if(res == ERROR_CALL_NOT_IMPLEMENTED)
2498 win_skip("SetEntriesInAclW is not implemented\n");
2501 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2502 ok(NewAcl == NULL ||
2503 broken(NewAcl != NULL), /* NT4 */
2504 "NewAcl=%p, expected NULL\n", NewAcl);
2507 OldAcl = HeapAlloc(GetProcessHeap(), 0, 256);
2508 res = InitializeAcl(OldAcl, 256, ACL_REVISION);
2509 if(!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2511 win_skip("ACLs not implemented - skipping tests\n");
2512 HeapFree(GetProcessHeap(), 0, OldAcl);
2515 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
2517 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
2518 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2520 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
2521 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &UsersSid);
2522 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2524 res = AddAccessAllowedAce(OldAcl, ACL_REVISION, KEY_READ, UsersSid);
2525 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2527 ExplicitAccess.grfAccessPermissions = KEY_WRITE;
2528 ExplicitAccess.grfAccessMode = GRANT_ACCESS;
2529 ExplicitAccess.grfInheritance = NO_INHERITANCE;
2530 ExplicitAccess.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
2531 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
2532 ExplicitAccess.Trustee.ptstrName = EveryoneSid;
2533 ExplicitAccess.Trustee.MultipleTrusteeOperation = 0xDEADBEEF;
2534 ExplicitAccess.Trustee.pMultipleTrustee = (PVOID)0xDEADBEEF;
2535 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2536 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2537 ok(NewAcl != NULL, "returned acl was NULL\n");
2540 ExplicitAccess.Trustee.TrusteeType = TRUSTEE_IS_UNKNOWN;
2541 ExplicitAccess.Trustee.pMultipleTrustee = NULL;
2542 ExplicitAccess.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2543 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2544 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2545 ok(NewAcl != NULL, "returned acl was NULL\n");
2548 if (PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale())) != LANG_ENGLISH)
2550 skip("Non-english locale (test with hardcoded 'Everyone')\n");
2554 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_USER;
2555 ExplicitAccess.Trustee.ptstrName = (LPWSTR)wszEveryone;
2556 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2557 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2558 ok(NewAcl != NULL, "returned acl was NULL\n");
2561 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_BAD_FORM;
2562 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2563 ok(res == ERROR_INVALID_PARAMETER ||
2564 broken(res == ERROR_NOT_SUPPORTED), /* NT4 */
2565 "SetEntriesInAclW failed: %u\n", res);
2566 ok(NewAcl == NULL ||
2567 broken(NewAcl != NULL), /* NT4 */
2568 "returned acl wasn't NULL: %p\n", NewAcl);
2570 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_USER;
2571 ExplicitAccess.Trustee.MultipleTrusteeOperation = TRUSTEE_IS_IMPERSONATE;
2572 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2573 ok(res == ERROR_INVALID_PARAMETER ||
2574 broken(res == ERROR_NOT_SUPPORTED), /* NT4 */
2575 "SetEntriesInAclW failed: %u\n", res);
2576 ok(NewAcl == NULL ||
2577 broken(NewAcl != NULL), /* NT4 */
2578 "returned acl wasn't NULL: %p\n", NewAcl);
2580 ExplicitAccess.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2581 ExplicitAccess.grfAccessMode = SET_ACCESS;
2582 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2583 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2584 ok(NewAcl != NULL, "returned acl was NULL\n");
2588 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_USER;
2589 ExplicitAccess.Trustee.ptstrName = (LPWSTR)wszCurrentUser;
2590 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2591 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2592 ok(NewAcl != NULL, "returned acl was NULL\n");
2595 ExplicitAccess.grfAccessMode = REVOKE_ACCESS;
2596 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
2597 ExplicitAccess.Trustee.ptstrName = UsersSid;
2598 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2599 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2600 ok(NewAcl != NULL, "returned acl was NULL\n");
2604 FreeSid(EveryoneSid);
2605 HeapFree(GetProcessHeap(), 0, OldAcl);
2608 static void test_GetNamedSecurityInfoA(void)
2610 PSECURITY_DESCRIPTOR pSecDesc;
2612 SECURITY_DESCRIPTOR_CONTROL control;
2616 BOOL owner_defaulted;
2617 BOOL group_defaulted;
2620 CHAR windows_dir[MAX_PATH];
2622 if (!pGetNamedSecurityInfoA)
2624 win_skip("GetNamedSecurityInfoA is not available\n");
2628 ret = GetWindowsDirectoryA(windows_dir, MAX_PATH);
2629 ok(ret, "GetWindowsDirectory failed with error %d\n", GetLastError());
2631 SetLastError(0xdeadbeef);
2632 error = pGetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,
2633 OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION,
2634 NULL, NULL, NULL, NULL, &pSecDesc);
2635 if (error != ERROR_SUCCESS && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2637 win_skip("GetNamedSecurityInfoA is not implemented\n");
2640 ok(!error, "GetNamedSecurityInfo failed with error %d\n", error);
2642 ret = GetSecurityDescriptorControl(pSecDesc, &control, &revision);
2643 ok(ret, "GetSecurityDescriptorControl failed with error %d\n", GetLastError());
2644 ok((control & (SE_SELF_RELATIVE|SE_DACL_PRESENT)) == (SE_SELF_RELATIVE|SE_DACL_PRESENT) ||
2645 broken((control & (SE_SELF_RELATIVE|SE_DACL_PRESENT)) == SE_DACL_PRESENT), /* NT4 */
2646 "control (0x%x) doesn't have (SE_SELF_RELATIVE|SE_DACL_PRESENT) flags set\n", control);
2647 ok(revision == SECURITY_DESCRIPTOR_REVISION1, "revision was %d instead of 1\n", revision);
2649 isNT4 = (control & (SE_SELF_RELATIVE|SE_DACL_PRESENT)) == SE_DACL_PRESENT;
2651 ret = GetSecurityDescriptorOwner(pSecDesc, &owner, &owner_defaulted);
2652 ok(ret, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
2653 ok(owner != NULL, "owner should not be NULL\n");
2655 ret = GetSecurityDescriptorGroup(pSecDesc, &group, &group_defaulted);
2656 ok(ret, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2657 ok(group != NULL, "group should not be NULL\n");
2658 LocalFree(pSecDesc);
2661 /* NULL descriptor tests */
2664 win_skip("NT4 does not support GetNamedSecutityInfo with a NULL descriptor\n");
2668 error = pGetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,DACL_SECURITY_INFORMATION,
2669 NULL, NULL, NULL, NULL, NULL);
2670 ok(error==ERROR_INVALID_PARAMETER, "GetNamedSecurityInfo failed with error %d\n", error);
2672 error = pGetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,DACL_SECURITY_INFORMATION,
2673 NULL, NULL, &dacl, NULL, NULL);
2674 ok(!error, "GetNamedSecurityInfo failed with error %d\n", error);
2675 ok(dacl != NULL, "dacl should not be NULL\n");
2677 error = pGetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,OWNER_SECURITY_INFORMATION,
2678 NULL, NULL, &dacl, NULL, NULL);
2679 ok(error==ERROR_INVALID_PARAMETER, "GetNamedSecurityInfo failed with error %d\n", error);
2682 static void test_ConvertStringSecurityDescriptor(void)
2685 PSECURITY_DESCRIPTOR pSD;
2686 static const WCHAR Blank[] = { 0 };
2690 const char *sidstring;
2697 { "D:(A;;GA;;;WD)", 0xdeadbeef, FALSE, ERROR_UNKNOWN_REVISION },
2698 /* test ACE string type */
2699 { "D:(A;;GA;;;WD)", SDDL_REVISION_1, TRUE },
2700 { "D:(D;;GA;;;WD)", SDDL_REVISION_1, TRUE },
2701 { "ERROR:(D;;GA;;;WD)", SDDL_REVISION_1, FALSE, ERROR_INVALID_PARAMETER },
2702 /* test ACE string with spaces */
2703 { " D:(D;;GA;;;WD)", SDDL_REVISION_1, TRUE },
2704 { "D: (D;;GA;;;WD)", SDDL_REVISION_1, TRUE },
2705 { "D:( D;;GA;;;WD)", SDDL_REVISION_1, TRUE },
2706 { "D:(D ;;GA;;;WD)", SDDL_REVISION_1, FALSE, RPC_S_INVALID_STRING_UUID, ERROR_INVALID_ACL }, /* Vista+ */
2707 { "D:(D; ;GA;;;WD)", SDDL_REVISION_1, TRUE },
2708 { "D:(D;; GA;;;WD)", SDDL_REVISION_1, TRUE },
2709 { "D:(D;;GA ;;;WD)", SDDL_REVISION_1, FALSE, ERROR_INVALID_ACL },
2710 { "D:(D;;GA; ;;WD)", SDDL_REVISION_1, TRUE },
2711 { "D:(D;;GA;; ;WD)", SDDL_REVISION_1, TRUE },
2712 { "D:(D;;GA;;; WD)", SDDL_REVISION_1, TRUE },
2713 { "D:(D;;GA;;;WD )", SDDL_REVISION_1, TRUE },
2714 /* test ACE string access rights */
2715 { "D:(A;;GA;;;WD)", SDDL_REVISION_1, TRUE },
2716 { "D:(A;;GRGWGX;;;WD)", SDDL_REVISION_1, TRUE },
2717 { "D:(A;;RCSDWDWO;;;WD)", SDDL_REVISION_1, TRUE },
2718 { "D:(A;;RPWPCCDCLCSWLODTCR;;;WD)", SDDL_REVISION_1, TRUE },
2719 { "D:(A;;FAFRFWFX;;;WD)", SDDL_REVISION_1, TRUE },
2720 { "D:(A;;KAKRKWKX;;;WD)", SDDL_REVISION_1, TRUE },
2721 { "D:(A;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1, TRUE },
2722 { "S:(AU;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1, TRUE },
2723 /* test ACE string access right error case */
2724 { "D:(A;;ROB;;;WD)", SDDL_REVISION_1, FALSE, ERROR_INVALID_ACL },
2725 /* test behaviour with empty strings */
2726 { "", SDDL_REVISION_1, TRUE },
2727 /* test ACE string SID */
2728 { "D:(D;;GA;;;S-1-0-0)", SDDL_REVISION_1, TRUE },
2729 { "D:(D;;GA;;;Nonexistent account)", SDDL_REVISION_1, FALSE, ERROR_INVALID_ACL, ERROR_INVALID_SID } /* W2K */
2732 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA)
2734 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
2738 for (i = 0; i < sizeof(cssd)/sizeof(cssd[0]); i++)
2742 SetLastError(0xdeadbeef);
2743 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2744 cssd[i].sidstring, cssd[i].revision, &pSD, NULL);
2745 GLE = GetLastError();
2746 ok(ret == cssd[i].ret, "(%02u) Expected %s (%d)\n", i, cssd[i].ret ? "success" : "failure", GLE);
2748 ok(GLE == cssd[i].GLE ||
2749 (cssd[i].altGLE && GLE == cssd[i].altGLE),
2750 "(%02u) Unexpected last error %d\n", i, GLE);
2755 /* test behaviour with NULL parameters */
2756 SetLastError(0xdeadbeef);
2757 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2758 NULL, 0xdeadbeef, &pSD, NULL);
2760 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2761 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2764 SetLastError(0xdeadbeef);
2765 ret = pConvertStringSecurityDescriptorToSecurityDescriptorW(
2766 NULL, 0xdeadbeef, &pSD, NULL);
2767 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2768 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2771 SetLastError(0xdeadbeef);
2772 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2773 "D:(A;;ROB;;;WD)", 0xdeadbeef, NULL, NULL);
2774 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2775 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2778 SetLastError(0xdeadbeef);
2779 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2780 "D:(A;;ROB;;;WD)", SDDL_REVISION_1, NULL, NULL);
2781 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2782 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2785 /* test behaviour with empty strings */
2786 SetLastError(0xdeadbeef);
2787 ret = pConvertStringSecurityDescriptorToSecurityDescriptorW(
2788 Blank, SDDL_REVISION_1, &pSD, NULL);
2789 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2793 static void test_ConvertSecurityDescriptorToString(void)
2795 SECURITY_DESCRIPTOR desc;
2796 SECURITY_INFORMATION sec_info = OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION|SACL_SECURITY_INFORMATION;
2805 if (!pConvertSecurityDescriptorToStringSecurityDescriptorA)
2807 win_skip("ConvertSecurityDescriptorToStringSecurityDescriptor is not available\n");
2810 if (!pCreateWellKnownSid)
2812 win_skip("CreateWellKnownSid is not available\n");
2816 /* It seems Windows XP adds an extra character to the length of the string for each ACE in an ACL. We
2817 * don't replicate this feature so we only test len >= strlen+1. */
2818 #define CHECK_RESULT_AND_FREE(exp_str) \
2819 ok(strcmp(string, (exp_str)) == 0, "String mismatch (expected \"%s\", got \"%s\")\n", (exp_str), string); \
2820 ok(len >= (strlen(exp_str) + 1), "Length mismatch (expected %d, got %d)\n", lstrlen(exp_str) + 1, len); \
2823 #define CHECK_ONE_OF_AND_FREE(exp_str1, exp_str2) \
2824 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); \
2825 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); \
2828 InitializeSecurityDescriptor(&desc, SECURITY_DESCRIPTOR_REVISION);
2829 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2830 CHECK_RESULT_AND_FREE("");
2833 pCreateWellKnownSid(WinLocalSid, NULL, sid_buf, &size);
2834 SetSecurityDescriptorOwner(&desc, sid_buf, FALSE);
2835 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2836 CHECK_RESULT_AND_FREE("O:S-1-2-0");
2838 SetSecurityDescriptorOwner(&desc, sid_buf, TRUE);
2839 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2840 CHECK_RESULT_AND_FREE("O:S-1-2-0");
2842 size = sizeof(sid_buf);
2843 pCreateWellKnownSid(WinLocalSystemSid, NULL, sid_buf, &size);
2844 SetSecurityDescriptorOwner(&desc, sid_buf, TRUE);
2845 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2846 CHECK_RESULT_AND_FREE("O:SY");
2848 pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid);
2849 SetSecurityDescriptorGroup(&desc, psid, TRUE);
2850 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2851 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576");
2853 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, GROUP_SECURITY_INFORMATION, &string, &len), "Conversion failed\n");
2854 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
2856 pacl = (PACL)acl_buf;
2857 InitializeAcl(pacl, sizeof(acl_buf), ACL_REVISION);
2858 SetSecurityDescriptorDacl(&desc, TRUE, pacl, TRUE);
2859 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2860 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
2862 SetSecurityDescriptorDacl(&desc, TRUE, pacl, FALSE);
2863 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2864 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
2866 pConvertStringSidToSidA("S-1-5-6", &psid2);
2867 pAddAccessAllowedAceEx(pacl, ACL_REVISION, NO_PROPAGATE_INHERIT_ACE, 0xf0000000, psid2);
2868 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2869 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)");
2871 pAddAccessAllowedAceEx(pacl, ACL_REVISION, INHERIT_ONLY_ACE|INHERITED_ACE, 0x00000003, psid2);
2872 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2873 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)");
2875 pAddAccessDeniedAceEx(pacl, ACL_REVISION, OBJECT_INHERIT_ACE|CONTAINER_INHERIT_ACE, 0xffffffff, psid);
2876 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2877 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)");
2880 pacl = (PACL)acl_buf;
2881 InitializeAcl(pacl, sizeof(acl_buf), ACL_REVISION);
2882 SetSecurityDescriptorSacl(&desc, TRUE, pacl, FALSE);
2883 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2884 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:");
2886 /* fails in win2k */
2887 SetSecurityDescriptorDacl(&desc, TRUE, NULL, FALSE);
2888 pAddAuditAccessAceEx(pacl, ACL_REVISION, VALID_INHERIT_FLAGS, KEY_READ|KEY_WRITE, psid2, TRUE, TRUE);
2889 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len))
2891 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)", /* XP */
2892 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)" /* Vista */);
2895 /* fails in win2k */
2896 pAddAuditAccessAceEx(pacl, ACL_REVISION, NO_PROPAGATE_INHERIT_ACE, FILE_GENERIC_READ|FILE_GENERIC_WRITE, psid2, TRUE, FALSE);
2897 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len))
2899 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", /* XP */
2900 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)" /* Vista */);
2907 static void test_SetSecurityDescriptorControl (PSECURITY_DESCRIPTOR sec)
2909 SECURITY_DESCRIPTOR_CONTROL ref;
2910 SECURITY_DESCRIPTOR_CONTROL test;
2912 SECURITY_DESCRIPTOR_CONTROL const mutable
2913 = SE_DACL_AUTO_INHERIT_REQ | SE_SACL_AUTO_INHERIT_REQ
2914 | SE_DACL_AUTO_INHERITED | SE_SACL_AUTO_INHERITED
2915 | SE_DACL_PROTECTED | SE_SACL_PROTECTED
2916 | 0x00000040 | 0x00000080 /* not defined in winnt.h */
2918 SECURITY_DESCRIPTOR_CONTROL const immutable
2919 = SE_OWNER_DEFAULTED | SE_GROUP_DEFAULTED
2920 | SE_DACL_PRESENT | SE_DACL_DEFAULTED
2921 | SE_SACL_PRESENT | SE_SACL_DEFAULTED
2922 | SE_RM_CONTROL_VALID | SE_SELF_RELATIVE
2927 LPCSTR fmt = "Expected error %s, got %u\n";
2929 GetSecurityDescriptorControl (sec, &ref, &dwRevision);
2931 /* The mutable bits are mutable regardless of the truth of
2932 SE_DACL_PRESENT and/or SE_SACL_PRESENT */
2934 /* Check call barfs if any bit-of-interest is immutable */
2935 for (bit = 0; bit < 16; ++bit)
2937 SECURITY_DESCRIPTOR_CONTROL const bitOfInterest = 1 << bit;
2938 SECURITY_DESCRIPTOR_CONTROL setOrClear = ref & bitOfInterest;
2940 SECURITY_DESCRIPTOR_CONTROL ctrl;
2942 DWORD dwExpect = (bitOfInterest & immutable)
2943 ? ERROR_INVALID_PARAMETER : 0xbebecaca;
2944 LPCSTR strExpect = (bitOfInterest & immutable)
2945 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
2947 ctrl = (bitOfInterest & mutable) ? ref + bitOfInterest : ref;
2948 setOrClear ^= bitOfInterest;
2949 SetLastError (0xbebecaca);
2950 pSetSecurityDescriptorControl (sec, bitOfInterest, setOrClear);
2951 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
2952 GetSecurityDescriptorControl(sec, &test, &dwRevision);
2953 expect_eq(test, ctrl, int, "%x");
2956 setOrClear ^= bitOfInterest;
2957 SetLastError (0xbebecaca);
2958 pSetSecurityDescriptorControl (sec, bitOfInterest, setOrClear);
2959 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
2960 GetSecurityDescriptorControl (sec, &test, &dwRevision);
2961 expect_eq(test, ref, int, "%x");
2964 /* Check call barfs if any bit-to-set is immutable
2965 even when not a bit-of-interest */
2966 for (bit = 0; bit < 16; ++bit)
2968 SECURITY_DESCRIPTOR_CONTROL const bitsOfInterest = mutable;
2969 SECURITY_DESCRIPTOR_CONTROL setOrClear = ref & bitsOfInterest;
2971 SECURITY_DESCRIPTOR_CONTROL ctrl;
2973 DWORD dwExpect = ((1 << bit) & immutable)
2974 ? ERROR_INVALID_PARAMETER : 0xbebecaca;
2975 LPCSTR strExpect = ((1 << bit) & immutable)
2976 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
2978 ctrl = ((1 << bit) & immutable) ? test : ref | mutable;
2979 setOrClear ^= bitsOfInterest;
2980 SetLastError (0xbebecaca);
2981 pSetSecurityDescriptorControl (sec, bitsOfInterest, setOrClear | (1 << bit));
2982 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
2983 GetSecurityDescriptorControl(sec, &test, &dwRevision);
2984 expect_eq(test, ctrl, int, "%x");
2986 ctrl = ((1 << bit) & immutable) ? test : ref | (1 << bit);
2987 setOrClear ^= bitsOfInterest;
2988 SetLastError (0xbebecaca);
2989 pSetSecurityDescriptorControl (sec, bitsOfInterest, setOrClear | (1 << bit));
2990 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
2991 GetSecurityDescriptorControl(sec, &test, &dwRevision);
2992 expect_eq(test, ctrl, int, "%x");
2996 static void test_PrivateObjectSecurity(void)
2998 SECURITY_INFORMATION sec_info = OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION|SACL_SECURITY_INFORMATION;
2999 SECURITY_DESCRIPTOR_CONTROL ctrl;
3000 PSECURITY_DESCRIPTOR sec;
3006 PSECURITY_DESCRIPTOR buf;
3008 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA)
3010 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
3014 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3016 "G:S-1-5-21-93476-23408-4576"
3017 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)"
3018 "(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3019 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
3020 SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
3022 test_SetSecurityDescriptorControl(sec);
3026 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3028 "G:S-1-5-21-93476-23408-4576",
3029 SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
3031 test_SetSecurityDescriptorControl(sec);
3035 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3037 "G:S-1-5-21-93476-23408-4576"
3038 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3039 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
3040 buf = HeapAlloc(GetProcessHeap(), 0, dwDescSize);
3041 pSetSecurityDescriptorControl(sec, SE_DACL_PROTECTED, SE_DACL_PROTECTED);
3042 GetSecurityDescriptorControl(sec, &ctrl, &dwRevision);
3043 expect_eq(ctrl, 0x9014, int, "%x");
3045 ok(GetPrivateObjectSecurity(sec, GROUP_SECURITY_INFORMATION, buf, dwDescSize, &retSize),
3046 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3047 ok(retSize <= dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
3048 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3049 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
3050 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
3051 expect_eq(ctrl, 0x8000, int, "%x");
3053 ok(GetPrivateObjectSecurity(sec, GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION, buf, dwDescSize, &retSize),
3054 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3055 ok(retSize <= dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
3056 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed err=%u\n", GetLastError());
3057 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)",
3058 "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 */
3059 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
3060 expect_eq(ctrl & (~ SE_DACL_PROTECTED), 0x8004, int, "%x");
3062 ok(GetPrivateObjectSecurity(sec, sec_info, buf, dwDescSize, &retSize),
3063 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3064 ok(retSize == dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
3065 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3066 CHECK_ONE_OF_AND_FREE("O:SY"
3067 "G:S-1-5-21-93476-23408-4576"
3068 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3069 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
3071 "G:S-1-5-21-93476-23408-4576"
3072 "D:P(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3073 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)"); /* Win7 */
3074 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
3075 expect_eq(ctrl & (~ SE_DACL_PROTECTED), 0x8014, int, "%x");
3077 SetLastError(0xdeadbeef);
3078 ok(GetPrivateObjectSecurity(sec, sec_info, buf, 5, &retSize) == FALSE, "GetPrivateObjectSecurity should have failed\n");
3079 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected error ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
3082 HeapFree(GetProcessHeap(), 0, buf);
3084 #undef CHECK_RESULT_AND_FREE
3085 #undef CHECK_ONE_OF_AND_FREE
3087 static void test_acls(void)
3090 PACL pAcl = (PACL)buffer;
3093 SetLastError(0xdeadbeef);
3094 ret = InitializeAcl(pAcl, sizeof(ACL) - 1, ACL_REVISION);
3095 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
3097 win_skip("InitializeAcl is not implemented\n");
3101 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InitializeAcl with too small a buffer should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
3103 SetLastError(0xdeadbeef);
3104 ret = InitializeAcl(pAcl, 0xffffffff, ACL_REVISION);
3105 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "InitializeAcl with too large a buffer should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3107 SetLastError(0xdeadbeef);
3108 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION1);
3109 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "InitializeAcl(ACL_REVISION1) should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3111 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION2);
3112 ok(ret, "InitializeAcl(ACL_REVISION2) failed with error %d\n", GetLastError());
3114 ret = IsValidAcl(pAcl);
3115 ok(ret, "IsValidAcl failed with error %d\n", GetLastError());
3117 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION3);
3118 ok(ret, "InitializeAcl(ACL_REVISION3) failed with error %d\n", GetLastError());
3120 ret = IsValidAcl(pAcl);
3121 ok(ret, "IsValidAcl failed with error %d\n", GetLastError());
3123 SetLastError(0xdeadbeef);
3124 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION4);
3125 if (GetLastError() != ERROR_INVALID_PARAMETER)
3127 ok(ret, "InitializeAcl(ACL_REVISION4) failed with error %d\n", GetLastError());
3129 ret = IsValidAcl(pAcl);
3130 ok(ret, "IsValidAcl failed with error %d\n", GetLastError());
3133 win_skip("ACL_REVISION4 is not implemented on NT4\n");
3135 SetLastError(0xdeadbeef);
3136 ret = InitializeAcl(pAcl, sizeof(buffer), -1);
3137 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "InitializeAcl(-1) failed with error %d\n", GetLastError());
3140 static void test_GetSecurityInfo(void)
3143 PSECURITY_DESCRIPTOR sd;
3148 if (!pGetSecurityInfo)
3150 win_skip("GetSecurityInfo is not available\n");
3154 /* Create something. Files have lots of associated security info. */
3155 obj = CreateFile(myARGV[0], GENERIC_READ, FILE_SHARE_READ, NULL,
3156 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
3157 if (obj == INVALID_HANDLE_VALUE)
3159 skip("Couldn't create an object for GetSecurityInfo test\n");
3163 ret = pGetSecurityInfo(obj, SE_FILE_OBJECT,
3164 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
3165 &owner, &group, &dacl, NULL, &sd);
3166 if (ret == ERROR_CALL_NOT_IMPLEMENTED)
3168 win_skip("GetSecurityInfo is not implemented\n");
3172 ok(ret == ERROR_SUCCESS, "GetSecurityInfo returned %d\n", ret);
3173 ok(sd != NULL, "GetSecurityInfo\n");
3174 ok(owner != NULL, "GetSecurityInfo\n");
3175 ok(group != NULL, "GetSecurityInfo\n");
3177 ok(IsValidAcl(dacl), "GetSecurityInfo\n");
3179 win_skip("No ACL information returned\n");
3183 if (!pCreateWellKnownSid)
3185 win_skip("NULL parameter test would crash on NT4\n");
3190 /* If we don't ask for the security descriptor, Windows will still give us
3191 the other stuff, leaving us no way to free it. */
3192 ret = pGetSecurityInfo(obj, SE_FILE_OBJECT,
3193 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
3194 &owner, &group, &dacl, NULL, NULL);
3195 ok(ret == ERROR_SUCCESS, "GetSecurityInfo returned %d\n", ret);
3196 ok(owner != NULL, "GetSecurityInfo\n");
3197 ok(group != NULL, "GetSecurityInfo\n");
3199 ok(IsValidAcl(dacl), "GetSecurityInfo\n");
3201 win_skip("No ACL information returned\n");
3206 static void test_GetSidSubAuthority(void)
3210 if (!pGetSidSubAuthority || !pConvertStringSidToSidA || !pIsValidSid || !pGetSidSubAuthorityCount)
3212 win_skip("Some functions not available\n");
3215 /* Note: on windows passing in an invalid index like -1, lets GetSidSubAuthority return 0x05000000 but
3216 still GetLastError returns ERROR_SUCCESS then. We don't test these unlikely cornercases here for now */
3217 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576",&psid),"ConvertStringSidToSidA failed\n");
3218 ok(pIsValidSid(psid),"Sid is not valid\n");
3219 SetLastError(0xbebecaca);
3220 ok(*pGetSidSubAuthorityCount(psid) == 4,"GetSidSubAuthorityCount gave %d expected 4\n",*pGetSidSubAuthorityCount(psid));
3221 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3222 SetLastError(0xbebecaca);
3223 ok(*pGetSidSubAuthority(psid,0) == 21,"GetSidSubAuthority gave %d expected 21\n",*pGetSidSubAuthority(psid,0));
3224 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3225 SetLastError(0xbebecaca);
3226 ok(*pGetSidSubAuthority(psid,1) == 93476,"GetSidSubAuthority gave %d expected 93476\n",*pGetSidSubAuthority(psid,1));
3227 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3228 SetLastError(0xbebecaca);
3229 ok(pGetSidSubAuthority(psid,4) != NULL,"Expected out of bounds GetSidSubAuthority to return a non-NULL pointer\n");
3230 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3234 static void test_CheckTokenMembership(void)
3236 PTOKEN_GROUPS token_groups;
3238 HANDLE process_token, token;
3243 if (!pCheckTokenMembership)
3245 win_skip("CheckTokenMembership is not available\n");
3248 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &process_token);
3249 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
3251 ret = DuplicateToken(process_token, SecurityImpersonation, &token);
3252 ok(ret, "DuplicateToken failed with error %d\n", GetLastError());
3255 ret = GetTokenInformation(token, TokenGroups, NULL, 0, &size);
3256 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
3257 "GetTokenInformation(TokenGroups) %s with error %d\n",
3258 ret ? "succeeded" : "failed", GetLastError());
3259 token_groups = HeapAlloc(GetProcessHeap(), 0, size);
3260 ret = GetTokenInformation(token, TokenGroups, token_groups, size, &size);
3261 ok(ret, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
3263 for (i = 0; i < token_groups->GroupCount; i++)
3265 if (token_groups->Groups[i].Attributes & SE_GROUP_ENABLED)
3269 if (i == token_groups->GroupCount)
3271 HeapFree(GetProcessHeap(), 0, token_groups);
3273 skip("user not a member of any group\n");
3277 ret = pCheckTokenMembership(token, token_groups->Groups[i].Sid, &is_member);
3278 ok(ret, "CheckTokenMembership failed with error %d\n", GetLastError());
3279 ok(is_member, "CheckTokenMembership should have detected sid as member\n");
3281 ret = pCheckTokenMembership(NULL, token_groups->Groups[i].Sid, &is_member);
3282 ok(ret, "CheckTokenMembership failed with error %d\n", GetLastError());
3283 ok(is_member, "CheckTokenMembership should have detected sid as member\n");
3285 ret = pCheckTokenMembership(process_token, token_groups->Groups[i].Sid, &is_member);
3287 ok(!ret && GetLastError() == ERROR_NO_IMPERSONATION_TOKEN,
3288 "CheckTokenMembership with process token %s with error %d\n",
3289 ret ? "succeeded" : "failed", GetLastError());
3290 ok(!is_member, "CheckTokenMembership should have cleared is_member\n");
3293 HeapFree(GetProcessHeap(), 0, token_groups);
3295 CloseHandle(process_token);
3298 static void test_EqualSid(void)
3302 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
3303 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
3305 SetLastError(0xdeadbeef);
3306 ret = AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
3307 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &sid1);
3308 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
3310 win_skip("AllocateAndInitializeSid is not implemented\n");
3313 ok(ret, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3314 ok(GetLastError() == 0xdeadbeef,
3315 "AllocateAndInitializeSid shouldn't have set last error to %d\n",
3318 ret = AllocateAndInitializeSid(&SIDAuthWorld, 1, SECURITY_WORLD_RID,
3319 0, 0, 0, 0, 0, 0, 0, &sid2);
3320 ok(ret, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3322 SetLastError(0xdeadbeef);
3323 ret = EqualSid(sid1, sid2);
3324 ok(!ret, "World and domain admins sids shouldn't have been equal\n");
3325 ok(GetLastError() == ERROR_SUCCESS ||
3326 broken(GetLastError() == 0xdeadbeef), /* NT4 */
3327 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
3330 SetLastError(0xdeadbeef);
3331 sid2 = FreeSid(sid2);
3332 ok(!sid2, "FreeSid should have returned NULL instead of %p\n", sid2);
3333 ok(GetLastError() == 0xdeadbeef,
3334 "FreeSid shouldn't have set last error to %d\n",
3337 ret = AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
3338 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &sid2);
3339 ok(ret, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3341 SetLastError(0xdeadbeef);
3342 ret = EqualSid(sid1, sid2);
3343 ok(ret, "Same sids should have been equal\n");
3344 ok(GetLastError() == ERROR_SUCCESS ||
3345 broken(GetLastError() == 0xdeadbeef), /* NT4 */
3346 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
3349 ((SID *)sid2)->Revision = 2;
3350 SetLastError(0xdeadbeef);
3351 ret = EqualSid(sid1, sid2);
3352 ok(!ret, "EqualSid with invalid sid should have returned FALSE\n");
3353 ok(GetLastError() == ERROR_SUCCESS ||
3354 broken(GetLastError() == 0xdeadbeef), /* NT4 */
3355 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
3357 ((SID *)sid2)->Revision = SID_REVISION;
3363 START_TEST(security)
3370 test_process_security_child();
3376 test_CreateWellKnownSid();
3377 test_FileSecurity();
3380 test_LookupAccountSid();
3381 test_LookupAccountName();
3382 test_security_descriptor();
3383 test_process_security();
3384 test_impersonation_level();
3385 test_SetEntriesInAcl();
3386 test_GetNamedSecurityInfoA();
3387 test_ConvertStringSecurityDescriptor();
3388 test_ConvertSecurityDescriptorToString();
3389 test_PrivateObjectSecurity();
3391 test_GetSecurityInfo();
3392 test_GetSidSubAuthority();
3393 test_CheckTokenMembership();