OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/configuration_policy_handler_chromeos.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/json/json_reader.h" |
| 10 #include "base/json/json_writer.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/string_util.h" |
| 13 #include "chrome/browser/chromeos/cros/onc_network_parser.h" |
| 14 #include "chrome/browser/policy/policy_error_map.h" |
| 15 #include "chrome/browser/policy/policy_map.h" |
| 16 #include "grit/generated_resources.h" |
| 17 |
| 18 namespace policy { |
| 19 |
| 20 NetworkConfigurationPolicyHandler::NetworkConfigurationPolicyHandler( |
| 21 ConfigurationPolicyType type) |
| 22 : TypeCheckingPolicyHandler(type, Value::TYPE_STRING) {} |
| 23 |
| 24 NetworkConfigurationPolicyHandler::~NetworkConfigurationPolicyHandler() {} |
| 25 |
| 26 bool NetworkConfigurationPolicyHandler::CheckPolicySettings( |
| 27 const PolicyMap& policies, |
| 28 PolicyErrorMap* errors) { |
| 29 const Value* value; |
| 30 if (!CheckAndGetValue(policies, errors, &value)) |
| 31 return false; |
| 32 |
| 33 if (value) { |
| 34 std::string onc_blob; |
| 35 value->GetAsString(&onc_blob); |
| 36 chromeos::OncNetworkParser parser(onc_blob); |
| 37 if (!parser.parse_error().empty()) { |
| 38 errors->AddError(policy_type(), |
| 39 IDS_POLICY_NETWORK_CONFIG_PARSE_ERROR, |
| 40 parser.parse_error()); |
| 41 return false; |
| 42 } |
| 43 } |
| 44 |
| 45 return true; |
| 46 } |
| 47 |
| 48 void NetworkConfigurationPolicyHandler::ApplyPolicySettings( |
| 49 const PolicyMap& policies, |
| 50 PrefValueMap* prefs) { |
| 51 // Network policy is read directly from the provider and injected into |
| 52 // NetworkLibrary, so no need to convert the policy settings into prefs. |
| 53 } |
| 54 |
| 55 void NetworkConfigurationPolicyHandler::PrepareForDisplaying( |
| 56 PolicyMap* policies) const { |
| 57 const Value* network_config = policies->Get(policy_type()); |
| 58 if (!network_config) |
| 59 return; |
| 60 |
| 61 Value* sanitized_config = SanitizeNetworkConfig(network_config); |
| 62 if (!sanitized_config) |
| 63 sanitized_config = Value::CreateNullValue(); |
| 64 |
| 65 policies->Set(policy_type(), sanitized_config); |
| 66 } |
| 67 |
| 68 // static |
| 69 Value* NetworkConfigurationPolicyHandler::SanitizeNetworkConfig( |
| 70 const Value* config) { |
| 71 std::string json_string; |
| 72 if (!config->GetAsString(&json_string)) |
| 73 return NULL; |
| 74 |
| 75 scoped_ptr<Value> json_value(base::JSONReader::Read(json_string, true)); |
| 76 if (!json_value.get() || !json_value->IsType(base::Value::TYPE_DICTIONARY)) |
| 77 return NULL; |
| 78 |
| 79 DictionaryValue* config_dict = |
| 80 static_cast<DictionaryValue*>(json_value.get()); |
| 81 |
| 82 // Strip any sensitive information from the JSON dictionary. |
| 83 base::ListValue* config_list = NULL; |
| 84 if (config_dict->GetList("NetworkConfigurations", &config_list)) { |
| 85 for (base::ListValue::const_iterator network_entry = config_list->begin(); |
| 86 network_entry != config_list->end(); |
| 87 ++network_entry) { |
| 88 if ((*network_entry) && |
| 89 (*network_entry)->IsType(base::Value::TYPE_DICTIONARY)) { |
| 90 StripSensitiveValues(static_cast<DictionaryValue*>(*network_entry)); |
| 91 } |
| 92 } |
| 93 } |
| 94 |
| 95 // Convert back to a string, pretty printing the contents. |
| 96 base::JSONWriter::WriteWithOptionalEscape(config_dict, true, false, |
| 97 &json_string); |
| 98 return Value::CreateStringValue(json_string); |
| 99 } |
| 100 |
| 101 // static |
| 102 void NetworkConfigurationPolicyHandler::StripSensitiveValues( |
| 103 DictionaryValue* network_dict) { |
| 104 // List of settings we filter from the network dictionary. |
| 105 static const char* kFilteredSettings[] = { |
| 106 "WiFi.Passphrase", |
| 107 "IPsec.EAP.Password", |
| 108 "IPsec.EAP.Password", |
| 109 "IPsec.XAUTH.Password", |
| 110 "L2TP.Password", |
| 111 }; |
| 112 // Placeholder to insert in place of the filtered setting. |
| 113 static const char kPlaceholder[] = "********"; |
| 114 |
| 115 for (size_t i = 0; i < arraysize(kFilteredSettings); ++i) { |
| 116 if (network_dict->Remove(kFilteredSettings[i], NULL)) { |
| 117 network_dict->Set(kFilteredSettings[i], |
| 118 Value::CreateStringValue(kPlaceholder)); |
| 119 } |
| 120 } |
| 121 } |
| 122 |
| 123 } // namespace policy |
OLD | NEW |