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 #ifndef EVENT_QUERY_STATE
42 #define EVENT_QUERY_STATE 0x0001
45 /* copied from Wine winternl.h - not included in the Windows SDK */
46 typedef enum _OBJECT_INFORMATION_CLASS
{
47 ObjectBasicInformation
,
48 ObjectNameInformation
,
49 ObjectTypeInformation
,
52 } OBJECT_INFORMATION_CLASS
, *POBJECT_INFORMATION_CLASS
;
54 typedef struct _OBJECT_BASIC_INFORMATION
{
56 ACCESS_MASK GrantedAccess
;
60 ULONG NonPagedPoolUsage
;
62 ULONG NameInformationLength
;
63 ULONG TypeInformationLength
;
64 ULONG SecurityDescriptorLength
;
65 LARGE_INTEGER CreateTime
;
66 } OBJECT_BASIC_INFORMATION
, *POBJECT_BASIC_INFORMATION
;
68 #define expect_eq(expr, value, type, format) { type ret = expr; ok((value) == ret, #expr " expected " format " got " format "\n", (value), (ret)); }
70 static BOOL (WINAPI
*pAddAccessAllowedAceEx
)(PACL
, DWORD
, DWORD
, DWORD
, PSID
);
71 static BOOL (WINAPI
*pAddAccessDeniedAceEx
)(PACL
, DWORD
, DWORD
, DWORD
, PSID
);
72 static BOOL (WINAPI
*pAddAuditAccessAceEx
)(PACL
, DWORD
, DWORD
, DWORD
, PSID
, BOOL
, BOOL
);
73 static VOID (WINAPI
*pBuildTrusteeWithSidA
)( PTRUSTEEA pTrustee
, PSID pSid
);
74 static VOID (WINAPI
*pBuildTrusteeWithNameA
)( PTRUSTEEA pTrustee
, LPSTR pName
);
75 static VOID (WINAPI
*pBuildTrusteeWithObjectsAndNameA
)( PTRUSTEEA pTrustee
,
76 POBJECTS_AND_NAME_A pObjName
,
77 SE_OBJECT_TYPE ObjectType
,
79 LPSTR InheritedObjectTypeName
,
81 static VOID (WINAPI
*pBuildTrusteeWithObjectsAndSidA
)( PTRUSTEEA pTrustee
,
82 POBJECTS_AND_SID pObjSid
,
84 GUID
* pInheritedObjectGuid
,
86 static LPSTR (WINAPI
*pGetTrusteeNameA
)( PTRUSTEEA pTrustee
);
87 static BOOL (WINAPI
*pMakeSelfRelativeSD
)( PSECURITY_DESCRIPTOR
, PSECURITY_DESCRIPTOR
, LPDWORD
);
88 static BOOL (WINAPI
*pConvertSidToStringSidA
)( PSID pSid
, LPSTR
*str
);
89 static BOOL (WINAPI
*pConvertStringSidToSidA
)( LPCSTR str
, PSID pSid
);
90 static BOOL (WINAPI
*pCheckTokenMembership
)(HANDLE
, PSID
, PBOOL
);
91 static BOOL (WINAPI
*pConvertStringSecurityDescriptorToSecurityDescriptorA
)(LPCSTR
, DWORD
,
92 PSECURITY_DESCRIPTOR
*, PULONG
);
93 static BOOL (WINAPI
*pConvertStringSecurityDescriptorToSecurityDescriptorW
)(LPCWSTR
, DWORD
,
94 PSECURITY_DESCRIPTOR
*, PULONG
);
95 static BOOL (WINAPI
*pConvertSecurityDescriptorToStringSecurityDescriptorA
)(PSECURITY_DESCRIPTOR
, DWORD
,
96 SECURITY_INFORMATION
, LPSTR
*, PULONG
);
97 static BOOL (WINAPI
*pGetFileSecurityA
)(LPCSTR
, SECURITY_INFORMATION
,
98 PSECURITY_DESCRIPTOR
, DWORD
, LPDWORD
);
99 static BOOL (WINAPI
*pSetFileSecurityA
)(LPCSTR
, SECURITY_INFORMATION
,
100 PSECURITY_DESCRIPTOR
);
101 static DWORD (WINAPI
*pGetNamedSecurityInfoA
)(LPSTR
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
102 PSID
*, PSID
*, PACL
*, PACL
*,
103 PSECURITY_DESCRIPTOR
*);
104 static PDWORD (WINAPI
*pGetSidSubAuthority
)(PSID
, DWORD
);
105 static PUCHAR (WINAPI
*pGetSidSubAuthorityCount
)(PSID
);
106 static BOOL (WINAPI
*pIsValidSid
)(PSID
);
107 static DWORD (WINAPI
*pRtlAdjustPrivilege
)(ULONG
,BOOLEAN
,BOOLEAN
,PBOOLEAN
);
108 static BOOL (WINAPI
*pCreateWellKnownSid
)(WELL_KNOWN_SID_TYPE
,PSID
,PSID
,DWORD
*);
109 static BOOL (WINAPI
*pDuplicateTokenEx
)(HANDLE
,DWORD
,LPSECURITY_ATTRIBUTES
,
110 SECURITY_IMPERSONATION_LEVEL
,TOKEN_TYPE
,PHANDLE
);
112 static NTSTATUS (WINAPI
*pLsaQueryInformationPolicy
)(LSA_HANDLE
,POLICY_INFORMATION_CLASS
,PVOID
*);
113 static NTSTATUS (WINAPI
*pLsaClose
)(LSA_HANDLE
);
114 static NTSTATUS (WINAPI
*pLsaFreeMemory
)(PVOID
);
115 static NTSTATUS (WINAPI
*pLsaOpenPolicy
)(PLSA_UNICODE_STRING
,PLSA_OBJECT_ATTRIBUTES
,ACCESS_MASK
,PLSA_HANDLE
);
116 static NTSTATUS (WINAPI
*pNtQueryObject
)(HANDLE
,OBJECT_INFORMATION_CLASS
,PVOID
,ULONG
,PULONG
);
117 static DWORD (WINAPI
*pSetEntriesInAclW
)(ULONG
, PEXPLICIT_ACCESSW
, PACL
, PACL
*);
118 static DWORD (WINAPI
*pSetEntriesInAclA
)(ULONG
, PEXPLICIT_ACCESSA
, PACL
, PACL
*);
119 static BOOL (WINAPI
*pSetSecurityDescriptorControl
)(PSECURITY_DESCRIPTOR
, SECURITY_DESCRIPTOR_CONTROL
,
120 SECURITY_DESCRIPTOR_CONTROL
);
121 static DWORD (WINAPI
*pGetSecurityInfo
)(HANDLE
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
122 PSID
*, PSID
*, PACL
*, PACL
*, PSECURITY_DESCRIPTOR
*);
123 static NTSTATUS (WINAPI
*pNtAccessCheck
)(PSECURITY_DESCRIPTOR
, HANDLE
, ACCESS_MASK
, PGENERIC_MAPPING
,
124 PPRIVILEGE_SET
, PULONG
, PULONG
, NTSTATUS
*);
125 static BOOL (WINAPI
*pCreateRestrictedToken
)(HANDLE
, DWORD
, DWORD
, PSID_AND_ATTRIBUTES
, DWORD
,
126 PLUID_AND_ATTRIBUTES
, DWORD
, PSID_AND_ATTRIBUTES
, PHANDLE
);
130 static char** myARGV
;
134 SID_IDENTIFIER_AUTHORITY auth
;
138 static void init(void)
142 hntdll
= GetModuleHandleA("ntdll.dll");
143 pNtQueryObject
= (void *)GetProcAddress( hntdll
, "NtQueryObject" );
144 pNtAccessCheck
= (void *)GetProcAddress( hntdll
, "NtAccessCheck" );
146 hmod
= GetModuleHandle("advapi32.dll");
147 pAddAccessAllowedAceEx
= (void *)GetProcAddress(hmod
, "AddAccessAllowedAceEx");
148 pAddAccessDeniedAceEx
= (void *)GetProcAddress(hmod
, "AddAccessDeniedAceEx");
149 pAddAuditAccessAceEx
= (void *)GetProcAddress(hmod
, "AddAuditAccessAceEx");
150 pCheckTokenMembership
= (void *)GetProcAddress(hmod
, "CheckTokenMembership");
151 pConvertStringSecurityDescriptorToSecurityDescriptorA
=
152 (void *)GetProcAddress(hmod
, "ConvertStringSecurityDescriptorToSecurityDescriptorA" );
153 pConvertStringSecurityDescriptorToSecurityDescriptorW
=
154 (void *)GetProcAddress(hmod
, "ConvertStringSecurityDescriptorToSecurityDescriptorW" );
155 pConvertSecurityDescriptorToStringSecurityDescriptorA
=
156 (void *)GetProcAddress(hmod
, "ConvertSecurityDescriptorToStringSecurityDescriptorA" );
157 pGetFileSecurityA
= (void *)GetProcAddress(hmod
, "GetFileSecurityA" );
158 pSetFileSecurityA
= (void *)GetProcAddress(hmod
, "SetFileSecurityA" );
159 pCreateWellKnownSid
= (void *)GetProcAddress( hmod
, "CreateWellKnownSid" );
160 pGetNamedSecurityInfoA
= (void *)GetProcAddress(hmod
, "GetNamedSecurityInfoA");
161 pGetSidSubAuthority
= (void *)GetProcAddress(hmod
, "GetSidSubAuthority");
162 pGetSidSubAuthorityCount
= (void *)GetProcAddress(hmod
, "GetSidSubAuthorityCount");
163 pIsValidSid
= (void *)GetProcAddress(hmod
, "IsValidSid");
164 pMakeSelfRelativeSD
= (void *)GetProcAddress(hmod
, "MakeSelfRelativeSD");
165 pSetEntriesInAclW
= (void *)GetProcAddress(hmod
, "SetEntriesInAclW");
166 pSetEntriesInAclA
= (void *)GetProcAddress(hmod
, "SetEntriesInAclA");
167 pSetSecurityDescriptorControl
= (void *)GetProcAddress(hmod
, "SetSecurityDescriptorControl");
168 pGetSecurityInfo
= (void *)GetProcAddress(hmod
, "GetSecurityInfo");
169 pCreateRestrictedToken
= (void *)GetProcAddress(hmod
, "CreateRestrictedToken");
171 myARGC
= winetest_get_mainargs( &myARGV
);
174 static void test_str_sid(const char *str_sid
)
179 if (pConvertStringSidToSidA(str_sid
, &psid
))
181 if (pConvertSidToStringSidA(psid
, &temp
))
183 trace(" %s: %s\n", str_sid
, temp
);
190 if (GetLastError() != ERROR_INVALID_SID
)
191 trace(" %s: couldn't be converted, returned %d\n", str_sid
, GetLastError());
193 trace(" %s: couldn't be converted\n", str_sid
);
197 static void test_sid(void)
199 struct sidRef refs
[] = {
200 { { {0x00,0x00,0x33,0x44,0x55,0x66} }, "S-1-860116326-1" },
201 { { {0x00,0x00,0x01,0x02,0x03,0x04} }, "S-1-16909060-1" },
202 { { {0x00,0x00,0x00,0x01,0x02,0x03} }, "S-1-66051-1" },
203 { { {0x00,0x00,0x00,0x00,0x01,0x02} }, "S-1-258-1" },
204 { { {0x00,0x00,0x00,0x00,0x00,0x02} }, "S-1-2-1" },
205 { { {0x00,0x00,0x00,0x00,0x00,0x0c} }, "S-1-12-1" },
207 const char noSubAuthStr
[] = "S-1-5";
214 pConvertSidToStringSidA
= (void *)GetProcAddress( hmod
, "ConvertSidToStringSidA" );
215 if( !pConvertSidToStringSidA
)
217 pConvertStringSidToSidA
= (void *)GetProcAddress( hmod
, "ConvertStringSidToSidA" );
218 if( !pConvertStringSidToSidA
)
221 r
= pConvertStringSidToSidA( NULL
, NULL
);
222 ok( !r
, "expected failure with NULL parameters\n" );
223 if( GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
225 ok( GetLastError() == ERROR_INVALID_PARAMETER
,
226 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
229 r
= pConvertStringSidToSidA( refs
[0].refStr
, NULL
);
230 ok( !r
&& GetLastError() == ERROR_INVALID_PARAMETER
,
231 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
234 r
= pConvertStringSidToSidA( NULL
, &str
);
235 ok( !r
&& GetLastError() == ERROR_INVALID_PARAMETER
,
236 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
239 r
= pConvertStringSidToSidA( noSubAuthStr
, &psid
);
241 "expected failure with no sub authorities\n" );
242 ok( GetLastError() == ERROR_INVALID_SID
,
243 "expected GetLastError() is ERROR_INVALID_SID, got %d\n",
246 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid
), "ConvertStringSidToSidA failed\n");
248 ok(pisid
->SubAuthorityCount
== 4, "Invalid sub authority count - expected 4, got %d\n", pisid
->SubAuthorityCount
);
249 ok(pisid
->SubAuthority
[0] == 21, "Invalid subauthority 0 - expected 21, got %d\n", pisid
->SubAuthority
[0]);
250 ok(pisid
->SubAuthority
[3] == 4576, "Invalid subauthority 0 - expected 4576, got %d\n", pisid
->SubAuthority
[3]);
254 for( i
= 0; i
< sizeof(refs
) / sizeof(refs
[0]); i
++ )
258 r
= AllocateAndInitializeSid( &refs
[i
].auth
, 1,1,0,0,0,0,0,0,0,
260 ok( r
, "failed to allocate sid\n" );
261 r
= pConvertSidToStringSidA( psid
, &str
);
262 ok( r
, "failed to convert sid\n" );
265 ok( !strcmp( str
, refs
[i
].refStr
),
266 "incorrect sid, expected %s, got %s\n", refs
[i
].refStr
, str
);
272 r
= pConvertStringSidToSidA( refs
[i
].refStr
, &psid
);
273 ok( r
, "failed to parse sid string\n" );
276 !memcmp( pisid
->IdentifierAuthority
.Value
, refs
[i
].auth
.Value
,
277 sizeof(refs
[i
].auth
) ),
278 "string sid %s didn't parse to expected value\n"
279 "(got 0x%04x%08x, expected 0x%04x%08x)\n",
281 MAKEWORD( pisid
->IdentifierAuthority
.Value
[1],
282 pisid
->IdentifierAuthority
.Value
[0] ),
283 MAKELONG( MAKEWORD( pisid
->IdentifierAuthority
.Value
[5],
284 pisid
->IdentifierAuthority
.Value
[4] ),
285 MAKEWORD( pisid
->IdentifierAuthority
.Value
[3],
286 pisid
->IdentifierAuthority
.Value
[2] ) ),
287 MAKEWORD( refs
[i
].auth
.Value
[1], refs
[i
].auth
.Value
[0] ),
288 MAKELONG( MAKEWORD( refs
[i
].auth
.Value
[5], refs
[i
].auth
.Value
[4] ),
289 MAKEWORD( refs
[i
].auth
.Value
[3], refs
[i
].auth
.Value
[2] ) ) );
294 trace("String SIDs:\n");
335 static void test_trustee(void)
337 GUID ObjectType
= {0x12345678, 0x1234, 0x5678, {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}};
338 GUID InheritedObjectType
= {0x23456789, 0x2345, 0x6786, {0x2, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}};
340 OBJECTS_AND_NAME_ oan
;
344 char szObjectTypeName
[] = "ObjectTypeName";
345 char szInheritedObjectTypeName
[] = "InheritedObjectTypeName";
346 char szTrusteeName
[] = "szTrusteeName";
347 SID_IDENTIFIER_AUTHORITY auth
= { {0x11,0x22,0,0,0, 0} };
349 memset( &ZeroGuid
, 0x00, sizeof (ZeroGuid
) );
351 pBuildTrusteeWithSidA
= (void *)GetProcAddress( hmod
, "BuildTrusteeWithSidA" );
352 pBuildTrusteeWithNameA
= (void *)GetProcAddress( hmod
, "BuildTrusteeWithNameA" );
353 pBuildTrusteeWithObjectsAndNameA
= (void *)GetProcAddress (hmod
, "BuildTrusteeWithObjectsAndNameA" );
354 pBuildTrusteeWithObjectsAndSidA
= (void *)GetProcAddress (hmod
, "BuildTrusteeWithObjectsAndSidA" );
355 pGetTrusteeNameA
= (void *)GetProcAddress (hmod
, "GetTrusteeNameA" );
356 if( !pBuildTrusteeWithSidA
|| !pBuildTrusteeWithNameA
||
357 !pBuildTrusteeWithObjectsAndNameA
|| !pBuildTrusteeWithObjectsAndSidA
||
361 if ( ! AllocateAndInitializeSid( &auth
, 1, 42, 0,0,0,0,0,0,0,&psid
) )
363 trace( "failed to init SID\n" );
367 /* test BuildTrusteeWithSidA */
368 memset( &trustee
, 0xff, sizeof trustee
);
369 pBuildTrusteeWithSidA( &trustee
, psid
);
371 ok( trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
372 ok( trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
,
373 "MultipleTrusteeOperation wrong\n");
374 ok( trustee
.TrusteeForm
== TRUSTEE_IS_SID
, "TrusteeForm wrong\n");
375 ok( trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
376 ok( trustee
.ptstrName
== psid
, "ptstrName wrong\n" );
378 /* test BuildTrusteeWithObjectsAndSidA (test 1) */
379 memset( &trustee
, 0xff, sizeof trustee
);
380 memset( &oas
, 0xff, sizeof(oas
) );
381 pBuildTrusteeWithObjectsAndSidA(&trustee
, &oas
, &ObjectType
,
382 &InheritedObjectType
, psid
);
384 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
385 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
386 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_SID
, "TrusteeForm wrong\n");
387 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
388 ok(trustee
.ptstrName
== (LPSTR
)&oas
, "ptstrName wrong\n");
390 ok(oas
.ObjectsPresent
== (ACE_OBJECT_TYPE_PRESENT
| ACE_INHERITED_OBJECT_TYPE_PRESENT
), "ObjectsPresent wrong\n");
391 ok(!memcmp(&oas
.ObjectTypeGuid
, &ObjectType
, sizeof(GUID
)), "ObjectTypeGuid wrong\n");
392 ok(!memcmp(&oas
.InheritedObjectTypeGuid
, &InheritedObjectType
, sizeof(GUID
)), "InheritedObjectTypeGuid wrong\n");
393 ok(oas
.pSid
== psid
, "pSid wrong\n");
395 /* test GetTrusteeNameA */
396 ok(pGetTrusteeNameA(&trustee
) == (LPSTR
)&oas
, "GetTrusteeName returned wrong value\n");
398 /* test BuildTrusteeWithObjectsAndSidA (test 2) */
399 memset( &trustee
, 0xff, sizeof trustee
);
400 memset( &oas
, 0xff, sizeof(oas
) );
401 pBuildTrusteeWithObjectsAndSidA(&trustee
, &oas
, NULL
,
402 &InheritedObjectType
, psid
);
404 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
405 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
406 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_SID
, "TrusteeForm wrong\n");
407 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
408 ok(trustee
.ptstrName
== (LPSTR
)&oas
, "ptstrName wrong\n");
410 ok(oas
.ObjectsPresent
== ACE_INHERITED_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
411 ok(!memcmp(&oas
.ObjectTypeGuid
, &ZeroGuid
, sizeof(GUID
)), "ObjectTypeGuid wrong\n");
412 ok(!memcmp(&oas
.InheritedObjectTypeGuid
, &InheritedObjectType
, sizeof(GUID
)), "InheritedObjectTypeGuid wrong\n");
413 ok(oas
.pSid
== psid
, "pSid wrong\n");
417 /* test BuildTrusteeWithNameA */
418 memset( &trustee
, 0xff, sizeof trustee
);
419 pBuildTrusteeWithNameA( &trustee
, szTrusteeName
);
421 ok( trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
422 ok( trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
,
423 "MultipleTrusteeOperation wrong\n");
424 ok( trustee
.TrusteeForm
== TRUSTEE_IS_NAME
, "TrusteeForm wrong\n");
425 ok( trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
426 ok( trustee
.ptstrName
== szTrusteeName
, "ptstrName wrong\n" );
428 /* test BuildTrusteeWithObjectsAndNameA (test 1) */
429 memset( &trustee
, 0xff, sizeof trustee
);
430 memset( &oan
, 0xff, sizeof(oan
) );
431 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, szObjectTypeName
,
432 szInheritedObjectTypeName
, szTrusteeName
);
434 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
435 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
436 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
437 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
438 ok(trustee
.ptstrName
== (LPTSTR
)&oan
, "ptstrName wrong\n");
440 ok(oan
.ObjectsPresent
== (ACE_OBJECT_TYPE_PRESENT
| ACE_INHERITED_OBJECT_TYPE_PRESENT
), "ObjectsPresent wrong\n");
441 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
442 ok(oan
.InheritedObjectTypeName
== szInheritedObjectTypeName
, "InheritedObjectTypeName wrong\n");
443 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
445 /* test GetTrusteeNameA */
446 ok(pGetTrusteeNameA(&trustee
) == (LPSTR
)&oan
, "GetTrusteeName returned wrong value\n");
448 /* test BuildTrusteeWithObjectsAndNameA (test 2) */
449 memset( &trustee
, 0xff, sizeof trustee
);
450 memset( &oan
, 0xff, sizeof(oan
) );
451 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, NULL
,
452 szInheritedObjectTypeName
, szTrusteeName
);
454 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
455 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
456 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
457 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
458 ok(trustee
.ptstrName
== (LPSTR
)&oan
, "ptstrName wrong\n");
460 ok(oan
.ObjectsPresent
== ACE_INHERITED_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
461 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
462 ok(oan
.InheritedObjectTypeName
== szInheritedObjectTypeName
, "InheritedObjectTypeName wrong\n");
463 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
465 /* test BuildTrusteeWithObjectsAndNameA (test 3) */
466 memset( &trustee
, 0xff, sizeof trustee
);
467 memset( &oan
, 0xff, sizeof(oan
) );
468 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, szObjectTypeName
,
469 NULL
, szTrusteeName
);
471 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
472 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
473 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
474 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
475 ok(trustee
.ptstrName
== (LPTSTR
)&oan
, "ptstrName wrong\n");
477 ok(oan
.ObjectsPresent
== ACE_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
478 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
479 ok(oan
.InheritedObjectTypeName
== NULL
, "InheritedObjectTypeName wrong\n");
480 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
483 /* If the first isn't defined, assume none is */
484 #ifndef SE_MIN_WELL_KNOWN_PRIVILEGE
485 #define SE_MIN_WELL_KNOWN_PRIVILEGE 2L
486 #define SE_CREATE_TOKEN_PRIVILEGE 2L
487 #define SE_ASSIGNPRIMARYTOKEN_PRIVILEGE 3L
488 #define SE_LOCK_MEMORY_PRIVILEGE 4L
489 #define SE_INCREASE_QUOTA_PRIVILEGE 5L
490 #define SE_MACHINE_ACCOUNT_PRIVILEGE 6L
491 #define SE_TCB_PRIVILEGE 7L
492 #define SE_SECURITY_PRIVILEGE 8L
493 #define SE_TAKE_OWNERSHIP_PRIVILEGE 9L
494 #define SE_LOAD_DRIVER_PRIVILEGE 10L
495 #define SE_SYSTEM_PROFILE_PRIVILEGE 11L
496 #define SE_SYSTEMTIME_PRIVILEGE 12L
497 #define SE_PROF_SINGLE_PROCESS_PRIVILEGE 13L
498 #define SE_INC_BASE_PRIORITY_PRIVILEGE 14L
499 #define SE_CREATE_PAGEFILE_PRIVILEGE 15L
500 #define SE_CREATE_PERMANENT_PRIVILEGE 16L
501 #define SE_BACKUP_PRIVILEGE 17L
502 #define SE_RESTORE_PRIVILEGE 18L
503 #define SE_SHUTDOWN_PRIVILEGE 19L
504 #define SE_DEBUG_PRIVILEGE 20L
505 #define SE_AUDIT_PRIVILEGE 21L
506 #define SE_SYSTEM_ENVIRONMENT_PRIVILEGE 22L
507 #define SE_CHANGE_NOTIFY_PRIVILEGE 23L
508 #define SE_REMOTE_SHUTDOWN_PRIVILEGE 24L
509 #define SE_UNDOCK_PRIVILEGE 25L
510 #define SE_SYNC_AGENT_PRIVILEGE 26L
511 #define SE_ENABLE_DELEGATION_PRIVILEGE 27L
512 #define SE_MANAGE_VOLUME_PRIVILEGE 28L
513 #define SE_IMPERSONATE_PRIVILEGE 29L
514 #define SE_CREATE_GLOBAL_PRIVILEGE 30L
515 #define SE_MAX_WELL_KNOWN_PRIVILEGE SE_CREATE_GLOBAL_PRIVILEGE
516 #endif /* ndef SE_MIN_WELL_KNOWN_PRIVILEGE */
518 static void test_allocateLuid(void)
520 BOOL (WINAPI
*pAllocateLocallyUniqueId
)(PLUID
);
524 pAllocateLocallyUniqueId
= (void*)GetProcAddress(hmod
, "AllocateLocallyUniqueId");
525 if (!pAllocateLocallyUniqueId
) return;
527 ret
= pAllocateLocallyUniqueId(&luid1
);
528 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
532 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
533 ret
= pAllocateLocallyUniqueId(&luid2
);
535 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
536 ok(luid1
.LowPart
> SE_MAX_WELL_KNOWN_PRIVILEGE
|| luid1
.HighPart
!= 0,
537 "AllocateLocallyUniqueId returned a well-known LUID\n");
538 ok(luid1
.LowPart
!= luid2
.LowPart
|| luid1
.HighPart
!= luid2
.HighPart
,
539 "AllocateLocallyUniqueId returned non-unique LUIDs\n");
540 ret
= pAllocateLocallyUniqueId(NULL
);
541 ok( !ret
&& GetLastError() == ERROR_NOACCESS
,
542 "AllocateLocallyUniqueId(NULL) didn't return ERROR_NOACCESS: %d\n",
546 static void test_lookupPrivilegeName(void)
548 BOOL (WINAPI
*pLookupPrivilegeNameA
)(LPCSTR
, PLUID
, LPSTR
, LPDWORD
);
549 char buf
[MAX_PATH
]; /* arbitrary, seems long enough */
550 DWORD cchName
= sizeof(buf
);
551 LUID luid
= { 0, 0 };
555 /* check whether it's available first */
556 pLookupPrivilegeNameA
= (void*)GetProcAddress(hmod
, "LookupPrivilegeNameA");
557 if (!pLookupPrivilegeNameA
) return;
558 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
559 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
560 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
563 /* check with a short buffer */
565 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
566 ret
= pLookupPrivilegeNameA(NULL
, &luid
, NULL
, &cchName
);
567 ok( !ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
568 "LookupPrivilegeNameA didn't fail with ERROR_INSUFFICIENT_BUFFER: %d\n",
570 ok(cchName
== strlen("SeCreateTokenPrivilege") + 1,
571 "LookupPrivilegeNameA returned an incorrect required length for\n"
572 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName
,
573 lstrlenA("SeCreateTokenPrivilege") + 1);
574 /* check a known value and its returned length on success */
575 cchName
= sizeof(buf
);
576 ok(pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
) &&
577 cchName
== strlen("SeCreateTokenPrivilege"),
578 "LookupPrivilegeNameA returned an incorrect output length for\n"
579 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName
,
580 (int)strlen("SeCreateTokenPrivilege"));
581 /* check known values */
582 for (i
= SE_MIN_WELL_KNOWN_PRIVILEGE
; i
<= SE_MAX_WELL_KNOWN_PRIVILEGE
; i
++)
585 cchName
= sizeof(buf
);
586 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
587 ok( ret
|| GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
588 "LookupPrivilegeNameA(0.%d) failed: %d\n", i
, GetLastError());
590 /* check a bogus LUID */
591 luid
.LowPart
= 0xdeadbeef;
592 cchName
= sizeof(buf
);
593 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
594 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
595 "LookupPrivilegeNameA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
597 /* check on a bogus system */
598 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
599 cchName
= sizeof(buf
);
600 ret
= pLookupPrivilegeNameA("b0gu5.Nam3", &luid
, buf
, &cchName
);
601 ok( !ret
&& (GetLastError() == RPC_S_SERVER_UNAVAILABLE
||
602 GetLastError() == RPC_S_INVALID_NET_ADDR
) /* w2k8 */,
603 "LookupPrivilegeNameA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
613 static void test_lookupPrivilegeValue(void)
615 static const struct NameToLUID privs
[] = {
616 { "SeCreateTokenPrivilege", SE_CREATE_TOKEN_PRIVILEGE
},
617 { "SeAssignPrimaryTokenPrivilege", SE_ASSIGNPRIMARYTOKEN_PRIVILEGE
},
618 { "SeLockMemoryPrivilege", SE_LOCK_MEMORY_PRIVILEGE
},
619 { "SeIncreaseQuotaPrivilege", SE_INCREASE_QUOTA_PRIVILEGE
},
620 { "SeMachineAccountPrivilege", SE_MACHINE_ACCOUNT_PRIVILEGE
},
621 { "SeTcbPrivilege", SE_TCB_PRIVILEGE
},
622 { "SeSecurityPrivilege", SE_SECURITY_PRIVILEGE
},
623 { "SeTakeOwnershipPrivilege", SE_TAKE_OWNERSHIP_PRIVILEGE
},
624 { "SeLoadDriverPrivilege", SE_LOAD_DRIVER_PRIVILEGE
},
625 { "SeSystemProfilePrivilege", SE_SYSTEM_PROFILE_PRIVILEGE
},
626 { "SeSystemtimePrivilege", SE_SYSTEMTIME_PRIVILEGE
},
627 { "SeProfileSingleProcessPrivilege", SE_PROF_SINGLE_PROCESS_PRIVILEGE
},
628 { "SeIncreaseBasePriorityPrivilege", SE_INC_BASE_PRIORITY_PRIVILEGE
},
629 { "SeCreatePagefilePrivilege", SE_CREATE_PAGEFILE_PRIVILEGE
},
630 { "SeCreatePermanentPrivilege", SE_CREATE_PERMANENT_PRIVILEGE
},
631 { "SeBackupPrivilege", SE_BACKUP_PRIVILEGE
},
632 { "SeRestorePrivilege", SE_RESTORE_PRIVILEGE
},
633 { "SeShutdownPrivilege", SE_SHUTDOWN_PRIVILEGE
},
634 { "SeDebugPrivilege", SE_DEBUG_PRIVILEGE
},
635 { "SeAuditPrivilege", SE_AUDIT_PRIVILEGE
},
636 { "SeSystemEnvironmentPrivilege", SE_SYSTEM_ENVIRONMENT_PRIVILEGE
},
637 { "SeChangeNotifyPrivilege", SE_CHANGE_NOTIFY_PRIVILEGE
},
638 { "SeRemoteShutdownPrivilege", SE_REMOTE_SHUTDOWN_PRIVILEGE
},
639 { "SeUndockPrivilege", SE_UNDOCK_PRIVILEGE
},
640 { "SeSyncAgentPrivilege", SE_SYNC_AGENT_PRIVILEGE
},
641 { "SeEnableDelegationPrivilege", SE_ENABLE_DELEGATION_PRIVILEGE
},
642 { "SeManageVolumePrivilege", SE_MANAGE_VOLUME_PRIVILEGE
},
643 { "SeImpersonatePrivilege", SE_IMPERSONATE_PRIVILEGE
},
644 { "SeCreateGlobalPrivilege", SE_CREATE_GLOBAL_PRIVILEGE
},
646 BOOL (WINAPI
*pLookupPrivilegeValueA
)(LPCSTR
, LPCSTR
, PLUID
);
651 /* check whether it's available first */
652 pLookupPrivilegeValueA
= (void*)GetProcAddress(hmod
, "LookupPrivilegeValueA");
653 if (!pLookupPrivilegeValueA
) return;
654 ret
= pLookupPrivilegeValueA(NULL
, "SeCreateTokenPrivilege", &luid
);
655 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
658 /* check a bogus system name */
659 ret
= pLookupPrivilegeValueA("b0gu5.Nam3", "SeCreateTokenPrivilege", &luid
);
660 ok( !ret
&& (GetLastError() == RPC_S_SERVER_UNAVAILABLE
||
661 GetLastError() == RPC_S_INVALID_NET_ADDR
) /* w2k8 */,
662 "LookupPrivilegeValueA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
664 /* check a NULL string */
665 ret
= pLookupPrivilegeValueA(NULL
, 0, &luid
);
666 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
667 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
669 /* check a bogus privilege name */
670 ret
= pLookupPrivilegeValueA(NULL
, "SeBogusPrivilege", &luid
);
671 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
672 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
674 /* check case insensitive */
675 ret
= pLookupPrivilegeValueA(NULL
, "sEcREATEtOKENpRIVILEGE", &luid
);
677 "LookupPrivilegeValueA(NULL, sEcREATEtOKENpRIVILEGE, &luid) failed: %d\n",
679 for (i
= 0; i
< sizeof(privs
) / sizeof(privs
[0]); i
++)
681 /* Not all privileges are implemented on all Windows versions, so
682 * don't worry if the call fails
684 if (pLookupPrivilegeValueA(NULL
, privs
[i
].name
, &luid
))
686 ok(luid
.LowPart
== privs
[i
].lowPart
,
687 "LookupPrivilegeValueA returned an invalid LUID for %s\n",
693 static void test_luid(void)
696 test_lookupPrivilegeName();
697 test_lookupPrivilegeValue();
700 static void test_FileSecurity(void)
702 char wintmpdir
[MAX_PATH
];
703 char path
[MAX_PATH
];
704 char file
[MAX_PATH
];
706 DWORD sdSize
, retSize
, rc
, granted
, priv_set_len
;
707 PRIVILEGE_SET priv_set
;
710 GENERIC_MAPPING mapping
= { FILE_READ_DATA
, FILE_WRITE_DATA
, FILE_EXECUTE
, FILE_ALL_ACCESS
};
711 const SECURITY_INFORMATION request
= OWNER_SECURITY_INFORMATION
712 | GROUP_SECURITY_INFORMATION
713 | DACL_SECURITY_INFORMATION
;
715 if (!pGetFileSecurityA
) {
716 win_skip ("GetFileSecurity is not available\n");
720 if (!pSetFileSecurityA
) {
721 win_skip ("SetFileSecurity is not available\n");
725 if (!GetTempPathA (sizeof (wintmpdir
), wintmpdir
)) {
726 win_skip ("GetTempPathA failed\n");
730 /* Create a temporary directory and in it a temporary file */
731 strcat (strcpy (path
, wintmpdir
), "rary");
732 SetLastError(0xdeadbeef);
733 rc
= CreateDirectoryA (path
, NULL
);
734 ok (rc
|| GetLastError() == ERROR_ALREADY_EXISTS
, "CreateDirectoryA "
735 "failed for '%s' with %d\n", path
, GetLastError());
737 strcat (strcpy (file
, path
), "\\ess");
738 SetLastError(0xdeadbeef);
739 fh
= CreateFileA (file
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, NULL
);
740 ok (fh
!= INVALID_HANDLE_VALUE
, "CreateFileA "
741 "failed for '%s' with %d\n", file
, GetLastError());
744 /* For the temporary file ... */
746 /* Get size needed */
748 SetLastError(0xdeadbeef);
749 rc
= pGetFileSecurityA (file
, request
, NULL
, 0, &retSize
);
750 if (!rc
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)) {
751 win_skip("GetFileSecurityA is not implemented\n");
754 ok (!rc
, "GetFileSecurityA "
755 "was expected to fail for '%s'\n", file
);
756 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetFileSecurityA "
757 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
758 ok (retSize
> sizeof (SECURITY_DESCRIPTOR
), "GetFileSecurityA returned size %d\n", retSize
);
761 sd
= HeapAlloc (GetProcessHeap (), 0, sdSize
);
763 /* Get security descriptor for real */
765 SetLastError(0xdeadbeef);
766 rc
= pGetFileSecurityA (file
, request
, sd
, sdSize
, &retSize
);
767 ok (rc
, "GetFileSecurityA "
768 "was not expected to fail '%s': %d\n", file
, GetLastError());
769 ok (retSize
== sdSize
||
770 broken(retSize
== 0), /* NT4 */
771 "GetFileSecurityA returned size %d; expected %d\n", retSize
, sdSize
);
773 /* Use it to set security descriptor */
774 SetLastError(0xdeadbeef);
775 rc
= pSetFileSecurityA (file
, request
, sd
);
776 ok (rc
, "SetFileSecurityA "
777 "was not expected to fail '%s': %d\n", file
, GetLastError());
779 HeapFree (GetProcessHeap (), 0, sd
);
781 /* Repeat for the temporary directory ... */
783 /* Get size needed */
785 SetLastError(0xdeadbeef);
786 rc
= pGetFileSecurityA (path
, request
, NULL
, 0, &retSize
);
787 ok (!rc
, "GetFileSecurityA "
788 "was expected to fail for '%s'\n", path
);
789 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetFileSecurityA "
790 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
791 ok (retSize
> sizeof (SECURITY_DESCRIPTOR
), "GetFileSecurityA returned size %d\n", retSize
);
794 sd
= HeapAlloc (GetProcessHeap (), 0, sdSize
);
796 /* Get security descriptor for real */
798 SetLastError(0xdeadbeef);
799 rc
= pGetFileSecurityA (path
, request
, sd
, sdSize
, &retSize
);
800 ok (rc
, "GetFileSecurityA "
801 "was not expected to fail '%s': %d\n", path
, GetLastError());
802 ok (retSize
== sdSize
||
803 broken(retSize
== 0), /* NT4 */
804 "GetFileSecurityA returned size %d; expected %d\n", retSize
, sdSize
);
806 /* Use it to set security descriptor */
807 SetLastError(0xdeadbeef);
808 rc
= pSetFileSecurityA (path
, request
, sd
);
809 ok (rc
, "SetFileSecurityA "
810 "was not expected to fail '%s': %d\n", path
, GetLastError());
811 HeapFree (GetProcessHeap (), 0, sd
);
814 strcpy (wintmpdir
, "\\Should not exist");
815 SetLastError(0xdeadbeef);
816 rc
= pGetFileSecurityA (wintmpdir
, OWNER_SECURITY_INFORMATION
, NULL
, 0, &sdSize
);
817 ok (!rc
, "GetFileSecurityA should fail for not existing directories/files\n");
818 ok (GetLastError() == ERROR_FILE_NOT_FOUND
,
819 "last error ERROR_FILE_NOT_FOUND expected, got %d\n", GetLastError());
822 /* Remove temporary file and directory */
824 RemoveDirectoryA(path
);
826 /* Test file access permissions for a file with FILE_ATTRIBUTE_ARCHIVE */
827 SetLastError(0xdeadbeef);
828 rc
= GetTempPath(sizeof(wintmpdir
), wintmpdir
);
829 ok(rc
, "GetTempPath error %d\n", GetLastError());
831 SetLastError(0xdeadbeef);
832 rc
= GetTempFileName(wintmpdir
, "tmp", 0, file
);
833 ok(rc
, "GetTempFileName error %d\n", GetLastError());
835 rc
= GetFileAttributes(file
);
836 rc
&= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
;
837 ok(rc
== FILE_ATTRIBUTE_ARCHIVE
, "expected FILE_ATTRIBUTE_ARCHIVE got %#x\n", rc
);
839 retSize
= 0xdeadbeef;
840 rc
= GetFileSecurity(file
, OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
842 ok(!rc
, "GetFileSecurity should fail\n");
843 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
844 "expected ERROR_INSUFFICIENT_BUFFER got %d\n", GetLastError());
845 ok(sdSize
> sizeof(SECURITY_DESCRIPTOR
), "got sd size %d\n", sdSize
);
847 sd
= HeapAlloc(GetProcessHeap (), 0, sdSize
);
848 retSize
= 0xdeadbeef;
849 SetLastError(0xdeadbeef);
850 rc
= GetFileSecurity(file
, OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
851 sd
, sdSize
, &retSize
);
852 ok(rc
, "GetFileSecurity error %d\n", GetLastError());
853 ok(retSize
== sdSize
|| broken(retSize
== 0) /* NT4 */, "expected %d, got %d\n", sdSize
, retSize
);
855 SetLastError(0xdeadbeef);
856 rc
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
, TRUE
, &token
);
857 ok(!rc
, "OpenThreadToken should fail\n");
858 ok(GetLastError() == ERROR_NO_TOKEN
, "expected ERROR_NO_TOKEN, got %d\n", GetLastError());
860 SetLastError(0xdeadbeef);
861 rc
= ImpersonateSelf(SecurityIdentification
);
862 ok(rc
, "ImpersonateSelf error %d\n", GetLastError());
864 SetLastError(0xdeadbeef);
865 rc
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
, TRUE
, &token
);
866 ok(rc
, "OpenThreadToken error %d\n", GetLastError());
868 SetLastError(0xdeadbeef);
870 ok(rc
, "RevertToSelf error %d\n", GetLastError());
872 priv_set_len
= sizeof(priv_set
);
873 granted
= 0xdeadbeef;
875 SetLastError(0xdeadbeef);
876 rc
= AccessCheck(sd
, token
, FILE_READ_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
877 ok(rc
, "AccessCheck error %d\n", GetLastError());
878 ok(status
== 1, "expected 1, got %d\n", status
);
879 ok(granted
== FILE_READ_DATA
, "expected FILE_READ_DATA, got %#x\n", granted
);
881 granted
= 0xdeadbeef;
883 SetLastError(0xdeadbeef);
884 rc
= AccessCheck(sd
, token
, FILE_WRITE_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
885 ok(rc
, "AccessCheck error %d\n", GetLastError());
886 ok(status
== 1, "expected 1, got %d\n", status
);
887 ok(granted
== FILE_WRITE_DATA
, "expected FILE_WRITE_DATA, got %#x\n", granted
);
889 granted
= 0xdeadbeef;
891 SetLastError(0xdeadbeef);
892 rc
= AccessCheck(sd
, token
, FILE_EXECUTE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
893 ok(rc
, "AccessCheck error %d\n", GetLastError());
894 ok(status
== 1, "expected 1, got %d\n", status
);
895 ok(granted
== FILE_EXECUTE
, "expected FILE_EXECUTE, got %#x\n", granted
);
897 granted
= 0xdeadbeef;
899 SetLastError(0xdeadbeef);
900 rc
= AccessCheck(sd
, token
, DELETE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
901 ok(rc
, "AccessCheck error %d\n", GetLastError());
902 ok(status
== 1, "expected 1, got %d\n", status
);
903 ok(granted
== DELETE
, "expected DELETE, got %#x\n", granted
);
905 granted
= 0xdeadbeef;
907 SetLastError(0xdeadbeef);
908 rc
= AccessCheck(sd
, token
, FILE_DELETE_CHILD
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
909 ok(rc
, "AccessCheck error %d\n", GetLastError());
910 ok(status
== 1, "expected 1, got %d\n", status
);
911 ok(granted
== FILE_DELETE_CHILD
, "expected FILE_DELETE_CHILD, got %#x\n", granted
);
913 granted
= 0xdeadbeef;
915 SetLastError(0xdeadbeef);
916 rc
= AccessCheck(sd
, token
, 0x1ff, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
917 ok(rc
, "AccessCheck error %d\n", GetLastError());
918 ok(status
== 1, "expected 1, got %d\n", status
);
919 ok(granted
== 0x1ff, "expected 0x1ff, got %#x\n", granted
);
921 granted
= 0xdeadbeef;
923 SetLastError(0xdeadbeef);
924 rc
= AccessCheck(sd
, token
, FILE_ALL_ACCESS
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
925 ok(rc
, "AccessCheck error %d\n", GetLastError());
926 ok(status
== 1, "expected 1, got %d\n", status
);
927 ok(granted
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", granted
);
929 SetLastError(0xdeadbeef);
930 rc
= AccessCheck(sd
, token
, 0xffffffff, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
931 ok(!rc
, "AccessCheck should fail\n");
932 ok(GetLastError() == ERROR_GENERIC_NOT_MAPPED
, "expected ERROR_GENERIC_NOT_MAPPED, got %d\n", GetLastError());
934 /* Test file access permissions for a file with FILE_ATTRIBUTE_READONLY */
935 SetLastError(0xdeadbeef);
936 fh
= CreateFile(file
, FILE_READ_DATA
, FILE_SHARE_READ
, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_READONLY
, 0);
937 ok(fh
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
938 retSize
= 0xdeadbeef;
939 SetLastError(0xdeadbeef);
940 rc
= WriteFile(fh
, "1", 1, &retSize
, NULL
);
941 ok(!rc
, "WriteFile should fail\n");
942 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
943 ok(retSize
== 0, "expected 0, got %d\n", retSize
);
946 rc
= GetFileAttributes(file
);
947 rc
&= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
;
949 ok(rc
== (FILE_ATTRIBUTE_ARCHIVE
|FILE_ATTRIBUTE_READONLY
),
950 "expected FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY got %#x\n", rc
);
952 SetLastError(0xdeadbeef);
953 rc
= SetFileAttributes(file
, FILE_ATTRIBUTE_ARCHIVE
);
954 ok(rc
, "SetFileAttributes error %d\n", GetLastError());
955 SetLastError(0xdeadbeef);
956 rc
= DeleteFile(file
);
957 ok(rc
, "DeleteFile error %d\n", GetLastError());
959 SetLastError(0xdeadbeef);
960 fh
= CreateFile(file
, FILE_READ_DATA
, FILE_SHARE_READ
, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_READONLY
, 0);
961 ok(fh
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
962 retSize
= 0xdeadbeef;
963 SetLastError(0xdeadbeef);
964 rc
= WriteFile(fh
, "1", 1, &retSize
, NULL
);
965 ok(!rc
, "WriteFile should fail\n");
966 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
967 ok(retSize
== 0, "expected 0, got %d\n", retSize
);
970 rc
= GetFileAttributes(file
);
971 rc
&= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
;
972 ok(rc
== (FILE_ATTRIBUTE_ARCHIVE
|FILE_ATTRIBUTE_READONLY
),
973 "expected FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY got %#x\n", rc
);
975 retSize
= 0xdeadbeef;
976 SetLastError(0xdeadbeef);
977 rc
= GetFileSecurity(file
, OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
978 sd
, sdSize
, &retSize
);
979 ok(rc
, "GetFileSecurity error %d\n", GetLastError());
980 ok(retSize
== sdSize
|| broken(retSize
== 0) /* NT4 */, "expected %d, got %d\n", sdSize
, retSize
);
982 priv_set_len
= sizeof(priv_set
);
983 granted
= 0xdeadbeef;
985 SetLastError(0xdeadbeef);
986 rc
= AccessCheck(sd
, token
, FILE_READ_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
987 ok(rc
, "AccessCheck error %d\n", GetLastError());
988 ok(status
== 1, "expected 1, got %d\n", status
);
989 ok(granted
== FILE_READ_DATA
, "expected FILE_READ_DATA, got %#x\n", granted
);
991 granted
= 0xdeadbeef;
993 SetLastError(0xdeadbeef);
994 rc
= AccessCheck(sd
, token
, FILE_WRITE_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
995 ok(rc
, "AccessCheck error %d\n", GetLastError());
997 ok(status
== 1, "expected 1, got %d\n", status
);
998 ok(granted
== FILE_WRITE_DATA
, "expected FILE_WRITE_DATA, got %#x\n", granted
);
1000 granted
= 0xdeadbeef;
1001 status
= 0xdeadbeef;
1002 SetLastError(0xdeadbeef);
1003 rc
= AccessCheck(sd
, token
, FILE_EXECUTE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1004 ok(rc
, "AccessCheck error %d\n", GetLastError());
1005 ok(status
== 1, "expected 1, got %d\n", status
);
1006 ok(granted
== FILE_EXECUTE
, "expected FILE_EXECUTE, got %#x\n", granted
);
1008 granted
= 0xdeadbeef;
1009 status
= 0xdeadbeef;
1010 SetLastError(0xdeadbeef);
1011 rc
= AccessCheck(sd
, token
, DELETE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1012 ok(rc
, "AccessCheck error %d\n", GetLastError());
1014 ok(status
== 1, "expected 1, got %d\n", status
);
1015 ok(granted
== DELETE
, "expected DELETE, got %#x\n", granted
);
1017 granted
= 0xdeadbeef;
1018 status
= 0xdeadbeef;
1019 SetLastError(0xdeadbeef);
1020 rc
= AccessCheck(sd
, token
, FILE_DELETE_CHILD
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1021 ok(rc
, "AccessCheck error %d\n", GetLastError());
1023 ok(status
== 1, "expected 1, got %d\n", status
);
1024 ok(granted
== FILE_DELETE_CHILD
, "expected FILE_DELETE_CHILD, got %#x\n", granted
);
1026 granted
= 0xdeadbeef;
1027 status
= 0xdeadbeef;
1028 SetLastError(0xdeadbeef);
1029 rc
= AccessCheck(sd
, token
, 0x1ff, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1030 ok(rc
, "AccessCheck error %d\n", GetLastError());
1032 ok(status
== 1, "expected 1, got %d\n", status
);
1033 ok(granted
== 0x1ff, "expected 0x1ff, got %#x\n", granted
);
1035 granted
= 0xdeadbeef;
1036 status
= 0xdeadbeef;
1037 SetLastError(0xdeadbeef);
1038 rc
= AccessCheck(sd
, token
, FILE_ALL_ACCESS
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1039 ok(rc
, "AccessCheck error %d\n", GetLastError());
1041 ok(status
== 1, "expected 1, got %d\n", status
);
1042 ok(granted
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", granted
);
1044 SetLastError(0xdeadbeef);
1045 rc
= DeleteFile(file
);
1046 ok(!rc
, "DeleteFile should fail\n");
1047 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1048 SetLastError(0xdeadbeef);
1049 rc
= SetFileAttributes(file
, FILE_ATTRIBUTE_ARCHIVE
);
1050 ok(rc
, "SetFileAttributes error %d\n", GetLastError());
1051 SetLastError(0xdeadbeef);
1052 rc
= DeleteFile(file
);
1053 ok(rc
, "DeleteFile error %d\n", GetLastError());
1056 HeapFree(GetProcessHeap(), 0, sd
);
1059 static void test_AccessCheck(void)
1061 PSID EveryoneSid
= NULL
, AdminSid
= NULL
, UsersSid
= NULL
;
1063 SECURITY_DESCRIPTOR
*SecurityDescriptor
= NULL
;
1064 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
1065 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
1066 GENERIC_MAPPING Mapping
= { KEY_READ
, KEY_WRITE
, KEY_EXECUTE
, KEY_ALL_ACCESS
};
1070 HANDLE ProcessToken
;
1073 PRIVILEGE_SET
*PrivSet
;
1075 HMODULE NtDllModule
;
1078 NTSTATUS ntret
, ntAccessStatus
;
1080 NtDllModule
= GetModuleHandle("ntdll.dll");
1083 skip("not running on NT, skipping test\n");
1086 pRtlAdjustPrivilege
= (void *)GetProcAddress(NtDllModule
, "RtlAdjustPrivilege");
1087 if (!pRtlAdjustPrivilege
)
1089 win_skip("missing RtlAdjustPrivilege, skipping test\n");
1093 Acl
= HeapAlloc(GetProcessHeap(), 0, 256);
1094 res
= InitializeAcl(Acl
, 256, ACL_REVISION
);
1095 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
1097 skip("ACLs not implemented - skipping tests\n");
1098 HeapFree(GetProcessHeap(), 0, Acl
);
1101 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
1103 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
1104 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1106 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1107 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &AdminSid
);
1108 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1110 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1111 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
1112 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1114 SecurityDescriptor
= HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
1116 res
= InitializeSecurityDescriptor(SecurityDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
1117 ok(res
, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
1119 res
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
1120 ok(res
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1122 PrivSetLen
= FIELD_OFFSET(PRIVILEGE_SET
, Privilege
[16]);
1123 PrivSet
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, PrivSetLen
);
1124 PrivSet
->PrivilegeCount
= 16;
1126 res
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &ProcessToken
);
1127 ok(res
, "OpenProcessToken failed with error %d\n", GetLastError());
1129 pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE
, FALSE
, TRUE
, &Enabled
);
1131 res
= DuplicateToken(ProcessToken
, SecurityImpersonation
, &Token
);
1132 ok(res
, "DuplicateToken failed with error %d\n", GetLastError());
1134 /* SD without owner/group */
1135 SetLastError(0xdeadbeef);
1136 Access
= AccessStatus
= 0x1abe11ed;
1137 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_QUERY_VALUE
, &Mapping
,
1138 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1139 err
= GetLastError();
1140 ok(!ret
&& err
== ERROR_INVALID_SECURITY_DESCR
, "AccessCheck should have "
1141 "failed with ERROR_INVALID_SECURITY_DESCR, instead of %d\n", err
);
1142 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1143 "Access and/or AccessStatus were changed!\n");
1145 /* Set owner and group */
1146 res
= SetSecurityDescriptorOwner(SecurityDescriptor
, AdminSid
, FALSE
);
1147 ok(res
, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
1148 res
= SetSecurityDescriptorGroup(SecurityDescriptor
, UsersSid
, TRUE
);
1149 ok(res
, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
1151 /* Generic access mask */
1152 SetLastError(0xdeadbeef);
1153 Access
= AccessStatus
= 0x1abe11ed;
1154 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1155 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1156 err
= GetLastError();
1157 ok(!ret
&& err
== ERROR_GENERIC_NOT_MAPPED
, "AccessCheck should have failed "
1158 "with ERROR_GENERIC_NOT_MAPPED, instead of %d\n", err
);
1159 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1160 "Access and/or AccessStatus were changed!\n");
1162 /* Generic access mask - no privilegeset buffer */
1163 SetLastError(0xdeadbeef);
1164 Access
= AccessStatus
= 0x1abe11ed;
1165 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1166 NULL
, &PrivSetLen
, &Access
, &AccessStatus
);
1167 err
= GetLastError();
1168 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1169 "with ERROR_NOACCESS, instead of %d\n", err
);
1170 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1171 "Access and/or AccessStatus were changed!\n");
1173 /* Generic access mask - no returnlength */
1174 SetLastError(0xdeadbeef);
1175 Access
= AccessStatus
= 0x1abe11ed;
1176 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1177 PrivSet
, NULL
, &Access
, &AccessStatus
);
1178 err
= GetLastError();
1179 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1180 "with ERROR_NOACCESS, instead of %d\n", err
);
1181 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1182 "Access and/or AccessStatus were changed!\n");
1184 /* Generic access mask - no privilegeset buffer, no returnlength */
1185 SetLastError(0xdeadbeef);
1186 Access
= AccessStatus
= 0x1abe11ed;
1187 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1188 NULL
, NULL
, &Access
, &AccessStatus
);
1189 err
= GetLastError();
1190 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1191 "with ERROR_NOACCESS, instead of %d\n", err
);
1192 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1193 "Access and/or AccessStatus were changed!\n");
1195 /* sd with no dacl present */
1196 Access
= AccessStatus
= 0x1abe11ed;
1197 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, FALSE
, NULL
, FALSE
);
1198 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1199 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1200 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1201 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1202 ok(AccessStatus
&& (Access
== KEY_READ
),
1203 "AccessCheck failed to grant access with error %d\n",
1206 /* sd with no dacl present - no privilegeset buffer */
1207 SetLastError(0xdeadbeef);
1208 Access
= AccessStatus
= 0x1abe11ed;
1209 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1210 NULL
, &PrivSetLen
, &Access
, &AccessStatus
);
1211 err
= GetLastError();
1212 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1213 "with ERROR_NOACCESS, instead of %d\n", err
);
1214 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1215 "Access and/or AccessStatus were changed!\n");
1219 /* Generic access mask - no privilegeset buffer */
1220 SetLastError(0xdeadbeef);
1221 Access
= ntAccessStatus
= 0x1abe11ed;
1222 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1223 NULL
, &PrivSetLen
, &Access
, &ntAccessStatus
);
1224 err
= GetLastError();
1225 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1226 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1227 ok(err
== 0xdeadbeef,
1228 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1229 ok(Access
== 0x1abe11ed && ntAccessStatus
== 0x1abe11ed,
1230 "Access and/or AccessStatus were changed!\n");
1232 /* Generic access mask - no returnlength */
1233 SetLastError(0xdeadbeef);
1234 Access
= ntAccessStatus
= 0x1abe11ed;
1235 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1236 PrivSet
, NULL
, &Access
, &ntAccessStatus
);
1237 err
= GetLastError();
1238 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1239 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1240 ok(err
== 0xdeadbeef,
1241 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1242 ok(Access
== 0x1abe11ed && ntAccessStatus
== 0x1abe11ed,
1243 "Access and/or AccessStatus were changed!\n");
1245 /* Generic access mask - no privilegeset buffer, no returnlength */
1246 SetLastError(0xdeadbeef);
1247 Access
= ntAccessStatus
= 0x1abe11ed;
1248 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1249 NULL
, NULL
, &Access
, &ntAccessStatus
);
1250 err
= GetLastError();
1251 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1252 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1253 ok(err
== 0xdeadbeef,
1254 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1255 ok(Access
== 0x1abe11ed && ntAccessStatus
== 0x1abe11ed,
1256 "Access and/or AccessStatus were changed!\n");
1259 win_skip("NtAccessCheck unavailable. Skipping.\n");
1261 /* sd with NULL dacl */
1262 Access
= AccessStatus
= 0x1abe11ed;
1263 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, NULL
, FALSE
);
1264 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1265 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1266 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1267 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1268 ok(AccessStatus
&& (Access
== KEY_READ
),
1269 "AccessCheck failed to grant access with error %d\n",
1272 /* sd with blank dacl */
1273 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
1274 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1275 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1276 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1277 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1278 err
= GetLastError();
1279 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1280 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1281 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1283 res
= AddAccessAllowedAce(Acl
, ACL_REVISION
, KEY_READ
, EveryoneSid
);
1284 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
1286 res
= AddAccessDeniedAce(Acl
, ACL_REVISION
, KEY_SET_VALUE
, AdminSid
);
1287 ok(res
, "AddAccessDeniedAce failed with error %d\n", GetLastError());
1290 Access
= AccessStatus
= 0x1abe11ed;
1291 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1292 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1293 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1294 ok(AccessStatus
&& (Access
== KEY_READ
),
1295 "AccessCheck failed to grant access with error %d\n",
1298 ret
= AccessCheck(SecurityDescriptor
, Token
, MAXIMUM_ALLOWED
, &Mapping
,
1299 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1300 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1302 "AccessCheck failed to grant any access with error %d\n",
1304 trace("AccessCheck with MAXIMUM_ALLOWED got Access 0x%08x\n", Access
);
1306 /* Access denied by SD */
1307 SetLastError(0xdeadbeef);
1308 Access
= AccessStatus
= 0x1abe11ed;
1309 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_WRITE
, &Mapping
,
1310 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1311 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1312 err
= GetLastError();
1313 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1314 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1315 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1318 PrivSet
->PrivilegeCount
= 16;
1319 ret
= AccessCheck(SecurityDescriptor
, Token
, ACCESS_SYSTEM_SECURITY
, &Mapping
,
1320 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1321 ok(ret
&& !AccessStatus
&& GetLastError() == ERROR_PRIVILEGE_NOT_HELD
,
1322 "AccessCheck should have failed with ERROR_PRIVILEGE_NOT_HELD, instead of %d\n",
1325 ret
= ImpersonateLoggedOnUser(Token
);
1326 ok(ret
, "ImpersonateLoggedOnUser failed with error %d\n", GetLastError());
1327 ret
= pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE
, TRUE
, TRUE
, &Enabled
);
1331 PrivSet
->PrivilegeCount
= 16;
1332 ret
= AccessCheck(SecurityDescriptor
, Token
, ACCESS_SYSTEM_SECURITY
, &Mapping
,
1333 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1334 ok(ret
&& AccessStatus
&& GetLastError() == 0,
1335 "AccessCheck should have succeeded, error %d\n",
1337 ok(Access
== ACCESS_SYSTEM_SECURITY
,
1338 "Access should be equal to ACCESS_SYSTEM_SECURITY instead of 0x%08x\n",
1342 trace("Couldn't get SE_SECURITY_PRIVILEGE (0x%08x), skipping ACCESS_SYSTEM_SECURITY test\n",
1344 ret
= RevertToSelf();
1345 ok(ret
, "RevertToSelf failed with error %d\n", GetLastError());
1347 /* test INHERIT_ONLY_ACE */
1348 ret
= InitializeAcl(Acl
, 256, ACL_REVISION
);
1349 ok(ret
, "InitializeAcl failed with error %d\n", GetLastError());
1351 /* NT doesn't have AddAccessAllowedAceEx. Skipping this call/test doesn't influence
1354 if (pAddAccessAllowedAceEx
)
1356 ret
= pAddAccessAllowedAceEx(Acl
, ACL_REVISION
, INHERIT_ONLY_ACE
, KEY_READ
, EveryoneSid
);
1357 ok(ret
, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
1360 win_skip("AddAccessAllowedAceEx is not available\n");
1362 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1363 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1364 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1365 err
= GetLastError();
1366 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1367 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1368 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1372 res
= DuplicateToken(ProcessToken
, SecurityAnonymous
, &Token
);
1373 ok(res
, "DuplicateToken failed with error %d\n", GetLastError());
1375 SetLastError(0xdeadbeef);
1376 ret
= AccessCheck(SecurityDescriptor
, Token
, MAXIMUM_ALLOWED
, &Mapping
,
1377 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1378 err
= GetLastError();
1379 ok(!ret
&& err
== ERROR_BAD_IMPERSONATION_LEVEL
, "AccessCheck should have failed "
1380 "with ERROR_BAD_IMPERSONATION_LEVEL, instead of %d\n", err
);
1384 SetLastError(0xdeadbeef);
1385 ret
= AccessCheck(SecurityDescriptor
, ProcessToken
, KEY_READ
, &Mapping
,
1386 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1387 err
= GetLastError();
1388 ok(!ret
&& err
== ERROR_NO_IMPERSONATION_TOKEN
, "AccessCheck should have failed "
1389 "with ERROR_NO_IMPERSONATION_TOKEN, instead of %d\n", err
);
1391 CloseHandle(ProcessToken
);
1394 FreeSid(EveryoneSid
);
1399 HeapFree(GetProcessHeap(), 0, Acl
);
1400 HeapFree(GetProcessHeap(), 0, SecurityDescriptor
);
1401 HeapFree(GetProcessHeap(), 0, PrivSet
);
1404 /* test GetTokenInformation for the various attributes */
1405 static void test_token_attr(void)
1407 HANDLE Token
, ImpersonationToken
;
1409 TOKEN_PRIVILEGES
*Privileges
;
1410 TOKEN_GROUPS
*Groups
;
1412 TOKEN_DEFAULT_DACL
*Dacl
;
1416 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
;
1419 /* cygwin-like use case */
1420 SetLastError(0xdeadbeef);
1421 ret
= OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED
, &Token
);
1422 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
1424 win_skip("OpenProcessToken is not implemented\n");
1427 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1430 DWORD buf
[256]; /* GetTokenInformation wants a dword-aligned buffer */
1432 ret
= GetTokenInformation(Token
, TokenUser
,(void*)buf
, Size
, &Size
);
1433 ok(ret
, "GetTokenInformation failed with error %d\n", GetLastError());
1434 Size
= sizeof(ImpersonationLevel
);
1435 ret
= GetTokenInformation(Token
, TokenImpersonationLevel
, &ImpersonationLevel
, Size
, &Size
);
1436 GLE
= GetLastError();
1437 ok(!ret
&& (GLE
== ERROR_INVALID_PARAMETER
), "GetTokenInformation(TokenImpersonationLevel) on primary token should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GLE
);
1441 if(!pConvertSidToStringSidA
)
1443 win_skip("ConvertSidToStringSidA is not available\n");
1447 SetLastError(0xdeadbeef);
1448 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS
, &Token
);
1449 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1452 /* insufficient buffer length */
1453 SetLastError(0xdeadbeef);
1455 ret
= GetTokenInformation(Token
, TokenGroups
, NULL
, 0, &Size2
);
1456 ok(Size2
> 1, "got %d\n", Size2
);
1457 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1458 "%d with error %d\n", ret
, GetLastError());
1460 Groups
= HeapAlloc(GetProcessHeap(), 0, Size2
);
1461 memset(Groups
, 0xcc, Size2
);
1463 ret
= GetTokenInformation(Token
, TokenGroups
, Groups
, Size2
, &Size
);
1464 ok(Size
> 1, "got %d\n", Size
);
1465 ok((!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
) || broken(ret
) /* wow64 */,
1466 "%d with error %d\n", ret
, GetLastError());
1468 ok(*((BYTE
*)Groups
) == 0xcc, "buffer altered\n");
1470 HeapFree(GetProcessHeap(), 0, Groups
);
1472 SetLastError(0xdeadbeef);
1473 ret
= GetTokenInformation(Token
, TokenGroups
, NULL
, 0, &Size
);
1474 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1475 "GetTokenInformation(TokenGroups) %s with error %d\n",
1476 ret
? "succeeded" : "failed", GetLastError());
1477 Groups
= HeapAlloc(GetProcessHeap(), 0, Size
);
1478 SetLastError(0xdeadbeef);
1479 ret
= GetTokenInformation(Token
, TokenGroups
, Groups
, Size
, &Size
);
1480 ok(ret
, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
1481 ok(GetLastError() == 0xdeadbeef,
1482 "GetTokenInformation shouldn't have set last error to %d\n",
1484 trace("TokenGroups:\n");
1485 for (i
= 0; i
< Groups
->GroupCount
; i
++)
1487 DWORD NameLength
= 255;
1489 DWORD DomainLength
= 255;
1491 SID_NAME_USE SidNameUse
;
1494 ret
= LookupAccountSid(NULL
, Groups
->Groups
[i
].Sid
, Name
, &NameLength
, Domain
, &DomainLength
, &SidNameUse
);
1497 pConvertSidToStringSidA(Groups
->Groups
[i
].Sid
, &SidString
);
1498 trace("%s, %s\\%s use: %d attr: 0x%08x\n", SidString
, Domain
, Name
, SidNameUse
, Groups
->Groups
[i
].Attributes
);
1499 LocalFree(SidString
);
1501 else trace("attr: 0x%08x LookupAccountSid failed with error %d\n", Groups
->Groups
[i
].Attributes
, GetLastError());
1503 HeapFree(GetProcessHeap(), 0, Groups
);
1506 ret
= GetTokenInformation(Token
, TokenUser
, NULL
, 0, &Size
);
1507 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1508 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1509 User
= HeapAlloc(GetProcessHeap(), 0, Size
);
1510 ret
= GetTokenInformation(Token
, TokenUser
, User
, Size
, &Size
);
1512 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1514 pConvertSidToStringSidA(User
->User
.Sid
, &SidString
);
1515 trace("TokenUser: %s attr: 0x%08x\n", SidString
, User
->User
.Attributes
);
1516 LocalFree(SidString
);
1517 HeapFree(GetProcessHeap(), 0, User
);
1520 ret
= GetTokenInformation(Token
, TokenPrivileges
, NULL
, 0, &Size
);
1521 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1522 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1523 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
1524 ret
= GetTokenInformation(Token
, TokenPrivileges
, Privileges
, Size
, &Size
);
1526 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1527 trace("TokenPrivileges:\n");
1528 for (i
= 0; i
< Privileges
->PrivilegeCount
; i
++)
1531 DWORD NameLen
= sizeof(Name
)/sizeof(Name
[0]);
1532 LookupPrivilegeName(NULL
, &Privileges
->Privileges
[i
].Luid
, Name
, &NameLen
);
1533 trace("\t%s, 0x%x\n", Name
, Privileges
->Privileges
[i
].Attributes
);
1535 HeapFree(GetProcessHeap(), 0, Privileges
);
1537 ret
= DuplicateToken(Token
, SecurityAnonymous
, &ImpersonationToken
);
1538 ok(ret
, "DuplicateToken failed with error %d\n", GetLastError());
1540 Size
= sizeof(ImpersonationLevel
);
1541 ret
= GetTokenInformation(ImpersonationToken
, TokenImpersonationLevel
, &ImpersonationLevel
, Size
, &Size
);
1542 ok(ret
, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1543 ok(ImpersonationLevel
== SecurityAnonymous
, "ImpersonationLevel should have been SecurityAnonymous instead of %d\n", ImpersonationLevel
);
1545 CloseHandle(ImpersonationToken
);
1548 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, NULL
, 0, &Size
);
1549 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1550 "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1552 Dacl
= HeapAlloc(GetProcessHeap(), 0, Size
);
1553 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size
);
1554 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1556 SetLastError(0xdeadbeef);
1557 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, NULL
, 0);
1558 GLE
= GetLastError();
1559 ok(!ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1560 ok(GLE
== ERROR_BAD_LENGTH
, "expected ERROR_BAD_LENGTH got %u\n", GLE
);
1562 SetLastError(0xdeadbeef);
1563 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, NULL
, Size
);
1564 GLE
= GetLastError();
1565 ok(!ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1566 ok(GLE
== ERROR_NOACCESS
, "expected ERROR_NOACCESS got %u\n", GLE
);
1568 acl
= Dacl
->DefaultDacl
;
1569 Dacl
->DefaultDacl
= NULL
;
1571 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
);
1572 ok(ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1575 Dacl
->DefaultDacl
= (ACL
*)0xdeadbeef;
1576 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size2
);
1577 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1578 ok(Dacl
->DefaultDacl
== NULL
, "expected NULL, got %p\n", Dacl
->DefaultDacl
);
1579 ok(Size2
== sizeof(TOKEN_DEFAULT_DACL
) || broken(Size2
== 2*sizeof(TOKEN_DEFAULT_DACL
)), /* WoW64 */
1580 "got %u expected sizeof(TOKEN_DEFAULT_DACL)\n", Size2
);
1582 Dacl
->DefaultDacl
= acl
;
1583 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
);
1584 ok(ret
, "SetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1586 if (Size2
== sizeof(TOKEN_DEFAULT_DACL
)) {
1587 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size2
);
1588 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1590 win_skip("TOKEN_DEFAULT_DACL size too small on WoW64\n");
1592 HeapFree(GetProcessHeap(), 0, Dacl
);
1596 typedef union _MAX_SID
1599 char max
[SECURITY_MAX_SID_SIZE
];
1602 static void test_sid_str(PSID
* sid
)
1605 BOOL ret
= pConvertSidToStringSidA(sid
, &str_sid
);
1606 ok(ret
, "ConvertSidToStringSidA() failed: %d\n", GetLastError());
1609 char account
[MAX_PATH
], domain
[MAX_PATH
];
1611 DWORD acc_size
= MAX_PATH
;
1612 DWORD dom_size
= MAX_PATH
;
1613 ret
= LookupAccountSid(NULL
, sid
, account
, &acc_size
, domain
, &dom_size
, &use
);
1614 ok(ret
|| (!ret
&& (GetLastError() == ERROR_NONE_MAPPED
)),
1615 "LookupAccountSid(%s) failed: %d\n", str_sid
, GetLastError());
1617 trace(" %s %s\\%s %d\n", str_sid
, domain
, account
, use
);
1618 else if (GetLastError() == ERROR_NONE_MAPPED
)
1619 trace(" %s couldn't be mapped\n", str_sid
);
1624 static const struct well_known_sid_value
1626 BOOL without_domain
;
1627 const char *sid_string
;
1628 } well_known_sid_values
[] = {
1629 /* 0 */ {TRUE
, "S-1-0-0"}, {TRUE
, "S-1-1-0"}, {TRUE
, "S-1-2-0"}, {TRUE
, "S-1-3-0"},
1630 /* 4 */ {TRUE
, "S-1-3-1"}, {TRUE
, "S-1-3-2"}, {TRUE
, "S-1-3-3"}, {TRUE
, "S-1-5"},
1631 /* 8 */ {FALSE
, "S-1-5-1"}, {TRUE
, "S-1-5-2"}, {TRUE
, "S-1-5-3"}, {TRUE
, "S-1-5-4"},
1632 /* 12 */ {TRUE
, "S-1-5-6"}, {TRUE
, "S-1-5-7"}, {TRUE
, "S-1-5-8"}, {TRUE
, "S-1-5-9"},
1633 /* 16 */ {TRUE
, "S-1-5-10"}, {TRUE
, "S-1-5-11"}, {TRUE
, "S-1-5-12"}, {TRUE
, "S-1-5-13"},
1634 /* 20 */ {TRUE
, "S-1-5-14"}, {FALSE
, NULL
}, {TRUE
, "S-1-5-18"}, {TRUE
, "S-1-5-19"},
1635 /* 24 */ {TRUE
, "S-1-5-20"}, {TRUE
, "S-1-5-32"},
1636 /* 26 */ {FALSE
, "S-1-5-32-544"}, {TRUE
, "S-1-5-32-545"}, {TRUE
, "S-1-5-32-546"},
1637 /* 29 */ {TRUE
, "S-1-5-32-547"}, {TRUE
, "S-1-5-32-548"}, {TRUE
, "S-1-5-32-549"},
1638 /* 32 */ {TRUE
, "S-1-5-32-550"}, {TRUE
, "S-1-5-32-551"}, {TRUE
, "S-1-5-32-552"},
1639 /* 35 */ {TRUE
, "S-1-5-32-554"}, {TRUE
, "S-1-5-32-555"}, {TRUE
, "S-1-5-32-556"},
1640 /* 38 */ {FALSE
, "S-1-5-21-12-23-34-45-56-500"}, {FALSE
, "S-1-5-21-12-23-34-45-56-501"},
1641 /* 40 */ {FALSE
, "S-1-5-21-12-23-34-45-56-502"}, {FALSE
, "S-1-5-21-12-23-34-45-56-512"},
1642 /* 42 */ {FALSE
, "S-1-5-21-12-23-34-45-56-513"}, {FALSE
, "S-1-5-21-12-23-34-45-56-514"},
1643 /* 44 */ {FALSE
, "S-1-5-21-12-23-34-45-56-515"}, {FALSE
, "S-1-5-21-12-23-34-45-56-516"},
1644 /* 46 */ {FALSE
, "S-1-5-21-12-23-34-45-56-517"}, {FALSE
, "S-1-5-21-12-23-34-45-56-518"},
1645 /* 48 */ {FALSE
, "S-1-5-21-12-23-34-45-56-519"}, {FALSE
, "S-1-5-21-12-23-34-45-56-520"},
1646 /* 50 */ {FALSE
, "S-1-5-21-12-23-34-45-56-553"},
1647 /* Added in Windows Server 2003 */
1648 /* 51 */ {TRUE
, "S-1-5-64-10"}, {TRUE
, "S-1-5-64-21"}, {TRUE
, "S-1-5-64-14"},
1649 /* 54 */ {TRUE
, "S-1-5-15"}, {TRUE
, "S-1-5-1000"}, {FALSE
, "S-1-5-32-557"},
1650 /* 57 */ {TRUE
, "S-1-5-32-558"}, {TRUE
, "S-1-5-32-559"}, {TRUE
, "S-1-5-32-560"},
1651 /* 60 */ {TRUE
, "S-1-5-32-561"}, {TRUE
, "S-1-5-32-562"},
1652 /* Added in Windows Vista: */
1653 /* 62 */ {TRUE
, "S-1-5-32-568"},
1654 /* 63 */ {TRUE
, "S-1-5-17"}, {FALSE
, "S-1-5-32-569"}, {TRUE
, "S-1-16-0"},
1655 /* 66 */ {TRUE
, "S-1-16-4096"}, {TRUE
, "S-1-16-8192"}, {TRUE
, "S-1-16-12288"},
1656 /* 69 */ {TRUE
, "S-1-16-16384"}, {TRUE
, "S-1-5-33"}, {TRUE
, "S-1-3-4"},
1657 /* 72 */ {FALSE
, "S-1-5-21-12-23-34-45-56-571"}, {FALSE
, "S-1-5-21-12-23-34-45-56-572"},
1658 /* 74 */ {TRUE
, "S-1-5-22"}, {FALSE
, "S-1-5-21-12-23-34-45-56-521"}, {TRUE
, "S-1-5-32-573"}
1661 static void test_CreateWellKnownSid(void)
1663 SID_IDENTIFIER_AUTHORITY ident
= { SECURITY_NT_AUTHORITY
};
1664 PSID domainsid
, sid
;
1669 if (!pCreateWellKnownSid
)
1671 win_skip("CreateWellKnownSid not available\n");
1676 SetLastError(0xdeadbeef);
1677 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, NULL
, &size
);
1678 error
= GetLastError();
1679 ok(!ret
, "CreateWellKnownSid succeeded\n");
1680 ok(error
== ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error
);
1681 ok(size
, "expected size > 0\n");
1683 SetLastError(0xdeadbeef);
1684 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, NULL
, &size
);
1685 error
= GetLastError();
1686 ok(!ret
, "CreateWellKnownSid succeeded\n");
1687 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %u\n", error
);
1689 sid
= HeapAlloc(GetProcessHeap(), 0, size
);
1690 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, sid
, &size
);
1691 ok(ret
, "CreateWellKnownSid failed %u\n", GetLastError());
1692 HeapFree(GetProcessHeap(), 0, sid
);
1694 /* a domain sid usually have three subauthorities but we test that CreateWellKnownSid doesn't check it */
1695 AllocateAndInitializeSid(&ident
, 6, SECURITY_NT_NON_UNIQUE
, 12, 23, 34, 45, 56, 0, 0, &domainsid
);
1697 for (i
= 0; i
< sizeof(well_known_sid_values
)/sizeof(well_known_sid_values
[0]); i
++)
1699 const struct well_known_sid_value
*value
= &well_known_sid_values
[i
];
1700 char sid_buffer
[SECURITY_MAX_SID_SIZE
];
1704 if (value
->sid_string
== NULL
)
1707 if (i
> WinAccountRasAndIasServersSid
)
1709 /* These SIDs aren't implemented by all Windows versions - detect it and break the loop */
1710 cb
= sizeof(sid_buffer
);
1711 if (!pCreateWellKnownSid(i
, domainsid
, sid_buffer
, &cb
))
1713 skip("Well known SIDs starting from %u are not implemented\n", i
);
1718 cb
= sizeof(sid_buffer
);
1719 ok(pCreateWellKnownSid(i
, value
->without_domain
? NULL
: domainsid
, sid_buffer
, &cb
), "Couldn't create well known sid %u\n", i
);
1720 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer
)), cb
, DWORD
, "%d");
1721 ok(IsValidSid(sid_buffer
), "The sid is not valid\n");
1722 ok(pConvertSidToStringSidA(sid_buffer
, &str
), "Couldn't convert SID to string\n");
1723 ok(strcmp(str
, value
->sid_string
) == 0, "SID mismatch - expected %s, got %s\n",
1724 value
->sid_string
, str
);
1727 if (value
->without_domain
)
1729 char buf2
[SECURITY_MAX_SID_SIZE
];
1731 ok(pCreateWellKnownSid(i
, domainsid
, buf2
, &cb
), "Couldn't create well known sid %u with optional domain\n", i
);
1732 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer
)), cb
, DWORD
, "%d");
1733 ok(memcmp(buf2
, sid_buffer
, cb
) == 0, "SID create with domain is different than without (%u)\n", i
);
1740 static void test_LookupAccountSid(void)
1742 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
1743 CHAR accountA
[MAX_PATH
], domainA
[MAX_PATH
], usernameA
[MAX_PATH
];
1744 DWORD acc_sizeA
, dom_sizeA
, user_sizeA
;
1745 DWORD real_acc_sizeA
, real_dom_sizeA
;
1746 WCHAR accountW
[MAX_PATH
], domainW
[MAX_PATH
];
1747 DWORD acc_sizeW
, dom_sizeW
;
1748 DWORD real_acc_sizeW
, real_dom_sizeW
;
1749 PSID pUsersSid
= NULL
;
1752 DWORD error
, size
, cbti
= 0;
1757 PTOKEN_USER ptiUser
= NULL
;
1759 /* native windows crashes if account size, domain size, or name use is NULL */
1761 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1762 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &pUsersSid
);
1763 ok(ret
|| (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
),
1764 "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1766 /* not running on NT so give up */
1767 if (!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
1770 real_acc_sizeA
= MAX_PATH
;
1771 real_dom_sizeA
= MAX_PATH
;
1772 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &real_acc_sizeA
, domainA
, &real_dom_sizeA
, &use
);
1773 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1775 /* try NULL account */
1776 acc_sizeA
= MAX_PATH
;
1777 dom_sizeA
= MAX_PATH
;
1778 ret
= LookupAccountSidA(NULL
, pUsersSid
, NULL
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1779 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1781 /* try NULL domain */
1782 acc_sizeA
= MAX_PATH
;
1783 dom_sizeA
= MAX_PATH
;
1784 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, NULL
, &dom_sizeA
, &use
);
1785 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1787 /* try a small account buffer */
1789 dom_sizeA
= MAX_PATH
;
1791 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1792 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1793 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1794 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1796 /* try a 0 sized account buffer */
1798 dom_sizeA
= MAX_PATH
;
1800 LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1801 /* this can fail or succeed depending on OS version but the size will always be returned */
1802 ok(acc_sizeA
== real_acc_sizeA
+ 1,
1803 "LookupAccountSidA() Expected acc_size = %u, got %u\n",
1804 real_acc_sizeA
+ 1, acc_sizeA
);
1806 /* try a 0 sized account buffer */
1808 dom_sizeA
= MAX_PATH
;
1809 LookupAccountSidA(NULL
, pUsersSid
, NULL
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1810 /* this can fail or succeed depending on OS version but the size will always be returned */
1811 ok(acc_sizeA
== real_acc_sizeA
+ 1,
1812 "LookupAccountSid() Expected acc_size = %u, got %u\n",
1813 real_acc_sizeA
+ 1, acc_sizeA
);
1815 /* try a small domain buffer */
1817 acc_sizeA
= MAX_PATH
;
1819 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1820 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1821 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1822 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1824 /* try a 0 sized domain buffer */
1826 acc_sizeA
= MAX_PATH
;
1828 LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1829 /* this can fail or succeed depending on OS version but the size will always be returned */
1830 ok(dom_sizeA
== real_dom_sizeA
+ 1,
1831 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1832 real_dom_sizeA
+ 1, dom_sizeA
);
1834 /* try a 0 sized domain buffer */
1836 acc_sizeA
= MAX_PATH
;
1837 LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, NULL
, &dom_sizeA
, &use
);
1838 /* this can fail or succeed depending on OS version but the size will always be returned */
1839 ok(dom_sizeA
== real_dom_sizeA
+ 1,
1840 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1841 real_dom_sizeA
+ 1, dom_sizeA
);
1843 real_acc_sizeW
= MAX_PATH
;
1844 real_dom_sizeW
= MAX_PATH
;
1845 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &real_acc_sizeW
, domainW
, &real_dom_sizeW
, &use
);
1846 ok(ret
, "LookupAccountSidW() Expected TRUE, got FALSE\n");
1848 /* try an invalid system name */
1849 real_acc_sizeA
= MAX_PATH
;
1850 real_dom_sizeA
= MAX_PATH
;
1851 ret
= LookupAccountSidA("deepthought", pUsersSid
, accountA
, &real_acc_sizeA
, domainA
, &real_dom_sizeA
, &use
);
1852 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1853 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE
|| GetLastError() == RPC_S_INVALID_NET_ADDR
/* Vista */,
1854 "LookupAccountSidA() Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %u\n", GetLastError());
1856 /* native windows crashes if domainW or accountW is NULL */
1858 /* try a small account buffer */
1860 dom_sizeW
= MAX_PATH
;
1862 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1863 ok(!ret
, "LookupAccountSidW() Expected FALSE got TRUE\n");
1864 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1865 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1867 /* try a 0 sized account buffer */
1869 dom_sizeW
= MAX_PATH
;
1871 LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1872 /* this can fail or succeed depending on OS version but the size will always be returned */
1873 ok(acc_sizeW
== real_acc_sizeW
+ 1,
1874 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1875 real_acc_sizeW
+ 1, acc_sizeW
);
1877 /* try a 0 sized account buffer */
1879 dom_sizeW
= MAX_PATH
;
1880 LookupAccountSidW(NULL
, pUsersSid
, NULL
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1881 /* this can fail or succeed depending on OS version but the size will always be returned */
1882 ok(acc_sizeW
== real_acc_sizeW
+ 1,
1883 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1884 real_acc_sizeW
+ 1, acc_sizeW
);
1886 /* try a small domain buffer */
1888 acc_sizeW
= MAX_PATH
;
1890 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1891 ok(!ret
, "LookupAccountSidW() Expected FALSE got TRUE\n");
1892 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1893 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1895 /* try a 0 sized domain buffer */
1897 acc_sizeW
= MAX_PATH
;
1899 LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1900 /* this can fail or succeed depending on OS version but the size will always be returned */
1901 ok(dom_sizeW
== real_dom_sizeW
+ 1,
1902 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1903 real_dom_sizeW
+ 1, dom_sizeW
);
1905 /* try a 0 sized domain buffer */
1907 acc_sizeW
= MAX_PATH
;
1908 LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, NULL
, &dom_sizeW
, &use
);
1909 /* this can fail or succeed depending on OS version but the size will always be returned */
1910 ok(dom_sizeW
== real_dom_sizeW
+ 1,
1911 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1912 real_dom_sizeW
+ 1, dom_sizeW
);
1914 acc_sizeW
= dom_sizeW
= use
= 0;
1915 SetLastError(0xdeadbeef);
1916 ret
= LookupAccountSidW(NULL
, pUsersSid
, NULL
, &acc_sizeW
, NULL
, &dom_sizeW
, &use
);
1917 error
= GetLastError();
1918 ok(!ret
, "LookupAccountSidW failed %u\n", GetLastError());
1919 ok(error
== ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error
);
1920 ok(acc_sizeW
, "expected non-zero account size\n");
1921 ok(dom_sizeW
, "expected non-zero domain size\n");
1922 ok(!use
, "expected zero use %u\n", use
);
1926 /* Test LookupAccountSid with Sid retrieved from token information.
1927 This assumes this process is running under the account of the current user.*/
1928 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
|TOKEN_DUPLICATE
, &hToken
);
1929 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1930 ret
= GetTokenInformation(hToken
, TokenUser
, NULL
, 0, &cbti
);
1931 ok(!ret
, "GetTokenInformation failed with error %d\n", GetLastError());
1932 ptiUser
= HeapAlloc(GetProcessHeap(), 0, cbti
);
1933 if (GetTokenInformation(hToken
, TokenUser
, ptiUser
, cbti
, &cbti
))
1935 acc_sizeA
= dom_sizeA
= MAX_PATH
;
1936 ret
= LookupAccountSidA(NULL
, ptiUser
->User
.Sid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1937 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1938 user_sizeA
= MAX_PATH
;
1939 ret
= GetUserNameA(usernameA
, &user_sizeA
);
1940 ok(ret
, "GetUserNameA() Expected TRUE, got FALSE\n");
1941 ok(lstrcmpA(usernameA
, accountA
) == 0, "LookupAccountSidA() Expected account name: %s got: %s\n", usernameA
, accountA
);
1943 HeapFree(GetProcessHeap(), 0, ptiUser
);
1945 if (pCreateWellKnownSid
&& pConvertSidToStringSidA
)
1947 trace("Well Known SIDs:\n");
1948 for (i
= 0; i
<= 60; i
++)
1950 size
= SECURITY_MAX_SID_SIZE
;
1951 if (pCreateWellKnownSid(i
, NULL
, &max_sid
.sid
, &size
))
1953 if (pConvertSidToStringSidA(&max_sid
.sid
, &str_sidA
))
1955 acc_sizeA
= MAX_PATH
;
1956 dom_sizeA
= MAX_PATH
;
1957 if (LookupAccountSidA(NULL
, &max_sid
.sid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
))
1958 trace(" %d: %s %s\\%s %d\n", i
, str_sidA
, domainA
, accountA
, use
);
1959 LocalFree(str_sidA
);
1964 if (GetLastError() != ERROR_INVALID_PARAMETER
)
1965 trace(" CreateWellKnownSid(%d) failed: %d\n", i
, GetLastError());
1967 trace(" %d: not supported\n", i
);
1971 pLsaQueryInformationPolicy
= (void *)GetProcAddress( hmod
, "LsaQueryInformationPolicy");
1972 pLsaOpenPolicy
= (void *)GetProcAddress( hmod
, "LsaOpenPolicy");
1973 pLsaFreeMemory
= (void *)GetProcAddress( hmod
, "LsaFreeMemory");
1974 pLsaClose
= (void *)GetProcAddress( hmod
, "LsaClose");
1976 if (pLsaQueryInformationPolicy
&& pLsaOpenPolicy
&& pLsaFreeMemory
&& pLsaClose
)
1980 LSA_OBJECT_ATTRIBUTES object_attributes
;
1982 ZeroMemory(&object_attributes
, sizeof(object_attributes
));
1983 object_attributes
.Length
= sizeof(object_attributes
);
1985 status
= pLsaOpenPolicy( NULL
, &object_attributes
, POLICY_ALL_ACCESS
, &handle
);
1986 ok(status
== STATUS_SUCCESS
|| status
== STATUS_ACCESS_DENIED
,
1987 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status
);
1989 /* try a more restricted access mask if necessary */
1990 if (status
== STATUS_ACCESS_DENIED
) {
1991 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
1992 status
= pLsaOpenPolicy( NULL
, &object_attributes
, POLICY_VIEW_LOCAL_INFORMATION
, &handle
);
1993 ok(status
== STATUS_SUCCESS
, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status
);
1996 if (status
== STATUS_SUCCESS
)
1998 PPOLICY_ACCOUNT_DOMAIN_INFO info
;
1999 status
= pLsaQueryInformationPolicy(handle
, PolicyAccountDomainInformation
, (PVOID
*)&info
);
2000 ok(status
== STATUS_SUCCESS
, "LsaQueryInformationPolicy() failed, returned 0x%08x\n", status
);
2001 if (status
== STATUS_SUCCESS
)
2003 ok(info
->DomainSid
!=0, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) missing SID\n");
2004 if (info
->DomainSid
)
2006 int count
= *GetSidSubAuthorityCount(info
->DomainSid
);
2007 CopySid(GetSidLengthRequired(count
), &max_sid
, info
->DomainSid
);
2008 test_sid_str((PSID
)&max_sid
.sid
);
2009 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_USER_RID_ADMIN
;
2010 max_sid
.sid
.SubAuthorityCount
= count
+ 1;
2011 test_sid_str((PSID
)&max_sid
.sid
);
2012 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_USER_RID_GUEST
;
2013 test_sid_str((PSID
)&max_sid
.sid
);
2014 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_ADMINS
;
2015 test_sid_str((PSID
)&max_sid
.sid
);
2016 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_USERS
;
2017 test_sid_str((PSID
)&max_sid
.sid
);
2018 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_GUESTS
;
2019 test_sid_str((PSID
)&max_sid
.sid
);
2020 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_COMPUTERS
;
2021 test_sid_str((PSID
)&max_sid
.sid
);
2022 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_CONTROLLERS
;
2023 test_sid_str((PSID
)&max_sid
.sid
);
2024 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_CERT_ADMINS
;
2025 test_sid_str((PSID
)&max_sid
.sid
);
2026 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_SCHEMA_ADMINS
;
2027 test_sid_str((PSID
)&max_sid
.sid
);
2028 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_ENTERPRISE_ADMINS
;
2029 test_sid_str((PSID
)&max_sid
.sid
);
2030 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_POLICY_ADMINS
;
2031 test_sid_str((PSID
)&max_sid
.sid
);
2032 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_ALIAS_RID_RAS_SERVERS
;
2033 test_sid_str((PSID
)&max_sid
.sid
);
2034 max_sid
.sid
.SubAuthority
[count
] = 1000; /* first user account */
2035 test_sid_str((PSID
)&max_sid
.sid
);
2038 pLsaFreeMemory((LPVOID
)info
);
2041 status
= pLsaClose(handle
);
2042 ok(status
== STATUS_SUCCESS
, "LsaClose() failed, returned 0x%08x\n", status
);
2048 static BOOL
get_sid_info(PSID psid
, LPSTR
*user
, LPSTR
*dom
)
2050 static CHAR account
[UNLEN
+ 1];
2051 static CHAR domain
[UNLEN
+ 1];
2052 DWORD size
, dom_size
;
2058 size
= dom_size
= UNLEN
+ 1;
2061 SetLastError(0xdeadbeef);
2062 return LookupAccountSidA(NULL
, psid
, account
, &size
, domain
, &dom_size
, &use
);
2065 static void check_wellknown_name(const char* name
, WELL_KNOWN_SID_TYPE result
)
2067 SID_IDENTIFIER_AUTHORITY ident
= { SECURITY_NT_AUTHORITY
};
2068 PSID domainsid
= NULL
;
2069 char wk_sid
[SECURITY_MAX_SID_SIZE
];
2072 DWORD sid_size
, domain_size
;
2073 SID_NAME_USE sid_use
;
2074 LPSTR domain
, account
, sid_domain
, wk_domain
, wk_account
;
2080 ret
= LookupAccountNameA(NULL
, name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2081 ok(!ret
, " %s Should have failed to lookup account name\n", name
);
2082 psid
= HeapAlloc(GetProcessHeap(),0,sid_size
);
2083 domain
= HeapAlloc(GetProcessHeap(),0,domain_size
);
2084 ret
= LookupAccountNameA(NULL
, name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2088 ok(!ret
, " %s Should have failed to lookup account name\n",name
);
2092 AllocateAndInitializeSid(&ident
, 6, SECURITY_NT_NON_UNIQUE
, 12, 23, 34, 45, 56, 0, 0, &domainsid
);
2093 cb
= sizeof(wk_sid
);
2094 if (!pCreateWellKnownSid(result
, domainsid
, wk_sid
, &cb
))
2096 win_skip("SID %i is not available on the system\n",result
);
2100 ret2
= get_sid_info(wk_sid
, &wk_account
, &wk_domain
);
2101 if (!ret2
&& GetLastError() == ERROR_NONE_MAPPED
)
2103 win_skip("CreateWellKnownSid() succeeded but the account '%s' is not present (W2K)\n", name
);
2107 get_sid_info(psid
, &account
, &sid_domain
);
2109 ok(ret
, "Failed to lookup account name %s\n",name
);
2110 ok(sid_size
!= 0, "sid_size was zero\n");
2112 ok(EqualSid(psid
,wk_sid
),"(%s) Sids fail to match well known sid!\n",name
);
2114 ok(!lstrcmp(account
, wk_account
), "Expected %s , got %s\n", account
, wk_account
);
2115 ok(!lstrcmp(domain
, wk_domain
), "Expected %s, got %s\n", wk_domain
, domain
);
2116 ok(sid_use
== SidTypeWellKnownGroup
, "Expected Use (5), got %d\n", sid_use
);
2120 HeapFree(GetProcessHeap(),0,psid
);
2121 HeapFree(GetProcessHeap(),0,domain
);
2124 static void test_LookupAccountName(void)
2126 DWORD sid_size
, domain_size
, user_size
;
2127 DWORD sid_save
, domain_save
;
2128 CHAR user_name
[UNLEN
+ 1];
2129 CHAR computer_name
[UNLEN
+ 1];
2130 SID_NAME_USE sid_use
;
2131 LPSTR domain
, account
, sid_dom
;
2135 /* native crashes if (assuming all other parameters correct):
2137 * - Sid is NULL and cbSid is > 0
2138 * - cbSid or cchReferencedDomainName are NULL
2139 * - ReferencedDomainName is NULL and cchReferencedDomainName is the correct size
2142 user_size
= UNLEN
+ 1;
2143 SetLastError(0xdeadbeef);
2144 ret
= GetUserNameA(user_name
, &user_size
);
2145 ok(ret
, "Failed to get user name : %d\n", GetLastError());
2150 sid_use
= 0xcafebabe;
2151 SetLastError(0xdeadbeef);
2152 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2153 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
2155 win_skip("LookupAccountNameA is not implemented\n");
2158 ok(!ret
, "Expected 0, got %d\n", ret
);
2159 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2160 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2161 ok(sid_size
!= 0, "Expected non-zero sid size\n");
2162 ok(domain_size
!= 0, "Expected non-zero domain size\n");
2163 ok(sid_use
== 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use
);
2165 sid_save
= sid_size
;
2166 domain_save
= domain_size
;
2168 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
2169 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
2171 /* try valid account name */
2172 ret
= LookupAccountNameA(NULL
, user_name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2173 get_sid_info(psid
, &account
, &sid_dom
);
2174 ok(ret
, "Failed to lookup account name\n");
2175 ok(sid_size
== GetLengthSid(psid
), "Expected %d, got %d\n", GetLengthSid(psid
), sid_size
);
2176 ok(!lstrcmp(account
, user_name
), "Expected %s, got %s\n", user_name
, account
);
2177 ok(!lstrcmp(domain
, sid_dom
), "Expected %s, got %s\n", sid_dom
, domain
);
2178 ok(domain_size
== domain_save
- 1, "Expected %d, got %d\n", domain_save
- 1, domain_size
);
2179 ok(strlen(domain
) == domain_size
, "Expected %d, got %d\n", lstrlen(domain
), domain_size
);
2180 ok(sid_use
== SidTypeUser
, "Expected SidTypeUser (%d), got %d\n", SidTypeUser
, sid_use
);
2181 domain_size
= domain_save
;
2182 sid_size
= sid_save
;
2184 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2186 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2190 ret
= LookupAccountNameA(NULL
, "Everyone", psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2191 get_sid_info(psid
, &account
, &sid_dom
);
2192 ok(ret
, "Failed to lookup account name\n");
2193 ok(sid_size
!= 0, "sid_size was zero\n");
2194 ok(!lstrcmp(account
, "Everyone"), "Expected Everyone, got %s\n", account
);
2195 ok(!lstrcmp(domain
, sid_dom
), "Expected %s, got %s\n", sid_dom
, domain
);
2196 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2197 ok(strlen(domain
) == domain_size
, "Expected %d, got %d\n", lstrlen(domain
), domain_size
);
2198 ok(sid_use
== SidTypeWellKnownGroup
, "Expected SidTypeWellKnownGroup (%d), got %d\n", SidTypeWellKnownGroup
, sid_use
);
2199 domain_size
= domain_save
;
2202 /* NULL Sid with zero sid size */
2203 SetLastError(0xdeadbeef);
2205 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, domain
, &domain_size
, &sid_use
);
2206 ok(!ret
, "Expected 0, got %d\n", ret
);
2207 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2208 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2209 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
2210 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
2212 /* try cchReferencedDomainName - 1 */
2213 SetLastError(0xdeadbeef);
2215 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, domain
, &domain_size
, &sid_use
);
2216 ok(!ret
, "Expected 0, got %d\n", ret
);
2217 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2218 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2219 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
2220 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
2222 /* NULL ReferencedDomainName with zero domain name size */
2223 SetLastError(0xdeadbeef);
2225 ret
= LookupAccountNameA(NULL
, user_name
, psid
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2226 ok(!ret
, "Expected 0, got %d\n", ret
);
2227 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2228 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2229 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
2230 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
2232 HeapFree(GetProcessHeap(), 0, psid
);
2233 HeapFree(GetProcessHeap(), 0, domain
);
2235 /* get sizes for NULL account name */
2238 sid_use
= 0xcafebabe;
2239 SetLastError(0xdeadbeef);
2240 ret
= LookupAccountNameA(NULL
, NULL
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2241 if (!ret
&& GetLastError() == ERROR_NONE_MAPPED
)
2242 win_skip("NULL account name doesn't work on NT4\n");
2245 ok(!ret
, "Expected 0, got %d\n", ret
);
2246 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2247 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2248 ok(sid_size
!= 0, "Expected non-zero sid size\n");
2249 ok(domain_size
!= 0, "Expected non-zero domain size\n");
2250 ok(sid_use
== 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use
);
2252 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
2253 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
2255 /* try NULL account name */
2256 ret
= LookupAccountNameA(NULL
, NULL
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2257 get_sid_info(psid
, &account
, &sid_dom
);
2258 ok(ret
, "Failed to lookup account name\n");
2259 /* Using a fixed string will not work on different locales */
2260 ok(!lstrcmp(account
, domain
),
2261 "Got %s for account and %s for domain, these should be the same\n",
2263 ok(sid_use
== SidTypeDomain
, "Expected SidTypeDomain (%d), got %d\n", SidTypeDomain
, sid_use
);
2265 HeapFree(GetProcessHeap(), 0, psid
);
2266 HeapFree(GetProcessHeap(), 0, domain
);
2269 /* try an invalid account name */
2270 SetLastError(0xdeadbeef);
2273 ret
= LookupAccountNameA(NULL
, "oogabooga", NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2274 ok(!ret
, "Expected 0, got %d\n", ret
);
2275 ok(GetLastError() == ERROR_NONE_MAPPED
||
2276 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE
),
2277 "Expected ERROR_NONE_MAPPED, got %d\n", GetLastError());
2278 ok(sid_size
== 0, "Expected 0, got %d\n", sid_size
);
2279 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2281 /* try an invalid system name */
2282 SetLastError(0xdeadbeef);
2285 ret
= LookupAccountNameA("deepthought", NULL
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2286 ok(!ret
, "Expected 0, got %d\n", ret
);
2287 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE
|| GetLastError() == RPC_S_INVALID_NET_ADDR
/* Vista */,
2288 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
2289 ok(sid_size
== 0, "Expected 0, got %d\n", sid_size
);
2290 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2292 /* try with the computer name as the account name */
2293 domain_size
= sizeof(computer_name
);
2294 GetComputerNameA(computer_name
, &domain_size
);
2297 ret
= LookupAccountNameA(NULL
, computer_name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2298 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
||
2299 GetLastError() == ERROR_NONE_MAPPED
/* in a domain */ ||
2300 broken(GetLastError() == ERROR_TRUSTED_DOMAIN_FAILURE
) ||
2301 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE
)),
2302 "LookupAccountNameA failed: %d\n", GetLastError());
2303 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
2305 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
2306 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
2307 ret
= LookupAccountNameA(NULL
, computer_name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2308 ok(ret
, "LookupAccountNameA failed: %d\n", GetLastError());
2309 ok(sid_use
== SidTypeDomain
||
2310 (sid_use
== SidTypeUser
&& ! strcmp(computer_name
, user_name
)), "expected SidTypeDomain for %s, got %d\n", computer_name
, sid_use
);
2311 HeapFree(GetProcessHeap(), 0, domain
);
2312 HeapFree(GetProcessHeap(), 0, psid
);
2315 /* Well Known names */
2316 if (!pCreateWellKnownSid
)
2318 win_skip("CreateWellKnownSid not available\n");
2322 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2324 skip("Non-English locale (skipping well known name creation tests)\n");
2328 check_wellknown_name("LocalService", WinLocalServiceSid
);
2329 check_wellknown_name("Local Service", WinLocalServiceSid
);
2331 check_wellknown_name("Local Service", 0);
2332 check_wellknown_name("NetworkService", WinNetworkServiceSid
);
2333 check_wellknown_name("Network Service", WinNetworkServiceSid
);
2335 /* example of some names where the spaces are not optional */
2336 check_wellknown_name("Terminal Server User", WinTerminalServerSid
);
2337 check_wellknown_name("TerminalServer User", 0);
2338 check_wellknown_name("TerminalServerUser", 0);
2339 check_wellknown_name("Terminal ServerUser", 0);
2341 check_wellknown_name("enterprise domain controllers",WinEnterpriseControllersSid
);
2342 check_wellknown_name("enterprisedomain controllers", 0);
2343 check_wellknown_name("enterprise domaincontrollers", 0);
2344 check_wellknown_name("enterprisedomaincontrollers", 0);
2346 /* case insensitivity */
2347 check_wellknown_name("lOCAlServICE", WinLocalServiceSid
);
2349 /* fully qualified account names */
2350 check_wellknown_name("NT AUTHORITY\\LocalService", WinLocalServiceSid
);
2351 check_wellknown_name("nt authority\\Network Service", WinNetworkServiceSid
);
2352 check_wellknown_name("nt authority test\\Network Service", 0);
2353 check_wellknown_name("Dummy\\Network Service", 0);
2354 check_wellknown_name("ntauthority\\Network Service", 0);
2357 static void test_security_descriptor(void)
2359 SECURITY_DESCRIPTOR sd
;
2362 BOOL isDefault
, isPresent
, ret
;
2366 SetLastError(0xdeadbeef);
2367 ret
= InitializeSecurityDescriptor(&sd
, SECURITY_DESCRIPTOR_REVISION
);
2368 if (ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2370 win_skip("InitializeSecurityDescriptor is not implemented\n");
2374 ok(GetSecurityDescriptorOwner(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorOwner failed\n");
2375 expect_eq(psid
, NULL
, PSID
, "%p");
2376 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2377 sd
.Control
|= SE_DACL_PRESENT
| SE_SACL_PRESENT
;
2379 SetLastError(0xdeadbeef);
2381 expect_eq(MakeSelfRelativeSD(&sd
, buf
, &size
), FALSE
, BOOL
, "%d");
2382 expect_eq(GetLastError(), ERROR_INSUFFICIENT_BUFFER
, DWORD
, "%u");
2383 ok(size
> 5, "Size not increased\n");
2386 expect_eq(MakeSelfRelativeSD(&sd
, buf
, &size
), TRUE
, BOOL
, "%d");
2387 ok(GetSecurityDescriptorOwner(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorOwner failed\n");
2388 expect_eq(psid
, NULL
, PSID
, "%p");
2389 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2390 ok(GetSecurityDescriptorGroup(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorGroup failed\n");
2391 expect_eq(psid
, NULL
, PSID
, "%p");
2392 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2393 ok(GetSecurityDescriptorDacl(&sd
, &isPresent
, &pacl
, &isDefault
), "GetSecurityDescriptorDacl failed\n");
2394 expect_eq(isPresent
, TRUE
, BOOL
, "%d");
2395 expect_eq(psid
, NULL
, PSID
, "%p");
2396 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2397 ok(GetSecurityDescriptorSacl(&sd
, &isPresent
, &pacl
, &isDefault
), "GetSecurityDescriptorSacl failed\n");
2398 expect_eq(isPresent
, TRUE
, BOOL
, "%d");
2399 expect_eq(psid
, NULL
, PSID
, "%p");
2400 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2404 #define TEST_GRANTED_ACCESS(a,b) test_granted_access(a,b,0,__LINE__)
2405 #define TEST_GRANTED_ACCESS2(a,b,c) test_granted_access(a,b,c,__LINE__)
2406 static void test_granted_access(HANDLE handle
, ACCESS_MASK access
,
2407 ACCESS_MASK alt
, int line
)
2409 OBJECT_BASIC_INFORMATION obj_info
;
2412 if (!pNtQueryObject
)
2414 skip_(__FILE__
, line
)("Not NT platform - skipping tests\n");
2418 status
= pNtQueryObject( handle
, ObjectBasicInformation
, &obj_info
,
2419 sizeof(obj_info
), NULL
);
2420 ok_(__FILE__
, line
)(!status
, "NtQueryObject with err: %08x\n", status
);
2422 ok_(__FILE__
, line
)(obj_info
.GrantedAccess
== access
||
2423 obj_info
.GrantedAccess
== alt
, "Granted access should be 0x%08x "
2424 "or 0x%08x, instead of 0x%08x\n", access
, alt
, obj_info
.GrantedAccess
);
2426 ok_(__FILE__
, line
)(obj_info
.GrantedAccess
== access
, "Granted access should "
2427 "be 0x%08x, instead of 0x%08x\n", access
, obj_info
.GrantedAccess
);
2430 #define CHECK_SET_SECURITY(o,i,e) \
2434 SetLastError( 0xdeadbeef ); \
2435 res = SetKernelObjectSecurity( o, i, SecurityDescriptor ); \
2436 err = GetLastError(); \
2437 if (e == ERROR_SUCCESS) \
2438 ok(res, "SetKernelObjectSecurity failed with %d\n", err); \
2440 ok(!res && err == e, "SetKernelObjectSecurity should have failed " \
2441 "with %s, instead of %d\n", #e, err); \
2444 static void test_process_security(void)
2448 PTOKEN_PRIMARY_GROUP group
;
2449 PSID AdminSid
= NULL
, UsersSid
= NULL
;
2451 SECURITY_DESCRIPTOR
*SecurityDescriptor
= NULL
;
2452 char buffer
[MAX_PATH
];
2453 PROCESS_INFORMATION info
;
2454 STARTUPINFOA startup
;
2455 SECURITY_ATTRIBUTES psa
;
2456 HANDLE token
, event
;
2459 Acl
= HeapAlloc(GetProcessHeap(), 0, 256);
2460 res
= InitializeAcl(Acl
, 256, ACL_REVISION
);
2461 if (!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2463 win_skip("ACLs not implemented - skipping tests\n");
2464 HeapFree(GetProcessHeap(), 0, Acl
);
2467 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
2469 /* get owner from the token we might be running as a user not admin */
2470 res
= OpenProcessToken( GetCurrentProcess(), MAXIMUM_ALLOWED
, &token
);
2471 ok(res
, "OpenProcessToken failed with error %d\n", GetLastError());
2474 HeapFree(GetProcessHeap(), 0, Acl
);
2478 res
= GetTokenInformation( token
, TokenOwner
, NULL
, 0, &size
);
2479 ok(!res
, "Expected failure, got %d\n", res
);
2480 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2481 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2483 owner
= HeapAlloc(GetProcessHeap(), 0, size
);
2484 res
= GetTokenInformation( token
, TokenOwner
, owner
, size
, &size
);
2485 ok(res
, "GetTokenInformation failed with error %d\n", GetLastError());
2486 AdminSid
= ((TOKEN_OWNER
*)owner
)->Owner
;
2488 res
= GetTokenInformation( token
, TokenPrimaryGroup
, NULL
, 0, &size
);
2489 ok(!res
, "Expected failure, got %d\n", res
);
2490 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2491 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2493 group
= HeapAlloc(GetProcessHeap(), 0, size
);
2494 res
= GetTokenInformation( token
, TokenPrimaryGroup
, group
, size
, &size
);
2495 ok(res
, "GetTokenInformation failed with error %d\n", GetLastError());
2496 UsersSid
= ((TOKEN_PRIMARY_GROUP
*)group
)->PrimaryGroup
;
2498 CloseHandle( token
);
2501 HeapFree(GetProcessHeap(), 0, group
);
2502 HeapFree(GetProcessHeap(), 0, owner
);
2503 HeapFree(GetProcessHeap(), 0, Acl
);
2507 res
= AddAccessDeniedAce(Acl
, ACL_REVISION
, PROCESS_VM_READ
, AdminSid
);
2508 ok(res
, "AddAccessDeniedAce failed with error %d\n", GetLastError());
2509 res
= AddAccessAllowedAce(Acl
, ACL_REVISION
, PROCESS_ALL_ACCESS
, AdminSid
);
2510 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2512 SecurityDescriptor
= HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
2513 res
= InitializeSecurityDescriptor(SecurityDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
2514 ok(res
, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
2516 event
= CreateEvent( NULL
, TRUE
, TRUE
, "test_event" );
2517 ok(event
!= NULL
, "CreateEvent %d\n", GetLastError());
2519 SecurityDescriptor
->Revision
= 0;
2520 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_UNKNOWN_REVISION
);
2521 SecurityDescriptor
->Revision
= SECURITY_DESCRIPTOR_REVISION
;
2523 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_INVALID_SECURITY_DESCR
);
2524 CHECK_SET_SECURITY( event
, GROUP_SECURITY_INFORMATION
, ERROR_INVALID_SECURITY_DESCR
);
2525 CHECK_SET_SECURITY( event
, SACL_SECURITY_INFORMATION
, ERROR_ACCESS_DENIED
);
2526 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2527 /* NULL DACL is valid and means default DACL from token */
2528 SecurityDescriptor
->Control
|= SE_DACL_PRESENT
;
2529 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2531 /* Set owner and group and dacl */
2532 res
= SetSecurityDescriptorOwner(SecurityDescriptor
, AdminSid
, FALSE
);
2533 ok(res
, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
2534 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2535 res
= SetSecurityDescriptorGroup(SecurityDescriptor
, UsersSid
, FALSE
);
2536 ok(res
, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2537 CHECK_SET_SECURITY( event
, GROUP_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2538 res
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
2539 ok(res
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
2540 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2542 sprintf(buffer
, "%s tests/security.c test", myARGV
[0]);
2543 memset(&startup
, 0, sizeof(startup
));
2544 startup
.cb
= sizeof(startup
);
2545 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
2546 startup
.wShowWindow
= SW_SHOWNORMAL
;
2548 psa
.nLength
= sizeof(psa
);
2549 psa
.lpSecurityDescriptor
= SecurityDescriptor
;
2550 psa
.bInheritHandle
= TRUE
;
2552 /* Doesn't matter what ACL say we should get full access for ourselves */
2553 res
= CreateProcessA( NULL
, buffer
, &psa
, NULL
, FALSE
, 0, NULL
, NULL
, &startup
, &info
);
2554 ok(res
, "CreateProcess with err:%d\n", GetLastError());
2555 TEST_GRANTED_ACCESS2( info
.hProcess
, PROCESS_ALL_ACCESS_NT4
,
2556 STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
);
2557 winetest_wait_child_process( info
.hProcess
);
2559 CloseHandle( info
.hProcess
);
2560 CloseHandle( info
.hThread
);
2561 CloseHandle( event
);
2562 HeapFree(GetProcessHeap(), 0, group
);
2563 HeapFree(GetProcessHeap(), 0, owner
);
2564 HeapFree(GetProcessHeap(), 0, Acl
);
2565 HeapFree(GetProcessHeap(), 0, SecurityDescriptor
);
2568 static void test_process_security_child(void)
2570 HANDLE handle
, handle1
;
2574 handle
= OpenProcess( PROCESS_TERMINATE
, FALSE
, GetCurrentProcessId() );
2575 ok(handle
!= NULL
, "OpenProcess(PROCESS_TERMINATE) with err:%d\n", GetLastError());
2576 TEST_GRANTED_ACCESS( handle
, PROCESS_TERMINATE
);
2578 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2579 &handle1
, 0, TRUE
, DUPLICATE_SAME_ACCESS
);
2580 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2581 TEST_GRANTED_ACCESS( handle1
, PROCESS_TERMINATE
);
2583 CloseHandle( handle1
);
2585 SetLastError( 0xdeadbeef );
2586 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2587 &handle1
, PROCESS_ALL_ACCESS
, TRUE
, 0 );
2588 err
= GetLastError();
2590 ok(!ret
&& err
== ERROR_ACCESS_DENIED
, "duplicating handle should have failed "
2591 "with STATUS_ACCESS_DENIED, instead of err:%d\n", err
);
2593 CloseHandle( handle
);
2595 /* These two should fail - they are denied by ACL */
2596 handle
= OpenProcess( PROCESS_VM_READ
, FALSE
, GetCurrentProcessId() );
2598 ok(handle
== NULL
, "OpenProcess(PROCESS_VM_READ) should have failed\n");
2599 handle
= OpenProcess( PROCESS_ALL_ACCESS
, FALSE
, GetCurrentProcessId() );
2601 ok(handle
== NULL
, "OpenProcess(PROCESS_ALL_ACCESS) should have failed\n");
2603 /* Documented privilege elevation */
2604 ret
= DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2605 &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
);
2606 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2607 TEST_GRANTED_ACCESS2( handle
, PROCESS_ALL_ACCESS_NT4
,
2608 STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
);
2610 CloseHandle( handle
);
2612 /* Same only explicitly asking for all access rights */
2613 ret
= DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2614 &handle
, PROCESS_ALL_ACCESS
, TRUE
, 0 );
2615 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2616 TEST_GRANTED_ACCESS2( handle
, PROCESS_ALL_ACCESS_NT4
,
2617 PROCESS_ALL_ACCESS
| PROCESS_QUERY_LIMITED_INFORMATION
);
2618 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2619 &handle1
, PROCESS_VM_READ
, TRUE
, 0 );
2620 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2621 TEST_GRANTED_ACCESS( handle1
, PROCESS_VM_READ
);
2622 CloseHandle( handle1
);
2623 CloseHandle( handle
);
2626 static void test_impersonation_level(void)
2628 HANDLE Token
, ProcessToken
;
2631 TOKEN_PRIVILEGES
*Privileges
;
2633 PRIVILEGE_SET
*PrivilegeSet
;
2639 pDuplicateTokenEx
= (void *)GetProcAddress(hmod
, "DuplicateTokenEx");
2640 if( !pDuplicateTokenEx
) {
2641 win_skip("DuplicateTokenEx is not available\n");
2644 SetLastError(0xdeadbeef);
2645 ret
= ImpersonateSelf(SecurityAnonymous
);
2646 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
2648 win_skip("ImpersonateSelf is not implemented\n");
2651 ok(ret
, "ImpersonateSelf(SecurityAnonymous) failed with error %d\n", GetLastError());
2652 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2653 ok(!ret
, "OpenThreadToken should have failed\n");
2654 error
= GetLastError();
2655 ok(error
== ERROR_CANT_OPEN_ANONYMOUS
, "OpenThreadToken on anonymous token should have returned ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error
);
2656 /* can't perform access check when opening object against an anonymous impersonation token */
2658 error
= RegOpenKeyEx(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2659 ok(error
== ERROR_INVALID_HANDLE
|| error
== ERROR_CANT_OPEN_ANONYMOUS
,
2660 "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE or ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error
);
2664 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
, &ProcessToken
);
2665 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
2667 ret
= pDuplicateTokenEx(ProcessToken
,
2668 TOKEN_QUERY
| TOKEN_DUPLICATE
| TOKEN_IMPERSONATE
, NULL
,
2669 SecurityAnonymous
, TokenImpersonation
, &Token
);
2670 ok(ret
, "DuplicateTokenEx failed with error %d\n", GetLastError());
2671 /* can't increase the impersonation level */
2672 ret
= DuplicateToken(Token
, SecurityIdentification
, &Token2
);
2673 error
= GetLastError();
2674 ok(!ret
&& error
== ERROR_BAD_IMPERSONATION_LEVEL
,
2675 "Duplicating a token and increasing the impersonation level should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2676 /* we can query anything from an anonymous token, including the user */
2677 ret
= GetTokenInformation(Token
, TokenUser
, NULL
, 0, &Size
);
2678 error
= GetLastError();
2679 ok(!ret
&& error
== ERROR_INSUFFICIENT_BUFFER
, "GetTokenInformation(TokenUser) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error
);
2680 User
= HeapAlloc(GetProcessHeap(), 0, Size
);
2681 ret
= GetTokenInformation(Token
, TokenUser
, User
, Size
, &Size
);
2682 ok(ret
, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
2683 HeapFree(GetProcessHeap(), 0, User
);
2685 /* PrivilegeCheck fails with SecurityAnonymous level */
2686 ret
= GetTokenInformation(Token
, TokenPrivileges
, NULL
, 0, &Size
);
2687 error
= GetLastError();
2688 ok(!ret
&& error
== ERROR_INSUFFICIENT_BUFFER
, "GetTokenInformation(TokenPrivileges) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error
);
2689 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
2690 ret
= GetTokenInformation(Token
, TokenPrivileges
, Privileges
, Size
, &Size
);
2691 ok(ret
, "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
2693 PrivilegeSet
= HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PRIVILEGE_SET
, Privilege
[Privileges
->PrivilegeCount
]));
2694 PrivilegeSet
->PrivilegeCount
= Privileges
->PrivilegeCount
;
2695 memcpy(PrivilegeSet
->Privilege
, Privileges
->Privileges
, PrivilegeSet
->PrivilegeCount
* sizeof(PrivilegeSet
->Privilege
[0]));
2696 PrivilegeSet
->Control
= PRIVILEGE_SET_ALL_NECESSARY
;
2697 HeapFree(GetProcessHeap(), 0, Privileges
);
2699 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2700 error
= GetLastError();
2701 ok(!ret
&& error
== ERROR_BAD_IMPERSONATION_LEVEL
, "PrivilegeCheck for SecurityAnonymous token should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2705 ret
= ImpersonateSelf(SecurityIdentification
);
2706 ok(ret
, "ImpersonateSelf(SecurityIdentification) failed with error %d\n", GetLastError());
2707 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2708 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
2710 /* can't perform access check when opening object against an identification impersonation token */
2711 error
= RegOpenKeyEx(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2713 ok(error
== ERROR_INVALID_HANDLE
|| error
== ERROR_BAD_IMPERSONATION_LEVEL
,
2714 "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE or ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2716 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2717 ok(ret
, "PrivilegeCheck for SecurityIdentification failed with error %d\n", GetLastError());
2721 ret
= ImpersonateSelf(SecurityImpersonation
);
2722 ok(ret
, "ImpersonateSelf(SecurityImpersonation) failed with error %d\n", GetLastError());
2723 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2724 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
2725 error
= RegOpenKeyEx(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2726 ok(error
== ERROR_SUCCESS
, "RegOpenKeyEx should have succeeded instead of failing with %d\n", error
);
2728 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2729 ok(ret
, "PrivilegeCheck for SecurityImpersonation failed with error %d\n", GetLastError());
2733 CloseHandle(ProcessToken
);
2735 HeapFree(GetProcessHeap(), 0, PrivilegeSet
);
2738 static void test_SetEntriesInAclW(void)
2741 PSID EveryoneSid
= NULL
, UsersSid
= NULL
;
2742 PACL OldAcl
= NULL
, NewAcl
;
2743 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
2744 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
2745 EXPLICIT_ACCESSW ExplicitAccess
;
2746 static const WCHAR wszEveryone
[] = {'E','v','e','r','y','o','n','e',0};
2747 static const WCHAR wszCurrentUser
[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
2749 if (!pSetEntriesInAclW
)
2751 win_skip("SetEntriesInAclW is not available\n");
2755 NewAcl
= (PACL
)0xdeadbeef;
2756 res
= pSetEntriesInAclW(0, NULL
, NULL
, &NewAcl
);
2757 if(res
== ERROR_CALL_NOT_IMPLEMENTED
)
2759 win_skip("SetEntriesInAclW is not implemented\n");
2762 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2763 ok(NewAcl
== NULL
||
2764 broken(NewAcl
!= NULL
), /* NT4 */
2765 "NewAcl=%p, expected NULL\n", NewAcl
);
2768 OldAcl
= HeapAlloc(GetProcessHeap(), 0, 256);
2769 res
= InitializeAcl(OldAcl
, 256, ACL_REVISION
);
2770 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2772 win_skip("ACLs not implemented - skipping tests\n");
2773 HeapFree(GetProcessHeap(), 0, OldAcl
);
2776 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
2778 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
2779 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2781 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
2782 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
2783 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2785 res
= AddAccessAllowedAce(OldAcl
, ACL_REVISION
, KEY_READ
, UsersSid
);
2786 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2788 ExplicitAccess
.grfAccessPermissions
= KEY_WRITE
;
2789 ExplicitAccess
.grfAccessMode
= GRANT_ACCESS
;
2790 ExplicitAccess
.grfInheritance
= NO_INHERITANCE
;
2791 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_WELL_KNOWN_GROUP
;
2792 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2793 ExplicitAccess
.Trustee
.ptstrName
= EveryoneSid
;
2794 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= 0xDEADBEEF;
2795 ExplicitAccess
.Trustee
.pMultipleTrustee
= (PVOID
)0xDEADBEEF;
2796 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2797 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2798 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2801 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2802 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
2803 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2804 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2805 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2806 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2809 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2811 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2815 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2816 ExplicitAccess
.Trustee
.ptstrName
= (LPWSTR
)wszEveryone
;
2817 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2818 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2819 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2822 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_BAD_FORM
;
2823 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2824 ok(res
== ERROR_INVALID_PARAMETER
||
2825 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2826 "SetEntriesInAclW failed: %u\n", res
);
2827 ok(NewAcl
== NULL
||
2828 broken(NewAcl
!= NULL
), /* NT4 */
2829 "returned acl wasn't NULL: %p\n", NewAcl
);
2831 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2832 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= TRUSTEE_IS_IMPERSONATE
;
2833 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2834 ok(res
== ERROR_INVALID_PARAMETER
||
2835 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2836 "SetEntriesInAclW failed: %u\n", res
);
2837 ok(NewAcl
== NULL
||
2838 broken(NewAcl
!= NULL
), /* NT4 */
2839 "returned acl wasn't NULL: %p\n", NewAcl
);
2841 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2842 ExplicitAccess
.grfAccessMode
= SET_ACCESS
;
2843 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2844 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2845 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2849 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2850 ExplicitAccess
.Trustee
.ptstrName
= (LPWSTR
)wszCurrentUser
;
2851 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2852 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2853 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2856 ExplicitAccess
.grfAccessMode
= REVOKE_ACCESS
;
2857 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2858 ExplicitAccess
.Trustee
.ptstrName
= UsersSid
;
2859 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2860 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2861 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2865 FreeSid(EveryoneSid
);
2866 HeapFree(GetProcessHeap(), 0, OldAcl
);
2869 static void test_SetEntriesInAclA(void)
2872 PSID EveryoneSid
= NULL
, UsersSid
= NULL
;
2873 PACL OldAcl
= NULL
, NewAcl
;
2874 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
2875 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
2876 EXPLICIT_ACCESS ExplicitAccess
;
2877 static const CHAR szEveryone
[] = {'E','v','e','r','y','o','n','e',0};
2878 static const CHAR szCurrentUser
[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
2880 if (!pSetEntriesInAclA
)
2882 win_skip("SetEntriesInAclA is not available\n");
2886 NewAcl
= (PACL
)0xdeadbeef;
2887 res
= pSetEntriesInAclA(0, NULL
, NULL
, &NewAcl
);
2888 if(res
== ERROR_CALL_NOT_IMPLEMENTED
)
2890 win_skip("SetEntriesInAclA is not implemented\n");
2893 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2894 ok(NewAcl
== NULL
||
2895 broken(NewAcl
!= NULL
), /* NT4 */
2896 "NewAcl=%p, expected NULL\n", NewAcl
);
2899 OldAcl
= HeapAlloc(GetProcessHeap(), 0, 256);
2900 res
= InitializeAcl(OldAcl
, 256, ACL_REVISION
);
2901 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2903 win_skip("ACLs not implemented - skipping tests\n");
2904 HeapFree(GetProcessHeap(), 0, OldAcl
);
2907 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
2909 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
2910 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2912 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
2913 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
2914 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2916 res
= AddAccessAllowedAce(OldAcl
, ACL_REVISION
, KEY_READ
, UsersSid
);
2917 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2919 ExplicitAccess
.grfAccessPermissions
= KEY_WRITE
;
2920 ExplicitAccess
.grfAccessMode
= GRANT_ACCESS
;
2921 ExplicitAccess
.grfInheritance
= NO_INHERITANCE
;
2922 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_WELL_KNOWN_GROUP
;
2923 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2924 ExplicitAccess
.Trustee
.ptstrName
= EveryoneSid
;
2925 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2926 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
2927 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2928 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2929 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2932 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2933 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
2934 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2935 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2936 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2937 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2940 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2942 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2946 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2947 ExplicitAccess
.Trustee
.ptstrName
= (LPSTR
)szEveryone
;
2948 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2949 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2950 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2953 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_BAD_FORM
;
2954 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2955 ok(res
== ERROR_INVALID_PARAMETER
||
2956 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2957 "SetEntriesInAclA failed: %u\n", res
);
2958 ok(NewAcl
== NULL
||
2959 broken(NewAcl
!= NULL
), /* NT4 */
2960 "returned acl wasn't NULL: %p\n", NewAcl
);
2962 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2963 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= TRUSTEE_IS_IMPERSONATE
;
2964 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2965 ok(res
== ERROR_INVALID_PARAMETER
||
2966 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2967 "SetEntriesInAclA failed: %u\n", res
);
2968 ok(NewAcl
== NULL
||
2969 broken(NewAcl
!= NULL
), /* NT4 */
2970 "returned acl wasn't NULL: %p\n", NewAcl
);
2972 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2973 ExplicitAccess
.grfAccessMode
= SET_ACCESS
;
2974 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2975 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2976 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2980 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2981 ExplicitAccess
.Trustee
.ptstrName
= (LPSTR
)szCurrentUser
;
2982 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2983 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2984 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2987 ExplicitAccess
.grfAccessMode
= REVOKE_ACCESS
;
2988 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2989 ExplicitAccess
.Trustee
.ptstrName
= UsersSid
;
2990 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2991 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2992 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2996 FreeSid(EveryoneSid
);
2997 HeapFree(GetProcessHeap(), 0, OldAcl
);
3000 static void test_GetNamedSecurityInfoA(void)
3002 PSECURITY_DESCRIPTOR pSecDesc
;
3004 SECURITY_DESCRIPTOR_CONTROL control
;
3008 BOOL owner_defaulted
;
3009 BOOL group_defaulted
;
3012 CHAR windows_dir
[MAX_PATH
];
3014 if (!pGetNamedSecurityInfoA
)
3016 win_skip("GetNamedSecurityInfoA is not available\n");
3020 ret
= GetWindowsDirectoryA(windows_dir
, MAX_PATH
);
3021 ok(ret
, "GetWindowsDirectory failed with error %d\n", GetLastError());
3023 SetLastError(0xdeadbeef);
3024 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,
3025 OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
3026 NULL
, NULL
, NULL
, NULL
, &pSecDesc
);
3027 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3029 win_skip("GetNamedSecurityInfoA is not implemented\n");
3032 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3034 ret
= GetSecurityDescriptorControl(pSecDesc
, &control
, &revision
);
3035 ok(ret
, "GetSecurityDescriptorControl failed with error %d\n", GetLastError());
3036 ok((control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == (SE_SELF_RELATIVE
|SE_DACL_PRESENT
) ||
3037 broken((control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == SE_DACL_PRESENT
), /* NT4 */
3038 "control (0x%x) doesn't have (SE_SELF_RELATIVE|SE_DACL_PRESENT) flags set\n", control
);
3039 ok(revision
== SECURITY_DESCRIPTOR_REVISION1
, "revision was %d instead of 1\n", revision
);
3041 isNT4
= (control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == SE_DACL_PRESENT
;
3043 ret
= GetSecurityDescriptorOwner(pSecDesc
, &owner
, &owner_defaulted
);
3044 ok(ret
, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
3045 ok(owner
!= NULL
, "owner should not be NULL\n");
3047 ret
= GetSecurityDescriptorGroup(pSecDesc
, &group
, &group_defaulted
);
3048 ok(ret
, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
3049 ok(group
!= NULL
, "group should not be NULL\n");
3050 LocalFree(pSecDesc
);
3053 /* NULL descriptor tests */
3056 win_skip("NT4 does not support GetNamedSecutityInfo with a NULL descriptor\n");
3060 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,DACL_SECURITY_INFORMATION
,
3061 NULL
, NULL
, NULL
, NULL
, NULL
);
3062 ok(error
==ERROR_INVALID_PARAMETER
, "GetNamedSecurityInfo failed with error %d\n", error
);
3064 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,DACL_SECURITY_INFORMATION
,
3065 NULL
, NULL
, &dacl
, NULL
, NULL
);
3066 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3067 ok(dacl
!= NULL
, "dacl should not be NULL\n");
3069 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,OWNER_SECURITY_INFORMATION
,
3070 NULL
, NULL
, &dacl
, NULL
, NULL
);
3071 ok(error
==ERROR_INVALID_PARAMETER
, "GetNamedSecurityInfo failed with error %d\n", error
);
3074 static void test_ConvertStringSecurityDescriptor(void)
3077 PSECURITY_DESCRIPTOR pSD
;
3078 static const WCHAR Blank
[] = { 0 };
3082 const char *sidstring
;
3089 { "D:(A;;GA;;;WD)", 0xdeadbeef, FALSE
, ERROR_UNKNOWN_REVISION
},
3090 /* test ACE string type */
3091 { "D:(A;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3092 { "D:(D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3093 { "ERROR:(D;;GA;;;WD)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_PARAMETER
},
3094 /* test ACE string with spaces */
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
, TRUE
},
3098 { "D:(D ;;GA;;;WD)", SDDL_REVISION_1
, FALSE
, RPC_S_INVALID_STRING_UUID
, ERROR_INVALID_ACL
}, /* Vista+ */
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
, FALSE
, ERROR_INVALID_ACL
},
3102 { "D:(D;;GA; ;;WD)", SDDL_REVISION_1
, TRUE
},
3103 { "D:(D;;GA;; ;WD)", SDDL_REVISION_1
, TRUE
},
3104 { "D:(D;;GA;;; WD)", SDDL_REVISION_1
, TRUE
},
3105 { "D:(D;;GA;;;WD )", SDDL_REVISION_1
, TRUE
},
3106 /* test ACE string access rights */
3107 { "D:(A;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3108 { "D:(A;;GRGWGX;;;WD)", SDDL_REVISION_1
, TRUE
},
3109 { "D:(A;;RCSDWDWO;;;WD)", SDDL_REVISION_1
, TRUE
},
3110 { "D:(A;;RPWPCCDCLCSWLODTCR;;;WD)", SDDL_REVISION_1
, TRUE
},
3111 { "D:(A;;FAFRFWFX;;;WD)", SDDL_REVISION_1
, TRUE
},
3112 { "D:(A;;KAKRKWKX;;;WD)", SDDL_REVISION_1
, TRUE
},
3113 { "D:(A;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1
, TRUE
},
3114 { "S:(AU;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1
, TRUE
},
3115 /* test ACE string access right error case */
3116 { "D:(A;;ROB;;;WD)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_ACL
},
3117 /* test behaviour with empty strings */
3118 { "", SDDL_REVISION_1
, TRUE
},
3119 /* test ACE string SID */
3120 { "D:(D;;GA;;;S-1-0-0)", SDDL_REVISION_1
, TRUE
},
3121 { "D:(D;;GA;;;Nonexistent account)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_ACL
, ERROR_INVALID_SID
} /* W2K */
3124 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA
)
3126 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
3130 for (i
= 0; i
< sizeof(cssd
)/sizeof(cssd
[0]); i
++)
3134 SetLastError(0xdeadbeef);
3135 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3136 cssd
[i
].sidstring
, cssd
[i
].revision
, &pSD
, NULL
);
3137 GLE
= GetLastError();
3138 ok(ret
== cssd
[i
].ret
, "(%02u) Expected %s (%d)\n", i
, cssd
[i
].ret
? "success" : "failure", GLE
);
3140 ok(GLE
== cssd
[i
].GLE
||
3141 (cssd
[i
].altGLE
&& GLE
== cssd
[i
].altGLE
),
3142 "(%02u) Unexpected last error %d\n", i
, GLE
);
3147 /* test behaviour with NULL parameters */
3148 SetLastError(0xdeadbeef);
3149 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3150 NULL
, 0xdeadbeef, &pSD
, NULL
);
3152 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3153 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3156 SetLastError(0xdeadbeef);
3157 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorW(
3158 NULL
, 0xdeadbeef, &pSD
, NULL
);
3159 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3160 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3163 SetLastError(0xdeadbeef);
3164 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3165 "D:(A;;ROB;;;WD)", 0xdeadbeef, NULL
, NULL
);
3166 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3167 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3170 SetLastError(0xdeadbeef);
3171 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3172 "D:(A;;ROB;;;WD)", SDDL_REVISION_1
, NULL
, NULL
);
3173 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3174 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3177 /* test behaviour with empty strings */
3178 SetLastError(0xdeadbeef);
3179 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorW(
3180 Blank
, SDDL_REVISION_1
, &pSD
, NULL
);
3181 ok(ret
, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
3185 static void test_ConvertSecurityDescriptorToString(void)
3187 SECURITY_DESCRIPTOR desc
;
3188 SECURITY_INFORMATION sec_info
= OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
|SACL_SECURITY_INFORMATION
;
3197 if (!pConvertSecurityDescriptorToStringSecurityDescriptorA
)
3199 win_skip("ConvertSecurityDescriptorToStringSecurityDescriptor is not available\n");
3202 if (!pCreateWellKnownSid
)
3204 win_skip("CreateWellKnownSid is not available\n");
3208 /* It seems Windows XP adds an extra character to the length of the string for each ACE in an ACL. We
3209 * don't replicate this feature so we only test len >= strlen+1. */
3210 #define CHECK_RESULT_AND_FREE(exp_str) \
3211 ok(strcmp(string, (exp_str)) == 0, "String mismatch (expected \"%s\", got \"%s\")\n", (exp_str), string); \
3212 ok(len >= (strlen(exp_str) + 1), "Length mismatch (expected %d, got %d)\n", lstrlen(exp_str) + 1, len); \
3215 #define CHECK_ONE_OF_AND_FREE(exp_str1, exp_str2) \
3216 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); \
3217 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); \
3220 InitializeSecurityDescriptor(&desc
, SECURITY_DESCRIPTOR_REVISION
);
3221 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3222 CHECK_RESULT_AND_FREE("");
3225 pCreateWellKnownSid(WinLocalSid
, NULL
, sid_buf
, &size
);
3226 SetSecurityDescriptorOwner(&desc
, sid_buf
, FALSE
);
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 SetSecurityDescriptorOwner(&desc
, sid_buf
, TRUE
);
3231 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3232 CHECK_RESULT_AND_FREE("O:S-1-2-0");
3234 size
= sizeof(sid_buf
);
3235 pCreateWellKnownSid(WinLocalSystemSid
, NULL
, sid_buf
, &size
);
3236 SetSecurityDescriptorOwner(&desc
, sid_buf
, TRUE
);
3237 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3238 CHECK_RESULT_AND_FREE("O:SY");
3240 pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid
);
3241 SetSecurityDescriptorGroup(&desc
, psid
, TRUE
);
3242 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3243 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576");
3245 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, GROUP_SECURITY_INFORMATION
, &string
, &len
), "Conversion failed\n");
3246 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
3248 pacl
= (PACL
)acl_buf
;
3249 InitializeAcl(pacl
, sizeof(acl_buf
), ACL_REVISION
);
3250 SetSecurityDescriptorDacl(&desc
, TRUE
, pacl
, TRUE
);
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 SetSecurityDescriptorDacl(&desc
, TRUE
, pacl
, FALSE
);
3255 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3256 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
3258 pConvertStringSidToSidA("S-1-5-6", &psid2
);
3259 pAddAccessAllowedAceEx(pacl
, ACL_REVISION
, NO_PROPAGATE_INHERIT_ACE
, 0xf0000000, 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)");
3263 pAddAccessAllowedAceEx(pacl
, ACL_REVISION
, INHERIT_ONLY_ACE
|INHERITED_ACE
, 0x00000003, psid2
);
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)");
3267 pAddAccessDeniedAceEx(pacl
, ACL_REVISION
, OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
, 0xffffffff, psid
);
3268 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3269 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)");
3272 pacl
= (PACL
)acl_buf
;
3273 InitializeAcl(pacl
, sizeof(acl_buf
), ACL_REVISION
);
3274 SetSecurityDescriptorSacl(&desc
, TRUE
, pacl
, FALSE
);
3275 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3276 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:");
3278 /* fails in win2k */
3279 SetSecurityDescriptorDacl(&desc
, TRUE
, NULL
, FALSE
);
3280 pAddAuditAccessAceEx(pacl
, ACL_REVISION
, VALID_INHERIT_FLAGS
, KEY_READ
|KEY_WRITE
, psid2
, TRUE
, TRUE
);
3281 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
))
3283 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)", /* XP */
3284 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)" /* Vista */);
3287 /* fails in win2k */
3288 pAddAuditAccessAceEx(pacl
, ACL_REVISION
, NO_PROPAGATE_INHERIT_ACE
, FILE_GENERIC_READ
|FILE_GENERIC_WRITE
, psid2
, TRUE
, FALSE
);
3289 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
))
3291 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", /* XP */
3292 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)" /* Vista */);
3299 static void test_SetSecurityDescriptorControl (PSECURITY_DESCRIPTOR sec
)
3301 SECURITY_DESCRIPTOR_CONTROL ref
;
3302 SECURITY_DESCRIPTOR_CONTROL test
;
3304 SECURITY_DESCRIPTOR_CONTROL
const mutable
3305 = SE_DACL_AUTO_INHERIT_REQ
| SE_SACL_AUTO_INHERIT_REQ
3306 | SE_DACL_AUTO_INHERITED
| SE_SACL_AUTO_INHERITED
3307 | SE_DACL_PROTECTED
| SE_SACL_PROTECTED
3308 | 0x00000040 | 0x00000080 /* not defined in winnt.h */
3310 SECURITY_DESCRIPTOR_CONTROL
const immutable
3311 = SE_OWNER_DEFAULTED
| SE_GROUP_DEFAULTED
3312 | SE_DACL_PRESENT
| SE_DACL_DEFAULTED
3313 | SE_SACL_PRESENT
| SE_SACL_DEFAULTED
3314 | SE_RM_CONTROL_VALID
| SE_SELF_RELATIVE
3319 LPCSTR fmt
= "Expected error %s, got %u\n";
3321 GetSecurityDescriptorControl (sec
, &ref
, &dwRevision
);
3323 /* The mutable bits are mutable regardless of the truth of
3324 SE_DACL_PRESENT and/or SE_SACL_PRESENT */
3326 /* Check call barfs if any bit-of-interest is immutable */
3327 for (bit
= 0; bit
< 16; ++bit
)
3329 SECURITY_DESCRIPTOR_CONTROL
const bitOfInterest
= 1 << bit
;
3330 SECURITY_DESCRIPTOR_CONTROL setOrClear
= ref
& bitOfInterest
;
3332 SECURITY_DESCRIPTOR_CONTROL ctrl
;
3334 DWORD dwExpect
= (bitOfInterest
& immutable
)
3335 ? ERROR_INVALID_PARAMETER
: 0xbebecaca;
3336 LPCSTR strExpect
= (bitOfInterest
& immutable
)
3337 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
3339 ctrl
= (bitOfInterest
& mutable) ? ref
+ bitOfInterest
: ref
;
3340 setOrClear
^= bitOfInterest
;
3341 SetLastError (0xbebecaca);
3342 pSetSecurityDescriptorControl (sec
, bitOfInterest
, setOrClear
);
3343 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3344 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
3345 expect_eq(test
, ctrl
, int, "%x");
3347 setOrClear
^= bitOfInterest
;
3348 SetLastError (0xbebecaca);
3349 pSetSecurityDescriptorControl (sec
, bitOfInterest
, setOrClear
);
3350 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3351 GetSecurityDescriptorControl (sec
, &test
, &dwRevision
);
3352 expect_eq(test
, ref
, int, "%x");
3355 /* Check call barfs if any bit-to-set is immutable
3356 even when not a bit-of-interest */
3357 for (bit
= 0; bit
< 16; ++bit
)
3359 SECURITY_DESCRIPTOR_CONTROL
const bitsOfInterest
= mutable;
3360 SECURITY_DESCRIPTOR_CONTROL setOrClear
= ref
& bitsOfInterest
;
3362 SECURITY_DESCRIPTOR_CONTROL ctrl
;
3364 DWORD dwExpect
= ((1 << bit
) & immutable
)
3365 ? ERROR_INVALID_PARAMETER
: 0xbebecaca;
3366 LPCSTR strExpect
= ((1 << bit
) & immutable
)
3367 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
3369 ctrl
= ((1 << bit
) & immutable
) ? test
: ref
| mutable;
3370 setOrClear
^= bitsOfInterest
;
3371 SetLastError (0xbebecaca);
3372 pSetSecurityDescriptorControl (sec
, bitsOfInterest
, setOrClear
| (1 << bit
));
3373 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3374 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
3375 expect_eq(test
, ctrl
, int, "%x");
3377 ctrl
= ((1 << bit
) & immutable
) ? test
: ref
| (1 << bit
);
3378 setOrClear
^= bitsOfInterest
;
3379 SetLastError (0xbebecaca);
3380 pSetSecurityDescriptorControl (sec
, bitsOfInterest
, setOrClear
| (1 << bit
));
3381 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3382 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
3383 expect_eq(test
, ctrl
, int, "%x");
3387 static void test_PrivateObjectSecurity(void)
3389 SECURITY_INFORMATION sec_info
= OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
|SACL_SECURITY_INFORMATION
;
3390 SECURITY_DESCRIPTOR_CONTROL ctrl
;
3391 PSECURITY_DESCRIPTOR sec
;
3397 PSECURITY_DESCRIPTOR buf
;
3400 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA
)
3402 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
3406 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3408 "G:S-1-5-21-93476-23408-4576"
3409 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)"
3410 "(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3411 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
3412 SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
3414 test_SetSecurityDescriptorControl(sec
);
3418 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3420 "G:S-1-5-21-93476-23408-4576",
3421 SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
3423 test_SetSecurityDescriptorControl(sec
);
3427 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3429 "G:S-1-5-21-93476-23408-4576"
3430 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3431 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
3432 buf
= HeapAlloc(GetProcessHeap(), 0, dwDescSize
);
3433 pSetSecurityDescriptorControl(sec
, SE_DACL_PROTECTED
, SE_DACL_PROTECTED
);
3434 GetSecurityDescriptorControl(sec
, &ctrl
, &dwRevision
);
3435 expect_eq(ctrl
, 0x9014, int, "%x");
3437 ret
= GetPrivateObjectSecurity(sec
, GROUP_SECURITY_INFORMATION
, buf
, dwDescSize
, &retSize
);
3438 ok(ret
, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3439 ok(retSize
<= dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
3440 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3441 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
3442 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
3443 expect_eq(ctrl
, 0x8000, int, "%x");
3445 ret
= GetPrivateObjectSecurity(sec
, GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, buf
, dwDescSize
, &retSize
);
3446 ok(ret
, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3447 ok(retSize
<= dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
3448 ret
= pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
);
3449 ok(ret
, "Conversion failed err=%u\n", GetLastError());
3450 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)",
3451 "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 */
3452 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
3453 expect_eq(ctrl
& (~ SE_DACL_PROTECTED
), 0x8004, int, "%x");
3455 ret
= GetPrivateObjectSecurity(sec
, sec_info
, buf
, dwDescSize
, &retSize
);
3456 ok(ret
, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3457 ok(retSize
== dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
3458 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3459 CHECK_ONE_OF_AND_FREE("O:SY"
3460 "G:S-1-5-21-93476-23408-4576"
3461 "D:(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)",
3464 "G:S-1-5-21-93476-23408-4576"
3465 "D:P(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3466 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)"); /* Win7 */
3467 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
3468 expect_eq(ctrl
& (~ SE_DACL_PROTECTED
), 0x8014, int, "%x");
3470 SetLastError(0xdeadbeef);
3471 ok(GetPrivateObjectSecurity(sec
, sec_info
, buf
, 5, &retSize
) == FALSE
, "GetPrivateObjectSecurity should have failed\n");
3472 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Expected error ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
3475 HeapFree(GetProcessHeap(), 0, buf
);
3477 #undef CHECK_RESULT_AND_FREE
3478 #undef CHECK_ONE_OF_AND_FREE
3480 static void test_acls(void)
3483 PACL pAcl
= (PACL
)buffer
;
3486 SetLastError(0xdeadbeef);
3487 ret
= InitializeAcl(pAcl
, sizeof(ACL
) - 1, ACL_REVISION
);
3488 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
3490 win_skip("InitializeAcl is not implemented\n");
3494 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "InitializeAcl with too small a buffer should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
3496 SetLastError(0xdeadbeef);
3497 ret
= InitializeAcl(pAcl
, 0xffffffff, ACL_REVISION
);
3498 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl with too large a buffer should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3500 SetLastError(0xdeadbeef);
3501 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION1
);
3502 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl(ACL_REVISION1) should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3504 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION2
);
3505 ok(ret
, "InitializeAcl(ACL_REVISION2) failed with error %d\n", GetLastError());
3507 ret
= IsValidAcl(pAcl
);
3508 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
3510 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION3
);
3511 ok(ret
, "InitializeAcl(ACL_REVISION3) failed with error %d\n", GetLastError());
3513 ret
= IsValidAcl(pAcl
);
3514 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
3516 SetLastError(0xdeadbeef);
3517 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION4
);
3518 if (GetLastError() != ERROR_INVALID_PARAMETER
)
3520 ok(ret
, "InitializeAcl(ACL_REVISION4) failed with error %d\n", GetLastError());
3522 ret
= IsValidAcl(pAcl
);
3523 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
3526 win_skip("ACL_REVISION4 is not implemented on NT4\n");
3528 SetLastError(0xdeadbeef);
3529 ret
= InitializeAcl(pAcl
, sizeof(buffer
), -1);
3530 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl(-1) failed with error %d\n", GetLastError());
3533 static void test_GetSecurityInfo(void)
3536 PSECURITY_DESCRIPTOR sd
;
3541 if (!pGetSecurityInfo
)
3543 win_skip("GetSecurityInfo is not available\n");
3547 /* Create something. Files have lots of associated security info. */
3548 obj
= CreateFile(myARGV
[0], GENERIC_READ
, FILE_SHARE_READ
, NULL
,
3549 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3550 if (obj
== INVALID_HANDLE_VALUE
)
3552 skip("Couldn't create an object for GetSecurityInfo test\n");
3556 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
,
3557 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
3558 &owner
, &group
, &dacl
, NULL
, &sd
);
3559 if (ret
== ERROR_CALL_NOT_IMPLEMENTED
)
3561 win_skip("GetSecurityInfo is not implemented\n");
3565 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
3566 ok(sd
!= NULL
, "GetSecurityInfo\n");
3567 ok(owner
!= NULL
, "GetSecurityInfo\n");
3568 ok(group
!= NULL
, "GetSecurityInfo\n");
3570 ok(IsValidAcl(dacl
), "GetSecurityInfo\n");
3572 win_skip("No ACL information returned\n");
3576 if (!pCreateWellKnownSid
)
3578 win_skip("NULL parameter test would crash on NT4\n");
3583 /* If we don't ask for the security descriptor, Windows will still give us
3584 the other stuff, leaving us no way to free it. */
3585 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
,
3586 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
3587 &owner
, &group
, &dacl
, NULL
, NULL
);
3588 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
3589 ok(owner
!= NULL
, "GetSecurityInfo\n");
3590 ok(group
!= NULL
, "GetSecurityInfo\n");
3592 ok(IsValidAcl(dacl
), "GetSecurityInfo\n");
3594 win_skip("No ACL information returned\n");
3599 static void test_GetSidSubAuthority(void)
3603 if (!pGetSidSubAuthority
|| !pConvertStringSidToSidA
|| !pIsValidSid
|| !pGetSidSubAuthorityCount
)
3605 win_skip("Some functions not available\n");
3608 /* Note: on windows passing in an invalid index like -1, lets GetSidSubAuthority return 0x05000000 but
3609 still GetLastError returns ERROR_SUCCESS then. We don't test these unlikely cornercases here for now */
3610 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576",&psid
),"ConvertStringSidToSidA failed\n");
3611 ok(pIsValidSid(psid
),"Sid is not valid\n");
3612 SetLastError(0xbebecaca);
3613 ok(*pGetSidSubAuthorityCount(psid
) == 4,"GetSidSubAuthorityCount gave %d expected 4\n",*pGetSidSubAuthorityCount(psid
));
3614 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3615 SetLastError(0xbebecaca);
3616 ok(*pGetSidSubAuthority(psid
,0) == 21,"GetSidSubAuthority gave %d expected 21\n",*pGetSidSubAuthority(psid
,0));
3617 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3618 SetLastError(0xbebecaca);
3619 ok(*pGetSidSubAuthority(psid
,1) == 93476,"GetSidSubAuthority gave %d expected 93476\n",*pGetSidSubAuthority(psid
,1));
3620 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3621 SetLastError(0xbebecaca);
3622 ok(pGetSidSubAuthority(psid
,4) != NULL
,"Expected out of bounds GetSidSubAuthority to return a non-NULL pointer\n");
3623 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3627 static void test_CheckTokenMembership(void)
3629 PTOKEN_GROUPS token_groups
;
3631 HANDLE process_token
, token
;
3636 if (!pCheckTokenMembership
)
3638 win_skip("CheckTokenMembership is not available\n");
3641 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &process_token
);
3642 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
3644 ret
= DuplicateToken(process_token
, SecurityImpersonation
, &token
);
3645 ok(ret
, "DuplicateToken failed with error %d\n", GetLastError());
3648 ret
= GetTokenInformation(token
, TokenGroups
, NULL
, 0, &size
);
3649 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
3650 "GetTokenInformation(TokenGroups) %s with error %d\n",
3651 ret
? "succeeded" : "failed", GetLastError());
3652 token_groups
= HeapAlloc(GetProcessHeap(), 0, size
);
3653 ret
= GetTokenInformation(token
, TokenGroups
, token_groups
, size
, &size
);
3654 ok(ret
, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
3656 for (i
= 0; i
< token_groups
->GroupCount
; i
++)
3658 if (token_groups
->Groups
[i
].Attributes
& SE_GROUP_ENABLED
)
3662 if (i
== token_groups
->GroupCount
)
3664 HeapFree(GetProcessHeap(), 0, token_groups
);
3666 skip("user not a member of any group\n");
3671 ret
= pCheckTokenMembership(token
, token_groups
->Groups
[i
].Sid
, &is_member
);
3672 ok(ret
, "CheckTokenMembership failed with error %d\n", GetLastError());
3673 ok(is_member
, "CheckTokenMembership should have detected sid as member\n");
3676 ret
= pCheckTokenMembership(NULL
, token_groups
->Groups
[i
].Sid
, &is_member
);
3677 ok(ret
, "CheckTokenMembership failed with error %d\n", GetLastError());
3678 ok(is_member
, "CheckTokenMembership should have detected sid as member\n");
3681 SetLastError(0xdeadbeef);
3682 ret
= pCheckTokenMembership(process_token
, token_groups
->Groups
[i
].Sid
, &is_member
);
3683 ok(!ret
&& GetLastError() == ERROR_NO_IMPERSONATION_TOKEN
,
3684 "CheckTokenMembership with process token %s with error %d\n",
3685 ret
? "succeeded" : "failed", GetLastError());
3686 ok(!is_member
, "CheckTokenMembership should have cleared is_member\n");
3688 HeapFree(GetProcessHeap(), 0, token_groups
);
3690 CloseHandle(process_token
);
3693 static void test_EqualSid(void)
3697 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
3698 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
3700 SetLastError(0xdeadbeef);
3701 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
3702 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &sid1
);
3703 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
3705 win_skip("AllocateAndInitializeSid is not implemented\n");
3708 ok(ret
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3709 ok(GetLastError() == 0xdeadbeef,
3710 "AllocateAndInitializeSid shouldn't have set last error to %d\n",
3713 ret
= AllocateAndInitializeSid(&SIDAuthWorld
, 1, SECURITY_WORLD_RID
,
3714 0, 0, 0, 0, 0, 0, 0, &sid2
);
3715 ok(ret
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3717 SetLastError(0xdeadbeef);
3718 ret
= EqualSid(sid1
, sid2
);
3719 ok(!ret
, "World and domain admins sids shouldn't have been equal\n");
3720 ok(GetLastError() == ERROR_SUCCESS
||
3721 broken(GetLastError() == 0xdeadbeef), /* NT4 */
3722 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
3725 SetLastError(0xdeadbeef);
3726 sid2
= FreeSid(sid2
);
3727 ok(!sid2
, "FreeSid should have returned NULL instead of %p\n", sid2
);
3728 ok(GetLastError() == 0xdeadbeef,
3729 "FreeSid shouldn't have set last error to %d\n",
3732 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
3733 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &sid2
);
3734 ok(ret
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3736 SetLastError(0xdeadbeef);
3737 ret
= EqualSid(sid1
, sid2
);
3738 ok(ret
, "Same sids should have been equal\n");
3739 ok(GetLastError() == ERROR_SUCCESS
||
3740 broken(GetLastError() == 0xdeadbeef), /* NT4 */
3741 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
3744 ((SID
*)sid2
)->Revision
= 2;
3745 SetLastError(0xdeadbeef);
3746 ret
= EqualSid(sid1
, sid2
);
3747 ok(!ret
, "EqualSid with invalid sid should have returned FALSE\n");
3748 ok(GetLastError() == ERROR_SUCCESS
||
3749 broken(GetLastError() == 0xdeadbeef), /* NT4 */
3750 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
3752 ((SID
*)sid2
)->Revision
= SID_REVISION
;
3758 static void test_GetUserNameA(void)
3760 char buffer
[UNLEN
+ 1], filler
[UNLEN
+ 1];
3761 DWORD required_len
, buffer_len
;
3764 /* Test crashes on Windows. */
3767 SetLastError(0xdeadbeef);
3768 GetUserNameA(NULL
, NULL
);
3771 SetLastError(0xdeadbeef);
3773 ret
= GetUserNameA(NULL
, &required_len
);
3774 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
3775 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
3776 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
3778 SetLastError(0xdeadbeef);
3780 ret
= GetUserNameA(NULL
, &required_len
);
3781 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
3782 ok(required_len
!= 0 && required_len
!= 1, "Outputted buffer length was %u\n", required_len
);
3783 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
3785 /* Tests crashes on Windows. */
3788 SetLastError(0xdeadbeef);
3789 required_len
= UNLEN
+ 1;
3790 GetUserNameA(NULL
, &required_len
);
3792 SetLastError(0xdeadbeef);
3793 GetUserNameA(buffer
, NULL
);
3796 memset(filler
, 'x', sizeof(filler
));
3798 /* Note that GetUserNameA on XP and newer outputs the number of bytes
3799 * required for a Unicode string, which affects a test in the next block. */
3800 SetLastError(0xdeadbeef);
3801 memcpy(buffer
, filler
, sizeof(filler
));
3803 ret
= GetUserNameA(buffer
, &required_len
);
3804 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
3805 ok(!memcmp(buffer
, filler
, sizeof(filler
)), "Output buffer was altered\n");
3806 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
3807 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
3809 SetLastError(0xdeadbeef);
3810 memcpy(buffer
, filler
, sizeof(filler
));
3811 buffer_len
= required_len
;
3812 ret
= GetUserNameA(buffer
, &buffer_len
);
3813 ok(ret
== TRUE
, "GetUserNameA returned %d, last error %u\n", ret
, GetLastError());
3814 ok(memcmp(buffer
, filler
, sizeof(filler
)) != 0, "Output buffer was untouched\n");
3815 ok(buffer_len
== required_len
||
3816 broken(buffer_len
== required_len
/ sizeof(WCHAR
)), /* XP+ */
3817 "Outputted buffer length was %u\n", buffer_len
);
3819 /* Use the reported buffer size from the last GetUserNameA call and pass
3820 * a length that is one less than the required value. */
3821 SetLastError(0xdeadbeef);
3822 memcpy(buffer
, filler
, sizeof(filler
));
3824 ret
= GetUserNameA(buffer
, &buffer_len
);
3825 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
3826 ok(!memcmp(buffer
, filler
, sizeof(filler
)), "Output buffer was untouched\n");
3827 ok(buffer_len
== required_len
, "Outputted buffer length was %u\n", buffer_len
);
3828 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
3831 static void test_GetUserNameW(void)
3833 WCHAR buffer
[UNLEN
+ 1], filler
[UNLEN
+ 1];
3834 DWORD required_len
, buffer_len
;
3837 /* Test crashes on Windows. */
3840 SetLastError(0xdeadbeef);
3841 GetUserNameW(NULL
, NULL
);
3844 SetLastError(0xdeadbeef);
3846 ret
= GetUserNameW(NULL
, &required_len
);
3847 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
3848 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
3849 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
3851 SetLastError(0xdeadbeef);
3853 ret
= GetUserNameW(NULL
, &required_len
);
3854 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
3855 ok(required_len
!= 0 && required_len
!= 1, "Outputted buffer length was %u\n", required_len
);
3856 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
3858 /* Tests crash on Windows. */
3861 SetLastError(0xdeadbeef);
3862 required_len
= UNLEN
+ 1;
3863 GetUserNameW(NULL
, &required_len
);
3865 SetLastError(0xdeadbeef);
3866 GetUserNameW(buffer
, NULL
);
3869 memset(filler
, 'x', sizeof(filler
));
3871 SetLastError(0xdeadbeef);
3872 memcpy(buffer
, filler
, sizeof(filler
));
3874 ret
= GetUserNameW(buffer
, &required_len
);
3875 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
3876 ok(!memcmp(buffer
, filler
, sizeof(filler
)), "Output buffer was altered\n");
3877 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
3878 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
3880 SetLastError(0xdeadbeef);
3881 memcpy(buffer
, filler
, sizeof(filler
));
3882 buffer_len
= required_len
;
3883 ret
= GetUserNameW(buffer
, &buffer_len
);
3884 ok(ret
== TRUE
, "GetUserNameW returned %d, last error %u\n", ret
, GetLastError());
3885 ok(memcmp(buffer
, filler
, sizeof(filler
)) != 0, "Output buffer was untouched\n");
3886 ok(buffer_len
== required_len
, "Outputted buffer length was %u\n", buffer_len
);
3888 /* GetUserNameW on XP and newer writes a truncated portion of the username string to the buffer. */
3889 SetLastError(0xdeadbeef);
3890 memcpy(buffer
, filler
, sizeof(filler
));
3892 ret
= GetUserNameW(buffer
, &buffer_len
);
3893 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
3894 ok(!memcmp(buffer
, filler
, sizeof(filler
)) ||
3895 broken(memcmp(buffer
, filler
, sizeof(filler
)) != 0), /* XP+ */
3896 "Output buffer was altered\n");
3897 ok(buffer_len
== required_len
, "Outputted buffer length was %u\n", buffer_len
);
3898 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
3901 static void test_CreateRestrictedToken(void)
3903 HANDLE process_token
, token
, r_token
;
3904 PTOKEN_GROUPS token_groups
, groups2
;
3905 SID_AND_ATTRIBUTES sattr
;
3911 if (!pCreateRestrictedToken
)
3913 win_skip("CreateRestrictedToken is not available\n");
3917 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &process_token
);
3918 ok(ret
, "got error %d\n", GetLastError());
3920 ret
= DuplicateTokenEx(process_token
, TOKEN_DUPLICATE
|TOKEN_ADJUST_GROUPS
|TOKEN_QUERY
,
3921 NULL
, SecurityImpersonation
, TokenImpersonation
, &token
);
3922 ok(ret
, "got error %d\n", GetLastError());
3925 ret
= GetTokenInformation(token
, TokenGroups
, NULL
, 0, &size
);
3926 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
3927 "got %d with error %d\n", ret
, GetLastError());
3928 token_groups
= HeapAlloc(GetProcessHeap(), 0, size
);
3929 ret
= GetTokenInformation(token
, TokenGroups
, token_groups
, size
, &size
);
3930 ok(ret
, "got error %d\n", GetLastError());
3932 for (i
= 0; i
< token_groups
->GroupCount
; i
++)
3934 if (token_groups
->Groups
[i
].Attributes
& SE_GROUP_ENABLED
)
3938 if (i
== token_groups
->GroupCount
)
3940 HeapFree(GetProcessHeap(), 0, token_groups
);
3942 skip("User not a member of any group\n");
3947 ret
= pCheckTokenMembership(token
, token_groups
->Groups
[i
].Sid
, &is_member
);
3948 ok(ret
, "got error %d\n", GetLastError());
3949 ok(is_member
, "not a member\n");
3951 /* disable a SID in new token */
3952 sattr
.Sid
= token_groups
->Groups
[i
].Sid
;
3953 sattr
.Attributes
= 0;
3955 ret
= pCreateRestrictedToken(token
, 0, 1, &sattr
, 0, NULL
, 0, NULL
, &r_token
);
3956 todo_wine
ok(ret
, "got error %d\n", GetLastError());
3960 /* check if a SID is enabled */
3962 ret
= pCheckTokenMembership(r_token
, token_groups
->Groups
[i
].Sid
, &is_member
);
3963 ok(ret
, "got error %d\n", GetLastError());
3964 ok(!is_member
, "not a member\n");
3966 ret
= GetTokenInformation(r_token
, TokenGroups
, NULL
, 0, &size
);
3967 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "got %d with error %d\n",
3968 ret
, GetLastError());
3969 groups2
= HeapAlloc(GetProcessHeap(), 0, size
);
3970 ret
= GetTokenInformation(r_token
, TokenGroups
, groups2
, size
, &size
);
3971 ok(ret
, "got error %d\n", GetLastError());
3973 for (j
= 0; j
< groups2
->GroupCount
; j
++)
3975 if (EqualSid(groups2
->Groups
[j
].Sid
, token_groups
->Groups
[i
].Sid
))
3979 ok(groups2
->Groups
[j
].Attributes
& SE_GROUP_USE_FOR_DENY_ONLY
,
3980 "got wrong attributes\n");
3981 ok((groups2
->Groups
[j
].Attributes
& SE_GROUP_ENABLED
) == 0,
3982 "got wrong attributes\n");
3984 HeapFree(GetProcessHeap(), 0, groups2
);
3987 HeapFree(GetProcessHeap(), 0, token_groups
);
3988 CloseHandle(r_token
);
3990 CloseHandle(process_token
);
3993 static void validate_default_security_descriptor(SECURITY_DESCRIPTOR
*sd
)
3995 BOOL ret
, present
, defaulted
;
3999 ret
= IsValidSecurityDescriptor(sd
);
4000 ok(ret
, "security descriptor is not valid\n");
4004 acl
= (void *)0xdeadbeef;
4005 SetLastError(0xdeadbeef);
4006 ret
= GetSecurityDescriptorDacl(sd
, &present
, &acl
, &defaulted
);
4007 ok(ret
, "GetSecurityDescriptorDacl error %d\n", GetLastError());
4009 ok(present
== 1, "acl is not present\n");
4011 ok(acl
!= (void *)0xdeadbeef && acl
!= NULL
, "acl pointer is not set\n");
4012 ok(defaulted
== 0, "defaulted is set to TRUE\n");
4015 sid
= (void *)0xdeadbeef;
4016 SetLastError(0xdeadbeef);
4017 ret
= GetSecurityDescriptorOwner(sd
, &sid
, &defaulted
);
4018 ok(ret
, "GetSecurityDescriptorOwner error %d\n", GetLastError());
4020 ok(sid
!= (void *)0xdeadbeef && sid
!= NULL
, "sid pointer is not set\n");
4021 ok(defaulted
== 0, "defaulted is set to TRUE\n");
4024 sid
= (void *)0xdeadbeef;
4025 SetLastError(0xdeadbeef);
4026 ret
= GetSecurityDescriptorGroup(sd
, &sid
, &defaulted
);
4027 ok(ret
, "GetSecurityDescriptorGroup error %d\n", GetLastError());
4029 ok(sid
!= (void *)0xdeadbeef && sid
!= NULL
, "sid pointer is not set\n");
4030 ok(defaulted
== 0, "defaulted is set to TRUE\n");
4033 static void test_default_handle_security(HANDLE token
, HANDLE handle
, GENERIC_MAPPING
*mapping
)
4035 DWORD ret
, length
, needed
, granted
, priv_set_len
;
4037 PRIVILEGE_SET priv_set
;
4038 SECURITY_DESCRIPTOR
*sd
;
4040 needed
= 0xdeadbeef;
4041 SetLastError(0xdeadbeef);
4042 ret
= GetKernelObjectSecurity(handle
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
4044 ok(!ret
, "GetKernelObjectSecurity should fail\n");
4045 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
4046 ok(needed
!= 0xdeadbeef, "GetKernelObjectSecurity should return required buffer length\n");
4049 sd
= HeapAlloc(GetProcessHeap(), 0, length
);
4051 needed
= 0xdeadbeef;
4052 SetLastError(0xdeadbeef);
4053 ret
= GetKernelObjectSecurity(handle
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
4054 sd
, length
, &needed
);
4055 ok(ret
, "GetKernelObjectSecurity error %d\n", GetLastError());
4056 ok(needed
== length
|| needed
== 0 /* file, pipe */, "GetKernelObjectSecurity should return %u instead of %u\n", length
, needed
);
4058 validate_default_security_descriptor(sd
);
4060 priv_set_len
= sizeof(priv_set
);
4061 granted
= 0xdeadbeef;
4062 status
= 0xdeadbeef;
4063 SetLastError(0xdeadbeef);
4064 ret
= AccessCheck(sd
, token
, MAXIMUM_ALLOWED
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4066 ok(ret
, "AccessCheck error %d\n", GetLastError());
4067 ok(status
== 1, "expected 1, got %d\n", status
);
4068 ok(granted
== mapping
->GenericAll
, "expected all access %#x, got %#x\n", mapping
->GenericAll
, granted
);
4070 priv_set_len
= sizeof(priv_set
);
4071 granted
= 0xdeadbeef;
4072 status
= 0xdeadbeef;
4073 SetLastError(0xdeadbeef);
4074 ret
= AccessCheck(sd
, token
, 0, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4076 ok(ret
, "AccessCheck error %d\n", GetLastError());
4077 ok(status
== 0 || broken(status
== 1) /* NT4 */, "expected 0, got %d\n", status
);
4078 ok(granted
== 0 || broken(granted
== mapping
->GenericRead
) /* NT4 */, "expected 0, got %#x\n", granted
);
4080 priv_set_len
= sizeof(priv_set
);
4081 granted
= 0xdeadbeef;
4082 status
= 0xdeadbeef;
4083 SetLastError(0xdeadbeef);
4084 ret
= AccessCheck(sd
, token
, ACCESS_SYSTEM_SECURITY
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4086 ok(ret
, "AccessCheck error %d\n", GetLastError());
4087 ok(status
== 0, "expected 0, got %d\n", status
);
4088 ok(granted
== 0, "expected 0, got %#x\n", granted
);
4090 priv_set_len
= sizeof(priv_set
);
4091 granted
= 0xdeadbeef;
4092 status
= 0xdeadbeef;
4093 SetLastError(0xdeadbeef);
4094 ret
= AccessCheck(sd
, token
, mapping
->GenericRead
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4096 ok(ret
, "AccessCheck error %d\n", GetLastError());
4097 ok(status
== 1, "expected 1, got %d\n", status
);
4098 ok(granted
== mapping
->GenericRead
, "expected read access %#x, got %#x\n", mapping
->GenericRead
, granted
);
4100 priv_set_len
= sizeof(priv_set
);
4101 granted
= 0xdeadbeef;
4102 status
= 0xdeadbeef;
4103 SetLastError(0xdeadbeef);
4104 ret
= AccessCheck(sd
, token
, mapping
->GenericWrite
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4106 ok(ret
, "AccessCheck error %d\n", GetLastError());
4107 ok(status
== 1, "expected 1, got %d\n", status
);
4108 ok(granted
== mapping
->GenericWrite
, "expected write access %#x, got %#x\n", mapping
->GenericWrite
, granted
);
4110 priv_set_len
= sizeof(priv_set
);
4111 granted
= 0xdeadbeef;
4112 status
= 0xdeadbeef;
4113 SetLastError(0xdeadbeef);
4114 ret
= AccessCheck(sd
, token
, mapping
->GenericExecute
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4116 ok(ret
, "AccessCheck error %d\n", GetLastError());
4117 ok(status
== 1, "expected 1, got %d\n", status
);
4118 ok(granted
== mapping
->GenericExecute
, "expected execute access %#x, got %#x\n", mapping
->GenericExecute
, granted
);
4120 HeapFree(GetProcessHeap(), 0, sd
);
4123 static void test_mutex_security(HANDLE token
)
4126 GENERIC_MAPPING mapping
= { STANDARD_RIGHTS_READ
| MUTANT_QUERY_STATE
| SYNCHRONIZE
,
4127 STANDARD_RIGHTS_WRITE
| MUTEX_MODIFY_STATE
| SYNCHRONIZE
,
4128 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
4129 STANDARD_RIGHTS_ALL
| MUTEX_ALL_ACCESS
};
4131 SetLastError(0xdeadbeef);
4132 mutex
= OpenMutex(0, FALSE
, "WineTestMutex");
4133 ok(!mutex
, "mutex should not exist\n");
4134 ok(GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %u\n", GetLastError());
4136 SetLastError(0xdeadbeef);
4137 mutex
= CreateMutex(NULL
, FALSE
, "WineTestMutex");
4138 ok(mutex
!= 0, "CreateMutex error %d\n", GetLastError());
4140 test_default_handle_security(token
, mutex
, &mapping
);
4142 CloseHandle (mutex
);
4145 static void test_event_security(HANDLE token
)
4148 GENERIC_MAPPING mapping
= { STANDARD_RIGHTS_READ
| EVENT_QUERY_STATE
| SYNCHRONIZE
,
4149 STANDARD_RIGHTS_WRITE
| EVENT_MODIFY_STATE
| SYNCHRONIZE
,
4150 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
4151 STANDARD_RIGHTS_ALL
| EVENT_ALL_ACCESS
};
4153 SetLastError(0xdeadbeef);
4154 event
= OpenEvent(0, FALSE
, "WineTestEvent");
4155 ok(!event
, "event should not exist\n");
4156 ok(GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %u\n", GetLastError());
4158 SetLastError(0xdeadbeef);
4159 event
= CreateEvent(NULL
, FALSE
, FALSE
, "WineTestEvent");
4160 ok(event
!= 0, "CreateEvent error %d\n", GetLastError());
4162 test_default_handle_security(token
, event
, &mapping
);
4167 #define WINE_TEST_PIPE "\\\\.\\pipe\\WineTestPipe"
4168 static void test_named_pipe_security(HANDLE token
)
4171 GENERIC_MAPPING mapping
= { FILE_GENERIC_READ
,
4173 FILE_GENERIC_EXECUTE
,
4174 STANDARD_RIGHTS_ALL
| FILE_ALL_ACCESS
};
4176 SetLastError(0xdeadbeef);
4177 pipe
= CreateNamedPipe(WINE_TEST_PIPE
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_FIRST_PIPE_INSTANCE
,
4178 PIPE_TYPE_BYTE
| PIPE_NOWAIT
, PIPE_UNLIMITED_INSTANCES
,
4179 0, 0, NMPWAIT_USE_DEFAULT_WAIT
, NULL
);
4180 ok(pipe
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe error %d\n", GetLastError());
4182 test_default_handle_security(token
, pipe
, &mapping
);
4184 SetLastError(0xdeadbeef);
4185 file
= CreateFile(WINE_TEST_PIPE
, FILE_ALL_ACCESS
, 0, NULL
, OPEN_EXISTING
, 0, 0);
4186 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4191 SetLastError(0xdeadbeef);
4192 file
= CreateFile("\\\\.\\pipe\\", FILE_ALL_ACCESS
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, 0);
4193 ok(file
!= INVALID_HANDLE_VALUE
|| broken(file
== INVALID_HANDLE_VALUE
) /* before Vista */, "CreateFile error %d\n", GetLastError());
4197 static BOOL
validate_impersonation_token(HANDLE token
, DWORD
*token_type
)
4201 SECURITY_IMPERSONATION_LEVEL sil
;
4205 SetLastError(0xdeadbeef);
4206 ret
= GetTokenInformation(token
, TokenType
, &type
, sizeof(type
), &needed
);
4207 ok(ret
, "GetTokenInformation error %d\n", GetLastError());
4208 ok(needed
== sizeof(type
), "GetTokenInformation should return required buffer length\n");
4209 ok(type
== TokenPrimary
|| type
== TokenImpersonation
, "expected TokenPrimary or TokenImpersonation, got %d\n", type
);
4212 if (type
!= TokenImpersonation
) return FALSE
;
4215 SetLastError(0xdeadbeef);
4216 ret
= GetTokenInformation(token
, TokenImpersonationLevel
, &sil
, sizeof(sil
), &needed
);
4217 ok(ret
, "GetTokenInformation error %d\n", GetLastError());
4218 ok(needed
== sizeof(sil
), "GetTokenInformation should return required buffer length\n");
4219 ok(sil
== SecurityImpersonation
, "expected SecurityImpersonation, got %d\n", sil
);
4221 needed
= 0xdeadbeef;
4222 SetLastError(0xdeadbeef);
4223 ret
= GetTokenInformation(token
, TokenDefaultDacl
, NULL
, 0, &needed
);
4224 ok(!ret
, "GetTokenInformation should fail\n");
4225 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
4226 ok(needed
!= 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
4227 ok(needed
> sizeof(TOKEN_DEFAULT_DACL
), "GetTokenInformation returned empty default DACL\n");
4229 needed
= 0xdeadbeef;
4230 SetLastError(0xdeadbeef);
4231 ret
= GetTokenInformation(token
, TokenOwner
, NULL
, 0, &needed
);
4232 ok(!ret
, "GetTokenInformation should fail\n");
4233 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
4234 ok(needed
!= 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
4235 ok(needed
> sizeof(TOKEN_OWNER
), "GetTokenInformation returned empty token owner\n");
4237 needed
= 0xdeadbeef;
4238 SetLastError(0xdeadbeef);
4239 ret
= GetTokenInformation(token
, TokenPrimaryGroup
, NULL
, 0, &needed
);
4240 ok(!ret
, "GetTokenInformation should fail\n");
4241 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
4242 ok(needed
!= 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
4243 ok(needed
> sizeof(TOKEN_PRIMARY_GROUP
), "GetTokenInformation returned empty token primary group\n");
4248 static void test_kernel_objects_security(void)
4250 HANDLE token
, process_token
;
4251 DWORD ret
, token_type
;
4253 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
| TOKEN_QUERY
, &process_token
);
4254 ok(ret
, "OpenProcessToken error %d\n", GetLastError());
4256 ret
= validate_impersonation_token(process_token
, &token_type
);
4257 ok(token_type
== TokenPrimary
, "expected TokenPrimary, got %d\n", token_type
);
4258 ok(!ret
, "access token should not be an impersonation token\n");
4260 ret
= DuplicateToken(process_token
, SecurityImpersonation
, &token
);
4261 ok(ret
, "DuplicateToken error %d\n", GetLastError());
4263 ret
= validate_impersonation_token(token
, &token_type
);
4264 ok(ret
, "access token should be a valid impersonation token\n");
4265 ok(token_type
== TokenImpersonation
, "expected TokenImpersonation, got %d\n", token_type
);
4267 test_mutex_security(token
);
4268 test_event_security(token
);
4269 test_named_pipe_security(token
);
4270 /* FIXME: test other kernel object types */
4272 CloseHandle(process_token
);
4276 START_TEST(security
)
4283 test_process_security_child();
4286 test_kernel_objects_security();
4290 test_CreateWellKnownSid();
4291 test_FileSecurity();
4294 test_LookupAccountSid();
4295 test_LookupAccountName();
4296 test_security_descriptor();
4297 test_process_security();
4298 test_impersonation_level();
4299 test_SetEntriesInAclW();
4300 test_SetEntriesInAclA();
4301 test_GetNamedSecurityInfoA();
4302 test_ConvertStringSecurityDescriptor();
4303 test_ConvertSecurityDescriptorToString();
4304 test_PrivateObjectSecurity();
4306 test_GetSecurityInfo();
4307 test_GetSidSubAuthority();
4308 test_CheckTokenMembership();
4310 test_GetUserNameA();
4311 test_GetUserNameW();
4312 test_CreateRestrictedToken();