advapi32: LookupAccountNameW() - return correct domain name length for not-so-well...
[wine/multimedia.git] / dlls / advapi32 / tests / security.c
blob4e9e7ef0742fb57a48811ea86b6cc662fb779301
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 /* native windows crashes if domainW or accountW is NULL */
1319 /* try a small account buffer */
1320 acc_sizeW = 1;
1321 dom_sizeW = MAX_PATH;
1322 accountW[0] = 0;
1323 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1324 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1325 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1326 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1328 /* try a 0 sized account buffer */
1329 acc_sizeW = 0;
1330 dom_sizeW = MAX_PATH;
1331 accountW[0] = 0;
1332 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1333 /* this can fail or succeed depending on OS version but the size will always be returned */
1334 ok(acc_sizeW == real_acc_sizeW + 1,
1335 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1336 real_acc_sizeW + 1, acc_sizeW);
1338 /* try a 0 sized account buffer */
1339 acc_sizeW = 0;
1340 dom_sizeW = MAX_PATH;
1341 ret = LookupAccountSidW(NULL, pUsersSid, NULL, &acc_sizeW, domainW, &dom_sizeW, &use);
1342 /* this can fail or succeed depending on OS version but the size will always be returned */
1343 ok(acc_sizeW == real_acc_sizeW + 1,
1344 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1345 real_acc_sizeW + 1, acc_sizeW);
1347 /* try a small domain buffer */
1348 dom_sizeW = 1;
1349 acc_sizeW = MAX_PATH;
1350 accountW[0] = 0;
1351 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1352 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1353 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1354 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1356 /* try a 0 sized domain buffer */
1357 dom_sizeW = 0;
1358 acc_sizeW = MAX_PATH;
1359 accountW[0] = 0;
1360 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1361 /* this can fail or succeed depending on OS version but the size will always be returned */
1362 ok(dom_sizeW == real_dom_sizeW + 1,
1363 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1364 real_dom_sizeW + 1, dom_sizeW);
1366 /* try a 0 sized domain buffer */
1367 dom_sizeW = 0;
1368 acc_sizeW = MAX_PATH;
1369 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, NULL, &dom_sizeW, &use);
1370 /* this can fail or succeed depending on OS version but the size will always be returned */
1371 ok(dom_sizeW == real_dom_sizeW + 1,
1372 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1373 real_dom_sizeW + 1, dom_sizeW);
1375 FreeSid(pUsersSid);
1377 if (pCreateWellKnownSid && pConvertSidToStringSidA)
1379 trace("Well Known SIDs:\n");
1380 for (i = 0; i <= 60; i++)
1382 size = SECURITY_MAX_SID_SIZE;
1383 if (pCreateWellKnownSid(i, NULL, &max_sid.sid, &size))
1385 if (pConvertSidToStringSidA(&max_sid.sid, &str_sidA))
1387 acc_sizeA = MAX_PATH;
1388 dom_sizeA = MAX_PATH;
1389 if (LookupAccountSidA(NULL, &max_sid.sid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use))
1390 trace(" %d: %s %s\\%s %d\n", i, str_sidA, domainA, accountA, use);
1391 LocalFree(str_sidA);
1394 else
1396 if (GetLastError() != ERROR_INVALID_PARAMETER)
1397 trace(" CreateWellKnownSid(%d) failed: %d\n", i, GetLastError());
1398 else
1399 trace(" %d: not supported\n", i);
1403 pLsaQueryInformationPolicy = (fnLsaQueryInformationPolicy)GetProcAddress( hmod, "LsaQueryInformationPolicy");
1404 pLsaOpenPolicy = (fnLsaOpenPolicy)GetProcAddress( hmod, "LsaOpenPolicy");
1405 pLsaFreeMemory = (fnLsaFreeMemory)GetProcAddress( hmod, "LsaFreeMemory");
1406 pLsaClose = (fnLsaClose)GetProcAddress( hmod, "LsaClose");
1408 if (pLsaQueryInformationPolicy && pLsaOpenPolicy && pLsaFreeMemory && pLsaClose)
1410 NTSTATUS status;
1411 LSA_HANDLE handle;
1412 LSA_OBJECT_ATTRIBUTES object_attributes;
1414 ZeroMemory(&object_attributes, sizeof(object_attributes));
1415 object_attributes.Length = sizeof(object_attributes);
1417 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_ALL_ACCESS, &handle);
1418 ok(status == STATUS_SUCCESS || status == STATUS_ACCESS_DENIED,
1419 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status);
1421 /* try a more restricted access mask if necessary */
1422 if (status == STATUS_ACCESS_DENIED) {
1423 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
1424 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_VIEW_LOCAL_INFORMATION, &handle);
1425 ok(status == STATUS_SUCCESS, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status);
1428 if (status == STATUS_SUCCESS)
1430 PPOLICY_ACCOUNT_DOMAIN_INFO info;
1431 status = pLsaQueryInformationPolicy(handle, PolicyAccountDomainInformation, (PVOID*)&info);
1432 ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy() failed, returned 0x%08x\n", status);
1433 if (status == STATUS_SUCCESS)
1435 ok(info->DomainSid!=0, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) missing SID\n");
1436 if (info->DomainSid)
1438 int count = *GetSidSubAuthorityCount(info->DomainSid);
1439 CopySid(GetSidLengthRequired(count), &max_sid, info->DomainSid);
1440 test_sid_str((PSID)&max_sid.sid);
1441 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_ADMIN;
1442 max_sid.sid.SubAuthorityCount = count + 1;
1443 test_sid_str((PSID)&max_sid.sid);
1444 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_GUEST;
1445 test_sid_str((PSID)&max_sid.sid);
1446 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ADMINS;
1447 test_sid_str((PSID)&max_sid.sid);
1448 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_USERS;
1449 test_sid_str((PSID)&max_sid.sid);
1450 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_GUESTS;
1451 test_sid_str((PSID)&max_sid.sid);
1452 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_COMPUTERS;
1453 test_sid_str((PSID)&max_sid.sid);
1454 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CONTROLLERS;
1455 test_sid_str((PSID)&max_sid.sid);
1456 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CERT_ADMINS;
1457 test_sid_str((PSID)&max_sid.sid);
1458 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_SCHEMA_ADMINS;
1459 test_sid_str((PSID)&max_sid.sid);
1460 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ENTERPRISE_ADMINS;
1461 test_sid_str((PSID)&max_sid.sid);
1462 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_POLICY_ADMINS;
1463 test_sid_str((PSID)&max_sid.sid);
1464 max_sid.sid.SubAuthority[count] = DOMAIN_ALIAS_RID_RAS_SERVERS;
1465 test_sid_str((PSID)&max_sid.sid);
1466 max_sid.sid.SubAuthority[count] = 1000; /* first user account */
1467 test_sid_str((PSID)&max_sid.sid);
1470 pLsaFreeMemory((LPVOID)info);
1473 status = pLsaClose(handle);
1474 ok(status == STATUS_SUCCESS, "LsaClose() failed, returned 0x%08x\n", status);
1480 static void get_sid_info(PSID psid, LPSTR *user, LPSTR *dom)
1482 static CHAR account[UNLEN + 1];
1483 static CHAR domain[UNLEN + 1];
1484 DWORD size, dom_size;
1485 SID_NAME_USE use;
1487 *user = account;
1488 *dom = domain;
1490 size = dom_size = UNLEN + 1;
1491 account[0] = '\0';
1492 domain[0] = '\0';
1493 LookupAccountSidA(NULL, psid, account, &size, domain, &dom_size, &use);
1496 static void test_LookupAccountName(void)
1498 DWORD sid_size, domain_size, user_size;
1499 DWORD sid_save, domain_save;
1500 CHAR user_name[UNLEN + 1];
1501 SID_NAME_USE sid_use;
1502 LPSTR domain, account, sid_dom;
1503 PSID psid;
1504 BOOL ret;
1506 /* native crashes if (assuming all other parameters correct):
1507 * - peUse is NULL
1508 * - Sid is NULL and cbSid is > 0
1509 * - cbSid or cchReferencedDomainName are NULL
1510 * - ReferencedDomainName is NULL and cchReferencedDomainName is the correct size
1513 user_size = UNLEN + 1;
1514 SetLastError(0xdeadbeef);
1515 ret = GetUserNameA(user_name, &user_size);
1516 if (!ret && (GetLastError() == ERROR_NOT_LOGGED_ON))
1518 /* Probably on win9x where the user used 'Cancel' instead of properly logging in */
1519 skip("Cannot get the user name (win9x and not logged in properly)\n");
1520 return;
1522 ok(ret, "Failed to get user name : %d\n", GetLastError());
1524 /* get sizes */
1525 sid_size = 0;
1526 domain_size = 0;
1527 sid_use = 0xcafebabe;
1528 SetLastError(0xdeadbeef);
1529 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, NULL, &domain_size, &sid_use);
1530 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1532 skip("LookupAccountNameA is not implemented\n");
1533 return;
1535 ok(!ret, "Expected 0, got %d\n", ret);
1536 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1537 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1538 ok(sid_size != 0, "Expected non-zero sid size\n");
1539 ok(domain_size != 0, "Expected non-zero domain size\n");
1540 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
1542 sid_save = sid_size;
1543 domain_save = domain_size;
1545 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
1546 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
1548 /* try valid account name */
1549 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, domain, &domain_size, &sid_use);
1550 get_sid_info(psid, &account, &sid_dom);
1551 ok(ret, "Failed to lookup account name\n");
1552 ok(sid_size == GetLengthSid(psid), "Expected %d, got %d\n", GetLengthSid(psid), sid_size);
1553 todo_wine
1555 ok(!lstrcmp(account, user_name), "Expected %s, got %s\n", user_name, account);
1556 ok(!lstrcmp(domain, sid_dom), "Expected %s, got %s\n", sid_dom, domain);
1558 ok(domain_size == domain_save - 1, "Expected %d, got %d\n", domain_save - 1, domain_size);
1559 ok(lstrlen(domain) == domain_size, "Expected %d, got %d\n", lstrlen(domain), domain_size);
1560 ok(sid_use == SidTypeUser, "Expected SidTypeUser (%d), got %d\n", SidTypeUser, sid_use);
1561 domain_size = domain_save;
1562 sid_size = sid_save;
1564 if (PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale())) != LANG_ENGLISH)
1566 skip("Non-english locale (test with hardcoded 'Everyone')\n");
1568 else
1570 ret = LookupAccountNameA(NULL, "Everyone", psid, &sid_size, domain, &domain_size, &sid_use);
1571 get_sid_info(psid, &account, &sid_dom);
1572 ok(ret, "Failed to lookup account name\n");
1573 ok(sid_size != 0, "sid_size was zero\n");
1574 ok(!lstrcmp(account, "Everyone"), "Expected Everyone, got %s\n", account);
1575 ok(!lstrcmp(domain, sid_dom), "Expected %s, got %s\n", sid_dom, domain);
1576 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
1577 ok(lstrlen(domain) == domain_size, "Expected %d, got %d\n", lstrlen(domain), domain_size);
1578 ok(sid_use == SidTypeWellKnownGroup, "Expected SidTypeWellKnownGroup (%d), got %d\n", SidTypeWellKnownGroup, sid_use);
1579 domain_size = domain_save;
1582 /* NULL Sid with zero sid size */
1583 SetLastError(0xdeadbeef);
1584 sid_size = 0;
1585 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
1586 ok(!ret, "Expected 0, got %d\n", ret);
1587 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1588 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1589 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1590 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1592 /* try cchReferencedDomainName - 1 */
1593 SetLastError(0xdeadbeef);
1594 domain_size--;
1595 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
1596 ok(!ret, "Expected 0, got %d\n", ret);
1597 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1598 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1599 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1600 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1602 /* NULL ReferencedDomainName with zero domain name size */
1603 SetLastError(0xdeadbeef);
1604 domain_size = 0;
1605 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, NULL, &domain_size, &sid_use);
1606 ok(!ret, "Expected 0, got %d\n", ret);
1607 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1608 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1609 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1610 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1612 HeapFree(GetProcessHeap(), 0, psid);
1613 HeapFree(GetProcessHeap(), 0, domain);
1615 /* get sizes for NULL account name */
1616 sid_size = 0;
1617 domain_size = 0;
1618 sid_use = 0xcafebabe;
1619 SetLastError(0xdeadbeef);
1620 ret = LookupAccountNameA(NULL, NULL, NULL, &sid_size, NULL, &domain_size, &sid_use);
1621 ok(!ret, "Expected 0, got %d\n", ret);
1622 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1623 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1624 ok(sid_size != 0, "Expected non-zero sid size\n");
1625 ok(domain_size != 0, "Expected non-zero domain size\n");
1626 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
1628 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
1629 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
1631 /* try NULL account name */
1632 ret = LookupAccountNameA(NULL, NULL, psid, &sid_size, domain, &domain_size, &sid_use);
1633 get_sid_info(psid, &account, &sid_dom);
1634 ok(ret, "Failed to lookup account name\n");
1635 todo_wine
1637 /* Using a fixed string will not work on different locales */
1638 ok(!lstrcmp(account, domain),
1639 "Got %s for account and %s for domain, these should be the same\n",
1640 account, domain);
1641 ok(sid_use == SidTypeDomain, "Expected SidTypeDomain (%d), got %d\n", SidTypeDomain, sid_use);
1644 /* try an invalid account name */
1645 SetLastError(0xdeadbeef);
1646 sid_size = 0;
1647 domain_size = 0;
1648 ret = LookupAccountNameA(NULL, "oogabooga", NULL, &sid_size, NULL, &domain_size, &sid_use);
1649 ok(!ret, "Expected 0, got %d\n", ret);
1650 todo_wine
1652 ok(GetLastError() == ERROR_NONE_MAPPED ||
1653 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE),
1654 "Expected ERROR_NONE_MAPPED, got %d\n", GetLastError());
1655 ok(sid_size == 0, "Expected 0, got %d\n", sid_size);
1656 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
1659 HeapFree(GetProcessHeap(), 0, psid);
1660 HeapFree(GetProcessHeap(), 0, domain);
1663 static void test_security_descriptor(void)
1665 SECURITY_DESCRIPTOR sd;
1666 char buf[8192];
1667 DWORD size;
1668 BOOL isDefault, isPresent, ret;
1669 PACL pacl;
1670 PSID psid;
1672 SetLastError(0xdeadbeef);
1673 ret = InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
1674 if (ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1676 skip("InitializeSecurityDescriptor is not implemented\n");
1677 return;
1680 ok(GetSecurityDescriptorOwner(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
1681 expect_eq(psid, NULL, PSID, "%p");
1682 expect_eq(isDefault, FALSE, BOOL, "%d");
1683 sd.Control |= SE_DACL_PRESENT | SE_SACL_PRESENT;
1685 SetLastError(0xdeadbeef);
1686 size = 5;
1687 expect_eq(MakeSelfRelativeSD(&sd, buf, &size), FALSE, BOOL, "%d");
1688 expect_eq(GetLastError(), ERROR_INSUFFICIENT_BUFFER, DWORD, "%u");
1689 ok(size > 5, "Size not increased\n");
1690 if (size <= 8192)
1692 expect_eq(MakeSelfRelativeSD(&sd, buf, &size), TRUE, BOOL, "%d");
1693 ok(GetSecurityDescriptorOwner(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
1694 expect_eq(psid, NULL, PSID, "%p");
1695 expect_eq(isDefault, FALSE, BOOL, "%d");
1696 ok(GetSecurityDescriptorGroup(&sd, &psid, &isDefault), "GetSecurityDescriptorGroup failed\n");
1697 expect_eq(psid, NULL, PSID, "%p");
1698 expect_eq(isDefault, FALSE, BOOL, "%d");
1699 ok(GetSecurityDescriptorDacl(&sd, &isPresent, &pacl, &isDefault), "GetSecurityDescriptorDacl failed\n");
1700 expect_eq(isPresent, TRUE, BOOL, "%d");
1701 expect_eq(psid, NULL, PSID, "%p");
1702 expect_eq(isDefault, FALSE, BOOL, "%d");
1703 ok(GetSecurityDescriptorSacl(&sd, &isPresent, &pacl, &isDefault), "GetSecurityDescriptorSacl failed\n");
1704 expect_eq(isPresent, TRUE, BOOL, "%d");
1705 expect_eq(psid, NULL, PSID, "%p");
1706 expect_eq(isDefault, FALSE, BOOL, "%d");
1710 #define TEST_GRANTED_ACCESS(a,b) test_granted_access(a,b,0,__LINE__)
1711 #define TEST_GRANTED_ACCESS2(a,b,c) test_granted_access(a,b,c,__LINE__)
1712 static void test_granted_access(HANDLE handle, ACCESS_MASK access,
1713 ACCESS_MASK alt, int line)
1715 OBJECT_BASIC_INFORMATION obj_info;
1716 NTSTATUS status;
1718 if (!pNtQueryObject)
1720 skip_(__FILE__, line)("Not NT platform - skipping tests\n");
1721 return;
1724 status = pNtQueryObject( handle, ObjectBasicInformation, &obj_info,
1725 sizeof(obj_info), NULL );
1726 ok_(__FILE__, line)(!status, "NtQueryObject with err: %08x\n", status);
1727 if (alt)
1728 ok_(__FILE__, line)(obj_info.GrantedAccess == access ||
1729 obj_info.GrantedAccess == alt, "Granted access should be 0x%08x "
1730 "or 0x%08x, instead of 0x%08x\n", access, alt, obj_info.GrantedAccess);
1731 else
1732 ok_(__FILE__, line)(obj_info.GrantedAccess == access, "Granted access should "
1733 "be 0x%08x, instead of 0x%08x\n", access, obj_info.GrantedAccess);
1736 #define CHECK_SET_SECURITY(o,i,e) \
1737 do{ \
1738 BOOL res; \
1739 DWORD err; \
1740 SetLastError( 0xdeadbeef ); \
1741 res = SetKernelObjectSecurity( o, i, SecurityDescriptor ); \
1742 err = GetLastError(); \
1743 if (e == ERROR_SUCCESS) \
1744 ok(res, "SetKernelObjectSecurity failed with %d\n", err); \
1745 else \
1746 ok(!res && err == e, "SetKernelObjectSecurity should have failed " \
1747 "with %s, instead of %d\n", #e, err); \
1748 }while(0)
1750 static void test_process_security(void)
1752 BOOL res;
1753 char owner[32], group[32];
1754 PSID AdminSid = NULL, UsersSid = NULL;
1755 PACL Acl = NULL;
1756 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL;
1757 char buffer[MAX_PATH];
1758 PROCESS_INFORMATION info;
1759 STARTUPINFOA startup;
1760 SECURITY_ATTRIBUTES psa;
1761 HANDLE token, event;
1762 DWORD tmp;
1764 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
1765 res = InitializeAcl(Acl, 256, ACL_REVISION);
1766 if (!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1768 skip("ACLs not implemented - skipping tests\n");
1769 HeapFree(GetProcessHeap(), 0, Acl);
1770 return;
1772 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
1774 /* get owner from the token we might be running as a user not admin */
1775 res = OpenProcessToken( GetCurrentProcess(), MAXIMUM_ALLOWED, &token );
1776 ok(res, "OpenProcessToken failed with error %d\n", GetLastError());
1777 if (!res)
1779 HeapFree(GetProcessHeap(), 0, Acl);
1780 return;
1783 res = GetTokenInformation( token, TokenOwner, owner, sizeof(owner), &tmp );
1784 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
1785 AdminSid = ((TOKEN_OWNER*)owner)->Owner;
1786 res = GetTokenInformation( token, TokenPrimaryGroup, group, sizeof(group), &tmp );
1787 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
1788 UsersSid = ((TOKEN_PRIMARY_GROUP*)group)->PrimaryGroup;
1790 CloseHandle( token );
1791 if (!res)
1793 HeapFree(GetProcessHeap(), 0, Acl);
1794 return;
1797 res = AddAccessDeniedAce(Acl, ACL_REVISION, PROCESS_VM_READ, AdminSid);
1798 ok(res, "AddAccessDeniedAce failed with error %d\n", GetLastError());
1799 res = AddAccessAllowedAce(Acl, ACL_REVISION, PROCESS_ALL_ACCESS, AdminSid);
1800 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
1802 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
1803 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
1804 ok(res, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
1806 event = CreateEvent( NULL, TRUE, TRUE, "test_event" );
1807 ok(event != NULL, "CreateEvent %d\n", GetLastError());
1809 SecurityDescriptor->Revision = 0;
1810 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_UNKNOWN_REVISION );
1811 SecurityDescriptor->Revision = SECURITY_DESCRIPTOR_REVISION;
1813 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_INVALID_SECURITY_DESCR );
1814 CHECK_SET_SECURITY( event, GROUP_SECURITY_INFORMATION, ERROR_INVALID_SECURITY_DESCR );
1815 CHECK_SET_SECURITY( event, SACL_SECURITY_INFORMATION, ERROR_ACCESS_DENIED );
1816 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
1817 /* NULL DACL is valid and means default DACL from token */
1818 SecurityDescriptor->Control |= SE_DACL_PRESENT;
1819 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
1821 /* Set owner and group and dacl */
1822 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
1823 ok(res, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
1824 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_SUCCESS );
1825 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, FALSE);
1826 ok(res, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
1827 CHECK_SET_SECURITY( event, GROUP_SECURITY_INFORMATION, ERROR_SUCCESS );
1828 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
1829 ok(res, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1830 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
1832 sprintf(buffer, "%s tests/security.c test", myARGV[0]);
1833 memset(&startup, 0, sizeof(startup));
1834 startup.cb = sizeof(startup);
1835 startup.dwFlags = STARTF_USESHOWWINDOW;
1836 startup.wShowWindow = SW_SHOWNORMAL;
1838 psa.nLength = sizeof(psa);
1839 psa.lpSecurityDescriptor = SecurityDescriptor;
1840 psa.bInheritHandle = TRUE;
1842 /* Doesn't matter what ACL say we should get full access for ourselves */
1843 ok(CreateProcessA( NULL, buffer, &psa, NULL, FALSE, 0, NULL, NULL, &startup, &info ),
1844 "CreateProcess with err:%d\n", GetLastError());
1845 TEST_GRANTED_ACCESS2( info.hProcess, PROCESS_ALL_ACCESS,
1846 STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL );
1847 winetest_wait_child_process( info.hProcess );
1849 CloseHandle( info.hProcess );
1850 CloseHandle( info.hThread );
1851 CloseHandle( event );
1852 HeapFree(GetProcessHeap(), 0, Acl);
1853 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
1856 static void test_process_security_child(void)
1858 HANDLE handle, handle1;
1859 BOOL ret;
1860 DWORD err;
1862 handle = OpenProcess( PROCESS_TERMINATE, FALSE, GetCurrentProcessId() );
1863 ok(handle != NULL, "OpenProcess(PROCESS_TERMINATE) with err:%d\n", GetLastError());
1864 TEST_GRANTED_ACCESS( handle, PROCESS_TERMINATE );
1866 ok(DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
1867 &handle1, 0, TRUE, DUPLICATE_SAME_ACCESS ),
1868 "duplicating handle err:%d\n", GetLastError());
1869 TEST_GRANTED_ACCESS( handle1, PROCESS_TERMINATE );
1871 CloseHandle( handle1 );
1873 SetLastError( 0xdeadbeef );
1874 ret = DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
1875 &handle1, PROCESS_ALL_ACCESS, TRUE, 0 );
1876 err = GetLastError();
1877 todo_wine
1878 ok(!ret && err == ERROR_ACCESS_DENIED, "duplicating handle should have failed "
1879 "with STATUS_ACCESS_DENIED, instead of err:%d\n", err);
1881 CloseHandle( handle );
1883 /* These two should fail - they are denied by ACL */
1884 handle = OpenProcess( PROCESS_VM_READ, FALSE, GetCurrentProcessId() );
1885 todo_wine
1886 ok(handle == NULL, "OpenProcess(PROCESS_VM_READ) should have failed\n");
1887 handle = OpenProcess( PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId() );
1888 todo_wine
1889 ok(handle == NULL, "OpenProcess(PROCESS_ALL_ACCESS) should have failed\n");
1891 /* Documented privilege elevation */
1892 ok(DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
1893 &handle, 0, TRUE, DUPLICATE_SAME_ACCESS ),
1894 "duplicating handle err:%d\n", GetLastError());
1895 TEST_GRANTED_ACCESS2( handle, PROCESS_ALL_ACCESS,
1896 STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL );
1898 CloseHandle( handle );
1900 /* Same only explicitly asking for all access rights */
1901 ok(DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
1902 &handle, PROCESS_ALL_ACCESS, TRUE, 0 ),
1903 "duplicating handle err:%d\n", GetLastError());
1904 TEST_GRANTED_ACCESS2( handle, PROCESS_ALL_ACCESS,
1905 PROCESS_ALL_ACCESS | PROCESS_QUERY_LIMITED_INFORMATION );
1906 ok(DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
1907 &handle1, PROCESS_VM_READ, TRUE, 0 ),
1908 "duplicating handle err:%d\n", GetLastError());
1909 TEST_GRANTED_ACCESS( handle1, PROCESS_VM_READ );
1910 CloseHandle( handle1 );
1911 CloseHandle( handle );
1914 static void test_impersonation_level(void)
1916 HANDLE Token, ProcessToken;
1917 HANDLE Token2;
1918 DWORD Size;
1919 TOKEN_PRIVILEGES *Privileges;
1920 TOKEN_USER *User;
1921 PRIVILEGE_SET *PrivilegeSet;
1922 BOOL AccessGranted;
1923 BOOL ret;
1924 HKEY hkey;
1925 DWORD error;
1927 pDuplicateTokenEx = (fnDuplicateTokenEx) GetProcAddress(hmod, "DuplicateTokenEx");
1928 if( !pDuplicateTokenEx ) {
1929 skip("DuplicateTokenEx is not available\n");
1930 return;
1932 SetLastError(0xdeadbeef);
1933 ret = ImpersonateSelf(SecurityAnonymous);
1934 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1936 skip("ImpersonateSelf is not implemented\n");
1937 return;
1939 ok(ret, "ImpersonateSelf(SecurityAnonymous) failed with error %d\n", GetLastError());
1940 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
1941 ok(!ret, "OpenThreadToken should have failed\n");
1942 error = GetLastError();
1943 ok(error == ERROR_CANT_OPEN_ANONYMOUS, "OpenThreadToken on anonymous token should have returned ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error);
1944 /* can't perform access check when opening object against an anonymous impersonation token */
1945 todo_wine {
1946 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
1947 ok(error == ERROR_INVALID_HANDLE, "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE instead of %d\n", error);
1949 RevertToSelf();
1951 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, &ProcessToken);
1952 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1954 ret = pDuplicateTokenEx(ProcessToken,
1955 TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_IMPERSONATE, NULL,
1956 SecurityAnonymous, TokenImpersonation, &Token);
1957 ok(ret, "DuplicateTokenEx failed with error %d\n", GetLastError());
1958 /* can't increase the impersonation level */
1959 ret = DuplicateToken(Token, SecurityIdentification, &Token2);
1960 error = GetLastError();
1961 ok(!ret && error == ERROR_BAD_IMPERSONATION_LEVEL,
1962 "Duplicating a token and increasing the impersonation level should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
1963 /* we can query anything from an anonymous token, including the user */
1964 ret = GetTokenInformation(Token, TokenUser, NULL, 0, &Size);
1965 error = GetLastError();
1966 ok(!ret && error == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenUser) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error);
1967 User = HeapAlloc(GetProcessHeap(), 0, Size);
1968 ret = GetTokenInformation(Token, TokenUser, User, Size, &Size);
1969 ok(ret, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1970 HeapFree(GetProcessHeap(), 0, User);
1972 /* PrivilegeCheck fails with SecurityAnonymous level */
1973 ret = GetTokenInformation(Token, TokenPrivileges, NULL, 0, &Size);
1974 error = GetLastError();
1975 ok(!ret && error == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenPrivileges) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error);
1976 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
1977 ret = GetTokenInformation(Token, TokenPrivileges, Privileges, Size, &Size);
1978 ok(ret, "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1980 PrivilegeSet = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PRIVILEGE_SET, Privilege[Privileges->PrivilegeCount]));
1981 PrivilegeSet->PrivilegeCount = Privileges->PrivilegeCount;
1982 memcpy(PrivilegeSet->Privilege, Privileges->Privileges, PrivilegeSet->PrivilegeCount * sizeof(PrivilegeSet->Privilege[0]));
1983 PrivilegeSet->Control = PRIVILEGE_SET_ALL_NECESSARY;
1984 HeapFree(GetProcessHeap(), 0, Privileges);
1986 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
1987 error = GetLastError();
1988 ok(!ret && error == ERROR_BAD_IMPERSONATION_LEVEL, "PrivilegeCheck for SecurityAnonymous token should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
1990 CloseHandle(Token);
1992 ret = ImpersonateSelf(SecurityIdentification);
1993 ok(ret, "ImpersonateSelf(SecurityIdentification) failed with error %d\n", GetLastError());
1994 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
1995 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
1997 /* can't perform access check when opening object against an identification impersonation token */
1998 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
1999 todo_wine {
2000 ok(error == ERROR_INVALID_HANDLE, "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE instead of %d\n", error);
2002 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2003 ok(ret, "PrivilegeCheck for SecurityIdentification failed with error %d\n", GetLastError());
2004 CloseHandle(Token);
2005 RevertToSelf();
2007 ret = ImpersonateSelf(SecurityImpersonation);
2008 ok(ret, "ImpersonateSelf(SecurityImpersonation) failed with error %d\n", GetLastError());
2009 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
2010 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
2011 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
2012 ok(error == ERROR_SUCCESS, "RegOpenKeyEx should have succeeded instead of failing with %d\n", error);
2013 RegCloseKey(hkey);
2014 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2015 ok(ret, "PrivilegeCheck for SecurityImpersonation failed with error %d\n", GetLastError());
2016 RevertToSelf();
2018 CloseHandle(Token);
2019 CloseHandle(ProcessToken);
2021 HeapFree(GetProcessHeap(), 0, PrivilegeSet);
2024 static void test_SetEntriesInAcl(void)
2026 DWORD res;
2027 PSID EveryoneSid = NULL, UsersSid = NULL;
2028 PACL OldAcl = NULL, NewAcl;
2029 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
2030 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
2031 EXPLICIT_ACCESSW ExplicitAccess;
2032 static const WCHAR wszEveryone[] = {'E','v','e','r','y','o','n','e',0};
2034 if (!pSetEntriesInAclW)
2036 skip("SetEntriesInAclW is not available\n");
2037 return;
2040 NewAcl = (PACL)0xdeadbeef;
2041 res = pSetEntriesInAclW(0, NULL, NULL, &NewAcl);
2042 if(res == ERROR_CALL_NOT_IMPLEMENTED)
2044 skip("SetEntriesInAclW is not implemented\n");
2045 return;
2047 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2048 ok(NewAcl == NULL, "NewAcl=%p, expected NULL\n", NewAcl);
2050 OldAcl = HeapAlloc(GetProcessHeap(), 0, 256);
2051 res = InitializeAcl(OldAcl, 256, ACL_REVISION);
2052 if(!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2054 skip("ACLs not implemented - skipping tests\n");
2055 HeapFree(GetProcessHeap(), 0, OldAcl);
2056 return;
2058 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
2060 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
2061 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2063 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
2064 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &UsersSid);
2065 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2067 res = AddAccessAllowedAce(OldAcl, ACL_REVISION, KEY_READ, UsersSid);
2068 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2070 ExplicitAccess.grfAccessPermissions = KEY_WRITE;
2071 ExplicitAccess.grfAccessMode = GRANT_ACCESS;
2072 ExplicitAccess.grfInheritance = NO_INHERITANCE;
2073 ExplicitAccess.Trustee.pMultipleTrustee = NULL;
2074 ExplicitAccess.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2075 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
2076 ExplicitAccess.Trustee.TrusteeType = TRUSTEE_IS_UNKNOWN;
2077 ExplicitAccess.Trustee.ptstrName = (LPWSTR)EveryoneSid;
2078 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2079 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2080 ok(NewAcl != NULL, "returned acl was NULL\n");
2081 LocalFree(NewAcl);
2083 if (PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale())) != LANG_ENGLISH)
2085 skip("Non-english locale (test with hardcoded 'Everyone')\n");
2087 else
2089 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_USER;
2090 ExplicitAccess.Trustee.ptstrName = (LPWSTR)wszEveryone;
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 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_BAD_FORM;
2097 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2098 ok(res == ERROR_INVALID_PARAMETER, "SetEntriesInAclW failed: %u\n", res);
2099 ok(NewAcl == NULL, "returned acl wasn't NULL: %p\n", NewAcl);
2100 LocalFree(NewAcl);
2102 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_USER;
2103 ExplicitAccess.Trustee.MultipleTrusteeOperation = TRUSTEE_IS_IMPERSONATE;
2104 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2105 ok(res == ERROR_INVALID_PARAMETER, "SetEntriesInAclW failed: %u\n", res);
2106 ok(NewAcl == NULL, "returned acl wasn't NULL: %p\n", NewAcl);
2107 LocalFree(NewAcl);
2109 ExplicitAccess.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2110 ExplicitAccess.grfAccessMode = SET_ACCESS;
2111 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2112 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2113 ok(NewAcl != NULL, "returned acl was NULL\n");
2114 LocalFree(NewAcl);
2117 ExplicitAccess.grfAccessMode = REVOKE_ACCESS;
2118 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
2119 ExplicitAccess.Trustee.ptstrName = (LPWSTR)UsersSid;
2120 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2121 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2122 ok(NewAcl != NULL, "returned acl was NULL\n");
2123 LocalFree(NewAcl);
2125 LocalFree(UsersSid);
2126 LocalFree(EveryoneSid);
2127 HeapFree(GetProcessHeap(), 0, OldAcl);
2130 static void test_GetNamedSecurityInfoA(void)
2132 PSECURITY_DESCRIPTOR pSecDesc;
2133 DWORD revision;
2134 SECURITY_DESCRIPTOR_CONTROL control;
2135 PSID owner;
2136 PSID group;
2137 BOOL owner_defaulted;
2138 BOOL group_defaulted;
2139 DWORD error;
2140 BOOL ret;
2141 CHAR windows_dir[MAX_PATH];
2143 if (!pGetNamedSecurityInfoA)
2145 skip("GetNamedSecurityInfoA is not available\n");
2146 return;
2149 ret = GetWindowsDirectoryA(windows_dir, MAX_PATH);
2150 ok(ret, "GetWindowsDirectory failed with error %d\n", GetLastError());
2152 SetLastError(0xdeadbeef);
2153 error = pGetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,
2154 OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION,
2155 NULL, NULL, NULL, NULL, &pSecDesc);
2156 if (error != ERROR_SUCCESS && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2158 skip("GetNamedSecurityInfoA is not implemented\n");
2159 return;
2161 ok(!error, "GetNamedSecurityInfo failed with error %d\n", error);
2163 ret = GetSecurityDescriptorControl(pSecDesc, &control, &revision);
2164 ok(ret, "GetSecurityDescriptorControl failed with error %d\n", GetLastError());
2165 ok((control & (SE_SELF_RELATIVE|SE_DACL_PRESENT)) == (SE_SELF_RELATIVE|SE_DACL_PRESENT),
2166 "control (0x%x) doesn't have (SE_SELF_RELATIVE|SE_DACL_PRESENT) flags set\n", control);
2167 ok(revision == SECURITY_DESCRIPTOR_REVISION1, "revision was %d instead of 1\n", revision);
2168 ret = GetSecurityDescriptorOwner(pSecDesc, &owner, &owner_defaulted);
2169 ok(ret, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
2170 ok(owner != NULL, "owner should not be NULL\n");
2171 ret = GetSecurityDescriptorGroup(pSecDesc, &group, &group_defaulted);
2172 ok(ret, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2173 ok(group != NULL, "group should not be NULL\n");
2176 static void test_ConvertStringSecurityDescriptor(void)
2178 BOOL ret;
2179 PSECURITY_DESCRIPTOR pSD;
2180 static const WCHAR Blank[] = { 0 };
2182 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA)
2184 skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
2185 return;
2188 SetLastError(0xdeadbeef);
2189 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2190 "D:(A;;GA;;;WD)", 0xdeadbeef, &pSD, NULL);
2191 ok(!ret && GetLastError() == ERROR_UNKNOWN_REVISION,
2192 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_UNKNOWN_REVISION instead of %d\n",
2193 GetLastError());
2195 /* test ACE string type */
2196 SetLastError(0xdeadbeef);
2197 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2198 "D:(A;;GA;;;WD)", SDDL_REVISION_1, &pSD, NULL);
2199 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2200 LocalFree(pSD);
2202 SetLastError(0xdeadbeef);
2203 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2204 "D:(D;;GA;;;WD)", SDDL_REVISION_1, &pSD, NULL);
2205 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2206 LocalFree(pSD);
2208 SetLastError(0xdeadbeef);
2209 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2210 "ERROR:(D;;GA;;;WD)", SDDL_REVISION_1, &pSD, NULL);
2211 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2212 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2213 GetLastError());
2215 /* test ACE string access rights */
2216 SetLastError(0xdeadbeef);
2217 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2218 "D:(A;;GA;;;WD)", SDDL_REVISION_1, &pSD, NULL);
2219 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2220 LocalFree(pSD);
2221 SetLastError(0xdeadbeef);
2222 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2223 "D:(A;;GRGWGX;;;WD)", SDDL_REVISION_1, &pSD, NULL);
2224 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2225 LocalFree(pSD);
2226 SetLastError(0xdeadbeef);
2227 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2228 "D:(A;;RCSDWDWO;;;WD)", SDDL_REVISION_1, &pSD, NULL);
2229 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2230 LocalFree(pSD);
2231 SetLastError(0xdeadbeef);
2232 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2233 "D:(A;;RPWPCCDCLCSWLODTCR;;;WD)", SDDL_REVISION_1, &pSD, NULL);
2234 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2235 LocalFree(pSD);
2236 SetLastError(0xdeadbeef);
2237 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2238 "D:(A;;FAFRFWFX;;;WD)", SDDL_REVISION_1, &pSD, NULL);
2239 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2240 LocalFree(pSD);
2241 SetLastError(0xdeadbeef);
2242 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2243 "D:(A;;KAKRKWKX;;;WD)", SDDL_REVISION_1, &pSD, NULL);
2244 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2245 LocalFree(pSD);
2246 SetLastError(0xdeadbeef);
2247 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2248 "D:(A;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1, &pSD, NULL);
2249 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2250 LocalFree(pSD);
2251 SetLastError(0xdeadbeef);
2252 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2253 "S:(AU;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1, &pSD, NULL);
2254 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2255 LocalFree(pSD);
2257 /* test ACE string access right error case */
2258 SetLastError(0xdeadbeef);
2259 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2260 "D:(A;;ROB;;;WD)", SDDL_REVISION_1, &pSD, NULL);
2261 ok(!ret && GetLastError() == ERROR_INVALID_ACL,
2262 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_ACL instead of %d\n",
2263 GetLastError());
2265 /* test behaviour with NULL parameters */
2266 SetLastError(0xdeadbeef);
2267 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2268 NULL, 0xdeadbeef, &pSD, NULL);
2269 todo_wine
2270 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2271 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2272 GetLastError());
2274 SetLastError(0xdeadbeef);
2275 ret = pConvertStringSecurityDescriptorToSecurityDescriptorW(
2276 NULL, 0xdeadbeef, &pSD, NULL);
2277 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2278 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2279 GetLastError());
2281 SetLastError(0xdeadbeef);
2282 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2283 "D:(A;;ROB;;;WD)", 0xdeadbeef, NULL, NULL);
2284 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2285 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2286 GetLastError());
2288 SetLastError(0xdeadbeef);
2289 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2290 "D:(A;;ROB;;;WD)", SDDL_REVISION_1, NULL, NULL);
2291 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2292 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2293 GetLastError());
2295 /* test behaviour with empty strings */
2296 SetLastError(0xdeadbeef);
2297 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2298 "", SDDL_REVISION_1, &pSD, NULL);
2299 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2301 SetLastError(0xdeadbeef);
2302 ret = pConvertStringSecurityDescriptorToSecurityDescriptorW(
2303 Blank, SDDL_REVISION_1, &pSD, NULL);
2304 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2306 /* test ACE string SID */
2307 SetLastError(0xdeadbeef);
2308 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2309 "D:(D;;GA;;;S-1-0-0)", SDDL_REVISION_1, &pSD, NULL);
2310 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2311 LocalFree(pSD);
2313 SetLastError(0xdeadbeef);
2314 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2315 "D:(D;;GA;;;Nonexistent account)", SDDL_REVISION_1, &pSD, NULL);
2316 ok(!ret, "Expected failure, got %d\n", ret);
2317 ok(GetLastError() == ERROR_INVALID_ACL || GetLastError() == ERROR_INVALID_SID,
2318 "Expected ERROR_INVALID_ACL or ERROR_INVALID_SID, got %d\n", GetLastError());
2321 static void test_ConvertSecurityDescriptorToString()
2323 SECURITY_DESCRIPTOR desc;
2324 SECURITY_INFORMATION sec_info = OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION|SACL_SECURITY_INFORMATION;
2325 LPSTR string;
2326 DWORD size;
2327 PSID psid, psid2;
2328 PACL pacl;
2329 char sid_buf[256];
2330 char acl_buf[8192];
2331 ULONG len;
2333 if (!pConvertSecurityDescriptorToStringSecurityDescriptorA)
2335 skip("ConvertSecurityDescriptorToStringSecurityDescriptor is not available\n");
2336 return;
2338 if (!pCreateWellKnownSid)
2340 skip("CreateWellKnownSid is not available\n");
2341 return;
2344 /* It seems Windows XP adds an extra character to the length of the string for each ACE in an ACL. We
2345 * don't replicate this feature so we only test len >= strlen+1. */
2346 #define CHECK_RESULT_AND_FREE(exp_str) \
2347 ok(strcmp(string, (exp_str)) == 0, "String mismatch (expected \"%s\", got \"%s\")\n", (exp_str), string); \
2348 ok(len >= (lstrlen(exp_str) + 1), "Length mismatch (expected %d, got %d)\n", lstrlen(exp_str) + 1, len); \
2349 LocalFree(string);
2351 #define CHECK_ONE_OF_AND_FREE(exp_str1, exp_str2) \
2352 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); \
2353 ok(len >= (strlen(string) + 1), "Length mismatch (expected %d, got %d)\n", lstrlen(string) + 1, len); \
2354 LocalFree(string);
2356 InitializeSecurityDescriptor(&desc, SECURITY_DESCRIPTOR_REVISION);
2357 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2358 CHECK_RESULT_AND_FREE("");
2360 size = 4096;
2361 pCreateWellKnownSid(WinLocalSid, NULL, sid_buf, &size);
2362 SetSecurityDescriptorOwner(&desc, (PSID)sid_buf, FALSE);
2363 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2364 CHECK_RESULT_AND_FREE("O:S-1-2-0");
2366 SetSecurityDescriptorOwner(&desc, (PSID)sid_buf, TRUE);
2367 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2368 CHECK_RESULT_AND_FREE("O:S-1-2-0");
2370 size = sizeof(sid_buf);
2371 pCreateWellKnownSid(WinLocalSystemSid, NULL, sid_buf, &size);
2372 SetSecurityDescriptorOwner(&desc, (PSID)sid_buf, TRUE);
2373 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2374 CHECK_RESULT_AND_FREE("O:SY");
2376 pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid);
2377 SetSecurityDescriptorGroup(&desc, psid, TRUE);
2378 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2379 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576");
2381 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, GROUP_SECURITY_INFORMATION, &string, &len), "Conversion failed\n");
2382 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
2384 pacl = (PACL)acl_buf;
2385 InitializeAcl(pacl, sizeof(acl_buf), ACL_REVISION);
2386 SetSecurityDescriptorDacl(&desc, TRUE, pacl, TRUE);
2387 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2388 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
2390 SetSecurityDescriptorDacl(&desc, TRUE, pacl, FALSE);
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-4576D:");
2394 pConvertStringSidToSidA("S-1-5-6", &psid2);
2395 pAddAccessAllowedAceEx(pacl, ACL_REVISION, NO_PROPAGATE_INHERIT_ACE, 0xf0000000, psid2);
2396 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2397 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)");
2399 pAddAccessAllowedAceEx(pacl, ACL_REVISION, INHERIT_ONLY_ACE|INHERITED_ACE, 0x00000003, psid2);
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:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)");
2403 pAddAccessDeniedAceEx(pacl, ACL_REVISION, OBJECT_INHERIT_ACE|CONTAINER_INHERIT_ACE, 0xffffffff, psid);
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:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)");
2408 pacl = (PACL)acl_buf;
2409 InitializeAcl(pacl, sizeof(acl_buf), ACL_REVISION);
2410 SetSecurityDescriptorSacl(&desc, TRUE, pacl, FALSE);
2411 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2412 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:");
2414 /* fails in win2k */
2415 SetSecurityDescriptorDacl(&desc, TRUE, NULL, FALSE);
2416 pAddAuditAccessAceEx(pacl, ACL_REVISION, VALID_INHERIT_FLAGS, KEY_READ|KEY_WRITE, psid2, TRUE, TRUE);
2417 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len))
2419 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)", /* XP */
2420 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)" /* Vista */);
2423 /* fails in win2k */
2424 pAddAuditAccessAceEx(pacl, ACL_REVISION, NO_PROPAGATE_INHERIT_ACE, FILE_GENERIC_READ|FILE_GENERIC_WRITE, psid2, TRUE, FALSE);
2425 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len))
2427 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", /* XP */
2428 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)" /* Vista */);
2432 static void test_SetSecurityDescriptorControl (PSECURITY_DESCRIPTOR sec)
2434 SECURITY_DESCRIPTOR_CONTROL ref;
2435 SECURITY_DESCRIPTOR_CONTROL test;
2437 SECURITY_DESCRIPTOR_CONTROL const mutable
2438 = SE_DACL_AUTO_INHERIT_REQ | SE_SACL_AUTO_INHERIT_REQ
2439 | SE_DACL_AUTO_INHERITED | SE_SACL_AUTO_INHERITED
2440 | SE_DACL_PROTECTED | SE_SACL_PROTECTED
2441 | 0x00000040 | 0x00000080 /* not defined in winnt.h */
2443 SECURITY_DESCRIPTOR_CONTROL const immutable
2444 = SE_OWNER_DEFAULTED | SE_GROUP_DEFAULTED
2445 | SE_DACL_PRESENT | SE_DACL_DEFAULTED
2446 | SE_SACL_PRESENT | SE_SACL_DEFAULTED
2447 | SE_RM_CONTROL_VALID | SE_SELF_RELATIVE
2450 int bit;
2451 DWORD dwRevision;
2452 LPCSTR fmt = "Expected error %s, got %u\n";
2454 GetSecurityDescriptorControl (sec, &ref, &dwRevision);
2456 /* The mutable bits are mutable regardless of the truth of
2457 SE_DACL_PRESENT and/or SE_SACL_PRESENT */
2459 /* Check call barfs if any bit-of-interest is immutable */
2460 for (bit = 0; bit < 16; ++bit)
2462 SECURITY_DESCRIPTOR_CONTROL const bitOfInterest = 1 << bit;
2463 SECURITY_DESCRIPTOR_CONTROL setOrClear = ref & bitOfInterest;
2465 SECURITY_DESCRIPTOR_CONTROL ctrl;
2467 DWORD dwExpect = (bitOfInterest & immutable)
2468 ? ERROR_INVALID_PARAMETER : 0xbebecaca;
2469 LPCSTR strExpect = (bitOfInterest & immutable)
2470 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
2472 ctrl = (bitOfInterest & mutable) ? ref + bitOfInterest : ref;
2473 setOrClear ^= bitOfInterest;
2474 SetLastError (0xbebecaca);
2475 pSetSecurityDescriptorControl (sec, bitOfInterest, setOrClear);
2476 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
2477 GetSecurityDescriptorControl(sec, &test, &dwRevision);
2478 expect_eq(test, ctrl, int, "%x");
2480 ctrl = ref;
2481 setOrClear ^= bitOfInterest;
2482 SetLastError (0xbebecaca);
2483 pSetSecurityDescriptorControl (sec, bitOfInterest, setOrClear);
2484 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
2485 GetSecurityDescriptorControl (sec, &test, &dwRevision);
2486 expect_eq(test, ref, int, "%x");
2489 /* Check call barfs if any bit-to-set is immutable
2490 even when not a bit-of-interest */
2491 for (bit = 0; bit < 16; ++bit)
2493 SECURITY_DESCRIPTOR_CONTROL const bitsOfInterest = mutable;
2494 SECURITY_DESCRIPTOR_CONTROL setOrClear = ref & bitsOfInterest;
2496 SECURITY_DESCRIPTOR_CONTROL ctrl;
2498 DWORD dwExpect = ((1 << bit) & immutable)
2499 ? ERROR_INVALID_PARAMETER : 0xbebecaca;
2500 LPCSTR strExpect = ((1 << bit) & immutable)
2501 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
2503 ctrl = ((1 << bit) & immutable) ? test : ref | mutable;
2504 setOrClear ^= bitsOfInterest;
2505 SetLastError (0xbebecaca);
2506 pSetSecurityDescriptorControl (sec, bitsOfInterest, setOrClear | (1 << bit));
2507 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
2508 GetSecurityDescriptorControl(sec, &test, &dwRevision);
2509 expect_eq(test, ctrl, int, "%x");
2511 ctrl = ((1 << bit) & immutable) ? test : ref | (1 << bit);
2512 setOrClear ^= bitsOfInterest;
2513 SetLastError (0xbebecaca);
2514 pSetSecurityDescriptorControl (sec, bitsOfInterest, setOrClear | (1 << bit));
2515 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
2516 GetSecurityDescriptorControl(sec, &test, &dwRevision);
2517 expect_eq(test, ctrl, int, "%x");
2521 static void test_PrivateObjectSecurity(void)
2523 SECURITY_INFORMATION sec_info = OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION|SACL_SECURITY_INFORMATION;
2524 SECURITY_DESCRIPTOR_CONTROL ctrl;
2525 PSECURITY_DESCRIPTOR sec;
2526 DWORD dwDescSize;
2527 DWORD dwRevision;
2528 DWORD retSize;
2529 LPSTR string;
2530 ULONG len;
2531 PSECURITY_DESCRIPTOR buf;
2533 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA)
2535 skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
2536 return;
2539 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
2540 "O:SY"
2541 "G:S-1-5-21-93476-23408-4576"
2542 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)"
2543 "(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
2544 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
2545 SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
2547 test_SetSecurityDescriptorControl(sec);
2549 LocalFree(sec);
2551 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
2552 "O:SY"
2553 "G:S-1-5-21-93476-23408-4576",
2554 SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
2556 test_SetSecurityDescriptorControl(sec);
2558 LocalFree(sec);
2560 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
2561 "O:SY"
2562 "G:S-1-5-21-93476-23408-4576"
2563 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
2564 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
2565 buf = HeapAlloc(GetProcessHeap(), 0, dwDescSize);
2566 pSetSecurityDescriptorControl(sec, SE_DACL_PROTECTED, SE_DACL_PROTECTED);
2567 GetSecurityDescriptorControl(sec, &ctrl, &dwRevision);
2568 expect_eq(ctrl, 0x9014, int, "%x");
2570 ok(GetPrivateObjectSecurity(sec, GROUP_SECURITY_INFORMATION, buf, dwDescSize, &retSize),
2571 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
2572 ok(retSize <= dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
2573 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2574 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
2575 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
2576 expect_eq(ctrl, 0x8000, int, "%x");
2578 ok(GetPrivateObjectSecurity(sec, GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION, buf, dwDescSize, &retSize),
2579 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
2580 ok(retSize <= dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
2581 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed err=%u\n", GetLastError());
2582 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)");
2583 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
2584 expect_eq(ctrl, 0x8004, int, "%x");
2586 ok(GetPrivateObjectSecurity(sec, sec_info, buf, dwDescSize, &retSize),
2587 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
2588 ok(retSize == dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
2589 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2590 CHECK_RESULT_AND_FREE("O:SY"
2591 "G:S-1-5-21-93476-23408-4576"
2592 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
2593 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)");
2594 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
2595 expect_eq(ctrl, 0x8014, int, "%x");
2597 SetLastError(0xdeadbeef);
2598 ok(GetPrivateObjectSecurity(sec, sec_info, buf, 5, &retSize) == FALSE, "GetPrivateObjectSecurity should have failed\n");
2599 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected error ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
2601 LocalFree(sec);
2602 HeapFree(GetProcessHeap(), 0, buf);
2604 #undef CHECK_RESULT_AND_FREE
2605 #undef CHECK_ONE_OF_AND_FREE
2607 static void test_acls(void)
2609 char buffer[256];
2610 PACL pAcl = (PACL)buffer;
2611 BOOL ret;
2613 SetLastError(0xdeadbeef);
2614 ret = InitializeAcl(pAcl, sizeof(ACL) - 1, ACL_REVISION);
2615 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2617 skip("InitializeAcl is not implemented\n");
2618 return;
2621 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InitializeAcl with too small a buffer should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
2623 SetLastError(0xdeadbeef);
2624 ret = InitializeAcl(pAcl, 0xffffffff, ACL_REVISION);
2625 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "InitializeAcl with too large a buffer should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
2627 SetLastError(0xdeadbeef);
2628 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION1);
2629 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "InitializeAcl(ACL_REVISION1) should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
2631 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION2);
2632 ok(ret, "InitializeAcl(ACL_REVISION2) failed with error %d\n", GetLastError());
2634 ret = IsValidAcl(pAcl);
2635 ok(ret, "IsValidAcl failed with error %d\n", GetLastError());
2637 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION3);
2638 ok(ret, "InitializeAcl(ACL_REVISION3) failed with error %d\n", GetLastError());
2640 ret = IsValidAcl(pAcl);
2641 ok(ret, "IsValidAcl failed with error %d\n", GetLastError());
2643 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION4);
2644 ok(ret, "InitializeAcl(ACL_REVISION4) failed with error %d\n", GetLastError());
2646 ret = IsValidAcl(pAcl);
2647 ok(ret, "IsValidAcl failed with error %d\n", GetLastError());
2649 SetLastError(0xdeadbeef);
2650 ret = InitializeAcl(pAcl, sizeof(buffer), -1);
2651 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "InitializeAcl(-1) failed with error %d\n", GetLastError());
2654 static void test_GetSecurityInfo(void)
2656 HANDLE obj;
2657 PSECURITY_DESCRIPTOR sd;
2658 PSID owner, group;
2659 PACL dacl;
2660 DWORD ret;
2662 if (!pGetSecurityInfo)
2664 win_skip("GetSecurityInfo is not available\n");
2665 return;
2668 /* Create something. Files have lots of associated security info. */
2669 obj = CreateFile(myARGV[0], GENERIC_READ, FILE_SHARE_READ, NULL,
2670 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2671 if (obj == INVALID_HANDLE_VALUE)
2673 skip("Couldn't create an object for GetSecurityInfo test\n");
2674 return;
2677 ret = pGetSecurityInfo(obj, SE_FILE_OBJECT,
2678 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
2679 &owner, &group, &dacl, NULL, &sd);
2680 if (ret == ERROR_CALL_NOT_IMPLEMENTED)
2682 win_skip("GetSecurityInfo is not implemented\n");
2683 CloseHandle(obj);
2684 return;
2686 ok(ret == ERROR_SUCCESS, "GetSecurityInfo returned %d\n", ret);
2687 ok(sd != NULL, "GetSecurityInfo\n");
2688 ok(owner != NULL, "GetSecurityInfo\n");
2689 ok(group != NULL, "GetSecurityInfo\n");
2690 ok(dacl != NULL, "GetSecurityInfo\n");
2691 ok(IsValidAcl(dacl), "GetSecurityInfo\n");
2693 LocalFree(sd);
2695 /* If we don't ask for the security descriptor, Windows will still give us
2696 the other stuff, leaving us no way to free it. */
2697 ret = pGetSecurityInfo(obj, SE_FILE_OBJECT,
2698 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
2699 &owner, &group, &dacl, NULL, NULL);
2700 ok(ret == ERROR_SUCCESS, "GetSecurityInfo returned %d\n", ret);
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 CloseHandle(obj);
2709 START_TEST(security)
2711 init();
2712 if (!hmod) return;
2714 if (myARGC >= 3)
2716 test_process_security_child();
2717 return;
2719 test_sid();
2720 test_trustee();
2721 test_luid();
2722 test_CreateWellKnownSid();
2723 test_FileSecurity();
2724 test_AccessCheck();
2725 test_token_attr();
2726 test_LookupAccountSid();
2727 test_LookupAccountName();
2728 test_security_descriptor();
2729 test_process_security();
2730 test_impersonation_level();
2731 test_SetEntriesInAcl();
2732 test_GetNamedSecurityInfoA();
2733 test_ConvertStringSecurityDescriptor();
2734 test_ConvertSecurityDescriptorToString();
2735 test_PrivateObjectSecurity();
2736 test_acls();
2737 test_GetSecurityInfo();