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