Roll src/third_party/WebKit 117ac0c:c0b53c2 (svn 202111:202114)
[chromium-blink-merge.git] / chromeos / network / policy_util.cc
blob7c2d0deacf9816c8f5d8257c72bb023948ed834f
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.
5 #include "chromeos/network/policy_util.h"
7 #include "base/logging.h"
8 #include "base/values.h"
9 #include "chromeos/network/network_profile.h"
10 #include "chromeos/network/network_ui_data.h"
11 #include "chromeos/network/onc/onc_merger.h"
12 #include "chromeos/network/onc/onc_normalizer.h"
13 #include "chromeos/network/onc/onc_signature.h"
14 #include "chromeos/network/onc/onc_translator.h"
15 #include "chromeos/network/onc/onc_utils.h"
16 #include "chromeos/network/shill_property_util.h"
17 #include "components/onc/onc_constants.h"
18 #include "third_party/cros_system_api/dbus/service_constants.h"
20 namespace chromeos {
22 namespace policy_util {
24 namespace {
26 // This fake credential contains a random postfix which is extremly unlikely to
27 // be used by any user.
28 const char kFakeCredential[] = "FAKE_CREDENTIAL_VPaJDV9x";
31 // Removes all kFakeCredential values from sensitive fields (determined by
32 // onc::FieldIsCredential) of |onc_object|.
33 void RemoveFakeCredentials(
34 const onc::OncValueSignature& signature,
35 base::DictionaryValue* onc_object) {
36 base::DictionaryValue::Iterator it(*onc_object);
37 while (!it.IsAtEnd()) {
38 base::Value* value = NULL;
39 std::string field_name = it.key();
40 // We need the non-const entry to remove nested values but DictionaryValue
41 // has no non-const iterator.
42 onc_object->GetWithoutPathExpansion(field_name, &value);
43 // Advance before delete.
44 it.Advance();
46 // If |value| is a dictionary, recurse.
47 base::DictionaryValue* nested_object = NULL;
48 if (value->GetAsDictionary(&nested_object)) {
49 const onc::OncFieldSignature* field_signature =
50 onc::GetFieldSignature(signature, field_name);
51 if (field_signature)
52 RemoveFakeCredentials(*field_signature->value_signature, nested_object);
53 else
54 LOG(ERROR) << "ONC has unrecoginzed field: " << field_name;
55 continue;
58 // If |value| is a string, check if it is a fake credential.
59 std::string string_value;
60 if (value->GetAsString(&string_value) &&
61 onc::FieldIsCredential(signature, field_name)) {
62 if (string_value == kFakeCredential) {
63 // The value wasn't modified by the UI, thus we remove the field to keep
64 // the existing value that is stored in Shill.
65 onc_object->RemoveWithoutPathExpansion(field_name, NULL);
67 // Otherwise, the value is set and modified by the UI, thus we keep that
68 // value to overwrite whatever is stored in Shill.
73 // Returns true if |policy| matches |actual_network|, which must be part of a
74 // ONC NetworkConfiguration. This should be the only such matching function
75 // within Chrome. Shill does such matching in several functions for network
76 // identification. For compatibility, we currently should stick to Shill's
77 // matching behavior.
78 bool IsPolicyMatching(const base::DictionaryValue& policy,
79 const base::DictionaryValue& actual_network) {
80 std::string policy_type;
81 policy.GetStringWithoutPathExpansion(::onc::network_config::kType,
82 &policy_type);
83 std::string actual_network_type;
84 actual_network.GetStringWithoutPathExpansion(::onc::network_config::kType,
85 &actual_network_type);
86 if (policy_type != actual_network_type)
87 return false;
89 if (actual_network_type == ::onc::network_type::kEthernet) {
90 const base::DictionaryValue* policy_ethernet = NULL;
91 policy.GetDictionaryWithoutPathExpansion(::onc::network_config::kEthernet,
92 &policy_ethernet);
93 const base::DictionaryValue* actual_ethernet = NULL;
94 actual_network.GetDictionaryWithoutPathExpansion(
95 ::onc::network_config::kEthernet, &actual_ethernet);
96 if (!policy_ethernet || !actual_ethernet)
97 return false;
99 std::string policy_auth;
100 policy_ethernet->GetStringWithoutPathExpansion(
101 ::onc::ethernet::kAuthentication, &policy_auth);
102 std::string actual_auth;
103 actual_ethernet->GetStringWithoutPathExpansion(
104 ::onc::ethernet::kAuthentication, &actual_auth);
105 return policy_auth == actual_auth;
106 } else if (actual_network_type == ::onc::network_type::kWiFi) {
107 const base::DictionaryValue* policy_wifi = NULL;
108 policy.GetDictionaryWithoutPathExpansion(::onc::network_config::kWiFi,
109 &policy_wifi);
110 const base::DictionaryValue* actual_wifi = NULL;
111 actual_network.GetDictionaryWithoutPathExpansion(
112 ::onc::network_config::kWiFi,
113 &actual_wifi);
114 if (!policy_wifi || !actual_wifi)
115 return false;
117 std::string policy_ssid;
118 policy_wifi->GetStringWithoutPathExpansion(::onc::wifi::kHexSSID,
119 &policy_ssid);
120 std::string actual_ssid;
121 actual_wifi->GetStringWithoutPathExpansion(::onc::wifi::kHexSSID,
122 &actual_ssid);
123 return (policy_ssid == actual_ssid);
125 return false;
128 // Returns true if AutoConnect is enabled by |policy| (as mandatory or
129 // recommended setting). Otherwise and on error returns false.
130 bool IsAutoConnectEnabledInPolicy(const base::DictionaryValue& policy) {
131 std::string type;
132 policy.GetStringWithoutPathExpansion(::onc::network_config::kType, &type);
134 std::string autoconnect_key;
135 std::string network_dict_key;
136 if (type == ::onc::network_type::kWiFi) {
137 network_dict_key = ::onc::network_config::kWiFi;
138 autoconnect_key = ::onc::wifi::kAutoConnect;
139 } else if (type == ::onc::network_type::kVPN) {
140 network_dict_key = ::onc::network_config::kVPN;
141 autoconnect_key = ::onc::vpn::kAutoConnect;
142 } else {
143 VLOG(2) << "Network type without autoconnect property.";
144 return false;
147 const base::DictionaryValue* network_dict = NULL;
148 policy.GetDictionaryWithoutPathExpansion(network_dict_key, &network_dict);
149 if (!network_dict) {
150 LOG(ERROR) << "ONC doesn't contain a " << network_dict_key
151 << " dictionary.";
152 return false;
155 bool autoconnect = false;
156 network_dict->GetBooleanWithoutPathExpansion(autoconnect_key, &autoconnect);
157 return autoconnect;
160 base::DictionaryValue* GetOrCreateDictionary(const std::string& key,
161 base::DictionaryValue* dict) {
162 base::DictionaryValue* inner_dict = NULL;
163 if (!dict->GetDictionaryWithoutPathExpansion(key, &inner_dict)) {
164 inner_dict = new base::DictionaryValue;
165 dict->SetWithoutPathExpansion(key, inner_dict);
167 return inner_dict;
170 base::DictionaryValue* GetOrCreateNestedDictionary(
171 const std::string& key1,
172 const std::string& key2,
173 base::DictionaryValue* dict) {
174 base::DictionaryValue* inner_dict = GetOrCreateDictionary(key1, dict);
175 return GetOrCreateDictionary(key2, inner_dict);
178 void ApplyGlobalAutoconnectPolicy(
179 NetworkProfile::Type profile_type,
180 base::DictionaryValue* augmented_onc_network) {
181 std::string type;
182 augmented_onc_network->GetStringWithoutPathExpansion(
183 ::onc::network_config::kType, &type);
184 if (type.empty()) {
185 LOG(ERROR) << "ONC dictionary with no Type.";
186 return;
189 // Managed dictionaries don't contain empty dictionaries (see onc_merger.cc),
190 // so add the Autoconnect dictionary in case Shill didn't report a value.
191 base::DictionaryValue* auto_connect_dictionary = NULL;
192 if (type == ::onc::network_type::kWiFi) {
193 auto_connect_dictionary =
194 GetOrCreateNestedDictionary(::onc::network_config::kWiFi,
195 ::onc::wifi::kAutoConnect,
196 augmented_onc_network);
197 } else if (type == ::onc::network_type::kVPN) {
198 auto_connect_dictionary =
199 GetOrCreateNestedDictionary(::onc::network_config::kVPN,
200 ::onc::vpn::kAutoConnect,
201 augmented_onc_network);
202 } else {
203 return; // Network type without auto-connect property.
206 std::string policy_source;
207 if (profile_type == NetworkProfile::TYPE_USER)
208 policy_source = ::onc::kAugmentationUserPolicy;
209 else if (profile_type == NetworkProfile::TYPE_SHARED)
210 policy_source = ::onc::kAugmentationDevicePolicy;
211 else
212 NOTREACHED();
214 auto_connect_dictionary->SetBooleanWithoutPathExpansion(policy_source, false);
215 auto_connect_dictionary->SetStringWithoutPathExpansion(
216 ::onc::kAugmentationEffectiveSetting, policy_source);
219 } // namespace
221 scoped_ptr<base::DictionaryValue> CreateManagedONC(
222 const base::DictionaryValue* global_policy,
223 const base::DictionaryValue* network_policy,
224 const base::DictionaryValue* user_settings,
225 const base::DictionaryValue* active_settings,
226 const NetworkProfile* profile) {
227 const base::DictionaryValue* user_policy = NULL;
228 const base::DictionaryValue* device_policy = NULL;
229 const base::DictionaryValue* nonshared_user_settings = NULL;
230 const base::DictionaryValue* shared_user_settings = NULL;
232 if (profile) {
233 if (profile->type() == NetworkProfile::TYPE_SHARED) {
234 device_policy = network_policy;
235 shared_user_settings = user_settings;
236 } else if (profile->type() == NetworkProfile::TYPE_USER) {
237 user_policy = network_policy;
238 nonshared_user_settings = user_settings;
239 } else {
240 NOTREACHED();
244 // This call also removes credentials from policies.
245 scoped_ptr<base::DictionaryValue> augmented_onc_network =
246 onc::MergeSettingsAndPoliciesToAugmented(
247 onc::kNetworkConfigurationSignature,
248 user_policy,
249 device_policy,
250 nonshared_user_settings,
251 shared_user_settings,
252 active_settings);
254 // If present, apply the Autoconnect policy only to networks that are not
255 // managed by policy.
256 if (!network_policy && global_policy && profile) {
257 bool allow_only_policy_autoconnect = false;
258 global_policy->GetBooleanWithoutPathExpansion(
259 ::onc::global_network_config::kAllowOnlyPolicyNetworksToAutoconnect,
260 &allow_only_policy_autoconnect);
261 if (allow_only_policy_autoconnect) {
262 ApplyGlobalAutoconnectPolicy(profile->type(),
263 augmented_onc_network.get());
267 return augmented_onc_network.Pass();
270 void SetShillPropertiesForGlobalPolicy(
271 const base::DictionaryValue& shill_dictionary,
272 const base::DictionaryValue& global_network_policy,
273 base::DictionaryValue* shill_properties_to_update) {
274 // kAllowOnlyPolicyNetworksToAutoconnect is currently the only global config.
276 std::string type;
277 shill_dictionary.GetStringWithoutPathExpansion(shill::kTypeProperty, &type);
278 if (NetworkTypePattern::Ethernet().MatchesType(type))
279 return; // Autoconnect for Ethernet cannot be configured.
281 // By default all networks are allowed to autoconnect.
282 bool only_policy_autoconnect = false;
283 global_network_policy.GetBooleanWithoutPathExpansion(
284 ::onc::global_network_config::kAllowOnlyPolicyNetworksToAutoconnect,
285 &only_policy_autoconnect);
286 if (!only_policy_autoconnect)
287 return;
289 bool old_autoconnect = false;
290 if (shill_dictionary.GetBooleanWithoutPathExpansion(
291 shill::kAutoConnectProperty, &old_autoconnect) &&
292 !old_autoconnect) {
293 // Autoconnect is already explictly disabled. No need to set it again.
294 return;
297 // If autconnect is not explicitly set yet, it might automatically be enabled
298 // by Shill. To prevent that, disable it explicitly.
299 shill_properties_to_update->SetBooleanWithoutPathExpansion(
300 shill::kAutoConnectProperty, false);
303 scoped_ptr<base::DictionaryValue> CreateShillConfiguration(
304 const NetworkProfile& profile,
305 const std::string& guid,
306 const base::DictionaryValue* global_policy,
307 const base::DictionaryValue* network_policy,
308 const base::DictionaryValue* user_settings) {
309 scoped_ptr<base::DictionaryValue> effective;
310 ::onc::ONCSource onc_source = ::onc::ONC_SOURCE_NONE;
311 if (network_policy) {
312 if (profile.type() == NetworkProfile::TYPE_SHARED) {
313 effective = onc::MergeSettingsAndPoliciesToEffective(
314 NULL, // no user policy
315 network_policy, // device policy
316 NULL, // no user settings
317 user_settings); // shared settings
318 onc_source = ::onc::ONC_SOURCE_DEVICE_POLICY;
319 } else if (profile.type() == NetworkProfile::TYPE_USER) {
320 effective = onc::MergeSettingsAndPoliciesToEffective(
321 network_policy, // user policy
322 NULL, // no device policy
323 user_settings, // user settings
324 NULL); // no shared settings
325 onc_source = ::onc::ONC_SOURCE_USER_POLICY;
326 } else {
327 NOTREACHED();
329 } else if (user_settings) {
330 effective.reset(user_settings->DeepCopy());
331 // TODO(pneubeck): change to source ONC_SOURCE_USER
332 onc_source = ::onc::ONC_SOURCE_NONE;
333 } else {
334 NOTREACHED();
335 onc_source = ::onc::ONC_SOURCE_NONE;
338 RemoveFakeCredentials(onc::kNetworkConfigurationSignature,
339 effective.get());
341 effective->SetStringWithoutPathExpansion(::onc::network_config::kGUID, guid);
343 // Remove irrelevant fields.
344 onc::Normalizer normalizer(true /* remove recommended fields */);
345 effective = normalizer.NormalizeObject(&onc::kNetworkConfigurationSignature,
346 *effective);
348 scoped_ptr<base::DictionaryValue> shill_dictionary(
349 onc::TranslateONCObjectToShill(&onc::kNetworkConfigurationSignature,
350 *effective));
352 shill_dictionary->SetStringWithoutPathExpansion(shill::kProfileProperty,
353 profile.path);
355 // If AutoConnect is enabled by policy, set the ManagedCredentials property to
356 // indicate to Shill that this network can be used for autoconnect even
357 // without a manual and successful connection attempt.
358 // Note that this is only an indicator for the administrator's true intention,
359 // i.e. when the administrator enables AutoConnect, we assume that the network
360 // is indeed connectable.
361 // Ideally, we would know whether the (policy) provided credentials are
362 // complete and only set ManagedCredentials in that case.
363 if (network_policy && IsAutoConnectEnabledInPolicy(*network_policy)) {
364 VLOG(1) << "Enable ManagedCredentials for managed network with GUID "
365 << guid;
366 shill_dictionary->SetBooleanWithoutPathExpansion(
367 shill::kManagedCredentialsProperty, true);
370 if (!network_policy && global_policy) {
371 // The network isn't managed. Global network policies have to be applied.
372 SetShillPropertiesForGlobalPolicy(
373 *shill_dictionary, *global_policy, shill_dictionary.get());
376 scoped_ptr<NetworkUIData> ui_data(NetworkUIData::CreateFromONC(onc_source));
378 if (user_settings) {
379 // Shill doesn't know that sensitive data is contained in the UIData
380 // property and might write it into logs or other insecure places. Thus, we
381 // have to remove or mask credentials.
383 // Shill's GetProperties doesn't return credentials. Masking credentials
384 // instead of just removing them, allows remembering if a credential is set
385 // or not.
386 scoped_ptr<base::DictionaryValue> sanitized_user_settings(
387 onc::MaskCredentialsInOncObject(onc::kNetworkConfigurationSignature,
388 *user_settings,
389 kFakeCredential));
390 ui_data->set_user_settings(sanitized_user_settings.Pass());
393 shill_property_util::SetUIData(*ui_data, shill_dictionary.get());
395 VLOG(2) << "Created Shill properties: " << *shill_dictionary;
397 return shill_dictionary.Pass();
400 const base::DictionaryValue* FindMatchingPolicy(
401 const GuidToPolicyMap& policies,
402 const base::DictionaryValue& actual_network) {
403 for (GuidToPolicyMap::const_iterator it = policies.begin();
404 it != policies.end(); ++it) {
405 if (IsPolicyMatching(*it->second, actual_network))
406 return it->second;
408 return NULL;
411 } // namespace policy_util
413 } // namespace chromeos