2 * Unit tests for security functions
4 * Copyright (c) 2004 Mike McCormack
5 * Copyright (c) 2011 Dmitry Timoshkov
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #define WIN32_NO_STATUS
36 #include "wine/test.h"
38 /* PROCESS_ALL_ACCESS in Vista+ PSDKs is incompatible with older Windows versions */
39 #define PROCESS_ALL_ACCESS_NT4 (PROCESS_ALL_ACCESS & ~0xf000)
41 /* copied from Wine winternl.h - not included in the Windows SDK */
42 typedef enum _OBJECT_INFORMATION_CLASS
{
43 ObjectBasicInformation
,
44 ObjectNameInformation
,
45 ObjectTypeInformation
,
48 } OBJECT_INFORMATION_CLASS
, *POBJECT_INFORMATION_CLASS
;
50 typedef struct _OBJECT_BASIC_INFORMATION
{
52 ACCESS_MASK GrantedAccess
;
56 ULONG NonPagedPoolUsage
;
58 ULONG NameInformationLength
;
59 ULONG TypeInformationLength
;
60 ULONG SecurityDescriptorLength
;
61 LARGE_INTEGER CreateTime
;
62 } OBJECT_BASIC_INFORMATION
, *POBJECT_BASIC_INFORMATION
;
64 #define expect_eq(expr, value, type, format) { type ret = expr; ok((value) == ret, #expr " expected " format " got " format "\n", (value), (ret)); }
66 static BOOL (WINAPI
*pAddAccessAllowedAceEx
)(PACL
, DWORD
, DWORD
, DWORD
, PSID
);
67 static BOOL (WINAPI
*pAddAccessDeniedAceEx
)(PACL
, DWORD
, DWORD
, DWORD
, PSID
);
68 static BOOL (WINAPI
*pAddAuditAccessAceEx
)(PACL
, DWORD
, DWORD
, DWORD
, PSID
, BOOL
, BOOL
);
69 static VOID (WINAPI
*pBuildTrusteeWithSidA
)( PTRUSTEEA pTrustee
, PSID pSid
);
70 static VOID (WINAPI
*pBuildTrusteeWithNameA
)( PTRUSTEEA pTrustee
, LPSTR pName
);
71 static VOID (WINAPI
*pBuildTrusteeWithObjectsAndNameA
)( PTRUSTEEA pTrustee
,
72 POBJECTS_AND_NAME_A pObjName
,
73 SE_OBJECT_TYPE ObjectType
,
75 LPSTR InheritedObjectTypeName
,
77 static VOID (WINAPI
*pBuildTrusteeWithObjectsAndSidA
)( PTRUSTEEA pTrustee
,
78 POBJECTS_AND_SID pObjSid
,
80 GUID
* pInheritedObjectGuid
,
82 static LPSTR (WINAPI
*pGetTrusteeNameA
)( PTRUSTEEA pTrustee
);
83 static BOOL (WINAPI
*pMakeSelfRelativeSD
)( PSECURITY_DESCRIPTOR
, PSECURITY_DESCRIPTOR
, LPDWORD
);
84 static BOOL (WINAPI
*pConvertSidToStringSidA
)( PSID pSid
, LPSTR
*str
);
85 static BOOL (WINAPI
*pConvertStringSidToSidA
)( LPCSTR str
, PSID pSid
);
86 static BOOL (WINAPI
*pCheckTokenMembership
)(HANDLE
, PSID
, PBOOL
);
87 static BOOL (WINAPI
*pConvertStringSecurityDescriptorToSecurityDescriptorA
)(LPCSTR
, DWORD
,
88 PSECURITY_DESCRIPTOR
*, PULONG
);
89 static BOOL (WINAPI
*pConvertStringSecurityDescriptorToSecurityDescriptorW
)(LPCWSTR
, DWORD
,
90 PSECURITY_DESCRIPTOR
*, PULONG
);
91 static BOOL (WINAPI
*pConvertSecurityDescriptorToStringSecurityDescriptorA
)(PSECURITY_DESCRIPTOR
, DWORD
,
92 SECURITY_INFORMATION
, LPSTR
*, PULONG
);
93 static BOOL (WINAPI
*pGetFileSecurityA
)(LPCSTR
, SECURITY_INFORMATION
,
94 PSECURITY_DESCRIPTOR
, DWORD
, LPDWORD
);
95 static BOOL (WINAPI
*pSetFileSecurityA
)(LPCSTR
, SECURITY_INFORMATION
,
96 PSECURITY_DESCRIPTOR
);
97 static DWORD (WINAPI
*pGetNamedSecurityInfoA
)(LPSTR
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
98 PSID
*, PSID
*, PACL
*, PACL
*,
99 PSECURITY_DESCRIPTOR
*);
100 static PDWORD (WINAPI
*pGetSidSubAuthority
)(PSID
, DWORD
);
101 static PUCHAR (WINAPI
*pGetSidSubAuthorityCount
)(PSID
);
102 static BOOL (WINAPI
*pIsValidSid
)(PSID
);
103 static DWORD (WINAPI
*pRtlAdjustPrivilege
)(ULONG
,BOOLEAN
,BOOLEAN
,PBOOLEAN
);
104 static BOOL (WINAPI
*pCreateWellKnownSid
)(WELL_KNOWN_SID_TYPE
,PSID
,PSID
,DWORD
*);
105 static BOOL (WINAPI
*pDuplicateTokenEx
)(HANDLE
,DWORD
,LPSECURITY_ATTRIBUTES
,
106 SECURITY_IMPERSONATION_LEVEL
,TOKEN_TYPE
,PHANDLE
);
108 static NTSTATUS (WINAPI
*pLsaQueryInformationPolicy
)(LSA_HANDLE
,POLICY_INFORMATION_CLASS
,PVOID
*);
109 static NTSTATUS (WINAPI
*pLsaClose
)(LSA_HANDLE
);
110 static NTSTATUS (WINAPI
*pLsaFreeMemory
)(PVOID
);
111 static NTSTATUS (WINAPI
*pLsaOpenPolicy
)(PLSA_UNICODE_STRING
,PLSA_OBJECT_ATTRIBUTES
,ACCESS_MASK
,PLSA_HANDLE
);
112 static NTSTATUS (WINAPI
*pNtQueryObject
)(HANDLE
,OBJECT_INFORMATION_CLASS
,PVOID
,ULONG
,PULONG
);
113 static DWORD (WINAPI
*pSetEntriesInAclW
)(ULONG
, PEXPLICIT_ACCESSW
, PACL
, PACL
*);
114 static DWORD (WINAPI
*pSetEntriesInAclA
)(ULONG
, PEXPLICIT_ACCESSA
, PACL
, PACL
*);
115 static BOOL (WINAPI
*pSetSecurityDescriptorControl
)(PSECURITY_DESCRIPTOR
, SECURITY_DESCRIPTOR_CONTROL
,
116 SECURITY_DESCRIPTOR_CONTROL
);
117 static DWORD (WINAPI
*pGetSecurityInfo
)(HANDLE
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
118 PSID
*, PSID
*, PACL
*, PACL
*, PSECURITY_DESCRIPTOR
*);
119 static NTSTATUS (WINAPI
*pNtAccessCheck
)(PSECURITY_DESCRIPTOR
, HANDLE
, ACCESS_MASK
, PGENERIC_MAPPING
,
120 PPRIVILEGE_SET
, PULONG
, PULONG
, NTSTATUS
*);
121 static BOOL (WINAPI
*pCreateRestrictedToken
)(HANDLE
, DWORD
, DWORD
, PSID_AND_ATTRIBUTES
, DWORD
,
122 PLUID_AND_ATTRIBUTES
, DWORD
, PSID_AND_ATTRIBUTES
, PHANDLE
);
126 static char** myARGV
;
130 SID_IDENTIFIER_AUTHORITY auth
;
134 static void init(void)
138 hntdll
= GetModuleHandleA("ntdll.dll");
139 pNtQueryObject
= (void *)GetProcAddress( hntdll
, "NtQueryObject" );
140 pNtAccessCheck
= (void *)GetProcAddress( hntdll
, "NtAccessCheck" );
142 hmod
= GetModuleHandle("advapi32.dll");
143 pAddAccessAllowedAceEx
= (void *)GetProcAddress(hmod
, "AddAccessAllowedAceEx");
144 pAddAccessDeniedAceEx
= (void *)GetProcAddress(hmod
, "AddAccessDeniedAceEx");
145 pAddAuditAccessAceEx
= (void *)GetProcAddress(hmod
, "AddAuditAccessAceEx");
146 pCheckTokenMembership
= (void *)GetProcAddress(hmod
, "CheckTokenMembership");
147 pConvertStringSecurityDescriptorToSecurityDescriptorA
=
148 (void *)GetProcAddress(hmod
, "ConvertStringSecurityDescriptorToSecurityDescriptorA" );
149 pConvertStringSecurityDescriptorToSecurityDescriptorW
=
150 (void *)GetProcAddress(hmod
, "ConvertStringSecurityDescriptorToSecurityDescriptorW" );
151 pConvertSecurityDescriptorToStringSecurityDescriptorA
=
152 (void *)GetProcAddress(hmod
, "ConvertSecurityDescriptorToStringSecurityDescriptorA" );
153 pGetFileSecurityA
= (void *)GetProcAddress(hmod
, "GetFileSecurityA" );
154 pSetFileSecurityA
= (void *)GetProcAddress(hmod
, "SetFileSecurityA" );
155 pCreateWellKnownSid
= (void *)GetProcAddress( hmod
, "CreateWellKnownSid" );
156 pGetNamedSecurityInfoA
= (void *)GetProcAddress(hmod
, "GetNamedSecurityInfoA");
157 pGetSidSubAuthority
= (void *)GetProcAddress(hmod
, "GetSidSubAuthority");
158 pGetSidSubAuthorityCount
= (void *)GetProcAddress(hmod
, "GetSidSubAuthorityCount");
159 pIsValidSid
= (void *)GetProcAddress(hmod
, "IsValidSid");
160 pMakeSelfRelativeSD
= (void *)GetProcAddress(hmod
, "MakeSelfRelativeSD");
161 pSetEntriesInAclW
= (void *)GetProcAddress(hmod
, "SetEntriesInAclW");
162 pSetEntriesInAclA
= (void *)GetProcAddress(hmod
, "SetEntriesInAclA");
163 pSetSecurityDescriptorControl
= (void *)GetProcAddress(hmod
, "SetSecurityDescriptorControl");
164 pGetSecurityInfo
= (void *)GetProcAddress(hmod
, "GetSecurityInfo");
165 pCreateRestrictedToken
= (void *)GetProcAddress(hmod
, "CreateRestrictedToken");
167 myARGC
= winetest_get_mainargs( &myARGV
);
170 static void test_str_sid(const char *str_sid
)
175 if (pConvertStringSidToSidA(str_sid
, &psid
))
177 if (pConvertSidToStringSidA(psid
, &temp
))
179 trace(" %s: %s\n", str_sid
, temp
);
186 if (GetLastError() != ERROR_INVALID_SID
)
187 trace(" %s: couldn't be converted, returned %d\n", str_sid
, GetLastError());
189 trace(" %s: couldn't be converted\n", str_sid
);
193 static void test_sid(void)
195 struct sidRef refs
[] = {
196 { { {0x00,0x00,0x33,0x44,0x55,0x66} }, "S-1-860116326-1" },
197 { { {0x00,0x00,0x01,0x02,0x03,0x04} }, "S-1-16909060-1" },
198 { { {0x00,0x00,0x00,0x01,0x02,0x03} }, "S-1-66051-1" },
199 { { {0x00,0x00,0x00,0x00,0x01,0x02} }, "S-1-258-1" },
200 { { {0x00,0x00,0x00,0x00,0x00,0x02} }, "S-1-2-1" },
201 { { {0x00,0x00,0x00,0x00,0x00,0x0c} }, "S-1-12-1" },
203 const char noSubAuthStr
[] = "S-1-5";
210 pConvertSidToStringSidA
= (void *)GetProcAddress( hmod
, "ConvertSidToStringSidA" );
211 if( !pConvertSidToStringSidA
)
213 pConvertStringSidToSidA
= (void *)GetProcAddress( hmod
, "ConvertStringSidToSidA" );
214 if( !pConvertStringSidToSidA
)
217 r
= pConvertStringSidToSidA( NULL
, NULL
);
218 ok( !r
, "expected failure with NULL parameters\n" );
219 if( GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
221 ok( GetLastError() == ERROR_INVALID_PARAMETER
,
222 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
225 r
= pConvertStringSidToSidA( refs
[0].refStr
, NULL
);
226 ok( !r
&& GetLastError() == ERROR_INVALID_PARAMETER
,
227 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
230 r
= pConvertStringSidToSidA( NULL
, &str
);
231 ok( !r
&& GetLastError() == ERROR_INVALID_PARAMETER
,
232 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
235 r
= pConvertStringSidToSidA( noSubAuthStr
, &psid
);
237 "expected failure with no sub authorities\n" );
238 ok( GetLastError() == ERROR_INVALID_SID
,
239 "expected GetLastError() is ERROR_INVALID_SID, got %d\n",
242 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid
), "ConvertStringSidToSidA failed\n");
244 ok(pisid
->SubAuthorityCount
== 4, "Invalid sub authority count - expected 4, got %d\n", pisid
->SubAuthorityCount
);
245 ok(pisid
->SubAuthority
[0] == 21, "Invalid subauthority 0 - expected 21, got %d\n", pisid
->SubAuthority
[0]);
246 ok(pisid
->SubAuthority
[3] == 4576, "Invalid subauthority 0 - expected 4576, got %d\n", pisid
->SubAuthority
[3]);
250 for( i
= 0; i
< sizeof(refs
) / sizeof(refs
[0]); i
++ )
254 r
= AllocateAndInitializeSid( &refs
[i
].auth
, 1,1,0,0,0,0,0,0,0,
256 ok( r
, "failed to allocate sid\n" );
257 r
= pConvertSidToStringSidA( psid
, &str
);
258 ok( r
, "failed to convert sid\n" );
261 ok( !strcmp( str
, refs
[i
].refStr
),
262 "incorrect sid, expected %s, got %s\n", refs
[i
].refStr
, str
);
268 r
= pConvertStringSidToSidA( refs
[i
].refStr
, &psid
);
269 ok( r
, "failed to parse sid string\n" );
272 !memcmp( pisid
->IdentifierAuthority
.Value
, refs
[i
].auth
.Value
,
273 sizeof(refs
[i
].auth
) ),
274 "string sid %s didn't parse to expected value\n"
275 "(got 0x%04x%08x, expected 0x%04x%08x)\n",
277 MAKEWORD( pisid
->IdentifierAuthority
.Value
[1],
278 pisid
->IdentifierAuthority
.Value
[0] ),
279 MAKELONG( MAKEWORD( pisid
->IdentifierAuthority
.Value
[5],
280 pisid
->IdentifierAuthority
.Value
[4] ),
281 MAKEWORD( pisid
->IdentifierAuthority
.Value
[3],
282 pisid
->IdentifierAuthority
.Value
[2] ) ),
283 MAKEWORD( refs
[i
].auth
.Value
[1], refs
[i
].auth
.Value
[0] ),
284 MAKELONG( MAKEWORD( refs
[i
].auth
.Value
[5], refs
[i
].auth
.Value
[4] ),
285 MAKEWORD( refs
[i
].auth
.Value
[3], refs
[i
].auth
.Value
[2] ) ) );
290 trace("String SIDs:\n");
331 static void test_trustee(void)
333 GUID ObjectType
= {0x12345678, 0x1234, 0x5678, {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}};
334 GUID InheritedObjectType
= {0x23456789, 0x2345, 0x6786, {0x2, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}};
336 OBJECTS_AND_NAME_ oan
;
340 char szObjectTypeName
[] = "ObjectTypeName";
341 char szInheritedObjectTypeName
[] = "InheritedObjectTypeName";
342 char szTrusteeName
[] = "szTrusteeName";
343 SID_IDENTIFIER_AUTHORITY auth
= { {0x11,0x22,0,0,0, 0} };
345 memset( &ZeroGuid
, 0x00, sizeof (ZeroGuid
) );
347 pBuildTrusteeWithSidA
= (void *)GetProcAddress( hmod
, "BuildTrusteeWithSidA" );
348 pBuildTrusteeWithNameA
= (void *)GetProcAddress( hmod
, "BuildTrusteeWithNameA" );
349 pBuildTrusteeWithObjectsAndNameA
= (void *)GetProcAddress (hmod
, "BuildTrusteeWithObjectsAndNameA" );
350 pBuildTrusteeWithObjectsAndSidA
= (void *)GetProcAddress (hmod
, "BuildTrusteeWithObjectsAndSidA" );
351 pGetTrusteeNameA
= (void *)GetProcAddress (hmod
, "GetTrusteeNameA" );
352 if( !pBuildTrusteeWithSidA
|| !pBuildTrusteeWithNameA
||
353 !pBuildTrusteeWithObjectsAndNameA
|| !pBuildTrusteeWithObjectsAndSidA
||
357 if ( ! AllocateAndInitializeSid( &auth
, 1, 42, 0,0,0,0,0,0,0,&psid
) )
359 trace( "failed to init SID\n" );
363 /* test BuildTrusteeWithSidA */
364 memset( &trustee
, 0xff, sizeof trustee
);
365 pBuildTrusteeWithSidA( &trustee
, psid
);
367 ok( trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
368 ok( trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
,
369 "MultipleTrusteeOperation wrong\n");
370 ok( trustee
.TrusteeForm
== TRUSTEE_IS_SID
, "TrusteeForm wrong\n");
371 ok( trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
372 ok( trustee
.ptstrName
== psid
, "ptstrName wrong\n" );
374 /* test BuildTrusteeWithObjectsAndSidA (test 1) */
375 memset( &trustee
, 0xff, sizeof trustee
);
376 memset( &oas
, 0xff, sizeof(oas
) );
377 pBuildTrusteeWithObjectsAndSidA(&trustee
, &oas
, &ObjectType
,
378 &InheritedObjectType
, psid
);
380 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
381 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
382 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_SID
, "TrusteeForm wrong\n");
383 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
384 ok(trustee
.ptstrName
== (LPSTR
)&oas
, "ptstrName wrong\n");
386 ok(oas
.ObjectsPresent
== (ACE_OBJECT_TYPE_PRESENT
| ACE_INHERITED_OBJECT_TYPE_PRESENT
), "ObjectsPresent wrong\n");
387 ok(!memcmp(&oas
.ObjectTypeGuid
, &ObjectType
, sizeof(GUID
)), "ObjectTypeGuid wrong\n");
388 ok(!memcmp(&oas
.InheritedObjectTypeGuid
, &InheritedObjectType
, sizeof(GUID
)), "InheritedObjectTypeGuid wrong\n");
389 ok(oas
.pSid
== psid
, "pSid wrong\n");
391 /* test GetTrusteeNameA */
392 ok(pGetTrusteeNameA(&trustee
) == (LPSTR
)&oas
, "GetTrusteeName returned wrong value\n");
394 /* test BuildTrusteeWithObjectsAndSidA (test 2) */
395 memset( &trustee
, 0xff, sizeof trustee
);
396 memset( &oas
, 0xff, sizeof(oas
) );
397 pBuildTrusteeWithObjectsAndSidA(&trustee
, &oas
, NULL
,
398 &InheritedObjectType
, psid
);
400 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
401 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
402 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_SID
, "TrusteeForm wrong\n");
403 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
404 ok(trustee
.ptstrName
== (LPSTR
)&oas
, "ptstrName wrong\n");
406 ok(oas
.ObjectsPresent
== ACE_INHERITED_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
407 ok(!memcmp(&oas
.ObjectTypeGuid
, &ZeroGuid
, sizeof(GUID
)), "ObjectTypeGuid wrong\n");
408 ok(!memcmp(&oas
.InheritedObjectTypeGuid
, &InheritedObjectType
, sizeof(GUID
)), "InheritedObjectTypeGuid wrong\n");
409 ok(oas
.pSid
== psid
, "pSid wrong\n");
413 /* test BuildTrusteeWithNameA */
414 memset( &trustee
, 0xff, sizeof trustee
);
415 pBuildTrusteeWithNameA( &trustee
, szTrusteeName
);
417 ok( trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
418 ok( trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
,
419 "MultipleTrusteeOperation wrong\n");
420 ok( trustee
.TrusteeForm
== TRUSTEE_IS_NAME
, "TrusteeForm wrong\n");
421 ok( trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
422 ok( trustee
.ptstrName
== szTrusteeName
, "ptstrName wrong\n" );
424 /* test BuildTrusteeWithObjectsAndNameA (test 1) */
425 memset( &trustee
, 0xff, sizeof trustee
);
426 memset( &oan
, 0xff, sizeof(oan
) );
427 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, szObjectTypeName
,
428 szInheritedObjectTypeName
, szTrusteeName
);
430 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
431 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
432 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
433 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
434 ok(trustee
.ptstrName
== (LPTSTR
)&oan
, "ptstrName wrong\n");
436 ok(oan
.ObjectsPresent
== (ACE_OBJECT_TYPE_PRESENT
| ACE_INHERITED_OBJECT_TYPE_PRESENT
), "ObjectsPresent wrong\n");
437 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
438 ok(oan
.InheritedObjectTypeName
== szInheritedObjectTypeName
, "InheritedObjectTypeName wrong\n");
439 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
441 /* test GetTrusteeNameA */
442 ok(pGetTrusteeNameA(&trustee
) == (LPSTR
)&oan
, "GetTrusteeName returned wrong value\n");
444 /* test BuildTrusteeWithObjectsAndNameA (test 2) */
445 memset( &trustee
, 0xff, sizeof trustee
);
446 memset( &oan
, 0xff, sizeof(oan
) );
447 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, NULL
,
448 szInheritedObjectTypeName
, szTrusteeName
);
450 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
451 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
452 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
453 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
454 ok(trustee
.ptstrName
== (LPSTR
)&oan
, "ptstrName wrong\n");
456 ok(oan
.ObjectsPresent
== ACE_INHERITED_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
457 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
458 ok(oan
.InheritedObjectTypeName
== szInheritedObjectTypeName
, "InheritedObjectTypeName wrong\n");
459 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
461 /* test BuildTrusteeWithObjectsAndNameA (test 3) */
462 memset( &trustee
, 0xff, sizeof trustee
);
463 memset( &oan
, 0xff, sizeof(oan
) );
464 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, szObjectTypeName
,
465 NULL
, szTrusteeName
);
467 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
468 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
469 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
470 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
471 ok(trustee
.ptstrName
== (LPTSTR
)&oan
, "ptstrName wrong\n");
473 ok(oan
.ObjectsPresent
== ACE_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
474 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
475 ok(oan
.InheritedObjectTypeName
== NULL
, "InheritedObjectTypeName wrong\n");
476 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
479 /* If the first isn't defined, assume none is */
480 #ifndef SE_MIN_WELL_KNOWN_PRIVILEGE
481 #define SE_MIN_WELL_KNOWN_PRIVILEGE 2L
482 #define SE_CREATE_TOKEN_PRIVILEGE 2L
483 #define SE_ASSIGNPRIMARYTOKEN_PRIVILEGE 3L
484 #define SE_LOCK_MEMORY_PRIVILEGE 4L
485 #define SE_INCREASE_QUOTA_PRIVILEGE 5L
486 #define SE_MACHINE_ACCOUNT_PRIVILEGE 6L
487 #define SE_TCB_PRIVILEGE 7L
488 #define SE_SECURITY_PRIVILEGE 8L
489 #define SE_TAKE_OWNERSHIP_PRIVILEGE 9L
490 #define SE_LOAD_DRIVER_PRIVILEGE 10L
491 #define SE_SYSTEM_PROFILE_PRIVILEGE 11L
492 #define SE_SYSTEMTIME_PRIVILEGE 12L
493 #define SE_PROF_SINGLE_PROCESS_PRIVILEGE 13L
494 #define SE_INC_BASE_PRIORITY_PRIVILEGE 14L
495 #define SE_CREATE_PAGEFILE_PRIVILEGE 15L
496 #define SE_CREATE_PERMANENT_PRIVILEGE 16L
497 #define SE_BACKUP_PRIVILEGE 17L
498 #define SE_RESTORE_PRIVILEGE 18L
499 #define SE_SHUTDOWN_PRIVILEGE 19L
500 #define SE_DEBUG_PRIVILEGE 20L
501 #define SE_AUDIT_PRIVILEGE 21L
502 #define SE_SYSTEM_ENVIRONMENT_PRIVILEGE 22L
503 #define SE_CHANGE_NOTIFY_PRIVILEGE 23L
504 #define SE_REMOTE_SHUTDOWN_PRIVILEGE 24L
505 #define SE_UNDOCK_PRIVILEGE 25L
506 #define SE_SYNC_AGENT_PRIVILEGE 26L
507 #define SE_ENABLE_DELEGATION_PRIVILEGE 27L
508 #define SE_MANAGE_VOLUME_PRIVILEGE 28L
509 #define SE_IMPERSONATE_PRIVILEGE 29L
510 #define SE_CREATE_GLOBAL_PRIVILEGE 30L
511 #define SE_MAX_WELL_KNOWN_PRIVILEGE SE_CREATE_GLOBAL_PRIVILEGE
512 #endif /* ndef SE_MIN_WELL_KNOWN_PRIVILEGE */
514 static void test_allocateLuid(void)
516 BOOL (WINAPI
*pAllocateLocallyUniqueId
)(PLUID
);
520 pAllocateLocallyUniqueId
= (void*)GetProcAddress(hmod
, "AllocateLocallyUniqueId");
521 if (!pAllocateLocallyUniqueId
) return;
523 ret
= pAllocateLocallyUniqueId(&luid1
);
524 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
528 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
529 ret
= pAllocateLocallyUniqueId(&luid2
);
531 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
532 ok(luid1
.LowPart
> SE_MAX_WELL_KNOWN_PRIVILEGE
|| luid1
.HighPart
!= 0,
533 "AllocateLocallyUniqueId returned a well-known LUID\n");
534 ok(luid1
.LowPart
!= luid2
.LowPart
|| luid1
.HighPart
!= luid2
.HighPart
,
535 "AllocateLocallyUniqueId returned non-unique LUIDs\n");
536 ret
= pAllocateLocallyUniqueId(NULL
);
537 ok( !ret
&& GetLastError() == ERROR_NOACCESS
,
538 "AllocateLocallyUniqueId(NULL) didn't return ERROR_NOACCESS: %d\n",
542 static void test_lookupPrivilegeName(void)
544 BOOL (WINAPI
*pLookupPrivilegeNameA
)(LPCSTR
, PLUID
, LPSTR
, LPDWORD
);
545 char buf
[MAX_PATH
]; /* arbitrary, seems long enough */
546 DWORD cchName
= sizeof(buf
);
547 LUID luid
= { 0, 0 };
551 /* check whether it's available first */
552 pLookupPrivilegeNameA
= (void*)GetProcAddress(hmod
, "LookupPrivilegeNameA");
553 if (!pLookupPrivilegeNameA
) return;
554 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
555 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
556 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
559 /* check with a short buffer */
561 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
562 ret
= pLookupPrivilegeNameA(NULL
, &luid
, NULL
, &cchName
);
563 ok( !ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
564 "LookupPrivilegeNameA didn't fail with ERROR_INSUFFICIENT_BUFFER: %d\n",
566 ok(cchName
== strlen("SeCreateTokenPrivilege") + 1,
567 "LookupPrivilegeNameA returned an incorrect required length for\n"
568 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName
,
569 lstrlenA("SeCreateTokenPrivilege") + 1);
570 /* check a known value and its returned length on success */
571 cchName
= sizeof(buf
);
572 ok(pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
) &&
573 cchName
== strlen("SeCreateTokenPrivilege"),
574 "LookupPrivilegeNameA returned an incorrect output length for\n"
575 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName
,
576 (int)strlen("SeCreateTokenPrivilege"));
577 /* check known values */
578 for (i
= SE_MIN_WELL_KNOWN_PRIVILEGE
; i
<= SE_MAX_WELL_KNOWN_PRIVILEGE
; i
++)
581 cchName
= sizeof(buf
);
582 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
583 ok( ret
|| GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
584 "LookupPrivilegeNameA(0.%d) failed: %d\n", i
, GetLastError());
586 /* check a bogus LUID */
587 luid
.LowPart
= 0xdeadbeef;
588 cchName
= sizeof(buf
);
589 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
590 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
591 "LookupPrivilegeNameA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
593 /* check on a bogus system */
594 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
595 cchName
= sizeof(buf
);
596 ret
= pLookupPrivilegeNameA("b0gu5.Nam3", &luid
, buf
, &cchName
);
597 ok( !ret
&& (GetLastError() == RPC_S_SERVER_UNAVAILABLE
||
598 GetLastError() == RPC_S_INVALID_NET_ADDR
) /* w2k8 */,
599 "LookupPrivilegeNameA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
609 static void test_lookupPrivilegeValue(void)
611 static const struct NameToLUID privs
[] = {
612 { "SeCreateTokenPrivilege", SE_CREATE_TOKEN_PRIVILEGE
},
613 { "SeAssignPrimaryTokenPrivilege", SE_ASSIGNPRIMARYTOKEN_PRIVILEGE
},
614 { "SeLockMemoryPrivilege", SE_LOCK_MEMORY_PRIVILEGE
},
615 { "SeIncreaseQuotaPrivilege", SE_INCREASE_QUOTA_PRIVILEGE
},
616 { "SeMachineAccountPrivilege", SE_MACHINE_ACCOUNT_PRIVILEGE
},
617 { "SeTcbPrivilege", SE_TCB_PRIVILEGE
},
618 { "SeSecurityPrivilege", SE_SECURITY_PRIVILEGE
},
619 { "SeTakeOwnershipPrivilege", SE_TAKE_OWNERSHIP_PRIVILEGE
},
620 { "SeLoadDriverPrivilege", SE_LOAD_DRIVER_PRIVILEGE
},
621 { "SeSystemProfilePrivilege", SE_SYSTEM_PROFILE_PRIVILEGE
},
622 { "SeSystemtimePrivilege", SE_SYSTEMTIME_PRIVILEGE
},
623 { "SeProfileSingleProcessPrivilege", SE_PROF_SINGLE_PROCESS_PRIVILEGE
},
624 { "SeIncreaseBasePriorityPrivilege", SE_INC_BASE_PRIORITY_PRIVILEGE
},
625 { "SeCreatePagefilePrivilege", SE_CREATE_PAGEFILE_PRIVILEGE
},
626 { "SeCreatePermanentPrivilege", SE_CREATE_PERMANENT_PRIVILEGE
},
627 { "SeBackupPrivilege", SE_BACKUP_PRIVILEGE
},
628 { "SeRestorePrivilege", SE_RESTORE_PRIVILEGE
},
629 { "SeShutdownPrivilege", SE_SHUTDOWN_PRIVILEGE
},
630 { "SeDebugPrivilege", SE_DEBUG_PRIVILEGE
},
631 { "SeAuditPrivilege", SE_AUDIT_PRIVILEGE
},
632 { "SeSystemEnvironmentPrivilege", SE_SYSTEM_ENVIRONMENT_PRIVILEGE
},
633 { "SeChangeNotifyPrivilege", SE_CHANGE_NOTIFY_PRIVILEGE
},
634 { "SeRemoteShutdownPrivilege", SE_REMOTE_SHUTDOWN_PRIVILEGE
},
635 { "SeUndockPrivilege", SE_UNDOCK_PRIVILEGE
},
636 { "SeSyncAgentPrivilege", SE_SYNC_AGENT_PRIVILEGE
},
637 { "SeEnableDelegationPrivilege", SE_ENABLE_DELEGATION_PRIVILEGE
},
638 { "SeManageVolumePrivilege", SE_MANAGE_VOLUME_PRIVILEGE
},
639 { "SeImpersonatePrivilege", SE_IMPERSONATE_PRIVILEGE
},
640 { "SeCreateGlobalPrivilege", SE_CREATE_GLOBAL_PRIVILEGE
},
642 BOOL (WINAPI
*pLookupPrivilegeValueA
)(LPCSTR
, LPCSTR
, PLUID
);
647 /* check whether it's available first */
648 pLookupPrivilegeValueA
= (void*)GetProcAddress(hmod
, "LookupPrivilegeValueA");
649 if (!pLookupPrivilegeValueA
) return;
650 ret
= pLookupPrivilegeValueA(NULL
, "SeCreateTokenPrivilege", &luid
);
651 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
654 /* check a bogus system name */
655 ret
= pLookupPrivilegeValueA("b0gu5.Nam3", "SeCreateTokenPrivilege", &luid
);
656 ok( !ret
&& (GetLastError() == RPC_S_SERVER_UNAVAILABLE
||
657 GetLastError() == RPC_S_INVALID_NET_ADDR
) /* w2k8 */,
658 "LookupPrivilegeValueA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
660 /* check a NULL string */
661 ret
= pLookupPrivilegeValueA(NULL
, 0, &luid
);
662 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
663 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
665 /* check a bogus privilege name */
666 ret
= pLookupPrivilegeValueA(NULL
, "SeBogusPrivilege", &luid
);
667 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
668 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
670 /* check case insensitive */
671 ret
= pLookupPrivilegeValueA(NULL
, "sEcREATEtOKENpRIVILEGE", &luid
);
673 "LookupPrivilegeValueA(NULL, sEcREATEtOKENpRIVILEGE, &luid) failed: %d\n",
675 for (i
= 0; i
< sizeof(privs
) / sizeof(privs
[0]); i
++)
677 /* Not all privileges are implemented on all Windows versions, so
678 * don't worry if the call fails
680 if (pLookupPrivilegeValueA(NULL
, privs
[i
].name
, &luid
))
682 ok(luid
.LowPart
== privs
[i
].lowPart
,
683 "LookupPrivilegeValueA returned an invalid LUID for %s\n",
689 static void test_luid(void)
692 test_lookupPrivilegeName();
693 test_lookupPrivilegeValue();
696 static void test_FileSecurity(void)
698 char wintmpdir
[MAX_PATH
];
699 char path
[MAX_PATH
];
700 char file
[MAX_PATH
];
702 DWORD sdSize
, retSize
, rc
, granted
, priv_set_len
;
703 PRIVILEGE_SET priv_set
;
706 GENERIC_MAPPING mapping
= { FILE_READ_DATA
, FILE_WRITE_DATA
, FILE_EXECUTE
, FILE_ALL_ACCESS
};
707 const SECURITY_INFORMATION request
= OWNER_SECURITY_INFORMATION
708 | GROUP_SECURITY_INFORMATION
709 | DACL_SECURITY_INFORMATION
;
711 if (!pGetFileSecurityA
) {
712 win_skip ("GetFileSecurity is not available\n");
716 if (!pSetFileSecurityA
) {
717 win_skip ("SetFileSecurity is not available\n");
721 if (!GetTempPathA (sizeof (wintmpdir
), wintmpdir
)) {
722 win_skip ("GetTempPathA failed\n");
726 /* Create a temporary directory and in it a temporary file */
727 strcat (strcpy (path
, wintmpdir
), "rary");
728 SetLastError(0xdeadbeef);
729 rc
= CreateDirectoryA (path
, NULL
);
730 ok (rc
|| GetLastError() == ERROR_ALREADY_EXISTS
, "CreateDirectoryA "
731 "failed for '%s' with %d\n", path
, GetLastError());
733 strcat (strcpy (file
, path
), "\\ess");
734 SetLastError(0xdeadbeef);
735 fh
= CreateFileA (file
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, NULL
);
736 ok (fh
!= INVALID_HANDLE_VALUE
, "CreateFileA "
737 "failed for '%s' with %d\n", file
, GetLastError());
740 /* For the temporary file ... */
742 /* Get size needed */
744 SetLastError(0xdeadbeef);
745 rc
= pGetFileSecurityA (file
, request
, NULL
, 0, &retSize
);
746 if (!rc
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)) {
747 win_skip("GetFileSecurityA is not implemented\n");
750 ok (!rc
, "GetFileSecurityA "
751 "was expected to fail for '%s'\n", file
);
752 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetFileSecurityA "
753 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
754 ok (retSize
> sizeof (SECURITY_DESCRIPTOR
), "GetFileSecurityA returned size %d\n", retSize
);
757 sd
= HeapAlloc (GetProcessHeap (), 0, sdSize
);
759 /* Get security descriptor for real */
761 SetLastError(0xdeadbeef);
762 rc
= pGetFileSecurityA (file
, request
, sd
, sdSize
, &retSize
);
763 ok (rc
, "GetFileSecurityA "
764 "was not expected to fail '%s': %d\n", file
, GetLastError());
765 ok (retSize
== sdSize
||
766 broken(retSize
== 0), /* NT4 */
767 "GetFileSecurityA returned size %d; expected %d\n", retSize
, sdSize
);
769 /* Use it to set security descriptor */
770 SetLastError(0xdeadbeef);
771 rc
= pSetFileSecurityA (file
, request
, sd
);
772 ok (rc
, "SetFileSecurityA "
773 "was not expected to fail '%s': %d\n", file
, GetLastError());
775 HeapFree (GetProcessHeap (), 0, sd
);
777 /* Repeat for the temporary directory ... */
779 /* Get size needed */
781 SetLastError(0xdeadbeef);
782 rc
= pGetFileSecurityA (path
, request
, NULL
, 0, &retSize
);
783 ok (!rc
, "GetFileSecurityA "
784 "was expected to fail for '%s'\n", path
);
785 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetFileSecurityA "
786 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
787 ok (retSize
> sizeof (SECURITY_DESCRIPTOR
), "GetFileSecurityA returned size %d\n", retSize
);
790 sd
= HeapAlloc (GetProcessHeap (), 0, sdSize
);
792 /* Get security descriptor for real */
794 SetLastError(0xdeadbeef);
795 rc
= pGetFileSecurityA (path
, request
, sd
, sdSize
, &retSize
);
796 ok (rc
, "GetFileSecurityA "
797 "was not expected to fail '%s': %d\n", path
, GetLastError());
798 ok (retSize
== sdSize
||
799 broken(retSize
== 0), /* NT4 */
800 "GetFileSecurityA returned size %d; expected %d\n", retSize
, sdSize
);
802 /* Use it to set security descriptor */
803 SetLastError(0xdeadbeef);
804 rc
= pSetFileSecurityA (path
, request
, sd
);
805 ok (rc
, "SetFileSecurityA "
806 "was not expected to fail '%s': %d\n", path
, GetLastError());
807 HeapFree (GetProcessHeap (), 0, sd
);
810 strcpy (wintmpdir
, "\\Should not exist");
811 SetLastError(0xdeadbeef);
812 rc
= pGetFileSecurityA (wintmpdir
, OWNER_SECURITY_INFORMATION
, NULL
, 0, &sdSize
);
813 ok (!rc
, "GetFileSecurityA should fail for not existing directories/files\n");
814 ok (GetLastError() == ERROR_FILE_NOT_FOUND
,
815 "last error ERROR_FILE_NOT_FOUND expected, got %d\n", GetLastError());
818 /* Remove temporary file and directory */
820 RemoveDirectoryA(path
);
822 /* Test file access permissions for a file with FILE_ATTRIBUTE_ARCHIVE */
823 SetLastError(0xdeadbeef);
824 rc
= GetTempPath(sizeof(wintmpdir
), wintmpdir
);
825 ok(rc
, "GetTempPath error %d\n", GetLastError());
827 SetLastError(0xdeadbeef);
828 rc
= GetTempFileName(wintmpdir
, "tmp", 0, file
);
829 ok(rc
, "GetTempFileName error %d\n", GetLastError());
831 rc
= GetFileAttributes(file
);
832 rc
&= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
;
833 ok(rc
== FILE_ATTRIBUTE_ARCHIVE
, "expected FILE_ATTRIBUTE_ARCHIVE got %#x\n", rc
);
835 retSize
= 0xdeadbeef;
836 rc
= GetFileSecurity(file
, OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
838 ok(!rc
, "GetFileSecurity should fail\n");
839 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
840 "expected ERROR_INSUFFICIENT_BUFFER got %d\n", GetLastError());
841 ok(sdSize
> sizeof(SECURITY_DESCRIPTOR
), "got sd size %d\n", sdSize
);
843 sd
= HeapAlloc(GetProcessHeap (), 0, sdSize
);
844 retSize
= 0xdeadbeef;
845 SetLastError(0xdeadbeef);
846 rc
= GetFileSecurity(file
, OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
847 sd
, sdSize
, &retSize
);
848 ok(rc
, "GetFileSecurity error %d\n", GetLastError());
849 ok(retSize
== sdSize
|| broken(retSize
== 0) /* NT4 */, "expected %d, got %d\n", sdSize
, retSize
);
851 SetLastError(0xdeadbeef);
852 rc
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
, TRUE
, &token
);
853 ok(!rc
, "OpenThreadToken should fail\n");
854 ok(GetLastError() == ERROR_NO_TOKEN
, "expected ERROR_NO_TOKEN, got %d\n", GetLastError());
856 SetLastError(0xdeadbeef);
857 rc
= ImpersonateSelf(SecurityIdentification
);
858 ok(rc
, "ImpersonateSelf error %d\n", GetLastError());
860 SetLastError(0xdeadbeef);
861 rc
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
, TRUE
, &token
);
862 ok(rc
, "OpenThreadToken error %d\n", GetLastError());
864 SetLastError(0xdeadbeef);
866 ok(rc
, "RevertToSelf error %d\n", GetLastError());
868 priv_set_len
= sizeof(priv_set
);
869 granted
= 0xdeadbeef;
871 SetLastError(0xdeadbeef);
872 rc
= AccessCheck(sd
, token
, FILE_READ_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
873 ok(rc
, "AccessCheck error %d\n", GetLastError());
874 ok(status
== 1, "expected 1, got %d\n", status
);
875 ok(granted
== FILE_READ_DATA
, "expected FILE_READ_DATA, got %#x\n", granted
);
877 granted
= 0xdeadbeef;
879 SetLastError(0xdeadbeef);
880 rc
= AccessCheck(sd
, token
, FILE_WRITE_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
881 ok(rc
, "AccessCheck error %d\n", GetLastError());
882 ok(status
== 1, "expected 1, got %d\n", status
);
883 ok(granted
== FILE_WRITE_DATA
, "expected FILE_WRITE_DATA, got %#x\n", granted
);
885 granted
= 0xdeadbeef;
887 SetLastError(0xdeadbeef);
888 rc
= AccessCheck(sd
, token
, FILE_EXECUTE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
889 ok(rc
, "AccessCheck error %d\n", GetLastError());
890 ok(status
== 1, "expected 1, got %d\n", status
);
891 ok(granted
== FILE_EXECUTE
, "expected FILE_EXECUTE, got %#x\n", granted
);
893 granted
= 0xdeadbeef;
895 SetLastError(0xdeadbeef);
896 rc
= AccessCheck(sd
, token
, DELETE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
897 ok(rc
, "AccessCheck error %d\n", GetLastError());
898 ok(status
== 1, "expected 1, got %d\n", status
);
899 ok(granted
== DELETE
, "expected DELETE, got %#x\n", granted
);
901 granted
= 0xdeadbeef;
903 SetLastError(0xdeadbeef);
904 rc
= AccessCheck(sd
, token
, FILE_DELETE_CHILD
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
905 ok(rc
, "AccessCheck error %d\n", GetLastError());
906 ok(status
== 1, "expected 1, got %d\n", status
);
907 ok(granted
== FILE_DELETE_CHILD
, "expected FILE_DELETE_CHILD, got %#x\n", granted
);
909 granted
= 0xdeadbeef;
911 SetLastError(0xdeadbeef);
912 rc
= AccessCheck(sd
, token
, 0x1ff, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
913 ok(rc
, "AccessCheck error %d\n", GetLastError());
914 ok(status
== 1, "expected 1, got %d\n", status
);
915 ok(granted
== 0x1ff, "expected 0x1ff, got %#x\n", granted
);
917 granted
= 0xdeadbeef;
919 SetLastError(0xdeadbeef);
920 rc
= AccessCheck(sd
, token
, FILE_ALL_ACCESS
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
921 ok(rc
, "AccessCheck error %d\n", GetLastError());
922 ok(status
== 1, "expected 1, got %d\n", status
);
923 ok(granted
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", granted
);
925 SetLastError(0xdeadbeef);
926 rc
= AccessCheck(sd
, token
, 0xffffffff, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
927 ok(!rc
, "AccessCheck should fail\n");
928 ok(GetLastError() == ERROR_GENERIC_NOT_MAPPED
, "expected ERROR_GENERIC_NOT_MAPPED, got %d\n", GetLastError());
930 /* Test file access permissions for a file with FILE_ATTRIBUTE_READONLY */
931 SetLastError(0xdeadbeef);
932 fh
= CreateFile(file
, FILE_READ_DATA
, FILE_SHARE_READ
, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_READONLY
, 0);
933 ok(fh
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
934 retSize
= 0xdeadbeef;
935 SetLastError(0xdeadbeef);
936 rc
= WriteFile(fh
, "1", 1, &retSize
, NULL
);
937 ok(!rc
, "WriteFile should fail\n");
938 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
939 ok(retSize
== 0, "expected 0, got %d\n", retSize
);
942 rc
= GetFileAttributes(file
);
943 rc
&= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
;
945 ok(rc
== (FILE_ATTRIBUTE_ARCHIVE
|FILE_ATTRIBUTE_READONLY
),
946 "expected FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY got %#x\n", rc
);
948 SetLastError(0xdeadbeef);
949 rc
= SetFileAttributes(file
, FILE_ATTRIBUTE_ARCHIVE
);
950 ok(rc
, "SetFileAttributes error %d\n", GetLastError());
951 SetLastError(0xdeadbeef);
952 rc
= DeleteFile(file
);
953 ok(rc
, "DeleteFile error %d\n", GetLastError());
955 SetLastError(0xdeadbeef);
956 fh
= CreateFile(file
, FILE_READ_DATA
, FILE_SHARE_READ
, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_READONLY
, 0);
957 ok(fh
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
958 retSize
= 0xdeadbeef;
959 SetLastError(0xdeadbeef);
960 rc
= WriteFile(fh
, "1", 1, &retSize
, NULL
);
961 ok(!rc
, "WriteFile should fail\n");
962 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
963 ok(retSize
== 0, "expected 0, got %d\n", retSize
);
966 rc
= GetFileAttributes(file
);
967 rc
&= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
;
968 ok(rc
== (FILE_ATTRIBUTE_ARCHIVE
|FILE_ATTRIBUTE_READONLY
),
969 "expected FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY got %#x\n", rc
);
971 retSize
= 0xdeadbeef;
972 SetLastError(0xdeadbeef);
973 rc
= GetFileSecurity(file
, OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
974 sd
, sdSize
, &retSize
);
975 ok(rc
, "GetFileSecurity error %d\n", GetLastError());
976 ok(retSize
== sdSize
|| broken(retSize
== 0) /* NT4 */, "expected %d, got %d\n", sdSize
, retSize
);
978 priv_set_len
= sizeof(priv_set
);
979 granted
= 0xdeadbeef;
981 SetLastError(0xdeadbeef);
982 rc
= AccessCheck(sd
, token
, FILE_READ_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
983 ok(rc
, "AccessCheck error %d\n", GetLastError());
984 ok(status
== 1, "expected 1, got %d\n", status
);
985 ok(granted
== FILE_READ_DATA
, "expected FILE_READ_DATA, got %#x\n", granted
);
987 granted
= 0xdeadbeef;
989 SetLastError(0xdeadbeef);
990 rc
= AccessCheck(sd
, token
, FILE_WRITE_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
991 ok(rc
, "AccessCheck error %d\n", GetLastError());
993 ok(status
== 1, "expected 1, got %d\n", status
);
994 ok(granted
== FILE_WRITE_DATA
, "expected FILE_WRITE_DATA, got %#x\n", granted
);
996 granted
= 0xdeadbeef;
998 SetLastError(0xdeadbeef);
999 rc
= AccessCheck(sd
, token
, FILE_EXECUTE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1000 ok(rc
, "AccessCheck error %d\n", GetLastError());
1001 ok(status
== 1, "expected 1, got %d\n", status
);
1002 ok(granted
== FILE_EXECUTE
, "expected FILE_EXECUTE, got %#x\n", granted
);
1004 granted
= 0xdeadbeef;
1005 status
= 0xdeadbeef;
1006 SetLastError(0xdeadbeef);
1007 rc
= AccessCheck(sd
, token
, DELETE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1008 ok(rc
, "AccessCheck error %d\n", GetLastError());
1010 ok(status
== 1, "expected 1, got %d\n", status
);
1011 ok(granted
== DELETE
, "expected DELETE, got %#x\n", granted
);
1013 granted
= 0xdeadbeef;
1014 status
= 0xdeadbeef;
1015 SetLastError(0xdeadbeef);
1016 rc
= AccessCheck(sd
, token
, FILE_DELETE_CHILD
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1017 ok(rc
, "AccessCheck error %d\n", GetLastError());
1019 ok(status
== 1, "expected 1, got %d\n", status
);
1020 ok(granted
== FILE_DELETE_CHILD
, "expected FILE_DELETE_CHILD, got %#x\n", granted
);
1022 granted
= 0xdeadbeef;
1023 status
= 0xdeadbeef;
1024 SetLastError(0xdeadbeef);
1025 rc
= AccessCheck(sd
, token
, 0x1ff, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1026 ok(rc
, "AccessCheck error %d\n", GetLastError());
1028 ok(status
== 1, "expected 1, got %d\n", status
);
1029 ok(granted
== 0x1ff, "expected 0x1ff, got %#x\n", granted
);
1031 granted
= 0xdeadbeef;
1032 status
= 0xdeadbeef;
1033 SetLastError(0xdeadbeef);
1034 rc
= AccessCheck(sd
, token
, FILE_ALL_ACCESS
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1035 ok(rc
, "AccessCheck error %d\n", GetLastError());
1037 ok(status
== 1, "expected 1, got %d\n", status
);
1038 ok(granted
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", granted
);
1040 SetLastError(0xdeadbeef);
1041 rc
= DeleteFile(file
);
1042 ok(!rc
, "DeleteFile should fail\n");
1043 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1044 SetLastError(0xdeadbeef);
1045 rc
= SetFileAttributes(file
, FILE_ATTRIBUTE_ARCHIVE
);
1046 ok(rc
, "SetFileAttributes error %d\n", GetLastError());
1047 SetLastError(0xdeadbeef);
1048 rc
= DeleteFile(file
);
1049 ok(rc
, "DeleteFile error %d\n", GetLastError());
1052 HeapFree(GetProcessHeap(), 0, sd
);
1055 static void test_AccessCheck(void)
1057 PSID EveryoneSid
= NULL
, AdminSid
= NULL
, UsersSid
= NULL
;
1059 SECURITY_DESCRIPTOR
*SecurityDescriptor
= NULL
;
1060 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
1061 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
1062 GENERIC_MAPPING Mapping
= { KEY_READ
, KEY_WRITE
, KEY_EXECUTE
, KEY_ALL_ACCESS
};
1066 HANDLE ProcessToken
;
1069 PRIVILEGE_SET
*PrivSet
;
1071 HMODULE NtDllModule
;
1074 NTSTATUS ntret
, ntAccessStatus
;
1076 NtDllModule
= GetModuleHandle("ntdll.dll");
1079 skip("not running on NT, skipping test\n");
1082 pRtlAdjustPrivilege
= (void *)GetProcAddress(NtDllModule
, "RtlAdjustPrivilege");
1083 if (!pRtlAdjustPrivilege
)
1085 win_skip("missing RtlAdjustPrivilege, skipping test\n");
1089 Acl
= HeapAlloc(GetProcessHeap(), 0, 256);
1090 res
= InitializeAcl(Acl
, 256, ACL_REVISION
);
1091 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
1093 skip("ACLs not implemented - skipping tests\n");
1094 HeapFree(GetProcessHeap(), 0, Acl
);
1097 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
1099 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
1100 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1102 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1103 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &AdminSid
);
1104 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1106 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1107 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
1108 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1110 SecurityDescriptor
= HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
1112 res
= InitializeSecurityDescriptor(SecurityDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
1113 ok(res
, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
1115 res
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
1116 ok(res
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1118 PrivSetLen
= FIELD_OFFSET(PRIVILEGE_SET
, Privilege
[16]);
1119 PrivSet
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, PrivSetLen
);
1120 PrivSet
->PrivilegeCount
= 16;
1122 res
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &ProcessToken
);
1123 ok(res
, "OpenProcessToken failed with error %d\n", GetLastError());
1125 pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE
, FALSE
, TRUE
, &Enabled
);
1127 res
= DuplicateToken(ProcessToken
, SecurityImpersonation
, &Token
);
1128 ok(res
, "DuplicateToken failed with error %d\n", GetLastError());
1130 /* SD without owner/group */
1131 SetLastError(0xdeadbeef);
1132 Access
= AccessStatus
= 0x1abe11ed;
1133 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_QUERY_VALUE
, &Mapping
,
1134 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1135 err
= GetLastError();
1136 ok(!ret
&& err
== ERROR_INVALID_SECURITY_DESCR
, "AccessCheck should have "
1137 "failed with ERROR_INVALID_SECURITY_DESCR, instead of %d\n", err
);
1138 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1139 "Access and/or AccessStatus were changed!\n");
1141 /* Set owner and group */
1142 res
= SetSecurityDescriptorOwner(SecurityDescriptor
, AdminSid
, FALSE
);
1143 ok(res
, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
1144 res
= SetSecurityDescriptorGroup(SecurityDescriptor
, UsersSid
, TRUE
);
1145 ok(res
, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
1147 /* Generic access mask */
1148 SetLastError(0xdeadbeef);
1149 Access
= AccessStatus
= 0x1abe11ed;
1150 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1151 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1152 err
= GetLastError();
1153 ok(!ret
&& err
== ERROR_GENERIC_NOT_MAPPED
, "AccessCheck should have failed "
1154 "with ERROR_GENERIC_NOT_MAPPED, instead of %d\n", err
);
1155 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1156 "Access and/or AccessStatus were changed!\n");
1158 /* Generic access mask - no privilegeset buffer */
1159 SetLastError(0xdeadbeef);
1160 Access
= AccessStatus
= 0x1abe11ed;
1161 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1162 NULL
, &PrivSetLen
, &Access
, &AccessStatus
);
1163 err
= GetLastError();
1164 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1165 "with ERROR_NOACCESS, instead of %d\n", err
);
1166 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1167 "Access and/or AccessStatus were changed!\n");
1169 /* Generic access mask - no returnlength */
1170 SetLastError(0xdeadbeef);
1171 Access
= AccessStatus
= 0x1abe11ed;
1172 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1173 PrivSet
, NULL
, &Access
, &AccessStatus
);
1174 err
= GetLastError();
1175 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1176 "with ERROR_NOACCESS, instead of %d\n", err
);
1177 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1178 "Access and/or AccessStatus were changed!\n");
1180 /* Generic access mask - no privilegeset buffer, no returnlength */
1181 SetLastError(0xdeadbeef);
1182 Access
= AccessStatus
= 0x1abe11ed;
1183 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1184 NULL
, NULL
, &Access
, &AccessStatus
);
1185 err
= GetLastError();
1186 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1187 "with ERROR_NOACCESS, instead of %d\n", err
);
1188 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1189 "Access and/or AccessStatus were changed!\n");
1191 /* sd with no dacl present */
1192 Access
= AccessStatus
= 0x1abe11ed;
1193 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, FALSE
, NULL
, FALSE
);
1194 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1195 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1196 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1197 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1198 ok(AccessStatus
&& (Access
== KEY_READ
),
1199 "AccessCheck failed to grant access with error %d\n",
1202 /* sd with no dacl present - no privilegeset buffer */
1203 SetLastError(0xdeadbeef);
1204 Access
= AccessStatus
= 0x1abe11ed;
1205 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1206 NULL
, &PrivSetLen
, &Access
, &AccessStatus
);
1207 err
= GetLastError();
1208 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1209 "with ERROR_NOACCESS, instead of %d\n", err
);
1210 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1211 "Access and/or AccessStatus were changed!\n");
1215 /* Generic access mask - no privilegeset buffer */
1216 SetLastError(0xdeadbeef);
1217 Access
= ntAccessStatus
= 0x1abe11ed;
1218 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1219 NULL
, &PrivSetLen
, &Access
, &ntAccessStatus
);
1220 err
= GetLastError();
1221 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1222 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1223 ok(err
== 0xdeadbeef,
1224 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1225 ok(Access
== 0x1abe11ed && ntAccessStatus
== 0x1abe11ed,
1226 "Access and/or AccessStatus were changed!\n");
1228 /* Generic access mask - no returnlength */
1229 SetLastError(0xdeadbeef);
1230 Access
= ntAccessStatus
= 0x1abe11ed;
1231 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1232 PrivSet
, NULL
, &Access
, &ntAccessStatus
);
1233 err
= GetLastError();
1234 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1235 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1236 ok(err
== 0xdeadbeef,
1237 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1238 ok(Access
== 0x1abe11ed && ntAccessStatus
== 0x1abe11ed,
1239 "Access and/or AccessStatus were changed!\n");
1241 /* Generic access mask - no privilegeset buffer, no returnlength */
1242 SetLastError(0xdeadbeef);
1243 Access
= ntAccessStatus
= 0x1abe11ed;
1244 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1245 NULL
, NULL
, &Access
, &ntAccessStatus
);
1246 err
= GetLastError();
1247 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1248 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1249 ok(err
== 0xdeadbeef,
1250 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1251 ok(Access
== 0x1abe11ed && ntAccessStatus
== 0x1abe11ed,
1252 "Access and/or AccessStatus were changed!\n");
1255 win_skip("NtAccessCheck unavailable. Skipping.\n");
1257 /* sd with NULL dacl */
1258 Access
= AccessStatus
= 0x1abe11ed;
1259 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, NULL
, FALSE
);
1260 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1261 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1262 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1263 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1264 ok(AccessStatus
&& (Access
== KEY_READ
),
1265 "AccessCheck failed to grant access with error %d\n",
1268 /* sd with blank dacl */
1269 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
1270 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1271 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1272 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1273 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1274 err
= GetLastError();
1275 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1276 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1277 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1279 res
= AddAccessAllowedAce(Acl
, ACL_REVISION
, KEY_READ
, EveryoneSid
);
1280 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
1282 res
= AddAccessDeniedAce(Acl
, ACL_REVISION
, KEY_SET_VALUE
, AdminSid
);
1283 ok(res
, "AddAccessDeniedAce failed with error %d\n", GetLastError());
1286 Access
= AccessStatus
= 0x1abe11ed;
1287 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1288 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1289 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1290 ok(AccessStatus
&& (Access
== KEY_READ
),
1291 "AccessCheck failed to grant access with error %d\n",
1294 ret
= AccessCheck(SecurityDescriptor
, Token
, MAXIMUM_ALLOWED
, &Mapping
,
1295 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1296 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1298 "AccessCheck failed to grant any access with error %d\n",
1300 trace("AccessCheck with MAXIMUM_ALLOWED got Access 0x%08x\n", Access
);
1302 /* Access denied by SD */
1303 SetLastError(0xdeadbeef);
1304 Access
= AccessStatus
= 0x1abe11ed;
1305 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_WRITE
, &Mapping
,
1306 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1307 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1308 err
= GetLastError();
1309 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1310 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1311 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1314 PrivSet
->PrivilegeCount
= 16;
1315 ret
= AccessCheck(SecurityDescriptor
, Token
, ACCESS_SYSTEM_SECURITY
, &Mapping
,
1316 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1317 ok(ret
&& !AccessStatus
&& GetLastError() == ERROR_PRIVILEGE_NOT_HELD
,
1318 "AccessCheck should have failed with ERROR_PRIVILEGE_NOT_HELD, instead of %d\n",
1321 ret
= ImpersonateLoggedOnUser(Token
);
1322 ok(ret
, "ImpersonateLoggedOnUser failed with error %d\n", GetLastError());
1323 ret
= pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE
, TRUE
, TRUE
, &Enabled
);
1327 PrivSet
->PrivilegeCount
= 16;
1328 ret
= AccessCheck(SecurityDescriptor
, Token
, ACCESS_SYSTEM_SECURITY
, &Mapping
,
1329 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1330 ok(ret
&& AccessStatus
&& GetLastError() == 0,
1331 "AccessCheck should have succeeded, error %d\n",
1333 ok(Access
== ACCESS_SYSTEM_SECURITY
,
1334 "Access should be equal to ACCESS_SYSTEM_SECURITY instead of 0x%08x\n",
1338 trace("Couldn't get SE_SECURITY_PRIVILEGE (0x%08x), skipping ACCESS_SYSTEM_SECURITY test\n",
1340 ret
= RevertToSelf();
1341 ok(ret
, "RevertToSelf failed with error %d\n", GetLastError());
1343 /* test INHERIT_ONLY_ACE */
1344 ret
= InitializeAcl(Acl
, 256, ACL_REVISION
);
1345 ok(ret
, "InitializeAcl failed with error %d\n", GetLastError());
1347 /* NT doesn't have AddAccessAllowedAceEx. Skipping this call/test doesn't influence
1350 if (pAddAccessAllowedAceEx
)
1352 ret
= pAddAccessAllowedAceEx(Acl
, ACL_REVISION
, INHERIT_ONLY_ACE
, KEY_READ
, EveryoneSid
);
1353 ok(ret
, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
1356 win_skip("AddAccessAllowedAceEx is not available\n");
1358 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1359 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1360 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1361 err
= GetLastError();
1362 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1363 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1364 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1368 res
= DuplicateToken(ProcessToken
, SecurityAnonymous
, &Token
);
1369 ok(res
, "DuplicateToken failed with error %d\n", GetLastError());
1371 SetLastError(0xdeadbeef);
1372 ret
= AccessCheck(SecurityDescriptor
, Token
, MAXIMUM_ALLOWED
, &Mapping
,
1373 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1374 err
= GetLastError();
1375 ok(!ret
&& err
== ERROR_BAD_IMPERSONATION_LEVEL
, "AccessCheck should have failed "
1376 "with ERROR_BAD_IMPERSONATION_LEVEL, instead of %d\n", err
);
1380 SetLastError(0xdeadbeef);
1381 ret
= AccessCheck(SecurityDescriptor
, ProcessToken
, KEY_READ
, &Mapping
,
1382 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1383 err
= GetLastError();
1384 ok(!ret
&& err
== ERROR_NO_IMPERSONATION_TOKEN
, "AccessCheck should have failed "
1385 "with ERROR_NO_IMPERSONATION_TOKEN, instead of %d\n", err
);
1387 CloseHandle(ProcessToken
);
1390 FreeSid(EveryoneSid
);
1395 HeapFree(GetProcessHeap(), 0, Acl
);
1396 HeapFree(GetProcessHeap(), 0, SecurityDescriptor
);
1397 HeapFree(GetProcessHeap(), 0, PrivSet
);
1400 /* test GetTokenInformation for the various attributes */
1401 static void test_token_attr(void)
1403 HANDLE Token
, ImpersonationToken
;
1405 TOKEN_PRIVILEGES
*Privileges
;
1406 TOKEN_GROUPS
*Groups
;
1408 TOKEN_DEFAULT_DACL
*Dacl
;
1412 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
;
1415 /* cygwin-like use case */
1416 SetLastError(0xdeadbeef);
1417 ret
= OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED
, &Token
);
1418 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
1420 win_skip("OpenProcessToken is not implemented\n");
1423 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1426 DWORD buf
[256]; /* GetTokenInformation wants a dword-aligned buffer */
1428 ret
= GetTokenInformation(Token
, TokenUser
,(void*)buf
, Size
, &Size
);
1429 ok(ret
, "GetTokenInformation failed with error %d\n", GetLastError());
1430 Size
= sizeof(ImpersonationLevel
);
1431 ret
= GetTokenInformation(Token
, TokenImpersonationLevel
, &ImpersonationLevel
, Size
, &Size
);
1432 GLE
= GetLastError();
1433 ok(!ret
&& (GLE
== ERROR_INVALID_PARAMETER
), "GetTokenInformation(TokenImpersonationLevel) on primary token should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GLE
);
1437 if(!pConvertSidToStringSidA
)
1439 win_skip("ConvertSidToStringSidA is not available\n");
1443 SetLastError(0xdeadbeef);
1444 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS
, &Token
);
1445 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1448 /* insufficient buffer length */
1449 SetLastError(0xdeadbeef);
1451 ret
= GetTokenInformation(Token
, TokenGroups
, NULL
, 0, &Size2
);
1452 ok(Size2
> 1, "got %d\n", Size2
);
1453 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1454 "%d with error %d\n", ret
, GetLastError());
1456 Groups
= HeapAlloc(GetProcessHeap(), 0, Size2
);
1457 memset(Groups
, 0xcc, Size2
);
1459 ret
= GetTokenInformation(Token
, TokenGroups
, Groups
, Size2
, &Size
);
1460 ok(Size
> 1, "got %d\n", Size
);
1461 ok((!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
) || broken(ret
) /* wow64 */,
1462 "%d with error %d\n", ret
, GetLastError());
1464 ok(*((BYTE
*)Groups
) == 0xcc, "buffer altered\n");
1466 HeapFree(GetProcessHeap(), 0, Groups
);
1468 SetLastError(0xdeadbeef);
1469 ret
= GetTokenInformation(Token
, TokenGroups
, NULL
, 0, &Size
);
1470 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1471 "GetTokenInformation(TokenGroups) %s with error %d\n",
1472 ret
? "succeeded" : "failed", GetLastError());
1473 Groups
= HeapAlloc(GetProcessHeap(), 0, Size
);
1474 SetLastError(0xdeadbeef);
1475 ret
= GetTokenInformation(Token
, TokenGroups
, Groups
, Size
, &Size
);
1476 ok(ret
, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
1477 ok(GetLastError() == 0xdeadbeef,
1478 "GetTokenInformation shouldn't have set last error to %d\n",
1480 trace("TokenGroups:\n");
1481 for (i
= 0; i
< Groups
->GroupCount
; i
++)
1483 DWORD NameLength
= 255;
1485 DWORD DomainLength
= 255;
1487 SID_NAME_USE SidNameUse
;
1490 ret
= LookupAccountSid(NULL
, Groups
->Groups
[i
].Sid
, Name
, &NameLength
, Domain
, &DomainLength
, &SidNameUse
);
1493 pConvertSidToStringSidA(Groups
->Groups
[i
].Sid
, &SidString
);
1494 trace("%s, %s\\%s use: %d attr: 0x%08x\n", SidString
, Domain
, Name
, SidNameUse
, Groups
->Groups
[i
].Attributes
);
1495 LocalFree(SidString
);
1497 else trace("attr: 0x%08x LookupAccountSid failed with error %d\n", Groups
->Groups
[i
].Attributes
, GetLastError());
1499 HeapFree(GetProcessHeap(), 0, Groups
);
1502 ret
= GetTokenInformation(Token
, TokenUser
, NULL
, 0, &Size
);
1503 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1504 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1505 User
= HeapAlloc(GetProcessHeap(), 0, Size
);
1506 ret
= GetTokenInformation(Token
, TokenUser
, User
, Size
, &Size
);
1508 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1510 pConvertSidToStringSidA(User
->User
.Sid
, &SidString
);
1511 trace("TokenUser: %s attr: 0x%08x\n", SidString
, User
->User
.Attributes
);
1512 LocalFree(SidString
);
1513 HeapFree(GetProcessHeap(), 0, User
);
1516 ret
= GetTokenInformation(Token
, TokenPrivileges
, NULL
, 0, &Size
);
1517 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1518 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1519 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
1520 ret
= GetTokenInformation(Token
, TokenPrivileges
, Privileges
, Size
, &Size
);
1522 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1523 trace("TokenPrivileges:\n");
1524 for (i
= 0; i
< Privileges
->PrivilegeCount
; i
++)
1527 DWORD NameLen
= sizeof(Name
)/sizeof(Name
[0]);
1528 LookupPrivilegeName(NULL
, &Privileges
->Privileges
[i
].Luid
, Name
, &NameLen
);
1529 trace("\t%s, 0x%x\n", Name
, Privileges
->Privileges
[i
].Attributes
);
1531 HeapFree(GetProcessHeap(), 0, Privileges
);
1533 ret
= DuplicateToken(Token
, SecurityAnonymous
, &ImpersonationToken
);
1534 ok(ret
, "DuplicateToken failed with error %d\n", GetLastError());
1536 Size
= sizeof(ImpersonationLevel
);
1537 ret
= GetTokenInformation(ImpersonationToken
, TokenImpersonationLevel
, &ImpersonationLevel
, Size
, &Size
);
1538 ok(ret
, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1539 ok(ImpersonationLevel
== SecurityAnonymous
, "ImpersonationLevel should have been SecurityAnonymous instead of %d\n", ImpersonationLevel
);
1541 CloseHandle(ImpersonationToken
);
1544 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, NULL
, 0, &Size
);
1545 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1546 "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1548 Dacl
= HeapAlloc(GetProcessHeap(), 0, Size
);
1549 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size
);
1550 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1552 SetLastError(0xdeadbeef);
1553 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, NULL
, 0);
1554 GLE
= GetLastError();
1555 ok(!ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1556 ok(GLE
== ERROR_BAD_LENGTH
, "expected ERROR_BAD_LENGTH got %u\n", GLE
);
1558 SetLastError(0xdeadbeef);
1559 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, NULL
, Size
);
1560 GLE
= GetLastError();
1561 ok(!ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1562 ok(GLE
== ERROR_NOACCESS
, "expected ERROR_NOACCESS got %u\n", GLE
);
1564 acl
= Dacl
->DefaultDacl
;
1565 Dacl
->DefaultDacl
= NULL
;
1567 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
);
1568 ok(ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1571 Dacl
->DefaultDacl
= (ACL
*)0xdeadbeef;
1572 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size2
);
1573 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1574 ok(Dacl
->DefaultDacl
== NULL
, "expected NULL, got %p\n", Dacl
->DefaultDacl
);
1575 ok(Size2
== sizeof(TOKEN_DEFAULT_DACL
) || broken(Size2
== 2*sizeof(TOKEN_DEFAULT_DACL
)), /* WoW64 */
1576 "got %u expected sizeof(TOKEN_DEFAULT_DACL)\n", Size2
);
1578 Dacl
->DefaultDacl
= acl
;
1579 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
);
1580 ok(ret
, "SetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1582 if (Size2
== sizeof(TOKEN_DEFAULT_DACL
)) {
1583 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size2
);
1584 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1586 win_skip("TOKEN_DEFAULT_DACL size too small on WoW64\n");
1588 HeapFree(GetProcessHeap(), 0, Dacl
);
1592 typedef union _MAX_SID
1595 char max
[SECURITY_MAX_SID_SIZE
];
1598 static void test_sid_str(PSID
* sid
)
1601 BOOL ret
= pConvertSidToStringSidA(sid
, &str_sid
);
1602 ok(ret
, "ConvertSidToStringSidA() failed: %d\n", GetLastError());
1605 char account
[MAX_PATH
], domain
[MAX_PATH
];
1607 DWORD acc_size
= MAX_PATH
;
1608 DWORD dom_size
= MAX_PATH
;
1609 ret
= LookupAccountSid(NULL
, sid
, account
, &acc_size
, domain
, &dom_size
, &use
);
1610 ok(ret
|| (!ret
&& (GetLastError() == ERROR_NONE_MAPPED
)),
1611 "LookupAccountSid(%s) failed: %d\n", str_sid
, GetLastError());
1613 trace(" %s %s\\%s %d\n", str_sid
, domain
, account
, use
);
1614 else if (GetLastError() == ERROR_NONE_MAPPED
)
1615 trace(" %s couldn't be mapped\n", str_sid
);
1620 static const struct well_known_sid_value
1622 BOOL without_domain
;
1623 const char *sid_string
;
1624 } well_known_sid_values
[] = {
1625 /* 0 */ {TRUE
, "S-1-0-0"}, {TRUE
, "S-1-1-0"}, {TRUE
, "S-1-2-0"}, {TRUE
, "S-1-3-0"},
1626 /* 4 */ {TRUE
, "S-1-3-1"}, {TRUE
, "S-1-3-2"}, {TRUE
, "S-1-3-3"}, {TRUE
, "S-1-5"},
1627 /* 8 */ {FALSE
, "S-1-5-1"}, {TRUE
, "S-1-5-2"}, {TRUE
, "S-1-5-3"}, {TRUE
, "S-1-5-4"},
1628 /* 12 */ {TRUE
, "S-1-5-6"}, {TRUE
, "S-1-5-7"}, {TRUE
, "S-1-5-8"}, {TRUE
, "S-1-5-9"},
1629 /* 16 */ {TRUE
, "S-1-5-10"}, {TRUE
, "S-1-5-11"}, {TRUE
, "S-1-5-12"}, {TRUE
, "S-1-5-13"},
1630 /* 20 */ {TRUE
, "S-1-5-14"}, {FALSE
, NULL
}, {TRUE
, "S-1-5-18"}, {TRUE
, "S-1-5-19"},
1631 /* 24 */ {TRUE
, "S-1-5-20"}, {TRUE
, "S-1-5-32"},
1632 /* 26 */ {FALSE
, "S-1-5-32-544"}, {TRUE
, "S-1-5-32-545"}, {TRUE
, "S-1-5-32-546"},
1633 /* 29 */ {TRUE
, "S-1-5-32-547"}, {TRUE
, "S-1-5-32-548"}, {TRUE
, "S-1-5-32-549"},
1634 /* 32 */ {TRUE
, "S-1-5-32-550"}, {TRUE
, "S-1-5-32-551"}, {TRUE
, "S-1-5-32-552"},
1635 /* 35 */ {TRUE
, "S-1-5-32-554"}, {TRUE
, "S-1-5-32-555"}, {TRUE
, "S-1-5-32-556"},
1636 /* 38 */ {FALSE
, "S-1-5-21-12-23-34-45-56-500"}, {FALSE
, "S-1-5-21-12-23-34-45-56-501"},
1637 /* 40 */ {FALSE
, "S-1-5-21-12-23-34-45-56-502"}, {FALSE
, "S-1-5-21-12-23-34-45-56-512"},
1638 /* 42 */ {FALSE
, "S-1-5-21-12-23-34-45-56-513"}, {FALSE
, "S-1-5-21-12-23-34-45-56-514"},
1639 /* 44 */ {FALSE
, "S-1-5-21-12-23-34-45-56-515"}, {FALSE
, "S-1-5-21-12-23-34-45-56-516"},
1640 /* 46 */ {FALSE
, "S-1-5-21-12-23-34-45-56-517"}, {FALSE
, "S-1-5-21-12-23-34-45-56-518"},
1641 /* 48 */ {FALSE
, "S-1-5-21-12-23-34-45-56-519"}, {FALSE
, "S-1-5-21-12-23-34-45-56-520"},
1642 /* 50 */ {FALSE
, "S-1-5-21-12-23-34-45-56-553"},
1643 /* Added in Windows Server 2003 */
1644 /* 51 */ {TRUE
, "S-1-5-64-10"}, {TRUE
, "S-1-5-64-21"}, {TRUE
, "S-1-5-64-14"},
1645 /* 54 */ {TRUE
, "S-1-5-15"}, {TRUE
, "S-1-5-1000"}, {FALSE
, "S-1-5-32-557"},
1646 /* 57 */ {TRUE
, "S-1-5-32-558"}, {TRUE
, "S-1-5-32-559"}, {TRUE
, "S-1-5-32-560"},
1647 /* 60 */ {TRUE
, "S-1-5-32-561"}, {TRUE
, "S-1-5-32-562"},
1648 /* Added in Windows Vista: */
1649 /* 62 */ {TRUE
, "S-1-5-32-568"},
1650 /* 63 */ {TRUE
, "S-1-5-17"}, {FALSE
, "S-1-5-32-569"}, {TRUE
, "S-1-16-0"},
1651 /* 66 */ {TRUE
, "S-1-16-4096"}, {TRUE
, "S-1-16-8192"}, {TRUE
, "S-1-16-12288"},
1652 /* 69 */ {TRUE
, "S-1-16-16384"}, {TRUE
, "S-1-5-33"}, {TRUE
, "S-1-3-4"},
1653 /* 72 */ {FALSE
, "S-1-5-21-12-23-34-45-56-571"}, {FALSE
, "S-1-5-21-12-23-34-45-56-572"},
1654 /* 74 */ {TRUE
, "S-1-5-22"}, {FALSE
, "S-1-5-21-12-23-34-45-56-521"}, {TRUE
, "S-1-5-32-573"}
1657 static void test_CreateWellKnownSid(void)
1659 SID_IDENTIFIER_AUTHORITY ident
= { SECURITY_NT_AUTHORITY
};
1660 PSID domainsid
, sid
;
1665 if (!pCreateWellKnownSid
)
1667 win_skip("CreateWellKnownSid not available\n");
1672 SetLastError(0xdeadbeef);
1673 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, NULL
, &size
);
1674 error
= GetLastError();
1675 ok(!ret
, "CreateWellKnownSid succeeded\n");
1676 ok(error
== ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error
);
1677 ok(size
, "expected size > 0\n");
1679 SetLastError(0xdeadbeef);
1680 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, NULL
, &size
);
1681 error
= GetLastError();
1682 ok(!ret
, "CreateWellKnownSid succeeded\n");
1683 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %u\n", error
);
1685 sid
= HeapAlloc(GetProcessHeap(), 0, size
);
1686 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, sid
, &size
);
1687 ok(ret
, "CreateWellKnownSid failed %u\n", GetLastError());
1688 HeapFree(GetProcessHeap(), 0, sid
);
1690 /* a domain sid usually have three subauthorities but we test that CreateWellKnownSid doesn't check it */
1691 AllocateAndInitializeSid(&ident
, 6, SECURITY_NT_NON_UNIQUE
, 12, 23, 34, 45, 56, 0, 0, &domainsid
);
1693 for (i
= 0; i
< sizeof(well_known_sid_values
)/sizeof(well_known_sid_values
[0]); i
++)
1695 const struct well_known_sid_value
*value
= &well_known_sid_values
[i
];
1696 char sid_buffer
[SECURITY_MAX_SID_SIZE
];
1700 if (value
->sid_string
== NULL
)
1703 if (i
> WinAccountRasAndIasServersSid
)
1705 /* These SIDs aren't implemented by all Windows versions - detect it and break the loop */
1706 cb
= sizeof(sid_buffer
);
1707 if (!pCreateWellKnownSid(i
, domainsid
, sid_buffer
, &cb
))
1709 skip("Well known SIDs starting from %u are not implemented\n", i
);
1714 cb
= sizeof(sid_buffer
);
1715 ok(pCreateWellKnownSid(i
, value
->without_domain
? NULL
: domainsid
, sid_buffer
, &cb
), "Couldn't create well known sid %u\n", i
);
1716 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer
)), cb
, DWORD
, "%d");
1717 ok(IsValidSid(sid_buffer
), "The sid is not valid\n");
1718 ok(pConvertSidToStringSidA(sid_buffer
, &str
), "Couldn't convert SID to string\n");
1719 ok(strcmp(str
, value
->sid_string
) == 0, "SID mismatch - expected %s, got %s\n",
1720 value
->sid_string
, str
);
1723 if (value
->without_domain
)
1725 char buf2
[SECURITY_MAX_SID_SIZE
];
1727 ok(pCreateWellKnownSid(i
, domainsid
, buf2
, &cb
), "Couldn't create well known sid %u with optional domain\n", i
);
1728 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer
)), cb
, DWORD
, "%d");
1729 ok(memcmp(buf2
, sid_buffer
, cb
) == 0, "SID create with domain is different than without (%u)\n", i
);
1736 static void test_LookupAccountSid(void)
1738 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
1739 CHAR accountA
[MAX_PATH
], domainA
[MAX_PATH
], usernameA
[MAX_PATH
];
1740 DWORD acc_sizeA
, dom_sizeA
, user_sizeA
;
1741 DWORD real_acc_sizeA
, real_dom_sizeA
;
1742 WCHAR accountW
[MAX_PATH
], domainW
[MAX_PATH
];
1743 DWORD acc_sizeW
, dom_sizeW
;
1744 DWORD real_acc_sizeW
, real_dom_sizeW
;
1745 PSID pUsersSid
= NULL
;
1748 DWORD error
, size
, cbti
= 0;
1753 PTOKEN_USER ptiUser
= NULL
;
1755 /* native windows crashes if account size, domain size, or name use is NULL */
1757 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1758 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &pUsersSid
);
1759 ok(ret
|| (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
),
1760 "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1762 /* not running on NT so give up */
1763 if (!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
1766 real_acc_sizeA
= MAX_PATH
;
1767 real_dom_sizeA
= MAX_PATH
;
1768 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &real_acc_sizeA
, domainA
, &real_dom_sizeA
, &use
);
1769 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1771 /* try NULL account */
1772 acc_sizeA
= MAX_PATH
;
1773 dom_sizeA
= MAX_PATH
;
1774 ret
= LookupAccountSidA(NULL
, pUsersSid
, NULL
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1775 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1777 /* try NULL domain */
1778 acc_sizeA
= MAX_PATH
;
1779 dom_sizeA
= MAX_PATH
;
1780 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, NULL
, &dom_sizeA
, &use
);
1781 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1783 /* try a small account buffer */
1785 dom_sizeA
= MAX_PATH
;
1787 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1788 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1789 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1790 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1792 /* try a 0 sized account buffer */
1794 dom_sizeA
= MAX_PATH
;
1796 LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1797 /* this can fail or succeed depending on OS version but the size will always be returned */
1798 ok(acc_sizeA
== real_acc_sizeA
+ 1,
1799 "LookupAccountSidA() Expected acc_size = %u, got %u\n",
1800 real_acc_sizeA
+ 1, acc_sizeA
);
1802 /* try a 0 sized account buffer */
1804 dom_sizeA
= MAX_PATH
;
1805 LookupAccountSidA(NULL
, pUsersSid
, NULL
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1806 /* this can fail or succeed depending on OS version but the size will always be returned */
1807 ok(acc_sizeA
== real_acc_sizeA
+ 1,
1808 "LookupAccountSid() Expected acc_size = %u, got %u\n",
1809 real_acc_sizeA
+ 1, acc_sizeA
);
1811 /* try a small domain buffer */
1813 acc_sizeA
= MAX_PATH
;
1815 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1816 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1817 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1818 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1820 /* try a 0 sized domain buffer */
1822 acc_sizeA
= MAX_PATH
;
1824 LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1825 /* this can fail or succeed depending on OS version but the size will always be returned */
1826 ok(dom_sizeA
== real_dom_sizeA
+ 1,
1827 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1828 real_dom_sizeA
+ 1, dom_sizeA
);
1830 /* try a 0 sized domain buffer */
1832 acc_sizeA
= MAX_PATH
;
1833 LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, NULL
, &dom_sizeA
, &use
);
1834 /* this can fail or succeed depending on OS version but the size will always be returned */
1835 ok(dom_sizeA
== real_dom_sizeA
+ 1,
1836 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1837 real_dom_sizeA
+ 1, dom_sizeA
);
1839 real_acc_sizeW
= MAX_PATH
;
1840 real_dom_sizeW
= MAX_PATH
;
1841 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &real_acc_sizeW
, domainW
, &real_dom_sizeW
, &use
);
1842 ok(ret
, "LookupAccountSidW() Expected TRUE, got FALSE\n");
1844 /* try an invalid system name */
1845 real_acc_sizeA
= MAX_PATH
;
1846 real_dom_sizeA
= MAX_PATH
;
1847 ret
= LookupAccountSidA("deepthought", pUsersSid
, accountA
, &real_acc_sizeA
, domainA
, &real_dom_sizeA
, &use
);
1848 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1849 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE
|| GetLastError() == RPC_S_INVALID_NET_ADDR
/* Vista */,
1850 "LookupAccountSidA() Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %u\n", GetLastError());
1852 /* native windows crashes if domainW or accountW is NULL */
1854 /* try a small account buffer */
1856 dom_sizeW
= MAX_PATH
;
1858 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1859 ok(!ret
, "LookupAccountSidW() Expected FALSE got TRUE\n");
1860 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1861 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1863 /* try a 0 sized account buffer */
1865 dom_sizeW
= MAX_PATH
;
1867 LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1868 /* this can fail or succeed depending on OS version but the size will always be returned */
1869 ok(acc_sizeW
== real_acc_sizeW
+ 1,
1870 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1871 real_acc_sizeW
+ 1, acc_sizeW
);
1873 /* try a 0 sized account buffer */
1875 dom_sizeW
= MAX_PATH
;
1876 LookupAccountSidW(NULL
, pUsersSid
, NULL
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1877 /* this can fail or succeed depending on OS version but the size will always be returned */
1878 ok(acc_sizeW
== real_acc_sizeW
+ 1,
1879 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1880 real_acc_sizeW
+ 1, acc_sizeW
);
1882 /* try a small domain buffer */
1884 acc_sizeW
= MAX_PATH
;
1886 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1887 ok(!ret
, "LookupAccountSidW() Expected FALSE got TRUE\n");
1888 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1889 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1891 /* try a 0 sized domain buffer */
1893 acc_sizeW
= MAX_PATH
;
1895 LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1896 /* this can fail or succeed depending on OS version but the size will always be returned */
1897 ok(dom_sizeW
== real_dom_sizeW
+ 1,
1898 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1899 real_dom_sizeW
+ 1, dom_sizeW
);
1901 /* try a 0 sized domain buffer */
1903 acc_sizeW
= MAX_PATH
;
1904 LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, NULL
, &dom_sizeW
, &use
);
1905 /* this can fail or succeed depending on OS version but the size will always be returned */
1906 ok(dom_sizeW
== real_dom_sizeW
+ 1,
1907 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1908 real_dom_sizeW
+ 1, dom_sizeW
);
1910 acc_sizeW
= dom_sizeW
= use
= 0;
1911 SetLastError(0xdeadbeef);
1912 ret
= LookupAccountSidW(NULL
, pUsersSid
, NULL
, &acc_sizeW
, NULL
, &dom_sizeW
, &use
);
1913 error
= GetLastError();
1914 ok(!ret
, "LookupAccountSidW failed %u\n", GetLastError());
1915 ok(error
== ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error
);
1916 ok(acc_sizeW
, "expected non-zero account size\n");
1917 ok(dom_sizeW
, "expected non-zero domain size\n");
1918 ok(!use
, "expected zero use %u\n", use
);
1922 /* Test LookupAccountSid with Sid retrieved from token information.
1923 This assumes this process is running under the account of the current user.*/
1924 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
|TOKEN_DUPLICATE
, &hToken
);
1925 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1926 ret
= GetTokenInformation(hToken
, TokenUser
, NULL
, 0, &cbti
);
1927 ok(!ret
, "GetTokenInformation failed with error %d\n", GetLastError());
1928 ptiUser
= HeapAlloc(GetProcessHeap(), 0, cbti
);
1929 if (GetTokenInformation(hToken
, TokenUser
, ptiUser
, cbti
, &cbti
))
1931 acc_sizeA
= dom_sizeA
= MAX_PATH
;
1932 ret
= LookupAccountSidA(NULL
, ptiUser
->User
.Sid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1933 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1934 user_sizeA
= MAX_PATH
;
1935 ret
= GetUserNameA(usernameA
, &user_sizeA
);
1936 ok(ret
, "GetUserNameA() Expected TRUE, got FALSE\n");
1937 ok(lstrcmpA(usernameA
, accountA
) == 0, "LookupAccountSidA() Expected account name: %s got: %s\n", usernameA
, accountA
);
1939 HeapFree(GetProcessHeap(), 0, ptiUser
);
1941 if (pCreateWellKnownSid
&& pConvertSidToStringSidA
)
1943 trace("Well Known SIDs:\n");
1944 for (i
= 0; i
<= 60; i
++)
1946 size
= SECURITY_MAX_SID_SIZE
;
1947 if (pCreateWellKnownSid(i
, NULL
, &max_sid
.sid
, &size
))
1949 if (pConvertSidToStringSidA(&max_sid
.sid
, &str_sidA
))
1951 acc_sizeA
= MAX_PATH
;
1952 dom_sizeA
= MAX_PATH
;
1953 if (LookupAccountSidA(NULL
, &max_sid
.sid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
))
1954 trace(" %d: %s %s\\%s %d\n", i
, str_sidA
, domainA
, accountA
, use
);
1955 LocalFree(str_sidA
);
1960 if (GetLastError() != ERROR_INVALID_PARAMETER
)
1961 trace(" CreateWellKnownSid(%d) failed: %d\n", i
, GetLastError());
1963 trace(" %d: not supported\n", i
);
1967 pLsaQueryInformationPolicy
= (void *)GetProcAddress( hmod
, "LsaQueryInformationPolicy");
1968 pLsaOpenPolicy
= (void *)GetProcAddress( hmod
, "LsaOpenPolicy");
1969 pLsaFreeMemory
= (void *)GetProcAddress( hmod
, "LsaFreeMemory");
1970 pLsaClose
= (void *)GetProcAddress( hmod
, "LsaClose");
1972 if (pLsaQueryInformationPolicy
&& pLsaOpenPolicy
&& pLsaFreeMemory
&& pLsaClose
)
1976 LSA_OBJECT_ATTRIBUTES object_attributes
;
1978 ZeroMemory(&object_attributes
, sizeof(object_attributes
));
1979 object_attributes
.Length
= sizeof(object_attributes
);
1981 status
= pLsaOpenPolicy( NULL
, &object_attributes
, POLICY_ALL_ACCESS
, &handle
);
1982 ok(status
== STATUS_SUCCESS
|| status
== STATUS_ACCESS_DENIED
,
1983 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status
);
1985 /* try a more restricted access mask if necessary */
1986 if (status
== STATUS_ACCESS_DENIED
) {
1987 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
1988 status
= pLsaOpenPolicy( NULL
, &object_attributes
, POLICY_VIEW_LOCAL_INFORMATION
, &handle
);
1989 ok(status
== STATUS_SUCCESS
, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status
);
1992 if (status
== STATUS_SUCCESS
)
1994 PPOLICY_ACCOUNT_DOMAIN_INFO info
;
1995 status
= pLsaQueryInformationPolicy(handle
, PolicyAccountDomainInformation
, (PVOID
*)&info
);
1996 ok(status
== STATUS_SUCCESS
, "LsaQueryInformationPolicy() failed, returned 0x%08x\n", status
);
1997 if (status
== STATUS_SUCCESS
)
1999 ok(info
->DomainSid
!=0, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) missing SID\n");
2000 if (info
->DomainSid
)
2002 int count
= *GetSidSubAuthorityCount(info
->DomainSid
);
2003 CopySid(GetSidLengthRequired(count
), &max_sid
, info
->DomainSid
);
2004 test_sid_str((PSID
)&max_sid
.sid
);
2005 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_USER_RID_ADMIN
;
2006 max_sid
.sid
.SubAuthorityCount
= count
+ 1;
2007 test_sid_str((PSID
)&max_sid
.sid
);
2008 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_USER_RID_GUEST
;
2009 test_sid_str((PSID
)&max_sid
.sid
);
2010 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_ADMINS
;
2011 test_sid_str((PSID
)&max_sid
.sid
);
2012 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_USERS
;
2013 test_sid_str((PSID
)&max_sid
.sid
);
2014 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_GUESTS
;
2015 test_sid_str((PSID
)&max_sid
.sid
);
2016 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_COMPUTERS
;
2017 test_sid_str((PSID
)&max_sid
.sid
);
2018 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_CONTROLLERS
;
2019 test_sid_str((PSID
)&max_sid
.sid
);
2020 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_CERT_ADMINS
;
2021 test_sid_str((PSID
)&max_sid
.sid
);
2022 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_SCHEMA_ADMINS
;
2023 test_sid_str((PSID
)&max_sid
.sid
);
2024 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_ENTERPRISE_ADMINS
;
2025 test_sid_str((PSID
)&max_sid
.sid
);
2026 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_POLICY_ADMINS
;
2027 test_sid_str((PSID
)&max_sid
.sid
);
2028 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_ALIAS_RID_RAS_SERVERS
;
2029 test_sid_str((PSID
)&max_sid
.sid
);
2030 max_sid
.sid
.SubAuthority
[count
] = 1000; /* first user account */
2031 test_sid_str((PSID
)&max_sid
.sid
);
2034 pLsaFreeMemory((LPVOID
)info
);
2037 status
= pLsaClose(handle
);
2038 ok(status
== STATUS_SUCCESS
, "LsaClose() failed, returned 0x%08x\n", status
);
2044 static BOOL
get_sid_info(PSID psid
, LPSTR
*user
, LPSTR
*dom
)
2046 static CHAR account
[UNLEN
+ 1];
2047 static CHAR domain
[UNLEN
+ 1];
2048 DWORD size
, dom_size
;
2054 size
= dom_size
= UNLEN
+ 1;
2057 SetLastError(0xdeadbeef);
2058 return LookupAccountSidA(NULL
, psid
, account
, &size
, domain
, &dom_size
, &use
);
2061 static void check_wellknown_name(const char* name
, WELL_KNOWN_SID_TYPE result
)
2063 SID_IDENTIFIER_AUTHORITY ident
= { SECURITY_NT_AUTHORITY
};
2064 PSID domainsid
= NULL
;
2065 char wk_sid
[SECURITY_MAX_SID_SIZE
];
2068 DWORD sid_size
, domain_size
;
2069 SID_NAME_USE sid_use
;
2070 LPSTR domain
, account
, sid_domain
, wk_domain
, wk_account
;
2076 ret
= LookupAccountNameA(NULL
, name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2077 ok(!ret
, " %s Should have failed to lookup account name\n", name
);
2078 psid
= HeapAlloc(GetProcessHeap(),0,sid_size
);
2079 domain
= HeapAlloc(GetProcessHeap(),0,domain_size
);
2080 ret
= LookupAccountNameA(NULL
, name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2084 ok(!ret
, " %s Should have failed to lookup account name\n",name
);
2088 AllocateAndInitializeSid(&ident
, 6, SECURITY_NT_NON_UNIQUE
, 12, 23, 34, 45, 56, 0, 0, &domainsid
);
2089 cb
= sizeof(wk_sid
);
2090 if (!pCreateWellKnownSid(result
, domainsid
, wk_sid
, &cb
))
2092 win_skip("SID %i is not available on the system\n",result
);
2096 ret2
= get_sid_info(wk_sid
, &wk_account
, &wk_domain
);
2097 if (!ret2
&& GetLastError() == ERROR_NONE_MAPPED
)
2099 win_skip("CreateWellKnownSid() succeeded but the account '%s' is not present (W2K)\n", name
);
2103 get_sid_info(psid
, &account
, &sid_domain
);
2105 ok(ret
, "Failed to lookup account name %s\n",name
);
2106 ok(sid_size
!= 0, "sid_size was zero\n");
2108 ok(EqualSid(psid
,wk_sid
),"(%s) Sids fail to match well known sid!\n",name
);
2110 ok(!lstrcmp(account
, wk_account
), "Expected %s , got %s\n", account
, wk_account
);
2111 ok(!lstrcmp(domain
, wk_domain
), "Expected %s, got %s\n", wk_domain
, domain
);
2112 ok(sid_use
== SidTypeWellKnownGroup
, "Expected Use (5), got %d\n", sid_use
);
2116 HeapFree(GetProcessHeap(),0,psid
);
2117 HeapFree(GetProcessHeap(),0,domain
);
2120 static void test_LookupAccountName(void)
2122 DWORD sid_size
, domain_size
, user_size
;
2123 DWORD sid_save
, domain_save
;
2124 CHAR user_name
[UNLEN
+ 1];
2125 CHAR computer_name
[UNLEN
+ 1];
2126 SID_NAME_USE sid_use
;
2127 LPSTR domain
, account
, sid_dom
;
2131 /* native crashes if (assuming all other parameters correct):
2133 * - Sid is NULL and cbSid is > 0
2134 * - cbSid or cchReferencedDomainName are NULL
2135 * - ReferencedDomainName is NULL and cchReferencedDomainName is the correct size
2138 user_size
= UNLEN
+ 1;
2139 SetLastError(0xdeadbeef);
2140 ret
= GetUserNameA(user_name
, &user_size
);
2141 ok(ret
, "Failed to get user name : %d\n", GetLastError());
2146 sid_use
= 0xcafebabe;
2147 SetLastError(0xdeadbeef);
2148 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2149 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
2151 win_skip("LookupAccountNameA is not implemented\n");
2154 ok(!ret
, "Expected 0, got %d\n", ret
);
2155 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2156 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2157 ok(sid_size
!= 0, "Expected non-zero sid size\n");
2158 ok(domain_size
!= 0, "Expected non-zero domain size\n");
2159 ok(sid_use
== 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use
);
2161 sid_save
= sid_size
;
2162 domain_save
= domain_size
;
2164 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
2165 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
2167 /* try valid account name */
2168 ret
= LookupAccountNameA(NULL
, user_name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2169 get_sid_info(psid
, &account
, &sid_dom
);
2170 ok(ret
, "Failed to lookup account name\n");
2171 ok(sid_size
== GetLengthSid(psid
), "Expected %d, got %d\n", GetLengthSid(psid
), sid_size
);
2172 ok(!lstrcmp(account
, user_name
), "Expected %s, got %s\n", user_name
, account
);
2173 ok(!lstrcmp(domain
, sid_dom
), "Expected %s, got %s\n", sid_dom
, domain
);
2174 ok(domain_size
== domain_save
- 1, "Expected %d, got %d\n", domain_save
- 1, domain_size
);
2175 ok(strlen(domain
) == domain_size
, "Expected %d, got %d\n", lstrlen(domain
), domain_size
);
2176 ok(sid_use
== SidTypeUser
, "Expected SidTypeUser (%d), got %d\n", SidTypeUser
, sid_use
);
2177 domain_size
= domain_save
;
2178 sid_size
= sid_save
;
2180 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2182 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2186 ret
= LookupAccountNameA(NULL
, "Everyone", psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2187 get_sid_info(psid
, &account
, &sid_dom
);
2188 ok(ret
, "Failed to lookup account name\n");
2189 ok(sid_size
!= 0, "sid_size was zero\n");
2190 ok(!lstrcmp(account
, "Everyone"), "Expected Everyone, got %s\n", account
);
2191 ok(!lstrcmp(domain
, sid_dom
), "Expected %s, got %s\n", sid_dom
, domain
);
2192 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2193 ok(strlen(domain
) == domain_size
, "Expected %d, got %d\n", lstrlen(domain
), domain_size
);
2194 ok(sid_use
== SidTypeWellKnownGroup
, "Expected SidTypeWellKnownGroup (%d), got %d\n", SidTypeWellKnownGroup
, sid_use
);
2195 domain_size
= domain_save
;
2198 /* NULL Sid with zero sid size */
2199 SetLastError(0xdeadbeef);
2201 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, domain
, &domain_size
, &sid_use
);
2202 ok(!ret
, "Expected 0, got %d\n", ret
);
2203 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2204 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2205 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
2206 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
2208 /* try cchReferencedDomainName - 1 */
2209 SetLastError(0xdeadbeef);
2211 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, domain
, &domain_size
, &sid_use
);
2212 ok(!ret
, "Expected 0, got %d\n", ret
);
2213 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2214 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2215 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
2216 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
2218 /* NULL ReferencedDomainName with zero domain name size */
2219 SetLastError(0xdeadbeef);
2221 ret
= LookupAccountNameA(NULL
, user_name
, psid
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2222 ok(!ret
, "Expected 0, got %d\n", ret
);
2223 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2224 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2225 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
2226 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
2228 HeapFree(GetProcessHeap(), 0, psid
);
2229 HeapFree(GetProcessHeap(), 0, domain
);
2231 /* get sizes for NULL account name */
2234 sid_use
= 0xcafebabe;
2235 SetLastError(0xdeadbeef);
2236 ret
= LookupAccountNameA(NULL
, NULL
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2237 if (!ret
&& GetLastError() == ERROR_NONE_MAPPED
)
2238 win_skip("NULL account name doesn't work on NT4\n");
2241 ok(!ret
, "Expected 0, got %d\n", ret
);
2242 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2243 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2244 ok(sid_size
!= 0, "Expected non-zero sid size\n");
2245 ok(domain_size
!= 0, "Expected non-zero domain size\n");
2246 ok(sid_use
== 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use
);
2248 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
2249 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
2251 /* try NULL account name */
2252 ret
= LookupAccountNameA(NULL
, NULL
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2253 get_sid_info(psid
, &account
, &sid_dom
);
2254 ok(ret
, "Failed to lookup account name\n");
2255 /* Using a fixed string will not work on different locales */
2256 ok(!lstrcmp(account
, domain
),
2257 "Got %s for account and %s for domain, these should be the same\n",
2259 ok(sid_use
== SidTypeDomain
, "Expected SidTypeDomain (%d), got %d\n", SidTypeDomain
, sid_use
);
2261 HeapFree(GetProcessHeap(), 0, psid
);
2262 HeapFree(GetProcessHeap(), 0, domain
);
2265 /* try an invalid account name */
2266 SetLastError(0xdeadbeef);
2269 ret
= LookupAccountNameA(NULL
, "oogabooga", NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2270 ok(!ret
, "Expected 0, got %d\n", ret
);
2271 ok(GetLastError() == ERROR_NONE_MAPPED
||
2272 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE
),
2273 "Expected ERROR_NONE_MAPPED, got %d\n", GetLastError());
2274 ok(sid_size
== 0, "Expected 0, got %d\n", sid_size
);
2275 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2277 /* try an invalid system name */
2278 SetLastError(0xdeadbeef);
2281 ret
= LookupAccountNameA("deepthought", NULL
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2282 ok(!ret
, "Expected 0, got %d\n", ret
);
2283 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE
|| GetLastError() == RPC_S_INVALID_NET_ADDR
/* Vista */,
2284 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
2285 ok(sid_size
== 0, "Expected 0, got %d\n", sid_size
);
2286 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2288 /* try with the computer name as the account name */
2289 domain_size
= sizeof(computer_name
);
2290 GetComputerNameA(computer_name
, &domain_size
);
2293 ret
= LookupAccountNameA(NULL
, computer_name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2294 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
||
2295 GetLastError() == ERROR_NONE_MAPPED
/* in a domain */ ||
2296 broken(GetLastError() == ERROR_TRUSTED_DOMAIN_FAILURE
) ||
2297 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE
)),
2298 "LookupAccountNameA failed: %d\n", GetLastError());
2299 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
2301 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
2302 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
2303 ret
= LookupAccountNameA(NULL
, computer_name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2304 ok(ret
, "LookupAccountNameA failed: %d\n", GetLastError());
2305 ok(sid_use
== SidTypeDomain
||
2306 (sid_use
== SidTypeUser
&& ! strcmp(computer_name
, user_name
)), "expected SidTypeDomain for %s, got %d\n", computer_name
, sid_use
);
2307 HeapFree(GetProcessHeap(), 0, domain
);
2308 HeapFree(GetProcessHeap(), 0, psid
);
2311 /* Well Known names */
2312 if (!pCreateWellKnownSid
)
2314 win_skip("CreateWellKnownSid not available\n");
2318 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2320 skip("Non-English locale (skipping well known name creation tests)\n");
2324 check_wellknown_name("LocalService", WinLocalServiceSid
);
2325 check_wellknown_name("Local Service", WinLocalServiceSid
);
2327 check_wellknown_name("Local Service", 0);
2328 check_wellknown_name("NetworkService", WinNetworkServiceSid
);
2329 check_wellknown_name("Network Service", WinNetworkServiceSid
);
2331 /* example of some names where the spaces are not optional */
2332 check_wellknown_name("Terminal Server User", WinTerminalServerSid
);
2333 check_wellknown_name("TerminalServer User", 0);
2334 check_wellknown_name("TerminalServerUser", 0);
2335 check_wellknown_name("Terminal ServerUser", 0);
2337 check_wellknown_name("enterprise domain controllers",WinEnterpriseControllersSid
);
2338 check_wellknown_name("enterprisedomain controllers", 0);
2339 check_wellknown_name("enterprise domaincontrollers", 0);
2340 check_wellknown_name("enterprisedomaincontrollers", 0);
2342 /* case insensitivity */
2343 check_wellknown_name("lOCAlServICE", WinLocalServiceSid
);
2345 /* fully qualified account names */
2346 check_wellknown_name("NT AUTHORITY\\LocalService", WinLocalServiceSid
);
2347 check_wellknown_name("nt authority\\Network Service", WinNetworkServiceSid
);
2348 check_wellknown_name("nt authority test\\Network Service", 0);
2349 check_wellknown_name("Dummy\\Network Service", 0);
2350 check_wellknown_name("ntauthority\\Network Service", 0);
2353 static void test_security_descriptor(void)
2355 SECURITY_DESCRIPTOR sd
;
2358 BOOL isDefault
, isPresent
, ret
;
2362 SetLastError(0xdeadbeef);
2363 ret
= InitializeSecurityDescriptor(&sd
, SECURITY_DESCRIPTOR_REVISION
);
2364 if (ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2366 win_skip("InitializeSecurityDescriptor is not implemented\n");
2370 ok(GetSecurityDescriptorOwner(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorOwner failed\n");
2371 expect_eq(psid
, NULL
, PSID
, "%p");
2372 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2373 sd
.Control
|= SE_DACL_PRESENT
| SE_SACL_PRESENT
;
2375 SetLastError(0xdeadbeef);
2377 expect_eq(MakeSelfRelativeSD(&sd
, buf
, &size
), FALSE
, BOOL
, "%d");
2378 expect_eq(GetLastError(), ERROR_INSUFFICIENT_BUFFER
, DWORD
, "%u");
2379 ok(size
> 5, "Size not increased\n");
2382 expect_eq(MakeSelfRelativeSD(&sd
, buf
, &size
), TRUE
, BOOL
, "%d");
2383 ok(GetSecurityDescriptorOwner(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorOwner failed\n");
2384 expect_eq(psid
, NULL
, PSID
, "%p");
2385 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2386 ok(GetSecurityDescriptorGroup(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorGroup failed\n");
2387 expect_eq(psid
, NULL
, PSID
, "%p");
2388 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2389 ok(GetSecurityDescriptorDacl(&sd
, &isPresent
, &pacl
, &isDefault
), "GetSecurityDescriptorDacl failed\n");
2390 expect_eq(isPresent
, TRUE
, BOOL
, "%d");
2391 expect_eq(psid
, NULL
, PSID
, "%p");
2392 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2393 ok(GetSecurityDescriptorSacl(&sd
, &isPresent
, &pacl
, &isDefault
), "GetSecurityDescriptorSacl failed\n");
2394 expect_eq(isPresent
, TRUE
, BOOL
, "%d");
2395 expect_eq(psid
, NULL
, PSID
, "%p");
2396 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2400 #define TEST_GRANTED_ACCESS(a,b) test_granted_access(a,b,0,__LINE__)
2401 #define TEST_GRANTED_ACCESS2(a,b,c) test_granted_access(a,b,c,__LINE__)
2402 static void test_granted_access(HANDLE handle
, ACCESS_MASK access
,
2403 ACCESS_MASK alt
, int line
)
2405 OBJECT_BASIC_INFORMATION obj_info
;
2408 if (!pNtQueryObject
)
2410 skip_(__FILE__
, line
)("Not NT platform - skipping tests\n");
2414 status
= pNtQueryObject( handle
, ObjectBasicInformation
, &obj_info
,
2415 sizeof(obj_info
), NULL
);
2416 ok_(__FILE__
, line
)(!status
, "NtQueryObject with err: %08x\n", status
);
2418 ok_(__FILE__
, line
)(obj_info
.GrantedAccess
== access
||
2419 obj_info
.GrantedAccess
== alt
, "Granted access should be 0x%08x "
2420 "or 0x%08x, instead of 0x%08x\n", access
, alt
, obj_info
.GrantedAccess
);
2422 ok_(__FILE__
, line
)(obj_info
.GrantedAccess
== access
, "Granted access should "
2423 "be 0x%08x, instead of 0x%08x\n", access
, obj_info
.GrantedAccess
);
2426 #define CHECK_SET_SECURITY(o,i,e) \
2430 SetLastError( 0xdeadbeef ); \
2431 res = SetKernelObjectSecurity( o, i, SecurityDescriptor ); \
2432 err = GetLastError(); \
2433 if (e == ERROR_SUCCESS) \
2434 ok(res, "SetKernelObjectSecurity failed with %d\n", err); \
2436 ok(!res && err == e, "SetKernelObjectSecurity should have failed " \
2437 "with %s, instead of %d\n", #e, err); \
2440 static void test_process_security(void)
2444 PTOKEN_PRIMARY_GROUP group
;
2445 PSID AdminSid
= NULL
, UsersSid
= NULL
;
2447 SECURITY_DESCRIPTOR
*SecurityDescriptor
= NULL
;
2448 char buffer
[MAX_PATH
];
2449 PROCESS_INFORMATION info
;
2450 STARTUPINFOA startup
;
2451 SECURITY_ATTRIBUTES psa
;
2452 HANDLE token
, event
;
2455 Acl
= HeapAlloc(GetProcessHeap(), 0, 256);
2456 res
= InitializeAcl(Acl
, 256, ACL_REVISION
);
2457 if (!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2459 win_skip("ACLs not implemented - skipping tests\n");
2460 HeapFree(GetProcessHeap(), 0, Acl
);
2463 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
2465 /* get owner from the token we might be running as a user not admin */
2466 res
= OpenProcessToken( GetCurrentProcess(), MAXIMUM_ALLOWED
, &token
);
2467 ok(res
, "OpenProcessToken failed with error %d\n", GetLastError());
2470 HeapFree(GetProcessHeap(), 0, Acl
);
2474 res
= GetTokenInformation( token
, TokenOwner
, NULL
, 0, &size
);
2475 ok(!res
, "Expected failure, got %d\n", res
);
2476 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2477 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2479 owner
= HeapAlloc(GetProcessHeap(), 0, size
);
2480 res
= GetTokenInformation( token
, TokenOwner
, owner
, size
, &size
);
2481 ok(res
, "GetTokenInformation failed with error %d\n", GetLastError());
2482 AdminSid
= ((TOKEN_OWNER
*)owner
)->Owner
;
2484 res
= GetTokenInformation( token
, TokenPrimaryGroup
, NULL
, 0, &size
);
2485 ok(!res
, "Expected failure, got %d\n", res
);
2486 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2487 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2489 group
= HeapAlloc(GetProcessHeap(), 0, size
);
2490 res
= GetTokenInformation( token
, TokenPrimaryGroup
, group
, size
, &size
);
2491 ok(res
, "GetTokenInformation failed with error %d\n", GetLastError());
2492 UsersSid
= ((TOKEN_PRIMARY_GROUP
*)group
)->PrimaryGroup
;
2494 CloseHandle( token
);
2497 HeapFree(GetProcessHeap(), 0, group
);
2498 HeapFree(GetProcessHeap(), 0, owner
);
2499 HeapFree(GetProcessHeap(), 0, Acl
);
2503 res
= AddAccessDeniedAce(Acl
, ACL_REVISION
, PROCESS_VM_READ
, AdminSid
);
2504 ok(res
, "AddAccessDeniedAce failed with error %d\n", GetLastError());
2505 res
= AddAccessAllowedAce(Acl
, ACL_REVISION
, PROCESS_ALL_ACCESS
, AdminSid
);
2506 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2508 SecurityDescriptor
= HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
2509 res
= InitializeSecurityDescriptor(SecurityDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
2510 ok(res
, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
2512 event
= CreateEvent( NULL
, TRUE
, TRUE
, "test_event" );
2513 ok(event
!= NULL
, "CreateEvent %d\n", GetLastError());
2515 SecurityDescriptor
->Revision
= 0;
2516 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_UNKNOWN_REVISION
);
2517 SecurityDescriptor
->Revision
= SECURITY_DESCRIPTOR_REVISION
;
2519 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_INVALID_SECURITY_DESCR
);
2520 CHECK_SET_SECURITY( event
, GROUP_SECURITY_INFORMATION
, ERROR_INVALID_SECURITY_DESCR
);
2521 CHECK_SET_SECURITY( event
, SACL_SECURITY_INFORMATION
, ERROR_ACCESS_DENIED
);
2522 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2523 /* NULL DACL is valid and means default DACL from token */
2524 SecurityDescriptor
->Control
|= SE_DACL_PRESENT
;
2525 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2527 /* Set owner and group and dacl */
2528 res
= SetSecurityDescriptorOwner(SecurityDescriptor
, AdminSid
, FALSE
);
2529 ok(res
, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
2530 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2531 res
= SetSecurityDescriptorGroup(SecurityDescriptor
, UsersSid
, FALSE
);
2532 ok(res
, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2533 CHECK_SET_SECURITY( event
, GROUP_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2534 res
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
2535 ok(res
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
2536 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2538 sprintf(buffer
, "%s tests/security.c test", myARGV
[0]);
2539 memset(&startup
, 0, sizeof(startup
));
2540 startup
.cb
= sizeof(startup
);
2541 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
2542 startup
.wShowWindow
= SW_SHOWNORMAL
;
2544 psa
.nLength
= sizeof(psa
);
2545 psa
.lpSecurityDescriptor
= SecurityDescriptor
;
2546 psa
.bInheritHandle
= TRUE
;
2548 /* Doesn't matter what ACL say we should get full access for ourselves */
2549 res
= CreateProcessA( NULL
, buffer
, &psa
, NULL
, FALSE
, 0, NULL
, NULL
, &startup
, &info
);
2550 ok(res
, "CreateProcess with err:%d\n", GetLastError());
2551 TEST_GRANTED_ACCESS2( info
.hProcess
, PROCESS_ALL_ACCESS_NT4
,
2552 STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
);
2553 winetest_wait_child_process( info
.hProcess
);
2555 CloseHandle( info
.hProcess
);
2556 CloseHandle( info
.hThread
);
2557 CloseHandle( event
);
2558 HeapFree(GetProcessHeap(), 0, group
);
2559 HeapFree(GetProcessHeap(), 0, owner
);
2560 HeapFree(GetProcessHeap(), 0, Acl
);
2561 HeapFree(GetProcessHeap(), 0, SecurityDescriptor
);
2564 static void test_process_security_child(void)
2566 HANDLE handle
, handle1
;
2570 handle
= OpenProcess( PROCESS_TERMINATE
, FALSE
, GetCurrentProcessId() );
2571 ok(handle
!= NULL
, "OpenProcess(PROCESS_TERMINATE) with err:%d\n", GetLastError());
2572 TEST_GRANTED_ACCESS( handle
, PROCESS_TERMINATE
);
2574 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2575 &handle1
, 0, TRUE
, DUPLICATE_SAME_ACCESS
);
2576 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2577 TEST_GRANTED_ACCESS( handle1
, PROCESS_TERMINATE
);
2579 CloseHandle( handle1
);
2581 SetLastError( 0xdeadbeef );
2582 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2583 &handle1
, PROCESS_ALL_ACCESS
, TRUE
, 0 );
2584 err
= GetLastError();
2586 ok(!ret
&& err
== ERROR_ACCESS_DENIED
, "duplicating handle should have failed "
2587 "with STATUS_ACCESS_DENIED, instead of err:%d\n", err
);
2589 CloseHandle( handle
);
2591 /* These two should fail - they are denied by ACL */
2592 handle
= OpenProcess( PROCESS_VM_READ
, FALSE
, GetCurrentProcessId() );
2594 ok(handle
== NULL
, "OpenProcess(PROCESS_VM_READ) should have failed\n");
2595 handle
= OpenProcess( PROCESS_ALL_ACCESS
, FALSE
, GetCurrentProcessId() );
2597 ok(handle
== NULL
, "OpenProcess(PROCESS_ALL_ACCESS) should have failed\n");
2599 /* Documented privilege elevation */
2600 ret
= DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2601 &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
);
2602 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2603 TEST_GRANTED_ACCESS2( handle
, PROCESS_ALL_ACCESS_NT4
,
2604 STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
);
2606 CloseHandle( handle
);
2608 /* Same only explicitly asking for all access rights */
2609 ret
= DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2610 &handle
, PROCESS_ALL_ACCESS
, TRUE
, 0 );
2611 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2612 TEST_GRANTED_ACCESS2( handle
, PROCESS_ALL_ACCESS_NT4
,
2613 PROCESS_ALL_ACCESS
| PROCESS_QUERY_LIMITED_INFORMATION
);
2614 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2615 &handle1
, PROCESS_VM_READ
, TRUE
, 0 );
2616 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2617 TEST_GRANTED_ACCESS( handle1
, PROCESS_VM_READ
);
2618 CloseHandle( handle1
);
2619 CloseHandle( handle
);
2622 static void test_impersonation_level(void)
2624 HANDLE Token
, ProcessToken
;
2627 TOKEN_PRIVILEGES
*Privileges
;
2629 PRIVILEGE_SET
*PrivilegeSet
;
2635 pDuplicateTokenEx
= (void *)GetProcAddress(hmod
, "DuplicateTokenEx");
2636 if( !pDuplicateTokenEx
) {
2637 win_skip("DuplicateTokenEx is not available\n");
2640 SetLastError(0xdeadbeef);
2641 ret
= ImpersonateSelf(SecurityAnonymous
);
2642 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
2644 win_skip("ImpersonateSelf is not implemented\n");
2647 ok(ret
, "ImpersonateSelf(SecurityAnonymous) failed with error %d\n", GetLastError());
2648 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2649 ok(!ret
, "OpenThreadToken should have failed\n");
2650 error
= GetLastError();
2651 ok(error
== ERROR_CANT_OPEN_ANONYMOUS
, "OpenThreadToken on anonymous token should have returned ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error
);
2652 /* can't perform access check when opening object against an anonymous impersonation token */
2654 error
= RegOpenKeyEx(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2655 ok(error
== ERROR_INVALID_HANDLE
|| error
== ERROR_CANT_OPEN_ANONYMOUS
,
2656 "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE or ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error
);
2660 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
, &ProcessToken
);
2661 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
2663 ret
= pDuplicateTokenEx(ProcessToken
,
2664 TOKEN_QUERY
| TOKEN_DUPLICATE
| TOKEN_IMPERSONATE
, NULL
,
2665 SecurityAnonymous
, TokenImpersonation
, &Token
);
2666 ok(ret
, "DuplicateTokenEx failed with error %d\n", GetLastError());
2667 /* can't increase the impersonation level */
2668 ret
= DuplicateToken(Token
, SecurityIdentification
, &Token2
);
2669 error
= GetLastError();
2670 ok(!ret
&& error
== ERROR_BAD_IMPERSONATION_LEVEL
,
2671 "Duplicating a token and increasing the impersonation level should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2672 /* we can query anything from an anonymous token, including the user */
2673 ret
= GetTokenInformation(Token
, TokenUser
, NULL
, 0, &Size
);
2674 error
= GetLastError();
2675 ok(!ret
&& error
== ERROR_INSUFFICIENT_BUFFER
, "GetTokenInformation(TokenUser) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error
);
2676 User
= HeapAlloc(GetProcessHeap(), 0, Size
);
2677 ret
= GetTokenInformation(Token
, TokenUser
, User
, Size
, &Size
);
2678 ok(ret
, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
2679 HeapFree(GetProcessHeap(), 0, User
);
2681 /* PrivilegeCheck fails with SecurityAnonymous level */
2682 ret
= GetTokenInformation(Token
, TokenPrivileges
, NULL
, 0, &Size
);
2683 error
= GetLastError();
2684 ok(!ret
&& error
== ERROR_INSUFFICIENT_BUFFER
, "GetTokenInformation(TokenPrivileges) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error
);
2685 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
2686 ret
= GetTokenInformation(Token
, TokenPrivileges
, Privileges
, Size
, &Size
);
2687 ok(ret
, "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
2689 PrivilegeSet
= HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PRIVILEGE_SET
, Privilege
[Privileges
->PrivilegeCount
]));
2690 PrivilegeSet
->PrivilegeCount
= Privileges
->PrivilegeCount
;
2691 memcpy(PrivilegeSet
->Privilege
, Privileges
->Privileges
, PrivilegeSet
->PrivilegeCount
* sizeof(PrivilegeSet
->Privilege
[0]));
2692 PrivilegeSet
->Control
= PRIVILEGE_SET_ALL_NECESSARY
;
2693 HeapFree(GetProcessHeap(), 0, Privileges
);
2695 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2696 error
= GetLastError();
2697 ok(!ret
&& error
== ERROR_BAD_IMPERSONATION_LEVEL
, "PrivilegeCheck for SecurityAnonymous token should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2701 ret
= ImpersonateSelf(SecurityIdentification
);
2702 ok(ret
, "ImpersonateSelf(SecurityIdentification) failed with error %d\n", GetLastError());
2703 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2704 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
2706 /* can't perform access check when opening object against an identification impersonation token */
2707 error
= RegOpenKeyEx(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2709 ok(error
== ERROR_INVALID_HANDLE
|| error
== ERROR_BAD_IMPERSONATION_LEVEL
,
2710 "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE or ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2712 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2713 ok(ret
, "PrivilegeCheck for SecurityIdentification failed with error %d\n", GetLastError());
2717 ret
= ImpersonateSelf(SecurityImpersonation
);
2718 ok(ret
, "ImpersonateSelf(SecurityImpersonation) failed with error %d\n", GetLastError());
2719 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2720 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
2721 error
= RegOpenKeyEx(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2722 ok(error
== ERROR_SUCCESS
, "RegOpenKeyEx should have succeeded instead of failing with %d\n", error
);
2724 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2725 ok(ret
, "PrivilegeCheck for SecurityImpersonation failed with error %d\n", GetLastError());
2729 CloseHandle(ProcessToken
);
2731 HeapFree(GetProcessHeap(), 0, PrivilegeSet
);
2734 static void test_SetEntriesInAclW(void)
2737 PSID EveryoneSid
= NULL
, UsersSid
= NULL
;
2738 PACL OldAcl
= NULL
, NewAcl
;
2739 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
2740 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
2741 EXPLICIT_ACCESSW ExplicitAccess
;
2742 static const WCHAR wszEveryone
[] = {'E','v','e','r','y','o','n','e',0};
2743 static const WCHAR wszCurrentUser
[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
2745 if (!pSetEntriesInAclW
)
2747 win_skip("SetEntriesInAclW is not available\n");
2751 NewAcl
= (PACL
)0xdeadbeef;
2752 res
= pSetEntriesInAclW(0, NULL
, NULL
, &NewAcl
);
2753 if(res
== ERROR_CALL_NOT_IMPLEMENTED
)
2755 win_skip("SetEntriesInAclW is not implemented\n");
2758 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2759 ok(NewAcl
== NULL
||
2760 broken(NewAcl
!= NULL
), /* NT4 */
2761 "NewAcl=%p, expected NULL\n", NewAcl
);
2764 OldAcl
= HeapAlloc(GetProcessHeap(), 0, 256);
2765 res
= InitializeAcl(OldAcl
, 256, ACL_REVISION
);
2766 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2768 win_skip("ACLs not implemented - skipping tests\n");
2769 HeapFree(GetProcessHeap(), 0, OldAcl
);
2772 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
2774 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
2775 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2777 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
2778 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
2779 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2781 res
= AddAccessAllowedAce(OldAcl
, ACL_REVISION
, KEY_READ
, UsersSid
);
2782 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2784 ExplicitAccess
.grfAccessPermissions
= KEY_WRITE
;
2785 ExplicitAccess
.grfAccessMode
= GRANT_ACCESS
;
2786 ExplicitAccess
.grfInheritance
= NO_INHERITANCE
;
2787 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_WELL_KNOWN_GROUP
;
2788 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2789 ExplicitAccess
.Trustee
.ptstrName
= EveryoneSid
;
2790 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= 0xDEADBEEF;
2791 ExplicitAccess
.Trustee
.pMultipleTrustee
= (PVOID
)0xDEADBEEF;
2792 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2793 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2794 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2797 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2798 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
2799 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2800 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2801 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2802 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2805 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2807 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2811 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2812 ExplicitAccess
.Trustee
.ptstrName
= (LPWSTR
)wszEveryone
;
2813 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2814 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2815 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2818 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_BAD_FORM
;
2819 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2820 ok(res
== ERROR_INVALID_PARAMETER
||
2821 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2822 "SetEntriesInAclW failed: %u\n", res
);
2823 ok(NewAcl
== NULL
||
2824 broken(NewAcl
!= NULL
), /* NT4 */
2825 "returned acl wasn't NULL: %p\n", NewAcl
);
2827 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2828 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= TRUSTEE_IS_IMPERSONATE
;
2829 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2830 ok(res
== ERROR_INVALID_PARAMETER
||
2831 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2832 "SetEntriesInAclW failed: %u\n", res
);
2833 ok(NewAcl
== NULL
||
2834 broken(NewAcl
!= NULL
), /* NT4 */
2835 "returned acl wasn't NULL: %p\n", NewAcl
);
2837 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2838 ExplicitAccess
.grfAccessMode
= SET_ACCESS
;
2839 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2840 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2841 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2845 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2846 ExplicitAccess
.Trustee
.ptstrName
= (LPWSTR
)wszCurrentUser
;
2847 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2848 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2849 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2852 ExplicitAccess
.grfAccessMode
= REVOKE_ACCESS
;
2853 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2854 ExplicitAccess
.Trustee
.ptstrName
= UsersSid
;
2855 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2856 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2857 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2861 FreeSid(EveryoneSid
);
2862 HeapFree(GetProcessHeap(), 0, OldAcl
);
2865 static void test_SetEntriesInAclA(void)
2868 PSID EveryoneSid
= NULL
, UsersSid
= NULL
;
2869 PACL OldAcl
= NULL
, NewAcl
;
2870 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
2871 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
2872 EXPLICIT_ACCESS ExplicitAccess
;
2873 static const CHAR szEveryone
[] = {'E','v','e','r','y','o','n','e',0};
2874 static const CHAR szCurrentUser
[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
2876 if (!pSetEntriesInAclA
)
2878 win_skip("SetEntriesInAclA is not available\n");
2882 NewAcl
= (PACL
)0xdeadbeef;
2883 res
= pSetEntriesInAclA(0, NULL
, NULL
, &NewAcl
);
2884 if(res
== ERROR_CALL_NOT_IMPLEMENTED
)
2886 win_skip("SetEntriesInAclA is not implemented\n");
2889 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2890 ok(NewAcl
== NULL
||
2891 broken(NewAcl
!= NULL
), /* NT4 */
2892 "NewAcl=%p, expected NULL\n", NewAcl
);
2895 OldAcl
= HeapAlloc(GetProcessHeap(), 0, 256);
2896 res
= InitializeAcl(OldAcl
, 256, ACL_REVISION
);
2897 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2899 win_skip("ACLs not implemented - skipping tests\n");
2900 HeapFree(GetProcessHeap(), 0, OldAcl
);
2903 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
2905 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
2906 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2908 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
2909 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
2910 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2912 res
= AddAccessAllowedAce(OldAcl
, ACL_REVISION
, KEY_READ
, UsersSid
);
2913 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2915 ExplicitAccess
.grfAccessPermissions
= KEY_WRITE
;
2916 ExplicitAccess
.grfAccessMode
= GRANT_ACCESS
;
2917 ExplicitAccess
.grfInheritance
= NO_INHERITANCE
;
2918 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_WELL_KNOWN_GROUP
;
2919 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2920 ExplicitAccess
.Trustee
.ptstrName
= EveryoneSid
;
2921 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2922 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
2923 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2924 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2925 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2928 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2929 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
2930 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2931 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2932 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2933 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2936 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2938 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2942 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2943 ExplicitAccess
.Trustee
.ptstrName
= (LPSTR
)szEveryone
;
2944 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2945 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2946 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2949 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_BAD_FORM
;
2950 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2951 ok(res
== ERROR_INVALID_PARAMETER
||
2952 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2953 "SetEntriesInAclA failed: %u\n", res
);
2954 ok(NewAcl
== NULL
||
2955 broken(NewAcl
!= NULL
), /* NT4 */
2956 "returned acl wasn't NULL: %p\n", NewAcl
);
2958 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2959 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= TRUSTEE_IS_IMPERSONATE
;
2960 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2961 ok(res
== ERROR_INVALID_PARAMETER
||
2962 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2963 "SetEntriesInAclA failed: %u\n", res
);
2964 ok(NewAcl
== NULL
||
2965 broken(NewAcl
!= NULL
), /* NT4 */
2966 "returned acl wasn't NULL: %p\n", NewAcl
);
2968 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2969 ExplicitAccess
.grfAccessMode
= SET_ACCESS
;
2970 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2971 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2972 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2976 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2977 ExplicitAccess
.Trustee
.ptstrName
= (LPSTR
)szCurrentUser
;
2978 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2979 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2980 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2983 ExplicitAccess
.grfAccessMode
= REVOKE_ACCESS
;
2984 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2985 ExplicitAccess
.Trustee
.ptstrName
= UsersSid
;
2986 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2987 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2988 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2992 FreeSid(EveryoneSid
);
2993 HeapFree(GetProcessHeap(), 0, OldAcl
);
2996 static void test_GetNamedSecurityInfoA(void)
2998 PSECURITY_DESCRIPTOR pSecDesc
;
3000 SECURITY_DESCRIPTOR_CONTROL control
;
3004 BOOL owner_defaulted
;
3005 BOOL group_defaulted
;
3008 CHAR windows_dir
[MAX_PATH
];
3010 if (!pGetNamedSecurityInfoA
)
3012 win_skip("GetNamedSecurityInfoA is not available\n");
3016 ret
= GetWindowsDirectoryA(windows_dir
, MAX_PATH
);
3017 ok(ret
, "GetWindowsDirectory failed with error %d\n", GetLastError());
3019 SetLastError(0xdeadbeef);
3020 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,
3021 OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
3022 NULL
, NULL
, NULL
, NULL
, &pSecDesc
);
3023 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3025 win_skip("GetNamedSecurityInfoA is not implemented\n");
3028 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3030 ret
= GetSecurityDescriptorControl(pSecDesc
, &control
, &revision
);
3031 ok(ret
, "GetSecurityDescriptorControl failed with error %d\n", GetLastError());
3032 ok((control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == (SE_SELF_RELATIVE
|SE_DACL_PRESENT
) ||
3033 broken((control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == SE_DACL_PRESENT
), /* NT4 */
3034 "control (0x%x) doesn't have (SE_SELF_RELATIVE|SE_DACL_PRESENT) flags set\n", control
);
3035 ok(revision
== SECURITY_DESCRIPTOR_REVISION1
, "revision was %d instead of 1\n", revision
);
3037 isNT4
= (control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == SE_DACL_PRESENT
;
3039 ret
= GetSecurityDescriptorOwner(pSecDesc
, &owner
, &owner_defaulted
);
3040 ok(ret
, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
3041 ok(owner
!= NULL
, "owner should not be NULL\n");
3043 ret
= GetSecurityDescriptorGroup(pSecDesc
, &group
, &group_defaulted
);
3044 ok(ret
, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
3045 ok(group
!= NULL
, "group should not be NULL\n");
3046 LocalFree(pSecDesc
);
3049 /* NULL descriptor tests */
3052 win_skip("NT4 does not support GetNamedSecutityInfo with a NULL descriptor\n");
3056 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,DACL_SECURITY_INFORMATION
,
3057 NULL
, NULL
, NULL
, NULL
, NULL
);
3058 ok(error
==ERROR_INVALID_PARAMETER
, "GetNamedSecurityInfo failed with error %d\n", error
);
3060 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,DACL_SECURITY_INFORMATION
,
3061 NULL
, NULL
, &dacl
, NULL
, NULL
);
3062 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3063 ok(dacl
!= NULL
, "dacl should not be NULL\n");
3065 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,OWNER_SECURITY_INFORMATION
,
3066 NULL
, NULL
, &dacl
, NULL
, NULL
);
3067 ok(error
==ERROR_INVALID_PARAMETER
, "GetNamedSecurityInfo failed with error %d\n", error
);
3070 static void test_ConvertStringSecurityDescriptor(void)
3073 PSECURITY_DESCRIPTOR pSD
;
3074 static const WCHAR Blank
[] = { 0 };
3078 const char *sidstring
;
3085 { "D:(A;;GA;;;WD)", 0xdeadbeef, FALSE
, ERROR_UNKNOWN_REVISION
},
3086 /* test ACE string type */
3087 { "D:(A;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3088 { "D:(D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3089 { "ERROR:(D;;GA;;;WD)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_PARAMETER
},
3090 /* test ACE string with spaces */
3091 { " D:(D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3092 { "D: (D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3093 { "D:( D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3094 { "D:(D ;;GA;;;WD)", SDDL_REVISION_1
, FALSE
, RPC_S_INVALID_STRING_UUID
, ERROR_INVALID_ACL
}, /* Vista+ */
3095 { "D:(D; ;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3096 { "D:(D;; GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3097 { "D:(D;;GA ;;;WD)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_ACL
},
3098 { "D:(D;;GA; ;;WD)", SDDL_REVISION_1
, TRUE
},
3099 { "D:(D;;GA;; ;WD)", SDDL_REVISION_1
, TRUE
},
3100 { "D:(D;;GA;;; WD)", SDDL_REVISION_1
, TRUE
},
3101 { "D:(D;;GA;;;WD )", SDDL_REVISION_1
, TRUE
},
3102 /* test ACE string access rights */
3103 { "D:(A;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3104 { "D:(A;;GRGWGX;;;WD)", SDDL_REVISION_1
, TRUE
},
3105 { "D:(A;;RCSDWDWO;;;WD)", SDDL_REVISION_1
, TRUE
},
3106 { "D:(A;;RPWPCCDCLCSWLODTCR;;;WD)", SDDL_REVISION_1
, TRUE
},
3107 { "D:(A;;FAFRFWFX;;;WD)", SDDL_REVISION_1
, TRUE
},
3108 { "D:(A;;KAKRKWKX;;;WD)", SDDL_REVISION_1
, TRUE
},
3109 { "D:(A;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1
, TRUE
},
3110 { "S:(AU;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1
, TRUE
},
3111 /* test ACE string access right error case */
3112 { "D:(A;;ROB;;;WD)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_ACL
},
3113 /* test behaviour with empty strings */
3114 { "", SDDL_REVISION_1
, TRUE
},
3115 /* test ACE string SID */
3116 { "D:(D;;GA;;;S-1-0-0)", SDDL_REVISION_1
, TRUE
},
3117 { "D:(D;;GA;;;Nonexistent account)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_ACL
, ERROR_INVALID_SID
} /* W2K */
3120 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA
)
3122 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
3126 for (i
= 0; i
< sizeof(cssd
)/sizeof(cssd
[0]); i
++)
3130 SetLastError(0xdeadbeef);
3131 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3132 cssd
[i
].sidstring
, cssd
[i
].revision
, &pSD
, NULL
);
3133 GLE
= GetLastError();
3134 ok(ret
== cssd
[i
].ret
, "(%02u) Expected %s (%d)\n", i
, cssd
[i
].ret
? "success" : "failure", GLE
);
3136 ok(GLE
== cssd
[i
].GLE
||
3137 (cssd
[i
].altGLE
&& GLE
== cssd
[i
].altGLE
),
3138 "(%02u) Unexpected last error %d\n", i
, GLE
);
3143 /* test behaviour with NULL parameters */
3144 SetLastError(0xdeadbeef);
3145 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3146 NULL
, 0xdeadbeef, &pSD
, NULL
);
3148 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3149 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3152 SetLastError(0xdeadbeef);
3153 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorW(
3154 NULL
, 0xdeadbeef, &pSD
, NULL
);
3155 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3156 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3159 SetLastError(0xdeadbeef);
3160 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3161 "D:(A;;ROB;;;WD)", 0xdeadbeef, NULL
, NULL
);
3162 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3163 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3166 SetLastError(0xdeadbeef);
3167 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3168 "D:(A;;ROB;;;WD)", SDDL_REVISION_1
, NULL
, NULL
);
3169 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3170 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3173 /* test behaviour with empty strings */
3174 SetLastError(0xdeadbeef);
3175 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorW(
3176 Blank
, SDDL_REVISION_1
, &pSD
, NULL
);
3177 ok(ret
, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
3181 static void test_ConvertSecurityDescriptorToString(void)
3183 SECURITY_DESCRIPTOR desc
;
3184 SECURITY_INFORMATION sec_info
= OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
|SACL_SECURITY_INFORMATION
;
3193 if (!pConvertSecurityDescriptorToStringSecurityDescriptorA
)
3195 win_skip("ConvertSecurityDescriptorToStringSecurityDescriptor is not available\n");
3198 if (!pCreateWellKnownSid
)
3200 win_skip("CreateWellKnownSid is not available\n");
3204 /* It seems Windows XP adds an extra character to the length of the string for each ACE in an ACL. We
3205 * don't replicate this feature so we only test len >= strlen+1. */
3206 #define CHECK_RESULT_AND_FREE(exp_str) \
3207 ok(strcmp(string, (exp_str)) == 0, "String mismatch (expected \"%s\", got \"%s\")\n", (exp_str), string); \
3208 ok(len >= (strlen(exp_str) + 1), "Length mismatch (expected %d, got %d)\n", lstrlen(exp_str) + 1, len); \
3211 #define CHECK_ONE_OF_AND_FREE(exp_str1, exp_str2) \
3212 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); \
3213 ok(len >= (strlen(exp_str1) + 1) || len >= (strlen(exp_str2) + 1), "Length mismatch (expected %d or %d, got %d)\n", lstrlen(exp_str1) + 1, lstrlen(exp_str2) + 1, len); \
3216 InitializeSecurityDescriptor(&desc
, SECURITY_DESCRIPTOR_REVISION
);
3217 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3218 CHECK_RESULT_AND_FREE("");
3221 pCreateWellKnownSid(WinLocalSid
, NULL
, sid_buf
, &size
);
3222 SetSecurityDescriptorOwner(&desc
, sid_buf
, FALSE
);
3223 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3224 CHECK_RESULT_AND_FREE("O:S-1-2-0");
3226 SetSecurityDescriptorOwner(&desc
, sid_buf
, TRUE
);
3227 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3228 CHECK_RESULT_AND_FREE("O:S-1-2-0");
3230 size
= sizeof(sid_buf
);
3231 pCreateWellKnownSid(WinLocalSystemSid
, NULL
, sid_buf
, &size
);
3232 SetSecurityDescriptorOwner(&desc
, sid_buf
, TRUE
);
3233 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3234 CHECK_RESULT_AND_FREE("O:SY");
3236 pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid
);
3237 SetSecurityDescriptorGroup(&desc
, psid
, TRUE
);
3238 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3239 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576");
3241 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, GROUP_SECURITY_INFORMATION
, &string
, &len
), "Conversion failed\n");
3242 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
3244 pacl
= (PACL
)acl_buf
;
3245 InitializeAcl(pacl
, sizeof(acl_buf
), ACL_REVISION
);
3246 SetSecurityDescriptorDacl(&desc
, TRUE
, pacl
, TRUE
);
3247 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3248 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
3250 SetSecurityDescriptorDacl(&desc
, TRUE
, pacl
, FALSE
);
3251 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3252 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
3254 pConvertStringSidToSidA("S-1-5-6", &psid2
);
3255 pAddAccessAllowedAceEx(pacl
, ACL_REVISION
, NO_PROPAGATE_INHERIT_ACE
, 0xf0000000, psid2
);
3256 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3257 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)");
3259 pAddAccessAllowedAceEx(pacl
, ACL_REVISION
, INHERIT_ONLY_ACE
|INHERITED_ACE
, 0x00000003, psid2
);
3260 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3261 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)");
3263 pAddAccessDeniedAceEx(pacl
, ACL_REVISION
, OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
, 0xffffffff, psid
);
3264 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3265 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)");
3268 pacl
= (PACL
)acl_buf
;
3269 InitializeAcl(pacl
, sizeof(acl_buf
), ACL_REVISION
);
3270 SetSecurityDescriptorSacl(&desc
, TRUE
, pacl
, FALSE
);
3271 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3272 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:");
3274 /* fails in win2k */
3275 SetSecurityDescriptorDacl(&desc
, TRUE
, NULL
, FALSE
);
3276 pAddAuditAccessAceEx(pacl
, ACL_REVISION
, VALID_INHERIT_FLAGS
, KEY_READ
|KEY_WRITE
, psid2
, TRUE
, TRUE
);
3277 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
))
3279 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)", /* XP */
3280 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)" /* Vista */);
3283 /* fails in win2k */
3284 pAddAuditAccessAceEx(pacl
, ACL_REVISION
, NO_PROPAGATE_INHERIT_ACE
, FILE_GENERIC_READ
|FILE_GENERIC_WRITE
, psid2
, TRUE
, FALSE
);
3285 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
))
3287 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", /* XP */
3288 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)" /* Vista */);
3295 static void test_SetSecurityDescriptorControl (PSECURITY_DESCRIPTOR sec
)
3297 SECURITY_DESCRIPTOR_CONTROL ref
;
3298 SECURITY_DESCRIPTOR_CONTROL test
;
3300 SECURITY_DESCRIPTOR_CONTROL
const mutable
3301 = SE_DACL_AUTO_INHERIT_REQ
| SE_SACL_AUTO_INHERIT_REQ
3302 | SE_DACL_AUTO_INHERITED
| SE_SACL_AUTO_INHERITED
3303 | SE_DACL_PROTECTED
| SE_SACL_PROTECTED
3304 | 0x00000040 | 0x00000080 /* not defined in winnt.h */
3306 SECURITY_DESCRIPTOR_CONTROL
const immutable
3307 = SE_OWNER_DEFAULTED
| SE_GROUP_DEFAULTED
3308 | SE_DACL_PRESENT
| SE_DACL_DEFAULTED
3309 | SE_SACL_PRESENT
| SE_SACL_DEFAULTED
3310 | SE_RM_CONTROL_VALID
| SE_SELF_RELATIVE
3315 LPCSTR fmt
= "Expected error %s, got %u\n";
3317 GetSecurityDescriptorControl (sec
, &ref
, &dwRevision
);
3319 /* The mutable bits are mutable regardless of the truth of
3320 SE_DACL_PRESENT and/or SE_SACL_PRESENT */
3322 /* Check call barfs if any bit-of-interest is immutable */
3323 for (bit
= 0; bit
< 16; ++bit
)
3325 SECURITY_DESCRIPTOR_CONTROL
const bitOfInterest
= 1 << bit
;
3326 SECURITY_DESCRIPTOR_CONTROL setOrClear
= ref
& bitOfInterest
;
3328 SECURITY_DESCRIPTOR_CONTROL ctrl
;
3330 DWORD dwExpect
= (bitOfInterest
& immutable
)
3331 ? ERROR_INVALID_PARAMETER
: 0xbebecaca;
3332 LPCSTR strExpect
= (bitOfInterest
& immutable
)
3333 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
3335 ctrl
= (bitOfInterest
& mutable) ? ref
+ bitOfInterest
: ref
;
3336 setOrClear
^= bitOfInterest
;
3337 SetLastError (0xbebecaca);
3338 pSetSecurityDescriptorControl (sec
, bitOfInterest
, setOrClear
);
3339 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3340 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
3341 expect_eq(test
, ctrl
, int, "%x");
3343 setOrClear
^= bitOfInterest
;
3344 SetLastError (0xbebecaca);
3345 pSetSecurityDescriptorControl (sec
, bitOfInterest
, setOrClear
);
3346 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3347 GetSecurityDescriptorControl (sec
, &test
, &dwRevision
);
3348 expect_eq(test
, ref
, int, "%x");
3351 /* Check call barfs if any bit-to-set is immutable
3352 even when not a bit-of-interest */
3353 for (bit
= 0; bit
< 16; ++bit
)
3355 SECURITY_DESCRIPTOR_CONTROL
const bitsOfInterest
= mutable;
3356 SECURITY_DESCRIPTOR_CONTROL setOrClear
= ref
& bitsOfInterest
;
3358 SECURITY_DESCRIPTOR_CONTROL ctrl
;
3360 DWORD dwExpect
= ((1 << bit
) & immutable
)
3361 ? ERROR_INVALID_PARAMETER
: 0xbebecaca;
3362 LPCSTR strExpect
= ((1 << bit
) & immutable
)
3363 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
3365 ctrl
= ((1 << bit
) & immutable
) ? test
: ref
| mutable;
3366 setOrClear
^= bitsOfInterest
;
3367 SetLastError (0xbebecaca);
3368 pSetSecurityDescriptorControl (sec
, bitsOfInterest
, setOrClear
| (1 << bit
));
3369 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3370 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
3371 expect_eq(test
, ctrl
, int, "%x");
3373 ctrl
= ((1 << bit
) & immutable
) ? test
: ref
| (1 << bit
);
3374 setOrClear
^= bitsOfInterest
;
3375 SetLastError (0xbebecaca);
3376 pSetSecurityDescriptorControl (sec
, bitsOfInterest
, setOrClear
| (1 << bit
));
3377 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3378 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
3379 expect_eq(test
, ctrl
, int, "%x");
3383 static void test_PrivateObjectSecurity(void)
3385 SECURITY_INFORMATION sec_info
= OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
|SACL_SECURITY_INFORMATION
;
3386 SECURITY_DESCRIPTOR_CONTROL ctrl
;
3387 PSECURITY_DESCRIPTOR sec
;
3393 PSECURITY_DESCRIPTOR buf
;
3396 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA
)
3398 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
3402 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3404 "G:S-1-5-21-93476-23408-4576"
3405 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)"
3406 "(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3407 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
3408 SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
3410 test_SetSecurityDescriptorControl(sec
);
3414 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3416 "G:S-1-5-21-93476-23408-4576",
3417 SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
3419 test_SetSecurityDescriptorControl(sec
);
3423 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3425 "G:S-1-5-21-93476-23408-4576"
3426 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3427 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
3428 buf
= HeapAlloc(GetProcessHeap(), 0, dwDescSize
);
3429 pSetSecurityDescriptorControl(sec
, SE_DACL_PROTECTED
, SE_DACL_PROTECTED
);
3430 GetSecurityDescriptorControl(sec
, &ctrl
, &dwRevision
);
3431 expect_eq(ctrl
, 0x9014, int, "%x");
3433 ret
= GetPrivateObjectSecurity(sec
, GROUP_SECURITY_INFORMATION
, buf
, dwDescSize
, &retSize
);
3434 ok(ret
, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3435 ok(retSize
<= dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
3436 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3437 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
3438 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
3439 expect_eq(ctrl
, 0x8000, int, "%x");
3441 ret
= GetPrivateObjectSecurity(sec
, GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, buf
, dwDescSize
, &retSize
);
3442 ok(ret
, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3443 ok(retSize
<= dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
3444 ret
= pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
);
3445 ok(ret
, "Conversion failed err=%u\n", GetLastError());
3446 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)",
3447 "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 */
3448 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
3449 expect_eq(ctrl
& (~ SE_DACL_PROTECTED
), 0x8004, int, "%x");
3451 ret
= GetPrivateObjectSecurity(sec
, sec_info
, buf
, dwDescSize
, &retSize
);
3452 ok(ret
, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3453 ok(retSize
== dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
3454 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3455 CHECK_ONE_OF_AND_FREE("O:SY"
3456 "G:S-1-5-21-93476-23408-4576"
3457 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3458 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
3460 "G:S-1-5-21-93476-23408-4576"
3461 "D:P(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3462 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)"); /* Win7 */
3463 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
3464 expect_eq(ctrl
& (~ SE_DACL_PROTECTED
), 0x8014, int, "%x");
3466 SetLastError(0xdeadbeef);
3467 ok(GetPrivateObjectSecurity(sec
, sec_info
, buf
, 5, &retSize
) == FALSE
, "GetPrivateObjectSecurity should have failed\n");
3468 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Expected error ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
3471 HeapFree(GetProcessHeap(), 0, buf
);
3473 #undef CHECK_RESULT_AND_FREE
3474 #undef CHECK_ONE_OF_AND_FREE
3476 static void test_acls(void)
3479 PACL pAcl
= (PACL
)buffer
;
3482 SetLastError(0xdeadbeef);
3483 ret
= InitializeAcl(pAcl
, sizeof(ACL
) - 1, ACL_REVISION
);
3484 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
3486 win_skip("InitializeAcl is not implemented\n");
3490 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "InitializeAcl with too small a buffer should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
3492 SetLastError(0xdeadbeef);
3493 ret
= InitializeAcl(pAcl
, 0xffffffff, ACL_REVISION
);
3494 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl with too large a buffer should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3496 SetLastError(0xdeadbeef);
3497 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION1
);
3498 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl(ACL_REVISION1) should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3500 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION2
);
3501 ok(ret
, "InitializeAcl(ACL_REVISION2) failed with error %d\n", GetLastError());
3503 ret
= IsValidAcl(pAcl
);
3504 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
3506 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION3
);
3507 ok(ret
, "InitializeAcl(ACL_REVISION3) failed with error %d\n", GetLastError());
3509 ret
= IsValidAcl(pAcl
);
3510 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
3512 SetLastError(0xdeadbeef);
3513 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION4
);
3514 if (GetLastError() != ERROR_INVALID_PARAMETER
)
3516 ok(ret
, "InitializeAcl(ACL_REVISION4) failed with error %d\n", GetLastError());
3518 ret
= IsValidAcl(pAcl
);
3519 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
3522 win_skip("ACL_REVISION4 is not implemented on NT4\n");
3524 SetLastError(0xdeadbeef);
3525 ret
= InitializeAcl(pAcl
, sizeof(buffer
), -1);
3526 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl(-1) failed with error %d\n", GetLastError());
3529 static void test_GetSecurityInfo(void)
3532 PSECURITY_DESCRIPTOR sd
;
3537 if (!pGetSecurityInfo
)
3539 win_skip("GetSecurityInfo is not available\n");
3543 /* Create something. Files have lots of associated security info. */
3544 obj
= CreateFile(myARGV
[0], GENERIC_READ
, FILE_SHARE_READ
, NULL
,
3545 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3546 if (obj
== INVALID_HANDLE_VALUE
)
3548 skip("Couldn't create an object for GetSecurityInfo test\n");
3552 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
,
3553 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
3554 &owner
, &group
, &dacl
, NULL
, &sd
);
3555 if (ret
== ERROR_CALL_NOT_IMPLEMENTED
)
3557 win_skip("GetSecurityInfo is not implemented\n");
3561 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
3562 ok(sd
!= NULL
, "GetSecurityInfo\n");
3563 ok(owner
!= NULL
, "GetSecurityInfo\n");
3564 ok(group
!= NULL
, "GetSecurityInfo\n");
3566 ok(IsValidAcl(dacl
), "GetSecurityInfo\n");
3568 win_skip("No ACL information returned\n");
3572 if (!pCreateWellKnownSid
)
3574 win_skip("NULL parameter test would crash on NT4\n");
3579 /* If we don't ask for the security descriptor, Windows will still give us
3580 the other stuff, leaving us no way to free it. */
3581 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
,
3582 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
3583 &owner
, &group
, &dacl
, NULL
, NULL
);
3584 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
3585 ok(owner
!= NULL
, "GetSecurityInfo\n");
3586 ok(group
!= NULL
, "GetSecurityInfo\n");
3588 ok(IsValidAcl(dacl
), "GetSecurityInfo\n");
3590 win_skip("No ACL information returned\n");
3595 static void test_GetSidSubAuthority(void)
3599 if (!pGetSidSubAuthority
|| !pConvertStringSidToSidA
|| !pIsValidSid
|| !pGetSidSubAuthorityCount
)
3601 win_skip("Some functions not available\n");
3604 /* Note: on windows passing in an invalid index like -1, lets GetSidSubAuthority return 0x05000000 but
3605 still GetLastError returns ERROR_SUCCESS then. We don't test these unlikely cornercases here for now */
3606 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576",&psid
),"ConvertStringSidToSidA failed\n");
3607 ok(pIsValidSid(psid
),"Sid is not valid\n");
3608 SetLastError(0xbebecaca);
3609 ok(*pGetSidSubAuthorityCount(psid
) == 4,"GetSidSubAuthorityCount gave %d expected 4\n",*pGetSidSubAuthorityCount(psid
));
3610 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3611 SetLastError(0xbebecaca);
3612 ok(*pGetSidSubAuthority(psid
,0) == 21,"GetSidSubAuthority gave %d expected 21\n",*pGetSidSubAuthority(psid
,0));
3613 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3614 SetLastError(0xbebecaca);
3615 ok(*pGetSidSubAuthority(psid
,1) == 93476,"GetSidSubAuthority gave %d expected 93476\n",*pGetSidSubAuthority(psid
,1));
3616 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3617 SetLastError(0xbebecaca);
3618 ok(pGetSidSubAuthority(psid
,4) != NULL
,"Expected out of bounds GetSidSubAuthority to return a non-NULL pointer\n");
3619 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3623 static void test_CheckTokenMembership(void)
3625 PTOKEN_GROUPS token_groups
;
3627 HANDLE process_token
, token
;
3632 if (!pCheckTokenMembership
)
3634 win_skip("CheckTokenMembership is not available\n");
3637 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &process_token
);
3638 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
3640 ret
= DuplicateToken(process_token
, SecurityImpersonation
, &token
);
3641 ok(ret
, "DuplicateToken failed with error %d\n", GetLastError());
3644 ret
= GetTokenInformation(token
, TokenGroups
, NULL
, 0, &size
);
3645 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
3646 "GetTokenInformation(TokenGroups) %s with error %d\n",
3647 ret
? "succeeded" : "failed", GetLastError());
3648 token_groups
= HeapAlloc(GetProcessHeap(), 0, size
);
3649 ret
= GetTokenInformation(token
, TokenGroups
, token_groups
, size
, &size
);
3650 ok(ret
, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
3652 for (i
= 0; i
< token_groups
->GroupCount
; i
++)
3654 if (token_groups
->Groups
[i
].Attributes
& SE_GROUP_ENABLED
)
3658 if (i
== token_groups
->GroupCount
)
3660 HeapFree(GetProcessHeap(), 0, token_groups
);
3662 skip("user not a member of any group\n");
3667 ret
= pCheckTokenMembership(token
, token_groups
->Groups
[i
].Sid
, &is_member
);
3668 ok(ret
, "CheckTokenMembership failed with error %d\n", GetLastError());
3669 ok(is_member
, "CheckTokenMembership should have detected sid as member\n");
3672 ret
= pCheckTokenMembership(NULL
, token_groups
->Groups
[i
].Sid
, &is_member
);
3673 ok(ret
, "CheckTokenMembership failed with error %d\n", GetLastError());
3674 ok(is_member
, "CheckTokenMembership should have detected sid as member\n");
3677 SetLastError(0xdeadbeef);
3678 ret
= pCheckTokenMembership(process_token
, token_groups
->Groups
[i
].Sid
, &is_member
);
3679 ok(!ret
&& GetLastError() == ERROR_NO_IMPERSONATION_TOKEN
,
3680 "CheckTokenMembership with process token %s with error %d\n",
3681 ret
? "succeeded" : "failed", GetLastError());
3682 ok(!is_member
, "CheckTokenMembership should have cleared is_member\n");
3684 HeapFree(GetProcessHeap(), 0, token_groups
);
3686 CloseHandle(process_token
);
3689 static void test_EqualSid(void)
3693 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
3694 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
3696 SetLastError(0xdeadbeef);
3697 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
3698 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &sid1
);
3699 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
3701 win_skip("AllocateAndInitializeSid is not implemented\n");
3704 ok(ret
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3705 ok(GetLastError() == 0xdeadbeef,
3706 "AllocateAndInitializeSid shouldn't have set last error to %d\n",
3709 ret
= AllocateAndInitializeSid(&SIDAuthWorld
, 1, SECURITY_WORLD_RID
,
3710 0, 0, 0, 0, 0, 0, 0, &sid2
);
3711 ok(ret
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3713 SetLastError(0xdeadbeef);
3714 ret
= EqualSid(sid1
, sid2
);
3715 ok(!ret
, "World and domain admins sids shouldn't have been equal\n");
3716 ok(GetLastError() == ERROR_SUCCESS
||
3717 broken(GetLastError() == 0xdeadbeef), /* NT4 */
3718 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
3721 SetLastError(0xdeadbeef);
3722 sid2
= FreeSid(sid2
);
3723 ok(!sid2
, "FreeSid should have returned NULL instead of %p\n", sid2
);
3724 ok(GetLastError() == 0xdeadbeef,
3725 "FreeSid shouldn't have set last error to %d\n",
3728 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
3729 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &sid2
);
3730 ok(ret
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3732 SetLastError(0xdeadbeef);
3733 ret
= EqualSid(sid1
, sid2
);
3734 ok(ret
, "Same sids should have been equal\n");
3735 ok(GetLastError() == ERROR_SUCCESS
||
3736 broken(GetLastError() == 0xdeadbeef), /* NT4 */
3737 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
3740 ((SID
*)sid2
)->Revision
= 2;
3741 SetLastError(0xdeadbeef);
3742 ret
= EqualSid(sid1
, sid2
);
3743 ok(!ret
, "EqualSid with invalid sid should have returned FALSE\n");
3744 ok(GetLastError() == ERROR_SUCCESS
||
3745 broken(GetLastError() == 0xdeadbeef), /* NT4 */
3746 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
3748 ((SID
*)sid2
)->Revision
= SID_REVISION
;
3754 static void test_GetUserNameA(void)
3756 char buffer
[UNLEN
+ 1], filler
[UNLEN
+ 1];
3757 DWORD required_len
, buffer_len
;
3760 /* Test crashes on Windows. */
3763 SetLastError(0xdeadbeef);
3764 GetUserNameA(NULL
, NULL
);
3767 SetLastError(0xdeadbeef);
3769 ret
= GetUserNameA(NULL
, &required_len
);
3770 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
3771 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
3772 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
3774 SetLastError(0xdeadbeef);
3776 ret
= GetUserNameA(NULL
, &required_len
);
3777 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
3778 ok(required_len
!= 0 && required_len
!= 1, "Outputted buffer length was %u\n", required_len
);
3779 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
3781 /* Tests crashes on Windows. */
3784 SetLastError(0xdeadbeef);
3785 required_len
= UNLEN
+ 1;
3786 GetUserNameA(NULL
, &required_len
);
3788 SetLastError(0xdeadbeef);
3789 GetUserNameA(buffer
, NULL
);
3792 memset(filler
, 'x', sizeof(filler
));
3794 /* Note that GetUserNameA on XP and newer outputs the number of bytes
3795 * required for a Unicode string, which affects a test in the next block. */
3796 SetLastError(0xdeadbeef);
3797 memcpy(buffer
, filler
, sizeof(filler
));
3799 ret
= GetUserNameA(buffer
, &required_len
);
3800 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
3801 ok(!memcmp(buffer
, filler
, sizeof(filler
)), "Output buffer was altered\n");
3802 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
3803 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
3805 SetLastError(0xdeadbeef);
3806 memcpy(buffer
, filler
, sizeof(filler
));
3807 buffer_len
= required_len
;
3808 ret
= GetUserNameA(buffer
, &buffer_len
);
3809 ok(ret
== TRUE
, "GetUserNameA returned %d, last error %u\n", ret
, GetLastError());
3810 ok(memcmp(buffer
, filler
, sizeof(filler
)) != 0, "Output buffer was untouched\n");
3811 ok(buffer_len
== required_len
||
3812 broken(buffer_len
== required_len
/ sizeof(WCHAR
)), /* XP+ */
3813 "Outputted buffer length was %u\n", buffer_len
);
3815 /* Use the reported buffer size from the last GetUserNameA call and pass
3816 * a length that is one less than the required value. */
3817 SetLastError(0xdeadbeef);
3818 memcpy(buffer
, filler
, sizeof(filler
));
3820 ret
= GetUserNameA(buffer
, &buffer_len
);
3821 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
3822 ok(!memcmp(buffer
, filler
, sizeof(filler
)), "Output buffer was untouched\n");
3823 ok(buffer_len
== required_len
, "Outputted buffer length was %u\n", buffer_len
);
3824 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
3827 static void test_GetUserNameW(void)
3829 WCHAR buffer
[UNLEN
+ 1], filler
[UNLEN
+ 1];
3830 DWORD required_len
, buffer_len
;
3833 /* Test crashes on Windows. */
3836 SetLastError(0xdeadbeef);
3837 GetUserNameW(NULL
, NULL
);
3840 SetLastError(0xdeadbeef);
3842 ret
= GetUserNameW(NULL
, &required_len
);
3843 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
3844 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
3845 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
3847 SetLastError(0xdeadbeef);
3849 ret
= GetUserNameW(NULL
, &required_len
);
3850 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
3851 ok(required_len
!= 0 && required_len
!= 1, "Outputted buffer length was %u\n", required_len
);
3852 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
3854 /* Tests crash on Windows. */
3857 SetLastError(0xdeadbeef);
3858 required_len
= UNLEN
+ 1;
3859 GetUserNameW(NULL
, &required_len
);
3861 SetLastError(0xdeadbeef);
3862 GetUserNameW(buffer
, NULL
);
3865 memset(filler
, 'x', sizeof(filler
));
3867 SetLastError(0xdeadbeef);
3868 memcpy(buffer
, filler
, sizeof(filler
));
3870 ret
= GetUserNameW(buffer
, &required_len
);
3871 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
3872 ok(!memcmp(buffer
, filler
, sizeof(filler
)), "Output buffer was altered\n");
3873 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
3874 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
3876 SetLastError(0xdeadbeef);
3877 memcpy(buffer
, filler
, sizeof(filler
));
3878 buffer_len
= required_len
;
3879 ret
= GetUserNameW(buffer
, &buffer_len
);
3880 ok(ret
== TRUE
, "GetUserNameW returned %d, last error %u\n", ret
, GetLastError());
3881 ok(memcmp(buffer
, filler
, sizeof(filler
)) != 0, "Output buffer was untouched\n");
3882 ok(buffer_len
== required_len
, "Outputted buffer length was %u\n", buffer_len
);
3884 /* GetUserNameW on XP and newer writes a truncated portion of the username string to the buffer. */
3885 SetLastError(0xdeadbeef);
3886 memcpy(buffer
, filler
, sizeof(filler
));
3888 ret
= GetUserNameW(buffer
, &buffer_len
);
3889 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
3890 ok(!memcmp(buffer
, filler
, sizeof(filler
)) ||
3891 broken(memcmp(buffer
, filler
, sizeof(filler
)) != 0), /* XP+ */
3892 "Output buffer was altered\n");
3893 ok(buffer_len
== required_len
, "Outputted buffer length was %u\n", buffer_len
);
3894 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
3897 static void test_CreateRestrictedToken(void)
3899 HANDLE process_token
, token
, r_token
;
3900 PTOKEN_GROUPS token_groups
, groups2
;
3901 SID_AND_ATTRIBUTES sattr
;
3907 if (!pCreateRestrictedToken
)
3909 win_skip("CreateRestrictedToken is not available\n");
3913 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &process_token
);
3914 ok(ret
, "got error %d\n", GetLastError());
3916 ret
= DuplicateTokenEx(process_token
, TOKEN_DUPLICATE
|TOKEN_ADJUST_GROUPS
|TOKEN_QUERY
,
3917 NULL
, SecurityImpersonation
, TokenImpersonation
, &token
);
3918 ok(ret
, "got error %d\n", GetLastError());
3921 ret
= GetTokenInformation(token
, TokenGroups
, NULL
, 0, &size
);
3922 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
3923 "got %d with error %d\n", ret
, GetLastError());
3924 token_groups
= HeapAlloc(GetProcessHeap(), 0, size
);
3925 ret
= GetTokenInformation(token
, TokenGroups
, token_groups
, size
, &size
);
3926 ok(ret
, "got error %d\n", GetLastError());
3928 for (i
= 0; i
< token_groups
->GroupCount
; i
++)
3930 if (token_groups
->Groups
[i
].Attributes
& SE_GROUP_ENABLED
)
3934 if (i
== token_groups
->GroupCount
)
3936 HeapFree(GetProcessHeap(), 0, token_groups
);
3938 skip("User not a member of any group\n");
3943 ret
= pCheckTokenMembership(token
, token_groups
->Groups
[i
].Sid
, &is_member
);
3944 ok(ret
, "got error %d\n", GetLastError());
3945 ok(is_member
, "not a member\n");
3947 /* disable a SID in new token */
3948 sattr
.Sid
= token_groups
->Groups
[i
].Sid
;
3949 sattr
.Attributes
= 0;
3951 ret
= pCreateRestrictedToken(token
, 0, 1, &sattr
, 0, NULL
, 0, NULL
, &r_token
);
3952 todo_wine
ok(ret
, "got error %d\n", GetLastError());
3956 /* check if a SID is enabled */
3958 ret
= pCheckTokenMembership(r_token
, token_groups
->Groups
[i
].Sid
, &is_member
);
3959 ok(ret
, "got error %d\n", GetLastError());
3960 ok(!is_member
, "not a member\n");
3962 ret
= GetTokenInformation(r_token
, TokenGroups
, NULL
, 0, &size
);
3963 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "got %d with error %d\n",
3964 ret
, GetLastError());
3965 groups2
= HeapAlloc(GetProcessHeap(), 0, size
);
3966 ret
= GetTokenInformation(r_token
, TokenGroups
, groups2
, size
, &size
);
3967 ok(ret
, "got error %d\n", GetLastError());
3969 for (j
= 0; j
< groups2
->GroupCount
; j
++)
3971 if (EqualSid(groups2
->Groups
[j
].Sid
, token_groups
->Groups
[i
].Sid
))
3975 ok(groups2
->Groups
[j
].Attributes
& SE_GROUP_USE_FOR_DENY_ONLY
,
3976 "got wrong attributes\n");
3977 ok((groups2
->Groups
[j
].Attributes
& SE_GROUP_ENABLED
) == 0,
3978 "got wrong attributes\n");
3980 HeapFree(GetProcessHeap(), 0, groups2
);
3983 HeapFree(GetProcessHeap(), 0, token_groups
);
3984 CloseHandle(r_token
);
3986 CloseHandle(process_token
);
3989 static void validate_default_security_descriptor(SECURITY_DESCRIPTOR
*sd
)
3991 BOOL ret
, present
, defaulted
;
3997 acl
= (void *)0xdeadbeef;
3998 SetLastError(0xdeadbeef);
3999 ret
= GetSecurityDescriptorDacl(sd
, &present
, &acl
, &defaulted
);
4000 ok(ret
, "GetSecurityDescriptorDacl error %d\n", GetLastError());
4002 ok(present
== 1, "acl is not present\n");
4004 ok(acl
!= (void *)0xdeadbeef && acl
!= NULL
, "acl pointer is not set\n");
4005 ok(defaulted
== 0, "defaulted is set to TRUE\n");
4008 sid
= (void *)0xdeadbeef;
4009 SetLastError(0xdeadbeef);
4010 ret
= GetSecurityDescriptorOwner(sd
, &sid
, &defaulted
);
4011 ok(ret
, "GetSecurityDescriptorOwner error %d\n", GetLastError());
4013 ok(sid
!= (void *)0xdeadbeef && sid
!= NULL
, "sid pointer is not set\n");
4014 ok(defaulted
== 0, "defaulted is set to TRUE\n");
4017 sid
= (void *)0xdeadbeef;
4018 SetLastError(0xdeadbeef);
4019 ret
= GetSecurityDescriptorGroup(sd
, &sid
, &defaulted
);
4020 ok(ret
, "GetSecurityDescriptorGroup error %d\n", GetLastError());
4022 ok(sid
!= (void *)0xdeadbeef && sid
!= NULL
, "sid pointer is not set\n");
4023 ok(defaulted
== 0, "defaulted is set to TRUE\n");
4026 static void test_default_handle_security(HANDLE token
, HANDLE handle
, GENERIC_MAPPING
*mapping
)
4028 DWORD ret
, length
, needed
, granted
, priv_set_len
;
4030 PRIVILEGE_SET priv_set
;
4031 SECURITY_DESCRIPTOR
*sd
;
4033 needed
= 0xdeadbeef;
4034 SetLastError(0xdeadbeef);
4035 ret
= GetKernelObjectSecurity(handle
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
4037 ok(!ret
, "GetKernelObjectSecurity should fail\n");
4038 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
4039 ok(needed
!= 0xdeadbeef, "GetKernelObjectSecurity should return required buffer length\n");
4042 sd
= HeapAlloc(GetProcessHeap(), 0, length
);
4045 SetLastError(0xdeadbeef);
4046 ret
= GetKernelObjectSecurity(handle
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
4047 sd
, length
, &needed
);
4048 ok(ret
, "GetKernelObjectSecurity error %d\n", GetLastError());
4049 ok(needed
== length
, "GetKernelObjectSecurity should return required buffer length\n");
4051 validate_default_security_descriptor(sd
);
4053 priv_set_len
= sizeof(priv_set
);
4054 granted
= 0xdeadbeef;
4055 status
= 0xdeadbeef;
4056 SetLastError(0xdeadbeef);
4057 ret
= AccessCheck(sd
, token
, MAXIMUM_ALLOWED
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4059 ok(ret
, "AccessCheck error %d\n", GetLastError());
4060 ok(status
== 1, "expected 1, got %d\n", status
);
4061 ok(granted
== mapping
->GenericAll
, "expected %#x, got %#x\n", mapping
->GenericAll
, granted
);
4063 HeapFree(GetProcessHeap(), 0, sd
);
4066 static void test_mutex_security(HANDLE token
)
4069 GENERIC_MAPPING mapping
= { STANDARD_RIGHTS_READ
, STANDARD_RIGHTS_WRITE
,
4070 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
4071 STANDARD_RIGHTS_ALL
| MUTEX_ALL_ACCESS
};
4073 SetLastError(0xdeadbeef);
4074 mutex
= OpenMutex(0, FALSE
, "WineTestMutex");
4075 ok(!mutex
, "mutex should not exist\n");
4076 ok(GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %u\n", GetLastError());
4078 SetLastError(0xdeadbeef);
4079 mutex
= CreateMutex(NULL
, FALSE
, "WineTestMutex");
4080 ok(mutex
!= 0, "CreateMutex error %d\n", GetLastError());
4082 test_default_handle_security(token
, mutex
, &mapping
);
4084 CloseHandle (mutex
);
4087 static BOOL
validate_impersonation_token(HANDLE token
, DWORD
*token_type
)
4091 SECURITY_IMPERSONATION_LEVEL sil
;
4095 SetLastError(0xdeadbeef);
4096 ret
= GetTokenInformation(token
, TokenType
, &type
, sizeof(type
), &needed
);
4097 ok(ret
, "GetTokenInformation error %d\n", GetLastError());
4098 ok(needed
== sizeof(type
), "GetTokenInformation should return required buffer length\n");
4099 ok(type
== TokenPrimary
|| type
== TokenImpersonation
, "expected TokenPrimary or TokenImpersonation, got %d\n", type
);
4102 if (type
!= TokenImpersonation
) return FALSE
;
4105 SetLastError(0xdeadbeef);
4106 ret
= GetTokenInformation(token
, TokenImpersonationLevel
, &sil
, sizeof(sil
), &needed
);
4107 ok(ret
, "GetTokenInformation error %d\n", GetLastError());
4108 ok(needed
== sizeof(sil
), "GetTokenInformation should return required buffer length\n");
4109 ok(sil
== SecurityImpersonation
, "expected SecurityImpersonation, got %d\n", sil
);
4111 needed
= 0xdeadbeef;
4112 SetLastError(0xdeadbeef);
4113 ret
= GetTokenInformation(token
, TokenDefaultDacl
, NULL
, 0, &needed
);
4114 ok(!ret
, "GetTokenInformation should fail\n");
4115 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
4116 ok(needed
!= 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
4117 ok(needed
> sizeof(TOKEN_DEFAULT_DACL
), "GetTokenInformation returned empty default DACL\n");
4119 needed
= 0xdeadbeef;
4120 SetLastError(0xdeadbeef);
4121 ret
= GetTokenInformation(token
, TokenOwner
, NULL
, 0, &needed
);
4122 ok(!ret
, "GetTokenInformation should fail\n");
4123 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
4124 ok(needed
!= 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
4125 ok(needed
> sizeof(TOKEN_OWNER
), "GetTokenInformation returned empty token owner\n");
4127 needed
= 0xdeadbeef;
4128 SetLastError(0xdeadbeef);
4129 ret
= GetTokenInformation(token
, TokenPrimaryGroup
, NULL
, 0, &needed
);
4130 ok(!ret
, "GetTokenInformation should fail\n");
4131 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
4132 ok(needed
!= 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
4133 ok(needed
> sizeof(TOKEN_PRIMARY_GROUP
), "GetTokenInformation returned empty token primary group\n");
4138 static void test_kernel_objects_security(void)
4140 HANDLE token
, process_token
;
4141 DWORD ret
, token_type
;
4143 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
| TOKEN_QUERY
, &process_token
);
4144 ok(ret
, "OpenProcessToken error %d\n", GetLastError());
4146 ret
= validate_impersonation_token(process_token
, &token_type
);
4147 ok(token_type
== TokenPrimary
, "expected TokenPrimary, got %d\n", token_type
);
4148 ok(!ret
, "access token should not be an impersonation token\n");
4150 ret
= DuplicateToken(process_token
, SecurityImpersonation
, &token
);
4151 ok(ret
, "DuplicateToken error %d\n", GetLastError());
4153 ret
= validate_impersonation_token(token
, &token_type
);
4154 ok(ret
, "access token should be a valid impersonation token\n");
4155 ok(token_type
== TokenImpersonation
, "expected TokenImpersonation, got %d\n", token_type
);
4157 test_mutex_security(token
);
4158 /* FIXME: test other kernel object types */
4160 CloseHandle(process_token
);
4164 START_TEST(security
)
4171 test_process_security_child();
4174 test_kernel_objects_security();
4178 test_CreateWellKnownSid();
4179 test_FileSecurity();
4182 test_LookupAccountSid();
4183 test_LookupAccountName();
4184 test_security_descriptor();
4185 test_process_security();
4186 test_impersonation_level();
4187 test_SetEntriesInAclW();
4188 test_SetEntriesInAclA();
4189 test_GetNamedSecurityInfoA();
4190 test_ConvertStringSecurityDescriptor();
4191 test_ConvertSecurityDescriptorToString();
4192 test_PrivateObjectSecurity();
4194 test_GetSecurityInfo();
4195 test_GetSidSubAuthority();
4196 test_CheckTokenMembership();
4198 test_GetUserNameA();
4199 test_GetUserNameW();
4200 test_CreateRestrictedToken();