ljusten | 1fa2988 | 2016-11-18 02:54:58 +0900 | [diff] [blame] | 1 | // Copyright 2013 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 "components/policy/core/common/registry_dict.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | |
| 9 | #include "base/json/json_reader.h" |
| 10 | #include "base/memory/ptr_util.h" |
| 11 | #include "base/strings/string_number_conversions.h" |
| 12 | #include "base/strings/string_util.h" |
| 13 | #include "base/strings/utf_string_conversions.h" |
| 14 | #include "base/sys_byteorder.h" |
| 15 | #include "base/values.h" |
| 16 | |
| 17 | #if defined(OS_WIN) |
| 18 | #include "base/win/registry.h" |
| 19 | #include "components/policy/core/common/schema.h" |
| 20 | |
| 21 | using base::win::RegistryKeyIterator; |
| 22 | using base::win::RegistryValueIterator; |
| 23 | #endif // #if defined(OS_WIN) |
| 24 | |
| 25 | namespace policy { |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | #if defined(OS_WIN) |
| 30 | // Validates that a key is numerical. Used for lists below. |
| 31 | bool IsKeyNumerical(const std::string& key) { |
| 32 | int temp = 0; |
| 33 | return base::StringToInt(key, &temp); |
| 34 | } |
| 35 | |
| 36 | // Converts a value (as read from the registry) to meet |schema|, converting |
| 37 | // types as necessary. Unconvertible types will show up as null values in the |
| 38 | // result. |
| 39 | std::unique_ptr<base::Value> ConvertValue(const base::Value& value, |
| 40 | const Schema& schema) { |
| 41 | if (!schema.valid()) |
| 42 | return value.CreateDeepCopy(); |
| 43 | |
| 44 | // If the type is good already, go with it. |
| 45 | if (value.IsType(schema.type())) { |
| 46 | // Recurse for complex types. |
| 47 | const base::DictionaryValue* dict = nullptr; |
| 48 | const base::ListValue* list = nullptr; |
| 49 | if (value.GetAsDictionary(&dict)) { |
| 50 | std::unique_ptr<base::DictionaryValue> result( |
| 51 | new base::DictionaryValue()); |
| 52 | for (base::DictionaryValue::Iterator entry(*dict); !entry.IsAtEnd(); |
| 53 | entry.Advance()) { |
| 54 | std::unique_ptr<base::Value> converted = |
| 55 | ConvertValue(entry.value(), schema.GetProperty(entry.key())); |
| 56 | if (converted) |
| 57 | result->SetWithoutPathExpansion(entry.key(), converted.release()); |
| 58 | } |
| 59 | return std::move(result); |
| 60 | } else if (value.GetAsList(&list)) { |
| 61 | std::unique_ptr<base::ListValue> result(new base::ListValue()); |
| 62 | for (base::ListValue::const_iterator entry(list->begin()); |
| 63 | entry != list->end(); ++entry) { |
| 64 | std::unique_ptr<base::Value> converted = |
jdoerrie | 4518400 | 2017-04-12 03:09:14 +0900 | [diff] [blame^] | 65 | ConvertValue(*entry, schema.GetItems()); |
ljusten | 1fa2988 | 2016-11-18 02:54:58 +0900 | [diff] [blame] | 66 | if (converted) |
| 67 | result->Append(std::move(converted)); |
| 68 | } |
| 69 | return std::move(result); |
| 70 | } |
| 71 | return value.CreateDeepCopy(); |
| 72 | } |
| 73 | |
| 74 | // Else, do some conversions to map windows registry data types to JSON types. |
| 75 | std::string string_value; |
| 76 | int int_value = 0; |
| 77 | switch (schema.type()) { |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 78 | case base::Value::Type::NONE: { |
jdoerrie | fcdbae7 | 2017-04-07 15:39:00 +0900 | [diff] [blame] | 79 | return base::MakeUnique<base::Value>(); |
ljusten | 1fa2988 | 2016-11-18 02:54:58 +0900 | [diff] [blame] | 80 | } |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 81 | case base::Value::Type::BOOLEAN: { |
ljusten | 1fa2988 | 2016-11-18 02:54:58 +0900 | [diff] [blame] | 82 | // Accept booleans encoded as either string or integer. |
| 83 | if (value.GetAsInteger(&int_value) || |
| 84 | (value.GetAsString(&string_value) && |
| 85 | base::StringToInt(string_value, &int_value))) { |
jdoerrie | bfe825e | 2017-03-02 21:09:19 +0900 | [diff] [blame] | 86 | return std::unique_ptr<base::Value>(new base::Value(int_value != 0)); |
ljusten | 1fa2988 | 2016-11-18 02:54:58 +0900 | [diff] [blame] | 87 | } |
| 88 | break; |
| 89 | } |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 90 | case base::Value::Type::INTEGER: { |
ljusten | 1fa2988 | 2016-11-18 02:54:58 +0900 | [diff] [blame] | 91 | // Integers may be string-encoded. |
| 92 | if (value.GetAsString(&string_value) && |
| 93 | base::StringToInt(string_value, &int_value)) { |
jdoerrie | bfe825e | 2017-03-02 21:09:19 +0900 | [diff] [blame] | 94 | return std::unique_ptr<base::Value>(new base::Value(int_value)); |
ljusten | 1fa2988 | 2016-11-18 02:54:58 +0900 | [diff] [blame] | 95 | } |
| 96 | break; |
| 97 | } |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 98 | case base::Value::Type::DOUBLE: { |
ljusten | 1fa2988 | 2016-11-18 02:54:58 +0900 | [diff] [blame] | 99 | // Doubles may be string-encoded or integer-encoded. |
| 100 | double double_value = 0; |
| 101 | if (value.GetAsDouble(&double_value) || |
| 102 | (value.GetAsString(&string_value) && |
| 103 | base::StringToDouble(string_value, &double_value))) { |
jdoerrie | bfe825e | 2017-03-02 21:09:19 +0900 | [diff] [blame] | 104 | return std::unique_ptr<base::Value>(new base::Value(double_value)); |
ljusten | 1fa2988 | 2016-11-18 02:54:58 +0900 | [diff] [blame] | 105 | } |
| 106 | break; |
| 107 | } |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 108 | case base::Value::Type::LIST: { |
ljusten | 1fa2988 | 2016-11-18 02:54:58 +0900 | [diff] [blame] | 109 | // Lists are encoded as subkeys with numbered value in the registry |
| 110 | // (non-numerical keys are ignored). |
| 111 | const base::DictionaryValue* dict = nullptr; |
| 112 | if (value.GetAsDictionary(&dict)) { |
| 113 | std::unique_ptr<base::ListValue> result(new base::ListValue()); |
| 114 | for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); |
| 115 | it.Advance()) { |
| 116 | if (!IsKeyNumerical(it.key())) |
| 117 | continue; |
| 118 | std::unique_ptr<base::Value> converted = |
| 119 | ConvertValue(it.value(), schema.GetItems()); |
| 120 | if (converted) |
| 121 | result->Append(std::move(converted)); |
| 122 | } |
| 123 | return std::move(result); |
| 124 | } |
| 125 | // Fall through in order to accept lists encoded as JSON strings. |
| 126 | } |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 127 | case base::Value::Type::DICTIONARY: { |
ljusten | 1fa2988 | 2016-11-18 02:54:58 +0900 | [diff] [blame] | 128 | // Dictionaries may be encoded as JSON strings. |
| 129 | if (value.GetAsString(&string_value)) { |
| 130 | std::unique_ptr<base::Value> result = |
| 131 | base::JSONReader::Read(string_value); |
| 132 | if (result && result->IsType(schema.type())) |
| 133 | return result; |
| 134 | } |
| 135 | break; |
| 136 | } |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 137 | case base::Value::Type::STRING: |
| 138 | case base::Value::Type::BINARY: |
ljusten | 1fa2988 | 2016-11-18 02:54:58 +0900 | [diff] [blame] | 139 | // No conversion possible. |
| 140 | break; |
| 141 | } |
| 142 | |
| 143 | LOG(WARNING) << "Failed to convert " << value.GetType() |
| 144 | << " to " << schema.type(); |
| 145 | return nullptr; |
| 146 | } |
| 147 | #endif // #if defined(OS_WIN) |
| 148 | |
| 149 | } // namespace |
| 150 | |
| 151 | bool CaseInsensitiveStringCompare::operator()(const std::string& a, |
| 152 | const std::string& b) const { |
| 153 | return base::CompareCaseInsensitiveASCII(a, b) < 0; |
| 154 | } |
| 155 | |
| 156 | RegistryDict::RegistryDict() {} |
| 157 | |
| 158 | RegistryDict::~RegistryDict() { |
| 159 | ClearKeys(); |
| 160 | ClearValues(); |
| 161 | } |
| 162 | |
| 163 | RegistryDict* RegistryDict::GetKey(const std::string& name) { |
| 164 | KeyMap::iterator entry = keys_.find(name); |
| 165 | return entry != keys_.end() ? entry->second.get() : nullptr; |
| 166 | } |
| 167 | |
| 168 | const RegistryDict* RegistryDict::GetKey(const std::string& name) const { |
| 169 | KeyMap::const_iterator entry = keys_.find(name); |
| 170 | return entry != keys_.end() ? entry->second.get() : nullptr; |
| 171 | } |
| 172 | |
| 173 | void RegistryDict::SetKey(const std::string& name, |
| 174 | std::unique_ptr<RegistryDict> dict) { |
| 175 | if (!dict) { |
| 176 | RemoveKey(name); |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | keys_[name] = std::move(dict); |
| 181 | } |
| 182 | |
| 183 | std::unique_ptr<RegistryDict> RegistryDict::RemoveKey(const std::string& name) { |
| 184 | std::unique_ptr<RegistryDict> result; |
| 185 | KeyMap::iterator entry = keys_.find(name); |
| 186 | if (entry != keys_.end()) { |
| 187 | result = std::move(entry->second); |
| 188 | keys_.erase(entry); |
| 189 | } |
| 190 | return result; |
| 191 | } |
| 192 | |
| 193 | void RegistryDict::ClearKeys() { |
| 194 | keys_.clear(); |
| 195 | } |
| 196 | |
| 197 | base::Value* RegistryDict::GetValue(const std::string& name) { |
| 198 | ValueMap::iterator entry = values_.find(name); |
| 199 | return entry != values_.end() ? entry->second.get() : nullptr; |
| 200 | } |
| 201 | |
| 202 | const base::Value* RegistryDict::GetValue(const std::string& name) const { |
| 203 | ValueMap::const_iterator entry = values_.find(name); |
| 204 | return entry != values_.end() ? entry->second.get() : nullptr; |
| 205 | } |
| 206 | |
| 207 | void RegistryDict::SetValue(const std::string& name, |
| 208 | std::unique_ptr<base::Value> dict) { |
| 209 | if (!dict) { |
| 210 | RemoveValue(name); |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | values_[name] = std::move(dict); |
| 215 | } |
| 216 | |
| 217 | std::unique_ptr<base::Value> RegistryDict::RemoveValue( |
| 218 | const std::string& name) { |
| 219 | std::unique_ptr<base::Value> result; |
| 220 | ValueMap::iterator entry = values_.find(name); |
| 221 | if (entry != values_.end()) { |
| 222 | result = std::move(entry->second); |
| 223 | values_.erase(entry); |
| 224 | } |
| 225 | return result; |
| 226 | } |
| 227 | |
| 228 | void RegistryDict::ClearValues() { |
| 229 | values_.clear(); |
| 230 | } |
| 231 | |
| 232 | void RegistryDict::Merge(const RegistryDict& other) { |
| 233 | for (KeyMap::const_iterator entry(other.keys_.begin()); |
| 234 | entry != other.keys_.end(); ++entry) { |
| 235 | std::unique_ptr<RegistryDict>& subdict = keys_[entry->first]; |
| 236 | if (!subdict) |
| 237 | subdict = base::MakeUnique<RegistryDict>(); |
| 238 | subdict->Merge(*entry->second); |
| 239 | } |
| 240 | |
| 241 | for (ValueMap::const_iterator entry(other.values_.begin()); |
| 242 | entry != other.values_.end(); ++entry) { |
| 243 | SetValue(entry->first, entry->second->CreateDeepCopy()); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void RegistryDict::Swap(RegistryDict* other) { |
| 248 | keys_.swap(other->keys_); |
| 249 | values_.swap(other->values_); |
| 250 | } |
| 251 | |
| 252 | #if defined(OS_WIN) |
| 253 | void RegistryDict::ReadRegistry(HKEY hive, const base::string16& root) { |
| 254 | ClearKeys(); |
| 255 | ClearValues(); |
| 256 | |
| 257 | // First, read all the values of the key. |
| 258 | for (RegistryValueIterator it(hive, root.c_str()); it.Valid(); ++it) { |
| 259 | const std::string name = base::UTF16ToUTF8(it.Name()); |
| 260 | switch (it.Type()) { |
| 261 | case REG_SZ: |
| 262 | case REG_EXPAND_SZ: |
jdoerrie | 0d1295b | 2017-03-06 20:12:04 +0900 | [diff] [blame] | 263 | SetValue(name, std::unique_ptr<base::Value>( |
| 264 | new base::Value(base::UTF16ToUTF8(it.Value())))); |
ljusten | 1fa2988 | 2016-11-18 02:54:58 +0900 | [diff] [blame] | 265 | continue; |
| 266 | case REG_DWORD_LITTLE_ENDIAN: |
| 267 | case REG_DWORD_BIG_ENDIAN: |
| 268 | if (it.ValueSize() == sizeof(DWORD)) { |
| 269 | DWORD dword_value = *(reinterpret_cast<const DWORD*>(it.Value())); |
| 270 | if (it.Type() == REG_DWORD_BIG_ENDIAN) |
| 271 | dword_value = base::NetToHost32(dword_value); |
| 272 | else |
| 273 | dword_value = base::ByteSwapToLE32(dword_value); |
jdoerrie | bfe825e | 2017-03-02 21:09:19 +0900 | [diff] [blame] | 274 | SetValue(name, std::unique_ptr<base::Value>( |
| 275 | new base::Value(static_cast<int>(dword_value)))); |
ljusten | 1fa2988 | 2016-11-18 02:54:58 +0900 | [diff] [blame] | 276 | continue; |
| 277 | } |
| 278 | case REG_NONE: |
| 279 | case REG_LINK: |
| 280 | case REG_MULTI_SZ: |
| 281 | case REG_RESOURCE_LIST: |
| 282 | case REG_FULL_RESOURCE_DESCRIPTOR: |
| 283 | case REG_RESOURCE_REQUIREMENTS_LIST: |
| 284 | case REG_QWORD_LITTLE_ENDIAN: |
| 285 | // Unsupported type, message gets logged below. |
| 286 | break; |
| 287 | } |
| 288 | |
| 289 | LOG(WARNING) << "Failed to read hive " << hive << " at " |
| 290 | << root << "\\" << name |
| 291 | << " type " << it.Type(); |
| 292 | } |
| 293 | |
| 294 | // Recurse for all subkeys. |
| 295 | for (RegistryKeyIterator it(hive, root.c_str()); it.Valid(); ++it) { |
| 296 | std::string name(base::UTF16ToUTF8(it.Name())); |
| 297 | std::unique_ptr<RegistryDict> subdict(new RegistryDict()); |
| 298 | subdict->ReadRegistry(hive, root + L"\\" + it.Name()); |
| 299 | SetKey(name, std::move(subdict)); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | std::unique_ptr<base::Value> RegistryDict::ConvertToJSON( |
| 304 | const Schema& schema) const { |
| 305 | base::Value::Type type = |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 306 | schema.valid() ? schema.type() : base::Value::Type::DICTIONARY; |
ljusten | 1fa2988 | 2016-11-18 02:54:58 +0900 | [diff] [blame] | 307 | switch (type) { |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 308 | case base::Value::Type::DICTIONARY: { |
ljusten | 1fa2988 | 2016-11-18 02:54:58 +0900 | [diff] [blame] | 309 | std::unique_ptr<base::DictionaryValue> result( |
| 310 | new base::DictionaryValue()); |
| 311 | for (RegistryDict::ValueMap::const_iterator entry(values_.begin()); |
| 312 | entry != values_.end(); ++entry) { |
| 313 | Schema subschema = |
| 314 | schema.valid() ? schema.GetProperty(entry->first) : Schema(); |
| 315 | std::unique_ptr<base::Value> converted = |
| 316 | ConvertValue(*entry->second, subschema); |
| 317 | if (converted) |
| 318 | result->SetWithoutPathExpansion(entry->first, converted.release()); |
| 319 | } |
| 320 | for (RegistryDict::KeyMap::const_iterator entry(keys_.begin()); |
| 321 | entry != keys_.end(); ++entry) { |
| 322 | Schema subschema = |
| 323 | schema.valid() ? schema.GetProperty(entry->first) : Schema(); |
| 324 | std::unique_ptr<base::Value> converted = |
| 325 | entry->second->ConvertToJSON(subschema); |
| 326 | if (converted) |
| 327 | result->SetWithoutPathExpansion(entry->first, converted.release()); |
| 328 | } |
| 329 | return std::move(result); |
| 330 | } |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 331 | case base::Value::Type::LIST: { |
ljusten | 1fa2988 | 2016-11-18 02:54:58 +0900 | [diff] [blame] | 332 | std::unique_ptr<base::ListValue> result(new base::ListValue()); |
| 333 | Schema item_schema = schema.valid() ? schema.GetItems() : Schema(); |
| 334 | for (RegistryDict::KeyMap::const_iterator entry(keys_.begin()); |
| 335 | entry != keys_.end(); ++entry) { |
| 336 | if (!IsKeyNumerical(entry->first)) |
| 337 | continue; |
| 338 | std::unique_ptr<base::Value> converted = |
| 339 | entry->second->ConvertToJSON(item_schema); |
| 340 | if (converted) |
| 341 | result->Append(std::move(converted)); |
| 342 | } |
| 343 | for (RegistryDict::ValueMap::const_iterator entry(values_.begin()); |
| 344 | entry != values_.end(); ++entry) { |
| 345 | if (!IsKeyNumerical(entry->first)) |
| 346 | continue; |
| 347 | std::unique_ptr<base::Value> converted = |
| 348 | ConvertValue(*entry->second, item_schema); |
| 349 | if (converted) |
| 350 | result->Append(std::move(converted)); |
| 351 | } |
| 352 | return std::move(result); |
| 353 | } |
| 354 | default: |
| 355 | LOG(WARNING) << "Can't convert registry key to schema type " << type; |
| 356 | } |
| 357 | |
| 358 | return nullptr; |
| 359 | } |
| 360 | #endif // #if defined(OS_WIN) |
| 361 | } // namespace policy |