| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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/chromeos/login/signed_settings_cache.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/base64.h" | |
| 10 #include "base/bind.h" | |
| 11 #include "chrome/browser/chromeos/cros_settings.h" | |
| 12 #include "chrome/browser/chromeos/login/ownership_service.h" | |
| 13 #include "chrome/browser/chromeos/login/signed_settings_helper.h" | |
| 14 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
| 15 #include "chrome/browser/prefs/pref_service.h" | |
| 16 #include "chrome/common/pref_names.h" | |
| 17 | |
| 18 using content::BrowserThread; | |
| 19 | |
| 20 namespace em = enterprise_management; | |
| 21 | |
| 22 namespace chromeos { | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 void OnStorePolicyCompleted(SignedSettings::ReturnCode code) { | |
| 27 if (code != SignedSettings::SUCCESS) | |
| 28 LOG(ERROR) << "Couldn't save temp store to the policy blob. code: " << code; | |
| 29 else | |
| 30 CrosSettings::Get()->ReloadProviders(); | |
| 31 } | |
| 32 | |
| 33 void FinishFinalize(PrefService* local_state, | |
| 34 SignedSettings::ReturnCode code, | |
| 35 const em::PolicyFetchResponse& policy) { | |
| 36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 37 | |
| 38 if (code != SignedSettings::SUCCESS) { | |
| 39 LOG(ERROR) << "Can't finalize temp store error code:" << code; | |
| 40 return; | |
| 41 } | |
| 42 | |
| 43 if (local_state) { | |
| 44 std::string encoded = | |
| 45 local_state->GetString(prefs::kSignedSettingsCache); | |
| 46 std::string policy_string; | |
| 47 if (!base::Base64Decode(encoded, &policy_string)) { | |
| 48 LOG(ERROR) << "Can't decode policy from base64 on finalizing."; | |
| 49 return; | |
| 50 } | |
| 51 | |
| 52 em::PolicyData merging_policy_data; | |
| 53 if (!merging_policy_data.ParseFromString(policy_string)) { | |
| 54 LOG(ERROR) << "Can't decode policy from string on finalizing."; | |
| 55 return; | |
| 56 } | |
| 57 | |
| 58 em::PolicyFetchResponse policy_envelope = policy; | |
| 59 DCHECK(policy_envelope.has_policy_data()); | |
| 60 em::PolicyData base_policy_data; | |
| 61 base_policy_data.ParseFromString(policy_envelope.policy_data()); | |
| 62 // Merge only the policy value as we should never ever rewrite the other | |
| 63 // fields of the PolicyData protobuf. | |
| 64 base_policy_data.set_policy_value(merging_policy_data.policy_value()); | |
| 65 policy_envelope.set_policy_data(base_policy_data.SerializeAsString()); | |
| 66 DCHECK(base_policy_data.has_username()); | |
| 67 policy_envelope.clear_policy_data_signature(); | |
| 68 SignedSettingsHelper::Get()->StartStorePolicyOp( | |
| 69 policy_envelope, base::Bind(&OnStorePolicyCompleted)); | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 // Reload the initial policy blob, and if successful apply the settings from | |
| 74 // temp storage, and write them back the blob in FinishFinalize. | |
| 75 void ReloadSignedSettingsAndFinalize( | |
| 76 PrefService* local_state, | |
| 77 OwnershipService::Status status, | |
| 78 bool current_user_is_owner) { | |
| 79 if (current_user_is_owner) { | |
| 80 SignedSettingsHelper::Get()->StartRetrievePolicyOp( | |
| 81 base::Bind(FinishFinalize, local_state)); | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 } // namespace | |
| 86 | |
| 87 namespace signed_settings_cache { | |
| 88 | |
| 89 void RegisterPrefs(PrefService* local_state) { | |
| 90 local_state->RegisterStringPref(prefs::kSignedSettingsCache, | |
| 91 "invalid", | |
| 92 PrefService::UNSYNCABLE_PREF); | |
| 93 } | |
| 94 | |
| 95 bool Store(const em::PolicyData& policy, PrefService* local_state) { | |
| 96 if (local_state) { | |
| 97 std::string policy_string = policy.SerializeAsString(); | |
| 98 std::string encoded; | |
| 99 if (!base::Base64Encode(policy_string, &encoded)) { | |
| 100 LOG(ERROR) << "Can't encode policy in base64."; | |
| 101 return false; | |
| 102 } | |
| 103 local_state->SetString(prefs::kSignedSettingsCache, encoded); | |
| 104 return true; | |
| 105 } | |
| 106 return false; | |
| 107 } | |
| 108 | |
| 109 bool Retrieve(em::PolicyData *policy, PrefService* local_state) { | |
| 110 if (local_state) { | |
| 111 std::string encoded = | |
| 112 local_state->GetString(prefs::kSignedSettingsCache); | |
| 113 std::string policy_string; | |
| 114 if (!base::Base64Decode(encoded, &policy_string)) { | |
| 115 // This is normal and happens on first boot. | |
| 116 VLOG(1) << "Can't decode policy from base64."; | |
| 117 return false; | |
| 118 } | |
| 119 return policy->ParseFromString(policy_string); | |
| 120 } | |
| 121 return false; | |
| 122 } | |
| 123 | |
| 124 void Finalize(PrefService* local_state) { | |
| 125 // First we have to make sure the owner is really logged in because the key | |
| 126 // notification is generated on every cloud policy key rotation too. | |
| 127 OwnershipService::GetSharedInstance()->GetStatusAsync( | |
| 128 base::Bind(&ReloadSignedSettingsAndFinalize, local_state)); | |
| 129 } | |
| 130 | |
| 131 } // namespace signed_settings_cache | |
| 132 | |
| 133 } // namespace chromeos | |
| OLD | NEW |