blob: ab083aad9f217d0097dbbaa30101c7cb03316635 [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
Darin Petkov0406e402010-10-06 21:33:11 -070012#include "update_engine/delta_performer.h"
Darin Petkov73058b42010-10-06 16:32:19 -070013#include "update_engine/prefs_interface.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000014#include "update_engine/utils.h"
15
16using std::string;
17
18namespace chromeos_update_engine {
19
Darin Petkov6c118642010-10-21 12:06:30 -070020const char OmahaResponseHandlerAction::kDeadlineFile[] =
21 "/tmp/update-check-response-deadline";
22
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080023OmahaResponseHandlerAction::OmahaResponseHandlerAction(
24 SystemState* system_state)
25 : system_state_(system_state),
Darin Petkovabc7bc02011-02-23 14:39:43 -080026 got_no_update_response_(false),
27 key_path_(DeltaPerformer::kUpdatePayloadPublicKeyPath) {}
28
adlr@google.com3defe6a2009-12-04 20:57:17 +000029void OmahaResponseHandlerAction::PerformAction() {
30 CHECK(HasInputObject());
31 ScopedActionCompleter completer(processor_, this);
Darin Petkov6a5b3222010-07-13 14:55:28 -070032 const OmahaResponse& response = GetInputObject();
adlr@google.com3defe6a2009-12-04 20:57:17 +000033 if (!response.update_exists) {
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080034 got_no_update_response_ = true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000035 LOG(INFO) << "There are no updates. Aborting.";
36 return;
37 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080038
39 // All decisions as to which URL should be used have already been done. So,
40 // make the download URL as the payload URL at the current url index.
41 uint32_t url_index = system_state_->payload_state()->GetUrlIndex();
42 LOG(INFO) << "Using Url" << url_index << " as the download url this time";
43 CHECK(url_index < response.payload_urls.size());
44 install_plan_.download_url = response.payload_urls[url_index];
45
46 // Fill up the other properties based on the response.
Jay Srinivasan51dcf262012-09-13 17:24:32 -070047 install_plan_.payload_size = response.size;
48 install_plan_.payload_hash = response.hash;
Jay Srinivasanf4318702012-09-24 11:56:24 -070049 install_plan_.metadata_size = response.metadata_size;
50 install_plan_.metadata_signature = response.metadata_signature;
Jay Srinivasan738fdf32012-12-07 17:40:54 -080051 install_plan_.hash_checks_mandatory = AreHashChecksMandatory(response);
Darin Petkov0406e402010-10-06 21:33:11 -070052 install_plan_.is_resume =
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080053 DeltaPerformer::CanResumeUpdate(system_state_->prefs(), response.hash);
Darin Petkov0406e402010-10-06 21:33:11 -070054 if (!install_plan_.is_resume) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080055 LOG_IF(WARNING, !DeltaPerformer::ResetUpdateProgress(
56 system_state_->prefs(), false))
Darin Petkov0406e402010-10-06 21:33:11 -070057 << "Unable to reset the update progress.";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080058 LOG_IF(WARNING, !system_state_->prefs()->SetString(
59 kPrefsUpdateCheckResponseHash, response.hash))
Darin Petkov0406e402010-10-06 21:33:11 -070060 << "Unable to save the update check response hash.";
61 }
62
adlr@google.com3defe6a2009-12-04 20:57:17 +000063 TEST_AND_RETURN(GetInstallDev(
64 (!boot_device_.empty() ? boot_device_ : utils::BootDevice()),
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070065 &install_plan_.install_path));
66 install_plan_.kernel_install_path =
67 utils::BootKernelDevice(install_plan_.install_path);
adlr@google.com3defe6a2009-12-04 20:57:17 +000068
Andrew de los Reyesf98bff82010-05-06 13:33:25 -070069 TEST_AND_RETURN(HasOutputPipe());
adlr@google.com3defe6a2009-12-04 20:57:17 +000070 if (HasOutputPipe())
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070071 SetOutputObject(install_plan_);
adlr@google.com3defe6a2009-12-04 20:57:17 +000072 LOG(INFO) << "Using this install plan:";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070073 install_plan_.Dump();
Darin Petkov6a5b3222010-07-13 14:55:28 -070074
Darin Petkov6c118642010-10-21 12:06:30 -070075 // Send the deadline data (if any) to Chrome through a file. This is a pretty
76 // hacky solution but should be OK for now.
77 //
78 // TODO(petkov): Rearchitect this to avoid communication through a
79 // file. Ideallly, we would include this information in D-Bus's GetStatus
80 // method and UpdateStatus signal. A potential issue is that update_engine may
81 // be unresponsive during an update download.
82 utils::WriteFile(kDeadlineFile,
83 response.deadline.data(),
84 response.deadline.size());
85 chmod(kDeadlineFile, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
86
Darin Petkovc1a8b422010-07-19 11:34:49 -070087 completer.set_code(kActionCodeSuccess);
adlr@google.com3defe6a2009-12-04 20:57:17 +000088}
89
90bool OmahaResponseHandlerAction::GetInstallDev(const std::string& boot_dev,
91 std::string* install_dev) {
Andrew de los Reyesf98bff82010-05-06 13:33:25 -070092 TEST_AND_RETURN_FALSE(utils::StringHasPrefix(boot_dev, "/dev/"));
adlr@google.com3defe6a2009-12-04 20:57:17 +000093 string ret(boot_dev);
Andrew de los Reyesf98bff82010-05-06 13:33:25 -070094 string::reverse_iterator it = ret.rbegin(); // last character in string
95 // Right now, we just switch '3' and '5' partition numbers.
96 TEST_AND_RETURN_FALSE((*it == '3') || (*it == '5'));
97 *it = (*it == '3') ? '5' : '3';
adlr@google.com3defe6a2009-12-04 20:57:17 +000098 *install_dev = ret;
99 return true;
100}
101
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800102bool OmahaResponseHandlerAction::AreHashChecksMandatory(
103 const OmahaResponse& response) {
104 // All our internal testing uses dev server which doesn't generate metadata
105 // signatures yet. So, in order not to break image_to_live or other AU tools,
106 // we should waive the hash checks for those cases. Since all internal
107 // testing is done using a dev_image or test_image, we can use that as a
108 // criteria for waiving. This criteria reduces the attack surface as
109 // opposed to waiving the checks when we're in dev mode, because we do want
110 // to enforce the hash checks when our end customers run in dev mode if they
111 // are using an official build, so that they are protected more.
112 if (!utils::IsOfficialBuild()) {
113 LOG(INFO) << "Waiving payload hash checks for unofficial builds";
114 return false;
115 }
116
117 // TODO(jaysri): VALIDATION: For official builds, we currently waive hash
118 // checks for HTTPS until we have rolled out at least once and are confident
119 // nothing breaks. chromium-os:37082 tracks turning this on for HTTPS
120 // eventually.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800121
122 // Even if there's a single non-HTTPS URL, make the hash checks as
123 // mandatory because we could be downloading the payload from any URL later
124 // on. It's really hard to do book-keeping based on each byte being
125 // downloaded to see whether we only used HTTPS throughout.
126 for (size_t i = 0; i < response.payload_urls.size(); i++) {
127 if (!StartsWithASCII(response.payload_urls[i], "https://", false)) {
128 LOG(INFO) << "Mandating payload hash checks since Omaha response "
129 << "contains non-HTTPS URL(s)";
130 return true;
131 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800132 }
133
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800134 LOG(INFO) << "Waiving payload hash checks since Omaha response "
135 << "only has HTTPS URL(s)";
136 return false;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800137}
138
adlr@google.com3defe6a2009-12-04 20:57:17 +0000139} // namespace chromeos_update_engine