blob: 3e6f5d278c6c7d76b5c3ef1e2289d4c3867f5481 [file] [log] [blame]
Darin Petkov7ed561b2011-10-04 02:59:03 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
adlr@google.com3defe6a2009-12-04 20:57:17 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/omaha_response_handler_action.h"
Darin Petkov73058b42010-10-06 16:32:19 -07006
adlr@google.com3defe6a2009-12-04 20:57:17 +00007#include <string>
Darin Petkov73058b42010-10-06 16:32:19 -07008
9#include <base/logging.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070010#include <base/strings/string_util.h>
Gilad Arnold1f847232014-04-07 12:07:49 -070011#include <policy/device_policy.h>
Darin Petkov73058b42010-10-06 16:32:19 -070012
Chris Sosa77f79e82014-06-02 18:16:24 -070013#include "update_engine/connection_manager.h"
Jay Srinivasand29695d2013-04-08 15:08:05 -070014#include "update_engine/constants.h"
Darin Petkov0406e402010-10-06 21:33:11 -070015#include "update_engine/delta_performer.h"
Alex Deymo42432912013-07-12 20:21:15 -070016#include "update_engine/hardware_interface.h"
Gilad Arnold1f847232014-04-07 12:07:49 -070017#include "update_engine/omaha_request_params.h"
Jay Srinivasan55f50c22013-01-10 19:24:35 -080018#include "update_engine/payload_state_interface.h"
Darin Petkov73058b42010-10-06 16:32:19 -070019#include "update_engine/prefs_interface.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000020#include "update_engine/utils.h"
21
22using std::string;
23
24namespace chromeos_update_engine {
25
Darin Petkov6c118642010-10-21 12:06:30 -070026const char OmahaResponseHandlerAction::kDeadlineFile[] =
27 "/tmp/update-check-response-deadline";
28
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080029OmahaResponseHandlerAction::OmahaResponseHandlerAction(
30 SystemState* system_state)
31 : system_state_(system_state),
Darin Petkovabc7bc02011-02-23 14:39:43 -080032 got_no_update_response_(false),
Gilad Arnold4dbd47e2013-07-22 05:39:26 -070033 key_path_(DeltaPerformer::kUpdatePayloadPublicKeyPath),
34 deadline_file_(kDeadlineFile) {}
35
36OmahaResponseHandlerAction::OmahaResponseHandlerAction(
37 SystemState* system_state, const string& deadline_file)
38 : system_state_(system_state),
39 got_no_update_response_(false),
40 key_path_(DeltaPerformer::kUpdatePayloadPublicKeyPath),
41 deadline_file_(deadline_file) {}
Darin Petkovabc7bc02011-02-23 14:39:43 -080042
adlr@google.com3defe6a2009-12-04 20:57:17 +000043void OmahaResponseHandlerAction::PerformAction() {
44 CHECK(HasInputObject());
45 ScopedActionCompleter completer(processor_, this);
Darin Petkov6a5b3222010-07-13 14:55:28 -070046 const OmahaResponse& response = GetInputObject();
adlr@google.com3defe6a2009-12-04 20:57:17 +000047 if (!response.update_exists) {
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080048 got_no_update_response_ = true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000049 LOG(INFO) << "There are no updates. Aborting.";
50 return;
51 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080052
53 // All decisions as to which URL should be used have already been done. So,
Jay Srinivasan53173b92013-05-17 17:13:01 -070054 // make the current URL as the download URL.
55 string current_url = system_state_->payload_state()->GetCurrentUrl();
56 if (current_url.empty()) {
57 // This shouldn't happen as we should always supply the HTTPS backup URL.
58 // Handling this anyway, just in case.
59 LOG(ERROR) << "There are no suitable URLs in the response to use.";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070060 completer.set_code(ErrorCode::kOmahaResponseInvalid);
Jay Srinivasan53173b92013-05-17 17:13:01 -070061 return;
62 }
63
64 install_plan_.download_url = current_url;
Chris Sosafb1020e2013-07-29 17:27:33 -070065 install_plan_.version = response.version;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080066
Gilad Arnold74b5f552014-10-07 08:17:16 -070067 OmahaRequestParams* const params = system_state_->request_params();
68 PayloadStateInterface* const payload_state = system_state_->payload_state();
David Zeuthen8f191b22013-08-06 12:27:50 -070069
70 // If we're using p2p to download and there is a local peer, use it.
Gilad Arnold74b5f552014-10-07 08:17:16 -070071 if (payload_state->GetUsingP2PForDownloading() &&
72 !payload_state->GetP2PUrl().empty()) {
David Zeuthen8f191b22013-08-06 12:27:50 -070073 LOG(INFO) << "Replacing URL " << install_plan_.download_url
Gilad Arnold74b5f552014-10-07 08:17:16 -070074 << " with local URL " << payload_state->GetP2PUrl()
David Zeuthen8f191b22013-08-06 12:27:50 -070075 << " since p2p is enabled.";
Gilad Arnold74b5f552014-10-07 08:17:16 -070076 install_plan_.download_url = payload_state->GetP2PUrl();
77 payload_state->SetUsingP2PForDownloading(true);
David Zeuthen8f191b22013-08-06 12:27:50 -070078 }
79
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080080 // Fill up the other properties based on the response.
Jay Srinivasan51dcf262012-09-13 17:24:32 -070081 install_plan_.payload_size = response.size;
82 install_plan_.payload_hash = response.hash;
Jay Srinivasanf4318702012-09-24 11:56:24 -070083 install_plan_.metadata_size = response.metadata_size;
84 install_plan_.metadata_signature = response.metadata_signature;
David Zeuthene7f89172013-10-31 10:21:04 -070085 install_plan_.public_key_rsa = response.public_key_rsa;
Jay Srinivasan738fdf32012-12-07 17:40:54 -080086 install_plan_.hash_checks_mandatory = AreHashChecksMandatory(response);
Darin Petkov0406e402010-10-06 21:33:11 -070087 install_plan_.is_resume =
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080088 DeltaPerformer::CanResumeUpdate(system_state_->prefs(), response.hash);
Chris Sosabe45bef2013-04-09 18:25:12 -070089 if (install_plan_.is_resume) {
Gilad Arnold74b5f552014-10-07 08:17:16 -070090 payload_state->UpdateResumed();
Chris Sosabe45bef2013-04-09 18:25:12 -070091 } else {
Gilad Arnold74b5f552014-10-07 08:17:16 -070092 payload_state->UpdateRestarted();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080093 LOG_IF(WARNING, !DeltaPerformer::ResetUpdateProgress(
94 system_state_->prefs(), false))
Darin Petkov0406e402010-10-06 21:33:11 -070095 << "Unable to reset the update progress.";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080096 LOG_IF(WARNING, !system_state_->prefs()->SetString(
97 kPrefsUpdateCheckResponseHash, response.hash))
Darin Petkov0406e402010-10-06 21:33:11 -070098 << "Unable to save the update check response hash.";
99 }
Gilad Arnold21504f02013-05-24 08:51:22 -0700100 install_plan_.is_full_update = !response.is_delta_payload;
Darin Petkov0406e402010-10-06 21:33:11 -0700101
Chris Sosad317e402013-06-12 13:47:09 -0700102 TEST_AND_RETURN(utils::GetInstallDev(
Alex Deymo42432912013-07-12 20:21:15 -0700103 (!boot_device_.empty() ? boot_device_ :
104 system_state_->hardware()->BootDevice()),
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700105 &install_plan_.install_path));
106 install_plan_.kernel_install_path =
J. Richard Barnette30842932013-10-28 15:04:23 -0700107 utils::KernelDeviceOfBootDevice(install_plan_.install_path);
Allie Wood9f6f0a52015-03-30 11:25:47 -0700108 install_plan_.source_path = system_state_->hardware()->BootDevice();
109 install_plan_.kernel_source_path =
110 utils::KernelDeviceOfBootDevice(install_plan_.source_path);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000111
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700112 if (params->to_more_stable_channel() && params->is_powerwash_allowed())
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700113 install_plan_.powerwash_required = true;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700114
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700115
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700116 TEST_AND_RETURN(HasOutputPipe());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000117 if (HasOutputPipe())
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700118 SetOutputObject(install_plan_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000119 LOG(INFO) << "Using this install plan:";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700120 install_plan_.Dump();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700121
Darin Petkov6c118642010-10-21 12:06:30 -0700122 // Send the deadline data (if any) to Chrome through a file. This is a pretty
123 // hacky solution but should be OK for now.
124 //
Alex Vakulenko072359c2014-07-18 11:41:07 -0700125 // TODO(petkov): Re-architect this to avoid communication through a
Chris Sosabe45bef2013-04-09 18:25:12 -0700126 // file. Ideally, we would include this information in D-Bus's GetStatus
Darin Petkov6c118642010-10-21 12:06:30 -0700127 // method and UpdateStatus signal. A potential issue is that update_engine may
128 // be unresponsive during an update download.
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700129 utils::WriteFile(deadline_file_.c_str(),
Darin Petkov6c118642010-10-21 12:06:30 -0700130 response.deadline.data(),
131 response.deadline.size());
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700132 chmod(deadline_file_.c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
Darin Petkov6c118642010-10-21 12:06:30 -0700133
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700134 completer.set_code(ErrorCode::kSuccess);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000135}
136
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800137bool OmahaResponseHandlerAction::AreHashChecksMandatory(
138 const OmahaResponse& response) {
David Pursell907b4fa2015-01-27 10:27:38 -0800139 // We sometimes need to waive the hash checks in order to download from
140 // sources that don't provide hashes, such as dev server.
141 // At this point UpdateAttempter::IsAnyUpdateSourceAllowed() has already been
142 // checked, so an unofficial update URL won't get this far unless it's OK to
143 // use without a hash. Additionally, we want to always waive hash checks on
144 // unofficial builds (i.e. dev/test images).
145 // The end result is this:
146 // * Base image:
147 // - Official URLs require a hash.
148 // - Unofficial URLs only get this far if the IsAnyUpdateSourceAllowed()
149 // devmode/debugd checks pass, in which case the hash is waived.
150 // * Dev/test image:
151 // - Any URL is allowed through with no hash checking.
152 if (!system_state_->request_params()->IsUpdateUrlOfficial() ||
153 !system_state_->hardware()->IsOfficialBuild()) {
David Pursell02c18642014-11-06 11:26:11 -0800154 // Still do a hash check if a public key is included.
David Zeuthene7f89172013-10-31 10:21:04 -0700155 if (!response.public_key_rsa.empty()) {
David Zeuthenbc27aac2013-11-26 11:17:48 -0800156 // The autoupdate_CatchBadSignatures test checks for this string
157 // in log-files. Keep in sync.
David Zeuthene7f89172013-10-31 10:21:04 -0700158 LOG(INFO) << "Mandating payload hash checks since Omaha Response "
159 << "for unofficial build includes public RSA key.";
160 return true;
161 } else {
David Pursell02c18642014-11-06 11:26:11 -0800162 LOG(INFO) << "Waiving payload hash checks for unofficial update URL.";
David Zeuthene7f89172013-10-31 10:21:04 -0700163 return false;
164 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800165 }
166
David Zeuthen8f191b22013-08-06 12:27:50 -0700167 // If we're using p2p, |install_plan_.download_url| may contain a
168 // HTTP URL even if |response.payload_urls| contain only HTTPS URLs.
169 if (!StartsWithASCII(install_plan_.download_url, "https://", false)) {
170 LOG(INFO) << "Mandating hash checks since download_url is not HTTPS.";
171 return true;
172 }
173
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800174 // TODO(jaysri): VALIDATION: For official builds, we currently waive hash
175 // checks for HTTPS until we have rolled out at least once and are confident
176 // nothing breaks. chromium-os:37082 tracks turning this on for HTTPS
177 // eventually.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800178
179 // Even if there's a single non-HTTPS URL, make the hash checks as
180 // mandatory because we could be downloading the payload from any URL later
181 // on. It's really hard to do book-keeping based on each byte being
182 // downloaded to see whether we only used HTTPS throughout.
183 for (size_t i = 0; i < response.payload_urls.size(); i++) {
184 if (!StartsWithASCII(response.payload_urls[i], "https://", false)) {
185 LOG(INFO) << "Mandating payload hash checks since Omaha response "
186 << "contains non-HTTPS URL(s)";
187 return true;
188 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800189 }
190
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800191 LOG(INFO) << "Waiving payload hash checks since Omaha response "
192 << "only has HTTPS URL(s)";
193 return false;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800194}
195
adlr@google.com3defe6a2009-12-04 20:57:17 +0000196} // namespace chromeos_update_engine