blob: 0b93109eecd0150c5e54331cf97c5319482a00b2 [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);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700127 virtual ~OmahaRequestAction();
128 typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType;
129 typedef ActionTraits<OmahaRequestAction>::OutputObjectType OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000130 void PerformAction();
131 void TerminateProcessing();
David Zeuthen33bae492014-02-25 16:16:18 -0800132 void ActionCompleted(ErrorCode code);
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"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000138 std::string Type() const { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000139
140 // Delegate methods (see http_fetcher.h)
141 virtual void ReceivedBytes(HttpFetcher *fetcher,
142 const char* bytes, int length);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000143
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700144 virtual void TransferComplete(HttpFetcher *fetcher, bool successful);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700145 // Returns true if this is an Event request, false if it's an UpdateCheck.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700146 bool IsEvent() const { return event_.get() != nullptr; }
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700147
rspangler@google.com49fdf182009-10-10 00:57:34 +0000148 private:
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700149 FRIEND_TEST(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE);
150 FRIEND_TEST(OmahaRequestActionTest,
151 GetInstallDateWhenOOBECompletedWithInvalidDate);
152 FRIEND_TEST(OmahaRequestActionTest,
153 GetInstallDateWhenOOBECompletedWithValidDate);
154 FRIEND_TEST(OmahaRequestActionTest,
155 GetInstallDateWhenOOBECompletedDateChanges);
David Zeuthen639aa362014-02-03 16:23:44 -0800156
157 // Enumeration used in PersistInstallDate().
158 enum InstallDateProvisioningSource {
159 kProvisionedFromOmahaResponse,
160 kProvisionedFromOOBEMarker,
161
162 // kProvisionedMax is the count of the number of enums above. Add
163 // any new enums above this line only.
164 kProvisionedMax
165 };
166
167 // Gets the install date, expressed as the number of PST8PDT
168 // calendar weeks since January 1st 2007, times seven. Returns -1 if
169 // unknown. See http://crbug.com/336838 for details about this value.
170 static int GetInstallDate(SystemState* system_state);
171
172 // Parses the Omaha Response in |doc| and sets the
173 // |install_date_days| field of |output_object| to the value of the
174 // elapsed_days attribute of the daystart element. Returns True if
175 // the value was set, False if it wasn't found.
David Zeuthene8ed8632014-07-24 13:38:10 -0400176 static bool ParseInstallDate(OmahaParserData* parser_data,
David Zeuthen639aa362014-02-03 16:23:44 -0800177 OmahaResponse* output_object);
178
179 // Returns True if the kPrefsInstallDateDays state variable is set,
180 // False otherwise.
181 static bool HasInstallDate(SystemState *system_state);
182
183 // Writes |install_date_days| into the kPrefsInstallDateDays state
184 // variable and emits an UMA stat for the |source| used. Returns
185 // True if the value was written, False if an error occurred.
186 static bool PersistInstallDate(SystemState *system_state,
187 int install_date_days,
188 InstallDateProvisioningSource source);
189
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
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700203 // Returns true if the download of a new update should be deferred.
204 // False if the update can be downloaded.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700205 bool ShouldDeferDownload(OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700206
207 // Returns true if the basic wall-clock-based waiting period has been
208 // satisfied based on the scattering policy setting. False otherwise.
209 // If true, it also indicates whether the additional update-check-count-based
210 // waiting period also needs to be satisfied before the download can begin.
211 WallClockWaitResult IsWallClockBasedWaitingSatisfied(
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700212 OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700213
214 // Returns true if the update-check-count-based waiting period has been
215 // satisfied. False otherwise.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700216 bool IsUpdateCheckCountBasedWaitingSatisfied();
217
218 // Parses the response from Omaha that's available in |doc| using the other
219 // helper methods below and populates the |output_object| with the relevant
220 // values. Returns true if we should continue the parsing. False otherwise,
221 // in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400222 bool ParseResponse(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700223 OmahaResponse* output_object,
224 ScopedActionCompleter* completer);
225
226 // Parses the status property in the given update_check_node and populates
227 // |output_object| if valid. Returns true if we should continue the parsing.
228 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400229 bool ParseStatus(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700230 OmahaResponse* output_object,
231 ScopedActionCompleter* completer);
232
233 // Parses the URL nodes in the given XML document and populates
234 // |output_object| if valid. Returns true if we should continue the parsing.
235 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400236 bool ParseUrls(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700237 OmahaResponse* output_object,
238 ScopedActionCompleter* completer);
239
240 // Parses the package node in the given XML document and populates
241 // |output_object| if valid. Returns true if we should continue the parsing.
242 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400243 bool ParsePackage(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700244 OmahaResponse* output_object,
245 ScopedActionCompleter* completer);
246
247 // Parses the other parameters in the given XML document and populates
248 // |output_object| if valid. Returns true if we should continue the parsing.
249 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400250 bool ParseParams(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700251 OmahaResponse* output_object,
252 ScopedActionCompleter* completer);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700253
David Zeuthen8f191b22013-08-06 12:27:50 -0700254 // Called by TransferComplete() to complete processing, either
255 // asynchronously after looking up resources via p2p or directly.
256 void CompleteProcessing();
257
258 // Helper to asynchronously look up payload on the LAN.
259 void LookupPayloadViaP2P(const OmahaResponse& response);
260
261 // Callback used by LookupPayloadViaP2P().
262 void OnLookupPayloadViaP2PCompleted(const std::string& url);
263
Chris Sosa77f79e82014-06-02 18:16:24 -0700264 // Returns true if the current update should be ignored.
265 bool ShouldIgnoreUpdate(const OmahaResponse& response) const;
266
267 // Returns true if updates are allowed over the current type of connection.
268 // False otherwise.
269 bool IsUpdateAllowedOverCurrentConnection() const;
270
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800271 // Global system context.
272 SystemState* system_state_;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700273
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700274 // Contains state that is relevant in the processing of the Omaha request.
275 OmahaRequestParams* params_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000276
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700277 // Pointer to the OmahaEvent info. This is an UpdateCheck request if null.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700278 std::unique_ptr<OmahaEvent> event_;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700279
rspangler@google.com49fdf182009-10-10 00:57:34 +0000280 // pointer to the HttpFetcher that does the http work
Ben Chan02f7c1d2014-10-18 15:18:02 -0700281 std::unique_ptr<HttpFetcher> http_fetcher_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000282
Thieu Le116fda32011-04-19 11:01:54 -0700283 // If true, only include the <ping> element in the request.
284 bool ping_only_;
285
rspangler@google.com49fdf182009-10-10 00:57:34 +0000286 // Stores the response from the omaha server
287 std::vector<char> response_buffer_;
288
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700289 // Initialized by InitPingDays to values that may be sent to Omaha
290 // as part of a ping message. Note that only positive values and -1
291 // are sent to Omaha.
292 int ping_active_days_;
293 int ping_roll_call_days_;
294
Darin Petkov6a5b3222010-07-13 14:55:28 -0700295 DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000296};
297
298} // namespace chromeos_update_engine
299
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700300#endif // UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H_