| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 // A helper library to keep track of a user's key by SID. | |
| 6 // Used by RLZ libary. Also to be used by SearchWithGoogle library. | |
| 7 | |
| 8 #include "rlz/win/lib/registry_util.h" | |
| 9 | |
| 10 #include "base/process_util.h" | |
| 11 #include "base/utf_string_conversions.h" | |
| 12 #include "base/win/registry.h" | |
| 13 #include "base/win/windows_version.h" | |
| 14 #include "rlz/lib/assert.h" | |
| 15 #include "rlz/win/lib/process_info.h" | |
| 16 | |
| 17 namespace rlz_lib { | |
| 18 | |
| 19 bool RegKeyReadValue(base::win::RegKey& key, const wchar_t* name, | |
| 20 char* value, size_t* value_size) { | |
| 21 value[0] = 0; | |
| 22 | |
| 23 std::wstring value_string; | |
| 24 if (key.ReadValue(name, &value_string) != ERROR_SUCCESS) { | |
| 25 return false; | |
| 26 } | |
| 27 | |
| 28 if (value_string.length() > *value_size) { | |
| 29 *value_size = value_string.length(); | |
| 30 return false; | |
| 31 } | |
| 32 | |
| 33 // Note that RLZ string are always ASCII by design. | |
| 34 strncpy(value, WideToUTF8(value_string).c_str(), *value_size); | |
| 35 value[*value_size - 1] = 0; | |
| 36 return true; | |
| 37 } | |
| 38 | |
| 39 bool RegKeyWriteValue(base::win::RegKey& key, const wchar_t* name, | |
| 40 const char* value) { | |
| 41 std::wstring value_string(ASCIIToWide(value)); | |
| 42 return key.WriteValue(name, value_string.c_str()) == ERROR_SUCCESS; | |
| 43 } | |
| 44 | |
| 45 bool HasUserKeyAccess(bool write_access) { | |
| 46 // The caller is trying to access HKEY_CURRENT_USER. Test to see if we can | |
| 47 // read from there. Don't try HKEY_CURRENT_USER because this will cause | |
| 48 // problems in the unit tests: if we open HKEY_CURRENT_USER directly here, | |
| 49 // the overriding done for unit tests will no longer work. So we try subkey | |
| 50 // "Software" which is known to always exist. | |
| 51 base::win::RegKey key; | |
| 52 if (key.Open(HKEY_CURRENT_USER, L"Software", KEY_READ) != ERROR_SUCCESS) | |
| 53 ASSERT_STRING("Could not open HKEY_CURRENT_USER"); | |
| 54 | |
| 55 if (ProcessInfo::IsRunningAsSystem()) { | |
| 56 ASSERT_STRING("UserKey::HasAccess: No access as SYSTEM without SID set."); | |
| 57 return false; | |
| 58 } | |
| 59 | |
| 60 if (write_access) { | |
| 61 if (base::win::GetVersion() < base::win::VERSION_VISTA) return true; | |
| 62 base::ProcessHandle process_handle = base::GetCurrentProcessHandle(); | |
| 63 base::IntegrityLevel level = base::INTEGRITY_UNKNOWN; | |
| 64 | |
| 65 if (!base::GetProcessIntegrityLevel(process_handle, &level)) { | |
| 66 ASSERT_STRING("UserKey::HasAccess: Cannot determine Integrity Level."); | |
| 67 return false; | |
| 68 } | |
| 69 if (level <= base::LOW_INTEGRITY) { | |
| 70 ASSERT_STRING("UserKey::HasAccess: Cannot write from Low Integrity."); | |
| 71 return false; | |
| 72 } | |
| 73 } | |
| 74 return true; | |
| 75 } | |
| 76 | |
| 77 } // namespace rlz_lib | |
| OLD | NEW |