a41a793743d6ea712ffdbcec01478d4785e6ed30
[reactos.git] / lib / ntdll / rtl / security.c
1 /* $Id$
2  *
3  * COPYRIGHT:       See COPYING in the top level directory
4  * PROJECT:         ReactOS kernel
5  * FILE:            lib/ntdll/rtl/security.c
6  * PURPOSE:         Miscellaneous securitiy related functions
7  * PROGRAMMER:      Eric Kohl
8  * UPDATE HISTORY:
9  *                  21/11/2001 Created
10  */
11
12 #include <ddk/ntddk.h>
13 #include <ntdll/rtl.h>
14
15
16 NTSTATUS STDCALL
17 RtlImpersonateSelf(IN SECURITY_IMPERSONATION_LEVEL ImpersonationLevel)
18 {
19   OBJECT_ATTRIBUTES ObjectAttributes;
20   SECURITY_QUALITY_OF_SERVICE SecQos;
21   HANDLE ProcessToken;
22   HANDLE ImpersonationToken;
23   NTSTATUS Status;
24
25   Status = NtOpenProcessToken(NtCurrentProcess(),
26                               TOKEN_DUPLICATE,
27                               &ProcessToken);
28   if (!NT_SUCCESS(Status))
29     return(Status);
30
31   SecQos.Length = sizeof(SECURITY_QUALITY_OF_SERVICE);
32   SecQos.ImpersonationLevel = ImpersonationLevel;
33   SecQos.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
34   SecQos.EffectiveOnly = FALSE;
35
36   ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
37   ObjectAttributes.RootDirectory = 0;
38   ObjectAttributes.ObjectName = NULL;
39   ObjectAttributes.Attributes = 0;
40   ObjectAttributes.SecurityDescriptor = NULL;
41   ObjectAttributes.SecurityQualityOfService = &SecQos;
42
43   Status = NtDuplicateToken(ProcessToken,
44                             TOKEN_IMPERSONATE,
45                             &ObjectAttributes,
46                             0,
47                             TokenImpersonation,
48                             &ImpersonationToken);
49   if (!NT_SUCCESS(Status))
50     {
51       NtClose(ProcessToken);
52       return(Status);
53     }
54
55   Status = NtSetInformationThread(NtCurrentThread(),
56                                   ThreadImpersonationToken,
57                                   &ImpersonationToken,
58                                   sizeof(HANDLE));
59   NtClose(ImpersonationToken);
60   NtClose(ProcessToken);
61
62   return(Status);
63 }
64
65 /* EOF */