Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2012 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 | // |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 17 | #include "update_engine/omaha_request_action.h" |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 18 | |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 19 | #include <inttypes.h> |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 20 | |
Zentaro Kavanagh | 0ff621c | 2018-07-13 13:06:56 -0700 | [diff] [blame] | 21 | #include <limits> |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 22 | #include <map> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 23 | #include <sstream> |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 24 | #include <string> |
Sen Jiang | 8cd4234 | 2018-01-31 12:06:59 -0800 | [diff] [blame] | 25 | #include <utility> |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 26 | #include <vector> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 27 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 28 | #include <base/bind.h> |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 29 | #include <base/logging.h> |
| 30 | #include <base/rand_util.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 31 | #include <base/strings/string_number_conversions.h> |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 32 | #include <base/strings/string_split.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 33 | #include <base/strings/string_util.h> |
| 34 | #include <base/strings/stringprintf.h> |
| 35 | #include <base/time/time.h> |
Sen Jiang | 684c9cd | 2017-10-17 16:26:45 -0700 | [diff] [blame] | 36 | #include <brillo/key_value_store.h> |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 37 | #include <expat.h> |
Alex Deymo | a259179 | 2015-11-17 00:39:40 -0300 | [diff] [blame] | 38 | #include <metrics/metrics_library.h> |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 39 | #include <policy/libpolicy.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 40 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 41 | #include "update_engine/common/action_pipe.h" |
| 42 | #include "update_engine/common/constants.h" |
| 43 | #include "update_engine/common/hardware_interface.h" |
| 44 | #include "update_engine/common/hash_calculator.h" |
| 45 | #include "update_engine/common/platform_constants.h" |
| 46 | #include "update_engine/common/prefs_interface.h" |
| 47 | #include "update_engine/common/utils.h" |
Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 48 | #include "update_engine/connection_manager_interface.h" |
Tianjie Xu | 282aa1f | 2017-09-05 13:42:45 -0700 | [diff] [blame] | 49 | #include "update_engine/metrics_reporter_interface.h" |
Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 50 | #include "update_engine/metrics_utils.h" |
Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 51 | #include "update_engine/omaha_request_builder_xml.h" |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 52 | #include "update_engine/omaha_request_params.h" |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 53 | #include "update_engine/p2p_manager.h" |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 54 | #include "update_engine/payload_state_interface.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 55 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 56 | using base::Time; |
| 57 | using base::TimeDelta; |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 58 | using chromeos_update_manager::kRollforwardInfinity; |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 59 | using std::map; |
Zentaro Kavanagh | 0ff621c | 2018-07-13 13:06:56 -0700 | [diff] [blame] | 60 | using std::numeric_limits; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 61 | using std::string; |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 62 | using std::vector; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 63 | |
| 64 | namespace chromeos_update_engine { |
| 65 | |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 66 | // List of custom attributes that we interpret in the Omaha response: |
| 67 | constexpr char kAttrDeadline[] = "deadline"; |
| 68 | constexpr char kAttrDisableP2PForDownloading[] = "DisableP2PForDownloading"; |
| 69 | constexpr char kAttrDisableP2PForSharing[] = "DisableP2PForSharing"; |
| 70 | constexpr char kAttrDisablePayloadBackoff[] = "DisablePayloadBackoff"; |
| 71 | constexpr char kAttrVersion[] = "version"; |
Jay Srinivasan | d671e97 | 2013-01-11 17:17:19 -0800 | [diff] [blame] | 72 | // Deprecated: "IsDelta" |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 73 | constexpr char kAttrIsDeltaPayload[] = "IsDeltaPayload"; |
| 74 | constexpr char kAttrMaxFailureCountPerUrl[] = "MaxFailureCountPerUrl"; |
| 75 | constexpr char kAttrMaxDaysToScatter[] = "MaxDaysToScatter"; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 76 | // Deprecated: "ManifestSignatureRsa" |
| 77 | // Deprecated: "ManifestSize" |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 78 | constexpr char kAttrMetadataSignatureRsa[] = "MetadataSignatureRsa"; |
| 79 | constexpr char kAttrMetadataSize[] = "MetadataSize"; |
| 80 | constexpr char kAttrMoreInfo[] = "MoreInfo"; |
| 81 | constexpr char kAttrNoUpdate[] = "noupdate"; |
Don Garrett | 42bd3aa | 2013-04-10 18:14:56 -0700 | [diff] [blame] | 82 | // Deprecated: "NeedsAdmin" |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 83 | constexpr char kAttrPollInterval[] = "PollInterval"; |
| 84 | constexpr char kAttrPowerwash[] = "Powerwash"; |
| 85 | constexpr char kAttrPrompt[] = "Prompt"; |
| 86 | constexpr char kAttrPublicKeyRsa[] = "PublicKeyRsa"; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 87 | |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 88 | // List of attributes that we interpret in the Omaha response: |
| 89 | constexpr char kAttrAppId[] = "appid"; |
| 90 | constexpr char kAttrCodeBase[] = "codebase"; |
| 91 | constexpr char kAttrCohort[] = "cohort"; |
| 92 | constexpr char kAttrCohortHint[] = "cohorthint"; |
| 93 | constexpr char kAttrCohortName[] = "cohortname"; |
| 94 | constexpr char kAttrElapsedDays[] = "elapsed_days"; |
| 95 | constexpr char kAttrElapsedSeconds[] = "elapsed_seconds"; |
| 96 | constexpr char kAttrEvent[] = "event"; |
| 97 | constexpr char kAttrHashSha256[] = "hash_sha256"; |
| 98 | // Deprecated: "hash"; Although we still need to pass it from the server for |
| 99 | // backward compatibility. |
| 100 | constexpr char kAttrName[] = "name"; |
| 101 | // Deprecated: "sha256"; Although we still need to pass it from the server for |
| 102 | // backward compatibility. |
| 103 | constexpr char kAttrSize[] = "size"; |
| 104 | constexpr char kAttrStatus[] = "status"; |
| 105 | |
| 106 | // List of values that we interpret in the Omaha response: |
| 107 | constexpr char kValPostInstall[] = "postinstall"; |
| 108 | constexpr char kValNoUpdate[] = "noupdate"; |
| 109 | |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 110 | // updatecheck attributes (without the underscore prefix). |
Amin Hassani | 2cbb069 | 2019-10-30 13:36:17 -0700 | [diff] [blame^] | 111 | // Deprecated: "eol" |
Jae Hoon Kim | 051627a | 2019-09-03 12:56:32 -0700 | [diff] [blame] | 112 | constexpr char kAttrEolDate[] = "eol_date"; |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 113 | constexpr char kAttrRollback[] = "rollback"; |
| 114 | constexpr char kAttrFirmwareVersion[] = "firmware_version"; |
| 115 | constexpr char kAttrKernelVersion[] = "kernel_version"; |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 116 | |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 117 | // Struct used for holding data obtained when parsing the XML. |
| 118 | struct OmahaParserData { |
David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 119 | explicit OmahaParserData(XML_Parser _xml_parser) : xml_parser(_xml_parser) {} |
| 120 | |
| 121 | // Pointer to the expat XML_Parser object. |
| 122 | XML_Parser xml_parser; |
| 123 | |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 124 | // This is the state of the parser as it's processing the XML. |
| 125 | bool failed = false; |
David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 126 | bool entity_decl = false; |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 127 | string current_path; |
| 128 | |
| 129 | // These are the values extracted from the XML. |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 130 | string updatecheck_poll_interval; |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 131 | map<string, string> updatecheck_attrs; |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 132 | string daystart_elapsed_days; |
| 133 | string daystart_elapsed_seconds; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 134 | |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 135 | struct App { |
Aaron Wood | 7dcdedf | 2017-09-06 17:17:41 -0700 | [diff] [blame] | 136 | string id; |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 137 | vector<string> url_codebase; |
| 138 | string manifest_version; |
| 139 | map<string, string> action_postinstall_attrs; |
| 140 | string updatecheck_status; |
Sen Jiang | b1e063a | 2017-09-15 17:44:31 -0700 | [diff] [blame] | 141 | string cohort; |
| 142 | string cohorthint; |
| 143 | string cohortname; |
| 144 | bool cohort_set = false; |
| 145 | bool cohorthint_set = false; |
| 146 | bool cohortname_set = false; |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 147 | |
| 148 | struct Package { |
| 149 | string name; |
| 150 | string size; |
| 151 | string hash; |
| 152 | }; |
| 153 | vector<Package> packages; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 154 | }; |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 155 | vector<App> apps; |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | namespace { |
| 159 | |
| 160 | // Callback function invoked by expat. |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 161 | void ParserHandlerStart(void* user_data, |
| 162 | const XML_Char* element, |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 163 | const XML_Char** attr) { |
| 164 | OmahaParserData* data = reinterpret_cast<OmahaParserData*>(user_data); |
| 165 | |
| 166 | if (data->failed) |
| 167 | return; |
| 168 | |
| 169 | data->current_path += string("/") + element; |
| 170 | |
| 171 | map<string, string> attrs; |
| 172 | if (attr != nullptr) { |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 173 | for (int n = 0; attr[n] != nullptr && attr[n + 1] != nullptr; n += 2) { |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 174 | string key = attr[n]; |
| 175 | string value = attr[n + 1]; |
| 176 | attrs[key] = value; |
| 177 | } |
| 178 | } |
| 179 | |
Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 180 | if (data->current_path == "/response/app") { |
Sen Jiang | b1e063a | 2017-09-15 17:44:31 -0700 | [diff] [blame] | 181 | OmahaParserData::App app; |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 182 | if (attrs.find(kAttrAppId) != attrs.end()) { |
| 183 | app.id = attrs[kAttrAppId]; |
Aaron Wood | 7dcdedf | 2017-09-06 17:17:41 -0700 | [diff] [blame] | 184 | } |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 185 | if (attrs.find(kAttrCohort) != attrs.end()) { |
Sen Jiang | b1e063a | 2017-09-15 17:44:31 -0700 | [diff] [blame] | 186 | app.cohort_set = true; |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 187 | app.cohort = attrs[kAttrCohort]; |
Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 188 | } |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 189 | if (attrs.find(kAttrCohortHint) != attrs.end()) { |
Sen Jiang | b1e063a | 2017-09-15 17:44:31 -0700 | [diff] [blame] | 190 | app.cohorthint_set = true; |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 191 | app.cohorthint = attrs[kAttrCohortHint]; |
Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 192 | } |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 193 | if (attrs.find(kAttrCohortName) != attrs.end()) { |
Sen Jiang | b1e063a | 2017-09-15 17:44:31 -0700 | [diff] [blame] | 194 | app.cohortname_set = true; |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 195 | app.cohortname = attrs[kAttrCohortName]; |
Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 196 | } |
Sen Jiang | b1e063a | 2017-09-15 17:44:31 -0700 | [diff] [blame] | 197 | data->apps.push_back(std::move(app)); |
Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 198 | } else if (data->current_path == "/response/app/updatecheck") { |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 199 | if (!data->apps.empty()) |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 200 | data->apps.back().updatecheck_status = attrs[kAttrStatus]; |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 201 | if (data->updatecheck_poll_interval.empty()) |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 202 | data->updatecheck_poll_interval = attrs[kAttrPollInterval]; |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 203 | // Omaha sends arbitrary key-value pairs as extra attributes starting with |
| 204 | // an underscore. |
| 205 | for (const auto& attr : attrs) { |
| 206 | if (!attr.first.empty() && attr.first[0] == '_') |
| 207 | data->updatecheck_attrs[attr.first.substr(1)] = attr.second; |
| 208 | } |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 209 | } else if (data->current_path == "/response/daystart") { |
| 210 | // Get the install-date. |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 211 | data->daystart_elapsed_days = attrs[kAttrElapsedDays]; |
| 212 | data->daystart_elapsed_seconds = attrs[kAttrElapsedSeconds]; |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 213 | } else if (data->current_path == "/response/app/updatecheck/urls/url") { |
| 214 | // Look at all <url> elements. |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 215 | if (!data->apps.empty()) |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 216 | data->apps.back().url_codebase.push_back(attrs[kAttrCodeBase]); |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 217 | } else if (data->current_path == |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 218 | "/response/app/updatecheck/manifest/packages/package") { |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 219 | // Look at all <package> elements. |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 220 | if (!data->apps.empty()) |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 221 | data->apps.back().packages.push_back({.name = attrs[kAttrName], |
| 222 | .size = attrs[kAttrSize], |
| 223 | .hash = attrs[kAttrHashSha256]}); |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 224 | } else if (data->current_path == "/response/app/updatecheck/manifest") { |
| 225 | // Get the version. |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 226 | if (!data->apps.empty()) |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 227 | data->apps.back().manifest_version = attrs[kAttrVersion]; |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 228 | } else if (data->current_path == |
| 229 | "/response/app/updatecheck/manifest/actions/action") { |
| 230 | // We only care about the postinstall action. |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 231 | if (attrs[kAttrEvent] == kValPostInstall && !data->apps.empty()) { |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 232 | data->apps.back().action_postinstall_attrs = std::move(attrs); |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // Callback function invoked by expat. |
| 238 | void ParserHandlerEnd(void* user_data, const XML_Char* element) { |
| 239 | OmahaParserData* data = reinterpret_cast<OmahaParserData*>(user_data); |
| 240 | if (data->failed) |
| 241 | return; |
| 242 | |
| 243 | const string path_suffix = string("/") + element; |
| 244 | |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 245 | if (!base::EndsWith( |
| 246 | data->current_path, path_suffix, base::CompareCase::SENSITIVE)) { |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 247 | LOG(ERROR) << "Unexpected end element '" << element |
| 248 | << "' with current_path='" << data->current_path << "'"; |
| 249 | data->failed = true; |
| 250 | return; |
| 251 | } |
| 252 | data->current_path.resize(data->current_path.size() - path_suffix.size()); |
| 253 | } |
| 254 | |
David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 255 | // Callback function invoked by expat. |
| 256 | // |
| 257 | // This is called for entity declarations. Since Omaha is guaranteed |
| 258 | // to never return any XML with entities our course of action is to |
| 259 | // just stop parsing. This avoids potential resource exhaustion |
| 260 | // problems AKA the "billion laughs". CVE-2013-0340. |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 261 | void ParserHandlerEntityDecl(void* user_data, |
| 262 | const XML_Char* entity_name, |
David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 263 | int is_parameter_entity, |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 264 | const XML_Char* value, |
David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 265 | int value_length, |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 266 | const XML_Char* base, |
| 267 | const XML_Char* system_id, |
| 268 | const XML_Char* public_id, |
| 269 | const XML_Char* notation_name) { |
David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 270 | OmahaParserData* data = reinterpret_cast<OmahaParserData*>(user_data); |
| 271 | |
| 272 | LOG(ERROR) << "XML entities are not supported. Aborting parsing."; |
| 273 | data->failed = true; |
| 274 | data->entity_decl = true; |
| 275 | XML_StopParser(data->xml_parser, false); |
| 276 | } |
| 277 | |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 278 | } // namespace |
| 279 | |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 280 | OmahaRequestAction::OmahaRequestAction( |
| 281 | SystemState* system_state, |
| 282 | OmahaEvent* event, |
| 283 | std::unique_ptr<HttpFetcher> http_fetcher, |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 284 | bool ping_only, |
| 285 | const string& session_id) |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 286 | : system_state_(system_state), |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 287 | params_(system_state->request_params()), |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 288 | event_(event), |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 289 | http_fetcher_(std::move(http_fetcher)), |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 290 | policy_provider_(std::make_unique<policy::PolicyProvider>()), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 291 | ping_only_(ping_only), |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 292 | ping_active_days_(0), |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 293 | ping_roll_call_days_(0), |
| 294 | session_id_(session_id) { |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 295 | policy_provider_->Reload(); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 296 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 297 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 298 | OmahaRequestAction::~OmahaRequestAction() {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 299 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 300 | // Calculates the value to use for the ping days parameter. |
| 301 | int OmahaRequestAction::CalculatePingDays(const string& key) { |
| 302 | int days = kNeverPinged; |
| 303 | int64_t last_ping = 0; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 304 | if (system_state_->prefs()->GetInt64(key, &last_ping) && last_ping >= 0) { |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 305 | days = (Time::Now() - Time::FromInternalValue(last_ping)).InDays(); |
| 306 | if (days < 0) { |
| 307 | // If |days| is negative, then the system clock must have jumped |
| 308 | // back in time since the ping was sent. Mark the value so that |
| 309 | // it doesn't get sent to the server but we still update the |
| 310 | // last ping daystart preference. This way the next ping time |
| 311 | // will be correct, hopefully. |
| 312 | days = kPingTimeJump; |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 313 | LOG(WARNING) |
| 314 | << "System clock jumped back in time. Resetting ping daystarts."; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | return days; |
| 318 | } |
| 319 | |
| 320 | void OmahaRequestAction::InitPingDays() { |
| 321 | // We send pings only along with update checks, not with events. |
| 322 | if (IsEvent()) { |
| 323 | return; |
| 324 | } |
| 325 | // TODO(petkov): Figure a way to distinguish active use pings |
| 326 | // vs. roll call pings. Currently, the two pings are identical. A |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 327 | // fix needs to change this code as well as UpdateLastPingDays and ShouldPing. |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 328 | ping_active_days_ = CalculatePingDays(kPrefsLastActivePingDay); |
| 329 | ping_roll_call_days_ = CalculatePingDays(kPrefsLastRollCallPingDay); |
| 330 | } |
| 331 | |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 332 | bool OmahaRequestAction::ShouldPing() const { |
Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 333 | if (ping_active_days_ == kNeverPinged && |
| 334 | ping_roll_call_days_ == kNeverPinged) { |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 335 | int powerwash_count = system_state_->hardware()->GetPowerwashCount(); |
| 336 | if (powerwash_count > 0) { |
| 337 | LOG(INFO) << "Not sending ping with a=-1 r=-1 to omaha because " |
| 338 | << "powerwash_count is " << powerwash_count; |
| 339 | return false; |
| 340 | } |
Amin Hassani | 1677e81 | 2017-06-21 13:36:36 -0700 | [diff] [blame] | 341 | if (system_state_->hardware()->GetFirstActiveOmahaPingSent()) { |
| 342 | LOG(INFO) << "Not sending ping with a=-1 r=-1 to omaha because " |
| 343 | << "the first_active_omaha_ping_sent is true"; |
| 344 | return false; |
| 345 | } |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 346 | return true; |
| 347 | } |
| 348 | return ping_active_days_ > 0 || ping_roll_call_days_ > 0; |
| 349 | } |
| 350 | |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 351 | // static |
| 352 | int OmahaRequestAction::GetInstallDate(SystemState* system_state) { |
| 353 | PrefsInterface* prefs = system_state->prefs(); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 354 | if (prefs == nullptr) |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 355 | return -1; |
| 356 | |
| 357 | // If we have the value stored on disk, just return it. |
| 358 | int64_t stored_value; |
| 359 | if (prefs->GetInt64(kPrefsInstallDateDays, &stored_value)) { |
| 360 | // Convert and sanity-check. |
| 361 | int install_date_days = static_cast<int>(stored_value); |
| 362 | if (install_date_days >= 0) |
| 363 | return install_date_days; |
| 364 | LOG(ERROR) << "Dropping stored Omaha InstallData since its value num_days=" |
| 365 | << install_date_days << " looks suspicious."; |
| 366 | prefs->Delete(kPrefsInstallDateDays); |
| 367 | } |
| 368 | |
| 369 | // Otherwise, if OOBE is not complete then do nothing and wait for |
| 370 | // ParseResponse() to call ParseInstallDate() and then |
| 371 | // PersistInstallDate() to set the kPrefsInstallDateDays state |
| 372 | // variable. Once that is done, we'll then report back in future |
| 373 | // Omaha requests. This works exactly because OOBE triggers an |
| 374 | // update check. |
| 375 | // |
| 376 | // However, if OOBE is complete and the kPrefsInstallDateDays state |
| 377 | // variable is not set, there are two possibilities |
| 378 | // |
| 379 | // 1. The update check in OOBE failed so we never got a response |
| 380 | // from Omaha (no network etc.); or |
| 381 | // |
| 382 | // 2. OOBE was done on an older version that didn't write to the |
| 383 | // kPrefsInstallDateDays state variable. |
| 384 | // |
| 385 | // In both cases, we approximate the install date by simply |
| 386 | // inspecting the timestamp of when OOBE happened. |
| 387 | |
| 388 | Time time_of_oobe; |
Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 389 | if (!system_state->hardware()->IsOOBEEnabled() || |
| 390 | !system_state->hardware()->IsOOBEComplete(&time_of_oobe)) { |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 391 | LOG(INFO) << "Not generating Omaha InstallData as we have " |
Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 392 | << "no prefs file and OOBE is not complete or not enabled."; |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 393 | return -1; |
| 394 | } |
| 395 | |
| 396 | int num_days; |
| 397 | if (!utils::ConvertToOmahaInstallDate(time_of_oobe, &num_days)) { |
| 398 | LOG(ERROR) << "Not generating Omaha InstallData from time of OOBE " |
| 399 | << "as its value '" << utils::ToString(time_of_oobe) |
| 400 | << "' looks suspicious."; |
| 401 | return -1; |
| 402 | } |
| 403 | |
| 404 | // Persist this to disk, for future use. |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 405 | if (!OmahaRequestAction::PersistInstallDate( |
| 406 | system_state, num_days, kProvisionedFromOOBEMarker)) |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 407 | return -1; |
| 408 | |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 409 | LOG(INFO) << "Set the Omaha InstallDate from OOBE time-stamp to " << num_days |
| 410 | << " days"; |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 411 | |
| 412 | return num_days; |
| 413 | } |
| 414 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 415 | void OmahaRequestAction::PerformAction() { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 416 | http_fetcher_->set_delegate(this); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 417 | InitPingDays(); |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 418 | if (ping_only_ && !ShouldPing()) { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 419 | processor_->ActionComplete(this, ErrorCode::kSuccess); |
Thieu Le | b44e9e8 | 2011-06-06 14:34:04 -0700 | [diff] [blame] | 420 | return; |
| 421 | } |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 422 | |
Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 423 | OmahaRequestBuilderXml omaha_request(event_.get(), |
| 424 | params_, |
| 425 | ping_only_, |
| 426 | ShouldPing(), // include_ping |
| 427 | ping_active_days_, |
| 428 | ping_roll_call_days_, |
| 429 | GetInstallDate(system_state_), |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 430 | system_state_->prefs(), |
| 431 | session_id_); |
Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 432 | string request_post = omaha_request.GetRequest(); |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 433 | |
Sen Jiang | 42fa45e | 2018-03-12 11:02:14 -0700 | [diff] [blame] | 434 | // Set X-Goog-Update headers. |
Alex Deymo | 14ad88e | 2016-06-29 12:30:14 -0700 | [diff] [blame] | 435 | http_fetcher_->SetHeader(kXGoogleUpdateInteractivity, |
| 436 | params_->interactive() ? "fg" : "bg"); |
| 437 | http_fetcher_->SetHeader(kXGoogleUpdateAppId, params_->GetAppId()); |
| 438 | http_fetcher_->SetHeader( |
| 439 | kXGoogleUpdateUpdater, |
| 440 | base::StringPrintf( |
| 441 | "%s-%s", constants::kOmahaUpdaterID, kOmahaUpdaterVersion)); |
| 442 | |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 443 | http_fetcher_->SetPostData( |
| 444 | request_post.data(), request_post.size(), kHttpContentTypeTextXml); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 445 | LOG(INFO) << "Posting an Omaha request to " << params_->update_url(); |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 446 | LOG(INFO) << "Request: " << request_post; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 447 | http_fetcher_->BeginTransfer(params_->update_url()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 450 | void OmahaRequestAction::TerminateProcessing() { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 451 | http_fetcher_->TerminateTransfer(); |
| 452 | } |
| 453 | |
| 454 | // We just store the response in the buffer. Once we've received all bytes, |
| 455 | // we'll look in the buffer and decide what to do. |
Amin Hassani | 0cd9d77 | 2018-07-31 23:55:43 -0700 | [diff] [blame] | 456 | bool OmahaRequestAction::ReceivedBytes(HttpFetcher* fetcher, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 457 | const void* bytes, |
| 458 | size_t length) { |
| 459 | const uint8_t* byte_ptr = reinterpret_cast<const uint8_t*>(bytes); |
| 460 | response_buffer_.insert(response_buffer_.end(), byte_ptr, byte_ptr + length); |
Amin Hassani | 0cd9d77 | 2018-07-31 23:55:43 -0700 | [diff] [blame] | 461 | return true; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | namespace { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 465 | |
| 466 | // Parses a 64 bit base-10 int from a string and returns it. Returns 0 |
| 467 | // on error. If the string contains "0", that's indistinguishable from |
| 468 | // error. |
| 469 | off_t ParseInt(const string& str) { |
| 470 | off_t ret = 0; |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 471 | int rc = sscanf(str.c_str(), "%" PRIi64, &ret); // NOLINT(runtime/printf) |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 472 | if (rc < 1) { |
| 473 | // failure |
| 474 | return 0; |
| 475 | } |
| 476 | return ret; |
| 477 | } |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 478 | |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 479 | // Parses |str| and returns |true| if, and only if, its value is "true". |
| 480 | bool ParseBool(const string& str) { |
| 481 | return str == "true"; |
| 482 | } |
| 483 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 484 | // Update the last ping day preferences based on the server daystart |
| 485 | // response. Returns true on success, false otherwise. |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 486 | bool UpdateLastPingDays(OmahaParserData* parser_data, PrefsInterface* prefs) { |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 487 | int64_t elapsed_seconds = 0; |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 488 | TEST_AND_RETURN_FALSE(base::StringToInt64( |
| 489 | parser_data->daystart_elapsed_seconds, &elapsed_seconds)); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 490 | TEST_AND_RETURN_FALSE(elapsed_seconds >= 0); |
| 491 | |
| 492 | // Remember the local time that matches the server's last midnight |
| 493 | // time. |
| 494 | Time daystart = Time::Now() - TimeDelta::FromSeconds(elapsed_seconds); |
| 495 | prefs->SetInt64(kPrefsLastActivePingDay, daystart.ToInternalValue()); |
| 496 | prefs->SetInt64(kPrefsLastRollCallPingDay, daystart.ToInternalValue()); |
| 497 | return true; |
| 498 | } |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 499 | |
| 500 | // Parses the package node in the given XML document and populates |
| 501 | // |output_object| if valid. Returns true if we should continue the parsing. |
| 502 | // False otherwise, in which case it sets any error code using |completer|. |
| 503 | bool ParsePackage(OmahaParserData::App* app, |
| 504 | OmahaResponse* output_object, |
| 505 | ScopedActionCompleter* completer) { |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 506 | if (app->updatecheck_status.empty() || |
| 507 | app->updatecheck_status == kValNoUpdate) { |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 508 | if (!app->packages.empty()) { |
| 509 | LOG(ERROR) << "No update in this <app> but <package> is not empty."; |
| 510 | completer->set_code(ErrorCode::kOmahaResponseInvalid); |
| 511 | return false; |
| 512 | } |
| 513 | return true; |
| 514 | } |
| 515 | if (app->packages.empty()) { |
| 516 | LOG(ERROR) << "Omaha Response has no packages"; |
| 517 | completer->set_code(ErrorCode::kOmahaResponseInvalid); |
| 518 | return false; |
| 519 | } |
| 520 | if (app->url_codebase.empty()) { |
| 521 | LOG(ERROR) << "No Omaha Response URLs"; |
| 522 | completer->set_code(ErrorCode::kOmahaResponseInvalid); |
| 523 | return false; |
| 524 | } |
| 525 | LOG(INFO) << "Found " << app->url_codebase.size() << " url(s)"; |
| 526 | vector<string> metadata_sizes = |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 527 | base::SplitString(app->action_postinstall_attrs[kAttrMetadataSize], |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 528 | ":", |
| 529 | base::TRIM_WHITESPACE, |
| 530 | base::SPLIT_WANT_ALL); |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 531 | vector<string> metadata_signatures = base::SplitString( |
| 532 | app->action_postinstall_attrs[kAttrMetadataSignatureRsa], |
| 533 | ":", |
| 534 | base::TRIM_WHITESPACE, |
| 535 | base::SPLIT_WANT_ALL); |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 536 | vector<string> is_delta_payloads = |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 537 | base::SplitString(app->action_postinstall_attrs[kAttrIsDeltaPayload], |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 538 | ":", |
| 539 | base::TRIM_WHITESPACE, |
| 540 | base::SPLIT_WANT_ALL); |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 541 | for (size_t i = 0; i < app->packages.size(); i++) { |
| 542 | const auto& package = app->packages[i]; |
| 543 | if (package.name.empty()) { |
| 544 | LOG(ERROR) << "Omaha Response has empty package name"; |
| 545 | completer->set_code(ErrorCode::kOmahaResponseInvalid); |
| 546 | return false; |
| 547 | } |
| 548 | LOG(INFO) << "Found package " << package.name; |
| 549 | |
| 550 | OmahaResponse::Package out_package; |
| 551 | for (const string& codebase : app->url_codebase) { |
| 552 | if (codebase.empty()) { |
| 553 | LOG(ERROR) << "Omaha Response URL has empty codebase"; |
| 554 | completer->set_code(ErrorCode::kOmahaResponseInvalid); |
| 555 | return false; |
| 556 | } |
| 557 | out_package.payload_urls.push_back(codebase + package.name); |
| 558 | } |
| 559 | // Parse the payload size. |
| 560 | base::StringToUint64(package.size, &out_package.size); |
| 561 | if (out_package.size <= 0) { |
| 562 | LOG(ERROR) << "Omaha Response has invalid payload size: " << package.size; |
| 563 | completer->set_code(ErrorCode::kOmahaResponseInvalid); |
| 564 | return false; |
| 565 | } |
| 566 | LOG(INFO) << "Payload size = " << out_package.size << " bytes"; |
| 567 | |
| 568 | if (i < metadata_sizes.size()) |
| 569 | base::StringToUint64(metadata_sizes[i], &out_package.metadata_size); |
| 570 | LOG(INFO) << "Payload metadata size = " << out_package.metadata_size |
| 571 | << " bytes"; |
| 572 | |
| 573 | if (i < metadata_signatures.size()) |
| 574 | out_package.metadata_signature = metadata_signatures[i]; |
| 575 | LOG(INFO) << "Payload metadata signature = " |
| 576 | << out_package.metadata_signature; |
| 577 | |
| 578 | out_package.hash = package.hash; |
| 579 | if (out_package.hash.empty()) { |
| 580 | LOG(ERROR) << "Omaha Response has empty hash_sha256 value"; |
| 581 | completer->set_code(ErrorCode::kOmahaResponseInvalid); |
| 582 | return false; |
| 583 | } |
| 584 | LOG(INFO) << "Payload hash = " << out_package.hash; |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 585 | |
| 586 | if (i < is_delta_payloads.size()) |
| 587 | out_package.is_delta = ParseBool(is_delta_payloads[i]); |
| 588 | LOG(INFO) << "Payload is delta = " << utils::ToString(out_package.is_delta); |
| 589 | |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 590 | output_object->packages.push_back(std::move(out_package)); |
| 591 | } |
| 592 | |
| 593 | return true; |
| 594 | } |
| 595 | |
Zentaro Kavanagh | 0ff621c | 2018-07-13 13:06:56 -0700 | [diff] [blame] | 596 | // Parses the 2 key version strings kernel_version and firmware_version. If the |
| 597 | // field is not present, or cannot be parsed the values default to 0xffff. |
Zentaro Kavanagh | 0ef9a2f | 2018-07-02 12:05:07 -0700 | [diff] [blame] | 598 | void ParseRollbackVersions(int allowed_milestones, |
| 599 | OmahaParserData* parser_data, |
Zentaro Kavanagh | 0ff621c | 2018-07-13 13:06:56 -0700 | [diff] [blame] | 600 | OmahaResponse* output_object) { |
| 601 | utils::ParseRollbackKeyVersion( |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 602 | parser_data->updatecheck_attrs[kAttrFirmwareVersion], |
Zentaro Kavanagh | 0ff621c | 2018-07-13 13:06:56 -0700 | [diff] [blame] | 603 | &output_object->rollback_key_version.firmware_key, |
| 604 | &output_object->rollback_key_version.firmware); |
| 605 | utils::ParseRollbackKeyVersion( |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 606 | parser_data->updatecheck_attrs[kAttrKernelVersion], |
Zentaro Kavanagh | 0ff621c | 2018-07-13 13:06:56 -0700 | [diff] [blame] | 607 | &output_object->rollback_key_version.kernel_key, |
| 608 | &output_object->rollback_key_version.kernel); |
Zentaro Kavanagh | 0ef9a2f | 2018-07-02 12:05:07 -0700 | [diff] [blame] | 609 | |
| 610 | // Create the attribute name strings for milestone N - allowed_milestones. |
| 611 | const string firmware_max_rollforward_attr = |
| 612 | base::StringPrintf("%s_%i", kAttrFirmwareVersion, allowed_milestones); |
| 613 | const string kernel_max_rollforward_attr = |
| 614 | base::StringPrintf("%s_%i", kAttrKernelVersion, allowed_milestones); |
| 615 | |
| 616 | const bool max_firmware_and_kernel_exist = |
| 617 | parser_data->updatecheck_attrs.count(firmware_max_rollforward_attr) > 0 && |
| 618 | parser_data->updatecheck_attrs.count(kernel_max_rollforward_attr) > 0; |
| 619 | |
| 620 | string firmware_version; |
| 621 | string kernel_version; |
| 622 | if (max_firmware_and_kernel_exist) { |
| 623 | firmware_version = |
| 624 | parser_data->updatecheck_attrs[firmware_max_rollforward_attr]; |
| 625 | kernel_version = |
| 626 | parser_data->updatecheck_attrs[kernel_max_rollforward_attr]; |
| 627 | } |
| 628 | |
| 629 | LOG(INFO) << "For milestone N-" << allowed_milestones |
| 630 | << " firmware_key_version=" << firmware_version |
| 631 | << " kernel_key_version=" << kernel_version; |
| 632 | |
| 633 | OmahaResponse::RollbackKeyVersion version; |
| 634 | utils::ParseRollbackKeyVersion( |
| 635 | firmware_version, &version.firmware_key, &version.firmware); |
| 636 | utils::ParseRollbackKeyVersion( |
| 637 | kernel_version, &version.kernel_key, &version.kernel); |
| 638 | |
| 639 | output_object->past_rollback_key_version = std::move(version); |
Zentaro Kavanagh | 0ff621c | 2018-07-13 13:06:56 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 642 | } // namespace |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 643 | |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 644 | bool OmahaRequestAction::ParseResponse(OmahaParserData* parser_data, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 645 | OmahaResponse* output_object, |
| 646 | ScopedActionCompleter* completer) { |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 647 | if (parser_data->apps.empty()) { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 648 | completer->set_code(ErrorCode::kOmahaResponseInvalid); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 649 | return false; |
| 650 | } |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 651 | LOG(INFO) << "Found " << parser_data->apps.size() << " <app>."; |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 652 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 653 | // chromium-os:37289: The PollInterval is not supported by Omaha server |
| 654 | // currently. But still keeping this existing code in case we ever decide to |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 655 | // slow down the request rate from the server-side. Note that the PollInterval |
| 656 | // is not persisted, so it has to be sent by the server on every response to |
| 657 | // guarantee that the scheduler uses this value (otherwise, if the device got |
| 658 | // rebooted after the last server-indicated value, it'll revert to the default |
| 659 | // value). Also kDefaultMaxUpdateChecks value for the scattering logic is |
| 660 | // based on the assumption that we perform an update check every hour so that |
| 661 | // the max value of 8 will roughly be equivalent to one work day. If we decide |
| 662 | // to use PollInterval permanently, we should update the |
| 663 | // max_update_checks_allowed to take PollInterval into account. Note: The |
| 664 | // parsing for PollInterval happens even before parsing of the status because |
| 665 | // we may want to specify the PollInterval even when there's no update. |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 666 | base::StringToInt(parser_data->updatecheck_poll_interval, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 667 | &output_object->poll_interval); |
| 668 | |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 669 | // Check for the "elapsed_days" attribute in the "daystart" |
| 670 | // element. This is the number of days since Jan 1 2007, 0:00 |
| 671 | // PST. If we don't have a persisted value of the Omaha InstallDate, |
| 672 | // we'll use it to calculate it and then persist it. |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 673 | if (ParseInstallDate(parser_data, output_object) && |
| 674 | !HasInstallDate(system_state_)) { |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 675 | // Since output_object->install_date_days is never negative, the |
| 676 | // elapsed_days -> install-date calculation is reduced to simply |
| 677 | // rounding down to the nearest number divisible by 7. |
| 678 | int remainder = output_object->install_date_days % 7; |
| 679 | int install_date_days_rounded = |
| 680 | output_object->install_date_days - remainder; |
| 681 | if (PersistInstallDate(system_state_, |
| 682 | install_date_days_rounded, |
| 683 | kProvisionedFromOmahaResponse)) { |
| 684 | LOG(INFO) << "Set the Omaha InstallDate from Omaha Response to " |
| 685 | << install_date_days_rounded << " days"; |
| 686 | } |
| 687 | } |
| 688 | |
Alex Deymo | 00d79ac | 2015-06-29 15:41:49 -0700 | [diff] [blame] | 689 | // We persist the cohorts sent by omaha even if the status is "noupdate". |
Sen Jiang | b1e063a | 2017-09-15 17:44:31 -0700 | [diff] [blame] | 690 | for (const auto& app : parser_data->apps) { |
| 691 | if (app.id == params_->GetAppId()) { |
| 692 | if (app.cohort_set) |
| 693 | PersistCohortData(kPrefsOmahaCohort, app.cohort); |
| 694 | if (app.cohorthint_set) |
| 695 | PersistCohortData(kPrefsOmahaCohortHint, app.cohorthint); |
| 696 | if (app.cohortname_set) |
| 697 | PersistCohortData(kPrefsOmahaCohortName, app.cohortname); |
| 698 | break; |
| 699 | } |
| 700 | } |
Alex Deymo | 00d79ac | 2015-06-29 15:41:49 -0700 | [diff] [blame] | 701 | |
Amin Hassani | 2cbb069 | 2019-10-30 13:36:17 -0700 | [diff] [blame^] | 702 | PersistEolInfo(parser_data->updatecheck_attrs); |
| 703 | |
Marton Hunyady | 199152d | 2018-05-07 19:08:48 +0200 | [diff] [blame] | 704 | // Rollback-related updatecheck attributes. |
| 705 | // Defaults to false if attribute is not present. |
| 706 | output_object->is_rollback = |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 707 | ParseBool(parser_data->updatecheck_attrs[kAttrRollback]); |
Zentaro Kavanagh | 0ff621c | 2018-07-13 13:06:56 -0700 | [diff] [blame] | 708 | |
| 709 | // Parses the rollback versions of the current image. If the fields do not |
| 710 | // exist they default to 0xffff for the 4 key versions. |
Zentaro Kavanagh | 0ef9a2f | 2018-07-02 12:05:07 -0700 | [diff] [blame] | 711 | ParseRollbackVersions( |
| 712 | params_->rollback_allowed_milestones(), parser_data, output_object); |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 713 | |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 714 | if (!ParseStatus(parser_data, output_object, completer)) |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 715 | return false; |
| 716 | |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 717 | if (!ParseParams(parser_data, output_object, completer)) |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 718 | return false; |
| 719 | |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 720 | // Package has to be parsed after Params now because ParseParams need to make |
| 721 | // sure that postinstall action exists. |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 722 | for (auto& app : parser_data->apps) |
| 723 | if (!ParsePackage(&app, output_object, completer)) |
| 724 | return false; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 725 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 726 | return true; |
| 727 | } |
| 728 | |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 729 | bool OmahaRequestAction::ParseStatus(OmahaParserData* parser_data, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 730 | OmahaResponse* output_object, |
| 731 | ScopedActionCompleter* completer) { |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 732 | output_object->update_exists = false; |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 733 | for (const auto& app : parser_data->apps) { |
| 734 | const string& status = app.updatecheck_status; |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 735 | if (status == kValNoUpdate) { |
Sen Jiang | 00adf7b | 2017-06-26 15:57:29 -0700 | [diff] [blame] | 736 | // Don't update if any app has status="noupdate". |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 737 | LOG(INFO) << "No update for <app> " << app.id; |
Sen Jiang | 00adf7b | 2017-06-26 15:57:29 -0700 | [diff] [blame] | 738 | output_object->update_exists = false; |
| 739 | break; |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 740 | } else if (status == "ok") { |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 741 | auto const& attr_no_update = |
| 742 | app.action_postinstall_attrs.find(kAttrNoUpdate); |
| 743 | if (attr_no_update != app.action_postinstall_attrs.end() && |
| 744 | attr_no_update->second == "true") { |
Sen Jiang | 00adf7b | 2017-06-26 15:57:29 -0700 | [diff] [blame] | 745 | // noupdate="true" in postinstall attributes means it's an update to |
| 746 | // self, only update if there's at least one app really have update. |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 747 | LOG(INFO) << "Update to self for <app> " << app.id; |
Sen Jiang | 00adf7b | 2017-06-26 15:57:29 -0700 | [diff] [blame] | 748 | } else { |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 749 | LOG(INFO) << "Update for <app> " << app.id; |
Sen Jiang | 00adf7b | 2017-06-26 15:57:29 -0700 | [diff] [blame] | 750 | output_object->update_exists = true; |
| 751 | } |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 752 | } else if (status.empty() && params_->is_install() && |
| 753 | params_->GetAppId() == app.id) { |
| 754 | // Skips the platform app for install operation. |
| 755 | LOG(INFO) << "No payload (and ignore) for <app> " << app.id; |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 756 | } else { |
| 757 | LOG(ERROR) << "Unknown Omaha response status: " << status; |
| 758 | completer->set_code(ErrorCode::kOmahaResponseInvalid); |
| 759 | return false; |
| 760 | } |
| 761 | } |
| 762 | if (!output_object->update_exists) { |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 763 | SetOutputObject(*output_object); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 764 | completer->set_code(ErrorCode::kSuccess); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 765 | } |
| 766 | |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 767 | return output_object->update_exists; |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 768 | } |
| 769 | |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 770 | bool OmahaRequestAction::ParseParams(OmahaParserData* parser_data, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 771 | OmahaResponse* output_object, |
| 772 | ScopedActionCompleter* completer) { |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 773 | map<string, string> attrs; |
| 774 | for (auto& app : parser_data->apps) { |
Aaron Wood | 7dcdedf | 2017-09-06 17:17:41 -0700 | [diff] [blame] | 775 | if (app.id == params_->GetAppId()) { |
| 776 | // this is the app (potentially the only app) |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 777 | output_object->version = app.manifest_version; |
Aaron Wood | 7dcdedf | 2017-09-06 17:17:41 -0700 | [diff] [blame] | 778 | } else if (!params_->system_app_id().empty() && |
| 779 | app.id == params_->system_app_id()) { |
| 780 | // this is the system app (this check is intentionally skipped if there is |
| 781 | // no system_app_id set) |
| 782 | output_object->system_version = app.manifest_version; |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 783 | } else if (params_->is_install() && |
| 784 | app.manifest_version != params_->app_version()) { |
| 785 | LOG(WARNING) << "An app has a different version (" << app.manifest_version |
| 786 | << ") that is different than platform app version (" |
| 787 | << params_->app_version() << ")"; |
Aaron Wood | 7dcdedf | 2017-09-06 17:17:41 -0700 | [diff] [blame] | 788 | } |
| 789 | if (!app.action_postinstall_attrs.empty() && attrs.empty()) { |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 790 | attrs = app.action_postinstall_attrs; |
Aaron Wood | 7dcdedf | 2017-09-06 17:17:41 -0700 | [diff] [blame] | 791 | } |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 792 | } |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 793 | if (params_->is_install()) { |
| 794 | LOG(INFO) << "Use request version for Install operation."; |
| 795 | output_object->version = params_->app_version(); |
| 796 | } |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 797 | if (output_object->version.empty()) { |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 798 | LOG(ERROR) << "Omaha Response does not have version in manifest!"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 799 | completer->set_code(ErrorCode::kOmahaResponseInvalid); |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 800 | return false; |
| 801 | } |
| 802 | |
| 803 | LOG(INFO) << "Received omaha response to update to version " |
| 804 | << output_object->version; |
| 805 | |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 806 | if (attrs.empty()) { |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 807 | LOG(ERROR) << "Omaha Response has no postinstall event action"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 808 | completer->set_code(ErrorCode::kOmahaResponseInvalid); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 809 | return false; |
| 810 | } |
| 811 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 812 | // Get the optional properties one by one. |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 813 | output_object->more_info_url = attrs[kAttrMoreInfo]; |
| 814 | output_object->prompt = ParseBool(attrs[kAttrPrompt]); |
| 815 | output_object->deadline = attrs[kAttrDeadline]; |
| 816 | output_object->max_days_to_scatter = ParseInt(attrs[kAttrMaxDaysToScatter]); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 817 | output_object->disable_p2p_for_downloading = |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 818 | ParseBool(attrs[kAttrDisableP2PForDownloading]); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 819 | output_object->disable_p2p_for_sharing = |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 820 | ParseBool(attrs[kAttrDisableP2PForSharing]); |
| 821 | output_object->public_key_rsa = attrs[kAttrPublicKeyRsa]; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 822 | |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 823 | string max = attrs[kAttrMaxFailureCountPerUrl]; |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 824 | if (!base::StringToUint(max, &output_object->max_failure_count_per_url)) |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 825 | output_object->max_failure_count_per_url = kDefaultMaxFailureCountPerUrl; |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 826 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 827 | output_object->disable_payload_backoff = |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 828 | ParseBool(attrs[kAttrDisablePayloadBackoff]); |
| 829 | output_object->powerwash_required = ParseBool(attrs[kAttrPowerwash]); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 830 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 831 | return true; |
| 832 | } |
| 833 | |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 834 | // If the transfer was successful, this uses expat to parse the response |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 835 | // and fill in the appropriate fields of the output object. Also, notifies |
| 836 | // the processor that we're done. |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 837 | void OmahaRequestAction::TransferComplete(HttpFetcher* fetcher, |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 838 | bool successful) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 839 | ScopedActionCompleter completer(processor_, this); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 840 | string current_response(response_buffer_.begin(), response_buffer_.end()); |
| 841 | LOG(INFO) << "Omaha request response: " << current_response; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 842 | |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 843 | PayloadStateInterface* const payload_state = system_state_->payload_state(); |
| 844 | |
Zentaro Kavanagh | 76af266 | 2018-05-15 10:54:53 -0700 | [diff] [blame] | 845 | // Set the max kernel key version based on whether rollback is allowed. |
| 846 | SetMaxKernelKeyVersionForRollback(); |
| 847 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 848 | // Events are best effort transactions -- assume they always succeed. |
| 849 | if (IsEvent()) { |
| 850 | CHECK(!HasOutputPipe()) << "No output pipe allowed for event requests."; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 851 | completer.set_code(ErrorCode::kSuccess); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 852 | return; |
| 853 | } |
| 854 | |
Amin Hassani | d3d8421 | 2019-08-17 00:27:44 -0700 | [diff] [blame] | 855 | ErrorCode aux_error_code = fetcher->GetAuxiliaryErrorCode(); |
| 856 | if (aux_error_code != ErrorCode::kSuccess) { |
| 857 | metrics::DownloadErrorCode download_error_code = |
| 858 | metrics_utils::GetDownloadErrorCode(aux_error_code); |
| 859 | system_state_->metrics_reporter()->ReportUpdateCheckMetrics( |
| 860 | system_state_, |
| 861 | metrics::CheckResult::kUnset, |
| 862 | metrics::CheckReaction::kUnset, |
| 863 | download_error_code); |
| 864 | } |
| 865 | |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 866 | if (!successful) { |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 867 | LOG(ERROR) << "Omaha request network transfer failed."; |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 868 | int code = GetHTTPResponseCode(); |
| 869 | // Makes sure we send sane error values. |
| 870 | if (code < 0 || code >= 1000) { |
| 871 | code = 999; |
| 872 | } |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 873 | completer.set_code(static_cast<ErrorCode>( |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 874 | static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + code)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 875 | return; |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 876 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 877 | |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 878 | XML_Parser parser = XML_ParserCreate(nullptr); |
David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 879 | OmahaParserData parser_data(parser); |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 880 | XML_SetUserData(parser, &parser_data); |
| 881 | XML_SetElementHandler(parser, ParserHandlerStart, ParserHandlerEnd); |
David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 882 | XML_SetEntityDeclHandler(parser, ParserHandlerEntityDecl); |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 883 | XML_Status res = |
| 884 | XML_Parse(parser, |
| 885 | reinterpret_cast<const char*>(response_buffer_.data()), |
| 886 | response_buffer_.size(), |
| 887 | XML_TRUE); |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 888 | |
| 889 | if (res != XML_STATUS_OK || parser_data.failed) { |
Alex Deymo | a9bb7dc | 2015-06-19 09:50:23 -0700 | [diff] [blame] | 890 | LOG(ERROR) << "Omaha response not valid XML: " |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 891 | << XML_ErrorString(XML_GetErrorCode(parser)) << " at line " |
| 892 | << XML_GetCurrentLineNumber(parser) << " col " |
| 893 | << XML_GetCurrentColumnNumber(parser); |
Amin Hassani | 91f48b4 | 2018-08-06 17:47:14 -0700 | [diff] [blame] | 894 | XML_ParserFree(parser); |
David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 895 | ErrorCode error_code = ErrorCode::kOmahaRequestXMLParseError; |
| 896 | if (response_buffer_.empty()) { |
| 897 | error_code = ErrorCode::kOmahaRequestEmptyResponseError; |
| 898 | } else if (parser_data.entity_decl) { |
| 899 | error_code = ErrorCode::kOmahaRequestXMLHasEntityDecl; |
| 900 | } |
| 901 | completer.set_code(error_code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 902 | return; |
| 903 | } |
Amin Hassani | 91f48b4 | 2018-08-06 17:47:14 -0700 | [diff] [blame] | 904 | XML_ParserFree(parser); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 905 | |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 906 | // Update the last ping day preferences based on the server daystart response |
| 907 | // even if we didn't send a ping. Omaha always includes the daystart in the |
| 908 | // response, but log the error if it didn't. |
| 909 | LOG_IF(ERROR, !UpdateLastPingDays(&parser_data, system_state_->prefs())) |
| 910 | << "Failed to update the last ping day preferences!"; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 911 | |
Amin Hassani | 1677e81 | 2017-06-21 13:36:36 -0700 | [diff] [blame] | 912 | // Sets first_active_omaha_ping_sent to true (vpd in CrOS). We only do this if |
| 913 | // we have got a response from omaha and if its value has never been set to |
| 914 | // true before. Failure of this function should be ignored. There should be no |
| 915 | // need to check if a=-1 has been sent because older devices have already sent |
| 916 | // their a=-1 in the past and we have to set first_active_omaha_ping_sent for |
| 917 | // future checks. |
| 918 | if (!system_state_->hardware()->GetFirstActiveOmahaPingSent()) { |
Amin Hassani | 80f4d4c | 2018-05-16 13:34:00 -0700 | [diff] [blame] | 919 | if (!system_state_->hardware()->SetFirstActiveOmahaPingSent()) { |
| 920 | system_state_->metrics_reporter()->ReportInternalErrorCode( |
| 921 | ErrorCode::kFirstActiveOmahaPingSentPersistenceError); |
| 922 | } |
Amin Hassani | 1677e81 | 2017-06-21 13:36:36 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 925 | if (!HasOutputPipe()) { |
| 926 | // Just set success to whether or not the http transfer succeeded, |
| 927 | // which must be true at this point in the code. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 928 | completer.set_code(ErrorCode::kSuccess); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 929 | return; |
| 930 | } |
| 931 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 932 | OmahaResponse output_object; |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 933 | if (!ParseResponse(&parser_data, &output_object, &completer)) |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 934 | return; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 935 | output_object.update_exists = true; |
| 936 | SetOutputObject(output_object); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 937 | |
Toni Barzic | 61544e6 | 2018-10-11 14:37:30 -0700 | [diff] [blame] | 938 | LoadOrPersistUpdateFirstSeenAtPref(); |
| 939 | |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 940 | ErrorCode error = ErrorCode::kSuccess; |
Toni Barzic | 61544e6 | 2018-10-11 14:37:30 -0700 | [diff] [blame] | 941 | if (ShouldIgnoreUpdate(output_object, &error)) { |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 942 | // No need to change output_object.update_exists here, since the value |
| 943 | // has been output to the pipe. |
| 944 | completer.set_code(error); |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 945 | return; |
| 946 | } |
| 947 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 948 | // If Omaha says to disable p2p, respect that |
| 949 | if (output_object.disable_p2p_for_downloading) { |
| 950 | LOG(INFO) << "Forcibly disabling use of p2p for downloading as " |
| 951 | << "requested by Omaha."; |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 952 | payload_state->SetUsingP2PForDownloading(false); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 953 | } |
| 954 | if (output_object.disable_p2p_for_sharing) { |
| 955 | LOG(INFO) << "Forcibly disabling use of p2p for sharing as " |
| 956 | << "requested by Omaha."; |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 957 | payload_state->SetUsingP2PForSharing(false); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 958 | } |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 959 | |
| 960 | // Update the payload state with the current response. The payload state |
| 961 | // will automatically reset all stale state if this response is different |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 962 | // from what's stored already. We are updating the payload state as late |
| 963 | // as possible in this method so that if a new release gets pushed and then |
| 964 | // got pulled back due to some issues, we don't want to clear our internal |
| 965 | // state unnecessarily. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 966 | payload_state->SetResponse(output_object); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 967 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 968 | // It could be we've already exceeded the deadline for when p2p is |
| 969 | // allowed or that we've tried too many times with p2p. Check that. |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 970 | if (payload_state->GetUsingP2PForDownloading()) { |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 971 | payload_state->P2PNewAttempt(); |
| 972 | if (!payload_state->P2PAttemptAllowed()) { |
| 973 | LOG(INFO) << "Forcibly disabling use of p2p for downloading because " |
| 974 | << "of previous failures when using p2p."; |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 975 | payload_state->SetUsingP2PForDownloading(false); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 976 | } |
| 977 | } |
| 978 | |
| 979 | // From here on, we'll complete stuff in CompleteProcessing() so |
| 980 | // disable |completer| since we'll create a new one in that |
| 981 | // function. |
| 982 | completer.set_should_complete(false); |
| 983 | |
| 984 | // If we're allowed to use p2p for downloading we do not pay |
| 985 | // attention to wall-clock-based waiting if the URL is indeed |
| 986 | // available via p2p. Therefore, check if the file is available via |
| 987 | // p2p before deferring... |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 988 | if (payload_state->GetUsingP2PForDownloading()) { |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 989 | LookupPayloadViaP2P(output_object); |
| 990 | } else { |
| 991 | CompleteProcessing(); |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | void OmahaRequestAction::CompleteProcessing() { |
| 996 | ScopedActionCompleter completer(processor_, this); |
| 997 | OmahaResponse& output_object = const_cast<OmahaResponse&>(GetOutputObject()); |
| 998 | PayloadStateInterface* payload_state = system_state_->payload_state(); |
| 999 | |
| 1000 | if (ShouldDeferDownload(&output_object)) { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 1001 | output_object.update_exists = false; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1002 | LOG(INFO) << "Ignoring Omaha updates as updates are deferred by policy."; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1003 | completer.set_code(ErrorCode::kOmahaUpdateDeferredPerPolicy); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 1004 | return; |
| 1005 | } |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1006 | |
Chris Sosa | 20f005c | 2013-09-05 13:53:08 -0700 | [diff] [blame] | 1007 | if (payload_state->ShouldBackoffDownload()) { |
| 1008 | output_object.update_exists = false; |
| 1009 | LOG(INFO) << "Ignoring Omaha updates in order to backoff our retry " |
| 1010 | << "attempts"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1011 | completer.set_code(ErrorCode::kOmahaUpdateDeferredForBackoff); |
Chris Sosa | 20f005c | 2013-09-05 13:53:08 -0700 | [diff] [blame] | 1012 | return; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1013 | } |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1014 | completer.set_code(ErrorCode::kSuccess); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | void OmahaRequestAction::OnLookupPayloadViaP2PCompleted(const string& url) { |
| 1018 | LOG(INFO) << "Lookup complete, p2p-client returned URL '" << url << "'"; |
| 1019 | if (!url.empty()) { |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1020 | system_state_->payload_state()->SetP2PUrl(url); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1021 | } else { |
| 1022 | LOG(INFO) << "Forcibly disabling use of p2p for downloading " |
| 1023 | << "because no suitable peer could be found."; |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1024 | system_state_->payload_state()->SetUsingP2PForDownloading(false); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1025 | } |
| 1026 | CompleteProcessing(); |
| 1027 | } |
| 1028 | |
| 1029 | void OmahaRequestAction::LookupPayloadViaP2P(const OmahaResponse& response) { |
David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1030 | // If the device is in the middle of an update, the state variables |
| 1031 | // kPrefsUpdateStateNextDataOffset, kPrefsUpdateStateNextDataLength |
| 1032 | // tracks the offset and length of the operation currently in |
| 1033 | // progress. The offset is based from the end of the manifest which |
| 1034 | // is kPrefsManifestMetadataSize bytes long. |
| 1035 | // |
| 1036 | // To make forward progress and avoid deadlocks, we need to find a |
| 1037 | // peer that has at least the entire operation we're currently |
| 1038 | // working on. Otherwise we may end up in a situation where two |
| 1039 | // devices bounce back and forth downloading from each other, |
| 1040 | // neither making any forward progress until one of them decides to |
| 1041 | // stop using p2p (via kMaxP2PAttempts and kMaxP2PAttemptTimeSeconds |
| 1042 | // safe-guards). See http://crbug.com/297170 for an example) |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1043 | size_t minimum_size = 0; |
David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1044 | int64_t manifest_metadata_size = 0; |
Alex Deymo | f25eb49 | 2016-02-26 00:20:08 -0800 | [diff] [blame] | 1045 | int64_t manifest_signature_size = 0; |
David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1046 | int64_t next_data_offset = 0; |
| 1047 | int64_t next_data_length = 0; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1048 | if (system_state_ && |
David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1049 | system_state_->prefs()->GetInt64(kPrefsManifestMetadataSize, |
| 1050 | &manifest_metadata_size) && |
| 1051 | manifest_metadata_size != -1 && |
Alex Deymo | f25eb49 | 2016-02-26 00:20:08 -0800 | [diff] [blame] | 1052 | system_state_->prefs()->GetInt64(kPrefsManifestSignatureSize, |
| 1053 | &manifest_signature_size) && |
| 1054 | manifest_signature_size != -1 && |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1055 | system_state_->prefs()->GetInt64(kPrefsUpdateStateNextDataOffset, |
David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1056 | &next_data_offset) && |
| 1057 | next_data_offset != -1 && |
| 1058 | system_state_->prefs()->GetInt64(kPrefsUpdateStateNextDataLength, |
| 1059 | &next_data_length)) { |
Alex Deymo | f25eb49 | 2016-02-26 00:20:08 -0800 | [diff] [blame] | 1060 | minimum_size = manifest_metadata_size + manifest_signature_size + |
| 1061 | next_data_offset + next_data_length; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1062 | } |
| 1063 | |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 1064 | // TODO(senj): Fix P2P for multiple package. |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 1065 | brillo::Blob raw_hash; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 1066 | if (!base::HexStringToBytes(response.packages[0].hash, &raw_hash)) |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 1067 | return; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 1068 | string file_id = |
| 1069 | utils::CalculateP2PFileId(raw_hash, response.packages[0].size); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1070 | if (system_state_->p2p_manager()) { |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 1071 | LOG(INFO) << "Checking if payload is available via p2p, file_id=" << file_id |
| 1072 | << " minimum_size=" << minimum_size; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1073 | system_state_->p2p_manager()->LookupUrlForFile( |
| 1074 | file_id, |
| 1075 | minimum_size, |
David Zeuthen | 4cc5ed2 | 2014-01-15 12:35:03 -0800 | [diff] [blame] | 1076 | TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds), |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1077 | base::Bind(&OmahaRequestAction::OnLookupPayloadViaP2PCompleted, |
| 1078 | base::Unretained(this))); |
| 1079 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1082 | bool OmahaRequestAction::ShouldDeferDownload(OmahaResponse* output_object) { |
Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 1083 | if (params_->interactive()) { |
| 1084 | LOG(INFO) << "Not deferring download because update is interactive."; |
| 1085 | return false; |
| 1086 | } |
| 1087 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1088 | // If we're using p2p to download _and_ we have a p2p URL, we never |
| 1089 | // defer the download. This is because the download will always |
| 1090 | // happen from a peer on the LAN and we've been waiting in line for |
| 1091 | // our turn. |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1092 | const PayloadStateInterface* payload_state = system_state_->payload_state(); |
| 1093 | if (payload_state->GetUsingP2PForDownloading() && |
| 1094 | !payload_state->GetP2PUrl().empty()) { |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1095 | LOG(INFO) << "Download not deferred because download " |
| 1096 | << "will happen from a local peer (via p2p)."; |
| 1097 | return false; |
| 1098 | } |
| 1099 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1100 | // We should defer the downloads only if we've first satisfied the |
| 1101 | // wall-clock-based-waiting period and then the update-check-based waiting |
| 1102 | // period, if required. |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1103 | if (!params_->wall_clock_based_wait_enabled()) { |
Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 1104 | LOG(INFO) << "Wall-clock-based waiting period is not enabled," |
| 1105 | << " so no deferring needed."; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1106 | return false; |
| 1107 | } |
| 1108 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1109 | switch (IsWallClockBasedWaitingSatisfied(output_object)) { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1110 | case kWallClockWaitNotSatisfied: |
| 1111 | // We haven't even satisfied the first condition, passing the |
| 1112 | // wall-clock-based waiting period, so we should defer the downloads |
| 1113 | // until that happens. |
| 1114 | LOG(INFO) << "wall-clock-based-wait not satisfied."; |
| 1115 | return true; |
| 1116 | |
| 1117 | case kWallClockWaitDoneButUpdateCheckWaitRequired: |
| 1118 | LOG(INFO) << "wall-clock-based-wait satisfied and " |
| 1119 | << "update-check-based-wait required."; |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1120 | return !IsUpdateCheckCountBasedWaitingSatisfied(); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1121 | |
| 1122 | case kWallClockWaitDoneAndUpdateCheckWaitNotRequired: |
| 1123 | // Wall-clock-based waiting period is satisfied, and it's determined |
| 1124 | // that we do not need the update-check-based wait. so no need to |
| 1125 | // defer downloads. |
| 1126 | LOG(INFO) << "wall-clock-based-wait satisfied and " |
| 1127 | << "update-check-based-wait is not required."; |
| 1128 | return false; |
| 1129 | |
| 1130 | default: |
| 1131 | // Returning false for this default case so we err on the |
| 1132 | // side of downloading updates than deferring in case of any bugs. |
| 1133 | NOTREACHED(); |
| 1134 | return false; |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | OmahaRequestAction::WallClockWaitResult |
| 1139 | OmahaRequestAction::IsWallClockBasedWaitingSatisfied( |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1140 | OmahaResponse* output_object) { |
May Lippert | 60aa3ca | 2018-08-15 16:55:29 -0700 | [diff] [blame] | 1141 | Time update_first_seen_at = LoadOrPersistUpdateFirstSeenAtPref(); |
| 1142 | if (update_first_seen_at == base::Time()) { |
| 1143 | LOG(INFO) << "Not scattering as UpdateFirstSeenAt value cannot be read or " |
| 1144 | "persisted"; |
| 1145 | return kWallClockWaitDoneAndUpdateCheckWaitNotRequired; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1146 | } |
| 1147 | |
Sen Jiang | 7c1171e | 2016-06-23 11:35:40 -0700 | [diff] [blame] | 1148 | TimeDelta elapsed_time = |
| 1149 | system_state_->clock()->GetWallclockTime() - update_first_seen_at; |
| 1150 | TimeDelta max_scatter_period = |
| 1151 | TimeDelta::FromDays(output_object->max_days_to_scatter); |
Adolfo Victoria | 497044c | 2018-07-18 07:51:42 -0700 | [diff] [blame] | 1152 | int64_t staging_wait_time_in_days = 0; |
| 1153 | // Use staging and its default max value if staging is on. |
| 1154 | if (system_state_->prefs()->GetInt64(kPrefsWallClockStagingWaitPeriod, |
| 1155 | &staging_wait_time_in_days) && |
| 1156 | staging_wait_time_in_days > 0) |
| 1157 | max_scatter_period = TimeDelta::FromDays(kMaxWaitTimeStagingInDays); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1158 | |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1159 | LOG(INFO) << "Waiting Period = " |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1160 | << utils::FormatSecs(params_->waiting_period().InSeconds()) |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1161 | << ", Time Elapsed = " |
| 1162 | << utils::FormatSecs(elapsed_time.InSeconds()) |
Adolfo Victoria | 497044c | 2018-07-18 07:51:42 -0700 | [diff] [blame] | 1163 | << ", MaxDaysToScatter = " << max_scatter_period.InDays(); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1164 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1165 | if (!output_object->deadline.empty()) { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1166 | // The deadline is set for all rules which serve a delta update from a |
| 1167 | // previous FSI, which means this update will be applied mostly in OOBE |
| 1168 | // cases. For these cases, we shouldn't scatter so as to finish the OOBE |
| 1169 | // quickly. |
| 1170 | LOG(INFO) << "Not scattering as deadline flag is set"; |
| 1171 | return kWallClockWaitDoneAndUpdateCheckWaitNotRequired; |
| 1172 | } |
| 1173 | |
| 1174 | if (max_scatter_period.InDays() == 0) { |
| 1175 | // This means the Omaha rule creator decides that this rule |
| 1176 | // should not be scattered irrespective of the policy. |
| 1177 | LOG(INFO) << "Not scattering as MaxDaysToScatter in rule is 0."; |
| 1178 | return kWallClockWaitDoneAndUpdateCheckWaitNotRequired; |
| 1179 | } |
| 1180 | |
| 1181 | if (elapsed_time > max_scatter_period) { |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1182 | // This means we've waited more than the upperbound wait in the rule |
| 1183 | // from the time we first saw a valid update available to us. |
| 1184 | // This will prevent update starvation. |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1185 | LOG(INFO) << "Not scattering as we're past the MaxDaysToScatter limit."; |
| 1186 | return kWallClockWaitDoneAndUpdateCheckWaitNotRequired; |
| 1187 | } |
| 1188 | |
| 1189 | // This means we are required to participate in scattering. |
| 1190 | // See if our turn has arrived now. |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1191 | TimeDelta remaining_wait_time = params_->waiting_period() - elapsed_time; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1192 | if (remaining_wait_time.InSeconds() <= 0) { |
| 1193 | // Yes, it's our turn now. |
| 1194 | LOG(INFO) << "Successfully passed the wall-clock-based-wait."; |
| 1195 | |
| 1196 | // But we can't download until the update-check-count-based wait is also |
| 1197 | // satisfied, so mark it as required now if update checks are enabled. |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1198 | return params_->update_check_count_wait_enabled() |
| 1199 | ? kWallClockWaitDoneButUpdateCheckWaitRequired |
| 1200 | : kWallClockWaitDoneAndUpdateCheckWaitNotRequired; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | // Not our turn yet, so we have to wait until our turn to |
| 1204 | // help scatter the downloads across all clients of the enterprise. |
| 1205 | LOG(INFO) << "Update deferred for another " |
| 1206 | << utils::FormatSecs(remaining_wait_time.InSeconds()) |
| 1207 | << " per policy."; |
| 1208 | return kWallClockWaitNotSatisfied; |
| 1209 | } |
| 1210 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1211 | bool OmahaRequestAction::IsUpdateCheckCountBasedWaitingSatisfied() { |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 1212 | int64_t update_check_count_value; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1213 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1214 | if (system_state_->prefs()->Exists(kPrefsUpdateCheckCount)) { |
| 1215 | if (!system_state_->prefs()->GetInt64(kPrefsUpdateCheckCount, |
| 1216 | &update_check_count_value)) { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1217 | // We are unable to read the update check count from file for some reason. |
| 1218 | // So let's proceed anyway so as to not stall the update. |
| 1219 | LOG(ERROR) << "Unable to read update check count. " |
| 1220 | << "Skipping update-check-count-based-wait."; |
| 1221 | return true; |
| 1222 | } |
| 1223 | } else { |
| 1224 | // This file does not exist. This means we haven't started our update |
| 1225 | // check count down yet, so this is the right time to start the count down. |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1226 | update_check_count_value = |
| 1227 | base::RandInt(params_->min_update_checks_needed(), |
| 1228 | params_->max_update_checks_allowed()); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1229 | |
| 1230 | LOG(INFO) << "Randomly picked update check count value = " |
| 1231 | << update_check_count_value; |
| 1232 | |
| 1233 | // Write out the initial value of update_check_count_value. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1234 | if (!system_state_->prefs()->SetInt64(kPrefsUpdateCheckCount, |
| 1235 | update_check_count_value)) { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1236 | // We weren't able to write the update check count file for some reason. |
| 1237 | // So let's proceed anyway so as to not stall the update. |
| 1238 | LOG(ERROR) << "Unable to write update check count. " |
| 1239 | << "Skipping update-check-count-based-wait."; |
| 1240 | return true; |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | if (update_check_count_value == 0) { |
| 1245 | LOG(INFO) << "Successfully passed the update-check-based-wait."; |
| 1246 | return true; |
| 1247 | } |
| 1248 | |
| 1249 | if (update_check_count_value < 0 || |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1250 | update_check_count_value > params_->max_update_checks_allowed()) { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1251 | // We err on the side of skipping scattering logic instead of stalling |
| 1252 | // a machine from receiving any updates in case of any unexpected state. |
| 1253 | LOG(ERROR) << "Invalid value for update check count detected. " |
| 1254 | << "Skipping update-check-count-based-wait."; |
| 1255 | return true; |
| 1256 | } |
| 1257 | |
| 1258 | // Legal value, we need to wait for more update checks to happen |
| 1259 | // until this becomes 0. |
| 1260 | LOG(INFO) << "Deferring Omaha updates for another " |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1261 | << update_check_count_value << " update checks per policy"; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1262 | return false; |
| 1263 | } |
| 1264 | |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1265 | // static |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 1266 | bool OmahaRequestAction::ParseInstallDate(OmahaParserData* parser_data, |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1267 | OmahaResponse* output_object) { |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1268 | int64_t elapsed_days = 0; |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1269 | if (!base::StringToInt64(parser_data->daystart_elapsed_days, &elapsed_days)) |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1270 | return false; |
| 1271 | |
| 1272 | if (elapsed_days < 0) |
| 1273 | return false; |
| 1274 | |
| 1275 | output_object->install_date_days = elapsed_days; |
| 1276 | return true; |
| 1277 | } |
| 1278 | |
| 1279 | // static |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1280 | bool OmahaRequestAction::HasInstallDate(SystemState* system_state) { |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1281 | PrefsInterface* prefs = system_state->prefs(); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1282 | if (prefs == nullptr) |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1283 | return false; |
| 1284 | |
| 1285 | return prefs->Exists(kPrefsInstallDateDays); |
| 1286 | } |
| 1287 | |
| 1288 | // static |
| 1289 | bool OmahaRequestAction::PersistInstallDate( |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1290 | SystemState* system_state, |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1291 | int install_date_days, |
| 1292 | InstallDateProvisioningSource source) { |
| 1293 | TEST_AND_RETURN_FALSE(install_date_days >= 0); |
| 1294 | |
| 1295 | PrefsInterface* prefs = system_state->prefs(); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1296 | if (prefs == nullptr) |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1297 | return false; |
| 1298 | |
| 1299 | if (!prefs->SetInt64(kPrefsInstallDateDays, install_date_days)) |
| 1300 | return false; |
| 1301 | |
Tianjie Xu | 282aa1f | 2017-09-05 13:42:45 -0700 | [diff] [blame] | 1302 | system_state->metrics_reporter()->ReportInstallDateProvisioningSource( |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1303 | static_cast<int>(source), // Sample. |
| 1304 | kProvisionedMax); // Maximum. |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1305 | return true; |
| 1306 | } |
| 1307 | |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1308 | bool OmahaRequestAction::PersistCohortData(const string& prefs_key, |
| 1309 | const string& new_value) { |
Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 1310 | if (new_value.empty() && system_state_->prefs()->Exists(prefs_key)) { |
| 1311 | LOG(INFO) << "Removing stored " << prefs_key << " value."; |
| 1312 | return system_state_->prefs()->Delete(prefs_key); |
| 1313 | } else if (!new_value.empty()) { |
| 1314 | LOG(INFO) << "Storing new setting " << prefs_key << " as " << new_value; |
| 1315 | return system_state_->prefs()->SetString(prefs_key, new_value); |
| 1316 | } |
| 1317 | return true; |
| 1318 | } |
| 1319 | |
Amin Hassani | 2cbb069 | 2019-10-30 13:36:17 -0700 | [diff] [blame^] | 1320 | bool OmahaRequestAction::PersistEolInfo(const map<string, string>& attrs) { |
Jae Hoon Kim | 051627a | 2019-09-03 12:56:32 -0700 | [diff] [blame] | 1321 | auto eol_date_attr = attrs.find(kAttrEolDate); |
| 1322 | if (eol_date_attr == attrs.end()) { |
| 1323 | system_state_->prefs()->Delete(kPrefsOmahaEolDate); |
| 1324 | } else if (!system_state_->prefs()->SetString(kPrefsOmahaEolDate, |
| 1325 | eol_date_attr->second)) { |
| 1326 | LOG(ERROR) << "Setting EOL date failed."; |
Amin Hassani | 2cbb069 | 2019-10-30 13:36:17 -0700 | [diff] [blame^] | 1327 | return false; |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 1328 | } |
Amin Hassani | 2cbb069 | 2019-10-30 13:36:17 -0700 | [diff] [blame^] | 1329 | return true; |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 1330 | } |
| 1331 | |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1332 | void OmahaRequestAction::ActionCompleted(ErrorCode code) { |
| 1333 | // We only want to report this on "update check". |
| 1334 | if (ping_only_ || event_ != nullptr) |
| 1335 | return; |
| 1336 | |
| 1337 | metrics::CheckResult result = metrics::CheckResult::kUnset; |
| 1338 | metrics::CheckReaction reaction = metrics::CheckReaction::kUnset; |
| 1339 | metrics::DownloadErrorCode download_error_code = |
| 1340 | metrics::DownloadErrorCode::kUnset; |
| 1341 | |
| 1342 | // Regular update attempt. |
| 1343 | switch (code) { |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1344 | case ErrorCode::kSuccess: |
| 1345 | // OK, we parsed the response successfully but that does |
| 1346 | // necessarily mean that an update is available. |
| 1347 | if (HasOutputPipe()) { |
| 1348 | const OmahaResponse& response = GetOutputObject(); |
| 1349 | if (response.update_exists) { |
| 1350 | result = metrics::CheckResult::kUpdateAvailable; |
| 1351 | reaction = metrics::CheckReaction::kUpdating; |
| 1352 | } else { |
| 1353 | result = metrics::CheckResult::kNoUpdateAvailable; |
| 1354 | } |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1355 | } else { |
| 1356 | result = metrics::CheckResult::kNoUpdateAvailable; |
| 1357 | } |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1358 | break; |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1359 | |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1360 | case ErrorCode::kOmahaUpdateIgnoredPerPolicy: |
| 1361 | case ErrorCode::kOmahaUpdateIgnoredOverCellular: |
| 1362 | result = metrics::CheckResult::kUpdateAvailable; |
| 1363 | reaction = metrics::CheckReaction::kIgnored; |
| 1364 | break; |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1365 | |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1366 | case ErrorCode::kOmahaUpdateDeferredPerPolicy: |
| 1367 | result = metrics::CheckResult::kUpdateAvailable; |
| 1368 | reaction = metrics::CheckReaction::kDeferring; |
| 1369 | break; |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1370 | |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1371 | case ErrorCode::kOmahaUpdateDeferredForBackoff: |
| 1372 | result = metrics::CheckResult::kUpdateAvailable; |
| 1373 | reaction = metrics::CheckReaction::kBackingOff; |
| 1374 | break; |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1375 | |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1376 | default: |
| 1377 | // We report two flavors of errors, "Download errors" and "Parsing |
| 1378 | // error". Try to convert to the former and if that doesn't work |
| 1379 | // we know it's the latter. |
| 1380 | metrics::DownloadErrorCode tmp_error = |
| 1381 | metrics_utils::GetDownloadErrorCode(code); |
| 1382 | if (tmp_error != metrics::DownloadErrorCode::kInputMalformed) { |
| 1383 | result = metrics::CheckResult::kDownloadError; |
| 1384 | download_error_code = tmp_error; |
| 1385 | } else { |
| 1386 | result = metrics::CheckResult::kParsingError; |
| 1387 | } |
| 1388 | break; |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1389 | } |
| 1390 | |
Amin Hassani | d3d8421 | 2019-08-17 00:27:44 -0700 | [diff] [blame] | 1391 | system_state_->metrics_reporter()->ReportUpdateCheckMetrics( |
| 1392 | system_state_, result, reaction, download_error_code); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1393 | } |
| 1394 | |
Toni Barzic | 61544e6 | 2018-10-11 14:37:30 -0700 | [diff] [blame] | 1395 | bool OmahaRequestAction::ShouldIgnoreUpdate(const OmahaResponse& response, |
| 1396 | ErrorCode* error) const { |
Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 1397 | // Note: policy decision to not update to a version we rolled back from. |
| 1398 | string rollback_version = |
| 1399 | system_state_->payload_state()->GetRollbackVersion(); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1400 | if (!rollback_version.empty()) { |
Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 1401 | LOG(INFO) << "Detected previous rollback from version " << rollback_version; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1402 | if (rollback_version == response.version) { |
Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 1403 | LOG(INFO) << "Received version that we rolled back from. Ignoring."; |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 1404 | *error = ErrorCode::kOmahaUpdateIgnoredPerPolicy; |
Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 1405 | return true; |
| 1406 | } |
| 1407 | } |
| 1408 | |
Toni Barzic | 61544e6 | 2018-10-11 14:37:30 -0700 | [diff] [blame] | 1409 | if (system_state_->hardware()->IsOOBEEnabled() && |
| 1410 | !system_state_->hardware()->IsOOBEComplete(nullptr) && |
| 1411 | (response.deadline.empty() || |
| 1412 | system_state_->payload_state()->GetRollbackHappened()) && |
| 1413 | params_->app_version() != "ForcedUpdate") { |
| 1414 | LOG(INFO) << "Ignoring a non-critical Omaha update before OOBE completion."; |
| 1415 | *error = ErrorCode::kNonCriticalUpdateInOOBE; |
| 1416 | return true; |
| 1417 | } |
| 1418 | |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 1419 | if (!IsUpdateAllowedOverCurrentConnection(error, response)) { |
Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 1420 | LOG(INFO) << "Update is not allowed over current connection."; |
| 1421 | return true; |
| 1422 | } |
| 1423 | |
| 1424 | // Note: We could technically delete the UpdateFirstSeenAt state when we |
| 1425 | // return true. If we do, it'll mean a device has to restart the |
| 1426 | // UpdateFirstSeenAt and thus help scattering take effect when the AU is |
| 1427 | // turned on again. On the other hand, it also increases the chance of update |
| 1428 | // starvation if an admin turns AU on/off more frequently. We choose to err on |
| 1429 | // the side of preventing starvation at the cost of not applying scattering in |
| 1430 | // those cases. |
| 1431 | return false; |
| 1432 | } |
| 1433 | |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 1434 | bool OmahaRequestAction::IsUpdateAllowedOverCellularByPrefs( |
| 1435 | const OmahaResponse& response) const { |
| 1436 | PrefsInterface* prefs = system_state_->prefs(); |
| 1437 | |
| 1438 | if (!prefs) { |
| 1439 | LOG(INFO) << "Disabling updates over cellular as the preferences are " |
| 1440 | "not available."; |
| 1441 | return false; |
| 1442 | } |
| 1443 | |
| 1444 | bool is_allowed; |
| 1445 | |
| 1446 | if (prefs->Exists(kPrefsUpdateOverCellularPermission) && |
| 1447 | prefs->GetBoolean(kPrefsUpdateOverCellularPermission, &is_allowed) && |
| 1448 | is_allowed) { |
| 1449 | LOG(INFO) << "Allowing updates over cellular as permission preference is " |
| 1450 | "set to true."; |
| 1451 | return true; |
| 1452 | } |
| 1453 | |
| 1454 | if (!prefs->Exists(kPrefsUpdateOverCellularTargetVersion) || |
| 1455 | !prefs->Exists(kPrefsUpdateOverCellularTargetSize)) { |
| 1456 | LOG(INFO) << "Disabling updates over cellular as permission preference is " |
| 1457 | "set to false or does not exist while target does not exist."; |
| 1458 | return false; |
| 1459 | } |
| 1460 | |
| 1461 | std::string target_version; |
| 1462 | int64_t target_size; |
| 1463 | |
| 1464 | if (!prefs->GetString(kPrefsUpdateOverCellularTargetVersion, |
| 1465 | &target_version) || |
| 1466 | !prefs->GetInt64(kPrefsUpdateOverCellularTargetSize, &target_size)) { |
| 1467 | LOG(INFO) << "Disabling updates over cellular as the target version or " |
| 1468 | "size is not accessible."; |
| 1469 | return false; |
| 1470 | } |
| 1471 | |
| 1472 | uint64_t total_packages_size = 0; |
| 1473 | for (const auto& package : response.packages) { |
| 1474 | total_packages_size += package.size; |
| 1475 | } |
| 1476 | if (target_version == response.version && |
| 1477 | static_cast<uint64_t>(target_size) == total_packages_size) { |
| 1478 | LOG(INFO) << "Allowing updates over cellular as the target matches the" |
| 1479 | "omaha response."; |
| 1480 | return true; |
| 1481 | } else { |
| 1482 | LOG(INFO) << "Disabling updates over cellular as the target does not" |
| 1483 | "match the omaha response."; |
| 1484 | return false; |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | bool OmahaRequestAction::IsUpdateAllowedOverCurrentConnection( |
| 1489 | ErrorCode* error, const OmahaResponse& response) const { |
Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 1490 | ConnectionType type; |
| 1491 | ConnectionTethering tethering; |
Alex Deymo | f6ee016 | 2015-07-31 12:35:22 -0700 | [diff] [blame] | 1492 | ConnectionManagerInterface* connection_manager = |
| 1493 | system_state_->connection_manager(); |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 1494 | if (!connection_manager->GetConnectionProperties(&type, &tethering)) { |
Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 1495 | LOG(INFO) << "We could not determine our connection type. " |
| 1496 | << "Defaulting to allow updates."; |
| 1497 | return true; |
| 1498 | } |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 1499 | |
Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 1500 | bool is_allowed = connection_manager->IsUpdateAllowedOver(type, tethering); |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 1501 | bool is_device_policy_set = |
| 1502 | connection_manager->IsAllowedConnectionTypesForUpdateSet(); |
| 1503 | // Treats tethered connection as if it is cellular connection. |
| 1504 | bool is_over_cellular = type == ConnectionType::kCellular || |
| 1505 | tethering == ConnectionTethering::kConfirmed; |
| 1506 | |
| 1507 | if (!is_over_cellular) { |
| 1508 | // There's no need to further check user preferences as we are not over |
| 1509 | // cellular connection. |
| 1510 | if (!is_allowed) |
| 1511 | *error = ErrorCode::kOmahaUpdateIgnoredPerPolicy; |
| 1512 | } else if (is_device_policy_set) { |
| 1513 | // There's no need to further check user preferences as the device policy |
| 1514 | // is set regarding updates over cellular. |
| 1515 | if (!is_allowed) |
| 1516 | *error = ErrorCode::kOmahaUpdateIgnoredPerPolicy; |
| 1517 | } else { |
| 1518 | // Deivce policy is not set, so user preferences overwrite whether to |
| 1519 | // allow updates over cellular. |
| 1520 | is_allowed = IsUpdateAllowedOverCellularByPrefs(response); |
| 1521 | if (!is_allowed) |
| 1522 | *error = ErrorCode::kOmahaUpdateIgnoredOverCellular; |
| 1523 | } |
| 1524 | |
Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 1525 | LOG(INFO) << "We are connected via " |
Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 1526 | << connection_utils::StringForConnectionType(type) |
Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 1527 | << ", Updates allowed: " << (is_allowed ? "Yes" : "No"); |
| 1528 | return is_allowed; |
| 1529 | } |
| 1530 | |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 1531 | bool OmahaRequestAction::IsRollbackEnabled() const { |
| 1532 | if (policy_provider_->IsConsumerDevice()) { |
| 1533 | LOG(INFO) << "Rollback is not enabled for consumer devices."; |
| 1534 | return false; |
| 1535 | } |
| 1536 | |
| 1537 | if (!policy_provider_->device_policy_is_loaded()) { |
| 1538 | LOG(INFO) << "No device policy is loaded. Assuming rollback enabled."; |
| 1539 | return true; |
| 1540 | } |
| 1541 | |
| 1542 | int allowed_milestones; |
| 1543 | if (!policy_provider_->GetDevicePolicy().GetRollbackAllowedMilestones( |
| 1544 | &allowed_milestones)) { |
| 1545 | LOG(INFO) << "RollbackAllowedMilestones policy can't be read. " |
| 1546 | "Defaulting to rollback enabled."; |
| 1547 | return true; |
| 1548 | } |
| 1549 | |
| 1550 | LOG(INFO) << "Rollback allows " << allowed_milestones << " milestones."; |
| 1551 | return allowed_milestones > 0; |
| 1552 | } |
| 1553 | |
| 1554 | void OmahaRequestAction::SetMaxKernelKeyVersionForRollback() const { |
Marton Hunyady | ffbfdfb | 2018-05-30 13:03:29 +0200 | [diff] [blame] | 1555 | int max_kernel_rollforward; |
| 1556 | int min_kernel_version = system_state_->hardware()->GetMinKernelKeyVersion(); |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 1557 | if (IsRollbackEnabled()) { |
| 1558 | // If rollback is enabled, set the max kernel key version to the current |
| 1559 | // kernel key version. This has the effect of freezing kernel key roll |
| 1560 | // forwards. |
| 1561 | // |
| 1562 | // TODO(zentaro): This behavior is temporary, and ensures that no kernel |
| 1563 | // key roll forward happens until the server side components of rollback |
| 1564 | // are implemented. Future changes will allow the Omaha server to return |
| 1565 | // the kernel key version from max_rollback_versions in the past. At that |
| 1566 | // point the max kernel key version will be set to that value, creating a |
| 1567 | // sliding window of versions that can be rolled back to. |
Zentaro Kavanagh | 5d95615 | 2018-05-15 09:40:33 -0700 | [diff] [blame] | 1568 | LOG(INFO) << "Rollback is enabled. Setting kernel_max_rollforward to " |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 1569 | << min_kernel_version; |
Marton Hunyady | ffbfdfb | 2018-05-30 13:03:29 +0200 | [diff] [blame] | 1570 | max_kernel_rollforward = min_kernel_version; |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 1571 | } else { |
| 1572 | // For devices that are not rollback enabled (ie. consumer devices), the |
| 1573 | // max kernel key version is set to 0xfffffffe, which is logically |
| 1574 | // infinity. This maintains the previous behavior that that kernel key |
| 1575 | // versions roll forward each time they are incremented. |
Zentaro Kavanagh | 5d95615 | 2018-05-15 09:40:33 -0700 | [diff] [blame] | 1576 | LOG(INFO) << "Rollback is disabled. Setting kernel_max_rollforward to " |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 1577 | << kRollforwardInfinity; |
Marton Hunyady | ffbfdfb | 2018-05-30 13:03:29 +0200 | [diff] [blame] | 1578 | max_kernel_rollforward = kRollforwardInfinity; |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 1579 | } |
| 1580 | |
Marton Hunyady | ffbfdfb | 2018-05-30 13:03:29 +0200 | [diff] [blame] | 1581 | bool max_rollforward_set = |
| 1582 | system_state_->hardware()->SetMaxKernelKeyRollforward( |
| 1583 | max_kernel_rollforward); |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 1584 | if (!max_rollforward_set) { |
Zentaro Kavanagh | 5d95615 | 2018-05-15 09:40:33 -0700 | [diff] [blame] | 1585 | LOG(ERROR) << "Failed to set kernel_max_rollforward"; |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 1586 | } |
Marton Hunyady | ffbfdfb | 2018-05-30 13:03:29 +0200 | [diff] [blame] | 1587 | // Report metrics |
| 1588 | system_state_->metrics_reporter()->ReportKeyVersionMetrics( |
| 1589 | min_kernel_version, max_kernel_rollforward, max_rollforward_set); |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 1590 | } |
| 1591 | |
May Lippert | 60aa3ca | 2018-08-15 16:55:29 -0700 | [diff] [blame] | 1592 | base::Time OmahaRequestAction::LoadOrPersistUpdateFirstSeenAtPref() const { |
| 1593 | Time update_first_seen_at; |
| 1594 | int64_t update_first_seen_at_int; |
| 1595 | if (system_state_->prefs()->Exists(kPrefsUpdateFirstSeenAt)) { |
| 1596 | if (system_state_->prefs()->GetInt64(kPrefsUpdateFirstSeenAt, |
| 1597 | &update_first_seen_at_int)) { |
| 1598 | // Note: This timestamp could be that of ANY update we saw in the past |
| 1599 | // (not necessarily this particular update we're considering to apply) |
| 1600 | // but never got to apply because of some reason (e.g. stop AU policy, |
| 1601 | // updates being pulled out from Omaha, changes in target version prefix, |
| 1602 | // new update being rolled out, etc.). But for the purposes of scattering |
| 1603 | // it doesn't matter which update the timestamp corresponds to. i.e. |
| 1604 | // the clock starts ticking the first time we see an update and we're |
| 1605 | // ready to apply when the random wait period is satisfied relative to |
| 1606 | // that first seen timestamp. |
| 1607 | update_first_seen_at = Time::FromInternalValue(update_first_seen_at_int); |
| 1608 | LOG(INFO) << "Using persisted value of UpdateFirstSeenAt: " |
| 1609 | << utils::ToString(update_first_seen_at); |
| 1610 | } else { |
| 1611 | // This seems like an unexpected error where the persisted value exists |
| 1612 | // but it's not readable for some reason. |
| 1613 | LOG(INFO) << "UpdateFirstSeenAt value cannot be read"; |
| 1614 | return base::Time(); |
| 1615 | } |
| 1616 | } else { |
| 1617 | update_first_seen_at = system_state_->clock()->GetWallclockTime(); |
| 1618 | update_first_seen_at_int = update_first_seen_at.ToInternalValue(); |
| 1619 | if (system_state_->prefs()->SetInt64(kPrefsUpdateFirstSeenAt, |
| 1620 | update_first_seen_at_int)) { |
| 1621 | LOG(INFO) << "Persisted the new value for UpdateFirstSeenAt: " |
| 1622 | << utils::ToString(update_first_seen_at); |
| 1623 | } else { |
| 1624 | // This seems like an unexpected error where the value cannot be |
| 1625 | // persisted for some reason. |
| 1626 | LOG(INFO) << "UpdateFirstSeenAt value " |
| 1627 | << utils::ToString(update_first_seen_at) |
| 1628 | << " cannot be persisted"; |
| 1629 | return base::Time(); |
| 1630 | } |
| 1631 | } |
| 1632 | return update_first_seen_at; |
| 1633 | } |
| 1634 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1635 | } // namespace chromeos_update_engine |