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 typedef VOID (WINAPI *fnBuildTrusteeWithSidA)( PTRUSTEEA pTrustee, PSID pSid );
39 typedef VOID (WINAPI *fnBuildTrusteeWithNameA)( PTRUSTEEA pTrustee, LPSTR pName );
40 typedef VOID (WINAPI *fnBuildTrusteeWithObjectsAndNameA)( PTRUSTEEA pTrustee,
41 POBJECTS_AND_NAME_A pObjName,
42 SE_OBJECT_TYPE ObjectType,
44 LPSTR InheritedObjectTypeName,
46 typedef VOID (WINAPI *fnBuildTrusteeWithObjectsAndSidA)( PTRUSTEEA pTrustee,
47 POBJECTS_AND_SID pObjSid,
49 GUID* pInheritedObjectGuid,
51 typedef LPSTR (WINAPI *fnGetTrusteeNameA)( PTRUSTEEA pTrustee );
52 typedef BOOL (WINAPI *fnConvertSidToStringSidA)( PSID pSid, LPSTR *str );
53 typedef BOOL (WINAPI *fnConvertStringSidToSidA)( LPCSTR str, PSID pSid );
54 typedef BOOL (WINAPI *fnGetFileSecurityA)(LPCSTR, SECURITY_INFORMATION,
55 PSECURITY_DESCRIPTOR, DWORD, LPDWORD);
56 typedef DWORD (WINAPI *fnRtlAdjustPrivilege)(ULONG,BOOLEAN,BOOLEAN,PBOOLEAN);
57 typedef BOOL (WINAPI *fnCreateWellKnownSid)(WELL_KNOWN_SID_TYPE,PSID,PSID,DWORD*);
58 typedef BOOL (WINAPI *fnDuplicateTokenEx)(HANDLE,DWORD,LPSECURITY_ATTRIBUTES,
59 SECURITY_IMPERSONATION_LEVEL,TOKEN_TYPE,PHANDLE);
61 typedef NTSTATUS (WINAPI *fnLsaQueryInformationPolicy)(LSA_HANDLE,POLICY_INFORMATION_CLASS,PVOID*);
62 typedef NTSTATUS (WINAPI *fnLsaClose)(LSA_HANDLE);
63 typedef NTSTATUS (WINAPI *fnLsaFreeMemory)(PVOID);
64 typedef NTSTATUS (WINAPI *fnLsaOpenPolicy)(PLSA_UNICODE_STRING,PLSA_OBJECT_ATTRIBUTES,ACCESS_MASK,PLSA_HANDLE);
65 static NTSTATUS (WINAPI *pNtQueryObject)(HANDLE,OBJECT_INFORMATION_CLASS,PVOID,ULONG,PULONG);
71 fnBuildTrusteeWithSidA pBuildTrusteeWithSidA;
72 fnBuildTrusteeWithNameA pBuildTrusteeWithNameA;
73 fnBuildTrusteeWithObjectsAndNameA pBuildTrusteeWithObjectsAndNameA;
74 fnBuildTrusteeWithObjectsAndSidA pBuildTrusteeWithObjectsAndSidA;
75 fnGetTrusteeNameA pGetTrusteeNameA;
76 fnConvertSidToStringSidA pConvertSidToStringSidA;
77 fnConvertStringSidToSidA pConvertStringSidToSidA;
78 fnGetFileSecurityA pGetFileSecurityA;
79 fnRtlAdjustPrivilege pRtlAdjustPrivilege;
80 fnCreateWellKnownSid pCreateWellKnownSid;
81 fnDuplicateTokenEx pDuplicateTokenEx;
82 fnLsaQueryInformationPolicy pLsaQueryInformationPolicy;
84 fnLsaFreeMemory pLsaFreeMemory;
85 fnLsaOpenPolicy pLsaOpenPolicy;
89 SID_IDENTIFIER_AUTHORITY auth;
93 static void init(void)
95 HMODULE hntdll = GetModuleHandleA("ntdll.dll");
97 hmod = GetModuleHandle("advapi32.dll");
98 myARGC = winetest_get_mainargs( &myARGV );
100 pNtQueryObject = (void *)GetProcAddress( hntdll, "NtQueryObject" );
103 static void test_str_sid(const char *str_sid)
108 if (pConvertStringSidToSidA(str_sid, &psid))
110 if (pConvertSidToStringSidA(psid, &temp))
112 trace(" %s: %s\n", str_sid, temp);
119 if (GetLastError() != ERROR_INVALID_SID)
120 trace(" %s: couldn't be converted, returned %d\n", str_sid, GetLastError());
122 trace(" %s: couldn't be converted\n", str_sid);
126 static void test_sid(void)
128 struct sidRef refs[] = {
129 { { {0x00,0x00,0x33,0x44,0x55,0x66} }, "S-1-860116326-1" },
130 { { {0x00,0x00,0x01,0x02,0x03,0x04} }, "S-1-16909060-1" },
131 { { {0x00,0x00,0x00,0x01,0x02,0x03} }, "S-1-66051-1" },
132 { { {0x00,0x00,0x00,0x00,0x01,0x02} }, "S-1-258-1" },
133 { { {0x00,0x00,0x00,0x00,0x00,0x02} }, "S-1-2-1" },
134 { { {0x00,0x00,0x00,0x00,0x00,0x0c} }, "S-1-12-1" },
136 const char noSubAuthStr[] = "S-1-5";
142 pConvertSidToStringSidA = (fnConvertSidToStringSidA)
143 GetProcAddress( hmod, "ConvertSidToStringSidA" );
144 if( !pConvertSidToStringSidA )
146 pConvertStringSidToSidA = (fnConvertStringSidToSidA)
147 GetProcAddress( hmod, "ConvertStringSidToSidA" );
148 if( !pConvertStringSidToSidA )
151 r = pConvertStringSidToSidA( NULL, NULL );
152 ok( !r, "expected failure with NULL parameters\n" );
153 if( GetLastError() == ERROR_CALL_NOT_IMPLEMENTED )
155 ok( GetLastError() == ERROR_INVALID_PARAMETER,
156 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
159 r = pConvertStringSidToSidA( refs[0].refStr, NULL );
160 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
161 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
164 r = pConvertStringSidToSidA( NULL, &str );
165 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
166 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %d\n",
169 r = pConvertStringSidToSidA( noSubAuthStr, &psid );
171 "expected failure with no sub authorities\n" );
172 ok( GetLastError() == ERROR_INVALID_SID,
173 "expected GetLastError() is ERROR_INVALID_SID, got %d\n",
176 for( i = 0; i < sizeof(refs) / sizeof(refs[0]); i++ )
180 r = AllocateAndInitializeSid( &refs[i].auth, 1,1,0,0,0,0,0,0,0,
182 ok( r, "failed to allocate sid\n" );
183 r = pConvertSidToStringSidA( psid, &str );
184 ok( r, "failed to convert sid\n" );
187 ok( !strcmp( str, refs[i].refStr ),
188 "incorrect sid, expected %s, got %s\n", refs[i].refStr, str );
194 r = pConvertStringSidToSidA( refs[i].refStr, &psid );
195 ok( r, "failed to parse sid string\n" );
198 !memcmp( pisid->IdentifierAuthority.Value, refs[i].auth.Value,
199 sizeof(refs[i].auth) ),
200 "string sid %s didn't parse to expected value\n"
201 "(got 0x%04x%08x, expected 0x%04x%08x)\n",
203 MAKEWORD( pisid->IdentifierAuthority.Value[1],
204 pisid->IdentifierAuthority.Value[0] ),
205 MAKELONG( MAKEWORD( pisid->IdentifierAuthority.Value[5],
206 pisid->IdentifierAuthority.Value[4] ),
207 MAKEWORD( pisid->IdentifierAuthority.Value[3],
208 pisid->IdentifierAuthority.Value[2] ) ),
209 MAKEWORD( refs[i].auth.Value[1], refs[i].auth.Value[0] ),
210 MAKELONG( MAKEWORD( refs[i].auth.Value[5], refs[i].auth.Value[4] ),
211 MAKEWORD( refs[i].auth.Value[3], refs[i].auth.Value[2] ) ) );
216 trace("String SIDs:\n");
257 static void test_trustee(void)
259 GUID ObjectType = {0x12345678, 0x1234, 0x5678, {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}};
260 GUID InheritedObjectType = {0x23456789, 0x2345, 0x6786, {0x2, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}};
262 OBJECTS_AND_NAME_ oan;
266 char szObjectTypeName[] = "ObjectTypeName";
267 char szInheritedObjectTypeName[] = "InheritedObjectTypeName";
268 char szTrusteeName[] = "szTrusteeName";
269 SID_IDENTIFIER_AUTHORITY auth = { {0x11,0x22,0,0,0, 0} };
271 memset( &ZeroGuid, 0x00, sizeof (ZeroGuid) );
273 pBuildTrusteeWithSidA = (fnBuildTrusteeWithSidA)
274 GetProcAddress( hmod, "BuildTrusteeWithSidA" );
275 pBuildTrusteeWithNameA = (fnBuildTrusteeWithNameA)
276 GetProcAddress( hmod, "BuildTrusteeWithNameA" );
277 pBuildTrusteeWithObjectsAndNameA = (fnBuildTrusteeWithObjectsAndNameA)
278 GetProcAddress (hmod, "BuildTrusteeWithObjectsAndNameA" );
279 pBuildTrusteeWithObjectsAndSidA = (fnBuildTrusteeWithObjectsAndSidA)
280 GetProcAddress (hmod, "BuildTrusteeWithObjectsAndSidA" );
281 pGetTrusteeNameA = (fnGetTrusteeNameA)
282 GetProcAddress (hmod, "GetTrusteeNameA" );
283 if( !pBuildTrusteeWithSidA || !pBuildTrusteeWithNameA ||
284 !pBuildTrusteeWithObjectsAndNameA || !pBuildTrusteeWithObjectsAndSidA ||
288 if ( ! AllocateAndInitializeSid( &auth, 1, 42, 0,0,0,0,0,0,0,&psid ) )
290 trace( "failed to init SID\n" );
294 /* test BuildTrusteeWithSidA */
295 memset( &trustee, 0xff, sizeof trustee );
296 pBuildTrusteeWithSidA( &trustee, psid );
298 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
299 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
300 "MultipleTrusteeOperation wrong\n");
301 ok( trustee.TrusteeForm == TRUSTEE_IS_SID, "TrusteeForm wrong\n");
302 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
303 ok( trustee.ptstrName == (LPSTR) psid, "ptstrName wrong\n" );
305 /* test BuildTrusteeWithObjectsAndSidA (test 1) */
306 memset( &trustee, 0xff, sizeof trustee );
307 memset( &oas, 0xff, sizeof(oas) );
308 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, &ObjectType,
309 &InheritedObjectType, psid);
311 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
312 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
313 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
314 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
315 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
317 ok(oas.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
318 ok(!memcmp(&oas.ObjectTypeGuid, &ObjectType, sizeof(GUID)), "ObjectTypeGuid wrong\n");
319 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
320 ok(oas.pSid == psid, "pSid wrong\n");
322 /* test GetTrusteeNameA */
323 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oas, "GetTrusteeName returned wrong value\n");
325 /* test BuildTrusteeWithObjectsAndSidA (test 2) */
326 memset( &trustee, 0xff, sizeof trustee );
327 memset( &oas, 0xff, sizeof(oas) );
328 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, NULL,
329 &InheritedObjectType, psid);
331 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
332 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
333 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
334 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
335 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
337 ok(oas.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
338 ok(!memcmp(&oas.ObjectTypeGuid, &ZeroGuid, sizeof(GUID)), "ObjectTypeGuid wrong\n");
339 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
340 ok(oas.pSid == psid, "pSid wrong\n");
344 /* test BuildTrusteeWithNameA */
345 memset( &trustee, 0xff, sizeof trustee );
346 pBuildTrusteeWithNameA( &trustee, szTrusteeName );
348 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
349 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
350 "MultipleTrusteeOperation wrong\n");
351 ok( trustee.TrusteeForm == TRUSTEE_IS_NAME, "TrusteeForm wrong\n");
352 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
353 ok( trustee.ptstrName == szTrusteeName, "ptstrName wrong\n" );
355 /* test BuildTrusteeWithObjectsAndNameA (test 1) */
356 memset( &trustee, 0xff, sizeof trustee );
357 memset( &oan, 0xff, sizeof(oan) );
358 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
359 szInheritedObjectTypeName, szTrusteeName);
361 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
362 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
363 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
364 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
365 ok(trustee.ptstrName == (LPTSTR)&oan, "ptstrName wrong\n");
367 ok(oan.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
368 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
369 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
370 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
372 /* test GetTrusteeNameA */
373 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oan, "GetTrusteeName returned wrong value\n");
375 /* test BuildTrusteeWithObjectsAndNameA (test 2) */
376 memset( &trustee, 0xff, sizeof trustee );
377 memset( &oan, 0xff, sizeof(oan) );
378 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, NULL,
379 szInheritedObjectTypeName, szTrusteeName);
381 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
382 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
383 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
384 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
385 ok(trustee.ptstrName == (LPSTR)&oan, "ptstrName wrong\n");
387 ok(oan.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
388 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
389 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
390 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
392 /* test BuildTrusteeWithObjectsAndNameA (test 3) */
393 memset( &trustee, 0xff, sizeof trustee );
394 memset( &oan, 0xff, sizeof(oan) );
395 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
396 NULL, szTrusteeName);
398 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
399 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
400 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
401 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
402 ok(trustee.ptstrName == (LPTSTR)&oan, "ptstrName wrong\n");
404 ok(oan.ObjectsPresent == ACE_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
405 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
406 ok(oan.InheritedObjectTypeName == NULL, "InheritedObjectTypeName wrong\n");
407 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
410 /* If the first isn't defined, assume none is */
411 #ifndef SE_MIN_WELL_KNOWN_PRIVILEGE
412 #define SE_MIN_WELL_KNOWN_PRIVILEGE 2L
413 #define SE_CREATE_TOKEN_PRIVILEGE 2L
414 #define SE_ASSIGNPRIMARYTOKEN_PRIVILEGE 3L
415 #define SE_LOCK_MEMORY_PRIVILEGE 4L
416 #define SE_INCREASE_QUOTA_PRIVILEGE 5L
417 #define SE_MACHINE_ACCOUNT_PRIVILEGE 6L
418 #define SE_TCB_PRIVILEGE 7L
419 #define SE_SECURITY_PRIVILEGE 8L
420 #define SE_TAKE_OWNERSHIP_PRIVILEGE 9L
421 #define SE_LOAD_DRIVER_PRIVILEGE 10L
422 #define SE_SYSTEM_PROFILE_PRIVILEGE 11L
423 #define SE_SYSTEMTIME_PRIVILEGE 12L
424 #define SE_PROF_SINGLE_PROCESS_PRIVILEGE 13L
425 #define SE_INC_BASE_PRIORITY_PRIVILEGE 14L
426 #define SE_CREATE_PAGEFILE_PRIVILEGE 15L
427 #define SE_CREATE_PERMANENT_PRIVILEGE 16L
428 #define SE_BACKUP_PRIVILEGE 17L
429 #define SE_RESTORE_PRIVILEGE 18L
430 #define SE_SHUTDOWN_PRIVILEGE 19L
431 #define SE_DEBUG_PRIVILEGE 20L
432 #define SE_AUDIT_PRIVILEGE 21L
433 #define SE_SYSTEM_ENVIRONMENT_PRIVILEGE 22L
434 #define SE_CHANGE_NOTIFY_PRIVILLEGE 23L
435 #define SE_REMOTE_SHUTDOWN_PRIVILEGE 24L
436 #define SE_UNDOCK_PRIVILEGE 25L
437 #define SE_SYNC_AGENT_PRIVILEGE 26L
438 #define SE_ENABLE_DELEGATION_PRIVILEGE 27L
439 #define SE_MANAGE_VOLUME_PRIVILEGE 28L
440 #define SE_IMPERSONATE_PRIVILEGE 29L
441 #define SE_CREATE_GLOBAL_PRIVILEGE 30L
442 #define SE_MAX_WELL_KNOWN_PRIVILEGE SE_CREATE_GLOBAL_PRIVILEGE
443 #endif /* ndef SE_MIN_WELL_KNOWN_PRIVILEGE */
445 static void test_allocateLuid(void)
447 BOOL (WINAPI *pAllocateLocallyUniqueId)(PLUID);
451 pAllocateLocallyUniqueId = (void*)GetProcAddress(hmod, "AllocateLocallyUniqueId");
452 if (!pAllocateLocallyUniqueId) return;
454 ret = pAllocateLocallyUniqueId(&luid1);
455 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
459 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
460 ret = pAllocateLocallyUniqueId(&luid2);
462 "AllocateLocallyUniqueId failed: %d\n", GetLastError());
463 ok(luid1.LowPart > SE_MAX_WELL_KNOWN_PRIVILEGE || luid1.HighPart != 0,
464 "AllocateLocallyUniqueId returned a well-known LUID\n");
465 ok(luid1.LowPart != luid2.LowPart || luid1.HighPart != luid2.HighPart,
466 "AllocateLocallyUniqueId returned non-unique LUIDs\n");
467 ret = pAllocateLocallyUniqueId(NULL);
468 ok( !ret && GetLastError() == ERROR_NOACCESS,
469 "AllocateLocallyUniqueId(NULL) didn't return ERROR_NOACCESS: %d\n",
473 static void test_lookupPrivilegeName(void)
475 BOOL (WINAPI *pLookupPrivilegeNameA)(LPCSTR, PLUID, LPSTR, LPDWORD);
476 char buf[MAX_PATH]; /* arbitrary, seems long enough */
477 DWORD cchName = sizeof(buf);
478 LUID luid = { 0, 0 };
482 /* check whether it's available first */
483 pLookupPrivilegeNameA = (void*)GetProcAddress(hmod, "LookupPrivilegeNameA");
484 if (!pLookupPrivilegeNameA) return;
485 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
486 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
487 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
490 /* check with a short buffer */
492 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
493 ret = pLookupPrivilegeNameA(NULL, &luid, NULL, &cchName);
494 ok( !ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
495 "LookupPrivilegeNameA didn't fail with ERROR_INSUFFICIENT_BUFFER: %d\n",
497 ok(cchName == strlen("SeCreateTokenPrivilege") + 1,
498 "LookupPrivilegeNameA returned an incorrect required length for\n"
499 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName,
500 lstrlenA("SeCreateTokenPrivilege") + 1);
501 /* check a known value and its returned length on success */
502 cchName = sizeof(buf);
503 ok(pLookupPrivilegeNameA(NULL, &luid, buf, &cchName) &&
504 cchName == strlen("SeCreateTokenPrivilege"),
505 "LookupPrivilegeNameA returned an incorrect output length for\n"
506 "SeCreateTokenPrivilege (got %d, expected %d)\n", cchName,
507 (int)strlen("SeCreateTokenPrivilege"));
508 /* check known values */
509 for (i = SE_MIN_WELL_KNOWN_PRIVILEGE; i < SE_MAX_WELL_KNOWN_PRIVILEGE; i++)
512 cchName = sizeof(buf);
513 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
514 ok( ret || GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
515 "LookupPrivilegeNameA(0.%d) failed: %d\n", i, GetLastError());
517 /* check a bogus LUID */
518 luid.LowPart = 0xdeadbeef;
519 cchName = sizeof(buf);
520 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
521 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
522 "LookupPrivilegeNameA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
524 /* check on a bogus system */
525 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
526 cchName = sizeof(buf);
527 ret = pLookupPrivilegeNameA("b0gu5.Nam3", &luid, buf, &cchName);
528 ok( !ret && GetLastError() == RPC_S_SERVER_UNAVAILABLE,
529 "LookupPrivilegeNameA didn't fail with RPC_S_SERVER_UNAVAILABLE: %d\n",
539 static void test_lookupPrivilegeValue(void)
541 static const struct NameToLUID privs[] = {
542 { "SeCreateTokenPrivilege", SE_CREATE_TOKEN_PRIVILEGE },
543 { "SeAssignPrimaryTokenPrivilege", SE_ASSIGNPRIMARYTOKEN_PRIVILEGE },
544 { "SeLockMemoryPrivilege", SE_LOCK_MEMORY_PRIVILEGE },
545 { "SeIncreaseQuotaPrivilege", SE_INCREASE_QUOTA_PRIVILEGE },
546 { "SeMachineAccountPrivilege", SE_MACHINE_ACCOUNT_PRIVILEGE },
547 { "SeTcbPrivilege", SE_TCB_PRIVILEGE },
548 { "SeSecurityPrivilege", SE_SECURITY_PRIVILEGE },
549 { "SeTakeOwnershipPrivilege", SE_TAKE_OWNERSHIP_PRIVILEGE },
550 { "SeLoadDriverPrivilege", SE_LOAD_DRIVER_PRIVILEGE },
551 { "SeSystemProfilePrivilege", SE_SYSTEM_PROFILE_PRIVILEGE },
552 { "SeSystemtimePrivilege", SE_SYSTEMTIME_PRIVILEGE },
553 { "SeProfileSingleProcessPrivilege", SE_PROF_SINGLE_PROCESS_PRIVILEGE },
554 { "SeIncreaseBasePriorityPrivilege", SE_INC_BASE_PRIORITY_PRIVILEGE },
555 { "SeCreatePagefilePrivilege", SE_CREATE_PAGEFILE_PRIVILEGE },
556 { "SeCreatePermanentPrivilege", SE_CREATE_PERMANENT_PRIVILEGE },
557 { "SeBackupPrivilege", SE_BACKUP_PRIVILEGE },
558 { "SeRestorePrivilege", SE_RESTORE_PRIVILEGE },
559 { "SeShutdownPrivilege", SE_SHUTDOWN_PRIVILEGE },
560 { "SeDebugPrivilege", SE_DEBUG_PRIVILEGE },
561 { "SeAuditPrivilege", SE_AUDIT_PRIVILEGE },
562 { "SeSystemEnvironmentPrivilege", SE_SYSTEM_ENVIRONMENT_PRIVILEGE },
563 { "SeChangeNotifyPrivilege", SE_CHANGE_NOTIFY_PRIVILLEGE },
564 { "SeRemoteShutdownPrivilege", SE_REMOTE_SHUTDOWN_PRIVILEGE },
565 { "SeUndockPrivilege", SE_UNDOCK_PRIVILEGE },
566 { "SeSyncAgentPrivilege", SE_SYNC_AGENT_PRIVILEGE },
567 { "SeEnableDelegationPrivilege", SE_ENABLE_DELEGATION_PRIVILEGE },
568 { "SeManageVolumePrivilege", SE_MANAGE_VOLUME_PRIVILEGE },
569 { "SeImpersonatePrivilege", SE_IMPERSONATE_PRIVILEGE },
570 { "SeCreateGlobalPrivilege", SE_CREATE_GLOBAL_PRIVILEGE },
572 BOOL (WINAPI *pLookupPrivilegeValueA)(LPCSTR, LPCSTR, PLUID);
577 /* check whether it's available first */
578 pLookupPrivilegeValueA = (void*)GetProcAddress(hmod, "LookupPrivilegeValueA");
579 if (!pLookupPrivilegeValueA) return;
580 ret = pLookupPrivilegeValueA(NULL, "SeCreateTokenPrivilege", &luid);
581 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
584 /* check a bogus system name */
585 ret = pLookupPrivilegeValueA("b0gu5.Nam3", "SeCreateTokenPrivilege", &luid);
586 ok( !ret && GetLastError() == RPC_S_SERVER_UNAVAILABLE,
587 "LookupPrivilegeValueA didn't fail with RPC_S_SERVER_UNAVAILABLE: %d\n",
589 /* check a NULL string */
590 ret = pLookupPrivilegeValueA(NULL, 0, &luid);
591 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
592 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
594 /* check a bogus privilege name */
595 ret = pLookupPrivilegeValueA(NULL, "SeBogusPrivilege", &luid);
596 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
597 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %d\n",
599 /* check case insensitive */
600 ret = pLookupPrivilegeValueA(NULL, "sEcREATEtOKENpRIVILEGE", &luid);
602 "LookupPrivilegeValueA(NULL, sEcREATEtOKENpRIVILEGE, &luid) failed: %d\n",
604 for (i = 0; i < sizeof(privs) / sizeof(privs[0]); i++)
606 /* Not all privileges are implemented on all Windows versions, so
607 * don't worry if the call fails
609 if (pLookupPrivilegeValueA(NULL, privs[i].name, &luid))
611 ok(luid.LowPart == privs[i].lowPart,
612 "LookupPrivilegeValueA returned an invalid LUID for %s\n",
618 static void test_luid(void)
621 test_lookupPrivilegeName();
622 test_lookupPrivilegeValue();
625 static void test_FileSecurity(void)
627 char directory[MAX_PATH];
628 DWORD retval, outSize;
632 pGetFileSecurityA = (fnGetFileSecurityA)
633 GetProcAddress( hmod, "GetFileSecurityA" );
634 if( !pGetFileSecurityA )
637 retval = GetTempPathA(sizeof(directory), directory);
639 trace("GetTempPathA failed\n");
643 strcpy(directory, "\\Should not exist");
645 SetLastError(NO_ERROR);
646 result = pGetFileSecurityA( directory,OWNER_SECURITY_INFORMATION,buffer,0x40,&outSize);
647 ok(!result, "GetFileSecurityA should fail for not existing directories/files\n");
648 ok( (GetLastError() == ERROR_FILE_NOT_FOUND ) ||
649 (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) ,
650 "last error ERROR_FILE_NOT_FOUND / ERROR_CALL_NOT_IMPLEMENTED (98) "
651 "expected, got %d\n", GetLastError());
654 static void test_AccessCheck(void)
656 PSID EveryoneSid = NULL, AdminSid = NULL, UsersSid = NULL;
658 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL;
659 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
660 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
661 GENERIC_MAPPING Mapping = { KEY_READ, KEY_WRITE, KEY_EXECUTE, KEY_ALL_ACCESS };
668 PRIVILEGE_SET *PrivSet;
674 NtDllModule = GetModuleHandle("ntdll.dll");
677 skip("not running on NT, skipping test\n");
680 pRtlAdjustPrivilege = (fnRtlAdjustPrivilege)
681 GetProcAddress(NtDllModule, "RtlAdjustPrivilege");
682 if (!pRtlAdjustPrivilege)
684 skip("missing RtlAdjustPrivilege, skipping test\n");
688 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
689 res = InitializeAcl(Acl, 256, ACL_REVISION);
690 if(!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
692 skip("ACLs not implemented - skipping tests\n");
695 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
697 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
698 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
700 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
701 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdminSid);
702 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
704 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
705 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &UsersSid);
706 ok(res, "AllocateAndInitializeSid failed with error %d\n", GetLastError());
708 res = AddAccessAllowedAce(Acl, ACL_REVISION, KEY_READ, EveryoneSid);
709 ok(res, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
711 res = AddAccessDeniedAce(Acl, ACL_REVISION, KEY_SET_VALUE, AdminSid);
712 ok(res, "AddAccessDeniedAce failed with error %d\n", GetLastError());
714 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
716 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
717 ok(res, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
719 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
720 ok(res, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
722 PrivSetLen = FIELD_OFFSET(PRIVILEGE_SET, Privilege[16]);
723 PrivSet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, PrivSetLen);
724 PrivSet->PrivilegeCount = 16;
726 res = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &ProcessToken);
727 ok(res, "OpenProcessToken failed with error %d\n", GetLastError());
729 pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, FALSE, TRUE, &Enabled);
731 res = DuplicateToken(ProcessToken, SecurityIdentification, &Token);
732 ok(res, "DuplicateToken failed with error %d\n", GetLastError());
734 /* SD without owner/group */
735 SetLastError(0xdeadbeef);
736 Access = AccessStatus = 0xdeadbeef;
737 ret = AccessCheck(SecurityDescriptor, Token, KEY_QUERY_VALUE, &Mapping,
738 PrivSet, &PrivSetLen, &Access, &AccessStatus);
739 err = GetLastError();
740 ok(!ret && err == ERROR_INVALID_SECURITY_DESCR, "AccessCheck should have "
741 "failed with ERROR_INVALID_SECURITY_DESCR, instead of %d\n", err);
742 ok(Access == 0xdeadbeef && AccessStatus == 0xdeadbeef,
743 "Access and/or AccessStatus were changed!\n");
745 /* Set owner and group */
746 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
747 ok(res, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
748 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, TRUE);
749 ok(res, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
751 /* Generic access mask */
752 SetLastError(0xdeadbeef);
753 ret = AccessCheck(SecurityDescriptor, Token, GENERIC_READ, &Mapping,
754 PrivSet, &PrivSetLen, &Access, &AccessStatus);
755 err = GetLastError();
756 ok(!ret && err == ERROR_GENERIC_NOT_MAPPED, "AccessCheck should have failed "
757 "with ERROR_GENERIC_NOT_MAPPED, instead of %d\n", err);
758 ok(Access == 0xdeadbeef && AccessStatus == 0xdeadbeef,
759 "Access and/or AccessStatus were changed!\n");
761 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
762 PrivSet, &PrivSetLen, &Access, &AccessStatus);
763 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
764 ok(AccessStatus && (Access == KEY_READ),
765 "AccessCheck failed to grant access with error %d\n",
768 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
769 PrivSet, &PrivSetLen, &Access, &AccessStatus);
770 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
772 "AccessCheck failed to grant any access with error %d\n",
774 trace("AccessCheck with MAXIMUM_ALLOWED got Access 0x%08x\n", Access);
776 /* Access denied by SD */
777 SetLastError(0xdeadbeef);
778 ret = AccessCheck(SecurityDescriptor, Token, KEY_WRITE, &Mapping,
779 PrivSet, &PrivSetLen, &Access, &AccessStatus);
780 ok(ret, "AccessCheck failed with error %d\n", GetLastError());
781 err = GetLastError();
782 ok(!AccessStatus && err == ERROR_ACCESS_DENIED, "AccessCheck should have failed "
783 "with ERROR_ACCESS_DENIED, instead of %d\n", err);
784 ok(!Access, "Should have failed to grant any access, got 0x%08x\n", Access);
787 PrivSet->PrivilegeCount = 16;
788 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
789 PrivSet, &PrivSetLen, &Access, &AccessStatus);
790 ok(ret && !AccessStatus && GetLastError() == ERROR_PRIVILEGE_NOT_HELD,
791 "AccessCheck should have failed with ERROR_PRIVILEGE_NOT_HELD, instead of %d\n",
794 ret = pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, TRUE, TRUE, &Enabled);
798 PrivSet->PrivilegeCount = 16;
799 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
800 PrivSet, &PrivSetLen, &Access, &AccessStatus);
801 ok(ret && AccessStatus && GetLastError() == 0,
802 "AccessCheck should have succeeded, error %d\n",
804 ok(Access == ACCESS_SYSTEM_SECURITY,
805 "Access should be equal to ACCESS_SYSTEM_SECURITY instead of 0x%08x\n",
809 trace("Couldn't get SE_SECURITY_PRIVILEGE (0x%08x), skipping ACCESS_SYSTEM_SECURITY test\n",
814 res = DuplicateToken(ProcessToken, SecurityAnonymous, &Token);
815 ok(res, "DuplicateToken failed with error %d\n", GetLastError());
817 SetLastError(0xdeadbeef);
818 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
819 PrivSet, &PrivSetLen, &Access, &AccessStatus);
820 err = GetLastError();
821 ok(!ret && err == ERROR_BAD_IMPERSONATION_LEVEL, "AccessCheck should have failed "
822 "with ERROR_BAD_IMPERSONATION_LEVEL, instead of %d\n", err);
826 SetLastError(0xdeadbeef);
827 ret = AccessCheck(SecurityDescriptor, ProcessToken, KEY_READ, &Mapping,
828 PrivSet, &PrivSetLen, &Access, &AccessStatus);
829 err = GetLastError();
830 ok(!ret && err == ERROR_NO_IMPERSONATION_TOKEN, "AccessCheck should have failed "
831 "with ERROR_NO_IMPERSONATION_TOKEN, instead of %d\n", err);
833 CloseHandle(ProcessToken);
836 FreeSid(EveryoneSid);
841 HeapFree(GetProcessHeap(), 0, Acl);
842 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
843 HeapFree(GetProcessHeap(), 0, PrivSet);
846 /* test GetTokenInformation for the various attributes */
847 static void test_token_attr(void)
849 HANDLE Token, ImpersonationToken;
851 TOKEN_PRIVILEGES *Privileges;
852 TOKEN_GROUPS *Groups;
857 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel;
859 /* cygwin-like use case */
860 SetLastError(0xdeadbeef);
861 ret = OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED, &Token);
862 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
864 skip("OpenProcessToken is not implemented\n");
867 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
872 ret = GetTokenInformation(Token, TokenUser,(void*)buf, Size, &Size);
873 ok(ret, "GetTokenInformation failed with error %d\n", GetLastError());
874 Size = sizeof(ImpersonationLevel);
875 ret = GetTokenInformation(Token, TokenImpersonationLevel, &ImpersonationLevel, Size, &Size);
876 GLE = GetLastError();
877 ok(!ret && (GLE == ERROR_INVALID_PARAMETER), "GetTokenInformation(TokenImpersonationLevel) on primary token should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GLE);
881 if(!pConvertSidToStringSidA)
883 skip("ConvertSidToStringSidA is not available\n");
887 SetLastError(0xdeadbeef);
888 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &Token);
889 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
892 ret = GetTokenInformation(Token, TokenGroups, NULL, 0, &Size);
893 Groups = HeapAlloc(GetProcessHeap(), 0, Size);
894 ret = GetTokenInformation(Token, TokenGroups, Groups, Size, &Size);
895 ok(ret, "GetTokenInformation(TokenGroups) failed with error %d\n", GetLastError());
896 trace("TokenGroups:\n");
897 for (i = 0; i < Groups->GroupCount; i++)
899 DWORD NameLength = 255;
901 DWORD DomainLength = 255;
903 SID_NAME_USE SidNameUse;
904 pConvertSidToStringSidA(Groups->Groups[i].Sid, &SidString);
907 ret = LookupAccountSid(NULL, Groups->Groups[i].Sid, Name, &NameLength, Domain, &DomainLength, &SidNameUse);
909 trace("\t%s, %s\\%s use: %d attr: 0x%08x\n", SidString, Domain, Name, SidNameUse, Groups->Groups[i].Attributes);
911 trace("\t%s, attr: 0x%08x LookupAccountSid failed with error %d\n", SidString, Groups->Groups[i].Attributes, GetLastError());
912 LocalFree(SidString);
914 HeapFree(GetProcessHeap(), 0, Groups);
917 ret = GetTokenInformation(Token, TokenUser, NULL, 0, &Size);
918 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
919 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
920 User = HeapAlloc(GetProcessHeap(), 0, Size);
921 ret = GetTokenInformation(Token, TokenUser, User, Size, &Size);
923 "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
925 pConvertSidToStringSidA(User->User.Sid, &SidString);
926 trace("TokenUser: %s attr: 0x%08x\n", SidString, User->User.Attributes);
927 LocalFree(SidString);
928 HeapFree(GetProcessHeap(), 0, User);
931 ret = GetTokenInformation(Token, TokenPrivileges, NULL, 0, &Size);
932 ok(!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
933 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
934 Privileges = HeapAlloc(GetProcessHeap(), 0, Size);
935 ret = GetTokenInformation(Token, TokenPrivileges, Privileges, Size, &Size);
937 "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
938 trace("TokenPrivileges:\n");
939 for (i = 0; i < Privileges->PrivilegeCount; i++)
942 DWORD NameLen = sizeof(Name)/sizeof(Name[0]);
943 LookupPrivilegeName(NULL, &Privileges->Privileges[i].Luid, Name, &NameLen);
944 trace("\t%s, 0x%x\n", Name, Privileges->Privileges[i].Attributes);
946 HeapFree(GetProcessHeap(), 0, Privileges);
948 ret = DuplicateToken(Token, SecurityAnonymous, &ImpersonationToken);
949 ok(ret, "DuplicateToken failed with error %d\n", GetLastError());
951 Size = sizeof(ImpersonationLevel);
952 ret = GetTokenInformation(ImpersonationToken, TokenImpersonationLevel, &ImpersonationLevel, Size, &Size);
953 ok(ret, "GetTokenInformation(TokenImpersonationLevel) failed with error %d\n", GetLastError());
954 ok(ImpersonationLevel == SecurityAnonymous, "ImpersonationLevel should have been SecurityAnonymous instead of %d\n", ImpersonationLevel);
956 CloseHandle(ImpersonationToken);
960 typedef union _MAX_SID
963 char max[SECURITY_MAX_SID_SIZE];
966 static void test_sid_str(PSID * sid)
969 BOOL ret = pConvertSidToStringSidA(sid, &str_sid);
970 ok(ret, "ConvertSidToStringSidA() failed: %d\n", GetLastError());
973 char account[MAX_PATH], domain[MAX_PATH];
975 DWORD acc_size = MAX_PATH;
976 DWORD dom_size = MAX_PATH;
977 ret = LookupAccountSid(NULL, sid, account, &acc_size, domain, &dom_size, &use);
978 ok(ret || (!ret && (GetLastError() == ERROR_NONE_MAPPED)),
979 "LookupAccountSid(%s) failed: %d\n", str_sid, GetLastError());
981 trace(" %s %s\\%s %d\n", str_sid, domain, account, use);
982 else if (GetLastError() == ERROR_NONE_MAPPED)
983 trace(" %s couldn't be mapped\n", str_sid);
988 static void test_LookupAccountSid(void)
990 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
991 CHAR accountA[MAX_PATH], domainA[MAX_PATH];
992 DWORD acc_sizeA, dom_sizeA;
993 DWORD real_acc_sizeA, real_dom_sizeA;
994 WCHAR accountW[MAX_PATH], domainW[MAX_PATH];
995 DWORD acc_sizeW, dom_sizeW;
996 DWORD real_acc_sizeW, real_dom_sizeW;
997 PSID pUsersSid = NULL;
1005 /* native windows crashes if account size, domain size, or name use is NULL */
1007 ret = AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
1008 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &pUsersSid);
1009 ok(ret || (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED),
1010 "AllocateAndInitializeSid failed with error %d\n", GetLastError());
1012 /* not running on NT so give up */
1013 if (!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1016 real_acc_sizeA = MAX_PATH;
1017 real_dom_sizeA = MAX_PATH;
1018 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &real_acc_sizeA, domainA, &real_dom_sizeA, &use);
1019 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1021 /* try NULL account */
1022 acc_sizeA = MAX_PATH;
1023 dom_sizeA = MAX_PATH;
1024 ret = LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
1025 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1027 /* try NULL domain */
1028 acc_sizeA = MAX_PATH;
1029 dom_sizeA = MAX_PATH;
1030 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
1031 ok(ret, "LookupAccountSidA() Expected TRUE, got FALSE\n");
1033 /* try a small account buffer */
1035 dom_sizeA = MAX_PATH;
1037 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1038 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1039 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1040 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1042 /* try a 0 sized account buffer */
1044 dom_sizeA = MAX_PATH;
1046 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1047 /* this can fail or succeed depending on OS version but the size will always be returned */
1048 ok(acc_sizeA == real_acc_sizeA + 1,
1049 "LookupAccountSidA() Expected acc_size = %u, got %u\n",
1050 real_acc_sizeA + 1, acc_sizeA);
1052 /* try a 0 sized account buffer */
1054 dom_sizeA = MAX_PATH;
1055 ret = LookupAccountSidA(NULL, pUsersSid, NULL, &acc_sizeA, domainA, &dom_sizeA, &use);
1056 /* this can fail or succeed depending on OS version but the size will always be returned */
1057 ok(acc_sizeA == real_acc_sizeA + 1,
1058 "LookupAccountSid() Expected acc_size = %u, got %u\n",
1059 real_acc_sizeA + 1, acc_sizeA);
1061 /* try a small domain buffer */
1063 acc_sizeA = MAX_PATH;
1065 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1066 ok(!ret, "LookupAccountSidA() Expected FALSE got TRUE\n");
1067 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1068 "LookupAccountSidA() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1070 /* try a 0 sized domain buffer */
1072 acc_sizeA = MAX_PATH;
1074 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use);
1075 /* this can fail or succeed depending on OS version but the size will always be returned */
1076 ok(dom_sizeA == real_dom_sizeA + 1,
1077 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1078 real_dom_sizeA + 1, dom_sizeA);
1080 /* try a 0 sized domain buffer */
1082 acc_sizeA = MAX_PATH;
1083 ret = LookupAccountSidA(NULL, pUsersSid, accountA, &acc_sizeA, NULL, &dom_sizeA, &use);
1084 /* this can fail or succeed depending on OS version but the size will always be returned */
1085 ok(dom_sizeA == real_dom_sizeA + 1,
1086 "LookupAccountSidA() Expected dom_size = %u, got %u\n",
1087 real_dom_sizeA + 1, dom_sizeA);
1089 real_acc_sizeW = MAX_PATH;
1090 real_dom_sizeW = MAX_PATH;
1091 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &real_acc_sizeW, domainW, &real_dom_sizeW, &use);
1092 ok(ret, "LookupAccountSidW() Expected TRUE, got FALSE\n");
1094 /* native windows crashes if domainW or accountW is NULL */
1096 /* try a small account buffer */
1098 dom_sizeW = MAX_PATH;
1100 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1101 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1102 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1103 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1105 /* try a 0 sized account buffer */
1107 dom_sizeW = MAX_PATH;
1109 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1110 /* this can fail or succeed depending on OS version but the size will always be returned */
1111 ok(acc_sizeW == real_acc_sizeW + 1,
1112 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1113 real_acc_sizeW + 1, acc_sizeW);
1115 /* try a 0 sized account buffer */
1117 dom_sizeW = MAX_PATH;
1118 ret = LookupAccountSidW(NULL, pUsersSid, NULL, &acc_sizeW, domainW, &dom_sizeW, &use);
1119 /* this can fail or succeed depending on OS version but the size will always be returned */
1120 ok(acc_sizeW == real_acc_sizeW + 1,
1121 "LookupAccountSidW() Expected acc_size = %u, got %u\n",
1122 real_acc_sizeW + 1, acc_sizeW);
1124 /* try a small domain buffer */
1126 acc_sizeW = MAX_PATH;
1128 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1129 ok(!ret, "LookupAccountSidW() Expected FALSE got TRUE\n");
1130 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1131 "LookupAccountSidW() Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1133 /* try a 0 sized domain buffer */
1135 acc_sizeW = MAX_PATH;
1137 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, domainW, &dom_sizeW, &use);
1138 /* this can fail or succeed depending on OS version but the size will always be returned */
1139 ok(dom_sizeW == real_dom_sizeW + 1,
1140 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1141 real_dom_sizeW + 1, dom_sizeW);
1143 /* try a 0 sized domain buffer */
1145 acc_sizeW = MAX_PATH;
1146 ret = LookupAccountSidW(NULL, pUsersSid, accountW, &acc_sizeW, NULL, &dom_sizeW, &use);
1147 /* this can fail or succeed depending on OS version but the size will always be returned */
1148 ok(dom_sizeW == real_dom_sizeW + 1,
1149 "LookupAccountSidW() Expected dom_size = %u, got %u\n",
1150 real_dom_sizeW + 1, dom_sizeW);
1154 pCreateWellKnownSid = (fnCreateWellKnownSid)GetProcAddress( hmod, "CreateWellKnownSid" );
1156 if (pCreateWellKnownSid && pConvertSidToStringSidA)
1158 trace("Well Known SIDs:\n");
1159 for (i = 0; i <= 60; i++)
1161 size = SECURITY_MAX_SID_SIZE;
1162 if (pCreateWellKnownSid(i, NULL, &max_sid.sid, &size))
1164 if (pConvertSidToStringSidA(&max_sid.sid, &str_sidA))
1166 acc_sizeA = MAX_PATH;
1167 dom_sizeA = MAX_PATH;
1168 if (LookupAccountSidA(NULL, &max_sid.sid, accountA, &acc_sizeA, domainA, &dom_sizeA, &use))
1169 trace(" %d: %s %s\\%s %d\n", i, str_sidA, domainA, accountA, use);
1170 LocalFree(str_sidA);
1175 if (GetLastError() != ERROR_INVALID_PARAMETER)
1176 trace(" CreateWellKnownSid(%d) failed: %d\n", i, GetLastError());
1178 trace(" %d: not supported\n", i);
1182 pLsaQueryInformationPolicy = (fnLsaQueryInformationPolicy)GetProcAddress( hmod, "LsaQueryInformationPolicy");
1183 pLsaOpenPolicy = (fnLsaOpenPolicy)GetProcAddress( hmod, "LsaOpenPolicy");
1184 pLsaFreeMemory = (fnLsaFreeMemory)GetProcAddress( hmod, "LsaFreeMemory");
1185 pLsaClose = (fnLsaClose)GetProcAddress( hmod, "LsaClose");
1187 if (pLsaQueryInformationPolicy && pLsaOpenPolicy && pLsaFreeMemory && pLsaClose)
1191 LSA_OBJECT_ATTRIBUTES object_attributes;
1193 ZeroMemory(&object_attributes, sizeof(object_attributes));
1194 object_attributes.Length = sizeof(object_attributes);
1196 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_ALL_ACCESS, &handle);
1197 ok(status == STATUS_SUCCESS || status == STATUS_ACCESS_DENIED,
1198 "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status);
1200 /* try a more restricted access mask if necessary */
1201 if (status == STATUS_ACCESS_DENIED) {
1202 trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
1203 status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_VIEW_LOCAL_INFORMATION, &handle);
1204 ok(status == STATUS_SUCCESS, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status);
1207 if (status == STATUS_SUCCESS)
1209 PPOLICY_ACCOUNT_DOMAIN_INFO info;
1210 status = pLsaQueryInformationPolicy(handle, PolicyAccountDomainInformation, (PVOID*)&info);
1211 ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy() failed, returned 0x%08x\n", status);
1212 if (status == STATUS_SUCCESS)
1214 ok(info->DomainSid!=0, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) missing SID\n");
1215 if (info->DomainSid)
1217 int count = *GetSidSubAuthorityCount(info->DomainSid);
1218 CopySid(GetSidLengthRequired(count), &max_sid, info->DomainSid);
1219 test_sid_str((PSID)&max_sid.sid);
1220 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_ADMIN;
1221 max_sid.sid.SubAuthorityCount = count + 1;
1222 test_sid_str((PSID)&max_sid.sid);
1223 max_sid.sid.SubAuthority[count] = DOMAIN_USER_RID_GUEST;
1224 test_sid_str((PSID)&max_sid.sid);
1225 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ADMINS;
1226 test_sid_str((PSID)&max_sid.sid);
1227 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_USERS;
1228 test_sid_str((PSID)&max_sid.sid);
1229 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_GUESTS;
1230 test_sid_str((PSID)&max_sid.sid);
1231 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_COMPUTERS;
1232 test_sid_str((PSID)&max_sid.sid);
1233 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CONTROLLERS;
1234 test_sid_str((PSID)&max_sid.sid);
1235 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_CERT_ADMINS;
1236 test_sid_str((PSID)&max_sid.sid);
1237 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_SCHEMA_ADMINS;
1238 test_sid_str((PSID)&max_sid.sid);
1239 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_ENTERPRISE_ADMINS;
1240 test_sid_str((PSID)&max_sid.sid);
1241 max_sid.sid.SubAuthority[count] = DOMAIN_GROUP_RID_POLICY_ADMINS;
1242 test_sid_str((PSID)&max_sid.sid);
1243 max_sid.sid.SubAuthority[count] = DOMAIN_ALIAS_RID_RAS_SERVERS;
1244 test_sid_str((PSID)&max_sid.sid);
1245 max_sid.sid.SubAuthority[count] = 1000; /* first user account */
1246 test_sid_str((PSID)&max_sid.sid);
1249 pLsaFreeMemory((LPVOID)info);
1252 status = pLsaClose(handle);
1253 ok(status == STATUS_SUCCESS, "LsaClose() failed, returned 0x%08x\n", status);
1259 static void get_sid_info(PSID psid, LPSTR *user, LPSTR *dom)
1261 static CHAR account[UNLEN + 1];
1262 static CHAR domain[UNLEN + 1];
1263 DWORD size, dom_size;
1269 size = dom_size = UNLEN + 1;
1272 LookupAccountSidA(NULL, psid, account, &size, domain, &dom_size, &use);
1275 static void test_LookupAccountName(void)
1277 DWORD sid_size, domain_size, user_size;
1278 DWORD sid_save, domain_save;
1279 CHAR user_name[UNLEN + 1];
1280 SID_NAME_USE sid_use;
1281 LPSTR domain, account, sid_dom;
1285 /* native crashes if (assuming all other parameters correct):
1287 * - Sid is NULL and cbSid is > 0
1288 * - cbSid or cchReferencedDomainName are NULL
1289 * - ReferencedDomainName is NULL and cchReferencedDomainName is the correct size
1292 user_size = UNLEN + 1;
1293 ret = GetUserNameA(user_name, &user_size);
1294 ok(ret, "Failed to get user name\n");
1299 sid_use = 0xcafebabe;
1300 SetLastError(0xdeadbeef);
1301 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, NULL, &domain_size, &sid_use);
1302 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1304 skip("LookupAccountNameA is not implemented\n");
1307 ok(!ret, "Expected 0, got %d\n", ret);
1308 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1309 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1310 ok(sid_size != 0, "Expected non-zero sid size\n");
1311 ok(domain_size != 0, "Expected non-zero domain size\n");
1312 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
1314 sid_save = sid_size;
1315 domain_save = domain_size;
1317 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
1318 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
1320 /* try valid account name */
1321 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, domain, &domain_size, &sid_use);
1322 get_sid_info(psid, &account, &sid_dom);
1323 ok(ret, "Failed to lookup account name\n");
1324 ok(sid_size == GetLengthSid(psid), "Expected %d, got %d\n", GetLengthSid(psid), sid_size);
1327 ok(!lstrcmp(account, user_name), "Expected %s, got %s\n", user_name, account);
1328 ok(!lstrcmp(domain, sid_dom), "Expected %s, got %s\n", sid_dom, domain);
1329 ok(domain_size == domain_save - 1, "Expected %d, got %d\n", domain_save - 1, domain_size);
1330 ok(lstrlen(domain) == domain_size, "Expected %d\n", lstrlen(domain));
1331 ok(sid_use == SidTypeUser, "Expected SidTypeUser, got %d\n", SidTypeUser);
1333 domain_size = domain_save;
1335 /* NULL Sid with zero sid size */
1336 SetLastError(0xdeadbeef);
1338 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
1339 ok(!ret, "Expected 0, got %d\n", ret);
1340 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1341 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1342 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1343 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1345 /* try cchReferencedDomainName - 1 */
1346 SetLastError(0xdeadbeef);
1348 ret = LookupAccountNameA(NULL, user_name, NULL, &sid_size, domain, &domain_size, &sid_use);
1349 ok(!ret, "Expected 0, got %d\n", ret);
1350 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1351 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1352 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1353 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1355 /* NULL ReferencedDomainName with zero domain name size */
1356 SetLastError(0xdeadbeef);
1358 ret = LookupAccountNameA(NULL, user_name, psid, &sid_size, NULL, &domain_size, &sid_use);
1359 ok(!ret, "Expected 0, got %d\n", ret);
1360 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1361 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1362 ok(sid_size == sid_save, "Expected %d, got %d\n", sid_save, sid_size);
1363 ok(domain_size == domain_save, "Expected %d, got %d\n", domain_save, domain_size);
1365 HeapFree(GetProcessHeap(), 0, psid);
1366 HeapFree(GetProcessHeap(), 0, domain);
1368 /* get sizes for NULL account name */
1371 sid_use = 0xcafebabe;
1372 SetLastError(0xdeadbeef);
1373 ret = LookupAccountNameA(NULL, NULL, NULL, &sid_size, NULL, &domain_size, &sid_use);
1374 ok(!ret, "Expected 0, got %d\n", ret);
1375 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1376 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1377 ok(sid_size != 0, "Expected non-zero sid size\n");
1378 ok(domain_size != 0, "Expected non-zero domain size\n");
1379 ok(sid_use == 0xcafebabe, "Expected 0xcafebabe, got %d\n", sid_use);
1381 psid = HeapAlloc(GetProcessHeap(), 0, sid_size);
1382 domain = HeapAlloc(GetProcessHeap(), 0, domain_size);
1384 /* try NULL account name */
1385 ret = LookupAccountNameA(NULL, NULL, psid, &sid_size, domain, &domain_size, &sid_use);
1386 get_sid_info(psid, &account, &sid_dom);
1387 ok(ret, "Failed to lookup account name\n");
1390 ok(!lstrcmp(account, "BUILTIN"), "Expected BUILTIN, got %s\n", account);
1391 ok(!lstrcmp(domain, "BUILTIN"), "Expected BUILTIN, got %s\n", domain);
1392 ok(sid_use == SidTypeDomain, "Expected SidTypeDomain, got %d\n", SidTypeDomain);
1395 /* try an invalid account name */
1396 SetLastError(0xdeadbeef);
1399 ret = LookupAccountNameA(NULL, "oogabooga", NULL, &sid_size, NULL, &domain_size, &sid_use);
1400 ok(!ret, "Expected 0, got %d\n", ret);
1403 ok(GetLastError() == ERROR_NONE_MAPPED,
1404 "Expected ERROR_NONE_MAPPED, got %d\n", GetLastError());
1405 ok(sid_size == 0, "Expected 0, got %d\n", sid_size);
1406 ok(domain_size == 0, "Expected 0, got %d\n", domain_size);
1409 HeapFree(GetProcessHeap(), 0, psid);
1410 HeapFree(GetProcessHeap(), 0, domain);
1413 #define TEST_GRANTED_ACCESS(a,b) test_granted_access(a,b,__LINE__)
1414 static void test_granted_access(HANDLE handle, ACCESS_MASK access, int line)
1416 OBJECT_BASIC_INFORMATION obj_info;
1419 if (!pNtQueryObject)
1421 skip_(__FILE__, line)("Not NT platform - skipping tests\n");
1425 status = pNtQueryObject( handle, ObjectBasicInformation, &obj_info,
1426 sizeof(obj_info), NULL );
1427 ok_(__FILE__, line)(!status, "NtQueryObject with err: %08x\n", status);
1428 ok_(__FILE__, line)(obj_info.GrantedAccess == access, "Gratned access should "
1429 "be 0x%08x, instead of 0x%08x\n", access, obj_info.GrantedAccess);
1432 #define CHECK_SET_SECURITY(o,i,e) \
1436 SetLastError( 0xdeadbeef ); \
1437 res = SetKernelObjectSecurity( o, i, SecurityDescriptor ); \
1438 err = GetLastError(); \
1439 if (e == ERROR_SUCCESS) \
1440 ok(res, "SetKernelObjectSecurity failed with %d\n", err); \
1442 ok(!res && err == e, "SetKernelObjectSecurity should have failed " \
1443 "with %s, instead of %d\n", #e, err); \
1446 static void test_process_security(void)
1449 char owner[32], group[32];
1450 PSID AdminSid = NULL, UsersSid = NULL;
1452 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL;
1453 char buffer[MAX_PATH];
1454 PROCESS_INFORMATION info;
1455 STARTUPINFOA startup;
1456 SECURITY_ATTRIBUTES psa;
1457 HANDLE token, event;
1460 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
1461 res = InitializeAcl(Acl, 256, ACL_REVISION);
1462 if (!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1464 skip("ACLs not implemented - skipping tests\n");
1467 ok(res, "InitializeAcl failed with error %d\n", GetLastError());
1469 /* get owner from the token we might be running as a user not admin */
1470 res = OpenProcessToken( GetCurrentProcess(), MAXIMUM_ALLOWED, &token );
1471 ok(res, "OpenProcessToken failed with error %d\n", GetLastError());
1474 res = GetTokenInformation( token, TokenOwner, owner, sizeof(owner), &tmp );
1475 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
1476 AdminSid = ((TOKEN_OWNER*)owner)->Owner;
1477 res = GetTokenInformation( token, TokenPrimaryGroup, group, sizeof(group), &tmp );
1478 ok(res, "GetTokenInformation failed with error %d\n", GetLastError());
1479 UsersSid = ((TOKEN_PRIMARY_GROUP*)group)->PrimaryGroup;
1481 CloseHandle( token );
1484 res = AddAccessDeniedAce(Acl, ACL_REVISION, PROCESS_VM_READ, AdminSid);
1485 ok(res, "AddAccessDeniedAce failed with error %d\n", GetLastError());
1486 res = AddAccessAllowedAce(Acl, ACL_REVISION, PROCESS_ALL_ACCESS, AdminSid);
1487 ok(res, "AddAccessAllowedAceEx failed with error %d\n", GetLastError());
1489 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
1490 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
1491 ok(res, "InitializeSecurityDescriptor failed with error %d\n", GetLastError());
1493 event = CreateEvent( NULL, TRUE, TRUE, "test_event" );
1494 ok(event != NULL, "CreateEvent %d\n", GetLastError());
1496 SecurityDescriptor->Revision = 0;
1497 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_UNKNOWN_REVISION );
1498 SecurityDescriptor->Revision = SECURITY_DESCRIPTOR_REVISION;
1500 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_INVALID_SECURITY_DESCR );
1501 CHECK_SET_SECURITY( event, GROUP_SECURITY_INFORMATION, ERROR_INVALID_SECURITY_DESCR );
1502 CHECK_SET_SECURITY( event, SACL_SECURITY_INFORMATION, ERROR_ACCESS_DENIED );
1503 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
1504 /* NULL DACL is valid and means default DACL from token */
1505 SecurityDescriptor->Control |= SE_DACL_PRESENT;
1506 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
1508 /* Set owner and group and dacl */
1509 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
1510 ok(res, "SetSecurityDescriptorOwner failed with error %d\n", GetLastError());
1511 CHECK_SET_SECURITY( event, OWNER_SECURITY_INFORMATION, ERROR_SUCCESS );
1512 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, FALSE);
1513 ok(res, "SetSecurityDescriptorGroup failed with error %d\n", GetLastError());
1514 CHECK_SET_SECURITY( event, GROUP_SECURITY_INFORMATION, ERROR_SUCCESS );
1515 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
1516 ok(res, "SetSecurityDescriptorDacl failed with error %d\n", GetLastError());
1517 CHECK_SET_SECURITY( event, DACL_SECURITY_INFORMATION, ERROR_SUCCESS );
1519 sprintf(buffer, "%s tests/security.c test", myARGV[0]);
1520 memset(&startup, 0, sizeof(startup));
1521 startup.cb = sizeof(startup);
1522 startup.dwFlags = STARTF_USESHOWWINDOW;
1523 startup.wShowWindow = SW_SHOWNORMAL;
1525 psa.nLength = sizeof(psa);
1526 psa.lpSecurityDescriptor = SecurityDescriptor;
1527 psa.bInheritHandle = TRUE;
1529 /* Doesn't matter what ACL say we should get full access for ourselves */
1530 ok(CreateProcessA( NULL, buffer, &psa, NULL, FALSE, 0, NULL, NULL, &startup, &info ),
1531 "CreateProcess with err:%d\n", GetLastError());
1532 TEST_GRANTED_ACCESS( info.hProcess, PROCESS_ALL_ACCESS );
1533 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1535 CloseHandle( event );
1536 HeapFree(GetProcessHeap(), 0, Acl);
1537 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
1540 static void test_process_security_child(void)
1542 HANDLE handle, handle1;
1546 handle = OpenProcess( PROCESS_TERMINATE, FALSE, GetCurrentProcessId() );
1547 ok(handle != NULL, "OpenProcess(PROCESS_TERMINATE) with err:%d\n", GetLastError());
1548 TEST_GRANTED_ACCESS( handle, PROCESS_TERMINATE );
1550 ok(DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
1551 &handle1, 0, TRUE, DUPLICATE_SAME_ACCESS ),
1552 "duplicating handle err:%d\n", GetLastError());
1553 TEST_GRANTED_ACCESS( handle1, PROCESS_TERMINATE );
1555 CloseHandle( handle1 );
1557 SetLastError( 0xdeadbeef );
1558 ret = DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
1559 &handle1, PROCESS_ALL_ACCESS, TRUE, 0 );
1560 err = GetLastError();
1562 ok(!ret && err == ERROR_ACCESS_DENIED, "duplicating handle should have failed "
1563 "with STATUS_ACCESS_DENIED, instead of err:%d\n", err);
1565 CloseHandle( handle );
1567 /* These two should fail - they are denied by ACL */
1568 handle = OpenProcess( PROCESS_VM_READ, FALSE, GetCurrentProcessId() );
1570 ok(handle == NULL, "OpenProcess(PROCESS_VM_READ) should have failed\n");
1571 handle = OpenProcess( PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId() );
1573 ok(handle == NULL, "OpenProcess(PROCESS_ALL_ACCESS) should have failed\n");
1575 /* Documented privilege elevation */
1576 ok(DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
1577 &handle, 0, TRUE, DUPLICATE_SAME_ACCESS ),
1578 "duplicating handle err:%d\n", GetLastError());
1579 TEST_GRANTED_ACCESS( handle, PROCESS_ALL_ACCESS );
1581 CloseHandle( handle );
1583 /* Same only explicitly asking for all access rights */
1584 ok(DuplicateHandle( GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(),
1585 &handle, PROCESS_ALL_ACCESS, TRUE, 0 ),
1586 "duplicating handle err:%d\n", GetLastError());
1587 TEST_GRANTED_ACCESS( handle, PROCESS_ALL_ACCESS );
1588 ok(DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
1589 &handle1, PROCESS_VM_READ, TRUE, 0 ),
1590 "duplicating handle err:%d\n", GetLastError());
1591 TEST_GRANTED_ACCESS( handle1, PROCESS_VM_READ );
1592 CloseHandle( handle1 );
1593 CloseHandle( handle );
1596 static void test_impersonation_level(void)
1598 HANDLE Token, ProcessToken;
1601 TOKEN_PRIVILEGES *Privileges;
1603 PRIVILEGE_SET *PrivilegeSet;
1609 pDuplicateTokenEx = (fnDuplicateTokenEx) GetProcAddress(hmod, "DuplicateTokenEx");
1610 if( !pDuplicateTokenEx ) {
1611 skip("DuplicateTokenEx is not available\n");
1614 SetLastError(0xdeadbeef);
1615 ret = ImpersonateSelf(SecurityAnonymous);
1616 if(!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1618 skip("ImpersonateSelf is not implemented\n");
1621 ok(ret, "ImpersonateSelf(SecurityAnonymous) failed with error %d\n", GetLastError());
1622 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
1623 ok(!ret, "OpenThreadToken should have failed\n");
1624 error = GetLastError();
1625 ok(error == ERROR_CANT_OPEN_ANONYMOUS, "OpenThreadToken on anonymous token should have returned ERROR_CANT_OPEN_ANONYMOUS instead of %d\n", error);
1626 /* can't perform access check when opening object against an anonymous impersonation token */
1628 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
1629 ok(error == ERROR_INVALID_HANDLE, "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE instead of %d\n", error);
1633 ret = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, &ProcessToken);
1634 ok(ret, "OpenProcessToken failed with error %d\n", GetLastError());
1636 ret = pDuplicateTokenEx(ProcessToken,
1637 TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_IMPERSONATE, NULL,
1638 SecurityAnonymous, TokenImpersonation, &Token);
1639 ok(ret, "DuplicateTokenEx failed with error %d\n", GetLastError());
1640 /* can't increase the impersonation level */
1641 ret = DuplicateToken(Token, SecurityIdentification, &Token2);
1642 error = GetLastError();
1643 ok(!ret && error == ERROR_BAD_IMPERSONATION_LEVEL,
1644 "Duplicating a token and increasing the impersonation level should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
1645 /* we can query anything from an anonymous token, including the user */
1646 ret = GetTokenInformation(Token, TokenUser, NULL, 0, &Size);
1647 error = GetLastError();
1648 ok(!ret && error == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenUser) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error);
1649 User = (TOKEN_USER *)HeapAlloc(GetProcessHeap(), 0, Size);
1650 ret = GetTokenInformation(Token, TokenUser, User, Size, &Size);
1651 ok(ret, "GetTokenInformation(TokenUser) failed with error %d\n", GetLastError());
1652 HeapFree(GetProcessHeap(), 0, User);
1654 /* PrivilegeCheck fails with SecurityAnonymous level */
1655 ret = GetTokenInformation(Token, TokenPrivileges, NULL, 0, &Size);
1656 error = GetLastError();
1657 ok(!ret && error == ERROR_INSUFFICIENT_BUFFER, "GetTokenInformation(TokenPrivileges) should have failed with ERROR_INSUFFICIENT_BUFFER instead of %d\n", error);
1658 Privileges = (TOKEN_PRIVILEGES *)HeapAlloc(GetProcessHeap(), 0, Size);
1659 ret = GetTokenInformation(Token, TokenPrivileges, Privileges, Size, &Size);
1660 ok(ret, "GetTokenInformation(TokenPrivileges) failed with error %d\n", GetLastError());
1662 PrivilegeSet = (PRIVILEGE_SET *)HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PRIVILEGE_SET, Privilege[Privileges->PrivilegeCount]));
1663 PrivilegeSet->PrivilegeCount = Privileges->PrivilegeCount;
1664 memcpy(PrivilegeSet->Privilege, Privileges->Privileges, PrivilegeSet->PrivilegeCount * sizeof(PrivilegeSet->Privilege[0]));
1665 PrivilegeSet->Control = PRIVILEGE_SET_ALL_NECESSARY;
1666 HeapFree(GetProcessHeap(), 0, Privileges);
1668 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
1669 error = GetLastError();
1670 ok(!ret && error == ERROR_BAD_IMPERSONATION_LEVEL, "PrivilegeCheck for SecurityAnonymous token should have failed with ERROR_BAD_IMPERSONATION_LEVEL instead of %d\n", error);
1674 ret = ImpersonateSelf(SecurityIdentification);
1675 ok(ret, "ImpersonateSelf(SecurityIdentification) failed with error %d\n", GetLastError());
1676 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
1677 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
1679 /* can't perform access check when opening object against an identification impersonation token */
1680 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
1682 ok(error == ERROR_INVALID_HANDLE, "RegOpenKeyEx should have failed with ERROR_INVALID_HANDLE instead of %d\n", error);
1684 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
1685 ok(ret, "PrivilegeCheck for SecurityIdentification failed with error %d\n", GetLastError());
1689 ret = ImpersonateSelf(SecurityImpersonation);
1690 ok(ret, "ImpersonateSelf(SecurityImpersonation) failed with error %d\n", GetLastError());
1691 ret = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY_SOURCE | TOKEN_IMPERSONATE | TOKEN_ADJUST_DEFAULT, TRUE, &Token);
1692 ok(ret, "OpenThreadToken failed with error %d\n", GetLastError());
1693 error = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hkey);
1694 ok(error == ERROR_SUCCESS, "RegOpenKeyEx should have succeeded instead of failing with %d\n", error);
1696 ret = PrivilegeCheck(Token, PrivilegeSet, &AccessGranted);
1697 ok(ret, "PrivilegeCheck for SecurityImpersonation failed with error %d\n", GetLastError());
1701 CloseHandle(ProcessToken);
1703 HeapFree(GetProcessHeap(), 0, PrivilegeSet);
1706 static void test_SetEntriesInAcl(void)
1708 ACL *acl = (ACL*)0xdeadbeef;
1711 res = SetEntriesInAclW(0, NULL, NULL, &acl);
1712 if(res == ERROR_CALL_NOT_IMPLEMENTED)
1714 skip("SetEntriesInAclW is not implemented\n");
1717 ok(res == ERROR_SUCCESS, "SetEntriesInAclW failed: %u\n", res);
1718 ok(acl == NULL, "acl=%p, expected NULL\n", acl);
1721 static void test_GetNamedSecurityInfoA(void)
1723 PSECURITY_DESCRIPTOR pSecDesc;
1725 SECURITY_DESCRIPTOR_CONTROL control;
1728 BOOL owner_defaulted;
1729 BOOL group_defaulted;
1732 CHAR windows_dir[MAX_PATH];
1734 ret = GetWindowsDirectoryA(windows_dir, MAX_PATH);
1735 ok(ret, "GetWindowsDirectory failed with error %d\n", GetLastError());
1737 SetLastError(0xdeadbeef);
1738 error = GetNamedSecurityInfoA(windows_dir, SE_FILE_OBJECT,
1739 OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION,
1740 NULL, NULL, NULL, NULL, &pSecDesc);
1741 if (error != ERROR_SUCCESS && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1743 skip("GetNamedSecurityInfoA is not implemented\n");
1746 ok(!error, "GetNamedSecurityInfo failed with error %d\n", error);
1748 ret = GetSecurityDescriptorControl(pSecDesc, &control, &revision);
1749 ok(ret, "GetSecurityDescriptorControl failed with error %d\n", GetLastError());
1750 ok((control & (SE_SELF_RELATIVE|SE_DACL_PRESENT)) == (SE_SELF_RELATIVE|SE_DACL_PRESENT),
1751 "control (0x%x) doesn't have (SE_SELF_RELATIVE|SE_DACL_PRESENT) flags set\n", control);
1752 ok(revision == SECURITY_DESCRIPTOR_REVISION1, "revision was %d instead of 1\n", revision);
1753 ret = GetSecurityDescriptorOwner(pSecDesc, &owner, &owner_defaulted);
1754 ok(ret, "GetSecurityDescriptorOwner failed with error %d\n", GetLastError());
1755 ok(owner != NULL, "owner should not be NULL\n");
1756 ret = GetSecurityDescriptorGroup(pSecDesc, &group, &group_defaulted);
1757 ok(ret, "GetSecurityDescriptorGroup failed with error %d\n", GetLastError());
1758 ok(group != NULL, "group should not be NULL\n");
1761 static void test_ConvertSecurityStringSecurityDescriptor(void)
1764 PSECURITY_DESCRIPTOR pSD;
1766 SetLastError(0xdeadbeef);
1767 ret = ConvertStringSecurityDescriptorToSecurityDescriptor(
1768 "D:(A;;GA;;;WD)", 0xdeadbeef, &pSD, NULL);
1769 ok(!ret && GetLastError() == ERROR_UNKNOWN_REVISION,
1770 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_UNKNOWN_REVISION instead of %d\n",
1773 /* test ACE string type */
1774 SetLastError(0xdeadbeef);
1775 ret = ConvertStringSecurityDescriptorToSecurityDescriptor(
1776 "D:(A;;GA;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1777 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1780 SetLastError(0xdeadbeef);
1781 ret = ConvertStringSecurityDescriptorToSecurityDescriptor(
1782 "D:(D;;GA;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1783 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1786 SetLastError(0xdeadbeef);
1787 ret = ConvertStringSecurityDescriptorToSecurityDescriptor(
1788 "ERROR:(D;;GA;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1789 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
1790 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_PARAMETER instead of %d\n",
1793 /* test ACE string access rights */
1794 SetLastError(0xdeadbeef);
1795 ret = ConvertStringSecurityDescriptorToSecurityDescriptor(
1796 "D:(A;;GA;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1797 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1799 SetLastError(0xdeadbeef);
1800 ret = ConvertStringSecurityDescriptorToSecurityDescriptor(
1801 "D:(A;;GRGWGX;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1802 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1804 SetLastError(0xdeadbeef);
1805 ret = ConvertStringSecurityDescriptorToSecurityDescriptor(
1806 "D:(A;;RCSDWDWO;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1807 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1809 SetLastError(0xdeadbeef);
1810 ret = ConvertStringSecurityDescriptorToSecurityDescriptor(
1811 "D:(A;;RPWPCCDCLCSWLODTCR;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1813 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1815 SetLastError(0xdeadbeef);
1816 ret = ConvertStringSecurityDescriptorToSecurityDescriptor(
1817 "D:(A;;FAFRFWFX;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1819 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1821 SetLastError(0xdeadbeef);
1822 ret = ConvertStringSecurityDescriptorToSecurityDescriptor(
1823 "D:(A;;KAKRKWKX;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1825 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1827 SetLastError(0xdeadbeef);
1828 ret = ConvertStringSecurityDescriptorToSecurityDescriptor(
1829 "D:(A;;0xFFFFFFFF;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1830 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1833 /* test ACE string access right error case */
1834 SetLastError(0xdeadbeef);
1835 ret = ConvertStringSecurityDescriptorToSecurityDescriptor(
1836 "D:(A;;ROB;;;WD)", SDDL_REVISION_1, &pSD, NULL);
1838 ok(!ret && GetLastError() == ERROR_INVALID_ACL,
1839 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_ACL instead of %d\n",
1842 /* test ACE string SID */
1843 SetLastError(0xdeadbeef);
1844 ret = ConvertStringSecurityDescriptorToSecurityDescriptor(
1845 "D:(D;;GA;;;S-1-0-0)", SDDL_REVISION_1, &pSD, NULL);
1846 ok(ret, "ConvertStringSecurityDescriptorToSecurityDescriptor failed with error %d\n", GetLastError());
1849 SetLastError(0xdeadbeef);
1850 ret = ConvertStringSecurityDescriptorToSecurityDescriptor(
1851 "D:(D;;GA;;;Non existant account)", SDDL_REVISION_1, &pSD, NULL);
1853 ok(!ret && GetLastError() == ERROR_INVALID_ACL,
1854 "ConvertStringSecurityDescriptorToSecurityDescriptor should have failed with ERROR_INVALID_ACL instead of %d\n",
1858 START_TEST(security)
1865 test_process_security_child();
1871 test_FileSecurity();
1874 test_LookupAccountSid();
1875 test_LookupAccountName();
1876 test_process_security();
1877 test_impersonation_level();
1878 test_SetEntriesInAcl();
1879 test_GetNamedSecurityInfoA();
1880 test_ConvertSecurityStringSecurityDescriptor();