blob: c85711b484cd8daf373a83c3685fcebf40236ec9 [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>
Jay Srinivasan738fdf32012-12-07 17:40:54 -080010#include "base/string_util.h"
Darin Petkov73058b42010-10-06 16:32:19 -070011
Jay Srinivasand29695d2013-04-08 15:08:05 -070012#include "update_engine/constants.h"
Darin Petkov0406e402010-10-06 21:33:11 -070013#include "update_engine/delta_performer.h"
Jay Srinivasan55f50c22013-01-10 19:24:35 -080014#include "update_engine/payload_state_interface.h"
Darin Petkov73058b42010-10-06 16:32:19 -070015#include "update_engine/prefs_interface.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000016#include "update_engine/utils.h"
17
18using std::string;
19
20namespace chromeos_update_engine {
21
Darin Petkov6c118642010-10-21 12:06:30 -070022const char OmahaResponseHandlerAction::kDeadlineFile[] =
23 "/tmp/update-check-response-deadline";
24
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080025OmahaResponseHandlerAction::OmahaResponseHandlerAction(
26 SystemState* system_state)
27 : system_state_(system_state),
Darin Petkovabc7bc02011-02-23 14:39:43 -080028 got_no_update_response_(false),
29 key_path_(DeltaPerformer::kUpdatePayloadPublicKeyPath) {}
30
adlr@google.com3defe6a2009-12-04 20:57:17 +000031void OmahaResponseHandlerAction::PerformAction() {
32 CHECK(HasInputObject());
33 ScopedActionCompleter completer(processor_, this);
Darin Petkov6a5b3222010-07-13 14:55:28 -070034 const OmahaResponse& response = GetInputObject();
adlr@google.com3defe6a2009-12-04 20:57:17 +000035 if (!response.update_exists) {
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080036 got_no_update_response_ = true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000037 LOG(INFO) << "There are no updates. Aborting.";
38 return;
39 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080040
41 // All decisions as to which URL should be used have already been done. So,
Jay Srinivasan53173b92013-05-17 17:13:01 -070042 // make the current URL as the download URL.
43 string current_url = system_state_->payload_state()->GetCurrentUrl();
44 if (current_url.empty()) {
45 // This shouldn't happen as we should always supply the HTTPS backup URL.
46 // Handling this anyway, just in case.
47 LOG(ERROR) << "There are no suitable URLs in the response to use.";
48 completer.set_code(kErrorCodeOmahaResponseInvalid);
49 return;
50 }
51
52 install_plan_.download_url = current_url;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080053
54 // Fill up the other properties based on the response.
Jay Srinivasan51dcf262012-09-13 17:24:32 -070055 install_plan_.payload_size = response.size;
56 install_plan_.payload_hash = response.hash;
Jay Srinivasanf4318702012-09-24 11:56:24 -070057 install_plan_.metadata_size = response.metadata_size;
58 install_plan_.metadata_signature = response.metadata_signature;
Jay Srinivasan738fdf32012-12-07 17:40:54 -080059 install_plan_.hash_checks_mandatory = AreHashChecksMandatory(response);
Darin Petkov0406e402010-10-06 21:33:11 -070060 install_plan_.is_resume =
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080061 DeltaPerformer::CanResumeUpdate(system_state_->prefs(), response.hash);
Chris Sosabe45bef2013-04-09 18:25:12 -070062 if (install_plan_.is_resume) {
63 system_state_->payload_state()->UpdateResumed();
64 } else {
Jay Srinivasan19409b72013-04-12 19:23:36 -070065 system_state_->payload_state()->UpdateRestarted();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080066 LOG_IF(WARNING, !DeltaPerformer::ResetUpdateProgress(
67 system_state_->prefs(), false))
Darin Petkov0406e402010-10-06 21:33:11 -070068 << "Unable to reset the update progress.";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080069 LOG_IF(WARNING, !system_state_->prefs()->SetString(
70 kPrefsUpdateCheckResponseHash, response.hash))
Darin Petkov0406e402010-10-06 21:33:11 -070071 << "Unable to save the update check response hash.";
72 }
73
adlr@google.com3defe6a2009-12-04 20:57:17 +000074 TEST_AND_RETURN(GetInstallDev(
75 (!boot_device_.empty() ? boot_device_ : utils::BootDevice()),
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070076 &install_plan_.install_path));
77 install_plan_.kernel_install_path =
78 utils::BootKernelDevice(install_plan_.install_path);
adlr@google.com3defe6a2009-12-04 20:57:17 +000079
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070080 OmahaRequestParams* params = system_state_->request_params();
81 if (params->to_more_stable_channel() && params->is_powerwash_allowed())
Jay Srinivasanae4697c2013-03-18 17:08:08 -070082 install_plan_.powerwash_required = true;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070083
Jay Srinivasanae4697c2013-03-18 17:08:08 -070084
Andrew de los Reyesf98bff82010-05-06 13:33:25 -070085 TEST_AND_RETURN(HasOutputPipe());
adlr@google.com3defe6a2009-12-04 20:57:17 +000086 if (HasOutputPipe())
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070087 SetOutputObject(install_plan_);
adlr@google.com3defe6a2009-12-04 20:57:17 +000088 LOG(INFO) << "Using this install plan:";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070089 install_plan_.Dump();
Darin Petkov6a5b3222010-07-13 14:55:28 -070090
Darin Petkov6c118642010-10-21 12:06:30 -070091 // Send the deadline data (if any) to Chrome through a file. This is a pretty
92 // hacky solution but should be OK for now.
93 //
94 // TODO(petkov): Rearchitect this to avoid communication through a
Chris Sosabe45bef2013-04-09 18:25:12 -070095 // file. Ideally, we would include this information in D-Bus's GetStatus
Darin Petkov6c118642010-10-21 12:06:30 -070096 // method and UpdateStatus signal. A potential issue is that update_engine may
97 // be unresponsive during an update download.
98 utils::WriteFile(kDeadlineFile,
99 response.deadline.data(),
100 response.deadline.size());
101 chmod(kDeadlineFile, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
102
David Zeuthena99981f2013-04-29 13:42:47 -0700103 completer.set_code(kErrorCodeSuccess);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000104}
105
106bool OmahaResponseHandlerAction::GetInstallDev(const std::string& boot_dev,
107 std::string* install_dev) {
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700108 TEST_AND_RETURN_FALSE(utils::StringHasPrefix(boot_dev, "/dev/"));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000109 string ret(boot_dev);
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700110 string::reverse_iterator it = ret.rbegin(); // last character in string
111 // Right now, we just switch '3' and '5' partition numbers.
112 TEST_AND_RETURN_FALSE((*it == '3') || (*it == '5'));
113 *it = (*it == '3') ? '5' : '3';
adlr@google.com3defe6a2009-12-04 20:57:17 +0000114 *install_dev = ret;
115 return true;
116}
117
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800118bool OmahaResponseHandlerAction::AreHashChecksMandatory(
119 const OmahaResponse& response) {
120 // All our internal testing uses dev server which doesn't generate metadata
121 // signatures yet. So, in order not to break image_to_live or other AU tools,
122 // we should waive the hash checks for those cases. Since all internal
123 // testing is done using a dev_image or test_image, we can use that as a
124 // criteria for waiving. This criteria reduces the attack surface as
125 // opposed to waiving the checks when we're in dev mode, because we do want
126 // to enforce the hash checks when our end customers run in dev mode if they
127 // are using an official build, so that they are protected more.
128 if (!utils::IsOfficialBuild()) {
129 LOG(INFO) << "Waiving payload hash checks for unofficial builds";
130 return false;
131 }
132
133 // TODO(jaysri): VALIDATION: For official builds, we currently waive hash
134 // checks for HTTPS until we have rolled out at least once and are confident
135 // nothing breaks. chromium-os:37082 tracks turning this on for HTTPS
136 // eventually.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800137
138 // Even if there's a single non-HTTPS URL, make the hash checks as
139 // mandatory because we could be downloading the payload from any URL later
140 // on. It's really hard to do book-keeping based on each byte being
141 // downloaded to see whether we only used HTTPS throughout.
142 for (size_t i = 0; i < response.payload_urls.size(); i++) {
143 if (!StartsWithASCII(response.payload_urls[i], "https://", false)) {
144 LOG(INFO) << "Mandating payload hash checks since Omaha response "
145 << "contains non-HTTPS URL(s)";
146 return true;
147 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800148 }
149
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800150 LOG(INFO) << "Waiving payload hash checks since Omaha response "
151 << "only has HTTPS URL(s)";
152 return false;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800153}
154
adlr@google.com3defe6a2009-12-04 20:57:17 +0000155} // namespace chromeos_update_engine