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 res = AddAccessAllowedAce(Acl, ACL_REVISION, KEY_READ, EveryoneSid);
739 ok(res, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
741 res = AddAccessDeniedAce(Acl, ACL_REVISION, KEY_SET_VALUE, AdminSid);
742 ok(res, "AddAccessDeniedAce failed with error %d\n", GetLastError());
744 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
746 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
747 ok(res, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
749 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
750 ok(res, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
752 PrivSetLen = FIELD_OFFSET(PRIVILEGE_SET, Privilege[16]);
753 PrivSet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, PrivSetLen);
754 PrivSet->PrivilegeCount = 16;
756 res = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &ProcessToken);
757 ok(res, "OpenProcessToken failed with error %d\n", GetLastError());
759 pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, FALSE, TRUE, &Enabled);
761 res = DuplicateToken(ProcessToken, SecurityIdentification, &Token);
762 ok(res, "DuplicateToken failed with error %d\n", GetLastError());
764 /* SD without owner/group */
765 SetLastError(0xdeadbeef);
766 Access = AccessStatus = 0xdeadbeef;
767 ret = AccessCheck(SecurityDescriptor, Token, KEY_QUERY_VALUE, &Mapping,
768 PrivSet, &PrivSetLen, &Access, &AccessStatus);
769 err = GetLastError();
770 ok(!ret && err == ERROR_INVALID_SECURITY_DESCR, "AccessCheck should have "
771 "failed with ERROR_INVALID_SECURITY_DESCR, instead of %d\n", err);
772 ok(Access == 0xdeadbeef && AccessStatus == 0xdeadbeef,
773 "Access and/or AccessStatus were changed!\n");
775 /* Set owner and group */
776 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
777 ok(res, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
778 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, TRUE);
779 ok(res, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
781 /* Generic access mask */
782 SetLastError(0xdeadbeef);
783 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
784 PrivSet, &PrivSetLen, &Access, &AccessStatus);
785 err = GetLastError();
786 ok(!ret && err == ERROR_GENERIC_NOT_MAPPED, "AccessCheck should have failed "
787 "with ERROR_GENERIC_NOT_MAPPED, instead of %d\n", err);
788 ok(Access == 0xdeadbeef && AccessStatus == 0xdeadbeef,
789 "Access and/or AccessStatus were changed!\n");
791 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
792 PrivSet, &PrivSetLen, &Access, &AccessStatus);
793 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
794 ok(AccessStatus && (Access == KEY_READ),
795 "AccessCheck failed to grant access with error %d\n",
798 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
799 PrivSet, &PrivSetLen, &Access, &AccessStatus);
800 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
802 "AccessCheck failed to grant any access with error %d\n",
804 trace("AccessCheck with MAXIMUM_ALLOWED got Access 0x%08x\n", Access);
806 /* Access denied by SD */
807 SetLastError(0xdeadbeef);
808 ret = AccessCheck(SecurityDescriptor, Token, KEY_WRITE, &Mapping,
809 PrivSet, &PrivSetLen, &Access, &AccessStatus);
810 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
811 err = GetLastError();
812 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
813 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
814 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
817 PrivSet->PrivilegeCount = 16;
818 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
819 PrivSet, &PrivSetLen, &Access, &AccessStatus);
820 ok(ret && !AccessStatus && GetLastError() == ERROR_PRIVILEGE_NOT_HELD,
821 "AccessCheck should have failed with ERROR_PRIVILEGE_NOT_HELD, instead of %d\n",
824 ret = pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, TRUE, TRUE, &Enabled);
828 PrivSet->PrivilegeCount = 16;
829 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
830 PrivSet, &PrivSetLen, &Access, &AccessStatus);
831 ok(ret && AccessStatus && GetLastError() == 0,
832 "AccessCheck should have succeeded, error %d\n",
834 ok(Access == ACCESS_SYSTEM_SECURITY,
835 "Access should be equal to ACCESS_SYSTEM_SECURITY instead of 0x%08x\n",
839 trace("Couldn't get SE_SECURITY_PRIVILEGE (0x%08x), skipping ACCESS_SYSTEM_SECURITY test\n",
844 res = DuplicateToken(ProcessToken, SecurityAnonymous, &Token);
845 ok(res, "DuplicateToken failed with error %d\n", GetLastError());
847 SetLastError(0xdeadbeef);
848 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
849 PrivSet, &PrivSetLen, &Access, &AccessStatus);
850 err = GetLastError();
851 ok(!ret && err == ERROR_BAD_IMPERSONATION_LEVEL, "AccessCheck should have failed "
852 "with ERROR_BAD_IMPERSONATION_LEVEL, instead of %d\n", err);
856 SetLastError(0xdeadbeef);
857 ret = AccessCheck(SecurityDescriptor, ProcessToken, KEY_READ, &Mapping,
858 PrivSet, &PrivSetLen, &Access, &AccessStatus);
859 err = GetLastError();
860 ok(!ret && err == ERROR_NO_IMPERSONATION_TOKEN, "AccessCheck should have failed "
861 "with ERROR_NO_IMPERSONATION_TOKEN, instead of %d\n", err);
863 CloseHandle(ProcessToken);
866 FreeSid(EveryoneSid);
871 HeapFree(GetProcessHeap(), 0, Acl);
872 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
873 HeapFree(GetProcessHeap(), 0, PrivSet);
876 /* test GetTokenInformation for the various attributes */
877 static void test_token_attr(void)
879 HANDLE Token, ImpersonationToken;
881 TOKEN_PRIVILEGES *Privileges;
882 TOKEN_GROUPS *Groups;
887 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel;
889 /* cygwin-like use case */
890 SetLastError(0xdeadbeef);
891 ret = OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED, &Token);
892 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
894 skip("OpenProcessToken is not implemented\n");
897 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
902 ret = GetTokenInformation(Token, TokenUser,(void*)buf, Size, &Size);
903 ok(ret, "GetTokenInformation failed with error %d\n", GetLastError());
904 Size = sizeof(ImpersonationLevel);
905 ret = GetTokenInformation(Token, TokenImpersonationLevel, &ImpersonationLevel, Size, &Size);
906 GLE = GetLastError();
907 ok(!ret && (GLE == ERROR_INVALID_PARAMETER), "GetTokenInformation(TokenImpersonationLevel) on primary token should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GLE);
911 if(!pConvertSidToStringSidA)
913 skip("ConvertSidToStringSidA is not available\n");
917 SetLastError(0xdeadbeef);
918 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &Token);
919 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
922 ret = GetTokenInformation(Token, TokenGroups, NULL, 0, &Size);
923 Groups = HeapAlloc(GetProcessHeap(), 0, Size);
924 ret = GetTokenInformation(Token, TokenGroups, Groups, Size, &Size);
925 ok(ret, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
926 trace("TokenGroups:\n");
927 for (i = 0; i < Groups->GroupCount; i++)
929 DWORD NameLength = 255;
931 DWORD DomainLength = 255;
933 SID_NAME_USE SidNameUse;
934 pConvertSidToStringSidA(Groups->Groups[i].Sid, &SidString);
937 ret = LookupAccountSid(NULL, Groups->Groups[i].Sid, Name, &NameLength, Domain, &DomainLength, &SidNameUse);
939 trace("\t%s, %s\\%s use: %d attr: 0x%08x\n", SidString, Domain, Name, SidNameUse, Groups->Groups[i].Attributes);
941 trace("\t%s, attr: 0x%08x LookupAccountSid failed with error %d\n", SidString, Groups->Groups[i].Attributes, GetLastError());
942 LocalFree(SidString);
944 HeapFree(GetProcessHeap(), 0, Groups);
947 ret = GetTokenInformation(Token, TokenUser, NULL, 0, &Size);
948 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
949 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
950 User = HeapAlloc(GetProcessHeap(), 0, Size);
951 ret = GetTokenInformation(Token, TokenUser, User, Size, &Size);
953 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
955 pConvertSidToStringSidA(User->User.Sid, &SidString);
956 trace("TokenUser: %s attr: 0x%08x\n", SidString, User->User.Attributes);
957 LocalFree(SidString);
958 HeapFree(GetProcessHeap(), 0, User);
961 ret = GetTokenInformation(Token, TokenPrivileges, NULL, 0, &Size);
962 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
963 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
964 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
965 ret = GetTokenInformation(Token, TokenPrivileges, Privileges, Size, &Size);
967 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
968 trace("TokenPrivileges:\n");
969 for (i = 0; i < Privileges->PrivilegeCount; i++)
972 DWORD NameLen = sizeof(Name)/sizeof(Name[0]);
973 LookupPrivilegeName(NULL, &Privileges->Privileges[i].Luid, Name, &NameLen);
974 trace("\t%s, 0x%x\n", Name, Privileges->Privileges[i].Attributes);
976 HeapFree(GetProcessHeap(), 0, Privileges);
978 ret = DuplicateToken(Token, SecurityAnonymous, &ImpersonationToken);
979 ok(ret, "DuplicateToken failed with error %d\n", GetLastError());
981 Size = sizeof(ImpersonationLevel);
982 ret = GetTokenInformation(ImpersonationToken, TokenImpersonationLevel, &ImpersonationLevel, Size, &Size);
983 ok(ret, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
984 ok(ImpersonationLevel == SecurityAnonymous, "ImpersonationLevel should have been SecurityAnonymous instead of %d\n", ImpersonationLevel);
986 CloseHandle(ImpersonationToken);
990 typedef union _MAX_SID
993 char max[SECURITY_MAX_SID_SIZE];
996 static void test_sid_str(PSID * sid)
999 BOOL ret = pConvertSidToStringSidA(sid, &str_sid);
1000 ok(ret, "ConvertSidToStringSidA() failed: %d\n", GetLastError());
1003 char account[MAX_PATH], domain[MAX_PATH];
1005 DWORD acc_size = MAX_PATH;
1006 DWORD dom_size = MAX_PATH;
1007 ret = LookupAccountSid(NULL, sid, account, &acc_size, domain, &dom_size, &use);
1008 ok(ret || (!ret && (GetLastError() == ERROR_NONE_MAPPED)),
1009 "LookupAccountSid(%s) failed: %d\n", str_sid, GetLastError());
1011 trace(" %s %s\\%s %d\n", str_sid, domain, account, use);
1012 else if (GetLastError() == ERROR_NONE_MAPPED)
1013 trace(" %s couldn't be mapped\n", str_sid);
1018 static void test_LookupAccountSid(void)
1020 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
1021 CHAR accountA[MAX_PATH], domainA[MAX_PATH];
1022 DWORD acc_sizeA, dom_sizeA;
1023 DWORD real_acc_sizeA, real_dom_sizeA;
1024 WCHAR accountW[MAX_PATH], domainW[MAX_PATH];
1025 DWORD acc_sizeW, dom_sizeW;
1026 DWORD real_acc_sizeW, real_dom_sizeW;
1027 PSID pUsersSid = NULL;
1035 /* native windows crashes if account size, domain size, or name use is NULL */
1037 ret = AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
1038 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &pUsersSid);
1039 ok(ret || (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED),
1040 "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1042 /* not running on NT so give up */
1043 if (!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1046 real_acc_sizeA = MAX_PATH;
1047 real_dom_sizeA = MAX_PATH;
1048 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &real_acc_sizeA, domainA, &real_dom_sizeA, &use);
1049 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1051 /* try NULL account */
1052 acc_sizeA = MAX_PATH;
1053 dom_sizeA = MAX_PATH;
1054 ret = LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
1055 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1057 /* try NULL domain */
1058 acc_sizeA = MAX_PATH;
1059 dom_sizeA = MAX_PATH;
1060 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
1061 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1063 /* try a small account buffer */
1065 dom_sizeA = MAX_PATH;
1067 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1068 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1069 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1070 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1072 /* try a 0 sized account buffer */
1074 dom_sizeA = MAX_PATH;
1076 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1077 /* this can fail or succeed depending on OS version but the size will always be returned */
1078 ok(acc_sizeA == real_acc_sizeA + 1,
1079 "LookupAccountSidA() Expected acc_size = %u, got %u\n",
1080 real_acc_sizeA + 1, acc_sizeA);
1082 /* try a 0 sized account buffer */
1084 dom_sizeA = MAX_PATH;
1085 ret = LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
1086 /* this can fail or succeed depending on OS version but the size will always be returned */
1087 ok(acc_sizeA == real_acc_sizeA + 1,
1088 "LookupAccountSid() Expected acc_size = %u, got %u\n",
1089 real_acc_sizeA + 1, acc_sizeA);
1091 /* try a small domain buffer */
1093 acc_sizeA = MAX_PATH;
1095 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1096 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1097 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1098 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1100 /* try a 0 sized domain buffer */
1102 acc_sizeA = MAX_PATH;
1104 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1105 /* this can fail or succeed depending on OS version but the size will always be returned */
1106 ok(dom_sizeA == real_dom_sizeA + 1,
1107 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1108 real_dom_sizeA + 1, dom_sizeA);
1110 /* try a 0 sized domain buffer */
1112 acc_sizeA = MAX_PATH;
1113 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
1114 /* this can fail or succeed depending on OS version but the size will always be returned */
1115 ok(dom_sizeA == real_dom_sizeA + 1,
1116 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1117 real_dom_sizeA + 1, dom_sizeA);
1119 real_acc_sizeW = MAX_PATH;
1120 real_dom_sizeW = MAX_PATH;
1121 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &real_acc_sizeW, domainW, &real_dom_sizeW, &use);
1122 ok(ret, "LookupAccountSidW() Expected TRUE, got FALSE\n");
1124 /* native windows crashes if domainW or accountW is NULL */
1126 /* try a small account buffer */
1128 dom_sizeW = MAX_PATH;
1130 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1131 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1132 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1133 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1135 /* try a 0 sized account buffer */
1137 dom_sizeW = MAX_PATH;
1139 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1140 /* this can fail or succeed depending on OS version but the size will always be returned */
1141 ok(acc_sizeW == real_acc_sizeW + 1,
1142 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1143 real_acc_sizeW + 1, acc_sizeW);
1145 /* try a 0 sized account buffer */
1147 dom_sizeW = MAX_PATH;
1148 ret = LookupAccountSidW(NULL, pUsersSid, NULL, &acc_sizeW, domainW, &dom_sizeW, &use);
1149 /* this can fail or succeed depending on OS version but the size will always be returned */
1150 ok(acc_sizeW == real_acc_sizeW + 1,
1151 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1152 real_acc_sizeW + 1, acc_sizeW);
1154 /* try a small domain buffer */
1156 acc_sizeW = MAX_PATH;
1158 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1159 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1160 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1161 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1163 /* try a 0 sized domain buffer */
1165 acc_sizeW = MAX_PATH;
1167 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1168 /* this can fail or succeed depending on OS version but the size will always be returned */
1169 ok(dom_sizeW == real_dom_sizeW + 1,
1170 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1171 real_dom_sizeW + 1, dom_sizeW);
1173 /* try a 0 sized domain buffer */
1175 acc_sizeW = MAX_PATH;
1176 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, NULL, &dom_sizeW, &use);
1177 /* this can fail or succeed depending on OS version but the size will always be returned */
1178 ok(dom_sizeW == real_dom_sizeW + 1,
1179 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1180 real_dom_sizeW + 1, dom_sizeW);
1184 pCreateWellKnownSid = (fnCreateWellKnownSid)GetProcAddress( hmod, "CreateWellKnownSid" );
1186 if (pCreateWellKnownSid && pConvertSidToStringSidA)
1188 trace("Well Known SIDs:\n");
1189 for (i = 0; i <= 60; i++)
1191 size = SECURITY_MAX_SID_SIZE;
1192 if (pCreateWellKnownSid(i, NULL, &max_sid.sid, &size))
1194 if (pConvertSidToStringSidA(&max_sid.sid, &str_sidA))
1196 acc_sizeA = MAX_PATH;
1197 dom_sizeA = MAX_PATH;
1198 if (LookupAccountSidA(NULL, &max_sid.sid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use))
1199 trace(" %d: %s %s\\%s %d\n", i, str_sidA, domainA, accountA, use);
1200 LocalFree(str_sidA);
1205 if (GetLastError() != ERROR_INVALID_PARAMETER)
1206 trace(" CreateWellKnownSid(%d) failed: %d\n", i, GetLastError());
1208 trace(" %d: not supported\n", i);
1212 pLsaQueryInformationPolicy = (fnLsaQueryInformationPolicy)GetProcAddress( hmod, "LsaQueryInformationPolicy");
1213 pLsaOpenPolicy = (fnLsaOpenPolicy)GetProcAddress( hmod, "LsaOpenPolicy");
1214 pLsaFreeMemory = (fnLsaFreeMemory)GetProcAddress( hmod, "LsaFreeMemory");
1215 pLsaClose = (fnLsaClose)GetProcAddress( hmod, "LsaClose");
1217 if (pLsaQueryInformationPolicy && pLsaOpenPolicy && pLsaFreeMemory && pLsaClose)
1221 LSA_OBJECT_ATTRIBUTES object_attributes;
1223 ZeroMemory(&object_attributes, sizeof(object_attributes));
1224 object_attributes.Length = sizeof(object_attributes);
1226 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_ALL_ACCESS, &handle);
1227 ok(status == STATUS_SUCCESS || status == STATUS_ACCESS_DENIED,
1228 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status);
1230 /* try a more restricted access mask if necessary */
1231 if (status == STATUS_ACCESS_DENIED) {
1232 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
1233 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_VIEW_LOCAL_INFORMATION, &handle);
1234 ok(status == STATUS_SUCCESS, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status);
1237 if (status == STATUS_SUCCESS)
1239 PPOLICY_ACCOUNT_DOMAIN_INFO info;
1240 status = pLsaQueryInformationPolicy(handle, PolicyAccountDomainInformation, (PVOID*)&info);
1241 ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy() failed, returned 0x%08x\n", status);
1242 if (status == STATUS_SUCCESS)
1244 ok(info->DomainSid!=0, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) missing SID\n");
1245 if (info->DomainSid)
1247 int count = *GetSidSubAuthorityCount(info->DomainSid);
1248 CopySid(GetSidLengthRequired(count), &max_sid, info->DomainSid);
1249 test_sid_str((PSID)&max_sid.sid);
1250 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_ADMIN;
1251 max_sid.sid.SubAuthorityCount = count + 1;
1252 test_sid_str((PSID)&max_sid.sid);
1253 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_GUEST;
1254 test_sid_str((PSID)&max_sid.sid);
1255 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ADMINS;
1256 test_sid_str((PSID)&max_sid.sid);
1257 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_USERS;
1258 test_sid_str((PSID)&max_sid.sid);
1259 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_GUESTS;
1260 test_sid_str((PSID)&max_sid.sid);
1261 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_COMPUTERS;
1262 test_sid_str((PSID)&max_sid.sid);
1263 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CONTROLLERS;
1264 test_sid_str((PSID)&max_sid.sid);
1265 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CERT_ADMINS;
1266 test_sid_str((PSID)&max_sid.sid);
1267 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_SCHEMA_ADMINS;
1268 test_sid_str((PSID)&max_sid.sid);
1269 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ENTERPRISE_ADMINS;
1270 test_sid_str((PSID)&max_sid.sid);
1271 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_POLICY_ADMINS;
1272 test_sid_str((PSID)&max_sid.sid);
1273 max_sid.sid.SubAuthority[count] = DOMAIN_ALIAS_RID_RAS_SERVERS;
1274 test_sid_str((PSID)&max_sid.sid);
1275 max_sid.sid.SubAuthority[count] = 1000; /* first user account */
1276 test_sid_str((PSID)&max_sid.sid);
1279 pLsaFreeMemory((LPVOID)info);
1282 status = pLsaClose(handle);
1283 ok(status == STATUS_SUCCESS, "LsaClose() failed, returned 0x%08x\n", status);
1289 static void get_sid_info(PSID psid, LPSTR *user, LPSTR *dom)
1291 static CHAR account[UNLEN + 1];
1292 static CHAR domain[UNLEN + 1];
1293 DWORD size, dom_size;
1299 size = dom_size = UNLEN + 1;
1302 LookupAccountSidA(NULL, psid, account, &size, domain, &dom_size, &use);
1305 static void test_LookupAccountName(void)
1307 DWORD sid_size, domain_size, user_size;
1308 DWORD sid_save, domain_save;
1309 CHAR user_name[UNLEN + 1];
1310 SID_NAME_USE sid_use;
1311 LPSTR domain, account, sid_dom;
1315 /* native crashes if (assuming all other parameters correct):
1317 * - Sid is NULL and cbSid is > 0
1318 * - cbSid or cchReferencedDomainName are NULL
1319 * - ReferencedDomainName is NULL and cchReferencedDomainName is the correct size
1322 user_size = UNLEN + 1;
1323 SetLastError(0xdeadbeef);
1324 ret = GetUserNameA(user_name, &user_size);
1325 if (!ret && (GetLastError() == ERROR_NOT_LOGGED_ON))
1327 /* Probably on win9x where the user used 'Cancel' instead of properly logging in */
1328 skip("Cannot get the user name (win9x and not logged in properly)\n");
1331 ok(ret, "Failed to get user name : %d\n", GetLastError());
1336 sid_use = 0xcafebabe;
1337 SetLastError(0xdeadbeef);
1338 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, NULL, &domain_size, &sid_use);
1339 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1341 skip("LookupAccountNameA is not implemented\n");
1344 ok(!ret, "Expected 0, got %d\n", ret);
1345 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1346 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1347 ok(sid_size != 0, "Expected non-zero sid size\n");
1348 ok(domain_size != 0, "Expected non-zero domain size\n");
1349 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
1351 sid_save = sid_size;
1352 domain_save = domain_size;
1354 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
1355 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
1357 /* try valid account name */
1358 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, domain, &domain_size, &sid_use);
1359 get_sid_info(psid, &account, &sid_dom);
1360 ok(ret, "Failed to lookup account name\n");
1361 ok(sid_size == GetLengthSid(psid), "Expected %d, got %d\n", GetLengthSid(psid), sid_size);
1364 ok(!lstrcmp(account, user_name), "Expected %s, got %s\n", user_name, account);
1365 ok(!lstrcmp(domain, sid_dom), "Expected %s, got %s\n", sid_dom, domain);
1366 ok(domain_size == domain_save - 1, "Expected %d, got %d\n", domain_save - 1, domain_size);
1367 ok(lstrlen(domain) == domain_size, "Expected %d\n", lstrlen(domain));
1368 ok(sid_use == SidTypeUser, "Expected SidTypeUser, got %d\n", SidTypeUser);
1370 domain_size = domain_save;
1372 /* NULL Sid with zero sid size */
1373 SetLastError(0xdeadbeef);
1375 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
1376 ok(!ret, "Expected 0, got %d\n", ret);
1377 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1378 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1379 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1380 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1382 /* try cchReferencedDomainName - 1 */
1383 SetLastError(0xdeadbeef);
1385 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
1386 ok(!ret, "Expected 0, got %d\n", ret);
1387 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1388 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1389 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1390 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1392 /* NULL ReferencedDomainName with zero domain name size */
1393 SetLastError(0xdeadbeef);
1395 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, NULL, &domain_size, &sid_use);
1396 ok(!ret, "Expected 0, got %d\n", ret);
1397 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1398 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1399 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1400 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1402 HeapFree(GetProcessHeap(), 0, psid);
1403 HeapFree(GetProcessHeap(), 0, domain);
1405 /* get sizes for NULL account name */
1408 sid_use = 0xcafebabe;
1409 SetLastError(0xdeadbeef);
1410 ret = LookupAccountNameA(NULL, NULL, NULL, &sid_size, NULL, &domain_size, &sid_use);
1411 ok(!ret, "Expected 0, got %d\n", ret);
1412 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1413 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1414 ok(sid_size != 0, "Expected non-zero sid size\n");
1415 ok(domain_size != 0, "Expected non-zero domain size\n");
1416 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
1418 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
1419 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
1421 /* try NULL account name */
1422 ret = LookupAccountNameA(NULL, NULL, psid, &sid_size, domain, &domain_size, &sid_use);
1423 get_sid_info(psid, &account, &sid_dom);
1424 ok(ret, "Failed to lookup account name\n");
1427 /* Using a fixed string will not work on different locales */
1428 ok(!lstrcmp(account, domain),
1429 "Got %s for account and %s for domain, these should be the same\n",
1431 ok(sid_use == SidTypeDomain, "Expected SidTypeDomain, got %d\n", SidTypeDomain);
1434 /* try an invalid account name */
1435 SetLastError(0xdeadbeef);
1438 ret = LookupAccountNameA(NULL, "oogabooga", NULL, &sid_size, NULL, &domain_size, &sid_use);
1439 ok(!ret, "Expected 0, got %d\n", ret);
1442 ok(GetLastError() == ERROR_NONE_MAPPED,
1443 "Expected ERROR_NONE_MAPPED, got %d\n", GetLastError());
1444 ok(sid_size == 0, "Expected 0, got %d\n", sid_size);
1445 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
1448 HeapFree(GetProcessHeap(), 0, psid);
1449 HeapFree(GetProcessHeap(), 0, domain);
1452 static void test_security_descriptor(void)
1454 SECURITY_DESCRIPTOR sd;
1457 BOOL isDefault, isPresent;
1461 InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
1462 ok(GetSecurityDescriptorOwner(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
1463 expect_eq(psid, NULL, PSID, "%p");
1464 expect_eq(isDefault, FALSE, BOOL, "%d");
1465 sd.Control |= SE_DACL_PRESENT | SE_SACL_PRESENT;
1467 SetLastError(0xdeadbeef);
1469 expect_eq(MakeSelfRelativeSD(&sd, buf, &size), FALSE, BOOL, "%d");
1470 expect_eq(GetLastError(), ERROR_INSUFFICIENT_BUFFER, DWORD, "%u");
1471 ok(size > 5, "Size not increased\n");
1474 expect_eq(MakeSelfRelativeSD(&sd, buf, &size), TRUE, BOOL, "%d");
1475 ok(GetSecurityDescriptorOwner(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
1476 expect_eq(psid, NULL, PSID, "%p");
1477 expect_eq(isDefault, FALSE, BOOL, "%d");
1478 ok(GetSecurityDescriptorGroup(&sd, &psid, &isDefault), "GetSecurityDescriptorOwner failed\n");
1479 expect_eq(psid, NULL, PSID, "%p");
1480 expect_eq(isDefault, FALSE, BOOL, "%d");
1481 ok(GetSecurityDescriptorDacl(&sd, &isPresent, &pacl, &isDefault), "GetSecurityDescriptorOwner failed\n");
1482 expect_eq(isPresent, TRUE, BOOL, "%d");
1483 expect_eq(psid, NULL, PSID, "%p");
1484 expect_eq(isDefault, FALSE, BOOL, "%d");
1485 ok(GetSecurityDescriptorSacl(&sd, &isPresent, &pacl, &isDefault), "GetSecurityDescriptorOwner failed\n");
1486 expect_eq(isPresent, TRUE, BOOL, "%d");
1487 expect_eq(psid, NULL, PSID, "%p");
1488 expect_eq(isDefault, FALSE, BOOL, "%d");
1492 #define TEST_GRANTED_ACCESS(a,b) test_granted_access(a,b,__LINE__)
1493 static void test_granted_access(HANDLE handle, ACCESS_MASK access, int line)
1495 OBJECT_BASIC_INFORMATION obj_info;
1498 if (!pNtQueryObject)
1500 skip_(__FILE__, line)("Not NT platform - skipping tests\n");
1504 status = pNtQueryObject( handle, ObjectBasicInformation, &obj_info,
1505 sizeof(obj_info), NULL );
1506 ok_(__FILE__, line)(!status, "NtQueryObject with err: %08x\n", status);
1507 ok_(__FILE__, line)(obj_info.GrantedAccess == access, "Granted access should "
1508 "be 0x%08x, instead of 0x%08x\n", access, obj_info.GrantedAccess);
1511 #define CHECK_SET_SECURITY(o,i,e) \
1515 SetLastError( 0xdeadbeef ); \
1516 res = SetKernelObjectSecurity( o, i, SecurityDescriptor ); \
1517 err = GetLastError(); \
1518 if (e == ERROR_SUCCESS) \
1519 ok(res, "SetKernelObjectSecurity failed with %d\n", err); \
1521 ok(!res && err == e, "SetKernelObjectSecurity should have failed " \
1522 "with %s, instead of %d\n", #e, err); \
1525 static void test_process_security(void)
1528 char owner[32], group[32];
1529 PSID AdminSid = NULL, UsersSid = NULL;
1531 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL;
1532 char buffer[MAX_PATH];
1533 PROCESS_INFORMATION info;
1534 STARTUPINFOA startup;
1535 SECURITY_ATTRIBUTES psa;
1536 HANDLE token, event;
1539 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
1540 res = InitializeAcl(Acl, 256, ACL_REVISION);
1541 if (!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1543 skip("ACLs not implemented - skipping tests\n");
1544 HeapFree(GetProcessHeap(), 0, Acl);
1547 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
1549 /* get owner from the token we might be running as a user not admin */
1550 res = OpenProcessToken( GetCurrentProcess(), MAXIMUM_ALLOWED, &token );
1551 ok(res, "OpenProcessToken failed with error %d\n", GetLastError());
1554 HeapFree(GetProcessHeap(), 0, Acl);
1558 res = GetTokenInformation( token, TokenOwner, owner, sizeof(owner), &tmp );
1559 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
1560 AdminSid = ((TOKEN_OWNER*)owner)->Owner;
1561 res = GetTokenInformation( token, TokenPrimaryGroup, group, sizeof(group), &tmp );
1562 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
1563 UsersSid = ((TOKEN_PRIMARY_GROUP*)group)->PrimaryGroup;
1565 CloseHandle( token );
1568 HeapFree(GetProcessHeap(), 0, Acl);
1572 res = AddAccessDeniedAce(Acl, ACL_REVISION, PROCESS_VM_READ, AdminSid);
1573 ok(res, "AddAccessDeniedAce failed with error %d\n", GetLastError());
1574 res = AddAccessAllowedAce(Acl, ACL_REVISION, PROCESS_ALL_ACCESS, AdminSid);
1575 ok(res, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
1577 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
1578 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
1579 ok(res, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
1581 event = CreateEvent( NULL, TRUE, TRUE, "test_event" );
1582 ok(event != NULL, "CreateEvent %d\n", GetLastError());
1584 SecurityDescriptor->Revision = 0;
1585 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_UNKNOWN_REVISION );
1586 SecurityDescriptor->Revision = SECURITY_DESCRIPTOR_REVISION;
1588 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_INVALID_SECURITY_DESCR );
1589 CHECK_SET_SECURITY( event, GROUP_SECURITY_INFORMATION, ERROR_INVALID_SECURITY_DESCR );
1590 CHECK_SET_SECURITY( event, SACL_SECURITY_INFORMATION, ERROR_ACCESS_DENIED );
1591 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
1592 /* NULL DACL is valid and means default DACL from token */
1593 SecurityDescriptor->Control |= SE_DACL_PRESENT;
1594 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
1596 /* Set owner and group and dacl */
1597 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
1598 ok(res, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
1599 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_SUCCESS );
1600 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, FALSE);
1601 ok(res, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
1602 CHECK_SET_SECURITY( event, GROUP_SECURITY_INFORMATION, ERROR_SUCCESS );
1603 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
1604 ok(res, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1605 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
1607 sprintf(buffer, "%s tests/security.c test", myARGV[0]);
1608 memset(&startup, 0, sizeof(startup));
1609 startup.cb = sizeof(startup);
1610 startup.dwFlags = STARTF_USESHOWWINDOW;
1611 startup.wShowWindow = SW_SHOWNORMAL;
1613 psa.nLength = sizeof(psa);
1614 psa.lpSecurityDescriptor = SecurityDescriptor;
1615 psa.bInheritHandle = TRUE;
1617 /* Doesn't matter what ACL say we should get full access for ourselves */
1618 ok(CreateProcessA( NULL, buffer, &psa, NULL, FALSE, 0, NULL, NULL, &startup, &info ),
1619 "CreateProcess with err:%d\n", GetLastError());
1620 TEST_GRANTED_ACCESS( info.hProcess, PROCESS_ALL_ACCESS );
1621 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1623 CloseHandle( event );
1624 HeapFree(GetProcessHeap(), 0, Acl);
1625 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
1628 static void test_process_security_child(void)
1630 HANDLE handle, handle1;
1634 handle = OpenProcess( PROCESS_TERMINATE, FALSE, GetCurrentProcessId() );
1635 ok(handle != NULL, "OpenProcess(PROCESS_TERMINATE) with err:%d\n", GetLastError());
1636 TEST_GRANTED_ACCESS( handle, PROCESS_TERMINATE );
1638 ok(DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
1639 &handle1, 0, TRUE, DUPLICATE_SAME_ACCESS ),
1640 "duplicating handle err:%d\n", GetLastError());
1641 TEST_GRANTED_ACCESS( handle1, PROCESS_TERMINATE );
1643 CloseHandle( handle1 );
1645 SetLastError( 0xdeadbeef );
1646 ret = DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
1647 &handle1, PROCESS_ALL_ACCESS, TRUE, 0 );
1648 err = GetLastError();
1650 ok(!ret && err == ERROR_ACCESS_DENIED, "duplicating handle should have failed "
1651 "with STATUS_ACCESS_DENIED, instead of err:%d\n", err);
1653 CloseHandle( handle );
1655 /* These two should fail - they are denied by ACL */
1656 handle = OpenProcess( PROCESS_VM_READ, FALSE, GetCurrentProcessId() );
1658 ok(handle == NULL, "OpenProcess(PROCESS_VM_READ) should have failed\n");
1659 handle = OpenProcess( PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId() );
1661 ok(handle == NULL, "OpenProcess(PROCESS_ALL_ACCESS) should have failed\n");
1663 /* Documented privilege elevation */
1664 ok(DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
1665 &handle, 0, TRUE, DUPLICATE_SAME_ACCESS ),
1666 "duplicating handle err:%d\n", GetLastError());
1667 TEST_GRANTED_ACCESS( handle, PROCESS_ALL_ACCESS );
1669 CloseHandle( handle );
1671 /* Same only explicitly asking for all access rights */
1672 ok(DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
1673 &handle, PROCESS_ALL_ACCESS, TRUE, 0 ),
1674 "duplicating handle err:%d\n", GetLastError());
1675 TEST_GRANTED_ACCESS( handle, PROCESS_ALL_ACCESS );
1676 ok(DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
1677 &handle1, PROCESS_VM_READ, TRUE, 0 ),
1678 "duplicating handle err:%d\n", GetLastError());
1679 TEST_GRANTED_ACCESS( handle1, PROCESS_VM_READ );
1680 CloseHandle( handle1 );
1681 CloseHandle( handle );
1684 static void test_impersonation_level(void)
1686 HANDLE Token, ProcessToken;
1689 TOKEN_PRIVILEGES *Privileges;
1691 PRIVILEGE_SET *PrivilegeSet;
1697 pDuplicateTokenEx = (fnDuplicateTokenEx) GetProcAddress(hmod, "DuplicateTokenEx");
1698 if( !pDuplicateTokenEx ) {
1699 skip("DuplicateTokenEx is not available\n");
1702 SetLastError(0xdeadbeef);
1703 ret = ImpersonateSelf(SecurityAnonymous);
1704 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1706 skip("ImpersonateSelf is not implemented\n");
1709 ok(ret, "ImpersonateSelf(SecurityAnonymous) failed with error %d\n", GetLastError());
1710 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
1711 ok(!ret, "OpenThreadToken should have failed\n");
1712 error = GetLastError();
1713 ok(error == ERROR_CANT_OPEN_ANONYMOUS, "OpenThreadToken on anonymous token should have returned ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error);
1714 /* can't perform access check when opening object against an anonymous impersonation token */
1716 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
1717 ok(error == ERROR_INVALID_HANDLE, "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE instead of %d\n", error);
1721 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, &ProcessToken);
1722 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1724 ret = pDuplicateTokenEx(ProcessToken,
1725 TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_IMPERSONATE, NULL,
1726 SecurityAnonymous, TokenImpersonation, &Token);
1727 ok(ret, "DuplicateTokenEx failed with error %d\n", GetLastError());
1728 /* can't increase the impersonation level */
1729 ret = DuplicateToken(Token, SecurityIdentification, &Token2);
1730 error = GetLastError();
1731 ok(!ret && error == ERROR_BAD_IMPERSONATION_LEVEL,
1732 "Duplicating a token and increasing the impersonation level should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
1733 /* we can query anything from an anonymous token, including the user */
1734 ret = GetTokenInformation(Token, TokenUser, NULL, 0, &Size);
1735 error = GetLastError();
1736 ok(!ret && error == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenUser) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error);
1737 User = (TOKEN_USER *)HeapAlloc(GetProcessHeap(), 0, Size);
1738 ret = GetTokenInformation(Token, TokenUser, User, Size, &Size);
1739 ok(ret, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1740 HeapFree(GetProcessHeap(), 0, User);
1742 /* PrivilegeCheck fails with SecurityAnonymous level */
1743 ret = GetTokenInformation(Token, TokenPrivileges, NULL, 0, &Size);
1744 error = GetLastError();
1745 ok(!ret && error == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenPrivileges) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error);
1746 Privileges = (TOKEN_PRIVILEGES *)HeapAlloc(GetProcessHeap(), 0, Size);
1747 ret = GetTokenInformation(Token, TokenPrivileges, Privileges, Size, &Size);
1748 ok(ret, "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1750 PrivilegeSet = (PRIVILEGE_SET *)HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PRIVILEGE_SET, Privilege[Privileges->PrivilegeCount]));
1751 PrivilegeSet->PrivilegeCount = Privileges->PrivilegeCount;
1752 memcpy(PrivilegeSet->Privilege, Privileges->Privileges, PrivilegeSet->PrivilegeCount * sizeof(PrivilegeSet->Privilege[0]));
1753 PrivilegeSet->Control = PRIVILEGE_SET_ALL_NECESSARY;
1754 HeapFree(GetProcessHeap(), 0, Privileges);
1756 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
1757 error = GetLastError();
1758 ok(!ret && error == ERROR_BAD_IMPERSONATION_LEVEL, "PrivilegeCheck for SecurityAnonymous token should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
1762 ret = ImpersonateSelf(SecurityIdentification);
1763 ok(ret, "ImpersonateSelf(SecurityIdentification) failed with error %d\n", GetLastError());
1764 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
1765 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
1767 /* can't perform access check when opening object against an identification impersonation token */
1768 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
1770 ok(error == ERROR_INVALID_HANDLE, "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE instead of %d\n", error);
1772 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
1773 ok(ret, "PrivilegeCheck for SecurityIdentification failed with error %d\n", GetLastError());
1777 ret = ImpersonateSelf(SecurityImpersonation);
1778 ok(ret, "ImpersonateSelf(SecurityImpersonation) failed with error %d\n", GetLastError());
1779 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
1780 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
1781 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
1782 ok(error == ERROR_SUCCESS, "RegOpenKeyEx should have succeeded instead of failing with %d\n", error);
1784 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
1785 ok(ret, "PrivilegeCheck for SecurityImpersonation failed with error %d\n", GetLastError());
1789 CloseHandle(ProcessToken);
1791 HeapFree(GetProcessHeap(), 0, PrivilegeSet);
1794 static void test_SetEntriesInAcl(void)
1796 ACL *acl = (ACL*)0xdeadbeef;
1799 if (!pSetEntriesInAclW)
1801 skip("SetEntriesInAclW is not available\n");
1805 res = pSetEntriesInAclW(0, NULL, NULL, &acl);
1806 if(res == ERROR_CALL_NOT_IMPLEMENTED)
1808 skip("SetEntriesInAclW is not implemented\n");
1811 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
1812 ok(acl == NULL, "acl=%p, expected NULL\n", acl);
1815 static void test_GetNamedSecurityInfoA(void)
1817 PSECURITY_DESCRIPTOR pSecDesc;
1819 SECURITY_DESCRIPTOR_CONTROL control;
1822 BOOL owner_defaulted;
1823 BOOL group_defaulted;
1826 CHAR windows_dir[MAX_PATH];
1828 if (!pGetNamedSecurityInfoA)
1830 skip("GetNamedSecurityInfoA is not available\n");
1834 ret = GetWindowsDirectoryA(windows_dir, MAX_PATH);
1835 ok(ret, "GetWindowsDirectory failed with error %d\n", GetLastError());
1837 SetLastError(0xdeadbeef);
1838 error = pGetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,
1839 OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION,
1840 NULL, NULL, NULL, NULL, &pSecDesc);
1841 if (error != ERROR_SUCCESS && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1843 skip("GetNamedSecurityInfoA is not implemented\n");
1846 ok(!error, "GetNamedSecurityInfo failed with error %d\n", error);
1848 ret = GetSecurityDescriptorControl(pSecDesc, &control, &revision);
1849 ok(ret, "GetSecurityDescriptorControl failed with error %d\n", GetLastError());
1850 ok((control & (SE_SELF_RELATIVE|SE_DACL_PRESENT)) == (SE_SELF_RELATIVE|SE_DACL_PRESENT),
1851 "control (0x%x) doesn't have (SE_SELF_RELATIVE|SE_DACL_PRESENT) flags set\n", control);
1852 ok(revision == SECURITY_DESCRIPTOR_REVISION1, "revision was %d instead of 1\n", revision);
1853 ret = GetSecurityDescriptorOwner(pSecDesc, &owner, &owner_defaulted);
1854 ok(ret, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
1855 ok(owner != NULL, "owner should not be NULL\n");
1856 ret = GetSecurityDescriptorGroup(pSecDesc, &group, &group_defaulted);
1857 ok(ret, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
1858 ok(group != NULL, "group should not be NULL\n");
1861 static void test_ConvertStringSecurityDescriptor(void)
1864 PSECURITY_DESCRIPTOR pSD;
1866 if (!pConvertStringSecurityDescriptorToSecurityDescriptorA)
1868 skip("ConvertStringSecurityDescriptorToSecurityDescriptor is not available\n");
1872 SetLastError(0xdeadbeef);
1873 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1874 "D:(A;;GA;;;WD)", 0xdeadbeef, &pSD, NULL);
1875 ok(!ret && GetLastError() == ERROR_UNKNOWN_REVISION,
1876 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_UNKNOWN_REVISION instead of %d\n",
1879 /* test ACE string type */
1880 SetLastError(0xdeadbeef);
1881 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1882 "D:(A;;GA;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1883 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1886 SetLastError(0xdeadbeef);
1887 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1888 "D:(D;;GA;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1889 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1892 SetLastError(0xdeadbeef);
1893 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1894 "ERROR:(D;;GA;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1895 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
1896 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
1899 /* test ACE string access rights */
1900 SetLastError(0xdeadbeef);
1901 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1902 "D:(A;;GA;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1903 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1905 SetLastError(0xdeadbeef);
1906 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1907 "D:(A;;GRGWGX;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1908 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1910 SetLastError(0xdeadbeef);
1911 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1912 "D:(A;;RCSDWDWO;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1913 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1915 SetLastError(0xdeadbeef);
1916 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1917 "D:(A;;RPWPCCDCLCSWLODTCR;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1918 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1920 SetLastError(0xdeadbeef);
1921 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1922 "D:(A;;FAFRFWFX;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1923 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1925 SetLastError(0xdeadbeef);
1926 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1927 "D:(A;;KAKRKWKX;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1928 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1930 SetLastError(0xdeadbeef);
1931 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1932 "D:(A;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1933 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1935 SetLastError(0xdeadbeef);
1936 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1937 "S:(AU;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1938 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1941 /* test ACE string access right error case */
1942 SetLastError(0xdeadbeef);
1943 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1944 "D:(A;;ROB;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1946 ok(!ret && GetLastError() == ERROR_INVALID_ACL,
1947 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_ACL instead of %d\n",
1950 /* test ACE string SID */
1951 SetLastError(0xdeadbeef);
1952 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1953 "D:(D;;GA;;;S-1-0-0)", SDDL_REVISION_1, &pSD, NULL);
1954 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1957 SetLastError(0xdeadbeef);
1958 ret = pConvertStringSecurityDescriptorToSecurityDescriptorA(
1959 "D:(D;;GA;;;Nonexistent account)", SDDL_REVISION_1, &pSD, NULL);
1961 ok(!ret && GetLastError() == ERROR_INVALID_ACL,
1962 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_ACL instead of %d\n",
1966 static void test_ConvertSecurityDescriptorToString()
1968 SECURITY_DESCRIPTOR desc;
1969 SECURITY_INFORMATION sec_info = OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION|SACL_SECURITY_INFORMATION;
1978 if (!pConvertSecurityDescriptorToStringSecurityDescriptorA)
1980 skip("ConvertSecurityDescriptorToStringSecurityDescriptor is not available\n");
1984 /* It seems Windows XP add an extra character to the length of the string for each ACE in an ACL. We
1985 * don't replicate this feature so we only test len >= strlen+1. */
1986 #define CHECK_RESULT_AND_FREE(exp_str) \
1987 ok(strcmp(string, (exp_str)) == 0, "String mismatch (expected \"%s\", got \"%s\")\n", (exp_str), string); \
1988 ok(len >= (strlen(exp_str) + 1), "Length mismatch (expected %d, got %d)\n", strlen(exp_str) + 1, len); \
1991 InitializeSecurityDescriptor(&desc, SECURITY_DESCRIPTOR_REVISION);
1992 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
1993 CHECK_RESULT_AND_FREE("");
1996 CreateWellKnownSid(WinLocalSid, NULL, sid_buf, &size);
1997 SetSecurityDescriptorOwner(&desc, (PSID)sid_buf, FALSE);
1998 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
1999 CHECK_RESULT_AND_FREE("O:S-1-2-0");
2001 SetSecurityDescriptorOwner(&desc, (PSID)sid_buf, TRUE);
2002 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2003 CHECK_RESULT_AND_FREE("O:S-1-2-0");
2005 size = sizeof(sid_buf);
2006 CreateWellKnownSid(WinLocalSystemSid, NULL, sid_buf, &size);
2007 SetSecurityDescriptorOwner(&desc, (PSID)sid_buf, TRUE);
2008 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2009 CHECK_RESULT_AND_FREE("O:SY");
2011 ConvertStringSidToSid("S-1-5-21-93476-23408-4576", &psid);
2012 SetSecurityDescriptorGroup(&desc, psid, TRUE);
2013 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2014 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576");
2016 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, GROUP_SECURITY_INFORMATION, &string, &len), "Conversion failed\n");
2017 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
2019 pacl = (PACL)acl_buf;
2020 InitializeAcl(pacl, sizeof(acl_buf), ACL_REVISION);
2021 SetSecurityDescriptorDacl(&desc, TRUE, pacl, TRUE);
2022 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2023 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
2025 SetSecurityDescriptorDacl(&desc, TRUE, pacl, FALSE);
2026 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2027 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:");
2029 ConvertStringSidToSid("S-1-5-6", &psid2);
2030 AddAccessAllowedAceEx(pacl, ACL_REVISION, NO_PROPAGATE_INHERIT_ACE, 0xf0000000, psid2);
2031 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2032 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)");
2034 AddAccessAllowedAceEx(pacl, ACL_REVISION, INHERIT_ONLY_ACE|INHERITED_ACE, 0x00000003, psid2);
2035 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2036 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)");
2038 AddAccessDeniedAceEx(pacl, ACL_REVISION, OBJECT_INHERIT_ACE|CONTAINER_INHERIT_ACE, 0xffffffff, psid);
2039 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2040 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)");
2043 pacl = (PACL)acl_buf;
2044 InitializeAcl(pacl, sizeof(acl_buf), ACL_REVISION);
2045 SetSecurityDescriptorSacl(&desc, TRUE, pacl, FALSE);
2046 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2047 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:");
2049 SetSecurityDescriptorDacl(&desc, TRUE, NULL, FALSE);
2050 AddAuditAccessAceEx(pacl, ACL_REVISION, VALID_INHERIT_FLAGS, KEY_READ|KEY_WRITE, psid2, TRUE, TRUE);
2051 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2052 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)");
2054 AddAuditAccessAceEx(pacl, ACL_REVISION, NO_PROPAGATE_INHERIT_ACE, FILE_GENERIC_READ|FILE_GENERIC_WRITE, psid2, TRUE, FALSE);
2055 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(&desc, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2056 CHECK_RESULT_AND_FREE("O:SYG:S-1-5-21-93476-23408-4576D:S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)");
2059 static void test_PrivateObjectSecurity(void)
2061 SECURITY_INFORMATION sec_info = OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION|SACL_SECURITY_INFORMATION;
2062 SECURITY_DESCRIPTOR_CONTROL ctrl;
2063 PSECURITY_DESCRIPTOR sec;
2069 PSECURITY_DESCRIPTOR buf;
2071 ok(ConvertStringSecurityDescriptorToSecurityDescriptorA(
2073 "G:S-1-5-21-93476-23408-4576"
2074 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
2075 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)", SDDL_REVISION_1, &sec, &dwDescSize), "Creating descriptor failed\n");
2076 buf = HeapAlloc(GetProcessHeap(), 0, dwDescSize);
2077 SetSecurityDescriptorControl(sec, SE_DACL_PROTECTED, SE_DACL_PROTECTED);
2078 GetSecurityDescriptorControl(sec, &ctrl, &dwRevision);
2079 todo_wine expect_eq(ctrl, 0x9014, int, "%x");
2081 ok(GetPrivateObjectSecurity(sec, GROUP_SECURITY_INFORMATION, buf, dwDescSize, &retSize),
2082 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
2083 ok(retSize <= dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
2084 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2085 CHECK_RESULT_AND_FREE("G:S-1-5-21-93476-23408-4576");
2086 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
2087 expect_eq(ctrl, 0x8000, int, "%x");
2089 ok(GetPrivateObjectSecurity(sec, GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION, buf, dwDescSize, &retSize),
2090 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
2091 ok(retSize <= dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
2092 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed err=%u\n", GetLastError());
2093 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)");
2094 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
2095 expect_eq(ctrl, 0x8004, int, "%x");
2097 ok(GetPrivateObjectSecurity(sec, sec_info, buf, dwDescSize, &retSize),
2098 "GetPrivateObjectSecurity failed (err=%u)\n", GetLastError());
2099 ok(retSize == dwDescSize, "Buffer too small (%d vs %d)\n", retSize, dwDescSize);
2100 ok(pConvertSecurityDescriptorToStringSecurityDescriptorA(buf, SDDL_REVISION_1, sec_info, &string, &len), "Conversion failed\n");
2101 CHECK_RESULT_AND_FREE("O:SY"
2102 "G:S-1-5-21-93476-23408-4576"
2103 "D:(A;NP;GAGXGWGR;;;SU)(A;IOID;CCDC;;;SU)(D;OICI;0xffffffff;;;S-1-5-21-93476-23408-4576)"
2104 "S:(AU;OICINPIOIDSAFA;CCDCLCSWRPRC;;;SU)(AU;NPSA;0x12019f;;;SU)");
2105 GetSecurityDescriptorControl(buf, &ctrl, &dwRevision);
2106 expect_eq(ctrl, 0x8014, int, "%x");
2108 SetLastError(0xdeadbeef);
2109 ok(GetPrivateObjectSecurity(sec, sec_info, buf, 5, &retSize) == FALSE, "GetPrivateObjectSecurity should have failed\n");
2110 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected error ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
2113 HeapFree(GetProcessHeap(), 0, buf);
2115 #undef CHECK_RESULT_AND_FREE
2117 START_TEST(security)
2124 test_process_security_child();
2130 test_FileSecurity();
2133 test_LookupAccountSid();
2134 test_LookupAccountName();
2135 test_security_descriptor();
2136 test_process_security();
2137 test_impersonation_level();
2138 test_SetEntriesInAcl();
2139 test_GetNamedSecurityInfoA();
2140 test_ConvertStringSecurityDescriptor();
2141 test_ConvertSecurityDescriptorToString();
2142 test_PrivateObjectSecurity();