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