| OLD | NEW |
| (Empty) |
| 1 // Copyright 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 "sync/engine/apply_control_data_updates.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/metrics/histogram.h" | |
| 12 #include "sync/engine/conflict_resolver.h" | |
| 13 #include "sync/engine/conflict_util.h" | |
| 14 #include "sync/engine/syncer_util.h" | |
| 15 #include "sync/syncable/directory.h" | |
| 16 #include "sync/syncable/mutable_entry.h" | |
| 17 #include "sync/syncable/nigori_handler.h" | |
| 18 #include "sync/syncable/nigori_util.h" | |
| 19 #include "sync/syncable/syncable_write_transaction.h" | |
| 20 #include "sync/util/cryptographer.h" | |
| 21 | |
| 22 namespace syncer { | |
| 23 | |
| 24 void ApplyControlDataUpdates(syncable::Directory* dir) { | |
| 25 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, dir); | |
| 26 | |
| 27 std::vector<int64_t> handles; | |
| 28 dir->GetUnappliedUpdateMetaHandles( | |
| 29 &trans, ToFullModelTypeSet(ControlTypes()), &handles); | |
| 30 | |
| 31 // First, go through and manually apply any new top level datatype nodes (so | |
| 32 // that we don't have to worry about hitting a CONFLICT_HIERARCHY with an | |
| 33 // entry because we haven't applied its parent yet). | |
| 34 // TODO(sync): if at some point we support control datatypes with actual | |
| 35 // hierarchies we'll need to revisit this logic. | |
| 36 ModelTypeSet control_types = ControlTypes(); | |
| 37 for (ModelTypeSet::Iterator iter = control_types.First(); iter.Good(); | |
| 38 iter.Inc()) { | |
| 39 ModelType type = iter.Get(); | |
| 40 syncable::MutableEntry entry(&trans, syncable::GET_TYPE_ROOT, type); | |
| 41 if (!entry.good()) | |
| 42 continue; | |
| 43 | |
| 44 if (!entry.GetIsUnappliedUpdate()) { | |
| 45 // If this is a type with client generated root, the root node has been | |
| 46 // created locally and might never be updated by the server. In that case | |
| 47 // it has to be marked as having the initial download completed (which is | |
| 48 // done by changing the root's base version to a value other than | |
| 49 // CHANGES_VERSION). This does nothing if the root's base version is | |
| 50 // already other than CHANGES_VERSION. | |
| 51 if (IsTypeWithClientGeneratedRoot(type)) { | |
| 52 dir->MarkInitialSyncEndedForType(&trans, type); | |
| 53 } | |
| 54 continue; | |
| 55 } | |
| 56 | |
| 57 DCHECK_EQ(type, entry.GetServerModelType()); | |
| 58 if (type == NIGORI) { | |
| 59 // Nigori node applications never fail. | |
| 60 ApplyNigoriUpdate(&trans, | |
| 61 &entry, | |
| 62 dir->GetCryptographer(&trans)); | |
| 63 } else { | |
| 64 ApplyControlUpdate(&trans, | |
| 65 &entry, | |
| 66 dir->GetCryptographer(&trans)); | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 // Go through the rest of the unapplied control updates, skipping over any | |
| 71 // top level folders. | |
| 72 for (std::vector<int64_t>::const_iterator iter = handles.begin(); | |
| 73 iter != handles.end(); ++iter) { | |
| 74 syncable::MutableEntry entry(&trans, syncable::GET_BY_HANDLE, *iter); | |
| 75 CHECK(entry.good()); | |
| 76 ModelType type = entry.GetServerModelType(); | |
| 77 CHECK(ControlTypes().Has(type)); | |
| 78 if (!entry.GetUniqueServerTag().empty()) { | |
| 79 // We should have already applied all top level control nodes. | |
| 80 DCHECK(!entry.GetIsUnappliedUpdate()); | |
| 81 continue; | |
| 82 } | |
| 83 | |
| 84 ApplyControlUpdate(&trans, | |
| 85 &entry, | |
| 86 dir->GetCryptographer(&trans)); | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 // Update the nigori handler with the server's nigori node. | |
| 91 // | |
| 92 // If we have a locally modified nigori node, we merge them manually. This | |
| 93 // handles the case where two clients both set a different passphrase. The | |
| 94 // second client to attempt to commit will go into a state of having pending | |
| 95 // keys, unioned the set of encrypted types, and eventually re-encrypt | |
| 96 // everything with the passphrase of the first client and commit the set of | |
| 97 // merged encryption keys. Until the second client provides the pending | |
| 98 // passphrase, the cryptographer will preserve the encryption keys based on the | |
| 99 // local passphrase, while the nigori node will preserve the server encryption | |
| 100 // keys. | |
| 101 void ApplyNigoriUpdate(syncable::WriteTransaction* const trans, | |
| 102 syncable::MutableEntry* const entry, | |
| 103 Cryptographer* cryptographer) { | |
| 104 DCHECK(entry->GetIsUnappliedUpdate()); | |
| 105 | |
| 106 // We apply the nigori update regardless of whether there's a conflict or | |
| 107 // not in order to preserve any new encrypted types or encryption keys. | |
| 108 // TODO(zea): consider having this return a bool reflecting whether it was a | |
| 109 // valid update or not, and in the case of invalid updates not overwrite the | |
| 110 // local data. | |
| 111 const sync_pb::NigoriSpecifics& nigori = | |
| 112 entry->GetServerSpecifics().nigori(); | |
| 113 trans->directory()->GetNigoriHandler()->ApplyNigoriUpdate(nigori, trans); | |
| 114 | |
| 115 // Make sure any unsynced changes are properly encrypted as necessary. | |
| 116 // We only perform this if the cryptographer is ready. If not, these are | |
| 117 // re-encrypted at SetDecryptionPassphrase time (via ReEncryptEverything). | |
| 118 // This logic covers the case where the nigori update marked new datatypes | |
| 119 // for encryption, but didn't change the passphrase. | |
| 120 if (cryptographer->is_ready()) { | |
| 121 // Note that we don't bother to encrypt any data for which IS_UNSYNCED | |
| 122 // == false here. The machine that turned on encryption should know about | |
| 123 // and re-encrypt all synced data. It's possible it could get interrupted | |
| 124 // during this process, but we currently reencrypt everything at startup | |
| 125 // as well, so as soon as a client is restarted with this datatype marked | |
| 126 // for encryption, all the data should be updated as necessary. | |
| 127 | |
| 128 // If this fails, something is wrong with the cryptographer, but there's | |
| 129 // nothing we can do about it here. | |
| 130 DVLOG(1) << "Received new nigori, encrypting unsynced changes."; | |
| 131 syncable::ProcessUnsyncedChangesForEncryption(trans); | |
| 132 } | |
| 133 | |
| 134 if (!entry->GetIsUnsynced()) { // Update only. | |
| 135 UpdateLocalDataFromServerData(trans, entry); | |
| 136 } else { // Conflict. | |
| 137 const sync_pb::EntitySpecifics& server_specifics = | |
| 138 entry->GetServerSpecifics(); | |
| 139 const sync_pb::NigoriSpecifics& server_nigori = server_specifics.nigori(); | |
| 140 const sync_pb::EntitySpecifics& local_specifics = | |
| 141 entry->GetSpecifics(); | |
| 142 const sync_pb::NigoriSpecifics& local_nigori = local_specifics.nigori(); | |
| 143 | |
| 144 // We initialize the new nigori with the server state, and will override | |
| 145 // it as necessary below. | |
| 146 sync_pb::EntitySpecifics new_specifics = entry->GetServerSpecifics(); | |
| 147 sync_pb::NigoriSpecifics* new_nigori = new_specifics.mutable_nigori(); | |
| 148 | |
| 149 // If the cryptographer is not ready, another client set a new encryption | |
| 150 // passphrase. If we had migrated locally, we will re-migrate when the | |
| 151 // pending keys are provided. If we had set a new custom passphrase locally | |
| 152 // the user will have another chance to set a custom passphrase later | |
| 153 // (assuming they hadn't set a custom passphrase on the other client). | |
| 154 // Therefore, we only attempt to merge the nigori nodes if the cryptographer | |
| 155 // is ready. | |
| 156 // Note: we only update the encryption keybag if we're sure that we aren't | |
| 157 // invalidating the keystore_decryptor_token (i.e. we're either | |
| 158 // not migrated or we copying over all local state). | |
| 159 if (cryptographer->is_ready()) { | |
| 160 if (local_nigori.has_passphrase_type() && | |
| 161 server_nigori.has_passphrase_type()) { | |
| 162 // They're both migrated, preserve the local nigori if the passphrase | |
| 163 // type is more conservative. | |
| 164 if (server_nigori.passphrase_type() == | |
| 165 sync_pb::NigoriSpecifics::KEYSTORE_PASSPHRASE && | |
| 166 local_nigori.passphrase_type() != | |
| 167 sync_pb::NigoriSpecifics::KEYSTORE_PASSPHRASE) { | |
| 168 DCHECK(local_nigori.passphrase_type() == | |
| 169 sync_pb::NigoriSpecifics::FROZEN_IMPLICIT_PASSPHRASE || | |
| 170 local_nigori.passphrase_type() == | |
| 171 sync_pb::NigoriSpecifics::CUSTOM_PASSPHRASE); | |
| 172 new_nigori->CopyFrom(local_nigori); | |
| 173 cryptographer->GetKeys(new_nigori->mutable_encryption_keybag()); | |
| 174 } | |
| 175 } else if (!local_nigori.has_passphrase_type() && | |
| 176 !server_nigori.has_passphrase_type()) { | |
| 177 // Set the explicit passphrase based on the local state. If the server | |
| 178 // had set an explict passphrase, we should have pending keys, so | |
| 179 // should not reach this code. | |
| 180 // Because neither side is migrated, we don't have to worry about the | |
| 181 // keystore decryptor token. | |
| 182 new_nigori->set_keybag_is_frozen(local_nigori.keybag_is_frozen()); | |
| 183 cryptographer->GetKeys(new_nigori->mutable_encryption_keybag()); | |
| 184 } else if (local_nigori.has_passphrase_type()) { | |
| 185 // Local is migrated but server is not. Copy over the local migrated | |
| 186 // data. | |
| 187 new_nigori->CopyFrom(local_nigori); | |
| 188 cryptographer->GetKeys(new_nigori->mutable_encryption_keybag()); | |
| 189 } // else leave the new nigori with the server state. | |
| 190 } | |
| 191 | |
| 192 // Always update to the safest set of encrypted types. | |
| 193 trans->directory()->GetNigoriHandler()->UpdateNigoriFromEncryptedTypes( | |
| 194 new_nigori, | |
| 195 trans); | |
| 196 | |
| 197 entry->PutSpecifics(new_specifics); | |
| 198 DVLOG(1) << "Resolving simple conflict, merging nigori nodes: " | |
| 199 << entry; | |
| 200 | |
| 201 conflict_util::OverwriteServerChanges(entry); | |
| 202 | |
| 203 UMA_HISTOGRAM_ENUMERATION("Sync.ResolveSimpleConflict", | |
| 204 ConflictResolver::NIGORI_MERGE, | |
| 205 ConflictResolver::CONFLICT_RESOLUTION_SIZE); | |
| 206 } | |
| 207 } | |
| 208 | |
| 209 void ApplyControlUpdate(syncable::WriteTransaction* const trans, | |
| 210 syncable::MutableEntry* const entry, | |
| 211 Cryptographer* cryptographer) { | |
| 212 DCHECK_NE(entry->GetServerModelType(), NIGORI); | |
| 213 DCHECK(entry->GetIsUnappliedUpdate()); | |
| 214 if (entry->GetIsUnsynced()) { | |
| 215 // We just let the server win all conflicts with control types. | |
| 216 DVLOG(1) << "Ignoring local changes for control update."; | |
| 217 conflict_util::IgnoreLocalChanges(entry); | |
| 218 UMA_HISTOGRAM_ENUMERATION("Sync.ResolveSimpleConflict", | |
| 219 ConflictResolver::OVERWRITE_LOCAL, | |
| 220 ConflictResolver::CONFLICT_RESOLUTION_SIZE); | |
| 221 } | |
| 222 | |
| 223 UpdateAttemptResponse response = AttemptToUpdateEntry( | |
| 224 trans, entry, cryptographer); | |
| 225 DCHECK_EQ(SUCCESS, response); | |
| 226 } | |
| 227 | |
| 228 } // namespace syncer | |
| OLD | NEW |