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