1 /* $Id: misc.c,v 1.23 2004/09/06 22:12:25 ekohl Exp $
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
20 AreAllAccessesGranted(DWORD GrantedAccess
,
23 return((BOOL
)RtlAreAllAccessesGranted(GrantedAccess
,
32 AreAnyAccessesGranted(DWORD GrantedAccess
,
35 return((BOOL
)RtlAreAnyAccessesGranted(GrantedAccess
,
40 /******************************************************************************
41 * GetFileSecurityA [ADVAPI32.@]
43 * Obtains Specified information about the security of a file or directory.
46 * lpFileName [I] Name of the file to get info for
47 * RequestedInformation [I] SE_ flags from "winnt.h"
48 * pSecurityDescriptor [O] Destination for security information
49 * nLength [I] Length of pSecurityDescriptor
50 * lpnLengthNeeded [O] Destination for length of returned security information
53 * Success: TRUE. pSecurityDescriptor contains the requested information.
54 * Failure: FALSE. lpnLengthNeeded contains the required space to return the info.
57 * The information returned is constrained by the callers access rights and
63 GetFileSecurityA(LPCSTR lpFileName
,
64 SECURITY_INFORMATION RequestedInformation
,
65 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
67 LPDWORD lpnLengthNeeded
)
69 UNICODE_STRING FileName
;
73 Status
= RtlCreateUnicodeStringFromAsciiz(&FileName
,
75 if (!NT_SUCCESS(Status
))
77 SetLastError(RtlNtStatusToDosError(Status
));
81 bResult
= GetFileSecurityW(FileName
.Buffer
,
87 RtlFreeUnicodeString(&FileName
);
97 GetFileSecurityW(LPCWSTR lpFileName
,
98 SECURITY_INFORMATION RequestedInformation
,
99 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
101 LPDWORD lpnLengthNeeded
)
103 OBJECT_ATTRIBUTES ObjectAttributes
;
104 IO_STATUS_BLOCK StatusBlock
;
105 UNICODE_STRING FileName
;
106 ULONG AccessMask
= 0;
110 DPRINT("GetFileSecurityW() called\n");
112 if (RequestedInformation
&
113 (OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
))
115 AccessMask
|= STANDARD_RIGHTS_READ
;
118 if (RequestedInformation
& SACL_SECURITY_INFORMATION
)
120 AccessMask
|= ACCESS_SYSTEM_SECURITY
;
123 if (!RtlDosPathNameToNtPathName_U((LPWSTR
)lpFileName
,
128 DPRINT("Invalid path\n");
129 SetLastError(ERROR_BAD_PATHNAME
);
133 InitializeObjectAttributes(&ObjectAttributes
,
135 OBJ_CASE_INSENSITIVE
,
139 Status
= NtOpenFile(&FileHandle
,
143 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
145 if (!NT_SUCCESS(Status
))
147 DPRINT("NtOpenFile() failed (Status %lx)\n", Status
);
148 SetLastError(RtlNtStatusToDosError(Status
));
152 RtlFreeUnicodeString(&FileName
);
154 Status
= NtQuerySecurityObject(FileHandle
,
155 RequestedInformation
,
161 if (!NT_SUCCESS(Status
))
163 DPRINT("NtQuerySecurityObject() failed (Status %lx)\n", Status
);
164 SetLastError(RtlNtStatusToDosError(Status
));
176 GetKernelObjectSecurity(HANDLE Handle
,
177 SECURITY_INFORMATION RequestedInformation
,
178 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
180 LPDWORD lpnLengthNeeded
)
184 Status
= NtQuerySecurityObject(Handle
,
185 RequestedInformation
,
189 if (!NT_SUCCESS(Status
))
191 SetLastError(RtlNtStatusToDosError(Status
));
198 /******************************************************************************
199 * SetFileSecurityW [ADVAPI32.@]
200 * Sets the security of a file or directory
205 SetFileSecurityW (LPCWSTR lpFileName
,
206 SECURITY_INFORMATION RequestedInformation
,
207 PSECURITY_DESCRIPTOR pSecurityDescriptor
)
209 DPRINT1("SetFileSecurityW : stub\n");
214 /******************************************************************************
215 * SetFileSecurityA [ADVAPI32.@]
216 * Sets the security of a file or directory
221 SetFileSecurityA (LPCSTR lpFileName
,
222 SECURITY_INFORMATION RequestedInformation
,
223 PSECURITY_DESCRIPTOR pSecurityDescriptor
)
225 DPRINT("SetFileSecurityA : stub\n");
234 SetKernelObjectSecurity(HANDLE Handle
,
235 SECURITY_INFORMATION SecurityInformation
,
236 PSECURITY_DESCRIPTOR SecurityDescriptor
)
240 Status
= NtSetSecurityObject(Handle
,
243 if (!NT_SUCCESS(Status
))
245 SetLastError(RtlNtStatusToDosError(Status
));
256 MapGenericMask(PDWORD AccessMask
,
257 PGENERIC_MAPPING GenericMapping
)
259 RtlMapGenericMask(AccessMask
,
268 ImpersonateLoggedOnUser(HANDLE hToken
)
270 SECURITY_QUALITY_OF_SERVICE Qos
;
271 OBJECT_ATTRIBUTES ObjectAttributes
;
278 /* Get the token type */
279 Status
= NtQueryInformationToken (hToken
,
284 if (!NT_SUCCESS(Status
))
286 SetLastError (RtlNtStatusToDosError (Status
));
290 if (Type
== TokenPrimary
)
292 /* Create a duplicate impersonation token */
293 Qos
.Length
= sizeof(SECURITY_QUALITY_OF_SERVICE
);
294 Qos
.ImpersonationLevel
= SecurityImpersonation
;
295 Qos
.ContextTrackingMode
= SECURITY_DYNAMIC_TRACKING
;
296 Qos
.EffectiveOnly
= FALSE
;
298 ObjectAttributes
.Length
= sizeof(OBJECT_ATTRIBUTES
);
299 ObjectAttributes
.RootDirectory
= NULL
;
300 ObjectAttributes
.ObjectName
= NULL
;
301 ObjectAttributes
.Attributes
= 0;
302 ObjectAttributes
.SecurityDescriptor
= NULL
;
303 ObjectAttributes
.SecurityQualityOfService
= &Qos
;
305 Status
= NtDuplicateToken (hToken
,
306 TOKEN_IMPERSONATE
| TOKEN_QUERY
,
311 if (!NT_SUCCESS(Status
))
313 SetLastError (RtlNtStatusToDosError (Status
));
321 /* User the original impersonation token */
326 /* Impersonate the the current thread */
327 Status
= NtSetInformationThread (NtCurrentThread (),
328 ThreadImpersonationToken
,
332 if (Duplicated
== TRUE
)
337 if (!NT_SUCCESS(Status
))
339 SetLastError (RtlNtStatusToDosError (Status
));
351 ImpersonateSelf(SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
)
355 Status
= RtlImpersonateSelf(ImpersonationLevel
);
356 if (!NT_SUCCESS(Status
))
358 SetLastError(RtlNtStatusToDosError(Status
));
374 Status
= NtSetInformationThread(NtCurrentThread(),
375 ThreadImpersonationToken
,
378 if (!NT_SUCCESS(Status
))
380 SetLastError(RtlNtStatusToDosError(Status
));
387 /******************************************************************************
388 * GetUserNameA [ADVAPI32.@]
390 * Get the current user name.
393 * lpszName [O] Destination for the user name.
394 * lpSize [I/O] Size of lpszName.
397 * Success: The length of the user name, including terminating NUL.
398 * Failure: ERROR_MORE_DATA if *lpSize is too small.
403 GetUserNameA( LPSTR lpszName
, LPDWORD lpSize
)
406 // char name[] = { "Administrator" };
408 /* We need to include the null character when determining the size of the buffer. */
409 // len = strlen(name) + 1;
410 // if (len > *lpSize)
412 // SetLastError(ERROR_MORE_DATA);
418 // strcpy(lpszName, name);
419 DPRINT1("GetUserNameA: stub\n");
423 /******************************************************************************
424 * GetUserNameW [ADVAPI32.@]
431 GetUserNameW( LPWSTR lpszName
, LPDWORD lpSize
)
433 // char name[] = { "Administrator" };
435 // DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
437 // if (len > *lpSize)
439 // SetLastError(ERROR_MORE_DATA);
445 // MultiByteToWideChar( CP_ACP, 0, name, -1, lpszName, len );
446 DPRINT1("GetUserNameW: stub\n");
451 /******************************************************************************
452 * LookupAccountSidA [ADVAPI32.@]
457 LookupAccountSidA (LPCSTR lpSystemName
,
461 LPSTR lpReferencedDomainName
,
462 LPDWORD cchReferencedDomainName
,
465 DPRINT1("LookupAccountSidA is unimplemented, but returns success\n");
466 lstrcpynA(lpName
, "Administrator", *cchName
);
467 lstrcpynA(lpReferencedDomainName
, "ReactOS", *cchReferencedDomainName
);
472 /******************************************************************************
473 * LookupAccountSidW [ADVAPI32.@]
478 LookupAccountSidW (LPCWSTR lpSystemName
,
482 LPWSTR lpReferencedDomainName
,
483 LPDWORD cchReferencedDomainName
,
486 DPRINT1("LookupAccountSidW is unimplemented, but returns success\n");
487 lstrcpynW(lpName
, L
"Administrator", *cchName
);
488 lstrcpynW(lpReferencedDomainName
, L
"ReactOS", *cchReferencedDomainName
);
493 /**********************************************************************
494 * LookupPrivilegeValueA EXPORTED
499 LookupPrivilegeValueA (LPCSTR lpSystemName
,
503 UNICODE_STRING SystemName
;
508 if (lpSystemName
!= NULL
)
510 RtlCreateUnicodeStringFromAsciiz (&SystemName
,
511 (LPSTR
)lpSystemName
);
514 /* Check the privilege name is not NULL */
517 SetLastError (ERROR_INVALID_PARAMETER
);
521 RtlCreateUnicodeStringFromAsciiz (&Name
,
524 Result
= LookupPrivilegeValueW ((lpSystemName
!= NULL
) ? SystemName
.Buffer
: NULL
,
528 RtlFreeUnicodeString (&Name
);
531 if (lpSystemName
!= NULL
)
533 RtlFreeUnicodeString (&SystemName
);
540 /**********************************************************************
541 * LookupPrivilegeValueW EXPORTED
546 LookupPrivilegeValueW (LPCWSTR SystemName
,
550 static const WCHAR
* const DefaultPrivNames
[] =
552 L
"SeCreateTokenPrivilege",
553 L
"SeAssignPrimaryTokenPrivilege",
554 L
"SeLockMemoryPrivilege",
555 L
"SeIncreaseQuotaPrivilege",
556 L
"SeUnsolicitedInputPrivilege",
557 L
"SeMachineAccountPrivilege",
559 L
"SeSecurityPrivilege",
560 L
"SeTakeOwnershipPrivilege",
561 L
"SeLoadDriverPrivilege",
562 L
"SeSystemProfilePrivilege",
563 L
"SeSystemtimePrivilege",
564 L
"SeProfileSingleProcessPrivilege",
565 L
"SeIncreaseBasePriorityPrivilege",
566 L
"SeCreatePagefilePrivilege",
567 L
"SeCreatePermanentPrivilege",
568 L
"SeBackupPrivilege",
569 L
"SeRestorePrivilege",
570 L
"SeShutdownPrivilege",
573 L
"SeSystemEnvironmentPrivilege",
574 L
"SeChangeNotifyPrivilege",
575 L
"SeRemoteShutdownPrivilege",
576 L
"SeUndockPrivilege",
577 L
"SeSyncAgentPrivilege",
578 L
"SeEnableDelegationPrivilege",
579 L
"SeManageVolumePrivilege",
580 L
"SeImpersonatePrivilege",
581 L
"SeCreateGlobalPrivilege"
585 if (NULL
!= SystemName
&& L
'\0' != *SystemName
)
587 DPRINT1("LookupPrivilegeValueW: not implemented for remote system\n");
588 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
592 for (Priv
= 0; Priv
< sizeof(DefaultPrivNames
) / sizeof(DefaultPrivNames
[0]); Priv
++)
594 if (0 == wcscmp(PrivName
, DefaultPrivNames
[Priv
]))
596 Luid
->LowPart
= Priv
+ 1;
602 DPRINT1("LookupPrivilegeValueW: no such privilege %S\n", PrivName
);
603 SetLastError(ERROR_NO_SUCH_PRIVILEGE
);
608 /**********************************************************************
609 * LookupPrivilegeDisplayNameA EXPORTED
614 LookupPrivilegeDisplayNameA (LPCSTR lpSystemName
,
617 LPDWORD cbDisplayName
,
618 LPDWORD lpLanguageId
)
620 DPRINT1("LookupPrivilegeDisplayNameA: stub\n");
621 SetLastError (ERROR_CALL_NOT_IMPLEMENTED
);
626 /**********************************************************************
627 * LookupPrivilegeDisplayNameW EXPORTED
632 LookupPrivilegeDisplayNameW (LPCWSTR lpSystemName
,
634 LPWSTR lpDisplayName
,
635 LPDWORD cbDisplayName
,
636 LPDWORD lpLanguageId
)
638 DPRINT1("LookupPrivilegeDisplayNameW: stub\n");
639 SetLastError (ERROR_CALL_NOT_IMPLEMENTED
);
644 /**********************************************************************
645 * LookupPrivilegeNameA EXPORTED
650 LookupPrivilegeNameA (LPCSTR lpSystemName
,
655 DPRINT1("LookupPrivilegeNameA: stub\n");
656 SetLastError (ERROR_CALL_NOT_IMPLEMENTED
);
661 /**********************************************************************
662 * LookupPrivilegeNameW EXPORTED
667 LookupPrivilegeNameW (LPCWSTR lpSystemName
,
672 DPRINT1("LookupPrivilegeNameW: stub\n");
673 SetLastError (ERROR_CALL_NOT_IMPLEMENTED
);
678 /**********************************************************************
679 * GetNamedSecurityInfoW EXPORTED
684 GetNamedSecurityInfoW(LPWSTR pObjectName
,
685 SE_OBJECT_TYPE ObjectType
,
686 SECURITY_INFORMATION SecurityInfo
,
691 PSECURITY_DESCRIPTOR
*ppSecurityDescriptor
)
693 DPRINT1("GetNamedSecurityInfoW: stub\n");
694 return ERROR_CALL_NOT_IMPLEMENTED
;
698 /**********************************************************************
699 * GetNamedSecurityInfoA EXPORTED
704 GetNamedSecurityInfoA(LPSTR pObjectName
,
705 SE_OBJECT_TYPE ObjectType
,
706 SECURITY_INFORMATION SecurityInfo
,
711 PSECURITY_DESCRIPTOR
*ppSecurityDescriptor
)
713 DPRINT1("GetNamedSecurityInfoA: stub\n");
714 return ERROR_CALL_NOT_IMPLEMENTED
;
718 /**********************************************************************
719 * SetNamedSecurityInfoW EXPORTED
724 SetNamedSecurityInfoW(LPWSTR pObjectName
,
725 SE_OBJECT_TYPE ObjectType
,
726 SECURITY_INFORMATION SecurityInfo
,
732 DPRINT1("SetNamedSecurityInfoW: stub\n");
733 return ERROR_CALL_NOT_IMPLEMENTED
;
737 /**********************************************************************
738 * SetNamedSecurityInfoA EXPORTED
743 SetNamedSecurityInfoA(LPSTR pObjectName
,
744 SE_OBJECT_TYPE ObjectType
,
745 SECURITY_INFORMATION SecurityInfo
,
751 DPRINT1("SetNamedSecurityInfoA: stub\n");
752 return ERROR_CALL_NOT_IMPLEMENTED
;
756 /**********************************************************************
757 * GetSecurityInfo EXPORTED
762 GetSecurityInfo(HANDLE handle
,
763 SE_OBJECT_TYPE ObjectType
,
764 SECURITY_INFORMATION SecurityInfo
,
769 PSECURITY_DESCRIPTOR
* ppSecurityDescriptor
)
771 DPRINT1("GetSecurityInfo: stub\n");
772 return ERROR_CALL_NOT_IMPLEMENTED
;
776 /**********************************************************************
777 * ImpersonateNamedPipeClient EXPORTED
782 ImpersonateNamedPipeClient(HANDLE hNamedPipe
)
784 IO_STATUS_BLOCK StatusBlock
;
787 DPRINT("ImpersonateNamedPipeClient() called\n");
789 Status
= NtFsControlFile(hNamedPipe
,
794 FSCTL_PIPE_IMPERSONATE
,
799 if (!NT_SUCCESS(Status
))
801 SetLastError(RtlNtStatusToDosError(Status
));