blob: 30b3d227c59880f328d5ef00efa2a97329985038 [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
Gilad Arnoldcf175a02014-07-10 16:48:47 -070017#ifndef UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H_
18#define UPDATE_ENGINE_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
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070031#include <brillo/secure_blob.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000032#include <curl/curl.h>
33
Alex Deymo39910dc2015-11-09 17:04:30 -080034#include "update_engine/common/action.h"
35#include "update_engine/common/http_fetcher.h"
Amin Hassani7fca2862019-03-28 16:09:22 -070036#include "update_engine/omaha_request_builder_xml.h"
Jay Srinivasan08262882012-12-28 19:29:43 -080037#include "update_engine/omaha_response.h"
Alex Deymoc1c17b42015-11-23 03:53:15 -030038#include "update_engine/system_state.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000039
Darin Petkov6a5b3222010-07-13 14:55:28 -070040// The Omaha Request action makes a request to Omaha and can output
41// the response on the output ActionPipe.
rspangler@google.com49fdf182009-10-10 00:57:34 +000042
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080043namespace policy {
44class PolicyProvider;
45}
46
rspangler@google.com49fdf182009-10-10 00:57:34 +000047namespace chromeos_update_engine {
48
rspangler@google.com49fdf182009-10-10 00:57:34 +000049class NoneType;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070050class OmahaRequestAction;
Yunlian Jianga178e5e2013-04-05 14:41:56 -070051class OmahaRequestParams;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070052class PrefsInterface;
rspangler@google.com49fdf182009-10-10 00:57:34 +000053
David Zeuthene8ed8632014-07-24 13:38:10 -040054// This struct is declared in the .cc file.
55struct OmahaParserData;
56
Amin Hassani7cc8bb02019-01-14 16:29:47 -080057template <>
Darin Petkov6a5b3222010-07-13 14:55:28 -070058class ActionTraits<OmahaRequestAction> {
rspangler@google.com49fdf182009-10-10 00:57:34 +000059 public:
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070060 // Takes parameters on the input pipe.
Darin Petkova4a8a8c2010-07-15 22:21:12 -070061 typedef NoneType InputObjectType;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070062 // On UpdateCheck success, puts the Omaha response on output. Event
63 // requests do not have an output pipe.
Darin Petkov6a5b3222010-07-13 14:55:28 -070064 typedef OmahaResponse OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +000065};
66
Darin Petkov6a5b3222010-07-13 14:55:28 -070067class OmahaRequestAction : public Action<OmahaRequestAction>,
68 public HttpFetcherDelegate {
rspangler@google.com49fdf182009-10-10 00:57:34 +000069 public:
Darin Petkov1cbd78f2010-07-29 12:38:34 -070070 static const int kPingTimeJump = -2;
Jae Hoon Kima3210e62020-05-07 11:32:44 -070071 // We choose this value of 3 as a heuristic for a work day in trying
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080072 // each URL, assuming we check roughly every 45 mins. This is a good time to
Jae Hoon Kima3210e62020-05-07 11:32:44 -070073 // wait so we don't give up the preferred URLs, but allow using the URL that
74 // appears earlier in list for every payload before resorting to the fallback
75 // URLs in the candiate URL list.
76 static const int kDefaultMaxFailureCountPerUrl = 3;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070077
Adolfo Victoria497044c2018-07-18 07:51:42 -070078 // If staging is enabled, set the maximum wait time to 28 days, since that is
79 // the predetermined wait time for staging.
80 static const int kMaxWaitTimeStagingInDays = 28;
81
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070082 // These are the possible outcome upon checking whether we satisfied
83 // the wall-clock-based-wait.
84 enum WallClockWaitResult {
85 kWallClockWaitNotSatisfied,
86 kWallClockWaitDoneButUpdateCheckWaitRequired,
87 kWallClockWaitDoneAndUpdateCheckWaitNotRequired,
88 };
89
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070090 // The ctor takes in all the parameters that will be used for making
91 // the request to Omaha. For some of them we have constants that
92 // should be used.
93 //
rspangler@google.com49fdf182009-10-10 00:57:34 +000094 // Takes ownership of the passed in HttpFetcher. Useful for testing.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070095 //
Alex Vakulenko88b591f2014-08-28 16:48:57 -070096 // Takes ownership of the passed in OmahaEvent. If |event| is null,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070097 // this is an UpdateCheck request, otherwise it's an Event request.
98 // Event requests always succeed.
99 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000100 // A good calling pattern is:
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700101 // OmahaRequestAction(..., new OmahaEvent(...), new WhateverHttpFetcher);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700102 // or
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700103 // OmahaRequestAction(..., nullptr, new WhateverHttpFetcher);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800104 OmahaRequestAction(SystemState* system_state,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700105 OmahaEvent* event,
Alex Deymoc1c17b42015-11-23 03:53:15 -0300106 std::unique_ptr<HttpFetcher> http_fetcher,
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700107 bool ping_only,
108 const std::string& session_id);
Alex Deymo610277e2014-11-11 21:18:11 -0800109 ~OmahaRequestAction() override;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700110 typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType;
111 typedef ActionTraits<OmahaRequestAction>::OutputObjectType OutputObjectType;
Alex Deymo610277e2014-11-11 21:18:11 -0800112 void PerformAction() override;
113 void TerminateProcessing() override;
114 void ActionCompleted(ErrorCode code) override;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000115
Darin Petkov1023a602010-08-30 13:47:51 -0700116 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
117
rspangler@google.com49fdf182009-10-10 00:57:34 +0000118 // Debugging/logging
Darin Petkov6a5b3222010-07-13 14:55:28 -0700119 static std::string StaticType() { return "OmahaRequestAction"; }
Alex Deymo610277e2014-11-11 21:18:11 -0800120 std::string Type() const override { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000121
122 // Delegate methods (see http_fetcher.h)
Amin Hassani0cd9d772018-07-31 23:55:43 -0700123 bool ReceivedBytes(HttpFetcher* fetcher,
124 const void* bytes,
125 size_t length) override;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000126
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800127 void TransferComplete(HttpFetcher* fetcher, bool successful) override;
Alex Deymo610277e2014-11-11 21:18:11 -0800128
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700129 // Returns true if this is an Event request, false if it's an UpdateCheck.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700130 bool IsEvent() const { return event_.get() != nullptr; }
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700131
rspangler@google.com49fdf182009-10-10 00:57:34 +0000132 private:
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800133 friend class OmahaRequestActionTest;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700134 friend class OmahaRequestActionTestProcessorDelegate;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700135 FRIEND_TEST(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE);
136 FRIEND_TEST(OmahaRequestActionTest,
137 GetInstallDateWhenOOBECompletedWithInvalidDate);
138 FRIEND_TEST(OmahaRequestActionTest,
139 GetInstallDateWhenOOBECompletedWithValidDate);
140 FRIEND_TEST(OmahaRequestActionTest,
141 GetInstallDateWhenOOBECompletedDateChanges);
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700142 friend class UpdateAttempterTest;
143 FRIEND_TEST(UpdateAttempterTest, SessionIdTestEnforceEmptyStrPingOmaha);
144 FRIEND_TEST(UpdateAttempterTest, SessionIdTestConsistencyInUpdateFlow);
David Zeuthen639aa362014-02-03 16:23:44 -0800145
146 // Enumeration used in PersistInstallDate().
147 enum InstallDateProvisioningSource {
148 kProvisionedFromOmahaResponse,
149 kProvisionedFromOOBEMarker,
150
151 // kProvisionedMax is the count of the number of enums above. Add
152 // any new enums above this line only.
153 kProvisionedMax
154 };
155
156 // Gets the install date, expressed as the number of PST8PDT
157 // calendar weeks since January 1st 2007, times seven. Returns -1 if
158 // unknown. See http://crbug.com/336838 for details about this value.
159 static int GetInstallDate(SystemState* system_state);
160
161 // Parses the Omaha Response in |doc| and sets the
162 // |install_date_days| field of |output_object| to the value of the
163 // elapsed_days attribute of the daystart element. Returns True if
164 // the value was set, False if it wasn't found.
David Zeuthene8ed8632014-07-24 13:38:10 -0400165 static bool ParseInstallDate(OmahaParserData* parser_data,
David Zeuthen639aa362014-02-03 16:23:44 -0800166 OmahaResponse* output_object);
167
168 // Returns True if the kPrefsInstallDateDays state variable is set,
169 // False otherwise.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800170 static bool HasInstallDate(SystemState* system_state);
David Zeuthen639aa362014-02-03 16:23:44 -0800171
172 // Writes |install_date_days| into the kPrefsInstallDateDays state
173 // variable and emits an UMA stat for the |source| used. Returns
174 // True if the value was written, False if an error occurred.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800175 static bool PersistInstallDate(SystemState* system_state,
David Zeuthen639aa362014-02-03 16:23:44 -0800176 int install_date_days,
177 InstallDateProvisioningSource source);
178
Alex Deymo8e18f932015-03-27 16:16:59 -0700179 // Persist the new cohort* value received in the XML file in the |prefs_key|
180 // preference file. If the |new_value| is empty, the currently stored value
181 // will be deleted. Don't call this function with an empty |new_value| if the
182 // value was not set in the XML, since that would delete the stored value.
183 bool PersistCohortData(const std::string& prefs_key,
184 const std::string& new_value);
185
Amin Hassani2cbb0692019-10-30 13:36:17 -0700186 // Parses and persists the end-of-life date flag sent back in the updatecheck
187 // tag attributes. The flags will be validated and stored in the Prefs.
188 bool PersistEolInfo(const std::map<std::string, std::string>& attrs);
Alex Deymob3fa53b2016-04-18 19:57:58 -0700189
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700190 // If this is an update check request, initializes
191 // |ping_active_days_| and |ping_roll_call_days_| to values that may
192 // be sent as pings to Omaha.
193 void InitPingDays();
194
Darin Petkov84c763c2010-07-29 16:27:58 -0700195 // Based on the persistent preference store values, calculates the
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700196 // number of days since the last ping sent for |key|.
197 int CalculatePingDays(const std::string& key);
198
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700199 // Returns whether we have "active_days" or "roll_call_days" ping values to
200 // send to Omaha and thus we should include them in the response.
201 bool ShouldPing() const;
202
Andrewe045aef2020-01-08 16:29:22 -0800203 // Process Omaha's response to a ping request and store the results in the DLC
204 // metadata directory.
205 void StorePingReply(const OmahaParserData& parser_data) const;
206
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700207 // Returns true if the download of a new update should be deferred.
208 // False if the update can be downloaded.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700209 bool ShouldDeferDownload(OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700210
211 // Returns true if the basic wall-clock-based waiting period has been
212 // satisfied based on the scattering policy setting. False otherwise.
213 // If true, it also indicates whether the additional update-check-count-based
214 // waiting period also needs to be satisfied before the download can begin.
215 WallClockWaitResult IsWallClockBasedWaitingSatisfied(
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700216 OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700217
218 // Returns true if the update-check-count-based waiting period has been
219 // satisfied. False otherwise.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700220 bool IsUpdateCheckCountBasedWaitingSatisfied();
221
222 // Parses the response from Omaha that's available in |doc| using the other
223 // helper methods below and populates the |output_object| with the relevant
224 // values. Returns true if we should continue the parsing. False otherwise,
225 // in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400226 bool ParseResponse(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700227 OmahaResponse* output_object,
228 ScopedActionCompleter* completer);
229
230 // Parses the status property in the given update_check_node and populates
231 // |output_object| if valid. Returns true if we should continue the parsing.
232 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400233 bool ParseStatus(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700234 OmahaResponse* output_object,
235 ScopedActionCompleter* completer);
236
237 // Parses the URL nodes in the given XML document and populates
238 // |output_object| if valid. Returns true if we should continue the parsing.
239 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400240 bool ParseUrls(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700241 OmahaResponse* output_object,
242 ScopedActionCompleter* completer);
243
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700244 // Parses the other parameters in the given XML document and populates
245 // |output_object| if valid. Returns true if we should continue the parsing.
246 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400247 bool ParseParams(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700248 OmahaResponse* output_object,
249 ScopedActionCompleter* completer);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700250
David Zeuthen8f191b22013-08-06 12:27:50 -0700251 // Called by TransferComplete() to complete processing, either
252 // asynchronously after looking up resources via p2p or directly.
253 void CompleteProcessing();
254
255 // Helper to asynchronously look up payload on the LAN.
256 void LookupPayloadViaP2P(const OmahaResponse& response);
257
258 // Callback used by LookupPayloadViaP2P().
259 void OnLookupPayloadViaP2PCompleted(const std::string& url);
260
Chris Sosa77f79e82014-06-02 18:16:24 -0700261 // Returns true if the current update should be ignored.
Toni Barzic61544e62018-10-11 14:37:30 -0700262 bool ShouldIgnoreUpdate(const OmahaResponse& response,
263 ErrorCode* error) const;
Weidong Guo421ff332017-04-17 10:08:38 -0700264
265 // Return true if updates are allowed by user preferences.
266 bool IsUpdateAllowedOverCellularByPrefs(const OmahaResponse& response) const;
Chris Sosa77f79e82014-06-02 18:16:24 -0700267
268 // Returns true if updates are allowed over the current type of connection.
269 // False otherwise.
Weidong Guo421ff332017-04-17 10:08:38 -0700270 bool IsUpdateAllowedOverCurrentConnection(
271 ErrorCode* error, const OmahaResponse& response) const;
Chris Sosa77f79e82014-06-02 18:16:24 -0700272
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800273 // Returns true if rollback is enabled. Always returns false for consumer
274 // devices.
275 bool IsRollbackEnabled() const;
276
277 // Sets the appropriate max kernel key version based on whether rollback is
278 // enabled.
279 void SetMaxKernelKeyVersionForRollback() const;
280
May Lippert60aa3ca2018-08-15 16:55:29 -0700281 // Reads and returns the kPrefsUpdateFirstSeenAt pref if the pref currently
282 // exists. Otherwise saves the current wallclock time to the
283 // kPrefsUpdateFirstSeenAt pref and returns it as a base::Time object.
284 base::Time LoadOrPersistUpdateFirstSeenAtPref() const;
285
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800286 // Global system context.
287 SystemState* system_state_;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700288
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700289 // Contains state that is relevant in the processing of the Omaha request.
290 OmahaRequestParams* params_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000291
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700292 // Pointer to the OmahaEvent info. This is an UpdateCheck request if null.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700293 std::unique_ptr<OmahaEvent> event_;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700294
rspangler@google.com49fdf182009-10-10 00:57:34 +0000295 // pointer to the HttpFetcher that does the http work
Ben Chan02f7c1d2014-10-18 15:18:02 -0700296 std::unique_ptr<HttpFetcher> http_fetcher_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000297
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800298 // Used for fetching information about the device policy.
299 std::unique_ptr<policy::PolicyProvider> policy_provider_;
300
Thieu Le116fda32011-04-19 11:01:54 -0700301 // If true, only include the <ping> element in the request.
302 bool ping_only_;
303
rspangler@google.com49fdf182009-10-10 00:57:34 +0000304 // Stores the response from the omaha server
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700305 brillo::Blob response_buffer_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000306
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700307 // Initialized by InitPingDays to values that may be sent to Omaha
308 // as part of a ping message. Note that only positive values and -1
309 // are sent to Omaha.
310 int ping_active_days_;
311 int ping_roll_call_days_;
312
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700313 std::string session_id_;
314
Darin Petkov6a5b3222010-07-13 14:55:28 -0700315 DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000316};
317
318} // namespace chromeos_update_engine
319
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700320#endif // UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H_