Implement LookupPrivilegeValueW for local machine
[reactos.git] / reactos / lib / advapi32 / sec / misc.c
1 /* $Id: misc.c,v 1.19 2004/07/06 22:08:48 gvg Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/advapi32/sec/misc.c
6 * PURPOSE: Miscellaneous security functions
7 */
8
9 #define NTOS_MODE_USER
10 #include <ntos.h>
11 #include <windows.h>
12 #include <accctrl.h>
13
14 #define NDEBUG
15 #include <debug.h>
16
17
18 /*
19 * @implemented
20 */
21 BOOL STDCALL
22 AreAllAccessesGranted(DWORD GrantedAccess,
23 DWORD DesiredAccess)
24 {
25 return((BOOL)RtlAreAllAccessesGranted(GrantedAccess,
26 DesiredAccess));
27 }
28
29
30 /*
31 * @implemented
32 */
33 BOOL STDCALL
34 AreAnyAccessesGranted(DWORD GrantedAccess,
35 DWORD DesiredAccess)
36 {
37 return((BOOL)RtlAreAnyAccessesGranted(GrantedAccess,
38 DesiredAccess));
39 }
40
41
42 /******************************************************************************
43 * GetFileSecurityA [ADVAPI32.@]
44 *
45 * Obtains Specified information about the security of a file or directory.
46 *
47 * PARAMS
48 * lpFileName [I] Name of the file to get info for
49 * RequestedInformation [I] SE_ flags from "winnt.h"
50 * pSecurityDescriptor [O] Destination for security information
51 * nLength [I] Length of pSecurityDescriptor
52 * lpnLengthNeeded [O] Destination for length of returned security information
53 *
54 * RETURNS
55 * Success: TRUE. pSecurityDescriptor contains the requested information.
56 * Failure: FALSE. lpnLengthNeeded contains the required space to return the info.
57 *
58 * NOTES
59 * The information returned is constrained by the callers access rights and
60 * privileges.
61 *
62 * @unimplemented
63 */
64 BOOL WINAPI
65 GetFileSecurityA (LPCSTR lpFileName,
66 SECURITY_INFORMATION RequestedInformation,
67 PSECURITY_DESCRIPTOR pSecurityDescriptor,
68 DWORD nLength,
69 LPDWORD lpnLengthNeeded)
70 {
71 DPRINT1("GetFileSecurityA: stub\n");
72 return TRUE;
73 }
74
75 /*
76 * @unimplemented
77 */
78 BOOL WINAPI
79 GetFileSecurityW (LPCWSTR lpFileName,
80 SECURITY_INFORMATION RequestedInformation,
81 PSECURITY_DESCRIPTOR pSecurityDescriptor,
82 DWORD nLength, LPDWORD lpnLengthNeeded)
83 {
84 DPRINT1("GetFileSecurityW: stub\n");
85 return TRUE;
86 }
87
88 /*
89 * @implemented
90 */
91 BOOL STDCALL
92 GetKernelObjectSecurity(HANDLE Handle,
93 SECURITY_INFORMATION RequestedInformation,
94 PSECURITY_DESCRIPTOR pSecurityDescriptor,
95 DWORD nLength,
96 LPDWORD lpnLengthNeeded)
97 {
98 NTSTATUS Status;
99
100 Status = NtQuerySecurityObject(Handle,
101 RequestedInformation,
102 pSecurityDescriptor,
103 nLength,
104 lpnLengthNeeded);
105 if (!NT_SUCCESS(Status))
106 {
107 SetLastError(RtlNtStatusToDosError(Status));
108 return(FALSE);
109 }
110 return(TRUE);
111 }
112
113
114 /******************************************************************************
115 * SetFileSecurityW [ADVAPI32.@]
116 * Sets the security of a file or directory
117 *
118 * @unimplemented
119 */
120 BOOL STDCALL
121 SetFileSecurityW (LPCWSTR lpFileName,
122 SECURITY_INFORMATION RequestedInformation,
123 PSECURITY_DESCRIPTOR pSecurityDescriptor)
124 {
125 DPRINT1("SetFileSecurityW : stub\n");
126 return TRUE;
127 }
128
129
130 /******************************************************************************
131 * SetFileSecurityA [ADVAPI32.@]
132 * Sets the security of a file or directory
133 *
134 * @unimplemented
135 */
136 BOOL STDCALL
137 SetFileSecurityA (LPCSTR lpFileName,
138 SECURITY_INFORMATION RequestedInformation,
139 PSECURITY_DESCRIPTOR pSecurityDescriptor)
140 {
141 DPRINT("SetFileSecurityA : stub\n");
142 return TRUE;
143 }
144
145
146 /*
147 * @implemented
148 */
149 BOOL STDCALL
150 SetKernelObjectSecurity(HANDLE Handle,
151 SECURITY_INFORMATION SecurityInformation,
152 PSECURITY_DESCRIPTOR SecurityDescriptor)
153 {
154 NTSTATUS Status;
155
156 Status = NtSetSecurityObject(Handle,
157 SecurityInformation,
158 SecurityDescriptor);
159 if (!NT_SUCCESS(Status))
160 {
161 SetLastError(RtlNtStatusToDosError(Status));
162 return FALSE;
163 }
164 return TRUE;
165 }
166
167
168 /*
169 * @implemented
170 */
171 VOID STDCALL
172 MapGenericMask(PDWORD AccessMask,
173 PGENERIC_MAPPING GenericMapping)
174 {
175 RtlMapGenericMask(AccessMask,
176 GenericMapping);
177 }
178
179
180 /*
181 * @implemented
182 */
183 BOOL STDCALL
184 ImpersonateLoggedOnUser(HANDLE hToken)
185 {
186 SECURITY_QUALITY_OF_SERVICE Qos;
187 OBJECT_ATTRIBUTES ObjectAttributes;
188 HANDLE NewToken;
189 TOKEN_TYPE Type;
190 ULONG ReturnLength;
191 BOOL Duplicated;
192 NTSTATUS Status;
193
194 /* Get the token type */
195 Status = NtQueryInformationToken (hToken,
196 TokenType,
197 &Type,
198 sizeof(TOKEN_TYPE),
199 &ReturnLength);
200 if (!NT_SUCCESS(Status))
201 {
202 SetLastError (RtlNtStatusToDosError (Status));
203 return FALSE;
204 }
205
206 if (Type == TokenPrimary)
207 {
208 /* Create a duplicate impersonation token */
209 Qos.Length = sizeof(SECURITY_QUALITY_OF_SERVICE);
210 Qos.ImpersonationLevel = SecurityImpersonation;
211 Qos.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
212 Qos.EffectiveOnly = FALSE;
213
214 ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
215 ObjectAttributes.RootDirectory = NULL;
216 ObjectAttributes.ObjectName = NULL;
217 ObjectAttributes.Attributes = 0;
218 ObjectAttributes.SecurityDescriptor = NULL;
219 ObjectAttributes.SecurityQualityOfService = &Qos;
220
221 Status = NtDuplicateToken (hToken,
222 TOKEN_IMPERSONATE | TOKEN_QUERY,
223 &ObjectAttributes,
224 FALSE,
225 TokenImpersonation,
226 &NewToken);
227 if (!NT_SUCCESS(Status))
228 {
229 SetLastError (RtlNtStatusToDosError (Status));
230 return FALSE;
231 }
232
233 Duplicated = TRUE;
234 }
235 else
236 {
237 /* User the original impersonation token */
238 NewToken = hToken;
239 Duplicated = FALSE;
240 }
241
242 /* Impersonate the the current thread */
243 Status = NtSetInformationThread (NtCurrentThread (),
244 ThreadImpersonationToken,
245 &NewToken,
246 sizeof(HANDLE));
247
248 if (Duplicated == TRUE)
249 {
250 NtClose (NewToken);
251 }
252
253 if (!NT_SUCCESS(Status))
254 {
255 SetLastError (RtlNtStatusToDosError (Status));
256 return FALSE;
257 }
258
259 return TRUE;
260 }
261
262
263 /*
264 * @implemented
265 */
266 BOOL STDCALL
267 ImpersonateSelf(SECURITY_IMPERSONATION_LEVEL ImpersonationLevel)
268 {
269 NTSTATUS Status;
270
271 Status = RtlImpersonateSelf(ImpersonationLevel);
272 if (!NT_SUCCESS(Status))
273 {
274 SetLastError(RtlNtStatusToDosError(Status));
275 return FALSE;
276 }
277 return TRUE;
278 }
279
280
281 /*
282 * @implemented
283 */
284 BOOL STDCALL
285 RevertToSelf(VOID)
286 {
287 NTSTATUS Status;
288 HANDLE Token = NULL;
289
290 Status = NtSetInformationThread(NtCurrentThread(),
291 ThreadImpersonationToken,
292 &Token,
293 sizeof(HANDLE));
294 if (!NT_SUCCESS(Status))
295 {
296 SetLastError(RtlNtStatusToDosError(Status));
297 return FALSE;
298 }
299 return TRUE;
300 }
301
302
303 /******************************************************************************
304 * GetUserNameA [ADVAPI32.@]
305 *
306 * Get the current user name.
307 *
308 * PARAMS
309 * lpszName [O] Destination for the user name.
310 * lpSize [I/O] Size of lpszName.
311 *
312 * RETURNS
313 * Success: The length of the user name, including terminating NUL.
314 * Failure: ERROR_MORE_DATA if *lpSize is too small.
315 *
316 * @unimplemented
317 */
318 BOOL WINAPI
319 GetUserNameA( LPSTR lpszName, LPDWORD lpSize )
320 {
321 // size_t len;
322 // char name[] = { "Administrator" };
323
324 /* We need to include the null character when determining the size of the buffer. */
325 // len = strlen(name) + 1;
326 // if (len > *lpSize)
327 // {
328 // SetLastError(ERROR_MORE_DATA);
329 // *lpSize = len;
330 // return 0;
331 // }
332
333 // *lpSize = len;
334 // strcpy(lpszName, name);
335 DPRINT1("GetUserNameA: stub\n");
336 return TRUE;
337 }
338
339 /******************************************************************************
340 * GetUserNameW [ADVAPI32.@]
341 *
342 * See GetUserNameA.
343 *
344 * @unimplemented
345 */
346 BOOL WINAPI
347 GetUserNameW( LPWSTR lpszName, LPDWORD lpSize )
348 {
349 // char name[] = { "Administrator" };
350
351 // DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
352
353 // if (len > *lpSize)
354 // {
355 // SetLastError(ERROR_MORE_DATA);
356 // *lpSize = len;
357 // return FALSE;
358 // }
359
360 // *lpSize = len;
361 // MultiByteToWideChar( CP_ACP, 0, name, -1, lpszName, len );
362 DPRINT1("GetUserNameW: stub\n");
363 return TRUE;
364 }
365
366
367 /******************************************************************************
368 * LookupAccountSidA [ADVAPI32.@]
369 *
370 * @unimplemented
371 */
372 BOOL STDCALL
373 LookupAccountSidA (LPCSTR lpSystemName,
374 PSID lpSid,
375 LPSTR lpName,
376 LPDWORD cchName,
377 LPSTR lpReferencedDomainName,
378 LPDWORD cchReferencedDomainName,
379 PSID_NAME_USE peUse)
380 {
381 DPRINT1("LookupAccountSidA is unimplemented, but returns success\n");
382 return TRUE;
383 }
384
385
386 /******************************************************************************
387 * LookupAccountSidW [ADVAPI32.@]
388 *
389 * @unimplemented
390 */
391 BOOL STDCALL
392 LookupAccountSidW (LPCWSTR lpSystemName,
393 PSID lpSid,
394 LPWSTR lpName,
395 LPDWORD cchName,
396 LPWSTR lpReferencedDomainName,
397 LPDWORD cchReferencedDomainName,
398 PSID_NAME_USE peUse)
399 {
400 DPRINT1("LookupAccountSidW is unimplemented, but returns success\n");
401 return TRUE;
402 }
403
404
405 /**********************************************************************
406 * LookupPrivilegeValueA EXPORTED
407 *
408 * @implemented
409 */
410 BOOL STDCALL
411 LookupPrivilegeValueA (LPCSTR lpSystemName,
412 LPCSTR lpName,
413 PLUID lpLuid)
414 {
415 UNICODE_STRING SystemName;
416 UNICODE_STRING Name;
417 BOOL Result;
418
419 /* Remote system? */
420 if (lpSystemName != NULL)
421 {
422 RtlCreateUnicodeStringFromAsciiz (&SystemName,
423 (LPSTR)lpSystemName);
424 }
425
426 /* Check the privilege name is not NULL */
427 if (lpName == NULL)
428 {
429 SetLastError (ERROR_INVALID_PARAMETER);
430 return FALSE;
431 }
432
433 RtlCreateUnicodeStringFromAsciiz (&Name,
434 (LPSTR)lpName);
435
436 Result = LookupPrivilegeValueW ((lpSystemName != NULL) ? SystemName.Buffer : NULL,
437 Name.Buffer,
438 lpLuid);
439
440 RtlFreeUnicodeString (&Name);
441
442 /* Remote system? */
443 if (lpSystemName != NULL)
444 {
445 RtlFreeUnicodeString (&SystemName);
446 }
447
448 return Result;
449 }
450
451
452 /**********************************************************************
453 * LookupPrivilegeValueW EXPORTED
454 *
455 * @unimplemented
456 */
457 BOOL STDCALL
458 LookupPrivilegeValueW (LPCWSTR SystemName,
459 LPCWSTR PrivName,
460 PLUID Luid)
461 {
462 static const WCHAR * const DefaultPrivNames[] =
463 {
464 L"SeCreateTokenPrivilege",
465 L"SeAssignPrimaryTokenPrivilege",
466 L"SeLockMemoryPrivilege",
467 L"SeIncreaseQuotaPrivilege",
468 L"SeUnsolicitedInputPrivilege",
469 L"SeMachineAccountPrivilege",
470 L"SeTcbPrivilege",
471 L"SeSecurityPrivilege",
472 L"SeTakeOwnershipPrivilege",
473 L"SeLoadDriverPrivilege",
474 L"SeSystemProfilePrivilege",
475 L"SeSystemtimePrivilege",
476 L"SeProfileSingleProcessPrivilege",
477 L"SeIncreaseBasePriorityPrivilege",
478 L"SeCreatePagefilePrivilege",
479 L"SeCreatePermanentPrivilege",
480 L"SeBackupPrivilege",
481 L"SeRestorePrivilege",
482 L"SeShutdownPrivilege",
483 L"SeDebugPrivilege",
484 L"SeAuditPrivilege",
485 L"SeSystemEnvironmentPrivilege",
486 L"SeChangeNotifyPrivilege",
487 L"SeRemoteShutdownPrivilege",
488 L"SeUndockPrivilege",
489 L"SeSyncAgentPrivilege",
490 L"SeEnableDelegationPrivilege",
491 L"SeManageVolumePrivilege",
492 L"SeImpersonatePrivilege",
493 L"SeCreateGlobalPrivilege"
494 };
495 unsigned Priv;
496
497 if (NULL != SystemName && L'\0' != *SystemName)
498 {
499 DPRINT1("LookupPrivilegeValueW: not implemented for remote system\n");
500 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
501 return FALSE;
502 }
503
504 for (Priv = 0; Priv < sizeof(DefaultPrivNames) / sizeof(DefaultPrivNames[0]); Priv++)
505 {
506 if (0 == wcscmp(PrivName, DefaultPrivNames[Priv]))
507 {
508 Luid->LowPart = Priv + 1;
509 Luid->HighPart = 0;
510 return TRUE;
511 }
512 }
513
514 DPRINT1("LookupPrivilegeValueW: no such privilege %S\n", PrivName);
515 SetLastError(ERROR_NO_SUCH_PRIVILEGE);
516 return FALSE;
517 }
518
519
520 /**********************************************************************
521 * LookupPrivilegeDisplayNameA EXPORTED
522 *
523 * @unimplemented
524 */
525 BOOL STDCALL
526 LookupPrivilegeDisplayNameA (LPCSTR lpSystemName,
527 LPCSTR lpName,
528 LPSTR lpDisplayName,
529 LPDWORD cbDisplayName,
530 LPDWORD lpLanguageId)
531 {
532 DPRINT1("LookupPrivilegeDisplayNameA: stub\n");
533 SetLastError (ERROR_CALL_NOT_IMPLEMENTED);
534 return FALSE;
535 }
536
537
538 /**********************************************************************
539 * LookupPrivilegeDisplayNameW EXPORTED
540 *
541 * @unimplemented
542 */
543 BOOL STDCALL
544 LookupPrivilegeDisplayNameW (LPCWSTR lpSystemName,
545 LPCWSTR lpName,
546 LPWSTR lpDisplayName,
547 LPDWORD cbDisplayName,
548 LPDWORD lpLanguageId)
549 {
550 DPRINT1("LookupPrivilegeDisplayNameW: stub\n");
551 SetLastError (ERROR_CALL_NOT_IMPLEMENTED);
552 return FALSE;
553 }
554
555
556 /**********************************************************************
557 * LookupPrivilegeNameA EXPORTED
558 *
559 * @unimplemented
560 */
561 BOOL STDCALL
562 LookupPrivilegeNameA (LPCSTR lpSystemName,
563 PLUID lpLuid,
564 LPSTR lpName,
565 LPDWORD cbName)
566 {
567 DPRINT1("LookupPrivilegeNameA: stub\n");
568 SetLastError (ERROR_CALL_NOT_IMPLEMENTED);
569 return FALSE;
570 }
571
572
573 /**********************************************************************
574 * LookupPrivilegeNameW EXPORTED
575 *
576 * @unimplemented
577 */
578 BOOL STDCALL
579 LookupPrivilegeNameW (LPCWSTR lpSystemName,
580 PLUID lpLuid,
581 LPWSTR lpName,
582 LPDWORD cbName)
583 {
584 DPRINT1("LookupPrivilegeNameW: stub\n");
585 SetLastError (ERROR_CALL_NOT_IMPLEMENTED);
586 return FALSE;
587 }
588
589
590 /**********************************************************************
591 * GetNamedSecurityInfoW EXPORTED
592 *
593 * @unimplemented
594 */
595 DWORD STDCALL
596 GetNamedSecurityInfoW(LPWSTR pObjectName,
597 SE_OBJECT_TYPE ObjectType,
598 SECURITY_INFORMATION SecurityInfo,
599 PSID *ppsidOwner,
600 PSID *ppsidGroup,
601 PACL *ppDacl,
602 PACL *ppSacl,
603 PSECURITY_DESCRIPTOR *ppSecurityDescriptor)
604 {
605 DPRINT1("GetNamedSecurityInfoW: stub\n");
606 return ERROR_CALL_NOT_IMPLEMENTED;
607 }
608
609
610 /**********************************************************************
611 * GetNamedSecurityInfoA EXPORTED
612 *
613 * @unimplemented
614 */
615 DWORD STDCALL
616 GetNamedSecurityInfoA(LPSTR pObjectName,
617 SE_OBJECT_TYPE ObjectType,
618 SECURITY_INFORMATION SecurityInfo,
619 PSID *ppsidOwner,
620 PSID *ppsidGroup,
621 PACL *ppDacl,
622 PACL *ppSacl,
623 PSECURITY_DESCRIPTOR *ppSecurityDescriptor)
624 {
625 DPRINT1("GetNamedSecurityInfoA: stub\n");
626 return ERROR_CALL_NOT_IMPLEMENTED;
627 }
628
629
630 /**********************************************************************
631 * SetNamedSecurityInfoW EXPORTED
632 *
633 * @unimplemented
634 */
635 DWORD STDCALL
636 SetNamedSecurityInfoW(LPWSTR pObjectName,
637 SE_OBJECT_TYPE ObjectType,
638 SECURITY_INFORMATION SecurityInfo,
639 PSID psidOwner,
640 PSID psidGroup,
641 PACL pDacl,
642 PACL pSacl)
643 {
644 DPRINT1("SetNamedSecurityInfoW: stub\n");
645 return ERROR_CALL_NOT_IMPLEMENTED;
646 }
647
648
649 /**********************************************************************
650 * SetNamedSecurityInfoA EXPORTED
651 *
652 * @unimplemented
653 */
654 DWORD STDCALL
655 SetNamedSecurityInfoA(LPSTR pObjectName,
656 SE_OBJECT_TYPE ObjectType,
657 SECURITY_INFORMATION SecurityInfo,
658 PSID psidOwner,
659 PSID psidGroup,
660 PACL pDacl,
661 PACL pSacl)
662 {
663 DPRINT1("SetNamedSecurityInfoA: stub\n");
664 return ERROR_CALL_NOT_IMPLEMENTED;
665 }
666
667 /* EOF */