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