2 * Unit tests for security functions
4 * Copyright (c) 2004 Mike McCormack
5 * Copyright (c) 2011 Dmitry Timoshkov
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #define WIN32_NO_STATUS
36 #include "wine/test.h"
38 #ifndef PROCESS_QUERY_LIMITED_INFORMATION
39 #define PROCESS_QUERY_LIMITED_INFORMATION 0x1000
42 /* PROCESS_ALL_ACCESS in Vista+ PSDKs is incompatible with older Windows versions */
43 #define PROCESS_ALL_ACCESS_NT4 (PROCESS_ALL_ACCESS & ~0xf000)
44 #define PROCESS_ALL_ACCESS_VISTA (PROCESS_ALL_ACCESS | 0xf000)
46 #ifndef EVENT_QUERY_STATE
47 #define EVENT_QUERY_STATE 0x0001
50 #ifndef SEMAPHORE_QUERY_STATE
51 #define SEMAPHORE_QUERY_STATE 0x0001
54 #ifndef THREAD_SET_LIMITED_INFORMATION
55 #define THREAD_SET_LIMITED_INFORMATION 0x0400
56 #define THREAD_QUERY_LIMITED_INFORMATION 0x0800
59 #define THREAD_ALL_ACCESS_NT4 (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x3ff)
60 #define THREAD_ALL_ACCESS_VISTA (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0xffff)
62 /* copied from Wine winternl.h - not included in the Windows SDK */
63 typedef enum _OBJECT_INFORMATION_CLASS
{
64 ObjectBasicInformation
,
65 ObjectNameInformation
,
66 ObjectTypeInformation
,
69 } OBJECT_INFORMATION_CLASS
, *POBJECT_INFORMATION_CLASS
;
71 typedef struct _OBJECT_BASIC_INFORMATION
{
73 ACCESS_MASK GrantedAccess
;
77 ULONG NonPagedPoolUsage
;
79 ULONG NameInformationLength
;
80 ULONG TypeInformationLength
;
81 ULONG SecurityDescriptorLength
;
82 LARGE_INTEGER CreateTime
;
83 } OBJECT_BASIC_INFORMATION
, *POBJECT_BASIC_INFORMATION
;
85 #define expect_eq(expr, value, type, format) { type ret_ = expr; ok((value) == ret_, #expr " expected " format " got " format "\n", (value), (ret_)); }
87 static BOOL (WINAPI
*pAddAccessAllowedAceEx
)(PACL
, DWORD
, DWORD
, DWORD
, PSID
);
88 static BOOL (WINAPI
*pAddAccessDeniedAceEx
)(PACL
, DWORD
, DWORD
, DWORD
, PSID
);
89 static BOOL (WINAPI
*pAddAuditAccessAceEx
)(PACL
, DWORD
, DWORD
, DWORD
, PSID
, BOOL
, BOOL
);
90 static VOID (WINAPI
*pBuildTrusteeWithSidA
)( PTRUSTEEA pTrustee
, PSID pSid
);
91 static VOID (WINAPI
*pBuildTrusteeWithNameA
)( PTRUSTEEA pTrustee
, LPSTR pName
);
92 static VOID (WINAPI
*pBuildTrusteeWithObjectsAndNameA
)( PTRUSTEEA pTrustee
,
93 POBJECTS_AND_NAME_A pObjName
,
94 SE_OBJECT_TYPE ObjectType
,
96 LPSTR InheritedObjectTypeName
,
98 static VOID (WINAPI
*pBuildTrusteeWithObjectsAndSidA
)( PTRUSTEEA pTrustee
,
99 POBJECTS_AND_SID pObjSid
,
101 GUID
* pInheritedObjectGuid
,
103 static LPSTR (WINAPI
*pGetTrusteeNameA
)( PTRUSTEEA pTrustee
);
104 static BOOL (WINAPI
*pMakeSelfRelativeSD
)( PSECURITY_DESCRIPTOR
, PSECURITY_DESCRIPTOR
, LPDWORD
);
105 static BOOL (WINAPI
*pConvertSidToStringSidA
)( PSID pSid
, LPSTR
*str
);
106 static BOOL (WINAPI
*pConvertStringSidToSidA
)( LPCSTR str
, PSID pSid
);
107 static BOOL (WINAPI
*pCheckTokenMembership
)(HANDLE
, PSID
, PBOOL
);
108 static BOOL (WINAPI
*pConvertStringSecurityDescriptorToSecurityDescriptorA
)(LPCSTR
, DWORD
,
109 PSECURITY_DESCRIPTOR
*, PULONG
);
110 static BOOL (WINAPI
*pConvertStringSecurityDescriptorToSecurityDescriptorW
)(LPCWSTR
, DWORD
,
111 PSECURITY_DESCRIPTOR
*, PULONG
);
112 static BOOL (WINAPI
*pConvertSecurityDescriptorToStringSecurityDescriptorA
)(PSECURITY_DESCRIPTOR
, DWORD
,
113 SECURITY_INFORMATION
, LPSTR
*, PULONG
);
114 static BOOL (WINAPI
*pGetFileSecurityA
)(LPCSTR
, SECURITY_INFORMATION
,
115 PSECURITY_DESCRIPTOR
, DWORD
, LPDWORD
);
116 static BOOL (WINAPI
*pSetFileSecurityA
)(LPCSTR
, SECURITY_INFORMATION
,
117 PSECURITY_DESCRIPTOR
);
118 static DWORD (WINAPI
*pGetNamedSecurityInfoA
)(LPSTR
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
119 PSID
*, PSID
*, PACL
*, PACL
*,
120 PSECURITY_DESCRIPTOR
*);
121 static DWORD (WINAPI
*pSetNamedSecurityInfoA
)(LPSTR
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
122 PSID
, PSID
, PACL
, PACL
);
123 static PDWORD (WINAPI
*pGetSidSubAuthority
)(PSID
, DWORD
);
124 static PUCHAR (WINAPI
*pGetSidSubAuthorityCount
)(PSID
);
125 static BOOL (WINAPI
*pIsValidSid
)(PSID
);
126 static DWORD (WINAPI
*pRtlAdjustPrivilege
)(ULONG
,BOOLEAN
,BOOLEAN
,PBOOLEAN
);
127 static BOOL (WINAPI
*pCreateWellKnownSid
)(WELL_KNOWN_SID_TYPE
,PSID
,PSID
,DWORD
*);
128 static BOOL (WINAPI
*pDuplicateTokenEx
)(HANDLE
,DWORD
,LPSECURITY_ATTRIBUTES
,
129 SECURITY_IMPERSONATION_LEVEL
,TOKEN_TYPE
,PHANDLE
);
131 static NTSTATUS (WINAPI
*pLsaQueryInformationPolicy
)(LSA_HANDLE
,POLICY_INFORMATION_CLASS
,PVOID
*);
132 static NTSTATUS (WINAPI
*pLsaClose
)(LSA_HANDLE
);
133 static NTSTATUS (WINAPI
*pLsaFreeMemory
)(PVOID
);
134 static NTSTATUS (WINAPI
*pLsaOpenPolicy
)(PLSA_UNICODE_STRING
,PLSA_OBJECT_ATTRIBUTES
,ACCESS_MASK
,PLSA_HANDLE
);
135 static NTSTATUS (WINAPI
*pNtQueryObject
)(HANDLE
,OBJECT_INFORMATION_CLASS
,PVOID
,ULONG
,PULONG
);
136 static DWORD (WINAPI
*pSetEntriesInAclW
)(ULONG
, PEXPLICIT_ACCESSW
, PACL
, PACL
*);
137 static DWORD (WINAPI
*pSetEntriesInAclA
)(ULONG
, PEXPLICIT_ACCESSA
, PACL
, PACL
*);
138 static BOOL (WINAPI
*pSetSecurityDescriptorControl
)(PSECURITY_DESCRIPTOR
, SECURITY_DESCRIPTOR_CONTROL
,
139 SECURITY_DESCRIPTOR_CONTROL
);
140 static DWORD (WINAPI
*pGetSecurityInfo
)(HANDLE
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
141 PSID
*, PSID
*, PACL
*, PACL
*, PSECURITY_DESCRIPTOR
*);
142 static DWORD (WINAPI
*pSetSecurityInfo
)(HANDLE
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
143 PSID
, PSID
, PACL
, PACL
);
144 static NTSTATUS (WINAPI
*pNtAccessCheck
)(PSECURITY_DESCRIPTOR
, HANDLE
, ACCESS_MASK
, PGENERIC_MAPPING
,
145 PPRIVILEGE_SET
, PULONG
, PULONG
, NTSTATUS
*);
146 static BOOL (WINAPI
*pCreateRestrictedToken
)(HANDLE
, DWORD
, DWORD
, PSID_AND_ATTRIBUTES
, DWORD
,
147 PLUID_AND_ATTRIBUTES
, DWORD
, PSID_AND_ATTRIBUTES
, PHANDLE
);
148 static BOOL (WINAPI
*pGetAclInformation
)(PACL
,LPVOID
,DWORD
,ACL_INFORMATION_CLASS
);
149 static BOOL (WINAPI
*pGetAce
)(PACL
,DWORD
,LPVOID
*);
153 static char** myARGV
;
165 SID_IDENTIFIER_AUTHORITY auth
;
169 static void init(void)
173 hntdll
= GetModuleHandleA("ntdll.dll");
174 pNtQueryObject
= (void *)GetProcAddress( hntdll
, "NtQueryObject" );
175 pNtAccessCheck
= (void *)GetProcAddress( hntdll
, "NtAccessCheck" );
177 hmod
= GetModuleHandleA("advapi32.dll");
178 pAddAccessAllowedAceEx
= (void *)GetProcAddress(hmod
, "AddAccessAllowedAceEx");
179 pAddAccessDeniedAceEx
= (void *)GetProcAddress(hmod
, "AddAccessDeniedAceEx");
180 pAddAuditAccessAceEx
= (void *)GetProcAddress(hmod
, "AddAuditAccessAceEx");
181 pCheckTokenMembership
= (void *)GetProcAddress(hmod
, "CheckTokenMembership");
182 pConvertStringSecurityDescriptorToSecurityDescriptorA
=
183 (void *)GetProcAddress(hmod
, "ConvertStringSecurityDescriptorToSecurityDescriptorA" );
184 pConvertStringSecurityDescriptorToSecurityDescriptorW
=
185 (void *)GetProcAddress(hmod
, "ConvertStringSecurityDescriptorToSecurityDescriptorW" );
186 pConvertSecurityDescriptorToStringSecurityDescriptorA
=
187 (void *)GetProcAddress(hmod
, "ConvertSecurityDescriptorToStringSecurityDescriptorA" );
188 pGetFileSecurityA
= (void *)GetProcAddress(hmod
, "GetFileSecurityA" );
189 pSetFileSecurityA
= (void *)GetProcAddress(hmod
, "SetFileSecurityA" );
190 pCreateWellKnownSid
= (void *)GetProcAddress( hmod
, "CreateWellKnownSid" );
191 pGetNamedSecurityInfoA
= (void *)GetProcAddress(hmod
, "GetNamedSecurityInfoA");
192 pSetNamedSecurityInfoA
= (void *)GetProcAddress(hmod
, "SetNamedSecurityInfoA");
193 pGetSidSubAuthority
= (void *)GetProcAddress(hmod
, "GetSidSubAuthority");
194 pGetSidSubAuthorityCount
= (void *)GetProcAddress(hmod
, "GetSidSubAuthorityCount");
195 pIsValidSid
= (void *)GetProcAddress(hmod
, "IsValidSid");
196 pMakeSelfRelativeSD
= (void *)GetProcAddress(hmod
, "MakeSelfRelativeSD");
197 pSetEntriesInAclW
= (void *)GetProcAddress(hmod
, "SetEntriesInAclW");
198 pSetEntriesInAclA
= (void *)GetProcAddress(hmod
, "SetEntriesInAclA");
199 pSetSecurityDescriptorControl
= (void *)GetProcAddress(hmod
, "SetSecurityDescriptorControl");
200 pGetSecurityInfo
= (void *)GetProcAddress(hmod
, "GetSecurityInfo");
201 pSetSecurityInfo
= (void *)GetProcAddress(hmod
, "SetSecurityInfo");
202 pCreateRestrictedToken
= (void *)GetProcAddress(hmod
, "CreateRestrictedToken");
203 pConvertSidToStringSidA
= (void *)GetProcAddress(hmod
, "ConvertSidToStringSidA");
204 pConvertStringSidToSidA
= (void *)GetProcAddress(hmod
, "ConvertStringSidToSidA");
205 pGetAclInformation
= (void *)GetProcAddress(hmod
, "GetAclInformation");
206 pGetAce
= (void *)GetProcAddress(hmod
, "GetAce");
208 myARGC
= winetest_get_mainargs( &myARGV
);
211 static SECURITY_DESCRIPTOR
* test_get_security_descriptor(HANDLE handle
, int line
)
213 /* use HeapFree(GetProcessHeap(), 0, sd); when done */
214 DWORD ret
, length
, needed
;
215 SECURITY_DESCRIPTOR
*sd
;
218 SetLastError(0xdeadbeef);
219 ret
= GetKernelObjectSecurity(handle
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
221 ok_(__FILE__
, line
)(!ret
, "GetKernelObjectSecurity should fail\n");
222 ok_(__FILE__
, line
)(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
223 ok_(__FILE__
, line
)(needed
!= 0xdeadbeef, "GetKernelObjectSecurity should return required buffer length\n");
226 sd
= HeapAlloc(GetProcessHeap(), 0, length
);
229 SetLastError(0xdeadbeef);
230 ret
= GetKernelObjectSecurity(handle
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
231 sd
, length
, &needed
);
232 ok_(__FILE__
, line
)(ret
, "GetKernelObjectSecurity error %d\n", GetLastError());
233 ok_(__FILE__
, line
)(needed
== length
|| needed
== 0 /* file, pipe */, "GetKernelObjectSecurity should return %u instead of %u\n", length
, needed
);
237 static void test_sid(void)
239 struct sidRef refs
[] = {
240 { { {0x00,0x00,0x33,0x44,0x55,0x66} }, "S-1-860116326-1" },
241 { { {0x00,0x00,0x01,0x02,0x03,0x04} }, "S-1-16909060-1" },
242 { { {0x00,0x00,0x00,0x01,0x02,0x03} }, "S-1-66051-1" },
243 { { {0x00,0x00,0x00,0x00,0x01,0x02} }, "S-1-258-1" },
244 { { {0x00,0x00,0x00,0x00,0x00,0x02} }, "S-1-2-1" },
245 { { {0x00,0x00,0x00,0x00,0x00,0x0c} }, "S-1-12-1" },
247 struct strsid_entry strsid_table
[] = {
248 {"AO", STRSID_OK
}, {"RU", STRSID_OK
}, {"AN", STRSID_OK
}, {"AU", STRSID_OK
},
249 {"BA", STRSID_OK
}, {"BG", STRSID_OK
}, {"BO", STRSID_OK
}, {"BU", STRSID_OK
},
250 {"CA", STRSID_OPT
}, {"CG", STRSID_OK
}, {"CO", STRSID_OK
}, {"DA", STRSID_OPT
},
251 {"DC", STRSID_OPT
}, {"DD", STRSID_OPT
}, {"DG", STRSID_OPT
}, {"DU", STRSID_OPT
},
252 {"EA", STRSID_OPT
}, {"ED", STRSID_OK
}, {"WD", STRSID_OK
}, {"PA", STRSID_OPT
},
253 {"IU", STRSID_OK
}, {"LA", STRSID_OK
}, {"LG", STRSID_OK
}, {"LS", STRSID_OK
},
254 {"SY", STRSID_OK
}, {"NU", STRSID_OK
}, {"NO", STRSID_OK
}, {"NS", STRSID_OK
},
255 {"PO", STRSID_OK
}, {"PS", STRSID_OK
}, {"PU", STRSID_OK
}, {"RS", STRSID_OPT
},
256 {"RD", STRSID_OK
}, {"RE", STRSID_OK
}, {"RC", STRSID_OK
}, {"SA", STRSID_OPT
},
257 {"SO", STRSID_OK
}, {"SU", STRSID_OK
}};
259 const char noSubAuthStr
[] = "S-1-5";
266 if( !pConvertSidToStringSidA
|| !pConvertStringSidToSidA
)
268 win_skip("ConvertSidToStringSidA or ConvertStringSidToSidA not available\n");
272 r
= pConvertStringSidToSidA( NULL
, NULL
);
273 ok( !r
, "expected failure with NULL parameters\n" );
274 if( GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
276 ok( GetLastError() == ERROR_INVALID_PARAMETER
,
277 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
280 r
= pConvertStringSidToSidA( refs
[0].refStr
, NULL
);
281 ok( !r
&& GetLastError() == ERROR_INVALID_PARAMETER
,
282 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
285 r
= pConvertStringSidToSidA( NULL
, &str
);
286 ok( !r
&& GetLastError() == ERROR_INVALID_PARAMETER
,
287 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
290 r
= pConvertStringSidToSidA( noSubAuthStr
, &psid
);
292 "expected failure with no sub authorities\n" );
293 ok( GetLastError() == ERROR_INVALID_SID
,
294 "expected GetLastError() is ERROR_INVALID_SID, got %d\n",
297 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid
), "ConvertStringSidToSidA failed\n");
299 ok(pisid
->SubAuthorityCount
== 4, "Invalid sub authority count - expected 4, got %d\n", pisid
->SubAuthorityCount
);
300 ok(pisid
->SubAuthority
[0] == 21, "Invalid subauthority 0 - expected 21, got %d\n", pisid
->SubAuthority
[0]);
301 ok(pisid
->SubAuthority
[3] == 4576, "Invalid subauthority 0 - expected 4576, got %d\n", pisid
->SubAuthority
[3]);
305 for( i
= 0; i
< sizeof(refs
) / sizeof(refs
[0]); i
++ )
307 r
= AllocateAndInitializeSid( &refs
[i
].auth
, 1,1,0,0,0,0,0,0,0,
309 ok( r
, "failed to allocate sid\n" );
310 r
= pConvertSidToStringSidA( psid
, &str
);
311 ok( r
, "failed to convert sid\n" );
314 ok( !strcmp( str
, refs
[i
].refStr
),
315 "incorrect sid, expected %s, got %s\n", refs
[i
].refStr
, str
);
321 r
= pConvertStringSidToSidA( refs
[i
].refStr
, &psid
);
322 ok( r
, "failed to parse sid string\n" );
325 !memcmp( pisid
->IdentifierAuthority
.Value
, refs
[i
].auth
.Value
,
326 sizeof(refs
[i
].auth
) ),
327 "string sid %s didn't parse to expected value\n"
328 "(got 0x%04x%08x, expected 0x%04x%08x)\n",
330 MAKEWORD( pisid
->IdentifierAuthority
.Value
[1],
331 pisid
->IdentifierAuthority
.Value
[0] ),
332 MAKELONG( MAKEWORD( pisid
->IdentifierAuthority
.Value
[5],
333 pisid
->IdentifierAuthority
.Value
[4] ),
334 MAKEWORD( pisid
->IdentifierAuthority
.Value
[3],
335 pisid
->IdentifierAuthority
.Value
[2] ) ),
336 MAKEWORD( refs
[i
].auth
.Value
[1], refs
[i
].auth
.Value
[0] ),
337 MAKELONG( MAKEWORD( refs
[i
].auth
.Value
[5], refs
[i
].auth
.Value
[4] ),
338 MAKEWORD( refs
[i
].auth
.Value
[3], refs
[i
].auth
.Value
[2] ) ) );
343 /* string constant format not supported before XP */
344 r
= pConvertStringSidToSidA(strsid_table
[0].str
, &psid
);
347 win_skip("String constant format not supported\n");
352 for(i
= 0; i
< sizeof(strsid_table
) / sizeof(strsid_table
[0]); i
++)
356 SetLastError(0xdeadbeef);
357 r
= pConvertStringSidToSidA(strsid_table
[i
].str
, &psid
);
359 if (!(strsid_table
[i
].flags
& STRSID_OPT
))
361 ok(r
, "%s: got %u\n", strsid_table
[i
].str
, GetLastError());
366 if ((winetest_debug
> 1) && (pConvertSidToStringSidA(psid
, &temp
)))
368 trace(" %s: %s\n", strsid_table
[i
].str
, temp
);
375 if (GetLastError() != ERROR_INVALID_SID
)
376 trace(" %s: couldn't be converted, returned %d\n", strsid_table
[i
].str
, GetLastError());
378 trace(" %s: couldn't be converted\n", strsid_table
[i
].str
);
383 static void test_trustee(void)
385 GUID ObjectType
= {0x12345678, 0x1234, 0x5678, {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}};
386 GUID InheritedObjectType
= {0x23456789, 0x2345, 0x6786, {0x2, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}};
388 OBJECTS_AND_NAME_A oan
;
392 char szObjectTypeName
[] = "ObjectTypeName";
393 char szInheritedObjectTypeName
[] = "InheritedObjectTypeName";
394 char szTrusteeName
[] = "szTrusteeName";
395 SID_IDENTIFIER_AUTHORITY auth
= { {0x11,0x22,0,0,0, 0} };
397 memset( &ZeroGuid
, 0x00, sizeof (ZeroGuid
) );
399 pBuildTrusteeWithSidA
= (void *)GetProcAddress( hmod
, "BuildTrusteeWithSidA" );
400 pBuildTrusteeWithNameA
= (void *)GetProcAddress( hmod
, "BuildTrusteeWithNameA" );
401 pBuildTrusteeWithObjectsAndNameA
= (void *)GetProcAddress (hmod
, "BuildTrusteeWithObjectsAndNameA" );
402 pBuildTrusteeWithObjectsAndSidA
= (void *)GetProcAddress (hmod
, "BuildTrusteeWithObjectsAndSidA" );
403 pGetTrusteeNameA
= (void *)GetProcAddress (hmod
, "GetTrusteeNameA" );
404 if( !pBuildTrusteeWithSidA
|| !pBuildTrusteeWithNameA
||
405 !pBuildTrusteeWithObjectsAndNameA
|| !pBuildTrusteeWithObjectsAndSidA
||
409 if ( ! AllocateAndInitializeSid( &auth
, 1, 42, 0,0,0,0,0,0,0,&psid
) )
411 trace( "failed to init SID\n" );
415 /* test BuildTrusteeWithSidA */
416 memset( &trustee
, 0xff, sizeof trustee
);
417 pBuildTrusteeWithSidA( &trustee
, psid
);
419 ok( trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
420 ok( trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
,
421 "MultipleTrusteeOperation wrong\n");
422 ok( trustee
.TrusteeForm
== TRUSTEE_IS_SID
, "TrusteeForm wrong\n");
423 ok( trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
424 ok( trustee
.ptstrName
== psid
, "ptstrName wrong\n" );
426 /* test BuildTrusteeWithObjectsAndSidA (test 1) */
427 memset( &trustee
, 0xff, sizeof trustee
);
428 memset( &oas
, 0xff, sizeof(oas
) );
429 pBuildTrusteeWithObjectsAndSidA(&trustee
, &oas
, &ObjectType
,
430 &InheritedObjectType
, psid
);
432 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
433 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
434 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_SID
, "TrusteeForm wrong\n");
435 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
436 ok(trustee
.ptstrName
== (LPSTR
)&oas
, "ptstrName wrong\n");
438 ok(oas
.ObjectsPresent
== (ACE_OBJECT_TYPE_PRESENT
| ACE_INHERITED_OBJECT_TYPE_PRESENT
), "ObjectsPresent wrong\n");
439 ok(!memcmp(&oas
.ObjectTypeGuid
, &ObjectType
, sizeof(GUID
)), "ObjectTypeGuid wrong\n");
440 ok(!memcmp(&oas
.InheritedObjectTypeGuid
, &InheritedObjectType
, sizeof(GUID
)), "InheritedObjectTypeGuid wrong\n");
441 ok(oas
.pSid
== psid
, "pSid wrong\n");
443 /* test GetTrusteeNameA */
444 ok(pGetTrusteeNameA(&trustee
) == (LPSTR
)&oas
, "GetTrusteeName returned wrong value\n");
446 /* test BuildTrusteeWithObjectsAndSidA (test 2) */
447 memset( &trustee
, 0xff, sizeof trustee
);
448 memset( &oas
, 0xff, sizeof(oas
) );
449 pBuildTrusteeWithObjectsAndSidA(&trustee
, &oas
, NULL
,
450 &InheritedObjectType
, psid
);
452 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
453 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
454 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_SID
, "TrusteeForm wrong\n");
455 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
456 ok(trustee
.ptstrName
== (LPSTR
)&oas
, "ptstrName wrong\n");
458 ok(oas
.ObjectsPresent
== ACE_INHERITED_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
459 ok(!memcmp(&oas
.ObjectTypeGuid
, &ZeroGuid
, sizeof(GUID
)), "ObjectTypeGuid wrong\n");
460 ok(!memcmp(&oas
.InheritedObjectTypeGuid
, &InheritedObjectType
, sizeof(GUID
)), "InheritedObjectTypeGuid wrong\n");
461 ok(oas
.pSid
== psid
, "pSid wrong\n");
465 /* test BuildTrusteeWithNameA */
466 memset( &trustee
, 0xff, sizeof trustee
);
467 pBuildTrusteeWithNameA( &trustee
, szTrusteeName
);
469 ok( trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
470 ok( trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
,
471 "MultipleTrusteeOperation wrong\n");
472 ok( trustee
.TrusteeForm
== TRUSTEE_IS_NAME
, "TrusteeForm wrong\n");
473 ok( trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
474 ok( trustee
.ptstrName
== szTrusteeName
, "ptstrName wrong\n" );
476 /* test BuildTrusteeWithObjectsAndNameA (test 1) */
477 memset( &trustee
, 0xff, sizeof trustee
);
478 memset( &oan
, 0xff, sizeof(oan
) );
479 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, szObjectTypeName
,
480 szInheritedObjectTypeName
, szTrusteeName
);
482 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
483 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
484 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
485 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
486 ok(trustee
.ptstrName
== (LPSTR
)&oan
, "ptstrName wrong\n");
488 ok(oan
.ObjectsPresent
== (ACE_OBJECT_TYPE_PRESENT
| ACE_INHERITED_OBJECT_TYPE_PRESENT
), "ObjectsPresent wrong\n");
489 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
490 ok(oan
.InheritedObjectTypeName
== szInheritedObjectTypeName
, "InheritedObjectTypeName wrong\n");
491 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
493 /* test GetTrusteeNameA */
494 ok(pGetTrusteeNameA(&trustee
) == (LPSTR
)&oan
, "GetTrusteeName returned wrong value\n");
496 /* test BuildTrusteeWithObjectsAndNameA (test 2) */
497 memset( &trustee
, 0xff, sizeof trustee
);
498 memset( &oan
, 0xff, sizeof(oan
) );
499 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, NULL
,
500 szInheritedObjectTypeName
, szTrusteeName
);
502 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
503 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
504 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
505 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
506 ok(trustee
.ptstrName
== (LPSTR
)&oan
, "ptstrName wrong\n");
508 ok(oan
.ObjectsPresent
== ACE_INHERITED_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
509 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
510 ok(oan
.InheritedObjectTypeName
== szInheritedObjectTypeName
, "InheritedObjectTypeName wrong\n");
511 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
513 /* test BuildTrusteeWithObjectsAndNameA (test 3) */
514 memset( &trustee
, 0xff, sizeof trustee
);
515 memset( &oan
, 0xff, sizeof(oan
) );
516 pBuildTrusteeWithObjectsAndNameA(&trustee
, &oan
, SE_KERNEL_OBJECT
, szObjectTypeName
,
517 NULL
, szTrusteeName
);
519 ok(trustee
.pMultipleTrustee
== NULL
, "pMultipleTrustee wrong\n");
520 ok(trustee
.MultipleTrusteeOperation
== NO_MULTIPLE_TRUSTEE
, "MultipleTrusteeOperation wrong\n");
521 ok(trustee
.TrusteeForm
== TRUSTEE_IS_OBJECTS_AND_NAME
, "TrusteeForm wrong\n");
522 ok(trustee
.TrusteeType
== TRUSTEE_IS_UNKNOWN
, "TrusteeType wrong\n");
523 ok(trustee
.ptstrName
== (LPSTR
)&oan
, "ptstrName wrong\n");
525 ok(oan
.ObjectsPresent
== ACE_OBJECT_TYPE_PRESENT
, "ObjectsPresent wrong\n");
526 ok(oan
.ObjectType
== SE_KERNEL_OBJECT
, "ObjectType wrong\n");
527 ok(oan
.InheritedObjectTypeName
== NULL
, "InheritedObjectTypeName wrong\n");
528 ok(oan
.ptstrName
== szTrusteeName
, "szTrusteeName wrong\n");
531 /* If the first isn't defined, assume none is */
532 #ifndef SE_MIN_WELL_KNOWN_PRIVILEGE
533 #define SE_MIN_WELL_KNOWN_PRIVILEGE 2L
534 #define SE_CREATE_TOKEN_PRIVILEGE 2L
535 #define SE_ASSIGNPRIMARYTOKEN_PRIVILEGE 3L
536 #define SE_LOCK_MEMORY_PRIVILEGE 4L
537 #define SE_INCREASE_QUOTA_PRIVILEGE 5L
538 #define SE_MACHINE_ACCOUNT_PRIVILEGE 6L
539 #define SE_TCB_PRIVILEGE 7L
540 #define SE_SECURITY_PRIVILEGE 8L
541 #define SE_TAKE_OWNERSHIP_PRIVILEGE 9L
542 #define SE_LOAD_DRIVER_PRIVILEGE 10L
543 #define SE_SYSTEM_PROFILE_PRIVILEGE 11L
544 #define SE_SYSTEMTIME_PRIVILEGE 12L
545 #define SE_PROF_SINGLE_PROCESS_PRIVILEGE 13L
546 #define SE_INC_BASE_PRIORITY_PRIVILEGE 14L
547 #define SE_CREATE_PAGEFILE_PRIVILEGE 15L
548 #define SE_CREATE_PERMANENT_PRIVILEGE 16L
549 #define SE_BACKUP_PRIVILEGE 17L
550 #define SE_RESTORE_PRIVILEGE 18L
551 #define SE_SHUTDOWN_PRIVILEGE 19L
552 #define SE_DEBUG_PRIVILEGE 20L
553 #define SE_AUDIT_PRIVILEGE 21L
554 #define SE_SYSTEM_ENVIRONMENT_PRIVILEGE 22L
555 #define SE_CHANGE_NOTIFY_PRIVILEGE 23L
556 #define SE_REMOTE_SHUTDOWN_PRIVILEGE 24L
557 #define SE_UNDOCK_PRIVILEGE 25L
558 #define SE_SYNC_AGENT_PRIVILEGE 26L
559 #define SE_ENABLE_DELEGATION_PRIVILEGE 27L
560 #define SE_MANAGE_VOLUME_PRIVILEGE 28L
561 #define SE_IMPERSONATE_PRIVILEGE 29L
562 #define SE_CREATE_GLOBAL_PRIVILEGE 30L
563 #define SE_MAX_WELL_KNOWN_PRIVILEGE SE_CREATE_GLOBAL_PRIVILEGE
564 #endif /* ndef SE_MIN_WELL_KNOWN_PRIVILEGE */
566 static void test_allocateLuid(void)
568 BOOL (WINAPI
*pAllocateLocallyUniqueId
)(PLUID
);
572 pAllocateLocallyUniqueId
= (void*)GetProcAddress(hmod
, "AllocateLocallyUniqueId");
573 if (!pAllocateLocallyUniqueId
) return;
575 ret
= pAllocateLocallyUniqueId(&luid1
);
576 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
580 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
581 ret
= pAllocateLocallyUniqueId(&luid2
);
583 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
584 ok(luid1
.LowPart
> SE_MAX_WELL_KNOWN_PRIVILEGE
|| luid1
.HighPart
!= 0,
585 "AllocateLocallyUniqueId returned a well-known LUID\n");
586 ok(luid1
.LowPart
!= luid2
.LowPart
|| luid1
.HighPart
!= luid2
.HighPart
,
587 "AllocateLocallyUniqueId returned non-unique LUIDs\n");
588 ret
= pAllocateLocallyUniqueId(NULL
);
589 ok( !ret
&& GetLastError() == ERROR_NOACCESS
,
590 "AllocateLocallyUniqueId(NULL) didn't return ERROR_NOACCESS: %d\n",
594 static void test_lookupPrivilegeName(void)
596 BOOL (WINAPI
*pLookupPrivilegeNameA
)(LPCSTR
, PLUID
, LPSTR
, LPDWORD
);
597 char buf
[MAX_PATH
]; /* arbitrary, seems long enough */
598 DWORD cchName
= sizeof(buf
);
599 LUID luid
= { 0, 0 };
603 /* check whether it's available first */
604 pLookupPrivilegeNameA
= (void*)GetProcAddress(hmod
, "LookupPrivilegeNameA");
605 if (!pLookupPrivilegeNameA
) return;
606 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
607 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
608 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
611 /* check with a short buffer */
613 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
614 ret
= pLookupPrivilegeNameA(NULL
, &luid
, NULL
, &cchName
);
615 ok( !ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
616 "LookupPrivilegeNameA didn't fail with ERROR_INSUFFICIENT_BUFFER: %d\n",
618 ok(cchName
== strlen("SeCreateTokenPrivilege") + 1,
619 "LookupPrivilegeNameA returned an incorrect required length for\n"
620 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName
,
621 lstrlenA("SeCreateTokenPrivilege") + 1);
622 /* check a known value and its returned length on success */
623 cchName
= sizeof(buf
);
624 ok(pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
) &&
625 cchName
== strlen("SeCreateTokenPrivilege"),
626 "LookupPrivilegeNameA returned an incorrect output length for\n"
627 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName
,
628 (int)strlen("SeCreateTokenPrivilege"));
629 /* check known values */
630 for (i
= SE_MIN_WELL_KNOWN_PRIVILEGE
; i
<= SE_MAX_WELL_KNOWN_PRIVILEGE
; i
++)
633 cchName
= sizeof(buf
);
634 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
635 ok( ret
|| GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
636 "LookupPrivilegeNameA(0.%d) failed: %d\n", i
, GetLastError());
638 /* check a bogus LUID */
639 luid
.LowPart
= 0xdeadbeef;
640 cchName
= sizeof(buf
);
641 ret
= pLookupPrivilegeNameA(NULL
, &luid
, buf
, &cchName
);
642 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
643 "LookupPrivilegeNameA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
645 /* check on a bogus system */
646 luid
.LowPart
= SE_CREATE_TOKEN_PRIVILEGE
;
647 cchName
= sizeof(buf
);
648 ret
= pLookupPrivilegeNameA("b0gu5.Nam3", &luid
, buf
, &cchName
);
649 ok( !ret
&& (GetLastError() == RPC_S_SERVER_UNAVAILABLE
||
650 GetLastError() == RPC_S_INVALID_NET_ADDR
) /* w2k8 */,
651 "LookupPrivilegeNameA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
661 static void test_lookupPrivilegeValue(void)
663 static const struct NameToLUID privs
[] = {
664 { "SeCreateTokenPrivilege", SE_CREATE_TOKEN_PRIVILEGE
},
665 { "SeAssignPrimaryTokenPrivilege", SE_ASSIGNPRIMARYTOKEN_PRIVILEGE
},
666 { "SeLockMemoryPrivilege", SE_LOCK_MEMORY_PRIVILEGE
},
667 { "SeIncreaseQuotaPrivilege", SE_INCREASE_QUOTA_PRIVILEGE
},
668 { "SeMachineAccountPrivilege", SE_MACHINE_ACCOUNT_PRIVILEGE
},
669 { "SeTcbPrivilege", SE_TCB_PRIVILEGE
},
670 { "SeSecurityPrivilege", SE_SECURITY_PRIVILEGE
},
671 { "SeTakeOwnershipPrivilege", SE_TAKE_OWNERSHIP_PRIVILEGE
},
672 { "SeLoadDriverPrivilege", SE_LOAD_DRIVER_PRIVILEGE
},
673 { "SeSystemProfilePrivilege", SE_SYSTEM_PROFILE_PRIVILEGE
},
674 { "SeSystemtimePrivilege", SE_SYSTEMTIME_PRIVILEGE
},
675 { "SeProfileSingleProcessPrivilege", SE_PROF_SINGLE_PROCESS_PRIVILEGE
},
676 { "SeIncreaseBasePriorityPrivilege", SE_INC_BASE_PRIORITY_PRIVILEGE
},
677 { "SeCreatePagefilePrivilege", SE_CREATE_PAGEFILE_PRIVILEGE
},
678 { "SeCreatePermanentPrivilege", SE_CREATE_PERMANENT_PRIVILEGE
},
679 { "SeBackupPrivilege", SE_BACKUP_PRIVILEGE
},
680 { "SeRestorePrivilege", SE_RESTORE_PRIVILEGE
},
681 { "SeShutdownPrivilege", SE_SHUTDOWN_PRIVILEGE
},
682 { "SeDebugPrivilege", SE_DEBUG_PRIVILEGE
},
683 { "SeAuditPrivilege", SE_AUDIT_PRIVILEGE
},
684 { "SeSystemEnvironmentPrivilege", SE_SYSTEM_ENVIRONMENT_PRIVILEGE
},
685 { "SeChangeNotifyPrivilege", SE_CHANGE_NOTIFY_PRIVILEGE
},
686 { "SeRemoteShutdownPrivilege", SE_REMOTE_SHUTDOWN_PRIVILEGE
},
687 { "SeUndockPrivilege", SE_UNDOCK_PRIVILEGE
},
688 { "SeSyncAgentPrivilege", SE_SYNC_AGENT_PRIVILEGE
},
689 { "SeEnableDelegationPrivilege", SE_ENABLE_DELEGATION_PRIVILEGE
},
690 { "SeManageVolumePrivilege", SE_MANAGE_VOLUME_PRIVILEGE
},
691 { "SeImpersonatePrivilege", SE_IMPERSONATE_PRIVILEGE
},
692 { "SeCreateGlobalPrivilege", SE_CREATE_GLOBAL_PRIVILEGE
},
694 BOOL (WINAPI
*pLookupPrivilegeValueA
)(LPCSTR
, LPCSTR
, PLUID
);
699 /* check whether it's available first */
700 pLookupPrivilegeValueA
= (void*)GetProcAddress(hmod
, "LookupPrivilegeValueA");
701 if (!pLookupPrivilegeValueA
) return;
702 ret
= pLookupPrivilegeValueA(NULL
, "SeCreateTokenPrivilege", &luid
);
703 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
706 /* check a bogus system name */
707 ret
= pLookupPrivilegeValueA("b0gu5.Nam3", "SeCreateTokenPrivilege", &luid
);
708 ok( !ret
&& (GetLastError() == RPC_S_SERVER_UNAVAILABLE
||
709 GetLastError() == RPC_S_INVALID_NET_ADDR
) /* w2k8 */,
710 "LookupPrivilegeValueA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
712 /* check a NULL string */
713 ret
= pLookupPrivilegeValueA(NULL
, 0, &luid
);
714 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
715 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
717 /* check a bogus privilege name */
718 ret
= pLookupPrivilegeValueA(NULL
, "SeBogusPrivilege", &luid
);
719 ok( !ret
&& GetLastError() == ERROR_NO_SUCH_PRIVILEGE
,
720 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
722 /* check case insensitive */
723 ret
= pLookupPrivilegeValueA(NULL
, "sEcREATEtOKENpRIVILEGE", &luid
);
725 "LookupPrivilegeValueA(NULL, sEcREATEtOKENpRIVILEGE, &luid) failed: %d\n",
727 for (i
= 0; i
< sizeof(privs
) / sizeof(privs
[0]); i
++)
729 /* Not all privileges are implemented on all Windows versions, so
730 * don't worry if the call fails
732 if (pLookupPrivilegeValueA(NULL
, privs
[i
].name
, &luid
))
734 ok(luid
.LowPart
== privs
[i
].lowPart
,
735 "LookupPrivilegeValueA returned an invalid LUID for %s\n",
741 static void test_luid(void)
744 test_lookupPrivilegeName();
745 test_lookupPrivilegeValue();
748 static void test_FileSecurity(void)
750 char wintmpdir
[MAX_PATH
];
751 char path
[MAX_PATH
];
752 char file
[MAX_PATH
];
754 DWORD sdSize
, retSize
, rc
, granted
, priv_set_len
;
755 PRIVILEGE_SET priv_set
;
758 GENERIC_MAPPING mapping
= { FILE_READ_DATA
, FILE_WRITE_DATA
, FILE_EXECUTE
, FILE_ALL_ACCESS
};
759 const SECURITY_INFORMATION request
= OWNER_SECURITY_INFORMATION
760 | GROUP_SECURITY_INFORMATION
761 | DACL_SECURITY_INFORMATION
;
763 if (!pGetFileSecurityA
) {
764 win_skip ("GetFileSecurity is not available\n");
768 if (!pSetFileSecurityA
) {
769 win_skip ("SetFileSecurity is not available\n");
773 if (!GetTempPathA (sizeof (wintmpdir
), wintmpdir
)) {
774 win_skip ("GetTempPathA failed\n");
778 /* Create a temporary directory and in it a temporary file */
779 strcat (strcpy (path
, wintmpdir
), "rary");
780 SetLastError(0xdeadbeef);
781 rc
= CreateDirectoryA (path
, NULL
);
782 ok (rc
|| GetLastError() == ERROR_ALREADY_EXISTS
, "CreateDirectoryA "
783 "failed for '%s' with %d\n", path
, GetLastError());
785 strcat (strcpy (file
, path
), "\\ess");
786 SetLastError(0xdeadbeef);
787 fh
= CreateFileA (file
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, NULL
);
788 ok (fh
!= INVALID_HANDLE_VALUE
, "CreateFileA "
789 "failed for '%s' with %d\n", file
, GetLastError());
792 /* For the temporary file ... */
794 /* Get size needed */
796 SetLastError(0xdeadbeef);
797 rc
= pGetFileSecurityA (file
, request
, NULL
, 0, &retSize
);
798 if (!rc
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)) {
799 win_skip("GetFileSecurityA is not implemented\n");
802 ok (!rc
, "GetFileSecurityA "
803 "was expected to fail for '%s'\n", file
);
804 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetFileSecurityA "
805 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
806 ok (retSize
> sizeof (SECURITY_DESCRIPTOR
), "GetFileSecurityA returned size %d\n", retSize
);
809 sd
= HeapAlloc (GetProcessHeap (), 0, sdSize
);
811 /* Get security descriptor for real */
813 SetLastError(0xdeadbeef);
814 rc
= pGetFileSecurityA (file
, request
, sd
, sdSize
, &retSize
);
815 ok (rc
, "GetFileSecurityA "
816 "was not expected to fail '%s': %d\n", file
, GetLastError());
817 ok (retSize
== sdSize
||
818 broken(retSize
== 0), /* NT4 */
819 "GetFileSecurityA returned size %d; expected %d\n", retSize
, sdSize
);
821 /* Use it to set security descriptor */
822 SetLastError(0xdeadbeef);
823 rc
= pSetFileSecurityA (file
, request
, sd
);
824 ok (rc
, "SetFileSecurityA "
825 "was not expected to fail '%s': %d\n", file
, GetLastError());
827 HeapFree (GetProcessHeap (), 0, sd
);
829 /* Repeat for the temporary directory ... */
831 /* Get size needed */
833 SetLastError(0xdeadbeef);
834 rc
= pGetFileSecurityA (path
, request
, NULL
, 0, &retSize
);
835 ok (!rc
, "GetFileSecurityA "
836 "was expected to fail for '%s'\n", path
);
837 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "GetFileSecurityA "
838 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
839 ok (retSize
> sizeof (SECURITY_DESCRIPTOR
), "GetFileSecurityA returned size %d\n", retSize
);
842 sd
= HeapAlloc (GetProcessHeap (), 0, sdSize
);
844 /* Get security descriptor for real */
846 SetLastError(0xdeadbeef);
847 rc
= pGetFileSecurityA (path
, request
, sd
, sdSize
, &retSize
);
848 ok (rc
, "GetFileSecurityA "
849 "was not expected to fail '%s': %d\n", path
, GetLastError());
850 ok (retSize
== sdSize
||
851 broken(retSize
== 0), /* NT4 */
852 "GetFileSecurityA returned size %d; expected %d\n", retSize
, sdSize
);
854 /* Use it to set security descriptor */
855 SetLastError(0xdeadbeef);
856 rc
= pSetFileSecurityA (path
, request
, sd
);
857 ok (rc
, "SetFileSecurityA "
858 "was not expected to fail '%s': %d\n", path
, GetLastError());
859 HeapFree (GetProcessHeap (), 0, sd
);
862 strcpy (wintmpdir
, "\\Should not exist");
863 SetLastError(0xdeadbeef);
864 rc
= pGetFileSecurityA (wintmpdir
, OWNER_SECURITY_INFORMATION
, NULL
, 0, &sdSize
);
865 ok (!rc
, "GetFileSecurityA should fail for not existing directories/files\n");
866 ok (GetLastError() == ERROR_FILE_NOT_FOUND
,
867 "last error ERROR_FILE_NOT_FOUND expected, got %d\n", GetLastError());
870 /* Remove temporary file and directory */
872 RemoveDirectoryA(path
);
874 /* Test file access permissions for a file with FILE_ATTRIBUTE_ARCHIVE */
875 SetLastError(0xdeadbeef);
876 rc
= GetTempPathA(sizeof(wintmpdir
), wintmpdir
);
877 ok(rc
, "GetTempPath error %d\n", GetLastError());
879 SetLastError(0xdeadbeef);
880 rc
= GetTempFileNameA(wintmpdir
, "tmp", 0, file
);
881 ok(rc
, "GetTempFileName error %d\n", GetLastError());
883 rc
= GetFileAttributesA(file
);
884 rc
&= ~(FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
|FILE_ATTRIBUTE_COMPRESSED
);
885 ok(rc
== FILE_ATTRIBUTE_ARCHIVE
, "expected FILE_ATTRIBUTE_ARCHIVE got %#x\n", rc
);
887 retSize
= 0xdeadbeef;
888 rc
= GetFileSecurityA(file
, OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
890 ok(!rc
, "GetFileSecurity should fail\n");
891 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
892 "expected ERROR_INSUFFICIENT_BUFFER got %d\n", GetLastError());
893 ok(sdSize
> sizeof(SECURITY_DESCRIPTOR
), "got sd size %d\n", sdSize
);
895 sd
= HeapAlloc(GetProcessHeap (), 0, sdSize
);
896 retSize
= 0xdeadbeef;
897 SetLastError(0xdeadbeef);
898 rc
= GetFileSecurityA(file
, OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
899 sd
, sdSize
, &retSize
);
900 ok(rc
, "GetFileSecurity error %d\n", GetLastError());
901 ok(retSize
== sdSize
|| broken(retSize
== 0) /* NT4 */, "expected %d, got %d\n", sdSize
, retSize
);
903 SetLastError(0xdeadbeef);
904 rc
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
, TRUE
, &token
);
905 ok(!rc
, "OpenThreadToken should fail\n");
906 ok(GetLastError() == ERROR_NO_TOKEN
, "expected ERROR_NO_TOKEN, got %d\n", GetLastError());
908 SetLastError(0xdeadbeef);
909 rc
= ImpersonateSelf(SecurityIdentification
);
910 ok(rc
, "ImpersonateSelf error %d\n", GetLastError());
912 SetLastError(0xdeadbeef);
913 rc
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
, TRUE
, &token
);
914 ok(rc
, "OpenThreadToken error %d\n", GetLastError());
916 SetLastError(0xdeadbeef);
918 ok(rc
, "RevertToSelf error %d\n", GetLastError());
920 priv_set_len
= sizeof(priv_set
);
921 granted
= 0xdeadbeef;
923 SetLastError(0xdeadbeef);
924 rc
= AccessCheck(sd
, token
, FILE_READ_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
925 ok(rc
, "AccessCheck error %d\n", GetLastError());
926 ok(status
== 1, "expected 1, got %d\n", status
);
927 ok(granted
== FILE_READ_DATA
, "expected FILE_READ_DATA, got %#x\n", granted
);
929 granted
= 0xdeadbeef;
931 SetLastError(0xdeadbeef);
932 rc
= AccessCheck(sd
, token
, FILE_WRITE_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
933 ok(rc
, "AccessCheck error %d\n", GetLastError());
934 ok(status
== 1, "expected 1, got %d\n", status
);
935 ok(granted
== FILE_WRITE_DATA
, "expected FILE_WRITE_DATA, got %#x\n", granted
);
937 granted
= 0xdeadbeef;
939 SetLastError(0xdeadbeef);
940 rc
= AccessCheck(sd
, token
, FILE_EXECUTE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
941 ok(rc
, "AccessCheck error %d\n", GetLastError());
942 ok(status
== 1, "expected 1, got %d\n", status
);
943 ok(granted
== FILE_EXECUTE
, "expected FILE_EXECUTE, got %#x\n", granted
);
945 granted
= 0xdeadbeef;
947 SetLastError(0xdeadbeef);
948 rc
= AccessCheck(sd
, token
, DELETE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
949 ok(rc
, "AccessCheck error %d\n", GetLastError());
950 ok(status
== 1, "expected 1, got %d\n", status
);
951 ok(granted
== DELETE
, "expected DELETE, got %#x\n", granted
);
953 granted
= 0xdeadbeef;
955 SetLastError(0xdeadbeef);
956 rc
= AccessCheck(sd
, token
, FILE_DELETE_CHILD
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
957 ok(rc
, "AccessCheck error %d\n", GetLastError());
958 ok(status
== 1, "expected 1, got %d\n", status
);
959 ok(granted
== FILE_DELETE_CHILD
, "expected FILE_DELETE_CHILD, got %#x\n", granted
);
961 granted
= 0xdeadbeef;
963 SetLastError(0xdeadbeef);
964 rc
= AccessCheck(sd
, token
, 0x1ff, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
965 ok(rc
, "AccessCheck error %d\n", GetLastError());
966 ok(status
== 1, "expected 1, got %d\n", status
);
967 ok(granted
== 0x1ff, "expected 0x1ff, got %#x\n", granted
);
969 granted
= 0xdeadbeef;
971 SetLastError(0xdeadbeef);
972 rc
= AccessCheck(sd
, token
, FILE_ALL_ACCESS
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
973 ok(rc
, "AccessCheck error %d\n", GetLastError());
974 ok(status
== 1, "expected 1, got %d\n", status
);
975 ok(granted
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", granted
);
977 SetLastError(0xdeadbeef);
978 rc
= AccessCheck(sd
, token
, 0xffffffff, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
979 ok(!rc
, "AccessCheck should fail\n");
980 ok(GetLastError() == ERROR_GENERIC_NOT_MAPPED
, "expected ERROR_GENERIC_NOT_MAPPED, got %d\n", GetLastError());
982 /* Test file access permissions for a file with FILE_ATTRIBUTE_READONLY */
983 SetLastError(0xdeadbeef);
984 fh
= CreateFileA(file
, FILE_READ_DATA
, FILE_SHARE_READ
, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_READONLY
, 0);
985 ok(fh
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
986 retSize
= 0xdeadbeef;
987 SetLastError(0xdeadbeef);
988 rc
= WriteFile(fh
, "1", 1, &retSize
, NULL
);
989 ok(!rc
, "WriteFile should fail\n");
990 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
991 ok(retSize
== 0, "expected 0, got %d\n", retSize
);
994 rc
= GetFileAttributesA(file
);
995 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 SetLastError(0xdeadbeef);
1001 rc
= SetFileAttributesA(file
, FILE_ATTRIBUTE_ARCHIVE
);
1002 ok(rc
, "SetFileAttributes error %d\n", GetLastError());
1003 SetLastError(0xdeadbeef);
1004 rc
= DeleteFileA(file
);
1005 ok(rc
, "DeleteFile error %d\n", GetLastError());
1007 SetLastError(0xdeadbeef);
1008 fh
= CreateFileA(file
, FILE_READ_DATA
, FILE_SHARE_READ
, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_READONLY
, 0);
1009 ok(fh
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
1010 retSize
= 0xdeadbeef;
1011 SetLastError(0xdeadbeef);
1012 rc
= WriteFile(fh
, "1", 1, &retSize
, NULL
);
1013 ok(!rc
, "WriteFile should fail\n");
1014 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1015 ok(retSize
== 0, "expected 0, got %d\n", retSize
);
1018 rc
= GetFileAttributesA(file
);
1019 rc
&= ~(FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
|FILE_ATTRIBUTE_COMPRESSED
);
1020 ok(rc
== (FILE_ATTRIBUTE_ARCHIVE
|FILE_ATTRIBUTE_READONLY
),
1021 "expected FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY got %#x\n", rc
);
1023 retSize
= 0xdeadbeef;
1024 SetLastError(0xdeadbeef);
1025 rc
= GetFileSecurityA(file
, OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
1026 sd
, sdSize
, &retSize
);
1027 ok(rc
, "GetFileSecurity error %d\n", GetLastError());
1028 ok(retSize
== sdSize
|| broken(retSize
== 0) /* NT4 */, "expected %d, got %d\n", sdSize
, retSize
);
1030 priv_set_len
= sizeof(priv_set
);
1031 granted
= 0xdeadbeef;
1032 status
= 0xdeadbeef;
1033 SetLastError(0xdeadbeef);
1034 rc
= AccessCheck(sd
, token
, FILE_READ_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1035 ok(rc
, "AccessCheck error %d\n", GetLastError());
1036 ok(status
== 1, "expected 1, got %d\n", status
);
1037 ok(granted
== FILE_READ_DATA
, "expected FILE_READ_DATA, got %#x\n", granted
);
1039 granted
= 0xdeadbeef;
1040 status
= 0xdeadbeef;
1041 SetLastError(0xdeadbeef);
1042 rc
= AccessCheck(sd
, token
, FILE_WRITE_DATA
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1043 ok(rc
, "AccessCheck error %d\n", GetLastError());
1045 ok(status
== 1, "expected 1, got %d\n", status
);
1046 ok(granted
== FILE_WRITE_DATA
, "expected FILE_WRITE_DATA, got %#x\n", granted
);
1048 granted
= 0xdeadbeef;
1049 status
= 0xdeadbeef;
1050 SetLastError(0xdeadbeef);
1051 rc
= AccessCheck(sd
, token
, FILE_EXECUTE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1052 ok(rc
, "AccessCheck error %d\n", GetLastError());
1053 ok(status
== 1, "expected 1, got %d\n", status
);
1054 ok(granted
== FILE_EXECUTE
, "expected FILE_EXECUTE, got %#x\n", granted
);
1056 granted
= 0xdeadbeef;
1057 status
= 0xdeadbeef;
1058 SetLastError(0xdeadbeef);
1059 rc
= AccessCheck(sd
, token
, DELETE
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1060 ok(rc
, "AccessCheck error %d\n", GetLastError());
1062 ok(status
== 1, "expected 1, got %d\n", status
);
1063 ok(granted
== DELETE
, "expected DELETE, got %#x\n", granted
);
1065 granted
= 0xdeadbeef;
1066 status
= 0xdeadbeef;
1067 SetLastError(0xdeadbeef);
1068 rc
= AccessCheck(sd
, token
, FILE_DELETE_CHILD
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1069 ok(rc
, "AccessCheck error %d\n", GetLastError());
1071 ok(status
== 1, "expected 1, got %d\n", status
);
1072 ok(granted
== FILE_DELETE_CHILD
, "expected FILE_DELETE_CHILD, got %#x\n", granted
);
1074 granted
= 0xdeadbeef;
1075 status
= 0xdeadbeef;
1076 SetLastError(0xdeadbeef);
1077 rc
= AccessCheck(sd
, token
, 0x1ff, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1078 ok(rc
, "AccessCheck error %d\n", GetLastError());
1080 ok(status
== 1, "expected 1, got %d\n", status
);
1081 ok(granted
== 0x1ff, "expected 0x1ff, got %#x\n", granted
);
1083 granted
= 0xdeadbeef;
1084 status
= 0xdeadbeef;
1085 SetLastError(0xdeadbeef);
1086 rc
= AccessCheck(sd
, token
, FILE_ALL_ACCESS
, &mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
1087 ok(rc
, "AccessCheck error %d\n", GetLastError());
1089 ok(status
== 1, "expected 1, got %d\n", status
);
1090 ok(granted
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", granted
);
1092 SetLastError(0xdeadbeef);
1093 rc
= DeleteFileA(file
);
1094 ok(!rc
, "DeleteFile should fail\n");
1095 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1096 SetLastError(0xdeadbeef);
1097 rc
= SetFileAttributesA(file
, FILE_ATTRIBUTE_ARCHIVE
);
1098 ok(rc
, "SetFileAttributes error %d\n", GetLastError());
1099 SetLastError(0xdeadbeef);
1100 rc
= DeleteFileA(file
);
1101 ok(rc
, "DeleteFile error %d\n", GetLastError());
1104 HeapFree(GetProcessHeap(), 0, sd
);
1107 static void test_AccessCheck(void)
1109 PSID EveryoneSid
= NULL
, AdminSid
= NULL
, UsersSid
= NULL
;
1111 SECURITY_DESCRIPTOR
*SecurityDescriptor
= NULL
;
1112 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
1113 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
1114 GENERIC_MAPPING Mapping
= { KEY_READ
, KEY_WRITE
, KEY_EXECUTE
, KEY_ALL_ACCESS
};
1118 HANDLE ProcessToken
;
1121 PRIVILEGE_SET
*PrivSet
;
1123 HMODULE NtDllModule
;
1126 NTSTATUS ntret
, ntAccessStatus
;
1128 NtDllModule
= GetModuleHandleA("ntdll.dll");
1131 skip("not running on NT, skipping test\n");
1134 pRtlAdjustPrivilege
= (void *)GetProcAddress(NtDllModule
, "RtlAdjustPrivilege");
1135 if (!pRtlAdjustPrivilege
)
1137 win_skip("missing RtlAdjustPrivilege, skipping test\n");
1141 Acl
= HeapAlloc(GetProcessHeap(), 0, 256);
1142 res
= InitializeAcl(Acl
, 256, ACL_REVISION
);
1143 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
1145 skip("ACLs not implemented - skipping tests\n");
1146 HeapFree(GetProcessHeap(), 0, Acl
);
1149 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
1151 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
1152 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1154 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1155 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &AdminSid
);
1156 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1158 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1159 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
1160 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1162 SecurityDescriptor
= HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
1164 res
= InitializeSecurityDescriptor(SecurityDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
1165 ok(res
, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
1167 res
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
1168 ok(res
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1170 PrivSetLen
= FIELD_OFFSET(PRIVILEGE_SET
, Privilege
[16]);
1171 PrivSet
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, PrivSetLen
);
1172 PrivSet
->PrivilegeCount
= 16;
1174 res
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &ProcessToken
);
1175 ok(res
, "OpenProcessToken failed with error %d\n", GetLastError());
1177 pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE
, FALSE
, TRUE
, &Enabled
);
1179 res
= DuplicateToken(ProcessToken
, SecurityImpersonation
, &Token
);
1180 ok(res
, "DuplicateToken failed with error %d\n", GetLastError());
1182 /* SD without owner/group */
1183 SetLastError(0xdeadbeef);
1184 Access
= AccessStatus
= 0x1abe11ed;
1185 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_QUERY_VALUE
, &Mapping
,
1186 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1187 err
= GetLastError();
1188 ok(!ret
&& err
== ERROR_INVALID_SECURITY_DESCR
, "AccessCheck should have "
1189 "failed with ERROR_INVALID_SECURITY_DESCR, instead of %d\n", err
);
1190 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1191 "Access and/or AccessStatus were changed!\n");
1193 /* Set owner and group */
1194 res
= SetSecurityDescriptorOwner(SecurityDescriptor
, AdminSid
, FALSE
);
1195 ok(res
, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
1196 res
= SetSecurityDescriptorGroup(SecurityDescriptor
, UsersSid
, TRUE
);
1197 ok(res
, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
1199 /* Generic access mask */
1200 SetLastError(0xdeadbeef);
1201 Access
= AccessStatus
= 0x1abe11ed;
1202 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1203 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1204 err
= GetLastError();
1205 ok(!ret
&& err
== ERROR_GENERIC_NOT_MAPPED
, "AccessCheck should have failed "
1206 "with ERROR_GENERIC_NOT_MAPPED, instead of %d\n", err
);
1207 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1208 "Access and/or AccessStatus were changed!\n");
1210 /* Generic access mask - no privilegeset buffer */
1211 SetLastError(0xdeadbeef);
1212 Access
= AccessStatus
= 0x1abe11ed;
1213 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1214 NULL
, &PrivSetLen
, &Access
, &AccessStatus
);
1215 err
= GetLastError();
1216 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1217 "with ERROR_NOACCESS, instead of %d\n", err
);
1218 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1219 "Access and/or AccessStatus were changed!\n");
1221 /* Generic access mask - no returnlength */
1222 SetLastError(0xdeadbeef);
1223 Access
= AccessStatus
= 0x1abe11ed;
1224 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1225 PrivSet
, NULL
, &Access
, &AccessStatus
);
1226 err
= GetLastError();
1227 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1228 "with ERROR_NOACCESS, instead of %d\n", err
);
1229 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1230 "Access and/or AccessStatus were changed!\n");
1232 /* Generic access mask - no privilegeset buffer, no returnlength */
1233 SetLastError(0xdeadbeef);
1234 Access
= AccessStatus
= 0x1abe11ed;
1235 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1236 NULL
, NULL
, &Access
, &AccessStatus
);
1237 err
= GetLastError();
1238 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1239 "with ERROR_NOACCESS, instead of %d\n", err
);
1240 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1241 "Access and/or AccessStatus were changed!\n");
1243 /* sd with no dacl present */
1244 Access
= AccessStatus
= 0x1abe11ed;
1245 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, FALSE
, NULL
, FALSE
);
1246 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1247 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1248 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1249 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1250 ok(AccessStatus
&& (Access
== KEY_READ
),
1251 "AccessCheck failed to grant access with error %d\n",
1254 /* sd with no dacl present - no privilegeset buffer */
1255 SetLastError(0xdeadbeef);
1256 Access
= AccessStatus
= 0x1abe11ed;
1257 ret
= AccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1258 NULL
, &PrivSetLen
, &Access
, &AccessStatus
);
1259 err
= GetLastError();
1260 ok(!ret
&& err
== ERROR_NOACCESS
, "AccessCheck should have failed "
1261 "with ERROR_NOACCESS, instead of %d\n", err
);
1262 ok(Access
== 0x1abe11ed && AccessStatus
== 0x1abe11ed,
1263 "Access and/or AccessStatus were changed!\n");
1267 /* Generic access mask - no privilegeset buffer */
1268 SetLastError(0xdeadbeef);
1269 Access
= ntAccessStatus
= 0x1abe11ed;
1270 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1271 NULL
, &PrivSetLen
, &Access
, &ntAccessStatus
);
1272 err
= GetLastError();
1273 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1274 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1275 ok(err
== 0xdeadbeef,
1276 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1277 ok(Access
== 0x1abe11ed && ntAccessStatus
== 0x1abe11ed,
1278 "Access and/or AccessStatus were changed!\n");
1280 /* Generic access mask - no returnlength */
1281 SetLastError(0xdeadbeef);
1282 Access
= ntAccessStatus
= 0x1abe11ed;
1283 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1284 PrivSet
, NULL
, &Access
, &ntAccessStatus
);
1285 err
= GetLastError();
1286 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1287 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1288 ok(err
== 0xdeadbeef,
1289 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1290 ok(Access
== 0x1abe11ed && ntAccessStatus
== 0x1abe11ed,
1291 "Access and/or AccessStatus were changed!\n");
1293 /* Generic access mask - no privilegeset buffer, no returnlength */
1294 SetLastError(0xdeadbeef);
1295 Access
= ntAccessStatus
= 0x1abe11ed;
1296 ntret
= pNtAccessCheck(SecurityDescriptor
, Token
, GENERIC_READ
, &Mapping
,
1297 NULL
, NULL
, &Access
, &ntAccessStatus
);
1298 err
= GetLastError();
1299 ok(ntret
== STATUS_ACCESS_VIOLATION
,
1300 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret
);
1301 ok(err
== 0xdeadbeef,
1302 "NtAccessCheck shouldn't set last error, got %d\n", err
);
1303 ok(Access
== 0x1abe11ed && ntAccessStatus
== 0x1abe11ed,
1304 "Access and/or AccessStatus were changed!\n");
1307 win_skip("NtAccessCheck unavailable. Skipping.\n");
1309 /* sd with NULL dacl */
1310 Access
= AccessStatus
= 0x1abe11ed;
1311 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, NULL
, FALSE
);
1312 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1313 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1314 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1315 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1316 ok(AccessStatus
&& (Access
== KEY_READ
),
1317 "AccessCheck failed to grant access with error %d\n",
1320 /* sd with blank dacl */
1321 ret
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
1322 ok(ret
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1323 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1324 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1325 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1326 err
= GetLastError();
1327 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1328 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1329 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1331 res
= AddAccessAllowedAce(Acl
, ACL_REVISION
, KEY_READ
, EveryoneSid
);
1332 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
1334 res
= AddAccessDeniedAce(Acl
, ACL_REVISION
, KEY_SET_VALUE
, AdminSid
);
1335 ok(res
, "AddAccessDeniedAce failed with error %d\n", GetLastError());
1338 Access
= AccessStatus
= 0x1abe11ed;
1339 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1340 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1341 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1342 ok(AccessStatus
&& (Access
== KEY_READ
),
1343 "AccessCheck failed to grant access with error %d\n",
1346 ret
= AccessCheck(SecurityDescriptor
, Token
, MAXIMUM_ALLOWED
, &Mapping
,
1347 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1348 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1350 "AccessCheck failed to grant any access with error %d\n",
1352 trace("AccessCheck with MAXIMUM_ALLOWED got Access 0x%08x\n", Access
);
1354 /* Access denied by SD */
1355 SetLastError(0xdeadbeef);
1356 Access
= AccessStatus
= 0x1abe11ed;
1357 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_WRITE
, &Mapping
,
1358 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1359 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1360 err
= GetLastError();
1361 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1362 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1363 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1366 PrivSet
->PrivilegeCount
= 16;
1367 ret
= AccessCheck(SecurityDescriptor
, Token
, ACCESS_SYSTEM_SECURITY
, &Mapping
,
1368 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1369 ok(ret
&& !AccessStatus
&& GetLastError() == ERROR_PRIVILEGE_NOT_HELD
,
1370 "AccessCheck should have failed with ERROR_PRIVILEGE_NOT_HELD, instead of %d\n",
1373 ret
= ImpersonateLoggedOnUser(Token
);
1374 ok(ret
, "ImpersonateLoggedOnUser failed with error %d\n", GetLastError());
1375 ret
= pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE
, TRUE
, TRUE
, &Enabled
);
1379 PrivSet
->PrivilegeCount
= 16;
1380 ret
= AccessCheck(SecurityDescriptor
, Token
, ACCESS_SYSTEM_SECURITY
, &Mapping
,
1381 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1382 ok(ret
&& AccessStatus
&& GetLastError() == 0,
1383 "AccessCheck should have succeeded, error %d\n",
1385 ok(Access
== ACCESS_SYSTEM_SECURITY
,
1386 "Access should be equal to ACCESS_SYSTEM_SECURITY instead of 0x%08x\n",
1390 trace("Couldn't get SE_SECURITY_PRIVILEGE (0x%08x), skipping ACCESS_SYSTEM_SECURITY test\n",
1392 ret
= RevertToSelf();
1393 ok(ret
, "RevertToSelf failed with error %d\n", GetLastError());
1395 /* test INHERIT_ONLY_ACE */
1396 ret
= InitializeAcl(Acl
, 256, ACL_REVISION
);
1397 ok(ret
, "InitializeAcl failed with error %d\n", GetLastError());
1399 /* NT doesn't have AddAccessAllowedAceEx. Skipping this call/test doesn't influence
1402 if (pAddAccessAllowedAceEx
)
1404 ret
= pAddAccessAllowedAceEx(Acl
, ACL_REVISION
, INHERIT_ONLY_ACE
, KEY_READ
, EveryoneSid
);
1405 ok(ret
, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
1408 win_skip("AddAccessAllowedAceEx is not available\n");
1410 ret
= AccessCheck(SecurityDescriptor
, Token
, KEY_READ
, &Mapping
,
1411 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1412 ok(ret
, "AccessCheck failed with error %d\n", GetLastError());
1413 err
= GetLastError();
1414 ok(!AccessStatus
&& err
== ERROR_ACCESS_DENIED
, "AccessCheck should have failed "
1415 "with ERROR_ACCESS_DENIED, instead of %d\n", err
);
1416 ok(!Access
, "Should have failed to grant any access, got 0x%08x\n", Access
);
1420 res
= DuplicateToken(ProcessToken
, SecurityAnonymous
, &Token
);
1421 ok(res
, "DuplicateToken failed with error %d\n", GetLastError());
1423 SetLastError(0xdeadbeef);
1424 ret
= AccessCheck(SecurityDescriptor
, Token
, MAXIMUM_ALLOWED
, &Mapping
,
1425 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1426 err
= GetLastError();
1427 ok(!ret
&& err
== ERROR_BAD_IMPERSONATION_LEVEL
, "AccessCheck should have failed "
1428 "with ERROR_BAD_IMPERSONATION_LEVEL, instead of %d\n", err
);
1432 SetLastError(0xdeadbeef);
1433 ret
= AccessCheck(SecurityDescriptor
, ProcessToken
, KEY_READ
, &Mapping
,
1434 PrivSet
, &PrivSetLen
, &Access
, &AccessStatus
);
1435 err
= GetLastError();
1436 ok(!ret
&& err
== ERROR_NO_IMPERSONATION_TOKEN
, "AccessCheck should have failed "
1437 "with ERROR_NO_IMPERSONATION_TOKEN, instead of %d\n", err
);
1439 CloseHandle(ProcessToken
);
1442 FreeSid(EveryoneSid
);
1447 HeapFree(GetProcessHeap(), 0, Acl
);
1448 HeapFree(GetProcessHeap(), 0, SecurityDescriptor
);
1449 HeapFree(GetProcessHeap(), 0, PrivSet
);
1452 /* test GetTokenInformation for the various attributes */
1453 static void test_token_attr(void)
1455 HANDLE Token
, ImpersonationToken
;
1457 TOKEN_PRIVILEGES
*Privileges
;
1458 TOKEN_GROUPS
*Groups
;
1460 TOKEN_DEFAULT_DACL
*Dacl
;
1464 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
;
1467 /* cygwin-like use case */
1468 SetLastError(0xdeadbeef);
1469 ret
= OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED
, &Token
);
1470 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
1472 win_skip("OpenProcessToken is not implemented\n");
1475 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1478 DWORD buf
[256]; /* GetTokenInformation wants a dword-aligned buffer */
1480 ret
= GetTokenInformation(Token
, TokenUser
,(void*)buf
, Size
, &Size
);
1481 ok(ret
, "GetTokenInformation failed with error %d\n", GetLastError());
1482 Size
= sizeof(ImpersonationLevel
);
1483 ret
= GetTokenInformation(Token
, TokenImpersonationLevel
, &ImpersonationLevel
, Size
, &Size
);
1484 GLE
= GetLastError();
1485 ok(!ret
&& (GLE
== ERROR_INVALID_PARAMETER
), "GetTokenInformation(TokenImpersonationLevel) on primary token should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GLE
);
1489 if(!pConvertSidToStringSidA
)
1491 win_skip("ConvertSidToStringSidA is not available\n");
1495 SetLastError(0xdeadbeef);
1496 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS
, &Token
);
1497 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1500 /* insufficient buffer length */
1501 SetLastError(0xdeadbeef);
1503 ret
= GetTokenInformation(Token
, TokenGroups
, NULL
, 0, &Size2
);
1504 ok(Size2
> 1, "got %d\n", Size2
);
1505 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1506 "%d with error %d\n", ret
, GetLastError());
1508 Groups
= HeapAlloc(GetProcessHeap(), 0, Size2
);
1509 memset(Groups
, 0xcc, Size2
);
1511 ret
= GetTokenInformation(Token
, TokenGroups
, Groups
, Size2
, &Size
);
1512 ok(Size
> 1, "got %d\n", Size
);
1513 ok((!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
) || broken(ret
) /* wow64 */,
1514 "%d with error %d\n", ret
, GetLastError());
1516 ok(*((BYTE
*)Groups
) == 0xcc, "buffer altered\n");
1518 HeapFree(GetProcessHeap(), 0, Groups
);
1520 SetLastError(0xdeadbeef);
1521 ret
= GetTokenInformation(Token
, TokenGroups
, NULL
, 0, &Size
);
1522 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1523 "GetTokenInformation(TokenGroups) %s with error %d\n",
1524 ret
? "succeeded" : "failed", GetLastError());
1525 Groups
= HeapAlloc(GetProcessHeap(), 0, Size
);
1526 SetLastError(0xdeadbeef);
1527 ret
= GetTokenInformation(Token
, TokenGroups
, Groups
, Size
, &Size
);
1528 ok(ret
, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
1529 ok(GetLastError() == 0xdeadbeef,
1530 "GetTokenInformation shouldn't have set last error to %d\n",
1532 trace("TokenGroups:\n");
1533 for (i
= 0; i
< Groups
->GroupCount
; i
++)
1535 DWORD NameLength
= 255;
1537 DWORD DomainLength
= 255;
1539 SID_NAME_USE SidNameUse
;
1542 ret
= LookupAccountSidA(NULL
, Groups
->Groups
[i
].Sid
, Name
, &NameLength
, Domain
, &DomainLength
, &SidNameUse
);
1545 pConvertSidToStringSidA(Groups
->Groups
[i
].Sid
, &SidString
);
1546 trace("%s, %s\\%s use: %d attr: 0x%08x\n", SidString
, Domain
, Name
, SidNameUse
, Groups
->Groups
[i
].Attributes
);
1547 LocalFree(SidString
);
1549 else trace("attr: 0x%08x LookupAccountSid failed with error %d\n", Groups
->Groups
[i
].Attributes
, GetLastError());
1551 HeapFree(GetProcessHeap(), 0, Groups
);
1554 ret
= GetTokenInformation(Token
, TokenUser
, NULL
, 0, &Size
);
1555 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1556 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1557 User
= HeapAlloc(GetProcessHeap(), 0, Size
);
1558 ret
= GetTokenInformation(Token
, TokenUser
, User
, Size
, &Size
);
1560 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1562 pConvertSidToStringSidA(User
->User
.Sid
, &SidString
);
1563 trace("TokenUser: %s attr: 0x%08x\n", SidString
, User
->User
.Attributes
);
1564 LocalFree(SidString
);
1565 HeapFree(GetProcessHeap(), 0, User
);
1568 ret
= GetTokenInformation(Token
, TokenPrivileges
, NULL
, 0, &Size
);
1569 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1570 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1571 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
1572 ret
= GetTokenInformation(Token
, TokenPrivileges
, Privileges
, Size
, &Size
);
1574 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1575 trace("TokenPrivileges:\n");
1576 for (i
= 0; i
< Privileges
->PrivilegeCount
; i
++)
1579 DWORD NameLen
= sizeof(Name
)/sizeof(Name
[0]);
1580 LookupPrivilegeNameA(NULL
, &Privileges
->Privileges
[i
].Luid
, Name
, &NameLen
);
1581 trace("\t%s, 0x%x\n", Name
, Privileges
->Privileges
[i
].Attributes
);
1583 HeapFree(GetProcessHeap(), 0, Privileges
);
1585 ret
= DuplicateToken(Token
, SecurityAnonymous
, &ImpersonationToken
);
1586 ok(ret
, "DuplicateToken failed with error %d\n", GetLastError());
1588 Size
= sizeof(ImpersonationLevel
);
1589 ret
= GetTokenInformation(ImpersonationToken
, TokenImpersonationLevel
, &ImpersonationLevel
, Size
, &Size
);
1590 ok(ret
, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1591 ok(ImpersonationLevel
== SecurityAnonymous
, "ImpersonationLevel should have been SecurityAnonymous instead of %d\n", ImpersonationLevel
);
1593 CloseHandle(ImpersonationToken
);
1596 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, NULL
, 0, &Size
);
1597 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
1598 "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1600 Dacl
= HeapAlloc(GetProcessHeap(), 0, Size
);
1601 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size
);
1602 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1604 SetLastError(0xdeadbeef);
1605 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, NULL
, 0);
1606 GLE
= GetLastError();
1607 ok(!ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1608 ok(GLE
== ERROR_BAD_LENGTH
, "expected ERROR_BAD_LENGTH got %u\n", GLE
);
1610 SetLastError(0xdeadbeef);
1611 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, NULL
, Size
);
1612 GLE
= GetLastError();
1613 ok(!ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1614 ok(GLE
== ERROR_NOACCESS
, "expected ERROR_NOACCESS got %u\n", GLE
);
1616 acl
= Dacl
->DefaultDacl
;
1617 Dacl
->DefaultDacl
= NULL
;
1619 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
);
1620 ok(ret
, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1623 Dacl
->DefaultDacl
= (ACL
*)0xdeadbeef;
1624 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size2
);
1625 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1626 ok(Dacl
->DefaultDacl
== NULL
, "expected NULL, got %p\n", Dacl
->DefaultDacl
);
1627 ok(Size2
== sizeof(TOKEN_DEFAULT_DACL
) || broken(Size2
== 2*sizeof(TOKEN_DEFAULT_DACL
)), /* WoW64 */
1628 "got %u expected sizeof(TOKEN_DEFAULT_DACL)\n", Size2
);
1630 Dacl
->DefaultDacl
= acl
;
1631 ret
= SetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
);
1632 ok(ret
, "SetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1634 if (Size2
== sizeof(TOKEN_DEFAULT_DACL
)) {
1635 ret
= GetTokenInformation(Token
, TokenDefaultDacl
, Dacl
, Size
, &Size2
);
1636 ok(ret
, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1638 win_skip("TOKEN_DEFAULT_DACL size too small on WoW64\n");
1640 HeapFree(GetProcessHeap(), 0, Dacl
);
1644 typedef union _MAX_SID
1647 char max
[SECURITY_MAX_SID_SIZE
];
1650 static void test_sid_str(PSID
* sid
)
1653 BOOL ret
= pConvertSidToStringSidA(sid
, &str_sid
);
1654 ok(ret
, "ConvertSidToStringSidA() failed: %d\n", GetLastError());
1657 char account
[MAX_PATH
], domain
[MAX_PATH
];
1659 DWORD acc_size
= MAX_PATH
;
1660 DWORD dom_size
= MAX_PATH
;
1661 ret
= LookupAccountSidA (NULL
, sid
, account
, &acc_size
, domain
, &dom_size
, &use
);
1662 ok(ret
|| (!ret
&& (GetLastError() == ERROR_NONE_MAPPED
)),
1663 "LookupAccountSid(%s) failed: %d\n", str_sid
, GetLastError());
1665 trace(" %s %s\\%s %d\n", str_sid
, domain
, account
, use
);
1666 else if (GetLastError() == ERROR_NONE_MAPPED
)
1667 trace(" %s couldn't be mapped\n", str_sid
);
1672 static const struct well_known_sid_value
1674 BOOL without_domain
;
1675 const char *sid_string
;
1676 } well_known_sid_values
[] = {
1677 /* 0 */ {TRUE
, "S-1-0-0"}, {TRUE
, "S-1-1-0"}, {TRUE
, "S-1-2-0"}, {TRUE
, "S-1-3-0"},
1678 /* 4 */ {TRUE
, "S-1-3-1"}, {TRUE
, "S-1-3-2"}, {TRUE
, "S-1-3-3"}, {TRUE
, "S-1-5"},
1679 /* 8 */ {FALSE
, "S-1-5-1"}, {TRUE
, "S-1-5-2"}, {TRUE
, "S-1-5-3"}, {TRUE
, "S-1-5-4"},
1680 /* 12 */ {TRUE
, "S-1-5-6"}, {TRUE
, "S-1-5-7"}, {TRUE
, "S-1-5-8"}, {TRUE
, "S-1-5-9"},
1681 /* 16 */ {TRUE
, "S-1-5-10"}, {TRUE
, "S-1-5-11"}, {TRUE
, "S-1-5-12"}, {TRUE
, "S-1-5-13"},
1682 /* 20 */ {TRUE
, "S-1-5-14"}, {FALSE
, NULL
}, {TRUE
, "S-1-5-18"}, {TRUE
, "S-1-5-19"},
1683 /* 24 */ {TRUE
, "S-1-5-20"}, {TRUE
, "S-1-5-32"},
1684 /* 26 */ {FALSE
, "S-1-5-32-544"}, {TRUE
, "S-1-5-32-545"}, {TRUE
, "S-1-5-32-546"},
1685 /* 29 */ {TRUE
, "S-1-5-32-547"}, {TRUE
, "S-1-5-32-548"}, {TRUE
, "S-1-5-32-549"},
1686 /* 32 */ {TRUE
, "S-1-5-32-550"}, {TRUE
, "S-1-5-32-551"}, {TRUE
, "S-1-5-32-552"},
1687 /* 35 */ {TRUE
, "S-1-5-32-554"}, {TRUE
, "S-1-5-32-555"}, {TRUE
, "S-1-5-32-556"},
1688 /* 38 */ {FALSE
, "S-1-5-21-12-23-34-45-56-500"}, {FALSE
, "S-1-5-21-12-23-34-45-56-501"},
1689 /* 40 */ {FALSE
, "S-1-5-21-12-23-34-45-56-502"}, {FALSE
, "S-1-5-21-12-23-34-45-56-512"},
1690 /* 42 */ {FALSE
, "S-1-5-21-12-23-34-45-56-513"}, {FALSE
, "S-1-5-21-12-23-34-45-56-514"},
1691 /* 44 */ {FALSE
, "S-1-5-21-12-23-34-45-56-515"}, {FALSE
, "S-1-5-21-12-23-34-45-56-516"},
1692 /* 46 */ {FALSE
, "S-1-5-21-12-23-34-45-56-517"}, {FALSE
, "S-1-5-21-12-23-34-45-56-518"},
1693 /* 48 */ {FALSE
, "S-1-5-21-12-23-34-45-56-519"}, {FALSE
, "S-1-5-21-12-23-34-45-56-520"},
1694 /* 50 */ {FALSE
, "S-1-5-21-12-23-34-45-56-553"},
1695 /* Added in Windows Server 2003 */
1696 /* 51 */ {TRUE
, "S-1-5-64-10"}, {TRUE
, "S-1-5-64-21"}, {TRUE
, "S-1-5-64-14"},
1697 /* 54 */ {TRUE
, "S-1-5-15"}, {TRUE
, "S-1-5-1000"}, {FALSE
, "S-1-5-32-557"},
1698 /* 57 */ {TRUE
, "S-1-5-32-558"}, {TRUE
, "S-1-5-32-559"}, {TRUE
, "S-1-5-32-560"},
1699 /* 60 */ {TRUE
, "S-1-5-32-561"}, {TRUE
, "S-1-5-32-562"},
1700 /* Added in Windows Vista: */
1701 /* 62 */ {TRUE
, "S-1-5-32-568"},
1702 /* 63 */ {TRUE
, "S-1-5-17"}, {FALSE
, "S-1-5-32-569"}, {TRUE
, "S-1-16-0"},
1703 /* 66 */ {TRUE
, "S-1-16-4096"}, {TRUE
, "S-1-16-8192"}, {TRUE
, "S-1-16-12288"},
1704 /* 69 */ {TRUE
, "S-1-16-16384"}, {TRUE
, "S-1-5-33"}, {TRUE
, "S-1-3-4"},
1705 /* 72 */ {FALSE
, "S-1-5-21-12-23-34-45-56-571"}, {FALSE
, "S-1-5-21-12-23-34-45-56-572"},
1706 /* 74 */ {TRUE
, "S-1-5-22"}, {FALSE
, "S-1-5-21-12-23-34-45-56-521"}, {TRUE
, "S-1-5-32-573"},
1707 /* 77 */ {FALSE
, "S-1-5-21-12-23-34-45-56-498"}, {TRUE
, "S-1-5-32-574"}, {TRUE
, "S-1-16-8448"}
1710 static void test_CreateWellKnownSid(void)
1712 SID_IDENTIFIER_AUTHORITY ident
= { SECURITY_NT_AUTHORITY
};
1713 PSID domainsid
, sid
;
1718 if (!pCreateWellKnownSid
)
1720 win_skip("CreateWellKnownSid not available\n");
1725 SetLastError(0xdeadbeef);
1726 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, NULL
, &size
);
1727 error
= GetLastError();
1728 ok(!ret
, "CreateWellKnownSid succeeded\n");
1729 ok(error
== ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error
);
1730 ok(size
, "expected size > 0\n");
1732 SetLastError(0xdeadbeef);
1733 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, NULL
, &size
);
1734 error
= GetLastError();
1735 ok(!ret
, "CreateWellKnownSid succeeded\n");
1736 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %u\n", error
);
1738 sid
= HeapAlloc(GetProcessHeap(), 0, size
);
1739 ret
= pCreateWellKnownSid(WinInteractiveSid
, NULL
, sid
, &size
);
1740 ok(ret
, "CreateWellKnownSid failed %u\n", GetLastError());
1741 HeapFree(GetProcessHeap(), 0, sid
);
1743 /* a domain sid usually have three subauthorities but we test that CreateWellKnownSid doesn't check it */
1744 AllocateAndInitializeSid(&ident
, 6, SECURITY_NT_NON_UNIQUE
, 12, 23, 34, 45, 56, 0, 0, &domainsid
);
1746 for (i
= 0; i
< sizeof(well_known_sid_values
)/sizeof(well_known_sid_values
[0]); i
++)
1748 const struct well_known_sid_value
*value
= &well_known_sid_values
[i
];
1749 char sid_buffer
[SECURITY_MAX_SID_SIZE
];
1753 if (value
->sid_string
== NULL
)
1756 if (i
> WinAccountRasAndIasServersSid
)
1758 /* These SIDs aren't implemented by all Windows versions - detect it and break the loop */
1759 cb
= sizeof(sid_buffer
);
1760 if (!pCreateWellKnownSid(i
, domainsid
, sid_buffer
, &cb
))
1762 skip("Well known SIDs starting from %u are not implemented\n", i
);
1767 cb
= sizeof(sid_buffer
);
1768 ok(pCreateWellKnownSid(i
, value
->without_domain
? NULL
: domainsid
, sid_buffer
, &cb
), "Couldn't create well known sid %u\n", i
);
1769 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer
)), cb
, DWORD
, "%d");
1770 ok(IsValidSid(sid_buffer
), "The sid is not valid\n");
1771 ok(pConvertSidToStringSidA(sid_buffer
, &str
), "Couldn't convert SID to string\n");
1772 ok(strcmp(str
, value
->sid_string
) == 0, "%d: SID mismatch - expected %s, got %s\n", i
,
1773 value
->sid_string
, str
);
1776 if (value
->without_domain
)
1778 char buf2
[SECURITY_MAX_SID_SIZE
];
1780 ok(pCreateWellKnownSid(i
, domainsid
, buf2
, &cb
), "Couldn't create well known sid %u with optional domain\n", i
);
1781 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer
)), cb
, DWORD
, "%d");
1782 ok(memcmp(buf2
, sid_buffer
, cb
) == 0, "SID create with domain is different than without (%u)\n", i
);
1789 static void test_LookupAccountSid(void)
1791 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
1792 CHAR accountA
[MAX_PATH
], domainA
[MAX_PATH
], usernameA
[MAX_PATH
];
1793 DWORD acc_sizeA
, dom_sizeA
, user_sizeA
;
1794 DWORD real_acc_sizeA
, real_dom_sizeA
;
1795 WCHAR accountW
[MAX_PATH
], domainW
[MAX_PATH
];
1796 DWORD acc_sizeW
, dom_sizeW
;
1797 DWORD real_acc_sizeW
, real_dom_sizeW
;
1798 PSID pUsersSid
= NULL
;
1801 DWORD error
, size
, cbti
= 0;
1806 PTOKEN_USER ptiUser
= NULL
;
1808 /* native windows crashes if account size, domain size, or name use is NULL */
1810 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
1811 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &pUsersSid
);
1812 ok(ret
|| (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
),
1813 "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1815 /* not running on NT so give up */
1816 if (!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
1819 real_acc_sizeA
= MAX_PATH
;
1820 real_dom_sizeA
= MAX_PATH
;
1821 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &real_acc_sizeA
, domainA
, &real_dom_sizeA
, &use
);
1822 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1824 /* try NULL account */
1825 acc_sizeA
= MAX_PATH
;
1826 dom_sizeA
= MAX_PATH
;
1827 ret
= LookupAccountSidA(NULL
, pUsersSid
, NULL
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1828 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1830 /* try NULL domain */
1831 acc_sizeA
= MAX_PATH
;
1832 dom_sizeA
= MAX_PATH
;
1833 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, NULL
, &dom_sizeA
, &use
);
1834 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1836 /* try a small account buffer */
1838 dom_sizeA
= MAX_PATH
;
1840 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1841 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1842 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1843 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1845 /* try a 0 sized account buffer */
1847 dom_sizeA
= MAX_PATH
;
1849 LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1850 /* this can fail or succeed depending on OS version but the size will always be returned */
1851 ok(acc_sizeA
== real_acc_sizeA
+ 1,
1852 "LookupAccountSidA() Expected acc_size = %u, got %u\n",
1853 real_acc_sizeA
+ 1, acc_sizeA
);
1855 /* try a 0 sized account buffer */
1857 dom_sizeA
= MAX_PATH
;
1858 LookupAccountSidA(NULL
, pUsersSid
, NULL
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1859 /* this can fail or succeed depending on OS version but the size will always be returned */
1860 ok(acc_sizeA
== real_acc_sizeA
+ 1,
1861 "LookupAccountSid() Expected acc_size = %u, got %u\n",
1862 real_acc_sizeA
+ 1, acc_sizeA
);
1864 /* try a small domain buffer */
1866 acc_sizeA
= MAX_PATH
;
1868 ret
= LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1869 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1870 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1871 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1873 /* try a 0 sized domain buffer */
1875 acc_sizeA
= MAX_PATH
;
1877 LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1878 /* this can fail or succeed depending on OS version but the size will always be returned */
1879 ok(dom_sizeA
== real_dom_sizeA
+ 1,
1880 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1881 real_dom_sizeA
+ 1, dom_sizeA
);
1883 /* try a 0 sized domain buffer */
1885 acc_sizeA
= MAX_PATH
;
1886 LookupAccountSidA(NULL
, pUsersSid
, accountA
, &acc_sizeA
, NULL
, &dom_sizeA
, &use
);
1887 /* this can fail or succeed depending on OS version but the size will always be returned */
1888 ok(dom_sizeA
== real_dom_sizeA
+ 1,
1889 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1890 real_dom_sizeA
+ 1, dom_sizeA
);
1892 real_acc_sizeW
= MAX_PATH
;
1893 real_dom_sizeW
= MAX_PATH
;
1894 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &real_acc_sizeW
, domainW
, &real_dom_sizeW
, &use
);
1895 ok(ret
, "LookupAccountSidW() Expected TRUE, got FALSE\n");
1897 /* try an invalid system name */
1898 real_acc_sizeA
= MAX_PATH
;
1899 real_dom_sizeA
= MAX_PATH
;
1900 ret
= LookupAccountSidA("deepthought", pUsersSid
, accountA
, &real_acc_sizeA
, domainA
, &real_dom_sizeA
, &use
);
1901 ok(!ret
, "LookupAccountSidA() Expected FALSE got TRUE\n");
1902 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE
|| GetLastError() == RPC_S_INVALID_NET_ADDR
/* Vista */,
1903 "LookupAccountSidA() Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %u\n", GetLastError());
1905 /* native windows crashes if domainW or accountW is NULL */
1907 /* try a small account buffer */
1909 dom_sizeW
= MAX_PATH
;
1911 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1912 ok(!ret
, "LookupAccountSidW() Expected FALSE got TRUE\n");
1913 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1914 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1916 /* try a 0 sized account buffer */
1918 dom_sizeW
= MAX_PATH
;
1920 LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1921 /* this can fail or succeed depending on OS version but the size will always be returned */
1922 ok(acc_sizeW
== real_acc_sizeW
+ 1,
1923 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1924 real_acc_sizeW
+ 1, acc_sizeW
);
1926 /* try a 0 sized account buffer */
1928 dom_sizeW
= MAX_PATH
;
1929 LookupAccountSidW(NULL
, pUsersSid
, NULL
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1930 /* this can fail or succeed depending on OS version but the size will always be returned */
1931 ok(acc_sizeW
== real_acc_sizeW
+ 1,
1932 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1933 real_acc_sizeW
+ 1, acc_sizeW
);
1935 /* try a small domain buffer */
1937 acc_sizeW
= MAX_PATH
;
1939 ret
= LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1940 ok(!ret
, "LookupAccountSidW() Expected FALSE got TRUE\n");
1941 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1942 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1944 /* try a 0 sized domain buffer */
1946 acc_sizeW
= MAX_PATH
;
1948 LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, domainW
, &dom_sizeW
, &use
);
1949 /* this can fail or succeed depending on OS version but the size will always be returned */
1950 ok(dom_sizeW
== real_dom_sizeW
+ 1,
1951 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1952 real_dom_sizeW
+ 1, dom_sizeW
);
1954 /* try a 0 sized domain buffer */
1956 acc_sizeW
= MAX_PATH
;
1957 LookupAccountSidW(NULL
, pUsersSid
, accountW
, &acc_sizeW
, NULL
, &dom_sizeW
, &use
);
1958 /* this can fail or succeed depending on OS version but the size will always be returned */
1959 ok(dom_sizeW
== real_dom_sizeW
+ 1,
1960 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1961 real_dom_sizeW
+ 1, dom_sizeW
);
1963 acc_sizeW
= dom_sizeW
= use
= 0;
1964 SetLastError(0xdeadbeef);
1965 ret
= LookupAccountSidW(NULL
, pUsersSid
, NULL
, &acc_sizeW
, NULL
, &dom_sizeW
, &use
);
1966 error
= GetLastError();
1967 ok(!ret
, "LookupAccountSidW failed %u\n", GetLastError());
1968 ok(error
== ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error
);
1969 ok(acc_sizeW
, "expected non-zero account size\n");
1970 ok(dom_sizeW
, "expected non-zero domain size\n");
1971 ok(!use
, "expected zero use %u\n", use
);
1975 /* Test LookupAccountSid with Sid retrieved from token information.
1976 This assumes this process is running under the account of the current user.*/
1977 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
|TOKEN_DUPLICATE
, &hToken
);
1978 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
1979 ret
= GetTokenInformation(hToken
, TokenUser
, NULL
, 0, &cbti
);
1980 ok(!ret
, "GetTokenInformation failed with error %d\n", GetLastError());
1981 ptiUser
= HeapAlloc(GetProcessHeap(), 0, cbti
);
1982 if (GetTokenInformation(hToken
, TokenUser
, ptiUser
, cbti
, &cbti
))
1984 acc_sizeA
= dom_sizeA
= MAX_PATH
;
1985 ret
= LookupAccountSidA(NULL
, ptiUser
->User
.Sid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
);
1986 ok(ret
, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1987 user_sizeA
= MAX_PATH
;
1988 ret
= GetUserNameA(usernameA
, &user_sizeA
);
1989 ok(ret
, "GetUserNameA() Expected TRUE, got FALSE\n");
1990 ok(lstrcmpA(usernameA
, accountA
) == 0, "LookupAccountSidA() Expected account name: %s got: %s\n", usernameA
, accountA
);
1992 HeapFree(GetProcessHeap(), 0, ptiUser
);
1994 if (pCreateWellKnownSid
&& pConvertSidToStringSidA
)
1996 trace("Well Known SIDs:\n");
1997 for (i
= 0; i
<= 60; i
++)
1999 size
= SECURITY_MAX_SID_SIZE
;
2000 if (pCreateWellKnownSid(i
, NULL
, &max_sid
.sid
, &size
))
2002 if (pConvertSidToStringSidA(&max_sid
.sid
, &str_sidA
))
2004 acc_sizeA
= MAX_PATH
;
2005 dom_sizeA
= MAX_PATH
;
2006 if (LookupAccountSidA(NULL
, &max_sid
.sid
, accountA
, &acc_sizeA
, domainA
, &dom_sizeA
, &use
))
2007 trace(" %d: %s %s\\%s %d\n", i
, str_sidA
, domainA
, accountA
, use
);
2008 LocalFree(str_sidA
);
2013 if (GetLastError() != ERROR_INVALID_PARAMETER
)
2014 trace(" CreateWellKnownSid(%d) failed: %d\n", i
, GetLastError());
2016 trace(" %d: not supported\n", i
);
2020 pLsaQueryInformationPolicy
= (void *)GetProcAddress( hmod
, "LsaQueryInformationPolicy");
2021 pLsaOpenPolicy
= (void *)GetProcAddress( hmod
, "LsaOpenPolicy");
2022 pLsaFreeMemory
= (void *)GetProcAddress( hmod
, "LsaFreeMemory");
2023 pLsaClose
= (void *)GetProcAddress( hmod
, "LsaClose");
2025 if (pLsaQueryInformationPolicy
&& pLsaOpenPolicy
&& pLsaFreeMemory
&& pLsaClose
)
2029 LSA_OBJECT_ATTRIBUTES object_attributes
;
2031 ZeroMemory(&object_attributes
, sizeof(object_attributes
));
2032 object_attributes
.Length
= sizeof(object_attributes
);
2034 status
= pLsaOpenPolicy( NULL
, &object_attributes
, POLICY_ALL_ACCESS
, &handle
);
2035 ok(status
== STATUS_SUCCESS
|| status
== STATUS_ACCESS_DENIED
,
2036 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status
);
2038 /* try a more restricted access mask if necessary */
2039 if (status
== STATUS_ACCESS_DENIED
) {
2040 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
2041 status
= pLsaOpenPolicy( NULL
, &object_attributes
, POLICY_VIEW_LOCAL_INFORMATION
, &handle
);
2042 ok(status
== STATUS_SUCCESS
, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status
);
2045 if (status
== STATUS_SUCCESS
)
2047 PPOLICY_ACCOUNT_DOMAIN_INFO info
;
2048 status
= pLsaQueryInformationPolicy(handle
, PolicyAccountDomainInformation
, (PVOID
*)&info
);
2049 ok(status
== STATUS_SUCCESS
, "LsaQueryInformationPolicy() failed, returned 0x%08x\n", status
);
2050 if (status
== STATUS_SUCCESS
)
2052 ok(info
->DomainSid
!=0, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) missing SID\n");
2053 if (info
->DomainSid
)
2055 int count
= *GetSidSubAuthorityCount(info
->DomainSid
);
2056 CopySid(GetSidLengthRequired(count
), &max_sid
, info
->DomainSid
);
2057 test_sid_str((PSID
)&max_sid
.sid
);
2058 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_USER_RID_ADMIN
;
2059 max_sid
.sid
.SubAuthorityCount
= count
+ 1;
2060 test_sid_str((PSID
)&max_sid
.sid
);
2061 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_USER_RID_GUEST
;
2062 test_sid_str((PSID
)&max_sid
.sid
);
2063 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_ADMINS
;
2064 test_sid_str((PSID
)&max_sid
.sid
);
2065 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_USERS
;
2066 test_sid_str((PSID
)&max_sid
.sid
);
2067 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_GUESTS
;
2068 test_sid_str((PSID
)&max_sid
.sid
);
2069 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_COMPUTERS
;
2070 test_sid_str((PSID
)&max_sid
.sid
);
2071 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_CONTROLLERS
;
2072 test_sid_str((PSID
)&max_sid
.sid
);
2073 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_CERT_ADMINS
;
2074 test_sid_str((PSID
)&max_sid
.sid
);
2075 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_SCHEMA_ADMINS
;
2076 test_sid_str((PSID
)&max_sid
.sid
);
2077 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_ENTERPRISE_ADMINS
;
2078 test_sid_str((PSID
)&max_sid
.sid
);
2079 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_GROUP_RID_POLICY_ADMINS
;
2080 test_sid_str((PSID
)&max_sid
.sid
);
2081 max_sid
.sid
.SubAuthority
[count
] = DOMAIN_ALIAS_RID_RAS_SERVERS
;
2082 test_sid_str((PSID
)&max_sid
.sid
);
2083 max_sid
.sid
.SubAuthority
[count
] = 1000; /* first user account */
2084 test_sid_str((PSID
)&max_sid
.sid
);
2087 pLsaFreeMemory((LPVOID
)info
);
2090 status
= pLsaClose(handle
);
2091 ok(status
== STATUS_SUCCESS
, "LsaClose() failed, returned 0x%08x\n", status
);
2097 static BOOL
get_sid_info(PSID psid
, LPSTR
*user
, LPSTR
*dom
)
2099 static CHAR account
[UNLEN
+ 1];
2100 static CHAR domain
[UNLEN
+ 1];
2101 DWORD size
, dom_size
;
2107 size
= dom_size
= UNLEN
+ 1;
2110 SetLastError(0xdeadbeef);
2111 return LookupAccountSidA(NULL
, psid
, account
, &size
, domain
, &dom_size
, &use
);
2114 static void check_wellknown_name(const char* name
, WELL_KNOWN_SID_TYPE result
)
2116 SID_IDENTIFIER_AUTHORITY ident
= { SECURITY_NT_AUTHORITY
};
2117 PSID domainsid
= NULL
;
2118 char wk_sid
[SECURITY_MAX_SID_SIZE
];
2121 DWORD sid_size
, domain_size
;
2122 SID_NAME_USE sid_use
;
2123 LPSTR domain
, account
, sid_domain
, wk_domain
, wk_account
;
2129 ret
= LookupAccountNameA(NULL
, name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2130 ok(!ret
, " %s Should have failed to lookup account name\n", name
);
2131 psid
= HeapAlloc(GetProcessHeap(),0,sid_size
);
2132 domain
= HeapAlloc(GetProcessHeap(),0,domain_size
);
2133 ret
= LookupAccountNameA(NULL
, name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2137 ok(!ret
, " %s Should have failed to lookup account name\n",name
);
2141 AllocateAndInitializeSid(&ident
, 6, SECURITY_NT_NON_UNIQUE
, 12, 23, 34, 45, 56, 0, 0, &domainsid
);
2142 cb
= sizeof(wk_sid
);
2143 if (!pCreateWellKnownSid(result
, domainsid
, wk_sid
, &cb
))
2145 win_skip("SID %i is not available on the system\n",result
);
2149 ret2
= get_sid_info(wk_sid
, &wk_account
, &wk_domain
);
2150 if (!ret2
&& GetLastError() == ERROR_NONE_MAPPED
)
2152 win_skip("CreateWellKnownSid() succeeded but the account '%s' is not present (W2K)\n", name
);
2156 get_sid_info(psid
, &account
, &sid_domain
);
2158 ok(ret
, "Failed to lookup account name %s\n",name
);
2159 ok(sid_size
!= 0, "sid_size was zero\n");
2161 ok(EqualSid(psid
,wk_sid
),"(%s) Sids fail to match well known sid!\n",name
);
2163 ok(!lstrcmpA(account
, wk_account
), "Expected %s , got %s\n", account
, wk_account
);
2164 ok(!lstrcmpA(domain
, wk_domain
), "Expected %s, got %s\n", wk_domain
, domain
);
2165 ok(sid_use
== SidTypeWellKnownGroup
, "Expected Use (5), got %d\n", sid_use
);
2169 HeapFree(GetProcessHeap(),0,psid
);
2170 HeapFree(GetProcessHeap(),0,domain
);
2173 static void test_LookupAccountName(void)
2175 DWORD sid_size
, domain_size
, user_size
;
2176 DWORD sid_save
, domain_save
;
2177 CHAR user_name
[UNLEN
+ 1];
2178 CHAR computer_name
[UNLEN
+ 1];
2179 SID_NAME_USE sid_use
;
2180 LPSTR domain
, account
, sid_dom
;
2184 /* native crashes if (assuming all other parameters correct):
2186 * - Sid is NULL and cbSid is > 0
2187 * - cbSid or cchReferencedDomainName are NULL
2188 * - ReferencedDomainName is NULL and cchReferencedDomainName is the correct size
2191 user_size
= UNLEN
+ 1;
2192 SetLastError(0xdeadbeef);
2193 ret
= GetUserNameA(user_name
, &user_size
);
2194 ok(ret
, "Failed to get user name : %d\n", GetLastError());
2199 sid_use
= 0xcafebabe;
2200 SetLastError(0xdeadbeef);
2201 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2202 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
2204 win_skip("LookupAccountNameA is not implemented\n");
2207 ok(!ret
, "Expected 0, got %d\n", ret
);
2208 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2209 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2210 ok(sid_size
!= 0, "Expected non-zero sid size\n");
2211 ok(domain_size
!= 0, "Expected non-zero domain size\n");
2212 ok(sid_use
== 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use
);
2214 sid_save
= sid_size
;
2215 domain_save
= domain_size
;
2217 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
2218 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
2220 /* try valid account name */
2221 ret
= LookupAccountNameA(NULL
, user_name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2222 get_sid_info(psid
, &account
, &sid_dom
);
2223 ok(ret
, "Failed to lookup account name\n");
2224 ok(sid_size
== GetLengthSid(psid
), "Expected %d, got %d\n", GetLengthSid(psid
), sid_size
);
2225 ok(!lstrcmpA(account
, user_name
), "Expected %s, got %s\n", user_name
, account
);
2226 ok(!lstrcmpA(domain
, sid_dom
), "Expected %s, got %s\n", sid_dom
, domain
);
2227 ok(domain_size
== domain_save
- 1, "Expected %d, got %d\n", domain_save
- 1, domain_size
);
2228 ok(strlen(domain
) == domain_size
, "Expected %d, got %d\n", lstrlenA(domain
), domain_size
);
2229 ok(sid_use
== SidTypeUser
, "Expected SidTypeUser (%d), got %d\n", SidTypeUser
, sid_use
);
2230 domain_size
= domain_save
;
2231 sid_size
= sid_save
;
2233 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2235 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2239 ret
= LookupAccountNameA(NULL
, "Everyone", psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2240 get_sid_info(psid
, &account
, &sid_dom
);
2241 ok(ret
, "Failed to lookup account name\n");
2242 ok(sid_size
!= 0, "sid_size was zero\n");
2243 ok(!lstrcmpA(account
, "Everyone"), "Expected Everyone, got %s\n", account
);
2244 ok(!lstrcmpA(domain
, sid_dom
), "Expected %s, got %s\n", sid_dom
, domain
);
2245 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2246 ok(strlen(domain
) == domain_size
, "Expected %d, got %d\n", lstrlenA(domain
), domain_size
);
2247 ok(sid_use
== SidTypeWellKnownGroup
, "Expected SidTypeWellKnownGroup (%d), got %d\n", SidTypeWellKnownGroup
, sid_use
);
2248 domain_size
= domain_save
;
2251 /* NULL Sid with zero sid size */
2252 SetLastError(0xdeadbeef);
2254 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, domain
, &domain_size
, &sid_use
);
2255 ok(!ret
, "Expected 0, got %d\n", ret
);
2256 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2257 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2258 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
2259 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
2261 /* try cchReferencedDomainName - 1 */
2262 SetLastError(0xdeadbeef);
2264 ret
= LookupAccountNameA(NULL
, user_name
, NULL
, &sid_size
, domain
, &domain_size
, &sid_use
);
2265 ok(!ret
, "Expected 0, got %d\n", ret
);
2266 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2267 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2268 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
2269 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
2271 /* NULL ReferencedDomainName with zero domain name size */
2272 SetLastError(0xdeadbeef);
2274 ret
= LookupAccountNameA(NULL
, user_name
, psid
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2275 ok(!ret
, "Expected 0, got %d\n", ret
);
2276 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2277 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2278 ok(sid_size
== sid_save
, "Expected %d, got %d\n", sid_save
, sid_size
);
2279 ok(domain_size
== domain_save
, "Expected %d, got %d\n", domain_save
, domain_size
);
2281 HeapFree(GetProcessHeap(), 0, psid
);
2282 HeapFree(GetProcessHeap(), 0, domain
);
2284 /* get sizes for NULL account name */
2287 sid_use
= 0xcafebabe;
2288 SetLastError(0xdeadbeef);
2289 ret
= LookupAccountNameA(NULL
, NULL
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2290 if (!ret
&& GetLastError() == ERROR_NONE_MAPPED
)
2291 win_skip("NULL account name doesn't work on NT4\n");
2294 ok(!ret
, "Expected 0, got %d\n", ret
);
2295 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2296 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2297 ok(sid_size
!= 0, "Expected non-zero sid size\n");
2298 ok(domain_size
!= 0, "Expected non-zero domain size\n");
2299 ok(sid_use
== 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use
);
2301 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
2302 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
2304 /* try NULL account name */
2305 ret
= LookupAccountNameA(NULL
, NULL
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2306 get_sid_info(psid
, &account
, &sid_dom
);
2307 ok(ret
, "Failed to lookup account name\n");
2308 /* Using a fixed string will not work on different locales */
2309 ok(!lstrcmpA(account
, domain
),
2310 "Got %s for account and %s for domain, these should be the same\n",
2312 ok(sid_use
== SidTypeDomain
, "Expected SidTypeDomain (%d), got %d\n", SidTypeDomain
, sid_use
);
2314 HeapFree(GetProcessHeap(), 0, psid
);
2315 HeapFree(GetProcessHeap(), 0, domain
);
2318 /* try an invalid account name */
2319 SetLastError(0xdeadbeef);
2322 ret
= LookupAccountNameA(NULL
, "oogabooga", NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2323 ok(!ret
, "Expected 0, got %d\n", ret
);
2324 ok(GetLastError() == ERROR_NONE_MAPPED
||
2325 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE
),
2326 "Expected ERROR_NONE_MAPPED, got %d\n", GetLastError());
2327 ok(sid_size
== 0, "Expected 0, got %d\n", sid_size
);
2328 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2330 /* try an invalid system name */
2331 SetLastError(0xdeadbeef);
2334 ret
= LookupAccountNameA("deepthought", NULL
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2335 ok(!ret
, "Expected 0, got %d\n", ret
);
2336 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE
|| GetLastError() == RPC_S_INVALID_NET_ADDR
/* Vista */,
2337 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
2338 ok(sid_size
== 0, "Expected 0, got %d\n", sid_size
);
2339 ok(domain_size
== 0, "Expected 0, got %d\n", domain_size
);
2341 /* try with the computer name as the account name */
2342 domain_size
= sizeof(computer_name
);
2343 GetComputerNameA(computer_name
, &domain_size
);
2346 ret
= LookupAccountNameA(NULL
, computer_name
, NULL
, &sid_size
, NULL
, &domain_size
, &sid_use
);
2347 ok(!ret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
||
2348 GetLastError() == ERROR_NONE_MAPPED
/* in a domain */ ||
2349 broken(GetLastError() == ERROR_TRUSTED_DOMAIN_FAILURE
) ||
2350 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE
)),
2351 "LookupAccountNameA failed: %d\n", GetLastError());
2352 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
2354 psid
= HeapAlloc(GetProcessHeap(), 0, sid_size
);
2355 domain
= HeapAlloc(GetProcessHeap(), 0, domain_size
);
2356 ret
= LookupAccountNameA(NULL
, computer_name
, psid
, &sid_size
, domain
, &domain_size
, &sid_use
);
2357 ok(ret
, "LookupAccountNameA failed: %d\n", GetLastError());
2358 ok(sid_use
== SidTypeDomain
||
2359 (sid_use
== SidTypeUser
&& ! strcmp(computer_name
, user_name
)), "expected SidTypeDomain for %s, got %d\n", computer_name
, sid_use
);
2360 HeapFree(GetProcessHeap(), 0, domain
);
2361 HeapFree(GetProcessHeap(), 0, psid
);
2364 /* Well Known names */
2365 if (!pCreateWellKnownSid
)
2367 win_skip("CreateWellKnownSid not available\n");
2371 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2373 skip("Non-English locale (skipping well known name creation tests)\n");
2377 check_wellknown_name("LocalService", WinLocalServiceSid
);
2378 check_wellknown_name("Local Service", WinLocalServiceSid
);
2380 check_wellknown_name("Local Service", 0);
2381 check_wellknown_name("NetworkService", WinNetworkServiceSid
);
2382 check_wellknown_name("Network Service", WinNetworkServiceSid
);
2384 /* example of some names where the spaces are not optional */
2385 check_wellknown_name("Terminal Server User", WinTerminalServerSid
);
2386 check_wellknown_name("TerminalServer User", 0);
2387 check_wellknown_name("TerminalServerUser", 0);
2388 check_wellknown_name("Terminal ServerUser", 0);
2390 check_wellknown_name("enterprise domain controllers",WinEnterpriseControllersSid
);
2391 check_wellknown_name("enterprisedomain controllers", 0);
2392 check_wellknown_name("enterprise domaincontrollers", 0);
2393 check_wellknown_name("enterprisedomaincontrollers", 0);
2395 /* case insensitivity */
2396 check_wellknown_name("lOCAlServICE", WinLocalServiceSid
);
2398 /* fully qualified account names */
2399 check_wellknown_name("NT AUTHORITY\\LocalService", WinLocalServiceSid
);
2400 check_wellknown_name("nt authority\\Network Service", WinNetworkServiceSid
);
2401 check_wellknown_name("nt authority test\\Network Service", 0);
2402 check_wellknown_name("Dummy\\Network Service", 0);
2403 check_wellknown_name("ntauthority\\Network Service", 0);
2406 static void test_security_descriptor(void)
2408 SECURITY_DESCRIPTOR sd
;
2411 BOOL isDefault
, isPresent
, ret
;
2415 SetLastError(0xdeadbeef);
2416 ret
= InitializeSecurityDescriptor(&sd
, SECURITY_DESCRIPTOR_REVISION
);
2417 if (ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2419 win_skip("InitializeSecurityDescriptor is not implemented\n");
2423 ok(GetSecurityDescriptorOwner(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorOwner failed\n");
2424 expect_eq(psid
, NULL
, PSID
, "%p");
2425 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2426 sd
.Control
|= SE_DACL_PRESENT
| SE_SACL_PRESENT
;
2428 SetLastError(0xdeadbeef);
2430 expect_eq(MakeSelfRelativeSD(&sd
, buf
, &size
), FALSE
, BOOL
, "%d");
2431 expect_eq(GetLastError(), ERROR_INSUFFICIENT_BUFFER
, DWORD
, "%u");
2432 ok(size
> 5, "Size not increased\n");
2435 expect_eq(MakeSelfRelativeSD(&sd
, buf
, &size
), TRUE
, BOOL
, "%d");
2436 ok(GetSecurityDescriptorOwner(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorOwner failed\n");
2437 expect_eq(psid
, NULL
, PSID
, "%p");
2438 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2439 ok(GetSecurityDescriptorGroup(&sd
, &psid
, &isDefault
), "GetSecurityDescriptorGroup failed\n");
2440 expect_eq(psid
, NULL
, PSID
, "%p");
2441 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2442 ok(GetSecurityDescriptorDacl(&sd
, &isPresent
, &pacl
, &isDefault
), "GetSecurityDescriptorDacl failed\n");
2443 expect_eq(isPresent
, TRUE
, BOOL
, "%d");
2444 expect_eq(psid
, NULL
, PSID
, "%p");
2445 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2446 ok(GetSecurityDescriptorSacl(&sd
, &isPresent
, &pacl
, &isDefault
), "GetSecurityDescriptorSacl failed\n");
2447 expect_eq(isPresent
, TRUE
, BOOL
, "%d");
2448 expect_eq(psid
, NULL
, PSID
, "%p");
2449 expect_eq(isDefault
, FALSE
, BOOL
, "%d");
2453 #define TEST_GRANTED_ACCESS(a,b) test_granted_access(a,b,0,__LINE__)
2454 #define TEST_GRANTED_ACCESS2(a,b,c) test_granted_access(a,b,c,__LINE__)
2455 static void test_granted_access(HANDLE handle
, ACCESS_MASK access
,
2456 ACCESS_MASK alt
, int line
)
2458 OBJECT_BASIC_INFORMATION obj_info
;
2461 if (!pNtQueryObject
)
2463 skip_(__FILE__
, line
)("Not NT platform - skipping tests\n");
2467 status
= pNtQueryObject( handle
, ObjectBasicInformation
, &obj_info
,
2468 sizeof(obj_info
), NULL
);
2469 ok_(__FILE__
, line
)(!status
, "NtQueryObject with err: %08x\n", status
);
2471 ok_(__FILE__
, line
)(obj_info
.GrantedAccess
== access
||
2472 obj_info
.GrantedAccess
== alt
, "Granted access should be 0x%08x "
2473 "or 0x%08x, instead of 0x%08x\n", access
, alt
, obj_info
.GrantedAccess
);
2475 ok_(__FILE__
, line
)(obj_info
.GrantedAccess
== access
, "Granted access should "
2476 "be 0x%08x, instead of 0x%08x\n", access
, obj_info
.GrantedAccess
);
2479 #define CHECK_SET_SECURITY(o,i,e) \
2483 SetLastError( 0xdeadbeef ); \
2484 res_ = SetKernelObjectSecurity( o, i, SecurityDescriptor ); \
2485 err = GetLastError(); \
2486 if (e == ERROR_SUCCESS) \
2487 ok(res_, "SetKernelObjectSecurity failed with %d\n", err); \
2489 ok(!res_ && err == e, "SetKernelObjectSecurity should have failed " \
2490 "with %s, instead of %d\n", #e, err); \
2493 static void test_process_security(void)
2497 PTOKEN_PRIMARY_GROUP group
;
2498 PSID AdminSid
= NULL
, UsersSid
= NULL
;
2500 SECURITY_DESCRIPTOR
*SecurityDescriptor
= NULL
;
2501 char buffer
[MAX_PATH
];
2502 PROCESS_INFORMATION info
;
2503 STARTUPINFOA startup
;
2504 SECURITY_ATTRIBUTES psa
;
2505 HANDLE token
, event
;
2508 Acl
= HeapAlloc(GetProcessHeap(), 0, 256);
2509 res
= InitializeAcl(Acl
, 256, ACL_REVISION
);
2510 if (!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2512 win_skip("ACLs not implemented - skipping tests\n");
2513 HeapFree(GetProcessHeap(), 0, Acl
);
2516 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
2518 /* get owner from the token we might be running as a user not admin */
2519 res
= OpenProcessToken( GetCurrentProcess(), MAXIMUM_ALLOWED
, &token
);
2520 ok(res
, "OpenProcessToken failed with error %d\n", GetLastError());
2523 HeapFree(GetProcessHeap(), 0, Acl
);
2527 res
= GetTokenInformation( token
, TokenOwner
, NULL
, 0, &size
);
2528 ok(!res
, "Expected failure, got %d\n", res
);
2529 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2530 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2532 owner
= HeapAlloc(GetProcessHeap(), 0, size
);
2533 res
= GetTokenInformation( token
, TokenOwner
, owner
, size
, &size
);
2534 ok(res
, "GetTokenInformation failed with error %d\n", GetLastError());
2535 AdminSid
= ((TOKEN_OWNER
*)owner
)->Owner
;
2537 res
= GetTokenInformation( token
, TokenPrimaryGroup
, NULL
, 0, &size
);
2538 ok(!res
, "Expected failure, got %d\n", res
);
2539 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
2540 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2542 group
= HeapAlloc(GetProcessHeap(), 0, size
);
2543 res
= GetTokenInformation( token
, TokenPrimaryGroup
, group
, size
, &size
);
2544 ok(res
, "GetTokenInformation failed with error %d\n", GetLastError());
2545 UsersSid
= ((TOKEN_PRIMARY_GROUP
*)group
)->PrimaryGroup
;
2547 CloseHandle( token
);
2550 HeapFree(GetProcessHeap(), 0, group
);
2551 HeapFree(GetProcessHeap(), 0, owner
);
2552 HeapFree(GetProcessHeap(), 0, Acl
);
2556 res
= AddAccessDeniedAce(Acl
, ACL_REVISION
, PROCESS_VM_READ
, AdminSid
);
2557 ok(res
, "AddAccessDeniedAce failed with error %d\n", GetLastError());
2558 res
= AddAccessAllowedAce(Acl
, ACL_REVISION
, PROCESS_ALL_ACCESS
, AdminSid
);
2559 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2561 SecurityDescriptor
= HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
2562 res
= InitializeSecurityDescriptor(SecurityDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
2563 ok(res
, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
2565 event
= CreateEventA( NULL
, TRUE
, TRUE
, "test_event" );
2566 ok(event
!= NULL
, "CreateEvent %d\n", GetLastError());
2568 SecurityDescriptor
->Revision
= 0;
2569 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_UNKNOWN_REVISION
);
2570 SecurityDescriptor
->Revision
= SECURITY_DESCRIPTOR_REVISION
;
2572 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_INVALID_SECURITY_DESCR
);
2573 CHECK_SET_SECURITY( event
, GROUP_SECURITY_INFORMATION
, ERROR_INVALID_SECURITY_DESCR
);
2574 CHECK_SET_SECURITY( event
, SACL_SECURITY_INFORMATION
, ERROR_ACCESS_DENIED
);
2575 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2576 /* NULL DACL is valid and means that everyone has access */
2577 SecurityDescriptor
->Control
|= SE_DACL_PRESENT
;
2578 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2580 /* Set owner and group and dacl */
2581 res
= SetSecurityDescriptorOwner(SecurityDescriptor
, AdminSid
, FALSE
);
2582 ok(res
, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
2583 CHECK_SET_SECURITY( event
, OWNER_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2584 res
= SetSecurityDescriptorGroup(SecurityDescriptor
, UsersSid
, FALSE
);
2585 ok(res
, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2586 CHECK_SET_SECURITY( event
, GROUP_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2587 res
= SetSecurityDescriptorDacl(SecurityDescriptor
, TRUE
, Acl
, FALSE
);
2588 ok(res
, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
2589 CHECK_SET_SECURITY( event
, DACL_SECURITY_INFORMATION
, ERROR_SUCCESS
);
2591 sprintf(buffer
, "%s tests/security.c test", myARGV
[0]);
2592 memset(&startup
, 0, sizeof(startup
));
2593 startup
.cb
= sizeof(startup
);
2594 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
2595 startup
.wShowWindow
= SW_SHOWNORMAL
;
2597 psa
.nLength
= sizeof(psa
);
2598 psa
.lpSecurityDescriptor
= SecurityDescriptor
;
2599 psa
.bInheritHandle
= TRUE
;
2601 /* Doesn't matter what ACL say we should get full access for ourselves */
2602 res
= CreateProcessA( NULL
, buffer
, &psa
, NULL
, FALSE
, 0, NULL
, NULL
, &startup
, &info
);
2603 ok(res
, "CreateProcess with err:%d\n", GetLastError());
2604 TEST_GRANTED_ACCESS2( info
.hProcess
, PROCESS_ALL_ACCESS_NT4
,
2605 STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
);
2606 winetest_wait_child_process( info
.hProcess
);
2608 CloseHandle( info
.hProcess
);
2609 CloseHandle( info
.hThread
);
2610 CloseHandle( event
);
2611 HeapFree(GetProcessHeap(), 0, group
);
2612 HeapFree(GetProcessHeap(), 0, owner
);
2613 HeapFree(GetProcessHeap(), 0, Acl
);
2614 HeapFree(GetProcessHeap(), 0, SecurityDescriptor
);
2617 static void test_process_security_child(void)
2619 HANDLE handle
, handle1
;
2623 handle
= OpenProcess( PROCESS_TERMINATE
, FALSE
, GetCurrentProcessId() );
2624 ok(handle
!= NULL
, "OpenProcess(PROCESS_TERMINATE) with err:%d\n", GetLastError());
2625 TEST_GRANTED_ACCESS( handle
, PROCESS_TERMINATE
);
2627 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2628 &handle1
, 0, TRUE
, DUPLICATE_SAME_ACCESS
);
2629 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2630 TEST_GRANTED_ACCESS( handle1
, PROCESS_TERMINATE
);
2632 CloseHandle( handle1
);
2634 SetLastError( 0xdeadbeef );
2635 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2636 &handle1
, PROCESS_ALL_ACCESS
, TRUE
, 0 );
2637 err
= GetLastError();
2639 ok(!ret
&& err
== ERROR_ACCESS_DENIED
, "duplicating handle should have failed "
2640 "with STATUS_ACCESS_DENIED, instead of err:%d\n", err
);
2642 CloseHandle( handle
);
2644 /* These two should fail - they are denied by ACL */
2645 handle
= OpenProcess( PROCESS_VM_READ
, FALSE
, GetCurrentProcessId() );
2647 ok(handle
== NULL
, "OpenProcess(PROCESS_VM_READ) should have failed\n");
2648 handle
= OpenProcess( PROCESS_ALL_ACCESS
, FALSE
, GetCurrentProcessId() );
2650 ok(handle
== NULL
, "OpenProcess(PROCESS_ALL_ACCESS) should have failed\n");
2652 /* Documented privilege elevation */
2653 ret
= DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2654 &handle
, 0, TRUE
, DUPLICATE_SAME_ACCESS
);
2655 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2656 TEST_GRANTED_ACCESS2( handle
, PROCESS_ALL_ACCESS_NT4
,
2657 STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
);
2659 CloseHandle( handle
);
2661 /* Same only explicitly asking for all access rights */
2662 ret
= DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2663 &handle
, PROCESS_ALL_ACCESS
, TRUE
, 0 );
2664 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2665 TEST_GRANTED_ACCESS2( handle
, PROCESS_ALL_ACCESS_NT4
,
2666 PROCESS_ALL_ACCESS
| PROCESS_QUERY_LIMITED_INFORMATION
);
2667 ret
= DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(),
2668 &handle1
, PROCESS_VM_READ
, TRUE
, 0 );
2669 ok(ret
, "duplicating handle err:%d\n", GetLastError());
2670 TEST_GRANTED_ACCESS( handle1
, PROCESS_VM_READ
);
2671 CloseHandle( handle1
);
2672 CloseHandle( handle
);
2675 static void test_impersonation_level(void)
2677 HANDLE Token
, ProcessToken
;
2680 TOKEN_PRIVILEGES
*Privileges
;
2682 PRIVILEGE_SET
*PrivilegeSet
;
2688 pDuplicateTokenEx
= (void *)GetProcAddress(hmod
, "DuplicateTokenEx");
2689 if( !pDuplicateTokenEx
) {
2690 win_skip("DuplicateTokenEx is not available\n");
2693 SetLastError(0xdeadbeef);
2694 ret
= ImpersonateSelf(SecurityAnonymous
);
2695 if(!ret
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
2697 win_skip("ImpersonateSelf is not implemented\n");
2700 ok(ret
, "ImpersonateSelf(SecurityAnonymous) failed with error %d\n", GetLastError());
2701 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2702 ok(!ret
, "OpenThreadToken should have failed\n");
2703 error
= GetLastError();
2704 ok(error
== ERROR_CANT_OPEN_ANONYMOUS
, "OpenThreadToken on anonymous token should have returned ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error
);
2705 /* can't perform access check when opening object against an anonymous impersonation token */
2707 error
= RegOpenKeyExA(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2708 ok(error
== ERROR_INVALID_HANDLE
|| error
== ERROR_CANT_OPEN_ANONYMOUS
|| error
== ERROR_BAD_IMPERSONATION_LEVEL
,
2709 "RegOpenKeyEx failed with %d\n", error
);
2713 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
, &ProcessToken
);
2714 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
2716 ret
= pDuplicateTokenEx(ProcessToken
,
2717 TOKEN_QUERY
| TOKEN_DUPLICATE
| TOKEN_IMPERSONATE
, NULL
,
2718 SecurityAnonymous
, TokenImpersonation
, &Token
);
2719 ok(ret
, "DuplicateTokenEx failed with error %d\n", GetLastError());
2720 /* can't increase the impersonation level */
2721 ret
= DuplicateToken(Token
, SecurityIdentification
, &Token2
);
2722 error
= GetLastError();
2723 ok(!ret
&& error
== ERROR_BAD_IMPERSONATION_LEVEL
,
2724 "Duplicating a token and increasing the impersonation level should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2725 /* we can query anything from an anonymous token, including the user */
2726 ret
= GetTokenInformation(Token
, TokenUser
, NULL
, 0, &Size
);
2727 error
= GetLastError();
2728 ok(!ret
&& error
== ERROR_INSUFFICIENT_BUFFER
, "GetTokenInformation(TokenUser) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error
);
2729 User
= HeapAlloc(GetProcessHeap(), 0, Size
);
2730 ret
= GetTokenInformation(Token
, TokenUser
, User
, Size
, &Size
);
2731 ok(ret
, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
2732 HeapFree(GetProcessHeap(), 0, User
);
2734 /* PrivilegeCheck fails with SecurityAnonymous level */
2735 ret
= GetTokenInformation(Token
, TokenPrivileges
, NULL
, 0, &Size
);
2736 error
= GetLastError();
2737 ok(!ret
&& error
== ERROR_INSUFFICIENT_BUFFER
, "GetTokenInformation(TokenPrivileges) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error
);
2738 Privileges
= HeapAlloc(GetProcessHeap(), 0, Size
);
2739 ret
= GetTokenInformation(Token
, TokenPrivileges
, Privileges
, Size
, &Size
);
2740 ok(ret
, "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
2742 PrivilegeSet
= HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PRIVILEGE_SET
, Privilege
[Privileges
->PrivilegeCount
]));
2743 PrivilegeSet
->PrivilegeCount
= Privileges
->PrivilegeCount
;
2744 memcpy(PrivilegeSet
->Privilege
, Privileges
->Privileges
, PrivilegeSet
->PrivilegeCount
* sizeof(PrivilegeSet
->Privilege
[0]));
2745 PrivilegeSet
->Control
= PRIVILEGE_SET_ALL_NECESSARY
;
2746 HeapFree(GetProcessHeap(), 0, Privileges
);
2748 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2749 error
= GetLastError();
2750 ok(!ret
&& error
== ERROR_BAD_IMPERSONATION_LEVEL
, "PrivilegeCheck for SecurityAnonymous token should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2754 ret
= ImpersonateSelf(SecurityIdentification
);
2755 ok(ret
, "ImpersonateSelf(SecurityIdentification) failed with error %d\n", GetLastError());
2756 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2757 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
2759 /* can't perform access check when opening object against an identification impersonation token */
2760 error
= RegOpenKeyExA(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2762 ok(error
== ERROR_INVALID_HANDLE
|| error
== ERROR_BAD_IMPERSONATION_LEVEL
,
2763 "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE or ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error
);
2765 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2766 ok(ret
, "PrivilegeCheck for SecurityIdentification failed with error %d\n", GetLastError());
2770 ret
= ImpersonateSelf(SecurityImpersonation
);
2771 ok(ret
, "ImpersonateSelf(SecurityImpersonation) failed with error %d\n", GetLastError());
2772 ret
= OpenThreadToken(GetCurrentThread(), TOKEN_QUERY
| TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY_SOURCE
| TOKEN_IMPERSONATE
| TOKEN_ADJUST_DEFAULT
, TRUE
, &Token
);
2773 ok(ret
, "OpenThreadToken failed with error %d\n", GetLastError());
2774 error
= RegOpenKeyExA(HKEY_CURRENT_USER
, "Software", 0, KEY_READ
, &hkey
);
2775 ok(error
== ERROR_SUCCESS
, "RegOpenKeyEx should have succeeded instead of failing with %d\n", error
);
2777 ret
= PrivilegeCheck(Token
, PrivilegeSet
, &AccessGranted
);
2778 ok(ret
, "PrivilegeCheck for SecurityImpersonation failed with error %d\n", GetLastError());
2782 CloseHandle(ProcessToken
);
2784 HeapFree(GetProcessHeap(), 0, PrivilegeSet
);
2787 static void test_SetEntriesInAclW(void)
2790 PSID EveryoneSid
= NULL
, UsersSid
= NULL
;
2791 PACL OldAcl
= NULL
, NewAcl
;
2792 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
2793 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
2794 EXPLICIT_ACCESSW ExplicitAccess
;
2795 static const WCHAR wszEveryone
[] = {'E','v','e','r','y','o','n','e',0};
2796 static const WCHAR wszCurrentUser
[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
2798 if (!pSetEntriesInAclW
)
2800 win_skip("SetEntriesInAclW is not available\n");
2804 NewAcl
= (PACL
)0xdeadbeef;
2805 res
= pSetEntriesInAclW(0, NULL
, NULL
, &NewAcl
);
2806 if(res
== ERROR_CALL_NOT_IMPLEMENTED
)
2808 win_skip("SetEntriesInAclW is not implemented\n");
2811 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2812 ok(NewAcl
== NULL
||
2813 broken(NewAcl
!= NULL
), /* NT4 */
2814 "NewAcl=%p, expected NULL\n", NewAcl
);
2817 OldAcl
= HeapAlloc(GetProcessHeap(), 0, 256);
2818 res
= InitializeAcl(OldAcl
, 256, ACL_REVISION
);
2819 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2821 win_skip("ACLs not implemented - skipping tests\n");
2822 HeapFree(GetProcessHeap(), 0, OldAcl
);
2825 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
2827 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
2828 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2830 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
2831 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
2832 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2834 res
= AddAccessAllowedAce(OldAcl
, ACL_REVISION
, KEY_READ
, UsersSid
);
2835 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2837 ExplicitAccess
.grfAccessPermissions
= KEY_WRITE
;
2838 ExplicitAccess
.grfAccessMode
= GRANT_ACCESS
;
2839 ExplicitAccess
.grfInheritance
= NO_INHERITANCE
;
2840 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_WELL_KNOWN_GROUP
;
2841 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2842 ExplicitAccess
.Trustee
.ptstrName
= EveryoneSid
;
2843 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= 0xDEADBEEF;
2844 ExplicitAccess
.Trustee
.pMultipleTrustee
= (PVOID
)0xDEADBEEF;
2845 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2846 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2847 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2850 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2851 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
2852 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2853 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2854 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2855 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2858 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2860 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2864 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2865 ExplicitAccess
.Trustee
.ptstrName
= (LPWSTR
)wszEveryone
;
2866 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2867 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2868 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2871 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_BAD_FORM
;
2872 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2873 ok(res
== ERROR_INVALID_PARAMETER
||
2874 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2875 "SetEntriesInAclW failed: %u\n", res
);
2876 ok(NewAcl
== NULL
||
2877 broken(NewAcl
!= NULL
), /* NT4 */
2878 "returned acl wasn't NULL: %p\n", NewAcl
);
2880 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2881 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= TRUSTEE_IS_IMPERSONATE
;
2882 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2883 ok(res
== ERROR_INVALID_PARAMETER
||
2884 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
2885 "SetEntriesInAclW failed: %u\n", res
);
2886 ok(NewAcl
== NULL
||
2887 broken(NewAcl
!= NULL
), /* NT4 */
2888 "returned acl wasn't NULL: %p\n", NewAcl
);
2890 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2891 ExplicitAccess
.grfAccessMode
= SET_ACCESS
;
2892 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2893 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2894 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2898 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2899 ExplicitAccess
.Trustee
.ptstrName
= (LPWSTR
)wszCurrentUser
;
2900 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2901 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2902 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2905 ExplicitAccess
.grfAccessMode
= REVOKE_ACCESS
;
2906 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2907 ExplicitAccess
.Trustee
.ptstrName
= UsersSid
;
2908 res
= pSetEntriesInAclW(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2909 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclW failed: %u\n", res
);
2910 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2914 FreeSid(EveryoneSid
);
2915 HeapFree(GetProcessHeap(), 0, OldAcl
);
2918 static void test_SetEntriesInAclA(void)
2921 PSID EveryoneSid
= NULL
, UsersSid
= NULL
;
2922 PACL OldAcl
= NULL
, NewAcl
;
2923 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
2924 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
2925 EXPLICIT_ACCESSA ExplicitAccess
;
2926 static const CHAR szEveryone
[] = {'E','v','e','r','y','o','n','e',0};
2927 static const CHAR szCurrentUser
[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
2929 if (!pSetEntriesInAclA
)
2931 win_skip("SetEntriesInAclA is not available\n");
2935 NewAcl
= (PACL
)0xdeadbeef;
2936 res
= pSetEntriesInAclA(0, NULL
, NULL
, &NewAcl
);
2937 if(res
== ERROR_CALL_NOT_IMPLEMENTED
)
2939 win_skip("SetEntriesInAclA is not implemented\n");
2942 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2943 ok(NewAcl
== NULL
||
2944 broken(NewAcl
!= NULL
), /* NT4 */
2945 "NewAcl=%p, expected NULL\n", NewAcl
);
2948 OldAcl
= HeapAlloc(GetProcessHeap(), 0, 256);
2949 res
= InitializeAcl(OldAcl
, 256, ACL_REVISION
);
2950 if(!res
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2952 win_skip("ACLs not implemented - skipping tests\n");
2953 HeapFree(GetProcessHeap(), 0, OldAcl
);
2956 ok(res
, "InitializeAcl failed with error %d\n", GetLastError());
2958 res
= AllocateAndInitializeSid( &SIDAuthWorld
, 1, SECURITY_WORLD_RID
, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid
);
2959 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2961 res
= AllocateAndInitializeSid( &SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
2962 DOMAIN_ALIAS_RID_USERS
, 0, 0, 0, 0, 0, 0, &UsersSid
);
2963 ok(res
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2965 res
= AddAccessAllowedAce(OldAcl
, ACL_REVISION
, KEY_READ
, UsersSid
);
2966 ok(res
, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2968 ExplicitAccess
.grfAccessPermissions
= KEY_WRITE
;
2969 ExplicitAccess
.grfAccessMode
= GRANT_ACCESS
;
2970 ExplicitAccess
.grfInheritance
= NO_INHERITANCE
;
2971 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_WELL_KNOWN_GROUP
;
2972 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
2973 ExplicitAccess
.Trustee
.ptstrName
= EveryoneSid
;
2974 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2975 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
2976 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2977 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2978 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2981 ExplicitAccess
.Trustee
.TrusteeType
= TRUSTEE_IS_UNKNOWN
;
2982 ExplicitAccess
.Trustee
.pMultipleTrustee
= NULL
;
2983 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
2984 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2985 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2986 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
2989 if (PRIMARYLANGID(GetSystemDefaultLangID()) != LANG_ENGLISH
)
2991 skip("Non-English locale (test with hardcoded 'Everyone')\n");
2995 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
2996 ExplicitAccess
.Trustee
.ptstrName
= (LPSTR
)szEveryone
;
2997 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
2998 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
2999 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3002 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_BAD_FORM
;
3003 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3004 ok(res
== ERROR_INVALID_PARAMETER
||
3005 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
3006 "SetEntriesInAclA failed: %u\n", res
);
3007 ok(NewAcl
== NULL
||
3008 broken(NewAcl
!= NULL
), /* NT4 */
3009 "returned acl wasn't NULL: %p\n", NewAcl
);
3011 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
3012 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= TRUSTEE_IS_IMPERSONATE
;
3013 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3014 ok(res
== ERROR_INVALID_PARAMETER
||
3015 broken(res
== ERROR_NOT_SUPPORTED
), /* NT4 */
3016 "SetEntriesInAclA failed: %u\n", res
);
3017 ok(NewAcl
== NULL
||
3018 broken(NewAcl
!= NULL
), /* NT4 */
3019 "returned acl wasn't NULL: %p\n", NewAcl
);
3021 ExplicitAccess
.Trustee
.MultipleTrusteeOperation
= NO_MULTIPLE_TRUSTEE
;
3022 ExplicitAccess
.grfAccessMode
= SET_ACCESS
;
3023 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3024 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3025 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3029 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_NAME
;
3030 ExplicitAccess
.Trustee
.ptstrName
= (LPSTR
)szCurrentUser
;
3031 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3032 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3033 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3036 ExplicitAccess
.grfAccessMode
= REVOKE_ACCESS
;
3037 ExplicitAccess
.Trustee
.TrusteeForm
= TRUSTEE_IS_SID
;
3038 ExplicitAccess
.Trustee
.ptstrName
= UsersSid
;
3039 res
= pSetEntriesInAclA(1, &ExplicitAccess
, OldAcl
, &NewAcl
);
3040 ok(res
== ERROR_SUCCESS
, "SetEntriesInAclA failed: %u\n", res
);
3041 ok(NewAcl
!= NULL
, "returned acl was NULL\n");
3045 FreeSid(EveryoneSid
);
3046 HeapFree(GetProcessHeap(), 0, OldAcl
);
3049 static void test_CreateDirectoryA(void)
3051 char admin_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
], *user
;
3052 DWORD sid_size
= sizeof(admin_ptr
), user_size
;
3053 PSID admin_sid
= (PSID
) admin_ptr
, user_sid
;
3054 char sd
[SECURITY_DESCRIPTOR_MIN_LENGTH
];
3055 PSECURITY_DESCRIPTOR pSD
= &sd
;
3056 ACL_SIZE_INFORMATION acl_size
;
3057 ACCESS_ALLOWED_ACE
*ace
;
3058 SECURITY_ATTRIBUTES sa
;
3059 char tmpdir
[MAX_PATH
];
3066 if (!pGetNamedSecurityInfoA
|| !pCreateWellKnownSid
)
3068 win_skip("Required functions are not available\n");
3072 if (!OpenThreadToken(GetCurrentThread(), TOKEN_READ
, TRUE
, &token
))
3074 if (GetLastError() != ERROR_NO_TOKEN
) bret
= FALSE
;
3075 else if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ
, &token
)) bret
= FALSE
;
3079 win_skip("Failed to get current user token\n");
3082 bret
= GetTokenInformation(token
, TokenUser
, NULL
, 0, &user_size
);
3083 ok(!bret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
3084 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
3085 user
= HeapAlloc(GetProcessHeap(), 0, user_size
);
3086 bret
= GetTokenInformation(token
, TokenUser
, user
, user_size
, &user_size
);
3087 ok(bret
, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
3088 CloseHandle( token
);
3089 user_sid
= ((TOKEN_USER
*)user
)->User
.Sid
;
3091 sa
.nLength
= sizeof(sa
);
3092 sa
.lpSecurityDescriptor
= pSD
;
3093 sa
.bInheritHandle
= TRUE
;
3094 InitializeSecurityDescriptor(pSD
, SECURITY_DESCRIPTOR_REVISION
);
3095 pCreateWellKnownSid(WinBuiltinAdministratorsSid
, NULL
, admin_sid
, &sid_size
);
3096 pDacl
= HeapAlloc(GetProcessHeap(), 0, 100);
3097 bret
= InitializeAcl(pDacl
, 100, ACL_REVISION
);
3098 ok(bret
, "Failed to initialize ACL.\n");
3099 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
,
3100 GENERIC_ALL
, user_sid
);
3101 ok(bret
, "Failed to add Current User to ACL.\n");
3102 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
,
3103 GENERIC_ALL
, admin_sid
);
3104 ok(bret
, "Failed to add Administrator Group to ACL.\n");
3105 bret
= SetSecurityDescriptorDacl(pSD
, TRUE
, pDacl
, FALSE
);
3106 ok(bret
, "Failed to add ACL to security desciptor.\n");
3108 GetTempPathA(MAX_PATH
, tmpdir
);
3109 lstrcatA(tmpdir
, "Please Remove Me");
3110 bret
= CreateDirectoryA(tmpdir
, &sa
);
3111 ok(bret
== TRUE
, "CreateDirectoryA(%s) failed err=%d\n", tmpdir
, GetLastError());
3112 HeapFree(GetProcessHeap(), 0, pDacl
);
3114 SetLastError(0xdeadbeef);
3115 error
= pGetNamedSecurityInfoA(tmpdir
, SE_FILE_OBJECT
,
3116 OWNER_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, (PSID
*)&owner
,
3117 NULL
, &pDacl
, NULL
, &pSD
);
3118 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3120 win_skip("GetNamedSecurityInfoA is not implemented\n");
3123 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3125 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3126 ok(bret
, "GetAclInformation failed\n");
3127 ok(acl_size
.AceCount
== 2, "GetAclInformation returned unexpected entry count (%d != 2).\n",
3129 if (acl_size
.AceCount
> 0)
3131 bret
= pGetAce(pDacl
, 0, (VOID
**)&ace
);
3132 ok(bret
, "Failed to get Current User ACE.\n");
3133 bret
= EqualSid(&ace
->SidStart
, user_sid
);
3134 todo_wine
ok(bret
, "Current User ACE != Current User SID.\n");
3135 todo_wine
ok(((ACE_HEADER
*)ace
)->AceFlags
== (OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
),
3136 "Current User ACE has unexpected flags (0x%x != 0x03)\n",
3137 ((ACE_HEADER
*)ace
)->AceFlags
);
3138 ok(ace
->Mask
== 0x1f01ff, "Current User ACE has unexpected mask (0x%x != 0x1f01ff)\n",
3141 if (acl_size
.AceCount
> 1)
3143 bret
= pGetAce(pDacl
, 1, (VOID
**)&ace
);
3144 ok(bret
, "Failed to get Administators Group ACE.\n");
3145 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
3146 todo_wine
ok(bret
, "Administators Group ACE != Administators Group SID.\n");
3147 todo_wine
ok(((ACE_HEADER
*)ace
)->AceFlags
== (OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
),
3148 "Administators Group ACE has unexpected flags (0x%x != 0x03)\n",
3149 ((ACE_HEADER
*)ace
)->AceFlags
);
3150 ok(ace
->Mask
== 0x1f01ff, "Administators Group ACE has unexpected mask (0x%x != 0x1f01ff)\n",
3156 HeapFree(GetProcessHeap(), 0, user
);
3157 bret
= RemoveDirectoryA(tmpdir
);
3158 ok(bret
== TRUE
, "RemoveDirectoryA should always succeed\n");
3161 static void test_GetNamedSecurityInfoA(void)
3163 char admin_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
], *user
;
3164 char system_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
];
3165 char users_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
];
3166 PSID admin_sid
= (PSID
) admin_ptr
, users_sid
= (PSID
) users_ptr
;
3167 PSID system_sid
= (PSID
) system_ptr
, user_sid
;
3168 DWORD sid_size
= sizeof(admin_ptr
), user_size
;
3169 char invalid_path
[] = "/an invalid file path";
3170 int users_ace_id
= -1, admins_ace_id
= -1, i
;
3171 char software_key
[] = "MACHINE\\Software";
3172 char sd
[SECURITY_DESCRIPTOR_MIN_LENGTH
];
3173 SECURITY_DESCRIPTOR_CONTROL control
;
3174 ACL_SIZE_INFORMATION acl_size
;
3175 CHAR windows_dir
[MAX_PATH
];
3176 PSECURITY_DESCRIPTOR pSD
;
3177 ACCESS_ALLOWED_ACE
*ace
;
3178 BOOL bret
= TRUE
, isNT4
;
3179 char tmpfile
[MAX_PATH
];
3180 DWORD error
, revision
;
3181 BOOL owner_defaulted
;
3182 BOOL group_defaulted
;
3183 BOOL dacl_defaulted
;
3184 HANDLE token
, hTemp
;
3190 if (!pSetNamedSecurityInfoA
|| !pGetNamedSecurityInfoA
|| !pCreateWellKnownSid
)
3192 win_skip("Required functions are not available\n");
3196 if (!OpenThreadToken(GetCurrentThread(), TOKEN_READ
, TRUE
, &token
))
3198 if (GetLastError() != ERROR_NO_TOKEN
) bret
= FALSE
;
3199 else if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ
, &token
)) bret
= FALSE
;
3203 win_skip("Failed to get current user token\n");
3206 bret
= GetTokenInformation(token
, TokenUser
, NULL
, 0, &user_size
);
3207 ok(!bret
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
3208 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
3209 user
= HeapAlloc(GetProcessHeap(), 0, user_size
);
3210 bret
= GetTokenInformation(token
, TokenUser
, user
, user_size
, &user_size
);
3211 ok(bret
, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
3212 CloseHandle( token
);
3213 user_sid
= ((TOKEN_USER
*)user
)->User
.Sid
;
3215 bret
= GetWindowsDirectoryA(windows_dir
, MAX_PATH
);
3216 ok(bret
, "GetWindowsDirectory failed with error %d\n", GetLastError());
3218 SetLastError(0xdeadbeef);
3219 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,
3220 OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
,
3221 NULL
, NULL
, NULL
, NULL
, &pSD
);
3222 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3224 win_skip("GetNamedSecurityInfoA is not implemented\n");
3225 HeapFree(GetProcessHeap(), 0, user
);
3228 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3230 bret
= GetSecurityDescriptorControl(pSD
, &control
, &revision
);
3231 ok(bret
, "GetSecurityDescriptorControl failed with error %d\n", GetLastError());
3232 ok((control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == (SE_SELF_RELATIVE
|SE_DACL_PRESENT
) ||
3233 broken((control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == SE_DACL_PRESENT
), /* NT4 */
3234 "control (0x%x) doesn't have (SE_SELF_RELATIVE|SE_DACL_PRESENT) flags set\n", control
);
3235 ok(revision
== SECURITY_DESCRIPTOR_REVISION1
, "revision was %d instead of 1\n", revision
);
3237 isNT4
= (control
& (SE_SELF_RELATIVE
|SE_DACL_PRESENT
)) == SE_DACL_PRESENT
;
3239 bret
= GetSecurityDescriptorOwner(pSD
, &owner
, &owner_defaulted
);
3240 ok(bret
, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
3241 ok(owner
!= NULL
, "owner should not be NULL\n");
3243 bret
= GetSecurityDescriptorGroup(pSD
, &group
, &group_defaulted
);
3244 ok(bret
, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
3245 ok(group
!= NULL
, "group should not be NULL\n");
3249 /* NULL descriptor tests */
3252 win_skip("NT4 does not support GetNamedSecutityInfo with a NULL descriptor\n");
3253 HeapFree(GetProcessHeap(), 0, user
);
3257 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,DACL_SECURITY_INFORMATION
,
3258 NULL
, NULL
, NULL
, NULL
, NULL
);
3259 ok(error
==ERROR_INVALID_PARAMETER
, "GetNamedSecurityInfo failed with error %d\n", error
);
3262 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,DACL_SECURITY_INFORMATION
,
3263 NULL
, NULL
, &pDacl
, NULL
, &pSD
);
3264 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3265 ok(pDacl
!= NULL
, "DACL should not be NULL\n");
3268 error
= pGetNamedSecurityInfoA(windows_dir
, SE_FILE_OBJECT
,OWNER_SECURITY_INFORMATION
,
3269 NULL
, NULL
, &pDacl
, NULL
, NULL
);
3270 ok(error
==ERROR_INVALID_PARAMETER
, "GetNamedSecurityInfo failed with error %d\n", error
);
3272 /* Test behavior of SetNamedSecurityInfo with an invalid path */
3273 SetLastError(0xdeadbeef);
3274 error
= pSetNamedSecurityInfoA(invalid_path
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
, NULL
,
3276 ok(error
== ERROR_FILE_NOT_FOUND
, "Unexpected error returned: 0x%x\n", error
);
3277 ok(GetLastError() == 0xdeadbeef, "Expected last error to remain unchanged.\n");
3279 /* Create security descriptor information and test that it comes back the same */
3281 pDacl
= HeapAlloc(GetProcessHeap(), 0, 100);
3282 InitializeSecurityDescriptor(pSD
, SECURITY_DESCRIPTOR_REVISION
);
3283 pCreateWellKnownSid(WinBuiltinAdministratorsSid
, NULL
, admin_sid
, &sid_size
);
3284 bret
= InitializeAcl(pDacl
, 100, ACL_REVISION
);
3285 ok(bret
, "Failed to initialize ACL.\n");
3286 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, user_sid
);
3287 ok(bret
, "Failed to add Current User to ACL.\n");
3288 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, admin_sid
);
3289 ok(bret
, "Failed to add Administrator Group to ACL.\n");
3290 bret
= SetSecurityDescriptorDacl(pSD
, TRUE
, pDacl
, FALSE
);
3291 ok(bret
, "Failed to add ACL to security desciptor.\n");
3292 GetTempFileNameA(".", "foo", 0, tmpfile
);
3293 hTemp
= CreateFileA(tmpfile
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
,
3294 FILE_FLAG_DELETE_ON_CLOSE
, NULL
);
3295 SetLastError(0xdeadbeef);
3296 error
= pSetNamedSecurityInfoA(tmpfile
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
, NULL
,
3298 HeapFree(GetProcessHeap(), 0, pDacl
);
3299 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3301 win_skip("SetNamedSecurityInfoA is not implemented\n");
3302 HeapFree(GetProcessHeap(), 0, user
);
3306 ok(!error
, "SetNamedSecurityInfoA failed with error %d\n", error
);
3307 SetLastError(0xdeadbeef);
3308 error
= pGetNamedSecurityInfoA(tmpfile
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
,
3309 NULL
, NULL
, &pDacl
, NULL
, &pSD
);
3310 if (error
!= ERROR_SUCCESS
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
3312 win_skip("GetNamedSecurityInfoA is not implemented\n");
3313 HeapFree(GetProcessHeap(), 0, user
);
3317 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3319 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3320 ok(bret
, "GetAclInformation failed\n");
3321 if (acl_size
.AceCount
> 0)
3323 bret
= pGetAce(pDacl
, 0, (VOID
**)&ace
);
3324 ok(bret
, "Failed to get Current User ACE.\n");
3325 bret
= EqualSid(&ace
->SidStart
, user_sid
);
3326 todo_wine
ok(bret
, "Current User ACE != Current User SID.\n");
3327 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
3328 "Current User ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
3329 ok(ace
->Mask
== 0x1f01ff, "Current User ACE has unexpected mask (0x%x != 0x1f01ff)\n",
3332 if (acl_size
.AceCount
> 1)
3334 bret
= pGetAce(pDacl
, 1, (VOID
**)&ace
);
3335 ok(bret
, "Failed to get Administators Group ACE.\n");
3336 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
3337 todo_wine
ok(bret
|| broken(!bret
) /* win2k */,
3338 "Administators Group ACE != Administators Group SID.\n");
3339 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
3340 "Administators Group ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
3341 ok(ace
->Mask
== 0x1f01ff || broken(ace
->Mask
== GENERIC_ALL
) /* win2k */,
3342 "Administators Group ACE has unexpected mask (0x%x != 0x1f01ff)\n", ace
->Mask
);
3345 HeapFree(GetProcessHeap(), 0, user
);
3348 /* Test querying the ownership of a built-in registry key */
3349 sid_size
= sizeof(system_ptr
);
3350 pCreateWellKnownSid(WinLocalSystemSid
, NULL
, system_sid
, &sid_size
);
3351 error
= pGetNamedSecurityInfoA(software_key
, SE_REGISTRY_KEY
,
3352 OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
,
3353 NULL
, NULL
, NULL
, NULL
, &pSD
);
3354 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3356 bret
= GetSecurityDescriptorOwner(pSD
, &owner
, &owner_defaulted
);
3357 ok(bret
, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
3358 ok(owner
!= NULL
, "owner should not be NULL\n");
3359 ok(EqualSid(owner
, admin_sid
), "MACHINE\\Software owner SID != Administrators SID.\n");
3361 bret
= GetSecurityDescriptorGroup(pSD
, &group
, &group_defaulted
);
3362 ok(bret
, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
3363 ok(group
!= NULL
, "group should not be NULL\n");
3364 ok(EqualSid(group
, admin_sid
) || broken(EqualSid(group
, system_sid
)) /* before Win7 */
3365 || broken(((SID
*)group
)->SubAuthority
[0] == SECURITY_NT_NON_UNIQUE
) /* Vista */,
3366 "MACHINE\\Software group SID != Local System SID.\n");
3369 /* Test querying the DACL of a built-in registry key */
3370 sid_size
= sizeof(users_ptr
);
3371 pCreateWellKnownSid(WinBuiltinUsersSid
, NULL
, users_sid
, &sid_size
);
3372 error
= pGetNamedSecurityInfoA(software_key
, SE_REGISTRY_KEY
, DACL_SECURITY_INFORMATION
,
3373 NULL
, NULL
, NULL
, NULL
, &pSD
);
3374 ok(!error
, "GetNamedSecurityInfo failed with error %d\n", error
);
3376 bret
= GetSecurityDescriptorDacl(pSD
, &dacl_present
, &pDacl
, &dacl_defaulted
);
3377 ok(bret
, "GetSecurityDescriptorDacl failed with error %d\n", GetLastError());
3378 ok(dacl_present
, "DACL should be present\n");
3379 ok(pDacl
&& IsValidAcl(pDacl
), "GetSecurityDescriptorDacl returned invalid DACL.\n");
3380 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3381 ok(bret
, "GetAclInformation failed\n");
3382 ok(acl_size
.AceCount
!= 0, "GetAclInformation returned no ACLs\n");
3383 for (i
=0; i
<acl_size
.AceCount
; i
++)
3385 bret
= pGetAce(pDacl
, i
, (VOID
**)&ace
);
3386 ok(bret
, "Failed to get ACE %d.\n", i
);
3387 bret
= EqualSid(&ace
->SidStart
, users_sid
);
3388 if (bret
) users_ace_id
= i
;
3389 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
3390 if (bret
) admins_ace_id
= i
;
3392 ok(users_ace_id
!= -1 || broken(users_ace_id
== -1) /* win2k */,
3393 "Bultin Users ACE not found.\n");
3394 if (users_ace_id
!= -1)
3396 bret
= pGetAce(pDacl
, users_ace_id
, (VOID
**)&ace
);
3397 ok(bret
, "Failed to get Builtin Users ACE.\n");
3398 flags
= ((ACE_HEADER
*)ace
)->AceFlags
;
3399 ok(flags
== (INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
)
3400 || broken(flags
== (INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
|INHERITED_ACE
)) /* w2k8 */,
3401 "Builtin Users ACE has unexpected flags (0x%x != 0x%x)\n", flags
,
3402 INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
);
3403 ok(ace
->Mask
== GENERIC_READ
, "Builtin Users ACE has unexpected mask (0x%x != 0x%x)\n",
3404 ace
->Mask
, GENERIC_READ
);
3406 ok(admins_ace_id
!= -1, "Bultin Admins ACE not found.\n");
3407 if (admins_ace_id
!= -1)
3409 bret
= pGetAce(pDacl
, admins_ace_id
, (VOID
**)&ace
);
3410 ok(bret
, "Failed to get Builtin Admins ACE.\n");
3411 flags
= ((ACE_HEADER
*)ace
)->AceFlags
;
3413 || broken(flags
== (INHERIT_ONLY_ACE
|CONTAINER_INHERIT_ACE
|INHERITED_ACE
)) /* w2k8 */,
3414 "Builtin Admins ACE has unexpected flags (0x%x != 0x0)\n", flags
);
3415 ok(ace
->Mask
== KEY_ALL_ACCESS
|| broken(ace
->Mask
== GENERIC_ALL
) /* w2k8 */,
3416 "Builtin Admins ACE has unexpected mask (0x%x != 0x%x)\n", ace
->Mask
, KEY_ALL_ACCESS
);
3421 static void test_ConvertStringSecurityDescriptor(void)
3424 PSECURITY_DESCRIPTOR pSD
;
3425 static const WCHAR Blank
[] = { 0 };
3429 const char *sidstring
;
3436 { "D:(A;;GA;;;WD)", 0xdeadbeef, FALSE
, ERROR_UNKNOWN_REVISION
},
3437 /* test ACE string type */
3438 { "D:(A;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3439 { "D:(D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3440 { "ERROR:(D;;GA;;;WD)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_PARAMETER
},
3441 /* test ACE string with spaces */
3442 { " D:(D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3443 { "D: (D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3444 { "D:( D;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3445 { "D:(D ;;GA;;;WD)", SDDL_REVISION_1
, FALSE
, RPC_S_INVALID_STRING_UUID
, ERROR_INVALID_ACL
}, /* Vista+ */
3446 { "D:(D; ;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3447 { "D:(D;; GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3448 { "D:(D;;GA ;;;WD)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_ACL
},
3449 { "D:(D;;GA; ;;WD)", SDDL_REVISION_1
, TRUE
},
3450 { "D:(D;;GA;; ;WD)", SDDL_REVISION_1
, TRUE
},
3451 { "D:(D;;GA;;; WD)", SDDL_REVISION_1
, TRUE
},
3452 { "D:(D;;GA;;;WD )", SDDL_REVISION_1
, TRUE
},
3453 /* test ACE string access rights */
3454 { "D:(A;;GA;;;WD)", SDDL_REVISION_1
, TRUE
},
3455 { "D:(A;;GRGWGX;;;WD)", SDDL_REVISION_1
, TRUE
},
3456 { "D:(A;;RCSDWDWO;;;WD)", SDDL_REVISION_1
, TRUE
},
3457 { "D:(A;;RPWPCCDCLCSWLODTCR;;;WD)", SDDL_REVISION_1
, TRUE
},
3458 { "D:(A;;FAFRFWFX;;;WD)", SDDL_REVISION_1
, TRUE
},
3459 { "D:(A;;KAKRKWKX;;;WD)", SDDL_REVISION_1
, TRUE
},
3460 { "D:(A;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1
, TRUE
},
3461 { "S:(AU;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1
, TRUE
},
3462 /* test ACE string access right error case */
3463 { "D:(A;;ROB;;;WD)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_ACL
},
3464 /* test behaviour with empty strings */
3465 { "", SDDL_REVISION_1
, TRUE
},
3466 /* test ACE string SID */
3467 { "D:(D;;GA;;;S-1-0-0)", SDDL_REVISION_1
, TRUE
},
3468 { "D:(D;;GA;;;Nonexistent account)", SDDL_REVISION_1
, FALSE
, ERROR_INVALID_ACL
, ERROR_INVALID_SID
} /* W2K */
3471 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA
)
3473 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
3477 for (i
= 0; i
< sizeof(cssd
)/sizeof(cssd
[0]); i
++)
3481 SetLastError(0xdeadbeef);
3482 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3483 cssd
[i
].sidstring
, cssd
[i
].revision
, &pSD
, NULL
);
3484 GLE
= GetLastError();
3485 ok(ret
== cssd
[i
].ret
, "(%02u) Expected %s (%d)\n", i
, cssd
[i
].ret
? "success" : "failure", GLE
);
3487 ok(GLE
== cssd
[i
].GLE
||
3488 (cssd
[i
].altGLE
&& GLE
== cssd
[i
].altGLE
),
3489 "(%02u) Unexpected last error %d\n", i
, GLE
);
3494 /* test behaviour with NULL parameters */
3495 SetLastError(0xdeadbeef);
3496 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3497 NULL
, 0xdeadbeef, &pSD
, NULL
);
3499 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3500 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3503 SetLastError(0xdeadbeef);
3504 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorW(
3505 NULL
, 0xdeadbeef, &pSD
, NULL
);
3506 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3507 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3510 SetLastError(0xdeadbeef);
3511 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3512 "D:(A;;ROB;;;WD)", 0xdeadbeef, NULL
, NULL
);
3513 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3514 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3517 SetLastError(0xdeadbeef);
3518 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorA(
3519 "D:(A;;ROB;;;WD)", SDDL_REVISION_1
, NULL
, NULL
);
3520 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
3521 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
3524 /* test behaviour with empty strings */
3525 SetLastError(0xdeadbeef);
3526 ret
= pConvertStringSecurityDescriptorToSecurityDescriptorW(
3527 Blank
, SDDL_REVISION_1
, &pSD
, NULL
);
3528 ok(ret
, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
3532 static void test_ConvertSecurityDescriptorToString(void)
3534 SECURITY_DESCRIPTOR desc
;
3535 SECURITY_INFORMATION sec_info
= OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
|SACL_SECURITY_INFORMATION
;
3544 if (!pConvertSecurityDescriptorToStringSecurityDescriptorA
)
3546 win_skip("ConvertSecurityDescriptorToStringSecurityDescriptor is not available\n");
3549 if (!pCreateWellKnownSid
)
3551 win_skip("CreateWellKnownSid is not available\n");
3555 /* It seems Windows XP adds an extra character to the length of the string for each ACE in an ACL. We
3556 * don't replicate this feature so we only test len >= strlen+1. */
3557 #define CHECK_RESULT_AND_FREE(exp_str) \
3558 ok(strcmp(string, (exp_str)) == 0, "String mismatch (expected \"%s\", got \"%s\")\n", (exp_str), string); \
3559 ok(len >= (strlen(exp_str) + 1), "Length mismatch (expected %d, got %d)\n", lstrlenA(exp_str) + 1, len); \
3562 #define CHECK_ONE_OF_AND_FREE(exp_str1, exp_str2) \
3563 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); \
3564 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); \
3567 InitializeSecurityDescriptor(&desc
, SECURITY_DESCRIPTOR_REVISION
);
3568 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3569 CHECK_RESULT_AND_FREE("");
3572 pCreateWellKnownSid(WinLocalSid
, NULL
, sid_buf
, &size
);
3573 SetSecurityDescriptorOwner(&desc
, sid_buf
, FALSE
);
3574 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3575 CHECK_RESULT_AND_FREE("O:S-1-2-0");
3577 SetSecurityDescriptorOwner(&desc
, sid_buf
, TRUE
);
3578 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3579 CHECK_RESULT_AND_FREE("O:S-1-2-0");
3581 size
= sizeof(sid_buf
);
3582 pCreateWellKnownSid(WinLocalSystemSid
, NULL
, sid_buf
, &size
);
3583 SetSecurityDescriptorOwner(&desc
, sid_buf
, TRUE
);
3584 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3585 CHECK_RESULT_AND_FREE("O:SY");
3587 pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid
);
3588 SetSecurityDescriptorGroup(&desc
, psid
, TRUE
);
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-4576");
3592 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, GROUP_SECURITY_INFORMATION
, &string
, &len
), "Conversion failed\n");
3593 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
3595 pacl
= (PACL
)acl_buf
;
3596 InitializeAcl(pacl
, sizeof(acl_buf
), ACL_REVISION
);
3597 SetSecurityDescriptorDacl(&desc
, TRUE
, pacl
, TRUE
);
3598 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3599 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
3601 SetSecurityDescriptorDacl(&desc
, TRUE
, pacl
, FALSE
);
3602 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3603 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
3605 pConvertStringSidToSidA("S-1-5-6", &psid2
);
3606 pAddAccessAllowedAceEx(pacl
, ACL_REVISION
, NO_PROPAGATE_INHERIT_ACE
, 0xf0000000, psid2
);
3607 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3608 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)");
3610 pAddAccessAllowedAceEx(pacl
, ACL_REVISION
, INHERIT_ONLY_ACE
|INHERITED_ACE
, 0x00000003, psid2
);
3611 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3612 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)");
3614 pAddAccessDeniedAceEx(pacl
, ACL_REVISION
, OBJECT_INHERIT_ACE
|CONTAINER_INHERIT_ACE
, 0xffffffff, psid
);
3615 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3616 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)");
3619 pacl
= (PACL
)acl_buf
;
3620 InitializeAcl(pacl
, sizeof(acl_buf
), ACL_REVISION
);
3621 SetSecurityDescriptorSacl(&desc
, TRUE
, pacl
, FALSE
);
3622 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3623 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:");
3625 /* fails in win2k */
3626 SetSecurityDescriptorDacl(&desc
, TRUE
, NULL
, FALSE
);
3627 pAddAuditAccessAceEx(pacl
, ACL_REVISION
, VALID_INHERIT_FLAGS
, KEY_READ
|KEY_WRITE
, psid2
, TRUE
, TRUE
);
3628 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
))
3630 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)", /* XP */
3631 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)" /* Vista */);
3634 /* fails in win2k */
3635 pAddAuditAccessAceEx(pacl
, ACL_REVISION
, NO_PROPAGATE_INHERIT_ACE
, FILE_GENERIC_READ
|FILE_GENERIC_WRITE
, psid2
, TRUE
, FALSE
);
3636 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc
, SDDL_REVISION_1
, sec_info
, &string
, &len
))
3638 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", /* XP */
3639 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)" /* Vista */);
3646 static void test_SetSecurityDescriptorControl (PSECURITY_DESCRIPTOR sec
)
3648 SECURITY_DESCRIPTOR_CONTROL ref
;
3649 SECURITY_DESCRIPTOR_CONTROL test
;
3651 SECURITY_DESCRIPTOR_CONTROL
const mutable
3652 = SE_DACL_AUTO_INHERIT_REQ
| SE_SACL_AUTO_INHERIT_REQ
3653 | SE_DACL_AUTO_INHERITED
| SE_SACL_AUTO_INHERITED
3654 | SE_DACL_PROTECTED
| SE_SACL_PROTECTED
3655 | 0x00000040 | 0x00000080 /* not defined in winnt.h */
3657 SECURITY_DESCRIPTOR_CONTROL
const immutable
3658 = SE_OWNER_DEFAULTED
| SE_GROUP_DEFAULTED
3659 | SE_DACL_PRESENT
| SE_DACL_DEFAULTED
3660 | SE_SACL_PRESENT
| SE_SACL_DEFAULTED
3661 | SE_RM_CONTROL_VALID
| SE_SELF_RELATIVE
3666 LPCSTR fmt
= "Expected error %s, got %u\n";
3668 GetSecurityDescriptorControl (sec
, &ref
, &dwRevision
);
3670 /* The mutable bits are mutable regardless of the truth of
3671 SE_DACL_PRESENT and/or SE_SACL_PRESENT */
3673 /* Check call barfs if any bit-of-interest is immutable */
3674 for (bit
= 0; bit
< 16; ++bit
)
3676 SECURITY_DESCRIPTOR_CONTROL
const bitOfInterest
= 1 << bit
;
3677 SECURITY_DESCRIPTOR_CONTROL setOrClear
= ref
& bitOfInterest
;
3679 SECURITY_DESCRIPTOR_CONTROL ctrl
;
3681 DWORD dwExpect
= (bitOfInterest
& immutable
)
3682 ? ERROR_INVALID_PARAMETER
: 0xbebecaca;
3683 LPCSTR strExpect
= (bitOfInterest
& immutable
)
3684 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
3686 ctrl
= (bitOfInterest
& mutable) ? ref
+ bitOfInterest
: ref
;
3687 setOrClear
^= bitOfInterest
;
3688 SetLastError (0xbebecaca);
3689 pSetSecurityDescriptorControl (sec
, bitOfInterest
, setOrClear
);
3690 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3691 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
3692 expect_eq(test
, ctrl
, int, "%x");
3694 setOrClear
^= bitOfInterest
;
3695 SetLastError (0xbebecaca);
3696 pSetSecurityDescriptorControl (sec
, bitOfInterest
, setOrClear
);
3697 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3698 GetSecurityDescriptorControl (sec
, &test
, &dwRevision
);
3699 expect_eq(test
, ref
, int, "%x");
3702 /* Check call barfs if any bit-to-set is immutable
3703 even when not a bit-of-interest */
3704 for (bit
= 0; bit
< 16; ++bit
)
3706 SECURITY_DESCRIPTOR_CONTROL
const bitsOfInterest
= mutable;
3707 SECURITY_DESCRIPTOR_CONTROL setOrClear
= ref
& bitsOfInterest
;
3709 SECURITY_DESCRIPTOR_CONTROL ctrl
;
3711 DWORD dwExpect
= ((1 << bit
) & immutable
)
3712 ? ERROR_INVALID_PARAMETER
: 0xbebecaca;
3713 LPCSTR strExpect
= ((1 << bit
) & immutable
)
3714 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
3716 ctrl
= ((1 << bit
) & immutable
) ? test
: ref
| mutable;
3717 setOrClear
^= bitsOfInterest
;
3718 SetLastError (0xbebecaca);
3719 pSetSecurityDescriptorControl (sec
, bitsOfInterest
, setOrClear
| (1 << bit
));
3720 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3721 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
3722 expect_eq(test
, ctrl
, int, "%x");
3724 ctrl
= ((1 << bit
) & immutable
) ? test
: ref
| (1 << bit
);
3725 setOrClear
^= bitsOfInterest
;
3726 SetLastError (0xbebecaca);
3727 pSetSecurityDescriptorControl (sec
, bitsOfInterest
, setOrClear
| (1 << bit
));
3728 ok (GetLastError () == dwExpect
, fmt
, strExpect
, GetLastError ());
3729 GetSecurityDescriptorControl(sec
, &test
, &dwRevision
);
3730 expect_eq(test
, ctrl
, int, "%x");
3734 static void test_PrivateObjectSecurity(void)
3736 SECURITY_INFORMATION sec_info
= OWNER_SECURITY_INFORMATION
|GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
|SACL_SECURITY_INFORMATION
;
3737 SECURITY_DESCRIPTOR_CONTROL ctrl
;
3738 PSECURITY_DESCRIPTOR sec
;
3744 PSECURITY_DESCRIPTOR buf
;
3747 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA
)
3749 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
3753 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3755 "G:S-1-5-21-93476-23408-4576"
3756 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)"
3757 "(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3758 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
3759 SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
3761 test_SetSecurityDescriptorControl(sec
);
3765 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3767 "G:S-1-5-21-93476-23408-4576",
3768 SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
3770 test_SetSecurityDescriptorControl(sec
);
3774 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3776 "G:S-1-5-21-93476-23408-4576"
3777 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3778 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", SDDL_REVISION_1
, &sec
, &dwDescSize
), "Creating descriptor failed\n");
3779 buf
= HeapAlloc(GetProcessHeap(), 0, dwDescSize
);
3780 pSetSecurityDescriptorControl(sec
, SE_DACL_PROTECTED
, SE_DACL_PROTECTED
);
3781 GetSecurityDescriptorControl(sec
, &ctrl
, &dwRevision
);
3782 expect_eq(ctrl
, 0x9014, int, "%x");
3784 ret
= GetPrivateObjectSecurity(sec
, GROUP_SECURITY_INFORMATION
, buf
, dwDescSize
, &retSize
);
3785 ok(ret
, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3786 ok(retSize
<= dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
3787 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3788 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
3789 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
3790 expect_eq(ctrl
, 0x8000, int, "%x");
3792 ret
= GetPrivateObjectSecurity(sec
, GROUP_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, buf
, dwDescSize
, &retSize
);
3793 ok(ret
, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3794 ok(retSize
<= dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
3795 ret
= pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
);
3796 ok(ret
, "Conversion failed err=%u\n", GetLastError());
3797 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)",
3798 "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 */
3799 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
3800 expect_eq(ctrl
& (~ SE_DACL_PROTECTED
), 0x8004, int, "%x");
3802 ret
= GetPrivateObjectSecurity(sec
, sec_info
, buf
, dwDescSize
, &retSize
);
3803 ok(ret
, "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3804 ok(retSize
== dwDescSize
, "Buffer too small (%d vs %d)\n", retSize
, dwDescSize
);
3805 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf
, SDDL_REVISION_1
, sec_info
, &string
, &len
), "Conversion failed\n");
3806 CHECK_ONE_OF_AND_FREE("O:SY"
3807 "G:S-1-5-21-93476-23408-4576"
3808 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3809 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
3811 "G:S-1-5-21-93476-23408-4576"
3812 "D:P(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3813 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)"); /* Win7 */
3814 GetSecurityDescriptorControl(buf
, &ctrl
, &dwRevision
);
3815 expect_eq(ctrl
& (~ SE_DACL_PROTECTED
), 0x8014, int, "%x");
3817 SetLastError(0xdeadbeef);
3818 ok(GetPrivateObjectSecurity(sec
, sec_info
, buf
, 5, &retSize
) == FALSE
, "GetPrivateObjectSecurity should have failed\n");
3819 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Expected error ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
3822 HeapFree(GetProcessHeap(), 0, buf
);
3824 #undef CHECK_RESULT_AND_FREE
3825 #undef CHECK_ONE_OF_AND_FREE
3827 static void test_acls(void)
3830 PACL pAcl
= (PACL
)buffer
;
3833 SetLastError(0xdeadbeef);
3834 ret
= InitializeAcl(pAcl
, sizeof(ACL
) - 1, ACL_REVISION
);
3835 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
3837 win_skip("InitializeAcl is not implemented\n");
3841 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "InitializeAcl with too small a buffer should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
3843 SetLastError(0xdeadbeef);
3844 ret
= InitializeAcl(pAcl
, 0xffffffff, ACL_REVISION
);
3845 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl with too large a buffer should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3847 SetLastError(0xdeadbeef);
3848 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION1
);
3849 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl(ACL_REVISION1) should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3851 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION2
);
3852 ok(ret
, "InitializeAcl(ACL_REVISION2) failed with error %d\n", GetLastError());
3854 ret
= IsValidAcl(pAcl
);
3855 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
3857 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION3
);
3858 ok(ret
, "InitializeAcl(ACL_REVISION3) failed with error %d\n", GetLastError());
3860 ret
= IsValidAcl(pAcl
);
3861 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
3863 SetLastError(0xdeadbeef);
3864 ret
= InitializeAcl(pAcl
, sizeof(buffer
), ACL_REVISION4
);
3865 if (GetLastError() != ERROR_INVALID_PARAMETER
)
3867 ok(ret
, "InitializeAcl(ACL_REVISION4) failed with error %d\n", GetLastError());
3869 ret
= IsValidAcl(pAcl
);
3870 ok(ret
, "IsValidAcl failed with error %d\n", GetLastError());
3873 win_skip("ACL_REVISION4 is not implemented on NT4\n");
3875 SetLastError(0xdeadbeef);
3876 ret
= InitializeAcl(pAcl
, sizeof(buffer
), -1);
3877 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
, "InitializeAcl(-1) failed with error %d\n", GetLastError());
3880 static void test_GetSecurityInfo(void)
3882 char b
[sizeof(TOKEN_USER
) + sizeof(SID
) + sizeof(DWORD
)*SID_MAX_SUB_AUTHORITIES
];
3883 char admin_ptr
[sizeof(SID
)+sizeof(ULONG
)*SID_MAX_SUB_AUTHORITIES
], dacl
[100];
3884 DWORD sid_size
= sizeof(admin_ptr
), l
= sizeof(b
);
3885 PSID admin_sid
= (PSID
) admin_ptr
, user_sid
;
3886 char sd
[SECURITY_DESCRIPTOR_MIN_LENGTH
];
3887 ACL_SIZE_INFORMATION acl_size
;
3888 PSECURITY_DESCRIPTOR pSD
;
3889 ACCESS_ALLOWED_ACE
*ace
;
3896 if (!pGetSecurityInfo
|| !pSetSecurityInfo
)
3898 win_skip("[Get|Set]SecurityInfo is not available\n");
3902 if (!OpenThreadToken(GetCurrentThread(), TOKEN_READ
, TRUE
, &token
))
3904 if (GetLastError() != ERROR_NO_TOKEN
) bret
= FALSE
;
3905 else if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ
, &token
)) bret
= FALSE
;
3909 win_skip("Failed to get current user token\n");
3912 GetTokenInformation(token
, TokenUser
, b
, l
, &l
);
3913 CloseHandle( token
);
3914 user_sid
= ((TOKEN_USER
*)b
)->User
.Sid
;
3916 /* Create something. Files have lots of associated security info. */
3917 obj
= CreateFileA(myARGV
[0], GENERIC_READ
|WRITE_DAC
, FILE_SHARE_READ
, NULL
,
3918 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3919 if (obj
== INVALID_HANDLE_VALUE
)
3921 skip("Couldn't create an object for GetSecurityInfo test\n");
3925 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
,
3926 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
3927 &owner
, &group
, &pDacl
, NULL
, &pSD
);
3928 if (ret
== ERROR_CALL_NOT_IMPLEMENTED
)
3930 win_skip("GetSecurityInfo is not implemented\n");
3934 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
3935 ok(pSD
!= NULL
, "GetSecurityInfo\n");
3936 ok(owner
!= NULL
, "GetSecurityInfo\n");
3937 ok(group
!= NULL
, "GetSecurityInfo\n");
3939 ok(IsValidAcl(pDacl
), "GetSecurityInfo\n");
3941 win_skip("No ACL information returned\n");
3945 if (!pCreateWellKnownSid
)
3947 win_skip("NULL parameter test would crash on NT4\n");
3952 /* If we don't ask for the security descriptor, Windows will still give us
3953 the other stuff, leaving us no way to free it. */
3954 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
,
3955 OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
3956 &owner
, &group
, &pDacl
, NULL
, NULL
);
3957 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
3958 ok(owner
!= NULL
, "GetSecurityInfo\n");
3959 ok(group
!= NULL
, "GetSecurityInfo\n");
3961 ok(IsValidAcl(pDacl
), "GetSecurityInfo\n");
3963 win_skip("No ACL information returned\n");
3965 /* Create security descriptor information and test that it comes back the same */
3967 pDacl
= (PACL
)&dacl
;
3968 InitializeSecurityDescriptor(pSD
, SECURITY_DESCRIPTOR_REVISION
);
3969 pCreateWellKnownSid(WinBuiltinAdministratorsSid
, NULL
, admin_sid
, &sid_size
);
3970 bret
= InitializeAcl(pDacl
, sizeof(dacl
), ACL_REVISION
);
3971 ok(bret
, "Failed to initialize ACL.\n");
3972 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, user_sid
);
3973 ok(bret
, "Failed to add Current User to ACL.\n");
3974 bret
= pAddAccessAllowedAceEx(pDacl
, ACL_REVISION
, 0, GENERIC_ALL
, admin_sid
);
3975 ok(bret
, "Failed to add Administrator Group to ACL.\n");
3976 bret
= SetSecurityDescriptorDacl(pSD
, TRUE
, pDacl
, FALSE
);
3977 ok(bret
, "Failed to add ACL to security desciptor.\n");
3978 ret
= pSetSecurityInfo(obj
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
,
3979 NULL
, NULL
, pDacl
, NULL
);
3980 ok(ret
== ERROR_SUCCESS
, "SetSecurityInfo returned %d\n", ret
);
3981 ret
= pGetSecurityInfo(obj
, SE_FILE_OBJECT
, DACL_SECURITY_INFORMATION
,
3982 NULL
, NULL
, &pDacl
, NULL
, &pSD
);
3983 ok(ret
== ERROR_SUCCESS
, "GetSecurityInfo returned %d\n", ret
);
3984 ok(pDacl
&& IsValidAcl(pDacl
), "GetSecurityInfo returned invalid DACL.\n");
3985 bret
= pGetAclInformation(pDacl
, &acl_size
, sizeof(acl_size
), AclSizeInformation
);
3986 ok(bret
, "GetAclInformation failed\n");
3987 if (acl_size
.AceCount
> 0)
3989 bret
= pGetAce(pDacl
, 0, (VOID
**)&ace
);
3990 ok(bret
, "Failed to get Current User ACE.\n");
3991 bret
= EqualSid(&ace
->SidStart
, user_sid
);
3992 todo_wine
ok(bret
, "Current User ACE != Current User SID.\n");
3993 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
3994 "Current User ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
3995 ok(ace
->Mask
== 0x1f01ff, "Current User ACE has unexpected mask (0x%x != 0x1f01ff)\n",
3998 if (acl_size
.AceCount
> 1)
4000 bret
= pGetAce(pDacl
, 1, (VOID
**)&ace
);
4001 ok(bret
, "Failed to get Administators Group ACE.\n");
4002 bret
= EqualSid(&ace
->SidStart
, admin_sid
);
4003 todo_wine
ok(bret
, "Administators Group ACE != Administators Group SID.\n");
4004 ok(((ACE_HEADER
*)ace
)->AceFlags
== 0,
4005 "Administators Group ACE has unexpected flags (0x%x != 0x0)\n", ((ACE_HEADER
*)ace
)->AceFlags
);
4006 ok(ace
->Mask
== 0x1f01ff, "Administators Group ACE has unexpected mask (0x%x != 0x1f01ff)\n",
4013 static void test_GetSidSubAuthority(void)
4017 if (!pGetSidSubAuthority
|| !pConvertStringSidToSidA
|| !pIsValidSid
|| !pGetSidSubAuthorityCount
)
4019 win_skip("Some functions not available\n");
4022 /* Note: on windows passing in an invalid index like -1, lets GetSidSubAuthority return 0x05000000 but
4023 still GetLastError returns ERROR_SUCCESS then. We don't test these unlikely cornercases here for now */
4024 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576",&psid
),"ConvertStringSidToSidA failed\n");
4025 ok(pIsValidSid(psid
),"Sid is not valid\n");
4026 SetLastError(0xbebecaca);
4027 ok(*pGetSidSubAuthorityCount(psid
) == 4,"GetSidSubAuthorityCount gave %d expected 4\n",*pGetSidSubAuthorityCount(psid
));
4028 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
4029 SetLastError(0xbebecaca);
4030 ok(*pGetSidSubAuthority(psid
,0) == 21,"GetSidSubAuthority gave %d expected 21\n",*pGetSidSubAuthority(psid
,0));
4031 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
4032 SetLastError(0xbebecaca);
4033 ok(*pGetSidSubAuthority(psid
,1) == 93476,"GetSidSubAuthority gave %d expected 93476\n",*pGetSidSubAuthority(psid
,1));
4034 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
4035 SetLastError(0xbebecaca);
4036 ok(pGetSidSubAuthority(psid
,4) != NULL
,"Expected out of bounds GetSidSubAuthority to return a non-NULL pointer\n");
4037 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
4041 static void test_CheckTokenMembership(void)
4043 PTOKEN_GROUPS token_groups
;
4045 HANDLE process_token
, token
;
4050 if (!pCheckTokenMembership
)
4052 win_skip("CheckTokenMembership is not available\n");
4055 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &process_token
);
4056 ok(ret
, "OpenProcessToken failed with error %d\n", GetLastError());
4058 ret
= DuplicateToken(process_token
, SecurityImpersonation
, &token
);
4059 ok(ret
, "DuplicateToken failed with error %d\n", GetLastError());
4062 ret
= GetTokenInformation(token
, TokenGroups
, NULL
, 0, &size
);
4063 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
4064 "GetTokenInformation(TokenGroups) %s with error %d\n",
4065 ret
? "succeeded" : "failed", GetLastError());
4066 token_groups
= HeapAlloc(GetProcessHeap(), 0, size
);
4067 ret
= GetTokenInformation(token
, TokenGroups
, token_groups
, size
, &size
);
4068 ok(ret
, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
4070 for (i
= 0; i
< token_groups
->GroupCount
; i
++)
4072 if (token_groups
->Groups
[i
].Attributes
& SE_GROUP_ENABLED
)
4076 if (i
== token_groups
->GroupCount
)
4078 HeapFree(GetProcessHeap(), 0, token_groups
);
4080 skip("user not a member of any group\n");
4085 ret
= pCheckTokenMembership(token
, token_groups
->Groups
[i
].Sid
, &is_member
);
4086 ok(ret
, "CheckTokenMembership failed with error %d\n", GetLastError());
4087 ok(is_member
, "CheckTokenMembership should have detected sid as member\n");
4090 ret
= pCheckTokenMembership(NULL
, token_groups
->Groups
[i
].Sid
, &is_member
);
4091 ok(ret
, "CheckTokenMembership failed with error %d\n", GetLastError());
4092 ok(is_member
, "CheckTokenMembership should have detected sid as member\n");
4095 SetLastError(0xdeadbeef);
4096 ret
= pCheckTokenMembership(process_token
, token_groups
->Groups
[i
].Sid
, &is_member
);
4097 ok(!ret
&& GetLastError() == ERROR_NO_IMPERSONATION_TOKEN
,
4098 "CheckTokenMembership with process token %s with error %d\n",
4099 ret
? "succeeded" : "failed", GetLastError());
4100 ok(!is_member
, "CheckTokenMembership should have cleared is_member\n");
4102 HeapFree(GetProcessHeap(), 0, token_groups
);
4104 CloseHandle(process_token
);
4107 static void test_EqualSid(void)
4111 SID_IDENTIFIER_AUTHORITY SIDAuthWorld
= { SECURITY_WORLD_SID_AUTHORITY
};
4112 SID_IDENTIFIER_AUTHORITY SIDAuthNT
= { SECURITY_NT_AUTHORITY
};
4114 SetLastError(0xdeadbeef);
4115 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
4116 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &sid1
);
4117 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
4119 win_skip("AllocateAndInitializeSid is not implemented\n");
4122 ok(ret
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
4123 ok(GetLastError() == 0xdeadbeef,
4124 "AllocateAndInitializeSid shouldn't have set last error to %d\n",
4127 ret
= AllocateAndInitializeSid(&SIDAuthWorld
, 1, SECURITY_WORLD_RID
,
4128 0, 0, 0, 0, 0, 0, 0, &sid2
);
4129 ok(ret
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
4131 SetLastError(0xdeadbeef);
4132 ret
= EqualSid(sid1
, sid2
);
4133 ok(!ret
, "World and domain admins sids shouldn't have been equal\n");
4134 ok(GetLastError() == ERROR_SUCCESS
||
4135 broken(GetLastError() == 0xdeadbeef), /* NT4 */
4136 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
4139 SetLastError(0xdeadbeef);
4140 sid2
= FreeSid(sid2
);
4141 ok(!sid2
, "FreeSid should have returned NULL instead of %p\n", sid2
);
4142 ok(GetLastError() == 0xdeadbeef,
4143 "FreeSid shouldn't have set last error to %d\n",
4146 ret
= AllocateAndInitializeSid(&SIDAuthNT
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
4147 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0, &sid2
);
4148 ok(ret
, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
4150 SetLastError(0xdeadbeef);
4151 ret
= EqualSid(sid1
, sid2
);
4152 ok(ret
, "Same sids should have been equal\n");
4153 ok(GetLastError() == ERROR_SUCCESS
||
4154 broken(GetLastError() == 0xdeadbeef), /* NT4 */
4155 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
4158 ((SID
*)sid2
)->Revision
= 2;
4159 SetLastError(0xdeadbeef);
4160 ret
= EqualSid(sid1
, sid2
);
4161 ok(!ret
, "EqualSid with invalid sid should have returned FALSE\n");
4162 ok(GetLastError() == ERROR_SUCCESS
||
4163 broken(GetLastError() == 0xdeadbeef), /* NT4 */
4164 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
4166 ((SID
*)sid2
)->Revision
= SID_REVISION
;
4172 static void test_GetUserNameA(void)
4174 char buffer
[UNLEN
+ 1], filler
[UNLEN
+ 1];
4175 DWORD required_len
, buffer_len
;
4178 /* Test crashes on Windows. */
4181 SetLastError(0xdeadbeef);
4182 GetUserNameA(NULL
, NULL
);
4185 SetLastError(0xdeadbeef);
4187 ret
= GetUserNameA(NULL
, &required_len
);
4188 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
4189 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
4190 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4192 SetLastError(0xdeadbeef);
4194 ret
= GetUserNameA(NULL
, &required_len
);
4195 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
4196 ok(required_len
!= 0 && required_len
!= 1, "Outputted buffer length was %u\n", required_len
);
4197 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4199 /* Tests crashes on Windows. */
4202 SetLastError(0xdeadbeef);
4203 required_len
= UNLEN
+ 1;
4204 GetUserNameA(NULL
, &required_len
);
4206 SetLastError(0xdeadbeef);
4207 GetUserNameA(buffer
, NULL
);
4210 memset(filler
, 'x', sizeof(filler
));
4212 /* Note that GetUserNameA on XP and newer outputs the number of bytes
4213 * required for a Unicode string, which affects a test in the next block. */
4214 SetLastError(0xdeadbeef);
4215 memcpy(buffer
, filler
, sizeof(filler
));
4217 ret
= GetUserNameA(buffer
, &required_len
);
4218 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
4219 ok(!memcmp(buffer
, filler
, sizeof(filler
)), "Output buffer was altered\n");
4220 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
4221 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4223 SetLastError(0xdeadbeef);
4224 memcpy(buffer
, filler
, sizeof(filler
));
4225 buffer_len
= required_len
;
4226 ret
= GetUserNameA(buffer
, &buffer_len
);
4227 ok(ret
== TRUE
, "GetUserNameA returned %d, last error %u\n", ret
, GetLastError());
4228 ok(memcmp(buffer
, filler
, sizeof(filler
)) != 0, "Output buffer was untouched\n");
4229 ok(buffer_len
== required_len
||
4230 broken(buffer_len
== required_len
/ sizeof(WCHAR
)), /* XP+ */
4231 "Outputted buffer length was %u\n", buffer_len
);
4233 /* Use the reported buffer size from the last GetUserNameA call and pass
4234 * a length that is one less than the required value. */
4235 SetLastError(0xdeadbeef);
4236 memcpy(buffer
, filler
, sizeof(filler
));
4238 ret
= GetUserNameA(buffer
, &buffer_len
);
4239 ok(ret
== FALSE
, "GetUserNameA returned %d\n", ret
);
4240 ok(!memcmp(buffer
, filler
, sizeof(filler
)), "Output buffer was untouched\n");
4241 ok(buffer_len
== required_len
, "Outputted buffer length was %u\n", buffer_len
);
4242 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4245 static void test_GetUserNameW(void)
4247 WCHAR buffer
[UNLEN
+ 1], filler
[UNLEN
+ 1];
4248 DWORD required_len
, buffer_len
;
4251 /* Test crashes on Windows. */
4254 SetLastError(0xdeadbeef);
4255 GetUserNameW(NULL
, NULL
);
4258 SetLastError(0xdeadbeef);
4260 ret
= GetUserNameW(NULL
, &required_len
);
4261 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
4262 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
4263 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4265 SetLastError(0xdeadbeef);
4267 ret
= GetUserNameW(NULL
, &required_len
);
4268 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
4269 ok(required_len
!= 0 && required_len
!= 1, "Outputted buffer length was %u\n", required_len
);
4270 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4272 /* Tests crash on Windows. */
4275 SetLastError(0xdeadbeef);
4276 required_len
= UNLEN
+ 1;
4277 GetUserNameW(NULL
, &required_len
);
4279 SetLastError(0xdeadbeef);
4280 GetUserNameW(buffer
, NULL
);
4283 memset(filler
, 'x', sizeof(filler
));
4285 SetLastError(0xdeadbeef);
4286 memcpy(buffer
, filler
, sizeof(filler
));
4288 ret
= GetUserNameW(buffer
, &required_len
);
4289 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
4290 ok(!memcmp(buffer
, filler
, sizeof(filler
)), "Output buffer was altered\n");
4291 ok(required_len
!= 0, "Outputted buffer length was %u\n", required_len
);
4292 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4294 SetLastError(0xdeadbeef);
4295 memcpy(buffer
, filler
, sizeof(filler
));
4296 buffer_len
= required_len
;
4297 ret
= GetUserNameW(buffer
, &buffer_len
);
4298 ok(ret
== TRUE
, "GetUserNameW returned %d, last error %u\n", ret
, GetLastError());
4299 ok(memcmp(buffer
, filler
, sizeof(filler
)) != 0, "Output buffer was untouched\n");
4300 ok(buffer_len
== required_len
, "Outputted buffer length was %u\n", buffer_len
);
4302 /* GetUserNameW on XP and newer writes a truncated portion of the username string to the buffer. */
4303 SetLastError(0xdeadbeef);
4304 memcpy(buffer
, filler
, sizeof(filler
));
4306 ret
= GetUserNameW(buffer
, &buffer_len
);
4307 ok(ret
== FALSE
, "GetUserNameW returned %d\n", ret
);
4308 ok(!memcmp(buffer
, filler
, sizeof(filler
)) ||
4309 broken(memcmp(buffer
, filler
, sizeof(filler
)) != 0), /* XP+ */
4310 "Output buffer was altered\n");
4311 ok(buffer_len
== required_len
, "Outputted buffer length was %u\n", buffer_len
);
4312 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Last error was %u\n", GetLastError());
4315 static void test_CreateRestrictedToken(void)
4317 HANDLE process_token
, token
, r_token
;
4318 PTOKEN_GROUPS token_groups
, groups2
;
4319 SID_AND_ATTRIBUTES sattr
;
4320 SECURITY_IMPERSONATION_LEVEL level
;
4327 if (!pCreateRestrictedToken
)
4329 win_skip("CreateRestrictedToken is not available\n");
4333 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
|TOKEN_QUERY
, &process_token
);
4334 ok(ret
, "got error %d\n", GetLastError());
4336 ret
= DuplicateTokenEx(process_token
, TOKEN_DUPLICATE
|TOKEN_ADJUST_GROUPS
|TOKEN_QUERY
,
4337 NULL
, SecurityImpersonation
, TokenImpersonation
, &token
);
4338 ok(ret
, "got error %d\n", GetLastError());
4341 ret
= GetTokenInformation(token
, TokenGroups
, NULL
, 0, &size
);
4342 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
4343 "got %d with error %d\n", ret
, GetLastError());
4344 token_groups
= HeapAlloc(GetProcessHeap(), 0, size
);
4345 ret
= GetTokenInformation(token
, TokenGroups
, token_groups
, size
, &size
);
4346 ok(ret
, "got error %d\n", GetLastError());
4348 for (i
= 0; i
< token_groups
->GroupCount
; i
++)
4350 if (token_groups
->Groups
[i
].Attributes
& SE_GROUP_ENABLED
)
4354 if (i
== token_groups
->GroupCount
)
4356 HeapFree(GetProcessHeap(), 0, token_groups
);
4358 skip("User not a member of any group\n");
4363 ret
= pCheckTokenMembership(token
, token_groups
->Groups
[i
].Sid
, &is_member
);
4364 ok(ret
, "got error %d\n", GetLastError());
4365 ok(is_member
, "not a member\n");
4367 /* disable a SID in new token */
4368 sattr
.Sid
= token_groups
->Groups
[i
].Sid
;
4369 sattr
.Attributes
= 0;
4371 ret
= pCreateRestrictedToken(token
, 0, 1, &sattr
, 0, NULL
, 0, NULL
, &r_token
);
4372 ok(ret
, "got error %d\n", GetLastError());
4376 /* check if a SID is enabled */
4378 ret
= pCheckTokenMembership(r_token
, token_groups
->Groups
[i
].Sid
, &is_member
);
4379 ok(ret
, "got error %d\n", GetLastError());
4380 todo_wine
ok(!is_member
, "not a member\n");
4382 ret
= GetTokenInformation(r_token
, TokenGroups
, NULL
, 0, &size
);
4383 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "got %d with error %d\n",
4384 ret
, GetLastError());
4385 groups2
= HeapAlloc(GetProcessHeap(), 0, size
);
4386 ret
= GetTokenInformation(r_token
, TokenGroups
, groups2
, size
, &size
);
4387 ok(ret
, "got error %d\n", GetLastError());
4389 for (j
= 0; j
< groups2
->GroupCount
; j
++)
4391 if (EqualSid(groups2
->Groups
[j
].Sid
, token_groups
->Groups
[i
].Sid
))
4395 todo_wine
ok(groups2
->Groups
[j
].Attributes
& SE_GROUP_USE_FOR_DENY_ONLY
,
4396 "got wrong attributes\n");
4397 todo_wine
ok((groups2
->Groups
[j
].Attributes
& SE_GROUP_ENABLED
) == 0,
4398 "got wrong attributes\n");
4400 HeapFree(GetProcessHeap(), 0, groups2
);
4402 size
= sizeof(type
);
4403 ret
= GetTokenInformation(r_token
, TokenType
, &type
, size
, &size
);
4404 ok(ret
, "got error %d\n", GetLastError());
4405 ok(type
== TokenImpersonation
, "got type %u\n", type
);
4407 size
= sizeof(level
);
4408 ret
= GetTokenInformation(r_token
, TokenImpersonationLevel
, &level
, size
, &size
);
4409 ok(ret
, "got error %d\n", GetLastError());
4410 ok(level
== SecurityImpersonation
, "got level %u\n", type
);
4413 HeapFree(GetProcessHeap(), 0, token_groups
);
4414 CloseHandle(r_token
);
4416 CloseHandle(process_token
);
4419 static void validate_default_security_descriptor(SECURITY_DESCRIPTOR
*sd
)
4421 BOOL ret
, present
, defaulted
;
4425 ret
= IsValidSecurityDescriptor(sd
);
4426 ok(ret
, "security descriptor is not valid\n");
4430 acl
= (void *)0xdeadbeef;
4431 SetLastError(0xdeadbeef);
4432 ret
= GetSecurityDescriptorDacl(sd
, &present
, &acl
, &defaulted
);
4433 ok(ret
, "GetSecurityDescriptorDacl error %d\n", GetLastError());
4435 ok(present
== 1, "acl is not present\n");
4437 ok(acl
!= (void *)0xdeadbeef && acl
!= NULL
, "acl pointer is not set\n");
4438 ok(defaulted
== 0, "defaulted is set to TRUE\n");
4441 sid
= (void *)0xdeadbeef;
4442 SetLastError(0xdeadbeef);
4443 ret
= GetSecurityDescriptorOwner(sd
, &sid
, &defaulted
);
4444 ok(ret
, "GetSecurityDescriptorOwner error %d\n", GetLastError());
4446 ok(sid
!= (void *)0xdeadbeef && sid
!= NULL
, "sid pointer is not set\n");
4447 ok(defaulted
== 0, "defaulted is set to TRUE\n");
4450 sid
= (void *)0xdeadbeef;
4451 SetLastError(0xdeadbeef);
4452 ret
= GetSecurityDescriptorGroup(sd
, &sid
, &defaulted
);
4453 ok(ret
, "GetSecurityDescriptorGroup error %d\n", GetLastError());
4455 ok(sid
!= (void *)0xdeadbeef && sid
!= NULL
, "sid pointer is not set\n");
4456 ok(defaulted
== 0, "defaulted is set to TRUE\n");
4459 static void test_default_handle_security(HANDLE token
, HANDLE handle
, GENERIC_MAPPING
*mapping
)
4461 DWORD ret
, granted
, priv_set_len
;
4463 PRIVILEGE_SET priv_set
;
4464 SECURITY_DESCRIPTOR
*sd
;
4466 sd
= test_get_security_descriptor(handle
, __LINE__
);
4467 validate_default_security_descriptor(sd
);
4469 priv_set_len
= sizeof(priv_set
);
4470 granted
= 0xdeadbeef;
4471 status
= 0xdeadbeef;
4472 SetLastError(0xdeadbeef);
4473 ret
= AccessCheck(sd
, token
, MAXIMUM_ALLOWED
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4475 ok(ret
, "AccessCheck error %d\n", GetLastError());
4476 ok(status
== 1, "expected 1, got %d\n", status
);
4477 ok(granted
== mapping
->GenericAll
, "expected all access %#x, got %#x\n", mapping
->GenericAll
, granted
);
4479 priv_set_len
= sizeof(priv_set
);
4480 granted
= 0xdeadbeef;
4481 status
= 0xdeadbeef;
4482 SetLastError(0xdeadbeef);
4483 ret
= AccessCheck(sd
, token
, 0, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4485 ok(ret
, "AccessCheck error %d\n", GetLastError());
4486 ok(status
== 0 || broken(status
== 1) /* NT4 */, "expected 0, got %d\n", status
);
4487 ok(granted
== 0 || broken(granted
== mapping
->GenericRead
) /* NT4 */, "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
, ACCESS_SYSTEM_SECURITY
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4495 ok(ret
, "AccessCheck error %d\n", GetLastError());
4496 ok(status
== 0, "expected 0, got %d\n", status
);
4497 ok(granted
== 0, "expected 0, got %#x\n", granted
);
4499 priv_set_len
= sizeof(priv_set
);
4500 granted
= 0xdeadbeef;
4501 status
= 0xdeadbeef;
4502 SetLastError(0xdeadbeef);
4503 ret
= AccessCheck(sd
, token
, mapping
->GenericRead
, 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
->GenericRead
, "expected read access %#x, got %#x\n", mapping
->GenericRead
, granted
);
4509 priv_set_len
= sizeof(priv_set
);
4510 granted
= 0xdeadbeef;
4511 status
= 0xdeadbeef;
4512 SetLastError(0xdeadbeef);
4513 ret
= AccessCheck(sd
, token
, mapping
->GenericWrite
, 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
->GenericWrite
, "expected write access %#x, got %#x\n", mapping
->GenericWrite
, granted
);
4519 priv_set_len
= sizeof(priv_set
);
4520 granted
= 0xdeadbeef;
4521 status
= 0xdeadbeef;
4522 SetLastError(0xdeadbeef);
4523 ret
= AccessCheck(sd
, token
, mapping
->GenericExecute
, mapping
, &priv_set
, &priv_set_len
, &granted
, &status
);
4525 ok(ret
, "AccessCheck error %d\n", GetLastError());
4526 ok(status
== 1, "expected 1, got %d\n", status
);
4527 ok(granted
== mapping
->GenericExecute
, "expected execute access %#x, got %#x\n", mapping
->GenericExecute
, granted
);
4529 HeapFree(GetProcessHeap(), 0, sd
);
4532 static ACCESS_MASK
get_obj_access(HANDLE obj
)
4534 OBJECT_BASIC_INFORMATION info
;
4537 if (!pNtQueryObject
) return 0;
4539 status
= pNtQueryObject(obj
, ObjectBasicInformation
, &info
, sizeof(info
), NULL
);
4540 ok(!status
, "NtQueryObject error %#x\n", status
);
4542 return info
.GrantedAccess
;
4545 static void test_mutex_security(HANDLE token
)
4547 DWORD ret
, i
, access
;
4549 GENERIC_MAPPING mapping
= { STANDARD_RIGHTS_READ
| MUTANT_QUERY_STATE
| SYNCHRONIZE
,
4550 STANDARD_RIGHTS_WRITE
| MUTEX_MODIFY_STATE
| SYNCHRONIZE
,
4551 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
4552 STANDARD_RIGHTS_ALL
| MUTEX_ALL_ACCESS
};
4555 int generic
, mapped
;
4559 { GENERIC_READ
, STANDARD_RIGHTS_READ
| MUTANT_QUERY_STATE
},
4560 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
},
4561 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
4562 { GENERIC_ALL
, STANDARD_RIGHTS_ALL
| MUTANT_QUERY_STATE
}
4565 SetLastError(0xdeadbeef);
4566 mutex
= OpenMutexA(0, FALSE
, "WineTestMutex");
4567 ok(!mutex
, "mutex should not exist\n");
4568 ok(GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %u\n", GetLastError());
4570 SetLastError(0xdeadbeef);
4571 mutex
= CreateMutexA(NULL
, FALSE
, "WineTestMutex");
4572 ok(mutex
!= 0, "CreateMutex error %d\n", GetLastError());
4574 access
= get_obj_access(mutex
);
4575 ok(access
== MUTANT_ALL_ACCESS
, "expected MUTANT_ALL_ACCESS, got %#x\n", access
);
4577 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4579 SetLastError( 0xdeadbeef );
4580 ret
= DuplicateHandle(GetCurrentProcess(), mutex
, GetCurrentProcess(), &dup
,
4581 map
[i
].generic
, FALSE
, 0);
4582 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4584 access
= get_obj_access(dup
);
4585 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4589 SetLastError(0xdeadbeef);
4590 dup
= OpenMutexA(0, FALSE
, "WineTestMutex");
4592 ok(!dup
, "OpenMutex should fail\n");
4594 ok(GetLastError() == ERROR_ACCESS_DENIED
, "wrong error %u\n", GetLastError());
4597 test_default_handle_security(token
, mutex
, &mapping
);
4599 CloseHandle (mutex
);
4602 static void test_event_security(HANDLE token
)
4604 DWORD ret
, i
, access
;
4606 GENERIC_MAPPING mapping
= { STANDARD_RIGHTS_READ
| EVENT_QUERY_STATE
| SYNCHRONIZE
,
4607 STANDARD_RIGHTS_WRITE
| EVENT_MODIFY_STATE
| SYNCHRONIZE
,
4608 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
4609 STANDARD_RIGHTS_ALL
| EVENT_ALL_ACCESS
};
4612 int generic
, mapped
;
4616 { GENERIC_READ
, STANDARD_RIGHTS_READ
| EVENT_QUERY_STATE
},
4617 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| EVENT_MODIFY_STATE
},
4618 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
4619 { GENERIC_ALL
, STANDARD_RIGHTS_ALL
| EVENT_QUERY_STATE
| EVENT_MODIFY_STATE
}
4622 SetLastError(0xdeadbeef);
4623 event
= OpenEventA(0, FALSE
, "WineTestEvent");
4624 ok(!event
, "event should not exist\n");
4625 ok(GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %u\n", GetLastError());
4627 SetLastError(0xdeadbeef);
4628 event
= CreateEventA(NULL
, FALSE
, FALSE
, "WineTestEvent");
4629 ok(event
!= 0, "CreateEvent error %d\n", GetLastError());
4631 access
= get_obj_access(event
);
4632 ok(access
== EVENT_ALL_ACCESS
, "expected EVENT_ALL_ACCESS, got %#x\n", access
);
4634 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4636 SetLastError( 0xdeadbeef );
4637 ret
= DuplicateHandle(GetCurrentProcess(), event
, GetCurrentProcess(), &dup
,
4638 map
[i
].generic
, FALSE
, 0);
4639 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4641 access
= get_obj_access(dup
);
4642 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4646 SetLastError(0xdeadbeef);
4647 dup
= OpenEventA(0, FALSE
, "WineTestEvent");
4649 ok(!dup
, "OpenEvent should fail\n");
4651 ok(GetLastError() == ERROR_ACCESS_DENIED
, "wrong error %u\n", GetLastError());
4654 test_default_handle_security(token
, event
, &mapping
);
4659 static void test_semaphore_security(HANDLE token
)
4661 DWORD ret
, i
, access
;
4663 GENERIC_MAPPING mapping
= { STANDARD_RIGHTS_READ
| SEMAPHORE_QUERY_STATE
,
4664 STANDARD_RIGHTS_WRITE
| SEMAPHORE_MODIFY_STATE
,
4665 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
4666 STANDARD_RIGHTS_ALL
| SEMAPHORE_ALL_ACCESS
};
4669 int generic
, mapped
;
4673 { GENERIC_READ
, STANDARD_RIGHTS_READ
| SEMAPHORE_QUERY_STATE
},
4674 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| SEMAPHORE_MODIFY_STATE
},
4675 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
4676 { GENERIC_ALL
, STANDARD_RIGHTS_ALL
| SEMAPHORE_QUERY_STATE
| SEMAPHORE_MODIFY_STATE
}
4679 SetLastError(0xdeadbeef);
4680 sem
= OpenSemaphoreA(0, FALSE
, "WineTestSemaphore");
4681 ok(!sem
, "semaphore should not exist\n");
4682 ok(GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %u\n", GetLastError());
4684 SetLastError(0xdeadbeef);
4685 sem
= CreateSemaphoreA(NULL
, 0, 10, "WineTestSemaphore");
4686 ok(sem
!= 0, "CreateSemaphore error %d\n", GetLastError());
4688 access
= get_obj_access(sem
);
4689 ok(access
== SEMAPHORE_ALL_ACCESS
, "expected SEMAPHORE_ALL_ACCESS, got %#x\n", access
);
4691 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4693 SetLastError( 0xdeadbeef );
4694 ret
= DuplicateHandle(GetCurrentProcess(), sem
, GetCurrentProcess(), &dup
,
4695 map
[i
].generic
, FALSE
, 0);
4696 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4698 access
= get_obj_access(dup
);
4699 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4704 test_default_handle_security(token
, sem
, &mapping
);
4709 #define WINE_TEST_PIPE "\\\\.\\pipe\\WineTestPipe"
4710 static void test_named_pipe_security(HANDLE token
)
4712 DWORD ret
, i
, access
;
4713 HANDLE pipe
, file
, dup
;
4714 GENERIC_MAPPING mapping
= { FILE_GENERIC_READ
,
4716 FILE_GENERIC_EXECUTE
,
4717 STANDARD_RIGHTS_ALL
| FILE_ALL_ACCESS
};
4720 int todo
, generic
, mapped
;
4724 { 1, GENERIC_READ
, FILE_GENERIC_READ
},
4725 { 1, GENERIC_WRITE
, FILE_GENERIC_WRITE
},
4726 { 1, GENERIC_EXECUTE
, FILE_GENERIC_EXECUTE
},
4727 { 1, GENERIC_ALL
, STANDARD_RIGHTS_ALL
| FILE_ALL_ACCESS
}
4733 } creation_access
[] =
4735 { PIPE_ACCESS_INBOUND
, FILE_GENERIC_READ
},
4736 { PIPE_ACCESS_OUTBOUND
, FILE_GENERIC_WRITE
},
4737 { PIPE_ACCESS_DUPLEX
, FILE_GENERIC_READ
|FILE_GENERIC_WRITE
},
4738 { PIPE_ACCESS_INBOUND
|WRITE_DAC
, FILE_GENERIC_READ
|WRITE_DAC
},
4739 { PIPE_ACCESS_INBOUND
|WRITE_OWNER
, FILE_GENERIC_READ
|WRITE_OWNER
}
4740 /* ACCESS_SYSTEM_SECURITY is also valid, but will fail with ERROR_PRIVILEGE_NOT_HELD */
4743 /* Test the different security access options for pipes */
4744 for (i
= 0; i
< sizeof(creation_access
)/sizeof(creation_access
[0]); i
++)
4746 SetLastError(0xdeadbeef);
4747 pipe
= CreateNamedPipeA(WINE_TEST_PIPE
, creation_access
[i
].open_mode
,
4748 PIPE_TYPE_BYTE
| PIPE_NOWAIT
, PIPE_UNLIMITED_INSTANCES
, 0, 0,
4749 NMPWAIT_USE_DEFAULT_WAIT
, NULL
);
4750 ok(pipe
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe(0x%x) error %d\n",
4751 creation_access
[i
].open_mode
, GetLastError());
4752 access
= get_obj_access(pipe
);
4753 ok(access
== creation_access
[i
].access
,
4754 "CreateNamedPipeA(0x%x) pipe expected access 0x%x (got 0x%x)\n",
4755 creation_access
[i
].open_mode
, creation_access
[i
].access
, access
);
4759 SetLastError(0xdeadbeef);
4760 pipe
= CreateNamedPipeA(WINE_TEST_PIPE
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_FIRST_PIPE_INSTANCE
,
4761 PIPE_TYPE_BYTE
| PIPE_NOWAIT
, PIPE_UNLIMITED_INSTANCES
,
4762 0, 0, NMPWAIT_USE_DEFAULT_WAIT
, NULL
);
4763 ok(pipe
!= INVALID_HANDLE_VALUE
, "CreateNamedPipe error %d\n", GetLastError());
4765 test_default_handle_security(token
, pipe
, &mapping
);
4767 SetLastError(0xdeadbeef);
4768 file
= CreateFileA(WINE_TEST_PIPE
, FILE_ALL_ACCESS
, 0, NULL
, OPEN_EXISTING
, 0, 0);
4769 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4771 access
= get_obj_access(file
);
4772 ok(access
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", access
);
4774 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4776 SetLastError( 0xdeadbeef );
4777 ret
= DuplicateHandle(GetCurrentProcess(), file
, GetCurrentProcess(), &dup
,
4778 map
[i
].generic
, FALSE
, 0);
4779 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4781 access
= get_obj_access(dup
);
4782 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4790 SetLastError(0xdeadbeef);
4791 file
= CreateFileA("\\\\.\\pipe\\", FILE_ALL_ACCESS
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, 0);
4792 ok(file
!= INVALID_HANDLE_VALUE
|| broken(file
== INVALID_HANDLE_VALUE
) /* before Vista */, "CreateFile error %d\n", GetLastError());
4794 if (file
!= INVALID_HANDLE_VALUE
)
4796 access
= get_obj_access(file
);
4797 ok(access
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", access
);
4799 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4801 SetLastError( 0xdeadbeef );
4802 ret
= DuplicateHandle(GetCurrentProcess(), file
, GetCurrentProcess(), &dup
,
4803 map
[i
].generic
, FALSE
, 0);
4804 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4806 access
= get_obj_access(dup
);
4809 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4811 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4820 static void test_file_security(HANDLE token
)
4822 DWORD ret
, i
, access
, bytes
;
4826 int generic
, mapped
;
4830 { GENERIC_READ
, FILE_GENERIC_READ
},
4831 { GENERIC_WRITE
, FILE_GENERIC_WRITE
},
4832 { GENERIC_EXECUTE
, FILE_GENERIC_EXECUTE
},
4833 { GENERIC_ALL
, STANDARD_RIGHTS_ALL
| FILE_ALL_ACCESS
}
4835 char temp_path
[MAX_PATH
];
4836 char file_name
[MAX_PATH
];
4839 GetTempPathA(MAX_PATH
, temp_path
);
4840 GetTempFileNameA(temp_path
, "tmp", 0, file_name
);
4843 SetLastError(0xdeadbeef);
4844 file
= CreateFileA(file_name
, GENERIC_ALL
, 0, NULL
, CREATE_ALWAYS
, 0, NULL
);
4845 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4847 access
= get_obj_access(file
);
4848 ok(access
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", access
);
4850 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4852 SetLastError( 0xdeadbeef );
4853 ret
= DuplicateHandle(GetCurrentProcess(), file
, GetCurrentProcess(), &dup
,
4854 map
[i
].generic
, FALSE
, 0);
4855 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4857 access
= get_obj_access(dup
);
4858 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4865 SetLastError(0xdeadbeef);
4866 file
= CreateFileA(file_name
, 0, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
4867 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4869 access
= get_obj_access(file
);
4871 ok(access
== (FILE_READ_ATTRIBUTES
| SYNCHRONIZE
), "expected FILE_READ_ATTRIBUTES | SYNCHRONIZE, got %#x\n", access
);
4874 SetLastError(0xdeadbeef);
4875 ret
= ReadFile(file
, buf
, sizeof(buf
), &bytes
, NULL
);
4876 ok(!ret
, "ReadFile should fail\n");
4877 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
4878 ok(bytes
== 0, "expected 0, got %u\n", bytes
);
4882 SetLastError(0xdeadbeef);
4883 file
= CreateFileA(file_name
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
4884 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4886 access
= get_obj_access(file
);
4888 ok(access
== (FILE_GENERIC_WRITE
| FILE_READ_ATTRIBUTES
), "expected FILE_GENERIC_WRITE | FILE_READ_ATTRIBUTES, got %#x\n", access
);
4891 SetLastError(0xdeadbeef);
4892 ret
= ReadFile(file
, buf
, sizeof(buf
), &bytes
, NULL
);
4893 ok(!ret
, "ReadFile should fail\n");
4894 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
4895 ok(bytes
== 0, "expected 0, got %u\n", bytes
);
4898 DeleteFileA(file_name
);
4901 SetLastError(0xdeadbeef);
4902 file
= CreateFileA(temp_path
, GENERIC_ALL
, 0, NULL
, OPEN_EXISTING
, FILE_FLAG_BACKUP_SEMANTICS
, 0);
4903 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4905 access
= get_obj_access(file
);
4906 ok(access
== FILE_ALL_ACCESS
, "expected FILE_ALL_ACCESS, got %#x\n", access
);
4908 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
4910 SetLastError( 0xdeadbeef );
4911 ret
= DuplicateHandle(GetCurrentProcess(), file
, GetCurrentProcess(), &dup
,
4912 map
[i
].generic
, FALSE
, 0);
4913 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
4915 access
= get_obj_access(dup
);
4916 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
4923 SetLastError(0xdeadbeef);
4924 file
= CreateFileA(temp_path
, 0, 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_READ_ATTRIBUTES
| SYNCHRONIZE
), "expected FILE_READ_ATTRIBUTES | SYNCHRONIZE, got %#x\n", access
);
4933 SetLastError(0xdeadbeef);
4934 file
= CreateFileA(temp_path
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, FILE_FLAG_BACKUP_SEMANTICS
, 0);
4935 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4937 access
= get_obj_access(file
);
4939 ok(access
== (FILE_GENERIC_WRITE
| FILE_READ_ATTRIBUTES
), "expected FILE_GENERIC_WRITE | FILE_READ_ATTRIBUTES, got %#x\n", access
);
4944 static void test_filemap_security(void)
4946 char temp_path
[MAX_PATH
];
4947 char file_name
[MAX_PATH
];
4948 DWORD ret
, i
, access
;
4949 HANDLE file
, mapping
, dup
;
4952 int generic
, mapped
;
4956 { GENERIC_READ
, STANDARD_RIGHTS_READ
| SECTION_QUERY
| SECTION_MAP_READ
},
4957 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| SECTION_MAP_WRITE
},
4958 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SECTION_MAP_EXECUTE
},
4959 { GENERIC_ALL
, STANDARD_RIGHTS_REQUIRED
| SECTION_ALL_ACCESS
}
4967 { PAGE_NOACCESS
, 0 },
4968 { PAGE_READONLY
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
},
4969 { PAGE_READWRITE
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
},
4970 { PAGE_WRITECOPY
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
},
4971 { PAGE_EXECUTE
, 0 },
4972 { PAGE_EXECUTE_READ
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_EXECUTE
},
4973 { PAGE_EXECUTE_READWRITE
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
| SECTION_MAP_EXECUTE
},
4974 { PAGE_EXECUTE_WRITECOPY
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_EXECUTE
}
4977 GetTempPathA(MAX_PATH
, temp_path
);
4978 GetTempFileNameA(temp_path
, "tmp", 0, file_name
);
4980 SetLastError(0xdeadbeef);
4981 file
= CreateFileA(file_name
, GENERIC_READ
|GENERIC_WRITE
|GENERIC_EXECUTE
, 0, NULL
, CREATE_ALWAYS
, 0, 0);
4982 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
4983 SetFilePointer(file
, 4096, NULL
, FILE_BEGIN
);
4986 for (i
= 0; i
< sizeof(prot_map
)/sizeof(prot_map
[0]); i
++)
4988 SetLastError(0xdeadbeef);
4989 mapping
= CreateFileMappingW(file
, NULL
, prot_map
[i
].prot
, 0, 4096, NULL
);
4990 if (prot_map
[i
].mapped
)
4994 /* NT4 and win2k don't support EXEC on file mappings */
4995 if (prot_map
[i
].prot
== PAGE_EXECUTE_READ
|| prot_map
[i
].prot
== PAGE_EXECUTE_READWRITE
|| prot_map
[i
].prot
== PAGE_EXECUTE_WRITECOPY
)
4997 win_skip("CreateFileMapping doesn't support PAGE_EXECUTE protection\n");
5001 ok(mapping
!= 0, "CreateFileMapping(%04x) error %d\n", prot_map
[i
].prot
, GetLastError());
5005 ok(!mapping
, "CreateFileMapping(%04x) should fail\n", prot_map
[i
].prot
);
5006 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
5010 access
= get_obj_access(mapping
);
5011 ok(access
== prot_map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, prot_map
[i
].mapped
, access
);
5013 CloseHandle(mapping
);
5016 SetLastError(0xdeadbeef);
5017 mapping
= CreateFileMappingW(file
, NULL
, PAGE_EXECUTE_READWRITE
, 0, 4096, NULL
);
5020 /* NT4 and win2k don't support EXEC on file mappings */
5021 win_skip("CreateFileMapping doesn't support PAGE_EXECUTE protection\n");
5023 DeleteFileA(file_name
);
5026 ok(mapping
!= 0, "CreateFileMapping error %d\n", GetLastError());
5028 access
= get_obj_access(mapping
);
5029 ok(access
== (STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
| SECTION_MAP_WRITE
| SECTION_MAP_EXECUTE
),
5030 "expected STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ | SECTION_MAP_WRITE | SECTION_MAP_EXECUTE, got %#x\n", access
);
5032 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5034 SetLastError( 0xdeadbeef );
5035 ret
= DuplicateHandle(GetCurrentProcess(), mapping
, GetCurrentProcess(), &dup
,
5036 map
[i
].generic
, FALSE
, 0);
5037 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5039 access
= get_obj_access(dup
);
5040 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5045 CloseHandle(mapping
);
5047 DeleteFileA(file_name
);
5050 static void test_thread_security(void)
5052 DWORD ret
, i
, access
;
5056 int generic
, mapped
;
5060 { GENERIC_READ
, STANDARD_RIGHTS_READ
| THREAD_QUERY_INFORMATION
| THREAD_GET_CONTEXT
},
5061 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| THREAD_SET_INFORMATION
| THREAD_SET_CONTEXT
| THREAD_TERMINATE
| THREAD_SUSPEND_RESUME
| 0x4 },
5062 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
5063 { GENERIC_ALL
, THREAD_ALL_ACCESS_NT4
}
5066 SetLastError(0xdeadbeef);
5067 thread
= CreateThread(NULL
, 0, (void *)0xdeadbeef, NULL
, CREATE_SUSPENDED
, &ret
);
5068 ok(thread
!= 0, "CreateThread error %d\n", GetLastError());
5070 access
= get_obj_access(thread
);
5071 ok(access
== THREAD_ALL_ACCESS_NT4
|| access
== THREAD_ALL_ACCESS_VISTA
, "expected THREAD_ALL_ACCESS, got %#x\n", access
);
5073 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5075 SetLastError( 0xdeadbeef );
5076 ret
= DuplicateHandle(GetCurrentProcess(), thread
, GetCurrentProcess(), &dup
,
5077 map
[i
].generic
, FALSE
, 0);
5078 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5080 access
= get_obj_access(dup
);
5081 switch (map
[i
].generic
)
5084 case GENERIC_EXECUTE
:
5085 ok(access
== map
[i
].mapped
||
5086 access
== (map
[i
].mapped
| THREAD_QUERY_LIMITED_INFORMATION
) /* Vista+ */ ||
5087 access
== (map
[i
].mapped
| THREAD_QUERY_LIMITED_INFORMATION
| THREAD_RESUME
) /* win8 */,
5088 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5092 ok(access
== map
[i
].mapped
||
5093 access
== (map
[i
].mapped
| THREAD_SET_LIMITED_INFORMATION
) /* Vista+ */ ||
5094 access
== (map
[i
].mapped
| THREAD_SET_LIMITED_INFORMATION
| THREAD_RESUME
) /* win8 */,
5095 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5098 ok(access
== map
[i
].mapped
|| access
== THREAD_ALL_ACCESS_VISTA
,
5099 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5102 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5109 TerminateThread(thread
, 0);
5110 CloseHandle(thread
);
5113 static void test_process_access(void)
5115 DWORD ret
, i
, access
;
5116 HANDLE process
, dup
;
5118 PROCESS_INFORMATION pi
;
5119 char cmdline
[] = "winver.exe";
5122 int generic
, mapped
;
5126 { GENERIC_READ
, STANDARD_RIGHTS_READ
| PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
},
5127 { GENERIC_WRITE
, STANDARD_RIGHTS_WRITE
| PROCESS_SET_QUOTA
| PROCESS_SET_INFORMATION
| PROCESS_SUSPEND_RESUME
|
5128 PROCESS_VM_WRITE
| PROCESS_DUP_HANDLE
| PROCESS_CREATE_PROCESS
| PROCESS_CREATE_THREAD
| PROCESS_VM_OPERATION
},
5129 { GENERIC_EXECUTE
, STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
},
5130 { GENERIC_ALL
, PROCESS_ALL_ACCESS_NT4
}
5133 memset(&sti
, 0, sizeof(sti
));
5134 sti
.cb
= sizeof(sti
);
5135 SetLastError(0xdeadbeef);
5136 ret
= CreateProcessA(NULL
, cmdline
, NULL
, NULL
, FALSE
, CREATE_SUSPENDED
, NULL
, NULL
, &sti
, &pi
);
5137 ok(ret
, "CreateProcess() error %d\n", GetLastError());
5139 CloseHandle(pi
.hThread
);
5140 process
= pi
.hProcess
;
5142 access
= get_obj_access(process
);
5143 ok(access
== PROCESS_ALL_ACCESS_NT4
|| access
== PROCESS_ALL_ACCESS_VISTA
, "expected PROCESS_ALL_ACCESS, got %#x\n", access
);
5145 for (i
= 0; i
< sizeof(map
)/sizeof(map
[0]); i
++)
5147 SetLastError( 0xdeadbeef );
5148 ret
= DuplicateHandle(GetCurrentProcess(), process
, GetCurrentProcess(), &dup
,
5149 map
[i
].generic
, FALSE
, 0);
5150 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
5152 access
= get_obj_access(dup
);
5153 switch (map
[i
].generic
)
5156 ok(access
== map
[i
].mapped
|| access
== (map
[i
].mapped
| PROCESS_QUERY_LIMITED_INFORMATION
) /* Vista+ */,
5157 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5160 ok(access
== map
[i
].mapped
||
5161 access
== (map
[i
].mapped
| PROCESS_TERMINATE
) /* before Vista */ ||
5162 access
== (map
[i
].mapped
| PROCESS_SET_LIMITED_INFORMATION
) /* win8 */,
5163 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5165 case GENERIC_EXECUTE
:
5166 ok(access
== map
[i
].mapped
|| access
== (map
[i
].mapped
| PROCESS_QUERY_LIMITED_INFORMATION
| PROCESS_TERMINATE
) /* Vista+ */,
5167 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5170 ok(access
== map
[i
].mapped
|| access
== PROCESS_ALL_ACCESS_VISTA
,
5171 "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5174 ok(access
== map
[i
].mapped
, "%d: expected %#x, got %#x\n", i
, map
[i
].mapped
, access
);
5181 TerminateProcess(process
, 0);
5182 CloseHandle(process
);
5185 static BOOL
validate_impersonation_token(HANDLE token
, DWORD
*token_type
)
5189 SECURITY_IMPERSONATION_LEVEL sil
;
5193 SetLastError(0xdeadbeef);
5194 ret
= GetTokenInformation(token
, TokenType
, &type
, sizeof(type
), &needed
);
5195 ok(ret
, "GetTokenInformation error %d\n", GetLastError());
5196 ok(needed
== sizeof(type
), "GetTokenInformation should return required buffer length\n");
5197 ok(type
== TokenPrimary
|| type
== TokenImpersonation
, "expected TokenPrimary or TokenImpersonation, got %d\n", type
);
5200 if (type
!= TokenImpersonation
) return FALSE
;
5203 SetLastError(0xdeadbeef);
5204 ret
= GetTokenInformation(token
, TokenImpersonationLevel
, &sil
, sizeof(sil
), &needed
);
5205 ok(ret
, "GetTokenInformation error %d\n", GetLastError());
5206 ok(needed
== sizeof(sil
), "GetTokenInformation should return required buffer length\n");
5207 ok(sil
== SecurityImpersonation
, "expected SecurityImpersonation, got %d\n", sil
);
5209 needed
= 0xdeadbeef;
5210 SetLastError(0xdeadbeef);
5211 ret
= GetTokenInformation(token
, TokenDefaultDacl
, 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_DEFAULT_DACL
), "GetTokenInformation returned empty default DACL\n");
5217 needed
= 0xdeadbeef;
5218 SetLastError(0xdeadbeef);
5219 ret
= GetTokenInformation(token
, TokenOwner
, NULL
, 0, &needed
);
5220 ok(!ret
, "GetTokenInformation should fail\n");
5221 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
5222 ok(needed
!= 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
5223 ok(needed
> sizeof(TOKEN_OWNER
), "GetTokenInformation returned empty token owner\n");
5225 needed
= 0xdeadbeef;
5226 SetLastError(0xdeadbeef);
5227 ret
= GetTokenInformation(token
, TokenPrimaryGroup
, NULL
, 0, &needed
);
5228 ok(!ret
, "GetTokenInformation should fail\n");
5229 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
5230 ok(needed
!= 0xdeadbeef, "GetTokenInformation should return required buffer length\n");
5231 ok(needed
> sizeof(TOKEN_PRIMARY_GROUP
), "GetTokenInformation returned empty token primary group\n");
5236 static void test_kernel_objects_security(void)
5238 HANDLE token
, process_token
;
5239 DWORD ret
, token_type
;
5241 ret
= OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE
| TOKEN_QUERY
, &process_token
);
5242 ok(ret
, "OpenProcessToken error %d\n", GetLastError());
5244 ret
= validate_impersonation_token(process_token
, &token_type
);
5245 ok(token_type
== TokenPrimary
, "expected TokenPrimary, got %d\n", token_type
);
5246 ok(!ret
, "access token should not be an impersonation token\n");
5248 ret
= DuplicateToken(process_token
, SecurityImpersonation
, &token
);
5249 ok(ret
, "DuplicateToken error %d\n", GetLastError());
5251 ret
= validate_impersonation_token(token
, &token_type
);
5252 ok(ret
, "access token should be a valid impersonation token\n");
5253 ok(token_type
== TokenImpersonation
, "expected TokenImpersonation, got %d\n", token_type
);
5255 test_mutex_security(token
);
5256 test_event_security(token
);
5257 test_named_pipe_security(token
);
5258 test_semaphore_security(token
);
5259 test_file_security(token
);
5260 test_filemap_security();
5261 test_thread_security();
5262 test_process_access();
5263 /* FIXME: test other kernel object types */
5265 CloseHandle(process_token
);
5269 static void test_TokenIntegrityLevel(void)
5271 TOKEN_MANDATORY_LABEL
*tml
;
5272 BYTE buffer
[64]; /* using max. 28 byte in win7 x64 */
5276 char *sidname
= NULL
;
5277 static SID medium_level
= {SID_REVISION
, 1, {SECURITY_MANDATORY_LABEL_AUTHORITY
},
5278 {SECURITY_MANDATORY_HIGH_RID
}};
5279 static SID high_level
= {SID_REVISION
, 1, {SECURITY_MANDATORY_LABEL_AUTHORITY
},
5280 {SECURITY_MANDATORY_MEDIUM_RID
}};
5282 SetLastError(0xdeadbeef);
5283 res
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
, &token
);
5284 ok(res
, "got %d with %d (expected TRUE)\n", res
, GetLastError());
5286 SetLastError(0xdeadbeef);
5287 res
= GetTokenInformation(token
, TokenIntegrityLevel
, buffer
, sizeof(buffer
), &size
);
5289 /* not supported before Vista */
5290 if (!res
&& ((GetLastError() == ERROR_INVALID_PARAMETER
) || GetLastError() == ERROR_INVALID_FUNCTION
))
5292 win_skip("TokenIntegrityLevel not supported\n");
5297 ok(res
, "got %u with %u (expected TRUE)\n", res
, GetLastError());
5304 tml
= (TOKEN_MANDATORY_LABEL
*) buffer
;
5305 ok(tml
->Label
.Attributes
== (SE_GROUP_INTEGRITY
| SE_GROUP_INTEGRITY_ENABLED
),
5306 "got 0x%x (expected 0x%x)\n", tml
->Label
.Attributes
, (SE_GROUP_INTEGRITY
| SE_GROUP_INTEGRITY_ENABLED
));
5308 SetLastError(0xdeadbeef);
5309 res
= pConvertSidToStringSidA(tml
->Label
.Sid
, &sidname
);
5310 ok(res
, "got %u and %u\n", res
, GetLastError());
5312 ok(EqualSid(tml
->Label
.Sid
, &medium_level
) || EqualSid(tml
->Label
.Sid
, &high_level
),
5313 "got %s (expected 'S-1-16-8192' or 'S-1-16-12288')\n", sidname
);
5319 static void test_default_dacl_owner_sid(void)
5322 BOOL ret
, defaulted
, present
, found
;
5324 SECURITY_DESCRIPTOR
*sd
;
5325 SECURITY_ATTRIBUTES sa
;
5328 ACCESS_ALLOWED_ACE
*ace
;
5330 sd
= HeapAlloc( GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH
);
5331 ret
= InitializeSecurityDescriptor( sd
, SECURITY_DESCRIPTOR_REVISION
);
5332 ok( ret
, "error %u\n", GetLastError() );
5334 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
5335 sa
.lpSecurityDescriptor
= sd
;
5336 sa
.bInheritHandle
= FALSE
;
5337 handle
= CreateEventA( &sa
, TRUE
, TRUE
, "test_event" );
5338 ok( handle
!= NULL
, "error %u\n", GetLastError() );
5341 ret
= GetKernelObjectSecurity( handle
, OWNER_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, NULL
, 0, &size
);
5342 ok( !ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "error %u\n", GetLastError() );
5344 sd
= HeapAlloc( GetProcessHeap(), 0, size
);
5345 ret
= GetKernelObjectSecurity( handle
, OWNER_SECURITY_INFORMATION
|DACL_SECURITY_INFORMATION
, sd
, size
, &size
);
5346 ok( ret
, "error %u\n", GetLastError() );
5348 owner
= (void *)0xdeadbeef;
5350 ret
= GetSecurityDescriptorOwner( sd
, &owner
, &defaulted
);
5351 ok( ret
, "error %u\n", GetLastError() );
5352 ok( owner
!= (void *)0xdeadbeef, "owner not set\n" );
5353 ok( !defaulted
, "owner defaulted\n" );
5355 dacl
= (void *)0xdeadbeef;
5358 ret
= GetSecurityDescriptorDacl( sd
, &present
, &dacl
, &defaulted
);
5359 ok( ret
, "error %u\n", GetLastError() );
5360 ok( present
, "dacl not present\n" );
5361 ok( dacl
!= (void *)0xdeadbeef, "dacl not set\n" );
5362 ok( !defaulted
, "dacl defaulted\n" );
5366 while (pGetAce( dacl
, index
++, (void **)&ace
))
5368 if (EqualSid( &ace
->SidStart
, owner
)) found
= TRUE
;
5370 ok( found
, "owner sid not found in dacl\n" );
5372 HeapFree( GetProcessHeap(), 0, sa
.lpSecurityDescriptor
);
5373 HeapFree( GetProcessHeap(), 0, sd
);
5374 CloseHandle( handle
);
5377 static void test_AdjustTokenPrivileges(void)
5379 TOKEN_PRIVILEGES tp
, prev
;
5385 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
, &token
))
5388 if (!LookupPrivilegeValueA(NULL
, SE_BACKUP_NAME
, &luid
))
5394 tp
.PrivilegeCount
= 1;
5395 tp
.Privileges
[0].Luid
= luid
;
5396 tp
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
5399 ret
= AdjustTokenPrivileges(token
, FALSE
, &tp
, sizeof(TOKEN_PRIVILEGES
), NULL
, &len
);
5400 ok(ret
, "got %d\n", ret
);
5401 ok(len
== 0xdeadbeef, "got length %d\n", len
);
5404 tp
.PrivilegeCount
= 1;
5405 tp
.Privileges
[0].Luid
= luid
;
5406 tp
.Privileges
[0].Attributes
= 0;
5407 AdjustTokenPrivileges(token
, FALSE
, &tp
, sizeof(TOKEN_PRIVILEGES
), &prev
, NULL
);
5412 START_TEST(security
)
5419 test_process_security_child();
5422 test_kernel_objects_security();
5426 test_CreateWellKnownSid();
5427 test_FileSecurity();
5430 test_LookupAccountSid();
5431 test_LookupAccountName();
5432 test_security_descriptor();
5433 test_process_security();
5434 test_impersonation_level();
5435 test_SetEntriesInAclW();
5436 test_SetEntriesInAclA();
5437 test_CreateDirectoryA();
5438 test_GetNamedSecurityInfoA();
5439 test_ConvertStringSecurityDescriptor();
5440 test_ConvertSecurityDescriptorToString();
5441 test_PrivateObjectSecurity();
5443 test_GetSecurityInfo();
5444 test_GetSidSubAuthority();
5445 test_CheckTokenMembership();
5447 test_GetUserNameA();
5448 test_GetUserNameW();
5449 test_CreateRestrictedToken();
5450 test_TokenIntegrityLevel();
5451 test_default_dacl_owner_sid();
5452 test_AdjustTokenPrivileges();