OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/installer/util/scoped_token_privilege.h" |
| 6 |
| 7 ScopedTokenPrivilege::ScopedTokenPrivilege(const wchar_t* privilege_name) |
| 8 : is_enabled_(false) { |
| 9 HANDLE temp_handle; |
| 10 if (!::OpenProcessToken(::GetCurrentProcess(), |
| 11 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, |
| 12 &temp_handle)) { |
| 13 return; |
| 14 } |
| 15 token_.Set(temp_handle); |
| 16 |
| 17 LUID privilege_luid; |
| 18 if (!::LookupPrivilegeValue(NULL, privilege_name, &privilege_luid)) { |
| 19 token_.Close(); |
| 20 return; |
| 21 } |
| 22 |
| 23 // Adjust the token's privileges to enable |privilege_name|. If this privilege |
| 24 // was already enabled, |previous_privileges_|.PrivilegeCount will be set to 0 |
| 25 // and we then know not to disable this privilege upon destruction. |
| 26 TOKEN_PRIVILEGES tp; |
| 27 tp.PrivilegeCount = 1; |
| 28 tp.Privileges[0].Luid = privilege_luid; |
| 29 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 30 DWORD return_length; |
| 31 if (!::AdjustTokenPrivileges(token_.Get(), FALSE, &tp, |
| 32 sizeof(TOKEN_PRIVILEGES), &previous_privileges_, |
| 33 &return_length)) { |
| 34 token_.Close(); |
| 35 return; |
| 36 } |
| 37 |
| 38 is_enabled_ = true; |
| 39 } |
| 40 |
| 41 ScopedTokenPrivilege::~ScopedTokenPrivilege() { |
| 42 if (is_enabled_ && previous_privileges_.PrivilegeCount != 0) { |
| 43 ::AdjustTokenPrivileges(token_.Get(), FALSE, &previous_privileges_, |
| 44 sizeof(TOKEN_PRIVILEGES), NULL, NULL); |
| 45 } |
| 46 } |
OLD | NEW |