blob: b56af1e88afb9ccbcd0d99c310ab582343b6fcf3 [file] [log] [blame]
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rspangler@google.com49fdf182009-10-10 00:57:34 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Gilad Arnoldcf175a02014-07-10 16:48:47 -07005#ifndef UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H_
6#define UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H_
rspangler@google.com49fdf182009-10-10 00:57:34 +00007
Alex Vakulenko44cab302014-07-23 13:12:15 -07008#include <fcntl.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +00009#include <sys/stat.h>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070010#include <sys/types.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000011
Ben Chan02f7c1d2014-10-18 15:18:02 -070012#include <memory>
rspangler@google.com49fdf182009-10-10 00:57:34 +000013#include <string>
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080014#include <vector>
rspangler@google.com49fdf182009-10-10 00:57:34 +000015
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080016#include <chromeos/secure_blob.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000017#include <curl/curl.h>
18
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070019#include "update_engine/action.h"
20#include "update_engine/http_fetcher.h"
Jay Srinivasan08262882012-12-28 19:29:43 -080021#include "update_engine/omaha_response.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000022
Darin Petkov6a5b3222010-07-13 14:55:28 -070023// The Omaha Request action makes a request to Omaha and can output
24// the response on the output ActionPipe.
rspangler@google.com49fdf182009-10-10 00:57:34 +000025
rspangler@google.com49fdf182009-10-10 00:57:34 +000026namespace chromeos_update_engine {
27
Alex Deymob0d74eb2015-03-30 17:59:17 -070028// Encodes XML entities in a given string. Input must be ASCII-7 valid. If
29// the input is invalid, the default value is used instead.
30std::string XmlEncodeWithDefault(const std::string& input,
31 const std::string& default_value);
32
33// Escapes text so it can be included as character data and attribute
34// values. The |input| string must be valid ASCII-7, no UTF-8 supported.
35// Returns whether the |input| was valid and escaped properly in |output|.
36bool XmlEncode(const std::string& input, std::string* output);
rspangler@google.com49fdf182009-10-10 00:57:34 +000037
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070038// This struct encapsulates the Omaha event information. For a
39// complete list of defined event types and results, see
40// http://code.google.com/p/omaha/wiki/ServerProtocol#event
41struct OmahaEvent {
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070042 // The Type values correspond to EVENT_TYPE values of Omaha.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070043 enum Type {
44 kTypeUnknown = 0,
45 kTypeDownloadComplete = 1,
46 kTypeInstallComplete = 2,
47 kTypeUpdateComplete = 3,
Darin Petkov8c2980e2010-07-16 15:16:49 -070048 kTypeUpdateDownloadStarted = 13,
49 kTypeUpdateDownloadFinished = 14,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070050 };
51
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070052 // The Result values correspond to EVENT_RESULT values of Omaha.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070053 enum Result {
54 kResultError = 0,
55 kResultSuccess = 1,
Darin Petkov95508da2011-01-05 12:42:29 -080056 kResultSuccessReboot = 2,
Alex Vakulenkod2779df2014-06-16 13:19:00 -070057 kResultUpdateDeferred = 9, // When we ignore/defer updates due to policy.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070058 };
59
60 OmahaEvent()
61 : type(kTypeUnknown),
62 result(kResultError),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070063 error_code(ErrorCode::kError) {}
Darin Petkove17f86b2010-07-20 09:12:01 -070064 explicit OmahaEvent(Type in_type)
65 : type(in_type),
66 result(kResultSuccess),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070067 error_code(ErrorCode::kSuccess) {}
David Zeuthena99981f2013-04-29 13:42:47 -070068 OmahaEvent(Type in_type, Result in_result, ErrorCode in_error_code)
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070069 : type(in_type),
70 result(in_result),
71 error_code(in_error_code) {}
72
73 Type type;
74 Result result;
David Zeuthena99981f2013-04-29 13:42:47 -070075 ErrorCode error_code;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070076};
77
rspangler@google.com49fdf182009-10-10 00:57:34 +000078class NoneType;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070079class OmahaRequestAction;
Yunlian Jianga178e5e2013-04-05 14:41:56 -070080class OmahaRequestParams;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070081class PrefsInterface;
rspangler@google.com49fdf182009-10-10 00:57:34 +000082
David Zeuthene8ed8632014-07-24 13:38:10 -040083// This struct is declared in the .cc file.
84struct OmahaParserData;
85
rspangler@google.com49fdf182009-10-10 00:57:34 +000086template<>
Darin Petkov6a5b3222010-07-13 14:55:28 -070087class ActionTraits<OmahaRequestAction> {
rspangler@google.com49fdf182009-10-10 00:57:34 +000088 public:
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070089 // Takes parameters on the input pipe.
Darin Petkova4a8a8c2010-07-15 22:21:12 -070090 typedef NoneType InputObjectType;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070091 // On UpdateCheck success, puts the Omaha response on output. Event
92 // requests do not have an output pipe.
Darin Petkov6a5b3222010-07-13 14:55:28 -070093 typedef OmahaResponse OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +000094};
95
Darin Petkov6a5b3222010-07-13 14:55:28 -070096class OmahaRequestAction : public Action<OmahaRequestAction>,
97 public HttpFetcherDelegate {
rspangler@google.com49fdf182009-10-10 00:57:34 +000098 public:
Darin Petkov1cbd78f2010-07-29 12:38:34 -070099 static const int kNeverPinged = -1;
100 static const int kPingTimeJump = -2;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800101 // We choose this value of 10 as a heuristic for a work day in trying
102 // each URL, assuming we check roughly every 45 mins. This is a good time to
103 // wait - neither too long nor too little - so we don't give up the preferred
104 // URLs that appear earlier in list too quickly before moving on to the
105 // fallback ones.
106 static const int kDefaultMaxFailureCountPerUrl = 10;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700107
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700108 // These are the possible outcome upon checking whether we satisfied
109 // the wall-clock-based-wait.
110 enum WallClockWaitResult {
111 kWallClockWaitNotSatisfied,
112 kWallClockWaitDoneButUpdateCheckWaitRequired,
113 kWallClockWaitDoneAndUpdateCheckWaitNotRequired,
114 };
115
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700116 // The ctor takes in all the parameters that will be used for making
117 // the request to Omaha. For some of them we have constants that
118 // should be used.
119 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000120 // Takes ownership of the passed in HttpFetcher. Useful for testing.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700121 //
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700122 // Takes ownership of the passed in OmahaEvent. If |event| is null,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700123 // this is an UpdateCheck request, otherwise it's an Event request.
124 // Event requests always succeed.
125 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000126 // A good calling pattern is:
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700127 // OmahaRequestAction(..., new OmahaEvent(...), new WhateverHttpFetcher);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700128 // or
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700129 // OmahaRequestAction(..., nullptr, new WhateverHttpFetcher);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800130 OmahaRequestAction(SystemState* system_state,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700131 OmahaEvent* event,
Thieu Le116fda32011-04-19 11:01:54 -0700132 HttpFetcher* http_fetcher,
133 bool ping_only);
Alex Deymo610277e2014-11-11 21:18:11 -0800134 ~OmahaRequestAction() override;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700135 typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType;
136 typedef ActionTraits<OmahaRequestAction>::OutputObjectType OutputObjectType;
Alex Deymo610277e2014-11-11 21:18:11 -0800137 void PerformAction() override;
138 void TerminateProcessing() override;
139 void ActionCompleted(ErrorCode code) override;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000140
Darin Petkov1023a602010-08-30 13:47:51 -0700141 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
142
rspangler@google.com49fdf182009-10-10 00:57:34 +0000143 // Debugging/logging
Darin Petkov6a5b3222010-07-13 14:55:28 -0700144 static std::string StaticType() { return "OmahaRequestAction"; }
Alex Deymo610277e2014-11-11 21:18:11 -0800145 std::string Type() const override { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000146
147 // Delegate methods (see http_fetcher.h)
Alex Deymo610277e2014-11-11 21:18:11 -0800148 void ReceivedBytes(HttpFetcher *fetcher,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800149 const void* bytes, size_t length) override;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000150
Alex Deymo610277e2014-11-11 21:18:11 -0800151 void TransferComplete(HttpFetcher *fetcher, bool successful) override;
152
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700153 // Returns true if this is an Event request, false if it's an UpdateCheck.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700154 bool IsEvent() const { return event_.get() != nullptr; }
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700155
rspangler@google.com49fdf182009-10-10 00:57:34 +0000156 private:
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700157 FRIEND_TEST(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE);
158 FRIEND_TEST(OmahaRequestActionTest,
159 GetInstallDateWhenOOBECompletedWithInvalidDate);
160 FRIEND_TEST(OmahaRequestActionTest,
161 GetInstallDateWhenOOBECompletedWithValidDate);
162 FRIEND_TEST(OmahaRequestActionTest,
163 GetInstallDateWhenOOBECompletedDateChanges);
David Zeuthen639aa362014-02-03 16:23:44 -0800164
165 // Enumeration used in PersistInstallDate().
166 enum InstallDateProvisioningSource {
167 kProvisionedFromOmahaResponse,
168 kProvisionedFromOOBEMarker,
169
170 // kProvisionedMax is the count of the number of enums above. Add
171 // any new enums above this line only.
172 kProvisionedMax
173 };
174
175 // Gets the install date, expressed as the number of PST8PDT
176 // calendar weeks since January 1st 2007, times seven. Returns -1 if
177 // unknown. See http://crbug.com/336838 for details about this value.
178 static int GetInstallDate(SystemState* system_state);
179
180 // Parses the Omaha Response in |doc| and sets the
181 // |install_date_days| field of |output_object| to the value of the
182 // elapsed_days attribute of the daystart element. Returns True if
183 // the value was set, False if it wasn't found.
David Zeuthene8ed8632014-07-24 13:38:10 -0400184 static bool ParseInstallDate(OmahaParserData* parser_data,
David Zeuthen639aa362014-02-03 16:23:44 -0800185 OmahaResponse* output_object);
186
187 // Returns True if the kPrefsInstallDateDays state variable is set,
188 // False otherwise.
189 static bool HasInstallDate(SystemState *system_state);
190
191 // Writes |install_date_days| into the kPrefsInstallDateDays state
192 // variable and emits an UMA stat for the |source| used. Returns
193 // True if the value was written, False if an error occurred.
194 static bool PersistInstallDate(SystemState *system_state,
195 int install_date_days,
196 InstallDateProvisioningSource source);
197
Alex Deymo8e18f932015-03-27 16:16:59 -0700198 // Persist the new cohort* value received in the XML file in the |prefs_key|
199 // preference file. If the |new_value| is empty, the currently stored value
200 // will be deleted. Don't call this function with an empty |new_value| if the
201 // value was not set in the XML, since that would delete the stored value.
202 bool PersistCohortData(const std::string& prefs_key,
203 const std::string& new_value);
204
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700205 // If this is an update check request, initializes
206 // |ping_active_days_| and |ping_roll_call_days_| to values that may
207 // be sent as pings to Omaha.
208 void InitPingDays();
209
Darin Petkov84c763c2010-07-29 16:27:58 -0700210 // Based on the persistent preference store values, calculates the
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700211 // number of days since the last ping sent for |key|.
212 int CalculatePingDays(const std::string& key);
213
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700214 // Returns whether we have "active_days" or "roll_call_days" ping values to
215 // send to Omaha and thus we should include them in the response.
216 bool ShouldPing() const;
217
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700218 // Returns true if the download of a new update should be deferred.
219 // False if the update can be downloaded.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700220 bool ShouldDeferDownload(OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700221
222 // Returns true if the basic wall-clock-based waiting period has been
223 // satisfied based on the scattering policy setting. False otherwise.
224 // If true, it also indicates whether the additional update-check-count-based
225 // waiting period also needs to be satisfied before the download can begin.
226 WallClockWaitResult IsWallClockBasedWaitingSatisfied(
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700227 OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700228
229 // Returns true if the update-check-count-based waiting period has been
230 // satisfied. False otherwise.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700231 bool IsUpdateCheckCountBasedWaitingSatisfied();
232
233 // Parses the response from Omaha that's available in |doc| using the other
234 // helper methods below and populates the |output_object| with the relevant
235 // values. Returns true if we should continue the parsing. False otherwise,
236 // in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400237 bool ParseResponse(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700238 OmahaResponse* output_object,
239 ScopedActionCompleter* completer);
240
241 // Parses the status property in the given update_check_node and populates
242 // |output_object| if valid. Returns true if we should continue the parsing.
243 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400244 bool ParseStatus(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700245 OmahaResponse* output_object,
246 ScopedActionCompleter* completer);
247
248 // Parses the URL nodes in the given XML document and populates
249 // |output_object| if valid. Returns true if we should continue the parsing.
250 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400251 bool ParseUrls(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700252 OmahaResponse* output_object,
253 ScopedActionCompleter* completer);
254
255 // Parses the package node in the given XML document and populates
256 // |output_object| if valid. Returns true if we should continue the parsing.
257 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400258 bool ParsePackage(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700259 OmahaResponse* output_object,
260 ScopedActionCompleter* completer);
261
262 // Parses the other parameters in the given XML document and populates
263 // |output_object| if valid. Returns true if we should continue the parsing.
264 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400265 bool ParseParams(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700266 OmahaResponse* output_object,
267 ScopedActionCompleter* completer);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700268
David Zeuthen8f191b22013-08-06 12:27:50 -0700269 // Called by TransferComplete() to complete processing, either
270 // asynchronously after looking up resources via p2p or directly.
271 void CompleteProcessing();
272
273 // Helper to asynchronously look up payload on the LAN.
274 void LookupPayloadViaP2P(const OmahaResponse& response);
275
276 // Callback used by LookupPayloadViaP2P().
277 void OnLookupPayloadViaP2PCompleted(const std::string& url);
278
Chris Sosa77f79e82014-06-02 18:16:24 -0700279 // Returns true if the current update should be ignored.
280 bool ShouldIgnoreUpdate(const OmahaResponse& response) const;
281
282 // Returns true if updates are allowed over the current type of connection.
283 // False otherwise.
284 bool IsUpdateAllowedOverCurrentConnection() 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
Thieu Le116fda32011-04-19 11:01:54 -0700298 // If true, only include the <ping> element in the request.
299 bool ping_only_;
300
rspangler@google.com49fdf182009-10-10 00:57:34 +0000301 // Stores the response from the omaha server
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800302 chromeos::Blob response_buffer_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000303
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700304 // Initialized by InitPingDays to values that may be sent to Omaha
305 // as part of a ping message. Note that only positive values and -1
306 // are sent to Omaha.
307 int ping_active_days_;
308 int ping_roll_call_days_;
309
Darin Petkov6a5b3222010-07-13 14:55:28 -0700310 DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000311};
312
313} // namespace chromeos_update_engine
314
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700315#endif // UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H_