blob: c3c8233a11eea0fafb7840b7367865510206700f [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"
Jay Srinivasan08262882012-12-28 19:29:43 -080036#include "update_engine/omaha_response.h"
Alex Deymoc1c17b42015-11-23 03:53:15 -030037#include "update_engine/system_state.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000038
Darin Petkov6a5b3222010-07-13 14:55:28 -070039// The Omaha Request action makes a request to Omaha and can output
40// the response on the output ActionPipe.
rspangler@google.com49fdf182009-10-10 00:57:34 +000041
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080042namespace policy {
43class PolicyProvider;
44}
45
rspangler@google.com49fdf182009-10-10 00:57:34 +000046namespace chromeos_update_engine {
47
Alex Deymob0d74eb2015-03-30 17:59:17 -070048// Encodes XML entities in a given string. Input must be ASCII-7 valid. If
49// the input is invalid, the default value is used instead.
50std::string XmlEncodeWithDefault(const std::string& input,
51 const std::string& default_value);
52
53// Escapes text so it can be included as character data and attribute
54// values. The |input| string must be valid ASCII-7, no UTF-8 supported.
55// Returns whether the |input| was valid and escaped properly in |output|.
56bool XmlEncode(const std::string& input, std::string* output);
rspangler@google.com49fdf182009-10-10 00:57:34 +000057
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070058// This struct encapsulates the Omaha event information. For a
59// complete list of defined event types and results, see
60// http://code.google.com/p/omaha/wiki/ServerProtocol#event
61struct OmahaEvent {
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070062 // The Type values correspond to EVENT_TYPE values of Omaha.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070063 enum Type {
64 kTypeUnknown = 0,
65 kTypeDownloadComplete = 1,
66 kTypeInstallComplete = 2,
67 kTypeUpdateComplete = 3,
Darin Petkov8c2980e2010-07-16 15:16:49 -070068 kTypeUpdateDownloadStarted = 13,
69 kTypeUpdateDownloadFinished = 14,
Alex Deymo9fded1e2015-11-05 12:31:19 -080070 // Chromium OS reserved type sent after the first reboot following an update
71 // completed.
72 kTypeRebootedAfterUpdate = 54,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070073 };
74
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070075 // The Result values correspond to EVENT_RESULT values of Omaha.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070076 enum Result {
77 kResultError = 0,
78 kResultSuccess = 1,
Alex Vakulenkod2779df2014-06-16 13:19:00 -070079 kResultUpdateDeferred = 9, // When we ignore/defer updates due to policy.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070080 };
81
82 OmahaEvent()
83 : type(kTypeUnknown),
84 result(kResultError),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070085 error_code(ErrorCode::kError) {}
Darin Petkove17f86b2010-07-20 09:12:01 -070086 explicit OmahaEvent(Type in_type)
87 : type(in_type),
88 result(kResultSuccess),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070089 error_code(ErrorCode::kSuccess) {}
David Zeuthena99981f2013-04-29 13:42:47 -070090 OmahaEvent(Type in_type, Result in_result, ErrorCode in_error_code)
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070091 : type(in_type),
92 result(in_result),
93 error_code(in_error_code) {}
94
95 Type type;
96 Result result;
David Zeuthena99981f2013-04-29 13:42:47 -070097 ErrorCode error_code;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070098};
99
rspangler@google.com49fdf182009-10-10 00:57:34 +0000100class NoneType;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700101class OmahaRequestAction;
Yunlian Jianga178e5e2013-04-05 14:41:56 -0700102class OmahaRequestParams;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700103class PrefsInterface;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000104
David Zeuthene8ed8632014-07-24 13:38:10 -0400105// This struct is declared in the .cc file.
106struct OmahaParserData;
107
rspangler@google.com49fdf182009-10-10 00:57:34 +0000108template<>
Darin Petkov6a5b3222010-07-13 14:55:28 -0700109class ActionTraits<OmahaRequestAction> {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000110 public:
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700111 // Takes parameters on the input pipe.
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700112 typedef NoneType InputObjectType;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700113 // On UpdateCheck success, puts the Omaha response on output. Event
114 // requests do not have an output pipe.
Darin Petkov6a5b3222010-07-13 14:55:28 -0700115 typedef OmahaResponse OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000116};
117
Darin Petkov6a5b3222010-07-13 14:55:28 -0700118class OmahaRequestAction : public Action<OmahaRequestAction>,
119 public HttpFetcherDelegate {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000120 public:
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700121 static const int kNeverPinged = -1;
122 static const int kPingTimeJump = -2;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800123 // We choose this value of 10 as a heuristic for a work day in trying
124 // each URL, assuming we check roughly every 45 mins. This is a good time to
125 // wait - neither too long nor too little - so we don't give up the preferred
126 // URLs that appear earlier in list too quickly before moving on to the
127 // fallback ones.
128 static const int kDefaultMaxFailureCountPerUrl = 10;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700129
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700130 // These are the possible outcome upon checking whether we satisfied
131 // the wall-clock-based-wait.
132 enum WallClockWaitResult {
133 kWallClockWaitNotSatisfied,
134 kWallClockWaitDoneButUpdateCheckWaitRequired,
135 kWallClockWaitDoneAndUpdateCheckWaitNotRequired,
136 };
137
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700138 // The ctor takes in all the parameters that will be used for making
139 // the request to Omaha. For some of them we have constants that
140 // should be used.
141 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000142 // Takes ownership of the passed in HttpFetcher. Useful for testing.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700143 //
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700144 // Takes ownership of the passed in OmahaEvent. If |event| is null,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700145 // this is an UpdateCheck request, otherwise it's an Event request.
146 // Event requests always succeed.
147 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000148 // A good calling pattern is:
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700149 // OmahaRequestAction(..., new OmahaEvent(...), new WhateverHttpFetcher);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700150 // or
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700151 // OmahaRequestAction(..., nullptr, new WhateverHttpFetcher);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800152 OmahaRequestAction(SystemState* system_state,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700153 OmahaEvent* event,
Alex Deymoc1c17b42015-11-23 03:53:15 -0300154 std::unique_ptr<HttpFetcher> http_fetcher,
Thieu Le116fda32011-04-19 11:01:54 -0700155 bool ping_only);
Alex Deymo610277e2014-11-11 21:18:11 -0800156 ~OmahaRequestAction() override;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700157 typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType;
158 typedef ActionTraits<OmahaRequestAction>::OutputObjectType OutputObjectType;
Alex Deymo610277e2014-11-11 21:18:11 -0800159 void PerformAction() override;
160 void TerminateProcessing() override;
161 void ActionCompleted(ErrorCode code) override;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000162
Darin Petkov1023a602010-08-30 13:47:51 -0700163 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
164
rspangler@google.com49fdf182009-10-10 00:57:34 +0000165 // Debugging/logging
Darin Petkov6a5b3222010-07-13 14:55:28 -0700166 static std::string StaticType() { return "OmahaRequestAction"; }
Alex Deymo610277e2014-11-11 21:18:11 -0800167 std::string Type() const override { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000168
169 // Delegate methods (see http_fetcher.h)
Amin Hassani0cd9d772018-07-31 23:55:43 -0700170 bool ReceivedBytes(HttpFetcher* fetcher,
171 const void* bytes,
172 size_t length) override;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000173
Alex Deymo610277e2014-11-11 21:18:11 -0800174 void TransferComplete(HttpFetcher *fetcher, bool successful) override;
175
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700176 // Returns true if this is an Event request, false if it's an UpdateCheck.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700177 bool IsEvent() const { return event_.get() != nullptr; }
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700178
rspangler@google.com49fdf182009-10-10 00:57:34 +0000179 private:
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800180 friend class OmahaRequestActionTest;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700181 friend class OmahaRequestActionTestProcessorDelegate;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700182 FRIEND_TEST(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE);
183 FRIEND_TEST(OmahaRequestActionTest,
184 GetInstallDateWhenOOBECompletedWithInvalidDate);
185 FRIEND_TEST(OmahaRequestActionTest,
186 GetInstallDateWhenOOBECompletedWithValidDate);
187 FRIEND_TEST(OmahaRequestActionTest,
188 GetInstallDateWhenOOBECompletedDateChanges);
David Zeuthen639aa362014-02-03 16:23:44 -0800189
190 // Enumeration used in PersistInstallDate().
191 enum InstallDateProvisioningSource {
192 kProvisionedFromOmahaResponse,
193 kProvisionedFromOOBEMarker,
194
195 // kProvisionedMax is the count of the number of enums above. Add
196 // any new enums above this line only.
197 kProvisionedMax
198 };
199
200 // Gets the install date, expressed as the number of PST8PDT
201 // calendar weeks since January 1st 2007, times seven. Returns -1 if
202 // unknown. See http://crbug.com/336838 for details about this value.
203 static int GetInstallDate(SystemState* system_state);
204
205 // Parses the Omaha Response in |doc| and sets the
206 // |install_date_days| field of |output_object| to the value of the
207 // elapsed_days attribute of the daystart element. Returns True if
208 // the value was set, False if it wasn't found.
David Zeuthene8ed8632014-07-24 13:38:10 -0400209 static bool ParseInstallDate(OmahaParserData* parser_data,
David Zeuthen639aa362014-02-03 16:23:44 -0800210 OmahaResponse* output_object);
211
212 // Returns True if the kPrefsInstallDateDays state variable is set,
213 // False otherwise.
214 static bool HasInstallDate(SystemState *system_state);
215
216 // Writes |install_date_days| into the kPrefsInstallDateDays state
217 // variable and emits an UMA stat for the |source| used. Returns
218 // True if the value was written, False if an error occurred.
219 static bool PersistInstallDate(SystemState *system_state,
220 int install_date_days,
221 InstallDateProvisioningSource source);
222
Alex Deymo8e18f932015-03-27 16:16:59 -0700223 // Persist the new cohort* value received in the XML file in the |prefs_key|
224 // preference file. If the |new_value| is empty, the currently stored value
225 // will be deleted. Don't call this function with an empty |new_value| if the
226 // value was not set in the XML, since that would delete the stored value.
227 bool PersistCohortData(const std::string& prefs_key,
228 const std::string& new_value);
229
Alex Deymob3fa53b2016-04-18 19:57:58 -0700230 // Parse and persist the end-of-life status flag sent back in the updatecheck
231 // tag attributes. The flag will be validated and stored in the Prefs.
232 bool PersistEolStatus(const std::map<std::string, std::string>& attrs);
233
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700234 // If this is an update check request, initializes
235 // |ping_active_days_| and |ping_roll_call_days_| to values that may
236 // be sent as pings to Omaha.
237 void InitPingDays();
238
Darin Petkov84c763c2010-07-29 16:27:58 -0700239 // Based on the persistent preference store values, calculates the
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700240 // number of days since the last ping sent for |key|.
241 int CalculatePingDays(const std::string& key);
242
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700243 // Returns whether we have "active_days" or "roll_call_days" ping values to
244 // send to Omaha and thus we should include them in the response.
245 bool ShouldPing() const;
246
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700247 // Returns true if the download of a new update should be deferred.
248 // False if the update can be downloaded.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700249 bool ShouldDeferDownload(OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700250
251 // Returns true if the basic wall-clock-based waiting period has been
252 // satisfied based on the scattering policy setting. False otherwise.
253 // If true, it also indicates whether the additional update-check-count-based
254 // waiting period also needs to be satisfied before the download can begin.
255 WallClockWaitResult IsWallClockBasedWaitingSatisfied(
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700256 OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700257
258 // Returns true if the update-check-count-based waiting period has been
259 // satisfied. False otherwise.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700260 bool IsUpdateCheckCountBasedWaitingSatisfied();
261
262 // Parses the response from Omaha that's available in |doc| using the other
263 // helper methods below and populates the |output_object| with the relevant
264 // values. Returns true if we should continue the parsing. False otherwise,
265 // in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400266 bool ParseResponse(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700267 OmahaResponse* output_object,
268 ScopedActionCompleter* completer);
269
270 // Parses the status property in the given update_check_node and populates
271 // |output_object| if valid. Returns true if we should continue the parsing.
272 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400273 bool ParseStatus(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700274 OmahaResponse* output_object,
275 ScopedActionCompleter* completer);
276
277 // Parses the URL nodes in the given XML document and populates
278 // |output_object| if valid. Returns true if we should continue the parsing.
279 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400280 bool ParseUrls(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700281 OmahaResponse* output_object,
282 ScopedActionCompleter* completer);
283
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700284 // Parses the other parameters in the given XML document and populates
285 // |output_object| if valid. Returns true if we should continue the parsing.
286 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400287 bool ParseParams(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700288 OmahaResponse* output_object,
289 ScopedActionCompleter* completer);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700290
David Zeuthen8f191b22013-08-06 12:27:50 -0700291 // Called by TransferComplete() to complete processing, either
292 // asynchronously after looking up resources via p2p or directly.
293 void CompleteProcessing();
294
295 // Helper to asynchronously look up payload on the LAN.
296 void LookupPayloadViaP2P(const OmahaResponse& response);
297
298 // Callback used by LookupPayloadViaP2P().
299 void OnLookupPayloadViaP2PCompleted(const std::string& url);
300
Chris Sosa77f79e82014-06-02 18:16:24 -0700301 // Returns true if the current update should be ignored.
Weidong Guo421ff332017-04-17 10:08:38 -0700302 bool ShouldIgnoreUpdate(ErrorCode* error,
303 const OmahaResponse& response) const;
304
305 // Return true if updates are allowed by user preferences.
306 bool IsUpdateAllowedOverCellularByPrefs(const OmahaResponse& response) const;
Chris Sosa77f79e82014-06-02 18:16:24 -0700307
308 // Returns true if updates are allowed over the current type of connection.
309 // False otherwise.
Weidong Guo421ff332017-04-17 10:08:38 -0700310 bool IsUpdateAllowedOverCurrentConnection(
311 ErrorCode* error, const OmahaResponse& response) const;
Chris Sosa77f79e82014-06-02 18:16:24 -0700312
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800313 // Returns true if rollback is enabled. Always returns false for consumer
314 // devices.
315 bool IsRollbackEnabled() const;
316
317 // Sets the appropriate max kernel key version based on whether rollback is
318 // enabled.
319 void SetMaxKernelKeyVersionForRollback() const;
320
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800321 // Global system context.
322 SystemState* system_state_;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700323
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700324 // Contains state that is relevant in the processing of the Omaha request.
325 OmahaRequestParams* params_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000326
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700327 // Pointer to the OmahaEvent info. This is an UpdateCheck request if null.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700328 std::unique_ptr<OmahaEvent> event_;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700329
rspangler@google.com49fdf182009-10-10 00:57:34 +0000330 // pointer to the HttpFetcher that does the http work
Ben Chan02f7c1d2014-10-18 15:18:02 -0700331 std::unique_ptr<HttpFetcher> http_fetcher_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000332
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800333 // Used for fetching information about the device policy.
334 std::unique_ptr<policy::PolicyProvider> policy_provider_;
335
Thieu Le116fda32011-04-19 11:01:54 -0700336 // If true, only include the <ping> element in the request.
337 bool ping_only_;
338
rspangler@google.com49fdf182009-10-10 00:57:34 +0000339 // Stores the response from the omaha server
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700340 brillo::Blob response_buffer_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000341
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700342 // Initialized by InitPingDays to values that may be sent to Omaha
343 // as part of a ping message. Note that only positive values and -1
344 // are sent to Omaha.
345 int ping_active_days_;
346 int ping_roll_call_days_;
347
Darin Petkov6a5b3222010-07-13 14:55:28 -0700348 DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000349};
350
351} // namespace chromeos_update_engine
352
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700353#endif // UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H_