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 typedef VOID (WINAPI *fnBuildTrusteeWithSidA)( PTRUSTEEA pTrustee, PSID pSid );
66 typedef VOID (WINAPI *fnBuildTrusteeWithNameA)( PTRUSTEEA pTrustee, LPSTR pName );
67 typedef VOID (WINAPI *fnBuildTrusteeWithObjectsAndNameA)( PTRUSTEEA pTrustee,
68 POBJECTS_AND_NAME_A pObjName,
69 SE_OBJECT_TYPE ObjectType,
71 LPSTR InheritedObjectTypeName,
73 typedef VOID (WINAPI *fnBuildTrusteeWithObjectsAndSidA)( PTRUSTEEA pTrustee,
74 POBJECTS_AND_SID pObjSid,
76 GUID* pInheritedObjectGuid,
78 typedef LPSTR (WINAPI *fnGetTrusteeNameA)( PTRUSTEEA pTrustee );
79 typedef BOOL (WINAPI *fnMakeSelfRelativeSD)( PSECURITY_DESCRIPTOR, PSECURITY_DESCRIPTOR, LPDWORD );
80 typedef BOOL (WINAPI *fnConvertSidToStringSidA)( PSID pSid, LPSTR *str );
81 typedef BOOL (WINAPI *fnConvertStringSidToSidA)( 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 typedef BOOL (WINAPI *fnGetFileSecurityA)(LPCSTR, SECURITY_INFORMATION,
90 PSECURITY_DESCRIPTOR, DWORD, LPDWORD);
91 typedef BOOL (WINAPI *fnSetFileSecurityA)(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 typedef DWORD (WINAPI *fnRtlAdjustPrivilege)(ULONG,BOOLEAN,BOOLEAN,PBOOLEAN);
100 typedef BOOL (WINAPI *fnCreateWellKnownSid)(WELL_KNOWN_SID_TYPE,PSID,PSID,DWORD*);
101 typedef BOOL (WINAPI *fnDuplicateTokenEx)(HANDLE,DWORD,LPSECURITY_ATTRIBUTES,
102 SECURITY_IMPERSONATION_LEVEL,TOKEN_TYPE,PHANDLE);
104 typedef NTSTATUS (WINAPI *fnLsaQueryInformationPolicy)(LSA_HANDLE,POLICY_INFORMATION_CLASS,PVOID*);
105 typedef NTSTATUS (WINAPI *fnLsaClose)(LSA_HANDLE);
106 typedef NTSTATUS (WINAPI *fnLsaFreeMemory)(PVOID);
107 typedef NTSTATUS (WINAPI *fnLsaOpenPolicy)(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;
121 fnBuildTrusteeWithSidA pBuildTrusteeWithSidA;
122 fnBuildTrusteeWithNameA pBuildTrusteeWithNameA;
123 fnBuildTrusteeWithObjectsAndNameA pBuildTrusteeWithObjectsAndNameA;
124 fnBuildTrusteeWithObjectsAndSidA pBuildTrusteeWithObjectsAndSidA;
125 fnGetTrusteeNameA pGetTrusteeNameA;
126 fnMakeSelfRelativeSD pMakeSelfRelativeSD;
127 fnConvertSidToStringSidA pConvertSidToStringSidA;
128 fnConvertStringSidToSidA pConvertStringSidToSidA;
129 fnGetFileSecurityA pGetFileSecurityA;
130 fnSetFileSecurityA pSetFileSecurityA;
131 fnRtlAdjustPrivilege pRtlAdjustPrivilege;
132 fnCreateWellKnownSid pCreateWellKnownSid;
133 fnDuplicateTokenEx pDuplicateTokenEx;
134 fnLsaQueryInformationPolicy pLsaQueryInformationPolicy;
135 fnLsaClose pLsaClose;
136 fnLsaFreeMemory pLsaFreeMemory;
137 fnLsaOpenPolicy pLsaOpenPolicy;
141 SID_IDENTIFIER_AUTHORITY auth;
145 static void init(void)
149 hntdll = GetModuleHandleA("ntdll.dll");
150 pNtQueryObject = (void *)GetProcAddress( hntdll, "NtQueryObject" );
151 pNtAccessCheck = (void *)GetProcAddress( hntdll, "NtAccessCheck" );
153 hmod = GetModuleHandle("advapi32.dll");
154 pAddAccessAllowedAceEx = (void *)GetProcAddress(hmod, "AddAccessAllowedAceEx");
155 pAddAccessDeniedAceEx = (void *)GetProcAddress(hmod, "AddAccessDeniedAceEx");
156 pAddAuditAccessAceEx = (void *)GetProcAddress(hmod, "AddAuditAccessAceEx");
157 pCheckTokenMembership = (void *)GetProcAddress(hmod, "CheckTokenMembership");
158 pConvertStringSecurityDescriptorToSecurityDescriptorA =
159 (void *)GetProcAddress(hmod, "ConvertStringSecurityDescriptorToSecurityDescriptorA" );
160 pConvertStringSecurityDescriptorToSecurityDescriptorW =
161 (void *)GetProcAddress(hmod, "ConvertStringSecurityDescriptorToSecurityDescriptorW" );
162 pConvertSecurityDescriptorToStringSecurityDescriptorA =
163 (void *)GetProcAddress(hmod, "ConvertSecurityDescriptorToStringSecurityDescriptorA" );
164 pGetFileSecurityA = (fnGetFileSecurityA)GetProcAddress(hmod, "GetFileSecurityA" );
165 pSetFileSecurityA = (fnSetFileSecurityA)GetProcAddress(hmod, "SetFileSecurityA" );
166 pCreateWellKnownSid = (fnCreateWellKnownSid)GetProcAddress( hmod, "CreateWellKnownSid" );
167 pGetNamedSecurityInfoA = (void *)GetProcAddress(hmod, "GetNamedSecurityInfoA");
168 pGetSidSubAuthority = (void *)GetProcAddress(hmod, "GetSidSubAuthority");
169 pGetSidSubAuthorityCount = (void *)GetProcAddress(hmod, "GetSidSubAuthorityCount");
170 pIsValidSid = (void *)GetProcAddress(hmod, "IsValidSid");
171 pMakeSelfRelativeSD = (void *)GetProcAddress(hmod, "MakeSelfRelativeSD");
172 pSetEntriesInAclW = (void *)GetProcAddress(hmod, "SetEntriesInAclW");
173 pSetSecurityDescriptorControl = (void *)GetProcAddress(hmod, "SetSecurityDescriptorControl");
174 pGetSecurityInfo = (void *)GetProcAddress(hmod, "GetSecurityInfo");
176 myARGC = winetest_get_mainargs( &myARGV );
179 static void test_str_sid(const char *str_sid)
184 if (pConvertStringSidToSidA(str_sid, &psid))
186 if (pConvertSidToStringSidA(psid, &temp))
188 trace(" %s: %s\n", str_sid, temp);
195 if (GetLastError() != ERROR_INVALID_SID)
196 trace(" %s: couldn't be converted, returned %d\n", str_sid, GetLastError());
198 trace(" %s: couldn't be converted\n", str_sid);
202 static void test_sid(void)
204 struct sidRef refs[] = {
205 { { {0x00,0x00,0x33,0x44,0x55,0x66} }, "S-1-860116326-1" },
206 { { {0x00,0x00,0x01,0x02,0x03,0x04} }, "S-1-16909060-1" },
207 { { {0x00,0x00,0x00,0x01,0x02,0x03} }, "S-1-66051-1" },
208 { { {0x00,0x00,0x00,0x00,0x01,0x02} }, "S-1-258-1" },
209 { { {0x00,0x00,0x00,0x00,0x00,0x02} }, "S-1-2-1" },
210 { { {0x00,0x00,0x00,0x00,0x00,0x0c} }, "S-1-12-1" },
212 const char noSubAuthStr[] = "S-1-5";
219 pConvertSidToStringSidA = (fnConvertSidToStringSidA)
220 GetProcAddress( hmod, "ConvertSidToStringSidA" );
221 if( !pConvertSidToStringSidA )
223 pConvertStringSidToSidA = (fnConvertStringSidToSidA)
224 GetProcAddress( hmod, "ConvertStringSidToSidA" );
225 if( !pConvertStringSidToSidA )
228 r = pConvertStringSidToSidA( NULL, NULL );
229 ok( !r, "expected failure with NULL parameters\n" );
230 if( GetLastError() == ERROR_CALL_NOT_IMPLEMENTED )
232 ok( GetLastError() == ERROR_INVALID_PARAMETER,
233 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
236 r = pConvertStringSidToSidA( refs[0].refStr, NULL );
237 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
238 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
241 r = pConvertStringSidToSidA( NULL, &str );
242 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
243 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
246 r = pConvertStringSidToSidA( noSubAuthStr, &psid );
248 "expected failure with no sub authorities\n" );
249 ok( GetLastError() == ERROR_INVALID_SID,
250 "expected GetLastError() is ERROR_INVALID_SID, got %d\n",
253 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid), "ConvertStringSidToSidA failed\n");
255 ok(pisid->SubAuthorityCount == 4, "Invalid sub authority count - expected 4, got %d\n", pisid->SubAuthorityCount);
256 ok(pisid->SubAuthority[0] == 21, "Invalid subauthority 0 - expceted 21, got %d\n", pisid->SubAuthority[0]);
257 ok(pisid->SubAuthority[3] == 4576, "Invalid subauthority 0 - expceted 4576, got %d\n", pisid->SubAuthority[3]);
260 for( i = 0; i < sizeof(refs) / sizeof(refs[0]); i++ )
264 r = AllocateAndInitializeSid( &refs[i].auth, 1,1,0,0,0,0,0,0,0,
266 ok( r, "failed to allocate sid\n" );
267 r = pConvertSidToStringSidA( psid, &str );
268 ok( r, "failed to convert sid\n" );
271 ok( !strcmp( str, refs[i].refStr ),
272 "incorrect sid, expected %s, got %s\n", refs[i].refStr, str );
278 r = pConvertStringSidToSidA( refs[i].refStr, &psid );
279 ok( r, "failed to parse sid string\n" );
282 !memcmp( pisid->IdentifierAuthority.Value, refs[i].auth.Value,
283 sizeof(refs[i].auth) ),
284 "string sid %s didn't parse to expected value\n"
285 "(got 0x%04x%08x, expected 0x%04x%08x)\n",
287 MAKEWORD( pisid->IdentifierAuthority.Value[1],
288 pisid->IdentifierAuthority.Value[0] ),
289 MAKELONG( MAKEWORD( pisid->IdentifierAuthority.Value[5],
290 pisid->IdentifierAuthority.Value[4] ),
291 MAKEWORD( pisid->IdentifierAuthority.Value[3],
292 pisid->IdentifierAuthority.Value[2] ) ),
293 MAKEWORD( refs[i].auth.Value[1], refs[i].auth.Value[0] ),
294 MAKELONG( MAKEWORD( refs[i].auth.Value[5], refs[i].auth.Value[4] ),
295 MAKEWORD( refs[i].auth.Value[3], refs[i].auth.Value[2] ) ) );
300 trace("String SIDs:\n");
341 static void test_trustee(void)
343 GUID ObjectType = {0x12345678, 0x1234, 0x5678, {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}};
344 GUID InheritedObjectType = {0x23456789, 0x2345, 0x6786, {0x2, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}};
346 OBJECTS_AND_NAME_ oan;
350 char szObjectTypeName[] = "ObjectTypeName";
351 char szInheritedObjectTypeName[] = "InheritedObjectTypeName";
352 char szTrusteeName[] = "szTrusteeName";
353 SID_IDENTIFIER_AUTHORITY auth = { {0x11,0x22,0,0,0, 0} };
355 memset( &ZeroGuid, 0x00, sizeof (ZeroGuid) );
357 pBuildTrusteeWithSidA = (fnBuildTrusteeWithSidA)
358 GetProcAddress( hmod, "BuildTrusteeWithSidA" );
359 pBuildTrusteeWithNameA = (fnBuildTrusteeWithNameA)
360 GetProcAddress( hmod, "BuildTrusteeWithNameA" );
361 pBuildTrusteeWithObjectsAndNameA = (fnBuildTrusteeWithObjectsAndNameA)
362 GetProcAddress (hmod, "BuildTrusteeWithObjectsAndNameA" );
363 pBuildTrusteeWithObjectsAndSidA = (fnBuildTrusteeWithObjectsAndSidA)
364 GetProcAddress (hmod, "BuildTrusteeWithObjectsAndSidA" );
365 pGetTrusteeNameA = (fnGetTrusteeNameA)
366 GetProcAddress (hmod, "GetTrusteeNameA" );
367 if( !pBuildTrusteeWithSidA || !pBuildTrusteeWithNameA ||
368 !pBuildTrusteeWithObjectsAndNameA || !pBuildTrusteeWithObjectsAndSidA ||
372 if ( ! AllocateAndInitializeSid( &auth, 1, 42, 0,0,0,0,0,0,0,&psid ) )
374 trace( "failed to init SID\n" );
378 /* test BuildTrusteeWithSidA */
379 memset( &trustee, 0xff, sizeof trustee );
380 pBuildTrusteeWithSidA( &trustee, psid );
382 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
383 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
384 "MultipleTrusteeOperation wrong\n");
385 ok( trustee.TrusteeForm == TRUSTEE_IS_SID, "TrusteeForm wrong\n");
386 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
387 ok( trustee.ptstrName == psid, "ptstrName wrong\n" );
389 /* test BuildTrusteeWithObjectsAndSidA (test 1) */
390 memset( &trustee, 0xff, sizeof trustee );
391 memset( &oas, 0xff, sizeof(oas) );
392 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, &ObjectType,
393 &InheritedObjectType, psid);
395 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
396 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
397 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
398 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
399 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
401 ok(oas.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
402 ok(!memcmp(&oas.ObjectTypeGuid, &ObjectType, sizeof(GUID)), "ObjectTypeGuid wrong\n");
403 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
404 ok(oas.pSid == psid, "pSid wrong\n");
406 /* test GetTrusteeNameA */
407 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oas, "GetTrusteeName returned wrong value\n");
409 /* test BuildTrusteeWithObjectsAndSidA (test 2) */
410 memset( &trustee, 0xff, sizeof trustee );
411 memset( &oas, 0xff, sizeof(oas) );
412 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, NULL,
413 &InheritedObjectType, psid);
415 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
416 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
417 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
418 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
419 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
421 ok(oas.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
422 ok(!memcmp(&oas.ObjectTypeGuid, &ZeroGuid, sizeof(GUID)), "ObjectTypeGuid wrong\n");
423 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
424 ok(oas.pSid == psid, "pSid wrong\n");
428 /* test BuildTrusteeWithNameA */
429 memset( &trustee, 0xff, sizeof trustee );
430 pBuildTrusteeWithNameA( &trustee, szTrusteeName );
432 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
433 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
434 "MultipleTrusteeOperation wrong\n");
435 ok( trustee.TrusteeForm == TRUSTEE_IS_NAME, "TrusteeForm wrong\n");
436 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
437 ok( trustee.ptstrName == szTrusteeName, "ptstrName wrong\n" );
439 /* test BuildTrusteeWithObjectsAndNameA (test 1) */
440 memset( &trustee, 0xff, sizeof trustee );
441 memset( &oan, 0xff, sizeof(oan) );
442 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
443 szInheritedObjectTypeName, szTrusteeName);
445 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
446 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
447 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
448 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
449 ok(trustee.ptstrName == (LPTSTR)&oan, "ptstrName wrong\n");
451 ok(oan.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
452 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
453 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
454 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
456 /* test GetTrusteeNameA */
457 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oan, "GetTrusteeName returned wrong value\n");
459 /* test BuildTrusteeWithObjectsAndNameA (test 2) */
460 memset( &trustee, 0xff, sizeof trustee );
461 memset( &oan, 0xff, sizeof(oan) );
462 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, NULL,
463 szInheritedObjectTypeName, szTrusteeName);
465 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
466 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
467 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
468 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
469 ok(trustee.ptstrName == (LPSTR)&oan, "ptstrName wrong\n");
471 ok(oan.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
472 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
473 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
474 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
476 /* test BuildTrusteeWithObjectsAndNameA (test 3) */
477 memset( &trustee, 0xff, sizeof trustee );
478 memset( &oan, 0xff, sizeof(oan) );
479 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
480 NULL, szTrusteeName);
482 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
483 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
484 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
485 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
486 ok(trustee.ptstrName == (LPTSTR)&oan, "ptstrName wrong\n");
488 ok(oan.ObjectsPresent == ACE_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
489 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
490 ok(oan.InheritedObjectTypeName == NULL, "InheritedObjectTypeName wrong\n");
491 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
494 /* If the first isn't defined, assume none is */
495 #ifndef SE_MIN_WELL_KNOWN_PRIVILEGE
496 #define SE_MIN_WELL_KNOWN_PRIVILEGE 2L
497 #define SE_CREATE_TOKEN_PRIVILEGE 2L
498 #define SE_ASSIGNPRIMARYTOKEN_PRIVILEGE 3L
499 #define SE_LOCK_MEMORY_PRIVILEGE 4L
500 #define SE_INCREASE_QUOTA_PRIVILEGE 5L
501 #define SE_MACHINE_ACCOUNT_PRIVILEGE 6L
502 #define SE_TCB_PRIVILEGE 7L
503 #define SE_SECURITY_PRIVILEGE 8L
504 #define SE_TAKE_OWNERSHIP_PRIVILEGE 9L
505 #define SE_LOAD_DRIVER_PRIVILEGE 10L
506 #define SE_SYSTEM_PROFILE_PRIVILEGE 11L
507 #define SE_SYSTEMTIME_PRIVILEGE 12L
508 #define SE_PROF_SINGLE_PROCESS_PRIVILEGE 13L
509 #define SE_INC_BASE_PRIORITY_PRIVILEGE 14L
510 #define SE_CREATE_PAGEFILE_PRIVILEGE 15L
511 #define SE_CREATE_PERMANENT_PRIVILEGE 16L
512 #define SE_BACKUP_PRIVILEGE 17L
513 #define SE_RESTORE_PRIVILEGE 18L
514 #define SE_SHUTDOWN_PRIVILEGE 19L
515 #define SE_DEBUG_PRIVILEGE 20L
516 #define SE_AUDIT_PRIVILEGE 21L
517 #define SE_SYSTEM_ENVIRONMENT_PRIVILEGE 22L
518 #define SE_CHANGE_NOTIFY_PRIVILEGE 23L
519 #define SE_REMOTE_SHUTDOWN_PRIVILEGE 24L
520 #define SE_UNDOCK_PRIVILEGE 25L
521 #define SE_SYNC_AGENT_PRIVILEGE 26L
522 #define SE_ENABLE_DELEGATION_PRIVILEGE 27L
523 #define SE_MANAGE_VOLUME_PRIVILEGE 28L
524 #define SE_IMPERSONATE_PRIVILEGE 29L
525 #define SE_CREATE_GLOBAL_PRIVILEGE 30L
526 #define SE_MAX_WELL_KNOWN_PRIVILEGE SE_CREATE_GLOBAL_PRIVILEGE
527 #endif /* ndef SE_MIN_WELL_KNOWN_PRIVILEGE */
529 static void test_allocateLuid(void)
531 BOOL (WINAPI *pAllocateLocallyUniqueId)(PLUID);
535 pAllocateLocallyUniqueId = (void*)GetProcAddress(hmod, "AllocateLocallyUniqueId");
536 if (!pAllocateLocallyUniqueId) return;
538 ret = pAllocateLocallyUniqueId(&luid1);
539 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
543 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
544 ret = pAllocateLocallyUniqueId(&luid2);
546 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
547 ok(luid1.LowPart > SE_MAX_WELL_KNOWN_PRIVILEGE || luid1.HighPart != 0,
548 "AllocateLocallyUniqueId returned a well-known LUID\n");
549 ok(luid1.LowPart != luid2.LowPart || luid1.HighPart != luid2.HighPart,
550 "AllocateLocallyUniqueId returned non-unique LUIDs\n");
551 ret = pAllocateLocallyUniqueId(NULL);
552 ok( !ret && GetLastError() == ERROR_NOACCESS,
553 "AllocateLocallyUniqueId(NULL) didn't return ERROR_NOACCESS: %d\n",
557 static void test_lookupPrivilegeName(void)
559 BOOL (WINAPI *pLookupPrivilegeNameA)(LPCSTR, PLUID, LPSTR, LPDWORD);
560 char buf[MAX_PATH]; /* arbitrary, seems long enough */
561 DWORD cchName = sizeof(buf);
562 LUID luid = { 0, 0 };
566 /* check whether it's available first */
567 pLookupPrivilegeNameA = (void*)GetProcAddress(hmod, "LookupPrivilegeNameA");
568 if (!pLookupPrivilegeNameA) return;
569 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
570 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
571 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
574 /* check with a short buffer */
576 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
577 ret = pLookupPrivilegeNameA(NULL, &luid, NULL, &cchName);
578 ok( !ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
579 "LookupPrivilegeNameA didn't fail with ERROR_INSUFFICIENT_BUFFER: %d\n",
581 ok(cchName == strlen("SeCreateTokenPrivilege") + 1,
582 "LookupPrivilegeNameA returned an incorrect required length for\n"
583 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName,
584 lstrlenA("SeCreateTokenPrivilege") + 1);
585 /* check a known value and its returned length on success */
586 cchName = sizeof(buf);
587 ok(pLookupPrivilegeNameA(NULL, &luid, buf, &cchName) &&
588 cchName == strlen("SeCreateTokenPrivilege"),
589 "LookupPrivilegeNameA returned an incorrect output length for\n"
590 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName,
591 (int)strlen("SeCreateTokenPrivilege"));
592 /* check known values */
593 for (i = SE_MIN_WELL_KNOWN_PRIVILEGE; i <= SE_MAX_WELL_KNOWN_PRIVILEGE; i++)
596 cchName = sizeof(buf);
597 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
598 ok( ret || GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
599 "LookupPrivilegeNameA(0.%d) failed: %d\n", i, GetLastError());
601 /* check a bogus LUID */
602 luid.LowPart = 0xdeadbeef;
603 cchName = sizeof(buf);
604 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
605 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
606 "LookupPrivilegeNameA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
608 /* check on a bogus system */
609 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
610 cchName = sizeof(buf);
611 ret = pLookupPrivilegeNameA("b0gu5.Nam3", &luid, buf, &cchName);
612 ok( !ret && (GetLastError() == RPC_S_SERVER_UNAVAILABLE ||
613 GetLastError() == RPC_S_INVALID_NET_ADDR) /* w2k8 */,
614 "LookupPrivilegeNameA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
624 static void test_lookupPrivilegeValue(void)
626 static const struct NameToLUID privs[] = {
627 { "SeCreateTokenPrivilege", SE_CREATE_TOKEN_PRIVILEGE },
628 { "SeAssignPrimaryTokenPrivilege", SE_ASSIGNPRIMARYTOKEN_PRIVILEGE },
629 { "SeLockMemoryPrivilege", SE_LOCK_MEMORY_PRIVILEGE },
630 { "SeIncreaseQuotaPrivilege", SE_INCREASE_QUOTA_PRIVILEGE },
631 { "SeMachineAccountPrivilege", SE_MACHINE_ACCOUNT_PRIVILEGE },
632 { "SeTcbPrivilege", SE_TCB_PRIVILEGE },
633 { "SeSecurityPrivilege", SE_SECURITY_PRIVILEGE },
634 { "SeTakeOwnershipPrivilege", SE_TAKE_OWNERSHIP_PRIVILEGE },
635 { "SeLoadDriverPrivilege", SE_LOAD_DRIVER_PRIVILEGE },
636 { "SeSystemProfilePrivilege", SE_SYSTEM_PROFILE_PRIVILEGE },
637 { "SeSystemtimePrivilege", SE_SYSTEMTIME_PRIVILEGE },
638 { "SeProfileSingleProcessPrivilege", SE_PROF_SINGLE_PROCESS_PRIVILEGE },
639 { "SeIncreaseBasePriorityPrivilege", SE_INC_BASE_PRIORITY_PRIVILEGE },
640 { "SeCreatePagefilePrivilege", SE_CREATE_PAGEFILE_PRIVILEGE },
641 { "SeCreatePermanentPrivilege", SE_CREATE_PERMANENT_PRIVILEGE },
642 { "SeBackupPrivilege", SE_BACKUP_PRIVILEGE },
643 { "SeRestorePrivilege", SE_RESTORE_PRIVILEGE },
644 { "SeShutdownPrivilege", SE_SHUTDOWN_PRIVILEGE },
645 { "SeDebugPrivilege", SE_DEBUG_PRIVILEGE },
646 { "SeAuditPrivilege", SE_AUDIT_PRIVILEGE },
647 { "SeSystemEnvironmentPrivilege", SE_SYSTEM_ENVIRONMENT_PRIVILEGE },
648 { "SeChangeNotifyPrivilege", SE_CHANGE_NOTIFY_PRIVILEGE },
649 { "SeRemoteShutdownPrivilege", SE_REMOTE_SHUTDOWN_PRIVILEGE },
650 { "SeUndockPrivilege", SE_UNDOCK_PRIVILEGE },
651 { "SeSyncAgentPrivilege", SE_SYNC_AGENT_PRIVILEGE },
652 { "SeEnableDelegationPrivilege", SE_ENABLE_DELEGATION_PRIVILEGE },
653 { "SeManageVolumePrivilege", SE_MANAGE_VOLUME_PRIVILEGE },
654 { "SeImpersonatePrivilege", SE_IMPERSONATE_PRIVILEGE },
655 { "SeCreateGlobalPrivilege", SE_CREATE_GLOBAL_PRIVILEGE },
657 BOOL (WINAPI *pLookupPrivilegeValueA)(LPCSTR, LPCSTR, PLUID);
662 /* check whether it's available first */
663 pLookupPrivilegeValueA = (void*)GetProcAddress(hmod, "LookupPrivilegeValueA");
664 if (!pLookupPrivilegeValueA) return;
665 ret = pLookupPrivilegeValueA(NULL, "SeCreateTokenPrivilege", &luid);
666 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
669 /* check a bogus system name */
670 ret = pLookupPrivilegeValueA("b0gu5.Nam3", "SeCreateTokenPrivilege", &luid);
671 ok( !ret && (GetLastError() == RPC_S_SERVER_UNAVAILABLE ||
672 GetLastError() == RPC_S_INVALID_NET_ADDR) /* w2k8 */,
673 "LookupPrivilegeValueA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
675 /* check a NULL string */
676 ret = pLookupPrivilegeValueA(NULL, 0, &luid);
677 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
678 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
680 /* check a bogus privilege name */
681 ret = pLookupPrivilegeValueA(NULL, "SeBogusPrivilege", &luid);
682 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
683 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
685 /* check case insensitive */
686 ret = pLookupPrivilegeValueA(NULL, "sEcREATEtOKENpRIVILEGE", &luid);
688 "LookupPrivilegeValueA(NULL, sEcREATEtOKENpRIVILEGE, &luid) failed: %d\n",
690 for (i = 0; i < sizeof(privs) / sizeof(privs[0]); i++)
692 /* Not all privileges are implemented on all Windows versions, so
693 * don't worry if the call fails
695 if (pLookupPrivilegeValueA(NULL, privs[i].name, &luid))
697 ok(luid.LowPart == privs[i].lowPart,
698 "LookupPrivilegeValueA returned an invalid LUID for %s\n",
704 static void test_luid(void)
707 test_lookupPrivilegeName();
708 test_lookupPrivilegeValue();
711 static void test_FileSecurity(void)
713 char wintmpdir [MAX_PATH];
714 char path [MAX_PATH];
715 char file [MAX_PATH];
721 SECURITY_INFORMATION const request = OWNER_SECURITY_INFORMATION
722 | GROUP_SECURITY_INFORMATION
723 | DACL_SECURITY_INFORMATION;
725 if (!pGetFileSecurityA) {
726 win_skip ("GetFileSecurity is not available\n");
730 if (!pSetFileSecurityA) {
731 win_skip ("SetFileSecurity is not available\n");
735 if (!GetTempPathA (sizeof (wintmpdir), wintmpdir)) {
736 win_skip ("GetTempPathA failed\n");
740 /* Create a temporary directory and in it a temporary file */
741 strcat (strcpy (path, wintmpdir), "rary");
742 SetLastError(0xdeadbeef);
743 rc = CreateDirectoryA (path, NULL);
744 ok (rc || GetLastError() == ERROR_ALREADY_EXISTS, "CreateDirectoryA "
745 "failed for '%s' with %d\n", path, GetLastError());
747 strcat (strcpy (file, path), "\\ess");
748 SetLastError(0xdeadbeef);
749 fh = CreateFileA (file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
750 ok (fh != INVALID_HANDLE_VALUE, "CreateFileA "
751 "failed for '%s' with %d\n", file, GetLastError());
754 /* For the temporary file ... */
756 /* Get size needed */
758 SetLastError(0xdeadbeef);
759 rc = pGetFileSecurityA (file, request, NULL, 0, &retSize);
760 if (!rc && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)) {
761 win_skip("GetFileSecurityA is not implemented\n");
764 ok (!rc, "GetFileSecurityA "
765 "was expected to fail for '%s'\n", file);
766 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetFileSecurityA "
767 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
768 ok (retSize > sizeof (SECURITY_DESCRIPTOR), "GetFileSecurityA returned size %d\n", retSize);
771 sd = HeapAlloc (GetProcessHeap (), 0, sdSize);
773 /* Get security descriptor for real */
775 SetLastError(0xdeadbeef);
776 rc = pGetFileSecurityA (file, request, sd, sdSize, &retSize);
777 ok (rc, "GetFileSecurityA "
778 "was not expected to fail '%s': %d\n", file, GetLastError());
779 ok (retSize == sdSize ||
780 broken(retSize == 0), /* NT4 */
781 "GetFileSecurityA returned size %d; expected %d\n", retSize, sdSize);
783 /* Use it to set security descriptor */
784 SetLastError(0xdeadbeef);
785 rc = pSetFileSecurityA (file, request, sd);
786 ok (rc, "SetFileSecurityA "
787 "was not expected to fail '%s': %d\n", file, GetLastError());
789 HeapFree (GetProcessHeap (), 0, sd);
791 /* Repeat for the temporary directory ... */
793 /* Get size needed */
795 SetLastError(0xdeadbeef);
796 rc = pGetFileSecurityA (path, request, NULL, 0, &retSize);
797 ok (!rc, "GetFileSecurityA "
798 "was expected to fail for '%s'\n", path);
799 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetFileSecurityA "
800 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
801 ok (retSize > sizeof (SECURITY_DESCRIPTOR), "GetFileSecurityA returned size %d\n", retSize);
804 sd = HeapAlloc (GetProcessHeap (), 0, sdSize);
806 /* Get security descriptor for real */
808 SetLastError(0xdeadbeef);
809 rc = pGetFileSecurityA (path, request, sd, sdSize, &retSize);
810 ok (rc, "GetFileSecurityA "
811 "was not expected to fail '%s': %d\n", path, GetLastError());
812 ok (retSize == sdSize ||
813 broken(retSize == 0), /* NT4 */
814 "GetFileSecurityA returned size %d; expected %d\n", retSize, sdSize);
816 /* Use it to set security descriptor */
817 SetLastError(0xdeadbeef);
818 rc = pSetFileSecurityA (path, request, sd);
819 ok (rc, "SetFileSecurityA "
820 "was not expected to fail '%s': %d\n", path, GetLastError());
821 HeapFree (GetProcessHeap (), 0, sd);
824 strcpy (wintmpdir, "\\Should not exist");
825 SetLastError(0xdeadbeef);
826 rc = pGetFileSecurityA (wintmpdir, OWNER_SECURITY_INFORMATION, NULL, 0, &sdSize);
827 ok (!rc, "GetFileSecurityA should fail for not existing directories/files\n");
828 ok (GetLastError() == ERROR_FILE_NOT_FOUND,
829 "last error ERROR_FILE_NOT_FOUND expected, got %d\n", GetLastError());
832 /* Remove temporary file and directory */
834 RemoveDirectoryA(path);
837 static void test_AccessCheck(void)
839 PSID EveryoneSid = NULL, AdminSid = NULL, UsersSid = NULL;
841 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL;
842 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
843 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
844 GENERIC_MAPPING Mapping = { KEY_READ, KEY_WRITE, KEY_EXECUTE, KEY_ALL_ACCESS };
851 PRIVILEGE_SET *PrivSet;
856 NTSTATUS ntret, ntAccessStatus;
858 NtDllModule = GetModuleHandle("ntdll.dll");
861 skip("not running on NT, skipping test\n");
864 pRtlAdjustPrivilege = (fnRtlAdjustPrivilege)
865 GetProcAddress(NtDllModule, "RtlAdjustPrivilege");
866 if (!pRtlAdjustPrivilege)
868 win_skip("missing RtlAdjustPrivilege, skipping test\n");
872 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
873 res = InitializeAcl(Acl, 256, ACL_REVISION);
874 if(!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
876 skip("ACLs not implemented - skipping tests\n");
877 HeapFree(GetProcessHeap(), 0, Acl);
880 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
882 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
883 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
885 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
886 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdminSid);
887 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
889 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
890 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &UsersSid);
891 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
893 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
895 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
896 ok(res, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
898 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
899 ok(res, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
901 PrivSetLen = FIELD_OFFSET(PRIVILEGE_SET, Privilege[16]);
902 PrivSet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, PrivSetLen);
903 PrivSet->PrivilegeCount = 16;
905 res = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &ProcessToken);
906 ok(res, "OpenProcessToken failed with error %d\n", GetLastError());
908 pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, FALSE, TRUE, &Enabled);
910 res = DuplicateToken(ProcessToken, SecurityImpersonation, &Token);
911 ok(res, "DuplicateToken failed with error %d\n", GetLastError());
913 /* SD without owner/group */
914 SetLastError(0xdeadbeef);
915 Access = AccessStatus = 0xdeadbeef;
916 ret = AccessCheck(SecurityDescriptor, Token, KEY_QUERY_VALUE, &Mapping,
917 PrivSet, &PrivSetLen, &Access, &AccessStatus);
918 err = GetLastError();
919 ok(!ret && err == ERROR_INVALID_SECURITY_DESCR, "AccessCheck should have "
920 "failed with ERROR_INVALID_SECURITY_DESCR, instead of %d\n", err);
921 ok(Access == 0xdeadbeef && AccessStatus == 0xdeadbeef,
922 "Access and/or AccessStatus were changed!\n");
924 /* Set owner and group */
925 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
926 ok(res, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
927 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, TRUE);
928 ok(res, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
930 /* Generic access mask */
931 SetLastError(0xdeadbeef);
932 Access = AccessStatus = 0xdeadbeef;
933 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
934 PrivSet, &PrivSetLen, &Access, &AccessStatus);
935 err = GetLastError();
936 ok(!ret && err == ERROR_GENERIC_NOT_MAPPED, "AccessCheck should have failed "
937 "with ERROR_GENERIC_NOT_MAPPED, instead of %d\n", err);
938 ok(Access == 0xdeadbeef && AccessStatus == 0xdeadbeef,
939 "Access and/or AccessStatus were changed!\n");
941 /* Generic access mask - no privilegeset buffer */
942 SetLastError(0xdeadbeef);
943 Access = AccessStatus = 0xdeadbeef;
944 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
945 NULL, &PrivSetLen, &Access, &AccessStatus);
946 err = GetLastError();
947 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
948 "with ERROR_NOACCESS, instead of %d\n", err);
949 ok(Access == 0xdeadbeef && AccessStatus == 0xdeadbeef,
950 "Access and/or AccessStatus were changed!\n");
952 /* Generic access mask - no returnlength */
953 SetLastError(0xdeadbeef);
954 Access = AccessStatus = 0xdeadbeef;
955 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
956 PrivSet, NULL, &Access, &AccessStatus);
957 err = GetLastError();
958 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
959 "with ERROR_NOACCESS, instead of %d\n", err);
960 ok(Access == 0xdeadbeef && AccessStatus == 0xdeadbeef,
961 "Access and/or AccessStatus were changed!\n");
963 /* Generic access mask - no privilegeset buffer, no returnlength */
964 SetLastError(0xdeadbeef);
965 Access = AccessStatus = 0xdeadbeef;
966 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
967 NULL, NULL, &Access, &AccessStatus);
968 err = GetLastError();
969 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
970 "with ERROR_NOACCESS, instead of %d\n", err);
971 ok(Access == 0xdeadbeef && AccessStatus == 0xdeadbeef,
972 "Access and/or AccessStatus were changed!\n");
974 /* sd with no dacl present */
975 Access = AccessStatus = 0xdeadbeef;
976 ret = SetSecurityDescriptorDacl(SecurityDescriptor, FALSE, NULL, FALSE);
977 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
978 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
979 PrivSet, &PrivSetLen, &Access, &AccessStatus);
980 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
981 ok(AccessStatus && (Access == KEY_READ),
982 "AccessCheck failed to grant access with error %d\n",
985 /* sd with no dacl present - no privilegeset buffer */
986 SetLastError(0xdeadbeef);
987 Access = AccessStatus = 0xdeadbeef;
988 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
989 NULL, &PrivSetLen, &Access, &AccessStatus);
990 err = GetLastError();
991 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
992 "with ERROR_NOACCESS, instead of %d\n", err);
993 ok(Access == 0xdeadbeef && AccessStatus == 0xdeadbeef,
994 "Access and/or AccessStatus were changed!\n");
998 /* Generic access mask - no privilegeset buffer */
999 SetLastError(0xdeadbeef);
1000 Access = ntAccessStatus = 0xdeadbeef;
1001 ntret = pNtAccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1002 NULL, &PrivSetLen, &Access, &ntAccessStatus);
1003 err = GetLastError();
1004 ok(ntret == STATUS_ACCESS_VIOLATION,
1005 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret);
1006 ok(err == 0xdeadbeef,
1007 "NtAccessCheck shouldn't set last error, got %d\n", err);
1008 ok(Access == 0xdeadbeef && ntAccessStatus == 0xdeadbeef,
1009 "Access and/or AccessStatus were changed!\n");
1011 /* Generic access mask - no returnlength */
1012 SetLastError(0xdeadbeef);
1013 Access = ntAccessStatus = 0xdeadbeef;
1014 ntret = pNtAccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1015 PrivSet, NULL, &Access, &ntAccessStatus);
1016 err = GetLastError();
1017 ok(ntret == STATUS_ACCESS_VIOLATION,
1018 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret);
1019 ok(err == 0xdeadbeef,
1020 "NtAccessCheck shouldn't set last error, got %d\n", err);
1021 ok(Access == 0xdeadbeef && ntAccessStatus == 0xdeadbeef,
1022 "Access and/or AccessStatus were changed!\n");
1024 /* Generic access mask - no privilegeset buffer, no returnlength */
1025 SetLastError(0xdeadbeef);
1026 Access = ntAccessStatus = 0xdeadbeef;
1027 ntret = pNtAccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1028 NULL, NULL, &Access, &ntAccessStatus);
1029 err = GetLastError();
1030 ok(ntret == STATUS_ACCESS_VIOLATION,
1031 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret);
1032 ok(err == 0xdeadbeef,
1033 "NtAccessCheck shouldn't set last error, got %d\n", err);
1034 ok(Access == 0xdeadbeef && ntAccessStatus == 0xdeadbeef,
1035 "Access and/or AccessStatus were changed!\n");
1038 win_skip("NtAccessCheck unavailable. Skipping.\n");
1040 /* sd with NULL dacl */
1041 Access = AccessStatus = 0xdeadbeef;
1042 ret = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, NULL, FALSE);
1043 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1044 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1045 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1046 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1047 ok(AccessStatus && (Access == KEY_READ),
1048 "AccessCheck failed to grant access with error %d\n",
1051 /* sd with blank dacl */
1052 ret = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
1053 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1054 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1055 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1056 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1057 err = GetLastError();
1058 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
1059 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
1060 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
1062 res = AddAccessAllowedAce(Acl, ACL_REVISION, KEY_READ, EveryoneSid);
1063 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
1065 res = AddAccessDeniedAce(Acl, ACL_REVISION, KEY_SET_VALUE, AdminSid);
1066 ok(res, "AddAccessDeniedAce failed with error %d\n", GetLastError());
1069 Access = AccessStatus = 0xdeadbeef;
1070 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1071 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1072 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1073 ok(AccessStatus && (Access == KEY_READ),
1074 "AccessCheck failed to grant access with error %d\n",
1077 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
1078 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1079 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1081 "AccessCheck failed to grant any access with error %d\n",
1083 trace("AccessCheck with MAXIMUM_ALLOWED got Access 0x%08x\n", Access);
1085 /* Access denied by SD */
1086 SetLastError(0xdeadbeef);
1087 Access = AccessStatus = 0xdeadbeef;
1088 ret = AccessCheck(SecurityDescriptor, Token, KEY_WRITE, &Mapping,
1089 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1090 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1091 err = GetLastError();
1092 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
1093 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
1094 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
1097 PrivSet->PrivilegeCount = 16;
1098 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
1099 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1100 ok(ret && !AccessStatus && GetLastError() == ERROR_PRIVILEGE_NOT_HELD,
1101 "AccessCheck should have failed with ERROR_PRIVILEGE_NOT_HELD, instead of %d\n",
1104 ret = ImpersonateLoggedOnUser(Token);
1105 ok(ret, "ImpersonateLoggedOnUser failed with error %d\n", GetLastError());
1106 ret = pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, TRUE, TRUE, &Enabled);
1110 PrivSet->PrivilegeCount = 16;
1111 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
1112 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1113 ok(ret && AccessStatus && GetLastError() == 0,
1114 "AccessCheck should have succeeded, error %d\n",
1116 ok(Access == ACCESS_SYSTEM_SECURITY,
1117 "Access should be equal to ACCESS_SYSTEM_SECURITY instead of 0x%08x\n",
1121 trace("Couldn't get SE_SECURITY_PRIVILEGE (0x%08x), skipping ACCESS_SYSTEM_SECURITY test\n",
1123 ret = RevertToSelf();
1124 ok(ret, "RevertToSelf failed with error %d\n", GetLastError());
1126 /* test INHERIT_ONLY_ACE */
1127 ret = InitializeAcl(Acl, 256, ACL_REVISION);
1128 ok(ret, "InitializeAcl failed with error %d\n", GetLastError());
1130 /* NT doesn't have AddAccessAllowedAceEx. Skipping this call/test doesn't influence
1133 if (pAddAccessAllowedAceEx)
1135 ret = pAddAccessAllowedAceEx(Acl, ACL_REVISION, INHERIT_ONLY_ACE, KEY_READ, EveryoneSid);
1136 ok(ret, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
1139 win_skip("AddAccessAllowedAceEx is not available\n");
1141 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1142 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1143 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1144 err = GetLastError();
1145 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
1146 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
1147 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
1151 res = DuplicateToken(ProcessToken, SecurityAnonymous, &Token);
1152 ok(res, "DuplicateToken failed with error %d\n", GetLastError());
1154 SetLastError(0xdeadbeef);
1155 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
1156 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1157 err = GetLastError();
1158 ok(!ret && err == ERROR_BAD_IMPERSONATION_LEVEL, "AccessCheck should have failed "
1159 "with ERROR_BAD_IMPERSONATION_LEVEL, instead of %d\n", err);
1163 SetLastError(0xdeadbeef);
1164 ret = AccessCheck(SecurityDescriptor, ProcessToken, KEY_READ, &Mapping,
1165 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1166 err = GetLastError();
1167 ok(!ret && err == ERROR_NO_IMPERSONATION_TOKEN, "AccessCheck should have failed "
1168 "with ERROR_NO_IMPERSONATION_TOKEN, instead of %d\n", err);
1170 CloseHandle(ProcessToken);
1173 FreeSid(EveryoneSid);
1178 HeapFree(GetProcessHeap(), 0, Acl);
1179 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
1180 HeapFree(GetProcessHeap(), 0, PrivSet);
1183 /* test GetTokenInformation for the various attributes */
1184 static void test_token_attr(void)
1186 HANDLE Token, ImpersonationToken;
1188 TOKEN_PRIVILEGES *Privileges;
1189 TOKEN_GROUPS *Groups;
1191 TOKEN_DEFAULT_DACL *Dacl;
1195 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel;
1198 /* cygwin-like use case */
1199 SetLastError(0xdeadbeef);
1200 ret = OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED, &Token);
1201 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1203 win_skip("OpenProcessToken is not implemented\n");
1206 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1209 DWORD buf[256]; /* GetTokenInformation wants a dword-aligned buffer */
1211 ret = GetTokenInformation(Token, TokenUser,(void*)buf, Size, &Size);
1212 ok(ret, "GetTokenInformation failed with error %d\n", GetLastError());
1213 Size = sizeof(ImpersonationLevel);
1214 ret = GetTokenInformation(Token, TokenImpersonationLevel, &ImpersonationLevel, Size, &Size);
1215 GLE = GetLastError();
1216 ok(!ret && (GLE == ERROR_INVALID_PARAMETER), "GetTokenInformation(TokenImpersonationLevel) on primary token should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GLE);
1220 if(!pConvertSidToStringSidA)
1222 win_skip("ConvertSidToStringSidA is not available\n");
1226 SetLastError(0xdeadbeef);
1227 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &Token);
1228 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1231 SetLastError(0xdeadbeef);
1232 ret = GetTokenInformation(Token, TokenGroups, NULL, 0, &Size);
1233 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1234 "GetTokenInformation(TokenGroups) %s with error %d\n",
1235 ret ? "succeeded" : "failed", GetLastError());
1236 Groups = HeapAlloc(GetProcessHeap(), 0, Size);
1237 SetLastError(0xdeadbeef);
1238 ret = GetTokenInformation(Token, TokenGroups, Groups, Size, &Size);
1239 ok(ret, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
1240 ok(GetLastError() == 0xdeadbeef,
1241 "GetTokenInformation shouldn't have set last error to %d\n",
1243 trace("TokenGroups:\n");
1244 for (i = 0; i < Groups->GroupCount; i++)
1246 DWORD NameLength = 255;
1248 DWORD DomainLength = 255;
1250 SID_NAME_USE SidNameUse;
1251 pConvertSidToStringSidA(Groups->Groups[i].Sid, &SidString);
1254 ret = LookupAccountSid(NULL, Groups->Groups[i].Sid, Name, &NameLength, Domain, &DomainLength, &SidNameUse);
1257 trace("%s, %s\\%s use: %d attr: 0x%08x\n", SidString, Domain, Name, SidNameUse, Groups->Groups[i].Attributes);
1258 LocalFree(SidString);
1260 else trace("attr: 0x%08x LookupAccountSid failed with error %d\n", Groups->Groups[i].Attributes, GetLastError());
1262 HeapFree(GetProcessHeap(), 0, Groups);
1265 ret = GetTokenInformation(Token, TokenUser, NULL, 0, &Size);
1266 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1267 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1268 User = HeapAlloc(GetProcessHeap(), 0, Size);
1269 ret = GetTokenInformation(Token, TokenUser, User, Size, &Size);
1271 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1273 pConvertSidToStringSidA(User->User.Sid, &SidString);
1274 trace("TokenUser: %s attr: 0x%08x\n", SidString, User->User.Attributes);
1275 LocalFree(SidString);
1276 HeapFree(GetProcessHeap(), 0, User);
1279 ret = GetTokenInformation(Token, TokenPrivileges, NULL, 0, &Size);
1280 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1281 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1282 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
1283 ret = GetTokenInformation(Token, TokenPrivileges, Privileges, Size, &Size);
1285 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1286 trace("TokenPrivileges:\n");
1287 for (i = 0; i < Privileges->PrivilegeCount; i++)
1290 DWORD NameLen = sizeof(Name)/sizeof(Name[0]);
1291 LookupPrivilegeName(NULL, &Privileges->Privileges[i].Luid, Name, &NameLen);
1292 trace("\t%s, 0x%x\n", Name, Privileges->Privileges[i].Attributes);
1294 HeapFree(GetProcessHeap(), 0, Privileges);
1296 ret = DuplicateToken(Token, SecurityAnonymous, &ImpersonationToken);
1297 ok(ret, "DuplicateToken failed with error %d\n", GetLastError());
1299 Size = sizeof(ImpersonationLevel);
1300 ret = GetTokenInformation(ImpersonationToken, TokenImpersonationLevel, &ImpersonationLevel, Size, &Size);
1301 ok(ret, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1302 ok(ImpersonationLevel == SecurityAnonymous, "ImpersonationLevel should have been SecurityAnonymous instead of %d\n", ImpersonationLevel);
1304 CloseHandle(ImpersonationToken);
1307 ret = GetTokenInformation(Token, TokenDefaultDacl, NULL, 0, &Size);
1308 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1309 "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1311 Dacl = HeapAlloc(GetProcessHeap(), 0, Size);
1312 ret = GetTokenInformation(Token, TokenDefaultDacl, Dacl, Size, &Size);
1313 ok(ret, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1315 SetLastError(0xdeadbeef);
1316 ret = SetTokenInformation(Token, TokenDefaultDacl, NULL, 0);
1317 GLE = GetLastError();
1318 ok(!ret, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1319 ok(GLE == ERROR_BAD_LENGTH, "expected ERROR_BAD_LENGTH got %u\n", GLE);
1321 SetLastError(0xdeadbeef);
1322 ret = SetTokenInformation(Token, TokenDefaultDacl, NULL, Size);
1323 GLE = GetLastError();
1324 ok(!ret, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1325 ok(GLE == ERROR_NOACCESS, "expected ERROR_NOACCESS got %u\n", GLE);
1327 acl = Dacl->DefaultDacl;
1328 Dacl->DefaultDacl = NULL;
1330 ret = SetTokenInformation(Token, TokenDefaultDacl, Dacl, Size);
1331 ok(ret, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1334 Dacl->DefaultDacl = (ACL *)0xdeadbeef;
1335 ret = GetTokenInformation(Token, TokenDefaultDacl, Dacl, Size, &Size2);
1336 ok(ret, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1337 ok(Dacl->DefaultDacl == NULL, "expected NULL, got %p\n", Dacl->DefaultDacl);
1338 ok(Size2 == sizeof(TOKEN_DEFAULT_DACL), "got %u expected sizeof(TOKEN_DEFAULT_DACL)\n", Size2);
1340 Dacl->DefaultDacl = acl;
1341 ret = SetTokenInformation(Token, TokenDefaultDacl, Dacl, Size);
1342 ok(ret, "SetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1344 ret = GetTokenInformation(Token, TokenDefaultDacl, Dacl, Size, &Size2);
1345 ok(ret, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1347 HeapFree(GetProcessHeap(), 0, Dacl);
1351 typedef union _MAX_SID
1354 char max[SECURITY_MAX_SID_SIZE];
1357 static void test_sid_str(PSID * sid)
1360 BOOL ret = pConvertSidToStringSidA(sid, &str_sid);
1361 ok(ret, "ConvertSidToStringSidA() failed: %d\n", GetLastError());
1364 char account[MAX_PATH], domain[MAX_PATH];
1366 DWORD acc_size = MAX_PATH;
1367 DWORD dom_size = MAX_PATH;
1368 ret = LookupAccountSid(NULL, sid, account, &acc_size, domain, &dom_size, &use);
1369 ok(ret || (!ret && (GetLastError() == ERROR_NONE_MAPPED)),
1370 "LookupAccountSid(%s) failed: %d\n", str_sid, GetLastError());
1372 trace(" %s %s\\%s %d\n", str_sid, domain, account, use);
1373 else if (GetLastError() == ERROR_NONE_MAPPED)
1374 trace(" %s couldn't be mapped\n", str_sid);
1379 struct well_known_sid_value
1381 BOOL without_domain;
1382 const char *sid_string;
1383 } well_known_sid_values[] = {
1384 /* 0 */ {TRUE, "S-1-0-0"}, {TRUE, "S-1-1-0"}, {TRUE, "S-1-2-0"}, {TRUE, "S-1-3-0"},
1385 /* 4 */ {TRUE, "S-1-3-1"}, {TRUE, "S-1-3-2"}, {TRUE, "S-1-3-3"}, {TRUE, "S-1-5"},
1386 /* 8 */ {FALSE, "S-1-5-1"}, {TRUE, "S-1-5-2"}, {TRUE, "S-1-5-3"}, {TRUE, "S-1-5-4"},
1387 /* 12 */ {TRUE, "S-1-5-6"}, {TRUE, "S-1-5-7"}, {TRUE, "S-1-5-8"}, {TRUE, "S-1-5-9"},
1388 /* 16 */ {TRUE, "S-1-5-10"}, {TRUE, "S-1-5-11"}, {TRUE, "S-1-5-12"}, {TRUE, "S-1-5-13"},
1389 /* 20 */ {TRUE, "S-1-5-14"}, {FALSE, NULL}, {TRUE, "S-1-5-18"}, {TRUE, "S-1-5-19"},
1390 /* 24 */ {TRUE, "S-1-5-20"}, {TRUE, "S-1-5-32"},
1391 /* 26 */ {FALSE, "S-1-5-32-544"}, {TRUE, "S-1-5-32-545"}, {TRUE, "S-1-5-32-546"},
1392 /* 29 */ {TRUE, "S-1-5-32-547"}, {TRUE, "S-1-5-32-548"}, {TRUE, "S-1-5-32-549"},
1393 /* 32 */ {TRUE, "S-1-5-32-550"}, {TRUE, "S-1-5-32-551"}, {TRUE, "S-1-5-32-552"},
1394 /* 35 */ {TRUE, "S-1-5-32-554"}, {TRUE, "S-1-5-32-555"}, {TRUE, "S-1-5-32-556"},
1395 /* 38 */ {FALSE, "S-1-5-21-12-23-34-45-56-500"}, {FALSE, "S-1-5-21-12-23-34-45-56-501"},
1396 /* 40 */ {FALSE, "S-1-5-21-12-23-34-45-56-502"}, {FALSE, "S-1-5-21-12-23-34-45-56-512"},
1397 /* 42 */ {FALSE, "S-1-5-21-12-23-34-45-56-513"}, {FALSE, "S-1-5-21-12-23-34-45-56-514"},
1398 /* 44 */ {FALSE, "S-1-5-21-12-23-34-45-56-515"}, {FALSE, "S-1-5-21-12-23-34-45-56-516"},
1399 /* 46 */ {FALSE, "S-1-5-21-12-23-34-45-56-517"}, {FALSE, "S-1-5-21-12-23-34-45-56-518"},
1400 /* 48 */ {FALSE, "S-1-5-21-12-23-34-45-56-519"}, {FALSE, "S-1-5-21-12-23-34-45-56-520"},
1401 /* 50 */ {FALSE, "S-1-5-21-12-23-34-45-56-553"},
1402 /* Added in Windows Server 2003 */
1403 /* 51 */ {TRUE, "S-1-5-64-10"}, {TRUE, "S-1-5-64-21"}, {TRUE, "S-1-5-64-14"},
1404 /* 54 */ {TRUE, "S-1-5-15"}, {TRUE, "S-1-5-1000"}, {FALSE, "S-1-5-32-557"},
1405 /* 57 */ {TRUE, "S-1-5-32-558"}, {TRUE, "S-1-5-32-559"}, {TRUE, "S-1-5-32-560"},
1406 /* 60 */ {TRUE, "S-1-5-32-561"}, {TRUE, "S-1-5-32-562"},
1407 /* Added in Windows Vista: */
1408 /* 62 */ {TRUE, "S-1-5-32-568"},
1409 /* 63 */ {TRUE, "S-1-5-17"}, {FALSE, "S-1-5-32-569"}, {TRUE, "S-1-16-0"},
1410 /* 66 */ {TRUE, "S-1-16-4096"}, {TRUE, "S-1-16-8192"}, {TRUE, "S-1-16-12288"},
1411 /* 69 */ {TRUE, "S-1-16-16384"}, {TRUE, "S-1-5-33"}, {TRUE, "S-1-3-4"},
1412 /* 72 */ {FALSE, "S-1-5-21-12-23-34-45-56-571"}, {FALSE, "S-1-5-21-12-23-34-45-56-572"},
1413 /* 74 */ {TRUE, "S-1-5-22"}, {FALSE, "S-1-5-21-12-23-34-45-56-521"}, {TRUE, "S-1-5-32-573"}
1416 static void test_CreateWellKnownSid(void)
1418 SID_IDENTIFIER_AUTHORITY ident = { SECURITY_NT_AUTHORITY };
1419 PSID domainsid, sid;
1424 if (!pCreateWellKnownSid)
1426 win_skip("CreateWellKnownSid not available\n");
1431 SetLastError(0xdeadbeef);
1432 ret = pCreateWellKnownSid(WinInteractiveSid, NULL, NULL, &size);
1433 error = GetLastError();
1434 ok(!ret, "CreateWellKnownSid succeeded\n");
1435 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
1436 ok(size, "expected size > 0\n");
1438 SetLastError(0xdeadbeef);
1439 ret = pCreateWellKnownSid(WinInteractiveSid, NULL, NULL, &size);
1440 error = GetLastError();
1441 ok(!ret, "CreateWellKnownSid succeeded\n");
1442 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
1444 sid = HeapAlloc(GetProcessHeap(), 0, size);
1445 ret = pCreateWellKnownSid(WinInteractiveSid, NULL, sid, &size);
1446 ok(ret, "CreateWellKnownSid failed %u\n", GetLastError());
1447 HeapFree(GetProcessHeap(), 0, sid);
1449 /* a domain sid usually have three subauthorities but we test that CreateWellKnownSid doesn't check it */
1450 AllocateAndInitializeSid(&ident, 6, SECURITY_NT_NON_UNIQUE, 12, 23, 34, 45, 56, 0, 0, &domainsid);
1452 for (i = 0; i < sizeof(well_known_sid_values)/sizeof(well_known_sid_values[0]); i++)
1454 struct well_known_sid_value *value = &well_known_sid_values[i];
1455 char sid_buffer[SECURITY_MAX_SID_SIZE];
1459 if (value->sid_string == NULL)
1462 if (i > WinAccountRasAndIasServersSid)
1464 /* These SIDs aren't implemented by all Windows versions - detect it and break the loop */
1465 cb = sizeof(sid_buffer);
1466 if (!pCreateWellKnownSid(i, domainsid, sid_buffer, &cb))
1468 skip("Well known SIDs starting from %d are not implemented\n", i);
1473 cb = sizeof(sid_buffer);
1474 ok(pCreateWellKnownSid(i, value->without_domain ? NULL : domainsid, sid_buffer, &cb), "Couldn't create well known sid %d\n", i);
1475 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer)), cb, DWORD, "%d");
1476 ok(IsValidSid(sid_buffer), "The sid is not valid\n");
1477 ok(pConvertSidToStringSidA(sid_buffer, &str), "Couldn't convert SID to string\n");
1478 ok(strcmp(str, value->sid_string) == 0, "SID mismatch - expected %s, got %s\n",
1479 value->sid_string, str);
1482 if (value->without_domain)
1484 char buf2[SECURITY_MAX_SID_SIZE];
1486 ok(pCreateWellKnownSid(i, domainsid, buf2, &cb), "Couldn't create well known sid %d with optional domain\n", i);
1487 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer)), cb, DWORD, "%d");
1488 ok(memcmp(buf2, sid_buffer, cb) == 0, "SID create with domain is different than without (%d)\n", i);
1493 static void test_LookupAccountSid(void)
1495 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
1496 CHAR accountA[MAX_PATH], domainA[MAX_PATH], usernameA[MAX_PATH];
1497 DWORD acc_sizeA, dom_sizeA, user_sizeA;
1498 DWORD real_acc_sizeA, real_dom_sizeA;
1499 WCHAR accountW[MAX_PATH], domainW[MAX_PATH];
1500 DWORD acc_sizeW, dom_sizeW;
1501 DWORD real_acc_sizeW, real_dom_sizeW;
1502 PSID pUsersSid = NULL;
1505 DWORD size,cbti = 0;
1510 PTOKEN_USER ptiUser = NULL;
1512 /* native windows crashes if account size, domain size, or name use is NULL */
1514 ret = AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
1515 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &pUsersSid);
1516 ok(ret || (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED),
1517 "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1519 /* not running on NT so give up */
1520 if (!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1523 real_acc_sizeA = MAX_PATH;
1524 real_dom_sizeA = MAX_PATH;
1525 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &real_acc_sizeA, domainA, &real_dom_sizeA, &use);
1526 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1528 /* try NULL account */
1529 acc_sizeA = MAX_PATH;
1530 dom_sizeA = MAX_PATH;
1531 ret = LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
1532 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1534 /* try NULL domain */
1535 acc_sizeA = MAX_PATH;
1536 dom_sizeA = MAX_PATH;
1537 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
1538 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1540 /* try a small account buffer */
1542 dom_sizeA = MAX_PATH;
1544 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1545 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1546 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1547 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1549 /* try a 0 sized account buffer */
1551 dom_sizeA = MAX_PATH;
1553 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1554 /* this can fail or succeed depending on OS version but the size will always be returned */
1555 ok(acc_sizeA == real_acc_sizeA + 1,
1556 "LookupAccountSidA() Expected acc_size = %u, got %u\n",
1557 real_acc_sizeA + 1, acc_sizeA);
1559 /* try a 0 sized account buffer */
1561 dom_sizeA = MAX_PATH;
1562 ret = LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
1563 /* this can fail or succeed depending on OS version but the size will always be returned */
1564 ok(acc_sizeA == real_acc_sizeA + 1,
1565 "LookupAccountSid() Expected acc_size = %u, got %u\n",
1566 real_acc_sizeA + 1, acc_sizeA);
1568 /* try a small domain buffer */
1570 acc_sizeA = MAX_PATH;
1572 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1573 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1574 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1575 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1577 /* try a 0 sized domain buffer */
1579 acc_sizeA = MAX_PATH;
1581 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1582 /* this can fail or succeed depending on OS version but the size will always be returned */
1583 ok(dom_sizeA == real_dom_sizeA + 1,
1584 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1585 real_dom_sizeA + 1, dom_sizeA);
1587 /* try a 0 sized domain buffer */
1589 acc_sizeA = MAX_PATH;
1590 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
1591 /* this can fail or succeed depending on OS version but the size will always be returned */
1592 ok(dom_sizeA == real_dom_sizeA + 1,
1593 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1594 real_dom_sizeA + 1, dom_sizeA);
1596 real_acc_sizeW = MAX_PATH;
1597 real_dom_sizeW = MAX_PATH;
1598 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &real_acc_sizeW, domainW, &real_dom_sizeW, &use);
1599 ok(ret, "LookupAccountSidW() Expected TRUE, got FALSE\n");
1601 /* try an invalid system name */
1602 real_acc_sizeA = MAX_PATH;
1603 real_dom_sizeA = MAX_PATH;
1604 ret = LookupAccountSidA("deepthought", pUsersSid, accountA, &real_acc_sizeA, domainA, &real_dom_sizeA, &use);
1605 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1606 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE || GetLastError() == RPC_S_INVALID_NET_ADDR /* Vista */,
1607 "LookupAccountSidA() Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %u\n", GetLastError());
1609 /* native windows crashes if domainW or accountW is NULL */
1611 /* try a small account buffer */
1613 dom_sizeW = MAX_PATH;
1615 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1616 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1617 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1618 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1620 /* try a 0 sized account buffer */
1622 dom_sizeW = MAX_PATH;
1624 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1625 /* this can fail or succeed depending on OS version but the size will always be returned */
1626 ok(acc_sizeW == real_acc_sizeW + 1,
1627 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1628 real_acc_sizeW + 1, acc_sizeW);
1630 /* try a 0 sized account buffer */
1632 dom_sizeW = MAX_PATH;
1633 ret = LookupAccountSidW(NULL, pUsersSid, NULL, &acc_sizeW, domainW, &dom_sizeW, &use);
1634 /* this can fail or succeed depending on OS version but the size will always be returned */
1635 ok(acc_sizeW == real_acc_sizeW + 1,
1636 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1637 real_acc_sizeW + 1, acc_sizeW);
1639 /* try a small domain buffer */
1641 acc_sizeW = MAX_PATH;
1643 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1644 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1645 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1646 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1648 /* try a 0 sized domain buffer */
1650 acc_sizeW = MAX_PATH;
1652 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1653 /* this can fail or succeed depending on OS version but the size will always be returned */
1654 ok(dom_sizeW == real_dom_sizeW + 1,
1655 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1656 real_dom_sizeW + 1, dom_sizeW);
1658 /* try a 0 sized domain buffer */
1660 acc_sizeW = MAX_PATH;
1661 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, NULL, &dom_sizeW, &use);
1662 /* this can fail or succeed depending on OS version but the size will always be returned */
1663 ok(dom_sizeW == real_dom_sizeW + 1,
1664 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1665 real_dom_sizeW + 1, dom_sizeW);
1669 /* Test LookupAccountSid with Sid retrieved from token information.
1670 This assumes this process is running under the account of the current user.*/
1671 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &hToken);
1672 ret = GetTokenInformation(hToken, TokenUser, NULL, 0, &cbti);
1673 ptiUser = (PTOKEN_USER) HeapAlloc(GetProcessHeap(), 0, cbti);
1674 if (GetTokenInformation(hToken, TokenUser, ptiUser, cbti, &cbti))
1676 acc_sizeA = dom_sizeA = MAX_PATH;
1677 ret = LookupAccountSidA(NULL, ptiUser->User.Sid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1678 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1679 user_sizeA = MAX_PATH;
1680 ret = GetUserNameA(usernameA , &user_sizeA);
1681 ok(ret, "GetUserNameA() Expected TRUE, got FALSE\n");
1684 ok(lstrcmpA(usernameA, accountA) == 0, "LookupAccountSidA() Expected account name: %s got: %s\n", usernameA, accountA );
1687 HeapFree(GetProcessHeap(), 0, ptiUser);
1689 if (pCreateWellKnownSid && pConvertSidToStringSidA)
1691 trace("Well Known SIDs:\n");
1692 for (i = 0; i <= 60; i++)
1694 size = SECURITY_MAX_SID_SIZE;
1695 if (pCreateWellKnownSid(i, NULL, &max_sid.sid, &size))
1697 if (pConvertSidToStringSidA(&max_sid.sid, &str_sidA))
1699 acc_sizeA = MAX_PATH;
1700 dom_sizeA = MAX_PATH;
1701 if (LookupAccountSidA(NULL, &max_sid.sid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use))
1702 trace(" %d: %s %s\\%s %d\n", i, str_sidA, domainA, accountA, use);
1703 LocalFree(str_sidA);
1708 if (GetLastError() != ERROR_INVALID_PARAMETER)
1709 trace(" CreateWellKnownSid(%d) failed: %d\n", i, GetLastError());
1711 trace(" %d: not supported\n", i);
1715 pLsaQueryInformationPolicy = (fnLsaQueryInformationPolicy)GetProcAddress( hmod, "LsaQueryInformationPolicy");
1716 pLsaOpenPolicy = (fnLsaOpenPolicy)GetProcAddress( hmod, "LsaOpenPolicy");
1717 pLsaFreeMemory = (fnLsaFreeMemory)GetProcAddress( hmod, "LsaFreeMemory");
1718 pLsaClose = (fnLsaClose)GetProcAddress( hmod, "LsaClose");
1720 if (pLsaQueryInformationPolicy && pLsaOpenPolicy && pLsaFreeMemory && pLsaClose)
1724 LSA_OBJECT_ATTRIBUTES object_attributes;
1726 ZeroMemory(&object_attributes, sizeof(object_attributes));
1727 object_attributes.Length = sizeof(object_attributes);
1729 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_ALL_ACCESS, &handle);
1730 ok(status == STATUS_SUCCESS || status == STATUS_ACCESS_DENIED,
1731 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status);
1733 /* try a more restricted access mask if necessary */
1734 if (status == STATUS_ACCESS_DENIED) {
1735 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
1736 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_VIEW_LOCAL_INFORMATION, &handle);
1737 ok(status == STATUS_SUCCESS, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status);
1740 if (status == STATUS_SUCCESS)
1742 PPOLICY_ACCOUNT_DOMAIN_INFO info;
1743 status = pLsaQueryInformationPolicy(handle, PolicyAccountDomainInformation, (PVOID*)&info);
1744 ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy() failed, returned 0x%08x\n", status);
1745 if (status == STATUS_SUCCESS)
1747 ok(info->DomainSid!=0, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) missing SID\n");
1748 if (info->DomainSid)
1750 int count = *GetSidSubAuthorityCount(info->DomainSid);
1751 CopySid(GetSidLengthRequired(count), &max_sid, info->DomainSid);
1752 test_sid_str((PSID)&max_sid.sid);
1753 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_ADMIN;
1754 max_sid.sid.SubAuthorityCount = count + 1;
1755 test_sid_str((PSID)&max_sid.sid);
1756 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_GUEST;
1757 test_sid_str((PSID)&max_sid.sid);
1758 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ADMINS;
1759 test_sid_str((PSID)&max_sid.sid);
1760 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_USERS;
1761 test_sid_str((PSID)&max_sid.sid);
1762 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_GUESTS;
1763 test_sid_str((PSID)&max_sid.sid);
1764 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_COMPUTERS;
1765 test_sid_str((PSID)&max_sid.sid);
1766 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CONTROLLERS;
1767 test_sid_str((PSID)&max_sid.sid);
1768 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CERT_ADMINS;
1769 test_sid_str((PSID)&max_sid.sid);
1770 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_SCHEMA_ADMINS;
1771 test_sid_str((PSID)&max_sid.sid);
1772 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ENTERPRISE_ADMINS;
1773 test_sid_str((PSID)&max_sid.sid);
1774 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_POLICY_ADMINS;
1775 test_sid_str((PSID)&max_sid.sid);
1776 max_sid.sid.SubAuthority[count] = DOMAIN_ALIAS_RID_RAS_SERVERS;
1777 test_sid_str((PSID)&max_sid.sid);
1778 max_sid.sid.SubAuthority[count] = 1000; /* first user account */
1779 test_sid_str((PSID)&max_sid.sid);
1782 pLsaFreeMemory((LPVOID)info);
1785 status = pLsaClose(handle);
1786 ok(status == STATUS_SUCCESS, "LsaClose() failed, returned 0x%08x\n", status);
1792 static BOOL get_sid_info(PSID psid, LPSTR *user, LPSTR *dom)
1794 static CHAR account[UNLEN + 1];
1795 static CHAR domain[UNLEN + 1];
1796 DWORD size, dom_size;
1802 size = dom_size = UNLEN + 1;
1805 SetLastError(0xdeadbeef);
1806 return LookupAccountSidA(NULL, psid, account, &size, domain, &dom_size, &use);
1809 static void check_wellknown_name(const char* name, WELL_KNOWN_SID_TYPE result)
1811 SID_IDENTIFIER_AUTHORITY ident = { SECURITY_NT_AUTHORITY };
1813 char wk_sid[SECURITY_MAX_SID_SIZE];
1816 DWORD sid_size, domain_size;
1817 SID_NAME_USE sid_use;
1818 LPSTR domain, account, sid_domain, wk_domain, wk_account;
1824 ret = LookupAccountNameA(NULL, name, NULL, &sid_size, NULL, &domain_size, &sid_use);
1825 psid = HeapAlloc(GetProcessHeap(),0,sid_size);
1826 domain = HeapAlloc(GetProcessHeap(),0,domain_size);
1827 ret = LookupAccountNameA(NULL, name, psid, &sid_size, domain, &domain_size, &sid_use);
1831 ok(!ret, " %s Should have failed to lookup account name\n",name);
1835 AllocateAndInitializeSid(&ident, 6, SECURITY_NT_NON_UNIQUE, 12, 23, 34, 45, 56, 0, 0, &domainsid);
1836 cb = sizeof(wk_sid);
1837 if (!pCreateWellKnownSid(result, domainsid, wk_sid, &cb))
1839 win_skip("SID %i is not available on the system\n",result);
1843 ret2 = get_sid_info(wk_sid, &wk_account, &wk_domain);
1844 if (!ret2 && GetLastError() == ERROR_NONE_MAPPED)
1846 win_skip("CreateWellKnownSid() succeeded but the account '%s' is not present (W2K)\n", name);
1850 get_sid_info(psid, &account, &sid_domain);
1852 ok(ret, "Failed to lookup account name %s\n",name);
1853 ok(sid_size != 0, "sid_size was zero\n");
1855 ok(EqualSid(psid,wk_sid),"(%s) Sids fail to match well known sid!\n",name);
1857 ok(!lstrcmp(account, wk_account), "Expected %s , got %s\n", account, wk_account);
1858 ok(!lstrcmp(domain, wk_domain), "Expected %s, got %s\n", wk_domain, domain);
1859 ok(sid_use == SidTypeWellKnownGroup , "Expected Use (5), got %d\n", sid_use);
1862 HeapFree(GetProcessHeap(),0,psid);
1863 HeapFree(GetProcessHeap(),0,domain);
1866 static void test_LookupAccountName(void)
1868 DWORD sid_size, domain_size, user_size;
1869 DWORD sid_save, domain_save;
1870 CHAR user_name[UNLEN + 1];
1871 CHAR computer_name[UNLEN + 1];
1872 SID_NAME_USE sid_use;
1873 LPSTR domain, account, sid_dom;
1877 /* native crashes if (assuming all other parameters correct):
1879 * - Sid is NULL and cbSid is > 0
1880 * - cbSid or cchReferencedDomainName are NULL
1881 * - ReferencedDomainName is NULL and cchReferencedDomainName is the correct size
1884 user_size = UNLEN + 1;
1885 SetLastError(0xdeadbeef);
1886 ret = GetUserNameA(user_name, &user_size);
1887 if (!ret && (GetLastError() == ERROR_NOT_LOGGED_ON))
1889 /* Probably on win9x where the user used 'Cancel' instead of properly logging in */
1890 skip("Cannot get the user name (win9x and not logged in properly)\n");
1893 ok(ret, "Failed to get user name : %d\n", GetLastError());
1898 sid_use = 0xcafebabe;
1899 SetLastError(0xdeadbeef);
1900 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, NULL, &domain_size, &sid_use);
1901 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1903 win_skip("LookupAccountNameA is not implemented\n");
1906 ok(!ret, "Expected 0, got %d\n", ret);
1907 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1908 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1909 ok(sid_size != 0, "Expected non-zero sid size\n");
1910 ok(domain_size != 0, "Expected non-zero domain size\n");
1911 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
1913 sid_save = sid_size;
1914 domain_save = domain_size;
1916 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
1917 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
1919 /* try valid account name */
1920 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, domain, &domain_size, &sid_use);
1921 get_sid_info(psid, &account, &sid_dom);
1922 ok(ret, "Failed to lookup account name\n");
1923 ok(sid_size == GetLengthSid(psid), "Expected %d, got %d\n", GetLengthSid(psid), sid_size);
1926 ok(!lstrcmp(account, user_name), "Expected %s, got %s\n", user_name, account);
1927 ok(!lstrcmp(domain, sid_dom), "Expected %s, got %s\n", sid_dom, domain);
1929 ok(domain_size == domain_save - 1, "Expected %d, got %d\n", domain_save - 1, domain_size);
1930 ok(lstrlen(domain) == domain_size, "Expected %d, got %d\n", lstrlen(domain), domain_size);
1931 ok(sid_use == SidTypeUser, "Expected SidTypeUser (%d), got %d\n", SidTypeUser, sid_use);
1932 domain_size = domain_save;
1933 sid_size = sid_save;
1935 if (PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale())) != LANG_ENGLISH)
1937 skip("Non-english locale (test with hardcoded 'Everyone')\n");
1941 ret = LookupAccountNameA(NULL, "Everyone", psid, &sid_size, domain, &domain_size, &sid_use);
1942 get_sid_info(psid, &account, &sid_dom);
1943 ok(ret, "Failed to lookup account name\n");
1944 ok(sid_size != 0, "sid_size was zero\n");
1945 ok(!lstrcmp(account, "Everyone"), "Expected Everyone, got %s\n", account);
1946 ok(!lstrcmp(domain, sid_dom), "Expected %s, got %s\n", sid_dom, domain);
1947 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
1948 ok(lstrlen(domain) == domain_size, "Expected %d, got %d\n", lstrlen(domain), domain_size);
1949 ok(sid_use == SidTypeWellKnownGroup, "Expected SidTypeWellKnownGroup (%d), got %d\n", SidTypeWellKnownGroup, sid_use);
1950 domain_size = domain_save;
1953 /* NULL Sid with zero sid size */
1954 SetLastError(0xdeadbeef);
1956 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
1957 ok(!ret, "Expected 0, got %d\n", ret);
1958 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1959 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1960 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1961 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1963 /* try cchReferencedDomainName - 1 */
1964 SetLastError(0xdeadbeef);
1966 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
1967 ok(!ret, "Expected 0, got %d\n", ret);
1968 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1969 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1970 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1971 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1973 /* NULL ReferencedDomainName with zero domain name size */
1974 SetLastError(0xdeadbeef);
1976 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, NULL, &domain_size, &sid_use);
1977 ok(!ret, "Expected 0, got %d\n", ret);
1978 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1979 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1980 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1981 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1983 HeapFree(GetProcessHeap(), 0, psid);
1984 HeapFree(GetProcessHeap(), 0, domain);
1986 /* get sizes for NULL account name */
1989 sid_use = 0xcafebabe;
1990 SetLastError(0xdeadbeef);
1991 ret = LookupAccountNameA(NULL, NULL, NULL, &sid_size, NULL, &domain_size, &sid_use);
1992 if (!ret && GetLastError() == ERROR_NONE_MAPPED)
1993 win_skip("NULL account name doesn't work on NT4\n");
1996 ok(!ret, "Expected 0, got %d\n", ret);
1997 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1998 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1999 ok(sid_size != 0, "Expected non-zero sid size\n");
2000 ok(domain_size != 0, "Expected non-zero domain size\n");
2001 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
2003 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
2004 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
2006 /* try NULL account name */
2007 ret = LookupAccountNameA(NULL, NULL, psid, &sid_size, domain, &domain_size, &sid_use);
2008 get_sid_info(psid, &account, &sid_dom);
2009 ok(ret, "Failed to lookup account name\n");
2010 /* Using a fixed string will not work on different locales */
2011 ok(!lstrcmp(account, domain),
2012 "Got %s for account and %s for domain, these should be the same\n",
2014 ok(sid_use == SidTypeDomain, "Expected SidTypeDomain (%d), got %d\n", SidTypeDomain, sid_use);
2016 HeapFree(GetProcessHeap(), 0, psid);
2017 HeapFree(GetProcessHeap(), 0, domain);
2020 /* try an invalid account name */
2021 SetLastError(0xdeadbeef);
2024 ret = LookupAccountNameA(NULL, "oogabooga", NULL, &sid_size, NULL, &domain_size, &sid_use);
2025 ok(!ret, "Expected 0, got %d\n", ret);
2026 ok(GetLastError() == ERROR_NONE_MAPPED ||
2027 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE),
2028 "Expected ERROR_NONE_MAPPED, got %d\n", GetLastError());
2029 ok(sid_size == 0, "Expected 0, got %d\n", sid_size);
2030 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
2032 /* try an invalid system name */
2033 SetLastError(0xdeadbeef);
2036 ret = LookupAccountNameA("deepthought", NULL, NULL, &sid_size, NULL, &domain_size, &sid_use);
2037 ok(!ret, "Expected 0, got %d\n", ret);
2038 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE || GetLastError() == RPC_S_INVALID_NET_ADDR /* Vista */,
2039 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
2040 ok(sid_size == 0, "Expected 0, got %d\n", sid_size);
2041 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
2043 /* try with the computer name as the account name */
2044 domain_size = sizeof(computer_name);
2045 GetComputerNameA(computer_name, &domain_size);
2048 ret = LookupAccountNameA(NULL, computer_name, NULL, &sid_size, NULL, &domain_size, &sid_use);
2049 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER ||
2050 GetLastError() == ERROR_NONE_MAPPED /* in a domain */ ||
2051 broken(GetLastError() == ERROR_TRUSTED_DOMAIN_FAILURE) ||
2052 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE)),
2053 "LookupAccountNameA failed: %d\n", GetLastError());
2054 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
2056 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
2057 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
2058 ret = LookupAccountNameA(NULL, computer_name, psid, &sid_size, domain, &domain_size, &sid_use);
2059 ok(ret, "LookupAccountNameA failed: %d\n", GetLastError());
2060 ok(sid_use == SidTypeDomain ||
2061 (sid_use == SidTypeUser && ! strcmp(computer_name, user_name)), "expected SidTypeDomain for %s, got %d\n", computer_name, sid_use);
2062 HeapFree(GetProcessHeap(), 0, domain);
2063 HeapFree(GetProcessHeap(), 0, psid);
2066 /* Well Known names */
2067 if (!pCreateWellKnownSid)
2069 win_skip("CreateWellKnownSid not available\n");
2073 if (PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale())) != LANG_ENGLISH)
2075 skip("Non-english locale (skipping well known name creation tests)\n");
2079 check_wellknown_name("LocalService", WinLocalServiceSid);
2080 check_wellknown_name("Local Service", WinLocalServiceSid);
2082 check_wellknown_name("Local Service", 0);
2083 check_wellknown_name("NetworkService", WinNetworkServiceSid);
2084 check_wellknown_name("Network Service", WinNetworkServiceSid);
2086 /* example of some names where the spaces are not optional */
2087 check_wellknown_name("Terminal Server User", WinTerminalServerSid);
2088 check_wellknown_name("TerminalServer User", 0);
2089 check_wellknown_name("TerminalServerUser", 0);
2090 check_wellknown_name("Terminal ServerUser", 0);
2092 check_wellknown_name("enterprise domain controllers",WinEnterpriseControllersSid);
2093 check_wellknown_name("enterprisedomain controllers", 0);
2094 check_wellknown_name("enterprise domaincontrollers", 0);
2095 check_wellknown_name("enterprisedomaincontrollers", 0);
2097 /* case insensitivity */
2098 check_wellknown_name("lOCAlServICE", WinLocalServiceSid);
2100 /* fully qualified account names */
2101 check_wellknown_name("NT AUTHORITY\\LocalService", WinLocalServiceSid);
2102 check_wellknown_name("nt authority\\Network Service", WinNetworkServiceSid);
2103 check_wellknown_name("nt authority test\\Network Service", 0);
2104 check_wellknown_name("Dummy\\Network Service", 0);
2105 check_wellknown_name("ntauthority\\Network Service", 0);
2108 static void test_security_descriptor(void)
2110 SECURITY_DESCRIPTOR sd;
2113 BOOL isDefault, isPresent, ret;
2117 SetLastError(0xdeadbeef);
2118 ret = InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
2119 if (ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2121 win_skip("InitializeSecurityDescriptor is not implemented\n");
2125 ok(GetSecurityDescriptorOwner(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
2126 expect_eq(psid, NULL, PSID, "%p");
2127 expect_eq(isDefault, FALSE, BOOL, "%d");
2128 sd.Control |= SE_DACL_PRESENT | SE_SACL_PRESENT;
2130 SetLastError(0xdeadbeef);
2132 expect_eq(MakeSelfRelativeSD(&sd, buf, &size), FALSE, BOOL, "%d");
2133 expect_eq(GetLastError(), ERROR_INSUFFICIENT_BUFFER, DWORD, "%u");
2134 ok(size > 5, "Size not increased\n");
2137 expect_eq(MakeSelfRelativeSD(&sd, buf, &size), TRUE, BOOL, "%d");
2138 ok(GetSecurityDescriptorOwner(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
2139 expect_eq(psid, NULL, PSID, "%p");
2140 expect_eq(isDefault, FALSE, BOOL, "%d");
2141 ok(GetSecurityDescriptorGroup(&sd, &psid, &isDefault), "GetSecurityDescriptorGroup failed\n");
2142 expect_eq(psid, NULL, PSID, "%p");
2143 expect_eq(isDefault, FALSE, BOOL, "%d");
2144 ok(GetSecurityDescriptorDacl(&sd, &isPresent, &pacl, &isDefault), "GetSecurityDescriptorDacl failed\n");
2145 expect_eq(isPresent, TRUE, BOOL, "%d");
2146 expect_eq(psid, NULL, PSID, "%p");
2147 expect_eq(isDefault, FALSE, BOOL, "%d");
2148 ok(GetSecurityDescriptorSacl(&sd, &isPresent, &pacl, &isDefault), "GetSecurityDescriptorSacl failed\n");
2149 expect_eq(isPresent, TRUE, BOOL, "%d");
2150 expect_eq(psid, NULL, PSID, "%p");
2151 expect_eq(isDefault, FALSE, BOOL, "%d");
2155 #define TEST_GRANTED_ACCESS(a,b) test_granted_access(a,b,0,__LINE__)
2156 #define TEST_GRANTED_ACCESS2(a,b,c) test_granted_access(a,b,c,__LINE__)
2157 static void test_granted_access(HANDLE handle, ACCESS_MASK access,
2158 ACCESS_MASK alt, int line)
2160 OBJECT_BASIC_INFORMATION obj_info;
2163 if (!pNtQueryObject)
2165 skip_(__FILE__, line)("Not NT platform - skipping tests\n");
2169 status = pNtQueryObject( handle, ObjectBasicInformation, &obj_info,
2170 sizeof(obj_info), NULL );
2171 ok_(__FILE__, line)(!status, "NtQueryObject with err: %08x\n", status);
2173 ok_(__FILE__, line)(obj_info.GrantedAccess == access ||
2174 obj_info.GrantedAccess == alt, "Granted access should be 0x%08x "
2175 "or 0x%08x, instead of 0x%08x\n", access, alt, obj_info.GrantedAccess);
2177 ok_(__FILE__, line)(obj_info.GrantedAccess == access, "Granted access should "
2178 "be 0x%08x, instead of 0x%08x\n", access, obj_info.GrantedAccess);
2181 #define CHECK_SET_SECURITY(o,i,e) \
2185 SetLastError( 0xdeadbeef ); \
2186 res = SetKernelObjectSecurity( o, i, SecurityDescriptor ); \
2187 err = GetLastError(); \
2188 if (e == ERROR_SUCCESS) \
2189 ok(res, "SetKernelObjectSecurity failed with %d\n", err); \
2191 ok(!res && err == e, "SetKernelObjectSecurity should have failed " \
2192 "with %s, instead of %d\n", #e, err); \
2195 static void test_process_security(void)
2199 PTOKEN_PRIMARY_GROUP group;
2200 PSID AdminSid = NULL, UsersSid = NULL;
2202 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL;
2203 char buffer[MAX_PATH];
2204 PROCESS_INFORMATION info;
2205 STARTUPINFOA startup;
2206 SECURITY_ATTRIBUTES psa;
2207 HANDLE token, event;
2210 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
2211 res = InitializeAcl(Acl, 256, ACL_REVISION);
2212 if (!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2214 win_skip("ACLs not implemented - skipping tests\n");
2215 HeapFree(GetProcessHeap(), 0, Acl);
2218 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
2220 /* get owner from the token we might be running as a user not admin */
2221 res = OpenProcessToken( GetCurrentProcess(), MAXIMUM_ALLOWED, &token );
2222 ok(res, "OpenProcessToken failed with error %d\n", GetLastError());
2225 HeapFree(GetProcessHeap(), 0, Acl);
2229 res = GetTokenInformation( token, TokenOwner, NULL, 0, &size );
2230 ok(!res, "Expected failure, got %d\n", res);
2231 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2232 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2234 owner = HeapAlloc(GetProcessHeap(), 0, size);
2235 res = GetTokenInformation( token, TokenOwner, owner, size, &size );
2236 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
2237 AdminSid = ((TOKEN_OWNER*)owner)->Owner;
2239 res = GetTokenInformation( token, TokenPrimaryGroup, NULL, 0, &size );
2240 ok(!res, "Expected failure, got %d\n", res);
2241 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2242 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2244 group = HeapAlloc(GetProcessHeap(), 0, size);
2245 res = GetTokenInformation( token, TokenPrimaryGroup, group, size, &size );
2246 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
2247 UsersSid = ((TOKEN_PRIMARY_GROUP*)group)->PrimaryGroup;
2249 CloseHandle( token );
2252 HeapFree(GetProcessHeap(), 0, group);
2253 HeapFree(GetProcessHeap(), 0, owner);
2254 HeapFree(GetProcessHeap(), 0, Acl);
2258 res = AddAccessDeniedAce(Acl, ACL_REVISION, PROCESS_VM_READ, AdminSid);
2259 ok(res, "AddAccessDeniedAce failed with error %d\n", GetLastError());
2260 res = AddAccessAllowedAce(Acl, ACL_REVISION, PROCESS_ALL_ACCESS, AdminSid);
2261 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2263 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
2264 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
2265 ok(res, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
2267 event = CreateEvent( NULL, TRUE, TRUE, "test_event" );
2268 ok(event != NULL, "CreateEvent %d\n", GetLastError());
2270 SecurityDescriptor->Revision = 0;
2271 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_UNKNOWN_REVISION );
2272 SecurityDescriptor->Revision = SECURITY_DESCRIPTOR_REVISION;
2274 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_INVALID_SECURITY_DESCR );
2275 CHECK_SET_SECURITY( event, GROUP_SECURITY_INFORMATION, ERROR_INVALID_SECURITY_DESCR );
2276 CHECK_SET_SECURITY( event, SACL_SECURITY_INFORMATION, ERROR_ACCESS_DENIED );
2277 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
2278 /* NULL DACL is valid and means default DACL from token */
2279 SecurityDescriptor->Control |= SE_DACL_PRESENT;
2280 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
2282 /* Set owner and group and dacl */
2283 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
2284 ok(res, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
2285 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_SUCCESS );
2286 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, FALSE);
2287 ok(res, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2288 CHECK_SET_SECURITY( event, GROUP_SECURITY_INFORMATION, ERROR_SUCCESS );
2289 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
2290 ok(res, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
2291 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
2293 sprintf(buffer, "%s tests/security.c test", myARGV[0]);
2294 memset(&startup, 0, sizeof(startup));
2295 startup.cb = sizeof(startup);
2296 startup.dwFlags = STARTF_USESHOWWINDOW;
2297 startup.wShowWindow = SW_SHOWNORMAL;
2299 psa.nLength = sizeof(psa);
2300 psa.lpSecurityDescriptor = SecurityDescriptor;
2301 psa.bInheritHandle = TRUE;
2303 /* Doesn't matter what ACL say we should get full access for ourselves */
2304 ok(CreateProcessA( NULL, buffer, &psa, NULL, FALSE, 0, NULL, NULL, &startup, &info ),
2305 "CreateProcess with err:%d\n", GetLastError());
2306 TEST_GRANTED_ACCESS2( info.hProcess, PROCESS_ALL_ACCESS,
2307 STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL );
2308 winetest_wait_child_process( info.hProcess );
2310 CloseHandle( info.hProcess );
2311 CloseHandle( info.hThread );
2312 CloseHandle( event );
2313 HeapFree(GetProcessHeap(), 0, group);
2314 HeapFree(GetProcessHeap(), 0, owner);
2315 HeapFree(GetProcessHeap(), 0, Acl);
2316 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
2319 static void test_process_security_child(void)
2321 HANDLE handle, handle1;
2325 handle = OpenProcess( PROCESS_TERMINATE, FALSE, GetCurrentProcessId() );
2326 ok(handle != NULL, "OpenProcess(PROCESS_TERMINATE) with err:%d\n", GetLastError());
2327 TEST_GRANTED_ACCESS( handle, PROCESS_TERMINATE );
2329 ok(DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
2330 &handle1, 0, TRUE, DUPLICATE_SAME_ACCESS ),
2331 "duplicating handle err:%d\n", GetLastError());
2332 TEST_GRANTED_ACCESS( handle1, PROCESS_TERMINATE );
2334 CloseHandle( handle1 );
2336 SetLastError( 0xdeadbeef );
2337 ret = DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
2338 &handle1, PROCESS_ALL_ACCESS, TRUE, 0 );
2339 err = GetLastError();
2341 ok(!ret && err == ERROR_ACCESS_DENIED, "duplicating handle should have failed "
2342 "with STATUS_ACCESS_DENIED, instead of err:%d\n", err);
2344 CloseHandle( handle );
2346 /* These two should fail - they are denied by ACL */
2347 handle = OpenProcess( PROCESS_VM_READ, FALSE, GetCurrentProcessId() );
2349 ok(handle == NULL, "OpenProcess(PROCESS_VM_READ) should have failed\n");
2350 handle = OpenProcess( PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId() );
2352 ok(handle == NULL, "OpenProcess(PROCESS_ALL_ACCESS) should have failed\n");
2354 /* Documented privilege elevation */
2355 ok(DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2356 &handle, 0, TRUE, DUPLICATE_SAME_ACCESS ),
2357 "duplicating handle err:%d\n", GetLastError());
2358 TEST_GRANTED_ACCESS2( handle, PROCESS_ALL_ACCESS,
2359 STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL );
2361 CloseHandle( handle );
2363 /* Same only explicitly asking for all access rights */
2364 ok(DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2365 &handle, PROCESS_ALL_ACCESS, TRUE, 0 ),
2366 "duplicating handle err:%d\n", GetLastError());
2367 TEST_GRANTED_ACCESS2( handle, PROCESS_ALL_ACCESS,
2368 PROCESS_ALL_ACCESS | PROCESS_QUERY_LIMITED_INFORMATION );
2369 ok(DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
2370 &handle1, PROCESS_VM_READ, TRUE, 0 ),
2371 "duplicating handle err:%d\n", GetLastError());
2372 TEST_GRANTED_ACCESS( handle1, PROCESS_VM_READ );
2373 CloseHandle( handle1 );
2374 CloseHandle( handle );
2377 static void test_impersonation_level(void)
2379 HANDLE Token, ProcessToken;
2382 TOKEN_PRIVILEGES *Privileges;
2384 PRIVILEGE_SET *PrivilegeSet;
2390 pDuplicateTokenEx = (fnDuplicateTokenEx) GetProcAddress(hmod, "DuplicateTokenEx");
2391 if( !pDuplicateTokenEx ) {
2392 win_skip("DuplicateTokenEx is not available\n");
2395 SetLastError(0xdeadbeef);
2396 ret = ImpersonateSelf(SecurityAnonymous);
2397 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2399 win_skip("ImpersonateSelf is not implemented\n");
2402 ok(ret, "ImpersonateSelf(SecurityAnonymous) failed with error %d\n", GetLastError());
2403 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
2404 ok(!ret, "OpenThreadToken should have failed\n");
2405 error = GetLastError();
2406 ok(error == ERROR_CANT_OPEN_ANONYMOUS, "OpenThreadToken on anonymous token should have returned ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error);
2407 /* can't perform access check when opening object against an anonymous impersonation token */
2409 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
2410 ok(error == ERROR_INVALID_HANDLE || error == ERROR_CANT_OPEN_ANONYMOUS,
2411 "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE or ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error);
2415 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, &ProcessToken);
2416 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
2418 ret = pDuplicateTokenEx(ProcessToken,
2419 TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_IMPERSONATE, NULL,
2420 SecurityAnonymous, TokenImpersonation, &Token);
2421 ok(ret, "DuplicateTokenEx failed with error %d\n", GetLastError());
2422 /* can't increase the impersonation level */
2423 ret = DuplicateToken(Token, SecurityIdentification, &Token2);
2424 error = GetLastError();
2425 ok(!ret && error == ERROR_BAD_IMPERSONATION_LEVEL,
2426 "Duplicating a token and increasing the impersonation level should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
2427 /* we can query anything from an anonymous token, including the user */
2428 ret = GetTokenInformation(Token, TokenUser, NULL, 0, &Size);
2429 error = GetLastError();
2430 ok(!ret && error == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenUser) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error);
2431 User = HeapAlloc(GetProcessHeap(), 0, Size);
2432 ret = GetTokenInformation(Token, TokenUser, User, Size, &Size);
2433 ok(ret, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
2434 HeapFree(GetProcessHeap(), 0, User);
2436 /* PrivilegeCheck fails with SecurityAnonymous level */
2437 ret = GetTokenInformation(Token, TokenPrivileges, NULL, 0, &Size);
2438 error = GetLastError();
2439 ok(!ret && error == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenPrivileges) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error);
2440 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
2441 ret = GetTokenInformation(Token, TokenPrivileges, Privileges, Size, &Size);
2442 ok(ret, "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
2444 PrivilegeSet = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PRIVILEGE_SET, Privilege[Privileges->PrivilegeCount]));
2445 PrivilegeSet->PrivilegeCount = Privileges->PrivilegeCount;
2446 memcpy(PrivilegeSet->Privilege, Privileges->Privileges, PrivilegeSet->PrivilegeCount * sizeof(PrivilegeSet->Privilege[0]));
2447 PrivilegeSet->Control = PRIVILEGE_SET_ALL_NECESSARY;
2448 HeapFree(GetProcessHeap(), 0, Privileges);
2450 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2451 error = GetLastError();
2452 ok(!ret && error == ERROR_BAD_IMPERSONATION_LEVEL, "PrivilegeCheck for SecurityAnonymous token should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
2456 ret = ImpersonateSelf(SecurityIdentification);
2457 ok(ret, "ImpersonateSelf(SecurityIdentification) failed with error %d\n", GetLastError());
2458 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
2459 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
2461 /* can't perform access check when opening object against an identification impersonation token */
2462 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
2464 ok(error == ERROR_INVALID_HANDLE || error == ERROR_BAD_IMPERSONATION_LEVEL,
2465 "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE or ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
2467 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2468 ok(ret, "PrivilegeCheck for SecurityIdentification failed with error %d\n", GetLastError());
2472 ret = ImpersonateSelf(SecurityImpersonation);
2473 ok(ret, "ImpersonateSelf(SecurityImpersonation) failed with error %d\n", GetLastError());
2474 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
2475 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
2476 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
2477 ok(error == ERROR_SUCCESS, "RegOpenKeyEx should have succeeded instead of failing with %d\n", error);
2479 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2480 ok(ret, "PrivilegeCheck for SecurityImpersonation failed with error %d\n", GetLastError());
2484 CloseHandle(ProcessToken);
2486 HeapFree(GetProcessHeap(), 0, PrivilegeSet);
2489 static void test_SetEntriesInAcl(void)
2492 PSID EveryoneSid = NULL, UsersSid = NULL;
2493 PACL OldAcl = NULL, NewAcl;
2494 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
2495 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
2496 EXPLICIT_ACCESSW ExplicitAccess;
2497 static const WCHAR wszEveryone[] = {'E','v','e','r','y','o','n','e',0};
2498 static const WCHAR wszCurrentUser[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
2500 if (!pSetEntriesInAclW)
2502 win_skip("SetEntriesInAclW is not available\n");
2506 NewAcl = (PACL)0xdeadbeef;
2507 res = pSetEntriesInAclW(0, NULL, NULL, &NewAcl);
2508 if(res == ERROR_CALL_NOT_IMPLEMENTED)
2510 win_skip("SetEntriesInAclW is not implemented\n");
2513 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2514 ok(NewAcl == NULL ||
2515 broken(NewAcl != NULL), /* NT4 */
2516 "NewAcl=%p, expected NULL\n", NewAcl);
2519 OldAcl = HeapAlloc(GetProcessHeap(), 0, 256);
2520 res = InitializeAcl(OldAcl, 256, ACL_REVISION);
2521 if(!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2523 win_skip("ACLs not implemented - skipping tests\n");
2524 HeapFree(GetProcessHeap(), 0, OldAcl);
2527 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
2529 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
2530 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2532 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
2533 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &UsersSid);
2534 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2536 res = AddAccessAllowedAce(OldAcl, ACL_REVISION, KEY_READ, UsersSid);
2537 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2539 ExplicitAccess.grfAccessPermissions = KEY_WRITE;
2540 ExplicitAccess.grfAccessMode = GRANT_ACCESS;
2541 ExplicitAccess.grfInheritance = NO_INHERITANCE;
2542 ExplicitAccess.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
2543 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
2544 ExplicitAccess.Trustee.ptstrName = EveryoneSid;
2545 ExplicitAccess.Trustee.MultipleTrusteeOperation = 0xDEADBEEF;
2546 ExplicitAccess.Trustee.pMultipleTrustee = (PVOID)0xDEADBEEF;
2547 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2548 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2549 ok(NewAcl != NULL, "returned acl was NULL\n");
2552 ExplicitAccess.Trustee.TrusteeType = TRUSTEE_IS_UNKNOWN;
2553 ExplicitAccess.Trustee.pMultipleTrustee = NULL;
2554 ExplicitAccess.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2555 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2556 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2557 ok(NewAcl != NULL, "returned acl was NULL\n");
2560 if (PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale())) != LANG_ENGLISH)
2562 skip("Non-english locale (test with hardcoded 'Everyone')\n");
2566 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_USER;
2567 ExplicitAccess.Trustee.ptstrName = (LPWSTR)wszEveryone;
2568 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2569 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2570 ok(NewAcl != NULL, "returned acl was NULL\n");
2573 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_BAD_FORM;
2574 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2575 ok(res == ERROR_INVALID_PARAMETER ||
2576 broken(res == ERROR_NOT_SUPPORTED), /* NT4 */
2577 "SetEntriesInAclW failed: %u\n", res);
2578 ok(NewAcl == NULL ||
2579 broken(NewAcl != NULL), /* NT4 */
2580 "returned acl wasn't NULL: %p\n", NewAcl);
2582 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_USER;
2583 ExplicitAccess.Trustee.MultipleTrusteeOperation = TRUSTEE_IS_IMPERSONATE;
2584 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2585 ok(res == ERROR_INVALID_PARAMETER ||
2586 broken(res == ERROR_NOT_SUPPORTED), /* NT4 */
2587 "SetEntriesInAclW failed: %u\n", res);
2588 ok(NewAcl == NULL ||
2589 broken(NewAcl != NULL), /* NT4 */
2590 "returned acl wasn't NULL: %p\n", NewAcl);
2592 ExplicitAccess.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2593 ExplicitAccess.grfAccessMode = SET_ACCESS;
2594 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2595 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2596 ok(NewAcl != NULL, "returned acl was NULL\n");
2600 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_USER;
2601 ExplicitAccess.Trustee.ptstrName = (LPWSTR)wszCurrentUser;
2602 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2603 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2604 ok(NewAcl != NULL, "returned acl was NULL\n");
2607 ExplicitAccess.grfAccessMode = REVOKE_ACCESS;
2608 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
2609 ExplicitAccess.Trustee.ptstrName = UsersSid;
2610 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2611 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2612 ok(NewAcl != NULL, "returned acl was NULL\n");
2615 LocalFree(UsersSid);
2616 LocalFree(EveryoneSid);
2617 HeapFree(GetProcessHeap(), 0, OldAcl);
2620 static void test_GetNamedSecurityInfoA(void)
2622 PSECURITY_DESCRIPTOR pSecDesc;
2624 SECURITY_DESCRIPTOR_CONTROL control;
2627 BOOL owner_defaulted;
2628 BOOL group_defaulted;
2631 CHAR windows_dir[MAX_PATH];
2633 if (!pGetNamedSecurityInfoA)
2635 win_skip("GetNamedSecurityInfoA is not available\n");
2639 ret = GetWindowsDirectoryA(windows_dir, MAX_PATH);
2640 ok(ret, "GetWindowsDirectory failed with error %d\n", GetLastError());
2642 SetLastError(0xdeadbeef);
2643 error = pGetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,
2644 OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION,
2645 NULL, NULL, NULL, NULL, &pSecDesc);
2646 if (error != ERROR_SUCCESS && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2648 win_skip("GetNamedSecurityInfoA is not implemented\n");
2651 ok(!error, "GetNamedSecurityInfo failed with error %d\n", error);
2653 ret = GetSecurityDescriptorControl(pSecDesc, &control, &revision);
2654 ok(ret, "GetSecurityDescriptorControl failed with error %d\n", GetLastError());
2655 ok((control & (SE_SELF_RELATIVE|SE_DACL_PRESENT)) == (SE_SELF_RELATIVE|SE_DACL_PRESENT) ||
2656 broken((control & (SE_SELF_RELATIVE|SE_DACL_PRESENT)) == SE_DACL_PRESENT), /* NT4 */
2657 "control (0x%x) doesn't have (SE_SELF_RELATIVE|SE_DACL_PRESENT) flags set\n", control);
2658 ok(revision == SECURITY_DESCRIPTOR_REVISION1, "revision was %d instead of 1\n", revision);
2659 ret = GetSecurityDescriptorOwner(pSecDesc, &owner, &owner_defaulted);
2660 ok(ret, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
2661 ok(owner != NULL, "owner should not be NULL\n");
2662 ret = GetSecurityDescriptorGroup(pSecDesc, &group, &group_defaulted);
2663 ok(ret, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2664 ok(group != NULL, "group should not be NULL\n");
2665 LocalFree(pSecDesc);
2668 static void test_ConvertStringSecurityDescriptor(void)
2671 PSECURITY_DESCRIPTOR pSD;
2672 static const WCHAR Blank[] = { 0 };
2676 const char *sidstring;
2683 { "D:(A;;GA;;;WD)", 0xdeadbeef, FALSE, ERROR_UNKNOWN_REVISION },
2684 /* test ACE string type */
2685 { "D:(A;;GA;;;WD)", SDDL_REVISION_1, TRUE },
2686 { "D:(D;;GA;;;WD)", SDDL_REVISION_1, TRUE },
2687 { "ERROR:(D;;GA;;;WD)", SDDL_REVISION_1, FALSE, ERROR_INVALID_PARAMETER },
2688 /* test ACE string with spaces */
2689 { " D:(D;;GA;;;WD)", SDDL_REVISION_1, TRUE },
2690 { "D: (D;;GA;;;WD)", SDDL_REVISION_1, TRUE },
2691 { "D:( D;;GA;;;WD)", SDDL_REVISION_1, TRUE },
2692 { "D:(D ;;GA;;;WD)", SDDL_REVISION_1, FALSE, RPC_S_INVALID_STRING_UUID, ERROR_INVALID_ACL }, /* Vista+ */
2693 { "D:(D; ;GA;;;WD)", SDDL_REVISION_1, TRUE },
2694 { "D:(D;; GA;;;WD)", SDDL_REVISION_1, TRUE },
2695 { "D:(D;;GA ;;;WD)", SDDL_REVISION_1, FALSE, ERROR_INVALID_ACL },
2696 { "D:(D;;GA; ;;WD)", SDDL_REVISION_1, TRUE },
2697 { "D:(D;;GA;; ;WD)", SDDL_REVISION_1, TRUE },
2698 { "D:(D;;GA;;; WD)", SDDL_REVISION_1, TRUE },
2699 { "D:(D;;GA;;;WD )", SDDL_REVISION_1, TRUE },
2700 /* test ACE string access rights */
2701 { "D:(A;;GA;;;WD)", SDDL_REVISION_1, TRUE },
2702 { "D:(A;;GRGWGX;;;WD)", SDDL_REVISION_1, TRUE },
2703 { "D:(A;;RCSDWDWO;;;WD)", SDDL_REVISION_1, TRUE },
2704 { "D:(A;;RPWPCCDCLCSWLODTCR;;;WD)", SDDL_REVISION_1, TRUE },
2705 { "D:(A;;FAFRFWFX;;;WD)", SDDL_REVISION_1, TRUE },
2706 { "D:(A;;KAKRKWKX;;;WD)", SDDL_REVISION_1, TRUE },
2707 { "D:(A;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1, TRUE },
2708 { "S:(AU;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1, TRUE },
2709 /* test ACE string access right error case */
2710 { "D:(A;;ROB;;;WD)", SDDL_REVISION_1, FALSE, ERROR_INVALID_ACL },
2711 /* test behaviour with empty strings */
2712 { "", SDDL_REVISION_1, TRUE },
2713 /* test ACE string SID */
2714 { "D:(D;;GA;;;S-1-0-0)", SDDL_REVISION_1, TRUE },
2715 { "D:(D;;GA;;;Nonexistent account)", SDDL_REVISION_1, FALSE, ERROR_INVALID_ACL, ERROR_INVALID_SID } /* W2K */
2718 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA)
2720 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
2724 for (i = 0; i < sizeof(cssd)/sizeof(cssd[0]); i++)
2728 SetLastError(0xdeadbeef);
2729 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2730 cssd[i].sidstring, cssd[i].revision, &pSD, NULL);
2731 GLE = GetLastError();
2732 ok(ret == cssd[i].ret, "(%02d) Expected %s (%d)\n", i, cssd[i].ret ? "success" : "failure", GLE);
2734 ok(GLE == cssd[i].GLE ||
2735 (cssd[i].altGLE && GLE == cssd[i].altGLE),
2736 "(%02d) Unexpected last error %d\n", i, GLE);
2741 /* test behaviour with NULL parameters */
2742 SetLastError(0xdeadbeef);
2743 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2744 NULL, 0xdeadbeef, &pSD, NULL);
2746 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2747 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2750 SetLastError(0xdeadbeef);
2751 ret = pConvertStringSecurityDescriptorToSecurityDescriptorW(
2752 NULL, 0xdeadbeef, &pSD, NULL);
2753 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2754 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2757 SetLastError(0xdeadbeef);
2758 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2759 "D:(A;;ROB;;;WD)", 0xdeadbeef, NULL, 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 = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2766 "D:(A;;ROB;;;WD)", SDDL_REVISION_1, NULL, NULL);
2767 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2768 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2771 /* test behaviour with empty strings */
2772 SetLastError(0xdeadbeef);
2773 ret = pConvertStringSecurityDescriptorToSecurityDescriptorW(
2774 Blank, SDDL_REVISION_1, &pSD, NULL);
2775 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2779 static void test_ConvertSecurityDescriptorToString(void)
2781 SECURITY_DESCRIPTOR desc;
2782 SECURITY_INFORMATION sec_info = OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION|SACL_SECURITY_INFORMATION;
2791 if (!pConvertSecurityDescriptorToStringSecurityDescriptorA)
2793 win_skip("ConvertSecurityDescriptorToStringSecurityDescriptor is not available\n");
2796 if (!pCreateWellKnownSid)
2798 win_skip("CreateWellKnownSid is not available\n");
2802 /* It seems Windows XP adds an extra character to the length of the string for each ACE in an ACL. We
2803 * don't replicate this feature so we only test len >= strlen+1. */
2804 #define CHECK_RESULT_AND_FREE(exp_str) \
2805 ok(strcmp(string, (exp_str)) == 0, "String mismatch (expected \"%s\", got \"%s\")\n", (exp_str), string); \
2806 ok(len >= (lstrlen(exp_str) + 1), "Length mismatch (expected %d, got %d)\n", lstrlen(exp_str) + 1, len); \
2809 #define CHECK_ONE_OF_AND_FREE(exp_str1, exp_str2) \
2810 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); \
2811 ok(len >= (strlen(string) + 1), "Length mismatch (expected %d, got %d)\n", lstrlen(string) + 1, len); \
2814 InitializeSecurityDescriptor(&desc, SECURITY_DESCRIPTOR_REVISION);
2815 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2816 CHECK_RESULT_AND_FREE("");
2819 pCreateWellKnownSid(WinLocalSid, NULL, sid_buf, &size);
2820 SetSecurityDescriptorOwner(&desc, sid_buf, FALSE);
2821 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2822 CHECK_RESULT_AND_FREE("O:S-1-2-0");
2824 SetSecurityDescriptorOwner(&desc, sid_buf, TRUE);
2825 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2826 CHECK_RESULT_AND_FREE("O:S-1-2-0");
2828 size = sizeof(sid_buf);
2829 pCreateWellKnownSid(WinLocalSystemSid, NULL, sid_buf, &size);
2830 SetSecurityDescriptorOwner(&desc, sid_buf, TRUE);
2831 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2832 CHECK_RESULT_AND_FREE("O:SY");
2834 pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid);
2835 SetSecurityDescriptorGroup(&desc, psid, TRUE);
2836 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2837 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576");
2839 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, GROUP_SECURITY_INFORMATION, &string, &len), "Conversion failed\n");
2840 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
2842 pacl = (PACL)acl_buf;
2843 InitializeAcl(pacl, sizeof(acl_buf), ACL_REVISION);
2844 SetSecurityDescriptorDacl(&desc, TRUE, pacl, TRUE);
2845 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2846 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
2848 SetSecurityDescriptorDacl(&desc, TRUE, pacl, FALSE);
2849 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2850 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
2852 pConvertStringSidToSidA("S-1-5-6", &psid2);
2853 pAddAccessAllowedAceEx(pacl, ACL_REVISION, NO_PROPAGATE_INHERIT_ACE, 0xf0000000, psid2);
2854 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2855 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)");
2857 pAddAccessAllowedAceEx(pacl, ACL_REVISION, INHERIT_ONLY_ACE|INHERITED_ACE, 0x00000003, psid2);
2858 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2859 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)");
2861 pAddAccessDeniedAceEx(pacl, ACL_REVISION, OBJECT_INHERIT_ACE|CONTAINER_INHERIT_ACE, 0xffffffff, psid);
2862 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2863 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)");
2866 pacl = (PACL)acl_buf;
2867 InitializeAcl(pacl, sizeof(acl_buf), ACL_REVISION);
2868 SetSecurityDescriptorSacl(&desc, TRUE, pacl, FALSE);
2869 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2870 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:");
2872 /* fails in win2k */
2873 SetSecurityDescriptorDacl(&desc, TRUE, NULL, FALSE);
2874 pAddAuditAccessAceEx(pacl, ACL_REVISION, VALID_INHERIT_FLAGS, KEY_READ|KEY_WRITE, psid2, TRUE, TRUE);
2875 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len))
2877 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)", /* XP */
2878 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)" /* Vista */);
2881 /* fails in win2k */
2882 pAddAuditAccessAceEx(pacl, ACL_REVISION, NO_PROPAGATE_INHERIT_ACE, FILE_GENERIC_READ|FILE_GENERIC_WRITE, psid2, TRUE, FALSE);
2883 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len))
2885 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", /* XP */
2886 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)" /* Vista */);
2890 static void test_SetSecurityDescriptorControl (PSECURITY_DESCRIPTOR sec)
2892 SECURITY_DESCRIPTOR_CONTROL ref;
2893 SECURITY_DESCRIPTOR_CONTROL test;
2895 SECURITY_DESCRIPTOR_CONTROL const mutable
2896 = SE_DACL_AUTO_INHERIT_REQ | SE_SACL_AUTO_INHERIT_REQ
2897 | SE_DACL_AUTO_INHERITED | SE_SACL_AUTO_INHERITED
2898 | SE_DACL_PROTECTED | SE_SACL_PROTECTED
2899 | 0x00000040 | 0x00000080 /* not defined in winnt.h */
2901 SECURITY_DESCRIPTOR_CONTROL const immutable
2902 = SE_OWNER_DEFAULTED | SE_GROUP_DEFAULTED
2903 | SE_DACL_PRESENT | SE_DACL_DEFAULTED
2904 | SE_SACL_PRESENT | SE_SACL_DEFAULTED
2905 | SE_RM_CONTROL_VALID | SE_SELF_RELATIVE
2910 LPCSTR fmt = "Expected error %s, got %u\n";
2912 GetSecurityDescriptorControl (sec, &ref, &dwRevision);
2914 /* The mutable bits are mutable regardless of the truth of
2915 SE_DACL_PRESENT and/or SE_SACL_PRESENT */
2917 /* Check call barfs if any bit-of-interest is immutable */
2918 for (bit = 0; bit < 16; ++bit)
2920 SECURITY_DESCRIPTOR_CONTROL const bitOfInterest = 1 << bit;
2921 SECURITY_DESCRIPTOR_CONTROL setOrClear = ref & bitOfInterest;
2923 SECURITY_DESCRIPTOR_CONTROL ctrl;
2925 DWORD dwExpect = (bitOfInterest & immutable)
2926 ? ERROR_INVALID_PARAMETER : 0xbebecaca;
2927 LPCSTR strExpect = (bitOfInterest & immutable)
2928 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
2930 ctrl = (bitOfInterest & mutable) ? ref + bitOfInterest : ref;
2931 setOrClear ^= bitOfInterest;
2932 SetLastError (0xbebecaca);
2933 pSetSecurityDescriptorControl (sec, bitOfInterest, setOrClear);
2934 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
2935 GetSecurityDescriptorControl(sec, &test, &dwRevision);
2936 expect_eq(test, ctrl, int, "%x");
2939 setOrClear ^= bitOfInterest;
2940 SetLastError (0xbebecaca);
2941 pSetSecurityDescriptorControl (sec, bitOfInterest, setOrClear);
2942 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
2943 GetSecurityDescriptorControl (sec, &test, &dwRevision);
2944 expect_eq(test, ref, int, "%x");
2947 /* Check call barfs if any bit-to-set is immutable
2948 even when not a bit-of-interest */
2949 for (bit = 0; bit < 16; ++bit)
2951 SECURITY_DESCRIPTOR_CONTROL const bitsOfInterest = mutable;
2952 SECURITY_DESCRIPTOR_CONTROL setOrClear = ref & bitsOfInterest;
2954 SECURITY_DESCRIPTOR_CONTROL ctrl;
2956 DWORD dwExpect = ((1 << bit) & immutable)
2957 ? ERROR_INVALID_PARAMETER : 0xbebecaca;
2958 LPCSTR strExpect = ((1 << bit) & immutable)
2959 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
2961 ctrl = ((1 << bit) & immutable) ? test : ref | mutable;
2962 setOrClear ^= bitsOfInterest;
2963 SetLastError (0xbebecaca);
2964 pSetSecurityDescriptorControl (sec, bitsOfInterest, setOrClear | (1 << bit));
2965 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
2966 GetSecurityDescriptorControl(sec, &test, &dwRevision);
2967 expect_eq(test, ctrl, int, "%x");
2969 ctrl = ((1 << bit) & immutable) ? test : ref | (1 << bit);
2970 setOrClear ^= bitsOfInterest;
2971 SetLastError (0xbebecaca);
2972 pSetSecurityDescriptorControl (sec, bitsOfInterest, setOrClear | (1 << bit));
2973 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
2974 GetSecurityDescriptorControl(sec, &test, &dwRevision);
2975 expect_eq(test, ctrl, int, "%x");
2979 static void test_PrivateObjectSecurity(void)
2981 SECURITY_INFORMATION sec_info = OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION|SACL_SECURITY_INFORMATION;
2982 SECURITY_DESCRIPTOR_CONTROL ctrl;
2983 PSECURITY_DESCRIPTOR sec;
2989 PSECURITY_DESCRIPTOR buf;
2991 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA)
2993 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
2997 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
2999 "G:S-1-5-21-93476-23408-4576"
3000 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)"
3001 "(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3002 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
3003 SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
3005 test_SetSecurityDescriptorControl(sec);
3009 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3011 "G:S-1-5-21-93476-23408-4576",
3012 SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
3014 test_SetSecurityDescriptorControl(sec);
3018 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3020 "G:S-1-5-21-93476-23408-4576"
3021 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3022 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
3023 buf = HeapAlloc(GetProcessHeap(), 0, dwDescSize);
3024 pSetSecurityDescriptorControl(sec, SE_DACL_PROTECTED, SE_DACL_PROTECTED);
3025 GetSecurityDescriptorControl(sec, &ctrl, &dwRevision);
3026 expect_eq(ctrl, 0x9014, int, "%x");
3028 ok(GetPrivateObjectSecurity(sec, GROUP_SECURITY_INFORMATION, buf, dwDescSize, &retSize),
3029 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3030 ok(retSize <= dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
3031 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3032 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
3033 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
3034 expect_eq(ctrl, 0x8000, int, "%x");
3036 ok(GetPrivateObjectSecurity(sec, GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION, buf, dwDescSize, &retSize),
3037 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3038 ok(retSize <= dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
3039 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed err=%u\n", GetLastError());
3040 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)",
3041 "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 */
3042 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
3043 expect_eq(ctrl & (~ SE_DACL_PROTECTED), 0x8004, int, "%x");
3045 ok(GetPrivateObjectSecurity(sec, sec_info, 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_ONE_OF_AND_FREE("O:SY"
3050 "G:S-1-5-21-93476-23408-4576"
3051 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3052 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
3054 "G:S-1-5-21-93476-23408-4576"
3055 "D:P(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3056 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)"); /* Win7 */
3057 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
3058 expect_eq(ctrl & (~ SE_DACL_PROTECTED), 0x8014, int, "%x");
3060 SetLastError(0xdeadbeef);
3061 ok(GetPrivateObjectSecurity(sec, sec_info, buf, 5, &retSize) == FALSE, "GetPrivateObjectSecurity should have failed\n");
3062 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected error ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
3065 HeapFree(GetProcessHeap(), 0, buf);
3067 #undef CHECK_RESULT_AND_FREE
3068 #undef CHECK_ONE_OF_AND_FREE
3070 static void test_acls(void)
3073 PACL pAcl = (PACL)buffer;
3076 SetLastError(0xdeadbeef);
3077 ret = InitializeAcl(pAcl, sizeof(ACL) - 1, ACL_REVISION);
3078 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
3080 win_skip("InitializeAcl is not implemented\n");
3084 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InitializeAcl with too small a buffer should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
3086 SetLastError(0xdeadbeef);
3087 ret = InitializeAcl(pAcl, 0xffffffff, ACL_REVISION);
3088 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "InitializeAcl with too large a buffer should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3090 SetLastError(0xdeadbeef);
3091 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION1);
3092 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "InitializeAcl(ACL_REVISION1) should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3094 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION2);
3095 ok(ret, "InitializeAcl(ACL_REVISION2) failed with error %d\n", GetLastError());
3097 ret = IsValidAcl(pAcl);
3098 ok(ret, "IsValidAcl failed with error %d\n", GetLastError());
3100 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION3);
3101 ok(ret, "InitializeAcl(ACL_REVISION3) failed with error %d\n", GetLastError());
3103 ret = IsValidAcl(pAcl);
3104 ok(ret, "IsValidAcl failed with error %d\n", GetLastError());
3106 SetLastError(0xdeadbeef);
3107 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION4);
3108 if (GetLastError() != ERROR_INVALID_PARAMETER)
3110 ok(ret, "InitializeAcl(ACL_REVISION4) failed with error %d\n", GetLastError());
3112 ret = IsValidAcl(pAcl);
3113 ok(ret, "IsValidAcl failed with error %d\n", GetLastError());
3116 win_skip("ACL_REVISION4 is not implemented on NT4\n");
3118 SetLastError(0xdeadbeef);
3119 ret = InitializeAcl(pAcl, sizeof(buffer), -1);
3120 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "InitializeAcl(-1) failed with error %d\n", GetLastError());
3123 static void test_GetSecurityInfo(void)
3126 PSECURITY_DESCRIPTOR sd;
3131 if (!pGetSecurityInfo)
3133 win_skip("GetSecurityInfo is not available\n");
3137 /* Create something. Files have lots of associated security info. */
3138 obj = CreateFile(myARGV[0], GENERIC_READ, FILE_SHARE_READ, NULL,
3139 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
3140 if (obj == INVALID_HANDLE_VALUE)
3142 skip("Couldn't create an object for GetSecurityInfo test\n");
3146 ret = pGetSecurityInfo(obj, SE_FILE_OBJECT,
3147 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
3148 &owner, &group, &dacl, NULL, &sd);
3149 if (ret == ERROR_CALL_NOT_IMPLEMENTED)
3151 win_skip("GetSecurityInfo is not implemented\n");
3155 ok(ret == ERROR_SUCCESS, "GetSecurityInfo returned %d\n", ret);
3156 ok(sd != NULL, "GetSecurityInfo\n");
3157 ok(owner != NULL, "GetSecurityInfo\n");
3158 ok(group != NULL, "GetSecurityInfo\n");
3160 ok(IsValidAcl(dacl), "GetSecurityInfo\n");
3162 win_skip("No ACL information returned\n");
3166 if (!pCreateWellKnownSid)
3168 win_skip("NULL parameter test would crash on NT4\n");
3173 /* If we don't ask for the security descriptor, Windows will still give us
3174 the other stuff, leaving us no way to free it. */
3175 ret = pGetSecurityInfo(obj, SE_FILE_OBJECT,
3176 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
3177 &owner, &group, &dacl, NULL, NULL);
3178 ok(ret == ERROR_SUCCESS, "GetSecurityInfo returned %d\n", ret);
3179 ok(owner != NULL, "GetSecurityInfo\n");
3180 ok(group != NULL, "GetSecurityInfo\n");
3182 ok(IsValidAcl(dacl), "GetSecurityInfo\n");
3184 win_skip("No ACL information returned\n");
3189 static void test_GetSidSubAuthority(void)
3193 if (!pGetSidSubAuthority || !pConvertStringSidToSidA || !pIsValidSid || !pGetSidSubAuthorityCount)
3195 win_skip("Some functions not available\n");
3198 /* Note: on windows passing in an invalid index like -1, lets GetSidSubAuthority return 0x05000000 but
3199 still GetLastError returns ERROR_SUCCESS then. We don't test these unlikely cornercases here for now */
3200 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576",&psid),"ConvertStringSidToSidA failed\n");
3201 ok(pIsValidSid(psid),"Sid is not valid\n");
3202 SetLastError(0xbebecaca);
3203 ok(*pGetSidSubAuthorityCount(psid) == 4,"GetSidSubAuthorityCount gave %d expected 4\n",*pGetSidSubAuthorityCount(psid));
3204 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3205 SetLastError(0xbebecaca);
3206 ok(*pGetSidSubAuthority(psid,0) == 21,"GetSidSubAuthority gave %d expected 21\n",*pGetSidSubAuthority(psid,0));
3207 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3208 SetLastError(0xbebecaca);
3209 ok(*pGetSidSubAuthority(psid,1) == 93476,"GetSidSubAuthority gave %d expected 93476\n",*pGetSidSubAuthority(psid,1));
3210 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3211 SetLastError(0xbebecaca);
3212 todo_wine ok(*pGetSidSubAuthority(psid,4) == 0,"GetSidSubAuthority gave %d,expected 0\n",*pGetSidSubAuthority(psid,4));
3213 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3217 static void test_CheckTokenMembership(void)
3219 PTOKEN_GROUPS token_groups;
3221 HANDLE process_token, token;
3226 if (!pCheckTokenMembership)
3228 win_skip("CheckTokenMembership is not available\n");
3231 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &process_token);
3232 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
3234 ret = DuplicateToken(process_token, SecurityImpersonation, &token);
3235 ok(ret, "DuplicateToken failed with error %d\n", GetLastError());
3238 ret = GetTokenInformation(token, TokenGroups, NULL, 0, &size);
3239 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
3240 "GetTokenInformation(TokenGroups) %s with error %d\n",
3241 ret ? "succeeded" : "failed", GetLastError());
3242 token_groups = HeapAlloc(GetProcessHeap(), 0, size);
3243 ret = GetTokenInformation(token, TokenGroups, token_groups, size, &size);
3244 ok(ret, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
3246 for (i = 0; i < token_groups->GroupCount; i++)
3248 if (token_groups->Groups[i].Attributes & SE_GROUP_ENABLED)
3252 if (i == token_groups->GroupCount)
3254 HeapFree(GetProcessHeap(), 0, token_groups);
3256 skip("user not a member of any group\n");
3260 ret = pCheckTokenMembership(token, token_groups->Groups[i].Sid, &is_member);
3261 ok(ret, "CheckTokenMembership failed with error %d\n", GetLastError());
3262 ok(is_member, "CheckTokenMembership should have detected sid as member\n");
3264 ret = pCheckTokenMembership(NULL, token_groups->Groups[i].Sid, &is_member);
3265 ok(ret, "CheckTokenMembership failed with error %d\n", GetLastError());
3266 ok(is_member, "CheckTokenMembership should have detected sid as member\n");
3268 ret = pCheckTokenMembership(process_token, token_groups->Groups[i].Sid, &is_member);
3270 ok(!ret && GetLastError() == ERROR_NO_IMPERSONATION_TOKEN,
3271 "CheckTokenMembership with process token %s with error %d\n",
3272 ret ? "succeeded" : "failed", GetLastError());
3273 ok(!is_member, "CheckTokenMembership should have cleared is_member\n");
3276 HeapFree(GetProcessHeap(), 0, token_groups);
3278 CloseHandle(process_token);
3281 static void test_EqualSid(void)
3285 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
3286 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
3288 SetLastError(0xdeadbeef);
3289 ret = AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
3290 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &sid1);
3291 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
3293 win_skip("AllocateAndInitializeSid is not implemented\n");
3296 ok(ret, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3297 ok(GetLastError() == 0xdeadbeef,
3298 "AllocateAndInitializeSid shouldn't have set last error to %d\n",
3301 ret = AllocateAndInitializeSid(&SIDAuthWorld, 1, SECURITY_WORLD_RID,
3302 0, 0, 0, 0, 0, 0, 0, &sid2);
3303 ok(ret, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3305 SetLastError(0xdeadbeef);
3306 ret = EqualSid(sid1, sid2);
3307 ok(!ret, "World and domain admins sids shouldn't have been equal\n");
3308 ok(GetLastError() == ERROR_SUCCESS ||
3309 broken(GetLastError() == 0xdeadbeef), /* NT4 */
3310 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
3313 SetLastError(0xdeadbeef);
3314 sid2 = FreeSid(sid2);
3315 ok(!sid2, "FreeSid should have returned NULL instead of %p\n", sid2);
3316 ok(GetLastError() == 0xdeadbeef,
3317 "FreeSid shouldn't have set last error to %d\n",
3320 ret = AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
3321 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &sid2);
3322 ok(ret, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3324 SetLastError(0xdeadbeef);
3325 ret = EqualSid(sid1, sid2);
3326 ok(ret, "Same sids should have been equal\n");
3327 ok(GetLastError() == ERROR_SUCCESS ||
3328 broken(GetLastError() == 0xdeadbeef), /* NT4 */
3329 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
3332 ((SID *)sid2)->Revision = 2;
3333 SetLastError(0xdeadbeef);
3334 ret = EqualSid(sid1, sid2);
3335 ok(!ret, "EqualSid with invalid sid should have returned FALSE\n");
3336 ok(GetLastError() == ERROR_SUCCESS ||
3337 broken(GetLastError() == 0xdeadbeef), /* NT4 */
3338 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
3340 ((SID *)sid2)->Revision = SID_REVISION;
3346 START_TEST(security)
3353 test_process_security_child();
3359 test_CreateWellKnownSid();
3360 test_FileSecurity();
3363 test_LookupAccountSid();
3364 test_LookupAccountName();
3365 test_security_descriptor();
3366 test_process_security();
3367 test_impersonation_level();
3368 test_SetEntriesInAcl();
3369 test_GetNamedSecurityInfoA();
3370 test_ConvertStringSecurityDescriptor();
3371 test_ConvertSecurityDescriptorToString();
3372 test_PrivateObjectSecurity();
3374 test_GetSecurityInfo();
3375 test_GetSidSubAuthority();
3376 test_CheckTokenMembership();