2 * Unit tests for security functions
4 * Copyright (c) 2004 Mike McCormack
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #define WIN32_NO_STATUS
35 #include "wine/test.h"
37 /* copied from Wine winternl.h - not included in the Windows SDK */
38 typedef enum _OBJECT_INFORMATION_CLASS
{
39 ObjectBasicInformation
,
40 ObjectNameInformation
,
41 ObjectTypeInformation
,
44 } OBJECT_INFORMATION_CLASS
, *POBJECT_INFORMATION_CLASS
;
46 typedef struct _OBJECT_BASIC_INFORMATION
{
48 ACCESS_MASK GrantedAccess
;
52 ULONG NonPagedPoolUsage
;
54 ULONG NameInformationLength
;
55 ULONG TypeInformationLength
;
56 ULONG SecurityDescriptorLength
;
57 LARGE_INTEGER CreateTime
;
58 } OBJECT_BASIC_INFORMATION
, *POBJECT_BASIC_INFORMATION
;
60 #define expect_eq(expr, value, type, format) { type ret = expr; ok((value) == ret, #expr " expected " format " got " format "\n", (value), (ret)); }
62 static BOOL (WINAPI
*pAddAccessAllowedAceEx
)(PACL
, DWORD
, DWORD
, DWORD
, PSID
);
63 static BOOL (WINAPI
*pAddAccessDeniedAceEx
)(PACL
, DWORD
, DWORD
, DWORD
, PSID
);
64 static BOOL (WINAPI
*pAddAuditAccessAceEx
)(PACL
, DWORD
, DWORD
, DWORD
, PSID
, BOOL
, BOOL
);
65 typedef VOID (WINAPI
*fnBuildTrusteeWithSidA
)( PTRUSTEEA pTrustee
, PSID pSid
);
66 typedef VOID (WINAPI
*fnBuildTrusteeWithNameA
)( PTRUSTEEA pTrustee
, LPSTR pName
);
67 typedef VOID (WINAPI
*fnBuildTrusteeWithObjectsAndNameA
)( PTRUSTEEA pTrustee
,
68 POBJECTS_AND_NAME_A pObjName
,
69 SE_OBJECT_TYPE ObjectType
,
71 LPSTR InheritedObjectTypeName
,
73 typedef VOID (WINAPI
*fnBuildTrusteeWithObjectsAndSidA
)( PTRUSTEEA pTrustee
,
74 POBJECTS_AND_SID pObjSid
,
76 GUID
* pInheritedObjectGuid
,
78 typedef LPSTR (WINAPI
*fnGetTrusteeNameA
)( PTRUSTEEA pTrustee
);
79 typedef BOOL (WINAPI
*fnMakeSelfRelativeSD
)( PSECURITY_DESCRIPTOR
, PSECURITY_DESCRIPTOR
, LPDWORD
);
80 typedef BOOL (WINAPI
*fnConvertSidToStringSidA
)( PSID pSid
, LPSTR
*str
);
81 typedef BOOL (WINAPI
*fnConvertStringSidToSidA
)( LPCSTR str
, PSID pSid
);
82 static BOOL (WINAPI
*pConvertStringSecurityDescriptorToSecurityDescriptorA
)(LPCSTR
, DWORD
,
83 PSECURITY_DESCRIPTOR
*, PULONG
);
84 static BOOL (WINAPI
*pConvertStringSecurityDescriptorToSecurityDescriptorW
)(LPCWSTR
, DWORD
,
85 PSECURITY_DESCRIPTOR
*, PULONG
);
86 static BOOL (WINAPI
*pConvertSecurityDescriptorToStringSecurityDescriptorA
)(PSECURITY_DESCRIPTOR
, DWORD
,
87 SECURITY_INFORMATION
, LPSTR
*, PULONG
);
88 typedef BOOL (WINAPI
*fnGetFileSecurityA
)(LPCSTR
, SECURITY_INFORMATION
,
89 PSECURITY_DESCRIPTOR
, DWORD
, LPDWORD
);
90 typedef BOOL (WINAPI
*fnSetFileSecurityA
)(LPCSTR
, SECURITY_INFORMATION
,
91 PSECURITY_DESCRIPTOR
);
92 static DWORD (WINAPI
*pGetNamedSecurityInfoA
)(LPSTR
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
93 PSID
*, PSID
*, PACL
*, PACL
*,
94 PSECURITY_DESCRIPTOR
*);
95 static PDWORD (WINAPI
*pGetSidSubAuthority
)(PSID
, DWORD
);
96 static PUCHAR (WINAPI
*pGetSidSubAuthorityCount
)(PSID
);
97 static BOOL (WINAPI
*pIsValidSid
)(PSID
);
98 typedef DWORD (WINAPI
*fnRtlAdjustPrivilege
)(ULONG
,BOOLEAN
,BOOLEAN
,PBOOLEAN
);
99 typedef BOOL (WINAPI
*fnCreateWellKnownSid
)(WELL_KNOWN_SID_TYPE
,PSID
,PSID
,DWORD
*);
100 typedef BOOL (WINAPI
*fnDuplicateTokenEx
)(HANDLE
,DWORD
,LPSECURITY_ATTRIBUTES
,
101 SECURITY_IMPERSONATION_LEVEL
,TOKEN_TYPE
,PHANDLE
);
103 typedef NTSTATUS (WINAPI
*fnLsaQueryInformationPolicy
)(LSA_HANDLE
,POLICY_INFORMATION_CLASS
,PVOID
*);
104 typedef NTSTATUS (WINAPI
*fnLsaClose
)(LSA_HANDLE
);
105 typedef NTSTATUS (WINAPI
*fnLsaFreeMemory
)(PVOID
);
106 typedef NTSTATUS (WINAPI
*fnLsaOpenPolicy
)(PLSA_UNICODE_STRING
,PLSA_OBJECT_ATTRIBUTES
,ACCESS_MASK
,PLSA_HANDLE
);
107 static NTSTATUS (WINAPI
*pNtQueryObject
)(HANDLE
,OBJECT_INFORMATION_CLASS
,PVOID
,ULONG
,PULONG
);
108 static DWORD (WINAPI
*pSetEntriesInAclW
)(ULONG
, PEXPLICIT_ACCESSW
, PACL
, PACL
*);
109 static BOOL (WINAPI
*pSetSecurityDescriptorControl
)(PSECURITY_DESCRIPTOR
, SECURITY_DESCRIPTOR_CONTROL
,
110 SECURITY_DESCRIPTOR_CONTROL
);
111 static DWORD (WINAPI
*pGetSecurityInfo
)(HANDLE
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
112 PSID
*, PSID
*, PACL
*, PACL
*, PSECURITY_DESCRIPTOR
*);
113 static NTSTATUS (WINAPI
*pNtAccessCheck
)(PSECURITY_DESCRIPTOR
, HANDLE
, ACCESS_MASK
, PGENERIC_MAPPING
,
114 PPRIVILEGE_SET
, PULONG
, PULONG
, NTSTATUS
*);
118 static char** myARGV
;
120 fnBuildTrusteeWithSidA pBuildTrusteeWithSidA
;
121 fnBuildTrusteeWithNameA pBuildTrusteeWithNameA
;
122 fnBuildTrusteeWithObjectsAndNameA pBuildTrusteeWithObjectsAndNameA
;
123 fnBuildTrusteeWithObjectsAndSidA pBuildTrusteeWithObjectsAndSidA
;
124 fnGetTrusteeNameA pGetTrusteeNameA
;
125 fnMakeSelfRelativeSD pMakeSelfRelativeSD
;
126 fnConvertSidToStringSidA pConvertSidToStringSidA
;
127 fnConvertStringSidToSidA pConvertStringSidToSidA
;
128 fnGetFileSecurityA pGetFileSecurityA
;
129 fnSetFileSecurityA pSetFileSecurityA
;
130 fnRtlAdjustPrivilege pRtlAdjustPrivilege
;
131 fnCreateWellKnownSid pCreateWellKnownSid
;
132 fnDuplicateTokenEx pDuplicateTokenEx
;
133 fnLsaQueryInformationPolicy pLsaQueryInformationPolicy
;
134 fnLsaClose pLsaClose
;
135 fnLsaFreeMemory pLsaFreeMemory
;
136 fnLsaOpenPolicy pLsaOpenPolicy
;
140 SID_IDENTIFIER_AUTHORITY auth
;
144 static void init(void)
148 hntdll
= GetModuleHandleA("ntdll.dll");
149 pNtQueryObject
= (void *)GetProcAddress( hntdll
, "NtQueryObject" );
150 pNtAccessCheck
= (void *)GetProcAddress( hntdll
, "NtAccessCheck" );
152 hmod
= GetModuleHandle("advapi32.dll");
153 pAddAccessAllowedAceEx
= (void *)GetProcAddress(hmod
, "AddAccessAllowedAceEx");
154 pAddAccessDeniedAceEx
= (void *)GetProcAddress(hmod
, "AddAccessDeniedAceEx");
155 pAddAuditAccessAceEx
= (void *)GetProcAddress(hmod
, "AddAuditAccessAceEx");
156 pConvertStringSecurityDescriptorToSecurityDescriptorA
=
157 (void *)GetProcAddress(hmod
, "ConvertStringSecurityDescriptorToSecurityDescriptorA" );
158 pConvertStringSecurityDescriptorToSecurityDescriptorW
=
159 (void *)GetProcAddress(hmod
, "ConvertStringSecurityDescriptorToSecurityDescriptorW" );
160 pConvertSecurityDescriptorToStringSecurityDescriptorA
=
161 (void *)GetProcAddress(hmod
, "ConvertSecurityDescriptorToStringSecurityDescriptorA" );
162 pGetFileSecurityA
= (fnGetFileSecurityA
)GetProcAddress(hmod
, "GetFileSecurityA" );
163 pSetFileSecurityA
= (fnSetFileSecurityA
)GetProcAddress(hmod
, "SetFileSecurityA" );
164 pCreateWellKnownSid
= (fnCreateWellKnownSid
)GetProcAddress( hmod
, "CreateWellKnownSid" );
165 pGetNamedSecurityInfoA
= (void *)GetProcAddress(hmod
, "GetNamedSecurityInfoA");
166 pGetSidSubAuthority
= (void *)GetProcAddress(hmod
, "GetSidSubAuthority");
167 pGetSidSubAuthorityCount
= (void *)GetProcAddress(hmod
, "GetSidSubAuthorityCount");
168 pIsValidSid
= (void *)GetProcAddress(hmod
, "IsValidSid");
169 pMakeSelfRelativeSD
= (void *)GetProcAddress(hmod
, "MakeSelfRelativeSD");
170 pSetEntriesInAclW
= (void *)GetProcAddress(hmod
, "SetEntriesInAclW");
171 pSetSecurityDescriptorControl
= (void *)GetProcAddress(hmod
, "SetSecurityDescriptorControl");
172 pGetSecurityInfo
= (void *)GetProcAddress(hmod
, "GetSecurityInfo");
174 myARGC
= winetest_get_mainargs( &myARGV
);
177 static void test_str_sid(const char *str_sid
)
182 if (pConvertStringSidToSidA(str_sid
, &psid
))
184 if (pConvertSidToStringSidA(psid
, &temp
))
186 trace(" %s: %s\n", str_sid
, temp
);
193 if (GetLastError() != ERROR_INVALID_SID
)
194 trace(" %s: couldn't be converted, returned %d\n", str_sid
, GetLastError());
196 trace(" %s: couldn't be converted\n", str_sid
);
200 static void test_sid(void)
202 struct sidRef refs
[] = {
203 { { {0x00,0x00,0x33,0x44,0x55,0x66} }, "S-1-860116326-1" },
204 { { {0x00,0x00,0x01,0x02,0x03,0x04} }, "S-1-16909060-1" },
205 { { {0x00,0x00,0x00,0x01,0x02,0x03} }, "S-1-66051-1" },
206 { { {0x00,0x00,0x00,0x00,0x01,0x02} }, "S-1-258-1" },
207 { { {0x00,0x00,0x00,0x00,0x00,0x02} }, "S-1-2-1" },
208 { { {0x00,0x00,0x00,0x00,0x00,0x0c} }, "S-1-12-1" },
210 const char noSubAuthStr
[] = "S-1-5";
217 pConvertSidToStringSidA
= (fnConvertSidToStringSidA
)
218 GetProcAddress( hmod
, "ConvertSidToStringSidA" );
219 if( !pConvertSidToStringSidA
)
221 pConvertStringSidToSidA
= (fnConvertStringSidToSidA
)
222 GetProcAddress( hmod
, "ConvertStringSidToSidA" );
223 if( !pConvertStringSidToSidA
)
226 r
= pConvertStringSidToSidA( NULL
, NULL
);
227 ok( !r
, "expected failure with NULL parameters\n" );
228 if( GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
230 ok( GetLastError() == ERROR_INVALID_PARAMETER
,
231 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
234 r
= pConvertStringSidToSidA( refs
[0].refStr
, NULL
);
235 ok( !r
&& GetLastError() == ERROR_INVALID_PARAMETER
,
236 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
239 r
= pConvertStringSidToSidA( NULL
, &str
);
240 ok( !r
&& GetLastError() == ERROR_INVALID_PARAMETER
,
241 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
244 r
= pConvertStringSidToSidA( noSubAuthStr
, &psid
);
246 "expected failure with no sub authorities\n" );
247 ok( GetLastError() == ERROR_INVALID_SID
,
248 "expected GetLastError() is ERROR_INVALID_SID, got %d\n",
251 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid
), "ConvertStringSidToSidA failed\n");
253 ok(pisid
->SubAuthorityCount
== 4, "Invalid sub authority count - expected 4, got %d\n", pisid
->SubAuthorityCount
);
254 ok(pisid
->SubAuthority
[0] == 21, "Invalid subauthority 0 - expceted 21, got %d\n", pisid
->SubAuthority
[0]);
255 ok(pisid
->SubAuthority
[3] == 4576, "Invalid subauthority 0 - expceted 4576, got %d\n", pisid
->SubAuthority
[3]);
258 for( i
= 0; i
< sizeof(refs
) / sizeof(refs
[0]); i
++ )
262 r
= AllocateAndInitializeSid( &refs
[i
].auth
, 1,1,0,0,0,0,0,0,0,
264 ok( r
, "failed to allocate sid\n" );
265 r
= pConvertSidToStringSidA( psid
, &str
);
266 ok( r
, "failed to convert sid\n" );
269 ok( !strcmp( str
, refs
[i
].refStr
),
270 "incorrect sid, expected %s, got %s\n", refs
[i
].refStr
, str
);
276 r
= pConvertStringSidToSidA( refs
[i
].refStr
, &psid
);
277 ok( r
, "failed to parse sid string\n" );
280 !memcmp( pisid
->IdentifierAuthority
.Value
, refs
[i
].auth
.Value
,
281 sizeof(refs
[i
].auth
) ),
282 "string sid %s didn't parse to expected value\n"
283 "(got 0x%04x%08x, expected 0x%04x%08x)\n",
285 MAKEWORD( pisid
->IdentifierAuthority
.Value
[1],
286 pisid
->IdentifierAuthority
.Value
[0] ),
287 MAKELONG( MAKEWORD( pisid
->IdentifierAuthority
.Value
[5],
288 pisid
->IdentifierAuthority
.Value
[4] ),
289 MAKEWORD( pisid
->IdentifierAuthority
.Value
[3],
290 pisid
->IdentifierAuthority
.Value
[2] ) ),
291 MAKEWORD( refs
[i
].auth
.Value
[1], refs
[i
].auth
.Value
[0] ),
292 MAKELONG( MAKEWORD( refs
[i
].auth
.Value
[5], refs
[i
].auth
.Value
[4] ),
293 MAKEWORD( refs
[i
].auth
.Value
[3], refs
[i
].auth
.Value
[2] ) ) );
298 trace("String SIDs:\n");
339 static void test_trustee(void)
341 GUID ObjectType
= {0x12345678, 0x1234, 0x5678, {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}};
342 GUID InheritedObjectType
= {0x23456789, 0x2345, 0x6786, {0x2, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}};
344 OBJECTS_AND_NAME_ oan
;
348 char szObjectTypeName
[] = "ObjectTypeName";
349 char szInheritedObjectTypeName
[] = "InheritedObjectTypeName";
350 char szTrusteeName
[] = "szTrusteeName";
351 SID_IDENTIFIER_AUTHORITY auth
= { {0x11,0x22,0,0,0, 0} };
353 memset( &ZeroGuid
, 0x00, sizeof (ZeroGuid
) );
355 pBuildTrusteeWithSidA
= (fnBuildTrusteeWithSidA
)
356 GetProcAddress( hmod
, "BuildTrusteeWithSidA" );
357 pBuildTrusteeWithNameA
= (fnBuildTrusteeWithNameA
)
358 GetProcAddress( hmod
, "BuildTrusteeWithNameA" );
359 pBuildTrusteeWithObjectsAndNameA
= (fnBuildTrusteeWithObjectsAndNameA
)
360 GetProcAddress (hmod
, "BuildTrusteeWithObjectsAndNameA" );
361 pBuildTrusteeWithObjectsAndSidA
= (fnBuildTrusteeWithObjectsAndSidA
)
362 GetProcAddress (hmod
, "BuildTrusteeWithObjectsAndSidA" );
363 pGetTrusteeNameA
= (fnGetTrusteeNameA
)
364 GetProcAddress (hmod
, "GetTrusteeNameA" );
365 if( !pBuildTrusteeWithSidA
|| !pBuildTrusteeWithNameA
||
366 !pBuildTrusteeWithObjectsAndNameA
|| !pBuildTrusteeWithObjectsAndSidA
||
370 if ( ! AllocateAndInitializeSid( &auth
, 1, 42, 0,0,0,0,0,0,0,&psid
) )
372 trace( "failed to init SID\n" );
376 /* test BuildTrusteeWithSidA */
377 memset( &trustee
, 0xff, sizeof trustee
);
378 pBuildTrusteeWithSidA( &trustee
, psid
);
380 ok( trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
381 ok( trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
,
382 "MultipleTrusteeOperation wrong\n");
383 ok( trustee
.TrusteeForm
== TRUSTEE_IS_SID
, "TrusteeForm wrong\n");
384 ok( trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
385 ok( trustee
.ptstrName
== psid
, "ptstrName wrong\n" );
387 /* test BuildTrusteeWithObjectsAndSidA (test 1) */
388 memset( &trustee
, 0xff, sizeof trustee
);
389 memset( &oas
, 0xff, sizeof(oas
) );
390 pBuildTrusteeWithObjectsAndSidA(&trustee
, &oas
, &ObjectType
,
391 &InheritedObjectType
, psid
);
393 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
394 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
395 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_SID
, "TrusteeForm wrong\n");
396 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
397 ok(trustee
.ptstrName
== (LPSTR
)&oas
, "ptstrName wrong\n");
399 ok(oas
.ObjectsPresent
== (ACE_OBJECT_TYPE_PRESENT
| ACE_INHERITED_OBJECT_TYPE_PRESENT
), "ObjectsPresent wrong\n");
400 ok(!memcmp(&oas
.ObjectTypeGuid
, &ObjectType
, sizeof(GUID
)), "ObjectTypeGuid wrong\n");
401 ok(!memcmp(&oas
.InheritedObjectTypeGuid
, &InheritedObjectType
, sizeof(GUID
)), "InheritedObjectTypeGuid wrong\n");
402 ok(oas
.pSid
== psid
, "pSid wrong\n");
404 /* test GetTrusteeNameA */
405 ok(pGetTrusteeNameA(&trustee
) == (LPSTR
)&oas
, "GetTrusteeName returned wrong value\n");
407 /* test BuildTrusteeWithObjectsAndSidA (test 2) */
408 memset( &trustee
, 0xff, sizeof trustee
);
409 memset( &oas
, 0xff, sizeof(oas
) );
410 pBuildTrusteeWithObjectsAndSidA(&trustee
, &oas
, NULL
,
411 &InheritedObjectType
, psid
);
413 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
414 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
415 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_SID
, "TrusteeForm wrong\n");
416 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
417 ok(trustee
.ptstrName
== (LPSTR
)&oas
, "ptstrName wrong\n");
419 ok(oas
.ObjectsPresent
== ACE_INHERITED_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
420 ok(!memcmp(&oas
.ObjectTypeGuid
, &ZeroGuid
, sizeof(GUID
)), "ObjectTypeGuid wrong\n");
421 ok(!memcmp(&oas
.InheritedObjectTypeGuid
, &InheritedObjectType
, sizeof(GUID
)), "InheritedObjectTypeGuid wrong\n");
422 ok(oas
.pSid
== psid
, "pSid wrong\n");
426 /* test BuildTrusteeWithNameA */
427 memset( &trustee
, 0xff, sizeof trustee
);
428 pBuildTrusteeWithNameA( &trustee
, szTrusteeName
);
430 ok( trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
431 ok( trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
,
432 "MultipleTrusteeOperation wrong\n");
433 ok( trustee
.TrusteeForm
== TRUSTEE_IS_NAME
, "TrusteeForm wrong\n");
434 ok( trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
435 ok( trustee
.ptstrName
== szTrusteeName
, "ptstrName wrong\n" );
437 /* test BuildTrusteeWithObjectsAndNameA (test 1) */
438 memset( &trustee
, 0xff, sizeof trustee
);
439 memset( &oan
, 0xff, sizeof(oan
) );
440 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, szObjectTypeName
,
441 szInheritedObjectTypeName
, szTrusteeName
);
443 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
444 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
445 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
446 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
447 ok(trustee
.ptstrName
== (LPTSTR
)&oan
, "ptstrName wrong\n");
449 ok(oan
.ObjectsPresent
== (ACE_OBJECT_TYPE_PRESENT
| ACE_INHERITED_OBJECT_TYPE_PRESENT
), "ObjectsPresent wrong\n");
450 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
451 ok(oan
.InheritedObjectTypeName
== szInheritedObjectTypeName
, "InheritedObjectTypeName wrong\n");
452 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
454 /* test GetTrusteeNameA */
455 ok(pGetTrusteeNameA(&trustee
) == (LPSTR
)&oan
, "GetTrusteeName returned wrong value\n");
457 /* test BuildTrusteeWithObjectsAndNameA (test 2) */
458 memset( &trustee
, 0xff, sizeof trustee
);
459 memset( &oan
, 0xff, sizeof(oan
) );
460 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, NULL
,
461 szInheritedObjectTypeName
, szTrusteeName
);
463 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
464 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
465 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
466 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
467 ok(trustee
.ptstrName
== (LPSTR
)&oan
, "ptstrName wrong\n");
469 ok(oan
.ObjectsPresent
== ACE_INHERITED_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
470 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
471 ok(oan
.InheritedObjectTypeName
== szInheritedObjectTypeName
, "InheritedObjectTypeName wrong\n");
472 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
474 /* test BuildTrusteeWithObjectsAndNameA (test 3) */
475 memset( &trustee
, 0xff, sizeof trustee
);
476 memset( &oan
, 0xff, sizeof(oan
) );
477 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, szObjectTypeName
,
478 NULL
, szTrusteeName
);
480 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
481 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
482 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
483 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
484 ok(trustee
.ptstrName
== (LPTSTR
)&oan
, "ptstrName wrong\n");
486 ok(oan
.ObjectsPresent
== ACE_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
487 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
488 ok(oan
.InheritedObjectTypeName
== NULL
, "InheritedObjectTypeName wrong\n");
489 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
492 /* If the first isn't defined, assume none is */
493 #ifndef SE_MIN_WELL_KNOWN_PRIVILEGE
494 #define SE_MIN_WELL_KNOWN_PRIVILEGE 2L
495 #define SE_CREATE_TOKEN_PRIVILEGE 2L
496 #define SE_ASSIGNPRIMARYTOKEN_PRIVILEGE 3L
497 #define SE_LOCK_MEMORY_PRIVILEGE 4L
498 #define SE_INCREASE_QUOTA_PRIVILEGE 5L
499 #define SE_MACHINE_ACCOUNT_PRIVILEGE 6L
500 #define SE_TCB_PRIVILEGE 7L
501 #define SE_SECURITY_PRIVILEGE 8L
502 #define SE_TAKE_OWNERSHIP_PRIVILEGE 9L
503 #define SE_LOAD_DRIVER_PRIVILEGE 10L
504 #define SE_SYSTEM_PROFILE_PRIVILEGE 11L
505 #define SE_SYSTEMTIME_PRIVILEGE 12L
506 #define SE_PROF_SINGLE_PROCESS_PRIVILEGE 13L
507 #define SE_INC_BASE_PRIORITY_PRIVILEGE 14L
508 #define SE_CREATE_PAGEFILE_PRIVILEGE 15L
509 #define SE_CREATE_PERMANENT_PRIVILEGE 16L
510 #define SE_BACKUP_PRIVILEGE 17L
511 #define SE_RESTORE_PRIVILEGE 18L
512 #define SE_SHUTDOWN_PRIVILEGE 19L
513 #define SE_DEBUG_PRIVILEGE 20L
514 #define SE_AUDIT_PRIVILEGE 21L
515 #define SE_SYSTEM_ENVIRONMENT_PRIVILEGE 22L
516 #define SE_CHANGE_NOTIFY_PRIVILEGE 23L
517 #define SE_REMOTE_SHUTDOWN_PRIVILEGE 24L
518 #define SE_UNDOCK_PRIVILEGE 25L
519 #define SE_SYNC_AGENT_PRIVILEGE 26L
520 #define SE_ENABLE_DELEGATION_PRIVILEGE 27L
521 #define SE_MANAGE_VOLUME_PRIVILEGE 28L
522 #define SE_IMPERSONATE_PRIVILEGE 29L
523 #define SE_CREATE_GLOBAL_PRIVILEGE 30L
524 #define SE_MAX_WELL_KNOWN_PRIVILEGE SE_CREATE_GLOBAL_PRIVILEGE
525 #endif /* ndef SE_MIN_WELL_KNOWN_PRIVILEGE */
527 static void test_allocateLuid(void)
529 BOOL (WINAPI
*pAllocateLocallyUniqueId
)(PLUID
);
533 pAllocateLocallyUniqueId
= (void*)GetProcAddress(hmod
, "AllocateLocallyUniqueId");
534 if (!pAllocateLocallyUniqueId
) return;
536 ret
= pAllocateLocallyUniqueId(&luid1
);
537 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
541 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
542 ret
= pAllocateLocallyUniqueId(&luid2
);
544 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
545 ok(luid1
.LowPart
> SE_MAX_WELL_KNOWN_PRIVILEGE
|| luid1
.HighPart
!= 0,
546 "AllocateLocallyUniqueId returned a well-known LUID\n");
547 ok(luid1
.LowPart
!= luid2
.LowPart
|| luid1
.HighPart
!= luid2
.HighPart
,
548 "AllocateLocallyUniqueId returned non-unique LUIDs\n");
549 ret
= pAllocateLocallyUniqueId(NULL
);
550 ok( !ret
&& GetLastError() == ERROR_NOACCESS
,
551 "AllocateLocallyUniqueId(NULL) didn't return ERROR_NOACCESS: %d\n",
555 static void test_lookupPrivilegeName(void)
557 BOOL (WINAPI
*pLookupPrivilegeNameA
)(LPCSTR
, PLUID
, LPSTR
, LPDWORD
);
558 char buf
[MAX_PATH
]; /* arbitrary, seems long enough */
559 DWORD cchName
= sizeof(buf
);
560 LUID luid
= { 0, 0 };
564 /* check whether it's available first */
565 pLookupPrivilegeNameA
= (void*)GetProcAddress(hmod
, "LookupPrivilegeNameA");
566 if (!pLookupPrivilegeNameA
) return;
567 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
568 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
569 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
572 /* check with a short buffer */
574 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
575 ret
= pLookupPrivilegeNameA(NULL
, &luid
, NULL
, &cchName
);
576 ok( !ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
577 "LookupPrivilegeNameA didn't fail with ERROR_INSUFFICIENT_BUFFER: %d\n",
579 ok(cchName
== strlen("SeCreateTokenPrivilege") + 1,
580 "LookupPrivilegeNameA returned an incorrect required length for\n"
581 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName
,
582 lstrlenA("SeCreateTokenPrivilege") + 1);
583 /* check a known value and its returned length on success */
584 cchName
= sizeof(buf
);
585 ok(pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
) &&
586 cchName
== strlen("SeCreateTokenPrivilege"),
587 "LookupPrivilegeNameA returned an incorrect output length for\n"
588 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName
,
589 (int)strlen("SeCreateTokenPrivilege"));
590 /* check known values */
591 for (i
= SE_MIN_WELL_KNOWN_PRIVILEGE
; i
<= SE_MAX_WELL_KNOWN_PRIVILEGE
; i
++)
594 cchName
= sizeof(buf
);
595 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
596 ok( ret
|| GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
597 "LookupPrivilegeNameA(0.%d) failed: %d\n", i
, GetLastError());
599 /* check a bogus LUID */
600 luid
.LowPart
= 0xdeadbeef;
601 cchName
= sizeof(buf
);
602 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
603 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
604 "LookupPrivilegeNameA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
606 /* check on a bogus system */
607 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
608 cchName
= sizeof(buf
);
609 ret
= pLookupPrivilegeNameA("b0gu5.Nam3", &luid
, buf
, &cchName
);
610 ok( !ret
&& (GetLastError() == RPC_S_SERVER_UNAVAILABLE
||
611 GetLastError() == RPC_S_INVALID_NET_ADDR
) /* w2k8 */,
612 "LookupPrivilegeNameA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
622 static void test_lookupPrivilegeValue(void)
624 static const struct NameToLUID privs
[] = {
625 { "SeCreateTokenPrivilege", SE_CREATE_TOKEN_PRIVILEGE
},
626 { "SeAssignPrimaryTokenPrivilege", SE_ASSIGNPRIMARYTOKEN_PRIVILEGE
},
627 { "SeLockMemoryPrivilege", SE_LOCK_MEMORY_PRIVILEGE
},
628 { "SeIncreaseQuotaPrivilege", SE_INCREASE_QUOTA_PRIVILEGE
},
629 { "SeMachineAccountPrivilege", SE_MACHINE_ACCOUNT_PRIVILEGE
},
630 { "SeTcbPrivilege", SE_TCB_PRIVILEGE
},
631 { "SeSecurityPrivilege", SE_SECURITY_PRIVILEGE
},
632 { "SeTakeOwnershipPrivilege", SE_TAKE_OWNERSHIP_PRIVILEGE
},
633 { "SeLoadDriverPrivilege", SE_LOAD_DRIVER_PRIVILEGE
},
634 { "SeSystemProfilePrivilege", SE_SYSTEM_PROFILE_PRIVILEGE
},
635 { "SeSystemtimePrivilege", SE_SYSTEMTIME_PRIVILEGE
},
636 { "SeProfileSingleProcessPrivilege", SE_PROF_SINGLE_PROCESS_PRIVILEGE
},
637 { "SeIncreaseBasePriorityPrivilege", SE_INC_BASE_PRIORITY_PRIVILEGE
},
638 { "SeCreatePagefilePrivilege", SE_CREATE_PAGEFILE_PRIVILEGE
},
639 { "SeCreatePermanentPrivilege", SE_CREATE_PERMANENT_PRIVILEGE
},
640 { "SeBackupPrivilege", SE_BACKUP_PRIVILEGE
},
641 { "SeRestorePrivilege", SE_RESTORE_PRIVILEGE
},
642 { "SeShutdownPrivilege", SE_SHUTDOWN_PRIVILEGE
},
643 { "SeDebugPrivilege", SE_DEBUG_PRIVILEGE
},
644 { "SeAuditPrivilege", SE_AUDIT_PRIVILEGE
},
645 { "SeSystemEnvironmentPrivilege", SE_SYSTEM_ENVIRONMENT_PRIVILEGE
},
646 { "SeChangeNotifyPrivilege", SE_CHANGE_NOTIFY_PRIVILEGE
},
647 { "SeRemoteShutdownPrivilege", SE_REMOTE_SHUTDOWN_PRIVILEGE
},
648 { "SeUndockPrivilege", SE_UNDOCK_PRIVILEGE
},
649 { "SeSyncAgentPrivilege", SE_SYNC_AGENT_PRIVILEGE
},
650 { "SeEnableDelegationPrivilege", SE_ENABLE_DELEGATION_PRIVILEGE
},
651 { "SeManageVolumePrivilege", SE_MANAGE_VOLUME_PRIVILEGE
},
652 { "SeImpersonatePrivilege", SE_IMPERSONATE_PRIVILEGE
},
653 { "SeCreateGlobalPrivilege", SE_CREATE_GLOBAL_PRIVILEGE
},
655 BOOL (WINAPI
*pLookupPrivilegeValueA
)(LPCSTR
, LPCSTR
, PLUID
);
660 /* check whether it's available first */
661 pLookupPrivilegeValueA
= (void*)GetProcAddress(hmod
, "LookupPrivilegeValueA");
662 if (!pLookupPrivilegeValueA
) return;
663 ret
= pLookupPrivilegeValueA(NULL
, "SeCreateTokenPrivilege", &luid
);
664 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
667 /* check a bogus system name */
668 ret
= pLookupPrivilegeValueA("b0gu5.Nam3", "SeCreateTokenPrivilege", &luid
);
669 ok( !ret
&& (GetLastError() == RPC_S_SERVER_UNAVAILABLE
||
670 GetLastError() == RPC_S_INVALID_NET_ADDR
) /* w2k8 */,
671 "LookupPrivilegeValueA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
673 /* check a NULL string */
674 ret
= pLookupPrivilegeValueA(NULL
, 0, &luid
);
675 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
676 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
678 /* check a bogus privilege name */
679 ret
= pLookupPrivilegeValueA(NULL
, "SeBogusPrivilege", &luid
);
680 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
681 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
683 /* check case insensitive */
684 ret
= pLookupPrivilegeValueA(NULL
, "sEcREATEtOKENpRIVILEGE", &luid
);
686 "LookupPrivilegeValueA(NULL, sEcREATEtOKENpRIVILEGE, &luid) failed: %d\n",
688 for (i
= 0; i
< sizeof(privs
) / sizeof(privs
[0]); i
++)
690 /* Not all privileges are implemented on all Windows versions, so
691 * don't worry if the call fails
693 if (pLookupPrivilegeValueA(NULL
, privs
[i
].name
, &luid
))
695 ok(luid
.LowPart
== privs
[i
].lowPart
,
696 "LookupPrivilegeValueA returned an invalid LUID for %s\n",
702 static void test_luid(void)
705 test_lookupPrivilegeName();
706 test_lookupPrivilegeValue();
709 static void test_FileSecurity(void)
711 char wintmpdir
[MAX_PATH
];
712 char path
[MAX_PATH
];
713 char file
[MAX_PATH
];
719 SECURITY_INFORMATION
const request
= OWNER_SECURITY_INFORMATION
720 | GROUP_SECURITY_INFORMATION
721 | DACL_SECURITY_INFORMATION
;
723 if (!pGetFileSecurityA
) {
724 win_skip ("GetFileSecurity is not available\n");
728 if (!pSetFileSecurityA
) {
729 win_skip ("SetFileSecurity is not available\n");
733 if (!GetTempPathA (sizeof (wintmpdir
), wintmpdir
)) {
734 win_skip ("GetTempPathA failed\n");
738 /* Create a temporary directory and in it a temporary file */
739 strcat (strcpy (path
, wintmpdir
), "rary");
740 SetLastError(0xdeadbeef);
741 rc
= CreateDirectoryA (path
, NULL
);
742 ok (rc
|| GetLastError() == ERROR_ALREADY_EXISTS
, "CreateDirectoryA "
743 "failed for '%s' with %d\n", path
, GetLastError());
745 strcat (strcpy (file
, path
), "\\ess");
746 SetLastError(0xdeadbeef);
747 fh
= CreateFileA (file
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, NULL
);
748 ok (fh
!= INVALID_HANDLE_VALUE
, "CreateFileA "
749 "failed for '%s' with %d\n", file
, GetLastError());
752 /* For the temporary file ... */
754 /* Get size needed */
756 SetLastError(0xdeadbeef);
757 rc
= pGetFileSecurityA (file
, request
, NULL
, 0, &retSize
);
758 if (!rc
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)) {
759 win_skip("GetFileSecurityA is not implemented\n");
762 ok (!rc
, "GetFileSecurityA "
763 "was expected to fail for '%s'\n", file
);
764 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetFileSecurityA "
765 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
766 ok (retSize
> sizeof (SECURITY_DESCRIPTOR
), "GetFileSecurityA returned size %d\n", retSize
);
769 sd
= HeapAlloc (GetProcessHeap (), 0, sdSize
);
771 /* Get security descriptor for real */
773 SetLastError(0xdeadbeef);
774 rc
= pGetFileSecurityA (file
, request
, sd
, sdSize
, &retSize
);
775 ok (rc
, "GetFileSecurityA "
776 "was not expected to fail '%s': %d\n", file
, GetLastError());
777 ok (retSize
== sdSize
||
778 broken(retSize
== 0), /* NT4 */
779 "GetFileSecurityA returned size %d; expected %d\n", retSize
, sdSize
);
781 /* Use it to set security descriptor */
782 SetLastError(0xdeadbeef);
783 rc
= pSetFileSecurityA (file
, request
, sd
);
784 ok (rc
, "SetFileSecurityA "
785 "was not expected to fail '%s': %d\n", file
, GetLastError());
787 HeapFree (GetProcessHeap (), 0, sd
);
789 /* Repeat for the temporary directory ... */
791 /* Get size needed */
793 SetLastError(0xdeadbeef);
794 rc
= pGetFileSecurityA (path
, request
, NULL
, 0, &retSize
);
795 ok (!rc
, "GetFileSecurityA "
796 "was expected to fail for '%s'\n", path
);
797 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetFileSecurityA "
798 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
799 ok (retSize
> sizeof (SECURITY_DESCRIPTOR
), "GetFileSecurityA returned size %d\n", retSize
);
802 sd
= HeapAlloc (GetProcessHeap (), 0, sdSize
);
804 /* Get security descriptor for real */
806 SetLastError(0xdeadbeef);
807 rc
= pGetFileSecurityA (path
, request
, sd
, sdSize
, &retSize
);
808 ok (rc
, "GetFileSecurityA "
809 "was not expected to fail '%s': %d\n", path
, GetLastError());
810 ok (retSize
== sdSize
||
811 broken(retSize
== 0), /* NT4 */
812 "GetFileSecurityA returned size %d; expected %d\n", retSize
, sdSize
);
814 /* Use it to set security descriptor */
815 SetLastError(0xdeadbeef);
816 rc
= pSetFileSecurityA (path
, request
, sd
);
817 ok (rc
, "SetFileSecurityA "
818 "was not expected to fail '%s': %d\n", path
, GetLastError());
819 HeapFree (GetProcessHeap (), 0, sd
);
822 strcpy (wintmpdir
, "\\Should not exist");
823 SetLastError(0xdeadbeef);
824 rc
= pGetFileSecurityA (wintmpdir
, OWNER_SECURITY_INFORMATION
, NULL
, 0, &sdSize
);
825 ok (!rc
, "GetFileSecurityA should fail for not existing directories/files\n");
826 ok (GetLastError() == ERROR_FILE_NOT_FOUND
,
827 "last error ERROR_FILE_NOT_FOUND expected, got %d\n", GetLastError());
830 /* Remove temporary file and directory */
832 RemoveDirectoryA(path
);
835 static void test_AccessCheck(void)
837 PSID EveryoneSid
= NULL
, AdminSid
= NULL
, UsersSid
= NULL
;
839 SECURITY_DESCRIPTOR
*SecurityDescriptor
= NULL
;
840 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
841 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
842 GENERIC_MAPPING Mapping
= { KEY_READ
, KEY_WRITE
, KEY_EXECUTE
, KEY_ALL_ACCESS
};
849 PRIVILEGE_SET
*PrivSet
;
854 NTSTATUS ntret
, ntAccessStatus
;
856 NtDllModule
= GetModuleHandle("ntdll.dll");
859 skip("not running on NT, skipping test\n");
862 pRtlAdjustPrivilege
= (fnRtlAdjustPrivilege
)
863 GetProcAddress(NtDllModule
, "RtlAdjustPrivilege");
864 if (!pRtlAdjustPrivilege
)
866 win_skip("missing RtlAdjustPrivilege, skipping test\n");
870 Acl
= HeapAlloc(GetProcessHeap(), 0, 256);
871 res
= InitializeAcl(Acl
, 256, ACL_REVISION
);
872 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
874 skip("ACLs not implemented - skipping tests\n");
875 HeapFree(GetProcessHeap(), 0, Acl
);
878 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
880 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
881 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
883 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
884 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &AdminSid
);
885 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
887 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
888 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
889 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
891 SecurityDescriptor
= HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
893 res
= InitializeSecurityDescriptor(SecurityDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
894 ok(res
, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
896 res
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
897 ok(res
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
899 PrivSetLen
= FIELD_OFFSET(PRIVILEGE_SET
, Privilege
[16]);
900 PrivSet
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, PrivSetLen
);
901 PrivSet
->PrivilegeCount
= 16;
903 res
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &ProcessToken
);
904 ok(res
, "OpenProcessToken failed with error %d\n", GetLastError());
906 pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE
, FALSE
, TRUE
, &Enabled
);
908 res
= DuplicateToken(ProcessToken
, SecurityImpersonation
, &Token
);
909 ok(res
, "DuplicateToken failed with error %d\n", GetLastError());
911 /* SD without owner/group */
912 SetLastError(0xdeadbeef);
913 Access
= AccessStatus
= 0xdeadbeef;
914 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_QUERY_VALUE
, &Mapping
,
915 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
916 err
= GetLastError();
917 ok(!ret
&& err
== ERROR_INVALID_SECURITY_DESCR
, "AccessCheck should have "
918 "failed with ERROR_INVALID_SECURITY_DESCR, instead of %d\n", err
);
919 ok(Access
== 0xdeadbeef && AccessStatus
== 0xdeadbeef,
920 "Access and/or AccessStatus were changed!\n");
922 /* Set owner and group */
923 res
= SetSecurityDescriptorOwner(SecurityDescriptor
, AdminSid
, FALSE
);
924 ok(res
, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
925 res
= SetSecurityDescriptorGroup(SecurityDescriptor
, UsersSid
, TRUE
);
926 ok(res
, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
928 /* Generic access mask */
929 SetLastError(0xdeadbeef);
930 Access
= AccessStatus
= 0xdeadbeef;
931 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
932 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
933 err
= GetLastError();
934 ok(!ret
&& err
== ERROR_GENERIC_NOT_MAPPED
, "AccessCheck should have failed "
935 "with ERROR_GENERIC_NOT_MAPPED, instead of %d\n", err
);
936 ok(Access
== 0xdeadbeef && AccessStatus
== 0xdeadbeef,
937 "Access and/or AccessStatus were changed!\n");
939 /* Generic access mask - no privilegeset buffer */
940 SetLastError(0xdeadbeef);
941 Access
= AccessStatus
= 0xdeadbeef;
942 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
943 NULL
, &PrivSetLen
, &Access
, &AccessStatus
);
944 err
= GetLastError();
945 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
946 "with ERROR_NOACCESS, instead of %d\n", err
);
947 ok(Access
== 0xdeadbeef && AccessStatus
== 0xdeadbeef,
948 "Access and/or AccessStatus were changed!\n");
950 /* Generic access mask - no returnlength */
951 SetLastError(0xdeadbeef);
952 Access
= AccessStatus
= 0xdeadbeef;
953 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
954 PrivSet
, NULL
, &Access
, &AccessStatus
);
955 err
= GetLastError();
956 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
957 "with ERROR_NOACCESS, instead of %d\n", err
);
958 ok(Access
== 0xdeadbeef && AccessStatus
== 0xdeadbeef,
959 "Access and/or AccessStatus were changed!\n");
961 /* Generic access mask - no privilegeset buffer, no returnlength */
962 SetLastError(0xdeadbeef);
963 Access
= AccessStatus
= 0xdeadbeef;
964 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
965 NULL
, NULL
, &Access
, &AccessStatus
);
966 err
= GetLastError();
967 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
968 "with ERROR_NOACCESS, instead of %d\n", err
);
969 ok(Access
== 0xdeadbeef && AccessStatus
== 0xdeadbeef,
970 "Access and/or AccessStatus were changed!\n");
972 /* sd with no dacl present */
973 Access
= AccessStatus
= 0xdeadbeef;
974 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, FALSE
, NULL
, FALSE
);
975 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
976 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
977 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
978 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
979 ok(AccessStatus
&& (Access
== KEY_READ
),
980 "AccessCheck failed to grant access with error %d\n",
983 /* sd with no dacl present - no privilegeset buffer */
984 SetLastError(0xdeadbeef);
985 Access
= AccessStatus
= 0xdeadbeef;
986 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
987 NULL
, &PrivSetLen
, &Access
, &AccessStatus
);
988 err
= GetLastError();
989 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
990 "with ERROR_NOACCESS, instead of %d\n", err
);
991 ok(Access
== 0xdeadbeef && AccessStatus
== 0xdeadbeef,
992 "Access and/or AccessStatus were changed!\n");
996 /* Generic access mask - no privilegeset buffer */
997 SetLastError(0xdeadbeef);
998 Access
= ntAccessStatus
= 0xdeadbeef;
999 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1000 NULL
, &PrivSetLen
, &Access
, &ntAccessStatus
);
1001 err
= GetLastError();
1002 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1003 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1004 ok(err
== 0xdeadbeef,
1005 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1006 ok(Access
== 0xdeadbeef && ntAccessStatus
== 0xdeadbeef,
1007 "Access and/or AccessStatus were changed!\n");
1009 /* Generic access mask - no returnlength */
1010 SetLastError(0xdeadbeef);
1011 Access
= ntAccessStatus
= 0xdeadbeef;
1012 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1013 PrivSet
, NULL
, &Access
, &ntAccessStatus
);
1014 err
= GetLastError();
1015 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1016 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1017 ok(err
== 0xdeadbeef,
1018 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1019 ok(Access
== 0xdeadbeef && ntAccessStatus
== 0xdeadbeef,
1020 "Access and/or AccessStatus were changed!\n");
1022 /* Generic access mask - no privilegeset buffer, no returnlength */
1023 SetLastError(0xdeadbeef);
1024 Access
= ntAccessStatus
= 0xdeadbeef;
1025 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1026 NULL
, NULL
, &Access
, &ntAccessStatus
);
1027 err
= GetLastError();
1028 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1029 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1030 ok(err
== 0xdeadbeef,
1031 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1032 ok(Access
== 0xdeadbeef && ntAccessStatus
== 0xdeadbeef,
1033 "Access and/or AccessStatus were changed!\n");
1036 win_skip("NtAccessCheck unavailable. Skipping.\n");
1038 /* sd with NULL dacl */
1039 Access
= AccessStatus
= 0xdeadbeef;
1040 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, NULL
, FALSE
);
1041 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1042 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1043 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1044 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1045 ok(AccessStatus
&& (Access
== KEY_READ
),
1046 "AccessCheck failed to grant access with error %d\n",
1049 /* sd with blank dacl */
1050 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
1051 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1052 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1053 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1054 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1055 err
= GetLastError();
1056 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1057 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1058 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1060 res
= AddAccessAllowedAce(Acl
, ACL_REVISION
, KEY_READ
, EveryoneSid
);
1061 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
1063 res
= AddAccessDeniedAce(Acl
, ACL_REVISION
, KEY_SET_VALUE
, AdminSid
);
1064 ok(res
, "AddAccessDeniedAce failed with error %d\n", GetLastError());
1067 Access
= AccessStatus
= 0xdeadbeef;
1068 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1069 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1070 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1071 ok(AccessStatus
&& (Access
== KEY_READ
),
1072 "AccessCheck failed to grant access with error %d\n",
1075 ret
= AccessCheck(SecurityDescriptor
, Token
, MAXIMUM_ALLOWED
, &Mapping
,
1076 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1077 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1079 "AccessCheck failed to grant any access with error %d\n",
1081 trace("AccessCheck with MAXIMUM_ALLOWED got Access 0x%08x\n", Access
);
1083 /* Access denied by SD */
1084 SetLastError(0xdeadbeef);
1085 Access
= AccessStatus
= 0xdeadbeef;
1086 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_WRITE
, &Mapping
,
1087 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1088 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1089 err
= GetLastError();
1090 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1091 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1092 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1095 PrivSet
->PrivilegeCount
= 16;
1096 ret
= AccessCheck(SecurityDescriptor
, Token
, ACCESS_SYSTEM_SECURITY
, &Mapping
,
1097 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1098 ok(ret
&& !AccessStatus
&& GetLastError() == ERROR_PRIVILEGE_NOT_HELD
,
1099 "AccessCheck should have failed with ERROR_PRIVILEGE_NOT_HELD, instead of %d\n",
1102 ret
= ImpersonateLoggedOnUser(Token
);
1103 ok(ret
, "ImpersonateLoggedOnUser failed with error %d\n", GetLastError());
1104 ret
= pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE
, TRUE
, TRUE
, &Enabled
);
1108 PrivSet
->PrivilegeCount
= 16;
1109 ret
= AccessCheck(SecurityDescriptor
, Token
, ACCESS_SYSTEM_SECURITY
, &Mapping
,
1110 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1111 ok(ret
&& AccessStatus
&& GetLastError() == 0,
1112 "AccessCheck should have succeeded, error %d\n",
1114 ok(Access
== ACCESS_SYSTEM_SECURITY
,
1115 "Access should be equal to ACCESS_SYSTEM_SECURITY instead of 0x%08x\n",
1119 trace("Couldn't get SE_SECURITY_PRIVILEGE (0x%08x), skipping ACCESS_SYSTEM_SECURITY test\n",
1121 ret
= RevertToSelf();
1122 ok(ret
, "RevertToSelf failed with error %d\n", GetLastError());
1124 /* test INHERIT_ONLY_ACE */
1125 ret
= InitializeAcl(Acl
, 256, ACL_REVISION
);
1126 ok(ret
, "InitializeAcl failed with error %d\n", GetLastError());
1128 /* NT doesn't have AddAccessAllowedAceEx. Skipping this call/test doesn't influence
1131 if (pAddAccessAllowedAceEx
)
1133 ret
= pAddAccessAllowedAceEx(Acl
, ACL_REVISION
, INHERIT_ONLY_ACE
, KEY_READ
, EveryoneSid
);
1134 ok(ret
, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
1137 win_skip("AddAccessAllowedAceEx is not available\n");
1139 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1140 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1141 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1142 err
= GetLastError();
1143 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1144 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1145 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1149 res
= DuplicateToken(ProcessToken
, SecurityAnonymous
, &Token
);
1150 ok(res
, "DuplicateToken failed with error %d\n", GetLastError());
1152 SetLastError(0xdeadbeef);
1153 ret
= AccessCheck(SecurityDescriptor
, Token
, MAXIMUM_ALLOWED
, &Mapping
,
1154 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1155 err
= GetLastError();
1156 ok(!ret
&& err
== ERROR_BAD_IMPERSONATION_LEVEL
, "AccessCheck should have failed "
1157 "with ERROR_BAD_IMPERSONATION_LEVEL, instead of %d\n", err
);
1161 SetLastError(0xdeadbeef);
1162 ret
= AccessCheck(SecurityDescriptor
, ProcessToken
, KEY_READ
, &Mapping
,
1163 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1164 err
= GetLastError();
1165 ok(!ret
&& err
== ERROR_NO_IMPERSONATION_TOKEN
, "AccessCheck should have failed "
1166 "with ERROR_NO_IMPERSONATION_TOKEN, instead of %d\n", err
);
1168 CloseHandle(ProcessToken
);
1171 FreeSid(EveryoneSid
);
1176 HeapFree(GetProcessHeap(), 0, Acl
);
1177 HeapFree(GetProcessHeap(), 0, SecurityDescriptor
);
1178 HeapFree(GetProcessHeap(), 0, PrivSet
);
1181 /* test GetTokenInformation for the various attributes */
1182 static void test_token_attr(void)
1184 HANDLE Token
, ImpersonationToken
;
1186 TOKEN_PRIVILEGES
*Privileges
;
1187 TOKEN_GROUPS
*Groups
;
1189 TOKEN_DEFAULT_DACL
*Dacl
;
1193 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
;
1196 /* cygwin-like use case */
1197 SetLastError(0xdeadbeef);
1198 ret
= OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED
, &Token
);
1199 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
1201 win_skip("OpenProcessToken is not implemented\n");
1204 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1207 DWORD buf
[256]; /* GetTokenInformation wants a dword-aligned buffer */
1209 ret
= GetTokenInformation(Token
, TokenUser
,(void*)buf
, Size
, &Size
);
1210 ok(ret
, "GetTokenInformation failed with error %d\n", GetLastError());
1211 Size
= sizeof(ImpersonationLevel
);
1212 ret
= GetTokenInformation(Token
, TokenImpersonationLevel
, &ImpersonationLevel
, Size
, &Size
);
1213 GLE
= GetLastError();
1214 ok(!ret
&& (GLE
== ERROR_INVALID_PARAMETER
), "GetTokenInformation(TokenImpersonationLevel) on primary token should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GLE
);
1218 if(!pConvertSidToStringSidA
)
1220 win_skip("ConvertSidToStringSidA is not available\n");
1224 SetLastError(0xdeadbeef);
1225 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS
, &Token
);
1226 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1229 ret
= GetTokenInformation(Token
, TokenGroups
, NULL
, 0, &Size
);
1230 Groups
= HeapAlloc(GetProcessHeap(), 0, Size
);
1231 ret
= GetTokenInformation(Token
, TokenGroups
, Groups
, Size
, &Size
);
1232 ok(ret
, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
1233 trace("TokenGroups:\n");
1234 for (i
= 0; i
< Groups
->GroupCount
; i
++)
1236 DWORD NameLength
= 255;
1238 DWORD DomainLength
= 255;
1240 SID_NAME_USE SidNameUse
;
1241 pConvertSidToStringSidA(Groups
->Groups
[i
].Sid
, &SidString
);
1244 ret
= LookupAccountSid(NULL
, Groups
->Groups
[i
].Sid
, Name
, &NameLength
, Domain
, &DomainLength
, &SidNameUse
);
1247 trace("%s, %s\\%s use: %d attr: 0x%08x\n", SidString
, Domain
, Name
, SidNameUse
, Groups
->Groups
[i
].Attributes
);
1248 LocalFree(SidString
);
1250 else trace("attr: 0x%08x LookupAccountSid failed with error %d\n", Groups
->Groups
[i
].Attributes
, GetLastError());
1252 HeapFree(GetProcessHeap(), 0, Groups
);
1255 ret
= GetTokenInformation(Token
, TokenUser
, NULL
, 0, &Size
);
1256 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1257 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1258 User
= HeapAlloc(GetProcessHeap(), 0, Size
);
1259 ret
= GetTokenInformation(Token
, TokenUser
, User
, Size
, &Size
);
1261 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1263 pConvertSidToStringSidA(User
->User
.Sid
, &SidString
);
1264 trace("TokenUser: %s attr: 0x%08x\n", SidString
, User
->User
.Attributes
);
1265 LocalFree(SidString
);
1266 HeapFree(GetProcessHeap(), 0, User
);
1269 ret
= GetTokenInformation(Token
, TokenPrivileges
, NULL
, 0, &Size
);
1270 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1271 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1272 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
1273 ret
= GetTokenInformation(Token
, TokenPrivileges
, Privileges
, Size
, &Size
);
1275 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1276 trace("TokenPrivileges:\n");
1277 for (i
= 0; i
< Privileges
->PrivilegeCount
; i
++)
1280 DWORD NameLen
= sizeof(Name
)/sizeof(Name
[0]);
1281 LookupPrivilegeName(NULL
, &Privileges
->Privileges
[i
].Luid
, Name
, &NameLen
);
1282 trace("\t%s, 0x%x\n", Name
, Privileges
->Privileges
[i
].Attributes
);
1284 HeapFree(GetProcessHeap(), 0, Privileges
);
1286 ret
= DuplicateToken(Token
, SecurityAnonymous
, &ImpersonationToken
);
1287 ok(ret
, "DuplicateToken failed with error %d\n", GetLastError());
1289 Size
= sizeof(ImpersonationLevel
);
1290 ret
= GetTokenInformation(ImpersonationToken
, TokenImpersonationLevel
, &ImpersonationLevel
, Size
, &Size
);
1291 ok(ret
, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1292 ok(ImpersonationLevel
== SecurityAnonymous
, "ImpersonationLevel should have been SecurityAnonymous instead of %d\n", ImpersonationLevel
);
1294 CloseHandle(ImpersonationToken
);
1297 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, NULL
, 0, &Size
);
1298 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1299 "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1301 Dacl
= HeapAlloc(GetProcessHeap(), 0, Size
);
1302 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size
);
1303 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1305 SetLastError(0xdeadbeef);
1306 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, NULL
, 0);
1307 GLE
= GetLastError();
1308 ok(!ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1309 ok(GLE
== ERROR_BAD_LENGTH
, "expected ERROR_BAD_LENGTH got %u\n", GLE
);
1311 SetLastError(0xdeadbeef);
1312 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, NULL
, Size
);
1313 GLE
= GetLastError();
1314 ok(!ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1315 ok(GLE
== ERROR_NOACCESS
, "expected ERROR_NOACCESS got %u\n", GLE
);
1317 acl
= Dacl
->DefaultDacl
;
1318 Dacl
->DefaultDacl
= NULL
;
1320 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
);
1321 ok(ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1324 Dacl
->DefaultDacl
= (ACL
*)0xdeadbeef;
1325 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size2
);
1326 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1327 ok(Dacl
->DefaultDacl
== NULL
, "expected NULL, got %p\n", Dacl
->DefaultDacl
);
1328 ok(Size2
== sizeof(TOKEN_DEFAULT_DACL
), "got %u expected sizeof(TOKEN_DEFAULT_DACL)\n", Size2
);
1330 Dacl
->DefaultDacl
= acl
;
1331 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
);
1332 ok(ret
, "SetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1334 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size2
);
1335 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1337 HeapFree(GetProcessHeap(), 0, Dacl
);
1341 typedef union _MAX_SID
1344 char max
[SECURITY_MAX_SID_SIZE
];
1347 static void test_sid_str(PSID
* sid
)
1350 BOOL ret
= pConvertSidToStringSidA(sid
, &str_sid
);
1351 ok(ret
, "ConvertSidToStringSidA() failed: %d\n", GetLastError());
1354 char account
[MAX_PATH
], domain
[MAX_PATH
];
1356 DWORD acc_size
= MAX_PATH
;
1357 DWORD dom_size
= MAX_PATH
;
1358 ret
= LookupAccountSid(NULL
, sid
, account
, &acc_size
, domain
, &dom_size
, &use
);
1359 ok(ret
|| (!ret
&& (GetLastError() == ERROR_NONE_MAPPED
)),
1360 "LookupAccountSid(%s) failed: %d\n", str_sid
, GetLastError());
1362 trace(" %s %s\\%s %d\n", str_sid
, domain
, account
, use
);
1363 else if (GetLastError() == ERROR_NONE_MAPPED
)
1364 trace(" %s couldn't be mapped\n", str_sid
);
1369 struct well_known_sid_value
1371 BOOL without_domain
;
1372 const char *sid_string
;
1373 } well_known_sid_values
[] = {
1374 /* 0 */ {TRUE
, "S-1-0-0"}, {TRUE
, "S-1-1-0"}, {TRUE
, "S-1-2-0"}, {TRUE
, "S-1-3-0"},
1375 /* 4 */ {TRUE
, "S-1-3-1"}, {TRUE
, "S-1-3-2"}, {TRUE
, "S-1-3-3"}, {TRUE
, "S-1-5"},
1376 /* 8 */ {FALSE
, "S-1-5-1"}, {TRUE
, "S-1-5-2"}, {TRUE
, "S-1-5-3"}, {TRUE
, "S-1-5-4"},
1377 /* 12 */ {TRUE
, "S-1-5-6"}, {TRUE
, "S-1-5-7"}, {TRUE
, "S-1-5-8"}, {TRUE
, "S-1-5-9"},
1378 /* 16 */ {TRUE
, "S-1-5-10"}, {TRUE
, "S-1-5-11"}, {TRUE
, "S-1-5-12"}, {TRUE
, "S-1-5-13"},
1379 /* 20 */ {TRUE
, "S-1-5-14"}, {FALSE
, NULL
}, {TRUE
, "S-1-5-18"}, {TRUE
, "S-1-5-19"},
1380 /* 24 */ {TRUE
, "S-1-5-20"}, {TRUE
, "S-1-5-32"},
1381 /* 26 */ {FALSE
, "S-1-5-32-544"}, {TRUE
, "S-1-5-32-545"}, {TRUE
, "S-1-5-32-546"},
1382 /* 29 */ {TRUE
, "S-1-5-32-547"}, {TRUE
, "S-1-5-32-548"}, {TRUE
, "S-1-5-32-549"},
1383 /* 32 */ {TRUE
, "S-1-5-32-550"}, {TRUE
, "S-1-5-32-551"}, {TRUE
, "S-1-5-32-552"},
1384 /* 35 */ {TRUE
, "S-1-5-32-554"}, {TRUE
, "S-1-5-32-555"}, {TRUE
, "S-1-5-32-556"},
1385 /* 38 */ {FALSE
, "S-1-5-21-12-23-34-45-56-500"}, {FALSE
, "S-1-5-21-12-23-34-45-56-501"},
1386 /* 40 */ {FALSE
, "S-1-5-21-12-23-34-45-56-502"}, {FALSE
, "S-1-5-21-12-23-34-45-56-512"},
1387 /* 42 */ {FALSE
, "S-1-5-21-12-23-34-45-56-513"}, {FALSE
, "S-1-5-21-12-23-34-45-56-514"},
1388 /* 44 */ {FALSE
, "S-1-5-21-12-23-34-45-56-515"}, {FALSE
, "S-1-5-21-12-23-34-45-56-516"},
1389 /* 46 */ {FALSE
, "S-1-5-21-12-23-34-45-56-517"}, {FALSE
, "S-1-5-21-12-23-34-45-56-518"},
1390 /* 48 */ {FALSE
, "S-1-5-21-12-23-34-45-56-519"}, {FALSE
, "S-1-5-21-12-23-34-45-56-520"},
1391 /* 50 */ {FALSE
, "S-1-5-21-12-23-34-45-56-553"},
1392 /* Added in Windows Server 2003 */
1393 /* 51 */ {TRUE
, "S-1-5-64-10"}, {TRUE
, "S-1-5-64-21"}, {TRUE
, "S-1-5-64-14"},
1394 /* 54 */ {TRUE
, "S-1-5-15"}, {TRUE
, "S-1-5-1000"}, {FALSE
, "S-1-5-32-557"},
1395 /* 57 */ {TRUE
, "S-1-5-32-558"}, {TRUE
, "S-1-5-32-559"}, {TRUE
, "S-1-5-32-560"},
1396 /* 60 */ {TRUE
, "S-1-5-32-561"}, {TRUE
, "S-1-5-32-562"},
1397 /* Added in Windows Vista: */
1398 /* 62 */ {TRUE
, "S-1-5-32-568"},
1399 /* 63 */ {TRUE
, "S-1-5-17"}, {FALSE
, "S-1-5-32-569"}, {TRUE
, "S-1-16-0"},
1400 /* 66 */ {TRUE
, "S-1-16-4096"}, {TRUE
, "S-1-16-8192"}, {TRUE
, "S-1-16-12288"},
1401 /* 69 */ {TRUE
, "S-1-16-16384"}, {TRUE
, "S-1-5-33"}, {TRUE
, "S-1-3-4"},
1402 /* 72 */ {FALSE
, "S-1-5-21-12-23-34-45-56-571"}, {FALSE
, "S-1-5-21-12-23-34-45-56-572"},
1403 /* 74 */ {TRUE
, "S-1-5-22"}, {FALSE
, "S-1-5-21-12-23-34-45-56-521"}, {TRUE
, "S-1-5-32-573"}
1406 static void test_CreateWellKnownSid(void)
1408 SID_IDENTIFIER_AUTHORITY ident
= { SECURITY_NT_AUTHORITY
};
1409 PSID domainsid
, sid
;
1414 if (!pCreateWellKnownSid
)
1416 win_skip("CreateWellKnownSid not available\n");
1421 SetLastError(0xdeadbeef);
1422 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, NULL
, &size
);
1423 error
= GetLastError();
1424 ok(!ret
, "CreateWellKnownSid succeeded\n");
1425 ok(error
== ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error
);
1426 ok(size
, "expected size > 0\n");
1428 SetLastError(0xdeadbeef);
1429 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, NULL
, &size
);
1430 error
= GetLastError();
1431 ok(!ret
, "CreateWellKnownSid succeeded\n");
1432 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %u\n", error
);
1434 sid
= HeapAlloc(GetProcessHeap(), 0, size
);
1435 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, sid
, &size
);
1436 ok(ret
, "CreateWellKnownSid failed %u\n", GetLastError());
1437 HeapFree(GetProcessHeap(), 0, sid
);
1439 /* a domain sid usually have three subauthorities but we test that CreateWellKnownSid doesn't check it */
1440 AllocateAndInitializeSid(&ident
, 6, SECURITY_NT_NON_UNIQUE
, 12, 23, 34, 45, 56, 0, 0, &domainsid
);
1442 for (i
= 0; i
< sizeof(well_known_sid_values
)/sizeof(well_known_sid_values
[0]); i
++)
1444 struct well_known_sid_value
*value
= &well_known_sid_values
[i
];
1445 char sid_buffer
[SECURITY_MAX_SID_SIZE
];
1449 if (value
->sid_string
== NULL
)
1452 if (i
> WinAccountRasAndIasServersSid
)
1454 /* These SIDs aren't implemented by all Windows versions - detect it and break the loop */
1455 cb
= sizeof(sid_buffer
);
1456 if (!pCreateWellKnownSid(i
, domainsid
, sid_buffer
, &cb
))
1458 skip("Well known SIDs starting from %d are not implemented\n", i
);
1463 cb
= sizeof(sid_buffer
);
1464 ok(pCreateWellKnownSid(i
, value
->without_domain
? NULL
: domainsid
, sid_buffer
, &cb
), "Couldn't create well known sid %d\n", i
);
1465 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer
)), cb
, DWORD
, "%d");
1466 ok(IsValidSid(sid_buffer
), "The sid is not valid\n");
1467 ok(pConvertSidToStringSidA(sid_buffer
, &str
), "Couldn't convert SID to string\n");
1468 ok(strcmp(str
, value
->sid_string
) == 0, "SID mismatch - expected %s, got %s\n",
1469 value
->sid_string
, str
);
1472 if (value
->without_domain
)
1474 char buf2
[SECURITY_MAX_SID_SIZE
];
1476 ok(pCreateWellKnownSid(i
, domainsid
, buf2
, &cb
), "Couldn't create well known sid %d with optional domain\n", i
);
1477 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer
)), cb
, DWORD
, "%d");
1478 ok(memcmp(buf2
, sid_buffer
, cb
) == 0, "SID create with domain is different than without (%d)\n", i
);
1483 static void test_LookupAccountSid(void)
1485 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
1486 CHAR accountA
[MAX_PATH
], domainA
[MAX_PATH
], usernameA
[MAX_PATH
];
1487 DWORD acc_sizeA
, dom_sizeA
, user_sizeA
;
1488 DWORD real_acc_sizeA
, real_dom_sizeA
;
1489 WCHAR accountW
[MAX_PATH
], domainW
[MAX_PATH
];
1490 DWORD acc_sizeW
, dom_sizeW
;
1491 DWORD real_acc_sizeW
, real_dom_sizeW
;
1492 PSID pUsersSid
= NULL
;
1495 DWORD size
,cbti
= 0;
1500 PTOKEN_USER ptiUser
= NULL
;
1502 /* native windows crashes if account size, domain size, or name use is NULL */
1504 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1505 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &pUsersSid
);
1506 ok(ret
|| (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
),
1507 "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1509 /* not running on NT so give up */
1510 if (!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
1513 real_acc_sizeA
= MAX_PATH
;
1514 real_dom_sizeA
= MAX_PATH
;
1515 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &real_acc_sizeA
, domainA
, &real_dom_sizeA
, &use
);
1516 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1518 /* try NULL account */
1519 acc_sizeA
= MAX_PATH
;
1520 dom_sizeA
= MAX_PATH
;
1521 ret
= LookupAccountSidA(NULL
, pUsersSid
, NULL
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1522 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1524 /* try NULL domain */
1525 acc_sizeA
= MAX_PATH
;
1526 dom_sizeA
= MAX_PATH
;
1527 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, NULL
, &dom_sizeA
, &use
);
1528 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1530 /* try a small account buffer */
1532 dom_sizeA
= MAX_PATH
;
1534 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1535 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1536 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1537 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1539 /* try a 0 sized account buffer */
1541 dom_sizeA
= MAX_PATH
;
1543 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1544 /* this can fail or succeed depending on OS version but the size will always be returned */
1545 ok(acc_sizeA
== real_acc_sizeA
+ 1,
1546 "LookupAccountSidA() Expected acc_size = %u, got %u\n",
1547 real_acc_sizeA
+ 1, acc_sizeA
);
1549 /* try a 0 sized account buffer */
1551 dom_sizeA
= MAX_PATH
;
1552 ret
= LookupAccountSidA(NULL
, pUsersSid
, NULL
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1553 /* this can fail or succeed depending on OS version but the size will always be returned */
1554 ok(acc_sizeA
== real_acc_sizeA
+ 1,
1555 "LookupAccountSid() Expected acc_size = %u, got %u\n",
1556 real_acc_sizeA
+ 1, acc_sizeA
);
1558 /* try a small domain buffer */
1560 acc_sizeA
= MAX_PATH
;
1562 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1563 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1564 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1565 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1567 /* try a 0 sized domain buffer */
1569 acc_sizeA
= MAX_PATH
;
1571 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1572 /* this can fail or succeed depending on OS version but the size will always be returned */
1573 ok(dom_sizeA
== real_dom_sizeA
+ 1,
1574 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1575 real_dom_sizeA
+ 1, dom_sizeA
);
1577 /* try a 0 sized domain buffer */
1579 acc_sizeA
= MAX_PATH
;
1580 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, NULL
, &dom_sizeA
, &use
);
1581 /* this can fail or succeed depending on OS version but the size will always be returned */
1582 ok(dom_sizeA
== real_dom_sizeA
+ 1,
1583 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1584 real_dom_sizeA
+ 1, dom_sizeA
);
1586 real_acc_sizeW
= MAX_PATH
;
1587 real_dom_sizeW
= MAX_PATH
;
1588 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &real_acc_sizeW
, domainW
, &real_dom_sizeW
, &use
);
1589 ok(ret
, "LookupAccountSidW() Expected TRUE, got FALSE\n");
1591 /* try an invalid system name */
1592 real_acc_sizeA
= MAX_PATH
;
1593 real_dom_sizeA
= MAX_PATH
;
1594 ret
= LookupAccountSidA("deepthought", pUsersSid
, accountA
, &real_acc_sizeA
, domainA
, &real_dom_sizeA
, &use
);
1595 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1596 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE
|| GetLastError() == RPC_S_INVALID_NET_ADDR
/* Vista */,
1597 "LookupAccountSidA() Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %u\n", GetLastError());
1599 /* native windows crashes if domainW or accountW is NULL */
1601 /* try a small account buffer */
1603 dom_sizeW
= MAX_PATH
;
1605 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1606 ok(!ret
, "LookupAccountSidW() Expected FALSE got TRUE\n");
1607 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1608 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1610 /* try a 0 sized account buffer */
1612 dom_sizeW
= MAX_PATH
;
1614 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1615 /* this can fail or succeed depending on OS version but the size will always be returned */
1616 ok(acc_sizeW
== real_acc_sizeW
+ 1,
1617 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1618 real_acc_sizeW
+ 1, acc_sizeW
);
1620 /* try a 0 sized account buffer */
1622 dom_sizeW
= MAX_PATH
;
1623 ret
= LookupAccountSidW(NULL
, pUsersSid
, NULL
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1624 /* this can fail or succeed depending on OS version but the size will always be returned */
1625 ok(acc_sizeW
== real_acc_sizeW
+ 1,
1626 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1627 real_acc_sizeW
+ 1, acc_sizeW
);
1629 /* try a small domain buffer */
1631 acc_sizeW
= MAX_PATH
;
1633 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1634 ok(!ret
, "LookupAccountSidW() Expected FALSE got TRUE\n");
1635 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1636 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1638 /* try a 0 sized domain buffer */
1640 acc_sizeW
= MAX_PATH
;
1642 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1643 /* this can fail or succeed depending on OS version but the size will always be returned */
1644 ok(dom_sizeW
== real_dom_sizeW
+ 1,
1645 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1646 real_dom_sizeW
+ 1, dom_sizeW
);
1648 /* try a 0 sized domain buffer */
1650 acc_sizeW
= MAX_PATH
;
1651 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, NULL
, &dom_sizeW
, &use
);
1652 /* this can fail or succeed depending on OS version but the size will always be returned */
1653 ok(dom_sizeW
== real_dom_sizeW
+ 1,
1654 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1655 real_dom_sizeW
+ 1, dom_sizeW
);
1659 /* Test LookupAccountSid with Sid retrieved from token information.
1660 This assumes this process is running under the account of the current user.*/
1661 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
|TOKEN_DUPLICATE
, &hToken
);
1662 ret
= GetTokenInformation(hToken
, TokenUser
, NULL
, 0, &cbti
);
1663 ptiUser
= (PTOKEN_USER
) HeapAlloc(GetProcessHeap(), 0, cbti
);
1664 if (GetTokenInformation(hToken
, TokenUser
, ptiUser
, cbti
, &cbti
))
1666 acc_sizeA
= dom_sizeA
= MAX_PATH
;
1667 ret
= LookupAccountSidA(NULL
, ptiUser
->User
.Sid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1668 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1669 user_sizeA
= MAX_PATH
;
1670 ret
= GetUserNameA(usernameA
, &user_sizeA
);
1671 ok(ret
, "GetUserNameA() Expected TRUE, got FALSE\n");
1674 ok(lstrcmpA(usernameA
, accountA
) == 0, "LookupAccountSidA() Expected account name: %s got: %s\n", usernameA
, accountA
);
1677 HeapFree(GetProcessHeap(), 0, ptiUser
);
1679 if (pCreateWellKnownSid
&& pConvertSidToStringSidA
)
1681 trace("Well Known SIDs:\n");
1682 for (i
= 0; i
<= 60; i
++)
1684 size
= SECURITY_MAX_SID_SIZE
;
1685 if (pCreateWellKnownSid(i
, NULL
, &max_sid
.sid
, &size
))
1687 if (pConvertSidToStringSidA(&max_sid
.sid
, &str_sidA
))
1689 acc_sizeA
= MAX_PATH
;
1690 dom_sizeA
= MAX_PATH
;
1691 if (LookupAccountSidA(NULL
, &max_sid
.sid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
))
1692 trace(" %d: %s %s\\%s %d\n", i
, str_sidA
, domainA
, accountA
, use
);
1693 LocalFree(str_sidA
);
1698 if (GetLastError() != ERROR_INVALID_PARAMETER
)
1699 trace(" CreateWellKnownSid(%d) failed: %d\n", i
, GetLastError());
1701 trace(" %d: not supported\n", i
);
1705 pLsaQueryInformationPolicy
= (fnLsaQueryInformationPolicy
)GetProcAddress( hmod
, "LsaQueryInformationPolicy");
1706 pLsaOpenPolicy
= (fnLsaOpenPolicy
)GetProcAddress( hmod
, "LsaOpenPolicy");
1707 pLsaFreeMemory
= (fnLsaFreeMemory
)GetProcAddress( hmod
, "LsaFreeMemory");
1708 pLsaClose
= (fnLsaClose
)GetProcAddress( hmod
, "LsaClose");
1710 if (pLsaQueryInformationPolicy
&& pLsaOpenPolicy
&& pLsaFreeMemory
&& pLsaClose
)
1714 LSA_OBJECT_ATTRIBUTES object_attributes
;
1716 ZeroMemory(&object_attributes
, sizeof(object_attributes
));
1717 object_attributes
.Length
= sizeof(object_attributes
);
1719 status
= pLsaOpenPolicy( NULL
, &object_attributes
, POLICY_ALL_ACCESS
, &handle
);
1720 ok(status
== STATUS_SUCCESS
|| status
== STATUS_ACCESS_DENIED
,
1721 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status
);
1723 /* try a more restricted access mask if necessary */
1724 if (status
== STATUS_ACCESS_DENIED
) {
1725 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
1726 status
= pLsaOpenPolicy( NULL
, &object_attributes
, POLICY_VIEW_LOCAL_INFORMATION
, &handle
);
1727 ok(status
== STATUS_SUCCESS
, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status
);
1730 if (status
== STATUS_SUCCESS
)
1732 PPOLICY_ACCOUNT_DOMAIN_INFO info
;
1733 status
= pLsaQueryInformationPolicy(handle
, PolicyAccountDomainInformation
, (PVOID
*)&info
);
1734 ok(status
== STATUS_SUCCESS
, "LsaQueryInformationPolicy() failed, returned 0x%08x\n", status
);
1735 if (status
== STATUS_SUCCESS
)
1737 ok(info
->DomainSid
!=0, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) missing SID\n");
1738 if (info
->DomainSid
)
1740 int count
= *GetSidSubAuthorityCount(info
->DomainSid
);
1741 CopySid(GetSidLengthRequired(count
), &max_sid
, info
->DomainSid
);
1742 test_sid_str((PSID
)&max_sid
.sid
);
1743 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_USER_RID_ADMIN
;
1744 max_sid
.sid
.SubAuthorityCount
= count
+ 1;
1745 test_sid_str((PSID
)&max_sid
.sid
);
1746 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_USER_RID_GUEST
;
1747 test_sid_str((PSID
)&max_sid
.sid
);
1748 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_ADMINS
;
1749 test_sid_str((PSID
)&max_sid
.sid
);
1750 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_USERS
;
1751 test_sid_str((PSID
)&max_sid
.sid
);
1752 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_GUESTS
;
1753 test_sid_str((PSID
)&max_sid
.sid
);
1754 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_COMPUTERS
;
1755 test_sid_str((PSID
)&max_sid
.sid
);
1756 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_CONTROLLERS
;
1757 test_sid_str((PSID
)&max_sid
.sid
);
1758 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_CERT_ADMINS
;
1759 test_sid_str((PSID
)&max_sid
.sid
);
1760 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_SCHEMA_ADMINS
;
1761 test_sid_str((PSID
)&max_sid
.sid
);
1762 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_ENTERPRISE_ADMINS
;
1763 test_sid_str((PSID
)&max_sid
.sid
);
1764 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_POLICY_ADMINS
;
1765 test_sid_str((PSID
)&max_sid
.sid
);
1766 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_ALIAS_RID_RAS_SERVERS
;
1767 test_sid_str((PSID
)&max_sid
.sid
);
1768 max_sid
.sid
.SubAuthority
[count
] = 1000; /* first user account */
1769 test_sid_str((PSID
)&max_sid
.sid
);
1772 pLsaFreeMemory((LPVOID
)info
);
1775 status
= pLsaClose(handle
);
1776 ok(status
== STATUS_SUCCESS
, "LsaClose() failed, returned 0x%08x\n", status
);
1782 static BOOL
get_sid_info(PSID psid
, LPSTR
*user
, LPSTR
*dom
)
1784 static CHAR account
[UNLEN
+ 1];
1785 static CHAR domain
[UNLEN
+ 1];
1786 DWORD size
, dom_size
;
1792 size
= dom_size
= UNLEN
+ 1;
1795 SetLastError(0xdeadbeef);
1796 return LookupAccountSidA(NULL
, psid
, account
, &size
, domain
, &dom_size
, &use
);
1799 static void check_wellknown_name(const char* name
, WELL_KNOWN_SID_TYPE result
)
1801 SID_IDENTIFIER_AUTHORITY ident
= { SECURITY_NT_AUTHORITY
};
1803 char wk_sid
[SECURITY_MAX_SID_SIZE
];
1806 DWORD sid_size
, domain_size
;
1807 SID_NAME_USE sid_use
;
1808 LPSTR domain
, account
, sid_domain
, wk_domain
, wk_account
;
1814 ret
= LookupAccountNameA(NULL
, name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
1815 psid
= HeapAlloc(GetProcessHeap(),0,sid_size
);
1816 domain
= HeapAlloc(GetProcessHeap(),0,domain_size
);
1817 ret
= LookupAccountNameA(NULL
, name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
1821 ok(!ret
, " %s Should have failed to lookup account name\n",name
);
1825 AllocateAndInitializeSid(&ident
, 6, SECURITY_NT_NON_UNIQUE
, 12, 23, 34, 45, 56, 0, 0, &domainsid
);
1826 cb
= sizeof(wk_sid
);
1827 if (!pCreateWellKnownSid(result
, domainsid
, wk_sid
, &cb
))
1829 win_skip("SID %i is not available on the system\n",result
);
1833 ret2
= get_sid_info(wk_sid
, &wk_account
, &wk_domain
);
1834 if (!ret2
&& GetLastError() == ERROR_NONE_MAPPED
)
1836 win_skip("CreateWellKnownSid() succeeded but the account '%s' is not present (W2K)\n", name
);
1840 get_sid_info(psid
, &account
, &sid_domain
);
1842 ok(ret
, "Failed to lookup account name %s\n",name
);
1843 ok(sid_size
!= 0, "sid_size was zero\n");
1845 ok(EqualSid(psid
,wk_sid
),"(%s) Sids fail to match well known sid!\n",name
);
1847 ok(!lstrcmp(account
, wk_account
), "Expected %s , got %s\n", account
, wk_account
);
1848 ok(!lstrcmp(domain
, wk_domain
), "Expected %s, got %s\n", wk_domain
, domain
);
1849 ok(sid_use
== SidTypeWellKnownGroup
, "Expected Use (5), got %d\n", sid_use
);
1852 HeapFree(GetProcessHeap(),0,psid
);
1853 HeapFree(GetProcessHeap(),0,domain
);
1856 static void test_LookupAccountName(void)
1858 DWORD sid_size
, domain_size
, user_size
;
1859 DWORD sid_save
, domain_save
;
1860 CHAR user_name
[UNLEN
+ 1];
1861 CHAR computer_name
[UNLEN
+ 1];
1862 SID_NAME_USE sid_use
;
1863 LPSTR domain
, account
, sid_dom
;
1867 /* native crashes if (assuming all other parameters correct):
1869 * - Sid is NULL and cbSid is > 0
1870 * - cbSid or cchReferencedDomainName are NULL
1871 * - ReferencedDomainName is NULL and cchReferencedDomainName is the correct size
1874 user_size
= UNLEN
+ 1;
1875 SetLastError(0xdeadbeef);
1876 ret
= GetUserNameA(user_name
, &user_size
);
1877 if (!ret
&& (GetLastError() == ERROR_NOT_LOGGED_ON
))
1879 /* Probably on win9x where the user used 'Cancel' instead of properly logging in */
1880 skip("Cannot get the user name (win9x and not logged in properly)\n");
1883 ok(ret
, "Failed to get user name : %d\n", GetLastError());
1888 sid_use
= 0xcafebabe;
1889 SetLastError(0xdeadbeef);
1890 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
1891 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
1893 win_skip("LookupAccountNameA is not implemented\n");
1896 ok(!ret
, "Expected 0, got %d\n", ret
);
1897 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1898 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1899 ok(sid_size
!= 0, "Expected non-zero sid size\n");
1900 ok(domain_size
!= 0, "Expected non-zero domain size\n");
1901 ok(sid_use
== 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use
);
1903 sid_save
= sid_size
;
1904 domain_save
= domain_size
;
1906 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
1907 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
1909 /* try valid account name */
1910 ret
= LookupAccountNameA(NULL
, user_name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
1911 get_sid_info(psid
, &account
, &sid_dom
);
1912 ok(ret
, "Failed to lookup account name\n");
1913 ok(sid_size
== GetLengthSid(psid
), "Expected %d, got %d\n", GetLengthSid(psid
), sid_size
);
1916 ok(!lstrcmp(account
, user_name
), "Expected %s, got %s\n", user_name
, account
);
1917 ok(!lstrcmp(domain
, sid_dom
), "Expected %s, got %s\n", sid_dom
, domain
);
1919 ok(domain_size
== domain_save
- 1, "Expected %d, got %d\n", domain_save
- 1, domain_size
);
1920 ok(lstrlen(domain
) == domain_size
, "Expected %d, got %d\n", lstrlen(domain
), domain_size
);
1921 ok(sid_use
== SidTypeUser
, "Expected SidTypeUser (%d), got %d\n", SidTypeUser
, sid_use
);
1922 domain_size
= domain_save
;
1923 sid_size
= sid_save
;
1925 if (PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale())) != LANG_ENGLISH
)
1927 skip("Non-english locale (test with hardcoded 'Everyone')\n");
1931 ret
= LookupAccountNameA(NULL
, "Everyone", psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
1932 get_sid_info(psid
, &account
, &sid_dom
);
1933 ok(ret
, "Failed to lookup account name\n");
1934 ok(sid_size
!= 0, "sid_size was zero\n");
1935 ok(!lstrcmp(account
, "Everyone"), "Expected Everyone, got %s\n", account
);
1936 ok(!lstrcmp(domain
, sid_dom
), "Expected %s, got %s\n", sid_dom
, domain
);
1937 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
1938 ok(lstrlen(domain
) == domain_size
, "Expected %d, got %d\n", lstrlen(domain
), domain_size
);
1939 ok(sid_use
== SidTypeWellKnownGroup
, "Expected SidTypeWellKnownGroup (%d), got %d\n", SidTypeWellKnownGroup
, sid_use
);
1940 domain_size
= domain_save
;
1943 /* NULL Sid with zero sid size */
1944 SetLastError(0xdeadbeef);
1946 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, domain
, &domain_size
, &sid_use
);
1947 ok(!ret
, "Expected 0, got %d\n", ret
);
1948 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1949 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1950 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
1951 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
1953 /* try cchReferencedDomainName - 1 */
1954 SetLastError(0xdeadbeef);
1956 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, domain
, &domain_size
, &sid_use
);
1957 ok(!ret
, "Expected 0, got %d\n", ret
);
1958 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1959 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1960 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
1961 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
1963 /* NULL ReferencedDomainName with zero domain name size */
1964 SetLastError(0xdeadbeef);
1966 ret
= LookupAccountNameA(NULL
, user_name
, psid
, &sid_size
, NULL
, &domain_size
, &sid_use
);
1967 ok(!ret
, "Expected 0, got %d\n", ret
);
1968 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1969 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1970 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
1971 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
1973 HeapFree(GetProcessHeap(), 0, psid
);
1974 HeapFree(GetProcessHeap(), 0, domain
);
1976 /* get sizes for NULL account name */
1979 sid_use
= 0xcafebabe;
1980 SetLastError(0xdeadbeef);
1981 ret
= LookupAccountNameA(NULL
, NULL
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
1982 if (!ret
&& GetLastError() == ERROR_NONE_MAPPED
)
1983 win_skip("NULL account name doesn't work on NT4\n");
1986 ok(!ret
, "Expected 0, got %d\n", ret
);
1987 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1988 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1989 ok(sid_size
!= 0, "Expected non-zero sid size\n");
1990 ok(domain_size
!= 0, "Expected non-zero domain size\n");
1991 ok(sid_use
== 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use
);
1993 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
1994 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
1996 /* try NULL account name */
1997 ret
= LookupAccountNameA(NULL
, NULL
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
1998 get_sid_info(psid
, &account
, &sid_dom
);
1999 ok(ret
, "Failed to lookup account name\n");
2000 /* Using a fixed string will not work on different locales */
2001 ok(!lstrcmp(account
, domain
),
2002 "Got %s for account and %s for domain, these should be the same\n",
2004 ok(sid_use
== SidTypeDomain
, "Expected SidTypeDomain (%d), got %d\n", SidTypeDomain
, sid_use
);
2006 HeapFree(GetProcessHeap(), 0, psid
);
2007 HeapFree(GetProcessHeap(), 0, domain
);
2010 /* try an invalid account name */
2011 SetLastError(0xdeadbeef);
2014 ret
= LookupAccountNameA(NULL
, "oogabooga", NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2015 ok(!ret
, "Expected 0, got %d\n", ret
);
2016 ok(GetLastError() == ERROR_NONE_MAPPED
||
2017 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE
),
2018 "Expected ERROR_NONE_MAPPED, got %d\n", GetLastError());
2019 ok(sid_size
== 0, "Expected 0, got %d\n", sid_size
);
2020 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2022 /* try an invalid system name */
2023 SetLastError(0xdeadbeef);
2026 ret
= LookupAccountNameA("deepthought", NULL
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2027 ok(!ret
, "Expected 0, got %d\n", ret
);
2028 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE
|| GetLastError() == RPC_S_INVALID_NET_ADDR
/* Vista */,
2029 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
2030 ok(sid_size
== 0, "Expected 0, got %d\n", sid_size
);
2031 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2033 /* try with the computer name as the account name */
2034 domain_size
= sizeof(computer_name
);
2035 GetComputerNameA(computer_name
, &domain_size
);
2038 ret
= LookupAccountNameA(NULL
, computer_name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2039 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
||
2040 GetLastError() == ERROR_NONE_MAPPED
/* in a domain */ ||
2041 broken(GetLastError() == ERROR_TRUSTED_DOMAIN_FAILURE
) ||
2042 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE
)),
2043 "LookupAccountNameA failed: %d\n", GetLastError());
2044 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
2046 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
2047 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
2048 ret
= LookupAccountNameA(NULL
, computer_name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2049 ok(ret
, "LookupAccountNameA failed: %d\n", GetLastError());
2050 ok(sid_use
== SidTypeDomain
, "expected SidTypeDomain, got %d\n", sid_use
);
2051 HeapFree(GetProcessHeap(), 0, domain
);
2052 HeapFree(GetProcessHeap(), 0, psid
);
2055 /* Well Known names */
2056 if (!pCreateWellKnownSid
)
2058 win_skip("CreateWellKnownSid not available\n");
2062 if (PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale())) != LANG_ENGLISH
)
2064 skip("Non-english locale (skipping well known name creation tests)\n");
2068 check_wellknown_name("LocalService", WinLocalServiceSid
);
2069 check_wellknown_name("Local Service", WinLocalServiceSid
);
2071 check_wellknown_name("Local Service", 0);
2072 check_wellknown_name("NetworkService", WinNetworkServiceSid
);
2073 check_wellknown_name("Network Service", WinNetworkServiceSid
);
2075 /* example of some names where the spaces are not optional */
2076 check_wellknown_name("Terminal Server User", WinTerminalServerSid
);
2077 check_wellknown_name("TerminalServer User", 0);
2078 check_wellknown_name("TerminalServerUser", 0);
2079 check_wellknown_name("Terminal ServerUser", 0);
2081 check_wellknown_name("enterprise domain controllers",WinEnterpriseControllersSid
);
2082 check_wellknown_name("enterprisedomain controllers", 0);
2083 check_wellknown_name("enterprise domaincontrollers", 0);
2084 check_wellknown_name("enterprisedomaincontrollers", 0);
2086 /* case insensitivity */
2087 check_wellknown_name("lOCAlServICE", WinLocalServiceSid
);
2089 /* fully qualified account names */
2090 check_wellknown_name("NT AUTHORITY\\LocalService", WinLocalServiceSid
);
2091 check_wellknown_name("nt authority\\Network Service", WinNetworkServiceSid
);
2092 check_wellknown_name("nt authority test\\Network Service", 0);
2093 check_wellknown_name("Dummy\\Network Service", 0);
2094 check_wellknown_name("ntauthority\\Network Service", 0);
2097 static void test_security_descriptor(void)
2099 SECURITY_DESCRIPTOR sd
;
2102 BOOL isDefault
, isPresent
, ret
;
2106 SetLastError(0xdeadbeef);
2107 ret
= InitializeSecurityDescriptor(&sd
, SECURITY_DESCRIPTOR_REVISION
);
2108 if (ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2110 win_skip("InitializeSecurityDescriptor is not implemented\n");
2114 ok(GetSecurityDescriptorOwner(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorOwner failed\n");
2115 expect_eq(psid
, NULL
, PSID
, "%p");
2116 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2117 sd
.Control
|= SE_DACL_PRESENT
| SE_SACL_PRESENT
;
2119 SetLastError(0xdeadbeef);
2121 expect_eq(MakeSelfRelativeSD(&sd
, buf
, &size
), FALSE
, BOOL
, "%d");
2122 expect_eq(GetLastError(), ERROR_INSUFFICIENT_BUFFER
, DWORD
, "%u");
2123 ok(size
> 5, "Size not increased\n");
2126 expect_eq(MakeSelfRelativeSD(&sd
, buf
, &size
), TRUE
, BOOL
, "%d");
2127 ok(GetSecurityDescriptorOwner(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorOwner failed\n");
2128 expect_eq(psid
, NULL
, PSID
, "%p");
2129 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2130 ok(GetSecurityDescriptorGroup(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorGroup failed\n");
2131 expect_eq(psid
, NULL
, PSID
, "%p");
2132 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2133 ok(GetSecurityDescriptorDacl(&sd
, &isPresent
, &pacl
, &isDefault
), "GetSecurityDescriptorDacl failed\n");
2134 expect_eq(isPresent
, TRUE
, BOOL
, "%d");
2135 expect_eq(psid
, NULL
, PSID
, "%p");
2136 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2137 ok(GetSecurityDescriptorSacl(&sd
, &isPresent
, &pacl
, &isDefault
), "GetSecurityDescriptorSacl failed\n");
2138 expect_eq(isPresent
, TRUE
, BOOL
, "%d");
2139 expect_eq(psid
, NULL
, PSID
, "%p");
2140 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2144 #define TEST_GRANTED_ACCESS(a,b) test_granted_access(a,b,0,__LINE__)
2145 #define TEST_GRANTED_ACCESS2(a,b,c) test_granted_access(a,b,c,__LINE__)
2146 static void test_granted_access(HANDLE handle
, ACCESS_MASK access
,
2147 ACCESS_MASK alt
, int line
)
2149 OBJECT_BASIC_INFORMATION obj_info
;
2152 if (!pNtQueryObject
)
2154 skip_(__FILE__
, line
)("Not NT platform - skipping tests\n");
2158 status
= pNtQueryObject( handle
, ObjectBasicInformation
, &obj_info
,
2159 sizeof(obj_info
), NULL
);
2160 ok_(__FILE__
, line
)(!status
, "NtQueryObject with err: %08x\n", status
);
2162 ok_(__FILE__
, line
)(obj_info
.GrantedAccess
== access
||
2163 obj_info
.GrantedAccess
== alt
, "Granted access should be 0x%08x "
2164 "or 0x%08x, instead of 0x%08x\n", access
, alt
, obj_info
.GrantedAccess
);
2166 ok_(__FILE__
, line
)(obj_info
.GrantedAccess
== access
, "Granted access should "
2167 "be 0x%08x, instead of 0x%08x\n", access
, obj_info
.GrantedAccess
);
2170 #define CHECK_SET_SECURITY(o,i,e) \
2174 SetLastError( 0xdeadbeef ); \
2175 res = SetKernelObjectSecurity( o, i, SecurityDescriptor ); \
2176 err = GetLastError(); \
2177 if (e == ERROR_SUCCESS) \
2178 ok(res, "SetKernelObjectSecurity failed with %d\n", err); \
2180 ok(!res && err == e, "SetKernelObjectSecurity should have failed " \
2181 "with %s, instead of %d\n", #e, err); \
2184 static void test_process_security(void)
2188 PTOKEN_PRIMARY_GROUP group
;
2189 PSID AdminSid
= NULL
, UsersSid
= NULL
;
2191 SECURITY_DESCRIPTOR
*SecurityDescriptor
= NULL
;
2192 char buffer
[MAX_PATH
];
2193 PROCESS_INFORMATION info
;
2194 STARTUPINFOA startup
;
2195 SECURITY_ATTRIBUTES psa
;
2196 HANDLE token
, event
;
2199 Acl
= HeapAlloc(GetProcessHeap(), 0, 256);
2200 res
= InitializeAcl(Acl
, 256, ACL_REVISION
);
2201 if (!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2203 win_skip("ACLs not implemented - skipping tests\n");
2204 HeapFree(GetProcessHeap(), 0, Acl
);
2207 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
2209 /* get owner from the token we might be running as a user not admin */
2210 res
= OpenProcessToken( GetCurrentProcess(), MAXIMUM_ALLOWED
, &token
);
2211 ok(res
, "OpenProcessToken failed with error %d\n", GetLastError());
2214 HeapFree(GetProcessHeap(), 0, Acl
);
2218 res
= GetTokenInformation( token
, TokenOwner
, NULL
, 0, &size
);
2219 ok(!res
, "Expected failure, got %d\n", res
);
2220 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2221 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2223 owner
= HeapAlloc(GetProcessHeap(), 0, size
);
2224 res
= GetTokenInformation( token
, TokenOwner
, owner
, size
, &size
);
2225 ok(res
, "GetTokenInformation failed with error %d\n", GetLastError());
2226 AdminSid
= ((TOKEN_OWNER
*)owner
)->Owner
;
2228 res
= GetTokenInformation( token
, TokenPrimaryGroup
, NULL
, 0, &size
);
2229 ok(!res
, "Expected failure, got %d\n", res
);
2230 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2231 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2233 group
= HeapAlloc(GetProcessHeap(), 0, size
);
2234 res
= GetTokenInformation( token
, TokenPrimaryGroup
, group
, size
, &size
);
2235 ok(res
, "GetTokenInformation failed with error %d\n", GetLastError());
2236 UsersSid
= ((TOKEN_PRIMARY_GROUP
*)group
)->PrimaryGroup
;
2238 CloseHandle( token
);
2241 HeapFree(GetProcessHeap(), 0, group
);
2242 HeapFree(GetProcessHeap(), 0, owner
);
2243 HeapFree(GetProcessHeap(), 0, Acl
);
2247 res
= AddAccessDeniedAce(Acl
, ACL_REVISION
, PROCESS_VM_READ
, AdminSid
);
2248 ok(res
, "AddAccessDeniedAce failed with error %d\n", GetLastError());
2249 res
= AddAccessAllowedAce(Acl
, ACL_REVISION
, PROCESS_ALL_ACCESS
, AdminSid
);
2250 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2252 SecurityDescriptor
= HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
2253 res
= InitializeSecurityDescriptor(SecurityDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
2254 ok(res
, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
2256 event
= CreateEvent( NULL
, TRUE
, TRUE
, "test_event" );
2257 ok(event
!= NULL
, "CreateEvent %d\n", GetLastError());
2259 SecurityDescriptor
->Revision
= 0;
2260 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_UNKNOWN_REVISION
);
2261 SecurityDescriptor
->Revision
= SECURITY_DESCRIPTOR_REVISION
;
2263 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_INVALID_SECURITY_DESCR
);
2264 CHECK_SET_SECURITY( event
, GROUP_SECURITY_INFORMATION
, ERROR_INVALID_SECURITY_DESCR
);
2265 CHECK_SET_SECURITY( event
, SACL_SECURITY_INFORMATION
, ERROR_ACCESS_DENIED
);
2266 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2267 /* NULL DACL is valid and means default DACL from token */
2268 SecurityDescriptor
->Control
|= SE_DACL_PRESENT
;
2269 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2271 /* Set owner and group and dacl */
2272 res
= SetSecurityDescriptorOwner(SecurityDescriptor
, AdminSid
, FALSE
);
2273 ok(res
, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
2274 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2275 res
= SetSecurityDescriptorGroup(SecurityDescriptor
, UsersSid
, FALSE
);
2276 ok(res
, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2277 CHECK_SET_SECURITY( event
, GROUP_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2278 res
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
2279 ok(res
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
2280 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2282 sprintf(buffer
, "%s tests/security.c test", myARGV
[0]);
2283 memset(&startup
, 0, sizeof(startup
));
2284 startup
.cb
= sizeof(startup
);
2285 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
2286 startup
.wShowWindow
= SW_SHOWNORMAL
;
2288 psa
.nLength
= sizeof(psa
);
2289 psa
.lpSecurityDescriptor
= SecurityDescriptor
;
2290 psa
.bInheritHandle
= TRUE
;
2292 /* Doesn't matter what ACL say we should get full access for ourselves */
2293 ok(CreateProcessA( NULL
, buffer
, &psa
, NULL
, FALSE
, 0, NULL
, NULL
, &startup
, &info
),
2294 "CreateProcess with err:%d\n", GetLastError());
2295 TEST_GRANTED_ACCESS2( info
.hProcess
, PROCESS_ALL_ACCESS
,
2296 STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
);
2297 winetest_wait_child_process( info
.hProcess
);
2299 CloseHandle( info
.hProcess
);
2300 CloseHandle( info
.hThread
);
2301 CloseHandle( event
);
2302 HeapFree(GetProcessHeap(), 0, group
);
2303 HeapFree(GetProcessHeap(), 0, owner
);
2304 HeapFree(GetProcessHeap(), 0, Acl
);
2305 HeapFree(GetProcessHeap(), 0, SecurityDescriptor
);
2308 static void test_process_security_child(void)
2310 HANDLE handle
, handle1
;
2314 handle
= OpenProcess( PROCESS_TERMINATE
, FALSE
, GetCurrentProcessId() );
2315 ok(handle
!= NULL
, "OpenProcess(PROCESS_TERMINATE) with err:%d\n", GetLastError());
2316 TEST_GRANTED_ACCESS( handle
, PROCESS_TERMINATE
);
2318 ok(DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2319 &handle1
, 0, TRUE
, DUPLICATE_SAME_ACCESS
),
2320 "duplicating handle err:%d\n", GetLastError());
2321 TEST_GRANTED_ACCESS( handle1
, PROCESS_TERMINATE
);
2323 CloseHandle( handle1
);
2325 SetLastError( 0xdeadbeef );
2326 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2327 &handle1
, PROCESS_ALL_ACCESS
, TRUE
, 0 );
2328 err
= GetLastError();
2330 ok(!ret
&& err
== ERROR_ACCESS_DENIED
, "duplicating handle should have failed "
2331 "with STATUS_ACCESS_DENIED, instead of err:%d\n", err
);
2333 CloseHandle( handle
);
2335 /* These two should fail - they are denied by ACL */
2336 handle
= OpenProcess( PROCESS_VM_READ
, FALSE
, GetCurrentProcessId() );
2338 ok(handle
== NULL
, "OpenProcess(PROCESS_VM_READ) should have failed\n");
2339 handle
= OpenProcess( PROCESS_ALL_ACCESS
, FALSE
, GetCurrentProcessId() );
2341 ok(handle
== NULL
, "OpenProcess(PROCESS_ALL_ACCESS) should have failed\n");
2343 /* Documented privilege elevation */
2344 ok(DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2345 &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
),
2346 "duplicating handle err:%d\n", GetLastError());
2347 TEST_GRANTED_ACCESS2( handle
, PROCESS_ALL_ACCESS
,
2348 STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
);
2350 CloseHandle( handle
);
2352 /* Same only explicitly asking for all access rights */
2353 ok(DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2354 &handle
, PROCESS_ALL_ACCESS
, TRUE
, 0 ),
2355 "duplicating handle err:%d\n", GetLastError());
2356 TEST_GRANTED_ACCESS2( handle
, PROCESS_ALL_ACCESS
,
2357 PROCESS_ALL_ACCESS
| PROCESS_QUERY_LIMITED_INFORMATION
);
2358 ok(DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2359 &handle1
, PROCESS_VM_READ
, TRUE
, 0 ),
2360 "duplicating handle err:%d\n", GetLastError());
2361 TEST_GRANTED_ACCESS( handle1
, PROCESS_VM_READ
);
2362 CloseHandle( handle1
);
2363 CloseHandle( handle
);
2366 static void test_impersonation_level(void)
2368 HANDLE Token
, ProcessToken
;
2371 TOKEN_PRIVILEGES
*Privileges
;
2373 PRIVILEGE_SET
*PrivilegeSet
;
2379 pDuplicateTokenEx
= (fnDuplicateTokenEx
) GetProcAddress(hmod
, "DuplicateTokenEx");
2380 if( !pDuplicateTokenEx
) {
2381 win_skip("DuplicateTokenEx is not available\n");
2384 SetLastError(0xdeadbeef);
2385 ret
= ImpersonateSelf(SecurityAnonymous
);
2386 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
2388 win_skip("ImpersonateSelf is not implemented\n");
2391 ok(ret
, "ImpersonateSelf(SecurityAnonymous) failed with error %d\n", GetLastError());
2392 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2393 ok(!ret
, "OpenThreadToken should have failed\n");
2394 error
= GetLastError();
2395 ok(error
== ERROR_CANT_OPEN_ANONYMOUS
, "OpenThreadToken on anonymous token should have returned ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error
);
2396 /* can't perform access check when opening object against an anonymous impersonation token */
2398 error
= RegOpenKeyEx(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2399 ok(error
== ERROR_INVALID_HANDLE
|| error
== ERROR_CANT_OPEN_ANONYMOUS
,
2400 "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE or ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error
);
2404 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
, &ProcessToken
);
2405 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
2407 ret
= pDuplicateTokenEx(ProcessToken
,
2408 TOKEN_QUERY
| TOKEN_DUPLICATE
| TOKEN_IMPERSONATE
, NULL
,
2409 SecurityAnonymous
, TokenImpersonation
, &Token
);
2410 ok(ret
, "DuplicateTokenEx failed with error %d\n", GetLastError());
2411 /* can't increase the impersonation level */
2412 ret
= DuplicateToken(Token
, SecurityIdentification
, &Token2
);
2413 error
= GetLastError();
2414 ok(!ret
&& error
== ERROR_BAD_IMPERSONATION_LEVEL
,
2415 "Duplicating a token and increasing the impersonation level should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2416 /* we can query anything from an anonymous token, including the user */
2417 ret
= GetTokenInformation(Token
, TokenUser
, NULL
, 0, &Size
);
2418 error
= GetLastError();
2419 ok(!ret
&& error
== ERROR_INSUFFICIENT_BUFFER
, "GetTokenInformation(TokenUser) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error
);
2420 User
= HeapAlloc(GetProcessHeap(), 0, Size
);
2421 ret
= GetTokenInformation(Token
, TokenUser
, User
, Size
, &Size
);
2422 ok(ret
, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
2423 HeapFree(GetProcessHeap(), 0, User
);
2425 /* PrivilegeCheck fails with SecurityAnonymous level */
2426 ret
= GetTokenInformation(Token
, TokenPrivileges
, NULL
, 0, &Size
);
2427 error
= GetLastError();
2428 ok(!ret
&& error
== ERROR_INSUFFICIENT_BUFFER
, "GetTokenInformation(TokenPrivileges) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error
);
2429 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
2430 ret
= GetTokenInformation(Token
, TokenPrivileges
, Privileges
, Size
, &Size
);
2431 ok(ret
, "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
2433 PrivilegeSet
= HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PRIVILEGE_SET
, Privilege
[Privileges
->PrivilegeCount
]));
2434 PrivilegeSet
->PrivilegeCount
= Privileges
->PrivilegeCount
;
2435 memcpy(PrivilegeSet
->Privilege
, Privileges
->Privileges
, PrivilegeSet
->PrivilegeCount
* sizeof(PrivilegeSet
->Privilege
[0]));
2436 PrivilegeSet
->Control
= PRIVILEGE_SET_ALL_NECESSARY
;
2437 HeapFree(GetProcessHeap(), 0, Privileges
);
2439 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2440 error
= GetLastError();
2441 ok(!ret
&& error
== ERROR_BAD_IMPERSONATION_LEVEL
, "PrivilegeCheck for SecurityAnonymous token should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2445 ret
= ImpersonateSelf(SecurityIdentification
);
2446 ok(ret
, "ImpersonateSelf(SecurityIdentification) failed with error %d\n", GetLastError());
2447 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2448 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
2450 /* can't perform access check when opening object against an identification impersonation token */
2451 error
= RegOpenKeyEx(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2453 ok(error
== ERROR_INVALID_HANDLE
|| error
== ERROR_BAD_IMPERSONATION_LEVEL
,
2454 "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE or ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2456 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2457 ok(ret
, "PrivilegeCheck for SecurityIdentification failed with error %d\n", GetLastError());
2461 ret
= ImpersonateSelf(SecurityImpersonation
);
2462 ok(ret
, "ImpersonateSelf(SecurityImpersonation) failed with error %d\n", GetLastError());
2463 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2464 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
2465 error
= RegOpenKeyEx(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2466 ok(error
== ERROR_SUCCESS
, "RegOpenKeyEx should have succeeded instead of failing with %d\n", error
);
2468 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2469 ok(ret
, "PrivilegeCheck for SecurityImpersonation failed with error %d\n", GetLastError());
2473 CloseHandle(ProcessToken
);
2475 HeapFree(GetProcessHeap(), 0, PrivilegeSet
);
2478 static void test_SetEntriesInAcl(void)
2481 PSID EveryoneSid
= NULL
, UsersSid
= NULL
;
2482 PACL OldAcl
= NULL
, NewAcl
;
2483 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
2484 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
2485 EXPLICIT_ACCESSW ExplicitAccess
;
2486 static const WCHAR wszEveryone
[] = {'E','v','e','r','y','o','n','e',0};
2487 static const WCHAR wszCurrentUser
[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
2489 if (!pSetEntriesInAclW
)
2491 win_skip("SetEntriesInAclW is not available\n");
2495 NewAcl
= (PACL
)0xdeadbeef;
2496 res
= pSetEntriesInAclW(0, NULL
, NULL
, &NewAcl
);
2497 if(res
== ERROR_CALL_NOT_IMPLEMENTED
)
2499 win_skip("SetEntriesInAclW is not implemented\n");
2502 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2503 ok(NewAcl
== NULL
||
2504 broken(NewAcl
!= NULL
), /* NT4 */
2505 "NewAcl=%p, expected NULL\n", NewAcl
);
2508 OldAcl
= HeapAlloc(GetProcessHeap(), 0, 256);
2509 res
= InitializeAcl(OldAcl
, 256, ACL_REVISION
);
2510 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2512 win_skip("ACLs not implemented - skipping tests\n");
2513 HeapFree(GetProcessHeap(), 0, OldAcl
);
2516 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
2518 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
2519 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2521 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
2522 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
2523 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2525 res
= AddAccessAllowedAce(OldAcl
, ACL_REVISION
, KEY_READ
, UsersSid
);
2526 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2528 ExplicitAccess
.grfAccessPermissions
= KEY_WRITE
;
2529 ExplicitAccess
.grfAccessMode
= GRANT_ACCESS
;
2530 ExplicitAccess
.grfInheritance
= NO_INHERITANCE
;
2531 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_WELL_KNOWN_GROUP
;
2532 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2533 ExplicitAccess
.Trustee
.ptstrName
= EveryoneSid
;
2534 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= 0xDEADBEEF;
2535 ExplicitAccess
.Trustee
.pMultipleTrustee
= (PVOID
)0xDEADBEEF;
2536 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2537 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2538 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2541 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2542 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
2543 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2544 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2545 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2546 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2549 if (PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale())) != LANG_ENGLISH
)
2551 skip("Non-english locale (test with hardcoded 'Everyone')\n");
2555 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_USER
;
2556 ExplicitAccess
.Trustee
.ptstrName
= (LPWSTR
)wszEveryone
;
2557 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2558 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2559 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2562 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_BAD_FORM
;
2563 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2564 ok(res
== ERROR_INVALID_PARAMETER
||
2565 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2566 "SetEntriesInAclW failed: %u\n", res
);
2567 ok(NewAcl
== NULL
||
2568 broken(NewAcl
!= NULL
), /* NT4 */
2569 "returned acl wasn't NULL: %p\n", NewAcl
);
2571 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_USER
;
2572 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= TRUSTEE_IS_IMPERSONATE
;
2573 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2574 ok(res
== ERROR_INVALID_PARAMETER
||
2575 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2576 "SetEntriesInAclW failed: %u\n", res
);
2577 ok(NewAcl
== NULL
||
2578 broken(NewAcl
!= NULL
), /* NT4 */
2579 "returned acl wasn't NULL: %p\n", NewAcl
);
2581 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2582 ExplicitAccess
.grfAccessMode
= SET_ACCESS
;
2583 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2584 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2585 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2589 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_USER
;
2590 ExplicitAccess
.Trustee
.ptstrName
= (LPWSTR
)wszCurrentUser
;
2591 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2592 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2593 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2596 ExplicitAccess
.grfAccessMode
= REVOKE_ACCESS
;
2597 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2598 ExplicitAccess
.Trustee
.ptstrName
= UsersSid
;
2599 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2600 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2601 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2604 LocalFree(UsersSid
);
2605 LocalFree(EveryoneSid
);
2606 HeapFree(GetProcessHeap(), 0, OldAcl
);
2609 static void test_GetNamedSecurityInfoA(void)
2611 PSECURITY_DESCRIPTOR pSecDesc
;
2613 SECURITY_DESCRIPTOR_CONTROL control
;
2616 BOOL owner_defaulted
;
2617 BOOL group_defaulted
;
2620 CHAR windows_dir
[MAX_PATH
];
2622 if (!pGetNamedSecurityInfoA
)
2624 win_skip("GetNamedSecurityInfoA is not available\n");
2628 ret
= GetWindowsDirectoryA(windows_dir
, MAX_PATH
);
2629 ok(ret
, "GetWindowsDirectory failed with error %d\n", GetLastError());
2631 SetLastError(0xdeadbeef);
2632 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,
2633 OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
2634 NULL
, NULL
, NULL
, NULL
, &pSecDesc
);
2635 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
2637 win_skip("GetNamedSecurityInfoA is not implemented\n");
2640 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
2642 ret
= GetSecurityDescriptorControl(pSecDesc
, &control
, &revision
);
2643 ok(ret
, "GetSecurityDescriptorControl failed with error %d\n", GetLastError());
2644 ok((control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == (SE_SELF_RELATIVE
|SE_DACL_PRESENT
) ||
2645 broken((control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == SE_DACL_PRESENT
), /* NT4 */
2646 "control (0x%x) doesn't have (SE_SELF_RELATIVE|SE_DACL_PRESENT) flags set\n", control
);
2647 ok(revision
== SECURITY_DESCRIPTOR_REVISION1
, "revision was %d instead of 1\n", revision
);
2648 ret
= GetSecurityDescriptorOwner(pSecDesc
, &owner
, &owner_defaulted
);
2649 ok(ret
, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
2650 ok(owner
!= NULL
, "owner should not be NULL\n");
2651 ret
= GetSecurityDescriptorGroup(pSecDesc
, &group
, &group_defaulted
);
2652 ok(ret
, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2653 ok(group
!= NULL
, "group should not be NULL\n");
2654 LocalFree(pSecDesc
);
2657 static void test_ConvertStringSecurityDescriptor(void)
2660 PSECURITY_DESCRIPTOR pSD
;
2661 static const WCHAR Blank
[] = { 0 };
2665 const char *sidstring
;
2671 { "D:(A;;GA;;;WD)", 0xdeadbeef, FALSE
, ERROR_UNKNOWN_REVISION
},
2672 /* test ACE string type */
2673 { "D:(A;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
2674 { "D:(D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
2675 { "ERROR:(D;;GA;;;WD)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_PARAMETER
},
2676 /* test ACE string access rights */
2677 { "D:(A;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
2678 { "D:(A;;GRGWGX;;;WD)", SDDL_REVISION_1
, TRUE
},
2679 { "D:(A;;RCSDWDWO;;;WD)", SDDL_REVISION_1
, TRUE
},
2680 { "D:(A;;RPWPCCDCLCSWLODTCR;;;WD)", SDDL_REVISION_1
, TRUE
},
2681 { "D:(A;;FAFRFWFX;;;WD)", SDDL_REVISION_1
, TRUE
},
2682 { "D:(A;;KAKRKWKX;;;WD)", SDDL_REVISION_1
, TRUE
},
2683 { "D:(A;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1
, TRUE
},
2684 { "S:(AU;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1
, TRUE
},
2685 /* test ACE string access right error case */
2686 { "D:(A;;ROB;;;WD)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_ACL
},
2687 /* test behaviour with empty strings */
2688 { "", SDDL_REVISION_1
, TRUE
},
2689 /* test ACE string SID */
2690 { "D:(D;;GA;;;S-1-0-0)", SDDL_REVISION_1
, TRUE
}
2693 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA
)
2695 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
2699 for (i
= 0; i
< sizeof(cssd
)/sizeof(cssd
[0]); i
++)
2703 SetLastError(0xdeadbeef);
2704 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
2705 cssd
[i
].sidstring
, cssd
[i
].revision
, &pSD
, NULL
);
2706 GLE
= GetLastError();
2707 ok(ret
== cssd
[i
].ret
, "(%02d) Expected %s (%d)\n", i
, cssd
[i
].ret
? "success" : "failure", GLE
);
2709 ok(GLE
== cssd
[i
].GLE
, "(%02d) Expected %d, got %d\n", i
, cssd
[i
].GLE
, GLE
);
2714 /* test behaviour with NULL parameters */
2715 SetLastError(0xdeadbeef);
2716 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
2717 NULL
, 0xdeadbeef, &pSD
, NULL
);
2719 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
2720 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2723 SetLastError(0xdeadbeef);
2724 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorW(
2725 NULL
, 0xdeadbeef, &pSD
, NULL
);
2726 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
2727 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2730 SetLastError(0xdeadbeef);
2731 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
2732 "D:(A;;ROB;;;WD)", 0xdeadbeef, NULL
, NULL
);
2733 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
2734 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2737 SetLastError(0xdeadbeef);
2738 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
2739 "D:(A;;ROB;;;WD)", SDDL_REVISION_1
, NULL
, NULL
);
2740 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
2741 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2744 /* test behaviour with empty strings */
2745 SetLastError(0xdeadbeef);
2746 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorW(
2747 Blank
, SDDL_REVISION_1
, &pSD
, NULL
);
2748 ok(ret
, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2750 /* test ACE string SID */
2751 SetLastError(0xdeadbeef);
2752 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
2753 "D:(D;;GA;;;Nonexistent account)", SDDL_REVISION_1
, &pSD
, NULL
);
2754 ok(!ret
, "Expected failure, got %d\n", ret
);
2755 ok(GetLastError() == ERROR_INVALID_ACL
|| GetLastError() == ERROR_INVALID_SID
,
2756 "Expected ERROR_INVALID_ACL or ERROR_INVALID_SID, got %d\n", GetLastError());
2759 static void test_ConvertSecurityDescriptorToString(void)
2761 SECURITY_DESCRIPTOR desc
;
2762 SECURITY_INFORMATION sec_info
= OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
|SACL_SECURITY_INFORMATION
;
2771 if (!pConvertSecurityDescriptorToStringSecurityDescriptorA
)
2773 win_skip("ConvertSecurityDescriptorToStringSecurityDescriptor is not available\n");
2776 if (!pCreateWellKnownSid
)
2778 win_skip("CreateWellKnownSid is not available\n");
2782 /* It seems Windows XP adds an extra character to the length of the string for each ACE in an ACL. We
2783 * don't replicate this feature so we only test len >= strlen+1. */
2784 #define CHECK_RESULT_AND_FREE(exp_str) \
2785 ok(strcmp(string, (exp_str)) == 0, "String mismatch (expected \"%s\", got \"%s\")\n", (exp_str), string); \
2786 ok(len >= (lstrlen(exp_str) + 1), "Length mismatch (expected %d, got %d)\n", lstrlen(exp_str) + 1, len); \
2789 #define CHECK_ONE_OF_AND_FREE(exp_str1, exp_str2) \
2790 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); \
2791 ok(len >= (strlen(string) + 1), "Length mismatch (expected %d, got %d)\n", lstrlen(string) + 1, len); \
2794 InitializeSecurityDescriptor(&desc
, SECURITY_DESCRIPTOR_REVISION
);
2795 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
2796 CHECK_RESULT_AND_FREE("");
2799 pCreateWellKnownSid(WinLocalSid
, NULL
, sid_buf
, &size
);
2800 SetSecurityDescriptorOwner(&desc
, sid_buf
, FALSE
);
2801 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
2802 CHECK_RESULT_AND_FREE("O:S-1-2-0");
2804 SetSecurityDescriptorOwner(&desc
, sid_buf
, TRUE
);
2805 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
2806 CHECK_RESULT_AND_FREE("O:S-1-2-0");
2808 size
= sizeof(sid_buf
);
2809 pCreateWellKnownSid(WinLocalSystemSid
, NULL
, sid_buf
, &size
);
2810 SetSecurityDescriptorOwner(&desc
, sid_buf
, TRUE
);
2811 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
2812 CHECK_RESULT_AND_FREE("O:SY");
2814 pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid
);
2815 SetSecurityDescriptorGroup(&desc
, psid
, TRUE
);
2816 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
2817 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576");
2819 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, GROUP_SECURITY_INFORMATION
, &string
, &len
), "Conversion failed\n");
2820 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
2822 pacl
= (PACL
)acl_buf
;
2823 InitializeAcl(pacl
, sizeof(acl_buf
), ACL_REVISION
);
2824 SetSecurityDescriptorDacl(&desc
, TRUE
, pacl
, TRUE
);
2825 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
2826 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
2828 SetSecurityDescriptorDacl(&desc
, TRUE
, pacl
, FALSE
);
2829 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
2830 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
2832 pConvertStringSidToSidA("S-1-5-6", &psid2
);
2833 pAddAccessAllowedAceEx(pacl
, ACL_REVISION
, NO_PROPAGATE_INHERIT_ACE
, 0xf0000000, psid2
);
2834 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
2835 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)");
2837 pAddAccessAllowedAceEx(pacl
, ACL_REVISION
, INHERIT_ONLY_ACE
|INHERITED_ACE
, 0x00000003, psid2
);
2838 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
2839 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)");
2841 pAddAccessDeniedAceEx(pacl
, ACL_REVISION
, OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
, 0xffffffff, psid
);
2842 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
2843 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)");
2846 pacl
= (PACL
)acl_buf
;
2847 InitializeAcl(pacl
, sizeof(acl_buf
), ACL_REVISION
);
2848 SetSecurityDescriptorSacl(&desc
, TRUE
, pacl
, FALSE
);
2849 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
2850 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:");
2852 /* fails in win2k */
2853 SetSecurityDescriptorDacl(&desc
, TRUE
, NULL
, FALSE
);
2854 pAddAuditAccessAceEx(pacl
, ACL_REVISION
, VALID_INHERIT_FLAGS
, KEY_READ
|KEY_WRITE
, psid2
, TRUE
, TRUE
);
2855 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
))
2857 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)", /* XP */
2858 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)" /* Vista */);
2861 /* fails in win2k */
2862 pAddAuditAccessAceEx(pacl
, ACL_REVISION
, NO_PROPAGATE_INHERIT_ACE
, FILE_GENERIC_READ
|FILE_GENERIC_WRITE
, psid2
, TRUE
, FALSE
);
2863 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
))
2865 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", /* XP */
2866 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)" /* Vista */);
2870 static void test_SetSecurityDescriptorControl (PSECURITY_DESCRIPTOR sec
)
2872 SECURITY_DESCRIPTOR_CONTROL ref
;
2873 SECURITY_DESCRIPTOR_CONTROL test
;
2875 SECURITY_DESCRIPTOR_CONTROL
const mutable
2876 = SE_DACL_AUTO_INHERIT_REQ
| SE_SACL_AUTO_INHERIT_REQ
2877 | SE_DACL_AUTO_INHERITED
| SE_SACL_AUTO_INHERITED
2878 | SE_DACL_PROTECTED
| SE_SACL_PROTECTED
2879 | 0x00000040 | 0x00000080 /* not defined in winnt.h */
2881 SECURITY_DESCRIPTOR_CONTROL
const immutable
2882 = SE_OWNER_DEFAULTED
| SE_GROUP_DEFAULTED
2883 | SE_DACL_PRESENT
| SE_DACL_DEFAULTED
2884 | SE_SACL_PRESENT
| SE_SACL_DEFAULTED
2885 | SE_RM_CONTROL_VALID
| SE_SELF_RELATIVE
2890 LPCSTR fmt
= "Expected error %s, got %u\n";
2892 GetSecurityDescriptorControl (sec
, &ref
, &dwRevision
);
2894 /* The mutable bits are mutable regardless of the truth of
2895 SE_DACL_PRESENT and/or SE_SACL_PRESENT */
2897 /* Check call barfs if any bit-of-interest is immutable */
2898 for (bit
= 0; bit
< 16; ++bit
)
2900 SECURITY_DESCRIPTOR_CONTROL
const bitOfInterest
= 1 << bit
;
2901 SECURITY_DESCRIPTOR_CONTROL setOrClear
= ref
& bitOfInterest
;
2903 SECURITY_DESCRIPTOR_CONTROL ctrl
;
2905 DWORD dwExpect
= (bitOfInterest
& immutable
)
2906 ? ERROR_INVALID_PARAMETER
: 0xbebecaca;
2907 LPCSTR strExpect
= (bitOfInterest
& immutable
)
2908 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
2910 ctrl
= (bitOfInterest
& mutable) ? ref
+ bitOfInterest
: ref
;
2911 setOrClear
^= bitOfInterest
;
2912 SetLastError (0xbebecaca);
2913 pSetSecurityDescriptorControl (sec
, bitOfInterest
, setOrClear
);
2914 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
2915 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
2916 expect_eq(test
, ctrl
, int, "%x");
2919 setOrClear
^= bitOfInterest
;
2920 SetLastError (0xbebecaca);
2921 pSetSecurityDescriptorControl (sec
, bitOfInterest
, setOrClear
);
2922 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
2923 GetSecurityDescriptorControl (sec
, &test
, &dwRevision
);
2924 expect_eq(test
, ref
, int, "%x");
2927 /* Check call barfs if any bit-to-set is immutable
2928 even when not a bit-of-interest */
2929 for (bit
= 0; bit
< 16; ++bit
)
2931 SECURITY_DESCRIPTOR_CONTROL
const bitsOfInterest
= mutable;
2932 SECURITY_DESCRIPTOR_CONTROL setOrClear
= ref
& bitsOfInterest
;
2934 SECURITY_DESCRIPTOR_CONTROL ctrl
;
2936 DWORD dwExpect
= ((1 << bit
) & immutable
)
2937 ? ERROR_INVALID_PARAMETER
: 0xbebecaca;
2938 LPCSTR strExpect
= ((1 << bit
) & immutable
)
2939 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
2941 ctrl
= ((1 << bit
) & immutable
) ? test
: ref
| mutable;
2942 setOrClear
^= bitsOfInterest
;
2943 SetLastError (0xbebecaca);
2944 pSetSecurityDescriptorControl (sec
, bitsOfInterest
, setOrClear
| (1 << bit
));
2945 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
2946 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
2947 expect_eq(test
, ctrl
, int, "%x");
2949 ctrl
= ((1 << bit
) & immutable
) ? test
: ref
| (1 << bit
);
2950 setOrClear
^= bitsOfInterest
;
2951 SetLastError (0xbebecaca);
2952 pSetSecurityDescriptorControl (sec
, bitsOfInterest
, setOrClear
| (1 << bit
));
2953 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
2954 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
2955 expect_eq(test
, ctrl
, int, "%x");
2959 static void test_PrivateObjectSecurity(void)
2961 SECURITY_INFORMATION sec_info
= OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
|SACL_SECURITY_INFORMATION
;
2962 SECURITY_DESCRIPTOR_CONTROL ctrl
;
2963 PSECURITY_DESCRIPTOR sec
;
2969 PSECURITY_DESCRIPTOR buf
;
2971 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA
)
2973 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
2977 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
2979 "G:S-1-5-21-93476-23408-4576"
2980 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)"
2981 "(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
2982 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
2983 SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
2985 test_SetSecurityDescriptorControl(sec
);
2989 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
2991 "G:S-1-5-21-93476-23408-4576",
2992 SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
2994 test_SetSecurityDescriptorControl(sec
);
2998 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3000 "G:S-1-5-21-93476-23408-4576"
3001 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3002 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
3003 buf
= HeapAlloc(GetProcessHeap(), 0, dwDescSize
);
3004 pSetSecurityDescriptorControl(sec
, SE_DACL_PROTECTED
, SE_DACL_PROTECTED
);
3005 GetSecurityDescriptorControl(sec
, &ctrl
, &dwRevision
);
3006 expect_eq(ctrl
, 0x9014, int, "%x");
3008 ok(GetPrivateObjectSecurity(sec
, GROUP_SECURITY_INFORMATION
, buf
, dwDescSize
, &retSize
),
3009 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3010 ok(retSize
<= dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
3011 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3012 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
3013 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
3014 expect_eq(ctrl
, 0x8000, int, "%x");
3016 ok(GetPrivateObjectSecurity(sec
, GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, buf
, dwDescSize
, &retSize
),
3017 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3018 ok(retSize
<= dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
3019 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed err=%u\n", GetLastError());
3020 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)",
3021 "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 */
3022 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
3023 expect_eq(ctrl
& (~ SE_DACL_PROTECTED
), 0x8004, int, "%x");
3025 ok(GetPrivateObjectSecurity(sec
, sec_info
, buf
, dwDescSize
, &retSize
),
3026 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3027 ok(retSize
== dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
3028 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3029 CHECK_ONE_OF_AND_FREE("O:SY"
3030 "G:S-1-5-21-93476-23408-4576"
3031 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3032 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
3034 "G:S-1-5-21-93476-23408-4576"
3035 "D:P(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3036 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)"); /* Win7 */
3037 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
3038 expect_eq(ctrl
& (~ SE_DACL_PROTECTED
), 0x8014, int, "%x");
3040 SetLastError(0xdeadbeef);
3041 ok(GetPrivateObjectSecurity(sec
, sec_info
, buf
, 5, &retSize
) == FALSE
, "GetPrivateObjectSecurity should have failed\n");
3042 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Expected error ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
3045 HeapFree(GetProcessHeap(), 0, buf
);
3047 #undef CHECK_RESULT_AND_FREE
3048 #undef CHECK_ONE_OF_AND_FREE
3050 static void test_acls(void)
3053 PACL pAcl
= (PACL
)buffer
;
3056 SetLastError(0xdeadbeef);
3057 ret
= InitializeAcl(pAcl
, sizeof(ACL
) - 1, ACL_REVISION
);
3058 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
3060 win_skip("InitializeAcl is not implemented\n");
3064 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "InitializeAcl with too small a buffer should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
3066 SetLastError(0xdeadbeef);
3067 ret
= InitializeAcl(pAcl
, 0xffffffff, ACL_REVISION
);
3068 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl with too large a buffer should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3070 SetLastError(0xdeadbeef);
3071 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION1
);
3072 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl(ACL_REVISION1) should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3074 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION2
);
3075 ok(ret
, "InitializeAcl(ACL_REVISION2) failed with error %d\n", GetLastError());
3077 ret
= IsValidAcl(pAcl
);
3078 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
3080 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION3
);
3081 ok(ret
, "InitializeAcl(ACL_REVISION3) failed with error %d\n", GetLastError());
3083 ret
= IsValidAcl(pAcl
);
3084 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
3086 SetLastError(0xdeadbeef);
3087 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION4
);
3088 if (GetLastError() != ERROR_INVALID_PARAMETER
)
3090 ok(ret
, "InitializeAcl(ACL_REVISION4) failed with error %d\n", GetLastError());
3092 ret
= IsValidAcl(pAcl
);
3093 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
3096 win_skip("ACL_REVISION4 is not implemented on NT4\n");
3098 SetLastError(0xdeadbeef);
3099 ret
= InitializeAcl(pAcl
, sizeof(buffer
), -1);
3100 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl(-1) failed with error %d\n", GetLastError());
3103 static void test_GetSecurityInfo(void)
3106 PSECURITY_DESCRIPTOR sd
;
3111 if (!pGetSecurityInfo
)
3113 win_skip("GetSecurityInfo is not available\n");
3117 /* Create something. Files have lots of associated security info. */
3118 obj
= CreateFile(myARGV
[0], GENERIC_READ
, FILE_SHARE_READ
, NULL
,
3119 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3120 if (obj
== INVALID_HANDLE_VALUE
)
3122 skip("Couldn't create an object for GetSecurityInfo test\n");
3126 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
,
3127 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
3128 &owner
, &group
, &dacl
, NULL
, &sd
);
3129 if (ret
== ERROR_CALL_NOT_IMPLEMENTED
)
3131 win_skip("GetSecurityInfo is not implemented\n");
3135 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
3136 ok(sd
!= NULL
, "GetSecurityInfo\n");
3137 ok(owner
!= NULL
, "GetSecurityInfo\n");
3138 ok(group
!= NULL
, "GetSecurityInfo\n");
3139 ok(dacl
!= NULL
, "GetSecurityInfo\n");
3140 ok(IsValidAcl(dacl
), "GetSecurityInfo\n");
3144 if (!pCreateWellKnownSid
)
3146 win_skip("NULL parameter test would crash on NT4\n");
3151 /* If we don't ask for the security descriptor, Windows will still give us
3152 the other stuff, leaving us no way to free it. */
3153 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
,
3154 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
3155 &owner
, &group
, &dacl
, NULL
, NULL
);
3156 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
3157 ok(owner
!= NULL
, "GetSecurityInfo\n");
3158 ok(group
!= NULL
, "GetSecurityInfo\n");
3159 ok(dacl
!= NULL
, "GetSecurityInfo\n");
3160 ok(IsValidAcl(dacl
), "GetSecurityInfo\n");
3165 static void test_GetSidSubAuthority(void)
3169 if (!pGetSidSubAuthority
|| !pConvertStringSidToSidA
|| !pIsValidSid
|| !pGetSidSubAuthorityCount
)
3171 win_skip("Some functions not available\n");
3174 /* Note: on windows passing in an invalid index like -1, lets GetSidSubAuthority return 0x05000000 but
3175 still GetLastError returns ERROR_SUCCESS then. We don't test these unlikely cornercases here for now */
3176 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576",&psid
),"ConvertStringSidToSidA failed\n");
3177 ok(pIsValidSid(psid
),"Sid is not valid\n");
3178 SetLastError(0xbebecaca);
3179 ok(*pGetSidSubAuthorityCount(psid
) == 4,"GetSidSubAuthorityCount gave %d expected 4\n",*pGetSidSubAuthorityCount(psid
));
3180 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3181 SetLastError(0xbebecaca);
3182 ok(*pGetSidSubAuthority(psid
,0) == 21,"GetSidSubAuthority gave %d expected 21\n",*pGetSidSubAuthority(psid
,0));
3183 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3184 SetLastError(0xbebecaca);
3185 ok(*pGetSidSubAuthority(psid
,1) == 93476,"GetSidSubAuthority gave %d expected 93476\n",*pGetSidSubAuthority(psid
,1));
3186 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3187 SetLastError(0xbebecaca);
3188 todo_wine
ok(*pGetSidSubAuthority(psid
,4) == 0,"GetSidSubAuthority gave %d,expected 0\n",*pGetSidSubAuthority(psid
,4));
3189 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3193 START_TEST(security
)
3200 test_process_security_child();
3206 test_CreateWellKnownSid();
3207 test_FileSecurity();
3210 test_LookupAccountSid();
3211 test_LookupAccountName();
3212 test_security_descriptor();
3213 test_process_security();
3214 test_impersonation_level();
3215 test_SetEntriesInAcl();
3216 test_GetNamedSecurityInfoA();
3217 test_ConvertStringSecurityDescriptor();
3218 test_ConvertSecurityDescriptorToString();
3219 test_PrivateObjectSecurity();
3221 test_GetSecurityInfo();
3222 test_GetSidSubAuthority();