blob: 3f9f7c45eddb1a66ec2ea1982f9e98de49b0cbc0 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
rspangler@google.com49fdf182009-10-10 00:57:34 +000016
Darin Petkov6a5b3222010-07-13 14:55:28 -070017#include "update_engine/omaha_request_action.h"
Darin Petkov85ced132010-09-01 10:20:56 -070018
Andrew de los Reyes08c4e272010-04-15 14:02:17 -070019#include <inttypes.h>
Darin Petkov85ced132010-09-01 10:20:56 -070020
David Zeuthene8ed8632014-07-24 13:38:10 -040021#include <map>
rspangler@google.com49fdf182009-10-10 00:57:34 +000022#include <sstream>
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070023#include <string>
David Zeuthene8ed8632014-07-24 13:38:10 -040024#include <vector>
rspangler@google.com49fdf182009-10-10 00:57:34 +000025
David Zeuthen8f191b22013-08-06 12:27:50 -070026#include <base/bind.h>
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070027#include <base/logging.h>
28#include <base/rand_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070029#include <base/strings/string_number_conversions.h>
Sen Jiang0affc2c2017-02-10 15:55:05 -080030#include <base/strings/string_split.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070031#include <base/strings/string_util.h>
32#include <base/strings/stringprintf.h>
33#include <base/time/time.h>
David Zeuthene8ed8632014-07-24 13:38:10 -040034#include <expat.h>
Alex Deymoa2591792015-11-17 00:39:40 -030035#include <metrics/metrics_library.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000036
Alex Deymo39910dc2015-11-09 17:04:30 -080037#include "update_engine/common/action_pipe.h"
38#include "update_engine/common/constants.h"
39#include "update_engine/common/hardware_interface.h"
40#include "update_engine/common/hash_calculator.h"
41#include "update_engine/common/platform_constants.h"
42#include "update_engine/common/prefs_interface.h"
43#include "update_engine/common/utils.h"
Sen Jiang255e22b2016-05-20 16:15:29 -070044#include "update_engine/connection_manager_interface.h"
Alex Deymo38429cf2015-11-11 18:27:22 -080045#include "update_engine/metrics.h"
46#include "update_engine/metrics_utils.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070047#include "update_engine/omaha_request_params.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070048#include "update_engine/p2p_manager.h"
Jay Srinivasan55f50c22013-01-10 19:24:35 -080049#include "update_engine/payload_state_interface.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000050
Darin Petkov1cbd78f2010-07-29 12:38:34 -070051using base::Time;
52using base::TimeDelta;
David Zeuthene8ed8632014-07-24 13:38:10 -040053using std::map;
rspangler@google.com49fdf182009-10-10 00:57:34 +000054using std::string;
David Zeuthene8ed8632014-07-24 13:38:10 -040055using std::vector;
rspangler@google.com49fdf182009-10-10 00:57:34 +000056
57namespace chromeos_update_engine {
58
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080059// List of custom pair tags that we interpret in the Omaha Response:
60static const char* kTagDeadline = "deadline";
Jay Srinivasan08262882012-12-28 19:29:43 -080061static const char* kTagDisablePayloadBackoff = "DisablePayloadBackoff";
Chris Sosa3b748432013-06-20 16:42:59 -070062static const char* kTagVersion = "version";
Jay Srinivasand671e972013-01-11 17:17:19 -080063// Deprecated: "IsDelta"
64static const char* kTagIsDeltaPayload = "IsDeltaPayload";
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080065static const char* kTagMaxFailureCountPerUrl = "MaxFailureCountPerUrl";
66static const char* kTagMaxDaysToScatter = "MaxDaysToScatter";
67// Deprecated: "ManifestSignatureRsa"
68// Deprecated: "ManifestSize"
69static const char* kTagMetadataSignatureRsa = "MetadataSignatureRsa";
70static const char* kTagMetadataSize = "MetadataSize";
71static const char* kTagMoreInfo = "MoreInfo";
Don Garrett42bd3aa2013-04-10 18:14:56 -070072// Deprecated: "NeedsAdmin"
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080073static const char* kTagPrompt = "Prompt";
David Zeuthen8f191b22013-08-06 12:27:50 -070074static const char* kTagDisableP2PForDownloading = "DisableP2PForDownloading";
75static const char* kTagDisableP2PForSharing = "DisableP2PForSharing";
David Zeuthene7f89172013-10-31 10:21:04 -070076static const char* kTagPublicKeyRsa = "PublicKeyRsa";
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080077
Alex Deymoac41a822015-09-15 20:52:53 -070078static const char* kOmahaUpdaterVersion = "0.1.0.0";
rspangler@google.com49fdf182009-10-10 00:57:34 +000079
Alex Deymo14ad88e2016-06-29 12:30:14 -070080// X-GoogleUpdate headers.
81static const char* kXGoogleUpdateInteractivity = "X-GoogleUpdate-Interactivity";
82static const char* kXGoogleUpdateAppId = "X-GoogleUpdate-AppId";
83static const char* kXGoogleUpdateUpdater = "X-GoogleUpdate-Updater";
84
Alex Deymob3fa53b2016-04-18 19:57:58 -070085// updatecheck attributes (without the underscore prefix).
86static const char* kEolAttr = "eol";
87
Alex Deymoac41a822015-09-15 20:52:53 -070088namespace {
rspangler@google.com49fdf182009-10-10 00:57:34 +000089
Darin Petkov1cbd78f2010-07-29 12:38:34 -070090// Returns an XML ping element attribute assignment with attribute
91// |name| and value |ping_days| if |ping_days| has a value that needs
92// to be sent, or an empty string otherwise.
93string GetPingAttribute(const string& name, int ping_days) {
Alex Deymoebbe7ef2014-10-30 13:02:49 -070094 if (ping_days > 0 || ping_days == OmahaRequestAction::kNeverPinged)
Alex Vakulenko75039d72014-03-25 12:36:28 -070095 return base::StringPrintf(" %s=\"%d\"", name.c_str(), ping_days);
Darin Petkov1cbd78f2010-07-29 12:38:34 -070096 return "";
97}
98
99// Returns an XML ping element if any of the elapsed days need to be
100// sent, or an empty string otherwise.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700101string GetPingXml(int ping_active_days, int ping_roll_call_days) {
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700102 string ping_active = GetPingAttribute("a", ping_active_days);
103 string ping_roll_call = GetPingAttribute("r", ping_roll_call_days);
104 if (!ping_active.empty() || !ping_roll_call.empty()) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700105 return base::StringPrintf(" <ping active=\"1\"%s%s></ping>\n",
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700106 ping_active.c_str(),
107 ping_roll_call.c_str());
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700108 }
109 return "";
110}
111
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700112// Returns an XML that goes into the body of the <app> element of the Omaha
113// request based on the given parameters.
114string GetAppBody(const OmahaEvent* event,
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700115 OmahaRequestParams* params,
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700116 bool ping_only,
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700117 bool include_ping,
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700118 int ping_active_days,
119 int ping_roll_call_days,
120 PrefsInterface* prefs) {
121 string app_body;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700122 if (event == nullptr) {
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700123 if (include_ping)
124 app_body = GetPingXml(ping_active_days, ping_roll_call_days);
Darin Petkov265f2902011-05-09 15:17:40 -0700125 if (!ping_only) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700126 app_body += base::StringPrintf(
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700127 " <updatecheck targetversionprefix=\"%s\""
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700128 "></updatecheck>\n",
Alex Deymob0d74eb2015-03-30 17:59:17 -0700129 XmlEncodeWithDefault(params->target_version_prefix(), "").c_str());
Jay Srinivasan0a708742012-03-20 11:26:12 -0700130
Darin Petkov265f2902011-05-09 15:17:40 -0700131 // If this is the first update check after a reboot following a previous
132 // update, generate an event containing the previous version number. If
133 // the previous version preference file doesn't exist the event is still
134 // generated with a previous version of 0.0.0.0 -- this is relevant for
135 // older clients or new installs. The previous version event is not sent
136 // for ping-only requests because they come before the client has
Alex Deymo87c08862015-10-30 21:56:55 -0700137 // rebooted. The previous version event is also not sent if it was already
138 // sent for this new version with a previous updatecheck.
Darin Petkov265f2902011-05-09 15:17:40 -0700139 string prev_version;
140 if (!prefs->GetString(kPrefsPreviousVersion, &prev_version)) {
141 prev_version = "0.0.0.0";
142 }
Alex Deymo87c08862015-10-30 21:56:55 -0700143 // We only store a non-empty previous version value after a successful
144 // update in the previous boot. After reporting it back to the server,
145 // we clear the previous version value so it doesn't get reported again.
146 if (!prev_version.empty()) {
147 app_body += base::StringPrintf(
148 " <event eventtype=\"%d\" eventresult=\"%d\" "
149 "previousversion=\"%s\"></event>\n",
Alex Deymo9fded1e2015-11-05 12:31:19 -0800150 OmahaEvent::kTypeRebootedAfterUpdate,
151 OmahaEvent::kResultSuccess,
Alex Deymo87c08862015-10-30 21:56:55 -0700152 XmlEncodeWithDefault(prev_version, "0.0.0.0").c_str());
153 LOG_IF(WARNING, !prefs->SetString(kPrefsPreviousVersion, ""))
154 << "Unable to reset the previous version.";
155 }
Darin Petkov95508da2011-01-05 12:42:29 -0800156 }
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700157 } else {
Darin Petkovc91dd6b2011-01-10 12:31:34 -0800158 // The error code is an optional attribute so append it only if the result
159 // is not success.
Darin Petkove17f86b2010-07-20 09:12:01 -0700160 string error_code;
161 if (event->result != OmahaEvent::kResultSuccess) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700162 error_code = base::StringPrintf(" errorcode=\"%d\"",
163 static_cast<int>(event->error_code));
Darin Petkove17f86b2010-07-20 09:12:01 -0700164 }
Alex Vakulenko75039d72014-03-25 12:36:28 -0700165 app_body = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700166 " <event eventtype=\"%d\" eventresult=\"%d\"%s></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -0700167 event->type, event->result, error_code.c_str());
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700168 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700169
170 return app_body;
171}
172
Alex Deymo8e18f932015-03-27 16:16:59 -0700173// Returns the cohort* argument to include in the <app> tag for the passed
174// |arg_name| and |prefs_key|, if any. The return value is suitable to
175// concatenate to the list of arguments and includes a space at the end.
176string GetCohortArgXml(PrefsInterface* prefs,
Alex Deymob0d74eb2015-03-30 17:59:17 -0700177 const string arg_name,
178 const string prefs_key) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700179 // There's nothing wrong with not having a given cohort setting, so we check
180 // existance first to avoid the warning log message.
181 if (!prefs->Exists(prefs_key))
182 return "";
183 string cohort_value;
184 if (!prefs->GetString(prefs_key, &cohort_value) || cohort_value.empty())
185 return "";
186 // This is a sanity check to avoid sending a huge XML file back to Ohama due
187 // to a compromised stateful partition making the update check fail in low
188 // network environments envent after a reboot.
189 if (cohort_value.size() > 1024) {
190 LOG(WARNING) << "The omaha cohort setting " << arg_name
191 << " has a too big value, which must be an error or an "
192 "attacker trying to inhibit updates.";
193 return "";
194 }
195
Alex Deymob0d74eb2015-03-30 17:59:17 -0700196 string escaped_xml_value;
197 if (!XmlEncode(cohort_value, &escaped_xml_value)) {
198 LOG(WARNING) << "The omaha cohort setting " << arg_name
199 << " is ASCII-7 invalid, ignoring it.";
200 return "";
201 }
202
Alex Deymo8e18f932015-03-27 16:16:59 -0700203 return base::StringPrintf("%s=\"%s\" ",
Alex Deymob0d74eb2015-03-30 17:59:17 -0700204 arg_name.c_str(), escaped_xml_value.c_str());
Alex Deymo8e18f932015-03-27 16:16:59 -0700205}
206
Sen Jiang94a4dec2017-03-28 18:23:35 -0700207struct OmahaAppData {
208 string id;
209 string version;
210};
211
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700212// Returns an XML that corresponds to the entire <app> node of the Omaha
213// request based on the given parameters.
214string GetAppXml(const OmahaEvent* event,
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700215 OmahaRequestParams* params,
Sen Jiang94a4dec2017-03-28 18:23:35 -0700216 const OmahaAppData& app_data,
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700217 bool ping_only,
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700218 bool include_ping,
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700219 int ping_active_days,
220 int ping_roll_call_days,
David Zeuthen639aa362014-02-03 16:23:44 -0800221 int install_date_in_days,
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700222 SystemState* system_state) {
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700223 string app_body = GetAppBody(event, params, ping_only, include_ping,
224 ping_active_days, ping_roll_call_days,
225 system_state->prefs());
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700226 string app_versions;
227
228 // If we are upgrading to a more stable channel and we are allowed to do
229 // powerwash, then pass 0.0.0.0 as the version. This is needed to get the
230 // highest-versioned payload on the destination channel.
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700231 if (params->to_more_stable_channel() && params->is_powerwash_allowed()) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700232 LOG(INFO) << "Passing OS version as 0.0.0.0 as we are set to powerwash "
233 << "on downgrading to the version in the more stable channel";
234 app_versions = "version=\"0.0.0.0\" from_version=\"" +
Sen Jiang94a4dec2017-03-28 18:23:35 -0700235 XmlEncodeWithDefault(app_data.version, "0.0.0.0") + "\" ";
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700236 } else {
Alex Deymob0d74eb2015-03-30 17:59:17 -0700237 app_versions = "version=\"" +
Sen Jiang94a4dec2017-03-28 18:23:35 -0700238 XmlEncodeWithDefault(app_data.version, "0.0.0.0") + "\" ";
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700239 }
240
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700241 string download_channel = params->download_channel();
Alex Deymob0d74eb2015-03-30 17:59:17 -0700242 string app_channels =
243 "track=\"" + XmlEncodeWithDefault(download_channel, "") + "\" ";
244 if (params->current_channel() != download_channel) {
245 app_channels += "from_track=\"" + XmlEncodeWithDefault(
246 params->current_channel(), "") + "\" ";
247 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700248
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700249 string delta_okay_str = params->delta_okay() ? "true" : "false";
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700250
David Zeuthen639aa362014-02-03 16:23:44 -0800251 // If install_date_days is not set (e.g. its value is -1 ), don't
252 // include the attribute.
253 string install_date_in_days_str = "";
254 if (install_date_in_days >= 0) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700255 install_date_in_days_str = base::StringPrintf("installdate=\"%d\" ",
256 install_date_in_days);
David Zeuthen639aa362014-02-03 16:23:44 -0800257 }
258
Alex Deymo8e18f932015-03-27 16:16:59 -0700259 string app_cohort_args;
260 app_cohort_args += GetCohortArgXml(system_state->prefs(),
261 "cohort", kPrefsOmahaCohort);
262 app_cohort_args += GetCohortArgXml(system_state->prefs(),
263 "cohorthint", kPrefsOmahaCohortHint);
264 app_cohort_args += GetCohortArgXml(system_state->prefs(),
265 "cohortname", kPrefsOmahaCohortName);
266
Alex Deymoebf6e122017-03-10 16:12:01 -0800267 string fingerprint_arg;
268 if (!params->os_build_fingerprint().empty()) {
269 fingerprint_arg =
270 "fingerprint=\"" + XmlEncodeWithDefault(params->os_build_fingerprint(), "") + "\" ";
271 }
272
Alex Deymob0d74eb2015-03-30 17:59:17 -0700273 string app_xml = " <app "
Sen Jiang94a4dec2017-03-28 18:23:35 -0700274 "appid=\"" + XmlEncodeWithDefault(app_data.id, "") + "\" " +
Alex Deymob0d74eb2015-03-30 17:59:17 -0700275 app_cohort_args +
276 app_versions +
277 app_channels +
Alex Deymoebf6e122017-03-10 16:12:01 -0800278 fingerprint_arg +
Alex Deymob0d74eb2015-03-30 17:59:17 -0700279 "lang=\"" + XmlEncodeWithDefault(params->app_lang(), "en-US") + "\" " +
280 "board=\"" + XmlEncodeWithDefault(params->os_board(), "") + "\" " +
281 "hardware_class=\"" + XmlEncodeWithDefault(params->hwid(), "") + "\" " +
282 "delta_okay=\"" + delta_okay_str + "\" "
283 "fw_version=\"" + XmlEncodeWithDefault(params->fw_version(), "") + "\" " +
284 "ec_version=\"" + XmlEncodeWithDefault(params->ec_version(), "") + "\" " +
285 install_date_in_days_str +
286 ">\n" +
287 app_body +
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700288 " </app>\n";
289
290 return app_xml;
291}
292
293// Returns an XML that corresponds to the entire <os> node of the Omaha
294// request based on the given parameters.
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700295string GetOsXml(OmahaRequestParams* params) {
Alex Deymob0d74eb2015-03-30 17:59:17 -0700296 string os_xml =" <os "
297 "version=\"" + XmlEncodeWithDefault(params->os_version(), "") + "\" " +
298 "platform=\"" + XmlEncodeWithDefault(params->os_platform(), "") + "\" " +
299 "sp=\"" + XmlEncodeWithDefault(params->os_sp(), "") + "\">"
300 "</os>\n";
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700301 return os_xml;
302}
303
304// Returns an XML that corresponds to the entire Omaha request based on the
305// given parameters.
306string GetRequestXml(const OmahaEvent* event,
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700307 OmahaRequestParams* params,
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700308 bool ping_only,
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700309 bool include_ping,
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700310 int ping_active_days,
311 int ping_roll_call_days,
David Zeuthen639aa362014-02-03 16:23:44 -0800312 int install_date_in_days,
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700313 SystemState* system_state) {
314 string os_xml = GetOsXml(params);
Sen Jiang94a4dec2017-03-28 18:23:35 -0700315 OmahaAppData product_app = {.id = params->GetAppId(),
316 .version = params->app_version()};
317 string app_xml = GetAppXml(event,
318 params,
319 product_app,
320 ping_only,
321 include_ping,
322 ping_active_days,
323 ping_roll_call_days,
324 install_date_in_days,
325 system_state);
326 if (!params->system_app_id().empty()) {
327 OmahaAppData system_app = {.id = params->system_app_id(),
328 .version = params->system_version()};
329 app_xml += GetAppXml(event,
330 params,
331 system_app,
332 ping_only,
333 include_ping,
334 ping_active_days,
335 ping_roll_call_days,
336 install_date_in_days,
337 system_state);
338 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700339
Alex Vakulenko75039d72014-03-25 12:36:28 -0700340 string install_source = base::StringPrintf("installsource=\"%s\" ",
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700341 (params->interactive() ? "ondemandupdate" : "scheduler"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700342
Alex Deymoac41a822015-09-15 20:52:53 -0700343 string updater_version = XmlEncodeWithDefault(
344 base::StringPrintf("%s-%s",
345 constants::kOmahaUpdaterID,
346 kOmahaUpdaterVersion), "");
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700347 string request_xml =
348 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
Alex Deymob0d74eb2015-03-30 17:59:17 -0700349 "<request protocol=\"3.0\" " + (
Alex Deymoac41a822015-09-15 20:52:53 -0700350 "version=\"" + updater_version + "\" "
351 "updaterversion=\"" + updater_version + "\" " +
Alex Deymob0d74eb2015-03-30 17:59:17 -0700352 install_source +
353 "ismachine=\"1\">\n") +
354 os_xml +
355 app_xml +
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700356 "</request>\n";
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700357
358 return request_xml;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000359}
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700360
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700361} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000362
David Zeuthene8ed8632014-07-24 13:38:10 -0400363// Struct used for holding data obtained when parsing the XML.
364struct OmahaParserData {
David Zeuthenf3e28012014-08-26 18:23:52 -0400365 explicit OmahaParserData(XML_Parser _xml_parser) : xml_parser(_xml_parser) {}
366
367 // Pointer to the expat XML_Parser object.
368 XML_Parser xml_parser;
369
David Zeuthene8ed8632014-07-24 13:38:10 -0400370 // This is the state of the parser as it's processing the XML.
371 bool failed = false;
David Zeuthenf3e28012014-08-26 18:23:52 -0400372 bool entity_decl = false;
David Zeuthene8ed8632014-07-24 13:38:10 -0400373 string current_path;
374
375 // These are the values extracted from the XML.
Alex Deymo8e18f932015-03-27 16:16:59 -0700376 string app_cohort;
377 string app_cohorthint;
378 string app_cohortname;
379 bool app_cohort_set = false;
380 bool app_cohorthint_set = false;
381 bool app_cohortname_set = false;
David Zeuthene8ed8632014-07-24 13:38:10 -0400382 string updatecheck_poll_interval;
Alex Deymob3fa53b2016-04-18 19:57:58 -0700383 map<string, string> updatecheck_attrs;
David Zeuthene8ed8632014-07-24 13:38:10 -0400384 string daystart_elapsed_days;
385 string daystart_elapsed_seconds;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800386
Sen Jiang81259682017-03-30 15:11:30 -0700387 struct App {
388 vector<string> url_codebase;
389 string manifest_version;
390 map<string, string> action_postinstall_attrs;
391 string updatecheck_status;
392
393 struct Package {
394 string name;
395 string size;
396 string hash;
397 };
398 vector<Package> packages;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800399 };
Sen Jiang81259682017-03-30 15:11:30 -0700400 vector<App> apps;
David Zeuthene8ed8632014-07-24 13:38:10 -0400401};
402
403namespace {
404
405// Callback function invoked by expat.
406void ParserHandlerStart(void* user_data, const XML_Char* element,
407 const XML_Char** attr) {
408 OmahaParserData* data = reinterpret_cast<OmahaParserData*>(user_data);
409
410 if (data->failed)
411 return;
412
413 data->current_path += string("/") + element;
414
415 map<string, string> attrs;
416 if (attr != nullptr) {
417 for (int n = 0; attr[n] != nullptr && attr[n+1] != nullptr; n += 2) {
418 string key = attr[n];
419 string value = attr[n + 1];
420 attrs[key] = value;
421 }
422 }
423
Alex Deymo8e18f932015-03-27 16:16:59 -0700424 if (data->current_path == "/response/app") {
Sen Jiang81259682017-03-30 15:11:30 -0700425 data->apps.emplace_back();
Alex Deymo8e18f932015-03-27 16:16:59 -0700426 if (attrs.find("cohort") != attrs.end()) {
427 data->app_cohort_set = true;
428 data->app_cohort = attrs["cohort"];
429 }
430 if (attrs.find("cohorthint") != attrs.end()) {
431 data->app_cohorthint_set = true;
432 data->app_cohorthint = attrs["cohorthint"];
433 }
434 if (attrs.find("cohortname") != attrs.end()) {
435 data->app_cohortname_set = true;
436 data->app_cohortname = attrs["cohortname"];
437 }
438 } else if (data->current_path == "/response/app/updatecheck") {
Sen Jiang81259682017-03-30 15:11:30 -0700439 if (!data->apps.empty())
440 data->apps.back().updatecheck_status = attrs["status"];
441 if (data->updatecheck_poll_interval.empty())
442 data->updatecheck_poll_interval = attrs["PollInterval"];
Alex Deymob3fa53b2016-04-18 19:57:58 -0700443 // Omaha sends arbitrary key-value pairs as extra attributes starting with
444 // an underscore.
445 for (const auto& attr : attrs) {
446 if (!attr.first.empty() && attr.first[0] == '_')
447 data->updatecheck_attrs[attr.first.substr(1)] = attr.second;
448 }
David Zeuthene8ed8632014-07-24 13:38:10 -0400449 } else if (data->current_path == "/response/daystart") {
450 // Get the install-date.
451 data->daystart_elapsed_days = attrs["elapsed_days"];
452 data->daystart_elapsed_seconds = attrs["elapsed_seconds"];
453 } else if (data->current_path == "/response/app/updatecheck/urls/url") {
454 // Look at all <url> elements.
Sen Jiang81259682017-03-30 15:11:30 -0700455 if (!data->apps.empty())
456 data->apps.back().url_codebase.push_back(attrs["codebase"]);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800457 } else if (data->current_path ==
David Zeuthene8ed8632014-07-24 13:38:10 -0400458 "/response/app/updatecheck/manifest/packages/package") {
Sen Jiang0affc2c2017-02-10 15:55:05 -0800459 // Look at all <package> elements.
Sen Jiang81259682017-03-30 15:11:30 -0700460 if (!data->apps.empty())
461 data->apps.back().packages.push_back({.name = attrs["name"],
462 .size = attrs["size"],
463 .hash = attrs["hash_sha256"]});
David Zeuthene8ed8632014-07-24 13:38:10 -0400464 } else if (data->current_path == "/response/app/updatecheck/manifest") {
465 // Get the version.
Sen Jiang81259682017-03-30 15:11:30 -0700466 if (!data->apps.empty())
467 data->apps.back().manifest_version = attrs[kTagVersion];
David Zeuthene8ed8632014-07-24 13:38:10 -0400468 } else if (data->current_path ==
469 "/response/app/updatecheck/manifest/actions/action") {
470 // We only care about the postinstall action.
Sen Jiang81259682017-03-30 15:11:30 -0700471 if (attrs["event"] == "postinstall" && !data->apps.empty()) {
472 data->apps.back().action_postinstall_attrs = std::move(attrs);
David Zeuthene8ed8632014-07-24 13:38:10 -0400473 }
474 }
475}
476
477// Callback function invoked by expat.
478void ParserHandlerEnd(void* user_data, const XML_Char* element) {
479 OmahaParserData* data = reinterpret_cast<OmahaParserData*>(user_data);
480 if (data->failed)
481 return;
482
483 const string path_suffix = string("/") + element;
484
Alex Vakulenko0103c362016-01-20 07:56:15 -0800485 if (!base::EndsWith(data->current_path, path_suffix,
486 base::CompareCase::SENSITIVE)) {
David Zeuthene8ed8632014-07-24 13:38:10 -0400487 LOG(ERROR) << "Unexpected end element '" << element
488 << "' with current_path='" << data->current_path << "'";
489 data->failed = true;
490 return;
491 }
492 data->current_path.resize(data->current_path.size() - path_suffix.size());
493}
494
David Zeuthenf3e28012014-08-26 18:23:52 -0400495// Callback function invoked by expat.
496//
497// This is called for entity declarations. Since Omaha is guaranteed
498// to never return any XML with entities our course of action is to
499// just stop parsing. This avoids potential resource exhaustion
500// problems AKA the "billion laughs". CVE-2013-0340.
501void ParserHandlerEntityDecl(void *user_data,
502 const XML_Char *entity_name,
503 int is_parameter_entity,
504 const XML_Char *value,
505 int value_length,
506 const XML_Char *base,
507 const XML_Char *system_id,
508 const XML_Char *public_id,
509 const XML_Char *notation_name) {
510 OmahaParserData* data = reinterpret_cast<OmahaParserData*>(user_data);
511
512 LOG(ERROR) << "XML entities are not supported. Aborting parsing.";
513 data->failed = true;
514 data->entity_decl = true;
515 XML_StopParser(data->xml_parser, false);
516}
517
David Zeuthene8ed8632014-07-24 13:38:10 -0400518} // namespace
519
Alex Deymob0d74eb2015-03-30 17:59:17 -0700520bool XmlEncode(const string& input, string* output) {
521 if (std::find_if(input.begin(), input.end(),
522 [](const char c){return c & 0x80;}) != input.end()) {
523 LOG(WARNING) << "Invalid ASCII-7 string passed to the XML encoder:";
524 utils::HexDumpString(input);
525 return false;
526 }
Alex Deymocc457852015-06-18 18:35:50 -0700527 output->clear();
528 // We need at least input.size() space in the output, but the code below will
529 // handle it if we need more.
530 output->reserve(input.size());
531 for (char c : input) {
532 switch (c) {
533 case '\"':
534 output->append("&quot;");
535 break;
536 case '\'':
537 output->append("&apos;");
538 break;
539 case '&':
540 output->append("&amp;");
541 break;
542 case '<':
543 output->append("&lt;");
544 break;
545 case '>':
546 output->append("&gt;");
547 break;
548 default:
549 output->push_back(c);
550 }
551 }
Alex Deymob0d74eb2015-03-30 17:59:17 -0700552 return true;
553}
554
555string XmlEncodeWithDefault(const string& input, const string& default_value) {
556 string output;
557 if (XmlEncode(input, &output))
558 return output;
559 return default_value;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000560}
561
Alex Deymoc1c17b42015-11-23 03:53:15 -0300562OmahaRequestAction::OmahaRequestAction(
563 SystemState* system_state,
564 OmahaEvent* event,
565 std::unique_ptr<HttpFetcher> http_fetcher,
566 bool ping_only)
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800567 : system_state_(system_state),
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700568 event_(event),
Alex Deymoc1c17b42015-11-23 03:53:15 -0300569 http_fetcher_(std::move(http_fetcher)),
Thieu Le116fda32011-04-19 11:01:54 -0700570 ping_only_(ping_only),
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700571 ping_active_days_(0),
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700572 ping_roll_call_days_(0) {
573 params_ = system_state->request_params();
574}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000575
Darin Petkov6a5b3222010-07-13 14:55:28 -0700576OmahaRequestAction::~OmahaRequestAction() {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000577
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700578// Calculates the value to use for the ping days parameter.
579int OmahaRequestAction::CalculatePingDays(const string& key) {
580 int days = kNeverPinged;
581 int64_t last_ping = 0;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800582 if (system_state_->prefs()->GetInt64(key, &last_ping) && last_ping >= 0) {
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700583 days = (Time::Now() - Time::FromInternalValue(last_ping)).InDays();
584 if (days < 0) {
585 // If |days| is negative, then the system clock must have jumped
586 // back in time since the ping was sent. Mark the value so that
587 // it doesn't get sent to the server but we still update the
588 // last ping daystart preference. This way the next ping time
589 // will be correct, hopefully.
590 days = kPingTimeJump;
591 LOG(WARNING) <<
592 "System clock jumped back in time. Resetting ping daystarts.";
593 }
594 }
595 return days;
596}
597
598void OmahaRequestAction::InitPingDays() {
599 // We send pings only along with update checks, not with events.
600 if (IsEvent()) {
601 return;
602 }
603 // TODO(petkov): Figure a way to distinguish active use pings
604 // vs. roll call pings. Currently, the two pings are identical. A
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700605 // fix needs to change this code as well as UpdateLastPingDays and ShouldPing.
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700606 ping_active_days_ = CalculatePingDays(kPrefsLastActivePingDay);
607 ping_roll_call_days_ = CalculatePingDays(kPrefsLastRollCallPingDay);
608}
609
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700610bool OmahaRequestAction::ShouldPing() const {
611 if (ping_active_days_ == OmahaRequestAction::kNeverPinged &&
612 ping_roll_call_days_ == OmahaRequestAction::kNeverPinged) {
613 int powerwash_count = system_state_->hardware()->GetPowerwashCount();
614 if (powerwash_count > 0) {
615 LOG(INFO) << "Not sending ping with a=-1 r=-1 to omaha because "
616 << "powerwash_count is " << powerwash_count;
617 return false;
618 }
619 return true;
620 }
621 return ping_active_days_ > 0 || ping_roll_call_days_ > 0;
622}
623
David Zeuthen639aa362014-02-03 16:23:44 -0800624// static
625int OmahaRequestAction::GetInstallDate(SystemState* system_state) {
626 PrefsInterface* prefs = system_state->prefs();
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700627 if (prefs == nullptr)
David Zeuthen639aa362014-02-03 16:23:44 -0800628 return -1;
629
630 // If we have the value stored on disk, just return it.
631 int64_t stored_value;
632 if (prefs->GetInt64(kPrefsInstallDateDays, &stored_value)) {
633 // Convert and sanity-check.
634 int install_date_days = static_cast<int>(stored_value);
635 if (install_date_days >= 0)
636 return install_date_days;
637 LOG(ERROR) << "Dropping stored Omaha InstallData since its value num_days="
638 << install_date_days << " looks suspicious.";
639 prefs->Delete(kPrefsInstallDateDays);
640 }
641
642 // Otherwise, if OOBE is not complete then do nothing and wait for
643 // ParseResponse() to call ParseInstallDate() and then
644 // PersistInstallDate() to set the kPrefsInstallDateDays state
645 // variable. Once that is done, we'll then report back in future
646 // Omaha requests. This works exactly because OOBE triggers an
647 // update check.
648 //
649 // However, if OOBE is complete and the kPrefsInstallDateDays state
650 // variable is not set, there are two possibilities
651 //
652 // 1. The update check in OOBE failed so we never got a response
653 // from Omaha (no network etc.); or
654 //
655 // 2. OOBE was done on an older version that didn't write to the
656 // kPrefsInstallDateDays state variable.
657 //
658 // In both cases, we approximate the install date by simply
659 // inspecting the timestamp of when OOBE happened.
660
661 Time time_of_oobe;
Alex Deymo46a9aae2016-05-04 20:20:11 -0700662 if (!system_state->hardware()->IsOOBEEnabled() ||
663 !system_state->hardware()->IsOOBEComplete(&time_of_oobe)) {
David Zeuthen639aa362014-02-03 16:23:44 -0800664 LOG(INFO) << "Not generating Omaha InstallData as we have "
Alex Deymo46a9aae2016-05-04 20:20:11 -0700665 << "no prefs file and OOBE is not complete or not enabled.";
David Zeuthen639aa362014-02-03 16:23:44 -0800666 return -1;
667 }
668
669 int num_days;
670 if (!utils::ConvertToOmahaInstallDate(time_of_oobe, &num_days)) {
671 LOG(ERROR) << "Not generating Omaha InstallData from time of OOBE "
672 << "as its value '" << utils::ToString(time_of_oobe)
673 << "' looks suspicious.";
674 return -1;
675 }
676
677 // Persist this to disk, for future use.
678 if (!OmahaRequestAction::PersistInstallDate(system_state,
679 num_days,
680 kProvisionedFromOOBEMarker))
681 return -1;
682
683 LOG(INFO) << "Set the Omaha InstallDate from OOBE time-stamp to "
684 << num_days << " days";
685
686 return num_days;
687}
688
Darin Petkov6a5b3222010-07-13 14:55:28 -0700689void OmahaRequestAction::PerformAction() {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000690 http_fetcher_->set_delegate(this);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700691 InitPingDays();
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700692 if (ping_only_ && !ShouldPing()) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700693 processor_->ActionComplete(this, ErrorCode::kSuccess);
Thieu Leb44e9e82011-06-06 14:34:04 -0700694 return;
695 }
David Zeuthen639aa362014-02-03 16:23:44 -0800696
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700697 string request_post(GetRequestXml(event_.get(),
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700698 params_,
Thieu Le116fda32011-04-19 11:01:54 -0700699 ping_only_,
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700700 ShouldPing(), // include_ping
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700701 ping_active_days_,
Darin Petkov95508da2011-01-05 12:42:29 -0800702 ping_roll_call_days_,
David Zeuthen639aa362014-02-03 16:23:44 -0800703 GetInstallDate(system_state_),
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700704 system_state_));
Jay Srinivasan0a708742012-03-20 11:26:12 -0700705
Alex Deymo14ad88e2016-06-29 12:30:14 -0700706 // Set X-GoogleUpdate headers.
707 http_fetcher_->SetHeader(kXGoogleUpdateInteractivity,
708 params_->interactive() ? "fg" : "bg");
709 http_fetcher_->SetHeader(kXGoogleUpdateAppId, params_->GetAppId());
710 http_fetcher_->SetHeader(
711 kXGoogleUpdateUpdater,
712 base::StringPrintf(
713 "%s-%s", constants::kOmahaUpdaterID, kOmahaUpdaterVersion));
714
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -0800715 http_fetcher_->SetPostData(request_post.data(), request_post.size(),
716 kHttpContentTypeTextXml);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700717 LOG(INFO) << "Posting an Omaha request to " << params_->update_url();
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700718 LOG(INFO) << "Request: " << request_post;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700719 http_fetcher_->BeginTransfer(params_->update_url());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000720}
721
Darin Petkov6a5b3222010-07-13 14:55:28 -0700722void OmahaRequestAction::TerminateProcessing() {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000723 http_fetcher_->TerminateTransfer();
724}
725
726// We just store the response in the buffer. Once we've received all bytes,
727// we'll look in the buffer and decide what to do.
Darin Petkov6a5b3222010-07-13 14:55:28 -0700728void OmahaRequestAction::ReceivedBytes(HttpFetcher *fetcher,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800729 const void* bytes,
730 size_t length) {
731 const uint8_t* byte_ptr = reinterpret_cast<const uint8_t*>(bytes);
732 response_buffer_.insert(response_buffer_.end(), byte_ptr, byte_ptr + length);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000733}
734
735namespace {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000736
737// Parses a 64 bit base-10 int from a string and returns it. Returns 0
738// on error. If the string contains "0", that's indistinguishable from
739// error.
740off_t ParseInt(const string& str) {
741 off_t ret = 0;
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700742 int rc = sscanf(str.c_str(), "%" PRIi64, &ret); // NOLINT(runtime/printf)
rspangler@google.com49fdf182009-10-10 00:57:34 +0000743 if (rc < 1) {
744 // failure
745 return 0;
746 }
747 return ret;
748}
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700749
David Zeuthene8ed8632014-07-24 13:38:10 -0400750// Parses |str| and returns |true| if, and only if, its value is "true".
751bool ParseBool(const string& str) {
752 return str == "true";
753}
754
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700755// Update the last ping day preferences based on the server daystart
756// response. Returns true on success, false otherwise.
David Zeuthene8ed8632014-07-24 13:38:10 -0400757bool UpdateLastPingDays(OmahaParserData *parser_data, PrefsInterface* prefs) {
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700758 int64_t elapsed_seconds = 0;
David Zeuthene8ed8632014-07-24 13:38:10 -0400759 TEST_AND_RETURN_FALSE(
760 base::StringToInt64(parser_data->daystart_elapsed_seconds,
761 &elapsed_seconds));
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700762 TEST_AND_RETURN_FALSE(elapsed_seconds >= 0);
763
764 // Remember the local time that matches the server's last midnight
765 // time.
766 Time daystart = Time::Now() - TimeDelta::FromSeconds(elapsed_seconds);
767 prefs->SetInt64(kPrefsLastActivePingDay, daystart.ToInternalValue());
768 prefs->SetInt64(kPrefsLastRollCallPingDay, daystart.ToInternalValue());
769 return true;
770}
Sen Jiang81259682017-03-30 15:11:30 -0700771
772// Parses the package node in the given XML document and populates
773// |output_object| if valid. Returns true if we should continue the parsing.
774// False otherwise, in which case it sets any error code using |completer|.
775bool ParsePackage(OmahaParserData::App* app,
776 OmahaResponse* output_object,
777 ScopedActionCompleter* completer) {
778 if (app->updatecheck_status == "noupdate") {
779 if (!app->packages.empty()) {
780 LOG(ERROR) << "No update in this <app> but <package> is not empty.";
781 completer->set_code(ErrorCode::kOmahaResponseInvalid);
782 return false;
783 }
784 return true;
785 }
786 if (app->packages.empty()) {
787 LOG(ERROR) << "Omaha Response has no packages";
788 completer->set_code(ErrorCode::kOmahaResponseInvalid);
789 return false;
790 }
791 if (app->url_codebase.empty()) {
792 LOG(ERROR) << "No Omaha Response URLs";
793 completer->set_code(ErrorCode::kOmahaResponseInvalid);
794 return false;
795 }
796 LOG(INFO) << "Found " << app->url_codebase.size() << " url(s)";
797 vector<string> metadata_sizes =
798 base::SplitString(app->action_postinstall_attrs[kTagMetadataSize],
799 ":",
800 base::TRIM_WHITESPACE,
801 base::SPLIT_WANT_ALL);
802 vector<string> metadata_signatures =
803 base::SplitString(app->action_postinstall_attrs[kTagMetadataSignatureRsa],
804 ":",
805 base::TRIM_WHITESPACE,
806 base::SPLIT_WANT_ALL);
807 for (size_t i = 0; i < app->packages.size(); i++) {
808 const auto& package = app->packages[i];
809 if (package.name.empty()) {
810 LOG(ERROR) << "Omaha Response has empty package name";
811 completer->set_code(ErrorCode::kOmahaResponseInvalid);
812 return false;
813 }
814 LOG(INFO) << "Found package " << package.name;
815
816 OmahaResponse::Package out_package;
817 for (const string& codebase : app->url_codebase) {
818 if (codebase.empty()) {
819 LOG(ERROR) << "Omaha Response URL has empty codebase";
820 completer->set_code(ErrorCode::kOmahaResponseInvalid);
821 return false;
822 }
823 out_package.payload_urls.push_back(codebase + package.name);
824 }
825 // Parse the payload size.
826 base::StringToUint64(package.size, &out_package.size);
827 if (out_package.size <= 0) {
828 LOG(ERROR) << "Omaha Response has invalid payload size: " << package.size;
829 completer->set_code(ErrorCode::kOmahaResponseInvalid);
830 return false;
831 }
832 LOG(INFO) << "Payload size = " << out_package.size << " bytes";
833
834 if (i < metadata_sizes.size())
835 base::StringToUint64(metadata_sizes[i], &out_package.metadata_size);
836 LOG(INFO) << "Payload metadata size = " << out_package.metadata_size
837 << " bytes";
838
839 if (i < metadata_signatures.size())
840 out_package.metadata_signature = metadata_signatures[i];
841 LOG(INFO) << "Payload metadata signature = "
842 << out_package.metadata_signature;
843
844 out_package.hash = package.hash;
845 if (out_package.hash.empty()) {
846 LOG(ERROR) << "Omaha Response has empty hash_sha256 value";
847 completer->set_code(ErrorCode::kOmahaResponseInvalid);
848 return false;
849 }
850 LOG(INFO) << "Payload hash = " << out_package.hash;
851 output_object->packages.push_back(std::move(out_package));
852 }
853
854 return true;
855}
856
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700857} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000858
David Zeuthene8ed8632014-07-24 13:38:10 -0400859bool OmahaRequestAction::ParseResponse(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700860 OmahaResponse* output_object,
861 ScopedActionCompleter* completer) {
Sen Jiang81259682017-03-30 15:11:30 -0700862 if (parser_data->apps.empty()) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700863 completer->set_code(ErrorCode::kOmahaResponseInvalid);
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700864 return false;
865 }
Sen Jiang81259682017-03-30 15:11:30 -0700866 LOG(INFO) << "Found " << parser_data->apps.size() << " <app>.";
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700867
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800868 // chromium-os:37289: The PollInterval is not supported by Omaha server
869 // currently. But still keeping this existing code in case we ever decide to
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700870 // slow down the request rate from the server-side. Note that the PollInterval
871 // is not persisted, so it has to be sent by the server on every response to
872 // guarantee that the scheduler uses this value (otherwise, if the device got
873 // rebooted after the last server-indicated value, it'll revert to the default
874 // value). Also kDefaultMaxUpdateChecks value for the scattering logic is
875 // based on the assumption that we perform an update check every hour so that
876 // the max value of 8 will roughly be equivalent to one work day. If we decide
877 // to use PollInterval permanently, we should update the
878 // max_update_checks_allowed to take PollInterval into account. Note: The
879 // parsing for PollInterval happens even before parsing of the status because
880 // we may want to specify the PollInterval even when there's no update.
David Zeuthene8ed8632014-07-24 13:38:10 -0400881 base::StringToInt(parser_data->updatecheck_poll_interval,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700882 &output_object->poll_interval);
883
David Zeuthen639aa362014-02-03 16:23:44 -0800884 // Check for the "elapsed_days" attribute in the "daystart"
885 // element. This is the number of days since Jan 1 2007, 0:00
886 // PST. If we don't have a persisted value of the Omaha InstallDate,
887 // we'll use it to calculate it and then persist it.
David Zeuthene8ed8632014-07-24 13:38:10 -0400888 if (ParseInstallDate(parser_data, output_object) &&
889 !HasInstallDate(system_state_)) {
David Zeuthen639aa362014-02-03 16:23:44 -0800890 // Since output_object->install_date_days is never negative, the
891 // elapsed_days -> install-date calculation is reduced to simply
892 // rounding down to the nearest number divisible by 7.
893 int remainder = output_object->install_date_days % 7;
894 int install_date_days_rounded =
895 output_object->install_date_days - remainder;
896 if (PersistInstallDate(system_state_,
897 install_date_days_rounded,
898 kProvisionedFromOmahaResponse)) {
899 LOG(INFO) << "Set the Omaha InstallDate from Omaha Response to "
900 << install_date_days_rounded << " days";
901 }
902 }
903
Alex Deymo00d79ac2015-06-29 15:41:49 -0700904 // We persist the cohorts sent by omaha even if the status is "noupdate".
905 if (parser_data->app_cohort_set)
906 PersistCohortData(kPrefsOmahaCohort, parser_data->app_cohort);
907 if (parser_data->app_cohorthint_set)
908 PersistCohortData(kPrefsOmahaCohortHint, parser_data->app_cohorthint);
909 if (parser_data->app_cohortname_set)
910 PersistCohortData(kPrefsOmahaCohortName, parser_data->app_cohortname);
911
Alex Deymob3fa53b2016-04-18 19:57:58 -0700912 // Parse the updatecheck attributes.
913 PersistEolStatus(parser_data->updatecheck_attrs);
914
David Zeuthene8ed8632014-07-24 13:38:10 -0400915 if (!ParseStatus(parser_data, output_object, completer))
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700916 return false;
917
David Zeuthene8ed8632014-07-24 13:38:10 -0400918 if (!ParseParams(parser_data, output_object, completer))
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700919 return false;
920
Sen Jiang0affc2c2017-02-10 15:55:05 -0800921 // Package has to be parsed after Params now because ParseParams need to make
922 // sure that postinstall action exists.
Sen Jiang81259682017-03-30 15:11:30 -0700923 for (auto& app : parser_data->apps)
924 if (!ParsePackage(&app, output_object, completer))
925 return false;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800926
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700927 return true;
928}
929
David Zeuthene8ed8632014-07-24 13:38:10 -0400930bool OmahaRequestAction::ParseStatus(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700931 OmahaResponse* output_object,
932 ScopedActionCompleter* completer) {
Sen Jiang81259682017-03-30 15:11:30 -0700933 output_object->update_exists = false;
934 for (size_t i = 0; i < parser_data->apps.size(); i++) {
935 const string& status = parser_data->apps[i].updatecheck_status;
936 if (status == "noupdate") {
937 LOG(INFO) << "No update for <app> " << i;
938 } else if (status == "ok") {
939 output_object->update_exists = true;
940 } else {
941 LOG(ERROR) << "Unknown Omaha response status: " << status;
942 completer->set_code(ErrorCode::kOmahaResponseInvalid);
943 return false;
944 }
945 }
946 if (!output_object->update_exists) {
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700947 SetOutputObject(*output_object);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700948 completer->set_code(ErrorCode::kSuccess);
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700949 }
950
Sen Jiang81259682017-03-30 15:11:30 -0700951 return output_object->update_exists;
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700952}
953
David Zeuthene8ed8632014-07-24 13:38:10 -0400954bool OmahaRequestAction::ParseParams(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700955 OmahaResponse* output_object,
956 ScopedActionCompleter* completer) {
Sen Jiang81259682017-03-30 15:11:30 -0700957 map<string, string> attrs;
958 for (auto& app : parser_data->apps) {
959 if (!app.manifest_version.empty() && output_object->version.empty())
960 output_object->version = app.manifest_version;
961 if (!app.action_postinstall_attrs.empty() && attrs.empty())
962 attrs = app.action_postinstall_attrs;
963 }
Chris Sosa3b748432013-06-20 16:42:59 -0700964 if (output_object->version.empty()) {
Chris Sosaaa18e162013-06-20 13:20:30 -0700965 LOG(ERROR) << "Omaha Response does not have version in manifest!";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700966 completer->set_code(ErrorCode::kOmahaResponseInvalid);
Chris Sosa3b748432013-06-20 16:42:59 -0700967 return false;
968 }
969
970 LOG(INFO) << "Received omaha response to update to version "
971 << output_object->version;
972
David Zeuthene8ed8632014-07-24 13:38:10 -0400973 if (attrs.empty()) {
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700974 LOG(ERROR) << "Omaha Response has no postinstall event action";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700975 completer->set_code(ErrorCode::kOmahaResponseInvalid);
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700976 return false;
977 }
978
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800979 // Get the optional properties one by one.
David Zeuthene8ed8632014-07-24 13:38:10 -0400980 output_object->more_info_url = attrs[kTagMoreInfo];
David Zeuthene8ed8632014-07-24 13:38:10 -0400981 output_object->prompt = ParseBool(attrs[kTagPrompt]);
982 output_object->deadline = attrs[kTagDeadline];
983 output_object->max_days_to_scatter = ParseInt(attrs[kTagMaxDaysToScatter]);
David Zeuthen8f191b22013-08-06 12:27:50 -0700984 output_object->disable_p2p_for_downloading =
David Zeuthene8ed8632014-07-24 13:38:10 -0400985 ParseBool(attrs[kTagDisableP2PForDownloading]);
David Zeuthen8f191b22013-08-06 12:27:50 -0700986 output_object->disable_p2p_for_sharing =
David Zeuthene8ed8632014-07-24 13:38:10 -0400987 ParseBool(attrs[kTagDisableP2PForSharing]);
988 output_object->public_key_rsa = attrs[kTagPublicKeyRsa];
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800989
David Zeuthene8ed8632014-07-24 13:38:10 -0400990 string max = attrs[kTagMaxFailureCountPerUrl];
Jay Srinivasan08262882012-12-28 19:29:43 -0800991 if (!base::StringToUint(max, &output_object->max_failure_count_per_url))
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800992 output_object->max_failure_count_per_url = kDefaultMaxFailureCountPerUrl;
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700993
David Zeuthene8ed8632014-07-24 13:38:10 -0400994 output_object->is_delta_payload = ParseBool(attrs[kTagIsDeltaPayload]);
Jay Srinivasan08262882012-12-28 19:29:43 -0800995
996 output_object->disable_payload_backoff =
David Zeuthene8ed8632014-07-24 13:38:10 -0400997 ParseBool(attrs[kTagDisablePayloadBackoff]);
Jay Srinivasan08262882012-12-28 19:29:43 -0800998
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700999 return true;
1000}
1001
David Zeuthene8ed8632014-07-24 13:38:10 -04001002// If the transfer was successful, this uses expat to parse the response
rspangler@google.com49fdf182009-10-10 00:57:34 +00001003// and fill in the appropriate fields of the output object. Also, notifies
1004// the processor that we're done.
Darin Petkov6a5b3222010-07-13 14:55:28 -07001005void OmahaRequestAction::TransferComplete(HttpFetcher *fetcher,
1006 bool successful) {
rspangler@google.com49fdf182009-10-10 00:57:34 +00001007 ScopedActionCompleter completer(processor_, this);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001008 string current_response(response_buffer_.begin(), response_buffer_.end());
1009 LOG(INFO) << "Omaha request response: " << current_response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001010
Gilad Arnold74b5f552014-10-07 08:17:16 -07001011 PayloadStateInterface* const payload_state = system_state_->payload_state();
1012
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001013 // Events are best effort transactions -- assume they always succeed.
1014 if (IsEvent()) {
1015 CHECK(!HasOutputPipe()) << "No output pipe allowed for event requests.";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001016 completer.set_code(ErrorCode::kSuccess);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001017 return;
1018 }
1019
Andrew de los Reyesf98bff82010-05-06 13:33:25 -07001020 if (!successful) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001021 LOG(ERROR) << "Omaha request network transfer failed.";
Darin Petkovedc522e2010-11-05 09:35:17 -07001022 int code = GetHTTPResponseCode();
1023 // Makes sure we send sane error values.
1024 if (code < 0 || code >= 1000) {
1025 code = 999;
1026 }
David Zeuthena99981f2013-04-29 13:42:47 -07001027 completer.set_code(static_cast<ErrorCode>(
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001028 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + code));
rspangler@google.com49fdf182009-10-10 00:57:34 +00001029 return;
Andrew de los Reyesf98bff82010-05-06 13:33:25 -07001030 }
rspangler@google.com49fdf182009-10-10 00:57:34 +00001031
David Zeuthene8ed8632014-07-24 13:38:10 -04001032 XML_Parser parser = XML_ParserCreate(nullptr);
David Zeuthenf3e28012014-08-26 18:23:52 -04001033 OmahaParserData parser_data(parser);
David Zeuthene8ed8632014-07-24 13:38:10 -04001034 XML_SetUserData(parser, &parser_data);
1035 XML_SetElementHandler(parser, ParserHandlerStart, ParserHandlerEnd);
David Zeuthenf3e28012014-08-26 18:23:52 -04001036 XML_SetEntityDeclHandler(parser, ParserHandlerEntityDecl);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001037 XML_Status res = XML_Parse(
1038 parser,
1039 reinterpret_cast<const char*>(response_buffer_.data()),
1040 response_buffer_.size(),
1041 XML_TRUE);
David Zeuthene8ed8632014-07-24 13:38:10 -04001042 XML_ParserFree(parser);
1043
1044 if (res != XML_STATUS_OK || parser_data.failed) {
Alex Deymoa9bb7dc2015-06-19 09:50:23 -07001045 LOG(ERROR) << "Omaha response not valid XML: "
1046 << XML_ErrorString(XML_GetErrorCode(parser))
1047 << " at line " << XML_GetCurrentLineNumber(parser)
1048 << " col " << XML_GetCurrentColumnNumber(parser);
David Zeuthenf3e28012014-08-26 18:23:52 -04001049 ErrorCode error_code = ErrorCode::kOmahaRequestXMLParseError;
1050 if (response_buffer_.empty()) {
1051 error_code = ErrorCode::kOmahaRequestEmptyResponseError;
1052 } else if (parser_data.entity_decl) {
1053 error_code = ErrorCode::kOmahaRequestXMLHasEntityDecl;
1054 }
1055 completer.set_code(error_code);
rspangler@google.com49fdf182009-10-10 00:57:34 +00001056 return;
1057 }
1058
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001059 // Update the last ping day preferences based on the server daystart response
1060 // even if we didn't send a ping. Omaha always includes the daystart in the
1061 // response, but log the error if it didn't.
1062 LOG_IF(ERROR, !UpdateLastPingDays(&parser_data, system_state_->prefs()))
1063 << "Failed to update the last ping day preferences!";
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001064
Thieu Le116fda32011-04-19 11:01:54 -07001065 if (!HasOutputPipe()) {
1066 // Just set success to whether or not the http transfer succeeded,
1067 // which must be true at this point in the code.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001068 completer.set_code(ErrorCode::kSuccess);
Thieu Le116fda32011-04-19 11:01:54 -07001069 return;
1070 }
1071
Darin Petkov6a5b3222010-07-13 14:55:28 -07001072 OmahaResponse output_object;
David Zeuthene8ed8632014-07-24 13:38:10 -04001073 if (!ParseResponse(&parser_data, &output_object, &completer))
rspangler@google.com49fdf182009-10-10 00:57:34 +00001074 return;
David Zeuthen8f191b22013-08-06 12:27:50 -07001075 output_object.update_exists = true;
1076 SetOutputObject(output_object);
rspangler@google.com49fdf182009-10-10 00:57:34 +00001077
Tao Bao5688d162017-06-06 13:09:06 -07001078 if (ShouldIgnoreUpdate(output_object)) {
1079 output_object.update_exists = false;
1080 completer.set_code(ErrorCode::kOmahaUpdateIgnoredPerPolicy);
Jay Srinivasan0a708742012-03-20 11:26:12 -07001081 return;
1082 }
1083
David Zeuthen8f191b22013-08-06 12:27:50 -07001084 // If Omaha says to disable p2p, respect that
1085 if (output_object.disable_p2p_for_downloading) {
1086 LOG(INFO) << "Forcibly disabling use of p2p for downloading as "
1087 << "requested by Omaha.";
Gilad Arnold74b5f552014-10-07 08:17:16 -07001088 payload_state->SetUsingP2PForDownloading(false);
David Zeuthen8f191b22013-08-06 12:27:50 -07001089 }
1090 if (output_object.disable_p2p_for_sharing) {
1091 LOG(INFO) << "Forcibly disabling use of p2p for sharing as "
1092 << "requested by Omaha.";
Gilad Arnold74b5f552014-10-07 08:17:16 -07001093 payload_state->SetUsingP2PForSharing(false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001094 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001095
1096 // Update the payload state with the current response. The payload state
1097 // will automatically reset all stale state if this response is different
Jay Srinivasan08262882012-12-28 19:29:43 -08001098 // from what's stored already. We are updating the payload state as late
1099 // as possible in this method so that if a new release gets pushed and then
1100 // got pulled back due to some issues, we don't want to clear our internal
1101 // state unnecessarily.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001102 payload_state->SetResponse(output_object);
Jay Srinivasan08262882012-12-28 19:29:43 -08001103
David Zeuthen8f191b22013-08-06 12:27:50 -07001104 // It could be we've already exceeded the deadline for when p2p is
1105 // allowed or that we've tried too many times with p2p. Check that.
Gilad Arnold74b5f552014-10-07 08:17:16 -07001106 if (payload_state->GetUsingP2PForDownloading()) {
David Zeuthen8f191b22013-08-06 12:27:50 -07001107 payload_state->P2PNewAttempt();
1108 if (!payload_state->P2PAttemptAllowed()) {
1109 LOG(INFO) << "Forcibly disabling use of p2p for downloading because "
1110 << "of previous failures when using p2p.";
Gilad Arnold74b5f552014-10-07 08:17:16 -07001111 payload_state->SetUsingP2PForDownloading(false);
David Zeuthen8f191b22013-08-06 12:27:50 -07001112 }
1113 }
1114
1115 // From here on, we'll complete stuff in CompleteProcessing() so
1116 // disable |completer| since we'll create a new one in that
1117 // function.
1118 completer.set_should_complete(false);
1119
1120 // If we're allowed to use p2p for downloading we do not pay
1121 // attention to wall-clock-based waiting if the URL is indeed
1122 // available via p2p. Therefore, check if the file is available via
1123 // p2p before deferring...
Gilad Arnold74b5f552014-10-07 08:17:16 -07001124 if (payload_state->GetUsingP2PForDownloading()) {
David Zeuthen8f191b22013-08-06 12:27:50 -07001125 LookupPayloadViaP2P(output_object);
1126 } else {
1127 CompleteProcessing();
1128 }
1129}
1130
1131void OmahaRequestAction::CompleteProcessing() {
1132 ScopedActionCompleter completer(processor_, this);
1133 OmahaResponse& output_object = const_cast<OmahaResponse&>(GetOutputObject());
1134 PayloadStateInterface* payload_state = system_state_->payload_state();
1135
Alex Deymo46a9aae2016-05-04 20:20:11 -07001136 if (system_state_->hardware()->IsOOBEEnabled() &&
1137 !system_state_->hardware()->IsOOBEComplete(nullptr) &&
Kevin Cernekeec5081a82016-04-08 12:29:52 -07001138 output_object.deadline.empty() &&
1139 params_->app_version() != "ForcedUpdate") {
Kevin Cernekee2494e282016-03-29 18:03:53 -07001140 output_object.update_exists = false;
1141 LOG(INFO) << "Ignoring non-critical Omaha updates until OOBE is done.";
1142 completer.set_code(ErrorCode::kNonCriticalUpdateInOOBE);
1143 return;
1144 }
1145
David Zeuthen8f191b22013-08-06 12:27:50 -07001146 if (ShouldDeferDownload(&output_object)) {
Jay Srinivasan08262882012-12-28 19:29:43 -08001147 output_object.update_exists = false;
David Zeuthen8f191b22013-08-06 12:27:50 -07001148 LOG(INFO) << "Ignoring Omaha updates as updates are deferred by policy.";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001149 completer.set_code(ErrorCode::kOmahaUpdateDeferredPerPolicy);
Jay Srinivasan08262882012-12-28 19:29:43 -08001150 return;
1151 }
David Zeuthen8f191b22013-08-06 12:27:50 -07001152
Chris Sosa20f005c2013-09-05 13:53:08 -07001153 if (payload_state->ShouldBackoffDownload()) {
1154 output_object.update_exists = false;
1155 LOG(INFO) << "Ignoring Omaha updates in order to backoff our retry "
1156 << "attempts";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001157 completer.set_code(ErrorCode::kOmahaUpdateDeferredForBackoff);
Chris Sosa20f005c2013-09-05 13:53:08 -07001158 return;
David Zeuthen8f191b22013-08-06 12:27:50 -07001159 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001160 completer.set_code(ErrorCode::kSuccess);
David Zeuthen8f191b22013-08-06 12:27:50 -07001161}
1162
1163void OmahaRequestAction::OnLookupPayloadViaP2PCompleted(const string& url) {
1164 LOG(INFO) << "Lookup complete, p2p-client returned URL '" << url << "'";
1165 if (!url.empty()) {
Gilad Arnold74b5f552014-10-07 08:17:16 -07001166 system_state_->payload_state()->SetP2PUrl(url);
David Zeuthen8f191b22013-08-06 12:27:50 -07001167 } else {
1168 LOG(INFO) << "Forcibly disabling use of p2p for downloading "
1169 << "because no suitable peer could be found.";
Gilad Arnold74b5f552014-10-07 08:17:16 -07001170 system_state_->payload_state()->SetUsingP2PForDownloading(false);
David Zeuthen8f191b22013-08-06 12:27:50 -07001171 }
1172 CompleteProcessing();
1173}
1174
1175void OmahaRequestAction::LookupPayloadViaP2P(const OmahaResponse& response) {
David Zeuthen41996ad2013-09-24 15:43:24 -07001176 // If the device is in the middle of an update, the state variables
1177 // kPrefsUpdateStateNextDataOffset, kPrefsUpdateStateNextDataLength
1178 // tracks the offset and length of the operation currently in
1179 // progress. The offset is based from the end of the manifest which
1180 // is kPrefsManifestMetadataSize bytes long.
1181 //
1182 // To make forward progress and avoid deadlocks, we need to find a
1183 // peer that has at least the entire operation we're currently
1184 // working on. Otherwise we may end up in a situation where two
1185 // devices bounce back and forth downloading from each other,
1186 // neither making any forward progress until one of them decides to
1187 // stop using p2p (via kMaxP2PAttempts and kMaxP2PAttemptTimeSeconds
1188 // safe-guards). See http://crbug.com/297170 for an example)
David Zeuthen8f191b22013-08-06 12:27:50 -07001189 size_t minimum_size = 0;
David Zeuthen41996ad2013-09-24 15:43:24 -07001190 int64_t manifest_metadata_size = 0;
Alex Deymof25eb492016-02-26 00:20:08 -08001191 int64_t manifest_signature_size = 0;
David Zeuthen41996ad2013-09-24 15:43:24 -07001192 int64_t next_data_offset = 0;
1193 int64_t next_data_length = 0;
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001194 if (system_state_ &&
David Zeuthen41996ad2013-09-24 15:43:24 -07001195 system_state_->prefs()->GetInt64(kPrefsManifestMetadataSize,
1196 &manifest_metadata_size) &&
1197 manifest_metadata_size != -1 &&
Alex Deymof25eb492016-02-26 00:20:08 -08001198 system_state_->prefs()->GetInt64(kPrefsManifestSignatureSize,
1199 &manifest_signature_size) &&
1200 manifest_signature_size != -1 &&
David Zeuthen8f191b22013-08-06 12:27:50 -07001201 system_state_->prefs()->GetInt64(kPrefsUpdateStateNextDataOffset,
David Zeuthen41996ad2013-09-24 15:43:24 -07001202 &next_data_offset) &&
1203 next_data_offset != -1 &&
1204 system_state_->prefs()->GetInt64(kPrefsUpdateStateNextDataLength,
1205 &next_data_length)) {
Alex Deymof25eb492016-02-26 00:20:08 -08001206 minimum_size = manifest_metadata_size + manifest_signature_size +
1207 next_data_offset + next_data_length;
David Zeuthen8f191b22013-08-06 12:27:50 -07001208 }
1209
Sen Jiang0affc2c2017-02-10 15:55:05 -08001210 // TODO(senj): Fix P2P for multiple package.
Sen Jiang2703ef42017-03-16 13:36:21 -07001211 brillo::Blob raw_hash;
Sen Jiang0affc2c2017-02-10 15:55:05 -08001212 if (!base::HexStringToBytes(response.packages[0].hash, &raw_hash))
Sen Jiang2703ef42017-03-16 13:36:21 -07001213 return;
Sen Jiang0affc2c2017-02-10 15:55:05 -08001214 string file_id =
1215 utils::CalculateP2PFileId(raw_hash, response.packages[0].size);
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001216 if (system_state_->p2p_manager()) {
Sen Jiang2703ef42017-03-16 13:36:21 -07001217 LOG(INFO) << "Checking if payload is available via p2p, file_id=" << file_id
1218 << " minimum_size=" << minimum_size;
David Zeuthen8f191b22013-08-06 12:27:50 -07001219 system_state_->p2p_manager()->LookupUrlForFile(
1220 file_id,
1221 minimum_size,
David Zeuthen4cc5ed22014-01-15 12:35:03 -08001222 TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds),
David Zeuthen8f191b22013-08-06 12:27:50 -07001223 base::Bind(&OmahaRequestAction::OnLookupPayloadViaP2PCompleted,
1224 base::Unretained(this)));
1225 }
rspangler@google.com49fdf182009-10-10 00:57:34 +00001226}
1227
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001228bool OmahaRequestAction::ShouldDeferDownload(OmahaResponse* output_object) {
Chris Sosa968d0572013-08-23 14:46:02 -07001229 if (params_->interactive()) {
1230 LOG(INFO) << "Not deferring download because update is interactive.";
1231 return false;
1232 }
1233
David Zeuthen8f191b22013-08-06 12:27:50 -07001234 // If we're using p2p to download _and_ we have a p2p URL, we never
1235 // defer the download. This is because the download will always
1236 // happen from a peer on the LAN and we've been waiting in line for
1237 // our turn.
Gilad Arnold74b5f552014-10-07 08:17:16 -07001238 const PayloadStateInterface* payload_state = system_state_->payload_state();
1239 if (payload_state->GetUsingP2PForDownloading() &&
1240 !payload_state->GetP2PUrl().empty()) {
David Zeuthen8f191b22013-08-06 12:27:50 -07001241 LOG(INFO) << "Download not deferred because download "
1242 << "will happen from a local peer (via p2p).";
1243 return false;
1244 }
1245
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001246 // We should defer the downloads only if we've first satisfied the
1247 // wall-clock-based-waiting period and then the update-check-based waiting
1248 // period, if required.
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001249 if (!params_->wall_clock_based_wait_enabled()) {
Chris Sosa968d0572013-08-23 14:46:02 -07001250 LOG(INFO) << "Wall-clock-based waiting period is not enabled,"
1251 << " so no deferring needed.";
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001252 return false;
1253 }
1254
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001255 switch (IsWallClockBasedWaitingSatisfied(output_object)) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001256 case kWallClockWaitNotSatisfied:
1257 // We haven't even satisfied the first condition, passing the
1258 // wall-clock-based waiting period, so we should defer the downloads
1259 // until that happens.
1260 LOG(INFO) << "wall-clock-based-wait not satisfied.";
1261 return true;
1262
1263 case kWallClockWaitDoneButUpdateCheckWaitRequired:
1264 LOG(INFO) << "wall-clock-based-wait satisfied and "
1265 << "update-check-based-wait required.";
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001266 return !IsUpdateCheckCountBasedWaitingSatisfied();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001267
1268 case kWallClockWaitDoneAndUpdateCheckWaitNotRequired:
1269 // Wall-clock-based waiting period is satisfied, and it's determined
1270 // that we do not need the update-check-based wait. so no need to
1271 // defer downloads.
1272 LOG(INFO) << "wall-clock-based-wait satisfied and "
1273 << "update-check-based-wait is not required.";
1274 return false;
1275
1276 default:
1277 // Returning false for this default case so we err on the
1278 // side of downloading updates than deferring in case of any bugs.
1279 NOTREACHED();
1280 return false;
1281 }
1282}
1283
1284OmahaRequestAction::WallClockWaitResult
1285OmahaRequestAction::IsWallClockBasedWaitingSatisfied(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001286 OmahaResponse* output_object) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001287 Time update_first_seen_at;
Ben Chan9abb7632014-08-07 00:10:53 -07001288 int64_t update_first_seen_at_int;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001289
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001290 if (system_state_->prefs()->Exists(kPrefsUpdateFirstSeenAt)) {
1291 if (system_state_->prefs()->GetInt64(kPrefsUpdateFirstSeenAt,
1292 &update_first_seen_at_int)) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001293 // Note: This timestamp could be that of ANY update we saw in the past
1294 // (not necessarily this particular update we're considering to apply)
1295 // but never got to apply because of some reason (e.g. stop AU policy,
1296 // updates being pulled out from Omaha, changes in target version prefix,
1297 // new update being rolled out, etc.). But for the purposes of scattering
1298 // it doesn't matter which update the timestamp corresponds to. i.e.
1299 // the clock starts ticking the first time we see an update and we're
1300 // ready to apply when the random wait period is satisfied relative to
1301 // that first seen timestamp.
1302 update_first_seen_at = Time::FromInternalValue(update_first_seen_at_int);
1303 LOG(INFO) << "Using persisted value of UpdateFirstSeenAt: "
1304 << utils::ToString(update_first_seen_at);
1305 } else {
1306 // This seems like an unexpected error where the persisted value exists
1307 // but it's not readable for some reason. Just skip scattering in this
1308 // case to be safe.
1309 LOG(INFO) << "Not scattering as UpdateFirstSeenAt value cannot be read";
1310 return kWallClockWaitDoneAndUpdateCheckWaitNotRequired;
1311 }
1312 } else {
Sen Jiang7c1171e2016-06-23 11:35:40 -07001313 update_first_seen_at = system_state_->clock()->GetWallclockTime();
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001314 update_first_seen_at_int = update_first_seen_at.ToInternalValue();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001315 if (system_state_->prefs()->SetInt64(kPrefsUpdateFirstSeenAt,
1316 update_first_seen_at_int)) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001317 LOG(INFO) << "Persisted the new value for UpdateFirstSeenAt: "
1318 << utils::ToString(update_first_seen_at);
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001319 } else {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001320 // This seems like an unexpected error where the value cannot be
1321 // persisted for some reason. Just skip scattering in this
1322 // case to be safe.
1323 LOG(INFO) << "Not scattering as UpdateFirstSeenAt value "
1324 << utils::ToString(update_first_seen_at)
1325 << " cannot be persisted";
1326 return kWallClockWaitDoneAndUpdateCheckWaitNotRequired;
1327 }
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001328 }
1329
Sen Jiang7c1171e2016-06-23 11:35:40 -07001330 TimeDelta elapsed_time =
1331 system_state_->clock()->GetWallclockTime() - update_first_seen_at;
1332 TimeDelta max_scatter_period =
1333 TimeDelta::FromDays(output_object->max_days_to_scatter);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001334
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001335 LOG(INFO) << "Waiting Period = "
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001336 << utils::FormatSecs(params_->waiting_period().InSeconds())
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001337 << ", Time Elapsed = "
1338 << utils::FormatSecs(elapsed_time.InSeconds())
1339 << ", MaxDaysToScatter = "
1340 << max_scatter_period.InDays();
1341
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001342 if (!output_object->deadline.empty()) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001343 // The deadline is set for all rules which serve a delta update from a
1344 // previous FSI, which means this update will be applied mostly in OOBE
1345 // cases. For these cases, we shouldn't scatter so as to finish the OOBE
1346 // quickly.
1347 LOG(INFO) << "Not scattering as deadline flag is set";
1348 return kWallClockWaitDoneAndUpdateCheckWaitNotRequired;
1349 }
1350
1351 if (max_scatter_period.InDays() == 0) {
1352 // This means the Omaha rule creator decides that this rule
1353 // should not be scattered irrespective of the policy.
1354 LOG(INFO) << "Not scattering as MaxDaysToScatter in rule is 0.";
1355 return kWallClockWaitDoneAndUpdateCheckWaitNotRequired;
1356 }
1357
1358 if (elapsed_time > max_scatter_period) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001359 // This means we've waited more than the upperbound wait in the rule
1360 // from the time we first saw a valid update available to us.
1361 // This will prevent update starvation.
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001362 LOG(INFO) << "Not scattering as we're past the MaxDaysToScatter limit.";
1363 return kWallClockWaitDoneAndUpdateCheckWaitNotRequired;
1364 }
1365
1366 // This means we are required to participate in scattering.
1367 // See if our turn has arrived now.
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001368 TimeDelta remaining_wait_time = params_->waiting_period() - elapsed_time;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001369 if (remaining_wait_time.InSeconds() <= 0) {
1370 // Yes, it's our turn now.
1371 LOG(INFO) << "Successfully passed the wall-clock-based-wait.";
1372
1373 // But we can't download until the update-check-count-based wait is also
1374 // satisfied, so mark it as required now if update checks are enabled.
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001375 return params_->update_check_count_wait_enabled() ?
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001376 kWallClockWaitDoneButUpdateCheckWaitRequired :
1377 kWallClockWaitDoneAndUpdateCheckWaitNotRequired;
1378 }
1379
1380 // Not our turn yet, so we have to wait until our turn to
1381 // help scatter the downloads across all clients of the enterprise.
1382 LOG(INFO) << "Update deferred for another "
1383 << utils::FormatSecs(remaining_wait_time.InSeconds())
1384 << " per policy.";
1385 return kWallClockWaitNotSatisfied;
1386}
1387
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001388bool OmahaRequestAction::IsUpdateCheckCountBasedWaitingSatisfied() {
Ben Chan9abb7632014-08-07 00:10:53 -07001389 int64_t update_check_count_value;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001390
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001391 if (system_state_->prefs()->Exists(kPrefsUpdateCheckCount)) {
1392 if (!system_state_->prefs()->GetInt64(kPrefsUpdateCheckCount,
1393 &update_check_count_value)) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001394 // We are unable to read the update check count from file for some reason.
1395 // So let's proceed anyway so as to not stall the update.
1396 LOG(ERROR) << "Unable to read update check count. "
1397 << "Skipping update-check-count-based-wait.";
1398 return true;
1399 }
1400 } else {
1401 // This file does not exist. This means we haven't started our update
1402 // check count down yet, so this is the right time to start the count down.
1403 update_check_count_value = base::RandInt(
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001404 params_->min_update_checks_needed(),
1405 params_->max_update_checks_allowed());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001406
1407 LOG(INFO) << "Randomly picked update check count value = "
1408 << update_check_count_value;
1409
1410 // Write out the initial value of update_check_count_value.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001411 if (!system_state_->prefs()->SetInt64(kPrefsUpdateCheckCount,
1412 update_check_count_value)) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001413 // We weren't able to write the update check count file for some reason.
1414 // So let's proceed anyway so as to not stall the update.
1415 LOG(ERROR) << "Unable to write update check count. "
1416 << "Skipping update-check-count-based-wait.";
1417 return true;
1418 }
1419 }
1420
1421 if (update_check_count_value == 0) {
1422 LOG(INFO) << "Successfully passed the update-check-based-wait.";
1423 return true;
1424 }
1425
1426 if (update_check_count_value < 0 ||
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001427 update_check_count_value > params_->max_update_checks_allowed()) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001428 // We err on the side of skipping scattering logic instead of stalling
1429 // a machine from receiving any updates in case of any unexpected state.
1430 LOG(ERROR) << "Invalid value for update check count detected. "
1431 << "Skipping update-check-count-based-wait.";
1432 return true;
1433 }
1434
1435 // Legal value, we need to wait for more update checks to happen
1436 // until this becomes 0.
1437 LOG(INFO) << "Deferring Omaha updates for another "
1438 << update_check_count_value
1439 << " update checks per policy";
1440 return false;
1441}
1442
David Zeuthen639aa362014-02-03 16:23:44 -08001443// static
David Zeuthene8ed8632014-07-24 13:38:10 -04001444bool OmahaRequestAction::ParseInstallDate(OmahaParserData* parser_data,
David Zeuthen639aa362014-02-03 16:23:44 -08001445 OmahaResponse* output_object) {
David Zeuthen639aa362014-02-03 16:23:44 -08001446 int64_t elapsed_days = 0;
David Zeuthene8ed8632014-07-24 13:38:10 -04001447 if (!base::StringToInt64(parser_data->daystart_elapsed_days,
David Zeuthen639aa362014-02-03 16:23:44 -08001448 &elapsed_days))
1449 return false;
1450
1451 if (elapsed_days < 0)
1452 return false;
1453
1454 output_object->install_date_days = elapsed_days;
1455 return true;
1456}
1457
1458// static
1459bool OmahaRequestAction::HasInstallDate(SystemState *system_state) {
1460 PrefsInterface* prefs = system_state->prefs();
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001461 if (prefs == nullptr)
David Zeuthen639aa362014-02-03 16:23:44 -08001462 return false;
1463
1464 return prefs->Exists(kPrefsInstallDateDays);
1465}
1466
1467// static
1468bool OmahaRequestAction::PersistInstallDate(
1469 SystemState *system_state,
1470 int install_date_days,
1471 InstallDateProvisioningSource source) {
1472 TEST_AND_RETURN_FALSE(install_date_days >= 0);
1473
1474 PrefsInterface* prefs = system_state->prefs();
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001475 if (prefs == nullptr)
David Zeuthen639aa362014-02-03 16:23:44 -08001476 return false;
1477
1478 if (!prefs->SetInt64(kPrefsInstallDateDays, install_date_days))
1479 return false;
1480
Alex Deymoaf9a8632015-09-23 18:51:48 -07001481 string metric_name = metrics::kMetricInstallDateProvisioningSource;
David Zeuthen33bae492014-02-25 16:16:18 -08001482 system_state->metrics_lib()->SendEnumToUMA(
1483 metric_name,
1484 static_cast<int>(source), // Sample.
1485 kProvisionedMax); // Maximum.
David Zeuthen639aa362014-02-03 16:23:44 -08001486
1487 return true;
1488}
1489
Alex Deymo8e18f932015-03-27 16:16:59 -07001490bool OmahaRequestAction::PersistCohortData(
1491 const string& prefs_key,
1492 const string& new_value) {
1493 if (new_value.empty() && system_state_->prefs()->Exists(prefs_key)) {
1494 LOG(INFO) << "Removing stored " << prefs_key << " value.";
1495 return system_state_->prefs()->Delete(prefs_key);
1496 } else if (!new_value.empty()) {
1497 LOG(INFO) << "Storing new setting " << prefs_key << " as " << new_value;
1498 return system_state_->prefs()->SetString(prefs_key, new_value);
1499 }
1500 return true;
1501}
1502
Alex Deymob3fa53b2016-04-18 19:57:58 -07001503bool OmahaRequestAction::PersistEolStatus(const map<string, string>& attrs) {
1504 auto eol_attr = attrs.find(kEolAttr);
1505 if (eol_attr != attrs.end()) {
1506 return system_state_->prefs()->SetString(kPrefsOmahaEolStatus,
1507 eol_attr->second);
1508 } else if (system_state_->prefs()->Exists(kPrefsOmahaEolStatus)) {
1509 return system_state_->prefs()->Delete(kPrefsOmahaEolStatus);
1510 }
1511 return true;
1512}
1513
David Zeuthen33bae492014-02-25 16:16:18 -08001514void OmahaRequestAction::ActionCompleted(ErrorCode code) {
1515 // We only want to report this on "update check".
1516 if (ping_only_ || event_ != nullptr)
1517 return;
1518
1519 metrics::CheckResult result = metrics::CheckResult::kUnset;
1520 metrics::CheckReaction reaction = metrics::CheckReaction::kUnset;
1521 metrics::DownloadErrorCode download_error_code =
1522 metrics::DownloadErrorCode::kUnset;
1523
1524 // Regular update attempt.
1525 switch (code) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001526 case ErrorCode::kSuccess:
David Zeuthen33bae492014-02-25 16:16:18 -08001527 // OK, we parsed the response successfully but that does
1528 // necessarily mean that an update is available.
1529 if (HasOutputPipe()) {
1530 const OmahaResponse& response = GetOutputObject();
1531 if (response.update_exists) {
1532 result = metrics::CheckResult::kUpdateAvailable;
1533 reaction = metrics::CheckReaction::kUpdating;
1534 } else {
1535 result = metrics::CheckResult::kNoUpdateAvailable;
1536 }
1537 } else {
1538 result = metrics::CheckResult::kNoUpdateAvailable;
1539 }
1540 break;
1541
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001542 case ErrorCode::kOmahaUpdateIgnoredPerPolicy:
David Zeuthen33bae492014-02-25 16:16:18 -08001543 result = metrics::CheckResult::kUpdateAvailable;
1544 reaction = metrics::CheckReaction::kIgnored;
1545 break;
1546
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001547 case ErrorCode::kOmahaUpdateDeferredPerPolicy:
David Zeuthen33bae492014-02-25 16:16:18 -08001548 result = metrics::CheckResult::kUpdateAvailable;
1549 reaction = metrics::CheckReaction::kDeferring;
1550 break;
1551
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001552 case ErrorCode::kOmahaUpdateDeferredForBackoff:
David Zeuthen33bae492014-02-25 16:16:18 -08001553 result = metrics::CheckResult::kUpdateAvailable;
1554 reaction = metrics::CheckReaction::kBackingOff;
1555 break;
1556
1557 default:
1558 // We report two flavors of errors, "Download errors" and "Parsing
1559 // error". Try to convert to the former and if that doesn't work
1560 // we know it's the latter.
Alex Deymo38429cf2015-11-11 18:27:22 -08001561 metrics::DownloadErrorCode tmp_error =
1562 metrics_utils::GetDownloadErrorCode(code);
David Zeuthen33bae492014-02-25 16:16:18 -08001563 if (tmp_error != metrics::DownloadErrorCode::kInputMalformed) {
1564 result = metrics::CheckResult::kDownloadError;
1565 download_error_code = tmp_error;
1566 } else {
1567 result = metrics::CheckResult::kParsingError;
1568 }
1569 break;
1570 }
1571
1572 metrics::ReportUpdateCheckMetrics(system_state_,
1573 result, reaction, download_error_code);
1574}
1575
Chris Sosa77f79e82014-06-02 18:16:24 -07001576bool OmahaRequestAction::ShouldIgnoreUpdate(
Tao Bao5688d162017-06-06 13:09:06 -07001577 const OmahaResponse& response) const {
Chris Sosa77f79e82014-06-02 18:16:24 -07001578 // Note: policy decision to not update to a version we rolled back from.
1579 string rollback_version =
1580 system_state_->payload_state()->GetRollbackVersion();
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001581 if (!rollback_version.empty()) {
Chris Sosa77f79e82014-06-02 18:16:24 -07001582 LOG(INFO) << "Detected previous rollback from version " << rollback_version;
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001583 if (rollback_version == response.version) {
Chris Sosa77f79e82014-06-02 18:16:24 -07001584 LOG(INFO) << "Received version that we rolled back from. Ignoring.";
1585 return true;
1586 }
1587 }
1588
Tao Bao5688d162017-06-06 13:09:06 -07001589 if (!IsUpdateAllowedOverCurrentConnection()) {
Chris Sosa77f79e82014-06-02 18:16:24 -07001590 LOG(INFO) << "Update is not allowed over current connection.";
1591 return true;
1592 }
1593
1594 // Note: We could technically delete the UpdateFirstSeenAt state when we
1595 // return true. If we do, it'll mean a device has to restart the
1596 // UpdateFirstSeenAt and thus help scattering take effect when the AU is
1597 // turned on again. On the other hand, it also increases the chance of update
1598 // starvation if an admin turns AU on/off more frequently. We choose to err on
1599 // the side of preventing starvation at the cost of not applying scattering in
1600 // those cases.
1601 return false;
1602}
1603
Tao Bao5688d162017-06-06 13:09:06 -07001604bool OmahaRequestAction::IsUpdateAllowedOverCurrentConnection() const {
Sen Jiang255e22b2016-05-20 16:15:29 -07001605 ConnectionType type;
1606 ConnectionTethering tethering;
Alex Deymof6ee0162015-07-31 12:35:22 -07001607 ConnectionManagerInterface* connection_manager =
1608 system_state_->connection_manager();
Alex Deymo30534502015-07-20 15:06:33 -07001609 if (!connection_manager->GetConnectionProperties(&type, &tethering)) {
Chris Sosa77f79e82014-06-02 18:16:24 -07001610 LOG(INFO) << "We could not determine our connection type. "
1611 << "Defaulting to allow updates.";
1612 return true;
1613 }
1614 bool is_allowed = connection_manager->IsUpdateAllowedOver(type, tethering);
1615 LOG(INFO) << "We are connected via "
Sen Jiang255e22b2016-05-20 16:15:29 -07001616 << connection_utils::StringForConnectionType(type)
Chris Sosa77f79e82014-06-02 18:16:24 -07001617 << ", Updates allowed: " << (is_allowed ? "Yes" : "No");
1618 return is_allowed;
1619}
1620
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001621} // namespace chromeos_update_engine