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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/test.h"
30 typedef VOID (WINAPI *fnBuildTrusteeWithSidA)( PTRUSTEEA pTrustee, PSID pSid );
31 typedef VOID (WINAPI *fnBuildTrusteeWithNameA)( PTRUSTEEA pTrustee, LPSTR pName );
32 typedef VOID (WINAPI *fnBuildTrusteeWithObjectsAndNameA)( PTRUSTEEA pTrustee,
33 POBJECTS_AND_NAME_A pObjName,
34 SE_OBJECT_TYPE ObjectType,
36 LPSTR InheritedObjectTypeName,
38 typedef VOID (WINAPI *fnBuildTrusteeWithObjectsAndSidA)( PTRUSTEEA pTrustee,
39 POBJECTS_AND_SID pObjSid,
41 GUID* pInheritedObjectGuid,
43 typedef LPSTR (WINAPI *fnGetTrusteeNameA)( PTRUSTEEA pTrustee );
44 typedef BOOL (WINAPI *fnConvertSidToStringSidA)( PSID pSid, LPSTR *str );
45 typedef BOOL (WINAPI *fnConvertStringSidToSidA)( LPCSTR str, PSID pSid );
46 typedef BOOL (WINAPI *fnGetFileSecurityA)(LPCSTR, SECURITY_INFORMATION,
47 PSECURITY_DESCRIPTOR, DWORD, LPDWORD);
48 typedef DWORD (WINAPI *fnRtlAdjustPrivilege)(ULONG,BOOLEAN,BOOLEAN,PBOOLEAN);
52 fnBuildTrusteeWithSidA pBuildTrusteeWithSidA;
53 fnBuildTrusteeWithNameA pBuildTrusteeWithNameA;
54 fnBuildTrusteeWithObjectsAndNameA pBuildTrusteeWithObjectsAndNameA;
55 fnBuildTrusteeWithObjectsAndSidA pBuildTrusteeWithObjectsAndSidA;
56 fnGetTrusteeNameA pGetTrusteeNameA;
57 fnConvertSidToStringSidA pConvertSidToStringSidA;
58 fnConvertStringSidToSidA pConvertStringSidToSidA;
59 fnGetFileSecurityA pGetFileSecurityA;
60 fnRtlAdjustPrivilege pRtlAdjustPrivilege;
64 SID_IDENTIFIER_AUTHORITY auth;
68 static void init(void)
70 hmod = GetModuleHandle("advapi32.dll");
73 static void test_sid(void)
75 struct sidRef refs[] = {
76 { { {0x00,0x00,0x33,0x44,0x55,0x66} }, "S-1-860116326-1" },
77 { { {0x00,0x00,0x01,0x02,0x03,0x04} }, "S-1-16909060-1" },
78 { { {0x00,0x00,0x00,0x01,0x02,0x03} }, "S-1-66051-1" },
79 { { {0x00,0x00,0x00,0x00,0x01,0x02} }, "S-1-258-1" },
80 { { {0x00,0x00,0x00,0x00,0x00,0x02} }, "S-1-2-1" },
81 { { {0x00,0x00,0x00,0x00,0x00,0x0c} }, "S-1-12-1" },
83 const char noSubAuthStr[] = "S-1-5";
89 pConvertSidToStringSidA = (fnConvertSidToStringSidA)
90 GetProcAddress( hmod, "ConvertSidToStringSidA" );
91 if( !pConvertSidToStringSidA )
93 pConvertStringSidToSidA = (fnConvertStringSidToSidA)
94 GetProcAddress( hmod, "ConvertStringSidToSidA" );
95 if( !pConvertStringSidToSidA )
98 r = pConvertStringSidToSidA( NULL, NULL );
99 ok( !r, "expected failure with NULL parameters\n" );
100 if( GetLastError() == ERROR_CALL_NOT_IMPLEMENTED )
102 ok( GetLastError() == ERROR_INVALID_PARAMETER,
103 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %ld\n",
106 r = pConvertStringSidToSidA( refs[0].refStr, NULL );
107 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
108 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %ld\n",
111 r = pConvertStringSidToSidA( NULL, &str );
112 ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
113 "expected GetLastError() is ERROR_INVALID_PARAMETER, got %ld\n",
116 r = pConvertStringSidToSidA( noSubAuthStr, &psid );
118 "expected failure with no sub authorities\n" );
119 ok( GetLastError() == ERROR_INVALID_SID,
120 "expected GetLastError() is ERROR_INVALID_SID, got %ld\n",
123 for( i = 0; i < sizeof(refs) / sizeof(refs[0]); i++ )
127 r = AllocateAndInitializeSid( &refs[i].auth, 1,1,0,0,0,0,0,0,0,
129 ok( r, "failed to allocate sid\n" );
130 r = pConvertSidToStringSidA( psid, &str );
131 ok( r, "failed to convert sid\n" );
134 ok( !strcmp( str, refs[i].refStr ),
135 "incorrect sid, expected %s, got %s\n", refs[i].refStr, str );
141 r = pConvertStringSidToSidA( refs[i].refStr, &psid );
142 ok( r, "failed to parse sid string\n" );
145 !memcmp( pisid->IdentifierAuthority.Value, refs[i].auth.Value,
146 sizeof(refs[i].auth) ),
147 "string sid %s didn't parse to expected value\n"
148 "(got 0x%04x%08lx, expected 0x%04x%08lx)\n",
150 MAKEWORD( pisid->IdentifierAuthority.Value[1],
151 pisid->IdentifierAuthority.Value[0] ),
152 MAKELONG( MAKEWORD( pisid->IdentifierAuthority.Value[5],
153 pisid->IdentifierAuthority.Value[4] ),
154 MAKEWORD( pisid->IdentifierAuthority.Value[3],
155 pisid->IdentifierAuthority.Value[2] ) ),
156 MAKEWORD( refs[i].auth.Value[1], refs[i].auth.Value[0] ),
157 MAKELONG( MAKEWORD( refs[i].auth.Value[5], refs[i].auth.Value[4] ),
158 MAKEWORD( refs[i].auth.Value[3], refs[i].auth.Value[2] ) ) );
164 static void test_trustee(void)
166 GUID ObjectType = {0x12345678, 0x1234, 0x5678, {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}};
167 GUID InheritedObjectType = {0x23456789, 0x2345, 0x6786, {0x2, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}};
169 OBJECTS_AND_NAME_ oan;
173 char szObjectTypeName[] = "ObjectTypeName";
174 char szInheritedObjectTypeName[] = "InheritedObjectTypeName";
175 char szTrusteeName[] = "szTrusteeName";
176 SID_IDENTIFIER_AUTHORITY auth = { {0x11,0x22,0,0,0, 0} };
178 memset( &ZeroGuid, 0x00, sizeof (ZeroGuid) );
180 pBuildTrusteeWithSidA = (fnBuildTrusteeWithSidA)
181 GetProcAddress( hmod, "BuildTrusteeWithSidA" );
182 pBuildTrusteeWithNameA = (fnBuildTrusteeWithNameA)
183 GetProcAddress( hmod, "BuildTrusteeWithNameA" );
184 pBuildTrusteeWithObjectsAndNameA = (fnBuildTrusteeWithObjectsAndNameA)
185 GetProcAddress (hmod, "BuildTrusteeWithObjectsAndNameA" );
186 pBuildTrusteeWithObjectsAndSidA = (fnBuildTrusteeWithObjectsAndSidA)
187 GetProcAddress (hmod, "BuildTrusteeWithObjectsAndSidA" );
188 pGetTrusteeNameA = (fnGetTrusteeNameA)
189 GetProcAddress (hmod, "GetTrusteeNameA" );
190 if( !pBuildTrusteeWithSidA || !pBuildTrusteeWithNameA ||
191 !pBuildTrusteeWithObjectsAndNameA || !pBuildTrusteeWithObjectsAndSidA ||
195 if ( ! AllocateAndInitializeSid( &auth, 1, 42, 0,0,0,0,0,0,0,&psid ) )
197 trace( "failed to init SID\n" );
201 /* test BuildTrusteeWithSidA */
202 memset( &trustee, 0xff, sizeof trustee );
203 pBuildTrusteeWithSidA( &trustee, psid );
205 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
206 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
207 "MultipleTrusteeOperation wrong\n");
208 ok( trustee.TrusteeForm == TRUSTEE_IS_SID, "TrusteeForm wrong\n");
209 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
210 ok( trustee.ptstrName == (LPSTR) psid, "ptstrName wrong\n" );
212 /* test BuildTrusteeWithObjectsAndSidA (test 1) */
213 memset( &trustee, 0xff, sizeof trustee );
214 memset( &oas, 0xff, sizeof(oas) );
215 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, &ObjectType,
216 &InheritedObjectType, psid);
218 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
219 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
220 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
221 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
222 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
224 ok(oas.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
225 ok(!memcmp(&oas.ObjectTypeGuid, &ObjectType, sizeof(GUID)), "ObjectTypeGuid wrong\n");
226 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
227 ok(oas.pSid == psid, "pSid wrong\n");
229 /* test GetTrusteeNameA */
230 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oas, "GetTrusteeName returned wrong value\n");
232 /* test BuildTrusteeWithObjectsAndSidA (test 2) */
233 memset( &trustee, 0xff, sizeof trustee );
234 memset( &oas, 0xff, sizeof(oas) );
235 pBuildTrusteeWithObjectsAndSidA(&trustee, &oas, NULL,
236 &InheritedObjectType, psid);
238 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
239 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
240 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_SID, "TrusteeForm wrong\n");
241 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
242 ok(trustee.ptstrName == (LPSTR)&oas, "ptstrName wrong\n");
244 ok(oas.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
245 ok(!memcmp(&oas.ObjectTypeGuid, &ZeroGuid, sizeof(GUID)), "ObjectTypeGuid wrong\n");
246 ok(!memcmp(&oas.InheritedObjectTypeGuid, &InheritedObjectType, sizeof(GUID)), "InheritedObjectTypeGuid wrong\n");
247 ok(oas.pSid == psid, "pSid wrong\n");
251 /* test BuildTrusteeWithNameA */
252 memset( &trustee, 0xff, sizeof trustee );
253 pBuildTrusteeWithNameA( &trustee, szTrusteeName );
255 ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
256 ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE,
257 "MultipleTrusteeOperation wrong\n");
258 ok( trustee.TrusteeForm == TRUSTEE_IS_NAME, "TrusteeForm wrong\n");
259 ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
260 ok( trustee.ptstrName == szTrusteeName, "ptstrName wrong\n" );
262 /* test BuildTrusteeWithObjectsAndNameA (test 1) */
263 memset( &trustee, 0xff, sizeof trustee );
264 memset( &oan, 0xff, sizeof(oan) );
265 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
266 szInheritedObjectTypeName, szTrusteeName);
268 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
269 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
270 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
271 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
272 ok(trustee.ptstrName == (LPTSTR)&oan, "ptstrName wrong\n");
274 ok(oan.ObjectsPresent == (ACE_OBJECT_TYPE_PRESENT | ACE_INHERITED_OBJECT_TYPE_PRESENT), "ObjectsPresent wrong\n");
275 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
276 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
277 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
279 /* test GetTrusteeNameA */
280 ok(pGetTrusteeNameA(&trustee) == (LPSTR)&oan, "GetTrusteeName returned wrong value\n");
282 /* test BuildTrusteeWithObjectsAndNameA (test 2) */
283 memset( &trustee, 0xff, sizeof trustee );
284 memset( &oan, 0xff, sizeof(oan) );
285 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, NULL,
286 szInheritedObjectTypeName, szTrusteeName);
288 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
289 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
290 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
291 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
292 ok(trustee.ptstrName == (LPSTR)&oan, "ptstrName wrong\n");
294 ok(oan.ObjectsPresent == ACE_INHERITED_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
295 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
296 ok(oan.InheritedObjectTypeName == szInheritedObjectTypeName, "InheritedObjectTypeName wrong\n");
297 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
299 /* test BuildTrusteeWithObjectsAndNameA (test 3) */
300 memset( &trustee, 0xff, sizeof trustee );
301 memset( &oan, 0xff, sizeof(oan) );
302 pBuildTrusteeWithObjectsAndNameA(&trustee, &oan, SE_KERNEL_OBJECT, szObjectTypeName,
303 NULL, szTrusteeName);
305 ok(trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
306 ok(trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, "MultipleTrusteeOperation wrong\n");
307 ok(trustee.TrusteeForm == TRUSTEE_IS_OBJECTS_AND_NAME, "TrusteeForm wrong\n");
308 ok(trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
309 ok(trustee.ptstrName == (LPTSTR)&oan, "ptstrName wrong\n");
311 ok(oan.ObjectsPresent == ACE_OBJECT_TYPE_PRESENT, "ObjectsPresent wrong\n");
312 ok(oan.ObjectType == SE_KERNEL_OBJECT, "ObjectType wrong\n");
313 ok(oan.InheritedObjectTypeName == NULL, "InheritedObjectTypeName wrong\n");
314 ok(oan.ptstrName == szTrusteeName, "szTrusteeName wrong\n");
317 /* If the first isn't defined, assume none is */
318 #ifndef SE_MIN_WELL_KNOWN_PRIVILEGE
319 #define SE_MIN_WELL_KNOWN_PRIVILEGE 2L
320 #define SE_CREATE_TOKEN_PRIVILEGE 2L
321 #define SE_ASSIGNPRIMARYTOKEN_PRIVILEGE 3L
322 #define SE_LOCK_MEMORY_PRIVILEGE 4L
323 #define SE_INCREASE_QUOTA_PRIVILEGE 5L
324 #define SE_MACHINE_ACCOUNT_PRIVILEGE 6L
325 #define SE_TCB_PRIVILEGE 7L
326 #define SE_SECURITY_PRIVILEGE 8L
327 #define SE_TAKE_OWNERSHIP_PRIVILEGE 9L
328 #define SE_LOAD_DRIVER_PRIVILEGE 10L
329 #define SE_SYSTEM_PROFILE_PRIVILEGE 11L
330 #define SE_SYSTEMTIME_PRIVILEGE 12L
331 #define SE_PROF_SINGLE_PROCESS_PRIVILEGE 13L
332 #define SE_INC_BASE_PRIORITY_PRIVILEGE 14L
333 #define SE_CREATE_PAGEFILE_PRIVILEGE 15L
334 #define SE_CREATE_PERMANENT_PRIVILEGE 16L
335 #define SE_BACKUP_PRIVILEGE 17L
336 #define SE_RESTORE_PRIVILEGE 18L
337 #define SE_SHUTDOWN_PRIVILEGE 19L
338 #define SE_DEBUG_PRIVILEGE 20L
339 #define SE_AUDIT_PRIVILEGE 21L
340 #define SE_SYSTEM_ENVIRONMENT_PRIVILEGE 22L
341 #define SE_CHANGE_NOTIFY_PRIVILLEGE 23L
342 #define SE_REMOTE_SHUTDOWN_PRIVILEGE 24L
343 #define SE_UNDOCK_PRIVILEGE 25L
344 #define SE_SYNC_AGENT_PRIVILEGE 26L
345 #define SE_ENABLE_DELEGATION_PRIVILEGE 27L
346 #define SE_MANAGE_VOLUME_PRIVILEGE 28L
347 #define SE_IMPERSONATE_PRIVILEGE 29L
348 #define SE_CREATE_GLOBAL_PRIVILEGE 30L
349 #define SE_MAX_WELL_KNOWN_PRIVILEGE SE_CREATE_GLOBAL_PRIVILEGE
350 #endif /* ndef SE_MIN_WELL_KNOWN_PRIVILEGE */
352 static void test_allocateLuid(void)
354 BOOL (WINAPI *pAllocateLocallyUniqueId)(PLUID);
358 pAllocateLocallyUniqueId = (void*)GetProcAddress(hmod, "AllocateLocallyUniqueId");
359 if (!pAllocateLocallyUniqueId) return;
361 ret = pAllocateLocallyUniqueId(&luid1);
362 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
366 "AllocateLocallyUniqueId failed: %ld\n", GetLastError());
367 ret = pAllocateLocallyUniqueId(&luid2);
369 "AllocateLocallyUniqueId failed: %ld\n", GetLastError());
370 ok(luid1.LowPart > SE_MAX_WELL_KNOWN_PRIVILEGE || luid1.HighPart != 0,
371 "AllocateLocallyUniqueId returned a well-known LUID\n");
372 ok(luid1.LowPart != luid2.LowPart || luid1.HighPart != luid2.HighPart,
373 "AllocateLocallyUniqueId returned non-unique LUIDs\n");
374 ret = pAllocateLocallyUniqueId(NULL);
375 ok( !ret && GetLastError() == ERROR_NOACCESS,
376 "AllocateLocallyUniqueId(NULL) didn't return ERROR_NOACCESS: %ld\n",
380 static void test_lookupPrivilegeName(void)
382 BOOL (WINAPI *pLookupPrivilegeNameA)(LPCSTR, PLUID, LPSTR, LPDWORD);
383 char buf[MAX_PATH]; /* arbitrary, seems long enough */
384 DWORD cchName = sizeof(buf);
385 LUID luid = { 0, 0 };
389 /* check whether it's available first */
390 pLookupPrivilegeNameA = (void*)GetProcAddress(hmod, "LookupPrivilegeNameA");
391 if (!pLookupPrivilegeNameA) return;
392 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
393 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
394 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
397 /* check with a short buffer */
399 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
400 ret = pLookupPrivilegeNameA(NULL, &luid, NULL, &cchName);
401 ok( !ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
402 "LookupPrivilegeNameA didn't fail with ERROR_INSUFFICIENT_BUFFER: %ld\n",
404 ok(cchName == strlen("SeCreateTokenPrivilege") + 1,
405 "LookupPrivilegeNameA returned an incorrect required length for\n"
406 "SeCreateTokenPrivilege (got %ld, expected %d)\n", cchName,
407 strlen("SeCreateTokenPrivilege") + 1);
408 /* check a known value and its returned length on success */
409 cchName = sizeof(buf);
410 ok(pLookupPrivilegeNameA(NULL, &luid, buf, &cchName) &&
411 cchName == strlen("SeCreateTokenPrivilege"),
412 "LookupPrivilegeNameA returned an incorrect output length for\n"
413 "SeCreateTokenPrivilege (got %ld, expected %d)\n", cchName,
414 (int)strlen("SeCreateTokenPrivilege"));
415 /* check known values */
416 for (i = SE_MIN_WELL_KNOWN_PRIVILEGE; i < SE_MAX_WELL_KNOWN_PRIVILEGE; i++)
419 cchName = sizeof(buf);
420 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
421 ok( ret || GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
422 "LookupPrivilegeNameA(0.%ld) failed: %ld\n", i, GetLastError());
424 /* check a bogus LUID */
425 luid.LowPart = 0xdeadbeef;
426 cchName = sizeof(buf);
427 ret = pLookupPrivilegeNameA(NULL, &luid, buf, &cchName);
428 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
429 "LookupPrivilegeNameA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %ld\n",
431 /* check on a bogus system */
432 luid.LowPart = SE_CREATE_TOKEN_PRIVILEGE;
433 cchName = sizeof(buf);
434 ret = pLookupPrivilegeNameA("b0gu5.Nam3", &luid, buf, &cchName);
435 ok( !ret && GetLastError() == RPC_S_SERVER_UNAVAILABLE,
436 "LookupPrivilegeNameA didn't fail with RPC_S_SERVER_UNAVAILABLE: %ld\n",
446 static void test_lookupPrivilegeValue(void)
448 static const struct NameToLUID privs[] = {
449 { "SeCreateTokenPrivilege", SE_CREATE_TOKEN_PRIVILEGE },
450 { "SeAssignPrimaryTokenPrivilege", SE_ASSIGNPRIMARYTOKEN_PRIVILEGE },
451 { "SeLockMemoryPrivilege", SE_LOCK_MEMORY_PRIVILEGE },
452 { "SeIncreaseQuotaPrivilege", SE_INCREASE_QUOTA_PRIVILEGE },
453 { "SeMachineAccountPrivilege", SE_MACHINE_ACCOUNT_PRIVILEGE },
454 { "SeTcbPrivilege", SE_TCB_PRIVILEGE },
455 { "SeSecurityPrivilege", SE_SECURITY_PRIVILEGE },
456 { "SeTakeOwnershipPrivilege", SE_TAKE_OWNERSHIP_PRIVILEGE },
457 { "SeLoadDriverPrivilege", SE_LOAD_DRIVER_PRIVILEGE },
458 { "SeSystemProfilePrivilege", SE_SYSTEM_PROFILE_PRIVILEGE },
459 { "SeSystemtimePrivilege", SE_SYSTEMTIME_PRIVILEGE },
460 { "SeProfileSingleProcessPrivilege", SE_PROF_SINGLE_PROCESS_PRIVILEGE },
461 { "SeIncreaseBasePriorityPrivilege", SE_INC_BASE_PRIORITY_PRIVILEGE },
462 { "SeCreatePagefilePrivilege", SE_CREATE_PAGEFILE_PRIVILEGE },
463 { "SeCreatePermanentPrivilege", SE_CREATE_PERMANENT_PRIVILEGE },
464 { "SeBackupPrivilege", SE_BACKUP_PRIVILEGE },
465 { "SeRestorePrivilege", SE_RESTORE_PRIVILEGE },
466 { "SeShutdownPrivilege", SE_SHUTDOWN_PRIVILEGE },
467 { "SeDebugPrivilege", SE_DEBUG_PRIVILEGE },
468 { "SeAuditPrivilege", SE_AUDIT_PRIVILEGE },
469 { "SeSystemEnvironmentPrivilege", SE_SYSTEM_ENVIRONMENT_PRIVILEGE },
470 { "SeChangeNotifyPrivilege", SE_CHANGE_NOTIFY_PRIVILLEGE },
471 { "SeRemoteShutdownPrivilege", SE_REMOTE_SHUTDOWN_PRIVILEGE },
472 { "SeUndockPrivilege", SE_UNDOCK_PRIVILEGE },
473 { "SeSyncAgentPrivilege", SE_SYNC_AGENT_PRIVILEGE },
474 { "SeEnableDelegationPrivilege", SE_ENABLE_DELEGATION_PRIVILEGE },
475 { "SeManageVolumePrivilege", SE_MANAGE_VOLUME_PRIVILEGE },
476 { "SeImpersonatePrivilege", SE_IMPERSONATE_PRIVILEGE },
477 { "SeCreateGlobalPrivilege", SE_CREATE_GLOBAL_PRIVILEGE },
479 BOOL (WINAPI *pLookupPrivilegeValueA)(LPCSTR, LPCSTR, PLUID);
484 /* check whether it's available first */
485 pLookupPrivilegeValueA = (void*)GetProcAddress(hmod, "LookupPrivilegeValueA");
486 if (!pLookupPrivilegeValueA) return;
487 ret = pLookupPrivilegeValueA(NULL, "SeCreateTokenPrivilege", &luid);
488 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
491 /* check a bogus system name */
492 ret = pLookupPrivilegeValueA("b0gu5.Nam3", "SeCreateTokenPrivilege", &luid);
493 ok( !ret && GetLastError() == RPC_S_SERVER_UNAVAILABLE,
494 "LookupPrivilegeValueA didn't fail with RPC_S_SERVER_UNAVAILABLE: %ld\n",
496 /* check a NULL string */
497 ret = pLookupPrivilegeValueA(NULL, 0, &luid);
498 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
499 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %ld\n",
501 /* check a bogus privilege name */
502 ret = pLookupPrivilegeValueA(NULL, "SeBogusPrivilege", &luid);
503 ok( !ret && GetLastError() == ERROR_NO_SUCH_PRIVILEGE,
504 "LookupPrivilegeValueA didn't fail with ERROR_NO_SUCH_PRIVILEGE: %ld\n",
506 /* check case insensitive */
507 ret = pLookupPrivilegeValueA(NULL, "sEcREATEtOKENpRIVILEGE", &luid);
509 "LookupPrivilegeValueA(NULL, sEcREATEtOKENpRIVILEGE, &luid) failed: %ld\n",
511 for (i = 0; i < sizeof(privs) / sizeof(privs[0]); i++)
513 /* Not all privileges are implemented on all Windows versions, so
514 * don't worry if the call fails
516 if (pLookupPrivilegeValueA(NULL, privs[i].name, &luid))
518 ok(luid.LowPart == privs[i].lowPart,
519 "LookupPrivilegeValueA returned an invalid LUID for %s\n",
525 static void test_luid(void)
528 test_lookupPrivilegeName();
529 test_lookupPrivilegeValue();
532 static void test_FileSecurity(void)
534 char directory[MAX_PATH];
535 DWORD retval, outSize;
539 pGetFileSecurityA = (fnGetFileSecurityA)
540 GetProcAddress( hmod, "GetFileSecurityA" );
541 if( !pGetFileSecurityA )
544 retval = GetTempPathA(sizeof(directory), directory);
546 trace("GetTempPathA failed\n");
550 strcpy(directory, "\\Should not exist");
552 SetLastError(NO_ERROR);
553 result = pGetFileSecurityA( directory,OWNER_SECURITY_INFORMATION,buffer,0x40,&outSize);
554 ok(!result, "GetFileSecurityA should fail for not existing directories/files\n");
555 ok( (GetLastError() == ERROR_FILE_NOT_FOUND ) ||
556 (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) ,
557 "last error ERROR_FILE_NOT_FOUND / ERROR_CALL_NOT_IMPLEMENTED (98) "
558 "expected, got %ld\n", GetLastError());
561 static void test_AccessCheck(void)
563 PSID EveryoneSid = NULL, AdminSid = NULL, UsersSid = NULL;
565 SECURITY_DESCRIPTOR *SecurityDescriptor = NULL;
566 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = { SECURITY_WORLD_SID_AUTHORITY };
567 SID_IDENTIFIER_AUTHORITY SIDAuthNT = { SECURITY_NT_AUTHORITY };
568 GENERIC_MAPPING Mapping = { KEY_READ, KEY_WRITE, KEY_EXECUTE, KEY_ALL_ACCESS };
574 PRIVILEGE_SET *PrivSet;
579 NtDllModule = GetModuleHandle("ntdll.dll");
583 trace("not running on NT, skipping test\n");
586 pRtlAdjustPrivilege = (fnRtlAdjustPrivilege)
587 GetProcAddress(NtDllModule, "RtlAdjustPrivilege");
588 if (!pRtlAdjustPrivilege) return;
590 Acl = HeapAlloc(GetProcessHeap(), 0, 256);
591 res = InitializeAcl(Acl, 256, ACL_REVISION);
592 if(!res && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
594 trace("ACLs not implemented - skipping tests\n");
597 ok(res, "InitializeAcl failed with error %ld\n", GetLastError());
599 res = AllocateAndInitializeSid( &SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
600 ok(res, "AllocateAndInitializeSid failed with error %ld\n", GetLastError());
602 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
603 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdminSid);
604 ok(res, "AllocateAndInitializeSid failed with error %ld\n", GetLastError());
606 res = AllocateAndInitializeSid( &SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID,
607 DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0, &UsersSid);
608 ok(res, "AllocateAndInitializeSid failed with error %ld\n", GetLastError());
610 res = AddAccessAllowedAce(Acl, ACL_REVISION, KEY_READ, EveryoneSid);
611 ok(res, "AddAccessAllowedAceEx failed with error %ld\n", GetLastError());
613 res = AddAccessAllowedAce(Acl, ACL_REVISION, KEY_ALL_ACCESS, AdminSid);
614 ok(res, "AddAccessAllowedAceEx failed with error %ld\n", GetLastError());
616 SecurityDescriptor = HeapAlloc(GetProcessHeap(), 0, SECURITY_DESCRIPTOR_MIN_LENGTH);
618 res = InitializeSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
619 ok(res, "InitializeSecurityDescriptor failed with error %ld\n", GetLastError());
621 res = SetSecurityDescriptorDacl(SecurityDescriptor, TRUE, Acl, FALSE);
622 ok(res, "SetSecurityDescriptorDacl failed with error %ld\n", GetLastError());
624 res = SetSecurityDescriptorOwner(SecurityDescriptor, AdminSid, FALSE);
625 ok(res, "SetSecurityDescriptorOwner failed with error %ld\n", GetLastError());
627 res = SetSecurityDescriptorGroup(SecurityDescriptor, UsersSid, TRUE);
628 ok(res, "SetSecurityDescriptorGroup failed with error %ld\n", GetLastError());
630 PrivSetLen = FIELD_OFFSET(PRIVILEGE_SET, Privilege[16]);
631 PrivSet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, PrivSetLen);
632 PrivSet->PrivilegeCount = 16;
634 ImpersonateSelf(SecurityImpersonation);
636 pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, FALSE, TRUE, &Enabled);
638 ret = OpenThreadToken(GetCurrentThread(),
639 TOKEN_QUERY, TRUE, &Token);
640 ok(ret, "OpenThreadToken failed with error %ld\n", GetLastError());
642 ret = AccessCheck(SecurityDescriptor, Token, KEY_READ, &Mapping,
643 PrivSet, &PrivSetLen, &Access, &AccessStatus);
644 ok(ret, "AccessCheck failed with error %ld\n", GetLastError());
645 ok(AccessStatus && (Access == KEY_READ),
646 "AccessCheck failed to grant access with error %ld\n",
649 ret = AccessCheck(SecurityDescriptor, Token, MAXIMUM_ALLOWED, &Mapping,
650 PrivSet, &PrivSetLen, &Access, &AccessStatus);
651 ok(ret, "AccessCheck failed with error %ld\n", GetLastError());
653 "AccessCheck failed to grant any access with error %ld\n",
655 trace("AccessCheck with MAXIMUM_ALLOWED got Access 0x%08lx\n", Access);
658 PrivSet->PrivilegeCount = 16;
659 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
660 PrivSet, &PrivSetLen, &Access, &AccessStatus);
661 ok(ret && !AccessStatus && GetLastError() == ERROR_PRIVILEGE_NOT_HELD,
662 "AccessCheck should have failed with ERROR_PRIVILEGE_NOT_HELD, instead of %ld\n",
665 ret = pRtlAdjustPrivilege(SE_SECURITY_PRIVILEGE, TRUE, TRUE, &Enabled);
669 PrivSet->PrivilegeCount = 16;
670 ret = AccessCheck(SecurityDescriptor, Token, ACCESS_SYSTEM_SECURITY, &Mapping,
671 PrivSet, &PrivSetLen, &Access, &AccessStatus);
672 ok(ret && AccessStatus && GetLastError() == 0,
673 "AccessCheck should have succeeded, error %ld\n",
675 ok(Access == ACCESS_SYSTEM_SECURITY,
676 "Access should be equal to ACCESS_SYSTEM_SECURITY instead of 0x%08lx\n",
680 trace("Couldn't get SE_SECURITY_PRIVILEGE (0x%08x), skipping ACCESS_SYSTEM_SECURITY test\n",
686 FreeSid(EveryoneSid);
691 HeapFree(GetProcessHeap(), 0, Acl);
692 HeapFree(GetProcessHeap(), 0, SecurityDescriptor);
693 HeapFree(GetProcessHeap(), 0, PrivSet);