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