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
25 #define WIN32_NO_STATUS
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,
44 } OBJECT_INFORMATION_CLASS, *POBJECT_INFORMATION_CLASS;
46 typedef struct _OBJECT_BASIC_INFORMATION {
48 ACCESS_MASK GrantedAccess;
52 ULONG NonPagedPoolUsage;
54 ULONG NameInformationLength;
55 ULONG TypeInformationLength;
56 ULONG SecurityDescriptorLength;
57 LARGE_INTEGER CreateTime;
58 } OBJECT_BASIC_INFORMATION, *POBJECT_BASIC_INFORMATION;
60 #define expect_eq(expr, value, type, format) { type ret = expr; ok((value) == ret, #expr " expected " format " got " format "\n", (value), (ret)); }
62 static BOOL (WINAPI *pAddAccessAllowedAceEx)(PACL, DWORD, DWORD, DWORD, PSID);
63 static BOOL (WINAPI *pAddAccessDeniedAceEx)(PACL, DWORD, DWORD, DWORD, PSID);
64 static BOOL (WINAPI *pAddAuditAccessAceEx)(PACL, DWORD, DWORD, DWORD, PSID, BOOL, BOOL);
65 typedef VOID (WINAPI *fnBuildTrusteeWithSidA)( PTRUSTEEA pTrustee, PSID pSid );
66 typedef VOID (WINAPI *fnBuildTrusteeWithNameA)( PTRUSTEEA pTrustee, LPSTR pName );
67 typedef VOID (WINAPI *fnBuildTrusteeWithObjectsAndNameA)( PTRUSTEEA pTrustee,
68 POBJECTS_AND_NAME_A pObjName,
69 SE_OBJECT_TYPE ObjectType,
71 LPSTR InheritedObjectTypeName,
73 typedef VOID (WINAPI *fnBuildTrusteeWithObjectsAndSidA)( PTRUSTEEA pTrustee,
74 POBJECTS_AND_SID pObjSid,
76 GUID* pInheritedObjectGuid,
78 typedef LPSTR (WINAPI *fnGetTrusteeNameA)( PTRUSTEEA pTrustee );
79 typedef BOOL (WINAPI *fnMakeSelfRelativeSD)( PSECURITY_DESCRIPTOR, PSECURITY_DESCRIPTOR, LPDWORD );
80 typedef BOOL (WINAPI *fnConvertSidToStringSidA)( PSID pSid, LPSTR *str );
81 typedef BOOL (WINAPI *fnConvertStringSidToSidA)( LPCSTR str, PSID pSid );
82 static BOOL (WINAPI *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 typedef BOOL (WINAPI *fnGetFileSecurityA)(LPCSTR, SECURITY_INFORMATION,
90 PSECURITY_DESCRIPTOR, DWORD, LPDWORD);
91 typedef BOOL (WINAPI *fnSetFileSecurityA)(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 typedef DWORD (WINAPI *fnRtlAdjustPrivilege)(ULONG,BOOLEAN,BOOLEAN,PBOOLEAN);
100 typedef BOOL (WINAPI *fnCreateWellKnownSid)(WELL_KNOWN_SID_TYPE,PSID,PSID,DWORD*);
101 typedef BOOL (WINAPI *fnDuplicateTokenEx)(HANDLE,DWORD,LPSECURITY_ATTRIBUTES,
102 SECURITY_IMPERSONATION_LEVEL,TOKEN_TYPE,PHANDLE);
104 typedef NTSTATUS (WINAPI *fnLsaQueryInformationPolicy)(LSA_HANDLE,POLICY_INFORMATION_CLASS,PVOID*);
105 typedef NTSTATUS (WINAPI *fnLsaClose)(LSA_HANDLE);
106 typedef NTSTATUS (WINAPI *fnLsaFreeMemory)(PVOID);
107 typedef NTSTATUS (WINAPI *fnLsaOpenPolicy)(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*);
119 static char** myARGV;
121 fnBuildTrusteeWithSidA pBuildTrusteeWithSidA;
122 fnBuildTrusteeWithNameA pBuildTrusteeWithNameA;
123 fnBuildTrusteeWithObjectsAndNameA pBuildTrusteeWithObjectsAndNameA;
124 fnBuildTrusteeWithObjectsAndSidA pBuildTrusteeWithObjectsAndSidA;
125 fnGetTrusteeNameA pGetTrusteeNameA;
126 fnMakeSelfRelativeSD pMakeSelfRelativeSD;
127 fnConvertSidToStringSidA pConvertSidToStringSidA;
128 fnConvertStringSidToSidA pConvertStringSidToSidA;
129 fnGetFileSecurityA pGetFileSecurityA;
130 fnSetFileSecurityA pSetFileSecurityA;
131 fnRtlAdjustPrivilege pRtlAdjustPrivilege;
132 fnCreateWellKnownSid pCreateWellKnownSid;
133 fnDuplicateTokenEx pDuplicateTokenEx;
134 fnLsaQueryInformationPolicy pLsaQueryInformationPolicy;
135 fnLsaClose pLsaClose;
136 fnLsaFreeMemory pLsaFreeMemory;
137 fnLsaOpenPolicy pLsaOpenPolicy;
141 SID_IDENTIFIER_AUTHORITY auth;
145 static void init(void)
149 hntdll = GetModuleHandleA("ntdll.dll");
150 pNtQueryObject = (void *)GetProcAddress( hntdll, "NtQueryObject" );
151 pNtAccessCheck = (void *)GetProcAddress( hntdll, "NtAccessCheck" );
153 hmod = GetModuleHandle("advapi32.dll");
154 pAddAccessAllowedAceEx = (void *)GetProcAddress(hmod, "AddAccessAllowedAceEx");
155 pAddAccessDeniedAceEx = (void *)GetProcAddress(hmod, "AddAccessDeniedAceEx");
156 pAddAuditAccessAceEx = (void *)GetProcAddress(hmod, "AddAuditAccessAceEx");
157 pCheckTokenMembership = (void *)GetProcAddress(hmod, "CheckTokenMembership");
158 pConvertStringSecurityDescriptorToSecurityDescriptorA =
159 (void *)GetProcAddress(hmod, "ConvertStringSecurityDescriptorToSecurityDescriptorA" );
160 pConvertStringSecurityDescriptorToSecurityDescriptorW =
161 (void *)GetProcAddress(hmod, "ConvertStringSecurityDescriptorToSecurityDescriptorW" );
162 pConvertSecurityDescriptorToStringSecurityDescriptorA =
163 (void *)GetProcAddress(hmod, "ConvertSecurityDescriptorToStringSecurityDescriptorA" );
164 pGetFileSecurityA = (fnGetFileSecurityA)GetProcAddress(hmod, "GetFileSecurityA" );
165 pSetFileSecurityA = (fnSetFileSecurityA)GetProcAddress(hmod, "SetFileSecurityA" );
166 pCreateWellKnownSid = (fnCreateWellKnownSid)GetProcAddress( hmod, "CreateWellKnownSid" );
167 pGetNamedSecurityInfoA = (void *)GetProcAddress(hmod, "GetNamedSecurityInfoA");
168 pGetSidSubAuthority = (void *)GetProcAddress(hmod, "GetSidSubAuthority");
169 pGetSidSubAuthorityCount = (void *)GetProcAddress(hmod, "GetSidSubAuthorityCount");
170 pIsValidSid = (void *)GetProcAddress(hmod, "IsValidSid");
171 pMakeSelfRelativeSD = (void *)GetProcAddress(hmod, "MakeSelfRelativeSD");
172 pSetEntriesInAclW = (void *)GetProcAddress(hmod, "SetEntriesInAclW");
173 pSetSecurityDescriptorControl = (void *)GetProcAddress(hmod, "SetSecurityDescriptorControl");
174 pGetSecurityInfo = (void *)GetProcAddress(hmod, "GetSecurityInfo");
176 myARGC = winetest_get_mainargs( &myARGV );
179 static void test_str_sid(const char *str_sid)
184 if (pConvertStringSidToSidA(str_sid, &psid))
186 if (pConvertSidToStringSidA(psid, &temp))
188 trace(" %s: %s\n", str_sid, temp);
195 if (GetLastError() != ERROR_INVALID_SID)
196 trace(" %s: couldn't be converted, returned %d\n", str_sid, GetLastError());
198 trace(" %s: couldn't be converted\n", str_sid);
202 static void test_sid(void)
204 struct sidRef refs[] = {
205 { { {0x00,0x00,0x33,0x44,0x55,0x66} }, "S-1-860116326-1" },
206 { { {0x00,0x00,0x01,0x02,0x03,0x04} }, "S-1-16909060-1" },
207 { { {0x00,0x00,0x00,0x01,0x02,0x03} }, "S-1-66051-1" },
208 { { {0x00,0x00,0x00,0x00,0x01,0x02} }, "S-1-258-1" },
209 { { {0x00,0x00,0x00,0x00,0x00,0x02} }, "S-1-2-1" },
210 { { {0x00,0x00,0x00,0x00,0x00,0x0c} }, "S-1-12-1" },
212 const char noSubAuthStr[] = "S-1-5";
219 pConvertSidToStringSidA = (fnConvertSidToStringSidA)
220 GetProcAddress( hmod, "ConvertSidToStringSidA" );
221 if( !pConvertSidToStringSidA )
223 pConvertStringSidToSidA = (fnConvertStringSidToSidA)
224 GetProcAddress( hmod, "ConvertStringSidToSidA" );
225 if( !pConvertStringSidToSidA )
228 r = pConvertStringSidToSidA( NULL, NULL );
229 ok( !r, "expected failure with NULL parameters\n" );
230 if( GetLastError() == ERROR_CALL_NOT_IMPLEMENTED )
232 ok( GetLastError() == ERROR_INVALID_PARAMETER,
233 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
236 r = pConvertStringSidToSidA( refs[0].refStr, NULL );
237 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
238 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
241 r = pConvertStringSidToSidA( NULL, &str );
242 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
243 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
246 r = pConvertStringSidToSidA( noSubAuthStr, &psid );
248 "expected failure with no sub authorities\n" );
249 ok( GetLastError() == ERROR_INVALID_SID,
250 "expected GetLastError() is ERROR_INVALID_SID, got %d\n",
253 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid), "ConvertStringSidToSidA failed\n");
255 ok(pisid->SubAuthorityCount == 4, "Invalid sub authority count - expected 4, got %d\n", pisid->SubAuthorityCount);
256 ok(pisid->SubAuthority[0] == 21, "Invalid subauthority 0 - expceted 21, got %d\n", pisid->SubAuthority[0]);
257 ok(pisid->SubAuthority[3] == 4576, "Invalid subauthority 0 - expceted 4576, got %d\n", pisid->SubAuthority[3]);
261 for( i = 0; i < sizeof(refs) / sizeof(refs[0]); i++ )
265 r = AllocateAndInitializeSid( &refs[i].auth, 1,1,0,0,0,0,0,0,0,
267 ok( r, "failed to allocate sid\n" );
268 r = pConvertSidToStringSidA( psid, &str );
269 ok( r, "failed to convert sid\n" );
272 ok( !strcmp( str, refs[i].refStr ),
273 "incorrect sid, expected %s, got %s\n", refs[i].refStr, str );
279 r = pConvertStringSidToSidA( refs[i].refStr, &psid );
280 ok( r, "failed to parse sid string\n" );
283 !memcmp( pisid->IdentifierAuthority.Value, refs[i].auth.Value,
284 sizeof(refs[i].auth) ),
285 "string sid %s didn't parse to expected value\n"
286 "(got 0x%04x%08x, expected 0x%04x%08x)\n",
288 MAKEWORD( pisid->IdentifierAuthority.Value[1],
289 pisid->IdentifierAuthority.Value[0] ),
290 MAKELONG( MAKEWORD( pisid->IdentifierAuthority.Value[5],
291 pisid->IdentifierAuthority.Value[4] ),
292 MAKEWORD( pisid->IdentifierAuthority.Value[3],
293 pisid->IdentifierAuthority.Value[2] ) ),
294 MAKEWORD( refs[i].auth.Value[1], refs[i].auth.Value[0] ),
295 MAKELONG( MAKEWORD( refs[i].auth.Value[5], refs[i].auth.Value[4] ),
296 MAKEWORD( refs[i].auth.Value[3], refs[i].auth.Value[2] ) ) );
301 trace("String SIDs:\n");
342 static void test_trustee(void)
344 GUID ObjectType = {0x12345678, 0x1234, 0x5678, {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}};
345 GUID InheritedObjectType = {0x23456789, 0x2345, 0x6786, {0x2, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}};
347 OBJECTS_AND_NAME_ oan;
351 char szObjectTypeName[] = "ObjectTypeName";
352 char szInheritedObjectTypeName[] = "InheritedObjectTypeName";
353 char szTrusteeName[] = "szTrusteeName";
354 SID_IDENTIFIER_AUTHORITY auth = { {0x11,0x22,0,0,0, 0} };
356 memset( &ZeroGuid, 0x00, sizeof (ZeroGuid) );
358 pBuildTrusteeWithSidA = (fnBuildTrusteeWithSidA)
359 GetProcAddress( hmod, "BuildTrusteeWithSidA" );
360 pBuildTrusteeWithNameA = (fnBuildTrusteeWithNameA)
361 GetProcAddress( hmod, "BuildTrusteeWithNameA" );
362 pBuildTrusteeWithObjectsAndNameA = (fnBuildTrusteeWithObjectsAndNameA)
363 GetProcAddress (hmod, "BuildTrusteeWithObjectsAndNameA" );
364 pBuildTrusteeWithObjectsAndSidA = (fnBuildTrusteeWithObjectsAndSidA)
365 GetProcAddress (hmod, "BuildTrusteeWithObjectsAndSidA" );
366 pGetTrusteeNameA = (fnGetTrusteeNameA)
367 GetProcAddress (hmod, "GetTrusteeNameA" );
368 if( !pBuildTrusteeWithSidA || !pBuildTrusteeWithNameA ||
369 !pBuildTrusteeWithObjectsAndNameA || !pBuildTrusteeWithObjectsAndSidA ||
373 if ( ! AllocateAndInitializeSid( &auth, 1, 42, 0,0,0,0,0,0,0,&psid ) )
375 trace( "failed to init SID\n" );
379 /* test BuildTrusteeWithSidA */
380 memset( &trustee, 0xff, sizeof trustee );
381 pBuildTrusteeWithSidA( &trustee, psid );
383 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
384 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
385 "MultipleTrusteeOperation wrong\n");
386 ok( trustee.TrusteeForm == TRUSTEE_IS_SID, "TrusteeForm wrong\n");
387 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
388 ok( trustee.ptstrName == psid, "ptstrName wrong\n" );
390 /* test BuildTrusteeWithObjectsAndSidA (test 1) */
391 memset( &trustee, 0xff, sizeof trustee );
392 memset( &oas, 0xff, sizeof(oas) );
393 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, &ObjectType,
394 &InheritedObjectType, psid);
396 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
397 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
398 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
399 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
400 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
402 ok(oas.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
403 ok(!memcmp(&oas.ObjectTypeGuid, &ObjectType, sizeof(GUID)), "ObjectTypeGuid wrong\n");
404 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
405 ok(oas.pSid == psid, "pSid wrong\n");
407 /* test GetTrusteeNameA */
408 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oas, "GetTrusteeName returned wrong value\n");
410 /* test BuildTrusteeWithObjectsAndSidA (test 2) */
411 memset( &trustee, 0xff, sizeof trustee );
412 memset( &oas, 0xff, sizeof(oas) );
413 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, NULL,
414 &InheritedObjectType, psid);
416 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
417 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
418 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
419 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
420 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
422 ok(oas.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
423 ok(!memcmp(&oas.ObjectTypeGuid, &ZeroGuid, sizeof(GUID)), "ObjectTypeGuid wrong\n");
424 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
425 ok(oas.pSid == psid, "pSid wrong\n");
429 /* test BuildTrusteeWithNameA */
430 memset( &trustee, 0xff, sizeof trustee );
431 pBuildTrusteeWithNameA( &trustee, szTrusteeName );
433 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
434 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
435 "MultipleTrusteeOperation wrong\n");
436 ok( trustee.TrusteeForm == TRUSTEE_IS_NAME, "TrusteeForm wrong\n");
437 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
438 ok( trustee.ptstrName == szTrusteeName, "ptstrName wrong\n" );
440 /* test BuildTrusteeWithObjectsAndNameA (test 1) */
441 memset( &trustee, 0xff, sizeof trustee );
442 memset( &oan, 0xff, sizeof(oan) );
443 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
444 szInheritedObjectTypeName, szTrusteeName);
446 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
447 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
448 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
449 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
450 ok(trustee.ptstrName == (LPTSTR)&oan, "ptstrName wrong\n");
452 ok(oan.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
453 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
454 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
455 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
457 /* test GetTrusteeNameA */
458 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oan, "GetTrusteeName returned wrong value\n");
460 /* test BuildTrusteeWithObjectsAndNameA (test 2) */
461 memset( &trustee, 0xff, sizeof trustee );
462 memset( &oan, 0xff, sizeof(oan) );
463 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, NULL,
464 szInheritedObjectTypeName, szTrusteeName);
466 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
467 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
468 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
469 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
470 ok(trustee.ptstrName == (LPSTR)&oan, "ptstrName wrong\n");
472 ok(oan.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
473 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
474 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
475 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
477 /* test BuildTrusteeWithObjectsAndNameA (test 3) */
478 memset( &trustee, 0xff, sizeof trustee );
479 memset( &oan, 0xff, sizeof(oan) );
480 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
481 NULL, szTrusteeName);
483 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
484 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
485 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
486 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
487 ok(trustee.ptstrName == (LPTSTR)&oan, "ptstrName wrong\n");
489 ok(oan.ObjectsPresent == ACE_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
490 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
491 ok(oan.InheritedObjectTypeName == NULL, "InheritedObjectTypeName wrong\n");
492 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
495 /* If the first isn't defined, assume none is */
496 #ifndef SE_MIN_WELL_KNOWN_PRIVILEGE
497 #define SE_MIN_WELL_KNOWN_PRIVILEGE 2L
498 #define SE_CREATE_TOKEN_PRIVILEGE 2L
499 #define SE_ASSIGNPRIMARYTOKEN_PRIVILEGE 3L
500 #define SE_LOCK_MEMORY_PRIVILEGE 4L
501 #define SE_INCREASE_QUOTA_PRIVILEGE 5L
502 #define SE_MACHINE_ACCOUNT_PRIVILEGE 6L
503 #define SE_TCB_PRIVILEGE 7L
504 #define SE_SECURITY_PRIVILEGE 8L
505 #define SE_TAKE_OWNERSHIP_PRIVILEGE 9L
506 #define SE_LOAD_DRIVER_PRIVILEGE 10L
507 #define SE_SYSTEM_PROFILE_PRIVILEGE 11L
508 #define SE_SYSTEMTIME_PRIVILEGE 12L
509 #define SE_PROF_SINGLE_PROCESS_PRIVILEGE 13L
510 #define SE_INC_BASE_PRIORITY_PRIVILEGE 14L
511 #define SE_CREATE_PAGEFILE_PRIVILEGE 15L
512 #define SE_CREATE_PERMANENT_PRIVILEGE 16L
513 #define SE_BACKUP_PRIVILEGE 17L
514 #define SE_RESTORE_PRIVILEGE 18L
515 #define SE_SHUTDOWN_PRIVILEGE 19L
516 #define SE_DEBUG_PRIVILEGE 20L
517 #define SE_AUDIT_PRIVILEGE 21L
518 #define SE_SYSTEM_ENVIRONMENT_PRIVILEGE 22L
519 #define SE_CHANGE_NOTIFY_PRIVILEGE 23L
520 #define SE_REMOTE_SHUTDOWN_PRIVILEGE 24L
521 #define SE_UNDOCK_PRIVILEGE 25L
522 #define SE_SYNC_AGENT_PRIVILEGE 26L
523 #define SE_ENABLE_DELEGATION_PRIVILEGE 27L
524 #define SE_MANAGE_VOLUME_PRIVILEGE 28L
525 #define SE_IMPERSONATE_PRIVILEGE 29L
526 #define SE_CREATE_GLOBAL_PRIVILEGE 30L
527 #define SE_MAX_WELL_KNOWN_PRIVILEGE SE_CREATE_GLOBAL_PRIVILEGE
528 #endif /* ndef SE_MIN_WELL_KNOWN_PRIVILEGE */
530 static void test_allocateLuid(void)
532 BOOL (WINAPI *pAllocateLocallyUniqueId)(PLUID);
536 pAllocateLocallyUniqueId = (void*)GetProcAddress(hmod, "AllocateLocallyUniqueId");
537 if (!pAllocateLocallyUniqueId) return;
539 ret = pAllocateLocallyUniqueId(&luid1);
540 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
544 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
545 ret = pAllocateLocallyUniqueId(&luid2);
547 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
548 ok(luid1.LowPart > SE_MAX_WELL_KNOWN_PRIVILEGE || luid1.HighPart != 0,
549 "AllocateLocallyUniqueId returned a well-known LUID\n");
550 ok(luid1.LowPart != luid2.LowPart || luid1.HighPart != luid2.HighPart,
551 "AllocateLocallyUniqueId returned non-unique LUIDs\n");
552 ret = pAllocateLocallyUniqueId(NULL);
553 ok( !ret && GetLastError() == ERROR_NOACCESS,
554 "AllocateLocallyUniqueId(NULL) didn't return ERROR_NOACCESS: %d\n",
558 static void test_lookupPrivilegeName(void)
560 BOOL (WINAPI *pLookupPrivilegeNameA)(LPCSTR, PLUID, LPSTR, LPDWORD);
561 char buf[MAX_PATH]; /* arbitrary, seems long enough */
562 DWORD cchName = sizeof(buf);
563 LUID luid = { 0, 0 };
567 /* check whether it's available first */
568 pLookupPrivilegeNameA = (void*)GetProcAddress(hmod, "LookupPrivilegeNameA");
569 if (!pLookupPrivilegeNameA) return;
570 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
571 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
572 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
575 /* check with a short buffer */
577 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
578 ret = pLookupPrivilegeNameA(NULL, &luid, NULL, &cchName);
579 ok( !ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
580 "LookupPrivilegeNameA didn't fail with ERROR_INSUFFICIENT_BUFFER: %d\n",
582 ok(cchName == strlen("SeCreateTokenPrivilege") + 1,
583 "LookupPrivilegeNameA returned an incorrect required length for\n"
584 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName,
585 lstrlenA("SeCreateTokenPrivilege") + 1);
586 /* check a known value and its returned length on success */
587 cchName = sizeof(buf);
588 ok(pLookupPrivilegeNameA(NULL, &luid, buf, &cchName) &&
589 cchName == strlen("SeCreateTokenPrivilege"),
590 "LookupPrivilegeNameA returned an incorrect output length for\n"
591 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName,
592 (int)strlen("SeCreateTokenPrivilege"));
593 /* check known values */
594 for (i = SE_MIN_WELL_KNOWN_PRIVILEGE; i <= SE_MAX_WELL_KNOWN_PRIVILEGE; i++)
597 cchName = sizeof(buf);
598 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
599 ok( ret || GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
600 "LookupPrivilegeNameA(0.%d) failed: %d\n", i, GetLastError());
602 /* check a bogus LUID */
603 luid.LowPart = 0xdeadbeef;
604 cchName = sizeof(buf);
605 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
606 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
607 "LookupPrivilegeNameA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
609 /* check on a bogus system */
610 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
611 cchName = sizeof(buf);
612 ret = pLookupPrivilegeNameA("b0gu5.Nam3", &luid, buf, &cchName);
613 ok( !ret && (GetLastError() == RPC_S_SERVER_UNAVAILABLE ||
614 GetLastError() == RPC_S_INVALID_NET_ADDR) /* w2k8 */,
615 "LookupPrivilegeNameA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
625 static void test_lookupPrivilegeValue(void)
627 static const struct NameToLUID privs[] = {
628 { "SeCreateTokenPrivilege", SE_CREATE_TOKEN_PRIVILEGE },
629 { "SeAssignPrimaryTokenPrivilege", SE_ASSIGNPRIMARYTOKEN_PRIVILEGE },
630 { "SeLockMemoryPrivilege", SE_LOCK_MEMORY_PRIVILEGE },
631 { "SeIncreaseQuotaPrivilege", SE_INCREASE_QUOTA_PRIVILEGE },
632 { "SeMachineAccountPrivilege", SE_MACHINE_ACCOUNT_PRIVILEGE },
633 { "SeTcbPrivilege", SE_TCB_PRIVILEGE },
634 { "SeSecurityPrivilege", SE_SECURITY_PRIVILEGE },
635 { "SeTakeOwnershipPrivilege", SE_TAKE_OWNERSHIP_PRIVILEGE },
636 { "SeLoadDriverPrivilege", SE_LOAD_DRIVER_PRIVILEGE },
637 { "SeSystemProfilePrivilege", SE_SYSTEM_PROFILE_PRIVILEGE },
638 { "SeSystemtimePrivilege", SE_SYSTEMTIME_PRIVILEGE },
639 { "SeProfileSingleProcessPrivilege", SE_PROF_SINGLE_PROCESS_PRIVILEGE },
640 { "SeIncreaseBasePriorityPrivilege", SE_INC_BASE_PRIORITY_PRIVILEGE },
641 { "SeCreatePagefilePrivilege", SE_CREATE_PAGEFILE_PRIVILEGE },
642 { "SeCreatePermanentPrivilege", SE_CREATE_PERMANENT_PRIVILEGE },
643 { "SeBackupPrivilege", SE_BACKUP_PRIVILEGE },
644 { "SeRestorePrivilege", SE_RESTORE_PRIVILEGE },
645 { "SeShutdownPrivilege", SE_SHUTDOWN_PRIVILEGE },
646 { "SeDebugPrivilege", SE_DEBUG_PRIVILEGE },
647 { "SeAuditPrivilege", SE_AUDIT_PRIVILEGE },
648 { "SeSystemEnvironmentPrivilege", SE_SYSTEM_ENVIRONMENT_PRIVILEGE },
649 { "SeChangeNotifyPrivilege", SE_CHANGE_NOTIFY_PRIVILEGE },
650 { "SeRemoteShutdownPrivilege", SE_REMOTE_SHUTDOWN_PRIVILEGE },
651 { "SeUndockPrivilege", SE_UNDOCK_PRIVILEGE },
652 { "SeSyncAgentPrivilege", SE_SYNC_AGENT_PRIVILEGE },
653 { "SeEnableDelegationPrivilege", SE_ENABLE_DELEGATION_PRIVILEGE },
654 { "SeManageVolumePrivilege", SE_MANAGE_VOLUME_PRIVILEGE },
655 { "SeImpersonatePrivilege", SE_IMPERSONATE_PRIVILEGE },
656 { "SeCreateGlobalPrivilege", SE_CREATE_GLOBAL_PRIVILEGE },
658 BOOL (WINAPI *pLookupPrivilegeValueA)(LPCSTR, LPCSTR, PLUID);
663 /* check whether it's available first */
664 pLookupPrivilegeValueA = (void*)GetProcAddress(hmod, "LookupPrivilegeValueA");
665 if (!pLookupPrivilegeValueA) return;
666 ret = pLookupPrivilegeValueA(NULL, "SeCreateTokenPrivilege", &luid);
667 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
670 /* check a bogus system name */
671 ret = pLookupPrivilegeValueA("b0gu5.Nam3", "SeCreateTokenPrivilege", &luid);
672 ok( !ret && (GetLastError() == RPC_S_SERVER_UNAVAILABLE ||
673 GetLastError() == RPC_S_INVALID_NET_ADDR) /* w2k8 */,
674 "LookupPrivilegeValueA didn't fail with RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR: %d\n",
676 /* check a NULL string */
677 ret = pLookupPrivilegeValueA(NULL, 0, &luid);
678 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
679 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
681 /* check a bogus privilege name */
682 ret = pLookupPrivilegeValueA(NULL, "SeBogusPrivilege", &luid);
683 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
684 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
686 /* check case insensitive */
687 ret = pLookupPrivilegeValueA(NULL, "sEcREATEtOKENpRIVILEGE", &luid);
689 "LookupPrivilegeValueA(NULL, sEcREATEtOKENpRIVILEGE, &luid) failed: %d\n",
691 for (i = 0; i < sizeof(privs) / sizeof(privs[0]); i++)
693 /* Not all privileges are implemented on all Windows versions, so
694 * don't worry if the call fails
696 if (pLookupPrivilegeValueA(NULL, privs[i].name, &luid))
698 ok(luid.LowPart == privs[i].lowPart,
699 "LookupPrivilegeValueA returned an invalid LUID for %s\n",
705 static void test_luid(void)
708 test_lookupPrivilegeName();
709 test_lookupPrivilegeValue();
712 static void test_FileSecurity(void)
714 char wintmpdir [MAX_PATH];
715 char path [MAX_PATH];
716 char file [MAX_PATH];
722 SECURITY_INFORMATION const request = OWNER_SECURITY_INFORMATION
723 | GROUP_SECURITY_INFORMATION
724 | DACL_SECURITY_INFORMATION;
726 if (!pGetFileSecurityA) {
727 win_skip ("GetFileSecurity is not available\n");
731 if (!pSetFileSecurityA) {
732 win_skip ("SetFileSecurity is not available\n");
736 if (!GetTempPathA (sizeof (wintmpdir), wintmpdir)) {
737 win_skip ("GetTempPathA failed\n");
741 /* Create a temporary directory and in it a temporary file */
742 strcat (strcpy (path, wintmpdir), "rary");
743 SetLastError(0xdeadbeef);
744 rc = CreateDirectoryA (path, NULL);
745 ok (rc || GetLastError() == ERROR_ALREADY_EXISTS, "CreateDirectoryA "
746 "failed for '%s' with %d\n", path, GetLastError());
748 strcat (strcpy (file, path), "\\ess");
749 SetLastError(0xdeadbeef);
750 fh = CreateFileA (file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
751 ok (fh != INVALID_HANDLE_VALUE, "CreateFileA "
752 "failed for '%s' with %d\n", file, GetLastError());
755 /* For the temporary file ... */
757 /* Get size needed */
759 SetLastError(0xdeadbeef);
760 rc = pGetFileSecurityA (file, request, NULL, 0, &retSize);
761 if (!rc && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)) {
762 win_skip("GetFileSecurityA is not implemented\n");
765 ok (!rc, "GetFileSecurityA "
766 "was expected to fail for '%s'\n", file);
767 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetFileSecurityA "
768 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
769 ok (retSize > sizeof (SECURITY_DESCRIPTOR), "GetFileSecurityA returned size %d\n", retSize);
772 sd = HeapAlloc (GetProcessHeap (), 0, sdSize);
774 /* Get security descriptor for real */
776 SetLastError(0xdeadbeef);
777 rc = pGetFileSecurityA (file, request, sd, sdSize, &retSize);
778 ok (rc, "GetFileSecurityA "
779 "was not expected to fail '%s': %d\n", file, GetLastError());
780 ok (retSize == sdSize ||
781 broken(retSize == 0), /* NT4 */
782 "GetFileSecurityA returned size %d; expected %d\n", retSize, sdSize);
784 /* Use it to set security descriptor */
785 SetLastError(0xdeadbeef);
786 rc = pSetFileSecurityA (file, request, sd);
787 ok (rc, "SetFileSecurityA "
788 "was not expected to fail '%s': %d\n", file, GetLastError());
790 HeapFree (GetProcessHeap (), 0, sd);
792 /* Repeat for the temporary directory ... */
794 /* Get size needed */
796 SetLastError(0xdeadbeef);
797 rc = pGetFileSecurityA (path, request, NULL, 0, &retSize);
798 ok (!rc, "GetFileSecurityA "
799 "was expected to fail for '%s'\n", path);
800 ok (GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetFileSecurityA "
801 "returned %d; expected ERROR_INSUFFICIENT_BUFFER\n", GetLastError());
802 ok (retSize > sizeof (SECURITY_DESCRIPTOR), "GetFileSecurityA returned size %d\n", retSize);
805 sd = HeapAlloc (GetProcessHeap (), 0, sdSize);
807 /* Get security descriptor for real */
809 SetLastError(0xdeadbeef);
810 rc = pGetFileSecurityA (path, request, sd, sdSize, &retSize);
811 ok (rc, "GetFileSecurityA "
812 "was not expected to fail '%s': %d\n", path, GetLastError());
813 ok (retSize == sdSize ||
814 broken(retSize == 0), /* NT4 */
815 "GetFileSecurityA returned size %d; expected %d\n", retSize, sdSize);
817 /* Use it to set security descriptor */
818 SetLastError(0xdeadbeef);
819 rc = pSetFileSecurityA (path, request, sd);
820 ok (rc, "SetFileSecurityA "
821 "was not expected to fail '%s': %d\n", path, GetLastError());
822 HeapFree (GetProcessHeap (), 0, sd);
825 strcpy (wintmpdir, "\\Should not exist");
826 SetLastError(0xdeadbeef);
827 rc = pGetFileSecurityA (wintmpdir, OWNER_SECURITY_INFORMATION, NULL, 0, &sdSize);
828 ok (!rc, "GetFileSecurityA should fail for not existing directories/files\n");
829 ok (GetLastError() == ERROR_FILE_NOT_FOUND,
830 "last error ERROR_FILE_NOT_FOUND expected, got %d\n", GetLastError());
833 /* Remove temporary file and directory */
835 RemoveDirectoryA(path);
838 static void test_AccessCheck(void)
840 PSID EveryoneSid = NULL, AdminSid = NULL, UsersSid = NULL;
842 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL;
843 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
844 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
845 GENERIC_MAPPING Mapping = { KEY_READ, KEY_WRITE, KEY_EXECUTE, KEY_ALL_ACCESS };
852 PRIVILEGE_SET *PrivSet;
857 NTSTATUS ntret, ntAccessStatus;
859 NtDllModule = GetModuleHandle("ntdll.dll");
862 skip("not running on NT, skipping test\n");
865 pRtlAdjustPrivilege = (fnRtlAdjustPrivilege)
866 GetProcAddress(NtDllModule, "RtlAdjustPrivilege");
867 if (!pRtlAdjustPrivilege)
869 win_skip("missing RtlAdjustPrivilege, skipping test\n");
873 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
874 res = InitializeAcl(Acl, 256, ACL_REVISION);
875 if(!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
877 skip("ACLs not implemented - skipping tests\n");
878 HeapFree(GetProcessHeap(), 0, Acl);
881 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
883 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
884 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
886 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
887 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdminSid);
888 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
890 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
891 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &UsersSid);
892 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
894 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
896 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
897 ok(res, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
899 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
900 ok(res, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
902 PrivSetLen = FIELD_OFFSET(PRIVILEGE_SET, Privilege[16]);
903 PrivSet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, PrivSetLen);
904 PrivSet->PrivilegeCount = 16;
906 res = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &ProcessToken);
907 ok(res, "OpenProcessToken failed with error %d\n", GetLastError());
909 pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, FALSE, TRUE, &Enabled);
911 res = DuplicateToken(ProcessToken, SecurityImpersonation, &Token);
912 ok(res, "DuplicateToken failed with error %d\n", GetLastError());
914 /* SD without owner/group */
915 SetLastError(0xdeadbeef);
916 Access = AccessStatus = 0x1abe11ed;
917 ret = AccessCheck(SecurityDescriptor, Token, KEY_QUERY_VALUE, &Mapping,
918 PrivSet, &PrivSetLen, &Access, &AccessStatus);
919 err = GetLastError();
920 ok(!ret && err == ERROR_INVALID_SECURITY_DESCR, "AccessCheck should have "
921 "failed with ERROR_INVALID_SECURITY_DESCR, instead of %d\n", err);
922 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
923 "Access and/or AccessStatus were changed!\n");
925 /* Set owner and group */
926 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
927 ok(res, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
928 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, TRUE);
929 ok(res, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
931 /* Generic access mask */
932 SetLastError(0xdeadbeef);
933 Access = AccessStatus = 0x1abe11ed;
934 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
935 PrivSet, &PrivSetLen, &Access, &AccessStatus);
936 err = GetLastError();
937 ok(!ret && err == ERROR_GENERIC_NOT_MAPPED, "AccessCheck should have failed "
938 "with ERROR_GENERIC_NOT_MAPPED, instead of %d\n", err);
939 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
940 "Access and/or AccessStatus were changed!\n");
942 /* Generic access mask - no privilegeset buffer */
943 SetLastError(0xdeadbeef);
944 Access = AccessStatus = 0x1abe11ed;
945 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
946 NULL, &PrivSetLen, &Access, &AccessStatus);
947 err = GetLastError();
948 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
949 "with ERROR_NOACCESS, instead of %d\n", err);
950 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
951 "Access and/or AccessStatus were changed!\n");
953 /* Generic access mask - no returnlength */
954 SetLastError(0xdeadbeef);
955 Access = AccessStatus = 0x1abe11ed;
956 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
957 PrivSet, NULL, &Access, &AccessStatus);
958 err = GetLastError();
959 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
960 "with ERROR_NOACCESS, instead of %d\n", err);
961 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
962 "Access and/or AccessStatus were changed!\n");
964 /* Generic access mask - no privilegeset buffer, no returnlength */
965 SetLastError(0xdeadbeef);
966 Access = AccessStatus = 0x1abe11ed;
967 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
968 NULL, NULL, &Access, &AccessStatus);
969 err = GetLastError();
970 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
971 "with ERROR_NOACCESS, instead of %d\n", err);
972 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
973 "Access and/or AccessStatus were changed!\n");
975 /* sd with no dacl present */
976 Access = AccessStatus = 0x1abe11ed;
977 ret = SetSecurityDescriptorDacl(SecurityDescriptor, FALSE, NULL, FALSE);
978 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
979 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
980 PrivSet, &PrivSetLen, &Access, &AccessStatus);
981 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
982 ok(AccessStatus && (Access == KEY_READ),
983 "AccessCheck failed to grant access with error %d\n",
986 /* sd with no dacl present - no privilegeset buffer */
987 SetLastError(0xdeadbeef);
988 Access = AccessStatus = 0x1abe11ed;
989 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
990 NULL, &PrivSetLen, &Access, &AccessStatus);
991 err = GetLastError();
992 ok(!ret && err == ERROR_NOACCESS, "AccessCheck should have failed "
993 "with ERROR_NOACCESS, instead of %d\n", err);
994 ok(Access == 0x1abe11ed && AccessStatus == 0x1abe11ed,
995 "Access and/or AccessStatus were changed!\n");
999 /* Generic access mask - no privilegeset buffer */
1000 SetLastError(0xdeadbeef);
1001 Access = ntAccessStatus = 0x1abe11ed;
1002 ntret = pNtAccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1003 NULL, &PrivSetLen, &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 /* Generic access mask - no returnlength */
1013 SetLastError(0xdeadbeef);
1014 Access = ntAccessStatus = 0x1abe11ed;
1015 ntret = pNtAccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1016 PrivSet, NULL, &Access, &ntAccessStatus);
1017 err = GetLastError();
1018 ok(ntret == STATUS_ACCESS_VIOLATION,
1019 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret);
1020 ok(err == 0xdeadbeef,
1021 "NtAccessCheck shouldn't set last error, got %d\n", err);
1022 ok(Access == 0x1abe11ed && ntAccessStatus == 0x1abe11ed,
1023 "Access and/or AccessStatus were changed!\n");
1025 /* Generic access mask - no privilegeset buffer, no returnlength */
1026 SetLastError(0xdeadbeef);
1027 Access = ntAccessStatus = 0x1abe11ed;
1028 ntret = pNtAccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
1029 NULL, NULL, &Access, &ntAccessStatus);
1030 err = GetLastError();
1031 ok(ntret == STATUS_ACCESS_VIOLATION,
1032 "NtAccessCheck should have failed with STATUS_ACCESS_VIOLATION, got %x\n", ntret);
1033 ok(err == 0xdeadbeef,
1034 "NtAccessCheck shouldn't set last error, got %d\n", err);
1035 ok(Access == 0x1abe11ed && ntAccessStatus == 0x1abe11ed,
1036 "Access and/or AccessStatus were changed!\n");
1039 win_skip("NtAccessCheck unavailable. Skipping.\n");
1041 /* sd with NULL dacl */
1042 Access = AccessStatus = 0x1abe11ed;
1043 ret = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, NULL, FALSE);
1044 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
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",
1052 /* sd with blank dacl */
1053 ret = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
1054 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1055 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1056 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1057 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1058 err = GetLastError();
1059 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
1060 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
1061 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
1063 res = AddAccessAllowedAce(Acl, ACL_REVISION, KEY_READ, EveryoneSid);
1064 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
1066 res = AddAccessDeniedAce(Acl, ACL_REVISION, KEY_SET_VALUE, AdminSid);
1067 ok(res, "AddAccessDeniedAce failed with error %d\n", GetLastError());
1070 Access = AccessStatus = 0x1abe11ed;
1071 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1072 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1073 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1074 ok(AccessStatus && (Access == KEY_READ),
1075 "AccessCheck failed to grant access with error %d\n",
1078 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
1079 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1080 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1082 "AccessCheck failed to grant any access with error %d\n",
1084 trace("AccessCheck with MAXIMUM_ALLOWED got Access 0x%08x\n", Access);
1086 /* Access denied by SD */
1087 SetLastError(0xdeadbeef);
1088 Access = AccessStatus = 0x1abe11ed;
1089 ret = AccessCheck(SecurityDescriptor, Token, KEY_WRITE, &Mapping,
1090 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1091 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1092 err = GetLastError();
1093 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
1094 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
1095 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
1098 PrivSet->PrivilegeCount = 16;
1099 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
1100 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1101 ok(ret && !AccessStatus && GetLastError() == ERROR_PRIVILEGE_NOT_HELD,
1102 "AccessCheck should have failed with ERROR_PRIVILEGE_NOT_HELD, instead of %d\n",
1105 ret = ImpersonateLoggedOnUser(Token);
1106 ok(ret, "ImpersonateLoggedOnUser failed with error %d\n", GetLastError());
1107 ret = pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, TRUE, TRUE, &Enabled);
1111 PrivSet->PrivilegeCount = 16;
1112 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
1113 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1114 ok(ret && AccessStatus && GetLastError() == 0,
1115 "AccessCheck should have succeeded, error %d\n",
1117 ok(Access == ACCESS_SYSTEM_SECURITY,
1118 "Access should be equal to ACCESS_SYSTEM_SECURITY instead of 0x%08x\n",
1122 trace("Couldn't get SE_SECURITY_PRIVILEGE (0x%08x), skipping ACCESS_SYSTEM_SECURITY test\n",
1124 ret = RevertToSelf();
1125 ok(ret, "RevertToSelf failed with error %d\n", GetLastError());
1127 /* test INHERIT_ONLY_ACE */
1128 ret = InitializeAcl(Acl, 256, ACL_REVISION);
1129 ok(ret, "InitializeAcl failed with error %d\n", GetLastError());
1131 /* NT doesn't have AddAccessAllowedAceEx. Skipping this call/test doesn't influence
1134 if (pAddAccessAllowedAceEx)
1136 ret = pAddAccessAllowedAceEx(Acl, ACL_REVISION, INHERIT_ONLY_ACE, KEY_READ, EveryoneSid);
1137 ok(ret, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
1140 win_skip("AddAccessAllowedAceEx is not available\n");
1142 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
1143 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1144 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
1145 err = GetLastError();
1146 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
1147 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
1148 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
1152 res = DuplicateToken(ProcessToken, SecurityAnonymous, &Token);
1153 ok(res, "DuplicateToken failed with error %d\n", GetLastError());
1155 SetLastError(0xdeadbeef);
1156 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
1157 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1158 err = GetLastError();
1159 ok(!ret && err == ERROR_BAD_IMPERSONATION_LEVEL, "AccessCheck should have failed "
1160 "with ERROR_BAD_IMPERSONATION_LEVEL, instead of %d\n", err);
1164 SetLastError(0xdeadbeef);
1165 ret = AccessCheck(SecurityDescriptor, ProcessToken, KEY_READ, &Mapping,
1166 PrivSet, &PrivSetLen, &Access, &AccessStatus);
1167 err = GetLastError();
1168 ok(!ret && err == ERROR_NO_IMPERSONATION_TOKEN, "AccessCheck should have failed "
1169 "with ERROR_NO_IMPERSONATION_TOKEN, instead of %d\n", err);
1171 CloseHandle(ProcessToken);
1174 FreeSid(EveryoneSid);
1179 HeapFree(GetProcessHeap(), 0, Acl);
1180 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
1181 HeapFree(GetProcessHeap(), 0, PrivSet);
1184 /* test GetTokenInformation for the various attributes */
1185 static void test_token_attr(void)
1187 HANDLE Token, ImpersonationToken;
1189 TOKEN_PRIVILEGES *Privileges;
1190 TOKEN_GROUPS *Groups;
1192 TOKEN_DEFAULT_DACL *Dacl;
1196 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel;
1199 /* cygwin-like use case */
1200 SetLastError(0xdeadbeef);
1201 ret = OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED, &Token);
1202 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1204 win_skip("OpenProcessToken is not implemented\n");
1207 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1210 DWORD buf[256]; /* GetTokenInformation wants a dword-aligned buffer */
1212 ret = GetTokenInformation(Token, TokenUser,(void*)buf, Size, &Size);
1213 ok(ret, "GetTokenInformation failed with error %d\n", GetLastError());
1214 Size = sizeof(ImpersonationLevel);
1215 ret = GetTokenInformation(Token, TokenImpersonationLevel, &ImpersonationLevel, Size, &Size);
1216 GLE = GetLastError();
1217 ok(!ret && (GLE == ERROR_INVALID_PARAMETER), "GetTokenInformation(TokenImpersonationLevel) on primary token should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GLE);
1221 if(!pConvertSidToStringSidA)
1223 win_skip("ConvertSidToStringSidA is not available\n");
1227 SetLastError(0xdeadbeef);
1228 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &Token);
1229 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1232 SetLastError(0xdeadbeef);
1233 ret = GetTokenInformation(Token, TokenGroups, NULL, 0, &Size);
1234 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1235 "GetTokenInformation(TokenGroups) %s with error %d\n",
1236 ret ? "succeeded" : "failed", GetLastError());
1237 Groups = HeapAlloc(GetProcessHeap(), 0, Size);
1238 SetLastError(0xdeadbeef);
1239 ret = GetTokenInformation(Token, TokenGroups, Groups, Size, &Size);
1240 ok(ret, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
1241 ok(GetLastError() == 0xdeadbeef,
1242 "GetTokenInformation shouldn't have set last error to %d\n",
1244 trace("TokenGroups:\n");
1245 for (i = 0; i < Groups->GroupCount; i++)
1247 DWORD NameLength = 255;
1249 DWORD DomainLength = 255;
1251 SID_NAME_USE SidNameUse;
1254 ret = LookupAccountSid(NULL, Groups->Groups[i].Sid, Name, &NameLength, Domain, &DomainLength, &SidNameUse);
1257 pConvertSidToStringSidA(Groups->Groups[i].Sid, &SidString);
1258 trace("%s, %s\\%s use: %d attr: 0x%08x\n", SidString, Domain, Name, SidNameUse, Groups->Groups[i].Attributes);
1259 LocalFree(SidString);
1261 else trace("attr: 0x%08x LookupAccountSid failed with error %d\n", Groups->Groups[i].Attributes, GetLastError());
1263 HeapFree(GetProcessHeap(), 0, Groups);
1266 ret = GetTokenInformation(Token, TokenUser, NULL, 0, &Size);
1267 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1268 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1269 User = HeapAlloc(GetProcessHeap(), 0, Size);
1270 ret = GetTokenInformation(Token, TokenUser, User, Size, &Size);
1272 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1274 pConvertSidToStringSidA(User->User.Sid, &SidString);
1275 trace("TokenUser: %s attr: 0x%08x\n", SidString, User->User.Attributes);
1276 LocalFree(SidString);
1277 HeapFree(GetProcessHeap(), 0, User);
1280 ret = GetTokenInformation(Token, TokenPrivileges, NULL, 0, &Size);
1281 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1282 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1283 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
1284 ret = GetTokenInformation(Token, TokenPrivileges, Privileges, Size, &Size);
1286 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1287 trace("TokenPrivileges:\n");
1288 for (i = 0; i < Privileges->PrivilegeCount; i++)
1291 DWORD NameLen = sizeof(Name)/sizeof(Name[0]);
1292 LookupPrivilegeName(NULL, &Privileges->Privileges[i].Luid, Name, &NameLen);
1293 trace("\t%s, 0x%x\n", Name, Privileges->Privileges[i].Attributes);
1295 HeapFree(GetProcessHeap(), 0, Privileges);
1297 ret = DuplicateToken(Token, SecurityAnonymous, &ImpersonationToken);
1298 ok(ret, "DuplicateToken failed with error %d\n", GetLastError());
1300 Size = sizeof(ImpersonationLevel);
1301 ret = GetTokenInformation(ImpersonationToken, TokenImpersonationLevel, &ImpersonationLevel, Size, &Size);
1302 ok(ret, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1303 ok(ImpersonationLevel == SecurityAnonymous, "ImpersonationLevel should have been SecurityAnonymous instead of %d\n", ImpersonationLevel);
1305 CloseHandle(ImpersonationToken);
1308 ret = GetTokenInformation(Token, TokenDefaultDacl, NULL, 0, &Size);
1309 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1310 "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1312 Dacl = HeapAlloc(GetProcessHeap(), 0, Size);
1313 ret = GetTokenInformation(Token, TokenDefaultDacl, Dacl, Size, &Size);
1314 ok(ret, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1316 SetLastError(0xdeadbeef);
1317 ret = SetTokenInformation(Token, TokenDefaultDacl, NULL, 0);
1318 GLE = GetLastError();
1319 ok(!ret, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1320 ok(GLE == ERROR_BAD_LENGTH, "expected ERROR_BAD_LENGTH got %u\n", GLE);
1322 SetLastError(0xdeadbeef);
1323 ret = SetTokenInformation(Token, TokenDefaultDacl, NULL, Size);
1324 GLE = GetLastError();
1325 ok(!ret, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1326 ok(GLE == ERROR_NOACCESS, "expected ERROR_NOACCESS got %u\n", GLE);
1328 acl = Dacl->DefaultDacl;
1329 Dacl->DefaultDacl = NULL;
1331 ret = SetTokenInformation(Token, TokenDefaultDacl, Dacl, Size);
1332 ok(ret, "SetTokenInformation(TokenDefaultDacl) succeeded\n");
1335 Dacl->DefaultDacl = (ACL *)0xdeadbeef;
1336 ret = GetTokenInformation(Token, TokenDefaultDacl, Dacl, Size, &Size2);
1337 ok(ret, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1338 ok(Dacl->DefaultDacl == NULL, "expected NULL, got %p\n", Dacl->DefaultDacl);
1339 ok(Size2 == sizeof(TOKEN_DEFAULT_DACL), "got %u expected sizeof(TOKEN_DEFAULT_DACL)\n", Size2);
1341 Dacl->DefaultDacl = acl;
1342 ret = SetTokenInformation(Token, TokenDefaultDacl, Dacl, Size);
1343 ok(ret, "SetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1345 ret = GetTokenInformation(Token, TokenDefaultDacl, Dacl, Size, &Size2);
1346 ok(ret, "GetTokenInformation(TokenDefaultDacl) failed with error %u\n", GetLastError());
1348 HeapFree(GetProcessHeap(), 0, Dacl);
1352 typedef union _MAX_SID
1355 char max[SECURITY_MAX_SID_SIZE];
1358 static void test_sid_str(PSID * sid)
1361 BOOL ret = pConvertSidToStringSidA(sid, &str_sid);
1362 ok(ret, "ConvertSidToStringSidA() failed: %d\n", GetLastError());
1365 char account[MAX_PATH], domain[MAX_PATH];
1367 DWORD acc_size = MAX_PATH;
1368 DWORD dom_size = MAX_PATH;
1369 ret = LookupAccountSid(NULL, sid, account, &acc_size, domain, &dom_size, &use);
1370 ok(ret || (!ret && (GetLastError() == ERROR_NONE_MAPPED)),
1371 "LookupAccountSid(%s) failed: %d\n", str_sid, GetLastError());
1373 trace(" %s %s\\%s %d\n", str_sid, domain, account, use);
1374 else if (GetLastError() == ERROR_NONE_MAPPED)
1375 trace(" %s couldn't be mapped\n", str_sid);
1380 struct well_known_sid_value
1382 BOOL without_domain;
1383 const char *sid_string;
1384 } well_known_sid_values[] = {
1385 /* 0 */ {TRUE, "S-1-0-0"}, {TRUE, "S-1-1-0"}, {TRUE, "S-1-2-0"}, {TRUE, "S-1-3-0"},
1386 /* 4 */ {TRUE, "S-1-3-1"}, {TRUE, "S-1-3-2"}, {TRUE, "S-1-3-3"}, {TRUE, "S-1-5"},
1387 /* 8 */ {FALSE, "S-1-5-1"}, {TRUE, "S-1-5-2"}, {TRUE, "S-1-5-3"}, {TRUE, "S-1-5-4"},
1388 /* 12 */ {TRUE, "S-1-5-6"}, {TRUE, "S-1-5-7"}, {TRUE, "S-1-5-8"}, {TRUE, "S-1-5-9"},
1389 /* 16 */ {TRUE, "S-1-5-10"}, {TRUE, "S-1-5-11"}, {TRUE, "S-1-5-12"}, {TRUE, "S-1-5-13"},
1390 /* 20 */ {TRUE, "S-1-5-14"}, {FALSE, NULL}, {TRUE, "S-1-5-18"}, {TRUE, "S-1-5-19"},
1391 /* 24 */ {TRUE, "S-1-5-20"}, {TRUE, "S-1-5-32"},
1392 /* 26 */ {FALSE, "S-1-5-32-544"}, {TRUE, "S-1-5-32-545"}, {TRUE, "S-1-5-32-546"},
1393 /* 29 */ {TRUE, "S-1-5-32-547"}, {TRUE, "S-1-5-32-548"}, {TRUE, "S-1-5-32-549"},
1394 /* 32 */ {TRUE, "S-1-5-32-550"}, {TRUE, "S-1-5-32-551"}, {TRUE, "S-1-5-32-552"},
1395 /* 35 */ {TRUE, "S-1-5-32-554"}, {TRUE, "S-1-5-32-555"}, {TRUE, "S-1-5-32-556"},
1396 /* 38 */ {FALSE, "S-1-5-21-12-23-34-45-56-500"}, {FALSE, "S-1-5-21-12-23-34-45-56-501"},
1397 /* 40 */ {FALSE, "S-1-5-21-12-23-34-45-56-502"}, {FALSE, "S-1-5-21-12-23-34-45-56-512"},
1398 /* 42 */ {FALSE, "S-1-5-21-12-23-34-45-56-513"}, {FALSE, "S-1-5-21-12-23-34-45-56-514"},
1399 /* 44 */ {FALSE, "S-1-5-21-12-23-34-45-56-515"}, {FALSE, "S-1-5-21-12-23-34-45-56-516"},
1400 /* 46 */ {FALSE, "S-1-5-21-12-23-34-45-56-517"}, {FALSE, "S-1-5-21-12-23-34-45-56-518"},
1401 /* 48 */ {FALSE, "S-1-5-21-12-23-34-45-56-519"}, {FALSE, "S-1-5-21-12-23-34-45-56-520"},
1402 /* 50 */ {FALSE, "S-1-5-21-12-23-34-45-56-553"},
1403 /* Added in Windows Server 2003 */
1404 /* 51 */ {TRUE, "S-1-5-64-10"}, {TRUE, "S-1-5-64-21"}, {TRUE, "S-1-5-64-14"},
1405 /* 54 */ {TRUE, "S-1-5-15"}, {TRUE, "S-1-5-1000"}, {FALSE, "S-1-5-32-557"},
1406 /* 57 */ {TRUE, "S-1-5-32-558"}, {TRUE, "S-1-5-32-559"}, {TRUE, "S-1-5-32-560"},
1407 /* 60 */ {TRUE, "S-1-5-32-561"}, {TRUE, "S-1-5-32-562"},
1408 /* Added in Windows Vista: */
1409 /* 62 */ {TRUE, "S-1-5-32-568"},
1410 /* 63 */ {TRUE, "S-1-5-17"}, {FALSE, "S-1-5-32-569"}, {TRUE, "S-1-16-0"},
1411 /* 66 */ {TRUE, "S-1-16-4096"}, {TRUE, "S-1-16-8192"}, {TRUE, "S-1-16-12288"},
1412 /* 69 */ {TRUE, "S-1-16-16384"}, {TRUE, "S-1-5-33"}, {TRUE, "S-1-3-4"},
1413 /* 72 */ {FALSE, "S-1-5-21-12-23-34-45-56-571"}, {FALSE, "S-1-5-21-12-23-34-45-56-572"},
1414 /* 74 */ {TRUE, "S-1-5-22"}, {FALSE, "S-1-5-21-12-23-34-45-56-521"}, {TRUE, "S-1-5-32-573"}
1417 static void test_CreateWellKnownSid(void)
1419 SID_IDENTIFIER_AUTHORITY ident = { SECURITY_NT_AUTHORITY };
1420 PSID domainsid, sid;
1425 if (!pCreateWellKnownSid)
1427 win_skip("CreateWellKnownSid not available\n");
1432 SetLastError(0xdeadbeef);
1433 ret = pCreateWellKnownSid(WinInteractiveSid, NULL, NULL, &size);
1434 error = GetLastError();
1435 ok(!ret, "CreateWellKnownSid succeeded\n");
1436 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
1437 ok(size, "expected size > 0\n");
1439 SetLastError(0xdeadbeef);
1440 ret = pCreateWellKnownSid(WinInteractiveSid, NULL, NULL, &size);
1441 error = GetLastError();
1442 ok(!ret, "CreateWellKnownSid succeeded\n");
1443 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
1445 sid = HeapAlloc(GetProcessHeap(), 0, size);
1446 ret = pCreateWellKnownSid(WinInteractiveSid, NULL, sid, &size);
1447 ok(ret, "CreateWellKnownSid failed %u\n", GetLastError());
1448 HeapFree(GetProcessHeap(), 0, sid);
1450 /* a domain sid usually have three subauthorities but we test that CreateWellKnownSid doesn't check it */
1451 AllocateAndInitializeSid(&ident, 6, SECURITY_NT_NON_UNIQUE, 12, 23, 34, 45, 56, 0, 0, &domainsid);
1453 for (i = 0; i < sizeof(well_known_sid_values)/sizeof(well_known_sid_values[0]); i++)
1455 struct well_known_sid_value *value = &well_known_sid_values[i];
1456 char sid_buffer[SECURITY_MAX_SID_SIZE];
1460 if (value->sid_string == NULL)
1463 if (i > WinAccountRasAndIasServersSid)
1465 /* These SIDs aren't implemented by all Windows versions - detect it and break the loop */
1466 cb = sizeof(sid_buffer);
1467 if (!pCreateWellKnownSid(i, domainsid, sid_buffer, &cb))
1469 skip("Well known SIDs starting from %u are not implemented\n", i);
1474 cb = sizeof(sid_buffer);
1475 ok(pCreateWellKnownSid(i, value->without_domain ? NULL : domainsid, sid_buffer, &cb), "Couldn't create well known sid %u\n", i);
1476 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer)), cb, DWORD, "%d");
1477 ok(IsValidSid(sid_buffer), "The sid is not valid\n");
1478 ok(pConvertSidToStringSidA(sid_buffer, &str), "Couldn't convert SID to string\n");
1479 ok(strcmp(str, value->sid_string) == 0, "SID mismatch - expected %s, got %s\n",
1480 value->sid_string, str);
1483 if (value->without_domain)
1485 char buf2[SECURITY_MAX_SID_SIZE];
1487 ok(pCreateWellKnownSid(i, domainsid, buf2, &cb), "Couldn't create well known sid %u with optional domain\n", i);
1488 expect_eq(GetSidLengthRequired(*GetSidSubAuthorityCount(sid_buffer)), cb, DWORD, "%d");
1489 ok(memcmp(buf2, sid_buffer, cb) == 0, "SID create with domain is different than without (%u)\n", i);
1496 static void test_LookupAccountSid(void)
1498 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
1499 CHAR accountA[MAX_PATH], domainA[MAX_PATH], usernameA[MAX_PATH];
1500 DWORD acc_sizeA, dom_sizeA, user_sizeA;
1501 DWORD real_acc_sizeA, real_dom_sizeA;
1502 WCHAR accountW[MAX_PATH], domainW[MAX_PATH];
1503 DWORD acc_sizeW, dom_sizeW;
1504 DWORD real_acc_sizeW, real_dom_sizeW;
1505 PSID pUsersSid = NULL;
1508 DWORD error, size, cbti = 0;
1513 PTOKEN_USER ptiUser = NULL;
1515 /* native windows crashes if account size, domain size, or name use is NULL */
1517 ret = AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
1518 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &pUsersSid);
1519 ok(ret || (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED),
1520 "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1522 /* not running on NT so give up */
1523 if (!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1526 real_acc_sizeA = MAX_PATH;
1527 real_dom_sizeA = MAX_PATH;
1528 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &real_acc_sizeA, domainA, &real_dom_sizeA, &use);
1529 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1531 /* try NULL account */
1532 acc_sizeA = MAX_PATH;
1533 dom_sizeA = MAX_PATH;
1534 ret = LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
1535 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1537 /* try NULL domain */
1538 acc_sizeA = MAX_PATH;
1539 dom_sizeA = MAX_PATH;
1540 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
1541 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1543 /* try a small account buffer */
1545 dom_sizeA = MAX_PATH;
1547 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1548 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1549 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1550 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1552 /* try a 0 sized account buffer */
1554 dom_sizeA = MAX_PATH;
1556 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1557 /* this can fail or succeed depending on OS version but the size will always be returned */
1558 ok(acc_sizeA == real_acc_sizeA + 1,
1559 "LookupAccountSidA() Expected acc_size = %u, got %u\n",
1560 real_acc_sizeA + 1, acc_sizeA);
1562 /* try a 0 sized account buffer */
1564 dom_sizeA = MAX_PATH;
1565 ret = LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
1566 /* this can fail or succeed depending on OS version but the size will always be returned */
1567 ok(acc_sizeA == real_acc_sizeA + 1,
1568 "LookupAccountSid() Expected acc_size = %u, got %u\n",
1569 real_acc_sizeA + 1, acc_sizeA);
1571 /* try a small domain buffer */
1573 acc_sizeA = MAX_PATH;
1575 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1576 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1577 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1578 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1580 /* try a 0 sized domain buffer */
1582 acc_sizeA = MAX_PATH;
1584 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1585 /* this can fail or succeed depending on OS version but the size will always be returned */
1586 ok(dom_sizeA == real_dom_sizeA + 1,
1587 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1588 real_dom_sizeA + 1, dom_sizeA);
1590 /* try a 0 sized domain buffer */
1592 acc_sizeA = MAX_PATH;
1593 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
1594 /* this can fail or succeed depending on OS version but the size will always be returned */
1595 ok(dom_sizeA == real_dom_sizeA + 1,
1596 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1597 real_dom_sizeA + 1, dom_sizeA);
1599 real_acc_sizeW = MAX_PATH;
1600 real_dom_sizeW = MAX_PATH;
1601 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &real_acc_sizeW, domainW, &real_dom_sizeW, &use);
1602 ok(ret, "LookupAccountSidW() Expected TRUE, got FALSE\n");
1604 /* try an invalid system name */
1605 real_acc_sizeA = MAX_PATH;
1606 real_dom_sizeA = MAX_PATH;
1607 ret = LookupAccountSidA("deepthought", pUsersSid, accountA, &real_acc_sizeA, domainA, &real_dom_sizeA, &use);
1608 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1609 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE || GetLastError() == RPC_S_INVALID_NET_ADDR /* Vista */,
1610 "LookupAccountSidA() Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %u\n", GetLastError());
1612 /* native windows crashes if domainW or accountW is NULL */
1614 /* try a small account buffer */
1616 dom_sizeW = MAX_PATH;
1618 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1619 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1620 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1621 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1623 /* try a 0 sized account buffer */
1625 dom_sizeW = MAX_PATH;
1627 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1628 /* this can fail or succeed depending on OS version but the size will always be returned */
1629 ok(acc_sizeW == real_acc_sizeW + 1,
1630 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1631 real_acc_sizeW + 1, acc_sizeW);
1633 /* try a 0 sized account buffer */
1635 dom_sizeW = MAX_PATH;
1636 ret = LookupAccountSidW(NULL, pUsersSid, NULL, &acc_sizeW, domainW, &dom_sizeW, &use);
1637 /* this can fail or succeed depending on OS version but the size will always be returned */
1638 ok(acc_sizeW == real_acc_sizeW + 1,
1639 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1640 real_acc_sizeW + 1, acc_sizeW);
1642 /* try a small domain buffer */
1644 acc_sizeW = MAX_PATH;
1646 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1647 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1648 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1649 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1651 /* try a 0 sized domain buffer */
1653 acc_sizeW = MAX_PATH;
1655 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1656 /* this can fail or succeed depending on OS version but the size will always be returned */
1657 ok(dom_sizeW == real_dom_sizeW + 1,
1658 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1659 real_dom_sizeW + 1, dom_sizeW);
1661 /* try a 0 sized domain buffer */
1663 acc_sizeW = MAX_PATH;
1664 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, NULL, &dom_sizeW, &use);
1665 /* this can fail or succeed depending on OS version but the size will always be returned */
1666 ok(dom_sizeW == real_dom_sizeW + 1,
1667 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1668 real_dom_sizeW + 1, dom_sizeW);
1670 acc_sizeW = dom_sizeW = use = 0;
1671 SetLastError(0xdeadbeef);
1672 ret = LookupAccountSidW(NULL, pUsersSid, NULL, &acc_sizeW, NULL, &dom_sizeW, &use);
1673 error = GetLastError();
1674 ok(!ret, "LookupAccountSidW failed %u\n", GetLastError());
1675 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
1676 ok(acc_sizeW, "expected non-zero account size\n");
1677 ok(dom_sizeW, "expected non-zero domain size\n");
1678 ok(!use, "expected zero use %u\n", use);
1682 /* Test LookupAccountSid with Sid retrieved from token information.
1683 This assumes this process is running under the account of the current user.*/
1684 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &hToken);
1685 ret = GetTokenInformation(hToken, TokenUser, NULL, 0, &cbti);
1686 ptiUser = HeapAlloc(GetProcessHeap(), 0, cbti);
1687 if (GetTokenInformation(hToken, TokenUser, ptiUser, cbti, &cbti))
1689 acc_sizeA = dom_sizeA = MAX_PATH;
1690 ret = LookupAccountSidA(NULL, ptiUser->User.Sid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1691 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1692 user_sizeA = MAX_PATH;
1693 ret = GetUserNameA(usernameA , &user_sizeA);
1694 ok(ret, "GetUserNameA() Expected TRUE, got FALSE\n");
1697 ok(lstrcmpA(usernameA, accountA) == 0, "LookupAccountSidA() Expected account name: %s got: %s\n", usernameA, accountA );
1700 HeapFree(GetProcessHeap(), 0, ptiUser);
1702 if (pCreateWellKnownSid && pConvertSidToStringSidA)
1704 trace("Well Known SIDs:\n");
1705 for (i = 0; i <= 60; i++)
1707 size = SECURITY_MAX_SID_SIZE;
1708 if (pCreateWellKnownSid(i, NULL, &max_sid.sid, &size))
1710 if (pConvertSidToStringSidA(&max_sid.sid, &str_sidA))
1712 acc_sizeA = MAX_PATH;
1713 dom_sizeA = MAX_PATH;
1714 if (LookupAccountSidA(NULL, &max_sid.sid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use))
1715 trace(" %d: %s %s\\%s %d\n", i, str_sidA, domainA, accountA, use);
1716 LocalFree(str_sidA);
1721 if (GetLastError() != ERROR_INVALID_PARAMETER)
1722 trace(" CreateWellKnownSid(%d) failed: %d\n", i, GetLastError());
1724 trace(" %d: not supported\n", i);
1728 pLsaQueryInformationPolicy = (fnLsaQueryInformationPolicy)GetProcAddress( hmod, "LsaQueryInformationPolicy");
1729 pLsaOpenPolicy = (fnLsaOpenPolicy)GetProcAddress( hmod, "LsaOpenPolicy");
1730 pLsaFreeMemory = (fnLsaFreeMemory)GetProcAddress( hmod, "LsaFreeMemory");
1731 pLsaClose = (fnLsaClose)GetProcAddress( hmod, "LsaClose");
1733 if (pLsaQueryInformationPolicy && pLsaOpenPolicy && pLsaFreeMemory && pLsaClose)
1737 LSA_OBJECT_ATTRIBUTES object_attributes;
1739 ZeroMemory(&object_attributes, sizeof(object_attributes));
1740 object_attributes.Length = sizeof(object_attributes);
1742 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_ALL_ACCESS, &handle);
1743 ok(status == STATUS_SUCCESS || status == STATUS_ACCESS_DENIED,
1744 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status);
1746 /* try a more restricted access mask if necessary */
1747 if (status == STATUS_ACCESS_DENIED) {
1748 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
1749 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_VIEW_LOCAL_INFORMATION, &handle);
1750 ok(status == STATUS_SUCCESS, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status);
1753 if (status == STATUS_SUCCESS)
1755 PPOLICY_ACCOUNT_DOMAIN_INFO info;
1756 status = pLsaQueryInformationPolicy(handle, PolicyAccountDomainInformation, (PVOID*)&info);
1757 ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy() failed, returned 0x%08x\n", status);
1758 if (status == STATUS_SUCCESS)
1760 ok(info->DomainSid!=0, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) missing SID\n");
1761 if (info->DomainSid)
1763 int count = *GetSidSubAuthorityCount(info->DomainSid);
1764 CopySid(GetSidLengthRequired(count), &max_sid, info->DomainSid);
1765 test_sid_str((PSID)&max_sid.sid);
1766 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_ADMIN;
1767 max_sid.sid.SubAuthorityCount = count + 1;
1768 test_sid_str((PSID)&max_sid.sid);
1769 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_GUEST;
1770 test_sid_str((PSID)&max_sid.sid);
1771 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ADMINS;
1772 test_sid_str((PSID)&max_sid.sid);
1773 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_USERS;
1774 test_sid_str((PSID)&max_sid.sid);
1775 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_GUESTS;
1776 test_sid_str((PSID)&max_sid.sid);
1777 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_COMPUTERS;
1778 test_sid_str((PSID)&max_sid.sid);
1779 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CONTROLLERS;
1780 test_sid_str((PSID)&max_sid.sid);
1781 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CERT_ADMINS;
1782 test_sid_str((PSID)&max_sid.sid);
1783 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_SCHEMA_ADMINS;
1784 test_sid_str((PSID)&max_sid.sid);
1785 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ENTERPRISE_ADMINS;
1786 test_sid_str((PSID)&max_sid.sid);
1787 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_POLICY_ADMINS;
1788 test_sid_str((PSID)&max_sid.sid);
1789 max_sid.sid.SubAuthority[count] = DOMAIN_ALIAS_RID_RAS_SERVERS;
1790 test_sid_str((PSID)&max_sid.sid);
1791 max_sid.sid.SubAuthority[count] = 1000; /* first user account */
1792 test_sid_str((PSID)&max_sid.sid);
1795 pLsaFreeMemory((LPVOID)info);
1798 status = pLsaClose(handle);
1799 ok(status == STATUS_SUCCESS, "LsaClose() failed, returned 0x%08x\n", status);
1805 static BOOL get_sid_info(PSID psid, LPSTR *user, LPSTR *dom)
1807 static CHAR account[UNLEN + 1];
1808 static CHAR domain[UNLEN + 1];
1809 DWORD size, dom_size;
1815 size = dom_size = UNLEN + 1;
1818 SetLastError(0xdeadbeef);
1819 return LookupAccountSidA(NULL, psid, account, &size, domain, &dom_size, &use);
1822 static void check_wellknown_name(const char* name, WELL_KNOWN_SID_TYPE result)
1824 SID_IDENTIFIER_AUTHORITY ident = { SECURITY_NT_AUTHORITY };
1825 PSID domainsid = NULL;
1826 char wk_sid[SECURITY_MAX_SID_SIZE];
1829 DWORD sid_size, domain_size;
1830 SID_NAME_USE sid_use;
1831 LPSTR domain, account, sid_domain, wk_domain, wk_account;
1837 ret = LookupAccountNameA(NULL, name, NULL, &sid_size, NULL, &domain_size, &sid_use);
1838 psid = HeapAlloc(GetProcessHeap(),0,sid_size);
1839 domain = HeapAlloc(GetProcessHeap(),0,domain_size);
1840 ret = LookupAccountNameA(NULL, name, psid, &sid_size, domain, &domain_size, &sid_use);
1844 ok(!ret, " %s Should have failed to lookup account name\n",name);
1848 AllocateAndInitializeSid(&ident, 6, SECURITY_NT_NON_UNIQUE, 12, 23, 34, 45, 56, 0, 0, &domainsid);
1849 cb = sizeof(wk_sid);
1850 if (!pCreateWellKnownSid(result, domainsid, wk_sid, &cb))
1852 win_skip("SID %i is not available on the system\n",result);
1856 ret2 = get_sid_info(wk_sid, &wk_account, &wk_domain);
1857 if (!ret2 && GetLastError() == ERROR_NONE_MAPPED)
1859 win_skip("CreateWellKnownSid() succeeded but the account '%s' is not present (W2K)\n", name);
1863 get_sid_info(psid, &account, &sid_domain);
1865 ok(ret, "Failed to lookup account name %s\n",name);
1866 ok(sid_size != 0, "sid_size was zero\n");
1868 ok(EqualSid(psid,wk_sid),"(%s) Sids fail to match well known sid!\n",name);
1870 ok(!lstrcmp(account, wk_account), "Expected %s , got %s\n", account, wk_account);
1871 ok(!lstrcmp(domain, wk_domain), "Expected %s, got %s\n", wk_domain, domain);
1872 ok(sid_use == SidTypeWellKnownGroup , "Expected Use (5), got %d\n", sid_use);
1876 HeapFree(GetProcessHeap(),0,psid);
1877 HeapFree(GetProcessHeap(),0,domain);
1880 static void test_LookupAccountName(void)
1882 DWORD sid_size, domain_size, user_size;
1883 DWORD sid_save, domain_save;
1884 CHAR user_name[UNLEN + 1];
1885 CHAR computer_name[UNLEN + 1];
1886 SID_NAME_USE sid_use;
1887 LPSTR domain, account, sid_dom;
1891 /* native crashes if (assuming all other parameters correct):
1893 * - Sid is NULL and cbSid is > 0
1894 * - cbSid or cchReferencedDomainName are NULL
1895 * - ReferencedDomainName is NULL and cchReferencedDomainName is the correct size
1898 user_size = UNLEN + 1;
1899 SetLastError(0xdeadbeef);
1900 ret = GetUserNameA(user_name, &user_size);
1901 if (!ret && (GetLastError() == ERROR_NOT_LOGGED_ON))
1903 /* Probably on win9x where the user used 'Cancel' instead of properly logging in */
1904 skip("Cannot get the user name (win9x and not logged in properly)\n");
1907 ok(ret, "Failed to get user name : %d\n", GetLastError());
1912 sid_use = 0xcafebabe;
1913 SetLastError(0xdeadbeef);
1914 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, NULL, &domain_size, &sid_use);
1915 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1917 win_skip("LookupAccountNameA is not implemented\n");
1920 ok(!ret, "Expected 0, got %d\n", ret);
1921 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1922 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1923 ok(sid_size != 0, "Expected non-zero sid size\n");
1924 ok(domain_size != 0, "Expected non-zero domain size\n");
1925 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
1927 sid_save = sid_size;
1928 domain_save = domain_size;
1930 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
1931 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
1933 /* try valid account name */
1934 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, domain, &domain_size, &sid_use);
1935 get_sid_info(psid, &account, &sid_dom);
1936 ok(ret, "Failed to lookup account name\n");
1937 ok(sid_size == GetLengthSid(psid), "Expected %d, got %d\n", GetLengthSid(psid), sid_size);
1940 ok(!lstrcmp(account, user_name), "Expected %s, got %s\n", user_name, account);
1941 ok(!lstrcmp(domain, sid_dom), "Expected %s, got %s\n", sid_dom, domain);
1943 ok(domain_size == domain_save - 1, "Expected %d, got %d\n", domain_save - 1, domain_size);
1944 ok(strlen(domain) == domain_size, "Expected %d, got %d\n", lstrlen(domain), domain_size);
1945 ok(sid_use == SidTypeUser, "Expected SidTypeUser (%d), got %d\n", SidTypeUser, sid_use);
1946 domain_size = domain_save;
1947 sid_size = sid_save;
1949 if (PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale())) != LANG_ENGLISH)
1951 skip("Non-english locale (test with hardcoded 'Everyone')\n");
1955 ret = LookupAccountNameA(NULL, "Everyone", psid, &sid_size, domain, &domain_size, &sid_use);
1956 get_sid_info(psid, &account, &sid_dom);
1957 ok(ret, "Failed to lookup account name\n");
1958 ok(sid_size != 0, "sid_size was zero\n");
1959 ok(!lstrcmp(account, "Everyone"), "Expected Everyone, got %s\n", account);
1960 ok(!lstrcmp(domain, sid_dom), "Expected %s, got %s\n", sid_dom, domain);
1961 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
1962 ok(strlen(domain) == domain_size, "Expected %d, got %d\n", lstrlen(domain), domain_size);
1963 ok(sid_use == SidTypeWellKnownGroup, "Expected SidTypeWellKnownGroup (%d), got %d\n", SidTypeWellKnownGroup, sid_use);
1964 domain_size = domain_save;
1967 /* NULL Sid with zero sid size */
1968 SetLastError(0xdeadbeef);
1970 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
1971 ok(!ret, "Expected 0, got %d\n", ret);
1972 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1973 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1974 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1975 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1977 /* try cchReferencedDomainName - 1 */
1978 SetLastError(0xdeadbeef);
1980 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
1981 ok(!ret, "Expected 0, got %d\n", ret);
1982 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1983 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1984 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1985 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1987 /* NULL ReferencedDomainName with zero domain name size */
1988 SetLastError(0xdeadbeef);
1990 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, NULL, &domain_size, &sid_use);
1991 ok(!ret, "Expected 0, got %d\n", ret);
1992 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1993 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1994 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1995 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1997 HeapFree(GetProcessHeap(), 0, psid);
1998 HeapFree(GetProcessHeap(), 0, domain);
2000 /* get sizes for NULL account name */
2003 sid_use = 0xcafebabe;
2004 SetLastError(0xdeadbeef);
2005 ret = LookupAccountNameA(NULL, NULL, NULL, &sid_size, NULL, &domain_size, &sid_use);
2006 if (!ret && GetLastError() == ERROR_NONE_MAPPED)
2007 win_skip("NULL account name doesn't work on NT4\n");
2010 ok(!ret, "Expected 0, got %d\n", ret);
2011 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2012 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2013 ok(sid_size != 0, "Expected non-zero sid size\n");
2014 ok(domain_size != 0, "Expected non-zero domain size\n");
2015 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
2017 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
2018 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
2020 /* try NULL account name */
2021 ret = LookupAccountNameA(NULL, NULL, psid, &sid_size, domain, &domain_size, &sid_use);
2022 get_sid_info(psid, &account, &sid_dom);
2023 ok(ret, "Failed to lookup account name\n");
2024 /* Using a fixed string will not work on different locales */
2025 ok(!lstrcmp(account, domain),
2026 "Got %s for account and %s for domain, these should be the same\n",
2028 ok(sid_use == SidTypeDomain, "Expected SidTypeDomain (%d), got %d\n", SidTypeDomain, sid_use);
2030 HeapFree(GetProcessHeap(), 0, psid);
2031 HeapFree(GetProcessHeap(), 0, domain);
2034 /* try an invalid account name */
2035 SetLastError(0xdeadbeef);
2038 ret = LookupAccountNameA(NULL, "oogabooga", NULL, &sid_size, NULL, &domain_size, &sid_use);
2039 ok(!ret, "Expected 0, got %d\n", ret);
2040 ok(GetLastError() == ERROR_NONE_MAPPED ||
2041 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE),
2042 "Expected ERROR_NONE_MAPPED, got %d\n", GetLastError());
2043 ok(sid_size == 0, "Expected 0, got %d\n", sid_size);
2044 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
2046 /* try an invalid system name */
2047 SetLastError(0xdeadbeef);
2050 ret = LookupAccountNameA("deepthought", NULL, NULL, &sid_size, NULL, &domain_size, &sid_use);
2051 ok(!ret, "Expected 0, got %d\n", ret);
2052 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE || GetLastError() == RPC_S_INVALID_NET_ADDR /* Vista */,
2053 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
2054 ok(sid_size == 0, "Expected 0, got %d\n", sid_size);
2055 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
2057 /* try with the computer name as the account name */
2058 domain_size = sizeof(computer_name);
2059 GetComputerNameA(computer_name, &domain_size);
2062 ret = LookupAccountNameA(NULL, computer_name, NULL, &sid_size, NULL, &domain_size, &sid_use);
2063 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER ||
2064 GetLastError() == ERROR_NONE_MAPPED /* in a domain */ ||
2065 broken(GetLastError() == ERROR_TRUSTED_DOMAIN_FAILURE) ||
2066 broken(GetLastError() == ERROR_TRUSTED_RELATIONSHIP_FAILURE)),
2067 "LookupAccountNameA failed: %d\n", GetLastError());
2068 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
2070 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
2071 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
2072 ret = LookupAccountNameA(NULL, computer_name, psid, &sid_size, domain, &domain_size, &sid_use);
2073 ok(ret, "LookupAccountNameA failed: %d\n", GetLastError());
2074 ok(sid_use == SidTypeDomain ||
2075 (sid_use == SidTypeUser && ! strcmp(computer_name, user_name)), "expected SidTypeDomain for %s, got %d\n", computer_name, sid_use);
2076 HeapFree(GetProcessHeap(), 0, domain);
2077 HeapFree(GetProcessHeap(), 0, psid);
2080 /* Well Known names */
2081 if (!pCreateWellKnownSid)
2083 win_skip("CreateWellKnownSid not available\n");
2087 if (PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale())) != LANG_ENGLISH)
2089 skip("Non-english locale (skipping well known name creation tests)\n");
2093 check_wellknown_name("LocalService", WinLocalServiceSid);
2094 check_wellknown_name("Local Service", WinLocalServiceSid);
2096 check_wellknown_name("Local Service", 0);
2097 check_wellknown_name("NetworkService", WinNetworkServiceSid);
2098 check_wellknown_name("Network Service", WinNetworkServiceSid);
2100 /* example of some names where the spaces are not optional */
2101 check_wellknown_name("Terminal Server User", WinTerminalServerSid);
2102 check_wellknown_name("TerminalServer User", 0);
2103 check_wellknown_name("TerminalServerUser", 0);
2104 check_wellknown_name("Terminal ServerUser", 0);
2106 check_wellknown_name("enterprise domain controllers",WinEnterpriseControllersSid);
2107 check_wellknown_name("enterprisedomain controllers", 0);
2108 check_wellknown_name("enterprise domaincontrollers", 0);
2109 check_wellknown_name("enterprisedomaincontrollers", 0);
2111 /* case insensitivity */
2112 check_wellknown_name("lOCAlServICE", WinLocalServiceSid);
2114 /* fully qualified account names */
2115 check_wellknown_name("NT AUTHORITY\\LocalService", WinLocalServiceSid);
2116 check_wellknown_name("nt authority\\Network Service", WinNetworkServiceSid);
2117 check_wellknown_name("nt authority test\\Network Service", 0);
2118 check_wellknown_name("Dummy\\Network Service", 0);
2119 check_wellknown_name("ntauthority\\Network Service", 0);
2122 static void test_security_descriptor(void)
2124 SECURITY_DESCRIPTOR sd;
2127 BOOL isDefault, isPresent, ret;
2131 SetLastError(0xdeadbeef);
2132 ret = InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
2133 if (ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2135 win_skip("InitializeSecurityDescriptor is not implemented\n");
2139 ok(GetSecurityDescriptorOwner(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
2140 expect_eq(psid, NULL, PSID, "%p");
2141 expect_eq(isDefault, FALSE, BOOL, "%d");
2142 sd.Control |= SE_DACL_PRESENT | SE_SACL_PRESENT;
2144 SetLastError(0xdeadbeef);
2146 expect_eq(MakeSelfRelativeSD(&sd, buf, &size), FALSE, BOOL, "%d");
2147 expect_eq(GetLastError(), ERROR_INSUFFICIENT_BUFFER, DWORD, "%u");
2148 ok(size > 5, "Size not increased\n");
2151 expect_eq(MakeSelfRelativeSD(&sd, buf, &size), TRUE, BOOL, "%d");
2152 ok(GetSecurityDescriptorOwner(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
2153 expect_eq(psid, NULL, PSID, "%p");
2154 expect_eq(isDefault, FALSE, BOOL, "%d");
2155 ok(GetSecurityDescriptorGroup(&sd, &psid, &isDefault), "GetSecurityDescriptorGroup failed\n");
2156 expect_eq(psid, NULL, PSID, "%p");
2157 expect_eq(isDefault, FALSE, BOOL, "%d");
2158 ok(GetSecurityDescriptorDacl(&sd, &isPresent, &pacl, &isDefault), "GetSecurityDescriptorDacl failed\n");
2159 expect_eq(isPresent, TRUE, BOOL, "%d");
2160 expect_eq(psid, NULL, PSID, "%p");
2161 expect_eq(isDefault, FALSE, BOOL, "%d");
2162 ok(GetSecurityDescriptorSacl(&sd, &isPresent, &pacl, &isDefault), "GetSecurityDescriptorSacl failed\n");
2163 expect_eq(isPresent, TRUE, BOOL, "%d");
2164 expect_eq(psid, NULL, PSID, "%p");
2165 expect_eq(isDefault, FALSE, BOOL, "%d");
2169 #define TEST_GRANTED_ACCESS(a,b) test_granted_access(a,b,0,__LINE__)
2170 #define TEST_GRANTED_ACCESS2(a,b,c) test_granted_access(a,b,c,__LINE__)
2171 static void test_granted_access(HANDLE handle, ACCESS_MASK access,
2172 ACCESS_MASK alt, int line)
2174 OBJECT_BASIC_INFORMATION obj_info;
2177 if (!pNtQueryObject)
2179 skip_(__FILE__, line)("Not NT platform - skipping tests\n");
2183 status = pNtQueryObject( handle, ObjectBasicInformation, &obj_info,
2184 sizeof(obj_info), NULL );
2185 ok_(__FILE__, line)(!status, "NtQueryObject with err: %08x\n", status);
2187 ok_(__FILE__, line)(obj_info.GrantedAccess == access ||
2188 obj_info.GrantedAccess == alt, "Granted access should be 0x%08x "
2189 "or 0x%08x, instead of 0x%08x\n", access, alt, obj_info.GrantedAccess);
2191 ok_(__FILE__, line)(obj_info.GrantedAccess == access, "Granted access should "
2192 "be 0x%08x, instead of 0x%08x\n", access, obj_info.GrantedAccess);
2195 #define CHECK_SET_SECURITY(o,i,e) \
2199 SetLastError( 0xdeadbeef ); \
2200 res = SetKernelObjectSecurity( o, i, SecurityDescriptor ); \
2201 err = GetLastError(); \
2202 if (e == ERROR_SUCCESS) \
2203 ok(res, "SetKernelObjectSecurity failed with %d\n", err); \
2205 ok(!res && err == e, "SetKernelObjectSecurity should have failed " \
2206 "with %s, instead of %d\n", #e, err); \
2209 static void test_process_security(void)
2213 PTOKEN_PRIMARY_GROUP group;
2214 PSID AdminSid = NULL, UsersSid = NULL;
2216 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL;
2217 char buffer[MAX_PATH];
2218 PROCESS_INFORMATION info;
2219 STARTUPINFOA startup;
2220 SECURITY_ATTRIBUTES psa;
2221 HANDLE token, event;
2224 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
2225 res = InitializeAcl(Acl, 256, ACL_REVISION);
2226 if (!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2228 win_skip("ACLs not implemented - skipping tests\n");
2229 HeapFree(GetProcessHeap(), 0, Acl);
2232 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
2234 /* get owner from the token we might be running as a user not admin */
2235 res = OpenProcessToken( GetCurrentProcess(), MAXIMUM_ALLOWED, &token );
2236 ok(res, "OpenProcessToken failed with error %d\n", GetLastError());
2239 HeapFree(GetProcessHeap(), 0, Acl);
2243 res = GetTokenInformation( token, TokenOwner, NULL, 0, &size );
2244 ok(!res, "Expected failure, got %d\n", res);
2245 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2246 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2248 owner = HeapAlloc(GetProcessHeap(), 0, size);
2249 res = GetTokenInformation( token, TokenOwner, owner, size, &size );
2250 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
2251 AdminSid = ((TOKEN_OWNER*)owner)->Owner;
2253 res = GetTokenInformation( token, TokenPrimaryGroup, NULL, 0, &size );
2254 ok(!res, "Expected failure, got %d\n", res);
2255 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
2256 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2258 group = HeapAlloc(GetProcessHeap(), 0, size);
2259 res = GetTokenInformation( token, TokenPrimaryGroup, group, size, &size );
2260 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
2261 UsersSid = ((TOKEN_PRIMARY_GROUP*)group)->PrimaryGroup;
2263 CloseHandle( token );
2266 HeapFree(GetProcessHeap(), 0, group);
2267 HeapFree(GetProcessHeap(), 0, owner);
2268 HeapFree(GetProcessHeap(), 0, Acl);
2272 res = AddAccessDeniedAce(Acl, ACL_REVISION, PROCESS_VM_READ, AdminSid);
2273 ok(res, "AddAccessDeniedAce failed with error %d\n", GetLastError());
2274 res = AddAccessAllowedAce(Acl, ACL_REVISION, PROCESS_ALL_ACCESS, AdminSid);
2275 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2277 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
2278 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
2279 ok(res, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
2281 event = CreateEvent( NULL, TRUE, TRUE, "test_event" );
2282 ok(event != NULL, "CreateEvent %d\n", GetLastError());
2284 SecurityDescriptor->Revision = 0;
2285 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_UNKNOWN_REVISION );
2286 SecurityDescriptor->Revision = SECURITY_DESCRIPTOR_REVISION;
2288 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_INVALID_SECURITY_DESCR );
2289 CHECK_SET_SECURITY( event, GROUP_SECURITY_INFORMATION, ERROR_INVALID_SECURITY_DESCR );
2290 CHECK_SET_SECURITY( event, SACL_SECURITY_INFORMATION, ERROR_ACCESS_DENIED );
2291 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
2292 /* NULL DACL is valid and means default DACL from token */
2293 SecurityDescriptor->Control |= SE_DACL_PRESENT;
2294 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
2296 /* Set owner and group and dacl */
2297 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
2298 ok(res, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
2299 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_SUCCESS );
2300 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, FALSE);
2301 ok(res, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2302 CHECK_SET_SECURITY( event, GROUP_SECURITY_INFORMATION, ERROR_SUCCESS );
2303 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
2304 ok(res, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
2305 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
2307 sprintf(buffer, "%s tests/security.c test", myARGV[0]);
2308 memset(&startup, 0, sizeof(startup));
2309 startup.cb = sizeof(startup);
2310 startup.dwFlags = STARTF_USESHOWWINDOW;
2311 startup.wShowWindow = SW_SHOWNORMAL;
2313 psa.nLength = sizeof(psa);
2314 psa.lpSecurityDescriptor = SecurityDescriptor;
2315 psa.bInheritHandle = TRUE;
2317 /* Doesn't matter what ACL say we should get full access for ourselves */
2318 ok(CreateProcessA( NULL, buffer, &psa, NULL, FALSE, 0, NULL, NULL, &startup, &info ),
2319 "CreateProcess with err:%d\n", GetLastError());
2320 TEST_GRANTED_ACCESS2( info.hProcess, PROCESS_ALL_ACCESS,
2321 STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL );
2322 winetest_wait_child_process( info.hProcess );
2324 CloseHandle( info.hProcess );
2325 CloseHandle( info.hThread );
2326 CloseHandle( event );
2327 HeapFree(GetProcessHeap(), 0, group);
2328 HeapFree(GetProcessHeap(), 0, owner);
2329 HeapFree(GetProcessHeap(), 0, Acl);
2330 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
2333 static void test_process_security_child(void)
2335 HANDLE handle, handle1;
2339 handle = OpenProcess( PROCESS_TERMINATE, FALSE, GetCurrentProcessId() );
2340 ok(handle != NULL, "OpenProcess(PROCESS_TERMINATE) with err:%d\n", GetLastError());
2341 TEST_GRANTED_ACCESS( handle, PROCESS_TERMINATE );
2343 ok(DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
2344 &handle1, 0, TRUE, DUPLICATE_SAME_ACCESS ),
2345 "duplicating handle err:%d\n", GetLastError());
2346 TEST_GRANTED_ACCESS( handle1, PROCESS_TERMINATE );
2348 CloseHandle( handle1 );
2350 SetLastError( 0xdeadbeef );
2351 ret = DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
2352 &handle1, PROCESS_ALL_ACCESS, TRUE, 0 );
2353 err = GetLastError();
2355 ok(!ret && err == ERROR_ACCESS_DENIED, "duplicating handle should have failed "
2356 "with STATUS_ACCESS_DENIED, instead of err:%d\n", err);
2358 CloseHandle( handle );
2360 /* These two should fail - they are denied by ACL */
2361 handle = OpenProcess( PROCESS_VM_READ, FALSE, GetCurrentProcessId() );
2363 ok(handle == NULL, "OpenProcess(PROCESS_VM_READ) should have failed\n");
2364 handle = OpenProcess( PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId() );
2366 ok(handle == NULL, "OpenProcess(PROCESS_ALL_ACCESS) should have failed\n");
2368 /* Documented privilege elevation */
2369 ok(DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2370 &handle, 0, TRUE, DUPLICATE_SAME_ACCESS ),
2371 "duplicating handle err:%d\n", GetLastError());
2372 TEST_GRANTED_ACCESS2( handle, PROCESS_ALL_ACCESS,
2373 STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL );
2375 CloseHandle( handle );
2377 /* Same only explicitly asking for all access rights */
2378 ok(DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
2379 &handle, PROCESS_ALL_ACCESS, TRUE, 0 ),
2380 "duplicating handle err:%d\n", GetLastError());
2381 TEST_GRANTED_ACCESS2( handle, PROCESS_ALL_ACCESS,
2382 PROCESS_ALL_ACCESS | PROCESS_QUERY_LIMITED_INFORMATION );
2383 ok(DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
2384 &handle1, PROCESS_VM_READ, TRUE, 0 ),
2385 "duplicating handle err:%d\n", GetLastError());
2386 TEST_GRANTED_ACCESS( handle1, PROCESS_VM_READ );
2387 CloseHandle( handle1 );
2388 CloseHandle( handle );
2391 static void test_impersonation_level(void)
2393 HANDLE Token, ProcessToken;
2396 TOKEN_PRIVILEGES *Privileges;
2398 PRIVILEGE_SET *PrivilegeSet;
2404 pDuplicateTokenEx = (fnDuplicateTokenEx) GetProcAddress(hmod, "DuplicateTokenEx");
2405 if( !pDuplicateTokenEx ) {
2406 win_skip("DuplicateTokenEx is not available\n");
2409 SetLastError(0xdeadbeef);
2410 ret = ImpersonateSelf(SecurityAnonymous);
2411 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2413 win_skip("ImpersonateSelf is not implemented\n");
2416 ok(ret, "ImpersonateSelf(SecurityAnonymous) failed with error %d\n", GetLastError());
2417 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
2418 ok(!ret, "OpenThreadToken should have failed\n");
2419 error = GetLastError();
2420 ok(error == ERROR_CANT_OPEN_ANONYMOUS, "OpenThreadToken on anonymous token should have returned ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error);
2421 /* can't perform access check when opening object against an anonymous impersonation token */
2423 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
2424 ok(error == ERROR_INVALID_HANDLE || error == ERROR_CANT_OPEN_ANONYMOUS,
2425 "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE or ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error);
2429 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, &ProcessToken);
2430 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
2432 ret = pDuplicateTokenEx(ProcessToken,
2433 TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_IMPERSONATE, NULL,
2434 SecurityAnonymous, TokenImpersonation, &Token);
2435 ok(ret, "DuplicateTokenEx failed with error %d\n", GetLastError());
2436 /* can't increase the impersonation level */
2437 ret = DuplicateToken(Token, SecurityIdentification, &Token2);
2438 error = GetLastError();
2439 ok(!ret && error == ERROR_BAD_IMPERSONATION_LEVEL,
2440 "Duplicating a token and increasing the impersonation level should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
2441 /* we can query anything from an anonymous token, including the user */
2442 ret = GetTokenInformation(Token, TokenUser, NULL, 0, &Size);
2443 error = GetLastError();
2444 ok(!ret && error == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenUser) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error);
2445 User = HeapAlloc(GetProcessHeap(), 0, Size);
2446 ret = GetTokenInformation(Token, TokenUser, User, Size, &Size);
2447 ok(ret, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
2448 HeapFree(GetProcessHeap(), 0, User);
2450 /* PrivilegeCheck fails with SecurityAnonymous level */
2451 ret = GetTokenInformation(Token, TokenPrivileges, NULL, 0, &Size);
2452 error = GetLastError();
2453 ok(!ret && error == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenPrivileges) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error);
2454 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
2455 ret = GetTokenInformation(Token, TokenPrivileges, Privileges, Size, &Size);
2456 ok(ret, "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
2458 PrivilegeSet = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PRIVILEGE_SET, Privilege[Privileges->PrivilegeCount]));
2459 PrivilegeSet->PrivilegeCount = Privileges->PrivilegeCount;
2460 memcpy(PrivilegeSet->Privilege, Privileges->Privileges, PrivilegeSet->PrivilegeCount * sizeof(PrivilegeSet->Privilege[0]));
2461 PrivilegeSet->Control = PRIVILEGE_SET_ALL_NECESSARY;
2462 HeapFree(GetProcessHeap(), 0, Privileges);
2464 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2465 error = GetLastError();
2466 ok(!ret && error == ERROR_BAD_IMPERSONATION_LEVEL, "PrivilegeCheck for SecurityAnonymous token should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
2470 ret = ImpersonateSelf(SecurityIdentification);
2471 ok(ret, "ImpersonateSelf(SecurityIdentification) failed with error %d\n", GetLastError());
2472 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
2473 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
2475 /* can't perform access check when opening object against an identification impersonation token */
2476 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
2478 ok(error == ERROR_INVALID_HANDLE || error == ERROR_BAD_IMPERSONATION_LEVEL,
2479 "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE or ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
2481 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2482 ok(ret, "PrivilegeCheck for SecurityIdentification failed with error %d\n", GetLastError());
2486 ret = ImpersonateSelf(SecurityImpersonation);
2487 ok(ret, "ImpersonateSelf(SecurityImpersonation) failed with error %d\n", GetLastError());
2488 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
2489 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
2490 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
2491 ok(error == ERROR_SUCCESS, "RegOpenKeyEx should have succeeded instead of failing with %d\n", error);
2493 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
2494 ok(ret, "PrivilegeCheck for SecurityImpersonation failed with error %d\n", GetLastError());
2498 CloseHandle(ProcessToken);
2500 HeapFree(GetProcessHeap(), 0, PrivilegeSet);
2503 static void test_SetEntriesInAcl(void)
2506 PSID EveryoneSid = NULL, UsersSid = NULL;
2507 PACL OldAcl = NULL, NewAcl;
2508 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
2509 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
2510 EXPLICIT_ACCESSW ExplicitAccess;
2511 static const WCHAR wszEveryone[] = {'E','v','e','r','y','o','n','e',0};
2512 static const WCHAR wszCurrentUser[] = { 'C','U','R','R','E','N','T','_','U','S','E','R','\0'};
2514 if (!pSetEntriesInAclW)
2516 win_skip("SetEntriesInAclW is not available\n");
2520 NewAcl = (PACL)0xdeadbeef;
2521 res = pSetEntriesInAclW(0, NULL, NULL, &NewAcl);
2522 if(res == ERROR_CALL_NOT_IMPLEMENTED)
2524 win_skip("SetEntriesInAclW is not implemented\n");
2527 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2528 ok(NewAcl == NULL ||
2529 broken(NewAcl != NULL), /* NT4 */
2530 "NewAcl=%p, expected NULL\n", NewAcl);
2533 OldAcl = HeapAlloc(GetProcessHeap(), 0, 256);
2534 res = InitializeAcl(OldAcl, 256, ACL_REVISION);
2535 if(!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2537 win_skip("ACLs not implemented - skipping tests\n");
2538 HeapFree(GetProcessHeap(), 0, OldAcl);
2541 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
2543 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
2544 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2546 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
2547 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &UsersSid);
2548 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
2550 res = AddAccessAllowedAce(OldAcl, ACL_REVISION, KEY_READ, UsersSid);
2551 ok(res, "AddAccessAllowedAce failed with error %d\n", GetLastError());
2553 ExplicitAccess.grfAccessPermissions = KEY_WRITE;
2554 ExplicitAccess.grfAccessMode = GRANT_ACCESS;
2555 ExplicitAccess.grfInheritance = NO_INHERITANCE;
2556 ExplicitAccess.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
2557 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
2558 ExplicitAccess.Trustee.ptstrName = EveryoneSid;
2559 ExplicitAccess.Trustee.MultipleTrusteeOperation = 0xDEADBEEF;
2560 ExplicitAccess.Trustee.pMultipleTrustee = (PVOID)0xDEADBEEF;
2561 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2562 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2563 ok(NewAcl != NULL, "returned acl was NULL\n");
2566 ExplicitAccess.Trustee.TrusteeType = TRUSTEE_IS_UNKNOWN;
2567 ExplicitAccess.Trustee.pMultipleTrustee = NULL;
2568 ExplicitAccess.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2569 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2570 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2571 ok(NewAcl != NULL, "returned acl was NULL\n");
2574 if (PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale())) != LANG_ENGLISH)
2576 skip("Non-english locale (test with hardcoded 'Everyone')\n");
2580 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_USER;
2581 ExplicitAccess.Trustee.ptstrName = (LPWSTR)wszEveryone;
2582 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2583 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2584 ok(NewAcl != NULL, "returned acl was NULL\n");
2587 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_BAD_FORM;
2588 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2589 ok(res == ERROR_INVALID_PARAMETER ||
2590 broken(res == ERROR_NOT_SUPPORTED), /* NT4 */
2591 "SetEntriesInAclW failed: %u\n", res);
2592 ok(NewAcl == NULL ||
2593 broken(NewAcl != NULL), /* NT4 */
2594 "returned acl wasn't NULL: %p\n", NewAcl);
2596 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_USER;
2597 ExplicitAccess.Trustee.MultipleTrusteeOperation = TRUSTEE_IS_IMPERSONATE;
2598 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2599 ok(res == ERROR_INVALID_PARAMETER ||
2600 broken(res == ERROR_NOT_SUPPORTED), /* NT4 */
2601 "SetEntriesInAclW failed: %u\n", res);
2602 ok(NewAcl == NULL ||
2603 broken(NewAcl != NULL), /* NT4 */
2604 "returned acl wasn't NULL: %p\n", NewAcl);
2606 ExplicitAccess.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
2607 ExplicitAccess.grfAccessMode = SET_ACCESS;
2608 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2609 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2610 ok(NewAcl != NULL, "returned acl was NULL\n");
2614 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_USER;
2615 ExplicitAccess.Trustee.ptstrName = (LPWSTR)wszCurrentUser;
2616 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2617 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2618 ok(NewAcl != NULL, "returned acl was NULL\n");
2621 ExplicitAccess.grfAccessMode = REVOKE_ACCESS;
2622 ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
2623 ExplicitAccess.Trustee.ptstrName = UsersSid;
2624 res = pSetEntriesInAclW(1, &ExplicitAccess, OldAcl, &NewAcl);
2625 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
2626 ok(NewAcl != NULL, "returned acl was NULL\n");
2630 FreeSid(EveryoneSid);
2631 HeapFree(GetProcessHeap(), 0, OldAcl);
2634 static void test_GetNamedSecurityInfoA(void)
2636 PSECURITY_DESCRIPTOR pSecDesc;
2638 SECURITY_DESCRIPTOR_CONTROL control;
2642 BOOL owner_defaulted;
2643 BOOL group_defaulted;
2646 CHAR windows_dir[MAX_PATH];
2648 if (!pGetNamedSecurityInfoA)
2650 win_skip("GetNamedSecurityInfoA is not available\n");
2654 ret = GetWindowsDirectoryA(windows_dir, MAX_PATH);
2655 ok(ret, "GetWindowsDirectory failed with error %d\n", GetLastError());
2657 SetLastError(0xdeadbeef);
2658 error = pGetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,
2659 OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION,
2660 NULL, NULL, NULL, NULL, &pSecDesc);
2661 if (error != ERROR_SUCCESS && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2663 win_skip("GetNamedSecurityInfoA is not implemented\n");
2666 ok(!error, "GetNamedSecurityInfo failed with error %d\n", error);
2668 ret = GetSecurityDescriptorControl(pSecDesc, &control, &revision);
2669 ok(ret, "GetSecurityDescriptorControl failed with error %d\n", GetLastError());
2670 ok((control & (SE_SELF_RELATIVE|SE_DACL_PRESENT)) == (SE_SELF_RELATIVE|SE_DACL_PRESENT) ||
2671 broken((control & (SE_SELF_RELATIVE|SE_DACL_PRESENT)) == SE_DACL_PRESENT), /* NT4 */
2672 "control (0x%x) doesn't have (SE_SELF_RELATIVE|SE_DACL_PRESENT) flags set\n", control);
2673 ok(revision == SECURITY_DESCRIPTOR_REVISION1, "revision was %d instead of 1\n", revision);
2675 isNT4 = (control & (SE_SELF_RELATIVE|SE_DACL_PRESENT)) == SE_DACL_PRESENT;
2677 ret = GetSecurityDescriptorOwner(pSecDesc, &owner, &owner_defaulted);
2678 ok(ret, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
2679 ok(owner != NULL, "owner should not be NULL\n");
2681 ret = GetSecurityDescriptorGroup(pSecDesc, &group, &group_defaulted);
2682 ok(ret, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
2683 ok(group != NULL, "group should not be NULL\n");
2684 LocalFree(pSecDesc);
2687 /* NULL descriptor tests */
2690 win_skip("NT4 does not support GetNamedSecutityInfo with a NULL descriptor\n");
2694 error = pGetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,DACL_SECURITY_INFORMATION,
2695 NULL, NULL, NULL, NULL, NULL);
2696 ok(error==ERROR_INVALID_PARAMETER, "GetNamedSecurityInfo failed with error %d\n", error);
2698 error = pGetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,DACL_SECURITY_INFORMATION,
2699 NULL, NULL, &dacl, NULL, NULL);
2700 ok(!error, "GetNamedSecurityInfo failed with error %d\n", error);
2701 ok(dacl != NULL, "dacl should not be NULL\n");
2703 error = pGetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,OWNER_SECURITY_INFORMATION,
2704 NULL, NULL, &dacl, NULL, NULL);
2705 ok(error==ERROR_INVALID_PARAMETER, "GetNamedSecurityInfo failed with error %d\n", error);
2708 static void test_ConvertStringSecurityDescriptor(void)
2711 PSECURITY_DESCRIPTOR pSD;
2712 static const WCHAR Blank[] = { 0 };
2716 const char *sidstring;
2723 { "D:(A;;GA;;;WD)", 0xdeadbeef, FALSE, ERROR_UNKNOWN_REVISION },
2724 /* test ACE string type */
2725 { "D:(A;;GA;;;WD)", SDDL_REVISION_1, TRUE },
2726 { "D:(D;;GA;;;WD)", SDDL_REVISION_1, TRUE },
2727 { "ERROR:(D;;GA;;;WD)", SDDL_REVISION_1, FALSE, ERROR_INVALID_PARAMETER },
2728 /* test ACE string with spaces */
2729 { " D:(D;;GA;;;WD)", SDDL_REVISION_1, TRUE },
2730 { "D: (D;;GA;;;WD)", SDDL_REVISION_1, TRUE },
2731 { "D:( D;;GA;;;WD)", SDDL_REVISION_1, TRUE },
2732 { "D:(D ;;GA;;;WD)", SDDL_REVISION_1, FALSE, RPC_S_INVALID_STRING_UUID, ERROR_INVALID_ACL }, /* Vista+ */
2733 { "D:(D; ;GA;;;WD)", SDDL_REVISION_1, TRUE },
2734 { "D:(D;; GA;;;WD)", SDDL_REVISION_1, TRUE },
2735 { "D:(D;;GA ;;;WD)", SDDL_REVISION_1, FALSE, ERROR_INVALID_ACL },
2736 { "D:(D;;GA; ;;WD)", SDDL_REVISION_1, TRUE },
2737 { "D:(D;;GA;; ;WD)", SDDL_REVISION_1, TRUE },
2738 { "D:(D;;GA;;; WD)", SDDL_REVISION_1, TRUE },
2739 { "D:(D;;GA;;;WD )", SDDL_REVISION_1, TRUE },
2740 /* test ACE string access rights */
2741 { "D:(A;;GA;;;WD)", SDDL_REVISION_1, TRUE },
2742 { "D:(A;;GRGWGX;;;WD)", SDDL_REVISION_1, TRUE },
2743 { "D:(A;;RCSDWDWO;;;WD)", SDDL_REVISION_1, TRUE },
2744 { "D:(A;;RPWPCCDCLCSWLODTCR;;;WD)", SDDL_REVISION_1, TRUE },
2745 { "D:(A;;FAFRFWFX;;;WD)", SDDL_REVISION_1, TRUE },
2746 { "D:(A;;KAKRKWKX;;;WD)", SDDL_REVISION_1, TRUE },
2747 { "D:(A;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1, TRUE },
2748 { "S:(AU;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1, TRUE },
2749 /* test ACE string access right error case */
2750 { "D:(A;;ROB;;;WD)", SDDL_REVISION_1, FALSE, ERROR_INVALID_ACL },
2751 /* test behaviour with empty strings */
2752 { "", SDDL_REVISION_1, TRUE },
2753 /* test ACE string SID */
2754 { "D:(D;;GA;;;S-1-0-0)", SDDL_REVISION_1, TRUE },
2755 { "D:(D;;GA;;;Nonexistent account)", SDDL_REVISION_1, FALSE, ERROR_INVALID_ACL, ERROR_INVALID_SID } /* W2K */
2758 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA)
2760 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
2764 for (i = 0; i < sizeof(cssd)/sizeof(cssd[0]); i++)
2768 SetLastError(0xdeadbeef);
2769 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2770 cssd[i].sidstring, cssd[i].revision, &pSD, NULL);
2771 GLE = GetLastError();
2772 ok(ret == cssd[i].ret, "(%02u) Expected %s (%d)\n", i, cssd[i].ret ? "success" : "failure", GLE);
2774 ok(GLE == cssd[i].GLE ||
2775 (cssd[i].altGLE && GLE == cssd[i].altGLE),
2776 "(%02u) Unexpected last error %d\n", i, GLE);
2781 /* test behaviour with NULL parameters */
2782 SetLastError(0xdeadbeef);
2783 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2784 NULL, 0xdeadbeef, &pSD, NULL);
2786 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2787 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2790 SetLastError(0xdeadbeef);
2791 ret = pConvertStringSecurityDescriptorToSecurityDescriptorW(
2792 NULL, 0xdeadbeef, &pSD, NULL);
2793 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2794 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2797 SetLastError(0xdeadbeef);
2798 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2799 "D:(A;;ROB;;;WD)", 0xdeadbeef, NULL, NULL);
2800 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2801 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2804 SetLastError(0xdeadbeef);
2805 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
2806 "D:(A;;ROB;;;WD)", SDDL_REVISION_1, NULL, NULL);
2807 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
2808 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
2811 /* test behaviour with empty strings */
2812 SetLastError(0xdeadbeef);
2813 ret = pConvertStringSecurityDescriptorToSecurityDescriptorW(
2814 Blank, SDDL_REVISION_1, &pSD, NULL);
2815 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
2819 static void test_ConvertSecurityDescriptorToString(void)
2821 SECURITY_DESCRIPTOR desc;
2822 SECURITY_INFORMATION sec_info = OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION|SACL_SECURITY_INFORMATION;
2831 if (!pConvertSecurityDescriptorToStringSecurityDescriptorA)
2833 win_skip("ConvertSecurityDescriptorToStringSecurityDescriptor is not available\n");
2836 if (!pCreateWellKnownSid)
2838 win_skip("CreateWellKnownSid is not available\n");
2842 /* It seems Windows XP adds an extra character to the length of the string for each ACE in an ACL. We
2843 * don't replicate this feature so we only test len >= strlen+1. */
2844 #define CHECK_RESULT_AND_FREE(exp_str) \
2845 ok(strcmp(string, (exp_str)) == 0, "String mismatch (expected \"%s\", got \"%s\")\n", (exp_str), string); \
2846 ok(len >= (strlen(exp_str) + 1), "Length mismatch (expected %d, got %d)\n", lstrlen(exp_str) + 1, len); \
2849 #define CHECK_ONE_OF_AND_FREE(exp_str1, exp_str2) \
2850 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); \
2851 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); \
2854 InitializeSecurityDescriptor(&desc, SECURITY_DESCRIPTOR_REVISION);
2855 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2856 CHECK_RESULT_AND_FREE("");
2859 pCreateWellKnownSid(WinLocalSid, NULL, sid_buf, &size);
2860 SetSecurityDescriptorOwner(&desc, sid_buf, FALSE);
2861 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2862 CHECK_RESULT_AND_FREE("O:S-1-2-0");
2864 SetSecurityDescriptorOwner(&desc, sid_buf, TRUE);
2865 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2866 CHECK_RESULT_AND_FREE("O:S-1-2-0");
2868 size = sizeof(sid_buf);
2869 pCreateWellKnownSid(WinLocalSystemSid, NULL, sid_buf, &size);
2870 SetSecurityDescriptorOwner(&desc, sid_buf, TRUE);
2871 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2872 CHECK_RESULT_AND_FREE("O:SY");
2874 pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid);
2875 SetSecurityDescriptorGroup(&desc, psid, TRUE);
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-4576");
2879 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, GROUP_SECURITY_INFORMATION, &string, &len), "Conversion failed\n");
2880 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
2882 pacl = (PACL)acl_buf;
2883 InitializeAcl(pacl, sizeof(acl_buf), ACL_REVISION);
2884 SetSecurityDescriptorDacl(&desc, TRUE, pacl, TRUE);
2885 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2886 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
2888 SetSecurityDescriptorDacl(&desc, TRUE, pacl, FALSE);
2889 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2890 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
2892 pConvertStringSidToSidA("S-1-5-6", &psid2);
2893 pAddAccessAllowedAceEx(pacl, ACL_REVISION, NO_PROPAGATE_INHERIT_ACE, 0xf0000000, psid2);
2894 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2895 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)");
2897 pAddAccessAllowedAceEx(pacl, ACL_REVISION, INHERIT_ONLY_ACE|INHERITED_ACE, 0x00000003, psid2);
2898 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2899 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)");
2901 pAddAccessDeniedAceEx(pacl, ACL_REVISION, OBJECT_INHERIT_ACE|CONTAINER_INHERIT_ACE, 0xffffffff, psid);
2902 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2903 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)");
2906 pacl = (PACL)acl_buf;
2907 InitializeAcl(pacl, sizeof(acl_buf), ACL_REVISION);
2908 SetSecurityDescriptorSacl(&desc, TRUE, pacl, FALSE);
2909 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2910 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:");
2912 /* fails in win2k */
2913 SetSecurityDescriptorDacl(&desc, TRUE, NULL, FALSE);
2914 pAddAuditAccessAceEx(pacl, ACL_REVISION, VALID_INHERIT_FLAGS, KEY_READ|KEY_WRITE, psid2, TRUE, TRUE);
2915 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len))
2917 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)", /* XP */
2918 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)" /* Vista */);
2921 /* fails in win2k */
2922 pAddAuditAccessAceEx(pacl, ACL_REVISION, NO_PROPAGATE_INHERIT_ACE, FILE_GENERIC_READ|FILE_GENERIC_WRITE, psid2, TRUE, FALSE);
2923 if (pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len))
2925 CHECK_ONE_OF_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", /* XP */
2926 "O:SYG:S-1-5-21-93476-23408-4576D:NO_ACCESS_CONTROLS:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)" /* Vista */);
2933 static void test_SetSecurityDescriptorControl (PSECURITY_DESCRIPTOR sec)
2935 SECURITY_DESCRIPTOR_CONTROL ref;
2936 SECURITY_DESCRIPTOR_CONTROL test;
2938 SECURITY_DESCRIPTOR_CONTROL const mutable
2939 = SE_DACL_AUTO_INHERIT_REQ | SE_SACL_AUTO_INHERIT_REQ
2940 | SE_DACL_AUTO_INHERITED | SE_SACL_AUTO_INHERITED
2941 | SE_DACL_PROTECTED | SE_SACL_PROTECTED
2942 | 0x00000040 | 0x00000080 /* not defined in winnt.h */
2944 SECURITY_DESCRIPTOR_CONTROL const immutable
2945 = SE_OWNER_DEFAULTED | SE_GROUP_DEFAULTED
2946 | SE_DACL_PRESENT | SE_DACL_DEFAULTED
2947 | SE_SACL_PRESENT | SE_SACL_DEFAULTED
2948 | SE_RM_CONTROL_VALID | SE_SELF_RELATIVE
2953 LPCSTR fmt = "Expected error %s, got %u\n";
2955 GetSecurityDescriptorControl (sec, &ref, &dwRevision);
2957 /* The mutable bits are mutable regardless of the truth of
2958 SE_DACL_PRESENT and/or SE_SACL_PRESENT */
2960 /* Check call barfs if any bit-of-interest is immutable */
2961 for (bit = 0; bit < 16; ++bit)
2963 SECURITY_DESCRIPTOR_CONTROL const bitOfInterest = 1 << bit;
2964 SECURITY_DESCRIPTOR_CONTROL setOrClear = ref & bitOfInterest;
2966 SECURITY_DESCRIPTOR_CONTROL ctrl;
2968 DWORD dwExpect = (bitOfInterest & immutable)
2969 ? ERROR_INVALID_PARAMETER : 0xbebecaca;
2970 LPCSTR strExpect = (bitOfInterest & immutable)
2971 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
2973 ctrl = (bitOfInterest & mutable) ? ref + bitOfInterest : ref;
2974 setOrClear ^= bitOfInterest;
2975 SetLastError (0xbebecaca);
2976 pSetSecurityDescriptorControl (sec, bitOfInterest, setOrClear);
2977 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
2978 GetSecurityDescriptorControl(sec, &test, &dwRevision);
2979 expect_eq(test, ctrl, int, "%x");
2982 setOrClear ^= bitOfInterest;
2983 SetLastError (0xbebecaca);
2984 pSetSecurityDescriptorControl (sec, bitOfInterest, setOrClear);
2985 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
2986 GetSecurityDescriptorControl (sec, &test, &dwRevision);
2987 expect_eq(test, ref, int, "%x");
2990 /* Check call barfs if any bit-to-set is immutable
2991 even when not a bit-of-interest */
2992 for (bit = 0; bit < 16; ++bit)
2994 SECURITY_DESCRIPTOR_CONTROL const bitsOfInterest = mutable;
2995 SECURITY_DESCRIPTOR_CONTROL setOrClear = ref & bitsOfInterest;
2997 SECURITY_DESCRIPTOR_CONTROL ctrl;
2999 DWORD dwExpect = ((1 << bit) & immutable)
3000 ? ERROR_INVALID_PARAMETER : 0xbebecaca;
3001 LPCSTR strExpect = ((1 << bit) & immutable)
3002 ? "ERROR_INVALID_PARAMETER" : "0xbebecaca";
3004 ctrl = ((1 << bit) & immutable) ? test : ref | mutable;
3005 setOrClear ^= bitsOfInterest;
3006 SetLastError (0xbebecaca);
3007 pSetSecurityDescriptorControl (sec, bitsOfInterest, setOrClear | (1 << bit));
3008 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
3009 GetSecurityDescriptorControl(sec, &test, &dwRevision);
3010 expect_eq(test, ctrl, int, "%x");
3012 ctrl = ((1 << bit) & immutable) ? test : ref | (1 << bit);
3013 setOrClear ^= bitsOfInterest;
3014 SetLastError (0xbebecaca);
3015 pSetSecurityDescriptorControl (sec, bitsOfInterest, setOrClear | (1 << bit));
3016 ok (GetLastError () == dwExpect, fmt, strExpect, GetLastError ());
3017 GetSecurityDescriptorControl(sec, &test, &dwRevision);
3018 expect_eq(test, ctrl, int, "%x");
3022 static void test_PrivateObjectSecurity(void)
3024 SECURITY_INFORMATION sec_info = OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION|SACL_SECURITY_INFORMATION;
3025 SECURITY_DESCRIPTOR_CONTROL ctrl;
3026 PSECURITY_DESCRIPTOR sec;
3032 PSECURITY_DESCRIPTOR buf;
3034 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA)
3036 win_skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
3040 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3042 "G:S-1-5-21-93476-23408-4576"
3043 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)"
3044 "(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3045 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
3046 SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
3048 test_SetSecurityDescriptorControl(sec);
3052 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3054 "G:S-1-5-21-93476-23408-4576",
3055 SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
3057 test_SetSecurityDescriptorControl(sec);
3061 ok(pConvertStringSecurityDescriptorToSecurityDescriptorA(
3063 "G:S-1-5-21-93476-23408-4576"
3064 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3065 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
3066 buf = HeapAlloc(GetProcessHeap(), 0, dwDescSize);
3067 pSetSecurityDescriptorControl(sec, SE_DACL_PROTECTED, SE_DACL_PROTECTED);
3068 GetSecurityDescriptorControl(sec, &ctrl, &dwRevision);
3069 expect_eq(ctrl, 0x9014, int, "%x");
3071 ok(GetPrivateObjectSecurity(sec, GROUP_SECURITY_INFORMATION, buf, dwDescSize, &retSize),
3072 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3073 ok(retSize <= dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
3074 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3075 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
3076 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
3077 expect_eq(ctrl, 0x8000, int, "%x");
3079 ok(GetPrivateObjectSecurity(sec, GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION, buf, dwDescSize, &retSize),
3080 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3081 ok(retSize <= dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
3082 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed err=%u\n", GetLastError());
3083 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)",
3084 "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 */
3085 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
3086 expect_eq(ctrl & (~ SE_DACL_PROTECTED), 0x8004, int, "%x");
3088 ok(GetPrivateObjectSecurity(sec, sec_info, buf, dwDescSize, &retSize),
3089 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
3090 ok(retSize == dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
3091 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
3092 CHECK_ONE_OF_AND_FREE("O:SY"
3093 "G:S-1-5-21-93476-23408-4576"
3094 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3095 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)",
3097 "G:S-1-5-21-93476-23408-4576"
3098 "D:P(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
3099 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)"); /* Win7 */
3100 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
3101 expect_eq(ctrl & (~ SE_DACL_PROTECTED), 0x8014, int, "%x");
3103 SetLastError(0xdeadbeef);
3104 ok(GetPrivateObjectSecurity(sec, sec_info, buf, 5, &retSize) == FALSE, "GetPrivateObjectSecurity should have failed\n");
3105 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected error ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
3108 HeapFree(GetProcessHeap(), 0, buf);
3110 #undef CHECK_RESULT_AND_FREE
3111 #undef CHECK_ONE_OF_AND_FREE
3113 static void test_acls(void)
3116 PACL pAcl = (PACL)buffer;
3119 SetLastError(0xdeadbeef);
3120 ret = InitializeAcl(pAcl, sizeof(ACL) - 1, ACL_REVISION);
3121 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
3123 win_skip("InitializeAcl is not implemented\n");
3127 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InitializeAcl with too small a buffer should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", GetLastError());
3129 SetLastError(0xdeadbeef);
3130 ret = InitializeAcl(pAcl, 0xffffffff, ACL_REVISION);
3131 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "InitializeAcl with too large a buffer should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3133 SetLastError(0xdeadbeef);
3134 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION1);
3135 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "InitializeAcl(ACL_REVISION1) should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
3137 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION2);
3138 ok(ret, "InitializeAcl(ACL_REVISION2) failed with error %d\n", GetLastError());
3140 ret = IsValidAcl(pAcl);
3141 ok(ret, "IsValidAcl failed with error %d\n", GetLastError());
3143 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION3);
3144 ok(ret, "InitializeAcl(ACL_REVISION3) failed with error %d\n", GetLastError());
3146 ret = IsValidAcl(pAcl);
3147 ok(ret, "IsValidAcl failed with error %d\n", GetLastError());
3149 SetLastError(0xdeadbeef);
3150 ret = InitializeAcl(pAcl, sizeof(buffer), ACL_REVISION4);
3151 if (GetLastError() != ERROR_INVALID_PARAMETER)
3153 ok(ret, "InitializeAcl(ACL_REVISION4) failed with error %d\n", GetLastError());
3155 ret = IsValidAcl(pAcl);
3156 ok(ret, "IsValidAcl failed with error %d\n", GetLastError());
3159 win_skip("ACL_REVISION4 is not implemented on NT4\n");
3161 SetLastError(0xdeadbeef);
3162 ret = InitializeAcl(pAcl, sizeof(buffer), -1);
3163 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "InitializeAcl(-1) failed with error %d\n", GetLastError());
3166 static void test_GetSecurityInfo(void)
3169 PSECURITY_DESCRIPTOR sd;
3174 if (!pGetSecurityInfo)
3176 win_skip("GetSecurityInfo is not available\n");
3180 /* Create something. Files have lots of associated security info. */
3181 obj = CreateFile(myARGV[0], GENERIC_READ, FILE_SHARE_READ, NULL,
3182 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
3183 if (obj == INVALID_HANDLE_VALUE)
3185 skip("Couldn't create an object for GetSecurityInfo test\n");
3189 ret = pGetSecurityInfo(obj, SE_FILE_OBJECT,
3190 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
3191 &owner, &group, &dacl, NULL, &sd);
3192 if (ret == ERROR_CALL_NOT_IMPLEMENTED)
3194 win_skip("GetSecurityInfo is not implemented\n");
3198 ok(ret == ERROR_SUCCESS, "GetSecurityInfo returned %d\n", ret);
3199 ok(sd != NULL, "GetSecurityInfo\n");
3200 ok(owner != NULL, "GetSecurityInfo\n");
3201 ok(group != NULL, "GetSecurityInfo\n");
3203 ok(IsValidAcl(dacl), "GetSecurityInfo\n");
3205 win_skip("No ACL information returned\n");
3209 if (!pCreateWellKnownSid)
3211 win_skip("NULL parameter test would crash on NT4\n");
3216 /* If we don't ask for the security descriptor, Windows will still give us
3217 the other stuff, leaving us no way to free it. */
3218 ret = pGetSecurityInfo(obj, SE_FILE_OBJECT,
3219 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
3220 &owner, &group, &dacl, NULL, NULL);
3221 ok(ret == ERROR_SUCCESS, "GetSecurityInfo returned %d\n", ret);
3222 ok(owner != NULL, "GetSecurityInfo\n");
3223 ok(group != NULL, "GetSecurityInfo\n");
3225 ok(IsValidAcl(dacl), "GetSecurityInfo\n");
3227 win_skip("No ACL information returned\n");
3232 static void test_GetSidSubAuthority(void)
3236 if (!pGetSidSubAuthority || !pConvertStringSidToSidA || !pIsValidSid || !pGetSidSubAuthorityCount)
3238 win_skip("Some functions not available\n");
3241 /* Note: on windows passing in an invalid index like -1, lets GetSidSubAuthority return 0x05000000 but
3242 still GetLastError returns ERROR_SUCCESS then. We don't test these unlikely cornercases here for now */
3243 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576",&psid),"ConvertStringSidToSidA failed\n");
3244 ok(pIsValidSid(psid),"Sid is not valid\n");
3245 SetLastError(0xbebecaca);
3246 ok(*pGetSidSubAuthorityCount(psid) == 4,"GetSidSubAuthorityCount gave %d expected 4\n",*pGetSidSubAuthorityCount(psid));
3247 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3248 SetLastError(0xbebecaca);
3249 ok(*pGetSidSubAuthority(psid,0) == 21,"GetSidSubAuthority gave %d expected 21\n",*pGetSidSubAuthority(psid,0));
3250 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3251 SetLastError(0xbebecaca);
3252 ok(*pGetSidSubAuthority(psid,1) == 93476,"GetSidSubAuthority gave %d expected 93476\n",*pGetSidSubAuthority(psid,1));
3253 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3254 SetLastError(0xbebecaca);
3255 ok(pGetSidSubAuthority(psid,4) != NULL,"Expected out of bounds GetSidSubAuthority to return a non-NULL pointer\n");
3256 ok(GetLastError() == 0,"GetLastError returned %d instead of 0\n",GetLastError());
3260 static void test_CheckTokenMembership(void)
3262 PTOKEN_GROUPS token_groups;
3264 HANDLE process_token, token;
3269 if (!pCheckTokenMembership)
3271 win_skip("CheckTokenMembership is not available\n");
3274 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &process_token);
3275 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
3277 ret = DuplicateToken(process_token, SecurityImpersonation, &token);
3278 ok(ret, "DuplicateToken failed with error %d\n", GetLastError());
3281 ret = GetTokenInformation(token, TokenGroups, NULL, 0, &size);
3282 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
3283 "GetTokenInformation(TokenGroups) %s with error %d\n",
3284 ret ? "succeeded" : "failed", GetLastError());
3285 token_groups = HeapAlloc(GetProcessHeap(), 0, size);
3286 ret = GetTokenInformation(token, TokenGroups, token_groups, size, &size);
3287 ok(ret, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
3289 for (i = 0; i < token_groups->GroupCount; i++)
3291 if (token_groups->Groups[i].Attributes & SE_GROUP_ENABLED)
3295 if (i == token_groups->GroupCount)
3297 HeapFree(GetProcessHeap(), 0, token_groups);
3299 skip("user not a member of any group\n");
3303 ret = pCheckTokenMembership(token, token_groups->Groups[i].Sid, &is_member);
3304 ok(ret, "CheckTokenMembership failed with error %d\n", GetLastError());
3305 ok(is_member, "CheckTokenMembership should have detected sid as member\n");
3307 ret = pCheckTokenMembership(NULL, token_groups->Groups[i].Sid, &is_member);
3308 ok(ret, "CheckTokenMembership failed with error %d\n", GetLastError());
3309 ok(is_member, "CheckTokenMembership should have detected sid as member\n");
3311 ret = pCheckTokenMembership(process_token, token_groups->Groups[i].Sid, &is_member);
3313 ok(!ret && GetLastError() == ERROR_NO_IMPERSONATION_TOKEN,
3314 "CheckTokenMembership with process token %s with error %d\n",
3315 ret ? "succeeded" : "failed", GetLastError());
3316 ok(!is_member, "CheckTokenMembership should have cleared is_member\n");
3319 HeapFree(GetProcessHeap(), 0, token_groups);
3321 CloseHandle(process_token);
3324 static void test_EqualSid(void)
3328 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
3329 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
3331 SetLastError(0xdeadbeef);
3332 ret = AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
3333 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &sid1);
3334 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
3336 win_skip("AllocateAndInitializeSid is not implemented\n");
3339 ok(ret, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3340 ok(GetLastError() == 0xdeadbeef,
3341 "AllocateAndInitializeSid shouldn't have set last error to %d\n",
3344 ret = AllocateAndInitializeSid(&SIDAuthWorld, 1, SECURITY_WORLD_RID,
3345 0, 0, 0, 0, 0, 0, 0, &sid2);
3346 ok(ret, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3348 SetLastError(0xdeadbeef);
3349 ret = EqualSid(sid1, sid2);
3350 ok(!ret, "World and domain admins sids shouldn't have been equal\n");
3351 ok(GetLastError() == ERROR_SUCCESS ||
3352 broken(GetLastError() == 0xdeadbeef), /* NT4 */
3353 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
3356 SetLastError(0xdeadbeef);
3357 sid2 = FreeSid(sid2);
3358 ok(!sid2, "FreeSid should have returned NULL instead of %p\n", sid2);
3359 ok(GetLastError() == 0xdeadbeef,
3360 "FreeSid shouldn't have set last error to %d\n",
3363 ret = AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
3364 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &sid2);
3365 ok(ret, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
3367 SetLastError(0xdeadbeef);
3368 ret = EqualSid(sid1, sid2);
3369 ok(ret, "Same sids should have been equal\n");
3370 ok(GetLastError() == ERROR_SUCCESS ||
3371 broken(GetLastError() == 0xdeadbeef), /* NT4 */
3372 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
3375 ((SID *)sid2)->Revision = 2;
3376 SetLastError(0xdeadbeef);
3377 ret = EqualSid(sid1, sid2);
3378 ok(!ret, "EqualSid with invalid sid should have returned FALSE\n");
3379 ok(GetLastError() == ERROR_SUCCESS ||
3380 broken(GetLastError() == 0xdeadbeef), /* NT4 */
3381 "EqualSid should have set last error to ERROR_SUCCESS instead of %d\n",
3383 ((SID *)sid2)->Revision = SID_REVISION;
3389 START_TEST(security)
3396 test_process_security_child();
3402 test_CreateWellKnownSid();
3403 test_FileSecurity();
3406 test_LookupAccountSid();
3407 test_LookupAccountName();
3408 test_security_descriptor();
3409 test_process_security();
3410 test_impersonation_level();
3411 test_SetEntriesInAcl();
3412 test_GetNamedSecurityInfoA();
3413 test_ConvertStringSecurityDescriptor();
3414 test_ConvertSecurityDescriptorToString();
3415 test_PrivateObjectSecurity();
3417 test_GetSecurityInfo();
3418 test_GetSidSubAuthority();
3419 test_CheckTokenMembership();