blob: 9576a056146f364e99743d687454e101de06b15c [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
rspangler@google.com49fdf182009-10-10 00:57:34 +000016
Amin Hassaniec7bc112020-10-29 16:47:58 -070017#ifndef UPDATE_ENGINE_CROS_OMAHA_REQUEST_ACTION_H_
18#define UPDATE_ENGINE_CROS_OMAHA_REQUEST_ACTION_H_
rspangler@google.com49fdf182009-10-10 00:57:34 +000019
Alex Vakulenko44cab302014-07-23 13:12:15 -070020#include <fcntl.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000021#include <sys/stat.h>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070022#include <sys/types.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000023
Alex Deymob3fa53b2016-04-18 19:57:58 -070024#include <map>
Ben Chan02f7c1d2014-10-18 15:18:02 -070025#include <memory>
rspangler@google.com49fdf182009-10-10 00:57:34 +000026#include <string>
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080027#include <vector>
rspangler@google.com49fdf182009-10-10 00:57:34 +000028
Alex Deymoc1c17b42015-11-23 03:53:15 -030029#include <gtest/gtest_prod.h> // for FRIEND_TEST
30
Jae Hoon Kime2cac612020-11-02 18:30:29 -080031#include <base/optional.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070032#include <brillo/secure_blob.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000033#include <curl/curl.h>
34
Alex Deymo39910dc2015-11-09 17:04:30 -080035#include "update_engine/common/action.h"
36#include "update_engine/common/http_fetcher.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070037#include "update_engine/common/system_state.h"
38#include "update_engine/cros/omaha_request_builder_xml.h"
39#include "update_engine/cros/omaha_response.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000040
Darin Petkov6a5b3222010-07-13 14:55:28 -070041// The Omaha Request action makes a request to Omaha and can output
42// the response on the output ActionPipe.
rspangler@google.com49fdf182009-10-10 00:57:34 +000043
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080044namespace policy {
45class PolicyProvider;
46}
47
rspangler@google.com49fdf182009-10-10 00:57:34 +000048namespace chromeos_update_engine {
49
rspangler@google.com49fdf182009-10-10 00:57:34 +000050class NoneType;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070051class OmahaRequestAction;
Yunlian Jianga178e5e2013-04-05 14:41:56 -070052class OmahaRequestParams;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070053class PrefsInterface;
rspangler@google.com49fdf182009-10-10 00:57:34 +000054
David Zeuthene8ed8632014-07-24 13:38:10 -040055// This struct is declared in the .cc file.
56struct OmahaParserData;
57
Amin Hassani7cc8bb02019-01-14 16:29:47 -080058template <>
Darin Petkov6a5b3222010-07-13 14:55:28 -070059class ActionTraits<OmahaRequestAction> {
rspangler@google.com49fdf182009-10-10 00:57:34 +000060 public:
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070061 // Takes parameters on the input pipe.
Darin Petkova4a8a8c2010-07-15 22:21:12 -070062 typedef NoneType InputObjectType;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070063 // On UpdateCheck success, puts the Omaha response on output. Event
64 // requests do not have an output pipe.
Darin Petkov6a5b3222010-07-13 14:55:28 -070065 typedef OmahaResponse OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +000066};
67
Darin Petkov6a5b3222010-07-13 14:55:28 -070068class OmahaRequestAction : public Action<OmahaRequestAction>,
69 public HttpFetcherDelegate {
rspangler@google.com49fdf182009-10-10 00:57:34 +000070 public:
Darin Petkov1cbd78f2010-07-29 12:38:34 -070071 static const int kPingTimeJump = -2;
Jae Hoon Kima3210e62020-05-07 11:32:44 -070072 // We choose this value of 3 as a heuristic for a work day in trying
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080073 // each URL, assuming we check roughly every 45 mins. This is a good time to
Jae Hoon Kima3210e62020-05-07 11:32:44 -070074 // wait so we don't give up the preferred URLs, but allow using the URL that
75 // appears earlier in list for every payload before resorting to the fallback
76 // URLs in the candiate URL list.
77 static const int kDefaultMaxFailureCountPerUrl = 3;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070078
Adolfo Victoria497044c2018-07-18 07:51:42 -070079 // If staging is enabled, set the maximum wait time to 28 days, since that is
80 // the predetermined wait time for staging.
81 static const int kMaxWaitTimeStagingInDays = 28;
82
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070083 // These are the possible outcome upon checking whether we satisfied
84 // the wall-clock-based-wait.
85 enum WallClockWaitResult {
86 kWallClockWaitNotSatisfied,
87 kWallClockWaitDoneButUpdateCheckWaitRequired,
88 kWallClockWaitDoneAndUpdateCheckWaitNotRequired,
89 };
90
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070091 // The ctor takes in all the parameters that will be used for making
92 // the request to Omaha. For some of them we have constants that
93 // should be used.
94 //
rspangler@google.com49fdf182009-10-10 00:57:34 +000095 // Takes ownership of the passed in HttpFetcher. Useful for testing.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070096 //
Alex Vakulenko88b591f2014-08-28 16:48:57 -070097 // Takes ownership of the passed in OmahaEvent. If |event| is null,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070098 // this is an UpdateCheck request, otherwise it's an Event request.
99 // Event requests always succeed.
100 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000101 // A good calling pattern is:
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700102 // OmahaRequestAction(..., new OmahaEvent(...), new WhateverHttpFetcher);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700103 // or
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700104 // OmahaRequestAction(..., nullptr, new WhateverHttpFetcher);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800105 OmahaRequestAction(SystemState* system_state,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700106 OmahaEvent* event,
Alex Deymoc1c17b42015-11-23 03:53:15 -0300107 std::unique_ptr<HttpFetcher> http_fetcher,
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700108 bool ping_only,
109 const std::string& session_id);
Alex Deymo610277e2014-11-11 21:18:11 -0800110 ~OmahaRequestAction() override;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700111 typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType;
112 typedef ActionTraits<OmahaRequestAction>::OutputObjectType OutputObjectType;
Alex Deymo610277e2014-11-11 21:18:11 -0800113 void PerformAction() override;
114 void TerminateProcessing() override;
115 void ActionCompleted(ErrorCode code) override;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000116
Darin Petkov1023a602010-08-30 13:47:51 -0700117 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
118
rspangler@google.com49fdf182009-10-10 00:57:34 +0000119 // Debugging/logging
Darin Petkov6a5b3222010-07-13 14:55:28 -0700120 static std::string StaticType() { return "OmahaRequestAction"; }
Alex Deymo610277e2014-11-11 21:18:11 -0800121 std::string Type() const override { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000122
123 // Delegate methods (see http_fetcher.h)
Amin Hassani0cd9d772018-07-31 23:55:43 -0700124 bool ReceivedBytes(HttpFetcher* fetcher,
125 const void* bytes,
126 size_t length) override;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000127
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800128 void TransferComplete(HttpFetcher* fetcher, bool successful) override;
Alex Deymo610277e2014-11-11 21:18:11 -0800129
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700130 // Returns true if this is an Event request, false if it's an UpdateCheck.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700131 bool IsEvent() const { return event_.get() != nullptr; }
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700132
rspangler@google.com49fdf182009-10-10 00:57:34 +0000133 private:
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800134 friend class OmahaRequestActionTest;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700135 friend class OmahaRequestActionTestProcessorDelegate;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700136 FRIEND_TEST(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE);
137 FRIEND_TEST(OmahaRequestActionTest,
138 GetInstallDateWhenOOBECompletedWithInvalidDate);
139 FRIEND_TEST(OmahaRequestActionTest,
140 GetInstallDateWhenOOBECompletedWithValidDate);
141 FRIEND_TEST(OmahaRequestActionTest,
142 GetInstallDateWhenOOBECompletedDateChanges);
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700143 friend class UpdateAttempterTest;
144 FRIEND_TEST(UpdateAttempterTest, SessionIdTestEnforceEmptyStrPingOmaha);
145 FRIEND_TEST(UpdateAttempterTest, SessionIdTestConsistencyInUpdateFlow);
David Zeuthen639aa362014-02-03 16:23:44 -0800146
147 // Enumeration used in PersistInstallDate().
148 enum InstallDateProvisioningSource {
149 kProvisionedFromOmahaResponse,
150 kProvisionedFromOOBEMarker,
151
152 // kProvisionedMax is the count of the number of enums above. Add
153 // any new enums above this line only.
154 kProvisionedMax
155 };
156
157 // Gets the install date, expressed as the number of PST8PDT
158 // calendar weeks since January 1st 2007, times seven. Returns -1 if
159 // unknown. See http://crbug.com/336838 for details about this value.
160 static int GetInstallDate(SystemState* system_state);
161
162 // Parses the Omaha Response in |doc| and sets the
163 // |install_date_days| field of |output_object| to the value of the
164 // elapsed_days attribute of the daystart element. Returns True if
165 // the value was set, False if it wasn't found.
David Zeuthene8ed8632014-07-24 13:38:10 -0400166 static bool ParseInstallDate(OmahaParserData* parser_data,
David Zeuthen639aa362014-02-03 16:23:44 -0800167 OmahaResponse* output_object);
168
169 // Returns True if the kPrefsInstallDateDays state variable is set,
170 // False otherwise.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800171 static bool HasInstallDate(SystemState* system_state);
David Zeuthen639aa362014-02-03 16:23:44 -0800172
173 // Writes |install_date_days| into the kPrefsInstallDateDays state
174 // variable and emits an UMA stat for the |source| used. Returns
175 // True if the value was written, False if an error occurred.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800176 static bool PersistInstallDate(SystemState* system_state,
David Zeuthen639aa362014-02-03 16:23:44 -0800177 int install_date_days,
178 InstallDateProvisioningSource source);
179
Jae Hoon Kime2cac612020-11-02 18:30:29 -0800180 // Persist the new cohort value received in the XML file in the |prefs_key|
181 // preference file. If the |new_value| is empty, do nothing. If the
182 // |new_value| stores and empty value, the currently stored value will be
183 // deleted. Don't call this function with an empty |new_value| if the value
184 // was not set in the XML, since that would delete the stored value.
185 void PersistCohortData(const std::string& prefs_key,
186 const base::Optional<std::string>& new_value);
187
188 // Parses and persists the cohorts sent back in the updatecheck tag
189 // attributes.
190 void PersistCohorts(const OmahaParserData& parser_data);
Alex Deymo8e18f932015-03-27 16:16:59 -0700191
Amin Hassani2cbb0692019-10-30 13:36:17 -0700192 // Parses and persists the end-of-life date flag sent back in the updatecheck
193 // tag attributes. The flags will be validated and stored in the Prefs.
194 bool PersistEolInfo(const std::map<std::string, std::string>& attrs);
Alex Deymob3fa53b2016-04-18 19:57:58 -0700195
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700196 // If this is an update check request, initializes
197 // |ping_active_days_| and |ping_roll_call_days_| to values that may
198 // be sent as pings to Omaha.
199 void InitPingDays();
200
Darin Petkov84c763c2010-07-29 16:27:58 -0700201 // Based on the persistent preference store values, calculates the
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700202 // number of days since the last ping sent for |key|.
203 int CalculatePingDays(const std::string& key);
204
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700205 // Returns whether we have "active_days" or "roll_call_days" ping values to
206 // send to Omaha and thus we should include them in the response.
207 bool ShouldPing() const;
208
Andrewe045aef2020-01-08 16:29:22 -0800209 // Process Omaha's response to a ping request and store the results in the DLC
210 // metadata directory.
211 void StorePingReply(const OmahaParserData& parser_data) const;
212
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700213 // Returns true if the download of a new update should be deferred.
214 // False if the update can be downloaded.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700215 bool ShouldDeferDownload(OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700216
217 // Returns true if the basic wall-clock-based waiting period has been
218 // satisfied based on the scattering policy setting. False otherwise.
219 // If true, it also indicates whether the additional update-check-count-based
220 // waiting period also needs to be satisfied before the download can begin.
221 WallClockWaitResult IsWallClockBasedWaitingSatisfied(
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700222 OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700223
224 // Returns true if the update-check-count-based waiting period has been
225 // satisfied. False otherwise.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700226 bool IsUpdateCheckCountBasedWaitingSatisfied();
227
228 // Parses the response from Omaha that's available in |doc| using the other
229 // helper methods below and populates the |output_object| with the relevant
230 // values. Returns true if we should continue the parsing. False otherwise,
231 // in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400232 bool ParseResponse(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700233 OmahaResponse* output_object,
234 ScopedActionCompleter* completer);
235
236 // Parses the status property in the given update_check_node and populates
237 // |output_object| if valid. Returns true if we should continue the parsing.
238 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400239 bool ParseStatus(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700240 OmahaResponse* output_object,
241 ScopedActionCompleter* completer);
242
243 // Parses the URL nodes in the given XML document and populates
244 // |output_object| if valid. Returns true if we should continue the parsing.
245 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400246 bool ParseUrls(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700247 OmahaResponse* output_object,
248 ScopedActionCompleter* completer);
249
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700250 // Parses the other parameters in the given XML document and populates
251 // |output_object| if valid. Returns true if we should continue the parsing.
252 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400253 bool ParseParams(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700254 OmahaResponse* output_object,
255 ScopedActionCompleter* completer);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700256
David Zeuthen8f191b22013-08-06 12:27:50 -0700257 // Called by TransferComplete() to complete processing, either
258 // asynchronously after looking up resources via p2p or directly.
259 void CompleteProcessing();
260
261 // Helper to asynchronously look up payload on the LAN.
262 void LookupPayloadViaP2P(const OmahaResponse& response);
263
264 // Callback used by LookupPayloadViaP2P().
265 void OnLookupPayloadViaP2PCompleted(const std::string& url);
266
Chris Sosa77f79e82014-06-02 18:16:24 -0700267 // Returns true if the current update should be ignored.
Toni Barzic61544e62018-10-11 14:37:30 -0700268 bool ShouldIgnoreUpdate(const OmahaResponse& response,
269 ErrorCode* error) const;
Weidong Guo421ff332017-04-17 10:08:38 -0700270
271 // Return true if updates are allowed by user preferences.
272 bool IsUpdateAllowedOverCellularByPrefs(const OmahaResponse& response) const;
Chris Sosa77f79e82014-06-02 18:16:24 -0700273
274 // Returns true if updates are allowed over the current type of connection.
275 // False otherwise.
Weidong Guo421ff332017-04-17 10:08:38 -0700276 bool IsUpdateAllowedOverCurrentConnection(
277 ErrorCode* error, const OmahaResponse& response) const;
Chris Sosa77f79e82014-06-02 18:16:24 -0700278
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800279 // Returns true if rollback is enabled. Always returns false for consumer
280 // devices.
281 bool IsRollbackEnabled() const;
282
283 // Sets the appropriate max kernel key version based on whether rollback is
284 // enabled.
285 void SetMaxKernelKeyVersionForRollback() const;
286
May Lippert60aa3ca2018-08-15 16:55:29 -0700287 // Reads and returns the kPrefsUpdateFirstSeenAt pref if the pref currently
288 // exists. Otherwise saves the current wallclock time to the
289 // kPrefsUpdateFirstSeenAt pref and returns it as a base::Time object.
290 base::Time LoadOrPersistUpdateFirstSeenAtPref() const;
291
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800292 // Global system context.
293 SystemState* system_state_;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700294
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700295 // Contains state that is relevant in the processing of the Omaha request.
296 OmahaRequestParams* params_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000297
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700298 // Pointer to the OmahaEvent info. This is an UpdateCheck request if null.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700299 std::unique_ptr<OmahaEvent> event_;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700300
rspangler@google.com49fdf182009-10-10 00:57:34 +0000301 // pointer to the HttpFetcher that does the http work
Ben Chan02f7c1d2014-10-18 15:18:02 -0700302 std::unique_ptr<HttpFetcher> http_fetcher_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000303
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800304 // Used for fetching information about the device policy.
305 std::unique_ptr<policy::PolicyProvider> policy_provider_;
306
Thieu Le116fda32011-04-19 11:01:54 -0700307 // If true, only include the <ping> element in the request.
308 bool ping_only_;
309
rspangler@google.com49fdf182009-10-10 00:57:34 +0000310 // Stores the response from the omaha server
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700311 brillo::Blob response_buffer_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000312
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700313 // Initialized by InitPingDays to values that may be sent to Omaha
314 // as part of a ping message. Note that only positive values and -1
315 // are sent to Omaha.
316 int ping_active_days_;
317 int ping_roll_call_days_;
318
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700319 std::string session_id_;
320
Darin Petkov6a5b3222010-07-13 14:55:28 -0700321 DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000322};
323
324} // namespace chromeos_update_engine
325
Amin Hassaniec7bc112020-10-29 16:47:58 -0700326#endif // UPDATE_ENGINE_CROS_OMAHA_REQUEST_ACTION_H_