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 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 9 | #include <sstream> |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 10 | #include <string> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 11 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 12 | #include <base/bind.h> |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 13 | #include <base/logging.h> |
| 14 | #include <base/rand_util.h> |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 15 | #include <base/string_number_conversions.h> |
| 16 | #include <base/string_util.h> |
Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 17 | #include <base/stringprintf.h> |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 18 | #include <base/time.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 19 | #include <libxml/xpath.h> |
| 20 | #include <libxml/xpathInternals.h> |
| 21 | |
| 22 | #include "update_engine/action_pipe.h" |
Jay Srinivasan | d29695d | 2013-04-08 15:08:05 -0700 | [diff] [blame] | 23 | #include "update_engine/constants.h" |
J. Richard Barnette | 056b0ab | 2013-10-29 15:24:56 -0700 | [diff] [blame] | 24 | #include "update_engine/hardware_interface.h" |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 25 | #include "update_engine/omaha_hash_calculator.h" |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 26 | #include "update_engine/omaha_request_params.h" |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 27 | #include "update_engine/p2p_manager.h" |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 28 | #include "update_engine/payload_state_interface.h" |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 29 | #include "update_engine/prefs_interface.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 30 | #include "update_engine/utils.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 31 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 32 | using base::Time; |
| 33 | using base::TimeDelta; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 34 | using std::string; |
| 35 | |
| 36 | namespace chromeos_update_engine { |
| 37 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 38 | // List of custom pair tags that we interpret in the Omaha Response: |
| 39 | static const char* kTagDeadline = "deadline"; |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 40 | static const char* kTagDisablePayloadBackoff = "DisablePayloadBackoff"; |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 41 | static const char* kTagVersion = "version"; |
Jay Srinivasan | d671e97 | 2013-01-11 17:17:19 -0800 | [diff] [blame] | 42 | // Deprecated: "IsDelta" |
| 43 | static const char* kTagIsDeltaPayload = "IsDeltaPayload"; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 44 | static const char* kTagMaxFailureCountPerUrl = "MaxFailureCountPerUrl"; |
| 45 | static const char* kTagMaxDaysToScatter = "MaxDaysToScatter"; |
| 46 | // Deprecated: "ManifestSignatureRsa" |
| 47 | // Deprecated: "ManifestSize" |
| 48 | static const char* kTagMetadataSignatureRsa = "MetadataSignatureRsa"; |
| 49 | static const char* kTagMetadataSize = "MetadataSize"; |
| 50 | static const char* kTagMoreInfo = "MoreInfo"; |
Don Garrett | 42bd3aa | 2013-04-10 18:14:56 -0700 | [diff] [blame] | 51 | // Deprecated: "NeedsAdmin" |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 52 | static const char* kTagPrompt = "Prompt"; |
| 53 | static const char* kTagSha256 = "sha256"; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 54 | static const char* kTagDisableP2PForDownloading = "DisableP2PForDownloading"; |
| 55 | static const char* kTagDisableP2PForSharing = "DisableP2PForSharing"; |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 56 | static const char* kTagPublicKeyRsa = "PublicKeyRsa"; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 57 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 58 | namespace { |
| 59 | |
| 60 | const string kGupdateVersion("ChromeOSUpdateEngine-0.1.0.0"); |
| 61 | |
| 62 | // This is handy for passing strings into libxml2 |
| 63 | #define ConstXMLStr(x) (reinterpret_cast<const xmlChar*>(x)) |
| 64 | |
| 65 | // These are for scoped_ptr_malloc, which is like scoped_ptr, but allows |
| 66 | // a custom free() function to be specified. |
| 67 | class ScopedPtrXmlDocFree { |
| 68 | public: |
| 69 | inline void operator()(void* x) const { |
| 70 | xmlFreeDoc(reinterpret_cast<xmlDoc*>(x)); |
| 71 | } |
| 72 | }; |
| 73 | class ScopedPtrXmlFree { |
| 74 | public: |
| 75 | inline void operator()(void* x) const { |
| 76 | xmlFree(x); |
| 77 | } |
| 78 | }; |
| 79 | class ScopedPtrXmlXPathObjectFree { |
| 80 | public: |
| 81 | inline void operator()(void* x) const { |
| 82 | xmlXPathFreeObject(reinterpret_cast<xmlXPathObject*>(x)); |
| 83 | } |
| 84 | }; |
| 85 | class ScopedPtrXmlXPathContextFree { |
| 86 | public: |
| 87 | inline void operator()(void* x) const { |
| 88 | xmlXPathFreeContext(reinterpret_cast<xmlXPathContext*>(x)); |
| 89 | } |
| 90 | }; |
| 91 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 92 | // Returns true if |ping_days| has a value that needs to be sent, |
| 93 | // false otherwise. |
| 94 | bool ShouldPing(int ping_days) { |
| 95 | return ping_days > 0 || ping_days == OmahaRequestAction::kNeverPinged; |
| 96 | } |
| 97 | |
| 98 | // Returns an XML ping element attribute assignment with attribute |
| 99 | // |name| and value |ping_days| if |ping_days| has a value that needs |
| 100 | // to be sent, or an empty string otherwise. |
| 101 | string GetPingAttribute(const string& name, int ping_days) { |
| 102 | if (ShouldPing(ping_days)) { |
| 103 | return StringPrintf(" %s=\"%d\"", name.c_str(), ping_days); |
| 104 | } |
| 105 | return ""; |
| 106 | } |
| 107 | |
| 108 | // Returns an XML ping element if any of the elapsed days need to be |
| 109 | // sent, or an empty string otherwise. |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 110 | string GetPingXml(int ping_active_days, int ping_roll_call_days) { |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 111 | string ping_active = GetPingAttribute("a", ping_active_days); |
| 112 | string ping_roll_call = GetPingAttribute("r", ping_roll_call_days); |
| 113 | if (!ping_active.empty() || !ping_roll_call.empty()) { |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 114 | return StringPrintf(" <ping active=\"1\"%s%s></ping>\n", |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 115 | ping_active.c_str(), |
| 116 | ping_roll_call.c_str()); |
| 117 | } |
| 118 | return ""; |
| 119 | } |
| 120 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 121 | // Returns an XML that goes into the body of the <app> element of the Omaha |
| 122 | // request based on the given parameters. |
| 123 | string GetAppBody(const OmahaEvent* event, |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 124 | OmahaRequestParams* params, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 125 | bool ping_only, |
| 126 | int ping_active_days, |
| 127 | int ping_roll_call_days, |
| 128 | PrefsInterface* prefs) { |
| 129 | string app_body; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 130 | if (event == NULL) { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 131 | app_body = GetPingXml(ping_active_days, ping_roll_call_days); |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 132 | if (!ping_only) { |
Jay Srinivasan | 56d5aa4 | 2012-03-26 14:27:59 -0700 | [diff] [blame] | 133 | // not passing update_disabled to Omaha because we want to |
| 134 | // get the update and report with UpdateDeferred result so that |
| 135 | // borgmon charts show up updates that are deferred. This is also |
| 136 | // the expected behavior when we move to Omaha v3.0 protocol, so it'll |
| 137 | // be consistent. |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 138 | app_body += StringPrintf( |
| 139 | " <updatecheck targetversionprefix=\"%s\"" |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 140 | "></updatecheck>\n", |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 141 | XmlEncode(params->target_version_prefix()).c_str()); |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 142 | |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 143 | // If this is the first update check after a reboot following a previous |
| 144 | // update, generate an event containing the previous version number. If |
| 145 | // the previous version preference file doesn't exist the event is still |
| 146 | // generated with a previous version of 0.0.0.0 -- this is relevant for |
| 147 | // older clients or new installs. The previous version event is not sent |
| 148 | // for ping-only requests because they come before the client has |
| 149 | // rebooted. |
| 150 | string prev_version; |
| 151 | if (!prefs->GetString(kPrefsPreviousVersion, &prev_version)) { |
| 152 | prev_version = "0.0.0.0"; |
| 153 | } |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 154 | |
| 155 | app_body += StringPrintf( |
| 156 | " <event eventtype=\"%d\" eventresult=\"%d\" " |
| 157 | "previousversion=\"%s\"></event>\n", |
| 158 | OmahaEvent::kTypeUpdateComplete, |
| 159 | OmahaEvent::kResultSuccessReboot, |
| 160 | XmlEncode(prev_version).c_str()); |
| 161 | LOG_IF(WARNING, !prefs->SetString(kPrefsPreviousVersion, "")) |
| 162 | << "Unable to reset the previous version."; |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 163 | } |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 164 | } else { |
Darin Petkov | c91dd6b | 2011-01-10 12:31:34 -0800 | [diff] [blame] | 165 | // The error code is an optional attribute so append it only if the result |
| 166 | // is not success. |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 167 | string error_code; |
| 168 | if (event->result != OmahaEvent::kResultSuccess) { |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 169 | error_code = StringPrintf(" errorcode=\"%d\"", event->error_code); |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 170 | } |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 171 | app_body = StringPrintf( |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 172 | " <event eventtype=\"%d\" eventresult=\"%d\"%s></event>\n", |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 173 | event->type, event->result, error_code.c_str()); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 174 | } |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 175 | |
| 176 | return app_body; |
| 177 | } |
| 178 | |
| 179 | // Returns an XML that corresponds to the entire <app> node of the Omaha |
| 180 | // request based on the given parameters. |
| 181 | string GetAppXml(const OmahaEvent* event, |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 182 | OmahaRequestParams* params, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 183 | bool ping_only, |
| 184 | int ping_active_days, |
| 185 | int ping_roll_call_days, |
| 186 | SystemState* system_state) { |
| 187 | string app_body = GetAppBody(event, params, ping_only, ping_active_days, |
| 188 | ping_roll_call_days, system_state->prefs()); |
| 189 | string app_versions; |
| 190 | |
| 191 | // If we are upgrading to a more stable channel and we are allowed to do |
| 192 | // powerwash, then pass 0.0.0.0 as the version. This is needed to get the |
| 193 | // highest-versioned payload on the destination channel. |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 194 | if (params->to_more_stable_channel() && params->is_powerwash_allowed()) { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 195 | LOG(INFO) << "Passing OS version as 0.0.0.0 as we are set to powerwash " |
| 196 | << "on downgrading to the version in the more stable channel"; |
| 197 | app_versions = "version=\"0.0.0.0\" from_version=\"" + |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 198 | XmlEncode(params->app_version()) + "\" "; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 199 | } else { |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 200 | app_versions = "version=\"" + XmlEncode(params->app_version()) + "\" "; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 203 | string download_channel = params->download_channel(); |
| 204 | string app_channels = "track=\"" + XmlEncode(download_channel) + "\" "; |
| 205 | if (params->current_channel() != download_channel) |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 206 | app_channels += |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 207 | "from_track=\"" + XmlEncode(params->current_channel()) + "\" "; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 208 | |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 209 | string delta_okay_str = params->delta_okay() ? "true" : "false"; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 210 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 211 | string app_xml = |
Jay Srinivasan | db0acdf | 2013-04-02 14:47:45 -0700 | [diff] [blame] | 212 | " <app appid=\"" + XmlEncode(params->GetAppId()) + "\" " + |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 213 | app_versions + |
| 214 | app_channels + |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 215 | "lang=\"" + XmlEncode(params->app_lang()) + "\" " + |
| 216 | "board=\"" + XmlEncode(params->os_board()) + "\" " + |
| 217 | "hardware_class=\"" + XmlEncode(params->hwid()) + "\" " + |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 218 | "delta_okay=\"" + delta_okay_str + "\" " |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 219 | "fw_version=\"" + XmlEncode(params->fw_version()) + "\" " + |
| 220 | "ec_version=\"" + XmlEncode(params->ec_version()) + "\" " + |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 221 | ">\n" + |
| 222 | app_body + |
| 223 | " </app>\n"; |
| 224 | |
| 225 | return app_xml; |
| 226 | } |
| 227 | |
| 228 | // Returns an XML that corresponds to the entire <os> node of the Omaha |
| 229 | // request based on the given parameters. |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 230 | string GetOsXml(OmahaRequestParams* params) { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 231 | string os_xml = |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 232 | " <os version=\"" + XmlEncode(params->os_version()) + "\" " + |
| 233 | "platform=\"" + XmlEncode(params->os_platform()) + "\" " + |
| 234 | "sp=\"" + XmlEncode(params->os_sp()) + "\">" |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 235 | "</os>\n"; |
| 236 | return os_xml; |
| 237 | } |
| 238 | |
| 239 | // Returns an XML that corresponds to the entire Omaha request based on the |
| 240 | // given parameters. |
| 241 | string GetRequestXml(const OmahaEvent* event, |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 242 | OmahaRequestParams* params, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 243 | bool ping_only, |
| 244 | int ping_active_days, |
| 245 | int ping_roll_call_days, |
| 246 | SystemState* system_state) { |
| 247 | string os_xml = GetOsXml(params); |
| 248 | string app_xml = GetAppXml(event, params, ping_only, ping_active_days, |
| 249 | ping_roll_call_days, system_state); |
| 250 | |
| 251 | string install_source = StringPrintf("installsource=\"%s\" ", |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 252 | (params->interactive() ? "ondemandupdate" : "scheduler")); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 253 | |
| 254 | string request_xml = |
| 255 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 256 | "<request protocol=\"3.0\" " |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 257 | "version=\"" + XmlEncode(kGupdateVersion) + "\" " |
| 258 | "updaterversion=\"" + XmlEncode(kGupdateVersion) + "\" " + |
| 259 | install_source + |
| 260 | "ismachine=\"1\">\n" + |
| 261 | os_xml + |
| 262 | app_xml + |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 263 | "</request>\n"; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 264 | |
| 265 | return request_xml; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 266 | } |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 267 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 268 | } // namespace {} |
| 269 | |
| 270 | // Encodes XML entities in a given string with libxml2. input must be |
| 271 | // UTF-8 formatted. Output will be UTF-8 formatted. |
| 272 | string XmlEncode(const string& input) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 273 | // // TODO(adlr): if allocating a new xmlDoc each time is taking up too much |
| 274 | // // cpu, considering creating one and caching it. |
| 275 | // scoped_ptr_malloc<xmlDoc, ScopedPtrXmlDocFree> xml_doc( |
| 276 | // xmlNewDoc(ConstXMLStr("1.0"))); |
| 277 | // if (!xml_doc.get()) { |
| 278 | // LOG(ERROR) << "Unable to create xmlDoc"; |
| 279 | // return ""; |
| 280 | // } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 281 | scoped_ptr_malloc<xmlChar, ScopedPtrXmlFree> str( |
| 282 | xmlEncodeEntitiesReentrant(NULL, ConstXMLStr(input.c_str()))); |
| 283 | return string(reinterpret_cast<const char *>(str.get())); |
| 284 | } |
| 285 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 286 | OmahaRequestAction::OmahaRequestAction(SystemState* system_state, |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 287 | OmahaEvent* event, |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 288 | HttpFetcher* http_fetcher, |
| 289 | bool ping_only) |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 290 | : system_state_(system_state), |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 291 | event_(event), |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 292 | http_fetcher_(http_fetcher), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 293 | ping_only_(ping_only), |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 294 | ping_active_days_(0), |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 295 | ping_roll_call_days_(0) { |
| 296 | params_ = system_state->request_params(); |
| 297 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 298 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 299 | OmahaRequestAction::~OmahaRequestAction() {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 300 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 301 | // Calculates the value to use for the ping days parameter. |
| 302 | int OmahaRequestAction::CalculatePingDays(const string& key) { |
| 303 | int days = kNeverPinged; |
| 304 | int64_t last_ping = 0; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 305 | if (system_state_->prefs()->GetInt64(key, &last_ping) && last_ping >= 0) { |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 306 | days = (Time::Now() - Time::FromInternalValue(last_ping)).InDays(); |
| 307 | if (days < 0) { |
| 308 | // If |days| is negative, then the system clock must have jumped |
| 309 | // back in time since the ping was sent. Mark the value so that |
| 310 | // it doesn't get sent to the server but we still update the |
| 311 | // last ping daystart preference. This way the next ping time |
| 312 | // will be correct, hopefully. |
| 313 | days = kPingTimeJump; |
| 314 | LOG(WARNING) << |
| 315 | "System clock jumped back in time. Resetting ping daystarts."; |
| 316 | } |
| 317 | } |
| 318 | return days; |
| 319 | } |
| 320 | |
| 321 | void OmahaRequestAction::InitPingDays() { |
| 322 | // We send pings only along with update checks, not with events. |
| 323 | if (IsEvent()) { |
| 324 | return; |
| 325 | } |
| 326 | // TODO(petkov): Figure a way to distinguish active use pings |
| 327 | // vs. roll call pings. Currently, the two pings are identical. A |
| 328 | // fix needs to change this code as well as UpdateLastPingDays. |
| 329 | ping_active_days_ = CalculatePingDays(kPrefsLastActivePingDay); |
| 330 | ping_roll_call_days_ = CalculatePingDays(kPrefsLastRollCallPingDay); |
| 331 | } |
| 332 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 333 | void OmahaRequestAction::PerformAction() { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 334 | http_fetcher_->set_delegate(this); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 335 | InitPingDays(); |
Thieu Le | b44e9e8 | 2011-06-06 14:34:04 -0700 | [diff] [blame] | 336 | if (ping_only_ && |
| 337 | !ShouldPing(ping_active_days_) && |
| 338 | !ShouldPing(ping_roll_call_days_)) { |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 339 | processor_->ActionComplete(this, kErrorCodeSuccess); |
Thieu Le | b44e9e8 | 2011-06-06 14:34:04 -0700 | [diff] [blame] | 340 | return; |
| 341 | } |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 342 | string request_post(GetRequestXml(event_.get(), |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 343 | params_, |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 344 | ping_only_, |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 345 | ping_active_days_, |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 346 | ping_roll_call_days_, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 347 | system_state_)); |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 348 | |
Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 349 | http_fetcher_->SetPostData(request_post.data(), request_post.size(), |
| 350 | kHttpContentTypeTextXml); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 351 | LOG(INFO) << "Posting an Omaha request to " << params_->update_url(); |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 352 | LOG(INFO) << "Request: " << request_post; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 353 | http_fetcher_->BeginTransfer(params_->update_url()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 356 | void OmahaRequestAction::TerminateProcessing() { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 357 | http_fetcher_->TerminateTransfer(); |
| 358 | } |
| 359 | |
| 360 | // We just store the response in the buffer. Once we've received all bytes, |
| 361 | // we'll look in the buffer and decide what to do. |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 362 | void OmahaRequestAction::ReceivedBytes(HttpFetcher *fetcher, |
| 363 | const char* bytes, |
| 364 | int length) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 365 | response_buffer_.reserve(response_buffer_.size() + length); |
| 366 | response_buffer_.insert(response_buffer_.end(), bytes, bytes + length); |
| 367 | } |
| 368 | |
| 369 | namespace { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 370 | // If non-NULL response, caller is responsible for calling xmlXPathFreeObject() |
| 371 | // on the returned object. |
| 372 | // This code is roughly based on the libxml tutorial at: |
| 373 | // http://xmlsoft.org/tutorial/apd.html |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 374 | xmlXPathObject* GetNodeSet(xmlDoc* doc, const xmlChar* xpath) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 375 | xmlXPathObject* result = NULL; |
| 376 | |
| 377 | scoped_ptr_malloc<xmlXPathContext, ScopedPtrXmlXPathContextFree> context( |
| 378 | xmlXPathNewContext(doc)); |
| 379 | if (!context.get()) { |
| 380 | LOG(ERROR) << "xmlXPathNewContext() returned NULL"; |
| 381 | return NULL; |
| 382 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 383 | |
| 384 | result = xmlXPathEvalExpression(xpath, context.get()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 385 | if (result == NULL) { |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 386 | LOG(ERROR) << "Unable to find " << xpath << " in XML document"; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 387 | return NULL; |
| 388 | } |
| 389 | if(xmlXPathNodeSetIsEmpty(result->nodesetval)){ |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 390 | LOG(INFO) << "Nodeset is empty for " << xpath; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 391 | xmlXPathFreeObject(result); |
| 392 | return NULL; |
| 393 | } |
| 394 | return result; |
| 395 | } |
| 396 | |
| 397 | // Returns the string value of a named attribute on a node, or empty string |
| 398 | // if no such node exists. If the attribute exists and has a value of |
| 399 | // empty string, there's no way to distinguish that from the attribute |
| 400 | // not existing. |
| 401 | string XmlGetProperty(xmlNode* node, const char* name) { |
| 402 | if (!xmlHasProp(node, ConstXMLStr(name))) |
| 403 | return ""; |
| 404 | scoped_ptr_malloc<xmlChar, ScopedPtrXmlFree> str( |
| 405 | xmlGetProp(node, ConstXMLStr(name))); |
| 406 | string ret(reinterpret_cast<const char *>(str.get())); |
| 407 | return ret; |
| 408 | } |
| 409 | |
| 410 | // Parses a 64 bit base-10 int from a string and returns it. Returns 0 |
| 411 | // on error. If the string contains "0", that's indistinguishable from |
| 412 | // error. |
| 413 | off_t ParseInt(const string& str) { |
| 414 | off_t ret = 0; |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 415 | int rc = sscanf(str.c_str(), "%" PRIi64, &ret); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 416 | if (rc < 1) { |
| 417 | // failure |
| 418 | return 0; |
| 419 | } |
| 420 | return ret; |
| 421 | } |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 422 | |
| 423 | // Update the last ping day preferences based on the server daystart |
| 424 | // response. Returns true on success, false otherwise. |
| 425 | bool UpdateLastPingDays(xmlDoc* doc, PrefsInterface* prefs) { |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 426 | static const char kDaystartNodeXpath[] = "/response/daystart"; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 427 | |
| 428 | scoped_ptr_malloc<xmlXPathObject, ScopedPtrXmlXPathObjectFree> |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 429 | xpath_nodeset(GetNodeSet(doc, ConstXMLStr(kDaystartNodeXpath))); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 430 | TEST_AND_RETURN_FALSE(xpath_nodeset.get()); |
| 431 | xmlNodeSet* nodeset = xpath_nodeset->nodesetval; |
| 432 | TEST_AND_RETURN_FALSE(nodeset && nodeset->nodeNr >= 1); |
| 433 | xmlNode* daystart_node = nodeset->nodeTab[0]; |
| 434 | TEST_AND_RETURN_FALSE(xmlHasProp(daystart_node, |
| 435 | ConstXMLStr("elapsed_seconds"))); |
| 436 | |
| 437 | int64_t elapsed_seconds = 0; |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 438 | TEST_AND_RETURN_FALSE(base::StringToInt64(XmlGetProperty(daystart_node, |
| 439 | "elapsed_seconds"), |
| 440 | &elapsed_seconds)); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 441 | TEST_AND_RETURN_FALSE(elapsed_seconds >= 0); |
| 442 | |
| 443 | // Remember the local time that matches the server's last midnight |
| 444 | // time. |
| 445 | Time daystart = Time::Now() - TimeDelta::FromSeconds(elapsed_seconds); |
| 446 | prefs->SetInt64(kPrefsLastActivePingDay, daystart.ToInternalValue()); |
| 447 | prefs->SetInt64(kPrefsLastRollCallPingDay, daystart.ToInternalValue()); |
| 448 | return true; |
| 449 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 450 | } // namespace {} |
| 451 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 452 | bool OmahaRequestAction::ParseResponse(xmlDoc* doc, |
| 453 | OmahaResponse* output_object, |
| 454 | ScopedActionCompleter* completer) { |
| 455 | static const char* kUpdatecheckNodeXpath("/response/app/updatecheck"); |
| 456 | |
| 457 | scoped_ptr_malloc<xmlXPathObject, ScopedPtrXmlXPathObjectFree> |
| 458 | xpath_nodeset(GetNodeSet(doc, ConstXMLStr(kUpdatecheckNodeXpath))); |
| 459 | if (!xpath_nodeset.get()) { |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 460 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 461 | return false; |
| 462 | } |
| 463 | |
| 464 | xmlNodeSet* nodeset = xpath_nodeset->nodesetval; |
| 465 | CHECK(nodeset) << "XPath missing UpdateCheck NodeSet"; |
| 466 | CHECK_GE(nodeset->nodeNr, 1); |
| 467 | xmlNode* update_check_node = nodeset->nodeTab[0]; |
| 468 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 469 | // chromium-os:37289: The PollInterval is not supported by Omaha server |
| 470 | // currently. But still keeping this existing code in case we ever decide to |
| 471 | // slow down the request rate from the server-side. Note that the |
| 472 | // PollInterval is not persisted, so it has to be sent by the server on every |
| 473 | // response to guarantee that the UpdateCheckScheduler uses this value |
| 474 | // (otherwise, if the device got rebooted after the last server-indicated |
| 475 | // value, it'll revert to the default value). Also kDefaultMaxUpdateChecks |
| 476 | // value for the scattering logic is based on the assumption that we perform |
| 477 | // an update check every hour so that the max value of 8 will roughly be |
| 478 | // equivalent to one work day. If we decide to use PollInterval permanently, |
| 479 | // we should update the max_update_checks_allowed to take PollInterval into |
| 480 | // account. Note: The parsing for PollInterval happens even before parsing |
| 481 | // of the status because we may want to specify the PollInterval even when |
| 482 | // there's no update. |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 483 | base::StringToInt(XmlGetProperty(update_check_node, "PollInterval"), |
| 484 | &output_object->poll_interval); |
| 485 | |
| 486 | if (!ParseStatus(update_check_node, output_object, completer)) |
| 487 | return false; |
| 488 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 489 | // Note: ParseUrls MUST be called before ParsePackage as ParsePackage |
| 490 | // appends the package name to the URLs populated in this method. |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 491 | if (!ParseUrls(doc, output_object, completer)) |
| 492 | return false; |
| 493 | |
| 494 | if (!ParsePackage(doc, output_object, completer)) |
| 495 | return false; |
| 496 | |
| 497 | if (!ParseParams(doc, output_object, completer)) |
| 498 | return false; |
| 499 | |
| 500 | return true; |
| 501 | } |
| 502 | |
| 503 | bool OmahaRequestAction::ParseStatus(xmlNode* update_check_node, |
| 504 | OmahaResponse* output_object, |
| 505 | ScopedActionCompleter* completer) { |
| 506 | // Get status. |
| 507 | if (!xmlHasProp(update_check_node, ConstXMLStr("status"))) { |
| 508 | LOG(ERROR) << "Omaha Response missing status"; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 509 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 510 | return false; |
| 511 | } |
| 512 | |
| 513 | const string status(XmlGetProperty(update_check_node, "status")); |
| 514 | if (status == "noupdate") { |
| 515 | LOG(INFO) << "No update."; |
| 516 | output_object->update_exists = false; |
| 517 | SetOutputObject(*output_object); |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 518 | completer->set_code(kErrorCodeSuccess); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 519 | return false; |
| 520 | } |
| 521 | |
| 522 | if (status != "ok") { |
| 523 | LOG(ERROR) << "Unknown Omaha response status: " << status; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 524 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 525 | return false; |
| 526 | } |
| 527 | |
| 528 | return true; |
| 529 | } |
| 530 | |
| 531 | bool OmahaRequestAction::ParseUrls(xmlDoc* doc, |
| 532 | OmahaResponse* output_object, |
| 533 | ScopedActionCompleter* completer) { |
| 534 | // Get the update URL. |
| 535 | static const char* kUpdateUrlNodeXPath("/response/app/updatecheck/urls/url"); |
| 536 | |
| 537 | scoped_ptr_malloc<xmlXPathObject, ScopedPtrXmlXPathObjectFree> |
| 538 | xpath_nodeset(GetNodeSet(doc, ConstXMLStr(kUpdateUrlNodeXPath))); |
| 539 | if (!xpath_nodeset.get()) { |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 540 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 541 | return false; |
| 542 | } |
| 543 | |
| 544 | xmlNodeSet* nodeset = xpath_nodeset->nodesetval; |
| 545 | CHECK(nodeset) << "XPath missing " << kUpdateUrlNodeXPath; |
| 546 | CHECK_GE(nodeset->nodeNr, 1); |
| 547 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 548 | LOG(INFO) << "Found " << nodeset->nodeNr << " url(s)"; |
| 549 | output_object->payload_urls.clear(); |
| 550 | for (int i = 0; i < nodeset->nodeNr; i++) { |
| 551 | xmlNode* url_node = nodeset->nodeTab[i]; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 552 | const string codebase(XmlGetProperty(url_node, "codebase")); |
| 553 | if (codebase.empty()) { |
| 554 | LOG(ERROR) << "Omaha Response URL has empty codebase"; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 555 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 556 | return false; |
| 557 | } |
| 558 | output_object->payload_urls.push_back(codebase); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 561 | return true; |
| 562 | } |
| 563 | |
| 564 | bool OmahaRequestAction::ParsePackage(xmlDoc* doc, |
| 565 | OmahaResponse* output_object, |
| 566 | ScopedActionCompleter* completer) { |
| 567 | // Get the package node. |
| 568 | static const char* kPackageNodeXPath( |
| 569 | "/response/app/updatecheck/manifest/packages/package"); |
| 570 | |
| 571 | scoped_ptr_malloc<xmlXPathObject, ScopedPtrXmlXPathObjectFree> |
| 572 | xpath_nodeset(GetNodeSet(doc, ConstXMLStr(kPackageNodeXPath))); |
| 573 | if (!xpath_nodeset.get()) { |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 574 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 575 | return false; |
| 576 | } |
| 577 | |
| 578 | xmlNodeSet* nodeset = xpath_nodeset->nodesetval; |
| 579 | CHECK(nodeset) << "XPath missing " << kPackageNodeXPath; |
| 580 | CHECK_GE(nodeset->nodeNr, 1); |
| 581 | |
| 582 | // We only care about the first package. |
| 583 | LOG(INFO) << "Processing first of " << nodeset->nodeNr << " package(s)"; |
| 584 | xmlNode* package_node = nodeset->nodeTab[0]; |
| 585 | |
| 586 | // Get package properties one by one. |
| 587 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 588 | // Parse the payload name to be appended to the base Url value. |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 589 | const string package_name(XmlGetProperty(package_node, "name")); |
| 590 | LOG(INFO) << "Omaha Response package name = " << package_name; |
| 591 | if (package_name.empty()) { |
| 592 | LOG(ERROR) << "Omaha Response has empty package name"; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 593 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 594 | return false; |
| 595 | } |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 596 | |
| 597 | // Append the package name to each URL in our list so that we don't |
| 598 | // propagate the urlBase vs packageName distinctions beyond this point. |
| 599 | // From now on, we only need to use payload_urls. |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 600 | for (size_t i = 0; i < output_object->payload_urls.size(); i++) |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 601 | output_object->payload_urls[i] += package_name; |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 602 | |
| 603 | // Parse the payload size. |
| 604 | off_t size = ParseInt(XmlGetProperty(package_node, "size")); |
| 605 | if (size <= 0) { |
| 606 | LOG(ERROR) << "Omaha Response has invalid payload size: " << size; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 607 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 608 | return false; |
| 609 | } |
| 610 | output_object->size = size; |
| 611 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 612 | LOG(INFO) << "Payload size = " << output_object->size << " bytes"; |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 613 | |
| 614 | return true; |
| 615 | } |
| 616 | |
| 617 | bool OmahaRequestAction::ParseParams(xmlDoc* doc, |
| 618 | OmahaResponse* output_object, |
| 619 | ScopedActionCompleter* completer) { |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 620 | // XPath location for response elements we care about. |
| 621 | static const char* kManifestNodeXPath("/response/app/updatecheck/manifest");\ |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 622 | static const char* kActionNodeXPath( |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 623 | "/response/app/updatecheck/manifest/actions/action"); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 624 | |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 625 | // Get the manifest node where version is present. |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 626 | scoped_ptr_malloc<xmlXPathObject, ScopedPtrXmlXPathObjectFree> |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 627 | xpath_manifest_nodeset(GetNodeSet(doc, ConstXMLStr(kManifestNodeXPath))); |
| 628 | if (!xpath_manifest_nodeset.get()) { |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 629 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 630 | return false; |
| 631 | } |
| 632 | |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 633 | // Grab the only matching node there should be from the xpath. |
| 634 | xmlNodeSet* nodeset = xpath_manifest_nodeset->nodesetval; |
| 635 | CHECK(nodeset) << "XPath missing " << kManifestNodeXPath; |
| 636 | CHECK_GE(nodeset->nodeNr, 1); |
| 637 | xmlNode* manifest_node = nodeset->nodeTab[0]; |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 638 | |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 639 | // Set the version. |
| 640 | output_object->version = XmlGetProperty(manifest_node, kTagVersion); |
| 641 | if (output_object->version.empty()) { |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 642 | LOG(ERROR) << "Omaha Response does not have version in manifest!"; |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 643 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
| 644 | return false; |
| 645 | } |
| 646 | |
| 647 | LOG(INFO) << "Received omaha response to update to version " |
| 648 | << output_object->version; |
| 649 | |
| 650 | // Grab the action nodes. |
| 651 | scoped_ptr_malloc<xmlXPathObject, ScopedPtrXmlXPathObjectFree> |
| 652 | xpath_action_nodeset(GetNodeSet(doc, ConstXMLStr(kActionNodeXPath))); |
| 653 | if (!xpath_action_nodeset.get()) { |
| 654 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
| 655 | return false; |
| 656 | } |
| 657 | |
| 658 | // We only care about the action that has event "postinstall", because this is |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 659 | // where Omaha puts all the generic name/value pairs in the rule. |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 660 | nodeset = xpath_action_nodeset->nodesetval; |
| 661 | CHECK(nodeset) << "XPath missing " << kActionNodeXPath; |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 662 | LOG(INFO) << "Found " << nodeset->nodeNr |
| 663 | << " action(s). Processing the postinstall action."; |
| 664 | |
| 665 | // pie_action_node holds the action node corresponding to the |
| 666 | // postinstall event action, if present. |
| 667 | xmlNode* pie_action_node = NULL; |
| 668 | for (int i = 0; i < nodeset->nodeNr; i++) { |
| 669 | xmlNode* action_node = nodeset->nodeTab[i]; |
| 670 | if (XmlGetProperty(action_node, "event") == "postinstall") { |
| 671 | pie_action_node = action_node; |
| 672 | break; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | if (!pie_action_node) { |
| 677 | LOG(ERROR) << "Omaha Response has no postinstall event action"; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 678 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 679 | return false; |
| 680 | } |
| 681 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 682 | output_object->hash = XmlGetProperty(pie_action_node, kTagSha256); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 683 | if (output_object->hash.empty()) { |
| 684 | LOG(ERROR) << "Omaha Response has empty sha256 value"; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 685 | completer->set_code(kErrorCodeOmahaResponseInvalid); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 686 | return false; |
| 687 | } |
| 688 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 689 | // Get the optional properties one by one. |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 690 | output_object->more_info_url = XmlGetProperty(pie_action_node, kTagMoreInfo); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 691 | output_object->metadata_size = |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 692 | ParseInt(XmlGetProperty(pie_action_node, kTagMetadataSize)); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 693 | output_object->metadata_signature = |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 694 | XmlGetProperty(pie_action_node, kTagMetadataSignatureRsa); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 695 | output_object->prompt = XmlGetProperty(pie_action_node, kTagPrompt) == "true"; |
| 696 | output_object->deadline = XmlGetProperty(pie_action_node, kTagDeadline); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 697 | output_object->max_days_to_scatter = |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 698 | ParseInt(XmlGetProperty(pie_action_node, kTagMaxDaysToScatter)); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 699 | output_object->disable_p2p_for_downloading = |
| 700 | (XmlGetProperty(pie_action_node, kTagDisableP2PForDownloading) == "true"); |
| 701 | output_object->disable_p2p_for_sharing = |
| 702 | (XmlGetProperty(pie_action_node, kTagDisableP2PForSharing) == "true"); |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 703 | output_object->public_key_rsa = |
| 704 | XmlGetProperty(pie_action_node, kTagPublicKeyRsa); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 705 | |
| 706 | string max = XmlGetProperty(pie_action_node, kTagMaxFailureCountPerUrl); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 707 | if (!base::StringToUint(max, &output_object->max_failure_count_per_url)) |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 708 | output_object->max_failure_count_per_url = kDefaultMaxFailureCountPerUrl; |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 709 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 710 | output_object->is_delta_payload = |
| 711 | XmlGetProperty(pie_action_node, kTagIsDeltaPayload) == "true"; |
| 712 | |
| 713 | output_object->disable_payload_backoff = |
| 714 | XmlGetProperty(pie_action_node, kTagDisablePayloadBackoff) == "true"; |
| 715 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 716 | return true; |
| 717 | } |
| 718 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 719 | // If the transfer was successful, this uses libxml2 to parse the response |
| 720 | // and fill in the appropriate fields of the output object. Also, notifies |
| 721 | // the processor that we're done. |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 722 | void OmahaRequestAction::TransferComplete(HttpFetcher *fetcher, |
| 723 | bool successful) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 724 | ScopedActionCompleter completer(processor_, this); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 725 | string current_response(response_buffer_.begin(), response_buffer_.end()); |
| 726 | LOG(INFO) << "Omaha request response: " << current_response; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 727 | |
| 728 | // Events are best effort transactions -- assume they always succeed. |
| 729 | if (IsEvent()) { |
| 730 | CHECK(!HasOutputPipe()) << "No output pipe allowed for event requests."; |
Andrew de los Reyes | 2008e4c | 2011-01-12 10:17:52 -0800 | [diff] [blame] | 731 | if (event_->result == OmahaEvent::kResultError && successful && |
J. Richard Barnette | 056b0ab | 2013-10-29 15:24:56 -0700 | [diff] [blame] | 732 | system_state_->hardware()->IsOfficialBuild()) { |
Andrew de los Reyes | 2008e4c | 2011-01-12 10:17:52 -0800 | [diff] [blame] | 733 | LOG(INFO) << "Signalling Crash Reporter."; |
| 734 | utils::ScheduleCrashReporterUpload(); |
| 735 | } |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 736 | completer.set_code(kErrorCodeSuccess); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 737 | return; |
| 738 | } |
| 739 | |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 740 | if (!successful) { |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 741 | LOG(ERROR) << "Omaha request network transfer failed."; |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 742 | int code = GetHTTPResponseCode(); |
| 743 | // Makes sure we send sane error values. |
| 744 | if (code < 0 || code >= 1000) { |
| 745 | code = 999; |
| 746 | } |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 747 | completer.set_code(static_cast<ErrorCode>( |
| 748 | kErrorCodeOmahaRequestHTTPResponseBase + code)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 749 | return; |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 750 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 751 | |
| 752 | // parse our response and fill the fields in the output object |
| 753 | scoped_ptr_malloc<xmlDoc, ScopedPtrXmlDocFree> doc( |
| 754 | xmlParseMemory(&response_buffer_[0], response_buffer_.size())); |
| 755 | if (!doc.get()) { |
| 756 | LOG(ERROR) << "Omaha response not valid XML"; |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 757 | completer.set_code(response_buffer_.empty() ? |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 758 | kErrorCodeOmahaRequestEmptyResponseError : |
| 759 | kErrorCodeOmahaRequestXMLParseError); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 760 | return; |
| 761 | } |
| 762 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 763 | // If a ping was sent, update the last ping day preferences based on |
| 764 | // the server daystart response. |
| 765 | if (ShouldPing(ping_active_days_) || |
| 766 | ShouldPing(ping_roll_call_days_) || |
| 767 | ping_active_days_ == kPingTimeJump || |
| 768 | ping_roll_call_days_ == kPingTimeJump) { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 769 | LOG_IF(ERROR, !UpdateLastPingDays(doc.get(), system_state_->prefs())) |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 770 | << "Failed to update the last ping day preferences!"; |
| 771 | } |
| 772 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 773 | if (!HasOutputPipe()) { |
| 774 | // Just set success to whether or not the http transfer succeeded, |
| 775 | // which must be true at this point in the code. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 776 | completer.set_code(kErrorCodeSuccess); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 777 | return; |
| 778 | } |
| 779 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 780 | OmahaResponse output_object; |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 781 | if (!ParseResponse(doc.get(), &output_object, &completer)) |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 782 | return; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 783 | output_object.update_exists = true; |
| 784 | SetOutputObject(output_object); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 785 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 786 | if (params_->update_disabled()) { |
Jay Srinivasan | 56d5aa4 | 2012-03-26 14:27:59 -0700 | [diff] [blame] | 787 | LOG(INFO) << "Ignoring Omaha updates as updates are disabled by policy."; |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 788 | output_object.update_exists = false; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 789 | completer.set_code(kErrorCodeOmahaUpdateIgnoredPerPolicy); |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 790 | // Note: We could technically delete the UpdateFirstSeenAt state here. |
| 791 | // If we do, it'll mean a device has to restart the UpdateFirstSeenAt |
| 792 | // and thus help scattering take effect when the AU is turned on again. |
| 793 | // On the other hand, it also increases the chance of update starvation if |
| 794 | // an admin turns AU on/off more frequently. We choose to err on the side |
| 795 | // of preventing starvation at the cost of not applying scattering in |
| 796 | // those cases. |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 797 | return; |
| 798 | } |
| 799 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 800 | // If Omaha says to disable p2p, respect that |
| 801 | if (output_object.disable_p2p_for_downloading) { |
| 802 | LOG(INFO) << "Forcibly disabling use of p2p for downloading as " |
| 803 | << "requested by Omaha."; |
| 804 | params_->set_use_p2p_for_downloading(false); |
| 805 | } |
| 806 | if (output_object.disable_p2p_for_sharing) { |
| 807 | LOG(INFO) << "Forcibly disabling use of p2p for sharing as " |
| 808 | << "requested by Omaha."; |
| 809 | params_->set_use_p2p_for_sharing(false); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 810 | } |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 811 | |
| 812 | // Update the payload state with the current response. The payload state |
| 813 | // will automatically reset all stale state if this response is different |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 814 | // from what's stored already. We are updating the payload state as late |
| 815 | // as possible in this method so that if a new release gets pushed and then |
| 816 | // got pulled back due to some issues, we don't want to clear our internal |
| 817 | // state unnecessarily. |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 818 | PayloadStateInterface* payload_state = system_state_->payload_state(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 819 | payload_state->SetResponse(output_object); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 820 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 821 | // It could be we've already exceeded the deadline for when p2p is |
| 822 | // allowed or that we've tried too many times with p2p. Check that. |
| 823 | if (params_->use_p2p_for_downloading()) { |
| 824 | payload_state->P2PNewAttempt(); |
| 825 | if (!payload_state->P2PAttemptAllowed()) { |
| 826 | LOG(INFO) << "Forcibly disabling use of p2p for downloading because " |
| 827 | << "of previous failures when using p2p."; |
| 828 | params_->set_use_p2p_for_downloading(false); |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | // From here on, we'll complete stuff in CompleteProcessing() so |
| 833 | // disable |completer| since we'll create a new one in that |
| 834 | // function. |
| 835 | completer.set_should_complete(false); |
| 836 | |
| 837 | // If we're allowed to use p2p for downloading we do not pay |
| 838 | // attention to wall-clock-based waiting if the URL is indeed |
| 839 | // available via p2p. Therefore, check if the file is available via |
| 840 | // p2p before deferring... |
| 841 | if (params_->use_p2p_for_downloading()) { |
| 842 | LookupPayloadViaP2P(output_object); |
| 843 | } else { |
| 844 | CompleteProcessing(); |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | void OmahaRequestAction::CompleteProcessing() { |
| 849 | ScopedActionCompleter completer(processor_, this); |
| 850 | OmahaResponse& output_object = const_cast<OmahaResponse&>(GetOutputObject()); |
| 851 | PayloadStateInterface* payload_state = system_state_->payload_state(); |
| 852 | |
| 853 | if (ShouldDeferDownload(&output_object)) { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 854 | output_object.update_exists = false; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 855 | LOG(INFO) << "Ignoring Omaha updates as updates are deferred by policy."; |
| 856 | completer.set_code(kErrorCodeOmahaUpdateDeferredPerPolicy); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 857 | return; |
| 858 | } |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 859 | |
Chris Sosa | 20f005c | 2013-09-05 13:53:08 -0700 | [diff] [blame] | 860 | if (payload_state->ShouldBackoffDownload()) { |
| 861 | output_object.update_exists = false; |
| 862 | LOG(INFO) << "Ignoring Omaha updates in order to backoff our retry " |
| 863 | << "attempts"; |
| 864 | completer.set_code(kErrorCodeOmahaUpdateDeferredForBackoff); |
| 865 | return; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 866 | } |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 867 | completer.set_code(kErrorCodeSuccess); |
| 868 | } |
| 869 | |
| 870 | void OmahaRequestAction::OnLookupPayloadViaP2PCompleted(const string& url) { |
| 871 | LOG(INFO) << "Lookup complete, p2p-client returned URL '" << url << "'"; |
| 872 | if (!url.empty()) { |
| 873 | params_->set_p2p_url(url); |
| 874 | } else { |
| 875 | LOG(INFO) << "Forcibly disabling use of p2p for downloading " |
| 876 | << "because no suitable peer could be found."; |
| 877 | params_->set_use_p2p_for_downloading(false); |
| 878 | } |
| 879 | CompleteProcessing(); |
| 880 | } |
| 881 | |
| 882 | void OmahaRequestAction::LookupPayloadViaP2P(const OmahaResponse& response) { |
David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 883 | // If the device is in the middle of an update, the state variables |
| 884 | // kPrefsUpdateStateNextDataOffset, kPrefsUpdateStateNextDataLength |
| 885 | // tracks the offset and length of the operation currently in |
| 886 | // progress. The offset is based from the end of the manifest which |
| 887 | // is kPrefsManifestMetadataSize bytes long. |
| 888 | // |
| 889 | // To make forward progress and avoid deadlocks, we need to find a |
| 890 | // peer that has at least the entire operation we're currently |
| 891 | // working on. Otherwise we may end up in a situation where two |
| 892 | // devices bounce back and forth downloading from each other, |
| 893 | // neither making any forward progress until one of them decides to |
| 894 | // stop using p2p (via kMaxP2PAttempts and kMaxP2PAttemptTimeSeconds |
| 895 | // safe-guards). See http://crbug.com/297170 for an example) |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 896 | size_t minimum_size = 0; |
David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 897 | int64_t manifest_metadata_size = 0; |
| 898 | int64_t next_data_offset = 0; |
| 899 | int64_t next_data_length = 0; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 900 | if (system_state_ != NULL && |
David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 901 | system_state_->prefs()->GetInt64(kPrefsManifestMetadataSize, |
| 902 | &manifest_metadata_size) && |
| 903 | manifest_metadata_size != -1 && |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 904 | system_state_->prefs()->GetInt64(kPrefsUpdateStateNextDataOffset, |
David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 905 | &next_data_offset) && |
| 906 | next_data_offset != -1 && |
| 907 | system_state_->prefs()->GetInt64(kPrefsUpdateStateNextDataLength, |
| 908 | &next_data_length)) { |
| 909 | minimum_size = manifest_metadata_size + next_data_offset + next_data_length; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | string file_id = utils::CalculateP2PFileId(response.hash, response.size); |
| 913 | if (system_state_->p2p_manager() != NULL) { |
| 914 | LOG(INFO) << "Checking if payload is available via p2p, file_id=" |
| 915 | << file_id << " minimum_size=" << minimum_size; |
| 916 | system_state_->p2p_manager()->LookupUrlForFile( |
| 917 | file_id, |
| 918 | minimum_size, |
| 919 | TimeDelta::FromHours(kMaxP2PNetworkWaitTimeSeconds), |
| 920 | base::Bind(&OmahaRequestAction::OnLookupPayloadViaP2PCompleted, |
| 921 | base::Unretained(this))); |
| 922 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 923 | } |
| 924 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 925 | bool OmahaRequestAction::ShouldDeferDownload(OmahaResponse* output_object) { |
Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 926 | if (params_->interactive()) { |
| 927 | LOG(INFO) << "Not deferring download because update is interactive."; |
| 928 | return false; |
| 929 | } |
| 930 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 931 | // If we're using p2p to download _and_ we have a p2p URL, we never |
| 932 | // defer the download. This is because the download will always |
| 933 | // happen from a peer on the LAN and we've been waiting in line for |
| 934 | // our turn. |
| 935 | if (params_->use_p2p_for_downloading() && !params_->p2p_url().empty()) { |
| 936 | LOG(INFO) << "Download not deferred because download " |
| 937 | << "will happen from a local peer (via p2p)."; |
| 938 | return false; |
| 939 | } |
| 940 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 941 | // We should defer the downloads only if we've first satisfied the |
| 942 | // wall-clock-based-waiting period and then the update-check-based waiting |
| 943 | // period, if required. |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 944 | if (!params_->wall_clock_based_wait_enabled()) { |
Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 945 | LOG(INFO) << "Wall-clock-based waiting period is not enabled," |
| 946 | << " so no deferring needed."; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 947 | return false; |
| 948 | } |
| 949 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 950 | switch (IsWallClockBasedWaitingSatisfied(output_object)) { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 951 | case kWallClockWaitNotSatisfied: |
| 952 | // We haven't even satisfied the first condition, passing the |
| 953 | // wall-clock-based waiting period, so we should defer the downloads |
| 954 | // until that happens. |
| 955 | LOG(INFO) << "wall-clock-based-wait not satisfied."; |
| 956 | return true; |
| 957 | |
| 958 | case kWallClockWaitDoneButUpdateCheckWaitRequired: |
| 959 | LOG(INFO) << "wall-clock-based-wait satisfied and " |
| 960 | << "update-check-based-wait required."; |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 961 | return !IsUpdateCheckCountBasedWaitingSatisfied(); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 962 | |
| 963 | case kWallClockWaitDoneAndUpdateCheckWaitNotRequired: |
| 964 | // Wall-clock-based waiting period is satisfied, and it's determined |
| 965 | // that we do not need the update-check-based wait. so no need to |
| 966 | // defer downloads. |
| 967 | LOG(INFO) << "wall-clock-based-wait satisfied and " |
| 968 | << "update-check-based-wait is not required."; |
| 969 | return false; |
| 970 | |
| 971 | default: |
| 972 | // Returning false for this default case so we err on the |
| 973 | // side of downloading updates than deferring in case of any bugs. |
| 974 | NOTREACHED(); |
| 975 | return false; |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | OmahaRequestAction::WallClockWaitResult |
| 980 | OmahaRequestAction::IsWallClockBasedWaitingSatisfied( |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 981 | OmahaResponse* output_object) { |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 982 | Time update_first_seen_at; |
| 983 | int64 update_first_seen_at_int; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 984 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 985 | if (system_state_->prefs()->Exists(kPrefsUpdateFirstSeenAt)) { |
| 986 | if (system_state_->prefs()->GetInt64(kPrefsUpdateFirstSeenAt, |
| 987 | &update_first_seen_at_int)) { |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 988 | // Note: This timestamp could be that of ANY update we saw in the past |
| 989 | // (not necessarily this particular update we're considering to apply) |
| 990 | // but never got to apply because of some reason (e.g. stop AU policy, |
| 991 | // updates being pulled out from Omaha, changes in target version prefix, |
| 992 | // new update being rolled out, etc.). But for the purposes of scattering |
| 993 | // it doesn't matter which update the timestamp corresponds to. i.e. |
| 994 | // the clock starts ticking the first time we see an update and we're |
| 995 | // ready to apply when the random wait period is satisfied relative to |
| 996 | // that first seen timestamp. |
| 997 | update_first_seen_at = Time::FromInternalValue(update_first_seen_at_int); |
| 998 | LOG(INFO) << "Using persisted value of UpdateFirstSeenAt: " |
| 999 | << utils::ToString(update_first_seen_at); |
| 1000 | } else { |
| 1001 | // This seems like an unexpected error where the persisted value exists |
| 1002 | // but it's not readable for some reason. Just skip scattering in this |
| 1003 | // case to be safe. |
| 1004 | LOG(INFO) << "Not scattering as UpdateFirstSeenAt value cannot be read"; |
| 1005 | return kWallClockWaitDoneAndUpdateCheckWaitNotRequired; |
| 1006 | } |
| 1007 | } else { |
| 1008 | update_first_seen_at = Time::Now(); |
| 1009 | update_first_seen_at_int = update_first_seen_at.ToInternalValue(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1010 | if (system_state_->prefs()->SetInt64(kPrefsUpdateFirstSeenAt, |
| 1011 | update_first_seen_at_int)) { |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1012 | LOG(INFO) << "Persisted the new value for UpdateFirstSeenAt: " |
| 1013 | << utils::ToString(update_first_seen_at); |
| 1014 | } |
| 1015 | else { |
| 1016 | // This seems like an unexpected error where the value cannot be |
| 1017 | // persisted for some reason. Just skip scattering in this |
| 1018 | // case to be safe. |
| 1019 | LOG(INFO) << "Not scattering as UpdateFirstSeenAt value " |
| 1020 | << utils::ToString(update_first_seen_at) |
| 1021 | << " cannot be persisted"; |
| 1022 | return kWallClockWaitDoneAndUpdateCheckWaitNotRequired; |
| 1023 | } |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1026 | TimeDelta elapsed_time = Time::Now() - update_first_seen_at; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1027 | TimeDelta max_scatter_period = TimeDelta::FromDays( |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1028 | output_object->max_days_to_scatter); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1029 | |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1030 | LOG(INFO) << "Waiting Period = " |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1031 | << utils::FormatSecs(params_->waiting_period().InSeconds()) |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1032 | << ", Time Elapsed = " |
| 1033 | << utils::FormatSecs(elapsed_time.InSeconds()) |
| 1034 | << ", MaxDaysToScatter = " |
| 1035 | << max_scatter_period.InDays(); |
| 1036 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1037 | if (!output_object->deadline.empty()) { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1038 | // The deadline is set for all rules which serve a delta update from a |
| 1039 | // previous FSI, which means this update will be applied mostly in OOBE |
| 1040 | // cases. For these cases, we shouldn't scatter so as to finish the OOBE |
| 1041 | // quickly. |
| 1042 | LOG(INFO) << "Not scattering as deadline flag is set"; |
| 1043 | return kWallClockWaitDoneAndUpdateCheckWaitNotRequired; |
| 1044 | } |
| 1045 | |
| 1046 | if (max_scatter_period.InDays() == 0) { |
| 1047 | // This means the Omaha rule creator decides that this rule |
| 1048 | // should not be scattered irrespective of the policy. |
| 1049 | LOG(INFO) << "Not scattering as MaxDaysToScatter in rule is 0."; |
| 1050 | return kWallClockWaitDoneAndUpdateCheckWaitNotRequired; |
| 1051 | } |
| 1052 | |
| 1053 | if (elapsed_time > max_scatter_period) { |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1054 | // This means we've waited more than the upperbound wait in the rule |
| 1055 | // from the time we first saw a valid update available to us. |
| 1056 | // This will prevent update starvation. |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1057 | LOG(INFO) << "Not scattering as we're past the MaxDaysToScatter limit."; |
| 1058 | return kWallClockWaitDoneAndUpdateCheckWaitNotRequired; |
| 1059 | } |
| 1060 | |
| 1061 | // This means we are required to participate in scattering. |
| 1062 | // See if our turn has arrived now. |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1063 | TimeDelta remaining_wait_time = params_->waiting_period() - elapsed_time; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1064 | if (remaining_wait_time.InSeconds() <= 0) { |
| 1065 | // Yes, it's our turn now. |
| 1066 | LOG(INFO) << "Successfully passed the wall-clock-based-wait."; |
| 1067 | |
| 1068 | // But we can't download until the update-check-count-based wait is also |
| 1069 | // satisfied, so mark it as required now if update checks are enabled. |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1070 | return params_->update_check_count_wait_enabled() ? |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1071 | kWallClockWaitDoneButUpdateCheckWaitRequired : |
| 1072 | kWallClockWaitDoneAndUpdateCheckWaitNotRequired; |
| 1073 | } |
| 1074 | |
| 1075 | // Not our turn yet, so we have to wait until our turn to |
| 1076 | // help scatter the downloads across all clients of the enterprise. |
| 1077 | LOG(INFO) << "Update deferred for another " |
| 1078 | << utils::FormatSecs(remaining_wait_time.InSeconds()) |
| 1079 | << " per policy."; |
| 1080 | return kWallClockWaitNotSatisfied; |
| 1081 | } |
| 1082 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1083 | bool OmahaRequestAction::IsUpdateCheckCountBasedWaitingSatisfied() { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1084 | int64 update_check_count_value; |
| 1085 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1086 | if (system_state_->prefs()->Exists(kPrefsUpdateCheckCount)) { |
| 1087 | if (!system_state_->prefs()->GetInt64(kPrefsUpdateCheckCount, |
| 1088 | &update_check_count_value)) { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1089 | // We are unable to read the update check count from file for some reason. |
| 1090 | // So let's proceed anyway so as to not stall the update. |
| 1091 | LOG(ERROR) << "Unable to read update check count. " |
| 1092 | << "Skipping update-check-count-based-wait."; |
| 1093 | return true; |
| 1094 | } |
| 1095 | } else { |
| 1096 | // This file does not exist. This means we haven't started our update |
| 1097 | // check count down yet, so this is the right time to start the count down. |
| 1098 | update_check_count_value = base::RandInt( |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1099 | params_->min_update_checks_needed(), |
| 1100 | params_->max_update_checks_allowed()); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1101 | |
| 1102 | LOG(INFO) << "Randomly picked update check count value = " |
| 1103 | << update_check_count_value; |
| 1104 | |
| 1105 | // Write out the initial value of update_check_count_value. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1106 | if (!system_state_->prefs()->SetInt64(kPrefsUpdateCheckCount, |
| 1107 | update_check_count_value)) { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1108 | // We weren't able to write the update check count file for some reason. |
| 1109 | // So let's proceed anyway so as to not stall the update. |
| 1110 | LOG(ERROR) << "Unable to write update check count. " |
| 1111 | << "Skipping update-check-count-based-wait."; |
| 1112 | return true; |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | if (update_check_count_value == 0) { |
| 1117 | LOG(INFO) << "Successfully passed the update-check-based-wait."; |
| 1118 | return true; |
| 1119 | } |
| 1120 | |
| 1121 | if (update_check_count_value < 0 || |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1122 | update_check_count_value > params_->max_update_checks_allowed()) { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1123 | // We err on the side of skipping scattering logic instead of stalling |
| 1124 | // a machine from receiving any updates in case of any unexpected state. |
| 1125 | LOG(ERROR) << "Invalid value for update check count detected. " |
| 1126 | << "Skipping update-check-count-based-wait."; |
| 1127 | return true; |
| 1128 | } |
| 1129 | |
| 1130 | // Legal value, we need to wait for more update checks to happen |
| 1131 | // until this becomes 0. |
| 1132 | LOG(INFO) << "Deferring Omaha updates for another " |
| 1133 | << update_check_count_value |
| 1134 | << " update checks per policy"; |
| 1135 | return false; |
| 1136 | } |
| 1137 | |
| 1138 | } // namespace chromeos_update_engine |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1139 | |
| 1140 | |