push a6a7df41ae272b3b1bf5fcfb544267c1e96afa99
[wine/hacks.git] / dlls / advapi32 / tests / security.c
blobd4f532d9b89f1fe7016a2fd67eb99e053c38c1a3
1 /*
2 * Unit tests for security functions
4 * Copyright (c) 2004 Mike McCormack
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <stdio.h>
24 #include "ntstatus.h"
25 #define WIN32_NO_STATUS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "aclapi.h"
30 #include "winnt.h"
31 #include "sddl.h"
32 #include "ntsecapi.h"
33 #include "lmcons.h"
35 #include "wine/test.h"
37 /* copied from Wine winternl.h - not included in the Windows SDK */
38 typedef enum _OBJECT_INFORMATION_CLASS {
39 ObjectBasicInformation,
40 ObjectNameInformation,
41 ObjectTypeInformation,
42 ObjectAllInformation,
43 ObjectDataInformation
44 } OBJECT_INFORMATION_CLASS, *POBJECT_INFORMATION_CLASS;
46 typedef struct _OBJECT_BASIC_INFORMATION {
47 ULONG Attributes;
48 ACCESS_MASK GrantedAccess;
49 ULONG HandleCount;
50 ULONG PointerCount;
51 ULONG PagedPoolUsage;
52 ULONG NonPagedPoolUsage;
53 ULONG Reserved[3];
54 ULONG NameInformationLength;
55 ULONG TypeInformationLength;
56 ULONG SecurityDescriptorLength;
57 LARGE_INTEGER CreateTime;
58 } OBJECT_BASIC_INFORMATION, *POBJECT_BASIC_INFORMATION;
60 #define expect_eq(expr, value, type, format) { type ret = expr; ok((value) == ret, #expr " expected " format " got " format "\n", (value), (ret)); }
62 static BOOL (WINAPI *pAddAccessAllowedAceEx)(PACL, DWORD, DWORD, DWORD, PSID);
63 static BOOL (WINAPI *pAddAccessDeniedAceEx)(PACL, DWORD, DWORD, DWORD, PSID);
64 static BOOL (WINAPI *pAddAuditAccessAceEx)(PACL, DWORD, DWORD, DWORD, PSID, BOOL, BOOL);
65 typedef VOID (WINAPI *fnBuildTrusteeWithSidA)( PTRUSTEEA pTrustee, PSID pSid );
66 typedef VOID (WINAPI *fnBuildTrusteeWithNameA)( PTRUSTEEA pTrustee, LPSTR pName );
67 typedef VOID (WINAPI *fnBuildTrusteeWithObjectsAndNameA)( PTRUSTEEA pTrustee,
68 POBJECTS_AND_NAME_A pObjName,
69 SE_OBJECT_TYPE ObjectType,
70 LPSTR ObjectTypeName,
71 LPSTR InheritedObjectTypeName,
72 LPSTR Name );
73 typedef VOID (WINAPI *fnBuildTrusteeWithObjectsAndSidA)( PTRUSTEEA pTrustee,
74 POBJECTS_AND_SID pObjSid,
75 GUID* pObjectGuid,
76 GUID* pInheritedObjectGuid,
77 PSID pSid );
78 typedef LPSTR (WINAPI *fnGetTrusteeNameA)( PTRUSTEEA pTrustee );
79 typedef BOOL (WINAPI *fnMakeSelfRelativeSD)( PSECURITY_DESCRIPTOR, PSECURITY_DESCRIPTOR, LPDWORD );
80 typedef BOOL (WINAPI *fnConvertSidToStringSidA)( PSID pSid, LPSTR *str );
81 typedef BOOL (WINAPI *fnConvertStringSidToSidA)( LPCSTR str, PSID pSid );
82 static BOOL (WINAPI *pConvertStringSecurityDescriptorToSecurityDescriptorA)(LPCSTR, DWORD,
83 PSECURITY_DESCRIPTOR*, PULONG );
84 static BOOL (WINAPI *pConvertStringSecurityDescriptorToSecurityDescriptorW)(LPCWSTR, DWORD,
85 PSECURITY_DESCRIPTOR*, PULONG );
86 static BOOL (WINAPI *pConvertSecurityDescriptorToStringSecurityDescriptorA)(PSECURITY_DESCRIPTOR, DWORD,
87 SECURITY_INFORMATION, LPSTR *, PULONG );
88 typedef BOOL (WINAPI *fnGetFileSecurityA)(LPCSTR, SECURITY_INFORMATION,
89 PSECURITY_DESCRIPTOR, DWORD, LPDWORD);
90 static DWORD (WINAPI *pGetNamedSecurityInfoA)(LPSTR, SE_OBJECT_TYPE, SECURITY_INFORMATION,
91 PSID*, PSID*, PACL*, PACL*,
92 PSECURITY_DESCRIPTOR*);
93 typedef DWORD (WINAPI *fnRtlAdjustPrivilege)(ULONG,BOOLEAN,BOOLEAN,PBOOLEAN);
94 typedef BOOL (WINAPI *fnCreateWellKnownSid)(WELL_KNOWN_SID_TYPE,PSID,PSID,DWORD*);
95 typedef BOOL (WINAPI *fnDuplicateTokenEx)(HANDLE,DWORD,LPSECURITY_ATTRIBUTES,
96 SECURITY_IMPERSONATION_LEVEL,TOKEN_TYPE,PHANDLE);
98 typedef NTSTATUS (WINAPI *fnLsaQueryInformationPolicy)(LSA_HANDLE,POLICY_INFORMATION_CLASS,PVOID*);
99 typedef NTSTATUS (WINAPI *fnLsaClose)(LSA_HANDLE);
100 typedef NTSTATUS (WINAPI *fnLsaFreeMemory)(PVOID);
101 typedef NTSTATUS (WINAPI *fnLsaOpenPolicy)(PLSA_UNICODE_STRING,PLSA_OBJECT_ATTRIBUTES,ACCESS_MASK,PLSA_HANDLE);
102 static NTSTATUS (WINAPI *pNtQueryObject)(HANDLE,OBJECT_INFORMATION_CLASS,PVOID,ULONG,PULONG);
103 static DWORD (WINAPI *pSetEntriesInAclW)(ULONG, PEXPLICIT_ACCESSW, PACL, PACL*);
104 static BOOL (WINAPI *pSetSecurityDescriptorControl)(PSECURITY_DESCRIPTOR, SECURITY_DESCRIPTOR_CONTROL,
105 SECURITY_DESCRIPTOR_CONTROL);
106 static DWORD (WINAPI *pGetSecurityInfo)(HANDLE, SE_OBJECT_TYPE, SECURITY_INFORMATION,
107 PSID*, PSID*, PACL*, PACL*, PSECURITY_DESCRIPTOR*);
109 static HMODULE hmod;
110 static int myARGC;
111 static char** myARGV;
113 fnBuildTrusteeWithSidA pBuildTrusteeWithSidA;
114 fnBuildTrusteeWithNameA pBuildTrusteeWithNameA;
115 fnBuildTrusteeWithObjectsAndNameA pBuildTrusteeWithObjectsAndNameA;
116 fnBuildTrusteeWithObjectsAndSidA pBuildTrusteeWithObjectsAndSidA;
117 fnGetTrusteeNameA pGetTrusteeNameA;
118 fnMakeSelfRelativeSD pMakeSelfRelativeSD;
119 fnConvertSidToStringSidA pConvertSidToStringSidA;
120 fnConvertStringSidToSidA pConvertStringSidToSidA;
121 fnGetFileSecurityA pGetFileSecurityA;
122 fnRtlAdjustPrivilege pRtlAdjustPrivilege;
123 fnCreateWellKnownSid pCreateWellKnownSid;
124 fnDuplicateTokenEx pDuplicateTokenEx;
125 fnLsaQueryInformationPolicy pLsaQueryInformationPolicy;
126 fnLsaClose pLsaClose;
127 fnLsaFreeMemory pLsaFreeMemory;
128 fnLsaOpenPolicy pLsaOpenPolicy;
130 struct sidRef
132 SID_IDENTIFIER_AUTHORITY auth;
133 const char *refStr;
136 static void init(void)
138 HMODULE hntdll;
140 hntdll = GetModuleHandleA("ntdll.dll");
141 pNtQueryObject = (void *)GetProcAddress( hntdll, "NtQueryObject" );
143 hmod = GetModuleHandle("advapi32.dll");
144 pAddAccessAllowedAceEx = (void *)GetProcAddress(hmod, "AddAccessAllowedAceEx");
145 pAddAccessDeniedAceEx = (void *)GetProcAddress(hmod, "AddAccessDeniedAceEx");
146 pAddAuditAccessAceEx = (void *)GetProcAddress(hmod, "AddAuditAccessAceEx");
147 pConvertStringSecurityDescriptorToSecurityDescriptorA =
148 (void *)GetProcAddress(hmod, "ConvertStringSecurityDescriptorToSecurityDescriptorA" );
149 pConvertStringSecurityDescriptorToSecurityDescriptorW =
150 (void *)GetProcAddress(hmod, "ConvertStringSecurityDescriptorToSecurityDescriptorW" );
151 pConvertSecurityDescriptorToStringSecurityDescriptorA =
152 (void *)GetProcAddress(hmod, "ConvertSecurityDescriptorToStringSecurityDescriptorA" );
153 pCreateWellKnownSid = (fnCreateWellKnownSid)GetProcAddress( hmod, "CreateWellKnownSid" );
154 pGetNamedSecurityInfoA = (void *)GetProcAddress(hmod, "GetNamedSecurityInfoA");
155 pMakeSelfRelativeSD = (void *)GetProcAddress(hmod, "MakeSelfRelativeSD");
156 pSetEntriesInAclW = (void *)GetProcAddress(hmod, "SetEntriesInAclW");
157 pSetSecurityDescriptorControl = (void *)GetProcAddress(hmod, "SetSecurityDescriptorControl");
158 pGetSecurityInfo = (void *)GetProcAddress(hmod, "GetSecurityInfo");
160 myARGC = winetest_get_mainargs( &myARGV );
163 static void test_str_sid(const char *str_sid)
165 PSID psid;
166 char *temp;
168 if (pConvertStringSidToSidA(str_sid, &psid))
170 if (pConvertSidToStringSidA(psid, &temp))
172 trace(" %s: %s\n", str_sid, temp);
173 LocalFree(temp);
175 LocalFree(psid);
177 else
179 if (GetLastError() != ERROR_INVALID_SID)
180 trace(" %s: couldn't be converted, returned %d\n", str_sid, GetLastError());
181 else
182 trace(" %s: couldn't be converted\n", str_sid);
186 static void test_sid(void)
188 struct sidRef refs[] = {
189 { { {0x00,0x00,0x33,0x44,0x55,0x66} }, "S-1-860116326-1" },
190 { { {0x00,0x00,0x01,0x02,0x03,0x04} }, "S-1-16909060-1" },
191 { { {0x00,0x00,0x00,0x01,0x02,0x03} }, "S-1-66051-1" },
192 { { {0x00,0x00,0x00,0x00,0x01,0x02} }, "S-1-258-1" },
193 { { {0x00,0x00,0x00,0x00,0x00,0x02} }, "S-1-2-1" },
194 { { {0x00,0x00,0x00,0x00,0x00,0x0c} }, "S-1-12-1" },
196 const char noSubAuthStr[] = "S-1-5";
197 unsigned int i;
198 PSID psid = NULL;
199 SID *pisid;
200 BOOL r;
201 LPSTR str = NULL;
203 pConvertSidToStringSidA = (fnConvertSidToStringSidA)
204 GetProcAddress( hmod, "ConvertSidToStringSidA" );
205 if( !pConvertSidToStringSidA )
206 return;
207 pConvertStringSidToSidA = (fnConvertStringSidToSidA)
208 GetProcAddress( hmod, "ConvertStringSidToSidA" );
209 if( !pConvertStringSidToSidA )
210 return;
212 r = pConvertStringSidToSidA( NULL, NULL );
213 ok( !r, "expected failure with NULL parameters\n" );
214 if( GetLastError() == ERROR_CALL_NOT_IMPLEMENTED )
215 return;
216 ok( GetLastError() == ERROR_INVALID_PARAMETER,
217 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
218 GetLastError() );
220 r = pConvertStringSidToSidA( refs[0].refStr, NULL );
221 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
222 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
223 GetLastError() );
225 r = pConvertStringSidToSidA( NULL, &str );
226 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
227 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
228 GetLastError() );
230 r = pConvertStringSidToSidA( noSubAuthStr, &psid );
231 ok( !r,
232 "expected failure with no sub authorities\n" );
233 ok( GetLastError() == ERROR_INVALID_SID,
234 "expected GetLastError() is ERROR_INVALID_SID, got %d\n",
235 GetLastError() );
237 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid), "ConvertStringSidToSidA failed\n");
238 pisid = (SID *)psid;
239 ok(pisid->SubAuthorityCount == 4, "Invalid sub authority count - expected 4, got %d\n", pisid->SubAuthorityCount);
240 ok(pisid->SubAuthority[0] == 21, "Invalid subauthority 0 - expceted 21, got %d\n", pisid->SubAuthority[0]);
241 ok(pisid->SubAuthority[3] == 4576, "Invalid subauthority 0 - expceted 4576, got %d\n", pisid->SubAuthority[3]);
242 LocalFree(str);
244 for( i = 0; i < sizeof(refs) / sizeof(refs[0]); i++ )
246 PISID pisid;
248 r = AllocateAndInitializeSid( &refs[i].auth, 1,1,0,0,0,0,0,0,0,
249 &psid );
250 ok( r, "failed to allocate sid\n" );
251 r = pConvertSidToStringSidA( psid, &str );
252 ok( r, "failed to convert sid\n" );
253 if (r)
255 ok( !strcmp( str, refs[i].refStr ),
256 "incorrect sid, expected %s, got %s\n", refs[i].refStr, str );
257 LocalFree( str );
259 if( psid )
260 FreeSid( psid );
262 r = pConvertStringSidToSidA( refs[i].refStr, &psid );
263 ok( r, "failed to parse sid string\n" );
264 pisid = (PISID)psid;
265 ok( pisid &&
266 !memcmp( pisid->IdentifierAuthority.Value, refs[i].auth.Value,
267 sizeof(refs[i].auth) ),
268 "string sid %s didn't parse to expected value\n"
269 "(got 0x%04x%08x, expected 0x%04x%08x)\n",
270 refs[i].refStr,
271 MAKEWORD( pisid->IdentifierAuthority.Value[1],
272 pisid->IdentifierAuthority.Value[0] ),
273 MAKELONG( MAKEWORD( pisid->IdentifierAuthority.Value[5],
274 pisid->IdentifierAuthority.Value[4] ),
275 MAKEWORD( pisid->IdentifierAuthority.Value[3],
276 pisid->IdentifierAuthority.Value[2] ) ),
277 MAKEWORD( refs[i].auth.Value[1], refs[i].auth.Value[0] ),
278 MAKELONG( MAKEWORD( refs[i].auth.Value[5], refs[i].auth.Value[4] ),
279 MAKEWORD( refs[i].auth.Value[3], refs[i].auth.Value[2] ) ) );
280 if( psid )
281 LocalFree( psid );
284 trace("String SIDs:\n");
285 test_str_sid("AO");
286 test_str_sid("RU");
287 test_str_sid("AN");
288 test_str_sid("AU");
289 test_str_sid("BA");
290 test_str_sid("BG");
291 test_str_sid("BO");
292 test_str_sid("BU");
293 test_str_sid("CA");
294 test_str_sid("CG");
295 test_str_sid("CO");
296 test_str_sid("DA");
297 test_str_sid("DC");
298 test_str_sid("DD");
299 test_str_sid("DG");
300 test_str_sid("DU");
301 test_str_sid("EA");
302 test_str_sid("ED");
303 test_str_sid("WD");
304 test_str_sid("PA");
305 test_str_sid("IU");
306 test_str_sid("LA");
307 test_str_sid("LG");
308 test_str_sid("LS");
309 test_str_sid("SY");
310 test_str_sid("NU");
311 test_str_sid("NO");
312 test_str_sid("NS");
313 test_str_sid("PO");
314 test_str_sid("PS");
315 test_str_sid("PU");
316 test_str_sid("RS");
317 test_str_sid("RD");
318 test_str_sid("RE");
319 test_str_sid("RC");
320 test_str_sid("SA");
321 test_str_sid("SO");
322 test_str_sid("SU");
325 static void test_trustee(void)
327 GUID ObjectType = {0x12345678, 0x1234, 0x5678, {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}};
328 GUID InheritedObjectType = {0x23456789, 0x2345, 0x6786, {0x2, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}};
329 GUID ZeroGuid;
330 OBJECTS_AND_NAME_ oan;
331 OBJECTS_AND_SID oas;
332 TRUSTEE trustee;
333 PSID psid;
334 char szObjectTypeName[] = "ObjectTypeName";
335 char szInheritedObjectTypeName[] = "InheritedObjectTypeName";
336 char szTrusteeName[] = "szTrusteeName";
337 SID_IDENTIFIER_AUTHORITY auth = { {0x11,0x22,0,0,0, 0} };
339 memset( &ZeroGuid, 0x00, sizeof (ZeroGuid) );
341 pBuildTrusteeWithSidA = (fnBuildTrusteeWithSidA)
342 GetProcAddress( hmod, "BuildTrusteeWithSidA" );
343 pBuildTrusteeWithNameA = (fnBuildTrusteeWithNameA)
344 GetProcAddress( hmod, "BuildTrusteeWithNameA" );
345 pBuildTrusteeWithObjectsAndNameA = (fnBuildTrusteeWithObjectsAndNameA)
346 GetProcAddress (hmod, "BuildTrusteeWithObjectsAndNameA" );
347 pBuildTrusteeWithObjectsAndSidA = (fnBuildTrusteeWithObjectsAndSidA)
348 GetProcAddress (hmod, "BuildTrusteeWithObjectsAndSidA" );
349 pGetTrusteeNameA = (fnGetTrusteeNameA)
350 GetProcAddress (hmod, "GetTrusteeNameA" );
351 if( !pBuildTrusteeWithSidA || !pBuildTrusteeWithNameA ||
352 !pBuildTrusteeWithObjectsAndNameA || !pBuildTrusteeWithObjectsAndSidA ||
353 !pGetTrusteeNameA )
354 return;
356 if ( ! AllocateAndInitializeSid( &auth, 1, 42, 0,0,0,0,0,0,0,&psid ) )
358 trace( "failed to init SID\n" );
359 return;
362 /* test BuildTrusteeWithSidA */
363 memset( &trustee, 0xff, sizeof trustee );
364 pBuildTrusteeWithSidA( &trustee, psid );
366 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
367 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
368 "MultipleTrusteeOperation wrong\n");
369 ok( trustee.TrusteeForm == TRUSTEE_IS_SID, "TrusteeForm wrong\n");
370 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
371 ok( trustee.ptstrName == (LPSTR) psid, "ptstrName wrong\n" );
373 /* test BuildTrusteeWithObjectsAndSidA (test 1) */
374 memset( &trustee, 0xff, sizeof trustee );
375 memset( &oas, 0xff, sizeof(oas) );
376 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, &ObjectType,
377 &InheritedObjectType, psid);
379 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
380 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
381 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
382 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
383 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
385 ok(oas.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
386 ok(!memcmp(&oas.ObjectTypeGuid, &ObjectType, sizeof(GUID)), "ObjectTypeGuid wrong\n");
387 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
388 ok(oas.pSid == psid, "pSid wrong\n");
390 /* test GetTrusteeNameA */
391 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oas, "GetTrusteeName returned wrong value\n");
393 /* test BuildTrusteeWithObjectsAndSidA (test 2) */
394 memset( &trustee, 0xff, sizeof trustee );
395 memset( &oas, 0xff, sizeof(oas) );
396 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, NULL,
397 &InheritedObjectType, psid);
399 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
400 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
401 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
402 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
403 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
405 ok(oas.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
406 ok(!memcmp(&oas.ObjectTypeGuid, &ZeroGuid, sizeof(GUID)), "ObjectTypeGuid wrong\n");
407 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
408 ok(oas.pSid == psid, "pSid wrong\n");
410 FreeSid( psid );
412 /* test BuildTrusteeWithNameA */
413 memset( &trustee, 0xff, sizeof trustee );
414 pBuildTrusteeWithNameA( &trustee, szTrusteeName );
416 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
417 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
418 "MultipleTrusteeOperation wrong\n");
419 ok( trustee.TrusteeForm == TRUSTEE_IS_NAME, "TrusteeForm wrong\n");
420 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
421 ok( trustee.ptstrName == szTrusteeName, "ptstrName wrong\n" );
423 /* test BuildTrusteeWithObjectsAndNameA (test 1) */
424 memset( &trustee, 0xff, sizeof trustee );
425 memset( &oan, 0xff, sizeof(oan) );
426 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
427 szInheritedObjectTypeName, szTrusteeName);
429 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
430 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
431 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
432 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
433 ok(trustee.ptstrName == (LPTSTR)&oan, "ptstrName wrong\n");
435 ok(oan.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
436 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
437 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
438 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
440 /* test GetTrusteeNameA */
441 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oan, "GetTrusteeName returned wrong value\n");
443 /* test BuildTrusteeWithObjectsAndNameA (test 2) */
444 memset( &trustee, 0xff, sizeof trustee );
445 memset( &oan, 0xff, sizeof(oan) );
446 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, NULL,
447 szInheritedObjectTypeName, szTrusteeName);
449 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
450 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
451 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
452 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
453 ok(trustee.ptstrName == (LPSTR)&oan, "ptstrName wrong\n");
455 ok(oan.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
456 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
457 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
458 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
460 /* test BuildTrusteeWithObjectsAndNameA (test 3) */
461 memset( &trustee, 0xff, sizeof trustee );
462 memset( &oan, 0xff, sizeof(oan) );
463 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
464 NULL, szTrusteeName);
466 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
467 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
468 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
469 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
470 ok(trustee.ptstrName == (LPTSTR)&oan, "ptstrName wrong\n");
472 ok(oan.ObjectsPresent == ACE_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
473 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
474 ok(oan.InheritedObjectTypeName == NULL, "InheritedObjectTypeName wrong\n");
475 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
478 /* If the first isn't defined, assume none is */
479 #ifndef SE_MIN_WELL_KNOWN_PRIVILEGE
480 #define SE_MIN_WELL_KNOWN_PRIVILEGE 2L
481 #define SE_CREATE_TOKEN_PRIVILEGE 2L
482 #define SE_ASSIGNPRIMARYTOKEN_PRIVILEGE 3L
483 #define SE_LOCK_MEMORY_PRIVILEGE 4L
484 #define SE_INCREASE_QUOTA_PRIVILEGE 5L
485 #define SE_MACHINE_ACCOUNT_PRIVILEGE 6L
486 #define SE_TCB_PRIVILEGE 7L
487 #define SE_SECURITY_PRIVILEGE 8L
488 #define SE_TAKE_OWNERSHIP_PRIVILEGE 9L
489 #define SE_LOAD_DRIVER_PRIVILEGE 10L
490 #define SE_SYSTEM_PROFILE_PRIVILEGE 11L
491 #define SE_SYSTEMTIME_PRIVILEGE 12L
492 #define SE_PROF_SINGLE_PROCESS_PRIVILEGE 13L
493 #define SE_INC_BASE_PRIORITY_PRIVILEGE 14L
494 #define SE_CREATE_PAGEFILE_PRIVILEGE 15L
495 #define SE_CREATE_PERMANENT_PRIVILEGE 16L
496 #define SE_BACKUP_PRIVILEGE 17L
497 #define SE_RESTORE_PRIVILEGE 18L
498 #define SE_SHUTDOWN_PRIVILEGE 19L
499 #define SE_DEBUG_PRIVILEGE 20L
500 #define SE_AUDIT_PRIVILEGE 21L
501 #define SE_SYSTEM_ENVIRONMENT_PRIVILEGE 22L
502 #define SE_CHANGE_NOTIFY_PRIVILLEGE 23L
503 #define SE_REMOTE_SHUTDOWN_PRIVILEGE 24L
504 #define SE_UNDOCK_PRIVILEGE 25L
505 #define SE_SYNC_AGENT_PRIVILEGE 26L
506 #define SE_ENABLE_DELEGATION_PRIVILEGE 27L
507 #define SE_MANAGE_VOLUME_PRIVILEGE 28L
508 #define SE_IMPERSONATE_PRIVILEGE 29L
509 #define SE_CREATE_GLOBAL_PRIVILEGE 30L
510 #define SE_MAX_WELL_KNOWN_PRIVILEGE SE_CREATE_GLOBAL_PRIVILEGE
511 #endif /* ndef SE_MIN_WELL_KNOWN_PRIVILEGE */
513 static void test_allocateLuid(void)
515 BOOL (WINAPI *pAllocateLocallyUniqueId)(PLUID);
516 LUID luid1, luid2;
517 BOOL ret;
519 pAllocateLocallyUniqueId = (void*)GetProcAddress(hmod, "AllocateLocallyUniqueId");
520 if (!pAllocateLocallyUniqueId) return;
522 ret = pAllocateLocallyUniqueId(&luid1);
523 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
524 return;
526 ok(ret,
527 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
528 ret = pAllocateLocallyUniqueId(&luid2);
529 ok( ret,
530 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
531 ok(luid1.LowPart > SE_MAX_WELL_KNOWN_PRIVILEGE || luid1.HighPart != 0,
532 "AllocateLocallyUniqueId returned a well-known LUID\n");
533 ok(luid1.LowPart != luid2.LowPart || luid1.HighPart != luid2.HighPart,
534 "AllocateLocallyUniqueId returned non-unique LUIDs\n");
535 ret = pAllocateLocallyUniqueId(NULL);
536 ok( !ret && GetLastError() == ERROR_NOACCESS,
537 "AllocateLocallyUniqueId(NULL) didn't return ERROR_NOACCESS: %d\n",
538 GetLastError());
541 static void test_lookupPrivilegeName(void)
543 BOOL (WINAPI *pLookupPrivilegeNameA)(LPCSTR, PLUID, LPSTR, LPDWORD);
544 char buf[MAX_PATH]; /* arbitrary, seems long enough */
545 DWORD cchName = sizeof(buf);
546 LUID luid = { 0, 0 };
547 LONG i;
548 BOOL ret;
550 /* check whether it's available first */
551 pLookupPrivilegeNameA = (void*)GetProcAddress(hmod, "LookupPrivilegeNameA");
552 if (!pLookupPrivilegeNameA) return;
553 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
554 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
555 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
556 return;
558 /* check with a short buffer */
559 cchName = 0;
560 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
561 ret = pLookupPrivilegeNameA(NULL, &luid, NULL, &cchName);
562 ok( !ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
563 "LookupPrivilegeNameA didn't fail with ERROR_INSUFFICIENT_BUFFER: %d\n",
564 GetLastError());
565 ok(cchName == strlen("SeCreateTokenPrivilege") + 1,
566 "LookupPrivilegeNameA returned an incorrect required length for\n"
567 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName,
568 lstrlenA("SeCreateTokenPrivilege") + 1);
569 /* check a known value and its returned length on success */
570 cchName = sizeof(buf);
571 ok(pLookupPrivilegeNameA(NULL, &luid, buf, &cchName) &&
572 cchName == strlen("SeCreateTokenPrivilege"),
573 "LookupPrivilegeNameA returned an incorrect output length for\n"
574 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName,
575 (int)strlen("SeCreateTokenPrivilege"));
576 /* check known values */
577 for (i = SE_MIN_WELL_KNOWN_PRIVILEGE; i < SE_MAX_WELL_KNOWN_PRIVILEGE; i++)
579 luid.LowPart = i;
580 cchName = sizeof(buf);
581 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
582 ok( ret || GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
583 "LookupPrivilegeNameA(0.%d) failed: %d\n", i, GetLastError());
585 /* check a bogus LUID */
586 luid.LowPart = 0xdeadbeef;
587 cchName = sizeof(buf);
588 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
589 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
590 "LookupPrivilegeNameA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
591 GetLastError());
592 /* check on a bogus system */
593 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
594 cchName = sizeof(buf);
595 ret = pLookupPrivilegeNameA("b0gu5.Nam3", &luid, buf, &cchName);
596 ok( !ret && (GetLastError() == RPC_S_SERVER_UNAVAILABLE ||
597 GetLastError() == RPC_S_INVALID_NET_ADDR) /* w2k8 */,
598 "LookupPrivilegeNameA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
599 GetLastError());
602 struct NameToLUID
604 const char *name;
605 DWORD lowPart;
608 static void test_lookupPrivilegeValue(void)
610 static const struct NameToLUID privs[] = {
611 { "SeCreateTokenPrivilege", SE_CREATE_TOKEN_PRIVILEGE },
612 { "SeAssignPrimaryTokenPrivilege", SE_ASSIGNPRIMARYTOKEN_PRIVILEGE },
613 { "SeLockMemoryPrivilege", SE_LOCK_MEMORY_PRIVILEGE },
614 { "SeIncreaseQuotaPrivilege", SE_INCREASE_QUOTA_PRIVILEGE },
615 { "SeMachineAccountPrivilege", SE_MACHINE_ACCOUNT_PRIVILEGE },
616 { "SeTcbPrivilege", SE_TCB_PRIVILEGE },
617 { "SeSecurityPrivilege", SE_SECURITY_PRIVILEGE },
618 { "SeTakeOwnershipPrivilege", SE_TAKE_OWNERSHIP_PRIVILEGE },
619 { "SeLoadDriverPrivilege", SE_LOAD_DRIVER_PRIVILEGE },
620 { "SeSystemProfilePrivilege", SE_SYSTEM_PROFILE_PRIVILEGE },
621 { "SeSystemtimePrivilege", SE_SYSTEMTIME_PRIVILEGE },
622 { "SeProfileSingleProcessPrivilege", SE_PROF_SINGLE_PROCESS_PRIVILEGE },
623 { "SeIncreaseBasePriorityPrivilege", SE_INC_BASE_PRIORITY_PRIVILEGE },
624 { "SeCreatePagefilePrivilege", SE_CREATE_PAGEFILE_PRIVILEGE },
625 { "SeCreatePermanentPrivilege", SE_CREATE_PERMANENT_PRIVILEGE },
626 { "SeBackupPrivilege", SE_BACKUP_PRIVILEGE },
627 { "SeRestorePrivilege", SE_RESTORE_PRIVILEGE },
628 { "SeShutdownPrivilege", SE_SHUTDOWN_PRIVILEGE },
629 { "SeDebugPrivilege", SE_DEBUG_PRIVILEGE },
630 { "SeAuditPrivilege", SE_AUDIT_PRIVILEGE },
631 { "SeSystemEnvironmentPrivilege", SE_SYSTEM_ENVIRONMENT_PRIVILEGE },
632 { "SeChangeNotifyPrivilege", SE_CHANGE_NOTIFY_PRIVILLEGE },
633 { "SeRemoteShutdownPrivilege", SE_REMOTE_SHUTDOWN_PRIVILEGE },
634 { "SeUndockPrivilege", SE_UNDOCK_PRIVILEGE },
635 { "SeSyncAgentPrivilege", SE_SYNC_AGENT_PRIVILEGE },
636 { "SeEnableDelegationPrivilege", SE_ENABLE_DELEGATION_PRIVILEGE },
637 { "SeManageVolumePrivilege", SE_MANAGE_VOLUME_PRIVILEGE },
638 { "SeImpersonatePrivilege", SE_IMPERSONATE_PRIVILEGE },
639 { "SeCreateGlobalPrivilege", SE_CREATE_GLOBAL_PRIVILEGE },
641 BOOL (WINAPI *pLookupPrivilegeValueA)(LPCSTR, LPCSTR, PLUID);
642 int i;
643 LUID luid;
644 BOOL ret;
646 /* check whether it's available first */
647 pLookupPrivilegeValueA = (void*)GetProcAddress(hmod, "LookupPrivilegeValueA");
648 if (!pLookupPrivilegeValueA) return;
649 ret = pLookupPrivilegeValueA(NULL, "SeCreateTokenPrivilege", &luid);
650 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
651 return;
653 /* check a bogus system name */
654 ret = pLookupPrivilegeValueA("b0gu5.Nam3", "SeCreateTokenPrivilege", &luid);
655 ok( !ret && (GetLastError() == RPC_S_SERVER_UNAVAILABLE ||
656 GetLastError() == RPC_S_INVALID_NET_ADDR) /* w2k8 */,
657 "LookupPrivilegeValueA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
658 GetLastError());
659 /* check a NULL string */
660 ret = pLookupPrivilegeValueA(NULL, 0, &luid);
661 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
662 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
663 GetLastError());
664 /* check a bogus privilege name */
665 ret = pLookupPrivilegeValueA(NULL, "SeBogusPrivilege", &luid);
666 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
667 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
668 GetLastError());
669 /* check case insensitive */
670 ret = pLookupPrivilegeValueA(NULL, "sEcREATEtOKENpRIVILEGE", &luid);
671 ok( ret,
672 "LookupPrivilegeValueA(NULL, sEcREATEtOKENpRIVILEGE, &luid) failed: %d\n",
673 GetLastError());
674 for (i = 0; i < sizeof(privs) / sizeof(privs[0]); i++)
676 /* Not all privileges are implemented on all Windows versions, so
677 * don't worry if the call fails
679 if (pLookupPrivilegeValueA(NULL, privs[i].name, &luid))
681 ok(luid.LowPart == privs[i].lowPart,
682 "LookupPrivilegeValueA returned an invalid LUID for %s\n",
683 privs[i].name);
688 static void test_luid(void)
690 test_allocateLuid();
691 test_lookupPrivilegeName();
692 test_lookupPrivilegeValue();
695 static void test_FileSecurity(void)
697 char directory[MAX_PATH];
698 DWORD retval, outSize;
699 BOOL result;
700 BYTE buffer[0x40];
702 pGetFileSecurityA = (fnGetFileSecurityA)
703 GetProcAddress( hmod, "GetFileSecurityA" );
704 if( !pGetFileSecurityA )
705 return;
707 retval = GetTempPathA(sizeof(directory), directory);
708 if (!retval) {
709 trace("GetTempPathA failed\n");
710 return;
713 strcpy(directory, "\\Should not exist");
715 SetLastError(NO_ERROR);
716 result = pGetFileSecurityA( directory,OWNER_SECURITY_INFORMATION,buffer,0x40,&outSize);
717 ok(!result, "GetFileSecurityA should fail for not existing directories/files\n");
718 ok( (GetLastError() == ERROR_FILE_NOT_FOUND ) ||
719 (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) ,
720 "last error ERROR_FILE_NOT_FOUND / ERROR_CALL_NOT_IMPLEMENTED (98) "
721 "expected, got %d\n", GetLastError());
724 static void test_AccessCheck(void)
726 PSID EveryoneSid = NULL, AdminSid = NULL, UsersSid = NULL;
727 PACL Acl = NULL;
728 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL;
729 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
730 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
731 GENERIC_MAPPING Mapping = { KEY_READ, KEY_WRITE, KEY_EXECUTE, KEY_ALL_ACCESS };
732 ACCESS_MASK Access;
733 BOOL AccessStatus;
734 HANDLE Token;
735 HANDLE ProcessToken;
736 BOOL ret;
737 DWORD PrivSetLen;
738 PRIVILEGE_SET *PrivSet;
739 BOOL res;
740 HMODULE NtDllModule;
741 BOOLEAN Enabled;
742 DWORD err;
744 NtDllModule = GetModuleHandle("ntdll.dll");
745 if (!NtDllModule)
747 skip("not running on NT, skipping test\n");
748 return;
750 pRtlAdjustPrivilege = (fnRtlAdjustPrivilege)
751 GetProcAddress(NtDllModule, "RtlAdjustPrivilege");
752 if (!pRtlAdjustPrivilege)
754 skip("missing RtlAdjustPrivilege, skipping test\n");
755 return;
758 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
759 res = InitializeAcl(Acl, 256, ACL_REVISION);
760 if(!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
762 skip("ACLs not implemented - skipping tests\n");
763 HeapFree(GetProcessHeap(), 0, Acl);
764 return;
766 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
768 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
769 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
771 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
772 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdminSid);
773 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
775 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
776 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &UsersSid);
777 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
779 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
781 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
782 ok(res, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
784 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
785 ok(res, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
787 PrivSetLen = FIELD_OFFSET(PRIVILEGE_SET, Privilege[16]);
788 PrivSet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, PrivSetLen);
789 PrivSet->PrivilegeCount = 16;
791 res = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &ProcessToken);
792 ok(res, "OpenProcessToken failed with error %d\n", GetLastError());
794 pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, FALSE, TRUE, &Enabled);
796 res = DuplicateToken(ProcessToken, SecurityImpersonation, &Token);
797 ok(res, "DuplicateToken failed with error %d\n", GetLastError());
799 /* SD without owner/group */
800 SetLastError(0xdeadbeef);
801 Access = AccessStatus = 0xdeadbeef;
802 ret = AccessCheck(SecurityDescriptor, Token, KEY_QUERY_VALUE, &Mapping,
803 PrivSet, &PrivSetLen, &Access, &AccessStatus);
804 err = GetLastError();
805 ok(!ret && err == ERROR_INVALID_SECURITY_DESCR, "AccessCheck should have "
806 "failed with ERROR_INVALID_SECURITY_DESCR, instead of %d\n", err);
807 ok(Access == 0xdeadbeef && AccessStatus == 0xdeadbeef,
808 "Access and/or AccessStatus were changed!\n");
810 /* Set owner and group */
811 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
812 ok(res, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
813 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, TRUE);
814 ok(res, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
816 /* Generic access mask */
817 SetLastError(0xdeadbeef);
818 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
819 PrivSet, &PrivSetLen, &Access, &AccessStatus);
820 err = GetLastError();
821 ok(!ret && err == ERROR_GENERIC_NOT_MAPPED, "AccessCheck should have failed "
822 "with ERROR_GENERIC_NOT_MAPPED, instead of %d\n", err);
823 ok(Access == 0xdeadbeef && AccessStatus == 0xdeadbeef,
824 "Access and/or AccessStatus were changed!\n");
826 /* sd with no dacl present */
827 ret = SetSecurityDescriptorDacl(SecurityDescriptor, FALSE, NULL, FALSE);
828 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
829 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
830 PrivSet, &PrivSetLen, &Access, &AccessStatus);
831 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
832 ok(AccessStatus && (Access == KEY_READ),
833 "AccessCheck failed to grant access with error %d\n",
834 GetLastError());
836 /* sd with NULL dacl */
837 ret = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, NULL, FALSE);
838 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
839 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
840 PrivSet, &PrivSetLen, &Access, &AccessStatus);
841 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
842 ok(AccessStatus && (Access == KEY_READ),
843 "AccessCheck failed to grant access with error %d\n",
844 GetLastError());
846 /* sd with blank dacl */
847 ret = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
848 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
849 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
850 PrivSet, &PrivSetLen, &Access, &AccessStatus);
851 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
852 err = GetLastError();
853 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
854 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
855 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
857 res = AddAccessAllowedAce(Acl, ACL_REVISION, KEY_READ, EveryoneSid);
858 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
860 res = AddAccessDeniedAce(Acl, ACL_REVISION, KEY_SET_VALUE, AdminSid);
861 ok(res, "AddAccessDeniedAce failed with error %d\n", GetLastError());
863 /* sd with dacl */
864 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
865 PrivSet, &PrivSetLen, &Access, &AccessStatus);
866 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
867 ok(AccessStatus && (Access == KEY_READ),
868 "AccessCheck failed to grant access with error %d\n",
869 GetLastError());
871 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
872 PrivSet, &PrivSetLen, &Access, &AccessStatus);
873 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
874 ok(AccessStatus,
875 "AccessCheck failed to grant any access with error %d\n",
876 GetLastError());
877 trace("AccessCheck with MAXIMUM_ALLOWED got Access 0x%08x\n", Access);
879 /* Access denied by SD */
880 SetLastError(0xdeadbeef);
881 ret = AccessCheck(SecurityDescriptor, Token, KEY_WRITE, &Mapping,
882 PrivSet, &PrivSetLen, &Access, &AccessStatus);
883 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
884 err = GetLastError();
885 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
886 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
887 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
889 SetLastError(0);
890 PrivSet->PrivilegeCount = 16;
891 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
892 PrivSet, &PrivSetLen, &Access, &AccessStatus);
893 ok(ret && !AccessStatus && GetLastError() == ERROR_PRIVILEGE_NOT_HELD,
894 "AccessCheck should have failed with ERROR_PRIVILEGE_NOT_HELD, instead of %d\n",
895 GetLastError());
897 ret = ImpersonateLoggedOnUser(Token);
898 ok(ret, "ImpersonateLoggedOnUser failed with error %d\n", GetLastError());
899 ret = pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, TRUE, TRUE, &Enabled);
900 if (!ret)
902 SetLastError(0);
903 PrivSet->PrivilegeCount = 16;
904 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
905 PrivSet, &PrivSetLen, &Access, &AccessStatus);
906 ok(ret && AccessStatus && GetLastError() == 0,
907 "AccessCheck should have succeeded, error %d\n",
908 GetLastError());
909 ok(Access == ACCESS_SYSTEM_SECURITY,
910 "Access should be equal to ACCESS_SYSTEM_SECURITY instead of 0x%08x\n",
911 Access);
913 else
914 trace("Couldn't get SE_SECURITY_PRIVILEGE (0x%08x), skipping ACCESS_SYSTEM_SECURITY test\n",
915 ret);
916 ret = RevertToSelf();
917 ok(ret, "RevertToSelf failed with error %d\n", GetLastError());
919 /* test INHERIT_ONLY_ACE */
920 ret = InitializeAcl(Acl, 256, ACL_REVISION);
921 ok(ret, "InitializeAcl failed with error %d\n", GetLastError());
923 /* NT doesn't have AddAccessAllowedAceEx. Skipping this call/test doesn't influence
924 * the next ones.
926 if (pAddAccessAllowedAceEx)
928 ret = pAddAccessAllowedAceEx(Acl, ACL_REVISION, INHERIT_ONLY_ACE, KEY_READ, EveryoneSid);
929 ok(ret, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
931 else
932 skip("AddAccessAllowedAceEx is not available\n");
934 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
935 PrivSet, &PrivSetLen, &Access, &AccessStatus);
936 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
937 err = GetLastError();
938 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
939 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
940 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
942 CloseHandle(Token);
944 res = DuplicateToken(ProcessToken, SecurityAnonymous, &Token);
945 ok(res, "DuplicateToken failed with error %d\n", GetLastError());
947 SetLastError(0xdeadbeef);
948 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
949 PrivSet, &PrivSetLen, &Access, &AccessStatus);
950 err = GetLastError();
951 ok(!ret && err == ERROR_BAD_IMPERSONATION_LEVEL, "AccessCheck should have failed "
952 "with ERROR_BAD_IMPERSONATION_LEVEL, instead of %d\n", err);
954 CloseHandle(Token);
956 SetLastError(0xdeadbeef);
957 ret = AccessCheck(SecurityDescriptor, ProcessToken, KEY_READ, &Mapping,
958 PrivSet, &PrivSetLen, &Access, &AccessStatus);
959 err = GetLastError();
960 ok(!ret && err == ERROR_NO_IMPERSONATION_TOKEN, "AccessCheck should have failed "
961 "with ERROR_NO_IMPERSONATION_TOKEN, instead of %d\n", err);
963 CloseHandle(ProcessToken);
965 if (EveryoneSid)
966 FreeSid(EveryoneSid);
967 if (AdminSid)
968 FreeSid(AdminSid);
969 if (UsersSid)
970 FreeSid(UsersSid);
971 HeapFree(GetProcessHeap(), 0, Acl);
972 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
973 HeapFree(GetProcessHeap(), 0, PrivSet);
976 /* test GetTokenInformation for the various attributes */
977 static void test_token_attr(void)
979 HANDLE Token, ImpersonationToken;
980 DWORD Size;
981 TOKEN_PRIVILEGES *Privileges;
982 TOKEN_GROUPS *Groups;
983 TOKEN_USER *User;
984 BOOL ret;
985 DWORD i, GLE;
986 LPSTR SidString;
987 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel;
989 /* cygwin-like use case */
990 SetLastError(0xdeadbeef);
991 ret = OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED, &Token);
992 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
994 skip("OpenProcessToken is not implemented\n");
995 return;
997 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
998 if (ret)
1000 BYTE buf[1024];
1001 Size = sizeof(buf);
1002 ret = GetTokenInformation(Token, TokenUser,(void*)buf, Size, &Size);
1003 ok(ret, "GetTokenInformation failed with error %d\n", GetLastError());
1004 Size = sizeof(ImpersonationLevel);
1005 ret = GetTokenInformation(Token, TokenImpersonationLevel, &ImpersonationLevel, Size, &Size);
1006 GLE = GetLastError();
1007 ok(!ret && (GLE == ERROR_INVALID_PARAMETER), "GetTokenInformation(TokenImpersonationLevel) on primary token should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GLE);
1008 CloseHandle(Token);
1011 if(!pConvertSidToStringSidA)
1013 skip("ConvertSidToStringSidA is not available\n");
1014 return;
1017 SetLastError(0xdeadbeef);
1018 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &Token);
1019 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1021 /* groups */
1022 ret = GetTokenInformation(Token, TokenGroups, NULL, 0, &Size);
1023 Groups = HeapAlloc(GetProcessHeap(), 0, Size);
1024 ret = GetTokenInformation(Token, TokenGroups, Groups, Size, &Size);
1025 ok(ret, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
1026 trace("TokenGroups:\n");
1027 for (i = 0; i < Groups->GroupCount; i++)
1029 DWORD NameLength = 255;
1030 TCHAR Name[255];
1031 DWORD DomainLength = 255;
1032 TCHAR Domain[255];
1033 SID_NAME_USE SidNameUse;
1034 pConvertSidToStringSidA(Groups->Groups[i].Sid, &SidString);
1035 Name[0] = '\0';
1036 Domain[0] = '\0';
1037 ret = LookupAccountSid(NULL, Groups->Groups[i].Sid, Name, &NameLength, Domain, &DomainLength, &SidNameUse);
1038 if (ret)
1039 trace("\t%s, %s\\%s use: %d attr: 0x%08x\n", SidString, Domain, Name, SidNameUse, Groups->Groups[i].Attributes);
1040 else
1041 trace("\t%s, attr: 0x%08x LookupAccountSid failed with error %d\n", SidString, Groups->Groups[i].Attributes, GetLastError());
1042 LocalFree(SidString);
1044 HeapFree(GetProcessHeap(), 0, Groups);
1046 /* user */
1047 ret = GetTokenInformation(Token, TokenUser, NULL, 0, &Size);
1048 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1049 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1050 User = HeapAlloc(GetProcessHeap(), 0, Size);
1051 ret = GetTokenInformation(Token, TokenUser, User, Size, &Size);
1052 ok(ret,
1053 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1055 pConvertSidToStringSidA(User->User.Sid, &SidString);
1056 trace("TokenUser: %s attr: 0x%08x\n", SidString, User->User.Attributes);
1057 LocalFree(SidString);
1058 HeapFree(GetProcessHeap(), 0, User);
1060 /* privileges */
1061 ret = GetTokenInformation(Token, TokenPrivileges, NULL, 0, &Size);
1062 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1063 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1064 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
1065 ret = GetTokenInformation(Token, TokenPrivileges, Privileges, Size, &Size);
1066 ok(ret,
1067 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1068 trace("TokenPrivileges:\n");
1069 for (i = 0; i < Privileges->PrivilegeCount; i++)
1071 TCHAR Name[256];
1072 DWORD NameLen = sizeof(Name)/sizeof(Name[0]);
1073 LookupPrivilegeName(NULL, &Privileges->Privileges[i].Luid, Name, &NameLen);
1074 trace("\t%s, 0x%x\n", Name, Privileges->Privileges[i].Attributes);
1076 HeapFree(GetProcessHeap(), 0, Privileges);
1078 ret = DuplicateToken(Token, SecurityAnonymous, &ImpersonationToken);
1079 ok(ret, "DuplicateToken failed with error %d\n", GetLastError());
1081 Size = sizeof(ImpersonationLevel);
1082 ret = GetTokenInformation(ImpersonationToken, TokenImpersonationLevel, &ImpersonationLevel, Size, &Size);
1083 ok(ret, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1084 ok(ImpersonationLevel == SecurityAnonymous, "ImpersonationLevel should have been SecurityAnonymous instead of %d\n", ImpersonationLevel);
1086 CloseHandle(ImpersonationToken);
1087 CloseHandle(Token);
1090 typedef union _MAX_SID
1092 SID sid;
1093 char max[SECURITY_MAX_SID_SIZE];
1094 } MAX_SID;
1096 static void test_sid_str(PSID * sid)
1098 char *str_sid;
1099 BOOL ret = pConvertSidToStringSidA(sid, &str_sid);
1100 ok(ret, "ConvertSidToStringSidA() failed: %d\n", GetLastError());
1101 if (ret)
1103 char account[MAX_PATH], domain[MAX_PATH];
1104 SID_NAME_USE use;
1105 DWORD acc_size = MAX_PATH;
1106 DWORD dom_size = MAX_PATH;
1107 ret = LookupAccountSid(NULL, sid, account, &acc_size, domain, &dom_size, &use);
1108 ok(ret || (!ret && (GetLastError() == ERROR_NONE_MAPPED)),
1109 "LookupAccountSid(%s) failed: %d\n", str_sid, GetLastError());
1110 if (ret)
1111 trace(" %s %s\\%s %d\n", str_sid, domain, account, use);
1112 else if (GetLastError() == ERROR_NONE_MAPPED)
1113 trace(" %s couldn't be mapped\n", str_sid);
1114 LocalFree(str_sid);
1118 struct well_known_sid_value
1120 BOOL without_domain;
1121 const char *sid_string;
1122 } well_known_sid_values[] = {
1123 /* 0 */ {TRUE, "S-1-0-0"}, {TRUE, "S-1-1-0"}, {TRUE, "S-1-2-0"}, {TRUE, "S-1-3-0"},
1124 /* 4 */ {TRUE, "S-1-3-1"}, {TRUE, "S-1-3-2"}, {TRUE, "S-1-3-3"}, {TRUE, "S-1-5"},
1125 /* 8 */ {FALSE, "S-1-5-1"}, {TRUE, "S-1-5-2"}, {TRUE, "S-1-5-3"}, {TRUE, "S-1-5-4"},
1126 /* 12 */ {TRUE, "S-1-5-6"}, {TRUE, "S-1-5-7"}, {TRUE, "S-1-5-8"}, {TRUE, "S-1-5-9"},
1127 /* 16 */ {TRUE, "S-1-5-10"}, {TRUE, "S-1-5-11"}, {TRUE, "S-1-5-12"}, {TRUE, "S-1-5-13"},
1128 /* 20 */ {TRUE, "S-1-5-14"}, {FALSE, NULL}, {TRUE, "S-1-5-18"}, {TRUE, "S-1-5-19"},
1129 /* 24 */ {TRUE, "S-1-5-20"}, {TRUE, "S-1-5-32"},
1130 /* 26 */ {FALSE, "S-1-5-32-544"}, {TRUE, "S-1-5-32-545"}, {TRUE, "S-1-5-32-546"},
1131 /* 29 */ {TRUE, "S-1-5-32-547"}, {TRUE, "S-1-5-32-548"}, {TRUE, "S-1-5-32-549"},
1132 /* 32 */ {TRUE, "S-1-5-32-550"}, {TRUE, "S-1-5-32-551"}, {TRUE, "S-1-5-32-552"},
1133 /* 35 */ {TRUE, "S-1-5-32-554"}, {TRUE, "S-1-5-32-555"}, {TRUE, "S-1-5-32-556"},
1134 /* 38 */ {FALSE, "S-1-5-21-12-23-34-45-56-500"}, {FALSE, "S-1-5-21-12-23-34-45-56-501"},
1135 /* 40 */ {FALSE, "S-1-5-21-12-23-34-45-56-502"}, {FALSE, "S-1-5-21-12-23-34-45-56-512"},
1136 /* 42 */ {FALSE, "S-1-5-21-12-23-34-45-56-513"}, {FALSE, "S-1-5-21-12-23-34-45-56-514"},
1137 /* 44 */ {FALSE, "S-1-5-21-12-23-34-45-56-515"}, {FALSE, "S-1-5-21-12-23-34-45-56-516"},
1138 /* 46 */ {FALSE, "S-1-5-21-12-23-34-45-56-517"}, {FALSE, "S-1-5-21-12-23-34-45-56-518"},
1139 /* 48 */ {FALSE, "S-1-5-21-12-23-34-45-56-519"}, {FALSE, "S-1-5-21-12-23-34-45-56-520"},
1140 /* 50 */ {FALSE, "S-1-5-21-12-23-34-45-56-553"},
1141 /* Added in Windows Server 2003 */
1142 /* 51 */ {TRUE, "S-1-5-64-10"}, {TRUE, "S-1-5-64-21"}, {TRUE, "S-1-5-64-14"},
1143 /* 54 */ {TRUE, "S-1-5-15"}, {TRUE, "S-1-5-1000"}, {FALSE, "S-1-5-32-557"},
1144 /* 57 */ {TRUE, "S-1-5-32-558"}, {TRUE, "S-1-5-32-559"}, {TRUE, "S-1-5-32-560"},
1145 /* 60 */ {TRUE, "S-1-5-32-561"}, {TRUE, "S-1-5-32-562"},
1146 /* Added in Windows Vista: */
1147 /* 62 */ {TRUE, "S-1-5-32-568"},
1148 /* 63 */ {TRUE, "S-1-5-17"}, {FALSE, "S-1-5-32-569"}, {TRUE, "S-1-16-0"},
1149 /* 66 */ {TRUE, "S-1-16-4096"}, {TRUE, "S-1-16-8192"}, {TRUE, "S-1-16-12288"},
1150 /* 69 */ {TRUE, "S-1-16-16384"}, {TRUE, "S-1-5-33"}, {TRUE, "S-1-3-4"},
1151 /* 72 */ {FALSE, "S-1-5-21-12-23-34-45-56-571"}, {FALSE, "S-1-5-21-12-23-34-45-56-572"},
1152 /* 74 */ {TRUE, "S-1-5-22"}, {FALSE, "S-1-5-21-12-23-34-45-56-521"}, {TRUE, "S-1-5-32-573"}
1155 static void test_CreateWellKnownSid()
1157 SID_IDENTIFIER_AUTHORITY ident = { SECURITY_NT_AUTHORITY };
1158 PSID domainsid;
1159 int i;
1161 if (!pCreateWellKnownSid)
1163 skip("CreateWellKnownSid not available\n");
1164 return;
1167 /* a domain sid usually have three subauthorities but we test that CreateWellKnownSid doesn't check it */
1168 AllocateAndInitializeSid(&ident, 6, SECURITY_NT_NON_UNIQUE, 12, 23, 34, 45, 56, 0, 0, &domainsid);
1170 for (i = 0; i < sizeof(well_known_sid_values)/sizeof(well_known_sid_values[0]); i++)
1172 struct well_known_sid_value *value = &well_known_sid_values[i];
1173 char sid_buffer[SECURITY_MAX_SID_SIZE];
1174 LPSTR str;
1175 DWORD cb;
1177 if (value->sid_string == NULL)
1178 continue;
1180 if (i > WinAccountRasAndIasServersSid)
1182 /* These SIDs aren't implemented by all Windows versions - detect it and break the loop */
1183 cb = sizeof(sid_buffer);
1184 if (!pCreateWellKnownSid(i, domainsid, sid_buffer, &cb))
1186 skip("Well known SIDs starting from %d are not implemented\n", i);
1187 break;
1191 cb = sizeof(sid_buffer);
1192 ok(pCreateWellKnownSid(i, value->without_domain ? NULL : domainsid, sid_buffer, &cb), "Couldn't create well known sid %d\n", i);
1193 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer)), cb, DWORD, "%d");
1194 ok(IsValidSid(sid_buffer), "The sid is not valid\n");
1195 ok(pConvertSidToStringSidA(sid_buffer, &str), "Couldn't convert SID to string\n");
1196 ok(strcmp(str, value->sid_string) == 0, "SID mismatch - expected %s, got %s\n",
1197 value->sid_string, str);
1198 LocalFree(str);
1200 if (value->without_domain)
1202 char buf2[SECURITY_MAX_SID_SIZE];
1203 cb = sizeof(buf2);
1204 ok(pCreateWellKnownSid(i, domainsid, buf2, &cb), "Couldn't create well known sid %d with optional domain\n", i);
1205 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer)), cb, DWORD, "%d");
1206 ok(memcmp(buf2, sid_buffer, cb) == 0, "SID create with domain is different than without (%d)\n", i);
1211 static void test_LookupAccountSid(void)
1213 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
1214 CHAR accountA[MAX_PATH], domainA[MAX_PATH];
1215 DWORD acc_sizeA, dom_sizeA;
1216 DWORD real_acc_sizeA, real_dom_sizeA;
1217 WCHAR accountW[MAX_PATH], domainW[MAX_PATH];
1218 DWORD acc_sizeW, dom_sizeW;
1219 DWORD real_acc_sizeW, real_dom_sizeW;
1220 PSID pUsersSid = NULL;
1221 SID_NAME_USE use;
1222 BOOL ret;
1223 DWORD size;
1224 MAX_SID max_sid;
1225 CHAR *str_sidA;
1226 int i;
1228 /* native windows crashes if account size, domain size, or name use is NULL */
1230 ret = AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
1231 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &pUsersSid);
1232 ok(ret || (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED),
1233 "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1235 /* not running on NT so give up */
1236 if (!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1237 return;
1239 real_acc_sizeA = MAX_PATH;
1240 real_dom_sizeA = MAX_PATH;
1241 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &real_acc_sizeA, domainA, &real_dom_sizeA, &use);
1242 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1244 /* try NULL account */
1245 acc_sizeA = MAX_PATH;
1246 dom_sizeA = MAX_PATH;
1247 ret = LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
1248 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1250 /* try NULL domain */
1251 acc_sizeA = MAX_PATH;
1252 dom_sizeA = MAX_PATH;
1253 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
1254 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1256 /* try a small account buffer */
1257 acc_sizeA = 1;
1258 dom_sizeA = MAX_PATH;
1259 accountA[0] = 0;
1260 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1261 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1262 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1263 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1265 /* try a 0 sized account buffer */
1266 acc_sizeA = 0;
1267 dom_sizeA = MAX_PATH;
1268 accountA[0] = 0;
1269 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1270 /* this can fail or succeed depending on OS version but the size will always be returned */
1271 ok(acc_sizeA == real_acc_sizeA + 1,
1272 "LookupAccountSidA() Expected acc_size = %u, got %u\n",
1273 real_acc_sizeA + 1, acc_sizeA);
1275 /* try a 0 sized account buffer */
1276 acc_sizeA = 0;
1277 dom_sizeA = MAX_PATH;
1278 ret = LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
1279 /* this can fail or succeed depending on OS version but the size will always be returned */
1280 ok(acc_sizeA == real_acc_sizeA + 1,
1281 "LookupAccountSid() Expected acc_size = %u, got %u\n",
1282 real_acc_sizeA + 1, acc_sizeA);
1284 /* try a small domain buffer */
1285 dom_sizeA = 1;
1286 acc_sizeA = MAX_PATH;
1287 accountA[0] = 0;
1288 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1289 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1290 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1291 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1293 /* try a 0 sized domain buffer */
1294 dom_sizeA = 0;
1295 acc_sizeA = MAX_PATH;
1296 accountA[0] = 0;
1297 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1298 /* this can fail or succeed depending on OS version but the size will always be returned */
1299 ok(dom_sizeA == real_dom_sizeA + 1,
1300 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1301 real_dom_sizeA + 1, dom_sizeA);
1303 /* try a 0 sized domain buffer */
1304 dom_sizeA = 0;
1305 acc_sizeA = MAX_PATH;
1306 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
1307 /* this can fail or succeed depending on OS version but the size will always be returned */
1308 ok(dom_sizeA == real_dom_sizeA + 1,
1309 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1310 real_dom_sizeA + 1, dom_sizeA);
1312 real_acc_sizeW = MAX_PATH;
1313 real_dom_sizeW = MAX_PATH;
1314 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &real_acc_sizeW, domainW, &real_dom_sizeW, &use);
1315 ok(ret, "LookupAccountSidW() Expected TRUE, got FALSE\n");
1317 /* try an invalid system name */
1318 real_acc_sizeA = MAX_PATH;
1319 real_dom_sizeA = MAX_PATH;
1320 ret = LookupAccountSidA("deepthought", pUsersSid, accountA, &real_acc_sizeA, domainA, &real_dom_sizeA, &use);
1321 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1322 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE,
1323 "LookupAccountSidA() Expected RPC_S_SERVER_UNAVAILABLE, got %u\n", GetLastError());
1325 /* native windows crashes if domainW or accountW is NULL */
1327 /* try a small account buffer */
1328 acc_sizeW = 1;
1329 dom_sizeW = MAX_PATH;
1330 accountW[0] = 0;
1331 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1332 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1333 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1334 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1336 /* try a 0 sized account buffer */
1337 acc_sizeW = 0;
1338 dom_sizeW = MAX_PATH;
1339 accountW[0] = 0;
1340 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1341 /* this can fail or succeed depending on OS version but the size will always be returned */
1342 ok(acc_sizeW == real_acc_sizeW + 1,
1343 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1344 real_acc_sizeW + 1, acc_sizeW);
1346 /* try a 0 sized account buffer */
1347 acc_sizeW = 0;
1348 dom_sizeW = MAX_PATH;
1349 ret = LookupAccountSidW(NULL, pUsersSid, NULL, &acc_sizeW, domainW, &dom_sizeW, &use);
1350 /* this can fail or succeed depending on OS version but the size will always be returned */
1351 ok(acc_sizeW == real_acc_sizeW + 1,
1352 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1353 real_acc_sizeW + 1, acc_sizeW);
1355 /* try a small domain buffer */
1356 dom_sizeW = 1;
1357 acc_sizeW = MAX_PATH;
1358 accountW[0] = 0;
1359 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1360 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1361 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1362 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1364 /* try a 0 sized domain buffer */
1365 dom_sizeW = 0;
1366 acc_sizeW = MAX_PATH;
1367 accountW[0] = 0;
1368 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1369 /* this can fail or succeed depending on OS version but the size will always be returned */
1370 ok(dom_sizeW == real_dom_sizeW + 1,
1371 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1372 real_dom_sizeW + 1, dom_sizeW);
1374 /* try a 0 sized domain buffer */
1375 dom_sizeW = 0;
1376 acc_sizeW = MAX_PATH;
1377 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, NULL, &dom_sizeW, &use);
1378 /* this can fail or succeed depending on OS version but the size will always be returned */
1379 ok(dom_sizeW == real_dom_sizeW + 1,
1380 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1381 real_dom_sizeW + 1, dom_sizeW);
1383 FreeSid(pUsersSid);
1385 if (pCreateWellKnownSid && pConvertSidToStringSidA)
1387 trace("Well Known SIDs:\n");
1388 for (i = 0; i <= 60; i++)
1390 size = SECURITY_MAX_SID_SIZE;
1391 if (pCreateWellKnownSid(i, NULL, &max_sid.sid, &size))
1393 if (pConvertSidToStringSidA(&max_sid.sid, &str_sidA))
1395 acc_sizeA = MAX_PATH;
1396 dom_sizeA = MAX_PATH;
1397 if (LookupAccountSidA(NULL, &max_sid.sid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use))
1398 trace(" %d: %s %s\\%s %d\n", i, str_sidA, domainA, accountA, use);
1399 LocalFree(str_sidA);
1402 else
1404 if (GetLastError() != ERROR_INVALID_PARAMETER)
1405 trace(" CreateWellKnownSid(%d) failed: %d\n", i, GetLastError());
1406 else
1407 trace(" %d: not supported\n", i);
1411 pLsaQueryInformationPolicy = (fnLsaQueryInformationPolicy)GetProcAddress( hmod, "LsaQueryInformationPolicy");
1412 pLsaOpenPolicy = (fnLsaOpenPolicy)GetProcAddress( hmod, "LsaOpenPolicy");
1413 pLsaFreeMemory = (fnLsaFreeMemory)GetProcAddress( hmod, "LsaFreeMemory");
1414 pLsaClose = (fnLsaClose)GetProcAddress( hmod, "LsaClose");
1416 if (pLsaQueryInformationPolicy && pLsaOpenPolicy && pLsaFreeMemory && pLsaClose)
1418 NTSTATUS status;
1419 LSA_HANDLE handle;
1420 LSA_OBJECT_ATTRIBUTES object_attributes;
1422 ZeroMemory(&object_attributes, sizeof(object_attributes));
1423 object_attributes.Length = sizeof(object_attributes);
1425 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_ALL_ACCESS, &handle);
1426 ok(status == STATUS_SUCCESS || status == STATUS_ACCESS_DENIED,
1427 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status);
1429 /* try a more restricted access mask if necessary */
1430 if (status == STATUS_ACCESS_DENIED) {
1431 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
1432 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_VIEW_LOCAL_INFORMATION, &handle);
1433 ok(status == STATUS_SUCCESS, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status);
1436 if (status == STATUS_SUCCESS)
1438 PPOLICY_ACCOUNT_DOMAIN_INFO info;
1439 status = pLsaQueryInformationPolicy(handle, PolicyAccountDomainInformation, (PVOID*)&info);
1440 ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy() failed, returned 0x%08x\n", status);
1441 if (status == STATUS_SUCCESS)
1443 ok(info->DomainSid!=0, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) missing SID\n");
1444 if (info->DomainSid)
1446 int count = *GetSidSubAuthorityCount(info->DomainSid);
1447 CopySid(GetSidLengthRequired(count), &max_sid, info->DomainSid);
1448 test_sid_str((PSID)&max_sid.sid);
1449 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_ADMIN;
1450 max_sid.sid.SubAuthorityCount = count + 1;
1451 test_sid_str((PSID)&max_sid.sid);
1452 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_GUEST;
1453 test_sid_str((PSID)&max_sid.sid);
1454 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ADMINS;
1455 test_sid_str((PSID)&max_sid.sid);
1456 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_USERS;
1457 test_sid_str((PSID)&max_sid.sid);
1458 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_GUESTS;
1459 test_sid_str((PSID)&max_sid.sid);
1460 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_COMPUTERS;
1461 test_sid_str((PSID)&max_sid.sid);
1462 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CONTROLLERS;
1463 test_sid_str((PSID)&max_sid.sid);
1464 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CERT_ADMINS;
1465 test_sid_str((PSID)&max_sid.sid);
1466 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_SCHEMA_ADMINS;
1467 test_sid_str((PSID)&max_sid.sid);
1468 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ENTERPRISE_ADMINS;
1469 test_sid_str((PSID)&max_sid.sid);
1470 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_POLICY_ADMINS;
1471 test_sid_str((PSID)&max_sid.sid);
1472 max_sid.sid.SubAuthority[count] = DOMAIN_ALIAS_RID_RAS_SERVERS;
1473 test_sid_str((PSID)&max_sid.sid);
1474 max_sid.sid.SubAuthority[count] = 1000; /* first user account */
1475 test_sid_str((PSID)&max_sid.sid);
1478 pLsaFreeMemory((LPVOID)info);
1481 status = pLsaClose(handle);
1482 ok(status == STATUS_SUCCESS, "LsaClose() failed, returned 0x%08x\n", status);
1488 static void get_sid_info(PSID psid, LPSTR *user, LPSTR *dom)
1490 static CHAR account[UNLEN + 1];
1491 static CHAR domain[UNLEN + 1];
1492 DWORD size, dom_size;
1493 SID_NAME_USE use;
1495 *user = account;
1496 *dom = domain;
1498 size = dom_size = UNLEN + 1;
1499 account[0] = '\0';
1500 domain[0] = '\0';
1501 LookupAccountSidA(NULL, psid, account, &size, domain, &dom_size, &use);
1504 static void test_LookupAccountName(void)
1506 DWORD sid_size, domain_size, user_size;
1507 DWORD sid_save, domain_save;
1508 CHAR user_name[UNLEN + 1];
1509 SID_NAME_USE sid_use;
1510 LPSTR domain, account, sid_dom;
1511 PSID psid;
1512 BOOL ret;
1514 /* native crashes if (assuming all other parameters correct):
1515 * - peUse is NULL
1516 * - Sid is NULL and cbSid is > 0
1517 * - cbSid or cchReferencedDomainName are NULL
1518 * - ReferencedDomainName is NULL and cchReferencedDomainName is the correct size
1521 user_size = UNLEN + 1;
1522 SetLastError(0xdeadbeef);
1523 ret = GetUserNameA(user_name, &user_size);
1524 if (!ret && (GetLastError() == ERROR_NOT_LOGGED_ON))
1526 /* Probably on win9x where the user used 'Cancel' instead of properly logging in */
1527 skip("Cannot get the user name (win9x and not logged in properly)\n");
1528 return;
1530 ok(ret, "Failed to get user name : %d\n", GetLastError());
1532 /* get sizes */
1533 sid_size = 0;
1534 domain_size = 0;
1535 sid_use = 0xcafebabe;
1536 SetLastError(0xdeadbeef);
1537 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, NULL, &domain_size, &sid_use);
1538 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1540 skip("LookupAccountNameA is not implemented\n");
1541 return;
1543 ok(!ret, "Expected 0, got %d\n", ret);
1544 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1545 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1546 ok(sid_size != 0, "Expected non-zero sid size\n");
1547 ok(domain_size != 0, "Expected non-zero domain size\n");
1548 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
1550 sid_save = sid_size;
1551 domain_save = domain_size;
1553 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
1554 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
1556 /* try valid account name */
1557 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, domain, &domain_size, &sid_use);
1558 get_sid_info(psid, &account, &sid_dom);
1559 ok(ret, "Failed to lookup account name\n");
1560 ok(sid_size == GetLengthSid(psid), "Expected %d, got %d\n", GetLengthSid(psid), sid_size);
1561 todo_wine
1563 ok(!lstrcmp(account, user_name), "Expected %s, got %s\n", user_name, account);
1564 ok(!lstrcmp(domain, sid_dom), "Expected %s, got %s\n", sid_dom, domain);
1566 ok(domain_size == domain_save - 1, "Expected %d, got %d\n", domain_save - 1, domain_size);
1567 ok(lstrlen(domain) == domain_size, "Expected %d, got %d\n", lstrlen(domain), domain_size);
1568 ok(sid_use == SidTypeUser, "Expected SidTypeUser (%d), got %d\n", SidTypeUser, sid_use);
1569 domain_size = domain_save;
1570 sid_size = sid_save;
1572 if (PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale())) != LANG_ENGLISH)
1574 skip("Non-english locale (test with hardcoded 'Everyone')\n");
1576 else
1578 ret = LookupAccountNameA(NULL, "Everyone", psid, &sid_size, domain, &domain_size, &sid_use);
1579 get_sid_info(psid, &account, &sid_dom);
1580 ok(ret, "Failed to lookup account name\n");
1581 ok(sid_size != 0, "sid_size was zero\n");
1582 ok(!lstrcmp(account, "Everyone"), "Expected Everyone, got %s\n", account);
1583 ok(!lstrcmp(domain, sid_dom), "Expected %s, got %s\n", sid_dom, domain);
1584 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
1585 ok(lstrlen(domain) == domain_size, "Expected %d, got %d\n", lstrlen(domain), domain_size);
1586 ok(sid_use == SidTypeWellKnownGroup, "Expected SidTypeWellKnownGroup (%d), got %d\n", SidTypeWellKnownGroup, sid_use);
1587 domain_size = domain_save;
1590 /* NULL Sid with zero sid size */
1591 SetLastError(0xdeadbeef);
1592 sid_size = 0;
1593 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
1594 ok(!ret, "Expected 0, got %d\n", ret);
1595 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1596 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1597 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1598 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1600 /* try cchReferencedDomainName - 1 */
1601 SetLastError(0xdeadbeef);
1602 domain_size--;
1603 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
1604 ok(!ret, "Expected 0, got %d\n", ret);
1605 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1606 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1607 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1608 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1610 /* NULL ReferencedDomainName with zero domain name size */
1611 SetLastError(0xdeadbeef);
1612 domain_size = 0;
1613 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, NULL, &domain_size, &sid_use);
1614 ok(!ret, "Expected 0, got %d\n", ret);
1615 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1616 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1617 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1618 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1620 HeapFree(GetProcessHeap(), 0, psid);
1621 HeapFree(GetProcessHeap(), 0, domain);
1623 /* get sizes for NULL account name */
1624 sid_size = 0;
1625 domain_size = 0;
1626 sid_use = 0xcafebabe;
1627 SetLastError(0xdeadbeef);
1628 ret = LookupAccountNameA(NULL, NULL, NULL, &sid_size, NULL, &domain_size, &sid_use);
1629 ok(!ret, "Expected 0, got %d\n", ret);
1630 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1631 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1632 ok(sid_size != 0, "Expected non-zero sid size\n");
1633 ok(domain_size != 0, "Expected non-zero domain size\n");
1634 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
1636 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
1637 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
1639 /* try NULL account name */
1640 ret = LookupAccountNameA(NULL, NULL, psid, &sid_size, domain, &domain_size, &sid_use);
1641 get_sid_info(psid, &account, &sid_dom);
1642 ok(ret, "Failed to lookup account name\n");
1643 /* Using a fixed string will not work on different locales */
1644 ok(!lstrcmp(account, domain),
1645 "Got %s for account and %s for domain, these should be the same\n",
1646 account, domain);
1647 ok(sid_use == SidTypeDomain, "Expected SidTypeDomain (%d), got %d\n", SidTypeDomain, sid_use);
1649 /* try an invalid account name */
1650 SetLastError(0xdeadbeef);
1651 sid_size = 0;
1652 domain_size = 0;
1653 ret = LookupAccountNameA(NULL, "oogabooga", NULL, &sid_size, NULL, &domain_size, &sid_use);
1654 ok(!ret, "Expected 0, got %d\n", ret);
1655 ok(GetLastError() == ERROR_NONE_MAPPED ||
1656 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE),
1657 "Expected ERROR_NONE_MAPPED, got %d\n", GetLastError());
1658 ok(sid_size == 0, "Expected 0, got %d\n", sid_size);
1659 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
1661 /* try an invalid system name */
1662 SetLastError(0xdeadbeef);
1663 sid_size = 0;
1664 domain_size = 0;
1665 ret = LookupAccountNameA("deepthought", NULL, NULL, &sid_size, NULL, &domain_size, &sid_use);
1666 ok(!ret, "Expected 0, got %d\n", ret);
1667 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE,
1668 "Expected RPC_S_SERVER_UNAVAILABLE, got %d\n", GetLastError());
1669 ok(sid_size == 0, "Expected 0, got %d\n", sid_size);
1670 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
1672 HeapFree(GetProcessHeap(), 0, psid);
1673 HeapFree(GetProcessHeap(), 0, domain);
1676 static void test_security_descriptor(void)
1678 SECURITY_DESCRIPTOR sd;
1679 char buf[8192];
1680 DWORD size;
1681 BOOL isDefault, isPresent, ret;
1682 PACL pacl;
1683 PSID psid;
1685 SetLastError(0xdeadbeef);
1686 ret = InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
1687 if (ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1689 skip("InitializeSecurityDescriptor is not implemented\n");
1690 return;
1693 ok(GetSecurityDescriptorOwner(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
1694 expect_eq(psid, NULL, PSID, "%p");
1695 expect_eq(isDefault, FALSE, BOOL, "%d");
1696 sd.Control |= SE_DACL_PRESENT | SE_SACL_PRESENT;
1698 SetLastError(0xdeadbeef);
1699 size = 5;
1700 expect_eq(MakeSelfRelativeSD(&sd, buf, &size), FALSE, BOOL, "%d");
1701 expect_eq(GetLastError(), ERROR_INSUFFICIENT_BUFFER, DWORD, "%u");
1702 ok(size > 5, "Size not increased\n");
1703 if (size <= 8192)
1705 expect_eq(MakeSelfRelativeSD(&sd, buf, &size), TRUE, BOOL, "%d");
1706 ok(GetSecurityDescriptorOwner(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
1707 expect_eq(psid, NULL, PSID, "%p");
1708 expect_eq(isDefault, FALSE, BOOL, "%d");
1709 ok(GetSecurityDescriptorGroup(&sd, &psid, &isDefault), "GetSecurityDescriptorGroup failed\n");
1710 expect_eq(psid, NULL, PSID, "%p");
1711 expect_eq(isDefault, FALSE, BOOL, "%d");
1712 ok(GetSecurityDescriptorDacl(&sd, &isPresent, &pacl, &isDefault), "GetSecurityDescriptorDacl failed\n");
1713 expect_eq(isPresent, TRUE, BOOL, "%d");
1714 expect_eq(psid, NULL, PSID, "%p");
1715 expect_eq(isDefault, FALSE, BOOL, "%d");
1716 ok(GetSecurityDescriptorSacl(&sd, &isPresent, &pacl, &isDefault), "GetSecurityDescriptorSacl failed\n");
1717 expect_eq(isPresent, TRUE, BOOL, "%d");
1718 expect_eq(psid, NULL, PSID, "%p");
1719 expect_eq(isDefault, FALSE, BOOL, "%d");
1723 #define TEST_GRANTED_ACCESS(a,b) test_granted_access(a,b,0,__LINE__)
1724 #define TEST_GRANTED_ACCESS2(a,b,c) test_granted_access(a,b,c,__LINE__)
1725 static void test_granted_access(HANDLE handle, ACCESS_MASK access,
1726 ACCESS_MASK alt, int line)
1728 OBJECT_BASIC_INFORMATION obj_info;
1729 NTSTATUS status;
1731 if (!pNtQueryObject)
1733 skip_(__FILE__, line)("Not NT platform - skipping tests\n");
1734 return;
1737 status = pNtQueryObject( handle, ObjectBasicInformation, &obj_info,
1738 sizeof(obj_info), NULL );
1739 ok_(__FILE__, line)(!status, "NtQueryObject with err: %08x\n", status);
1740 if (alt)
1741 ok_(__FILE__, line)(obj_info.GrantedAccess == access ||
1742 obj_info.GrantedAccess == alt, "Granted access should be 0x%08x "
1743 "or 0x%08x, instead of 0x%08x\n", access, alt, obj_info.GrantedAccess);
1744 else
1745 ok_(__FILE__, line)(obj_info.GrantedAccess == access, "Granted access should "
1746 "be 0x%08x, instead of 0x%08x\n", access, obj_info.GrantedAccess);
1749 #define CHECK_SET_SECURITY(o,i,e) \
1750 do{ \
1751 BOOL res; \
1752 DWORD err; \
1753 SetLastError( 0xdeadbeef ); \
1754 res = SetKernelObjectSecurity( o, i, SecurityDescriptor ); \
1755 err = GetLastError(); \
1756 if (e == ERROR_SUCCESS) \
1757 ok(res, "SetKernelObjectSecurity failed with %d\n", err); \
1758 else \
1759 ok(!res && err == e, "SetKernelObjectSecurity should have failed " \
1760 "with %s, instead of %d\n", #e, err); \
1761 }while(0)
1763 static void test_process_security(void)
1765 BOOL res;
1766 char owner[32], group[32];
1767 PSID AdminSid = NULL, UsersSid = NULL;
1768 PACL Acl = NULL;
1769 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL;
1770 char buffer[MAX_PATH];
1771 PROCESS_INFORMATION info;
1772 STARTUPINFOA startup;
1773 SECURITY_ATTRIBUTES psa;
1774 HANDLE token, event;
1775 DWORD tmp;
1777 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
1778 res = InitializeAcl(Acl, 256, ACL_REVISION);
1779 if (!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1781 skip("ACLs not implemented - skipping tests\n");
1782 HeapFree(GetProcessHeap(), 0, Acl);
1783 return;
1785 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
1787 /* get owner from the token we might be running as a user not admin */
1788 res = OpenProcessToken( GetCurrentProcess(), MAXIMUM_ALLOWED, &token );
1789 ok(res, "OpenProcessToken failed with error %d\n", GetLastError());
1790 if (!res)
1792 HeapFree(GetProcessHeap(), 0, Acl);
1793 return;
1796 res = GetTokenInformation( token, TokenOwner, owner, sizeof(owner), &tmp );
1797 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
1798 AdminSid = ((TOKEN_OWNER*)owner)->Owner;
1799 res = GetTokenInformation( token, TokenPrimaryGroup, group, sizeof(group), &tmp );
1800 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
1801 UsersSid = ((TOKEN_PRIMARY_GROUP*)group)->PrimaryGroup;
1803 CloseHandle( token );
1804 if (!res)
1806 HeapFree(GetProcessHeap(), 0, Acl);
1807 return;
1810 res = AddAccessDeniedAce(Acl, ACL_REVISION, PROCESS_VM_READ, AdminSid);
1811 ok(res, "AddAccessDeniedAce failed with error %d\n", GetLastError());
1812 res = AddAccessAllowedAce(Acl, ACL_REVISION, PROCESS_ALL_ACCESS, AdminSid);
1813 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
1815 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
1816 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
1817 ok(res, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
1819 event = CreateEvent( NULL, TRUE, TRUE, "test_event" );
1820 ok(event != NULL, "CreateEvent %d\n", GetLastError());
1822 SecurityDescriptor->Revision = 0;
1823 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_UNKNOWN_REVISION );
1824 SecurityDescriptor->Revision = SECURITY_DESCRIPTOR_REVISION;
1826 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_INVALID_SECURITY_DESCR );
1827 CHECK_SET_SECURITY( event, GROUP_SECURITY_INFORMATION, ERROR_INVALID_SECURITY_DESCR );
1828 CHECK_SET_SECURITY( event, SACL_SECURITY_INFORMATION, ERROR_ACCESS_DENIED );
1829 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
1830 /* NULL DACL is valid and means default DACL from token */
1831 SecurityDescriptor->Control |= SE_DACL_PRESENT;
1832 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
1834 /* Set owner and group and dacl */
1835 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
1836 ok(res, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
1837 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_SUCCESS );
1838 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, FALSE);
1839 ok(res, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
1840 CHECK_SET_SECURITY( event, GROUP_SECURITY_INFORMATION, ERROR_SUCCESS );
1841 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
1842 ok(res, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1843 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
1845 sprintf(buffer, "%s tests/security.c test", myARGV[0]);
1846 memset(&startup, 0, sizeof(startup));
1847 startup.cb = sizeof(startup);
1848 startup.dwFlags = STARTF_USESHOWWINDOW;
1849 startup.wShowWindow = SW_SHOWNORMAL;
1851 psa.nLength = sizeof(psa);
1852 psa.lpSecurityDescriptor = SecurityDescriptor;
1853 psa.bInheritHandle = TRUE;
1855 /* Doesn't matter what ACL say we should get full access for ourselves */
1856 ok(CreateProcessA( NULL, buffer, &psa, NULL, FALSE, 0, NULL, NULL, &startup, &info ),
1857 "CreateProcess with err:%d\n", GetLastError());
1858 TEST_GRANTED_ACCESS2( info.hProcess, PROCESS_ALL_ACCESS,
1859 STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL );
1860 winetest_wait_child_process( info.hProcess );
1862 CloseHandle( info.hProcess );
1863 CloseHandle( info.hThread );
1864 CloseHandle( event );
1865 HeapFree(GetProcessHeap(), 0, Acl);
1866 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
1869 static void test_process_security_child(void)
1871 HANDLE handle, handle1;
1872 BOOL ret;
1873 DWORD err;
1875 handle = OpenProcess( PROCESS_TERMINATE, FALSE, GetCurrentProcessId() );
1876 ok(handle != NULL, "OpenProcess(PROCESS_TERMINATE) with err:%d\n", GetLastError());
1877 TEST_GRANTED_ACCESS( handle, PROCESS_TERMINATE );
1879 ok(DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
1880 &handle1, 0, TRUE, DUPLICATE_SAME_ACCESS ),
1881 "duplicating handle err:%d\n", GetLastError());
1882 TEST_GRANTED_ACCESS( handle1, PROCESS_TERMINATE );
1884 CloseHandle( handle1 );
1886 SetLastError( 0xdeadbeef );
1887 ret = DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
1888 &handle1, PROCESS_ALL_ACCESS, TRUE, 0 );
1889 err = GetLastError();
1890 todo_wine
1891 ok(!ret && err == ERROR_ACCESS_DENIED, "duplicating handle should have failed "
1892 "with STATUS_ACCESS_DENIED, instead of err:%d\n", err);
1894 CloseHandle( handle );
1896 /* These two should fail - they are denied by ACL */
1897 handle = OpenProcess( PROCESS_VM_READ, FALSE, GetCurrentProcessId() );
1898 todo_wine
1899 ok(handle == NULL, "OpenProcess(PROCESS_VM_READ) should have failed\n");
1900 handle = OpenProcess( PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId() );
1901 todo_wine
1902 ok(handle == NULL, "OpenProcess(PROCESS_ALL_ACCESS) should have failed\n");
1904 /* Documented privilege elevation */
1905 ok(DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
1906 &handle, 0, TRUE, DUPLICATE_SAME_ACCESS ),
1907 "duplicating handle err:%d\n", GetLastError());
1908 TEST_GRANTED_ACCESS2( handle, PROCESS_ALL_ACCESS,
1909 STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL );
1911 CloseHandle( handle );
1913 /* Same only explicitly asking for all access rights */
1914 ok(DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
1915 &handle, PROCESS_ALL_ACCESS, TRUE, 0 ),
1916 "duplicating handle err:%d\n", GetLastError());
1917 TEST_GRANTED_ACCESS2( handle, PROCESS_ALL_ACCESS,
1918 PROCESS_ALL_ACCESS | PROCESS_QUERY_LIMITED_INFORMATION );
1919 ok(DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
1920 &handle1, PROCESS_VM_READ, TRUE, 0 ),
1921 "duplicating handle err:%d\n", GetLastError());
1922 TEST_GRANTED_ACCESS( handle1, PROCESS_VM_READ );
1923 CloseHandle( handle1 );
1924 CloseHandle( handle );
1927 static void test_impersonation_level(void)
1929 HANDLE Token, ProcessToken;
1930 HANDLE Token2;
1931 DWORD Size;
1932 TOKEN_PRIVILEGES *Privileges;
1933 TOKEN_USER *User;
1934 PRIVILEGE_SET *PrivilegeSet;
1935 BOOL AccessGranted;
1936 BOOL ret;
1937 HKEY hkey;
1938 DWORD error;
1940 pDuplicateTokenEx = (fnDuplicateTokenEx) GetProcAddress(hmod, "DuplicateTokenEx");
1941 if( !pDuplicateTokenEx ) {
1942 skip("DuplicateTokenEx is not available\n");
1943 return;
1945 SetLastError(0xdeadbeef);
1946 ret = ImpersonateSelf(SecurityAnonymous);
1947 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1949 skip("ImpersonateSelf is not implemented\n");
1950 return;
1952 ok(ret, "ImpersonateSelf(SecurityAnonymous) failed with error %d\n", GetLastError());
1953 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
1954 ok(!ret, "OpenThreadToken should have failed\n");
1955 error = GetLastError();
1956 ok(error == ERROR_CANT_OPEN_ANONYMOUS, "OpenThreadToken on anonymous token should have returned ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error);
1957 /* can't perform access check when opening object against an anonymous impersonation token */
1958 todo_wine {
1959 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
1960 ok(error == ERROR_INVALID_HANDLE, "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE instead of %d\n", error);
1962 RevertToSelf();
1964 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, &ProcessToken);
1965 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1967 ret = pDuplicateTokenEx(ProcessToken,
1968 TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_IMPERSONATE, NULL,
1969 SecurityAnonymous, TokenImpersonation, &Token);
1970 ok(ret, "DuplicateTokenEx failed with error %d\n", GetLastError());
1971 /* can't increase the impersonation level */
1972 ret = DuplicateToken(Token, SecurityIdentification, &Token2);
1973 error = GetLastError();
1974 ok(!ret && error == ERROR_BAD_IMPERSONATION_LEVEL,
1975 "Duplicating a token and increasing the impersonation level should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
1976 /* we can query anything from an anonymous token, including the user */
1977 ret = GetTokenInformation(Token, TokenUser, NULL, 0, &Size);
1978 error = GetLastError();
1979 ok(!ret && error == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenUser) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error);
1980 User = HeapAlloc(GetProcessHeap(), 0, Size);
1981 ret = GetTokenInformation(Token, TokenUser, User, Size, &Size);
1982 ok(ret, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1983 HeapFree(GetProcessHeap(), 0, User);
1985 /* PrivilegeCheck fails with SecurityAnonymous level */
1986 ret = GetTokenInformation(Token, TokenPrivileges, NULL, 0, &Size);
1987 error = GetLastError();
1988 ok(!ret && error == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenPrivileges) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error);
1989 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
1990 ret = GetTokenInformation(Token, TokenPrivileges, Privileges, Size, &Size);
1991 ok(ret, "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1993 PrivilegeSet = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PRIVILEGE_SET, Privilege[Privileges->PrivilegeCount]));
1994 PrivilegeSet->PrivilegeCount = Privileges->PrivilegeCount;
1995 memcpy(PrivilegeSet->Privilege, Privileges->Privileges, PrivilegeSet->PrivilegeCount * sizeof(PrivilegeSet->Privilege[0]));
1996 PrivilegeSet->Control = PRIVILEGE_SET_ALL_NECESSARY;
1997 HeapFree(GetProcessHeap(), 0, Privileges);
1999 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2000 error = GetLastError();
2001 ok(!ret && error == ERROR_BAD_IMPERSONATION_LEVEL, "PrivilegeCheck for SecurityAnonymous token should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
2003 CloseHandle(Token);
2005 ret = ImpersonateSelf(SecurityIdentification);
2006 ok(ret, "ImpersonateSelf(SecurityIdentification) failed with error %d\n", GetLastError());
2007 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
2008 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
2010 /* can't perform access check when opening object against an identification impersonation token */
2011 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
2012 todo_wine {
2013 ok(error == ERROR_INVALID_HANDLE, "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE instead of %d\n", error);
2015 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2016 ok(ret, "PrivilegeCheck for SecurityIdentification failed with error %d\n", GetLastError());
2017 CloseHandle(Token);
2018 RevertToSelf();
2020 ret = ImpersonateSelf(SecurityImpersonation);
2021 ok(ret, "ImpersonateSelf(SecurityImpersonation) failed with error %d\n", GetLastError());
2022 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
2023 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
2024 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
2025 ok(error == ERROR_SUCCESS, "RegOpenKeyEx should have succeeded instead of failing with %d\n", error);
2026 RegCloseKey(hkey);
2027 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2028 ok(ret, "PrivilegeCheck for SecurityImpersonation failed with error %d\n", GetLastError());
2029 RevertToSelf();
2031 CloseHandle(Token);
2032 CloseHandle(ProcessToken);
2034 HeapFree(GetProcessHeap(), 0, PrivilegeSet);
2037 static void test_SetEntriesInAcl(void)
2039 DWORD res;
2040 PSID EveryoneSid = NULL, UsersSid = NULL;
2041 PACL OldAcl = NULL, NewAcl;
2042 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
2043 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
2044 EXPLICIT_ACCESSW ExplicitAccess;
2045 static const WCHAR wszEveryone[] = {'E','v','e','r','y','o','n','e',0};
2047 if (!pSetEntriesInAclW)
2049 skip("SetEntriesInAclW is not available\n");
2050 return;
2053 NewAcl = (PACL)0xdeadbeef;
2054 res = pSetEntriesInAclW(0, NULL, NULL, &NewAcl);
2055 if(res == ERROR_CALL_NOT_IMPLEMENTED)
2057 skip("SetEntriesInAclW is not implemented\n");
2058 return;
2060 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2061 ok(NewAcl == NULL, "NewAcl=%p, expected NULL\n", NewAcl);
2063 OldAcl = HeapAlloc(GetProcessHeap(), 0, 256);
2064 res = InitializeAcl(OldAcl, 256, ACL_REVISION);
2065 if(!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2067 skip("ACLs not implemented - skipping tests\n");
2068 HeapFree(GetProcessHeap(), 0, OldAcl);
2069 return;
2071 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
2073 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
2074 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2076 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
2077 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &UsersSid);
2078 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2080 res = AddAccessAllowedAce(OldAcl, ACL_REVISION, KEY_READ, UsersSid);
2081 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2083 ExplicitAccess.grfAccessPermissions = KEY_WRITE;
2084 ExplicitAccess.grfAccessMode = GRANT_ACCESS;
2085 ExplicitAccess.grfInheritance = NO_INHERITANCE;
2086 ExplicitAccess.Trustee.pMultipleTrustee = NULL;
2087 ExplicitAccess.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2088 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
2089 ExplicitAccess.Trustee.TrusteeType = TRUSTEE_IS_UNKNOWN;
2090 ExplicitAccess.Trustee.ptstrName = (LPWSTR)EveryoneSid;
2091 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2092 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2093 ok(NewAcl != NULL, "returned acl was NULL\n");
2094 LocalFree(NewAcl);
2096 if (PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale())) != LANG_ENGLISH)
2098 skip("Non-english locale (test with hardcoded 'Everyone')\n");
2100 else
2102 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_USER;
2103 ExplicitAccess.Trustee.ptstrName = (LPWSTR)wszEveryone;
2104 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2105 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2106 ok(NewAcl != NULL, "returned acl was NULL\n");
2107 LocalFree(NewAcl);
2109 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_BAD_FORM;
2110 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2111 ok(res == ERROR_INVALID_PARAMETER, "SetEntriesInAclW failed: %u\n", res);
2112 ok(NewAcl == NULL, "returned acl wasn't NULL: %p\n", NewAcl);
2113 LocalFree(NewAcl);
2115 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_USER;
2116 ExplicitAccess.Trustee.MultipleTrusteeOperation = TRUSTEE_IS_IMPERSONATE;
2117 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2118 ok(res == ERROR_INVALID_PARAMETER, "SetEntriesInAclW failed: %u\n", res);
2119 ok(NewAcl == NULL, "returned acl wasn't NULL: %p\n", NewAcl);
2120 LocalFree(NewAcl);
2122 ExplicitAccess.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2123 ExplicitAccess.grfAccessMode = SET_ACCESS;
2124 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2125 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2126 ok(NewAcl != NULL, "returned acl was NULL\n");
2127 LocalFree(NewAcl);
2130 ExplicitAccess.grfAccessMode = REVOKE_ACCESS;
2131 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
2132 ExplicitAccess.Trustee.ptstrName = (LPWSTR)UsersSid;
2133 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2134 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2135 ok(NewAcl != NULL, "returned acl was NULL\n");
2136 LocalFree(NewAcl);
2138 LocalFree(UsersSid);
2139 LocalFree(EveryoneSid);
2140 HeapFree(GetProcessHeap(), 0, OldAcl);
2143 static void test_GetNamedSecurityInfoA(void)
2145 PSECURITY_DESCRIPTOR pSecDesc;
2146 DWORD revision;
2147 SECURITY_DESCRIPTOR_CONTROL control;
2148 PSID owner;
2149 PSID group;
2150 BOOL owner_defaulted;
2151 BOOL group_defaulted;
2152 DWORD error;
2153 BOOL ret;
2154 CHAR windows_dir[MAX_PATH];
2156 if (!pGetNamedSecurityInfoA)
2158 skip("GetNamedSecurityInfoA is not available\n");
2159 return;
2162 ret = GetWindowsDirectoryA(windows_dir, MAX_PATH);
2163 ok(ret, "GetWindowsDirectory failed with error %d\n", GetLastError());
2165 SetLastError(0xdeadbeef);
2166 error = pGetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,
2167 OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION,
2168 NULL, NULL, NULL, NULL, &pSecDesc);
2169 if (error != ERROR_SUCCESS && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2171 skip("GetNamedSecurityInfoA is not implemented\n");
2172 return;
2174 ok(!error, "GetNamedSecurityInfo failed with error %d\n", error);
2176 ret = GetSecurityDescriptorControl(pSecDesc, &control, &revision);
2177 ok(ret, "GetSecurityDescriptorControl failed with error %d\n", GetLastError());
2178 ok((control & (SE_SELF_RELATIVE|SE_DACL_PRESENT)) == (SE_SELF_RELATIVE|SE_DACL_PRESENT),
2179 "control (0x%x) doesn't have (SE_SELF_RELATIVE|SE_DACL_PRESENT) flags set\n", control);
2180 ok(revision == SECURITY_DESCRIPTOR_REVISION1, "revision was %d instead of 1\n", revision);
2181 ret = GetSecurityDescriptorOwner(pSecDesc, &owner, &owner_defaulted);
2182 ok(ret, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
2183 ok(owner != NULL, "owner should not be NULL\n");
2184 ret = GetSecurityDescriptorGroup(pSecDesc, &group, &group_defaulted);
2185 ok(ret, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2186 ok(group != NULL, "group should not be NULL\n");
2189 static void test_ConvertStringSecurityDescriptor(void)
2191 BOOL ret;
2192 PSECURITY_DESCRIPTOR pSD;
2193 static const WCHAR Blank[] = { 0 };
2195 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA)
2197 skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
2198 return;
2201 SetLastError(0xdeadbeef);
2202 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2203 "D:(A;;GA;;;WD)", 0xdeadbeef, &pSD, NULL);
2204 ok(!ret && GetLastError() == ERROR_UNKNOWN_REVISION,
2205 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_UNKNOWN_REVISION instead of %d\n",
2206 GetLastError());
2208 /* test ACE string type */
2209 SetLastError(0xdeadbeef);
2210 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2211 "D:(A;;GA;;;WD)", SDDL_REVISION_1, &pSD, NULL);
2212 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2213 LocalFree(pSD);
2215 SetLastError(0xdeadbeef);
2216 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2217 "D:(D;;GA;;;WD)", SDDL_REVISION_1, &pSD, NULL);
2218 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2219 LocalFree(pSD);
2221 SetLastError(0xdeadbeef);
2222 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2223 "ERROR:(D;;GA;;;WD)", SDDL_REVISION_1, &pSD, NULL);
2224 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2225 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2226 GetLastError());
2228 /* test ACE string access rights */
2229 SetLastError(0xdeadbeef);
2230 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2231 "D:(A;;GA;;;WD)", SDDL_REVISION_1, &pSD, NULL);
2232 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2233 LocalFree(pSD);
2234 SetLastError(0xdeadbeef);
2235 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2236 "D:(A;;GRGWGX;;;WD)", SDDL_REVISION_1, &pSD, NULL);
2237 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2238 LocalFree(pSD);
2239 SetLastError(0xdeadbeef);
2240 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2241 "D:(A;;RCSDWDWO;;;WD)", SDDL_REVISION_1, &pSD, NULL);
2242 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2243 LocalFree(pSD);
2244 SetLastError(0xdeadbeef);
2245 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2246 "D:(A;;RPWPCCDCLCSWLODTCR;;;WD)", SDDL_REVISION_1, &pSD, NULL);
2247 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2248 LocalFree(pSD);
2249 SetLastError(0xdeadbeef);
2250 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2251 "D:(A;;FAFRFWFX;;;WD)", SDDL_REVISION_1, &pSD, NULL);
2252 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2253 LocalFree(pSD);
2254 SetLastError(0xdeadbeef);
2255 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2256 "D:(A;;KAKRKWKX;;;WD)", SDDL_REVISION_1, &pSD, NULL);
2257 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2258 LocalFree(pSD);
2259 SetLastError(0xdeadbeef);
2260 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2261 "D:(A;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1, &pSD, NULL);
2262 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2263 LocalFree(pSD);
2264 SetLastError(0xdeadbeef);
2265 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2266 "S:(AU;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1, &pSD, NULL);
2267 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2268 LocalFree(pSD);
2270 /* test ACE string access right error case */
2271 SetLastError(0xdeadbeef);
2272 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2273 "D:(A;;ROB;;;WD)", SDDL_REVISION_1, &pSD, NULL);
2274 ok(!ret && GetLastError() == ERROR_INVALID_ACL,
2275 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_ACL instead of %d\n",
2276 GetLastError());
2278 /* test behaviour with NULL parameters */
2279 SetLastError(0xdeadbeef);
2280 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2281 NULL, 0xdeadbeef, &pSD, NULL);
2282 todo_wine
2283 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2284 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2285 GetLastError());
2287 SetLastError(0xdeadbeef);
2288 ret = pConvertStringSecurityDescriptorToSecurityDescriptorW(
2289 NULL, 0xdeadbeef, &pSD, NULL);
2290 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2291 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2292 GetLastError());
2294 SetLastError(0xdeadbeef);
2295 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2296 "D:(A;;ROB;;;WD)", 0xdeadbeef, NULL, NULL);
2297 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2298 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2299 GetLastError());
2301 SetLastError(0xdeadbeef);
2302 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2303 "D:(A;;ROB;;;WD)", SDDL_REVISION_1, NULL, NULL);
2304 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2305 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2306 GetLastError());
2308 /* test behaviour with empty strings */
2309 SetLastError(0xdeadbeef);
2310 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2311 "", SDDL_REVISION_1, &pSD, NULL);
2312 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2314 SetLastError(0xdeadbeef);
2315 ret = pConvertStringSecurityDescriptorToSecurityDescriptorW(
2316 Blank, SDDL_REVISION_1, &pSD, NULL);
2317 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2319 /* test ACE string SID */
2320 SetLastError(0xdeadbeef);
2321 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2322 "D:(D;;GA;;;S-1-0-0)", SDDL_REVISION_1, &pSD, NULL);
2323 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2324 LocalFree(pSD);
2326 SetLastError(0xdeadbeef);
2327 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2328 "D:(D;;GA;;;Nonexistent account)", SDDL_REVISION_1, &pSD, NULL);
2329 ok(!ret, "Expected failure, got %d\n", ret);
2330 ok(GetLastError() == ERROR_INVALID_ACL || GetLastError() == ERROR_INVALID_SID,
2331 "Expected ERROR_INVALID_ACL or ERROR_INVALID_SID, got %d\n", GetLastError());
2334 static void test_ConvertSecurityDescriptorToString()
2336 SECURITY_DESCRIPTOR desc;
2337 SECURITY_INFORMATION sec_info = OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION|SACL_SECURITY_INFORMATION;
2338 LPSTR string;
2339 DWORD size;
2340 PSID psid, psid2;
2341 PACL pacl;
2342 char sid_buf[256];
2343 char acl_buf[8192];
2344 ULONG len;
2346 if (!pConvertSecurityDescriptorToStringSecurityDescriptorA)
2348 skip("ConvertSecurityDescriptorToStringSecurityDescriptor is not available\n");
2349 return;
2351 if (!pCreateWellKnownSid)
2353 skip("CreateWellKnownSid is not available\n");
2354 return;
2357 /* It seems Windows XP adds an extra character to the length of the string for each ACE in an ACL. We
2358 * don't replicate this feature so we only test len >= strlen+1. */
2359 #define CHECK_RESULT_AND_FREE(exp_str) \
2360 ok(strcmp(string, (exp_str)) == 0, "String mismatch (expected \"%s\", got \"%s\")\n", (exp_str), string); \
2361 ok(len >= (lstrlen(exp_str) + 1), "Length mismatch (expected %d, got %d)\n", lstrlen(exp_str) + 1, len); \
2362 LocalFree(string);
2364 #define CHECK_ONE_OF_AND_FREE(exp_str1, exp_str2) \
2365 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); \
2366 ok(len >= (strlen(string) + 1), "Length mismatch (expected %d, got %d)\n", lstrlen(string) + 1, len); \
2367 LocalFree(string);
2369 InitializeSecurityDescriptor(&desc, SECURITY_DESCRIPTOR_REVISION);
2370 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2371 CHECK_RESULT_AND_FREE("");
2373 size = 4096;
2374 pCreateWellKnownSid(WinLocalSid, NULL, sid_buf, &size);
2375 SetSecurityDescriptorOwner(&desc, (PSID)sid_buf, FALSE);
2376 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2377 CHECK_RESULT_AND_FREE("O:S-1-2-0");
2379 SetSecurityDescriptorOwner(&desc, (PSID)sid_buf, TRUE);
2380 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2381 CHECK_RESULT_AND_FREE("O:S-1-2-0");
2383 size = sizeof(sid_buf);
2384 pCreateWellKnownSid(WinLocalSystemSid, NULL, sid_buf, &size);
2385 SetSecurityDescriptorOwner(&desc, (PSID)sid_buf, TRUE);
2386 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2387 CHECK_RESULT_AND_FREE("O:SY");
2389 pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid);
2390 SetSecurityDescriptorGroup(&desc, psid, TRUE);
2391 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2392 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576");
2394 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, GROUP_SECURITY_INFORMATION, &string, &len), "Conversion failed\n");
2395 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
2397 pacl = (PACL)acl_buf;
2398 InitializeAcl(pacl, sizeof(acl_buf), ACL_REVISION);
2399 SetSecurityDescriptorDacl(&desc, TRUE, pacl, TRUE);
2400 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2401 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
2403 SetSecurityDescriptorDacl(&desc, TRUE, pacl, FALSE);
2404 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2405 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
2407 pConvertStringSidToSidA("S-1-5-6", &psid2);
2408 pAddAccessAllowedAceEx(pacl, ACL_REVISION, NO_PROPAGATE_INHERIT_ACE, 0xf0000000, psid2);
2409 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2410 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)");
2412 pAddAccessAllowedAceEx(pacl, ACL_REVISION, INHERIT_ONLY_ACE|INHERITED_ACE, 0x00000003, psid2);
2413 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2414 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)");
2416 pAddAccessDeniedAceEx(pacl, ACL_REVISION, OBJECT_INHERIT_ACE|CONTAINER_INHERIT_ACE, 0xffffffff, psid);
2417 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2418 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)");
2421 pacl = (PACL)acl_buf;
2422 InitializeAcl(pacl, sizeof(acl_buf), ACL_REVISION);
2423 SetSecurityDescriptorSacl(&desc, TRUE, pacl, FALSE);
2424 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2425 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:");
2427 /* fails in win2k */
2428 SetSecurityDescriptorDacl(&desc, TRUE, NULL, FALSE);
2429 pAddAuditAccessAceEx(pacl, ACL_REVISION, VALID_INHERIT_FLAGS, KEY_READ|KEY_WRITE, psid2, TRUE, TRUE);
2430 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len))
2432 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)", /* XP */
2433 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)" /* Vista */);
2436 /* fails in win2k */
2437 pAddAuditAccessAceEx(pacl, ACL_REVISION, NO_PROPAGATE_INHERIT_ACE, FILE_GENERIC_READ|FILE_GENERIC_WRITE, psid2, TRUE, FALSE);
2438 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len))
2440 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", /* XP */
2441 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)" /* Vista */);
2445 static void test_SetSecurityDescriptorControl (PSECURITY_DESCRIPTOR sec)
2447 SECURITY_DESCRIPTOR_CONTROL ref;
2448 SECURITY_DESCRIPTOR_CONTROL test;
2450 SECURITY_DESCRIPTOR_CONTROL const mutable
2451 = SE_DACL_AUTO_INHERIT_REQ | SE_SACL_AUTO_INHERIT_REQ
2452 | SE_DACL_AUTO_INHERITED | SE_SACL_AUTO_INHERITED
2453 | SE_DACL_PROTECTED | SE_SACL_PROTECTED
2454 | 0x00000040 | 0x00000080 /* not defined in winnt.h */
2456 SECURITY_DESCRIPTOR_CONTROL const immutable
2457 = SE_OWNER_DEFAULTED | SE_GROUP_DEFAULTED
2458 | SE_DACL_PRESENT | SE_DACL_DEFAULTED
2459 | SE_SACL_PRESENT | SE_SACL_DEFAULTED
2460 | SE_RM_CONTROL_VALID | SE_SELF_RELATIVE
2463 int bit;
2464 DWORD dwRevision;
2465 LPCSTR fmt = "Expected error %s, got %u\n";
2467 GetSecurityDescriptorControl (sec, &ref, &dwRevision);
2469 /* The mutable bits are mutable regardless of the truth of
2470 SE_DACL_PRESENT and/or SE_SACL_PRESENT */
2472 /* Check call barfs if any bit-of-interest is immutable */
2473 for (bit = 0; bit < 16; ++bit)
2475 SECURITY_DESCRIPTOR_CONTROL const bitOfInterest = 1 << bit;
2476 SECURITY_DESCRIPTOR_CONTROL setOrClear = ref & bitOfInterest;
2478 SECURITY_DESCRIPTOR_CONTROL ctrl;
2480 DWORD dwExpect = (bitOfInterest & immutable)
2481 ? ERROR_INVALID_PARAMETER : 0xbebecaca;
2482 LPCSTR strExpect = (bitOfInterest & immutable)
2483 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
2485 ctrl = (bitOfInterest & mutable) ? ref + bitOfInterest : ref;
2486 setOrClear ^= bitOfInterest;
2487 SetLastError (0xbebecaca);
2488 pSetSecurityDescriptorControl (sec, bitOfInterest, setOrClear);
2489 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
2490 GetSecurityDescriptorControl(sec, &test, &dwRevision);
2491 expect_eq(test, ctrl, int, "%x");
2493 ctrl = ref;
2494 setOrClear ^= bitOfInterest;
2495 SetLastError (0xbebecaca);
2496 pSetSecurityDescriptorControl (sec, bitOfInterest, setOrClear);
2497 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
2498 GetSecurityDescriptorControl (sec, &test, &dwRevision);
2499 expect_eq(test, ref, int, "%x");
2502 /* Check call barfs if any bit-to-set is immutable
2503 even when not a bit-of-interest */
2504 for (bit = 0; bit < 16; ++bit)
2506 SECURITY_DESCRIPTOR_CONTROL const bitsOfInterest = mutable;
2507 SECURITY_DESCRIPTOR_CONTROL setOrClear = ref & bitsOfInterest;
2509 SECURITY_DESCRIPTOR_CONTROL ctrl;
2511 DWORD dwExpect = ((1 << bit) & immutable)
2512 ? ERROR_INVALID_PARAMETER : 0xbebecaca;
2513 LPCSTR strExpect = ((1 << bit) & immutable)
2514 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
2516 ctrl = ((1 << bit) & immutable) ? test : ref | mutable;
2517 setOrClear ^= bitsOfInterest;
2518 SetLastError (0xbebecaca);
2519 pSetSecurityDescriptorControl (sec, bitsOfInterest, setOrClear | (1 << bit));
2520 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
2521 GetSecurityDescriptorControl(sec, &test, &dwRevision);
2522 expect_eq(test, ctrl, int, "%x");
2524 ctrl = ((1 << bit) & immutable) ? test : ref | (1 << bit);
2525 setOrClear ^= bitsOfInterest;
2526 SetLastError (0xbebecaca);
2527 pSetSecurityDescriptorControl (sec, bitsOfInterest, setOrClear | (1 << bit));
2528 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
2529 GetSecurityDescriptorControl(sec, &test, &dwRevision);
2530 expect_eq(test, ctrl, int, "%x");
2534 static void test_PrivateObjectSecurity(void)
2536 SECURITY_INFORMATION sec_info = OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION|SACL_SECURITY_INFORMATION;
2537 SECURITY_DESCRIPTOR_CONTROL ctrl;
2538 PSECURITY_DESCRIPTOR sec;
2539 DWORD dwDescSize;
2540 DWORD dwRevision;
2541 DWORD retSize;
2542 LPSTR string;
2543 ULONG len;
2544 PSECURITY_DESCRIPTOR buf;
2546 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA)
2548 skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
2549 return;
2552 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
2553 "O:SY"
2554 "G:S-1-5-21-93476-23408-4576"
2555 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)"
2556 "(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
2557 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
2558 SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
2560 test_SetSecurityDescriptorControl(sec);
2562 LocalFree(sec);
2564 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
2565 "O:SY"
2566 "G:S-1-5-21-93476-23408-4576",
2567 SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
2569 test_SetSecurityDescriptorControl(sec);
2571 LocalFree(sec);
2573 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
2574 "O:SY"
2575 "G:S-1-5-21-93476-23408-4576"
2576 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
2577 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
2578 buf = HeapAlloc(GetProcessHeap(), 0, dwDescSize);
2579 pSetSecurityDescriptorControl(sec, SE_DACL_PROTECTED, SE_DACL_PROTECTED);
2580 GetSecurityDescriptorControl(sec, &ctrl, &dwRevision);
2581 expect_eq(ctrl, 0x9014, int, "%x");
2583 ok(GetPrivateObjectSecurity(sec, GROUP_SECURITY_INFORMATION, buf, dwDescSize, &retSize),
2584 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
2585 ok(retSize <= dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
2586 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2587 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
2588 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
2589 expect_eq(ctrl, 0x8000, int, "%x");
2591 ok(GetPrivateObjectSecurity(sec, GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION, buf, dwDescSize, &retSize),
2592 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
2593 ok(retSize <= dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
2594 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed err=%u\n", GetLastError());
2595 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)");
2596 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
2597 expect_eq(ctrl, 0x8004, int, "%x");
2599 ok(GetPrivateObjectSecurity(sec, sec_info, buf, dwDescSize, &retSize),
2600 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
2601 ok(retSize == dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
2602 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2603 CHECK_RESULT_AND_FREE("O:SY"
2604 "G:S-1-5-21-93476-23408-4576"
2605 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
2606 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)");
2607 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
2608 expect_eq(ctrl, 0x8014, int, "%x");
2610 SetLastError(0xdeadbeef);
2611 ok(GetPrivateObjectSecurity(sec, sec_info, buf, 5, &retSize) == FALSE, "GetPrivateObjectSecurity should have failed\n");
2612 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected error ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
2614 LocalFree(sec);
2615 HeapFree(GetProcessHeap(), 0, buf);
2617 #undef CHECK_RESULT_AND_FREE
2618 #undef CHECK_ONE_OF_AND_FREE
2620 static void test_acls(void)
2622 char buffer[256];
2623 PACL pAcl = (PACL)buffer;
2624 BOOL ret;
2626 SetLastError(0xdeadbeef);
2627 ret = InitializeAcl(pAcl, sizeof(ACL) - 1, ACL_REVISION);
2628 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2630 skip("InitializeAcl is not implemented\n");
2631 return;
2634 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InitializeAcl with too small a buffer should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
2636 SetLastError(0xdeadbeef);
2637 ret = InitializeAcl(pAcl, 0xffffffff, ACL_REVISION);
2638 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "InitializeAcl with too large a buffer should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
2640 SetLastError(0xdeadbeef);
2641 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION1);
2642 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "InitializeAcl(ACL_REVISION1) should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
2644 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION2);
2645 ok(ret, "InitializeAcl(ACL_REVISION2) failed with error %d\n", GetLastError());
2647 ret = IsValidAcl(pAcl);
2648 ok(ret, "IsValidAcl failed with error %d\n", GetLastError());
2650 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION3);
2651 ok(ret, "InitializeAcl(ACL_REVISION3) failed with error %d\n", GetLastError());
2653 ret = IsValidAcl(pAcl);
2654 ok(ret, "IsValidAcl failed with error %d\n", GetLastError());
2656 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION4);
2657 ok(ret, "InitializeAcl(ACL_REVISION4) failed with error %d\n", GetLastError());
2659 ret = IsValidAcl(pAcl);
2660 ok(ret, "IsValidAcl failed with error %d\n", GetLastError());
2662 SetLastError(0xdeadbeef);
2663 ret = InitializeAcl(pAcl, sizeof(buffer), -1);
2664 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "InitializeAcl(-1) failed with error %d\n", GetLastError());
2667 static void test_GetSecurityInfo(void)
2669 HANDLE obj;
2670 PSECURITY_DESCRIPTOR sd;
2671 PSID owner, group;
2672 PACL dacl;
2673 DWORD ret;
2675 if (!pGetSecurityInfo)
2677 win_skip("GetSecurityInfo is not available\n");
2678 return;
2681 /* Create something. Files have lots of associated security info. */
2682 obj = CreateFile(myARGV[0], GENERIC_READ, FILE_SHARE_READ, NULL,
2683 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2684 if (obj == INVALID_HANDLE_VALUE)
2686 skip("Couldn't create an object for GetSecurityInfo test\n");
2687 return;
2690 ret = pGetSecurityInfo(obj, SE_FILE_OBJECT,
2691 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
2692 &owner, &group, &dacl, NULL, &sd);
2693 if (ret == ERROR_CALL_NOT_IMPLEMENTED)
2695 win_skip("GetSecurityInfo is not implemented\n");
2696 CloseHandle(obj);
2697 return;
2699 ok(ret == ERROR_SUCCESS, "GetSecurityInfo returned %d\n", ret);
2700 ok(sd != NULL, "GetSecurityInfo\n");
2701 ok(owner != NULL, "GetSecurityInfo\n");
2702 ok(group != NULL, "GetSecurityInfo\n");
2703 ok(dacl != NULL, "GetSecurityInfo\n");
2704 ok(IsValidAcl(dacl), "GetSecurityInfo\n");
2706 LocalFree(sd);
2708 /* If we don't ask for the security descriptor, Windows will still give us
2709 the other stuff, leaving us no way to free it. */
2710 ret = pGetSecurityInfo(obj, SE_FILE_OBJECT,
2711 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
2712 &owner, &group, &dacl, NULL, NULL);
2713 ok(ret == ERROR_SUCCESS, "GetSecurityInfo returned %d\n", ret);
2714 ok(owner != NULL, "GetSecurityInfo\n");
2715 ok(group != NULL, "GetSecurityInfo\n");
2716 ok(dacl != NULL, "GetSecurityInfo\n");
2717 ok(IsValidAcl(dacl), "GetSecurityInfo\n");
2719 CloseHandle(obj);
2722 START_TEST(security)
2724 init();
2725 if (!hmod) return;
2727 if (myARGC >= 3)
2729 test_process_security_child();
2730 return;
2732 test_sid();
2733 test_trustee();
2734 test_luid();
2735 test_CreateWellKnownSid();
2736 test_FileSecurity();
2737 test_AccessCheck();
2738 test_token_attr();
2739 test_LookupAccountSid();
2740 test_LookupAccountName();
2741 test_security_descriptor();
2742 test_process_security();
2743 test_impersonation_level();
2744 test_SetEntriesInAcl();
2745 test_GetNamedSecurityInfoA();
2746 test_ConvertStringSecurityDescriptor();
2747 test_ConvertSecurityDescriptorToString();
2748 test_PrivateObjectSecurity();
2749 test_acls();
2750 test_GetSecurityInfo();