Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / advapi32 / tests / security.c
blobedfecb9f499e8298e2069001d1c6f0cc80aec675
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 static VOID (WINAPI *pBuildTrusteeWithSidA)( PTRUSTEEA pTrustee, PSID pSid );
66 static VOID (WINAPI *pBuildTrusteeWithNameA)( PTRUSTEEA pTrustee, LPSTR pName );
67 static VOID (WINAPI *pBuildTrusteeWithObjectsAndNameA)( PTRUSTEEA pTrustee,
68 POBJECTS_AND_NAME_A pObjName,
69 SE_OBJECT_TYPE ObjectType,
70 LPSTR ObjectTypeName,
71 LPSTR InheritedObjectTypeName,
72 LPSTR Name );
73 static VOID (WINAPI *pBuildTrusteeWithObjectsAndSidA)( PTRUSTEEA pTrustee,
74 POBJECTS_AND_SID pObjSid,
75 GUID* pObjectGuid,
76 GUID* pInheritedObjectGuid,
77 PSID pSid );
78 static LPSTR (WINAPI *pGetTrusteeNameA)( PTRUSTEEA pTrustee );
79 static BOOL (WINAPI *pMakeSelfRelativeSD)( PSECURITY_DESCRIPTOR, PSECURITY_DESCRIPTOR, LPDWORD );
80 static BOOL (WINAPI *pConvertSidToStringSidA)( PSID pSid, LPSTR *str );
81 static BOOL (WINAPI *pConvertStringSidToSidA)( LPCSTR str, PSID pSid );
82 static BOOL (WINAPI *pCheckTokenMembership)(HANDLE, PSID, PBOOL);
83 static BOOL (WINAPI *pConvertStringSecurityDescriptorToSecurityDescriptorA)(LPCSTR, DWORD,
84 PSECURITY_DESCRIPTOR*, PULONG );
85 static BOOL (WINAPI *pConvertStringSecurityDescriptorToSecurityDescriptorW)(LPCWSTR, DWORD,
86 PSECURITY_DESCRIPTOR*, PULONG );
87 static BOOL (WINAPI *pConvertSecurityDescriptorToStringSecurityDescriptorA)(PSECURITY_DESCRIPTOR, DWORD,
88 SECURITY_INFORMATION, LPSTR *, PULONG );
89 static BOOL (WINAPI *pGetFileSecurityA)(LPCSTR, SECURITY_INFORMATION,
90 PSECURITY_DESCRIPTOR, DWORD, LPDWORD);
91 static BOOL (WINAPI *pSetFileSecurityA)(LPCSTR, SECURITY_INFORMATION,
92 PSECURITY_DESCRIPTOR);
93 static DWORD (WINAPI *pGetNamedSecurityInfoA)(LPSTR, SE_OBJECT_TYPE, SECURITY_INFORMATION,
94 PSID*, PSID*, PACL*, PACL*,
95 PSECURITY_DESCRIPTOR*);
96 static PDWORD (WINAPI *pGetSidSubAuthority)(PSID, DWORD);
97 static PUCHAR (WINAPI *pGetSidSubAuthorityCount)(PSID);
98 static BOOL (WINAPI *pIsValidSid)(PSID);
99 static DWORD (WINAPI *pRtlAdjustPrivilege)(ULONG,BOOLEAN,BOOLEAN,PBOOLEAN);
100 static BOOL (WINAPI *pCreateWellKnownSid)(WELL_KNOWN_SID_TYPE,PSID,PSID,DWORD*);
101 static BOOL (WINAPI *pDuplicateTokenEx)(HANDLE,DWORD,LPSECURITY_ATTRIBUTES,
102 SECURITY_IMPERSONATION_LEVEL,TOKEN_TYPE,PHANDLE);
104 static NTSTATUS (WINAPI *pLsaQueryInformationPolicy)(LSA_HANDLE,POLICY_INFORMATION_CLASS,PVOID*);
105 static NTSTATUS (WINAPI *pLsaClose)(LSA_HANDLE);
106 static NTSTATUS (WINAPI *pLsaFreeMemory)(PVOID);
107 static NTSTATUS (WINAPI *pLsaOpenPolicy)(PLSA_UNICODE_STRING,PLSA_OBJECT_ATTRIBUTES,ACCESS_MASK,PLSA_HANDLE);
108 static NTSTATUS (WINAPI *pNtQueryObject)(HANDLE,OBJECT_INFORMATION_CLASS,PVOID,ULONG,PULONG);
109 static DWORD (WINAPI *pSetEntriesInAclW)(ULONG, PEXPLICIT_ACCESSW, PACL, PACL*);
110 static BOOL (WINAPI *pSetSecurityDescriptorControl)(PSECURITY_DESCRIPTOR, SECURITY_DESCRIPTOR_CONTROL,
111 SECURITY_DESCRIPTOR_CONTROL);
112 static DWORD (WINAPI *pGetSecurityInfo)(HANDLE, SE_OBJECT_TYPE, SECURITY_INFORMATION,
113 PSID*, PSID*, PACL*, PACL*, PSECURITY_DESCRIPTOR*);
114 static NTSTATUS (WINAPI *pNtAccessCheck)(PSECURITY_DESCRIPTOR, HANDLE, ACCESS_MASK, PGENERIC_MAPPING,
115 PPRIVILEGE_SET, PULONG, PULONG, NTSTATUS*);
117 static HMODULE hmod;
118 static int myARGC;
119 static char** myARGV;
121 struct sidRef
123 SID_IDENTIFIER_AUTHORITY auth;
124 const char *refStr;
127 static void init(void)
129 HMODULE hntdll;
131 hntdll = GetModuleHandleA("ntdll.dll");
132 pNtQueryObject = (void *)GetProcAddress( hntdll, "NtQueryObject" );
133 pNtAccessCheck = (void *)GetProcAddress( hntdll, "NtAccessCheck" );
135 hmod = GetModuleHandle("advapi32.dll");
136 pAddAccessAllowedAceEx = (void *)GetProcAddress(hmod, "AddAccessAllowedAceEx");
137 pAddAccessDeniedAceEx = (void *)GetProcAddress(hmod, "AddAccessDeniedAceEx");
138 pAddAuditAccessAceEx = (void *)GetProcAddress(hmod, "AddAuditAccessAceEx");
139 pCheckTokenMembership = (void *)GetProcAddress(hmod, "CheckTokenMembership");
140 pConvertStringSecurityDescriptorToSecurityDescriptorA =
141 (void *)GetProcAddress(hmod, "ConvertStringSecurityDescriptorToSecurityDescriptorA" );
142 pConvertStringSecurityDescriptorToSecurityDescriptorW =
143 (void *)GetProcAddress(hmod, "ConvertStringSecurityDescriptorToSecurityDescriptorW" );
144 pConvertSecurityDescriptorToStringSecurityDescriptorA =
145 (void *)GetProcAddress(hmod, "ConvertSecurityDescriptorToStringSecurityDescriptorA" );
146 pGetFileSecurityA = (void *)GetProcAddress(hmod, "GetFileSecurityA" );
147 pSetFileSecurityA = (void *)GetProcAddress(hmod, "SetFileSecurityA" );
148 pCreateWellKnownSid = (void *)GetProcAddress( hmod, "CreateWellKnownSid" );
149 pGetNamedSecurityInfoA = (void *)GetProcAddress(hmod, "GetNamedSecurityInfoA");
150 pGetSidSubAuthority = (void *)GetProcAddress(hmod, "GetSidSubAuthority");
151 pGetSidSubAuthorityCount = (void *)GetProcAddress(hmod, "GetSidSubAuthorityCount");
152 pIsValidSid = (void *)GetProcAddress(hmod, "IsValidSid");
153 pMakeSelfRelativeSD = (void *)GetProcAddress(hmod, "MakeSelfRelativeSD");
154 pSetEntriesInAclW = (void *)GetProcAddress(hmod, "SetEntriesInAclW");
155 pSetSecurityDescriptorControl = (void *)GetProcAddress(hmod, "SetSecurityDescriptorControl");
156 pGetSecurityInfo = (void *)GetProcAddress(hmod, "GetSecurityInfo");
158 myARGC = winetest_get_mainargs( &myARGV );
161 static void test_str_sid(const char *str_sid)
163 PSID psid;
164 char *temp;
166 if (pConvertStringSidToSidA(str_sid, &psid))
168 if (pConvertSidToStringSidA(psid, &temp))
170 trace(" %s: %s\n", str_sid, temp);
171 LocalFree(temp);
173 LocalFree(psid);
175 else
177 if (GetLastError() != ERROR_INVALID_SID)
178 trace(" %s: couldn't be converted, returned %d\n", str_sid, GetLastError());
179 else
180 trace(" %s: couldn't be converted\n", str_sid);
184 static void test_sid(void)
186 struct sidRef refs[] = {
187 { { {0x00,0x00,0x33,0x44,0x55,0x66} }, "S-1-860116326-1" },
188 { { {0x00,0x00,0x01,0x02,0x03,0x04} }, "S-1-16909060-1" },
189 { { {0x00,0x00,0x00,0x01,0x02,0x03} }, "S-1-66051-1" },
190 { { {0x00,0x00,0x00,0x00,0x01,0x02} }, "S-1-258-1" },
191 { { {0x00,0x00,0x00,0x00,0x00,0x02} }, "S-1-2-1" },
192 { { {0x00,0x00,0x00,0x00,0x00,0x0c} }, "S-1-12-1" },
194 const char noSubAuthStr[] = "S-1-5";
195 unsigned int i;
196 PSID psid = NULL;
197 SID *pisid;
198 BOOL r;
199 LPSTR str = NULL;
201 pConvertSidToStringSidA = (void *)GetProcAddress( hmod, "ConvertSidToStringSidA" );
202 if( !pConvertSidToStringSidA )
203 return;
204 pConvertStringSidToSidA = (void *)GetProcAddress( hmod, "ConvertStringSidToSidA" );
205 if( !pConvertStringSidToSidA )
206 return;
208 r = pConvertStringSidToSidA( NULL, NULL );
209 ok( !r, "expected failure with NULL parameters\n" );
210 if( GetLastError() == ERROR_CALL_NOT_IMPLEMENTED )
211 return;
212 ok( GetLastError() == ERROR_INVALID_PARAMETER,
213 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
214 GetLastError() );
216 r = pConvertStringSidToSidA( refs[0].refStr, NULL );
217 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
218 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
219 GetLastError() );
221 r = pConvertStringSidToSidA( NULL, &str );
222 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
223 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
224 GetLastError() );
226 r = pConvertStringSidToSidA( noSubAuthStr, &psid );
227 ok( !r,
228 "expected failure with no sub authorities\n" );
229 ok( GetLastError() == ERROR_INVALID_SID,
230 "expected GetLastError() is ERROR_INVALID_SID, got %d\n",
231 GetLastError() );
233 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid), "ConvertStringSidToSidA failed\n");
234 pisid = psid;
235 ok(pisid->SubAuthorityCount == 4, "Invalid sub authority count - expected 4, got %d\n", pisid->SubAuthorityCount);
236 ok(pisid->SubAuthority[0] == 21, "Invalid subauthority 0 - expceted 21, got %d\n", pisid->SubAuthority[0]);
237 ok(pisid->SubAuthority[3] == 4576, "Invalid subauthority 0 - expceted 4576, got %d\n", pisid->SubAuthority[3]);
238 LocalFree(str);
239 LocalFree(psid);
241 for( i = 0; i < sizeof(refs) / sizeof(refs[0]); i++ )
243 PISID pisid;
245 r = AllocateAndInitializeSid( &refs[i].auth, 1,1,0,0,0,0,0,0,0,
246 &psid );
247 ok( r, "failed to allocate sid\n" );
248 r = pConvertSidToStringSidA( psid, &str );
249 ok( r, "failed to convert sid\n" );
250 if (r)
252 ok( !strcmp( str, refs[i].refStr ),
253 "incorrect sid, expected %s, got %s\n", refs[i].refStr, str );
254 LocalFree( str );
256 if( psid )
257 FreeSid( psid );
259 r = pConvertStringSidToSidA( refs[i].refStr, &psid );
260 ok( r, "failed to parse sid string\n" );
261 pisid = psid;
262 ok( pisid &&
263 !memcmp( pisid->IdentifierAuthority.Value, refs[i].auth.Value,
264 sizeof(refs[i].auth) ),
265 "string sid %s didn't parse to expected value\n"
266 "(got 0x%04x%08x, expected 0x%04x%08x)\n",
267 refs[i].refStr,
268 MAKEWORD( pisid->IdentifierAuthority.Value[1],
269 pisid->IdentifierAuthority.Value[0] ),
270 MAKELONG( MAKEWORD( pisid->IdentifierAuthority.Value[5],
271 pisid->IdentifierAuthority.Value[4] ),
272 MAKEWORD( pisid->IdentifierAuthority.Value[3],
273 pisid->IdentifierAuthority.Value[2] ) ),
274 MAKEWORD( refs[i].auth.Value[1], refs[i].auth.Value[0] ),
275 MAKELONG( MAKEWORD( refs[i].auth.Value[5], refs[i].auth.Value[4] ),
276 MAKEWORD( refs[i].auth.Value[3], refs[i].auth.Value[2] ) ) );
277 if( psid )
278 LocalFree( psid );
281 trace("String SIDs:\n");
282 test_str_sid("AO");
283 test_str_sid("RU");
284 test_str_sid("AN");
285 test_str_sid("AU");
286 test_str_sid("BA");
287 test_str_sid("BG");
288 test_str_sid("BO");
289 test_str_sid("BU");
290 test_str_sid("CA");
291 test_str_sid("CG");
292 test_str_sid("CO");
293 test_str_sid("DA");
294 test_str_sid("DC");
295 test_str_sid("DD");
296 test_str_sid("DG");
297 test_str_sid("DU");
298 test_str_sid("EA");
299 test_str_sid("ED");
300 test_str_sid("WD");
301 test_str_sid("PA");
302 test_str_sid("IU");
303 test_str_sid("LA");
304 test_str_sid("LG");
305 test_str_sid("LS");
306 test_str_sid("SY");
307 test_str_sid("NU");
308 test_str_sid("NO");
309 test_str_sid("NS");
310 test_str_sid("PO");
311 test_str_sid("PS");
312 test_str_sid("PU");
313 test_str_sid("RS");
314 test_str_sid("RD");
315 test_str_sid("RE");
316 test_str_sid("RC");
317 test_str_sid("SA");
318 test_str_sid("SO");
319 test_str_sid("SU");
322 static void test_trustee(void)
324 GUID ObjectType = {0x12345678, 0x1234, 0x5678, {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}};
325 GUID InheritedObjectType = {0x23456789, 0x2345, 0x6786, {0x2, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}};
326 GUID ZeroGuid;
327 OBJECTS_AND_NAME_ oan;
328 OBJECTS_AND_SID oas;
329 TRUSTEE trustee;
330 PSID psid;
331 char szObjectTypeName[] = "ObjectTypeName";
332 char szInheritedObjectTypeName[] = "InheritedObjectTypeName";
333 char szTrusteeName[] = "szTrusteeName";
334 SID_IDENTIFIER_AUTHORITY auth = { {0x11,0x22,0,0,0, 0} };
336 memset( &ZeroGuid, 0x00, sizeof (ZeroGuid) );
338 pBuildTrusteeWithSidA = (void *)GetProcAddress( hmod, "BuildTrusteeWithSidA" );
339 pBuildTrusteeWithNameA = (void *)GetProcAddress( hmod, "BuildTrusteeWithNameA" );
340 pBuildTrusteeWithObjectsAndNameA = (void *)GetProcAddress (hmod, "BuildTrusteeWithObjectsAndNameA" );
341 pBuildTrusteeWithObjectsAndSidA = (void *)GetProcAddress (hmod, "BuildTrusteeWithObjectsAndSidA" );
342 pGetTrusteeNameA = (void *)GetProcAddress (hmod, "GetTrusteeNameA" );
343 if( !pBuildTrusteeWithSidA || !pBuildTrusteeWithNameA ||
344 !pBuildTrusteeWithObjectsAndNameA || !pBuildTrusteeWithObjectsAndSidA ||
345 !pGetTrusteeNameA )
346 return;
348 if ( ! AllocateAndInitializeSid( &auth, 1, 42, 0,0,0,0,0,0,0,&psid ) )
350 trace( "failed to init SID\n" );
351 return;
354 /* test BuildTrusteeWithSidA */
355 memset( &trustee, 0xff, sizeof trustee );
356 pBuildTrusteeWithSidA( &trustee, psid );
358 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
359 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
360 "MultipleTrusteeOperation wrong\n");
361 ok( trustee.TrusteeForm == TRUSTEE_IS_SID, "TrusteeForm wrong\n");
362 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
363 ok( trustee.ptstrName == psid, "ptstrName wrong\n" );
365 /* test BuildTrusteeWithObjectsAndSidA (test 1) */
366 memset( &trustee, 0xff, sizeof trustee );
367 memset( &oas, 0xff, sizeof(oas) );
368 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, &ObjectType,
369 &InheritedObjectType, psid);
371 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
372 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
373 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
374 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
375 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
377 ok(oas.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
378 ok(!memcmp(&oas.ObjectTypeGuid, &ObjectType, sizeof(GUID)), "ObjectTypeGuid wrong\n");
379 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
380 ok(oas.pSid == psid, "pSid wrong\n");
382 /* test GetTrusteeNameA */
383 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oas, "GetTrusteeName returned wrong value\n");
385 /* test BuildTrusteeWithObjectsAndSidA (test 2) */
386 memset( &trustee, 0xff, sizeof trustee );
387 memset( &oas, 0xff, sizeof(oas) );
388 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, NULL,
389 &InheritedObjectType, psid);
391 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
392 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
393 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
394 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
395 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
397 ok(oas.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
398 ok(!memcmp(&oas.ObjectTypeGuid, &ZeroGuid, sizeof(GUID)), "ObjectTypeGuid wrong\n");
399 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
400 ok(oas.pSid == psid, "pSid wrong\n");
402 FreeSid( psid );
404 /* test BuildTrusteeWithNameA */
405 memset( &trustee, 0xff, sizeof trustee );
406 pBuildTrusteeWithNameA( &trustee, szTrusteeName );
408 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
409 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
410 "MultipleTrusteeOperation wrong\n");
411 ok( trustee.TrusteeForm == TRUSTEE_IS_NAME, "TrusteeForm wrong\n");
412 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
413 ok( trustee.ptstrName == szTrusteeName, "ptstrName wrong\n" );
415 /* test BuildTrusteeWithObjectsAndNameA (test 1) */
416 memset( &trustee, 0xff, sizeof trustee );
417 memset( &oan, 0xff, sizeof(oan) );
418 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
419 szInheritedObjectTypeName, szTrusteeName);
421 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
422 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
423 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
424 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
425 ok(trustee.ptstrName == (LPTSTR)&oan, "ptstrName wrong\n");
427 ok(oan.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
428 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
429 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
430 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
432 /* test GetTrusteeNameA */
433 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oan, "GetTrusteeName returned wrong value\n");
435 /* test BuildTrusteeWithObjectsAndNameA (test 2) */
436 memset( &trustee, 0xff, sizeof trustee );
437 memset( &oan, 0xff, sizeof(oan) );
438 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, NULL,
439 szInheritedObjectTypeName, szTrusteeName);
441 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
442 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
443 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
444 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
445 ok(trustee.ptstrName == (LPSTR)&oan, "ptstrName wrong\n");
447 ok(oan.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
448 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
449 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
450 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
452 /* test BuildTrusteeWithObjectsAndNameA (test 3) */
453 memset( &trustee, 0xff, sizeof trustee );
454 memset( &oan, 0xff, sizeof(oan) );
455 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
456 NULL, szTrusteeName);
458 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
459 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
460 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
461 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
462 ok(trustee.ptstrName == (LPTSTR)&oan, "ptstrName wrong\n");
464 ok(oan.ObjectsPresent == ACE_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
465 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
466 ok(oan.InheritedObjectTypeName == NULL, "InheritedObjectTypeName wrong\n");
467 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
470 /* If the first isn't defined, assume none is */
471 #ifndef SE_MIN_WELL_KNOWN_PRIVILEGE
472 #define SE_MIN_WELL_KNOWN_PRIVILEGE 2L
473 #define SE_CREATE_TOKEN_PRIVILEGE 2L
474 #define SE_ASSIGNPRIMARYTOKEN_PRIVILEGE 3L
475 #define SE_LOCK_MEMORY_PRIVILEGE 4L
476 #define SE_INCREASE_QUOTA_PRIVILEGE 5L
477 #define SE_MACHINE_ACCOUNT_PRIVILEGE 6L
478 #define SE_TCB_PRIVILEGE 7L
479 #define SE_SECURITY_PRIVILEGE 8L
480 #define SE_TAKE_OWNERSHIP_PRIVILEGE 9L
481 #define SE_LOAD_DRIVER_PRIVILEGE 10L
482 #define SE_SYSTEM_PROFILE_PRIVILEGE 11L
483 #define SE_SYSTEMTIME_PRIVILEGE 12L
484 #define SE_PROF_SINGLE_PROCESS_PRIVILEGE 13L
485 #define SE_INC_BASE_PRIORITY_PRIVILEGE 14L
486 #define SE_CREATE_PAGEFILE_PRIVILEGE 15L
487 #define SE_CREATE_PERMANENT_PRIVILEGE 16L
488 #define SE_BACKUP_PRIVILEGE 17L
489 #define SE_RESTORE_PRIVILEGE 18L
490 #define SE_SHUTDOWN_PRIVILEGE 19L
491 #define SE_DEBUG_PRIVILEGE 20L
492 #define SE_AUDIT_PRIVILEGE 21L
493 #define SE_SYSTEM_ENVIRONMENT_PRIVILEGE 22L
494 #define SE_CHANGE_NOTIFY_PRIVILEGE 23L
495 #define SE_REMOTE_SHUTDOWN_PRIVILEGE 24L
496 #define SE_UNDOCK_PRIVILEGE 25L
497 #define SE_SYNC_AGENT_PRIVILEGE 26L
498 #define SE_ENABLE_DELEGATION_PRIVILEGE 27L
499 #define SE_MANAGE_VOLUME_PRIVILEGE 28L
500 #define SE_IMPERSONATE_PRIVILEGE 29L
501 #define SE_CREATE_GLOBAL_PRIVILEGE 30L
502 #define SE_MAX_WELL_KNOWN_PRIVILEGE SE_CREATE_GLOBAL_PRIVILEGE
503 #endif /* ndef SE_MIN_WELL_KNOWN_PRIVILEGE */
505 static void test_allocateLuid(void)
507 BOOL (WINAPI *pAllocateLocallyUniqueId)(PLUID);
508 LUID luid1, luid2;
509 BOOL ret;
511 pAllocateLocallyUniqueId = (void*)GetProcAddress(hmod, "AllocateLocallyUniqueId");
512 if (!pAllocateLocallyUniqueId) return;
514 ret = pAllocateLocallyUniqueId(&luid1);
515 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
516 return;
518 ok(ret,
519 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
520 ret = pAllocateLocallyUniqueId(&luid2);
521 ok( ret,
522 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
523 ok(luid1.LowPart > SE_MAX_WELL_KNOWN_PRIVILEGE || luid1.HighPart != 0,
524 "AllocateLocallyUniqueId returned a well-known LUID\n");
525 ok(luid1.LowPart != luid2.LowPart || luid1.HighPart != luid2.HighPart,
526 "AllocateLocallyUniqueId returned non-unique LUIDs\n");
527 ret = pAllocateLocallyUniqueId(NULL);
528 ok( !ret && GetLastError() == ERROR_NOACCESS,
529 "AllocateLocallyUniqueId(NULL) didn't return ERROR_NOACCESS: %d\n",
530 GetLastError());
533 static void test_lookupPrivilegeName(void)
535 BOOL (WINAPI *pLookupPrivilegeNameA)(LPCSTR, PLUID, LPSTR, LPDWORD);
536 char buf[MAX_PATH]; /* arbitrary, seems long enough */
537 DWORD cchName = sizeof(buf);
538 LUID luid = { 0, 0 };
539 LONG i;
540 BOOL ret;
542 /* check whether it's available first */
543 pLookupPrivilegeNameA = (void*)GetProcAddress(hmod, "LookupPrivilegeNameA");
544 if (!pLookupPrivilegeNameA) return;
545 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
546 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
547 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
548 return;
550 /* check with a short buffer */
551 cchName = 0;
552 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
553 ret = pLookupPrivilegeNameA(NULL, &luid, NULL, &cchName);
554 ok( !ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
555 "LookupPrivilegeNameA didn't fail with ERROR_INSUFFICIENT_BUFFER: %d\n",
556 GetLastError());
557 ok(cchName == strlen("SeCreateTokenPrivilege") + 1,
558 "LookupPrivilegeNameA returned an incorrect required length for\n"
559 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName,
560 lstrlenA("SeCreateTokenPrivilege") + 1);
561 /* check a known value and its returned length on success */
562 cchName = sizeof(buf);
563 ok(pLookupPrivilegeNameA(NULL, &luid, buf, &cchName) &&
564 cchName == strlen("SeCreateTokenPrivilege"),
565 "LookupPrivilegeNameA returned an incorrect output length for\n"
566 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName,
567 (int)strlen("SeCreateTokenPrivilege"));
568 /* check known values */
569 for (i = SE_MIN_WELL_KNOWN_PRIVILEGE; i <= SE_MAX_WELL_KNOWN_PRIVILEGE; i++)
571 luid.LowPart = i;
572 cchName = sizeof(buf);
573 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
574 ok( ret || GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
575 "LookupPrivilegeNameA(0.%d) failed: %d\n", i, GetLastError());
577 /* check a bogus LUID */
578 luid.LowPart = 0xdeadbeef;
579 cchName = sizeof(buf);
580 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
581 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
582 "LookupPrivilegeNameA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
583 GetLastError());
584 /* check on a bogus system */
585 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
586 cchName = sizeof(buf);
587 ret = pLookupPrivilegeNameA("b0gu5.Nam3", &luid, buf, &cchName);
588 ok( !ret && (GetLastError() == RPC_S_SERVER_UNAVAILABLE ||
589 GetLastError() == RPC_S_INVALID_NET_ADDR) /* w2k8 */,
590 "LookupPrivilegeNameA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
591 GetLastError());
594 struct NameToLUID
596 const char *name;
597 DWORD lowPart;
600 static void test_lookupPrivilegeValue(void)
602 static const struct NameToLUID privs[] = {
603 { "SeCreateTokenPrivilege", SE_CREATE_TOKEN_PRIVILEGE },
604 { "SeAssignPrimaryTokenPrivilege", SE_ASSIGNPRIMARYTOKEN_PRIVILEGE },
605 { "SeLockMemoryPrivilege", SE_LOCK_MEMORY_PRIVILEGE },
606 { "SeIncreaseQuotaPrivilege", SE_INCREASE_QUOTA_PRIVILEGE },
607 { "SeMachineAccountPrivilege", SE_MACHINE_ACCOUNT_PRIVILEGE },
608 { "SeTcbPrivilege", SE_TCB_PRIVILEGE },
609 { "SeSecurityPrivilege", SE_SECURITY_PRIVILEGE },
610 { "SeTakeOwnershipPrivilege", SE_TAKE_OWNERSHIP_PRIVILEGE },
611 { "SeLoadDriverPrivilege", SE_LOAD_DRIVER_PRIVILEGE },
612 { "SeSystemProfilePrivilege", SE_SYSTEM_PROFILE_PRIVILEGE },
613 { "SeSystemtimePrivilege", SE_SYSTEMTIME_PRIVILEGE },
614 { "SeProfileSingleProcessPrivilege", SE_PROF_SINGLE_PROCESS_PRIVILEGE },
615 { "SeIncreaseBasePriorityPrivilege", SE_INC_BASE_PRIORITY_PRIVILEGE },
616 { "SeCreatePagefilePrivilege", SE_CREATE_PAGEFILE_PRIVILEGE },
617 { "SeCreatePermanentPrivilege", SE_CREATE_PERMANENT_PRIVILEGE },
618 { "SeBackupPrivilege", SE_BACKUP_PRIVILEGE },
619 { "SeRestorePrivilege", SE_RESTORE_PRIVILEGE },
620 { "SeShutdownPrivilege", SE_SHUTDOWN_PRIVILEGE },
621 { "SeDebugPrivilege", SE_DEBUG_PRIVILEGE },
622 { "SeAuditPrivilege", SE_AUDIT_PRIVILEGE },
623 { "SeSystemEnvironmentPrivilege", SE_SYSTEM_ENVIRONMENT_PRIVILEGE },
624 { "SeChangeNotifyPrivilege", SE_CHANGE_NOTIFY_PRIVILEGE },
625 { "SeRemoteShutdownPrivilege", SE_REMOTE_SHUTDOWN_PRIVILEGE },
626 { "SeUndockPrivilege", SE_UNDOCK_PRIVILEGE },
627 { "SeSyncAgentPrivilege", SE_SYNC_AGENT_PRIVILEGE },
628 { "SeEnableDelegationPrivilege", SE_ENABLE_DELEGATION_PRIVILEGE },
629 { "SeManageVolumePrivilege", SE_MANAGE_VOLUME_PRIVILEGE },
630 { "SeImpersonatePrivilege", SE_IMPERSONATE_PRIVILEGE },
631 { "SeCreateGlobalPrivilege", SE_CREATE_GLOBAL_PRIVILEGE },
633 BOOL (WINAPI *pLookupPrivilegeValueA)(LPCSTR, LPCSTR, PLUID);
634 unsigned int i;
635 LUID luid;
636 BOOL ret;
638 /* check whether it's available first */
639 pLookupPrivilegeValueA = (void*)GetProcAddress(hmod, "LookupPrivilegeValueA");
640 if (!pLookupPrivilegeValueA) return;
641 ret = pLookupPrivilegeValueA(NULL, "SeCreateTokenPrivilege", &luid);
642 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
643 return;
645 /* check a bogus system name */
646 ret = pLookupPrivilegeValueA("b0gu5.Nam3", "SeCreateTokenPrivilege", &luid);
647 ok( !ret && (GetLastError() == RPC_S_SERVER_UNAVAILABLE ||
648 GetLastError() == RPC_S_INVALID_NET_ADDR) /* w2k8 */,
649 "LookupPrivilegeValueA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
650 GetLastError());
651 /* check a NULL string */
652 ret = pLookupPrivilegeValueA(NULL, 0, &luid);
653 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
654 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
655 GetLastError());
656 /* check a bogus privilege name */
657 ret = pLookupPrivilegeValueA(NULL, "SeBogusPrivilege", &luid);
658 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
659 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
660 GetLastError());
661 /* check case insensitive */
662 ret = pLookupPrivilegeValueA(NULL, "sEcREATEtOKENpRIVILEGE", &luid);
663 ok( ret,
664 "LookupPrivilegeValueA(NULL, sEcREATEtOKENpRIVILEGE, &luid) failed: %d\n",
665 GetLastError());
666 for (i = 0; i < sizeof(privs) / sizeof(privs[0]); i++)
668 /* Not all privileges are implemented on all Windows versions, so
669 * don't worry if the call fails
671 if (pLookupPrivilegeValueA(NULL, privs[i].name, &luid))
673 ok(luid.LowPart == privs[i].lowPart,
674 "LookupPrivilegeValueA returned an invalid LUID for %s\n",
675 privs[i].name);
680 static void test_luid(void)
682 test_allocateLuid();
683 test_lookupPrivilegeName();
684 test_lookupPrivilegeValue();
687 static void test_FileSecurity(void)
689 char wintmpdir [MAX_PATH];
690 char path [MAX_PATH];
691 char file [MAX_PATH];
692 BOOL rc;
693 HANDLE fh;
694 DWORD sdSize;
695 DWORD retSize;
696 BYTE *sd;
697 SECURITY_INFORMATION const request = OWNER_SECURITY_INFORMATION
698 | GROUP_SECURITY_INFORMATION
699 | DACL_SECURITY_INFORMATION;
701 if (!pGetFileSecurityA) {
702 win_skip ("GetFileSecurity is not available\n");
703 return;
706 if (!pSetFileSecurityA) {
707 win_skip ("SetFileSecurity is not available\n");
708 return;
711 if (!GetTempPathA (sizeof (wintmpdir), wintmpdir)) {
712 win_skip ("GetTempPathA failed\n");
713 return;
716 /* Create a temporary directory and in it a temporary file */
717 strcat (strcpy (path, wintmpdir), "rary");
718 SetLastError(0xdeadbeef);
719 rc = CreateDirectoryA (path, NULL);
720 ok (rc || GetLastError() == ERROR_ALREADY_EXISTS, "CreateDirectoryA "
721 "failed for '%s' with %d\n", path, GetLastError());
723 strcat (strcpy (file, path), "\\ess");
724 SetLastError(0xdeadbeef);
725 fh = CreateFileA (file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
726 ok (fh != INVALID_HANDLE_VALUE, "CreateFileA "
727 "failed for '%s' with %d\n", file, GetLastError());
728 CloseHandle (fh);
730 /* For the temporary file ... */
732 /* Get size needed */
733 retSize = 0;
734 SetLastError(0xdeadbeef);
735 rc = pGetFileSecurityA (file, request, NULL, 0, &retSize);
736 if (!rc && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)) {
737 win_skip("GetFileSecurityA is not implemented\n");
738 goto cleanup;
740 ok (!rc, "GetFileSecurityA "
741 "was expected to fail for '%s'\n", file);
742 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetFileSecurityA "
743 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
744 ok (retSize > sizeof (SECURITY_DESCRIPTOR), "GetFileSecurityA returned size %d\n", retSize);
746 sdSize = retSize;
747 sd = HeapAlloc (GetProcessHeap (), 0, sdSize);
749 /* Get security descriptor for real */
750 retSize = -1;
751 SetLastError(0xdeadbeef);
752 rc = pGetFileSecurityA (file, request, sd, sdSize, &retSize);
753 ok (rc, "GetFileSecurityA "
754 "was not expected to fail '%s': %d\n", file, GetLastError());
755 ok (retSize == sdSize ||
756 broken(retSize == 0), /* NT4 */
757 "GetFileSecurityA returned size %d; expected %d\n", retSize, sdSize);
759 /* Use it to set security descriptor */
760 SetLastError(0xdeadbeef);
761 rc = pSetFileSecurityA (file, request, sd);
762 ok (rc, "SetFileSecurityA "
763 "was not expected to fail '%s': %d\n", file, GetLastError());
765 HeapFree (GetProcessHeap (), 0, sd);
767 /* Repeat for the temporary directory ... */
769 /* Get size needed */
770 retSize = 0;
771 SetLastError(0xdeadbeef);
772 rc = pGetFileSecurityA (path, request, NULL, 0, &retSize);
773 ok (!rc, "GetFileSecurityA "
774 "was expected to fail for '%s'\n", path);
775 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetFileSecurityA "
776 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
777 ok (retSize > sizeof (SECURITY_DESCRIPTOR), "GetFileSecurityA returned size %d\n", retSize);
779 sdSize = retSize;
780 sd = HeapAlloc (GetProcessHeap (), 0, sdSize);
782 /* Get security descriptor for real */
783 retSize = -1;
784 SetLastError(0xdeadbeef);
785 rc = pGetFileSecurityA (path, request, sd, sdSize, &retSize);
786 ok (rc, "GetFileSecurityA "
787 "was not expected to fail '%s': %d\n", path, GetLastError());
788 ok (retSize == sdSize ||
789 broken(retSize == 0), /* NT4 */
790 "GetFileSecurityA returned size %d; expected %d\n", retSize, sdSize);
792 /* Use it to set security descriptor */
793 SetLastError(0xdeadbeef);
794 rc = pSetFileSecurityA (path, request, sd);
795 ok (rc, "SetFileSecurityA "
796 "was not expected to fail '%s': %d\n", path, GetLastError());
797 HeapFree (GetProcessHeap (), 0, sd);
799 /* Old test */
800 strcpy (wintmpdir, "\\Should not exist");
801 SetLastError(0xdeadbeef);
802 rc = pGetFileSecurityA (wintmpdir, OWNER_SECURITY_INFORMATION, NULL, 0, &sdSize);
803 ok (!rc, "GetFileSecurityA should fail for not existing directories/files\n");
804 ok (GetLastError() == ERROR_FILE_NOT_FOUND,
805 "last error ERROR_FILE_NOT_FOUND expected, got %d\n", GetLastError());
807 cleanup:
808 /* Remove temporary file and directory */
809 DeleteFileA(file);
810 RemoveDirectoryA(path);
813 static void test_AccessCheck(void)
815 PSID EveryoneSid = NULL, AdminSid = NULL, UsersSid = NULL;
816 PACL Acl = NULL;
817 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL;
818 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
819 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
820 GENERIC_MAPPING Mapping = { KEY_READ, KEY_WRITE, KEY_EXECUTE, KEY_ALL_ACCESS };
821 ACCESS_MASK Access;
822 BOOL AccessStatus;
823 HANDLE Token;
824 HANDLE ProcessToken;
825 BOOL ret;
826 DWORD PrivSetLen;
827 PRIVILEGE_SET *PrivSet;
828 BOOL res;
829 HMODULE NtDllModule;
830 BOOLEAN Enabled;
831 DWORD err;
832 NTSTATUS ntret, ntAccessStatus;
834 NtDllModule = GetModuleHandle("ntdll.dll");
835 if (!NtDllModule)
837 skip("not running on NT, skipping test\n");
838 return;
840 pRtlAdjustPrivilege = (void *)GetProcAddress(NtDllModule, "RtlAdjustPrivilege");
841 if (!pRtlAdjustPrivilege)
843 win_skip("missing RtlAdjustPrivilege, skipping test\n");
844 return;
847 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
848 res = InitializeAcl(Acl, 256, ACL_REVISION);
849 if(!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
851 skip("ACLs not implemented - skipping tests\n");
852 HeapFree(GetProcessHeap(), 0, Acl);
853 return;
855 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
857 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
858 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
860 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
861 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdminSid);
862 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
864 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
865 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &UsersSid);
866 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
868 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
870 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
871 ok(res, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
873 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
874 ok(res, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
876 PrivSetLen = FIELD_OFFSET(PRIVILEGE_SET, Privilege[16]);
877 PrivSet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, PrivSetLen);
878 PrivSet->PrivilegeCount = 16;
880 res = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &ProcessToken);
881 ok(res, "OpenProcessToken failed with error %d\n", GetLastError());
883 pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, FALSE, TRUE, &Enabled);
885 res = DuplicateToken(ProcessToken, SecurityImpersonation, &Token);
886 ok(res, "DuplicateToken failed with error %d\n", GetLastError());
888 /* SD without owner/group */
889 SetLastError(0xdeadbeef);
890 Access = AccessStatus = 0x1abe11ed;
891 ret = AccessCheck(SecurityDescriptor, Token, KEY_QUERY_VALUE, &Mapping,
892 PrivSet, &PrivSetLen, &Access, &AccessStatus);
893 err = GetLastError();
894 ok(!ret && err == ERROR_INVALID_SECURITY_DESCR, "AccessCheck should have "
895 "failed with ERROR_INVALID_SECURITY_DESCR, instead of %d\n", err);
896 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
897 "Access and/or AccessStatus were changed!\n");
899 /* Set owner and group */
900 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
901 ok(res, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
902 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, TRUE);
903 ok(res, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
905 /* Generic access mask */
906 SetLastError(0xdeadbeef);
907 Access = AccessStatus = 0x1abe11ed;
908 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
909 PrivSet, &PrivSetLen, &Access, &AccessStatus);
910 err = GetLastError();
911 ok(!ret && err == ERROR_GENERIC_NOT_MAPPED, "AccessCheck should have failed "
912 "with ERROR_GENERIC_NOT_MAPPED, instead of %d\n", err);
913 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
914 "Access and/or AccessStatus were changed!\n");
916 /* Generic access mask - no privilegeset buffer */
917 SetLastError(0xdeadbeef);
918 Access = AccessStatus = 0x1abe11ed;
919 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
920 NULL, &PrivSetLen, &Access, &AccessStatus);
921 err = GetLastError();
922 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
923 "with ERROR_NOACCESS, instead of %d\n", err);
924 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
925 "Access and/or AccessStatus were changed!\n");
927 /* Generic access mask - no returnlength */
928 SetLastError(0xdeadbeef);
929 Access = AccessStatus = 0x1abe11ed;
930 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
931 PrivSet, NULL, &Access, &AccessStatus);
932 err = GetLastError();
933 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
934 "with ERROR_NOACCESS, instead of %d\n", err);
935 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
936 "Access and/or AccessStatus were changed!\n");
938 /* Generic access mask - no privilegeset buffer, no returnlength */
939 SetLastError(0xdeadbeef);
940 Access = AccessStatus = 0x1abe11ed;
941 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
942 NULL, NULL, &Access, &AccessStatus);
943 err = GetLastError();
944 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
945 "with ERROR_NOACCESS, instead of %d\n", err);
946 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
947 "Access and/or AccessStatus were changed!\n");
949 /* sd with no dacl present */
950 Access = AccessStatus = 0x1abe11ed;
951 ret = SetSecurityDescriptorDacl(SecurityDescriptor, FALSE, NULL, FALSE);
952 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
953 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
954 PrivSet, &PrivSetLen, &Access, &AccessStatus);
955 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
956 ok(AccessStatus && (Access == KEY_READ),
957 "AccessCheck failed to grant access with error %d\n",
958 GetLastError());
960 /* sd with no dacl present - no privilegeset buffer */
961 SetLastError(0xdeadbeef);
962 Access = AccessStatus = 0x1abe11ed;
963 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
964 NULL, &PrivSetLen, &Access, &AccessStatus);
965 err = GetLastError();
966 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
967 "with ERROR_NOACCESS, instead of %d\n", err);
968 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
969 "Access and/or AccessStatus were changed!\n");
971 if(pNtAccessCheck)
973 /* Generic access mask - no privilegeset buffer */
974 SetLastError(0xdeadbeef);
975 Access = ntAccessStatus = 0x1abe11ed;
976 ntret = pNtAccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
977 NULL, &PrivSetLen, &Access, &ntAccessStatus);
978 err = GetLastError();
979 ok(ntret == STATUS_ACCESS_VIOLATION,
980 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret);
981 ok(err == 0xdeadbeef,
982 "NtAccessCheck shouldn't set last error, got %d\n", err);
983 ok(Access == 0x1abe11ed && ntAccessStatus == 0x1abe11ed,
984 "Access and/or AccessStatus were changed!\n");
986 /* Generic access mask - no returnlength */
987 SetLastError(0xdeadbeef);
988 Access = ntAccessStatus = 0x1abe11ed;
989 ntret = pNtAccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
990 PrivSet, NULL, &Access, &ntAccessStatus);
991 err = GetLastError();
992 ok(ntret == STATUS_ACCESS_VIOLATION,
993 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret);
994 ok(err == 0xdeadbeef,
995 "NtAccessCheck shouldn't set last error, got %d\n", err);
996 ok(Access == 0x1abe11ed && ntAccessStatus == 0x1abe11ed,
997 "Access and/or AccessStatus were changed!\n");
999 /* Generic access mask - no privilegeset buffer, no returnlength */
1000 SetLastError(0xdeadbeef);
1001 Access = ntAccessStatus = 0x1abe11ed;
1002 ntret = pNtAccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1003 NULL, NULL, &Access, &ntAccessStatus);
1004 err = GetLastError();
1005 ok(ntret == STATUS_ACCESS_VIOLATION,
1006 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret);
1007 ok(err == 0xdeadbeef,
1008 "NtAccessCheck shouldn't set last error, got %d\n", err);
1009 ok(Access == 0x1abe11ed && ntAccessStatus == 0x1abe11ed,
1010 "Access and/or AccessStatus were changed!\n");
1012 else
1013 win_skip("NtAccessCheck unavailable. Skipping.\n");
1015 /* sd with NULL dacl */
1016 Access = AccessStatus = 0x1abe11ed;
1017 ret = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, NULL, FALSE);
1018 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1019 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1020 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1021 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1022 ok(AccessStatus && (Access == KEY_READ),
1023 "AccessCheck failed to grant access with error %d\n",
1024 GetLastError());
1026 /* sd with blank dacl */
1027 ret = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
1028 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1029 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1030 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1031 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1032 err = GetLastError();
1033 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
1034 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
1035 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
1037 res = AddAccessAllowedAce(Acl, ACL_REVISION, KEY_READ, EveryoneSid);
1038 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
1040 res = AddAccessDeniedAce(Acl, ACL_REVISION, KEY_SET_VALUE, AdminSid);
1041 ok(res, "AddAccessDeniedAce failed with error %d\n", GetLastError());
1043 /* sd with dacl */
1044 Access = AccessStatus = 0x1abe11ed;
1045 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1046 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1047 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1048 ok(AccessStatus && (Access == KEY_READ),
1049 "AccessCheck failed to grant access with error %d\n",
1050 GetLastError());
1052 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
1053 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1054 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1055 ok(AccessStatus,
1056 "AccessCheck failed to grant any access with error %d\n",
1057 GetLastError());
1058 trace("AccessCheck with MAXIMUM_ALLOWED got Access 0x%08x\n", Access);
1060 /* Access denied by SD */
1061 SetLastError(0xdeadbeef);
1062 Access = AccessStatus = 0x1abe11ed;
1063 ret = AccessCheck(SecurityDescriptor, Token, KEY_WRITE, &Mapping,
1064 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1065 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1066 err = GetLastError();
1067 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
1068 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
1069 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
1071 SetLastError(0);
1072 PrivSet->PrivilegeCount = 16;
1073 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
1074 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1075 ok(ret && !AccessStatus && GetLastError() == ERROR_PRIVILEGE_NOT_HELD,
1076 "AccessCheck should have failed with ERROR_PRIVILEGE_NOT_HELD, instead of %d\n",
1077 GetLastError());
1079 ret = ImpersonateLoggedOnUser(Token);
1080 ok(ret, "ImpersonateLoggedOnUser failed with error %d\n", GetLastError());
1081 ret = pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, TRUE, TRUE, &Enabled);
1082 if (!ret)
1084 SetLastError(0);
1085 PrivSet->PrivilegeCount = 16;
1086 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
1087 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1088 ok(ret && AccessStatus && GetLastError() == 0,
1089 "AccessCheck should have succeeded, error %d\n",
1090 GetLastError());
1091 ok(Access == ACCESS_SYSTEM_SECURITY,
1092 "Access should be equal to ACCESS_SYSTEM_SECURITY instead of 0x%08x\n",
1093 Access);
1095 else
1096 trace("Couldn't get SE_SECURITY_PRIVILEGE (0x%08x), skipping ACCESS_SYSTEM_SECURITY test\n",
1097 ret);
1098 ret = RevertToSelf();
1099 ok(ret, "RevertToSelf failed with error %d\n", GetLastError());
1101 /* test INHERIT_ONLY_ACE */
1102 ret = InitializeAcl(Acl, 256, ACL_REVISION);
1103 ok(ret, "InitializeAcl failed with error %d\n", GetLastError());
1105 /* NT doesn't have AddAccessAllowedAceEx. Skipping this call/test doesn't influence
1106 * the next ones.
1108 if (pAddAccessAllowedAceEx)
1110 ret = pAddAccessAllowedAceEx(Acl, ACL_REVISION, INHERIT_ONLY_ACE, KEY_READ, EveryoneSid);
1111 ok(ret, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
1113 else
1114 win_skip("AddAccessAllowedAceEx is not available\n");
1116 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1117 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1118 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1119 err = GetLastError();
1120 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
1121 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
1122 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
1124 CloseHandle(Token);
1126 res = DuplicateToken(ProcessToken, SecurityAnonymous, &Token);
1127 ok(res, "DuplicateToken failed with error %d\n", GetLastError());
1129 SetLastError(0xdeadbeef);
1130 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
1131 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1132 err = GetLastError();
1133 ok(!ret && err == ERROR_BAD_IMPERSONATION_LEVEL, "AccessCheck should have failed "
1134 "with ERROR_BAD_IMPERSONATION_LEVEL, instead of %d\n", err);
1136 CloseHandle(Token);
1138 SetLastError(0xdeadbeef);
1139 ret = AccessCheck(SecurityDescriptor, ProcessToken, KEY_READ, &Mapping,
1140 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1141 err = GetLastError();
1142 ok(!ret && err == ERROR_NO_IMPERSONATION_TOKEN, "AccessCheck should have failed "
1143 "with ERROR_NO_IMPERSONATION_TOKEN, instead of %d\n", err);
1145 CloseHandle(ProcessToken);
1147 if (EveryoneSid)
1148 FreeSid(EveryoneSid);
1149 if (AdminSid)
1150 FreeSid(AdminSid);
1151 if (UsersSid)
1152 FreeSid(UsersSid);
1153 HeapFree(GetProcessHeap(), 0, Acl);
1154 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
1155 HeapFree(GetProcessHeap(), 0, PrivSet);
1158 /* test GetTokenInformation for the various attributes */
1159 static void test_token_attr(void)
1161 HANDLE Token, ImpersonationToken;
1162 DWORD Size, Size2;
1163 TOKEN_PRIVILEGES *Privileges;
1164 TOKEN_GROUPS *Groups;
1165 TOKEN_USER *User;
1166 TOKEN_DEFAULT_DACL *Dacl;
1167 BOOL ret;
1168 DWORD i, GLE;
1169 LPSTR SidString;
1170 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel;
1171 ACL *acl;
1173 /* cygwin-like use case */
1174 SetLastError(0xdeadbeef);
1175 ret = OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED, &Token);
1176 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1178 win_skip("OpenProcessToken is not implemented\n");
1179 return;
1181 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1182 if (ret)
1184 DWORD buf[256]; /* GetTokenInformation wants a dword-aligned buffer */
1185 Size = sizeof(buf);
1186 ret = GetTokenInformation(Token, TokenUser,(void*)buf, Size, &Size);
1187 ok(ret, "GetTokenInformation failed with error %d\n", GetLastError());
1188 Size = sizeof(ImpersonationLevel);
1189 ret = GetTokenInformation(Token, TokenImpersonationLevel, &ImpersonationLevel, Size, &Size);
1190 GLE = GetLastError();
1191 ok(!ret && (GLE == ERROR_INVALID_PARAMETER), "GetTokenInformation(TokenImpersonationLevel) on primary token should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GLE);
1192 CloseHandle(Token);
1195 if(!pConvertSidToStringSidA)
1197 win_skip("ConvertSidToStringSidA is not available\n");
1198 return;
1201 SetLastError(0xdeadbeef);
1202 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &Token);
1203 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1205 /* groups */
1206 SetLastError(0xdeadbeef);
1207 ret = GetTokenInformation(Token, TokenGroups, NULL, 0, &Size);
1208 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1209 "GetTokenInformation(TokenGroups) %s with error %d\n",
1210 ret ? "succeeded" : "failed", GetLastError());
1211 Groups = HeapAlloc(GetProcessHeap(), 0, Size);
1212 SetLastError(0xdeadbeef);
1213 ret = GetTokenInformation(Token, TokenGroups, Groups, Size, &Size);
1214 ok(ret, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
1215 ok(GetLastError() == 0xdeadbeef,
1216 "GetTokenInformation shouldn't have set last error to %d\n",
1217 GetLastError());
1218 trace("TokenGroups:\n");
1219 for (i = 0; i < Groups->GroupCount; i++)
1221 DWORD NameLength = 255;
1222 TCHAR Name[255];
1223 DWORD DomainLength = 255;
1224 TCHAR Domain[255];
1225 SID_NAME_USE SidNameUse;
1226 Name[0] = '\0';
1227 Domain[0] = '\0';
1228 ret = LookupAccountSid(NULL, Groups->Groups[i].Sid, Name, &NameLength, Domain, &DomainLength, &SidNameUse);
1229 if (ret)
1231 pConvertSidToStringSidA(Groups->Groups[i].Sid, &SidString);
1232 trace("%s, %s\\%s use: %d attr: 0x%08x\n", SidString, Domain, Name, SidNameUse, Groups->Groups[i].Attributes);
1233 LocalFree(SidString);
1235 else trace("attr: 0x%08x LookupAccountSid failed with error %d\n", Groups->Groups[i].Attributes, GetLastError());
1237 HeapFree(GetProcessHeap(), 0, Groups);
1239 /* user */
1240 ret = GetTokenInformation(Token, TokenUser, NULL, 0, &Size);
1241 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1242 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1243 User = HeapAlloc(GetProcessHeap(), 0, Size);
1244 ret = GetTokenInformation(Token, TokenUser, User, Size, &Size);
1245 ok(ret,
1246 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1248 pConvertSidToStringSidA(User->User.Sid, &SidString);
1249 trace("TokenUser: %s attr: 0x%08x\n", SidString, User->User.Attributes);
1250 LocalFree(SidString);
1251 HeapFree(GetProcessHeap(), 0, User);
1253 /* privileges */
1254 ret = GetTokenInformation(Token, TokenPrivileges, NULL, 0, &Size);
1255 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1256 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1257 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
1258 ret = GetTokenInformation(Token, TokenPrivileges, Privileges, Size, &Size);
1259 ok(ret,
1260 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1261 trace("TokenPrivileges:\n");
1262 for (i = 0; i < Privileges->PrivilegeCount; i++)
1264 TCHAR Name[256];
1265 DWORD NameLen = sizeof(Name)/sizeof(Name[0]);
1266 LookupPrivilegeName(NULL, &Privileges->Privileges[i].Luid, Name, &NameLen);
1267 trace("\t%s, 0x%x\n", Name, Privileges->Privileges[i].Attributes);
1269 HeapFree(GetProcessHeap(), 0, Privileges);
1271 ret = DuplicateToken(Token, SecurityAnonymous, &ImpersonationToken);
1272 ok(ret, "DuplicateToken failed with error %d\n", GetLastError());
1274 Size = sizeof(ImpersonationLevel);
1275 ret = GetTokenInformation(ImpersonationToken, TokenImpersonationLevel, &ImpersonationLevel, Size, &Size);
1276 ok(ret, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1277 ok(ImpersonationLevel == SecurityAnonymous, "ImpersonationLevel should have been SecurityAnonymous instead of %d\n", ImpersonationLevel);
1279 CloseHandle(ImpersonationToken);
1281 /* default dacl */
1282 ret = GetTokenInformation(Token, TokenDefaultDacl, NULL, 0, &Size);
1283 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1284 "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1286 Dacl = HeapAlloc(GetProcessHeap(), 0, Size);
1287 ret = GetTokenInformation(Token, TokenDefaultDacl, Dacl, Size, &Size);
1288 ok(ret, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1290 SetLastError(0xdeadbeef);
1291 ret = SetTokenInformation(Token, TokenDefaultDacl, NULL, 0);
1292 GLE = GetLastError();
1293 ok(!ret, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1294 ok(GLE == ERROR_BAD_LENGTH, "expected ERROR_BAD_LENGTH got %u\n", GLE);
1296 SetLastError(0xdeadbeef);
1297 ret = SetTokenInformation(Token, TokenDefaultDacl, NULL, Size);
1298 GLE = GetLastError();
1299 ok(!ret, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1300 ok(GLE == ERROR_NOACCESS, "expected ERROR_NOACCESS got %u\n", GLE);
1302 acl = Dacl->DefaultDacl;
1303 Dacl->DefaultDacl = NULL;
1305 ret = SetTokenInformation(Token, TokenDefaultDacl, Dacl, Size);
1306 ok(ret, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1308 Size2 = 0;
1309 Dacl->DefaultDacl = (ACL *)0xdeadbeef;
1310 ret = GetTokenInformation(Token, TokenDefaultDacl, Dacl, Size, &Size2);
1311 ok(ret, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1312 ok(Dacl->DefaultDacl == NULL, "expected NULL, got %p\n", Dacl->DefaultDacl);
1313 ok(Size2 == sizeof(TOKEN_DEFAULT_DACL) || broken(Size2 == 2*sizeof(TOKEN_DEFAULT_DACL)), /* WoW64 */
1314 "got %u expected %u (sizeof(TOKEN_DEFAULT_DACL))\n", Size2, sizeof(TOKEN_DEFAULT_DACL));
1316 Dacl->DefaultDacl = acl;
1317 ret = SetTokenInformation(Token, TokenDefaultDacl, Dacl, Size);
1318 ok(ret, "SetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1320 if (Size2 == sizeof(TOKEN_DEFAULT_DACL)) {
1321 ret = GetTokenInformation(Token, TokenDefaultDacl, Dacl, Size, &Size2);
1322 ok(ret, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1323 } else
1324 win_skip("TOKEN_DEFAULT_DACL size too small on WoW64\n");
1326 HeapFree(GetProcessHeap(), 0, Dacl);
1327 CloseHandle(Token);
1330 typedef union _MAX_SID
1332 SID sid;
1333 char max[SECURITY_MAX_SID_SIZE];
1334 } MAX_SID;
1336 static void test_sid_str(PSID * sid)
1338 char *str_sid;
1339 BOOL ret = pConvertSidToStringSidA(sid, &str_sid);
1340 ok(ret, "ConvertSidToStringSidA() failed: %d\n", GetLastError());
1341 if (ret)
1343 char account[MAX_PATH], domain[MAX_PATH];
1344 SID_NAME_USE use;
1345 DWORD acc_size = MAX_PATH;
1346 DWORD dom_size = MAX_PATH;
1347 ret = LookupAccountSid(NULL, sid, account, &acc_size, domain, &dom_size, &use);
1348 ok(ret || (!ret && (GetLastError() == ERROR_NONE_MAPPED)),
1349 "LookupAccountSid(%s) failed: %d\n", str_sid, GetLastError());
1350 if (ret)
1351 trace(" %s %s\\%s %d\n", str_sid, domain, account, use);
1352 else if (GetLastError() == ERROR_NONE_MAPPED)
1353 trace(" %s couldn't be mapped\n", str_sid);
1354 LocalFree(str_sid);
1358 static const struct well_known_sid_value
1360 BOOL without_domain;
1361 const char *sid_string;
1362 } well_known_sid_values[] = {
1363 /* 0 */ {TRUE, "S-1-0-0"}, {TRUE, "S-1-1-0"}, {TRUE, "S-1-2-0"}, {TRUE, "S-1-3-0"},
1364 /* 4 */ {TRUE, "S-1-3-1"}, {TRUE, "S-1-3-2"}, {TRUE, "S-1-3-3"}, {TRUE, "S-1-5"},
1365 /* 8 */ {FALSE, "S-1-5-1"}, {TRUE, "S-1-5-2"}, {TRUE, "S-1-5-3"}, {TRUE, "S-1-5-4"},
1366 /* 12 */ {TRUE, "S-1-5-6"}, {TRUE, "S-1-5-7"}, {TRUE, "S-1-5-8"}, {TRUE, "S-1-5-9"},
1367 /* 16 */ {TRUE, "S-1-5-10"}, {TRUE, "S-1-5-11"}, {TRUE, "S-1-5-12"}, {TRUE, "S-1-5-13"},
1368 /* 20 */ {TRUE, "S-1-5-14"}, {FALSE, NULL}, {TRUE, "S-1-5-18"}, {TRUE, "S-1-5-19"},
1369 /* 24 */ {TRUE, "S-1-5-20"}, {TRUE, "S-1-5-32"},
1370 /* 26 */ {FALSE, "S-1-5-32-544"}, {TRUE, "S-1-5-32-545"}, {TRUE, "S-1-5-32-546"},
1371 /* 29 */ {TRUE, "S-1-5-32-547"}, {TRUE, "S-1-5-32-548"}, {TRUE, "S-1-5-32-549"},
1372 /* 32 */ {TRUE, "S-1-5-32-550"}, {TRUE, "S-1-5-32-551"}, {TRUE, "S-1-5-32-552"},
1373 /* 35 */ {TRUE, "S-1-5-32-554"}, {TRUE, "S-1-5-32-555"}, {TRUE, "S-1-5-32-556"},
1374 /* 38 */ {FALSE, "S-1-5-21-12-23-34-45-56-500"}, {FALSE, "S-1-5-21-12-23-34-45-56-501"},
1375 /* 40 */ {FALSE, "S-1-5-21-12-23-34-45-56-502"}, {FALSE, "S-1-5-21-12-23-34-45-56-512"},
1376 /* 42 */ {FALSE, "S-1-5-21-12-23-34-45-56-513"}, {FALSE, "S-1-5-21-12-23-34-45-56-514"},
1377 /* 44 */ {FALSE, "S-1-5-21-12-23-34-45-56-515"}, {FALSE, "S-1-5-21-12-23-34-45-56-516"},
1378 /* 46 */ {FALSE, "S-1-5-21-12-23-34-45-56-517"}, {FALSE, "S-1-5-21-12-23-34-45-56-518"},
1379 /* 48 */ {FALSE, "S-1-5-21-12-23-34-45-56-519"}, {FALSE, "S-1-5-21-12-23-34-45-56-520"},
1380 /* 50 */ {FALSE, "S-1-5-21-12-23-34-45-56-553"},
1381 /* Added in Windows Server 2003 */
1382 /* 51 */ {TRUE, "S-1-5-64-10"}, {TRUE, "S-1-5-64-21"}, {TRUE, "S-1-5-64-14"},
1383 /* 54 */ {TRUE, "S-1-5-15"}, {TRUE, "S-1-5-1000"}, {FALSE, "S-1-5-32-557"},
1384 /* 57 */ {TRUE, "S-1-5-32-558"}, {TRUE, "S-1-5-32-559"}, {TRUE, "S-1-5-32-560"},
1385 /* 60 */ {TRUE, "S-1-5-32-561"}, {TRUE, "S-1-5-32-562"},
1386 /* Added in Windows Vista: */
1387 /* 62 */ {TRUE, "S-1-5-32-568"},
1388 /* 63 */ {TRUE, "S-1-5-17"}, {FALSE, "S-1-5-32-569"}, {TRUE, "S-1-16-0"},
1389 /* 66 */ {TRUE, "S-1-16-4096"}, {TRUE, "S-1-16-8192"}, {TRUE, "S-1-16-12288"},
1390 /* 69 */ {TRUE, "S-1-16-16384"}, {TRUE, "S-1-5-33"}, {TRUE, "S-1-3-4"},
1391 /* 72 */ {FALSE, "S-1-5-21-12-23-34-45-56-571"}, {FALSE, "S-1-5-21-12-23-34-45-56-572"},
1392 /* 74 */ {TRUE, "S-1-5-22"}, {FALSE, "S-1-5-21-12-23-34-45-56-521"}, {TRUE, "S-1-5-32-573"}
1395 static void test_CreateWellKnownSid(void)
1397 SID_IDENTIFIER_AUTHORITY ident = { SECURITY_NT_AUTHORITY };
1398 PSID domainsid, sid;
1399 DWORD size, error;
1400 BOOL ret;
1401 unsigned int i;
1403 if (!pCreateWellKnownSid)
1405 win_skip("CreateWellKnownSid not available\n");
1406 return;
1409 size = 0;
1410 SetLastError(0xdeadbeef);
1411 ret = pCreateWellKnownSid(WinInteractiveSid, NULL, NULL, &size);
1412 error = GetLastError();
1413 ok(!ret, "CreateWellKnownSid succeeded\n");
1414 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
1415 ok(size, "expected size > 0\n");
1417 SetLastError(0xdeadbeef);
1418 ret = pCreateWellKnownSid(WinInteractiveSid, NULL, NULL, &size);
1419 error = GetLastError();
1420 ok(!ret, "CreateWellKnownSid succeeded\n");
1421 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
1423 sid = HeapAlloc(GetProcessHeap(), 0, size);
1424 ret = pCreateWellKnownSid(WinInteractiveSid, NULL, sid, &size);
1425 ok(ret, "CreateWellKnownSid failed %u\n", GetLastError());
1426 HeapFree(GetProcessHeap(), 0, sid);
1428 /* a domain sid usually have three subauthorities but we test that CreateWellKnownSid doesn't check it */
1429 AllocateAndInitializeSid(&ident, 6, SECURITY_NT_NON_UNIQUE, 12, 23, 34, 45, 56, 0, 0, &domainsid);
1431 for (i = 0; i < sizeof(well_known_sid_values)/sizeof(well_known_sid_values[0]); i++)
1433 const struct well_known_sid_value *value = &well_known_sid_values[i];
1434 char sid_buffer[SECURITY_MAX_SID_SIZE];
1435 LPSTR str;
1436 DWORD cb;
1438 if (value->sid_string == NULL)
1439 continue;
1441 if (i > WinAccountRasAndIasServersSid)
1443 /* These SIDs aren't implemented by all Windows versions - detect it and break the loop */
1444 cb = sizeof(sid_buffer);
1445 if (!pCreateWellKnownSid(i, domainsid, sid_buffer, &cb))
1447 skip("Well known SIDs starting from %u are not implemented\n", i);
1448 break;
1452 cb = sizeof(sid_buffer);
1453 ok(pCreateWellKnownSid(i, value->without_domain ? NULL : domainsid, sid_buffer, &cb), "Couldn't create well known sid %u\n", i);
1454 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer)), cb, DWORD, "%d");
1455 ok(IsValidSid(sid_buffer), "The sid is not valid\n");
1456 ok(pConvertSidToStringSidA(sid_buffer, &str), "Couldn't convert SID to string\n");
1457 ok(strcmp(str, value->sid_string) == 0, "SID mismatch - expected %s, got %s\n",
1458 value->sid_string, str);
1459 LocalFree(str);
1461 if (value->without_domain)
1463 char buf2[SECURITY_MAX_SID_SIZE];
1464 cb = sizeof(buf2);
1465 ok(pCreateWellKnownSid(i, domainsid, buf2, &cb), "Couldn't create well known sid %u with optional domain\n", i);
1466 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer)), cb, DWORD, "%d");
1467 ok(memcmp(buf2, sid_buffer, cb) == 0, "SID create with domain is different than without (%u)\n", i);
1471 FreeSid(domainsid);
1474 static void test_LookupAccountSid(void)
1476 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
1477 CHAR accountA[MAX_PATH], domainA[MAX_PATH], usernameA[MAX_PATH];
1478 DWORD acc_sizeA, dom_sizeA, user_sizeA;
1479 DWORD real_acc_sizeA, real_dom_sizeA;
1480 WCHAR accountW[MAX_PATH], domainW[MAX_PATH];
1481 DWORD acc_sizeW, dom_sizeW;
1482 DWORD real_acc_sizeW, real_dom_sizeW;
1483 PSID pUsersSid = NULL;
1484 SID_NAME_USE use;
1485 BOOL ret;
1486 DWORD error, size, cbti = 0;
1487 MAX_SID max_sid;
1488 CHAR *str_sidA;
1489 int i;
1490 HANDLE hToken;
1491 PTOKEN_USER ptiUser = NULL;
1493 /* native windows crashes if account size, domain size, or name use is NULL */
1495 ret = AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
1496 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &pUsersSid);
1497 ok(ret || (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED),
1498 "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1500 /* not running on NT so give up */
1501 if (!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1502 return;
1504 real_acc_sizeA = MAX_PATH;
1505 real_dom_sizeA = MAX_PATH;
1506 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &real_acc_sizeA, domainA, &real_dom_sizeA, &use);
1507 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1509 /* try NULL account */
1510 acc_sizeA = MAX_PATH;
1511 dom_sizeA = MAX_PATH;
1512 ret = LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
1513 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1515 /* try NULL domain */
1516 acc_sizeA = MAX_PATH;
1517 dom_sizeA = MAX_PATH;
1518 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
1519 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1521 /* try a small account buffer */
1522 acc_sizeA = 1;
1523 dom_sizeA = MAX_PATH;
1524 accountA[0] = 0;
1525 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1526 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1527 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1528 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1530 /* try a 0 sized account buffer */
1531 acc_sizeA = 0;
1532 dom_sizeA = MAX_PATH;
1533 accountA[0] = 0;
1534 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1535 /* this can fail or succeed depending on OS version but the size will always be returned */
1536 ok(acc_sizeA == real_acc_sizeA + 1,
1537 "LookupAccountSidA() Expected acc_size = %u, got %u\n",
1538 real_acc_sizeA + 1, acc_sizeA);
1540 /* try a 0 sized account buffer */
1541 acc_sizeA = 0;
1542 dom_sizeA = MAX_PATH;
1543 ret = LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
1544 /* this can fail or succeed depending on OS version but the size will always be returned */
1545 ok(acc_sizeA == real_acc_sizeA + 1,
1546 "LookupAccountSid() Expected acc_size = %u, got %u\n",
1547 real_acc_sizeA + 1, acc_sizeA);
1549 /* try a small domain buffer */
1550 dom_sizeA = 1;
1551 acc_sizeA = MAX_PATH;
1552 accountA[0] = 0;
1553 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1554 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1555 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1556 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1558 /* try a 0 sized domain buffer */
1559 dom_sizeA = 0;
1560 acc_sizeA = MAX_PATH;
1561 accountA[0] = 0;
1562 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1563 /* this can fail or succeed depending on OS version but the size will always be returned */
1564 ok(dom_sizeA == real_dom_sizeA + 1,
1565 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1566 real_dom_sizeA + 1, dom_sizeA);
1568 /* try a 0 sized domain buffer */
1569 dom_sizeA = 0;
1570 acc_sizeA = MAX_PATH;
1571 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
1572 /* this can fail or succeed depending on OS version but the size will always be returned */
1573 ok(dom_sizeA == real_dom_sizeA + 1,
1574 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1575 real_dom_sizeA + 1, dom_sizeA);
1577 real_acc_sizeW = MAX_PATH;
1578 real_dom_sizeW = MAX_PATH;
1579 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &real_acc_sizeW, domainW, &real_dom_sizeW, &use);
1580 ok(ret, "LookupAccountSidW() Expected TRUE, got FALSE\n");
1582 /* try an invalid system name */
1583 real_acc_sizeA = MAX_PATH;
1584 real_dom_sizeA = MAX_PATH;
1585 ret = LookupAccountSidA("deepthought", pUsersSid, accountA, &real_acc_sizeA, domainA, &real_dom_sizeA, &use);
1586 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1587 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE || GetLastError() == RPC_S_INVALID_NET_ADDR /* Vista */,
1588 "LookupAccountSidA() Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %u\n", GetLastError());
1590 /* native windows crashes if domainW or accountW is NULL */
1592 /* try a small account buffer */
1593 acc_sizeW = 1;
1594 dom_sizeW = MAX_PATH;
1595 accountW[0] = 0;
1596 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1597 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1598 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1599 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1601 /* try a 0 sized account buffer */
1602 acc_sizeW = 0;
1603 dom_sizeW = MAX_PATH;
1604 accountW[0] = 0;
1605 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1606 /* this can fail or succeed depending on OS version but the size will always be returned */
1607 ok(acc_sizeW == real_acc_sizeW + 1,
1608 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1609 real_acc_sizeW + 1, acc_sizeW);
1611 /* try a 0 sized account buffer */
1612 acc_sizeW = 0;
1613 dom_sizeW = MAX_PATH;
1614 ret = LookupAccountSidW(NULL, pUsersSid, NULL, &acc_sizeW, domainW, &dom_sizeW, &use);
1615 /* this can fail or succeed depending on OS version but the size will always be returned */
1616 ok(acc_sizeW == real_acc_sizeW + 1,
1617 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1618 real_acc_sizeW + 1, acc_sizeW);
1620 /* try a small domain buffer */
1621 dom_sizeW = 1;
1622 acc_sizeW = MAX_PATH;
1623 accountW[0] = 0;
1624 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1625 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1626 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1627 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1629 /* try a 0 sized domain buffer */
1630 dom_sizeW = 0;
1631 acc_sizeW = MAX_PATH;
1632 accountW[0] = 0;
1633 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1634 /* this can fail or succeed depending on OS version but the size will always be returned */
1635 ok(dom_sizeW == real_dom_sizeW + 1,
1636 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1637 real_dom_sizeW + 1, dom_sizeW);
1639 /* try a 0 sized domain buffer */
1640 dom_sizeW = 0;
1641 acc_sizeW = MAX_PATH;
1642 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, NULL, &dom_sizeW, &use);
1643 /* this can fail or succeed depending on OS version but the size will always be returned */
1644 ok(dom_sizeW == real_dom_sizeW + 1,
1645 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1646 real_dom_sizeW + 1, dom_sizeW);
1648 acc_sizeW = dom_sizeW = use = 0;
1649 SetLastError(0xdeadbeef);
1650 ret = LookupAccountSidW(NULL, pUsersSid, NULL, &acc_sizeW, NULL, &dom_sizeW, &use);
1651 error = GetLastError();
1652 ok(!ret, "LookupAccountSidW failed %u\n", GetLastError());
1653 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
1654 ok(acc_sizeW, "expected non-zero account size\n");
1655 ok(dom_sizeW, "expected non-zero domain size\n");
1656 ok(!use, "expected zero use %u\n", use);
1658 FreeSid(pUsersSid);
1660 /* Test LookupAccountSid with Sid retrieved from token information.
1661 This assumes this process is running under the account of the current user.*/
1662 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &hToken);
1663 ret = GetTokenInformation(hToken, TokenUser, NULL, 0, &cbti);
1664 ptiUser = HeapAlloc(GetProcessHeap(), 0, cbti);
1665 if (GetTokenInformation(hToken, TokenUser, ptiUser, cbti, &cbti))
1667 acc_sizeA = dom_sizeA = MAX_PATH;
1668 ret = LookupAccountSidA(NULL, ptiUser->User.Sid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1669 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1670 user_sizeA = MAX_PATH;
1671 ret = GetUserNameA(usernameA , &user_sizeA);
1672 ok(ret, "GetUserNameA() Expected TRUE, got FALSE\n");
1673 todo_wine
1675 ok(lstrcmpA(usernameA, accountA) == 0, "LookupAccountSidA() Expected account name: %s got: %s\n", usernameA, accountA );
1678 HeapFree(GetProcessHeap(), 0, ptiUser);
1680 if (pCreateWellKnownSid && pConvertSidToStringSidA)
1682 trace("Well Known SIDs:\n");
1683 for (i = 0; i <= 60; i++)
1685 size = SECURITY_MAX_SID_SIZE;
1686 if (pCreateWellKnownSid(i, NULL, &max_sid.sid, &size))
1688 if (pConvertSidToStringSidA(&max_sid.sid, &str_sidA))
1690 acc_sizeA = MAX_PATH;
1691 dom_sizeA = MAX_PATH;
1692 if (LookupAccountSidA(NULL, &max_sid.sid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use))
1693 trace(" %d: %s %s\\%s %d\n", i, str_sidA, domainA, accountA, use);
1694 LocalFree(str_sidA);
1697 else
1699 if (GetLastError() != ERROR_INVALID_PARAMETER)
1700 trace(" CreateWellKnownSid(%d) failed: %d\n", i, GetLastError());
1701 else
1702 trace(" %d: not supported\n", i);
1706 pLsaQueryInformationPolicy = (void *)GetProcAddress( hmod, "LsaQueryInformationPolicy");
1707 pLsaOpenPolicy = (void *)GetProcAddress( hmod, "LsaOpenPolicy");
1708 pLsaFreeMemory = (void *)GetProcAddress( hmod, "LsaFreeMemory");
1709 pLsaClose = (void *)GetProcAddress( hmod, "LsaClose");
1711 if (pLsaQueryInformationPolicy && pLsaOpenPolicy && pLsaFreeMemory && pLsaClose)
1713 NTSTATUS status;
1714 LSA_HANDLE handle;
1715 LSA_OBJECT_ATTRIBUTES object_attributes;
1717 ZeroMemory(&object_attributes, sizeof(object_attributes));
1718 object_attributes.Length = sizeof(object_attributes);
1720 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_ALL_ACCESS, &handle);
1721 ok(status == STATUS_SUCCESS || status == STATUS_ACCESS_DENIED,
1722 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status);
1724 /* try a more restricted access mask if necessary */
1725 if (status == STATUS_ACCESS_DENIED) {
1726 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
1727 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_VIEW_LOCAL_INFORMATION, &handle);
1728 ok(status == STATUS_SUCCESS, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status);
1731 if (status == STATUS_SUCCESS)
1733 PPOLICY_ACCOUNT_DOMAIN_INFO info;
1734 status = pLsaQueryInformationPolicy(handle, PolicyAccountDomainInformation, (PVOID*)&info);
1735 ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy() failed, returned 0x%08x\n", status);
1736 if (status == STATUS_SUCCESS)
1738 ok(info->DomainSid!=0, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) missing SID\n");
1739 if (info->DomainSid)
1741 int count = *GetSidSubAuthorityCount(info->DomainSid);
1742 CopySid(GetSidLengthRequired(count), &max_sid, info->DomainSid);
1743 test_sid_str((PSID)&max_sid.sid);
1744 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_ADMIN;
1745 max_sid.sid.SubAuthorityCount = count + 1;
1746 test_sid_str((PSID)&max_sid.sid);
1747 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_GUEST;
1748 test_sid_str((PSID)&max_sid.sid);
1749 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ADMINS;
1750 test_sid_str((PSID)&max_sid.sid);
1751 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_USERS;
1752 test_sid_str((PSID)&max_sid.sid);
1753 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_GUESTS;
1754 test_sid_str((PSID)&max_sid.sid);
1755 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_COMPUTERS;
1756 test_sid_str((PSID)&max_sid.sid);
1757 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CONTROLLERS;
1758 test_sid_str((PSID)&max_sid.sid);
1759 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CERT_ADMINS;
1760 test_sid_str((PSID)&max_sid.sid);
1761 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_SCHEMA_ADMINS;
1762 test_sid_str((PSID)&max_sid.sid);
1763 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ENTERPRISE_ADMINS;
1764 test_sid_str((PSID)&max_sid.sid);
1765 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_POLICY_ADMINS;
1766 test_sid_str((PSID)&max_sid.sid);
1767 max_sid.sid.SubAuthority[count] = DOMAIN_ALIAS_RID_RAS_SERVERS;
1768 test_sid_str((PSID)&max_sid.sid);
1769 max_sid.sid.SubAuthority[count] = 1000; /* first user account */
1770 test_sid_str((PSID)&max_sid.sid);
1773 pLsaFreeMemory((LPVOID)info);
1776 status = pLsaClose(handle);
1777 ok(status == STATUS_SUCCESS, "LsaClose() failed, returned 0x%08x\n", status);
1783 static BOOL get_sid_info(PSID psid, LPSTR *user, LPSTR *dom)
1785 static CHAR account[UNLEN + 1];
1786 static CHAR domain[UNLEN + 1];
1787 DWORD size, dom_size;
1788 SID_NAME_USE use;
1790 *user = account;
1791 *dom = domain;
1793 size = dom_size = UNLEN + 1;
1794 account[0] = '\0';
1795 domain[0] = '\0';
1796 SetLastError(0xdeadbeef);
1797 return LookupAccountSidA(NULL, psid, account, &size, domain, &dom_size, &use);
1800 static void check_wellknown_name(const char* name, WELL_KNOWN_SID_TYPE result)
1802 SID_IDENTIFIER_AUTHORITY ident = { SECURITY_NT_AUTHORITY };
1803 PSID domainsid = NULL;
1804 char wk_sid[SECURITY_MAX_SID_SIZE];
1805 DWORD cb;
1807 DWORD sid_size, domain_size;
1808 SID_NAME_USE sid_use;
1809 LPSTR domain, account, sid_domain, wk_domain, wk_account;
1810 PSID psid;
1811 BOOL ret ,ret2;
1813 sid_size = 0;
1814 domain_size = 0;
1815 ret = LookupAccountNameA(NULL, name, NULL, &sid_size, NULL, &domain_size, &sid_use);
1816 psid = HeapAlloc(GetProcessHeap(),0,sid_size);
1817 domain = HeapAlloc(GetProcessHeap(),0,domain_size);
1818 ret = LookupAccountNameA(NULL, name, psid, &sid_size, domain, &domain_size, &sid_use);
1820 if (!result)
1822 ok(!ret, " %s Should have failed to lookup account name\n",name);
1823 goto cleanup;
1826 AllocateAndInitializeSid(&ident, 6, SECURITY_NT_NON_UNIQUE, 12, 23, 34, 45, 56, 0, 0, &domainsid);
1827 cb = sizeof(wk_sid);
1828 if (!pCreateWellKnownSid(result, domainsid, wk_sid, &cb))
1830 win_skip("SID %i is not available on the system\n",result);
1831 goto cleanup;
1834 ret2 = get_sid_info(wk_sid, &wk_account, &wk_domain);
1835 if (!ret2 && GetLastError() == ERROR_NONE_MAPPED)
1837 win_skip("CreateWellKnownSid() succeeded but the account '%s' is not present (W2K)\n", name);
1838 goto cleanup;
1841 get_sid_info(psid, &account, &sid_domain);
1843 ok(ret, "Failed to lookup account name %s\n",name);
1844 ok(sid_size != 0, "sid_size was zero\n");
1846 ok(EqualSid(psid,wk_sid),"(%s) Sids fail to match well known sid!\n",name);
1848 ok(!lstrcmp(account, wk_account), "Expected %s , got %s\n", account, wk_account);
1849 ok(!lstrcmp(domain, wk_domain), "Expected %s, got %s\n", wk_domain, domain);
1850 ok(sid_use == SidTypeWellKnownGroup , "Expected Use (5), got %d\n", sid_use);
1852 cleanup:
1853 FreeSid(domainsid);
1854 HeapFree(GetProcessHeap(),0,psid);
1855 HeapFree(GetProcessHeap(),0,domain);
1858 static void test_LookupAccountName(void)
1860 DWORD sid_size, domain_size, user_size;
1861 DWORD sid_save, domain_save;
1862 CHAR user_name[UNLEN + 1];
1863 CHAR computer_name[UNLEN + 1];
1864 SID_NAME_USE sid_use;
1865 LPSTR domain, account, sid_dom;
1866 PSID psid;
1867 BOOL ret;
1869 /* native crashes if (assuming all other parameters correct):
1870 * - peUse is NULL
1871 * - Sid is NULL and cbSid is > 0
1872 * - cbSid or cchReferencedDomainName are NULL
1873 * - ReferencedDomainName is NULL and cchReferencedDomainName is the correct size
1876 user_size = UNLEN + 1;
1877 SetLastError(0xdeadbeef);
1878 ret = GetUserNameA(user_name, &user_size);
1879 if (!ret && (GetLastError() == ERROR_NOT_LOGGED_ON))
1881 /* Probably on win9x where the user used 'Cancel' instead of properly logging in */
1882 skip("Cannot get the user name (win9x and not logged in properly)\n");
1883 return;
1885 ok(ret, "Failed to get user name : %d\n", GetLastError());
1887 /* get sizes */
1888 sid_size = 0;
1889 domain_size = 0;
1890 sid_use = 0xcafebabe;
1891 SetLastError(0xdeadbeef);
1892 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, NULL, &domain_size, &sid_use);
1893 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1895 win_skip("LookupAccountNameA is not implemented\n");
1896 return;
1898 ok(!ret, "Expected 0, got %d\n", ret);
1899 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1900 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1901 ok(sid_size != 0, "Expected non-zero sid size\n");
1902 ok(domain_size != 0, "Expected non-zero domain size\n");
1903 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
1905 sid_save = sid_size;
1906 domain_save = domain_size;
1908 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
1909 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
1911 /* try valid account name */
1912 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, domain, &domain_size, &sid_use);
1913 get_sid_info(psid, &account, &sid_dom);
1914 ok(ret, "Failed to lookup account name\n");
1915 ok(sid_size == GetLengthSid(psid), "Expected %d, got %d\n", GetLengthSid(psid), sid_size);
1916 todo_wine
1918 ok(!lstrcmp(account, user_name), "Expected %s, got %s\n", user_name, account);
1919 ok(!lstrcmp(domain, sid_dom), "Expected %s, got %s\n", sid_dom, domain);
1921 ok(domain_size == domain_save - 1, "Expected %d, got %d\n", domain_save - 1, domain_size);
1922 ok(strlen(domain) == domain_size, "Expected %d, got %d\n", lstrlen(domain), domain_size);
1923 ok(sid_use == SidTypeUser, "Expected SidTypeUser (%d), got %d\n", SidTypeUser, sid_use);
1924 domain_size = domain_save;
1925 sid_size = sid_save;
1927 if (PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale())) != LANG_ENGLISH)
1929 skip("Non-english locale (test with hardcoded 'Everyone')\n");
1931 else
1933 ret = LookupAccountNameA(NULL, "Everyone", psid, &sid_size, domain, &domain_size, &sid_use);
1934 get_sid_info(psid, &account, &sid_dom);
1935 ok(ret, "Failed to lookup account name\n");
1936 ok(sid_size != 0, "sid_size was zero\n");
1937 ok(!lstrcmp(account, "Everyone"), "Expected Everyone, got %s\n", account);
1938 ok(!lstrcmp(domain, sid_dom), "Expected %s, got %s\n", sid_dom, domain);
1939 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
1940 ok(strlen(domain) == domain_size, "Expected %d, got %d\n", lstrlen(domain), domain_size);
1941 ok(sid_use == SidTypeWellKnownGroup, "Expected SidTypeWellKnownGroup (%d), got %d\n", SidTypeWellKnownGroup, sid_use);
1942 domain_size = domain_save;
1945 /* NULL Sid with zero sid size */
1946 SetLastError(0xdeadbeef);
1947 sid_size = 0;
1948 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
1949 ok(!ret, "Expected 0, got %d\n", ret);
1950 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1951 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1952 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1953 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1955 /* try cchReferencedDomainName - 1 */
1956 SetLastError(0xdeadbeef);
1957 domain_size--;
1958 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
1959 ok(!ret, "Expected 0, got %d\n", ret);
1960 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1961 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1962 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1963 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1965 /* NULL ReferencedDomainName with zero domain name size */
1966 SetLastError(0xdeadbeef);
1967 domain_size = 0;
1968 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, NULL, &domain_size, &sid_use);
1969 ok(!ret, "Expected 0, got %d\n", ret);
1970 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1971 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1972 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1973 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1975 HeapFree(GetProcessHeap(), 0, psid);
1976 HeapFree(GetProcessHeap(), 0, domain);
1978 /* get sizes for NULL account name */
1979 sid_size = 0;
1980 domain_size = 0;
1981 sid_use = 0xcafebabe;
1982 SetLastError(0xdeadbeef);
1983 ret = LookupAccountNameA(NULL, NULL, NULL, &sid_size, NULL, &domain_size, &sid_use);
1984 if (!ret && GetLastError() == ERROR_NONE_MAPPED)
1985 win_skip("NULL account name doesn't work on NT4\n");
1986 else
1988 ok(!ret, "Expected 0, got %d\n", ret);
1989 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1990 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1991 ok(sid_size != 0, "Expected non-zero sid size\n");
1992 ok(domain_size != 0, "Expected non-zero domain size\n");
1993 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
1995 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
1996 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
1998 /* try NULL account name */
1999 ret = LookupAccountNameA(NULL, NULL, psid, &sid_size, domain, &domain_size, &sid_use);
2000 get_sid_info(psid, &account, &sid_dom);
2001 ok(ret, "Failed to lookup account name\n");
2002 /* Using a fixed string will not work on different locales */
2003 ok(!lstrcmp(account, domain),
2004 "Got %s for account and %s for domain, these should be the same\n",
2005 account, domain);
2006 ok(sid_use == SidTypeDomain, "Expected SidTypeDomain (%d), got %d\n", SidTypeDomain, sid_use);
2008 HeapFree(GetProcessHeap(), 0, psid);
2009 HeapFree(GetProcessHeap(), 0, domain);
2012 /* try an invalid account name */
2013 SetLastError(0xdeadbeef);
2014 sid_size = 0;
2015 domain_size = 0;
2016 ret = LookupAccountNameA(NULL, "oogabooga", NULL, &sid_size, NULL, &domain_size, &sid_use);
2017 ok(!ret, "Expected 0, got %d\n", ret);
2018 ok(GetLastError() == ERROR_NONE_MAPPED ||
2019 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE),
2020 "Expected ERROR_NONE_MAPPED, got %d\n", GetLastError());
2021 ok(sid_size == 0, "Expected 0, got %d\n", sid_size);
2022 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
2024 /* try an invalid system name */
2025 SetLastError(0xdeadbeef);
2026 sid_size = 0;
2027 domain_size = 0;
2028 ret = LookupAccountNameA("deepthought", NULL, NULL, &sid_size, NULL, &domain_size, &sid_use);
2029 ok(!ret, "Expected 0, got %d\n", ret);
2030 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE || GetLastError() == RPC_S_INVALID_NET_ADDR /* Vista */,
2031 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
2032 ok(sid_size == 0, "Expected 0, got %d\n", sid_size);
2033 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
2035 /* try with the computer name as the account name */
2036 domain_size = sizeof(computer_name);
2037 GetComputerNameA(computer_name, &domain_size);
2038 sid_size = 0;
2039 domain_size = 0;
2040 ret = LookupAccountNameA(NULL, computer_name, NULL, &sid_size, NULL, &domain_size, &sid_use);
2041 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER ||
2042 GetLastError() == ERROR_NONE_MAPPED /* in a domain */ ||
2043 broken(GetLastError() == ERROR_TRUSTED_DOMAIN_FAILURE) ||
2044 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE)),
2045 "LookupAccountNameA failed: %d\n", GetLastError());
2046 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
2048 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
2049 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
2050 ret = LookupAccountNameA(NULL, computer_name, psid, &sid_size, domain, &domain_size, &sid_use);
2051 ok(ret, "LookupAccountNameA failed: %d\n", GetLastError());
2052 ok(sid_use == SidTypeDomain ||
2053 (sid_use == SidTypeUser && ! strcmp(computer_name, user_name)), "expected SidTypeDomain for %s, got %d\n", computer_name, sid_use);
2054 HeapFree(GetProcessHeap(), 0, domain);
2055 HeapFree(GetProcessHeap(), 0, psid);
2058 /* Well Known names */
2059 if (!pCreateWellKnownSid)
2061 win_skip("CreateWellKnownSid not available\n");
2062 return;
2065 if (PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale())) != LANG_ENGLISH)
2067 skip("Non-english locale (skipping well known name creation tests)\n");
2068 return;
2071 check_wellknown_name("LocalService", WinLocalServiceSid);
2072 check_wellknown_name("Local Service", WinLocalServiceSid);
2073 /* 2 spaces */
2074 check_wellknown_name("Local Service", 0);
2075 check_wellknown_name("NetworkService", WinNetworkServiceSid);
2076 check_wellknown_name("Network Service", WinNetworkServiceSid);
2078 /* example of some names where the spaces are not optional */
2079 check_wellknown_name("Terminal Server User", WinTerminalServerSid);
2080 check_wellknown_name("TerminalServer User", 0);
2081 check_wellknown_name("TerminalServerUser", 0);
2082 check_wellknown_name("Terminal ServerUser", 0);
2084 check_wellknown_name("enterprise domain controllers",WinEnterpriseControllersSid);
2085 check_wellknown_name("enterprisedomain controllers", 0);
2086 check_wellknown_name("enterprise domaincontrollers", 0);
2087 check_wellknown_name("enterprisedomaincontrollers", 0);
2089 /* case insensitivity */
2090 check_wellknown_name("lOCAlServICE", WinLocalServiceSid);
2092 /* fully qualified account names */
2093 check_wellknown_name("NT AUTHORITY\\LocalService", WinLocalServiceSid);
2094 check_wellknown_name("nt authority\\Network Service", WinNetworkServiceSid);
2095 check_wellknown_name("nt authority test\\Network Service", 0);
2096 check_wellknown_name("Dummy\\Network Service", 0);
2097 check_wellknown_name("ntauthority\\Network Service", 0);
2100 static void test_security_descriptor(void)
2102 SECURITY_DESCRIPTOR sd;
2103 char buf[8192];
2104 DWORD size;
2105 BOOL isDefault, isPresent, ret;
2106 PACL pacl;
2107 PSID psid;
2109 SetLastError(0xdeadbeef);
2110 ret = InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
2111 if (ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2113 win_skip("InitializeSecurityDescriptor is not implemented\n");
2114 return;
2117 ok(GetSecurityDescriptorOwner(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
2118 expect_eq(psid, NULL, PSID, "%p");
2119 expect_eq(isDefault, FALSE, BOOL, "%d");
2120 sd.Control |= SE_DACL_PRESENT | SE_SACL_PRESENT;
2122 SetLastError(0xdeadbeef);
2123 size = 5;
2124 expect_eq(MakeSelfRelativeSD(&sd, buf, &size), FALSE, BOOL, "%d");
2125 expect_eq(GetLastError(), ERROR_INSUFFICIENT_BUFFER, DWORD, "%u");
2126 ok(size > 5, "Size not increased\n");
2127 if (size <= 8192)
2129 expect_eq(MakeSelfRelativeSD(&sd, buf, &size), TRUE, BOOL, "%d");
2130 ok(GetSecurityDescriptorOwner(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
2131 expect_eq(psid, NULL, PSID, "%p");
2132 expect_eq(isDefault, FALSE, BOOL, "%d");
2133 ok(GetSecurityDescriptorGroup(&sd, &psid, &isDefault), "GetSecurityDescriptorGroup failed\n");
2134 expect_eq(psid, NULL, PSID, "%p");
2135 expect_eq(isDefault, FALSE, BOOL, "%d");
2136 ok(GetSecurityDescriptorDacl(&sd, &isPresent, &pacl, &isDefault), "GetSecurityDescriptorDacl failed\n");
2137 expect_eq(isPresent, TRUE, BOOL, "%d");
2138 expect_eq(psid, NULL, PSID, "%p");
2139 expect_eq(isDefault, FALSE, BOOL, "%d");
2140 ok(GetSecurityDescriptorSacl(&sd, &isPresent, &pacl, &isDefault), "GetSecurityDescriptorSacl failed\n");
2141 expect_eq(isPresent, TRUE, BOOL, "%d");
2142 expect_eq(psid, NULL, PSID, "%p");
2143 expect_eq(isDefault, FALSE, BOOL, "%d");
2147 #define TEST_GRANTED_ACCESS(a,b) test_granted_access(a,b,0,__LINE__)
2148 #define TEST_GRANTED_ACCESS2(a,b,c) test_granted_access(a,b,c,__LINE__)
2149 static void test_granted_access(HANDLE handle, ACCESS_MASK access,
2150 ACCESS_MASK alt, int line)
2152 OBJECT_BASIC_INFORMATION obj_info;
2153 NTSTATUS status;
2155 if (!pNtQueryObject)
2157 skip_(__FILE__, line)("Not NT platform - skipping tests\n");
2158 return;
2161 status = pNtQueryObject( handle, ObjectBasicInformation, &obj_info,
2162 sizeof(obj_info), NULL );
2163 ok_(__FILE__, line)(!status, "NtQueryObject with err: %08x\n", status);
2164 if (alt)
2165 ok_(__FILE__, line)(obj_info.GrantedAccess == access ||
2166 obj_info.GrantedAccess == alt, "Granted access should be 0x%08x "
2167 "or 0x%08x, instead of 0x%08x\n", access, alt, obj_info.GrantedAccess);
2168 else
2169 ok_(__FILE__, line)(obj_info.GrantedAccess == access, "Granted access should "
2170 "be 0x%08x, instead of 0x%08x\n", access, obj_info.GrantedAccess);
2173 #define CHECK_SET_SECURITY(o,i,e) \
2174 do{ \
2175 BOOL res; \
2176 DWORD err; \
2177 SetLastError( 0xdeadbeef ); \
2178 res = SetKernelObjectSecurity( o, i, SecurityDescriptor ); \
2179 err = GetLastError(); \
2180 if (e == ERROR_SUCCESS) \
2181 ok(res, "SetKernelObjectSecurity failed with %d\n", err); \
2182 else \
2183 ok(!res && err == e, "SetKernelObjectSecurity should have failed " \
2184 "with %s, instead of %d\n", #e, err); \
2185 }while(0)
2187 static void test_process_security(void)
2189 BOOL res;
2190 PTOKEN_OWNER owner;
2191 PTOKEN_PRIMARY_GROUP group;
2192 PSID AdminSid = NULL, UsersSid = NULL;
2193 PACL Acl = NULL;
2194 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL;
2195 char buffer[MAX_PATH];
2196 PROCESS_INFORMATION info;
2197 STARTUPINFOA startup;
2198 SECURITY_ATTRIBUTES psa;
2199 HANDLE token, event;
2200 DWORD size;
2202 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
2203 res = InitializeAcl(Acl, 256, ACL_REVISION);
2204 if (!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2206 win_skip("ACLs not implemented - skipping tests\n");
2207 HeapFree(GetProcessHeap(), 0, Acl);
2208 return;
2210 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
2212 /* get owner from the token we might be running as a user not admin */
2213 res = OpenProcessToken( GetCurrentProcess(), MAXIMUM_ALLOWED, &token );
2214 ok(res, "OpenProcessToken failed with error %d\n", GetLastError());
2215 if (!res)
2217 HeapFree(GetProcessHeap(), 0, Acl);
2218 return;
2221 res = GetTokenInformation( token, TokenOwner, NULL, 0, &size );
2222 ok(!res, "Expected failure, got %d\n", res);
2223 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2224 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2226 owner = HeapAlloc(GetProcessHeap(), 0, size);
2227 res = GetTokenInformation( token, TokenOwner, owner, size, &size );
2228 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
2229 AdminSid = ((TOKEN_OWNER*)owner)->Owner;
2231 res = GetTokenInformation( token, TokenPrimaryGroup, NULL, 0, &size );
2232 ok(!res, "Expected failure, got %d\n", res);
2233 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2234 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2236 group = HeapAlloc(GetProcessHeap(), 0, size);
2237 res = GetTokenInformation( token, TokenPrimaryGroup, group, size, &size );
2238 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
2239 UsersSid = ((TOKEN_PRIMARY_GROUP*)group)->PrimaryGroup;
2241 CloseHandle( token );
2242 if (!res)
2244 HeapFree(GetProcessHeap(), 0, group);
2245 HeapFree(GetProcessHeap(), 0, owner);
2246 HeapFree(GetProcessHeap(), 0, Acl);
2247 return;
2250 res = AddAccessDeniedAce(Acl, ACL_REVISION, PROCESS_VM_READ, AdminSid);
2251 ok(res, "AddAccessDeniedAce failed with error %d\n", GetLastError());
2252 res = AddAccessAllowedAce(Acl, ACL_REVISION, PROCESS_ALL_ACCESS, AdminSid);
2253 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2255 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
2256 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
2257 ok(res, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
2259 event = CreateEvent( NULL, TRUE, TRUE, "test_event" );
2260 ok(event != NULL, "CreateEvent %d\n", GetLastError());
2262 SecurityDescriptor->Revision = 0;
2263 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_UNKNOWN_REVISION );
2264 SecurityDescriptor->Revision = SECURITY_DESCRIPTOR_REVISION;
2266 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_INVALID_SECURITY_DESCR );
2267 CHECK_SET_SECURITY( event, GROUP_SECURITY_INFORMATION, ERROR_INVALID_SECURITY_DESCR );
2268 CHECK_SET_SECURITY( event, SACL_SECURITY_INFORMATION, ERROR_ACCESS_DENIED );
2269 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
2270 /* NULL DACL is valid and means default DACL from token */
2271 SecurityDescriptor->Control |= SE_DACL_PRESENT;
2272 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
2274 /* Set owner and group and dacl */
2275 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
2276 ok(res, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
2277 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_SUCCESS );
2278 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, FALSE);
2279 ok(res, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2280 CHECK_SET_SECURITY( event, GROUP_SECURITY_INFORMATION, ERROR_SUCCESS );
2281 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
2282 ok(res, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
2283 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
2285 sprintf(buffer, "%s tests/security.c test", myARGV[0]);
2286 memset(&startup, 0, sizeof(startup));
2287 startup.cb = sizeof(startup);
2288 startup.dwFlags = STARTF_USESHOWWINDOW;
2289 startup.wShowWindow = SW_SHOWNORMAL;
2291 psa.nLength = sizeof(psa);
2292 psa.lpSecurityDescriptor = SecurityDescriptor;
2293 psa.bInheritHandle = TRUE;
2295 /* Doesn't matter what ACL say we should get full access for ourselves */
2296 ok(CreateProcessA( NULL, buffer, &psa, NULL, FALSE, 0, NULL, NULL, &startup, &info ),
2297 "CreateProcess with err:%d\n", GetLastError());
2298 TEST_GRANTED_ACCESS2( info.hProcess, PROCESS_ALL_ACCESS,
2299 STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL );
2300 winetest_wait_child_process( info.hProcess );
2302 CloseHandle( info.hProcess );
2303 CloseHandle( info.hThread );
2304 CloseHandle( event );
2305 HeapFree(GetProcessHeap(), 0, group);
2306 HeapFree(GetProcessHeap(), 0, owner);
2307 HeapFree(GetProcessHeap(), 0, Acl);
2308 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
2311 static void test_process_security_child(void)
2313 HANDLE handle, handle1;
2314 BOOL ret;
2315 DWORD err;
2317 handle = OpenProcess( PROCESS_TERMINATE, FALSE, GetCurrentProcessId() );
2318 ok(handle != NULL, "OpenProcess(PROCESS_TERMINATE) with err:%d\n", GetLastError());
2319 TEST_GRANTED_ACCESS( handle, PROCESS_TERMINATE );
2321 ok(DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
2322 &handle1, 0, TRUE, DUPLICATE_SAME_ACCESS ),
2323 "duplicating handle err:%d\n", GetLastError());
2324 TEST_GRANTED_ACCESS( handle1, PROCESS_TERMINATE );
2326 CloseHandle( handle1 );
2328 SetLastError( 0xdeadbeef );
2329 ret = DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
2330 &handle1, PROCESS_ALL_ACCESS, TRUE, 0 );
2331 err = GetLastError();
2332 todo_wine
2333 ok(!ret && err == ERROR_ACCESS_DENIED, "duplicating handle should have failed "
2334 "with STATUS_ACCESS_DENIED, instead of err:%d\n", err);
2336 CloseHandle( handle );
2338 /* These two should fail - they are denied by ACL */
2339 handle = OpenProcess( PROCESS_VM_READ, FALSE, GetCurrentProcessId() );
2340 todo_wine
2341 ok(handle == NULL, "OpenProcess(PROCESS_VM_READ) should have failed\n");
2342 handle = OpenProcess( PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId() );
2343 todo_wine
2344 ok(handle == NULL, "OpenProcess(PROCESS_ALL_ACCESS) should have failed\n");
2346 /* Documented privilege elevation */
2347 ok(DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2348 &handle, 0, TRUE, DUPLICATE_SAME_ACCESS ),
2349 "duplicating handle err:%d\n", GetLastError());
2350 TEST_GRANTED_ACCESS2( handle, PROCESS_ALL_ACCESS,
2351 STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL );
2353 CloseHandle( handle );
2355 /* Same only explicitly asking for all access rights */
2356 ok(DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2357 &handle, PROCESS_ALL_ACCESS, TRUE, 0 ),
2358 "duplicating handle err:%d\n", GetLastError());
2359 TEST_GRANTED_ACCESS2( handle, PROCESS_ALL_ACCESS,
2360 PROCESS_ALL_ACCESS | PROCESS_QUERY_LIMITED_INFORMATION );
2361 ok(DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
2362 &handle1, PROCESS_VM_READ, TRUE, 0 ),
2363 "duplicating handle err:%d\n", GetLastError());
2364 TEST_GRANTED_ACCESS( handle1, PROCESS_VM_READ );
2365 CloseHandle( handle1 );
2366 CloseHandle( handle );
2369 static void test_impersonation_level(void)
2371 HANDLE Token, ProcessToken;
2372 HANDLE Token2;
2373 DWORD Size;
2374 TOKEN_PRIVILEGES *Privileges;
2375 TOKEN_USER *User;
2376 PRIVILEGE_SET *PrivilegeSet;
2377 BOOL AccessGranted;
2378 BOOL ret;
2379 HKEY hkey;
2380 DWORD error;
2382 pDuplicateTokenEx = (void *)GetProcAddress(hmod, "DuplicateTokenEx");
2383 if( !pDuplicateTokenEx ) {
2384 win_skip("DuplicateTokenEx is not available\n");
2385 return;
2387 SetLastError(0xdeadbeef);
2388 ret = ImpersonateSelf(SecurityAnonymous);
2389 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2391 win_skip("ImpersonateSelf is not implemented\n");
2392 return;
2394 ok(ret, "ImpersonateSelf(SecurityAnonymous) failed with error %d\n", GetLastError());
2395 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
2396 ok(!ret, "OpenThreadToken should have failed\n");
2397 error = GetLastError();
2398 ok(error == ERROR_CANT_OPEN_ANONYMOUS, "OpenThreadToken on anonymous token should have returned ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error);
2399 /* can't perform access check when opening object against an anonymous impersonation token */
2400 todo_wine {
2401 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
2402 ok(error == ERROR_INVALID_HANDLE || error == ERROR_CANT_OPEN_ANONYMOUS,
2403 "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE or ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error);
2405 RevertToSelf();
2407 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, &ProcessToken);
2408 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
2410 ret = pDuplicateTokenEx(ProcessToken,
2411 TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_IMPERSONATE, NULL,
2412 SecurityAnonymous, TokenImpersonation, &Token);
2413 ok(ret, "DuplicateTokenEx failed with error %d\n", GetLastError());
2414 /* can't increase the impersonation level */
2415 ret = DuplicateToken(Token, SecurityIdentification, &Token2);
2416 error = GetLastError();
2417 ok(!ret && error == ERROR_BAD_IMPERSONATION_LEVEL,
2418 "Duplicating a token and increasing the impersonation level should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
2419 /* we can query anything from an anonymous token, including the user */
2420 ret = GetTokenInformation(Token, TokenUser, NULL, 0, &Size);
2421 error = GetLastError();
2422 ok(!ret && error == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenUser) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error);
2423 User = HeapAlloc(GetProcessHeap(), 0, Size);
2424 ret = GetTokenInformation(Token, TokenUser, User, Size, &Size);
2425 ok(ret, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
2426 HeapFree(GetProcessHeap(), 0, User);
2428 /* PrivilegeCheck fails with SecurityAnonymous level */
2429 ret = GetTokenInformation(Token, TokenPrivileges, NULL, 0, &Size);
2430 error = GetLastError();
2431 ok(!ret && error == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenPrivileges) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error);
2432 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
2433 ret = GetTokenInformation(Token, TokenPrivileges, Privileges, Size, &Size);
2434 ok(ret, "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
2436 PrivilegeSet = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PRIVILEGE_SET, Privilege[Privileges->PrivilegeCount]));
2437 PrivilegeSet->PrivilegeCount = Privileges->PrivilegeCount;
2438 memcpy(PrivilegeSet->Privilege, Privileges->Privileges, PrivilegeSet->PrivilegeCount * sizeof(PrivilegeSet->Privilege[0]));
2439 PrivilegeSet->Control = PRIVILEGE_SET_ALL_NECESSARY;
2440 HeapFree(GetProcessHeap(), 0, Privileges);
2442 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2443 error = GetLastError();
2444 ok(!ret && error == ERROR_BAD_IMPERSONATION_LEVEL, "PrivilegeCheck for SecurityAnonymous token should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
2446 CloseHandle(Token);
2448 ret = ImpersonateSelf(SecurityIdentification);
2449 ok(ret, "ImpersonateSelf(SecurityIdentification) failed with error %d\n", GetLastError());
2450 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
2451 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
2453 /* can't perform access check when opening object against an identification impersonation token */
2454 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
2455 todo_wine {
2456 ok(error == ERROR_INVALID_HANDLE || error == ERROR_BAD_IMPERSONATION_LEVEL,
2457 "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE or ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
2459 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2460 ok(ret, "PrivilegeCheck for SecurityIdentification failed with error %d\n", GetLastError());
2461 CloseHandle(Token);
2462 RevertToSelf();
2464 ret = ImpersonateSelf(SecurityImpersonation);
2465 ok(ret, "ImpersonateSelf(SecurityImpersonation) failed with error %d\n", GetLastError());
2466 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
2467 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
2468 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
2469 ok(error == ERROR_SUCCESS, "RegOpenKeyEx should have succeeded instead of failing with %d\n", error);
2470 RegCloseKey(hkey);
2471 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2472 ok(ret, "PrivilegeCheck for SecurityImpersonation failed with error %d\n", GetLastError());
2473 RevertToSelf();
2475 CloseHandle(Token);
2476 CloseHandle(ProcessToken);
2478 HeapFree(GetProcessHeap(), 0, PrivilegeSet);
2481 static void test_SetEntriesInAcl(void)
2483 DWORD res;
2484 PSID EveryoneSid = NULL, UsersSid = NULL;
2485 PACL OldAcl = NULL, NewAcl;
2486 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
2487 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
2488 EXPLICIT_ACCESSW ExplicitAccess;
2489 static const WCHAR wszEveryone[] = {'E','v','e','r','y','o','n','e',0};
2490 static const WCHAR wszCurrentUser[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
2492 if (!pSetEntriesInAclW)
2494 win_skip("SetEntriesInAclW is not available\n");
2495 return;
2498 NewAcl = (PACL)0xdeadbeef;
2499 res = pSetEntriesInAclW(0, NULL, NULL, &NewAcl);
2500 if(res == ERROR_CALL_NOT_IMPLEMENTED)
2502 win_skip("SetEntriesInAclW is not implemented\n");
2503 return;
2505 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2506 ok(NewAcl == NULL ||
2507 broken(NewAcl != NULL), /* NT4 */
2508 "NewAcl=%p, expected NULL\n", NewAcl);
2509 LocalFree(NewAcl);
2511 OldAcl = HeapAlloc(GetProcessHeap(), 0, 256);
2512 res = InitializeAcl(OldAcl, 256, ACL_REVISION);
2513 if(!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2515 win_skip("ACLs not implemented - skipping tests\n");
2516 HeapFree(GetProcessHeap(), 0, OldAcl);
2517 return;
2519 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
2521 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
2522 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2524 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
2525 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &UsersSid);
2526 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2528 res = AddAccessAllowedAce(OldAcl, ACL_REVISION, KEY_READ, UsersSid);
2529 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2531 ExplicitAccess.grfAccessPermissions = KEY_WRITE;
2532 ExplicitAccess.grfAccessMode = GRANT_ACCESS;
2533 ExplicitAccess.grfInheritance = NO_INHERITANCE;
2534 ExplicitAccess.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
2535 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
2536 ExplicitAccess.Trustee.ptstrName = EveryoneSid;
2537 ExplicitAccess.Trustee.MultipleTrusteeOperation = 0xDEADBEEF;
2538 ExplicitAccess.Trustee.pMultipleTrustee = (PVOID)0xDEADBEEF;
2539 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2540 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2541 ok(NewAcl != NULL, "returned acl was NULL\n");
2542 LocalFree(NewAcl);
2544 ExplicitAccess.Trustee.TrusteeType = TRUSTEE_IS_UNKNOWN;
2545 ExplicitAccess.Trustee.pMultipleTrustee = NULL;
2546 ExplicitAccess.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2547 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2548 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2549 ok(NewAcl != NULL, "returned acl was NULL\n");
2550 LocalFree(NewAcl);
2552 if (PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale())) != LANG_ENGLISH)
2554 skip("Non-english locale (test with hardcoded 'Everyone')\n");
2556 else
2558 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_USER;
2559 ExplicitAccess.Trustee.ptstrName = (LPWSTR)wszEveryone;
2560 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2561 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2562 ok(NewAcl != NULL, "returned acl was NULL\n");
2563 LocalFree(NewAcl);
2565 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_BAD_FORM;
2566 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2567 ok(res == ERROR_INVALID_PARAMETER ||
2568 broken(res == ERROR_NOT_SUPPORTED), /* NT4 */
2569 "SetEntriesInAclW failed: %u\n", res);
2570 ok(NewAcl == NULL ||
2571 broken(NewAcl != NULL), /* NT4 */
2572 "returned acl wasn't NULL: %p\n", NewAcl);
2574 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_USER;
2575 ExplicitAccess.Trustee.MultipleTrusteeOperation = TRUSTEE_IS_IMPERSONATE;
2576 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2577 ok(res == ERROR_INVALID_PARAMETER ||
2578 broken(res == ERROR_NOT_SUPPORTED), /* NT4 */
2579 "SetEntriesInAclW failed: %u\n", res);
2580 ok(NewAcl == NULL ||
2581 broken(NewAcl != NULL), /* NT4 */
2582 "returned acl wasn't NULL: %p\n", NewAcl);
2584 ExplicitAccess.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2585 ExplicitAccess.grfAccessMode = SET_ACCESS;
2586 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2587 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2588 ok(NewAcl != NULL, "returned acl was NULL\n");
2589 LocalFree(NewAcl);
2592 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_USER;
2593 ExplicitAccess.Trustee.ptstrName = (LPWSTR)wszCurrentUser;
2594 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2595 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2596 ok(NewAcl != NULL, "returned acl was NULL\n");
2597 LocalFree(NewAcl);
2599 ExplicitAccess.grfAccessMode = REVOKE_ACCESS;
2600 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
2601 ExplicitAccess.Trustee.ptstrName = UsersSid;
2602 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2603 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2604 ok(NewAcl != NULL, "returned acl was NULL\n");
2605 LocalFree(NewAcl);
2607 FreeSid(UsersSid);
2608 FreeSid(EveryoneSid);
2609 HeapFree(GetProcessHeap(), 0, OldAcl);
2612 static void test_GetNamedSecurityInfoA(void)
2614 PSECURITY_DESCRIPTOR pSecDesc;
2615 DWORD revision;
2616 SECURITY_DESCRIPTOR_CONTROL control;
2617 PSID owner;
2618 PSID group;
2619 PACL dacl;
2620 BOOL owner_defaulted;
2621 BOOL group_defaulted;
2622 DWORD error;
2623 BOOL ret, isNT4;
2624 CHAR windows_dir[MAX_PATH];
2626 if (!pGetNamedSecurityInfoA)
2628 win_skip("GetNamedSecurityInfoA is not available\n");
2629 return;
2632 ret = GetWindowsDirectoryA(windows_dir, MAX_PATH);
2633 ok(ret, "GetWindowsDirectory failed with error %d\n", GetLastError());
2635 SetLastError(0xdeadbeef);
2636 error = pGetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,
2637 OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION,
2638 NULL, NULL, NULL, NULL, &pSecDesc);
2639 if (error != ERROR_SUCCESS && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2641 win_skip("GetNamedSecurityInfoA is not implemented\n");
2642 return;
2644 ok(!error, "GetNamedSecurityInfo failed with error %d\n", error);
2646 ret = GetSecurityDescriptorControl(pSecDesc, &control, &revision);
2647 ok(ret, "GetSecurityDescriptorControl failed with error %d\n", GetLastError());
2648 ok((control & (SE_SELF_RELATIVE|SE_DACL_PRESENT)) == (SE_SELF_RELATIVE|SE_DACL_PRESENT) ||
2649 broken((control & (SE_SELF_RELATIVE|SE_DACL_PRESENT)) == SE_DACL_PRESENT), /* NT4 */
2650 "control (0x%x) doesn't have (SE_SELF_RELATIVE|SE_DACL_PRESENT) flags set\n", control);
2651 ok(revision == SECURITY_DESCRIPTOR_REVISION1, "revision was %d instead of 1\n", revision);
2653 isNT4 = (control & (SE_SELF_RELATIVE|SE_DACL_PRESENT)) == SE_DACL_PRESENT;
2655 ret = GetSecurityDescriptorOwner(pSecDesc, &owner, &owner_defaulted);
2656 ok(ret, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
2657 ok(owner != NULL, "owner should not be NULL\n");
2659 ret = GetSecurityDescriptorGroup(pSecDesc, &group, &group_defaulted);
2660 ok(ret, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2661 ok(group != NULL, "group should not be NULL\n");
2662 LocalFree(pSecDesc);
2665 /* NULL descriptor tests */
2666 if(isNT4)
2668 win_skip("NT4 does not support GetNamedSecutityInfo with a NULL descriptor\n");
2669 return;
2672 error = pGetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,DACL_SECURITY_INFORMATION,
2673 NULL, NULL, NULL, NULL, NULL);
2674 ok(error==ERROR_INVALID_PARAMETER, "GetNamedSecurityInfo failed with error %d\n", error);
2676 error = pGetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,DACL_SECURITY_INFORMATION,
2677 NULL, NULL, &dacl, NULL, NULL);
2678 ok(!error, "GetNamedSecurityInfo failed with error %d\n", error);
2679 ok(dacl != NULL, "dacl should not be NULL\n");
2681 error = pGetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,OWNER_SECURITY_INFORMATION,
2682 NULL, NULL, &dacl, NULL, NULL);
2683 ok(error==ERROR_INVALID_PARAMETER, "GetNamedSecurityInfo failed with error %d\n", error);
2686 static void test_ConvertStringSecurityDescriptor(void)
2688 BOOL ret;
2689 PSECURITY_DESCRIPTOR pSD;
2690 static const WCHAR Blank[] = { 0 };
2691 unsigned int i;
2692 static const struct
2694 const char *sidstring;
2695 DWORD revision;
2696 BOOL ret;
2697 DWORD GLE;
2698 DWORD altGLE;
2699 } cssd[] =
2701 { "D:(A;;GA;;;WD)", 0xdeadbeef, FALSE, ERROR_UNKNOWN_REVISION },
2702 /* test ACE string type */
2703 { "D:(A;;GA;;;WD)", SDDL_REVISION_1, TRUE },
2704 { "D:(D;;GA;;;WD)", SDDL_REVISION_1, TRUE },
2705 { "ERROR:(D;;GA;;;WD)", SDDL_REVISION_1, FALSE, ERROR_INVALID_PARAMETER },
2706 /* test ACE string with spaces */
2707 { " D:(D;;GA;;;WD)", SDDL_REVISION_1, TRUE },
2708 { "D: (D;;GA;;;WD)", SDDL_REVISION_1, TRUE },
2709 { "D:( D;;GA;;;WD)", SDDL_REVISION_1, TRUE },
2710 { "D:(D ;;GA;;;WD)", SDDL_REVISION_1, FALSE, RPC_S_INVALID_STRING_UUID, ERROR_INVALID_ACL }, /* Vista+ */
2711 { "D:(D; ;GA;;;WD)", SDDL_REVISION_1, TRUE },
2712 { "D:(D;; GA;;;WD)", SDDL_REVISION_1, TRUE },
2713 { "D:(D;;GA ;;;WD)", SDDL_REVISION_1, FALSE, ERROR_INVALID_ACL },
2714 { "D:(D;;GA; ;;WD)", SDDL_REVISION_1, TRUE },
2715 { "D:(D;;GA;; ;WD)", SDDL_REVISION_1, TRUE },
2716 { "D:(D;;GA;;; WD)", SDDL_REVISION_1, TRUE },
2717 { "D:(D;;GA;;;WD )", SDDL_REVISION_1, TRUE },
2718 /* test ACE string access rights */
2719 { "D:(A;;GA;;;WD)", SDDL_REVISION_1, TRUE },
2720 { "D:(A;;GRGWGX;;;WD)", SDDL_REVISION_1, TRUE },
2721 { "D:(A;;RCSDWDWO;;;WD)", SDDL_REVISION_1, TRUE },
2722 { "D:(A;;RPWPCCDCLCSWLODTCR;;;WD)", SDDL_REVISION_1, TRUE },
2723 { "D:(A;;FAFRFWFX;;;WD)", SDDL_REVISION_1, TRUE },
2724 { "D:(A;;KAKRKWKX;;;WD)", SDDL_REVISION_1, TRUE },
2725 { "D:(A;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1, TRUE },
2726 { "S:(AU;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1, TRUE },
2727 /* test ACE string access right error case */
2728 { "D:(A;;ROB;;;WD)", SDDL_REVISION_1, FALSE, ERROR_INVALID_ACL },
2729 /* test behaviour with empty strings */
2730 { "", SDDL_REVISION_1, TRUE },
2731 /* test ACE string SID */
2732 { "D:(D;;GA;;;S-1-0-0)", SDDL_REVISION_1, TRUE },
2733 { "D:(D;;GA;;;Nonexistent account)", SDDL_REVISION_1, FALSE, ERROR_INVALID_ACL, ERROR_INVALID_SID } /* W2K */
2736 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA)
2738 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
2739 return;
2742 for (i = 0; i < sizeof(cssd)/sizeof(cssd[0]); i++)
2744 DWORD GLE;
2746 SetLastError(0xdeadbeef);
2747 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2748 cssd[i].sidstring, cssd[i].revision, &pSD, NULL);
2749 GLE = GetLastError();
2750 ok(ret == cssd[i].ret, "(%02u) Expected %s (%d)\n", i, cssd[i].ret ? "success" : "failure", GLE);
2751 if (!cssd[i].ret)
2752 ok(GLE == cssd[i].GLE ||
2753 (cssd[i].altGLE && GLE == cssd[i].altGLE),
2754 "(%02u) Unexpected last error %d\n", i, GLE);
2755 if (ret)
2756 LocalFree(pSD);
2759 /* test behaviour with NULL parameters */
2760 SetLastError(0xdeadbeef);
2761 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2762 NULL, 0xdeadbeef, &pSD, NULL);
2763 todo_wine
2764 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2765 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2766 GetLastError());
2768 SetLastError(0xdeadbeef);
2769 ret = pConvertStringSecurityDescriptorToSecurityDescriptorW(
2770 NULL, 0xdeadbeef, &pSD, NULL);
2771 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2772 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2773 GetLastError());
2775 SetLastError(0xdeadbeef);
2776 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2777 "D:(A;;ROB;;;WD)", 0xdeadbeef, NULL, NULL);
2778 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2779 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2780 GetLastError());
2782 SetLastError(0xdeadbeef);
2783 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2784 "D:(A;;ROB;;;WD)", SDDL_REVISION_1, NULL, NULL);
2785 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2786 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2787 GetLastError());
2789 /* test behaviour with empty strings */
2790 SetLastError(0xdeadbeef);
2791 ret = pConvertStringSecurityDescriptorToSecurityDescriptorW(
2792 Blank, SDDL_REVISION_1, &pSD, NULL);
2793 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2794 LocalFree(pSD);
2797 static void test_ConvertSecurityDescriptorToString(void)
2799 SECURITY_DESCRIPTOR desc;
2800 SECURITY_INFORMATION sec_info = OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION|SACL_SECURITY_INFORMATION;
2801 LPSTR string;
2802 DWORD size;
2803 PSID psid, psid2;
2804 PACL pacl;
2805 char sid_buf[256];
2806 char acl_buf[8192];
2807 ULONG len;
2809 if (!pConvertSecurityDescriptorToStringSecurityDescriptorA)
2811 win_skip("ConvertSecurityDescriptorToStringSecurityDescriptor is not available\n");
2812 return;
2814 if (!pCreateWellKnownSid)
2816 win_skip("CreateWellKnownSid is not available\n");
2817 return;
2820 /* It seems Windows XP adds an extra character to the length of the string for each ACE in an ACL. We
2821 * don't replicate this feature so we only test len >= strlen+1. */
2822 #define CHECK_RESULT_AND_FREE(exp_str) \
2823 ok(strcmp(string, (exp_str)) == 0, "String mismatch (expected \"%s\", got \"%s\")\n", (exp_str), string); \
2824 ok(len >= (strlen(exp_str) + 1), "Length mismatch (expected %d, got %d)\n", lstrlen(exp_str) + 1, len); \
2825 LocalFree(string);
2827 #define CHECK_ONE_OF_AND_FREE(exp_str1, exp_str2) \
2828 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); \
2829 ok(len >= (strlen(exp_str1) + 1) || len >= (strlen(exp_str2) + 1), "Length mismatch (expected %d or %d, got %d)\n", lstrlen(exp_str1) + 1, lstrlen(exp_str2) + 1, len); \
2830 LocalFree(string);
2832 InitializeSecurityDescriptor(&desc, SECURITY_DESCRIPTOR_REVISION);
2833 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2834 CHECK_RESULT_AND_FREE("");
2836 size = 4096;
2837 pCreateWellKnownSid(WinLocalSid, NULL, sid_buf, &size);
2838 SetSecurityDescriptorOwner(&desc, sid_buf, FALSE);
2839 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2840 CHECK_RESULT_AND_FREE("O:S-1-2-0");
2842 SetSecurityDescriptorOwner(&desc, sid_buf, TRUE);
2843 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2844 CHECK_RESULT_AND_FREE("O:S-1-2-0");
2846 size = sizeof(sid_buf);
2847 pCreateWellKnownSid(WinLocalSystemSid, NULL, sid_buf, &size);
2848 SetSecurityDescriptorOwner(&desc, sid_buf, TRUE);
2849 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2850 CHECK_RESULT_AND_FREE("O:SY");
2852 pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid);
2853 SetSecurityDescriptorGroup(&desc, psid, TRUE);
2854 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2855 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576");
2857 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, GROUP_SECURITY_INFORMATION, &string, &len), "Conversion failed\n");
2858 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
2860 pacl = (PACL)acl_buf;
2861 InitializeAcl(pacl, sizeof(acl_buf), ACL_REVISION);
2862 SetSecurityDescriptorDacl(&desc, TRUE, pacl, TRUE);
2863 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2864 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
2866 SetSecurityDescriptorDacl(&desc, TRUE, pacl, FALSE);
2867 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2868 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
2870 pConvertStringSidToSidA("S-1-5-6", &psid2);
2871 pAddAccessAllowedAceEx(pacl, ACL_REVISION, NO_PROPAGATE_INHERIT_ACE, 0xf0000000, psid2);
2872 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2873 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)");
2875 pAddAccessAllowedAceEx(pacl, ACL_REVISION, INHERIT_ONLY_ACE|INHERITED_ACE, 0x00000003, psid2);
2876 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2877 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)");
2879 pAddAccessDeniedAceEx(pacl, ACL_REVISION, OBJECT_INHERIT_ACE|CONTAINER_INHERIT_ACE, 0xffffffff, psid);
2880 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2881 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)");
2884 pacl = (PACL)acl_buf;
2885 InitializeAcl(pacl, sizeof(acl_buf), ACL_REVISION);
2886 SetSecurityDescriptorSacl(&desc, TRUE, pacl, FALSE);
2887 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2888 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:");
2890 /* fails in win2k */
2891 SetSecurityDescriptorDacl(&desc, TRUE, NULL, FALSE);
2892 pAddAuditAccessAceEx(pacl, ACL_REVISION, VALID_INHERIT_FLAGS, KEY_READ|KEY_WRITE, psid2, TRUE, TRUE);
2893 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len))
2895 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)", /* XP */
2896 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)" /* Vista */);
2899 /* fails in win2k */
2900 pAddAuditAccessAceEx(pacl, ACL_REVISION, NO_PROPAGATE_INHERIT_ACE, FILE_GENERIC_READ|FILE_GENERIC_WRITE, psid2, TRUE, FALSE);
2901 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len))
2903 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", /* XP */
2904 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)" /* Vista */);
2907 LocalFree(psid2);
2908 LocalFree(psid);
2911 static void test_SetSecurityDescriptorControl (PSECURITY_DESCRIPTOR sec)
2913 SECURITY_DESCRIPTOR_CONTROL ref;
2914 SECURITY_DESCRIPTOR_CONTROL test;
2916 SECURITY_DESCRIPTOR_CONTROL const mutable
2917 = SE_DACL_AUTO_INHERIT_REQ | SE_SACL_AUTO_INHERIT_REQ
2918 | SE_DACL_AUTO_INHERITED | SE_SACL_AUTO_INHERITED
2919 | SE_DACL_PROTECTED | SE_SACL_PROTECTED
2920 | 0x00000040 | 0x00000080 /* not defined in winnt.h */
2922 SECURITY_DESCRIPTOR_CONTROL const immutable
2923 = SE_OWNER_DEFAULTED | SE_GROUP_DEFAULTED
2924 | SE_DACL_PRESENT | SE_DACL_DEFAULTED
2925 | SE_SACL_PRESENT | SE_SACL_DEFAULTED
2926 | SE_RM_CONTROL_VALID | SE_SELF_RELATIVE
2929 int bit;
2930 DWORD dwRevision;
2931 LPCSTR fmt = "Expected error %s, got %u\n";
2933 GetSecurityDescriptorControl (sec, &ref, &dwRevision);
2935 /* The mutable bits are mutable regardless of the truth of
2936 SE_DACL_PRESENT and/or SE_SACL_PRESENT */
2938 /* Check call barfs if any bit-of-interest is immutable */
2939 for (bit = 0; bit < 16; ++bit)
2941 SECURITY_DESCRIPTOR_CONTROL const bitOfInterest = 1 << bit;
2942 SECURITY_DESCRIPTOR_CONTROL setOrClear = ref & bitOfInterest;
2944 SECURITY_DESCRIPTOR_CONTROL ctrl;
2946 DWORD dwExpect = (bitOfInterest & immutable)
2947 ? ERROR_INVALID_PARAMETER : 0xbebecaca;
2948 LPCSTR strExpect = (bitOfInterest & immutable)
2949 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
2951 ctrl = (bitOfInterest & mutable) ? ref + bitOfInterest : ref;
2952 setOrClear ^= bitOfInterest;
2953 SetLastError (0xbebecaca);
2954 pSetSecurityDescriptorControl (sec, bitOfInterest, setOrClear);
2955 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
2956 GetSecurityDescriptorControl(sec, &test, &dwRevision);
2957 expect_eq(test, ctrl, int, "%x");
2959 ctrl = ref;
2960 setOrClear ^= bitOfInterest;
2961 SetLastError (0xbebecaca);
2962 pSetSecurityDescriptorControl (sec, bitOfInterest, setOrClear);
2963 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
2964 GetSecurityDescriptorControl (sec, &test, &dwRevision);
2965 expect_eq(test, ref, int, "%x");
2968 /* Check call barfs if any bit-to-set is immutable
2969 even when not a bit-of-interest */
2970 for (bit = 0; bit < 16; ++bit)
2972 SECURITY_DESCRIPTOR_CONTROL const bitsOfInterest = mutable;
2973 SECURITY_DESCRIPTOR_CONTROL setOrClear = ref & bitsOfInterest;
2975 SECURITY_DESCRIPTOR_CONTROL ctrl;
2977 DWORD dwExpect = ((1 << bit) & immutable)
2978 ? ERROR_INVALID_PARAMETER : 0xbebecaca;
2979 LPCSTR strExpect = ((1 << bit) & immutable)
2980 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
2982 ctrl = ((1 << bit) & immutable) ? test : ref | mutable;
2983 setOrClear ^= bitsOfInterest;
2984 SetLastError (0xbebecaca);
2985 pSetSecurityDescriptorControl (sec, bitsOfInterest, setOrClear | (1 << bit));
2986 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
2987 GetSecurityDescriptorControl(sec, &test, &dwRevision);
2988 expect_eq(test, ctrl, int, "%x");
2990 ctrl = ((1 << bit) & immutable) ? test : ref | (1 << bit);
2991 setOrClear ^= bitsOfInterest;
2992 SetLastError (0xbebecaca);
2993 pSetSecurityDescriptorControl (sec, bitsOfInterest, setOrClear | (1 << bit));
2994 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
2995 GetSecurityDescriptorControl(sec, &test, &dwRevision);
2996 expect_eq(test, ctrl, int, "%x");
3000 static void test_PrivateObjectSecurity(void)
3002 SECURITY_INFORMATION sec_info = OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION|SACL_SECURITY_INFORMATION;
3003 SECURITY_DESCRIPTOR_CONTROL ctrl;
3004 PSECURITY_DESCRIPTOR sec;
3005 DWORD dwDescSize;
3006 DWORD dwRevision;
3007 DWORD retSize;
3008 LPSTR string;
3009 ULONG len;
3010 PSECURITY_DESCRIPTOR buf;
3012 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA)
3014 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
3015 return;
3018 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3019 "O:SY"
3020 "G:S-1-5-21-93476-23408-4576"
3021 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)"
3022 "(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3023 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
3024 SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
3026 test_SetSecurityDescriptorControl(sec);
3028 LocalFree(sec);
3030 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3031 "O:SY"
3032 "G:S-1-5-21-93476-23408-4576",
3033 SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
3035 test_SetSecurityDescriptorControl(sec);
3037 LocalFree(sec);
3039 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3040 "O:SY"
3041 "G:S-1-5-21-93476-23408-4576"
3042 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3043 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
3044 buf = HeapAlloc(GetProcessHeap(), 0, dwDescSize);
3045 pSetSecurityDescriptorControl(sec, SE_DACL_PROTECTED, SE_DACL_PROTECTED);
3046 GetSecurityDescriptorControl(sec, &ctrl, &dwRevision);
3047 expect_eq(ctrl, 0x9014, int, "%x");
3049 ok(GetPrivateObjectSecurity(sec, GROUP_SECURITY_INFORMATION, buf, dwDescSize, &retSize),
3050 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3051 ok(retSize <= dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
3052 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3053 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
3054 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
3055 expect_eq(ctrl, 0x8000, int, "%x");
3057 ok(GetPrivateObjectSecurity(sec, GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION, buf, dwDescSize, &retSize),
3058 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3059 ok(retSize <= dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
3060 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed err=%u\n", GetLastError());
3061 CHECK_ONE_OF_AND_FREE("G:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)",
3062 "G:S-1-5-21-93476-23408-4576D:P(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"); /* Win7 */
3063 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
3064 expect_eq(ctrl & (~ SE_DACL_PROTECTED), 0x8004, int, "%x");
3066 ok(GetPrivateObjectSecurity(sec, sec_info, buf, dwDescSize, &retSize),
3067 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3068 ok(retSize == dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
3069 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3070 CHECK_ONE_OF_AND_FREE("O:SY"
3071 "G:S-1-5-21-93476-23408-4576"
3072 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3073 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
3074 "O:SY"
3075 "G:S-1-5-21-93476-23408-4576"
3076 "D:P(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3077 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)"); /* Win7 */
3078 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
3079 expect_eq(ctrl & (~ SE_DACL_PROTECTED), 0x8014, int, "%x");
3081 SetLastError(0xdeadbeef);
3082 ok(GetPrivateObjectSecurity(sec, sec_info, buf, 5, &retSize) == FALSE, "GetPrivateObjectSecurity should have failed\n");
3083 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected error ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
3085 LocalFree(sec);
3086 HeapFree(GetProcessHeap(), 0, buf);
3088 #undef CHECK_RESULT_AND_FREE
3089 #undef CHECK_ONE_OF_AND_FREE
3091 static void test_acls(void)
3093 char buffer[256];
3094 PACL pAcl = (PACL)buffer;
3095 BOOL ret;
3097 SetLastError(0xdeadbeef);
3098 ret = InitializeAcl(pAcl, sizeof(ACL) - 1, ACL_REVISION);
3099 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
3101 win_skip("InitializeAcl is not implemented\n");
3102 return;
3105 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InitializeAcl with too small a buffer should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
3107 SetLastError(0xdeadbeef);
3108 ret = InitializeAcl(pAcl, 0xffffffff, ACL_REVISION);
3109 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "InitializeAcl with too large a buffer should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3111 SetLastError(0xdeadbeef);
3112 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION1);
3113 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "InitializeAcl(ACL_REVISION1) should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3115 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION2);
3116 ok(ret, "InitializeAcl(ACL_REVISION2) failed with error %d\n", GetLastError());
3118 ret = IsValidAcl(pAcl);
3119 ok(ret, "IsValidAcl failed with error %d\n", GetLastError());
3121 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION3);
3122 ok(ret, "InitializeAcl(ACL_REVISION3) failed with error %d\n", GetLastError());
3124 ret = IsValidAcl(pAcl);
3125 ok(ret, "IsValidAcl failed with error %d\n", GetLastError());
3127 SetLastError(0xdeadbeef);
3128 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION4);
3129 if (GetLastError() != ERROR_INVALID_PARAMETER)
3131 ok(ret, "InitializeAcl(ACL_REVISION4) failed with error %d\n", GetLastError());
3133 ret = IsValidAcl(pAcl);
3134 ok(ret, "IsValidAcl failed with error %d\n", GetLastError());
3136 else
3137 win_skip("ACL_REVISION4 is not implemented on NT4\n");
3139 SetLastError(0xdeadbeef);
3140 ret = InitializeAcl(pAcl, sizeof(buffer), -1);
3141 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "InitializeAcl(-1) failed with error %d\n", GetLastError());
3144 static void test_GetSecurityInfo(void)
3146 HANDLE obj;
3147 PSECURITY_DESCRIPTOR sd;
3148 PSID owner, group;
3149 PACL dacl;
3150 DWORD ret;
3152 if (!pGetSecurityInfo)
3154 win_skip("GetSecurityInfo is not available\n");
3155 return;
3158 /* Create something. Files have lots of associated security info. */
3159 obj = CreateFile(myARGV[0], GENERIC_READ, FILE_SHARE_READ, NULL,
3160 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
3161 if (obj == INVALID_HANDLE_VALUE)
3163 skip("Couldn't create an object for GetSecurityInfo test\n");
3164 return;
3167 ret = pGetSecurityInfo(obj, SE_FILE_OBJECT,
3168 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
3169 &owner, &group, &dacl, NULL, &sd);
3170 if (ret == ERROR_CALL_NOT_IMPLEMENTED)
3172 win_skip("GetSecurityInfo is not implemented\n");
3173 CloseHandle(obj);
3174 return;
3176 ok(ret == ERROR_SUCCESS, "GetSecurityInfo returned %d\n", ret);
3177 ok(sd != NULL, "GetSecurityInfo\n");
3178 ok(owner != NULL, "GetSecurityInfo\n");
3179 ok(group != NULL, "GetSecurityInfo\n");
3180 if (dacl != NULL)
3181 ok(IsValidAcl(dacl), "GetSecurityInfo\n");
3182 else
3183 win_skip("No ACL information returned\n");
3185 LocalFree(sd);
3187 if (!pCreateWellKnownSid)
3189 win_skip("NULL parameter test would crash on NT4\n");
3190 CloseHandle(obj);
3191 return;
3194 /* If we don't ask for the security descriptor, Windows will still give us
3195 the other stuff, leaving us no way to free it. */
3196 ret = pGetSecurityInfo(obj, SE_FILE_OBJECT,
3197 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
3198 &owner, &group, &dacl, NULL, NULL);
3199 ok(ret == ERROR_SUCCESS, "GetSecurityInfo returned %d\n", ret);
3200 ok(owner != NULL, "GetSecurityInfo\n");
3201 ok(group != NULL, "GetSecurityInfo\n");
3202 if (dacl != NULL)
3203 ok(IsValidAcl(dacl), "GetSecurityInfo\n");
3204 else
3205 win_skip("No ACL information returned\n");
3207 CloseHandle(obj);
3210 static void test_GetSidSubAuthority(void)
3212 PSID psid = NULL;
3214 if (!pGetSidSubAuthority || !pConvertStringSidToSidA || !pIsValidSid || !pGetSidSubAuthorityCount)
3216 win_skip("Some functions not available\n");
3217 return;
3219 /* Note: on windows passing in an invalid index like -1, lets GetSidSubAuthority return 0x05000000 but
3220 still GetLastError returns ERROR_SUCCESS then. We don't test these unlikely cornercases here for now */
3221 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576",&psid),"ConvertStringSidToSidA failed\n");
3222 ok(pIsValidSid(psid),"Sid is not valid\n");
3223 SetLastError(0xbebecaca);
3224 ok(*pGetSidSubAuthorityCount(psid) == 4,"GetSidSubAuthorityCount gave %d expected 4\n",*pGetSidSubAuthorityCount(psid));
3225 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3226 SetLastError(0xbebecaca);
3227 ok(*pGetSidSubAuthority(psid,0) == 21,"GetSidSubAuthority gave %d expected 21\n",*pGetSidSubAuthority(psid,0));
3228 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3229 SetLastError(0xbebecaca);
3230 ok(*pGetSidSubAuthority(psid,1) == 93476,"GetSidSubAuthority gave %d expected 93476\n",*pGetSidSubAuthority(psid,1));
3231 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3232 SetLastError(0xbebecaca);
3233 ok(pGetSidSubAuthority(psid,4) != NULL,"Expected out of bounds GetSidSubAuthority to return a non-NULL pointer\n");
3234 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3235 LocalFree(psid);
3238 static void test_CheckTokenMembership(void)
3240 PTOKEN_GROUPS token_groups;
3241 DWORD size;
3242 HANDLE process_token, token;
3243 BOOL is_member;
3244 BOOL ret;
3245 DWORD i;
3247 if (!pCheckTokenMembership)
3249 win_skip("CheckTokenMembership is not available\n");
3250 return;
3252 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &process_token);
3253 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
3255 ret = DuplicateToken(process_token, SecurityImpersonation, &token);
3256 ok(ret, "DuplicateToken failed with error %d\n", GetLastError());
3258 /* groups */
3259 ret = GetTokenInformation(token, TokenGroups, NULL, 0, &size);
3260 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
3261 "GetTokenInformation(TokenGroups) %s with error %d\n",
3262 ret ? "succeeded" : "failed", GetLastError());
3263 token_groups = HeapAlloc(GetProcessHeap(), 0, size);
3264 ret = GetTokenInformation(token, TokenGroups, token_groups, size, &size);
3265 ok(ret, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
3267 for (i = 0; i < token_groups->GroupCount; i++)
3269 if (token_groups->Groups[i].Attributes & SE_GROUP_ENABLED)
3270 break;
3273 if (i == token_groups->GroupCount)
3275 HeapFree(GetProcessHeap(), 0, token_groups);
3276 CloseHandle(token);
3277 skip("user not a member of any group\n");
3278 return;
3281 ret = pCheckTokenMembership(token, token_groups->Groups[i].Sid, &is_member);
3282 ok(ret, "CheckTokenMembership failed with error %d\n", GetLastError());
3283 ok(is_member, "CheckTokenMembership should have detected sid as member\n");
3285 ret = pCheckTokenMembership(NULL, token_groups->Groups[i].Sid, &is_member);
3286 ok(ret, "CheckTokenMembership failed with error %d\n", GetLastError());
3287 ok(is_member, "CheckTokenMembership should have detected sid as member\n");
3289 ret = pCheckTokenMembership(process_token, token_groups->Groups[i].Sid, &is_member);
3290 todo_wine {
3291 ok(!ret && GetLastError() == ERROR_NO_IMPERSONATION_TOKEN,
3292 "CheckTokenMembership with process token %s with error %d\n",
3293 ret ? "succeeded" : "failed", GetLastError());
3294 ok(!is_member, "CheckTokenMembership should have cleared is_member\n");
3297 HeapFree(GetProcessHeap(), 0, token_groups);
3298 CloseHandle(token);
3299 CloseHandle(process_token);
3302 static void test_EqualSid(void)
3304 PSID sid1, sid2;
3305 BOOL ret;
3306 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
3307 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
3309 SetLastError(0xdeadbeef);
3310 ret = AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
3311 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &sid1);
3312 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
3314 win_skip("AllocateAndInitializeSid is not implemented\n");
3315 return;
3317 ok(ret, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3318 ok(GetLastError() == 0xdeadbeef,
3319 "AllocateAndInitializeSid shouldn't have set last error to %d\n",
3320 GetLastError());
3322 ret = AllocateAndInitializeSid(&SIDAuthWorld, 1, SECURITY_WORLD_RID,
3323 0, 0, 0, 0, 0, 0, 0, &sid2);
3324 ok(ret, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3326 SetLastError(0xdeadbeef);
3327 ret = EqualSid(sid1, sid2);
3328 ok(!ret, "World and domain admins sids shouldn't have been equal\n");
3329 ok(GetLastError() == ERROR_SUCCESS ||
3330 broken(GetLastError() == 0xdeadbeef), /* NT4 */
3331 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
3332 GetLastError());
3334 SetLastError(0xdeadbeef);
3335 sid2 = FreeSid(sid2);
3336 ok(!sid2, "FreeSid should have returned NULL instead of %p\n", sid2);
3337 ok(GetLastError() == 0xdeadbeef,
3338 "FreeSid shouldn't have set last error to %d\n",
3339 GetLastError());
3341 ret = AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
3342 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &sid2);
3343 ok(ret, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3345 SetLastError(0xdeadbeef);
3346 ret = EqualSid(sid1, sid2);
3347 ok(ret, "Same sids should have been equal\n");
3348 ok(GetLastError() == ERROR_SUCCESS ||
3349 broken(GetLastError() == 0xdeadbeef), /* NT4 */
3350 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
3351 GetLastError());
3353 ((SID *)sid2)->Revision = 2;
3354 SetLastError(0xdeadbeef);
3355 ret = EqualSid(sid1, sid2);
3356 ok(!ret, "EqualSid with invalid sid should have returned FALSE\n");
3357 ok(GetLastError() == ERROR_SUCCESS ||
3358 broken(GetLastError() == 0xdeadbeef), /* NT4 */
3359 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
3360 GetLastError());
3361 ((SID *)sid2)->Revision = SID_REVISION;
3363 FreeSid(sid1);
3364 FreeSid(sid2);
3367 START_TEST(security)
3369 init();
3370 if (!hmod) return;
3372 if (myARGC >= 3)
3374 test_process_security_child();
3375 return;
3377 test_sid();
3378 test_trustee();
3379 test_luid();
3380 test_CreateWellKnownSid();
3381 test_FileSecurity();
3382 test_AccessCheck();
3383 test_token_attr();
3384 test_LookupAccountSid();
3385 test_LookupAccountName();
3386 test_security_descriptor();
3387 test_process_security();
3388 test_impersonation_level();
3389 test_SetEntriesInAcl();
3390 test_GetNamedSecurityInfoA();
3391 test_ConvertStringSecurityDescriptor();
3392 test_ConvertSecurityDescriptorToString();
3393 test_PrivateObjectSecurity();
3394 test_acls();
3395 test_GetSecurityInfo();
3396 test_GetSidSubAuthority();
3397 test_CheckTokenMembership();
3398 test_EqualSid();