blob: 7109518f72050088e6aa8d17689bb83438b3eeb4 [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//
Darin Petkova4a8a8c2010-07-15 22:21:12 -070016
Gilad Arnoldcf175a02014-07-10 16:48:47 -070017#ifndef UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H_
18#define UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H_
Darin Petkova4a8a8c2010-07-15 22:21:12 -070019
Ben Chan9abb7632014-08-07 00:10:53 -070020#include <stdint.h>
21
Darin Petkova4a8a8c2010-07-15 22:21:12 -070022#include <string>
23
Ben Chan05735a12014-09-03 07:48:22 -070024#include <base/macros.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070025#include <base/time/time.h>
Darin Petkov49d91322010-10-25 16:34:58 -070026#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkova4a8a8c2010-07-15 22:21:12 -070027
28// This gathers local system information and prepares info used by the
29// Omaha request action.
30
31namespace chromeos_update_engine {
32
Jay Srinivasan55f50c22013-01-10 19:24:35 -080033// The default "official" Omaha update URL.
David Pursell02c18642014-11-06 11:26:11 -080034extern const char kProductionOmahaUrl[];
35
36// The autoupdate test Omaha update URL.
37extern const char kAUTestOmahaUrl[];
Jay Srinivasan55f50c22013-01-10 19:24:35 -080038
Jay Srinivasanae4697c2013-03-18 17:08:08 -070039class SystemState;
Jay Srinivasan0a708742012-03-20 11:26:12 -070040
Jay Srinivasanae4697c2013-03-18 17:08:08 -070041// This class encapsulates the data Omaha gets for the request, along with
42// essential state needed for the processing of the request/response. The
43// strings in this struct should not be XML escaped.
44//
Alex Vakulenkod2779df2014-06-16 13:19:00 -070045// TODO(jaysri): chromium-os:39752 tracks the need to rename this class to
Jay Srinivasanae4697c2013-03-18 17:08:08 -070046// reflect its lifetime more appropriately.
47class OmahaRequestParams {
48 public:
Alex Vakulenkod2779df2014-06-16 13:19:00 -070049 explicit OmahaRequestParams(SystemState* system_state)
Jay Srinivasanae4697c2013-03-18 17:08:08 -070050 : system_state_(system_state),
51 os_platform_(kOsPlatform),
52 os_version_(kOsVersion),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070053 board_app_id_(kAppId),
Jay Srinivasandb0acdf2013-04-02 14:47:45 -070054 canary_app_id_(kAppId),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070055 delta_okay_(true),
56 interactive_(false),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070057 wall_clock_based_wait_enabled_(false),
58 update_check_count_wait_enabled_(false),
59 min_update_checks_needed_(kDefaultMinUpdateChecks),
60 max_update_checks_allowed_(kDefaultMaxUpdateChecks),
61 is_powerwash_allowed_(false),
62 force_lock_down_(false),
Gilad Arnold74b5f552014-10-07 08:17:16 -070063 forced_lock_down_(false) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -070064 InitFromLsbValue();
65 }
Jay Srinivasan0a708742012-03-20 11:26:12 -070066
Jay Srinivasanae4697c2013-03-18 17:08:08 -070067 OmahaRequestParams(SystemState* system_state,
68 const std::string& in_os_platform,
Darin Petkova4a8a8c2010-07-15 22:21:12 -070069 const std::string& in_os_version,
70 const std::string& in_os_sp,
71 const std::string& in_os_board,
72 const std::string& in_app_id,
73 const std::string& in_app_version,
74 const std::string& in_app_lang,
Jay Srinivasanae4697c2013-03-18 17:08:08 -070075 const std::string& in_target_channel,
76 const std::string& in_hwid,
Chris Sosac1972482013-04-30 22:31:10 -070077 const std::string& in_fw_version,
78 const std::string& in_ec_version,
Jay Srinivasan0a708742012-03-20 11:26:12 -070079 bool in_delta_okay,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -080080 bool in_interactive,
Jay Srinivasan0a708742012-03-20 11:26:12 -070081 const std::string& in_update_url,
Gilad Arnold74b5f552014-10-07 08:17:16 -070082 const std::string& in_target_version_prefix)
Jay Srinivasanae4697c2013-03-18 17:08:08 -070083 : system_state_(system_state),
84 os_platform_(in_os_platform),
85 os_version_(in_os_version),
86 os_sp_(in_os_sp),
87 os_board_(in_os_board),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070088 board_app_id_(in_app_id),
Jay Srinivasandb0acdf2013-04-02 14:47:45 -070089 canary_app_id_(in_app_id),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070090 app_version_(in_app_version),
91 app_lang_(in_app_lang),
92 current_channel_(in_target_channel),
93 target_channel_(in_target_channel),
94 hwid_(in_hwid),
Chris Sosac1972482013-04-30 22:31:10 -070095 fw_version_(in_fw_version),
96 ec_version_(in_ec_version),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070097 delta_okay_(in_delta_okay),
98 interactive_(in_interactive),
99 update_url_(in_update_url),
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700100 target_version_prefix_(in_target_version_prefix),
101 wall_clock_based_wait_enabled_(false),
102 update_check_count_wait_enabled_(false),
103 min_update_checks_needed_(kDefaultMinUpdateChecks),
104 max_update_checks_allowed_(kDefaultMaxUpdateChecks),
105 is_powerwash_allowed_(false),
106 force_lock_down_(false),
Gilad Arnold74b5f552014-10-07 08:17:16 -0700107 forced_lock_down_(false) {}
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700108
Alex Deymoe8948702014-11-11 21:44:45 -0800109 virtual ~OmahaRequestParams() = default;
110
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700111 // Setters and getters for the various properties.
112 inline std::string os_platform() const { return os_platform_; }
113 inline std::string os_version() const { return os_version_; }
114 inline std::string os_sp() const { return os_sp_; }
115 inline std::string os_board() const { return os_board_; }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700116 inline std::string board_app_id() const { return board_app_id_; }
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700117 inline std::string canary_app_id() const { return canary_app_id_; }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700118 inline std::string app_lang() const { return app_lang_; }
119 inline std::string hwid() const { return hwid_; }
Chris Sosac1972482013-04-30 22:31:10 -0700120 inline std::string fw_version() const { return fw_version_; }
121 inline std::string ec_version() const { return ec_version_; }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700122
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700123 inline void set_app_version(const std::string& version) {
124 app_version_ = version;
125 }
126 inline std::string app_version() const { return app_version_; }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700127
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700128 inline std::string current_channel() const { return current_channel_; }
129 inline std::string target_channel() const { return target_channel_; }
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700130 inline std::string download_channel() const { return download_channel_; }
Darin Petkov49d91322010-10-25 16:34:58 -0700131
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700132 // Can client accept a delta ?
133 inline void set_delta_okay(bool ok) { delta_okay_ = ok; }
134 inline bool delta_okay() const { return delta_okay_; }
Jay Srinivasan0a708742012-03-20 11:26:12 -0700135
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700136 // True if this is a user-initiated update check.
Chris Sosa968d0572013-08-23 14:46:02 -0700137 inline void set_interactive(bool interactive) { interactive_ = interactive; }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700138 inline bool interactive() const { return interactive_; }
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700139
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700140 inline void set_update_url(const std::string& url) { update_url_ = url; }
141 inline std::string update_url() const { return update_url_; }
142
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700143 inline void set_target_version_prefix(const std::string& prefix) {
144 target_version_prefix_ = prefix;
145 }
146
147 inline std::string target_version_prefix() const {
148 return target_version_prefix_;
149 }
150
151 inline void set_wall_clock_based_wait_enabled(bool enabled) {
152 wall_clock_based_wait_enabled_ = enabled;
153 }
154 inline bool wall_clock_based_wait_enabled() const {
155 return wall_clock_based_wait_enabled_;
156 }
157
158 inline void set_waiting_period(base::TimeDelta period) {
159 waiting_period_ = period;
160 }
161 base::TimeDelta waiting_period() const { return waiting_period_; }
162
163 inline void set_update_check_count_wait_enabled(bool enabled) {
164 update_check_count_wait_enabled_ = enabled;
165 }
166
167 inline bool update_check_count_wait_enabled() const {
168 return update_check_count_wait_enabled_;
169 }
170
Ben Chan9abb7632014-08-07 00:10:53 -0700171 inline void set_min_update_checks_needed(int64_t min) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700172 min_update_checks_needed_ = min;
173 }
Ben Chan9abb7632014-08-07 00:10:53 -0700174 inline int64_t min_update_checks_needed() const {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700175 return min_update_checks_needed_;
176 }
177
Ben Chan9abb7632014-08-07 00:10:53 -0700178 inline void set_max_update_checks_allowed(int64_t max) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700179 max_update_checks_allowed_ = max;
180 }
Ben Chan9abb7632014-08-07 00:10:53 -0700181 inline int64_t max_update_checks_allowed() const {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700182 return max_update_checks_allowed_;
183 }
184
185 // True if we're trying to update to a more stable channel.
186 // i.e. index(target_channel) > index(current_channel).
Alex Deymo560ae1d2014-10-28 02:17:54 -0700187 virtual bool to_more_stable_channel() const;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700188
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700189 // Returns the app id corresponding to the current value of the
190 // download channel.
Alex Deymo560ae1d2014-10-28 02:17:54 -0700191 virtual std::string GetAppId() const;
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700192
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700193 // Suggested defaults
David Pursell02c18642014-11-06 11:26:11 -0800194 static const char kAppId[];
195 static const char kOsPlatform[];
196 static const char kOsVersion[];
197 static const char kUpdateUrl[];
198 static const char kUpdateChannelKey[];
199 static const char kIsPowerwashAllowedKey[];
200 static const char kAutoUpdateServerKey[];
Ben Chan9abb7632014-08-07 00:10:53 -0700201 static const int64_t kDefaultMinUpdateChecks = 0;
202 static const int64_t kDefaultMaxUpdateChecks = 8;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700203
Darin Petkov5a7f5652010-07-22 21:40:09 -0700204 // Initializes all the data in the object. Non-empty
205 // |in_app_version| or |in_update_url| prevents automatic detection
206 // of the parameter. Returns true on success, false otherwise.
207 bool Init(const std::string& in_app_version,
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200208 const std::string& in_update_url,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -0800209 bool in_interactive);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700210
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700211 // Permanently changes the release channel to |channel|. Performs a
212 // powerwash, if required and allowed.
213 // Returns true on success, false otherwise. Note: This call will fail if
214 // there's a channel change pending already. This is to serialize all the
215 // channel changes done by the user in order to avoid having to solve
216 // numerous edge cases around ensuring the powerwash happens as intended in
217 // all such cases.
Alex Deymo560ae1d2014-10-28 02:17:54 -0700218 virtual bool SetTargetChannel(const std::string& channel,
219 bool is_powerwash_allowed);
Darin Petkov49d91322010-10-25 16:34:58 -0700220
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700221 // Updates the download channel for this particular attempt from the current
222 // value of target channel. This method takes a "snapshot" of the current
223 // value of target channel and uses it for all subsequent Omaha requests for
224 // this attempt (i.e. initial request as well as download progress/error
225 // event requests). The snapshot will be updated only when either this method
226 // or Init is called again.
Alex Deymo560ae1d2014-10-28 02:17:54 -0700227 virtual void UpdateDownloadChannel();
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700228
Alex Deymo560ae1d2014-10-28 02:17:54 -0700229 virtual bool is_powerwash_allowed() const { return is_powerwash_allowed_; }
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900230
David Pursell02c18642014-11-06 11:26:11 -0800231 // Check if the provided update URL is official, meaning either the default
232 // autoupdate server or the autoupdate autotest server.
233 virtual bool IsUpdateUrlOfficial() const;
234
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700235 // For unit-tests.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700236 void set_root(const std::string& root);
Chris Sosa28e479c2013-07-12 11:39:53 -0700237 void set_current_channel(const std::string& channel) {
238 current_channel_ = channel;
239 }
Gilad Arnold76b2b482014-04-01 13:32:43 -0700240 void set_target_channel(const std::string& channel) {
241 target_channel_ = channel;
242 }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700243
Darin Petkov10d02dd2011-01-10 14:57:39 -0800244 // Enforce security mode for testing purposes.
245 void SetLockDown(bool lock);
Darin Petkov49d91322010-10-25 16:34:58 -0700246
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700247 private:
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700248 FRIEND_TEST(OmahaRequestParamsTest, IsValidChannelTest);
249 FRIEND_TEST(OmahaRequestParamsTest, ShouldLockDownTest);
250 FRIEND_TEST(OmahaRequestParamsTest, ChannelIndexTest);
251 FRIEND_TEST(OmahaRequestParamsTest, LsbPreserveTest);
Chris Sosac1972482013-04-30 22:31:10 -0700252 FRIEND_TEST(OmahaRequestParamsTest, CollectECFWVersionsTest);
Darin Petkov49d91322010-10-25 16:34:58 -0700253
254 // Use a validator that is a non-static member of this class so that its
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700255 // inputs can be mocked in unit tests (e.g., build type for IsValidChannel).
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700256 typedef bool(
257 OmahaRequestParams::*ValueValidator)( // NOLINT(readability/casting)
Darin Petkov49d91322010-10-25 16:34:58 -0700258 const std::string&) const;
259
Darin Petkov10d02dd2011-01-10 14:57:39 -0800260 // Returns true if parameter values should be locked down for security
261 // reasons. If this is an official build running in normal boot mode, all
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700262 // values except the release channel are parsed only from the read-only rootfs
263 // partition and the channel values are restricted to a pre-approved set.
Darin Petkov10d02dd2011-01-10 14:57:39 -0800264 bool ShouldLockDown() const;
Darin Petkov49d91322010-10-25 16:34:58 -0700265
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700266 // Returns true if |channel| is a valid channel, false otherwise. This method
267 // restricts the channel value only if the image is official (see
Darin Petkov49d91322010-10-25 16:34:58 -0700268 // IsOfficialBuild).
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700269 bool IsValidChannel(const std::string& channel) const;
270
271 // Returns the index of the given channel.
272 int GetChannelIndex(const std::string& channel) const;
273
Chris Sosac1972482013-04-30 22:31:10 -0700274 // Returns True if we should store the fw/ec versions based on our hwid_.
275 // Compares hwid to a set of whitelisted prefixes.
276 bool CollectECFWVersions() const;
277
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700278 // These are individual helper methods to initialize the said properties from
279 // the LSB value.
280 void SetTargetChannelFromLsbValue();
281 void SetCurrentChannelFromLsbValue();
282 void SetIsPowerwashAllowedFromLsbValue();
283
284 // Initializes the required properties from the LSB value.
285 void InitFromLsbValue();
Darin Petkov49d91322010-10-25 16:34:58 -0700286
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700287 // Fetches the value for a given key from
Darin Petkova3df55b2010-11-15 13:33:55 -0800288 // /mnt/stateful_partition/etc/lsb-release if possible and |stateful_override|
289 // is true. Failing that, it looks for the key in /etc/lsb-release. If
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700290 // |validator| is non-null, uses it to validate and ignore invalid values.
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700291 std::string GetLsbValue(const std::string& key,
Darin Petkov49d91322010-10-25 16:34:58 -0700292 const std::string& default_value,
Darin Petkova3df55b2010-11-15 13:33:55 -0800293 ValueValidator validator,
294 bool stateful_override) const;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700295
296 // Gets the machine type (e.g. "i686").
297 std::string GetMachineType() const;
298
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700299 // Global system context.
300 SystemState* system_state_;
301
302 // Basic properties of the OS and Application that go into the Omaha request.
303 std::string os_platform_;
304 std::string os_version_;
305 std::string os_sp_;
306 std::string os_board_;
307
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700308 // The board app id identifies the app id for the board irrespective of the
309 // channel that we're on. The canary app id identifies the app id to be used
310 // iff we're in the canary-channel. These values could be different depending
311 // on how the release tools are implemented.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700312 std::string board_app_id_;
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700313 std::string canary_app_id_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700314
315 std::string app_version_;
316 std::string app_lang_;
317
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700318 // The three channel values we deal with.
319 // Current channel: is always the channel from /etc/lsb-release. It never
320 // changes. It's just read in during initialization.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700321 std::string current_channel_;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700322
323 // Target channel: It starts off with the value of current channel. But if
324 // the user changes the channel, then it'll have a different value. If the
325 // user changes multiple times, target channel will always contain the most
326 // recent change and is updated immediately to the user-selected value even
327 // if we're in the middle of a download (as opposed to download channel
328 // which gets updated only at the start of next download)
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700329 std::string target_channel_;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700330
331 // The channel from which we're downloading the payload. This should normally
332 // be the same as target channel. But if the user made another channel change
333 // we started the download, then they'd be different, in which case, we'd
334 // detect elsewhere that the target channel has been changed and cancel the
335 // current download attempt.
336 std::string download_channel_;
337
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700338 std::string hwid_; // Hardware Qualification ID of the client
Chris Sosac1972482013-04-30 22:31:10 -0700339 std::string fw_version_; // Chrome OS Firmware Version.
340 std::string ec_version_; // Chrome OS EC Version.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700341 bool delta_okay_; // If this client can accept a delta
342 bool interactive_; // Whether this is a user-initiated update check
343
344 // The URL to send the Omaha request to.
345 std::string update_url_;
346
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700347 // Prefix of the target OS version that the enterprise wants this device
348 // to be pinned to. It's empty otherwise.
349 std::string target_version_prefix_;
350
351 // True if scattering is enabled, in which case waiting_period_ specifies the
352 // amount of absolute time that we've to wait for before sending a request to
353 // Omaha.
354 bool wall_clock_based_wait_enabled_;
355 base::TimeDelta waiting_period_;
356
357 // True if scattering is enabled to denote the number of update checks
358 // we've to skip before we can send a request to Omaha. The min and max
359 // values establish the bounds for a random number to be chosen within that
360 // range to enable such a wait.
361 bool update_check_count_wait_enabled_;
Ben Chan9abb7632014-08-07 00:10:53 -0700362 int64_t min_update_checks_needed_;
363 int64_t max_update_checks_allowed_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700364
365 // True if we are allowed to do powerwash, if required, on a channel change.
366 bool is_powerwash_allowed_;
367
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700368 // When reading files, prepend root_ to the paths. Useful for testing.
369 std::string root_;
370
Darin Petkov10d02dd2011-01-10 14:57:39 -0800371 // Force security lock down for testing purposes.
372 bool force_lock_down_;
373 bool forced_lock_down_;
Darin Petkov49d91322010-10-25 16:34:58 -0700374
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700375 // TODO(jaysri): Uncomment this after fixing unit tests, as part of
376 // chromium-os:39752
377 // DISALLOW_COPY_AND_ASSIGN(OmahaRequestParams);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700378};
379
380} // namespace chromeos_update_engine
381
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700382#endif // UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H_