Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 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.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 17 | #include "update_engine/omaha_request_action.h" |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 18 | |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 19 | #include <inttypes.h> |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 20 | |
Zentaro Kavanagh | 0ff621c | 2018-07-13 13:06:56 -0700 | [diff] [blame] | 21 | #include <limits> |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 22 | #include <map> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 23 | #include <sstream> |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 24 | #include <string> |
Sen Jiang | 8cd4234 | 2018-01-31 12:06:59 -0800 | [diff] [blame] | 25 | #include <utility> |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 26 | #include <vector> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 27 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 28 | #include <base/bind.h> |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 29 | #include <base/logging.h> |
| 30 | #include <base/rand_util.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 31 | #include <base/strings/string_number_conversions.h> |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 32 | #include <base/strings/string_split.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 33 | #include <base/strings/string_util.h> |
| 34 | #include <base/strings/stringprintf.h> |
| 35 | #include <base/time/time.h> |
Sen Jiang | 684c9cd | 2017-10-17 16:26:45 -0700 | [diff] [blame] | 36 | #include <brillo/key_value_store.h> |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 37 | #include <expat.h> |
Alex Deymo | a259179 | 2015-11-17 00:39:40 -0300 | [diff] [blame] | 38 | #include <metrics/metrics_library.h> |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 39 | #include <policy/libpolicy.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 40 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 41 | #include "update_engine/common/action_pipe.h" |
| 42 | #include "update_engine/common/constants.h" |
| 43 | #include "update_engine/common/hardware_interface.h" |
| 44 | #include "update_engine/common/hash_calculator.h" |
| 45 | #include "update_engine/common/platform_constants.h" |
| 46 | #include "update_engine/common/prefs_interface.h" |
| 47 | #include "update_engine/common/utils.h" |
Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 48 | #include "update_engine/connection_manager_interface.h" |
Tianjie Xu | 282aa1f | 2017-09-05 13:42:45 -0700 | [diff] [blame] | 49 | #include "update_engine/metrics_reporter_interface.h" |
Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 50 | #include "update_engine/metrics_utils.h" |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 51 | #include "update_engine/omaha_request_params.h" |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 52 | #include "update_engine/p2p_manager.h" |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 53 | #include "update_engine/payload_state_interface.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 54 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 55 | using base::Time; |
| 56 | using base::TimeDelta; |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 57 | using chromeos_update_manager::kRollforwardInfinity; |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 58 | using std::map; |
Zentaro Kavanagh | 0ff621c | 2018-07-13 13:06:56 -0700 | [diff] [blame] | 59 | using std::numeric_limits; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 60 | using std::string; |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 61 | using std::vector; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 62 | |
| 63 | namespace chromeos_update_engine { |
| 64 | |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 65 | // List of custom attributes that we interpret in the Omaha response: |
| 66 | constexpr char kAttrDeadline[] = "deadline"; |
| 67 | constexpr char kAttrDisableP2PForDownloading[] = "DisableP2PForDownloading"; |
| 68 | constexpr char kAttrDisableP2PForSharing[] = "DisableP2PForSharing"; |
| 69 | constexpr char kAttrDisablePayloadBackoff[] = "DisablePayloadBackoff"; |
| 70 | constexpr char kAttrVersion[] = "version"; |
Jay Srinivasan | d671e97 | 2013-01-11 17:17:19 -0800 | [diff] [blame] | 71 | // Deprecated: "IsDelta" |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 72 | constexpr char kAttrIsDeltaPayload[] = "IsDeltaPayload"; |
| 73 | constexpr char kAttrMaxFailureCountPerUrl[] = "MaxFailureCountPerUrl"; |
| 74 | constexpr char kAttrMaxDaysToScatter[] = "MaxDaysToScatter"; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 75 | // Deprecated: "ManifestSignatureRsa" |
| 76 | // Deprecated: "ManifestSize" |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 77 | constexpr char kAttrMetadataSignatureRsa[] = "MetadataSignatureRsa"; |
| 78 | constexpr char kAttrMetadataSize[] = "MetadataSize"; |
| 79 | constexpr char kAttrMoreInfo[] = "MoreInfo"; |
| 80 | constexpr char kAttrNoUpdate[] = "noupdate"; |
Don Garrett | 42bd3aa | 2013-04-10 18:14:56 -0700 | [diff] [blame] | 81 | // Deprecated: "NeedsAdmin" |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 82 | constexpr char kAttrPollInterval[] = "PollInterval"; |
| 83 | constexpr char kAttrPowerwash[] = "Powerwash"; |
| 84 | constexpr char kAttrPrompt[] = "Prompt"; |
| 85 | constexpr char kAttrPublicKeyRsa[] = "PublicKeyRsa"; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 86 | |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 87 | // List of attributes that we interpret in the Omaha response: |
| 88 | constexpr char kAttrAppId[] = "appid"; |
| 89 | constexpr char kAttrCodeBase[] = "codebase"; |
| 90 | constexpr char kAttrCohort[] = "cohort"; |
| 91 | constexpr char kAttrCohortHint[] = "cohorthint"; |
| 92 | constexpr char kAttrCohortName[] = "cohortname"; |
| 93 | constexpr char kAttrElapsedDays[] = "elapsed_days"; |
| 94 | constexpr char kAttrElapsedSeconds[] = "elapsed_seconds"; |
| 95 | constexpr char kAttrEvent[] = "event"; |
| 96 | constexpr char kAttrHashSha256[] = "hash_sha256"; |
| 97 | // Deprecated: "hash"; Although we still need to pass it from the server for |
| 98 | // backward compatibility. |
| 99 | constexpr char kAttrName[] = "name"; |
| 100 | // Deprecated: "sha256"; Although we still need to pass it from the server for |
| 101 | // backward compatibility. |
| 102 | constexpr char kAttrSize[] = "size"; |
| 103 | constexpr char kAttrStatus[] = "status"; |
| 104 | |
| 105 | // List of values that we interpret in the Omaha response: |
| 106 | constexpr char kValPostInstall[] = "postinstall"; |
| 107 | constexpr char kValNoUpdate[] = "noupdate"; |
| 108 | |
| 109 | constexpr char kOmahaUpdaterVersion[] = "0.1.0.0"; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 110 | |
Sen Jiang | 42fa45e | 2018-03-12 11:02:14 -0700 | [diff] [blame] | 111 | // X-Goog-Update headers. |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 112 | constexpr char kXGoogleUpdateInteractivity[] = "X-Goog-Update-Interactivity"; |
| 113 | constexpr char kXGoogleUpdateAppId[] = "X-Goog-Update-AppId"; |
| 114 | constexpr char kXGoogleUpdateUpdater[] = "X-Goog-Update-Updater"; |
Alex Deymo | 14ad88e | 2016-06-29 12:30:14 -0700 | [diff] [blame] | 115 | |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 116 | // updatecheck attributes (without the underscore prefix). |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 117 | constexpr char kAttrEol[] = "eol"; |
| 118 | constexpr char kAttrRollback[] = "rollback"; |
| 119 | constexpr char kAttrFirmwareVersion[] = "firmware_version"; |
| 120 | constexpr char kAttrKernelVersion[] = "kernel_version"; |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 121 | |
Alex Deymo | ac41a82 | 2015-09-15 20:52:53 -0700 | [diff] [blame] | 122 | namespace { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 123 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 124 | // Returns an XML ping element attribute assignment with attribute |
| 125 | // |name| and value |ping_days| if |ping_days| has a value that needs |
| 126 | // to be sent, or an empty string otherwise. |
| 127 | string GetPingAttribute(const string& name, int ping_days) { |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 128 | if (ping_days > 0 || ping_days == OmahaRequestAction::kNeverPinged) |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 129 | return base::StringPrintf(" %s=\"%d\"", name.c_str(), ping_days); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 130 | return ""; |
| 131 | } |
| 132 | |
| 133 | // Returns an XML ping element if any of the elapsed days need to be |
| 134 | // sent, or an empty string otherwise. |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 135 | string GetPingXml(int ping_active_days, int ping_roll_call_days) { |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 136 | string ping_active = GetPingAttribute("a", ping_active_days); |
| 137 | string ping_roll_call = GetPingAttribute("r", ping_roll_call_days); |
| 138 | if (!ping_active.empty() || !ping_roll_call.empty()) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 139 | return base::StringPrintf(" <ping active=\"1\"%s%s></ping>\n", |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 140 | ping_active.c_str(), |
| 141 | ping_roll_call.c_str()); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 142 | } |
| 143 | return ""; |
| 144 | } |
| 145 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 146 | // Returns an XML that goes into the body of the <app> element of the Omaha |
| 147 | // request based on the given parameters. |
| 148 | string GetAppBody(const OmahaEvent* event, |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 149 | OmahaRequestParams* params, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 150 | bool ping_only, |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 151 | bool include_ping, |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 152 | bool skip_updatecheck, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 153 | int ping_active_days, |
| 154 | int ping_roll_call_days, |
| 155 | PrefsInterface* prefs) { |
| 156 | string app_body; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 157 | if (event == nullptr) { |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 158 | if (include_ping) |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 159 | app_body = GetPingXml(ping_active_days, ping_roll_call_days); |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 160 | if (!ping_only) { |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 161 | if (!skip_updatecheck) { |
| 162 | app_body += " <updatecheck"; |
| 163 | if (!params->target_version_prefix().empty()) { |
| 164 | app_body += base::StringPrintf( |
| 165 | " targetversionprefix=\"%s\"", |
| 166 | XmlEncodeWithDefault(params->target_version_prefix(), "") |
| 167 | .c_str()); |
| 168 | // Rollback requires target_version_prefix set. |
| 169 | if (params->rollback_allowed()) { |
| 170 | app_body += " rollback_allowed=\"true\""; |
| 171 | } |
Marton Hunyady | ba51c3f | 2018-04-25 15:18:10 +0200 | [diff] [blame] | 172 | } |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 173 | app_body += "></updatecheck>\n"; |
Marton Hunyady | ba51c3f | 2018-04-25 15:18:10 +0200 | [diff] [blame] | 174 | } |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 175 | |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 176 | // If this is the first update check after a reboot following a previous |
| 177 | // update, generate an event containing the previous version number. If |
| 178 | // the previous version preference file doesn't exist the event is still |
| 179 | // generated with a previous version of 0.0.0.0 -- this is relevant for |
| 180 | // older clients or new installs. The previous version event is not sent |
| 181 | // for ping-only requests because they come before the client has |
Alex Deymo | 87c0886 | 2015-10-30 21:56:55 -0700 | [diff] [blame] | 182 | // rebooted. The previous version event is also not sent if it was already |
| 183 | // sent for this new version with a previous updatecheck. |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 184 | string prev_version; |
| 185 | if (!prefs->GetString(kPrefsPreviousVersion, &prev_version)) { |
| 186 | prev_version = "0.0.0.0"; |
| 187 | } |
Alex Deymo | 87c0886 | 2015-10-30 21:56:55 -0700 | [diff] [blame] | 188 | // We only store a non-empty previous version value after a successful |
| 189 | // update in the previous boot. After reporting it back to the server, |
| 190 | // we clear the previous version value so it doesn't get reported again. |
| 191 | if (!prev_version.empty()) { |
| 192 | app_body += base::StringPrintf( |
| 193 | " <event eventtype=\"%d\" eventresult=\"%d\" " |
| 194 | "previousversion=\"%s\"></event>\n", |
Alex Deymo | 9fded1e | 2015-11-05 12:31:19 -0800 | [diff] [blame] | 195 | OmahaEvent::kTypeRebootedAfterUpdate, |
| 196 | OmahaEvent::kResultSuccess, |
Alex Deymo | 87c0886 | 2015-10-30 21:56:55 -0700 | [diff] [blame] | 197 | XmlEncodeWithDefault(prev_version, "0.0.0.0").c_str()); |
| 198 | LOG_IF(WARNING, !prefs->SetString(kPrefsPreviousVersion, "")) |
| 199 | << "Unable to reset the previous version."; |
| 200 | } |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 201 | } |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 202 | } else { |
Darin Petkov | c91dd6b | 2011-01-10 12:31:34 -0800 | [diff] [blame] | 203 | // The error code is an optional attribute so append it only if the result |
| 204 | // is not success. |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 205 | string error_code; |
| 206 | if (event->result != OmahaEvent::kResultSuccess) { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 207 | error_code = base::StringPrintf(" errorcode=\"%d\"", |
| 208 | static_cast<int>(event->error_code)); |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 209 | } |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 210 | app_body = base::StringPrintf( |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 211 | " <event eventtype=\"%d\" eventresult=\"%d\"%s></event>\n", |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 212 | event->type, |
| 213 | event->result, |
| 214 | error_code.c_str()); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 215 | } |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 216 | |
| 217 | return app_body; |
| 218 | } |
| 219 | |
Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 220 | // Returns the cohort* argument to include in the <app> tag for the passed |
| 221 | // |arg_name| and |prefs_key|, if any. The return value is suitable to |
| 222 | // concatenate to the list of arguments and includes a space at the end. |
| 223 | string GetCohortArgXml(PrefsInterface* prefs, |
Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 224 | const string arg_name, |
| 225 | const string prefs_key) { |
Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 226 | // There's nothing wrong with not having a given cohort setting, so we check |
Sen Jiang | 771f648 | 2018-04-04 17:59:10 -0700 | [diff] [blame] | 227 | // existence first to avoid the warning log message. |
Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 228 | if (!prefs->Exists(prefs_key)) |
| 229 | return ""; |
| 230 | string cohort_value; |
| 231 | if (!prefs->GetString(prefs_key, &cohort_value) || cohort_value.empty()) |
| 232 | return ""; |
| 233 | // This is a sanity check to avoid sending a huge XML file back to Ohama due |
| 234 | // to a compromised stateful partition making the update check fail in low |
| 235 | // network environments envent after a reboot. |
| 236 | if (cohort_value.size() > 1024) { |
| 237 | LOG(WARNING) << "The omaha cohort setting " << arg_name |
| 238 | << " has a too big value, which must be an error or an " |
| 239 | "attacker trying to inhibit updates."; |
| 240 | return ""; |
| 241 | } |
| 242 | |
Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 243 | string escaped_xml_value; |
| 244 | if (!XmlEncode(cohort_value, &escaped_xml_value)) { |
| 245 | LOG(WARNING) << "The omaha cohort setting " << arg_name |
| 246 | << " is ASCII-7 invalid, ignoring it."; |
| 247 | return ""; |
| 248 | } |
| 249 | |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 250 | return base::StringPrintf( |
| 251 | "%s=\"%s\" ", arg_name.c_str(), escaped_xml_value.c_str()); |
Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Sen Jiang | 94a4dec | 2017-03-28 18:23:35 -0700 | [diff] [blame] | 254 | struct OmahaAppData { |
| 255 | string id; |
| 256 | string version; |
Sen Jiang | 684c9cd | 2017-10-17 16:26:45 -0700 | [diff] [blame] | 257 | string product_components; |
Sen Jiang | 94a4dec | 2017-03-28 18:23:35 -0700 | [diff] [blame] | 258 | }; |
| 259 | |
Sen Jiang | 684c9cd | 2017-10-17 16:26:45 -0700 | [diff] [blame] | 260 | bool IsValidComponentID(const string& id) { |
| 261 | for (char c : id) { |
| 262 | if (!isalnum(c) && c != '-' && c != '_' && c != '.') |
| 263 | return false; |
| 264 | } |
| 265 | return true; |
| 266 | } |
| 267 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 268 | // Returns an XML that corresponds to the entire <app> node of the Omaha |
| 269 | // request based on the given parameters. |
| 270 | string GetAppXml(const OmahaEvent* event, |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 271 | OmahaRequestParams* params, |
Sen Jiang | 94a4dec | 2017-03-28 18:23:35 -0700 | [diff] [blame] | 272 | const OmahaAppData& app_data, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 273 | bool ping_only, |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 274 | bool include_ping, |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 275 | bool skip_updatecheck, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 276 | int ping_active_days, |
| 277 | int ping_roll_call_days, |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 278 | int install_date_in_days, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 279 | SystemState* system_state) { |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 280 | string app_body = GetAppBody(event, |
| 281 | params, |
| 282 | ping_only, |
| 283 | include_ping, |
| 284 | skip_updatecheck, |
| 285 | ping_active_days, |
| 286 | ping_roll_call_days, |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 287 | system_state->prefs()); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 288 | string app_versions; |
| 289 | |
Sen Jiang | 8cd4234 | 2018-01-31 12:06:59 -0800 | [diff] [blame] | 290 | // If we are downgrading to a more stable channel and we are allowed to do |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 291 | // powerwash, then pass 0.0.0.0 as the version. This is needed to get the |
| 292 | // highest-versioned payload on the destination channel. |
Sen Jiang | 8500d3a | 2018-02-08 12:04:05 -0800 | [diff] [blame] | 293 | if (params->ShouldPowerwash()) { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 294 | LOG(INFO) << "Passing OS version as 0.0.0.0 as we are set to powerwash " |
| 295 | << "on downgrading to the version in the more stable channel"; |
| 296 | app_versions = "version=\"0.0.0.0\" from_version=\"" + |
Sen Jiang | 94a4dec | 2017-03-28 18:23:35 -0700 | [diff] [blame] | 297 | XmlEncodeWithDefault(app_data.version, "0.0.0.0") + "\" "; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 298 | } else { |
Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 299 | app_versions = "version=\"" + |
Sen Jiang | 94a4dec | 2017-03-28 18:23:35 -0700 | [diff] [blame] | 300 | XmlEncodeWithDefault(app_data.version, "0.0.0.0") + "\" "; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 303 | string download_channel = params->download_channel(); |
Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 304 | string app_channels = |
| 305 | "track=\"" + XmlEncodeWithDefault(download_channel, "") + "\" "; |
| 306 | if (params->current_channel() != download_channel) { |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 307 | app_channels += "from_track=\"" + |
| 308 | XmlEncodeWithDefault(params->current_channel(), "") + "\" "; |
Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 309 | } |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 310 | |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 311 | string delta_okay_str = params->delta_okay() ? "true" : "false"; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 312 | |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 313 | // If install_date_days is not set (e.g. its value is -1 ), don't |
| 314 | // include the attribute. |
| 315 | string install_date_in_days_str = ""; |
| 316 | if (install_date_in_days >= 0) { |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 317 | install_date_in_days_str = |
| 318 | base::StringPrintf("installdate=\"%d\" ", install_date_in_days); |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 321 | string app_cohort_args; |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 322 | app_cohort_args += |
| 323 | GetCohortArgXml(system_state->prefs(), "cohort", kPrefsOmahaCohort); |
| 324 | app_cohort_args += GetCohortArgXml( |
| 325 | system_state->prefs(), "cohorthint", kPrefsOmahaCohortHint); |
| 326 | app_cohort_args += GetCohortArgXml( |
| 327 | system_state->prefs(), "cohortname", kPrefsOmahaCohortName); |
Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 328 | |
Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame] | 329 | string fingerprint_arg; |
| 330 | if (!params->os_build_fingerprint().empty()) { |
Sen Jiang | 8cd4234 | 2018-01-31 12:06:59 -0800 | [diff] [blame] | 331 | fingerprint_arg = "fingerprint=\"" + |
| 332 | XmlEncodeWithDefault(params->os_build_fingerprint(), "") + |
| 333 | "\" "; |
Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Sen Jiang | 1d5d95f | 2017-05-19 11:33:10 -0700 | [diff] [blame] | 336 | string buildtype_arg; |
| 337 | if (!params->os_build_type().empty()) { |
| 338 | buildtype_arg = "os_build_type=\"" + |
| 339 | XmlEncodeWithDefault(params->os_build_type(), "") + "\" "; |
| 340 | } |
| 341 | |
Sen Jiang | 684c9cd | 2017-10-17 16:26:45 -0700 | [diff] [blame] | 342 | string product_components_args; |
Sen Jiang | 8500d3a | 2018-02-08 12:04:05 -0800 | [diff] [blame] | 343 | if (!params->ShouldPowerwash() && !app_data.product_components.empty()) { |
Sen Jiang | 684c9cd | 2017-10-17 16:26:45 -0700 | [diff] [blame] | 344 | brillo::KeyValueStore store; |
| 345 | if (store.LoadFromString(app_data.product_components)) { |
| 346 | for (const string& key : store.GetKeys()) { |
| 347 | if (!IsValidComponentID(key)) { |
| 348 | LOG(ERROR) << "Invalid component id: " << key; |
| 349 | continue; |
| 350 | } |
| 351 | string version; |
| 352 | if (!store.GetString(key, &version)) { |
| 353 | LOG(ERROR) << "Failed to get version for " << key |
| 354 | << " in product_components."; |
| 355 | continue; |
| 356 | } |
| 357 | product_components_args += |
| 358 | base::StringPrintf("_%s.version=\"%s\" ", |
| 359 | key.c_str(), |
| 360 | XmlEncodeWithDefault(version, "").c_str()); |
| 361 | } |
| 362 | } else { |
| 363 | LOG(ERROR) << "Failed to parse product_components:\n" |
| 364 | << app_data.product_components; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | // clang-format off |
Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 369 | string app_xml = " <app " |
Sen Jiang | 94a4dec | 2017-03-28 18:23:35 -0700 | [diff] [blame] | 370 | "appid=\"" + XmlEncodeWithDefault(app_data.id, "") + "\" " + |
Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 371 | app_cohort_args + |
| 372 | app_versions + |
| 373 | app_channels + |
Sen Jiang | 684c9cd | 2017-10-17 16:26:45 -0700 | [diff] [blame] | 374 | product_components_args + |
Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame] | 375 | fingerprint_arg + |
Sen Jiang | 1d5d95f | 2017-05-19 11:33:10 -0700 | [diff] [blame] | 376 | buildtype_arg + |
Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 377 | "lang=\"" + XmlEncodeWithDefault(params->app_lang(), "en-US") + "\" " + |
| 378 | "board=\"" + XmlEncodeWithDefault(params->os_board(), "") + "\" " + |
| 379 | "hardware_class=\"" + XmlEncodeWithDefault(params->hwid(), "") + "\" " + |
| 380 | "delta_okay=\"" + delta_okay_str + "\" " |
| 381 | "fw_version=\"" + XmlEncodeWithDefault(params->fw_version(), "") + "\" " + |
| 382 | "ec_version=\"" + XmlEncodeWithDefault(params->ec_version(), "") + "\" " + |
| 383 | install_date_in_days_str + |
| 384 | ">\n" + |
| 385 | app_body + |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 386 | " </app>\n"; |
Sen Jiang | 684c9cd | 2017-10-17 16:26:45 -0700 | [diff] [blame] | 387 | // clang-format on |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 388 | return app_xml; |
| 389 | } |
| 390 | |
| 391 | // Returns an XML that corresponds to the entire <os> node of the Omaha |
| 392 | // request based on the given parameters. |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 393 | string GetOsXml(OmahaRequestParams* params) { |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 394 | string os_xml = |
| 395 | " <os " |
| 396 | "version=\"" + |
| 397 | XmlEncodeWithDefault(params->os_version(), "") + "\" " + "platform=\"" + |
| 398 | XmlEncodeWithDefault(params->os_platform(), "") + "\" " + "sp=\"" + |
| 399 | XmlEncodeWithDefault(params->os_sp(), "") + |
| 400 | "\">" |
Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 401 | "</os>\n"; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 402 | return os_xml; |
| 403 | } |
| 404 | |
| 405 | // Returns an XML that corresponds to the entire Omaha request based on the |
| 406 | // given parameters. |
| 407 | string GetRequestXml(const OmahaEvent* event, |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 408 | OmahaRequestParams* params, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 409 | bool ping_only, |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 410 | bool include_ping, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 411 | int ping_active_days, |
| 412 | int ping_roll_call_days, |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 413 | int install_date_in_days, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 414 | SystemState* system_state) { |
| 415 | string os_xml = GetOsXml(params); |
Sen Jiang | 684c9cd | 2017-10-17 16:26:45 -0700 | [diff] [blame] | 416 | OmahaAppData product_app = { |
| 417 | .id = params->GetAppId(), |
| 418 | .version = params->app_version(), |
| 419 | .product_components = params->product_components()}; |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 420 | // Skips updatecheck for platform app in case of an install operation. |
Sen Jiang | 94a4dec | 2017-03-28 18:23:35 -0700 | [diff] [blame] | 421 | string app_xml = GetAppXml(event, |
| 422 | params, |
| 423 | product_app, |
| 424 | ping_only, |
| 425 | include_ping, |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 426 | params->is_install(), /* skip_updatecheck */ |
Sen Jiang | 94a4dec | 2017-03-28 18:23:35 -0700 | [diff] [blame] | 427 | ping_active_days, |
| 428 | ping_roll_call_days, |
| 429 | install_date_in_days, |
| 430 | system_state); |
| 431 | if (!params->system_app_id().empty()) { |
| 432 | OmahaAppData system_app = {.id = params->system_app_id(), |
| 433 | .version = params->system_version()}; |
| 434 | app_xml += GetAppXml(event, |
| 435 | params, |
| 436 | system_app, |
| 437 | ping_only, |
| 438 | include_ping, |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 439 | false, /* skip_updatecheck */ |
Sen Jiang | 94a4dec | 2017-03-28 18:23:35 -0700 | [diff] [blame] | 440 | ping_active_days, |
| 441 | ping_roll_call_days, |
| 442 | install_date_in_days, |
| 443 | system_state); |
| 444 | } |
Xiaochu Liu | f53a5d3 | 2018-11-26 13:48:59 -0800 | [diff] [blame] | 445 | // Create APP ID according to |dlc_module_id| (sticking the current AppID to |
| 446 | // the DLC module ID with an underscode). |
| 447 | for (const auto& dlc_module_id : params->dlc_module_ids()) { |
| 448 | OmahaAppData dlc_module_app = { |
| 449 | .id = params->GetAppId() + "_" + dlc_module_id, |
| 450 | .version = params->app_version()}; |
Xiaochu Liu | 88d9038 | 2018-08-29 16:09:11 -0700 | [diff] [blame] | 451 | app_xml += GetAppXml(event, |
| 452 | params, |
Xiaochu Liu | f53a5d3 | 2018-11-26 13:48:59 -0800 | [diff] [blame] | 453 | dlc_module_app, |
Xiaochu Liu | 88d9038 | 2018-08-29 16:09:11 -0700 | [diff] [blame] | 454 | ping_only, |
| 455 | include_ping, |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 456 | false, /* skip_updatecheck */ |
Xiaochu Liu | 88d9038 | 2018-08-29 16:09:11 -0700 | [diff] [blame] | 457 | ping_active_days, |
| 458 | ping_roll_call_days, |
| 459 | install_date_in_days, |
| 460 | system_state); |
| 461 | } |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 462 | |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 463 | string install_source = base::StringPrintf( |
| 464 | "installsource=\"%s\" ", |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 465 | (params->interactive() ? "ondemandupdate" : "scheduler")); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 466 | |
Alex Deymo | ac41a82 | 2015-09-15 20:52:53 -0700 | [diff] [blame] | 467 | string updater_version = XmlEncodeWithDefault( |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 468 | base::StringPrintf( |
| 469 | "%s-%s", constants::kOmahaUpdaterID, kOmahaUpdaterVersion), |
| 470 | ""); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 471 | string request_xml = |
| 472 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 473 | "<request protocol=\"3.0\" " + |
| 474 | ("version=\"" + updater_version + |
| 475 | "\" " |
| 476 | "updaterversion=\"" + |
| 477 | updater_version + "\" " + install_source + "ismachine=\"1\">\n") + |
| 478 | os_xml + app_xml + "</request>\n"; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 479 | |
| 480 | return request_xml; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 481 | } |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 482 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 483 | } // namespace |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 484 | |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 485 | // Struct used for holding data obtained when parsing the XML. |
| 486 | struct OmahaParserData { |
David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 487 | explicit OmahaParserData(XML_Parser _xml_parser) : xml_parser(_xml_parser) {} |
| 488 | |
| 489 | // Pointer to the expat XML_Parser object. |
| 490 | XML_Parser xml_parser; |
| 491 | |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 492 | // This is the state of the parser as it's processing the XML. |
| 493 | bool failed = false; |
David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 494 | bool entity_decl = false; |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 495 | string current_path; |
| 496 | |
| 497 | // These are the values extracted from the XML. |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 498 | string updatecheck_poll_interval; |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 499 | map<string, string> updatecheck_attrs; |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 500 | string daystart_elapsed_days; |
| 501 | string daystart_elapsed_seconds; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 502 | |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 503 | struct App { |
Aaron Wood | 7dcdedf | 2017-09-06 17:17:41 -0700 | [diff] [blame] | 504 | string id; |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 505 | vector<string> url_codebase; |
| 506 | string manifest_version; |
| 507 | map<string, string> action_postinstall_attrs; |
| 508 | string updatecheck_status; |
Sen Jiang | b1e063a | 2017-09-15 17:44:31 -0700 | [diff] [blame] | 509 | string cohort; |
| 510 | string cohorthint; |
| 511 | string cohortname; |
| 512 | bool cohort_set = false; |
| 513 | bool cohorthint_set = false; |
| 514 | bool cohortname_set = false; |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 515 | |
| 516 | struct Package { |
| 517 | string name; |
| 518 | string size; |
| 519 | string hash; |
| 520 | }; |
| 521 | vector<Package> packages; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 522 | }; |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 523 | vector<App> apps; |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 524 | }; |
| 525 | |
| 526 | namespace { |
| 527 | |
| 528 | // Callback function invoked by expat. |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 529 | void ParserHandlerStart(void* user_data, |
| 530 | const XML_Char* element, |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 531 | const XML_Char** attr) { |
| 532 | OmahaParserData* data = reinterpret_cast<OmahaParserData*>(user_data); |
| 533 | |
| 534 | if (data->failed) |
| 535 | return; |
| 536 | |
| 537 | data->current_path += string("/") + element; |
| 538 | |
| 539 | map<string, string> attrs; |
| 540 | if (attr != nullptr) { |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 541 | for (int n = 0; attr[n] != nullptr && attr[n + 1] != nullptr; n += 2) { |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 542 | string key = attr[n]; |
| 543 | string value = attr[n + 1]; |
| 544 | attrs[key] = value; |
| 545 | } |
| 546 | } |
| 547 | |
Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 548 | if (data->current_path == "/response/app") { |
Sen Jiang | b1e063a | 2017-09-15 17:44:31 -0700 | [diff] [blame] | 549 | OmahaParserData::App app; |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 550 | if (attrs.find(kAttrAppId) != attrs.end()) { |
| 551 | app.id = attrs[kAttrAppId]; |
Aaron Wood | 7dcdedf | 2017-09-06 17:17:41 -0700 | [diff] [blame] | 552 | } |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 553 | if (attrs.find(kAttrCohort) != attrs.end()) { |
Sen Jiang | b1e063a | 2017-09-15 17:44:31 -0700 | [diff] [blame] | 554 | app.cohort_set = true; |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 555 | app.cohort = attrs[kAttrCohort]; |
Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 556 | } |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 557 | if (attrs.find(kAttrCohortHint) != attrs.end()) { |
Sen Jiang | b1e063a | 2017-09-15 17:44:31 -0700 | [diff] [blame] | 558 | app.cohorthint_set = true; |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 559 | app.cohorthint = attrs[kAttrCohortHint]; |
Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 560 | } |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 561 | if (attrs.find(kAttrCohortName) != attrs.end()) { |
Sen Jiang | b1e063a | 2017-09-15 17:44:31 -0700 | [diff] [blame] | 562 | app.cohortname_set = true; |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 563 | app.cohortname = attrs[kAttrCohortName]; |
Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 564 | } |
Sen Jiang | b1e063a | 2017-09-15 17:44:31 -0700 | [diff] [blame] | 565 | data->apps.push_back(std::move(app)); |
Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 566 | } else if (data->current_path == "/response/app/updatecheck") { |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 567 | if (!data->apps.empty()) |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 568 | data->apps.back().updatecheck_status = attrs[kAttrStatus]; |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 569 | if (data->updatecheck_poll_interval.empty()) |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 570 | data->updatecheck_poll_interval = attrs[kAttrPollInterval]; |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 571 | // Omaha sends arbitrary key-value pairs as extra attributes starting with |
| 572 | // an underscore. |
| 573 | for (const auto& attr : attrs) { |
| 574 | if (!attr.first.empty() && attr.first[0] == '_') |
| 575 | data->updatecheck_attrs[attr.first.substr(1)] = attr.second; |
| 576 | } |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 577 | } else if (data->current_path == "/response/daystart") { |
| 578 | // Get the install-date. |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 579 | data->daystart_elapsed_days = attrs[kAttrElapsedDays]; |
| 580 | data->daystart_elapsed_seconds = attrs[kAttrElapsedSeconds]; |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 581 | } else if (data->current_path == "/response/app/updatecheck/urls/url") { |
| 582 | // Look at all <url> elements. |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 583 | if (!data->apps.empty()) |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 584 | data->apps.back().url_codebase.push_back(attrs[kAttrCodeBase]); |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 585 | } else if (data->current_path == |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 586 | "/response/app/updatecheck/manifest/packages/package") { |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 587 | // Look at all <package> elements. |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 588 | if (!data->apps.empty()) |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 589 | data->apps.back().packages.push_back({.name = attrs[kAttrName], |
| 590 | .size = attrs[kAttrSize], |
| 591 | .hash = attrs[kAttrHashSha256]}); |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 592 | } else if (data->current_path == "/response/app/updatecheck/manifest") { |
| 593 | // Get the version. |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 594 | if (!data->apps.empty()) |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 595 | data->apps.back().manifest_version = attrs[kAttrVersion]; |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 596 | } else if (data->current_path == |
| 597 | "/response/app/updatecheck/manifest/actions/action") { |
| 598 | // We only care about the postinstall action. |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 599 | if (attrs[kAttrEvent] == kValPostInstall && !data->apps.empty()) { |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 600 | data->apps.back().action_postinstall_attrs = std::move(attrs); |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | // Callback function invoked by expat. |
| 606 | void ParserHandlerEnd(void* user_data, const XML_Char* element) { |
| 607 | OmahaParserData* data = reinterpret_cast<OmahaParserData*>(user_data); |
| 608 | if (data->failed) |
| 609 | return; |
| 610 | |
| 611 | const string path_suffix = string("/") + element; |
| 612 | |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 613 | if (!base::EndsWith( |
| 614 | data->current_path, path_suffix, base::CompareCase::SENSITIVE)) { |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 615 | LOG(ERROR) << "Unexpected end element '" << element |
| 616 | << "' with current_path='" << data->current_path << "'"; |
| 617 | data->failed = true; |
| 618 | return; |
| 619 | } |
| 620 | data->current_path.resize(data->current_path.size() - path_suffix.size()); |
| 621 | } |
| 622 | |
David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 623 | // Callback function invoked by expat. |
| 624 | // |
| 625 | // This is called for entity declarations. Since Omaha is guaranteed |
| 626 | // to never return any XML with entities our course of action is to |
| 627 | // just stop parsing. This avoids potential resource exhaustion |
| 628 | // problems AKA the "billion laughs". CVE-2013-0340. |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 629 | void ParserHandlerEntityDecl(void* user_data, |
| 630 | const XML_Char* entity_name, |
David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 631 | int is_parameter_entity, |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 632 | const XML_Char* value, |
David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 633 | int value_length, |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 634 | const XML_Char* base, |
| 635 | const XML_Char* system_id, |
| 636 | const XML_Char* public_id, |
| 637 | const XML_Char* notation_name) { |
David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 638 | OmahaParserData* data = reinterpret_cast<OmahaParserData*>(user_data); |
| 639 | |
| 640 | LOG(ERROR) << "XML entities are not supported. Aborting parsing."; |
| 641 | data->failed = true; |
| 642 | data->entity_decl = true; |
| 643 | XML_StopParser(data->xml_parser, false); |
| 644 | } |
| 645 | |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 646 | } // namespace |
| 647 | |
Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 648 | bool XmlEncode(const string& input, string* output) { |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 649 | if (std::find_if(input.begin(), input.end(), [](const char c) { |
| 650 | return c & 0x80; |
| 651 | }) != input.end()) { |
Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 652 | LOG(WARNING) << "Invalid ASCII-7 string passed to the XML encoder:"; |
| 653 | utils::HexDumpString(input); |
| 654 | return false; |
| 655 | } |
Alex Deymo | cc45785 | 2015-06-18 18:35:50 -0700 | [diff] [blame] | 656 | output->clear(); |
| 657 | // We need at least input.size() space in the output, but the code below will |
| 658 | // handle it if we need more. |
| 659 | output->reserve(input.size()); |
| 660 | for (char c : input) { |
| 661 | switch (c) { |
| 662 | case '\"': |
| 663 | output->append("""); |
| 664 | break; |
| 665 | case '\'': |
| 666 | output->append("'"); |
| 667 | break; |
| 668 | case '&': |
| 669 | output->append("&"); |
| 670 | break; |
| 671 | case '<': |
| 672 | output->append("<"); |
| 673 | break; |
| 674 | case '>': |
| 675 | output->append(">"); |
| 676 | break; |
| 677 | default: |
| 678 | output->push_back(c); |
| 679 | } |
| 680 | } |
Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 681 | return true; |
| 682 | } |
| 683 | |
| 684 | string XmlEncodeWithDefault(const string& input, const string& default_value) { |
| 685 | string output; |
| 686 | if (XmlEncode(input, &output)) |
| 687 | return output; |
| 688 | return default_value; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 691 | OmahaRequestAction::OmahaRequestAction( |
| 692 | SystemState* system_state, |
| 693 | OmahaEvent* event, |
| 694 | std::unique_ptr<HttpFetcher> http_fetcher, |
| 695 | bool ping_only) |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 696 | : system_state_(system_state), |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 697 | params_(system_state->request_params()), |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 698 | event_(event), |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 699 | http_fetcher_(std::move(http_fetcher)), |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 700 | policy_provider_(std::make_unique<policy::PolicyProvider>()), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 701 | ping_only_(ping_only), |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 702 | ping_active_days_(0), |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 703 | ping_roll_call_days_(0) { |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 704 | policy_provider_->Reload(); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 705 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 706 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 707 | OmahaRequestAction::~OmahaRequestAction() {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 708 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 709 | // Calculates the value to use for the ping days parameter. |
| 710 | int OmahaRequestAction::CalculatePingDays(const string& key) { |
| 711 | int days = kNeverPinged; |
| 712 | int64_t last_ping = 0; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 713 | if (system_state_->prefs()->GetInt64(key, &last_ping) && last_ping >= 0) { |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 714 | days = (Time::Now() - Time::FromInternalValue(last_ping)).InDays(); |
| 715 | if (days < 0) { |
| 716 | // If |days| is negative, then the system clock must have jumped |
| 717 | // back in time since the ping was sent. Mark the value so that |
| 718 | // it doesn't get sent to the server but we still update the |
| 719 | // last ping daystart preference. This way the next ping time |
| 720 | // will be correct, hopefully. |
| 721 | days = kPingTimeJump; |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 722 | LOG(WARNING) |
| 723 | << "System clock jumped back in time. Resetting ping daystarts."; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | return days; |
| 727 | } |
| 728 | |
| 729 | void OmahaRequestAction::InitPingDays() { |
| 730 | // We send pings only along with update checks, not with events. |
| 731 | if (IsEvent()) { |
| 732 | return; |
| 733 | } |
| 734 | // TODO(petkov): Figure a way to distinguish active use pings |
| 735 | // vs. roll call pings. Currently, the two pings are identical. A |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 736 | // fix needs to change this code as well as UpdateLastPingDays and ShouldPing. |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 737 | ping_active_days_ = CalculatePingDays(kPrefsLastActivePingDay); |
| 738 | ping_roll_call_days_ = CalculatePingDays(kPrefsLastRollCallPingDay); |
| 739 | } |
| 740 | |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 741 | bool OmahaRequestAction::ShouldPing() const { |
| 742 | if (ping_active_days_ == OmahaRequestAction::kNeverPinged && |
| 743 | ping_roll_call_days_ == OmahaRequestAction::kNeverPinged) { |
| 744 | int powerwash_count = system_state_->hardware()->GetPowerwashCount(); |
| 745 | if (powerwash_count > 0) { |
| 746 | LOG(INFO) << "Not sending ping with a=-1 r=-1 to omaha because " |
| 747 | << "powerwash_count is " << powerwash_count; |
| 748 | return false; |
| 749 | } |
Amin Hassani | 1677e81 | 2017-06-21 13:36:36 -0700 | [diff] [blame] | 750 | if (system_state_->hardware()->GetFirstActiveOmahaPingSent()) { |
| 751 | LOG(INFO) << "Not sending ping with a=-1 r=-1 to omaha because " |
| 752 | << "the first_active_omaha_ping_sent is true"; |
| 753 | return false; |
| 754 | } |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 755 | return true; |
| 756 | } |
| 757 | return ping_active_days_ > 0 || ping_roll_call_days_ > 0; |
| 758 | } |
| 759 | |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 760 | // static |
| 761 | int OmahaRequestAction::GetInstallDate(SystemState* system_state) { |
| 762 | PrefsInterface* prefs = system_state->prefs(); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 763 | if (prefs == nullptr) |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 764 | return -1; |
| 765 | |
| 766 | // If we have the value stored on disk, just return it. |
| 767 | int64_t stored_value; |
| 768 | if (prefs->GetInt64(kPrefsInstallDateDays, &stored_value)) { |
| 769 | // Convert and sanity-check. |
| 770 | int install_date_days = static_cast<int>(stored_value); |
| 771 | if (install_date_days >= 0) |
| 772 | return install_date_days; |
| 773 | LOG(ERROR) << "Dropping stored Omaha InstallData since its value num_days=" |
| 774 | << install_date_days << " looks suspicious."; |
| 775 | prefs->Delete(kPrefsInstallDateDays); |
| 776 | } |
| 777 | |
| 778 | // Otherwise, if OOBE is not complete then do nothing and wait for |
| 779 | // ParseResponse() to call ParseInstallDate() and then |
| 780 | // PersistInstallDate() to set the kPrefsInstallDateDays state |
| 781 | // variable. Once that is done, we'll then report back in future |
| 782 | // Omaha requests. This works exactly because OOBE triggers an |
| 783 | // update check. |
| 784 | // |
| 785 | // However, if OOBE is complete and the kPrefsInstallDateDays state |
| 786 | // variable is not set, there are two possibilities |
| 787 | // |
| 788 | // 1. The update check in OOBE failed so we never got a response |
| 789 | // from Omaha (no network etc.); or |
| 790 | // |
| 791 | // 2. OOBE was done on an older version that didn't write to the |
| 792 | // kPrefsInstallDateDays state variable. |
| 793 | // |
| 794 | // In both cases, we approximate the install date by simply |
| 795 | // inspecting the timestamp of when OOBE happened. |
| 796 | |
| 797 | Time time_of_oobe; |
Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 798 | if (!system_state->hardware()->IsOOBEEnabled() || |
| 799 | !system_state->hardware()->IsOOBEComplete(&time_of_oobe)) { |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 800 | LOG(INFO) << "Not generating Omaha InstallData as we have " |
Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 801 | << "no prefs file and OOBE is not complete or not enabled."; |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 802 | return -1; |
| 803 | } |
| 804 | |
| 805 | int num_days; |
| 806 | if (!utils::ConvertToOmahaInstallDate(time_of_oobe, &num_days)) { |
| 807 | LOG(ERROR) << "Not generating Omaha InstallData from time of OOBE " |
| 808 | << "as its value '" << utils::ToString(time_of_oobe) |
| 809 | << "' looks suspicious."; |
| 810 | return -1; |
| 811 | } |
| 812 | |
| 813 | // Persist this to disk, for future use. |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 814 | if (!OmahaRequestAction::PersistInstallDate( |
| 815 | system_state, num_days, kProvisionedFromOOBEMarker)) |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 816 | return -1; |
| 817 | |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 818 | LOG(INFO) << "Set the Omaha InstallDate from OOBE time-stamp to " << num_days |
| 819 | << " days"; |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 820 | |
| 821 | return num_days; |
| 822 | } |
| 823 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 824 | void OmahaRequestAction::PerformAction() { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 825 | http_fetcher_->set_delegate(this); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 826 | InitPingDays(); |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 827 | if (ping_only_ && !ShouldPing()) { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 828 | processor_->ActionComplete(this, ErrorCode::kSuccess); |
Thieu Le | b44e9e8 | 2011-06-06 14:34:04 -0700 | [diff] [blame] | 829 | return; |
| 830 | } |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 831 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 832 | string request_post(GetRequestXml(event_.get(), |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 833 | params_, |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 834 | ping_only_, |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 835 | ShouldPing(), // include_ping |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 836 | ping_active_days_, |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 837 | ping_roll_call_days_, |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 838 | GetInstallDate(system_state_), |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 839 | system_state_)); |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 840 | |
Sen Jiang | 42fa45e | 2018-03-12 11:02:14 -0700 | [diff] [blame] | 841 | // Set X-Goog-Update headers. |
Alex Deymo | 14ad88e | 2016-06-29 12:30:14 -0700 | [diff] [blame] | 842 | http_fetcher_->SetHeader(kXGoogleUpdateInteractivity, |
| 843 | params_->interactive() ? "fg" : "bg"); |
| 844 | http_fetcher_->SetHeader(kXGoogleUpdateAppId, params_->GetAppId()); |
| 845 | http_fetcher_->SetHeader( |
| 846 | kXGoogleUpdateUpdater, |
| 847 | base::StringPrintf( |
| 848 | "%s-%s", constants::kOmahaUpdaterID, kOmahaUpdaterVersion)); |
| 849 | |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 850 | http_fetcher_->SetPostData( |
| 851 | request_post.data(), request_post.size(), kHttpContentTypeTextXml); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 852 | LOG(INFO) << "Posting an Omaha request to " << params_->update_url(); |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 853 | LOG(INFO) << "Request: " << request_post; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 854 | http_fetcher_->BeginTransfer(params_->update_url()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 857 | void OmahaRequestAction::TerminateProcessing() { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 858 | http_fetcher_->TerminateTransfer(); |
| 859 | } |
| 860 | |
| 861 | // We just store the response in the buffer. Once we've received all bytes, |
| 862 | // we'll look in the buffer and decide what to do. |
Amin Hassani | 0cd9d77 | 2018-07-31 23:55:43 -0700 | [diff] [blame] | 863 | bool OmahaRequestAction::ReceivedBytes(HttpFetcher* fetcher, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 864 | const void* bytes, |
| 865 | size_t length) { |
| 866 | const uint8_t* byte_ptr = reinterpret_cast<const uint8_t*>(bytes); |
| 867 | response_buffer_.insert(response_buffer_.end(), byte_ptr, byte_ptr + length); |
Amin Hassani | 0cd9d77 | 2018-07-31 23:55:43 -0700 | [diff] [blame] | 868 | return true; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | namespace { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 872 | |
| 873 | // Parses a 64 bit base-10 int from a string and returns it. Returns 0 |
| 874 | // on error. If the string contains "0", that's indistinguishable from |
| 875 | // error. |
| 876 | off_t ParseInt(const string& str) { |
| 877 | off_t ret = 0; |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 878 | int rc = sscanf(str.c_str(), "%" PRIi64, &ret); // NOLINT(runtime/printf) |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 879 | if (rc < 1) { |
| 880 | // failure |
| 881 | return 0; |
| 882 | } |
| 883 | return ret; |
| 884 | } |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 885 | |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 886 | // Parses |str| and returns |true| if, and only if, its value is "true". |
| 887 | bool ParseBool(const string& str) { |
| 888 | return str == "true"; |
| 889 | } |
| 890 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 891 | // Update the last ping day preferences based on the server daystart |
| 892 | // response. Returns true on success, false otherwise. |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 893 | bool UpdateLastPingDays(OmahaParserData* parser_data, PrefsInterface* prefs) { |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 894 | int64_t elapsed_seconds = 0; |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 895 | TEST_AND_RETURN_FALSE(base::StringToInt64( |
| 896 | parser_data->daystart_elapsed_seconds, &elapsed_seconds)); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 897 | TEST_AND_RETURN_FALSE(elapsed_seconds >= 0); |
| 898 | |
| 899 | // Remember the local time that matches the server's last midnight |
| 900 | // time. |
| 901 | Time daystart = Time::Now() - TimeDelta::FromSeconds(elapsed_seconds); |
| 902 | prefs->SetInt64(kPrefsLastActivePingDay, daystart.ToInternalValue()); |
| 903 | prefs->SetInt64(kPrefsLastRollCallPingDay, daystart.ToInternalValue()); |
| 904 | return true; |
| 905 | } |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 906 | |
| 907 | // Parses the package node in the given XML document and populates |
| 908 | // |output_object| if valid. Returns true if we should continue the parsing. |
| 909 | // False otherwise, in which case it sets any error code using |completer|. |
| 910 | bool ParsePackage(OmahaParserData::App* app, |
| 911 | OmahaResponse* output_object, |
| 912 | ScopedActionCompleter* completer) { |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 913 | if (app->updatecheck_status.empty() || |
| 914 | app->updatecheck_status == kValNoUpdate) { |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 915 | if (!app->packages.empty()) { |
| 916 | LOG(ERROR) << "No update in this <app> but <package> is not empty."; |
| 917 | completer->set_code(ErrorCode::kOmahaResponseInvalid); |
| 918 | return false; |
| 919 | } |
| 920 | return true; |
| 921 | } |
| 922 | if (app->packages.empty()) { |
| 923 | LOG(ERROR) << "Omaha Response has no packages"; |
| 924 | completer->set_code(ErrorCode::kOmahaResponseInvalid); |
| 925 | return false; |
| 926 | } |
| 927 | if (app->url_codebase.empty()) { |
| 928 | LOG(ERROR) << "No Omaha Response URLs"; |
| 929 | completer->set_code(ErrorCode::kOmahaResponseInvalid); |
| 930 | return false; |
| 931 | } |
| 932 | LOG(INFO) << "Found " << app->url_codebase.size() << " url(s)"; |
| 933 | vector<string> metadata_sizes = |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 934 | base::SplitString(app->action_postinstall_attrs[kAttrMetadataSize], |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 935 | ":", |
| 936 | base::TRIM_WHITESPACE, |
| 937 | base::SPLIT_WANT_ALL); |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 938 | vector<string> metadata_signatures = base::SplitString( |
| 939 | app->action_postinstall_attrs[kAttrMetadataSignatureRsa], |
| 940 | ":", |
| 941 | base::TRIM_WHITESPACE, |
| 942 | base::SPLIT_WANT_ALL); |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 943 | vector<string> is_delta_payloads = |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 944 | base::SplitString(app->action_postinstall_attrs[kAttrIsDeltaPayload], |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 945 | ":", |
| 946 | base::TRIM_WHITESPACE, |
| 947 | base::SPLIT_WANT_ALL); |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 948 | for (size_t i = 0; i < app->packages.size(); i++) { |
| 949 | const auto& package = app->packages[i]; |
| 950 | if (package.name.empty()) { |
| 951 | LOG(ERROR) << "Omaha Response has empty package name"; |
| 952 | completer->set_code(ErrorCode::kOmahaResponseInvalid); |
| 953 | return false; |
| 954 | } |
| 955 | LOG(INFO) << "Found package " << package.name; |
| 956 | |
| 957 | OmahaResponse::Package out_package; |
| 958 | for (const string& codebase : app->url_codebase) { |
| 959 | if (codebase.empty()) { |
| 960 | LOG(ERROR) << "Omaha Response URL has empty codebase"; |
| 961 | completer->set_code(ErrorCode::kOmahaResponseInvalid); |
| 962 | return false; |
| 963 | } |
| 964 | out_package.payload_urls.push_back(codebase + package.name); |
| 965 | } |
| 966 | // Parse the payload size. |
| 967 | base::StringToUint64(package.size, &out_package.size); |
| 968 | if (out_package.size <= 0) { |
| 969 | LOG(ERROR) << "Omaha Response has invalid payload size: " << package.size; |
| 970 | completer->set_code(ErrorCode::kOmahaResponseInvalid); |
| 971 | return false; |
| 972 | } |
| 973 | LOG(INFO) << "Payload size = " << out_package.size << " bytes"; |
| 974 | |
| 975 | if (i < metadata_sizes.size()) |
| 976 | base::StringToUint64(metadata_sizes[i], &out_package.metadata_size); |
| 977 | LOG(INFO) << "Payload metadata size = " << out_package.metadata_size |
| 978 | << " bytes"; |
| 979 | |
| 980 | if (i < metadata_signatures.size()) |
| 981 | out_package.metadata_signature = metadata_signatures[i]; |
| 982 | LOG(INFO) << "Payload metadata signature = " |
| 983 | << out_package.metadata_signature; |
| 984 | |
| 985 | out_package.hash = package.hash; |
| 986 | if (out_package.hash.empty()) { |
| 987 | LOG(ERROR) << "Omaha Response has empty hash_sha256 value"; |
| 988 | completer->set_code(ErrorCode::kOmahaResponseInvalid); |
| 989 | return false; |
| 990 | } |
| 991 | LOG(INFO) << "Payload hash = " << out_package.hash; |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 992 | |
| 993 | if (i < is_delta_payloads.size()) |
| 994 | out_package.is_delta = ParseBool(is_delta_payloads[i]); |
| 995 | LOG(INFO) << "Payload is delta = " << utils::ToString(out_package.is_delta); |
| 996 | |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 997 | output_object->packages.push_back(std::move(out_package)); |
| 998 | } |
| 999 | |
| 1000 | return true; |
| 1001 | } |
| 1002 | |
Zentaro Kavanagh | 0ff621c | 2018-07-13 13:06:56 -0700 | [diff] [blame] | 1003 | // Parses the 2 key version strings kernel_version and firmware_version. If the |
| 1004 | // field is not present, or cannot be parsed the values default to 0xffff. |
| 1005 | void ParseRollbackVersions(OmahaParserData* parser_data, |
| 1006 | OmahaResponse* output_object) { |
| 1007 | utils::ParseRollbackKeyVersion( |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 1008 | parser_data->updatecheck_attrs[kAttrFirmwareVersion], |
Zentaro Kavanagh | 0ff621c | 2018-07-13 13:06:56 -0700 | [diff] [blame] | 1009 | &output_object->rollback_key_version.firmware_key, |
| 1010 | &output_object->rollback_key_version.firmware); |
| 1011 | utils::ParseRollbackKeyVersion( |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 1012 | parser_data->updatecheck_attrs[kAttrKernelVersion], |
Zentaro Kavanagh | 0ff621c | 2018-07-13 13:06:56 -0700 | [diff] [blame] | 1013 | &output_object->rollback_key_version.kernel_key, |
| 1014 | &output_object->rollback_key_version.kernel); |
| 1015 | } |
| 1016 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1017 | } // namespace |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1018 | |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 1019 | bool OmahaRequestAction::ParseResponse(OmahaParserData* parser_data, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1020 | OmahaResponse* output_object, |
| 1021 | ScopedActionCompleter* completer) { |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 1022 | if (parser_data->apps.empty()) { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1023 | completer->set_code(ErrorCode::kOmahaResponseInvalid); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1024 | return false; |
| 1025 | } |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 1026 | LOG(INFO) << "Found " << parser_data->apps.size() << " <app>."; |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1027 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1028 | // chromium-os:37289: The PollInterval is not supported by Omaha server |
| 1029 | // 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] | 1030 | // slow down the request rate from the server-side. Note that the PollInterval |
| 1031 | // is not persisted, so it has to be sent by the server on every response to |
| 1032 | // guarantee that the scheduler uses this value (otherwise, if the device got |
| 1033 | // rebooted after the last server-indicated value, it'll revert to the default |
| 1034 | // value). Also kDefaultMaxUpdateChecks value for the scattering logic is |
| 1035 | // based on the assumption that we perform an update check every hour so that |
| 1036 | // the max value of 8 will roughly be equivalent to one work day. If we decide |
| 1037 | // to use PollInterval permanently, we should update the |
| 1038 | // max_update_checks_allowed to take PollInterval into account. Note: The |
| 1039 | // parsing for PollInterval happens even before parsing of the status because |
| 1040 | // 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] | 1041 | base::StringToInt(parser_data->updatecheck_poll_interval, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1042 | &output_object->poll_interval); |
| 1043 | |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1044 | // Check for the "elapsed_days" attribute in the "daystart" |
| 1045 | // element. This is the number of days since Jan 1 2007, 0:00 |
| 1046 | // PST. If we don't have a persisted value of the Omaha InstallDate, |
| 1047 | // we'll use it to calculate it and then persist it. |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 1048 | if (ParseInstallDate(parser_data, output_object) && |
| 1049 | !HasInstallDate(system_state_)) { |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1050 | // Since output_object->install_date_days is never negative, the |
| 1051 | // elapsed_days -> install-date calculation is reduced to simply |
| 1052 | // rounding down to the nearest number divisible by 7. |
| 1053 | int remainder = output_object->install_date_days % 7; |
| 1054 | int install_date_days_rounded = |
| 1055 | output_object->install_date_days - remainder; |
| 1056 | if (PersistInstallDate(system_state_, |
| 1057 | install_date_days_rounded, |
| 1058 | kProvisionedFromOmahaResponse)) { |
| 1059 | LOG(INFO) << "Set the Omaha InstallDate from Omaha Response to " |
| 1060 | << install_date_days_rounded << " days"; |
| 1061 | } |
| 1062 | } |
| 1063 | |
Alex Deymo | 00d79ac | 2015-06-29 15:41:49 -0700 | [diff] [blame] | 1064 | // We persist the cohorts sent by omaha even if the status is "noupdate". |
Sen Jiang | b1e063a | 2017-09-15 17:44:31 -0700 | [diff] [blame] | 1065 | for (const auto& app : parser_data->apps) { |
| 1066 | if (app.id == params_->GetAppId()) { |
| 1067 | if (app.cohort_set) |
| 1068 | PersistCohortData(kPrefsOmahaCohort, app.cohort); |
| 1069 | if (app.cohorthint_set) |
| 1070 | PersistCohortData(kPrefsOmahaCohortHint, app.cohorthint); |
| 1071 | if (app.cohortname_set) |
| 1072 | PersistCohortData(kPrefsOmahaCohortName, app.cohortname); |
| 1073 | break; |
| 1074 | } |
| 1075 | } |
Alex Deymo | 00d79ac | 2015-06-29 15:41:49 -0700 | [diff] [blame] | 1076 | |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 1077 | // Parse the updatecheck attributes. |
| 1078 | PersistEolStatus(parser_data->updatecheck_attrs); |
Marton Hunyady | 199152d | 2018-05-07 19:08:48 +0200 | [diff] [blame] | 1079 | // Rollback-related updatecheck attributes. |
| 1080 | // Defaults to false if attribute is not present. |
| 1081 | output_object->is_rollback = |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 1082 | ParseBool(parser_data->updatecheck_attrs[kAttrRollback]); |
Zentaro Kavanagh | 0ff621c | 2018-07-13 13:06:56 -0700 | [diff] [blame] | 1083 | |
| 1084 | // Parses the rollback versions of the current image. If the fields do not |
| 1085 | // exist they default to 0xffff for the 4 key versions. |
| 1086 | ParseRollbackVersions(parser_data, output_object); |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 1087 | |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 1088 | if (!ParseStatus(parser_data, output_object, completer)) |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1089 | return false; |
| 1090 | |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 1091 | if (!ParseParams(parser_data, output_object, completer)) |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1092 | return false; |
| 1093 | |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 1094 | // Package has to be parsed after Params now because ParseParams need to make |
| 1095 | // sure that postinstall action exists. |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 1096 | for (auto& app : parser_data->apps) |
| 1097 | if (!ParsePackage(&app, output_object, completer)) |
| 1098 | return false; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 1099 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1100 | return true; |
| 1101 | } |
| 1102 | |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 1103 | bool OmahaRequestAction::ParseStatus(OmahaParserData* parser_data, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1104 | OmahaResponse* output_object, |
| 1105 | ScopedActionCompleter* completer) { |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 1106 | output_object->update_exists = false; |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 1107 | for (const auto& app : parser_data->apps) { |
| 1108 | const string& status = app.updatecheck_status; |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 1109 | if (status == kValNoUpdate) { |
Sen Jiang | 00adf7b | 2017-06-26 15:57:29 -0700 | [diff] [blame] | 1110 | // Don't update if any app has status="noupdate". |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 1111 | LOG(INFO) << "No update for <app> " << app.id; |
Sen Jiang | 00adf7b | 2017-06-26 15:57:29 -0700 | [diff] [blame] | 1112 | output_object->update_exists = false; |
| 1113 | break; |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 1114 | } else if (status == "ok") { |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 1115 | auto const& attr_no_update = |
| 1116 | app.action_postinstall_attrs.find(kAttrNoUpdate); |
| 1117 | if (attr_no_update != app.action_postinstall_attrs.end() && |
| 1118 | attr_no_update->second == "true") { |
Sen Jiang | 00adf7b | 2017-06-26 15:57:29 -0700 | [diff] [blame] | 1119 | // noupdate="true" in postinstall attributes means it's an update to |
| 1120 | // self, only update if there's at least one app really have update. |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 1121 | LOG(INFO) << "Update to self for <app> " << app.id; |
Sen Jiang | 00adf7b | 2017-06-26 15:57:29 -0700 | [diff] [blame] | 1122 | } else { |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 1123 | LOG(INFO) << "Update for <app> " << app.id; |
Sen Jiang | 00adf7b | 2017-06-26 15:57:29 -0700 | [diff] [blame] | 1124 | output_object->update_exists = true; |
| 1125 | } |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 1126 | } else if (status.empty() && params_->is_install() && |
| 1127 | params_->GetAppId() == app.id) { |
| 1128 | // Skips the platform app for install operation. |
| 1129 | LOG(INFO) << "No payload (and ignore) for <app> " << app.id; |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 1130 | } else { |
| 1131 | LOG(ERROR) << "Unknown Omaha response status: " << status; |
| 1132 | completer->set_code(ErrorCode::kOmahaResponseInvalid); |
| 1133 | return false; |
| 1134 | } |
| 1135 | } |
| 1136 | if (!output_object->update_exists) { |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1137 | SetOutputObject(*output_object); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1138 | completer->set_code(ErrorCode::kSuccess); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 1141 | return output_object->update_exists; |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 1144 | bool OmahaRequestAction::ParseParams(OmahaParserData* parser_data, |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1145 | OmahaResponse* output_object, |
| 1146 | ScopedActionCompleter* completer) { |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 1147 | map<string, string> attrs; |
| 1148 | for (auto& app : parser_data->apps) { |
Aaron Wood | 7dcdedf | 2017-09-06 17:17:41 -0700 | [diff] [blame] | 1149 | if (app.id == params_->GetAppId()) { |
| 1150 | // this is the app (potentially the only app) |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 1151 | output_object->version = app.manifest_version; |
Aaron Wood | 7dcdedf | 2017-09-06 17:17:41 -0700 | [diff] [blame] | 1152 | } else if (!params_->system_app_id().empty() && |
| 1153 | app.id == params_->system_app_id()) { |
| 1154 | // this is the system app (this check is intentionally skipped if there is |
| 1155 | // no system_app_id set) |
| 1156 | output_object->system_version = app.manifest_version; |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 1157 | } else if (params_->is_install() && |
| 1158 | app.manifest_version != params_->app_version()) { |
| 1159 | LOG(WARNING) << "An app has a different version (" << app.manifest_version |
| 1160 | << ") that is different than platform app version (" |
| 1161 | << params_->app_version() << ")"; |
Aaron Wood | 7dcdedf | 2017-09-06 17:17:41 -0700 | [diff] [blame] | 1162 | } |
| 1163 | if (!app.action_postinstall_attrs.empty() && attrs.empty()) { |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 1164 | attrs = app.action_postinstall_attrs; |
Aaron Wood | 7dcdedf | 2017-09-06 17:17:41 -0700 | [diff] [blame] | 1165 | } |
Sen Jiang | 8125968 | 2017-03-30 15:11:30 -0700 | [diff] [blame] | 1166 | } |
Xiaochu Liu | 6310be6 | 2018-10-11 15:09:03 -0700 | [diff] [blame] | 1167 | if (params_->is_install()) { |
| 1168 | LOG(INFO) << "Use request version for Install operation."; |
| 1169 | output_object->version = params_->app_version(); |
| 1170 | } |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 1171 | if (output_object->version.empty()) { |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 1172 | LOG(ERROR) << "Omaha Response does not have version in manifest!"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1173 | completer->set_code(ErrorCode::kOmahaResponseInvalid); |
Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 1174 | return false; |
| 1175 | } |
| 1176 | |
| 1177 | LOG(INFO) << "Received omaha response to update to version " |
| 1178 | << output_object->version; |
| 1179 | |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 1180 | if (attrs.empty()) { |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1181 | LOG(ERROR) << "Omaha Response has no postinstall event action"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1182 | completer->set_code(ErrorCode::kOmahaResponseInvalid); |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1183 | return false; |
| 1184 | } |
| 1185 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1186 | // Get the optional properties one by one. |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 1187 | output_object->more_info_url = attrs[kAttrMoreInfo]; |
| 1188 | output_object->prompt = ParseBool(attrs[kAttrPrompt]); |
| 1189 | output_object->deadline = attrs[kAttrDeadline]; |
| 1190 | output_object->max_days_to_scatter = ParseInt(attrs[kAttrMaxDaysToScatter]); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1191 | output_object->disable_p2p_for_downloading = |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 1192 | ParseBool(attrs[kAttrDisableP2PForDownloading]); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1193 | output_object->disable_p2p_for_sharing = |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 1194 | ParseBool(attrs[kAttrDisableP2PForSharing]); |
| 1195 | output_object->public_key_rsa = attrs[kAttrPublicKeyRsa]; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 1196 | |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 1197 | string max = attrs[kAttrMaxFailureCountPerUrl]; |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 1198 | if (!base::StringToUint(max, &output_object->max_failure_count_per_url)) |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 1199 | output_object->max_failure_count_per_url = kDefaultMaxFailureCountPerUrl; |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1200 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 1201 | output_object->disable_payload_backoff = |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 1202 | ParseBool(attrs[kAttrDisablePayloadBackoff]); |
| 1203 | output_object->powerwash_required = ParseBool(attrs[kAttrPowerwash]); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 1204 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1205 | return true; |
| 1206 | } |
| 1207 | |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 1208 | // 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] | 1209 | // and fill in the appropriate fields of the output object. Also, notifies |
| 1210 | // the processor that we're done. |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1211 | void OmahaRequestAction::TransferComplete(HttpFetcher* fetcher, |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1212 | bool successful) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1213 | ScopedActionCompleter completer(processor_, this); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1214 | string current_response(response_buffer_.begin(), response_buffer_.end()); |
| 1215 | LOG(INFO) << "Omaha request response: " << current_response; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1216 | |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1217 | PayloadStateInterface* const payload_state = system_state_->payload_state(); |
| 1218 | |
Zentaro Kavanagh | 76af266 | 2018-05-15 10:54:53 -0700 | [diff] [blame] | 1219 | // Set the max kernel key version based on whether rollback is allowed. |
| 1220 | SetMaxKernelKeyVersionForRollback(); |
| 1221 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1222 | // Events are best effort transactions -- assume they always succeed. |
| 1223 | if (IsEvent()) { |
| 1224 | CHECK(!HasOutputPipe()) << "No output pipe allowed for event requests."; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1225 | completer.set_code(ErrorCode::kSuccess); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1226 | return; |
| 1227 | } |
| 1228 | |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 1229 | if (!successful) { |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 1230 | LOG(ERROR) << "Omaha request network transfer failed."; |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 1231 | int code = GetHTTPResponseCode(); |
| 1232 | // Makes sure we send sane error values. |
| 1233 | if (code < 0 || code >= 1000) { |
| 1234 | code = 999; |
| 1235 | } |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1236 | completer.set_code(static_cast<ErrorCode>( |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1237 | static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + code)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1238 | return; |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 1239 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1240 | |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 1241 | XML_Parser parser = XML_ParserCreate(nullptr); |
David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 1242 | OmahaParserData parser_data(parser); |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 1243 | XML_SetUserData(parser, &parser_data); |
| 1244 | XML_SetElementHandler(parser, ParserHandlerStart, ParserHandlerEnd); |
David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 1245 | XML_SetEntityDeclHandler(parser, ParserHandlerEntityDecl); |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1246 | XML_Status res = |
| 1247 | XML_Parse(parser, |
| 1248 | reinterpret_cast<const char*>(response_buffer_.data()), |
| 1249 | response_buffer_.size(), |
| 1250 | XML_TRUE); |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 1251 | |
| 1252 | if (res != XML_STATUS_OK || parser_data.failed) { |
Alex Deymo | a9bb7dc | 2015-06-19 09:50:23 -0700 | [diff] [blame] | 1253 | LOG(ERROR) << "Omaha response not valid XML: " |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1254 | << XML_ErrorString(XML_GetErrorCode(parser)) << " at line " |
| 1255 | << XML_GetCurrentLineNumber(parser) << " col " |
| 1256 | << XML_GetCurrentColumnNumber(parser); |
Amin Hassani | 91f48b4 | 2018-08-06 17:47:14 -0700 | [diff] [blame] | 1257 | XML_ParserFree(parser); |
David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 1258 | ErrorCode error_code = ErrorCode::kOmahaRequestXMLParseError; |
| 1259 | if (response_buffer_.empty()) { |
| 1260 | error_code = ErrorCode::kOmahaRequestEmptyResponseError; |
| 1261 | } else if (parser_data.entity_decl) { |
| 1262 | error_code = ErrorCode::kOmahaRequestXMLHasEntityDecl; |
| 1263 | } |
| 1264 | completer.set_code(error_code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1265 | return; |
| 1266 | } |
Amin Hassani | 91f48b4 | 2018-08-06 17:47:14 -0700 | [diff] [blame] | 1267 | XML_ParserFree(parser); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1268 | |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 1269 | // Update the last ping day preferences based on the server daystart response |
| 1270 | // even if we didn't send a ping. Omaha always includes the daystart in the |
| 1271 | // response, but log the error if it didn't. |
| 1272 | LOG_IF(ERROR, !UpdateLastPingDays(&parser_data, system_state_->prefs())) |
| 1273 | << "Failed to update the last ping day preferences!"; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 1274 | |
Amin Hassani | 1677e81 | 2017-06-21 13:36:36 -0700 | [diff] [blame] | 1275 | // Sets first_active_omaha_ping_sent to true (vpd in CrOS). We only do this if |
| 1276 | // we have got a response from omaha and if its value has never been set to |
| 1277 | // true before. Failure of this function should be ignored. There should be no |
| 1278 | // need to check if a=-1 has been sent because older devices have already sent |
| 1279 | // their a=-1 in the past and we have to set first_active_omaha_ping_sent for |
| 1280 | // future checks. |
| 1281 | if (!system_state_->hardware()->GetFirstActiveOmahaPingSent()) { |
Amin Hassani | 80f4d4c | 2018-05-16 13:34:00 -0700 | [diff] [blame] | 1282 | if (!system_state_->hardware()->SetFirstActiveOmahaPingSent()) { |
| 1283 | system_state_->metrics_reporter()->ReportInternalErrorCode( |
| 1284 | ErrorCode::kFirstActiveOmahaPingSentPersistenceError); |
| 1285 | } |
Amin Hassani | 1677e81 | 2017-06-21 13:36:36 -0700 | [diff] [blame] | 1286 | } |
| 1287 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 1288 | if (!HasOutputPipe()) { |
| 1289 | // Just set success to whether or not the http transfer succeeded, |
| 1290 | // which must be true at this point in the code. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1291 | completer.set_code(ErrorCode::kSuccess); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 1292 | return; |
| 1293 | } |
| 1294 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1295 | OmahaResponse output_object; |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 1296 | if (!ParseResponse(&parser_data, &output_object, &completer)) |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1297 | return; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1298 | output_object.update_exists = true; |
| 1299 | SetOutputObject(output_object); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1300 | |
Toni Barzic | 61544e6 | 2018-10-11 14:37:30 -0700 | [diff] [blame] | 1301 | LoadOrPersistUpdateFirstSeenAtPref(); |
| 1302 | |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 1303 | ErrorCode error = ErrorCode::kSuccess; |
Toni Barzic | 61544e6 | 2018-10-11 14:37:30 -0700 | [diff] [blame] | 1304 | if (ShouldIgnoreUpdate(output_object, &error)) { |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 1305 | // No need to change output_object.update_exists here, since the value |
| 1306 | // has been output to the pipe. |
| 1307 | completer.set_code(error); |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 1308 | return; |
| 1309 | } |
| 1310 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1311 | // If Omaha says to disable p2p, respect that |
| 1312 | if (output_object.disable_p2p_for_downloading) { |
| 1313 | LOG(INFO) << "Forcibly disabling use of p2p for downloading as " |
| 1314 | << "requested by Omaha."; |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1315 | payload_state->SetUsingP2PForDownloading(false); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1316 | } |
| 1317 | if (output_object.disable_p2p_for_sharing) { |
| 1318 | LOG(INFO) << "Forcibly disabling use of p2p for sharing as " |
| 1319 | << "requested by Omaha."; |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1320 | payload_state->SetUsingP2PForSharing(false); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1321 | } |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1322 | |
| 1323 | // Update the payload state with the current response. The payload state |
| 1324 | // will automatically reset all stale state if this response is different |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 1325 | // from what's stored already. We are updating the payload state as late |
| 1326 | // as possible in this method so that if a new release gets pushed and then |
| 1327 | // got pulled back due to some issues, we don't want to clear our internal |
| 1328 | // state unnecessarily. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1329 | payload_state->SetResponse(output_object); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 1330 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1331 | // It could be we've already exceeded the deadline for when p2p is |
| 1332 | // allowed or that we've tried too many times with p2p. Check that. |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1333 | if (payload_state->GetUsingP2PForDownloading()) { |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1334 | payload_state->P2PNewAttempt(); |
| 1335 | if (!payload_state->P2PAttemptAllowed()) { |
| 1336 | LOG(INFO) << "Forcibly disabling use of p2p for downloading because " |
| 1337 | << "of previous failures when using p2p."; |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1338 | payload_state->SetUsingP2PForDownloading(false); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | // From here on, we'll complete stuff in CompleteProcessing() so |
| 1343 | // disable |completer| since we'll create a new one in that |
| 1344 | // function. |
| 1345 | completer.set_should_complete(false); |
| 1346 | |
| 1347 | // If we're allowed to use p2p for downloading we do not pay |
| 1348 | // attention to wall-clock-based waiting if the URL is indeed |
| 1349 | // available via p2p. Therefore, check if the file is available via |
| 1350 | // p2p before deferring... |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1351 | if (payload_state->GetUsingP2PForDownloading()) { |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1352 | LookupPayloadViaP2P(output_object); |
| 1353 | } else { |
| 1354 | CompleteProcessing(); |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | void OmahaRequestAction::CompleteProcessing() { |
| 1359 | ScopedActionCompleter completer(processor_, this); |
| 1360 | OmahaResponse& output_object = const_cast<OmahaResponse&>(GetOutputObject()); |
| 1361 | PayloadStateInterface* payload_state = system_state_->payload_state(); |
| 1362 | |
| 1363 | if (ShouldDeferDownload(&output_object)) { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 1364 | output_object.update_exists = false; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1365 | LOG(INFO) << "Ignoring Omaha updates as updates are deferred by policy."; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1366 | completer.set_code(ErrorCode::kOmahaUpdateDeferredPerPolicy); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 1367 | return; |
| 1368 | } |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1369 | |
Chris Sosa | 20f005c | 2013-09-05 13:53:08 -0700 | [diff] [blame] | 1370 | if (payload_state->ShouldBackoffDownload()) { |
| 1371 | output_object.update_exists = false; |
| 1372 | LOG(INFO) << "Ignoring Omaha updates in order to backoff our retry " |
| 1373 | << "attempts"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1374 | completer.set_code(ErrorCode::kOmahaUpdateDeferredForBackoff); |
Chris Sosa | 20f005c | 2013-09-05 13:53:08 -0700 | [diff] [blame] | 1375 | return; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1376 | } |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1377 | completer.set_code(ErrorCode::kSuccess); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | void OmahaRequestAction::OnLookupPayloadViaP2PCompleted(const string& url) { |
| 1381 | LOG(INFO) << "Lookup complete, p2p-client returned URL '" << url << "'"; |
| 1382 | if (!url.empty()) { |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1383 | system_state_->payload_state()->SetP2PUrl(url); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1384 | } else { |
| 1385 | LOG(INFO) << "Forcibly disabling use of p2p for downloading " |
| 1386 | << "because no suitable peer could be found."; |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1387 | system_state_->payload_state()->SetUsingP2PForDownloading(false); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1388 | } |
| 1389 | CompleteProcessing(); |
| 1390 | } |
| 1391 | |
| 1392 | void OmahaRequestAction::LookupPayloadViaP2P(const OmahaResponse& response) { |
David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1393 | // If the device is in the middle of an update, the state variables |
| 1394 | // kPrefsUpdateStateNextDataOffset, kPrefsUpdateStateNextDataLength |
| 1395 | // tracks the offset and length of the operation currently in |
| 1396 | // progress. The offset is based from the end of the manifest which |
| 1397 | // is kPrefsManifestMetadataSize bytes long. |
| 1398 | // |
| 1399 | // To make forward progress and avoid deadlocks, we need to find a |
| 1400 | // peer that has at least the entire operation we're currently |
| 1401 | // working on. Otherwise we may end up in a situation where two |
| 1402 | // devices bounce back and forth downloading from each other, |
| 1403 | // neither making any forward progress until one of them decides to |
| 1404 | // stop using p2p (via kMaxP2PAttempts and kMaxP2PAttemptTimeSeconds |
| 1405 | // safe-guards). See http://crbug.com/297170 for an example) |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1406 | size_t minimum_size = 0; |
David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1407 | int64_t manifest_metadata_size = 0; |
Alex Deymo | f25eb49 | 2016-02-26 00:20:08 -0800 | [diff] [blame] | 1408 | int64_t manifest_signature_size = 0; |
David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1409 | int64_t next_data_offset = 0; |
| 1410 | int64_t next_data_length = 0; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1411 | if (system_state_ && |
David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1412 | system_state_->prefs()->GetInt64(kPrefsManifestMetadataSize, |
| 1413 | &manifest_metadata_size) && |
| 1414 | manifest_metadata_size != -1 && |
Alex Deymo | f25eb49 | 2016-02-26 00:20:08 -0800 | [diff] [blame] | 1415 | system_state_->prefs()->GetInt64(kPrefsManifestSignatureSize, |
| 1416 | &manifest_signature_size) && |
| 1417 | manifest_signature_size != -1 && |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1418 | system_state_->prefs()->GetInt64(kPrefsUpdateStateNextDataOffset, |
David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1419 | &next_data_offset) && |
| 1420 | next_data_offset != -1 && |
| 1421 | system_state_->prefs()->GetInt64(kPrefsUpdateStateNextDataLength, |
| 1422 | &next_data_length)) { |
Alex Deymo | f25eb49 | 2016-02-26 00:20:08 -0800 | [diff] [blame] | 1423 | minimum_size = manifest_metadata_size + manifest_signature_size + |
| 1424 | next_data_offset + next_data_length; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1425 | } |
| 1426 | |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 1427 | // TODO(senj): Fix P2P for multiple package. |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 1428 | brillo::Blob raw_hash; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 1429 | if (!base::HexStringToBytes(response.packages[0].hash, &raw_hash)) |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 1430 | return; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 1431 | string file_id = |
| 1432 | utils::CalculateP2PFileId(raw_hash, response.packages[0].size); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1433 | if (system_state_->p2p_manager()) { |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 1434 | LOG(INFO) << "Checking if payload is available via p2p, file_id=" << file_id |
| 1435 | << " minimum_size=" << minimum_size; |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1436 | system_state_->p2p_manager()->LookupUrlForFile( |
| 1437 | file_id, |
| 1438 | minimum_size, |
David Zeuthen | 4cc5ed2 | 2014-01-15 12:35:03 -0800 | [diff] [blame] | 1439 | TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds), |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1440 | base::Bind(&OmahaRequestAction::OnLookupPayloadViaP2PCompleted, |
| 1441 | base::Unretained(this))); |
| 1442 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1445 | bool OmahaRequestAction::ShouldDeferDownload(OmahaResponse* output_object) { |
Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 1446 | if (params_->interactive()) { |
| 1447 | LOG(INFO) << "Not deferring download because update is interactive."; |
| 1448 | return false; |
| 1449 | } |
| 1450 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1451 | // If we're using p2p to download _and_ we have a p2p URL, we never |
| 1452 | // defer the download. This is because the download will always |
| 1453 | // happen from a peer on the LAN and we've been waiting in line for |
| 1454 | // our turn. |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1455 | const PayloadStateInterface* payload_state = system_state_->payload_state(); |
| 1456 | if (payload_state->GetUsingP2PForDownloading() && |
| 1457 | !payload_state->GetP2PUrl().empty()) { |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1458 | LOG(INFO) << "Download not deferred because download " |
| 1459 | << "will happen from a local peer (via p2p)."; |
| 1460 | return false; |
| 1461 | } |
| 1462 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1463 | // We should defer the downloads only if we've first satisfied the |
| 1464 | // wall-clock-based-waiting period and then the update-check-based waiting |
| 1465 | // period, if required. |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1466 | if (!params_->wall_clock_based_wait_enabled()) { |
Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 1467 | LOG(INFO) << "Wall-clock-based waiting period is not enabled," |
| 1468 | << " so no deferring needed."; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1469 | return false; |
| 1470 | } |
| 1471 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1472 | switch (IsWallClockBasedWaitingSatisfied(output_object)) { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1473 | case kWallClockWaitNotSatisfied: |
| 1474 | // We haven't even satisfied the first condition, passing the |
| 1475 | // wall-clock-based waiting period, so we should defer the downloads |
| 1476 | // until that happens. |
| 1477 | LOG(INFO) << "wall-clock-based-wait not satisfied."; |
| 1478 | return true; |
| 1479 | |
| 1480 | case kWallClockWaitDoneButUpdateCheckWaitRequired: |
| 1481 | LOG(INFO) << "wall-clock-based-wait satisfied and " |
| 1482 | << "update-check-based-wait required."; |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1483 | return !IsUpdateCheckCountBasedWaitingSatisfied(); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1484 | |
| 1485 | case kWallClockWaitDoneAndUpdateCheckWaitNotRequired: |
| 1486 | // Wall-clock-based waiting period is satisfied, and it's determined |
| 1487 | // that we do not need the update-check-based wait. so no need to |
| 1488 | // defer downloads. |
| 1489 | LOG(INFO) << "wall-clock-based-wait satisfied and " |
| 1490 | << "update-check-based-wait is not required."; |
| 1491 | return false; |
| 1492 | |
| 1493 | default: |
| 1494 | // Returning false for this default case so we err on the |
| 1495 | // side of downloading updates than deferring in case of any bugs. |
| 1496 | NOTREACHED(); |
| 1497 | return false; |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | OmahaRequestAction::WallClockWaitResult |
| 1502 | OmahaRequestAction::IsWallClockBasedWaitingSatisfied( |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1503 | OmahaResponse* output_object) { |
May Lippert | 60aa3ca | 2018-08-15 16:55:29 -0700 | [diff] [blame] | 1504 | Time update_first_seen_at = LoadOrPersistUpdateFirstSeenAtPref(); |
| 1505 | if (update_first_seen_at == base::Time()) { |
| 1506 | LOG(INFO) << "Not scattering as UpdateFirstSeenAt value cannot be read or " |
| 1507 | "persisted"; |
| 1508 | return kWallClockWaitDoneAndUpdateCheckWaitNotRequired; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1509 | } |
| 1510 | |
Sen Jiang | 7c1171e | 2016-06-23 11:35:40 -0700 | [diff] [blame] | 1511 | TimeDelta elapsed_time = |
| 1512 | system_state_->clock()->GetWallclockTime() - update_first_seen_at; |
| 1513 | TimeDelta max_scatter_period = |
| 1514 | TimeDelta::FromDays(output_object->max_days_to_scatter); |
Adolfo Victoria | 497044c | 2018-07-18 07:51:42 -0700 | [diff] [blame] | 1515 | int64_t staging_wait_time_in_days = 0; |
| 1516 | // Use staging and its default max value if staging is on. |
| 1517 | if (system_state_->prefs()->GetInt64(kPrefsWallClockStagingWaitPeriod, |
| 1518 | &staging_wait_time_in_days) && |
| 1519 | staging_wait_time_in_days > 0) |
| 1520 | max_scatter_period = TimeDelta::FromDays(kMaxWaitTimeStagingInDays); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1521 | |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1522 | LOG(INFO) << "Waiting Period = " |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1523 | << utils::FormatSecs(params_->waiting_period().InSeconds()) |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1524 | << ", Time Elapsed = " |
| 1525 | << utils::FormatSecs(elapsed_time.InSeconds()) |
Adolfo Victoria | 497044c | 2018-07-18 07:51:42 -0700 | [diff] [blame] | 1526 | << ", MaxDaysToScatter = " << max_scatter_period.InDays(); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1527 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1528 | if (!output_object->deadline.empty()) { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1529 | // The deadline is set for all rules which serve a delta update from a |
| 1530 | // previous FSI, which means this update will be applied mostly in OOBE |
| 1531 | // cases. For these cases, we shouldn't scatter so as to finish the OOBE |
| 1532 | // quickly. |
| 1533 | LOG(INFO) << "Not scattering as deadline flag is set"; |
| 1534 | return kWallClockWaitDoneAndUpdateCheckWaitNotRequired; |
| 1535 | } |
| 1536 | |
| 1537 | if (max_scatter_period.InDays() == 0) { |
| 1538 | // This means the Omaha rule creator decides that this rule |
| 1539 | // should not be scattered irrespective of the policy. |
| 1540 | LOG(INFO) << "Not scattering as MaxDaysToScatter in rule is 0."; |
| 1541 | return kWallClockWaitDoneAndUpdateCheckWaitNotRequired; |
| 1542 | } |
| 1543 | |
| 1544 | if (elapsed_time > max_scatter_period) { |
Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1545 | // This means we've waited more than the upperbound wait in the rule |
| 1546 | // from the time we first saw a valid update available to us. |
| 1547 | // This will prevent update starvation. |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1548 | LOG(INFO) << "Not scattering as we're past the MaxDaysToScatter limit."; |
| 1549 | return kWallClockWaitDoneAndUpdateCheckWaitNotRequired; |
| 1550 | } |
| 1551 | |
| 1552 | // This means we are required to participate in scattering. |
| 1553 | // See if our turn has arrived now. |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1554 | TimeDelta remaining_wait_time = params_->waiting_period() - elapsed_time; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1555 | if (remaining_wait_time.InSeconds() <= 0) { |
| 1556 | // Yes, it's our turn now. |
| 1557 | LOG(INFO) << "Successfully passed the wall-clock-based-wait."; |
| 1558 | |
| 1559 | // But we can't download until the update-check-count-based wait is also |
| 1560 | // satisfied, so mark it as required now if update checks are enabled. |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1561 | return params_->update_check_count_wait_enabled() |
| 1562 | ? kWallClockWaitDoneButUpdateCheckWaitRequired |
| 1563 | : kWallClockWaitDoneAndUpdateCheckWaitNotRequired; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | // Not our turn yet, so we have to wait until our turn to |
| 1567 | // help scatter the downloads across all clients of the enterprise. |
| 1568 | LOG(INFO) << "Update deferred for another " |
| 1569 | << utils::FormatSecs(remaining_wait_time.InSeconds()) |
| 1570 | << " per policy."; |
| 1571 | return kWallClockWaitNotSatisfied; |
| 1572 | } |
| 1573 | |
Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1574 | bool OmahaRequestAction::IsUpdateCheckCountBasedWaitingSatisfied() { |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 1575 | int64_t update_check_count_value; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1576 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1577 | if (system_state_->prefs()->Exists(kPrefsUpdateCheckCount)) { |
| 1578 | if (!system_state_->prefs()->GetInt64(kPrefsUpdateCheckCount, |
| 1579 | &update_check_count_value)) { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1580 | // We are unable to read the update check count from file for some reason. |
| 1581 | // So let's proceed anyway so as to not stall the update. |
| 1582 | LOG(ERROR) << "Unable to read update check count. " |
| 1583 | << "Skipping update-check-count-based-wait."; |
| 1584 | return true; |
| 1585 | } |
| 1586 | } else { |
| 1587 | // This file does not exist. This means we haven't started our update |
| 1588 | // check count down yet, so this is the right time to start the count down. |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1589 | update_check_count_value = |
| 1590 | base::RandInt(params_->min_update_checks_needed(), |
| 1591 | params_->max_update_checks_allowed()); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1592 | |
| 1593 | LOG(INFO) << "Randomly picked update check count value = " |
| 1594 | << update_check_count_value; |
| 1595 | |
| 1596 | // Write out the initial value of update_check_count_value. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1597 | if (!system_state_->prefs()->SetInt64(kPrefsUpdateCheckCount, |
| 1598 | update_check_count_value)) { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1599 | // We weren't able to write the update check count file for some reason. |
| 1600 | // So let's proceed anyway so as to not stall the update. |
| 1601 | LOG(ERROR) << "Unable to write update check count. " |
| 1602 | << "Skipping update-check-count-based-wait."; |
| 1603 | return true; |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | if (update_check_count_value == 0) { |
| 1608 | LOG(INFO) << "Successfully passed the update-check-based-wait."; |
| 1609 | return true; |
| 1610 | } |
| 1611 | |
| 1612 | if (update_check_count_value < 0 || |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1613 | update_check_count_value > params_->max_update_checks_allowed()) { |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1614 | // We err on the side of skipping scattering logic instead of stalling |
| 1615 | // a machine from receiving any updates in case of any unexpected state. |
| 1616 | LOG(ERROR) << "Invalid value for update check count detected. " |
| 1617 | << "Skipping update-check-count-based-wait."; |
| 1618 | return true; |
| 1619 | } |
| 1620 | |
| 1621 | // Legal value, we need to wait for more update checks to happen |
| 1622 | // until this becomes 0. |
| 1623 | LOG(INFO) << "Deferring Omaha updates for another " |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1624 | << update_check_count_value << " update checks per policy"; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1625 | return false; |
| 1626 | } |
| 1627 | |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1628 | // static |
David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 1629 | bool OmahaRequestAction::ParseInstallDate(OmahaParserData* parser_data, |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1630 | OmahaResponse* output_object) { |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1631 | int64_t elapsed_days = 0; |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1632 | if (!base::StringToInt64(parser_data->daystart_elapsed_days, &elapsed_days)) |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1633 | return false; |
| 1634 | |
| 1635 | if (elapsed_days < 0) |
| 1636 | return false; |
| 1637 | |
| 1638 | output_object->install_date_days = elapsed_days; |
| 1639 | return true; |
| 1640 | } |
| 1641 | |
| 1642 | // static |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1643 | bool OmahaRequestAction::HasInstallDate(SystemState* system_state) { |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1644 | PrefsInterface* prefs = system_state->prefs(); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1645 | if (prefs == nullptr) |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1646 | return false; |
| 1647 | |
| 1648 | return prefs->Exists(kPrefsInstallDateDays); |
| 1649 | } |
| 1650 | |
| 1651 | // static |
| 1652 | bool OmahaRequestAction::PersistInstallDate( |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1653 | SystemState* system_state, |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1654 | int install_date_days, |
| 1655 | InstallDateProvisioningSource source) { |
| 1656 | TEST_AND_RETURN_FALSE(install_date_days >= 0); |
| 1657 | |
| 1658 | PrefsInterface* prefs = system_state->prefs(); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1659 | if (prefs == nullptr) |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1660 | return false; |
| 1661 | |
| 1662 | if (!prefs->SetInt64(kPrefsInstallDateDays, install_date_days)) |
| 1663 | return false; |
| 1664 | |
Tianjie Xu | 282aa1f | 2017-09-05 13:42:45 -0700 | [diff] [blame] | 1665 | system_state->metrics_reporter()->ReportInstallDateProvisioningSource( |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1666 | static_cast<int>(source), // Sample. |
| 1667 | kProvisionedMax); // Maximum. |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1668 | return true; |
| 1669 | } |
| 1670 | |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1671 | bool OmahaRequestAction::PersistCohortData(const string& prefs_key, |
| 1672 | const string& new_value) { |
Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 1673 | if (new_value.empty() && system_state_->prefs()->Exists(prefs_key)) { |
| 1674 | LOG(INFO) << "Removing stored " << prefs_key << " value."; |
| 1675 | return system_state_->prefs()->Delete(prefs_key); |
| 1676 | } else if (!new_value.empty()) { |
| 1677 | LOG(INFO) << "Storing new setting " << prefs_key << " as " << new_value; |
| 1678 | return system_state_->prefs()->SetString(prefs_key, new_value); |
| 1679 | } |
| 1680 | return true; |
| 1681 | } |
| 1682 | |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 1683 | bool OmahaRequestAction::PersistEolStatus(const map<string, string>& attrs) { |
Amin Hassani | 6600a56 | 2018-09-24 15:17:19 -0700 | [diff] [blame] | 1684 | auto eol_attr = attrs.find(kAttrEol); |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 1685 | if (eol_attr != attrs.end()) { |
| 1686 | return system_state_->prefs()->SetString(kPrefsOmahaEolStatus, |
| 1687 | eol_attr->second); |
| 1688 | } else if (system_state_->prefs()->Exists(kPrefsOmahaEolStatus)) { |
| 1689 | return system_state_->prefs()->Delete(kPrefsOmahaEolStatus); |
| 1690 | } |
| 1691 | return true; |
| 1692 | } |
| 1693 | |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1694 | void OmahaRequestAction::ActionCompleted(ErrorCode code) { |
| 1695 | // We only want to report this on "update check". |
| 1696 | if (ping_only_ || event_ != nullptr) |
| 1697 | return; |
| 1698 | |
| 1699 | metrics::CheckResult result = metrics::CheckResult::kUnset; |
| 1700 | metrics::CheckReaction reaction = metrics::CheckReaction::kUnset; |
| 1701 | metrics::DownloadErrorCode download_error_code = |
| 1702 | metrics::DownloadErrorCode::kUnset; |
| 1703 | |
| 1704 | // Regular update attempt. |
| 1705 | switch (code) { |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1706 | case ErrorCode::kSuccess: |
| 1707 | // OK, we parsed the response successfully but that does |
| 1708 | // necessarily mean that an update is available. |
| 1709 | if (HasOutputPipe()) { |
| 1710 | const OmahaResponse& response = GetOutputObject(); |
| 1711 | if (response.update_exists) { |
| 1712 | result = metrics::CheckResult::kUpdateAvailable; |
| 1713 | reaction = metrics::CheckReaction::kUpdating; |
| 1714 | } else { |
| 1715 | result = metrics::CheckResult::kNoUpdateAvailable; |
| 1716 | } |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1717 | } else { |
| 1718 | result = metrics::CheckResult::kNoUpdateAvailable; |
| 1719 | } |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1720 | break; |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1721 | |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1722 | case ErrorCode::kOmahaUpdateIgnoredPerPolicy: |
| 1723 | case ErrorCode::kOmahaUpdateIgnoredOverCellular: |
| 1724 | result = metrics::CheckResult::kUpdateAvailable; |
| 1725 | reaction = metrics::CheckReaction::kIgnored; |
| 1726 | break; |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1727 | |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1728 | case ErrorCode::kOmahaUpdateDeferredPerPolicy: |
| 1729 | result = metrics::CheckResult::kUpdateAvailable; |
| 1730 | reaction = metrics::CheckReaction::kDeferring; |
| 1731 | break; |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1732 | |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1733 | case ErrorCode::kOmahaUpdateDeferredForBackoff: |
| 1734 | result = metrics::CheckResult::kUpdateAvailable; |
| 1735 | reaction = metrics::CheckReaction::kBackingOff; |
| 1736 | break; |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1737 | |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1738 | default: |
| 1739 | // We report two flavors of errors, "Download errors" and "Parsing |
| 1740 | // error". Try to convert to the former and if that doesn't work |
| 1741 | // we know it's the latter. |
| 1742 | metrics::DownloadErrorCode tmp_error = |
| 1743 | metrics_utils::GetDownloadErrorCode(code); |
| 1744 | if (tmp_error != metrics::DownloadErrorCode::kInputMalformed) { |
| 1745 | result = metrics::CheckResult::kDownloadError; |
| 1746 | download_error_code = tmp_error; |
| 1747 | } else { |
| 1748 | result = metrics::CheckResult::kParsingError; |
| 1749 | } |
| 1750 | break; |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1751 | } |
| 1752 | |
Tianjie Xu | 282aa1f | 2017-09-05 13:42:45 -0700 | [diff] [blame] | 1753 | system_state_->metrics_reporter()->ReportUpdateCheckMetrics( |
| 1754 | system_state_, result, reaction, download_error_code); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1755 | } |
| 1756 | |
Toni Barzic | 61544e6 | 2018-10-11 14:37:30 -0700 | [diff] [blame] | 1757 | bool OmahaRequestAction::ShouldIgnoreUpdate(const OmahaResponse& response, |
| 1758 | ErrorCode* error) const { |
Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 1759 | // Note: policy decision to not update to a version we rolled back from. |
| 1760 | string rollback_version = |
| 1761 | system_state_->payload_state()->GetRollbackVersion(); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1762 | if (!rollback_version.empty()) { |
Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 1763 | LOG(INFO) << "Detected previous rollback from version " << rollback_version; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1764 | if (rollback_version == response.version) { |
Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 1765 | LOG(INFO) << "Received version that we rolled back from. Ignoring."; |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 1766 | *error = ErrorCode::kOmahaUpdateIgnoredPerPolicy; |
Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 1767 | return true; |
| 1768 | } |
| 1769 | } |
| 1770 | |
Toni Barzic | 61544e6 | 2018-10-11 14:37:30 -0700 | [diff] [blame] | 1771 | if (system_state_->hardware()->IsOOBEEnabled() && |
| 1772 | !system_state_->hardware()->IsOOBEComplete(nullptr) && |
| 1773 | (response.deadline.empty() || |
| 1774 | system_state_->payload_state()->GetRollbackHappened()) && |
| 1775 | params_->app_version() != "ForcedUpdate") { |
| 1776 | LOG(INFO) << "Ignoring a non-critical Omaha update before OOBE completion."; |
| 1777 | *error = ErrorCode::kNonCriticalUpdateInOOBE; |
| 1778 | return true; |
| 1779 | } |
| 1780 | |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 1781 | if (!IsUpdateAllowedOverCurrentConnection(error, response)) { |
Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 1782 | LOG(INFO) << "Update is not allowed over current connection."; |
| 1783 | return true; |
| 1784 | } |
| 1785 | |
| 1786 | // Note: We could technically delete the UpdateFirstSeenAt state when we |
| 1787 | // return true. If we do, it'll mean a device has to restart the |
| 1788 | // UpdateFirstSeenAt and thus help scattering take effect when the AU is |
| 1789 | // turned on again. On the other hand, it also increases the chance of update |
| 1790 | // starvation if an admin turns AU on/off more frequently. We choose to err on |
| 1791 | // the side of preventing starvation at the cost of not applying scattering in |
| 1792 | // those cases. |
| 1793 | return false; |
| 1794 | } |
| 1795 | |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 1796 | bool OmahaRequestAction::IsUpdateAllowedOverCellularByPrefs( |
| 1797 | const OmahaResponse& response) const { |
| 1798 | PrefsInterface* prefs = system_state_->prefs(); |
| 1799 | |
| 1800 | if (!prefs) { |
| 1801 | LOG(INFO) << "Disabling updates over cellular as the preferences are " |
| 1802 | "not available."; |
| 1803 | return false; |
| 1804 | } |
| 1805 | |
| 1806 | bool is_allowed; |
| 1807 | |
| 1808 | if (prefs->Exists(kPrefsUpdateOverCellularPermission) && |
| 1809 | prefs->GetBoolean(kPrefsUpdateOverCellularPermission, &is_allowed) && |
| 1810 | is_allowed) { |
| 1811 | LOG(INFO) << "Allowing updates over cellular as permission preference is " |
| 1812 | "set to true."; |
| 1813 | return true; |
| 1814 | } |
| 1815 | |
| 1816 | if (!prefs->Exists(kPrefsUpdateOverCellularTargetVersion) || |
| 1817 | !prefs->Exists(kPrefsUpdateOverCellularTargetSize)) { |
| 1818 | LOG(INFO) << "Disabling updates over cellular as permission preference is " |
| 1819 | "set to false or does not exist while target does not exist."; |
| 1820 | return false; |
| 1821 | } |
| 1822 | |
| 1823 | std::string target_version; |
| 1824 | int64_t target_size; |
| 1825 | |
| 1826 | if (!prefs->GetString(kPrefsUpdateOverCellularTargetVersion, |
| 1827 | &target_version) || |
| 1828 | !prefs->GetInt64(kPrefsUpdateOverCellularTargetSize, &target_size)) { |
| 1829 | LOG(INFO) << "Disabling updates over cellular as the target version or " |
| 1830 | "size is not accessible."; |
| 1831 | return false; |
| 1832 | } |
| 1833 | |
| 1834 | uint64_t total_packages_size = 0; |
| 1835 | for (const auto& package : response.packages) { |
| 1836 | total_packages_size += package.size; |
| 1837 | } |
| 1838 | if (target_version == response.version && |
| 1839 | static_cast<uint64_t>(target_size) == total_packages_size) { |
| 1840 | LOG(INFO) << "Allowing updates over cellular as the target matches the" |
| 1841 | "omaha response."; |
| 1842 | return true; |
| 1843 | } else { |
| 1844 | LOG(INFO) << "Disabling updates over cellular as the target does not" |
| 1845 | "match the omaha response."; |
| 1846 | return false; |
| 1847 | } |
| 1848 | } |
| 1849 | |
| 1850 | bool OmahaRequestAction::IsUpdateAllowedOverCurrentConnection( |
| 1851 | ErrorCode* error, const OmahaResponse& response) const { |
Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 1852 | ConnectionType type; |
| 1853 | ConnectionTethering tethering; |
Alex Deymo | f6ee016 | 2015-07-31 12:35:22 -0700 | [diff] [blame] | 1854 | ConnectionManagerInterface* connection_manager = |
| 1855 | system_state_->connection_manager(); |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 1856 | if (!connection_manager->GetConnectionProperties(&type, &tethering)) { |
Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 1857 | LOG(INFO) << "We could not determine our connection type. " |
| 1858 | << "Defaulting to allow updates."; |
| 1859 | return true; |
| 1860 | } |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 1861 | |
Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 1862 | bool is_allowed = connection_manager->IsUpdateAllowedOver(type, tethering); |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 1863 | bool is_device_policy_set = |
| 1864 | connection_manager->IsAllowedConnectionTypesForUpdateSet(); |
| 1865 | // Treats tethered connection as if it is cellular connection. |
| 1866 | bool is_over_cellular = type == ConnectionType::kCellular || |
| 1867 | tethering == ConnectionTethering::kConfirmed; |
| 1868 | |
| 1869 | if (!is_over_cellular) { |
| 1870 | // There's no need to further check user preferences as we are not over |
| 1871 | // cellular connection. |
| 1872 | if (!is_allowed) |
| 1873 | *error = ErrorCode::kOmahaUpdateIgnoredPerPolicy; |
| 1874 | } else if (is_device_policy_set) { |
| 1875 | // There's no need to further check user preferences as the device policy |
| 1876 | // is set regarding updates over cellular. |
| 1877 | if (!is_allowed) |
| 1878 | *error = ErrorCode::kOmahaUpdateIgnoredPerPolicy; |
| 1879 | } else { |
| 1880 | // Deivce policy is not set, so user preferences overwrite whether to |
| 1881 | // allow updates over cellular. |
| 1882 | is_allowed = IsUpdateAllowedOverCellularByPrefs(response); |
| 1883 | if (!is_allowed) |
| 1884 | *error = ErrorCode::kOmahaUpdateIgnoredOverCellular; |
| 1885 | } |
| 1886 | |
Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 1887 | LOG(INFO) << "We are connected via " |
Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 1888 | << connection_utils::StringForConnectionType(type) |
Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 1889 | << ", Updates allowed: " << (is_allowed ? "Yes" : "No"); |
| 1890 | return is_allowed; |
| 1891 | } |
| 1892 | |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 1893 | bool OmahaRequestAction::IsRollbackEnabled() const { |
| 1894 | if (policy_provider_->IsConsumerDevice()) { |
| 1895 | LOG(INFO) << "Rollback is not enabled for consumer devices."; |
| 1896 | return false; |
| 1897 | } |
| 1898 | |
| 1899 | if (!policy_provider_->device_policy_is_loaded()) { |
| 1900 | LOG(INFO) << "No device policy is loaded. Assuming rollback enabled."; |
| 1901 | return true; |
| 1902 | } |
| 1903 | |
| 1904 | int allowed_milestones; |
| 1905 | if (!policy_provider_->GetDevicePolicy().GetRollbackAllowedMilestones( |
| 1906 | &allowed_milestones)) { |
| 1907 | LOG(INFO) << "RollbackAllowedMilestones policy can't be read. " |
| 1908 | "Defaulting to rollback enabled."; |
| 1909 | return true; |
| 1910 | } |
| 1911 | |
| 1912 | LOG(INFO) << "Rollback allows " << allowed_milestones << " milestones."; |
| 1913 | return allowed_milestones > 0; |
| 1914 | } |
| 1915 | |
| 1916 | void OmahaRequestAction::SetMaxKernelKeyVersionForRollback() const { |
Marton Hunyady | ffbfdfb | 2018-05-30 13:03:29 +0200 | [diff] [blame] | 1917 | int max_kernel_rollforward; |
| 1918 | int min_kernel_version = system_state_->hardware()->GetMinKernelKeyVersion(); |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 1919 | if (IsRollbackEnabled()) { |
| 1920 | // If rollback is enabled, set the max kernel key version to the current |
| 1921 | // kernel key version. This has the effect of freezing kernel key roll |
| 1922 | // forwards. |
| 1923 | // |
| 1924 | // TODO(zentaro): This behavior is temporary, and ensures that no kernel |
| 1925 | // key roll forward happens until the server side components of rollback |
| 1926 | // are implemented. Future changes will allow the Omaha server to return |
| 1927 | // the kernel key version from max_rollback_versions in the past. At that |
| 1928 | // point the max kernel key version will be set to that value, creating a |
| 1929 | // sliding window of versions that can be rolled back to. |
Zentaro Kavanagh | 5d95615 | 2018-05-15 09:40:33 -0700 | [diff] [blame] | 1930 | LOG(INFO) << "Rollback is enabled. Setting kernel_max_rollforward to " |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 1931 | << min_kernel_version; |
Marton Hunyady | ffbfdfb | 2018-05-30 13:03:29 +0200 | [diff] [blame] | 1932 | max_kernel_rollforward = min_kernel_version; |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 1933 | } else { |
| 1934 | // For devices that are not rollback enabled (ie. consumer devices), the |
| 1935 | // max kernel key version is set to 0xfffffffe, which is logically |
| 1936 | // infinity. This maintains the previous behavior that that kernel key |
| 1937 | // versions roll forward each time they are incremented. |
Zentaro Kavanagh | 5d95615 | 2018-05-15 09:40:33 -0700 | [diff] [blame] | 1938 | LOG(INFO) << "Rollback is disabled. Setting kernel_max_rollforward to " |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 1939 | << kRollforwardInfinity; |
Marton Hunyady | ffbfdfb | 2018-05-30 13:03:29 +0200 | [diff] [blame] | 1940 | max_kernel_rollforward = kRollforwardInfinity; |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 1941 | } |
| 1942 | |
Marton Hunyady | ffbfdfb | 2018-05-30 13:03:29 +0200 | [diff] [blame] | 1943 | bool max_rollforward_set = |
| 1944 | system_state_->hardware()->SetMaxKernelKeyRollforward( |
| 1945 | max_kernel_rollforward); |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 1946 | if (!max_rollforward_set) { |
Zentaro Kavanagh | 5d95615 | 2018-05-15 09:40:33 -0700 | [diff] [blame] | 1947 | LOG(ERROR) << "Failed to set kernel_max_rollforward"; |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 1948 | } |
Marton Hunyady | ffbfdfb | 2018-05-30 13:03:29 +0200 | [diff] [blame] | 1949 | // Report metrics |
| 1950 | system_state_->metrics_reporter()->ReportKeyVersionMetrics( |
| 1951 | min_kernel_version, max_kernel_rollforward, max_rollforward_set); |
Zentaro Kavanagh | 1f899d5 | 2018-02-27 15:02:47 -0800 | [diff] [blame] | 1952 | } |
| 1953 | |
May Lippert | 60aa3ca | 2018-08-15 16:55:29 -0700 | [diff] [blame] | 1954 | base::Time OmahaRequestAction::LoadOrPersistUpdateFirstSeenAtPref() const { |
| 1955 | Time update_first_seen_at; |
| 1956 | int64_t update_first_seen_at_int; |
| 1957 | if (system_state_->prefs()->Exists(kPrefsUpdateFirstSeenAt)) { |
| 1958 | if (system_state_->prefs()->GetInt64(kPrefsUpdateFirstSeenAt, |
| 1959 | &update_first_seen_at_int)) { |
| 1960 | // Note: This timestamp could be that of ANY update we saw in the past |
| 1961 | // (not necessarily this particular update we're considering to apply) |
| 1962 | // but never got to apply because of some reason (e.g. stop AU policy, |
| 1963 | // updates being pulled out from Omaha, changes in target version prefix, |
| 1964 | // new update being rolled out, etc.). But for the purposes of scattering |
| 1965 | // it doesn't matter which update the timestamp corresponds to. i.e. |
| 1966 | // the clock starts ticking the first time we see an update and we're |
| 1967 | // ready to apply when the random wait period is satisfied relative to |
| 1968 | // that first seen timestamp. |
| 1969 | update_first_seen_at = Time::FromInternalValue(update_first_seen_at_int); |
| 1970 | LOG(INFO) << "Using persisted value of UpdateFirstSeenAt: " |
| 1971 | << utils::ToString(update_first_seen_at); |
| 1972 | } else { |
| 1973 | // This seems like an unexpected error where the persisted value exists |
| 1974 | // but it's not readable for some reason. |
| 1975 | LOG(INFO) << "UpdateFirstSeenAt value cannot be read"; |
| 1976 | return base::Time(); |
| 1977 | } |
| 1978 | } else { |
| 1979 | update_first_seen_at = system_state_->clock()->GetWallclockTime(); |
| 1980 | update_first_seen_at_int = update_first_seen_at.ToInternalValue(); |
| 1981 | if (system_state_->prefs()->SetInt64(kPrefsUpdateFirstSeenAt, |
| 1982 | update_first_seen_at_int)) { |
| 1983 | LOG(INFO) << "Persisted the new value for UpdateFirstSeenAt: " |
| 1984 | << utils::ToString(update_first_seen_at); |
| 1985 | } else { |
| 1986 | // This seems like an unexpected error where the value cannot be |
| 1987 | // persisted for some reason. |
| 1988 | LOG(INFO) << "UpdateFirstSeenAt value " |
| 1989 | << utils::ToString(update_first_seen_at) |
| 1990 | << " cannot be persisted"; |
| 1991 | return base::Time(); |
| 1992 | } |
| 1993 | } |
| 1994 | return update_first_seen_at; |
| 1995 | } |
| 1996 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1997 | } // namespace chromeos_update_engine |