blob: d086a24e19f7bb27f7d2be40b7c3a1d9581e18de [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
16#include <curl/curl.h>
17
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070018#include "update_engine/action.h"
19#include "update_engine/http_fetcher.h"
Jay Srinivasan08262882012-12-28 19:29:43 -080020#include "update_engine/omaha_response.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000021
Darin Petkov6a5b3222010-07-13 14:55:28 -070022// The Omaha Request action makes a request to Omaha and can output
23// the response on the output ActionPipe.
rspangler@google.com49fdf182009-10-10 00:57:34 +000024
rspangler@google.com49fdf182009-10-10 00:57:34 +000025namespace chromeos_update_engine {
26
27// Encodes XML entities in a given string with libxml2. input must be
28// UTF-8 formatted. Output will be UTF-8 formatted.
29std::string XmlEncode(const std::string& input);
30
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070031// This struct encapsulates the Omaha event information. For a
32// complete list of defined event types and results, see
33// http://code.google.com/p/omaha/wiki/ServerProtocol#event
34struct OmahaEvent {
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070035 // The Type values correspond to EVENT_TYPE values of Omaha.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070036 enum Type {
37 kTypeUnknown = 0,
38 kTypeDownloadComplete = 1,
39 kTypeInstallComplete = 2,
40 kTypeUpdateComplete = 3,
Darin Petkov8c2980e2010-07-16 15:16:49 -070041 kTypeUpdateDownloadStarted = 13,
42 kTypeUpdateDownloadFinished = 14,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070043 };
44
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070045 // The Result values correspond to EVENT_RESULT values of Omaha.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070046 enum Result {
47 kResultError = 0,
48 kResultSuccess = 1,
Darin Petkov95508da2011-01-05 12:42:29 -080049 kResultSuccessReboot = 2,
Alex Vakulenkod2779df2014-06-16 13:19:00 -070050 kResultUpdateDeferred = 9, // When we ignore/defer updates due to policy.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070051 };
52
53 OmahaEvent()
54 : type(kTypeUnknown),
55 result(kResultError),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070056 error_code(ErrorCode::kError) {}
Darin Petkove17f86b2010-07-20 09:12:01 -070057 explicit OmahaEvent(Type in_type)
58 : type(in_type),
59 result(kResultSuccess),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070060 error_code(ErrorCode::kSuccess) {}
David Zeuthena99981f2013-04-29 13:42:47 -070061 OmahaEvent(Type in_type, Result in_result, ErrorCode in_error_code)
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070062 : type(in_type),
63 result(in_result),
64 error_code(in_error_code) {}
65
66 Type type;
67 Result result;
David Zeuthena99981f2013-04-29 13:42:47 -070068 ErrorCode error_code;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070069};
70
rspangler@google.com49fdf182009-10-10 00:57:34 +000071class NoneType;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070072class OmahaRequestAction;
Yunlian Jianga178e5e2013-04-05 14:41:56 -070073class OmahaRequestParams;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070074class PrefsInterface;
rspangler@google.com49fdf182009-10-10 00:57:34 +000075
David Zeuthene8ed8632014-07-24 13:38:10 -040076// This struct is declared in the .cc file.
77struct OmahaParserData;
78
rspangler@google.com49fdf182009-10-10 00:57:34 +000079template<>
Darin Petkov6a5b3222010-07-13 14:55:28 -070080class ActionTraits<OmahaRequestAction> {
rspangler@google.com49fdf182009-10-10 00:57:34 +000081 public:
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070082 // Takes parameters on the input pipe.
Darin Petkova4a8a8c2010-07-15 22:21:12 -070083 typedef NoneType InputObjectType;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070084 // On UpdateCheck success, puts the Omaha response on output. Event
85 // requests do not have an output pipe.
Darin Petkov6a5b3222010-07-13 14:55:28 -070086 typedef OmahaResponse OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +000087};
88
Darin Petkov6a5b3222010-07-13 14:55:28 -070089class OmahaRequestAction : public Action<OmahaRequestAction>,
90 public HttpFetcherDelegate {
rspangler@google.com49fdf182009-10-10 00:57:34 +000091 public:
Darin Petkov1cbd78f2010-07-29 12:38:34 -070092 static const int kNeverPinged = -1;
93 static const int kPingTimeJump = -2;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080094 // We choose this value of 10 as a heuristic for a work day in trying
95 // each URL, assuming we check roughly every 45 mins. This is a good time to
96 // wait - neither too long nor too little - so we don't give up the preferred
97 // URLs that appear earlier in list too quickly before moving on to the
98 // fallback ones.
99 static const int kDefaultMaxFailureCountPerUrl = 10;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700100
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700101 // These are the possible outcome upon checking whether we satisfied
102 // the wall-clock-based-wait.
103 enum WallClockWaitResult {
104 kWallClockWaitNotSatisfied,
105 kWallClockWaitDoneButUpdateCheckWaitRequired,
106 kWallClockWaitDoneAndUpdateCheckWaitNotRequired,
107 };
108
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700109 // The ctor takes in all the parameters that will be used for making
110 // the request to Omaha. For some of them we have constants that
111 // should be used.
112 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000113 // Takes ownership of the passed in HttpFetcher. Useful for testing.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700114 //
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700115 // Takes ownership of the passed in OmahaEvent. If |event| is null,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700116 // this is an UpdateCheck request, otherwise it's an Event request.
117 // Event requests always succeed.
118 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000119 // A good calling pattern is:
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700120 // OmahaRequestAction(..., new OmahaEvent(...), new WhateverHttpFetcher);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700121 // or
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700122 // OmahaRequestAction(..., nullptr, new WhateverHttpFetcher);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800123 OmahaRequestAction(SystemState* system_state,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700124 OmahaEvent* event,
Thieu Le116fda32011-04-19 11:01:54 -0700125 HttpFetcher* http_fetcher,
126 bool ping_only);
Alex Deymo610277e2014-11-11 21:18:11 -0800127 ~OmahaRequestAction() override;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700128 typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType;
129 typedef ActionTraits<OmahaRequestAction>::OutputObjectType OutputObjectType;
Alex Deymo610277e2014-11-11 21:18:11 -0800130 void PerformAction() override;
131 void TerminateProcessing() override;
132 void ActionCompleted(ErrorCode code) override;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000133
Darin Petkov1023a602010-08-30 13:47:51 -0700134 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
135
rspangler@google.com49fdf182009-10-10 00:57:34 +0000136 // Debugging/logging
Darin Petkov6a5b3222010-07-13 14:55:28 -0700137 static std::string StaticType() { return "OmahaRequestAction"; }
Alex Deymo610277e2014-11-11 21:18:11 -0800138 std::string Type() const override { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000139
140 // Delegate methods (see http_fetcher.h)
Alex Deymo610277e2014-11-11 21:18:11 -0800141 void ReceivedBytes(HttpFetcher *fetcher,
142 const char* bytes, int length) override;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000143
Alex Deymo610277e2014-11-11 21:18:11 -0800144 void TransferComplete(HttpFetcher *fetcher, bool successful) override;
145
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700146 // Returns true if this is an Event request, false if it's an UpdateCheck.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700147 bool IsEvent() const { return event_.get() != nullptr; }
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700148
rspangler@google.com49fdf182009-10-10 00:57:34 +0000149 private:
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700150 FRIEND_TEST(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE);
151 FRIEND_TEST(OmahaRequestActionTest,
152 GetInstallDateWhenOOBECompletedWithInvalidDate);
153 FRIEND_TEST(OmahaRequestActionTest,
154 GetInstallDateWhenOOBECompletedWithValidDate);
155 FRIEND_TEST(OmahaRequestActionTest,
156 GetInstallDateWhenOOBECompletedDateChanges);
David Zeuthen639aa362014-02-03 16:23:44 -0800157
158 // Enumeration used in PersistInstallDate().
159 enum InstallDateProvisioningSource {
160 kProvisionedFromOmahaResponse,
161 kProvisionedFromOOBEMarker,
162
163 // kProvisionedMax is the count of the number of enums above. Add
164 // any new enums above this line only.
165 kProvisionedMax
166 };
167
168 // Gets the install date, expressed as the number of PST8PDT
169 // calendar weeks since January 1st 2007, times seven. Returns -1 if
170 // unknown. See http://crbug.com/336838 for details about this value.
171 static int GetInstallDate(SystemState* system_state);
172
173 // Parses the Omaha Response in |doc| and sets the
174 // |install_date_days| field of |output_object| to the value of the
175 // elapsed_days attribute of the daystart element. Returns True if
176 // the value was set, False if it wasn't found.
David Zeuthene8ed8632014-07-24 13:38:10 -0400177 static bool ParseInstallDate(OmahaParserData* parser_data,
David Zeuthen639aa362014-02-03 16:23:44 -0800178 OmahaResponse* output_object);
179
180 // Returns True if the kPrefsInstallDateDays state variable is set,
181 // False otherwise.
182 static bool HasInstallDate(SystemState *system_state);
183
184 // Writes |install_date_days| into the kPrefsInstallDateDays state
185 // variable and emits an UMA stat for the |source| used. Returns
186 // True if the value was written, False if an error occurred.
187 static bool PersistInstallDate(SystemState *system_state,
188 int install_date_days,
189 InstallDateProvisioningSource source);
190
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700191 // If this is an update check request, initializes
192 // |ping_active_days_| and |ping_roll_call_days_| to values that may
193 // be sent as pings to Omaha.
194 void InitPingDays();
195
Darin Petkov84c763c2010-07-29 16:27:58 -0700196 // Based on the persistent preference store values, calculates the
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700197 // number of days since the last ping sent for |key|.
198 int CalculatePingDays(const std::string& key);
199
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700200 // Returns whether we have "active_days" or "roll_call_days" ping values to
201 // send to Omaha and thus we should include them in the response.
202 bool ShouldPing() const;
203
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700204 // Returns true if the download of a new update should be deferred.
205 // False if the update can be downloaded.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700206 bool ShouldDeferDownload(OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700207
208 // Returns true if the basic wall-clock-based waiting period has been
209 // satisfied based on the scattering policy setting. False otherwise.
210 // If true, it also indicates whether the additional update-check-count-based
211 // waiting period also needs to be satisfied before the download can begin.
212 WallClockWaitResult IsWallClockBasedWaitingSatisfied(
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700213 OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700214
215 // Returns true if the update-check-count-based waiting period has been
216 // satisfied. False otherwise.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700217 bool IsUpdateCheckCountBasedWaitingSatisfied();
218
219 // Parses the response from Omaha that's available in |doc| using the other
220 // helper methods below and populates the |output_object| with the relevant
221 // values. Returns true if we should continue the parsing. False otherwise,
222 // in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400223 bool ParseResponse(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700224 OmahaResponse* output_object,
225 ScopedActionCompleter* completer);
226
227 // Parses the status property in the given update_check_node and populates
228 // |output_object| if valid. Returns true if we should continue the parsing.
229 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400230 bool ParseStatus(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700231 OmahaResponse* output_object,
232 ScopedActionCompleter* completer);
233
234 // Parses the URL nodes in the given XML document and populates
235 // |output_object| if valid. Returns true if we should continue the parsing.
236 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400237 bool ParseUrls(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700238 OmahaResponse* output_object,
239 ScopedActionCompleter* completer);
240
241 // Parses the package node in the given XML document 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 ParsePackage(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700245 OmahaResponse* output_object,
246 ScopedActionCompleter* completer);
247
248 // Parses the other parameters 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 ParseParams(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700252 OmahaResponse* output_object,
253 ScopedActionCompleter* completer);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700254
David Zeuthen8f191b22013-08-06 12:27:50 -0700255 // Called by TransferComplete() to complete processing, either
256 // asynchronously after looking up resources via p2p or directly.
257 void CompleteProcessing();
258
259 // Helper to asynchronously look up payload on the LAN.
260 void LookupPayloadViaP2P(const OmahaResponse& response);
261
262 // Callback used by LookupPayloadViaP2P().
263 void OnLookupPayloadViaP2PCompleted(const std::string& url);
264
Chris Sosa77f79e82014-06-02 18:16:24 -0700265 // Returns true if the current update should be ignored.
266 bool ShouldIgnoreUpdate(const OmahaResponse& response) const;
267
268 // Returns true if updates are allowed over the current type of connection.
269 // False otherwise.
270 bool IsUpdateAllowedOverCurrentConnection() const;
271
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800272 // Global system context.
273 SystemState* system_state_;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700274
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700275 // Contains state that is relevant in the processing of the Omaha request.
276 OmahaRequestParams* params_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000277
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700278 // Pointer to the OmahaEvent info. This is an UpdateCheck request if null.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700279 std::unique_ptr<OmahaEvent> event_;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700280
rspangler@google.com49fdf182009-10-10 00:57:34 +0000281 // pointer to the HttpFetcher that does the http work
Ben Chan02f7c1d2014-10-18 15:18:02 -0700282 std::unique_ptr<HttpFetcher> http_fetcher_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000283
Thieu Le116fda32011-04-19 11:01:54 -0700284 // If true, only include the <ping> element in the request.
285 bool ping_only_;
286
rspangler@google.com49fdf182009-10-10 00:57:34 +0000287 // Stores the response from the omaha server
288 std::vector<char> response_buffer_;
289
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700290 // Initialized by InitPingDays to values that may be sent to Omaha
291 // as part of a ping message. Note that only positive values and -1
292 // are sent to Omaha.
293 int ping_active_days_;
294 int ping_roll_call_days_;
295
Darin Petkov6a5b3222010-07-13 14:55:28 -0700296 DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000297};
298
299} // namespace chromeos_update_engine
300
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700301#endif // UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H_