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