blob: 6fef8e5b08a4b5949b93e3581ba70ee2292034cc [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
Alex Deymo759c2752014-03-17 21:09:36 -07005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H_
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H_
rspangler@google.com49fdf182009-10-10 00:57:34 +00007
rspangler@google.com49fdf182009-10-10 00:57:34 +00008#include <sys/stat.h>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07009#include <sys/types.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000010#include <fcntl.h>
11
12#include <string>
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080013#include <vector>
rspangler@google.com49fdf182009-10-10 00:57:34 +000014
Chris Masoned903c3b2011-05-12 15:35:46 -070015#include <base/memory/scoped_ptr.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000016#include <curl/curl.h>
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070017#include <libxml/parser.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000018
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
28// Encodes XML entities in a given string with libxml2. input must be
29// UTF-8 formatted. Output will be UTF-8 formatted.
30std::string XmlEncode(const std::string& input);
31
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070032// This struct encapsulates the Omaha event information. For a
33// complete list of defined event types and results, see
34// http://code.google.com/p/omaha/wiki/ServerProtocol#event
35struct OmahaEvent {
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070036 // The Type values correspond to EVENT_TYPE values of Omaha.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070037 enum Type {
38 kTypeUnknown = 0,
39 kTypeDownloadComplete = 1,
40 kTypeInstallComplete = 2,
41 kTypeUpdateComplete = 3,
Darin Petkov8c2980e2010-07-16 15:16:49 -070042 kTypeUpdateDownloadStarted = 13,
43 kTypeUpdateDownloadFinished = 14,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070044 };
45
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070046 // The Result values correspond to EVENT_RESULT values of Omaha.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070047 enum Result {
48 kResultError = 0,
49 kResultSuccess = 1,
Darin Petkov95508da2011-01-05 12:42:29 -080050 kResultSuccessReboot = 2,
Alex Vakulenkod2779df2014-06-16 13:19:00 -070051 kResultUpdateDeferred = 9, // When we ignore/defer updates due to policy.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070052 };
53
54 OmahaEvent()
55 : type(kTypeUnknown),
56 result(kResultError),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070057 error_code(ErrorCode::kError) {}
Darin Petkove17f86b2010-07-20 09:12:01 -070058 explicit OmahaEvent(Type in_type)
59 : type(in_type),
60 result(kResultSuccess),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070061 error_code(ErrorCode::kSuccess) {}
David Zeuthena99981f2013-04-29 13:42:47 -070062 OmahaEvent(Type in_type, Result in_result, ErrorCode in_error_code)
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070063 : type(in_type),
64 result(in_result),
65 error_code(in_error_code) {}
66
67 Type type;
68 Result result;
David Zeuthena99981f2013-04-29 13:42:47 -070069 ErrorCode error_code;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070070};
71
rspangler@google.com49fdf182009-10-10 00:57:34 +000072class NoneType;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070073class OmahaRequestAction;
Yunlian Jianga178e5e2013-04-05 14:41:56 -070074class OmahaRequestParams;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070075class PrefsInterface;
rspangler@google.com49fdf182009-10-10 00:57:34 +000076
77template<>
Darin Petkov6a5b3222010-07-13 14:55:28 -070078class ActionTraits<OmahaRequestAction> {
rspangler@google.com49fdf182009-10-10 00:57:34 +000079 public:
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070080 // Takes parameters on the input pipe.
Darin Petkova4a8a8c2010-07-15 22:21:12 -070081 typedef NoneType InputObjectType;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070082 // On UpdateCheck success, puts the Omaha response on output. Event
83 // requests do not have an output pipe.
Darin Petkov6a5b3222010-07-13 14:55:28 -070084 typedef OmahaResponse OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +000085};
86
Darin Petkov6a5b3222010-07-13 14:55:28 -070087class OmahaRequestAction : public Action<OmahaRequestAction>,
88 public HttpFetcherDelegate {
rspangler@google.com49fdf182009-10-10 00:57:34 +000089 public:
Darin Petkov1cbd78f2010-07-29 12:38:34 -070090 static const int kNeverPinged = -1;
91 static const int kPingTimeJump = -2;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080092 // We choose this value of 10 as a heuristic for a work day in trying
93 // each URL, assuming we check roughly every 45 mins. This is a good time to
94 // wait - neither too long nor too little - so we don't give up the preferred
95 // URLs that appear earlier in list too quickly before moving on to the
96 // fallback ones.
97 static const int kDefaultMaxFailureCountPerUrl = 10;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070098
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070099 // These are the possible outcome upon checking whether we satisfied
100 // the wall-clock-based-wait.
101 enum WallClockWaitResult {
102 kWallClockWaitNotSatisfied,
103 kWallClockWaitDoneButUpdateCheckWaitRequired,
104 kWallClockWaitDoneAndUpdateCheckWaitNotRequired,
105 };
106
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700107 // The ctor takes in all the parameters that will be used for making
108 // the request to Omaha. For some of them we have constants that
109 // should be used.
110 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000111 // Takes ownership of the passed in HttpFetcher. Useful for testing.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700112 //
113 // Takes ownership of the passed in OmahaEvent. If |event| is NULL,
114 // this is an UpdateCheck request, otherwise it's an Event request.
115 // Event requests always succeed.
116 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000117 // A good calling pattern is:
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700118 // OmahaRequestAction(..., new OmahaEvent(...), new WhateverHttpFetcher);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700119 // or
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700120 // OmahaRequestAction(..., NULL, new WhateverHttpFetcher);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800121 OmahaRequestAction(SystemState* system_state,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700122 OmahaEvent* event,
Thieu Le116fda32011-04-19 11:01:54 -0700123 HttpFetcher* http_fetcher,
124 bool ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700125 virtual ~OmahaRequestAction();
126 typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType;
127 typedef ActionTraits<OmahaRequestAction>::OutputObjectType OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000128 void PerformAction();
129 void TerminateProcessing();
David Zeuthen33bae492014-02-25 16:16:18 -0800130 void ActionCompleted(ErrorCode code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000131
Darin Petkov1023a602010-08-30 13:47:51 -0700132 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
133
rspangler@google.com49fdf182009-10-10 00:57:34 +0000134 // Debugging/logging
Darin Petkov6a5b3222010-07-13 14:55:28 -0700135 static std::string StaticType() { return "OmahaRequestAction"; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000136 std::string Type() const { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000137
138 // Delegate methods (see http_fetcher.h)
139 virtual void ReceivedBytes(HttpFetcher *fetcher,
140 const char* bytes, int length);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000141
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700142 virtual void TransferComplete(HttpFetcher *fetcher, bool successful);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700143 // Returns true if this is an Event request, false if it's an UpdateCheck.
144 bool IsEvent() const { return event_.get() != NULL; }
145
rspangler@google.com49fdf182009-10-10 00:57:34 +0000146 private:
David Zeuthen639aa362014-02-03 16:23:44 -0800147 FRIEND_TEST(OmahaRequestActionTest, GetInstallDate);
148
149 // Enumeration used in PersistInstallDate().
150 enum InstallDateProvisioningSource {
151 kProvisionedFromOmahaResponse,
152 kProvisionedFromOOBEMarker,
153
154 // kProvisionedMax is the count of the number of enums above. Add
155 // any new enums above this line only.
156 kProvisionedMax
157 };
158
159 // Gets the install date, expressed as the number of PST8PDT
160 // calendar weeks since January 1st 2007, times seven. Returns -1 if
161 // unknown. See http://crbug.com/336838 for details about this value.
162 static int GetInstallDate(SystemState* system_state);
163
164 // Parses the Omaha Response in |doc| and sets the
165 // |install_date_days| field of |output_object| to the value of the
166 // elapsed_days attribute of the daystart element. Returns True if
167 // the value was set, False if it wasn't found.
168 static bool ParseInstallDate(xmlDoc* doc,
169 OmahaResponse* output_object);
170
171 // Returns True if the kPrefsInstallDateDays state variable is set,
172 // False otherwise.
173 static bool HasInstallDate(SystemState *system_state);
174
175 // Writes |install_date_days| into the kPrefsInstallDateDays state
176 // variable and emits an UMA stat for the |source| used. Returns
177 // True if the value was written, False if an error occurred.
178 static bool PersistInstallDate(SystemState *system_state,
179 int install_date_days,
180 InstallDateProvisioningSource source);
181
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700182 // If this is an update check request, initializes
183 // |ping_active_days_| and |ping_roll_call_days_| to values that may
184 // be sent as pings to Omaha.
185 void InitPingDays();
186
Darin Petkov84c763c2010-07-29 16:27:58 -0700187 // Based on the persistent preference store values, calculates the
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700188 // number of days since the last ping sent for |key|.
189 int CalculatePingDays(const std::string& key);
190
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700191 // Returns true if the download of a new update should be deferred.
192 // False if the update can be downloaded.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700193 bool ShouldDeferDownload(OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700194
195 // Returns true if the basic wall-clock-based waiting period has been
196 // satisfied based on the scattering policy setting. False otherwise.
197 // If true, it also indicates whether the additional update-check-count-based
198 // waiting period also needs to be satisfied before the download can begin.
199 WallClockWaitResult IsWallClockBasedWaitingSatisfied(
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700200 OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700201
202 // Returns true if the update-check-count-based waiting period has been
203 // satisfied. False otherwise.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700204 bool IsUpdateCheckCountBasedWaitingSatisfied();
205
206 // Parses the response from Omaha that's available in |doc| using the other
207 // helper methods below and populates the |output_object| with the relevant
208 // values. Returns true if we should continue the parsing. False otherwise,
209 // in which case it sets any error code using |completer|.
210 bool ParseResponse(xmlDoc* doc,
211 OmahaResponse* output_object,
212 ScopedActionCompleter* completer);
213
214 // Parses the status property in the given update_check_node and populates
215 // |output_object| if valid. Returns true if we should continue the parsing.
216 // False otherwise, in which case it sets any error code using |completer|.
217 bool ParseStatus(xmlNode* update_check_node,
218 OmahaResponse* output_object,
219 ScopedActionCompleter* completer);
220
221 // Parses the URL nodes in the given XML document and populates
222 // |output_object| if valid. Returns true if we should continue the parsing.
223 // False otherwise, in which case it sets any error code using |completer|.
224 bool ParseUrls(xmlDoc* doc,
225 OmahaResponse* output_object,
226 ScopedActionCompleter* completer);
227
228 // Parses the package node in the given XML document and populates
229 // |output_object| if valid. Returns true if we should continue the parsing.
230 // False otherwise, in which case it sets any error code using |completer|.
231 bool ParsePackage(xmlDoc* doc,
232 OmahaResponse* output_object,
233 ScopedActionCompleter* completer);
234
235 // Parses the other parameters in the given XML document and populates
236 // |output_object| if valid. Returns true if we should continue the parsing.
237 // False otherwise, in which case it sets any error code using |completer|.
238 bool ParseParams(xmlDoc* doc,
239 OmahaResponse* output_object,
240 ScopedActionCompleter* completer);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700241
David Zeuthen8f191b22013-08-06 12:27:50 -0700242 // Called by TransferComplete() to complete processing, either
243 // asynchronously after looking up resources via p2p or directly.
244 void CompleteProcessing();
245
246 // Helper to asynchronously look up payload on the LAN.
247 void LookupPayloadViaP2P(const OmahaResponse& response);
248
249 // Callback used by LookupPayloadViaP2P().
250 void OnLookupPayloadViaP2PCompleted(const std::string& url);
251
Chris Sosa77f79e82014-06-02 18:16:24 -0700252 // Returns true if the current update should be ignored.
253 bool ShouldIgnoreUpdate(const OmahaResponse& response) const;
254
255 // Returns true if updates are allowed over the current type of connection.
256 // False otherwise.
257 bool IsUpdateAllowedOverCurrentConnection() const;
258
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800259 // Global system context.
260 SystemState* system_state_;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700261
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700262 // Contains state that is relevant in the processing of the Omaha request.
263 OmahaRequestParams* params_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000264
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700265 // Pointer to the OmahaEvent info. This is an UpdateCheck request if NULL.
266 scoped_ptr<OmahaEvent> event_;
267
rspangler@google.com49fdf182009-10-10 00:57:34 +0000268 // pointer to the HttpFetcher that does the http work
269 scoped_ptr<HttpFetcher> http_fetcher_;
270
Thieu Le116fda32011-04-19 11:01:54 -0700271 // If true, only include the <ping> element in the request.
272 bool ping_only_;
273
rspangler@google.com49fdf182009-10-10 00:57:34 +0000274 // Stores the response from the omaha server
275 std::vector<char> response_buffer_;
276
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700277 // Initialized by InitPingDays to values that may be sent to Omaha
278 // as part of a ping message. Note that only positive values and -1
279 // are sent to Omaha.
280 int ping_active_days_;
281 int ping_roll_call_days_;
282
Darin Petkov6a5b3222010-07-13 14:55:28 -0700283 DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000284};
285
286} // namespace chromeos_update_engine
287
Alex Deymo759c2752014-03-17 21:09:36 -0700288#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H_