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.
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.
8 #include "rlz/win/lib/registry_util.h"
10 #include "base/process/process_handle.h"
11 #include "base/strings/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"
19 bool RegKeyReadValue(base::win::RegKey
& key
, const wchar_t* name
,
20 char* value
, size_t* value_size
) {
23 std::wstring value_string
;
24 if (key
.ReadValue(name
, &value_string
) != ERROR_SUCCESS
) {
28 if (value_string
.length() > *value_size
) {
29 *value_size
= value_string
.length();
33 // Note that RLZ string are always ASCII by design.
34 strncpy(value
, base::WideToUTF8(value_string
).c_str(), *value_size
);
35 value
[*value_size
- 1] = 0;
39 bool RegKeyWriteValue(base::win::RegKey
& key
, const wchar_t* name
,
41 std::wstring
value_string(base::ASCIIToWide(value
));
42 return key
.WriteValue(name
, value_string
.c_str()) == ERROR_SUCCESS
;
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");
55 if (ProcessInfo::IsRunningAsSystem()) {
56 ASSERT_STRING("UserKey::HasAccess: No access as SYSTEM without SID set.");
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
;
65 if (!base::GetProcessIntegrityLevel(process_handle
, &level
)) {
66 ASSERT_STRING("UserKey::HasAccess: Cannot determine Integrity Level.");
69 if (level
<= base::LOW_INTEGRITY
) {
70 ASSERT_STRING("UserKey::HasAccess: Cannot write from Low Integrity.");
77 } // namespace rlz_lib