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
36 #include "wine/test.h"
38 #define expect_eq(expr, value, type, format) { type ret = expr; ok((value) == ret, #expr " expected " format " got " format "\n", (value), (ret)); }
40 typedef VOID (WINAPI *fnBuildTrusteeWithSidA)( PTRUSTEEA pTrustee, PSID pSid );
41 typedef VOID (WINAPI *fnBuildTrusteeWithNameA)( PTRUSTEEA pTrustee, LPSTR pName );
42 typedef VOID (WINAPI *fnBuildTrusteeWithObjectsAndNameA)( PTRUSTEEA pTrustee,
43 POBJECTS_AND_NAME_A pObjName,
44 SE_OBJECT_TYPE ObjectType,
46 LPSTR InheritedObjectTypeName,
48 typedef VOID (WINAPI *fnBuildTrusteeWithObjectsAndSidA)( PTRUSTEEA pTrustee,
49 POBJECTS_AND_SID pObjSid,
51 GUID* pInheritedObjectGuid,
53 typedef LPSTR (WINAPI *fnGetTrusteeNameA)( PTRUSTEEA pTrustee );
54 typedef BOOL (WINAPI *fnMakeSelfRelativeSD)( PSECURITY_DESCRIPTOR, PSECURITY_DESCRIPTOR, LPDWORD );
55 typedef BOOL (WINAPI *fnConvertSidToStringSidA)( PSID pSid, LPSTR *str );
56 typedef BOOL (WINAPI *fnConvertStringSidToSidA)( LPCSTR str, PSID pSid );
57 static BOOL (WINAPI *pConvertStringSecurityDescriptorToSecurityDescriptorA)(LPCSTR, DWORD,
58 PSECURITY_DESCRIPTOR*, PULONG );
59 static BOOL (WINAPI *pConvertSecurityDescriptorToStringSecurityDescriptorA)(PSECURITY_DESCRIPTOR, DWORD,
60 SECURITY_INFORMATION, LPSTR *, PULONG );
61 typedef BOOL (WINAPI *fnGetFileSecurityA)(LPCSTR, SECURITY_INFORMATION,
62 PSECURITY_DESCRIPTOR, DWORD, LPDWORD);
63 static DWORD (WINAPI *pGetNamedSecurityInfoA)(LPSTR, SE_OBJECT_TYPE, SECURITY_INFORMATION,
64 PSID*, PSID*, PACL*, PACL*,
65 PSECURITY_DESCRIPTOR*);
66 typedef DWORD (WINAPI *fnRtlAdjustPrivilege)(ULONG,BOOLEAN,BOOLEAN,PBOOLEAN);
67 typedef BOOL (WINAPI *fnCreateWellKnownSid)(WELL_KNOWN_SID_TYPE,PSID,PSID,DWORD*);
68 typedef BOOL (WINAPI *fnDuplicateTokenEx)(HANDLE,DWORD,LPSECURITY_ATTRIBUTES,
69 SECURITY_IMPERSONATION_LEVEL,TOKEN_TYPE,PHANDLE);
71 typedef NTSTATUS (WINAPI *fnLsaQueryInformationPolicy)(LSA_HANDLE,POLICY_INFORMATION_CLASS,PVOID*);
72 typedef NTSTATUS (WINAPI *fnLsaClose)(LSA_HANDLE);
73 typedef NTSTATUS (WINAPI *fnLsaFreeMemory)(PVOID);
74 typedef NTSTATUS (WINAPI *fnLsaOpenPolicy)(PLSA_UNICODE_STRING,PLSA_OBJECT_ATTRIBUTES,ACCESS_MASK,PLSA_HANDLE);
75 static NTSTATUS (WINAPI *pNtQueryObject)(HANDLE,OBJECT_INFORMATION_CLASS,PVOID,ULONG,PULONG);
76 static DWORD (WINAPI *pSetEntriesInAclW)(ULONG, PEXPLICIT_ACCESSW, PACL, PACL*);
82 fnBuildTrusteeWithSidA pBuildTrusteeWithSidA;
83 fnBuildTrusteeWithNameA pBuildTrusteeWithNameA;
84 fnBuildTrusteeWithObjectsAndNameA pBuildTrusteeWithObjectsAndNameA;
85 fnBuildTrusteeWithObjectsAndSidA pBuildTrusteeWithObjectsAndSidA;
86 fnGetTrusteeNameA pGetTrusteeNameA;
87 fnMakeSelfRelativeSD pMakeSelfRelativeSD;
88 fnConvertSidToStringSidA pConvertSidToStringSidA;
89 fnConvertStringSidToSidA pConvertStringSidToSidA;
90 fnGetFileSecurityA pGetFileSecurityA;
91 fnRtlAdjustPrivilege pRtlAdjustPrivilege;
92 fnCreateWellKnownSid pCreateWellKnownSid;
93 fnDuplicateTokenEx pDuplicateTokenEx;
94 fnLsaQueryInformationPolicy pLsaQueryInformationPolicy;
96 fnLsaFreeMemory pLsaFreeMemory;
97 fnLsaOpenPolicy pLsaOpenPolicy;
101 SID_IDENTIFIER_AUTHORITY auth;
105 static void init(void)
109 hntdll = GetModuleHandleA("ntdll.dll");
110 pNtQueryObject = (void *)GetProcAddress( hntdll, "NtQueryObject" );
112 hmod = GetModuleHandle("advapi32.dll");
113 pConvertStringSecurityDescriptorToSecurityDescriptorA =
114 (void *)GetProcAddress(hmod, "ConvertStringSecurityDescriptorToSecurityDescriptorA" );
115 pConvertSecurityDescriptorToStringSecurityDescriptorA =
116 (void *)GetProcAddress(hmod, "ConvertSecurityDescriptorToStringSecurityDescriptorA" );
117 pMakeSelfRelativeSD = (void *)GetProcAddress(hmod, "MakeSelfRelativeSD");
118 pGetNamedSecurityInfoA = (void *)GetProcAddress(hmod, "GetNamedSecurityInfoA");
119 pSetEntriesInAclW = (void *)GetProcAddress(hmod, "SetEntriesInAclW");
121 myARGC = winetest_get_mainargs( &myARGV );
124 static void test_str_sid(const char *str_sid)
129 if (pConvertStringSidToSidA(str_sid, &psid))
131 if (pConvertSidToStringSidA(psid, &temp))
133 trace(" %s: %s\n", str_sid, temp);
140 if (GetLastError() != ERROR_INVALID_SID)
141 trace(" %s: couldn't be converted, returned %d\n", str_sid, GetLastError());
143 trace(" %s: couldn't be converted\n", str_sid);
147 static void test_sid(void)
149 struct sidRef refs[] = {
150 { { {0x00,0x00,0x33,0x44,0x55,0x66} }, "S-1-860116326-1" },
151 { { {0x00,0x00,0x01,0x02,0x03,0x04} }, "S-1-16909060-1" },
152 { { {0x00,0x00,0x00,0x01,0x02,0x03} }, "S-1-66051-1" },
153 { { {0x00,0x00,0x00,0x00,0x01,0x02} }, "S-1-258-1" },
154 { { {0x00,0x00,0x00,0x00,0x00,0x02} }, "S-1-2-1" },
155 { { {0x00,0x00,0x00,0x00,0x00,0x0c} }, "S-1-12-1" },
157 const char noSubAuthStr[] = "S-1-5";
164 pConvertSidToStringSidA = (fnConvertSidToStringSidA)
165 GetProcAddress( hmod, "ConvertSidToStringSidA" );
166 if( !pConvertSidToStringSidA )
168 pConvertStringSidToSidA = (fnConvertStringSidToSidA)
169 GetProcAddress( hmod, "ConvertStringSidToSidA" );
170 if( !pConvertStringSidToSidA )
173 r = pConvertStringSidToSidA( NULL, NULL );
174 ok( !r, "expected failure with NULL parameters\n" );
175 if( GetLastError() == ERROR_CALL_NOT_IMPLEMENTED )
177 ok( GetLastError() == ERROR_INVALID_PARAMETER,
178 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
181 r = pConvertStringSidToSidA( refs[0].refStr, NULL );
182 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
183 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
186 r = pConvertStringSidToSidA( NULL, &str );
187 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
188 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
191 r = pConvertStringSidToSidA( noSubAuthStr, &psid );
193 "expected failure with no sub authorities\n" );
194 ok( GetLastError() == ERROR_INVALID_SID,
195 "expected GetLastError() is ERROR_INVALID_SID, got %d\n",
198 ok(pConvertStringSidToSidA("S-1-5-21-93476-23408-4576", &psid), "ConvertStringSidToSidA failed\n");
200 ok(pisid->SubAuthorityCount == 4, "Invalid sub authority count - expected 4, got %d\n", pisid->SubAuthorityCount);
201 ok(pisid->SubAuthority[0] == 21, "Invalid subauthority 0 - expceted 21, got %d\n", pisid->SubAuthority[0]);
202 ok(pisid->SubAuthority[3] == 4576, "Invalid subauthority 0 - expceted 4576, got %d\n", pisid->SubAuthority[3]);
205 for( i = 0; i < sizeof(refs) / sizeof(refs[0]); i++ )
209 r = AllocateAndInitializeSid( &refs[i].auth, 1,1,0,0,0,0,0,0,0,
211 ok( r, "failed to allocate sid\n" );
212 r = pConvertSidToStringSidA( psid, &str );
213 ok( r, "failed to convert sid\n" );
216 ok( !strcmp( str, refs[i].refStr ),
217 "incorrect sid, expected %s, got %s\n", refs[i].refStr, str );
223 r = pConvertStringSidToSidA( refs[i].refStr, &psid );
224 ok( r, "failed to parse sid string\n" );
227 !memcmp( pisid->IdentifierAuthority.Value, refs[i].auth.Value,
228 sizeof(refs[i].auth) ),
229 "string sid %s didn't parse to expected value\n"
230 "(got 0x%04x%08x, expected 0x%04x%08x)\n",
232 MAKEWORD( pisid->IdentifierAuthority.Value[1],
233 pisid->IdentifierAuthority.Value[0] ),
234 MAKELONG( MAKEWORD( pisid->IdentifierAuthority.Value[5],
235 pisid->IdentifierAuthority.Value[4] ),
236 MAKEWORD( pisid->IdentifierAuthority.Value[3],
237 pisid->IdentifierAuthority.Value[2] ) ),
238 MAKEWORD( refs[i].auth.Value[1], refs[i].auth.Value[0] ),
239 MAKELONG( MAKEWORD( refs[i].auth.Value[5], refs[i].auth.Value[4] ),
240 MAKEWORD( refs[i].auth.Value[3], refs[i].auth.Value[2] ) ) );
245 trace("String SIDs:\n");
286 static void test_trustee(void)
288 GUID ObjectType = {0x12345678, 0x1234, 0x5678, {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}};
289 GUID InheritedObjectType = {0x23456789, 0x2345, 0x6786, {0x2, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}};
291 OBJECTS_AND_NAME_ oan;
295 char szObjectTypeName[] = "ObjectTypeName";
296 char szInheritedObjectTypeName[] = "InheritedObjectTypeName";
297 char szTrusteeName[] = "szTrusteeName";
298 SID_IDENTIFIER_AUTHORITY auth = { {0x11,0x22,0,0,0, 0} };
300 memset( &ZeroGuid, 0x00, sizeof (ZeroGuid) );
302 pBuildTrusteeWithSidA = (fnBuildTrusteeWithSidA)
303 GetProcAddress( hmod, "BuildTrusteeWithSidA" );
304 pBuildTrusteeWithNameA = (fnBuildTrusteeWithNameA)
305 GetProcAddress( hmod, "BuildTrusteeWithNameA" );
306 pBuildTrusteeWithObjectsAndNameA = (fnBuildTrusteeWithObjectsAndNameA)
307 GetProcAddress (hmod, "BuildTrusteeWithObjectsAndNameA" );
308 pBuildTrusteeWithObjectsAndSidA = (fnBuildTrusteeWithObjectsAndSidA)
309 GetProcAddress (hmod, "BuildTrusteeWithObjectsAndSidA" );
310 pGetTrusteeNameA = (fnGetTrusteeNameA)
311 GetProcAddress (hmod, "GetTrusteeNameA" );
312 if( !pBuildTrusteeWithSidA || !pBuildTrusteeWithNameA ||
313 !pBuildTrusteeWithObjectsAndNameA || !pBuildTrusteeWithObjectsAndSidA ||
317 if ( ! AllocateAndInitializeSid( &auth, 1, 42, 0,0,0,0,0,0,0,&psid ) )
319 trace( "failed to init SID\n" );
323 /* test BuildTrusteeWithSidA */
324 memset( &trustee, 0xff, sizeof trustee );
325 pBuildTrusteeWithSidA( &trustee, psid );
327 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
328 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
329 "MultipleTrusteeOperation wrong\n");
330 ok( trustee.TrusteeForm == TRUSTEE_IS_SID, "TrusteeForm wrong\n");
331 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
332 ok( trustee.ptstrName == (LPSTR) psid, "ptstrName wrong\n" );
334 /* test BuildTrusteeWithObjectsAndSidA (test 1) */
335 memset( &trustee, 0xff, sizeof trustee );
336 memset( &oas, 0xff, sizeof(oas) );
337 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, &ObjectType,
338 &InheritedObjectType, psid);
340 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
341 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
342 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
343 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
344 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
346 ok(oas.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
347 ok(!memcmp(&oas.ObjectTypeGuid, &ObjectType, sizeof(GUID)), "ObjectTypeGuid wrong\n");
348 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
349 ok(oas.pSid == psid, "pSid wrong\n");
351 /* test GetTrusteeNameA */
352 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oas, "GetTrusteeName returned wrong value\n");
354 /* test BuildTrusteeWithObjectsAndSidA (test 2) */
355 memset( &trustee, 0xff, sizeof trustee );
356 memset( &oas, 0xff, sizeof(oas) );
357 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, NULL,
358 &InheritedObjectType, psid);
360 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
361 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
362 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
363 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
364 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
366 ok(oas.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
367 ok(!memcmp(&oas.ObjectTypeGuid, &ZeroGuid, sizeof(GUID)), "ObjectTypeGuid wrong\n");
368 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
369 ok(oas.pSid == psid, "pSid wrong\n");
373 /* test BuildTrusteeWithNameA */
374 memset( &trustee, 0xff, sizeof trustee );
375 pBuildTrusteeWithNameA( &trustee, szTrusteeName );
377 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
378 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
379 "MultipleTrusteeOperation wrong\n");
380 ok( trustee.TrusteeForm == TRUSTEE_IS_NAME, "TrusteeForm wrong\n");
381 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
382 ok( trustee.ptstrName == szTrusteeName, "ptstrName wrong\n" );
384 /* test BuildTrusteeWithObjectsAndNameA (test 1) */
385 memset( &trustee, 0xff, sizeof trustee );
386 memset( &oan, 0xff, sizeof(oan) );
387 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
388 szInheritedObjectTypeName, szTrusteeName);
390 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
391 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
392 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
393 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
394 ok(trustee.ptstrName == (LPTSTR)&oan, "ptstrName wrong\n");
396 ok(oan.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
397 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
398 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
399 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
401 /* test GetTrusteeNameA */
402 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oan, "GetTrusteeName returned wrong value\n");
404 /* test BuildTrusteeWithObjectsAndNameA (test 2) */
405 memset( &trustee, 0xff, sizeof trustee );
406 memset( &oan, 0xff, sizeof(oan) );
407 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, NULL,
408 szInheritedObjectTypeName, szTrusteeName);
410 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
411 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
412 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
413 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
414 ok(trustee.ptstrName == (LPSTR)&oan, "ptstrName wrong\n");
416 ok(oan.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
417 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
418 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
419 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
421 /* test BuildTrusteeWithObjectsAndNameA (test 3) */
422 memset( &trustee, 0xff, sizeof trustee );
423 memset( &oan, 0xff, sizeof(oan) );
424 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
425 NULL, szTrusteeName);
427 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
428 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
429 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
430 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
431 ok(trustee.ptstrName == (LPTSTR)&oan, "ptstrName wrong\n");
433 ok(oan.ObjectsPresent == ACE_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
434 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
435 ok(oan.InheritedObjectTypeName == NULL, "InheritedObjectTypeName wrong\n");
436 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
439 /* If the first isn't defined, assume none is */
440 #ifndef SE_MIN_WELL_KNOWN_PRIVILEGE
441 #define SE_MIN_WELL_KNOWN_PRIVILEGE 2L
442 #define SE_CREATE_TOKEN_PRIVILEGE 2L
443 #define SE_ASSIGNPRIMARYTOKEN_PRIVILEGE 3L
444 #define SE_LOCK_MEMORY_PRIVILEGE 4L
445 #define SE_INCREASE_QUOTA_PRIVILEGE 5L
446 #define SE_MACHINE_ACCOUNT_PRIVILEGE 6L
447 #define SE_TCB_PRIVILEGE 7L
448 #define SE_SECURITY_PRIVILEGE 8L
449 #define SE_TAKE_OWNERSHIP_PRIVILEGE 9L
450 #define SE_LOAD_DRIVER_PRIVILEGE 10L
451 #define SE_SYSTEM_PROFILE_PRIVILEGE 11L
452 #define SE_SYSTEMTIME_PRIVILEGE 12L
453 #define SE_PROF_SINGLE_PROCESS_PRIVILEGE 13L
454 #define SE_INC_BASE_PRIORITY_PRIVILEGE 14L
455 #define SE_CREATE_PAGEFILE_PRIVILEGE 15L
456 #define SE_CREATE_PERMANENT_PRIVILEGE 16L
457 #define SE_BACKUP_PRIVILEGE 17L
458 #define SE_RESTORE_PRIVILEGE 18L
459 #define SE_SHUTDOWN_PRIVILEGE 19L
460 #define SE_DEBUG_PRIVILEGE 20L
461 #define SE_AUDIT_PRIVILEGE 21L
462 #define SE_SYSTEM_ENVIRONMENT_PRIVILEGE 22L
463 #define SE_CHANGE_NOTIFY_PRIVILLEGE 23L
464 #define SE_REMOTE_SHUTDOWN_PRIVILEGE 24L
465 #define SE_UNDOCK_PRIVILEGE 25L
466 #define SE_SYNC_AGENT_PRIVILEGE 26L
467 #define SE_ENABLE_DELEGATION_PRIVILEGE 27L
468 #define SE_MANAGE_VOLUME_PRIVILEGE 28L
469 #define SE_IMPERSONATE_PRIVILEGE 29L
470 #define SE_CREATE_GLOBAL_PRIVILEGE 30L
471 #define SE_MAX_WELL_KNOWN_PRIVILEGE SE_CREATE_GLOBAL_PRIVILEGE
472 #endif /* ndef SE_MIN_WELL_KNOWN_PRIVILEGE */
474 static void test_allocateLuid(void)
476 BOOL (WINAPI *pAllocateLocallyUniqueId)(PLUID);
480 pAllocateLocallyUniqueId = (void*)GetProcAddress(hmod, "AllocateLocallyUniqueId");
481 if (!pAllocateLocallyUniqueId) return;
483 ret = pAllocateLocallyUniqueId(&luid1);
484 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
488 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
489 ret = pAllocateLocallyUniqueId(&luid2);
491 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
492 ok(luid1.LowPart > SE_MAX_WELL_KNOWN_PRIVILEGE || luid1.HighPart != 0,
493 "AllocateLocallyUniqueId returned a well-known LUID\n");
494 ok(luid1.LowPart != luid2.LowPart || luid1.HighPart != luid2.HighPart,
495 "AllocateLocallyUniqueId returned non-unique LUIDs\n");
496 ret = pAllocateLocallyUniqueId(NULL);
497 ok( !ret && GetLastError() == ERROR_NOACCESS,
498 "AllocateLocallyUniqueId(NULL) didn't return ERROR_NOACCESS: %d\n",
502 static void test_lookupPrivilegeName(void)
504 BOOL (WINAPI *pLookupPrivilegeNameA)(LPCSTR, PLUID, LPSTR, LPDWORD);
505 char buf[MAX_PATH]; /* arbitrary, seems long enough */
506 DWORD cchName = sizeof(buf);
507 LUID luid = { 0, 0 };
511 /* check whether it's available first */
512 pLookupPrivilegeNameA = (void*)GetProcAddress(hmod, "LookupPrivilegeNameA");
513 if (!pLookupPrivilegeNameA) return;
514 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
515 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
516 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
519 /* check with a short buffer */
521 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
522 ret = pLookupPrivilegeNameA(NULL, &luid, NULL, &cchName);
523 ok( !ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
524 "LookupPrivilegeNameA didn't fail with ERROR_INSUFFICIENT_BUFFER: %d\n",
526 ok(cchName == strlen("SeCreateTokenPrivilege") + 1,
527 "LookupPrivilegeNameA returned an incorrect required length for\n"
528 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName,
529 lstrlenA("SeCreateTokenPrivilege") + 1);
530 /* check a known value and its returned length on success */
531 cchName = sizeof(buf);
532 ok(pLookupPrivilegeNameA(NULL, &luid, buf, &cchName) &&
533 cchName == strlen("SeCreateTokenPrivilege"),
534 "LookupPrivilegeNameA returned an incorrect output length for\n"
535 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName,
536 (int)strlen("SeCreateTokenPrivilege"));
537 /* check known values */
538 for (i = SE_MIN_WELL_KNOWN_PRIVILEGE; i < SE_MAX_WELL_KNOWN_PRIVILEGE; i++)
541 cchName = sizeof(buf);
542 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
543 ok( ret || GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
544 "LookupPrivilegeNameA(0.%d) failed: %d\n", i, GetLastError());
546 /* check a bogus LUID */
547 luid.LowPart = 0xdeadbeef;
548 cchName = sizeof(buf);
549 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
550 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
551 "LookupPrivilegeNameA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
553 /* check on a bogus system */
554 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
555 cchName = sizeof(buf);
556 ret = pLookupPrivilegeNameA("b0gu5.Nam3", &luid, buf, &cchName);
557 ok( !ret && GetLastError() == RPC_S_SERVER_UNAVAILABLE,
558 "LookupPrivilegeNameA didn't fail with RPC_S_SERVER_UNAVAILABLE: %d\n",
568 static void test_lookupPrivilegeValue(void)
570 static const struct NameToLUID privs[] = {
571 { "SeCreateTokenPrivilege", SE_CREATE_TOKEN_PRIVILEGE },
572 { "SeAssignPrimaryTokenPrivilege", SE_ASSIGNPRIMARYTOKEN_PRIVILEGE },
573 { "SeLockMemoryPrivilege", SE_LOCK_MEMORY_PRIVILEGE },
574 { "SeIncreaseQuotaPrivilege", SE_INCREASE_QUOTA_PRIVILEGE },
575 { "SeMachineAccountPrivilege", SE_MACHINE_ACCOUNT_PRIVILEGE },
576 { "SeTcbPrivilege", SE_TCB_PRIVILEGE },
577 { "SeSecurityPrivilege", SE_SECURITY_PRIVILEGE },
578 { "SeTakeOwnershipPrivilege", SE_TAKE_OWNERSHIP_PRIVILEGE },
579 { "SeLoadDriverPrivilege", SE_LOAD_DRIVER_PRIVILEGE },
580 { "SeSystemProfilePrivilege", SE_SYSTEM_PROFILE_PRIVILEGE },
581 { "SeSystemtimePrivilege", SE_SYSTEMTIME_PRIVILEGE },
582 { "SeProfileSingleProcessPrivilege", SE_PROF_SINGLE_PROCESS_PRIVILEGE },
583 { "SeIncreaseBasePriorityPrivilege", SE_INC_BASE_PRIORITY_PRIVILEGE },
584 { "SeCreatePagefilePrivilege", SE_CREATE_PAGEFILE_PRIVILEGE },
585 { "SeCreatePermanentPrivilege", SE_CREATE_PERMANENT_PRIVILEGE },
586 { "SeBackupPrivilege", SE_BACKUP_PRIVILEGE },
587 { "SeRestorePrivilege", SE_RESTORE_PRIVILEGE },
588 { "SeShutdownPrivilege", SE_SHUTDOWN_PRIVILEGE },
589 { "SeDebugPrivilege", SE_DEBUG_PRIVILEGE },
590 { "SeAuditPrivilege", SE_AUDIT_PRIVILEGE },
591 { "SeSystemEnvironmentPrivilege", SE_SYSTEM_ENVIRONMENT_PRIVILEGE },
592 { "SeChangeNotifyPrivilege", SE_CHANGE_NOTIFY_PRIVILLEGE },
593 { "SeRemoteShutdownPrivilege", SE_REMOTE_SHUTDOWN_PRIVILEGE },
594 { "SeUndockPrivilege", SE_UNDOCK_PRIVILEGE },
595 { "SeSyncAgentPrivilege", SE_SYNC_AGENT_PRIVILEGE },
596 { "SeEnableDelegationPrivilege", SE_ENABLE_DELEGATION_PRIVILEGE },
597 { "SeManageVolumePrivilege", SE_MANAGE_VOLUME_PRIVILEGE },
598 { "SeImpersonatePrivilege", SE_IMPERSONATE_PRIVILEGE },
599 { "SeCreateGlobalPrivilege", SE_CREATE_GLOBAL_PRIVILEGE },
601 BOOL (WINAPI *pLookupPrivilegeValueA)(LPCSTR, LPCSTR, PLUID);
606 /* check whether it's available first */
607 pLookupPrivilegeValueA = (void*)GetProcAddress(hmod, "LookupPrivilegeValueA");
608 if (!pLookupPrivilegeValueA) return;
609 ret = pLookupPrivilegeValueA(NULL, "SeCreateTokenPrivilege", &luid);
610 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
613 /* check a bogus system name */
614 ret = pLookupPrivilegeValueA("b0gu5.Nam3", "SeCreateTokenPrivilege", &luid);
615 ok( !ret && GetLastError() == RPC_S_SERVER_UNAVAILABLE,
616 "LookupPrivilegeValueA didn't fail with RPC_S_SERVER_UNAVAILABLE: %d\n",
618 /* check a NULL string */
619 ret = pLookupPrivilegeValueA(NULL, 0, &luid);
620 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
621 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
623 /* check a bogus privilege name */
624 ret = pLookupPrivilegeValueA(NULL, "SeBogusPrivilege", &luid);
625 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
626 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
628 /* check case insensitive */
629 ret = pLookupPrivilegeValueA(NULL, "sEcREATEtOKENpRIVILEGE", &luid);
631 "LookupPrivilegeValueA(NULL, sEcREATEtOKENpRIVILEGE, &luid) failed: %d\n",
633 for (i = 0; i < sizeof(privs) / sizeof(privs[0]); i++)
635 /* Not all privileges are implemented on all Windows versions, so
636 * don't worry if the call fails
638 if (pLookupPrivilegeValueA(NULL, privs[i].name, &luid))
640 ok(luid.LowPart == privs[i].lowPart,
641 "LookupPrivilegeValueA returned an invalid LUID for %s\n",
647 static void test_luid(void)
650 test_lookupPrivilegeName();
651 test_lookupPrivilegeValue();
654 static void test_FileSecurity(void)
656 char directory[MAX_PATH];
657 DWORD retval, outSize;
661 pGetFileSecurityA = (fnGetFileSecurityA)
662 GetProcAddress( hmod, "GetFileSecurityA" );
663 if( !pGetFileSecurityA )
666 retval = GetTempPathA(sizeof(directory), directory);
668 trace("GetTempPathA failed\n");
672 strcpy(directory, "\\Should not exist");
674 SetLastError(NO_ERROR);
675 result = pGetFileSecurityA( directory,OWNER_SECURITY_INFORMATION,buffer,0x40,&outSize);
676 ok(!result, "GetFileSecurityA should fail for not existing directories/files\n");
677 ok( (GetLastError() == ERROR_FILE_NOT_FOUND ) ||
678 (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) ,
679 "last error ERROR_FILE_NOT_FOUND / ERROR_CALL_NOT_IMPLEMENTED (98) "
680 "expected, got %d\n", GetLastError());
683 static void test_AccessCheck(void)
685 PSID EveryoneSid = NULL, AdminSid = NULL, UsersSid = NULL;
687 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL;
688 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
689 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
690 GENERIC_MAPPING Mapping = { KEY_READ, KEY_WRITE, KEY_EXECUTE, KEY_ALL_ACCESS };
697 PRIVILEGE_SET *PrivSet;
703 NtDllModule = GetModuleHandle("ntdll.dll");
706 skip("not running on NT, skipping test\n");
709 pRtlAdjustPrivilege = (fnRtlAdjustPrivilege)
710 GetProcAddress(NtDllModule, "RtlAdjustPrivilege");
711 if (!pRtlAdjustPrivilege)
713 skip("missing RtlAdjustPrivilege, skipping test\n");
717 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
718 res = InitializeAcl(Acl, 256, ACL_REVISION);
719 if(!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
721 skip("ACLs not implemented - skipping tests\n");
722 HeapFree(GetProcessHeap(), 0, Acl);
725 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
727 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
728 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
730 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
731 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdminSid);
732 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
734 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
735 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &UsersSid);
736 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
738 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
740 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
741 ok(res, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
743 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
744 ok(res, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
746 PrivSetLen = FIELD_OFFSET(PRIVILEGE_SET, Privilege[16]);
747 PrivSet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, PrivSetLen);
748 PrivSet->PrivilegeCount = 16;
750 res = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &ProcessToken);
751 ok(res, "OpenProcessToken failed with error %d\n", GetLastError());
753 pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, FALSE, TRUE, &Enabled);
755 res = DuplicateToken(ProcessToken, SecurityIdentification, &Token);
756 ok(res, "DuplicateToken failed with error %d\n", GetLastError());
758 /* SD without owner/group */
759 SetLastError(0xdeadbeef);
760 Access = AccessStatus = 0xdeadbeef;
761 ret = AccessCheck(SecurityDescriptor, Token, KEY_QUERY_VALUE, &Mapping,
762 PrivSet, &PrivSetLen, &Access, &AccessStatus);
763 err = GetLastError();
764 ok(!ret && err == ERROR_INVALID_SECURITY_DESCR, "AccessCheck should have "
765 "failed with ERROR_INVALID_SECURITY_DESCR, instead of %d\n", err);
766 ok(Access == 0xdeadbeef && AccessStatus == 0xdeadbeef,
767 "Access and/or AccessStatus were changed!\n");
769 /* Set owner and group */
770 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
771 ok(res, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
772 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, TRUE);
773 ok(res, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
775 /* Generic access mask */
776 SetLastError(0xdeadbeef);
777 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
778 PrivSet, &PrivSetLen, &Access, &AccessStatus);
779 err = GetLastError();
780 ok(!ret && err == ERROR_GENERIC_NOT_MAPPED, "AccessCheck should have failed "
781 "with ERROR_GENERIC_NOT_MAPPED, instead of %d\n", err);
782 ok(Access == 0xdeadbeef && AccessStatus == 0xdeadbeef,
783 "Access and/or AccessStatus were changed!\n");
785 /* sd with no dacl present */
786 ret = SetSecurityDescriptorDacl(SecurityDescriptor, FALSE, NULL, FALSE);
788 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
789 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
790 PrivSet, &PrivSetLen, &Access, &AccessStatus);
791 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
792 ok(AccessStatus && (Access == KEY_READ),
793 "AccessCheck failed to grant access with error %d\n",
796 /* sd with NULL dacl */
797 ret = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, NULL, FALSE);
798 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
799 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
800 PrivSet, &PrivSetLen, &Access, &AccessStatus);
801 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
803 ok(AccessStatus && (Access == KEY_READ),
804 "AccessCheck failed to grant access with error %d\n",
807 /* sd with blank dacl */
808 ret = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
809 ok(ret, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
810 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
811 PrivSet, &PrivSetLen, &Access, &AccessStatus);
812 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
813 err = GetLastError();
814 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
815 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
816 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
818 res = AddAccessAllowedAce(Acl, ACL_REVISION, KEY_READ, EveryoneSid);
819 ok(res, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
821 res = AddAccessDeniedAce(Acl, ACL_REVISION, KEY_SET_VALUE, AdminSid);
822 ok(res, "AddAccessDeniedAce failed with error %d\n", GetLastError());
825 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
826 PrivSet, &PrivSetLen, &Access, &AccessStatus);
827 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
828 ok(AccessStatus && (Access == KEY_READ),
829 "AccessCheck failed to grant access with error %d\n",
832 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
833 PrivSet, &PrivSetLen, &Access, &AccessStatus);
834 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
836 "AccessCheck failed to grant any access with error %d\n",
838 trace("AccessCheck with MAXIMUM_ALLOWED got Access 0x%08x\n", Access);
840 /* Access denied by SD */
841 SetLastError(0xdeadbeef);
842 ret = AccessCheck(SecurityDescriptor, Token, KEY_WRITE, &Mapping,
843 PrivSet, &PrivSetLen, &Access, &AccessStatus);
844 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
845 err = GetLastError();
846 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
847 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
848 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
851 PrivSet->PrivilegeCount = 16;
852 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
853 PrivSet, &PrivSetLen, &Access, &AccessStatus);
854 ok(ret && !AccessStatus && GetLastError() == ERROR_PRIVILEGE_NOT_HELD,
855 "AccessCheck should have failed with ERROR_PRIVILEGE_NOT_HELD, instead of %d\n",
858 ret = pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, TRUE, TRUE, &Enabled);
862 PrivSet->PrivilegeCount = 16;
863 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
864 PrivSet, &PrivSetLen, &Access, &AccessStatus);
865 ok(ret && AccessStatus && GetLastError() == 0,
866 "AccessCheck should have succeeded, error %d\n",
868 ok(Access == ACCESS_SYSTEM_SECURITY,
869 "Access should be equal to ACCESS_SYSTEM_SECURITY instead of 0x%08x\n",
873 trace("Couldn't get SE_SECURITY_PRIVILEGE (0x%08x), skipping ACCESS_SYSTEM_SECURITY test\n",
878 res = DuplicateToken(ProcessToken, SecurityAnonymous, &Token);
879 ok(res, "DuplicateToken failed with error %d\n", GetLastError());
881 SetLastError(0xdeadbeef);
882 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
883 PrivSet, &PrivSetLen, &Access, &AccessStatus);
884 err = GetLastError();
885 ok(!ret && err == ERROR_BAD_IMPERSONATION_LEVEL, "AccessCheck should have failed "
886 "with ERROR_BAD_IMPERSONATION_LEVEL, instead of %d\n", err);
890 SetLastError(0xdeadbeef);
891 ret = AccessCheck(SecurityDescriptor, ProcessToken, KEY_READ, &Mapping,
892 PrivSet, &PrivSetLen, &Access, &AccessStatus);
893 err = GetLastError();
894 ok(!ret && err == ERROR_NO_IMPERSONATION_TOKEN, "AccessCheck should have failed "
895 "with ERROR_NO_IMPERSONATION_TOKEN, instead of %d\n", err);
897 CloseHandle(ProcessToken);
900 FreeSid(EveryoneSid);
905 HeapFree(GetProcessHeap(), 0, Acl);
906 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
907 HeapFree(GetProcessHeap(), 0, PrivSet);
910 /* test GetTokenInformation for the various attributes */
911 static void test_token_attr(void)
913 HANDLE Token, ImpersonationToken;
915 TOKEN_PRIVILEGES *Privileges;
916 TOKEN_GROUPS *Groups;
921 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel;
923 /* cygwin-like use case */
924 SetLastError(0xdeadbeef);
925 ret = OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED, &Token);
926 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
928 skip("OpenProcessToken is not implemented\n");
931 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
936 ret = GetTokenInformation(Token, TokenUser,(void*)buf, Size, &Size);
937 ok(ret, "GetTokenInformation failed with error %d\n", GetLastError());
938 Size = sizeof(ImpersonationLevel);
939 ret = GetTokenInformation(Token, TokenImpersonationLevel, &ImpersonationLevel, Size, &Size);
940 GLE = GetLastError();
941 ok(!ret && (GLE == ERROR_INVALID_PARAMETER), "GetTokenInformation(TokenImpersonationLevel) on primary token should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GLE);
945 if(!pConvertSidToStringSidA)
947 skip("ConvertSidToStringSidA is not available\n");
951 SetLastError(0xdeadbeef);
952 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &Token);
953 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
956 ret = GetTokenInformation(Token, TokenGroups, NULL, 0, &Size);
957 Groups = HeapAlloc(GetProcessHeap(), 0, Size);
958 ret = GetTokenInformation(Token, TokenGroups, Groups, Size, &Size);
959 ok(ret, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
960 trace("TokenGroups:\n");
961 for (i = 0; i < Groups->GroupCount; i++)
963 DWORD NameLength = 255;
965 DWORD DomainLength = 255;
967 SID_NAME_USE SidNameUse;
968 pConvertSidToStringSidA(Groups->Groups[i].Sid, &SidString);
971 ret = LookupAccountSid(NULL, Groups->Groups[i].Sid, Name, &NameLength, Domain, &DomainLength, &SidNameUse);
973 trace("\t%s, %s\\%s use: %d attr: 0x%08x\n", SidString, Domain, Name, SidNameUse, Groups->Groups[i].Attributes);
975 trace("\t%s, attr: 0x%08x LookupAccountSid failed with error %d\n", SidString, Groups->Groups[i].Attributes, GetLastError());
976 LocalFree(SidString);
978 HeapFree(GetProcessHeap(), 0, Groups);
981 ret = GetTokenInformation(Token, TokenUser, NULL, 0, &Size);
982 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
983 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
984 User = HeapAlloc(GetProcessHeap(), 0, Size);
985 ret = GetTokenInformation(Token, TokenUser, User, Size, &Size);
987 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
989 pConvertSidToStringSidA(User->User.Sid, &SidString);
990 trace("TokenUser: %s attr: 0x%08x\n", SidString, User->User.Attributes);
991 LocalFree(SidString);
992 HeapFree(GetProcessHeap(), 0, User);
995 ret = GetTokenInformation(Token, TokenPrivileges, NULL, 0, &Size);
996 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
997 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
998 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
999 ret = GetTokenInformation(Token, TokenPrivileges, Privileges, Size, &Size);
1001 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1002 trace("TokenPrivileges:\n");
1003 for (i = 0; i < Privileges->PrivilegeCount; i++)
1006 DWORD NameLen = sizeof(Name)/sizeof(Name[0]);
1007 LookupPrivilegeName(NULL, &Privileges->Privileges[i].Luid, Name, &NameLen);
1008 trace("\t%s, 0x%x\n", Name, Privileges->Privileges[i].Attributes);
1010 HeapFree(GetProcessHeap(), 0, Privileges);
1012 ret = DuplicateToken(Token, SecurityAnonymous, &ImpersonationToken);
1013 ok(ret, "DuplicateToken failed with error %d\n", GetLastError());
1015 Size = sizeof(ImpersonationLevel);
1016 ret = GetTokenInformation(ImpersonationToken, TokenImpersonationLevel, &ImpersonationLevel, Size, &Size);
1017 ok(ret, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
1018 ok(ImpersonationLevel == SecurityAnonymous, "ImpersonationLevel should have been SecurityAnonymous instead of %d\n", ImpersonationLevel);
1020 CloseHandle(ImpersonationToken);
1024 typedef union _MAX_SID
1027 char max[SECURITY_MAX_SID_SIZE];
1030 static void test_sid_str(PSID * sid)
1033 BOOL ret = pConvertSidToStringSidA(sid, &str_sid);
1034 ok(ret, "ConvertSidToStringSidA() failed: %d\n", GetLastError());
1037 char account[MAX_PATH], domain[MAX_PATH];
1039 DWORD acc_size = MAX_PATH;
1040 DWORD dom_size = MAX_PATH;
1041 ret = LookupAccountSid(NULL, sid, account, &acc_size, domain, &dom_size, &use);
1042 ok(ret || (!ret && (GetLastError() == ERROR_NONE_MAPPED)),
1043 "LookupAccountSid(%s) failed: %d\n", str_sid, GetLastError());
1045 trace(" %s %s\\%s %d\n", str_sid, domain, account, use);
1046 else if (GetLastError() == ERROR_NONE_MAPPED)
1047 trace(" %s couldn't be mapped\n", str_sid);
1052 static void test_LookupAccountSid(void)
1054 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
1055 CHAR accountA[MAX_PATH], domainA[MAX_PATH];
1056 DWORD acc_sizeA, dom_sizeA;
1057 DWORD real_acc_sizeA, real_dom_sizeA;
1058 WCHAR accountW[MAX_PATH], domainW[MAX_PATH];
1059 DWORD acc_sizeW, dom_sizeW;
1060 DWORD real_acc_sizeW, real_dom_sizeW;
1061 PSID pUsersSid = NULL;
1069 /* native windows crashes if account size, domain size, or name use is NULL */
1071 ret = AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
1072 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &pUsersSid);
1073 ok(ret || (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED),
1074 "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1076 /* not running on NT so give up */
1077 if (!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1080 real_acc_sizeA = MAX_PATH;
1081 real_dom_sizeA = MAX_PATH;
1082 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &real_acc_sizeA, domainA, &real_dom_sizeA, &use);
1083 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1085 /* try NULL account */
1086 acc_sizeA = MAX_PATH;
1087 dom_sizeA = MAX_PATH;
1088 ret = LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
1089 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1091 /* try NULL domain */
1092 acc_sizeA = MAX_PATH;
1093 dom_sizeA = MAX_PATH;
1094 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
1095 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1097 /* try a small account buffer */
1099 dom_sizeA = MAX_PATH;
1101 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1102 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1103 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1104 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1106 /* try a 0 sized account buffer */
1108 dom_sizeA = MAX_PATH;
1110 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1111 /* this can fail or succeed depending on OS version but the size will always be returned */
1112 ok(acc_sizeA == real_acc_sizeA + 1,
1113 "LookupAccountSidA() Expected acc_size = %u, got %u\n",
1114 real_acc_sizeA + 1, acc_sizeA);
1116 /* try a 0 sized account buffer */
1118 dom_sizeA = MAX_PATH;
1119 ret = LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
1120 /* this can fail or succeed depending on OS version but the size will always be returned */
1121 ok(acc_sizeA == real_acc_sizeA + 1,
1122 "LookupAccountSid() Expected acc_size = %u, got %u\n",
1123 real_acc_sizeA + 1, acc_sizeA);
1125 /* try a small domain buffer */
1127 acc_sizeA = MAX_PATH;
1129 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1130 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1131 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1132 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1134 /* try a 0 sized domain buffer */
1136 acc_sizeA = MAX_PATH;
1138 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1139 /* this can fail or succeed depending on OS version but the size will always be returned */
1140 ok(dom_sizeA == real_dom_sizeA + 1,
1141 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1142 real_dom_sizeA + 1, dom_sizeA);
1144 /* try a 0 sized domain buffer */
1146 acc_sizeA = MAX_PATH;
1147 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
1148 /* this can fail or succeed depending on OS version but the size will always be returned */
1149 ok(dom_sizeA == real_dom_sizeA + 1,
1150 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1151 real_dom_sizeA + 1, dom_sizeA);
1153 real_acc_sizeW = MAX_PATH;
1154 real_dom_sizeW = MAX_PATH;
1155 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &real_acc_sizeW, domainW, &real_dom_sizeW, &use);
1156 ok(ret, "LookupAccountSidW() Expected TRUE, got FALSE\n");
1158 /* native windows crashes if domainW or accountW is NULL */
1160 /* try a small account buffer */
1162 dom_sizeW = MAX_PATH;
1164 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1165 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1166 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1167 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1169 /* try a 0 sized account buffer */
1171 dom_sizeW = MAX_PATH;
1173 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1174 /* this can fail or succeed depending on OS version but the size will always be returned */
1175 ok(acc_sizeW == real_acc_sizeW + 1,
1176 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1177 real_acc_sizeW + 1, acc_sizeW);
1179 /* try a 0 sized account buffer */
1181 dom_sizeW = MAX_PATH;
1182 ret = LookupAccountSidW(NULL, pUsersSid, NULL, &acc_sizeW, domainW, &dom_sizeW, &use);
1183 /* this can fail or succeed depending on OS version but the size will always be returned */
1184 ok(acc_sizeW == real_acc_sizeW + 1,
1185 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1186 real_acc_sizeW + 1, acc_sizeW);
1188 /* try a small domain buffer */
1190 acc_sizeW = MAX_PATH;
1192 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1193 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1194 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1195 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1197 /* try a 0 sized domain buffer */
1199 acc_sizeW = MAX_PATH;
1201 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1202 /* this can fail or succeed depending on OS version but the size will always be returned */
1203 ok(dom_sizeW == real_dom_sizeW + 1,
1204 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1205 real_dom_sizeW + 1, dom_sizeW);
1207 /* try a 0 sized domain buffer */
1209 acc_sizeW = MAX_PATH;
1210 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, NULL, &dom_sizeW, &use);
1211 /* this can fail or succeed depending on OS version but the size will always be returned */
1212 ok(dom_sizeW == real_dom_sizeW + 1,
1213 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1214 real_dom_sizeW + 1, dom_sizeW);
1218 pCreateWellKnownSid = (fnCreateWellKnownSid)GetProcAddress( hmod, "CreateWellKnownSid" );
1220 if (pCreateWellKnownSid && pConvertSidToStringSidA)
1222 trace("Well Known SIDs:\n");
1223 for (i = 0; i <= 60; i++)
1225 size = SECURITY_MAX_SID_SIZE;
1226 if (pCreateWellKnownSid(i, NULL, &max_sid.sid, &size))
1228 if (pConvertSidToStringSidA(&max_sid.sid, &str_sidA))
1230 acc_sizeA = MAX_PATH;
1231 dom_sizeA = MAX_PATH;
1232 if (LookupAccountSidA(NULL, &max_sid.sid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use))
1233 trace(" %d: %s %s\\%s %d\n", i, str_sidA, domainA, accountA, use);
1234 LocalFree(str_sidA);
1239 if (GetLastError() != ERROR_INVALID_PARAMETER)
1240 trace(" CreateWellKnownSid(%d) failed: %d\n", i, GetLastError());
1242 trace(" %d: not supported\n", i);
1246 pLsaQueryInformationPolicy = (fnLsaQueryInformationPolicy)GetProcAddress( hmod, "LsaQueryInformationPolicy");
1247 pLsaOpenPolicy = (fnLsaOpenPolicy)GetProcAddress( hmod, "LsaOpenPolicy");
1248 pLsaFreeMemory = (fnLsaFreeMemory)GetProcAddress( hmod, "LsaFreeMemory");
1249 pLsaClose = (fnLsaClose)GetProcAddress( hmod, "LsaClose");
1251 if (pLsaQueryInformationPolicy && pLsaOpenPolicy && pLsaFreeMemory && pLsaClose)
1255 LSA_OBJECT_ATTRIBUTES object_attributes;
1257 ZeroMemory(&object_attributes, sizeof(object_attributes));
1258 object_attributes.Length = sizeof(object_attributes);
1260 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_ALL_ACCESS, &handle);
1261 ok(status == STATUS_SUCCESS || status == STATUS_ACCESS_DENIED,
1262 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status);
1264 /* try a more restricted access mask if necessary */
1265 if (status == STATUS_ACCESS_DENIED) {
1266 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
1267 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_VIEW_LOCAL_INFORMATION, &handle);
1268 ok(status == STATUS_SUCCESS, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status);
1271 if (status == STATUS_SUCCESS)
1273 PPOLICY_ACCOUNT_DOMAIN_INFO info;
1274 status = pLsaQueryInformationPolicy(handle, PolicyAccountDomainInformation, (PVOID*)&info);
1275 ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy() failed, returned 0x%08x\n", status);
1276 if (status == STATUS_SUCCESS)
1278 ok(info->DomainSid!=0, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) missing SID\n");
1279 if (info->DomainSid)
1281 int count = *GetSidSubAuthorityCount(info->DomainSid);
1282 CopySid(GetSidLengthRequired(count), &max_sid, info->DomainSid);
1283 test_sid_str((PSID)&max_sid.sid);
1284 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_ADMIN;
1285 max_sid.sid.SubAuthorityCount = count + 1;
1286 test_sid_str((PSID)&max_sid.sid);
1287 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_GUEST;
1288 test_sid_str((PSID)&max_sid.sid);
1289 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ADMINS;
1290 test_sid_str((PSID)&max_sid.sid);
1291 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_USERS;
1292 test_sid_str((PSID)&max_sid.sid);
1293 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_GUESTS;
1294 test_sid_str((PSID)&max_sid.sid);
1295 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_COMPUTERS;
1296 test_sid_str((PSID)&max_sid.sid);
1297 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CONTROLLERS;
1298 test_sid_str((PSID)&max_sid.sid);
1299 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CERT_ADMINS;
1300 test_sid_str((PSID)&max_sid.sid);
1301 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_SCHEMA_ADMINS;
1302 test_sid_str((PSID)&max_sid.sid);
1303 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ENTERPRISE_ADMINS;
1304 test_sid_str((PSID)&max_sid.sid);
1305 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_POLICY_ADMINS;
1306 test_sid_str((PSID)&max_sid.sid);
1307 max_sid.sid.SubAuthority[count] = DOMAIN_ALIAS_RID_RAS_SERVERS;
1308 test_sid_str((PSID)&max_sid.sid);
1309 max_sid.sid.SubAuthority[count] = 1000; /* first user account */
1310 test_sid_str((PSID)&max_sid.sid);
1313 pLsaFreeMemory((LPVOID)info);
1316 status = pLsaClose(handle);
1317 ok(status == STATUS_SUCCESS, "LsaClose() failed, returned 0x%08x\n", status);
1323 static void get_sid_info(PSID psid, LPSTR *user, LPSTR *dom)
1325 static CHAR account[UNLEN + 1];
1326 static CHAR domain[UNLEN + 1];
1327 DWORD size, dom_size;
1333 size = dom_size = UNLEN + 1;
1336 LookupAccountSidA(NULL, psid, account, &size, domain, &dom_size, &use);
1339 static void test_LookupAccountName(void)
1341 DWORD sid_size, domain_size, user_size;
1342 DWORD sid_save, domain_save;
1343 CHAR user_name[UNLEN + 1];
1344 SID_NAME_USE sid_use;
1345 LPSTR domain, account, sid_dom;
1349 /* native crashes if (assuming all other parameters correct):
1351 * - Sid is NULL and cbSid is > 0
1352 * - cbSid or cchReferencedDomainName are NULL
1353 * - ReferencedDomainName is NULL and cchReferencedDomainName is the correct size
1356 user_size = UNLEN + 1;
1357 SetLastError(0xdeadbeef);
1358 ret = GetUserNameA(user_name, &user_size);
1359 if (!ret && (GetLastError() == ERROR_NOT_LOGGED_ON))
1361 /* Probably on win9x where the user used 'Cancel' instead of properly logging in */
1362 skip("Cannot get the user name (win9x and not logged in properly)\n");
1365 ok(ret, "Failed to get user name : %d\n", GetLastError());
1370 sid_use = 0xcafebabe;
1371 SetLastError(0xdeadbeef);
1372 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, NULL, &domain_size, &sid_use);
1373 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1375 skip("LookupAccountNameA is not implemented\n");
1378 ok(!ret, "Expected 0, got %d\n", ret);
1379 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1380 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1381 ok(sid_size != 0, "Expected non-zero sid size\n");
1382 ok(domain_size != 0, "Expected non-zero domain size\n");
1383 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
1385 sid_save = sid_size;
1386 domain_save = domain_size;
1388 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
1389 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
1391 /* try valid account name */
1392 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, domain, &domain_size, &sid_use);
1393 get_sid_info(psid, &account, &sid_dom);
1394 ok(ret, "Failed to lookup account name\n");
1395 ok(sid_size == GetLengthSid(psid), "Expected %d, got %d\n", GetLengthSid(psid), sid_size);
1398 ok(!lstrcmp(account, user_name), "Expected %s, got %s\n", user_name, account);
1399 ok(!lstrcmp(domain, sid_dom), "Expected %s, got %s\n", sid_dom, domain);
1400 ok(domain_size == domain_save - 1, "Expected %d, got %d\n", domain_save - 1, domain_size);
1401 ok(lstrlen(domain) == domain_size, "Expected %d\n", lstrlen(domain));
1402 ok(sid_use == SidTypeUser, "Expected SidTypeUser, got %d\n", SidTypeUser);
1404 domain_size = domain_save;
1406 /* NULL Sid with zero sid size */
1407 SetLastError(0xdeadbeef);
1409 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
1410 ok(!ret, "Expected 0, got %d\n", ret);
1411 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1412 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1413 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1414 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1416 /* try cchReferencedDomainName - 1 */
1417 SetLastError(0xdeadbeef);
1419 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
1420 ok(!ret, "Expected 0, got %d\n", ret);
1421 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1422 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1423 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1424 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1426 /* NULL ReferencedDomainName with zero domain name size */
1427 SetLastError(0xdeadbeef);
1429 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, NULL, &domain_size, &sid_use);
1430 ok(!ret, "Expected 0, got %d\n", ret);
1431 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1432 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1433 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1434 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1436 HeapFree(GetProcessHeap(), 0, psid);
1437 HeapFree(GetProcessHeap(), 0, domain);
1439 /* get sizes for NULL account name */
1442 sid_use = 0xcafebabe;
1443 SetLastError(0xdeadbeef);
1444 ret = LookupAccountNameA(NULL, NULL, NULL, &sid_size, NULL, &domain_size, &sid_use);
1445 ok(!ret, "Expected 0, got %d\n", ret);
1446 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1447 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1448 ok(sid_size != 0, "Expected non-zero sid size\n");
1449 ok(domain_size != 0, "Expected non-zero domain size\n");
1450 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
1452 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
1453 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
1455 /* try NULL account name */
1456 ret = LookupAccountNameA(NULL, NULL, psid, &sid_size, domain, &domain_size, &sid_use);
1457 get_sid_info(psid, &account, &sid_dom);
1458 ok(ret, "Failed to lookup account name\n");
1461 /* Using a fixed string will not work on different locales */
1462 ok(!lstrcmp(account, domain),
1463 "Got %s for account and %s for domain, these should be the same\n",
1465 ok(sid_use == SidTypeDomain, "Expected SidTypeDomain, got %d\n", SidTypeDomain);
1468 /* try an invalid account name */
1469 SetLastError(0xdeadbeef);
1472 ret = LookupAccountNameA(NULL, "oogabooga", NULL, &sid_size, NULL, &domain_size, &sid_use);
1473 ok(!ret, "Expected 0, got %d\n", ret);
1476 ok(GetLastError() == ERROR_NONE_MAPPED,
1477 "Expected ERROR_NONE_MAPPED, got %d\n", GetLastError());
1478 ok(sid_size == 0, "Expected 0, got %d\n", sid_size);
1479 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
1482 HeapFree(GetProcessHeap(), 0, psid);
1483 HeapFree(GetProcessHeap(), 0, domain);
1486 static void test_security_descriptor(void)
1488 SECURITY_DESCRIPTOR sd;
1491 BOOL isDefault, isPresent;
1495 InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
1496 ok(GetSecurityDescriptorOwner(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
1497 expect_eq(psid, NULL, PSID, "%p");
1498 expect_eq(isDefault, FALSE, BOOL, "%d");
1499 sd.Control |= SE_DACL_PRESENT | SE_SACL_PRESENT;
1501 SetLastError(0xdeadbeef);
1503 expect_eq(MakeSelfRelativeSD(&sd, buf, &size), FALSE, BOOL, "%d");
1504 expect_eq(GetLastError(), ERROR_INSUFFICIENT_BUFFER, DWORD, "%u");
1505 ok(size > 5, "Size not increased\n");
1508 expect_eq(MakeSelfRelativeSD(&sd, buf, &size), TRUE, BOOL, "%d");
1509 ok(GetSecurityDescriptorOwner(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
1510 expect_eq(psid, NULL, PSID, "%p");
1511 expect_eq(isDefault, FALSE, BOOL, "%d");
1512 ok(GetSecurityDescriptorGroup(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
1513 expect_eq(psid, NULL, PSID, "%p");
1514 expect_eq(isDefault, FALSE, BOOL, "%d");
1515 ok(GetSecurityDescriptorDacl(&sd, &isPresent, &pacl, &isDefault), "GetSecurityDescriptorOwner failed\n");
1516 expect_eq(isPresent, TRUE, BOOL, "%d");
1517 expect_eq(psid, NULL, PSID, "%p");
1518 expect_eq(isDefault, FALSE, BOOL, "%d");
1519 ok(GetSecurityDescriptorSacl(&sd, &isPresent, &pacl, &isDefault), "GetSecurityDescriptorOwner failed\n");
1520 expect_eq(isPresent, TRUE, BOOL, "%d");
1521 expect_eq(psid, NULL, PSID, "%p");
1522 expect_eq(isDefault, FALSE, BOOL, "%d");
1526 #define TEST_GRANTED_ACCESS(a,b) test_granted_access(a,b,__LINE__)
1527 static void test_granted_access(HANDLE handle, ACCESS_MASK access, int line)
1529 OBJECT_BASIC_INFORMATION obj_info;
1532 if (!pNtQueryObject)
1534 skip_(__FILE__, line)("Not NT platform - skipping tests\n");
1538 status = pNtQueryObject( handle, ObjectBasicInformation, &obj_info,
1539 sizeof(obj_info), NULL );
1540 ok_(__FILE__, line)(!status, "NtQueryObject with err: %08x\n", status);
1541 ok_(__FILE__, line)(obj_info.GrantedAccess == access, "Granted access should "
1542 "be 0x%08x, instead of 0x%08x\n", access, obj_info.GrantedAccess);
1545 #define CHECK_SET_SECURITY(o,i,e) \
1549 SetLastError( 0xdeadbeef ); \
1550 res = SetKernelObjectSecurity( o, i, SecurityDescriptor ); \
1551 err = GetLastError(); \
1552 if (e == ERROR_SUCCESS) \
1553 ok(res, "SetKernelObjectSecurity failed with %d\n", err); \
1555 ok(!res && err == e, "SetKernelObjectSecurity should have failed " \
1556 "with %s, instead of %d\n", #e, err); \
1559 static void test_process_security(void)
1562 char owner[32], group[32];
1563 PSID AdminSid = NULL, UsersSid = NULL;
1565 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL;
1566 char buffer[MAX_PATH];
1567 PROCESS_INFORMATION info;
1568 STARTUPINFOA startup;
1569 SECURITY_ATTRIBUTES psa;
1570 HANDLE token, event;
1573 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
1574 res = InitializeAcl(Acl, 256, ACL_REVISION);
1575 if (!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1577 skip("ACLs not implemented - skipping tests\n");
1578 HeapFree(GetProcessHeap(), 0, Acl);
1581 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
1583 /* get owner from the token we might be running as a user not admin */
1584 res = OpenProcessToken( GetCurrentProcess(), MAXIMUM_ALLOWED, &token );
1585 ok(res, "OpenProcessToken failed with error %d\n", GetLastError());
1588 HeapFree(GetProcessHeap(), 0, Acl);
1592 res = GetTokenInformation( token, TokenOwner, owner, sizeof(owner), &tmp );
1593 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
1594 AdminSid = ((TOKEN_OWNER*)owner)->Owner;
1595 res = GetTokenInformation( token, TokenPrimaryGroup, group, sizeof(group), &tmp );
1596 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
1597 UsersSid = ((TOKEN_PRIMARY_GROUP*)group)->PrimaryGroup;
1599 CloseHandle( token );
1602 HeapFree(GetProcessHeap(), 0, Acl);
1606 res = AddAccessDeniedAce(Acl, ACL_REVISION, PROCESS_VM_READ, AdminSid);
1607 ok(res, "AddAccessDeniedAce failed with error %d\n", GetLastError());
1608 res = AddAccessAllowedAce(Acl, ACL_REVISION, PROCESS_ALL_ACCESS, AdminSid);
1609 ok(res, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
1611 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
1612 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
1613 ok(res, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
1615 event = CreateEvent( NULL, TRUE, TRUE, "test_event" );
1616 ok(event != NULL, "CreateEvent %d\n", GetLastError());
1618 SecurityDescriptor->Revision = 0;
1619 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_UNKNOWN_REVISION );
1620 SecurityDescriptor->Revision = SECURITY_DESCRIPTOR_REVISION;
1622 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_INVALID_SECURITY_DESCR );
1623 CHECK_SET_SECURITY( event, GROUP_SECURITY_INFORMATION, ERROR_INVALID_SECURITY_DESCR );
1624 CHECK_SET_SECURITY( event, SACL_SECURITY_INFORMATION, ERROR_ACCESS_DENIED );
1625 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
1626 /* NULL DACL is valid and means default DACL from token */
1627 SecurityDescriptor->Control |= SE_DACL_PRESENT;
1628 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
1630 /* Set owner and group and dacl */
1631 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
1632 ok(res, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
1633 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_SUCCESS );
1634 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, FALSE);
1635 ok(res, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
1636 CHECK_SET_SECURITY( event, GROUP_SECURITY_INFORMATION, ERROR_SUCCESS );
1637 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
1638 ok(res, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1639 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
1641 sprintf(buffer, "%s tests/security.c test", myARGV[0]);
1642 memset(&startup, 0, sizeof(startup));
1643 startup.cb = sizeof(startup);
1644 startup.dwFlags = STARTF_USESHOWWINDOW;
1645 startup.wShowWindow = SW_SHOWNORMAL;
1647 psa.nLength = sizeof(psa);
1648 psa.lpSecurityDescriptor = SecurityDescriptor;
1649 psa.bInheritHandle = TRUE;
1651 /* Doesn't matter what ACL say we should get full access for ourselves */
1652 ok(CreateProcessA( NULL, buffer, &psa, NULL, FALSE, 0, NULL, NULL, &startup, &info ),
1653 "CreateProcess with err:%d\n", GetLastError());
1654 TEST_GRANTED_ACCESS( info.hProcess, PROCESS_ALL_ACCESS );
1655 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1657 CloseHandle( event );
1658 HeapFree(GetProcessHeap(), 0, Acl);
1659 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
1662 static void test_process_security_child(void)
1664 HANDLE handle, handle1;
1668 handle = OpenProcess( PROCESS_TERMINATE, FALSE, GetCurrentProcessId() );
1669 ok(handle != NULL, "OpenProcess(PROCESS_TERMINATE) with err:%d\n", GetLastError());
1670 TEST_GRANTED_ACCESS( handle, PROCESS_TERMINATE );
1672 ok(DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
1673 &handle1, 0, TRUE, DUPLICATE_SAME_ACCESS ),
1674 "duplicating handle err:%d\n", GetLastError());
1675 TEST_GRANTED_ACCESS( handle1, PROCESS_TERMINATE );
1677 CloseHandle( handle1 );
1679 SetLastError( 0xdeadbeef );
1680 ret = DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
1681 &handle1, PROCESS_ALL_ACCESS, TRUE, 0 );
1682 err = GetLastError();
1684 ok(!ret && err == ERROR_ACCESS_DENIED, "duplicating handle should have failed "
1685 "with STATUS_ACCESS_DENIED, instead of err:%d\n", err);
1687 CloseHandle( handle );
1689 /* These two should fail - they are denied by ACL */
1690 handle = OpenProcess( PROCESS_VM_READ, FALSE, GetCurrentProcessId() );
1692 ok(handle == NULL, "OpenProcess(PROCESS_VM_READ) should have failed\n");
1693 handle = OpenProcess( PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId() );
1695 ok(handle == NULL, "OpenProcess(PROCESS_ALL_ACCESS) should have failed\n");
1697 /* Documented privilege elevation */
1698 ok(DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
1699 &handle, 0, TRUE, DUPLICATE_SAME_ACCESS ),
1700 "duplicating handle err:%d\n", GetLastError());
1701 TEST_GRANTED_ACCESS( handle, PROCESS_ALL_ACCESS );
1703 CloseHandle( handle );
1705 /* Same only explicitly asking for all access rights */
1706 ok(DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
1707 &handle, PROCESS_ALL_ACCESS, TRUE, 0 ),
1708 "duplicating handle err:%d\n", GetLastError());
1709 TEST_GRANTED_ACCESS( handle, PROCESS_ALL_ACCESS );
1710 ok(DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
1711 &handle1, PROCESS_VM_READ, TRUE, 0 ),
1712 "duplicating handle err:%d\n", GetLastError());
1713 TEST_GRANTED_ACCESS( handle1, PROCESS_VM_READ );
1714 CloseHandle( handle1 );
1715 CloseHandle( handle );
1718 static void test_impersonation_level(void)
1720 HANDLE Token, ProcessToken;
1723 TOKEN_PRIVILEGES *Privileges;
1725 PRIVILEGE_SET *PrivilegeSet;
1731 pDuplicateTokenEx = (fnDuplicateTokenEx) GetProcAddress(hmod, "DuplicateTokenEx");
1732 if( !pDuplicateTokenEx ) {
1733 skip("DuplicateTokenEx is not available\n");
1736 SetLastError(0xdeadbeef);
1737 ret = ImpersonateSelf(SecurityAnonymous);
1738 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1740 skip("ImpersonateSelf is not implemented\n");
1743 ok(ret, "ImpersonateSelf(SecurityAnonymous) failed with error %d\n", GetLastError());
1744 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
1745 ok(!ret, "OpenThreadToken should have failed\n");
1746 error = GetLastError();
1747 ok(error == ERROR_CANT_OPEN_ANONYMOUS, "OpenThreadToken on anonymous token should have returned ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error);
1748 /* can't perform access check when opening object against an anonymous impersonation token */
1750 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
1751 ok(error == ERROR_INVALID_HANDLE, "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE instead of %d\n", error);
1755 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, &ProcessToken);
1756 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1758 ret = pDuplicateTokenEx(ProcessToken,
1759 TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_IMPERSONATE, NULL,
1760 SecurityAnonymous, TokenImpersonation, &Token);
1761 ok(ret, "DuplicateTokenEx failed with error %d\n", GetLastError());
1762 /* can't increase the impersonation level */
1763 ret = DuplicateToken(Token, SecurityIdentification, &Token2);
1764 error = GetLastError();
1765 ok(!ret && error == ERROR_BAD_IMPERSONATION_LEVEL,
1766 "Duplicating a token and increasing the impersonation level should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
1767 /* we can query anything from an anonymous token, including the user */
1768 ret = GetTokenInformation(Token, TokenUser, NULL, 0, &Size);
1769 error = GetLastError();
1770 ok(!ret && error == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenUser) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error);
1771 User = (TOKEN_USER *)HeapAlloc(GetProcessHeap(), 0, Size);
1772 ret = GetTokenInformation(Token, TokenUser, User, Size, &Size);
1773 ok(ret, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1774 HeapFree(GetProcessHeap(), 0, User);
1776 /* PrivilegeCheck fails with SecurityAnonymous level */
1777 ret = GetTokenInformation(Token, TokenPrivileges, NULL, 0, &Size);
1778 error = GetLastError();
1779 ok(!ret && error == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenPrivileges) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error);
1780 Privileges = (TOKEN_PRIVILEGES *)HeapAlloc(GetProcessHeap(), 0, Size);
1781 ret = GetTokenInformation(Token, TokenPrivileges, Privileges, Size, &Size);
1782 ok(ret, "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1784 PrivilegeSet = (PRIVILEGE_SET *)HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PRIVILEGE_SET, Privilege[Privileges->PrivilegeCount]));
1785 PrivilegeSet->PrivilegeCount = Privileges->PrivilegeCount;
1786 memcpy(PrivilegeSet->Privilege, Privileges->Privileges, PrivilegeSet->PrivilegeCount * sizeof(PrivilegeSet->Privilege[0]));
1787 PrivilegeSet->Control = PRIVILEGE_SET_ALL_NECESSARY;
1788 HeapFree(GetProcessHeap(), 0, Privileges);
1790 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
1791 error = GetLastError();
1792 ok(!ret && error == ERROR_BAD_IMPERSONATION_LEVEL, "PrivilegeCheck for SecurityAnonymous token should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
1796 ret = ImpersonateSelf(SecurityIdentification);
1797 ok(ret, "ImpersonateSelf(SecurityIdentification) failed with error %d\n", GetLastError());
1798 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
1799 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
1801 /* can't perform access check when opening object against an identification impersonation token */
1802 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
1804 ok(error == ERROR_INVALID_HANDLE, "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE instead of %d\n", error);
1806 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
1807 ok(ret, "PrivilegeCheck for SecurityIdentification failed with error %d\n", GetLastError());
1811 ret = ImpersonateSelf(SecurityImpersonation);
1812 ok(ret, "ImpersonateSelf(SecurityImpersonation) failed with error %d\n", GetLastError());
1813 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
1814 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
1815 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
1816 ok(error == ERROR_SUCCESS, "RegOpenKeyEx should have succeeded instead of failing with %d\n", error);
1818 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
1819 ok(ret, "PrivilegeCheck for SecurityImpersonation failed with error %d\n", GetLastError());
1823 CloseHandle(ProcessToken);
1825 HeapFree(GetProcessHeap(), 0, PrivilegeSet);
1828 static void test_SetEntriesInAcl(void)
1830 ACL *acl = (ACL*)0xdeadbeef;
1833 if (!pSetEntriesInAclW)
1835 skip("SetEntriesInAclW is not available\n");
1839 res = pSetEntriesInAclW(0, NULL, NULL, &acl);
1840 if(res == ERROR_CALL_NOT_IMPLEMENTED)
1842 skip("SetEntriesInAclW is not implemented\n");
1845 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
1846 ok(acl == NULL, "acl=%p, expected NULL\n", acl);
1849 static void test_GetNamedSecurityInfoA(void)
1851 PSECURITY_DESCRIPTOR pSecDesc;
1853 SECURITY_DESCRIPTOR_CONTROL control;
1856 BOOL owner_defaulted;
1857 BOOL group_defaulted;
1860 CHAR windows_dir[MAX_PATH];
1862 if (!pGetNamedSecurityInfoA)
1864 skip("GetNamedSecurityInfoA is not available\n");
1868 ret = GetWindowsDirectoryA(windows_dir, MAX_PATH);
1869 ok(ret, "GetWindowsDirectory failed with error %d\n", GetLastError());
1871 SetLastError(0xdeadbeef);
1872 error = pGetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,
1873 OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION,
1874 NULL, NULL, NULL, NULL, &pSecDesc);
1875 if (error != ERROR_SUCCESS && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1877 skip("GetNamedSecurityInfoA is not implemented\n");
1880 ok(!error, "GetNamedSecurityInfo failed with error %d\n", error);
1882 ret = GetSecurityDescriptorControl(pSecDesc, &control, &revision);
1883 ok(ret, "GetSecurityDescriptorControl failed with error %d\n", GetLastError());
1884 ok((control & (SE_SELF_RELATIVE|SE_DACL_PRESENT)) == (SE_SELF_RELATIVE|SE_DACL_PRESENT),
1885 "control (0x%x) doesn't have (SE_SELF_RELATIVE|SE_DACL_PRESENT) flags set\n", control);
1886 ok(revision == SECURITY_DESCRIPTOR_REVISION1, "revision was %d instead of 1\n", revision);
1887 ret = GetSecurityDescriptorOwner(pSecDesc, &owner, &owner_defaulted);
1888 ok(ret, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
1889 ok(owner != NULL, "owner should not be NULL\n");
1890 ret = GetSecurityDescriptorGroup(pSecDesc, &group, &group_defaulted);
1891 ok(ret, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
1892 ok(group != NULL, "group should not be NULL\n");
1895 static void test_ConvertStringSecurityDescriptor(void)
1898 PSECURITY_DESCRIPTOR pSD;
1900 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA)
1902 skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
1906 SetLastError(0xdeadbeef);
1907 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1908 "D:(A;;GA;;;WD)", 0xdeadbeef, &pSD, NULL);
1909 ok(!ret && GetLastError() == ERROR_UNKNOWN_REVISION,
1910 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_UNKNOWN_REVISION instead of %d\n",
1913 /* test ACE string type */
1914 SetLastError(0xdeadbeef);
1915 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1916 "D:(A;;GA;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1917 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1920 SetLastError(0xdeadbeef);
1921 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1922 "D:(D;;GA;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1923 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1926 SetLastError(0xdeadbeef);
1927 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1928 "ERROR:(D;;GA;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1929 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
1930 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
1933 /* test ACE string access rights */
1934 SetLastError(0xdeadbeef);
1935 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1936 "D:(A;;GA;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1937 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1939 SetLastError(0xdeadbeef);
1940 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1941 "D:(A;;GRGWGX;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1942 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1944 SetLastError(0xdeadbeef);
1945 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1946 "D:(A;;RCSDWDWO;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1947 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1949 SetLastError(0xdeadbeef);
1950 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1951 "D:(A;;RPWPCCDCLCSWLODTCR;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1952 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1954 SetLastError(0xdeadbeef);
1955 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1956 "D:(A;;FAFRFWFX;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1957 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1959 SetLastError(0xdeadbeef);
1960 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1961 "D:(A;;KAKRKWKX;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1962 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1964 SetLastError(0xdeadbeef);
1965 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1966 "D:(A;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1967 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1969 SetLastError(0xdeadbeef);
1970 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1971 "S:(AU;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1972 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1975 /* test ACE string access right error case */
1976 SetLastError(0xdeadbeef);
1977 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1978 "D:(A;;ROB;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1980 ok(!ret && GetLastError() == ERROR_INVALID_ACL,
1981 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_ACL instead of %d\n",
1984 /* test ACE string SID */
1985 SetLastError(0xdeadbeef);
1986 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1987 "D:(D;;GA;;;S-1-0-0)", SDDL_REVISION_1, &pSD, NULL);
1988 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1991 SetLastError(0xdeadbeef);
1992 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1993 "D:(D;;GA;;;Nonexistent account)", SDDL_REVISION_1, &pSD, NULL);
1995 ok(!ret && GetLastError() == ERROR_INVALID_ACL,
1996 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_ACL instead of %d\n",
2000 static void test_ConvertSecurityDescriptorToString()
2002 SECURITY_DESCRIPTOR desc;
2003 SECURITY_INFORMATION sec_info = OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION|SACL_SECURITY_INFORMATION;
2012 if (!pConvertSecurityDescriptorToStringSecurityDescriptorA)
2014 skip("ConvertSecurityDescriptorToStringSecurityDescriptor is not available\n");
2018 /* It seems Windows XP adds an extra character to the length of the string for each ACE in an ACL. We
2019 * don't replicate this feature so we only test len >= strlen+1. */
2020 #define CHECK_RESULT_AND_FREE(exp_str) \
2021 ok(strcmp(string, (exp_str)) == 0, "String mismatch (expected \"%s\", got \"%s\")\n", (exp_str), string); \
2022 ok(len >= (strlen(exp_str) + 1), "Length mismatch (expected %d, got %d)\n", strlen(exp_str) + 1, len); \
2025 InitializeSecurityDescriptor(&desc, SECURITY_DESCRIPTOR_REVISION);
2026 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2027 CHECK_RESULT_AND_FREE("");
2030 CreateWellKnownSid(WinLocalSid, NULL, sid_buf, &size);
2031 SetSecurityDescriptorOwner(&desc, (PSID)sid_buf, FALSE);
2032 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2033 CHECK_RESULT_AND_FREE("O:S-1-2-0");
2035 SetSecurityDescriptorOwner(&desc, (PSID)sid_buf, TRUE);
2036 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2037 CHECK_RESULT_AND_FREE("O:S-1-2-0");
2039 size = sizeof(sid_buf);
2040 CreateWellKnownSid(WinLocalSystemSid, NULL, sid_buf, &size);
2041 SetSecurityDescriptorOwner(&desc, (PSID)sid_buf, TRUE);
2042 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2043 CHECK_RESULT_AND_FREE("O:SY");
2045 ConvertStringSidToSid("S-1-5-21-93476-23408-4576", &psid);
2046 SetSecurityDescriptorGroup(&desc, psid, TRUE);
2047 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2048 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576");
2050 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, GROUP_SECURITY_INFORMATION, &string, &len), "Conversion failed\n");
2051 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
2053 pacl = (PACL)acl_buf;
2054 InitializeAcl(pacl, sizeof(acl_buf), ACL_REVISION);
2055 SetSecurityDescriptorDacl(&desc, TRUE, pacl, TRUE);
2056 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2057 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
2059 SetSecurityDescriptorDacl(&desc, TRUE, pacl, FALSE);
2060 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2061 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
2063 ConvertStringSidToSid("S-1-5-6", &psid2);
2064 AddAccessAllowedAceEx(pacl, ACL_REVISION, NO_PROPAGATE_INHERIT_ACE, 0xf0000000, psid2);
2065 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2066 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)");
2068 AddAccessAllowedAceEx(pacl, ACL_REVISION, INHERIT_ONLY_ACE|INHERITED_ACE, 0x00000003, psid2);
2069 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2070 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)");
2072 AddAccessDeniedAceEx(pacl, ACL_REVISION, OBJECT_INHERIT_ACE|CONTAINER_INHERIT_ACE, 0xffffffff, psid);
2073 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2074 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)");
2077 pacl = (PACL)acl_buf;
2078 InitializeAcl(pacl, sizeof(acl_buf), ACL_REVISION);
2079 SetSecurityDescriptorSacl(&desc, TRUE, pacl, FALSE);
2080 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2081 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:");
2083 SetSecurityDescriptorDacl(&desc, TRUE, NULL, FALSE);
2084 AddAuditAccessAceEx(pacl, ACL_REVISION, VALID_INHERIT_FLAGS, KEY_READ|KEY_WRITE, psid2, TRUE, TRUE);
2085 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2086 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)");
2088 AddAuditAccessAceEx(pacl, ACL_REVISION, NO_PROPAGATE_INHERIT_ACE, FILE_GENERIC_READ|FILE_GENERIC_WRITE, psid2, TRUE, FALSE);
2089 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2090 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)");
2093 static void test_PrivateObjectSecurity(void)
2095 SECURITY_INFORMATION sec_info = OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION|SACL_SECURITY_INFORMATION;
2096 SECURITY_DESCRIPTOR_CONTROL ctrl;
2097 PSECURITY_DESCRIPTOR sec;
2103 PSECURITY_DESCRIPTOR buf;
2105 ok(ConvertStringSecurityDescriptorToSecurityDescriptorA(
2107 "G:S-1-5-21-93476-23408-4576"
2108 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
2109 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
2110 buf = HeapAlloc(GetProcessHeap(), 0, dwDescSize);
2111 SetSecurityDescriptorControl(sec, SE_DACL_PROTECTED, SE_DACL_PROTECTED);
2112 GetSecurityDescriptorControl(sec, &ctrl, &dwRevision);
2113 todo_wine expect_eq(ctrl, 0x9014, int, "%x");
2115 ok(GetPrivateObjectSecurity(sec, GROUP_SECURITY_INFORMATION, buf, dwDescSize, &retSize),
2116 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
2117 ok(retSize <= dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
2118 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2119 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
2120 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
2121 expect_eq(ctrl, 0x8000, int, "%x");
2123 ok(GetPrivateObjectSecurity(sec, GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION, buf, dwDescSize, &retSize),
2124 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
2125 ok(retSize <= dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
2126 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed err=%u\n", GetLastError());
2127 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)");
2128 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
2129 expect_eq(ctrl, 0x8004, int, "%x");
2131 ok(GetPrivateObjectSecurity(sec, sec_info, buf, dwDescSize, &retSize),
2132 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
2133 ok(retSize == dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
2134 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2135 CHECK_RESULT_AND_FREE("O:SY"
2136 "G:S-1-5-21-93476-23408-4576"
2137 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
2138 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)");
2139 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
2140 expect_eq(ctrl, 0x8014, int, "%x");
2142 SetLastError(0xdeadbeef);
2143 ok(GetPrivateObjectSecurity(sec, sec_info, buf, 5, &retSize) == FALSE, "GetPrivateObjectSecurity should have failed\n");
2144 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected error ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
2147 HeapFree(GetProcessHeap(), 0, buf);
2149 #undef CHECK_RESULT_AND_FREE
2151 START_TEST(security)
2158 test_process_security_child();
2164 test_FileSecurity();
2167 test_LookupAccountSid();
2168 test_LookupAccountName();
2169 test_security_descriptor();
2170 test_process_security();
2171 test_impersonation_level();
2172 test_SetEntriesInAcl();
2173 test_GetNamedSecurityInfoA();
2174 test_ConvertStringSecurityDescriptor();
2175 test_ConvertSecurityDescriptorToString();
2176 test_PrivateObjectSecurity();