2 * Unit tests for security functions
4 * Copyright (c) 2004 Mike McCormack
5 * Copyright (c) 2011 Dmitry Timoshkov
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #define WIN32_NO_STATUS
27 #define WIN32_LEAN_AND_MEAN
39 #include "wine/test.h"
41 #ifndef PROCESS_QUERY_LIMITED_INFORMATION
42 #define PROCESS_QUERY_LIMITED_INFORMATION 0x1000
45 /* PROCESS_ALL_ACCESS in Vista+ PSDKs is incompatible with older Windows versions */
46 #define PROCESS_ALL_ACCESS_NT4 (PROCESS_ALL_ACCESS & ~0xf000)
47 #define PROCESS_ALL_ACCESS_VISTA (PROCESS_ALL_ACCESS | 0xf000)
49 #ifndef EVENT_QUERY_STATE
50 #define EVENT_QUERY_STATE 0x0001
53 #ifndef SEMAPHORE_QUERY_STATE
54 #define SEMAPHORE_QUERY_STATE 0x0001
57 #ifndef THREAD_SET_LIMITED_INFORMATION
58 #define THREAD_SET_LIMITED_INFORMATION 0x0400
59 #define THREAD_QUERY_LIMITED_INFORMATION 0x0800
62 #define THREAD_ALL_ACCESS_NT4 (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x3ff)
63 #define THREAD_ALL_ACCESS_VISTA (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0xffff)
65 /* copied from Wine winternl.h - not included in the Windows SDK */
66 typedef enum _OBJECT_INFORMATION_CLASS
{
67 ObjectBasicInformation
,
68 ObjectNameInformation
,
69 ObjectTypeInformation
,
72 } OBJECT_INFORMATION_CLASS
, *POBJECT_INFORMATION_CLASS
;
74 typedef struct _OBJECT_BASIC_INFORMATION
{
76 ACCESS_MASK GrantedAccess
;
80 ULONG NonPagedPoolUsage
;
82 ULONG NameInformationLength
;
83 ULONG TypeInformationLength
;
84 ULONG SecurityDescriptorLength
;
85 LARGE_INTEGER CreateTime
;
86 } OBJECT_BASIC_INFORMATION
, *POBJECT_BASIC_INFORMATION
;
88 #define expect_eq(expr, value, type, format) { type ret_ = expr; ok((value) == ret_, #expr " expected " format " got " format "\n", (value), (ret_)); }
90 static BOOL (WINAPI
*pAddAccessAllowedAceEx
)(PACL
, DWORD
, DWORD
, DWORD
, PSID
);
91 static BOOL (WINAPI
*pAddAccessDeniedAceEx
)(PACL
, DWORD
, DWORD
, DWORD
, PSID
);
92 static BOOL (WINAPI
*pAddAuditAccessAceEx
)(PACL
, DWORD
, DWORD
, DWORD
, PSID
, BOOL
, BOOL
);
93 static VOID (WINAPI
*pBuildTrusteeWithSidA
)( PTRUSTEEA pTrustee
, PSID pSid
);
94 static VOID (WINAPI
*pBuildTrusteeWithNameA
)( PTRUSTEEA pTrustee
, LPSTR pName
);
95 static VOID (WINAPI
*pBuildTrusteeWithObjectsAndNameA
)( PTRUSTEEA pTrustee
,
96 POBJECTS_AND_NAME_A pObjName
,
97 SE_OBJECT_TYPE ObjectType
,
99 LPSTR InheritedObjectTypeName
,
101 static VOID (WINAPI
*pBuildTrusteeWithObjectsAndSidA
)( PTRUSTEEA pTrustee
,
102 POBJECTS_AND_SID pObjSid
,
104 GUID
* pInheritedObjectGuid
,
106 static LPSTR (WINAPI
*pGetTrusteeNameA
)( PTRUSTEEA pTrustee
);
107 static BOOL (WINAPI
*pMakeSelfRelativeSD
)( PSECURITY_DESCRIPTOR
, PSECURITY_DESCRIPTOR
, LPDWORD
);
108 static BOOL (WINAPI
*pConvertSidToStringSidA
)( PSID pSid
, LPSTR
*str
);
109 static BOOL (WINAPI
*pConvertStringSidToSidA
)( LPCSTR str
, PSID pSid
);
110 static BOOL (WINAPI
*pCheckTokenMembership
)(HANDLE
, PSID
, PBOOL
);
111 static BOOL (WINAPI
*pConvertStringSecurityDescriptorToSecurityDescriptorA
)(LPCSTR
, DWORD
,
112 PSECURITY_DESCRIPTOR
*, PULONG
);
113 static BOOL (WINAPI
*pConvertStringSecurityDescriptorToSecurityDescriptorW
)(LPCWSTR
, DWORD
,
114 PSECURITY_DESCRIPTOR
*, PULONG
);
115 static BOOL (WINAPI
*pConvertSecurityDescriptorToStringSecurityDescriptorA
)(PSECURITY_DESCRIPTOR
, DWORD
,
116 SECURITY_INFORMATION
, LPSTR
*, PULONG
);
117 static BOOL (WINAPI
*pGetFileSecurityA
)(LPCSTR
, SECURITY_INFORMATION
,
118 PSECURITY_DESCRIPTOR
, DWORD
, LPDWORD
);
119 static BOOL (WINAPI
*pSetFileSecurityA
)(LPCSTR
, SECURITY_INFORMATION
,
120 PSECURITY_DESCRIPTOR
);
121 static DWORD (WINAPI
*pGetNamedSecurityInfoA
)(LPSTR
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
122 PSID
*, PSID
*, PACL
*, PACL
*,
123 PSECURITY_DESCRIPTOR
*);
124 static DWORD (WINAPI
*pSetNamedSecurityInfoA
)(LPSTR
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
125 PSID
, PSID
, PACL
, PACL
);
126 static PDWORD (WINAPI
*pGetSidSubAuthority
)(PSID
, DWORD
);
127 static PUCHAR (WINAPI
*pGetSidSubAuthorityCount
)(PSID
);
128 static BOOL (WINAPI
*pIsValidSid
)(PSID
);
129 static DWORD (WINAPI
*pRtlAdjustPrivilege
)(ULONG
,BOOLEAN
,BOOLEAN
,PBOOLEAN
);
130 static BOOL (WINAPI
*pCreateWellKnownSid
)(WELL_KNOWN_SID_TYPE
,PSID
,PSID
,DWORD
*);
131 static BOOL (WINAPI
*pDuplicateTokenEx
)(HANDLE
,DWORD
,LPSECURITY_ATTRIBUTES
,
132 SECURITY_IMPERSONATION_LEVEL
,TOKEN_TYPE
,PHANDLE
);
134 static NTSTATUS (WINAPI
*pLsaQueryInformationPolicy
)(LSA_HANDLE
,POLICY_INFORMATION_CLASS
,PVOID
*);
135 static NTSTATUS (WINAPI
*pLsaClose
)(LSA_HANDLE
);
136 static NTSTATUS (WINAPI
*pLsaFreeMemory
)(PVOID
);
137 static NTSTATUS (WINAPI
*pLsaOpenPolicy
)(PLSA_UNICODE_STRING
,PLSA_OBJECT_ATTRIBUTES
,ACCESS_MASK
,PLSA_HANDLE
);
138 static NTSTATUS (WINAPI
*pNtQueryObject
)(HANDLE
,OBJECT_INFORMATION_CLASS
,PVOID
,ULONG
,PULONG
);
139 static DWORD (WINAPI
*pSetEntriesInAclW
)(ULONG
, PEXPLICIT_ACCESSW
, PACL
, PACL
*);
140 static DWORD (WINAPI
*pSetEntriesInAclA
)(ULONG
, PEXPLICIT_ACCESSA
, PACL
, PACL
*);
141 static BOOL (WINAPI
*pSetSecurityDescriptorControl
)(PSECURITY_DESCRIPTOR
, SECURITY_DESCRIPTOR_CONTROL
,
142 SECURITY_DESCRIPTOR_CONTROL
);
143 static DWORD (WINAPI
*pGetSecurityInfo
)(HANDLE
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
144 PSID
*, PSID
*, PACL
*, PACL
*, PSECURITY_DESCRIPTOR
*);
145 static DWORD (WINAPI
*pSetSecurityInfo
)(HANDLE
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
146 PSID
, PSID
, PACL
, PACL
);
147 static NTSTATUS (WINAPI
*pNtAccessCheck
)(PSECURITY_DESCRIPTOR
, HANDLE
, ACCESS_MASK
, PGENERIC_MAPPING
,
148 PPRIVILEGE_SET
, PULONG
, PULONG
, NTSTATUS
*);
149 static BOOL (WINAPI
*pCreateRestrictedToken
)(HANDLE
, DWORD
, DWORD
, PSID_AND_ATTRIBUTES
, DWORD
,
150 PLUID_AND_ATTRIBUTES
, DWORD
, PSID_AND_ATTRIBUTES
, PHANDLE
);
151 static BOOL (WINAPI
*pGetAclInformation
)(PACL
,LPVOID
,DWORD
,ACL_INFORMATION_CLASS
);
152 static BOOL (WINAPI
*pGetAce
)(PACL
,DWORD
,LPVOID
*);
156 static char** myARGV
;
168 SID_IDENTIFIER_AUTHORITY auth
;
172 static void init(void)
176 hntdll
= GetModuleHandleA("ntdll.dll");
177 pNtQueryObject
= (void *)GetProcAddress( hntdll
, "NtQueryObject" );
178 pNtAccessCheck
= (void *)GetProcAddress( hntdll
, "NtAccessCheck" );
180 hmod
= GetModuleHandleA("advapi32.dll");
181 pAddAccessAllowedAceEx
= (void *)GetProcAddress(hmod
, "AddAccessAllowedAceEx");
182 pAddAccessDeniedAceEx
= (void *)GetProcAddress(hmod
, "AddAccessDeniedAceEx");
183 pAddAuditAccessAceEx
= (void *)GetProcAddress(hmod
, "AddAuditAccessAceEx");
184 pCheckTokenMembership
= (void *)GetProcAddress(hmod
, "CheckTokenMembership");
185 pConvertStringSecurityDescriptorToSecurityDescriptorA
=
186 (void *)GetProcAddress(hmod
, "ConvertStringSecurityDescriptorToSecurityDescriptorA" );
187 pConvertStringSecurityDescriptorToSecurityDescriptorW
=
188 (void *)GetProcAddress(hmod
, "ConvertStringSecurityDescriptorToSecurityDescriptorW" );
189 pConvertSecurityDescriptorToStringSecurityDescriptorA
=
190 (void *)GetProcAddress(hmod
, "ConvertSecurityDescriptorToStringSecurityDescriptorA" );
191 pGetFileSecurityA
= (void *)GetProcAddress(hmod
, "GetFileSecurityA" );
192 pSetFileSecurityA
= (void *)GetProcAddress(hmod
, "SetFileSecurityA" );
193 pCreateWellKnownSid
= (void *)GetProcAddress( hmod
, "CreateWellKnownSid" );
194 pGetNamedSecurityInfoA
= (void *)GetProcAddress(hmod
, "GetNamedSecurityInfoA");
195 pSetNamedSecurityInfoA
= (void *)GetProcAddress(hmod
, "SetNamedSecurityInfoA");
196 pGetSidSubAuthority
= (void *)GetProcAddress(hmod
, "GetSidSubAuthority");
197 pGetSidSubAuthorityCount
= (void *)GetProcAddress(hmod
, "GetSidSubAuthorityCount");
198 pIsValidSid
= (void *)GetProcAddress(hmod
, "IsValidSid");
199 pMakeSelfRelativeSD
= (void *)GetProcAddress(hmod
, "MakeSelfRelativeSD");
200 pSetEntriesInAclW
= (void *)GetProcAddress(hmod
, "SetEntriesInAclW");
201 pSetEntriesInAclA
= (void *)GetProcAddress(hmod
, "SetEntriesInAclA");
202 pSetSecurityDescriptorControl
= (void *)GetProcAddress(hmod
, "SetSecurityDescriptorControl");
203 pGetSecurityInfo
= (void *)GetProcAddress(hmod
, "GetSecurityInfo");
204 pSetSecurityInfo
= (void *)GetProcAddress(hmod
, "SetSecurityInfo");
205 pCreateRestrictedToken
= (void *)GetProcAddress(hmod
, "CreateRestrictedToken");
206 pConvertSidToStringSidA
= (void *)GetProcAddress(hmod
, "ConvertSidToStringSidA");
207 pConvertStringSidToSidA
= (void *)GetProcAddress(hmod
, "ConvertStringSidToSidA");
208 pGetAclInformation
= (void *)GetProcAddress(hmod
, "GetAclInformation");
209 pGetAce
= (void *)GetProcAddress(hmod
, "GetAce");
211 myARGC
= winetest_get_mainargs( &myARGV
);
214 static void test_sid(void)
216 struct sidRef refs
[] = {
217 { { {0x00,0x00,0x33,0x44,0x55,0x66} }, "S-1-860116326-1" },
218 { { {0x00,0x00,0x01,0x02,0x03,0x04} }, "S-1-16909060-1" },
219 { { {0x00,0x00,0x00,0x01,0x02,0x03} }, "S-1-66051-1" },
220 { { {0x00,0x00,0x00,0x00,0x01,0x02} }, "S-1-258-1" },
221 { { {0x00,0x00,0x00,0x00,0x00,0x02} }, "S-1-2-1" },
222 { { {0x00,0x00,0x00,0x00,0x00,0x0c} }, "S-1-12-1" },
224 struct strsid_entry strsid_table
[] = {
225 {"AO", STRSID_OK
}, {"RU", STRSID_OK
}, {"AN", STRSID_OK
}, {"AU", STRSID_OK
},
226 {"BA", STRSID_OK
}, {"BG", STRSID_OK
}, {"BO", STRSID_OK
}, {"BU", STRSID_OK
},
227 {"CA", STRSID_OPT
}, {"CG", STRSID_OK
}, {"CO", STRSID_OK
}, {"DA", STRSID_OPT
},
228 {"DC", STRSID_OPT
}, {"DD", STRSID_OPT
}, {"DG", STRSID_OPT
}, {"DU", STRSID_OPT
},
229 {"EA", STRSID_OPT
}, {"ED", STRSID_OK
}, {"WD", STRSID_OK
}, {"PA", STRSID_OPT
},
230 {"IU", STRSID_OK
}, {"LA", STRSID_OK
}, {"LG", STRSID_OK
}, {"LS", STRSID_OK
},
231 {"SY", STRSID_OK
}, {"NU", STRSID_OK
}, {"NO", STRSID_OK
}, {"NS", STRSID_OK
},
232 {"PO", STRSID_OK
}, {"PS", STRSID_OK
}, {"PU", STRSID_OK
}, {"RS", STRSID_OPT
},
233 {"RD", STRSID_OK
}, {"RE", STRSID_OK
}, {"RC", STRSID_OK
}, {"SA", STRSID_OPT
},
234 {"SO", STRSID_OK
}, {"SU", STRSID_OK
}};
236 const char noSubAuthStr
[] = "S-1-5";
243 if( !pConvertSidToStringSidA
|| !pConvertStringSidToSidA
)
245 win_skip("ConvertSidToStringSidA or ConvertStringSidToSidA not available\n");
249 r
= pConvertStringSidToSidA( NULL
, NULL
);
250 ok( !r
, "expected failure with NULL parameters\n" );
251 if( GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
253 ok( GetLastError() == ERROR_INVALID_PARAMETER
,
254 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
257 r
= pConvertStringSidToSidA( refs
[0].refStr
, NULL
);
258 ok( !r
&& GetLastError() == ERROR_INVALID_PARAMETER
,
259 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
262 r
= pConvertStringSidToSidA( NULL
, &str
);
263 ok( !r
&& GetLastError() == ERROR_INVALID_PARAMETER
,
264 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
267 r
= pConvertStringSidToSidA( noSubAuthStr
, &psid
);
269 "expected failure with no sub authorities\n" );
270 ok( GetLastError() == ERROR_INVALID_SID
,
271 "expected GetLastError() is ERROR_INVALID_SID, got %d\n",
274 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid
), "ConvertStringSidToSidA failed\n");
276 ok(pisid
->SubAuthorityCount
== 4, "Invalid sub authority count - expected 4, got %d\n", pisid
->SubAuthorityCount
);
277 ok(pisid
->SubAuthority
[0] == 21, "Invalid subauthority 0 - expected 21, got %d\n", pisid
->SubAuthority
[0]);
278 ok(pisid
->SubAuthority
[3] == 4576, "Invalid subauthority 0 - expected 4576, got %d\n", pisid
->SubAuthority
[3]);
282 for( i
= 0; i
< sizeof(refs
) / sizeof(refs
[0]); i
++ )
284 r
= AllocateAndInitializeSid( &refs
[i
].auth
, 1,1,0,0,0,0,0,0,0,
286 ok( r
, "failed to allocate sid\n" );
287 r
= pConvertSidToStringSidA( psid
, &str
);
288 ok( r
, "failed to convert sid\n" );
291 ok( !strcmp( str
, refs
[i
].refStr
),
292 "incorrect sid, expected %s, got %s\n", refs
[i
].refStr
, str
);
298 r
= pConvertStringSidToSidA( refs
[i
].refStr
, &psid
);
299 ok( r
, "failed to parse sid string\n" );
302 !memcmp( pisid
->IdentifierAuthority
.Value
, refs
[i
].auth
.Value
,
303 sizeof(refs
[i
].auth
) ),
304 "string sid %s didn't parse to expected value\n"
305 "(got 0x%04x%08x, expected 0x%04x%08x)\n",
307 MAKEWORD( pisid
->IdentifierAuthority
.Value
[1],
308 pisid
->IdentifierAuthority
.Value
[0] ),
309 MAKELONG( MAKEWORD( pisid
->IdentifierAuthority
.Value
[5],
310 pisid
->IdentifierAuthority
.Value
[4] ),
311 MAKEWORD( pisid
->IdentifierAuthority
.Value
[3],
312 pisid
->IdentifierAuthority
.Value
[2] ) ),
313 MAKEWORD( refs
[i
].auth
.Value
[1], refs
[i
].auth
.Value
[0] ),
314 MAKELONG( MAKEWORD( refs
[i
].auth
.Value
[5], refs
[i
].auth
.Value
[4] ),
315 MAKEWORD( refs
[i
].auth
.Value
[3], refs
[i
].auth
.Value
[2] ) ) );
320 /* string constant format not supported before XP */
321 r
= pConvertStringSidToSidA(strsid_table
[0].str
, &psid
);
324 win_skip("String constant format not supported\n");
329 for(i
= 0; i
< sizeof(strsid_table
) / sizeof(strsid_table
[0]); i
++)
333 SetLastError(0xdeadbeef);
334 r
= pConvertStringSidToSidA(strsid_table
[i
].str
, &psid
);
336 if (!(strsid_table
[i
].flags
& STRSID_OPT
))
338 ok(r
, "%s: got %u\n", strsid_table
[i
].str
, GetLastError());
343 if ((winetest_debug
> 1) && (pConvertSidToStringSidA(psid
, &temp
)))
345 trace(" %s: %s\n", strsid_table
[i
].str
, temp
);
352 if (GetLastError() != ERROR_INVALID_SID
)
353 trace(" %s: couldn't be converted, returned %d\n", strsid_table
[i
].str
, GetLastError());
355 trace(" %s: couldn't be converted\n", strsid_table
[i
].str
);
360 static void test_trustee(void)
362 GUID ObjectType
= {0x12345678, 0x1234, 0x5678, {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}};
363 GUID InheritedObjectType
= {0x23456789, 0x2345, 0x6786, {0x2, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}};
365 OBJECTS_AND_NAME_A oan
;
369 char szObjectTypeName
[] = "ObjectTypeName";
370 char szInheritedObjectTypeName
[] = "InheritedObjectTypeName";
371 char szTrusteeName
[] = "szTrusteeName";
372 SID_IDENTIFIER_AUTHORITY auth
= { {0x11,0x22,0,0,0, 0} };
374 memset( &ZeroGuid
, 0x00, sizeof (ZeroGuid
) );
376 pBuildTrusteeWithSidA
= (void *)GetProcAddress( hmod
, "BuildTrusteeWithSidA" );
377 pBuildTrusteeWithNameA
= (void *)GetProcAddress( hmod
, "BuildTrusteeWithNameA" );
378 pBuildTrusteeWithObjectsAndNameA
= (void *)GetProcAddress (hmod
, "BuildTrusteeWithObjectsAndNameA" );
379 pBuildTrusteeWithObjectsAndSidA
= (void *)GetProcAddress (hmod
, "BuildTrusteeWithObjectsAndSidA" );
380 pGetTrusteeNameA
= (void *)GetProcAddress (hmod
, "GetTrusteeNameA" );
381 if( !pBuildTrusteeWithSidA
|| !pBuildTrusteeWithNameA
||
382 !pBuildTrusteeWithObjectsAndNameA
|| !pBuildTrusteeWithObjectsAndSidA
||
386 if ( ! AllocateAndInitializeSid( &auth
, 1, 42, 0,0,0,0,0,0,0,&psid
) )
388 trace( "failed to init SID\n" );
392 /* test BuildTrusteeWithSidA */
393 memset( &trustee
, 0xff, sizeof trustee
);
394 pBuildTrusteeWithSidA( &trustee
, psid
);
396 ok( trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
397 ok( trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
,
398 "MultipleTrusteeOperation wrong\n");
399 ok( trustee
.TrusteeForm
== TRUSTEE_IS_SID
, "TrusteeForm wrong\n");
400 ok( trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
401 ok( trustee
.ptstrName
== psid
, "ptstrName wrong\n" );
403 /* test BuildTrusteeWithObjectsAndSidA (test 1) */
404 memset( &trustee
, 0xff, sizeof trustee
);
405 memset( &oas
, 0xff, sizeof(oas
) );
406 pBuildTrusteeWithObjectsAndSidA(&trustee
, &oas
, &ObjectType
,
407 &InheritedObjectType
, psid
);
409 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
410 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
411 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_SID
, "TrusteeForm wrong\n");
412 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
413 ok(trustee
.ptstrName
== (LPSTR
)&oas
, "ptstrName wrong\n");
415 ok(oas
.ObjectsPresent
== (ACE_OBJECT_TYPE_PRESENT
| ACE_INHERITED_OBJECT_TYPE_PRESENT
), "ObjectsPresent wrong\n");
416 ok(!memcmp(&oas
.ObjectTypeGuid
, &ObjectType
, sizeof(GUID
)), "ObjectTypeGuid wrong\n");
417 ok(!memcmp(&oas
.InheritedObjectTypeGuid
, &InheritedObjectType
, sizeof(GUID
)), "InheritedObjectTypeGuid wrong\n");
418 ok(oas
.pSid
== psid
, "pSid wrong\n");
420 /* test GetTrusteeNameA */
421 ok(pGetTrusteeNameA(&trustee
) == (LPSTR
)&oas
, "GetTrusteeName returned wrong value\n");
423 /* test BuildTrusteeWithObjectsAndSidA (test 2) */
424 memset( &trustee
, 0xff, sizeof trustee
);
425 memset( &oas
, 0xff, sizeof(oas
) );
426 pBuildTrusteeWithObjectsAndSidA(&trustee
, &oas
, NULL
,
427 &InheritedObjectType
, psid
);
429 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
430 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
431 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_SID
, "TrusteeForm wrong\n");
432 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
433 ok(trustee
.ptstrName
== (LPSTR
)&oas
, "ptstrName wrong\n");
435 ok(oas
.ObjectsPresent
== ACE_INHERITED_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
436 ok(!memcmp(&oas
.ObjectTypeGuid
, &ZeroGuid
, sizeof(GUID
)), "ObjectTypeGuid wrong\n");
437 ok(!memcmp(&oas
.InheritedObjectTypeGuid
, &InheritedObjectType
, sizeof(GUID
)), "InheritedObjectTypeGuid wrong\n");
438 ok(oas
.pSid
== psid
, "pSid wrong\n");
442 /* test BuildTrusteeWithNameA */
443 memset( &trustee
, 0xff, sizeof trustee
);
444 pBuildTrusteeWithNameA( &trustee
, szTrusteeName
);
446 ok( trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
447 ok( trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
,
448 "MultipleTrusteeOperation wrong\n");
449 ok( trustee
.TrusteeForm
== TRUSTEE_IS_NAME
, "TrusteeForm wrong\n");
450 ok( trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
451 ok( trustee
.ptstrName
== szTrusteeName
, "ptstrName wrong\n" );
453 /* test BuildTrusteeWithObjectsAndNameA (test 1) */
454 memset( &trustee
, 0xff, sizeof trustee
);
455 memset( &oan
, 0xff, sizeof(oan
) );
456 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, szObjectTypeName
,
457 szInheritedObjectTypeName
, szTrusteeName
);
459 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
460 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
461 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
462 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
463 ok(trustee
.ptstrName
== (LPSTR
)&oan
, "ptstrName wrong\n");
465 ok(oan
.ObjectsPresent
== (ACE_OBJECT_TYPE_PRESENT
| ACE_INHERITED_OBJECT_TYPE_PRESENT
), "ObjectsPresent wrong\n");
466 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
467 ok(oan
.InheritedObjectTypeName
== szInheritedObjectTypeName
, "InheritedObjectTypeName wrong\n");
468 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
470 /* test GetTrusteeNameA */
471 ok(pGetTrusteeNameA(&trustee
) == (LPSTR
)&oan
, "GetTrusteeName returned wrong value\n");
473 /* test BuildTrusteeWithObjectsAndNameA (test 2) */
474 memset( &trustee
, 0xff, sizeof trustee
);
475 memset( &oan
, 0xff, sizeof(oan
) );
476 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, NULL
,
477 szInheritedObjectTypeName
, szTrusteeName
);
479 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
480 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
481 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
482 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
483 ok(trustee
.ptstrName
== (LPSTR
)&oan
, "ptstrName wrong\n");
485 ok(oan
.ObjectsPresent
== ACE_INHERITED_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
486 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
487 ok(oan
.InheritedObjectTypeName
== szInheritedObjectTypeName
, "InheritedObjectTypeName wrong\n");
488 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
490 /* test BuildTrusteeWithObjectsAndNameA (test 3) */
491 memset( &trustee
, 0xff, sizeof trustee
);
492 memset( &oan
, 0xff, sizeof(oan
) );
493 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, szObjectTypeName
,
494 NULL
, szTrusteeName
);
496 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
497 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
498 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
499 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
500 ok(trustee
.ptstrName
== (LPSTR
)&oan
, "ptstrName wrong\n");
502 ok(oan
.ObjectsPresent
== ACE_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
503 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
504 ok(oan
.InheritedObjectTypeName
== NULL
, "InheritedObjectTypeName wrong\n");
505 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
508 /* If the first isn't defined, assume none is */
509 #ifndef SE_MIN_WELL_KNOWN_PRIVILEGE
510 #define SE_MIN_WELL_KNOWN_PRIVILEGE 2L
511 #define SE_CREATE_TOKEN_PRIVILEGE 2L
512 #define SE_ASSIGNPRIMARYTOKEN_PRIVILEGE 3L
513 #define SE_LOCK_MEMORY_PRIVILEGE 4L
514 #define SE_INCREASE_QUOTA_PRIVILEGE 5L
515 #define SE_MACHINE_ACCOUNT_PRIVILEGE 6L
516 #define SE_TCB_PRIVILEGE 7L
517 #define SE_SECURITY_PRIVILEGE 8L
518 #define SE_TAKE_OWNERSHIP_PRIVILEGE 9L
519 #define SE_LOAD_DRIVER_PRIVILEGE 10L
520 #define SE_SYSTEM_PROFILE_PRIVILEGE 11L
521 #define SE_SYSTEMTIME_PRIVILEGE 12L
522 #define SE_PROF_SINGLE_PROCESS_PRIVILEGE 13L
523 #define SE_INC_BASE_PRIORITY_PRIVILEGE 14L
524 #define SE_CREATE_PAGEFILE_PRIVILEGE 15L
525 #define SE_CREATE_PERMANENT_PRIVILEGE 16L
526 #define SE_BACKUP_PRIVILEGE 17L
527 #define SE_RESTORE_PRIVILEGE 18L
528 #define SE_SHUTDOWN_PRIVILEGE 19L
529 #define SE_DEBUG_PRIVILEGE 20L
530 #define SE_AUDIT_PRIVILEGE 21L
531 #define SE_SYSTEM_ENVIRONMENT_PRIVILEGE 22L
532 #define SE_CHANGE_NOTIFY_PRIVILEGE 23L
533 #define SE_REMOTE_SHUTDOWN_PRIVILEGE 24L
534 #define SE_UNDOCK_PRIVILEGE 25L
535 #define SE_SYNC_AGENT_PRIVILEGE 26L
536 #define SE_ENABLE_DELEGATION_PRIVILEGE 27L
537 #define SE_MANAGE_VOLUME_PRIVILEGE 28L
538 #define SE_IMPERSONATE_PRIVILEGE 29L
539 #define SE_CREATE_GLOBAL_PRIVILEGE 30L
540 #define SE_MAX_WELL_KNOWN_PRIVILEGE SE_CREATE_GLOBAL_PRIVILEGE
541 #endif /* ndef SE_MIN_WELL_KNOWN_PRIVILEGE */
543 static void test_allocateLuid(void)
545 BOOL (WINAPI
*pAllocateLocallyUniqueId
)(PLUID
);
549 pAllocateLocallyUniqueId
= (void*)GetProcAddress(hmod
, "AllocateLocallyUniqueId");
550 if (!pAllocateLocallyUniqueId
) return;
552 ret
= pAllocateLocallyUniqueId(&luid1
);
553 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
557 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
558 ret
= pAllocateLocallyUniqueId(&luid2
);
560 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
561 ok(luid1
.LowPart
> SE_MAX_WELL_KNOWN_PRIVILEGE
|| luid1
.HighPart
!= 0,
562 "AllocateLocallyUniqueId returned a well-known LUID\n");
563 ok(luid1
.LowPart
!= luid2
.LowPart
|| luid1
.HighPart
!= luid2
.HighPart
,
564 "AllocateLocallyUniqueId returned non-unique LUIDs\n");
565 ret
= pAllocateLocallyUniqueId(NULL
);
566 ok( !ret
&& GetLastError() == ERROR_NOACCESS
,
567 "AllocateLocallyUniqueId(NULL) didn't return ERROR_NOACCESS: %d\n",
571 static void test_lookupPrivilegeName(void)
573 BOOL (WINAPI
*pLookupPrivilegeNameA
)(LPCSTR
, PLUID
, LPSTR
, LPDWORD
);
574 char buf
[MAX_PATH
]; /* arbitrary, seems long enough */
575 DWORD cchName
= sizeof(buf
);
576 LUID luid
= { 0, 0 };
580 /* check whether it's available first */
581 pLookupPrivilegeNameA
= (void*)GetProcAddress(hmod
, "LookupPrivilegeNameA");
582 if (!pLookupPrivilegeNameA
) return;
583 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
584 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
585 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
588 /* check with a short buffer */
590 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
591 ret
= pLookupPrivilegeNameA(NULL
, &luid
, NULL
, &cchName
);
592 ok( !ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
593 "LookupPrivilegeNameA didn't fail with ERROR_INSUFFICIENT_BUFFER: %d\n",
595 ok(cchName
== strlen("SeCreateTokenPrivilege") + 1,
596 "LookupPrivilegeNameA returned an incorrect required length for\n"
597 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName
,
598 lstrlenA("SeCreateTokenPrivilege") + 1);
599 /* check a known value and its returned length on success */
600 cchName
= sizeof(buf
);
601 ok(pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
) &&
602 cchName
== strlen("SeCreateTokenPrivilege"),
603 "LookupPrivilegeNameA returned an incorrect output length for\n"
604 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName
,
605 (int)strlen("SeCreateTokenPrivilege"));
606 /* check known values */
607 for (i
= SE_MIN_WELL_KNOWN_PRIVILEGE
; i
<= SE_MAX_WELL_KNOWN_PRIVILEGE
; i
++)
610 cchName
= sizeof(buf
);
611 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
612 ok( ret
|| GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
613 "LookupPrivilegeNameA(0.%d) failed: %d\n", i
, GetLastError());
615 /* check a bogus LUID */
616 luid
.LowPart
= 0xdeadbeef;
617 cchName
= sizeof(buf
);
618 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
619 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
620 "LookupPrivilegeNameA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
622 /* check on a bogus system */
623 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
624 cchName
= sizeof(buf
);
625 ret
= pLookupPrivilegeNameA("b0gu5.Nam3", &luid
, buf
, &cchName
);
626 ok( !ret
&& (GetLastError() == RPC_S_SERVER_UNAVAILABLE
||
627 GetLastError() == RPC_S_INVALID_NET_ADDR
) /* w2k8 */,
628 "LookupPrivilegeNameA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
638 static void test_lookupPrivilegeValue(void)
640 static const struct NameToLUID privs
[] = {
641 { "SeCreateTokenPrivilege", SE_CREATE_TOKEN_PRIVILEGE
},
642 { "SeAssignPrimaryTokenPrivilege", SE_ASSIGNPRIMARYTOKEN_PRIVILEGE
},
643 { "SeLockMemoryPrivilege", SE_LOCK_MEMORY_PRIVILEGE
},
644 { "SeIncreaseQuotaPrivilege", SE_INCREASE_QUOTA_PRIVILEGE
},
645 { "SeMachineAccountPrivilege", SE_MACHINE_ACCOUNT_PRIVILEGE
},
646 { "SeTcbPrivilege", SE_TCB_PRIVILEGE
},
647 { "SeSecurityPrivilege", SE_SECURITY_PRIVILEGE
},
648 { "SeTakeOwnershipPrivilege", SE_TAKE_OWNERSHIP_PRIVILEGE
},
649 { "SeLoadDriverPrivilege", SE_LOAD_DRIVER_PRIVILEGE
},
650 { "SeSystemProfilePrivilege", SE_SYSTEM_PROFILE_PRIVILEGE
},
651 { "SeSystemtimePrivilege", SE_SYSTEMTIME_PRIVILEGE
},
652 { "SeProfileSingleProcessPrivilege", SE_PROF_SINGLE_PROCESS_PRIVILEGE
},
653 { "SeIncreaseBasePriorityPrivilege", SE_INC_BASE_PRIORITY_PRIVILEGE
},
654 { "SeCreatePagefilePrivilege", SE_CREATE_PAGEFILE_PRIVILEGE
},
655 { "SeCreatePermanentPrivilege", SE_CREATE_PERMANENT_PRIVILEGE
},
656 { "SeBackupPrivilege", SE_BACKUP_PRIVILEGE
},
657 { "SeRestorePrivilege", SE_RESTORE_PRIVILEGE
},
658 { "SeShutdownPrivilege", SE_SHUTDOWN_PRIVILEGE
},
659 { "SeDebugPrivilege", SE_DEBUG_PRIVILEGE
},
660 { "SeAuditPrivilege", SE_AUDIT_PRIVILEGE
},
661 { "SeSystemEnvironmentPrivilege", SE_SYSTEM_ENVIRONMENT_PRIVILEGE
},
662 { "SeChangeNotifyPrivilege", SE_CHANGE_NOTIFY_PRIVILEGE
},
663 { "SeRemoteShutdownPrivilege", SE_REMOTE_SHUTDOWN_PRIVILEGE
},
664 { "SeUndockPrivilege", SE_UNDOCK_PRIVILEGE
},
665 { "SeSyncAgentPrivilege", SE_SYNC_AGENT_PRIVILEGE
},
666 { "SeEnableDelegationPrivilege", SE_ENABLE_DELEGATION_PRIVILEGE
},
667 { "SeManageVolumePrivilege", SE_MANAGE_VOLUME_PRIVILEGE
},
668 { "SeImpersonatePrivilege", SE_IMPERSONATE_PRIVILEGE
},
669 { "SeCreateGlobalPrivilege", SE_CREATE_GLOBAL_PRIVILEGE
},
671 BOOL (WINAPI
*pLookupPrivilegeValueA
)(LPCSTR
, LPCSTR
, PLUID
);
676 /* check whether it's available first */
677 pLookupPrivilegeValueA
= (void*)GetProcAddress(hmod
, "LookupPrivilegeValueA");
678 if (!pLookupPrivilegeValueA
) return;
679 ret
= pLookupPrivilegeValueA(NULL
, "SeCreateTokenPrivilege", &luid
);
680 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
683 /* check a bogus system name */
684 ret
= pLookupPrivilegeValueA("b0gu5.Nam3", "SeCreateTokenPrivilege", &luid
);
685 ok( !ret
&& (GetLastError() == RPC_S_SERVER_UNAVAILABLE
||
686 GetLastError() == RPC_S_INVALID_NET_ADDR
) /* w2k8 */,
687 "LookupPrivilegeValueA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
689 /* check a NULL string */
690 ret
= pLookupPrivilegeValueA(NULL
, 0, &luid
);
691 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
692 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
694 /* check a bogus privilege name */
695 ret
= pLookupPrivilegeValueA(NULL
, "SeBogusPrivilege", &luid
);
696 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
697 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
699 /* check case insensitive */
700 ret
= pLookupPrivilegeValueA(NULL
, "sEcREATEtOKENpRIVILEGE", &luid
);
702 "LookupPrivilegeValueA(NULL, sEcREATEtOKENpRIVILEGE, &luid) failed: %d\n",
704 for (i
= 0; i
< sizeof(privs
) / sizeof(privs
[0]); i
++)
706 /* Not all privileges are implemented on all Windows versions, so
707 * don't worry if the call fails
709 if (pLookupPrivilegeValueA(NULL
, privs
[i
].name
, &luid
))
711 ok(luid
.LowPart
== privs
[i
].lowPart
,
712 "LookupPrivilegeValueA returned an invalid LUID for %s\n",
718 static void test_luid(void)
721 test_lookupPrivilegeName();
722 test_lookupPrivilegeValue();
725 static void test_FileSecurity(void)
727 char wintmpdir
[MAX_PATH
];
728 char path
[MAX_PATH
];
729 char file
[MAX_PATH
];
731 DWORD sdSize
, retSize
, rc
, granted
, priv_set_len
;
732 PRIVILEGE_SET priv_set
;
735 GENERIC_MAPPING mapping
= { FILE_READ_DATA
, FILE_WRITE_DATA
, FILE_EXECUTE
, FILE_ALL_ACCESS
};
736 const SECURITY_INFORMATION request
= OWNER_SECURITY_INFORMATION
737 | GROUP_SECURITY_INFORMATION
738 | DACL_SECURITY_INFORMATION
;
740 if (!pGetFileSecurityA
) {
741 win_skip ("GetFileSecurity is not available\n");
745 if (!pSetFileSecurityA
) {
746 win_skip ("SetFileSecurity is not available\n");
750 if (!GetTempPathA (sizeof (wintmpdir
), wintmpdir
)) {
751 win_skip ("GetTempPathA failed\n");
755 /* Create a temporary directory and in it a temporary file */
756 strcat (strcpy (path
, wintmpdir
), "rary");
757 SetLastError(0xdeadbeef);
758 rc
= CreateDirectoryA (path
, NULL
);
759 ok (rc
|| GetLastError() == ERROR_ALREADY_EXISTS
, "CreateDirectoryA "
760 "failed for '%s' with %d\n", path
, GetLastError());
762 strcat (strcpy (file
, path
), "\\ess");
763 SetLastError(0xdeadbeef);
764 fh
= CreateFileA (file
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, NULL
);
765 ok (fh
!= INVALID_HANDLE_VALUE
, "CreateFileA "
766 "failed for '%s' with %d\n", file
, GetLastError());
769 /* For the temporary file ... */
771 /* Get size needed */
773 SetLastError(0xdeadbeef);
774 rc
= pGetFileSecurityA (file
, request
, NULL
, 0, &retSize
);
775 if (!rc
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)) {
776 win_skip("GetFileSecurityA is not implemented\n");
779 ok (!rc
, "GetFileSecurityA "
780 "was expected to fail for '%s'\n", file
);
781 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetFileSecurityA "
782 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
783 ok (retSize
> sizeof (SECURITY_DESCRIPTOR
), "GetFileSecurityA returned size %d\n", retSize
);
786 sd
= HeapAlloc (GetProcessHeap (), 0, sdSize
);
788 /* Get security descriptor for real */
790 SetLastError(0xdeadbeef);
791 rc
= pGetFileSecurityA (file
, request
, sd
, sdSize
, &retSize
);
792 ok (rc
, "GetFileSecurityA "
793 "was not expected to fail '%s': %d\n", file
, GetLastError());
794 ok (retSize
== sdSize
||
795 broken(retSize
== 0), /* NT4 */
796 "GetFileSecurityA returned size %d; expected %d\n", retSize
, sdSize
);
798 /* Use it to set security descriptor */
799 SetLastError(0xdeadbeef);
800 rc
= pSetFileSecurityA (file
, request
, sd
);
801 ok (rc
, "SetFileSecurityA "
802 "was not expected to fail '%s': %d\n", file
, GetLastError());
804 HeapFree (GetProcessHeap (), 0, sd
);
806 /* Repeat for the temporary directory ... */
808 /* Get size needed */
810 SetLastError(0xdeadbeef);
811 rc
= pGetFileSecurityA (path
, request
, NULL
, 0, &retSize
);
812 ok (!rc
, "GetFileSecurityA "
813 "was expected to fail for '%s'\n", path
);
814 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetFileSecurityA "
815 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
816 ok (retSize
> sizeof (SECURITY_DESCRIPTOR
), "GetFileSecurityA returned size %d\n", retSize
);
819 sd
= HeapAlloc (GetProcessHeap (), 0, sdSize
);
821 /* Get security descriptor for real */
823 SetLastError(0xdeadbeef);
824 rc
= pGetFileSecurityA (path
, request
, sd
, sdSize
, &retSize
);
825 ok (rc
, "GetFileSecurityA "
826 "was not expected to fail '%s': %d\n", path
, GetLastError());
827 ok (retSize
== sdSize
||
828 broken(retSize
== 0), /* NT4 */
829 "GetFileSecurityA returned size %d; expected %d\n", retSize
, sdSize
);
831 /* Use it to set security descriptor */
832 SetLastError(0xdeadbeef);
833 rc
= pSetFileSecurityA (path
, request
, sd
);
834 ok (rc
, "SetFileSecurityA "
835 "was not expected to fail '%s': %d\n", path
, GetLastError());
836 HeapFree (GetProcessHeap (), 0, sd
);
839 strcpy (wintmpdir
, "\\Should not exist");
840 SetLastError(0xdeadbeef);
841 rc
= pGetFileSecurityA (wintmpdir
, OWNER_SECURITY_INFORMATION
, NULL
, 0, &sdSize
);
842 ok (!rc
, "GetFileSecurityA should fail for not existing directories/files\n");
843 ok (GetLastError() == ERROR_FILE_NOT_FOUND
,
844 "last error ERROR_FILE_NOT_FOUND expected, got %d\n", GetLastError());
847 /* Remove temporary file and directory */
849 RemoveDirectoryA(path
);
851 /* Test file access permissions for a file with FILE_ATTRIBUTE_ARCHIVE */
852 SetLastError(0xdeadbeef);
853 rc
= GetTempPathA(sizeof(wintmpdir
), wintmpdir
);
854 ok(rc
, "GetTempPath error %d\n", GetLastError());
856 SetLastError(0xdeadbeef);
857 rc
= GetTempFileNameA(wintmpdir
, "tmp", 0, file
);
858 ok(rc
, "GetTempFileName error %d\n", GetLastError());
860 rc
= GetFileAttributesA(file
);
861 rc
&= ~(FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
|FILE_ATTRIBUTE_COMPRESSED
);
862 ok(rc
== FILE_ATTRIBUTE_ARCHIVE
, "expected FILE_ATTRIBUTE_ARCHIVE got %#x\n", rc
);
864 retSize
= 0xdeadbeef;
865 rc
= GetFileSecurityA(file
, OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
867 ok(!rc
, "GetFileSecurity should fail\n");
868 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
869 "expected ERROR_INSUFFICIENT_BUFFER got %d\n", GetLastError());
870 ok(sdSize
> sizeof(SECURITY_DESCRIPTOR
), "got sd size %d\n", sdSize
);
872 sd
= HeapAlloc(GetProcessHeap (), 0, sdSize
);
873 retSize
= 0xdeadbeef;
874 SetLastError(0xdeadbeef);
875 rc
= GetFileSecurityA(file
, OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
876 sd
, sdSize
, &retSize
);
877 ok(rc
, "GetFileSecurity error %d\n", GetLastError());
878 ok(retSize
== sdSize
|| broken(retSize
== 0) /* NT4 */, "expected %d, got %d\n", sdSize
, retSize
);
880 SetLastError(0xdeadbeef);
881 rc
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
, TRUE
, &token
);
882 ok(!rc
, "OpenThreadToken should fail\n");
883 ok(GetLastError() == ERROR_NO_TOKEN
, "expected ERROR_NO_TOKEN, got %d\n", GetLastError());
885 SetLastError(0xdeadbeef);
886 rc
= ImpersonateSelf(SecurityIdentification
);
887 ok(rc
, "ImpersonateSelf error %d\n", GetLastError());
889 SetLastError(0xdeadbeef);
890 rc
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
, TRUE
, &token
);
891 ok(rc
, "OpenThreadToken error %d\n", GetLastError());
893 SetLastError(0xdeadbeef);
895 ok(rc
, "RevertToSelf error %d\n", GetLastError());
897 priv_set_len
= sizeof(priv_set
);
898 granted
= 0xdeadbeef;
900 SetLastError(0xdeadbeef);
901 rc
= AccessCheck(sd
, token
, FILE_READ_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
902 ok(rc
, "AccessCheck error %d\n", GetLastError());
903 ok(status
== 1, "expected 1, got %d\n", status
);
904 ok(granted
== FILE_READ_DATA
, "expected FILE_READ_DATA, got %#x\n", granted
);
906 granted
= 0xdeadbeef;
908 SetLastError(0xdeadbeef);
909 rc
= AccessCheck(sd
, token
, FILE_WRITE_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
910 ok(rc
, "AccessCheck error %d\n", GetLastError());
911 ok(status
== 1, "expected 1, got %d\n", status
);
912 ok(granted
== FILE_WRITE_DATA
, "expected FILE_WRITE_DATA, got %#x\n", granted
);
914 granted
= 0xdeadbeef;
916 SetLastError(0xdeadbeef);
917 rc
= AccessCheck(sd
, token
, FILE_EXECUTE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
918 ok(rc
, "AccessCheck error %d\n", GetLastError());
919 ok(status
== 1, "expected 1, got %d\n", status
);
920 ok(granted
== FILE_EXECUTE
, "expected FILE_EXECUTE, got %#x\n", granted
);
922 granted
= 0xdeadbeef;
924 SetLastError(0xdeadbeef);
925 rc
= AccessCheck(sd
, token
, DELETE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
926 ok(rc
, "AccessCheck error %d\n", GetLastError());
927 ok(status
== 1, "expected 1, got %d\n", status
);
928 ok(granted
== DELETE
, "expected DELETE, got %#x\n", granted
);
930 granted
= 0xdeadbeef;
932 SetLastError(0xdeadbeef);
933 rc
= AccessCheck(sd
, token
, FILE_DELETE_CHILD
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
934 ok(rc
, "AccessCheck error %d\n", GetLastError());
935 ok(status
== 1, "expected 1, got %d\n", status
);
936 ok(granted
== FILE_DELETE_CHILD
, "expected FILE_DELETE_CHILD, got %#x\n", granted
);
938 granted
= 0xdeadbeef;
940 SetLastError(0xdeadbeef);
941 rc
= AccessCheck(sd
, token
, 0x1ff, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
942 ok(rc
, "AccessCheck error %d\n", GetLastError());
943 ok(status
== 1, "expected 1, got %d\n", status
);
944 ok(granted
== 0x1ff, "expected 0x1ff, got %#x\n", granted
);
946 granted
= 0xdeadbeef;
948 SetLastError(0xdeadbeef);
949 rc
= AccessCheck(sd
, token
, FILE_ALL_ACCESS
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
950 ok(rc
, "AccessCheck error %d\n", GetLastError());
951 ok(status
== 1, "expected 1, got %d\n", status
);
952 ok(granted
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", granted
);
954 SetLastError(0xdeadbeef);
955 rc
= AccessCheck(sd
, token
, 0xffffffff, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
956 ok(!rc
, "AccessCheck should fail\n");
957 ok(GetLastError() == ERROR_GENERIC_NOT_MAPPED
, "expected ERROR_GENERIC_NOT_MAPPED, got %d\n", GetLastError());
959 /* Test file access permissions for a file with FILE_ATTRIBUTE_READONLY */
960 SetLastError(0xdeadbeef);
961 fh
= CreateFileA(file
, FILE_READ_DATA
, FILE_SHARE_READ
, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_READONLY
, 0);
962 ok(fh
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
963 retSize
= 0xdeadbeef;
964 SetLastError(0xdeadbeef);
965 rc
= WriteFile(fh
, "1", 1, &retSize
, NULL
);
966 ok(!rc
, "WriteFile should fail\n");
967 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
968 ok(retSize
== 0, "expected 0, got %d\n", retSize
);
971 rc
= GetFileAttributesA(file
);
972 rc
&= ~(FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
|FILE_ATTRIBUTE_COMPRESSED
);
974 ok(rc
== (FILE_ATTRIBUTE_ARCHIVE
|FILE_ATTRIBUTE_READONLY
),
975 "expected FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY got %#x\n", rc
);
977 SetLastError(0xdeadbeef);
978 rc
= SetFileAttributesA(file
, FILE_ATTRIBUTE_ARCHIVE
);
979 ok(rc
, "SetFileAttributes error %d\n", GetLastError());
980 SetLastError(0xdeadbeef);
981 rc
= DeleteFileA(file
);
982 ok(rc
, "DeleteFile error %d\n", GetLastError());
984 SetLastError(0xdeadbeef);
985 fh
= CreateFileA(file
, FILE_READ_DATA
, FILE_SHARE_READ
, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_READONLY
, 0);
986 ok(fh
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
987 retSize
= 0xdeadbeef;
988 SetLastError(0xdeadbeef);
989 rc
= WriteFile(fh
, "1", 1, &retSize
, NULL
);
990 ok(!rc
, "WriteFile should fail\n");
991 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
992 ok(retSize
== 0, "expected 0, got %d\n", retSize
);
995 rc
= GetFileAttributesA(file
);
996 rc
&= ~(FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
|FILE_ATTRIBUTE_COMPRESSED
);
997 ok(rc
== (FILE_ATTRIBUTE_ARCHIVE
|FILE_ATTRIBUTE_READONLY
),
998 "expected FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY got %#x\n", rc
);
1000 retSize
= 0xdeadbeef;
1001 SetLastError(0xdeadbeef);
1002 rc
= GetFileSecurityA(file
, OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
1003 sd
, sdSize
, &retSize
);
1004 ok(rc
, "GetFileSecurity error %d\n", GetLastError());
1005 ok(retSize
== sdSize
|| broken(retSize
== 0) /* NT4 */, "expected %d, got %d\n", sdSize
, retSize
);
1007 priv_set_len
= sizeof(priv_set
);
1008 granted
= 0xdeadbeef;
1009 status
= 0xdeadbeef;
1010 SetLastError(0xdeadbeef);
1011 rc
= AccessCheck(sd
, token
, FILE_READ_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1012 ok(rc
, "AccessCheck error %d\n", GetLastError());
1013 ok(status
== 1, "expected 1, got %d\n", status
);
1014 ok(granted
== FILE_READ_DATA
, "expected FILE_READ_DATA, got %#x\n", granted
);
1016 granted
= 0xdeadbeef;
1017 status
= 0xdeadbeef;
1018 SetLastError(0xdeadbeef);
1019 rc
= AccessCheck(sd
, token
, FILE_WRITE_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1020 ok(rc
, "AccessCheck error %d\n", GetLastError());
1022 ok(status
== 1, "expected 1, got %d\n", status
);
1023 ok(granted
== FILE_WRITE_DATA
, "expected FILE_WRITE_DATA, got %#x\n", granted
);
1025 granted
= 0xdeadbeef;
1026 status
= 0xdeadbeef;
1027 SetLastError(0xdeadbeef);
1028 rc
= AccessCheck(sd
, token
, FILE_EXECUTE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1029 ok(rc
, "AccessCheck error %d\n", GetLastError());
1030 ok(status
== 1, "expected 1, got %d\n", status
);
1031 ok(granted
== FILE_EXECUTE
, "expected FILE_EXECUTE, got %#x\n", granted
);
1033 granted
= 0xdeadbeef;
1034 status
= 0xdeadbeef;
1035 SetLastError(0xdeadbeef);
1036 rc
= AccessCheck(sd
, token
, DELETE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1037 ok(rc
, "AccessCheck error %d\n", GetLastError());
1039 ok(status
== 1, "expected 1, got %d\n", status
);
1040 ok(granted
== DELETE
, "expected DELETE, got %#x\n", granted
);
1042 granted
= 0xdeadbeef;
1043 status
= 0xdeadbeef;
1044 SetLastError(0xdeadbeef);
1045 rc
= AccessCheck(sd
, token
, FILE_DELETE_CHILD
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1046 ok(rc
, "AccessCheck error %d\n", GetLastError());
1048 ok(status
== 1, "expected 1, got %d\n", status
);
1049 ok(granted
== FILE_DELETE_CHILD
, "expected FILE_DELETE_CHILD, got %#x\n", granted
);
1051 granted
= 0xdeadbeef;
1052 status
= 0xdeadbeef;
1053 SetLastError(0xdeadbeef);
1054 rc
= AccessCheck(sd
, token
, 0x1ff, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1055 ok(rc
, "AccessCheck error %d\n", GetLastError());
1057 ok(status
== 1, "expected 1, got %d\n", status
);
1058 ok(granted
== 0x1ff, "expected 0x1ff, got %#x\n", granted
);
1060 granted
= 0xdeadbeef;
1061 status
= 0xdeadbeef;
1062 SetLastError(0xdeadbeef);
1063 rc
= AccessCheck(sd
, token
, FILE_ALL_ACCESS
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1064 ok(rc
, "AccessCheck error %d\n", GetLastError());
1066 ok(status
== 1, "expected 1, got %d\n", status
);
1067 ok(granted
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", granted
);
1069 SetLastError(0xdeadbeef);
1070 rc
= DeleteFileA(file
);
1071 ok(!rc
, "DeleteFile should fail\n");
1072 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1073 SetLastError(0xdeadbeef);
1074 rc
= SetFileAttributesA(file
, FILE_ATTRIBUTE_ARCHIVE
);
1075 ok(rc
, "SetFileAttributes error %d\n", GetLastError());
1076 SetLastError(0xdeadbeef);
1077 rc
= DeleteFileA(file
);
1078 ok(rc
, "DeleteFile error %d\n", GetLastError());
1081 HeapFree(GetProcessHeap(), 0, sd
);
1084 static void test_AccessCheck(void)
1086 PSID EveryoneSid
= NULL
, AdminSid
= NULL
, UsersSid
= NULL
;
1088 SECURITY_DESCRIPTOR
*SecurityDescriptor
= NULL
;
1089 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
1090 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
1091 GENERIC_MAPPING Mapping
= { KEY_READ
, KEY_WRITE
, KEY_EXECUTE
, KEY_ALL_ACCESS
};
1095 HANDLE ProcessToken
;
1098 PRIVILEGE_SET
*PrivSet
;
1100 HMODULE NtDllModule
;
1103 NTSTATUS ntret
, ntAccessStatus
;
1105 NtDllModule
= GetModuleHandleA("ntdll.dll");
1108 skip("not running on NT, skipping test\n");
1111 pRtlAdjustPrivilege
= (void *)GetProcAddress(NtDllModule
, "RtlAdjustPrivilege");
1112 if (!pRtlAdjustPrivilege
)
1114 win_skip("missing RtlAdjustPrivilege, skipping test\n");
1118 Acl
= HeapAlloc(GetProcessHeap(), 0, 256);
1119 res
= InitializeAcl(Acl
, 256, ACL_REVISION
);
1120 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
1122 skip("ACLs not implemented - skipping tests\n");
1123 HeapFree(GetProcessHeap(), 0, Acl
);
1126 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
1128 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
1129 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1131 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1132 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &AdminSid
);
1133 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1135 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1136 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
1137 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1139 SecurityDescriptor
= HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
1141 res
= InitializeSecurityDescriptor(SecurityDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
1142 ok(res
, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
1144 res
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
1145 ok(res
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1147 PrivSetLen
= FIELD_OFFSET(PRIVILEGE_SET
, Privilege
[16]);
1148 PrivSet
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, PrivSetLen
);
1149 PrivSet
->PrivilegeCount
= 16;
1151 res
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &ProcessToken
);
1152 ok(res
, "OpenProcessToken failed with error %d\n", GetLastError());
1154 pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE
, FALSE
, TRUE
, &Enabled
);
1156 res
= DuplicateToken(ProcessToken
, SecurityImpersonation
, &Token
);
1157 ok(res
, "DuplicateToken failed with error %d\n", GetLastError());
1159 /* SD without owner/group */
1160 SetLastError(0xdeadbeef);
1161 Access
= AccessStatus
= 0x1abe11ed;
1162 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_QUERY_VALUE
, &Mapping
,
1163 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1164 err
= GetLastError();
1165 ok(!ret
&& err
== ERROR_INVALID_SECURITY_DESCR
, "AccessCheck should have "
1166 "failed with ERROR_INVALID_SECURITY_DESCR, instead of %d\n", err
);
1167 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1168 "Access and/or AccessStatus were changed!\n");
1170 /* Set owner and group */
1171 res
= SetSecurityDescriptorOwner(SecurityDescriptor
, AdminSid
, FALSE
);
1172 ok(res
, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
1173 res
= SetSecurityDescriptorGroup(SecurityDescriptor
, UsersSid
, TRUE
);
1174 ok(res
, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
1176 /* Generic access mask */
1177 SetLastError(0xdeadbeef);
1178 Access
= AccessStatus
= 0x1abe11ed;
1179 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1180 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1181 err
= GetLastError();
1182 ok(!ret
&& err
== ERROR_GENERIC_NOT_MAPPED
, "AccessCheck should have failed "
1183 "with ERROR_GENERIC_NOT_MAPPED, instead of %d\n", err
);
1184 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1185 "Access and/or AccessStatus were changed!\n");
1187 /* Generic access mask - no privilegeset buffer */
1188 SetLastError(0xdeadbeef);
1189 Access
= AccessStatus
= 0x1abe11ed;
1190 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1191 NULL
, &PrivSetLen
, &Access
, &AccessStatus
);
1192 err
= GetLastError();
1193 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1194 "with ERROR_NOACCESS, instead of %d\n", err
);
1195 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1196 "Access and/or AccessStatus were changed!\n");
1198 /* Generic access mask - no returnlength */
1199 SetLastError(0xdeadbeef);
1200 Access
= AccessStatus
= 0x1abe11ed;
1201 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1202 PrivSet
, NULL
, &Access
, &AccessStatus
);
1203 err
= GetLastError();
1204 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1205 "with ERROR_NOACCESS, instead of %d\n", err
);
1206 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1207 "Access and/or AccessStatus were changed!\n");
1209 /* Generic access mask - no privilegeset buffer, no returnlength */
1210 SetLastError(0xdeadbeef);
1211 Access
= AccessStatus
= 0x1abe11ed;
1212 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1213 NULL
, NULL
, &Access
, &AccessStatus
);
1214 err
= GetLastError();
1215 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1216 "with ERROR_NOACCESS, instead of %d\n", err
);
1217 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1218 "Access and/or AccessStatus were changed!\n");
1220 /* sd with no dacl present */
1221 Access
= AccessStatus
= 0x1abe11ed;
1222 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, FALSE
, NULL
, FALSE
);
1223 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1224 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1225 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1226 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1227 ok(AccessStatus
&& (Access
== KEY_READ
),
1228 "AccessCheck failed to grant access with error %d\n",
1231 /* sd with no dacl present - no privilegeset buffer */
1232 SetLastError(0xdeadbeef);
1233 Access
= AccessStatus
= 0x1abe11ed;
1234 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1235 NULL
, &PrivSetLen
, &Access
, &AccessStatus
);
1236 err
= GetLastError();
1237 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1238 "with ERROR_NOACCESS, instead of %d\n", err
);
1239 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1240 "Access and/or AccessStatus were changed!\n");
1244 /* Generic access mask - no privilegeset buffer */
1245 SetLastError(0xdeadbeef);
1246 Access
= ntAccessStatus
= 0x1abe11ed;
1247 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1248 NULL
, &PrivSetLen
, &Access
, &ntAccessStatus
);
1249 err
= GetLastError();
1250 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1251 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1252 ok(err
== 0xdeadbeef,
1253 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1254 ok(Access
== 0x1abe11ed && ntAccessStatus
== 0x1abe11ed,
1255 "Access and/or AccessStatus were changed!\n");
1257 /* Generic access mask - no returnlength */
1258 SetLastError(0xdeadbeef);
1259 Access
= ntAccessStatus
= 0x1abe11ed;
1260 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1261 PrivSet
, NULL
, &Access
, &ntAccessStatus
);
1262 err
= GetLastError();
1263 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1264 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1265 ok(err
== 0xdeadbeef,
1266 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1267 ok(Access
== 0x1abe11ed && ntAccessStatus
== 0x1abe11ed,
1268 "Access and/or AccessStatus were changed!\n");
1270 /* Generic access mask - no privilegeset buffer, no returnlength */
1271 SetLastError(0xdeadbeef);
1272 Access
= ntAccessStatus
= 0x1abe11ed;
1273 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1274 NULL
, NULL
, &Access
, &ntAccessStatus
);
1275 err
= GetLastError();
1276 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1277 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1278 ok(err
== 0xdeadbeef,
1279 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1280 ok(Access
== 0x1abe11ed && ntAccessStatus
== 0x1abe11ed,
1281 "Access and/or AccessStatus were changed!\n");
1284 win_skip("NtAccessCheck unavailable. Skipping.\n");
1286 /* sd with NULL dacl */
1287 Access
= AccessStatus
= 0x1abe11ed;
1288 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, NULL
, FALSE
);
1289 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1290 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1291 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1292 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1293 ok(AccessStatus
&& (Access
== KEY_READ
),
1294 "AccessCheck failed to grant access with error %d\n",
1297 /* sd with blank dacl */
1298 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
1299 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1300 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1301 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1302 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1303 err
= GetLastError();
1304 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1305 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1306 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1308 res
= AddAccessAllowedAce(Acl
, ACL_REVISION
, KEY_READ
, EveryoneSid
);
1309 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
1311 res
= AddAccessDeniedAce(Acl
, ACL_REVISION
, KEY_SET_VALUE
, AdminSid
);
1312 ok(res
, "AddAccessDeniedAce failed with error %d\n", GetLastError());
1315 Access
= AccessStatus
= 0x1abe11ed;
1316 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1317 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1318 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1319 ok(AccessStatus
&& (Access
== KEY_READ
),
1320 "AccessCheck failed to grant access with error %d\n",
1323 ret
= AccessCheck(SecurityDescriptor
, Token
, MAXIMUM_ALLOWED
, &Mapping
,
1324 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1325 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1327 "AccessCheck failed to grant any access with error %d\n",
1329 trace("AccessCheck with MAXIMUM_ALLOWED got Access 0x%08x\n", Access
);
1331 /* Access denied by SD */
1332 SetLastError(0xdeadbeef);
1333 Access
= AccessStatus
= 0x1abe11ed;
1334 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_WRITE
, &Mapping
,
1335 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1336 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1337 err
= GetLastError();
1338 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1339 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1340 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1343 PrivSet
->PrivilegeCount
= 16;
1344 ret
= AccessCheck(SecurityDescriptor
, Token
, ACCESS_SYSTEM_SECURITY
, &Mapping
,
1345 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1346 ok(ret
&& !AccessStatus
&& GetLastError() == ERROR_PRIVILEGE_NOT_HELD
,
1347 "AccessCheck should have failed with ERROR_PRIVILEGE_NOT_HELD, instead of %d\n",
1350 ret
= ImpersonateLoggedOnUser(Token
);
1351 ok(ret
, "ImpersonateLoggedOnUser failed with error %d\n", GetLastError());
1352 ret
= pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE
, TRUE
, TRUE
, &Enabled
);
1356 PrivSet
->PrivilegeCount
= 16;
1357 ret
= AccessCheck(SecurityDescriptor
, Token
, ACCESS_SYSTEM_SECURITY
, &Mapping
,
1358 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1359 ok(ret
&& AccessStatus
&& GetLastError() == 0,
1360 "AccessCheck should have succeeded, error %d\n",
1362 ok(Access
== ACCESS_SYSTEM_SECURITY
,
1363 "Access should be equal to ACCESS_SYSTEM_SECURITY instead of 0x%08x\n",
1367 trace("Couldn't get SE_SECURITY_PRIVILEGE (0x%08x), skipping ACCESS_SYSTEM_SECURITY test\n",
1369 ret
= RevertToSelf();
1370 ok(ret
, "RevertToSelf failed with error %d\n", GetLastError());
1372 /* test INHERIT_ONLY_ACE */
1373 ret
= InitializeAcl(Acl
, 256, ACL_REVISION
);
1374 ok(ret
, "InitializeAcl failed with error %d\n", GetLastError());
1376 /* NT doesn't have AddAccessAllowedAceEx. Skipping this call/test doesn't influence
1379 if (pAddAccessAllowedAceEx
)
1381 ret
= pAddAccessAllowedAceEx(Acl
, ACL_REVISION
, INHERIT_ONLY_ACE
, KEY_READ
, EveryoneSid
);
1382 ok(ret
, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
1385 win_skip("AddAccessAllowedAceEx is not available\n");
1387 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1388 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1389 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1390 err
= GetLastError();
1391 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1392 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1393 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1397 res
= DuplicateToken(ProcessToken
, SecurityAnonymous
, &Token
);
1398 ok(res
, "DuplicateToken failed with error %d\n", GetLastError());
1400 SetLastError(0xdeadbeef);
1401 ret
= AccessCheck(SecurityDescriptor
, Token
, MAXIMUM_ALLOWED
, &Mapping
,
1402 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1403 err
= GetLastError();
1404 ok(!ret
&& err
== ERROR_BAD_IMPERSONATION_LEVEL
, "AccessCheck should have failed "
1405 "with ERROR_BAD_IMPERSONATION_LEVEL, instead of %d\n", err
);
1409 SetLastError(0xdeadbeef);
1410 ret
= AccessCheck(SecurityDescriptor
, ProcessToken
, KEY_READ
, &Mapping
,
1411 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1412 err
= GetLastError();
1413 ok(!ret
&& err
== ERROR_NO_IMPERSONATION_TOKEN
, "AccessCheck should have failed "
1414 "with ERROR_NO_IMPERSONATION_TOKEN, instead of %d\n", err
);
1416 CloseHandle(ProcessToken
);
1419 FreeSid(EveryoneSid
);
1424 HeapFree(GetProcessHeap(), 0, Acl
);
1425 HeapFree(GetProcessHeap(), 0, SecurityDescriptor
);
1426 HeapFree(GetProcessHeap(), 0, PrivSet
);
1429 /* test GetTokenInformation for the various attributes */
1430 static void test_token_attr(void)
1432 HANDLE Token
, ImpersonationToken
;
1434 TOKEN_PRIVILEGES
*Privileges
;
1435 TOKEN_GROUPS
*Groups
;
1437 TOKEN_DEFAULT_DACL
*Dacl
;
1441 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
;
1444 /* cygwin-like use case */
1445 SetLastError(0xdeadbeef);
1446 ret
= OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED
, &Token
);
1447 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
1449 win_skip("OpenProcessToken is not implemented\n");
1452 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1455 DWORD buf
[256]; /* GetTokenInformation wants a dword-aligned buffer */
1457 ret
= GetTokenInformation(Token
, TokenUser
,(void*)buf
, Size
, &Size
);
1458 ok(ret
, "GetTokenInformation failed with error %d\n", GetLastError());
1459 Size
= sizeof(ImpersonationLevel
);
1460 ret
= GetTokenInformation(Token
, TokenImpersonationLevel
, &ImpersonationLevel
, Size
, &Size
);
1461 GLE
= GetLastError();
1462 ok(!ret
&& (GLE
== ERROR_INVALID_PARAMETER
), "GetTokenInformation(TokenImpersonationLevel) on primary token should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GLE
);
1466 if(!pConvertSidToStringSidA
)
1468 win_skip("ConvertSidToStringSidA is not available\n");
1472 SetLastError(0xdeadbeef);
1473 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS
, &Token
);
1474 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1477 /* insufficient buffer length */
1478 SetLastError(0xdeadbeef);
1480 ret
= GetTokenInformation(Token
, TokenGroups
, NULL
, 0, &Size2
);
1481 ok(Size2
> 1, "got %d\n", Size2
);
1482 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1483 "%d with error %d\n", ret
, GetLastError());
1485 Groups
= HeapAlloc(GetProcessHeap(), 0, Size2
);
1486 memset(Groups
, 0xcc, Size2
);
1488 ret
= GetTokenInformation(Token
, TokenGroups
, Groups
, Size2
, &Size
);
1489 ok(Size
> 1, "got %d\n", Size
);
1490 ok((!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
) || broken(ret
) /* wow64 */,
1491 "%d with error %d\n", ret
, GetLastError());
1493 ok(*((BYTE
*)Groups
) == 0xcc, "buffer altered\n");
1495 HeapFree(GetProcessHeap(), 0, Groups
);
1497 SetLastError(0xdeadbeef);
1498 ret
= GetTokenInformation(Token
, TokenGroups
, NULL
, 0, &Size
);
1499 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1500 "GetTokenInformation(TokenGroups) %s with error %d\n",
1501 ret
? "succeeded" : "failed", GetLastError());
1502 Groups
= HeapAlloc(GetProcessHeap(), 0, Size
);
1503 SetLastError(0xdeadbeef);
1504 ret
= GetTokenInformation(Token
, TokenGroups
, Groups
, Size
, &Size
);
1505 ok(ret
, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
1506 ok(GetLastError() == 0xdeadbeef,
1507 "GetTokenInformation shouldn't have set last error to %d\n",
1509 trace("TokenGroups:\n");
1510 for (i
= 0; i
< Groups
->GroupCount
; i
++)
1512 DWORD NameLength
= 255;
1514 DWORD DomainLength
= 255;
1516 SID_NAME_USE SidNameUse
;
1519 ret
= LookupAccountSidA(NULL
, Groups
->Groups
[i
].Sid
, Name
, &NameLength
, Domain
, &DomainLength
, &SidNameUse
);
1522 pConvertSidToStringSidA(Groups
->Groups
[i
].Sid
, &SidString
);
1523 trace("%s, %s\\%s use: %d attr: 0x%08x\n", SidString
, Domain
, Name
, SidNameUse
, Groups
->Groups
[i
].Attributes
);
1524 LocalFree(SidString
);
1526 else trace("attr: 0x%08x LookupAccountSid failed with error %d\n", Groups
->Groups
[i
].Attributes
, GetLastError());
1528 HeapFree(GetProcessHeap(), 0, Groups
);
1531 ret
= GetTokenInformation(Token
, TokenUser
, NULL
, 0, &Size
);
1532 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1533 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1534 User
= HeapAlloc(GetProcessHeap(), 0, Size
);
1535 ret
= GetTokenInformation(Token
, TokenUser
, User
, Size
, &Size
);
1537 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1539 pConvertSidToStringSidA(User
->User
.Sid
, &SidString
);
1540 trace("TokenUser: %s attr: 0x%08x\n", SidString
, User
->User
.Attributes
);
1541 LocalFree(SidString
);
1542 HeapFree(GetProcessHeap(), 0, User
);
1545 ret
= GetTokenInformation(Token
, TokenPrivileges
, NULL
, 0, &Size
);
1546 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1547 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1548 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
1549 ret
= GetTokenInformation(Token
, TokenPrivileges
, Privileges
, Size
, &Size
);
1551 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1552 trace("TokenPrivileges:\n");
1553 for (i
= 0; i
< Privileges
->PrivilegeCount
; i
++)
1556 DWORD NameLen
= sizeof(Name
)/sizeof(Name
[0]);
1557 LookupPrivilegeNameA(NULL
, &Privileges
->Privileges
[i
].Luid
, Name
, &NameLen
);
1558 trace("\t%s, 0x%x\n", Name
, Privileges
->Privileges
[i
].Attributes
);
1560 HeapFree(GetProcessHeap(), 0, Privileges
);
1562 ret
= DuplicateToken(Token
, SecurityAnonymous
, &ImpersonationToken
);
1563 ok(ret
, "DuplicateToken failed with error %d\n", GetLastError());
1565 Size
= sizeof(ImpersonationLevel
);
1566 ret
= GetTokenInformation(ImpersonationToken
, TokenImpersonationLevel
, &ImpersonationLevel
, Size
, &Size
);
1567 ok(ret
, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1568 ok(ImpersonationLevel
== SecurityAnonymous
, "ImpersonationLevel should have been SecurityAnonymous instead of %d\n", ImpersonationLevel
);
1570 CloseHandle(ImpersonationToken
);
1573 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, NULL
, 0, &Size
);
1574 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1575 "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1577 Dacl
= HeapAlloc(GetProcessHeap(), 0, Size
);
1578 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size
);
1579 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1581 SetLastError(0xdeadbeef);
1582 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, NULL
, 0);
1583 GLE
= GetLastError();
1584 ok(!ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1585 ok(GLE
== ERROR_BAD_LENGTH
, "expected ERROR_BAD_LENGTH got %u\n", GLE
);
1587 SetLastError(0xdeadbeef);
1588 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, NULL
, Size
);
1589 GLE
= GetLastError();
1590 ok(!ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1591 ok(GLE
== ERROR_NOACCESS
, "expected ERROR_NOACCESS got %u\n", GLE
);
1593 acl
= Dacl
->DefaultDacl
;
1594 Dacl
->DefaultDacl
= NULL
;
1596 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
);
1597 ok(ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1600 Dacl
->DefaultDacl
= (ACL
*)0xdeadbeef;
1601 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size2
);
1602 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1603 ok(Dacl
->DefaultDacl
== NULL
, "expected NULL, got %p\n", Dacl
->DefaultDacl
);
1604 ok(Size2
== sizeof(TOKEN_DEFAULT_DACL
) || broken(Size2
== 2*sizeof(TOKEN_DEFAULT_DACL
)), /* WoW64 */
1605 "got %u expected sizeof(TOKEN_DEFAULT_DACL)\n", Size2
);
1607 Dacl
->DefaultDacl
= acl
;
1608 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
);
1609 ok(ret
, "SetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1611 if (Size2
== sizeof(TOKEN_DEFAULT_DACL
)) {
1612 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size2
);
1613 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1615 win_skip("TOKEN_DEFAULT_DACL size too small on WoW64\n");
1617 HeapFree(GetProcessHeap(), 0, Dacl
);
1621 typedef union _MAX_SID
1624 char max
[SECURITY_MAX_SID_SIZE
];
1627 static void test_sid_str(PSID
* sid
)
1630 BOOL ret
= pConvertSidToStringSidA(sid
, &str_sid
);
1631 ok(ret
, "ConvertSidToStringSidA() failed: %d\n", GetLastError());
1634 char account
[MAX_PATH
], domain
[MAX_PATH
];
1636 DWORD acc_size
= MAX_PATH
;
1637 DWORD dom_size
= MAX_PATH
;
1638 ret
= LookupAccountSidA (NULL
, sid
, account
, &acc_size
, domain
, &dom_size
, &use
);
1639 ok(ret
|| (!ret
&& (GetLastError() == ERROR_NONE_MAPPED
)),
1640 "LookupAccountSid(%s) failed: %d\n", str_sid
, GetLastError());
1642 trace(" %s %s\\%s %d\n", str_sid
, domain
, account
, use
);
1643 else if (GetLastError() == ERROR_NONE_MAPPED
)
1644 trace(" %s couldn't be mapped\n", str_sid
);
1649 static const struct well_known_sid_value
1651 BOOL without_domain
;
1652 const char *sid_string
;
1653 } well_known_sid_values
[] = {
1654 /* 0 */ {TRUE
, "S-1-0-0"}, {TRUE
, "S-1-1-0"}, {TRUE
, "S-1-2-0"}, {TRUE
, "S-1-3-0"},
1655 /* 4 */ {TRUE
, "S-1-3-1"}, {TRUE
, "S-1-3-2"}, {TRUE
, "S-1-3-3"}, {TRUE
, "S-1-5"},
1656 /* 8 */ {FALSE
, "S-1-5-1"}, {TRUE
, "S-1-5-2"}, {TRUE
, "S-1-5-3"}, {TRUE
, "S-1-5-4"},
1657 /* 12 */ {TRUE
, "S-1-5-6"}, {TRUE
, "S-1-5-7"}, {TRUE
, "S-1-5-8"}, {TRUE
, "S-1-5-9"},
1658 /* 16 */ {TRUE
, "S-1-5-10"}, {TRUE
, "S-1-5-11"}, {TRUE
, "S-1-5-12"}, {TRUE
, "S-1-5-13"},
1659 /* 20 */ {TRUE
, "S-1-5-14"}, {FALSE
, NULL
}, {TRUE
, "S-1-5-18"}, {TRUE
, "S-1-5-19"},
1660 /* 24 */ {TRUE
, "S-1-5-20"}, {TRUE
, "S-1-5-32"},
1661 /* 26 */ {FALSE
, "S-1-5-32-544"}, {TRUE
, "S-1-5-32-545"}, {TRUE
, "S-1-5-32-546"},
1662 /* 29 */ {TRUE
, "S-1-5-32-547"}, {TRUE
, "S-1-5-32-548"}, {TRUE
, "S-1-5-32-549"},
1663 /* 32 */ {TRUE
, "S-1-5-32-550"}, {TRUE
, "S-1-5-32-551"}, {TRUE
, "S-1-5-32-552"},
1664 /* 35 */ {TRUE
, "S-1-5-32-554"}, {TRUE
, "S-1-5-32-555"}, {TRUE
, "S-1-5-32-556"},
1665 /* 38 */ {FALSE
, "S-1-5-21-12-23-34-45-56-500"}, {FALSE
, "S-1-5-21-12-23-34-45-56-501"},
1666 /* 40 */ {FALSE
, "S-1-5-21-12-23-34-45-56-502"}, {FALSE
, "S-1-5-21-12-23-34-45-56-512"},
1667 /* 42 */ {FALSE
, "S-1-5-21-12-23-34-45-56-513"}, {FALSE
, "S-1-5-21-12-23-34-45-56-514"},
1668 /* 44 */ {FALSE
, "S-1-5-21-12-23-34-45-56-515"}, {FALSE
, "S-1-5-21-12-23-34-45-56-516"},
1669 /* 46 */ {FALSE
, "S-1-5-21-12-23-34-45-56-517"}, {FALSE
, "S-1-5-21-12-23-34-45-56-518"},
1670 /* 48 */ {FALSE
, "S-1-5-21-12-23-34-45-56-519"}, {FALSE
, "S-1-5-21-12-23-34-45-56-520"},
1671 /* 50 */ {FALSE
, "S-1-5-21-12-23-34-45-56-553"},
1672 /* Added in Windows Server 2003 */
1673 /* 51 */ {TRUE
, "S-1-5-64-10"}, {TRUE
, "S-1-5-64-21"}, {TRUE
, "S-1-5-64-14"},
1674 /* 54 */ {TRUE
, "S-1-5-15"}, {TRUE
, "S-1-5-1000"}, {FALSE
, "S-1-5-32-557"},
1675 /* 57 */ {TRUE
, "S-1-5-32-558"}, {TRUE
, "S-1-5-32-559"}, {TRUE
, "S-1-5-32-560"},
1676 /* 60 */ {TRUE
, "S-1-5-32-561"}, {TRUE
, "S-1-5-32-562"},
1677 /* Added in Windows Vista: */
1678 /* 62 */ {TRUE
, "S-1-5-32-568"},
1679 /* 63 */ {TRUE
, "S-1-5-17"}, {FALSE
, "S-1-5-32-569"}, {TRUE
, "S-1-16-0"},
1680 /* 66 */ {TRUE
, "S-1-16-4096"}, {TRUE
, "S-1-16-8192"}, {TRUE
, "S-1-16-12288"},
1681 /* 69 */ {TRUE
, "S-1-16-16384"}, {TRUE
, "S-1-5-33"}, {TRUE
, "S-1-3-4"},
1682 /* 72 */ {FALSE
, "S-1-5-21-12-23-34-45-56-571"}, {FALSE
, "S-1-5-21-12-23-34-45-56-572"},
1683 /* 74 */ {TRUE
, "S-1-5-22"}, {FALSE
, "S-1-5-21-12-23-34-45-56-521"}, {TRUE
, "S-1-5-32-573"},
1684 /* 77 */ {FALSE
, "S-1-5-21-12-23-34-45-56-498"}, {TRUE
, "S-1-5-32-574"}, {TRUE
, "S-1-16-8448"}
1687 static void test_CreateWellKnownSid(void)
1689 SID_IDENTIFIER_AUTHORITY ident
= { SECURITY_NT_AUTHORITY
};
1690 PSID domainsid
, sid
;
1695 if (!pCreateWellKnownSid
)
1697 win_skip("CreateWellKnownSid not available\n");
1702 SetLastError(0xdeadbeef);
1703 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, NULL
, &size
);
1704 error
= GetLastError();
1705 ok(!ret
, "CreateWellKnownSid succeeded\n");
1706 ok(error
== ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error
);
1707 ok(size
, "expected size > 0\n");
1709 SetLastError(0xdeadbeef);
1710 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, NULL
, &size
);
1711 error
= GetLastError();
1712 ok(!ret
, "CreateWellKnownSid succeeded\n");
1713 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %u\n", error
);
1715 sid
= HeapAlloc(GetProcessHeap(), 0, size
);
1716 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, sid
, &size
);
1717 ok(ret
, "CreateWellKnownSid failed %u\n", GetLastError());
1718 HeapFree(GetProcessHeap(), 0, sid
);
1720 /* a domain sid usually have three subauthorities but we test that CreateWellKnownSid doesn't check it */
1721 AllocateAndInitializeSid(&ident
, 6, SECURITY_NT_NON_UNIQUE
, 12, 23, 34, 45, 56, 0, 0, &domainsid
);
1723 for (i
= 0; i
< sizeof(well_known_sid_values
)/sizeof(well_known_sid_values
[0]); i
++)
1725 const struct well_known_sid_value
*value
= &well_known_sid_values
[i
];
1726 char sid_buffer
[SECURITY_MAX_SID_SIZE
];
1730 if (value
->sid_string
== NULL
)
1733 if (i
> WinAccountRasAndIasServersSid
)
1735 /* These SIDs aren't implemented by all Windows versions - detect it and break the loop */
1736 cb
= sizeof(sid_buffer
);
1737 if (!pCreateWellKnownSid(i
, domainsid
, sid_buffer
, &cb
))
1739 skip("Well known SIDs starting from %u are not implemented\n", i
);
1744 cb
= sizeof(sid_buffer
);
1745 ok(pCreateWellKnownSid(i
, value
->without_domain
? NULL
: domainsid
, sid_buffer
, &cb
), "Couldn't create well known sid %u\n", i
);
1746 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer
)), cb
, DWORD
, "%d");
1747 ok(IsValidSid(sid_buffer
), "The sid is not valid\n");
1748 ok(pConvertSidToStringSidA(sid_buffer
, &str
), "Couldn't convert SID to string\n");
1749 ok(strcmp(str
, value
->sid_string
) == 0, "%d: SID mismatch - expected %s, got %s\n", i
,
1750 value
->sid_string
, str
);
1753 if (value
->without_domain
)
1755 char buf2
[SECURITY_MAX_SID_SIZE
];
1757 ok(pCreateWellKnownSid(i
, domainsid
, buf2
, &cb
), "Couldn't create well known sid %u with optional domain\n", i
);
1758 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer
)), cb
, DWORD
, "%d");
1759 ok(memcmp(buf2
, sid_buffer
, cb
) == 0, "SID create with domain is different than without (%u)\n", i
);
1766 static void test_LookupAccountSid(void)
1768 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
1769 CHAR accountA
[MAX_PATH
], domainA
[MAX_PATH
], usernameA
[MAX_PATH
];
1770 DWORD acc_sizeA
, dom_sizeA
, user_sizeA
;
1771 DWORD real_acc_sizeA
, real_dom_sizeA
;
1772 WCHAR accountW
[MAX_PATH
], domainW
[MAX_PATH
];
1773 DWORD acc_sizeW
, dom_sizeW
;
1774 DWORD real_acc_sizeW
, real_dom_sizeW
;
1775 PSID pUsersSid
= NULL
;
1778 DWORD error
, size
, cbti
= 0;
1783 PTOKEN_USER ptiUser
= NULL
;
1785 /* native windows crashes if account size, domain size, or name use is NULL */
1787 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1788 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &pUsersSid
);
1789 ok(ret
|| (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
),
1790 "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1792 /* not running on NT so give up */
1793 if (!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
1796 real_acc_sizeA
= MAX_PATH
;
1797 real_dom_sizeA
= MAX_PATH
;
1798 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &real_acc_sizeA
, domainA
, &real_dom_sizeA
, &use
);
1799 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1801 /* try NULL account */
1802 acc_sizeA
= MAX_PATH
;
1803 dom_sizeA
= MAX_PATH
;
1804 ret
= LookupAccountSidA(NULL
, pUsersSid
, NULL
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1805 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1807 /* try NULL domain */
1808 acc_sizeA
= MAX_PATH
;
1809 dom_sizeA
= MAX_PATH
;
1810 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, NULL
, &dom_sizeA
, &use
);
1811 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1813 /* try a small account buffer */
1815 dom_sizeA
= MAX_PATH
;
1817 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1818 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1819 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1820 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1822 /* try a 0 sized account buffer */
1824 dom_sizeA
= MAX_PATH
;
1826 LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1827 /* this can fail or succeed depending on OS version but the size will always be returned */
1828 ok(acc_sizeA
== real_acc_sizeA
+ 1,
1829 "LookupAccountSidA() Expected acc_size = %u, got %u\n",
1830 real_acc_sizeA
+ 1, acc_sizeA
);
1832 /* try a 0 sized account buffer */
1834 dom_sizeA
= MAX_PATH
;
1835 LookupAccountSidA(NULL
, pUsersSid
, NULL
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1836 /* this can fail or succeed depending on OS version but the size will always be returned */
1837 ok(acc_sizeA
== real_acc_sizeA
+ 1,
1838 "LookupAccountSid() Expected acc_size = %u, got %u\n",
1839 real_acc_sizeA
+ 1, acc_sizeA
);
1841 /* try a small domain buffer */
1843 acc_sizeA
= MAX_PATH
;
1845 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1846 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1847 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1848 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1850 /* try a 0 sized domain buffer */
1852 acc_sizeA
= MAX_PATH
;
1854 LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1855 /* this can fail or succeed depending on OS version but the size will always be returned */
1856 ok(dom_sizeA
== real_dom_sizeA
+ 1,
1857 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1858 real_dom_sizeA
+ 1, dom_sizeA
);
1860 /* try a 0 sized domain buffer */
1862 acc_sizeA
= MAX_PATH
;
1863 LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, NULL
, &dom_sizeA
, &use
);
1864 /* this can fail or succeed depending on OS version but the size will always be returned */
1865 ok(dom_sizeA
== real_dom_sizeA
+ 1,
1866 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1867 real_dom_sizeA
+ 1, dom_sizeA
);
1869 real_acc_sizeW
= MAX_PATH
;
1870 real_dom_sizeW
= MAX_PATH
;
1871 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &real_acc_sizeW
, domainW
, &real_dom_sizeW
, &use
);
1872 ok(ret
, "LookupAccountSidW() Expected TRUE, got FALSE\n");
1874 /* try an invalid system name */
1875 real_acc_sizeA
= MAX_PATH
;
1876 real_dom_sizeA
= MAX_PATH
;
1877 ret
= LookupAccountSidA("deepthought", pUsersSid
, accountA
, &real_acc_sizeA
, domainA
, &real_dom_sizeA
, &use
);
1878 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1879 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE
|| GetLastError() == RPC_S_INVALID_NET_ADDR
/* Vista */,
1880 "LookupAccountSidA() Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %u\n", GetLastError());
1882 /* native windows crashes if domainW or accountW is NULL */
1884 /* try a small account buffer */
1886 dom_sizeW
= MAX_PATH
;
1888 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1889 ok(!ret
, "LookupAccountSidW() Expected FALSE got TRUE\n");
1890 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1891 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1893 /* try a 0 sized account buffer */
1895 dom_sizeW
= MAX_PATH
;
1897 LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1898 /* this can fail or succeed depending on OS version but the size will always be returned */
1899 ok(acc_sizeW
== real_acc_sizeW
+ 1,
1900 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1901 real_acc_sizeW
+ 1, acc_sizeW
);
1903 /* try a 0 sized account buffer */
1905 dom_sizeW
= MAX_PATH
;
1906 LookupAccountSidW(NULL
, pUsersSid
, NULL
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1907 /* this can fail or succeed depending on OS version but the size will always be returned */
1908 ok(acc_sizeW
== real_acc_sizeW
+ 1,
1909 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1910 real_acc_sizeW
+ 1, acc_sizeW
);
1912 /* try a small domain buffer */
1914 acc_sizeW
= MAX_PATH
;
1916 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1917 ok(!ret
, "LookupAccountSidW() Expected FALSE got TRUE\n");
1918 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1919 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1921 /* try a 0 sized domain buffer */
1923 acc_sizeW
= MAX_PATH
;
1925 LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1926 /* this can fail or succeed depending on OS version but the size will always be returned */
1927 ok(dom_sizeW
== real_dom_sizeW
+ 1,
1928 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1929 real_dom_sizeW
+ 1, dom_sizeW
);
1931 /* try a 0 sized domain buffer */
1933 acc_sizeW
= MAX_PATH
;
1934 LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, NULL
, &dom_sizeW
, &use
);
1935 /* this can fail or succeed depending on OS version but the size will always be returned */
1936 ok(dom_sizeW
== real_dom_sizeW
+ 1,
1937 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1938 real_dom_sizeW
+ 1, dom_sizeW
);
1940 acc_sizeW
= dom_sizeW
= use
= 0;
1941 SetLastError(0xdeadbeef);
1942 ret
= LookupAccountSidW(NULL
, pUsersSid
, NULL
, &acc_sizeW
, NULL
, &dom_sizeW
, &use
);
1943 error
= GetLastError();
1944 ok(!ret
, "LookupAccountSidW failed %u\n", GetLastError());
1945 ok(error
== ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error
);
1946 ok(acc_sizeW
, "expected non-zero account size\n");
1947 ok(dom_sizeW
, "expected non-zero domain size\n");
1948 ok(!use
, "expected zero use %u\n", use
);
1952 /* Test LookupAccountSid with Sid retrieved from token information.
1953 This assumes this process is running under the account of the current user.*/
1954 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
|TOKEN_DUPLICATE
, &hToken
);
1955 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1956 ret
= GetTokenInformation(hToken
, TokenUser
, NULL
, 0, &cbti
);
1957 ok(!ret
, "GetTokenInformation failed with error %d\n", GetLastError());
1958 ptiUser
= HeapAlloc(GetProcessHeap(), 0, cbti
);
1959 if (GetTokenInformation(hToken
, TokenUser
, ptiUser
, cbti
, &cbti
))
1961 acc_sizeA
= dom_sizeA
= MAX_PATH
;
1962 ret
= LookupAccountSidA(NULL
, ptiUser
->User
.Sid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1963 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1964 user_sizeA
= MAX_PATH
;
1965 ret
= GetUserNameA(usernameA
, &user_sizeA
);
1966 ok(ret
, "GetUserNameA() Expected TRUE, got FALSE\n");
1967 ok(lstrcmpA(usernameA
, accountA
) == 0, "LookupAccountSidA() Expected account name: %s got: %s\n", usernameA
, accountA
);
1969 HeapFree(GetProcessHeap(), 0, ptiUser
);
1971 if (pCreateWellKnownSid
&& pConvertSidToStringSidA
)
1973 trace("Well Known SIDs:\n");
1974 for (i
= 0; i
<= 60; i
++)
1976 size
= SECURITY_MAX_SID_SIZE
;
1977 if (pCreateWellKnownSid(i
, NULL
, &max_sid
.sid
, &size
))
1979 if (pConvertSidToStringSidA(&max_sid
.sid
, &str_sidA
))
1981 acc_sizeA
= MAX_PATH
;
1982 dom_sizeA
= MAX_PATH
;
1983 if (LookupAccountSidA(NULL
, &max_sid
.sid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
))
1984 trace(" %d: %s %s\\%s %d\n", i
, str_sidA
, domainA
, accountA
, use
);
1985 LocalFree(str_sidA
);
1990 if (GetLastError() != ERROR_INVALID_PARAMETER
)
1991 trace(" CreateWellKnownSid(%d) failed: %d\n", i
, GetLastError());
1993 trace(" %d: not supported\n", i
);
1997 pLsaQueryInformationPolicy
= (void *)GetProcAddress( hmod
, "LsaQueryInformationPolicy");
1998 pLsaOpenPolicy
= (void *)GetProcAddress( hmod
, "LsaOpenPolicy");
1999 pLsaFreeMemory
= (void *)GetProcAddress( hmod
, "LsaFreeMemory");
2000 pLsaClose
= (void *)GetProcAddress( hmod
, "LsaClose");
2002 if (pLsaQueryInformationPolicy
&& pLsaOpenPolicy
&& pLsaFreeMemory
&& pLsaClose
)
2006 LSA_OBJECT_ATTRIBUTES object_attributes
;
2008 ZeroMemory(&object_attributes
, sizeof(object_attributes
));
2009 object_attributes
.Length
= sizeof(object_attributes
);
2011 status
= pLsaOpenPolicy( NULL
, &object_attributes
, POLICY_ALL_ACCESS
, &handle
);
2012 ok(status
== STATUS_SUCCESS
|| status
== STATUS_ACCESS_DENIED
,
2013 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status
);
2015 /* try a more restricted access mask if necessary */
2016 if (status
== STATUS_ACCESS_DENIED
) {
2017 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
2018 status
= pLsaOpenPolicy( NULL
, &object_attributes
, POLICY_VIEW_LOCAL_INFORMATION
, &handle
);
2019 ok(status
== STATUS_SUCCESS
, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status
);
2022 if (status
== STATUS_SUCCESS
)
2024 PPOLICY_ACCOUNT_DOMAIN_INFO info
;
2025 status
= pLsaQueryInformationPolicy(handle
, PolicyAccountDomainInformation
, (PVOID
*)&info
);
2026 ok(status
== STATUS_SUCCESS
, "LsaQueryInformationPolicy() failed, returned 0x%08x\n", status
);
2027 if (status
== STATUS_SUCCESS
)
2029 ok(info
->DomainSid
!=0, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) missing SID\n");
2030 if (info
->DomainSid
)
2032 int count
= *GetSidSubAuthorityCount(info
->DomainSid
);
2033 CopySid(GetSidLengthRequired(count
), &max_sid
, info
->DomainSid
);
2034 test_sid_str((PSID
)&max_sid
.sid
);
2035 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_USER_RID_ADMIN
;
2036 max_sid
.sid
.SubAuthorityCount
= count
+ 1;
2037 test_sid_str((PSID
)&max_sid
.sid
);
2038 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_USER_RID_GUEST
;
2039 test_sid_str((PSID
)&max_sid
.sid
);
2040 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_ADMINS
;
2041 test_sid_str((PSID
)&max_sid
.sid
);
2042 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_USERS
;
2043 test_sid_str((PSID
)&max_sid
.sid
);
2044 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_GUESTS
;
2045 test_sid_str((PSID
)&max_sid
.sid
);
2046 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_COMPUTERS
;
2047 test_sid_str((PSID
)&max_sid
.sid
);
2048 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_CONTROLLERS
;
2049 test_sid_str((PSID
)&max_sid
.sid
);
2050 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_CERT_ADMINS
;
2051 test_sid_str((PSID
)&max_sid
.sid
);
2052 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_SCHEMA_ADMINS
;
2053 test_sid_str((PSID
)&max_sid
.sid
);
2054 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_ENTERPRISE_ADMINS
;
2055 test_sid_str((PSID
)&max_sid
.sid
);
2056 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_POLICY_ADMINS
;
2057 test_sid_str((PSID
)&max_sid
.sid
);
2058 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_ALIAS_RID_RAS_SERVERS
;
2059 test_sid_str((PSID
)&max_sid
.sid
);
2060 max_sid
.sid
.SubAuthority
[count
] = 1000; /* first user account */
2061 test_sid_str((PSID
)&max_sid
.sid
);
2064 pLsaFreeMemory((LPVOID
)info
);
2067 status
= pLsaClose(handle
);
2068 ok(status
== STATUS_SUCCESS
, "LsaClose() failed, returned 0x%08x\n", status
);
2074 static BOOL
get_sid_info(PSID psid
, LPSTR
*user
, LPSTR
*dom
)
2076 static CHAR account
[UNLEN
+ 1];
2077 static CHAR domain
[UNLEN
+ 1];
2078 DWORD size
, dom_size
;
2084 size
= dom_size
= UNLEN
+ 1;
2087 SetLastError(0xdeadbeef);
2088 return LookupAccountSidA(NULL
, psid
, account
, &size
, domain
, &dom_size
, &use
);
2091 static void check_wellknown_name(const char* name
, WELL_KNOWN_SID_TYPE result
)
2093 SID_IDENTIFIER_AUTHORITY ident
= { SECURITY_NT_AUTHORITY
};
2094 PSID domainsid
= NULL
;
2095 char wk_sid
[SECURITY_MAX_SID_SIZE
];
2098 DWORD sid_size
, domain_size
;
2099 SID_NAME_USE sid_use
;
2100 LPSTR domain
, account
, sid_domain
, wk_domain
, wk_account
;
2106 ret
= LookupAccountNameA(NULL
, name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2107 ok(!ret
, " %s Should have failed to lookup account name\n", name
);
2108 psid
= HeapAlloc(GetProcessHeap(),0,sid_size
);
2109 domain
= HeapAlloc(GetProcessHeap(),0,domain_size
);
2110 ret
= LookupAccountNameA(NULL
, name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2114 ok(!ret
, " %s Should have failed to lookup account name\n",name
);
2118 AllocateAndInitializeSid(&ident
, 6, SECURITY_NT_NON_UNIQUE
, 12, 23, 34, 45, 56, 0, 0, &domainsid
);
2119 cb
= sizeof(wk_sid
);
2120 if (!pCreateWellKnownSid(result
, domainsid
, wk_sid
, &cb
))
2122 win_skip("SID %i is not available on the system\n",result
);
2126 ret2
= get_sid_info(wk_sid
, &wk_account
, &wk_domain
);
2127 if (!ret2
&& GetLastError() == ERROR_NONE_MAPPED
)
2129 win_skip("CreateWellKnownSid() succeeded but the account '%s' is not present (W2K)\n", name
);
2133 get_sid_info(psid
, &account
, &sid_domain
);
2135 ok(ret
, "Failed to lookup account name %s\n",name
);
2136 ok(sid_size
!= 0, "sid_size was zero\n");
2138 ok(EqualSid(psid
,wk_sid
),"(%s) Sids fail to match well known sid!\n",name
);
2140 ok(!lstrcmpA(account
, wk_account
), "Expected %s , got %s\n", account
, wk_account
);
2141 ok(!lstrcmpA(domain
, wk_domain
), "Expected %s, got %s\n", wk_domain
, domain
);
2142 ok(sid_use
== SidTypeWellKnownGroup
, "Expected Use (5), got %d\n", sid_use
);
2146 HeapFree(GetProcessHeap(),0,psid
);
2147 HeapFree(GetProcessHeap(),0,domain
);
2150 static void test_LookupAccountName(void)
2152 DWORD sid_size
, domain_size
, user_size
;
2153 DWORD sid_save
, domain_save
;
2154 CHAR user_name
[UNLEN
+ 1];
2155 CHAR computer_name
[UNLEN
+ 1];
2156 SID_NAME_USE sid_use
;
2157 LPSTR domain
, account
, sid_dom
;
2161 /* native crashes if (assuming all other parameters correct):
2163 * - Sid is NULL and cbSid is > 0
2164 * - cbSid or cchReferencedDomainName are NULL
2165 * - ReferencedDomainName is NULL and cchReferencedDomainName is the correct size
2168 user_size
= UNLEN
+ 1;
2169 SetLastError(0xdeadbeef);
2170 ret
= GetUserNameA(user_name
, &user_size
);
2171 ok(ret
, "Failed to get user name : %d\n", GetLastError());
2176 sid_use
= 0xcafebabe;
2177 SetLastError(0xdeadbeef);
2178 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2179 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
2181 win_skip("LookupAccountNameA is not implemented\n");
2184 ok(!ret
, "Expected 0, got %d\n", ret
);
2185 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2186 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2187 ok(sid_size
!= 0, "Expected non-zero sid size\n");
2188 ok(domain_size
!= 0, "Expected non-zero domain size\n");
2189 ok(sid_use
== 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use
);
2191 sid_save
= sid_size
;
2192 domain_save
= domain_size
;
2194 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
2195 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
2197 /* try valid account name */
2198 ret
= LookupAccountNameA(NULL
, user_name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2199 get_sid_info(psid
, &account
, &sid_dom
);
2200 ok(ret
, "Failed to lookup account name\n");
2201 ok(sid_size
== GetLengthSid(psid
), "Expected %d, got %d\n", GetLengthSid(psid
), sid_size
);
2202 ok(!lstrcmpA(account
, user_name
), "Expected %s, got %s\n", user_name
, account
);
2203 ok(!lstrcmpA(domain
, sid_dom
), "Expected %s, got %s\n", sid_dom
, domain
);
2204 ok(domain_size
== domain_save
- 1, "Expected %d, got %d\n", domain_save
- 1, domain_size
);
2205 ok(strlen(domain
) == domain_size
, "Expected %d, got %d\n", lstrlenA(domain
), domain_size
);
2206 ok(sid_use
== SidTypeUser
, "Expected SidTypeUser (%d), got %d\n", SidTypeUser
, sid_use
);
2207 domain_size
= domain_save
;
2208 sid_size
= sid_save
;
2210 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2212 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2216 ret
= LookupAccountNameA(NULL
, "Everyone", psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2217 get_sid_info(psid
, &account
, &sid_dom
);
2218 ok(ret
, "Failed to lookup account name\n");
2219 ok(sid_size
!= 0, "sid_size was zero\n");
2220 ok(!lstrcmpA(account
, "Everyone"), "Expected Everyone, got %s\n", account
);
2221 ok(!lstrcmpA(domain
, sid_dom
), "Expected %s, got %s\n", sid_dom
, domain
);
2222 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2223 ok(strlen(domain
) == domain_size
, "Expected %d, got %d\n", lstrlenA(domain
), domain_size
);
2224 ok(sid_use
== SidTypeWellKnownGroup
, "Expected SidTypeWellKnownGroup (%d), got %d\n", SidTypeWellKnownGroup
, sid_use
);
2225 domain_size
= domain_save
;
2228 /* NULL Sid with zero sid size */
2229 SetLastError(0xdeadbeef);
2231 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, domain
, &domain_size
, &sid_use
);
2232 ok(!ret
, "Expected 0, got %d\n", ret
);
2233 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2234 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2235 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
2236 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
2238 /* try cchReferencedDomainName - 1 */
2239 SetLastError(0xdeadbeef);
2241 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, domain
, &domain_size
, &sid_use
);
2242 ok(!ret
, "Expected 0, got %d\n", ret
);
2243 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2244 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2245 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
2246 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
2248 /* NULL ReferencedDomainName with zero domain name size */
2249 SetLastError(0xdeadbeef);
2251 ret
= LookupAccountNameA(NULL
, user_name
, psid
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2252 ok(!ret
, "Expected 0, got %d\n", ret
);
2253 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2254 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2255 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
2256 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
2258 HeapFree(GetProcessHeap(), 0, psid
);
2259 HeapFree(GetProcessHeap(), 0, domain
);
2261 /* get sizes for NULL account name */
2264 sid_use
= 0xcafebabe;
2265 SetLastError(0xdeadbeef);
2266 ret
= LookupAccountNameA(NULL
, NULL
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2267 if (!ret
&& GetLastError() == ERROR_NONE_MAPPED
)
2268 win_skip("NULL account name doesn't work on NT4\n");
2271 ok(!ret
, "Expected 0, got %d\n", ret
);
2272 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2273 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2274 ok(sid_size
!= 0, "Expected non-zero sid size\n");
2275 ok(domain_size
!= 0, "Expected non-zero domain size\n");
2276 ok(sid_use
== 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use
);
2278 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
2279 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
2281 /* try NULL account name */
2282 ret
= LookupAccountNameA(NULL
, NULL
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2283 get_sid_info(psid
, &account
, &sid_dom
);
2284 ok(ret
, "Failed to lookup account name\n");
2285 /* Using a fixed string will not work on different locales */
2286 ok(!lstrcmpA(account
, domain
),
2287 "Got %s for account and %s for domain, these should be the same\n",
2289 ok(sid_use
== SidTypeDomain
, "Expected SidTypeDomain (%d), got %d\n", SidTypeDomain
, sid_use
);
2291 HeapFree(GetProcessHeap(), 0, psid
);
2292 HeapFree(GetProcessHeap(), 0, domain
);
2295 /* try an invalid account name */
2296 SetLastError(0xdeadbeef);
2299 ret
= LookupAccountNameA(NULL
, "oogabooga", NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2300 ok(!ret
, "Expected 0, got %d\n", ret
);
2301 ok(GetLastError() == ERROR_NONE_MAPPED
||
2302 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE
),
2303 "Expected ERROR_NONE_MAPPED, got %d\n", GetLastError());
2304 ok(sid_size
== 0, "Expected 0, got %d\n", sid_size
);
2305 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2307 /* try an invalid system name */
2308 SetLastError(0xdeadbeef);
2311 ret
= LookupAccountNameA("deepthought", NULL
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2312 ok(!ret
, "Expected 0, got %d\n", ret
);
2313 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE
|| GetLastError() == RPC_S_INVALID_NET_ADDR
/* Vista */,
2314 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
2315 ok(sid_size
== 0, "Expected 0, got %d\n", sid_size
);
2316 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2318 /* try with the computer name as the account name */
2319 domain_size
= sizeof(computer_name
);
2320 GetComputerNameA(computer_name
, &domain_size
);
2323 ret
= LookupAccountNameA(NULL
, computer_name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2324 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
||
2325 GetLastError() == ERROR_NONE_MAPPED
/* in a domain */ ||
2326 broken(GetLastError() == ERROR_TRUSTED_DOMAIN_FAILURE
) ||
2327 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE
)),
2328 "LookupAccountNameA failed: %d\n", GetLastError());
2329 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
2331 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
2332 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
2333 ret
= LookupAccountNameA(NULL
, computer_name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2334 ok(ret
, "LookupAccountNameA failed: %d\n", GetLastError());
2335 ok(sid_use
== SidTypeDomain
||
2336 (sid_use
== SidTypeUser
&& ! strcmp(computer_name
, user_name
)), "expected SidTypeDomain for %s, got %d\n", computer_name
, sid_use
);
2337 HeapFree(GetProcessHeap(), 0, domain
);
2338 HeapFree(GetProcessHeap(), 0, psid
);
2341 /* Well Known names */
2342 if (!pCreateWellKnownSid
)
2344 win_skip("CreateWellKnownSid not available\n");
2348 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2350 skip("Non-English locale (skipping well known name creation tests)\n");
2354 check_wellknown_name("LocalService", WinLocalServiceSid
);
2355 check_wellknown_name("Local Service", WinLocalServiceSid
);
2357 check_wellknown_name("Local Service", 0);
2358 check_wellknown_name("NetworkService", WinNetworkServiceSid
);
2359 check_wellknown_name("Network Service", WinNetworkServiceSid
);
2361 /* example of some names where the spaces are not optional */
2362 check_wellknown_name("Terminal Server User", WinTerminalServerSid
);
2363 check_wellknown_name("TerminalServer User", 0);
2364 check_wellknown_name("TerminalServerUser", 0);
2365 check_wellknown_name("Terminal ServerUser", 0);
2367 check_wellknown_name("enterprise domain controllers",WinEnterpriseControllersSid
);
2368 check_wellknown_name("enterprisedomain controllers", 0);
2369 check_wellknown_name("enterprise domaincontrollers", 0);
2370 check_wellknown_name("enterprisedomaincontrollers", 0);
2372 /* case insensitivity */
2373 check_wellknown_name("lOCAlServICE", WinLocalServiceSid
);
2375 /* fully qualified account names */
2376 check_wellknown_name("NT AUTHORITY\\LocalService", WinLocalServiceSid
);
2377 check_wellknown_name("nt authority\\Network Service", WinNetworkServiceSid
);
2378 check_wellknown_name("nt authority test\\Network Service", 0);
2379 check_wellknown_name("Dummy\\Network Service", 0);
2380 check_wellknown_name("ntauthority\\Network Service", 0);
2383 static void test_security_descriptor(void)
2385 SECURITY_DESCRIPTOR sd
;
2388 BOOL isDefault
, isPresent
, ret
;
2392 SetLastError(0xdeadbeef);
2393 ret
= InitializeSecurityDescriptor(&sd
, SECURITY_DESCRIPTOR_REVISION
);
2394 if (ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2396 win_skip("InitializeSecurityDescriptor is not implemented\n");
2400 ok(GetSecurityDescriptorOwner(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorOwner failed\n");
2401 expect_eq(psid
, NULL
, PSID
, "%p");
2402 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2403 sd
.Control
|= SE_DACL_PRESENT
| SE_SACL_PRESENT
;
2405 SetLastError(0xdeadbeef);
2407 expect_eq(MakeSelfRelativeSD(&sd
, buf
, &size
), FALSE
, BOOL
, "%d");
2408 expect_eq(GetLastError(), ERROR_INSUFFICIENT_BUFFER
, DWORD
, "%u");
2409 ok(size
> 5, "Size not increased\n");
2412 expect_eq(MakeSelfRelativeSD(&sd
, buf
, &size
), TRUE
, BOOL
, "%d");
2413 ok(GetSecurityDescriptorOwner(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorOwner failed\n");
2414 expect_eq(psid
, NULL
, PSID
, "%p");
2415 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2416 ok(GetSecurityDescriptorGroup(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorGroup failed\n");
2417 expect_eq(psid
, NULL
, PSID
, "%p");
2418 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2419 ok(GetSecurityDescriptorDacl(&sd
, &isPresent
, &pacl
, &isDefault
), "GetSecurityDescriptorDacl failed\n");
2420 expect_eq(isPresent
, TRUE
, BOOL
, "%d");
2421 expect_eq(psid
, NULL
, PSID
, "%p");
2422 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2423 ok(GetSecurityDescriptorSacl(&sd
, &isPresent
, &pacl
, &isDefault
), "GetSecurityDescriptorSacl failed\n");
2424 expect_eq(isPresent
, TRUE
, BOOL
, "%d");
2425 expect_eq(psid
, NULL
, PSID
, "%p");
2426 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2430 #define TEST_GRANTED_ACCESS(a,b) test_granted_access(a,b,0,__LINE__)
2431 #define TEST_GRANTED_ACCESS2(a,b,c) test_granted_access(a,b,c,__LINE__)
2432 static void test_granted_access(HANDLE handle
, ACCESS_MASK access
,
2433 ACCESS_MASK alt
, int line
)
2435 OBJECT_BASIC_INFORMATION obj_info
;
2438 if (!pNtQueryObject
)
2440 skip_(__FILE__
, line
)("Not NT platform - skipping tests\n");
2444 status
= pNtQueryObject( handle
, ObjectBasicInformation
, &obj_info
,
2445 sizeof(obj_info
), NULL
);
2446 ok_(__FILE__
, line
)(!status
, "NtQueryObject with err: %08x\n", status
);
2448 ok_(__FILE__
, line
)(obj_info
.GrantedAccess
== access
||
2449 obj_info
.GrantedAccess
== alt
, "Granted access should be 0x%08x "
2450 "or 0x%08x, instead of 0x%08x\n", access
, alt
, obj_info
.GrantedAccess
);
2452 ok_(__FILE__
, line
)(obj_info
.GrantedAccess
== access
, "Granted access should "
2453 "be 0x%08x, instead of 0x%08x\n", access
, obj_info
.GrantedAccess
);
2456 #define CHECK_SET_SECURITY(o,i,e) \
2460 SetLastError( 0xdeadbeef ); \
2461 res_ = SetKernelObjectSecurity( o, i, SecurityDescriptor ); \
2462 err = GetLastError(); \
2463 if (e == ERROR_SUCCESS) \
2464 ok(res_, "SetKernelObjectSecurity failed with %d\n", err); \
2466 ok(!res_ && err == e, "SetKernelObjectSecurity should have failed " \
2467 "with %s, instead of %d\n", #e, err); \
2470 static void test_process_security(void)
2474 PTOKEN_PRIMARY_GROUP group
;
2475 PSID AdminSid
= NULL
, UsersSid
= NULL
;
2477 SECURITY_DESCRIPTOR
*SecurityDescriptor
= NULL
;
2478 char buffer
[MAX_PATH
];
2479 PROCESS_INFORMATION info
;
2480 STARTUPINFOA startup
;
2481 SECURITY_ATTRIBUTES psa
;
2482 HANDLE token
, event
;
2485 Acl
= HeapAlloc(GetProcessHeap(), 0, 256);
2486 res
= InitializeAcl(Acl
, 256, ACL_REVISION
);
2487 if (!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2489 win_skip("ACLs not implemented - skipping tests\n");
2490 HeapFree(GetProcessHeap(), 0, Acl
);
2493 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
2495 /* get owner from the token we might be running as a user not admin */
2496 res
= OpenProcessToken( GetCurrentProcess(), MAXIMUM_ALLOWED
, &token
);
2497 ok(res
, "OpenProcessToken failed with error %d\n", GetLastError());
2500 HeapFree(GetProcessHeap(), 0, Acl
);
2504 res
= GetTokenInformation( token
, TokenOwner
, NULL
, 0, &size
);
2505 ok(!res
, "Expected failure, got %d\n", res
);
2506 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2507 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2509 owner
= HeapAlloc(GetProcessHeap(), 0, size
);
2510 res
= GetTokenInformation( token
, TokenOwner
, owner
, size
, &size
);
2511 ok(res
, "GetTokenInformation failed with error %d\n", GetLastError());
2512 AdminSid
= ((TOKEN_OWNER
*)owner
)->Owner
;
2514 res
= GetTokenInformation( token
, TokenPrimaryGroup
, NULL
, 0, &size
);
2515 ok(!res
, "Expected failure, got %d\n", res
);
2516 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2517 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2519 group
= HeapAlloc(GetProcessHeap(), 0, size
);
2520 res
= GetTokenInformation( token
, TokenPrimaryGroup
, group
, size
, &size
);
2521 ok(res
, "GetTokenInformation failed with error %d\n", GetLastError());
2522 UsersSid
= ((TOKEN_PRIMARY_GROUP
*)group
)->PrimaryGroup
;
2524 CloseHandle( token
);
2527 HeapFree(GetProcessHeap(), 0, group
);
2528 HeapFree(GetProcessHeap(), 0, owner
);
2529 HeapFree(GetProcessHeap(), 0, Acl
);
2533 res
= AddAccessDeniedAce(Acl
, ACL_REVISION
, PROCESS_VM_READ
, AdminSid
);
2534 ok(res
, "AddAccessDeniedAce failed with error %d\n", GetLastError());
2535 res
= AddAccessAllowedAce(Acl
, ACL_REVISION
, PROCESS_ALL_ACCESS
, AdminSid
);
2536 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2538 SecurityDescriptor
= HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
2539 res
= InitializeSecurityDescriptor(SecurityDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
2540 ok(res
, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
2542 event
= CreateEventA( NULL
, TRUE
, TRUE
, "test_event" );
2543 ok(event
!= NULL
, "CreateEvent %d\n", GetLastError());
2545 SecurityDescriptor
->Revision
= 0;
2546 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_UNKNOWN_REVISION
);
2547 SecurityDescriptor
->Revision
= SECURITY_DESCRIPTOR_REVISION
;
2549 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_INVALID_SECURITY_DESCR
);
2550 CHECK_SET_SECURITY( event
, GROUP_SECURITY_INFORMATION
, ERROR_INVALID_SECURITY_DESCR
);
2551 CHECK_SET_SECURITY( event
, SACL_SECURITY_INFORMATION
, ERROR_ACCESS_DENIED
);
2552 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2553 /* NULL DACL is valid and means default DACL from token */
2554 SecurityDescriptor
->Control
|= SE_DACL_PRESENT
;
2555 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2557 /* Set owner and group and dacl */
2558 res
= SetSecurityDescriptorOwner(SecurityDescriptor
, AdminSid
, FALSE
);
2559 ok(res
, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
2560 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2561 res
= SetSecurityDescriptorGroup(SecurityDescriptor
, UsersSid
, FALSE
);
2562 ok(res
, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2563 CHECK_SET_SECURITY( event
, GROUP_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2564 res
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
2565 ok(res
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
2566 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2568 sprintf(buffer
, "%s tests/security.c test", myARGV
[0]);
2569 memset(&startup
, 0, sizeof(startup
));
2570 startup
.cb
= sizeof(startup
);
2571 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
2572 startup
.wShowWindow
= SW_SHOWNORMAL
;
2574 psa
.nLength
= sizeof(psa
);
2575 psa
.lpSecurityDescriptor
= SecurityDescriptor
;
2576 psa
.bInheritHandle
= TRUE
;
2578 /* Doesn't matter what ACL say we should get full access for ourselves */
2579 res
= CreateProcessA( NULL
, buffer
, &psa
, NULL
, FALSE
, 0, NULL
, NULL
, &startup
, &info
);
2580 ok(res
, "CreateProcess with err:%d\n", GetLastError());
2581 TEST_GRANTED_ACCESS2( info
.hProcess
, PROCESS_ALL_ACCESS_NT4
,
2582 STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
);
2583 winetest_wait_child_process( info
.hProcess
);
2585 CloseHandle( info
.hProcess
);
2586 CloseHandle( info
.hThread
);
2587 CloseHandle( event
);
2588 HeapFree(GetProcessHeap(), 0, group
);
2589 HeapFree(GetProcessHeap(), 0, owner
);
2590 HeapFree(GetProcessHeap(), 0, Acl
);
2591 HeapFree(GetProcessHeap(), 0, SecurityDescriptor
);
2594 static void test_process_security_child(void)
2596 HANDLE handle
, handle1
;
2600 handle
= OpenProcess( PROCESS_TERMINATE
, FALSE
, GetCurrentProcessId() );
2601 ok(handle
!= NULL
, "OpenProcess(PROCESS_TERMINATE) with err:%d\n", GetLastError());
2602 TEST_GRANTED_ACCESS( handle
, PROCESS_TERMINATE
);
2604 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2605 &handle1
, 0, TRUE
, DUPLICATE_SAME_ACCESS
);
2606 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2607 TEST_GRANTED_ACCESS( handle1
, PROCESS_TERMINATE
);
2609 CloseHandle( handle1
);
2611 SetLastError( 0xdeadbeef );
2612 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2613 &handle1
, PROCESS_ALL_ACCESS
, TRUE
, 0 );
2614 err
= GetLastError();
2616 ok(!ret
&& err
== ERROR_ACCESS_DENIED
, "duplicating handle should have failed "
2617 "with STATUS_ACCESS_DENIED, instead of err:%d\n", err
);
2619 CloseHandle( handle
);
2621 /* These two should fail - they are denied by ACL */
2622 handle
= OpenProcess( PROCESS_VM_READ
, FALSE
, GetCurrentProcessId() );
2624 ok(handle
== NULL
, "OpenProcess(PROCESS_VM_READ) should have failed\n");
2625 handle
= OpenProcess( PROCESS_ALL_ACCESS
, FALSE
, GetCurrentProcessId() );
2627 ok(handle
== NULL
, "OpenProcess(PROCESS_ALL_ACCESS) should have failed\n");
2629 /* Documented privilege elevation */
2630 ret
= DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2631 &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
);
2632 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2633 TEST_GRANTED_ACCESS2( handle
, PROCESS_ALL_ACCESS_NT4
,
2634 STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
);
2636 CloseHandle( handle
);
2638 /* Same only explicitly asking for all access rights */
2639 ret
= DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2640 &handle
, PROCESS_ALL_ACCESS
, TRUE
, 0 );
2641 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2642 TEST_GRANTED_ACCESS2( handle
, PROCESS_ALL_ACCESS_NT4
,
2643 PROCESS_ALL_ACCESS
| PROCESS_QUERY_LIMITED_INFORMATION
);
2644 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2645 &handle1
, PROCESS_VM_READ
, TRUE
, 0 );
2646 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2647 TEST_GRANTED_ACCESS( handle1
, PROCESS_VM_READ
);
2648 CloseHandle( handle1
);
2649 CloseHandle( handle
);
2652 static void test_impersonation_level(void)
2654 HANDLE Token
, ProcessToken
;
2657 TOKEN_PRIVILEGES
*Privileges
;
2659 PRIVILEGE_SET
*PrivilegeSet
;
2665 pDuplicateTokenEx
= (void *)GetProcAddress(hmod
, "DuplicateTokenEx");
2666 if( !pDuplicateTokenEx
) {
2667 win_skip("DuplicateTokenEx is not available\n");
2670 SetLastError(0xdeadbeef);
2671 ret
= ImpersonateSelf(SecurityAnonymous
);
2672 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
2674 win_skip("ImpersonateSelf is not implemented\n");
2677 ok(ret
, "ImpersonateSelf(SecurityAnonymous) failed with error %d\n", GetLastError());
2678 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2679 ok(!ret
, "OpenThreadToken should have failed\n");
2680 error
= GetLastError();
2681 ok(error
== ERROR_CANT_OPEN_ANONYMOUS
, "OpenThreadToken on anonymous token should have returned ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error
);
2682 /* can't perform access check when opening object against an anonymous impersonation token */
2684 error
= RegOpenKeyExA(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2685 ok(error
== ERROR_INVALID_HANDLE
|| error
== ERROR_CANT_OPEN_ANONYMOUS
|| error
== ERROR_BAD_IMPERSONATION_LEVEL
,
2686 "RegOpenKeyEx failed with %d\n", error
);
2690 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
, &ProcessToken
);
2691 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
2693 ret
= pDuplicateTokenEx(ProcessToken
,
2694 TOKEN_QUERY
| TOKEN_DUPLICATE
| TOKEN_IMPERSONATE
, NULL
,
2695 SecurityAnonymous
, TokenImpersonation
, &Token
);
2696 ok(ret
, "DuplicateTokenEx failed with error %d\n", GetLastError());
2697 /* can't increase the impersonation level */
2698 ret
= DuplicateToken(Token
, SecurityIdentification
, &Token2
);
2699 error
= GetLastError();
2700 ok(!ret
&& error
== ERROR_BAD_IMPERSONATION_LEVEL
,
2701 "Duplicating a token and increasing the impersonation level should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2702 /* we can query anything from an anonymous token, including the user */
2703 ret
= GetTokenInformation(Token
, TokenUser
, NULL
, 0, &Size
);
2704 error
= GetLastError();
2705 ok(!ret
&& error
== ERROR_INSUFFICIENT_BUFFER
, "GetTokenInformation(TokenUser) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error
);
2706 User
= HeapAlloc(GetProcessHeap(), 0, Size
);
2707 ret
= GetTokenInformation(Token
, TokenUser
, User
, Size
, &Size
);
2708 ok(ret
, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
2709 HeapFree(GetProcessHeap(), 0, User
);
2711 /* PrivilegeCheck fails with SecurityAnonymous level */
2712 ret
= GetTokenInformation(Token
, TokenPrivileges
, NULL
, 0, &Size
);
2713 error
= GetLastError();
2714 ok(!ret
&& error
== ERROR_INSUFFICIENT_BUFFER
, "GetTokenInformation(TokenPrivileges) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error
);
2715 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
2716 ret
= GetTokenInformation(Token
, TokenPrivileges
, Privileges
, Size
, &Size
);
2717 ok(ret
, "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
2719 PrivilegeSet
= HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PRIVILEGE_SET
, Privilege
[Privileges
->PrivilegeCount
]));
2720 PrivilegeSet
->PrivilegeCount
= Privileges
->PrivilegeCount
;
2721 memcpy(PrivilegeSet
->Privilege
, Privileges
->Privileges
, PrivilegeSet
->PrivilegeCount
* sizeof(PrivilegeSet
->Privilege
[0]));
2722 PrivilegeSet
->Control
= PRIVILEGE_SET_ALL_NECESSARY
;
2723 HeapFree(GetProcessHeap(), 0, Privileges
);
2725 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2726 error
= GetLastError();
2727 ok(!ret
&& error
== ERROR_BAD_IMPERSONATION_LEVEL
, "PrivilegeCheck for SecurityAnonymous token should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2731 ret
= ImpersonateSelf(SecurityIdentification
);
2732 ok(ret
, "ImpersonateSelf(SecurityIdentification) failed with error %d\n", GetLastError());
2733 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2734 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
2736 /* can't perform access check when opening object against an identification impersonation token */
2737 error
= RegOpenKeyExA(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2739 ok(error
== ERROR_INVALID_HANDLE
|| error
== ERROR_BAD_IMPERSONATION_LEVEL
,
2740 "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE or ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2742 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2743 ok(ret
, "PrivilegeCheck for SecurityIdentification failed with error %d\n", GetLastError());
2747 ret
= ImpersonateSelf(SecurityImpersonation
);
2748 ok(ret
, "ImpersonateSelf(SecurityImpersonation) failed with error %d\n", GetLastError());
2749 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2750 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
2751 error
= RegOpenKeyExA(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2752 ok(error
== ERROR_SUCCESS
, "RegOpenKeyEx should have succeeded instead of failing with %d\n", error
);
2754 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2755 ok(ret
, "PrivilegeCheck for SecurityImpersonation failed with error %d\n", GetLastError());
2759 CloseHandle(ProcessToken
);
2761 HeapFree(GetProcessHeap(), 0, PrivilegeSet
);
2764 static void test_SetEntriesInAclW(void)
2767 PSID EveryoneSid
= NULL
, UsersSid
= NULL
;
2768 PACL OldAcl
= NULL
, NewAcl
;
2769 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
2770 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
2771 EXPLICIT_ACCESSW ExplicitAccess
;
2772 static const WCHAR wszEveryone
[] = {'E','v','e','r','y','o','n','e',0};
2773 static const WCHAR wszCurrentUser
[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
2775 if (!pSetEntriesInAclW
)
2777 win_skip("SetEntriesInAclW is not available\n");
2781 NewAcl
= (PACL
)0xdeadbeef;
2782 res
= pSetEntriesInAclW(0, NULL
, NULL
, &NewAcl
);
2783 if(res
== ERROR_CALL_NOT_IMPLEMENTED
)
2785 win_skip("SetEntriesInAclW is not implemented\n");
2788 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2789 ok(NewAcl
== NULL
||
2790 broken(NewAcl
!= NULL
), /* NT4 */
2791 "NewAcl=%p, expected NULL\n", NewAcl
);
2794 OldAcl
= HeapAlloc(GetProcessHeap(), 0, 256);
2795 res
= InitializeAcl(OldAcl
, 256, ACL_REVISION
);
2796 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2798 win_skip("ACLs not implemented - skipping tests\n");
2799 HeapFree(GetProcessHeap(), 0, OldAcl
);
2802 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
2804 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
2805 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2807 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
2808 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
2809 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2811 res
= AddAccessAllowedAce(OldAcl
, ACL_REVISION
, KEY_READ
, UsersSid
);
2812 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2814 ExplicitAccess
.grfAccessPermissions
= KEY_WRITE
;
2815 ExplicitAccess
.grfAccessMode
= GRANT_ACCESS
;
2816 ExplicitAccess
.grfInheritance
= NO_INHERITANCE
;
2817 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_WELL_KNOWN_GROUP
;
2818 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2819 ExplicitAccess
.Trustee
.ptstrName
= EveryoneSid
;
2820 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= 0xDEADBEEF;
2821 ExplicitAccess
.Trustee
.pMultipleTrustee
= (PVOID
)0xDEADBEEF;
2822 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2823 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2824 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2827 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2828 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
2829 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2830 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2831 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2832 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2835 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2837 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2841 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2842 ExplicitAccess
.Trustee
.ptstrName
= (LPWSTR
)wszEveryone
;
2843 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2844 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2845 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2848 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_BAD_FORM
;
2849 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2850 ok(res
== ERROR_INVALID_PARAMETER
||
2851 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2852 "SetEntriesInAclW failed: %u\n", res
);
2853 ok(NewAcl
== NULL
||
2854 broken(NewAcl
!= NULL
), /* NT4 */
2855 "returned acl wasn't NULL: %p\n", NewAcl
);
2857 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2858 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= TRUSTEE_IS_IMPERSONATE
;
2859 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2860 ok(res
== ERROR_INVALID_PARAMETER
||
2861 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2862 "SetEntriesInAclW failed: %u\n", res
);
2863 ok(NewAcl
== NULL
||
2864 broken(NewAcl
!= NULL
), /* NT4 */
2865 "returned acl wasn't NULL: %p\n", NewAcl
);
2867 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2868 ExplicitAccess
.grfAccessMode
= SET_ACCESS
;
2869 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2870 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2871 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2875 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2876 ExplicitAccess
.Trustee
.ptstrName
= (LPWSTR
)wszCurrentUser
;
2877 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2878 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2879 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2882 ExplicitAccess
.grfAccessMode
= REVOKE_ACCESS
;
2883 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2884 ExplicitAccess
.Trustee
.ptstrName
= UsersSid
;
2885 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2886 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2887 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2891 FreeSid(EveryoneSid
);
2892 HeapFree(GetProcessHeap(), 0, OldAcl
);
2895 static void test_SetEntriesInAclA(void)
2898 PSID EveryoneSid
= NULL
, UsersSid
= NULL
;
2899 PACL OldAcl
= NULL
, NewAcl
;
2900 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
2901 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
2902 EXPLICIT_ACCESSA ExplicitAccess
;
2903 static const CHAR szEveryone
[] = {'E','v','e','r','y','o','n','e',0};
2904 static const CHAR szCurrentUser
[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
2906 if (!pSetEntriesInAclA
)
2908 win_skip("SetEntriesInAclA is not available\n");
2912 NewAcl
= (PACL
)0xdeadbeef;
2913 res
= pSetEntriesInAclA(0, NULL
, NULL
, &NewAcl
);
2914 if(res
== ERROR_CALL_NOT_IMPLEMENTED
)
2916 win_skip("SetEntriesInAclA is not implemented\n");
2919 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2920 ok(NewAcl
== NULL
||
2921 broken(NewAcl
!= NULL
), /* NT4 */
2922 "NewAcl=%p, expected NULL\n", NewAcl
);
2925 OldAcl
= HeapAlloc(GetProcessHeap(), 0, 256);
2926 res
= InitializeAcl(OldAcl
, 256, ACL_REVISION
);
2927 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2929 win_skip("ACLs not implemented - skipping tests\n");
2930 HeapFree(GetProcessHeap(), 0, OldAcl
);
2933 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
2935 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
2936 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2938 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
2939 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
2940 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2942 res
= AddAccessAllowedAce(OldAcl
, ACL_REVISION
, KEY_READ
, UsersSid
);
2943 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2945 ExplicitAccess
.grfAccessPermissions
= KEY_WRITE
;
2946 ExplicitAccess
.grfAccessMode
= GRANT_ACCESS
;
2947 ExplicitAccess
.grfInheritance
= NO_INHERITANCE
;
2948 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_WELL_KNOWN_GROUP
;
2949 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2950 ExplicitAccess
.Trustee
.ptstrName
= EveryoneSid
;
2951 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2952 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
2953 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2954 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2955 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2958 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2959 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
2960 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2961 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2962 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2963 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2966 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2968 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2972 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2973 ExplicitAccess
.Trustee
.ptstrName
= (LPSTR
)szEveryone
;
2974 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2975 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2976 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2979 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_BAD_FORM
;
2980 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2981 ok(res
== ERROR_INVALID_PARAMETER
||
2982 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2983 "SetEntriesInAclA failed: %u\n", res
);
2984 ok(NewAcl
== NULL
||
2985 broken(NewAcl
!= NULL
), /* NT4 */
2986 "returned acl wasn't NULL: %p\n", NewAcl
);
2988 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2989 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= TRUSTEE_IS_IMPERSONATE
;
2990 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2991 ok(res
== ERROR_INVALID_PARAMETER
||
2992 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2993 "SetEntriesInAclA failed: %u\n", res
);
2994 ok(NewAcl
== NULL
||
2995 broken(NewAcl
!= NULL
), /* NT4 */
2996 "returned acl wasn't NULL: %p\n", NewAcl
);
2998 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2999 ExplicitAccess
.grfAccessMode
= SET_ACCESS
;
3000 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3001 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3002 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3006 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
3007 ExplicitAccess
.Trustee
.ptstrName
= (LPSTR
)szCurrentUser
;
3008 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3009 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3010 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3013 ExplicitAccess
.grfAccessMode
= REVOKE_ACCESS
;
3014 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
3015 ExplicitAccess
.Trustee
.ptstrName
= UsersSid
;
3016 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3017 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3018 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3022 FreeSid(EveryoneSid
);
3023 HeapFree(GetProcessHeap(), 0, OldAcl
);
3026 static void test_CreateDirectoryA(void)
3028 char admin_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
], *user
;
3029 DWORD sid_size
= sizeof(admin_ptr
), user_size
;
3030 PSID admin_sid
= (PSID
) admin_ptr
, user_sid
;
3031 char sd
[SECURITY_DESCRIPTOR_MIN_LENGTH
];
3032 PSECURITY_DESCRIPTOR pSD
= &sd
;
3033 ACL_SIZE_INFORMATION acl_size
;
3034 ACCESS_ALLOWED_ACE
*ace
;
3035 SECURITY_ATTRIBUTES sa
;
3036 char tmpdir
[MAX_PATH
];
3043 if (!pGetNamedSecurityInfoA
|| !pCreateWellKnownSid
)
3045 win_skip("Required functions are not available\n");
3049 if (!OpenThreadToken(GetCurrentThread(), TOKEN_READ
, TRUE
, &token
))
3051 if (GetLastError() != ERROR_NO_TOKEN
) bret
= FALSE
;
3052 else if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ
, &token
)) bret
= FALSE
;
3056 win_skip("Failed to get current user token\n");
3059 bret
= GetTokenInformation(token
, TokenUser
, NULL
, 0, &user_size
);
3060 ok(!bret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
3061 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
3062 user
= HeapAlloc(GetProcessHeap(), 0, user_size
);
3063 bret
= GetTokenInformation(token
, TokenUser
, user
, user_size
, &user_size
);
3064 ok(bret
, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
3065 CloseHandle( token
);
3066 user_sid
= ((TOKEN_USER
*)user
)->User
.Sid
;
3068 sa
.nLength
= sizeof(sa
);
3069 sa
.lpSecurityDescriptor
= pSD
;
3070 sa
.bInheritHandle
= TRUE
;
3071 InitializeSecurityDescriptor(pSD
, SECURITY_DESCRIPTOR_REVISION
);
3072 pCreateWellKnownSid(WinBuiltinAdministratorsSid
, NULL
, admin_sid
, &sid_size
);
3073 pDacl
= HeapAlloc(GetProcessHeap(), 0, 100);
3074 bret
= InitializeAcl(pDacl
, 100, ACL_REVISION
);
3075 ok(bret
, "Failed to initialize ACL.\n");
3076 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
,
3077 GENERIC_ALL
, user_sid
);
3078 ok(bret
, "Failed to add Current User to ACL.\n");
3079 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
,
3080 GENERIC_ALL
, admin_sid
);
3081 ok(bret
, "Failed to add Administrator Group to ACL.\n");
3082 bret
= SetSecurityDescriptorDacl(pSD
, TRUE
, pDacl
, FALSE
);
3083 ok(bret
, "Failed to add ACL to security desciptor.\n");
3085 GetTempPathA(MAX_PATH
, tmpdir
);
3086 lstrcatA(tmpdir
, "Please Remove Me");
3087 bret
= CreateDirectoryA(tmpdir
, &sa
);
3088 ok(bret
== TRUE
, "CreateDirectoryA(%s) failed err=%d\n", tmpdir
, GetLastError());
3089 HeapFree(GetProcessHeap(), 0, pDacl
);
3091 SetLastError(0xdeadbeef);
3092 error
= pGetNamedSecurityInfoA(tmpdir
, SE_FILE_OBJECT
,
3093 OWNER_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, (PSID
*)&owner
,
3094 NULL
, &pDacl
, NULL
, &pSD
);
3095 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3097 win_skip("GetNamedSecurityInfoA is not implemented\n");
3100 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3102 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3103 ok(bret
, "GetAclInformation failed\n");
3104 ok(acl_size
.AceCount
== 2, "GetAclInformation returned unexpected entry count (%d != 2).\n",
3106 if (acl_size
.AceCount
> 0)
3108 bret
= pGetAce(pDacl
, 0, (VOID
**)&ace
);
3109 ok(bret
, "Failed to get Current User ACE.\n");
3110 bret
= EqualSid(&ace
->SidStart
, user_sid
);
3111 todo_wine
ok(bret
, "Current User ACE != Current User SID.\n");
3112 todo_wine
ok(((ACE_HEADER
*)ace
)->AceFlags
== (OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
),
3113 "Current User ACE has unexpected flags (0x%x != 0x03)\n",
3114 ((ACE_HEADER
*)ace
)->AceFlags
);
3115 ok(ace
->Mask
== 0x1f01ff, "Current User ACE has unexpected mask (0x%x != 0x1f01ff)\n",
3118 if (acl_size
.AceCount
> 1)
3120 bret
= pGetAce(pDacl
, 1, (VOID
**)&ace
);
3121 ok(bret
, "Failed to get Administators Group ACE.\n");
3122 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
3123 todo_wine
ok(bret
, "Administators Group ACE != Administators Group SID.\n");
3124 todo_wine
ok(((ACE_HEADER
*)ace
)->AceFlags
== (OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
),
3125 "Administators Group ACE has unexpected flags (0x%x != 0x03)\n",
3126 ((ACE_HEADER
*)ace
)->AceFlags
);
3127 ok(ace
->Mask
== 0x1f01ff, "Administators Group ACE has unexpected mask (0x%x != 0x1f01ff)\n",
3132 HeapFree(GetProcessHeap(), 0, user
);
3133 bret
= RemoveDirectoryA(tmpdir
);
3134 ok(bret
== TRUE
, "RemoveDirectoryA should always succeed\n");
3137 static void test_GetNamedSecurityInfoA(void)
3139 char admin_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
], *user
;
3140 char system_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
];
3141 char users_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
];
3142 PSID admin_sid
= (PSID
) admin_ptr
, users_sid
= (PSID
) users_ptr
;
3143 PSID system_sid
= (PSID
) system_ptr
, user_sid
;
3144 DWORD sid_size
= sizeof(admin_ptr
), user_size
;
3145 char invalid_path
[] = "/an invalid file path";
3146 int users_ace_id
= -1, admins_ace_id
= -1, i
;
3147 char software_key
[] = "MACHINE\\Software";
3148 char sd
[SECURITY_DESCRIPTOR_MIN_LENGTH
];
3149 SECURITY_DESCRIPTOR_CONTROL control
;
3150 ACL_SIZE_INFORMATION acl_size
;
3151 CHAR windows_dir
[MAX_PATH
];
3152 PSECURITY_DESCRIPTOR pSD
;
3153 ACCESS_ALLOWED_ACE
*ace
;
3154 BOOL bret
= TRUE
, isNT4
;
3155 char tmpfile
[MAX_PATH
];
3156 DWORD error
, revision
;
3157 BOOL owner_defaulted
;
3158 BOOL group_defaulted
;
3159 BOOL dacl_defaulted
;
3160 HANDLE token
, hTemp
;
3166 if (!pSetNamedSecurityInfoA
|| !pGetNamedSecurityInfoA
|| !pCreateWellKnownSid
)
3168 win_skip("Required functions are not available\n");
3172 if (!OpenThreadToken(GetCurrentThread(), TOKEN_READ
, TRUE
, &token
))
3174 if (GetLastError() != ERROR_NO_TOKEN
) bret
= FALSE
;
3175 else if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ
, &token
)) bret
= FALSE
;
3179 win_skip("Failed to get current user token\n");
3182 bret
= GetTokenInformation(token
, TokenUser
, NULL
, 0, &user_size
);
3183 ok(!bret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
3184 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
3185 user
= HeapAlloc(GetProcessHeap(), 0, user_size
);
3186 bret
= GetTokenInformation(token
, TokenUser
, user
, user_size
, &user_size
);
3187 ok(bret
, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
3188 CloseHandle( token
);
3189 user_sid
= ((TOKEN_USER
*)user
)->User
.Sid
;
3191 bret
= GetWindowsDirectoryA(windows_dir
, MAX_PATH
);
3192 ok(bret
, "GetWindowsDirectory failed with error %d\n", GetLastError());
3194 SetLastError(0xdeadbeef);
3195 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,
3196 OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
3197 NULL
, NULL
, NULL
, NULL
, &pSD
);
3198 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3200 win_skip("GetNamedSecurityInfoA is not implemented\n");
3201 HeapFree(GetProcessHeap(), 0, user
);
3204 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3206 bret
= GetSecurityDescriptorControl(pSD
, &control
, &revision
);
3207 ok(bret
, "GetSecurityDescriptorControl failed with error %d\n", GetLastError());
3208 ok((control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == (SE_SELF_RELATIVE
|SE_DACL_PRESENT
) ||
3209 broken((control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == SE_DACL_PRESENT
), /* NT4 */
3210 "control (0x%x) doesn't have (SE_SELF_RELATIVE|SE_DACL_PRESENT) flags set\n", control
);
3211 ok(revision
== SECURITY_DESCRIPTOR_REVISION1
, "revision was %d instead of 1\n", revision
);
3213 isNT4
= (control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == SE_DACL_PRESENT
;
3215 bret
= GetSecurityDescriptorOwner(pSD
, &owner
, &owner_defaulted
);
3216 ok(bret
, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
3217 ok(owner
!= NULL
, "owner should not be NULL\n");
3219 bret
= GetSecurityDescriptorGroup(pSD
, &group
, &group_defaulted
);
3220 ok(bret
, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
3221 ok(group
!= NULL
, "group should not be NULL\n");
3225 /* NULL descriptor tests */
3228 win_skip("NT4 does not support GetNamedSecutityInfo with a NULL descriptor\n");
3229 HeapFree(GetProcessHeap(), 0, user
);
3233 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,DACL_SECURITY_INFORMATION
,
3234 NULL
, NULL
, NULL
, NULL
, NULL
);
3235 ok(error
==ERROR_INVALID_PARAMETER
, "GetNamedSecurityInfo failed with error %d\n", error
);
3237 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,DACL_SECURITY_INFORMATION
,
3238 NULL
, NULL
, &pDacl
, NULL
, NULL
);
3239 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3240 ok(pDacl
!= NULL
, "DACL should not be NULL\n");
3242 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,OWNER_SECURITY_INFORMATION
,
3243 NULL
, NULL
, &pDacl
, NULL
, NULL
);
3244 ok(error
==ERROR_INVALID_PARAMETER
, "GetNamedSecurityInfo failed with error %d\n", error
);
3246 /* Test behavior of SetNamedSecurityInfo with an invalid path */
3247 SetLastError(0xdeadbeef);
3248 error
= pSetNamedSecurityInfoA(invalid_path
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
, NULL
,
3250 ok(error
== ERROR_FILE_NOT_FOUND
, "Unexpected error returned: 0x%x\n", error
);
3251 ok(GetLastError() == 0xdeadbeef, "Expected last error to remain unchanged.\n");
3253 /* Create security descriptor information and test that it comes back the same */
3255 pDacl
= HeapAlloc(GetProcessHeap(), 0, 100);
3256 InitializeSecurityDescriptor(pSD
, SECURITY_DESCRIPTOR_REVISION
);
3257 pCreateWellKnownSid(WinBuiltinAdministratorsSid
, NULL
, admin_sid
, &sid_size
);
3258 bret
= InitializeAcl(pDacl
, 100, ACL_REVISION
);
3259 ok(bret
, "Failed to initialize ACL.\n");
3260 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, user_sid
);
3261 ok(bret
, "Failed to add Current User to ACL.\n");
3262 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, admin_sid
);
3263 ok(bret
, "Failed to add Administrator Group to ACL.\n");
3264 bret
= SetSecurityDescriptorDacl(pSD
, TRUE
, pDacl
, FALSE
);
3265 ok(bret
, "Failed to add ACL to security desciptor.\n");
3266 GetTempFileNameA(".", "foo", 0, tmpfile
);
3267 hTemp
= CreateFileA(tmpfile
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
,
3268 FILE_FLAG_DELETE_ON_CLOSE
, NULL
);
3269 SetLastError(0xdeadbeef);
3270 error
= pSetNamedSecurityInfoA(tmpfile
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
, NULL
,
3272 HeapFree(GetProcessHeap(), 0, pDacl
);
3273 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3275 win_skip("SetNamedSecurityInfoA is not implemented\n");
3276 HeapFree(GetProcessHeap(), 0, user
);
3280 ok(!error
, "SetNamedSecurityInfoA failed with error %d\n", error
);
3281 SetLastError(0xdeadbeef);
3282 error
= pGetNamedSecurityInfoA(tmpfile
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
,
3283 NULL
, NULL
, &pDacl
, NULL
, &pSD
);
3284 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3286 win_skip("GetNamedSecurityInfoA is not implemented\n");
3287 HeapFree(GetProcessHeap(), 0, user
);
3291 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3293 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3294 ok(bret
, "GetAclInformation failed\n");
3295 if (acl_size
.AceCount
> 0)
3297 bret
= pGetAce(pDacl
, 0, (VOID
**)&ace
);
3298 ok(bret
, "Failed to get Current User ACE.\n");
3299 bret
= EqualSid(&ace
->SidStart
, user_sid
);
3300 todo_wine
ok(bret
, "Current User ACE != Current User SID.\n");
3301 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
3302 "Current User ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
3303 ok(ace
->Mask
== 0x1f01ff, "Current User ACE has unexpected mask (0x%x != 0x1f01ff)\n",
3306 if (acl_size
.AceCount
> 1)
3308 bret
= pGetAce(pDacl
, 1, (VOID
**)&ace
);
3309 ok(bret
, "Failed to get Administators Group ACE.\n");
3310 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
3311 todo_wine
ok(bret
|| broken(!bret
) /* win2k */,
3312 "Administators Group ACE != Administators Group SID.\n");
3313 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
3314 "Administators Group ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
3315 ok(ace
->Mask
== 0x1f01ff || broken(ace
->Mask
== GENERIC_ALL
) /* win2k */,
3316 "Administators Group ACE has unexpected mask (0x%x != 0x1f01ff)\n", ace
->Mask
);
3319 HeapFree(GetProcessHeap(), 0, user
);
3322 /* Test querying the ownership of a built-in registry key */
3323 sid_size
= sizeof(system_ptr
);
3324 pCreateWellKnownSid(WinLocalSystemSid
, NULL
, system_sid
, &sid_size
);
3325 error
= pGetNamedSecurityInfoA(software_key
, SE_REGISTRY_KEY
,
3326 OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
,
3327 NULL
, NULL
, NULL
, NULL
, &pSD
);
3328 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3330 bret
= GetSecurityDescriptorOwner(pSD
, &owner
, &owner_defaulted
);
3331 ok(bret
, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
3332 ok(owner
!= NULL
, "owner should not be NULL\n");
3333 ok(EqualSid(owner
, admin_sid
), "MACHINE\\Software owner SID != Administrators SID.\n");
3335 bret
= GetSecurityDescriptorGroup(pSD
, &group
, &group_defaulted
);
3336 ok(bret
, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
3337 ok(group
!= NULL
, "group should not be NULL\n");
3338 ok(EqualSid(group
, admin_sid
) || broken(EqualSid(group
, system_sid
)) /* before Win7 */
3339 || broken(((SID
*)group
)->SubAuthority
[0] == SECURITY_NT_NON_UNIQUE
) /* Vista */,
3340 "MACHINE\\Software group SID != Local System SID.\n");
3343 /* Test querying the DACL of a built-in registry key */
3344 sid_size
= sizeof(users_ptr
);
3345 pCreateWellKnownSid(WinBuiltinUsersSid
, NULL
, users_sid
, &sid_size
);
3346 error
= pGetNamedSecurityInfoA(software_key
, SE_REGISTRY_KEY
, DACL_SECURITY_INFORMATION
,
3347 NULL
, NULL
, NULL
, NULL
, &pSD
);
3348 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3350 bret
= GetSecurityDescriptorDacl(pSD
, &dacl_present
, &pDacl
, &dacl_defaulted
);
3351 ok(bret
, "GetSecurityDescriptorDacl failed with error %d\n", GetLastError());
3352 ok(dacl_present
, "DACL should be present\n");
3353 ok(pDacl
&& IsValidAcl(pDacl
), "GetSecurityDescriptorDacl returned invalid DACL.\n");
3354 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3355 ok(bret
, "GetAclInformation failed\n");
3356 ok(acl_size
.AceCount
!= 0, "GetAclInformation returned no ACLs\n");
3357 for (i
=0; i
<acl_size
.AceCount
; i
++)
3359 bret
= pGetAce(pDacl
, i
, (VOID
**)&ace
);
3360 ok(bret
, "Failed to get ACE %d.\n", i
);
3361 bret
= EqualSid(&ace
->SidStart
, users_sid
);
3362 if (bret
) users_ace_id
= i
;
3363 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
3364 if (bret
) admins_ace_id
= i
;
3366 ok(users_ace_id
!= -1 || broken(users_ace_id
== -1) /* win2k */,
3367 "Bultin Users ACE not found.\n");
3368 if (users_ace_id
!= -1)
3370 bret
= pGetAce(pDacl
, users_ace_id
, (VOID
**)&ace
);
3371 ok(bret
, "Failed to get Builtin Users ACE.\n");
3372 flags
= ((ACE_HEADER
*)ace
)->AceFlags
;
3373 ok(flags
== (INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
)
3374 || broken(flags
== (INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
|INHERITED_ACE
)) /* w2k8 */,
3375 "Builtin Users ACE has unexpected flags (0x%x != 0x%x)\n", flags
,
3376 INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
);
3377 ok(ace
->Mask
== GENERIC_READ
, "Builtin Users ACE has unexpected mask (0x%x != 0x%x)\n",
3378 ace
->Mask
, GENERIC_READ
);
3380 ok(admins_ace_id
!= -1, "Bultin Admins ACE not found.\n");
3381 if (admins_ace_id
!= -1)
3383 bret
= pGetAce(pDacl
, admins_ace_id
, (VOID
**)&ace
);
3384 ok(bret
, "Failed to get Builtin Admins ACE.\n");
3385 flags
= ((ACE_HEADER
*)ace
)->AceFlags
;
3387 || broken(flags
== (INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
|INHERITED_ACE
)) /* w2k8 */,
3388 "Builtin Admins ACE has unexpected flags (0x%x != 0x0)\n", flags
);
3389 ok(ace
->Mask
== KEY_ALL_ACCESS
|| broken(ace
->Mask
== GENERIC_ALL
) /* w2k8 */,
3390 "Builtin Admins ACE has unexpected mask (0x%x != 0x%x)\n", ace
->Mask
, KEY_ALL_ACCESS
);
3395 static void test_ConvertStringSecurityDescriptor(void)
3398 PSECURITY_DESCRIPTOR pSD
;
3399 static const WCHAR Blank
[] = { 0 };
3403 const char *sidstring
;
3410 { "D:(A;;GA;;;WD)", 0xdeadbeef, FALSE
, ERROR_UNKNOWN_REVISION
},
3411 /* test ACE string type */
3412 { "D:(A;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3413 { "D:(D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3414 { "ERROR:(D;;GA;;;WD)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_PARAMETER
},
3415 /* test ACE string with spaces */
3416 { " D:(D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3417 { "D: (D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3418 { "D:( D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3419 { "D:(D ;;GA;;;WD)", SDDL_REVISION_1
, FALSE
, RPC_S_INVALID_STRING_UUID
, ERROR_INVALID_ACL
}, /* Vista+ */
3420 { "D:(D; ;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3421 { "D:(D;; GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3422 { "D:(D;;GA ;;;WD)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_ACL
},
3423 { "D:(D;;GA; ;;WD)", SDDL_REVISION_1
, TRUE
},
3424 { "D:(D;;GA;; ;WD)", SDDL_REVISION_1
, TRUE
},
3425 { "D:(D;;GA;;; WD)", SDDL_REVISION_1
, TRUE
},
3426 { "D:(D;;GA;;;WD )", SDDL_REVISION_1
, TRUE
},
3427 /* test ACE string access rights */
3428 { "D:(A;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3429 { "D:(A;;GRGWGX;;;WD)", SDDL_REVISION_1
, TRUE
},
3430 { "D:(A;;RCSDWDWO;;;WD)", SDDL_REVISION_1
, TRUE
},
3431 { "D:(A;;RPWPCCDCLCSWLODTCR;;;WD)", SDDL_REVISION_1
, TRUE
},
3432 { "D:(A;;FAFRFWFX;;;WD)", SDDL_REVISION_1
, TRUE
},
3433 { "D:(A;;KAKRKWKX;;;WD)", SDDL_REVISION_1
, TRUE
},
3434 { "D:(A;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1
, TRUE
},
3435 { "S:(AU;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1
, TRUE
},
3436 /* test ACE string access right error case */
3437 { "D:(A;;ROB;;;WD)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_ACL
},
3438 /* test behaviour with empty strings */
3439 { "", SDDL_REVISION_1
, TRUE
},
3440 /* test ACE string SID */
3441 { "D:(D;;GA;;;S-1-0-0)", SDDL_REVISION_1
, TRUE
},
3442 { "D:(D;;GA;;;Nonexistent account)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_ACL
, ERROR_INVALID_SID
} /* W2K */
3445 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA
)
3447 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
3451 for (i
= 0; i
< sizeof(cssd
)/sizeof(cssd
[0]); i
++)
3455 SetLastError(0xdeadbeef);
3456 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3457 cssd
[i
].sidstring
, cssd
[i
].revision
, &pSD
, NULL
);
3458 GLE
= GetLastError();
3459 ok(ret
== cssd
[i
].ret
, "(%02u) Expected %s (%d)\n", i
, cssd
[i
].ret
? "success" : "failure", GLE
);
3461 ok(GLE
== cssd
[i
].GLE
||
3462 (cssd
[i
].altGLE
&& GLE
== cssd
[i
].altGLE
),
3463 "(%02u) Unexpected last error %d\n", i
, GLE
);
3468 /* test behaviour with NULL parameters */
3469 SetLastError(0xdeadbeef);
3470 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3471 NULL
, 0xdeadbeef, &pSD
, NULL
);
3473 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3474 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3477 SetLastError(0xdeadbeef);
3478 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorW(
3479 NULL
, 0xdeadbeef, &pSD
, NULL
);
3480 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3481 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3484 SetLastError(0xdeadbeef);
3485 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3486 "D:(A;;ROB;;;WD)", 0xdeadbeef, NULL
, NULL
);
3487 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3488 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3491 SetLastError(0xdeadbeef);
3492 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3493 "D:(A;;ROB;;;WD)", SDDL_REVISION_1
, NULL
, NULL
);
3494 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3495 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3498 /* test behaviour with empty strings */
3499 SetLastError(0xdeadbeef);
3500 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorW(
3501 Blank
, SDDL_REVISION_1
, &pSD
, NULL
);
3502 ok(ret
, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
3506 static void test_ConvertSecurityDescriptorToString(void)
3508 SECURITY_DESCRIPTOR desc
;
3509 SECURITY_INFORMATION sec_info
= OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
|SACL_SECURITY_INFORMATION
;
3518 if (!pConvertSecurityDescriptorToStringSecurityDescriptorA
)
3520 win_skip("ConvertSecurityDescriptorToStringSecurityDescriptor is not available\n");
3523 if (!pCreateWellKnownSid
)
3525 win_skip("CreateWellKnownSid is not available\n");
3529 /* It seems Windows XP adds an extra character to the length of the string for each ACE in an ACL. We
3530 * don't replicate this feature so we only test len >= strlen+1. */
3531 #define CHECK_RESULT_AND_FREE(exp_str) \
3532 ok(strcmp(string, (exp_str)) == 0, "String mismatch (expected \"%s\", got \"%s\")\n", (exp_str), string); \
3533 ok(len >= (strlen(exp_str) + 1), "Length mismatch (expected %d, got %d)\n", lstrlenA(exp_str) + 1, len); \
3536 #define CHECK_ONE_OF_AND_FREE(exp_str1, exp_str2) \
3537 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); \
3538 ok(len >= (strlen(exp_str1) + 1) || len >= (strlen(exp_str2) + 1), "Length mismatch (expected %d or %d, got %d)\n", lstrlenA(exp_str1) + 1, lstrlenA(exp_str2) + 1, len); \
3541 InitializeSecurityDescriptor(&desc
, SECURITY_DESCRIPTOR_REVISION
);
3542 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3543 CHECK_RESULT_AND_FREE("");
3546 pCreateWellKnownSid(WinLocalSid
, NULL
, sid_buf
, &size
);
3547 SetSecurityDescriptorOwner(&desc
, sid_buf
, FALSE
);
3548 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3549 CHECK_RESULT_AND_FREE("O:S-1-2-0");
3551 SetSecurityDescriptorOwner(&desc
, sid_buf
, TRUE
);
3552 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3553 CHECK_RESULT_AND_FREE("O:S-1-2-0");
3555 size
= sizeof(sid_buf
);
3556 pCreateWellKnownSid(WinLocalSystemSid
, NULL
, sid_buf
, &size
);
3557 SetSecurityDescriptorOwner(&desc
, sid_buf
, TRUE
);
3558 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3559 CHECK_RESULT_AND_FREE("O:SY");
3561 pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid
);
3562 SetSecurityDescriptorGroup(&desc
, psid
, TRUE
);
3563 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3564 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576");
3566 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, GROUP_SECURITY_INFORMATION
, &string
, &len
), "Conversion failed\n");
3567 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
3569 pacl
= (PACL
)acl_buf
;
3570 InitializeAcl(pacl
, sizeof(acl_buf
), ACL_REVISION
);
3571 SetSecurityDescriptorDacl(&desc
, TRUE
, pacl
, TRUE
);
3572 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3573 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
3575 SetSecurityDescriptorDacl(&desc
, TRUE
, pacl
, FALSE
);
3576 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3577 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
3579 pConvertStringSidToSidA("S-1-5-6", &psid2
);
3580 pAddAccessAllowedAceEx(pacl
, ACL_REVISION
, NO_PROPAGATE_INHERIT_ACE
, 0xf0000000, psid2
);
3581 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3582 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)");
3584 pAddAccessAllowedAceEx(pacl
, ACL_REVISION
, INHERIT_ONLY_ACE
|INHERITED_ACE
, 0x00000003, psid2
);
3585 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3586 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)");
3588 pAddAccessDeniedAceEx(pacl
, ACL_REVISION
, OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
, 0xffffffff, psid
);
3589 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3590 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)");
3593 pacl
= (PACL
)acl_buf
;
3594 InitializeAcl(pacl
, sizeof(acl_buf
), ACL_REVISION
);
3595 SetSecurityDescriptorSacl(&desc
, TRUE
, pacl
, FALSE
);
3596 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3597 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:");
3599 /* fails in win2k */
3600 SetSecurityDescriptorDacl(&desc
, TRUE
, NULL
, FALSE
);
3601 pAddAuditAccessAceEx(pacl
, ACL_REVISION
, VALID_INHERIT_FLAGS
, KEY_READ
|KEY_WRITE
, psid2
, TRUE
, TRUE
);
3602 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
))
3604 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)", /* XP */
3605 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)" /* Vista */);
3608 /* fails in win2k */
3609 pAddAuditAccessAceEx(pacl
, ACL_REVISION
, NO_PROPAGATE_INHERIT_ACE
, FILE_GENERIC_READ
|FILE_GENERIC_WRITE
, psid2
, TRUE
, FALSE
);
3610 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
))
3612 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", /* XP */
3613 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)" /* Vista */);
3620 static void test_SetSecurityDescriptorControl (PSECURITY_DESCRIPTOR sec
)
3622 SECURITY_DESCRIPTOR_CONTROL ref
;
3623 SECURITY_DESCRIPTOR_CONTROL test
;
3625 SECURITY_DESCRIPTOR_CONTROL
const mutable
3626 = SE_DACL_AUTO_INHERIT_REQ
| SE_SACL_AUTO_INHERIT_REQ
3627 | SE_DACL_AUTO_INHERITED
| SE_SACL_AUTO_INHERITED
3628 | SE_DACL_PROTECTED
| SE_SACL_PROTECTED
3629 | 0x00000040 | 0x00000080 /* not defined in winnt.h */
3631 SECURITY_DESCRIPTOR_CONTROL
const immutable
3632 = SE_OWNER_DEFAULTED
| SE_GROUP_DEFAULTED
3633 | SE_DACL_PRESENT
| SE_DACL_DEFAULTED
3634 | SE_SACL_PRESENT
| SE_SACL_DEFAULTED
3635 | SE_RM_CONTROL_VALID
| SE_SELF_RELATIVE
3640 LPCSTR fmt
= "Expected error %s, got %u\n";
3642 GetSecurityDescriptorControl (sec
, &ref
, &dwRevision
);
3644 /* The mutable bits are mutable regardless of the truth of
3645 SE_DACL_PRESENT and/or SE_SACL_PRESENT */
3647 /* Check call barfs if any bit-of-interest is immutable */
3648 for (bit
= 0; bit
< 16; ++bit
)
3650 SECURITY_DESCRIPTOR_CONTROL
const bitOfInterest
= 1 << bit
;
3651 SECURITY_DESCRIPTOR_CONTROL setOrClear
= ref
& bitOfInterest
;
3653 SECURITY_DESCRIPTOR_CONTROL ctrl
;
3655 DWORD dwExpect
= (bitOfInterest
& immutable
)
3656 ? ERROR_INVALID_PARAMETER
: 0xbebecaca;
3657 LPCSTR strExpect
= (bitOfInterest
& immutable
)
3658 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
3660 ctrl
= (bitOfInterest
& mutable) ? ref
+ bitOfInterest
: ref
;
3661 setOrClear
^= bitOfInterest
;
3662 SetLastError (0xbebecaca);
3663 pSetSecurityDescriptorControl (sec
, bitOfInterest
, setOrClear
);
3664 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3665 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
3666 expect_eq(test
, ctrl
, int, "%x");
3668 setOrClear
^= bitOfInterest
;
3669 SetLastError (0xbebecaca);
3670 pSetSecurityDescriptorControl (sec
, bitOfInterest
, setOrClear
);
3671 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3672 GetSecurityDescriptorControl (sec
, &test
, &dwRevision
);
3673 expect_eq(test
, ref
, int, "%x");
3676 /* Check call barfs if any bit-to-set is immutable
3677 even when not a bit-of-interest */
3678 for (bit
= 0; bit
< 16; ++bit
)
3680 SECURITY_DESCRIPTOR_CONTROL
const bitsOfInterest
= mutable;
3681 SECURITY_DESCRIPTOR_CONTROL setOrClear
= ref
& bitsOfInterest
;
3683 SECURITY_DESCRIPTOR_CONTROL ctrl
;
3685 DWORD dwExpect
= ((1 << bit
) & immutable
)
3686 ? ERROR_INVALID_PARAMETER
: 0xbebecaca;
3687 LPCSTR strExpect
= ((1 << bit
) & immutable
)
3688 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
3690 ctrl
= ((1 << bit
) & immutable
) ? test
: ref
| mutable;
3691 setOrClear
^= bitsOfInterest
;
3692 SetLastError (0xbebecaca);
3693 pSetSecurityDescriptorControl (sec
, bitsOfInterest
, setOrClear
| (1 << bit
));
3694 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3695 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
3696 expect_eq(test
, ctrl
, int, "%x");
3698 ctrl
= ((1 << bit
) & immutable
) ? test
: ref
| (1 << bit
);
3699 setOrClear
^= bitsOfInterest
;
3700 SetLastError (0xbebecaca);
3701 pSetSecurityDescriptorControl (sec
, bitsOfInterest
, setOrClear
| (1 << bit
));
3702 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3703 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
3704 expect_eq(test
, ctrl
, int, "%x");
3708 static void test_PrivateObjectSecurity(void)
3710 SECURITY_INFORMATION sec_info
= OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
|SACL_SECURITY_INFORMATION
;
3711 SECURITY_DESCRIPTOR_CONTROL ctrl
;
3712 PSECURITY_DESCRIPTOR sec
;
3718 PSECURITY_DESCRIPTOR buf
;
3721 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA
)
3723 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
3727 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3729 "G:S-1-5-21-93476-23408-4576"
3730 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)"
3731 "(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3732 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
3733 SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
3735 test_SetSecurityDescriptorControl(sec
);
3739 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3741 "G:S-1-5-21-93476-23408-4576",
3742 SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
3744 test_SetSecurityDescriptorControl(sec
);
3748 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3750 "G:S-1-5-21-93476-23408-4576"
3751 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3752 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
3753 buf
= HeapAlloc(GetProcessHeap(), 0, dwDescSize
);
3754 pSetSecurityDescriptorControl(sec
, SE_DACL_PROTECTED
, SE_DACL_PROTECTED
);
3755 GetSecurityDescriptorControl(sec
, &ctrl
, &dwRevision
);
3756 expect_eq(ctrl
, 0x9014, int, "%x");
3758 ret
= GetPrivateObjectSecurity(sec
, GROUP_SECURITY_INFORMATION
, buf
, dwDescSize
, &retSize
);
3759 ok(ret
, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3760 ok(retSize
<= dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
3761 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3762 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
3763 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
3764 expect_eq(ctrl
, 0x8000, int, "%x");
3766 ret
= GetPrivateObjectSecurity(sec
, GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, buf
, dwDescSize
, &retSize
);
3767 ok(ret
, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3768 ok(retSize
<= dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
3769 ret
= pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
);
3770 ok(ret
, "Conversion failed err=%u\n", GetLastError());
3771 CHECK_ONE_OF_AND_FREE("G:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)",
3772 "G:S-1-5-21-93476-23408-4576D:P(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"); /* Win7 */
3773 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
3774 expect_eq(ctrl
& (~ SE_DACL_PROTECTED
), 0x8004, int, "%x");
3776 ret
= GetPrivateObjectSecurity(sec
, sec_info
, buf
, dwDescSize
, &retSize
);
3777 ok(ret
, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3778 ok(retSize
== dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
3779 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3780 CHECK_ONE_OF_AND_FREE("O:SY"
3781 "G:S-1-5-21-93476-23408-4576"
3782 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3783 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
3785 "G:S-1-5-21-93476-23408-4576"
3786 "D:P(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3787 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)"); /* Win7 */
3788 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
3789 expect_eq(ctrl
& (~ SE_DACL_PROTECTED
), 0x8014, int, "%x");
3791 SetLastError(0xdeadbeef);
3792 ok(GetPrivateObjectSecurity(sec
, sec_info
, buf
, 5, &retSize
) == FALSE
, "GetPrivateObjectSecurity should have failed\n");
3793 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Expected error ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
3796 HeapFree(GetProcessHeap(), 0, buf
);
3798 #undef CHECK_RESULT_AND_FREE
3799 #undef CHECK_ONE_OF_AND_FREE
3801 static void test_acls(void)
3804 PACL pAcl
= (PACL
)buffer
;
3807 SetLastError(0xdeadbeef);
3808 ret
= InitializeAcl(pAcl
, sizeof(ACL
) - 1, ACL_REVISION
);
3809 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
3811 win_skip("InitializeAcl is not implemented\n");
3815 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "InitializeAcl with too small a buffer should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
3817 SetLastError(0xdeadbeef);
3818 ret
= InitializeAcl(pAcl
, 0xffffffff, ACL_REVISION
);
3819 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl with too large a buffer should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3821 SetLastError(0xdeadbeef);
3822 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION1
);
3823 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl(ACL_REVISION1) should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3825 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION2
);
3826 ok(ret
, "InitializeAcl(ACL_REVISION2) failed with error %d\n", GetLastError());
3828 ret
= IsValidAcl(pAcl
);
3829 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
3831 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION3
);
3832 ok(ret
, "InitializeAcl(ACL_REVISION3) failed with error %d\n", GetLastError());
3834 ret
= IsValidAcl(pAcl
);
3835 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
3837 SetLastError(0xdeadbeef);
3838 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION4
);
3839 if (GetLastError() != ERROR_INVALID_PARAMETER
)
3841 ok(ret
, "InitializeAcl(ACL_REVISION4) failed with error %d\n", GetLastError());
3843 ret
= IsValidAcl(pAcl
);
3844 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
3847 win_skip("ACL_REVISION4 is not implemented on NT4\n");
3849 SetLastError(0xdeadbeef);
3850 ret
= InitializeAcl(pAcl
, sizeof(buffer
), -1);
3851 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl(-1) failed with error %d\n", GetLastError());
3854 static void test_GetSecurityInfo(void)
3856 char b
[sizeof(TOKEN_USER
) + sizeof(SID
) + sizeof(DWORD
)*SID_MAX_SUB_AUTHORITIES
];
3857 char admin_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
], dacl
[100];
3858 DWORD sid_size
= sizeof(admin_ptr
), l
= sizeof(b
);
3859 PSID admin_sid
= (PSID
) admin_ptr
, user_sid
;
3860 char sd
[SECURITY_DESCRIPTOR_MIN_LENGTH
];
3861 ACL_SIZE_INFORMATION acl_size
;
3862 PSECURITY_DESCRIPTOR pSD
;
3863 ACCESS_ALLOWED_ACE
*ace
;
3870 if (!pGetSecurityInfo
|| !pSetSecurityInfo
)
3872 win_skip("[Get|Set]SecurityInfo is not available\n");
3876 if (!OpenThreadToken(GetCurrentThread(), TOKEN_READ
, TRUE
, &token
))
3878 if (GetLastError() != ERROR_NO_TOKEN
) bret
= FALSE
;
3879 else if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ
, &token
)) bret
= FALSE
;
3883 win_skip("Failed to get current user token\n");
3886 GetTokenInformation(token
, TokenUser
, b
, l
, &l
);
3887 CloseHandle( token
);
3888 user_sid
= ((TOKEN_USER
*)b
)->User
.Sid
;
3890 /* Create something. Files have lots of associated security info. */
3891 obj
= CreateFileA(myARGV
[0], GENERIC_READ
|WRITE_DAC
, FILE_SHARE_READ
, NULL
,
3892 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3893 if (obj
== INVALID_HANDLE_VALUE
)
3895 skip("Couldn't create an object for GetSecurityInfo test\n");
3899 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
,
3900 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
3901 &owner
, &group
, &pDacl
, NULL
, &pSD
);
3902 if (ret
== ERROR_CALL_NOT_IMPLEMENTED
)
3904 win_skip("GetSecurityInfo is not implemented\n");
3908 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
3909 ok(pSD
!= NULL
, "GetSecurityInfo\n");
3910 ok(owner
!= NULL
, "GetSecurityInfo\n");
3911 ok(group
!= NULL
, "GetSecurityInfo\n");
3913 ok(IsValidAcl(pDacl
), "GetSecurityInfo\n");
3915 win_skip("No ACL information returned\n");
3919 if (!pCreateWellKnownSid
)
3921 win_skip("NULL parameter test would crash on NT4\n");
3926 /* If we don't ask for the security descriptor, Windows will still give us
3927 the other stuff, leaving us no way to free it. */
3928 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
,
3929 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
3930 &owner
, &group
, &pDacl
, NULL
, NULL
);
3931 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
3932 ok(owner
!= NULL
, "GetSecurityInfo\n");
3933 ok(group
!= NULL
, "GetSecurityInfo\n");
3935 ok(IsValidAcl(pDacl
), "GetSecurityInfo\n");
3937 win_skip("No ACL information returned\n");
3939 /* Create security descriptor information and test that it comes back the same */
3941 pDacl
= (PACL
)&dacl
;
3942 InitializeSecurityDescriptor(pSD
, SECURITY_DESCRIPTOR_REVISION
);
3943 pCreateWellKnownSid(WinBuiltinAdministratorsSid
, NULL
, admin_sid
, &sid_size
);
3944 bret
= InitializeAcl(pDacl
, sizeof(dacl
), ACL_REVISION
);
3945 ok(bret
, "Failed to initialize ACL.\n");
3946 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, user_sid
);
3947 ok(bret
, "Failed to add Current User to ACL.\n");
3948 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, admin_sid
);
3949 ok(bret
, "Failed to add Administrator Group to ACL.\n");
3950 bret
= SetSecurityDescriptorDacl(pSD
, TRUE
, pDacl
, FALSE
);
3951 ok(bret
, "Failed to add ACL to security desciptor.\n");
3952 ret
= pSetSecurityInfo(obj
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
,
3953 NULL
, NULL
, pDacl
, NULL
);
3954 ok(ret
== ERROR_SUCCESS
, "SetSecurityInfo returned %d\n", ret
);
3955 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
,
3956 NULL
, NULL
, &pDacl
, NULL
, NULL
);
3957 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
3958 ok(pDacl
&& IsValidAcl(pDacl
), "GetSecurityInfo returned invalid DACL.\n");
3959 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3960 ok(bret
, "GetAclInformation failed\n");
3961 if (acl_size
.AceCount
> 0)
3963 bret
= pGetAce(pDacl
, 0, (VOID
**)&ace
);
3964 ok(bret
, "Failed to get Current User ACE.\n");
3965 bret
= EqualSid(&ace
->SidStart
, user_sid
);
3966 todo_wine
ok(bret
, "Current User ACE != Current User SID.\n");
3967 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
3968 "Current User ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
3969 ok(ace
->Mask
== 0x1f01ff, "Current User ACE has unexpected mask (0x%x != 0x1f01ff)\n",
3972 if (acl_size
.AceCount
> 1)
3974 bret
= pGetAce(pDacl
, 1, (VOID
**)&ace
);
3975 ok(bret
, "Failed to get Administators Group ACE.\n");
3976 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
3977 todo_wine
ok(bret
, "Administators Group ACE != Administators Group SID.\n");
3978 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
3979 "Administators Group ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
3980 ok(ace
->Mask
== 0x1f01ff, "Administators Group ACE has unexpected mask (0x%x != 0x1f01ff)\n",
3986 static void test_GetSidSubAuthority(void)
3990 if (!pGetSidSubAuthority
|| !pConvertStringSidToSidA
|| !pIsValidSid
|| !pGetSidSubAuthorityCount
)
3992 win_skip("Some functions not available\n");
3995 /* Note: on windows passing in an invalid index like -1, lets GetSidSubAuthority return 0x05000000 but
3996 still GetLastError returns ERROR_SUCCESS then. We don't test these unlikely cornercases here for now */
3997 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576",&psid
),"ConvertStringSidToSidA failed\n");
3998 ok(pIsValidSid(psid
),"Sid is not valid\n");
3999 SetLastError(0xbebecaca);
4000 ok(*pGetSidSubAuthorityCount(psid
) == 4,"GetSidSubAuthorityCount gave %d expected 4\n",*pGetSidSubAuthorityCount(psid
));
4001 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
4002 SetLastError(0xbebecaca);
4003 ok(*pGetSidSubAuthority(psid
,0) == 21,"GetSidSubAuthority gave %d expected 21\n",*pGetSidSubAuthority(psid
,0));
4004 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
4005 SetLastError(0xbebecaca);
4006 ok(*pGetSidSubAuthority(psid
,1) == 93476,"GetSidSubAuthority gave %d expected 93476\n",*pGetSidSubAuthority(psid
,1));
4007 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
4008 SetLastError(0xbebecaca);
4009 ok(pGetSidSubAuthority(psid
,4) != NULL
,"Expected out of bounds GetSidSubAuthority to return a non-NULL pointer\n");
4010 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
4014 static void test_CheckTokenMembership(void)
4016 PTOKEN_GROUPS token_groups
;
4018 HANDLE process_token
, token
;
4023 if (!pCheckTokenMembership
)
4025 win_skip("CheckTokenMembership is not available\n");
4028 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &process_token
);
4029 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
4031 ret
= DuplicateToken(process_token
, SecurityImpersonation
, &token
);
4032 ok(ret
, "DuplicateToken failed with error %d\n", GetLastError());
4035 ret
= GetTokenInformation(token
, TokenGroups
, NULL
, 0, &size
);
4036 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
4037 "GetTokenInformation(TokenGroups) %s with error %d\n",
4038 ret
? "succeeded" : "failed", GetLastError());
4039 token_groups
= HeapAlloc(GetProcessHeap(), 0, size
);
4040 ret
= GetTokenInformation(token
, TokenGroups
, token_groups
, size
, &size
);
4041 ok(ret
, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
4043 for (i
= 0; i
< token_groups
->GroupCount
; i
++)
4045 if (token_groups
->Groups
[i
].Attributes
& SE_GROUP_ENABLED
)
4049 if (i
== token_groups
->GroupCount
)
4051 HeapFree(GetProcessHeap(), 0, token_groups
);
4053 skip("user not a member of any group\n");
4058 ret
= pCheckTokenMembership(token
, token_groups
->Groups
[i
].Sid
, &is_member
);
4059 ok(ret
, "CheckTokenMembership failed with error %d\n", GetLastError());
4060 ok(is_member
, "CheckTokenMembership should have detected sid as member\n");
4063 ret
= pCheckTokenMembership(NULL
, token_groups
->Groups
[i
].Sid
, &is_member
);
4064 ok(ret
, "CheckTokenMembership failed with error %d\n", GetLastError());
4065 ok(is_member
, "CheckTokenMembership should have detected sid as member\n");
4068 SetLastError(0xdeadbeef);
4069 ret
= pCheckTokenMembership(process_token
, token_groups
->Groups
[i
].Sid
, &is_member
);
4070 ok(!ret
&& GetLastError() == ERROR_NO_IMPERSONATION_TOKEN
,
4071 "CheckTokenMembership with process token %s with error %d\n",
4072 ret
? "succeeded" : "failed", GetLastError());
4073 ok(!is_member
, "CheckTokenMembership should have cleared is_member\n");
4075 HeapFree(GetProcessHeap(), 0, token_groups
);
4077 CloseHandle(process_token
);
4080 static void test_EqualSid(void)
4084 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
4085 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
4087 SetLastError(0xdeadbeef);
4088 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
4089 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &sid1
);
4090 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
4092 win_skip("AllocateAndInitializeSid is not implemented\n");
4095 ok(ret
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
4096 ok(GetLastError() == 0xdeadbeef,
4097 "AllocateAndInitializeSid shouldn't have set last error to %d\n",
4100 ret
= AllocateAndInitializeSid(&SIDAuthWorld
, 1, SECURITY_WORLD_RID
,
4101 0, 0, 0, 0, 0, 0, 0, &sid2
);
4102 ok(ret
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
4104 SetLastError(0xdeadbeef);
4105 ret
= EqualSid(sid1
, sid2
);
4106 ok(!ret
, "World and domain admins sids shouldn't have been equal\n");
4107 ok(GetLastError() == ERROR_SUCCESS
||
4108 broken(GetLastError() == 0xdeadbeef), /* NT4 */
4109 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
4112 SetLastError(0xdeadbeef);
4113 sid2
= FreeSid(sid2
);
4114 ok(!sid2
, "FreeSid should have returned NULL instead of %p\n", sid2
);
4115 ok(GetLastError() == 0xdeadbeef,
4116 "FreeSid shouldn't have set last error to %d\n",
4119 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
4120 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &sid2
);
4121 ok(ret
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
4123 SetLastError(0xdeadbeef);
4124 ret
= EqualSid(sid1
, sid2
);
4125 ok(ret
, "Same sids should have been equal\n");
4126 ok(GetLastError() == ERROR_SUCCESS
||
4127 broken(GetLastError() == 0xdeadbeef), /* NT4 */
4128 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
4131 ((SID
*)sid2
)->Revision
= 2;
4132 SetLastError(0xdeadbeef);
4133 ret
= EqualSid(sid1
, sid2
);
4134 ok(!ret
, "EqualSid with invalid sid should have returned FALSE\n");
4135 ok(GetLastError() == ERROR_SUCCESS
||
4136 broken(GetLastError() == 0xdeadbeef), /* NT4 */
4137 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
4139 ((SID
*)sid2
)->Revision
= SID_REVISION
;
4145 static void test_GetUserNameA(void)
4147 char buffer
[UNLEN
+ 1], filler
[UNLEN
+ 1];
4148 DWORD required_len
, buffer_len
;
4151 /* Test crashes on Windows. */
4154 SetLastError(0xdeadbeef);
4155 GetUserNameA(NULL
, NULL
);
4158 SetLastError(0xdeadbeef);
4160 ret
= GetUserNameA(NULL
, &required_len
);
4161 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
4162 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
4163 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4165 SetLastError(0xdeadbeef);
4167 ret
= GetUserNameA(NULL
, &required_len
);
4168 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
4169 ok(required_len
!= 0 && required_len
!= 1, "Outputted buffer length was %u\n", required_len
);
4170 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4172 /* Tests crashes on Windows. */
4175 SetLastError(0xdeadbeef);
4176 required_len
= UNLEN
+ 1;
4177 GetUserNameA(NULL
, &required_len
);
4179 SetLastError(0xdeadbeef);
4180 GetUserNameA(buffer
, NULL
);
4183 memset(filler
, 'x', sizeof(filler
));
4185 /* Note that GetUserNameA on XP and newer outputs the number of bytes
4186 * required for a Unicode string, which affects a test in the next block. */
4187 SetLastError(0xdeadbeef);
4188 memcpy(buffer
, filler
, sizeof(filler
));
4190 ret
= GetUserNameA(buffer
, &required_len
);
4191 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
4192 ok(!memcmp(buffer
, filler
, sizeof(filler
)), "Output buffer was altered\n");
4193 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
4194 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4196 SetLastError(0xdeadbeef);
4197 memcpy(buffer
, filler
, sizeof(filler
));
4198 buffer_len
= required_len
;
4199 ret
= GetUserNameA(buffer
, &buffer_len
);
4200 ok(ret
== TRUE
, "GetUserNameA returned %d, last error %u\n", ret
, GetLastError());
4201 ok(memcmp(buffer
, filler
, sizeof(filler
)) != 0, "Output buffer was untouched\n");
4202 ok(buffer_len
== required_len
||
4203 broken(buffer_len
== required_len
/ sizeof(WCHAR
)), /* XP+ */
4204 "Outputted buffer length was %u\n", buffer_len
);
4206 /* Use the reported buffer size from the last GetUserNameA call and pass
4207 * a length that is one less than the required value. */
4208 SetLastError(0xdeadbeef);
4209 memcpy(buffer
, filler
, sizeof(filler
));
4211 ret
= GetUserNameA(buffer
, &buffer_len
);
4212 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
4213 ok(!memcmp(buffer
, filler
, sizeof(filler
)), "Output buffer was untouched\n");
4214 ok(buffer_len
== required_len
, "Outputted buffer length was %u\n", buffer_len
);
4215 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4218 static void test_GetUserNameW(void)
4220 WCHAR buffer
[UNLEN
+ 1], filler
[UNLEN
+ 1];
4221 DWORD required_len
, buffer_len
;
4224 /* Test crashes on Windows. */
4227 SetLastError(0xdeadbeef);
4228 GetUserNameW(NULL
, NULL
);
4231 SetLastError(0xdeadbeef);
4233 ret
= GetUserNameW(NULL
, &required_len
);
4234 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
4235 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
4236 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4238 SetLastError(0xdeadbeef);
4240 ret
= GetUserNameW(NULL
, &required_len
);
4241 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
4242 ok(required_len
!= 0 && required_len
!= 1, "Outputted buffer length was %u\n", required_len
);
4243 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4245 /* Tests crash on Windows. */
4248 SetLastError(0xdeadbeef);
4249 required_len
= UNLEN
+ 1;
4250 GetUserNameW(NULL
, &required_len
);
4252 SetLastError(0xdeadbeef);
4253 GetUserNameW(buffer
, NULL
);
4256 memset(filler
, 'x', sizeof(filler
));
4258 SetLastError(0xdeadbeef);
4259 memcpy(buffer
, filler
, sizeof(filler
));
4261 ret
= GetUserNameW(buffer
, &required_len
);
4262 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
4263 ok(!memcmp(buffer
, filler
, sizeof(filler
)), "Output buffer was altered\n");
4264 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
4265 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4267 SetLastError(0xdeadbeef);
4268 memcpy(buffer
, filler
, sizeof(filler
));
4269 buffer_len
= required_len
;
4270 ret
= GetUserNameW(buffer
, &buffer_len
);
4271 ok(ret
== TRUE
, "GetUserNameW returned %d, last error %u\n", ret
, GetLastError());
4272 ok(memcmp(buffer
, filler
, sizeof(filler
)) != 0, "Output buffer was untouched\n");
4273 ok(buffer_len
== required_len
, "Outputted buffer length was %u\n", buffer_len
);
4275 /* GetUserNameW on XP and newer writes a truncated portion of the username string to the buffer. */
4276 SetLastError(0xdeadbeef);
4277 memcpy(buffer
, filler
, sizeof(filler
));
4279 ret
= GetUserNameW(buffer
, &buffer_len
);
4280 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
4281 ok(!memcmp(buffer
, filler
, sizeof(filler
)) ||
4282 broken(memcmp(buffer
, filler
, sizeof(filler
)) != 0), /* XP+ */
4283 "Output buffer was altered\n");
4284 ok(buffer_len
== required_len
, "Outputted buffer length was %u\n", buffer_len
);
4285 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4288 static void test_CreateRestrictedToken(void)
4290 HANDLE process_token
, token
, r_token
;
4291 PTOKEN_GROUPS token_groups
, groups2
;
4292 SID_AND_ATTRIBUTES sattr
;
4293 SECURITY_IMPERSONATION_LEVEL level
;
4300 if (!pCreateRestrictedToken
)
4302 win_skip("CreateRestrictedToken is not available\n");
4306 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &process_token
);
4307 ok(ret
, "got error %d\n", GetLastError());
4309 ret
= DuplicateTokenEx(process_token
, TOKEN_DUPLICATE
|TOKEN_ADJUST_GROUPS
|TOKEN_QUERY
,
4310 NULL
, SecurityImpersonation
, TokenImpersonation
, &token
);
4311 ok(ret
, "got error %d\n", GetLastError());
4314 ret
= GetTokenInformation(token
, TokenGroups
, NULL
, 0, &size
);
4315 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
4316 "got %d with error %d\n", ret
, GetLastError());
4317 token_groups
= HeapAlloc(GetProcessHeap(), 0, size
);
4318 ret
= GetTokenInformation(token
, TokenGroups
, token_groups
, size
, &size
);
4319 ok(ret
, "got error %d\n", GetLastError());
4321 for (i
= 0; i
< token_groups
->GroupCount
; i
++)
4323 if (token_groups
->Groups
[i
].Attributes
& SE_GROUP_ENABLED
)
4327 if (i
== token_groups
->GroupCount
)
4329 HeapFree(GetProcessHeap(), 0, token_groups
);
4331 skip("User not a member of any group\n");
4336 ret
= pCheckTokenMembership(token
, token_groups
->Groups
[i
].Sid
, &is_member
);
4337 ok(ret
, "got error %d\n", GetLastError());
4338 ok(is_member
, "not a member\n");
4340 /* disable a SID in new token */
4341 sattr
.Sid
= token_groups
->Groups
[i
].Sid
;
4342 sattr
.Attributes
= 0;
4344 ret
= pCreateRestrictedToken(token
, 0, 1, &sattr
, 0, NULL
, 0, NULL
, &r_token
);
4345 ok(ret
, "got error %d\n", GetLastError());
4349 /* check if a SID is enabled */
4351 ret
= pCheckTokenMembership(r_token
, token_groups
->Groups
[i
].Sid
, &is_member
);
4352 ok(ret
, "got error %d\n", GetLastError());
4353 todo_wine
ok(!is_member
, "not a member\n");
4355 ret
= GetTokenInformation(r_token
, TokenGroups
, NULL
, 0, &size
);
4356 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "got %d with error %d\n",
4357 ret
, GetLastError());
4358 groups2
= HeapAlloc(GetProcessHeap(), 0, size
);
4359 ret
= GetTokenInformation(r_token
, TokenGroups
, groups2
, size
, &size
);
4360 ok(ret
, "got error %d\n", GetLastError());
4362 for (j
= 0; j
< groups2
->GroupCount
; j
++)
4364 if (EqualSid(groups2
->Groups
[j
].Sid
, token_groups
->Groups
[i
].Sid
))
4368 todo_wine
ok(groups2
->Groups
[j
].Attributes
& SE_GROUP_USE_FOR_DENY_ONLY
,
4369 "got wrong attributes\n");
4370 todo_wine
ok((groups2
->Groups
[j
].Attributes
& SE_GROUP_ENABLED
) == 0,
4371 "got wrong attributes\n");
4373 HeapFree(GetProcessHeap(), 0, groups2
);
4375 size
= sizeof(type
);
4376 ret
= GetTokenInformation(r_token
, TokenType
, &type
, size
, &size
);
4377 ok(ret
, "got error %d\n", GetLastError());
4378 ok(type
== TokenImpersonation
, "got type %u\n", type
);
4380 size
= sizeof(level
);
4381 ret
= GetTokenInformation(r_token
, TokenImpersonationLevel
, &level
, size
, &size
);
4382 ok(ret
, "got error %d\n", GetLastError());
4383 ok(level
== SecurityImpersonation
, "got level %u\n", type
);
4386 HeapFree(GetProcessHeap(), 0, token_groups
);
4387 CloseHandle(r_token
);
4389 CloseHandle(process_token
);
4392 static void validate_default_security_descriptor(SECURITY_DESCRIPTOR
*sd
)
4394 BOOL ret
, present
, defaulted
;
4398 ret
= IsValidSecurityDescriptor(sd
);
4399 ok(ret
, "security descriptor is not valid\n");
4403 acl
= (void *)0xdeadbeef;
4404 SetLastError(0xdeadbeef);
4405 ret
= GetSecurityDescriptorDacl(sd
, &present
, &acl
, &defaulted
);
4406 ok(ret
, "GetSecurityDescriptorDacl error %d\n", GetLastError());
4408 ok(present
== 1, "acl is not present\n");
4410 ok(acl
!= (void *)0xdeadbeef && acl
!= NULL
, "acl pointer is not set\n");
4411 ok(defaulted
== 0, "defaulted is set to TRUE\n");
4414 sid
= (void *)0xdeadbeef;
4415 SetLastError(0xdeadbeef);
4416 ret
= GetSecurityDescriptorOwner(sd
, &sid
, &defaulted
);
4417 ok(ret
, "GetSecurityDescriptorOwner error %d\n", GetLastError());
4419 ok(sid
!= (void *)0xdeadbeef && sid
!= NULL
, "sid pointer is not set\n");
4420 ok(defaulted
== 0, "defaulted is set to TRUE\n");
4423 sid
= (void *)0xdeadbeef;
4424 SetLastError(0xdeadbeef);
4425 ret
= GetSecurityDescriptorGroup(sd
, &sid
, &defaulted
);
4426 ok(ret
, "GetSecurityDescriptorGroup error %d\n", GetLastError());
4428 ok(sid
!= (void *)0xdeadbeef && sid
!= NULL
, "sid pointer is not set\n");
4429 ok(defaulted
== 0, "defaulted is set to TRUE\n");
4432 static void test_default_handle_security(HANDLE token
, HANDLE handle
, GENERIC_MAPPING
*mapping
)
4434 DWORD ret
, length
, needed
, granted
, priv_set_len
;
4436 PRIVILEGE_SET priv_set
;
4437 SECURITY_DESCRIPTOR
*sd
;
4439 needed
= 0xdeadbeef;
4440 SetLastError(0xdeadbeef);
4441 ret
= GetKernelObjectSecurity(handle
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
4443 ok(!ret
, "GetKernelObjectSecurity should fail\n");
4444 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
4445 ok(needed
!= 0xdeadbeef, "GetKernelObjectSecurity should return required buffer length\n");
4448 sd
= HeapAlloc(GetProcessHeap(), 0, length
);
4450 needed
= 0xdeadbeef;
4451 SetLastError(0xdeadbeef);
4452 ret
= GetKernelObjectSecurity(handle
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
4453 sd
, length
, &needed
);
4454 ok(ret
, "GetKernelObjectSecurity error %d\n", GetLastError());
4455 ok(needed
== length
|| needed
== 0 /* file, pipe */, "GetKernelObjectSecurity should return %u instead of %u\n", length
, needed
);
4457 validate_default_security_descriptor(sd
);
4459 priv_set_len
= sizeof(priv_set
);
4460 granted
= 0xdeadbeef;
4461 status
= 0xdeadbeef;
4462 SetLastError(0xdeadbeef);
4463 ret
= AccessCheck(sd
, token
, MAXIMUM_ALLOWED
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4465 ok(ret
, "AccessCheck error %d\n", GetLastError());
4466 ok(status
== 1, "expected 1, got %d\n", status
);
4467 ok(granted
== mapping
->GenericAll
, "expected all access %#x, got %#x\n", mapping
->GenericAll
, granted
);
4469 priv_set_len
= sizeof(priv_set
);
4470 granted
= 0xdeadbeef;
4471 status
= 0xdeadbeef;
4472 SetLastError(0xdeadbeef);
4473 ret
= AccessCheck(sd
, token
, 0, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4475 ok(ret
, "AccessCheck error %d\n", GetLastError());
4476 ok(status
== 0 || broken(status
== 1) /* NT4 */, "expected 0, got %d\n", status
);
4477 ok(granted
== 0 || broken(granted
== mapping
->GenericRead
) /* NT4 */, "expected 0, got %#x\n", granted
);
4479 priv_set_len
= sizeof(priv_set
);
4480 granted
= 0xdeadbeef;
4481 status
= 0xdeadbeef;
4482 SetLastError(0xdeadbeef);
4483 ret
= AccessCheck(sd
, token
, ACCESS_SYSTEM_SECURITY
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4485 ok(ret
, "AccessCheck error %d\n", GetLastError());
4486 ok(status
== 0, "expected 0, got %d\n", status
);
4487 ok(granted
== 0, "expected 0, got %#x\n", granted
);
4489 priv_set_len
= sizeof(priv_set
);
4490 granted
= 0xdeadbeef;
4491 status
= 0xdeadbeef;
4492 SetLastError(0xdeadbeef);
4493 ret
= AccessCheck(sd
, token
, mapping
->GenericRead
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4495 ok(ret
, "AccessCheck error %d\n", GetLastError());
4496 ok(status
== 1, "expected 1, got %d\n", status
);
4497 ok(granted
== mapping
->GenericRead
, "expected read access %#x, got %#x\n", mapping
->GenericRead
, granted
);
4499 priv_set_len
= sizeof(priv_set
);
4500 granted
= 0xdeadbeef;
4501 status
= 0xdeadbeef;
4502 SetLastError(0xdeadbeef);
4503 ret
= AccessCheck(sd
, token
, mapping
->GenericWrite
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4505 ok(ret
, "AccessCheck error %d\n", GetLastError());
4506 ok(status
== 1, "expected 1, got %d\n", status
);
4507 ok(granted
== mapping
->GenericWrite
, "expected write access %#x, got %#x\n", mapping
->GenericWrite
, granted
);
4509 priv_set_len
= sizeof(priv_set
);
4510 granted
= 0xdeadbeef;
4511 status
= 0xdeadbeef;
4512 SetLastError(0xdeadbeef);
4513 ret
= AccessCheck(sd
, token
, mapping
->GenericExecute
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4515 ok(ret
, "AccessCheck error %d\n", GetLastError());
4516 ok(status
== 1, "expected 1, got %d\n", status
);
4517 ok(granted
== mapping
->GenericExecute
, "expected execute access %#x, got %#x\n", mapping
->GenericExecute
, granted
);
4519 HeapFree(GetProcessHeap(), 0, sd
);
4522 static ACCESS_MASK
get_obj_access(HANDLE obj
)
4524 OBJECT_BASIC_INFORMATION info
;
4527 if (!pNtQueryObject
) return 0;
4529 status
= pNtQueryObject(obj
, ObjectBasicInformation
, &info
, sizeof(info
), NULL
);
4530 ok(!status
, "NtQueryObject error %#x\n", status
);
4532 return info
.GrantedAccess
;
4535 static void test_mutex_security(HANDLE token
)
4537 DWORD ret
, i
, access
;
4539 GENERIC_MAPPING mapping
= { STANDARD_RIGHTS_READ
| MUTANT_QUERY_STATE
| SYNCHRONIZE
,
4540 STANDARD_RIGHTS_WRITE
| MUTEX_MODIFY_STATE
| SYNCHRONIZE
,
4541 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
4542 STANDARD_RIGHTS_ALL
| MUTEX_ALL_ACCESS
};
4545 int generic
, mapped
;
4549 { GENERIC_READ
, STANDARD_RIGHTS_READ
| MUTANT_QUERY_STATE
},
4550 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
},
4551 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
4552 { GENERIC_ALL
, STANDARD_RIGHTS_ALL
| MUTANT_QUERY_STATE
}
4555 SetLastError(0xdeadbeef);
4556 mutex
= OpenMutexA(0, FALSE
, "WineTestMutex");
4557 ok(!mutex
, "mutex should not exist\n");
4558 ok(GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %u\n", GetLastError());
4560 SetLastError(0xdeadbeef);
4561 mutex
= CreateMutexA(NULL
, FALSE
, "WineTestMutex");
4562 ok(mutex
!= 0, "CreateMutex error %d\n", GetLastError());
4564 access
= get_obj_access(mutex
);
4565 ok(access
== MUTANT_ALL_ACCESS
, "expected MUTANT_ALL_ACCESS, got %#x\n", access
);
4567 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4569 SetLastError( 0xdeadbeef );
4570 ret
= DuplicateHandle(GetCurrentProcess(), mutex
, GetCurrentProcess(), &dup
,
4571 map
[i
].generic
, FALSE
, 0);
4572 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4574 access
= get_obj_access(dup
);
4575 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4579 SetLastError(0xdeadbeef);
4580 dup
= OpenMutexA(0, FALSE
, "WineTestMutex");
4582 ok(!dup
, "OpenMutex should fail\n");
4584 ok(GetLastError() == ERROR_ACCESS_DENIED
, "wrong error %u\n", GetLastError());
4587 test_default_handle_security(token
, mutex
, &mapping
);
4589 CloseHandle (mutex
);
4592 static void test_event_security(HANDLE token
)
4594 DWORD ret
, i
, access
;
4596 GENERIC_MAPPING mapping
= { STANDARD_RIGHTS_READ
| EVENT_QUERY_STATE
| SYNCHRONIZE
,
4597 STANDARD_RIGHTS_WRITE
| EVENT_MODIFY_STATE
| SYNCHRONIZE
,
4598 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
4599 STANDARD_RIGHTS_ALL
| EVENT_ALL_ACCESS
};
4602 int generic
, mapped
;
4606 { GENERIC_READ
, STANDARD_RIGHTS_READ
| EVENT_QUERY_STATE
},
4607 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| EVENT_MODIFY_STATE
},
4608 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
4609 { GENERIC_ALL
, STANDARD_RIGHTS_ALL
| EVENT_QUERY_STATE
| EVENT_MODIFY_STATE
}
4612 SetLastError(0xdeadbeef);
4613 event
= OpenEventA(0, FALSE
, "WineTestEvent");
4614 ok(!event
, "event should not exist\n");
4615 ok(GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %u\n", GetLastError());
4617 SetLastError(0xdeadbeef);
4618 event
= CreateEventA(NULL
, FALSE
, FALSE
, "WineTestEvent");
4619 ok(event
!= 0, "CreateEvent error %d\n", GetLastError());
4621 access
= get_obj_access(event
);
4622 ok(access
== EVENT_ALL_ACCESS
, "expected EVENT_ALL_ACCESS, got %#x\n", access
);
4624 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4626 SetLastError( 0xdeadbeef );
4627 ret
= DuplicateHandle(GetCurrentProcess(), event
, GetCurrentProcess(), &dup
,
4628 map
[i
].generic
, FALSE
, 0);
4629 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4631 access
= get_obj_access(dup
);
4632 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4636 SetLastError(0xdeadbeef);
4637 dup
= OpenEventA(0, FALSE
, "WineTestEvent");
4639 ok(!dup
, "OpenEvent should fail\n");
4641 ok(GetLastError() == ERROR_ACCESS_DENIED
, "wrong error %u\n", GetLastError());
4644 test_default_handle_security(token
, event
, &mapping
);
4649 static void test_semaphore_security(HANDLE token
)
4651 DWORD ret
, i
, access
;
4653 GENERIC_MAPPING mapping
= { STANDARD_RIGHTS_READ
| SEMAPHORE_QUERY_STATE
,
4654 STANDARD_RIGHTS_WRITE
| SEMAPHORE_MODIFY_STATE
,
4655 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
4656 STANDARD_RIGHTS_ALL
| SEMAPHORE_ALL_ACCESS
};
4659 int generic
, mapped
;
4663 { GENERIC_READ
, STANDARD_RIGHTS_READ
| SEMAPHORE_QUERY_STATE
},
4664 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| SEMAPHORE_MODIFY_STATE
},
4665 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
4666 { GENERIC_ALL
, STANDARD_RIGHTS_ALL
| SEMAPHORE_QUERY_STATE
| SEMAPHORE_MODIFY_STATE
}
4669 SetLastError(0xdeadbeef);
4670 sem
= OpenSemaphoreA(0, FALSE
, "WineTestSemaphore");
4671 ok(!sem
, "semaphore should not exist\n");
4672 ok(GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %u\n", GetLastError());
4674 SetLastError(0xdeadbeef);
4675 sem
= CreateSemaphoreA(NULL
, 0, 10, "WineTestSemaphore");
4676 ok(sem
!= 0, "CreateSemaphore error %d\n", GetLastError());
4678 access
= get_obj_access(sem
);
4679 ok(access
== SEMAPHORE_ALL_ACCESS
, "expected SEMAPHORE_ALL_ACCESS, got %#x\n", access
);
4681 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4683 SetLastError( 0xdeadbeef );
4684 ret
= DuplicateHandle(GetCurrentProcess(), sem
, GetCurrentProcess(), &dup
,
4685 map
[i
].generic
, FALSE
, 0);
4686 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4688 access
= get_obj_access(dup
);
4689 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4694 test_default_handle_security(token
, sem
, &mapping
);
4699 #define WINE_TEST_PIPE "\\\\.\\pipe\\WineTestPipe"
4700 static void test_named_pipe_security(HANDLE token
)
4702 DWORD ret
, i
, access
;
4703 HANDLE pipe
, file
, dup
;
4704 GENERIC_MAPPING mapping
= { FILE_GENERIC_READ
,
4706 FILE_GENERIC_EXECUTE
,
4707 STANDARD_RIGHTS_ALL
| FILE_ALL_ACCESS
};
4710 int todo
, generic
, mapped
;
4714 { 1, GENERIC_READ
, FILE_GENERIC_READ
},
4715 { 1, GENERIC_WRITE
, FILE_GENERIC_WRITE
},
4716 { 1, GENERIC_EXECUTE
, FILE_GENERIC_EXECUTE
},
4717 { 1, GENERIC_ALL
, STANDARD_RIGHTS_ALL
| FILE_ALL_ACCESS
}
4723 } creation_access
[] =
4725 { PIPE_ACCESS_INBOUND
, FILE_GENERIC_READ
},
4726 { PIPE_ACCESS_OUTBOUND
, FILE_GENERIC_WRITE
},
4727 { PIPE_ACCESS_DUPLEX
, FILE_GENERIC_READ
|FILE_GENERIC_WRITE
},
4728 { PIPE_ACCESS_INBOUND
|WRITE_DAC
, FILE_GENERIC_READ
|WRITE_DAC
},
4729 { PIPE_ACCESS_INBOUND
|WRITE_OWNER
, FILE_GENERIC_READ
|WRITE_OWNER
}
4730 /* ACCESS_SYSTEM_SECURITY is also valid, but will fail with ERROR_PRIVILEGE_NOT_HELD */
4733 /* Test the different security access options for pipes */
4734 for (i
= 0; i
< sizeof(creation_access
)/sizeof(creation_access
[0]); i
++)
4736 SetLastError(0xdeadbeef);
4737 pipe
= CreateNamedPipeA(WINE_TEST_PIPE
, creation_access
[i
].open_mode
,
4738 PIPE_TYPE_BYTE
| PIPE_NOWAIT
, PIPE_UNLIMITED_INSTANCES
, 0, 0,
4739 NMPWAIT_USE_DEFAULT_WAIT
, NULL
);
4740 ok(pipe
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe(0x%x) error %d\n",
4741 creation_access
[i
].open_mode
, GetLastError());
4742 access
= get_obj_access(pipe
);
4743 ok(access
== creation_access
[i
].access
,
4744 "CreateNamedPipeA(0x%x) pipe expected access 0x%x (got 0x%x)\n",
4745 creation_access
[i
].open_mode
, creation_access
[i
].access
, access
);
4749 SetLastError(0xdeadbeef);
4750 pipe
= CreateNamedPipeA(WINE_TEST_PIPE
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_FIRST_PIPE_INSTANCE
,
4751 PIPE_TYPE_BYTE
| PIPE_NOWAIT
, PIPE_UNLIMITED_INSTANCES
,
4752 0, 0, NMPWAIT_USE_DEFAULT_WAIT
, NULL
);
4753 ok(pipe
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe error %d\n", GetLastError());
4755 test_default_handle_security(token
, pipe
, &mapping
);
4757 SetLastError(0xdeadbeef);
4758 file
= CreateFileA(WINE_TEST_PIPE
, FILE_ALL_ACCESS
, 0, NULL
, OPEN_EXISTING
, 0, 0);
4759 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4761 access
= get_obj_access(file
);
4762 ok(access
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", access
);
4764 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4766 SetLastError( 0xdeadbeef );
4767 ret
= DuplicateHandle(GetCurrentProcess(), file
, GetCurrentProcess(), &dup
,
4768 map
[i
].generic
, FALSE
, 0);
4769 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4771 access
= get_obj_access(dup
);
4772 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4780 SetLastError(0xdeadbeef);
4781 file
= CreateFileA("\\\\.\\pipe\\", FILE_ALL_ACCESS
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, 0);
4782 ok(file
!= INVALID_HANDLE_VALUE
|| broken(file
== INVALID_HANDLE_VALUE
) /* before Vista */, "CreateFile error %d\n", GetLastError());
4784 if (file
!= INVALID_HANDLE_VALUE
)
4786 access
= get_obj_access(file
);
4787 ok(access
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", access
);
4789 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4791 SetLastError( 0xdeadbeef );
4792 ret
= DuplicateHandle(GetCurrentProcess(), file
, GetCurrentProcess(), &dup
,
4793 map
[i
].generic
, FALSE
, 0);
4794 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4796 access
= get_obj_access(dup
);
4799 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4801 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4810 static void test_file_security(HANDLE token
)
4812 DWORD ret
, i
, access
, bytes
;
4816 int generic
, mapped
;
4820 { GENERIC_READ
, FILE_GENERIC_READ
},
4821 { GENERIC_WRITE
, FILE_GENERIC_WRITE
},
4822 { GENERIC_EXECUTE
, FILE_GENERIC_EXECUTE
},
4823 { GENERIC_ALL
, STANDARD_RIGHTS_ALL
| FILE_ALL_ACCESS
}
4825 char temp_path
[MAX_PATH
];
4826 char file_name
[MAX_PATH
];
4829 GetTempPathA(MAX_PATH
, temp_path
);
4830 GetTempFileNameA(temp_path
, "tmp", 0, file_name
);
4833 SetLastError(0xdeadbeef);
4834 file
= CreateFileA(file_name
, GENERIC_ALL
, 0, NULL
, CREATE_ALWAYS
, 0, NULL
);
4835 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4837 access
= get_obj_access(file
);
4838 ok(access
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", access
);
4840 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4842 SetLastError( 0xdeadbeef );
4843 ret
= DuplicateHandle(GetCurrentProcess(), file
, GetCurrentProcess(), &dup
,
4844 map
[i
].generic
, FALSE
, 0);
4845 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4847 access
= get_obj_access(dup
);
4848 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4855 SetLastError(0xdeadbeef);
4856 file
= CreateFileA(file_name
, 0, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
4857 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4859 access
= get_obj_access(file
);
4861 ok(access
== (FILE_READ_ATTRIBUTES
| SYNCHRONIZE
), "expected FILE_READ_ATTRIBUTES | SYNCHRONIZE, got %#x\n", access
);
4864 SetLastError(0xdeadbeef);
4865 ret
= ReadFile(file
, buf
, sizeof(buf
), &bytes
, NULL
);
4866 ok(!ret
, "ReadFile should fail\n");
4867 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
4868 ok(bytes
== 0, "expected 0, got %u\n", bytes
);
4872 SetLastError(0xdeadbeef);
4873 file
= CreateFileA(file_name
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
4874 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4876 access
= get_obj_access(file
);
4878 ok(access
== (FILE_GENERIC_WRITE
| FILE_READ_ATTRIBUTES
), "expected FILE_GENERIC_WRITE | FILE_READ_ATTRIBUTES, got %#x\n", access
);
4881 SetLastError(0xdeadbeef);
4882 ret
= ReadFile(file
, buf
, sizeof(buf
), &bytes
, NULL
);
4883 ok(!ret
, "ReadFile should fail\n");
4884 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
4885 ok(bytes
== 0, "expected 0, got %u\n", bytes
);
4888 DeleteFileA(file_name
);
4891 SetLastError(0xdeadbeef);
4892 file
= CreateFileA(temp_path
, GENERIC_ALL
, 0, NULL
, OPEN_EXISTING
, FILE_FLAG_BACKUP_SEMANTICS
, 0);
4893 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4895 access
= get_obj_access(file
);
4896 ok(access
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", access
);
4898 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4900 SetLastError( 0xdeadbeef );
4901 ret
= DuplicateHandle(GetCurrentProcess(), file
, GetCurrentProcess(), &dup
,
4902 map
[i
].generic
, FALSE
, 0);
4903 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4905 access
= get_obj_access(dup
);
4906 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4913 SetLastError(0xdeadbeef);
4914 file
= CreateFileA(temp_path
, 0, 0, NULL
, OPEN_EXISTING
, FILE_FLAG_BACKUP_SEMANTICS
, 0);
4915 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4917 access
= get_obj_access(file
);
4919 ok(access
== (FILE_READ_ATTRIBUTES
| SYNCHRONIZE
), "expected FILE_READ_ATTRIBUTES | SYNCHRONIZE, got %#x\n", access
);
4923 SetLastError(0xdeadbeef);
4924 file
= CreateFileA(temp_path
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, FILE_FLAG_BACKUP_SEMANTICS
, 0);
4925 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4927 access
= get_obj_access(file
);
4929 ok(access
== (FILE_GENERIC_WRITE
| FILE_READ_ATTRIBUTES
), "expected FILE_GENERIC_WRITE | FILE_READ_ATTRIBUTES, got %#x\n", access
);
4934 static void test_filemap_security(void)
4936 char temp_path
[MAX_PATH
];
4937 char file_name
[MAX_PATH
];
4938 DWORD ret
, i
, access
;
4939 HANDLE file
, mapping
, dup
;
4942 int generic
, mapped
;
4946 { GENERIC_READ
, STANDARD_RIGHTS_READ
| SECTION_QUERY
| SECTION_MAP_READ
},
4947 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| SECTION_MAP_WRITE
},
4948 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SECTION_MAP_EXECUTE
},
4949 { GENERIC_ALL
, STANDARD_RIGHTS_REQUIRED
| SECTION_ALL_ACCESS
}
4957 { PAGE_NOACCESS
, 0 },
4958 { PAGE_READONLY
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
},
4959 { PAGE_READWRITE
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
},
4960 { PAGE_WRITECOPY
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
},
4961 { PAGE_EXECUTE
, 0 },
4962 { PAGE_EXECUTE_READ
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_EXECUTE
},
4963 { PAGE_EXECUTE_READWRITE
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
| SECTION_MAP_EXECUTE
},
4964 { PAGE_EXECUTE_WRITECOPY
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_EXECUTE
}
4967 GetTempPathA(MAX_PATH
, temp_path
);
4968 GetTempFileNameA(temp_path
, "tmp", 0, file_name
);
4970 SetLastError(0xdeadbeef);
4971 file
= CreateFileA(file_name
, GENERIC_READ
|GENERIC_WRITE
|GENERIC_EXECUTE
, 0, NULL
, CREATE_ALWAYS
, 0, 0);
4972 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4973 SetFilePointer(file
, 4096, NULL
, FILE_BEGIN
);
4976 for (i
= 0; i
< sizeof(prot_map
)/sizeof(prot_map
[0]); i
++)
4978 SetLastError(0xdeadbeef);
4979 mapping
= CreateFileMappingW(file
, NULL
, prot_map
[i
].prot
, 0, 4096, NULL
);
4980 if (prot_map
[i
].mapped
)
4984 /* NT4 and win2k don't support EXEC on file mappings */
4985 if (prot_map
[i
].prot
== PAGE_EXECUTE_READ
|| prot_map
[i
].prot
== PAGE_EXECUTE_READWRITE
|| prot_map
[i
].prot
== PAGE_EXECUTE_WRITECOPY
)
4987 win_skip("CreateFileMapping doesn't support PAGE_EXECUTE protection\n");
4991 ok(mapping
!= 0, "CreateFileMapping(%04x) error %d\n", prot_map
[i
].prot
, GetLastError());
4995 ok(!mapping
, "CreateFileMapping(%04x) should fail\n", prot_map
[i
].prot
);
4996 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
5000 access
= get_obj_access(mapping
);
5001 ok(access
== prot_map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, prot_map
[i
].mapped
, access
);
5003 CloseHandle(mapping
);
5006 SetLastError(0xdeadbeef);
5007 mapping
= CreateFileMappingW(file
, NULL
, PAGE_EXECUTE_READWRITE
, 0, 4096, NULL
);
5010 /* NT4 and win2k don't support EXEC on file mappings */
5011 win_skip("CreateFileMapping doesn't support PAGE_EXECUTE protection\n");
5013 DeleteFileA(file_name
);
5016 ok(mapping
!= 0, "CreateFileMapping error %d\n", GetLastError());
5018 access
= get_obj_access(mapping
);
5019 ok(access
== (STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
| SECTION_MAP_EXECUTE
),
5020 "expected STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ | SECTION_MAP_WRITE | SECTION_MAP_EXECUTE, got %#x\n", access
);
5022 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5024 SetLastError( 0xdeadbeef );
5025 ret
= DuplicateHandle(GetCurrentProcess(), mapping
, GetCurrentProcess(), &dup
,
5026 map
[i
].generic
, FALSE
, 0);
5027 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5029 access
= get_obj_access(dup
);
5030 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5035 CloseHandle(mapping
);
5037 DeleteFileA(file_name
);
5040 static void test_thread_security(void)
5042 DWORD ret
, i
, access
;
5046 int generic
, mapped
;
5050 { GENERIC_READ
, STANDARD_RIGHTS_READ
| THREAD_QUERY_INFORMATION
| THREAD_GET_CONTEXT
},
5051 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| THREAD_SET_INFORMATION
| THREAD_SET_CONTEXT
| THREAD_TERMINATE
| THREAD_SUSPEND_RESUME
| 0x4 },
5052 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
5053 { GENERIC_ALL
, THREAD_ALL_ACCESS_NT4
}
5056 SetLastError(0xdeadbeef);
5057 thread
= CreateThread(NULL
, 0, (void *)0xdeadbeef, NULL
, CREATE_SUSPENDED
, &ret
);
5058 ok(thread
!= 0, "CreateThread error %d\n", GetLastError());
5060 access
= get_obj_access(thread
);
5061 ok(access
== THREAD_ALL_ACCESS_NT4
|| access
== THREAD_ALL_ACCESS_VISTA
, "expected THREAD_ALL_ACCESS, got %#x\n", access
);
5063 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5065 SetLastError( 0xdeadbeef );
5066 ret
= DuplicateHandle(GetCurrentProcess(), thread
, GetCurrentProcess(), &dup
,
5067 map
[i
].generic
, FALSE
, 0);
5068 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5070 access
= get_obj_access(dup
);
5071 switch (map
[i
].generic
)
5074 case GENERIC_EXECUTE
:
5075 ok(access
== map
[i
].mapped
|| access
== (map
[i
].mapped
| THREAD_QUERY_LIMITED_INFORMATION
) /* Vista+ */,
5076 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5080 ok(access
== map
[i
].mapped
|| access
== (map
[i
].mapped
| THREAD_SET_LIMITED_INFORMATION
) /* Vista+ */,
5081 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5084 ok(access
== map
[i
].mapped
|| access
== THREAD_ALL_ACCESS_VISTA
,
5085 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5088 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5095 TerminateThread(thread
, 0);
5096 CloseHandle(thread
);
5099 static void test_process_access(void)
5101 DWORD ret
, i
, access
;
5102 HANDLE process
, dup
;
5104 PROCESS_INFORMATION pi
;
5105 char cmdline
[] = "winver.exe";
5108 int generic
, mapped
;
5112 { GENERIC_READ
, STANDARD_RIGHTS_READ
| PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
},
5113 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| PROCESS_SET_QUOTA
| PROCESS_SET_INFORMATION
| PROCESS_SUSPEND_RESUME
|
5114 PROCESS_VM_WRITE
| PROCESS_DUP_HANDLE
| PROCESS_CREATE_PROCESS
| PROCESS_CREATE_THREAD
| PROCESS_VM_OPERATION
},
5115 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
5116 { GENERIC_ALL
, PROCESS_ALL_ACCESS_NT4
}
5119 memset(&sti
, 0, sizeof(sti
));
5120 sti
.cb
= sizeof(sti
);
5121 SetLastError(0xdeadbeef);
5122 ret
= CreateProcessA(NULL
, cmdline
, NULL
, NULL
, FALSE
, CREATE_SUSPENDED
, NULL
, NULL
, &sti
, &pi
);
5123 ok(ret
, "CreateProcess() error %d\n", GetLastError());
5125 CloseHandle(pi
.hThread
);
5126 process
= pi
.hProcess
;
5128 access
= get_obj_access(process
);
5129 ok(access
== PROCESS_ALL_ACCESS_NT4
|| access
== PROCESS_ALL_ACCESS_VISTA
, "expected PROCESS_ALL_ACCESS, got %#x\n", access
);
5131 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5133 SetLastError( 0xdeadbeef );
5134 ret
= DuplicateHandle(GetCurrentProcess(), process
, GetCurrentProcess(), &dup
,
5135 map
[i
].generic
, FALSE
, 0);
5136 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5138 access
= get_obj_access(dup
);
5139 switch (map
[i
].generic
)
5142 ok(access
== map
[i
].mapped
|| access
== (map
[i
].mapped
| PROCESS_QUERY_LIMITED_INFORMATION
) /* Vista+ */,
5143 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5146 ok(access
== map
[i
].mapped
|| access
== (map
[i
].mapped
| PROCESS_TERMINATE
) /* before Vista */,
5147 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5149 case GENERIC_EXECUTE
:
5150 ok(access
== map
[i
].mapped
|| access
== (map
[i
].mapped
| PROCESS_QUERY_LIMITED_INFORMATION
| PROCESS_TERMINATE
) /* Vista+ */,
5151 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5154 ok(access
== map
[i
].mapped
|| access
== PROCESS_ALL_ACCESS_VISTA
,
5155 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5158 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5165 TerminateProcess(process
, 0);
5166 CloseHandle(process
);
5169 static BOOL
validate_impersonation_token(HANDLE token
, DWORD
*token_type
)
5173 SECURITY_IMPERSONATION_LEVEL sil
;
5177 SetLastError(0xdeadbeef);
5178 ret
= GetTokenInformation(token
, TokenType
, &type
, sizeof(type
), &needed
);
5179 ok(ret
, "GetTokenInformation error %d\n", GetLastError());
5180 ok(needed
== sizeof(type
), "GetTokenInformation should return required buffer length\n");
5181 ok(type
== TokenPrimary
|| type
== TokenImpersonation
, "expected TokenPrimary or TokenImpersonation, got %d\n", type
);
5184 if (type
!= TokenImpersonation
) return FALSE
;
5187 SetLastError(0xdeadbeef);
5188 ret
= GetTokenInformation(token
, TokenImpersonationLevel
, &sil
, sizeof(sil
), &needed
);
5189 ok(ret
, "GetTokenInformation error %d\n", GetLastError());
5190 ok(needed
== sizeof(sil
), "GetTokenInformation should return required buffer length\n");
5191 ok(sil
== SecurityImpersonation
, "expected SecurityImpersonation, got %d\n", sil
);
5193 needed
= 0xdeadbeef;
5194 SetLastError(0xdeadbeef);
5195 ret
= GetTokenInformation(token
, TokenDefaultDacl
, NULL
, 0, &needed
);
5196 ok(!ret
, "GetTokenInformation should fail\n");
5197 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
5198 ok(needed
!= 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
5199 ok(needed
> sizeof(TOKEN_DEFAULT_DACL
), "GetTokenInformation returned empty default DACL\n");
5201 needed
= 0xdeadbeef;
5202 SetLastError(0xdeadbeef);
5203 ret
= GetTokenInformation(token
, TokenOwner
, NULL
, 0, &needed
);
5204 ok(!ret
, "GetTokenInformation should fail\n");
5205 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
5206 ok(needed
!= 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
5207 ok(needed
> sizeof(TOKEN_OWNER
), "GetTokenInformation returned empty token owner\n");
5209 needed
= 0xdeadbeef;
5210 SetLastError(0xdeadbeef);
5211 ret
= GetTokenInformation(token
, TokenPrimaryGroup
, NULL
, 0, &needed
);
5212 ok(!ret
, "GetTokenInformation should fail\n");
5213 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
5214 ok(needed
!= 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
5215 ok(needed
> sizeof(TOKEN_PRIMARY_GROUP
), "GetTokenInformation returned empty token primary group\n");
5220 static void test_kernel_objects_security(void)
5222 HANDLE token
, process_token
;
5223 DWORD ret
, token_type
;
5225 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
| TOKEN_QUERY
, &process_token
);
5226 ok(ret
, "OpenProcessToken error %d\n", GetLastError());
5228 ret
= validate_impersonation_token(process_token
, &token_type
);
5229 ok(token_type
== TokenPrimary
, "expected TokenPrimary, got %d\n", token_type
);
5230 ok(!ret
, "access token should not be an impersonation token\n");
5232 ret
= DuplicateToken(process_token
, SecurityImpersonation
, &token
);
5233 ok(ret
, "DuplicateToken error %d\n", GetLastError());
5235 ret
= validate_impersonation_token(token
, &token_type
);
5236 ok(ret
, "access token should be a valid impersonation token\n");
5237 ok(token_type
== TokenImpersonation
, "expected TokenImpersonation, got %d\n", token_type
);
5239 test_mutex_security(token
);
5240 test_event_security(token
);
5241 test_named_pipe_security(token
);
5242 test_semaphore_security(token
);
5243 test_file_security(token
);
5244 test_filemap_security();
5245 test_thread_security();
5246 test_process_access();
5247 /* FIXME: test other kernel object types */
5249 CloseHandle(process_token
);
5253 static void test_TokenIntegrityLevel(void)
5255 TOKEN_MANDATORY_LABEL
*tml
;
5256 BYTE buffer
[64]; /* using max. 28 byte in win7 x64 */
5260 char *sidname
= NULL
;
5261 static SID medium_level
= {SID_REVISION
, 1, {SECURITY_MANDATORY_LABEL_AUTHORITY
},
5262 {SECURITY_MANDATORY_HIGH_RID
}};
5263 static SID high_level
= {SID_REVISION
, 1, {SECURITY_MANDATORY_LABEL_AUTHORITY
},
5264 {SECURITY_MANDATORY_MEDIUM_RID
}};
5266 SetLastError(0xdeadbeef);
5267 res
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
, &token
);
5268 ok(res
, "got %d with %d (expected TRUE)\n", res
, GetLastError());
5270 SetLastError(0xdeadbeef);
5271 res
= GetTokenInformation(token
, TokenIntegrityLevel
, buffer
, sizeof(buffer
), &size
);
5273 /* not supported before Vista */
5274 if (!res
&& ((GetLastError() == ERROR_INVALID_PARAMETER
) || GetLastError() == ERROR_INVALID_FUNCTION
))
5276 win_skip("TokenIntegrityLevel not supported\n");
5281 ok(res
, "got %u with %u (expected TRUE)\n", res
, GetLastError());
5288 tml
= (TOKEN_MANDATORY_LABEL
*) buffer
;
5289 ok(tml
->Label
.Attributes
== (SE_GROUP_INTEGRITY
| SE_GROUP_INTEGRITY_ENABLED
),
5290 "got 0x%x (expected 0x%x)\n", tml
->Label
.Attributes
, (SE_GROUP_INTEGRITY
| SE_GROUP_INTEGRITY_ENABLED
));
5292 SetLastError(0xdeadbeef);
5293 res
= pConvertSidToStringSidA(tml
->Label
.Sid
, &sidname
);
5294 ok(res
, "got %u and %u\n", res
, GetLastError());
5296 ok(EqualSid(tml
->Label
.Sid
, &medium_level
) || EqualSid(tml
->Label
.Sid
, &high_level
),
5297 "got %s (expected 'S-1-16-8192' or 'S-1-16-12288')\n", sidname
);
5303 static void test_default_dacl_owner_sid(void)
5306 BOOL ret
, defaulted
, present
, found
;
5308 SECURITY_DESCRIPTOR
*sd
;
5309 SECURITY_ATTRIBUTES sa
;
5312 ACCESS_ALLOWED_ACE
*ace
;
5314 sd
= HeapAlloc( GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
5315 ret
= InitializeSecurityDescriptor( sd
, SECURITY_DESCRIPTOR_REVISION
);
5316 ok( ret
, "error %u\n", GetLastError() );
5318 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
5319 sa
.lpSecurityDescriptor
= sd
;
5320 sa
.bInheritHandle
= FALSE
;
5321 handle
= CreateEventA( &sa
, TRUE
, TRUE
, "test_event" );
5322 ok( handle
!= NULL
, "error %u\n", GetLastError() );
5325 ret
= GetKernelObjectSecurity( handle
, OWNER_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, NULL
, 0, &size
);
5326 ok( !ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "error %u\n", GetLastError() );
5328 sd
= HeapAlloc( GetProcessHeap(), 0, size
);
5329 ret
= GetKernelObjectSecurity( handle
, OWNER_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, sd
, size
, &size
);
5330 ok( ret
, "error %u\n", GetLastError() );
5332 owner
= (void *)0xdeadbeef;
5334 ret
= GetSecurityDescriptorOwner( sd
, &owner
, &defaulted
);
5335 ok( ret
, "error %u\n", GetLastError() );
5336 ok( owner
!= (void *)0xdeadbeef, "owner not set\n" );
5337 todo_wine
ok( !defaulted
, "owner defaulted\n" );
5339 dacl
= (void *)0xdeadbeef;
5342 ret
= GetSecurityDescriptorDacl( sd
, &present
, &dacl
, &defaulted
);
5343 ok( ret
, "error %u\n", GetLastError() );
5344 ok( present
, "dacl not present\n" );
5345 ok( dacl
!= (void *)0xdeadbeef, "dacl not set\n" );
5346 todo_wine
ok( !defaulted
, "dacl defaulted\n" );
5350 while (pGetAce( dacl
, index
++, (void **)&ace
))
5352 if (EqualSid( &ace
->SidStart
, owner
)) found
= TRUE
;
5354 ok( found
, "owner sid not found in dacl\n" );
5356 HeapFree( GetProcessHeap(), 0, sa
.lpSecurityDescriptor
);
5357 HeapFree( GetProcessHeap(), 0, sd
);
5358 CloseHandle( handle
);
5361 static void test_AdjustTokenPrivileges(void)
5363 TOKEN_PRIVILEGES tp
, prev
;
5369 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
, &token
))
5372 if (!LookupPrivilegeValueA(NULL
, SE_BACKUP_NAME
, &luid
))
5378 tp
.PrivilegeCount
= 1;
5379 tp
.Privileges
[0].Luid
= luid
;
5380 tp
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
5383 ret
= AdjustTokenPrivileges(token
, FALSE
, &tp
, sizeof(TOKEN_PRIVILEGES
), NULL
, &len
);
5384 ok(ret
, "got %d\n", ret
);
5385 ok(len
== 0xdeadbeef, "got length %d\n", len
);
5388 tp
.PrivilegeCount
= 1;
5389 tp
.Privileges
[0].Luid
= luid
;
5390 tp
.Privileges
[0].Attributes
= 0;
5391 AdjustTokenPrivileges(token
, FALSE
, &tp
, sizeof(TOKEN_PRIVILEGES
), &prev
, NULL
);
5396 START_TEST(security
)
5403 test_process_security_child();
5406 test_kernel_objects_security();
5410 test_CreateWellKnownSid();
5411 test_FileSecurity();
5414 test_LookupAccountSid();
5415 test_LookupAccountName();
5416 test_security_descriptor();
5417 test_process_security();
5418 test_impersonation_level();
5419 test_SetEntriesInAclW();
5420 test_SetEntriesInAclA();
5421 test_CreateDirectoryA();
5422 test_GetNamedSecurityInfoA();
5423 test_ConvertStringSecurityDescriptor();
5424 test_ConvertSecurityDescriptorToString();
5425 test_PrivateObjectSecurity();
5427 test_GetSecurityInfo();
5428 test_GetSidSubAuthority();
5429 test_CheckTokenMembership();
5431 test_GetUserNameA();
5432 test_GetUserNameW();
5433 test_CreateRestrictedToken();
5434 test_TokenIntegrityLevel();
5435 test_default_dacl_owner_sid();
5436 test_AdjustTokenPrivileges();