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 namespace installer { |
| 8 |
| 9 ScopedTokenPrivilege::ScopedTokenPrivilege(const wchar_t* privilege_name) |
| 10 : is_enabled_(false) { |
| 11 HANDLE temp_handle; |
| 12 if (!::OpenProcessToken(::GetCurrentProcess(), |
| 13 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, |
| 14 &temp_handle)) { |
| 15 return; |
| 16 } |
| 17 token_.Set(temp_handle); |
| 18 |
| 19 LUID privilege_luid; |
| 20 if (!::LookupPrivilegeValue(NULL, privilege_name, &privilege_luid)) { |
| 21 token_.Close(); |
| 22 return; |
| 23 } |
| 24 |
| 25 // Adjust the token's privileges to enable |privilege_name|. If this privilege |
| 26 // was already enabled, |previous_privileges_|.PrivilegeCount will be set to 0 |
| 27 // and we then know not to disable this privilege upon destruction. |
| 28 TOKEN_PRIVILEGES tp; |
| 29 tp.PrivilegeCount = 1; |
| 30 tp.Privileges[0].Luid = privilege_luid; |
| 31 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 32 DWORD return_length; |
| 33 if (!::AdjustTokenPrivileges(token_.Get(), FALSE, &tp, |
| 34 sizeof(TOKEN_PRIVILEGES), &previous_privileges_, |
| 35 &return_length)) { |
| 36 token_.Close(); |
| 37 return; |
| 38 } |
| 39 |
| 40 is_enabled_ = true; |
| 41 } |
| 42 |
| 43 ScopedTokenPrivilege::~ScopedTokenPrivilege() { |
| 44 if (is_enabled_ && previous_privileges_.PrivilegeCount != 0) { |
| 45 ::AdjustTokenPrivileges(token_.Get(), FALSE, &previous_privileges_, |
| 46 sizeof(TOKEN_PRIVILEGES), NULL, NULL); |
| 47 } |
| 48 } |
| 49 |
| 50 } // namespace installer |
OLD | NEW |