blob: f1b81ec40f6cd1d67a4f7a14df36c5f775977732 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
rspangler@google.com49fdf182009-10-10 00:57:34 +000016
Gilad Arnoldcf175a02014-07-10 16:48:47 -070017#ifndef UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H_
18#define UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H_
rspangler@google.com49fdf182009-10-10 00:57:34 +000019
Alex Vakulenko44cab302014-07-23 13:12:15 -070020#include <fcntl.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000021#include <sys/stat.h>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070022#include <sys/types.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000023
Ben Chan02f7c1d2014-10-18 15:18:02 -070024#include <memory>
rspangler@google.com49fdf182009-10-10 00:57:34 +000025#include <string>
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080026#include <vector>
rspangler@google.com49fdf182009-10-10 00:57:34 +000027
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070028#include <brillo/secure_blob.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000029#include <curl/curl.h>
30
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070031#include "update_engine/action.h"
32#include "update_engine/http_fetcher.h"
Jay Srinivasan08262882012-12-28 19:29:43 -080033#include "update_engine/omaha_response.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000034
Darin Petkov6a5b3222010-07-13 14:55:28 -070035// The Omaha Request action makes a request to Omaha and can output
36// the response on the output ActionPipe.
rspangler@google.com49fdf182009-10-10 00:57:34 +000037
rspangler@google.com49fdf182009-10-10 00:57:34 +000038namespace chromeos_update_engine {
39
Alex Deymob0d74eb2015-03-30 17:59:17 -070040// Encodes XML entities in a given string. Input must be ASCII-7 valid. If
41// the input is invalid, the default value is used instead.
42std::string XmlEncodeWithDefault(const std::string& input,
43 const std::string& default_value);
44
45// Escapes text so it can be included as character data and attribute
46// values. The |input| string must be valid ASCII-7, no UTF-8 supported.
47// Returns whether the |input| was valid and escaped properly in |output|.
48bool XmlEncode(const std::string& input, std::string* output);
rspangler@google.com49fdf182009-10-10 00:57:34 +000049
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070050// This struct encapsulates the Omaha event information. For a
51// complete list of defined event types and results, see
52// http://code.google.com/p/omaha/wiki/ServerProtocol#event
53struct OmahaEvent {
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070054 // The Type values correspond to EVENT_TYPE values of Omaha.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070055 enum Type {
56 kTypeUnknown = 0,
57 kTypeDownloadComplete = 1,
58 kTypeInstallComplete = 2,
59 kTypeUpdateComplete = 3,
Darin Petkov8c2980e2010-07-16 15:16:49 -070060 kTypeUpdateDownloadStarted = 13,
61 kTypeUpdateDownloadFinished = 14,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070062 };
63
Jay Srinivasan56d5aa42012-03-26 14:27:59 -070064 // The Result values correspond to EVENT_RESULT values of Omaha.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070065 enum Result {
66 kResultError = 0,
67 kResultSuccess = 1,
Darin Petkov95508da2011-01-05 12:42:29 -080068 kResultSuccessReboot = 2,
Alex Vakulenkod2779df2014-06-16 13:19:00 -070069 kResultUpdateDeferred = 9, // When we ignore/defer updates due to policy.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070070 };
71
72 OmahaEvent()
73 : type(kTypeUnknown),
74 result(kResultError),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070075 error_code(ErrorCode::kError) {}
Darin Petkove17f86b2010-07-20 09:12:01 -070076 explicit OmahaEvent(Type in_type)
77 : type(in_type),
78 result(kResultSuccess),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070079 error_code(ErrorCode::kSuccess) {}
David Zeuthena99981f2013-04-29 13:42:47 -070080 OmahaEvent(Type in_type, Result in_result, ErrorCode in_error_code)
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070081 : type(in_type),
82 result(in_result),
83 error_code(in_error_code) {}
84
85 Type type;
86 Result result;
David Zeuthena99981f2013-04-29 13:42:47 -070087 ErrorCode error_code;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070088};
89
rspangler@google.com49fdf182009-10-10 00:57:34 +000090class NoneType;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070091class OmahaRequestAction;
Yunlian Jianga178e5e2013-04-05 14:41:56 -070092class OmahaRequestParams;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070093class PrefsInterface;
rspangler@google.com49fdf182009-10-10 00:57:34 +000094
David Zeuthene8ed8632014-07-24 13:38:10 -040095// This struct is declared in the .cc file.
96struct OmahaParserData;
97
rspangler@google.com49fdf182009-10-10 00:57:34 +000098template<>
Darin Petkov6a5b3222010-07-13 14:55:28 -070099class ActionTraits<OmahaRequestAction> {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000100 public:
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700101 // Takes parameters on the input pipe.
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700102 typedef NoneType InputObjectType;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700103 // On UpdateCheck success, puts the Omaha response on output. Event
104 // requests do not have an output pipe.
Darin Petkov6a5b3222010-07-13 14:55:28 -0700105 typedef OmahaResponse OutputObjectType;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000106};
107
Darin Petkov6a5b3222010-07-13 14:55:28 -0700108class OmahaRequestAction : public Action<OmahaRequestAction>,
109 public HttpFetcherDelegate {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000110 public:
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700111 static const int kNeverPinged = -1;
112 static const int kPingTimeJump = -2;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800113 // We choose this value of 10 as a heuristic for a work day in trying
114 // each URL, assuming we check roughly every 45 mins. This is a good time to
115 // wait - neither too long nor too little - so we don't give up the preferred
116 // URLs that appear earlier in list too quickly before moving on to the
117 // fallback ones.
118 static const int kDefaultMaxFailureCountPerUrl = 10;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700119
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700120 // These are the possible outcome upon checking whether we satisfied
121 // the wall-clock-based-wait.
122 enum WallClockWaitResult {
123 kWallClockWaitNotSatisfied,
124 kWallClockWaitDoneButUpdateCheckWaitRequired,
125 kWallClockWaitDoneAndUpdateCheckWaitNotRequired,
126 };
127
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700128 // The ctor takes in all the parameters that will be used for making
129 // the request to Omaha. For some of them we have constants that
130 // should be used.
131 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000132 // Takes ownership of the passed in HttpFetcher. Useful for testing.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700133 //
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700134 // Takes ownership of the passed in OmahaEvent. If |event| is null,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700135 // this is an UpdateCheck request, otherwise it's an Event request.
136 // Event requests always succeed.
137 //
rspangler@google.com49fdf182009-10-10 00:57:34 +0000138 // A good calling pattern is:
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700139 // OmahaRequestAction(..., new OmahaEvent(...), new WhateverHttpFetcher);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700140 // or
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700141 // OmahaRequestAction(..., nullptr, new WhateverHttpFetcher);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800142 OmahaRequestAction(SystemState* system_state,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700143 OmahaEvent* event,
Thieu Le116fda32011-04-19 11:01:54 -0700144 HttpFetcher* http_fetcher,
145 bool ping_only);
Alex Deymo610277e2014-11-11 21:18:11 -0800146 ~OmahaRequestAction() override;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700147 typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType;
148 typedef ActionTraits<OmahaRequestAction>::OutputObjectType OutputObjectType;
Alex Deymo610277e2014-11-11 21:18:11 -0800149 void PerformAction() override;
150 void TerminateProcessing() override;
151 void ActionCompleted(ErrorCode code) override;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000152
Darin Petkov1023a602010-08-30 13:47:51 -0700153 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
154
rspangler@google.com49fdf182009-10-10 00:57:34 +0000155 // Debugging/logging
Darin Petkov6a5b3222010-07-13 14:55:28 -0700156 static std::string StaticType() { return "OmahaRequestAction"; }
Alex Deymo610277e2014-11-11 21:18:11 -0800157 std::string Type() const override { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000158
159 // Delegate methods (see http_fetcher.h)
Alex Deymo610277e2014-11-11 21:18:11 -0800160 void ReceivedBytes(HttpFetcher *fetcher,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800161 const void* bytes, size_t length) override;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000162
Alex Deymo610277e2014-11-11 21:18:11 -0800163 void TransferComplete(HttpFetcher *fetcher, bool successful) override;
164
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700165 // Returns true if this is an Event request, false if it's an UpdateCheck.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700166 bool IsEvent() const { return event_.get() != nullptr; }
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700167
rspangler@google.com49fdf182009-10-10 00:57:34 +0000168 private:
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700169 FRIEND_TEST(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE);
170 FRIEND_TEST(OmahaRequestActionTest,
171 GetInstallDateWhenOOBECompletedWithInvalidDate);
172 FRIEND_TEST(OmahaRequestActionTest,
173 GetInstallDateWhenOOBECompletedWithValidDate);
174 FRIEND_TEST(OmahaRequestActionTest,
175 GetInstallDateWhenOOBECompletedDateChanges);
David Zeuthen639aa362014-02-03 16:23:44 -0800176
177 // Enumeration used in PersistInstallDate().
178 enum InstallDateProvisioningSource {
179 kProvisionedFromOmahaResponse,
180 kProvisionedFromOOBEMarker,
181
182 // kProvisionedMax is the count of the number of enums above. Add
183 // any new enums above this line only.
184 kProvisionedMax
185 };
186
187 // Gets the install date, expressed as the number of PST8PDT
188 // calendar weeks since January 1st 2007, times seven. Returns -1 if
189 // unknown. See http://crbug.com/336838 for details about this value.
190 static int GetInstallDate(SystemState* system_state);
191
192 // Parses the Omaha Response in |doc| and sets the
193 // |install_date_days| field of |output_object| to the value of the
194 // elapsed_days attribute of the daystart element. Returns True if
195 // the value was set, False if it wasn't found.
David Zeuthene8ed8632014-07-24 13:38:10 -0400196 static bool ParseInstallDate(OmahaParserData* parser_data,
David Zeuthen639aa362014-02-03 16:23:44 -0800197 OmahaResponse* output_object);
198
199 // Returns True if the kPrefsInstallDateDays state variable is set,
200 // False otherwise.
201 static bool HasInstallDate(SystemState *system_state);
202
203 // Writes |install_date_days| into the kPrefsInstallDateDays state
204 // variable and emits an UMA stat for the |source| used. Returns
205 // True if the value was written, False if an error occurred.
206 static bool PersistInstallDate(SystemState *system_state,
207 int install_date_days,
208 InstallDateProvisioningSource source);
209
Alex Deymo8e18f932015-03-27 16:16:59 -0700210 // Persist the new cohort* value received in the XML file in the |prefs_key|
211 // preference file. If the |new_value| is empty, the currently stored value
212 // will be deleted. Don't call this function with an empty |new_value| if the
213 // value was not set in the XML, since that would delete the stored value.
214 bool PersistCohortData(const std::string& prefs_key,
215 const std::string& new_value);
216
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700217 // If this is an update check request, initializes
218 // |ping_active_days_| and |ping_roll_call_days_| to values that may
219 // be sent as pings to Omaha.
220 void InitPingDays();
221
Darin Petkov84c763c2010-07-29 16:27:58 -0700222 // Based on the persistent preference store values, calculates the
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700223 // number of days since the last ping sent for |key|.
224 int CalculatePingDays(const std::string& key);
225
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700226 // Returns whether we have "active_days" or "roll_call_days" ping values to
227 // send to Omaha and thus we should include them in the response.
228 bool ShouldPing() const;
229
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700230 // Returns true if the download of a new update should be deferred.
231 // False if the update can be downloaded.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700232 bool ShouldDeferDownload(OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700233
234 // Returns true if the basic wall-clock-based waiting period has been
235 // satisfied based on the scattering policy setting. False otherwise.
236 // If true, it also indicates whether the additional update-check-count-based
237 // waiting period also needs to be satisfied before the download can begin.
238 WallClockWaitResult IsWallClockBasedWaitingSatisfied(
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700239 OmahaResponse* output_object);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700240
241 // Returns true if the update-check-count-based waiting period has been
242 // satisfied. False otherwise.
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700243 bool IsUpdateCheckCountBasedWaitingSatisfied();
244
245 // Parses the response from Omaha that's available in |doc| using the other
246 // helper methods below and populates the |output_object| with the relevant
247 // values. Returns true if we should continue the parsing. False otherwise,
248 // in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400249 bool ParseResponse(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700250 OmahaResponse* output_object,
251 ScopedActionCompleter* completer);
252
253 // Parses the status property in the given update_check_node and populates
254 // |output_object| if valid. Returns true if we should continue the parsing.
255 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400256 bool ParseStatus(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700257 OmahaResponse* output_object,
258 ScopedActionCompleter* completer);
259
260 // Parses the URL nodes in the given XML document and populates
261 // |output_object| if valid. Returns true if we should continue the parsing.
262 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400263 bool ParseUrls(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700264 OmahaResponse* output_object,
265 ScopedActionCompleter* completer);
266
267 // Parses the package node in the given XML document and populates
268 // |output_object| if valid. Returns true if we should continue the parsing.
269 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400270 bool ParsePackage(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700271 OmahaResponse* output_object,
272 ScopedActionCompleter* completer);
273
274 // Parses the other parameters in the given XML document and populates
275 // |output_object| if valid. Returns true if we should continue the parsing.
276 // False otherwise, in which case it sets any error code using |completer|.
David Zeuthene8ed8632014-07-24 13:38:10 -0400277 bool ParseParams(OmahaParserData* parser_data,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700278 OmahaResponse* output_object,
279 ScopedActionCompleter* completer);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700280
David Zeuthen8f191b22013-08-06 12:27:50 -0700281 // Called by TransferComplete() to complete processing, either
282 // asynchronously after looking up resources via p2p or directly.
283 void CompleteProcessing();
284
285 // Helper to asynchronously look up payload on the LAN.
286 void LookupPayloadViaP2P(const OmahaResponse& response);
287
288 // Callback used by LookupPayloadViaP2P().
289 void OnLookupPayloadViaP2PCompleted(const std::string& url);
290
Chris Sosa77f79e82014-06-02 18:16:24 -0700291 // Returns true if the current update should be ignored.
292 bool ShouldIgnoreUpdate(const OmahaResponse& response) const;
293
294 // Returns true if updates are allowed over the current type of connection.
295 // False otherwise.
296 bool IsUpdateAllowedOverCurrentConnection() const;
297
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800298 // Global system context.
299 SystemState* system_state_;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700300
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700301 // Contains state that is relevant in the processing of the Omaha request.
302 OmahaRequestParams* params_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000303
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700304 // Pointer to the OmahaEvent info. This is an UpdateCheck request if null.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700305 std::unique_ptr<OmahaEvent> event_;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700306
rspangler@google.com49fdf182009-10-10 00:57:34 +0000307 // pointer to the HttpFetcher that does the http work
Ben Chan02f7c1d2014-10-18 15:18:02 -0700308 std::unique_ptr<HttpFetcher> http_fetcher_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000309
Thieu Le116fda32011-04-19 11:01:54 -0700310 // If true, only include the <ping> element in the request.
311 bool ping_only_;
312
rspangler@google.com49fdf182009-10-10 00:57:34 +0000313 // Stores the response from the omaha server
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700314 brillo::Blob response_buffer_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000315
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700316 // Initialized by InitPingDays to values that may be sent to Omaha
317 // as part of a ping message. Note that only positive values and -1
318 // are sent to Omaha.
319 int ping_active_days_;
320 int ping_roll_call_days_;
321
Darin Petkov6a5b3222010-07-13 14:55:28 -0700322 DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000323};
324
325} // namespace chromeos_update_engine
326
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700327#endif // UPDATE_ENGINE_OMAHA_REQUEST_ACTION_H_