blob: b9f70f04feda2d163f54b3eb9a5ddb05a0616a91 [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
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
David Zeuthene8ed8632014-07-24 13:38:10 -040077// This struct is declared in the .cc file.
78struct OmahaParserData;
79
rspangler@google.com49fdf182009-10-10 00:57:34 +000080template<>
Darin Petkov6a5b3222010-07-13 14:55:28 -070081class ActionTraits<OmahaRequestAction> {
rspangler@google.com49fdf182009-10-10 00:57:34 +000082 public:
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070083 // Takes parameters on the input pipe.
Darin Petkova4a8a8c2010-07-15 22:21:12 -070084 typedef NoneType InputObjectType;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070085 // On UpdateCheck success, puts the Omaha response on output. Event
86 // requests do not have an output pipe.
Darin Petkov6a5b3222010-07-13 14:55:28 -070087 typedef OmahaResponse OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +000088};
89
Darin Petkov6a5b3222010-07-13 14:55:28 -070090class OmahaRequestAction : public Action<OmahaRequestAction>,
91 public HttpFetcherDelegate {
rspangler@google.com49fdf182009-10-10 00:57:34 +000092 public:
Darin Petkov1cbd78f2010-07-29 12:38:34 -070093 static const int kNeverPinged = -1;
94 static const int kPingTimeJump = -2;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080095 // We choose this value of 10 as a heuristic for a work day in trying
96 // each URL, assuming we check roughly every 45 mins. This is a good time to
97 // wait - neither too long nor too little - so we don't give up the preferred
98 // URLs that appear earlier in list too quickly before moving on to the
99 // fallback ones.
100 static const int kDefaultMaxFailureCountPerUrl = 10;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700101
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700102 // These are the possible outcome upon checking whether we satisfied
103 // the wall-clock-based-wait.
104 enum WallClockWaitResult {
105 kWallClockWaitNotSatisfied,
106 kWallClockWaitDoneButUpdateCheckWaitRequired,
107 kWallClockWaitDoneAndUpdateCheckWaitNotRequired,
108 };
109
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700110 // The ctor takes in all the parameters that will be used for making
111 // the request to Omaha. For some of them we have constants that
112 // should be used.
113 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000114 // Takes ownership of the passed in HttpFetcher. Useful for testing.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700115 //
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700116 // Takes ownership of the passed in OmahaEvent. If |event| is null,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700117 // this is an UpdateCheck request, otherwise it's an Event request.
118 // Event requests always succeed.
119 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000120 // A good calling pattern is:
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700121 // OmahaRequestAction(..., new OmahaEvent(...), new WhateverHttpFetcher);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700122 // or
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700123 // OmahaRequestAction(..., nullptr, new WhateverHttpFetcher);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800124 OmahaRequestAction(SystemState* system_state,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700125 OmahaEvent* event,
Thieu Le116fda32011-04-19 11:01:54 -0700126 HttpFetcher* http_fetcher,
127 bool ping_only);
Alex Deymo610277e2014-11-11 21:18:11 -0800128 ~OmahaRequestAction() override;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700129 typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType;
130 typedef ActionTraits<OmahaRequestAction>::OutputObjectType OutputObjectType;
Alex Deymo610277e2014-11-11 21:18:11 -0800131 void PerformAction() override;
132 void TerminateProcessing() override;
133 void ActionCompleted(ErrorCode code) override;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000134
Darin Petkov1023a602010-08-30 13:47:51 -0700135 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
136
rspangler@google.com49fdf182009-10-10 00:57:34 +0000137 // Debugging/logging
Darin Petkov6a5b3222010-07-13 14:55:28 -0700138 static std::string StaticType() { return "OmahaRequestAction"; }
Alex Deymo610277e2014-11-11 21:18:11 -0800139 std::string Type() const override { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000140
141 // Delegate methods (see http_fetcher.h)
Alex Deymo610277e2014-11-11 21:18:11 -0800142 void ReceivedBytes(HttpFetcher *fetcher,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800143 const void* bytes, size_t length) override;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000144
Alex Deymo610277e2014-11-11 21:18:11 -0800145 void TransferComplete(HttpFetcher *fetcher, bool successful) override;
146
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700147 // Returns true if this is an Event request, false if it's an UpdateCheck.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700148 bool IsEvent() const { return event_.get() != nullptr; }
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700149
rspangler@google.com49fdf182009-10-10 00:57:34 +0000150 private:
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700151 FRIEND_TEST(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE);
152 FRIEND_TEST(OmahaRequestActionTest,
153 GetInstallDateWhenOOBECompletedWithInvalidDate);
154 FRIEND_TEST(OmahaRequestActionTest,
155 GetInstallDateWhenOOBECompletedWithValidDate);
156 FRIEND_TEST(OmahaRequestActionTest,
157 GetInstallDateWhenOOBECompletedDateChanges);
David Zeuthen639aa362014-02-03 16:23:44 -0800158
159 // Enumeration used in PersistInstallDate().
160 enum InstallDateProvisioningSource {
161 kProvisionedFromOmahaResponse,
162 kProvisionedFromOOBEMarker,
163
164 // kProvisionedMax is the count of the number of enums above. Add
165 // any new enums above this line only.
166 kProvisionedMax
167 };
168
169 // Gets the install date, expressed as the number of PST8PDT
170 // calendar weeks since January 1st 2007, times seven. Returns -1 if
171 // unknown. See http://crbug.com/336838 for details about this value.
172 static int GetInstallDate(SystemState* system_state);
173
174 // Parses the Omaha Response in |doc| and sets the
175 // |install_date_days| field of |output_object| to the value of the
176 // elapsed_days attribute of the daystart element. Returns True if
177 // the value was set, False if it wasn't found.
David Zeuthene8ed8632014-07-24 13:38:10 -0400178 static bool ParseInstallDate(OmahaParserData* parser_data,
David Zeuthen639aa362014-02-03 16:23:44 -0800179 OmahaResponse* output_object);
180
181 // Returns True if the kPrefsInstallDateDays state variable is set,
182 // False otherwise.
183 static bool HasInstallDate(SystemState *system_state);
184
185 // Writes |install_date_days| into the kPrefsInstallDateDays state
186 // variable and emits an UMA stat for the |source| used. Returns
187 // True if the value was written, False if an error occurred.
188 static bool PersistInstallDate(SystemState *system_state,
189 int install_date_days,
190 InstallDateProvisioningSource source);
191
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700192 // If this is an update check request, initializes
193 // |ping_active_days_| and |ping_roll_call_days_| to values that may
194 // be sent as pings to Omaha.
195 void InitPingDays();
196
Darin Petkov84c763c2010-07-29 16:27:58 -0700197 // Based on the persistent preference store values, calculates the
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700198 // number of days since the last ping sent for |key|.
199 int CalculatePingDays(const std::string& key);
200
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700201 // Returns whether we have "active_days" or "roll_call_days" ping values to
202 // send to Omaha and thus we should include them in the response.
203 bool ShouldPing() const;
204
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700205 // Returns true if the download of a new update should be deferred.
206 // False if the update can be downloaded.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700207 bool ShouldDeferDownload(OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700208
209 // Returns true if the basic wall-clock-based waiting period has been
210 // satisfied based on the scattering policy setting. False otherwise.
211 // If true, it also indicates whether the additional update-check-count-based
212 // waiting period also needs to be satisfied before the download can begin.
213 WallClockWaitResult IsWallClockBasedWaitingSatisfied(
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700214 OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700215
216 // Returns true if the update-check-count-based waiting period has been
217 // satisfied. False otherwise.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700218 bool IsUpdateCheckCountBasedWaitingSatisfied();
219
220 // Parses the response from Omaha that's available in |doc| using the other
221 // helper methods below and populates the |output_object| with the relevant
222 // values. Returns true if we should continue the parsing. False otherwise,
223 // in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400224 bool ParseResponse(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700225 OmahaResponse* output_object,
226 ScopedActionCompleter* completer);
227
228 // Parses the status property in the given update_check_node 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|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400231 bool ParseStatus(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700232 OmahaResponse* output_object,
233 ScopedActionCompleter* completer);
234
235 // Parses the URL nodes 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|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400238 bool ParseUrls(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700239 OmahaResponse* output_object,
240 ScopedActionCompleter* completer);
241
242 // Parses the package node in the given XML document and populates
243 // |output_object| if valid. Returns true if we should continue the parsing.
244 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400245 bool ParsePackage(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700246 OmahaResponse* output_object,
247 ScopedActionCompleter* completer);
248
249 // Parses the other parameters in the given XML document and populates
250 // |output_object| if valid. Returns true if we should continue the parsing.
251 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400252 bool ParseParams(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700253 OmahaResponse* output_object,
254 ScopedActionCompleter* completer);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700255
David Zeuthen8f191b22013-08-06 12:27:50 -0700256 // Called by TransferComplete() to complete processing, either
257 // asynchronously after looking up resources via p2p or directly.
258 void CompleteProcessing();
259
260 // Helper to asynchronously look up payload on the LAN.
261 void LookupPayloadViaP2P(const OmahaResponse& response);
262
263 // Callback used by LookupPayloadViaP2P().
264 void OnLookupPayloadViaP2PCompleted(const std::string& url);
265
Chris Sosa77f79e82014-06-02 18:16:24 -0700266 // Returns true if the current update should be ignored.
267 bool ShouldIgnoreUpdate(const OmahaResponse& response) const;
268
269 // Returns true if updates are allowed over the current type of connection.
270 // False otherwise.
271 bool IsUpdateAllowedOverCurrentConnection() const;
272
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800273 // Global system context.
274 SystemState* system_state_;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700275
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700276 // Contains state that is relevant in the processing of the Omaha request.
277 OmahaRequestParams* params_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000278
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700279 // Pointer to the OmahaEvent info. This is an UpdateCheck request if null.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700280 std::unique_ptr<OmahaEvent> event_;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700281
rspangler@google.com49fdf182009-10-10 00:57:34 +0000282 // pointer to the HttpFetcher that does the http work
Ben Chan02f7c1d2014-10-18 15:18:02 -0700283 std::unique_ptr<HttpFetcher> http_fetcher_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000284
Thieu Le116fda32011-04-19 11:01:54 -0700285 // If true, only include the <ping> element in the request.
286 bool ping_only_;
287
rspangler@google.com49fdf182009-10-10 00:57:34 +0000288 // Stores the response from the omaha server
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800289 chromeos::Blob response_buffer_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000290
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700291 // Initialized by InitPingDays to values that may be sent to Omaha
292 // as part of a ping message. Note that only positive values and -1
293 // are sent to Omaha.
294 int ping_active_days_;
295 int ping_roll_call_days_;
296
Darin Petkov6a5b3222010-07-13 14:55:28 -0700297 DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000298};
299
300} // namespace chromeos_update_engine
301
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700302#endif // UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H_