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