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 "chrome/browser/policy/registry_dict_win.h" 6 7 #include "base/json/json_reader.h" 8 #include "base/stl_util.h" 9 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_util.h" 11 #include "base/strings/utf_string_conversions.h" 12 #include "base/sys_byteorder.h" 13 #include "base/values.h" 14 #include "base/win/registry.h" 15 #include "chrome/common/json_schema/json_schema_constants.h" 16 17 namespace schema = json_schema_constants; 18 19 using base::win::RegistryKeyIterator; 20 using base::win::RegistryValueIterator; 21 22 namespace policy { 23 24 namespace { 25 26 // Returns the entry with key |name| in |dictionary| (can be NULL), or NULL. 27 const base::DictionaryValue* GetEntry(const base::DictionaryValue* dictionary, 28 const std::string& name) { 29 if (!dictionary) 30 return NULL; 31 const base::DictionaryValue* entry = NULL; 32 dictionary->GetDictionaryWithoutPathExpansion(name, &entry); 33 return entry; 34 } 35 36 // Returns the Value type described in |schema|, or |default_type| if not found. 37 base::Value::Type GetValueTypeForSchema(const base::DictionaryValue* schema, 38 base::Value::Type default_type) { 39 // JSON-schema types to base::Value::Type mapping. 40 static const struct { 41 // JSON schema type. 42 const char* schema_type; 43 // Correspondent value type. 44 base::Value::Type value_type; 45 } kSchemaToValueTypeMap[] = { 46 { schema::kArray, base::Value::TYPE_LIST }, 47 { schema::kBoolean, base::Value::TYPE_BOOLEAN }, 48 { schema::kInteger, base::Value::TYPE_INTEGER }, 49 { schema::kNull, base::Value::TYPE_NULL }, 50 { schema::kNumber, base::Value::TYPE_DOUBLE }, 51 { schema::kObject, base::Value::TYPE_DICTIONARY }, 52 { schema::kString, base::Value::TYPE_STRING }, 53 }; 54 55 if (!schema) 56 return default_type; 57 std::string type; 58 if (!schema->GetStringWithoutPathExpansion(schema::kType, &type)) 59 return default_type; 60 for (size_t i = 0; i < arraysize(kSchemaToValueTypeMap); ++i) { 61 if (type == kSchemaToValueTypeMap[i].schema_type) 62 return kSchemaToValueTypeMap[i].value_type; 63 } 64 return default_type; 65 } 66 67 // Returns the schema for property |name| given the |schema| of an object. 68 // Returns the "additionalProperties" schema if no specific schema for 69 // |name| is present. Returns NULL if no schema is found. 70 const base::DictionaryValue* GetSchemaFor(const base::DictionaryValue* schema, 71 const std::string& name) { 72 const base::DictionaryValue* properties = 73 GetEntry(schema, schema::kProperties); 74 const base::DictionaryValue* sub_schema = GetEntry(properties, name); 75 if (sub_schema) 76 return sub_schema; 77 // "additionalProperties" can be a boolean, but that case is ignored. 78 return GetEntry(schema, schema::kAdditionalProperties); 79 } 80 81 // Converts a value (as read from the registry) to meet |schema|, converting 82 // types as necessary. Unconvertible types will show up as NULL values in the 83 // result. 84 scoped_ptr<base::Value> ConvertValue(const base::Value& value, 85 const base::DictionaryValue* schema) { 86 // Figure out the type to convert to from the schema. 87 const base::Value::Type result_type( 88 GetValueTypeForSchema(schema, value.GetType())); 89 90 // If the type is good already, go with it. 91 if (value.IsType(result_type)) { 92 // Recurse for complex types if there is a schema. 93 if (schema) { 94 const base::DictionaryValue* dict = NULL; 95 const base::ListValue* list = NULL; 96 if (value.GetAsDictionary(&dict)) { 97 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue()); 98 for (base::DictionaryValue::Iterator entry(*dict); !entry.IsAtEnd(); 99 entry.Advance()) { 100 scoped_ptr<base::Value> converted_value( 101 ConvertValue(entry.value(), GetSchemaFor(schema, entry.key()))); 102 result->SetWithoutPathExpansion(entry.key(), 103 converted_value.release()); 104 } 105 return result.Pass(); 106 } else if (value.GetAsList(&list)) { 107 scoped_ptr<base::ListValue> result(new base::ListValue()); 108 const base::DictionaryValue* item_schema = 109 GetEntry(schema, schema::kItems); 110 for (base::ListValue::const_iterator entry(list->begin()); 111 entry != list->end(); ++entry) { 112 result->Append(ConvertValue(**entry, item_schema).release()); 113 } 114 return result.Pass(); 115 } 116 } 117 return make_scoped_ptr(value.DeepCopy()); 118 } 119 120 // Else, do some conversions to map windows registry data types to JSON types. 121 std::string string_value; 122 int int_value = 0; 123 switch (result_type) { 124 case base::Value::TYPE_NULL: { 125 return make_scoped_ptr(base::Value::CreateNullValue()); 126 } 127 case base::Value::TYPE_BOOLEAN: { 128 // Accept booleans encoded as either string or integer. 129 if (value.GetAsInteger(&int_value) || 130 (value.GetAsString(&string_value) && 131 base::StringToInt(string_value, &int_value))) { 132 return make_scoped_ptr(Value::CreateBooleanValue(int_value != 0)); 133 } 134 break; 135 } 136 case base::Value::TYPE_INTEGER: { 137 // Integers may be string-encoded. 138 if (value.GetAsString(&string_value) && 139 base::StringToInt(string_value, &int_value)) { 140 return make_scoped_ptr(base::Value::CreateIntegerValue(int_value)); 141 } 142 break; 143 } 144 case base::Value::TYPE_DOUBLE: { 145 // Doubles may be string-encoded or integer-encoded. 146 double double_value = 0; 147 if (value.GetAsInteger(&int_value)) { 148 return make_scoped_ptr(base::Value::CreateDoubleValue(int_value)); 149 } else if (value.GetAsString(&string_value) && 150 base::StringToDouble(string_value, &double_value)) { 151 return make_scoped_ptr(base::Value::CreateDoubleValue(double_value)); 152 } 153 break; 154 } 155 case base::Value::TYPE_LIST: { 156 // Lists are encoded as subkeys with numbered value in the registry. 157 const base::DictionaryValue* dict = NULL; 158 if (value.GetAsDictionary(&dict)) { 159 scoped_ptr<base::ListValue> result(new base::ListValue()); 160 const base::DictionaryValue* item_schema = 161 GetEntry(schema, schema::kItems); 162 for (int i = 1; ; ++i) { 163 const base::Value* entry = NULL; 164 if (!dict->Get(base::IntToString(i), &entry)) 165 break; 166 result->Append(ConvertValue(*entry, item_schema).release()); 167 } 168 return result.Pass(); 169 } 170 // Fall through in order to accept lists encoded as JSON strings. 171 } 172 case base::Value::TYPE_DICTIONARY: { 173 // Dictionaries may be encoded as JSON strings. 174 if (value.GetAsString(&string_value)) { 175 scoped_ptr<base::Value> result(base::JSONReader::Read(string_value)); 176 if (result && result->IsType(result_type)) 177 return result.Pass(); 178 } 179 break; 180 } 181 case base::Value::TYPE_STRING: 182 case base::Value::TYPE_BINARY: 183 // No conversion possible. 184 break; 185 } 186 187 LOG(WARNING) << "Failed to convert " << value.GetType() 188 << " to " << result_type; 189 return make_scoped_ptr(base::Value::CreateNullValue()); 190 } 191 192 } // namespace 193 194 bool CaseInsensitiveStringCompare::operator()(const std::string& a, 195 const std::string& b) const { 196 return base::strcasecmp(a.c_str(), b.c_str()) < 0; 197 } 198 199 RegistryDict::RegistryDict() {} 200 201 RegistryDict::~RegistryDict() { 202 ClearKeys(); 203 ClearValues(); 204 } 205 206 RegistryDict* RegistryDict::GetKey(const std::string& name) { 207 KeyMap::iterator entry = keys_.find(name); 208 return entry != keys_.end() ? entry->second : NULL; 209 } 210 211 const RegistryDict* RegistryDict::GetKey(const std::string& name) const { 212 KeyMap::const_iterator entry = keys_.find(name); 213 return entry != keys_.end() ? entry->second : NULL; 214 } 215 216 void RegistryDict::SetKey(const std::string& name, 217 scoped_ptr<RegistryDict> dict) { 218 if (!dict) { 219 RemoveKey(name); 220 return; 221 } 222 223 RegistryDict*& entry = keys_[name]; 224 delete entry; 225 entry = dict.release(); 226 } 227 228 scoped_ptr<RegistryDict> RegistryDict::RemoveKey(const std::string& name) { 229 scoped_ptr<RegistryDict> result; 230 KeyMap::iterator entry = keys_.find(name); 231 if (entry != keys_.end()) { 232 result.reset(entry->second); 233 keys_.erase(entry); 234 } 235 return result.Pass(); 236 } 237 238 void RegistryDict::ClearKeys() { 239 STLDeleteValues(&keys_); 240 } 241 242 base::Value* RegistryDict::GetValue(const std::string& name) { 243 ValueMap::iterator entry = values_.find(name); 244 return entry != values_.end() ? entry->second : NULL; 245 } 246 247 const base::Value* RegistryDict::GetValue(const std::string& name) const { 248 ValueMap::const_iterator entry = values_.find(name); 249 return entry != values_.end() ? entry->second : NULL; 250 } 251 252 void RegistryDict::SetValue(const std::string& name, 253 scoped_ptr<base::Value> dict) { 254 if (!dict) { 255 RemoveValue(name); 256 return; 257 } 258 259 Value*& entry = values_[name]; 260 delete entry; 261 entry = dict.release(); 262 } 263 264 scoped_ptr<base::Value> RegistryDict::RemoveValue(const std::string& name) { 265 scoped_ptr<base::Value> result; 266 ValueMap::iterator entry = values_.find(name); 267 if (entry != values_.end()) { 268 result.reset(entry->second); 269 values_.erase(entry); 270 } 271 return result.Pass(); 272 } 273 274 void RegistryDict::ClearValues() { 275 STLDeleteValues(&values_); 276 } 277 278 void RegistryDict::Merge(const RegistryDict& other) { 279 for (KeyMap::const_iterator entry(other.keys_.begin()); 280 entry != other.keys_.end(); ++entry) { 281 RegistryDict*& subdict = keys_[entry->first]; 282 if (!subdict) 283 subdict = new RegistryDict(); 284 subdict->Merge(*entry->second); 285 } 286 287 for (ValueMap::const_iterator entry(other.values_.begin()); 288 entry != other.values_.end(); ++entry) { 289 SetValue(entry->first, make_scoped_ptr(entry->second->DeepCopy())); 290 } 291 } 292 293 void RegistryDict::Swap(RegistryDict* other) { 294 keys_.swap(other->keys_); 295 values_.swap(other->values_); 296 } 297 298 void RegistryDict::ReadRegistry(HKEY hive, const string16& root) { 299 ClearKeys(); 300 ClearValues(); 301 302 // First, read all the values of the key. 303 for (RegistryValueIterator it(hive, root.c_str()); it.Valid(); ++it) { 304 const std::string name = UTF16ToUTF8(it.Name()); 305 switch (it.Type()) { 306 case REG_SZ: 307 case REG_EXPAND_SZ: 308 SetValue( 309 name, 310 make_scoped_ptr(new base::StringValue(UTF16ToUTF8(it.Value())))); 311 continue; 312 case REG_DWORD_LITTLE_ENDIAN: 313 case REG_DWORD_BIG_ENDIAN: 314 if (it.ValueSize() == sizeof(DWORD)) { 315 DWORD dword_value = *(reinterpret_cast<const DWORD*>(it.Value())); 316 if (it.Type() == REG_DWORD_BIG_ENDIAN) 317 dword_value = base::NetToHost32(dword_value); 318 else 319 dword_value = base::ByteSwapToLE32(dword_value); 320 SetValue( 321 name, 322 make_scoped_ptr(base::Value::CreateIntegerValue(dword_value))); 323 continue; 324 } 325 case REG_NONE: 326 case REG_LINK: 327 case REG_MULTI_SZ: 328 case REG_RESOURCE_LIST: 329 case REG_FULL_RESOURCE_DESCRIPTOR: 330 case REG_RESOURCE_REQUIREMENTS_LIST: 331 case REG_QWORD_LITTLE_ENDIAN: 332 // Unsupported type, message gets logged below. 333 break; 334 } 335 336 LOG(WARNING) << "Failed to read hive " << hive << " at " 337 << root << "\\" << name 338 << " type " << it.Type(); 339 } 340 341 // Recurse for all subkeys. 342 for (RegistryKeyIterator it(hive, root.c_str()); it.Valid(); ++it) { 343 std::string name(UTF16ToUTF8(it.Name())); 344 scoped_ptr<RegistryDict> subdict(new RegistryDict()); 345 subdict->ReadRegistry(hive, root + L"\\" + it.Name()); 346 SetKey(name, subdict.Pass()); 347 } 348 } 349 350 scoped_ptr<base::Value> RegistryDict::ConvertToJSON( 351 const base::DictionaryValue* schema) const { 352 base::Value::Type type = 353 GetValueTypeForSchema(schema, base::Value::TYPE_DICTIONARY); 354 switch (type) { 355 case base::Value::TYPE_DICTIONARY: { 356 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue()); 357 for (RegistryDict::ValueMap::const_iterator entry(values_.begin()); 358 entry != values_.end(); ++entry) { 359 result->SetWithoutPathExpansion( 360 entry->first, 361 ConvertValue(*entry->second, 362 GetSchemaFor(schema, entry->first)).release()); 363 } 364 for (RegistryDict::KeyMap::const_iterator entry(keys_.begin()); 365 entry != keys_.end(); ++entry) { 366 result->SetWithoutPathExpansion( 367 entry->first, 368 entry->second->ConvertToJSON( 369 GetSchemaFor(schema, entry->first)).release()); 370 } 371 return result.Pass(); 372 } 373 case base::Value::TYPE_LIST: { 374 scoped_ptr<base::ListValue> result(new base::ListValue()); 375 const base::DictionaryValue* item_schema = 376 GetEntry(schema, schema::kItems); 377 for (int i = 1; ; ++i) { 378 const std::string name(base::IntToString(i)); 379 const RegistryDict* key = GetKey(name); 380 if (key) { 381 result->Append(key->ConvertToJSON(item_schema).release()); 382 continue; 383 } 384 const base::Value* value = GetValue(name); 385 if (value) { 386 result->Append(ConvertValue(*value, item_schema).release()); 387 continue; 388 } 389 break; 390 } 391 return result.Pass(); 392 } 393 default: 394 LOG(WARNING) << "Can't convert registry key to schema type " << type; 395 } 396 397 return make_scoped_ptr(base::Value::CreateNullValue()); 398 } 399 400 } // namespace policy 401