-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenutil.cpp
123 lines (107 loc) · 3.82 KB
/
tokenutil.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// --------------------------------------------------------------------------
// Module Name: TokenUtil.cpp
//
// Copyright (c) 1999-2000, Microsoft Corporation
//
// Functions that are useful for token manipulation.
//
// History: 1999-08-18 vtan created
// 1999-11-16 vtan separate file
// 2000-02-01 vtan moved from Neptune to Whistler
// 2000-03-31 vtan duplicated from ds to shell
// --------------------------------------------------------------------------
#include <windows.h>
#include "TokenUtil.h"
// --------------------------------------------------------------------------
// ::OpenEffectiveToken
//
// Arguments: dwDesiredAccess = Access to open the handle with.
//
// Returns: BOOL
//
// Purpose: Opens the effective token. If the thread is impersonating then
// this is opened. Otherwise the process token is opened.
//
// History: 2000-03-31 vtan created
// --------------------------------------------------------------------------
STDAPI_(BOOL) OpenEffectiveToken (IN DWORD dwDesiredAccess, OUT HANDLE *phToken)
{
BOOL fResult;
if (IsBadWritePtr(phToken, sizeof(*phToken)))
{
SetLastError(ERROR_INVALID_PARAMETER);
fResult = FALSE;
}
else
{
*phToken = NULL;
fResult = OpenThreadToken(GetCurrentThread(), dwDesiredAccess, FALSE, phToken);
if ((fResult == FALSE) && (GetLastError() == ERROR_NO_TOKEN))
{
fResult = OpenProcessToken(GetCurrentProcess(), dwDesiredAccess, phToken);
}
}
return(fResult);
}
// --------------------------------------------------------------------------
// CPrivilegeEnable::CPrivilegeEnable
//
// Arguments: pszName = Name of the privilege to enable.
//
// Returns: <none>
//
// Purpose: Gets the current state of the privilege and enables it. The
// privilege is specified by name and looked up.
//
// History: 1999-08-23 vtan created
// --------------------------------------------------------------------------
CPrivilegeEnable::CPrivilegeEnable (const TCHAR *pszName) :
_fSet(false),
_hToken(NULL)
{
if (OpenEffectiveToken(TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, &_hToken) != FALSE)
{
TOKEN_PRIVILEGES newPrivilege;
if (LookupPrivilegeValue(NULL, pszName, &newPrivilege.Privileges[0].Luid) != FALSE)
{
DWORD dwReturnTokenPrivilegesSize;
newPrivilege.PrivilegeCount = 1;
newPrivilege.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
_fSet = (AdjustTokenPrivileges(_hToken,
FALSE,
&newPrivilege,
sizeof(newPrivilege),
&_tokenPrivilegePrevious,
&dwReturnTokenPrivilegesSize) != FALSE);
}
}
}
// --------------------------------------------------------------------------
// CPrivilegeEnable::~CPrivilegeEnable
//
// Arguments: <none>
//
// Returns: <none>
//
// Purpose: Restores the previous state of the privilege prior to
// instantiation of the object.
//
// History: 1999-08-23 vtan created
// --------------------------------------------------------------------------
CPrivilegeEnable::~CPrivilegeEnable (void)
{
if (_fSet)
{
(BOOL)AdjustTokenPrivileges(_hToken,
FALSE,
&_tokenPrivilegePrevious,
0,
NULL,
NULL);
}
if (_hToken != NULL)
{
(BOOL)CloseHandle(_hToken);
_hToken = NULL;
}
}