blob: f7afc7761599eee4c2634103034b6c1d84f7ecb6 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2011 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
adlr@google.com3defe6a2009-12-04 20:57:17 +000016
17#include "update_engine/omaha_response_handler_action.h"
Darin Petkov73058b42010-10-06 16:32:19 -070018
adlr@google.com3defe6a2009-12-04 20:57:17 +000019#include <string>
Darin Petkov73058b42010-10-06 16:32:19 -070020
21#include <base/logging.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070022#include <base/strings/string_util.h>
Gilad Arnold1f847232014-04-07 12:07:49 -070023#include <policy/device_policy.h>
Darin Petkov73058b42010-10-06 16:32:19 -070024
Alex Deymof6ee0162015-07-31 12:35:22 -070025#include "update_engine/connection_manager_interface.h"
Jay Srinivasand29695d2013-04-08 15:08:05 -070026#include "update_engine/constants.h"
Darin Petkov0406e402010-10-06 21:33:11 -070027#include "update_engine/delta_performer.h"
Alex Deymo42432912013-07-12 20:21:15 -070028#include "update_engine/hardware_interface.h"
Gilad Arnold1f847232014-04-07 12:07:49 -070029#include "update_engine/omaha_request_params.h"
Jay Srinivasan55f50c22013-01-10 19:24:35 -080030#include "update_engine/payload_state_interface.h"
Darin Petkov73058b42010-10-06 16:32:19 -070031#include "update_engine/prefs_interface.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000032#include "update_engine/utils.h"
33
34using std::string;
35
36namespace chromeos_update_engine {
37
Darin Petkov6c118642010-10-21 12:06:30 -070038const char OmahaResponseHandlerAction::kDeadlineFile[] =
39 "/tmp/update-check-response-deadline";
40
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080041OmahaResponseHandlerAction::OmahaResponseHandlerAction(
42 SystemState* system_state)
43 : system_state_(system_state),
Darin Petkovabc7bc02011-02-23 14:39:43 -080044 got_no_update_response_(false),
Gilad Arnold4dbd47e2013-07-22 05:39:26 -070045 key_path_(DeltaPerformer::kUpdatePayloadPublicKeyPath),
46 deadline_file_(kDeadlineFile) {}
47
48OmahaResponseHandlerAction::OmahaResponseHandlerAction(
49 SystemState* system_state, const string& deadline_file)
50 : system_state_(system_state),
51 got_no_update_response_(false),
52 key_path_(DeltaPerformer::kUpdatePayloadPublicKeyPath),
53 deadline_file_(deadline_file) {}
Darin Petkovabc7bc02011-02-23 14:39:43 -080054
adlr@google.com3defe6a2009-12-04 20:57:17 +000055void OmahaResponseHandlerAction::PerformAction() {
56 CHECK(HasInputObject());
57 ScopedActionCompleter completer(processor_, this);
Darin Petkov6a5b3222010-07-13 14:55:28 -070058 const OmahaResponse& response = GetInputObject();
adlr@google.com3defe6a2009-12-04 20:57:17 +000059 if (!response.update_exists) {
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080060 got_no_update_response_ = true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000061 LOG(INFO) << "There are no updates. Aborting.";
62 return;
63 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080064
65 // All decisions as to which URL should be used have already been done. So,
Jay Srinivasan53173b92013-05-17 17:13:01 -070066 // make the current URL as the download URL.
67 string current_url = system_state_->payload_state()->GetCurrentUrl();
68 if (current_url.empty()) {
69 // This shouldn't happen as we should always supply the HTTPS backup URL.
70 // Handling this anyway, just in case.
71 LOG(ERROR) << "There are no suitable URLs in the response to use.";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070072 completer.set_code(ErrorCode::kOmahaResponseInvalid);
Jay Srinivasan53173b92013-05-17 17:13:01 -070073 return;
74 }
75
76 install_plan_.download_url = current_url;
Chris Sosafb1020e2013-07-29 17:27:33 -070077 install_plan_.version = response.version;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080078
Gilad Arnold74b5f552014-10-07 08:17:16 -070079 OmahaRequestParams* const params = system_state_->request_params();
80 PayloadStateInterface* const payload_state = system_state_->payload_state();
David Zeuthen8f191b22013-08-06 12:27:50 -070081
82 // If we're using p2p to download and there is a local peer, use it.
Gilad Arnold74b5f552014-10-07 08:17:16 -070083 if (payload_state->GetUsingP2PForDownloading() &&
84 !payload_state->GetP2PUrl().empty()) {
David Zeuthen8f191b22013-08-06 12:27:50 -070085 LOG(INFO) << "Replacing URL " << install_plan_.download_url
Gilad Arnold74b5f552014-10-07 08:17:16 -070086 << " with local URL " << payload_state->GetP2PUrl()
David Zeuthen8f191b22013-08-06 12:27:50 -070087 << " since p2p is enabled.";
Gilad Arnold74b5f552014-10-07 08:17:16 -070088 install_plan_.download_url = payload_state->GetP2PUrl();
89 payload_state->SetUsingP2PForDownloading(true);
David Zeuthen8f191b22013-08-06 12:27:50 -070090 }
91
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080092 // Fill up the other properties based on the response.
Jay Srinivasan51dcf262012-09-13 17:24:32 -070093 install_plan_.payload_size = response.size;
94 install_plan_.payload_hash = response.hash;
Jay Srinivasanf4318702012-09-24 11:56:24 -070095 install_plan_.metadata_size = response.metadata_size;
96 install_plan_.metadata_signature = response.metadata_signature;
David Zeuthene7f89172013-10-31 10:21:04 -070097 install_plan_.public_key_rsa = response.public_key_rsa;
Jay Srinivasan738fdf32012-12-07 17:40:54 -080098 install_plan_.hash_checks_mandatory = AreHashChecksMandatory(response);
Darin Petkov0406e402010-10-06 21:33:11 -070099 install_plan_.is_resume =
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800100 DeltaPerformer::CanResumeUpdate(system_state_->prefs(), response.hash);
Chris Sosabe45bef2013-04-09 18:25:12 -0700101 if (install_plan_.is_resume) {
Gilad Arnold74b5f552014-10-07 08:17:16 -0700102 payload_state->UpdateResumed();
Chris Sosabe45bef2013-04-09 18:25:12 -0700103 } else {
Gilad Arnold74b5f552014-10-07 08:17:16 -0700104 payload_state->UpdateRestarted();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800105 LOG_IF(WARNING, !DeltaPerformer::ResetUpdateProgress(
106 system_state_->prefs(), false))
Darin Petkov0406e402010-10-06 21:33:11 -0700107 << "Unable to reset the update progress.";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800108 LOG_IF(WARNING, !system_state_->prefs()->SetString(
109 kPrefsUpdateCheckResponseHash, response.hash))
Darin Petkov0406e402010-10-06 21:33:11 -0700110 << "Unable to save the update check response hash.";
111 }
Gilad Arnold21504f02013-05-24 08:51:22 -0700112 install_plan_.is_full_update = !response.is_delta_payload;
Darin Petkov0406e402010-10-06 21:33:11 -0700113
Chris Sosad317e402013-06-12 13:47:09 -0700114 TEST_AND_RETURN(utils::GetInstallDev(
Alex Deymo42432912013-07-12 20:21:15 -0700115 (!boot_device_.empty() ? boot_device_ :
116 system_state_->hardware()->BootDevice()),
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700117 &install_plan_.install_path));
118 install_plan_.kernel_install_path =
J. Richard Barnette30842932013-10-28 15:04:23 -0700119 utils::KernelDeviceOfBootDevice(install_plan_.install_path);
Allie Wood9f6f0a52015-03-30 11:25:47 -0700120 install_plan_.source_path = system_state_->hardware()->BootDevice();
121 install_plan_.kernel_source_path =
122 utils::KernelDeviceOfBootDevice(install_plan_.source_path);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000123
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700124 if (params->to_more_stable_channel() && params->is_powerwash_allowed())
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700125 install_plan_.powerwash_required = true;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700126
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700127
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700128 TEST_AND_RETURN(HasOutputPipe());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000129 if (HasOutputPipe())
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700130 SetOutputObject(install_plan_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000131 LOG(INFO) << "Using this install plan:";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700132 install_plan_.Dump();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700133
Darin Petkov6c118642010-10-21 12:06:30 -0700134 // Send the deadline data (if any) to Chrome through a file. This is a pretty
135 // hacky solution but should be OK for now.
136 //
Alex Vakulenko072359c2014-07-18 11:41:07 -0700137 // TODO(petkov): Re-architect this to avoid communication through a
Chris Sosabe45bef2013-04-09 18:25:12 -0700138 // file. Ideally, we would include this information in D-Bus's GetStatus
Darin Petkov6c118642010-10-21 12:06:30 -0700139 // method and UpdateStatus signal. A potential issue is that update_engine may
140 // be unresponsive during an update download.
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700141 utils::WriteFile(deadline_file_.c_str(),
Darin Petkov6c118642010-10-21 12:06:30 -0700142 response.deadline.data(),
143 response.deadline.size());
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700144 chmod(deadline_file_.c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
Darin Petkov6c118642010-10-21 12:06:30 -0700145
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700146 completer.set_code(ErrorCode::kSuccess);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000147}
148
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800149bool OmahaResponseHandlerAction::AreHashChecksMandatory(
150 const OmahaResponse& response) {
David Pursell907b4fa2015-01-27 10:27:38 -0800151 // We sometimes need to waive the hash checks in order to download from
152 // sources that don't provide hashes, such as dev server.
153 // At this point UpdateAttempter::IsAnyUpdateSourceAllowed() has already been
154 // checked, so an unofficial update URL won't get this far unless it's OK to
155 // use without a hash. Additionally, we want to always waive hash checks on
156 // unofficial builds (i.e. dev/test images).
157 // The end result is this:
158 // * Base image:
159 // - Official URLs require a hash.
160 // - Unofficial URLs only get this far if the IsAnyUpdateSourceAllowed()
161 // devmode/debugd checks pass, in which case the hash is waived.
162 // * Dev/test image:
163 // - Any URL is allowed through with no hash checking.
164 if (!system_state_->request_params()->IsUpdateUrlOfficial() ||
165 !system_state_->hardware()->IsOfficialBuild()) {
David Pursell02c18642014-11-06 11:26:11 -0800166 // Still do a hash check if a public key is included.
David Zeuthene7f89172013-10-31 10:21:04 -0700167 if (!response.public_key_rsa.empty()) {
David Zeuthenbc27aac2013-11-26 11:17:48 -0800168 // The autoupdate_CatchBadSignatures test checks for this string
169 // in log-files. Keep in sync.
David Zeuthene7f89172013-10-31 10:21:04 -0700170 LOG(INFO) << "Mandating payload hash checks since Omaha Response "
171 << "for unofficial build includes public RSA key.";
172 return true;
173 } else {
David Pursell02c18642014-11-06 11:26:11 -0800174 LOG(INFO) << "Waiving payload hash checks for unofficial update URL.";
David Zeuthene7f89172013-10-31 10:21:04 -0700175 return false;
176 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800177 }
178
David Zeuthen8f191b22013-08-06 12:27:50 -0700179 // If we're using p2p, |install_plan_.download_url| may contain a
180 // HTTP URL even if |response.payload_urls| contain only HTTPS URLs.
Alex Vakulenko6a9d3492015-06-15 12:53:22 -0700181 if (!base::StartsWithASCII(install_plan_.download_url, "https://", false)) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700182 LOG(INFO) << "Mandating hash checks since download_url is not HTTPS.";
183 return true;
184 }
185
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800186 // TODO(jaysri): VALIDATION: For official builds, we currently waive hash
187 // checks for HTTPS until we have rolled out at least once and are confident
188 // nothing breaks. chromium-os:37082 tracks turning this on for HTTPS
189 // eventually.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800190
191 // Even if there's a single non-HTTPS URL, make the hash checks as
192 // mandatory because we could be downloading the payload from any URL later
193 // on. It's really hard to do book-keeping based on each byte being
194 // downloaded to see whether we only used HTTPS throughout.
195 for (size_t i = 0; i < response.payload_urls.size(); i++) {
Alex Vakulenko6a9d3492015-06-15 12:53:22 -0700196 if (!base::StartsWithASCII(response.payload_urls[i], "https://", false)) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800197 LOG(INFO) << "Mandating payload hash checks since Omaha response "
198 << "contains non-HTTPS URL(s)";
199 return true;
200 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800201 }
202
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800203 LOG(INFO) << "Waiving payload hash checks since Omaha response "
204 << "only has HTTPS URL(s)";
205 return false;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800206}
207
adlr@google.com3defe6a2009-12-04 20:57:17 +0000208} // namespace chromeos_update_engine