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 typedef DWORD (WINAPI
*fnRtlAdjustPrivilege
)(ULONG
,BOOLEAN
,BOOLEAN
,PBOOLEAN
);
96 typedef BOOL (WINAPI
*fnCreateWellKnownSid
)(WELL_KNOWN_SID_TYPE
,PSID
,PSID
,DWORD
*);
97 typedef BOOL (WINAPI
*fnDuplicateTokenEx
)(HANDLE
,DWORD
,LPSECURITY_ATTRIBUTES
,
98 SECURITY_IMPERSONATION_LEVEL
,TOKEN_TYPE
,PHANDLE
);
100 typedef NTSTATUS (WINAPI
*fnLsaQueryInformationPolicy
)(LSA_HANDLE
,POLICY_INFORMATION_CLASS
,PVOID
*);
101 typedef NTSTATUS (WINAPI
*fnLsaClose
)(LSA_HANDLE
);
102 typedef NTSTATUS (WINAPI
*fnLsaFreeMemory
)(PVOID
);
103 typedef NTSTATUS (WINAPI
*fnLsaOpenPolicy
)(PLSA_UNICODE_STRING
,PLSA_OBJECT_ATTRIBUTES
,ACCESS_MASK
,PLSA_HANDLE
);
104 static NTSTATUS (WINAPI
*pNtQueryObject
)(HANDLE
,OBJECT_INFORMATION_CLASS
,PVOID
,ULONG
,PULONG
);
105 static DWORD (WINAPI
*pSetEntriesInAclW
)(ULONG
, PEXPLICIT_ACCESSW
, PACL
, PACL
*);
106 static BOOL (WINAPI
*pSetSecurityDescriptorControl
)(PSECURITY_DESCRIPTOR
, SECURITY_DESCRIPTOR_CONTROL
,
107 SECURITY_DESCRIPTOR_CONTROL
);
108 static DWORD (WINAPI
*pGetSecurityInfo
)(HANDLE
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
109 PSID
*, PSID
*, PACL
*, PACL
*, PSECURITY_DESCRIPTOR
*);
113 static char** myARGV
;
115 fnBuildTrusteeWithSidA pBuildTrusteeWithSidA
;
116 fnBuildTrusteeWithNameA pBuildTrusteeWithNameA
;
117 fnBuildTrusteeWithObjectsAndNameA pBuildTrusteeWithObjectsAndNameA
;
118 fnBuildTrusteeWithObjectsAndSidA pBuildTrusteeWithObjectsAndSidA
;
119 fnGetTrusteeNameA pGetTrusteeNameA
;
120 fnMakeSelfRelativeSD pMakeSelfRelativeSD
;
121 fnConvertSidToStringSidA pConvertSidToStringSidA
;
122 fnConvertStringSidToSidA pConvertStringSidToSidA
;
123 fnGetFileSecurityA pGetFileSecurityA
;
124 fnSetFileSecurityA pSetFileSecurityA
;
125 fnRtlAdjustPrivilege pRtlAdjustPrivilege
;
126 fnCreateWellKnownSid pCreateWellKnownSid
;
127 fnDuplicateTokenEx pDuplicateTokenEx
;
128 fnLsaQueryInformationPolicy pLsaQueryInformationPolicy
;
129 fnLsaClose pLsaClose
;
130 fnLsaFreeMemory pLsaFreeMemory
;
131 fnLsaOpenPolicy pLsaOpenPolicy
;
135 SID_IDENTIFIER_AUTHORITY auth
;
139 static void init(void)
143 hntdll
= GetModuleHandleA("ntdll.dll");
144 pNtQueryObject
= (void *)GetProcAddress( hntdll
, "NtQueryObject" );
146 hmod
= GetModuleHandle("advapi32.dll");
147 pAddAccessAllowedAceEx
= (void *)GetProcAddress(hmod
, "AddAccessAllowedAceEx");
148 pAddAccessDeniedAceEx
= (void *)GetProcAddress(hmod
, "AddAccessDeniedAceEx");
149 pAddAuditAccessAceEx
= (void *)GetProcAddress(hmod
, "AddAuditAccessAceEx");
150 pConvertStringSecurityDescriptorToSecurityDescriptorA
=
151 (void *)GetProcAddress(hmod
, "ConvertStringSecurityDescriptorToSecurityDescriptorA" );
152 pConvertStringSecurityDescriptorToSecurityDescriptorW
=
153 (void *)GetProcAddress(hmod
, "ConvertStringSecurityDescriptorToSecurityDescriptorW" );
154 pConvertSecurityDescriptorToStringSecurityDescriptorA
=
155 (void *)GetProcAddress(hmod
, "ConvertSecurityDescriptorToStringSecurityDescriptorA" );
156 pGetFileSecurityA
= (fnGetFileSecurityA
)GetProcAddress(hmod
, "GetFileSecurityA" );
157 pSetFileSecurityA
= (fnSetFileSecurityA
)GetProcAddress(hmod
, "SetFileSecurityA" );
158 pCreateWellKnownSid
= (fnCreateWellKnownSid
)GetProcAddress( hmod
, "CreateWellKnownSid" );
159 pGetNamedSecurityInfoA
= (void *)GetProcAddress(hmod
, "GetNamedSecurityInfoA");
160 pMakeSelfRelativeSD
= (void *)GetProcAddress(hmod
, "MakeSelfRelativeSD");
161 pSetEntriesInAclW
= (void *)GetProcAddress(hmod
, "SetEntriesInAclW");
162 pSetSecurityDescriptorControl
= (void *)GetProcAddress(hmod
, "SetSecurityDescriptorControl");
163 pGetSecurityInfo
= (void *)GetProcAddress(hmod
, "GetSecurityInfo");
165 myARGC
= winetest_get_mainargs( &myARGV
);
168 static void test_str_sid(const char *str_sid
)
173 if (pConvertStringSidToSidA(str_sid
, &psid
))
175 if (pConvertSidToStringSidA(psid
, &temp
))
177 trace(" %s: %s\n", str_sid
, temp
);
184 if (GetLastError() != ERROR_INVALID_SID
)
185 trace(" %s: couldn't be converted, returned %d\n", str_sid
, GetLastError());
187 trace(" %s: couldn't be converted\n", str_sid
);
191 static void test_sid(void)
193 struct sidRef refs
[] = {
194 { { {0x00,0x00,0x33,0x44,0x55,0x66} }, "S-1-860116326-1" },
195 { { {0x00,0x00,0x01,0x02,0x03,0x04} }, "S-1-16909060-1" },
196 { { {0x00,0x00,0x00,0x01,0x02,0x03} }, "S-1-66051-1" },
197 { { {0x00,0x00,0x00,0x00,0x01,0x02} }, "S-1-258-1" },
198 { { {0x00,0x00,0x00,0x00,0x00,0x02} }, "S-1-2-1" },
199 { { {0x00,0x00,0x00,0x00,0x00,0x0c} }, "S-1-12-1" },
201 const char noSubAuthStr
[] = "S-1-5";
208 pConvertSidToStringSidA
= (fnConvertSidToStringSidA
)
209 GetProcAddress( hmod
, "ConvertSidToStringSidA" );
210 if( !pConvertSidToStringSidA
)
212 pConvertStringSidToSidA
= (fnConvertStringSidToSidA
)
213 GetProcAddress( hmod
, "ConvertStringSidToSidA" );
214 if( !pConvertStringSidToSidA
)
217 r
= pConvertStringSidToSidA( NULL
, NULL
);
218 ok( !r
, "expected failure with NULL parameters\n" );
219 if( GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
221 ok( GetLastError() == ERROR_INVALID_PARAMETER
,
222 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
225 r
= pConvertStringSidToSidA( refs
[0].refStr
, NULL
);
226 ok( !r
&& GetLastError() == ERROR_INVALID_PARAMETER
,
227 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
230 r
= pConvertStringSidToSidA( NULL
, &str
);
231 ok( !r
&& GetLastError() == ERROR_INVALID_PARAMETER
,
232 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
235 r
= pConvertStringSidToSidA( noSubAuthStr
, &psid
);
237 "expected failure with no sub authorities\n" );
238 ok( GetLastError() == ERROR_INVALID_SID
,
239 "expected GetLastError() is ERROR_INVALID_SID, got %d\n",
242 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid
), "ConvertStringSidToSidA failed\n");
244 ok(pisid
->SubAuthorityCount
== 4, "Invalid sub authority count - expected 4, got %d\n", pisid
->SubAuthorityCount
);
245 ok(pisid
->SubAuthority
[0] == 21, "Invalid subauthority 0 - expceted 21, got %d\n", pisid
->SubAuthority
[0]);
246 ok(pisid
->SubAuthority
[3] == 4576, "Invalid subauthority 0 - expceted 4576, got %d\n", pisid
->SubAuthority
[3]);
249 for( i
= 0; i
< sizeof(refs
) / sizeof(refs
[0]); i
++ )
253 r
= AllocateAndInitializeSid( &refs
[i
].auth
, 1,1,0,0,0,0,0,0,0,
255 ok( r
, "failed to allocate sid\n" );
256 r
= pConvertSidToStringSidA( psid
, &str
);
257 ok( r
, "failed to convert sid\n" );
260 ok( !strcmp( str
, refs
[i
].refStr
),
261 "incorrect sid, expected %s, got %s\n", refs
[i
].refStr
, str
);
267 r
= pConvertStringSidToSidA( refs
[i
].refStr
, &psid
);
268 ok( r
, "failed to parse sid string\n" );
271 !memcmp( pisid
->IdentifierAuthority
.Value
, refs
[i
].auth
.Value
,
272 sizeof(refs
[i
].auth
) ),
273 "string sid %s didn't parse to expected value\n"
274 "(got 0x%04x%08x, expected 0x%04x%08x)\n",
276 MAKEWORD( pisid
->IdentifierAuthority
.Value
[1],
277 pisid
->IdentifierAuthority
.Value
[0] ),
278 MAKELONG( MAKEWORD( pisid
->IdentifierAuthority
.Value
[5],
279 pisid
->IdentifierAuthority
.Value
[4] ),
280 MAKEWORD( pisid
->IdentifierAuthority
.Value
[3],
281 pisid
->IdentifierAuthority
.Value
[2] ) ),
282 MAKEWORD( refs
[i
].auth
.Value
[1], refs
[i
].auth
.Value
[0] ),
283 MAKELONG( MAKEWORD( refs
[i
].auth
.Value
[5], refs
[i
].auth
.Value
[4] ),
284 MAKEWORD( refs
[i
].auth
.Value
[3], refs
[i
].auth
.Value
[2] ) ) );
289 trace("String SIDs:\n");
330 static void test_trustee(void)
332 GUID ObjectType
= {0x12345678, 0x1234, 0x5678, {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}};
333 GUID InheritedObjectType
= {0x23456789, 0x2345, 0x6786, {0x2, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}};
335 OBJECTS_AND_NAME_ oan
;
339 char szObjectTypeName
[] = "ObjectTypeName";
340 char szInheritedObjectTypeName
[] = "InheritedObjectTypeName";
341 char szTrusteeName
[] = "szTrusteeName";
342 SID_IDENTIFIER_AUTHORITY auth
= { {0x11,0x22,0,0,0, 0} };
344 memset( &ZeroGuid
, 0x00, sizeof (ZeroGuid
) );
346 pBuildTrusteeWithSidA
= (fnBuildTrusteeWithSidA
)
347 GetProcAddress( hmod
, "BuildTrusteeWithSidA" );
348 pBuildTrusteeWithNameA
= (fnBuildTrusteeWithNameA
)
349 GetProcAddress( hmod
, "BuildTrusteeWithNameA" );
350 pBuildTrusteeWithObjectsAndNameA
= (fnBuildTrusteeWithObjectsAndNameA
)
351 GetProcAddress (hmod
, "BuildTrusteeWithObjectsAndNameA" );
352 pBuildTrusteeWithObjectsAndSidA
= (fnBuildTrusteeWithObjectsAndSidA
)
353 GetProcAddress (hmod
, "BuildTrusteeWithObjectsAndSidA" );
354 pGetTrusteeNameA
= (fnGetTrusteeNameA
)
355 GetProcAddress (hmod
, "GetTrusteeNameA" );
356 if( !pBuildTrusteeWithSidA
|| !pBuildTrusteeWithNameA
||
357 !pBuildTrusteeWithObjectsAndNameA
|| !pBuildTrusteeWithObjectsAndSidA
||
361 if ( ! AllocateAndInitializeSid( &auth
, 1, 42, 0,0,0,0,0,0,0,&psid
) )
363 trace( "failed to init SID\n" );
367 /* test BuildTrusteeWithSidA */
368 memset( &trustee
, 0xff, sizeof trustee
);
369 pBuildTrusteeWithSidA( &trustee
, psid
);
371 ok( trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
372 ok( trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
,
373 "MultipleTrusteeOperation wrong\n");
374 ok( trustee
.TrusteeForm
== TRUSTEE_IS_SID
, "TrusteeForm wrong\n");
375 ok( trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
376 ok( trustee
.ptstrName
== (LPSTR
) psid
, "ptstrName wrong\n" );
378 /* test BuildTrusteeWithObjectsAndSidA (test 1) */
379 memset( &trustee
, 0xff, sizeof trustee
);
380 memset( &oas
, 0xff, sizeof(oas
) );
381 pBuildTrusteeWithObjectsAndSidA(&trustee
, &oas
, &ObjectType
,
382 &InheritedObjectType
, psid
);
384 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
385 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
386 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_SID
, "TrusteeForm wrong\n");
387 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
388 ok(trustee
.ptstrName
== (LPSTR
)&oas
, "ptstrName wrong\n");
390 ok(oas
.ObjectsPresent
== (ACE_OBJECT_TYPE_PRESENT
| ACE_INHERITED_OBJECT_TYPE_PRESENT
), "ObjectsPresent wrong\n");
391 ok(!memcmp(&oas
.ObjectTypeGuid
, &ObjectType
, sizeof(GUID
)), "ObjectTypeGuid wrong\n");
392 ok(!memcmp(&oas
.InheritedObjectTypeGuid
, &InheritedObjectType
, sizeof(GUID
)), "InheritedObjectTypeGuid wrong\n");
393 ok(oas
.pSid
== psid
, "pSid wrong\n");
395 /* test GetTrusteeNameA */
396 ok(pGetTrusteeNameA(&trustee
) == (LPSTR
)&oas
, "GetTrusteeName returned wrong value\n");
398 /* test BuildTrusteeWithObjectsAndSidA (test 2) */
399 memset( &trustee
, 0xff, sizeof trustee
);
400 memset( &oas
, 0xff, sizeof(oas
) );
401 pBuildTrusteeWithObjectsAndSidA(&trustee
, &oas
, NULL
,
402 &InheritedObjectType
, psid
);
404 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
405 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
406 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_SID
, "TrusteeForm wrong\n");
407 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
408 ok(trustee
.ptstrName
== (LPSTR
)&oas
, "ptstrName wrong\n");
410 ok(oas
.ObjectsPresent
== ACE_INHERITED_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
411 ok(!memcmp(&oas
.ObjectTypeGuid
, &ZeroGuid
, sizeof(GUID
)), "ObjectTypeGuid wrong\n");
412 ok(!memcmp(&oas
.InheritedObjectTypeGuid
, &InheritedObjectType
, sizeof(GUID
)), "InheritedObjectTypeGuid wrong\n");
413 ok(oas
.pSid
== psid
, "pSid wrong\n");
417 /* test BuildTrusteeWithNameA */
418 memset( &trustee
, 0xff, sizeof trustee
);
419 pBuildTrusteeWithNameA( &trustee
, szTrusteeName
);
421 ok( trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
422 ok( trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
,
423 "MultipleTrusteeOperation wrong\n");
424 ok( trustee
.TrusteeForm
== TRUSTEE_IS_NAME
, "TrusteeForm wrong\n");
425 ok( trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
426 ok( trustee
.ptstrName
== szTrusteeName
, "ptstrName wrong\n" );
428 /* test BuildTrusteeWithObjectsAndNameA (test 1) */
429 memset( &trustee
, 0xff, sizeof trustee
);
430 memset( &oan
, 0xff, sizeof(oan
) );
431 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, szObjectTypeName
,
432 szInheritedObjectTypeName
, szTrusteeName
);
434 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
435 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
436 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
437 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
438 ok(trustee
.ptstrName
== (LPTSTR
)&oan
, "ptstrName wrong\n");
440 ok(oan
.ObjectsPresent
== (ACE_OBJECT_TYPE_PRESENT
| ACE_INHERITED_OBJECT_TYPE_PRESENT
), "ObjectsPresent wrong\n");
441 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
442 ok(oan
.InheritedObjectTypeName
== szInheritedObjectTypeName
, "InheritedObjectTypeName wrong\n");
443 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
445 /* test GetTrusteeNameA */
446 ok(pGetTrusteeNameA(&trustee
) == (LPSTR
)&oan
, "GetTrusteeName returned wrong value\n");
448 /* test BuildTrusteeWithObjectsAndNameA (test 2) */
449 memset( &trustee
, 0xff, sizeof trustee
);
450 memset( &oan
, 0xff, sizeof(oan
) );
451 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, NULL
,
452 szInheritedObjectTypeName
, szTrusteeName
);
454 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
455 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
456 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
457 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
458 ok(trustee
.ptstrName
== (LPSTR
)&oan
, "ptstrName wrong\n");
460 ok(oan
.ObjectsPresent
== ACE_INHERITED_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
461 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
462 ok(oan
.InheritedObjectTypeName
== szInheritedObjectTypeName
, "InheritedObjectTypeName wrong\n");
463 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
465 /* test BuildTrusteeWithObjectsAndNameA (test 3) */
466 memset( &trustee
, 0xff, sizeof trustee
);
467 memset( &oan
, 0xff, sizeof(oan
) );
468 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, szObjectTypeName
,
469 NULL
, szTrusteeName
);
471 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
472 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
473 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
474 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
475 ok(trustee
.ptstrName
== (LPTSTR
)&oan
, "ptstrName wrong\n");
477 ok(oan
.ObjectsPresent
== ACE_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
478 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
479 ok(oan
.InheritedObjectTypeName
== NULL
, "InheritedObjectTypeName wrong\n");
480 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
483 /* If the first isn't defined, assume none is */
484 #ifndef SE_MIN_WELL_KNOWN_PRIVILEGE
485 #define SE_MIN_WELL_KNOWN_PRIVILEGE 2L
486 #define SE_CREATE_TOKEN_PRIVILEGE 2L
487 #define SE_ASSIGNPRIMARYTOKEN_PRIVILEGE 3L
488 #define SE_LOCK_MEMORY_PRIVILEGE 4L
489 #define SE_INCREASE_QUOTA_PRIVILEGE 5L
490 #define SE_MACHINE_ACCOUNT_PRIVILEGE 6L
491 #define SE_TCB_PRIVILEGE 7L
492 #define SE_SECURITY_PRIVILEGE 8L
493 #define SE_TAKE_OWNERSHIP_PRIVILEGE 9L
494 #define SE_LOAD_DRIVER_PRIVILEGE 10L
495 #define SE_SYSTEM_PROFILE_PRIVILEGE 11L
496 #define SE_SYSTEMTIME_PRIVILEGE 12L
497 #define SE_PROF_SINGLE_PROCESS_PRIVILEGE 13L
498 #define SE_INC_BASE_PRIORITY_PRIVILEGE 14L
499 #define SE_CREATE_PAGEFILE_PRIVILEGE 15L
500 #define SE_CREATE_PERMANENT_PRIVILEGE 16L
501 #define SE_BACKUP_PRIVILEGE 17L
502 #define SE_RESTORE_PRIVILEGE 18L
503 #define SE_SHUTDOWN_PRIVILEGE 19L
504 #define SE_DEBUG_PRIVILEGE 20L
505 #define SE_AUDIT_PRIVILEGE 21L
506 #define SE_SYSTEM_ENVIRONMENT_PRIVILEGE 22L
507 #define SE_CHANGE_NOTIFY_PRIVILLEGE 23L
508 #define SE_REMOTE_SHUTDOWN_PRIVILEGE 24L
509 #define SE_UNDOCK_PRIVILEGE 25L
510 #define SE_SYNC_AGENT_PRIVILEGE 26L
511 #define SE_ENABLE_DELEGATION_PRIVILEGE 27L
512 #define SE_MANAGE_VOLUME_PRIVILEGE 28L
513 #define SE_IMPERSONATE_PRIVILEGE 29L
514 #define SE_CREATE_GLOBAL_PRIVILEGE 30L
515 #define SE_MAX_WELL_KNOWN_PRIVILEGE SE_CREATE_GLOBAL_PRIVILEGE
516 #endif /* ndef SE_MIN_WELL_KNOWN_PRIVILEGE */
518 static void test_allocateLuid(void)
520 BOOL (WINAPI
*pAllocateLocallyUniqueId
)(PLUID
);
524 pAllocateLocallyUniqueId
= (void*)GetProcAddress(hmod
, "AllocateLocallyUniqueId");
525 if (!pAllocateLocallyUniqueId
) return;
527 ret
= pAllocateLocallyUniqueId(&luid1
);
528 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
532 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
533 ret
= pAllocateLocallyUniqueId(&luid2
);
535 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
536 ok(luid1
.LowPart
> SE_MAX_WELL_KNOWN_PRIVILEGE
|| luid1
.HighPart
!= 0,
537 "AllocateLocallyUniqueId returned a well-known LUID\n");
538 ok(luid1
.LowPart
!= luid2
.LowPart
|| luid1
.HighPart
!= luid2
.HighPart
,
539 "AllocateLocallyUniqueId returned non-unique LUIDs\n");
540 ret
= pAllocateLocallyUniqueId(NULL
);
541 ok( !ret
&& GetLastError() == ERROR_NOACCESS
,
542 "AllocateLocallyUniqueId(NULL) didn't return ERROR_NOACCESS: %d\n",
546 static void test_lookupPrivilegeName(void)
548 BOOL (WINAPI
*pLookupPrivilegeNameA
)(LPCSTR
, PLUID
, LPSTR
, LPDWORD
);
549 char buf
[MAX_PATH
]; /* arbitrary, seems long enough */
550 DWORD cchName
= sizeof(buf
);
551 LUID luid
= { 0, 0 };
555 /* check whether it's available first */
556 pLookupPrivilegeNameA
= (void*)GetProcAddress(hmod
, "LookupPrivilegeNameA");
557 if (!pLookupPrivilegeNameA
) return;
558 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
559 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
560 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
563 /* check with a short buffer */
565 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
566 ret
= pLookupPrivilegeNameA(NULL
, &luid
, NULL
, &cchName
);
567 ok( !ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
568 "LookupPrivilegeNameA didn't fail with ERROR_INSUFFICIENT_BUFFER: %d\n",
570 ok(cchName
== strlen("SeCreateTokenPrivilege") + 1,
571 "LookupPrivilegeNameA returned an incorrect required length for\n"
572 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName
,
573 lstrlenA("SeCreateTokenPrivilege") + 1);
574 /* check a known value and its returned length on success */
575 cchName
= sizeof(buf
);
576 ok(pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
) &&
577 cchName
== strlen("SeCreateTokenPrivilege"),
578 "LookupPrivilegeNameA returned an incorrect output length for\n"
579 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName
,
580 (int)strlen("SeCreateTokenPrivilege"));
581 /* check known values */
582 for (i
= SE_MIN_WELL_KNOWN_PRIVILEGE
; i
< SE_MAX_WELL_KNOWN_PRIVILEGE
; i
++)
585 cchName
= sizeof(buf
);
586 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
587 ok( ret
|| GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
588 "LookupPrivilegeNameA(0.%d) failed: %d\n", i
, GetLastError());
590 /* check a bogus LUID */
591 luid
.LowPart
= 0xdeadbeef;
592 cchName
= sizeof(buf
);
593 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
594 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
595 "LookupPrivilegeNameA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
597 /* check on a bogus system */
598 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
599 cchName
= sizeof(buf
);
600 ret
= pLookupPrivilegeNameA("b0gu5.Nam3", &luid
, buf
, &cchName
);
601 ok( !ret
&& (GetLastError() == RPC_S_SERVER_UNAVAILABLE
||
602 GetLastError() == RPC_S_INVALID_NET_ADDR
) /* w2k8 */,
603 "LookupPrivilegeNameA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
613 static void test_lookupPrivilegeValue(void)
615 static const struct NameToLUID privs
[] = {
616 { "SeCreateTokenPrivilege", SE_CREATE_TOKEN_PRIVILEGE
},
617 { "SeAssignPrimaryTokenPrivilege", SE_ASSIGNPRIMARYTOKEN_PRIVILEGE
},
618 { "SeLockMemoryPrivilege", SE_LOCK_MEMORY_PRIVILEGE
},
619 { "SeIncreaseQuotaPrivilege", SE_INCREASE_QUOTA_PRIVILEGE
},
620 { "SeMachineAccountPrivilege", SE_MACHINE_ACCOUNT_PRIVILEGE
},
621 { "SeTcbPrivilege", SE_TCB_PRIVILEGE
},
622 { "SeSecurityPrivilege", SE_SECURITY_PRIVILEGE
},
623 { "SeTakeOwnershipPrivilege", SE_TAKE_OWNERSHIP_PRIVILEGE
},
624 { "SeLoadDriverPrivilege", SE_LOAD_DRIVER_PRIVILEGE
},
625 { "SeSystemProfilePrivilege", SE_SYSTEM_PROFILE_PRIVILEGE
},
626 { "SeSystemtimePrivilege", SE_SYSTEMTIME_PRIVILEGE
},
627 { "SeProfileSingleProcessPrivilege", SE_PROF_SINGLE_PROCESS_PRIVILEGE
},
628 { "SeIncreaseBasePriorityPrivilege", SE_INC_BASE_PRIORITY_PRIVILEGE
},
629 { "SeCreatePagefilePrivilege", SE_CREATE_PAGEFILE_PRIVILEGE
},
630 { "SeCreatePermanentPrivilege", SE_CREATE_PERMANENT_PRIVILEGE
},
631 { "SeBackupPrivilege", SE_BACKUP_PRIVILEGE
},
632 { "SeRestorePrivilege", SE_RESTORE_PRIVILEGE
},
633 { "SeShutdownPrivilege", SE_SHUTDOWN_PRIVILEGE
},
634 { "SeDebugPrivilege", SE_DEBUG_PRIVILEGE
},
635 { "SeAuditPrivilege", SE_AUDIT_PRIVILEGE
},
636 { "SeSystemEnvironmentPrivilege", SE_SYSTEM_ENVIRONMENT_PRIVILEGE
},
637 { "SeChangeNotifyPrivilege", SE_CHANGE_NOTIFY_PRIVILLEGE
},
638 { "SeRemoteShutdownPrivilege", SE_REMOTE_SHUTDOWN_PRIVILEGE
},
639 { "SeUndockPrivilege", SE_UNDOCK_PRIVILEGE
},
640 { "SeSyncAgentPrivilege", SE_SYNC_AGENT_PRIVILEGE
},
641 { "SeEnableDelegationPrivilege", SE_ENABLE_DELEGATION_PRIVILEGE
},
642 { "SeManageVolumePrivilege", SE_MANAGE_VOLUME_PRIVILEGE
},
643 { "SeImpersonatePrivilege", SE_IMPERSONATE_PRIVILEGE
},
644 { "SeCreateGlobalPrivilege", SE_CREATE_GLOBAL_PRIVILEGE
},
646 BOOL (WINAPI
*pLookupPrivilegeValueA
)(LPCSTR
, LPCSTR
, PLUID
);
651 /* check whether it's available first */
652 pLookupPrivilegeValueA
= (void*)GetProcAddress(hmod
, "LookupPrivilegeValueA");
653 if (!pLookupPrivilegeValueA
) return;
654 ret
= pLookupPrivilegeValueA(NULL
, "SeCreateTokenPrivilege", &luid
);
655 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
658 /* check a bogus system name */
659 ret
= pLookupPrivilegeValueA("b0gu5.Nam3", "SeCreateTokenPrivilege", &luid
);
660 ok( !ret
&& (GetLastError() == RPC_S_SERVER_UNAVAILABLE
||
661 GetLastError() == RPC_S_INVALID_NET_ADDR
) /* w2k8 */,
662 "LookupPrivilegeValueA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
664 /* check a NULL string */
665 ret
= pLookupPrivilegeValueA(NULL
, 0, &luid
);
666 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
667 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
669 /* check a bogus privilege name */
670 ret
= pLookupPrivilegeValueA(NULL
, "SeBogusPrivilege", &luid
);
671 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
672 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
674 /* check case insensitive */
675 ret
= pLookupPrivilegeValueA(NULL
, "sEcREATEtOKENpRIVILEGE", &luid
);
677 "LookupPrivilegeValueA(NULL, sEcREATEtOKENpRIVILEGE, &luid) failed: %d\n",
679 for (i
= 0; i
< sizeof(privs
) / sizeof(privs
[0]); i
++)
681 /* Not all privileges are implemented on all Windows versions, so
682 * don't worry if the call fails
684 if (pLookupPrivilegeValueA(NULL
, privs
[i
].name
, &luid
))
686 ok(luid
.LowPart
== privs
[i
].lowPart
,
687 "LookupPrivilegeValueA returned an invalid LUID for %s\n",
693 static void test_luid(void)
696 test_lookupPrivilegeName();
697 test_lookupPrivilegeValue();
700 static void test_FileSecurity(void)
702 char wintmpdir
[MAX_PATH
];
703 char path
[MAX_PATH
];
704 char file
[MAX_PATH
];
710 SECURITY_INFORMATION
const request
= OWNER_SECURITY_INFORMATION
711 | GROUP_SECURITY_INFORMATION
712 | DACL_SECURITY_INFORMATION
;
714 if (!pGetFileSecurityA
) {
715 win_skip ("GetFileSecurity is not available\n");
719 if (!pSetFileSecurityA
) {
720 win_skip ("SetFileSecurity is not available\n");
724 if (!GetTempPathA (sizeof (wintmpdir
), wintmpdir
)) {
725 win_skip ("GetTempPathA failed\n");
729 /* Create a temporary directory and in it a temporary file */
730 strcat (strcpy (path
, wintmpdir
), "rary");
731 SetLastError (NO_ERROR
);
732 rc
= CreateDirectoryA (path
, NULL
);
733 ok (rc
|| GetLastError() == ERROR_ALREADY_EXISTS
, "CreateDirectoryA "
734 "failed for '%s' with %d\n", path
, GetLastError());
736 strcat (strcpy (file
, path
), "\\ess");
737 SetLastError (NO_ERROR
);
738 fh
= CreateFileA (file
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, NULL
);
739 ok (fh
!= INVALID_HANDLE_VALUE
, "CreateFileA "
740 "failed for '%s' with %d\n", file
, GetLastError());
743 /* For the temporary file ... */
745 /* Get size needed */
747 SetLastError (NO_ERROR
);
748 rc
= pGetFileSecurityA (file
, request
, NULL
, 0, &retSize
);
749 ok (!rc
, "GetFileSecurityA "
750 "was expected to fail for '%s'\n", file
);
751 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetFileSecurityA "
752 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
753 ok (retSize
> sizeof (SECURITY_DESCRIPTOR
), "GetFileSecurityA "
754 "returned size %d; expected > %d\n", retSize
, sizeof (SECURITY_DESCRIPTOR
));
757 sd
= HeapAlloc (GetProcessHeap (), 0, sdSize
);
759 /* Get security descriptor for real */
761 SetLastError (NO_ERROR
);
762 rc
= pGetFileSecurityA (file
, request
, sd
, sdSize
, &retSize
);
763 ok (rc
, "GetFileSecurityA "
764 "was not expected to fail '%s'\n", file
);
765 ok (GetLastError () == NO_ERROR
, "GetFileSecurityA "
766 "returned %d; expected NO_ERROR\n", GetLastError ());
767 ok (retSize
== sdSize
, "GetFileSecurityA "
768 "returned size %d; expected %d\n", retSize
, sdSize
);
770 /* Use it to set security descriptor */
771 SetLastError (NO_ERROR
);
772 rc
= pSetFileSecurityA (file
, request
, sd
);
773 ok (rc
, "SetFileSecurityA "
774 "was not expected to fail '%s'\n", file
);
775 ok (GetLastError () == NO_ERROR
, "SetFileSecurityA "
776 "returned %d; expected NO_ERROR\n", GetLastError ());
778 HeapFree (GetProcessHeap (), 0, sd
);
780 /* Repeat for the temporary directory ... */
782 /* Get size needed */
784 SetLastError (NO_ERROR
);
785 rc
= pGetFileSecurityA (path
, request
, NULL
, 0, &retSize
);
786 ok (!rc
, "GetFileSecurityA "
787 "was expected to fail for '%s'\n", path
);
788 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetFileSecurityA "
789 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
790 ok (retSize
> sizeof (SECURITY_DESCRIPTOR
), "GetFileSecurityA "
791 "returned size %d; expected > %d\n", retSize
, sizeof (SECURITY_DESCRIPTOR
));
794 sd
= HeapAlloc (GetProcessHeap (), 0, sdSize
);
796 /* Get security descriptor for real */
798 SetLastError (NO_ERROR
);
799 rc
= pGetFileSecurityA (path
, request
, sd
, sdSize
, &retSize
);
800 ok (rc
, "GetFileSecurityA "
801 "was not expected to fail '%s'\n", path
);
802 ok (GetLastError () == NO_ERROR
, "GetFileSecurityA "
803 "returned %d; expected NO_ERROR\n", GetLastError ());
804 ok (retSize
== sdSize
, "GetFileSecurityA "
805 "returned size %d; expected %d\n", retSize
, sdSize
);
807 /* Use it to set security descriptor */
808 SetLastError (NO_ERROR
);
809 rc
= pSetFileSecurityA (path
, request
, sd
);
810 ok (rc
, "SetFileSecurityA "
811 "was not expected to fail '%s'\n", path
);
812 ok (GetLastError () == NO_ERROR
, "SetFileSecurityA "
813 "returned %d; expected NO_ERROR\n", GetLastError ());
815 HeapFree (GetProcessHeap (), 0, sd
);
817 /* Remove temporary file and directory */
819 RemoveDirectoryA (path
);
822 strcpy (wintmpdir
, "\\Should not exist");
823 SetLastError (NO_ERROR
);
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 (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
),
828 "last error ERROR_FILE_NOT_FOUND / ERROR_CALL_NOT_IMPLEMENTED (98) "
829 "expected, got %d\n", GetLastError());
832 static void test_AccessCheck(void)
834 PSID EveryoneSid
= NULL
, AdminSid
= NULL
, UsersSid
= NULL
;
836 SECURITY_DESCRIPTOR
*SecurityDescriptor
= NULL
;
837 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
838 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
839 GENERIC_MAPPING Mapping
= { KEY_READ
, KEY_WRITE
, KEY_EXECUTE
, KEY_ALL_ACCESS
};
846 PRIVILEGE_SET
*PrivSet
;
852 NtDllModule
= GetModuleHandle("ntdll.dll");
855 skip("not running on NT, skipping test\n");
858 pRtlAdjustPrivilege
= (fnRtlAdjustPrivilege
)
859 GetProcAddress(NtDllModule
, "RtlAdjustPrivilege");
860 if (!pRtlAdjustPrivilege
)
862 skip("missing RtlAdjustPrivilege, skipping test\n");
866 Acl
= HeapAlloc(GetProcessHeap(), 0, 256);
867 res
= InitializeAcl(Acl
, 256, ACL_REVISION
);
868 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
870 skip("ACLs not implemented - skipping tests\n");
871 HeapFree(GetProcessHeap(), 0, Acl
);
874 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
876 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
877 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
879 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
880 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &AdminSid
);
881 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
883 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
884 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
885 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
887 SecurityDescriptor
= HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
889 res
= InitializeSecurityDescriptor(SecurityDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
890 ok(res
, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
892 res
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
893 ok(res
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
895 PrivSetLen
= FIELD_OFFSET(PRIVILEGE_SET
, Privilege
[16]);
896 PrivSet
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, PrivSetLen
);
897 PrivSet
->PrivilegeCount
= 16;
899 res
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &ProcessToken
);
900 ok(res
, "OpenProcessToken failed with error %d\n", GetLastError());
902 pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE
, FALSE
, TRUE
, &Enabled
);
904 res
= DuplicateToken(ProcessToken
, SecurityImpersonation
, &Token
);
905 ok(res
, "DuplicateToken failed with error %d\n", GetLastError());
907 /* SD without owner/group */
908 SetLastError(0xdeadbeef);
909 Access
= AccessStatus
= 0xdeadbeef;
910 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_QUERY_VALUE
, &Mapping
,
911 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
912 err
= GetLastError();
913 ok(!ret
&& err
== ERROR_INVALID_SECURITY_DESCR
, "AccessCheck should have "
914 "failed with ERROR_INVALID_SECURITY_DESCR, instead of %d\n", err
);
915 ok(Access
== 0xdeadbeef && AccessStatus
== 0xdeadbeef,
916 "Access and/or AccessStatus were changed!\n");
918 /* Set owner and group */
919 res
= SetSecurityDescriptorOwner(SecurityDescriptor
, AdminSid
, FALSE
);
920 ok(res
, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
921 res
= SetSecurityDescriptorGroup(SecurityDescriptor
, UsersSid
, TRUE
);
922 ok(res
, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
924 /* Generic access mask */
925 SetLastError(0xdeadbeef);
926 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
927 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
928 err
= GetLastError();
929 ok(!ret
&& err
== ERROR_GENERIC_NOT_MAPPED
, "AccessCheck should have failed "
930 "with ERROR_GENERIC_NOT_MAPPED, instead of %d\n", err
);
931 ok(Access
== 0xdeadbeef && AccessStatus
== 0xdeadbeef,
932 "Access and/or AccessStatus were changed!\n");
934 /* sd with no dacl present */
935 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, FALSE
, NULL
, FALSE
);
936 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
937 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
938 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
939 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
940 ok(AccessStatus
&& (Access
== KEY_READ
),
941 "AccessCheck failed to grant access with error %d\n",
944 /* sd with NULL dacl */
945 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, NULL
, FALSE
);
946 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
947 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
948 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
949 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
950 ok(AccessStatus
&& (Access
== KEY_READ
),
951 "AccessCheck failed to grant access with error %d\n",
954 /* sd with blank dacl */
955 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
956 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
957 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
958 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
959 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
960 err
= GetLastError();
961 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
962 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
963 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
965 res
= AddAccessAllowedAce(Acl
, ACL_REVISION
, KEY_READ
, EveryoneSid
);
966 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
968 res
= AddAccessDeniedAce(Acl
, ACL_REVISION
, KEY_SET_VALUE
, AdminSid
);
969 ok(res
, "AddAccessDeniedAce failed with error %d\n", GetLastError());
972 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
973 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
974 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
975 ok(AccessStatus
&& (Access
== KEY_READ
),
976 "AccessCheck failed to grant access with error %d\n",
979 ret
= AccessCheck(SecurityDescriptor
, Token
, MAXIMUM_ALLOWED
, &Mapping
,
980 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
981 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
983 "AccessCheck failed to grant any access with error %d\n",
985 trace("AccessCheck with MAXIMUM_ALLOWED got Access 0x%08x\n", Access
);
987 /* Access denied by SD */
988 SetLastError(0xdeadbeef);
989 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_WRITE
, &Mapping
,
990 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
991 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
992 err
= GetLastError();
993 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
994 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
995 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
998 PrivSet
->PrivilegeCount
= 16;
999 ret
= AccessCheck(SecurityDescriptor
, Token
, ACCESS_SYSTEM_SECURITY
, &Mapping
,
1000 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1001 ok(ret
&& !AccessStatus
&& GetLastError() == ERROR_PRIVILEGE_NOT_HELD
,
1002 "AccessCheck should have failed with ERROR_PRIVILEGE_NOT_HELD, instead of %d\n",
1005 ret
= ImpersonateLoggedOnUser(Token
);
1006 ok(ret
, "ImpersonateLoggedOnUser failed with error %d\n", GetLastError());
1007 ret
= pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE
, TRUE
, TRUE
, &Enabled
);
1011 PrivSet
->PrivilegeCount
= 16;
1012 ret
= AccessCheck(SecurityDescriptor
, Token
, ACCESS_SYSTEM_SECURITY
, &Mapping
,
1013 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1014 ok(ret
&& AccessStatus
&& GetLastError() == 0,
1015 "AccessCheck should have succeeded, error %d\n",
1017 ok(Access
== ACCESS_SYSTEM_SECURITY
,
1018 "Access should be equal to ACCESS_SYSTEM_SECURITY instead of 0x%08x\n",
1022 trace("Couldn't get SE_SECURITY_PRIVILEGE (0x%08x), skipping ACCESS_SYSTEM_SECURITY test\n",
1024 ret
= RevertToSelf();
1025 ok(ret
, "RevertToSelf failed with error %d\n", GetLastError());
1027 /* test INHERIT_ONLY_ACE */
1028 ret
= InitializeAcl(Acl
, 256, ACL_REVISION
);
1029 ok(ret
, "InitializeAcl failed with error %d\n", GetLastError());
1031 /* NT doesn't have AddAccessAllowedAceEx. Skipping this call/test doesn't influence
1034 if (pAddAccessAllowedAceEx
)
1036 ret
= pAddAccessAllowedAceEx(Acl
, ACL_REVISION
, INHERIT_ONLY_ACE
, KEY_READ
, EveryoneSid
);
1037 ok(ret
, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
1040 skip("AddAccessAllowedAceEx is not available\n");
1042 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1043 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1044 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1045 err
= GetLastError();
1046 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1047 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1048 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1052 res
= DuplicateToken(ProcessToken
, SecurityAnonymous
, &Token
);
1053 ok(res
, "DuplicateToken failed with error %d\n", GetLastError());
1055 SetLastError(0xdeadbeef);
1056 ret
= AccessCheck(SecurityDescriptor
, Token
, MAXIMUM_ALLOWED
, &Mapping
,
1057 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1058 err
= GetLastError();
1059 ok(!ret
&& err
== ERROR_BAD_IMPERSONATION_LEVEL
, "AccessCheck should have failed "
1060 "with ERROR_BAD_IMPERSONATION_LEVEL, instead of %d\n", err
);
1064 SetLastError(0xdeadbeef);
1065 ret
= AccessCheck(SecurityDescriptor
, ProcessToken
, KEY_READ
, &Mapping
,
1066 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1067 err
= GetLastError();
1068 ok(!ret
&& err
== ERROR_NO_IMPERSONATION_TOKEN
, "AccessCheck should have failed "
1069 "with ERROR_NO_IMPERSONATION_TOKEN, instead of %d\n", err
);
1071 CloseHandle(ProcessToken
);
1074 FreeSid(EveryoneSid
);
1079 HeapFree(GetProcessHeap(), 0, Acl
);
1080 HeapFree(GetProcessHeap(), 0, SecurityDescriptor
);
1081 HeapFree(GetProcessHeap(), 0, PrivSet
);
1084 /* test GetTokenInformation for the various attributes */
1085 static void test_token_attr(void)
1087 HANDLE Token
, ImpersonationToken
;
1089 TOKEN_PRIVILEGES
*Privileges
;
1090 TOKEN_GROUPS
*Groups
;
1095 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
;
1097 /* cygwin-like use case */
1098 SetLastError(0xdeadbeef);
1099 ret
= OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED
, &Token
);
1100 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
1102 skip("OpenProcessToken is not implemented\n");
1105 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1110 ret
= GetTokenInformation(Token
, TokenUser
,(void*)buf
, Size
, &Size
);
1111 ok(ret
, "GetTokenInformation failed with error %d\n", GetLastError());
1112 Size
= sizeof(ImpersonationLevel
);
1113 ret
= GetTokenInformation(Token
, TokenImpersonationLevel
, &ImpersonationLevel
, Size
, &Size
);
1114 GLE
= GetLastError();
1115 ok(!ret
&& (GLE
== ERROR_INVALID_PARAMETER
), "GetTokenInformation(TokenImpersonationLevel) on primary token should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GLE
);
1119 if(!pConvertSidToStringSidA
)
1121 skip("ConvertSidToStringSidA is not available\n");
1125 SetLastError(0xdeadbeef);
1126 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
|TOKEN_DUPLICATE
, &Token
);
1127 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1130 ret
= GetTokenInformation(Token
, TokenGroups
, NULL
, 0, &Size
);
1131 Groups
= HeapAlloc(GetProcessHeap(), 0, Size
);
1132 ret
= GetTokenInformation(Token
, TokenGroups
, Groups
, Size
, &Size
);
1133 ok(ret
, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
1134 trace("TokenGroups:\n");
1135 for (i
= 0; i
< Groups
->GroupCount
; i
++)
1137 DWORD NameLength
= 255;
1139 DWORD DomainLength
= 255;
1141 SID_NAME_USE SidNameUse
;
1142 pConvertSidToStringSidA(Groups
->Groups
[i
].Sid
, &SidString
);
1145 ret
= LookupAccountSid(NULL
, Groups
->Groups
[i
].Sid
, Name
, &NameLength
, Domain
, &DomainLength
, &SidNameUse
);
1147 trace("\t%s, %s\\%s use: %d attr: 0x%08x\n", SidString
, Domain
, Name
, SidNameUse
, Groups
->Groups
[i
].Attributes
);
1149 trace("\t%s, attr: 0x%08x LookupAccountSid failed with error %d\n", SidString
, Groups
->Groups
[i
].Attributes
, GetLastError());
1150 LocalFree(SidString
);
1152 HeapFree(GetProcessHeap(), 0, Groups
);
1155 ret
= GetTokenInformation(Token
, TokenUser
, NULL
, 0, &Size
);
1156 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1157 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1158 User
= HeapAlloc(GetProcessHeap(), 0, Size
);
1159 ret
= GetTokenInformation(Token
, TokenUser
, User
, Size
, &Size
);
1161 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1163 pConvertSidToStringSidA(User
->User
.Sid
, &SidString
);
1164 trace("TokenUser: %s attr: 0x%08x\n", SidString
, User
->User
.Attributes
);
1165 LocalFree(SidString
);
1166 HeapFree(GetProcessHeap(), 0, User
);
1169 ret
= GetTokenInformation(Token
, TokenPrivileges
, NULL
, 0, &Size
);
1170 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1171 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1172 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
1173 ret
= GetTokenInformation(Token
, TokenPrivileges
, Privileges
, Size
, &Size
);
1175 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1176 trace("TokenPrivileges:\n");
1177 for (i
= 0; i
< Privileges
->PrivilegeCount
; i
++)
1180 DWORD NameLen
= sizeof(Name
)/sizeof(Name
[0]);
1181 LookupPrivilegeName(NULL
, &Privileges
->Privileges
[i
].Luid
, Name
, &NameLen
);
1182 trace("\t%s, 0x%x\n", Name
, Privileges
->Privileges
[i
].Attributes
);
1184 HeapFree(GetProcessHeap(), 0, Privileges
);
1186 ret
= DuplicateToken(Token
, SecurityAnonymous
, &ImpersonationToken
);
1187 ok(ret
, "DuplicateToken failed with error %d\n", GetLastError());
1189 Size
= sizeof(ImpersonationLevel
);
1190 ret
= GetTokenInformation(ImpersonationToken
, TokenImpersonationLevel
, &ImpersonationLevel
, Size
, &Size
);
1191 ok(ret
, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1192 ok(ImpersonationLevel
== SecurityAnonymous
, "ImpersonationLevel should have been SecurityAnonymous instead of %d\n", ImpersonationLevel
);
1194 CloseHandle(ImpersonationToken
);
1198 typedef union _MAX_SID
1201 char max
[SECURITY_MAX_SID_SIZE
];
1204 static void test_sid_str(PSID
* sid
)
1207 BOOL ret
= pConvertSidToStringSidA(sid
, &str_sid
);
1208 ok(ret
, "ConvertSidToStringSidA() failed: %d\n", GetLastError());
1211 char account
[MAX_PATH
], domain
[MAX_PATH
];
1213 DWORD acc_size
= MAX_PATH
;
1214 DWORD dom_size
= MAX_PATH
;
1215 ret
= LookupAccountSid(NULL
, sid
, account
, &acc_size
, domain
, &dom_size
, &use
);
1216 ok(ret
|| (!ret
&& (GetLastError() == ERROR_NONE_MAPPED
)),
1217 "LookupAccountSid(%s) failed: %d\n", str_sid
, GetLastError());
1219 trace(" %s %s\\%s %d\n", str_sid
, domain
, account
, use
);
1220 else if (GetLastError() == ERROR_NONE_MAPPED
)
1221 trace(" %s couldn't be mapped\n", str_sid
);
1226 struct well_known_sid_value
1228 BOOL without_domain
;
1229 const char *sid_string
;
1230 } well_known_sid_values
[] = {
1231 /* 0 */ {TRUE
, "S-1-0-0"}, {TRUE
, "S-1-1-0"}, {TRUE
, "S-1-2-0"}, {TRUE
, "S-1-3-0"},
1232 /* 4 */ {TRUE
, "S-1-3-1"}, {TRUE
, "S-1-3-2"}, {TRUE
, "S-1-3-3"}, {TRUE
, "S-1-5"},
1233 /* 8 */ {FALSE
, "S-1-5-1"}, {TRUE
, "S-1-5-2"}, {TRUE
, "S-1-5-3"}, {TRUE
, "S-1-5-4"},
1234 /* 12 */ {TRUE
, "S-1-5-6"}, {TRUE
, "S-1-5-7"}, {TRUE
, "S-1-5-8"}, {TRUE
, "S-1-5-9"},
1235 /* 16 */ {TRUE
, "S-1-5-10"}, {TRUE
, "S-1-5-11"}, {TRUE
, "S-1-5-12"}, {TRUE
, "S-1-5-13"},
1236 /* 20 */ {TRUE
, "S-1-5-14"}, {FALSE
, NULL
}, {TRUE
, "S-1-5-18"}, {TRUE
, "S-1-5-19"},
1237 /* 24 */ {TRUE
, "S-1-5-20"}, {TRUE
, "S-1-5-32"},
1238 /* 26 */ {FALSE
, "S-1-5-32-544"}, {TRUE
, "S-1-5-32-545"}, {TRUE
, "S-1-5-32-546"},
1239 /* 29 */ {TRUE
, "S-1-5-32-547"}, {TRUE
, "S-1-5-32-548"}, {TRUE
, "S-1-5-32-549"},
1240 /* 32 */ {TRUE
, "S-1-5-32-550"}, {TRUE
, "S-1-5-32-551"}, {TRUE
, "S-1-5-32-552"},
1241 /* 35 */ {TRUE
, "S-1-5-32-554"}, {TRUE
, "S-1-5-32-555"}, {TRUE
, "S-1-5-32-556"},
1242 /* 38 */ {FALSE
, "S-1-5-21-12-23-34-45-56-500"}, {FALSE
, "S-1-5-21-12-23-34-45-56-501"},
1243 /* 40 */ {FALSE
, "S-1-5-21-12-23-34-45-56-502"}, {FALSE
, "S-1-5-21-12-23-34-45-56-512"},
1244 /* 42 */ {FALSE
, "S-1-5-21-12-23-34-45-56-513"}, {FALSE
, "S-1-5-21-12-23-34-45-56-514"},
1245 /* 44 */ {FALSE
, "S-1-5-21-12-23-34-45-56-515"}, {FALSE
, "S-1-5-21-12-23-34-45-56-516"},
1246 /* 46 */ {FALSE
, "S-1-5-21-12-23-34-45-56-517"}, {FALSE
, "S-1-5-21-12-23-34-45-56-518"},
1247 /* 48 */ {FALSE
, "S-1-5-21-12-23-34-45-56-519"}, {FALSE
, "S-1-5-21-12-23-34-45-56-520"},
1248 /* 50 */ {FALSE
, "S-1-5-21-12-23-34-45-56-553"},
1249 /* Added in Windows Server 2003 */
1250 /* 51 */ {TRUE
, "S-1-5-64-10"}, {TRUE
, "S-1-5-64-21"}, {TRUE
, "S-1-5-64-14"},
1251 /* 54 */ {TRUE
, "S-1-5-15"}, {TRUE
, "S-1-5-1000"}, {FALSE
, "S-1-5-32-557"},
1252 /* 57 */ {TRUE
, "S-1-5-32-558"}, {TRUE
, "S-1-5-32-559"}, {TRUE
, "S-1-5-32-560"},
1253 /* 60 */ {TRUE
, "S-1-5-32-561"}, {TRUE
, "S-1-5-32-562"},
1254 /* Added in Windows Vista: */
1255 /* 62 */ {TRUE
, "S-1-5-32-568"},
1256 /* 63 */ {TRUE
, "S-1-5-17"}, {FALSE
, "S-1-5-32-569"}, {TRUE
, "S-1-16-0"},
1257 /* 66 */ {TRUE
, "S-1-16-4096"}, {TRUE
, "S-1-16-8192"}, {TRUE
, "S-1-16-12288"},
1258 /* 69 */ {TRUE
, "S-1-16-16384"}, {TRUE
, "S-1-5-33"}, {TRUE
, "S-1-3-4"},
1259 /* 72 */ {FALSE
, "S-1-5-21-12-23-34-45-56-571"}, {FALSE
, "S-1-5-21-12-23-34-45-56-572"},
1260 /* 74 */ {TRUE
, "S-1-5-22"}, {FALSE
, "S-1-5-21-12-23-34-45-56-521"}, {TRUE
, "S-1-5-32-573"}
1263 static void test_CreateWellKnownSid()
1265 SID_IDENTIFIER_AUTHORITY ident
= { SECURITY_NT_AUTHORITY
};
1269 if (!pCreateWellKnownSid
)
1271 skip("CreateWellKnownSid not available\n");
1275 /* a domain sid usually have three subauthorities but we test that CreateWellKnownSid doesn't check it */
1276 AllocateAndInitializeSid(&ident
, 6, SECURITY_NT_NON_UNIQUE
, 12, 23, 34, 45, 56, 0, 0, &domainsid
);
1278 for (i
= 0; i
< sizeof(well_known_sid_values
)/sizeof(well_known_sid_values
[0]); i
++)
1280 struct well_known_sid_value
*value
= &well_known_sid_values
[i
];
1281 char sid_buffer
[SECURITY_MAX_SID_SIZE
];
1285 if (value
->sid_string
== NULL
)
1288 if (i
> WinAccountRasAndIasServersSid
)
1290 /* These SIDs aren't implemented by all Windows versions - detect it and break the loop */
1291 cb
= sizeof(sid_buffer
);
1292 if (!pCreateWellKnownSid(i
, domainsid
, sid_buffer
, &cb
))
1294 skip("Well known SIDs starting from %d are not implemented\n", i
);
1299 cb
= sizeof(sid_buffer
);
1300 ok(pCreateWellKnownSid(i
, value
->without_domain
? NULL
: domainsid
, sid_buffer
, &cb
), "Couldn't create well known sid %d\n", i
);
1301 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer
)), cb
, DWORD
, "%d");
1302 ok(IsValidSid(sid_buffer
), "The sid is not valid\n");
1303 ok(pConvertSidToStringSidA(sid_buffer
, &str
), "Couldn't convert SID to string\n");
1304 ok(strcmp(str
, value
->sid_string
) == 0, "SID mismatch - expected %s, got %s\n",
1305 value
->sid_string
, str
);
1308 if (value
->without_domain
)
1310 char buf2
[SECURITY_MAX_SID_SIZE
];
1312 ok(pCreateWellKnownSid(i
, domainsid
, buf2
, &cb
), "Couldn't create well known sid %d with optional domain\n", i
);
1313 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer
)), cb
, DWORD
, "%d");
1314 ok(memcmp(buf2
, sid_buffer
, cb
) == 0, "SID create with domain is different than without (%d)\n", i
);
1319 static void test_LookupAccountSid(void)
1321 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
1322 CHAR accountA
[MAX_PATH
], domainA
[MAX_PATH
];
1323 DWORD acc_sizeA
, dom_sizeA
;
1324 DWORD real_acc_sizeA
, real_dom_sizeA
;
1325 WCHAR accountW
[MAX_PATH
], domainW
[MAX_PATH
];
1326 DWORD acc_sizeW
, dom_sizeW
;
1327 DWORD real_acc_sizeW
, real_dom_sizeW
;
1328 PSID pUsersSid
= NULL
;
1336 /* native windows crashes if account size, domain size, or name use is NULL */
1338 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1339 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &pUsersSid
);
1340 ok(ret
|| (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
),
1341 "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1343 /* not running on NT so give up */
1344 if (!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
1347 real_acc_sizeA
= MAX_PATH
;
1348 real_dom_sizeA
= MAX_PATH
;
1349 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &real_acc_sizeA
, domainA
, &real_dom_sizeA
, &use
);
1350 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1352 /* try NULL account */
1353 acc_sizeA
= MAX_PATH
;
1354 dom_sizeA
= MAX_PATH
;
1355 ret
= LookupAccountSidA(NULL
, pUsersSid
, NULL
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1356 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1358 /* try NULL domain */
1359 acc_sizeA
= MAX_PATH
;
1360 dom_sizeA
= MAX_PATH
;
1361 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, NULL
, &dom_sizeA
, &use
);
1362 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1364 /* try a small account buffer */
1366 dom_sizeA
= MAX_PATH
;
1368 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1369 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1370 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1371 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1373 /* try a 0 sized account buffer */
1375 dom_sizeA
= MAX_PATH
;
1377 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1378 /* this can fail or succeed depending on OS version but the size will always be returned */
1379 ok(acc_sizeA
== real_acc_sizeA
+ 1,
1380 "LookupAccountSidA() Expected acc_size = %u, got %u\n",
1381 real_acc_sizeA
+ 1, acc_sizeA
);
1383 /* try a 0 sized account buffer */
1385 dom_sizeA
= MAX_PATH
;
1386 ret
= LookupAccountSidA(NULL
, pUsersSid
, NULL
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1387 /* this can fail or succeed depending on OS version but the size will always be returned */
1388 ok(acc_sizeA
== real_acc_sizeA
+ 1,
1389 "LookupAccountSid() Expected acc_size = %u, got %u\n",
1390 real_acc_sizeA
+ 1, acc_sizeA
);
1392 /* try a small domain buffer */
1394 acc_sizeA
= MAX_PATH
;
1396 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1397 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1398 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1399 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1401 /* try a 0 sized domain buffer */
1403 acc_sizeA
= MAX_PATH
;
1405 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1406 /* this can fail or succeed depending on OS version but the size will always be returned */
1407 ok(dom_sizeA
== real_dom_sizeA
+ 1,
1408 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1409 real_dom_sizeA
+ 1, dom_sizeA
);
1411 /* try a 0 sized domain buffer */
1413 acc_sizeA
= MAX_PATH
;
1414 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, NULL
, &dom_sizeA
, &use
);
1415 /* this can fail or succeed depending on OS version but the size will always be returned */
1416 ok(dom_sizeA
== real_dom_sizeA
+ 1,
1417 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1418 real_dom_sizeA
+ 1, dom_sizeA
);
1420 real_acc_sizeW
= MAX_PATH
;
1421 real_dom_sizeW
= MAX_PATH
;
1422 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &real_acc_sizeW
, domainW
, &real_dom_sizeW
, &use
);
1423 ok(ret
, "LookupAccountSidW() Expected TRUE, got FALSE\n");
1425 /* try an invalid system name */
1426 real_acc_sizeA
= MAX_PATH
;
1427 real_dom_sizeA
= MAX_PATH
;
1428 ret
= LookupAccountSidA("deepthought", pUsersSid
, accountA
, &real_acc_sizeA
, domainA
, &real_dom_sizeA
, &use
);
1429 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1430 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE
|| GetLastError() == RPC_S_INVALID_NET_ADDR
/* Vista */,
1431 "LookupAccountSidA() Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %u\n", GetLastError());
1433 /* native windows crashes if domainW or accountW is NULL */
1435 /* try a small account buffer */
1437 dom_sizeW
= MAX_PATH
;
1439 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1440 ok(!ret
, "LookupAccountSidW() Expected FALSE got TRUE\n");
1441 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1442 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1444 /* try a 0 sized account buffer */
1446 dom_sizeW
= MAX_PATH
;
1448 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1449 /* this can fail or succeed depending on OS version but the size will always be returned */
1450 ok(acc_sizeW
== real_acc_sizeW
+ 1,
1451 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1452 real_acc_sizeW
+ 1, acc_sizeW
);
1454 /* try a 0 sized account buffer */
1456 dom_sizeW
= MAX_PATH
;
1457 ret
= LookupAccountSidW(NULL
, pUsersSid
, NULL
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1458 /* this can fail or succeed depending on OS version but the size will always be returned */
1459 ok(acc_sizeW
== real_acc_sizeW
+ 1,
1460 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1461 real_acc_sizeW
+ 1, acc_sizeW
);
1463 /* try a small domain buffer */
1465 acc_sizeW
= MAX_PATH
;
1467 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1468 ok(!ret
, "LookupAccountSidW() Expected FALSE got TRUE\n");
1469 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1470 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1472 /* try a 0 sized domain buffer */
1474 acc_sizeW
= MAX_PATH
;
1476 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1477 /* this can fail or succeed depending on OS version but the size will always be returned */
1478 ok(dom_sizeW
== real_dom_sizeW
+ 1,
1479 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1480 real_dom_sizeW
+ 1, dom_sizeW
);
1482 /* try a 0 sized domain buffer */
1484 acc_sizeW
= MAX_PATH
;
1485 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, NULL
, &dom_sizeW
, &use
);
1486 /* this can fail or succeed depending on OS version but the size will always be returned */
1487 ok(dom_sizeW
== real_dom_sizeW
+ 1,
1488 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1489 real_dom_sizeW
+ 1, dom_sizeW
);
1493 if (pCreateWellKnownSid
&& pConvertSidToStringSidA
)
1495 trace("Well Known SIDs:\n");
1496 for (i
= 0; i
<= 60; i
++)
1498 size
= SECURITY_MAX_SID_SIZE
;
1499 if (pCreateWellKnownSid(i
, NULL
, &max_sid
.sid
, &size
))
1501 if (pConvertSidToStringSidA(&max_sid
.sid
, &str_sidA
))
1503 acc_sizeA
= MAX_PATH
;
1504 dom_sizeA
= MAX_PATH
;
1505 if (LookupAccountSidA(NULL
, &max_sid
.sid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
))
1506 trace(" %d: %s %s\\%s %d\n", i
, str_sidA
, domainA
, accountA
, use
);
1507 LocalFree(str_sidA
);
1512 if (GetLastError() != ERROR_INVALID_PARAMETER
)
1513 trace(" CreateWellKnownSid(%d) failed: %d\n", i
, GetLastError());
1515 trace(" %d: not supported\n", i
);
1519 pLsaQueryInformationPolicy
= (fnLsaQueryInformationPolicy
)GetProcAddress( hmod
, "LsaQueryInformationPolicy");
1520 pLsaOpenPolicy
= (fnLsaOpenPolicy
)GetProcAddress( hmod
, "LsaOpenPolicy");
1521 pLsaFreeMemory
= (fnLsaFreeMemory
)GetProcAddress( hmod
, "LsaFreeMemory");
1522 pLsaClose
= (fnLsaClose
)GetProcAddress( hmod
, "LsaClose");
1524 if (pLsaQueryInformationPolicy
&& pLsaOpenPolicy
&& pLsaFreeMemory
&& pLsaClose
)
1528 LSA_OBJECT_ATTRIBUTES object_attributes
;
1530 ZeroMemory(&object_attributes
, sizeof(object_attributes
));
1531 object_attributes
.Length
= sizeof(object_attributes
);
1533 status
= pLsaOpenPolicy( NULL
, &object_attributes
, POLICY_ALL_ACCESS
, &handle
);
1534 ok(status
== STATUS_SUCCESS
|| status
== STATUS_ACCESS_DENIED
,
1535 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status
);
1537 /* try a more restricted access mask if necessary */
1538 if (status
== STATUS_ACCESS_DENIED
) {
1539 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
1540 status
= pLsaOpenPolicy( NULL
, &object_attributes
, POLICY_VIEW_LOCAL_INFORMATION
, &handle
);
1541 ok(status
== STATUS_SUCCESS
, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status
);
1544 if (status
== STATUS_SUCCESS
)
1546 PPOLICY_ACCOUNT_DOMAIN_INFO info
;
1547 status
= pLsaQueryInformationPolicy(handle
, PolicyAccountDomainInformation
, (PVOID
*)&info
);
1548 ok(status
== STATUS_SUCCESS
, "LsaQueryInformationPolicy() failed, returned 0x%08x\n", status
);
1549 if (status
== STATUS_SUCCESS
)
1551 ok(info
->DomainSid
!=0, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) missing SID\n");
1552 if (info
->DomainSid
)
1554 int count
= *GetSidSubAuthorityCount(info
->DomainSid
);
1555 CopySid(GetSidLengthRequired(count
), &max_sid
, info
->DomainSid
);
1556 test_sid_str((PSID
)&max_sid
.sid
);
1557 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_USER_RID_ADMIN
;
1558 max_sid
.sid
.SubAuthorityCount
= count
+ 1;
1559 test_sid_str((PSID
)&max_sid
.sid
);
1560 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_USER_RID_GUEST
;
1561 test_sid_str((PSID
)&max_sid
.sid
);
1562 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_ADMINS
;
1563 test_sid_str((PSID
)&max_sid
.sid
);
1564 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_USERS
;
1565 test_sid_str((PSID
)&max_sid
.sid
);
1566 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_GUESTS
;
1567 test_sid_str((PSID
)&max_sid
.sid
);
1568 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_COMPUTERS
;
1569 test_sid_str((PSID
)&max_sid
.sid
);
1570 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_CONTROLLERS
;
1571 test_sid_str((PSID
)&max_sid
.sid
);
1572 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_CERT_ADMINS
;
1573 test_sid_str((PSID
)&max_sid
.sid
);
1574 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_SCHEMA_ADMINS
;
1575 test_sid_str((PSID
)&max_sid
.sid
);
1576 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_ENTERPRISE_ADMINS
;
1577 test_sid_str((PSID
)&max_sid
.sid
);
1578 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_POLICY_ADMINS
;
1579 test_sid_str((PSID
)&max_sid
.sid
);
1580 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_ALIAS_RID_RAS_SERVERS
;
1581 test_sid_str((PSID
)&max_sid
.sid
);
1582 max_sid
.sid
.SubAuthority
[count
] = 1000; /* first user account */
1583 test_sid_str((PSID
)&max_sid
.sid
);
1586 pLsaFreeMemory((LPVOID
)info
);
1589 status
= pLsaClose(handle
);
1590 ok(status
== STATUS_SUCCESS
, "LsaClose() failed, returned 0x%08x\n", status
);
1596 static void get_sid_info(PSID psid
, LPSTR
*user
, LPSTR
*dom
)
1598 static CHAR account
[UNLEN
+ 1];
1599 static CHAR domain
[UNLEN
+ 1];
1600 DWORD size
, dom_size
;
1606 size
= dom_size
= UNLEN
+ 1;
1609 LookupAccountSidA(NULL
, psid
, account
, &size
, domain
, &dom_size
, &use
);
1612 static void test_LookupAccountName(void)
1614 DWORD sid_size
, domain_size
, user_size
;
1615 DWORD sid_save
, domain_save
;
1616 CHAR user_name
[UNLEN
+ 1];
1617 SID_NAME_USE sid_use
;
1618 LPSTR domain
, account
, sid_dom
;
1622 /* native crashes if (assuming all other parameters correct):
1624 * - Sid is NULL and cbSid is > 0
1625 * - cbSid or cchReferencedDomainName are NULL
1626 * - ReferencedDomainName is NULL and cchReferencedDomainName is the correct size
1629 user_size
= UNLEN
+ 1;
1630 SetLastError(0xdeadbeef);
1631 ret
= GetUserNameA(user_name
, &user_size
);
1632 if (!ret
&& (GetLastError() == ERROR_NOT_LOGGED_ON
))
1634 /* Probably on win9x where the user used 'Cancel' instead of properly logging in */
1635 skip("Cannot get the user name (win9x and not logged in properly)\n");
1638 ok(ret
, "Failed to get user name : %d\n", GetLastError());
1643 sid_use
= 0xcafebabe;
1644 SetLastError(0xdeadbeef);
1645 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
1646 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
1648 skip("LookupAccountNameA is not implemented\n");
1651 ok(!ret
, "Expected 0, got %d\n", ret
);
1652 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1653 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1654 ok(sid_size
!= 0, "Expected non-zero sid size\n");
1655 ok(domain_size
!= 0, "Expected non-zero domain size\n");
1656 ok(sid_use
== 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use
);
1658 sid_save
= sid_size
;
1659 domain_save
= domain_size
;
1661 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
1662 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
1664 /* try valid account name */
1665 ret
= LookupAccountNameA(NULL
, user_name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
1666 get_sid_info(psid
, &account
, &sid_dom
);
1667 ok(ret
, "Failed to lookup account name\n");
1668 ok(sid_size
== GetLengthSid(psid
), "Expected %d, got %d\n", GetLengthSid(psid
), sid_size
);
1671 ok(!lstrcmp(account
, user_name
), "Expected %s, got %s\n", user_name
, account
);
1672 ok(!lstrcmp(domain
, sid_dom
), "Expected %s, got %s\n", sid_dom
, domain
);
1674 ok(domain_size
== domain_save
- 1, "Expected %d, got %d\n", domain_save
- 1, domain_size
);
1675 ok(lstrlen(domain
) == domain_size
, "Expected %d, got %d\n", lstrlen(domain
), domain_size
);
1676 ok(sid_use
== SidTypeUser
, "Expected SidTypeUser (%d), got %d\n", SidTypeUser
, sid_use
);
1677 domain_size
= domain_save
;
1678 sid_size
= sid_save
;
1680 if (PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale())) != LANG_ENGLISH
)
1682 skip("Non-english locale (test with hardcoded 'Everyone')\n");
1686 ret
= LookupAccountNameA(NULL
, "Everyone", psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
1687 get_sid_info(psid
, &account
, &sid_dom
);
1688 ok(ret
, "Failed to lookup account name\n");
1689 ok(sid_size
!= 0, "sid_size was zero\n");
1690 ok(!lstrcmp(account
, "Everyone"), "Expected Everyone, got %s\n", account
);
1691 ok(!lstrcmp(domain
, sid_dom
), "Expected %s, got %s\n", sid_dom
, domain
);
1692 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
1693 ok(lstrlen(domain
) == domain_size
, "Expected %d, got %d\n", lstrlen(domain
), domain_size
);
1694 ok(sid_use
== SidTypeWellKnownGroup
, "Expected SidTypeWellKnownGroup (%d), got %d\n", SidTypeWellKnownGroup
, sid_use
);
1695 domain_size
= domain_save
;
1698 /* NULL Sid with zero sid size */
1699 SetLastError(0xdeadbeef);
1701 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, domain
, &domain_size
, &sid_use
);
1702 ok(!ret
, "Expected 0, got %d\n", ret
);
1703 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1704 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1705 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
1706 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
1708 /* try cchReferencedDomainName - 1 */
1709 SetLastError(0xdeadbeef);
1711 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, domain
, &domain_size
, &sid_use
);
1712 ok(!ret
, "Expected 0, got %d\n", ret
);
1713 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1714 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1715 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
1716 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
1718 /* NULL ReferencedDomainName with zero domain name size */
1719 SetLastError(0xdeadbeef);
1721 ret
= LookupAccountNameA(NULL
, user_name
, psid
, &sid_size
, NULL
, &domain_size
, &sid_use
);
1722 ok(!ret
, "Expected 0, got %d\n", ret
);
1723 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1724 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1725 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
1726 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
1728 HeapFree(GetProcessHeap(), 0, psid
);
1729 HeapFree(GetProcessHeap(), 0, domain
);
1731 /* get sizes for NULL account name */
1734 sid_use
= 0xcafebabe;
1735 SetLastError(0xdeadbeef);
1736 ret
= LookupAccountNameA(NULL
, NULL
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
1737 ok(!ret
, "Expected 0, got %d\n", ret
);
1738 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1739 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1740 ok(sid_size
!= 0, "Expected non-zero sid size\n");
1741 ok(domain_size
!= 0, "Expected non-zero domain size\n");
1742 ok(sid_use
== 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use
);
1744 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
1745 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
1747 /* try NULL account name */
1748 ret
= LookupAccountNameA(NULL
, NULL
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
1749 get_sid_info(psid
, &account
, &sid_dom
);
1750 ok(ret
, "Failed to lookup account name\n");
1751 /* Using a fixed string will not work on different locales */
1752 ok(!lstrcmp(account
, domain
),
1753 "Got %s for account and %s for domain, these should be the same\n",
1755 ok(sid_use
== SidTypeDomain
, "Expected SidTypeDomain (%d), got %d\n", SidTypeDomain
, sid_use
);
1757 /* try an invalid account name */
1758 SetLastError(0xdeadbeef);
1761 ret
= LookupAccountNameA(NULL
, "oogabooga", NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
1762 ok(!ret
, "Expected 0, got %d\n", ret
);
1763 ok(GetLastError() == ERROR_NONE_MAPPED
||
1764 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE
),
1765 "Expected ERROR_NONE_MAPPED, got %d\n", GetLastError());
1766 ok(sid_size
== 0, "Expected 0, got %d\n", sid_size
);
1767 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
1769 /* try an invalid system name */
1770 SetLastError(0xdeadbeef);
1773 ret
= LookupAccountNameA("deepthought", NULL
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
1774 ok(!ret
, "Expected 0, got %d\n", ret
);
1775 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE
|| GetLastError() == RPC_S_INVALID_NET_ADDR
/* Vista */,
1776 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
1777 ok(sid_size
== 0, "Expected 0, got %d\n", sid_size
);
1778 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
1780 HeapFree(GetProcessHeap(), 0, psid
);
1781 HeapFree(GetProcessHeap(), 0, domain
);
1784 static void test_security_descriptor(void)
1786 SECURITY_DESCRIPTOR sd
;
1789 BOOL isDefault
, isPresent
, ret
;
1793 SetLastError(0xdeadbeef);
1794 ret
= InitializeSecurityDescriptor(&sd
, SECURITY_DESCRIPTOR_REVISION
);
1795 if (ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
1797 skip("InitializeSecurityDescriptor is not implemented\n");
1801 ok(GetSecurityDescriptorOwner(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorOwner failed\n");
1802 expect_eq(psid
, NULL
, PSID
, "%p");
1803 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
1804 sd
.Control
|= SE_DACL_PRESENT
| SE_SACL_PRESENT
;
1806 SetLastError(0xdeadbeef);
1808 expect_eq(MakeSelfRelativeSD(&sd
, buf
, &size
), FALSE
, BOOL
, "%d");
1809 expect_eq(GetLastError(), ERROR_INSUFFICIENT_BUFFER
, DWORD
, "%u");
1810 ok(size
> 5, "Size not increased\n");
1813 expect_eq(MakeSelfRelativeSD(&sd
, buf
, &size
), TRUE
, BOOL
, "%d");
1814 ok(GetSecurityDescriptorOwner(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorOwner failed\n");
1815 expect_eq(psid
, NULL
, PSID
, "%p");
1816 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
1817 ok(GetSecurityDescriptorGroup(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorGroup failed\n");
1818 expect_eq(psid
, NULL
, PSID
, "%p");
1819 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
1820 ok(GetSecurityDescriptorDacl(&sd
, &isPresent
, &pacl
, &isDefault
), "GetSecurityDescriptorDacl failed\n");
1821 expect_eq(isPresent
, TRUE
, BOOL
, "%d");
1822 expect_eq(psid
, NULL
, PSID
, "%p");
1823 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
1824 ok(GetSecurityDescriptorSacl(&sd
, &isPresent
, &pacl
, &isDefault
), "GetSecurityDescriptorSacl failed\n");
1825 expect_eq(isPresent
, TRUE
, BOOL
, "%d");
1826 expect_eq(psid
, NULL
, PSID
, "%p");
1827 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
1831 #define TEST_GRANTED_ACCESS(a,b) test_granted_access(a,b,0,__LINE__)
1832 #define TEST_GRANTED_ACCESS2(a,b,c) test_granted_access(a,b,c,__LINE__)
1833 static void test_granted_access(HANDLE handle
, ACCESS_MASK access
,
1834 ACCESS_MASK alt
, int line
)
1836 OBJECT_BASIC_INFORMATION obj_info
;
1839 if (!pNtQueryObject
)
1841 skip_(__FILE__
, line
)("Not NT platform - skipping tests\n");
1845 status
= pNtQueryObject( handle
, ObjectBasicInformation
, &obj_info
,
1846 sizeof(obj_info
), NULL
);
1847 ok_(__FILE__
, line
)(!status
, "NtQueryObject with err: %08x\n", status
);
1849 ok_(__FILE__
, line
)(obj_info
.GrantedAccess
== access
||
1850 obj_info
.GrantedAccess
== alt
, "Granted access should be 0x%08x "
1851 "or 0x%08x, instead of 0x%08x\n", access
, alt
, obj_info
.GrantedAccess
);
1853 ok_(__FILE__
, line
)(obj_info
.GrantedAccess
== access
, "Granted access should "
1854 "be 0x%08x, instead of 0x%08x\n", access
, obj_info
.GrantedAccess
);
1857 #define CHECK_SET_SECURITY(o,i,e) \
1861 SetLastError( 0xdeadbeef ); \
1862 res = SetKernelObjectSecurity( o, i, SecurityDescriptor ); \
1863 err = GetLastError(); \
1864 if (e == ERROR_SUCCESS) \
1865 ok(res, "SetKernelObjectSecurity failed with %d\n", err); \
1867 ok(!res && err == e, "SetKernelObjectSecurity should have failed " \
1868 "with %s, instead of %d\n", #e, err); \
1871 static void test_process_security(void)
1874 char owner
[32], group
[32];
1875 PSID AdminSid
= NULL
, UsersSid
= NULL
;
1877 SECURITY_DESCRIPTOR
*SecurityDescriptor
= NULL
;
1878 char buffer
[MAX_PATH
];
1879 PROCESS_INFORMATION info
;
1880 STARTUPINFOA startup
;
1881 SECURITY_ATTRIBUTES psa
;
1882 HANDLE token
, event
;
1885 Acl
= HeapAlloc(GetProcessHeap(), 0, 256);
1886 res
= InitializeAcl(Acl
, 256, ACL_REVISION
);
1887 if (!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
1889 skip("ACLs not implemented - skipping tests\n");
1890 HeapFree(GetProcessHeap(), 0, Acl
);
1893 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
1895 /* get owner from the token we might be running as a user not admin */
1896 res
= OpenProcessToken( GetCurrentProcess(), MAXIMUM_ALLOWED
, &token
);
1897 ok(res
, "OpenProcessToken failed with error %d\n", GetLastError());
1900 HeapFree(GetProcessHeap(), 0, Acl
);
1904 res
= GetTokenInformation( token
, TokenOwner
, owner
, sizeof(owner
), &tmp
);
1905 ok(res
, "GetTokenInformation failed with error %d\n", GetLastError());
1906 AdminSid
= ((TOKEN_OWNER
*)owner
)->Owner
;
1907 res
= GetTokenInformation( token
, TokenPrimaryGroup
, group
, sizeof(group
), &tmp
);
1908 ok(res
, "GetTokenInformation failed with error %d\n", GetLastError());
1909 UsersSid
= ((TOKEN_PRIMARY_GROUP
*)group
)->PrimaryGroup
;
1911 CloseHandle( token
);
1914 HeapFree(GetProcessHeap(), 0, Acl
);
1918 res
= AddAccessDeniedAce(Acl
, ACL_REVISION
, PROCESS_VM_READ
, AdminSid
);
1919 ok(res
, "AddAccessDeniedAce failed with error %d\n", GetLastError());
1920 res
= AddAccessAllowedAce(Acl
, ACL_REVISION
, PROCESS_ALL_ACCESS
, AdminSid
);
1921 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
1923 SecurityDescriptor
= HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
1924 res
= InitializeSecurityDescriptor(SecurityDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
1925 ok(res
, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
1927 event
= CreateEvent( NULL
, TRUE
, TRUE
, "test_event" );
1928 ok(event
!= NULL
, "CreateEvent %d\n", GetLastError());
1930 SecurityDescriptor
->Revision
= 0;
1931 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_UNKNOWN_REVISION
);
1932 SecurityDescriptor
->Revision
= SECURITY_DESCRIPTOR_REVISION
;
1934 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_INVALID_SECURITY_DESCR
);
1935 CHECK_SET_SECURITY( event
, GROUP_SECURITY_INFORMATION
, ERROR_INVALID_SECURITY_DESCR
);
1936 CHECK_SET_SECURITY( event
, SACL_SECURITY_INFORMATION
, ERROR_ACCESS_DENIED
);
1937 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
1938 /* NULL DACL is valid and means default DACL from token */
1939 SecurityDescriptor
->Control
|= SE_DACL_PRESENT
;
1940 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
1942 /* Set owner and group and dacl */
1943 res
= SetSecurityDescriptorOwner(SecurityDescriptor
, AdminSid
, FALSE
);
1944 ok(res
, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
1945 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_SUCCESS
);
1946 res
= SetSecurityDescriptorGroup(SecurityDescriptor
, UsersSid
, FALSE
);
1947 ok(res
, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
1948 CHECK_SET_SECURITY( event
, GROUP_SECURITY_INFORMATION
, ERROR_SUCCESS
);
1949 res
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
1950 ok(res
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1951 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
1953 sprintf(buffer
, "%s tests/security.c test", myARGV
[0]);
1954 memset(&startup
, 0, sizeof(startup
));
1955 startup
.cb
= sizeof(startup
);
1956 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
1957 startup
.wShowWindow
= SW_SHOWNORMAL
;
1959 psa
.nLength
= sizeof(psa
);
1960 psa
.lpSecurityDescriptor
= SecurityDescriptor
;
1961 psa
.bInheritHandle
= TRUE
;
1963 /* Doesn't matter what ACL say we should get full access for ourselves */
1964 ok(CreateProcessA( NULL
, buffer
, &psa
, NULL
, FALSE
, 0, NULL
, NULL
, &startup
, &info
),
1965 "CreateProcess with err:%d\n", GetLastError());
1966 TEST_GRANTED_ACCESS2( info
.hProcess
, PROCESS_ALL_ACCESS
,
1967 STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
);
1968 winetest_wait_child_process( info
.hProcess
);
1970 CloseHandle( info
.hProcess
);
1971 CloseHandle( info
.hThread
);
1972 CloseHandle( event
);
1973 HeapFree(GetProcessHeap(), 0, Acl
);
1974 HeapFree(GetProcessHeap(), 0, SecurityDescriptor
);
1977 static void test_process_security_child(void)
1979 HANDLE handle
, handle1
;
1983 handle
= OpenProcess( PROCESS_TERMINATE
, FALSE
, GetCurrentProcessId() );
1984 ok(handle
!= NULL
, "OpenProcess(PROCESS_TERMINATE) with err:%d\n", GetLastError());
1985 TEST_GRANTED_ACCESS( handle
, PROCESS_TERMINATE
);
1987 ok(DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
1988 &handle1
, 0, TRUE
, DUPLICATE_SAME_ACCESS
),
1989 "duplicating handle err:%d\n", GetLastError());
1990 TEST_GRANTED_ACCESS( handle1
, PROCESS_TERMINATE
);
1992 CloseHandle( handle1
);
1994 SetLastError( 0xdeadbeef );
1995 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
1996 &handle1
, PROCESS_ALL_ACCESS
, TRUE
, 0 );
1997 err
= GetLastError();
1999 ok(!ret
&& err
== ERROR_ACCESS_DENIED
, "duplicating handle should have failed "
2000 "with STATUS_ACCESS_DENIED, instead of err:%d\n", err
);
2002 CloseHandle( handle
);
2004 /* These two should fail - they are denied by ACL */
2005 handle
= OpenProcess( PROCESS_VM_READ
, FALSE
, GetCurrentProcessId() );
2007 ok(handle
== NULL
, "OpenProcess(PROCESS_VM_READ) should have failed\n");
2008 handle
= OpenProcess( PROCESS_ALL_ACCESS
, FALSE
, GetCurrentProcessId() );
2010 ok(handle
== NULL
, "OpenProcess(PROCESS_ALL_ACCESS) should have failed\n");
2012 /* Documented privilege elevation */
2013 ok(DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2014 &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
),
2015 "duplicating handle err:%d\n", GetLastError());
2016 TEST_GRANTED_ACCESS2( handle
, PROCESS_ALL_ACCESS
,
2017 STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
);
2019 CloseHandle( handle
);
2021 /* Same only explicitly asking for all access rights */
2022 ok(DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2023 &handle
, PROCESS_ALL_ACCESS
, TRUE
, 0 ),
2024 "duplicating handle err:%d\n", GetLastError());
2025 TEST_GRANTED_ACCESS2( handle
, PROCESS_ALL_ACCESS
,
2026 PROCESS_ALL_ACCESS
| PROCESS_QUERY_LIMITED_INFORMATION
);
2027 ok(DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2028 &handle1
, PROCESS_VM_READ
, TRUE
, 0 ),
2029 "duplicating handle err:%d\n", GetLastError());
2030 TEST_GRANTED_ACCESS( handle1
, PROCESS_VM_READ
);
2031 CloseHandle( handle1
);
2032 CloseHandle( handle
);
2035 static void test_impersonation_level(void)
2037 HANDLE Token
, ProcessToken
;
2040 TOKEN_PRIVILEGES
*Privileges
;
2042 PRIVILEGE_SET
*PrivilegeSet
;
2048 pDuplicateTokenEx
= (fnDuplicateTokenEx
) GetProcAddress(hmod
, "DuplicateTokenEx");
2049 if( !pDuplicateTokenEx
) {
2050 skip("DuplicateTokenEx is not available\n");
2053 SetLastError(0xdeadbeef);
2054 ret
= ImpersonateSelf(SecurityAnonymous
);
2055 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
2057 skip("ImpersonateSelf is not implemented\n");
2060 ok(ret
, "ImpersonateSelf(SecurityAnonymous) failed with error %d\n", GetLastError());
2061 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2062 ok(!ret
, "OpenThreadToken should have failed\n");
2063 error
= GetLastError();
2064 ok(error
== ERROR_CANT_OPEN_ANONYMOUS
, "OpenThreadToken on anonymous token should have returned ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error
);
2065 /* can't perform access check when opening object against an anonymous impersonation token */
2067 error
= RegOpenKeyEx(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2068 ok(error
== ERROR_INVALID_HANDLE
, "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE instead of %d\n", error
);
2072 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
, &ProcessToken
);
2073 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
2075 ret
= pDuplicateTokenEx(ProcessToken
,
2076 TOKEN_QUERY
| TOKEN_DUPLICATE
| TOKEN_IMPERSONATE
, NULL
,
2077 SecurityAnonymous
, TokenImpersonation
, &Token
);
2078 ok(ret
, "DuplicateTokenEx failed with error %d\n", GetLastError());
2079 /* can't increase the impersonation level */
2080 ret
= DuplicateToken(Token
, SecurityIdentification
, &Token2
);
2081 error
= GetLastError();
2082 ok(!ret
&& error
== ERROR_BAD_IMPERSONATION_LEVEL
,
2083 "Duplicating a token and increasing the impersonation level should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2084 /* we can query anything from an anonymous token, including the user */
2085 ret
= GetTokenInformation(Token
, TokenUser
, NULL
, 0, &Size
);
2086 error
= GetLastError();
2087 ok(!ret
&& error
== ERROR_INSUFFICIENT_BUFFER
, "GetTokenInformation(TokenUser) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error
);
2088 User
= HeapAlloc(GetProcessHeap(), 0, Size
);
2089 ret
= GetTokenInformation(Token
, TokenUser
, User
, Size
, &Size
);
2090 ok(ret
, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
2091 HeapFree(GetProcessHeap(), 0, User
);
2093 /* PrivilegeCheck fails with SecurityAnonymous level */
2094 ret
= GetTokenInformation(Token
, TokenPrivileges
, NULL
, 0, &Size
);
2095 error
= GetLastError();
2096 ok(!ret
&& error
== ERROR_INSUFFICIENT_BUFFER
, "GetTokenInformation(TokenPrivileges) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error
);
2097 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
2098 ret
= GetTokenInformation(Token
, TokenPrivileges
, Privileges
, Size
, &Size
);
2099 ok(ret
, "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
2101 PrivilegeSet
= HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PRIVILEGE_SET
, Privilege
[Privileges
->PrivilegeCount
]));
2102 PrivilegeSet
->PrivilegeCount
= Privileges
->PrivilegeCount
;
2103 memcpy(PrivilegeSet
->Privilege
, Privileges
->Privileges
, PrivilegeSet
->PrivilegeCount
* sizeof(PrivilegeSet
->Privilege
[0]));
2104 PrivilegeSet
->Control
= PRIVILEGE_SET_ALL_NECESSARY
;
2105 HeapFree(GetProcessHeap(), 0, Privileges
);
2107 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2108 error
= GetLastError();
2109 ok(!ret
&& error
== ERROR_BAD_IMPERSONATION_LEVEL
, "PrivilegeCheck for SecurityAnonymous token should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2113 ret
= ImpersonateSelf(SecurityIdentification
);
2114 ok(ret
, "ImpersonateSelf(SecurityIdentification) failed with error %d\n", GetLastError());
2115 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2116 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
2118 /* can't perform access check when opening object against an identification impersonation token */
2119 error
= RegOpenKeyEx(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2121 ok(error
== ERROR_INVALID_HANDLE
, "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE instead of %d\n", error
);
2123 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2124 ok(ret
, "PrivilegeCheck for SecurityIdentification failed with error %d\n", GetLastError());
2128 ret
= ImpersonateSelf(SecurityImpersonation
);
2129 ok(ret
, "ImpersonateSelf(SecurityImpersonation) failed with error %d\n", GetLastError());
2130 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2131 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
2132 error
= RegOpenKeyEx(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2133 ok(error
== ERROR_SUCCESS
, "RegOpenKeyEx should have succeeded instead of failing with %d\n", error
);
2135 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2136 ok(ret
, "PrivilegeCheck for SecurityImpersonation failed with error %d\n", GetLastError());
2140 CloseHandle(ProcessToken
);
2142 HeapFree(GetProcessHeap(), 0, PrivilegeSet
);
2145 static void test_SetEntriesInAcl(void)
2148 PSID EveryoneSid
= NULL
, UsersSid
= NULL
;
2149 PACL OldAcl
= NULL
, NewAcl
;
2150 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
2151 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
2152 EXPLICIT_ACCESSW ExplicitAccess
;
2153 static const WCHAR wszEveryone
[] = {'E','v','e','r','y','o','n','e',0};
2155 if (!pSetEntriesInAclW
)
2157 skip("SetEntriesInAclW is not available\n");
2161 NewAcl
= (PACL
)0xdeadbeef;
2162 res
= pSetEntriesInAclW(0, NULL
, NULL
, &NewAcl
);
2163 if(res
== ERROR_CALL_NOT_IMPLEMENTED
)
2165 skip("SetEntriesInAclW is not implemented\n");
2168 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2169 ok(NewAcl
== NULL
, "NewAcl=%p, expected NULL\n", NewAcl
);
2171 OldAcl
= HeapAlloc(GetProcessHeap(), 0, 256);
2172 res
= InitializeAcl(OldAcl
, 256, ACL_REVISION
);
2173 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2175 skip("ACLs not implemented - skipping tests\n");
2176 HeapFree(GetProcessHeap(), 0, OldAcl
);
2179 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
2181 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
2182 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2184 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
2185 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
2186 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2188 res
= AddAccessAllowedAce(OldAcl
, ACL_REVISION
, KEY_READ
, UsersSid
);
2189 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2191 ExplicitAccess
.grfAccessPermissions
= KEY_WRITE
;
2192 ExplicitAccess
.grfAccessMode
= GRANT_ACCESS
;
2193 ExplicitAccess
.grfInheritance
= NO_INHERITANCE
;
2194 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
2195 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2196 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2197 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2198 ExplicitAccess
.Trustee
.ptstrName
= (LPWSTR
)EveryoneSid
;
2199 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2200 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2201 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2204 if (PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale())) != LANG_ENGLISH
)
2206 skip("Non-english locale (test with hardcoded 'Everyone')\n");
2210 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_USER
;
2211 ExplicitAccess
.Trustee
.ptstrName
= (LPWSTR
)wszEveryone
;
2212 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2213 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2214 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2217 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_BAD_FORM
;
2218 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2219 ok(res
== ERROR_INVALID_PARAMETER
, "SetEntriesInAclW failed: %u\n", res
);
2220 ok(NewAcl
== NULL
, "returned acl wasn't NULL: %p\n", NewAcl
);
2223 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_USER
;
2224 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= TRUSTEE_IS_IMPERSONATE
;
2225 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2226 ok(res
== ERROR_INVALID_PARAMETER
, "SetEntriesInAclW failed: %u\n", res
);
2227 ok(NewAcl
== NULL
, "returned acl wasn't NULL: %p\n", NewAcl
);
2230 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2231 ExplicitAccess
.grfAccessMode
= SET_ACCESS
;
2232 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2233 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2234 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2238 ExplicitAccess
.grfAccessMode
= REVOKE_ACCESS
;
2239 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2240 ExplicitAccess
.Trustee
.ptstrName
= (LPWSTR
)UsersSid
;
2241 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2242 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2243 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2246 LocalFree(UsersSid
);
2247 LocalFree(EveryoneSid
);
2248 HeapFree(GetProcessHeap(), 0, OldAcl
);
2251 static void test_GetNamedSecurityInfoA(void)
2253 PSECURITY_DESCRIPTOR pSecDesc
;
2255 SECURITY_DESCRIPTOR_CONTROL control
;
2258 BOOL owner_defaulted
;
2259 BOOL group_defaulted
;
2262 CHAR windows_dir
[MAX_PATH
];
2264 if (!pGetNamedSecurityInfoA
)
2266 skip("GetNamedSecurityInfoA is not available\n");
2270 ret
= GetWindowsDirectoryA(windows_dir
, MAX_PATH
);
2271 ok(ret
, "GetWindowsDirectory failed with error %d\n", GetLastError());
2273 SetLastError(0xdeadbeef);
2274 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,
2275 OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
2276 NULL
, NULL
, NULL
, NULL
, &pSecDesc
);
2277 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
2279 skip("GetNamedSecurityInfoA is not implemented\n");
2282 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
2284 ret
= GetSecurityDescriptorControl(pSecDesc
, &control
, &revision
);
2285 ok(ret
, "GetSecurityDescriptorControl failed with error %d\n", GetLastError());
2286 ok((control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == (SE_SELF_RELATIVE
|SE_DACL_PRESENT
),
2287 "control (0x%x) doesn't have (SE_SELF_RELATIVE|SE_DACL_PRESENT) flags set\n", control
);
2288 ok(revision
== SECURITY_DESCRIPTOR_REVISION1
, "revision was %d instead of 1\n", revision
);
2289 ret
= GetSecurityDescriptorOwner(pSecDesc
, &owner
, &owner_defaulted
);
2290 ok(ret
, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
2291 ok(owner
!= NULL
, "owner should not be NULL\n");
2292 ret
= GetSecurityDescriptorGroup(pSecDesc
, &group
, &group_defaulted
);
2293 ok(ret
, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2294 ok(group
!= NULL
, "group should not be NULL\n");
2297 static void test_ConvertStringSecurityDescriptor(void)
2300 PSECURITY_DESCRIPTOR pSD
;
2301 static const WCHAR Blank
[] = { 0 };
2303 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA
)
2305 skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
2309 SetLastError(0xdeadbeef);
2310 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
2311 "D:(A;;GA;;;WD)", 0xdeadbeef, &pSD
, NULL
);
2312 ok(!ret
&& GetLastError() == ERROR_UNKNOWN_REVISION
,
2313 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_UNKNOWN_REVISION instead of %d\n",
2316 /* test ACE string type */
2317 SetLastError(0xdeadbeef);
2318 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
2319 "D:(A;;GA;;;WD)", SDDL_REVISION_1
, &pSD
, NULL
);
2320 ok(ret
, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2323 SetLastError(0xdeadbeef);
2324 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
2325 "D:(D;;GA;;;WD)", SDDL_REVISION_1
, &pSD
, NULL
);
2326 ok(ret
, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2329 SetLastError(0xdeadbeef);
2330 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
2331 "ERROR:(D;;GA;;;WD)", SDDL_REVISION_1
, &pSD
, NULL
);
2332 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
2333 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2336 /* test ACE string access rights */
2337 SetLastError(0xdeadbeef);
2338 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
2339 "D:(A;;GA;;;WD)", SDDL_REVISION_1
, &pSD
, NULL
);
2340 ok(ret
, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2342 SetLastError(0xdeadbeef);
2343 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
2344 "D:(A;;GRGWGX;;;WD)", SDDL_REVISION_1
, &pSD
, NULL
);
2345 ok(ret
, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2347 SetLastError(0xdeadbeef);
2348 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
2349 "D:(A;;RCSDWDWO;;;WD)", SDDL_REVISION_1
, &pSD
, NULL
);
2350 ok(ret
, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2352 SetLastError(0xdeadbeef);
2353 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
2354 "D:(A;;RPWPCCDCLCSWLODTCR;;;WD)", SDDL_REVISION_1
, &pSD
, NULL
);
2355 ok(ret
, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2357 SetLastError(0xdeadbeef);
2358 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
2359 "D:(A;;FAFRFWFX;;;WD)", SDDL_REVISION_1
, &pSD
, NULL
);
2360 ok(ret
, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2362 SetLastError(0xdeadbeef);
2363 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
2364 "D:(A;;KAKRKWKX;;;WD)", SDDL_REVISION_1
, &pSD
, NULL
);
2365 ok(ret
, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2367 SetLastError(0xdeadbeef);
2368 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
2369 "D:(A;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1
, &pSD
, NULL
);
2370 ok(ret
, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2372 SetLastError(0xdeadbeef);
2373 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
2374 "S:(AU;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1
, &pSD
, NULL
);
2375 ok(ret
, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2378 /* test ACE string access right error case */
2379 SetLastError(0xdeadbeef);
2380 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
2381 "D:(A;;ROB;;;WD)", SDDL_REVISION_1
, &pSD
, NULL
);
2382 ok(!ret
&& GetLastError() == ERROR_INVALID_ACL
,
2383 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_ACL instead of %d\n",
2386 /* test behaviour with NULL parameters */
2387 SetLastError(0xdeadbeef);
2388 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
2389 NULL
, 0xdeadbeef, &pSD
, NULL
);
2391 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
2392 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2395 SetLastError(0xdeadbeef);
2396 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorW(
2397 NULL
, 0xdeadbeef, &pSD
, NULL
);
2398 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
2399 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2402 SetLastError(0xdeadbeef);
2403 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
2404 "D:(A;;ROB;;;WD)", 0xdeadbeef, NULL
, NULL
);
2405 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
2406 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2409 SetLastError(0xdeadbeef);
2410 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
2411 "D:(A;;ROB;;;WD)", SDDL_REVISION_1
, NULL
, NULL
);
2412 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
2413 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2416 /* test behaviour with empty strings */
2417 SetLastError(0xdeadbeef);
2418 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
2419 "", SDDL_REVISION_1
, &pSD
, NULL
);
2420 ok(ret
, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2422 SetLastError(0xdeadbeef);
2423 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorW(
2424 Blank
, SDDL_REVISION_1
, &pSD
, NULL
);
2425 ok(ret
, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2427 /* test ACE string SID */
2428 SetLastError(0xdeadbeef);
2429 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
2430 "D:(D;;GA;;;S-1-0-0)", SDDL_REVISION_1
, &pSD
, NULL
);
2431 ok(ret
, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2434 SetLastError(0xdeadbeef);
2435 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
2436 "D:(D;;GA;;;Nonexistent account)", SDDL_REVISION_1
, &pSD
, NULL
);
2437 ok(!ret
, "Expected failure, got %d\n", ret
);
2438 ok(GetLastError() == ERROR_INVALID_ACL
|| GetLastError() == ERROR_INVALID_SID
,
2439 "Expected ERROR_INVALID_ACL or ERROR_INVALID_SID, got %d\n", GetLastError());
2442 static void test_ConvertSecurityDescriptorToString()
2444 SECURITY_DESCRIPTOR desc
;
2445 SECURITY_INFORMATION sec_info
= OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
|SACL_SECURITY_INFORMATION
;
2454 if (!pConvertSecurityDescriptorToStringSecurityDescriptorA
)
2456 skip("ConvertSecurityDescriptorToStringSecurityDescriptor is not available\n");
2459 if (!pCreateWellKnownSid
)
2461 skip("CreateWellKnownSid is not available\n");
2465 /* It seems Windows XP adds an extra character to the length of the string for each ACE in an ACL. We
2466 * don't replicate this feature so we only test len >= strlen+1. */
2467 #define CHECK_RESULT_AND_FREE(exp_str) \
2468 ok(strcmp(string, (exp_str)) == 0, "String mismatch (expected \"%s\", got \"%s\")\n", (exp_str), string); \
2469 ok(len >= (lstrlen(exp_str) + 1), "Length mismatch (expected %d, got %d)\n", lstrlen(exp_str) + 1, len); \
2472 #define CHECK_ONE_OF_AND_FREE(exp_str1, exp_str2) \
2473 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); \
2474 ok(len >= (strlen(string) + 1), "Length mismatch (expected %d, got %d)\n", lstrlen(string) + 1, len); \
2477 InitializeSecurityDescriptor(&desc
, SECURITY_DESCRIPTOR_REVISION
);
2478 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
2479 CHECK_RESULT_AND_FREE("");
2482 pCreateWellKnownSid(WinLocalSid
, NULL
, sid_buf
, &size
);
2483 SetSecurityDescriptorOwner(&desc
, (PSID
)sid_buf
, FALSE
);
2484 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
2485 CHECK_RESULT_AND_FREE("O:S-1-2-0");
2487 SetSecurityDescriptorOwner(&desc
, (PSID
)sid_buf
, TRUE
);
2488 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
2489 CHECK_RESULT_AND_FREE("O:S-1-2-0");
2491 size
= sizeof(sid_buf
);
2492 pCreateWellKnownSid(WinLocalSystemSid
, NULL
, sid_buf
, &size
);
2493 SetSecurityDescriptorOwner(&desc
, (PSID
)sid_buf
, TRUE
);
2494 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
2495 CHECK_RESULT_AND_FREE("O:SY");
2497 pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid
);
2498 SetSecurityDescriptorGroup(&desc
, psid
, TRUE
);
2499 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
2500 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576");
2502 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, GROUP_SECURITY_INFORMATION
, &string
, &len
), "Conversion failed\n");
2503 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
2505 pacl
= (PACL
)acl_buf
;
2506 InitializeAcl(pacl
, sizeof(acl_buf
), ACL_REVISION
);
2507 SetSecurityDescriptorDacl(&desc
, TRUE
, pacl
, TRUE
);
2508 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
2509 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
2511 SetSecurityDescriptorDacl(&desc
, TRUE
, pacl
, FALSE
);
2512 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
2513 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
2515 pConvertStringSidToSidA("S-1-5-6", &psid2
);
2516 pAddAccessAllowedAceEx(pacl
, ACL_REVISION
, NO_PROPAGATE_INHERIT_ACE
, 0xf0000000, psid2
);
2517 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
2518 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)");
2520 pAddAccessAllowedAceEx(pacl
, ACL_REVISION
, INHERIT_ONLY_ACE
|INHERITED_ACE
, 0x00000003, psid2
);
2521 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
2522 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)");
2524 pAddAccessDeniedAceEx(pacl
, ACL_REVISION
, OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
, 0xffffffff, psid
);
2525 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
2526 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)");
2529 pacl
= (PACL
)acl_buf
;
2530 InitializeAcl(pacl
, sizeof(acl_buf
), ACL_REVISION
);
2531 SetSecurityDescriptorSacl(&desc
, TRUE
, pacl
, FALSE
);
2532 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
2533 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:");
2535 /* fails in win2k */
2536 SetSecurityDescriptorDacl(&desc
, TRUE
, NULL
, FALSE
);
2537 pAddAuditAccessAceEx(pacl
, ACL_REVISION
, VALID_INHERIT_FLAGS
, KEY_READ
|KEY_WRITE
, psid2
, TRUE
, TRUE
);
2538 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
))
2540 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)", /* XP */
2541 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)" /* Vista */);
2544 /* fails in win2k */
2545 pAddAuditAccessAceEx(pacl
, ACL_REVISION
, NO_PROPAGATE_INHERIT_ACE
, FILE_GENERIC_READ
|FILE_GENERIC_WRITE
, psid2
, TRUE
, FALSE
);
2546 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
))
2548 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", /* XP */
2549 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)" /* Vista */);
2553 static void test_SetSecurityDescriptorControl (PSECURITY_DESCRIPTOR sec
)
2555 SECURITY_DESCRIPTOR_CONTROL ref
;
2556 SECURITY_DESCRIPTOR_CONTROL test
;
2558 SECURITY_DESCRIPTOR_CONTROL
const mutable
2559 = SE_DACL_AUTO_INHERIT_REQ
| SE_SACL_AUTO_INHERIT_REQ
2560 | SE_DACL_AUTO_INHERITED
| SE_SACL_AUTO_INHERITED
2561 | SE_DACL_PROTECTED
| SE_SACL_PROTECTED
2562 | 0x00000040 | 0x00000080 /* not defined in winnt.h */
2564 SECURITY_DESCRIPTOR_CONTROL
const immutable
2565 = SE_OWNER_DEFAULTED
| SE_GROUP_DEFAULTED
2566 | SE_DACL_PRESENT
| SE_DACL_DEFAULTED
2567 | SE_SACL_PRESENT
| SE_SACL_DEFAULTED
2568 | SE_RM_CONTROL_VALID
| SE_SELF_RELATIVE
2573 LPCSTR fmt
= "Expected error %s, got %u\n";
2575 GetSecurityDescriptorControl (sec
, &ref
, &dwRevision
);
2577 /* The mutable bits are mutable regardless of the truth of
2578 SE_DACL_PRESENT and/or SE_SACL_PRESENT */
2580 /* Check call barfs if any bit-of-interest is immutable */
2581 for (bit
= 0; bit
< 16; ++bit
)
2583 SECURITY_DESCRIPTOR_CONTROL
const bitOfInterest
= 1 << bit
;
2584 SECURITY_DESCRIPTOR_CONTROL setOrClear
= ref
& bitOfInterest
;
2586 SECURITY_DESCRIPTOR_CONTROL ctrl
;
2588 DWORD dwExpect
= (bitOfInterest
& immutable
)
2589 ? ERROR_INVALID_PARAMETER
: 0xbebecaca;
2590 LPCSTR strExpect
= (bitOfInterest
& immutable
)
2591 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
2593 ctrl
= (bitOfInterest
& mutable) ? ref
+ bitOfInterest
: ref
;
2594 setOrClear
^= bitOfInterest
;
2595 SetLastError (0xbebecaca);
2596 pSetSecurityDescriptorControl (sec
, bitOfInterest
, setOrClear
);
2597 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
2598 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
2599 expect_eq(test
, ctrl
, int, "%x");
2602 setOrClear
^= bitOfInterest
;
2603 SetLastError (0xbebecaca);
2604 pSetSecurityDescriptorControl (sec
, bitOfInterest
, setOrClear
);
2605 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
2606 GetSecurityDescriptorControl (sec
, &test
, &dwRevision
);
2607 expect_eq(test
, ref
, int, "%x");
2610 /* Check call barfs if any bit-to-set is immutable
2611 even when not a bit-of-interest */
2612 for (bit
= 0; bit
< 16; ++bit
)
2614 SECURITY_DESCRIPTOR_CONTROL
const bitsOfInterest
= mutable;
2615 SECURITY_DESCRIPTOR_CONTROL setOrClear
= ref
& bitsOfInterest
;
2617 SECURITY_DESCRIPTOR_CONTROL ctrl
;
2619 DWORD dwExpect
= ((1 << bit
) & immutable
)
2620 ? ERROR_INVALID_PARAMETER
: 0xbebecaca;
2621 LPCSTR strExpect
= ((1 << bit
) & immutable
)
2622 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
2624 ctrl
= ((1 << bit
) & immutable
) ? test
: ref
| mutable;
2625 setOrClear
^= bitsOfInterest
;
2626 SetLastError (0xbebecaca);
2627 pSetSecurityDescriptorControl (sec
, bitsOfInterest
, setOrClear
| (1 << bit
));
2628 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
2629 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
2630 expect_eq(test
, ctrl
, int, "%x");
2632 ctrl
= ((1 << bit
) & immutable
) ? test
: ref
| (1 << bit
);
2633 setOrClear
^= bitsOfInterest
;
2634 SetLastError (0xbebecaca);
2635 pSetSecurityDescriptorControl (sec
, bitsOfInterest
, setOrClear
| (1 << bit
));
2636 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
2637 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
2638 expect_eq(test
, ctrl
, int, "%x");
2642 static void test_PrivateObjectSecurity(void)
2644 SECURITY_INFORMATION sec_info
= OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
|SACL_SECURITY_INFORMATION
;
2645 SECURITY_DESCRIPTOR_CONTROL ctrl
;
2646 PSECURITY_DESCRIPTOR sec
;
2652 PSECURITY_DESCRIPTOR buf
;
2654 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA
)
2656 skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
2660 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
2662 "G:S-1-5-21-93476-23408-4576"
2663 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)"
2664 "(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
2665 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
2666 SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
2668 test_SetSecurityDescriptorControl(sec
);
2672 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
2674 "G:S-1-5-21-93476-23408-4576",
2675 SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
2677 test_SetSecurityDescriptorControl(sec
);
2681 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
2683 "G:S-1-5-21-93476-23408-4576"
2684 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
2685 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
2686 buf
= HeapAlloc(GetProcessHeap(), 0, dwDescSize
);
2687 pSetSecurityDescriptorControl(sec
, SE_DACL_PROTECTED
, SE_DACL_PROTECTED
);
2688 GetSecurityDescriptorControl(sec
, &ctrl
, &dwRevision
);
2689 expect_eq(ctrl
, 0x9014, int, "%x");
2691 ok(GetPrivateObjectSecurity(sec
, GROUP_SECURITY_INFORMATION
, buf
, dwDescSize
, &retSize
),
2692 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
2693 ok(retSize
<= dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
2694 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
2695 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
2696 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
2697 expect_eq(ctrl
, 0x8000, int, "%x");
2699 ok(GetPrivateObjectSecurity(sec
, GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, buf
, dwDescSize
, &retSize
),
2700 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
2701 ok(retSize
<= dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
2702 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed err=%u\n", GetLastError());
2703 CHECK_RESULT_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)");
2704 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
2705 expect_eq(ctrl
, 0x8004, int, "%x");
2707 ok(GetPrivateObjectSecurity(sec
, sec_info
, buf
, dwDescSize
, &retSize
),
2708 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
2709 ok(retSize
== dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
2710 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
2711 CHECK_RESULT_AND_FREE("O:SY"
2712 "G:S-1-5-21-93476-23408-4576"
2713 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
2714 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)");
2715 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
2716 expect_eq(ctrl
, 0x8014, int, "%x");
2718 SetLastError(0xdeadbeef);
2719 ok(GetPrivateObjectSecurity(sec
, sec_info
, buf
, 5, &retSize
) == FALSE
, "GetPrivateObjectSecurity should have failed\n");
2720 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Expected error ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
2723 HeapFree(GetProcessHeap(), 0, buf
);
2725 #undef CHECK_RESULT_AND_FREE
2726 #undef CHECK_ONE_OF_AND_FREE
2728 static void test_acls(void)
2731 PACL pAcl
= (PACL
)buffer
;
2734 SetLastError(0xdeadbeef);
2735 ret
= InitializeAcl(pAcl
, sizeof(ACL
) - 1, ACL_REVISION
);
2736 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2738 skip("InitializeAcl is not implemented\n");
2742 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "InitializeAcl with too small a buffer should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
2744 SetLastError(0xdeadbeef);
2745 ret
= InitializeAcl(pAcl
, 0xffffffff, ACL_REVISION
);
2746 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl with too large a buffer should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
2748 SetLastError(0xdeadbeef);
2749 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION1
);
2750 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl(ACL_REVISION1) should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
2752 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION2
);
2753 ok(ret
, "InitializeAcl(ACL_REVISION2) failed with error %d\n", GetLastError());
2755 ret
= IsValidAcl(pAcl
);
2756 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
2758 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION3
);
2759 ok(ret
, "InitializeAcl(ACL_REVISION3) failed with error %d\n", GetLastError());
2761 ret
= IsValidAcl(pAcl
);
2762 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
2764 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION4
);
2765 ok(ret
, "InitializeAcl(ACL_REVISION4) failed with error %d\n", GetLastError());
2767 ret
= IsValidAcl(pAcl
);
2768 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
2770 SetLastError(0xdeadbeef);
2771 ret
= InitializeAcl(pAcl
, sizeof(buffer
), -1);
2772 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl(-1) failed with error %d\n", GetLastError());
2775 static void test_GetSecurityInfo(void)
2778 PSECURITY_DESCRIPTOR sd
;
2783 if (!pGetSecurityInfo
)
2785 win_skip("GetSecurityInfo is not available\n");
2789 /* Create something. Files have lots of associated security info. */
2790 obj
= CreateFile(myARGV
[0], GENERIC_READ
, FILE_SHARE_READ
, NULL
,
2791 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2792 if (obj
== INVALID_HANDLE_VALUE
)
2794 skip("Couldn't create an object for GetSecurityInfo test\n");
2798 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
,
2799 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
2800 &owner
, &group
, &dacl
, NULL
, &sd
);
2801 if (ret
== ERROR_CALL_NOT_IMPLEMENTED
)
2803 win_skip("GetSecurityInfo is not implemented\n");
2807 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
2808 ok(sd
!= NULL
, "GetSecurityInfo\n");
2809 ok(owner
!= NULL
, "GetSecurityInfo\n");
2810 ok(group
!= NULL
, "GetSecurityInfo\n");
2811 ok(dacl
!= NULL
, "GetSecurityInfo\n");
2812 ok(IsValidAcl(dacl
), "GetSecurityInfo\n");
2816 /* If we don't ask for the security descriptor, Windows will still give us
2817 the other stuff, leaving us no way to free it. */
2818 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
,
2819 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
2820 &owner
, &group
, &dacl
, NULL
, NULL
);
2821 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
2822 ok(owner
!= NULL
, "GetSecurityInfo\n");
2823 ok(group
!= NULL
, "GetSecurityInfo\n");
2824 ok(dacl
!= NULL
, "GetSecurityInfo\n");
2825 ok(IsValidAcl(dacl
), "GetSecurityInfo\n");
2830 START_TEST(security
)
2837 test_process_security_child();
2843 test_CreateWellKnownSid();
2844 test_FileSecurity();
2847 test_LookupAccountSid();
2848 test_LookupAccountName();
2849 test_security_descriptor();
2850 test_process_security();
2851 test_impersonation_level();
2852 test_SetEntriesInAcl();
2853 test_GetNamedSecurityInfoA();
2854 test_ConvertStringSecurityDescriptor();
2855 test_ConvertSecurityDescriptorToString();
2856 test_PrivateObjectSecurity();
2858 test_GetSecurityInfo();