Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(443)

Side by Side Diff: chromeos/network/managed_network_configuration_handler.cc

Issue 13957012: Adding a NetworkProfileHandler used by ManagedNetworkConfigurationHandler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chromeos/network/managed_network_configuration_handler.h" 5 #include "chromeos/network/managed_network_configuration_handler.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/guid.h" 11 #include "base/guid.h"
12 #include "base/json/json_writer.h" 12 #include "base/json/json_writer.h"
13 #include "base/location.h" 13 #include "base/location.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/memory/ref_counted.h" 15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h" 16 #include "base/memory/scoped_ptr.h"
17 #include "base/stl_util.h" 17 #include "base/stl_util.h"
18 #include "base/string_util.h"
18 #include "base/values.h" 19 #include "base/values.h"
19 #include "chromeos/dbus/dbus_method_call_status.h" 20 #include "chromeos/dbus/dbus_method_call_status.h"
20 #include "chromeos/dbus/dbus_thread_manager.h" 21 #include "chromeos/dbus/dbus_thread_manager.h"
21 #include "chromeos/dbus/shill_manager_client.h" 22 #include "chromeos/dbus/shill_manager_client.h"
22 #include "chromeos/dbus/shill_profile_client.h" 23 #include "chromeos/dbus/shill_profile_client.h"
23 #include "chromeos/dbus/shill_service_client.h" 24 #include "chromeos/dbus/shill_service_client.h"
24 #include "chromeos/network/network_configuration_handler.h" 25 #include "chromeos/network/network_configuration_handler.h"
25 #include "chromeos/network/network_event_log.h" 26 #include "chromeos/network/network_event_log.h"
26 #include "chromeos/network/network_handler_callbacks.h" 27 #include "chromeos/network/network_handler_callbacks.h"
28 #include "chromeos/network/network_profile.h"
29 #include "chromeos/network/network_profile_handler.h"
27 #include "chromeos/network/network_state.h" 30 #include "chromeos/network/network_state.h"
28 #include "chromeos/network/network_state_handler.h" 31 #include "chromeos/network/network_state_handler.h"
29 #include "chromeos/network/network_ui_data.h" 32 #include "chromeos/network/network_ui_data.h"
30 #include "chromeos/network/onc/onc_constants.h" 33 #include "chromeos/network/onc/onc_constants.h"
31 #include "chromeos/network/onc/onc_merger.h" 34 #include "chromeos/network/onc/onc_merger.h"
32 #include "chromeos/network/onc/onc_signature.h" 35 #include "chromeos/network/onc/onc_signature.h"
33 #include "chromeos/network/onc/onc_translator.h" 36 #include "chromeos/network/onc/onc_translator.h"
34 #include "chromeos/network/onc/onc_utils.h" 37 #include "chromeos/network/onc/onc_utils.h"
35 #include "chromeos/network/onc/onc_validator.h" 38 #include "chromeos/network/onc/onc_validator.h"
36 #include "dbus/object_path.h" 39 #include "dbus/object_path.h"
37 #include "third_party/cros_system_api/dbus/service_constants.h" 40 #include "third_party/cros_system_api/dbus/service_constants.h"
38 41
39 namespace chromeos { 42 namespace chromeos {
40 43
41 namespace { 44 namespace {
42 45
43 ManagedNetworkConfigurationHandler* g_configuration_handler_instance = NULL;
44
45 const char kLogModule[] = "ManagedNetworkConfigurationHandler"; 46 const char kLogModule[] = "ManagedNetworkConfigurationHandler";
46 47
47 // These are error strings used for error callbacks. None of these error 48 // These are error strings used for error callbacks. None of these error
48 // messages are user-facing: they should only appear in logs. 49 // messages are user-facing: they should only appear in logs.
49 const char kInvalidUserSettingsMessage[] = "User settings are invalid."; 50 const char kInvalidUserSettingsMessage[] = "User settings are invalid.";
50 const char kInvalidUserSettings[] = "Error.InvalidUserSettings"; 51 const char kInvalidUserSettings[] = "Error.InvalidUserSettings";
51 const char kNetworkAlreadyConfiguredMessage[] = 52 const char kNetworkAlreadyConfiguredMessage[] =
52 "Network is already configured."; 53 "Network is already configured.";
53 const char kNetworkAlreadyConfigured[] = "Error.NetworkAlreadyConfigured"; 54 const char kNetworkAlreadyConfigured[] = "Error.NetworkAlreadyConfigured";
54 const char kPoliciesNotInitializedMessage[] = "Policies not initialized."; 55 const char kPoliciesNotInitializedMessage[] = "Policies not initialized.";
55 const char kPoliciesNotInitialized[] = "Error.PoliciesNotInitialized"; 56 const char kPoliciesNotInitialized[] = "Error.PoliciesNotInitialized";
57 const char kProfileNotInitializedMessage[] = "Profile not initialized.";
58 const char kProfileNotInitialized[] = "Error.ProflieNotInitialized";
56 const char kServicePath[] = "servicePath"; 59 const char kServicePath[] = "servicePath";
57 const char kSetOnUnconfiguredNetworkMessage[] = 60 const char kSetOnUnconfiguredNetworkMessage[] =
58 "Unable to modify properties of an unconfigured network."; 61 "Unable to modify properties of an unconfigured network.";
59 const char kSetOnUnconfiguredNetwork[] = "Error.SetCalledOnUnconfiguredNetwork"; 62 const char kSetOnUnconfiguredNetwork[] = "Error.SetCalledOnUnconfiguredNetwork";
60 const char kUIDataErrorMessage[] = "UI data contains errors."; 63 const char kUIDataErrorMessage[] = "UI data contains errors.";
61 const char kUIDataError[] = "Error.UIData"; 64 const char kUIDataError[] = "Error.UIData";
65 const char kUnknownProfilePathMessage[] = "Profile path is unknown.";
66 const char kUnknownProfilePath[] = "Error.UnknownProfilePath";
62 const char kUnknownServicePathMessage[] = "Service path is unknown."; 67 const char kUnknownServicePathMessage[] = "Service path is unknown.";
63 const char kUnknownServicePath[] = "Error.UnknownServicePath"; 68 const char kUnknownServicePath[] = "Error.UnknownServicePath";
64 69
65 enum ProfileType {
66 PROFILE_NONE, // Not in any profile.
67 PROFILE_SHARED, // In the shared profile, shared by all users on device.
68 PROFILE_USER // In the user profile, not visible to other users.
69 };
70
71 const char kSharedProfilePath[] = "/profile/default";
72 const char kUserProfilePath[] = "/profile/chronos/shill";
73
74 // This fake credential contains a random postfix which is extremly unlikely to 70 // This fake credential contains a random postfix which is extremly unlikely to
75 // be used by any user. 71 // be used by any user.
76 const char kFakeCredential[] = "FAKE_CREDENTIAL_VPaJDV9x"; 72 const char kFakeCredential[] = "FAKE_CREDENTIAL_VPaJDV9x";
77 73
74 std::string ToDebugString(onc::ONCSource source,
75 const std::string& userhash) {
76 return source == onc::ONC_SOURCE_USER_POLICY ?
77 ("user policy of " + userhash) : "device policy";
78 }
79
78 void RunErrorCallback(const std::string& service_path, 80 void RunErrorCallback(const std::string& service_path,
79 const std::string& error_name, 81 const std::string& error_name,
80 const std::string& error_message, 82 const std::string& error_message,
81 const network_handler::ErrorCallback& error_callback) { 83 const network_handler::ErrorCallback& error_callback) {
82 network_event_log::AddEntry(kLogModule, error_name, error_message); 84 network_event_log::AddEntry(kLogModule, error_name, error_message);
83 error_callback.Run( 85 error_callback.Run(
84 error_name, 86 error_name,
85 make_scoped_ptr( 87 make_scoped_ptr(
86 network_handler::CreateErrorData(service_path, 88 network_handler::CreateErrorData(service_path,
87 error_name, 89 error_name,
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 // value to overwrite whatever is stored in Shill. 177 // value to overwrite whatever is stored in Shill.
176 } 178 }
177 } 179 }
178 } 180 }
179 181
180 // Creates a Shill property dictionary from the given arguments. The resulting 182 // Creates a Shill property dictionary from the given arguments. The resulting
181 // dictionary will be sent to Shill by the caller. Depending on the profile 183 // dictionary will be sent to Shill by the caller. Depending on the profile
182 // path, |policy| is interpreted as the user or device policy and |settings| as 184 // path, |policy| is interpreted as the user or device policy and |settings| as
183 // the user or shared settings. 185 // the user or shared settings.
184 scoped_ptr<base::DictionaryValue> CreateShillConfiguration( 186 scoped_ptr<base::DictionaryValue> CreateShillConfiguration(
185 const std::string& profile_path, 187 const NetworkProfile& profile,
186 const std::string& guid, 188 const std::string& guid,
187 const base::DictionaryValue* policy, 189 const base::DictionaryValue* policy,
188 const base::DictionaryValue* settings) { 190 const base::DictionaryValue* settings) {
189 scoped_ptr<base::DictionaryValue> effective; 191 scoped_ptr<base::DictionaryValue> effective;
190
191 onc::ONCSource onc_source; 192 onc::ONCSource onc_source;
192 if (policy) { 193 if (policy) {
193 if (profile_path == kSharedProfilePath) { 194 if (profile.type() == NetworkProfile::TYPE_SHARED) {
194 effective = onc::MergeSettingsAndPoliciesToEffective( 195 effective = onc::MergeSettingsAndPoliciesToEffective(
195 NULL, // no user policy 196 NULL, // no user policy
196 policy, // device policy 197 policy, // device policy
197 NULL, // no user settings 198 NULL, // no user settings
198 settings); // shared settings 199 settings); // shared settings
199 onc_source = onc::ONC_SOURCE_DEVICE_POLICY; 200 onc_source = onc::ONC_SOURCE_DEVICE_POLICY;
200 } else { 201 } else if (profile.type() == NetworkProfile::TYPE_USER) {
201 effective = onc::MergeSettingsAndPoliciesToEffective( 202 effective = onc::MergeSettingsAndPoliciesToEffective(
202 policy, // user policy 203 policy, // user policy
203 NULL, // no device policy 204 NULL, // no device policy
204 settings, // user settings 205 settings, // user settings
205 NULL); // no shared settings 206 NULL); // no shared settings
206 onc_source = onc::ONC_SOURCE_USER_POLICY; 207 onc_source = onc::ONC_SOURCE_USER_POLICY;
208 } else {
209 NOTREACHED();
207 } 210 }
208 } else if (settings) { 211 } else if (settings) {
209 effective.reset(settings->DeepCopy()); 212 effective.reset(settings->DeepCopy());
210 // TODO(pneubeck): change to source ONC_SOURCE_USER 213 // TODO(pneubeck): change to source ONC_SOURCE_USER
211 onc_source = onc::ONC_SOURCE_NONE; 214 onc_source = onc::ONC_SOURCE_NONE;
212 } else { 215 } else {
213 NOTREACHED(); 216 NOTREACHED();
214 onc_source = onc::ONC_SOURCE_NONE; 217 onc_source = onc::ONC_SOURCE_NONE;
215 } 218 }
216 219
217 RemoveFakeCredentials(onc::kNetworkConfigurationSignature, 220 RemoveFakeCredentials(onc::kNetworkConfigurationSignature,
218 effective.get()); 221 effective.get());
219 222
220 effective->SetStringWithoutPathExpansion(onc::network_config::kGUID, guid); 223 effective->SetStringWithoutPathExpansion(onc::network_config::kGUID, guid);
221 224
222 scoped_ptr<base::DictionaryValue> shill_dictionary( 225 scoped_ptr<base::DictionaryValue> shill_dictionary(
223 onc::TranslateONCObjectToShill(&onc::kNetworkConfigurationSignature, 226 onc::TranslateONCObjectToShill(&onc::kNetworkConfigurationSignature,
224 *effective)); 227 *effective));
225 228
226 shill_dictionary->SetStringWithoutPathExpansion(flimflam::kProfileProperty, 229 shill_dictionary->SetStringWithoutPathExpansion(flimflam::kProfileProperty,
227 profile_path); 230 profile.path);
228 231
229 scoped_ptr<NetworkUIData> ui_data; 232 scoped_ptr<NetworkUIData> ui_data;
230 if (policy) 233 if (policy)
231 ui_data = CreateUIDataFromONC(onc_source, *policy); 234 ui_data = CreateUIDataFromONC(onc_source, *policy);
232 else 235 else
233 ui_data.reset(new NetworkUIData()); 236 ui_data.reset(new NetworkUIData());
234 237
235 if (settings) { 238 if (settings) {
236 // Shill doesn't know that sensitive data is contained in the UIData 239 // Shill doesn't know that sensitive data is contained in the UIData
237 // property and might write it into logs or other insecure places. Thus, we 240 // property and might write it into logs or other insecure places. Thus, we
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 policy.GetStringWithoutPathExpansion(onc::wifi::kSSID, &policy_ssid); 279 policy.GetStringWithoutPathExpansion(onc::wifi::kSSID, &policy_ssid);
277 std::string network_ssid; 280 std::string network_ssid;
278 onc_network_part.GetStringWithoutPathExpansion(onc::wifi::kSSID, 281 onc_network_part.GetStringWithoutPathExpansion(onc::wifi::kSSID,
279 &network_ssid); 282 &network_ssid);
280 return (policy_ssid == network_ssid); 283 return (policy_ssid == network_ssid);
281 } 284 }
282 285
283 // Returns the policy of |policies| matching |onc_network_part|, if any 286 // Returns the policy of |policies| matching |onc_network_part|, if any
284 // exists. Returns NULL otherwise. 287 // exists. Returns NULL otherwise.
285 const base::DictionaryValue* FindMatchingPolicy( 288 const base::DictionaryValue* FindMatchingPolicy(
286 const ManagedNetworkConfigurationHandler::PolicyMap &policies, 289 const ManagedNetworkConfigurationHandler::GuidToPolicy &policies,
287 const base::DictionaryValue& onc_network_part) { 290 const base::DictionaryValue& onc_network_part) {
288 for (ManagedNetworkConfigurationHandler::PolicyMap::const_iterator it = 291 for (ManagedNetworkConfigurationHandler::GuidToPolicy::const_iterator it =
289 policies.begin(); it != policies.end(); ++it) { 292 policies.begin(); it != policies.end(); ++it) {
290 if (IsPolicyMatching(*it->second, onc_network_part)) 293 if (IsPolicyMatching(*it->second, onc_network_part))
291 return it->second; 294 return it->second;
292 } 295 }
293 return NULL; 296 return NULL;
294 } 297 }
295 298
299 const base::DictionaryValue* GetByGUID(
300 const ManagedNetworkConfigurationHandler::GuidToPolicy &policies,
301 const std::string& guid) {
302 ManagedNetworkConfigurationHandler::GuidToPolicy::const_iterator it =
303 policies.find(guid);
304 if (it == policies.end())
305 return NULL;
306 return it->second;
307
308 }
309
296 void TranslatePropertiesToOncAndRunCallback( 310 void TranslatePropertiesToOncAndRunCallback(
297 const network_handler::DictionaryResultCallback& callback, 311 const network_handler::DictionaryResultCallback& callback,
298 const std::string& service_path, 312 const std::string& service_path,
299 const base::DictionaryValue& shill_properties) { 313 const base::DictionaryValue& shill_properties) {
300 scoped_ptr<base::DictionaryValue> onc_network( 314 scoped_ptr<base::DictionaryValue> onc_network(
301 onc::TranslateShillServiceToONCPart( 315 onc::TranslateShillServiceToONCPart(
302 shill_properties, 316 shill_properties,
303 &onc::kNetworkWithStateSignature)); 317 &onc::kNetworkWithStateSignature));
304 callback.Run(service_path, *onc_network); 318 callback.Run(service_path, *onc_network);
305 } 319 }
306 320
307 } // namespace 321 } // namespace
308 322
323 ManagedNetworkConfigurationHandler* g_configuration_handler_instance = NULL;
324
309 // static 325 // static
310 void ManagedNetworkConfigurationHandler::Initialize() { 326 void ManagedNetworkConfigurationHandler::Initialize(
327 NetworkProfileHandler* profile_handler) {
311 CHECK(!g_configuration_handler_instance); 328 CHECK(!g_configuration_handler_instance);
312 g_configuration_handler_instance = new ManagedNetworkConfigurationHandler; 329 g_configuration_handler_instance =
330 new ManagedNetworkConfigurationHandler(profile_handler);
313 } 331 }
314 332
315 // static 333 // static
316 bool ManagedNetworkConfigurationHandler::IsInitialized() { 334 bool ManagedNetworkConfigurationHandler::IsInitialized() {
317 return g_configuration_handler_instance; 335 return g_configuration_handler_instance;
318 } 336 }
319 337
320 // static 338 // static
321 void ManagedNetworkConfigurationHandler::Shutdown() { 339 void ManagedNetworkConfigurationHandler::Shutdown() {
322 CHECK(g_configuration_handler_instance); 340 CHECK(g_configuration_handler_instance);
323 delete g_configuration_handler_instance; 341 delete g_configuration_handler_instance;
324 g_configuration_handler_instance = NULL; 342 g_configuration_handler_instance = NULL;
325 } 343 }
326 344
327 // static 345 // static
328 ManagedNetworkConfigurationHandler* ManagedNetworkConfigurationHandler::Get() { 346 ManagedNetworkConfigurationHandler* ManagedNetworkConfigurationHandler::Get() {
329 CHECK(g_configuration_handler_instance); 347 CHECK(g_configuration_handler_instance);
330 return g_configuration_handler_instance; 348 return g_configuration_handler_instance;
331 } 349 }
332 350
333 void ManagedNetworkConfigurationHandler::GetManagedProperties( 351 void ManagedNetworkConfigurationHandler::GetManagedProperties(
352 const std::string& userhash,
334 const std::string& service_path, 353 const std::string& service_path,
335 const network_handler::DictionaryResultCallback& callback, 354 const network_handler::DictionaryResultCallback& callback,
336 const network_handler::ErrorCallback& error_callback) { 355 const network_handler::ErrorCallback& error_callback) {
337 if (!user_policies_initialized_ || !device_policies_initialized_) { 356 if (!GetPoliciesForUser(userhash) || !GetPoliciesForUser(std::string())) {
338 RunErrorCallback(service_path, 357 RunErrorCallback(service_path,
339 kPoliciesNotInitialized, 358 kPoliciesNotInitialized,
340 kPoliciesNotInitializedMessage, 359 kPoliciesNotInitializedMessage,
341 error_callback); 360 error_callback);
342 return; 361 return;
343 } 362 }
344 NetworkConfigurationHandler::Get()->GetProperties( 363 NetworkConfigurationHandler::Get()->GetProperties(
345 service_path, 364 service_path,
346 base::Bind( 365 base::Bind(
347 &ManagedNetworkConfigurationHandler::GetManagedPropertiesCallback, 366 &ManagedNetworkConfigurationHandler::GetManagedPropertiesCallback,
348 weak_ptr_factory_.GetWeakPtr(), 367 weak_ptr_factory_.GetWeakPtr(),
349 callback, 368 callback,
350 error_callback), 369 error_callback),
351 error_callback); 370 error_callback);
352 } 371 }
353 372
354 void ManagedNetworkConfigurationHandler::GetManagedPropertiesCallback( 373 void ManagedNetworkConfigurationHandler::GetManagedPropertiesCallback(
355 const network_handler::DictionaryResultCallback& callback, 374 const network_handler::DictionaryResultCallback& callback,
356 const network_handler::ErrorCallback& error_callback, 375 const network_handler::ErrorCallback& error_callback,
357 const std::string& service_path, 376 const std::string& service_path,
358 const base::DictionaryValue& shill_properties) { 377 const base::DictionaryValue& shill_properties) {
359 std::string profile_path; 378 std::string profile_path;
360 ProfileType profile_type = PROFILE_NONE; 379 shill_properties.GetStringWithoutPathExpansion(flimflam::kProfileProperty,
361 if (shill_properties.GetStringWithoutPathExpansion( 380 &profile_path);
362 flimflam::kProfileProperty, &profile_path)) { 381 const NetworkProfile* profile =
363 if (profile_path == kSharedProfilePath) 382 profile_handler_->GetProfileForPath(profile_path);
364 profile_type = PROFILE_SHARED; 383 if (!profile) {
365 else if (!profile_path.empty()) 384 VLOG(1) << "No or no known profile received for service "
366 profile_type = PROFILE_USER; 385 << service_path << ".";
367 } else {
368 VLOG(1) << "No profile path for service " << service_path << ".";
369 } 386 }
370 387
371 scoped_ptr<NetworkUIData> ui_data = GetUIData(shill_properties); 388 scoped_ptr<NetworkUIData> ui_data = GetUIData(shill_properties);
372 389
373 const base::DictionaryValue* user_settings = NULL; 390 const base::DictionaryValue* user_settings = NULL;
374 const base::DictionaryValue* shared_settings = NULL; 391 const base::DictionaryValue* shared_settings = NULL;
375 392
376 if (ui_data) { 393 if (ui_data && profile) {
377 if (profile_type == PROFILE_SHARED) 394 if (profile->type() == NetworkProfile::TYPE_SHARED)
378 shared_settings = ui_data->user_settings(); 395 shared_settings = ui_data->user_settings();
379 else if (profile_type == PROFILE_USER) 396 else if (profile->type() == NetworkProfile::TYPE_USER)
380 user_settings = ui_data->user_settings(); 397 user_settings = ui_data->user_settings();
381 } else if (profile_type != PROFILE_NONE) { 398 else
399 NOTREACHED();
400 } else if (profile) {
382 LOG(WARNING) << "Service " << service_path << " of profile " 401 LOG(WARNING) << "Service " << service_path << " of profile "
383 << profile_path << " contains no or no valid UIData."; 402 << profile_path << " contains no or no valid UIData.";
384 // TODO(pneubeck): add a conversion of user configured entries of old 403 // TODO(pneubeck): add a conversion of user configured entries of old
385 // ChromeOS versions. We will have to use a heuristic to determine which 404 // ChromeOS versions. We will have to use a heuristic to determine which
386 // properties _might_ be user configured. 405 // properties _might_ be user configured.
387 } 406 }
388 407
389 scoped_ptr<base::DictionaryValue> active_settings( 408 scoped_ptr<base::DictionaryValue> active_settings(
390 onc::TranslateShillServiceToONCPart( 409 onc::TranslateShillServiceToONCPart(
391 shill_properties, 410 shill_properties,
392 &onc::kNetworkWithStateSignature)); 411 &onc::kNetworkWithStateSignature));
393 412
394 std::string guid; 413 std::string guid;
395 active_settings->GetStringWithoutPathExpansion(onc::network_config::kGUID, 414 active_settings->GetStringWithoutPathExpansion(onc::network_config::kGUID,
396 &guid); 415 &guid);
397 416
398 const base::DictionaryValue* user_policy = NULL; 417 const base::DictionaryValue* user_policy = NULL;
399 const base::DictionaryValue* device_policy = NULL; 418 const base::DictionaryValue* device_policy = NULL;
400 if (!guid.empty()) { 419 if (!guid.empty() && profile) {
401 // We already checked that the policies were initialized. No need to do that 420 const GuidToPolicy* policies = GetPoliciesForProfile(*profile);
402 // again. 421 if (!policies) {
403 if (profile_type == PROFILE_SHARED) 422 RunErrorCallback(service_path,
404 device_policy = device_policies_by_guid_[guid]; 423 kPoliciesNotInitialized,
405 else if (profile_type == PROFILE_USER) 424 kPoliciesNotInitializedMessage,
406 user_policy = user_policies_by_guid_[guid]; 425 error_callback);
426 return;
427 }
428 const base::DictionaryValue* policy = GetByGUID(*policies, guid);
429 if (profile->type() == NetworkProfile::TYPE_SHARED)
430 device_policy = policy;
431 else if (profile->type() == NetworkProfile::TYPE_USER)
432 user_policy = policy;
433 else
434 NOTREACHED();
407 } 435 }
408 436
409 // This call also removes credentials from policies. 437 // This call also removes credentials from policies.
410 scoped_ptr<base::DictionaryValue> augmented_properties = 438 scoped_ptr<base::DictionaryValue> augmented_properties =
411 onc::MergeSettingsAndPoliciesToAugmented( 439 onc::MergeSettingsAndPoliciesToAugmented(
412 onc::kNetworkConfigurationSignature, 440 onc::kNetworkConfigurationSignature,
413 user_policy, 441 user_policy,
414 device_policy, 442 device_policy,
415 user_settings, 443 user_settings,
416 shared_settings, 444 shared_settings,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 // TODO(pneubeck): create an initial configuration in this case. As for 477 // TODO(pneubeck): create an initial configuration in this case. As for
450 // CreateConfiguration, user settings from older ChromeOS versions have to 478 // CreateConfiguration, user settings from older ChromeOS versions have to
451 // determined here. 479 // determined here.
452 RunErrorCallback(service_path, 480 RunErrorCallback(service_path,
453 kSetOnUnconfiguredNetwork, 481 kSetOnUnconfiguredNetwork,
454 kSetOnUnconfiguredNetworkMessage, 482 kSetOnUnconfiguredNetworkMessage,
455 error_callback); 483 error_callback);
456 return; 484 return;
457 } 485 }
458 486
487 const std::string& profile_path = state->profile_path();
488 const NetworkProfile *profile =
489 profile_handler_->GetProfileForPath(profile_path);
490 if (!profile) {
491 RunErrorCallback(service_path,
492 kUnknownProfilePath,
493 kUnknownProfilePathMessage,
494 error_callback);
495 return;
496 }
497
498 VLOG(2) << "SetProperties: Found GUID " << guid << " and profile "
499 << profile->ToDebugString();
500
501 const GuidToPolicy* policies = GetPoliciesForProfile(*profile);
502 if (!policies) {
503 RunErrorCallback(service_path,
504 kPoliciesNotInitialized,
505 kPoliciesNotInitializedMessage,
506 error_callback);
507 return;
508 }
509
459 // Validate the ONC dictionary. We are liberal and ignore unknown field 510 // Validate the ONC dictionary. We are liberal and ignore unknown field
460 // names. User settings are only partial ONC, thus we ignore missing fields. 511 // names. User settings are only partial ONC, thus we ignore missing fields.
461 onc::Validator validator(false, // Ignore unknown fields. 512 onc::Validator validator(false, // Ignore unknown fields.
462 false, // Ignore invalid recommended field names. 513 false, // Ignore invalid recommended field names.
463 false, // Ignore missing fields. 514 false, // Ignore missing fields.
464 false); // This ONC does not comes from policy. 515 false); // This ONC does not comes from policy.
465 516
466 onc::Validator::Result validation_result; 517 onc::Validator::Result validation_result;
467 scoped_ptr<base::DictionaryValue> validated_user_settings = 518 scoped_ptr<base::DictionaryValue> validated_user_settings =
468 validator.ValidateAndRepairObject( 519 validator.ValidateAndRepairObject(
469 &onc::kNetworkConfigurationSignature, 520 &onc::kNetworkConfigurationSignature,
470 user_settings, 521 user_settings,
471 &validation_result); 522 &validation_result);
472 523
473 if (validation_result == onc::Validator::INVALID) { 524 if (validation_result == onc::Validator::INVALID) {
474 LOG(ERROR) << "ONC user settings are invalid and couldn't be repaired.";
475 RunErrorCallback(service_path, 525 RunErrorCallback(service_path,
476 kInvalidUserSettings, 526 kInvalidUserSettings,
477 kInvalidUserSettingsMessage, 527 kInvalidUserSettingsMessage,
478 error_callback); 528 error_callback);
479 return; 529 return;
480 } 530 }
481 if (validation_result == onc::Validator::VALID_WITH_WARNINGS) 531 if (validation_result == onc::Validator::VALID_WITH_WARNINGS)
482 LOG(WARNING) << "Validation of ONC user settings produced warnings."; 532 LOG(WARNING) << "Validation of ONC user settings produced warnings.";
483 533
484 VLOG(2) << "SetProperties: Found GUID " << guid << " and profile " 534 const base::DictionaryValue* policy = GetByGUID(*policies, guid);
485 << state->profile_path();
486
487 const PolicyMap* policies_by_guid =
488 GetPoliciesForProfile(state->profile_path());
489
490 if (!policies_by_guid) {
491 RunErrorCallback(service_path,
492 kPoliciesNotInitialized,
493 kPoliciesNotInitializedMessage,
494 error_callback);
495 return;
496 }
497
498 const base::DictionaryValue* policy = NULL;
499 PolicyMap::const_iterator it = policies_by_guid->find(guid);
500 if (it != policies_by_guid->end())
501 policy = it->second;
502
503 VLOG(2) << "This configuration is " << (policy ? "" : "not ") << "managed."; 535 VLOG(2) << "This configuration is " << (policy ? "" : "not ") << "managed.";
504 536
505 scoped_ptr<base::DictionaryValue> shill_dictionary( 537 scoped_ptr<base::DictionaryValue> shill_dictionary(
506 CreateShillConfiguration(state->profile_path(), guid, policy, 538 CreateShillConfiguration(*profile, guid, policy, &user_settings));
507 &user_settings));
508 539
509 NetworkConfigurationHandler::Get()->SetProperties(service_path, 540 NetworkConfigurationHandler::Get()->SetProperties(service_path,
510 *shill_dictionary, 541 *shill_dictionary,
511 callback, 542 callback,
512 error_callback); 543 error_callback);
513 } 544 }
514 545
515 void ManagedNetworkConfigurationHandler::Connect( 546 void ManagedNetworkConfigurationHandler::Connect(
516 const std::string& service_path, 547 const std::string& service_path,
517 const base::Closure& callback, 548 const base::Closure& callback,
518 const network_handler::ErrorCallback& error_callback) const { 549 const network_handler::ErrorCallback& error_callback) const {
519 NetworkConfigurationHandler::Get()->Connect(service_path, 550 NetworkConfigurationHandler::Get()->Connect(service_path,
520 callback, 551 callback,
521 error_callback); 552 error_callback);
522 } 553 }
523 554
524 void ManagedNetworkConfigurationHandler::Disconnect( 555 void ManagedNetworkConfigurationHandler::Disconnect(
525 const std::string& service_path, 556 const std::string& service_path,
526 const base::Closure& callback, 557 const base::Closure& callback,
527 const network_handler::ErrorCallback& error_callback) const { 558 const network_handler::ErrorCallback& error_callback) const {
528 NetworkConfigurationHandler::Get()->Disconnect(service_path, 559 NetworkConfigurationHandler::Get()->Disconnect(service_path,
529 callback, 560 callback,
530 error_callback); 561 error_callback);
531 } 562 }
532 563
533 void ManagedNetworkConfigurationHandler::CreateConfiguration( 564 void ManagedNetworkConfigurationHandler::CreateConfiguration(
565 const std::string& userhash,
534 const base::DictionaryValue& properties, 566 const base::DictionaryValue& properties,
535 const network_handler::StringResultCallback& callback, 567 const network_handler::StringResultCallback& callback,
536 const network_handler::ErrorCallback& error_callback) const { 568 const network_handler::ErrorCallback& error_callback) const {
537 std::string profile_path = kUserProfilePath; 569 const GuidToPolicy* policies = GetPoliciesForUser(userhash);
538 const PolicyMap* policies_by_guid = GetPoliciesForProfile(profile_path); 570 if (!policies) {
539
540 if (!policies_by_guid) {
541 RunErrorCallback("", 571 RunErrorCallback("",
542 kPoliciesNotInitialized, 572 kPoliciesNotInitialized,
543 kPoliciesNotInitializedMessage, 573 kPoliciesNotInitializedMessage,
544 error_callback); 574 error_callback);
545 return; 575 return;
546 } 576 }
547 577
548 if (FindMatchingPolicy(*policies_by_guid, properties)) { 578 if (FindMatchingPolicy(*policies, properties)) {
549 RunErrorCallback("", 579 RunErrorCallback("",
550 kNetworkAlreadyConfigured, 580 kNetworkAlreadyConfigured,
551 kNetworkAlreadyConfiguredMessage, 581 kNetworkAlreadyConfiguredMessage,
552 error_callback); 582 error_callback);
553 } 583 }
554 584
585 const NetworkProfile* profile =
586 profile_handler_->GetProfileForUserhash(userhash);
587 if (!profile) {
588 RunErrorCallback("",
589 kProfileNotInitialized,
590 kProfileNotInitializedMessage,
591 error_callback);
592 }
593
555 // TODO(pneubeck): In case of WiFi, check that no other configuration for the 594 // TODO(pneubeck): In case of WiFi, check that no other configuration for the
556 // same {SSID, mode, security} exists. We don't support such multiple 595 // same {SSID, mode, security} exists. We don't support such multiple
557 // configurations, yet. 596 // configurations, yet.
558 597
559 // Generate a new GUID for this configuration. Ignore the maybe provided GUID 598 // Generate a new GUID for this configuration. Ignore the maybe provided GUID
560 // in |properties| as it is not our own and from an untrusted source. 599 // in |properties| as it is not our own and from an untrusted source.
561 std::string guid = base::GenerateGUID(); 600 std::string guid = base::GenerateGUID();
562
563 scoped_ptr<base::DictionaryValue> shill_dictionary( 601 scoped_ptr<base::DictionaryValue> shill_dictionary(
564 CreateShillConfiguration(profile_path, guid, NULL /*no policy*/, 602 CreateShillConfiguration(*profile, guid, NULL /*no policy*/,
565 &properties)); 603 &properties));
566 604
567 NetworkConfigurationHandler::Get()->CreateConfiguration(*shill_dictionary, 605 NetworkConfigurationHandler::Get()->CreateConfiguration(*shill_dictionary,
568 callback, 606 callback,
569 error_callback); 607 error_callback);
570 } 608 }
571 609
572 void ManagedNetworkConfigurationHandler::RemoveConfiguration( 610 void ManagedNetworkConfigurationHandler::RemoveConfiguration(
573 const std::string& service_path, 611 const std::string& service_path,
574 const base::Closure& callback, 612 const base::Closure& callback,
575 const network_handler::ErrorCallback& error_callback) const { 613 const network_handler::ErrorCallback& error_callback) const {
576 NetworkConfigurationHandler::Get()->RemoveConfiguration(service_path, 614 NetworkConfigurationHandler::Get()->RemoveConfiguration(service_path,
577 callback, 615 callback,
578 error_callback); 616 error_callback);
579 } 617 }
580 618
581 // This class compares (entry point is Run()) |modified_policies| with the 619 // This class compares (entry point is Run()) |modified_policies| with the
582 // existing entries in the provided Shill profile |profile|. It fetches all 620 // existing entries in the provided Shill profile |profile|. It fetches all
583 // entries in parallel (GetProfileProperties), compares each entry with the 621 // entries in parallel (GetProfileProperties), compares each entry with the
584 // current policies (GetEntry) and adds all missing policies 622 // current policies (GetEntry) and adds all missing policies
585 // (~PolicyApplicator). 623 // (~PolicyApplicator).
586 class ManagedNetworkConfigurationHandler::PolicyApplicator 624 class ManagedNetworkConfigurationHandler::PolicyApplicator
587 : public base::RefCounted<PolicyApplicator> { 625 : public base::RefCounted<PolicyApplicator> {
588 public: 626 public:
589 typedef ManagedNetworkConfigurationHandler::PolicyMap PolicyMap; 627 typedef ManagedNetworkConfigurationHandler::GuidToPolicy GuidToPolicy;
590 628
591 // |modified_policies| must not be NULL and will be empty afterwards. 629 // |modified_policies| must not be NULL and will be empty afterwards.
592 PolicyApplicator(base::WeakPtr<ManagedNetworkConfigurationHandler> handler, 630 PolicyApplicator(base::WeakPtr<ManagedNetworkConfigurationHandler> handler,
593 const std::string& profile, 631 const NetworkProfile& profile,
594 std::set<std::string>* modified_policies) 632 std::set<std::string>* modified_policies)
595 : handler_(handler), 633 : handler_(handler),
596 profile_path_(profile) { 634 profile_(profile) {
597 remaining_policies_.swap(*modified_policies); 635 remaining_policies_.swap(*modified_policies);
598 } 636 }
599 637
600 void Run() { 638 void Run() {
601 DBusThreadManager::Get()->GetShillProfileClient()->GetProperties( 639 DBusThreadManager::Get()->GetShillProfileClient()->GetProperties(
602 dbus::ObjectPath(profile_path_), 640 dbus::ObjectPath(profile_.path),
603 base::Bind(&PolicyApplicator::GetProfileProperties, this), 641 base::Bind(&PolicyApplicator::GetProfileProperties, this),
604 base::Bind(&LogErrorMessage, FROM_HERE)); 642 base::Bind(&LogErrorMessage, FROM_HERE));
605 } 643 }
606 644
607 private: 645 private:
608 friend class base::RefCounted<PolicyApplicator>; 646 friend class base::RefCounted<PolicyApplicator>;
609 647
610 void GetProfileProperties(const base::DictionaryValue& profile_properties) { 648 void GetProfileProperties(const base::DictionaryValue& profile_properties) {
611 if (!handler_) { 649 if (!handler_) {
612 LOG(WARNING) << "Handler destructed during policy application to profile " 650 LOG(WARNING) << "Handler destructed during policy application to profile "
613 << profile_path_; 651 << profile_.ToDebugString();
614 return; 652 return;
615 } 653 }
616 654
617 VLOG(2) << "Received properties for profile " << profile_path_; 655 VLOG(2) << "Received properties for profile " << profile_.ToDebugString();
618 const base::ListValue* entries = NULL; 656 const base::ListValue* entries = NULL;
619 if (!profile_properties.GetListWithoutPathExpansion( 657 if (!profile_properties.GetListWithoutPathExpansion(
620 flimflam::kEntriesProperty, &entries)) { 658 flimflam::kEntriesProperty, &entries)) {
621 LOG(ERROR) << "Profile " << profile_path_ 659 LOG(ERROR) << "Profile " << profile_.ToDebugString()
622 << " doesn't contain the property " 660 << " doesn't contain the property "
623 << flimflam::kEntriesProperty; 661 << flimflam::kEntriesProperty;
624 return; 662 return;
625 } 663 }
626 664
627 for (base::ListValue::const_iterator it = entries->begin(); 665 for (base::ListValue::const_iterator it = entries->begin();
628 it != entries->end(); ++it) { 666 it != entries->end(); ++it) {
629 std::string entry; 667 std::string entry;
630 (*it)->GetAsString(&entry); 668 (*it)->GetAsString(&entry);
631 669
632 std::ostringstream entry_failure; 670 std::ostringstream entry_failure;
633 DBusThreadManager::Get()->GetShillProfileClient()->GetEntry( 671 DBusThreadManager::Get()->GetShillProfileClient()->GetEntry(
634 dbus::ObjectPath(profile_path_), 672 dbus::ObjectPath(profile_.path),
635 entry, 673 entry,
636 base::Bind(&PolicyApplicator::GetEntry, this, entry), 674 base::Bind(&PolicyApplicator::GetEntry, this, entry),
637 base::Bind(&LogErrorMessage, FROM_HERE)); 675 base::Bind(&LogErrorMessage, FROM_HERE));
638 } 676 }
639 } 677 }
640 678
641 void GetEntry(const std::string& entry, 679 void GetEntry(const std::string& entry,
642 const base::DictionaryValue& entry_properties) { 680 const base::DictionaryValue& entry_properties) {
643 if (!handler_) { 681 if (!handler_) {
644 LOG(WARNING) << "Handler destructed during policy application to profile " 682 LOG(WARNING) << "Handler destructed during policy application to profile "
645 << profile_path_; 683 << profile_.ToDebugString();
646 return; 684 return;
647 } 685 }
648 686
649 VLOG(2) << "Received properties for entry " << entry << " of profile " 687 VLOG(2) << "Received properties for entry " << entry << " of profile "
650 << profile_path_; 688 << profile_.ToDebugString();
651 689
652 scoped_ptr<base::DictionaryValue> onc_part( 690 scoped_ptr<base::DictionaryValue> onc_part(
653 onc::TranslateShillServiceToONCPart( 691 onc::TranslateShillServiceToONCPart(
654 entry_properties, 692 entry_properties,
655 &onc::kNetworkWithStateSignature)); 693 &onc::kNetworkWithStateSignature));
656 694
657 std::string old_guid; 695 std::string old_guid;
658 if (!onc_part->GetStringWithoutPathExpansion(onc::network_config::kGUID, 696 if (!onc_part->GetStringWithoutPathExpansion(onc::network_config::kGUID,
659 &old_guid)) { 697 &old_guid)) {
660 LOG(WARNING) << "Entry " << entry << " of profile " << profile_path_ 698 LOG(WARNING) << "Entry " << entry << " of profile "
661 << " doesn't contain a GUID."; 699 << profile_.ToDebugString() << " doesn't contain a GUID.";
662 // This might be an entry of an older ChromeOS version. Assume it to be 700 // This might be an entry of an older ChromeOS version. Assume it to be
663 // unmanaged. 701 // unmanaged.
664 return; 702 return;
665 } 703 }
666 704
667 scoped_ptr<NetworkUIData> ui_data = GetUIData(entry_properties); 705 scoped_ptr<NetworkUIData> ui_data = GetUIData(entry_properties);
668 if (!ui_data) { 706 if (!ui_data) {
669 VLOG(1) << "Entry " << entry << " of profile " << profile_path_ 707 VLOG(1) << "Entry " << entry << " of profile "
708 << profile_.ToDebugString()
670 << " contains no or no valid UIData."; 709 << " contains no or no valid UIData.";
671 // This might be an entry of an older ChromeOS version. Assume it to be 710 // This might be an entry of an older ChromeOS version. Assume it to be
672 // unmanaged. 711 // unmanaged.
673 return; 712 return;
674 } 713 }
675 714
676 bool was_managed = 715 bool was_managed =
677 (ui_data->onc_source() == onc::ONC_SOURCE_DEVICE_POLICY || 716 (ui_data->onc_source() == onc::ONC_SOURCE_DEVICE_POLICY ||
678 ui_data->onc_source() == onc::ONC_SOURCE_USER_POLICY); 717 ui_data->onc_source() == onc::ONC_SOURCE_USER_POLICY);
679 718
680 // The relevant policy must have been initialized, otherwise we hadn't Run 719 // The relevant policy must have been initialized, otherwise we hadn't Run
681 // this PolicyApplicator. 720 // this PolicyApplicator.
682 const PolicyMap& policies_by_guid = 721 const GuidToPolicy& policies = *handler_->GetPoliciesForProfile(profile_);
683 *handler_->GetPoliciesForProfile(profile_path_);
684 722
685 const base::DictionaryValue* new_policy = NULL; 723 const base::DictionaryValue* new_policy = NULL;
686 if (was_managed) { 724 if (was_managed) {
687 // If we have a GUID that might match a current policy, do a lookup using 725 // If we have a GUID that might match a current policy, do a lookup using
688 // that GUID at first. In particular this is necessary, as some networks 726 // that GUID at first. In particular this is necessary, as some networks
689 // can't be matched to policies by properties (e.g. VPN). 727 // can't be matched to policies by properties (e.g. VPN).
690 PolicyMap::const_iterator it = policies_by_guid.find(old_guid); 728 new_policy = GetByGUID(policies, old_guid);
691 if (it != policies_by_guid.end())
692 new_policy = it->second;
693 } 729 }
694 730
695 if (!new_policy) { 731 if (!new_policy) {
696 // If we didn't find a policy by GUID, still a new policy might match. 732 // If we didn't find a policy by GUID, still a new policy might match.
697 new_policy = FindMatchingPolicy(policies_by_guid, *onc_part); 733 new_policy = FindMatchingPolicy(policies, *onc_part);
698 } 734 }
699 735
700 if (new_policy) { 736 if (new_policy) {
701 std::string new_guid; 737 std::string new_guid;
702 new_policy->GetStringWithoutPathExpansion(onc::network_config::kGUID, 738 new_policy->GetStringWithoutPathExpansion(onc::network_config::kGUID,
703 &new_guid); 739 &new_guid);
704 740
705 VLOG_IF(1, was_managed && old_guid != new_guid) 741 VLOG_IF(1, was_managed && old_guid != new_guid)
706 << "Updating configuration previously managed by policy " << old_guid 742 << "Updating configuration previously managed by policy " << old_guid
707 << " with new policy " << new_guid << "."; 743 << " with new policy " << new_guid << ".";
708 VLOG_IF(1, !was_managed) 744 VLOG_IF(1, !was_managed)
709 << "Applying policy " << new_guid << " to previously unmanaged " 745 << "Applying policy " << new_guid << " to previously unmanaged "
710 << "configuration."; 746 << "configuration.";
711 747
712 if (old_guid == new_guid && 748 if (old_guid == new_guid &&
713 remaining_policies_.find(new_guid) == remaining_policies_.end()) { 749 remaining_policies_.find(new_guid) == remaining_policies_.end()) {
714 VLOG(1) << "Not updating existing managed configuration with guid " 750 VLOG(1) << "Not updating existing managed configuration with guid "
715 << new_guid << " because the policy didn't change."; 751 << new_guid << " because the policy didn't change.";
716 } else { 752 } else {
717 VLOG_IF(1, old_guid == new_guid) 753 VLOG_IF(1, old_guid == new_guid)
718 << "Updating previously managed configuration with the updated " 754 << "Updating previously managed configuration with the updated "
719 << "policy " << new_guid << "."; 755 << "policy " << new_guid << ".";
720 756
721 // Update the existing configuration with the maybe changed 757 // Update the existing configuration with the maybe changed
722 // policy. Thereby the GUID might change. 758 // policy. Thereby the GUID might change.
723 scoped_ptr<base::DictionaryValue> shill_dictionary = 759 scoped_ptr<base::DictionaryValue> shill_dictionary =
724 CreateShillConfiguration(profile_path_, new_guid, new_policy, 760 CreateShillConfiguration(profile_, new_guid, new_policy,
725 ui_data->user_settings()); 761 ui_data->user_settings());
726 NetworkConfigurationHandler::Get()->CreateConfiguration( 762 NetworkConfigurationHandler::Get()->CreateConfiguration(
727 *shill_dictionary, 763 *shill_dictionary,
728 base::Bind(&IgnoreString), 764 base::Bind(&IgnoreString),
729 base::Bind(&LogErrorWithDict, FROM_HERE)); 765 base::Bind(&LogErrorWithDict, FROM_HERE));
730 remaining_policies_.erase(new_guid); 766 remaining_policies_.erase(new_guid);
731 } 767 }
732 } else if (was_managed) { 768 } else if (was_managed) {
733 VLOG(1) << "Removing configuration previously managed by policy " 769 VLOG(1) << "Removing configuration previously managed by policy "
734 << old_guid << ", because the policy was removed."; 770 << old_guid << ", because the policy was removed.";
735 771
736 // Remove the entry, because the network was managed but isn't anymore. 772 // Remove the entry, because the network was managed but isn't anymore.
737 // Note: An alternative might be to preserve the user settings, but it's 773 // Note: An alternative might be to preserve the user settings, but it's
738 // unclear which values originating the policy should be removed. 774 // unclear which values originating the policy should be removed.
739 DeleteEntry(entry); 775 DeleteEntry(entry);
740 } else { 776 } else {
741 VLOG(2) << "Ignore unmanaged entry."; 777 VLOG(2) << "Ignore unmanaged entry.";
742 778
743 // The entry wasn't managed and doesn't match any current policy. Thus 779 // The entry wasn't managed and doesn't match any current policy. Thus
744 // leave it as it is. 780 // leave it as it is.
745 } 781 }
746 } 782 }
747 783
748 void DeleteEntry(const std::string& entry) { 784 void DeleteEntry(const std::string& entry) {
749 DBusThreadManager::Get()->GetShillProfileClient()->DeleteEntry( 785 DBusThreadManager::Get()->GetShillProfileClient()->DeleteEntry(
750 dbus::ObjectPath(profile_path_), 786 dbus::ObjectPath(profile_.path),
751 entry, 787 entry,
752 base::Bind(&base::DoNothing), 788 base::Bind(&base::DoNothing),
753 base::Bind(&LogErrorMessage, FROM_HERE)); 789 base::Bind(&LogErrorMessage, FROM_HERE));
754 } 790 }
755 791
756 virtual ~PolicyApplicator() { 792 virtual ~PolicyApplicator() {
793 if (!handler_) {
794 LOG(WARNING) << "Handler destructed during policy application to profile "
795 << profile_.ToDebugString();
796 return;
797 }
798
757 if (remaining_policies_.empty()) 799 if (remaining_policies_.empty())
758 return; 800 return;
759 801
760 VLOG(2) << "Create new managed network configurations in profile" 802 VLOG(2) << "Create new managed network configurations in profile"
761 << profile_path_ << "."; 803 << profile_.ToDebugString() << ".";
762 // All profile entries were compared to policies. |configureGUIDs_| contains 804 // All profile entries were compared to policies. |configureGUIDs_| contains
763 // all matched policies. From the remainder of policies, new configurations 805 // all matched policies. From the remainder of policies, new configurations
764 // have to be created. 806 // have to be created.
765 807
766 // The relevant policy must have been initialized, otherwise we hadn't Run 808 // The relevant policy must have been initialized, otherwise we hadn't Run
767 // this PolicyApplicator. 809 // this PolicyApplicator.
768 const PolicyMap& policies_by_guid = 810 const GuidToPolicy& policies = *handler_->GetPoliciesForProfile(profile_);
769 *handler_->GetPoliciesForProfile(profile_path_);
770 811
771 for (std::set<std::string>::iterator it = remaining_policies_.begin(); 812 for (std::set<std::string>::iterator it = remaining_policies_.begin();
772 it != remaining_policies_.end(); ++it) { 813 it != remaining_policies_.end(); ++it) {
773 PolicyMap::const_iterator policy_it = policies_by_guid.find(*it); 814 const base::DictionaryValue* policy = GetByGUID(policies, *it);
774 if (policy_it == policies_by_guid.end()) { 815 if (!policy) {
775 LOG(ERROR) << "Policy " << *it << " doesn't exist anymore."; 816 LOG(ERROR) << "Policy " << *it << " doesn't exist anymore.";
776 continue; 817 continue;
777 } 818 }
778 819
779 const base::DictionaryValue* policy = policy_it->second;
780
781 VLOG(1) << "Creating new configuration managed by policy " << *it 820 VLOG(1) << "Creating new configuration managed by policy " << *it
782 << " in profile " << profile_path_ << "."; 821 << " in profile " << profile_.ToDebugString() << ".";
783 822
784 scoped_ptr<base::DictionaryValue> shill_dictionary = 823 scoped_ptr<base::DictionaryValue> shill_dictionary =
785 CreateShillConfiguration(profile_path_, *it, policy, NULL); 824 CreateShillConfiguration(profile_, *it, policy,
825 NULL /* no user settings */);
786 NetworkConfigurationHandler::Get()->CreateConfiguration( 826 NetworkConfigurationHandler::Get()->CreateConfiguration(
787 *shill_dictionary, 827 *shill_dictionary,
788 base::Bind(&IgnoreString), 828 base::Bind(&IgnoreString),
789 base::Bind(&LogErrorWithDict, FROM_HERE)); 829 base::Bind(&LogErrorWithDict, FROM_HERE));
790 } 830 }
791 } 831 }
792 832
793 std::set<std::string> remaining_policies_; 833 std::set<std::string> remaining_policies_;
794 base::WeakPtr<ManagedNetworkConfigurationHandler> handler_; 834 base::WeakPtr<ManagedNetworkConfigurationHandler> handler_;
795 std::string profile_path_; 835 NetworkProfile profile_;
796 836
797 DISALLOW_COPY_AND_ASSIGN(PolicyApplicator); 837 DISALLOW_COPY_AND_ASSIGN(PolicyApplicator);
798 }; 838 };
799 839
800 void ManagedNetworkConfigurationHandler::SetPolicy( 840 void ManagedNetworkConfigurationHandler::SetPolicy(
801 onc::ONCSource onc_source, 841 onc::ONCSource onc_source,
842 const std::string& userhash,
802 const base::ListValue& network_configs_onc) { 843 const base::ListValue& network_configs_onc) {
803 VLOG(1) << "Setting policies for ONC source " 844 VLOG(1) << "Setting policies from " << ToDebugString(onc_source, userhash)
804 << onc::GetSourceAsString(onc_source) << "."; 845 << ".";
805 846
806 PolicyMap* policies; 847 // |userhash| must be empty for device policies.
807 std::string profile; 848 DCHECK(onc_source != chromeos::onc::ONC_SOURCE_DEVICE_POLICY ||
808 if (onc_source == chromeos::onc::ONC_SOURCE_USER_POLICY) { 849 userhash.empty());
809 policies = &user_policies_by_guid_; 850 GuidToPolicy& policies = policies_by_user_[userhash];
810 profile = kUserProfilePath;
811 user_policies_initialized_ = true;
812 } else {
813 policies = &device_policies_by_guid_;
814 profile = kSharedProfilePath;
815 device_policies_initialized_ = true;
816 }
817 851
818 PolicyMap old_policies; 852 GuidToPolicy old_policies;
819 policies->swap(old_policies); 853 policies.swap(old_policies);
820 854
821 // This stores all GUIDs of policies that have changed or are new. 855 // This stores all GUIDs of policies that have changed or are new.
822 std::set<std::string> modified_policies; 856 std::set<std::string> modified_policies;
823 857
824 for (base::ListValue::const_iterator it = network_configs_onc.begin(); 858 for (base::ListValue::const_iterator it = network_configs_onc.begin();
825 it != network_configs_onc.end(); ++it) { 859 it != network_configs_onc.end(); ++it) {
826 const base::DictionaryValue* network = NULL; 860 const base::DictionaryValue* network = NULL;
827 (*it)->GetAsDictionary(&network); 861 (*it)->GetAsDictionary(&network);
828 DCHECK(network); 862 DCHECK(network);
829 863
830 std::string guid; 864 std::string guid;
831 network->GetStringWithoutPathExpansion(onc::network_config::kGUID, &guid); 865 network->GetStringWithoutPathExpansion(onc::network_config::kGUID, &guid);
832 DCHECK(!guid.empty()); 866 DCHECK(!guid.empty());
833 867
834 if (policies->count(guid) > 0) { 868 if (policies.count(guid) > 0) {
835 LOG(ERROR) << "ONC from " << onc::GetSourceAsString(onc_source) 869 LOG(ERROR) << "ONC from " << ToDebugString(onc_source, userhash)
836 << " contains several entries for the same GUID " 870 << " contains several entries for the same GUID "
837 << guid << "."; 871 << guid << ".";
838 delete (*policies)[guid]; 872 delete policies[guid];
839 } 873 }
840 const base::DictionaryValue* new_entry = network->DeepCopy(); 874 const base::DictionaryValue* new_entry = network->DeepCopy();
841 (*policies)[guid] = new_entry; 875 policies[guid] = new_entry;
842 876
843 const base::DictionaryValue* old_entry = old_policies[guid]; 877 const base::DictionaryValue* old_entry = old_policies[guid];
844 if (!old_entry || !old_entry->Equals(new_entry)) 878 if (!old_entry || !old_entry->Equals(new_entry))
845 modified_policies.insert(guid); 879 modified_policies.insert(guid);
846 } 880 }
847 881
848 STLDeleteValues(&old_policies); 882 STLDeleteValues(&old_policies);
849 883
884 const NetworkProfile* profile =
885 profile_handler_->GetProfileForUserhash(userhash);
886 if (!profile) {
887 VLOG(1) << "The relevant Shill profile isn't initialized yet, postponing "
888 << "policy application.";
889 return;
890 }
891
892 scoped_refptr<PolicyApplicator> applicator = new PolicyApplicator(
893 weak_ptr_factory_.GetWeakPtr(),
894 *profile,
895 &modified_policies);
896 applicator->Run();
897 }
898
899 void ManagedNetworkConfigurationHandler::OnProfileAdded(
900 const NetworkProfile& profile) {
901 VLOG(1) << "Adding profile " << profile.ToDebugString() << "'.";
902
903 const GuidToPolicy* policies = GetPoliciesForProfile(profile);
904 if (!policies) {
905 VLOG(1) << "The relevant policy is not initialized, "
906 << "postponing policy application.";
907 return;
908 }
909
910 std::set<std::string> policy_guids;
911 for (GuidToPolicy::const_iterator it = policies->begin();
912 it != policies->end(); ++it) {
913 policy_guids.insert(it->first);
914 }
915
850 scoped_refptr<PolicyApplicator> applicator = new PolicyApplicator( 916 scoped_refptr<PolicyApplicator> applicator = new PolicyApplicator(
851 weak_ptr_factory_.GetWeakPtr(), 917 weak_ptr_factory_.GetWeakPtr(),
852 profile, 918 profile,
853 &modified_policies); 919 &policy_guids);
854 applicator->Run(); 920 applicator->Run();
855 } 921 }
856 922
857 const ManagedNetworkConfigurationHandler::PolicyMap* 923 void ManagedNetworkConfigurationHandler::OnProfileRemoved(
858 ManagedNetworkConfigurationHandler::GetPoliciesForProfile( 924 const NetworkProfile& profile) {
859 const std::string& profile) const { 925 // Nothing to do in this case.
860 if (profile == kSharedProfilePath) {
861 if (device_policies_initialized_)
862 return &device_policies_by_guid_;
863 } else if (user_policies_initialized_) {
864 return &user_policies_by_guid_;
865 }
866 return NULL;
867 } 926 }
868 927
869 ManagedNetworkConfigurationHandler::ManagedNetworkConfigurationHandler() 928 const ManagedNetworkConfigurationHandler::GuidToPolicy*
870 : user_policies_initialized_(false), 929 ManagedNetworkConfigurationHandler::GetPoliciesForUser(
871 device_policies_initialized_(false), 930 const std::string& userhash) const {
931 UserToPolicies::const_iterator it = policies_by_user_.find(userhash);
932 if (it == policies_by_user_.end())
933 return NULL;
934 return &it->second;
935 }
936
937 const ManagedNetworkConfigurationHandler::GuidToPolicy*
938 ManagedNetworkConfigurationHandler::GetPoliciesForProfile(
939 const NetworkProfile& profile) const {
940 DCHECK(profile.type() != NetworkProfile::TYPE_SHARED ||
941 profile.userhash.empty());
942 return GetPoliciesForUser(profile.userhash);
943 }
944
945 ManagedNetworkConfigurationHandler::ManagedNetworkConfigurationHandler(
946 NetworkProfileHandler* profile_handler)
947 : profile_handler_(profile_handler),
872 weak_ptr_factory_(this) { 948 weak_ptr_factory_(this) {
949 profile_handler_->AddObserver(this);
873 } 950 }
874 951
875 ManagedNetworkConfigurationHandler::~ManagedNetworkConfigurationHandler() { 952 ManagedNetworkConfigurationHandler::~ManagedNetworkConfigurationHandler() {
876 STLDeleteValues(&user_policies_by_guid_); 953 profile_handler_->RemoveObserver(this);
877 STLDeleteValues(&device_policies_by_guid_); 954 for (UserToPolicies::iterator it = policies_by_user_.begin();
955 it != policies_by_user_.end(); ++it) {
956 STLDeleteValues(&it->second);
957 }
878 } 958 }
879 959
880 } // namespace chromeos 960 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698